blob: 3d9f24e606712cc4be3e0cdd64beae2a1ffe4bdd [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"
Evan Cheng6edb0ea2010-09-17 22:28:18 +000028#include "llvm/ADT/SmallSet.h"
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000029#include "llvm/ADT/Statistic.h"
Evan Cheng4dc301a2010-08-19 17:33:11 +000030#include "llvm/Support/CommandLine.h"
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000031#include "llvm/Support/Debug.h"
Bill Wendling1e973aa2009-08-22 20:26:23 +000032#include "llvm/Support/raw_ostream.h"
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000033using namespace llvm;
34
Evan Cheng4dc301a2010-08-19 17:33:11 +000035static cl::opt<bool>
36SplitEdges("machine-sink-split",
37 cl::desc("Split critical edges during machine sinking"),
38 cl::init(false), cl::Hidden);
39static cl::opt<unsigned>
40SplitLimit("split-limit",
41 cl::init(~0u), cl::Hidden);
42
Evan Cheng6edb0ea2010-09-17 22:28:18 +000043STATISTIC(NumSunk, "Number of machine instructions sunk");
44STATISTIC(NumSplit, "Number of critical edges split");
45STATISTIC(NumCoalesces, "Number of copies coalesced");
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000046
47namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000048 class MachineSinking : public MachineFunctionPass {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000049 const TargetInstrInfo *TII;
Dan Gohman19778e72009-09-25 22:53:29 +000050 const TargetRegisterInfo *TRI;
Evan Cheng6edb0ea2010-09-17 22:28:18 +000051 MachineRegisterInfo *MRI; // Machine register information
Dan Gohmana5225ad2009-08-05 01:19:01 +000052 MachineDominatorTree *DT; // Machine dominator tree
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +000053 MachineLoopInfo *LI;
Dan Gohmana70dca12009-10-09 23:27:56 +000054 AliasAnalysis *AA;
Dan Gohman45094e32009-09-26 02:34:00 +000055 BitVector AllocatableSet; // Which physregs are allocatable?
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000056
Evan Cheng6edb0ea2010-09-17 22:28:18 +000057 // Remember which edges have been considered for breaking.
58 SmallSet<std::pair<MachineBasicBlock*,MachineBasicBlock*>, 8>
59 CEBCandidates;
60
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000061 public:
62 static char ID; // Pass identification
Owen Anderson90c579d2010-08-06 18:33:48 +000063 MachineSinking() : MachineFunctionPass(ID) {}
Jim Grosbach6ee358b2010-06-03 23:49:57 +000064
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000065 virtual bool runOnMachineFunction(MachineFunction &MF);
Jim Grosbach6ee358b2010-06-03 23:49:57 +000066
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000067 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000068 AU.setPreservesCFG();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000069 MachineFunctionPass::getAnalysisUsage(AU);
Dan Gohmana70dca12009-10-09 23:27:56 +000070 AU.addRequired<AliasAnalysis>();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000071 AU.addRequired<MachineDominatorTree>();
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +000072 AU.addRequired<MachineLoopInfo>();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000073 AU.addPreserved<MachineDominatorTree>();
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +000074 AU.addPreserved<MachineLoopInfo>();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000075 }
Evan Cheng6edb0ea2010-09-17 22:28:18 +000076
77 virtual void releaseMemory() {
78 CEBCandidates.clear();
79 }
80
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000081 private:
82 bool ProcessBlock(MachineBasicBlock &MBB);
Evan Cheng6edb0ea2010-09-17 22:28:18 +000083 bool isWorthBreakingCriticalEdge(MachineInstr *MI,
84 MachineBasicBlock *From,
85 MachineBasicBlock *To);
86 MachineBasicBlock *SplitCriticalEdge(MachineInstr *MI,
87 MachineBasicBlock *From,
88 MachineBasicBlock *To,
Evan Cheng23997862010-09-18 06:42:17 +000089 bool AllPHIUse);
Chris Lattneraad193a2008-01-12 00:17:41 +000090 bool SinkInstruction(MachineInstr *MI, bool &SawStore);
Evan Chengc3439ad2010-08-18 23:09:25 +000091 bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB,
Evan Cheng6edb0ea2010-09-17 22:28:18 +000092 MachineBasicBlock *DefMBB,
Evan Cheng23997862010-09-18 06:42:17 +000093 bool &AllPHIUse, bool &LocalUse) const;
Evan Cheng6edb0ea2010-09-17 22:28:18 +000094 bool PerformTrivialForwardCoalescing(MachineInstr *MI,
95 MachineBasicBlock *MBB);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000096 };
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000097} // end anonymous namespace
Jim Grosbach6ee358b2010-06-03 23:49:57 +000098
Dan Gohman844731a2008-05-13 00:00:25 +000099char MachineSinking::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000100INITIALIZE_PASS(MachineSinking, "machine-sink",
101 "Machine code sinking", false, false);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000102
103FunctionPass *llvm::createMachineSinkingPass() { return new MachineSinking(); }
104
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000105bool MachineSinking::PerformTrivialForwardCoalescing(MachineInstr *MI,
106 MachineBasicBlock *MBB) {
107 if (!MI->isCopy())
108 return false;
109
110 unsigned SrcReg = MI->getOperand(1).getReg();
111 unsigned DstReg = MI->getOperand(0).getReg();
112 if (!TargetRegisterInfo::isVirtualRegister(SrcReg) ||
113 !TargetRegisterInfo::isVirtualRegister(DstReg) ||
114 !MRI->hasOneNonDBGUse(SrcReg))
115 return false;
116
117 const TargetRegisterClass *SRC = MRI->getRegClass(SrcReg);
118 const TargetRegisterClass *DRC = MRI->getRegClass(DstReg);
119 if (SRC != DRC)
120 return false;
121
122 MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
123 if (DefMI->isCopyLike())
124 return false;
125 DEBUG(dbgs() << "Coalescing: " << *DefMI);
126 DEBUG(dbgs() << "*** to: " << *MI);
127 MRI->replaceRegWith(DstReg, SrcReg);
128 MI->eraseFromParent();
129 ++NumCoalesces;
130 return true;
131}
132
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000133/// AllUsesDominatedByBlock - Return true if all uses of the specified register
Evan Chengc3439ad2010-08-18 23:09:25 +0000134/// occur in blocks dominated by the specified block. If any use is in the
135/// definition block, then return false since it is never legal to move def
136/// after uses.
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000137bool
138MachineSinking::AllUsesDominatedByBlock(unsigned Reg,
139 MachineBasicBlock *MBB,
140 MachineBasicBlock *DefMBB,
Evan Cheng23997862010-09-18 06:42:17 +0000141 bool &AllPHIUse, bool &LocalUse) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000142 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
143 "Only makes sense for vregs");
Evan Cheng23997862010-09-18 06:42:17 +0000144
145 if (MRI->use_nodbg_empty(Reg))
146 return true;
147
Dale Johannesenb0812f12010-03-05 00:02:59 +0000148 // Ignoring debug uses is necessary so debug info doesn't affect the code.
149 // This may leave a referencing dbg_value in the original block, before
150 // the definition of the vreg. Dwarf generator handles this although the
151 // user might not get the right info at runtime.
Evan Cheng23997862010-09-18 06:42:17 +0000152
153 // PHI is in the successor BB. e.g.
154 // BB#1: derived from LLVM BB %bb4.preheader
155 // Predecessors according to CFG: BB#0
156 // ...
157 // %reg16385<def> = DEC64_32r %reg16437, %EFLAGS<imp-def,dead>
158 // ...
159 // JE_4 <BB#37>, %EFLAGS<imp-use>
160 // Successors according to CFG: BB#37 BB#2
161 //
162 // BB#2: derived from LLVM BB %bb.nph
163 // Predecessors according to CFG: BB#0 BB#1
164 // %reg16386<def> = PHI %reg16434, <BB#0>, %reg16385, <BB#1>
165 //
166 // Machine sink should break the critical edge first.
167 AllPHIUse = true;
168 for (MachineRegisterInfo::use_nodbg_iterator
169 I = MRI->use_nodbg_begin(Reg), E = MRI->use_nodbg_end();
170 I != E; ++I) {
171 MachineInstr *UseInst = &*I;
172 MachineBasicBlock *UseBlock = UseInst->getParent();
173 if (!(UseBlock == MBB && UseInst->isPHI() &&
174 UseInst->getOperand(I.getOperandNo()+1).getMBB() == DefMBB)) {
175 AllPHIUse = false;
176 break;
177 }
178 }
179 if (AllPHIUse)
180 return true;
181
Bill Wendling05c68372010-06-02 23:04:26 +0000182 for (MachineRegisterInfo::use_nodbg_iterator
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000183 I = MRI->use_nodbg_begin(Reg), E = MRI->use_nodbg_end();
Bill Wendling05c68372010-06-02 23:04:26 +0000184 I != E; ++I) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000185 // Determine the block of the use.
186 MachineInstr *UseInst = &*I;
187 MachineBasicBlock *UseBlock = UseInst->getParent();
Evan Cheng23997862010-09-18 06:42:17 +0000188 if (UseInst->isPHI()) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000189 // PHI nodes use the operand in the predecessor block, not the block with
190 // the PHI.
191 UseBlock = UseInst->getOperand(I.getOperandNo()+1).getMBB();
Evan Chenge5e79462010-08-19 18:33:29 +0000192 } else if (UseBlock == DefMBB) {
193 LocalUse = true;
194 return false;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000195 }
Bill Wendling05c68372010-06-02 23:04:26 +0000196
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000197 // Check that it dominates.
198 if (!DT->dominates(MBB, UseBlock))
199 return false;
200 }
Bill Wendling05c68372010-06-02 23:04:26 +0000201
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000202 return true;
203}
204
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000205bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
David Greenec19a9cd2010-01-05 01:26:00 +0000206 DEBUG(dbgs() << "******** Machine Sinking ********\n");
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000207
Dan Gohman4e9785e2009-10-19 14:52:05 +0000208 const TargetMachine &TM = MF.getTarget();
209 TII = TM.getInstrInfo();
210 TRI = TM.getRegisterInfo();
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000211 MRI = &MF.getRegInfo();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000212 DT = &getAnalysis<MachineDominatorTree>();
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +0000213 LI = &getAnalysis<MachineLoopInfo>();
Dan Gohmana70dca12009-10-09 23:27:56 +0000214 AA = &getAnalysis<AliasAnalysis>();
Dan Gohman4e9785e2009-10-19 14:52:05 +0000215 AllocatableSet = TRI->getAllocatableSet(MF);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000216
217 bool EverMadeChange = false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000218
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000219 while (1) {
220 bool MadeChange = false;
221
222 // Process all basic blocks.
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000223 CEBCandidates.clear();
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000224 for (MachineFunction::iterator I = MF.begin(), E = MF.end();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000225 I != E; ++I)
226 MadeChange |= ProcessBlock(*I);
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000227
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000228 // If this iteration over the code changed anything, keep iterating.
229 if (!MadeChange) break;
230 EverMadeChange = true;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000231 }
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000232 return EverMadeChange;
233}
234
235bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000236 // Can't sink anything out of a block that has less than two successors.
Chris Lattner296185c2009-04-10 16:38:36 +0000237 if (MBB.succ_size() <= 1 || MBB.empty()) return false;
238
Dan Gohmanc4ae94d2010-04-05 19:17:22 +0000239 // Don't bother sinking code out of unreachable blocks. In addition to being
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000240 // unprofitable, it can also lead to infinite looping, because in an
241 // unreachable loop there may be nowhere to stop.
Dan Gohmanc4ae94d2010-04-05 19:17:22 +0000242 if (!DT->isReachableFromEntry(&MBB)) return false;
243
Chris Lattner296185c2009-04-10 16:38:36 +0000244 bool MadeChange = false;
245
Chris Lattneraad193a2008-01-12 00:17:41 +0000246 // Walk the basic block bottom-up. Remember if we saw a store.
Chris Lattner296185c2009-04-10 16:38:36 +0000247 MachineBasicBlock::iterator I = MBB.end();
248 --I;
249 bool ProcessedBegin, SawStore = false;
250 do {
251 MachineInstr *MI = I; // The instruction to sink.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000252
Chris Lattner296185c2009-04-10 16:38:36 +0000253 // Predecrement I (if it's not begin) so that it isn't invalidated by
254 // sinking.
255 ProcessedBegin = I == MBB.begin();
256 if (!ProcessedBegin)
257 --I;
Dale Johannesenb0812f12010-03-05 00:02:59 +0000258
259 if (MI->isDebugValue())
260 continue;
261
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000262 if (PerformTrivialForwardCoalescing(MI, &MBB))
263 continue;
264
Chris Lattner296185c2009-04-10 16:38:36 +0000265 if (SinkInstruction(MI, SawStore))
266 ++NumSunk, MadeChange = true;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000267
Chris Lattner296185c2009-04-10 16:38:36 +0000268 // If we just processed the first instruction in the block, we're done.
269 } while (!ProcessedBegin);
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000270
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000271 return MadeChange;
272}
273
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000274bool MachineSinking::isWorthBreakingCriticalEdge(MachineInstr *MI,
275 MachineBasicBlock *From,
276 MachineBasicBlock *To) {
277 // FIXME: Need much better heuristics.
278
279 // If the pass has already considered breaking this edge (during this pass
280 // through the function), then let's go ahead and break it. This means
281 // sinking multiple "cheap" instructions into the same block.
282 if (!CEBCandidates.insert(std::make_pair(From, To)))
283 return true;
284
285 if (!(MI->isCopyLike() || MI->getDesc().isAsCheapAsAMove()))
286 return true;
287
288 // MI is cheap, we probably don't want to break the critical edge for it.
289 // However, if this would allow some definitions of its source operands
290 // to be sunk then it's probably worth it.
291 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
292 const MachineOperand &MO = MI->getOperand(i);
293 if (!MO.isReg()) continue;
294 unsigned Reg = MO.getReg();
295 if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg))
296 continue;
297 if (MRI->hasOneNonDBGUse(Reg))
298 return true;
299 }
300
301 return false;
302}
303
304MachineBasicBlock *MachineSinking::SplitCriticalEdge(MachineInstr *MI,
305 MachineBasicBlock *FromBB,
306 MachineBasicBlock *ToBB,
Evan Cheng23997862010-09-18 06:42:17 +0000307 bool AllPHIUse) {
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000308 if (!isWorthBreakingCriticalEdge(MI, FromBB, ToBB))
309 return 0;
310
Evan Cheng4dc301a2010-08-19 17:33:11 +0000311 // Avoid breaking back edge. From == To means backedge for single BB loop.
312 if (!SplitEdges || NumSplit == SplitLimit || FromBB == ToBB)
313 return 0;
314
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000315 // Check for backedges of more "complex" loops.
316 if (LI->getLoopFor(FromBB) == LI->getLoopFor(ToBB) &&
317 LI->isLoopHeader(ToBB))
318 return 0;
319
320 // It's not always legal to break critical edges and sink the computation
321 // to the edge.
322 //
323 // BB#1:
324 // v1024
325 // Beq BB#3
326 // <fallthrough>
327 // BB#2:
328 // ... no uses of v1024
329 // <fallthrough>
330 // BB#3:
331 // ...
332 // = v1024
333 //
334 // If BB#1 -> BB#3 edge is broken and computation of v1024 is inserted:
335 //
336 // BB#1:
337 // ...
338 // Bne BB#2
339 // BB#4:
340 // v1024 =
341 // B BB#3
342 // BB#2:
343 // ... no uses of v1024
344 // <fallthrough>
345 // BB#3:
346 // ...
347 // = v1024
348 //
349 // This is incorrect since v1024 is not computed along the BB#1->BB#2->BB#3
350 // flow. We need to ensure the new basic block where the computation is
351 // sunk to dominates all the uses.
352 // It's only legal to break critical edge and sink the computation to the
353 // new block if all the predecessors of "To", except for "From", are
354 // not dominated by "From". Given SSA property, this means these
355 // predecessors are dominated by "To".
356 //
357 // There is no need to do this check if all the uses are PHI nodes. PHI
358 // sources are only defined on the specific predecessor edges.
Evan Cheng23997862010-09-18 06:42:17 +0000359 if (!AllPHIUse) {
Evan Cheng4dc301a2010-08-19 17:33:11 +0000360 for (MachineBasicBlock::pred_iterator PI = ToBB->pred_begin(),
361 E = ToBB->pred_end(); PI != E; ++PI) {
362 if (*PI == FromBB)
363 continue;
364 if (!DT->dominates(ToBB, *PI))
365 return 0;
366 }
Evan Cheng4dc301a2010-08-19 17:33:11 +0000367 }
368
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000369 return FromBB->SplitCriticalEdge(ToBB, this);
Evan Cheng4dc301a2010-08-19 17:33:11 +0000370}
371
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000372/// SinkInstruction - Determine whether it is safe to sink the specified machine
373/// instruction out of its current block into a successor.
Chris Lattneraad193a2008-01-12 00:17:41 +0000374bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) {
Evan Chengb27087f2008-03-13 00:44:09 +0000375 // Check if it's safe to move the instruction.
Evan Chengac1abde2010-03-02 19:03:01 +0000376 if (!MI->isSafeToMove(TII, AA, SawStore))
Chris Lattneraad193a2008-01-12 00:17:41 +0000377 return false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000378
Chris Lattnere430e1c2008-01-05 06:47:58 +0000379 // FIXME: This should include support for sinking instructions within the
380 // block they are currently in to shorten the live ranges. We often get
381 // instructions sunk into the top of a large block, but it would be better to
382 // also sink them down before their first use in the block. This xform has to
383 // be careful not to *increase* register pressure though, e.g. sinking
384 // "x = y + z" down if it kills y and z would increase the live ranges of y
Dan Gohmana5225ad2009-08-05 01:19:01 +0000385 // and z and only shrink the live range of x.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000386
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000387 // Loop over all the operands of the specified instruction. If there is
388 // anything we can't handle, bail out.
389 MachineBasicBlock *ParentBlock = MI->getParent();
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000390
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000391 // SuccToSinkTo - This is the successor to sink this instruction to, once we
392 // decide.
393 MachineBasicBlock *SuccToSinkTo = 0;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000394
Evan Cheng23997862010-09-18 06:42:17 +0000395 bool AllPHIUse = false;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000396 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
397 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000398 if (!MO.isReg()) continue; // Ignore non-register operands.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000399
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000400 unsigned Reg = MO.getReg();
401 if (Reg == 0) continue;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000402
Dan Gohman6f0d0242008-02-10 18:45:23 +0000403 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Dan Gohman19778e72009-09-25 22:53:29 +0000404 if (MO.isUse()) {
405 // If the physreg has no defs anywhere, it's just an ambient register
Dan Gohman45094e32009-09-26 02:34:00 +0000406 // and we can freely move its uses. Alternatively, if it's allocatable,
407 // it could get allocated to something with a def during allocation.
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000408 if (!MRI->def_empty(Reg))
Dan Gohman19778e72009-09-25 22:53:29 +0000409 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000410
Dan Gohman45094e32009-09-26 02:34:00 +0000411 if (AllocatableSet.test(Reg))
412 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000413
Dan Gohman19778e72009-09-25 22:53:29 +0000414 // Check for a def among the register's aliases too.
Dan Gohman45094e32009-09-26 02:34:00 +0000415 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
416 unsigned AliasReg = *Alias;
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000417 if (!MRI->def_empty(AliasReg))
Dan Gohman19778e72009-09-25 22:53:29 +0000418 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000419
Dan Gohman45094e32009-09-26 02:34:00 +0000420 if (AllocatableSet.test(AliasReg))
421 return false;
422 }
Bill Wendling730c07e2010-06-25 20:48:10 +0000423 } else if (!MO.isDead()) {
424 // A def that isn't dead. We can't move it.
425 return false;
Dan Gohman19778e72009-09-25 22:53:29 +0000426 }
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000427 } else {
428 // Virtual register uses are always safe to sink.
429 if (MO.isUse()) continue;
Evan Chengb6f54172009-02-07 01:21:47 +0000430
431 // If it's not safe to move defs of the register class, then abort.
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000432 if (!TII->isSafeToMoveRegClassDefs(MRI->getRegClass(Reg)))
Evan Chengb6f54172009-02-07 01:21:47 +0000433 return false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000434
Chris Lattnere430e1c2008-01-05 06:47:58 +0000435 // FIXME: This picks a successor to sink into based on having one
436 // successor that dominates all the uses. However, there are cases where
437 // sinking can happen but where the sink point isn't a successor. For
438 // example:
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000439 //
Chris Lattnere430e1c2008-01-05 06:47:58 +0000440 // x = computation
441 // if () {} else {}
442 // use x
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000443 //
Bill Wendling05c68372010-06-02 23:04:26 +0000444 // the instruction could be sunk over the whole diamond for the
Chris Lattnere430e1c2008-01-05 06:47:58 +0000445 // if/then/else (or loop, etc), allowing it to be sunk into other blocks
446 // after that.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000447
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000448 // Virtual register defs can only be sunk if all their uses are in blocks
449 // dominated by one of the successors.
450 if (SuccToSinkTo) {
451 // If a previous operand picked a block to sink to, then this operand
452 // must be sinkable to the same block.
Evan Chenge5e79462010-08-19 18:33:29 +0000453 bool LocalUse = false;
Evan Cheng23997862010-09-18 06:42:17 +0000454 if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, ParentBlock,
455 AllPHIUse, LocalUse))
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000456 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000457
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000458 continue;
459 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000460
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000461 // Otherwise, we should look at all the successors and decide which one
462 // we should sink to.
463 for (MachineBasicBlock::succ_iterator SI = ParentBlock->succ_begin(),
464 E = ParentBlock->succ_end(); SI != E; ++SI) {
Evan Chenge5e79462010-08-19 18:33:29 +0000465 bool LocalUse = false;
Evan Cheng23997862010-09-18 06:42:17 +0000466 if (AllUsesDominatedByBlock(Reg, *SI, ParentBlock,
467 AllPHIUse, LocalUse)) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000468 SuccToSinkTo = *SI;
469 break;
470 }
Evan Chengc3439ad2010-08-18 23:09:25 +0000471 if (LocalUse)
472 // Def is used locally, it's never safe to move this def.
473 return false;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000474 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000475
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000476 // If we couldn't find a block to sink to, ignore this instruction.
477 if (SuccToSinkTo == 0)
478 return false;
479 }
480 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000481
Chris Lattner9bb459b2008-01-05 01:39:17 +0000482 // If there are no outputs, it must have side-effects.
483 if (SuccToSinkTo == 0)
484 return false;
Evan Chengb5999792009-02-15 08:36:12 +0000485
486 // It's not safe to sink instructions to EH landing pad. Control flow into
487 // landing pad is implicitly defined.
488 if (SuccToSinkTo->isLandingPad())
489 return false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000490
Dan Gohmandfffba62009-10-19 14:56:05 +0000491 // It is not possible to sink an instruction into its own block. This can
Chris Lattner296185c2009-04-10 16:38:36 +0000492 // happen with loops.
493 if (MI->getParent() == SuccToSinkTo)
494 return false;
Bill Wendling869d60d2010-06-03 07:54:20 +0000495
Daniel Dunbard24c9d52010-06-23 00:48:25 +0000496 // If the instruction to move defines a dead physical register which is live
497 // when leaving the basic block, don't move it because it could turn into a
498 // "zombie" define of that preg. E.g., EFLAGS. (<rdar://problem/8030636>)
Bill Wendling730c07e2010-06-25 20:48:10 +0000499 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
500 const MachineOperand &MO = MI->getOperand(I);
501 if (!MO.isReg()) continue;
502 unsigned Reg = MO.getReg();
503 if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
504 if (SuccToSinkTo->isLiveIn(Reg))
Bill Wendling869d60d2010-06-03 07:54:20 +0000505 return false;
Bill Wendling730c07e2010-06-25 20:48:10 +0000506 }
Bill Wendling869d60d2010-06-03 07:54:20 +0000507
Bill Wendling05c68372010-06-02 23:04:26 +0000508 DEBUG(dbgs() << "Sink instr " << *MI << "\tinto block " << *SuccToSinkTo);
509
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000510 // If the block has multiple predecessors, this would introduce computation on
511 // a path that it doesn't already exist. We could split the critical edge,
512 // but for now we just punt.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000513 if (SuccToSinkTo->pred_size() > 1) {
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000514 // We cannot sink a load across a critical edge - there may be stores in
515 // other code paths.
Evan Cheng4dc301a2010-08-19 17:33:11 +0000516 bool TryBreak = false;
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000517 bool store = true;
518 if (!MI->isSafeToMove(TII, AA, store)) {
Evan Chengf942c132010-08-19 23:33:02 +0000519 DEBUG(dbgs() << " *** NOTE: Won't sink load along critical edge.\n");
Evan Cheng4dc301a2010-08-19 17:33:11 +0000520 TryBreak = true;
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000521 }
522
523 // We don't want to sink across a critical edge if we don't dominate the
524 // successor. We could be introducing calculations to new code paths.
Evan Cheng4dc301a2010-08-19 17:33:11 +0000525 if (!TryBreak && !DT->dominates(ParentBlock, SuccToSinkTo)) {
Evan Chengf942c132010-08-19 23:33:02 +0000526 DEBUG(dbgs() << " *** NOTE: Critical edge found\n");
Evan Cheng4dc301a2010-08-19 17:33:11 +0000527 TryBreak = true;
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000528 }
529
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +0000530 // Don't sink instructions into a loop.
Evan Cheng4dc301a2010-08-19 17:33:11 +0000531 if (!TryBreak && LI->isLoopHeader(SuccToSinkTo)) {
Evan Chengf942c132010-08-19 23:33:02 +0000532 DEBUG(dbgs() << " *** NOTE: Loop header found\n");
Evan Cheng4dc301a2010-08-19 17:33:11 +0000533 TryBreak = true;
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +0000534 }
535
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000536 // Otherwise we are OK with sinking along a critical edge.
Evan Cheng4dc301a2010-08-19 17:33:11 +0000537 if (!TryBreak)
538 DEBUG(dbgs() << "Sinking along critical edge.\n");
539 else {
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000540 MachineBasicBlock *NewSucc =
Evan Cheng23997862010-09-18 06:42:17 +0000541 SplitCriticalEdge(MI, ParentBlock, SuccToSinkTo, AllPHIUse);
Evan Cheng4dc301a2010-08-19 17:33:11 +0000542 if (!NewSucc) {
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000543 DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
544 "break critical edge\n");
Evan Cheng4dc301a2010-08-19 17:33:11 +0000545 return false;
546 } else {
Evan Chengf942c132010-08-19 23:33:02 +0000547 DEBUG(dbgs() << " *** Splitting critical edge:"
Evan Cheng4dc301a2010-08-19 17:33:11 +0000548 " BB#" << ParentBlock->getNumber()
549 << " -- BB#" << NewSucc->getNumber()
550 << " -- BB#" << SuccToSinkTo->getNumber() << '\n');
Evan Cheng4dc301a2010-08-19 17:33:11 +0000551 SuccToSinkTo = NewSucc;
552 ++NumSplit;
553 }
554 }
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000555 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000556
Evan Cheng23997862010-09-18 06:42:17 +0000557 if (AllPHIUse) {
558 if (NumSplit == SplitLimit)
559 return false;
560 MachineBasicBlock *NewSucc = SplitCriticalEdge(MI, ParentBlock,
561 SuccToSinkTo, AllPHIUse);
562 if (!NewSucc) {
563 DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
564 "break critical edge\n");
565 return false;
566 }
567
568 DEBUG(dbgs() << " *** Splitting critical edge:"
569 " BB#" << ParentBlock->getNumber()
570 << " -- BB#" << NewSucc->getNumber()
571 << " -- BB#" << SuccToSinkTo->getNumber() << '\n');
572 SuccToSinkTo = NewSucc;
573 ++NumSplit;
574 }
575
Bill Wendling05c68372010-06-02 23:04:26 +0000576 // Determine where to insert into. Skip phi nodes.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000577 MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
Evan Cheng23997862010-09-18 06:42:17 +0000578 while (InsertPos != SuccToSinkTo->end() && InsertPos->isPHI())
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000579 ++InsertPos;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000580
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000581 // Move the instruction.
582 SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
583 ++MachineBasicBlock::iterator(MI));
Dan Gohmane6cd7572010-05-13 20:34:42 +0000584
Bill Wendling05c68372010-06-02 23:04:26 +0000585 // Conservatively, clear any kill flags, since it's possible that they are no
586 // longer correct.
Dan Gohmane6cd7572010-05-13 20:34:42 +0000587 MI->clearKillInfo();
588
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000589 return true;
590}