LLVM  5.0.0
MipsOptimizePICCall.cpp
Go to the documentation of this file.
1 //===--------- MipsOptimizePICCall.cpp - Optimize PIC Calls ---------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass eliminates unnecessary instructions that set up $gp and replace
11 // instructions that load target function addresses with copy instructions.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "Mips.h"
17 #include "MipsMachineFunction.h"
18 #include "MipsTargetMachine.h"
23 
24 using namespace llvm;
25 
26 #define DEBUG_TYPE "optimize-mips-pic-call"
27 
28 static cl::opt<bool> LoadTargetFromGOT("mips-load-target-from-got",
29  cl::init(true),
30  cl::desc("Load target address from GOT"),
31  cl::Hidden);
32 
33 static cl::opt<bool> EraseGPOpnd("mips-erase-gp-opnd",
34  cl::init(true), cl::desc("Erase GP Operand"),
35  cl::Hidden);
36 
37 namespace {
39 
40 typedef std::pair<unsigned, unsigned> CntRegP;
43 AllocatorTy;
45  AllocatorTy> ScopedHTType;
46 
47 class MBBInfo {
48 public:
49  MBBInfo(MachineDomTreeNode *N);
50  const MachineDomTreeNode *getNode() const;
51  bool isVisited() const;
52  void preVisit(ScopedHTType &ScopedHT);
53  void postVisit();
54 
55 private:
56  MachineDomTreeNode *Node;
57  ScopedHTType::ScopeTy *HTScope;
58 };
59 
60 class OptimizePICCall : public MachineFunctionPass {
61 public:
62  OptimizePICCall() : MachineFunctionPass(ID) {}
63 
64  StringRef getPassName() const override { return "Mips OptimizePICCall"; }
65 
66  bool runOnMachineFunction(MachineFunction &F) override;
67 
68  void getAnalysisUsage(AnalysisUsage &AU) const override {
71  }
72 
73 private:
74  /// \brief Visit MBB.
75  bool visitNode(MBBInfo &MBBI);
76 
77  /// \brief Test if MI jumps to a function via a register.
78  ///
79  /// Also, return the virtual register containing the target function's address
80  /// and the underlying object in Reg and Val respectively, if the function's
81  /// address can be resolved lazily.
82  bool isCallViaRegister(MachineInstr &MI, unsigned &Reg,
83  ValueType &Val) const;
84 
85  /// \brief Return the number of instructions that dominate the current
86  /// instruction and load the function address from object Entry.
87  unsigned getCount(ValueType Entry);
88 
89  /// \brief Return the destination virtual register of the last instruction
90  /// that loads from object Entry.
91  unsigned getReg(ValueType Entry);
92 
93  /// \brief Update ScopedHT.
94  void incCntAndSetReg(ValueType Entry, unsigned Reg);
95 
96  ScopedHTType ScopedHT;
97  static char ID;
98 };
99 
100 char OptimizePICCall::ID = 0;
101 } // end of anonymous namespace
102 
103 /// Return the first MachineOperand of MI if it is a used virtual register.
105  if (MI.getNumOperands() == 0)
106  return nullptr;
107 
108  MachineOperand &MO = MI.getOperand(0);
109 
110  if (!MO.isReg() || !MO.isUse() ||
112  return nullptr;
113 
114  return &MO;
115 }
116 
117 /// Return type of register Reg.
119  const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
120  const TargetRegisterClass *RC = MF.getRegInfo().getRegClass(Reg);
121  assert(TRI.legalclasstypes_end(*RC) - TRI.legalclasstypes_begin(*RC) == 1);
122  return *TRI.legalclasstypes_begin(*RC);
123 }
124 
125 /// Do the following transformation:
126 ///
127 /// jalr $vreg
128 /// =>
129 /// copy $t9, $vreg
130 /// jalr $t9
133  MachineFunction &MF = *MBB->getParent();
134  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
135  unsigned SrcReg = I->getOperand(0).getReg();
136  unsigned DstReg = getRegTy(SrcReg, MF) == MVT::i32 ? Mips::T9 : Mips::T9_64;
137  BuildMI(*MBB, I, I->getDebugLoc(), TII.get(TargetOpcode::COPY), DstReg)
138  .addReg(SrcReg);
139  I->getOperand(0).setReg(DstReg);
140 }
141 
142 /// Search MI's operands for register GP and erase it.
143 static void eraseGPOpnd(MachineInstr &MI) {
144  if (!EraseGPOpnd)
145  return;
146 
147  MachineFunction &MF = *MI.getParent()->getParent();
149  unsigned Reg = Ty == MVT::i32 ? Mips::GP : Mips::GP_64;
150 
151  for (unsigned I = 0; I < MI.getNumOperands(); ++I) {
152  MachineOperand &MO = MI.getOperand(I);
153  if (MO.isReg() && MO.getReg() == Reg) {
154  MI.RemoveOperand(I);
155  return;
156  }
157  }
158 
159  llvm_unreachable(nullptr);
160 }
161 
162 MBBInfo::MBBInfo(MachineDomTreeNode *N) : Node(N), HTScope(nullptr) {}
163 
164 const MachineDomTreeNode *MBBInfo::getNode() const { return Node; }
165 
166 bool MBBInfo::isVisited() const { return HTScope; }
167 
168 void MBBInfo::preVisit(ScopedHTType &ScopedHT) {
169  HTScope = new ScopedHTType::ScopeTy(ScopedHT);
170 }
171 
172 void MBBInfo::postVisit() {
173  delete HTScope;
174 }
175 
176 // OptimizePICCall methods.
177 bool OptimizePICCall::runOnMachineFunction(MachineFunction &F) {
178  if (static_cast<const MipsSubtarget &>(F.getSubtarget()).inMips16Mode())
179  return false;
180 
181  // Do a pre-order traversal of the dominator tree.
182  MachineDominatorTree *MDT = &getAnalysis<MachineDominatorTree>();
183  bool Changed = false;
184 
185  SmallVector<MBBInfo, 8> WorkList(1, MBBInfo(MDT->getRootNode()));
186 
187  while (!WorkList.empty()) {
188  MBBInfo &MBBI = WorkList.back();
189 
190  // If this MBB has already been visited, destroy the scope for the MBB and
191  // pop it from the work list.
192  if (MBBI.isVisited()) {
193  MBBI.postVisit();
194  WorkList.pop_back();
195  continue;
196  }
197 
198  // Visit the MBB and add its children to the work list.
199  MBBI.preVisit(ScopedHT);
200  Changed |= visitNode(MBBI);
201  const MachineDomTreeNode *Node = MBBI.getNode();
202  const std::vector<MachineDomTreeNode *> &Children = Node->getChildren();
203  WorkList.append(Children.begin(), Children.end());
204  }
205 
206  return Changed;
207 }
208 
209 bool OptimizePICCall::visitNode(MBBInfo &MBBI) {
210  bool Changed = false;
211  MachineBasicBlock *MBB = MBBI.getNode()->getBlock();
212 
213  for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
214  ++I) {
215  unsigned Reg;
216  ValueType Entry;
217 
218  // Skip instructions that are not call instructions via registers.
219  if (!isCallViaRegister(*I, Reg, Entry))
220  continue;
221 
222  Changed = true;
223  unsigned N = getCount(Entry);
224 
225  if (N != 0) {
226  // If a function has been called more than twice, we do not have to emit a
227  // load instruction to get the function address from the GOT, but can
228  // instead reuse the address that has been loaded before.
229  if (N >= 2 && !LoadTargetFromGOT)
230  getCallTargetRegOpnd(*I)->setReg(getReg(Entry));
231 
232  // Erase the $gp operand if this isn't the first time a function has
233  // been called. $gp needs to be set up only if the function call can go
234  // through a lazy binding stub.
235  eraseGPOpnd(*I);
236  }
237 
238  if (Entry)
239  incCntAndSetReg(Entry, Reg);
240 
241  setCallTargetReg(MBB, I);
242  }
243 
244  return Changed;
245 }
246 
247 bool OptimizePICCall::isCallViaRegister(MachineInstr &MI, unsigned &Reg,
248  ValueType &Val) const {
249  if (!MI.isCall())
250  return false;
251 
253 
254  // Return if MI is not a function call via a register.
255  if (!MO)
256  return false;
257 
258  // Get the instruction that loads the function address from the GOT.
259  Reg = MO->getReg();
260  Val = nullptr;
262  MachineInstr *DefMI = MRI.getVRegDef(Reg);
263 
264  assert(DefMI);
265 
266  // See if DefMI is an instruction that loads from a GOT entry that holds the
267  // address of a lazy binding stub.
268  if (!DefMI->mayLoad() || DefMI->getNumOperands() < 3)
269  return true;
270 
271  unsigned Flags = DefMI->getOperand(2).getTargetFlags();
272 
273  if (Flags != MipsII::MO_GOT_CALL && Flags != MipsII::MO_CALL_LO16)
274  return true;
275 
276  // Return the underlying object for the GOT entry in Val.
277  assert(DefMI->hasOneMemOperand());
278  Val = (*DefMI->memoperands_begin())->getValue();
279  if (!Val)
280  Val = (*DefMI->memoperands_begin())->getPseudoValue();
281  return true;
282 }
283 
284 unsigned OptimizePICCall::getCount(ValueType Entry) {
285  return ScopedHT.lookup(Entry).first;
286 }
287 
288 unsigned OptimizePICCall::getReg(ValueType Entry) {
289  unsigned Reg = ScopedHT.lookup(Entry).second;
290  assert(Reg);
291  return Reg;
292 }
293 
294 void OptimizePICCall::incCntAndSetReg(ValueType Entry, unsigned Reg) {
295  CntRegP P = ScopedHT.lookup(Entry);
296  ScopedHT.insert(Entry, std::make_pair(P.first + 1, Reg));
297 }
298 
299 /// Return an OptimizeCall object.
301  return new OptimizePICCall();
302 }
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
MachineDomTreeNode * getRootNode() const
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
static MachineOperand * getCallTargetRegOpnd(MachineInstr &MI)
Return the first MachineOperand of MI if it is a used virtual register.
static void setCallTargetReg(MachineBasicBlock *MBB, MachineBasicBlock::iterator I)
Do the following transformation:
const std::vector< DomTreeNodeBase * > & getChildren() const
AnalysisUsage & addRequired()
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool mayLoad(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read memory.
Definition: MachineInstr.h:600
const TargetRegisterClass * getRegClass(unsigned Reg) const
Return the register class of the specified virtual register.
Reg
All possible values of the reg field in the ModR/M byte.
RecyclingAllocator - This class wraps an Allocator, adding the functionality of recycling deleted obj...
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Definition: MachineInstr.h:282
void RemoveOperand(unsigned i)
Erase an operand from an instruction, leaving it with one fewer operand than it started with...
#define F(x, y, z)
Definition: MD5.cpp:55
vt_iterator legalclasstypes_begin(const TargetRegisterClass &RC) const
Loop over all of the value types that can be represented by values.
Base class for the actual dominator tree node.
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:139
TargetInstrInfo - Interface to description of machine instruction set.
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition: Allocator.h:375
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
#define P(N)
vt_iterator legalclasstypes_end(const TargetRegisterClass &RC) const
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:401
unsigned getTargetFlags() const
MO_GOT_CALL - Represents the offset into the global offset table at which the address of a call site ...
Definition: MipsBaseInfo.h:44
unsigned const MachineRegisterInfo * MRI
FunctionPass * createMipsOptimizePICCallPass()
Return an OptimizeCall object.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:284
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Represent the analysis usage information of a pass.
bool hasOneMemOperand() const
Return true if this instruction has exactly one MachineMemOperand.
Definition: MachineInstr.h:378
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
static cl::opt< bool > LoadTargetFromGOT("mips-load-target-from-got", cl::init(true), cl::desc("Load target address from GOT"), cl::Hidden)
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
Definition: MCInstrInfo.h:45
#define E
Definition: LargeTest.cpp:27
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
const size_t N
static cl::opt< bool > EraseGPOpnd("mips-erase-gp-opnd", cl::init(true), cl::desc("Erase GP Operand"), cl::Hidden)
static unsigned getReg(const void *D, unsigned RC, unsigned RegNo)
llvm_unreachable("Unknown RegBank!")
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
static std::vector< std::string > Flags
Definition: FlagsTest.cpp:8
Representation of each machine instruction.
Definition: MachineInstr.h:59
static void eraseGPOpnd(MachineInstr &MI)
Search MI's operands for register GP and erase it.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void setReg(unsigned Reg)
Change the register this operand corresponds to.
#define I(x, y, z)
Definition: MD5.cpp:58
bool isCall(QueryType Type=AnyInBundle) const
Definition: MachineInstr.h:432
MachineInstr * getVRegDef(unsigned Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
virtual const TargetInstrInfo * getInstrInfo() const
static MVT::SimpleValueType getRegTy(unsigned Reg, MachineFunction &MF)
Return type of register Reg.
IRTranslator LLVM IR MI
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.
Definition: MachineInstr.h:363