xref: /llvm-project/llvm/lib/Target/Mips/MipsOptimizePICCall.cpp (revision 117296c0a00860072854dd3fee8cd710b7955cf7)
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 
15 #include "Mips.h"
16 #include "MCTargetDesc/MipsBaseInfo.h"
17 #include "MipsMachineFunction.h"
18 #include "MipsTargetMachine.h"
19 #include "llvm/ADT/ScopedHashTable.h"
20 #include "llvm/CodeGen/MachineDominators.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/Support/CommandLine.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 {
38 typedef PointerUnion<const Value *, const PseudoSourceValue *> ValueType;
39 
40 typedef std::pair<unsigned, unsigned> CntRegP;
41 typedef RecyclingAllocator<BumpPtrAllocator,
42                            ScopedHashTableVal<ValueType, CntRegP> >
43 AllocatorTy;
44 typedef ScopedHashTable<ValueType, CntRegP, DenseMapInfo<ValueType>,
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(TargetMachine &tm) : 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 {
69     AU.addRequired<MachineDominatorTree>();
70     MachineFunctionPass::getAnalysisUsage(AU);
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.
104 static MachineOperand *getCallTargetRegOpnd(MachineInstr &MI) {
105   if (MI.getNumOperands() == 0)
106     return nullptr;
107 
108   MachineOperand &MO = MI.getOperand(0);
109 
110   if (!MO.isReg() || !MO.isUse() ||
111       !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
112     return nullptr;
113 
114   return &MO;
115 }
116 
117 /// Return type of register Reg.
118 static MVT::SimpleValueType getRegTy(unsigned Reg, MachineFunction &MF) {
119   const TargetRegisterClass *RC = MF.getRegInfo().getRegClass(Reg);
120   assert(RC->vt_end() - RC->vt_begin() == 1);
121   return *RC->vt_begin();
122 }
123 
124 /// Do the following transformation:
125 ///
126 /// jalr $vreg
127 /// =>
128 /// copy $t9, $vreg
129 /// jalr $t9
130 static void setCallTargetReg(MachineBasicBlock *MBB,
131                              MachineBasicBlock::iterator I) {
132   MachineFunction &MF = *MBB->getParent();
133   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
134   unsigned SrcReg = I->getOperand(0).getReg();
135   unsigned DstReg = getRegTy(SrcReg, MF) == MVT::i32 ? Mips::T9 : Mips::T9_64;
136   BuildMI(*MBB, I, I->getDebugLoc(), TII.get(TargetOpcode::COPY), DstReg)
137       .addReg(SrcReg);
138   I->getOperand(0).setReg(DstReg);
139 }
140 
141 /// Search MI's operands for register GP and erase it.
142 static void eraseGPOpnd(MachineInstr &MI) {
143   if (!EraseGPOpnd)
144     return;
145 
146   MachineFunction &MF = *MI.getParent()->getParent();
147   MVT::SimpleValueType Ty = getRegTy(MI.getOperand(0).getReg(), MF);
148   unsigned Reg = Ty == MVT::i32 ? Mips::GP : Mips::GP_64;
149 
150   for (unsigned I = 0; I < MI.getNumOperands(); ++I) {
151     MachineOperand &MO = MI.getOperand(I);
152     if (MO.isReg() && MO.getReg() == Reg) {
153       MI.RemoveOperand(I);
154       return;
155     }
156   }
157 
158   llvm_unreachable(nullptr);
159 }
160 
161 MBBInfo::MBBInfo(MachineDomTreeNode *N) : Node(N), HTScope(nullptr) {}
162 
163 const MachineDomTreeNode *MBBInfo::getNode() const { return Node; }
164 
165 bool MBBInfo::isVisited() const { return HTScope; }
166 
167 void MBBInfo::preVisit(ScopedHTType &ScopedHT) {
168   HTScope = new ScopedHTType::ScopeTy(ScopedHT);
169 }
170 
171 void MBBInfo::postVisit() {
172   delete HTScope;
173 }
174 
175 // OptimizePICCall methods.
176 bool OptimizePICCall::runOnMachineFunction(MachineFunction &F) {
177   if (skipFunction(*F.getFunction()))
178     return false;
179 
180   if (static_cast<const MipsSubtarget &>(F.getSubtarget()).inMips16Mode())
181     return false;
182 
183   // Do a pre-order traversal of the dominator tree.
184   MachineDominatorTree *MDT = &getAnalysis<MachineDominatorTree>();
185   bool Changed = false;
186 
187   SmallVector<MBBInfo, 8> WorkList(1, MBBInfo(MDT->getRootNode()));
188 
189   while (!WorkList.empty()) {
190     MBBInfo &MBBI = WorkList.back();
191 
192     // If this MBB has already been visited, destroy the scope for the MBB and
193     // pop it from the work list.
194     if (MBBI.isVisited()) {
195       MBBI.postVisit();
196       WorkList.pop_back();
197       continue;
198     }
199 
200     // Visit the MBB and add its children to the work list.
201     MBBI.preVisit(ScopedHT);
202     Changed |= visitNode(MBBI);
203     const MachineDomTreeNode *Node = MBBI.getNode();
204     const std::vector<MachineDomTreeNode *> &Children = Node->getChildren();
205     WorkList.append(Children.begin(), Children.end());
206   }
207 
208   return Changed;
209 }
210 
211 bool OptimizePICCall::visitNode(MBBInfo &MBBI) {
212   bool Changed = false;
213   MachineBasicBlock *MBB = MBBI.getNode()->getBlock();
214 
215   for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
216        ++I) {
217     unsigned Reg;
218     ValueType Entry;
219 
220     // Skip instructions that are not call instructions via registers.
221     if (!isCallViaRegister(*I, Reg, Entry))
222       continue;
223 
224     Changed = true;
225     unsigned N = getCount(Entry);
226 
227     if (N != 0) {
228       // If a function has been called more than twice, we do not have to emit a
229       // load instruction to get the function address from the GOT, but can
230       // instead reuse the address that has been loaded before.
231       if (N >= 2 && !LoadTargetFromGOT)
232         getCallTargetRegOpnd(*I)->setReg(getReg(Entry));
233 
234       // Erase the $gp operand if this isn't the first time a function has
235       // been called. $gp needs to be set up only if the function call can go
236       // through a lazy binding stub.
237       eraseGPOpnd(*I);
238     }
239 
240     if (Entry)
241       incCntAndSetReg(Entry, Reg);
242 
243     setCallTargetReg(MBB, I);
244   }
245 
246   return Changed;
247 }
248 
249 bool OptimizePICCall::isCallViaRegister(MachineInstr &MI, unsigned &Reg,
250                                         ValueType &Val) const {
251   if (!MI.isCall())
252     return false;
253 
254   MachineOperand *MO = getCallTargetRegOpnd(MI);
255 
256   // Return if MI is not a function call via a register.
257   if (!MO)
258     return false;
259 
260   // Get the instruction that loads the function address from the GOT.
261   Reg = MO->getReg();
262   Val = (Value*)nullptr;
263   MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
264   MachineInstr *DefMI = MRI.getVRegDef(Reg);
265 
266   assert(DefMI);
267 
268   // See if DefMI is an instruction that loads from a GOT entry that holds the
269   // address of a lazy binding stub.
270   if (!DefMI->mayLoad() || DefMI->getNumOperands() < 3)
271     return true;
272 
273   unsigned Flags = DefMI->getOperand(2).getTargetFlags();
274 
275   if (Flags != MipsII::MO_GOT_CALL && Flags != MipsII::MO_CALL_LO16)
276     return true;
277 
278   // Return the underlying object for the GOT entry in Val.
279   assert(DefMI->hasOneMemOperand());
280   Val = (*DefMI->memoperands_begin())->getValue();
281   if (!Val)
282     Val = (*DefMI->memoperands_begin())->getPseudoValue();
283   return true;
284 }
285 
286 unsigned OptimizePICCall::getCount(ValueType Entry) {
287   return ScopedHT.lookup(Entry).first;
288 }
289 
290 unsigned OptimizePICCall::getReg(ValueType Entry) {
291   unsigned Reg = ScopedHT.lookup(Entry).second;
292   assert(Reg);
293   return Reg;
294 }
295 
296 void OptimizePICCall::incCntAndSetReg(ValueType Entry, unsigned Reg) {
297   CntRegP P = ScopedHT.lookup(Entry);
298   ScopedHT.insert(Entry, std::make_pair(P.first + 1, Reg));
299 }
300 
301 /// Return an OptimizeCall object.
302 FunctionPass *llvm::createMipsOptimizePICCallPass(MipsTargetMachine &TM) {
303   return new OptimizePICCall(TM);
304 }
305