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 | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/ScopedHashTable.h" |
| 24 | #include "llvm/ADT/Statistic.h" |
| 25 | #include "llvm/Support/Debug.h" |
| 26 | |
| 27 | using namespace llvm; |
| 28 | |
Evan Cheng | 16b48b8 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 29 | STATISTIC(NumCoalesces, "Number of copies coalesced"); |
| 30 | STATISTIC(NumCSEs, "Number of common subexpression eliminated"); |
| 31 | |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | class MachineCSE : public MachineFunctionPass { |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 34 | const TargetInstrInfo *TII; |
Evan Cheng | b3958e8 | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 35 | const TargetRegisterInfo *TRI; |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 36 | MachineRegisterInfo *MRI; |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 37 | MachineDominatorTree *DT; |
Evan Cheng | a5f32cb | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 38 | AliasAnalysis *AA; |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 39 | public: |
| 40 | static char ID; // Pass identification |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 41 | MachineCSE() : MachineFunctionPass(&ID), CurrVN(0) {} |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 42 | |
| 43 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 44 | |
| 45 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 46 | AU.setPreservesCFG(); |
| 47 | MachineFunctionPass::getAnalysisUsage(AU); |
Evan Cheng | a5f32cb | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 48 | AU.addRequired<AliasAnalysis>(); |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 49 | AU.addRequired<MachineDominatorTree>(); |
| 50 | AU.addPreserved<MachineDominatorTree>(); |
| 51 | } |
| 52 | |
| 53 | private: |
Evan Cheng | 16b48b8 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 54 | unsigned CurrVN; |
Evan Cheng | 05bdcbb | 2010-03-03 23:27:36 +0000 | [diff] [blame] | 55 | ScopedHashTable<MachineInstr*, unsigned, MachineInstrExpressionTrait> VNT; |
Evan Cheng | 16b48b8 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 56 | SmallVector<MachineInstr*, 64> Exps; |
| 57 | |
Evan Cheng | a5f32cb | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 58 | bool PerformTrivialCoalescing(MachineInstr *MI, MachineBasicBlock *MBB); |
Evan Cheng | b3958e8 | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 59 | bool isPhysDefTriviallyDead(unsigned Reg, |
| 60 | MachineBasicBlock::const_iterator I, |
| 61 | MachineBasicBlock::const_iterator E); |
Evan Cheng | a5f32cb | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 62 | bool hasLivePhysRegDefUse(MachineInstr *MI, MachineBasicBlock *MBB); |
| 63 | bool isCSECandidate(MachineInstr *MI); |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 64 | bool ProcessBlock(MachineDomTreeNode *Node); |
| 65 | }; |
| 66 | } // end anonymous namespace |
| 67 | |
| 68 | char MachineCSE::ID = 0; |
| 69 | static RegisterPass<MachineCSE> |
| 70 | X("machine-cse", "Machine Common Subexpression Elimination"); |
| 71 | |
| 72 | FunctionPass *llvm::createMachineCSEPass() { return new MachineCSE(); } |
| 73 | |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 74 | bool MachineCSE::PerformTrivialCoalescing(MachineInstr *MI, |
| 75 | MachineBasicBlock *MBB) { |
| 76 | bool Changed = false; |
| 77 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 78 | MachineOperand &MO = MI->getOperand(i); |
Evan Cheng | 16b48b8 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 79 | if (!MO.isReg() || !MO.isUse()) |
| 80 | continue; |
| 81 | unsigned Reg = MO.getReg(); |
| 82 | if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 83 | continue; |
| 84 | if (!MRI->hasOneUse(Reg)) |
| 85 | // Only coalesce single use copies. This ensure the copy will be |
| 86 | // deleted. |
| 87 | continue; |
| 88 | MachineInstr *DefMI = MRI->getVRegDef(Reg); |
| 89 | if (DefMI->getParent() != MBB) |
| 90 | continue; |
| 91 | unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx; |
| 92 | if (TII->isMoveInstr(*DefMI, SrcReg, DstReg, SrcSubIdx, DstSubIdx) && |
| 93 | TargetRegisterInfo::isVirtualRegister(SrcReg) && |
| 94 | !SrcSubIdx && !DstSubIdx) { |
| 95 | MO.setReg(SrcReg); |
| 96 | DefMI->eraseFromParent(); |
| 97 | ++NumCoalesces; |
| 98 | Changed = true; |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | return Changed; |
| 103 | } |
| 104 | |
Evan Cheng | b3958e8 | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 105 | bool MachineCSE::isPhysDefTriviallyDead(unsigned Reg, |
| 106 | MachineBasicBlock::const_iterator I, |
| 107 | MachineBasicBlock::const_iterator E) { |
| 108 | unsigned LookAheadLeft = 5; |
| 109 | while (LookAheadLeft--) { |
| 110 | if (I == E) |
| 111 | // Reached end of block, register is obviously dead. |
| 112 | return true; |
| 113 | |
| 114 | if (I->isDebugValue()) |
| 115 | continue; |
| 116 | bool SeenDef = false; |
| 117 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 118 | const MachineOperand &MO = I->getOperand(i); |
| 119 | if (!MO.isReg() || !MO.getReg()) |
| 120 | continue; |
| 121 | if (!TRI->regsOverlap(MO.getReg(), Reg)) |
| 122 | continue; |
| 123 | if (MO.isUse()) |
| 124 | return false; |
| 125 | SeenDef = true; |
| 126 | } |
| 127 | if (SeenDef) |
| 128 | // See a def of Reg (or an alias) before encountering any use, it's |
| 129 | // trivially dead. |
| 130 | return true; |
| 131 | ++I; |
| 132 | } |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | bool MachineCSE::hasLivePhysRegDefUse(MachineInstr *MI, MachineBasicBlock *MBB){ |
| 137 | unsigned PhysDef = 0; |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 138 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 139 | MachineOperand &MO = MI->getOperand(i); |
| 140 | if (!MO.isReg()) |
| 141 | continue; |
| 142 | unsigned Reg = MO.getReg(); |
| 143 | if (!Reg) |
| 144 | continue; |
Evan Cheng | b3958e8 | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 145 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
| 146 | if (MO.isUse()) |
| 147 | // Can't touch anything to read a physical register. |
| 148 | return true; |
| 149 | if (MO.isDead()) |
| 150 | // If the def is dead, it's ok. |
| 151 | continue; |
| 152 | // Ok, this is a physical register def that's not marked "dead". That's |
| 153 | // common since this pass is run before livevariables. We can scan |
| 154 | // forward a few instructions and check if it is obviously dead. |
| 155 | if (PhysDef) |
| 156 | // Multiple physical register defs. These are rare, forget about it. |
| 157 | return true; |
| 158 | PhysDef = Reg; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if (PhysDef) { |
| 163 | MachineBasicBlock::iterator I = MI; I = llvm::next(I); |
| 164 | if (!isPhysDefTriviallyDead(PhysDef, I, MBB->end())) |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 165 | return true; |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 166 | } |
| 167 | return false; |
| 168 | } |
| 169 | |
Evan Cheng | a5f32cb | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 170 | bool MachineCSE::isCSECandidate(MachineInstr *MI) { |
| 171 | // Ignore copies or instructions that read / write physical registers |
| 172 | // (except for dead defs of physical registers). |
| 173 | unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx; |
| 174 | if (TII->isMoveInstr(*MI, SrcReg, DstReg, SrcSubIdx, DstSubIdx) || |
| 175 | MI->isExtractSubreg() || MI->isInsertSubreg() || MI->isSubregToReg()) |
| 176 | return false; |
| 177 | |
| 178 | // Ignore stuff that we obviously can't move. |
| 179 | const TargetInstrDesc &TID = MI->getDesc(); |
| 180 | if (TID.mayStore() || TID.isCall() || TID.isTerminator() || |
| 181 | TID.hasUnmodeledSideEffects()) |
| 182 | return false; |
| 183 | |
| 184 | if (TID.mayLoad()) { |
| 185 | // Okay, this instruction does a load. As a refinement, we allow the target |
| 186 | // to decide whether the loaded value is actually a constant. If so, we can |
| 187 | // actually use it as a load. |
| 188 | if (!MI->isInvariantLoad(AA)) |
| 189 | // FIXME: we should be able to hoist loads with no other side effects if |
| 190 | // there are no other instructions which can change memory in this loop. |
| 191 | // This is a trivial form of alias analysis. |
| 192 | return false; |
| 193 | } |
| 194 | return true; |
| 195 | } |
| 196 | |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 197 | bool MachineCSE::ProcessBlock(MachineDomTreeNode *Node) { |
| 198 | bool Changed = false; |
| 199 | |
Evan Cheng | 05bdcbb | 2010-03-03 23:27:36 +0000 | [diff] [blame] | 200 | ScopedHashTableScope<MachineInstr*, unsigned, |
| 201 | MachineInstrExpressionTrait> VNTS(VNT); |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 202 | MachineBasicBlock *MBB = Node->getBlock(); |
Evan Cheng | 16b48b8 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 203 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E; ) { |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 204 | MachineInstr *MI = &*I; |
Evan Cheng | 16b48b8 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 205 | ++I; |
Evan Cheng | a5f32cb | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 206 | |
| 207 | if (!isCSECandidate(MI)) |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 208 | continue; |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 209 | |
| 210 | bool FoundCSE = VNT.count(MI); |
| 211 | if (!FoundCSE) { |
| 212 | // Look for trivial copy coalescing opportunities. |
| 213 | if (PerformTrivialCoalescing(MI, MBB)) |
| 214 | FoundCSE = VNT.count(MI); |
| 215 | } |
Evan Cheng | b3958e8 | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 216 | // FIXME: commute commutable instructions? |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 217 | |
Evan Cheng | 67bda72 | 2010-03-03 23:59:08 +0000 | [diff] [blame] | 218 | // If the instruction defines a physical register and the value *may* be |
| 219 | // 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] | 220 | if (FoundCSE && hasLivePhysRegDefUse(MI, MBB)) |
Evan Cheng | 67bda72 | 2010-03-03 23:59:08 +0000 | [diff] [blame] | 221 | FoundCSE = false; |
| 222 | |
Evan Cheng | 16b48b8 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 223 | if (!FoundCSE) { |
| 224 | VNT.insert(MI, CurrVN++); |
| 225 | Exps.push_back(MI); |
| 226 | continue; |
| 227 | } |
| 228 | |
| 229 | // Found a common subexpression, eliminate it. |
| 230 | unsigned CSVN = VNT.lookup(MI); |
| 231 | MachineInstr *CSMI = Exps[CSVN]; |
| 232 | DEBUG(dbgs() << "Examining: " << *MI); |
| 233 | DEBUG(dbgs() << "*** Found a common subexpression: " << *CSMI); |
| 234 | unsigned NumDefs = MI->getDesc().getNumDefs(); |
| 235 | for (unsigned i = 0, e = MI->getNumOperands(); NumDefs && i != e; ++i) { |
| 236 | MachineOperand &MO = MI->getOperand(i); |
| 237 | if (!MO.isReg() || !MO.isDef()) |
| 238 | continue; |
| 239 | unsigned OldReg = MO.getReg(); |
| 240 | unsigned NewReg = CSMI->getOperand(i).getReg(); |
Evan Cheng | 6cc1aea | 2010-03-06 01:14:19 +0000 | [diff] [blame] | 241 | if (OldReg == NewReg) |
| 242 | continue; |
| 243 | assert(TargetRegisterInfo::isVirtualRegister(OldReg) && |
Evan Cheng | 16b48b8 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 244 | TargetRegisterInfo::isVirtualRegister(NewReg) && |
| 245 | "Do not CSE physical register defs!"); |
| 246 | MRI->replaceRegWith(OldReg, NewReg); |
| 247 | --NumDefs; |
| 248 | } |
| 249 | MI->eraseFromParent(); |
| 250 | ++NumCSEs; |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | // Recursively call ProcessBlock with childred. |
| 254 | const std::vector<MachineDomTreeNode*> &Children = Node->getChildren(); |
| 255 | for (unsigned i = 0, e = Children.size(); i != e; ++i) |
| 256 | Changed |= ProcessBlock(Children[i]); |
| 257 | |
| 258 | return Changed; |
| 259 | } |
| 260 | |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 261 | bool MachineCSE::runOnMachineFunction(MachineFunction &MF) { |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 262 | TII = MF.getTarget().getInstrInfo(); |
Evan Cheng | b3958e8 | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 263 | TRI = MF.getTarget().getRegisterInfo(); |
Evan Cheng | 6ba9554 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 264 | MRI = &MF.getRegInfo(); |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 265 | DT = &getAnalysis<MachineDominatorTree>(); |
Evan Cheng | a5f32cb | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 266 | AA = &getAnalysis<AliasAnalysis>(); |
Evan Cheng | c6fe333 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 267 | return ProcessBlock(DT->getRootNode()); |
| 268 | } |