blob: a6158b4c69333867525674bfac71f98fa0217b84 [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.
Eric Christopher572e03a2015-06-19 01:53:21 +000047 /// If replacement succeeds, then also process the newly created
Eric Christopher31b81ce2014-06-03 21:01:35 +000048 /// 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;
Eric Christopher31b81ce2014-06-03 21:01:35 +000091 const X86InstrInfo *TII; // Machine instruction info.
92};
93char FixupLEAPass::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000094}
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000095
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000096MachineInstr *
97FixupLEAPass::postRAConvertToLEA(MachineFunction::iterator &MFI,
Preston Gurd128920d2013-04-25 21:31:33 +000098 MachineBasicBlock::iterator &MBBI) const {
Eric Christopher31b81ce2014-06-03 21:01:35 +000099 MachineInstr *MI = MBBI;
100 MachineInstr *NewMI;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000101 switch (MI->getOpcode()) {
Alexey Volkov6226de62014-05-20 08:55:50 +0000102 case X86::MOV32rr:
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000103 case X86::MOV64rr: {
Eric Christopher31b81ce2014-06-03 21:01:35 +0000104 const MachineOperand &Src = MI->getOperand(1);
105 const MachineOperand &Dest = MI->getOperand(0);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000106 NewMI = BuildMI(*MF, MI->getDebugLoc(),
Eric Christopher31b81ce2014-06-03 21:01:35 +0000107 TII->get(MI->getOpcode() == X86::MOV32rr ? X86::LEA32r
108 : X86::LEA64r))
109 .addOperand(Dest)
110 .addOperand(Src)
111 .addImm(1)
112 .addReg(0)
113 .addImm(0)
114 .addReg(0);
115 MFI->insert(MBBI, NewMI); // Insert the new inst
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000116 return NewMI;
117 }
118 case X86::ADD64ri32:
119 case X86::ADD64ri8:
120 case X86::ADD64ri32_DB:
121 case X86::ADD64ri8_DB:
122 case X86::ADD32ri:
123 case X86::ADD32ri8:
124 case X86::ADD32ri_DB:
125 case X86::ADD32ri8_DB:
126 case X86::ADD16ri:
127 case X86::ADD16ri8:
128 case X86::ADD16ri_DB:
129 case X86::ADD16ri8_DB:
130 if (!MI->getOperand(2).isImm()) {
131 // convertToThreeAddress will call getImm()
132 // which requires isImm() to be true
Craig Topper062a2ba2014-04-25 05:30:21 +0000133 return nullptr;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000134 }
Preston Gurdf03a6e72013-09-30 23:51:22 +0000135 break;
Preston Gurdf0b62882013-09-30 23:18:42 +0000136 case X86::ADD16rr:
137 case X86::ADD16rr_DB:
138 if (MI->getOperand(1).getReg() != MI->getOperand(2).getReg()) {
139 // if src1 != src2, then convertToThreeAddress will
140 // need to create a Virtual register, which we cannot do
141 // after register allocation.
Craig Topper062a2ba2014-04-25 05:30:21 +0000142 return nullptr;
Preston Gurdf0b62882013-09-30 23:18:42 +0000143 }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000144 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000145 return TII->convertToThreeAddress(MFI, MBBI, nullptr);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000146}
147
Eric Christopher31b81ce2014-06-03 21:01:35 +0000148FunctionPass *llvm::createX86FixupLEAs() { return new FixupLEAPass(); }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000149
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000150bool FixupLEAPass::runOnMachineFunction(MachineFunction &Func) {
Eric Christopherdd240fd2014-06-03 21:01:39 +0000151 MF = &Func;
Eric Christopher4369c9b2015-02-20 08:01:52 +0000152 const X86Subtarget &ST = Func.getSubtarget<X86Subtarget>();
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000153 if (!ST.LEAusesAG() && !ST.slowLEA())
154 return false;
155
Eric Christopherd361ff82015-02-05 19:27:01 +0000156 TII = ST.getInstrInfo();
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000157
158 DEBUG(dbgs() << "Start X86FixupLEAs\n";);
159 // Process all basic blocks.
160 for (MachineFunction::iterator I = Func.begin(), E = Func.end(); I != E; ++I)
161 processBasicBlock(Func, I);
162 DEBUG(dbgs() << "End X86FixupLEAs\n";);
163
164 return true;
165}
166
Eric Christopher31b81ce2014-06-03 21:01:35 +0000167FixupLEAPass::RegUsageState
168FixupLEAPass::usesRegister(MachineOperand &p, MachineBasicBlock::iterator I) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000169 RegUsageState RegUsage = RU_NotUsed;
Eric Christopher31b81ce2014-06-03 21:01:35 +0000170 MachineInstr *MI = I;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000171
172 for (unsigned int i = 0; i < MI->getNumOperands(); ++i) {
Eric Christopher31b81ce2014-06-03 21:01:35 +0000173 MachineOperand &opnd = MI->getOperand(i);
174 if (opnd.isReg() && opnd.getReg() == p.getReg()) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000175 if (opnd.isDef())
176 return RU_Write;
177 RegUsage = RU_Read;
178 }
179 }
180 return RegUsage;
181}
182
183/// getPreviousInstr - Given a reference to an instruction in a basic
184/// block, return a reference to the previous instruction in the block,
185/// wrapping around to the last instruction of the block if the block
186/// branches to itself.
Eric Christopher31b81ce2014-06-03 21:01:35 +0000187static inline bool getPreviousInstr(MachineBasicBlock::iterator &I,
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000188 MachineFunction::iterator MFI) {
189 if (I == MFI->begin()) {
Duncan P. N. Exon Smithd77de642015-10-19 21:48:29 +0000190 if (MFI->isPredecessor(&*MFI)) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000191 I = --MFI->end();
192 return true;
Eric Christopher31b81ce2014-06-03 21:01:35 +0000193 } else
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000194 return false;
195 }
196 --I;
197 return true;
198}
199
Eric Christopher31b81ce2014-06-03 21:01:35 +0000200MachineBasicBlock::iterator
201FixupLEAPass::searchBackwards(MachineOperand &p, MachineBasicBlock::iterator &I,
202 MachineFunction::iterator MFI) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000203 int InstrDistance = 1;
204 MachineBasicBlock::iterator CurInst;
205 static const int INSTR_DISTANCE_THRESHOLD = 5;
206
207 CurInst = I;
208 bool Found;
209 Found = getPreviousInstr(CurInst, MFI);
Eric Christopher31b81ce2014-06-03 21:01:35 +0000210 while (Found && I != CurInst) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000211 if (CurInst->isCall() || CurInst->isInlineAsm())
212 break;
213 if (InstrDistance > INSTR_DISTANCE_THRESHOLD)
214 break; // too far back to make a difference
Eric Christopher31b81ce2014-06-03 21:01:35 +0000215 if (usesRegister(p, CurInst) == RU_Write) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000216 return CurInst;
217 }
Eric Christopherd9134482014-08-04 21:25:23 +0000218 InstrDistance += TII->getInstrLatency(
Eric Christopherd361ff82015-02-05 19:27:01 +0000219 MF->getSubtarget().getInstrItineraryData(), CurInst);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000220 Found = getPreviousInstr(CurInst, MFI);
221 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000222 return nullptr;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000223}
224
Eric Christopher31b81ce2014-06-03 21:01:35 +0000225void FixupLEAPass::processInstruction(MachineBasicBlock::iterator &I,
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000226 MachineFunction::iterator MFI) {
227 // Process a load, store, or LEA instruction.
228 MachineInstr *MI = I;
229 int opcode = MI->getOpcode();
Eric Christopher31b81ce2014-06-03 21:01:35 +0000230 const MCInstrDesc &Desc = MI->getDesc();
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000231 int AddrOffset = X86II::getMemoryOperandNo(Desc.TSFlags, opcode);
232 if (AddrOffset >= 0) {
233 AddrOffset += X86II::getOperandBias(Desc);
Eric Christopher31b81ce2014-06-03 21:01:35 +0000234 MachineOperand &p = MI->getOperand(AddrOffset + X86::AddrBaseReg);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000235 if (p.isReg() && p.getReg() != X86::ESP) {
236 seekLEAFixup(p, I, MFI);
237 }
Eric Christopher31b81ce2014-06-03 21:01:35 +0000238 MachineOperand &q = MI->getOperand(AddrOffset + X86::AddrIndexReg);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000239 if (q.isReg() && q.getReg() != X86::ESP) {
240 seekLEAFixup(q, I, MFI);
241 }
242 }
243}
244
Eric Christopher31b81ce2014-06-03 21:01:35 +0000245void FixupLEAPass::seekLEAFixup(MachineOperand &p,
246 MachineBasicBlock::iterator &I,
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000247 MachineFunction::iterator MFI) {
248 MachineBasicBlock::iterator MBI = searchBackwards(p, I, MFI);
249 if (MBI) {
Eric Christopher31b81ce2014-06-03 21:01:35 +0000250 MachineInstr *NewMI = postRAConvertToLEA(MFI, MBI);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000251 if (NewMI) {
252 ++NumLEAs;
Alexey Volkov6226de62014-05-20 08:55:50 +0000253 DEBUG(dbgs() << "FixLEA: Candidate to replace:"; MBI->dump(););
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000254 // now to replace with an equivalent LEA...
Alexey Volkov6226de62014-05-20 08:55:50 +0000255 DEBUG(dbgs() << "FixLEA: Replaced by: "; NewMI->dump(););
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000256 MFI->erase(MBI);
257 MachineBasicBlock::iterator J =
Eric Christopher31b81ce2014-06-03 21:01:35 +0000258 static_cast<MachineBasicBlock::iterator>(NewMI);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000259 processInstruction(J, MFI);
260 }
261 }
262}
263
Alexey Volkov6226de62014-05-20 08:55:50 +0000264void FixupLEAPass::processInstructionForSLM(MachineBasicBlock::iterator &I,
265 MachineFunction::iterator MFI) {
266 MachineInstr *MI = I;
267 const int opcode = MI->getOpcode();
268 if (opcode != X86::LEA16r && opcode != X86::LEA32r && opcode != X86::LEA64r &&
269 opcode != X86::LEA64_32r)
270 return;
271 if (MI->getOperand(5).getReg() != 0 || !MI->getOperand(4).isImm() ||
272 !TII->isSafeToClobberEFLAGS(*MFI, I))
273 return;
274 const unsigned DstR = MI->getOperand(0).getReg();
275 const unsigned SrcR1 = MI->getOperand(1).getReg();
276 const unsigned SrcR2 = MI->getOperand(3).getReg();
277 if ((SrcR1 == 0 || SrcR1 != DstR) && (SrcR2 == 0 || SrcR2 != DstR))
278 return;
279 if (MI->getOperand(2).getImm() > 1)
280 return;
281 int addrr_opcode, addri_opcode;
282 switch (opcode) {
Craig Topperd3c02f12015-01-05 10:15:49 +0000283 default: llvm_unreachable("Unexpected LEA instruction");
Alexey Volkov6226de62014-05-20 08:55:50 +0000284 case X86::LEA16r:
285 addrr_opcode = X86::ADD16rr;
286 addri_opcode = X86::ADD16ri;
287 break;
288 case X86::LEA32r:
289 addrr_opcode = X86::ADD32rr;
290 addri_opcode = X86::ADD32ri;
291 break;
292 case X86::LEA64_32r:
293 case X86::LEA64r:
294 addrr_opcode = X86::ADD64rr;
295 addri_opcode = X86::ADD64ri32;
296 break;
Alexey Volkov6226de62014-05-20 08:55:50 +0000297 }
298 DEBUG(dbgs() << "FixLEA: Candidate to replace:"; I->dump(););
299 DEBUG(dbgs() << "FixLEA: Replaced by: ";);
Craig Topper66f09ad2014-06-08 22:29:17 +0000300 MachineInstr *NewMI = nullptr;
Alexey Volkov6226de62014-05-20 08:55:50 +0000301 const MachineOperand &Dst = MI->getOperand(0);
302 // Make ADD instruction for two registers writing to LEA's destination
303 if (SrcR1 != 0 && SrcR2 != 0) {
304 const MachineOperand &Src1 = MI->getOperand(SrcR1 == DstR ? 1 : 3);
305 const MachineOperand &Src2 = MI->getOperand(SrcR1 == DstR ? 3 : 1);
306 NewMI = BuildMI(*MF, MI->getDebugLoc(), TII->get(addrr_opcode))
307 .addOperand(Dst)
308 .addOperand(Src1)
309 .addOperand(Src2);
310 MFI->insert(I, NewMI);
311 DEBUG(NewMI->dump(););
312 }
313 // Make ADD instruction for immediate
314 if (MI->getOperand(4).getImm() != 0) {
315 const MachineOperand &SrcR = MI->getOperand(SrcR1 == DstR ? 1 : 3);
316 NewMI = BuildMI(*MF, MI->getDebugLoc(), TII->get(addri_opcode))
317 .addOperand(Dst)
318 .addOperand(SrcR)
319 .addImm(MI->getOperand(4).getImm());
320 MFI->insert(I, NewMI);
321 DEBUG(NewMI->dump(););
322 }
323 if (NewMI) {
324 MFI->erase(I);
325 I = static_cast<MachineBasicBlock::iterator>(NewMI);
326 }
327}
328
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000329bool FixupLEAPass::processBasicBlock(MachineFunction &MF,
330 MachineFunction::iterator MFI) {
331
Alexey Volkov6226de62014-05-20 08:55:50 +0000332 for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I) {
Eric Christopher4369c9b2015-02-20 08:01:52 +0000333 if (MF.getSubtarget<X86Subtarget>().isSLM())
Alexey Volkov6226de62014-05-20 08:55:50 +0000334 processInstructionForSLM(I, MFI);
335 else
336 processInstruction(I, MFI);
337 }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000338 return false;
339}