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