blob: 9823e650531621d251c556ea3664ff00ed92df46 [file] [log] [blame]
Chris Lattnerf3edc092008-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 Wendling7ee730e2010-06-02 23:04:26 +000010// This pass moves instructions into successor blocks when possible, so that
Dan Gohman5d79a2c2009-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 Lattnerf3edc092008-01-04 07:36:53 +000016//
17//===----------------------------------------------------------------------===//
18
Chris Lattnerf3edc092008-01-04 07:36:53 +000019#include "llvm/CodeGen/Passes.h"
Quentin Colombet5cded892014-08-11 23:52:01 +000020#include "llvm/ADT/SetVector.h"
Evan Chenge53ab6d2010-09-17 22:28:18 +000021#include "llvm/ADT/SmallSet.h"
Chris Lattnerf3edc092008-01-04 07:36:53 +000022#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/Analysis/AliasAnalysis.h"
Bruno Cardoso Lopesd04f7592014-09-25 23:14:26 +000024#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/CodeGen/MachineDominators.h"
26#include "llvm/CodeGen/MachineLoopInfo.h"
27#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengae9939c2010-08-19 17:33:11 +000028#include "llvm/Support/CommandLine.h"
Chris Lattnerf3edc092008-01-04 07:36:53 +000029#include "llvm/Support/Debug.h"
Bill Wendling63aa0002009-08-22 20:26:23 +000030#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Target/TargetInstrInfo.h"
32#include "llvm/Target/TargetMachine.h"
33#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000034#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattnerf3edc092008-01-04 07:36:53 +000035using namespace llvm;
36
Chandler Carruth1b9dde02014-04-22 02:02:50 +000037#define DEBUG_TYPE "machine-sink"
38
Andrew Trick9e761992012-02-08 21:22:43 +000039static cl::opt<bool>
Evan Chengae9939c2010-08-19 17:33:11 +000040SplitEdges("machine-sink-split",
41 cl::desc("Split critical edges during machine sinking"),
Evan Chengf3e9a482010-09-20 22:52:00 +000042 cl::init(true), cl::Hidden);
Evan Chengae9939c2010-08-19 17:33:11 +000043
Bruno Cardoso Lopesd04f7592014-09-25 23:14:26 +000044static cl::opt<bool>
45UseBlockFreqInfo("machine-sink-bfi",
46 cl::desc("Use block frequency info to find successors to sink"),
47 cl::init(true), cl::Hidden);
48
49
Evan Chenge53ab6d2010-09-17 22:28:18 +000050STATISTIC(NumSunk, "Number of machine instructions sunk");
51STATISTIC(NumSplit, "Number of critical edges split");
52STATISTIC(NumCoalesces, "Number of copies coalesced");
Chris Lattnerf3edc092008-01-04 07:36:53 +000053
54namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000055 class MachineSinking : public MachineFunctionPass {
Chris Lattnerf3edc092008-01-04 07:36:53 +000056 const TargetInstrInfo *TII;
Dan Gohmana3176872009-09-25 22:53:29 +000057 const TargetRegisterInfo *TRI;
Jingyue Wufd47fb92014-10-01 15:22:13 +000058 MachineRegisterInfo *MRI; // Machine register information
59 MachineDominatorTree *DT; // Machine dominator tree
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +000060 MachineLoopInfo *LI;
Bruno Cardoso Lopesd04f7592014-09-25 23:14:26 +000061 const MachineBlockFrequencyInfo *MBFI;
Dan Gohman87b02d52009-10-09 23:27:56 +000062 AliasAnalysis *AA;
Chris Lattnerf3edc092008-01-04 07:36:53 +000063
Evan Chenge53ab6d2010-09-17 22:28:18 +000064 // Remember which edges have been considered for breaking.
65 SmallSet<std::pair<MachineBasicBlock*,MachineBasicBlock*>, 8>
66 CEBCandidates;
Quentin Colombet5cded892014-08-11 23:52:01 +000067 // Remember which edges we are about to split.
68 // This is different from CEBCandidates since those edges
69 // will be split.
70 SetVector<std::pair<MachineBasicBlock*,MachineBasicBlock*> > ToSplit;
Evan Chenge53ab6d2010-09-17 22:28:18 +000071
Chris Lattnerf3edc092008-01-04 07:36:53 +000072 public:
73 static char ID; // Pass identification
Owen Anderson6c18d1a2010-10-19 17:21:58 +000074 MachineSinking() : MachineFunctionPass(ID) {
75 initializeMachineSinkingPass(*PassRegistry::getPassRegistry());
76 }
Jim Grosbach01edd682010-06-03 23:49:57 +000077
Craig Topper4584cd52014-03-07 09:26:03 +000078 bool runOnMachineFunction(MachineFunction &MF) override;
Jim Grosbach01edd682010-06-03 23:49:57 +000079
Craig Topper4584cd52014-03-07 09:26:03 +000080 void getAnalysisUsage(AnalysisUsage &AU) const override {
Dan Gohman04023152009-07-31 23:37:33 +000081 AU.setPreservesCFG();
Chris Lattnerf3edc092008-01-04 07:36:53 +000082 MachineFunctionPass::getAnalysisUsage(AU);
Dan Gohman87b02d52009-10-09 23:27:56 +000083 AU.addRequired<AliasAnalysis>();
Chris Lattnerf3edc092008-01-04 07:36:53 +000084 AU.addRequired<MachineDominatorTree>();
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +000085 AU.addRequired<MachineLoopInfo>();
Chris Lattnerf3edc092008-01-04 07:36:53 +000086 AU.addPreserved<MachineDominatorTree>();
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +000087 AU.addPreserved<MachineLoopInfo>();
Bruno Cardoso Lopesd04f7592014-09-25 23:14:26 +000088 if (UseBlockFreqInfo)
89 AU.addRequired<MachineBlockFrequencyInfo>();
Chris Lattnerf3edc092008-01-04 07:36:53 +000090 }
Evan Chenge53ab6d2010-09-17 22:28:18 +000091
Craig Topper4584cd52014-03-07 09:26:03 +000092 void releaseMemory() override {
Evan Chenge53ab6d2010-09-17 22:28:18 +000093 CEBCandidates.clear();
94 }
95
Chris Lattnerf3edc092008-01-04 07:36:53 +000096 private:
97 bool ProcessBlock(MachineBasicBlock &MBB);
Evan Chenge53ab6d2010-09-17 22:28:18 +000098 bool isWorthBreakingCriticalEdge(MachineInstr *MI,
99 MachineBasicBlock *From,
100 MachineBasicBlock *To);
Quentin Colombet5cded892014-08-11 23:52:01 +0000101 /// \brief Postpone the splitting of the given critical
102 /// edge (\p From, \p To).
103 ///
104 /// We do not split the edges on the fly. Indeed, this invalidates
105 /// the dominance information and thus triggers a lot of updates
106 /// of that information underneath.
107 /// Instead, we postpone all the splits after each iteration of
108 /// the main loop. That way, the information is at least valid
109 /// for the lifetime of an iteration.
110 ///
111 /// \return True if the edge is marked as toSplit, false otherwise.
112 /// False can be retruned if, for instance, this is not profitable.
113 bool PostponeSplitCriticalEdge(MachineInstr *MI,
114 MachineBasicBlock *From,
115 MachineBasicBlock *To,
116 bool BreakPHIEdge);
Chris Lattner08af5a92008-01-12 00:17:41 +0000117 bool SinkInstruction(MachineInstr *MI, bool &SawStore);
Evan Cheng25b60682010-08-18 23:09:25 +0000118 bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB,
Evan Chenge53ab6d2010-09-17 22:28:18 +0000119 MachineBasicBlock *DefMBB,
Evan Cheng2031b762010-09-20 19:12:55 +0000120 bool &BreakPHIEdge, bool &LocalUse) const;
Devang Patelc2686882011-12-14 23:20:38 +0000121 MachineBasicBlock *FindSuccToSinkTo(MachineInstr *MI, MachineBasicBlock *MBB,
122 bool &BreakPHIEdge);
Andrew Trick9e761992012-02-08 21:22:43 +0000123 bool isProfitableToSinkTo(unsigned Reg, MachineInstr *MI,
Devang Patelc2686882011-12-14 23:20:38 +0000124 MachineBasicBlock *MBB,
125 MachineBasicBlock *SuccToSinkTo);
Devang Patelb94c9a42011-12-08 21:48:01 +0000126
Evan Chenge53ab6d2010-09-17 22:28:18 +0000127 bool PerformTrivialForwardCoalescing(MachineInstr *MI,
128 MachineBasicBlock *MBB);
Chris Lattnerf3edc092008-01-04 07:36:53 +0000129 };
Chris Lattnerf3edc092008-01-04 07:36:53 +0000130} // end anonymous namespace
Jim Grosbach01edd682010-06-03 23:49:57 +0000131
Dan Gohmand78c4002008-05-13 00:00:25 +0000132char MachineSinking::ID = 0;
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000133char &llvm::MachineSinkingID = MachineSinking::ID;
Owen Anderson8ac477f2010-10-12 19:48:12 +0000134INITIALIZE_PASS_BEGIN(MachineSinking, "machine-sink",
135 "Machine code sinking", false, false)
136INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
137INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
138INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
139INITIALIZE_PASS_END(MachineSinking, "machine-sink",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000140 "Machine code sinking", false, false)
Chris Lattnerf3edc092008-01-04 07:36:53 +0000141
Evan Chenge53ab6d2010-09-17 22:28:18 +0000142bool MachineSinking::PerformTrivialForwardCoalescing(MachineInstr *MI,
143 MachineBasicBlock *MBB) {
144 if (!MI->isCopy())
145 return false;
146
147 unsigned SrcReg = MI->getOperand(1).getReg();
148 unsigned DstReg = MI->getOperand(0).getReg();
149 if (!TargetRegisterInfo::isVirtualRegister(SrcReg) ||
150 !TargetRegisterInfo::isVirtualRegister(DstReg) ||
151 !MRI->hasOneNonDBGUse(SrcReg))
152 return false;
153
154 const TargetRegisterClass *SRC = MRI->getRegClass(SrcReg);
155 const TargetRegisterClass *DRC = MRI->getRegClass(DstReg);
156 if (SRC != DRC)
157 return false;
158
159 MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
160 if (DefMI->isCopyLike())
161 return false;
162 DEBUG(dbgs() << "Coalescing: " << *DefMI);
163 DEBUG(dbgs() << "*** to: " << *MI);
164 MRI->replaceRegWith(DstReg, SrcReg);
165 MI->eraseFromParent();
Patrik Hagglund57d315b2014-09-09 07:47:00 +0000166
167 // Conservatively, clear any kill flags, since it's possible that they are no
168 // longer correct.
169 MRI->clearKillFlags(SrcReg);
170
Evan Chenge53ab6d2010-09-17 22:28:18 +0000171 ++NumCoalesces;
172 return true;
173}
174
Chris Lattnerf3edc092008-01-04 07:36:53 +0000175/// AllUsesDominatedByBlock - Return true if all uses of the specified register
Evan Cheng25b60682010-08-18 23:09:25 +0000176/// occur in blocks dominated by the specified block. If any use is in the
177/// definition block, then return false since it is never legal to move def
178/// after uses.
Evan Chenge53ab6d2010-09-17 22:28:18 +0000179bool
180MachineSinking::AllUsesDominatedByBlock(unsigned Reg,
181 MachineBasicBlock *MBB,
182 MachineBasicBlock *DefMBB,
Evan Cheng2031b762010-09-20 19:12:55 +0000183 bool &BreakPHIEdge,
184 bool &LocalUse) const {
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000185 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
186 "Only makes sense for vregs");
Evan Chengb339f3d2010-09-18 06:42:17 +0000187
Devang Patel706574a2011-12-09 01:25:04 +0000188 // Ignore debug uses because debug info doesn't affect the code.
Evan Chengb339f3d2010-09-18 06:42:17 +0000189 if (MRI->use_nodbg_empty(Reg))
190 return true;
191
Evan Cheng2031b762010-09-20 19:12:55 +0000192 // BreakPHIEdge is true if all the uses are in the successor MBB being sunken
193 // into and they are all PHI nodes. In this case, machine-sink must break
194 // the critical edge first. e.g.
195 //
Evan Chengb339f3d2010-09-18 06:42:17 +0000196 // BB#1: derived from LLVM BB %bb4.preheader
197 // Predecessors according to CFG: BB#0
198 // ...
199 // %reg16385<def> = DEC64_32r %reg16437, %EFLAGS<imp-def,dead>
200 // ...
201 // JE_4 <BB#37>, %EFLAGS<imp-use>
202 // Successors according to CFG: BB#37 BB#2
203 //
204 // BB#2: derived from LLVM BB %bb.nph
205 // Predecessors according to CFG: BB#0 BB#1
206 // %reg16386<def> = PHI %reg16434, <BB#0>, %reg16385, <BB#1>
Evan Cheng2031b762010-09-20 19:12:55 +0000207 BreakPHIEdge = true;
Owen Andersonb36376e2014-03-17 19:36:09 +0000208 for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
209 MachineInstr *UseInst = MO.getParent();
210 unsigned OpNo = &MO - &UseInst->getOperand(0);
Evan Chengb339f3d2010-09-18 06:42:17 +0000211 MachineBasicBlock *UseBlock = UseInst->getParent();
212 if (!(UseBlock == MBB && UseInst->isPHI() &&
Owen Andersonb36376e2014-03-17 19:36:09 +0000213 UseInst->getOperand(OpNo+1).getMBB() == DefMBB)) {
Evan Cheng2031b762010-09-20 19:12:55 +0000214 BreakPHIEdge = false;
Evan Chengb339f3d2010-09-18 06:42:17 +0000215 break;
216 }
217 }
Evan Cheng2031b762010-09-20 19:12:55 +0000218 if (BreakPHIEdge)
Evan Chengb339f3d2010-09-18 06:42:17 +0000219 return true;
220
Owen Andersonb36376e2014-03-17 19:36:09 +0000221 for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
Chris Lattnerf3edc092008-01-04 07:36:53 +0000222 // Determine the block of the use.
Owen Andersonb36376e2014-03-17 19:36:09 +0000223 MachineInstr *UseInst = MO.getParent();
224 unsigned OpNo = &MO - &UseInst->getOperand(0);
Chris Lattnerf3edc092008-01-04 07:36:53 +0000225 MachineBasicBlock *UseBlock = UseInst->getParent();
Evan Chengb339f3d2010-09-18 06:42:17 +0000226 if (UseInst->isPHI()) {
Chris Lattnerf3edc092008-01-04 07:36:53 +0000227 // PHI nodes use the operand in the predecessor block, not the block with
228 // the PHI.
Owen Andersonb36376e2014-03-17 19:36:09 +0000229 UseBlock = UseInst->getOperand(OpNo+1).getMBB();
Evan Cheng361b9be2010-08-19 18:33:29 +0000230 } else if (UseBlock == DefMBB) {
231 LocalUse = true;
232 return false;
Chris Lattnerf3edc092008-01-04 07:36:53 +0000233 }
Bill Wendling7ee730e2010-06-02 23:04:26 +0000234
Chris Lattnerf3edc092008-01-04 07:36:53 +0000235 // Check that it dominates.
236 if (!DT->dominates(MBB, UseBlock))
237 return false;
238 }
Bill Wendling7ee730e2010-06-02 23:04:26 +0000239
Chris Lattnerf3edc092008-01-04 07:36:53 +0000240 return true;
241}
242
Chris Lattnerf3edc092008-01-04 07:36:53 +0000243bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
Paul Robinson7c99ec52014-03-31 17:43:35 +0000244 if (skipOptnoneFunction(*MF.getFunction()))
245 return false;
246
David Greene4b7aa242010-01-05 01:26:00 +0000247 DEBUG(dbgs() << "******** Machine Sinking ********\n");
Jim Grosbach01edd682010-06-03 23:49:57 +0000248
Dan Gohman20a327b2009-10-19 14:52:05 +0000249 const TargetMachine &TM = MF.getTarget();
Eric Christopherd9134482014-08-04 21:25:23 +0000250 TII = TM.getSubtargetImpl()->getInstrInfo();
251 TRI = TM.getSubtargetImpl()->getRegisterInfo();
Evan Chenge53ab6d2010-09-17 22:28:18 +0000252 MRI = &MF.getRegInfo();
Chris Lattnerf3edc092008-01-04 07:36:53 +0000253 DT = &getAnalysis<MachineDominatorTree>();
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +0000254 LI = &getAnalysis<MachineLoopInfo>();
Bruno Cardoso Lopesd04f7592014-09-25 23:14:26 +0000255 MBFI = UseBlockFreqInfo ? &getAnalysis<MachineBlockFrequencyInfo>() : nullptr;
Dan Gohman87b02d52009-10-09 23:27:56 +0000256 AA = &getAnalysis<AliasAnalysis>();
Chris Lattnerf3edc092008-01-04 07:36:53 +0000257
258 bool EverMadeChange = false;
Jim Grosbach01edd682010-06-03 23:49:57 +0000259
Chris Lattnerf3edc092008-01-04 07:36:53 +0000260 while (1) {
261 bool MadeChange = false;
262
263 // Process all basic blocks.
Evan Chenge53ab6d2010-09-17 22:28:18 +0000264 CEBCandidates.clear();
Quentin Colombet5cded892014-08-11 23:52:01 +0000265 ToSplit.clear();
Jim Grosbach01edd682010-06-03 23:49:57 +0000266 for (MachineFunction::iterator I = MF.begin(), E = MF.end();
Chris Lattnerf3edc092008-01-04 07:36:53 +0000267 I != E; ++I)
268 MadeChange |= ProcessBlock(*I);
Jim Grosbach01edd682010-06-03 23:49:57 +0000269
Quentin Colombet5cded892014-08-11 23:52:01 +0000270 // If we have anything we marked as toSplit, split it now.
271 for (auto &Pair : ToSplit) {
272 auto NewSucc = Pair.first->SplitCriticalEdge(Pair.second, this);
273 if (NewSucc != nullptr) {
274 DEBUG(dbgs() << " *** Splitting critical edge:"
275 " BB#" << Pair.first->getNumber()
276 << " -- BB#" << NewSucc->getNumber()
277 << " -- BB#" << Pair.second->getNumber() << '\n');
278 MadeChange = true;
279 ++NumSplit;
280 } else
281 DEBUG(dbgs() << " *** Not legal to break critical edge\n");
282 }
Chris Lattnerf3edc092008-01-04 07:36:53 +0000283 // If this iteration over the code changed anything, keep iterating.
284 if (!MadeChange) break;
285 EverMadeChange = true;
Jim Grosbach01edd682010-06-03 23:49:57 +0000286 }
Chris Lattnerf3edc092008-01-04 07:36:53 +0000287 return EverMadeChange;
288}
289
290bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
Chris Lattnerf3edc092008-01-04 07:36:53 +0000291 // Can't sink anything out of a block that has less than two successors.
Chris Lattner30c3de62009-04-10 16:38:36 +0000292 if (MBB.succ_size() <= 1 || MBB.empty()) return false;
293
Dan Gohman918a90a2010-04-05 19:17:22 +0000294 // Don't bother sinking code out of unreachable blocks. In addition to being
Jim Grosbach01edd682010-06-03 23:49:57 +0000295 // unprofitable, it can also lead to infinite looping, because in an
296 // unreachable loop there may be nowhere to stop.
Dan Gohman918a90a2010-04-05 19:17:22 +0000297 if (!DT->isReachableFromEntry(&MBB)) return false;
298
Chris Lattner30c3de62009-04-10 16:38:36 +0000299 bool MadeChange = false;
300
Chris Lattner08af5a92008-01-12 00:17:41 +0000301 // Walk the basic block bottom-up. Remember if we saw a store.
Chris Lattner30c3de62009-04-10 16:38:36 +0000302 MachineBasicBlock::iterator I = MBB.end();
303 --I;
304 bool ProcessedBegin, SawStore = false;
305 do {
306 MachineInstr *MI = I; // The instruction to sink.
Jim Grosbach01edd682010-06-03 23:49:57 +0000307
Chris Lattner30c3de62009-04-10 16:38:36 +0000308 // Predecrement I (if it's not begin) so that it isn't invalidated by
309 // sinking.
310 ProcessedBegin = I == MBB.begin();
311 if (!ProcessedBegin)
312 --I;
Dale Johannesen2061c842010-03-05 00:02:59 +0000313
314 if (MI->isDebugValue())
315 continue;
316
Evan Chengfe917ef2011-04-11 18:47:20 +0000317 bool Joined = PerformTrivialForwardCoalescing(MI, &MBB);
318 if (Joined) {
319 MadeChange = true;
Evan Chenge53ab6d2010-09-17 22:28:18 +0000320 continue;
Evan Chengfe917ef2011-04-11 18:47:20 +0000321 }
Evan Chenge53ab6d2010-09-17 22:28:18 +0000322
Chris Lattner30c3de62009-04-10 16:38:36 +0000323 if (SinkInstruction(MI, SawStore))
324 ++NumSunk, MadeChange = true;
Jim Grosbach01edd682010-06-03 23:49:57 +0000325
Chris Lattner30c3de62009-04-10 16:38:36 +0000326 // If we just processed the first instruction in the block, we're done.
327 } while (!ProcessedBegin);
Jim Grosbach01edd682010-06-03 23:49:57 +0000328
Chris Lattnerf3edc092008-01-04 07:36:53 +0000329 return MadeChange;
330}
331
Evan Chenge53ab6d2010-09-17 22:28:18 +0000332bool MachineSinking::isWorthBreakingCriticalEdge(MachineInstr *MI,
333 MachineBasicBlock *From,
334 MachineBasicBlock *To) {
335 // FIXME: Need much better heuristics.
336
337 // If the pass has already considered breaking this edge (during this pass
338 // through the function), then let's go ahead and break it. This means
339 // sinking multiple "cheap" instructions into the same block.
340 if (!CEBCandidates.insert(std::make_pair(From, To)))
341 return true;
342
Jiangning Liuc3053122014-07-29 01:55:19 +0000343 if (!MI->isCopy() && !TII->isAsCheapAsAMove(MI))
Evan Chenge53ab6d2010-09-17 22:28:18 +0000344 return true;
345
346 // MI is cheap, we probably don't want to break the critical edge for it.
347 // However, if this would allow some definitions of its source operands
348 // to be sunk then it's probably worth it.
349 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
350 const MachineOperand &MO = MI->getOperand(i);
Will Dietz5cb7f4e2013-10-14 16:57:17 +0000351 if (!MO.isReg() || !MO.isUse())
Evan Chenge53ab6d2010-09-17 22:28:18 +0000352 continue;
Will Dietz5cb7f4e2013-10-14 16:57:17 +0000353 unsigned Reg = MO.getReg();
354 if (Reg == 0)
355 continue;
356
357 // We don't move live definitions of physical registers,
358 // so sinking their uses won't enable any opportunities.
359 if (TargetRegisterInfo::isPhysicalRegister(Reg))
360 continue;
361
362 // If this instruction is the only user of a virtual register,
363 // check if breaking the edge will enable sinking
364 // both this instruction and the defining instruction.
365 if (MRI->hasOneNonDBGUse(Reg)) {
366 // If the definition resides in same MBB,
367 // claim it's likely we can sink these together.
368 // If definition resides elsewhere, we aren't
369 // blocking it from being sunk so don't break the edge.
370 MachineInstr *DefMI = MRI->getVRegDef(Reg);
371 if (DefMI->getParent() == MI->getParent())
372 return true;
373 }
Evan Chenge53ab6d2010-09-17 22:28:18 +0000374 }
375
376 return false;
377}
378
Quentin Colombet5cded892014-08-11 23:52:01 +0000379bool MachineSinking::PostponeSplitCriticalEdge(MachineInstr *MI,
380 MachineBasicBlock *FromBB,
381 MachineBasicBlock *ToBB,
382 bool BreakPHIEdge) {
Evan Chenge53ab6d2010-09-17 22:28:18 +0000383 if (!isWorthBreakingCriticalEdge(MI, FromBB, ToBB))
Quentin Colombet5cded892014-08-11 23:52:01 +0000384 return false;
Evan Chenge53ab6d2010-09-17 22:28:18 +0000385
Evan Chengae9939c2010-08-19 17:33:11 +0000386 // Avoid breaking back edge. From == To means backedge for single BB loop.
Evan Chengf3e9a482010-09-20 22:52:00 +0000387 if (!SplitEdges || FromBB == ToBB)
Quentin Colombet5cded892014-08-11 23:52:01 +0000388 return false;
Evan Chengae9939c2010-08-19 17:33:11 +0000389
Evan Chenge53ab6d2010-09-17 22:28:18 +0000390 // Check for backedges of more "complex" loops.
391 if (LI->getLoopFor(FromBB) == LI->getLoopFor(ToBB) &&
392 LI->isLoopHeader(ToBB))
Quentin Colombet5cded892014-08-11 23:52:01 +0000393 return false;
Evan Chenge53ab6d2010-09-17 22:28:18 +0000394
395 // It's not always legal to break critical edges and sink the computation
396 // to the edge.
397 //
398 // BB#1:
399 // v1024
400 // Beq BB#3
401 // <fallthrough>
402 // BB#2:
403 // ... no uses of v1024
404 // <fallthrough>
405 // BB#3:
406 // ...
407 // = v1024
408 //
409 // If BB#1 -> BB#3 edge is broken and computation of v1024 is inserted:
410 //
411 // BB#1:
412 // ...
413 // Bne BB#2
414 // BB#4:
415 // v1024 =
416 // B BB#3
417 // BB#2:
418 // ... no uses of v1024
419 // <fallthrough>
420 // BB#3:
421 // ...
422 // = v1024
423 //
424 // This is incorrect since v1024 is not computed along the BB#1->BB#2->BB#3
425 // flow. We need to ensure the new basic block where the computation is
426 // sunk to dominates all the uses.
427 // It's only legal to break critical edge and sink the computation to the
428 // new block if all the predecessors of "To", except for "From", are
429 // not dominated by "From". Given SSA property, this means these
430 // predecessors are dominated by "To".
431 //
432 // There is no need to do this check if all the uses are PHI nodes. PHI
433 // sources are only defined on the specific predecessor edges.
Evan Cheng2031b762010-09-20 19:12:55 +0000434 if (!BreakPHIEdge) {
Evan Chengae9939c2010-08-19 17:33:11 +0000435 for (MachineBasicBlock::pred_iterator PI = ToBB->pred_begin(),
436 E = ToBB->pred_end(); PI != E; ++PI) {
437 if (*PI == FromBB)
438 continue;
439 if (!DT->dominates(ToBB, *PI))
Quentin Colombet5cded892014-08-11 23:52:01 +0000440 return false;
Evan Chengae9939c2010-08-19 17:33:11 +0000441 }
Evan Chengae9939c2010-08-19 17:33:11 +0000442 }
443
Quentin Colombet5cded892014-08-11 23:52:01 +0000444 ToSplit.insert(std::make_pair(FromBB, ToBB));
445
446 return true;
Evan Chengae9939c2010-08-19 17:33:11 +0000447}
448
Evan Chengd4b31a72010-09-23 06:53:00 +0000449static bool AvoidsSinking(MachineInstr *MI, MachineRegisterInfo *MRI) {
450 return MI->isInsertSubreg() || MI->isSubregToReg() || MI->isRegSequence();
451}
452
Andrew Trick9e761992012-02-08 21:22:43 +0000453/// collectDebgValues - Scan instructions following MI and collect any
Devang Patel9de7a7d2011-09-07 00:07:58 +0000454/// matching DBG_VALUEs.
Andrew Trick9e761992012-02-08 21:22:43 +0000455static void collectDebugValues(MachineInstr *MI,
Craig Topperb94011f2013-07-14 04:42:23 +0000456 SmallVectorImpl<MachineInstr *> &DbgValues) {
Devang Patel9de7a7d2011-09-07 00:07:58 +0000457 DbgValues.clear();
458 if (!MI->getOperand(0).isReg())
459 return;
460
461 MachineBasicBlock::iterator DI = MI; ++DI;
462 for (MachineBasicBlock::iterator DE = MI->getParent()->end();
463 DI != DE; ++DI) {
464 if (!DI->isDebugValue())
465 return;
466 if (DI->getOperand(0).isReg() &&
467 DI->getOperand(0).getReg() == MI->getOperand(0).getReg())
468 DbgValues.push_back(DI);
469 }
470}
471
Jingyue Wufd47fb92014-10-01 15:22:13 +0000472/// isPostDominatedBy - Return true if A is post dominated by B.
473static bool isPostDominatedBy(MachineBasicBlock *A, MachineBasicBlock *B) {
474
475 // FIXME - Use real post dominator.
476 if (A->succ_size() != 2)
477 return false;
478 MachineBasicBlock::succ_iterator I = A->succ_begin();
479 if (B == *I)
480 ++I;
481 MachineBasicBlock *OtherSuccBlock = *I;
482 if (OtherSuccBlock->succ_size() != 1 ||
483 *(OtherSuccBlock->succ_begin()) != B)
484 return false;
485
486 return true;
487}
488
Devang Patelc2686882011-12-14 23:20:38 +0000489/// isProfitableToSinkTo - Return true if it is profitable to sink MI.
Andrew Trick9e761992012-02-08 21:22:43 +0000490bool MachineSinking::isProfitableToSinkTo(unsigned Reg, MachineInstr *MI,
Devang Patelc2686882011-12-14 23:20:38 +0000491 MachineBasicBlock *MBB,
492 MachineBasicBlock *SuccToSinkTo) {
493 assert (MI && "Invalid MachineInstr!");
494 assert (SuccToSinkTo && "Invalid SinkTo Candidate BB");
495
496 if (MBB == SuccToSinkTo)
497 return false;
498
499 // It is profitable if SuccToSinkTo does not post dominate current block.
Jingyue Wufd47fb92014-10-01 15:22:13 +0000500 if (!isPostDominatedBy(MBB, SuccToSinkTo))
501 return true;
Devang Patelc2686882011-12-14 23:20:38 +0000502
503 // Check if only use in post dominated block is PHI instruction.
504 bool NonPHIUse = false;
Owen Andersonb36376e2014-03-17 19:36:09 +0000505 for (MachineInstr &UseInst : MRI->use_nodbg_instructions(Reg)) {
506 MachineBasicBlock *UseBlock = UseInst.getParent();
507 if (UseBlock == SuccToSinkTo && !UseInst.isPHI())
Devang Patelc2686882011-12-14 23:20:38 +0000508 NonPHIUse = true;
509 }
510 if (!NonPHIUse)
511 return true;
512
513 // If SuccToSinkTo post dominates then also it may be profitable if MI
514 // can further profitably sinked into another block in next round.
515 bool BreakPHIEdge = false;
516 // FIXME - If finding successor is compile time expensive then catch results.
517 if (MachineBasicBlock *MBB2 = FindSuccToSinkTo(MI, SuccToSinkTo, BreakPHIEdge))
518 return isProfitableToSinkTo(Reg, MI, SuccToSinkTo, MBB2);
519
520 // If SuccToSinkTo is final destination and it is a post dominator of current
521 // block then it is not profitable to sink MI into SuccToSinkTo block.
522 return false;
523}
524
Devang Patelb94c9a42011-12-08 21:48:01 +0000525/// FindSuccToSinkTo - Find a successor to sink this instruction to.
526MachineBasicBlock *MachineSinking::FindSuccToSinkTo(MachineInstr *MI,
Devang Patelc2686882011-12-14 23:20:38 +0000527 MachineBasicBlock *MBB,
528 bool &BreakPHIEdge) {
529
530 assert (MI && "Invalid MachineInstr!");
531 assert (MBB && "Invalid MachineBasicBlock!");
Jim Grosbach01edd682010-06-03 23:49:57 +0000532
Chris Lattnerf3edc092008-01-04 07:36:53 +0000533 // Loop over all the operands of the specified instruction. If there is
534 // anything we can't handle, bail out.
Jim Grosbach01edd682010-06-03 23:49:57 +0000535
Chris Lattnerf3edc092008-01-04 07:36:53 +0000536 // SuccToSinkTo - This is the successor to sink this instruction to, once we
537 // decide.
Craig Topperc0196b12014-04-14 00:51:57 +0000538 MachineBasicBlock *SuccToSinkTo = nullptr;
Chris Lattnerf3edc092008-01-04 07:36:53 +0000539 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
540 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000541 if (!MO.isReg()) continue; // Ignore non-register operands.
Jim Grosbach01edd682010-06-03 23:49:57 +0000542
Chris Lattnerf3edc092008-01-04 07:36:53 +0000543 unsigned Reg = MO.getReg();
544 if (Reg == 0) continue;
Jim Grosbach01edd682010-06-03 23:49:57 +0000545
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000546 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Dan Gohmana3176872009-09-25 22:53:29 +0000547 if (MO.isUse()) {
548 // If the physreg has no defs anywhere, it's just an ambient register
Dan Gohman2f5bdcb2009-09-26 02:34:00 +0000549 // and we can freely move its uses. Alternatively, if it's allocatable,
550 // it could get allocated to something with a def during allocation.
Jakob Stoklund Olesen86ae07f2012-01-16 22:34:08 +0000551 if (!MRI->isConstantPhysReg(Reg, *MBB->getParent()))
Craig Topperc0196b12014-04-14 00:51:57 +0000552 return nullptr;
Bill Wendlinge41e40f2010-06-25 20:48:10 +0000553 } else if (!MO.isDead()) {
554 // A def that isn't dead. We can't move it.
Craig Topperc0196b12014-04-14 00:51:57 +0000555 return nullptr;
Dan Gohmana3176872009-09-25 22:53:29 +0000556 }
Chris Lattnerf3edc092008-01-04 07:36:53 +0000557 } else {
558 // Virtual register uses are always safe to sink.
559 if (MO.isUse()) continue;
Evan Cheng47a65a12009-02-07 01:21:47 +0000560
561 // If it's not safe to move defs of the register class, then abort.
Evan Chenge53ab6d2010-09-17 22:28:18 +0000562 if (!TII->isSafeToMoveRegClassDefs(MRI->getRegClass(Reg)))
Craig Topperc0196b12014-04-14 00:51:57 +0000563 return nullptr;
Jim Grosbach01edd682010-06-03 23:49:57 +0000564
Chris Lattneree61d142008-01-05 06:47:58 +0000565 // FIXME: This picks a successor to sink into based on having one
566 // successor that dominates all the uses. However, there are cases where
567 // sinking can happen but where the sink point isn't a successor. For
568 // example:
Jim Grosbach01edd682010-06-03 23:49:57 +0000569 //
Chris Lattneree61d142008-01-05 06:47:58 +0000570 // x = computation
571 // if () {} else {}
572 // use x
Jim Grosbach01edd682010-06-03 23:49:57 +0000573 //
Bill Wendling7ee730e2010-06-02 23:04:26 +0000574 // the instruction could be sunk over the whole diamond for the
Chris Lattneree61d142008-01-05 06:47:58 +0000575 // if/then/else (or loop, etc), allowing it to be sunk into other blocks
576 // after that.
Jim Grosbach01edd682010-06-03 23:49:57 +0000577
Chris Lattnerf3edc092008-01-04 07:36:53 +0000578 // Virtual register defs can only be sunk if all their uses are in blocks
579 // dominated by one of the successors.
580 if (SuccToSinkTo) {
581 // If a previous operand picked a block to sink to, then this operand
582 // must be sinkable to the same block.
Evan Cheng361b9be2010-08-19 18:33:29 +0000583 bool LocalUse = false;
Devang Patelc2686882011-12-14 23:20:38 +0000584 if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, MBB,
Evan Cheng2031b762010-09-20 19:12:55 +0000585 BreakPHIEdge, LocalUse))
Craig Topperc0196b12014-04-14 00:51:57 +0000586 return nullptr;
Bill Wendling7ee730e2010-06-02 23:04:26 +0000587
Chris Lattnerf3edc092008-01-04 07:36:53 +0000588 continue;
589 }
Jim Grosbach01edd682010-06-03 23:49:57 +0000590
Chris Lattnerf3edc092008-01-04 07:36:53 +0000591 // Otherwise, we should look at all the successors and decide which one
Bruno Cardoso Lopesd04f7592014-09-25 23:14:26 +0000592 // we should sink to. If we have reliable block frequency information
593 // (frequency != 0) available, give successors with smaller frequencies
594 // higher priority, otherwise prioritize smaller loop depths.
595 SmallVector<MachineBasicBlock*, 4> Succs(MBB->succ_begin(),
596 MBB->succ_end());
597 // Sort Successors according to their loop depth or block frequency info.
Benjamin Kramer3a377bc2014-03-01 11:47:00 +0000598 std::stable_sort(
599 Succs.begin(), Succs.end(),
Bruno Cardoso Lopesd04f7592014-09-25 23:14:26 +0000600 [this](const MachineBasicBlock *L, const MachineBasicBlock *R) {
601 uint64_t LHSFreq = MBFI ? MBFI->getBlockFreq(L).getFrequency() : 0;
602 uint64_t RHSFreq = MBFI ? MBFI->getBlockFreq(R).getFrequency() : 0;
603 bool HasBlockFreq = LHSFreq != 0 && RHSFreq != 0;
604 return HasBlockFreq ? LHSFreq < RHSFreq
605 : LI->getLoopDepth(L) < LI->getLoopDepth(R);
Benjamin Kramer3a377bc2014-03-01 11:47:00 +0000606 });
Craig Toppere1c1d362013-07-03 05:11:49 +0000607 for (SmallVectorImpl<MachineBasicBlock *>::iterator SI = Succs.begin(),
608 E = Succs.end(); SI != E; ++SI) {
Devang Patelc2686882011-12-14 23:20:38 +0000609 MachineBasicBlock *SuccBlock = *SI;
Evan Cheng361b9be2010-08-19 18:33:29 +0000610 bool LocalUse = false;
Devang Patelc2686882011-12-14 23:20:38 +0000611 if (AllUsesDominatedByBlock(Reg, SuccBlock, MBB,
Evan Cheng2031b762010-09-20 19:12:55 +0000612 BreakPHIEdge, LocalUse)) {
Devang Patel1a3c1692011-12-08 21:33:23 +0000613 SuccToSinkTo = SuccBlock;
Chris Lattnerf3edc092008-01-04 07:36:53 +0000614 break;
615 }
Evan Cheng25b60682010-08-18 23:09:25 +0000616 if (LocalUse)
617 // Def is used locally, it's never safe to move this def.
Craig Topperc0196b12014-04-14 00:51:57 +0000618 return nullptr;
Chris Lattnerf3edc092008-01-04 07:36:53 +0000619 }
Jim Grosbach01edd682010-06-03 23:49:57 +0000620
Chris Lattnerf3edc092008-01-04 07:36:53 +0000621 // If we couldn't find a block to sink to, ignore this instruction.
Craig Topperc0196b12014-04-14 00:51:57 +0000622 if (!SuccToSinkTo)
623 return nullptr;
624 if (!isProfitableToSinkTo(Reg, MI, MBB, SuccToSinkTo))
625 return nullptr;
Chris Lattnerf3edc092008-01-04 07:36:53 +0000626 }
627 }
Devang Patel202cf2f2011-12-08 23:52:00 +0000628
629 // It is not possible to sink an instruction into its own block. This can
630 // happen with loops.
Devang Patelc2686882011-12-14 23:20:38 +0000631 if (MBB == SuccToSinkTo)
Craig Topperc0196b12014-04-14 00:51:57 +0000632 return nullptr;
Devang Patel202cf2f2011-12-08 23:52:00 +0000633
634 // It's not safe to sink instructions to EH landing pad. Control flow into
635 // landing pad is implicitly defined.
636 if (SuccToSinkTo && SuccToSinkTo->isLandingPad())
Craig Topperc0196b12014-04-14 00:51:57 +0000637 return nullptr;
Devang Patel202cf2f2011-12-08 23:52:00 +0000638
Devang Patelb94c9a42011-12-08 21:48:01 +0000639 return SuccToSinkTo;
640}
641
642/// SinkInstruction - Determine whether it is safe to sink the specified machine
643/// instruction out of its current block into a successor.
644bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) {
645 // Don't sink insert_subreg, subreg_to_reg, reg_sequence. These are meant to
646 // be close to the source to make it easier to coalesce.
647 if (AvoidsSinking(MI, MRI))
648 return false;
649
650 // Check if it's safe to move the instruction.
651 if (!MI->isSafeToMove(TII, AA, SawStore))
652 return false;
653
654 // FIXME: This should include support for sinking instructions within the
655 // block they are currently in to shorten the live ranges. We often get
656 // instructions sunk into the top of a large block, but it would be better to
657 // also sink them down before their first use in the block. This xform has to
658 // be careful not to *increase* register pressure though, e.g. sinking
659 // "x = y + z" down if it kills y and z would increase the live ranges of y
660 // and z and only shrink the live range of x.
661
662 bool BreakPHIEdge = false;
Devang Patelc2686882011-12-14 23:20:38 +0000663 MachineBasicBlock *ParentBlock = MI->getParent();
664 MachineBasicBlock *SuccToSinkTo = FindSuccToSinkTo(MI, ParentBlock, BreakPHIEdge);
Jim Grosbach01edd682010-06-03 23:49:57 +0000665
Chris Lattner6ec78272008-01-05 01:39:17 +0000666 // If there are no outputs, it must have side-effects.
Craig Topperc0196b12014-04-14 00:51:57 +0000667 if (!SuccToSinkTo)
Chris Lattner6ec78272008-01-05 01:39:17 +0000668 return false;
Evan Cheng25104362009-02-15 08:36:12 +0000669
Bill Wendlingf82aea62010-06-03 07:54:20 +0000670
Daniel Dunbaref5a4382010-06-23 00:48:25 +0000671 // If the instruction to move defines a dead physical register which is live
672 // when leaving the basic block, don't move it because it could turn into a
673 // "zombie" define of that preg. E.g., EFLAGS. (<rdar://problem/8030636>)
Bill Wendlinge41e40f2010-06-25 20:48:10 +0000674 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
675 const MachineOperand &MO = MI->getOperand(I);
676 if (!MO.isReg()) continue;
677 unsigned Reg = MO.getReg();
678 if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
679 if (SuccToSinkTo->isLiveIn(Reg))
Bill Wendlingf82aea62010-06-03 07:54:20 +0000680 return false;
Bill Wendlinge41e40f2010-06-25 20:48:10 +0000681 }
Bill Wendlingf82aea62010-06-03 07:54:20 +0000682
Bill Wendling7ee730e2010-06-02 23:04:26 +0000683 DEBUG(dbgs() << "Sink instr " << *MI << "\tinto block " << *SuccToSinkTo);
684
Will Dietz5cb7f4e2013-10-14 16:57:17 +0000685 // If the block has multiple predecessors, this is a critical edge.
686 // Decide if we can sink along it or need to break the edge.
Chris Lattnerf3edc092008-01-04 07:36:53 +0000687 if (SuccToSinkTo->pred_size() > 1) {
Jakob Stoklund Olesen20b71e22010-04-13 19:06:14 +0000688 // We cannot sink a load across a critical edge - there may be stores in
689 // other code paths.
Evan Chengae9939c2010-08-19 17:33:11 +0000690 bool TryBreak = false;
Jakob Stoklund Olesen20b71e22010-04-13 19:06:14 +0000691 bool store = true;
692 if (!MI->isSafeToMove(TII, AA, store)) {
Evan Chenge5af9302010-08-19 23:33:02 +0000693 DEBUG(dbgs() << " *** NOTE: Won't sink load along critical edge.\n");
Evan Chengae9939c2010-08-19 17:33:11 +0000694 TryBreak = true;
Jakob Stoklund Olesen20b71e22010-04-13 19:06:14 +0000695 }
696
697 // We don't want to sink across a critical edge if we don't dominate the
698 // successor. We could be introducing calculations to new code paths.
Evan Chengae9939c2010-08-19 17:33:11 +0000699 if (!TryBreak && !DT->dominates(ParentBlock, SuccToSinkTo)) {
Evan Chenge5af9302010-08-19 23:33:02 +0000700 DEBUG(dbgs() << " *** NOTE: Critical edge found\n");
Evan Chengae9939c2010-08-19 17:33:11 +0000701 TryBreak = true;
Jakob Stoklund Olesen20b71e22010-04-13 19:06:14 +0000702 }
703
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +0000704 // Don't sink instructions into a loop.
Evan Chengae9939c2010-08-19 17:33:11 +0000705 if (!TryBreak && LI->isLoopHeader(SuccToSinkTo)) {
Evan Chenge5af9302010-08-19 23:33:02 +0000706 DEBUG(dbgs() << " *** NOTE: Loop header found\n");
Evan Chengae9939c2010-08-19 17:33:11 +0000707 TryBreak = true;
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +0000708 }
709
Jakob Stoklund Olesen20b71e22010-04-13 19:06:14 +0000710 // Otherwise we are OK with sinking along a critical edge.
Evan Chengae9939c2010-08-19 17:33:11 +0000711 if (!TryBreak)
712 DEBUG(dbgs() << "Sinking along critical edge.\n");
713 else {
Quentin Colombet5cded892014-08-11 23:52:01 +0000714 // Mark this edge as to be split.
715 // If the edge can actually be split, the next iteration of the main loop
716 // will sink MI in the newly created block.
717 bool Status =
718 PostponeSplitCriticalEdge(MI, ParentBlock, SuccToSinkTo, BreakPHIEdge);
719 if (!Status)
Evan Chenge53ab6d2010-09-17 22:28:18 +0000720 DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
Quentin Colombet5cded892014-08-11 23:52:01 +0000721 "break critical edge\n");
722 // The instruction will not be sunk this time.
723 return false;
Evan Chengae9939c2010-08-19 17:33:11 +0000724 }
Chris Lattnerf3edc092008-01-04 07:36:53 +0000725 }
Jim Grosbach01edd682010-06-03 23:49:57 +0000726
Evan Cheng2031b762010-09-20 19:12:55 +0000727 if (BreakPHIEdge) {
728 // BreakPHIEdge is true if all the uses are in the successor MBB being
729 // sunken into and they are all PHI nodes. In this case, machine-sink must
730 // break the critical edge first.
Quentin Colombet5cded892014-08-11 23:52:01 +0000731 bool Status = PostponeSplitCriticalEdge(MI, ParentBlock,
732 SuccToSinkTo, BreakPHIEdge);
733 if (!Status)
Evan Chengb339f3d2010-09-18 06:42:17 +0000734 DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
735 "break critical edge\n");
Quentin Colombet5cded892014-08-11 23:52:01 +0000736 // The instruction will not be sunk this time.
737 return false;
Evan Chengb339f3d2010-09-18 06:42:17 +0000738 }
739
Bill Wendling7ee730e2010-06-02 23:04:26 +0000740 // Determine where to insert into. Skip phi nodes.
Chris Lattnerf3edc092008-01-04 07:36:53 +0000741 MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
Evan Chengb339f3d2010-09-18 06:42:17 +0000742 while (InsertPos != SuccToSinkTo->end() && InsertPos->isPHI())
Chris Lattnerf3edc092008-01-04 07:36:53 +0000743 ++InsertPos;
Jim Grosbach01edd682010-06-03 23:49:57 +0000744
Devang Patel9de7a7d2011-09-07 00:07:58 +0000745 // collect matching debug values.
746 SmallVector<MachineInstr *, 2> DbgValuesToSink;
747 collectDebugValues(MI, DbgValuesToSink);
748
Chris Lattnerf3edc092008-01-04 07:36:53 +0000749 // Move the instruction.
750 SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
751 ++MachineBasicBlock::iterator(MI));
Dan Gohmanc90f51c2010-05-13 20:34:42 +0000752
Devang Patel9de7a7d2011-09-07 00:07:58 +0000753 // Move debug values.
Craig Toppere1c1d362013-07-03 05:11:49 +0000754 for (SmallVectorImpl<MachineInstr *>::iterator DBI = DbgValuesToSink.begin(),
Devang Patel9de7a7d2011-09-07 00:07:58 +0000755 DBE = DbgValuesToSink.end(); DBI != DBE; ++DBI) {
756 MachineInstr *DbgMI = *DBI;
757 SuccToSinkTo->splice(InsertPos, ParentBlock, DbgMI,
758 ++MachineBasicBlock::iterator(DbgMI));
759 }
760
Juergen Ributzka4bea4942014-09-04 02:07:36 +0000761 // Conservatively, clear any kill flags, since it's possible that they are no
762 // longer correct.
763 MI->clearKillInfo();
Dan Gohmanc90f51c2010-05-13 20:34:42 +0000764
Chris Lattnerf3edc092008-01-04 07:36:53 +0000765 return true;
766}