blob: f92ddb2b908ada6131b8d0c1d95bae784f5144bb [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) {
Bill Wendlingb7a89922009-08-22 20:25:44 +0000134 DEBUG(errs() << "******** Machine LICM ********\n");
Bill Wendlinga17ad592007-12-11 22:22:22 +0000135
Bill Wendling0f940c92007-12-07 21:42:31 +0000136 Changed = false;
Bill Wendlingacb04ec2008-08-31 02:30:23 +0000137 TM = &MF.getTarget();
Bill Wendling9258cd32008-01-02 19:32:43 +0000138 TII = TM->getInstrInfo();
Dan Gohmana8fb3362009-09-25 23:58:45 +0000139 TRI = TM->getRegisterInfo();
Bill Wendlingacb04ec2008-08-31 02:30:23 +0000140 RegInfo = &MF.getRegInfo();
Dan Gohman45094e32009-09-26 02:34:00 +0000141 AllocatableSet = TRI->getAllocatableSet(MF);
Bill Wendling0f940c92007-12-07 21:42:31 +0000142
143 // Get our Loop information...
144 LI = &getAnalysis<MachineLoopInfo>();
145 DT = &getAnalysis<MachineDominatorTree>();
Dan Gohmane33f44c2009-10-07 17:38:06 +0000146 AA = &getAnalysis<AliasAnalysis>();
Bill Wendling0f940c92007-12-07 21:42:31 +0000147
148 for (MachineLoopInfo::iterator
149 I = LI->begin(), E = LI->end(); I != E; ++I) {
Bill Wendlinga17ad592007-12-11 22:22:22 +0000150 CurLoop = *I;
Bill Wendling0f940c92007-12-07 21:42:31 +0000151
Dan Gohmanc475c362009-01-15 22:01:38 +0000152 // Only visit outer-most preheader-sporting loops.
153 if (!LoopIsOuterMostWithPreheader(CurLoop))
154 continue;
155
156 // Determine the block to which to hoist instructions. If we can't find a
157 // suitable loop preheader, we can't do any hoisting.
158 //
159 // FIXME: We are only hoisting if the basic block coming into this loop
160 // has only one successor. This isn't the case in general because we haven't
161 // broken critical edges or added preheaders.
162 CurPreheader = CurLoop->getLoopPreheader();
163 if (!CurPreheader)
164 continue;
165
166 HoistRegion(DT->getNode(CurLoop->getHeader()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000167 }
168
169 return Changed;
170}
171
Bill Wendling0f940c92007-12-07 21:42:31 +0000172/// HoistRegion - Walk the specified region of the CFG (defined by all blocks
173/// dominated by the specified block, and that are in the current loop) in depth
174/// first order w.r.t the DominatorTree. This allows us to visit definitions
175/// before uses, allowing us to hoist a loop body in one pass without iteration.
176///
177void MachineLICM::HoistRegion(MachineDomTreeNode *N) {
178 assert(N != 0 && "Null dominator tree node?");
179 MachineBasicBlock *BB = N->getBlock();
180
181 // If this subregion is not in the top level loop at all, exit.
182 if (!CurLoop->contains(BB)) return;
183
Dan Gohmanc475c362009-01-15 22:01:38 +0000184 for (MachineBasicBlock::iterator
Evan Chengaf6949d2009-02-05 08:45:46 +0000185 MII = BB->begin(), E = BB->end(); MII != E; ) {
186 MachineBasicBlock::iterator NextMII = MII; ++NextMII;
187 MachineInstr &MI = *MII;
Bill Wendling0f940c92007-12-07 21:42:31 +0000188
Dan Gohmanc475c362009-01-15 22:01:38 +0000189 Hoist(MI);
Evan Chengaf6949d2009-02-05 08:45:46 +0000190
191 MII = NextMII;
Dan Gohmanc475c362009-01-15 22:01:38 +0000192 }
Bill Wendling0f940c92007-12-07 21:42:31 +0000193
194 const std::vector<MachineDomTreeNode*> &Children = N->getChildren();
195
196 for (unsigned I = 0, E = Children.size(); I != E; ++I)
197 HoistRegion(Children[I]);
198}
199
Bill Wendling041b3f82007-12-08 23:58:46 +0000200/// IsLoopInvariantInst - Returns true if the instruction is loop
Bill Wendling0f940c92007-12-07 21:42:31 +0000201/// invariant. I.e., all virtual register operands are defined outside of the
Bill Wendling60ff1a32007-12-20 01:08:10 +0000202/// loop, physical registers aren't accessed explicitly, and there are no side
203/// effects that aren't captured by the operands or other flags.
Bill Wendling0f940c92007-12-07 21:42:31 +0000204///
Bill Wendling041b3f82007-12-08 23:58:46 +0000205bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) {
Chris Lattnera22edc82008-01-10 23:08:24 +0000206 const TargetInstrDesc &TID = I.getDesc();
207
208 // Ignore stuff that we obviously can't hoist.
Dan Gohman237dee12008-12-23 17:28:50 +0000209 if (TID.mayStore() || TID.isCall() || TID.isTerminator() ||
Chris Lattnera22edc82008-01-10 23:08:24 +0000210 TID.hasUnmodeledSideEffects())
211 return false;
Evan Cheng9b61f332009-02-04 07:17:49 +0000212
Chris Lattnera22edc82008-01-10 23:08:24 +0000213 if (TID.mayLoad()) {
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000214 // Okay, this instruction does a load. As a refinement, we allow the target
215 // to decide whether the loaded value is actually a constant. If so, we can
216 // actually use it as a load.
Dan Gohmane33f44c2009-10-07 17:38:06 +0000217 if (!I.isInvariantLoad(AA))
Chris Lattnera22edc82008-01-10 23:08:24 +0000218 // FIXME: we should be able to sink loads with no other side effects if
219 // there is nothing that can change memory from here until the end of
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000220 // block. This is a trivial form of alias analysis.
Chris Lattnera22edc82008-01-10 23:08:24 +0000221 return false;
Chris Lattnera22edc82008-01-10 23:08:24 +0000222 }
Bill Wendling074223a2008-03-10 08:13:01 +0000223
Bill Wendling280f4562007-12-18 21:38:04 +0000224 DEBUG({
Bill Wendlingb7a89922009-08-22 20:25:44 +0000225 errs() << "--- Checking if we can hoist " << I;
Chris Lattner749c6f62008-01-07 07:27:27 +0000226 if (I.getDesc().getImplicitUses()) {
Bill Wendlingb7a89922009-08-22 20:25:44 +0000227 errs() << " * Instruction has implicit uses:\n";
Bill Wendling280f4562007-12-18 21:38:04 +0000228
Dan Gohman6f0d0242008-02-10 18:45:23 +0000229 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
Chris Lattner749c6f62008-01-07 07:27:27 +0000230 for (const unsigned *ImpUses = I.getDesc().getImplicitUses();
Chris Lattner69244302008-01-07 01:56:04 +0000231 *ImpUses; ++ImpUses)
Bill Wendlingb7a89922009-08-22 20:25:44 +0000232 errs() << " -> " << TRI->getName(*ImpUses) << "\n";
Bill Wendling280f4562007-12-18 21:38:04 +0000233 }
234
Chris Lattner749c6f62008-01-07 07:27:27 +0000235 if (I.getDesc().getImplicitDefs()) {
Bill Wendlingb7a89922009-08-22 20:25:44 +0000236 errs() << " * Instruction has implicit defines:\n";
Bill Wendling280f4562007-12-18 21:38:04 +0000237
Dan Gohman6f0d0242008-02-10 18:45:23 +0000238 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
Chris Lattner749c6f62008-01-07 07:27:27 +0000239 for (const unsigned *ImpDefs = I.getDesc().getImplicitDefs();
Chris Lattner69244302008-01-07 01:56:04 +0000240 *ImpDefs; ++ImpDefs)
Bill Wendlingb7a89922009-08-22 20:25:44 +0000241 errs() << " -> " << TRI->getName(*ImpDefs) << "\n";
Bill Wendling280f4562007-12-18 21:38:04 +0000242 }
Bill Wendling280f4562007-12-18 21:38:04 +0000243 });
244
Bill Wendlingd3361e92008-08-18 00:33:49 +0000245 if (I.getDesc().getImplicitDefs() || I.getDesc().getImplicitUses()) {
Bill Wendlingb7a89922009-08-22 20:25:44 +0000246 DEBUG(errs() << "Cannot hoist with implicit defines or uses\n");
Bill Wendlingd3361e92008-08-18 00:33:49 +0000247 return false;
248 }
249
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000250 // The instruction is loop invariant if all of its operands are.
Bill Wendling0f940c92007-12-07 21:42:31 +0000251 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
252 const MachineOperand &MO = I.getOperand(i);
253
Dan Gohmand735b802008-10-03 15:45:36 +0000254 if (!MO.isReg())
Bill Wendlingfb018d02008-08-20 20:32:05 +0000255 continue;
256
Dan Gohmanc475c362009-01-15 22:01:38 +0000257 unsigned Reg = MO.getReg();
258 if (Reg == 0) continue;
259
260 // Don't hoist an instruction that uses or defines a physical register.
Dan Gohmana8fb3362009-09-25 23:58:45 +0000261 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Dan Gohmana8fb3362009-09-25 23:58:45 +0000262 if (MO.isUse()) {
263 // If the physreg has no defs anywhere, it's just an ambient register
Dan Gohman45094e32009-09-26 02:34:00 +0000264 // and we can freely move its uses. Alternatively, if it's allocatable,
265 // it could get allocated to something with a def during allocation.
Dan Gohmana8fb3362009-09-25 23:58:45 +0000266 if (!RegInfo->def_empty(Reg))
267 return false;
Dan Gohman45094e32009-09-26 02:34:00 +0000268 if (AllocatableSet.test(Reg))
269 return false;
Dan Gohmana8fb3362009-09-25 23:58:45 +0000270 // Check for a def among the register's aliases too.
Dan Gohman45094e32009-09-26 02:34:00 +0000271 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
272 unsigned AliasReg = *Alias;
273 if (!RegInfo->def_empty(AliasReg))
Dan Gohmana8fb3362009-09-25 23:58:45 +0000274 return false;
Dan Gohman45094e32009-09-26 02:34:00 +0000275 if (AllocatableSet.test(AliasReg))
276 return false;
277 }
Dan Gohmana8fb3362009-09-25 23:58:45 +0000278 // Otherwise it's safe to move.
279 continue;
280 } else if (!MO.isDead()) {
281 // A def that isn't dead. We can't move it.
282 return false;
283 }
284 }
Bill Wendlingfb018d02008-08-20 20:32:05 +0000285
286 if (!MO.isUse())
Bill Wendling0f940c92007-12-07 21:42:31 +0000287 continue;
288
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000289 assert(RegInfo->getVRegDef(Reg) &&
290 "Machine instr not mapped for this vreg?!");
Bill Wendling0f940c92007-12-07 21:42:31 +0000291
292 // If the loop contains the definition of an operand, then the instruction
293 // isn't loop invariant.
Bill Wendling9258cd32008-01-02 19:32:43 +0000294 if (CurLoop->contains(RegInfo->getVRegDef(Reg)->getParent()))
Bill Wendling0f940c92007-12-07 21:42:31 +0000295 return false;
296 }
297
298 // If we got this far, the instruction is loop invariant!
299 return true;
300}
301
Evan Chengaf6949d2009-02-05 08:45:46 +0000302
303/// HasPHIUses - Return true if the specified register has any PHI use.
304static bool HasPHIUses(unsigned Reg, MachineRegisterInfo *RegInfo) {
Evan Cheng45e94d62009-02-04 09:19:56 +0000305 for (MachineRegisterInfo::use_iterator UI = RegInfo->use_begin(Reg),
306 UE = RegInfo->use_end(); UI != UE; ++UI) {
307 MachineInstr *UseMI = &*UI;
Evan Chengaf6949d2009-02-05 08:45:46 +0000308 if (UseMI->getOpcode() == TargetInstrInfo::PHI)
309 return true;
Evan Cheng45e94d62009-02-04 09:19:56 +0000310 }
Evan Chengaf6949d2009-02-05 08:45:46 +0000311 return false;
Evan Cheng45e94d62009-02-04 09:19:56 +0000312}
313
314/// IsProfitableToHoist - Return true if it is potentially profitable to hoist
315/// the given loop invariant.
316bool MachineLICM::IsProfitableToHoist(MachineInstr &MI) {
Evan Chengefc78392009-02-27 00:02:22 +0000317 if (MI.getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
318 return false;
319
Evan Cheng45e94d62009-02-04 09:19:56 +0000320 // FIXME: For now, only hoist re-materilizable instructions. LICM will
321 // increase register pressure. We want to make sure it doesn't increase
322 // spilling.
Dan Gohmana70dca12009-10-09 23:27:56 +0000323 if (!TII->isTriviallyReMaterializable(&MI, AA))
Evan Cheng45e94d62009-02-04 09:19:56 +0000324 return false;
325
Evan Chengaf6949d2009-02-05 08:45:46 +0000326 // If result(s) of this instruction is used by PHIs, then don't hoist it.
327 // The presence of joins makes it difficult for current register allocator
328 // implementation to perform remat.
Evan Cheng45e94d62009-02-04 09:19:56 +0000329 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
330 const MachineOperand &MO = MI.getOperand(i);
331 if (!MO.isReg() || !MO.isDef())
332 continue;
Evan Chengaf6949d2009-02-05 08:45:46 +0000333 if (HasPHIUses(MO.getReg(), RegInfo))
334 return false;
Evan Cheng45e94d62009-02-04 09:19:56 +0000335 }
Evan Chengaf6949d2009-02-05 08:45:46 +0000336
337 return true;
338}
339
340static const MachineInstr *LookForDuplicate(const MachineInstr *MI,
Evan Chengefc78392009-02-27 00:02:22 +0000341 std::vector<const MachineInstr*> &PrevMIs,
342 MachineRegisterInfo *RegInfo) {
Evan Chengaf6949d2009-02-05 08:45:46 +0000343 unsigned NumOps = MI->getNumOperands();
344 for (unsigned i = 0, e = PrevMIs.size(); i != e; ++i) {
345 const MachineInstr *PrevMI = PrevMIs[i];
346 unsigned NumOps2 = PrevMI->getNumOperands();
347 if (NumOps != NumOps2)
348 continue;
349 bool IsSame = true;
350 for (unsigned j = 0; j != NumOps; ++j) {
351 const MachineOperand &MO = MI->getOperand(j);
Evan Chengefc78392009-02-27 00:02:22 +0000352 if (MO.isReg() && MO.isDef()) {
353 if (RegInfo->getRegClass(MO.getReg()) !=
354 RegInfo->getRegClass(PrevMI->getOperand(j).getReg())) {
355 IsSame = false;
356 break;
357 }
Evan Chengaf6949d2009-02-05 08:45:46 +0000358 continue;
Evan Chengefc78392009-02-27 00:02:22 +0000359 }
Evan Chengaf6949d2009-02-05 08:45:46 +0000360 if (!MO.isIdenticalTo(PrevMI->getOperand(j))) {
361 IsSame = false;
362 break;
363 }
364 }
365 if (IsSame)
366 return PrevMI;
367 }
368 return 0;
Evan Cheng45e94d62009-02-04 09:19:56 +0000369}
370
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000371/// Hoist - When an instruction is found to use only loop invariant operands
372/// that are safe to hoist, this instruction is called to do the dirty work.
Bill Wendling0f940c92007-12-07 21:42:31 +0000373///
Bill Wendlingb48519c2007-12-08 01:47:01 +0000374void MachineLICM::Hoist(MachineInstr &MI) {
Bill Wendling041b3f82007-12-08 23:58:46 +0000375 if (!IsLoopInvariantInst(MI)) return;
Evan Cheng45e94d62009-02-04 09:19:56 +0000376 if (!IsProfitableToHoist(MI)) return;
Bill Wendling0f940c92007-12-07 21:42:31 +0000377
Dan Gohmanc475c362009-01-15 22:01:38 +0000378 // Now move the instructions to the predecessor, inserting it before any
379 // terminator instructions.
380 DEBUG({
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000381 errs() << "Hoisting " << MI;
Dan Gohmanc475c362009-01-15 22:01:38 +0000382 if (CurPreheader->getBasicBlock())
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000383 errs() << " to MachineBasicBlock "
384 << CurPreheader->getBasicBlock()->getName();
Dan Gohmanc475c362009-01-15 22:01:38 +0000385 if (MI.getParent()->getBasicBlock())
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000386 errs() << " from MachineBasicBlock "
387 << MI.getParent()->getBasicBlock()->getName();
388 errs() << "\n";
Dan Gohmanc475c362009-01-15 22:01:38 +0000389 });
Bill Wendling0f940c92007-12-07 21:42:31 +0000390
Evan Chengaf6949d2009-02-05 08:45:46 +0000391 // Look for opportunity to CSE the hoisted instruction.
392 std::pair<unsigned, unsigned> BBOpcPair =
393 std::make_pair(CurPreheader->getNumber(), MI.getOpcode());
394 DenseMap<std::pair<unsigned, unsigned>,
395 std::vector<const MachineInstr*> >::iterator CI = CSEMap.find(BBOpcPair);
396 bool DoneCSE = false;
397 if (CI != CSEMap.end()) {
Evan Chengefc78392009-02-27 00:02:22 +0000398 const MachineInstr *Dup = LookForDuplicate(&MI, CI->second, RegInfo);
Evan Chengaf6949d2009-02-05 08:45:46 +0000399 if (Dup) {
Bill Wendlingb7a89922009-08-22 20:25:44 +0000400 DEBUG(errs() << "CSEing " << MI << " with " << *Dup);
Evan Chengaf6949d2009-02-05 08:45:46 +0000401 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
402 const MachineOperand &MO = MI.getOperand(i);
403 if (MO.isReg() && MO.isDef())
404 RegInfo->replaceRegWith(MO.getReg(), Dup->getOperand(i).getReg());
405 }
406 MI.eraseFromParent();
407 DoneCSE = true;
408 ++NumCSEed;
409 }
410 }
411
412 // Otherwise, splice the instruction to the preheader.
413 if (!DoneCSE) {
414 CurPreheader->splice(CurPreheader->getFirstTerminator(),
415 MI.getParent(), &MI);
416 // Add to the CSE map.
417 if (CI != CSEMap.end())
418 CI->second.push_back(&MI);
419 else {
420 std::vector<const MachineInstr*> CSEMIs;
421 CSEMIs.push_back(&MI);
422 CSEMap.insert(std::make_pair(BBOpcPair, CSEMIs));
423 }
424 }
Bill Wendling0f940c92007-12-07 21:42:31 +0000425
Dan Gohmanc475c362009-01-15 22:01:38 +0000426 ++NumHoisted;
Bill Wendling0f940c92007-12-07 21:42:31 +0000427 Changed = true;
Bill Wendling0f940c92007-12-07 21:42:31 +0000428}