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