Bill Wendling | b958b0d | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1 | //===-- MachineLICM.cpp - Machine Loop Invariant Code Motion Pass ---------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Bill Wendling and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass performs loop invariant code motion on machine instructions. We |
| 11 | // attempt to remove as much code from the body of a loop as possible. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "machine-licm" |
| 16 | #include "llvm/ADT/SmallVector.h" |
| 17 | #include "llvm/ADT/Statistic.h" |
| 18 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 19 | #include "llvm/CodeGen/MachineDominators.h" |
| 20 | #include "llvm/CodeGen/MachineInstr.h" |
| 21 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 22 | #include "llvm/CodeGen/Passes.h" |
| 23 | #include "llvm/Target/TargetInstrInfo.h" |
| 24 | #include "llvm/Support/CFG.h" |
| 25 | #include "llvm/Support/CommandLine.h" |
| 26 | #include "llvm/Support/Compiler.h" |
| 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Target/MRegisterInfo.h" |
| 29 | #include "llvm/Target/TargetMachine.h" |
| 30 | #include <map> |
| 31 | |
| 32 | using namespace llvm; |
| 33 | |
| 34 | namespace { |
| 35 | // Hidden options to help debugging |
| 36 | cl::opt<bool> |
| 37 | PerformLICM("machine-licm", |
| 38 | cl::init(false), cl::Hidden); |
| 39 | } |
| 40 | |
| 41 | namespace { |
| 42 | class VISIBILITY_HIDDEN MachineLICM : public MachineFunctionPass { |
| 43 | // Various analyses that we use... |
| 44 | MachineLoopInfo *LI; // Current MachineLoopInfo |
| 45 | MachineDominatorTree *DT; // Machine dominator tree for the current Loop |
| 46 | |
| 47 | const TargetInstrInfo *TII; |
| 48 | |
| 49 | // State that is updated as we process loops |
| 50 | bool Changed; // True if a loop is changed. |
| 51 | MachineLoop *CurLoop; // The current loop we are working on. |
| 52 | |
| 53 | // Map the def of a virtual register to the machine instruction. |
| 54 | std::map<unsigned, const MachineInstr*> VRegDefs; |
| 55 | public: |
| 56 | static char ID; // Pass identification, replacement for typeid |
| 57 | MachineLICM() : MachineFunctionPass((intptr_t)&ID) {} |
| 58 | |
| 59 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 60 | |
| 61 | /// FIXME: Loop preheaders? |
| 62 | /// |
| 63 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 64 | AU.setPreservesCFG(); |
| 65 | AU.addRequired<MachineLoopInfo>(); |
| 66 | AU.addRequired<MachineDominatorTree>(); |
| 67 | } |
| 68 | private: |
| 69 | /// GatherAllLoops - Get all loops in depth first order. |
| 70 | /// |
| 71 | void GatherAllLoops(MachineLoop *L, SmallVectorImpl<MachineLoop*> &Loops) { |
| 72 | const std::vector<MachineLoop*> &SubLoops = L->getSubLoops(); |
| 73 | |
| 74 | for (MachineLoop::iterator |
| 75 | I = SubLoops.begin(), E = SubLoops.end(); I != E; ++I) |
| 76 | GatherAllLoops(*I, Loops); |
| 77 | |
| 78 | Loops.push_back(L); |
| 79 | } |
| 80 | |
| 81 | /// MapVirtualRegisterDefs - Create a map of which machine instruction |
| 82 | /// defines a virtual register. |
| 83 | /// |
| 84 | void MapVirtualRegisterDefs(const MachineFunction &MF); |
| 85 | |
| 86 | /// isInSubLoop - A little predicate that returns true if the specified |
| 87 | /// basic block is in a subloop of the current one, not the current one |
| 88 | /// itself. |
| 89 | /// |
| 90 | bool isInSubLoop(MachineBasicBlock *BB) { |
| 91 | assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop"); |
| 92 | |
| 93 | for (MachineLoop::iterator |
| 94 | I = CurLoop->begin(), E = CurLoop->end(); I != E; ++I) |
| 95 | if ((*I)->contains(BB)) |
| 96 | return true; // A subloop actually contains this block! |
| 97 | |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | /// CanHoistInst - Checks that this instructions is one that can be hoisted |
| 102 | /// out of the loop. I.e., it has no side effects, isn't a control flow |
| 103 | /// instr, etc. |
| 104 | /// |
| 105 | bool CanHoistInst(MachineInstr &I) const { |
| 106 | const TargetInstrDescriptor *TID = I.getInstrDescriptor(); |
| 107 | MachineOpCode Opcode = TID->Opcode; |
| 108 | |
| 109 | return TII->isTriviallyReMaterializable(&I) && |
| 110 | // FIXME: Below necessary? |
| 111 | !(TII->isReturn(Opcode) || |
| 112 | TII->isTerminatorInstr(Opcode) || |
| 113 | TII->isBranch(Opcode) || |
| 114 | TII->isIndirectBranch(Opcode) || |
| 115 | TII->isBarrier(Opcode) || |
| 116 | TII->isCall(Opcode) || |
| 117 | TII->isLoad(Opcode) || // TODO: Do loads and stores. |
| 118 | TII->isStore(Opcode)); |
| 119 | } |
| 120 | |
| 121 | /// isLoopInvariantInst - Returns true if the instruction is loop |
| 122 | /// invariant. I.e., all virtual register operands are defined outside of |
| 123 | /// the loop, physical registers aren't accessed (explicitly or implicitly), |
| 124 | /// and the instruction is hoistable. |
| 125 | /// |
| 126 | bool isLoopInvariantInst(MachineInstr &I); |
| 127 | |
| 128 | /// FindPredecessors - Get all of the predecessors of the loop that are not |
| 129 | /// back-edges. |
| 130 | /// |
| 131 | void FindPredecessors(std::vector<MachineBasicBlock*> &Preds){ |
| 132 | const MachineBasicBlock *Header = CurLoop->getHeader(); |
| 133 | |
| 134 | for (MachineBasicBlock::const_pred_iterator |
| 135 | I = Header->pred_begin(), E = Header->pred_end(); I != E; ++I) |
| 136 | if (!CurLoop->contains(*I)) |
| 137 | Preds.push_back(*I); |
| 138 | } |
| 139 | |
| 140 | /// MoveInstToBlock - Moves the machine instruction to the bottom of the |
| 141 | /// predecessor basic block (but before the terminator instructions). |
| 142 | /// |
| 143 | void MoveInstToBlock(MachineBasicBlock *MBB, MachineInstr *MI) { |
| 144 | MachineBasicBlock::iterator Iter = MBB->getFirstTerminator(); |
| 145 | MBB->insert(Iter, MI); |
| 146 | } |
| 147 | |
| 148 | /// HoistRegion - Walk the specified region of the CFG (defined by all |
| 149 | /// blocks dominated by the specified block, and that are in the current |
| 150 | /// loop) in depth first order w.r.t the DominatorTree. This allows us to |
| 151 | /// visit definitions before uses, allowing us to hoist a loop body in one |
| 152 | /// pass without iteration. |
| 153 | /// |
| 154 | void HoistRegion(MachineDomTreeNode *N); |
| 155 | |
| 156 | /// Hoist - When an instruction is found to only use loop invariant operands |
| 157 | /// that is safe to hoist, this instruction is called to do the dirty work. |
| 158 | /// |
| 159 | bool Hoist(MachineInstr &MI); |
| 160 | }; |
| 161 | |
| 162 | char MachineLICM::ID = 0; |
| 163 | RegisterPass<MachineLICM> X("machine-licm", |
| 164 | "Machine Loop Invariant Code Motion"); |
| 165 | } // end anonymous namespace |
| 166 | |
| 167 | FunctionPass *llvm::createMachineLICMPass() { return new MachineLICM(); } |
| 168 | |
| 169 | /// Hoist expressions out of the specified loop. Note, alias info for inner loop |
| 170 | /// is not preserved so it is not a good idea to run LICM multiple times on one |
| 171 | /// loop. |
| 172 | /// |
| 173 | bool MachineLICM::runOnMachineFunction(MachineFunction &MF) { |
| 174 | if (!PerformLICM) return false; // For debugging. |
| 175 | |
| 176 | Changed = false; |
| 177 | TII = MF.getTarget().getInstrInfo(); |
| 178 | |
| 179 | // Get our Loop information... |
| 180 | LI = &getAnalysis<MachineLoopInfo>(); |
| 181 | DT = &getAnalysis<MachineDominatorTree>(); |
| 182 | |
| 183 | for (MachineLoopInfo::iterator |
| 184 | I = LI->begin(), E = LI->end(); I != E; ++I) { |
| 185 | MachineLoop *L = *I; |
| 186 | CurLoop = L; |
| 187 | |
| 188 | // Visit all of the instructions of the loop. We want to visit the subloops |
| 189 | // first, though, so that we can hoist their invariants first into their |
| 190 | // containing loop before we process that loop. |
| 191 | SmallVector<MachineLoop*, 16> Loops; |
| 192 | GatherAllLoops(L, Loops); |
| 193 | |
| 194 | for (SmallVector<MachineLoop*, 8>::iterator |
| 195 | II = Loops.begin(), IE = Loops.end(); II != IE; ++II) { |
| 196 | L = *II; |
| 197 | |
| 198 | // Traverse the body of the loop in depth first order on the dominator |
| 199 | // tree so that we are guaranteed to see definitions before we see uses. |
| 200 | HoistRegion(DT->getNode(L->getHeader())); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | return Changed; |
| 205 | } |
| 206 | |
| 207 | /// MapVirtualRegisterDefs - Create a map of which machine instruction defines a |
| 208 | /// virtual register. |
| 209 | /// |
| 210 | void MachineLICM::MapVirtualRegisterDefs(const MachineFunction &MF) { |
| 211 | for (MachineFunction::const_iterator |
| 212 | I = MF.begin(), E = MF.end(); I != E; ++I) { |
| 213 | const MachineBasicBlock &MBB = *I; |
| 214 | |
| 215 | for (MachineBasicBlock::const_iterator |
| 216 | II = MBB.begin(), IE = MBB.end(); II != IE; ++II) { |
| 217 | const MachineInstr &MI = *II; |
| 218 | |
| 219 | if (MI.getNumOperands() > 0) { |
| 220 | const MachineOperand &MO = MI.getOperand(0); |
| 221 | |
| 222 | if (MO.isRegister() && MO.isDef() && |
| 223 | MRegisterInfo::isVirtualRegister(MO.getReg())) |
| 224 | VRegDefs[MO.getReg()] = &MI; |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /// HoistRegion - Walk the specified region of the CFG (defined by all blocks |
| 231 | /// dominated by the specified block, and that are in the current loop) in depth |
| 232 | /// first order w.r.t the DominatorTree. This allows us to visit definitions |
| 233 | /// before uses, allowing us to hoist a loop body in one pass without iteration. |
| 234 | /// |
| 235 | void MachineLICM::HoistRegion(MachineDomTreeNode *N) { |
| 236 | assert(N != 0 && "Null dominator tree node?"); |
| 237 | MachineBasicBlock *BB = N->getBlock(); |
| 238 | |
| 239 | // If this subregion is not in the top level loop at all, exit. |
| 240 | if (!CurLoop->contains(BB)) return; |
| 241 | |
| 242 | // Only need to process the contents of this block if it is not part of a |
| 243 | // subloop (which would already have been processed). |
| 244 | if (!isInSubLoop(BB)) |
| 245 | for (MachineBasicBlock::iterator |
| 246 | I = BB->begin(), E = BB->end(); I != E; ) { |
| 247 | MachineInstr &MI = *I++; |
| 248 | |
| 249 | // Try hoisting the instruction out of the loop. We can only do this if |
| 250 | // all of the operands of the instruction are loop invariant and if it is |
| 251 | // safe to hoist the instruction. |
| 252 | if (Hoist(MI)) |
| 253 | // Hoisting was successful! Remove bothersome instruction now. |
| 254 | MI.getParent()->remove(&MI); |
| 255 | } |
| 256 | |
| 257 | const std::vector<MachineDomTreeNode*> &Children = N->getChildren(); |
| 258 | |
| 259 | for (unsigned I = 0, E = Children.size(); I != E; ++I) |
| 260 | HoistRegion(Children[I]); |
| 261 | } |
| 262 | |
| 263 | /// isLoopInvariantInst - Returns true if the instruction is loop |
| 264 | /// invariant. I.e., all virtual register operands are defined outside of the |
| 265 | /// loop, physical registers aren't accessed (explicitly or implicitly), and the |
| 266 | /// instruction is hoistable. |
| 267 | /// |
| 268 | bool MachineLICM::isLoopInvariantInst(MachineInstr &I) { |
| 269 | const TargetInstrDescriptor *TID = I.getInstrDescriptor(); |
| 270 | |
| 271 | // Don't hoist if this instruction implicitly reads physical registers or |
| 272 | // doesn't take any operands. |
| 273 | if (TID->ImplicitUses || !I.getNumOperands()) return false; |
| 274 | |
| 275 | if (!CanHoistInst(I)) return false; |
| 276 | |
| 277 | // The instruction is loop invariant if all of its operands are loop-invariant |
| 278 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
| 279 | const MachineOperand &MO = I.getOperand(i); |
| 280 | |
| 281 | if (!MO.isRegister() || !MO.isUse()) |
| 282 | continue; |
| 283 | |
| 284 | unsigned Reg = MO.getReg(); |
| 285 | |
| 286 | // Don't hoist instructions that access physical registers. |
| 287 | if (!MRegisterInfo::isVirtualRegister(Reg)) |
| 288 | return false; |
| 289 | |
| 290 | assert(VRegDefs[Reg] && "Machine instr not mapped for this vreg?!"); |
| 291 | |
| 292 | // If the loop contains the definition of an operand, then the instruction |
| 293 | // isn't loop invariant. |
| 294 | if (CurLoop->contains(VRegDefs[Reg]->getParent())) |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | // If we got this far, the instruction is loop invariant! |
| 299 | return true; |
| 300 | } |
| 301 | |
| 302 | /// Hoist - When an instruction is found to only use loop invariant operands |
| 303 | /// that is safe to hoist, this instruction is called to do the dirty work. |
| 304 | /// |
| 305 | bool MachineLICM::Hoist(MachineInstr &MI) { |
| 306 | if (!isLoopInvariantInst(MI)) return false; |
| 307 | |
| 308 | std::vector<MachineBasicBlock*> Preds; |
| 309 | |
| 310 | // Non-back-edge predecessors. |
| 311 | FindPredecessors(Preds); |
| 312 | if (Preds.empty()) return false; |
| 313 | |
| 314 | // Check that the predecessors are qualified to take the hoisted |
| 315 | // instruction. I.e., there is only one edge from each predecessor, and it's |
| 316 | // to the loop header. |
| 317 | for (std::vector<MachineBasicBlock*>::iterator |
| 318 | I = Preds.begin(), E = Preds.end(); I != E; ++I) { |
| 319 | MachineBasicBlock *MBB = *I; |
| 320 | |
| 321 | // FIXME: We are assuming at first that the basic blocks coming into this |
| 322 | // loop have only one successor each. This isn't the case in general because |
| 323 | // we haven't broken critical edges or added preheaders. |
| 324 | if (MBB->succ_size() != 1) return false; |
| 325 | assert(*MBB->succ_begin() == CurLoop->getHeader() && |
| 326 | "The predecessor doesn't feed directly into the loop header!"); |
| 327 | } |
| 328 | |
| 329 | // Now move the instructions to the predecessors. |
| 330 | for (std::vector<MachineBasicBlock*>::iterator |
| 331 | I = Preds.begin(), E = Preds.end(); I != E; ++I) |
| 332 | MoveInstToBlock(*I, MI.clone()); |
| 333 | |
| 334 | Changed = true; |
| 335 | return true; |
| 336 | } |