blob: 4eac338848fa63acbf72b24a0ff7815799d99d73 [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"
Dan Gohmane33f44c2009-10-07 17:38:06 +000031#include "llvm/Analysis/AliasAnalysis.h"
Evan Chengaf6949d2009-02-05 08:45:46 +000032#include "llvm/ADT/DenseMap.h"
Chris Lattnerac695822008-01-04 06:41:45 +000033#include "llvm/ADT/Statistic.h"
Chris Lattnerac695822008-01-04 06:41:45 +000034#include "llvm/Support/Debug.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000035#include "llvm/Support/raw_ostream.h"
Bill Wendling0f940c92007-12-07 21:42:31 +000036
37using namespace llvm;
38
Bill Wendling041b3f82007-12-08 23:58:46 +000039STATISTIC(NumHoisted, "Number of machine instructions hoisted out of loops");
Evan Chengaf6949d2009-02-05 08:45:46 +000040STATISTIC(NumCSEed, "Number of hoisted machine instructions CSEed");
Bill Wendlingb48519c2007-12-08 01:47:01 +000041
Bill Wendling0f940c92007-12-07 21:42:31 +000042namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000043 class MachineLICM : public MachineFunctionPass {
Bill Wendling9258cd32008-01-02 19:32:43 +000044 const TargetMachine *TM;
Bill Wendlingefe2be72007-12-11 23:27:51 +000045 const TargetInstrInfo *TII;
Dan Gohmana8fb3362009-09-25 23:58:45 +000046 const TargetRegisterInfo *TRI;
Dan Gohman45094e32009-09-26 02:34:00 +000047 BitVector AllocatableSet;
Bill Wendling12ebf142007-12-11 19:40:06 +000048
Bill Wendling0f940c92007-12-07 21:42:31 +000049 // Various analyses that we use...
Dan Gohmane33f44c2009-10-07 17:38:06 +000050 AliasAnalysis *AA; // Alias analysis info.
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +000051 MachineLoopInfo *LI; // Current MachineLoopInfo
52 MachineDominatorTree *DT; // Machine dominator tree for the cur loop
Bill Wendling9258cd32008-01-02 19:32:43 +000053 MachineRegisterInfo *RegInfo; // Machine register information
Bill Wendling0f940c92007-12-07 21:42:31 +000054
Bill Wendling0f940c92007-12-07 21:42:31 +000055 // State that is updated as we process loops
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +000056 bool Changed; // True if a loop is changed.
57 MachineLoop *CurLoop; // The current loop we are working on.
Dan Gohmanc475c362009-01-15 22:01:38 +000058 MachineBasicBlock *CurPreheader; // The preheader for CurLoop.
Evan Chengaf6949d2009-02-05 08:45:46 +000059
60 // For each BB and opcode pair, keep a list of hoisted instructions.
61 DenseMap<std::pair<unsigned, unsigned>,
62 std::vector<const MachineInstr*> > CSEMap;
Bill Wendling0f940c92007-12-07 21:42:31 +000063 public:
64 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000065 MachineLICM() : MachineFunctionPass(&ID) {}
Bill Wendling0f940c92007-12-07 21:42:31 +000066
67 virtual bool runOnMachineFunction(MachineFunction &MF);
68
Dan Gohman72241702008-12-18 01:37:56 +000069 const char *getPassName() const { return "Machine Instruction LICM"; }
70
Bill Wendling074223a2008-03-10 08:13:01 +000071 // FIXME: Loop preheaders?
Bill Wendling0f940c92007-12-07 21:42:31 +000072 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
73 AU.setPreservesCFG();
74 AU.addRequired<MachineLoopInfo>();
75 AU.addRequired<MachineDominatorTree>();
Dan Gohmane33f44c2009-10-07 17:38:06 +000076 AU.addRequired<AliasAnalysis>();
Bill Wendlingd5da7042008-01-04 08:48:49 +000077 AU.addPreserved<MachineLoopInfo>();
78 AU.addPreserved<MachineDominatorTree>();
79 MachineFunctionPass::getAnalysisUsage(AU);
Bill Wendling0f940c92007-12-07 21:42:31 +000080 }
Evan Chengaf6949d2009-02-05 08:45:46 +000081
82 virtual void releaseMemory() {
83 CSEMap.clear();
84 }
85
Bill Wendling0f940c92007-12-07 21:42:31 +000086 private:
Bill Wendling041b3f82007-12-08 23:58:46 +000087 /// IsLoopInvariantInst - Returns true if the instruction is loop
Bill Wendling0f940c92007-12-07 21:42:31 +000088 /// invariant. I.e., all virtual register operands are defined outside of
89 /// the loop, physical registers aren't accessed (explicitly or implicitly),
90 /// and the instruction is hoistable.
91 ///
Bill Wendling041b3f82007-12-08 23:58:46 +000092 bool IsLoopInvariantInst(MachineInstr &I);
Bill Wendling0f940c92007-12-07 21:42:31 +000093
Evan Cheng45e94d62009-02-04 09:19:56 +000094 /// IsProfitableToHoist - Return true if it is potentially profitable to
95 /// hoist the given loop invariant.
96 bool IsProfitableToHoist(MachineInstr &MI);
97
Bill Wendling0f940c92007-12-07 21:42:31 +000098 /// HoistRegion - Walk the specified region of the CFG (defined by all
99 /// blocks dominated by the specified block, and that are in the current
100 /// loop) in depth first order w.r.t the DominatorTree. This allows us to
101 /// visit definitions before uses, allowing us to hoist a loop body in one
102 /// pass without iteration.
103 ///
104 void HoistRegion(MachineDomTreeNode *N);
105
106 /// Hoist - When an instruction is found to only use loop invariant operands
107 /// that is safe to hoist, this instruction is called to do the dirty work.
108 ///
Bill Wendlingb48519c2007-12-08 01:47:01 +0000109 void Hoist(MachineInstr &MI);
Bill Wendling0f940c92007-12-07 21:42:31 +0000110 };
Bill Wendling0f940c92007-12-07 21:42:31 +0000111} // end anonymous namespace
112
Dan Gohman844731a2008-05-13 00:00:25 +0000113char MachineLICM::ID = 0;
114static RegisterPass<MachineLICM>
Bill Wendling8870ce92008-07-07 05:42:27 +0000115X("machinelicm", "Machine Loop Invariant Code Motion");
Dan Gohman844731a2008-05-13 00:00:25 +0000116
Bill Wendling0f940c92007-12-07 21:42:31 +0000117FunctionPass *llvm::createMachineLICMPass() { return new MachineLICM(); }
118
Dan Gohmanc475c362009-01-15 22:01:38 +0000119/// LoopIsOuterMostWithPreheader - Test if the given loop is the outer-most
120/// loop that has a preheader.
121static bool LoopIsOuterMostWithPreheader(MachineLoop *CurLoop) {
122 for (MachineLoop *L = CurLoop->getParentLoop(); L; L = L->getParentLoop())
123 if (L->getLoopPreheader())
124 return false;
125 return true;
126}
127
Bill Wendling0f940c92007-12-07 21:42:31 +0000128/// Hoist expressions out of the specified loop. Note, alias info for inner loop
129/// is not preserved so it is not a good idea to run LICM multiple times on one
130/// loop.
131///
132bool MachineLICM::runOnMachineFunction(MachineFunction &MF) {
Bill Wendlingb7a89922009-08-22 20:25:44 +0000133 DEBUG(errs() << "******** Machine LICM ********\n");
Bill Wendlinga17ad592007-12-11 22:22:22 +0000134
Bill Wendling0f940c92007-12-07 21:42:31 +0000135 Changed = false;
Bill Wendlingacb04ec2008-08-31 02:30:23 +0000136 TM = &MF.getTarget();
Bill Wendling9258cd32008-01-02 19:32:43 +0000137 TII = TM->getInstrInfo();
Dan Gohmana8fb3362009-09-25 23:58:45 +0000138 TRI = TM->getRegisterInfo();
Bill Wendlingacb04ec2008-08-31 02:30:23 +0000139 RegInfo = &MF.getRegInfo();
Dan Gohman45094e32009-09-26 02:34:00 +0000140 AllocatableSet = TRI->getAllocatableSet(MF);
Bill Wendling0f940c92007-12-07 21:42:31 +0000141
142 // Get our Loop information...
143 LI = &getAnalysis<MachineLoopInfo>();
144 DT = &getAnalysis<MachineDominatorTree>();
Dan Gohmane33f44c2009-10-07 17:38:06 +0000145 AA = &getAnalysis<AliasAnalysis>();
Bill Wendling0f940c92007-12-07 21:42:31 +0000146
147 for (MachineLoopInfo::iterator
148 I = LI->begin(), E = LI->end(); I != E; ++I) {
Bill Wendlinga17ad592007-12-11 22:22:22 +0000149 CurLoop = *I;
Bill Wendling0f940c92007-12-07 21:42:31 +0000150
Dan Gohmanc475c362009-01-15 22:01:38 +0000151 // Only visit outer-most preheader-sporting loops.
152 if (!LoopIsOuterMostWithPreheader(CurLoop))
153 continue;
154
155 // Determine the block to which to hoist instructions. If we can't find a
156 // suitable loop preheader, we can't do any hoisting.
157 //
158 // FIXME: We are only hoisting if the basic block coming into this loop
159 // has only one successor. This isn't the case in general because we haven't
160 // broken critical edges or added preheaders.
161 CurPreheader = CurLoop->getLoopPreheader();
162 if (!CurPreheader)
163 continue;
164
165 HoistRegion(DT->getNode(CurLoop->getHeader()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000166 }
167
168 return Changed;
169}
170
Bill Wendling0f940c92007-12-07 21:42:31 +0000171/// HoistRegion - Walk the specified region of the CFG (defined by all blocks
172/// dominated by the specified block, and that are in the current loop) in depth
173/// first order w.r.t the DominatorTree. This allows us to visit definitions
174/// before uses, allowing us to hoist a loop body in one pass without iteration.
175///
176void MachineLICM::HoistRegion(MachineDomTreeNode *N) {
177 assert(N != 0 && "Null dominator tree node?");
178 MachineBasicBlock *BB = N->getBlock();
179
180 // If this subregion is not in the top level loop at all, exit.
181 if (!CurLoop->contains(BB)) return;
182
Dan Gohmanc475c362009-01-15 22:01:38 +0000183 for (MachineBasicBlock::iterator
Evan Chengaf6949d2009-02-05 08:45:46 +0000184 MII = BB->begin(), E = BB->end(); MII != E; ) {
185 MachineBasicBlock::iterator NextMII = MII; ++NextMII;
186 MachineInstr &MI = *MII;
Bill Wendling0f940c92007-12-07 21:42:31 +0000187
Dan Gohmanc475c362009-01-15 22:01:38 +0000188 Hoist(MI);
Evan Chengaf6949d2009-02-05 08:45:46 +0000189
190 MII = NextMII;
Dan Gohmanc475c362009-01-15 22:01:38 +0000191 }
Bill Wendling0f940c92007-12-07 21:42:31 +0000192
193 const std::vector<MachineDomTreeNode*> &Children = N->getChildren();
194
195 for (unsigned I = 0, E = Children.size(); I != E; ++I)
196 HoistRegion(Children[I]);
197}
198
Bill Wendling041b3f82007-12-08 23:58:46 +0000199/// IsLoopInvariantInst - Returns true if the instruction is loop
Bill Wendling0f940c92007-12-07 21:42:31 +0000200/// invariant. I.e., all virtual register operands are defined outside of the
Bill Wendling60ff1a32007-12-20 01:08:10 +0000201/// loop, physical registers aren't accessed explicitly, and there are no side
202/// effects that aren't captured by the operands or other flags.
Bill Wendling0f940c92007-12-07 21:42:31 +0000203///
Bill Wendling041b3f82007-12-08 23:58:46 +0000204bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) {
Chris Lattnera22edc82008-01-10 23:08:24 +0000205 const TargetInstrDesc &TID = I.getDesc();
206
207 // Ignore stuff that we obviously can't hoist.
Dan Gohman237dee12008-12-23 17:28:50 +0000208 if (TID.mayStore() || TID.isCall() || TID.isTerminator() ||
Chris Lattnera22edc82008-01-10 23:08:24 +0000209 TID.hasUnmodeledSideEffects())
210 return false;
Evan Cheng9b61f332009-02-04 07:17:49 +0000211
Chris Lattnera22edc82008-01-10 23:08:24 +0000212 if (TID.mayLoad()) {
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000213 // 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.
Dan Gohmane33f44c2009-10-07 17:38:06 +0000216 if (!I.isInvariantLoad(AA))
Chris Lattnera22edc82008-01-10 23:08:24 +0000217 // 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
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000219 // block. This is a trivial form of alias analysis.
Chris Lattnera22edc82008-01-10 23:08:24 +0000220 return false;
Chris Lattnera22edc82008-01-10 23:08:24 +0000221 }
Bill Wendling074223a2008-03-10 08:13:01 +0000222
Bill Wendling280f4562007-12-18 21:38:04 +0000223 DEBUG({
Bill Wendlingb7a89922009-08-22 20:25:44 +0000224 errs() << "--- Checking if we can hoist " << I;
Chris Lattner749c6f62008-01-07 07:27:27 +0000225 if (I.getDesc().getImplicitUses()) {
Bill Wendlingb7a89922009-08-22 20:25:44 +0000226 errs() << " * Instruction has implicit uses:\n";
Bill Wendling280f4562007-12-18 21:38:04 +0000227
Dan Gohman6f0d0242008-02-10 18:45:23 +0000228 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
Chris Lattner749c6f62008-01-07 07:27:27 +0000229 for (const unsigned *ImpUses = I.getDesc().getImplicitUses();
Chris Lattner69244302008-01-07 01:56:04 +0000230 *ImpUses; ++ImpUses)
Bill Wendlingb7a89922009-08-22 20:25:44 +0000231 errs() << " -> " << TRI->getName(*ImpUses) << "\n";
Bill Wendling280f4562007-12-18 21:38:04 +0000232 }
233
Chris Lattner749c6f62008-01-07 07:27:27 +0000234 if (I.getDesc().getImplicitDefs()) {
Bill Wendlingb7a89922009-08-22 20:25:44 +0000235 errs() << " * Instruction has implicit defines:\n";
Bill Wendling280f4562007-12-18 21:38:04 +0000236
Dan Gohman6f0d0242008-02-10 18:45:23 +0000237 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
Chris Lattner749c6f62008-01-07 07:27:27 +0000238 for (const unsigned *ImpDefs = I.getDesc().getImplicitDefs();
Chris Lattner69244302008-01-07 01:56:04 +0000239 *ImpDefs; ++ImpDefs)
Bill Wendlingb7a89922009-08-22 20:25:44 +0000240 errs() << " -> " << TRI->getName(*ImpDefs) << "\n";
Bill Wendling280f4562007-12-18 21:38:04 +0000241 }
Bill Wendling280f4562007-12-18 21:38:04 +0000242 });
243
Bill Wendlingd3361e92008-08-18 00:33:49 +0000244 if (I.getDesc().getImplicitDefs() || I.getDesc().getImplicitUses()) {
Bill Wendlingb7a89922009-08-22 20:25:44 +0000245 DEBUG(errs() << "Cannot hoist with implicit defines or uses\n");
Bill Wendlingd3361e92008-08-18 00:33:49 +0000246 return false;
247 }
248
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000249 // The instruction is loop invariant if all of its operands are.
Bill Wendling0f940c92007-12-07 21:42:31 +0000250 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
251 const MachineOperand &MO = I.getOperand(i);
252
Dan Gohmand735b802008-10-03 15:45:36 +0000253 if (!MO.isReg())
Bill Wendlingfb018d02008-08-20 20:32:05 +0000254 continue;
255
Dan Gohmanc475c362009-01-15 22:01:38 +0000256 unsigned Reg = MO.getReg();
257 if (Reg == 0) continue;
258
259 // Don't hoist an instruction that uses or defines a physical register.
Dan Gohmana8fb3362009-09-25 23:58:45 +0000260 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Dan Gohmana8fb3362009-09-25 23:58:45 +0000261 if (MO.isUse()) {
262 // If the physreg has no defs anywhere, it's just an ambient register
Dan Gohman45094e32009-09-26 02:34:00 +0000263 // and we can freely move its uses. Alternatively, if it's allocatable,
264 // it could get allocated to something with a def during allocation.
Dan Gohmana8fb3362009-09-25 23:58:45 +0000265 if (!RegInfo->def_empty(Reg))
266 return false;
Dan Gohman45094e32009-09-26 02:34:00 +0000267 if (AllocatableSet.test(Reg))
268 return false;
Dan Gohmana8fb3362009-09-25 23:58:45 +0000269 // Check for a def among the register's aliases too.
Dan Gohman45094e32009-09-26 02:34:00 +0000270 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
271 unsigned AliasReg = *Alias;
272 if (!RegInfo->def_empty(AliasReg))
Dan Gohmana8fb3362009-09-25 23:58:45 +0000273 return false;
Dan Gohman45094e32009-09-26 02:34:00 +0000274 if (AllocatableSet.test(AliasReg))
275 return false;
276 }
Dan Gohmana8fb3362009-09-25 23:58:45 +0000277 // Otherwise it's safe to move.
278 continue;
279 } else if (!MO.isDead()) {
280 // A def that isn't dead. We can't move it.
281 return false;
282 }
283 }
Bill Wendlingfb018d02008-08-20 20:32:05 +0000284
285 if (!MO.isUse())
Bill Wendling0f940c92007-12-07 21:42:31 +0000286 continue;
287
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000288 assert(RegInfo->getVRegDef(Reg) &&
289 "Machine instr not mapped for this vreg?!");
Bill Wendling0f940c92007-12-07 21:42:31 +0000290
291 // If the loop contains the definition of an operand, then the instruction
292 // isn't loop invariant.
Bill Wendling9258cd32008-01-02 19:32:43 +0000293 if (CurLoop->contains(RegInfo->getVRegDef(Reg)->getParent()))
Bill Wendling0f940c92007-12-07 21:42:31 +0000294 return false;
295 }
296
297 // If we got this far, the instruction is loop invariant!
298 return true;
299}
300
Evan Chengaf6949d2009-02-05 08:45:46 +0000301
302/// HasPHIUses - Return true if the specified register has any PHI use.
303static bool HasPHIUses(unsigned Reg, MachineRegisterInfo *RegInfo) {
Evan Cheng45e94d62009-02-04 09:19:56 +0000304 for (MachineRegisterInfo::use_iterator UI = RegInfo->use_begin(Reg),
305 UE = RegInfo->use_end(); UI != UE; ++UI) {
306 MachineInstr *UseMI = &*UI;
Evan Chengaf6949d2009-02-05 08:45:46 +0000307 if (UseMI->getOpcode() == TargetInstrInfo::PHI)
308 return true;
Evan Cheng45e94d62009-02-04 09:19:56 +0000309 }
Evan Chengaf6949d2009-02-05 08:45:46 +0000310 return false;
Evan Cheng45e94d62009-02-04 09:19:56 +0000311}
312
313/// IsProfitableToHoist - Return true if it is potentially profitable to hoist
314/// the given loop invariant.
315bool MachineLICM::IsProfitableToHoist(MachineInstr &MI) {
Evan Chengefc78392009-02-27 00:02:22 +0000316 if (MI.getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
317 return false;
318
Evan Cheng45e94d62009-02-04 09:19:56 +0000319 // FIXME: For now, only hoist re-materilizable instructions. LICM will
320 // increase register pressure. We want to make sure it doesn't increase
321 // spilling.
Dan Gohmana70dca12009-10-09 23:27:56 +0000322 if (!TII->isTriviallyReMaterializable(&MI, AA))
Evan Cheng45e94d62009-02-04 09:19:56 +0000323 return false;
324
Evan Chengaf6949d2009-02-05 08:45:46 +0000325 // If result(s) of this instruction is used by PHIs, then don't hoist it.
326 // The presence of joins makes it difficult for current register allocator
327 // implementation to perform remat.
Evan Cheng45e94d62009-02-04 09:19:56 +0000328 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
329 const MachineOperand &MO = MI.getOperand(i);
330 if (!MO.isReg() || !MO.isDef())
331 continue;
Evan Chengaf6949d2009-02-05 08:45:46 +0000332 if (HasPHIUses(MO.getReg(), RegInfo))
333 return false;
Evan Cheng45e94d62009-02-04 09:19:56 +0000334 }
Evan Chengaf6949d2009-02-05 08:45:46 +0000335
336 return true;
337}
338
339static const MachineInstr *LookForDuplicate(const MachineInstr *MI,
Evan Chengefc78392009-02-27 00:02:22 +0000340 std::vector<const MachineInstr*> &PrevMIs,
341 MachineRegisterInfo *RegInfo) {
Evan Chengaf6949d2009-02-05 08:45:46 +0000342 unsigned NumOps = MI->getNumOperands();
343 for (unsigned i = 0, e = PrevMIs.size(); i != e; ++i) {
344 const MachineInstr *PrevMI = PrevMIs[i];
345 unsigned NumOps2 = PrevMI->getNumOperands();
346 if (NumOps != NumOps2)
347 continue;
348 bool IsSame = true;
349 for (unsigned j = 0; j != NumOps; ++j) {
350 const MachineOperand &MO = MI->getOperand(j);
Evan Chengefc78392009-02-27 00:02:22 +0000351 if (MO.isReg() && MO.isDef()) {
352 if (RegInfo->getRegClass(MO.getReg()) !=
353 RegInfo->getRegClass(PrevMI->getOperand(j).getReg())) {
354 IsSame = false;
355 break;
356 }
Evan Chengaf6949d2009-02-05 08:45:46 +0000357 continue;
Evan Chengefc78392009-02-27 00:02:22 +0000358 }
Evan Chengaf6949d2009-02-05 08:45:46 +0000359 if (!MO.isIdenticalTo(PrevMI->getOperand(j))) {
360 IsSame = false;
361 break;
362 }
363 }
364 if (IsSame)
365 return PrevMI;
366 }
367 return 0;
Evan Cheng45e94d62009-02-04 09:19:56 +0000368}
369
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000370/// Hoist - When an instruction is found to use only loop invariant operands
371/// that are safe to hoist, this instruction is called to do the dirty work.
Bill Wendling0f940c92007-12-07 21:42:31 +0000372///
Bill Wendlingb48519c2007-12-08 01:47:01 +0000373void MachineLICM::Hoist(MachineInstr &MI) {
Bill Wendling041b3f82007-12-08 23:58:46 +0000374 if (!IsLoopInvariantInst(MI)) return;
Evan Cheng45e94d62009-02-04 09:19:56 +0000375 if (!IsProfitableToHoist(MI)) return;
Bill Wendling0f940c92007-12-07 21:42:31 +0000376
Dan Gohmanc475c362009-01-15 22:01:38 +0000377 // Now move the instructions to the predecessor, inserting it before any
378 // terminator instructions.
379 DEBUG({
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000380 errs() << "Hoisting " << MI;
Dan Gohmanc475c362009-01-15 22:01:38 +0000381 if (CurPreheader->getBasicBlock())
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000382 errs() << " to MachineBasicBlock "
383 << CurPreheader->getBasicBlock()->getName();
Dan Gohmanc475c362009-01-15 22:01:38 +0000384 if (MI.getParent()->getBasicBlock())
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000385 errs() << " from MachineBasicBlock "
386 << MI.getParent()->getBasicBlock()->getName();
387 errs() << "\n";
Dan Gohmanc475c362009-01-15 22:01:38 +0000388 });
Bill Wendling0f940c92007-12-07 21:42:31 +0000389
Evan Chengaf6949d2009-02-05 08:45:46 +0000390 // Look for opportunity to CSE the hoisted instruction.
391 std::pair<unsigned, unsigned> BBOpcPair =
392 std::make_pair(CurPreheader->getNumber(), MI.getOpcode());
393 DenseMap<std::pair<unsigned, unsigned>,
394 std::vector<const MachineInstr*> >::iterator CI = CSEMap.find(BBOpcPair);
395 bool DoneCSE = false;
396 if (CI != CSEMap.end()) {
Evan Chengefc78392009-02-27 00:02:22 +0000397 const MachineInstr *Dup = LookForDuplicate(&MI, CI->second, RegInfo);
Evan Chengaf6949d2009-02-05 08:45:46 +0000398 if (Dup) {
Bill Wendlingb7a89922009-08-22 20:25:44 +0000399 DEBUG(errs() << "CSEing " << MI << " with " << *Dup);
Evan Chengaf6949d2009-02-05 08:45:46 +0000400 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
401 const MachineOperand &MO = MI.getOperand(i);
402 if (MO.isReg() && MO.isDef())
403 RegInfo->replaceRegWith(MO.getReg(), Dup->getOperand(i).getReg());
404 }
405 MI.eraseFromParent();
406 DoneCSE = true;
407 ++NumCSEed;
408 }
409 }
410
411 // Otherwise, splice the instruction to the preheader.
412 if (!DoneCSE) {
413 CurPreheader->splice(CurPreheader->getFirstTerminator(),
414 MI.getParent(), &MI);
415 // Add to the CSE map.
416 if (CI != CSEMap.end())
417 CI->second.push_back(&MI);
418 else {
419 std::vector<const MachineInstr*> CSEMIs;
420 CSEMIs.push_back(&MI);
421 CSEMap.insert(std::make_pair(BBOpcPair, CSEMIs));
422 }
423 }
Bill Wendling0f940c92007-12-07 21:42:31 +0000424
Dan Gohmanc475c362009-01-15 22:01:38 +0000425 ++NumHoisted;
Bill Wendling0f940c92007-12-07 21:42:31 +0000426 Changed = true;
Bill Wendling0f940c92007-12-07 21:42:31 +0000427}