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