39 #define DEBUG_TYPE "x86-optimize-LEAs"
43 cl::desc(
"X86: Disable LEA optimizations."),
46 STATISTIC(NumSubstLEAs,
"Number of LEA instruction substitutions");
47 STATISTIC(NumRedundantLEAs,
"Number of redundant LEA instructions removed");
73 Operands[3] = Segment;
78 for (
int i = 0; i < 4; ++i)
103 return MemOpKey(PtrInfo::getEmptyKey(), PtrInfo::getEmptyKey(),
104 PtrInfo::getEmptyKey(), PtrInfo::getEmptyKey(),
105 PtrInfo::getEmptyKey());
109 return MemOpKey(PtrInfo::getTombstoneKey(), PtrInfo::getTombstoneKey(),
110 PtrInfo::getTombstoneKey(), PtrInfo::getTombstoneKey(),
111 PtrInfo::getTombstoneKey());
117 assert(Val.Disp != PtrInfo::getEmptyKey() &&
"Cannot hash the empty key");
118 assert(Val.Disp != PtrInfo::getTombstoneKey() &&
119 "Cannot hash the tombstone key");
122 *Val.Operands[2], *Val.Operands[3]);
128 switch (Val.Disp->getType()) {
154 return (
unsigned)
Hash;
157 static bool isEqual(
const MemOpKey &LHS,
const MemOpKey &RHS) {
160 if (RHS.Disp == PtrInfo::getEmptyKey())
161 return LHS.Disp == PtrInfo::getEmptyKey();
162 if (RHS.Disp == PtrInfo::getTombstoneKey())
163 return LHS.Disp == PtrInfo::getTombstoneKey();
173 "The instruction must be a LEA, a load or a store");
198 "Address displacement operand is not valid");
215 return Opcode == X86::LEA16r || Opcode == X86::LEA32r ||
216 Opcode == X86::LEA64r || Opcode == X86::LEA64_32r;
224 StringRef getPassName()
const override {
return "X86 LEA Optimize"; }
244 int64_t &AddrDispShift,
int &Dist);
249 int64_t getAddrDispShift(
const MachineInstr &MI1,
unsigned N1,
257 int64_t &AddrDispShift)
const;
265 bool removeRedundantAddrCalc(MemOpMap &LEAs);
271 int64_t AddrDispShift);
274 bool removeRedundantLEAs(MemOpMap &LEAs);
289 int OptimizeLEAPass::calcInstrDist(
const MachineInstr &First,
294 "Instructions are in different basic blocks");
295 assert(InstrPos.find(&First) != InstrPos.end() &&
296 InstrPos.find(&Last) != InstrPos.end() &&
297 "Instructions' positions are undefined");
299 return InstrPos[&Last] - InstrPos[&First];
314 int64_t &AddrDispShift,
int &Dist) {
323 for (
auto DefMI : List) {
325 int64_t AddrDispShiftTemp = getAddrDispShift(MI, MemOpNo, *DefMI, 1);
337 MRI->getRegClass(DefMI->getOperand(0).getReg()))
344 int DistTemp = calcInstrDist(*DefMI, MI);
346 "The distance between two different instructions cannot be zero");
347 if (DistTemp > 0 || BestLEA ==
nullptr) {
350 if (BestLEA !=
nullptr && !
isInt<8>(AddrDispShiftTemp) &&
355 AddrDispShift = AddrDispShiftTemp;
364 return BestLEA !=
nullptr;
370 int64_t OptimizeLEAPass::getAddrDispShift(
const MachineInstr &MI1,
unsigned N1,
377 "Address displacement operands are not compatible");
394 bool OptimizeLEAPass::isReplaceable(
const MachineInstr &First,
396 int64_t &AddrDispShift)
const {
398 "The function works only with LEA instructions");
409 AddrDispShift = getAddrDispShift(Last, 1, First, 1);
452 for (
auto &MI : MBB) {
458 InstrPos[&
MI] = Pos += 2;
461 LEAs[
getMemOpKey(MI, 1)].push_back(const_cast<MachineInstr *>(&MI));
468 bool OptimizeLEAPass::removeRedundantAddrCalc(MemOpMap &LEAs) {
469 bool Changed =
false;
475 for (
auto I = MBB->begin(),
E = MBB->end();
I !=
E;) {
494 int64_t AddrDispShift;
496 if (!chooseBestLEA(LEAs[
getMemOpKey(MI, MemOpNo)], MI, DefMI, AddrDispShift,
509 InstrPos[DefMI] = InstrPos[&
MI] - 1;
512 assert(((InstrPos[DefMI] == 1 &&
516 "Instruction positioning is broken");
523 DEBUG(
dbgs() <<
"OptimizeLEAs: Candidate to replace: "; MI.
dump(););
545 int64_t AddrDispShift) {
548 if (AddrDispShift != 0)
559 IsIndirect, VReg,
Offset, Var, Expr);
563 bool OptimizeLEAPass::removeRedundantLEAs(MemOpMap &LEAs) {
564 bool Changed =
false;
567 for (
auto &
E : LEAs) {
568 auto &List =
E.second;
571 auto I1 = List.begin();
572 while (I1 != List.end()) {
574 auto I2 = std::next(I1);
575 while (I2 != List.end()) {
577 int64_t AddrDispShift;
581 assert(calcInstrDist(First, Last) > 0 &&
582 "LEAs must be in occurrence order in the list");
585 if (!isReplaceable(First, Last, AddrDispShift)) {
595 for (
auto UI =
MRI->use_begin(LastVReg), UE =
MRI->use_end();
604 replaceDebugValue(MI, FirstVReg, AddrDispShift);
621 else if (!Op.
isJTI())
626 MRI->clearKillFlags(FirstVReg);
629 DEBUG(
dbgs() <<
"OptimizeLEAs: Remove redundant LEA: "; Last.
dump(););
634 "The LEA's def register must have no uses");
650 bool Changed =
false;
660 for (
auto &MBB : MF) {
672 Changed |= removeRedundantLEAs(LEAs);
676 if (MF.getFunction()->optForSize())
677 Changed |= removeRedundantAddrCalc(LEAs);
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
const GlobalValue * getGlobal() const
FunctionPass * createX86OptimizeLEAs()
Return a pass that removes redundant LEA instructions and redundant address recalculations.
STATISTIC(NumFunctions,"Total number of functions")
void ChangeToRegister(unsigned Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isDebug=false)
ChangeToRegister - Replace this operand with a new register operand of the specified value...
MachineBasicBlock * getMBB() const
static unsigned getHashValue(const MemOpKey &Val)
Describe properties that are true of each instruction in the target description file.
MachineInstrBuilder MachineInstrBuilder &DefMI const MCInstrDesc & Desc
Address of indexed Jump Table for switch.
constexpr bool isInt< 8 >(int64_t x)
bool isBlockAddress() const
isBlockAddress - Tests if this is a MO_BlockAddress operand.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
MachineBasicBlock reference.
const char * getSymbolName() const
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
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...
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.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
Name of external global symbol.
unsigned getNumOperands() const
Access to explicit operands of the instruction.
bool isCPI() const
isCPI - Tests if this is a MO_ConstantPoolIndex operand.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
static bool isSimilarDispOp(const MachineOperand &MO1, const MachineOperand &MO2)
Returns true if two address displacement operands are of the same type and use the same symbol/index/...
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
void ChangeToImmediate(int64_t ImmVal)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value...
const MachineBasicBlock * getParent() const
bool isDebugValue() const
static bool isEqual(const MemOpKey &LHS, const MemOpKey &RHS)
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Address of a global value.
initializer< Ty > init(const Ty &Val)
unsigned const MachineRegisterInfo * MRI
bool isIndirectDebugValue() const
A DBG_VALUE is indirect iff the first operand is a register and the second operand is an immediate...
static MemOpKey getEmptyKey()
const MachineOperand & getOperand(unsigned i) const
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
Address of a basic block.
void setImm(int64_t immVal)
FunctionPass class - This class is used to implement most global optimizations.
int64_t getOffset() const
Return the offset from the symbol in this operand.
unsigned getOperandBias(const MCInstrDesc &Desc)
getOperandBias - compute any additional adjustment needed to the offset to the start of the memory op...
const DIExpression * getDebugExpression() const
Return the complex address expression referenced by this DBG_VALUE instruction.
void setOffset(int64_t Offset)
static bool isValidDispOp(const MachineOperand &MO)
constexpr bool isInt< 32 >(int64_t x)
static bool isLEA(const MachineInstr &MI)
Returns true if the instruction is LEA.
AddrSegmentReg - The operand # of the segment in the memory operand.
MachineOperand class - Representation of each machine instruction operand.
DenseMapInfo< const MachineOperand * > PtrInfo
bool isMBB() const
isMBB - Tests if this is a MO_MachineBasicBlock operand.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
MCSymbol reference (for debug/eh info)
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
An opaque object representing a hash code.
static cl::opt< bool > DisableX86LEAOpt("disable-x86-lea-opt", cl::Hidden, cl::desc("X86: Disable LEA optimizations."), cl::init(false))
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
llvm_unreachable("Unknown RegBank!")
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
bool mayLoadOrStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read or modify memory.
void setReg(unsigned Reg)
Change the register this operand corresponds to.
MCSymbol * getMCSymbol() const
MachineInstr * removeFromParent()
Unlink 'this' from the containing basic block, and return it without deleting it. ...
const DILocalVariable * getDebugVariable() const
Return the debug variable referenced by this DBG_VALUE instruction.
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static MemOpKey getMemOpKey(const MachineInstr &MI, unsigned N)
Returns a hash table key based on memory operands of MI.
static const Function * getParent(const Value *V)
static DIExpression * prepend(const DIExpression *DIExpr, bool Deref, int64_t Offset=0, bool StackValue=false)
Prepend DIExpr with a deref and offset operation and optionally turn it into a stack value...
std::string Hash(const Unit &U)
StringRef - Represent a constant reference to a string, i.e.
const BlockAddress * getBlockAddress() const
Address of indexed Constant in Constant Pool.
bool operator==(uint64_t V1, const APInt &V2)
bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
static bool isIdenticalOp(const MachineOperand &MO1, const MachineOperand &MO2)
Returns true if two machine operands are identical and they are not physical registers.
int getMemoryOperandNo(uint64_t TSFlags)
getMemoryOperandNo - The function returns the MCInst operand # for the first field of the memory oper...
static MemOpKey getTombstoneKey()