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