blob: 33a8baac594b59172d2a4d48149ec5bd1b718f85 [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 +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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000043 /// Loop over all of the instructions in the basic block
Eric Christopher31b81ce2014-06-03 21:01:35 +000044 /// 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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000049 /// Given a machine register, look for the instruction
Eric Christopher31b81ce2014-06-03 21:01:35 +000050 /// 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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000057 /// Given a memory access or LEA instruction
Eric Christopher31b81ce2014-06-03 21:01:35 +000058 /// 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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000064 /// Given a LEA instruction which is unprofitable
Eric Christopher31b81ce2014-06-03 21:01:35 +000065 /// 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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000070 /// Given a LEA instruction which is unprofitable
Lama Saba2ea271b2017-05-18 08:11:50 +000071 /// 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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000085 /// Look for LEAs that add 1 to reg or subtract 1 from reg
Michael Kuperstein12982a82015-11-11 11:44:31 +000086 /// and convert them to INC or DEC respectively.
87 bool fixupIncDec(MachineBasicBlock::iterator &I,
88 MachineFunction::iterator MFI) const;
89
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000090 /// Determine if an instruction references a machine register
Eric Christopher31b81ce2014-06-03 21:01:35 +000091 /// 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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000094 /// Step backwards through a basic block, looking
Eric Christopher31b81ce2014-06-03 21:01:35 +000095 /// 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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000101 /// if an instruction can be converted to an
Eric Christopher31b81ce2014-06-03 21:01:35 +0000102 /// 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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000116 /// Loop over all of the basic blocks,
Eric Christopher31b81ce2014-06-03 21:01:35 +0000117 /// 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:
Simon Pilgrima3a9d8122018-04-13 15:09:39 +0000128 TargetSchedModel TSM;
Eric Christopher31b81ce2014-06-03 21:01:35 +0000129 MachineFunction *MF;
Eric Christopher31b81ce2014-06-03 21:01:35 +0000130 const X86InstrInfo *TII; // Machine instruction info.
Michael Kuperstein12982a82015-11-11 11:44:31 +0000131 bool OptIncDec;
132 bool OptLEA;
Eric Christopher31b81ce2014-06-03 21:01:35 +0000133};
Reid Kleckner0ad69fc2017-05-16 19:55:03 +0000134}
Lama Saba52e89252017-05-16 16:01:36 +0000135
Lama Saba2ea271b2017-05-18 08:11:50 +0000136char FixupLEAPass::ID = 0;
137
138INITIALIZE_PASS(FixupLEAPass, FIXUPLEA_NAME, FIXUPLEA_DESC, false, false)
139
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000140MachineInstr *
141FixupLEAPass::postRAConvertToLEA(MachineFunction::iterator &MFI,
Preston Gurd128920d2013-04-25 21:31:33 +0000142 MachineBasicBlock::iterator &MBBI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000143 MachineInstr &MI = *MBBI;
144 switch (MI.getOpcode()) {
Alexey Volkov6226de62014-05-20 08:55:50 +0000145 case X86::MOV32rr:
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000146 case X86::MOV64rr: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000147 const MachineOperand &Src = MI.getOperand(1);
148 const MachineOperand &Dest = MI.getOperand(0);
149 MachineInstr *NewMI =
150 BuildMI(*MF, MI.getDebugLoc(),
151 TII->get(MI.getOpcode() == X86::MOV32rr ? X86::LEA32r
152 : X86::LEA64r))
Diana Picus116bbab2017-01-13 09:58:52 +0000153 .add(Dest)
154 .add(Src)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000155 .addImm(1)
156 .addReg(0)
157 .addImm(0)
158 .addReg(0);
Eric Christopher31b81ce2014-06-03 21:01:35 +0000159 MFI->insert(MBBI, NewMI); // Insert the new inst
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000160 return NewMI;
161 }
162 case X86::ADD64ri32:
163 case X86::ADD64ri8:
164 case X86::ADD64ri32_DB:
165 case X86::ADD64ri8_DB:
166 case X86::ADD32ri:
167 case X86::ADD32ri8:
168 case X86::ADD32ri_DB:
169 case X86::ADD32ri8_DB:
170 case X86::ADD16ri:
171 case X86::ADD16ri8:
172 case X86::ADD16ri_DB:
173 case X86::ADD16ri8_DB:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000174 if (!MI.getOperand(2).isImm()) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000175 // convertToThreeAddress will call getImm()
176 // which requires isImm() to be true
Craig Topper062a2ba2014-04-25 05:30:21 +0000177 return nullptr;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000178 }
Preston Gurdf03a6e72013-09-30 23:51:22 +0000179 break;
Preston Gurdf0b62882013-09-30 23:18:42 +0000180 case X86::ADD16rr:
181 case X86::ADD16rr_DB:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000182 if (MI.getOperand(1).getReg() != MI.getOperand(2).getReg()) {
Preston Gurdf0b62882013-09-30 23:18:42 +0000183 // if src1 != src2, then convertToThreeAddress will
184 // need to create a Virtual register, which we cannot do
185 // after register allocation.
Craig Topper062a2ba2014-04-25 05:30:21 +0000186 return nullptr;
Preston Gurdf0b62882013-09-30 23:18:42 +0000187 }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000188 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000189 return TII->convertToThreeAddress(MFI, MI, nullptr);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000190}
191
Eric Christopher31b81ce2014-06-03 21:01:35 +0000192FunctionPass *llvm::createX86FixupLEAs() { return new FixupLEAPass(); }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000193
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000194bool FixupLEAPass::runOnMachineFunction(MachineFunction &Func) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000195 if (skipFunction(Func.getFunction()))
Andrew Kaylor2bee5ef2016-04-26 21:44:24 +0000196 return false;
197
Eric Christopherdd240fd2014-06-03 21:01:39 +0000198 MF = &Func;
Eric Christopher4369c9b2015-02-20 08:01:52 +0000199 const X86Subtarget &ST = Func.getSubtarget<X86Subtarget>();
Matthias Braunf1caa282017-12-15 22:22:58 +0000200 OptIncDec = !ST.slowIncDec() || Func.getFunction().optForMinSize();
Lama Saba2ea271b2017-05-18 08:11:50 +0000201 OptLEA = ST.LEAusesAG() || ST.slowLEA() || ST.slow3OpsLEA();
Michael Kuperstein12982a82015-11-11 11:44:31 +0000202
203 if (!OptLEA && !OptIncDec)
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000204 return false;
205
Simon Pilgrima3a9d8122018-04-13 15:09:39 +0000206 TSM.init(&Func.getSubtarget());
Eric Christopherd361ff82015-02-05 19:27:01 +0000207 TII = ST.getInstrInfo();
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000208
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000209 LLVM_DEBUG(dbgs() << "Start X86FixupLEAs\n";);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000210 // Process all basic blocks.
211 for (MachineFunction::iterator I = Func.begin(), E = Func.end(); I != E; ++I)
212 processBasicBlock(Func, I);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000213 LLVM_DEBUG(dbgs() << "End X86FixupLEAs\n";);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000214
215 return true;
216}
217
Eric Christopher31b81ce2014-06-03 21:01:35 +0000218FixupLEAPass::RegUsageState
219FixupLEAPass::usesRegister(MachineOperand &p, MachineBasicBlock::iterator I) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000220 RegUsageState RegUsage = RU_NotUsed;
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000221 MachineInstr &MI = *I;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000222
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000223 for (unsigned int i = 0; i < MI.getNumOperands(); ++i) {
224 MachineOperand &opnd = MI.getOperand(i);
Eric Christopher31b81ce2014-06-03 21:01:35 +0000225 if (opnd.isReg() && opnd.getReg() == p.getReg()) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000226 if (opnd.isDef())
227 return RU_Write;
228 RegUsage = RU_Read;
229 }
230 }
231 return RegUsage;
232}
233
234/// getPreviousInstr - Given a reference to an instruction in a basic
235/// block, return a reference to the previous instruction in the block,
236/// wrapping around to the last instruction of the block if the block
237/// branches to itself.
Eric Christopher31b81ce2014-06-03 21:01:35 +0000238static inline bool getPreviousInstr(MachineBasicBlock::iterator &I,
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000239 MachineFunction::iterator MFI) {
240 if (I == MFI->begin()) {
Duncan P. N. Exon Smithd77de642015-10-19 21:48:29 +0000241 if (MFI->isPredecessor(&*MFI)) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000242 I = --MFI->end();
243 return true;
Eric Christopher31b81ce2014-06-03 21:01:35 +0000244 } else
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000245 return false;
246 }
247 --I;
248 return true;
249}
250
Eric Christopher31b81ce2014-06-03 21:01:35 +0000251MachineBasicBlock::iterator
252FixupLEAPass::searchBackwards(MachineOperand &p, MachineBasicBlock::iterator &I,
253 MachineFunction::iterator MFI) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000254 int InstrDistance = 1;
255 MachineBasicBlock::iterator CurInst;
256 static const int INSTR_DISTANCE_THRESHOLD = 5;
257
258 CurInst = I;
259 bool Found;
260 Found = getPreviousInstr(CurInst, MFI);
Eric Christopher31b81ce2014-06-03 21:01:35 +0000261 while (Found && I != CurInst) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000262 if (CurInst->isCall() || CurInst->isInlineAsm())
263 break;
264 if (InstrDistance > INSTR_DISTANCE_THRESHOLD)
265 break; // too far back to make a difference
Eric Christopher31b81ce2014-06-03 21:01:35 +0000266 if (usesRegister(p, CurInst) == RU_Write) {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000267 return CurInst;
268 }
Simon Pilgrima3a9d8122018-04-13 15:09:39 +0000269 InstrDistance += TSM.computeInstrLatency(&*CurInst);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000270 Found = getPreviousInstr(CurInst, MFI);
271 }
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000272 return MachineBasicBlock::iterator();
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000273}
274
Lama Saba2ea271b2017-05-18 08:11:50 +0000275static inline bool isLEA(const int Opcode) {
276 return Opcode == X86::LEA16r || Opcode == X86::LEA32r ||
277 Opcode == X86::LEA64r || Opcode == X86::LEA64_32r;
278}
279
280static inline bool isInefficientLEAReg(unsigned int Reg) {
Craig Topperb2cc9a12018-08-03 03:45:19 +0000281 return Reg == X86::EBP || Reg == X86::RBP ||
282 Reg == X86::R13D || Reg == X86::R13;
Lama Saba2ea271b2017-05-18 08:11:50 +0000283}
284
285static inline bool isRegOperand(const MachineOperand &Op) {
286 return Op.isReg() && Op.getReg() != X86::NoRegister;
287}
288/// hasIneffecientLEARegs - LEA that uses base and index registers
289/// where the base is EBP, RBP, or R13
Andrea Di Biagiob6022aa2018-07-19 16:42:15 +0000290// TODO: use a variant scheduling class to model the latency profile
291// of LEA instructions, and implement this logic as a scheduling predicate.
Lama Saba2ea271b2017-05-18 08:11:50 +0000292static inline bool hasInefficientLEABaseReg(const MachineOperand &Base,
293 const MachineOperand &Index) {
294 return Base.isReg() && isInefficientLEAReg(Base.getReg()) &&
295 isRegOperand(Index);
296}
297
298static inline bool hasLEAOffset(const MachineOperand &Offset) {
299 return (Offset.isImm() && Offset.getImm() != 0) || Offset.isGlobal();
300}
301
Lama Saba2ea271b2017-05-18 08:11:50 +0000302static inline int getADDrrFromLEA(int LEAOpcode) {
303 switch (LEAOpcode) {
304 default:
305 llvm_unreachable("Unexpected LEA instruction");
306 case X86::LEA16r:
307 return X86::ADD16rr;
308 case X86::LEA32r:
309 return X86::ADD32rr;
310 case X86::LEA64_32r:
311 case X86::LEA64r:
312 return X86::ADD64rr;
313 }
314}
315
316static inline int getADDriFromLEA(int LEAOpcode, const MachineOperand &Offset) {
317 bool IsInt8 = Offset.isImm() && isInt<8>(Offset.getImm());
318 switch (LEAOpcode) {
319 default:
320 llvm_unreachable("Unexpected LEA instruction");
321 case X86::LEA16r:
322 return IsInt8 ? X86::ADD16ri8 : X86::ADD16ri;
323 case X86::LEA32r:
324 case X86::LEA64_32r:
325 return IsInt8 ? X86::ADD32ri8 : X86::ADD32ri;
326 case X86::LEA64r:
327 return IsInt8 ? X86::ADD64ri8 : X86::ADD64ri32;
328 }
Michael Kuperstein12982a82015-11-11 11:44:31 +0000329}
330
331/// isLEASimpleIncOrDec - Does this LEA have one these forms:
332/// lea %reg, 1(%reg)
333/// lea %reg, -1(%reg)
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000334static inline bool isLEASimpleIncOrDec(MachineInstr &LEA) {
335 unsigned SrcReg = LEA.getOperand(1 + X86::AddrBaseReg).getReg();
336 unsigned DstReg = LEA.getOperand(0).getReg();
Michael Kuperstein12982a82015-11-11 11:44:31 +0000337 unsigned AddrDispOp = 1 + X86::AddrDisp;
338 return SrcReg == DstReg &&
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000339 LEA.getOperand(1 + X86::AddrIndexReg).getReg() == 0 &&
340 LEA.getOperand(1 + X86::AddrSegmentReg).getReg() == 0 &&
341 LEA.getOperand(AddrDispOp).isImm() &&
342 (LEA.getOperand(AddrDispOp).getImm() == 1 ||
343 LEA.getOperand(AddrDispOp).getImm() == -1);
Michael Kuperstein12982a82015-11-11 11:44:31 +0000344}
345
346bool FixupLEAPass::fixupIncDec(MachineBasicBlock::iterator &I,
347 MachineFunction::iterator MFI) const {
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000348 MachineInstr &MI = *I;
349 int Opcode = MI.getOpcode();
Michael Kuperstein12982a82015-11-11 11:44:31 +0000350 if (!isLEA(Opcode))
351 return false;
352
353 if (isLEASimpleIncOrDec(MI) && TII->isSafeToClobberEFLAGS(*MFI, I)) {
354 int NewOpcode;
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000355 bool isINC = MI.getOperand(4).getImm() == 1;
Michael Kuperstein12982a82015-11-11 11:44:31 +0000356 switch (Opcode) {
357 case X86::LEA16r:
358 NewOpcode = isINC ? X86::INC16r : X86::DEC16r;
359 break;
360 case X86::LEA32r:
361 case X86::LEA64_32r:
362 NewOpcode = isINC ? X86::INC32r : X86::DEC32r;
363 break;
364 case X86::LEA64r:
365 NewOpcode = isINC ? X86::INC64r : X86::DEC64r;
366 break;
367 }
368
369 MachineInstr *NewMI =
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000370 BuildMI(*MFI, I, MI.getDebugLoc(), TII->get(NewOpcode))
Diana Picus116bbab2017-01-13 09:58:52 +0000371 .add(MI.getOperand(0))
372 .add(MI.getOperand(1));
Michael Kuperstein12982a82015-11-11 11:44:31 +0000373 MFI->erase(I);
374 I = static_cast<MachineBasicBlock::iterator>(NewMI);
375 return true;
376 }
377 return false;
378}
379
Eric Christopher31b81ce2014-06-03 21:01:35 +0000380void FixupLEAPass::processInstruction(MachineBasicBlock::iterator &I,
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000381 MachineFunction::iterator MFI) {
382 // Process a load, store, or LEA instruction.
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000383 MachineInstr &MI = *I;
384 const MCInstrDesc &Desc = MI.getDesc();
Craig Topper477649a2016-04-28 05:58:46 +0000385 int AddrOffset = X86II::getMemoryOperandNo(Desc.TSFlags);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000386 if (AddrOffset >= 0) {
387 AddrOffset += X86II::getOperandBias(Desc);
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000388 MachineOperand &p = MI.getOperand(AddrOffset + X86::AddrBaseReg);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000389 if (p.isReg() && p.getReg() != X86::ESP) {
390 seekLEAFixup(p, I, MFI);
391 }
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000392 MachineOperand &q = MI.getOperand(AddrOffset + X86::AddrIndexReg);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000393 if (q.isReg() && q.getReg() != X86::ESP) {
394 seekLEAFixup(q, I, MFI);
395 }
396 }
397}
398
Eric Christopher31b81ce2014-06-03 21:01:35 +0000399void FixupLEAPass::seekLEAFixup(MachineOperand &p,
400 MachineBasicBlock::iterator &I,
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000401 MachineFunction::iterator MFI) {
402 MachineBasicBlock::iterator MBI = searchBackwards(p, I, MFI);
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000403 if (MBI != MachineBasicBlock::iterator()) {
Eric Christopher31b81ce2014-06-03 21:01:35 +0000404 MachineInstr *NewMI = postRAConvertToLEA(MFI, MBI);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000405 if (NewMI) {
406 ++NumLEAs;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000407 LLVM_DEBUG(dbgs() << "FixLEA: Candidate to replace:"; MBI->dump(););
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000408 // now to replace with an equivalent LEA...
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000409 LLVM_DEBUG(dbgs() << "FixLEA: Replaced by: "; NewMI->dump(););
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000410 MFI->erase(MBI);
411 MachineBasicBlock::iterator J =
Eric Christopher31b81ce2014-06-03 21:01:35 +0000412 static_cast<MachineBasicBlock::iterator>(NewMI);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000413 processInstruction(J, MFI);
414 }
415 }
416}
417
Alexey Volkov6226de62014-05-20 08:55:50 +0000418void FixupLEAPass::processInstructionForSLM(MachineBasicBlock::iterator &I,
419 MachineFunction::iterator MFI) {
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000420 MachineInstr &MI = *I;
Lama Saba2ea271b2017-05-18 08:11:50 +0000421 const int Opcode = MI.getOpcode();
422 if (!isLEA(Opcode))
Alexey Volkov6226de62014-05-20 08:55:50 +0000423 return;
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000424 if (MI.getOperand(5).getReg() != 0 || !MI.getOperand(4).isImm() ||
Alexey Volkov6226de62014-05-20 08:55:50 +0000425 !TII->isSafeToClobberEFLAGS(*MFI, I))
426 return;
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000427 const unsigned DstR = MI.getOperand(0).getReg();
428 const unsigned SrcR1 = MI.getOperand(1).getReg();
429 const unsigned SrcR2 = MI.getOperand(3).getReg();
Alexey Volkov6226de62014-05-20 08:55:50 +0000430 if ((SrcR1 == 0 || SrcR1 != DstR) && (SrcR2 == 0 || SrcR2 != DstR))
431 return;
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000432 if (MI.getOperand(2).getImm() > 1)
Alexey Volkov6226de62014-05-20 08:55:50 +0000433 return;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000434 LLVM_DEBUG(dbgs() << "FixLEA: Candidate to replace:"; I->dump(););
435 LLVM_DEBUG(dbgs() << "FixLEA: Replaced by: ";);
Craig Topper66f09ad2014-06-08 22:29:17 +0000436 MachineInstr *NewMI = nullptr;
Alexey Volkov6226de62014-05-20 08:55:50 +0000437 // Make ADD instruction for two registers writing to LEA's destination
438 if (SrcR1 != 0 && SrcR2 != 0) {
Lama Saba2ea271b2017-05-18 08:11:50 +0000439 const MCInstrDesc &ADDrr = TII->get(getADDrrFromLEA(Opcode));
440 const MachineOperand &Src = MI.getOperand(SrcR1 == DstR ? 3 : 1);
441 NewMI =
442 BuildMI(*MFI, I, MI.getDebugLoc(), ADDrr, DstR).addReg(DstR).add(Src);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000443 LLVM_DEBUG(NewMI->dump(););
Alexey Volkov6226de62014-05-20 08:55:50 +0000444 }
445 // Make ADD instruction for immediate
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000446 if (MI.getOperand(4).getImm() != 0) {
Lama Saba2ea271b2017-05-18 08:11:50 +0000447 const MCInstrDesc &ADDri =
448 TII->get(getADDriFromLEA(Opcode, MI.getOperand(4)));
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000449 const MachineOperand &SrcR = MI.getOperand(SrcR1 == DstR ? 1 : 3);
Lama Saba2ea271b2017-05-18 08:11:50 +0000450 NewMI = BuildMI(*MFI, I, MI.getDebugLoc(), ADDri, DstR)
Diana Picus116bbab2017-01-13 09:58:52 +0000451 .add(SrcR)
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000452 .addImm(MI.getOperand(4).getImm());
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000453 LLVM_DEBUG(NewMI->dump(););
Alexey Volkov6226de62014-05-20 08:55:50 +0000454 }
455 if (NewMI) {
456 MFI->erase(I);
Lama Saba2ea271b2017-05-18 08:11:50 +0000457 I = NewMI;
Alexey Volkov6226de62014-05-20 08:55:50 +0000458 }
459}
460
Lama Saba2ea271b2017-05-18 08:11:50 +0000461MachineInstr *
462FixupLEAPass::processInstrForSlow3OpLEA(MachineInstr &MI,
463 MachineFunction::iterator MFI) {
464
465 const int LEAOpcode = MI.getOpcode();
466 if (!isLEA(LEAOpcode))
467 return nullptr;
468
469 const MachineOperand &Dst = MI.getOperand(0);
470 const MachineOperand &Base = MI.getOperand(1);
471 const MachineOperand &Scale = MI.getOperand(2);
472 const MachineOperand &Index = MI.getOperand(3);
473 const MachineOperand &Offset = MI.getOperand(4);
474 const MachineOperand &Segment = MI.getOperand(5);
475
Andrea Di Biagiob6022aa2018-07-19 16:42:15 +0000476 if (!(TII->isThreeOperandsLEA(MI) ||
Lama Saba2ea271b2017-05-18 08:11:50 +0000477 hasInefficientLEABaseReg(Base, Index)) ||
478 !TII->isSafeToClobberEFLAGS(*MFI, MI) ||
479 Segment.getReg() != X86::NoRegister)
480 return nullptr;
481
482 unsigned int DstR = Dst.getReg();
483 unsigned int BaseR = Base.getReg();
484 unsigned int IndexR = Index.getReg();
485 unsigned SSDstR =
486 (LEAOpcode == X86::LEA64_32r) ? getX86SubSuperRegister(DstR, 64) : DstR;
487 bool IsScale1 = Scale.getImm() == 1;
488 bool IsInefficientBase = isInefficientLEAReg(BaseR);
489 bool IsInefficientIndex = isInefficientLEAReg(IndexR);
490
491 // Skip these cases since it takes more than 2 instructions
492 // to replace the LEA instruction.
493 if (IsInefficientBase && SSDstR == BaseR && !IsScale1)
494 return nullptr;
495 if (LEAOpcode == X86::LEA64_32r && IsInefficientBase &&
496 (IsInefficientIndex || !IsScale1))
497 return nullptr;
498
499 const DebugLoc DL = MI.getDebugLoc();
500 const MCInstrDesc &ADDrr = TII->get(getADDrrFromLEA(LEAOpcode));
501 const MCInstrDesc &ADDri = TII->get(getADDriFromLEA(LEAOpcode, Offset));
502
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000503 LLVM_DEBUG(dbgs() << "FixLEA: Candidate to replace:"; MI.dump(););
504 LLVM_DEBUG(dbgs() << "FixLEA: Replaced by: ";);
Lama Saba2ea271b2017-05-18 08:11:50 +0000505
506 // First try to replace LEA with one or two (for the 3-op LEA case)
507 // add instructions:
508 // 1.lea (%base,%index,1), %base => add %index,%base
509 // 2.lea (%base,%index,1), %index => add %base,%index
510 if (IsScale1 && (DstR == BaseR || DstR == IndexR)) {
511 const MachineOperand &Src = DstR == BaseR ? Index : Base;
512 MachineInstr *NewMI =
513 BuildMI(*MFI, MI, DL, ADDrr, DstR).addReg(DstR).add(Src);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000514 LLVM_DEBUG(NewMI->dump(););
Lama Saba2ea271b2017-05-18 08:11:50 +0000515 // Create ADD instruction for the Offset in case of 3-Ops LEA.
516 if (hasLEAOffset(Offset)) {
517 NewMI = BuildMI(*MFI, MI, DL, ADDri, DstR).addReg(DstR).add(Offset);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000518 LLVM_DEBUG(NewMI->dump(););
Lama Saba2ea271b2017-05-18 08:11:50 +0000519 }
520 return NewMI;
521 }
522 // If the base is inefficient try switching the index and base operands,
523 // otherwise just break the 3-Ops LEA inst into 2-Ops LEA + ADD instruction:
524 // lea offset(%base,%index,scale),%dst =>
525 // lea (%base,%index,scale); add offset,%dst
526 if (!IsInefficientBase || (!IsInefficientIndex && IsScale1)) {
527 MachineInstr *NewMI = BuildMI(*MFI, MI, DL, TII->get(LEAOpcode))
528 .add(Dst)
529 .add(IsInefficientBase ? Index : Base)
530 .add(Scale)
531 .add(IsInefficientBase ? Base : Index)
532 .addImm(0)
533 .add(Segment);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000534 LLVM_DEBUG(NewMI->dump(););
Lama Saba2ea271b2017-05-18 08:11:50 +0000535 // Create ADD instruction for the Offset in case of 3-Ops LEA.
536 if (hasLEAOffset(Offset)) {
537 NewMI = BuildMI(*MFI, MI, DL, ADDri, DstR).addReg(DstR).add(Offset);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000538 LLVM_DEBUG(NewMI->dump(););
Lama Saba2ea271b2017-05-18 08:11:50 +0000539 }
540 return NewMI;
541 }
542 // Handle the rest of the cases with inefficient base register:
543 assert(SSDstR != BaseR && "SSDstR == BaseR should be handled already!");
544 assert(IsInefficientBase && "efficient base should be handled already!");
545
546 // lea (%base,%index,1), %dst => mov %base,%dst; add %index,%dst
547 if (IsScale1 && !hasLEAOffset(Offset)) {
Serguei Katkov1ce71372018-01-26 04:49:26 +0000548 bool BIK = Base.isKill() && BaseR != IndexR;
549 TII->copyPhysReg(*MFI, MI, DL, DstR, BaseR, BIK);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000550 LLVM_DEBUG(MI.getPrevNode()->dump(););
Lama Saba2ea271b2017-05-18 08:11:50 +0000551
552 MachineInstr *NewMI =
553 BuildMI(*MFI, MI, DL, ADDrr, DstR).addReg(DstR).add(Index);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000554 LLVM_DEBUG(NewMI->dump(););
Lama Saba2ea271b2017-05-18 08:11:50 +0000555 return NewMI;
556 }
557 // lea offset(%base,%index,scale), %dst =>
558 // lea offset( ,%index,scale), %dst; add %base,%dst
559 MachineInstr *NewMI = BuildMI(*MFI, MI, DL, TII->get(LEAOpcode))
560 .add(Dst)
561 .addReg(0)
562 .add(Scale)
563 .add(Index)
564 .add(Offset)
565 .add(Segment);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000566 LLVM_DEBUG(NewMI->dump(););
Lama Saba2ea271b2017-05-18 08:11:50 +0000567
568 NewMI = BuildMI(*MFI, MI, DL, ADDrr, DstR).addReg(DstR).add(Base);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000569 LLVM_DEBUG(NewMI->dump(););
Lama Saba2ea271b2017-05-18 08:11:50 +0000570 return NewMI;
571}
572
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000573bool FixupLEAPass::processBasicBlock(MachineFunction &MF,
574 MachineFunction::iterator MFI) {
575
Alexey Volkov6226de62014-05-20 08:55:50 +0000576 for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I) {
Michael Kuperstein12982a82015-11-11 11:44:31 +0000577 if (OptIncDec)
578 if (fixupIncDec(I, MFI))
579 continue;
580
581 if (OptLEA) {
Craig Topper9164b9b2018-07-31 00:43:54 +0000582 if (MF.getSubtarget<X86Subtarget>().slowLEA())
Michael Kuperstein12982a82015-11-11 11:44:31 +0000583 processInstructionForSLM(I, MFI);
Lama Saba2ea271b2017-05-18 08:11:50 +0000584
585 else {
586 if (MF.getSubtarget<X86Subtarget>().slow3OpsLEA()) {
587 if (auto *NewMI = processInstrForSlow3OpLEA(*I, MFI)) {
588 MFI->erase(I);
589 I = NewMI;
590 }
591 } else
592 processInstruction(I, MFI);
593 }
Michael Kuperstein12982a82015-11-11 11:44:31 +0000594 }
Alexey Volkov6226de62014-05-20 08:55:50 +0000595 }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000596 return false;
597}