blob: 27bc4843f410e0af9e5028d87baa4f03d17a1704 [file] [log] [blame]
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001//===- MipsOptimizePICCall.cpp - Optimize PIC Calls -----------------------===//
Akira Hatanaka168d4e52013-11-27 23:38:42 +00002//
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
Akira Hatanaka168d4e52013-11-27 23:38:42 +000015#include "MCTargetDesc/MipsBaseInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "Mips.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000017#include "MipsRegisterInfo.h"
18#include "MipsSubtarget.h"
19#include "llvm/ADT/PointerUnion.h"
Akira Hatanaka168d4e52013-11-27 23:38:42 +000020#include "llvm/ADT/ScopedHashTable.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000021#include "llvm/ADT/SmallVector.h"
22#include "llvm/CodeGen/MachineBasicBlock.h"
Akira Hatanaka168d4e52013-11-27 23:38:42 +000023#include "llvm/CodeGen/MachineDominators.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000024#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineFunctionPass.h"
26#include "llvm/CodeGen/MachineInstr.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
28#include "llvm/CodeGen/MachineOperand.h"
Akira Hatanaka168d4e52013-11-27 23:38:42 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000030#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000031#include "llvm/CodeGen/TargetOpcodes.h"
32#include "llvm/CodeGen/TargetRegisterInfo.h"
33#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000034#include "llvm/Support/Allocator.h"
Akira Hatanaka168d4e52013-11-27 23:38:42 +000035#include "llvm/Support/CommandLine.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000036#include "llvm/Support/ErrorHandling.h"
David Blaikie13e77db2018-03-23 23:58:25 +000037#include "llvm/Support/MachineValueType.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000038#include "llvm/Support/RecyclingAllocator.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000039#include <cassert>
40#include <utility>
41#include <vector>
Akira Hatanaka168d4e52013-11-27 23:38:42 +000042
43using namespace llvm;
44
Chandler Carruth84e68b22014-04-22 02:41:26 +000045#define DEBUG_TYPE "optimize-mips-pic-call"
46
Akira Hatanaka168d4e52013-11-27 23:38:42 +000047static cl::opt<bool> LoadTargetFromGOT("mips-load-target-from-got",
48 cl::init(true),
49 cl::desc("Load target address from GOT"),
50 cl::Hidden);
51
52static cl::opt<bool> EraseGPOpnd("mips-erase-gp-opnd",
53 cl::init(true), cl::desc("Erase GP Operand"),
54 cl::Hidden);
55
56namespace {
Nick Lewyckyaad475b2014-04-15 07:22:52 +000057
Eugene Zelenko79220eae2017-08-03 22:12:30 +000058using ValueType = PointerUnion<const Value *, const PseudoSourceValue *>;
59using CntRegP = std::pair<unsigned, unsigned>;
60using AllocatorTy = RecyclingAllocator<BumpPtrAllocator,
61 ScopedHashTableVal<ValueType, CntRegP>>;
62using ScopedHTType = ScopedHashTable<ValueType, CntRegP,
63 DenseMapInfo<ValueType>, AllocatorTy>;
Akira Hatanaka168d4e52013-11-27 23:38:42 +000064
65class MBBInfo {
66public:
67 MBBInfo(MachineDomTreeNode *N);
Eugene Zelenko79220eae2017-08-03 22:12:30 +000068
Akira Hatanaka168d4e52013-11-27 23:38:42 +000069 const MachineDomTreeNode *getNode() const;
70 bool isVisited() const;
71 void preVisit(ScopedHTType &ScopedHT);
72 void postVisit();
73
74private:
75 MachineDomTreeNode *Node;
76 ScopedHTType::ScopeTy *HTScope;
77};
78
79class OptimizePICCall : public MachineFunctionPass {
80public:
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +000081 OptimizePICCall() : MachineFunctionPass(ID) {}
Akira Hatanaka168d4e52013-11-27 23:38:42 +000082
Mehdi Amini117296c2016-10-01 02:56:57 +000083 StringRef getPassName() const override { return "Mips OptimizePICCall"; }
Akira Hatanaka168d4e52013-11-27 23:38:42 +000084
Craig Topper56c590a2014-04-29 07:58:02 +000085 bool runOnMachineFunction(MachineFunction &F) override;
Akira Hatanaka168d4e52013-11-27 23:38:42 +000086
Craig Topper56c590a2014-04-29 07:58:02 +000087 void getAnalysisUsage(AnalysisUsage &AU) const override {
Akira Hatanaka168d4e52013-11-27 23:38:42 +000088 AU.addRequired<MachineDominatorTree>();
89 MachineFunctionPass::getAnalysisUsage(AU);
90 }
91
92private:
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000093 /// Visit MBB.
Akira Hatanaka168d4e52013-11-27 23:38:42 +000094 bool visitNode(MBBInfo &MBBI);
95
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000096 /// Test if MI jumps to a function via a register.
Akira Hatanaka168d4e52013-11-27 23:38:42 +000097 ///
98 /// Also, return the virtual register containing the target function's address
99 /// and the underlying object in Reg and Val respectively, if the function's
100 /// address can be resolved lazily.
101 bool isCallViaRegister(MachineInstr &MI, unsigned &Reg,
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000102 ValueType &Val) const;
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000103
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000104 /// Return the number of instructions that dominate the current
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000105 /// instruction and load the function address from object Entry.
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000106 unsigned getCount(ValueType Entry);
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000107
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000108 /// Return the destination virtual register of the last instruction
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000109 /// that loads from object Entry.
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000110 unsigned getReg(ValueType Entry);
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000111
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000112 /// Update ScopedHT.
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000113 void incCntAndSetReg(ValueType Entry, unsigned Reg);
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000114
115 ScopedHTType ScopedHT;
Eugene Zelenko79220eae2017-08-03 22:12:30 +0000116
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000117 static char ID;
118};
119
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000120} // end of anonymous namespace
121
Eugene Zelenko79220eae2017-08-03 22:12:30 +0000122char OptimizePICCall::ID = 0;
123
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000124/// Return the first MachineOperand of MI if it is a used virtual register.
125static MachineOperand *getCallTargetRegOpnd(MachineInstr &MI) {
126 if (MI.getNumOperands() == 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000127 return nullptr;
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000128
129 MachineOperand &MO = MI.getOperand(0);
130
131 if (!MO.isReg() || !MO.isUse() ||
132 !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Craig Topper062a2ba2014-04-25 05:30:21 +0000133 return nullptr;
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000134
135 return &MO;
136}
137
138/// Return type of register Reg.
139static MVT::SimpleValueType getRegTy(unsigned Reg, MachineFunction &MF) {
Krzysztof Parzyszekc8e8e2a2017-04-24 19:51:12 +0000140 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000141 const TargetRegisterClass *RC = MF.getRegInfo().getRegClass(Reg);
Krzysztof Parzyszekc8e8e2a2017-04-24 19:51:12 +0000142 assert(TRI.legalclasstypes_end(*RC) - TRI.legalclasstypes_begin(*RC) == 1);
143 return *TRI.legalclasstypes_begin(*RC);
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000144}
145
146/// Do the following transformation:
147///
148/// jalr $vreg
149/// =>
150/// copy $t9, $vreg
151/// jalr $t9
152static void setCallTargetReg(MachineBasicBlock *MBB,
153 MachineBasicBlock::iterator I) {
154 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +0000155 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000156 unsigned SrcReg = I->getOperand(0).getReg();
157 unsigned DstReg = getRegTy(SrcReg, MF) == MVT::i32 ? Mips::T9 : Mips::T9_64;
158 BuildMI(*MBB, I, I->getDebugLoc(), TII.get(TargetOpcode::COPY), DstReg)
159 .addReg(SrcReg);
160 I->getOperand(0).setReg(DstReg);
161}
162
163/// Search MI's operands for register GP and erase it.
164static void eraseGPOpnd(MachineInstr &MI) {
165 if (!EraseGPOpnd)
166 return;
167
168 MachineFunction &MF = *MI.getParent()->getParent();
169 MVT::SimpleValueType Ty = getRegTy(MI.getOperand(0).getReg(), MF);
170 unsigned Reg = Ty == MVT::i32 ? Mips::GP : Mips::GP_64;
171
172 for (unsigned I = 0; I < MI.getNumOperands(); ++I) {
173 MachineOperand &MO = MI.getOperand(I);
174 if (MO.isReg() && MO.getReg() == Reg) {
175 MI.RemoveOperand(I);
176 return;
177 }
178 }
179
Craig Toppere73658d2014-04-28 04:05:08 +0000180 llvm_unreachable(nullptr);
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000181}
182
Craig Topper062a2ba2014-04-25 05:30:21 +0000183MBBInfo::MBBInfo(MachineDomTreeNode *N) : Node(N), HTScope(nullptr) {}
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000184
185const MachineDomTreeNode *MBBInfo::getNode() const { return Node; }
186
187bool MBBInfo::isVisited() const { return HTScope; }
188
189void MBBInfo::preVisit(ScopedHTType &ScopedHT) {
190 HTScope = new ScopedHTType::ScopeTy(ScopedHT);
191}
192
193void MBBInfo::postVisit() {
194 delete HTScope;
195}
196
197// OptimizePICCall methods.
198bool OptimizePICCall::runOnMachineFunction(MachineFunction &F) {
Eric Christopher96e72c62015-01-29 23:27:36 +0000199 if (static_cast<const MipsSubtarget &>(F.getSubtarget()).inMips16Mode())
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000200 return false;
201
202 // Do a pre-order traversal of the dominator tree.
203 MachineDominatorTree *MDT = &getAnalysis<MachineDominatorTree>();
204 bool Changed = false;
205
206 SmallVector<MBBInfo, 8> WorkList(1, MBBInfo(MDT->getRootNode()));
207
208 while (!WorkList.empty()) {
209 MBBInfo &MBBI = WorkList.back();
210
211 // If this MBB has already been visited, destroy the scope for the MBB and
212 // pop it from the work list.
213 if (MBBI.isVisited()) {
214 MBBI.postVisit();
215 WorkList.pop_back();
216 continue;
217 }
218
219 // Visit the MBB and add its children to the work list.
220 MBBI.preVisit(ScopedHT);
221 Changed |= visitNode(MBBI);
222 const MachineDomTreeNode *Node = MBBI.getNode();
223 const std::vector<MachineDomTreeNode *> &Children = Node->getChildren();
224 WorkList.append(Children.begin(), Children.end());
225 }
226
227 return Changed;
228}
229
230bool OptimizePICCall::visitNode(MBBInfo &MBBI) {
231 bool Changed = false;
232 MachineBasicBlock *MBB = MBBI.getNode()->getBlock();
233
234 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
235 ++I) {
236 unsigned Reg;
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000237 ValueType Entry;
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000238
239 // Skip instructions that are not call instructions via registers.
240 if (!isCallViaRegister(*I, Reg, Entry))
241 continue;
242
243 Changed = true;
244 unsigned N = getCount(Entry);
245
246 if (N != 0) {
247 // If a function has been called more than twice, we do not have to emit a
248 // load instruction to get the function address from the GOT, but can
249 // instead reuse the address that has been loaded before.
250 if (N >= 2 && !LoadTargetFromGOT)
251 getCallTargetRegOpnd(*I)->setReg(getReg(Entry));
252
253 // Erase the $gp operand if this isn't the first time a function has
254 // been called. $gp needs to be set up only if the function call can go
255 // through a lazy binding stub.
256 eraseGPOpnd(*I);
257 }
258
259 if (Entry)
260 incCntAndSetReg(Entry, Reg);
261
262 setCallTargetReg(MBB, I);
263 }
264
265 return Changed;
266}
267
268bool OptimizePICCall::isCallViaRegister(MachineInstr &MI, unsigned &Reg,
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000269 ValueType &Val) const {
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000270 if (!MI.isCall())
271 return false;
272
273 MachineOperand *MO = getCallTargetRegOpnd(MI);
274
275 // Return if MI is not a function call via a register.
276 if (!MO)
277 return false;
278
279 // Get the instruction that loads the function address from the GOT.
280 Reg = MO->getReg();
Serge Gueltonf4dc59b2017-05-11 08:53:00 +0000281 Val = nullptr;
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000282 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
283 MachineInstr *DefMI = MRI.getVRegDef(Reg);
284
285 assert(DefMI);
286
287 // See if DefMI is an instruction that loads from a GOT entry that holds the
288 // address of a lazy binding stub.
289 if (!DefMI->mayLoad() || DefMI->getNumOperands() < 3)
290 return true;
291
292 unsigned Flags = DefMI->getOperand(2).getTargetFlags();
293
294 if (Flags != MipsII::MO_GOT_CALL && Flags != MipsII::MO_CALL_LO16)
295 return true;
296
297 // Return the underlying object for the GOT entry in Val.
298 assert(DefMI->hasOneMemOperand());
299 Val = (*DefMI->memoperands_begin())->getValue();
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000300 if (!Val)
301 Val = (*DefMI->memoperands_begin())->getPseudoValue();
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000302 return true;
303}
304
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000305unsigned OptimizePICCall::getCount(ValueType Entry) {
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000306 return ScopedHT.lookup(Entry).first;
307}
308
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000309unsigned OptimizePICCall::getReg(ValueType Entry) {
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000310 unsigned Reg = ScopedHT.lookup(Entry).second;
311 assert(Reg);
312 return Reg;
313}
314
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000315void OptimizePICCall::incCntAndSetReg(ValueType Entry, unsigned Reg) {
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000316 CntRegP P = ScopedHT.lookup(Entry);
317 ScopedHT.insert(Entry, std::make_pair(P.first + 1, Reg));
318}
319
320/// Return an OptimizeCall object.
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000321FunctionPass *llvm::createMipsOptimizePICCallPass() {
322 return new OptimizePICCall();
Akira Hatanaka168d4e52013-11-27 23:38:42 +0000323}