blob: e6b9c76e9cd3c58691051b55e17e3e094fcec3f5 [file] [log] [blame]
Bill Wendling0f940c92007-12-07 21:42:31 +00001//===-- MachineLICM.cpp - Machine Loop Invariant Code Motion Pass ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bill Wendling0f940c92007-12-07 21:42:31 +00007//
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"
Chris Lattnerac695822008-01-04 06:41:45 +000016#include "llvm/CodeGen/Passes.h"
Bill Wendling0f940c92007-12-07 21:42:31 +000017#include "llvm/CodeGen/MachineDominators.h"
Bill Wendling0f940c92007-12-07 21:42:31 +000018#include "llvm/CodeGen/MachineLoopInfo.h"
Bill Wendling9258cd32008-01-02 19:32:43 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendling0f940c92007-12-07 21:42:31 +000020#include "llvm/Target/MRegisterInfo.h"
Bill Wendlingefe2be72007-12-11 23:27:51 +000021#include "llvm/Target/TargetInstrInfo.h"
Bill Wendling0f940c92007-12-07 21:42:31 +000022#include "llvm/Target/TargetMachine.h"
Chris Lattnerac695822008-01-04 06:41:45 +000023#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"
Bill Wendling0f940c92007-12-07 21:42:31 +000028
29using namespace llvm;
30
Bill Wendling041b3f82007-12-08 23:58:46 +000031STATISTIC(NumHoisted, "Number of machine instructions hoisted out of loops");
Bill Wendlingb48519c2007-12-08 01:47:01 +000032
Bill Wendling0f940c92007-12-07 21:42:31 +000033namespace {
34 class VISIBILITY_HIDDEN MachineLICM : public MachineFunctionPass {
Bill Wendling9258cd32008-01-02 19:32:43 +000035 const TargetMachine *TM;
Bill Wendlingefe2be72007-12-11 23:27:51 +000036 const TargetInstrInfo *TII;
37 MachineFunction *CurMF; // Current MachineFunction
Bill Wendling12ebf142007-12-11 19:40:06 +000038
Bill Wendling0f940c92007-12-07 21:42:31 +000039 // Various analyses that we use...
40 MachineLoopInfo *LI; // Current MachineLoopInfo
41 MachineDominatorTree *DT; // Machine dominator tree for the current Loop
Bill Wendling9258cd32008-01-02 19:32:43 +000042 MachineRegisterInfo *RegInfo; // Machine register information
Bill Wendling0f940c92007-12-07 21:42:31 +000043
Bill Wendling0f940c92007-12-07 21:42:31 +000044 // State that is updated as we process loops
45 bool Changed; // True if a loop is changed.
46 MachineLoop *CurLoop; // The current loop we are working on.
Bill Wendling0f940c92007-12-07 21:42:31 +000047 public:
48 static char ID; // Pass identification, replacement for typeid
49 MachineLICM() : MachineFunctionPass((intptr_t)&ID) {}
50
51 virtual bool runOnMachineFunction(MachineFunction &MF);
52
53 /// FIXME: Loop preheaders?
54 ///
55 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Bill Wendlingb082c6f2008-01-04 07:50:05 +000056 MachineFunctionPass::getAnalysisUsage(AU);
Bill Wendling0f940c92007-12-07 21:42:31 +000057 AU.setPreservesCFG();
58 AU.addRequired<MachineLoopInfo>();
59 AU.addRequired<MachineDominatorTree>();
60 }
61 private:
Bill Wendlingb48519c2007-12-08 01:47:01 +000062 /// VisitAllLoops - Visit all of the loops in depth first order and try to
63 /// hoist invariant instructions from them.
Bill Wendling0f940c92007-12-07 21:42:31 +000064 ///
Bill Wendlingb48519c2007-12-08 01:47:01 +000065 void VisitAllLoops(MachineLoop *L) {
Bill Wendling0f940c92007-12-07 21:42:31 +000066 const std::vector<MachineLoop*> &SubLoops = L->getSubLoops();
67
68 for (MachineLoop::iterator
Bill Wendlingb48519c2007-12-08 01:47:01 +000069 I = SubLoops.begin(), E = SubLoops.end(); I != E; ++I) {
70 MachineLoop *ML = *I;
Bill Wendling0f940c92007-12-07 21:42:31 +000071
Bill Wendlingb48519c2007-12-08 01:47:01 +000072 // Traverse the body of the loop in depth first order on the dominator
73 // tree so that we are guaranteed to see definitions before we see uses.
74 VisitAllLoops(ML);
75 HoistRegion(DT->getNode(ML->getHeader()));
76 }
77
78 HoistRegion(DT->getNode(L->getHeader()));
Bill Wendling0f940c92007-12-07 21:42:31 +000079 }
80
Bill Wendling041b3f82007-12-08 23:58:46 +000081 /// IsInSubLoop - A little predicate that returns true if the specified
Bill Wendling0f940c92007-12-07 21:42:31 +000082 /// basic block is in a subloop of the current one, not the current one
83 /// itself.
84 ///
Bill Wendling041b3f82007-12-08 23:58:46 +000085 bool IsInSubLoop(MachineBasicBlock *BB) {
Bill Wendling0f940c92007-12-07 21:42:31 +000086 assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop");
Bill Wendling650b0522007-12-11 18:45:11 +000087 return LI->getLoopFor(BB) != CurLoop;
Bill Wendling0f940c92007-12-07 21:42:31 +000088 }
89
Bill Wendling041b3f82007-12-08 23:58:46 +000090 /// IsLoopInvariantInst - Returns true if the instruction is loop
Bill Wendling0f940c92007-12-07 21:42:31 +000091 /// invariant. I.e., all virtual register operands are defined outside of
92 /// the loop, physical registers aren't accessed (explicitly or implicitly),
93 /// and the instruction is hoistable.
94 ///
Bill Wendling041b3f82007-12-08 23:58:46 +000095 bool IsLoopInvariantInst(MachineInstr &I);
Bill Wendling0f940c92007-12-07 21:42:31 +000096
97 /// FindPredecessors - Get all of the predecessors of the loop that are not
98 /// back-edges.
99 ///
Bill Wendling650b0522007-12-11 18:45:11 +0000100 void FindPredecessors(std::vector<MachineBasicBlock*> &Preds) {
Bill Wendling0f940c92007-12-07 21:42:31 +0000101 const MachineBasicBlock *Header = CurLoop->getHeader();
102
103 for (MachineBasicBlock::const_pred_iterator
104 I = Header->pred_begin(), E = Header->pred_end(); I != E; ++I)
105 if (!CurLoop->contains(*I))
106 Preds.push_back(*I);
107 }
108
Bill Wendlingb48519c2007-12-08 01:47:01 +0000109 /// MoveInstToEndOfBlock - Moves the machine instruction to the bottom of
110 /// the predecessor basic block (but before the terminator instructions).
Bill Wendling0f940c92007-12-07 21:42:31 +0000111 ///
Bill Wendling9258cd32008-01-02 19:32:43 +0000112 void MoveInstToEndOfBlock(MachineBasicBlock *ToMBB,
113 MachineBasicBlock *FromMBB,
114 MachineInstr *MI) {
Bill Wendlingefe2be72007-12-11 23:27:51 +0000115 DEBUG({
116 DOUT << "Hoisting " << *MI;
Bill Wendling9258cd32008-01-02 19:32:43 +0000117 if (ToMBB->getBasicBlock())
Bill Wendlingefe2be72007-12-11 23:27:51 +0000118 DOUT << " to MachineBasicBlock "
Bill Wendling9258cd32008-01-02 19:32:43 +0000119 << ToMBB->getBasicBlock()->getName();
Bill Wendlingefe2be72007-12-11 23:27:51 +0000120 DOUT << "\n";
121 });
Bill Wendling9258cd32008-01-02 19:32:43 +0000122
123 MachineBasicBlock::iterator WhereIter = ToMBB->getFirstTerminator();
124 MachineBasicBlock::iterator To, From = FromMBB->begin();
125
126 while (&*From != MI)
127 ++From;
128
129 assert(From != FromMBB->end() && "Didn't find instr in BB!");
130
131 To = From;
132 ToMBB->splice(WhereIter, FromMBB, From, ++To);
Bill Wendlingb48519c2007-12-08 01:47:01 +0000133 ++NumHoisted;
Bill Wendling0f940c92007-12-07 21:42:31 +0000134 }
135
136 /// HoistRegion - Walk the specified region of the CFG (defined by all
137 /// blocks dominated by the specified block, and that are in the current
138 /// loop) in depth first order w.r.t the DominatorTree. This allows us to
139 /// visit definitions before uses, allowing us to hoist a loop body in one
140 /// pass without iteration.
141 ///
142 void HoistRegion(MachineDomTreeNode *N);
143
144 /// Hoist - When an instruction is found to only use loop invariant operands
145 /// that is safe to hoist, this instruction is called to do the dirty work.
146 ///
Bill Wendlingb48519c2007-12-08 01:47:01 +0000147 void Hoist(MachineInstr &MI);
Bill Wendling0f940c92007-12-07 21:42:31 +0000148 };
149
150 char MachineLICM::ID = 0;
151 RegisterPass<MachineLICM> X("machine-licm",
152 "Machine Loop Invariant Code Motion");
153} // end anonymous namespace
154
155FunctionPass *llvm::createMachineLICMPass() { return new MachineLICM(); }
156
157/// Hoist expressions out of the specified loop. Note, alias info for inner loop
158/// is not preserved so it is not a good idea to run LICM multiple times on one
159/// loop.
160///
161bool MachineLICM::runOnMachineFunction(MachineFunction &MF) {
Bill Wendlinga17ad592007-12-11 22:22:22 +0000162 DOUT << "******** Machine LICM ********\n";
163
Bill Wendling0f940c92007-12-07 21:42:31 +0000164 Changed = false;
Bill Wendling12ebf142007-12-11 19:40:06 +0000165 CurMF = &MF;
Bill Wendling9258cd32008-01-02 19:32:43 +0000166 TM = &CurMF->getTarget();
167 TII = TM->getInstrInfo();
Bill Wendlingdde059a2008-01-02 21:10:54 +0000168 RegInfo = &CurMF->getRegInfo();
Bill Wendling0f940c92007-12-07 21:42:31 +0000169
170 // Get our Loop information...
171 LI = &getAnalysis<MachineLoopInfo>();
172 DT = &getAnalysis<MachineDominatorTree>();
173
174 for (MachineLoopInfo::iterator
175 I = LI->begin(), E = LI->end(); I != E; ++I) {
Bill Wendlinga17ad592007-12-11 22:22:22 +0000176 CurLoop = *I;
Bill Wendling0f940c92007-12-07 21:42:31 +0000177
178 // Visit all of the instructions of the loop. We want to visit the subloops
179 // first, though, so that we can hoist their invariants first into their
180 // containing loop before we process that loop.
Bill Wendlinga17ad592007-12-11 22:22:22 +0000181 VisitAllLoops(CurLoop);
Bill Wendling0f940c92007-12-07 21:42:31 +0000182 }
183
184 return Changed;
185}
186
Bill Wendling0f940c92007-12-07 21:42:31 +0000187/// HoistRegion - Walk the specified region of the CFG (defined by all blocks
188/// dominated by the specified block, and that are in the current loop) in depth
189/// first order w.r.t the DominatorTree. This allows us to visit definitions
190/// before uses, allowing us to hoist a loop body in one pass without iteration.
191///
192void MachineLICM::HoistRegion(MachineDomTreeNode *N) {
193 assert(N != 0 && "Null dominator tree node?");
194 MachineBasicBlock *BB = N->getBlock();
195
196 // If this subregion is not in the top level loop at all, exit.
197 if (!CurLoop->contains(BB)) return;
198
199 // Only need to process the contents of this block if it is not part of a
200 // subloop (which would already have been processed).
Bill Wendling041b3f82007-12-08 23:58:46 +0000201 if (!IsInSubLoop(BB))
Bill Wendling0f940c92007-12-07 21:42:31 +0000202 for (MachineBasicBlock::iterator
203 I = BB->begin(), E = BB->end(); I != E; ) {
204 MachineInstr &MI = *I++;
205
206 // Try hoisting the instruction out of the loop. We can only do this if
207 // all of the operands of the instruction are loop invariant and if it is
208 // safe to hoist the instruction.
Bill Wendlingb48519c2007-12-08 01:47:01 +0000209 Hoist(MI);
Bill Wendling0f940c92007-12-07 21:42:31 +0000210 }
211
212 const std::vector<MachineDomTreeNode*> &Children = N->getChildren();
213
214 for (unsigned I = 0, E = Children.size(); I != E; ++I)
215 HoistRegion(Children[I]);
216}
217
Bill Wendling041b3f82007-12-08 23:58:46 +0000218/// IsLoopInvariantInst - Returns true if the instruction is loop
Bill Wendling0f940c92007-12-07 21:42:31 +0000219/// invariant. I.e., all virtual register operands are defined outside of the
Bill Wendling60ff1a32007-12-20 01:08:10 +0000220/// loop, physical registers aren't accessed explicitly, and there are no side
221/// effects that aren't captured by the operands or other flags.
Bill Wendling0f940c92007-12-07 21:42:31 +0000222///
Bill Wendling041b3f82007-12-08 23:58:46 +0000223bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) {
Bill Wendling280f4562007-12-18 21:38:04 +0000224 DEBUG({
225 DOUT << "--- Checking if we can hoist " << I;
226 if (I.getInstrDescriptor()->ImplicitUses) {
227 DOUT << " * Instruction has implicit uses:\n";
228
Bill Wendling9258cd32008-01-02 19:32:43 +0000229 const MRegisterInfo *MRI = TM->getRegisterInfo();
Bill Wendling280f4562007-12-18 21:38:04 +0000230 const unsigned *ImpUses = I.getInstrDescriptor()->ImplicitUses;
231
232 for (; *ImpUses; ++ImpUses)
233 DOUT << " -> " << MRI->getName(*ImpUses) << "\n";
234 }
235
236 if (I.getInstrDescriptor()->ImplicitDefs) {
237 DOUT << " * Instruction has implicit defines:\n";
238
Bill Wendling9258cd32008-01-02 19:32:43 +0000239 const MRegisterInfo *MRI = TM->getRegisterInfo();
Bill Wendling280f4562007-12-18 21:38:04 +0000240 const unsigned *ImpDefs = I.getInstrDescriptor()->ImplicitDefs;
241
242 for (; *ImpDefs; ++ImpDefs)
243 DOUT << " -> " << MRI->getName(*ImpDefs) << "\n";
244 }
245
246 if (TII->hasUnmodelledSideEffects(&I))
247 DOUT << " * Instruction has side effects.\n";
248 });
249
Bill Wendling0f940c92007-12-07 21:42:31 +0000250 // The instruction is loop invariant if all of its operands are loop-invariant
251 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
252 const MachineOperand &MO = I.getOperand(i);
253
Bill Wendling280f4562007-12-18 21:38:04 +0000254 if (!(MO.isRegister() && MO.getReg() && MO.isUse()))
Bill Wendling0f940c92007-12-07 21:42:31 +0000255 continue;
256
257 unsigned Reg = MO.getReg();
258
259 // Don't hoist instructions that access physical registers.
260 if (!MRegisterInfo::isVirtualRegister(Reg))
261 return false;
262
Bill Wendling9258cd32008-01-02 19:32:43 +0000263 assert(RegInfo->getVRegDef(Reg)&&"Machine instr not mapped for this vreg?");
Bill Wendling0f940c92007-12-07 21:42:31 +0000264
265 // If the loop contains the definition of an operand, then the instruction
266 // isn't loop invariant.
Bill Wendling9258cd32008-01-02 19:32:43 +0000267 if (CurLoop->contains(RegInfo->getVRegDef(Reg)->getParent()))
Bill Wendling0f940c92007-12-07 21:42:31 +0000268 return false;
269 }
270
Bill Wendling60ff1a32007-12-20 01:08:10 +0000271 // Don't hoist something that has unmodelled side effects.
Bill Wendling280f4562007-12-18 21:38:04 +0000272 if (TII->hasUnmodelledSideEffects(&I)) return false;
273
Bill Wendling0f940c92007-12-07 21:42:31 +0000274 // If we got this far, the instruction is loop invariant!
275 return true;
276}
277
278/// Hoist - When an instruction is found to only use loop invariant operands
279/// that is safe to hoist, this instruction is called to do the dirty work.
280///
Bill Wendlingb48519c2007-12-08 01:47:01 +0000281void MachineLICM::Hoist(MachineInstr &MI) {
Bill Wendling041b3f82007-12-08 23:58:46 +0000282 if (!IsLoopInvariantInst(MI)) return;
Bill Wendling0f940c92007-12-07 21:42:31 +0000283
284 std::vector<MachineBasicBlock*> Preds;
285
286 // Non-back-edge predecessors.
287 FindPredecessors(Preds);
Bill Wendling0f940c92007-12-07 21:42:31 +0000288
Bill Wendlingb48519c2007-12-08 01:47:01 +0000289 // Either we don't have any predecessors(?!) or we have more than one, which
290 // is forbidden.
291 if (Preds.empty() || Preds.size() != 1) return;
Bill Wendling0f940c92007-12-07 21:42:31 +0000292
Bill Wendlingb48519c2007-12-08 01:47:01 +0000293 // Check that the predecessor is qualified to take the hoisted
294 // instruction. I.e., there is only one edge from the predecessor, and it's to
295 // the loop header.
296 MachineBasicBlock *MBB = Preds.front();
Bill Wendling0f940c92007-12-07 21:42:31 +0000297
Bill Wendling041b3f82007-12-08 23:58:46 +0000298 // FIXME: We are assuming at first that the basic block coming into this loop
299 // has only one successor. This isn't the case in general because we haven't
300 // broken critical edges or added preheaders.
Bill Wendlingb48519c2007-12-08 01:47:01 +0000301 if (MBB->succ_size() != 1) return;
302 assert(*MBB->succ_begin() == CurLoop->getHeader() &&
303 "The predecessor doesn't feed directly into the loop header!");
Bill Wendling0f940c92007-12-07 21:42:31 +0000304
Bill Wendlingb48519c2007-12-08 01:47:01 +0000305 // Now move the instructions to the predecessor.
Bill Wendling9258cd32008-01-02 19:32:43 +0000306 MoveInstToEndOfBlock(MBB, MI.getParent(), &MI);
Bill Wendling0f940c92007-12-07 21:42:31 +0000307 Changed = true;
Bill Wendling0f940c92007-12-07 21:42:31 +0000308}