blob: ec8f7138ccee45ac2cb02c2d09966946e72fe15e [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"),
Evan Cheng44be1a82010-09-20 22:52:00 +000038 cl::init(true), cl::Hidden);
Evan Cheng4dc301a2010-08-19 17:33:11 +000039
Evan Cheng6edb0ea2010-09-17 22:28:18 +000040STATISTIC(NumSunk, "Number of machine instructions sunk");
41STATISTIC(NumSplit, "Number of critical edges split");
42STATISTIC(NumCoalesces, "Number of copies coalesced");
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000043
44namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000045 class MachineSinking : public MachineFunctionPass {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000046 const TargetInstrInfo *TII;
Dan Gohman19778e72009-09-25 22:53:29 +000047 const TargetRegisterInfo *TRI;
Evan Cheng6edb0ea2010-09-17 22:28:18 +000048 MachineRegisterInfo *MRI; // Machine register information
Dan Gohmana5225ad2009-08-05 01:19:01 +000049 MachineDominatorTree *DT; // Machine dominator tree
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +000050 MachineLoopInfo *LI;
Dan Gohmana70dca12009-10-09 23:27:56 +000051 AliasAnalysis *AA;
Dan Gohman45094e32009-09-26 02:34:00 +000052 BitVector AllocatableSet; // Which physregs are allocatable?
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000053
Evan Cheng6edb0ea2010-09-17 22:28:18 +000054 // Remember which edges have been considered for breaking.
55 SmallSet<std::pair<MachineBasicBlock*,MachineBasicBlock*>, 8>
56 CEBCandidates;
57
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000058 public:
59 static char ID; // Pass identification
Owen Anderson90c579d2010-08-06 18:33:48 +000060 MachineSinking() : MachineFunctionPass(ID) {}
Jim Grosbach6ee358b2010-06-03 23:49:57 +000061
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000062 virtual bool runOnMachineFunction(MachineFunction &MF);
Jim Grosbach6ee358b2010-06-03 23:49:57 +000063
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000064 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000065 AU.setPreservesCFG();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000066 MachineFunctionPass::getAnalysisUsage(AU);
Dan Gohmana70dca12009-10-09 23:27:56 +000067 AU.addRequired<AliasAnalysis>();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000068 AU.addRequired<MachineDominatorTree>();
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +000069 AU.addRequired<MachineLoopInfo>();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000070 AU.addPreserved<MachineDominatorTree>();
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +000071 AU.addPreserved<MachineLoopInfo>();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000072 }
Evan Cheng6edb0ea2010-09-17 22:28:18 +000073
74 virtual void releaseMemory() {
75 CEBCandidates.clear();
76 }
77
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000078 private:
79 bool ProcessBlock(MachineBasicBlock &MBB);
Evan Cheng6edb0ea2010-09-17 22:28:18 +000080 bool isWorthBreakingCriticalEdge(MachineInstr *MI,
81 MachineBasicBlock *From,
82 MachineBasicBlock *To);
83 MachineBasicBlock *SplitCriticalEdge(MachineInstr *MI,
84 MachineBasicBlock *From,
85 MachineBasicBlock *To,
Evan Cheng7af6dc42010-09-20 19:12:55 +000086 bool BreakPHIEdge);
Chris Lattneraad193a2008-01-12 00:17:41 +000087 bool SinkInstruction(MachineInstr *MI, bool &SawStore);
Evan Chengc3439ad2010-08-18 23:09:25 +000088 bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB,
Evan Cheng6edb0ea2010-09-17 22:28:18 +000089 MachineBasicBlock *DefMBB,
Evan Cheng7af6dc42010-09-20 19:12:55 +000090 bool &BreakPHIEdge, bool &LocalUse) const;
Evan Cheng6edb0ea2010-09-17 22:28:18 +000091 bool PerformTrivialForwardCoalescing(MachineInstr *MI,
92 MachineBasicBlock *MBB);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000093 };
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000094} // end anonymous namespace
Jim Grosbach6ee358b2010-06-03 23:49:57 +000095
Dan Gohman844731a2008-05-13 00:00:25 +000096char MachineSinking::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +000097INITIALIZE_PASS_BEGIN(MachineSinking, "machine-sink",
98 "Machine code sinking", false, false)
99INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
100INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
101INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
102INITIALIZE_PASS_END(MachineSinking, "machine-sink",
Owen Andersonce665bd2010-10-07 22:25:06 +0000103 "Machine code sinking", false, false)
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000104
105FunctionPass *llvm::createMachineSinkingPass() { return new MachineSinking(); }
106
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000107bool MachineSinking::PerformTrivialForwardCoalescing(MachineInstr *MI,
108 MachineBasicBlock *MBB) {
109 if (!MI->isCopy())
110 return false;
111
112 unsigned SrcReg = MI->getOperand(1).getReg();
113 unsigned DstReg = MI->getOperand(0).getReg();
114 if (!TargetRegisterInfo::isVirtualRegister(SrcReg) ||
115 !TargetRegisterInfo::isVirtualRegister(DstReg) ||
116 !MRI->hasOneNonDBGUse(SrcReg))
117 return false;
118
119 const TargetRegisterClass *SRC = MRI->getRegClass(SrcReg);
120 const TargetRegisterClass *DRC = MRI->getRegClass(DstReg);
121 if (SRC != DRC)
122 return false;
123
124 MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
125 if (DefMI->isCopyLike())
126 return false;
127 DEBUG(dbgs() << "Coalescing: " << *DefMI);
128 DEBUG(dbgs() << "*** to: " << *MI);
129 MRI->replaceRegWith(DstReg, SrcReg);
130 MI->eraseFromParent();
131 ++NumCoalesces;
132 return true;
133}
134
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000135/// AllUsesDominatedByBlock - Return true if all uses of the specified register
Evan Chengc3439ad2010-08-18 23:09:25 +0000136/// occur in blocks dominated by the specified block. If any use is in the
137/// definition block, then return false since it is never legal to move def
138/// after uses.
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000139bool
140MachineSinking::AllUsesDominatedByBlock(unsigned Reg,
141 MachineBasicBlock *MBB,
142 MachineBasicBlock *DefMBB,
Evan Cheng7af6dc42010-09-20 19:12:55 +0000143 bool &BreakPHIEdge,
144 bool &LocalUse) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000145 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
146 "Only makes sense for vregs");
Evan Cheng23997862010-09-18 06:42:17 +0000147
148 if (MRI->use_nodbg_empty(Reg))
149 return true;
150
Dale Johannesenb0812f12010-03-05 00:02:59 +0000151 // Ignoring debug uses is necessary so debug info doesn't affect the code.
152 // This may leave a referencing dbg_value in the original block, before
153 // the definition of the vreg. Dwarf generator handles this although the
154 // user might not get the right info at runtime.
Evan Cheng23997862010-09-18 06:42:17 +0000155
Evan Cheng7af6dc42010-09-20 19:12:55 +0000156 // BreakPHIEdge is true if all the uses are in the successor MBB being sunken
157 // into and they are all PHI nodes. In this case, machine-sink must break
158 // the critical edge first. e.g.
159 //
Evan Cheng23997862010-09-18 06:42:17 +0000160 // BB#1: derived from LLVM BB %bb4.preheader
161 // Predecessors according to CFG: BB#0
162 // ...
163 // %reg16385<def> = DEC64_32r %reg16437, %EFLAGS<imp-def,dead>
164 // ...
165 // JE_4 <BB#37>, %EFLAGS<imp-use>
166 // Successors according to CFG: BB#37 BB#2
167 //
168 // BB#2: derived from LLVM BB %bb.nph
169 // Predecessors according to CFG: BB#0 BB#1
170 // %reg16386<def> = PHI %reg16434, <BB#0>, %reg16385, <BB#1>
Evan Cheng7af6dc42010-09-20 19:12:55 +0000171 BreakPHIEdge = true;
Evan Cheng23997862010-09-18 06:42:17 +0000172 for (MachineRegisterInfo::use_nodbg_iterator
173 I = MRI->use_nodbg_begin(Reg), E = MRI->use_nodbg_end();
174 I != E; ++I) {
175 MachineInstr *UseInst = &*I;
176 MachineBasicBlock *UseBlock = UseInst->getParent();
177 if (!(UseBlock == MBB && UseInst->isPHI() &&
178 UseInst->getOperand(I.getOperandNo()+1).getMBB() == DefMBB)) {
Evan Cheng7af6dc42010-09-20 19:12:55 +0000179 BreakPHIEdge = false;
Evan Cheng23997862010-09-18 06:42:17 +0000180 break;
181 }
182 }
Evan Cheng7af6dc42010-09-20 19:12:55 +0000183 if (BreakPHIEdge)
Evan Cheng23997862010-09-18 06:42:17 +0000184 return true;
185
Bill Wendling05c68372010-06-02 23:04:26 +0000186 for (MachineRegisterInfo::use_nodbg_iterator
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000187 I = MRI->use_nodbg_begin(Reg), E = MRI->use_nodbg_end();
Bill Wendling05c68372010-06-02 23:04:26 +0000188 I != E; ++I) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000189 // Determine the block of the use.
190 MachineInstr *UseInst = &*I;
191 MachineBasicBlock *UseBlock = UseInst->getParent();
Evan Cheng23997862010-09-18 06:42:17 +0000192 if (UseInst->isPHI()) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000193 // PHI nodes use the operand in the predecessor block, not the block with
194 // the PHI.
195 UseBlock = UseInst->getOperand(I.getOperandNo()+1).getMBB();
Evan Chenge5e79462010-08-19 18:33:29 +0000196 } else if (UseBlock == DefMBB) {
197 LocalUse = true;
198 return false;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000199 }
Bill Wendling05c68372010-06-02 23:04:26 +0000200
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000201 // Check that it dominates.
202 if (!DT->dominates(MBB, UseBlock))
203 return false;
204 }
Bill Wendling05c68372010-06-02 23:04:26 +0000205
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000206 return true;
207}
208
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000209bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
David Greenec19a9cd2010-01-05 01:26:00 +0000210 DEBUG(dbgs() << "******** Machine Sinking ********\n");
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000211
Dan Gohman4e9785e2009-10-19 14:52:05 +0000212 const TargetMachine &TM = MF.getTarget();
213 TII = TM.getInstrInfo();
214 TRI = TM.getRegisterInfo();
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000215 MRI = &MF.getRegInfo();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000216 DT = &getAnalysis<MachineDominatorTree>();
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +0000217 LI = &getAnalysis<MachineLoopInfo>();
Dan Gohmana70dca12009-10-09 23:27:56 +0000218 AA = &getAnalysis<AliasAnalysis>();
Dan Gohman4e9785e2009-10-19 14:52:05 +0000219 AllocatableSet = TRI->getAllocatableSet(MF);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000220
221 bool EverMadeChange = false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000222
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000223 while (1) {
224 bool MadeChange = false;
225
226 // Process all basic blocks.
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000227 CEBCandidates.clear();
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000228 for (MachineFunction::iterator I = MF.begin(), E = MF.end();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000229 I != E; ++I)
230 MadeChange |= ProcessBlock(*I);
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000231
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000232 // If this iteration over the code changed anything, keep iterating.
233 if (!MadeChange) break;
234 EverMadeChange = true;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000235 }
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000236 return EverMadeChange;
237}
238
239bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000240 // Can't sink anything out of a block that has less than two successors.
Chris Lattner296185c2009-04-10 16:38:36 +0000241 if (MBB.succ_size() <= 1 || MBB.empty()) return false;
242
Dan Gohmanc4ae94d2010-04-05 19:17:22 +0000243 // Don't bother sinking code out of unreachable blocks. In addition to being
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000244 // unprofitable, it can also lead to infinite looping, because in an
245 // unreachable loop there may be nowhere to stop.
Dan Gohmanc4ae94d2010-04-05 19:17:22 +0000246 if (!DT->isReachableFromEntry(&MBB)) return false;
247
Chris Lattner296185c2009-04-10 16:38:36 +0000248 bool MadeChange = false;
249
Chris Lattneraad193a2008-01-12 00:17:41 +0000250 // Walk the basic block bottom-up. Remember if we saw a store.
Chris Lattner296185c2009-04-10 16:38:36 +0000251 MachineBasicBlock::iterator I = MBB.end();
252 --I;
253 bool ProcessedBegin, SawStore = false;
254 do {
255 MachineInstr *MI = I; // The instruction to sink.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000256
Chris Lattner296185c2009-04-10 16:38:36 +0000257 // Predecrement I (if it's not begin) so that it isn't invalidated by
258 // sinking.
259 ProcessedBegin = I == MBB.begin();
260 if (!ProcessedBegin)
261 --I;
Dale Johannesenb0812f12010-03-05 00:02:59 +0000262
263 if (MI->isDebugValue())
264 continue;
265
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000266 if (PerformTrivialForwardCoalescing(MI, &MBB))
267 continue;
268
Chris Lattner296185c2009-04-10 16:38:36 +0000269 if (SinkInstruction(MI, SawStore))
270 ++NumSunk, MadeChange = true;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000271
Chris Lattner296185c2009-04-10 16:38:36 +0000272 // If we just processed the first instruction in the block, we're done.
273 } while (!ProcessedBegin);
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000274
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000275 return MadeChange;
276}
277
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000278bool MachineSinking::isWorthBreakingCriticalEdge(MachineInstr *MI,
279 MachineBasicBlock *From,
280 MachineBasicBlock *To) {
281 // FIXME: Need much better heuristics.
282
283 // If the pass has already considered breaking this edge (during this pass
284 // through the function), then let's go ahead and break it. This means
285 // sinking multiple "cheap" instructions into the same block.
286 if (!CEBCandidates.insert(std::make_pair(From, To)))
287 return true;
288
Evan Chengb0cdf8a2010-09-23 06:53:00 +0000289 if (!MI->isCopy() && !MI->getDesc().isAsCheapAsAMove())
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000290 return true;
291
292 // MI is cheap, we probably don't want to break the critical edge for it.
293 // However, if this would allow some definitions of its source operands
294 // to be sunk then it's probably worth it.
295 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
296 const MachineOperand &MO = MI->getOperand(i);
297 if (!MO.isReg()) continue;
298 unsigned Reg = MO.getReg();
299 if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg))
300 continue;
301 if (MRI->hasOneNonDBGUse(Reg))
302 return true;
303 }
304
305 return false;
306}
307
308MachineBasicBlock *MachineSinking::SplitCriticalEdge(MachineInstr *MI,
309 MachineBasicBlock *FromBB,
310 MachineBasicBlock *ToBB,
Evan Cheng7af6dc42010-09-20 19:12:55 +0000311 bool BreakPHIEdge) {
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000312 if (!isWorthBreakingCriticalEdge(MI, FromBB, ToBB))
313 return 0;
314
Evan Cheng4dc301a2010-08-19 17:33:11 +0000315 // Avoid breaking back edge. From == To means backedge for single BB loop.
Evan Cheng44be1a82010-09-20 22:52:00 +0000316 if (!SplitEdges || FromBB == ToBB)
Evan Cheng4dc301a2010-08-19 17:33:11 +0000317 return 0;
318
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000319 // Check for backedges of more "complex" loops.
320 if (LI->getLoopFor(FromBB) == LI->getLoopFor(ToBB) &&
321 LI->isLoopHeader(ToBB))
322 return 0;
323
324 // It's not always legal to break critical edges and sink the computation
325 // to the edge.
326 //
327 // BB#1:
328 // v1024
329 // Beq BB#3
330 // <fallthrough>
331 // BB#2:
332 // ... no uses of v1024
333 // <fallthrough>
334 // BB#3:
335 // ...
336 // = v1024
337 //
338 // If BB#1 -> BB#3 edge is broken and computation of v1024 is inserted:
339 //
340 // BB#1:
341 // ...
342 // Bne BB#2
343 // BB#4:
344 // v1024 =
345 // B BB#3
346 // BB#2:
347 // ... no uses of v1024
348 // <fallthrough>
349 // BB#3:
350 // ...
351 // = v1024
352 //
353 // This is incorrect since v1024 is not computed along the BB#1->BB#2->BB#3
354 // flow. We need to ensure the new basic block where the computation is
355 // sunk to dominates all the uses.
356 // It's only legal to break critical edge and sink the computation to the
357 // new block if all the predecessors of "To", except for "From", are
358 // not dominated by "From". Given SSA property, this means these
359 // predecessors are dominated by "To".
360 //
361 // There is no need to do this check if all the uses are PHI nodes. PHI
362 // sources are only defined on the specific predecessor edges.
Evan Cheng7af6dc42010-09-20 19:12:55 +0000363 if (!BreakPHIEdge) {
Evan Cheng4dc301a2010-08-19 17:33:11 +0000364 for (MachineBasicBlock::pred_iterator PI = ToBB->pred_begin(),
365 E = ToBB->pred_end(); PI != E; ++PI) {
366 if (*PI == FromBB)
367 continue;
368 if (!DT->dominates(ToBB, *PI))
369 return 0;
370 }
Evan Cheng4dc301a2010-08-19 17:33:11 +0000371 }
372
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000373 return FromBB->SplitCriticalEdge(ToBB, this);
Evan Cheng4dc301a2010-08-19 17:33:11 +0000374}
375
Evan Chengb0cdf8a2010-09-23 06:53:00 +0000376static bool AvoidsSinking(MachineInstr *MI, MachineRegisterInfo *MRI) {
377 return MI->isInsertSubreg() || MI->isSubregToReg() || MI->isRegSequence();
378}
379
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000380/// SinkInstruction - Determine whether it is safe to sink the specified machine
381/// instruction out of its current block into a successor.
Chris Lattneraad193a2008-01-12 00:17:41 +0000382bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) {
Evan Chengb0cdf8a2010-09-23 06:53:00 +0000383 // Don't sink insert_subreg, subreg_to_reg, reg_sequence. These are meant to
384 // be close to the source to make it easier to coalesce.
385 if (AvoidsSinking(MI, MRI))
386 return false;
387
Evan Chengb27087f2008-03-13 00:44:09 +0000388 // Check if it's safe to move the instruction.
Evan Chengac1abde2010-03-02 19:03:01 +0000389 if (!MI->isSafeToMove(TII, AA, SawStore))
Chris Lattneraad193a2008-01-12 00:17:41 +0000390 return false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000391
Chris Lattnere430e1c2008-01-05 06:47:58 +0000392 // FIXME: This should include support for sinking instructions within the
393 // block they are currently in to shorten the live ranges. We often get
394 // instructions sunk into the top of a large block, but it would be better to
395 // also sink them down before their first use in the block. This xform has to
396 // be careful not to *increase* register pressure though, e.g. sinking
397 // "x = y + z" down if it kills y and z would increase the live ranges of y
Dan Gohmana5225ad2009-08-05 01:19:01 +0000398 // and z and only shrink the live range of x.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000399
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000400 // Loop over all the operands of the specified instruction. If there is
401 // anything we can't handle, bail out.
402 MachineBasicBlock *ParentBlock = MI->getParent();
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000403
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000404 // SuccToSinkTo - This is the successor to sink this instruction to, once we
405 // decide.
406 MachineBasicBlock *SuccToSinkTo = 0;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000407
Evan Cheng7af6dc42010-09-20 19:12:55 +0000408 bool BreakPHIEdge = false;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000409 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
410 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000411 if (!MO.isReg()) continue; // Ignore non-register operands.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000412
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000413 unsigned Reg = MO.getReg();
414 if (Reg == 0) continue;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000415
Dan Gohman6f0d0242008-02-10 18:45:23 +0000416 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Dan Gohman19778e72009-09-25 22:53:29 +0000417 if (MO.isUse()) {
418 // If the physreg has no defs anywhere, it's just an ambient register
Dan Gohman45094e32009-09-26 02:34:00 +0000419 // and we can freely move its uses. Alternatively, if it's allocatable,
420 // it could get allocated to something with a def during allocation.
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000421 if (!MRI->def_empty(Reg))
Dan Gohman19778e72009-09-25 22:53:29 +0000422 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000423
Dan Gohman45094e32009-09-26 02:34:00 +0000424 if (AllocatableSet.test(Reg))
425 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000426
Dan Gohman19778e72009-09-25 22:53:29 +0000427 // Check for a def among the register's aliases too.
Dan Gohman45094e32009-09-26 02:34:00 +0000428 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
429 unsigned AliasReg = *Alias;
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000430 if (!MRI->def_empty(AliasReg))
Dan Gohman19778e72009-09-25 22:53:29 +0000431 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000432
Dan Gohman45094e32009-09-26 02:34:00 +0000433 if (AllocatableSet.test(AliasReg))
434 return false;
435 }
Bill Wendling730c07e2010-06-25 20:48:10 +0000436 } else if (!MO.isDead()) {
437 // A def that isn't dead. We can't move it.
438 return false;
Dan Gohman19778e72009-09-25 22:53:29 +0000439 }
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000440 } else {
441 // Virtual register uses are always safe to sink.
442 if (MO.isUse()) continue;
Evan Chengb6f54172009-02-07 01:21:47 +0000443
444 // If it's not safe to move defs of the register class, then abort.
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000445 if (!TII->isSafeToMoveRegClassDefs(MRI->getRegClass(Reg)))
Evan Chengb6f54172009-02-07 01:21:47 +0000446 return false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000447
Chris Lattnere430e1c2008-01-05 06:47:58 +0000448 // FIXME: This picks a successor to sink into based on having one
449 // successor that dominates all the uses. However, there are cases where
450 // sinking can happen but where the sink point isn't a successor. For
451 // example:
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000452 //
Chris Lattnere430e1c2008-01-05 06:47:58 +0000453 // x = computation
454 // if () {} else {}
455 // use x
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000456 //
Bill Wendling05c68372010-06-02 23:04:26 +0000457 // the instruction could be sunk over the whole diamond for the
Chris Lattnere430e1c2008-01-05 06:47:58 +0000458 // if/then/else (or loop, etc), allowing it to be sunk into other blocks
459 // after that.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000460
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000461 // Virtual register defs can only be sunk if all their uses are in blocks
462 // dominated by one of the successors.
463 if (SuccToSinkTo) {
464 // If a previous operand picked a block to sink to, then this operand
465 // must be sinkable to the same block.
Evan Chenge5e79462010-08-19 18:33:29 +0000466 bool LocalUse = false;
Evan Cheng23997862010-09-18 06:42:17 +0000467 if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, ParentBlock,
Evan Cheng7af6dc42010-09-20 19:12:55 +0000468 BreakPHIEdge, LocalUse))
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000469 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000470
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000471 continue;
472 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000473
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000474 // Otherwise, we should look at all the successors and decide which one
475 // we should sink to.
476 for (MachineBasicBlock::succ_iterator SI = ParentBlock->succ_begin(),
477 E = ParentBlock->succ_end(); SI != E; ++SI) {
Evan Chenge5e79462010-08-19 18:33:29 +0000478 bool LocalUse = false;
Evan Cheng23997862010-09-18 06:42:17 +0000479 if (AllUsesDominatedByBlock(Reg, *SI, ParentBlock,
Evan Cheng7af6dc42010-09-20 19:12:55 +0000480 BreakPHIEdge, LocalUse)) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000481 SuccToSinkTo = *SI;
482 break;
483 }
Evan Chengc3439ad2010-08-18 23:09:25 +0000484 if (LocalUse)
485 // Def is used locally, it's never safe to move this def.
486 return false;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000487 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000488
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000489 // If we couldn't find a block to sink to, ignore this instruction.
490 if (SuccToSinkTo == 0)
491 return false;
492 }
493 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000494
Chris Lattner9bb459b2008-01-05 01:39:17 +0000495 // If there are no outputs, it must have side-effects.
496 if (SuccToSinkTo == 0)
497 return false;
Evan Chengb5999792009-02-15 08:36:12 +0000498
499 // It's not safe to sink instructions to EH landing pad. Control flow into
500 // landing pad is implicitly defined.
501 if (SuccToSinkTo->isLandingPad())
502 return false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000503
Dan Gohmandfffba62009-10-19 14:56:05 +0000504 // It is not possible to sink an instruction into its own block. This can
Chris Lattner296185c2009-04-10 16:38:36 +0000505 // happen with loops.
506 if (MI->getParent() == SuccToSinkTo)
507 return false;
Bill Wendling869d60d2010-06-03 07:54:20 +0000508
Daniel Dunbard24c9d52010-06-23 00:48:25 +0000509 // If the instruction to move defines a dead physical register which is live
510 // when leaving the basic block, don't move it because it could turn into a
511 // "zombie" define of that preg. E.g., EFLAGS. (<rdar://problem/8030636>)
Bill Wendling730c07e2010-06-25 20:48:10 +0000512 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
513 const MachineOperand &MO = MI->getOperand(I);
514 if (!MO.isReg()) continue;
515 unsigned Reg = MO.getReg();
516 if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
517 if (SuccToSinkTo->isLiveIn(Reg))
Bill Wendling869d60d2010-06-03 07:54:20 +0000518 return false;
Bill Wendling730c07e2010-06-25 20:48:10 +0000519 }
Bill Wendling869d60d2010-06-03 07:54:20 +0000520
Bill Wendling05c68372010-06-02 23:04:26 +0000521 DEBUG(dbgs() << "Sink instr " << *MI << "\tinto block " << *SuccToSinkTo);
522
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000523 // If the block has multiple predecessors, this would introduce computation on
524 // a path that it doesn't already exist. We could split the critical edge,
525 // but for now we just punt.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000526 if (SuccToSinkTo->pred_size() > 1) {
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000527 // We cannot sink a load across a critical edge - there may be stores in
528 // other code paths.
Evan Cheng4dc301a2010-08-19 17:33:11 +0000529 bool TryBreak = false;
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000530 bool store = true;
531 if (!MI->isSafeToMove(TII, AA, store)) {
Evan Chengf942c132010-08-19 23:33:02 +0000532 DEBUG(dbgs() << " *** NOTE: Won't sink load along critical edge.\n");
Evan Cheng4dc301a2010-08-19 17:33:11 +0000533 TryBreak = true;
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000534 }
535
536 // We don't want to sink across a critical edge if we don't dominate the
537 // successor. We could be introducing calculations to new code paths.
Evan Cheng4dc301a2010-08-19 17:33:11 +0000538 if (!TryBreak && !DT->dominates(ParentBlock, SuccToSinkTo)) {
Evan Chengf942c132010-08-19 23:33:02 +0000539 DEBUG(dbgs() << " *** NOTE: Critical edge found\n");
Evan Cheng4dc301a2010-08-19 17:33:11 +0000540 TryBreak = true;
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000541 }
542
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +0000543 // Don't sink instructions into a loop.
Evan Cheng4dc301a2010-08-19 17:33:11 +0000544 if (!TryBreak && LI->isLoopHeader(SuccToSinkTo)) {
Evan Chengf942c132010-08-19 23:33:02 +0000545 DEBUG(dbgs() << " *** NOTE: Loop header found\n");
Evan Cheng4dc301a2010-08-19 17:33:11 +0000546 TryBreak = true;
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +0000547 }
548
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000549 // Otherwise we are OK with sinking along a critical edge.
Evan Cheng4dc301a2010-08-19 17:33:11 +0000550 if (!TryBreak)
551 DEBUG(dbgs() << "Sinking along critical edge.\n");
552 else {
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000553 MachineBasicBlock *NewSucc =
Evan Cheng7af6dc42010-09-20 19:12:55 +0000554 SplitCriticalEdge(MI, ParentBlock, SuccToSinkTo, BreakPHIEdge);
Evan Cheng4dc301a2010-08-19 17:33:11 +0000555 if (!NewSucc) {
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000556 DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
557 "break critical edge\n");
Evan Cheng4dc301a2010-08-19 17:33:11 +0000558 return false;
559 } else {
Evan Chengf942c132010-08-19 23:33:02 +0000560 DEBUG(dbgs() << " *** Splitting critical edge:"
Evan Cheng4dc301a2010-08-19 17:33:11 +0000561 " BB#" << ParentBlock->getNumber()
562 << " -- BB#" << NewSucc->getNumber()
563 << " -- BB#" << SuccToSinkTo->getNumber() << '\n');
Evan Cheng4dc301a2010-08-19 17:33:11 +0000564 SuccToSinkTo = NewSucc;
565 ++NumSplit;
Evan Cheng7af6dc42010-09-20 19:12:55 +0000566 BreakPHIEdge = false;
Evan Cheng4dc301a2010-08-19 17:33:11 +0000567 }
568 }
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000569 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000570
Evan Cheng7af6dc42010-09-20 19:12:55 +0000571 if (BreakPHIEdge) {
572 // BreakPHIEdge is true if all the uses are in the successor MBB being
573 // sunken into and they are all PHI nodes. In this case, machine-sink must
574 // break the critical edge first.
Evan Cheng23997862010-09-18 06:42:17 +0000575 MachineBasicBlock *NewSucc = SplitCriticalEdge(MI, ParentBlock,
Evan Cheng7af6dc42010-09-20 19:12:55 +0000576 SuccToSinkTo, BreakPHIEdge);
Evan Cheng23997862010-09-18 06:42:17 +0000577 if (!NewSucc) {
578 DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
579 "break critical edge\n");
580 return false;
581 }
582
583 DEBUG(dbgs() << " *** Splitting critical edge:"
584 " BB#" << ParentBlock->getNumber()
585 << " -- BB#" << NewSucc->getNumber()
586 << " -- BB#" << SuccToSinkTo->getNumber() << '\n');
587 SuccToSinkTo = NewSucc;
588 ++NumSplit;
589 }
590
Bill Wendling05c68372010-06-02 23:04:26 +0000591 // Determine where to insert into. Skip phi nodes.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000592 MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
Evan Cheng23997862010-09-18 06:42:17 +0000593 while (InsertPos != SuccToSinkTo->end() && InsertPos->isPHI())
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000594 ++InsertPos;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000595
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000596 // Move the instruction.
597 SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
598 ++MachineBasicBlock::iterator(MI));
Dan Gohmane6cd7572010-05-13 20:34:42 +0000599
Bill Wendling05c68372010-06-02 23:04:26 +0000600 // Conservatively, clear any kill flags, since it's possible that they are no
601 // longer correct.
Dan Gohmane6cd7572010-05-13 20:34:42 +0000602 MI->clearKillInfo();
603
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000604 return true;
605}