Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 1 | //===-- MachineCSE.cpp - Machine Common Subexpression Elimination Pass ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass performs global common subexpression elimination on machine |
Evan Cheng | c5bbba1 | 2010-03-02 19:02:27 +0000 | [diff] [blame] | 11 | // instructions using a scoped hash table based value numbering scheme. It |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 12 | // must be run while the machine function is still in SSA form. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #define DEBUG_TYPE "machine-cse" |
| 17 | #include "llvm/CodeGen/Passes.h" |
| 18 | #include "llvm/CodeGen/MachineDominators.h" |
| 19 | #include "llvm/CodeGen/MachineInstr.h" |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Evan Cheng | a5f32cb | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/AliasAnalysis.h" |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetInstrInfo.h" |
Evan Cheng | 3115698 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/DenseMap.h" |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/ScopedHashTable.h" |
Evan Cheng | 189c1ec | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallSet.h" |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/Statistic.h" |
| 27 | #include "llvm/Support/Debug.h" |
Cameron Zwarich | 53eeba5 | 2011-01-03 04:07:46 +0000 | [diff] [blame] | 28 | #include "llvm/Support/RecyclingAllocator.h" |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace llvm; |
| 31 | |
Evan Cheng | 16b48b8 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 32 | STATISTIC(NumCoalesces, "Number of copies coalesced"); |
| 33 | STATISTIC(NumCSEs, "Number of common subexpression eliminated"); |
Evan Cheng | 189c1ec | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 34 | STATISTIC(NumPhysCSEs, |
| 35 | "Number of physreg referencing common subexpr eliminated"); |
Eli Friedman | baf717a | 2011-05-04 22:10:36 +0000 | [diff] [blame] | 36 | STATISTIC(NumCrossBlockPhysCSEs, |
| 37 | "Number of physreg common subexprs cross-block eliminated"); |
Evan Cheng | a63cde2 | 2010-12-15 22:16:21 +0000 | [diff] [blame] | 38 | STATISTIC(NumCommutes, "Number of copies coalesced after commuting"); |
Bob Wilson | 3844173 | 2010-06-03 18:28:31 +0000 | [diff] [blame] | 39 | |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 40 | namespace { |
| 41 | class MachineCSE : public MachineFunctionPass { |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 42 | const TargetInstrInfo *TII; |
Evan Cheng | b3958e8 | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 43 | const TargetRegisterInfo *TRI; |
Evan Cheng | a5f32cb | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 44 | AliasAnalysis *AA; |
Evan Cheng | 31f94c7 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 45 | MachineDominatorTree *DT; |
| 46 | MachineRegisterInfo *MRI; |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 47 | public: |
| 48 | static char ID; // Pass identification |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 49 | MachineCSE() : MachineFunctionPass(ID), LookAheadLimit(5), CurrVN(0) { |
| 50 | initializeMachineCSEPass(*PassRegistry::getPassRegistry()); |
| 51 | } |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 52 | |
| 53 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 54 | |
| 55 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 56 | AU.setPreservesCFG(); |
| 57 | MachineFunctionPass::getAnalysisUsage(AU); |
Evan Cheng | a5f32cb | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 58 | AU.addRequired<AliasAnalysis>(); |
Evan Cheng | 6542416 | 2010-08-17 20:57:42 +0000 | [diff] [blame] | 59 | AU.addPreservedID(MachineLoopInfoID); |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 60 | AU.addRequired<MachineDominatorTree>(); |
| 61 | AU.addPreserved<MachineDominatorTree>(); |
| 62 | } |
| 63 | |
Evan Cheng | c2b768f | 2010-09-17 21:59:42 +0000 | [diff] [blame] | 64 | virtual void releaseMemory() { |
| 65 | ScopeMap.clear(); |
| 66 | Exps.clear(); |
| 67 | } |
| 68 | |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 69 | private: |
Evan Cheng | 835810b | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 70 | const unsigned LookAheadLimit; |
Cameron Zwarich | 53eeba5 | 2011-01-03 04:07:46 +0000 | [diff] [blame] | 71 | typedef RecyclingAllocator<BumpPtrAllocator, |
| 72 | ScopedHashTableVal<MachineInstr*, unsigned> > AllocatorTy; |
| 73 | typedef ScopedHashTable<MachineInstr*, unsigned, |
| 74 | MachineInstrExpressionTrait, AllocatorTy> ScopedHTType; |
| 75 | typedef ScopedHTType::ScopeTy ScopeType; |
Evan Cheng | 3115698 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 76 | DenseMap<MachineBasicBlock*, ScopeType*> ScopeMap; |
Cameron Zwarich | 53eeba5 | 2011-01-03 04:07:46 +0000 | [diff] [blame] | 77 | ScopedHTType VNT; |
Evan Cheng | 16b48b8 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 78 | SmallVector<MachineInstr*, 64> Exps; |
Evan Cheng | 3115698 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 79 | unsigned CurrVN; |
Evan Cheng | 16b48b8 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 80 | |
Evan Cheng | a5f32cb | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 81 | bool PerformTrivialCoalescing(MachineInstr *MI, MachineBasicBlock *MBB); |
Evan Cheng | b3958e8 | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 82 | bool isPhysDefTriviallyDead(unsigned Reg, |
| 83 | MachineBasicBlock::const_iterator I, |
Evan Cheng | 835810b | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 84 | MachineBasicBlock::const_iterator E) const ; |
Evan Cheng | 189c1ec | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 85 | bool hasLivePhysRegDefUses(const MachineInstr *MI, |
| 86 | const MachineBasicBlock *MBB, |
Eli Friedman | baf717a | 2011-05-04 22:10:36 +0000 | [diff] [blame] | 87 | SmallSet<unsigned,8> &PhysRefs, |
| 88 | SmallVector<unsigned,8> &PhysDefs) const; |
Evan Cheng | 189c1ec | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 89 | bool PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *MI, |
| 90 | SmallSet<unsigned,8> &PhysRefs) const; |
Evan Cheng | a5f32cb | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 91 | bool isCSECandidate(MachineInstr *MI); |
Evan Cheng | 2938a00 | 2010-03-10 02:12:03 +0000 | [diff] [blame] | 92 | bool isProfitableToCSE(unsigned CSReg, unsigned Reg, |
| 93 | MachineInstr *CSMI, MachineInstr *MI); |
Evan Cheng | 3115698 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 94 | void EnterScope(MachineBasicBlock *MBB); |
| 95 | void ExitScope(MachineBasicBlock *MBB); |
| 96 | bool ProcessBlock(MachineBasicBlock *MBB); |
| 97 | void ExitScopeIfDone(MachineDomTreeNode *Node, |
| 98 | DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren, |
| 99 | DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> &ParentMap); |
| 100 | bool PerformCSE(MachineDomTreeNode *Node); |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 101 | }; |
| 102 | } // end anonymous namespace |
| 103 | |
| 104 | char MachineCSE::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 105 | INITIALIZE_PASS_BEGIN(MachineCSE, "machine-cse", |
| 106 | "Machine Common Subexpression Elimination", false, false) |
| 107 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
| 108 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 109 | INITIALIZE_PASS_END(MachineCSE, "machine-cse", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 110 | "Machine Common Subexpression Elimination", false, false) |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 111 | |
| 112 | FunctionPass *llvm::createMachineCSEPass() { return new MachineCSE(); } |
| 113 | |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 114 | bool MachineCSE::PerformTrivialCoalescing(MachineInstr *MI, |
| 115 | MachineBasicBlock *MBB) { |
| 116 | bool Changed = false; |
| 117 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 118 | MachineOperand &MO = MI->getOperand(i); |
Evan Cheng | 16b48b8 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 119 | if (!MO.isReg() || !MO.isUse()) |
| 120 | continue; |
| 121 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 122 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
Evan Cheng | 16b48b8 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 123 | continue; |
Evan Cheng | f437f73 | 2010-09-17 21:56:26 +0000 | [diff] [blame] | 124 | if (!MRI->hasOneNonDBGUse(Reg)) |
Evan Cheng | 16b48b8 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 125 | // Only coalesce single use copies. This ensure the copy will be |
| 126 | // deleted. |
| 127 | continue; |
| 128 | MachineInstr *DefMI = MRI->getVRegDef(Reg); |
| 129 | if (DefMI->getParent() != MBB) |
| 130 | continue; |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 131 | if (!DefMI->isCopy()) |
| 132 | continue; |
Jakob Stoklund Olesen | 04c528a | 2010-07-16 04:45:42 +0000 | [diff] [blame] | 133 | unsigned SrcReg = DefMI->getOperand(1).getReg(); |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 134 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) |
| 135 | continue; |
| 136 | if (DefMI->getOperand(0).getSubReg() || DefMI->getOperand(1).getSubReg()) |
| 137 | continue; |
Jakob Stoklund Olesen | bf4699c | 2010-10-06 23:54:39 +0000 | [diff] [blame] | 138 | if (!MRI->constrainRegClass(SrcReg, MRI->getRegClass(Reg))) |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 139 | continue; |
| 140 | DEBUG(dbgs() << "Coalescing: " << *DefMI); |
Jakob Stoklund Olesen | bf4699c | 2010-10-06 23:54:39 +0000 | [diff] [blame] | 141 | DEBUG(dbgs() << "*** to: " << *MI); |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 142 | MO.setReg(SrcReg); |
| 143 | MRI->clearKillFlags(SrcReg); |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 144 | DefMI->eraseFromParent(); |
| 145 | ++NumCoalesces; |
| 146 | Changed = true; |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | return Changed; |
| 150 | } |
| 151 | |
Evan Cheng | 835810b | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 152 | bool |
| 153 | MachineCSE::isPhysDefTriviallyDead(unsigned Reg, |
| 154 | MachineBasicBlock::const_iterator I, |
| 155 | MachineBasicBlock::const_iterator E) const { |
Eric Christopher | e81d010 | 2010-05-21 23:40:03 +0000 | [diff] [blame] | 156 | unsigned LookAheadLeft = LookAheadLimit; |
Evan Cheng | 112e5e7 | 2010-03-23 20:33:48 +0000 | [diff] [blame] | 157 | while (LookAheadLeft) { |
Evan Cheng | 2250425 | 2010-03-24 01:50:28 +0000 | [diff] [blame] | 158 | // Skip over dbg_value's. |
| 159 | while (I != E && I->isDebugValue()) |
| 160 | ++I; |
| 161 | |
Evan Cheng | b3958e8 | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 162 | if (I == E) |
| 163 | // Reached end of block, register is obviously dead. |
| 164 | return true; |
| 165 | |
Evan Cheng | b3958e8 | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 166 | bool SeenDef = false; |
| 167 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 168 | const MachineOperand &MO = I->getOperand(i); |
| 169 | if (!MO.isReg() || !MO.getReg()) |
| 170 | continue; |
| 171 | if (!TRI->regsOverlap(MO.getReg(), Reg)) |
| 172 | continue; |
| 173 | if (MO.isUse()) |
Evan Cheng | 835810b | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 174 | // Found a use! |
Evan Cheng | b3958e8 | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 175 | return false; |
| 176 | SeenDef = true; |
| 177 | } |
| 178 | if (SeenDef) |
| 179 | // See a def of Reg (or an alias) before encountering any use, it's |
| 180 | // trivially dead. |
| 181 | return true; |
Evan Cheng | 112e5e7 | 2010-03-23 20:33:48 +0000 | [diff] [blame] | 182 | |
| 183 | --LookAheadLeft; |
Evan Cheng | b3958e8 | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 184 | ++I; |
| 185 | } |
| 186 | return false; |
| 187 | } |
| 188 | |
Evan Cheng | 189c1ec | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 189 | /// hasLivePhysRegDefUses - Return true if the specified instruction read/write |
Evan Cheng | 835810b | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 190 | /// physical registers (except for dead defs of physical registers). It also |
Evan Cheng | 2b4e727 | 2010-06-04 23:28:13 +0000 | [diff] [blame] | 191 | /// returns the physical register def by reference if it's the only one and the |
| 192 | /// instruction does not uses a physical register. |
Evan Cheng | 189c1ec | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 193 | bool MachineCSE::hasLivePhysRegDefUses(const MachineInstr *MI, |
| 194 | const MachineBasicBlock *MBB, |
Eli Friedman | baf717a | 2011-05-04 22:10:36 +0000 | [diff] [blame] | 195 | SmallSet<unsigned,8> &PhysRefs, |
| 196 | SmallVector<unsigned,8> &PhysDefs) const{ |
Evan Cheng | 189c1ec | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 197 | MachineBasicBlock::const_iterator I = MI; I = llvm::next(I); |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 198 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
Evan Cheng | 835810b | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 199 | const MachineOperand &MO = MI->getOperand(i); |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 200 | if (!MO.isReg()) |
| 201 | continue; |
| 202 | unsigned Reg = MO.getReg(); |
| 203 | if (!Reg) |
| 204 | continue; |
Evan Cheng | 835810b | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 205 | if (TargetRegisterInfo::isVirtualRegister(Reg)) |
| 206 | continue; |
Evan Cheng | 189c1ec | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 207 | // If the def is dead, it's ok. But the def may not marked "dead". That's |
Evan Cheng | 835810b | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 208 | // common since this pass is run before livevariables. We can scan |
| 209 | // forward a few instructions and check if it is obviously dead. |
Evan Cheng | 189c1ec | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 210 | if (MO.isDef() && |
| 211 | (MO.isDead() || isPhysDefTriviallyDead(Reg, I, MBB->end()))) |
| 212 | continue; |
Eli Friedman | baf717a | 2011-05-04 22:10:36 +0000 | [diff] [blame] | 213 | PhysDefs.push_back(Reg); |
Evan Cheng | 189c1ec | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 214 | PhysRefs.insert(Reg); |
| 215 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) |
| 216 | PhysRefs.insert(*Alias); |
Evan Cheng | b3958e8 | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Evan Cheng | 189c1ec | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 219 | return !PhysRefs.empty(); |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Evan Cheng | 189c1ec | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 222 | bool MachineCSE::PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *MI, |
| 223 | SmallSet<unsigned,8> &PhysRefs) const { |
Eli Friedman | baf717a | 2011-05-04 22:10:36 +0000 | [diff] [blame] | 224 | // Look backward from MI to find CSMI. |
Evan Cheng | 835810b | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 225 | unsigned LookAheadLeft = LookAheadLimit; |
Eli Friedman | baf717a | 2011-05-04 22:10:36 +0000 | [diff] [blame] | 226 | MachineBasicBlock *CurBB = MI->getParent(); |
| 227 | MachineBasicBlock::const_reverse_iterator I(MI); |
| 228 | MachineBasicBlock::const_reverse_iterator E(CurBB->rend()); |
Evan Cheng | 835810b | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 229 | while (LookAheadLeft) { |
Eli Friedman | baf717a | 2011-05-04 22:10:36 +0000 | [diff] [blame] | 230 | while (LookAheadLeft && I != E) { |
| 231 | // Skip over dbg_value's. |
| 232 | while (I != E && I->isDebugValue()) |
| 233 | ++I; |
| 234 | |
| 235 | if (I == E) break; |
| 236 | |
| 237 | if (&*I == CSMI) |
| 238 | return true; |
| 239 | |
| 240 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 241 | const MachineOperand &MO = I->getOperand(i); |
| 242 | if (!MO.isReg() || !MO.isDef()) |
| 243 | continue; |
| 244 | unsigned MOReg = MO.getReg(); |
| 245 | if (TargetRegisterInfo::isVirtualRegister(MOReg)) |
| 246 | continue; |
| 247 | if (PhysRefs.count(MOReg)) |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | --LookAheadLeft; |
Evan Cheng | 835810b | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 252 | ++I; |
Evan Cheng | 189c1ec | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 253 | } |
Eli Friedman | baf717a | 2011-05-04 22:10:36 +0000 | [diff] [blame] | 254 | // Go back another BB; for now, only go back at most one BB. |
| 255 | MachineBasicBlock *CSBB = CSMI->getParent(); |
| 256 | if (!CSBB->isSuccessor(CurBB) || CurBB->pred_size() != 1) |
| 257 | return false; |
| 258 | CurBB = CSBB; |
| 259 | I = CSBB->rbegin(); |
| 260 | E = CSBB->rend(); |
Evan Cheng | 835810b | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | return false; |
| 264 | } |
| 265 | |
Evan Cheng | a5f32cb | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 266 | bool MachineCSE::isCSECandidate(MachineInstr *MI) { |
Evan Cheng | 5196018 | 2010-03-08 23:49:12 +0000 | [diff] [blame] | 267 | if (MI->isLabel() || MI->isPHI() || MI->isImplicitDef() || |
Dale Johannesen | e68ea06 | 2010-03-11 02:10:24 +0000 | [diff] [blame] | 268 | MI->isKill() || MI->isInlineAsm() || MI->isDebugValue()) |
Evan Cheng | 5196018 | 2010-03-08 23:49:12 +0000 | [diff] [blame] | 269 | return false; |
| 270 | |
Evan Cheng | 2938a00 | 2010-03-10 02:12:03 +0000 | [diff] [blame] | 271 | // Ignore copies. |
Jakob Stoklund Olesen | 04c528a | 2010-07-16 04:45:42 +0000 | [diff] [blame] | 272 | if (MI->isCopyLike()) |
Evan Cheng | a5f32cb | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 273 | return false; |
| 274 | |
| 275 | // Ignore stuff that we obviously can't move. |
| 276 | const TargetInstrDesc &TID = MI->getDesc(); |
| 277 | if (TID.mayStore() || TID.isCall() || TID.isTerminator() || |
Evan Cheng | c36b706 | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 278 | MI->hasUnmodeledSideEffects()) |
Evan Cheng | a5f32cb | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 279 | return false; |
| 280 | |
| 281 | if (TID.mayLoad()) { |
| 282 | // Okay, this instruction does a load. As a refinement, we allow the target |
| 283 | // to decide whether the loaded value is actually a constant. If so, we can |
| 284 | // actually use it as a load. |
| 285 | if (!MI->isInvariantLoad(AA)) |
| 286 | // FIXME: we should be able to hoist loads with no other side effects if |
| 287 | // there are no other instructions which can change memory in this loop. |
| 288 | // This is a trivial form of alias analysis. |
| 289 | return false; |
| 290 | } |
| 291 | return true; |
| 292 | } |
| 293 | |
Evan Cheng | 31f94c7 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 294 | /// isProfitableToCSE - Return true if it's profitable to eliminate MI with a |
| 295 | /// common expression that defines Reg. |
Evan Cheng | 2938a00 | 2010-03-10 02:12:03 +0000 | [diff] [blame] | 296 | bool MachineCSE::isProfitableToCSE(unsigned CSReg, unsigned Reg, |
| 297 | MachineInstr *CSMI, MachineInstr *MI) { |
| 298 | // FIXME: Heuristics that works around the lack the live range splitting. |
| 299 | |
Chris Lattner | 622a11b | 2011-01-10 07:51:31 +0000 | [diff] [blame] | 300 | // Heuristics #1: Don't CSE "cheap" computation if the def is not local or in |
| 301 | // an immediate predecessor. We don't want to increase register pressure and |
| 302 | // end up causing other computation to be spilled. |
Evan Cheng | 2938a00 | 2010-03-10 02:12:03 +0000 | [diff] [blame] | 303 | if (MI->getDesc().isAsCheapAsAMove()) { |
| 304 | MachineBasicBlock *CSBB = CSMI->getParent(); |
| 305 | MachineBasicBlock *BB = MI->getParent(); |
Chris Lattner | 622a11b | 2011-01-10 07:51:31 +0000 | [diff] [blame] | 306 | if (CSBB != BB && !CSBB->isSuccessor(BB)) |
Evan Cheng | 2938a00 | 2010-03-10 02:12:03 +0000 | [diff] [blame] | 307 | return false; |
| 308 | } |
| 309 | |
| 310 | // Heuristics #2: If the expression doesn't not use a vr and the only use |
| 311 | // of the redundant computation are copies, do not cse. |
| 312 | bool HasVRegUse = false; |
| 313 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 314 | const MachineOperand &MO = MI->getOperand(i); |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 315 | if (MO.isReg() && MO.isUse() && |
Evan Cheng | 2938a00 | 2010-03-10 02:12:03 +0000 | [diff] [blame] | 316 | TargetRegisterInfo::isVirtualRegister(MO.getReg())) { |
| 317 | HasVRegUse = true; |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | if (!HasVRegUse) { |
| 322 | bool HasNonCopyUse = false; |
| 323 | for (MachineRegisterInfo::use_nodbg_iterator I = MRI->use_nodbg_begin(Reg), |
| 324 | E = MRI->use_nodbg_end(); I != E; ++I) { |
| 325 | MachineInstr *Use = &*I; |
| 326 | // Ignore copies. |
Jakob Stoklund Olesen | 04c528a | 2010-07-16 04:45:42 +0000 | [diff] [blame] | 327 | if (!Use->isCopyLike()) { |
Evan Cheng | 2938a00 | 2010-03-10 02:12:03 +0000 | [diff] [blame] | 328 | HasNonCopyUse = true; |
| 329 | break; |
| 330 | } |
| 331 | } |
| 332 | if (!HasNonCopyUse) |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | // Heuristics #3: If the common subexpression is used by PHIs, do not reuse |
| 337 | // it unless the defined value is already used in the BB of the new use. |
Evan Cheng | 31f94c7 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 338 | bool HasPHI = false; |
| 339 | SmallPtrSet<MachineBasicBlock*, 4> CSBBs; |
Evan Cheng | 2938a00 | 2010-03-10 02:12:03 +0000 | [diff] [blame] | 340 | for (MachineRegisterInfo::use_nodbg_iterator I = MRI->use_nodbg_begin(CSReg), |
Evan Cheng | 31f94c7 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 341 | E = MRI->use_nodbg_end(); I != E; ++I) { |
| 342 | MachineInstr *Use = &*I; |
| 343 | HasPHI |= Use->isPHI(); |
| 344 | CSBBs.insert(Use->getParent()); |
| 345 | } |
| 346 | |
| 347 | if (!HasPHI) |
| 348 | return true; |
| 349 | return CSBBs.count(MI->getParent()); |
| 350 | } |
| 351 | |
Evan Cheng | 3115698 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 352 | void MachineCSE::EnterScope(MachineBasicBlock *MBB) { |
| 353 | DEBUG(dbgs() << "Entering: " << MBB->getName() << '\n'); |
| 354 | ScopeType *Scope = new ScopeType(VNT); |
| 355 | ScopeMap[MBB] = Scope; |
| 356 | } |
| 357 | |
| 358 | void MachineCSE::ExitScope(MachineBasicBlock *MBB) { |
| 359 | DEBUG(dbgs() << "Exiting: " << MBB->getName() << '\n'); |
| 360 | DenseMap<MachineBasicBlock*, ScopeType*>::iterator SI = ScopeMap.find(MBB); |
| 361 | assert(SI != ScopeMap.end()); |
| 362 | ScopeMap.erase(SI); |
| 363 | delete SI->second; |
| 364 | } |
| 365 | |
| 366 | bool MachineCSE::ProcessBlock(MachineBasicBlock *MBB) { |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 367 | bool Changed = false; |
| 368 | |
Evan Cheng | 31f94c7 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 369 | SmallVector<std::pair<unsigned, unsigned>, 8> CSEPairs; |
Evan Cheng | 16b48b8 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 370 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E; ) { |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 371 | MachineInstr *MI = &*I; |
Evan Cheng | 16b48b8 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 372 | ++I; |
Evan Cheng | a5f32cb | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 373 | |
| 374 | if (!isCSECandidate(MI)) |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 375 | continue; |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 376 | |
| 377 | bool FoundCSE = VNT.count(MI); |
| 378 | if (!FoundCSE) { |
| 379 | // Look for trivial copy coalescing opportunities. |
Evan Cheng | db8771a | 2010-04-02 02:21:24 +0000 | [diff] [blame] | 380 | if (PerformTrivialCoalescing(MI, MBB)) { |
Evan Cheng | cfea985 | 2011-04-11 18:47:20 +0000 | [diff] [blame] | 381 | Changed = true; |
| 382 | |
Evan Cheng | db8771a | 2010-04-02 02:21:24 +0000 | [diff] [blame] | 383 | // After coalescing MI itself may become a copy. |
Jakob Stoklund Olesen | 04c528a | 2010-07-16 04:45:42 +0000 | [diff] [blame] | 384 | if (MI->isCopyLike()) |
Evan Cheng | db8771a | 2010-04-02 02:21:24 +0000 | [diff] [blame] | 385 | continue; |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 386 | FoundCSE = VNT.count(MI); |
Evan Cheng | db8771a | 2010-04-02 02:21:24 +0000 | [diff] [blame] | 387 | } |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 388 | } |
Evan Cheng | a63cde2 | 2010-12-15 22:16:21 +0000 | [diff] [blame] | 389 | |
| 390 | // Commute commutable instructions. |
| 391 | bool Commuted = false; |
| 392 | if (!FoundCSE && MI->getDesc().isCommutable()) { |
| 393 | MachineInstr *NewMI = TII->commuteInstruction(MI); |
| 394 | if (NewMI) { |
| 395 | Commuted = true; |
| 396 | FoundCSE = VNT.count(NewMI); |
Evan Cheng | cfea985 | 2011-04-11 18:47:20 +0000 | [diff] [blame] | 397 | if (NewMI != MI) { |
Evan Cheng | a63cde2 | 2010-12-15 22:16:21 +0000 | [diff] [blame] | 398 | // New instruction. It doesn't need to be kept. |
| 399 | NewMI->eraseFromParent(); |
Evan Cheng | cfea985 | 2011-04-11 18:47:20 +0000 | [diff] [blame] | 400 | Changed = true; |
| 401 | } else if (!FoundCSE) |
Evan Cheng | a63cde2 | 2010-12-15 22:16:21 +0000 | [diff] [blame] | 402 | // MI was changed but it didn't help, commute it back! |
| 403 | (void)TII->commuteInstruction(MI); |
| 404 | } |
| 405 | } |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 406 | |
Evan Cheng | 189c1ec | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 407 | // If the instruction defines physical registers and the values *may* be |
Evan Cheng | 67bda72 | 2010-03-03 23:59:08 +0000 | [diff] [blame] | 408 | // used, then it's not safe to replace it with a common subexpression. |
Evan Cheng | 189c1ec | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 409 | // It's also not safe if the instruction uses physical registers. |
| 410 | SmallSet<unsigned,8> PhysRefs; |
Eli Friedman | baf717a | 2011-05-04 22:10:36 +0000 | [diff] [blame] | 411 | SmallVector<unsigned,8> DirectPhysRefs; |
| 412 | if (FoundCSE && hasLivePhysRegDefUses(MI, MBB, PhysRefs, DirectPhysRefs)) { |
Evan Cheng | 67bda72 | 2010-03-03 23:59:08 +0000 | [diff] [blame] | 413 | FoundCSE = false; |
| 414 | |
Evan Cheng | 835810b | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 415 | // ... Unless the CS is local and it also defines the physical register |
Evan Cheng | 189c1ec | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 416 | // which is not clobbered in between and the physical register uses |
| 417 | // were not clobbered. |
| 418 | unsigned CSVN = VNT.lookup(MI); |
| 419 | MachineInstr *CSMI = Exps[CSVN]; |
| 420 | if (PhysRegDefsReach(CSMI, MI, PhysRefs)) |
| 421 | FoundCSE = true; |
Evan Cheng | 835810b | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Evan Cheng | 16b48b8 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 424 | if (!FoundCSE) { |
| 425 | VNT.insert(MI, CurrVN++); |
| 426 | Exps.push_back(MI); |
| 427 | continue; |
| 428 | } |
| 429 | |
| 430 | // Found a common subexpression, eliminate it. |
| 431 | unsigned CSVN = VNT.lookup(MI); |
| 432 | MachineInstr *CSMI = Exps[CSVN]; |
| 433 | DEBUG(dbgs() << "Examining: " << *MI); |
| 434 | DEBUG(dbgs() << "*** Found a common subexpression: " << *CSMI); |
Evan Cheng | 31f94c7 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 435 | |
| 436 | // Check if it's profitable to perform this CSE. |
| 437 | bool DoCSE = true; |
Evan Cheng | 16b48b8 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 438 | unsigned NumDefs = MI->getDesc().getNumDefs(); |
| 439 | for (unsigned i = 0, e = MI->getNumOperands(); NumDefs && i != e; ++i) { |
| 440 | MachineOperand &MO = MI->getOperand(i); |
| 441 | if (!MO.isReg() || !MO.isDef()) |
| 442 | continue; |
| 443 | unsigned OldReg = MO.getReg(); |
| 444 | unsigned NewReg = CSMI->getOperand(i).getReg(); |
Evan Cheng | 6cc1aea | 2010-03-06 01:14:19 +0000 | [diff] [blame] | 445 | if (OldReg == NewReg) |
| 446 | continue; |
| 447 | assert(TargetRegisterInfo::isVirtualRegister(OldReg) && |
Evan Cheng | 16b48b8 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 448 | TargetRegisterInfo::isVirtualRegister(NewReg) && |
| 449 | "Do not CSE physical register defs!"); |
Evan Cheng | 2938a00 | 2010-03-10 02:12:03 +0000 | [diff] [blame] | 450 | if (!isProfitableToCSE(NewReg, OldReg, CSMI, MI)) { |
Evan Cheng | 31f94c7 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 451 | DoCSE = false; |
| 452 | break; |
| 453 | } |
| 454 | CSEPairs.push_back(std::make_pair(OldReg, NewReg)); |
Evan Cheng | 16b48b8 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 455 | --NumDefs; |
| 456 | } |
Evan Cheng | 31f94c7 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 457 | |
| 458 | // Actually perform the elimination. |
| 459 | if (DoCSE) { |
Dan Gohman | 49b4589 | 2010-05-13 19:24:00 +0000 | [diff] [blame] | 460 | for (unsigned i = 0, e = CSEPairs.size(); i != e; ++i) { |
Evan Cheng | 31f94c7 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 461 | MRI->replaceRegWith(CSEPairs[i].first, CSEPairs[i].second); |
Dan Gohman | 49b4589 | 2010-05-13 19:24:00 +0000 | [diff] [blame] | 462 | MRI->clearKillFlags(CSEPairs[i].second); |
| 463 | } |
Evan Cheng | 31f94c7 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 464 | MI->eraseFromParent(); |
Eli Friedman | baf717a | 2011-05-04 22:10:36 +0000 | [diff] [blame] | 465 | if (!DirectPhysRefs.empty() && CSMI->getParent() != MBB) { |
| 466 | assert(CSMI->getParent()->isSuccessor(MBB)); |
| 467 | ++NumCrossBlockPhysCSEs; |
| 468 | SmallVector<unsigned,8>::iterator PI = DirectPhysRefs.begin(), |
| 469 | PE = DirectPhysRefs.end(); |
| 470 | for (; PI != PE; ++PI) |
Eli Friedman | 5f6bf5d | 2011-05-05 16:18:11 +0000 | [diff] [blame] | 471 | if (!MBB->isLiveIn(*PI)) |
| 472 | MBB->addLiveIn(*PI); |
Eli Friedman | baf717a | 2011-05-04 22:10:36 +0000 | [diff] [blame] | 473 | } |
Evan Cheng | 31f94c7 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 474 | ++NumCSEs; |
Evan Cheng | 189c1ec | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 475 | if (!PhysRefs.empty()) |
Evan Cheng | 2b4e727 | 2010-06-04 23:28:13 +0000 | [diff] [blame] | 476 | ++NumPhysCSEs; |
Evan Cheng | a63cde2 | 2010-12-15 22:16:21 +0000 | [diff] [blame] | 477 | if (Commuted) |
| 478 | ++NumCommutes; |
Evan Cheng | cfea985 | 2011-04-11 18:47:20 +0000 | [diff] [blame] | 479 | Changed = true; |
Evan Cheng | 31f94c7 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 480 | } else { |
| 481 | DEBUG(dbgs() << "*** Not profitable, avoid CSE!\n"); |
| 482 | VNT.insert(MI, CurrVN++); |
| 483 | Exps.push_back(MI); |
| 484 | } |
| 485 | CSEPairs.clear(); |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Evan Cheng | 3115698 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 488 | return Changed; |
| 489 | } |
| 490 | |
| 491 | /// ExitScopeIfDone - Destroy scope for the MBB that corresponds to the given |
| 492 | /// dominator tree node if its a leaf or all of its children are done. Walk |
| 493 | /// up the dominator tree to destroy ancestors which are now done. |
| 494 | void |
| 495 | MachineCSE::ExitScopeIfDone(MachineDomTreeNode *Node, |
| 496 | DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren, |
| 497 | DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> &ParentMap) { |
| 498 | if (OpenChildren[Node]) |
| 499 | return; |
| 500 | |
| 501 | // Pop scope. |
| 502 | ExitScope(Node->getBlock()); |
| 503 | |
| 504 | // Now traverse upwards to pop ancestors whose offsprings are all done. |
| 505 | while (MachineDomTreeNode *Parent = ParentMap[Node]) { |
| 506 | unsigned Left = --OpenChildren[Parent]; |
| 507 | if (Left != 0) |
| 508 | break; |
| 509 | ExitScope(Parent->getBlock()); |
| 510 | Node = Parent; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | bool MachineCSE::PerformCSE(MachineDomTreeNode *Node) { |
| 515 | SmallVector<MachineDomTreeNode*, 32> Scopes; |
| 516 | SmallVector<MachineDomTreeNode*, 8> WorkList; |
| 517 | DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> ParentMap; |
| 518 | DenseMap<MachineDomTreeNode*, unsigned> OpenChildren; |
| 519 | |
Evan Cheng | c2b768f | 2010-09-17 21:59:42 +0000 | [diff] [blame] | 520 | CurrVN = 0; |
| 521 | |
Evan Cheng | 3115698 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 522 | // Perform a DFS walk to determine the order of visit. |
| 523 | WorkList.push_back(Node); |
| 524 | do { |
| 525 | Node = WorkList.pop_back_val(); |
| 526 | Scopes.push_back(Node); |
| 527 | const std::vector<MachineDomTreeNode*> &Children = Node->getChildren(); |
| 528 | unsigned NumChildren = Children.size(); |
| 529 | OpenChildren[Node] = NumChildren; |
| 530 | for (unsigned i = 0; i != NumChildren; ++i) { |
| 531 | MachineDomTreeNode *Child = Children[i]; |
| 532 | ParentMap[Child] = Node; |
| 533 | WorkList.push_back(Child); |
| 534 | } |
| 535 | } while (!WorkList.empty()); |
| 536 | |
| 537 | // Now perform CSE. |
| 538 | bool Changed = false; |
| 539 | for (unsigned i = 0, e = Scopes.size(); i != e; ++i) { |
| 540 | MachineDomTreeNode *Node = Scopes[i]; |
| 541 | MachineBasicBlock *MBB = Node->getBlock(); |
| 542 | EnterScope(MBB); |
| 543 | Changed |= ProcessBlock(MBB); |
| 544 | // If it's a leaf node, it's done. Traverse upwards to pop ancestors. |
| 545 | ExitScopeIfDone(Node, OpenChildren, ParentMap); |
| 546 | } |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 547 | |
| 548 | return Changed; |
| 549 | } |
| 550 | |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 551 | bool MachineCSE::runOnMachineFunction(MachineFunction &MF) { |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 552 | TII = MF.getTarget().getInstrInfo(); |
Evan Cheng | b3958e8 | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 553 | TRI = MF.getTarget().getRegisterInfo(); |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 554 | MRI = &MF.getRegInfo(); |
Evan Cheng | a5f32cb | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 555 | AA = &getAnalysis<AliasAnalysis>(); |
Evan Cheng | 31f94c7 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 556 | DT = &getAnalysis<MachineDominatorTree>(); |
Evan Cheng | 3115698 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 557 | return PerformCSE(DT->getRootNode()); |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 558 | } |