blob: 4a9ea72b05a24ff91e7f81ea1a4c5a0a8b3edd95 [file] [log] [blame]
Chris Lattnerc4ce73f2008-01-04 07:36:53 +00001//===-- MachineSink.cpp - Sinking for machine instructions ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Bill Wendling05c68372010-06-02 23:04:26 +000010// This pass moves instructions into successor blocks when possible, so that
Dan Gohmana5225ad2009-08-05 01:19:01 +000011// they aren't executed on paths where their results aren't needed.
12//
13// This pass is not intended to be a replacement or a complete alternative
14// for an LLVM-IR-level sinking pass. It is only designed to sink simple
15// constructs that are not exposed before lowering and instruction selection.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000016//
17//===----------------------------------------------------------------------===//
18
19#define DEBUG_TYPE "machine-sink"
20#include "llvm/CodeGen/Passes.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
22#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +000023#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohmana70dca12009-10-09 23:27:56 +000024#include "llvm/Analysis/AliasAnalysis.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000025#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000026#include "llvm/Target/TargetInstrInfo.h"
27#include "llvm/Target/TargetMachine.h"
Daniel Dunbard24c9d52010-06-23 00:48:25 +000028#include "llvm/ADT/SmallSet.h"
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000029#include "llvm/ADT/Statistic.h"
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000030#include "llvm/Support/Debug.h"
Bill Wendling1e973aa2009-08-22 20:26:23 +000031#include "llvm/Support/raw_ostream.h"
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000032using namespace llvm;
33
34STATISTIC(NumSunk, "Number of machine instructions sunk");
35
36namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000037 class MachineSinking : public MachineFunctionPass {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000038 const TargetInstrInfo *TII;
Dan Gohman19778e72009-09-25 22:53:29 +000039 const TargetRegisterInfo *TRI;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000040 MachineRegisterInfo *RegInfo; // Machine register information
Dan Gohmana5225ad2009-08-05 01:19:01 +000041 MachineDominatorTree *DT; // Machine dominator tree
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +000042 MachineLoopInfo *LI;
Dan Gohmana70dca12009-10-09 23:27:56 +000043 AliasAnalysis *AA;
Dan Gohman45094e32009-09-26 02:34:00 +000044 BitVector AllocatableSet; // Which physregs are allocatable?
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000045
46 public:
47 static char ID; // Pass identification
Dan Gohmanae73dc12008-09-04 17:05:41 +000048 MachineSinking() : MachineFunctionPass(&ID) {}
Jim Grosbach6ee358b2010-06-03 23:49:57 +000049
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000050 virtual bool runOnMachineFunction(MachineFunction &MF);
Jim Grosbach6ee358b2010-06-03 23:49:57 +000051
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000052 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000053 AU.setPreservesCFG();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000054 MachineFunctionPass::getAnalysisUsage(AU);
Dan Gohmana70dca12009-10-09 23:27:56 +000055 AU.addRequired<AliasAnalysis>();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000056 AU.addRequired<MachineDominatorTree>();
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +000057 AU.addRequired<MachineLoopInfo>();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000058 AU.addPreserved<MachineDominatorTree>();
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +000059 AU.addPreserved<MachineLoopInfo>();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000060 }
61 private:
62 bool ProcessBlock(MachineBasicBlock &MBB);
Chris Lattneraad193a2008-01-12 00:17:41 +000063 bool SinkInstruction(MachineInstr *MI, bool &SawStore);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000064 bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB) const;
Daniel Dunbard24c9d52010-06-23 00:48:25 +000065 bool LiveOutOfBasicBlock(const MachineInstr *MI, unsigned Reg) const;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000066 };
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000067} // end anonymous namespace
Jim Grosbach6ee358b2010-06-03 23:49:57 +000068
Dan Gohman844731a2008-05-13 00:00:25 +000069char MachineSinking::ID = 0;
70static RegisterPass<MachineSinking>
71X("machine-sink", "Machine code sinking");
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000072
73FunctionPass *llvm::createMachineSinkingPass() { return new MachineSinking(); }
74
75/// AllUsesDominatedByBlock - Return true if all uses of the specified register
76/// occur in blocks dominated by the specified block.
Jim Grosbach6ee358b2010-06-03 23:49:57 +000077bool MachineSinking::AllUsesDominatedByBlock(unsigned Reg,
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000078 MachineBasicBlock *MBB) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +000079 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
80 "Only makes sense for vregs");
Dale Johannesenb0812f12010-03-05 00:02:59 +000081 // Ignoring debug uses is necessary so debug info doesn't affect the code.
82 // This may leave a referencing dbg_value in the original block, before
83 // the definition of the vreg. Dwarf generator handles this although the
84 // user might not get the right info at runtime.
Bill Wendling05c68372010-06-02 23:04:26 +000085 for (MachineRegisterInfo::use_nodbg_iterator
86 I = RegInfo->use_nodbg_begin(Reg), E = RegInfo->use_nodbg_end();
87 I != E; ++I) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000088 // Determine the block of the use.
89 MachineInstr *UseInst = &*I;
90 MachineBasicBlock *UseBlock = UseInst->getParent();
Bill Wendling05c68372010-06-02 23:04:26 +000091
Chris Lattner518bb532010-02-09 19:54:29 +000092 if (UseInst->isPHI()) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000093 // PHI nodes use the operand in the predecessor block, not the block with
94 // the PHI.
95 UseBlock = UseInst->getOperand(I.getOperandNo()+1).getMBB();
96 }
Bill Wendling05c68372010-06-02 23:04:26 +000097
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000098 // Check that it dominates.
99 if (!DT->dominates(MBB, UseBlock))
100 return false;
101 }
Bill Wendling05c68372010-06-02 23:04:26 +0000102
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000103 return true;
104}
105
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000106bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
David Greenec19a9cd2010-01-05 01:26:00 +0000107 DEBUG(dbgs() << "******** Machine Sinking ********\n");
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000108
Dan Gohman4e9785e2009-10-19 14:52:05 +0000109 const TargetMachine &TM = MF.getTarget();
110 TII = TM.getInstrInfo();
111 TRI = TM.getRegisterInfo();
112 RegInfo = &MF.getRegInfo();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000113 DT = &getAnalysis<MachineDominatorTree>();
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +0000114 LI = &getAnalysis<MachineLoopInfo>();
Dan Gohmana70dca12009-10-09 23:27:56 +0000115 AA = &getAnalysis<AliasAnalysis>();
Dan Gohman4e9785e2009-10-19 14:52:05 +0000116 AllocatableSet = TRI->getAllocatableSet(MF);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000117
118 bool EverMadeChange = false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000119
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000120 while (1) {
121 bool MadeChange = false;
122
123 // Process all basic blocks.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000124 for (MachineFunction::iterator I = MF.begin(), E = MF.end();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000125 I != E; ++I)
126 MadeChange |= ProcessBlock(*I);
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000127
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000128 // If this iteration over the code changed anything, keep iterating.
129 if (!MadeChange) break;
130 EverMadeChange = true;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000131 }
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000132 return EverMadeChange;
133}
134
135bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000136 // Can't sink anything out of a block that has less than two successors.
Chris Lattner296185c2009-04-10 16:38:36 +0000137 if (MBB.succ_size() <= 1 || MBB.empty()) return false;
138
Dan Gohmanc4ae94d2010-04-05 19:17:22 +0000139 // Don't bother sinking code out of unreachable blocks. In addition to being
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000140 // unprofitable, it can also lead to infinite looping, because in an
141 // unreachable loop there may be nowhere to stop.
Dan Gohmanc4ae94d2010-04-05 19:17:22 +0000142 if (!DT->isReachableFromEntry(&MBB)) return false;
143
Chris Lattner296185c2009-04-10 16:38:36 +0000144 bool MadeChange = false;
145
Chris Lattneraad193a2008-01-12 00:17:41 +0000146 // Walk the basic block bottom-up. Remember if we saw a store.
Chris Lattner296185c2009-04-10 16:38:36 +0000147 MachineBasicBlock::iterator I = MBB.end();
148 --I;
149 bool ProcessedBegin, SawStore = false;
150 do {
151 MachineInstr *MI = I; // The instruction to sink.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000152
Chris Lattner296185c2009-04-10 16:38:36 +0000153 // Predecrement I (if it's not begin) so that it isn't invalidated by
154 // sinking.
155 ProcessedBegin = I == MBB.begin();
156 if (!ProcessedBegin)
157 --I;
Dale Johannesenb0812f12010-03-05 00:02:59 +0000158
159 if (MI->isDebugValue())
160 continue;
161
Chris Lattner296185c2009-04-10 16:38:36 +0000162 if (SinkInstruction(MI, SawStore))
163 ++NumSunk, MadeChange = true;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000164
Chris Lattner296185c2009-04-10 16:38:36 +0000165 // If we just processed the first instruction in the block, we're done.
166 } while (!ProcessedBegin);
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000167
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000168 return MadeChange;
169}
170
Daniel Dunbard24c9d52010-06-23 00:48:25 +0000171/// LiveOutOfBasicBlock - Determine if the physical register, defined and dead
172/// in MI, is live on exit from the basic block.
173bool MachineSinking::LiveOutOfBasicBlock(const MachineInstr *MI,
174 unsigned Reg) const {
175 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
176 "Only want to determine if a physical register is live out of a BB!");
177
178 const MachineBasicBlock *MBB = MI->getParent();
179 SmallSet<unsigned, 8> KilledRegs;
180 MachineBasicBlock::const_iterator I = MBB->end();
181 MachineBasicBlock::const_iterator E = MBB->begin();
182 assert(I != E && "How can there be an empty block at this point?!");
183
184 // Loop through the instructions bottom-up. If we see a kill of the preg
185 // first, then it's not live out of the BB. If we see a use or def first, then
186 // we assume that it is live out of the BB.
187 do {
188 const MachineInstr &CurMI = *--I;
189
190 for (unsigned i = 0, e = CurMI.getNumOperands(); i != e; ++i) {
191 const MachineOperand &MO = CurMI.getOperand(i);
192 if (!MO.isReg()) continue; // Ignore non-register operands.
193
194 unsigned MOReg = MO.getReg();
195 if (MOReg == 0) continue;
196
197 if (MOReg == Reg) {
198 if (MO.isKill())
199 return false;
200 if (MO.isUse() || MO.isDef())
201 return true;
202 }
203 }
204 } while (I != E);
205
206 return false;
207}
208
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000209/// SinkInstruction - Determine whether it is safe to sink the specified machine
210/// instruction out of its current block into a successor.
Chris Lattneraad193a2008-01-12 00:17:41 +0000211bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) {
Evan Chengb27087f2008-03-13 00:44:09 +0000212 // Check if it's safe to move the instruction.
Evan Chengac1abde2010-03-02 19:03:01 +0000213 if (!MI->isSafeToMove(TII, AA, SawStore))
Chris Lattneraad193a2008-01-12 00:17:41 +0000214 return false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000215
Chris Lattnere430e1c2008-01-05 06:47:58 +0000216 // FIXME: This should include support for sinking instructions within the
217 // block they are currently in to shorten the live ranges. We often get
218 // instructions sunk into the top of a large block, but it would be better to
219 // also sink them down before their first use in the block. This xform has to
220 // be careful not to *increase* register pressure though, e.g. sinking
221 // "x = y + z" down if it kills y and z would increase the live ranges of y
Dan Gohmana5225ad2009-08-05 01:19:01 +0000222 // and z and only shrink the live range of x.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000223
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000224 // Loop over all the operands of the specified instruction. If there is
225 // anything we can't handle, bail out.
226 MachineBasicBlock *ParentBlock = MI->getParent();
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000227
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000228 // SuccToSinkTo - This is the successor to sink this instruction to, once we
229 // decide.
230 MachineBasicBlock *SuccToSinkTo = 0;
Daniel Dunbard24c9d52010-06-23 00:48:25 +0000231 SmallVector<unsigned, 4> PhysRegs;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000232
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000233 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
234 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000235 if (!MO.isReg()) continue; // Ignore non-register operands.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000236
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000237 unsigned Reg = MO.getReg();
238 if (Reg == 0) continue;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000239
Dan Gohman6f0d0242008-02-10 18:45:23 +0000240 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Dan Gohman19778e72009-09-25 22:53:29 +0000241 if (MO.isUse()) {
242 // If the physreg has no defs anywhere, it's just an ambient register
Dan Gohman45094e32009-09-26 02:34:00 +0000243 // and we can freely move its uses. Alternatively, if it's allocatable,
244 // it could get allocated to something with a def during allocation.
Dan Gohman19778e72009-09-25 22:53:29 +0000245 if (!RegInfo->def_empty(Reg))
246 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000247
Dan Gohman45094e32009-09-26 02:34:00 +0000248 if (AllocatableSet.test(Reg))
249 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000250
Dan Gohman19778e72009-09-25 22:53:29 +0000251 // Check for a def among the register's aliases too.
Dan Gohman45094e32009-09-26 02:34:00 +0000252 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
253 unsigned AliasReg = *Alias;
254 if (!RegInfo->def_empty(AliasReg))
Dan Gohman19778e72009-09-25 22:53:29 +0000255 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000256
Dan Gohman45094e32009-09-26 02:34:00 +0000257 if (AllocatableSet.test(AliasReg))
258 return false;
259 }
Daniel Dunbard24c9d52010-06-23 00:48:25 +0000260 } else {
261 if (!MO.isDead())
262 // A def that isn't dead. We can't move it.
263 return false;
264 else
265 PhysRegs.push_back(Reg);
Dan Gohman19778e72009-09-25 22:53:29 +0000266 }
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000267 } else {
268 // Virtual register uses are always safe to sink.
269 if (MO.isUse()) continue;
Evan Chengb6f54172009-02-07 01:21:47 +0000270
271 // If it's not safe to move defs of the register class, then abort.
272 if (!TII->isSafeToMoveRegClassDefs(RegInfo->getRegClass(Reg)))
273 return false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000274
Chris Lattnere430e1c2008-01-05 06:47:58 +0000275 // FIXME: This picks a successor to sink into based on having one
276 // successor that dominates all the uses. However, there are cases where
277 // sinking can happen but where the sink point isn't a successor. For
278 // example:
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000279 //
Chris Lattnere430e1c2008-01-05 06:47:58 +0000280 // x = computation
281 // if () {} else {}
282 // use x
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000283 //
Bill Wendling05c68372010-06-02 23:04:26 +0000284 // the instruction could be sunk over the whole diamond for the
Chris Lattnere430e1c2008-01-05 06:47:58 +0000285 // if/then/else (or loop, etc), allowing it to be sunk into other blocks
286 // after that.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000287
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000288 // Virtual register defs can only be sunk if all their uses are in blocks
289 // dominated by one of the successors.
290 if (SuccToSinkTo) {
291 // If a previous operand picked a block to sink to, then this operand
292 // must be sinkable to the same block.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000293 if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo))
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000294 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000295
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000296 continue;
297 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000298
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000299 // Otherwise, we should look at all the successors and decide which one
300 // we should sink to.
301 for (MachineBasicBlock::succ_iterator SI = ParentBlock->succ_begin(),
302 E = ParentBlock->succ_end(); SI != E; ++SI) {
303 if (AllUsesDominatedByBlock(Reg, *SI)) {
304 SuccToSinkTo = *SI;
305 break;
306 }
307 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000308
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000309 // If we couldn't find a block to sink to, ignore this instruction.
310 if (SuccToSinkTo == 0)
311 return false;
312 }
313 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000314
Chris Lattner9bb459b2008-01-05 01:39:17 +0000315 // If there are no outputs, it must have side-effects.
316 if (SuccToSinkTo == 0)
317 return false;
Evan Chengb5999792009-02-15 08:36:12 +0000318
319 // It's not safe to sink instructions to EH landing pad. Control flow into
320 // landing pad is implicitly defined.
321 if (SuccToSinkTo->isLandingPad())
322 return false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000323
Dan Gohmandfffba62009-10-19 14:56:05 +0000324 // It is not possible to sink an instruction into its own block. This can
Chris Lattner296185c2009-04-10 16:38:36 +0000325 // happen with loops.
326 if (MI->getParent() == SuccToSinkTo)
327 return false;
Bill Wendling869d60d2010-06-03 07:54:20 +0000328
Daniel Dunbard24c9d52010-06-23 00:48:25 +0000329 // If the instruction to move defines a dead physical register which is live
330 // when leaving the basic block, don't move it because it could turn into a
331 // "zombie" define of that preg. E.g., EFLAGS. (<rdar://problem/8030636>)
332 for (SmallVectorImpl<unsigned>::const_iterator
333 I = PhysRegs.begin(), E = PhysRegs.end(); I != E; ++I)
334 if (LiveOutOfBasicBlock(MI, *I))
Bill Wendling869d60d2010-06-03 07:54:20 +0000335 return false;
336
Bill Wendling05c68372010-06-02 23:04:26 +0000337 DEBUG(dbgs() << "Sink instr " << *MI << "\tinto block " << *SuccToSinkTo);
338
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000339 // If the block has multiple predecessors, this would introduce computation on
340 // a path that it doesn't already exist. We could split the critical edge,
341 // but for now we just punt.
Chris Lattnere430e1c2008-01-05 06:47:58 +0000342 // FIXME: Split critical edges if not backedges.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000343 if (SuccToSinkTo->pred_size() > 1) {
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000344 // We cannot sink a load across a critical edge - there may be stores in
345 // other code paths.
346 bool store = true;
347 if (!MI->isSafeToMove(TII, AA, store)) {
348 DEBUG(dbgs() << " *** PUNTING: Wont sink load along critical edge.\n");
349 return false;
350 }
351
352 // We don't want to sink across a critical edge if we don't dominate the
353 // successor. We could be introducing calculations to new code paths.
354 if (!DT->dominates(ParentBlock, SuccToSinkTo)) {
355 DEBUG(dbgs() << " *** PUNTING: Critical edge found\n");
356 return false;
357 }
358
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +0000359 // Don't sink instructions into a loop.
360 if (LI->isLoopHeader(SuccToSinkTo)) {
361 DEBUG(dbgs() << " *** PUNTING: Loop header found\n");
362 return false;
363 }
364
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000365 // Otherwise we are OK with sinking along a critical edge.
366 DEBUG(dbgs() << "Sinking along critical edge.\n");
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000367 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000368
Bill Wendling05c68372010-06-02 23:04:26 +0000369 // Determine where to insert into. Skip phi nodes.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000370 MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
Chris Lattner518bb532010-02-09 19:54:29 +0000371 while (InsertPos != SuccToSinkTo->end() && InsertPos->isPHI())
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000372 ++InsertPos;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000373
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000374 // Move the instruction.
375 SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
376 ++MachineBasicBlock::iterator(MI));
Dan Gohmane6cd7572010-05-13 20:34:42 +0000377
Bill Wendling05c68372010-06-02 23:04:26 +0000378 // Conservatively, clear any kill flags, since it's possible that they are no
379 // longer correct.
Dan Gohmane6cd7572010-05-13 20:34:42 +0000380 MI->clearKillInfo();
381
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000382 return true;
383}