blob: fbf21139ac4c7ffc6568221ffbd16ab97820269b [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//
Dan Gohmanc475c362009-01-15 22:01:38 +000013// This pass does not attempt to throttle itself to limit register pressure.
14// The register allocation phases are expected to perform rematerialization
15// to recover when register pressure is high.
16//
17// This pass is not intended to be a replacement or a complete alternative
18// for the LLVM-IR-level LICM pass. It is only designed to hoist simple
19// constructs that are not exposed before lowering and instruction selection.
20//
Bill Wendling0f940c92007-12-07 21:42:31 +000021//===----------------------------------------------------------------------===//
22
23#define DEBUG_TYPE "machine-licm"
Chris Lattnerac695822008-01-04 06:41:45 +000024#include "llvm/CodeGen/Passes.h"
Bill Wendling0f940c92007-12-07 21:42:31 +000025#include "llvm/CodeGen/MachineDominators.h"
Bill Wendling0f940c92007-12-07 21:42:31 +000026#include "llvm/CodeGen/MachineLoopInfo.h"
Bill Wendling9258cd32008-01-02 19:32:43 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Bill Wendlingefe2be72007-12-11 23:27:51 +000029#include "llvm/Target/TargetInstrInfo.h"
Bill Wendling0f940c92007-12-07 21:42:31 +000030#include "llvm/Target/TargetMachine.h"
Chris Lattnerac695822008-01-04 06:41:45 +000031#include "llvm/ADT/Statistic.h"
32#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/Compiler.h"
34#include "llvm/Support/Debug.h"
Bill Wendling0f940c92007-12-07 21:42:31 +000035
36using namespace llvm;
37
Bill Wendling041b3f82007-12-08 23:58:46 +000038STATISTIC(NumHoisted, "Number of machine instructions hoisted out of loops");
Bill Wendlingb48519c2007-12-08 01:47:01 +000039
Bill Wendling0f940c92007-12-07 21:42:31 +000040namespace {
41 class VISIBILITY_HIDDEN MachineLICM : public MachineFunctionPass {
Bill Wendling9258cd32008-01-02 19:32:43 +000042 const TargetMachine *TM;
Bill Wendlingefe2be72007-12-11 23:27:51 +000043 const TargetInstrInfo *TII;
Bill Wendling12ebf142007-12-11 19:40:06 +000044
Bill Wendling0f940c92007-12-07 21:42:31 +000045 // Various analyses that we use...
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +000046 MachineLoopInfo *LI; // Current MachineLoopInfo
47 MachineDominatorTree *DT; // Machine dominator tree for the cur loop
Bill Wendling9258cd32008-01-02 19:32:43 +000048 MachineRegisterInfo *RegInfo; // Machine register information
Bill Wendling0f940c92007-12-07 21:42:31 +000049
Bill Wendling0f940c92007-12-07 21:42:31 +000050 // State that is updated as we process loops
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +000051 bool Changed; // True if a loop is changed.
52 MachineLoop *CurLoop; // The current loop we are working on.
Dan Gohmanc475c362009-01-15 22:01:38 +000053 MachineBasicBlock *CurPreheader; // The preheader for CurLoop.
Bill Wendling0f940c92007-12-07 21:42:31 +000054 public:
55 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000056 MachineLICM() : MachineFunctionPass(&ID) {}
Bill Wendling0f940c92007-12-07 21:42:31 +000057
58 virtual bool runOnMachineFunction(MachineFunction &MF);
59
Dan Gohman72241702008-12-18 01:37:56 +000060 const char *getPassName() const { return "Machine Instruction LICM"; }
61
Bill Wendling074223a2008-03-10 08:13:01 +000062 // FIXME: Loop preheaders?
Bill Wendling0f940c92007-12-07 21:42:31 +000063 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
64 AU.setPreservesCFG();
65 AU.addRequired<MachineLoopInfo>();
66 AU.addRequired<MachineDominatorTree>();
Bill Wendlingd5da7042008-01-04 08:48:49 +000067 AU.addPreserved<MachineLoopInfo>();
68 AU.addPreserved<MachineDominatorTree>();
69 MachineFunctionPass::getAnalysisUsage(AU);
Bill Wendling0f940c92007-12-07 21:42:31 +000070 }
71 private:
Bill Wendling041b3f82007-12-08 23:58:46 +000072 /// IsLoopInvariantInst - Returns true if the instruction is loop
Bill Wendling0f940c92007-12-07 21:42:31 +000073 /// invariant. I.e., all virtual register operands are defined outside of
74 /// the loop, physical registers aren't accessed (explicitly or implicitly),
75 /// and the instruction is hoistable.
76 ///
Bill Wendling041b3f82007-12-08 23:58:46 +000077 bool IsLoopInvariantInst(MachineInstr &I);
Bill Wendling0f940c92007-12-07 21:42:31 +000078
Bill Wendling0f940c92007-12-07 21:42:31 +000079 /// HoistRegion - Walk the specified region of the CFG (defined by all
80 /// blocks dominated by the specified block, and that are in the current
81 /// loop) in depth first order w.r.t the DominatorTree. This allows us to
82 /// visit definitions before uses, allowing us to hoist a loop body in one
83 /// pass without iteration.
84 ///
85 void HoistRegion(MachineDomTreeNode *N);
86
87 /// Hoist - When an instruction is found to only use loop invariant operands
88 /// that is safe to hoist, this instruction is called to do the dirty work.
89 ///
Bill Wendlingb48519c2007-12-08 01:47:01 +000090 void Hoist(MachineInstr &MI);
Bill Wendling0f940c92007-12-07 21:42:31 +000091 };
Bill Wendling0f940c92007-12-07 21:42:31 +000092} // end anonymous namespace
93
Dan Gohman844731a2008-05-13 00:00:25 +000094char MachineLICM::ID = 0;
95static RegisterPass<MachineLICM>
Bill Wendling8870ce92008-07-07 05:42:27 +000096X("machinelicm", "Machine Loop Invariant Code Motion");
Dan Gohman844731a2008-05-13 00:00:25 +000097
Bill Wendling0f940c92007-12-07 21:42:31 +000098FunctionPass *llvm::createMachineLICMPass() { return new MachineLICM(); }
99
Dan Gohmanc475c362009-01-15 22:01:38 +0000100/// LoopIsOuterMostWithPreheader - Test if the given loop is the outer-most
101/// loop that has a preheader.
102static bool LoopIsOuterMostWithPreheader(MachineLoop *CurLoop) {
103 for (MachineLoop *L = CurLoop->getParentLoop(); L; L = L->getParentLoop())
104 if (L->getLoopPreheader())
105 return false;
106 return true;
107}
108
Bill Wendling0f940c92007-12-07 21:42:31 +0000109/// Hoist expressions out of the specified loop. Note, alias info for inner loop
110/// is not preserved so it is not a good idea to run LICM multiple times on one
111/// loop.
112///
113bool MachineLICM::runOnMachineFunction(MachineFunction &MF) {
Bill Wendlinga17ad592007-12-11 22:22:22 +0000114 DOUT << "******** Machine LICM ********\n";
115
Bill Wendling0f940c92007-12-07 21:42:31 +0000116 Changed = false;
Bill Wendlingacb04ec2008-08-31 02:30:23 +0000117 TM = &MF.getTarget();
Bill Wendling9258cd32008-01-02 19:32:43 +0000118 TII = TM->getInstrInfo();
Bill Wendlingacb04ec2008-08-31 02:30:23 +0000119 RegInfo = &MF.getRegInfo();
Bill Wendling0f940c92007-12-07 21:42:31 +0000120
121 // Get our Loop information...
122 LI = &getAnalysis<MachineLoopInfo>();
123 DT = &getAnalysis<MachineDominatorTree>();
124
125 for (MachineLoopInfo::iterator
126 I = LI->begin(), E = LI->end(); I != E; ++I) {
Bill Wendlinga17ad592007-12-11 22:22:22 +0000127 CurLoop = *I;
Bill Wendling0f940c92007-12-07 21:42:31 +0000128
Dan Gohmanc475c362009-01-15 22:01:38 +0000129 // Only visit outer-most preheader-sporting loops.
130 if (!LoopIsOuterMostWithPreheader(CurLoop))
131 continue;
132
133 // Determine the block to which to hoist instructions. If we can't find a
134 // suitable loop preheader, we can't do any hoisting.
135 //
136 // FIXME: We are only hoisting if the basic block coming into this loop
137 // has only one successor. This isn't the case in general because we haven't
138 // broken critical edges or added preheaders.
139 CurPreheader = CurLoop->getLoopPreheader();
140 if (!CurPreheader)
141 continue;
142
143 HoistRegion(DT->getNode(CurLoop->getHeader()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000144 }
145
146 return Changed;
147}
148
Bill Wendling0f940c92007-12-07 21:42:31 +0000149/// HoistRegion - Walk the specified region of the CFG (defined by all blocks
150/// dominated by the specified block, and that are in the current loop) in depth
151/// first order w.r.t the DominatorTree. This allows us to visit definitions
152/// before uses, allowing us to hoist a loop body in one pass without iteration.
153///
154void MachineLICM::HoistRegion(MachineDomTreeNode *N) {
155 assert(N != 0 && "Null dominator tree node?");
156 MachineBasicBlock *BB = N->getBlock();
157
158 // If this subregion is not in the top level loop at all, exit.
159 if (!CurLoop->contains(BB)) return;
160
Dan Gohmanc475c362009-01-15 22:01:38 +0000161 for (MachineBasicBlock::iterator
162 I = BB->begin(), E = BB->end(); I != E; ) {
163 MachineInstr &MI = *I++;
Bill Wendling0f940c92007-12-07 21:42:31 +0000164
Dan Gohmanc475c362009-01-15 22:01:38 +0000165 // Try hoisting the instruction out of the loop. We can only do this if
166 // all of the operands of the instruction are loop invariant and if it is
167 // safe to hoist the instruction.
168 Hoist(MI);
169 }
Bill Wendling0f940c92007-12-07 21:42:31 +0000170
171 const std::vector<MachineDomTreeNode*> &Children = N->getChildren();
172
173 for (unsigned I = 0, E = Children.size(); I != E; ++I)
174 HoistRegion(Children[I]);
175}
176
Bill Wendling041b3f82007-12-08 23:58:46 +0000177/// IsLoopInvariantInst - Returns true if the instruction is loop
Bill Wendling0f940c92007-12-07 21:42:31 +0000178/// invariant. I.e., all virtual register operands are defined outside of the
Bill Wendling60ff1a32007-12-20 01:08:10 +0000179/// loop, physical registers aren't accessed explicitly, and there are no side
180/// effects that aren't captured by the operands or other flags.
Bill Wendling0f940c92007-12-07 21:42:31 +0000181///
Bill Wendling041b3f82007-12-08 23:58:46 +0000182bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) {
Chris Lattnera22edc82008-01-10 23:08:24 +0000183 const TargetInstrDesc &TID = I.getDesc();
184
185 // Ignore stuff that we obviously can't hoist.
Dan Gohman237dee12008-12-23 17:28:50 +0000186 if (TID.mayStore() || TID.isCall() || TID.isTerminator() ||
Chris Lattnera22edc82008-01-10 23:08:24 +0000187 TID.hasUnmodeledSideEffects())
188 return false;
189
190 if (TID.mayLoad()) {
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000191 // Okay, this instruction does a load. As a refinement, we allow the target
192 // to decide whether the loaded value is actually a constant. If so, we can
193 // actually use it as a load.
194 if (!TII->isInvariantLoad(&I))
Chris Lattnera22edc82008-01-10 23:08:24 +0000195 // FIXME: we should be able to sink loads with no other side effects if
196 // there is nothing that can change memory from here until the end of
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000197 // block. This is a trivial form of alias analysis.
Chris Lattnera22edc82008-01-10 23:08:24 +0000198 return false;
Chris Lattnera22edc82008-01-10 23:08:24 +0000199 }
Bill Wendling074223a2008-03-10 08:13:01 +0000200
Bill Wendling280f4562007-12-18 21:38:04 +0000201 DEBUG({
202 DOUT << "--- Checking if we can hoist " << I;
Chris Lattner749c6f62008-01-07 07:27:27 +0000203 if (I.getDesc().getImplicitUses()) {
Bill Wendling280f4562007-12-18 21:38:04 +0000204 DOUT << " * Instruction has implicit uses:\n";
205
Dan Gohman6f0d0242008-02-10 18:45:23 +0000206 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
Chris Lattner749c6f62008-01-07 07:27:27 +0000207 for (const unsigned *ImpUses = I.getDesc().getImplicitUses();
Chris Lattner69244302008-01-07 01:56:04 +0000208 *ImpUses; ++ImpUses)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000209 DOUT << " -> " << TRI->getName(*ImpUses) << "\n";
Bill Wendling280f4562007-12-18 21:38:04 +0000210 }
211
Chris Lattner749c6f62008-01-07 07:27:27 +0000212 if (I.getDesc().getImplicitDefs()) {
Bill Wendling280f4562007-12-18 21:38:04 +0000213 DOUT << " * Instruction has implicit defines:\n";
214
Dan Gohman6f0d0242008-02-10 18:45:23 +0000215 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
Chris Lattner749c6f62008-01-07 07:27:27 +0000216 for (const unsigned *ImpDefs = I.getDesc().getImplicitDefs();
Chris Lattner69244302008-01-07 01:56:04 +0000217 *ImpDefs; ++ImpDefs)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000218 DOUT << " -> " << TRI->getName(*ImpDefs) << "\n";
Bill Wendling280f4562007-12-18 21:38:04 +0000219 }
Bill Wendling280f4562007-12-18 21:38:04 +0000220 });
221
Bill Wendlingd3361e92008-08-18 00:33:49 +0000222 if (I.getDesc().getImplicitDefs() || I.getDesc().getImplicitUses()) {
223 DOUT << "Cannot hoist with implicit defines or uses\n";
224 return false;
225 }
226
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000227 // The instruction is loop invariant if all of its operands are.
Bill Wendling0f940c92007-12-07 21:42:31 +0000228 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
229 const MachineOperand &MO = I.getOperand(i);
230
Dan Gohmand735b802008-10-03 15:45:36 +0000231 if (!MO.isReg())
Bill Wendlingfb018d02008-08-20 20:32:05 +0000232 continue;
233
Dan Gohmanc475c362009-01-15 22:01:38 +0000234 unsigned Reg = MO.getReg();
235 if (Reg == 0) continue;
236
237 // Don't hoist an instruction that uses or defines a physical register.
238 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Bill Wendlingfb018d02008-08-20 20:32:05 +0000239 return false;
240
241 if (!MO.isUse())
Bill Wendling0f940c92007-12-07 21:42:31 +0000242 continue;
243
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000244 assert(RegInfo->getVRegDef(Reg) &&
245 "Machine instr not mapped for this vreg?!");
Bill Wendling0f940c92007-12-07 21:42:31 +0000246
247 // If the loop contains the definition of an operand, then the instruction
248 // isn't loop invariant.
Bill Wendling9258cd32008-01-02 19:32:43 +0000249 if (CurLoop->contains(RegInfo->getVRegDef(Reg)->getParent()))
Bill Wendling0f940c92007-12-07 21:42:31 +0000250 return false;
251 }
252
253 // If we got this far, the instruction is loop invariant!
254 return true;
255}
256
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000257/// Hoist - When an instruction is found to use only loop invariant operands
258/// that are safe to hoist, this instruction is called to do the dirty work.
Bill Wendling0f940c92007-12-07 21:42:31 +0000259///
Bill Wendlingb48519c2007-12-08 01:47:01 +0000260void MachineLICM::Hoist(MachineInstr &MI) {
Bill Wendling041b3f82007-12-08 23:58:46 +0000261 if (!IsLoopInvariantInst(MI)) return;
Bill Wendling0f940c92007-12-07 21:42:31 +0000262
Dan Gohmanc475c362009-01-15 22:01:38 +0000263 // Now move the instructions to the predecessor, inserting it before any
264 // terminator instructions.
265 DEBUG({
266 DOUT << "Hoisting " << MI;
267 if (CurPreheader->getBasicBlock())
268 DOUT << " to MachineBasicBlock "
269 << CurPreheader->getBasicBlock()->getName();
270 if (MI.getParent()->getBasicBlock())
271 DOUT << " from MachineBasicBlock "
272 << MI.getParent()->getBasicBlock()->getName();
273 DOUT << "\n";
274 });
Bill Wendling0f940c92007-12-07 21:42:31 +0000275
Dan Gohmanc475c362009-01-15 22:01:38 +0000276 CurPreheader->splice(CurPreheader->getFirstTerminator(), MI.getParent(), &MI);
Bill Wendling0f940c92007-12-07 21:42:31 +0000277
Dan Gohmanc475c362009-01-15 22:01:38 +0000278 ++NumHoisted;
Bill Wendling0f940c92007-12-07 21:42:31 +0000279 Changed = true;
Bill Wendling0f940c92007-12-07 21:42:31 +0000280}