blob: 8123d99680b91c4c1159e82ad97daab02bad30fe [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"
Evan Chengaf6949d2009-02-05 08:45:46 +000031#include "llvm/ADT/DenseMap.h"
Chris Lattnerac695822008-01-04 06:41:45 +000032#include "llvm/ADT/Statistic.h"
Chris Lattnerac695822008-01-04 06:41:45 +000033#include "llvm/Support/Compiler.h"
34#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 {
43 class VISIBILITY_HIDDEN 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;
Bill Wendling12ebf142007-12-11 19:40:06 +000047
Bill Wendling0f940c92007-12-07 21:42:31 +000048 // Various analyses that we use...
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +000049 MachineLoopInfo *LI; // Current MachineLoopInfo
50 MachineDominatorTree *DT; // Machine dominator tree for the cur loop
Bill Wendling9258cd32008-01-02 19:32:43 +000051 MachineRegisterInfo *RegInfo; // Machine register information
Bill Wendling0f940c92007-12-07 21:42:31 +000052
Bill Wendling0f940c92007-12-07 21:42:31 +000053 // State that is updated as we process loops
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +000054 bool Changed; // True if a loop is changed.
55 MachineLoop *CurLoop; // The current loop we are working on.
Dan Gohmanc475c362009-01-15 22:01:38 +000056 MachineBasicBlock *CurPreheader; // The preheader for CurLoop.
Evan Chengaf6949d2009-02-05 08:45:46 +000057
58 // For each BB and opcode pair, keep a list of hoisted instructions.
59 DenseMap<std::pair<unsigned, unsigned>,
60 std::vector<const MachineInstr*> > CSEMap;
Bill Wendling0f940c92007-12-07 21:42:31 +000061 public:
62 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000063 MachineLICM() : MachineFunctionPass(&ID) {}
Bill Wendling0f940c92007-12-07 21:42:31 +000064
65 virtual bool runOnMachineFunction(MachineFunction &MF);
66
Dan Gohman72241702008-12-18 01:37:56 +000067 const char *getPassName() const { return "Machine Instruction LICM"; }
68
Bill Wendling074223a2008-03-10 08:13:01 +000069 // FIXME: Loop preheaders?
Bill Wendling0f940c92007-12-07 21:42:31 +000070 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
71 AU.setPreservesCFG();
72 AU.addRequired<MachineLoopInfo>();
73 AU.addRequired<MachineDominatorTree>();
Bill Wendlingd5da7042008-01-04 08:48:49 +000074 AU.addPreserved<MachineLoopInfo>();
75 AU.addPreserved<MachineDominatorTree>();
76 MachineFunctionPass::getAnalysisUsage(AU);
Bill Wendling0f940c92007-12-07 21:42:31 +000077 }
Evan Chengaf6949d2009-02-05 08:45:46 +000078
79 virtual void releaseMemory() {
80 CSEMap.clear();
81 }
82
Bill Wendling0f940c92007-12-07 21:42:31 +000083 private:
Bill Wendling041b3f82007-12-08 23:58:46 +000084 /// IsLoopInvariantInst - Returns true if the instruction is loop
Bill Wendling0f940c92007-12-07 21:42:31 +000085 /// invariant. I.e., all virtual register operands are defined outside of
86 /// the loop, physical registers aren't accessed (explicitly or implicitly),
87 /// and the instruction is hoistable.
88 ///
Bill Wendling041b3f82007-12-08 23:58:46 +000089 bool IsLoopInvariantInst(MachineInstr &I);
Bill Wendling0f940c92007-12-07 21:42:31 +000090
Evan Cheng45e94d62009-02-04 09:19:56 +000091 /// IsProfitableToHoist - Return true if it is potentially profitable to
92 /// hoist the given loop invariant.
93 bool IsProfitableToHoist(MachineInstr &MI);
94
Bill Wendling0f940c92007-12-07 21:42:31 +000095 /// HoistRegion - Walk the specified region of the CFG (defined by all
96 /// blocks dominated by the specified block, and that are in the current
97 /// loop) in depth first order w.r.t the DominatorTree. This allows us to
98 /// visit definitions before uses, allowing us to hoist a loop body in one
99 /// pass without iteration.
100 ///
101 void HoistRegion(MachineDomTreeNode *N);
102
103 /// Hoist - When an instruction is found to only use loop invariant operands
104 /// that is safe to hoist, this instruction is called to do the dirty work.
105 ///
Bill Wendlingb48519c2007-12-08 01:47:01 +0000106 void Hoist(MachineInstr &MI);
Bill Wendling0f940c92007-12-07 21:42:31 +0000107 };
Bill Wendling0f940c92007-12-07 21:42:31 +0000108} // end anonymous namespace
109
Dan Gohman844731a2008-05-13 00:00:25 +0000110char MachineLICM::ID = 0;
111static RegisterPass<MachineLICM>
Bill Wendling8870ce92008-07-07 05:42:27 +0000112X("machinelicm", "Machine Loop Invariant Code Motion");
Dan Gohman844731a2008-05-13 00:00:25 +0000113
Bill Wendling0f940c92007-12-07 21:42:31 +0000114FunctionPass *llvm::createMachineLICMPass() { return new MachineLICM(); }
115
Dan Gohmanc475c362009-01-15 22:01:38 +0000116/// LoopIsOuterMostWithPreheader - Test if the given loop is the outer-most
117/// loop that has a preheader.
118static bool LoopIsOuterMostWithPreheader(MachineLoop *CurLoop) {
119 for (MachineLoop *L = CurLoop->getParentLoop(); L; L = L->getParentLoop())
120 if (L->getLoopPreheader())
121 return false;
122 return true;
123}
124
Bill Wendling0f940c92007-12-07 21:42:31 +0000125/// Hoist expressions out of the specified loop. Note, alias info for inner loop
126/// is not preserved so it is not a good idea to run LICM multiple times on one
127/// loop.
128///
129bool MachineLICM::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng740854b2009-02-05 08:51:13 +0000130 const Function *F = MF.getFunction();
131 if (F->hasFnAttr(Attribute::OptimizeForSize))
132 return false;
133
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();
Bill Wendling0f940c92007-12-07 21:42:31 +0000141
142 // Get our Loop information...
143 LI = &getAnalysis<MachineLoopInfo>();
144 DT = &getAnalysis<MachineDominatorTree>();
145
146 for (MachineLoopInfo::iterator
147 I = LI->begin(), E = LI->end(); I != E; ++I) {
Bill Wendlinga17ad592007-12-11 22:22:22 +0000148 CurLoop = *I;
Bill Wendling0f940c92007-12-07 21:42:31 +0000149
Dan Gohmanc475c362009-01-15 22:01:38 +0000150 // Only visit outer-most preheader-sporting loops.
151 if (!LoopIsOuterMostWithPreheader(CurLoop))
152 continue;
153
154 // Determine the block to which to hoist instructions. If we can't find a
155 // suitable loop preheader, we can't do any hoisting.
156 //
157 // FIXME: We are only hoisting if the basic block coming into this loop
158 // has only one successor. This isn't the case in general because we haven't
159 // broken critical edges or added preheaders.
160 CurPreheader = CurLoop->getLoopPreheader();
161 if (!CurPreheader)
162 continue;
163
164 HoistRegion(DT->getNode(CurLoop->getHeader()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000165 }
166
167 return Changed;
168}
169
Bill Wendling0f940c92007-12-07 21:42:31 +0000170/// HoistRegion - Walk the specified region of the CFG (defined by all blocks
171/// dominated by the specified block, and that are in the current loop) in depth
172/// first order w.r.t the DominatorTree. This allows us to visit definitions
173/// before uses, allowing us to hoist a loop body in one pass without iteration.
174///
175void MachineLICM::HoistRegion(MachineDomTreeNode *N) {
176 assert(N != 0 && "Null dominator tree node?");
177 MachineBasicBlock *BB = N->getBlock();
178
179 // If this subregion is not in the top level loop at all, exit.
180 if (!CurLoop->contains(BB)) return;
181
Dan Gohmanc475c362009-01-15 22:01:38 +0000182 for (MachineBasicBlock::iterator
Evan Chengaf6949d2009-02-05 08:45:46 +0000183 MII = BB->begin(), E = BB->end(); MII != E; ) {
184 MachineBasicBlock::iterator NextMII = MII; ++NextMII;
185 MachineInstr &MI = *MII;
Bill Wendling0f940c92007-12-07 21:42:31 +0000186
Dan Gohmanc475c362009-01-15 22:01:38 +0000187 Hoist(MI);
Evan Chengaf6949d2009-02-05 08:45:46 +0000188
189 MII = NextMII;
Dan Gohmanc475c362009-01-15 22:01:38 +0000190 }
Bill Wendling0f940c92007-12-07 21:42:31 +0000191
192 const std::vector<MachineDomTreeNode*> &Children = N->getChildren();
193
194 for (unsigned I = 0, E = Children.size(); I != E; ++I)
195 HoistRegion(Children[I]);
196}
197
Bill Wendling041b3f82007-12-08 23:58:46 +0000198/// IsLoopInvariantInst - Returns true if the instruction is loop
Bill Wendling0f940c92007-12-07 21:42:31 +0000199/// invariant. I.e., all virtual register operands are defined outside of the
Bill Wendling60ff1a32007-12-20 01:08:10 +0000200/// loop, physical registers aren't accessed explicitly, and there are no side
201/// effects that aren't captured by the operands or other flags.
Bill Wendling0f940c92007-12-07 21:42:31 +0000202///
Bill Wendling041b3f82007-12-08 23:58:46 +0000203bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) {
Chris Lattnera22edc82008-01-10 23:08:24 +0000204 const TargetInstrDesc &TID = I.getDesc();
205
206 // Ignore stuff that we obviously can't hoist.
Dan Gohman237dee12008-12-23 17:28:50 +0000207 if (TID.mayStore() || TID.isCall() || TID.isTerminator() ||
Chris Lattnera22edc82008-01-10 23:08:24 +0000208 TID.hasUnmodeledSideEffects())
209 return false;
Evan Cheng9b61f332009-02-04 07:17:49 +0000210
Chris Lattnera22edc82008-01-10 23:08:24 +0000211 if (TID.mayLoad()) {
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000212 // Okay, this instruction does a load. As a refinement, we allow the target
213 // to decide whether the loaded value is actually a constant. If so, we can
214 // actually use it as a load.
Evan Cheng45e94d62009-02-04 09:19:56 +0000215 if (!TII->isInvariantLoad(&I))
Chris Lattnera22edc82008-01-10 23:08:24 +0000216 // FIXME: we should be able to sink loads with no other side effects if
217 // there is nothing that can change memory from here until the end of
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000218 // block. This is a trivial form of alias analysis.
Chris Lattnera22edc82008-01-10 23:08:24 +0000219 return false;
Chris Lattnera22edc82008-01-10 23:08:24 +0000220 }
Bill Wendling074223a2008-03-10 08:13:01 +0000221
Bill Wendling280f4562007-12-18 21:38:04 +0000222 DEBUG({
Bill Wendlingb7a89922009-08-22 20:25:44 +0000223 errs() << "--- Checking if we can hoist " << I;
Chris Lattner749c6f62008-01-07 07:27:27 +0000224 if (I.getDesc().getImplicitUses()) {
Bill Wendlingb7a89922009-08-22 20:25:44 +0000225 errs() << " * Instruction has implicit uses:\n";
Bill Wendling280f4562007-12-18 21:38:04 +0000226
Dan Gohman6f0d0242008-02-10 18:45:23 +0000227 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
Chris Lattner749c6f62008-01-07 07:27:27 +0000228 for (const unsigned *ImpUses = I.getDesc().getImplicitUses();
Chris Lattner69244302008-01-07 01:56:04 +0000229 *ImpUses; ++ImpUses)
Bill Wendlingb7a89922009-08-22 20:25:44 +0000230 errs() << " -> " << TRI->getName(*ImpUses) << "\n";
Bill Wendling280f4562007-12-18 21:38:04 +0000231 }
232
Chris Lattner749c6f62008-01-07 07:27:27 +0000233 if (I.getDesc().getImplicitDefs()) {
Bill Wendlingb7a89922009-08-22 20:25:44 +0000234 errs() << " * Instruction has implicit defines:\n";
Bill Wendling280f4562007-12-18 21:38:04 +0000235
Dan Gohman6f0d0242008-02-10 18:45:23 +0000236 const TargetRegisterInfo *TRI = TM->getRegisterInfo();
Chris Lattner749c6f62008-01-07 07:27:27 +0000237 for (const unsigned *ImpDefs = I.getDesc().getImplicitDefs();
Chris Lattner69244302008-01-07 01:56:04 +0000238 *ImpDefs; ++ImpDefs)
Bill Wendlingb7a89922009-08-22 20:25:44 +0000239 errs() << " -> " << TRI->getName(*ImpDefs) << "\n";
Bill Wendling280f4562007-12-18 21:38:04 +0000240 }
Bill Wendling280f4562007-12-18 21:38:04 +0000241 });
242
Bill Wendlingd3361e92008-08-18 00:33:49 +0000243 if (I.getDesc().getImplicitDefs() || I.getDesc().getImplicitUses()) {
Bill Wendlingb7a89922009-08-22 20:25:44 +0000244 DEBUG(errs() << "Cannot hoist with implicit defines or uses\n");
Bill Wendlingd3361e92008-08-18 00:33:49 +0000245 return false;
246 }
247
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000248 // The instruction is loop invariant if all of its operands are.
Bill Wendling0f940c92007-12-07 21:42:31 +0000249 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
250 const MachineOperand &MO = I.getOperand(i);
251
Dan Gohmand735b802008-10-03 15:45:36 +0000252 if (!MO.isReg())
Bill Wendlingfb018d02008-08-20 20:32:05 +0000253 continue;
254
Dan Gohmanc475c362009-01-15 22:01:38 +0000255 unsigned Reg = MO.getReg();
256 if (Reg == 0) continue;
257
258 // Don't hoist an instruction that uses or defines a physical register.
Dan Gohmana8fb3362009-09-25 23:58:45 +0000259 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
260 // If this is a physical register use, we can't move it. If it is a def,
261 // we can move it, but only if the def is dead.
262 if (MO.isUse()) {
263 // If the physreg has no defs anywhere, it's just an ambient register
264 // and we can freely move its uses.
265 if (!RegInfo->def_empty(Reg))
266 return false;
267 // Check for a def among the register's aliases too.
268 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias)
269 if (!RegInfo->def_empty(*Alias))
270 return false;
271 // Otherwise it's safe to move.
272 continue;
273 } else if (!MO.isDead()) {
274 // A def that isn't dead. We can't move it.
275 return false;
276 }
277 }
Bill Wendlingfb018d02008-08-20 20:32:05 +0000278
279 if (!MO.isUse())
Bill Wendling0f940c92007-12-07 21:42:31 +0000280 continue;
281
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000282 assert(RegInfo->getVRegDef(Reg) &&
283 "Machine instr not mapped for this vreg?!");
Bill Wendling0f940c92007-12-07 21:42:31 +0000284
285 // If the loop contains the definition of an operand, then the instruction
286 // isn't loop invariant.
Bill Wendling9258cd32008-01-02 19:32:43 +0000287 if (CurLoop->contains(RegInfo->getVRegDef(Reg)->getParent()))
Bill Wendling0f940c92007-12-07 21:42:31 +0000288 return false;
289 }
290
291 // If we got this far, the instruction is loop invariant!
292 return true;
293}
294
Evan Chengaf6949d2009-02-05 08:45:46 +0000295
296/// HasPHIUses - Return true if the specified register has any PHI use.
297static bool HasPHIUses(unsigned Reg, MachineRegisterInfo *RegInfo) {
Evan Cheng45e94d62009-02-04 09:19:56 +0000298 for (MachineRegisterInfo::use_iterator UI = RegInfo->use_begin(Reg),
299 UE = RegInfo->use_end(); UI != UE; ++UI) {
300 MachineInstr *UseMI = &*UI;
Evan Chengaf6949d2009-02-05 08:45:46 +0000301 if (UseMI->getOpcode() == TargetInstrInfo::PHI)
302 return true;
Evan Cheng45e94d62009-02-04 09:19:56 +0000303 }
Evan Chengaf6949d2009-02-05 08:45:46 +0000304 return false;
Evan Cheng45e94d62009-02-04 09:19:56 +0000305}
306
307/// IsProfitableToHoist - Return true if it is potentially profitable to hoist
308/// the given loop invariant.
309bool MachineLICM::IsProfitableToHoist(MachineInstr &MI) {
Evan Chengefc78392009-02-27 00:02:22 +0000310 if (MI.getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
311 return false;
312
Evan Cheng45e94d62009-02-04 09:19:56 +0000313 const TargetInstrDesc &TID = MI.getDesc();
314
Evan Cheng45e94d62009-02-04 09:19:56 +0000315 // FIXME: For now, only hoist re-materilizable instructions. LICM will
316 // increase register pressure. We want to make sure it doesn't increase
317 // spilling.
Evan Cheng5caa8832009-02-04 09:21:58 +0000318 if (!TID.mayLoad() && (!TID.isRematerializable() ||
319 !TII->isTriviallyReMaterializable(&MI)))
Evan Cheng45e94d62009-02-04 09:19:56 +0000320 return false;
321
Evan Chengaf6949d2009-02-05 08:45:46 +0000322 // If result(s) of this instruction is used by PHIs, then don't hoist it.
323 // The presence of joins makes it difficult for current register allocator
324 // implementation to perform remat.
Evan Cheng45e94d62009-02-04 09:19:56 +0000325 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
326 const MachineOperand &MO = MI.getOperand(i);
327 if (!MO.isReg() || !MO.isDef())
328 continue;
Evan Chengaf6949d2009-02-05 08:45:46 +0000329 if (HasPHIUses(MO.getReg(), RegInfo))
330 return false;
Evan Cheng45e94d62009-02-04 09:19:56 +0000331 }
Evan Chengaf6949d2009-02-05 08:45:46 +0000332
333 return true;
334}
335
336static const MachineInstr *LookForDuplicate(const MachineInstr *MI,
Evan Chengefc78392009-02-27 00:02:22 +0000337 std::vector<const MachineInstr*> &PrevMIs,
338 MachineRegisterInfo *RegInfo) {
Evan Chengaf6949d2009-02-05 08:45:46 +0000339 unsigned NumOps = MI->getNumOperands();
340 for (unsigned i = 0, e = PrevMIs.size(); i != e; ++i) {
341 const MachineInstr *PrevMI = PrevMIs[i];
342 unsigned NumOps2 = PrevMI->getNumOperands();
343 if (NumOps != NumOps2)
344 continue;
345 bool IsSame = true;
346 for (unsigned j = 0; j != NumOps; ++j) {
347 const MachineOperand &MO = MI->getOperand(j);
Evan Chengefc78392009-02-27 00:02:22 +0000348 if (MO.isReg() && MO.isDef()) {
349 if (RegInfo->getRegClass(MO.getReg()) !=
350 RegInfo->getRegClass(PrevMI->getOperand(j).getReg())) {
351 IsSame = false;
352 break;
353 }
Evan Chengaf6949d2009-02-05 08:45:46 +0000354 continue;
Evan Chengefc78392009-02-27 00:02:22 +0000355 }
Evan Chengaf6949d2009-02-05 08:45:46 +0000356 if (!MO.isIdenticalTo(PrevMI->getOperand(j))) {
357 IsSame = false;
358 break;
359 }
360 }
361 if (IsSame)
362 return PrevMI;
363 }
364 return 0;
Evan Cheng45e94d62009-02-04 09:19:56 +0000365}
366
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000367/// Hoist - When an instruction is found to use only loop invariant operands
368/// that are safe to hoist, this instruction is called to do the dirty work.
Bill Wendling0f940c92007-12-07 21:42:31 +0000369///
Bill Wendlingb48519c2007-12-08 01:47:01 +0000370void MachineLICM::Hoist(MachineInstr &MI) {
Bill Wendling041b3f82007-12-08 23:58:46 +0000371 if (!IsLoopInvariantInst(MI)) return;
Evan Cheng45e94d62009-02-04 09:19:56 +0000372 if (!IsProfitableToHoist(MI)) return;
Bill Wendling0f940c92007-12-07 21:42:31 +0000373
Dan Gohmanc475c362009-01-15 22:01:38 +0000374 // Now move the instructions to the predecessor, inserting it before any
375 // terminator instructions.
376 DEBUG({
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000377 errs() << "Hoisting " << MI;
Dan Gohmanc475c362009-01-15 22:01:38 +0000378 if (CurPreheader->getBasicBlock())
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000379 errs() << " to MachineBasicBlock "
380 << CurPreheader->getBasicBlock()->getName();
Dan Gohmanc475c362009-01-15 22:01:38 +0000381 if (MI.getParent()->getBasicBlock())
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000382 errs() << " from MachineBasicBlock "
383 << MI.getParent()->getBasicBlock()->getName();
384 errs() << "\n";
Dan Gohmanc475c362009-01-15 22:01:38 +0000385 });
Bill Wendling0f940c92007-12-07 21:42:31 +0000386
Evan Chengaf6949d2009-02-05 08:45:46 +0000387 // Look for opportunity to CSE the hoisted instruction.
388 std::pair<unsigned, unsigned> BBOpcPair =
389 std::make_pair(CurPreheader->getNumber(), MI.getOpcode());
390 DenseMap<std::pair<unsigned, unsigned>,
391 std::vector<const MachineInstr*> >::iterator CI = CSEMap.find(BBOpcPair);
392 bool DoneCSE = false;
393 if (CI != CSEMap.end()) {
Evan Chengefc78392009-02-27 00:02:22 +0000394 const MachineInstr *Dup = LookForDuplicate(&MI, CI->second, RegInfo);
Evan Chengaf6949d2009-02-05 08:45:46 +0000395 if (Dup) {
Bill Wendlingb7a89922009-08-22 20:25:44 +0000396 DEBUG(errs() << "CSEing " << MI << " with " << *Dup);
Evan Chengaf6949d2009-02-05 08:45:46 +0000397 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
398 const MachineOperand &MO = MI.getOperand(i);
399 if (MO.isReg() && MO.isDef())
400 RegInfo->replaceRegWith(MO.getReg(), Dup->getOperand(i).getReg());
401 }
402 MI.eraseFromParent();
403 DoneCSE = true;
404 ++NumCSEed;
405 }
406 }
407
408 // Otherwise, splice the instruction to the preheader.
409 if (!DoneCSE) {
410 CurPreheader->splice(CurPreheader->getFirstTerminator(),
411 MI.getParent(), &MI);
412 // Add to the CSE map.
413 if (CI != CSEMap.end())
414 CI->second.push_back(&MI);
415 else {
416 std::vector<const MachineInstr*> CSEMIs;
417 CSEMIs.push_back(&MI);
418 CSEMap.insert(std::make_pair(BBOpcPair, CSEMIs));
419 }
420 }
Bill Wendling0f940c92007-12-07 21:42:31 +0000421
Dan Gohmanc475c362009-01-15 22:01:38 +0000422 ++NumHoisted;
Bill Wendling0f940c92007-12-07 21:42:31 +0000423 Changed = true;
Bill Wendling0f940c92007-12-07 21:42:31 +0000424}