blob: b5827aadcf5ba30dba9eb2ceeb5bf71c539a569d [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/Compiler.h"
35#include "llvm/Support/Debug.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000036#include "llvm/Support/raw_ostream.h"
Bill Wendling0f940c92007-12-07 21:42:31 +000037
38using namespace llvm;
39
Bill Wendling041b3f82007-12-08 23:58:46 +000040STATISTIC(NumHoisted, "Number of machine instructions hoisted out of loops");
Evan Chengaf6949d2009-02-05 08:45:46 +000041STATISTIC(NumCSEed, "Number of hoisted machine instructions CSEed");
Bill Wendlingb48519c2007-12-08 01:47:01 +000042
Bill Wendling0f940c92007-12-07 21:42:31 +000043namespace {
44 class VISIBILITY_HIDDEN MachineLICM : public MachineFunctionPass {
Bill Wendling9258cd32008-01-02 19:32:43 +000045 const TargetMachine *TM;
Bill Wendlingefe2be72007-12-11 23:27:51 +000046 const TargetInstrInfo *TII;
Dan Gohmana8fb3362009-09-25 23:58:45 +000047 const TargetRegisterInfo *TRI;
Dan Gohman45094e32009-09-26 02:34:00 +000048 BitVector AllocatableSet;
Bill Wendling12ebf142007-12-11 19:40:06 +000049
Bill Wendling0f940c92007-12-07 21:42:31 +000050 // Various analyses that we use...
Dan Gohmane33f44c2009-10-07 17:38:06 +000051 AliasAnalysis *AA; // Alias analysis info.
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +000052 MachineLoopInfo *LI; // Current MachineLoopInfo
53 MachineDominatorTree *DT; // Machine dominator tree for the cur loop
Bill Wendling9258cd32008-01-02 19:32:43 +000054 MachineRegisterInfo *RegInfo; // Machine register information
Bill Wendling0f940c92007-12-07 21:42:31 +000055
Bill Wendling0f940c92007-12-07 21:42:31 +000056 // State that is updated as we process loops
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +000057 bool Changed; // True if a loop is changed.
58 MachineLoop *CurLoop; // The current loop we are working on.
Dan Gohmanc475c362009-01-15 22:01:38 +000059 MachineBasicBlock *CurPreheader; // The preheader for CurLoop.
Evan Chengaf6949d2009-02-05 08:45:46 +000060
61 // For each BB and opcode pair, keep a list of hoisted instructions.
62 DenseMap<std::pair<unsigned, unsigned>,
63 std::vector<const MachineInstr*> > CSEMap;
Bill Wendling0f940c92007-12-07 21:42:31 +000064 public:
65 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000066 MachineLICM() : MachineFunctionPass(&ID) {}
Bill Wendling0f940c92007-12-07 21:42:31 +000067
68 virtual bool runOnMachineFunction(MachineFunction &MF);
69
Dan Gohman72241702008-12-18 01:37:56 +000070 const char *getPassName() const { return "Machine Instruction LICM"; }
71
Bill Wendling074223a2008-03-10 08:13:01 +000072 // FIXME: Loop preheaders?
Bill Wendling0f940c92007-12-07 21:42:31 +000073 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
74 AU.setPreservesCFG();
75 AU.addRequired<MachineLoopInfo>();
76 AU.addRequired<MachineDominatorTree>();
Dan Gohmane33f44c2009-10-07 17:38:06 +000077 AU.addRequired<AliasAnalysis>();
Bill Wendlingd5da7042008-01-04 08:48:49 +000078 AU.addPreserved<MachineLoopInfo>();
79 AU.addPreserved<MachineDominatorTree>();
80 MachineFunctionPass::getAnalysisUsage(AU);
Bill Wendling0f940c92007-12-07 21:42:31 +000081 }
Evan Chengaf6949d2009-02-05 08:45:46 +000082
83 virtual void releaseMemory() {
84 CSEMap.clear();
85 }
86
Bill Wendling0f940c92007-12-07 21:42:31 +000087 private:
Bill Wendling041b3f82007-12-08 23:58:46 +000088 /// IsLoopInvariantInst - Returns true if the instruction is loop
Bill Wendling0f940c92007-12-07 21:42:31 +000089 /// invariant. I.e., all virtual register operands are defined outside of
90 /// the loop, physical registers aren't accessed (explicitly or implicitly),
91 /// and the instruction is hoistable.
92 ///
Bill Wendling041b3f82007-12-08 23:58:46 +000093 bool IsLoopInvariantInst(MachineInstr &I);
Bill Wendling0f940c92007-12-07 21:42:31 +000094
Evan Cheng45e94d62009-02-04 09:19:56 +000095 /// IsProfitableToHoist - Return true if it is potentially profitable to
96 /// hoist the given loop invariant.
97 bool IsProfitableToHoist(MachineInstr &MI);
98
Bill Wendling0f940c92007-12-07 21:42:31 +000099 /// HoistRegion - Walk the specified region of the CFG (defined by all
100 /// blocks dominated by the specified block, and that are in the current
101 /// loop) in depth first order w.r.t the DominatorTree. This allows us to
102 /// visit definitions before uses, allowing us to hoist a loop body in one
103 /// pass without iteration.
104 ///
105 void HoistRegion(MachineDomTreeNode *N);
106
107 /// Hoist - When an instruction is found to only use loop invariant operands
108 /// that is safe to hoist, this instruction is called to do the dirty work.
109 ///
Bill Wendlingb48519c2007-12-08 01:47:01 +0000110 void Hoist(MachineInstr &MI);
Bill Wendling0f940c92007-12-07 21:42:31 +0000111 };
Bill Wendling0f940c92007-12-07 21:42:31 +0000112} // end anonymous namespace
113
Dan Gohman844731a2008-05-13 00:00:25 +0000114char MachineLICM::ID = 0;
115static RegisterPass<MachineLICM>
Bill Wendling8870ce92008-07-07 05:42:27 +0000116X("machinelicm", "Machine Loop Invariant Code Motion");
Dan Gohman844731a2008-05-13 00:00:25 +0000117
Bill Wendling0f940c92007-12-07 21:42:31 +0000118FunctionPass *llvm::createMachineLICMPass() { return new MachineLICM(); }
119
Dan Gohmanc475c362009-01-15 22:01:38 +0000120/// LoopIsOuterMostWithPreheader - Test if the given loop is the outer-most
121/// loop that has a preheader.
122static bool LoopIsOuterMostWithPreheader(MachineLoop *CurLoop) {
123 for (MachineLoop *L = CurLoop->getParentLoop(); L; L = L->getParentLoop())
124 if (L->getLoopPreheader())
125 return false;
126 return true;
127}
128
Bill Wendling0f940c92007-12-07 21:42:31 +0000129/// Hoist expressions out of the specified loop. Note, alias info for inner loop
130/// is not preserved so it is not a good idea to run LICM multiple times on one
131/// loop.
132///
133bool MachineLICM::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng740854b2009-02-05 08:51:13 +0000134 const Function *F = MF.getFunction();
135 if (F->hasFnAttr(Attribute::OptimizeForSize))
136 return false;
137
Bill Wendlingb7a89922009-08-22 20:25:44 +0000138 DEBUG(errs() << "******** Machine LICM ********\n");
Bill Wendlinga17ad592007-12-11 22:22:22 +0000139
Bill Wendling0f940c92007-12-07 21:42:31 +0000140 Changed = false;
Bill Wendlingacb04ec2008-08-31 02:30:23 +0000141 TM = &MF.getTarget();
Bill Wendling9258cd32008-01-02 19:32:43 +0000142 TII = TM->getInstrInfo();
Dan Gohmana8fb3362009-09-25 23:58:45 +0000143 TRI = TM->getRegisterInfo();
Bill Wendlingacb04ec2008-08-31 02:30:23 +0000144 RegInfo = &MF.getRegInfo();
Dan Gohman45094e32009-09-26 02:34:00 +0000145 AllocatableSet = TRI->getAllocatableSet(MF);
Bill Wendling0f940c92007-12-07 21:42:31 +0000146
147 // Get our Loop information...
148 LI = &getAnalysis<MachineLoopInfo>();
149 DT = &getAnalysis<MachineDominatorTree>();
Dan Gohmane33f44c2009-10-07 17:38:06 +0000150 AA = &getAnalysis<AliasAnalysis>();
Bill Wendling0f940c92007-12-07 21:42:31 +0000151
152 for (MachineLoopInfo::iterator
153 I = LI->begin(), E = LI->end(); I != E; ++I) {
Bill Wendlinga17ad592007-12-11 22:22:22 +0000154 CurLoop = *I;
Bill Wendling0f940c92007-12-07 21:42:31 +0000155
Dan Gohmanc475c362009-01-15 22:01:38 +0000156 // Only visit outer-most preheader-sporting loops.
157 if (!LoopIsOuterMostWithPreheader(CurLoop))
158 continue;
159
160 // Determine the block to which to hoist instructions. If we can't find a
161 // suitable loop preheader, we can't do any hoisting.
162 //
163 // FIXME: We are only hoisting if the basic block coming into this loop
164 // has only one successor. This isn't the case in general because we haven't
165 // broken critical edges or added preheaders.
166 CurPreheader = CurLoop->getLoopPreheader();
167 if (!CurPreheader)
168 continue;
169
170 HoistRegion(DT->getNode(CurLoop->getHeader()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000171 }
172
173 return Changed;
174}
175
Bill Wendling0f940c92007-12-07 21:42:31 +0000176/// HoistRegion - Walk the specified region of the CFG (defined by all blocks
177/// dominated by the specified block, and that are in the current loop) in depth
178/// first order w.r.t the DominatorTree. This allows us to visit definitions
179/// before uses, allowing us to hoist a loop body in one pass without iteration.
180///
181void MachineLICM::HoistRegion(MachineDomTreeNode *N) {
182 assert(N != 0 && "Null dominator tree node?");
183 MachineBasicBlock *BB = N->getBlock();
184
185 // If this subregion is not in the top level loop at all, exit.
186 if (!CurLoop->contains(BB)) return;
187
Dan Gohmanc475c362009-01-15 22:01:38 +0000188 for (MachineBasicBlock::iterator
Evan Chengaf6949d2009-02-05 08:45:46 +0000189 MII = BB->begin(), E = BB->end(); MII != E; ) {
190 MachineBasicBlock::iterator NextMII = MII; ++NextMII;
191 MachineInstr &MI = *MII;
Bill Wendling0f940c92007-12-07 21:42:31 +0000192
Dan Gohmanc475c362009-01-15 22:01:38 +0000193 Hoist(MI);
Evan Chengaf6949d2009-02-05 08:45:46 +0000194
195 MII = NextMII;
Dan Gohmanc475c362009-01-15 22:01:38 +0000196 }
Bill Wendling0f940c92007-12-07 21:42:31 +0000197
198 const std::vector<MachineDomTreeNode*> &Children = N->getChildren();
199
200 for (unsigned I = 0, E = Children.size(); I != E; ++I)
201 HoistRegion(Children[I]);
202}
203
Bill Wendling041b3f82007-12-08 23:58:46 +0000204/// IsLoopInvariantInst - Returns true if the instruction is loop
Bill Wendling0f940c92007-12-07 21:42:31 +0000205/// invariant. I.e., all virtual register operands are defined outside of the
Bill Wendling60ff1a32007-12-20 01:08:10 +0000206/// loop, physical registers aren't accessed explicitly, and there are no side
207/// effects that aren't captured by the operands or other flags.
Bill Wendling0f940c92007-12-07 21:42:31 +0000208///
Bill Wendling041b3f82007-12-08 23:58:46 +0000209bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) {
Chris Lattnera22edc82008-01-10 23:08:24 +0000210 const TargetInstrDesc &TID = I.getDesc();
211
212 // Ignore stuff that we obviously can't hoist.
Dan Gohman237dee12008-12-23 17:28:50 +0000213 if (TID.mayStore() || TID.isCall() || TID.isTerminator() ||
Chris Lattnera22edc82008-01-10 23:08:24 +0000214 TID.hasUnmodeledSideEffects())
215 return false;
Evan Cheng9b61f332009-02-04 07:17:49 +0000216
Chris Lattnera22edc82008-01-10 23:08:24 +0000217 if (TID.mayLoad()) {
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000218 // Okay, this instruction does a load. As a refinement, we allow the target
219 // to decide whether the loaded value is actually a constant. If so, we can
220 // actually use it as a load.
Dan Gohmane33f44c2009-10-07 17:38:06 +0000221 if (!I.isInvariantLoad(AA))
Chris Lattnera22edc82008-01-10 23:08:24 +0000222 // FIXME: we should be able to sink loads with no other side effects if
223 // there is nothing that can change memory from here until the end of
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000224 // block. This is a trivial form of alias analysis.
Chris Lattnera22edc82008-01-10 23:08:24 +0000225 return false;
Chris Lattnera22edc82008-01-10 23:08:24 +0000226 }
Bill Wendling074223a2008-03-10 08:13:01 +0000227
Bill Wendling280f4562007-12-18 21:38:04 +0000228 DEBUG({
Bill Wendlingb7a89922009-08-22 20:25:44 +0000229 errs() << "--- Checking if we can hoist " << I;
Chris Lattner749c6f62008-01-07 07:27:27 +0000230 if (I.getDesc().getImplicitUses()) {
Bill Wendlingb7a89922009-08-22 20:25:44 +0000231 errs() << " * Instruction has implicit uses:\n";
Bill Wendling280f4562007-12-18 21:38:04 +0000232
Dan Gohman6f0d0242008-02-10 18:45:23 +0000233 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
Chris Lattner749c6f62008-01-07 07:27:27 +0000234 for (const unsigned *ImpUses = I.getDesc().getImplicitUses();
Chris Lattner69244302008-01-07 01:56:04 +0000235 *ImpUses; ++ImpUses)
Bill Wendlingb7a89922009-08-22 20:25:44 +0000236 errs() << " -> " << TRI->getName(*ImpUses) << "\n";
Bill Wendling280f4562007-12-18 21:38:04 +0000237 }
238
Chris Lattner749c6f62008-01-07 07:27:27 +0000239 if (I.getDesc().getImplicitDefs()) {
Bill Wendlingb7a89922009-08-22 20:25:44 +0000240 errs() << " * Instruction has implicit defines:\n";
Bill Wendling280f4562007-12-18 21:38:04 +0000241
Dan Gohman6f0d0242008-02-10 18:45:23 +0000242 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
Chris Lattner749c6f62008-01-07 07:27:27 +0000243 for (const unsigned *ImpDefs = I.getDesc().getImplicitDefs();
Chris Lattner69244302008-01-07 01:56:04 +0000244 *ImpDefs; ++ImpDefs)
Bill Wendlingb7a89922009-08-22 20:25:44 +0000245 errs() << " -> " << TRI->getName(*ImpDefs) << "\n";
Bill Wendling280f4562007-12-18 21:38:04 +0000246 }
Bill Wendling280f4562007-12-18 21:38:04 +0000247 });
248
Bill Wendlingd3361e92008-08-18 00:33:49 +0000249 if (I.getDesc().getImplicitDefs() || I.getDesc().getImplicitUses()) {
Bill Wendlingb7a89922009-08-22 20:25:44 +0000250 DEBUG(errs() << "Cannot hoist with implicit defines or uses\n");
Bill Wendlingd3361e92008-08-18 00:33:49 +0000251 return false;
252 }
253
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000254 // The instruction is loop invariant if all of its operands are.
Bill Wendling0f940c92007-12-07 21:42:31 +0000255 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
256 const MachineOperand &MO = I.getOperand(i);
257
Dan Gohmand735b802008-10-03 15:45:36 +0000258 if (!MO.isReg())
Bill Wendlingfb018d02008-08-20 20:32:05 +0000259 continue;
260
Dan Gohmanc475c362009-01-15 22:01:38 +0000261 unsigned Reg = MO.getReg();
262 if (Reg == 0) continue;
263
264 // Don't hoist an instruction that uses or defines a physical register.
Dan Gohmana8fb3362009-09-25 23:58:45 +0000265 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Dan Gohmana8fb3362009-09-25 23:58:45 +0000266 if (MO.isUse()) {
267 // If the physreg has no defs anywhere, it's just an ambient register
Dan Gohman45094e32009-09-26 02:34:00 +0000268 // and we can freely move its uses. Alternatively, if it's allocatable,
269 // it could get allocated to something with a def during allocation.
Dan Gohmana8fb3362009-09-25 23:58:45 +0000270 if (!RegInfo->def_empty(Reg))
271 return false;
Dan Gohman45094e32009-09-26 02:34:00 +0000272 if (AllocatableSet.test(Reg))
273 return false;
Dan Gohmana8fb3362009-09-25 23:58:45 +0000274 // Check for a def among the register's aliases too.
Dan Gohman45094e32009-09-26 02:34:00 +0000275 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
276 unsigned AliasReg = *Alias;
277 if (!RegInfo->def_empty(AliasReg))
Dan Gohmana8fb3362009-09-25 23:58:45 +0000278 return false;
Dan Gohman45094e32009-09-26 02:34:00 +0000279 if (AllocatableSet.test(AliasReg))
280 return false;
281 }
Dan Gohmana8fb3362009-09-25 23:58:45 +0000282 // Otherwise it's safe to move.
283 continue;
284 } else if (!MO.isDead()) {
285 // A def that isn't dead. We can't move it.
286 return false;
287 }
288 }
Bill Wendlingfb018d02008-08-20 20:32:05 +0000289
290 if (!MO.isUse())
Bill Wendling0f940c92007-12-07 21:42:31 +0000291 continue;
292
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000293 assert(RegInfo->getVRegDef(Reg) &&
294 "Machine instr not mapped for this vreg?!");
Bill Wendling0f940c92007-12-07 21:42:31 +0000295
296 // If the loop contains the definition of an operand, then the instruction
297 // isn't loop invariant.
Bill Wendling9258cd32008-01-02 19:32:43 +0000298 if (CurLoop->contains(RegInfo->getVRegDef(Reg)->getParent()))
Bill Wendling0f940c92007-12-07 21:42:31 +0000299 return false;
300 }
301
302 // If we got this far, the instruction is loop invariant!
303 return true;
304}
305
Evan Chengaf6949d2009-02-05 08:45:46 +0000306
307/// HasPHIUses - Return true if the specified register has any PHI use.
308static bool HasPHIUses(unsigned Reg, MachineRegisterInfo *RegInfo) {
Evan Cheng45e94d62009-02-04 09:19:56 +0000309 for (MachineRegisterInfo::use_iterator UI = RegInfo->use_begin(Reg),
310 UE = RegInfo->use_end(); UI != UE; ++UI) {
311 MachineInstr *UseMI = &*UI;
Evan Chengaf6949d2009-02-05 08:45:46 +0000312 if (UseMI->getOpcode() == TargetInstrInfo::PHI)
313 return true;
Evan Cheng45e94d62009-02-04 09:19:56 +0000314 }
Evan Chengaf6949d2009-02-05 08:45:46 +0000315 return false;
Evan Cheng45e94d62009-02-04 09:19:56 +0000316}
317
318/// IsProfitableToHoist - Return true if it is potentially profitable to hoist
319/// the given loop invariant.
320bool MachineLICM::IsProfitableToHoist(MachineInstr &MI) {
Evan Chengefc78392009-02-27 00:02:22 +0000321 if (MI.getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
322 return false;
323
Evan Cheng45e94d62009-02-04 09:19:56 +0000324 const TargetInstrDesc &TID = MI.getDesc();
325
Evan Cheng45e94d62009-02-04 09:19:56 +0000326 // FIXME: For now, only hoist re-materilizable instructions. LICM will
327 // increase register pressure. We want to make sure it doesn't increase
328 // spilling.
Evan Cheng5caa8832009-02-04 09:21:58 +0000329 if (!TID.mayLoad() && (!TID.isRematerializable() ||
330 !TII->isTriviallyReMaterializable(&MI)))
Evan Cheng45e94d62009-02-04 09:19:56 +0000331 return false;
332
Evan Chengaf6949d2009-02-05 08:45:46 +0000333 // If result(s) of this instruction is used by PHIs, then don't hoist it.
334 // The presence of joins makes it difficult for current register allocator
335 // implementation to perform remat.
Evan Cheng45e94d62009-02-04 09:19:56 +0000336 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
337 const MachineOperand &MO = MI.getOperand(i);
338 if (!MO.isReg() || !MO.isDef())
339 continue;
Evan Chengaf6949d2009-02-05 08:45:46 +0000340 if (HasPHIUses(MO.getReg(), RegInfo))
341 return false;
Evan Cheng45e94d62009-02-04 09:19:56 +0000342 }
Evan Chengaf6949d2009-02-05 08:45:46 +0000343
344 return true;
345}
346
347static const MachineInstr *LookForDuplicate(const MachineInstr *MI,
Evan Chengefc78392009-02-27 00:02:22 +0000348 std::vector<const MachineInstr*> &PrevMIs,
349 MachineRegisterInfo *RegInfo) {
Evan Chengaf6949d2009-02-05 08:45:46 +0000350 unsigned NumOps = MI->getNumOperands();
351 for (unsigned i = 0, e = PrevMIs.size(); i != e; ++i) {
352 const MachineInstr *PrevMI = PrevMIs[i];
353 unsigned NumOps2 = PrevMI->getNumOperands();
354 if (NumOps != NumOps2)
355 continue;
356 bool IsSame = true;
357 for (unsigned j = 0; j != NumOps; ++j) {
358 const MachineOperand &MO = MI->getOperand(j);
Evan Chengefc78392009-02-27 00:02:22 +0000359 if (MO.isReg() && MO.isDef()) {
360 if (RegInfo->getRegClass(MO.getReg()) !=
361 RegInfo->getRegClass(PrevMI->getOperand(j).getReg())) {
362 IsSame = false;
363 break;
364 }
Evan Chengaf6949d2009-02-05 08:45:46 +0000365 continue;
Evan Chengefc78392009-02-27 00:02:22 +0000366 }
Evan Chengaf6949d2009-02-05 08:45:46 +0000367 if (!MO.isIdenticalTo(PrevMI->getOperand(j))) {
368 IsSame = false;
369 break;
370 }
371 }
372 if (IsSame)
373 return PrevMI;
374 }
375 return 0;
Evan Cheng45e94d62009-02-04 09:19:56 +0000376}
377
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000378/// Hoist - When an instruction is found to use only loop invariant operands
379/// that are safe to hoist, this instruction is called to do the dirty work.
Bill Wendling0f940c92007-12-07 21:42:31 +0000380///
Bill Wendlingb48519c2007-12-08 01:47:01 +0000381void MachineLICM::Hoist(MachineInstr &MI) {
Bill Wendling041b3f82007-12-08 23:58:46 +0000382 if (!IsLoopInvariantInst(MI)) return;
Evan Cheng45e94d62009-02-04 09:19:56 +0000383 if (!IsProfitableToHoist(MI)) return;
Bill Wendling0f940c92007-12-07 21:42:31 +0000384
Dan Gohmanc475c362009-01-15 22:01:38 +0000385 // Now move the instructions to the predecessor, inserting it before any
386 // terminator instructions.
387 DEBUG({
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000388 errs() << "Hoisting " << MI;
Dan Gohmanc475c362009-01-15 22:01:38 +0000389 if (CurPreheader->getBasicBlock())
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000390 errs() << " to MachineBasicBlock "
391 << CurPreheader->getBasicBlock()->getName();
Dan Gohmanc475c362009-01-15 22:01:38 +0000392 if (MI.getParent()->getBasicBlock())
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000393 errs() << " from MachineBasicBlock "
394 << MI.getParent()->getBasicBlock()->getName();
395 errs() << "\n";
Dan Gohmanc475c362009-01-15 22:01:38 +0000396 });
Bill Wendling0f940c92007-12-07 21:42:31 +0000397
Evan Chengaf6949d2009-02-05 08:45:46 +0000398 // Look for opportunity to CSE the hoisted instruction.
399 std::pair<unsigned, unsigned> BBOpcPair =
400 std::make_pair(CurPreheader->getNumber(), MI.getOpcode());
401 DenseMap<std::pair<unsigned, unsigned>,
402 std::vector<const MachineInstr*> >::iterator CI = CSEMap.find(BBOpcPair);
403 bool DoneCSE = false;
404 if (CI != CSEMap.end()) {
Evan Chengefc78392009-02-27 00:02:22 +0000405 const MachineInstr *Dup = LookForDuplicate(&MI, CI->second, RegInfo);
Evan Chengaf6949d2009-02-05 08:45:46 +0000406 if (Dup) {
Bill Wendlingb7a89922009-08-22 20:25:44 +0000407 DEBUG(errs() << "CSEing " << MI << " with " << *Dup);
Evan Chengaf6949d2009-02-05 08:45:46 +0000408 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
409 const MachineOperand &MO = MI.getOperand(i);
410 if (MO.isReg() && MO.isDef())
411 RegInfo->replaceRegWith(MO.getReg(), Dup->getOperand(i).getReg());
412 }
413 MI.eraseFromParent();
414 DoneCSE = true;
415 ++NumCSEed;
416 }
417 }
418
419 // Otherwise, splice the instruction to the preheader.
420 if (!DoneCSE) {
421 CurPreheader->splice(CurPreheader->getFirstTerminator(),
422 MI.getParent(), &MI);
423 // Add to the CSE map.
424 if (CI != CSEMap.end())
425 CI->second.push_back(&MI);
426 else {
427 std::vector<const MachineInstr*> CSEMIs;
428 CSEMIs.push_back(&MI);
429 CSEMap.insert(std::make_pair(BBOpcPair, CSEMIs));
430 }
431 }
Bill Wendling0f940c92007-12-07 21:42:31 +0000432
Dan Gohmanc475c362009-01-15 22:01:38 +0000433 ++NumHoisted;
Bill Wendling0f940c92007-12-07 21:42:31 +0000434 Changed = true;
Bill Wendling0f940c92007-12-07 21:42:31 +0000435}