blob: b9c1eb4b684fe9d7493c35ce244fb614ae0f7497 [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 Andersond13db2c2010-07-21 22:09:45 +000097INITIALIZE_PASS(MachineSinking, "machine-sink",
98 "Machine code sinking", false, false);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000099
100FunctionPass *llvm::createMachineSinkingPass() { return new MachineSinking(); }
101
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000102bool MachineSinking::PerformTrivialForwardCoalescing(MachineInstr *MI,
103 MachineBasicBlock *MBB) {
104 if (!MI->isCopy())
105 return false;
106
107 unsigned SrcReg = MI->getOperand(1).getReg();
108 unsigned DstReg = MI->getOperand(0).getReg();
109 if (!TargetRegisterInfo::isVirtualRegister(SrcReg) ||
110 !TargetRegisterInfo::isVirtualRegister(DstReg) ||
111 !MRI->hasOneNonDBGUse(SrcReg))
112 return false;
113
114 const TargetRegisterClass *SRC = MRI->getRegClass(SrcReg);
115 const TargetRegisterClass *DRC = MRI->getRegClass(DstReg);
116 if (SRC != DRC)
117 return false;
118
119 MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
120 if (DefMI->isCopyLike())
121 return false;
122 DEBUG(dbgs() << "Coalescing: " << *DefMI);
123 DEBUG(dbgs() << "*** to: " << *MI);
124 MRI->replaceRegWith(DstReg, SrcReg);
125 MI->eraseFromParent();
126 ++NumCoalesces;
127 return true;
128}
129
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000130/// AllUsesDominatedByBlock - Return true if all uses of the specified register
Evan Chengc3439ad2010-08-18 23:09:25 +0000131/// occur in blocks dominated by the specified block. If any use is in the
132/// definition block, then return false since it is never legal to move def
133/// after uses.
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000134bool
135MachineSinking::AllUsesDominatedByBlock(unsigned Reg,
136 MachineBasicBlock *MBB,
137 MachineBasicBlock *DefMBB,
Evan Cheng7af6dc42010-09-20 19:12:55 +0000138 bool &BreakPHIEdge,
139 bool &LocalUse) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000140 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
141 "Only makes sense for vregs");
Evan Cheng23997862010-09-18 06:42:17 +0000142
143 if (MRI->use_nodbg_empty(Reg))
144 return true;
145
Dale Johannesenb0812f12010-03-05 00:02:59 +0000146 // Ignoring debug uses is necessary so debug info doesn't affect the code.
147 // This may leave a referencing dbg_value in the original block, before
148 // the definition of the vreg. Dwarf generator handles this although the
149 // user might not get the right info at runtime.
Evan Cheng23997862010-09-18 06:42:17 +0000150
Evan Cheng7af6dc42010-09-20 19:12:55 +0000151 // BreakPHIEdge is true if all the uses are in the successor MBB being sunken
152 // into and they are all PHI nodes. In this case, machine-sink must break
153 // the critical edge first. e.g.
154 //
Evan Cheng23997862010-09-18 06:42:17 +0000155 // BB#1: derived from LLVM BB %bb4.preheader
156 // Predecessors according to CFG: BB#0
157 // ...
158 // %reg16385<def> = DEC64_32r %reg16437, %EFLAGS<imp-def,dead>
159 // ...
160 // JE_4 <BB#37>, %EFLAGS<imp-use>
161 // Successors according to CFG: BB#37 BB#2
162 //
163 // BB#2: derived from LLVM BB %bb.nph
164 // Predecessors according to CFG: BB#0 BB#1
165 // %reg16386<def> = PHI %reg16434, <BB#0>, %reg16385, <BB#1>
Evan Cheng7af6dc42010-09-20 19:12:55 +0000166 BreakPHIEdge = true;
Evan Cheng23997862010-09-18 06:42:17 +0000167 for (MachineRegisterInfo::use_nodbg_iterator
168 I = MRI->use_nodbg_begin(Reg), E = MRI->use_nodbg_end();
169 I != E; ++I) {
170 MachineInstr *UseInst = &*I;
171 MachineBasicBlock *UseBlock = UseInst->getParent();
172 if (!(UseBlock == MBB && UseInst->isPHI() &&
173 UseInst->getOperand(I.getOperandNo()+1).getMBB() == DefMBB)) {
Evan Cheng7af6dc42010-09-20 19:12:55 +0000174 BreakPHIEdge = false;
Evan Cheng23997862010-09-18 06:42:17 +0000175 break;
176 }
177 }
Evan Cheng7af6dc42010-09-20 19:12:55 +0000178 if (BreakPHIEdge)
Evan Cheng23997862010-09-18 06:42:17 +0000179 return true;
180
Bill Wendling05c68372010-06-02 23:04:26 +0000181 for (MachineRegisterInfo::use_nodbg_iterator
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000182 I = MRI->use_nodbg_begin(Reg), E = MRI->use_nodbg_end();
Bill Wendling05c68372010-06-02 23:04:26 +0000183 I != E; ++I) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000184 // Determine the block of the use.
185 MachineInstr *UseInst = &*I;
186 MachineBasicBlock *UseBlock = UseInst->getParent();
Evan Cheng23997862010-09-18 06:42:17 +0000187 if (UseInst->isPHI()) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000188 // PHI nodes use the operand in the predecessor block, not the block with
189 // the PHI.
190 UseBlock = UseInst->getOperand(I.getOperandNo()+1).getMBB();
Evan Chenge5e79462010-08-19 18:33:29 +0000191 } else if (UseBlock == DefMBB) {
192 LocalUse = true;
193 return false;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000194 }
Bill Wendling05c68372010-06-02 23:04:26 +0000195
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000196 // Check that it dominates.
197 if (!DT->dominates(MBB, UseBlock))
198 return false;
199 }
Bill Wendling05c68372010-06-02 23:04:26 +0000200
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000201 return true;
202}
203
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000204bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
David Greenec19a9cd2010-01-05 01:26:00 +0000205 DEBUG(dbgs() << "******** Machine Sinking ********\n");
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000206
Dan Gohman4e9785e2009-10-19 14:52:05 +0000207 const TargetMachine &TM = MF.getTarget();
208 TII = TM.getInstrInfo();
209 TRI = TM.getRegisterInfo();
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000210 MRI = &MF.getRegInfo();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000211 DT = &getAnalysis<MachineDominatorTree>();
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +0000212 LI = &getAnalysis<MachineLoopInfo>();
Dan Gohmana70dca12009-10-09 23:27:56 +0000213 AA = &getAnalysis<AliasAnalysis>();
Dan Gohman4e9785e2009-10-19 14:52:05 +0000214 AllocatableSet = TRI->getAllocatableSet(MF);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000215
216 bool EverMadeChange = false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000217
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000218 while (1) {
219 bool MadeChange = false;
220
221 // Process all basic blocks.
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000222 CEBCandidates.clear();
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000223 for (MachineFunction::iterator I = MF.begin(), E = MF.end();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000224 I != E; ++I)
225 MadeChange |= ProcessBlock(*I);
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000226
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000227 // If this iteration over the code changed anything, keep iterating.
228 if (!MadeChange) break;
229 EverMadeChange = true;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000230 }
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000231 return EverMadeChange;
232}
233
234bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000235 // Can't sink anything out of a block that has less than two successors.
Chris Lattner296185c2009-04-10 16:38:36 +0000236 if (MBB.succ_size() <= 1 || MBB.empty()) return false;
237
Dan Gohmanc4ae94d2010-04-05 19:17:22 +0000238 // Don't bother sinking code out of unreachable blocks. In addition to being
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000239 // unprofitable, it can also lead to infinite looping, because in an
240 // unreachable loop there may be nowhere to stop.
Dan Gohmanc4ae94d2010-04-05 19:17:22 +0000241 if (!DT->isReachableFromEntry(&MBB)) return false;
242
Chris Lattner296185c2009-04-10 16:38:36 +0000243 bool MadeChange = false;
244
Chris Lattneraad193a2008-01-12 00:17:41 +0000245 // Walk the basic block bottom-up. Remember if we saw a store.
Chris Lattner296185c2009-04-10 16:38:36 +0000246 MachineBasicBlock::iterator I = MBB.end();
247 --I;
248 bool ProcessedBegin, SawStore = false;
249 do {
250 MachineInstr *MI = I; // The instruction to sink.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000251
Chris Lattner296185c2009-04-10 16:38:36 +0000252 // Predecrement I (if it's not begin) so that it isn't invalidated by
253 // sinking.
254 ProcessedBegin = I == MBB.begin();
255 if (!ProcessedBegin)
256 --I;
Dale Johannesenb0812f12010-03-05 00:02:59 +0000257
258 if (MI->isDebugValue())
259 continue;
260
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000261 if (PerformTrivialForwardCoalescing(MI, &MBB))
262 continue;
263
Chris Lattner296185c2009-04-10 16:38:36 +0000264 if (SinkInstruction(MI, SawStore))
265 ++NumSunk, MadeChange = true;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000266
Chris Lattner296185c2009-04-10 16:38:36 +0000267 // If we just processed the first instruction in the block, we're done.
268 } while (!ProcessedBegin);
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000269
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000270 return MadeChange;
271}
272
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000273bool MachineSinking::isWorthBreakingCriticalEdge(MachineInstr *MI,
274 MachineBasicBlock *From,
275 MachineBasicBlock *To) {
276 // FIXME: Need much better heuristics.
277
278 // If the pass has already considered breaking this edge (during this pass
279 // through the function), then let's go ahead and break it. This means
280 // sinking multiple "cheap" instructions into the same block.
281 if (!CEBCandidates.insert(std::make_pair(From, To)))
282 return true;
283
Evan Chengb0cdf8a2010-09-23 06:53:00 +0000284 if (!MI->isCopy() && !MI->getDesc().isAsCheapAsAMove())
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000285 return true;
286
287 // MI is cheap, we probably don't want to break the critical edge for it.
288 // However, if this would allow some definitions of its source operands
289 // to be sunk then it's probably worth it.
290 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
291 const MachineOperand &MO = MI->getOperand(i);
292 if (!MO.isReg()) continue;
293 unsigned Reg = MO.getReg();
294 if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg))
295 continue;
296 if (MRI->hasOneNonDBGUse(Reg))
297 return true;
298 }
299
300 return false;
301}
302
303MachineBasicBlock *MachineSinking::SplitCriticalEdge(MachineInstr *MI,
304 MachineBasicBlock *FromBB,
305 MachineBasicBlock *ToBB,
Evan Cheng7af6dc42010-09-20 19:12:55 +0000306 bool BreakPHIEdge) {
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000307 if (!isWorthBreakingCriticalEdge(MI, FromBB, ToBB))
308 return 0;
309
Evan Cheng4dc301a2010-08-19 17:33:11 +0000310 // Avoid breaking back edge. From == To means backedge for single BB loop.
Evan Cheng44be1a82010-09-20 22:52:00 +0000311 if (!SplitEdges || FromBB == ToBB)
Evan Cheng4dc301a2010-08-19 17:33:11 +0000312 return 0;
313
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000314 // Check for backedges of more "complex" loops.
315 if (LI->getLoopFor(FromBB) == LI->getLoopFor(ToBB) &&
316 LI->isLoopHeader(ToBB))
317 return 0;
318
319 // It's not always legal to break critical edges and sink the computation
320 // to the edge.
321 //
322 // BB#1:
323 // v1024
324 // Beq BB#3
325 // <fallthrough>
326 // BB#2:
327 // ... no uses of v1024
328 // <fallthrough>
329 // BB#3:
330 // ...
331 // = v1024
332 //
333 // If BB#1 -> BB#3 edge is broken and computation of v1024 is inserted:
334 //
335 // BB#1:
336 // ...
337 // Bne BB#2
338 // BB#4:
339 // v1024 =
340 // B BB#3
341 // BB#2:
342 // ... no uses of v1024
343 // <fallthrough>
344 // BB#3:
345 // ...
346 // = v1024
347 //
348 // This is incorrect since v1024 is not computed along the BB#1->BB#2->BB#3
349 // flow. We need to ensure the new basic block where the computation is
350 // sunk to dominates all the uses.
351 // It's only legal to break critical edge and sink the computation to the
352 // new block if all the predecessors of "To", except for "From", are
353 // not dominated by "From". Given SSA property, this means these
354 // predecessors are dominated by "To".
355 //
356 // There is no need to do this check if all the uses are PHI nodes. PHI
357 // sources are only defined on the specific predecessor edges.
Evan Cheng7af6dc42010-09-20 19:12:55 +0000358 if (!BreakPHIEdge) {
Evan Cheng4dc301a2010-08-19 17:33:11 +0000359 for (MachineBasicBlock::pred_iterator PI = ToBB->pred_begin(),
360 E = ToBB->pred_end(); PI != E; ++PI) {
361 if (*PI == FromBB)
362 continue;
363 if (!DT->dominates(ToBB, *PI))
364 return 0;
365 }
Evan Cheng4dc301a2010-08-19 17:33:11 +0000366 }
367
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000368 return FromBB->SplitCriticalEdge(ToBB, this);
Evan Cheng4dc301a2010-08-19 17:33:11 +0000369}
370
Evan Chengb0cdf8a2010-09-23 06:53:00 +0000371static bool AvoidsSinking(MachineInstr *MI, MachineRegisterInfo *MRI) {
372 return MI->isInsertSubreg() || MI->isSubregToReg() || MI->isRegSequence();
373}
374
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000375/// SinkInstruction - Determine whether it is safe to sink the specified machine
376/// instruction out of its current block into a successor.
Chris Lattneraad193a2008-01-12 00:17:41 +0000377bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) {
Evan Chengb0cdf8a2010-09-23 06:53:00 +0000378 // Don't sink insert_subreg, subreg_to_reg, reg_sequence. These are meant to
379 // be close to the source to make it easier to coalesce.
380 if (AvoidsSinking(MI, MRI))
381 return false;
382
Evan Chengb27087f2008-03-13 00:44:09 +0000383 // Check if it's safe to move the instruction.
Evan Chengac1abde2010-03-02 19:03:01 +0000384 if (!MI->isSafeToMove(TII, AA, SawStore))
Chris Lattneraad193a2008-01-12 00:17:41 +0000385 return false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000386
Chris Lattnere430e1c2008-01-05 06:47:58 +0000387 // FIXME: This should include support for sinking instructions within the
388 // block they are currently in to shorten the live ranges. We often get
389 // instructions sunk into the top of a large block, but it would be better to
390 // also sink them down before their first use in the block. This xform has to
391 // be careful not to *increase* register pressure though, e.g. sinking
392 // "x = y + z" down if it kills y and z would increase the live ranges of y
Dan Gohmana5225ad2009-08-05 01:19:01 +0000393 // and z and only shrink the live range of x.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000394
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000395 // Loop over all the operands of the specified instruction. If there is
396 // anything we can't handle, bail out.
397 MachineBasicBlock *ParentBlock = MI->getParent();
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000398
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000399 // SuccToSinkTo - This is the successor to sink this instruction to, once we
400 // decide.
401 MachineBasicBlock *SuccToSinkTo = 0;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000402
Evan Cheng7af6dc42010-09-20 19:12:55 +0000403 bool BreakPHIEdge = false;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000404 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
405 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000406 if (!MO.isReg()) continue; // Ignore non-register operands.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000407
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000408 unsigned Reg = MO.getReg();
409 if (Reg == 0) continue;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000410
Dan Gohman6f0d0242008-02-10 18:45:23 +0000411 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Dan Gohman19778e72009-09-25 22:53:29 +0000412 if (MO.isUse()) {
413 // If the physreg has no defs anywhere, it's just an ambient register
Dan Gohman45094e32009-09-26 02:34:00 +0000414 // and we can freely move its uses. Alternatively, if it's allocatable,
415 // it could get allocated to something with a def during allocation.
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000416 if (!MRI->def_empty(Reg))
Dan Gohman19778e72009-09-25 22:53:29 +0000417 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000418
Dan Gohman45094e32009-09-26 02:34:00 +0000419 if (AllocatableSet.test(Reg))
420 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000421
Dan Gohman19778e72009-09-25 22:53:29 +0000422 // Check for a def among the register's aliases too.
Dan Gohman45094e32009-09-26 02:34:00 +0000423 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
424 unsigned AliasReg = *Alias;
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000425 if (!MRI->def_empty(AliasReg))
Dan Gohman19778e72009-09-25 22:53:29 +0000426 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000427
Dan Gohman45094e32009-09-26 02:34:00 +0000428 if (AllocatableSet.test(AliasReg))
429 return false;
430 }
Bill Wendling730c07e2010-06-25 20:48:10 +0000431 } else if (!MO.isDead()) {
432 // A def that isn't dead. We can't move it.
433 return false;
Dan Gohman19778e72009-09-25 22:53:29 +0000434 }
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000435 } else {
436 // Virtual register uses are always safe to sink.
437 if (MO.isUse()) continue;
Evan Chengb6f54172009-02-07 01:21:47 +0000438
439 // If it's not safe to move defs of the register class, then abort.
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000440 if (!TII->isSafeToMoveRegClassDefs(MRI->getRegClass(Reg)))
Evan Chengb6f54172009-02-07 01:21:47 +0000441 return false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000442
Chris Lattnere430e1c2008-01-05 06:47:58 +0000443 // FIXME: This picks a successor to sink into based on having one
444 // successor that dominates all the uses. However, there are cases where
445 // sinking can happen but where the sink point isn't a successor. For
446 // example:
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000447 //
Chris Lattnere430e1c2008-01-05 06:47:58 +0000448 // x = computation
449 // if () {} else {}
450 // use x
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000451 //
Bill Wendling05c68372010-06-02 23:04:26 +0000452 // the instruction could be sunk over the whole diamond for the
Chris Lattnere430e1c2008-01-05 06:47:58 +0000453 // if/then/else (or loop, etc), allowing it to be sunk into other blocks
454 // after that.
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000455
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000456 // Virtual register defs can only be sunk if all their uses are in blocks
457 // dominated by one of the successors.
458 if (SuccToSinkTo) {
459 // If a previous operand picked a block to sink to, then this operand
460 // must be sinkable to the same block.
Evan Chenge5e79462010-08-19 18:33:29 +0000461 bool LocalUse = false;
Evan Cheng23997862010-09-18 06:42:17 +0000462 if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, ParentBlock,
Evan Cheng7af6dc42010-09-20 19:12:55 +0000463 BreakPHIEdge, LocalUse))
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000464 return false;
Bill Wendling05c68372010-06-02 23:04:26 +0000465
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000466 continue;
467 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000468
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000469 // Otherwise, we should look at all the successors and decide which one
470 // we should sink to.
471 for (MachineBasicBlock::succ_iterator SI = ParentBlock->succ_begin(),
472 E = ParentBlock->succ_end(); SI != E; ++SI) {
Evan Chenge5e79462010-08-19 18:33:29 +0000473 bool LocalUse = false;
Evan Cheng23997862010-09-18 06:42:17 +0000474 if (AllUsesDominatedByBlock(Reg, *SI, ParentBlock,
Evan Cheng7af6dc42010-09-20 19:12:55 +0000475 BreakPHIEdge, LocalUse)) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000476 SuccToSinkTo = *SI;
477 break;
478 }
Evan Chengc3439ad2010-08-18 23:09:25 +0000479 if (LocalUse)
480 // Def is used locally, it's never safe to move this def.
481 return false;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000482 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000483
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000484 // If we couldn't find a block to sink to, ignore this instruction.
485 if (SuccToSinkTo == 0)
486 return false;
487 }
488 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000489
Chris Lattner9bb459b2008-01-05 01:39:17 +0000490 // If there are no outputs, it must have side-effects.
491 if (SuccToSinkTo == 0)
492 return false;
Evan Chengb5999792009-02-15 08:36:12 +0000493
494 // It's not safe to sink instructions to EH landing pad. Control flow into
495 // landing pad is implicitly defined.
496 if (SuccToSinkTo->isLandingPad())
497 return false;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000498
Dan Gohmandfffba62009-10-19 14:56:05 +0000499 // It is not possible to sink an instruction into its own block. This can
Chris Lattner296185c2009-04-10 16:38:36 +0000500 // happen with loops.
501 if (MI->getParent() == SuccToSinkTo)
502 return false;
Bill Wendling869d60d2010-06-03 07:54:20 +0000503
Daniel Dunbard24c9d52010-06-23 00:48:25 +0000504 // If the instruction to move defines a dead physical register which is live
505 // when leaving the basic block, don't move it because it could turn into a
506 // "zombie" define of that preg. E.g., EFLAGS. (<rdar://problem/8030636>)
Bill Wendling730c07e2010-06-25 20:48:10 +0000507 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
508 const MachineOperand &MO = MI->getOperand(I);
509 if (!MO.isReg()) continue;
510 unsigned Reg = MO.getReg();
511 if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
512 if (SuccToSinkTo->isLiveIn(Reg))
Bill Wendling869d60d2010-06-03 07:54:20 +0000513 return false;
Bill Wendling730c07e2010-06-25 20:48:10 +0000514 }
Bill Wendling869d60d2010-06-03 07:54:20 +0000515
Bill Wendling05c68372010-06-02 23:04:26 +0000516 DEBUG(dbgs() << "Sink instr " << *MI << "\tinto block " << *SuccToSinkTo);
517
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000518 // If the block has multiple predecessors, this would introduce computation on
519 // a path that it doesn't already exist. We could split the critical edge,
520 // but for now we just punt.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000521 if (SuccToSinkTo->pred_size() > 1) {
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000522 // We cannot sink a load across a critical edge - there may be stores in
523 // other code paths.
Evan Cheng4dc301a2010-08-19 17:33:11 +0000524 bool TryBreak = false;
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000525 bool store = true;
526 if (!MI->isSafeToMove(TII, AA, store)) {
Evan Chengf942c132010-08-19 23:33:02 +0000527 DEBUG(dbgs() << " *** NOTE: Won't sink load along critical edge.\n");
Evan Cheng4dc301a2010-08-19 17:33:11 +0000528 TryBreak = true;
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000529 }
530
531 // We don't want to sink across a critical edge if we don't dominate the
532 // successor. We could be introducing calculations to new code paths.
Evan Cheng4dc301a2010-08-19 17:33:11 +0000533 if (!TryBreak && !DT->dominates(ParentBlock, SuccToSinkTo)) {
Evan Chengf942c132010-08-19 23:33:02 +0000534 DEBUG(dbgs() << " *** NOTE: Critical edge found\n");
Evan Cheng4dc301a2010-08-19 17:33:11 +0000535 TryBreak = true;
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000536 }
537
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +0000538 // Don't sink instructions into a loop.
Evan Cheng4dc301a2010-08-19 17:33:11 +0000539 if (!TryBreak && LI->isLoopHeader(SuccToSinkTo)) {
Evan Chengf942c132010-08-19 23:33:02 +0000540 DEBUG(dbgs() << " *** NOTE: Loop header found\n");
Evan Cheng4dc301a2010-08-19 17:33:11 +0000541 TryBreak = true;
Jakob Stoklund Olesen626f3d72010-04-15 23:41:02 +0000542 }
543
Jakob Stoklund Olesen8d171602010-04-13 19:06:14 +0000544 // Otherwise we are OK with sinking along a critical edge.
Evan Cheng4dc301a2010-08-19 17:33:11 +0000545 if (!TryBreak)
546 DEBUG(dbgs() << "Sinking along critical edge.\n");
547 else {
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000548 MachineBasicBlock *NewSucc =
Evan Cheng7af6dc42010-09-20 19:12:55 +0000549 SplitCriticalEdge(MI, ParentBlock, SuccToSinkTo, BreakPHIEdge);
Evan Cheng4dc301a2010-08-19 17:33:11 +0000550 if (!NewSucc) {
Evan Cheng6edb0ea2010-09-17 22:28:18 +0000551 DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
552 "break critical edge\n");
Evan Cheng4dc301a2010-08-19 17:33:11 +0000553 return false;
554 } else {
Evan Chengf942c132010-08-19 23:33:02 +0000555 DEBUG(dbgs() << " *** Splitting critical edge:"
Evan Cheng4dc301a2010-08-19 17:33:11 +0000556 " BB#" << ParentBlock->getNumber()
557 << " -- BB#" << NewSucc->getNumber()
558 << " -- BB#" << SuccToSinkTo->getNumber() << '\n');
Evan Cheng4dc301a2010-08-19 17:33:11 +0000559 SuccToSinkTo = NewSucc;
560 ++NumSplit;
Evan Cheng7af6dc42010-09-20 19:12:55 +0000561 BreakPHIEdge = false;
Evan Cheng4dc301a2010-08-19 17:33:11 +0000562 }
563 }
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000564 }
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000565
Evan Cheng7af6dc42010-09-20 19:12:55 +0000566 if (BreakPHIEdge) {
567 // BreakPHIEdge is true if all the uses are in the successor MBB being
568 // sunken into and they are all PHI nodes. In this case, machine-sink must
569 // break the critical edge first.
Evan Cheng23997862010-09-18 06:42:17 +0000570 MachineBasicBlock *NewSucc = SplitCriticalEdge(MI, ParentBlock,
Evan Cheng7af6dc42010-09-20 19:12:55 +0000571 SuccToSinkTo, BreakPHIEdge);
Evan Cheng23997862010-09-18 06:42:17 +0000572 if (!NewSucc) {
573 DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
574 "break critical edge\n");
575 return false;
576 }
577
578 DEBUG(dbgs() << " *** Splitting critical edge:"
579 " BB#" << ParentBlock->getNumber()
580 << " -- BB#" << NewSucc->getNumber()
581 << " -- BB#" << SuccToSinkTo->getNumber() << '\n');
582 SuccToSinkTo = NewSucc;
583 ++NumSplit;
584 }
585
Bill Wendling05c68372010-06-02 23:04:26 +0000586 // Determine where to insert into. Skip phi nodes.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000587 MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
Evan Cheng23997862010-09-18 06:42:17 +0000588 while (InsertPos != SuccToSinkTo->end() && InsertPos->isPHI())
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000589 ++InsertPos;
Jim Grosbach6ee358b2010-06-03 23:49:57 +0000590
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000591 // Move the instruction.
592 SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
593 ++MachineBasicBlock::iterator(MI));
Dan Gohmane6cd7572010-05-13 20:34:42 +0000594
Bill Wendling05c68372010-06-02 23:04:26 +0000595 // Conservatively, clear any kill flags, since it's possible that they are no
596 // longer correct.
Dan Gohmane6cd7572010-05-13 20:34:42 +0000597 MI->clearKillInfo();
598
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000599 return true;
600}