blob: da5f16957506b773471c6961f10dbaf82b776e1b [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"
Simon Pilgrima3a9d8122018-04-13 15:09:39 +000023#include "llvm/CodeGen/TargetSchedule.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 +000028#define FIXUPLEA_DESC "X86 LEA Fixup"
29#define FIXUPLEA_NAME "x86-fixup-LEAs"
30
31#define DEBUG_TYPE FIXUPLEA_NAME
Chandler Carruth84e68b22014-04-22 02:41:26 +000032
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000033STATISTIC(NumLEAs, "Number of LEA instructions created");
34
35namespace {
Eric Christopher31b81ce2014-06-03 21:01:35 +000036class FixupLEAPass : public MachineFunctionPass {
37 enum RegUsageState { RU_NotUsed, RU_Write, RU_Read };
Lama Saba2ea271b2017-05-18 08:11:50 +000038
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000039 /// Loop over all of the instructions in the basic block
Eric Christopher31b81ce2014-06-03 21:01:35 +000040 /// replacing applicable instructions with LEA instructions,
41 /// where appropriate.
42 bool processBasicBlock(MachineFunction &MF, MachineFunction::iterator MFI);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000043
Preston Gurd128920d2013-04-25 21:31:33 +000044
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000045 /// Given a machine register, look for the instruction
Eric Christopher31b81ce2014-06-03 21:01:35 +000046 /// 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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000053 /// Given a memory access or LEA instruction
Eric Christopher31b81ce2014-06-03 21:01:35 +000054 /// 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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000060 /// Given a LEA instruction which is unprofitable
Simon Pilgrimd5d72242018-11-01 14:57:07 +000061 /// on SlowLEA targets try to replace it with an equivalent ADD instruction.
62 void processInstructionForSlowLEA(MachineBasicBlock::iterator &I,
63 MachineFunction::iterator MFI);
Lama Saba2ea271b2017-05-18 08:11:50 +000064
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000065 /// Given a LEA instruction which is unprofitable
Lama Saba2ea271b2017-05-18 08:11:50 +000066 /// on SNB+ try to replace it with other instructions.
67 /// According to Intel's Optimization Reference Manual:
68 /// " For LEA instructions with three source operands and some specific
69 /// situations, instruction latency has increased to 3 cycles, and must
70 /// dispatch via port 1:
71 /// - LEA that has all three source operands: base, index, and offset
72 /// - LEA that uses base and index registers where the base is EBP, RBP,
73 /// or R13
74 /// - LEA that uses RIP relative addressing mode
75 /// - LEA that uses 16-bit addressing mode "
76 /// This function currently handles the first 2 cases only.
77 MachineInstr *processInstrForSlow3OpLEA(MachineInstr &MI,
78 MachineFunction::iterator MFI);
79
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000080 /// Look for LEAs that add 1 to reg or subtract 1 from reg
Michael Kuperstein12982a82015-11-11 11:44:31 +000081 /// and convert them to INC or DEC respectively.
82 bool fixupIncDec(MachineBasicBlock::iterator &I,
83 MachineFunction::iterator MFI) const;
84
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000085 /// Determine if an instruction references a machine register
Eric Christopher31b81ce2014-06-03 21:01:35 +000086 /// and, if so, whether it reads or writes the register.
87 RegUsageState usesRegister(MachineOperand &p, MachineBasicBlock::iterator I);
Preston Gurd128920d2013-04-25 21:31:33 +000088
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000089 /// Step backwards through a basic block, looking
Eric Christopher31b81ce2014-06-03 21:01:35 +000090 /// for an instruction which writes a register within
91 /// a maximum of INSTR_DISTANCE_THRESHOLD instruction latency cycles.
92 MachineBasicBlock::iterator searchBackwards(MachineOperand &p,
93 MachineBasicBlock::iterator &I,
94 MachineFunction::iterator MFI);
Preston Gurd128920d2013-04-25 21:31:33 +000095
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000096 /// if an instruction can be converted to an
Eric Christopher31b81ce2014-06-03 21:01:35 +000097 /// equivalent LEA, insert the new instruction into the basic block
98 /// and return a pointer to it. Otherwise, return zero.
99 MachineInstr *postRAConvertToLEA(MachineFunction::iterator &MFI,
100 MachineBasicBlock::iterator &MBBI) const;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000101
Eric Christopher31b81ce2014-06-03 21:01:35 +0000102public:
Lama Saba2ea271b2017-05-18 08:11:50 +0000103 static char ID;
104
105 StringRef getPassName() const override { return FIXUPLEA_DESC; }
106
107 FixupLEAPass() : MachineFunctionPass(ID) {
108 initializeFixupLEAPassPass(*PassRegistry::getPassRegistry());
109 }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000110
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000111 /// Loop over all of the basic blocks,
Eric Christopher31b81ce2014-06-03 21:01:35 +0000112 /// replacing instructions by equivalent LEA instructions
113 /// if needed and when possible.
114 bool runOnMachineFunction(MachineFunction &MF) override;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000115
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000116 // This pass runs after regalloc and doesn't support VReg operands.
117 MachineFunctionProperties getRequiredProperties() const override {
118 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000119 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000120 }
121
Eric Christopher31b81ce2014-06-03 21:01:35 +0000122private:
Simon Pilgrima3a9d8122018-04-13 15:09:39 +0000123 TargetSchedModel TSM;
Eric Christopher31b81ce2014-06-03 21:01:35 +0000124 MachineFunction *MF;
Eric Christopher31b81ce2014-06-03 21:01:35 +0000125 const X86InstrInfo *TII; // Machine instruction info.
Michael Kuperstein12982a82015-11-11 11:44:31 +0000126 bool OptIncDec;
127 bool OptLEA;
Eric Christopher31b81ce2014-06-03 21:01:35 +0000128};
Reid Kleckner0ad69fc2017-05-16 19:55:03 +0000129}
Lama Saba52e89252017-05-16 16:01:36 +0000130
Lama Saba2ea271b2017-05-18 08:11:50 +0000131char FixupLEAPass::ID = 0;
132
133INITIALIZE_PASS(FixupLEAPass, FIXUPLEA_NAME, FIXUPLEA_DESC, false, false)
134
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000135MachineInstr *
136FixupLEAPass::postRAConvertToLEA(MachineFunction::iterator &MFI,
Preston Gurd128920d2013-04-25 21:31:33 +0000137 MachineBasicBlock::iterator &MBBI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000138 MachineInstr &MI = *MBBI;
139 switch (MI.getOpcode()) {
Alexey Volkov6226de62014-05-20 08:55:50 +0000140 case X86::MOV32rr:
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000141 case X86::MOV64rr: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000142 const MachineOperand &Src = MI.getOperand(1);
143 const MachineOperand &Dest = MI.getOperand(0);
144 MachineInstr *NewMI =
145 BuildMI(*MF, MI.getDebugLoc(),
146 TII->get(MI.getOpcode() == X86::MOV32rr ? X86::LEA32r
147 : X86::LEA64r))
Diana Picus116bbab2017-01-13 09:58:52 +0000148 .add(Dest)
149 .add(Src)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000150 .addImm(1)
151 .addReg(0)
152 .addImm(0)
153 .addReg(0);
Eric Christopher31b81ce2014-06-03 21:01:35 +0000154 MFI->insert(MBBI, NewMI); // Insert the new inst
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000155 return NewMI;
156 }
157 case X86::ADD64ri32:
158 case X86::ADD64ri8:
159 case X86::ADD64ri32_DB:
160 case X86::ADD64ri8_DB:
161 case X86::ADD32ri:
162 case X86::ADD32ri8:
163 case X86::ADD32ri_DB:
164 case X86::ADD32ri8_DB:
165 case X86::ADD16ri:
166 case X86::ADD16ri8:
167 case X86::ADD16ri_DB:
168 case X86::ADD16ri8_DB:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000169 if (!MI.getOperand(2).isImm()) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000170 // convertToThreeAddress will call getImm()
171 // which requires isImm() to be true
Craig Topper062a2ba2014-04-25 05:30:21 +0000172 return nullptr;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000173 }
Preston Gurdf03a6e72013-09-30 23:51:22 +0000174 break;
Preston Gurdf0b62882013-09-30 23:18:42 +0000175 case X86::ADD16rr:
176 case X86::ADD16rr_DB:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000177 if (MI.getOperand(1).getReg() != MI.getOperand(2).getReg()) {
Preston Gurdf0b62882013-09-30 23:18:42 +0000178 // if src1 != src2, then convertToThreeAddress will
179 // need to create a Virtual register, which we cannot do
180 // after register allocation.
Craig Topper062a2ba2014-04-25 05:30:21 +0000181 return nullptr;
Preston Gurdf0b62882013-09-30 23:18:42 +0000182 }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000183 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000184 return TII->convertToThreeAddress(MFI, MI, nullptr);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000185}
186
Eric Christopher31b81ce2014-06-03 21:01:35 +0000187FunctionPass *llvm::createX86FixupLEAs() { return new FixupLEAPass(); }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000188
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000189bool FixupLEAPass::runOnMachineFunction(MachineFunction &Func) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000190 if (skipFunction(Func.getFunction()))
Andrew Kaylor2bee5ef2016-04-26 21:44:24 +0000191 return false;
192
Eric Christopherdd240fd2014-06-03 21:01:39 +0000193 MF = &Func;
Eric Christopher4369c9b2015-02-20 08:01:52 +0000194 const X86Subtarget &ST = Func.getSubtarget<X86Subtarget>();
Matthias Braunf1caa282017-12-15 22:22:58 +0000195 OptIncDec = !ST.slowIncDec() || Func.getFunction().optForMinSize();
Lama Saba2ea271b2017-05-18 08:11:50 +0000196 OptLEA = ST.LEAusesAG() || ST.slowLEA() || ST.slow3OpsLEA();
Michael Kuperstein12982a82015-11-11 11:44:31 +0000197
198 if (!OptLEA && !OptIncDec)
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000199 return false;
200
Simon Pilgrima3a9d8122018-04-13 15:09:39 +0000201 TSM.init(&Func.getSubtarget());
Eric Christopherd361ff82015-02-05 19:27:01 +0000202 TII = ST.getInstrInfo();
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000203
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000204 LLVM_DEBUG(dbgs() << "Start X86FixupLEAs\n";);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000205 // Process all basic blocks.
206 for (MachineFunction::iterator I = Func.begin(), E = Func.end(); I != E; ++I)
207 processBasicBlock(Func, I);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000208 LLVM_DEBUG(dbgs() << "End X86FixupLEAs\n";);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000209
210 return true;
211}
212
Eric Christopher31b81ce2014-06-03 21:01:35 +0000213FixupLEAPass::RegUsageState
214FixupLEAPass::usesRegister(MachineOperand &p, MachineBasicBlock::iterator I) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000215 RegUsageState RegUsage = RU_NotUsed;
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000216 MachineInstr &MI = *I;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000217
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000218 for (unsigned int i = 0; i < MI.getNumOperands(); ++i) {
219 MachineOperand &opnd = MI.getOperand(i);
Eric Christopher31b81ce2014-06-03 21:01:35 +0000220 if (opnd.isReg() && opnd.getReg() == p.getReg()) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000221 if (opnd.isDef())
222 return RU_Write;
223 RegUsage = RU_Read;
224 }
225 }
226 return RegUsage;
227}
228
229/// getPreviousInstr - Given a reference to an instruction in a basic
230/// block, return a reference to the previous instruction in the block,
231/// wrapping around to the last instruction of the block if the block
232/// branches to itself.
Eric Christopher31b81ce2014-06-03 21:01:35 +0000233static inline bool getPreviousInstr(MachineBasicBlock::iterator &I,
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000234 MachineFunction::iterator MFI) {
235 if (I == MFI->begin()) {
Duncan P. N. Exon Smithd77de642015-10-19 21:48:29 +0000236 if (MFI->isPredecessor(&*MFI)) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000237 I = --MFI->end();
238 return true;
Eric Christopher31b81ce2014-06-03 21:01:35 +0000239 } else
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000240 return false;
241 }
242 --I;
243 return true;
244}
245
Eric Christopher31b81ce2014-06-03 21:01:35 +0000246MachineBasicBlock::iterator
247FixupLEAPass::searchBackwards(MachineOperand &p, MachineBasicBlock::iterator &I,
248 MachineFunction::iterator MFI) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000249 int InstrDistance = 1;
250 MachineBasicBlock::iterator CurInst;
251 static const int INSTR_DISTANCE_THRESHOLD = 5;
252
253 CurInst = I;
254 bool Found;
255 Found = getPreviousInstr(CurInst, MFI);
Eric Christopher31b81ce2014-06-03 21:01:35 +0000256 while (Found && I != CurInst) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000257 if (CurInst->isCall() || CurInst->isInlineAsm())
258 break;
259 if (InstrDistance > INSTR_DISTANCE_THRESHOLD)
260 break; // too far back to make a difference
Eric Christopher31b81ce2014-06-03 21:01:35 +0000261 if (usesRegister(p, CurInst) == RU_Write) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000262 return CurInst;
263 }
Simon Pilgrima3a9d8122018-04-13 15:09:39 +0000264 InstrDistance += TSM.computeInstrLatency(&*CurInst);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000265 Found = getPreviousInstr(CurInst, MFI);
266 }
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000267 return MachineBasicBlock::iterator();
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000268}
269
Lama Saba2ea271b2017-05-18 08:11:50 +0000270static inline bool isLEA(const int Opcode) {
271 return Opcode == X86::LEA16r || Opcode == X86::LEA32r ||
272 Opcode == X86::LEA64r || Opcode == X86::LEA64_32r;
273}
274
275static inline bool isInefficientLEAReg(unsigned int Reg) {
Craig Topperb2cc9a12018-08-03 03:45:19 +0000276 return Reg == X86::EBP || Reg == X86::RBP ||
277 Reg == X86::R13D || Reg == X86::R13;
Lama Saba2ea271b2017-05-18 08:11:50 +0000278}
279
280static inline bool isRegOperand(const MachineOperand &Op) {
281 return Op.isReg() && Op.getReg() != X86::NoRegister;
282}
283/// hasIneffecientLEARegs - LEA that uses base and index registers
284/// where the base is EBP, RBP, or R13
Andrea Di Biagiob6022aa2018-07-19 16:42:15 +0000285// TODO: use a variant scheduling class to model the latency profile
286// of LEA instructions, and implement this logic as a scheduling predicate.
Lama Saba2ea271b2017-05-18 08:11:50 +0000287static inline bool hasInefficientLEABaseReg(const MachineOperand &Base,
288 const MachineOperand &Index) {
289 return Base.isReg() && isInefficientLEAReg(Base.getReg()) &&
290 isRegOperand(Index);
291}
292
293static inline bool hasLEAOffset(const MachineOperand &Offset) {
294 return (Offset.isImm() && Offset.getImm() != 0) || Offset.isGlobal();
295}
296
Lama Saba2ea271b2017-05-18 08:11:50 +0000297static inline int getADDrrFromLEA(int LEAOpcode) {
298 switch (LEAOpcode) {
299 default:
300 llvm_unreachable("Unexpected LEA instruction");
301 case X86::LEA16r:
302 return X86::ADD16rr;
303 case X86::LEA32r:
304 return X86::ADD32rr;
305 case X86::LEA64_32r:
306 case X86::LEA64r:
307 return X86::ADD64rr;
308 }
309}
310
311static inline int getADDriFromLEA(int LEAOpcode, const MachineOperand &Offset) {
312 bool IsInt8 = Offset.isImm() && isInt<8>(Offset.getImm());
313 switch (LEAOpcode) {
314 default:
315 llvm_unreachable("Unexpected LEA instruction");
316 case X86::LEA16r:
317 return IsInt8 ? X86::ADD16ri8 : X86::ADD16ri;
318 case X86::LEA32r:
319 case X86::LEA64_32r:
320 return IsInt8 ? X86::ADD32ri8 : X86::ADD32ri;
321 case X86::LEA64r:
322 return IsInt8 ? X86::ADD64ri8 : X86::ADD64ri32;
323 }
Michael Kuperstein12982a82015-11-11 11:44:31 +0000324}
325
326/// isLEASimpleIncOrDec - Does this LEA have one these forms:
327/// lea %reg, 1(%reg)
328/// lea %reg, -1(%reg)
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000329static inline bool isLEASimpleIncOrDec(MachineInstr &LEA) {
330 unsigned SrcReg = LEA.getOperand(1 + X86::AddrBaseReg).getReg();
331 unsigned DstReg = LEA.getOperand(0).getReg();
Michael Kuperstein12982a82015-11-11 11:44:31 +0000332 unsigned AddrDispOp = 1 + X86::AddrDisp;
333 return SrcReg == DstReg &&
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000334 LEA.getOperand(1 + X86::AddrIndexReg).getReg() == 0 &&
335 LEA.getOperand(1 + X86::AddrSegmentReg).getReg() == 0 &&
336 LEA.getOperand(AddrDispOp).isImm() &&
337 (LEA.getOperand(AddrDispOp).getImm() == 1 ||
338 LEA.getOperand(AddrDispOp).getImm() == -1);
Michael Kuperstein12982a82015-11-11 11:44:31 +0000339}
340
341bool FixupLEAPass::fixupIncDec(MachineBasicBlock::iterator &I,
342 MachineFunction::iterator MFI) const {
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000343 MachineInstr &MI = *I;
344 int Opcode = MI.getOpcode();
Michael Kuperstein12982a82015-11-11 11:44:31 +0000345 if (!isLEA(Opcode))
346 return false;
347
348 if (isLEASimpleIncOrDec(MI) && TII->isSafeToClobberEFLAGS(*MFI, I)) {
349 int NewOpcode;
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000350 bool isINC = MI.getOperand(4).getImm() == 1;
Michael Kuperstein12982a82015-11-11 11:44:31 +0000351 switch (Opcode) {
352 case X86::LEA16r:
353 NewOpcode = isINC ? X86::INC16r : X86::DEC16r;
354 break;
355 case X86::LEA32r:
356 case X86::LEA64_32r:
357 NewOpcode = isINC ? X86::INC32r : X86::DEC32r;
358 break;
359 case X86::LEA64r:
360 NewOpcode = isINC ? X86::INC64r : X86::DEC64r;
361 break;
362 }
363
364 MachineInstr *NewMI =
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000365 BuildMI(*MFI, I, MI.getDebugLoc(), TII->get(NewOpcode))
Diana Picus116bbab2017-01-13 09:58:52 +0000366 .add(MI.getOperand(0))
367 .add(MI.getOperand(1));
Michael Kuperstein12982a82015-11-11 11:44:31 +0000368 MFI->erase(I);
369 I = static_cast<MachineBasicBlock::iterator>(NewMI);
370 return true;
371 }
372 return false;
373}
374
Eric Christopher31b81ce2014-06-03 21:01:35 +0000375void FixupLEAPass::processInstruction(MachineBasicBlock::iterator &I,
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000376 MachineFunction::iterator MFI) {
377 // Process a load, store, or LEA instruction.
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000378 MachineInstr &MI = *I;
379 const MCInstrDesc &Desc = MI.getDesc();
Craig Topper477649a2016-04-28 05:58:46 +0000380 int AddrOffset = X86II::getMemoryOperandNo(Desc.TSFlags);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000381 if (AddrOffset >= 0) {
382 AddrOffset += X86II::getOperandBias(Desc);
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000383 MachineOperand &p = MI.getOperand(AddrOffset + X86::AddrBaseReg);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000384 if (p.isReg() && p.getReg() != X86::ESP) {
385 seekLEAFixup(p, I, MFI);
386 }
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000387 MachineOperand &q = MI.getOperand(AddrOffset + X86::AddrIndexReg);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000388 if (q.isReg() && q.getReg() != X86::ESP) {
389 seekLEAFixup(q, I, MFI);
390 }
391 }
392}
393
Eric Christopher31b81ce2014-06-03 21:01:35 +0000394void FixupLEAPass::seekLEAFixup(MachineOperand &p,
395 MachineBasicBlock::iterator &I,
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000396 MachineFunction::iterator MFI) {
397 MachineBasicBlock::iterator MBI = searchBackwards(p, I, MFI);
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000398 if (MBI != MachineBasicBlock::iterator()) {
Eric Christopher31b81ce2014-06-03 21:01:35 +0000399 MachineInstr *NewMI = postRAConvertToLEA(MFI, MBI);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000400 if (NewMI) {
401 ++NumLEAs;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000402 LLVM_DEBUG(dbgs() << "FixLEA: Candidate to replace:"; MBI->dump(););
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000403 // now to replace with an equivalent LEA...
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000404 LLVM_DEBUG(dbgs() << "FixLEA: Replaced by: "; NewMI->dump(););
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000405 MFI->erase(MBI);
406 MachineBasicBlock::iterator J =
Eric Christopher31b81ce2014-06-03 21:01:35 +0000407 static_cast<MachineBasicBlock::iterator>(NewMI);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000408 processInstruction(J, MFI);
409 }
410 }
411}
412
Simon Pilgrimd5d72242018-11-01 14:57:07 +0000413void FixupLEAPass::processInstructionForSlowLEA(MachineBasicBlock::iterator &I,
414 MachineFunction::iterator MFI) {
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000415 MachineInstr &MI = *I;
Lama Saba2ea271b2017-05-18 08:11:50 +0000416 const int Opcode = MI.getOpcode();
417 if (!isLEA(Opcode))
Alexey Volkov6226de62014-05-20 08:55:50 +0000418 return;
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000419 if (MI.getOperand(5).getReg() != 0 || !MI.getOperand(4).isImm() ||
Alexey Volkov6226de62014-05-20 08:55:50 +0000420 !TII->isSafeToClobberEFLAGS(*MFI, I))
421 return;
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000422 const unsigned DstR = MI.getOperand(0).getReg();
423 const unsigned SrcR1 = MI.getOperand(1).getReg();
424 const unsigned SrcR2 = MI.getOperand(3).getReg();
Alexey Volkov6226de62014-05-20 08:55:50 +0000425 if ((SrcR1 == 0 || SrcR1 != DstR) && (SrcR2 == 0 || SrcR2 != DstR))
426 return;
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000427 if (MI.getOperand(2).getImm() > 1)
Alexey Volkov6226de62014-05-20 08:55:50 +0000428 return;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000429 LLVM_DEBUG(dbgs() << "FixLEA: Candidate to replace:"; I->dump(););
430 LLVM_DEBUG(dbgs() << "FixLEA: Replaced by: ";);
Craig Topper66f09ad2014-06-08 22:29:17 +0000431 MachineInstr *NewMI = nullptr;
Alexey Volkov6226de62014-05-20 08:55:50 +0000432 // Make ADD instruction for two registers writing to LEA's destination
433 if (SrcR1 != 0 && SrcR2 != 0) {
Lama Saba2ea271b2017-05-18 08:11:50 +0000434 const MCInstrDesc &ADDrr = TII->get(getADDrrFromLEA(Opcode));
435 const MachineOperand &Src = MI.getOperand(SrcR1 == DstR ? 3 : 1);
436 NewMI =
437 BuildMI(*MFI, I, MI.getDebugLoc(), ADDrr, DstR).addReg(DstR).add(Src);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000438 LLVM_DEBUG(NewMI->dump(););
Alexey Volkov6226de62014-05-20 08:55:50 +0000439 }
440 // Make ADD instruction for immediate
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000441 if (MI.getOperand(4).getImm() != 0) {
Lama Saba2ea271b2017-05-18 08:11:50 +0000442 const MCInstrDesc &ADDri =
443 TII->get(getADDriFromLEA(Opcode, MI.getOperand(4)));
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000444 const MachineOperand &SrcR = MI.getOperand(SrcR1 == DstR ? 1 : 3);
Lama Saba2ea271b2017-05-18 08:11:50 +0000445 NewMI = BuildMI(*MFI, I, MI.getDebugLoc(), ADDri, DstR)
Diana Picus116bbab2017-01-13 09:58:52 +0000446 .add(SrcR)
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000447 .addImm(MI.getOperand(4).getImm());
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000448 LLVM_DEBUG(NewMI->dump(););
Alexey Volkov6226de62014-05-20 08:55:50 +0000449 }
450 if (NewMI) {
451 MFI->erase(I);
Lama Saba2ea271b2017-05-18 08:11:50 +0000452 I = NewMI;
Alexey Volkov6226de62014-05-20 08:55:50 +0000453 }
454}
455
Lama Saba2ea271b2017-05-18 08:11:50 +0000456MachineInstr *
457FixupLEAPass::processInstrForSlow3OpLEA(MachineInstr &MI,
458 MachineFunction::iterator MFI) {
459
460 const int LEAOpcode = MI.getOpcode();
461 if (!isLEA(LEAOpcode))
462 return nullptr;
463
464 const MachineOperand &Dst = MI.getOperand(0);
465 const MachineOperand &Base = MI.getOperand(1);
466 const MachineOperand &Scale = MI.getOperand(2);
467 const MachineOperand &Index = MI.getOperand(3);
468 const MachineOperand &Offset = MI.getOperand(4);
469 const MachineOperand &Segment = MI.getOperand(5);
470
Andrea Di Biagiob6022aa2018-07-19 16:42:15 +0000471 if (!(TII->isThreeOperandsLEA(MI) ||
Lama Saba2ea271b2017-05-18 08:11:50 +0000472 hasInefficientLEABaseReg(Base, Index)) ||
473 !TII->isSafeToClobberEFLAGS(*MFI, MI) ||
474 Segment.getReg() != X86::NoRegister)
475 return nullptr;
476
477 unsigned int DstR = Dst.getReg();
478 unsigned int BaseR = Base.getReg();
479 unsigned int IndexR = Index.getReg();
480 unsigned SSDstR =
481 (LEAOpcode == X86::LEA64_32r) ? getX86SubSuperRegister(DstR, 64) : DstR;
482 bool IsScale1 = Scale.getImm() == 1;
483 bool IsInefficientBase = isInefficientLEAReg(BaseR);
484 bool IsInefficientIndex = isInefficientLEAReg(IndexR);
485
486 // Skip these cases since it takes more than 2 instructions
487 // to replace the LEA instruction.
488 if (IsInefficientBase && SSDstR == BaseR && !IsScale1)
489 return nullptr;
490 if (LEAOpcode == X86::LEA64_32r && IsInefficientBase &&
491 (IsInefficientIndex || !IsScale1))
492 return nullptr;
493
494 const DebugLoc DL = MI.getDebugLoc();
495 const MCInstrDesc &ADDrr = TII->get(getADDrrFromLEA(LEAOpcode));
496 const MCInstrDesc &ADDri = TII->get(getADDriFromLEA(LEAOpcode, Offset));
497
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000498 LLVM_DEBUG(dbgs() << "FixLEA: Candidate to replace:"; MI.dump(););
499 LLVM_DEBUG(dbgs() << "FixLEA: Replaced by: ";);
Lama Saba2ea271b2017-05-18 08:11:50 +0000500
501 // First try to replace LEA with one or two (for the 3-op LEA case)
502 // add instructions:
503 // 1.lea (%base,%index,1), %base => add %index,%base
504 // 2.lea (%base,%index,1), %index => add %base,%index
505 if (IsScale1 && (DstR == BaseR || DstR == IndexR)) {
506 const MachineOperand &Src = DstR == BaseR ? Index : Base;
507 MachineInstr *NewMI =
508 BuildMI(*MFI, MI, DL, ADDrr, DstR).addReg(DstR).add(Src);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000509 LLVM_DEBUG(NewMI->dump(););
Lama Saba2ea271b2017-05-18 08:11:50 +0000510 // Create ADD instruction for the Offset in case of 3-Ops LEA.
511 if (hasLEAOffset(Offset)) {
512 NewMI = BuildMI(*MFI, MI, DL, ADDri, DstR).addReg(DstR).add(Offset);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000513 LLVM_DEBUG(NewMI->dump(););
Lama Saba2ea271b2017-05-18 08:11:50 +0000514 }
515 return NewMI;
516 }
517 // If the base is inefficient try switching the index and base operands,
518 // otherwise just break the 3-Ops LEA inst into 2-Ops LEA + ADD instruction:
519 // lea offset(%base,%index,scale),%dst =>
520 // lea (%base,%index,scale); add offset,%dst
521 if (!IsInefficientBase || (!IsInefficientIndex && IsScale1)) {
522 MachineInstr *NewMI = BuildMI(*MFI, MI, DL, TII->get(LEAOpcode))
523 .add(Dst)
524 .add(IsInefficientBase ? Index : Base)
525 .add(Scale)
526 .add(IsInefficientBase ? Base : Index)
527 .addImm(0)
528 .add(Segment);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000529 LLVM_DEBUG(NewMI->dump(););
Lama Saba2ea271b2017-05-18 08:11:50 +0000530 // Create ADD instruction for the Offset in case of 3-Ops LEA.
531 if (hasLEAOffset(Offset)) {
532 NewMI = BuildMI(*MFI, MI, DL, ADDri, DstR).addReg(DstR).add(Offset);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000533 LLVM_DEBUG(NewMI->dump(););
Lama Saba2ea271b2017-05-18 08:11:50 +0000534 }
535 return NewMI;
536 }
537 // Handle the rest of the cases with inefficient base register:
538 assert(SSDstR != BaseR && "SSDstR == BaseR should be handled already!");
539 assert(IsInefficientBase && "efficient base should be handled already!");
540
541 // lea (%base,%index,1), %dst => mov %base,%dst; add %index,%dst
542 if (IsScale1 && !hasLEAOffset(Offset)) {
Serguei Katkov1ce71372018-01-26 04:49:26 +0000543 bool BIK = Base.isKill() && BaseR != IndexR;
544 TII->copyPhysReg(*MFI, MI, DL, DstR, BaseR, BIK);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000545 LLVM_DEBUG(MI.getPrevNode()->dump(););
Lama Saba2ea271b2017-05-18 08:11:50 +0000546
547 MachineInstr *NewMI =
548 BuildMI(*MFI, MI, DL, ADDrr, DstR).addReg(DstR).add(Index);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000549 LLVM_DEBUG(NewMI->dump(););
Lama Saba2ea271b2017-05-18 08:11:50 +0000550 return NewMI;
551 }
552 // lea offset(%base,%index,scale), %dst =>
553 // lea offset( ,%index,scale), %dst; add %base,%dst
554 MachineInstr *NewMI = BuildMI(*MFI, MI, DL, TII->get(LEAOpcode))
555 .add(Dst)
556 .addReg(0)
557 .add(Scale)
558 .add(Index)
559 .add(Offset)
560 .add(Segment);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000561 LLVM_DEBUG(NewMI->dump(););
Lama Saba2ea271b2017-05-18 08:11:50 +0000562
563 NewMI = BuildMI(*MFI, MI, DL, ADDrr, DstR).addReg(DstR).add(Base);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000564 LLVM_DEBUG(NewMI->dump(););
Lama Saba2ea271b2017-05-18 08:11:50 +0000565 return NewMI;
566}
567
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000568bool FixupLEAPass::processBasicBlock(MachineFunction &MF,
569 MachineFunction::iterator MFI) {
570
Alexey Volkov6226de62014-05-20 08:55:50 +0000571 for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I) {
Michael Kuperstein12982a82015-11-11 11:44:31 +0000572 if (OptIncDec)
573 if (fixupIncDec(I, MFI))
574 continue;
575
576 if (OptLEA) {
Craig Topper9164b9b2018-07-31 00:43:54 +0000577 if (MF.getSubtarget<X86Subtarget>().slowLEA())
Simon Pilgrimd5d72242018-11-01 14:57:07 +0000578 processInstructionForSlowLEA(I, MFI);
Lama Saba2ea271b2017-05-18 08:11:50 +0000579
580 else {
581 if (MF.getSubtarget<X86Subtarget>().slow3OpsLEA()) {
582 if (auto *NewMI = processInstrForSlow3OpLEA(*I, MFI)) {
583 MFI->erase(I);
584 I = NewMI;
585 }
586 } else
587 processInstruction(I, MFI);
588 }
Michael Kuperstein12982a82015-11-11 11:44:31 +0000589 }
Alexey Volkov6226de62014-05-20 08:55:50 +0000590 }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000591 return false;
592}