1 //===-- MachineLICM.cpp - Machine Loop Invariant Code Motion Pass ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "machine-licm"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/CodeGen/MachineDominators.h"
18 #include "llvm/CodeGen/MachineLoopInfo.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/Target/TargetRegisterInfo.h"
21 #include "llvm/Target/TargetInstrInfo.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/Compiler.h"
27 #include "llvm/Support/Debug.h"
31 STATISTIC(NumHoisted, "Number of machine instructions hoisted out of loops");
34 class VISIBILITY_HIDDEN MachineLICM : public MachineFunctionPass {
35 const TargetMachine *TM;
36 const TargetInstrInfo *TII;
38 // Various analyses that we use...
39 MachineLoopInfo *LI; // Current MachineLoopInfo
40 MachineDominatorTree *DT; // Machine dominator tree for the cur loop
41 MachineRegisterInfo *RegInfo; // Machine register information
43 // State that is updated as we process loops
44 bool Changed; // True if a loop is changed.
45 MachineLoop *CurLoop; // The current loop we are working on.
47 static char ID; // Pass identification, replacement for typeid
48 MachineLICM() : MachineFunctionPass(&ID) {}
50 virtual bool runOnMachineFunction(MachineFunction &MF);
52 const char *getPassName() const { return "Machine Instruction LICM"; }
54 // FIXME: Loop preheaders?
55 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
57 AU.addRequired<MachineLoopInfo>();
58 AU.addRequired<MachineDominatorTree>();
59 AU.addPreserved<MachineLoopInfo>();
60 AU.addPreserved<MachineDominatorTree>();
61 MachineFunctionPass::getAnalysisUsage(AU);
64 /// VisitAllLoops - Visit all of the loops in depth first order and try to
65 /// hoist invariant instructions from them.
67 void VisitAllLoops(MachineLoop *L) {
68 const std::vector<MachineLoop*> &SubLoops = L->getSubLoops();
70 for (MachineLoop::iterator
71 I = SubLoops.begin(), E = SubLoops.end(); I != E; ++I) {
74 // Traverse the body of the loop in depth first order on the dominator
75 // tree so that we are guaranteed to see definitions before we see uses.
77 HoistRegion(DT->getNode(ML->getHeader()));
80 HoistRegion(DT->getNode(L->getHeader()));
83 /// IsInSubLoop - A little predicate that returns true if the specified
84 /// basic block is in a subloop of the current one, not the current one
87 bool IsInSubLoop(MachineBasicBlock *BB) {
88 assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop");
89 return LI->getLoopFor(BB) != CurLoop;
92 /// IsLoopInvariantInst - Returns true if the instruction is loop
93 /// invariant. I.e., all virtual register operands are defined outside of
94 /// the loop, physical registers aren't accessed (explicitly or implicitly),
95 /// and the instruction is hoistable.
97 bool IsLoopInvariantInst(MachineInstr &I);
99 /// FindPredecessors - Get all of the predecessors of the loop that are not
102 void FindPredecessors(std::vector<MachineBasicBlock*> &Preds) {
103 const MachineBasicBlock *Header = CurLoop->getHeader();
105 for (MachineBasicBlock::const_pred_iterator
106 I = Header->pred_begin(), E = Header->pred_end(); I != E; ++I)
107 if (!CurLoop->contains(*I))
111 /// MoveInstToEndOfBlock - Moves the machine instruction to the bottom of
112 /// the predecessor basic block (but before the terminator instructions).
114 void MoveInstToEndOfBlock(MachineBasicBlock *ToMBB,
115 MachineBasicBlock *FromMBB,
118 /// HoistRegion - Walk the specified region of the CFG (defined by all
119 /// blocks dominated by the specified block, and that are in the current
120 /// loop) in depth first order w.r.t the DominatorTree. This allows us to
121 /// visit definitions before uses, allowing us to hoist a loop body in one
122 /// pass without iteration.
124 void HoistRegion(MachineDomTreeNode *N);
126 /// Hoist - When an instruction is found to only use loop invariant operands
127 /// that is safe to hoist, this instruction is called to do the dirty work.
129 void Hoist(MachineInstr &MI);
131 } // end anonymous namespace
133 char MachineLICM::ID = 0;
134 static RegisterPass<MachineLICM>
135 X("machinelicm", "Machine Loop Invariant Code Motion");
137 FunctionPass *llvm::createMachineLICMPass() { return new MachineLICM(); }
139 /// Hoist expressions out of the specified loop. Note, alias info for inner loop
140 /// is not preserved so it is not a good idea to run LICM multiple times on one
143 bool MachineLICM::runOnMachineFunction(MachineFunction &MF) {
144 DOUT << "******** Machine LICM ********\n";
147 TM = &MF.getTarget();
148 TII = TM->getInstrInfo();
149 RegInfo = &MF.getRegInfo();
151 // Get our Loop information...
152 LI = &getAnalysis<MachineLoopInfo>();
153 DT = &getAnalysis<MachineDominatorTree>();
155 for (MachineLoopInfo::iterator
156 I = LI->begin(), E = LI->end(); I != E; ++I) {
159 // Visit all of the instructions of the loop. We want to visit the subloops
160 // first, though, so that we can hoist their invariants first into their
161 // containing loop before we process that loop.
162 VisitAllLoops(CurLoop);
168 /// HoistRegion - Walk the specified region of the CFG (defined by all blocks
169 /// dominated by the specified block, and that are in the current loop) in depth
170 /// first order w.r.t the DominatorTree. This allows us to visit definitions
171 /// before uses, allowing us to hoist a loop body in one pass without iteration.
173 void MachineLICM::HoistRegion(MachineDomTreeNode *N) {
174 assert(N != 0 && "Null dominator tree node?");
175 MachineBasicBlock *BB = N->getBlock();
177 // If this subregion is not in the top level loop at all, exit.
178 if (!CurLoop->contains(BB)) return;
180 // Only need to process the contents of this block if it is not part of a
181 // subloop (which would already have been processed).
182 if (!IsInSubLoop(BB))
183 for (MachineBasicBlock::iterator
184 I = BB->begin(), E = BB->end(); I != E; ) {
185 MachineInstr &MI = *I++;
187 // Try hoisting the instruction out of the loop. We can only do this if
188 // all of the operands of the instruction are loop invariant and if it is
189 // safe to hoist the instruction.
193 const std::vector<MachineDomTreeNode*> &Children = N->getChildren();
195 for (unsigned I = 0, E = Children.size(); I != E; ++I)
196 HoistRegion(Children[I]);
199 /// IsLoopInvariantInst - Returns true if the instruction is loop
200 /// invariant. I.e., all virtual register operands are defined outside of the
201 /// loop, physical registers aren't accessed explicitly, and there are no side
202 /// effects that aren't captured by the operands or other flags.
204 bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) {
205 const TargetInstrDesc &TID = I.getDesc();
207 // Ignore stuff that we obviously can't hoist.
208 if (TID.mayStore() || TID.isCall() || TID.isReturn() || TID.isBranch() ||
209 TID.hasUnmodeledSideEffects())
213 // Okay, this instruction does a load. As a refinement, we allow the target
214 // to decide whether the loaded value is actually a constant. If so, we can
215 // actually use it as a load.
216 if (!TII->isInvariantLoad(&I))
217 // FIXME: we should be able to sink loads with no other side effects if
218 // there is nothing that can change memory from here until the end of
219 // block. This is a trivial form of alias analysis.
224 DOUT << "--- Checking if we can hoist " << I;
225 if (I.getDesc().getImplicitUses()) {
226 DOUT << " * Instruction has implicit uses:\n";
228 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
229 for (const unsigned *ImpUses = I.getDesc().getImplicitUses();
231 DOUT << " -> " << TRI->getName(*ImpUses) << "\n";
234 if (I.getDesc().getImplicitDefs()) {
235 DOUT << " * Instruction has implicit defines:\n";
237 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
238 for (const unsigned *ImpDefs = I.getDesc().getImplicitDefs();
240 DOUT << " -> " << TRI->getName(*ImpDefs) << "\n";
244 if (I.getDesc().getImplicitDefs() || I.getDesc().getImplicitUses()) {
245 DOUT << "Cannot hoist with implicit defines or uses\n";
249 // The instruction is loop invariant if all of its operands are.
250 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
251 const MachineOperand &MO = I.getOperand(i);
256 if (MO.isDef() && TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
257 // Don't hoist an instruction that defines a physical register.
263 unsigned Reg = MO.getReg();
264 if (Reg == 0) continue;
266 // Don't hoist instructions that access physical registers.
267 if (TargetRegisterInfo::isPhysicalRegister(Reg))
270 assert(RegInfo->getVRegDef(Reg) &&
271 "Machine instr not mapped for this vreg?!");
273 // If the loop contains the definition of an operand, then the instruction
274 // isn't loop invariant.
275 if (CurLoop->contains(RegInfo->getVRegDef(Reg)->getParent()))
279 // If we got this far, the instruction is loop invariant!
283 /// MoveInstToEndOfBlock - Moves the machine instruction to the bottom of the
284 /// predecessor basic block (but before the terminator instructions).
286 void MachineLICM::MoveInstToEndOfBlock(MachineBasicBlock *ToMBB,
287 MachineBasicBlock *FromMBB,
290 DOUT << "Hoisting " << *MI;
291 if (ToMBB->getBasicBlock())
292 DOUT << " to MachineBasicBlock "
293 << ToMBB->getBasicBlock()->getName();
294 if (FromMBB->getBasicBlock())
295 DOUT << " from MachineBasicBlock "
296 << FromMBB->getBasicBlock()->getName();
300 MachineBasicBlock::iterator WhereIter = ToMBB->getFirstTerminator();
301 MachineBasicBlock::iterator To, From = FromMBB->begin();
306 assert(From != FromMBB->end() && "Didn't find instr in BB!");
309 ToMBB->splice(WhereIter, FromMBB, From, ++To);
313 /// Hoist - When an instruction is found to use only loop invariant operands
314 /// that are safe to hoist, this instruction is called to do the dirty work.
316 void MachineLICM::Hoist(MachineInstr &MI) {
317 if (!IsLoopInvariantInst(MI)) return;
319 std::vector<MachineBasicBlock*> Preds;
321 // Non-back-edge predecessors.
322 FindPredecessors(Preds);
324 // Either we don't have any predecessors(?!) or we have more than one, which
326 if (Preds.empty() || Preds.size() != 1) return;
328 // Check that the predecessor is qualified to take the hoisted instruction.
329 // I.e., there is only one edge from the predecessor, and it's to the loop
331 MachineBasicBlock *MBB = Preds.front();
333 // FIXME: We are assuming at first that the basic block coming into this loop
334 // has only one successor. This isn't the case in general because we haven't
335 // broken critical edges or added preheaders.
336 if (MBB->succ_size() != 1) return;
337 assert(*MBB->succ_begin() == CurLoop->getHeader() &&
338 "The predecessor doesn't feed directly into the loop header!");
340 // Now move the instructions to the predecessor.
341 MoveInstToEndOfBlock(MBB, MI.getParent(), &MI);