LLVM 24.0.0git
X86FixupLEAs.cpp
Go to the documentation of this file.
1//===-- X86FixupLEAs.cpp - use or replace LEA instructions -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the pass that finds instructions that can be
10// re-written as LEA instructions in order to reduce pipeline delays.
11// It replaces LEAs with ADD/INC/DEC when that is better for size/speed.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
16#include "X86InstrInfo.h"
17#include "X86Subtarget.h"
18#include "llvm/ADT/Statistic.h"
24#include "llvm/CodeGen/Passes.h"
27#include "llvm/Support/Debug.h"
29using namespace llvm;
30
31#define FIXUPLEA_DESC "X86 LEA Fixup"
32#define FIXUPLEA_NAME "x86-fixup-leas"
33
34#define DEBUG_TYPE FIXUPLEA_NAME
35
36STATISTIC(NumLEAs, "Number of LEA instructions created");
37
38namespace {
39class FixupLEAsImpl {
40 enum RegUsageState { RU_NotUsed, RU_Write, RU_Read };
41
42 /// Given a machine register, look for the instruction
43 /// which writes it in the current basic block. If found,
44 /// try to replace it with an equivalent LEA instruction.
45 /// If replacement succeeds, then also process the newly created
46 /// instruction.
47 void seekLEAFixup(MachineOperand &p, MachineBasicBlock::iterator &I,
48 MachineBasicBlock &MBB);
49
50 /// Given a memory access or LEA instruction
51 /// whose address mode uses a base and/or index register, look for
52 /// an opportunity to replace the instruction which sets the base or index
53 /// register with an equivalent LEA instruction.
54 void processInstruction(MachineBasicBlock::iterator &I,
55 MachineBasicBlock &MBB);
56
57 /// Given a LEA instruction which is unprofitable
58 /// on SlowLEA targets try to replace it with an equivalent ADD instruction.
59 void processInstructionForSlowLEA(MachineBasicBlock::iterator &I,
60 MachineBasicBlock &MBB);
61
62 /// Given a LEA instruction which is unprofitable
63 /// on SNB+ try to replace it with other instructions.
64 /// According to Intel's Optimization Reference Manual:
65 /// " For LEA instructions with three source operands and some specific
66 /// situations, instruction latency has increased to 3 cycles, and must
67 /// dispatch via port 1:
68 /// - LEA that has all three source operands: base, index, and offset
69 /// - LEA that uses base and index registers where the base is EBP, RBP,
70 /// or R13
71 /// - LEA that uses RIP relative addressing mode
72 /// - LEA that uses 16-bit addressing mode "
73 /// This function currently handles the first 2 cases only.
74 void processInstrForSlow3OpLEA(MachineBasicBlock::iterator &I,
75 MachineBasicBlock &MBB, bool OptIncDec);
76
77 /// Look for LEAs that are really two address LEAs that we might be able to
78 /// turn into regular ADD instructions.
79 bool optTwoAddrLEA(MachineBasicBlock::iterator &I,
80 MachineBasicBlock &MBB, bool OptIncDec,
81 bool UseLEAForSP) const;
82
83 /// Look for and transform the sequence
84 /// lea (reg1, reg2), reg3
85 /// sub reg3, reg4
86 /// to
87 /// sub reg1, reg4
88 /// sub reg2, reg4
89 /// It can also optimize the sequence lea/add similarly.
90 bool optLEAALU(MachineBasicBlock::iterator &I, MachineBasicBlock &MBB) const;
91
92 /// Step forwards in MBB, looking for an ADD/SUB instruction which uses
93 /// the dest register of LEA instruction I.
95 MachineBasicBlock &MBB) const;
96
97 /// Check instructions between LeaI and AluI (exclusively).
98 /// Set BaseIndexDef to true if base or index register from LeaI is defined.
99 /// Set AluDestRef to true if the dest register of AluI is used or defined.
100 /// *KilledBase is set to the killed base register usage.
101 /// *KilledIndex is set to the killed index register usage.
102 void checkRegUsage(MachineBasicBlock::iterator &LeaI,
103 MachineBasicBlock::iterator &AluI, bool &BaseIndexDef,
104 bool &AluDestRef, MachineOperand **KilledBase,
105 MachineOperand **KilledIndex) const;
106
107 /// Determine if an instruction references a machine register
108 /// and, if so, whether it reads or writes the register.
109 RegUsageState usesRegister(MachineOperand &p, MachineBasicBlock::iterator I);
110
111 /// Step backwards through a basic block, looking
112 /// for an instruction which writes a register within
113 /// a maximum of INSTR_DISTANCE_THRESHOLD instruction latency cycles.
114 MachineBasicBlock::iterator searchBackwards(MachineOperand &p,
116 MachineBasicBlock &MBB);
117
118 /// if an instruction can be converted to an
119 /// equivalent LEA, insert the new instruction into the basic block
120 /// and return a pointer to it. Otherwise, return zero.
121 MachineInstr *postRAConvertToLEA(MachineBasicBlock &MBB,
123
124public:
125 FixupLEAsImpl(ProfileSummaryInfo *PSI, MachineBlockFrequencyInfo *MBFI)
126 : PSI(PSI), MBFI(MBFI) {}
127
128 /// Loop over all of the basic blocks,
129 /// replacing instructions by equivalent LEA instructions
130 /// if needed and when possible.
131 bool runOnMachineFunction(MachineFunction &MF);
132
133private:
134 TargetSchedModel TSM;
135 const X86InstrInfo *TII = nullptr;
136 const X86RegisterInfo *TRI = nullptr;
137 ProfileSummaryInfo *PSI;
138 MachineBlockFrequencyInfo *MBFI;
139};
140
141class FixupLEAsLegacy : public MachineFunctionPass {
142public:
143 static char ID;
144
145 StringRef getPassName() const override { return FIXUPLEA_DESC; }
146
147 FixupLEAsLegacy() : MachineFunctionPass(ID) {}
148
149 bool runOnMachineFunction(MachineFunction &MF) override;
150
151 // This pass runs after regalloc and doesn't support VReg operands.
152 MachineFunctionProperties getRequiredProperties() const override {
153 return MachineFunctionProperties().setNoVRegs();
154 }
155
156 void getAnalysisUsage(AnalysisUsage &AU) const override {
157 AU.addRequired<ProfileSummaryInfoWrapperPass>();
158 AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
160 }
161};
162}
163
164char FixupLEAsLegacy::ID = 0;
165
166INITIALIZE_PASS(FixupLEAsLegacy, FIXUPLEA_NAME, FIXUPLEA_DESC, false, false)
167
169FixupLEAsImpl::postRAConvertToLEA(MachineBasicBlock &MBB,
171 MachineInstr &MI = *MBBI;
172 switch (MI.getOpcode()) {
173 case X86::MOV32rr:
174 case X86::MOV64rr: {
175 const MachineOperand &Src = MI.getOperand(1);
176 const MachineOperand &Dest = MI.getOperand(0);
177 MachineInstr *NewMI =
178 BuildMI(MBB, MBBI, MI.getDebugLoc(),
179 TII->get(MI.getOpcode() == X86::MOV32rr ? X86::LEA32r
180 : X86::LEA64r))
181 .add(Dest)
182 .add(Src)
183 .addImm(1)
184 .addReg(0)
185 .addImm(0)
186 .addReg(0);
187 return NewMI;
188 }
189 }
190
191 if (!MI.isConvertibleTo3Addr())
192 return nullptr;
193
194 switch (MI.getOpcode()) {
195 default:
196 // Only convert instructions that we've verified are safe.
197 return nullptr;
198 case X86::ADD64ri32:
199 case X86::ADD64ri32_DB:
200 case X86::ADD32ri:
201 case X86::ADD32ri_DB:
202 if (!MI.getOperand(2).isImm()) {
203 // convertToThreeAddress will call getImm()
204 // which requires isImm() to be true
205 return nullptr;
206 }
207 break;
208 case X86::SHL64ri:
209 case X86::SHL32ri:
210 case X86::INC64r:
211 case X86::INC32r:
212 case X86::DEC64r:
213 case X86::DEC32r:
214 case X86::ADD64rr:
215 case X86::ADD64rr_DB:
216 case X86::ADD32rr:
217 case X86::ADD32rr_DB:
218 // These instructions are all fine to convert.
219 break;
220 }
221 return TII->convertToThreeAddress(MI, /*LIS=*/nullptr);
222}
223
225 return new FixupLEAsLegacy();
226}
227
228static bool isLEA(unsigned Opcode) {
229 return Opcode == X86::LEA32r || Opcode == X86::LEA64r ||
230 Opcode == X86::LEA64_32r;
231}
232
233bool FixupLEAsImpl::runOnMachineFunction(MachineFunction &MF) {
234 const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
235 bool IsSlowLEA = ST.slowLEA();
236 bool IsSlow3OpsLEA = ST.slow3OpsLEA();
237 bool LEAUsesAG = ST.leaUsesAG();
238
239 bool OptIncDec = !ST.slowIncDec() || MF.getFunction().hasOptSize();
240 bool UseLEAForSP = ST.useLeaForSP();
241
242 TSM.init(&ST);
243 TII = ST.getInstrInfo();
244 TRI = ST.getRegisterInfo();
245
246 LLVM_DEBUG(dbgs() << "Start X86FixupLEAs\n";);
247 for (MachineBasicBlock &MBB : MF) {
248 // First pass. Try to remove or optimize existing LEAs.
249 bool OptIncDecPerBB =
250 OptIncDec || llvm::shouldOptimizeForSize(&MBB, PSI, MBFI);
251 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) {
252 if (!isLEA(I->getOpcode()))
253 continue;
254
255 if (optTwoAddrLEA(I, MBB, OptIncDecPerBB, UseLEAForSP))
256 continue;
257
258 if (IsSlowLEA)
259 processInstructionForSlowLEA(I, MBB);
260 else if (IsSlow3OpsLEA)
261 processInstrForSlow3OpLEA(I, MBB, OptIncDecPerBB);
262 }
263
264 // Second pass for creating LEAs. This may reverse some of the
265 // transformations above.
266 if (LEAUsesAG) {
267 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I)
268 processInstruction(I, MBB);
269 }
270 }
271
272 LLVM_DEBUG(dbgs() << "End X86FixupLEAs\n";);
273
274 return true;
275}
276
277FixupLEAsImpl::RegUsageState
278FixupLEAsImpl::usesRegister(MachineOperand &p, MachineBasicBlock::iterator I) {
279 RegUsageState RegUsage = RU_NotUsed;
280 MachineInstr &MI = *I;
281
282 for (const MachineOperand &MO : MI.operands()) {
283 if (MO.isReg() && MO.getReg() == p.getReg()) {
284 if (MO.isDef())
285 return RU_Write;
286 RegUsage = RU_Read;
287 }
288 }
289 return RegUsage;
290}
291
292/// getPreviousInstr - Given a reference to an instruction in a basic
293/// block, return a reference to the previous instruction in the block,
294/// wrapping around to the last instruction of the block if the block
295/// branches to itself.
298 if (I == MBB.begin()) {
299 if (MBB.isPredecessor(&MBB)) {
300 I = --MBB.end();
301 return true;
302 } else
303 return false;
304 }
305 --I;
306 return true;
307}
308
309MachineBasicBlock::iterator FixupLEAsImpl::searchBackwards(
310 MachineOperand &p, MachineBasicBlock::iterator &I, MachineBasicBlock &MBB) {
311 int InstrDistance = 1;
313 static const int INSTR_DISTANCE_THRESHOLD = 5;
314
315 CurInst = I;
316 bool Found;
317 Found = getPreviousInstr(CurInst, MBB);
318 while (Found && I != CurInst) {
319 if (CurInst->isCall() || CurInst->isInlineAsm())
320 break;
321 if (InstrDistance > INSTR_DISTANCE_THRESHOLD)
322 break; // too far back to make a difference
323 if (usesRegister(p, CurInst) == RU_Write) {
324 return CurInst;
325 }
326 InstrDistance += TSM.computeInstrLatency(&*CurInst);
327 Found = getPreviousInstr(CurInst, MBB);
328 }
330}
331
332static inline bool isInefficientLEAReg(Register Reg) {
333 return Reg == X86::EBP || Reg == X86::RBP ||
334 Reg == X86::R13D || Reg == X86::R13;
335}
336
337/// Returns true if this LEA uses base and index registers, and the base
338/// register is known to be inefficient for the subtarget.
339// TODO: use a variant scheduling class to model the latency profile
340// of LEA instructions, and implement this logic as a scheduling predicate.
342 const MachineOperand &Index) {
343 return Base.isReg() && isInefficientLEAReg(Base.getReg()) && Index.isReg() &&
344 Index.getReg().isValid();
345}
346
347// Returns true if this operand may have a non-zero offset.
348static inline bool mayHaveOffset(const MachineOperand &Offset) {
349 return !(Offset.isImm() && Offset.getImm() == 0);
350}
351
352static inline unsigned getADDrrFromLEA(unsigned LEAOpcode) {
353 switch (LEAOpcode) {
354 default:
355 llvm_unreachable("Unexpected LEA instruction");
356 case X86::LEA32r:
357 case X86::LEA64_32r:
358 return X86::ADD32rr;
359 case X86::LEA64r:
360 return X86::ADD64rr;
361 }
362}
363
364static inline unsigned getSUBrrFromLEA(unsigned LEAOpcode) {
365 switch (LEAOpcode) {
366 default:
367 llvm_unreachable("Unexpected LEA instruction");
368 case X86::LEA32r:
369 case X86::LEA64_32r:
370 return X86::SUB32rr;
371 case X86::LEA64r:
372 return X86::SUB64rr;
373 }
374}
375
376static inline unsigned getADDriFromLEA(unsigned LEAOpcode,
377 const MachineOperand &Offset) {
378 switch (LEAOpcode) {
379 default:
380 llvm_unreachable("Unexpected LEA instruction");
381 case X86::LEA32r:
382 case X86::LEA64_32r:
383 return X86::ADD32ri;
384 case X86::LEA64r:
385 return X86::ADD64ri32;
386 }
387}
388
389static inline unsigned getSUBriFromLEA(unsigned LEAOpcode) {
390 switch (LEAOpcode) {
391 default:
392 llvm_unreachable("Unexpected LEA instruction");
393 case X86::LEA32r:
394 case X86::LEA64_32r:
395 return X86::SUB32ri;
396 case X86::LEA64r:
397 return X86::SUB64ri32;
398 }
399}
400
401static inline unsigned getINCDECFromLEA(unsigned LEAOpcode, bool IsINC) {
402 switch (LEAOpcode) {
403 default:
404 llvm_unreachable("Unexpected LEA instruction");
405 case X86::LEA32r:
406 case X86::LEA64_32r:
407 return IsINC ? X86::INC32r : X86::DEC32r;
408 case X86::LEA64r:
409 return IsINC ? X86::INC64r : X86::DEC64r;
410 }
411}
412
414FixupLEAsImpl::searchALUInst(MachineBasicBlock::iterator &I,
415 MachineBasicBlock &MBB) const {
416 const X86Subtarget &ST = MBB.getParent()->getSubtarget<X86Subtarget>();
417 unsigned InstrDistance = 1;
418
419 unsigned LEAOpcode = I->getOpcode();
420 unsigned AddOpcode = getADDrrFromLEA(LEAOpcode);
421 unsigned SubOpcode = getSUBrrFromLEA(LEAOpcode);
422 Register DestReg = I->getOperand(0).getReg();
423
424 for (MachineInstr &CurInst : instructionsWithoutDebug(
425 std::next(I), MBB.end(), /*SkipPseudoOp=*/false)) {
426 if (CurInst.isCall() || CurInst.isInlineAsm())
427 break;
428 if (InstrDistance > ST.getCLOpts().fixup_leas_search_distance_threshold)
429 break;
430
431 // Check if the lea dest register is used in an add/sub instruction only.
432 for (unsigned I = 0, E = CurInst.getNumOperands(); I != E; ++I) {
433 MachineOperand &Opnd = CurInst.getOperand(I);
434 if (Opnd.isReg()) {
435 if (Opnd.getReg() == DestReg) {
436 if (Opnd.isDef() || !Opnd.isKill())
438
439 unsigned AluOpcode = CurInst.getOpcode();
440 if (AluOpcode != AddOpcode && AluOpcode != SubOpcode)
442
443 MachineOperand &Opnd2 = CurInst.getOperand(3 - I);
444 MachineOperand AluDest = CurInst.getOperand(0);
445 if (Opnd2.getReg() != AluDest.getReg())
447
448 // X - (Y + Z) may generate different flags than (X - Y) - Z when
449 // there is overflow. So we can't change the alu instruction if the
450 // flags register is live.
451 if (!CurInst.registerDefIsDead(X86::EFLAGS, TRI))
453
454 return CurInst.getIterator();
455 }
456 if (TRI->regsOverlap(DestReg, Opnd.getReg()))
458 }
459 }
460
461 InstrDistance++;
462 }
464}
465
466void FixupLEAsImpl::checkRegUsage(MachineBasicBlock::iterator &LeaI,
468 bool &BaseIndexDef, bool &AluDestRef,
469 MachineOperand **KilledBase,
470 MachineOperand **KilledIndex) const {
471 BaseIndexDef = AluDestRef = false;
472 *KilledBase = *KilledIndex = nullptr;
473 Register BaseReg = LeaI->getOperand(1 + X86::AddrBaseReg).getReg();
474 Register IndexReg = LeaI->getOperand(1 + X86::AddrIndexReg).getReg();
475 Register AluDestReg = AluI->getOperand(0).getReg();
476
477 for (MachineInstr &CurInst : llvm::make_range(std::next(LeaI), AluI)) {
478 for (MachineOperand &Opnd : CurInst.operands()) {
479 if (!Opnd.isReg())
480 continue;
481 Register Reg = Opnd.getReg();
482 if (TRI->regsOverlap(Reg, AluDestReg))
483 AluDestRef = true;
484 if (TRI->regsOverlap(Reg, BaseReg)) {
485 if (Opnd.isDef())
486 BaseIndexDef = true;
487 else if (Opnd.isKill())
488 *KilledBase = &Opnd;
489 }
490 if (TRI->regsOverlap(Reg, IndexReg)) {
491 if (Opnd.isDef())
492 BaseIndexDef = true;
493 else if (Opnd.isKill())
494 *KilledIndex = &Opnd;
495 }
496 }
497 }
498}
499
500bool FixupLEAsImpl::optLEAALU(MachineBasicBlock::iterator &I,
501 MachineBasicBlock &MBB) const {
502 // Look for an add/sub instruction which uses the result of lea.
503 MachineBasicBlock::iterator AluI = searchALUInst(I, MBB);
504 if (AluI == MachineBasicBlock::iterator())
505 return false;
506
507 // Check if there are any related register usage between lea and alu.
508 bool BaseIndexDef, AluDestRef;
509 MachineOperand *KilledBase, *KilledIndex;
510 checkRegUsage(I, AluI, BaseIndexDef, AluDestRef, &KilledBase, &KilledIndex);
511
512 MachineBasicBlock::iterator InsertPos = AluI;
513 if (BaseIndexDef) {
514 if (AluDestRef)
515 return false;
516 InsertPos = I;
517 KilledBase = KilledIndex = nullptr;
518 }
519
520 // Check if there are same registers.
521 Register AluDestReg = AluI->getOperand(0).getReg();
522 Register BaseReg = I->getOperand(1 + X86::AddrBaseReg).getReg();
523 Register IndexReg = I->getOperand(1 + X86::AddrIndexReg).getReg();
524 if (I->getOpcode() == X86::LEA64_32r) {
525 BaseReg = TRI->getSubReg(BaseReg, X86::sub_32bit);
526 IndexReg = TRI->getSubReg(IndexReg, X86::sub_32bit);
527 }
528 if (AluDestReg == IndexReg) {
529 if (BaseReg == IndexReg)
530 return false;
531 std::swap(BaseReg, IndexReg);
532 std::swap(KilledBase, KilledIndex);
533 }
534 if (BaseReg == IndexReg)
535 KilledBase = nullptr;
536
537 // Now it's safe to change instructions.
538 MachineInstr *NewMI1, *NewMI2;
539 unsigned NewOpcode = AluI->getOpcode();
540 NewMI1 = BuildMI(MBB, InsertPos, AluI->getDebugLoc(), TII->get(NewOpcode),
541 AluDestReg)
542 .addReg(AluDestReg, RegState::Kill)
543 .addReg(BaseReg, getKillRegState(KilledBase));
544 NewMI1->addRegisterDead(X86::EFLAGS, TRI);
545 NewMI2 = BuildMI(MBB, InsertPos, AluI->getDebugLoc(), TII->get(NewOpcode),
546 AluDestReg)
547 .addReg(AluDestReg, RegState::Kill)
548 .addReg(IndexReg, getKillRegState(KilledIndex));
549 NewMI2->addRegisterDead(X86::EFLAGS, TRI);
550
551 // Clear the old Kill flags.
552 if (KilledBase)
553 KilledBase->setIsKill(false);
554 if (KilledIndex)
555 KilledIndex->setIsKill(false);
556
557 MBB.getParent()->substituteDebugValuesForInst(*AluI, *NewMI2, 1);
558 MBB.erase(I);
559 MBB.erase(AluI);
560 I = NewMI1;
561 return true;
562}
563
564bool FixupLEAsImpl::optTwoAddrLEA(MachineBasicBlock::iterator &I,
565 MachineBasicBlock &MBB, bool OptIncDec,
566 bool UseLEAForSP) const {
567 MachineInstr &MI = *I;
568
569 const MachineOperand &Base = MI.getOperand(1 + X86::AddrBaseReg);
570 const MachineOperand &Scale = MI.getOperand(1 + X86::AddrScaleAmt);
571 const MachineOperand &Index = MI.getOperand(1 + X86::AddrIndexReg);
572 const MachineOperand &Disp = MI.getOperand(1 + X86::AddrDisp);
573 const MachineOperand &Segment = MI.getOperand(1 + X86::AddrSegmentReg);
574
575 if (Segment.getReg().isValid() || !Disp.isImm() || Scale.getImm() > 1 ||
576 MBB.computeRegisterLiveness(TRI, X86::EFLAGS, I) !=
578 return false;
579
580 Register DestReg = MI.getOperand(0).getReg();
581 Register BaseReg = Base.getReg();
582 Register IndexReg = Index.getReg();
583
584 // Don't change stack adjustment LEAs.
585 if (UseLEAForSP && (DestReg == X86::ESP || DestReg == X86::RSP))
586 return false;
587
588 // LEA64_32 has 64-bit operands but 32-bit result.
589 if (MI.getOpcode() == X86::LEA64_32r) {
590 if (BaseReg)
591 BaseReg = TRI->getSubReg(BaseReg, X86::sub_32bit);
592 if (IndexReg)
593 IndexReg = TRI->getSubReg(IndexReg, X86::sub_32bit);
594 }
595
596 MachineInstr *NewMI = nullptr;
597
598 // Case 1.
599 // Look for lea(%reg1, %reg2), %reg1 or lea(%reg2, %reg1), %reg1
600 // which can be turned into add %reg2, %reg1
601 if (BaseReg.isValid() && IndexReg.isValid() && Disp.getImm() == 0 &&
602 (DestReg == BaseReg || DestReg == IndexReg)) {
603 unsigned NewOpcode = getADDrrFromLEA(MI.getOpcode());
604 if (DestReg != BaseReg)
605 std::swap(BaseReg, IndexReg);
606
607 if (MI.getOpcode() == X86::LEA64_32r) {
608 // TODO: Do we need the super register implicit use?
609 NewMI = BuildMI(MBB, I, MI.getDebugLoc(), TII->get(NewOpcode), DestReg)
610 .addReg(BaseReg).addReg(IndexReg)
611 .addReg(Base.getReg(), RegState::Implicit)
612 .addReg(Index.getReg(), RegState::Implicit);
613 } else {
614 NewMI = BuildMI(MBB, I, MI.getDebugLoc(), TII->get(NewOpcode), DestReg)
615 .addReg(BaseReg).addReg(IndexReg);
616 }
617 } else if (DestReg == BaseReg && !IndexReg) {
618 // Case 2.
619 // This is an LEA with only a base register and a displacement,
620 // We can use ADDri or INC/DEC.
621
622 // Does this LEA have one these forms:
623 // lea %reg, 1(%reg)
624 // lea %reg, -1(%reg)
625 if (OptIncDec && (Disp.getImm() == 1 || Disp.getImm() == -1)) {
626 bool IsINC = Disp.getImm() == 1;
627 unsigned NewOpcode = getINCDECFromLEA(MI.getOpcode(), IsINC);
628
629 if (MI.getOpcode() == X86::LEA64_32r) {
630 // TODO: Do we need the super register implicit use?
631 NewMI = BuildMI(MBB, I, MI.getDebugLoc(), TII->get(NewOpcode), DestReg)
632 .addReg(BaseReg).addReg(Base.getReg(), RegState::Implicit);
633 } else {
634 NewMI = BuildMI(MBB, I, MI.getDebugLoc(), TII->get(NewOpcode), DestReg)
635 .addReg(BaseReg);
636 }
637 } else {
638 unsigned NewOpcode = getADDriFromLEA(MI.getOpcode(), Disp);
639 if (MI.getOpcode() == X86::LEA64_32r) {
640 // TODO: Do we need the super register implicit use?
641 NewMI = BuildMI(MBB, I, MI.getDebugLoc(), TII->get(NewOpcode), DestReg)
642 .addReg(BaseReg).addImm(Disp.getImm())
643 .addReg(Base.getReg(), RegState::Implicit);
644 } else {
645 NewMI = BuildMI(MBB, I, MI.getDebugLoc(), TII->get(NewOpcode), DestReg)
646 .addReg(BaseReg).addImm(Disp.getImm());
647 }
648 }
649 } else if (BaseReg.isValid() && IndexReg.isValid() && Disp.getImm() == 0) {
650 // Case 3.
651 // Look for and transform the sequence
652 // lea (reg1, reg2), reg3
653 // sub reg3, reg4
654 return optLEAALU(I, MBB);
655 } else
656 return false;
657
659 MBB.erase(I);
660 I = NewMI;
661 return true;
662}
663
664void FixupLEAsImpl::processInstruction(MachineBasicBlock::iterator &I,
665 MachineBasicBlock &MBB) {
666 // Process a load, store, or LEA instruction.
667 MachineInstr &MI = *I;
668 int AddrOffset = X86II::getMemoryOperandIdx(MI.getDesc());
669 if (AddrOffset >= 0) {
670 MachineOperand &p = MI.getOperand(AddrOffset + X86::AddrBaseReg);
671 if (p.isReg() && p.getReg() != X86::ESP) {
672 seekLEAFixup(p, I, MBB);
673 }
674 MachineOperand &q = MI.getOperand(AddrOffset + X86::AddrIndexReg);
675 if (q.isReg() && q.getReg() != X86::ESP) {
676 seekLEAFixup(q, I, MBB);
677 }
678 }
679}
680
681void FixupLEAsImpl::seekLEAFixup(MachineOperand &p,
683 MachineBasicBlock &MBB) {
684 MachineBasicBlock::iterator MBI = searchBackwards(p, I, MBB);
685 if (MBI != MachineBasicBlock::iterator()) {
686 MachineInstr *NewMI = postRAConvertToLEA(MBB, MBI);
687 if (NewMI) {
688 ++NumLEAs;
689 LLVM_DEBUG(dbgs() << "FixLEA: Candidate to replace:"; MBI->dump(););
690 // now to replace with an equivalent LEA...
691 LLVM_DEBUG(dbgs() << "FixLEA: Replaced by: "; NewMI->dump(););
692 MBB.getParent()->substituteDebugValuesForInst(*MBI, *NewMI, 1);
693 MBB.erase(MBI);
695 static_cast<MachineBasicBlock::iterator>(NewMI);
696 processInstruction(J, MBB);
697 }
698 }
699}
700
701void FixupLEAsImpl::processInstructionForSlowLEA(MachineBasicBlock::iterator &I,
702 MachineBasicBlock &MBB) {
703 MachineInstr &MI = *I;
704 const unsigned Opcode = MI.getOpcode();
705
706 const MachineOperand &Dst = MI.getOperand(0);
707 const MachineOperand &Base = MI.getOperand(1 + X86::AddrBaseReg);
708 const MachineOperand &Scale = MI.getOperand(1 + X86::AddrScaleAmt);
709 const MachineOperand &Index = MI.getOperand(1 + X86::AddrIndexReg);
710 const MachineOperand &Offset = MI.getOperand(1 + X86::AddrDisp);
711 const MachineOperand &Segment = MI.getOperand(1 + X86::AddrSegmentReg);
712
713 if (Segment.getReg().isValid() || !Offset.isImm() ||
714 MBB.computeRegisterLiveness(TRI, X86::EFLAGS, I, 4) !=
716 return;
717 const Register DstR = Dst.getReg();
718 const Register SrcR1 = Base.getReg();
719 const Register SrcR2 = Index.getReg();
720 if ((!SrcR1 || SrcR1 != DstR) && (!SrcR2 || SrcR2 != DstR))
721 return;
722 if (Scale.getImm() > 1)
723 return;
724 LLVM_DEBUG(dbgs() << "FixLEA: Candidate to replace:"; I->dump(););
725 LLVM_DEBUG(dbgs() << "FixLEA: Replaced by: ";);
726 MachineInstr *NewMI = nullptr;
727 // Make ADD instruction for two registers writing to LEA's destination
728 if (SrcR1 && SrcR2) {
729 const MCInstrDesc &ADDrr = TII->get(getADDrrFromLEA(Opcode));
730 const MachineOperand &Src = SrcR1 == DstR ? Index : Base;
731 NewMI =
732 BuildMI(MBB, I, MI.getDebugLoc(), ADDrr, DstR).addReg(DstR).add(Src);
733 LLVM_DEBUG(NewMI->dump(););
734 }
735 // Make ADD instruction for immediate
736 if (Offset.getImm() != 0) {
737 const MCInstrDesc &ADDri =
738 TII->get(getADDriFromLEA(Opcode, Offset));
739 const MachineOperand &SrcR = SrcR1 == DstR ? Base : Index;
740 NewMI = BuildMI(MBB, I, MI.getDebugLoc(), ADDri, DstR)
741 .add(SrcR)
742 .addImm(Offset.getImm());
743 LLVM_DEBUG(NewMI->dump(););
744 }
745 if (NewMI) {
747 MBB.erase(I);
748 I = NewMI;
749 }
750}
751
752void FixupLEAsImpl::processInstrForSlow3OpLEA(MachineBasicBlock::iterator &I,
753 MachineBasicBlock &MBB,
754 bool OptIncDec) {
755 MachineInstr &MI = *I;
756 const unsigned LEAOpcode = MI.getOpcode();
757
758 const MachineOperand &Dest = MI.getOperand(0);
759 const MachineOperand &Base = MI.getOperand(1 + X86::AddrBaseReg);
760 const MachineOperand &Scale = MI.getOperand(1 + X86::AddrScaleAmt);
761 const MachineOperand &Index = MI.getOperand(1 + X86::AddrIndexReg);
762 const MachineOperand &Offset = MI.getOperand(1 + X86::AddrDisp);
763 const MachineOperand &Segment = MI.getOperand(1 + X86::AddrSegmentReg);
764
765 if (!(TII->isThreeOperandsLEA(MI) || hasInefficientLEABaseReg(Base, Index)) ||
766 MBB.computeRegisterLiveness(TRI, X86::EFLAGS, I, 4) !=
768 Segment.getReg().isValid())
769 return;
770
771 Register DestReg = Dest.getReg();
772 Register BaseReg = Base.getReg();
773 Register IndexReg = Index.getReg();
774
775 if (MI.getOpcode() == X86::LEA64_32r) {
776 if (BaseReg)
777 BaseReg = TRI->getSubReg(BaseReg, X86::sub_32bit);
778 if (IndexReg)
779 IndexReg = TRI->getSubReg(IndexReg, X86::sub_32bit);
780 }
781
782 bool IsScale1 = Scale.getImm() == 1;
783 bool IsInefficientBase = isInefficientLEAReg(BaseReg);
784 bool IsInefficientIndex = isInefficientLEAReg(IndexReg);
785
786 // Skip these cases since it takes more than 2 instructions
787 // to replace the LEA instruction.
788 if (IsInefficientBase && DestReg == BaseReg && !IsScale1)
789 return;
790
791 LLVM_DEBUG(dbgs() << "FixLEA: Candidate to replace:"; MI.dump(););
792 LLVM_DEBUG(dbgs() << "FixLEA: Replaced by: ";);
793
794 MachineInstr *NewMI = nullptr;
795 bool BaseOrIndexIsDst = DestReg == BaseReg || DestReg == IndexReg;
796 // First try and remove the base while sticking with LEA iff base == index and
797 // scale == 1. We can handle:
798 // 1. lea D(%base,%index,1) -> lea D(,%index,2)
799 // 2. lea D(%r13/%rbp,%index) -> lea D(,%index,2)
800 // Only do this if the LEA would otherwise be split into 2-instruction
801 // (either it has a an Offset or neither base nor index are dst)
802 if (IsScale1 && BaseReg == IndexReg &&
803 (mayHaveOffset(Offset) || (IsInefficientBase && !BaseOrIndexIsDst))) {
804 NewMI = BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(LEAOpcode))
805 .add(Dest)
806 .addReg(0)
807 .addImm(2)
808 .add(Index)
809 .add(Offset)
810 .add(Segment);
811 LLVM_DEBUG(NewMI->dump(););
812
814 MBB.erase(I);
815 I = NewMI;
816 return;
817 } else if (IsScale1 && BaseOrIndexIsDst) {
818 // Try to replace LEA with one or two (for the 3-op LEA case)
819 // add instructions:
820 // 1.lea (%base,%index,1), %base => add %index,%base
821 // 2.lea (%base,%index,1), %index => add %base,%index
822
823 unsigned NewOpc = getADDrrFromLEA(MI.getOpcode());
824 if (DestReg != BaseReg)
825 std::swap(BaseReg, IndexReg);
826
827 if (MI.getOpcode() == X86::LEA64_32r) {
828 // TODO: Do we need the super register implicit use?
829 NewMI = BuildMI(MBB, I, MI.getDebugLoc(), TII->get(NewOpc), DestReg)
830 .addReg(BaseReg)
831 .addReg(IndexReg)
832 .addReg(Base.getReg(), RegState::Implicit)
833 .addReg(Index.getReg(), RegState::Implicit);
834 } else {
835 NewMI = BuildMI(MBB, I, MI.getDebugLoc(), TII->get(NewOpc), DestReg)
836 .addReg(BaseReg)
837 .addReg(IndexReg);
838 }
839 } else if (!IsInefficientBase || (!IsInefficientIndex && IsScale1)) {
840 // If the base is inefficient try switching the index and base operands,
841 // otherwise just break the 3-Ops LEA inst into 2-Ops LEA + ADD instruction:
842 // lea offset(%base,%index,scale),%dst =>
843 // lea (%base,%index,scale); add offset,%dst
844 NewMI = BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(LEAOpcode))
845 .add(Dest)
846 .add(IsInefficientBase ? Index : Base)
847 .add(Scale)
848 .add(IsInefficientBase ? Base : Index)
849 .addImm(0)
850 .add(Segment);
851 LLVM_DEBUG(NewMI->dump(););
852 }
853
854 // If either replacement succeeded above, add the offset if needed, then
855 // replace the instruction.
856 if (NewMI) {
857 // Create ADD instruction for the Offset in case of 3-Ops LEA.
858 if (mayHaveOffset(Offset)) {
859 if (OptIncDec && Offset.isImm() &&
860 (Offset.getImm() == 1 || Offset.getImm() == -1)) {
861 unsigned NewOpc =
862 getINCDECFromLEA(MI.getOpcode(), Offset.getImm() == 1);
863 NewMI = BuildMI(MBB, I, MI.getDebugLoc(), TII->get(NewOpc), DestReg)
864 .addReg(DestReg);
865 LLVM_DEBUG(NewMI->dump(););
866 } else if (Offset.isImm() && Offset.getImm() == 128) {
867 // ADD of +128 needs a 32-bit immediate, while SUB of -128 fits the
868 // sign-extended 8-bit form, three bytes shorter. EFLAGS was proved
869 // dead above, so the different flag results don't matter.
870 unsigned NewOpc = getSUBriFromLEA(MI.getOpcode());
871 NewMI = BuildMI(MBB, I, MI.getDebugLoc(), TII->get(NewOpc), DestReg)
872 .addReg(DestReg)
873 .addImm(-128);
874 LLVM_DEBUG(NewMI->dump(););
875 } else {
876 unsigned NewOpc = getADDriFromLEA(MI.getOpcode(), Offset);
877 NewMI = BuildMI(MBB, I, MI.getDebugLoc(), TII->get(NewOpc), DestReg)
878 .addReg(DestReg)
879 .add(Offset);
880 LLVM_DEBUG(NewMI->dump(););
881 }
882 }
883
885 MBB.erase(I);
886 I = NewMI;
887 return;
888 }
889
890 // Handle the rest of the cases with inefficient base register:
891 assert(DestReg != BaseReg && "DestReg == BaseReg should be handled already!");
892 assert(IsInefficientBase && "efficient base should be handled already!");
893
894 // FIXME: Handle LEA64_32r.
895 if (LEAOpcode == X86::LEA64_32r)
896 return;
897
898 // lea (%base,%index,1), %dst => mov %base,%dst; add %index,%dst
899 if (IsScale1 && !mayHaveOffset(Offset)) {
900 bool BIK = Base.isKill() && BaseReg != IndexReg;
901 TII->copyPhysReg(MBB, MI, MI.getDebugLoc(), DestReg, BaseReg, BIK);
902 LLVM_DEBUG(MI.getPrevNode()->dump(););
903
904 unsigned NewOpc = getADDrrFromLEA(MI.getOpcode());
905 NewMI = BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(NewOpc), DestReg)
906 .addReg(DestReg)
907 .add(Index);
908 LLVM_DEBUG(NewMI->dump(););
909
911 MBB.erase(I);
912 I = NewMI;
913 return;
914 }
915
916 // lea offset(%base,%index,scale), %dst =>
917 // lea offset( ,%index,scale), %dst; add %base,%dst
918 NewMI = BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(LEAOpcode))
919 .add(Dest)
920 .addReg(0)
921 .add(Scale)
922 .add(Index)
923 .add(Offset)
924 .add(Segment);
925 LLVM_DEBUG(NewMI->dump(););
926
927 unsigned NewOpc = getADDrrFromLEA(MI.getOpcode());
928 NewMI = BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(NewOpc), DestReg)
929 .addReg(DestReg)
930 .add(Base);
931 LLVM_DEBUG(NewMI->dump(););
932
934 MBB.erase(I);
935 I = NewMI;
936}
937
938bool FixupLEAsLegacy::runOnMachineFunction(MachineFunction &MF) {
939 if (skipFunction(MF.getFunction()))
940 return false;
941
942 auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
943 auto *MBFI = (PSI && PSI->hasProfileSummary())
944 ? &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI()
945 : nullptr;
946 FixupLEAsImpl PassImpl(PSI, MBFI);
947 return PassImpl.runOnMachineFunction(MF);
948}
949
952 ProfileSummaryInfo *PSI =
954 .getCachedResult<ProfileSummaryAnalysis>(
955 *MF.getFunction().getParent());
956 if (!PSI)
957 report_fatal_error("x86-fixup-leas requires ProfileSummaryAnalysis", false);
960
961 FixupLEAsImpl PassImpl(PSI, MBFI);
962 bool Changed = PassImpl.runOnMachineFunction(MF);
963 if (!Changed)
964 return PreservedAnalyses::all();
967 return PA;
968}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
===- LazyMachineBlockFrequencyInfo.h - Lazy Block Frequency -*- C++ -*–===//
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
static unsigned getINCDECFromLEA(unsigned LEAOpcode, bool IsINC)
static bool isLEA(unsigned Opcode)
static unsigned getSUBriFromLEA(unsigned LEAOpcode)
static bool isInefficientLEAReg(Register Reg)
static bool hasInefficientLEABaseReg(const MachineOperand &Base, const MachineOperand &Index)
Returns true if this LEA uses base and index registers, and the base register is known to be ineffici...
static unsigned getADDriFromLEA(unsigned LEAOpcode, const MachineOperand &Offset)
static bool getPreviousInstr(MachineBasicBlock::iterator &I, MachineBasicBlock &MBB)
getPreviousInstr - Given a reference to an instruction in a basic block, return a reference to the pr...
#define FIXUPLEA_DESC
static unsigned getADDrrFromLEA(unsigned LEAOpcode)
#define FIXUPLEA_NAME
static unsigned getSUBrrFromLEA(unsigned LEAOpcode)
static bool mayHaveOffset(const MachineOperand &Offset)
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
Definition Function.h:699
Module * getParent()
Get the module that this global value is contained inside of...
void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, const DebugLoc &DL, Register DestReg, Register SrcReg, bool KillSrc, bool RenamableDest=false, bool RenamableSrc=false) const override
Emit instructions to copy a pair of physical registers.
LLVM_ABI LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI, MCRegister Reg, const_iterator Before, unsigned Neighborhood=10) const
Return whether (physical) register Reg has been defined and not killed as of just before Before.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
MachineInstrBundleIterator< MachineInstr > iterator
@ LQR_Dead
Register is known to be fully dead.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
void substituteDebugValuesForInst(const MachineInstr &Old, MachineInstr &New, unsigned MaxOperand=UINT_MAX)
Create substitutions for any tracked values in Old, to point at New.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
Representation of each machine instruction.
LLVM_ABI void dump() const
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI bool addRegisterDead(Register Reg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
We have determined MI defined a register without a use.
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
void setIsKill(bool Val=true)
Register getReg() const
getReg - Returns the register number.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
Analysis providing profile information.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isValid() const
Definition Register.h:112
LLVM_ABI void init(const TargetSubtargetInfo *TSInfo, bool EnableSModel=true, bool EnableSItins=true)
Initialize the machine model for instruction scheduling.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
int getMemoryOperandIdx(const MCInstrDesc &Desc)
BaseReg
Stack frame base register. Bit 0 of FREInfo.Info.
Definition SFrame.h:77
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
OuterAnalysisManagerProxy< ModuleAnalysisManager, MachineFunction > ModuleAnalysisManagerMachineFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr RegState getKillRegState(bool B)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
FunctionPass * createX86FixupLEAsLegacyPass()
auto instructionsWithoutDebug(IterT It, IterT End, bool SkipPseudoOp=true)
Construct a range iterator which begins at It and moves forwards until End is reached,...
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880