34 #define FIXUPLEA_DESC "X86 LEA Fixup"
35 #define FIXUPLEA_NAME "x86-fixup-LEAs"
37 #define DEBUG_TYPE FIXUPLEA_NAME
39 STATISTIC(NumLEAs,
"Number of LEA instructions created");
43 enum RegUsageState { RU_NotUsed, RU_Write, RU_Read };
160 MFI->insert(MBBI, NewMI);
165 case X86::ADD64ri32_DB:
166 case X86::ADD64ri8_DB:
169 case X86::ADD32ri_DB:
170 case X86::ADD32ri8_DB:
173 case X86::ADD16ri_DB:
174 case X86::ADD16ri8_DB:
182 case X86::ADD16rr_DB:
190 return TII->convertToThreeAddress(MFI, MI,
nullptr);
204 if (!OptLEA && !OptIncDec)
209 DEBUG(
dbgs() <<
"Start X86FixupLEAs\n";);
212 processBasicBlock(Func,
I);
218 FixupLEAPass::RegUsageState
220 RegUsageState
RegUsage = RU_NotUsed;
240 if (I == MFI->begin()) {
241 if (MFI->isPredecessor(&*MFI)) {
254 int InstrDistance = 1;
256 static const int INSTR_DISTANCE_THRESHOLD = 5;
261 while (Found && I != CurInst) {
262 if (CurInst->isCall() || CurInst->isInlineAsm())
264 if (InstrDistance > INSTR_DISTANCE_THRESHOLD)
266 if (usesRegister(p, CurInst) == RU_Write) {
270 MF->getSubtarget().getInstrItineraryData(), *CurInst);
276 static inline bool isLEA(
const int Opcode) {
277 return Opcode == X86::LEA16r || Opcode == X86::LEA32r ||
278 Opcode == X86::LEA64r || Opcode == X86::LEA64_32r;
282 return Reg == X86::EBP || Reg == X86::RBP || Reg == X86::R13;
286 return Op.
isReg() && Op.
getReg() != X86::NoRegister;
327 return IsInt8 ? X86::ADD16ri8 : X86::ADD16ri;
330 return IsInt8 ? X86::ADD32ri8 : X86::ADD32ri;
332 return IsInt8 ? X86::ADD64ri8 : X86::ADD64ri32;
343 return SrcReg == DstReg &&
363 NewOpcode = isINC ? X86::INC16r : X86::DEC16r;
367 NewOpcode = isINC ? X86::INC32r : X86::DEC32r;
370 NewOpcode = isINC ? X86::INC64r : X86::DEC64r;
391 if (AddrOffset >= 0) {
395 seekLEAFixup(p, I, MFI);
399 seekLEAFixup(q, I, MFI);
412 DEBUG(
dbgs() <<
"FixLEA: Candidate to replace:"; MBI->dump(););
418 processInstruction(J, MFI);
430 !
TII->isSafeToClobberEFLAGS(*MFI, I))
435 if ((SrcR1 == 0 || SrcR1 != DstR) && (SrcR2 == 0 || SrcR2 != DstR))
439 DEBUG(
dbgs() <<
"FixLEA: Candidate to replace:"; I->dump(););
440 DEBUG(
dbgs() <<
"FixLEA: Replaced by: ";);
443 if (SrcR1 != 0 && SrcR2 != 0) {
448 DEBUG(NewMI->dump(););
458 DEBUG(NewMI->dump(););
467 FixupLEAPass::processInstrForSlow3OpLEA(
MachineInstr &MI,
471 if (!
isLEA(LEAOpcode))
483 !
TII->isSafeToClobberEFLAGS(*MFI, MI) ||
484 Segment.
getReg() != X86::NoRegister)
487 unsigned int DstR = Dst.
getReg();
488 unsigned int BaseR = Base.
getReg();
489 unsigned int IndexR = Index.
getReg();
492 bool IsScale1 = Scale.
getImm() == 1;
498 if (IsInefficientBase && SSDstR == BaseR && !IsScale1)
500 if (LEAOpcode == X86::LEA64_32r && IsInefficientBase &&
501 (IsInefficientIndex || !IsScale1))
508 DEBUG(
dbgs() <<
"FixLEA: Candidate to replace:"; MI.
dump(););
509 DEBUG(
dbgs() <<
"FixLEA: Replaced by: ";);
515 if (IsScale1 && (DstR == BaseR || DstR == IndexR)) {
531 if (!IsInefficientBase || (!IsInefficientIndex && IsScale1)) {
534 .add(IsInefficientBase ? Index : Base)
536 .add(IsInefficientBase ? Base : Index)
548 assert(SSDstR != BaseR &&
"SSDstR == BaseR should be handled already!");
549 assert(IsInefficientBase &&
"efficient base should be handled already!");
558 DEBUG(NewMI->dump(););
573 DEBUG(NewMI->dump(););
582 if (fixupIncDec(I, MFI))
587 processInstructionForSLM(I, MFI);
591 if (
auto *NewMI = processInstrForSlow3OpLEA(*I, MFI)) {
596 processInstruction(I, MFI);
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
static bool isRegOperand(const MachineOperand &Op)
STATISTIC(NumFunctions,"Total number of functions")
static bool isLEASimpleIncOrDec(MachineInstr &LEA)
isLEASimpleIncOrDec - Does this LEA have one these forms: lea reg, 1(reg) lea reg, -1(reg)
const X86InstrInfo * getInstrInfo() const override
Describe properties that are true of each instruction in the target description file.
MachineInstrBuilder MachineInstrBuilder &DefMI const MCInstrDesc & Desc
const MachineInstrBuilder & add(const MachineOperand &MO) const
static bool getPreviousInstr(MachineBasicBlock::iterator &I, MachineFunction::iterator MFI)
getPreviousInstr - Given a reference to an instruction in a basic block, return a reference to the pr...
constexpr bool isInt< 8 >(int64_t x)
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
static bool isLEA(const int Opcode)
static int getADDrrFromLEA(int LEAOpcode)
bool optForMinSize() const
Optimize this function for minimum size (-Oz).
static bool hasInefficientLEABaseReg(const MachineOperand &Base, const MachineOperand &Index)
hasIneffecientLEARegs - LEA that uses base and index registers where the base is EBP, RBP, or R13
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Reg
All possible values of the reg field in the ModR/M byte.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
unsigned getNumOperands() const
Access to explicit operands of the instruction.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
FunctionPass * createX86FixupLEAs()
Return a pass that selectively replaces certain instructions (like add, sub, inc, dec...
unsigned getInstrLatency(const InstrItineraryData *ItinData, const MachineInstr &MI, unsigned *PredCost=nullptr) const override
Compute the instruction latency of a given instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
static int getADDriFromLEA(int LEAOpcode, const MachineOperand &Offset)
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
MachineInstrBundleIterator< MachineInstr > iterator
static bool isInefficientLEAReg(unsigned int Reg)
const MachineOperand & getOperand(unsigned i) const
void initializeFixupLEAPassPass(PassRegistry &)
FunctionPass class - This class is used to implement most global optimizations.
unsigned getOperandBias(const MCInstrDesc &Desc)
getOperandBias - compute any additional adjustment needed to the offset to the start of the memory op...
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Iterator for intrusive lists based on ilist_node.
AddrSegmentReg - The operand # of the segment in the memory operand.
MachineOperand class - Representation of each machine instruction operand.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
unsigned getX86SubSuperRegister(unsigned, unsigned, bool High=false)
Returns the sub or super register of a specific X86 register.
static bool isThreeOperandsLEA(const MachineOperand &Base, const MachineOperand &Index, const MachineOperand &Offset)
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
llvm_unreachable("Unknown RegBank!")
void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, const DebugLoc &DL, unsigned DestReg, unsigned SrcReg, bool KillSrc) const override
Emit instructions to copy a pair of physical registers.
MachineFunctionProperties & set(Property P)
Representation of each machine instruction.
static bool hasLEAOffset(const MachineOperand &Offset)
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
const MachineInstrBuilder & addReg(unsigned 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.
int getMemoryOperandNo(uint64_t TSFlags)
getMemoryOperandNo - The function returns the MCInst operand # for the first field of the memory oper...