blob: b41bf99f19b2440ae18ef5b129bbecce5f1c3bf7 [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"
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000020#include "llvm/CodeGen/MachineFunctionPass.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000022#include "llvm/CodeGen/Passes.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000023#include "llvm/CodeGen/TargetInstrInfo.h"
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000024#include "llvm/Support/Debug.h"
25#include "llvm/Support/raw_ostream.h"
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000026using namespace llvm;
27
Lama Saba2ea271b2017-05-18 08:11:50 +000028namespace llvm {
29void initializeFixupLEAPassPass(PassRegistry &);
30}
31
32#define FIXUPLEA_DESC "X86 LEA Fixup"
33#define FIXUPLEA_NAME "x86-fixup-LEAs"
34
35#define DEBUG_TYPE FIXUPLEA_NAME
Chandler Carruth84e68b22014-04-22 02:41:26 +000036
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000037STATISTIC(NumLEAs, "Number of LEA instructions created");
38
39namespace {
Eric Christopher31b81ce2014-06-03 21:01:35 +000040class FixupLEAPass : public MachineFunctionPass {
41 enum RegUsageState { RU_NotUsed, RU_Write, RU_Read };
Lama Saba2ea271b2017-05-18 08:11:50 +000042
Eric Christopher31b81ce2014-06-03 21:01:35 +000043 /// \brief Loop over all of the instructions in the basic block
44 /// replacing applicable instructions with LEA instructions,
45 /// where appropriate.
46 bool processBasicBlock(MachineFunction &MF, MachineFunction::iterator MFI);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000047
Preston Gurd128920d2013-04-25 21:31:33 +000048
Eric Christopher31b81ce2014-06-03 21:01:35 +000049 /// \brief Given a machine register, look for the instruction
50 /// which writes it in the current basic block. If found,
51 /// try to replace it with an equivalent LEA instruction.
Eric Christopher572e03a2015-06-19 01:53:21 +000052 /// If replacement succeeds, then also process the newly created
Eric Christopher31b81ce2014-06-03 21:01:35 +000053 /// instruction.
54 void seekLEAFixup(MachineOperand &p, MachineBasicBlock::iterator &I,
55 MachineFunction::iterator MFI);
Preston Gurd128920d2013-04-25 21:31:33 +000056
Eric Christopher31b81ce2014-06-03 21:01:35 +000057 /// \brief Given a memory access or LEA instruction
58 /// whose address mode uses a base and/or index register, look for
59 /// an opportunity to replace the instruction which sets the base or index
60 /// register with an equivalent LEA instruction.
61 void processInstruction(MachineBasicBlock::iterator &I,
62 MachineFunction::iterator MFI);
Preston Gurd128920d2013-04-25 21:31:33 +000063
Eric Christopher31b81ce2014-06-03 21:01:35 +000064 /// \brief Given a LEA instruction which is unprofitable
65 /// on Silvermont try to replace it with an equivalent ADD instruction
66 void processInstructionForSLM(MachineBasicBlock::iterator &I,
67 MachineFunction::iterator MFI);
Alexey Volkov6226de62014-05-20 08:55:50 +000068
Lama Saba2ea271b2017-05-18 08:11:50 +000069
70 /// \brief Given a LEA instruction which is unprofitable
71 /// on SNB+ try to replace it with other instructions.
72 /// According to Intel's Optimization Reference Manual:
73 /// " For LEA instructions with three source operands and some specific
74 /// situations, instruction latency has increased to 3 cycles, and must
75 /// dispatch via port 1:
76 /// - LEA that has all three source operands: base, index, and offset
77 /// - LEA that uses base and index registers where the base is EBP, RBP,
78 /// or R13
79 /// - LEA that uses RIP relative addressing mode
80 /// - LEA that uses 16-bit addressing mode "
81 /// This function currently handles the first 2 cases only.
82 MachineInstr *processInstrForSlow3OpLEA(MachineInstr &MI,
83 MachineFunction::iterator MFI);
84
Michael Kuperstein12982a82015-11-11 11:44:31 +000085 /// \brief Look for LEAs that add 1 to reg or subtract 1 from reg
86 /// and convert them to INC or DEC respectively.
87 bool fixupIncDec(MachineBasicBlock::iterator &I,
88 MachineFunction::iterator MFI) const;
89
Eric Christopher31b81ce2014-06-03 21:01:35 +000090 /// \brief Determine if an instruction references a machine register
91 /// and, if so, whether it reads or writes the register.
92 RegUsageState usesRegister(MachineOperand &p, MachineBasicBlock::iterator I);
Preston Gurd128920d2013-04-25 21:31:33 +000093
Eric Christopher31b81ce2014-06-03 21:01:35 +000094 /// \brief Step backwards through a basic block, looking
95 /// for an instruction which writes a register within
96 /// a maximum of INSTR_DISTANCE_THRESHOLD instruction latency cycles.
97 MachineBasicBlock::iterator searchBackwards(MachineOperand &p,
98 MachineBasicBlock::iterator &I,
99 MachineFunction::iterator MFI);
Preston Gurd128920d2013-04-25 21:31:33 +0000100
Eric Christopher31b81ce2014-06-03 21:01:35 +0000101 /// \brief if an instruction can be converted to an
102 /// equivalent LEA, insert the new instruction into the basic block
103 /// and return a pointer to it. Otherwise, return zero.
104 MachineInstr *postRAConvertToLEA(MachineFunction::iterator &MFI,
105 MachineBasicBlock::iterator &MBBI) const;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000106
Eric Christopher31b81ce2014-06-03 21:01:35 +0000107public:
Lama Saba2ea271b2017-05-18 08:11:50 +0000108 static char ID;
109
110 StringRef getPassName() const override { return FIXUPLEA_DESC; }
111
112 FixupLEAPass() : MachineFunctionPass(ID) {
113 initializeFixupLEAPassPass(*PassRegistry::getPassRegistry());
114 }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000115
Eric Christopher31b81ce2014-06-03 21:01:35 +0000116 /// \brief Loop over all of the basic blocks,
117 /// replacing instructions by equivalent LEA instructions
118 /// if needed and when possible.
119 bool runOnMachineFunction(MachineFunction &MF) override;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000120
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000121 // This pass runs after regalloc and doesn't support VReg operands.
122 MachineFunctionProperties getRequiredProperties() const override {
123 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000124 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000125 }
126
Eric Christopher31b81ce2014-06-03 21:01:35 +0000127private:
128 MachineFunction *MF;
Eric Christopher31b81ce2014-06-03 21:01:35 +0000129 const X86InstrInfo *TII; // Machine instruction info.
Michael Kuperstein12982a82015-11-11 11:44:31 +0000130 bool OptIncDec;
131 bool OptLEA;
Eric Christopher31b81ce2014-06-03 21:01:35 +0000132};
Reid Kleckner0ad69fc2017-05-16 19:55:03 +0000133}
Lama Saba52e89252017-05-16 16:01:36 +0000134
Lama Saba2ea271b2017-05-18 08:11:50 +0000135char FixupLEAPass::ID = 0;
136
137INITIALIZE_PASS(FixupLEAPass, FIXUPLEA_NAME, FIXUPLEA_DESC, false, false)
138
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000139MachineInstr *
140FixupLEAPass::postRAConvertToLEA(MachineFunction::iterator &MFI,
Preston Gurd128920d2013-04-25 21:31:33 +0000141 MachineBasicBlock::iterator &MBBI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000142 MachineInstr &MI = *MBBI;
143 switch (MI.getOpcode()) {
Alexey Volkov6226de62014-05-20 08:55:50 +0000144 case X86::MOV32rr:
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000145 case X86::MOV64rr: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000146 const MachineOperand &Src = MI.getOperand(1);
147 const MachineOperand &Dest = MI.getOperand(0);
148 MachineInstr *NewMI =
149 BuildMI(*MF, MI.getDebugLoc(),
150 TII->get(MI.getOpcode() == X86::MOV32rr ? X86::LEA32r
151 : X86::LEA64r))
Diana Picus116bbab2017-01-13 09:58:52 +0000152 .add(Dest)
153 .add(Src)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000154 .addImm(1)
155 .addReg(0)
156 .addImm(0)
157 .addReg(0);
Eric Christopher31b81ce2014-06-03 21:01:35 +0000158 MFI->insert(MBBI, NewMI); // Insert the new inst
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000159 return NewMI;
160 }
161 case X86::ADD64ri32:
162 case X86::ADD64ri8:
163 case X86::ADD64ri32_DB:
164 case X86::ADD64ri8_DB:
165 case X86::ADD32ri:
166 case X86::ADD32ri8:
167 case X86::ADD32ri_DB:
168 case X86::ADD32ri8_DB:
169 case X86::ADD16ri:
170 case X86::ADD16ri8:
171 case X86::ADD16ri_DB:
172 case X86::ADD16ri8_DB:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000173 if (!MI.getOperand(2).isImm()) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000174 // convertToThreeAddress will call getImm()
175 // which requires isImm() to be true
Craig Topper062a2ba2014-04-25 05:30:21 +0000176 return nullptr;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000177 }
Preston Gurdf03a6e72013-09-30 23:51:22 +0000178 break;
Preston Gurdf0b62882013-09-30 23:18:42 +0000179 case X86::ADD16rr:
180 case X86::ADD16rr_DB:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000181 if (MI.getOperand(1).getReg() != MI.getOperand(2).getReg()) {
Preston Gurdf0b62882013-09-30 23:18:42 +0000182 // if src1 != src2, then convertToThreeAddress will
183 // need to create a Virtual register, which we cannot do
184 // after register allocation.
Craig Topper062a2ba2014-04-25 05:30:21 +0000185 return nullptr;
Preston Gurdf0b62882013-09-30 23:18:42 +0000186 }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000187 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000188 return TII->convertToThreeAddress(MFI, MI, nullptr);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000189}
190
Eric Christopher31b81ce2014-06-03 21:01:35 +0000191FunctionPass *llvm::createX86FixupLEAs() { return new FixupLEAPass(); }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000192
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000193bool FixupLEAPass::runOnMachineFunction(MachineFunction &Func) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000194 if (skipFunction(Func.getFunction()))
Andrew Kaylor2bee5ef2016-04-26 21:44:24 +0000195 return false;
196
Eric Christopherdd240fd2014-06-03 21:01:39 +0000197 MF = &Func;
Eric Christopher4369c9b2015-02-20 08:01:52 +0000198 const X86Subtarget &ST = Func.getSubtarget<X86Subtarget>();
Matthias Braunf1caa282017-12-15 22:22:58 +0000199 OptIncDec = !ST.slowIncDec() || Func.getFunction().optForMinSize();
Lama Saba2ea271b2017-05-18 08:11:50 +0000200 OptLEA = ST.LEAusesAG() || ST.slowLEA() || ST.slow3OpsLEA();
Michael Kuperstein12982a82015-11-11 11:44:31 +0000201
202 if (!OptLEA && !OptIncDec)
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000203 return false;
204
Eric Christopherd361ff82015-02-05 19:27:01 +0000205 TII = ST.getInstrInfo();
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000206
207 DEBUG(dbgs() << "Start X86FixupLEAs\n";);
208 // Process all basic blocks.
209 for (MachineFunction::iterator I = Func.begin(), E = Func.end(); I != E; ++I)
210 processBasicBlock(Func, I);
211 DEBUG(dbgs() << "End X86FixupLEAs\n";);
212
213 return true;
214}
215
Eric Christopher31b81ce2014-06-03 21:01:35 +0000216FixupLEAPass::RegUsageState
217FixupLEAPass::usesRegister(MachineOperand &p, MachineBasicBlock::iterator I) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000218 RegUsageState RegUsage = RU_NotUsed;
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000219 MachineInstr &MI = *I;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000220
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000221 for (unsigned int i = 0; i < MI.getNumOperands(); ++i) {
222 MachineOperand &opnd = MI.getOperand(i);
Eric Christopher31b81ce2014-06-03 21:01:35 +0000223 if (opnd.isReg() && opnd.getReg() == p.getReg()) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000224 if (opnd.isDef())
225 return RU_Write;
226 RegUsage = RU_Read;
227 }
228 }
229 return RegUsage;
230}
231
232/// getPreviousInstr - Given a reference to an instruction in a basic
233/// block, return a reference to the previous instruction in the block,
234/// wrapping around to the last instruction of the block if the block
235/// branches to itself.
Eric Christopher31b81ce2014-06-03 21:01:35 +0000236static inline bool getPreviousInstr(MachineBasicBlock::iterator &I,
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000237 MachineFunction::iterator MFI) {
238 if (I == MFI->begin()) {
Duncan P. N. Exon Smithd77de642015-10-19 21:48:29 +0000239 if (MFI->isPredecessor(&*MFI)) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000240 I = --MFI->end();
241 return true;
Eric Christopher31b81ce2014-06-03 21:01:35 +0000242 } else
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000243 return false;
244 }
245 --I;
246 return true;
247}
248
Eric Christopher31b81ce2014-06-03 21:01:35 +0000249MachineBasicBlock::iterator
250FixupLEAPass::searchBackwards(MachineOperand &p, MachineBasicBlock::iterator &I,
251 MachineFunction::iterator MFI) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000252 int InstrDistance = 1;
253 MachineBasicBlock::iterator CurInst;
254 static const int INSTR_DISTANCE_THRESHOLD = 5;
255
256 CurInst = I;
257 bool Found;
258 Found = getPreviousInstr(CurInst, MFI);
Eric Christopher31b81ce2014-06-03 21:01:35 +0000259 while (Found && I != CurInst) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000260 if (CurInst->isCall() || CurInst->isInlineAsm())
261 break;
262 if (InstrDistance > INSTR_DISTANCE_THRESHOLD)
263 break; // too far back to make a difference
Eric Christopher31b81ce2014-06-03 21:01:35 +0000264 if (usesRegister(p, CurInst) == RU_Write) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000265 return CurInst;
266 }
Eric Christopherd9134482014-08-04 21:25:23 +0000267 InstrDistance += TII->getInstrLatency(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000268 MF->getSubtarget().getInstrItineraryData(), *CurInst);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000269 Found = getPreviousInstr(CurInst, MFI);
270 }
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000271 return MachineBasicBlock::iterator();
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000272}
273
Lama Saba2ea271b2017-05-18 08:11:50 +0000274static inline bool isLEA(const int Opcode) {
275 return Opcode == X86::LEA16r || Opcode == X86::LEA32r ||
276 Opcode == X86::LEA64r || Opcode == X86::LEA64_32r;
277}
278
279static inline bool isInefficientLEAReg(unsigned int Reg) {
280 return Reg == X86::EBP || Reg == X86::RBP || Reg == X86::R13;
281}
282
283static inline bool isRegOperand(const MachineOperand &Op) {
284 return Op.isReg() && Op.getReg() != X86::NoRegister;
285}
286/// hasIneffecientLEARegs - LEA that uses base and index registers
287/// where the base is EBP, RBP, or R13
288static inline bool hasInefficientLEABaseReg(const MachineOperand &Base,
289 const MachineOperand &Index) {
290 return Base.isReg() && isInefficientLEAReg(Base.getReg()) &&
291 isRegOperand(Index);
292}
293
294static inline bool hasLEAOffset(const MachineOperand &Offset) {
295 return (Offset.isImm() && Offset.getImm() != 0) || Offset.isGlobal();
296}
297
298// LEA instruction that has all three operands: offset, base and index
299static inline bool isThreeOperandsLEA(const MachineOperand &Base,
300 const MachineOperand &Index,
301 const MachineOperand &Offset) {
302 return isRegOperand(Base) && isRegOperand(Index) && hasLEAOffset(Offset);
303}
304
305static inline int getADDrrFromLEA(int LEAOpcode) {
306 switch (LEAOpcode) {
307 default:
308 llvm_unreachable("Unexpected LEA instruction");
309 case X86::LEA16r:
310 return X86::ADD16rr;
311 case X86::LEA32r:
312 return X86::ADD32rr;
313 case X86::LEA64_32r:
314 case X86::LEA64r:
315 return X86::ADD64rr;
316 }
317}
318
319static inline int getADDriFromLEA(int LEAOpcode, const MachineOperand &Offset) {
320 bool IsInt8 = Offset.isImm() && isInt<8>(Offset.getImm());
321 switch (LEAOpcode) {
322 default:
323 llvm_unreachable("Unexpected LEA instruction");
324 case X86::LEA16r:
325 return IsInt8 ? X86::ADD16ri8 : X86::ADD16ri;
326 case X86::LEA32r:
327 case X86::LEA64_32r:
328 return IsInt8 ? X86::ADD32ri8 : X86::ADD32ri;
329 case X86::LEA64r:
330 return IsInt8 ? X86::ADD64ri8 : X86::ADD64ri32;
331 }
Michael Kuperstein12982a82015-11-11 11:44:31 +0000332}
333
334/// isLEASimpleIncOrDec - Does this LEA have one these forms:
335/// lea %reg, 1(%reg)
336/// lea %reg, -1(%reg)
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000337static inline bool isLEASimpleIncOrDec(MachineInstr &LEA) {
338 unsigned SrcReg = LEA.getOperand(1 + X86::AddrBaseReg).getReg();
339 unsigned DstReg = LEA.getOperand(0).getReg();
Michael Kuperstein12982a82015-11-11 11:44:31 +0000340 unsigned AddrDispOp = 1 + X86::AddrDisp;
341 return SrcReg == DstReg &&
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000342 LEA.getOperand(1 + X86::AddrIndexReg).getReg() == 0 &&
343 LEA.getOperand(1 + X86::AddrSegmentReg).getReg() == 0 &&
344 LEA.getOperand(AddrDispOp).isImm() &&
345 (LEA.getOperand(AddrDispOp).getImm() == 1 ||
346 LEA.getOperand(AddrDispOp).getImm() == -1);
Michael Kuperstein12982a82015-11-11 11:44:31 +0000347}
348
349bool FixupLEAPass::fixupIncDec(MachineBasicBlock::iterator &I,
350 MachineFunction::iterator MFI) const {
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000351 MachineInstr &MI = *I;
352 int Opcode = MI.getOpcode();
Michael Kuperstein12982a82015-11-11 11:44:31 +0000353 if (!isLEA(Opcode))
354 return false;
355
356 if (isLEASimpleIncOrDec(MI) && TII->isSafeToClobberEFLAGS(*MFI, I)) {
357 int NewOpcode;
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000358 bool isINC = MI.getOperand(4).getImm() == 1;
Michael Kuperstein12982a82015-11-11 11:44:31 +0000359 switch (Opcode) {
360 case X86::LEA16r:
361 NewOpcode = isINC ? X86::INC16r : X86::DEC16r;
362 break;
363 case X86::LEA32r:
364 case X86::LEA64_32r:
365 NewOpcode = isINC ? X86::INC32r : X86::DEC32r;
366 break;
367 case X86::LEA64r:
368 NewOpcode = isINC ? X86::INC64r : X86::DEC64r;
369 break;
370 }
371
372 MachineInstr *NewMI =
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000373 BuildMI(*MFI, I, MI.getDebugLoc(), TII->get(NewOpcode))
Diana Picus116bbab2017-01-13 09:58:52 +0000374 .add(MI.getOperand(0))
375 .add(MI.getOperand(1));
Michael Kuperstein12982a82015-11-11 11:44:31 +0000376 MFI->erase(I);
377 I = static_cast<MachineBasicBlock::iterator>(NewMI);
378 return true;
379 }
380 return false;
381}
382
Eric Christopher31b81ce2014-06-03 21:01:35 +0000383void FixupLEAPass::processInstruction(MachineBasicBlock::iterator &I,
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000384 MachineFunction::iterator MFI) {
385 // Process a load, store, or LEA instruction.
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000386 MachineInstr &MI = *I;
387 const MCInstrDesc &Desc = MI.getDesc();
Craig Topper477649a2016-04-28 05:58:46 +0000388 int AddrOffset = X86II::getMemoryOperandNo(Desc.TSFlags);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000389 if (AddrOffset >= 0) {
390 AddrOffset += X86II::getOperandBias(Desc);
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000391 MachineOperand &p = MI.getOperand(AddrOffset + X86::AddrBaseReg);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000392 if (p.isReg() && p.getReg() != X86::ESP) {
393 seekLEAFixup(p, I, MFI);
394 }
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000395 MachineOperand &q = MI.getOperand(AddrOffset + X86::AddrIndexReg);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000396 if (q.isReg() && q.getReg() != X86::ESP) {
397 seekLEAFixup(q, I, MFI);
398 }
399 }
400}
401
Eric Christopher31b81ce2014-06-03 21:01:35 +0000402void FixupLEAPass::seekLEAFixup(MachineOperand &p,
403 MachineBasicBlock::iterator &I,
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000404 MachineFunction::iterator MFI) {
405 MachineBasicBlock::iterator MBI = searchBackwards(p, I, MFI);
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000406 if (MBI != MachineBasicBlock::iterator()) {
Eric Christopher31b81ce2014-06-03 21:01:35 +0000407 MachineInstr *NewMI = postRAConvertToLEA(MFI, MBI);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000408 if (NewMI) {
409 ++NumLEAs;
Alexey Volkov6226de62014-05-20 08:55:50 +0000410 DEBUG(dbgs() << "FixLEA: Candidate to replace:"; MBI->dump(););
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000411 // now to replace with an equivalent LEA...
Alexey Volkov6226de62014-05-20 08:55:50 +0000412 DEBUG(dbgs() << "FixLEA: Replaced by: "; NewMI->dump(););
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000413 MFI->erase(MBI);
414 MachineBasicBlock::iterator J =
Eric Christopher31b81ce2014-06-03 21:01:35 +0000415 static_cast<MachineBasicBlock::iterator>(NewMI);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000416 processInstruction(J, MFI);
417 }
418 }
419}
420
Alexey Volkov6226de62014-05-20 08:55:50 +0000421void FixupLEAPass::processInstructionForSLM(MachineBasicBlock::iterator &I,
422 MachineFunction::iterator MFI) {
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000423 MachineInstr &MI = *I;
Lama Saba2ea271b2017-05-18 08:11:50 +0000424 const int Opcode = MI.getOpcode();
425 if (!isLEA(Opcode))
Alexey Volkov6226de62014-05-20 08:55:50 +0000426 return;
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000427 if (MI.getOperand(5).getReg() != 0 || !MI.getOperand(4).isImm() ||
Alexey Volkov6226de62014-05-20 08:55:50 +0000428 !TII->isSafeToClobberEFLAGS(*MFI, I))
429 return;
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000430 const unsigned DstR = MI.getOperand(0).getReg();
431 const unsigned SrcR1 = MI.getOperand(1).getReg();
432 const unsigned SrcR2 = MI.getOperand(3).getReg();
Alexey Volkov6226de62014-05-20 08:55:50 +0000433 if ((SrcR1 == 0 || SrcR1 != DstR) && (SrcR2 == 0 || SrcR2 != DstR))
434 return;
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000435 if (MI.getOperand(2).getImm() > 1)
Alexey Volkov6226de62014-05-20 08:55:50 +0000436 return;
Alexey Volkov6226de62014-05-20 08:55:50 +0000437 DEBUG(dbgs() << "FixLEA: Candidate to replace:"; I->dump(););
438 DEBUG(dbgs() << "FixLEA: Replaced by: ";);
Craig Topper66f09ad2014-06-08 22:29:17 +0000439 MachineInstr *NewMI = nullptr;
Alexey Volkov6226de62014-05-20 08:55:50 +0000440 // Make ADD instruction for two registers writing to LEA's destination
441 if (SrcR1 != 0 && SrcR2 != 0) {
Lama Saba2ea271b2017-05-18 08:11:50 +0000442 const MCInstrDesc &ADDrr = TII->get(getADDrrFromLEA(Opcode));
443 const MachineOperand &Src = MI.getOperand(SrcR1 == DstR ? 3 : 1);
444 NewMI =
445 BuildMI(*MFI, I, MI.getDebugLoc(), ADDrr, DstR).addReg(DstR).add(Src);
Alexey Volkov6226de62014-05-20 08:55:50 +0000446 DEBUG(NewMI->dump(););
447 }
448 // Make ADD instruction for immediate
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000449 if (MI.getOperand(4).getImm() != 0) {
Lama Saba2ea271b2017-05-18 08:11:50 +0000450 const MCInstrDesc &ADDri =
451 TII->get(getADDriFromLEA(Opcode, MI.getOperand(4)));
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000452 const MachineOperand &SrcR = MI.getOperand(SrcR1 == DstR ? 1 : 3);
Lama Saba2ea271b2017-05-18 08:11:50 +0000453 NewMI = BuildMI(*MFI, I, MI.getDebugLoc(), ADDri, DstR)
Diana Picus116bbab2017-01-13 09:58:52 +0000454 .add(SrcR)
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000455 .addImm(MI.getOperand(4).getImm());
Alexey Volkov6226de62014-05-20 08:55:50 +0000456 DEBUG(NewMI->dump(););
457 }
458 if (NewMI) {
459 MFI->erase(I);
Lama Saba2ea271b2017-05-18 08:11:50 +0000460 I = NewMI;
Alexey Volkov6226de62014-05-20 08:55:50 +0000461 }
462}
463
Lama Saba2ea271b2017-05-18 08:11:50 +0000464MachineInstr *
465FixupLEAPass::processInstrForSlow3OpLEA(MachineInstr &MI,
466 MachineFunction::iterator MFI) {
467
468 const int LEAOpcode = MI.getOpcode();
469 if (!isLEA(LEAOpcode))
470 return nullptr;
471
472 const MachineOperand &Dst = MI.getOperand(0);
473 const MachineOperand &Base = MI.getOperand(1);
474 const MachineOperand &Scale = MI.getOperand(2);
475 const MachineOperand &Index = MI.getOperand(3);
476 const MachineOperand &Offset = MI.getOperand(4);
477 const MachineOperand &Segment = MI.getOperand(5);
478
479 if (!(isThreeOperandsLEA(Base, Index, Offset) ||
480 hasInefficientLEABaseReg(Base, Index)) ||
481 !TII->isSafeToClobberEFLAGS(*MFI, MI) ||
482 Segment.getReg() != X86::NoRegister)
483 return nullptr;
484
485 unsigned int DstR = Dst.getReg();
486 unsigned int BaseR = Base.getReg();
487 unsigned int IndexR = Index.getReg();
488 unsigned SSDstR =
489 (LEAOpcode == X86::LEA64_32r) ? getX86SubSuperRegister(DstR, 64) : DstR;
490 bool IsScale1 = Scale.getImm() == 1;
491 bool IsInefficientBase = isInefficientLEAReg(BaseR);
492 bool IsInefficientIndex = isInefficientLEAReg(IndexR);
493
494 // Skip these cases since it takes more than 2 instructions
495 // to replace the LEA instruction.
496 if (IsInefficientBase && SSDstR == BaseR && !IsScale1)
497 return nullptr;
498 if (LEAOpcode == X86::LEA64_32r && IsInefficientBase &&
499 (IsInefficientIndex || !IsScale1))
500 return nullptr;
501
502 const DebugLoc DL = MI.getDebugLoc();
503 const MCInstrDesc &ADDrr = TII->get(getADDrrFromLEA(LEAOpcode));
504 const MCInstrDesc &ADDri = TII->get(getADDriFromLEA(LEAOpcode, Offset));
505
506 DEBUG(dbgs() << "FixLEA: Candidate to replace:"; MI.dump(););
507 DEBUG(dbgs() << "FixLEA: Replaced by: ";);
508
509 // First try to replace LEA with one or two (for the 3-op LEA case)
510 // add instructions:
511 // 1.lea (%base,%index,1), %base => add %index,%base
512 // 2.lea (%base,%index,1), %index => add %base,%index
513 if (IsScale1 && (DstR == BaseR || DstR == IndexR)) {
514 const MachineOperand &Src = DstR == BaseR ? Index : Base;
515 MachineInstr *NewMI =
516 BuildMI(*MFI, MI, DL, ADDrr, DstR).addReg(DstR).add(Src);
517 DEBUG(NewMI->dump(););
518 // Create ADD instruction for the Offset in case of 3-Ops LEA.
519 if (hasLEAOffset(Offset)) {
520 NewMI = BuildMI(*MFI, MI, DL, ADDri, DstR).addReg(DstR).add(Offset);
521 DEBUG(NewMI->dump(););
522 }
523 return NewMI;
524 }
525 // If the base is inefficient try switching the index and base operands,
526 // otherwise just break the 3-Ops LEA inst into 2-Ops LEA + ADD instruction:
527 // lea offset(%base,%index,scale),%dst =>
528 // lea (%base,%index,scale); add offset,%dst
529 if (!IsInefficientBase || (!IsInefficientIndex && IsScale1)) {
530 MachineInstr *NewMI = BuildMI(*MFI, MI, DL, TII->get(LEAOpcode))
531 .add(Dst)
532 .add(IsInefficientBase ? Index : Base)
533 .add(Scale)
534 .add(IsInefficientBase ? Base : Index)
535 .addImm(0)
536 .add(Segment);
537 DEBUG(NewMI->dump(););
538 // Create ADD instruction for the Offset in case of 3-Ops LEA.
539 if (hasLEAOffset(Offset)) {
540 NewMI = BuildMI(*MFI, MI, DL, ADDri, DstR).addReg(DstR).add(Offset);
541 DEBUG(NewMI->dump(););
542 }
543 return NewMI;
544 }
545 // Handle the rest of the cases with inefficient base register:
546 assert(SSDstR != BaseR && "SSDstR == BaseR should be handled already!");
547 assert(IsInefficientBase && "efficient base should be handled already!");
548
549 // lea (%base,%index,1), %dst => mov %base,%dst; add %index,%dst
550 if (IsScale1 && !hasLEAOffset(Offset)) {
551 TII->copyPhysReg(*MFI, MI, DL, DstR, BaseR, Base.isKill());
552 DEBUG(MI.getPrevNode()->dump(););
553
554 MachineInstr *NewMI =
555 BuildMI(*MFI, MI, DL, ADDrr, DstR).addReg(DstR).add(Index);
556 DEBUG(NewMI->dump(););
557 return NewMI;
558 }
559 // lea offset(%base,%index,scale), %dst =>
560 // lea offset( ,%index,scale), %dst; add %base,%dst
561 MachineInstr *NewMI = BuildMI(*MFI, MI, DL, TII->get(LEAOpcode))
562 .add(Dst)
563 .addReg(0)
564 .add(Scale)
565 .add(Index)
566 .add(Offset)
567 .add(Segment);
568 DEBUG(NewMI->dump(););
569
570 NewMI = BuildMI(*MFI, MI, DL, ADDrr, DstR).addReg(DstR).add(Base);
571 DEBUG(NewMI->dump(););
572 return NewMI;
573}
574
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000575bool FixupLEAPass::processBasicBlock(MachineFunction &MF,
576 MachineFunction::iterator MFI) {
577
Alexey Volkov6226de62014-05-20 08:55:50 +0000578 for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I) {
Michael Kuperstein12982a82015-11-11 11:44:31 +0000579 if (OptIncDec)
580 if (fixupIncDec(I, MFI))
581 continue;
582
583 if (OptLEA) {
584 if (MF.getSubtarget<X86Subtarget>().isSLM())
585 processInstructionForSLM(I, MFI);
Lama Saba2ea271b2017-05-18 08:11:50 +0000586
587 else {
588 if (MF.getSubtarget<X86Subtarget>().slow3OpsLEA()) {
589 if (auto *NewMI = processInstrForSlow3OpLEA(*I, MFI)) {
590 MFI->erase(I);
591 I = NewMI;
592 }
593 } else
594 processInstruction(I, MFI);
595 }
Michael Kuperstein12982a82015-11-11 11:44:31 +0000596 }
Alexey Volkov6226de62014-05-20 08:55:50 +0000597 }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000598 return false;
599}