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