blob: 02736aca45053819d696de808ee140cd27bb3a6d [file] [log] [blame]
Preston Gurd8b7ab4b2013-04-25 20:29:37 +00001//===-- X86FixupLEAs.cpp - use or replace LEA instructions -----------===//
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//
Sanjay Patel63604412014-07-16 20:18:49 +000010// This file defines the pass that finds instructions that can be
11// re-written as LEA instructions in order to reduce pipeline delays.
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000012//
13//===----------------------------------------------------------------------===//
14
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000015#include "X86.h"
16#include "X86InstrInfo.h"
17#include "X86Subtarget.h"
18#include "llvm/ADT/Statistic.h"
19#include "llvm/CodeGen/LiveVariables.h"
20#include "llvm/CodeGen/MachineFunctionPass.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
23#include "llvm/CodeGen/Passes.h"
24#include "llvm/Support/Debug.h"
25#include "llvm/Support/raw_ostream.h"
26#include "llvm/Target/TargetInstrInfo.h"
27using namespace llvm;
28
Chandler Carruth84e68b22014-04-22 02:41:26 +000029#define DEBUG_TYPE "x86-fixup-LEAs"
30
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000031STATISTIC(NumLEAs, "Number of LEA instructions created");
32
33namespace {
Eric Christopher31b81ce2014-06-03 21:01:35 +000034class FixupLEAPass : public MachineFunctionPass {
35 enum RegUsageState { RU_NotUsed, RU_Write, RU_Read };
36 static char ID;
37 /// \brief Loop over all of the instructions in the basic block
38 /// replacing applicable instructions with LEA instructions,
39 /// where appropriate.
40 bool processBasicBlock(MachineFunction &MF, MachineFunction::iterator MFI);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000041
Sanjay Patel63604412014-07-16 20:18:49 +000042 const char *getPassName() const override { return "X86 LEA Fixup"; }
Preston Gurd128920d2013-04-25 21:31:33 +000043
Eric Christopher31b81ce2014-06-03 21:01:35 +000044 /// \brief Given a machine register, look for the instruction
45 /// which writes it in the current basic block. If found,
46 /// try to replace it with an equivalent LEA instruction.
47 /// If replacement succeeds, then also process the the newly created
48 /// instruction.
49 void seekLEAFixup(MachineOperand &p, MachineBasicBlock::iterator &I,
50 MachineFunction::iterator MFI);
Preston Gurd128920d2013-04-25 21:31:33 +000051
Eric Christopher31b81ce2014-06-03 21:01:35 +000052 /// \brief Given a memory access or LEA instruction
53 /// whose address mode uses a base and/or index register, look for
54 /// an opportunity to replace the instruction which sets the base or index
55 /// register with an equivalent LEA instruction.
56 void processInstruction(MachineBasicBlock::iterator &I,
57 MachineFunction::iterator MFI);
Preston Gurd128920d2013-04-25 21:31:33 +000058
Eric Christopher31b81ce2014-06-03 21:01:35 +000059 /// \brief Given a LEA instruction which is unprofitable
60 /// on Silvermont try to replace it with an equivalent ADD instruction
61 void processInstructionForSLM(MachineBasicBlock::iterator &I,
62 MachineFunction::iterator MFI);
Alexey Volkov6226de62014-05-20 08:55:50 +000063
Eric Christopher31b81ce2014-06-03 21:01:35 +000064 /// \brief Determine if an instruction references a machine register
65 /// and, if so, whether it reads or writes the register.
66 RegUsageState usesRegister(MachineOperand &p, MachineBasicBlock::iterator I);
Preston Gurd128920d2013-04-25 21:31:33 +000067
Eric Christopher31b81ce2014-06-03 21:01:35 +000068 /// \brief Step backwards through a basic block, looking
69 /// for an instruction which writes a register within
70 /// a maximum of INSTR_DISTANCE_THRESHOLD instruction latency cycles.
71 MachineBasicBlock::iterator searchBackwards(MachineOperand &p,
72 MachineBasicBlock::iterator &I,
73 MachineFunction::iterator MFI);
Preston Gurd128920d2013-04-25 21:31:33 +000074
Eric Christopher31b81ce2014-06-03 21:01:35 +000075 /// \brief if an instruction can be converted to an
76 /// equivalent LEA, insert the new instruction into the basic block
77 /// and return a pointer to it. Otherwise, return zero.
78 MachineInstr *postRAConvertToLEA(MachineFunction::iterator &MFI,
79 MachineBasicBlock::iterator &MBBI) const;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000080
Eric Christopher31b81ce2014-06-03 21:01:35 +000081public:
82 FixupLEAPass() : MachineFunctionPass(ID) {}
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000083
Eric Christopher31b81ce2014-06-03 21:01:35 +000084 /// \brief Loop over all of the basic blocks,
85 /// replacing instructions by equivalent LEA instructions
86 /// if needed and when possible.
87 bool runOnMachineFunction(MachineFunction &MF) override;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000088
Eric Christopher31b81ce2014-06-03 21:01:35 +000089private:
90 MachineFunction *MF;
91 const TargetMachine *TM;
92 const X86InstrInfo *TII; // Machine instruction info.
93};
94char FixupLEAPass::ID = 0;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000095}
96
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000097MachineInstr *
98FixupLEAPass::postRAConvertToLEA(MachineFunction::iterator &MFI,
Preston Gurd128920d2013-04-25 21:31:33 +000099 MachineBasicBlock::iterator &MBBI) const {
Eric Christopher31b81ce2014-06-03 21:01:35 +0000100 MachineInstr *MI = MBBI;
101 MachineInstr *NewMI;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000102 switch (MI->getOpcode()) {
Alexey Volkov6226de62014-05-20 08:55:50 +0000103 case X86::MOV32rr:
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000104 case X86::MOV64rr: {
Eric Christopher31b81ce2014-06-03 21:01:35 +0000105 const MachineOperand &Src = MI->getOperand(1);
106 const MachineOperand &Dest = MI->getOperand(0);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000107 NewMI = BuildMI(*MF, MI->getDebugLoc(),
Eric Christopher31b81ce2014-06-03 21:01:35 +0000108 TII->get(MI->getOpcode() == X86::MOV32rr ? X86::LEA32r
109 : X86::LEA64r))
110 .addOperand(Dest)
111 .addOperand(Src)
112 .addImm(1)
113 .addReg(0)
114 .addImm(0)
115 .addReg(0);
116 MFI->insert(MBBI, NewMI); // Insert the new inst
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000117 return NewMI;
118 }
119 case X86::ADD64ri32:
120 case X86::ADD64ri8:
121 case X86::ADD64ri32_DB:
122 case X86::ADD64ri8_DB:
123 case X86::ADD32ri:
124 case X86::ADD32ri8:
125 case X86::ADD32ri_DB:
126 case X86::ADD32ri8_DB:
127 case X86::ADD16ri:
128 case X86::ADD16ri8:
129 case X86::ADD16ri_DB:
130 case X86::ADD16ri8_DB:
131 if (!MI->getOperand(2).isImm()) {
132 // convertToThreeAddress will call getImm()
133 // which requires isImm() to be true
Craig Topper062a2ba2014-04-25 05:30:21 +0000134 return nullptr;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000135 }
Preston Gurdf03a6e72013-09-30 23:51:22 +0000136 break;
Preston Gurdf0b62882013-09-30 23:18:42 +0000137 case X86::ADD16rr:
138 case X86::ADD16rr_DB:
139 if (MI->getOperand(1).getReg() != MI->getOperand(2).getReg()) {
140 // if src1 != src2, then convertToThreeAddress will
141 // need to create a Virtual register, which we cannot do
142 // after register allocation.
Craig Topper062a2ba2014-04-25 05:30:21 +0000143 return nullptr;
Preston Gurdf0b62882013-09-30 23:18:42 +0000144 }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000145 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000146 return TII->convertToThreeAddress(MFI, MBBI, nullptr);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000147}
148
Eric Christopher31b81ce2014-06-03 21:01:35 +0000149FunctionPass *llvm::createX86FixupLEAs() { return new FixupLEAPass(); }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000150
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000151bool FixupLEAPass::runOnMachineFunction(MachineFunction &Func) {
Eric Christopherdd240fd2014-06-03 21:01:39 +0000152 MF = &Func;
Eric Christophere0bd2fa2014-05-22 01:45:59 +0000153 TM = &Func.getTarget();
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000154 const X86Subtarget &ST = TM->getSubtarget<X86Subtarget>();
155 if (!ST.LEAusesAG() && !ST.slowLEA())
156 return false;
157
Eric Christopherd9134482014-08-04 21:25:23 +0000158 TII =
159 static_cast<const X86InstrInfo *>(TM->getSubtargetImpl()->getInstrInfo());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000160
161 DEBUG(dbgs() << "Start X86FixupLEAs\n";);
162 // Process all basic blocks.
163 for (MachineFunction::iterator I = Func.begin(), E = Func.end(); I != E; ++I)
164 processBasicBlock(Func, I);
165 DEBUG(dbgs() << "End X86FixupLEAs\n";);
166
167 return true;
168}
169
Eric Christopher31b81ce2014-06-03 21:01:35 +0000170FixupLEAPass::RegUsageState
171FixupLEAPass::usesRegister(MachineOperand &p, MachineBasicBlock::iterator I) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000172 RegUsageState RegUsage = RU_NotUsed;
Eric Christopher31b81ce2014-06-03 21:01:35 +0000173 MachineInstr *MI = I;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000174
175 for (unsigned int i = 0; i < MI->getNumOperands(); ++i) {
Eric Christopher31b81ce2014-06-03 21:01:35 +0000176 MachineOperand &opnd = MI->getOperand(i);
177 if (opnd.isReg() && opnd.getReg() == p.getReg()) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000178 if (opnd.isDef())
179 return RU_Write;
180 RegUsage = RU_Read;
181 }
182 }
183 return RegUsage;
184}
185
186/// getPreviousInstr - Given a reference to an instruction in a basic
187/// block, return a reference to the previous instruction in the block,
188/// wrapping around to the last instruction of the block if the block
189/// branches to itself.
Eric Christopher31b81ce2014-06-03 21:01:35 +0000190static inline bool getPreviousInstr(MachineBasicBlock::iterator &I,
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000191 MachineFunction::iterator MFI) {
192 if (I == MFI->begin()) {
193 if (MFI->isPredecessor(MFI)) {
194 I = --MFI->end();
195 return true;
Eric Christopher31b81ce2014-06-03 21:01:35 +0000196 } else
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000197 return false;
198 }
199 --I;
200 return true;
201}
202
Eric Christopher31b81ce2014-06-03 21:01:35 +0000203MachineBasicBlock::iterator
204FixupLEAPass::searchBackwards(MachineOperand &p, MachineBasicBlock::iterator &I,
205 MachineFunction::iterator MFI) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000206 int InstrDistance = 1;
207 MachineBasicBlock::iterator CurInst;
208 static const int INSTR_DISTANCE_THRESHOLD = 5;
209
210 CurInst = I;
211 bool Found;
212 Found = getPreviousInstr(CurInst, MFI);
Eric Christopher31b81ce2014-06-03 21:01:35 +0000213 while (Found && I != CurInst) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000214 if (CurInst->isCall() || CurInst->isInlineAsm())
215 break;
216 if (InstrDistance > INSTR_DISTANCE_THRESHOLD)
217 break; // too far back to make a difference
Eric Christopher31b81ce2014-06-03 21:01:35 +0000218 if (usesRegister(p, CurInst) == RU_Write) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000219 return CurInst;
220 }
Eric Christopherd9134482014-08-04 21:25:23 +0000221 InstrDistance += TII->getInstrLatency(
222 TM->getSubtargetImpl()->getInstrItineraryData(), CurInst);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000223 Found = getPreviousInstr(CurInst, MFI);
224 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000225 return nullptr;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000226}
227
Eric Christopher31b81ce2014-06-03 21:01:35 +0000228void FixupLEAPass::processInstruction(MachineBasicBlock::iterator &I,
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000229 MachineFunction::iterator MFI) {
230 // Process a load, store, or LEA instruction.
231 MachineInstr *MI = I;
232 int opcode = MI->getOpcode();
Eric Christopher31b81ce2014-06-03 21:01:35 +0000233 const MCInstrDesc &Desc = MI->getDesc();
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000234 int AddrOffset = X86II::getMemoryOperandNo(Desc.TSFlags, opcode);
235 if (AddrOffset >= 0) {
236 AddrOffset += X86II::getOperandBias(Desc);
Eric Christopher31b81ce2014-06-03 21:01:35 +0000237 MachineOperand &p = MI->getOperand(AddrOffset + X86::AddrBaseReg);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000238 if (p.isReg() && p.getReg() != X86::ESP) {
239 seekLEAFixup(p, I, MFI);
240 }
Eric Christopher31b81ce2014-06-03 21:01:35 +0000241 MachineOperand &q = MI->getOperand(AddrOffset + X86::AddrIndexReg);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000242 if (q.isReg() && q.getReg() != X86::ESP) {
243 seekLEAFixup(q, I, MFI);
244 }
245 }
246}
247
Eric Christopher31b81ce2014-06-03 21:01:35 +0000248void FixupLEAPass::seekLEAFixup(MachineOperand &p,
249 MachineBasicBlock::iterator &I,
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000250 MachineFunction::iterator MFI) {
251 MachineBasicBlock::iterator MBI = searchBackwards(p, I, MFI);
252 if (MBI) {
Eric Christopher31b81ce2014-06-03 21:01:35 +0000253 MachineInstr *NewMI = postRAConvertToLEA(MFI, MBI);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000254 if (NewMI) {
255 ++NumLEAs;
Alexey Volkov6226de62014-05-20 08:55:50 +0000256 DEBUG(dbgs() << "FixLEA: Candidate to replace:"; MBI->dump(););
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000257 // now to replace with an equivalent LEA...
Alexey Volkov6226de62014-05-20 08:55:50 +0000258 DEBUG(dbgs() << "FixLEA: Replaced by: "; NewMI->dump(););
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000259 MFI->erase(MBI);
260 MachineBasicBlock::iterator J =
Eric Christopher31b81ce2014-06-03 21:01:35 +0000261 static_cast<MachineBasicBlock::iterator>(NewMI);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000262 processInstruction(J, MFI);
263 }
264 }
265}
266
Alexey Volkov6226de62014-05-20 08:55:50 +0000267void FixupLEAPass::processInstructionForSLM(MachineBasicBlock::iterator &I,
268 MachineFunction::iterator MFI) {
269 MachineInstr *MI = I;
270 const int opcode = MI->getOpcode();
271 if (opcode != X86::LEA16r && opcode != X86::LEA32r && opcode != X86::LEA64r &&
272 opcode != X86::LEA64_32r)
273 return;
274 if (MI->getOperand(5).getReg() != 0 || !MI->getOperand(4).isImm() ||
275 !TII->isSafeToClobberEFLAGS(*MFI, I))
276 return;
277 const unsigned DstR = MI->getOperand(0).getReg();
278 const unsigned SrcR1 = MI->getOperand(1).getReg();
279 const unsigned SrcR2 = MI->getOperand(3).getReg();
280 if ((SrcR1 == 0 || SrcR1 != DstR) && (SrcR2 == 0 || SrcR2 != DstR))
281 return;
282 if (MI->getOperand(2).getImm() > 1)
283 return;
284 int addrr_opcode, addri_opcode;
285 switch (opcode) {
286 case X86::LEA16r:
287 addrr_opcode = X86::ADD16rr;
288 addri_opcode = X86::ADD16ri;
289 break;
290 case X86::LEA32r:
291 addrr_opcode = X86::ADD32rr;
292 addri_opcode = X86::ADD32ri;
293 break;
294 case X86::LEA64_32r:
295 case X86::LEA64r:
296 addrr_opcode = X86::ADD64rr;
297 addri_opcode = X86::ADD64ri32;
298 break;
299 default:
300 assert(false && "Unexpected LEA instruction");
301 }
302 DEBUG(dbgs() << "FixLEA: Candidate to replace:"; I->dump(););
303 DEBUG(dbgs() << "FixLEA: Replaced by: ";);
Craig Topper66f09ad2014-06-08 22:29:17 +0000304 MachineInstr *NewMI = nullptr;
Alexey Volkov6226de62014-05-20 08:55:50 +0000305 const MachineOperand &Dst = MI->getOperand(0);
306 // Make ADD instruction for two registers writing to LEA's destination
307 if (SrcR1 != 0 && SrcR2 != 0) {
308 const MachineOperand &Src1 = MI->getOperand(SrcR1 == DstR ? 1 : 3);
309 const MachineOperand &Src2 = MI->getOperand(SrcR1 == DstR ? 3 : 1);
310 NewMI = BuildMI(*MF, MI->getDebugLoc(), TII->get(addrr_opcode))
311 .addOperand(Dst)
312 .addOperand(Src1)
313 .addOperand(Src2);
314 MFI->insert(I, NewMI);
315 DEBUG(NewMI->dump(););
316 }
317 // Make ADD instruction for immediate
318 if (MI->getOperand(4).getImm() != 0) {
319 const MachineOperand &SrcR = MI->getOperand(SrcR1 == DstR ? 1 : 3);
320 NewMI = BuildMI(*MF, MI->getDebugLoc(), TII->get(addri_opcode))
321 .addOperand(Dst)
322 .addOperand(SrcR)
323 .addImm(MI->getOperand(4).getImm());
324 MFI->insert(I, NewMI);
325 DEBUG(NewMI->dump(););
326 }
327 if (NewMI) {
328 MFI->erase(I);
329 I = static_cast<MachineBasicBlock::iterator>(NewMI);
330 }
331}
332
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000333bool FixupLEAPass::processBasicBlock(MachineFunction &MF,
334 MachineFunction::iterator MFI) {
335
Alexey Volkov6226de62014-05-20 08:55:50 +0000336 for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I) {
337 if (TM->getSubtarget<X86Subtarget>().isSLM())
338 processInstructionForSLM(I, MFI);
339 else
340 processInstruction(I, MFI);
341 }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000342 return false;
343}