1*9880d681SAndroid Build Coastguard Worker //===-- MachineCSE.cpp - Machine Common Subexpression Elimination Pass ----===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This pass performs global common subexpression elimination on machine
11*9880d681SAndroid Build Coastguard Worker // instructions using a scoped hash table based value numbering scheme. It
12*9880d681SAndroid Build Coastguard Worker // must be run while the machine function is still in SSA form.
13*9880d681SAndroid Build Coastguard Worker //
14*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
15*9880d681SAndroid Build Coastguard Worker
16*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Passes.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/DenseMap.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/ScopedHashTable.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallSet.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AliasAnalysis.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineDominators.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstr.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/RecyclingAllocator.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
30*9880d681SAndroid Build Coastguard Worker using namespace llvm;
31*9880d681SAndroid Build Coastguard Worker
32*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "machine-cse"
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard Worker STATISTIC(NumCoalesces, "Number of copies coalesced");
35*9880d681SAndroid Build Coastguard Worker STATISTIC(NumCSEs, "Number of common subexpression eliminated");
36*9880d681SAndroid Build Coastguard Worker STATISTIC(NumPhysCSEs,
37*9880d681SAndroid Build Coastguard Worker "Number of physreg referencing common subexpr eliminated");
38*9880d681SAndroid Build Coastguard Worker STATISTIC(NumCrossBBCSEs,
39*9880d681SAndroid Build Coastguard Worker "Number of cross-MBB physreg referencing CS eliminated");
40*9880d681SAndroid Build Coastguard Worker STATISTIC(NumCommutes, "Number of copies coalesced after commuting");
41*9880d681SAndroid Build Coastguard Worker
42*9880d681SAndroid Build Coastguard Worker namespace {
43*9880d681SAndroid Build Coastguard Worker class MachineCSE : public MachineFunctionPass {
44*9880d681SAndroid Build Coastguard Worker const TargetInstrInfo *TII;
45*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI;
46*9880d681SAndroid Build Coastguard Worker AliasAnalysis *AA;
47*9880d681SAndroid Build Coastguard Worker MachineDominatorTree *DT;
48*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo *MRI;
49*9880d681SAndroid Build Coastguard Worker public:
50*9880d681SAndroid Build Coastguard Worker static char ID; // Pass identification
MachineCSE()51*9880d681SAndroid Build Coastguard Worker MachineCSE() : MachineFunctionPass(ID), LookAheadLimit(0), CurrVN(0) {
52*9880d681SAndroid Build Coastguard Worker initializeMachineCSEPass(*PassRegistry::getPassRegistry());
53*9880d681SAndroid Build Coastguard Worker }
54*9880d681SAndroid Build Coastguard Worker
55*9880d681SAndroid Build Coastguard Worker bool runOnMachineFunction(MachineFunction &MF) override;
56*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage(AnalysisUsage & AU) const57*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override {
58*9880d681SAndroid Build Coastguard Worker AU.setPreservesCFG();
59*9880d681SAndroid Build Coastguard Worker MachineFunctionPass::getAnalysisUsage(AU);
60*9880d681SAndroid Build Coastguard Worker AU.addRequired<AAResultsWrapperPass>();
61*9880d681SAndroid Build Coastguard Worker AU.addPreservedID(MachineLoopInfoID);
62*9880d681SAndroid Build Coastguard Worker AU.addRequired<MachineDominatorTree>();
63*9880d681SAndroid Build Coastguard Worker AU.addPreserved<MachineDominatorTree>();
64*9880d681SAndroid Build Coastguard Worker }
65*9880d681SAndroid Build Coastguard Worker
releaseMemory()66*9880d681SAndroid Build Coastguard Worker void releaseMemory() override {
67*9880d681SAndroid Build Coastguard Worker ScopeMap.clear();
68*9880d681SAndroid Build Coastguard Worker Exps.clear();
69*9880d681SAndroid Build Coastguard Worker }
70*9880d681SAndroid Build Coastguard Worker
71*9880d681SAndroid Build Coastguard Worker private:
72*9880d681SAndroid Build Coastguard Worker unsigned LookAheadLimit;
73*9880d681SAndroid Build Coastguard Worker typedef RecyclingAllocator<BumpPtrAllocator,
74*9880d681SAndroid Build Coastguard Worker ScopedHashTableVal<MachineInstr*, unsigned> > AllocatorTy;
75*9880d681SAndroid Build Coastguard Worker typedef ScopedHashTable<MachineInstr*, unsigned,
76*9880d681SAndroid Build Coastguard Worker MachineInstrExpressionTrait, AllocatorTy> ScopedHTType;
77*9880d681SAndroid Build Coastguard Worker typedef ScopedHTType::ScopeTy ScopeType;
78*9880d681SAndroid Build Coastguard Worker DenseMap<MachineBasicBlock*, ScopeType*> ScopeMap;
79*9880d681SAndroid Build Coastguard Worker ScopedHTType VNT;
80*9880d681SAndroid Build Coastguard Worker SmallVector<MachineInstr*, 64> Exps;
81*9880d681SAndroid Build Coastguard Worker unsigned CurrVN;
82*9880d681SAndroid Build Coastguard Worker
83*9880d681SAndroid Build Coastguard Worker bool PerformTrivialCopyPropagation(MachineInstr *MI,
84*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB);
85*9880d681SAndroid Build Coastguard Worker bool isPhysDefTriviallyDead(unsigned Reg,
86*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::const_iterator I,
87*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::const_iterator E) const;
88*9880d681SAndroid Build Coastguard Worker bool hasLivePhysRegDefUses(const MachineInstr *MI,
89*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *MBB,
90*9880d681SAndroid Build Coastguard Worker SmallSet<unsigned,8> &PhysRefs,
91*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<unsigned> &PhysDefs,
92*9880d681SAndroid Build Coastguard Worker bool &PhysUseDef) const;
93*9880d681SAndroid Build Coastguard Worker bool PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *MI,
94*9880d681SAndroid Build Coastguard Worker SmallSet<unsigned,8> &PhysRefs,
95*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<unsigned> &PhysDefs,
96*9880d681SAndroid Build Coastguard Worker bool &NonLocal) const;
97*9880d681SAndroid Build Coastguard Worker bool isCSECandidate(MachineInstr *MI);
98*9880d681SAndroid Build Coastguard Worker bool isProfitableToCSE(unsigned CSReg, unsigned Reg,
99*9880d681SAndroid Build Coastguard Worker MachineInstr *CSMI, MachineInstr *MI);
100*9880d681SAndroid Build Coastguard Worker void EnterScope(MachineBasicBlock *MBB);
101*9880d681SAndroid Build Coastguard Worker void ExitScope(MachineBasicBlock *MBB);
102*9880d681SAndroid Build Coastguard Worker bool ProcessBlock(MachineBasicBlock *MBB);
103*9880d681SAndroid Build Coastguard Worker void ExitScopeIfDone(MachineDomTreeNode *Node,
104*9880d681SAndroid Build Coastguard Worker DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren);
105*9880d681SAndroid Build Coastguard Worker bool PerformCSE(MachineDomTreeNode *Node);
106*9880d681SAndroid Build Coastguard Worker };
107*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
108*9880d681SAndroid Build Coastguard Worker
109*9880d681SAndroid Build Coastguard Worker char MachineCSE::ID = 0;
110*9880d681SAndroid Build Coastguard Worker char &llvm::MachineCSEID = MachineCSE::ID;
111*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(MachineCSE, "machine-cse",
112*9880d681SAndroid Build Coastguard Worker "Machine Common Subexpression Elimination", false, false)
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)113*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
114*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
115*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(MachineCSE, "machine-cse",
116*9880d681SAndroid Build Coastguard Worker "Machine Common Subexpression Elimination", false, false)
117*9880d681SAndroid Build Coastguard Worker
118*9880d681SAndroid Build Coastguard Worker /// The source register of a COPY machine instruction can be propagated to all
119*9880d681SAndroid Build Coastguard Worker /// its users, and this propagation could increase the probability of finding
120*9880d681SAndroid Build Coastguard Worker /// common subexpressions. If the COPY has only one user, the COPY itself can
121*9880d681SAndroid Build Coastguard Worker /// be removed.
122*9880d681SAndroid Build Coastguard Worker bool MachineCSE::PerformTrivialCopyPropagation(MachineInstr *MI,
123*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB) {
124*9880d681SAndroid Build Coastguard Worker bool Changed = false;
125*9880d681SAndroid Build Coastguard Worker for (MachineOperand &MO : MI->operands()) {
126*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isUse())
127*9880d681SAndroid Build Coastguard Worker continue;
128*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
129*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isVirtualRegister(Reg))
130*9880d681SAndroid Build Coastguard Worker continue;
131*9880d681SAndroid Build Coastguard Worker bool OnlyOneUse = MRI->hasOneNonDBGUse(Reg);
132*9880d681SAndroid Build Coastguard Worker MachineInstr *DefMI = MRI->getVRegDef(Reg);
133*9880d681SAndroid Build Coastguard Worker if (!DefMI->isCopy())
134*9880d681SAndroid Build Coastguard Worker continue;
135*9880d681SAndroid Build Coastguard Worker unsigned SrcReg = DefMI->getOperand(1).getReg();
136*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isVirtualRegister(SrcReg))
137*9880d681SAndroid Build Coastguard Worker continue;
138*9880d681SAndroid Build Coastguard Worker if (DefMI->getOperand(0).getSubReg())
139*9880d681SAndroid Build Coastguard Worker continue;
140*9880d681SAndroid Build Coastguard Worker // FIXME: We should trivially coalesce subregister copies to expose CSE
141*9880d681SAndroid Build Coastguard Worker // opportunities on instructions with truncated operands (see
142*9880d681SAndroid Build Coastguard Worker // cse-add-with-overflow.ll). This can be done here as follows:
143*9880d681SAndroid Build Coastguard Worker // if (SrcSubReg)
144*9880d681SAndroid Build Coastguard Worker // RC = TRI->getMatchingSuperRegClass(MRI->getRegClass(SrcReg), RC,
145*9880d681SAndroid Build Coastguard Worker // SrcSubReg);
146*9880d681SAndroid Build Coastguard Worker // MO.substVirtReg(SrcReg, SrcSubReg, *TRI);
147*9880d681SAndroid Build Coastguard Worker //
148*9880d681SAndroid Build Coastguard Worker // The 2-addr pass has been updated to handle coalesced subregs. However,
149*9880d681SAndroid Build Coastguard Worker // some machine-specific code still can't handle it.
150*9880d681SAndroid Build Coastguard Worker // To handle it properly we also need a way find a constrained subregister
151*9880d681SAndroid Build Coastguard Worker // class given a super-reg class and subreg index.
152*9880d681SAndroid Build Coastguard Worker if (DefMI->getOperand(1).getSubReg())
153*9880d681SAndroid Build Coastguard Worker continue;
154*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass *RC = MRI->getRegClass(Reg);
155*9880d681SAndroid Build Coastguard Worker if (!MRI->constrainRegClass(SrcReg, RC))
156*9880d681SAndroid Build Coastguard Worker continue;
157*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Coalescing: " << *DefMI);
158*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "*** to: " << *MI);
159*9880d681SAndroid Build Coastguard Worker // Propagate SrcReg of copies to MI.
160*9880d681SAndroid Build Coastguard Worker MO.setReg(SrcReg);
161*9880d681SAndroid Build Coastguard Worker MRI->clearKillFlags(SrcReg);
162*9880d681SAndroid Build Coastguard Worker // Coalesce single use copies.
163*9880d681SAndroid Build Coastguard Worker if (OnlyOneUse) {
164*9880d681SAndroid Build Coastguard Worker DefMI->eraseFromParent();
165*9880d681SAndroid Build Coastguard Worker ++NumCoalesces;
166*9880d681SAndroid Build Coastguard Worker }
167*9880d681SAndroid Build Coastguard Worker Changed = true;
168*9880d681SAndroid Build Coastguard Worker }
169*9880d681SAndroid Build Coastguard Worker
170*9880d681SAndroid Build Coastguard Worker return Changed;
171*9880d681SAndroid Build Coastguard Worker }
172*9880d681SAndroid Build Coastguard Worker
173*9880d681SAndroid Build Coastguard Worker bool
isPhysDefTriviallyDead(unsigned Reg,MachineBasicBlock::const_iterator I,MachineBasicBlock::const_iterator E) const174*9880d681SAndroid Build Coastguard Worker MachineCSE::isPhysDefTriviallyDead(unsigned Reg,
175*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::const_iterator I,
176*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::const_iterator E) const {
177*9880d681SAndroid Build Coastguard Worker unsigned LookAheadLeft = LookAheadLimit;
178*9880d681SAndroid Build Coastguard Worker while (LookAheadLeft) {
179*9880d681SAndroid Build Coastguard Worker // Skip over dbg_value's.
180*9880d681SAndroid Build Coastguard Worker while (I != E && I->isDebugValue())
181*9880d681SAndroid Build Coastguard Worker ++I;
182*9880d681SAndroid Build Coastguard Worker
183*9880d681SAndroid Build Coastguard Worker if (I == E)
184*9880d681SAndroid Build Coastguard Worker // Reached end of block, register is obviously dead.
185*9880d681SAndroid Build Coastguard Worker return true;
186*9880d681SAndroid Build Coastguard Worker
187*9880d681SAndroid Build Coastguard Worker bool SeenDef = false;
188*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &MO : I->operands()) {
189*9880d681SAndroid Build Coastguard Worker if (MO.isRegMask() && MO.clobbersPhysReg(Reg))
190*9880d681SAndroid Build Coastguard Worker SeenDef = true;
191*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.getReg())
192*9880d681SAndroid Build Coastguard Worker continue;
193*9880d681SAndroid Build Coastguard Worker if (!TRI->regsOverlap(MO.getReg(), Reg))
194*9880d681SAndroid Build Coastguard Worker continue;
195*9880d681SAndroid Build Coastguard Worker if (MO.isUse())
196*9880d681SAndroid Build Coastguard Worker // Found a use!
197*9880d681SAndroid Build Coastguard Worker return false;
198*9880d681SAndroid Build Coastguard Worker SeenDef = true;
199*9880d681SAndroid Build Coastguard Worker }
200*9880d681SAndroid Build Coastguard Worker if (SeenDef)
201*9880d681SAndroid Build Coastguard Worker // See a def of Reg (or an alias) before encountering any use, it's
202*9880d681SAndroid Build Coastguard Worker // trivially dead.
203*9880d681SAndroid Build Coastguard Worker return true;
204*9880d681SAndroid Build Coastguard Worker
205*9880d681SAndroid Build Coastguard Worker --LookAheadLeft;
206*9880d681SAndroid Build Coastguard Worker ++I;
207*9880d681SAndroid Build Coastguard Worker }
208*9880d681SAndroid Build Coastguard Worker return false;
209*9880d681SAndroid Build Coastguard Worker }
210*9880d681SAndroid Build Coastguard Worker
211*9880d681SAndroid Build Coastguard Worker /// hasLivePhysRegDefUses - Return true if the specified instruction read/write
212*9880d681SAndroid Build Coastguard Worker /// physical registers (except for dead defs of physical registers). It also
213*9880d681SAndroid Build Coastguard Worker /// returns the physical register def by reference if it's the only one and the
214*9880d681SAndroid Build Coastguard Worker /// instruction does not uses a physical register.
hasLivePhysRegDefUses(const MachineInstr * MI,const MachineBasicBlock * MBB,SmallSet<unsigned,8> & PhysRefs,SmallVectorImpl<unsigned> & PhysDefs,bool & PhysUseDef) const215*9880d681SAndroid Build Coastguard Worker bool MachineCSE::hasLivePhysRegDefUses(const MachineInstr *MI,
216*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *MBB,
217*9880d681SAndroid Build Coastguard Worker SmallSet<unsigned,8> &PhysRefs,
218*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<unsigned> &PhysDefs,
219*9880d681SAndroid Build Coastguard Worker bool &PhysUseDef) const{
220*9880d681SAndroid Build Coastguard Worker // First, add all uses to PhysRefs.
221*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &MO : MI->operands()) {
222*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || MO.isDef())
223*9880d681SAndroid Build Coastguard Worker continue;
224*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
225*9880d681SAndroid Build Coastguard Worker if (!Reg)
226*9880d681SAndroid Build Coastguard Worker continue;
227*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isVirtualRegister(Reg))
228*9880d681SAndroid Build Coastguard Worker continue;
229*9880d681SAndroid Build Coastguard Worker // Reading constant physregs is ok.
230*9880d681SAndroid Build Coastguard Worker if (!MRI->isConstantPhysReg(Reg, *MBB->getParent()))
231*9880d681SAndroid Build Coastguard Worker for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
232*9880d681SAndroid Build Coastguard Worker PhysRefs.insert(*AI);
233*9880d681SAndroid Build Coastguard Worker }
234*9880d681SAndroid Build Coastguard Worker
235*9880d681SAndroid Build Coastguard Worker // Next, collect all defs into PhysDefs. If any is already in PhysRefs
236*9880d681SAndroid Build Coastguard Worker // (which currently contains only uses), set the PhysUseDef flag.
237*9880d681SAndroid Build Coastguard Worker PhysUseDef = false;
238*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::const_iterator I = MI; I = std::next(I);
239*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &MO : MI->operands()) {
240*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isDef())
241*9880d681SAndroid Build Coastguard Worker continue;
242*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
243*9880d681SAndroid Build Coastguard Worker if (!Reg)
244*9880d681SAndroid Build Coastguard Worker continue;
245*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isVirtualRegister(Reg))
246*9880d681SAndroid Build Coastguard Worker continue;
247*9880d681SAndroid Build Coastguard Worker // Check against PhysRefs even if the def is "dead".
248*9880d681SAndroid Build Coastguard Worker if (PhysRefs.count(Reg))
249*9880d681SAndroid Build Coastguard Worker PhysUseDef = true;
250*9880d681SAndroid Build Coastguard Worker // If the def is dead, it's ok. But the def may not marked "dead". That's
251*9880d681SAndroid Build Coastguard Worker // common since this pass is run before livevariables. We can scan
252*9880d681SAndroid Build Coastguard Worker // forward a few instructions and check if it is obviously dead.
253*9880d681SAndroid Build Coastguard Worker if (!MO.isDead() && !isPhysDefTriviallyDead(Reg, I, MBB->end()))
254*9880d681SAndroid Build Coastguard Worker PhysDefs.push_back(Reg);
255*9880d681SAndroid Build Coastguard Worker }
256*9880d681SAndroid Build Coastguard Worker
257*9880d681SAndroid Build Coastguard Worker // Finally, add all defs to PhysRefs as well.
258*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = PhysDefs.size(); i != e; ++i)
259*9880d681SAndroid Build Coastguard Worker for (MCRegAliasIterator AI(PhysDefs[i], TRI, true); AI.isValid(); ++AI)
260*9880d681SAndroid Build Coastguard Worker PhysRefs.insert(*AI);
261*9880d681SAndroid Build Coastguard Worker
262*9880d681SAndroid Build Coastguard Worker return !PhysRefs.empty();
263*9880d681SAndroid Build Coastguard Worker }
264*9880d681SAndroid Build Coastguard Worker
PhysRegDefsReach(MachineInstr * CSMI,MachineInstr * MI,SmallSet<unsigned,8> & PhysRefs,SmallVectorImpl<unsigned> & PhysDefs,bool & NonLocal) const265*9880d681SAndroid Build Coastguard Worker bool MachineCSE::PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *MI,
266*9880d681SAndroid Build Coastguard Worker SmallSet<unsigned,8> &PhysRefs,
267*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<unsigned> &PhysDefs,
268*9880d681SAndroid Build Coastguard Worker bool &NonLocal) const {
269*9880d681SAndroid Build Coastguard Worker // For now conservatively returns false if the common subexpression is
270*9880d681SAndroid Build Coastguard Worker // not in the same basic block as the given instruction. The only exception
271*9880d681SAndroid Build Coastguard Worker // is if the common subexpression is in the sole predecessor block.
272*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *MBB = MI->getParent();
273*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *CSMBB = CSMI->getParent();
274*9880d681SAndroid Build Coastguard Worker
275*9880d681SAndroid Build Coastguard Worker bool CrossMBB = false;
276*9880d681SAndroid Build Coastguard Worker if (CSMBB != MBB) {
277*9880d681SAndroid Build Coastguard Worker if (MBB->pred_size() != 1 || *MBB->pred_begin() != CSMBB)
278*9880d681SAndroid Build Coastguard Worker return false;
279*9880d681SAndroid Build Coastguard Worker
280*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = PhysDefs.size(); i != e; ++i) {
281*9880d681SAndroid Build Coastguard Worker if (MRI->isAllocatable(PhysDefs[i]) || MRI->isReserved(PhysDefs[i]))
282*9880d681SAndroid Build Coastguard Worker // Avoid extending live range of physical registers if they are
283*9880d681SAndroid Build Coastguard Worker //allocatable or reserved.
284*9880d681SAndroid Build Coastguard Worker return false;
285*9880d681SAndroid Build Coastguard Worker }
286*9880d681SAndroid Build Coastguard Worker CrossMBB = true;
287*9880d681SAndroid Build Coastguard Worker }
288*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::const_iterator I = CSMI; I = std::next(I);
289*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::const_iterator E = MI;
290*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::const_iterator EE = CSMBB->end();
291*9880d681SAndroid Build Coastguard Worker unsigned LookAheadLeft = LookAheadLimit;
292*9880d681SAndroid Build Coastguard Worker while (LookAheadLeft) {
293*9880d681SAndroid Build Coastguard Worker // Skip over dbg_value's.
294*9880d681SAndroid Build Coastguard Worker while (I != E && I != EE && I->isDebugValue())
295*9880d681SAndroid Build Coastguard Worker ++I;
296*9880d681SAndroid Build Coastguard Worker
297*9880d681SAndroid Build Coastguard Worker if (I == EE) {
298*9880d681SAndroid Build Coastguard Worker assert(CrossMBB && "Reaching end-of-MBB without finding MI?");
299*9880d681SAndroid Build Coastguard Worker (void)CrossMBB;
300*9880d681SAndroid Build Coastguard Worker CrossMBB = false;
301*9880d681SAndroid Build Coastguard Worker NonLocal = true;
302*9880d681SAndroid Build Coastguard Worker I = MBB->begin();
303*9880d681SAndroid Build Coastguard Worker EE = MBB->end();
304*9880d681SAndroid Build Coastguard Worker continue;
305*9880d681SAndroid Build Coastguard Worker }
306*9880d681SAndroid Build Coastguard Worker
307*9880d681SAndroid Build Coastguard Worker if (I == E)
308*9880d681SAndroid Build Coastguard Worker return true;
309*9880d681SAndroid Build Coastguard Worker
310*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &MO : I->operands()) {
311*9880d681SAndroid Build Coastguard Worker // RegMasks go on instructions like calls that clobber lots of physregs.
312*9880d681SAndroid Build Coastguard Worker // Don't attempt to CSE across such an instruction.
313*9880d681SAndroid Build Coastguard Worker if (MO.isRegMask())
314*9880d681SAndroid Build Coastguard Worker return false;
315*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isDef())
316*9880d681SAndroid Build Coastguard Worker continue;
317*9880d681SAndroid Build Coastguard Worker unsigned MOReg = MO.getReg();
318*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isVirtualRegister(MOReg))
319*9880d681SAndroid Build Coastguard Worker continue;
320*9880d681SAndroid Build Coastguard Worker if (PhysRefs.count(MOReg))
321*9880d681SAndroid Build Coastguard Worker return false;
322*9880d681SAndroid Build Coastguard Worker }
323*9880d681SAndroid Build Coastguard Worker
324*9880d681SAndroid Build Coastguard Worker --LookAheadLeft;
325*9880d681SAndroid Build Coastguard Worker ++I;
326*9880d681SAndroid Build Coastguard Worker }
327*9880d681SAndroid Build Coastguard Worker
328*9880d681SAndroid Build Coastguard Worker return false;
329*9880d681SAndroid Build Coastguard Worker }
330*9880d681SAndroid Build Coastguard Worker
isCSECandidate(MachineInstr * MI)331*9880d681SAndroid Build Coastguard Worker bool MachineCSE::isCSECandidate(MachineInstr *MI) {
332*9880d681SAndroid Build Coastguard Worker if (MI->isPosition() || MI->isPHI() || MI->isImplicitDef() || MI->isKill() ||
333*9880d681SAndroid Build Coastguard Worker MI->isInlineAsm() || MI->isDebugValue())
334*9880d681SAndroid Build Coastguard Worker return false;
335*9880d681SAndroid Build Coastguard Worker
336*9880d681SAndroid Build Coastguard Worker // Ignore copies.
337*9880d681SAndroid Build Coastguard Worker if (MI->isCopyLike())
338*9880d681SAndroid Build Coastguard Worker return false;
339*9880d681SAndroid Build Coastguard Worker
340*9880d681SAndroid Build Coastguard Worker // Ignore stuff that we obviously can't move.
341*9880d681SAndroid Build Coastguard Worker if (MI->mayStore() || MI->isCall() || MI->isTerminator() ||
342*9880d681SAndroid Build Coastguard Worker MI->hasUnmodeledSideEffects())
343*9880d681SAndroid Build Coastguard Worker return false;
344*9880d681SAndroid Build Coastguard Worker
345*9880d681SAndroid Build Coastguard Worker if (MI->mayLoad()) {
346*9880d681SAndroid Build Coastguard Worker // Okay, this instruction does a load. As a refinement, we allow the target
347*9880d681SAndroid Build Coastguard Worker // to decide whether the loaded value is actually a constant. If so, we can
348*9880d681SAndroid Build Coastguard Worker // actually use it as a load.
349*9880d681SAndroid Build Coastguard Worker if (!MI->isInvariantLoad(AA))
350*9880d681SAndroid Build Coastguard Worker // FIXME: we should be able to hoist loads with no other side effects if
351*9880d681SAndroid Build Coastguard Worker // there are no other instructions which can change memory in this loop.
352*9880d681SAndroid Build Coastguard Worker // This is a trivial form of alias analysis.
353*9880d681SAndroid Build Coastguard Worker return false;
354*9880d681SAndroid Build Coastguard Worker }
355*9880d681SAndroid Build Coastguard Worker
356*9880d681SAndroid Build Coastguard Worker // Ignore stack guard loads, otherwise the register that holds CSEed value may
357*9880d681SAndroid Build Coastguard Worker // be spilled and get loaded back with corrupted data.
358*9880d681SAndroid Build Coastguard Worker if (MI->getOpcode() == TargetOpcode::LOAD_STACK_GUARD)
359*9880d681SAndroid Build Coastguard Worker return false;
360*9880d681SAndroid Build Coastguard Worker
361*9880d681SAndroid Build Coastguard Worker return true;
362*9880d681SAndroid Build Coastguard Worker }
363*9880d681SAndroid Build Coastguard Worker
364*9880d681SAndroid Build Coastguard Worker /// isProfitableToCSE - Return true if it's profitable to eliminate MI with a
365*9880d681SAndroid Build Coastguard Worker /// common expression that defines Reg.
isProfitableToCSE(unsigned CSReg,unsigned Reg,MachineInstr * CSMI,MachineInstr * MI)366*9880d681SAndroid Build Coastguard Worker bool MachineCSE::isProfitableToCSE(unsigned CSReg, unsigned Reg,
367*9880d681SAndroid Build Coastguard Worker MachineInstr *CSMI, MachineInstr *MI) {
368*9880d681SAndroid Build Coastguard Worker // FIXME: Heuristics that works around the lack the live range splitting.
369*9880d681SAndroid Build Coastguard Worker
370*9880d681SAndroid Build Coastguard Worker // If CSReg is used at all uses of Reg, CSE should not increase register
371*9880d681SAndroid Build Coastguard Worker // pressure of CSReg.
372*9880d681SAndroid Build Coastguard Worker bool MayIncreasePressure = true;
373*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isVirtualRegister(CSReg) &&
374*9880d681SAndroid Build Coastguard Worker TargetRegisterInfo::isVirtualRegister(Reg)) {
375*9880d681SAndroid Build Coastguard Worker MayIncreasePressure = false;
376*9880d681SAndroid Build Coastguard Worker SmallPtrSet<MachineInstr*, 8> CSUses;
377*9880d681SAndroid Build Coastguard Worker for (MachineInstr &MI : MRI->use_nodbg_instructions(CSReg)) {
378*9880d681SAndroid Build Coastguard Worker CSUses.insert(&MI);
379*9880d681SAndroid Build Coastguard Worker }
380*9880d681SAndroid Build Coastguard Worker for (MachineInstr &MI : MRI->use_nodbg_instructions(Reg)) {
381*9880d681SAndroid Build Coastguard Worker if (!CSUses.count(&MI)) {
382*9880d681SAndroid Build Coastguard Worker MayIncreasePressure = true;
383*9880d681SAndroid Build Coastguard Worker break;
384*9880d681SAndroid Build Coastguard Worker }
385*9880d681SAndroid Build Coastguard Worker }
386*9880d681SAndroid Build Coastguard Worker }
387*9880d681SAndroid Build Coastguard Worker if (!MayIncreasePressure) return true;
388*9880d681SAndroid Build Coastguard Worker
389*9880d681SAndroid Build Coastguard Worker // Heuristics #1: Don't CSE "cheap" computation if the def is not local or in
390*9880d681SAndroid Build Coastguard Worker // an immediate predecessor. We don't want to increase register pressure and
391*9880d681SAndroid Build Coastguard Worker // end up causing other computation to be spilled.
392*9880d681SAndroid Build Coastguard Worker if (TII->isAsCheapAsAMove(*MI)) {
393*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *CSBB = CSMI->getParent();
394*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *BB = MI->getParent();
395*9880d681SAndroid Build Coastguard Worker if (CSBB != BB && !CSBB->isSuccessor(BB))
396*9880d681SAndroid Build Coastguard Worker return false;
397*9880d681SAndroid Build Coastguard Worker }
398*9880d681SAndroid Build Coastguard Worker
399*9880d681SAndroid Build Coastguard Worker // Heuristics #2: If the expression doesn't not use a vr and the only use
400*9880d681SAndroid Build Coastguard Worker // of the redundant computation are copies, do not cse.
401*9880d681SAndroid Build Coastguard Worker bool HasVRegUse = false;
402*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &MO : MI->operands()) {
403*9880d681SAndroid Build Coastguard Worker if (MO.isReg() && MO.isUse() &&
404*9880d681SAndroid Build Coastguard Worker TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
405*9880d681SAndroid Build Coastguard Worker HasVRegUse = true;
406*9880d681SAndroid Build Coastguard Worker break;
407*9880d681SAndroid Build Coastguard Worker }
408*9880d681SAndroid Build Coastguard Worker }
409*9880d681SAndroid Build Coastguard Worker if (!HasVRegUse) {
410*9880d681SAndroid Build Coastguard Worker bool HasNonCopyUse = false;
411*9880d681SAndroid Build Coastguard Worker for (MachineInstr &MI : MRI->use_nodbg_instructions(Reg)) {
412*9880d681SAndroid Build Coastguard Worker // Ignore copies.
413*9880d681SAndroid Build Coastguard Worker if (!MI.isCopyLike()) {
414*9880d681SAndroid Build Coastguard Worker HasNonCopyUse = true;
415*9880d681SAndroid Build Coastguard Worker break;
416*9880d681SAndroid Build Coastguard Worker }
417*9880d681SAndroid Build Coastguard Worker }
418*9880d681SAndroid Build Coastguard Worker if (!HasNonCopyUse)
419*9880d681SAndroid Build Coastguard Worker return false;
420*9880d681SAndroid Build Coastguard Worker }
421*9880d681SAndroid Build Coastguard Worker
422*9880d681SAndroid Build Coastguard Worker // Heuristics #3: If the common subexpression is used by PHIs, do not reuse
423*9880d681SAndroid Build Coastguard Worker // it unless the defined value is already used in the BB of the new use.
424*9880d681SAndroid Build Coastguard Worker bool HasPHI = false;
425*9880d681SAndroid Build Coastguard Worker SmallPtrSet<MachineBasicBlock*, 4> CSBBs;
426*9880d681SAndroid Build Coastguard Worker for (MachineInstr &MI : MRI->use_nodbg_instructions(CSReg)) {
427*9880d681SAndroid Build Coastguard Worker HasPHI |= MI.isPHI();
428*9880d681SAndroid Build Coastguard Worker CSBBs.insert(MI.getParent());
429*9880d681SAndroid Build Coastguard Worker }
430*9880d681SAndroid Build Coastguard Worker
431*9880d681SAndroid Build Coastguard Worker if (!HasPHI)
432*9880d681SAndroid Build Coastguard Worker return true;
433*9880d681SAndroid Build Coastguard Worker return CSBBs.count(MI->getParent());
434*9880d681SAndroid Build Coastguard Worker }
435*9880d681SAndroid Build Coastguard Worker
EnterScope(MachineBasicBlock * MBB)436*9880d681SAndroid Build Coastguard Worker void MachineCSE::EnterScope(MachineBasicBlock *MBB) {
437*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Entering: " << MBB->getName() << '\n');
438*9880d681SAndroid Build Coastguard Worker ScopeType *Scope = new ScopeType(VNT);
439*9880d681SAndroid Build Coastguard Worker ScopeMap[MBB] = Scope;
440*9880d681SAndroid Build Coastguard Worker }
441*9880d681SAndroid Build Coastguard Worker
ExitScope(MachineBasicBlock * MBB)442*9880d681SAndroid Build Coastguard Worker void MachineCSE::ExitScope(MachineBasicBlock *MBB) {
443*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Exiting: " << MBB->getName() << '\n');
444*9880d681SAndroid Build Coastguard Worker DenseMap<MachineBasicBlock*, ScopeType*>::iterator SI = ScopeMap.find(MBB);
445*9880d681SAndroid Build Coastguard Worker assert(SI != ScopeMap.end());
446*9880d681SAndroid Build Coastguard Worker delete SI->second;
447*9880d681SAndroid Build Coastguard Worker ScopeMap.erase(SI);
448*9880d681SAndroid Build Coastguard Worker }
449*9880d681SAndroid Build Coastguard Worker
ProcessBlock(MachineBasicBlock * MBB)450*9880d681SAndroid Build Coastguard Worker bool MachineCSE::ProcessBlock(MachineBasicBlock *MBB) {
451*9880d681SAndroid Build Coastguard Worker bool Changed = false;
452*9880d681SAndroid Build Coastguard Worker
453*9880d681SAndroid Build Coastguard Worker SmallVector<std::pair<unsigned, unsigned>, 8> CSEPairs;
454*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned, 2> ImplicitDefsToUpdate;
455*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned, 2> ImplicitDefs;
456*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E; ) {
457*9880d681SAndroid Build Coastguard Worker MachineInstr *MI = &*I;
458*9880d681SAndroid Build Coastguard Worker ++I;
459*9880d681SAndroid Build Coastguard Worker
460*9880d681SAndroid Build Coastguard Worker if (!isCSECandidate(MI))
461*9880d681SAndroid Build Coastguard Worker continue;
462*9880d681SAndroid Build Coastguard Worker
463*9880d681SAndroid Build Coastguard Worker bool FoundCSE = VNT.count(MI);
464*9880d681SAndroid Build Coastguard Worker if (!FoundCSE) {
465*9880d681SAndroid Build Coastguard Worker // Using trivial copy propagation to find more CSE opportunities.
466*9880d681SAndroid Build Coastguard Worker if (PerformTrivialCopyPropagation(MI, MBB)) {
467*9880d681SAndroid Build Coastguard Worker Changed = true;
468*9880d681SAndroid Build Coastguard Worker
469*9880d681SAndroid Build Coastguard Worker // After coalescing MI itself may become a copy.
470*9880d681SAndroid Build Coastguard Worker if (MI->isCopyLike())
471*9880d681SAndroid Build Coastguard Worker continue;
472*9880d681SAndroid Build Coastguard Worker
473*9880d681SAndroid Build Coastguard Worker // Try again to see if CSE is possible.
474*9880d681SAndroid Build Coastguard Worker FoundCSE = VNT.count(MI);
475*9880d681SAndroid Build Coastguard Worker }
476*9880d681SAndroid Build Coastguard Worker }
477*9880d681SAndroid Build Coastguard Worker
478*9880d681SAndroid Build Coastguard Worker // Commute commutable instructions.
479*9880d681SAndroid Build Coastguard Worker bool Commuted = false;
480*9880d681SAndroid Build Coastguard Worker if (!FoundCSE && MI->isCommutable()) {
481*9880d681SAndroid Build Coastguard Worker if (MachineInstr *NewMI = TII->commuteInstruction(*MI)) {
482*9880d681SAndroid Build Coastguard Worker Commuted = true;
483*9880d681SAndroid Build Coastguard Worker FoundCSE = VNT.count(NewMI);
484*9880d681SAndroid Build Coastguard Worker if (NewMI != MI) {
485*9880d681SAndroid Build Coastguard Worker // New instruction. It doesn't need to be kept.
486*9880d681SAndroid Build Coastguard Worker NewMI->eraseFromParent();
487*9880d681SAndroid Build Coastguard Worker Changed = true;
488*9880d681SAndroid Build Coastguard Worker } else if (!FoundCSE)
489*9880d681SAndroid Build Coastguard Worker // MI was changed but it didn't help, commute it back!
490*9880d681SAndroid Build Coastguard Worker (void)TII->commuteInstruction(*MI);
491*9880d681SAndroid Build Coastguard Worker }
492*9880d681SAndroid Build Coastguard Worker }
493*9880d681SAndroid Build Coastguard Worker
494*9880d681SAndroid Build Coastguard Worker // If the instruction defines physical registers and the values *may* be
495*9880d681SAndroid Build Coastguard Worker // used, then it's not safe to replace it with a common subexpression.
496*9880d681SAndroid Build Coastguard Worker // It's also not safe if the instruction uses physical registers.
497*9880d681SAndroid Build Coastguard Worker bool CrossMBBPhysDef = false;
498*9880d681SAndroid Build Coastguard Worker SmallSet<unsigned, 8> PhysRefs;
499*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned, 2> PhysDefs;
500*9880d681SAndroid Build Coastguard Worker bool PhysUseDef = false;
501*9880d681SAndroid Build Coastguard Worker if (FoundCSE && hasLivePhysRegDefUses(MI, MBB, PhysRefs,
502*9880d681SAndroid Build Coastguard Worker PhysDefs, PhysUseDef)) {
503*9880d681SAndroid Build Coastguard Worker FoundCSE = false;
504*9880d681SAndroid Build Coastguard Worker
505*9880d681SAndroid Build Coastguard Worker // ... Unless the CS is local or is in the sole predecessor block
506*9880d681SAndroid Build Coastguard Worker // and it also defines the physical register which is not clobbered
507*9880d681SAndroid Build Coastguard Worker // in between and the physical register uses were not clobbered.
508*9880d681SAndroid Build Coastguard Worker // This can never be the case if the instruction both uses and
509*9880d681SAndroid Build Coastguard Worker // defines the same physical register, which was detected above.
510*9880d681SAndroid Build Coastguard Worker if (!PhysUseDef) {
511*9880d681SAndroid Build Coastguard Worker unsigned CSVN = VNT.lookup(MI);
512*9880d681SAndroid Build Coastguard Worker MachineInstr *CSMI = Exps[CSVN];
513*9880d681SAndroid Build Coastguard Worker if (PhysRegDefsReach(CSMI, MI, PhysRefs, PhysDefs, CrossMBBPhysDef))
514*9880d681SAndroid Build Coastguard Worker FoundCSE = true;
515*9880d681SAndroid Build Coastguard Worker }
516*9880d681SAndroid Build Coastguard Worker }
517*9880d681SAndroid Build Coastguard Worker
518*9880d681SAndroid Build Coastguard Worker if (!FoundCSE) {
519*9880d681SAndroid Build Coastguard Worker VNT.insert(MI, CurrVN++);
520*9880d681SAndroid Build Coastguard Worker Exps.push_back(MI);
521*9880d681SAndroid Build Coastguard Worker continue;
522*9880d681SAndroid Build Coastguard Worker }
523*9880d681SAndroid Build Coastguard Worker
524*9880d681SAndroid Build Coastguard Worker // Found a common subexpression, eliminate it.
525*9880d681SAndroid Build Coastguard Worker unsigned CSVN = VNT.lookup(MI);
526*9880d681SAndroid Build Coastguard Worker MachineInstr *CSMI = Exps[CSVN];
527*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Examining: " << *MI);
528*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "*** Found a common subexpression: " << *CSMI);
529*9880d681SAndroid Build Coastguard Worker
530*9880d681SAndroid Build Coastguard Worker // Check if it's profitable to perform this CSE.
531*9880d681SAndroid Build Coastguard Worker bool DoCSE = true;
532*9880d681SAndroid Build Coastguard Worker unsigned NumDefs = MI->getDesc().getNumDefs() +
533*9880d681SAndroid Build Coastguard Worker MI->getDesc().getNumImplicitDefs();
534*9880d681SAndroid Build Coastguard Worker
535*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = MI->getNumOperands(); NumDefs && i != e; ++i) {
536*9880d681SAndroid Build Coastguard Worker MachineOperand &MO = MI->getOperand(i);
537*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isDef())
538*9880d681SAndroid Build Coastguard Worker continue;
539*9880d681SAndroid Build Coastguard Worker unsigned OldReg = MO.getReg();
540*9880d681SAndroid Build Coastguard Worker unsigned NewReg = CSMI->getOperand(i).getReg();
541*9880d681SAndroid Build Coastguard Worker
542*9880d681SAndroid Build Coastguard Worker // Go through implicit defs of CSMI and MI, if a def is not dead at MI,
543*9880d681SAndroid Build Coastguard Worker // we should make sure it is not dead at CSMI.
544*9880d681SAndroid Build Coastguard Worker if (MO.isImplicit() && !MO.isDead() && CSMI->getOperand(i).isDead())
545*9880d681SAndroid Build Coastguard Worker ImplicitDefsToUpdate.push_back(i);
546*9880d681SAndroid Build Coastguard Worker
547*9880d681SAndroid Build Coastguard Worker // Keep track of implicit defs of CSMI and MI, to clear possibly
548*9880d681SAndroid Build Coastguard Worker // made-redundant kill flags.
549*9880d681SAndroid Build Coastguard Worker if (MO.isImplicit() && !MO.isDead() && OldReg == NewReg)
550*9880d681SAndroid Build Coastguard Worker ImplicitDefs.push_back(OldReg);
551*9880d681SAndroid Build Coastguard Worker
552*9880d681SAndroid Build Coastguard Worker if (OldReg == NewReg) {
553*9880d681SAndroid Build Coastguard Worker --NumDefs;
554*9880d681SAndroid Build Coastguard Worker continue;
555*9880d681SAndroid Build Coastguard Worker }
556*9880d681SAndroid Build Coastguard Worker
557*9880d681SAndroid Build Coastguard Worker assert(TargetRegisterInfo::isVirtualRegister(OldReg) &&
558*9880d681SAndroid Build Coastguard Worker TargetRegisterInfo::isVirtualRegister(NewReg) &&
559*9880d681SAndroid Build Coastguard Worker "Do not CSE physical register defs!");
560*9880d681SAndroid Build Coastguard Worker
561*9880d681SAndroid Build Coastguard Worker if (!isProfitableToCSE(NewReg, OldReg, CSMI, MI)) {
562*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "*** Not profitable, avoid CSE!\n");
563*9880d681SAndroid Build Coastguard Worker DoCSE = false;
564*9880d681SAndroid Build Coastguard Worker break;
565*9880d681SAndroid Build Coastguard Worker }
566*9880d681SAndroid Build Coastguard Worker
567*9880d681SAndroid Build Coastguard Worker // Don't perform CSE if the result of the old instruction cannot exist
568*9880d681SAndroid Build Coastguard Worker // within the register class of the new instruction.
569*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass *OldRC = MRI->getRegClass(OldReg);
570*9880d681SAndroid Build Coastguard Worker if (!MRI->constrainRegClass(NewReg, OldRC)) {
571*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "*** Not the same register class, avoid CSE!\n");
572*9880d681SAndroid Build Coastguard Worker DoCSE = false;
573*9880d681SAndroid Build Coastguard Worker break;
574*9880d681SAndroid Build Coastguard Worker }
575*9880d681SAndroid Build Coastguard Worker
576*9880d681SAndroid Build Coastguard Worker CSEPairs.push_back(std::make_pair(OldReg, NewReg));
577*9880d681SAndroid Build Coastguard Worker --NumDefs;
578*9880d681SAndroid Build Coastguard Worker }
579*9880d681SAndroid Build Coastguard Worker
580*9880d681SAndroid Build Coastguard Worker // Actually perform the elimination.
581*9880d681SAndroid Build Coastguard Worker if (DoCSE) {
582*9880d681SAndroid Build Coastguard Worker for (std::pair<unsigned, unsigned> &CSEPair : CSEPairs) {
583*9880d681SAndroid Build Coastguard Worker unsigned OldReg = CSEPair.first;
584*9880d681SAndroid Build Coastguard Worker unsigned NewReg = CSEPair.second;
585*9880d681SAndroid Build Coastguard Worker // OldReg may have been unused but is used now, clear the Dead flag
586*9880d681SAndroid Build Coastguard Worker MachineInstr *Def = MRI->getUniqueVRegDef(NewReg);
587*9880d681SAndroid Build Coastguard Worker assert(Def != nullptr && "CSEd register has no unique definition?");
588*9880d681SAndroid Build Coastguard Worker Def->clearRegisterDeads(NewReg);
589*9880d681SAndroid Build Coastguard Worker // Replace with NewReg and clear kill flags which may be wrong now.
590*9880d681SAndroid Build Coastguard Worker MRI->replaceRegWith(OldReg, NewReg);
591*9880d681SAndroid Build Coastguard Worker MRI->clearKillFlags(NewReg);
592*9880d681SAndroid Build Coastguard Worker }
593*9880d681SAndroid Build Coastguard Worker
594*9880d681SAndroid Build Coastguard Worker // Go through implicit defs of CSMI and MI, if a def is not dead at MI,
595*9880d681SAndroid Build Coastguard Worker // we should make sure it is not dead at CSMI.
596*9880d681SAndroid Build Coastguard Worker for (unsigned ImplicitDefToUpdate : ImplicitDefsToUpdate)
597*9880d681SAndroid Build Coastguard Worker CSMI->getOperand(ImplicitDefToUpdate).setIsDead(false);
598*9880d681SAndroid Build Coastguard Worker
599*9880d681SAndroid Build Coastguard Worker // Go through implicit defs of CSMI and MI, and clear the kill flags on
600*9880d681SAndroid Build Coastguard Worker // their uses in all the instructions between CSMI and MI.
601*9880d681SAndroid Build Coastguard Worker // We might have made some of the kill flags redundant, consider:
602*9880d681SAndroid Build Coastguard Worker // subs ... %NZCV<imp-def> <- CSMI
603*9880d681SAndroid Build Coastguard Worker // csinc ... %NZCV<imp-use,kill> <- this kill flag isn't valid anymore
604*9880d681SAndroid Build Coastguard Worker // subs ... %NZCV<imp-def> <- MI, to be eliminated
605*9880d681SAndroid Build Coastguard Worker // csinc ... %NZCV<imp-use,kill>
606*9880d681SAndroid Build Coastguard Worker // Since we eliminated MI, and reused a register imp-def'd by CSMI
607*9880d681SAndroid Build Coastguard Worker // (here %NZCV), that register, if it was killed before MI, should have
608*9880d681SAndroid Build Coastguard Worker // that kill flag removed, because it's lifetime was extended.
609*9880d681SAndroid Build Coastguard Worker if (CSMI->getParent() == MI->getParent()) {
610*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::iterator II = CSMI, IE = MI; II != IE; ++II)
611*9880d681SAndroid Build Coastguard Worker for (auto ImplicitDef : ImplicitDefs)
612*9880d681SAndroid Build Coastguard Worker if (MachineOperand *MO = II->findRegisterUseOperand(
613*9880d681SAndroid Build Coastguard Worker ImplicitDef, /*isKill=*/true, TRI))
614*9880d681SAndroid Build Coastguard Worker MO->setIsKill(false);
615*9880d681SAndroid Build Coastguard Worker } else {
616*9880d681SAndroid Build Coastguard Worker // If the instructions aren't in the same BB, bail out and clear the
617*9880d681SAndroid Build Coastguard Worker // kill flag on all uses of the imp-def'd register.
618*9880d681SAndroid Build Coastguard Worker for (auto ImplicitDef : ImplicitDefs)
619*9880d681SAndroid Build Coastguard Worker MRI->clearKillFlags(ImplicitDef);
620*9880d681SAndroid Build Coastguard Worker }
621*9880d681SAndroid Build Coastguard Worker
622*9880d681SAndroid Build Coastguard Worker if (CrossMBBPhysDef) {
623*9880d681SAndroid Build Coastguard Worker // Add physical register defs now coming in from a predecessor to MBB
624*9880d681SAndroid Build Coastguard Worker // livein list.
625*9880d681SAndroid Build Coastguard Worker while (!PhysDefs.empty()) {
626*9880d681SAndroid Build Coastguard Worker unsigned LiveIn = PhysDefs.pop_back_val();
627*9880d681SAndroid Build Coastguard Worker if (!MBB->isLiveIn(LiveIn))
628*9880d681SAndroid Build Coastguard Worker MBB->addLiveIn(LiveIn);
629*9880d681SAndroid Build Coastguard Worker }
630*9880d681SAndroid Build Coastguard Worker ++NumCrossBBCSEs;
631*9880d681SAndroid Build Coastguard Worker }
632*9880d681SAndroid Build Coastguard Worker
633*9880d681SAndroid Build Coastguard Worker MI->eraseFromParent();
634*9880d681SAndroid Build Coastguard Worker ++NumCSEs;
635*9880d681SAndroid Build Coastguard Worker if (!PhysRefs.empty())
636*9880d681SAndroid Build Coastguard Worker ++NumPhysCSEs;
637*9880d681SAndroid Build Coastguard Worker if (Commuted)
638*9880d681SAndroid Build Coastguard Worker ++NumCommutes;
639*9880d681SAndroid Build Coastguard Worker Changed = true;
640*9880d681SAndroid Build Coastguard Worker } else {
641*9880d681SAndroid Build Coastguard Worker VNT.insert(MI, CurrVN++);
642*9880d681SAndroid Build Coastguard Worker Exps.push_back(MI);
643*9880d681SAndroid Build Coastguard Worker }
644*9880d681SAndroid Build Coastguard Worker CSEPairs.clear();
645*9880d681SAndroid Build Coastguard Worker ImplicitDefsToUpdate.clear();
646*9880d681SAndroid Build Coastguard Worker ImplicitDefs.clear();
647*9880d681SAndroid Build Coastguard Worker }
648*9880d681SAndroid Build Coastguard Worker
649*9880d681SAndroid Build Coastguard Worker return Changed;
650*9880d681SAndroid Build Coastguard Worker }
651*9880d681SAndroid Build Coastguard Worker
652*9880d681SAndroid Build Coastguard Worker /// ExitScopeIfDone - Destroy scope for the MBB that corresponds to the given
653*9880d681SAndroid Build Coastguard Worker /// dominator tree node if its a leaf or all of its children are done. Walk
654*9880d681SAndroid Build Coastguard Worker /// up the dominator tree to destroy ancestors which are now done.
655*9880d681SAndroid Build Coastguard Worker void
ExitScopeIfDone(MachineDomTreeNode * Node,DenseMap<MachineDomTreeNode *,unsigned> & OpenChildren)656*9880d681SAndroid Build Coastguard Worker MachineCSE::ExitScopeIfDone(MachineDomTreeNode *Node,
657*9880d681SAndroid Build Coastguard Worker DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren) {
658*9880d681SAndroid Build Coastguard Worker if (OpenChildren[Node])
659*9880d681SAndroid Build Coastguard Worker return;
660*9880d681SAndroid Build Coastguard Worker
661*9880d681SAndroid Build Coastguard Worker // Pop scope.
662*9880d681SAndroid Build Coastguard Worker ExitScope(Node->getBlock());
663*9880d681SAndroid Build Coastguard Worker
664*9880d681SAndroid Build Coastguard Worker // Now traverse upwards to pop ancestors whose offsprings are all done.
665*9880d681SAndroid Build Coastguard Worker while (MachineDomTreeNode *Parent = Node->getIDom()) {
666*9880d681SAndroid Build Coastguard Worker unsigned Left = --OpenChildren[Parent];
667*9880d681SAndroid Build Coastguard Worker if (Left != 0)
668*9880d681SAndroid Build Coastguard Worker break;
669*9880d681SAndroid Build Coastguard Worker ExitScope(Parent->getBlock());
670*9880d681SAndroid Build Coastguard Worker Node = Parent;
671*9880d681SAndroid Build Coastguard Worker }
672*9880d681SAndroid Build Coastguard Worker }
673*9880d681SAndroid Build Coastguard Worker
PerformCSE(MachineDomTreeNode * Node)674*9880d681SAndroid Build Coastguard Worker bool MachineCSE::PerformCSE(MachineDomTreeNode *Node) {
675*9880d681SAndroid Build Coastguard Worker SmallVector<MachineDomTreeNode*, 32> Scopes;
676*9880d681SAndroid Build Coastguard Worker SmallVector<MachineDomTreeNode*, 8> WorkList;
677*9880d681SAndroid Build Coastguard Worker DenseMap<MachineDomTreeNode*, unsigned> OpenChildren;
678*9880d681SAndroid Build Coastguard Worker
679*9880d681SAndroid Build Coastguard Worker CurrVN = 0;
680*9880d681SAndroid Build Coastguard Worker
681*9880d681SAndroid Build Coastguard Worker // Perform a DFS walk to determine the order of visit.
682*9880d681SAndroid Build Coastguard Worker WorkList.push_back(Node);
683*9880d681SAndroid Build Coastguard Worker do {
684*9880d681SAndroid Build Coastguard Worker Node = WorkList.pop_back_val();
685*9880d681SAndroid Build Coastguard Worker Scopes.push_back(Node);
686*9880d681SAndroid Build Coastguard Worker const std::vector<MachineDomTreeNode*> &Children = Node->getChildren();
687*9880d681SAndroid Build Coastguard Worker OpenChildren[Node] = Children.size();
688*9880d681SAndroid Build Coastguard Worker for (MachineDomTreeNode *Child : Children)
689*9880d681SAndroid Build Coastguard Worker WorkList.push_back(Child);
690*9880d681SAndroid Build Coastguard Worker } while (!WorkList.empty());
691*9880d681SAndroid Build Coastguard Worker
692*9880d681SAndroid Build Coastguard Worker // Now perform CSE.
693*9880d681SAndroid Build Coastguard Worker bool Changed = false;
694*9880d681SAndroid Build Coastguard Worker for (MachineDomTreeNode *Node : Scopes) {
695*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = Node->getBlock();
696*9880d681SAndroid Build Coastguard Worker EnterScope(MBB);
697*9880d681SAndroid Build Coastguard Worker Changed |= ProcessBlock(MBB);
698*9880d681SAndroid Build Coastguard Worker // If it's a leaf node, it's done. Traverse upwards to pop ancestors.
699*9880d681SAndroid Build Coastguard Worker ExitScopeIfDone(Node, OpenChildren);
700*9880d681SAndroid Build Coastguard Worker }
701*9880d681SAndroid Build Coastguard Worker
702*9880d681SAndroid Build Coastguard Worker return Changed;
703*9880d681SAndroid Build Coastguard Worker }
704*9880d681SAndroid Build Coastguard Worker
runOnMachineFunction(MachineFunction & MF)705*9880d681SAndroid Build Coastguard Worker bool MachineCSE::runOnMachineFunction(MachineFunction &MF) {
706*9880d681SAndroid Build Coastguard Worker if (skipFunction(*MF.getFunction()))
707*9880d681SAndroid Build Coastguard Worker return false;
708*9880d681SAndroid Build Coastguard Worker
709*9880d681SAndroid Build Coastguard Worker TII = MF.getSubtarget().getInstrInfo();
710*9880d681SAndroid Build Coastguard Worker TRI = MF.getSubtarget().getRegisterInfo();
711*9880d681SAndroid Build Coastguard Worker MRI = &MF.getRegInfo();
712*9880d681SAndroid Build Coastguard Worker AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
713*9880d681SAndroid Build Coastguard Worker DT = &getAnalysis<MachineDominatorTree>();
714*9880d681SAndroid Build Coastguard Worker LookAheadLimit = TII->getMachineCSELookAheadLimit();
715*9880d681SAndroid Build Coastguard Worker return PerformCSE(DT->getRootNode());
716*9880d681SAndroid Build Coastguard Worker }
717