55 #define DEBUG_TYPE "x86-optimize-LEAs" 59 cl::desc(
"X86: Disable LEA optimizations."),
62 STATISTIC(NumSubstLEAs,
"Number of LEA instruction substitutions");
63 STATISTIC(NumRedundantLEAs,
"Number of redundant LEA instructions removed");
95 for (
int i = 0; i < 4; ++i)
122 return MemOpKey(PtrInfo::getEmptyKey(), PtrInfo::getEmptyKey(),
123 PtrInfo::getEmptyKey(), PtrInfo::getEmptyKey(),
124 PtrInfo::getEmptyKey());
128 return MemOpKey(PtrInfo::getTombstoneKey(), PtrInfo::getTombstoneKey(),
129 PtrInfo::getTombstoneKey(), PtrInfo::getTombstoneKey(),
130 PtrInfo::getTombstoneKey());
136 assert(Val.Disp != PtrInfo::getEmptyKey() &&
"Cannot hash the empty key");
137 assert(Val.Disp != PtrInfo::getTombstoneKey() &&
138 "Cannot hash the tombstone key");
141 *Val.Operands[2], *Val.Operands[3]);
147 switch (Val.Disp->getType()) {
173 return (
unsigned)Hash;
176 static bool isEqual(
const MemOpKey &LHS,
const MemOpKey &RHS) {
179 if (RHS.Disp == PtrInfo::getEmptyKey())
180 return LHS.Disp == PtrInfo::getEmptyKey();
181 if (RHS.Disp == PtrInfo::getTombstoneKey())
182 return LHS.Disp == PtrInfo::getTombstoneKey();
193 "The instruction must be a LEA, a load or a store");
217 "Address displacement operand is not valid");
234 return Opcode == X86::LEA16r || Opcode == X86::LEA32r ||
235 Opcode == X86::LEA64r || Opcode == X86::LEA64_32r;
244 StringRef getPassName()
const override {
return "X86 LEA Optimize"; }
272 int64_t &AddrDispShift,
int &Dist);
277 int64_t getAddrDispShift(
const MachineInstr &MI1,
unsigned N1,
285 int64_t &AddrDispShift)
const;
293 bool removeRedundantAddrCalc(MemOpMap &LEAs);
299 int64_t AddrDispShift);
302 bool removeRedundantLEAs(MemOpMap &LEAs);
323 assert(Last.getParent() == First.getParent() &&
324 "Instructions are in different basic blocks");
325 assert(InstrPos.find(&First) != InstrPos.end() &&
326 InstrPos.find(&Last) != InstrPos.end() &&
327 "Instructions' positions are undefined");
329 return InstrPos[&Last] - InstrPos[&First];
341 bool X86OptimizeLEAPass::chooseBestLEA(
343 MachineInstr *&BestLEA, int64_t &AddrDispShift,
int &Dist) {
352 for (
auto DefMI : List) {
354 int64_t AddrDispShiftTemp = getAddrDispShift(MI, MemOpNo, *
DefMI, 1);
373 int DistTemp = calcInstrDist(*
DefMI, MI);
375 "The distance between two different instructions cannot be zero");
376 if (DistTemp > 0 || BestLEA ==
nullptr) {
379 if (BestLEA !=
nullptr && !
isInt<8>(AddrDispShiftTemp) &&
384 AddrDispShift = AddrDispShiftTemp;
393 return BestLEA !=
nullptr;
399 int64_t X86OptimizeLEAPass::getAddrDispShift(
const MachineInstr &MI1,
407 "Address displacement operands are not compatible");
424 bool X86OptimizeLEAPass::isReplaceable(
const MachineInstr &First,
426 int64_t &AddrDispShift)
const {
428 "The function works only with LEA instructions");
439 AddrDispShift = getAddrDispShift(Last, 1, First, 1);
483 for (
auto &MI : MBB) {
489 InstrPos[&
MI] = Pos += 2;
492 LEAs[
getMemOpKey(MI, 1)].push_back(const_cast<MachineInstr *>(&MI));
499 bool X86OptimizeLEAPass::removeRedundantAddrCalc(MemOpMap &LEAs) {
500 bool Changed =
false;
506 for (
auto I = MBB->begin(),
E = MBB->end();
I !=
E;) {
525 if (Insns == LEAs.end())
530 int64_t AddrDispShift;
532 if (!chooseBestLEA(Insns->second, MI, DefMI, AddrDispShift, Dist))
544 InstrPos[
DefMI] = InstrPos[&
MI] - 1;
547 assert(((InstrPos[DefMI] == 1 &&
551 "Instruction positioning is broken");
580 int64_t AddrDispShift) {
582 if (AddrDispShift != 0)
592 return BuildMI(*MBB, MBB->
erase(&MI), DL,
TII->get(TargetOpcode::DBG_VALUE),
593 IsIndirect, VReg, Var, Expr);
597 bool X86OptimizeLEAPass::removeRedundantLEAs(MemOpMap &LEAs) {
598 bool Changed =
false;
601 for (
auto &
E : LEAs) {
602 auto &List =
E.second;
605 auto I1 = List.
begin();
606 while (I1 != List.
end()) {
608 auto I2 = std::next(I1);
609 while (I2 != List.
end()) {
611 int64_t AddrDispShift;
615 assert(calcInstrDist(First, Last) > 0 &&
616 "LEAs must be in occurrence order in the list");
619 if (!isReplaceable(First, Last, AddrDispShift)) {
629 for (
auto UI =
MRI->use_begin(LastVReg), UE =
MRI->use_end();
638 replaceDebugValue(MI, FirstVReg, AddrDispShift);
655 else if (!Op.
isJTI())
660 MRI->clearKillFlags(FirstVReg);
669 "The LEA's def register must have no uses");
685 bool Changed =
false;
694 &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
695 auto *MBFI = (PSI && PSI->hasProfileSummary()) ?
696 &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() :
700 for (
auto &MBB : MF) {
712 Changed |= removeRedundantLEAs(LEAs);
716 bool OptForSize = MF.getFunction().hasOptSize() ||
719 Changed |= removeRedundantAddrCalc(LEAs);
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
FunctionPass * createX86OptimizeLEAs()
Return a pass that removes redundant LEA instructions and redundant address recalculations.
MachineBasicBlock * getMBB() const
This class represents lattice values for constants.
static unsigned getHashValue(const MemOpKey &Val)
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
Describe properties that are true of each instruction in the target description file.
Address of indexed Jump Table for switch.
constexpr bool isInt< 8 >(int64_t x)
bool mayLoadOrStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read or modify memory.
MachineBasicBlock reference.
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
bool isCPI() const
isCPI - Tests if this is a MO_ConstantPoolIndex operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
static DIExpression * prepend(const DIExpression *Expr, uint8_t Flags, int64_t Offset=0)
Prepend DIExpr with a deref and offset operation and optionally turn it into a stack value or/and an ...
AnalysisUsage & addRequired()
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
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
unsigned getNumOperands() const
Retuns the total number of operands.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
Name of external global symbol.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const char * getSymbolName() const
mir Rename Register Operands
INITIALIZE_PASS(X86OptimizeLEAPass, DEBUG_TYPE, "X86 optimize LEA pass", false, false) int X86OptimizeLEAPass
===- LazyMachineBlockFrequencyInfo.h - Lazy Block Frequency -*- C++ -*–===//
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
void setReg(Register Reg)
Change the register this operand corresponds to.
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/...
void ChangeToImmediate(int64_t ImmVal)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value...
bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
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
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static MemOpKey getEmptyKey()
const GlobalValue * getGlobal() const
This is an alternative analysis pass to MachineBlockFrequencyInfo.
Represent the analysis usage information of a pass.
Address of a basic block.
void setImm(int64_t immVal)
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...
void setOffset(int64_t Offset)
iterator erase(const_iterator CI)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const DIExpression * getDebugExpression() const
Return the complex address expression referenced by this DBG_VALUE instruction.
static bool isValidDispOp(const MachineOperand &MO)
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
constexpr bool isInt< 32 >(int64_t x)
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
static bool isLEA(const MachineInstr &MI)
Returns true if the instruction is LEA.
bool isDebugValue() const
MachineOperand class - Representation of each machine instruction operand.
AddrSegmentReg - The operand # of the segment in the memory operand.
MachineInstrBuilder MachineInstrBuilder & DefMI
bool isBlockAddress() const
isBlockAddress - Tests if this is a MO_BlockAddress operand.
const Function & getFunction() const
Return the LLVM function that this machine code represents.
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.
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...
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 MachineBasicBlock * getParent() const
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.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
int64_t getOffset() const
Return the offset from the symbol in this operand.
const BlockAddress * getBlockAddress() const
void ChangeToRegister(Register 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...
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
MachineInstr * removeFromParent()
Unlink 'this' from the containing basic block, and return it without deleting it. ...
bool isReg() const
isReg - Tests if this is a MO_Register operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MCSymbol * getMCSymbol() const
static MemOpKey getMemOpKey(const MachineInstr &MI, unsigned N)
Returns a hash table key based on memory operands of MI.
const DILocalVariable * getDebugVariable() const
Return the debug variable referenced by this DBG_VALUE instruction.
static const Function * getParent(const Value *V)
StringRef - Represent a constant reference to a string, i.e.
bool isMBB() const
isMBB - Tests if this is a MO_MachineBasicBlock operand.
Address of indexed Constant in Constant Pool.
bool operator==(uint64_t V1, const APInt &V2)
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
bool isIndirectDebugValue() const
A DBG_VALUE is indirect iff the first operand is a register and the second operand is an immediate...
Wrapper class representing virtual and physical registers.
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)
The function returns the MCInst operand # for the first field of the memory operand.
static MemOpKey getTombstoneKey()