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