blob: f1daf85fb84c4ff2504f49b68c752cfb208d39c4 [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"
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000028#include "llvm/ADT/Statistic.h"
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000029#include "llvm/Support/Debug.h"
Bill Wendling1e973aa2009-08-22 20:26:23 +000030#include "llvm/Support/raw_ostream.h"
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000031using namespace llvm;
32
33STATISTIC(NumSunk, "Number of machine instructions sunk");
34
35namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000036 class MachineSinking : public MachineFunctionPass {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000037 const TargetInstrInfo *TII;
Dan Gohman19778e72009-09-25 22:53:29 +000038 const TargetRegisterInfo *TRI;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000039 MachineRegisterInfo *RegInfo; // Machine register information
Dan Gohmana5225ad2009-08-05 01:19:01 +000040 MachineDominatorTree *DT; // Machine dominator tree
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +000041 MachineLoopInfo *LI;
Dan Gohmana70dca12009-10-09 23:27:56 +000042 AliasAnalysis *AA;
Dan Gohman45094e32009-09-26 02:34:00 +000043 BitVector AllocatableSet; // Which physregs are allocatable?
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000044
45 public:
46 static char ID; // Pass identification
Owen Anderson90c579d2010-08-06 18:33:48 +000047 MachineSinking() : MachineFunctionPass(ID) {}
Jim Grosbach6ee358b2010-06-03 23:49:57 +000048
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000049 virtual bool runOnMachineFunction(MachineFunction &MF);
Jim Grosbach6ee358b2010-06-03 23:49:57 +000050
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000051 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000052 AU.setPreservesCFG();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000053 MachineFunctionPass::getAnalysisUsage(AU);
Dan Gohmana70dca12009-10-09 23:27:56 +000054 AU.addRequired<AliasAnalysis>();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000055 AU.addRequired<MachineDominatorTree>();
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +000056 AU.addRequired<MachineLoopInfo>();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000057 AU.addPreserved<MachineDominatorTree>();
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +000058 AU.addPreserved<MachineLoopInfo>();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000059 }
60 private:
61 bool ProcessBlock(MachineBasicBlock &MBB);
Chris Lattneraad193a2008-01-12 00:17:41 +000062 bool SinkInstruction(MachineInstr *MI, bool &SawStore);
Evan Chengc3439ad2010-08-18 23:09:25 +000063 bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB,
64 MachineBasicBlock *DefMBB, bool &LocalUse) const;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000065 };
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000066} // end anonymous namespace
Jim Grosbach6ee358b2010-06-03 23:49:57 +000067
Dan Gohman844731a2008-05-13 00:00:25 +000068char MachineSinking::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000069INITIALIZE_PASS(MachineSinking, "machine-sink",
70 "Machine code sinking", false, false);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000071
72FunctionPass *llvm::createMachineSinkingPass() { return new MachineSinking(); }
73
74/// AllUsesDominatedByBlock - Return true if all uses of the specified register
Evan Chengc3439ad2010-08-18 23:09:25 +000075/// occur in blocks dominated by the specified block. If any use is in the
76/// definition block, then return false since it is never legal to move def
77/// after uses.
Jim Grosbach6ee358b2010-06-03 23:49:57 +000078bool MachineSinking::AllUsesDominatedByBlock(unsigned Reg,
Evan Chengc3439ad2010-08-18 23:09:25 +000079 MachineBasicBlock *MBB,
80 MachineBasicBlock *DefMBB,
81 bool &LocalUse) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +000082 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
83 "Only makes sense for vregs");
Dale Johannesenb0812f12010-03-05 00:02:59 +000084 // Ignoring debug uses is necessary so debug info doesn't affect the code.
85 // This may leave a referencing dbg_value in the original block, before
86 // the definition of the vreg. Dwarf generator handles this although the
87 // user might not get the right info at runtime.
Bill Wendling05c68372010-06-02 23:04:26 +000088 for (MachineRegisterInfo::use_nodbg_iterator
89 I = RegInfo->use_nodbg_begin(Reg), E = RegInfo->use_nodbg_end();
90 I != E; ++I) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000091 // Determine the block of the use.
92 MachineInstr *UseInst = &*I;
93 MachineBasicBlock *UseBlock = UseInst->getParent();
Evan Chengc3439ad2010-08-18 23:09:25 +000094 if (UseBlock == DefMBB) {
95 LocalUse = true;
96 return false;
97 }
Bill Wendling05c68372010-06-02 23:04:26 +000098
Chris Lattner518bb532010-02-09 19:54:29 +000099 if (UseInst->isPHI()) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000100 // PHI nodes use the operand in the predecessor block, not the block with
101 // the PHI.
102 UseBlock = UseInst->getOperand(I.getOperandNo()+1).getMBB();
103 }
Bill Wendling05c68372010-06-02 23:04:26 +0000104
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000105 // Check that it dominates.
106 if (!DT->dominates(MBB, UseBlock))
107 return false;
108 }
Bill Wendling05c68372010-06-02 23:04:26 +0000109
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000110 return true;
111}
112
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000113bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
David Greenec19a9cd2010-01-05 01:26:00 +0000114 DEBUG(dbgs() << "******** Machine Sinking ********\n");
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000115
Dan Gohman4e9785e2009-10-19 14:52:05 +0000116 const TargetMachine &TM = MF.getTarget();
117 TII = TM.getInstrInfo();
118 TRI = TM.getRegisterInfo();
119 RegInfo = &MF.getRegInfo();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000120 DT = &getAnalysis<MachineDominatorTree>();
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +0000121 LI = &getAnalysis<MachineLoopInfo>();
Dan Gohmana70dca12009-10-09 23:27:56 +0000122 AA = &getAnalysis<AliasAnalysis>();
Dan Gohman4e9785e2009-10-19 14:52:05 +0000123 AllocatableSet = TRI->getAllocatableSet(MF);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000124
125 bool EverMadeChange = false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000126
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000127 while (1) {
128 bool MadeChange = false;
129
130 // Process all basic blocks.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000131 for (MachineFunction::iterator I = MF.begin(), E = MF.end();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000132 I != E; ++I)
133 MadeChange |= ProcessBlock(*I);
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000134
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000135 // If this iteration over the code changed anything, keep iterating.
136 if (!MadeChange) break;
137 EverMadeChange = true;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000138 }
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000139 return EverMadeChange;
140}
141
142bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000143 // Can't sink anything out of a block that has less than two successors.
Chris Lattner296185c2009-04-10 16:38:36 +0000144 if (MBB.succ_size() <= 1 || MBB.empty()) return false;
145
Dan Gohmanc4ae94d2010-04-05 19:17:22 +0000146 // Don't bother sinking code out of unreachable blocks. In addition to being
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000147 // unprofitable, it can also lead to infinite looping, because in an
148 // unreachable loop there may be nowhere to stop.
Dan Gohmanc4ae94d2010-04-05 19:17:22 +0000149 if (!DT->isReachableFromEntry(&MBB)) return false;
150
Chris Lattner296185c2009-04-10 16:38:36 +0000151 bool MadeChange = false;
152
Chris Lattneraad193a2008-01-12 00:17:41 +0000153 // Walk the basic block bottom-up. Remember if we saw a store.
Chris Lattner296185c2009-04-10 16:38:36 +0000154 MachineBasicBlock::iterator I = MBB.end();
155 --I;
156 bool ProcessedBegin, SawStore = false;
157 do {
158 MachineInstr *MI = I; // The instruction to sink.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000159
Chris Lattner296185c2009-04-10 16:38:36 +0000160 // Predecrement I (if it's not begin) so that it isn't invalidated by
161 // sinking.
162 ProcessedBegin = I == MBB.begin();
163 if (!ProcessedBegin)
164 --I;
Dale Johannesenb0812f12010-03-05 00:02:59 +0000165
166 if (MI->isDebugValue())
167 continue;
168
Chris Lattner296185c2009-04-10 16:38:36 +0000169 if (SinkInstruction(MI, SawStore))
170 ++NumSunk, MadeChange = true;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000171
Chris Lattner296185c2009-04-10 16:38:36 +0000172 // If we just processed the first instruction in the block, we're done.
173 } while (!ProcessedBegin);
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000174
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000175 return MadeChange;
176}
177
178/// SinkInstruction - Determine whether it is safe to sink the specified machine
179/// instruction out of its current block into a successor.
Chris Lattneraad193a2008-01-12 00:17:41 +0000180bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) {
Evan Chengb27087f2008-03-13 00:44:09 +0000181 // Check if it's safe to move the instruction.
Evan Chengac1abde2010-03-02 19:03:01 +0000182 if (!MI->isSafeToMove(TII, AA, SawStore))
Chris Lattneraad193a2008-01-12 00:17:41 +0000183 return false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000184
Chris Lattnere430e1c2008-01-05 06:47:58 +0000185 // FIXME: This should include support for sinking instructions within the
186 // block they are currently in to shorten the live ranges. We often get
187 // instructions sunk into the top of a large block, but it would be better to
188 // also sink them down before their first use in the block. This xform has to
189 // be careful not to *increase* register pressure though, e.g. sinking
190 // "x = y + z" down if it kills y and z would increase the live ranges of y
Dan Gohmana5225ad2009-08-05 01:19:01 +0000191 // and z and only shrink the live range of x.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000192
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000193 // Loop over all the operands of the specified instruction. If there is
194 // anything we can't handle, bail out.
195 MachineBasicBlock *ParentBlock = MI->getParent();
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000196
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000197 // SuccToSinkTo - This is the successor to sink this instruction to, once we
198 // decide.
199 MachineBasicBlock *SuccToSinkTo = 0;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000200
Evan Chengc3439ad2010-08-18 23:09:25 +0000201 bool LocalUse = false;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000202 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
203 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000204 if (!MO.isReg()) continue; // Ignore non-register operands.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000205
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000206 unsigned Reg = MO.getReg();
207 if (Reg == 0) continue;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000208
Dan Gohman6f0d0242008-02-10 18:45:23 +0000209 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Dan Gohman19778e72009-09-25 22:53:29 +0000210 if (MO.isUse()) {
211 // If the physreg has no defs anywhere, it's just an ambient register
Dan Gohman45094e32009-09-26 02:34:00 +0000212 // and we can freely move its uses. Alternatively, if it's allocatable,
213 // it could get allocated to something with a def during allocation.
Dan Gohman19778e72009-09-25 22:53:29 +0000214 if (!RegInfo->def_empty(Reg))
215 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000216
Dan Gohman45094e32009-09-26 02:34:00 +0000217 if (AllocatableSet.test(Reg))
218 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000219
Dan Gohman19778e72009-09-25 22:53:29 +0000220 // Check for a def among the register's aliases too.
Dan Gohman45094e32009-09-26 02:34:00 +0000221 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
222 unsigned AliasReg = *Alias;
223 if (!RegInfo->def_empty(AliasReg))
Dan Gohman19778e72009-09-25 22:53:29 +0000224 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000225
Dan Gohman45094e32009-09-26 02:34:00 +0000226 if (AllocatableSet.test(AliasReg))
227 return false;
228 }
Bill Wendling730c07e2010-06-25 20:48:10 +0000229 } else if (!MO.isDead()) {
230 // A def that isn't dead. We can't move it.
231 return false;
Dan Gohman19778e72009-09-25 22:53:29 +0000232 }
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000233 } else {
234 // Virtual register uses are always safe to sink.
235 if (MO.isUse()) continue;
Evan Chengb6f54172009-02-07 01:21:47 +0000236
237 // If it's not safe to move defs of the register class, then abort.
238 if (!TII->isSafeToMoveRegClassDefs(RegInfo->getRegClass(Reg)))
239 return false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000240
Chris Lattnere430e1c2008-01-05 06:47:58 +0000241 // FIXME: This picks a successor to sink into based on having one
242 // successor that dominates all the uses. However, there are cases where
243 // sinking can happen but where the sink point isn't a successor. For
244 // example:
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000245 //
Chris Lattnere430e1c2008-01-05 06:47:58 +0000246 // x = computation
247 // if () {} else {}
248 // use x
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000249 //
Bill Wendling05c68372010-06-02 23:04:26 +0000250 // the instruction could be sunk over the whole diamond for the
Chris Lattnere430e1c2008-01-05 06:47:58 +0000251 // if/then/else (or loop, etc), allowing it to be sunk into other blocks
252 // after that.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000253
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000254 // Virtual register defs can only be sunk if all their uses are in blocks
255 // dominated by one of the successors.
256 if (SuccToSinkTo) {
257 // If a previous operand picked a block to sink to, then this operand
258 // must be sinkable to the same block.
Evan Chengc3439ad2010-08-18 23:09:25 +0000259 if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, ParentBlock, LocalUse))
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000260 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000261
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000262 continue;
263 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000264
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000265 // Otherwise, we should look at all the successors and decide which one
266 // we should sink to.
267 for (MachineBasicBlock::succ_iterator SI = ParentBlock->succ_begin(),
268 E = ParentBlock->succ_end(); SI != E; ++SI) {
Evan Chengc3439ad2010-08-18 23:09:25 +0000269 if (AllUsesDominatedByBlock(Reg, *SI, ParentBlock, LocalUse)) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000270 SuccToSinkTo = *SI;
271 break;
272 }
Evan Chengc3439ad2010-08-18 23:09:25 +0000273 if (LocalUse)
274 // Def is used locally, it's never safe to move this def.
275 return false;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000276 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000277
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000278 // If we couldn't find a block to sink to, ignore this instruction.
279 if (SuccToSinkTo == 0)
280 return false;
281 }
282 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000283
Chris Lattner9bb459b2008-01-05 01:39:17 +0000284 // If there are no outputs, it must have side-effects.
285 if (SuccToSinkTo == 0)
286 return false;
Evan Chengb5999792009-02-15 08:36:12 +0000287
288 // It's not safe to sink instructions to EH landing pad. Control flow into
289 // landing pad is implicitly defined.
290 if (SuccToSinkTo->isLandingPad())
291 return false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000292
Dan Gohmandfffba62009-10-19 14:56:05 +0000293 // It is not possible to sink an instruction into its own block. This can
Chris Lattner296185c2009-04-10 16:38:36 +0000294 // happen with loops.
295 if (MI->getParent() == SuccToSinkTo)
296 return false;
Bill Wendling869d60d2010-06-03 07:54:20 +0000297
Daniel Dunbard24c9d52010-06-23 00:48:25 +0000298 // If the instruction to move defines a dead physical register which is live
299 // when leaving the basic block, don't move it because it could turn into a
300 // "zombie" define of that preg. E.g., EFLAGS. (<rdar://problem/8030636>)
Bill Wendling730c07e2010-06-25 20:48:10 +0000301 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
302 const MachineOperand &MO = MI->getOperand(I);
303 if (!MO.isReg()) continue;
304 unsigned Reg = MO.getReg();
305 if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
306 if (SuccToSinkTo->isLiveIn(Reg))
Bill Wendling869d60d2010-06-03 07:54:20 +0000307 return false;
Bill Wendling730c07e2010-06-25 20:48:10 +0000308 }
Bill Wendling869d60d2010-06-03 07:54:20 +0000309
Bill Wendling05c68372010-06-02 23:04:26 +0000310 DEBUG(dbgs() << "Sink instr " << *MI << "\tinto block " << *SuccToSinkTo);
311
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000312 // If the block has multiple predecessors, this would introduce computation on
313 // a path that it doesn't already exist. We could split the critical edge,
314 // but for now we just punt.
Chris Lattnere430e1c2008-01-05 06:47:58 +0000315 // FIXME: Split critical edges if not backedges.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000316 if (SuccToSinkTo->pred_size() > 1) {
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000317 // We cannot sink a load across a critical edge - there may be stores in
318 // other code paths.
319 bool store = true;
320 if (!MI->isSafeToMove(TII, AA, store)) {
321 DEBUG(dbgs() << " *** PUNTING: Wont sink load along critical edge.\n");
322 return false;
323 }
324
325 // We don't want to sink across a critical edge if we don't dominate the
326 // successor. We could be introducing calculations to new code paths.
327 if (!DT->dominates(ParentBlock, SuccToSinkTo)) {
328 DEBUG(dbgs() << " *** PUNTING: Critical edge found\n");
329 return false;
330 }
331
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +0000332 // Don't sink instructions into a loop.
333 if (LI->isLoopHeader(SuccToSinkTo)) {
334 DEBUG(dbgs() << " *** PUNTING: Loop header found\n");
335 return false;
336 }
337
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000338 // Otherwise we are OK with sinking along a critical edge.
339 DEBUG(dbgs() << "Sinking along critical edge.\n");
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000340 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000341
Bill Wendling05c68372010-06-02 23:04:26 +0000342 // Determine where to insert into. Skip phi nodes.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000343 MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
Chris Lattner518bb532010-02-09 19:54:29 +0000344 while (InsertPos != SuccToSinkTo->end() && InsertPos->isPHI())
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000345 ++InsertPos;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000346
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000347 // Move the instruction.
348 SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
349 ++MachineBasicBlock::iterator(MI));
Dan Gohmane6cd7572010-05-13 20:34:42 +0000350
Bill Wendling05c68372010-06-02 23:04:26 +0000351 // Conservatively, clear any kill flags, since it's possible that they are no
352 // longer correct.
Dan Gohmane6cd7572010-05-13 20:34:42 +0000353 MI->clearKillInfo();
354
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000355 return true;
356}