blob: 81ea5ec551a6eaf27ed3214ee103930f99bb40cd [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"
Evan Cheng4dc301a2010-08-19 17:33:11 +000029#include "llvm/Support/CommandLine.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
Evan Cheng4dc301a2010-08-19 17:33:11 +000034static cl::opt<bool>
35SplitEdges("machine-sink-split",
36 cl::desc("Split critical edges during machine sinking"),
37 cl::init(false), cl::Hidden);
38static cl::opt<unsigned>
39SplitLimit("split-limit",
40 cl::init(~0u), cl::Hidden);
41
42STATISTIC(NumSunk, "Number of machine instructions sunk");
43STATISTIC(NumSplit, "Number of critical edges split");
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000044
45namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000046 class MachineSinking : public MachineFunctionPass {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000047 const TargetInstrInfo *TII;
Dan Gohman19778e72009-09-25 22:53:29 +000048 const TargetRegisterInfo *TRI;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000049 MachineRegisterInfo *RegInfo; // Machine register information
Dan Gohmana5225ad2009-08-05 01:19:01 +000050 MachineDominatorTree *DT; // Machine dominator tree
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +000051 MachineLoopInfo *LI;
Dan Gohmana70dca12009-10-09 23:27:56 +000052 AliasAnalysis *AA;
Dan Gohman45094e32009-09-26 02:34:00 +000053 BitVector AllocatableSet; // Which physregs are allocatable?
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000054
55 public:
56 static char ID; // Pass identification
Owen Anderson90c579d2010-08-06 18:33:48 +000057 MachineSinking() : MachineFunctionPass(ID) {}
Jim Grosbach6ee358b2010-06-03 23:49:57 +000058
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000059 virtual bool runOnMachineFunction(MachineFunction &MF);
Jim Grosbach6ee358b2010-06-03 23:49:57 +000060
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000061 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000062 AU.setPreservesCFG();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000063 MachineFunctionPass::getAnalysisUsage(AU);
Dan Gohmana70dca12009-10-09 23:27:56 +000064 AU.addRequired<AliasAnalysis>();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000065 AU.addRequired<MachineDominatorTree>();
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +000066 AU.addRequired<MachineLoopInfo>();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000067 AU.addPreserved<MachineDominatorTree>();
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +000068 AU.addPreserved<MachineLoopInfo>();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000069 }
70 private:
71 bool ProcessBlock(MachineBasicBlock &MBB);
Evan Cheng4dc301a2010-08-19 17:33:11 +000072 MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *From,
73 MachineBasicBlock *To);
Chris Lattneraad193a2008-01-12 00:17:41 +000074 bool SinkInstruction(MachineInstr *MI, bool &SawStore);
Evan Chengc3439ad2010-08-18 23:09:25 +000075 bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB,
76 MachineBasicBlock *DefMBB, bool &LocalUse) const;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000077 };
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000078} // end anonymous namespace
Jim Grosbach6ee358b2010-06-03 23:49:57 +000079
Dan Gohman844731a2008-05-13 00:00:25 +000080char MachineSinking::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000081INITIALIZE_PASS(MachineSinking, "machine-sink",
82 "Machine code sinking", false, false);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000083
84FunctionPass *llvm::createMachineSinkingPass() { return new MachineSinking(); }
85
86/// AllUsesDominatedByBlock - Return true if all uses of the specified register
Evan Chengc3439ad2010-08-18 23:09:25 +000087/// occur in blocks dominated by the specified block. If any use is in the
88/// definition block, then return false since it is never legal to move def
89/// after uses.
Jim Grosbach6ee358b2010-06-03 23:49:57 +000090bool MachineSinking::AllUsesDominatedByBlock(unsigned Reg,
Evan Chengc3439ad2010-08-18 23:09:25 +000091 MachineBasicBlock *MBB,
92 MachineBasicBlock *DefMBB,
93 bool &LocalUse) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +000094 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
95 "Only makes sense for vregs");
Dale Johannesenb0812f12010-03-05 00:02:59 +000096 // Ignoring debug uses is necessary so debug info doesn't affect the code.
97 // This may leave a referencing dbg_value in the original block, before
98 // the definition of the vreg. Dwarf generator handles this although the
99 // user might not get the right info at runtime.
Bill Wendling05c68372010-06-02 23:04:26 +0000100 for (MachineRegisterInfo::use_nodbg_iterator
101 I = RegInfo->use_nodbg_begin(Reg), E = RegInfo->use_nodbg_end();
102 I != E; ++I) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000103 // Determine the block of the use.
104 MachineInstr *UseInst = &*I;
105 MachineBasicBlock *UseBlock = UseInst->getParent();
Evan Chengc3439ad2010-08-18 23:09:25 +0000106 if (UseBlock == DefMBB) {
107 LocalUse = true;
108 return false;
109 }
Bill Wendling05c68372010-06-02 23:04:26 +0000110
Chris Lattner518bb532010-02-09 19:54:29 +0000111 if (UseInst->isPHI()) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000112 // PHI nodes use the operand in the predecessor block, not the block with
113 // the PHI.
114 UseBlock = UseInst->getOperand(I.getOperandNo()+1).getMBB();
115 }
Bill Wendling05c68372010-06-02 23:04:26 +0000116
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000117 // Check that it dominates.
118 if (!DT->dominates(MBB, UseBlock))
119 return false;
120 }
Bill Wendling05c68372010-06-02 23:04:26 +0000121
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000122 return true;
123}
124
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000125bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
David Greenec19a9cd2010-01-05 01:26:00 +0000126 DEBUG(dbgs() << "******** Machine Sinking ********\n");
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000127
Dan Gohman4e9785e2009-10-19 14:52:05 +0000128 const TargetMachine &TM = MF.getTarget();
129 TII = TM.getInstrInfo();
130 TRI = TM.getRegisterInfo();
131 RegInfo = &MF.getRegInfo();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000132 DT = &getAnalysis<MachineDominatorTree>();
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +0000133 LI = &getAnalysis<MachineLoopInfo>();
Dan Gohmana70dca12009-10-09 23:27:56 +0000134 AA = &getAnalysis<AliasAnalysis>();
Dan Gohman4e9785e2009-10-19 14:52:05 +0000135 AllocatableSet = TRI->getAllocatableSet(MF);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000136
137 bool EverMadeChange = false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000138
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000139 while (1) {
140 bool MadeChange = false;
141
142 // Process all basic blocks.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000143 for (MachineFunction::iterator I = MF.begin(), E = MF.end();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000144 I != E; ++I)
145 MadeChange |= ProcessBlock(*I);
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000146
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000147 // If this iteration over the code changed anything, keep iterating.
148 if (!MadeChange) break;
149 EverMadeChange = true;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000150 }
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000151 return EverMadeChange;
152}
153
154bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000155 // Can't sink anything out of a block that has less than two successors.
Chris Lattner296185c2009-04-10 16:38:36 +0000156 if (MBB.succ_size() <= 1 || MBB.empty()) return false;
157
Dan Gohmanc4ae94d2010-04-05 19:17:22 +0000158 // Don't bother sinking code out of unreachable blocks. In addition to being
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000159 // unprofitable, it can also lead to infinite looping, because in an
160 // unreachable loop there may be nowhere to stop.
Dan Gohmanc4ae94d2010-04-05 19:17:22 +0000161 if (!DT->isReachableFromEntry(&MBB)) return false;
162
Chris Lattner296185c2009-04-10 16:38:36 +0000163 bool MadeChange = false;
164
Chris Lattneraad193a2008-01-12 00:17:41 +0000165 // Walk the basic block bottom-up. Remember if we saw a store.
Chris Lattner296185c2009-04-10 16:38:36 +0000166 MachineBasicBlock::iterator I = MBB.end();
167 --I;
168 bool ProcessedBegin, SawStore = false;
169 do {
170 MachineInstr *MI = I; // The instruction to sink.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000171
Chris Lattner296185c2009-04-10 16:38:36 +0000172 // Predecrement I (if it's not begin) so that it isn't invalidated by
173 // sinking.
174 ProcessedBegin = I == MBB.begin();
175 if (!ProcessedBegin)
176 --I;
Dale Johannesenb0812f12010-03-05 00:02:59 +0000177
178 if (MI->isDebugValue())
179 continue;
180
Chris Lattner296185c2009-04-10 16:38:36 +0000181 if (SinkInstruction(MI, SawStore))
182 ++NumSunk, MadeChange = true;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000183
Chris Lattner296185c2009-04-10 16:38:36 +0000184 // If we just processed the first instruction in the block, we're done.
185 } while (!ProcessedBegin);
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000186
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000187 return MadeChange;
188}
189
Evan Cheng4dc301a2010-08-19 17:33:11 +0000190MachineBasicBlock *MachineSinking::SplitCriticalEdge(MachineBasicBlock *FromBB,
191 MachineBasicBlock *ToBB) {
192 // Avoid breaking back edge. From == To means backedge for single BB loop.
193 if (!SplitEdges || NumSplit == SplitLimit || FromBB == ToBB)
194 return 0;
195
196 // Check for more "complex" loops.
197 if (LI->getLoopFor(FromBB) != LI->getLoopFor(ToBB) ||
198 !LI->isLoopHeader(ToBB)) {
199 // It's not always legal to break critical edges and sink the computation
200 // to the edge.
201 //
202 // BB#1:
203 // v1024
204 // Beq BB#3
205 // <fallthrough>
206 // BB#2:
207 // ... no uses of v1024
208 // <fallthrough>
209 // BB#3:
210 // ...
211 // = v1024
212 //
213 // If BB#1 -> BB#3 edge is broken and computation of v1024 is inserted:
214 //
215 // BB#1:
216 // ...
217 // Bne BB#2
218 // BB#4:
219 // v1024 =
220 // B BB#3
221 // BB#2:
222 // ... no uses of v1024
223 // <fallthrough>
224 // BB#3:
225 // ...
226 // = v1024
227 //
228 // This is incorrect since v1024 is not computed along the BB#1->BB#2->BB#3
229 // flow. We need to ensure the new basic block where the computation is
230 // sunk to dominates all the uses.
231 // It's only legal to break critical edge and sink the computation to the
232 // new block if all the predecessors of "To", except for "From", are
233 // not dominated by "From". Given SSA property, this means these
234 // predecessors are dominated by "To".
235 for (MachineBasicBlock::pred_iterator PI = ToBB->pred_begin(),
236 E = ToBB->pred_end(); PI != E; ++PI) {
237 if (*PI == FromBB)
238 continue;
239 if (!DT->dominates(ToBB, *PI))
240 return 0;
241 }
242
243 // FIXME: Determine if it's cost effective to break this edge.
244 return FromBB->SplitCriticalEdge(ToBB, this);
245 }
246
247 return 0;
248}
249
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000250/// SinkInstruction - Determine whether it is safe to sink the specified machine
251/// instruction out of its current block into a successor.
Chris Lattneraad193a2008-01-12 00:17:41 +0000252bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) {
Evan Chengb27087f2008-03-13 00:44:09 +0000253 // Check if it's safe to move the instruction.
Evan Chengac1abde2010-03-02 19:03:01 +0000254 if (!MI->isSafeToMove(TII, AA, SawStore))
Chris Lattneraad193a2008-01-12 00:17:41 +0000255 return false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000256
Chris Lattnere430e1c2008-01-05 06:47:58 +0000257 // FIXME: This should include support for sinking instructions within the
258 // block they are currently in to shorten the live ranges. We often get
259 // instructions sunk into the top of a large block, but it would be better to
260 // also sink them down before their first use in the block. This xform has to
261 // be careful not to *increase* register pressure though, e.g. sinking
262 // "x = y + z" down if it kills y and z would increase the live ranges of y
Dan Gohmana5225ad2009-08-05 01:19:01 +0000263 // and z and only shrink the live range of x.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000264
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000265 // Loop over all the operands of the specified instruction. If there is
266 // anything we can't handle, bail out.
267 MachineBasicBlock *ParentBlock = MI->getParent();
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000268
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000269 // SuccToSinkTo - This is the successor to sink this instruction to, once we
270 // decide.
271 MachineBasicBlock *SuccToSinkTo = 0;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000272
Evan Chengc3439ad2010-08-18 23:09:25 +0000273 bool LocalUse = false;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000274 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
275 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000276 if (!MO.isReg()) continue; // Ignore non-register operands.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000277
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000278 unsigned Reg = MO.getReg();
279 if (Reg == 0) continue;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000280
Dan Gohman6f0d0242008-02-10 18:45:23 +0000281 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Dan Gohman19778e72009-09-25 22:53:29 +0000282 if (MO.isUse()) {
283 // If the physreg has no defs anywhere, it's just an ambient register
Dan Gohman45094e32009-09-26 02:34:00 +0000284 // and we can freely move its uses. Alternatively, if it's allocatable,
285 // it could get allocated to something with a def during allocation.
Dan Gohman19778e72009-09-25 22:53:29 +0000286 if (!RegInfo->def_empty(Reg))
287 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000288
Dan Gohman45094e32009-09-26 02:34:00 +0000289 if (AllocatableSet.test(Reg))
290 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000291
Dan Gohman19778e72009-09-25 22:53:29 +0000292 // Check for a def among the register's aliases too.
Dan Gohman45094e32009-09-26 02:34:00 +0000293 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
294 unsigned AliasReg = *Alias;
295 if (!RegInfo->def_empty(AliasReg))
Dan Gohman19778e72009-09-25 22:53:29 +0000296 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000297
Dan Gohman45094e32009-09-26 02:34:00 +0000298 if (AllocatableSet.test(AliasReg))
299 return false;
300 }
Bill Wendling730c07e2010-06-25 20:48:10 +0000301 } else if (!MO.isDead()) {
302 // A def that isn't dead. We can't move it.
303 return false;
Dan Gohman19778e72009-09-25 22:53:29 +0000304 }
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000305 } else {
306 // Virtual register uses are always safe to sink.
307 if (MO.isUse()) continue;
Evan Chengb6f54172009-02-07 01:21:47 +0000308
309 // If it's not safe to move defs of the register class, then abort.
310 if (!TII->isSafeToMoveRegClassDefs(RegInfo->getRegClass(Reg)))
311 return false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000312
Chris Lattnere430e1c2008-01-05 06:47:58 +0000313 // FIXME: This picks a successor to sink into based on having one
314 // successor that dominates all the uses. However, there are cases where
315 // sinking can happen but where the sink point isn't a successor. For
316 // example:
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000317 //
Chris Lattnere430e1c2008-01-05 06:47:58 +0000318 // x = computation
319 // if () {} else {}
320 // use x
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000321 //
Bill Wendling05c68372010-06-02 23:04:26 +0000322 // the instruction could be sunk over the whole diamond for the
Chris Lattnere430e1c2008-01-05 06:47:58 +0000323 // if/then/else (or loop, etc), allowing it to be sunk into other blocks
324 // after that.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000325
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000326 // Virtual register defs can only be sunk if all their uses are in blocks
327 // dominated by one of the successors.
328 if (SuccToSinkTo) {
329 // If a previous operand picked a block to sink to, then this operand
330 // must be sinkable to the same block.
Evan Chengc3439ad2010-08-18 23:09:25 +0000331 if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, ParentBlock, LocalUse))
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000332 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000333
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000334 continue;
335 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000336
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000337 // Otherwise, we should look at all the successors and decide which one
338 // we should sink to.
339 for (MachineBasicBlock::succ_iterator SI = ParentBlock->succ_begin(),
340 E = ParentBlock->succ_end(); SI != E; ++SI) {
Evan Chengc3439ad2010-08-18 23:09:25 +0000341 if (AllUsesDominatedByBlock(Reg, *SI, ParentBlock, LocalUse)) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000342 SuccToSinkTo = *SI;
343 break;
344 }
Evan Chengc3439ad2010-08-18 23:09:25 +0000345 if (LocalUse)
346 // Def is used locally, it's never safe to move this def.
347 return false;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000348 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000349
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000350 // If we couldn't find a block to sink to, ignore this instruction.
351 if (SuccToSinkTo == 0)
352 return false;
353 }
354 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000355
Chris Lattner9bb459b2008-01-05 01:39:17 +0000356 // If there are no outputs, it must have side-effects.
357 if (SuccToSinkTo == 0)
358 return false;
Evan Chengb5999792009-02-15 08:36:12 +0000359
360 // It's not safe to sink instructions to EH landing pad. Control flow into
361 // landing pad is implicitly defined.
362 if (SuccToSinkTo->isLandingPad())
363 return false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000364
Dan Gohmandfffba62009-10-19 14:56:05 +0000365 // It is not possible to sink an instruction into its own block. This can
Chris Lattner296185c2009-04-10 16:38:36 +0000366 // happen with loops.
367 if (MI->getParent() == SuccToSinkTo)
368 return false;
Bill Wendling869d60d2010-06-03 07:54:20 +0000369
Daniel Dunbard24c9d52010-06-23 00:48:25 +0000370 // If the instruction to move defines a dead physical register which is live
371 // when leaving the basic block, don't move it because it could turn into a
372 // "zombie" define of that preg. E.g., EFLAGS. (<rdar://problem/8030636>)
Bill Wendling730c07e2010-06-25 20:48:10 +0000373 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
374 const MachineOperand &MO = MI->getOperand(I);
375 if (!MO.isReg()) continue;
376 unsigned Reg = MO.getReg();
377 if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
378 if (SuccToSinkTo->isLiveIn(Reg))
Bill Wendling869d60d2010-06-03 07:54:20 +0000379 return false;
Bill Wendling730c07e2010-06-25 20:48:10 +0000380 }
Bill Wendling869d60d2010-06-03 07:54:20 +0000381
Bill Wendling05c68372010-06-02 23:04:26 +0000382 DEBUG(dbgs() << "Sink instr " << *MI << "\tinto block " << *SuccToSinkTo);
383
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000384 // If the block has multiple predecessors, this would introduce computation on
385 // a path that it doesn't already exist. We could split the critical edge,
386 // but for now we just punt.
Chris Lattnere430e1c2008-01-05 06:47:58 +0000387 // FIXME: Split critical edges if not backedges.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000388 if (SuccToSinkTo->pred_size() > 1) {
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000389 // We cannot sink a load across a critical edge - there may be stores in
390 // other code paths.
Evan Cheng4dc301a2010-08-19 17:33:11 +0000391 bool TryBreak = false;
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000392 bool store = true;
393 if (!MI->isSafeToMove(TII, AA, store)) {
Evan Cheng4dc301a2010-08-19 17:33:11 +0000394 DEBUG(dbgs() << " *** PUNTING: Won't sink load along critical edge.\n");
395 TryBreak = true;
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000396 }
397
398 // We don't want to sink across a critical edge if we don't dominate the
399 // successor. We could be introducing calculations to new code paths.
Evan Cheng4dc301a2010-08-19 17:33:11 +0000400 if (!TryBreak && !DT->dominates(ParentBlock, SuccToSinkTo)) {
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000401 DEBUG(dbgs() << " *** PUNTING: Critical edge found\n");
Evan Cheng4dc301a2010-08-19 17:33:11 +0000402 TryBreak = true;
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000403 }
404
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +0000405 // Don't sink instructions into a loop.
Evan Cheng4dc301a2010-08-19 17:33:11 +0000406 if (!TryBreak && LI->isLoopHeader(SuccToSinkTo)) {
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +0000407 DEBUG(dbgs() << " *** PUNTING: Loop header found\n");
Evan Cheng4dc301a2010-08-19 17:33:11 +0000408 TryBreak = true;
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +0000409 }
410
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000411 // Otherwise we are OK with sinking along a critical edge.
Evan Cheng4dc301a2010-08-19 17:33:11 +0000412 if (!TryBreak)
413 DEBUG(dbgs() << "Sinking along critical edge.\n");
414 else {
415 MachineBasicBlock *NewSucc = SplitCriticalEdge(ParentBlock, SuccToSinkTo);
416 if (!NewSucc) {
417 DEBUG(dbgs() <<
418 " *** PUNTING: Not legal or profitable to break critical edge\n");
419 return false;
420 } else {
421 DEBUG(dbgs() << "*** Splitting critical edge:"
422 " BB#" << ParentBlock->getNumber()
423 << " -- BB#" << NewSucc->getNumber()
424 << " -- BB#" << SuccToSinkTo->getNumber() << '\n');
425 //assert(DT->dominates(NewSucc, SuccToSinkTo) &&
426 //"New BB doesn't dominate all uses!");
427 SuccToSinkTo = NewSucc;
428 ++NumSplit;
429 }
430 }
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000431 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000432
Bill Wendling05c68372010-06-02 23:04:26 +0000433 // Determine where to insert into. Skip phi nodes.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000434 MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
Chris Lattner518bb532010-02-09 19:54:29 +0000435 while (InsertPos != SuccToSinkTo->end() && InsertPos->isPHI())
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000436 ++InsertPos;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000437
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000438 // Move the instruction.
439 SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
440 ++MachineBasicBlock::iterator(MI));
Dan Gohmane6cd7572010-05-13 20:34:42 +0000441
Bill Wendling05c68372010-06-02 23:04:26 +0000442 // Conservatively, clear any kill flags, since it's possible that they are no
443 // longer correct.
Dan Gohmane6cd7572010-05-13 20:34:42 +0000444 MI->clearKillInfo();
445
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000446 return true;
447}