27 #define FIXUPLEA_DESC "X86 LEA Fixup" 28 #define FIXUPLEA_NAME "x86-fixup-LEAs" 30 #define DEBUG_TYPE FIXUPLEA_NAME 32 STATISTIC(NumLEAs,
"Number of LEA instructions created");
36 enum RegUsageState { RU_NotUsed, RU_Write, RU_Read };
77 bool UseLEAForSP)
const;
157 case X86::ADD64ri32_DB:
158 case X86::ADD64ri8_DB:
161 case X86::ADD32ri_DB:
162 case X86::ADD32ri8_DB:
176 case X86::ADD64rr_DB:
178 case X86::ADD32rr_DB:
183 return TII->convertToThreeAddress(MFI, MI,
nullptr);
188 static bool isLEA(
unsigned Opcode) {
189 return Opcode == X86::LEA32r || Opcode == X86::LEA64r ||
190 Opcode == X86::LEA64_32r;
213 if (!
isLEA(
I->getOpcode()))
216 if (optTwoAddrLEA(
I, MBB, OptIncDec, UseLEAForSP))
220 processInstructionForSlowLEA(
I, MBB);
221 else if (IsSlow3OpsLEA)
222 processInstrForSlow3OpLEA(
I, MBB, OptIncDec);
229 processInstruction(
I, MBB);
238 FixupLEAPass::RegUsageState
240 RegUsageState
RegUsage = RU_NotUsed;
260 if (I == MBB.
begin()) {
274 int InstrDistance = 1;
276 static const int INSTR_DISTANCE_THRESHOLD = 5;
281 while (Found && I != CurInst) {
282 if (CurInst->isCall() || CurInst->isInlineAsm())
284 if (InstrDistance > INSTR_DISTANCE_THRESHOLD)
286 if (usesRegister(p, CurInst) == RU_Write) {
289 InstrDistance += TSM.computeInstrLatency(&*CurInst);
296 return Reg == X86::EBP || Reg == X86::RBP ||
297 Reg == X86::R13D || Reg == X86::R13;
307 Index.
getReg() != X86::NoRegister;
334 return IsInt8 ? X86::ADD32ri8 : X86::ADD32ri;
336 return IsInt8 ? X86::ADD64ri8 : X86::ADD64ri32;
346 return IsINC ? X86::INC32r : X86::DEC32r;
348 return IsINC ? X86::INC64r : X86::DEC64r;
354 bool UseLEAForSP)
const {
364 !
TII->isSafeToClobberEFLAGS(MBB, I))
372 if (UseLEAForSP && (DestReg ==
X86::ESP || DestReg == X86::RSP))
378 BaseReg =
TRI->getSubReg(BaseReg, X86::sub_32bit);
380 IndexReg =
TRI->getSubReg(IndexReg, X86::sub_32bit);
387 if (BaseReg != 0 && IndexReg != 0 && Disp.
getImm() == 0 &&
388 (DestReg == BaseReg || DestReg == IndexReg)) {
390 if (DestReg != BaseReg)
396 .addReg(BaseReg).
addReg(IndexReg)
401 .addReg(BaseReg).
addReg(IndexReg);
403 }
else if (DestReg == BaseReg && IndexReg == 0) {
410 if (OptIncDec && (Disp.
getImm() == 1 || Disp.
getImm() == -1)) {
411 bool IsINC = Disp.
getImm() == 1;
448 if (AddrOffset >= 0) {
452 seekLEAFixup(p, I, MBB);
456 seekLEAFixup(q, I, MBB);
469 LLVM_DEBUG(
dbgs() <<
"FixLEA: Candidate to replace:"; MBI->dump(););
475 processInstruction(J, MBB);
493 !
TII->isSafeToClobberEFLAGS(MBB, I))
498 if ((SrcR1 == 0 || SrcR1 != DstR) && (SrcR2 == 0 || SrcR2 != DstR))
502 LLVM_DEBUG(
dbgs() <<
"FixLEA: Candidate to replace:"; I->dump(););
506 if (SrcR1 != 0 && SrcR2 != 0) {
514 if (Offset.
getImm() != 0) {
533 const unsigned LEAOpcode = MI.
getOpcode();
543 !
TII->isSafeToClobberEFLAGS(MBB, MI) ||
544 Segment.
getReg() != X86::NoRegister)
553 BaseReg =
TRI->getSubReg(BaseReg, X86::sub_32bit);
555 IndexReg =
TRI->getSubReg(IndexReg, X86::sub_32bit);
558 bool IsScale1 = Scale.
getImm() == 1;
564 if (IsInefficientBase && DestReg == BaseReg && !IsScale1)
576 if (IsScale1 && (DestReg == BaseReg || DestReg == IndexReg)) {
578 if (DestReg != BaseReg)
593 }
else if (!IsInefficientBase || (!IsInefficientIndex && IsScale1)) {
600 .add(IsInefficientBase ? Index : Base)
602 .add(IsInefficientBase ? Base : Index)
613 if (OptIncDec && Offset.
isImm() &&
635 assert(DestReg != BaseReg &&
"DestReg == BaseReg should be handled already!");
636 assert(IsInefficientBase &&
"efficient base should be handled already!");
639 if (LEAOpcode == X86::LEA64_32r)
644 bool BIK = Base.
isKill() && BaseReg != IndexReg;
const MachineInstrBuilder & add(const MachineOperand &MO) const
This class represents lattice values for constants.
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
const X86InstrInfo * getInstrInfo() const override
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Describe properties that are true of each instruction in the target description file.
static bool isInefficientLEAReg(unsigned Reg)
constexpr bool isInt< 8 >(int64_t x)
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
static bool hasInefficientLEABaseReg(const MachineOperand &Base, const MachineOperand &Index)
Returns true if this LEA uses base an index registers, and the base register is known to be inefficie...
void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, const DebugLoc &DL, MCRegister DestReg, MCRegister SrcReg, bool KillSrc) const override
Emit instructions to copy a pair of physical registers.
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Provide an instruction scheduling machine model to CodeGen passes.
const HexagonInstrInfo * TII
unsigned getNumOperands() const
Retuns the total number of operands.
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...
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
FunctionPass * createX86FixupLEAs()
Return a pass that selectively replaces certain instructions (like add, sub, inc, dec...
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
MachineInstrBundleIterator< MachineInstr > iterator
bool isConvertibleTo3Addr(QueryType Type=IgnoreBundle) const
Return true if this is a 2-address instruction which can be changed into a 3-address instruction if n...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool isPredecessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a predecessor of this block.
FunctionPass class - This class is used to implement most global optimizations.
unsigned getOperandBias(const MCInstrDesc &Desc)
Compute whether all of the def operands are repeated in the uses and therefore should be skipped...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static unsigned getADDriFromLEA(unsigned LEAOpcode, const MachineOperand &Offset)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
const X86RegisterInfo * getRegisterInfo() const override
Iterator for intrusive lists based on ilist_node.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
static uint64_t add(uint64_t LeftOp, uint64_t RightOp)
MachineOperand class - Representation of each machine instruction operand.
AddrSegmentReg - The operand # of the segment in the memory operand.
static unsigned getINCDECFromLEA(unsigned LEAOpcode, bool IsINC)
const Function & getFunction() const
Return the LLVM function that this machine code represents.
static unsigned getADDrrFromLEA(unsigned LEAOpcode)
static bool isLEA(unsigned Opcode)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
MachineFunctionProperties & set(Property P)
Representation of each machine instruction.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
static bool hasLEAOffset(const MachineOperand &Offset)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Properties which a MachineFunction may have at a given point in time.
Wrapper class representing virtual and physical registers.
int getMemoryOperandNo(uint64_t TSFlags)
The function returns the MCInst operand # for the first field of the memory operand.