51#define DEBUG_TYPE "machine-cse"
53STATISTIC(NumCoalesces,
"Number of copies coalesced");
54STATISTIC(NumCSEs,
"Number of common subexpression eliminated");
55STATISTIC(NumPREs,
"Number of partial redundant expression"
56 " transformed to fully redundant");
58 "Number of physreg referencing common subexpr eliminated");
60 "Number of cross-MBB physreg referencing CS eliminated");
61STATISTIC(NumCommutes,
"Number of copies coalesced after commuting");
66 cl::desc(
"Threshold for the size of CSUses"));
70 cl::desc(
"Override the profitability heuristics for Machine CSE"));
83 : DT(DT), MBFI(MBFI) {}
84 bool run(MachineFunction &MF);
89 ScopedHashTableVal<MachineInstr *, unsigned>>;
91 ScopedHashTable<MachineInstr *, unsigned, MachineInstrExpressionTrait,
96 unsigned LookAheadLimit = 0;
97 DenseMap<MachineBasicBlock *, ScopeType *> ScopeMap;
98 DenseMap<MachineInstr *, MachineBasicBlock *, MachineInstrExpressionTrait>
104 bool PerformTrivialCopyPropagation(MachineInstr *
MI, MachineBasicBlock *
MBB);
105 bool isPhysDefTriviallyDead(MCRegister
Reg,
108 bool hasLivePhysRegDefUses(
const MachineInstr *
MI,
109 const MachineBasicBlock *
MBB,
110 SmallSet<MCRegister, 8> &PhysRefs,
111 PhysDefVector &PhysDefs,
bool &PhysUseDef)
const;
112 bool PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *
MI,
113 const SmallSet<MCRegister, 8> &PhysRefs,
114 const PhysDefVector &PhysDefs,
bool &NonLocal)
const;
115 bool isCSECandidate(MachineInstr *
MI);
118 void EnterScope(MachineBasicBlock *
MBB);
119 void ExitScope(MachineBasicBlock *
MBB);
120 bool ProcessBlockCSE(MachineBasicBlock *
MBB);
122 DenseMap<MachineDomTreeNode *, unsigned> &OpenChildren);
125 bool isPRECandidate(MachineInstr *
MI, SmallSet<MCRegister, 8> &PhysRefs);
126 bool ProcessBlockPRE(MachineDominatorTree *MDT, MachineBasicBlock *
MBB);
127 bool PerformSimplePRE(MachineDominatorTree *DT);
130 bool isProfitableToHoistInto(MachineBasicBlock *CandidateBB,
131 MachineBasicBlock *
MBB, MachineBasicBlock *MBB1);
132 void releaseMemory();
139 MachineCSELegacy() : MachineFunctionPass(ID) {}
141 bool runOnMachineFunction(MachineFunction &MF)
override;
143 void getAnalysisUsage(AnalysisUsage &AU)
const override {
149 AU.
addRequired<MachineBlockFrequencyInfoWrapperPass>();
150 AU.
addPreserved<MachineBlockFrequencyInfoWrapperPass>();
153 MachineFunctionProperties getRequiredProperties()
const override {
154 return MachineFunctionProperties().setIsSSA();
159char MachineCSELegacy::ID = 0;
164 "Machine Common Subexpression Elimination",
false,
false)
173bool MachineCSEImpl::PerformTrivialCopyPropagation(
MachineInstr *
MI,
177 Register Reg = MO.getReg();
178 if (!Reg.isVirtual())
180 bool OnlyOneUse = MRI->hasOneNonDBGUse(Reg);
181 MachineInstr *DefMI = MRI->getVRegDef(Reg);
182 if (!DefMI || !DefMI->isCopy())
184 Register SrcReg = DefMI->getOperand(1).getReg();
185 if (!SrcReg.isVirtual())
199 if (DefMI->getOperand(1).getSubReg())
201 if (!MRI->constrainRegAttrs(SrcReg, Reg))
203 LLVM_DEBUG(dbgs() <<
"Coalescing: " << *DefMI);
204 LLVM_DEBUG(dbgs() <<
"*** to: " << *MI);
208 MRI->clearKillFlags(SrcReg);
214 DefMI->changeDebugValuesDefReg(SrcReg);
216 DefMI->eraseFromParent();
225bool MachineCSEImpl::isPhysDefTriviallyDead(
228 unsigned LookAheadLeft = LookAheadLimit;
229 while (LookAheadLeft) {
237 bool SeenDef =
false;
238 for (
const MachineOperand &MO :
I->operands()) {
239 if (MO.isRegMask() && MO.clobbersPhysReg(
Reg))
241 if (!MO.isReg() || !MO.getReg())
243 if (!
TRI->regsOverlap(MO.getReg(),
Reg))
274 return TRI.isCallerPreservedPhysReg(
Reg, MF) ||
TII.isIgnorableUse(MO) ||
275 (
MRI.reservedRegsFrozen() &&
MRI.isConstantPhysReg(
Reg));
282bool MachineCSEImpl::hasLivePhysRegDefUses(
const MachineInstr *
MI,
283 const MachineBasicBlock *
MBB,
284 SmallSet<MCRegister, 8> &PhysRefs,
285 PhysDefVector &PhysDefs,
286 bool &PhysUseDef)
const {
288 for (
const MachineOperand &MO :
MI->all_uses()) {
297 for (MCRegAliasIterator AI(
Reg,
TRI,
true); AI.isValid(); ++AI)
306 const MachineOperand &MO = MOP.value();
321 PhysDefs.emplace_back(MOP.index(),
Reg);
325 for (
const auto &Def : PhysDefs)
326 for (MCRegAliasIterator AI(
Def.second,
TRI,
true); AI.isValid(); ++AI)
329 return !PhysRefs.
empty();
332bool MachineCSEImpl::PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *
MI,
333 const SmallSet<MCRegister, 8> &PhysRefs,
334 const PhysDefVector &PhysDefs,
335 bool &NonLocal)
const {
339 const MachineBasicBlock *
MBB =
MI->getParent();
340 const MachineBasicBlock *CSMBB = CSMI->
getParent();
342 bool CrossMBB =
false;
347 for (
const auto &PhysDef : PhysDefs) {
348 if (
MRI->isAllocatable(PhysDef.second) ||
MRI->isReserved(PhysDef.second))
358 unsigned LookAheadLeft = LookAheadLimit;
359 while (LookAheadLeft) {
361 while (
I !=
E &&
I != EE &&
I->isDebugInstr())
365 assert(CrossMBB &&
"Reaching end-of-MBB without finding MI?");
377 for (
const MachineOperand &MO :
I->operands()) {
398bool MachineCSEImpl::isCSECandidate(MachineInstr *
MI) {
399 if (
MI->isPosition() ||
MI->isPHI() ||
MI->isImplicitDef() ||
MI->isKill() ||
400 MI->isInlineAsm() ||
MI->isDebugInstr() ||
MI->isJumpTableDebugInfo() ||
405 if (
MI->isCopyLike())
409 if (
MI->mayStore() ||
MI->isCall() ||
MI->isTerminator() ||
410 MI->mayRaiseFPException() ||
MI->hasUnmodeledSideEffects())
417 if (!
MI->isDereferenceableInvariantLoad())
426 if (
MI->getOpcode() == TargetOpcode::LOAD_STACK_GUARD)
436 MachineBasicBlock *CSBB,
445 bool MayIncreasePressure =
true;
447 MayIncreasePressure =
false;
448 SmallPtrSet<MachineInstr*, 8> CSUses;
450 for (MachineInstr &
MI :
MRI->use_nodbg_instructions(CSReg)) {
455 MayIncreasePressure =
true;
459 if (!MayIncreasePressure)
460 for (MachineInstr &
MI :
MRI->use_nodbg_instructions(
Reg)) {
462 MayIncreasePressure =
true;
467 if (!MayIncreasePressure)
return true;
473 MachineBasicBlock *BB =
MI->getParent();
480 bool HasVRegUse =
false;
481 for (
const MachineOperand &MO :
MI->all_uses()) {
488 bool HasNonCopyUse =
false;
489 for (MachineInstr &
MI :
MRI->use_nodbg_instructions(
Reg)) {
491 if (!
MI.isCopyLike()) {
492 HasNonCopyUse =
true;
503 for (MachineInstr &
UseMI :
MRI->use_nodbg_instructions(CSReg)) {
504 HasPHI |=
UseMI.isPHI();
505 if (
UseMI.getParent() ==
MI->getParent())
512void MachineCSEImpl::EnterScope(MachineBasicBlock *
MBB) {
514 ScopeType *
Scope =
new ScopeType(VNT);
518void MachineCSEImpl::ExitScope(MachineBasicBlock *
MBB) {
520 DenseMap<MachineBasicBlock*, ScopeType*>::iterator
SI = ScopeMap.find(
MBB);
521 assert(SI != ScopeMap.end());
526bool MachineCSEImpl::ProcessBlockCSE(MachineBasicBlock *
MBB) {
530 SmallVector<unsigned, 2> ImplicitDefsToUpdate;
533 if (!isCSECandidate(&
MI))
536 bool FoundCSE = VNT.
count(&
MI);
539 if (PerformTrivialCopyPropagation(&
MI,
MBB)) {
552 bool Commuted =
false;
553 if (!FoundCSE &&
MI.isCommutable()) {
554 if (MachineInstr *NewMI =
TII->commuteInstruction(
MI)) {
556 FoundCSE = VNT.
count(NewMI);
559 NewMI->eraseFromParent();
561 }
else if (!FoundCSE)
563 (void)
TII->commuteInstruction(
MI);
570 bool CrossMBBPhysDef =
false;
571 SmallSet<MCRegister, 8> PhysRefs;
572 PhysDefVector PhysDefs;
573 bool PhysUseDef =
false;
575 hasLivePhysRegDefUses(&
MI,
MBB, PhysRefs, PhysDefs, PhysUseDef)) {
585 MachineInstr *CSMI = Exps[CSVN];
586 if (PhysRegDefsReach(CSMI, &
MI, PhysRefs, PhysDefs, CrossMBBPhysDef))
599 MachineInstr *CSMI = Exps[CSVN];
601 LLVM_DEBUG(
dbgs() <<
"*** Found a common subexpression: " << *CSMI);
612 if (
MI.isConvergent() &&
MI.getParent() != CSMI->
getParent()) {
613 LLVM_DEBUG(
dbgs() <<
"*** Convergent MI and subexpression exist in "
614 "different BBs, avoid CSE!\n");
622 unsigned NumDefs =
MI.getNumDefs();
624 for (
unsigned i = 0, e =
MI.getNumOperands(); NumDefs && i != e; ++i) {
625 MachineOperand &MO =
MI.getOperand(i);
641 if (OldReg == NewReg) {
647 "Do not CSE physical register defs!");
649 if (!isProfitableToCSE(NewReg, OldReg, CSMI->
getParent(), &
MI)) {
658 if (!
MRI->constrainRegAttrs(NewReg, OldReg)) {
660 dbgs() <<
"*** Not the same register constraints, avoid CSE!\n");
671 for (
const std::pair<Register, Register> &CSEPair : CSEPairs) {
675 MachineInstr *
Def =
MRI->getUniqueVRegDef(NewReg);
676 assert(Def !=
nullptr &&
"CSEd register has no unique definition?");
677 Def->clearRegisterDeads(NewReg);
679 MRI->replaceRegWith(OldReg, NewReg);
680 MRI->clearKillFlags(NewReg);
685 for (
unsigned ImplicitDefToUpdate : ImplicitDefsToUpdate)
687 for (
const auto &PhysDef : PhysDefs)
688 if (!
MI.getOperand(PhysDef.first).isDead())
703 for (
auto ImplicitDef : ImplicitDefs)
704 if (MachineOperand *MO =
II->findRegisterUseOperand(
705 ImplicitDef,
TRI,
true))
710 for (
auto ImplicitDef : ImplicitDefs)
711 MRI->clearKillFlags(ImplicitDef);
714 if (CrossMBBPhysDef) {
717 while (!PhysDefs.empty()) {
718 auto LiveIn = PhysDefs.pop_back_val();
725 MI.eraseFromParent();
727 if (!PhysRefs.
empty())
737 ImplicitDefsToUpdate.clear();
738 ImplicitDefs.clear();
747void MachineCSEImpl::ExitScopeIfDone(
749 DenseMap<MachineDomTreeNode *, unsigned> &OpenChildren) {
750 if (OpenChildren[Node])
754 ExitScope(
Node->getBlock());
758 unsigned Left = --OpenChildren[Parent];
761 ExitScope(Parent->getBlock());
769 DenseMap<MachineDomTreeNode*, unsigned> OpenChildren;
778 size_t WorkListSize = WorkList.
size();
780 OpenChildren[
Node] = WorkList.
size() - WorkListSize;
781 }
while (!WorkList.
empty());
786 MachineBasicBlock *
MBB =
Node->getBlock();
790 ExitScopeIfDone(Node, OpenChildren);
799bool MachineCSEImpl::isPRECandidate(MachineInstr *
MI,
800 SmallSet<MCRegister, 8> &PhysRefs) {
801 if (!isCSECandidate(
MI) ||
802 MI->isNotDuplicable() ||
805 MI->getNumDefs() != 1 ||
806 MI->getNumExplicitDefs() != 1)
809 for (
const MachineOperand &MO :
MI->operands()) {
821bool MachineCSEImpl::ProcessBlockPRE(MachineDominatorTree *DT,
822 MachineBasicBlock *
MBB) {
825 SmallSet<MCRegister, 8> PhysRefs;
826 if (!isPRECandidate(&
MI, PhysRefs))
833 auto *MBB1 = It->second;
836 "MBB cannot properly dominate MBB1 while DFS through dominators tree!");
838 if (!CMBB->isLegalToHoistInto())
841 if (!isProfitableToHoistInto(CMBB,
MBB, MBB1))
848 if (BB !=
nullptr && BB1 !=
nullptr &&
858 if (
MI.isConvergent() && CMBB !=
MBB)
864 PhysDefVector PhysDefs;
865 if (!PhysRefs.
empty() &&
866 !PhysRegDefsReach(&*(CMBB->getFirstTerminator()), &
MI, PhysRefs,
871 "First operand of instr with one explicit def must be this def");
874 if (!isProfitableToCSE(NewReg, VReg, CMBB, &
MI))
876 MachineInstr &NewMI =
877 TII->duplicate(*CMBB, CMBB->getFirstTerminator(),
MI);
901bool MachineCSEImpl::PerformSimplePRE(MachineDominatorTree *DT) {
911 MachineBasicBlock *
MBB =
Node->getBlock();
914 }
while (!BBs.
empty());
919bool MachineCSEImpl::isProfitableToHoistInto(MachineBasicBlock *CandidateBB,
920 MachineBasicBlock *
MBB,
921 MachineBasicBlock *MBB1) {
926 "CandidateBB should dominate MBB1");
931void MachineCSEImpl::releaseMemory() {
937bool MachineCSEImpl::run(MachineFunction &MF) {
941 LookAheadLimit =
TII->getMachineCSELookAheadLimit();
942 bool ChangedPRE, ChangedCSE;
943 ChangedPRE = PerformSimplePRE(DT);
946 return ChangedPRE || ChangedCSE;
956 MachineCSEImpl Impl(&MDT, &MBFI);
974 getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
976 getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
977 MachineCSEImpl Impl(&MDT, &MBFI);
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder & UseMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the BumpPtrAllocator interface.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseMap class.
const HexagonInstrInfo * TII
static bool isCallerPreservedOrConstPhysReg(MCRegister Reg, const MachineOperand &MO, const MachineFunction &MF, const TargetRegisterInfo &TRI, const TargetInstrInfo &TII)
static cl::opt< int > CSUsesThreshold("csuses-threshold", cl::Hidden, cl::init(1024), cl::desc("Threshold for the size of CSUses"))
static cl::opt< bool > AggressiveMachineCSE("aggressive-machine-cse", cl::Hidden, cl::init(false), cl::desc("Override the profitability heuristics for Machine CSE"))
Register const TargetRegisterInfo * TRI
Promote Memory to Register
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This file defines the SmallPtrSet class.
This file defines the SmallSet class.
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)
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addPreservedID(const void *ID)
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Represents analyses that only rely on functions' control flow.
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
NodeT * findNearestCommonDominator(NodeT *A, NodeT *B) const
Find nearest common dominator basic block for basic block A and B.
bool properlyDominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
properlyDominates - Returns true iff A dominates B and A != B.
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
bool isAsCheapAsAMove(const MachineInstr &MI) const override
Wrapper class representing physical registers. Should be passed by value.
An RAII based helper class to modify MachineFunctionProperties when running pass.
unsigned pred_size() const
MachineInstrBundleIterator< const MachineInstr > const_iterator
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
pred_iterator pred_begin()
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
LLVM_ABI bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
MachineInstrBundleIterator< MachineInstr > iterator
LLVM_ABI StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
LLVM_ABI bool isLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
LLVM_ABI BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const
getblockFreq - Return block frequency.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Analysis pass which computes a MachineDominatorTree.
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
bool dominates(const MachineInstr *A, const MachineInstr *B) const
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.
const MachineBasicBlock * getParent() const
const MachineOperand & getOperand(unsigned i) const
void setDebugLoc(DebugLoc DL)
Replace current source information with new such.
Analysis pass that exposes the MachineLoopInfo for a machine function.
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
void setIsDead(bool Val=true)
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
void setIsKill(bool Val=true)
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
size_type count(const K &Key) const
Return 1 if the specified key is in the table, 0 otherwise.
void insert(const K &Key, const V &Val)
V lookup(const K &Key) const
ScopedHashTableScope< MachineInstr *, unsigned, MachineInstrExpressionTrait, AllocatorTy > ScopeTy
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
initializer< Ty > init(const Ty &Val)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
Scope
Defines the scope in which this symbol should be visible: Default – Visible in the public interface o...
NodeAddr< DefNode * > Def
NodeAddr< NodeBase * > Node
This is an optimization pass for GlobalISel generic memory operations.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
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
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
IterT skipDebugInstructionsForward(IterT It, IterT End, bool SkipPseudoOp=true)
Increment It until it points to a non-debug instruction or to End and return the resulting iterator.
LLVM_ABI char & MachineLoopInfoID
MachineLoopInfo - This pass is a loop analysis pass.
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...
DomTreeNodeBase< MachineBasicBlock > MachineDomTreeNode
LLVM_ABI char & MachineCSELegacyID
MachineCSE - This pass performs global CSE on machine instructions.
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
LLVM_ABI bool isPotentiallyReachable(const Instruction *From, const Instruction *To, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet=nullptr, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr)
Determine whether instruction 'To' is reachable from 'From', without passing through any blocks in Ex...