30#define FIXUPLEA_DESC "X86 LEA Fixup"
31#define FIXUPLEA_NAME "x86-fixup-LEAs"
33#define DEBUG_TYPE FIXUPLEA_NAME
35STATISTIC(NumLEAs,
"Number of LEA instructions created");
39 enum RegUsageState { RU_NotUsed, RU_Write, RU_Read };
47 MachineBasicBlock &
MBB);
54 MachineBasicBlock &
MBB);
59 MachineBasicBlock &
MBB);
74 MachineBasicBlock &
MBB,
bool OptIncDec);
79 MachineBasicBlock &
MBB,
bool OptIncDec,
80 bool UseLEAForSP)
const;
94 MachineBasicBlock &
MBB)
const;
103 bool &AluDestRef, MachineOperand **KilledBase,
104 MachineOperand **KilledIndex)
const;
115 MachineBasicBlock &
MBB);
120 MachineInstr *postRAConvertToLEA(MachineBasicBlock &
MBB,
126 StringRef getPassName()
const override {
return FIXUPLEA_DESC; }
128 FixupLEAPass() : MachineFunctionPass(ID) { }
133 bool runOnMachineFunction(MachineFunction &MF)
override;
136 MachineFunctionProperties getRequiredProperties()
const override {
137 return MachineFunctionProperties().setNoVRegs();
140 void getAnalysisUsage(AnalysisUsage &AU)
const override {
142 AU.
addRequired<LazyMachineBlockFrequencyInfoPass>();
147 TargetSchedModel TSM;
148 const X86InstrInfo *TII =
nullptr;
149 const X86RegisterInfo *TRI =
nullptr;
153char FixupLEAPass::ID = 0;
161 switch (
MI.getOpcode()) {
164 const MachineOperand &Src = MI.getOperand(1);
165 const MachineOperand &Dest = MI.getOperand(0);
166 MachineInstr *NewMI =
167 BuildMI(MBB, MBBI, MI.getDebugLoc(),
168 TII->get(MI.getOpcode() == X86::MOV32rr ? X86::LEA32r
180 if (!
MI.isConvertibleTo3Addr())
183 switch (
MI.getOpcode()) {
188 case X86::ADD64ri32_DB:
190 case X86::ADD32ri_DB:
191 if (!MI.getOperand(2).isImm()) {
204 case X86::ADD64rr_DB:
206 case X86::ADD32rr_DB:
210 return TII->convertToThreeAddress(
MI,
nullptr,
nullptr);
216 return Opcode == X86::LEA32r || Opcode == X86::LEA64r ||
217 Opcode == X86::LEA64_32r;
225 bool IsSlowLEA =
ST.slowLEA();
226 bool IsSlow3OpsLEA =
ST.slow3OpsLEA();
227 bool LEAUsesAG =
ST.leaUsesAG();
230 bool UseLEAForSP =
ST.useLeaForSP();
233 TII =
ST.getInstrInfo();
234 TRI =
ST.getRegisterInfo();
235 auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
236 auto *MBFI = (PSI && PSI->hasProfileSummary())
237 ? &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI()
241 for (MachineBasicBlock &
MBB : MF) {
243 bool OptIncDecPerBB =
246 if (!
isLEA(
I->getOpcode()))
249 if (optTwoAddrLEA(
I,
MBB, OptIncDecPerBB, UseLEAForSP))
253 processInstructionForSlowLEA(
I,
MBB);
254 else if (IsSlow3OpsLEA)
255 processInstrForSlow3OpLEA(
I,
MBB, OptIncDecPerBB);
262 processInstruction(
I,
MBB);
271FixupLEAPass::RegUsageState
273 RegUsageState
RegUsage = RU_NotUsed;
274 MachineInstr &
MI = *
I;
276 for (
const MachineOperand &MO :
MI.operands()) {
277 if (MO.isReg() && MO.getReg() ==
p.getReg()) {
292 if (
I ==
MBB.begin()) {
293 if (
MBB.isPredecessor(&
MBB)) {
305 MachineBasicBlock &
MBB) {
306 int InstrDistance = 1;
308 static const int INSTR_DISTANCE_THRESHOLD = 5;
313 while (Found &&
I != CurInst) {
314 if (CurInst->isCall() || CurInst->isInlineAsm())
316 if (InstrDistance > INSTR_DISTANCE_THRESHOLD)
318 if (usesRegister(p, CurInst) == RU_Write) {
321 InstrDistance += TSM.computeInstrLatency(&*CurInst);
328 return Reg == X86::EBP ||
Reg == X86::RBP ||
329 Reg == X86::R13D ||
Reg == X86::R13;
339 Index.getReg().isValid();
380 return X86::ADD64ri32;
390 return IsINC ? X86::INC32r : X86::DEC32r;
392 return IsINC ? X86::INC64r : X86::DEC64r;
398 MachineBasicBlock &
MBB)
const {
399 const int InstrDistanceThreshold = 5;
400 int InstrDistance = 1;
403 unsigned LEAOpcode =
I->getOpcode();
406 Register DestReg =
I->getOperand(0).getReg();
408 while (CurInst !=
MBB.
end()) {
409 if (CurInst->isCall() || CurInst->isInlineAsm())
411 if (InstrDistance > InstrDistanceThreshold)
415 for (
unsigned I = 0,
E = CurInst->getNumOperands();
I !=
E; ++
I) {
416 MachineOperand &Opnd = CurInst->getOperand(
I);
418 if (Opnd.
getReg() == DestReg) {
422 unsigned AluOpcode = CurInst->getOpcode();
423 if (AluOpcode != AddOpcode && AluOpcode != SubOpcode)
426 MachineOperand &Opnd2 = CurInst->getOperand(3 -
I);
427 MachineOperand AluDest = CurInst->getOperand(0);
434 if (!CurInst->registerDefIsDead(X86::EFLAGS,
TRI))
439 if (
TRI->regsOverlap(DestReg, Opnd.
getReg()))
452 bool &BaseIndexDef,
bool &AluDestRef,
453 MachineOperand **KilledBase,
454 MachineOperand **KilledIndex)
const {
455 BaseIndexDef = AluDestRef =
false;
456 *KilledBase = *KilledIndex =
nullptr;
459 Register AluDestReg = AluI->getOperand(0).getReg();
462 for (MachineOperand &Opnd : CurInst.operands()) {
466 if (
TRI->regsOverlap(
Reg, AluDestReg))
468 if (
TRI->regsOverlap(
Reg, BaseReg)) {
474 if (
TRI->regsOverlap(
Reg, IndexReg)) {
478 *KilledIndex = &Opnd;
485 MachineBasicBlock &
MBB)
const {
492 bool BaseIndexDef, AluDestRef;
493 MachineOperand *KilledBase, *KilledIndex;
494 checkRegUsage(
I, AluI, BaseIndexDef, AluDestRef, &KilledBase, &KilledIndex);
501 KilledBase = KilledIndex =
nullptr;
505 Register AluDestReg = AluI->getOperand(0).getReg();
508 if (
I->getOpcode() == X86::LEA64_32r) {
509 BaseReg =
TRI->getSubReg(BaseReg, X86::sub_32bit);
510 IndexReg =
TRI->getSubReg(IndexReg, X86::sub_32bit);
512 if (AluDestReg == IndexReg) {
513 if (BaseReg == IndexReg)
518 if (BaseReg == IndexReg)
519 KilledBase =
nullptr;
522 MachineInstr *NewMI1, *NewMI2;
523 unsigned NewOpcode = AluI->getOpcode();
524 NewMI1 =
BuildMI(
MBB, InsertPos, AluI->getDebugLoc(),
TII->get(NewOpcode),
529 NewMI2 =
BuildMI(
MBB, InsertPos, AluI->getDebugLoc(),
TII->get(NewOpcode),
549 MachineBasicBlock &
MBB,
bool OptIncDec,
550 bool UseLEAForSP)
const {
551 MachineInstr &
MI = *
I;
569 if (UseLEAForSP && (DestReg == X86::ESP || DestReg == X86::RSP))
573 if (
MI.getOpcode() == X86::LEA64_32r) {
575 BaseReg =
TRI->getSubReg(BaseReg, X86::sub_32bit);
577 IndexReg =
TRI->getSubReg(IndexReg, X86::sub_32bit);
580 MachineInstr *NewMI =
nullptr;
586 (DestReg == BaseReg || DestReg == IndexReg)) {
588 if (DestReg != BaseReg)
591 if (
MI.getOpcode() == X86::LEA64_32r) {
601 }
else if (DestReg == BaseReg && !IndexReg) {
609 if (OptIncDec && (Disp.
getImm() == 1 || Disp.
getImm() == -1)) {
610 bool IsINC = Disp.
getImm() == 1;
613 if (
MI.getOpcode() == X86::LEA64_32r) {
623 if (
MI.getOpcode() == X86::LEA64_32r) {
638 return optLEAALU(
I,
MBB);
649 MachineBasicBlock &
MBB) {
651 MachineInstr &
MI = *
I;
652 const MCInstrDesc &
Desc =
MI.getDesc();
654 if (AddrOffset >= 0) {
657 if (
p.isReg() &&
p.getReg() != X86::ESP) {
658 seekLEAFixup(p,
I,
MBB);
661 if (
q.isReg() &&
q.getReg() != X86::ESP) {
662 seekLEAFixup(q,
I,
MBB);
667void FixupLEAPass::seekLEAFixup(MachineOperand &p,
669 MachineBasicBlock &
MBB) {
672 MachineInstr *NewMI = postRAConvertToLEA(
MBB, MBI);
675 LLVM_DEBUG(
dbgs() <<
"FixLEA: Candidate to replace:"; MBI->dump(););
682 processInstruction(J,
MBB);
688 MachineBasicBlock &
MBB) {
689 MachineInstr &
MI = *
I;
690 const unsigned Opcode =
MI.getOpcode();
692 const MachineOperand &Dst =
MI.getOperand(0);
706 if ((!SrcR1 || SrcR1 != DstR) && (!SrcR2 || SrcR2 != DstR))
712 MachineInstr *NewMI =
nullptr;
714 if (SrcR1 && SrcR2) {
716 const MachineOperand &Src = SrcR1 == DstR ?
Index :
Base;
722 if (
Offset.getImm() != 0) {
723 const MCInstrDesc &ADDri =
725 const MachineOperand &SrcR = SrcR1 == DstR ?
Base :
Index;
739 MachineBasicBlock &
MBB,
741 MachineInstr &
MI = *
I;
742 const unsigned LEAOpcode =
MI.getOpcode();
744 const MachineOperand &Dest =
MI.getOperand(0);
761 if (
MI.getOpcode() == X86::LEA64_32r) {
763 BaseReg =
TRI->getSubReg(BaseReg, X86::sub_32bit);
765 IndexReg =
TRI->getSubReg(IndexReg, X86::sub_32bit);
768 bool IsScale1 = Scale.
getImm() == 1;
774 if (IsInefficientBase && DestReg == BaseReg && !IsScale1)
780 MachineInstr *NewMI =
nullptr;
781 bool BaseOrIndexIsDst = DestReg ==
BaseReg || DestReg == IndexReg;
788 if (IsScale1 && BaseReg == IndexReg &&
803 }
else if (IsScale1 && BaseOrIndexIsDst) {
810 if (DestReg != BaseReg)
813 if (
MI.getOpcode() == X86::LEA64_32r) {
825 }
else if (!IsInefficientBase || (!IsInefficientIndex && IsScale1)) {
832 .
add(IsInefficientBase ? Index :
Base)
834 .
add(IsInefficientBase ?
Base : Index)
845 if (OptIncDec &&
Offset.isImm() &&
868 assert(DestReg != BaseReg &&
"DestReg == BaseReg should be handled already!");
869 assert(IsInefficientBase &&
"efficient base should be handled already!");
872 if (LEAOpcode == X86::LEA64_32r)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const HexagonInstrInfo * TII
===- LazyMachineBlockFrequencyInfo.h - Lazy Block Frequency -*- C++ -*–===//
Register const TargetRegisterInfo * TRI
Promote Memory to Register
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static unsigned getINCDECFromLEA(unsigned LEAOpcode, bool IsINC)
static bool isLEA(unsigned Opcode)
static bool hasLEAOffset(const MachineOperand &Offset)
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...
static unsigned getADDrrFromLEA(unsigned LEAOpcode)
static unsigned getSUBrrFromLEA(unsigned LEAOpcode)
AnalysisUsage & addRequired()
FunctionPass class - This class is used to implement most global optimizations.
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
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.
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 & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
LLVM_ABI void dump() 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.
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.
Wrapper class representing virtual and physical registers.
constexpr bool isValid() const
LLVM_ABI void init(const TargetSubtargetInfo *TSInfo, bool EnableSModel=true, bool EnableSItins=true)
Initialize the machine model for instruction scheduling.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Kill
The last use of a register.
int getMemoryOperandNo(uint64_t TSFlags)
unsigned getOperandBias(const MCInstrDesc &Desc)
Compute whether all of the def operands are repeated in the uses and therefore should be skipped.
BaseReg
Stack frame base register. Bit 0 of FREInfo.Info.
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
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.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
FunctionPass * createX86FixupLEAs()
Return a pass that selectively replaces certain instructions (like add, sub, inc, dec,...
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.