55#define DEBUG_TYPE "x86-optimize-leas"
59 cl::desc(
"X86: Disable LEA optimizations."),
62STATISTIC(NumSubstLEAs,
"Number of LEA instruction substitutions");
63STATISTIC(NumRedundantLEAs,
"Number of redundant LEA instructions removed");
90 Operands[3] = Segment;
95 for (
int i = 0; i < 4; ++i)
107 const MachineOperand *Operands[4];
110 const MachineOperand *Disp;
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]);
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");
216 "Address displacement operand is not valid");
232 unsigned Opcode =
MI.getOpcode();
233 return Opcode == X86::LEA16r || Opcode == X86::LEA32r ||
234 Opcode == X86::LEA64r || Opcode == X86::LEA64_32r;
239class X86OptimizeLEAsImpl {
241 bool runOnMachineFunction(MachineFunction &MF, ProfileSummaryInfo *PSI,
242 MachineBlockFrequencyInfo *MBFI);
245 using MemOpMap = DenseMap<MemOpKey, SmallVector<MachineInstr *, 16>>;
249 int calcInstrDist(
const MachineInstr &
First,
const MachineInstr &
Last);
255 bool chooseBestLEA(
const SmallVectorImpl<MachineInstr *> &
List,
256 const MachineInstr &
MI, MachineInstr *&BestLEA,
257 int64_t &AddrDispShift,
int &Dist);
262 int64_t getAddrDispShift(
const MachineInstr &MI1,
unsigned N1,
263 const MachineInstr &MI2,
unsigned N2)
const;
269 bool isReplaceable(
const MachineInstr &
First,
const MachineInstr &
Last,
270 int64_t &AddrDispShift)
const;
275 void findLEAs(
const MachineBasicBlock &
MBB, MemOpMap &LEAs);
278 bool removeRedundantAddrCalc(MemOpMap &LEAs);
283 MachineInstr *replaceDebugValue(MachineInstr &
MI,
Register OldReg,
284 Register NewReg, int64_t AddrDispShift);
287 bool removeRedundantLEAs(MemOpMap &LEAs);
289 DenseMap<const MachineInstr *, unsigned> InstrPos;
291 MachineRegisterInfo *MRI =
nullptr;
292 const X86InstrInfo *TII =
nullptr;
293 const X86RegisterInfo *TRI =
nullptr;
298 X86OptimizeLEAsLegacy() : MachineFunctionPass(ID) {}
300 StringRef getPassName()
const override {
return "X86 LEA Optimize"; }
305 bool runOnMachineFunction(MachineFunction &MF)
override;
309 void getAnalysisUsage(AnalysisUsage &AU)
const override {
311 AU.
addRequired<LazyMachineBlockFrequencyInfoPass>();
318char X86OptimizeLEAsLegacy::ID = 0;
321 return new X86OptimizeLEAsLegacy();
331 "Instructions are in different basic blocks");
333 "Instructions' positions are undefined");
335 return InstrPos[&
Last] - InstrPos[&
First];
347bool X86OptimizeLEAsImpl::chooseBestLEA(
349 MachineInstr *&BestLEA, int64_t &AddrDispShift,
int &Dist) {
350 const MCInstrDesc &
Desc =
MI.getDesc();
359 int64_t AddrDispShiftTemp = getAddrDispShift(
MI, MemOpNo, *
DefMI, 1);
378 int DistTemp = calcInstrDist(*
DefMI,
MI);
380 "The distance between two different instructions cannot be zero");
381 if (DistTemp > 0 || BestLEA ==
nullptr) {
384 if (BestLEA !=
nullptr && !
isInt<8>(AddrDispShiftTemp) &&
389 AddrDispShift = AddrDispShiftTemp;
398 return BestLEA !=
nullptr;
404int64_t X86OptimizeLEAsImpl::getAddrDispShift(
const MachineInstr &MI1,
406 const MachineInstr &MI2,
412 "Address displacement operands are not compatible");
429bool X86OptimizeLEAsImpl::isReplaceable(
const MachineInstr &
First,
430 const MachineInstr &
Last,
431 int64_t &AddrDispShift)
const {
433 "The function works only with LEA instructions");
439 if (
MRI->getRegClass(
First.getOperand(0).getReg()) !=
440 MRI->getRegClass(
Last.getOperand(0).getReg()))
444 AddrDispShift = getAddrDispShift(
Last, 1,
First, 1);
449 for (
auto &MO :
MRI->use_nodbg_operands(
Last.getOperand(0).getReg())) {
450 MachineInstr &
MI = *MO.getParent();
453 const MCInstrDesc &
Desc =
MI.getDesc();
470 for (
unsigned i = 0; i <
MI.getNumOperands(); i++)
485void X86OptimizeLEAsImpl::findLEAs(
const MachineBasicBlock &
MBB,
488 for (
auto &
MI :
MBB) {
494 InstrPos[&
MI] = Pos += 2;
504bool X86OptimizeLEAsImpl::removeRedundantAddrCalc(MemOpMap &LEAs) {
508 MachineBasicBlock *
MBB = (*LEAs.begin()->second.begin())->
getParent();
513 if (!
MI.mayLoadOrStore())
517 const MCInstrDesc &
Desc =
MI.getDesc();
528 if (Insns == LEAs.end())
533 int64_t AddrDispShift;
535 if (!chooseBestLEA(Insns->second,
MI,
DefMI, AddrDispShift, Dist))
547 InstrPos[
DefMI] = InstrPos[&
MI] - 1;
554 "Instruction positioning is broken");
568 .ChangeToRegister(X86::NoRegister,
false);
569 MI.getOperand(MemOpNo +
X86::AddrDisp).ChangeToImmediate(AddrDispShift);
571 .ChangeToRegister(X86::NoRegister,
false);
581MachineInstr *X86OptimizeLEAsImpl::replaceDebugValue(MachineInstr &
MI,
584 int64_t AddrDispShift) {
585 const DIExpression *Expr =
MI.getDebugExpression();
586 if (AddrDispShift != 0) {
587 if (
MI.isNonListDebugValue()) {
595 for (MachineOperand &
Op :
MI.getDebugOperandsForReg(OldReg)) {
596 unsigned OpIdx =
MI.getDebugOperandIndex(&
Op);
603 MachineBasicBlock *
MBB =
MI.getParent();
605 bool IsIndirect =
MI.isIndirectDebugValue();
606 const MDNode *Var =
MI.getDebugVariable();
607 unsigned Opcode =
MI.isNonListDebugValue() ? TargetOpcode::DBG_VALUE
608 : TargetOpcode::DBG_VALUE_LIST;
610 assert(
MI.getDebugOffset().getImm() == 0 &&
611 "DBG_VALUE with nonzero offset");
615 auto replaceOldReg = [OldReg, NewReg](
const MachineOperand &
Op) {
616 if (
Op.isReg() &&
Op.getReg() == OldReg)
618 false,
false,
false,
false,
false,
622 for (
const MachineOperand &
Op :
MI.debug_operands())
629bool X86OptimizeLEAsImpl::removeRedundantLEAs(MemOpMap &LEAs) {
633 for (
auto &
E : LEAs) {
634 auto &
List =
E.second;
638 while (I1 !=
List.end()) {
640 auto I2 = std::next(I1);
641 while (I2 !=
List.end()) {
642 MachineInstr &
Last = **I2;
643 int64_t AddrDispShift;
648 "LEAs must be in occurrence order in the list");
651 if (!isReplaceable(
First,
Last, AddrDispShift)) {
665 while (!
MRI->use_empty(LastVReg)) {
666 MachineOperand &MO = *
MRI->use_begin(LastVReg);
669 if (
MI.isDebugValue()) {
673 replaceDebugValue(
MI, LastVReg, FirstVReg, AddrDispShift);
678 const MCInstrDesc &
Desc =
MI.getDesc();
689 Op.setImm(
Op.getImm() + AddrDispShift);
690 else if (!
Op.isJTI())
691 Op.setOffset(
Op.getOffset() + AddrDispShift);
695 MRI->clearKillFlags(FirstVReg);
704 "The LEA's def register must have no uses");
705 Last.eraseFromParent();
719bool X86OptimizeLEAsImpl::runOnMachineFunction(
720 MachineFunction &MF, ProfileSummaryInfo *PSI,
721 MachineBlockFrequencyInfo *MBFI) {
732 for (
auto &
MBB : MF) {
744 Changed |= removeRedundantLEAs(LEAs);
749 Changed |= removeRedundantAddrCalc(LEAs);
755bool X86OptimizeLEAsLegacy::runOnMachineFunction(MachineFunction &MF) {
758 ProfileSummaryInfo *PSI =
759 &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
760 MachineBlockFrequencyInfo *MBFI =
762 ? &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI()
764 X86OptimizeLEAsImpl PassImpl;
765 return PassImpl.runOnMachineFunction(MF, PSI, MBFI);
773 .getCachedResult<ProfileSummaryAnalysis>(
779 X86OptimizeLEAsImpl PassImpl;
780 bool Changed = PassImpl.runOnMachineFunction(MF, PSI, MBFI);
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines DenseMapInfo traits for DenseMap.
This file defines the DenseMap class.
const HexagonInstrInfo * TII
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
===- LazyMachineBlockFrequencyInfo.h - Lazy Block Frequency -*- C++ -*–===//
Register const TargetRegisterInfo * TRI
Promote Memory to Register
MachineInstr unsigned OpIdx
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static bool isLEA(unsigned Opcode)
static cl::opt< bool > DisableX86LEAOpt("disable-x86-lea-opt", cl::Hidden, cl::desc("X86: Disable LEA optimizations."), cl::init(false))
static bool isLEA(const MachineInstr &MI)
Returns true if the instruction is LEA.
static bool isValidDispOp(const MachineOperand &MO)
static MemOpKey getMemOpKey(const MachineInstr &MI, unsigned N)
Returns a hash table key based on memory operands of MI.
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/...
static bool isIdenticalOp(const MachineOperand &MO1, const MachineOperand &MO2)
Returns true if two machine operands are identical and they are not physical registers.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
Represents analyses that only rely on functions' control flow.
static LLVM_ABI void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
static LLVM_ABI DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
static LLVM_ABI 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 ...
FunctionPass class - This class is used to implement most global optimizations.
Module * getParent()
Get the module that this global value is contained inside of...
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
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
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
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.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
LLVM_ABI MachineInstr * removeFromParent()
Unlink 'this' from the containing basic block, and return it without deleting it.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineBasicBlock * getMBB() const
bool isCPI() const
isCPI - Tests if this is a MO_ConstantPoolIndex operand.
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
const BlockAddress * getBlockAddress() const
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
const char * getSymbolName() const
bool isBlockAddress() const
isBlockAddress - Tests if this is a MO_BlockAddress operand.
Register getReg() const
getReg - Returns the register number.
LLVM_ABI bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
MCSymbol * getMCSymbol() const
@ MO_Immediate
Immediate operand.
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
@ MO_MCSymbol
MCSymbol reference (for debug/eh info)
@ MO_GlobalAddress
Address of a global value.
@ MO_BlockAddress
Address of a basic block.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_ExternalSymbol
Name of external global symbol.
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
int64_t getOffset() const
Return the offset from the symbol in this operand.
bool isMBB() const
isMBB - Tests if this is a MO_MachineBasicBlock operand.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Analysis providing profile information.
bool hasProfileSummary() const
Returns true if profile summary is available.
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
An opaque object representing a hash code.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
OuterAnalysisManagerProxy< ModuleAnalysisManager, MachineFunction > ModuleAnalysisManagerMachineFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
FunctionPass * createX86OptimizeLEAsLegacyPass()
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
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.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
DWARFExpression::Operation Op
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
static unsigned getHashValue(const MemOpKey &Val)
static MemOpKey getEmptyKey()
DenseMapInfo< const MachineOperand * > PtrInfo
static bool isEqual(const MemOpKey &LHS, const MemOpKey &RHS)
static MemOpKey getTombstoneKey()
An information struct used to provide DenseMap with the various necessary components for a given valu...