blob: 12095917ca30cce7ad6c516ef81a376de1c7ffdd [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.
Michael Kuperstein12982a82015-11-11 11:44:31 +000012// When optimizing for size it replaces suitable LEAs with INC or DEC.
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000013//
14//===----------------------------------------------------------------------===//
15
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000016#include "X86.h"
17#include "X86InstrInfo.h"
18#include "X86Subtarget.h"
19#include "llvm/ADT/Statistic.h"
20#include "llvm/CodeGen/LiveVariables.h"
21#include "llvm/CodeGen/MachineFunctionPass.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
24#include "llvm/CodeGen/Passes.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/Support/raw_ostream.h"
27#include "llvm/Target/TargetInstrInfo.h"
28using namespace llvm;
29
Chandler Carruth84e68b22014-04-22 02:41:26 +000030#define DEBUG_TYPE "x86-fixup-LEAs"
31
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000032STATISTIC(NumLEAs, "Number of LEA instructions created");
33
34namespace {
Eric Christopher31b81ce2014-06-03 21:01:35 +000035class FixupLEAPass : public MachineFunctionPass {
36 enum RegUsageState { RU_NotUsed, RU_Write, RU_Read };
37 static char ID;
38 /// \brief Loop over all of the instructions in the basic block
39 /// replacing applicable instructions with LEA instructions,
40 /// where appropriate.
41 bool processBasicBlock(MachineFunction &MF, MachineFunction::iterator MFI);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000042
Mehdi Amini117296c2016-10-01 02:56:57 +000043 StringRef getPassName() const override { return "X86 LEA Fixup"; }
Preston Gurd128920d2013-04-25 21:31:33 +000044
Eric Christopher31b81ce2014-06-03 21:01:35 +000045 /// \brief Given a machine register, look for the instruction
46 /// which writes it in the current basic block. If found,
47 /// try to replace it with an equivalent LEA instruction.
Eric Christopher572e03a2015-06-19 01:53:21 +000048 /// If replacement succeeds, then also process the newly created
Eric Christopher31b81ce2014-06-03 21:01:35 +000049 /// instruction.
50 void seekLEAFixup(MachineOperand &p, MachineBasicBlock::iterator &I,
51 MachineFunction::iterator MFI);
Preston Gurd128920d2013-04-25 21:31:33 +000052
Eric Christopher31b81ce2014-06-03 21:01:35 +000053 /// \brief Given a memory access or LEA instruction
54 /// whose address mode uses a base and/or index register, look for
55 /// an opportunity to replace the instruction which sets the base or index
56 /// register with an equivalent LEA instruction.
57 void processInstruction(MachineBasicBlock::iterator &I,
58 MachineFunction::iterator MFI);
Preston Gurd128920d2013-04-25 21:31:33 +000059
Eric Christopher31b81ce2014-06-03 21:01:35 +000060 /// \brief Given a LEA instruction which is unprofitable
61 /// on Silvermont try to replace it with an equivalent ADD instruction
62 void processInstructionForSLM(MachineBasicBlock::iterator &I,
63 MachineFunction::iterator MFI);
Alexey Volkov6226de62014-05-20 08:55:50 +000064
Michael Kuperstein12982a82015-11-11 11:44:31 +000065 /// \brief Look for LEAs that add 1 to reg or subtract 1 from reg
66 /// and convert them to INC or DEC respectively.
67 bool fixupIncDec(MachineBasicBlock::iterator &I,
68 MachineFunction::iterator MFI) const;
69
Eric Christopher31b81ce2014-06-03 21:01:35 +000070 /// \brief Determine if an instruction references a machine register
71 /// and, if so, whether it reads or writes the register.
72 RegUsageState usesRegister(MachineOperand &p, MachineBasicBlock::iterator I);
Preston Gurd128920d2013-04-25 21:31:33 +000073
Eric Christopher31b81ce2014-06-03 21:01:35 +000074 /// \brief Step backwards through a basic block, looking
75 /// for an instruction which writes a register within
76 /// a maximum of INSTR_DISTANCE_THRESHOLD instruction latency cycles.
77 MachineBasicBlock::iterator searchBackwards(MachineOperand &p,
78 MachineBasicBlock::iterator &I,
79 MachineFunction::iterator MFI);
Preston Gurd128920d2013-04-25 21:31:33 +000080
Eric Christopher31b81ce2014-06-03 21:01:35 +000081 /// \brief if an instruction can be converted to an
82 /// equivalent LEA, insert the new instruction into the basic block
83 /// and return a pointer to it. Otherwise, return zero.
84 MachineInstr *postRAConvertToLEA(MachineFunction::iterator &MFI,
85 MachineBasicBlock::iterator &MBBI) const;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000086
Eric Christopher31b81ce2014-06-03 21:01:35 +000087public:
88 FixupLEAPass() : MachineFunctionPass(ID) {}
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000089
Eric Christopher31b81ce2014-06-03 21:01:35 +000090 /// \brief Loop over all of the basic blocks,
91 /// replacing instructions by equivalent LEA instructions
92 /// if needed and when possible.
93 bool runOnMachineFunction(MachineFunction &MF) override;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000094
Derek Schuff1dbf7a52016-04-04 17:09:25 +000095 // This pass runs after regalloc and doesn't support VReg operands.
96 MachineFunctionProperties getRequiredProperties() const override {
97 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000098 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000099 }
100
Eric Christopher31b81ce2014-06-03 21:01:35 +0000101private:
102 MachineFunction *MF;
Eric Christopher31b81ce2014-06-03 21:01:35 +0000103 const X86InstrInfo *TII; // Machine instruction info.
Michael Kuperstein12982a82015-11-11 11:44:31 +0000104 bool OptIncDec;
105 bool OptLEA;
Eric Christopher31b81ce2014-06-03 21:01:35 +0000106};
107char FixupLEAPass::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000108}
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000109
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000110MachineInstr *
111FixupLEAPass::postRAConvertToLEA(MachineFunction::iterator &MFI,
Preston Gurd128920d2013-04-25 21:31:33 +0000112 MachineBasicBlock::iterator &MBBI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000113 MachineInstr &MI = *MBBI;
114 switch (MI.getOpcode()) {
Alexey Volkov6226de62014-05-20 08:55:50 +0000115 case X86::MOV32rr:
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000116 case X86::MOV64rr: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000117 const MachineOperand &Src = MI.getOperand(1);
118 const MachineOperand &Dest = MI.getOperand(0);
119 MachineInstr *NewMI =
120 BuildMI(*MF, MI.getDebugLoc(),
121 TII->get(MI.getOpcode() == X86::MOV32rr ? X86::LEA32r
122 : X86::LEA64r))
123 .addOperand(Dest)
124 .addOperand(Src)
125 .addImm(1)
126 .addReg(0)
127 .addImm(0)
128 .addReg(0);
Eric Christopher31b81ce2014-06-03 21:01:35 +0000129 MFI->insert(MBBI, NewMI); // Insert the new inst
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000130 return NewMI;
131 }
132 case X86::ADD64ri32:
133 case X86::ADD64ri8:
134 case X86::ADD64ri32_DB:
135 case X86::ADD64ri8_DB:
136 case X86::ADD32ri:
137 case X86::ADD32ri8:
138 case X86::ADD32ri_DB:
139 case X86::ADD32ri8_DB:
140 case X86::ADD16ri:
141 case X86::ADD16ri8:
142 case X86::ADD16ri_DB:
143 case X86::ADD16ri8_DB:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000144 if (!MI.getOperand(2).isImm()) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000145 // convertToThreeAddress will call getImm()
146 // which requires isImm() to be true
Craig Topper062a2ba2014-04-25 05:30:21 +0000147 return nullptr;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000148 }
Preston Gurdf03a6e72013-09-30 23:51:22 +0000149 break;
Preston Gurdf0b62882013-09-30 23:18:42 +0000150 case X86::ADD16rr:
151 case X86::ADD16rr_DB:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000152 if (MI.getOperand(1).getReg() != MI.getOperand(2).getReg()) {
Preston Gurdf0b62882013-09-30 23:18:42 +0000153 // if src1 != src2, then convertToThreeAddress will
154 // need to create a Virtual register, which we cannot do
155 // after register allocation.
Craig Topper062a2ba2014-04-25 05:30:21 +0000156 return nullptr;
Preston Gurdf0b62882013-09-30 23:18:42 +0000157 }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000158 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000159 return TII->convertToThreeAddress(MFI, MI, nullptr);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000160}
161
Eric Christopher31b81ce2014-06-03 21:01:35 +0000162FunctionPass *llvm::createX86FixupLEAs() { return new FixupLEAPass(); }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000163
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000164bool FixupLEAPass::runOnMachineFunction(MachineFunction &Func) {
Andrew Kaylor2bee5ef2016-04-26 21:44:24 +0000165 if (skipFunction(*Func.getFunction()))
166 return false;
167
Eric Christopherdd240fd2014-06-03 21:01:39 +0000168 MF = &Func;
Eric Christopher4369c9b2015-02-20 08:01:52 +0000169 const X86Subtarget &ST = Func.getSubtarget<X86Subtarget>();
Michael Kuperstein12982a82015-11-11 11:44:31 +0000170 OptIncDec = !ST.slowIncDec() || Func.getFunction()->optForMinSize();
171 OptLEA = ST.LEAusesAG() || ST.slowLEA();
172
173 if (!OptLEA && !OptIncDec)
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000174 return false;
175
Eric Christopherd361ff82015-02-05 19:27:01 +0000176 TII = ST.getInstrInfo();
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000177
178 DEBUG(dbgs() << "Start X86FixupLEAs\n";);
179 // Process all basic blocks.
180 for (MachineFunction::iterator I = Func.begin(), E = Func.end(); I != E; ++I)
181 processBasicBlock(Func, I);
182 DEBUG(dbgs() << "End X86FixupLEAs\n";);
183
184 return true;
185}
186
Eric Christopher31b81ce2014-06-03 21:01:35 +0000187FixupLEAPass::RegUsageState
188FixupLEAPass::usesRegister(MachineOperand &p, MachineBasicBlock::iterator I) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000189 RegUsageState RegUsage = RU_NotUsed;
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000190 MachineInstr &MI = *I;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000191
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000192 for (unsigned int i = 0; i < MI.getNumOperands(); ++i) {
193 MachineOperand &opnd = MI.getOperand(i);
Eric Christopher31b81ce2014-06-03 21:01:35 +0000194 if (opnd.isReg() && opnd.getReg() == p.getReg()) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000195 if (opnd.isDef())
196 return RU_Write;
197 RegUsage = RU_Read;
198 }
199 }
200 return RegUsage;
201}
202
203/// getPreviousInstr - Given a reference to an instruction in a basic
204/// block, return a reference to the previous instruction in the block,
205/// wrapping around to the last instruction of the block if the block
206/// branches to itself.
Eric Christopher31b81ce2014-06-03 21:01:35 +0000207static inline bool getPreviousInstr(MachineBasicBlock::iterator &I,
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000208 MachineFunction::iterator MFI) {
209 if (I == MFI->begin()) {
Duncan P. N. Exon Smithd77de642015-10-19 21:48:29 +0000210 if (MFI->isPredecessor(&*MFI)) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000211 I = --MFI->end();
212 return true;
Eric Christopher31b81ce2014-06-03 21:01:35 +0000213 } else
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000214 return false;
215 }
216 --I;
217 return true;
218}
219
Eric Christopher31b81ce2014-06-03 21:01:35 +0000220MachineBasicBlock::iterator
221FixupLEAPass::searchBackwards(MachineOperand &p, MachineBasicBlock::iterator &I,
222 MachineFunction::iterator MFI) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000223 int InstrDistance = 1;
224 MachineBasicBlock::iterator CurInst;
225 static const int INSTR_DISTANCE_THRESHOLD = 5;
226
227 CurInst = I;
228 bool Found;
229 Found = getPreviousInstr(CurInst, MFI);
Eric Christopher31b81ce2014-06-03 21:01:35 +0000230 while (Found && I != CurInst) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000231 if (CurInst->isCall() || CurInst->isInlineAsm())
232 break;
233 if (InstrDistance > INSTR_DISTANCE_THRESHOLD)
234 break; // too far back to make a difference
Eric Christopher31b81ce2014-06-03 21:01:35 +0000235 if (usesRegister(p, CurInst) == RU_Write) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000236 return CurInst;
237 }
Eric Christopherd9134482014-08-04 21:25:23 +0000238 InstrDistance += TII->getInstrLatency(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000239 MF->getSubtarget().getInstrItineraryData(), *CurInst);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000240 Found = getPreviousInstr(CurInst, MFI);
241 }
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000242 return MachineBasicBlock::iterator();
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000243}
244
Michael Kuperstein12982a82015-11-11 11:44:31 +0000245static inline bool isLEA(const int opcode) {
246 return opcode == X86::LEA16r || opcode == X86::LEA32r ||
247 opcode == X86::LEA64r || opcode == X86::LEA64_32r;
248}
249
250/// isLEASimpleIncOrDec - Does this LEA have one these forms:
251/// lea %reg, 1(%reg)
252/// lea %reg, -1(%reg)
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000253static inline bool isLEASimpleIncOrDec(MachineInstr &LEA) {
254 unsigned SrcReg = LEA.getOperand(1 + X86::AddrBaseReg).getReg();
255 unsigned DstReg = LEA.getOperand(0).getReg();
Michael Kuperstein12982a82015-11-11 11:44:31 +0000256 unsigned AddrDispOp = 1 + X86::AddrDisp;
257 return SrcReg == DstReg &&
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000258 LEA.getOperand(1 + X86::AddrIndexReg).getReg() == 0 &&
259 LEA.getOperand(1 + X86::AddrSegmentReg).getReg() == 0 &&
260 LEA.getOperand(AddrDispOp).isImm() &&
261 (LEA.getOperand(AddrDispOp).getImm() == 1 ||
262 LEA.getOperand(AddrDispOp).getImm() == -1);
Michael Kuperstein12982a82015-11-11 11:44:31 +0000263}
264
265bool FixupLEAPass::fixupIncDec(MachineBasicBlock::iterator &I,
266 MachineFunction::iterator MFI) const {
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000267 MachineInstr &MI = *I;
268 int Opcode = MI.getOpcode();
Michael Kuperstein12982a82015-11-11 11:44:31 +0000269 if (!isLEA(Opcode))
270 return false;
271
272 if (isLEASimpleIncOrDec(MI) && TII->isSafeToClobberEFLAGS(*MFI, I)) {
273 int NewOpcode;
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000274 bool isINC = MI.getOperand(4).getImm() == 1;
Michael Kuperstein12982a82015-11-11 11:44:31 +0000275 switch (Opcode) {
276 case X86::LEA16r:
277 NewOpcode = isINC ? X86::INC16r : X86::DEC16r;
278 break;
279 case X86::LEA32r:
280 case X86::LEA64_32r:
281 NewOpcode = isINC ? X86::INC32r : X86::DEC32r;
282 break;
283 case X86::LEA64r:
284 NewOpcode = isINC ? X86::INC64r : X86::DEC64r;
285 break;
286 }
287
288 MachineInstr *NewMI =
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000289 BuildMI(*MFI, I, MI.getDebugLoc(), TII->get(NewOpcode))
290 .addOperand(MI.getOperand(0))
291 .addOperand(MI.getOperand(1));
Michael Kuperstein12982a82015-11-11 11:44:31 +0000292 MFI->erase(I);
293 I = static_cast<MachineBasicBlock::iterator>(NewMI);
294 return true;
295 }
296 return false;
297}
298
Eric Christopher31b81ce2014-06-03 21:01:35 +0000299void FixupLEAPass::processInstruction(MachineBasicBlock::iterator &I,
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000300 MachineFunction::iterator MFI) {
301 // Process a load, store, or LEA instruction.
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000302 MachineInstr &MI = *I;
303 const MCInstrDesc &Desc = MI.getDesc();
Craig Topper477649a2016-04-28 05:58:46 +0000304 int AddrOffset = X86II::getMemoryOperandNo(Desc.TSFlags);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000305 if (AddrOffset >= 0) {
306 AddrOffset += X86II::getOperandBias(Desc);
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000307 MachineOperand &p = MI.getOperand(AddrOffset + X86::AddrBaseReg);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000308 if (p.isReg() && p.getReg() != X86::ESP) {
309 seekLEAFixup(p, I, MFI);
310 }
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000311 MachineOperand &q = MI.getOperand(AddrOffset + X86::AddrIndexReg);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000312 if (q.isReg() && q.getReg() != X86::ESP) {
313 seekLEAFixup(q, I, MFI);
314 }
315 }
316}
317
Eric Christopher31b81ce2014-06-03 21:01:35 +0000318void FixupLEAPass::seekLEAFixup(MachineOperand &p,
319 MachineBasicBlock::iterator &I,
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000320 MachineFunction::iterator MFI) {
321 MachineBasicBlock::iterator MBI = searchBackwards(p, I, MFI);
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000322 if (MBI != MachineBasicBlock::iterator()) {
Eric Christopher31b81ce2014-06-03 21:01:35 +0000323 MachineInstr *NewMI = postRAConvertToLEA(MFI, MBI);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000324 if (NewMI) {
325 ++NumLEAs;
Alexey Volkov6226de62014-05-20 08:55:50 +0000326 DEBUG(dbgs() << "FixLEA: Candidate to replace:"; MBI->dump(););
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000327 // now to replace with an equivalent LEA...
Alexey Volkov6226de62014-05-20 08:55:50 +0000328 DEBUG(dbgs() << "FixLEA: Replaced by: "; NewMI->dump(););
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000329 MFI->erase(MBI);
330 MachineBasicBlock::iterator J =
Eric Christopher31b81ce2014-06-03 21:01:35 +0000331 static_cast<MachineBasicBlock::iterator>(NewMI);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000332 processInstruction(J, MFI);
333 }
334 }
335}
336
Alexey Volkov6226de62014-05-20 08:55:50 +0000337void FixupLEAPass::processInstructionForSLM(MachineBasicBlock::iterator &I,
338 MachineFunction::iterator MFI) {
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000339 MachineInstr &MI = *I;
340 const int opcode = MI.getOpcode();
Michael Kuperstein12982a82015-11-11 11:44:31 +0000341 if (!isLEA(opcode))
Alexey Volkov6226de62014-05-20 08:55:50 +0000342 return;
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000343 if (MI.getOperand(5).getReg() != 0 || !MI.getOperand(4).isImm() ||
Alexey Volkov6226de62014-05-20 08:55:50 +0000344 !TII->isSafeToClobberEFLAGS(*MFI, I))
345 return;
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000346 const unsigned DstR = MI.getOperand(0).getReg();
347 const unsigned SrcR1 = MI.getOperand(1).getReg();
348 const unsigned SrcR2 = MI.getOperand(3).getReg();
Alexey Volkov6226de62014-05-20 08:55:50 +0000349 if ((SrcR1 == 0 || SrcR1 != DstR) && (SrcR2 == 0 || SrcR2 != DstR))
350 return;
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000351 if (MI.getOperand(2).getImm() > 1)
Alexey Volkov6226de62014-05-20 08:55:50 +0000352 return;
353 int addrr_opcode, addri_opcode;
354 switch (opcode) {
Michael Kuperstein12982a82015-11-11 11:44:31 +0000355 default:
356 llvm_unreachable("Unexpected LEA instruction");
Alexey Volkov6226de62014-05-20 08:55:50 +0000357 case X86::LEA16r:
358 addrr_opcode = X86::ADD16rr;
359 addri_opcode = X86::ADD16ri;
360 break;
361 case X86::LEA32r:
362 addrr_opcode = X86::ADD32rr;
363 addri_opcode = X86::ADD32ri;
364 break;
365 case X86::LEA64_32r:
366 case X86::LEA64r:
367 addrr_opcode = X86::ADD64rr;
368 addri_opcode = X86::ADD64ri32;
369 break;
Alexey Volkov6226de62014-05-20 08:55:50 +0000370 }
371 DEBUG(dbgs() << "FixLEA: Candidate to replace:"; I->dump(););
372 DEBUG(dbgs() << "FixLEA: Replaced by: ";);
Craig Topper66f09ad2014-06-08 22:29:17 +0000373 MachineInstr *NewMI = nullptr;
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000374 const MachineOperand &Dst = MI.getOperand(0);
Alexey Volkov6226de62014-05-20 08:55:50 +0000375 // Make ADD instruction for two registers writing to LEA's destination
376 if (SrcR1 != 0 && SrcR2 != 0) {
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000377 const MachineOperand &Src1 = MI.getOperand(SrcR1 == DstR ? 1 : 3);
378 const MachineOperand &Src2 = MI.getOperand(SrcR1 == DstR ? 3 : 1);
379 NewMI = BuildMI(*MF, MI.getDebugLoc(), TII->get(addrr_opcode))
Alexey Volkov6226de62014-05-20 08:55:50 +0000380 .addOperand(Dst)
381 .addOperand(Src1)
382 .addOperand(Src2);
383 MFI->insert(I, NewMI);
384 DEBUG(NewMI->dump(););
385 }
386 // Make ADD instruction for immediate
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000387 if (MI.getOperand(4).getImm() != 0) {
388 const MachineOperand &SrcR = MI.getOperand(SrcR1 == DstR ? 1 : 3);
389 NewMI = BuildMI(*MF, MI.getDebugLoc(), TII->get(addri_opcode))
Alexey Volkov6226de62014-05-20 08:55:50 +0000390 .addOperand(Dst)
391 .addOperand(SrcR)
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000392 .addImm(MI.getOperand(4).getImm());
Alexey Volkov6226de62014-05-20 08:55:50 +0000393 MFI->insert(I, NewMI);
394 DEBUG(NewMI->dump(););
395 }
396 if (NewMI) {
397 MFI->erase(I);
398 I = static_cast<MachineBasicBlock::iterator>(NewMI);
399 }
400}
401
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000402bool FixupLEAPass::processBasicBlock(MachineFunction &MF,
403 MachineFunction::iterator MFI) {
404
Alexey Volkov6226de62014-05-20 08:55:50 +0000405 for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I) {
Michael Kuperstein12982a82015-11-11 11:44:31 +0000406 if (OptIncDec)
407 if (fixupIncDec(I, MFI))
408 continue;
409
410 if (OptLEA) {
411 if (MF.getSubtarget<X86Subtarget>().isSLM())
412 processInstructionForSLM(I, MFI);
413 else
414 processInstruction(I, MFI);
415 }
Alexey Volkov6226de62014-05-20 08:55:50 +0000416 }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000417 return false;
418}