49 #define DEBUG_TYPE "machine-cse" 51 STATISTIC(NumCoalesces,
"Number of copies coalesced");
52 STATISTIC(NumCSEs,
"Number of common subexpression eliminated");
54 "Number of physreg referencing common subexpr eliminated");
56 "Number of cross-MBB physreg referencing CS eliminated");
57 STATISTIC(NumCommutes,
"Number of copies coalesced after commuting");
86 void releaseMemory()
override {
97 using ScopeType = ScopedHTType::ScopeTy;
99 unsigned LookAheadLimit = 0;
105 bool PerformTrivialCopyPropagation(MachineInstr *
MI,
107 bool isPhysDefTriviallyDead(
unsigned Reg,
110 bool hasLivePhysRegDefUses(
const MachineInstr *
MI,
114 bool &PhysUseDef)
const;
115 bool PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *
MI,
118 bool &NonLocal)
const;
119 bool isCSECandidate(MachineInstr *
MI);
120 bool isProfitableToCSE(
unsigned CSReg,
unsigned Reg,
121 MachineInstr *CSMI, MachineInstr *
MI);
137 "Machine Common Subexpression Elimination",
false,
false)
149 bool Changed =
false;
151 if (!MO.isReg() || !MO.isUse())
153 unsigned Reg = MO.getReg();
198 MachineCSE::isPhysDefTriviallyDead(
unsigned Reg,
201 unsigned LookAheadLeft = LookAheadLimit;
202 while (LookAheadLeft) {
210 bool SeenDef =
false;
212 if (MO.isRegMask() && MO.clobbersPhysReg(Reg))
214 if (!MO.isReg() || !MO.getReg())
242 bool &PhysUseDef)
const{
245 if (!MO.isReg() || MO.isDef())
247 unsigned Reg = MO.getReg();
263 if (!MO.isReg() || !MO.isDef())
265 unsigned Reg = MO.getReg();
271 if (PhysRefs.
count(Reg))
276 if (!MO.isDead() && !isPhysDefTriviallyDead(Reg, I, MBB->
end()))
281 for (
unsigned i = 0, e = PhysDefs.
size(); i != e; ++i)
285 return !PhysRefs.
empty();
291 bool &NonLocal)
const {
298 bool CrossMBB =
false;
303 for (
unsigned i = 0, e = PhysDefs.
size(); i != e; ++i) {
314 unsigned LookAheadLeft = LookAheadLimit;
315 while (LookAheadLeft) {
317 while (I != E && I != EE && I->isDebugInstr())
321 assert(CrossMBB &&
"Reaching end-of-MBB without finding MI?");
338 if (!MO.isReg() || !MO.isDef())
340 unsigned MOReg = MO.getReg();
343 if (PhysRefs.
count(MOReg))
381 if (MI->
getOpcode() == TargetOpcode::LOAD_STACK_GUARD)
389 bool MachineCSE::isProfitableToCSE(
unsigned CSReg,
unsigned Reg,
395 bool MayIncreasePressure =
true;
398 MayIncreasePressure =
false;
404 if (!CSUses.
count(&MI)) {
405 MayIncreasePressure =
true;
410 if (!MayIncreasePressure)
return true;
424 bool HasVRegUse =
false;
426 if (MO.isReg() && MO.isUse() &&
433 bool HasNonCopyUse =
false;
437 HasNonCopyUse =
true;
449 HasPHI |=
UseMI.isPHI();
459 ScopeType *Scope =
new ScopeType(VNT);
460 ScopeMap[MBB] = Scope;
466 assert(SI != ScopeMap.end());
472 bool Changed =
false;
481 if (!isCSECandidate(MI))
484 bool FoundCSE = VNT.count(MI);
487 if (PerformTrivialCopyPropagation(MI, MBB)) {
495 FoundCSE = VNT.count(MI);
500 bool Commuted =
false;
504 FoundCSE = VNT.count(NewMI);
507 NewMI->eraseFromParent();
509 }
else if (!FoundCSE)
518 bool CrossMBBPhysDef =
false;
521 bool PhysUseDef =
false;
522 if (FoundCSE && hasLivePhysRegDefUses(MI, MBB, PhysRefs,
523 PhysDefs, PhysUseDef)) {
532 unsigned CSVN = VNT.lookup(MI);
534 if (PhysRegDefsReach(CSMI, MI, PhysRefs, PhysDefs, CrossMBBPhysDef))
540 VNT.insert(MI, CurrVN++);
546 unsigned CSVN = VNT.lookup(MI);
549 LLVM_DEBUG(
dbgs() <<
"*** Found a common subexpression: " << *CSMI);
555 for (
unsigned i = 0, e = MI->
getNumOperands(); NumDefs && i != e; ++i) {
559 unsigned OldReg = MO.
getReg();
572 if (OldReg == NewReg) {
579 "Do not CSE physical register defs!");
581 if (!isProfitableToCSE(NewReg, OldReg, CSMI, MI)) {
592 dbgs() <<
"*** Not the same register constraints, avoid CSE!\n");
597 CSEPairs.
push_back(std::make_pair(OldReg, NewReg));
603 for (std::pair<unsigned, unsigned> &CSEPair : CSEPairs) {
604 unsigned OldReg = CSEPair.first;
605 unsigned NewReg = CSEPair.second;
608 assert(Def !=
nullptr &&
"CSEd register has no unique definition?");
617 for (
unsigned ImplicitDefToUpdate : ImplicitDefsToUpdate)
632 for (
auto ImplicitDef : ImplicitDefs)
634 ImplicitDef,
true, TRI))
635 MO->setIsKill(
false);
639 for (
auto ImplicitDef : ImplicitDefs)
643 if (CrossMBBPhysDef) {
646 while (!PhysDefs.
empty()) {
656 if (!PhysRefs.
empty())
662 VNT.insert(MI, CurrVN++);
666 ImplicitDefsToUpdate.
clear();
667 ImplicitDefs.
clear();
679 if (OpenChildren[Node])
683 ExitScope(Node->getBlock());
687 unsigned Left = --OpenChildren[Parent];
690 ExitScope(Parent->getBlock());
707 const std::vector<MachineDomTreeNode*> &Children = Node->
getChildren();
708 OpenChildren[Node] = Children.
size();
711 }
while (!WorkList.
empty());
714 bool Changed =
false;
720 ExitScopeIfDone(Node, OpenChildren);
733 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
734 DT = &getAnalysis<MachineDominatorTree>();
Machine Common Subexpression Elimination
void push_back(const T &Elt)
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
bool isCall(QueryType Type=AnyInBundle) const
bool isAllocatable(unsigned PhysReg) const
isAllocatable - Returns true when PhysReg belongs to an allocatable register class and it hasn't been...
Compute iterated dominance frontiers using a linear time algorithm.
INITIALIZE_PASS_BEGIN(MachineCSE, DEBUG_TYPE, "Machine Common Subexpression Elimination", false, false) INITIALIZE_PASS_END(MachineCSE
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
void initializeMachineCSEPass(PassRegistry &)
unsigned getReg() const
getReg - Returns the register number.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
MachineInstr * commuteInstruction(MachineInstr &MI, bool NewMI=false, unsigned OpIdx1=CommuteAnyOperandIndex, unsigned OpIdx2=CommuteAnyOperandIndex) const
This method commutes the operands of the given machine instruction MI.
unsigned getSubReg() const
bool constrainRegAttrs(unsigned Reg, unsigned ConstrainingReg, unsigned MinNumRegs=0)
Constrain the register class or the register bank of the virtual register Reg to be a common subclass...
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
void setIsDead(bool Val=true)
iterator_range< mop_iterator > operands()
bool isCopyLike() const
Return true if the instruction behaves like a copy.
Special DenseMapInfo traits to compare MachineInstr* by value of the instruction rather than by point...
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
virtual unsigned getMachineCSELookAheadLimit() const
Return the value to use for the MachineCSE's LookAheadLimit, which is a heuristic used for CSE'ing ph...
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
LLVM_NODISCARD bool empty() const
char & MachineLoopInfoID
MachineLoopInfo - This pass is a loop analysis pass.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
unsigned getNumOperands() const
Access to explicit operands of the instruction.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
bool isTerminator(QueryType Type=AnyInBundle) const
Returns true if this instruction part of the terminator for a basic block.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
RecyclingAllocator - This class wraps an Allocator, adding the functionality of recycling deleted obj...
MachineInstr * getVRegDef(unsigned Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
char & MachineCSEID
MachineCSE - This pass performs global CSE on machine instructions.
void clearKillFlags(unsigned Reg) const
clearKillFlags - Iterate over all the uses of the given register and clear the kill flag from the Mac...
Base class for the actual dominator tree node.
const std::vector< DomTreeNodeBase * > & getChildren() const
AnalysisUsage & addPreservedID(const void *ID)
COFF::MachineTypes Machine
virtual const TargetInstrInfo * getInstrInfo() const
static bool ProcessBlock(BasicBlock &BB, DominatorTree &DT, LoopInfo &LI, AAResults &AA)
bool isDereferenceableInvariantLoad(AliasAnalysis *AA) const
Return true if this load instruction never traps and points to a memory location whose value doesn't ...
TargetInstrInfo - Interface to description of machine instruction set.
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
bool mayStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly modify memory.
void addLiveIn(MCPhysReg PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
unsigned const MachineRegisterInfo * MRI
StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
bool isCallerPreservedOrConstPhysReg(unsigned PhysReg) const
Returns true if either isConstantPhysReg or TRI->isCallerPreservedPhysReg returns true...
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.
void clearRegisterDeads(unsigned Reg)
Clear all dead flags on operands defining register Reg.
MachineInstrBuilder & UseMI
DomTreeNodeBase * getIDom() const
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
MCRegAliasIterator enumerates all registers aliasing Reg.
Represent the analysis usage information of a pass.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
bool isImplicitDef() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
pred_iterator pred_begin()
bool isDebugInstr() const
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
virtual bool isAsCheapAsAMove(const MachineInstr &MI) const
Return true if the instruction is as cheap as a move instruction.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
bool regsOverlap(unsigned regA, unsigned regB) const
Returns true if the two registers are equal or alias each other.
MachineDomTreeNode * getRootNode() const
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
MachineInstrBuilder MachineInstrBuilder & DefMI
LLVM_NODISCARD T pop_back_val()
void setPreservesCFG()
This function should be called by the pass, iff they do not:
unsigned pred_size() const
MachineInstr * getUniqueVRegDef(unsigned Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
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.
bool isLiveIn(MCPhysReg Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
void replaceRegWith(unsigned FromReg, unsigned ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
IterT skipDebugInstructionsForward(IterT It, IterT End)
Increment It until it points to a non-debug instruction or to End and return the resulting iterator...
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.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
LLVM_NODISCARD bool empty() const
unsigned getNumDefs() const
Returns the total number of definitions.
bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
bool hasOneNonDBGUse(unsigned RegNo) const
hasOneNonDBGUse - Return true if there is exactly one non-Debug instruction using the specified regis...
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool mayLoad(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read memory.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool hasUnmodeledSideEffects() const
Return true if this instruction has side effects that are not modeled by mayLoad / mayStore...
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
const MachineOperand & getOperand(unsigned i) const
iterator_range< use_instr_nodbg_iterator > use_nodbg_instructions(unsigned Reg) const
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
bool isCommutable(QueryType Type=IgnoreBundle) const
Return true if this may be a 2- or 3-address instruction (of the form "X = op Y, Z, ..."), which produces the same result if Y and Z are exchanged.
bool isReserved(unsigned PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.