blob: 90ca25a6cef3c8e1f4d86686ed4f43bd4c437ea8 [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"
Jingyue Wu29542802014-10-15 03:27:43 +000027#include "llvm/CodeGen/MachinePostDominators.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengae9939c2010-08-19 17:33:11 +000029#include "llvm/Support/CommandLine.h"
Chris Lattnerf3edc092008-01-04 07:36:53 +000030#include "llvm/Support/Debug.h"
Bill Wendling63aa0002009-08-22 20:26:23 +000031#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Target/TargetInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#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 Wu29542802014-10-15 03:27:43 +000058 MachineRegisterInfo *MRI; // Machine register information
59 MachineDominatorTree *DT; // Machine dominator tree
60 MachinePostDominatorTree *PDT; // Machine post dominator tree
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +000061 MachineLoopInfo *LI;
Bruno Cardoso Lopesd04f7592014-09-25 23:14:26 +000062 const MachineBlockFrequencyInfo *MBFI;
Dan Gohman87b02d52009-10-09 23:27:56 +000063 AliasAnalysis *AA;
Chris Lattnerf3edc092008-01-04 07:36:53 +000064
Evan Chenge53ab6d2010-09-17 22:28:18 +000065 // Remember which edges have been considered for breaking.
66 SmallSet<std::pair<MachineBasicBlock*,MachineBasicBlock*>, 8>
67 CEBCandidates;
Quentin Colombet5cded892014-08-11 23:52:01 +000068 // Remember which edges we are about to split.
69 // This is different from CEBCandidates since those edges
70 // will be split.
71 SetVector<std::pair<MachineBasicBlock*,MachineBasicBlock*> > ToSplit;
Evan Chenge53ab6d2010-09-17 22:28:18 +000072
Chris Lattnerf3edc092008-01-04 07:36:53 +000073 public:
74 static char ID; // Pass identification
Owen Anderson6c18d1a2010-10-19 17:21:58 +000075 MachineSinking() : MachineFunctionPass(ID) {
76 initializeMachineSinkingPass(*PassRegistry::getPassRegistry());
77 }
Jim Grosbach01edd682010-06-03 23:49:57 +000078
Craig Topper4584cd52014-03-07 09:26:03 +000079 bool runOnMachineFunction(MachineFunction &MF) override;
Jim Grosbach01edd682010-06-03 23:49:57 +000080
Craig Topper4584cd52014-03-07 09:26:03 +000081 void getAnalysisUsage(AnalysisUsage &AU) const override {
Dan Gohman04023152009-07-31 23:37:33 +000082 AU.setPreservesCFG();
Chris Lattnerf3edc092008-01-04 07:36:53 +000083 MachineFunctionPass::getAnalysisUsage(AU);
Dan Gohman87b02d52009-10-09 23:27:56 +000084 AU.addRequired<AliasAnalysis>();
Chris Lattnerf3edc092008-01-04 07:36:53 +000085 AU.addRequired<MachineDominatorTree>();
Jingyue Wu29542802014-10-15 03:27:43 +000086 AU.addRequired<MachinePostDominatorTree>();
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +000087 AU.addRequired<MachineLoopInfo>();
Chris Lattnerf3edc092008-01-04 07:36:53 +000088 AU.addPreserved<MachineDominatorTree>();
Jingyue Wu29542802014-10-15 03:27:43 +000089 AU.addPreserved<MachinePostDominatorTree>();
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +000090 AU.addPreserved<MachineLoopInfo>();
Bruno Cardoso Lopesd04f7592014-09-25 23:14:26 +000091 if (UseBlockFreqInfo)
92 AU.addRequired<MachineBlockFrequencyInfo>();
Chris Lattnerf3edc092008-01-04 07:36:53 +000093 }
Evan Chenge53ab6d2010-09-17 22:28:18 +000094
Craig Topper4584cd52014-03-07 09:26:03 +000095 void releaseMemory() override {
Evan Chenge53ab6d2010-09-17 22:28:18 +000096 CEBCandidates.clear();
97 }
98
Chris Lattnerf3edc092008-01-04 07:36:53 +000099 private:
100 bool ProcessBlock(MachineBasicBlock &MBB);
Evan Chenge53ab6d2010-09-17 22:28:18 +0000101 bool isWorthBreakingCriticalEdge(MachineInstr *MI,
102 MachineBasicBlock *From,
103 MachineBasicBlock *To);
Quentin Colombet5cded892014-08-11 23:52:01 +0000104 /// \brief Postpone the splitting of the given critical
105 /// edge (\p From, \p To).
106 ///
107 /// We do not split the edges on the fly. Indeed, this invalidates
108 /// the dominance information and thus triggers a lot of updates
109 /// of that information underneath.
110 /// Instead, we postpone all the splits after each iteration of
111 /// the main loop. That way, the information is at least valid
112 /// for the lifetime of an iteration.
113 ///
114 /// \return True if the edge is marked as toSplit, false otherwise.
Patrik Hagglundd06de4b2014-12-04 10:36:42 +0000115 /// False can be returned if, for instance, this is not profitable.
Quentin Colombet5cded892014-08-11 23:52:01 +0000116 bool PostponeSplitCriticalEdge(MachineInstr *MI,
117 MachineBasicBlock *From,
118 MachineBasicBlock *To,
119 bool BreakPHIEdge);
Chris Lattner08af5a92008-01-12 00:17:41 +0000120 bool SinkInstruction(MachineInstr *MI, bool &SawStore);
Evan Cheng25b60682010-08-18 23:09:25 +0000121 bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB,
Evan Chenge53ab6d2010-09-17 22:28:18 +0000122 MachineBasicBlock *DefMBB,
Evan Cheng2031b762010-09-20 19:12:55 +0000123 bool &BreakPHIEdge, bool &LocalUse) const;
Devang Patelc2686882011-12-14 23:20:38 +0000124 MachineBasicBlock *FindSuccToSinkTo(MachineInstr *MI, MachineBasicBlock *MBB,
125 bool &BreakPHIEdge);
Andrew Trick9e761992012-02-08 21:22:43 +0000126 bool isProfitableToSinkTo(unsigned Reg, MachineInstr *MI,
Devang Patelc2686882011-12-14 23:20:38 +0000127 MachineBasicBlock *MBB,
128 MachineBasicBlock *SuccToSinkTo);
Devang Patelb94c9a42011-12-08 21:48:01 +0000129
Evan Chenge53ab6d2010-09-17 22:28:18 +0000130 bool PerformTrivialForwardCoalescing(MachineInstr *MI,
131 MachineBasicBlock *MBB);
Chris Lattnerf3edc092008-01-04 07:36:53 +0000132 };
Chris Lattnerf3edc092008-01-04 07:36:53 +0000133} // end anonymous namespace
Jim Grosbach01edd682010-06-03 23:49:57 +0000134
Dan Gohmand78c4002008-05-13 00:00:25 +0000135char MachineSinking::ID = 0;
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000136char &llvm::MachineSinkingID = MachineSinking::ID;
Owen Anderson8ac477f2010-10-12 19:48:12 +0000137INITIALIZE_PASS_BEGIN(MachineSinking, "machine-sink",
138 "Machine code sinking", false, false)
139INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
140INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
141INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
142INITIALIZE_PASS_END(MachineSinking, "machine-sink",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000143 "Machine code sinking", false, false)
Chris Lattnerf3edc092008-01-04 07:36:53 +0000144
Evan Chenge53ab6d2010-09-17 22:28:18 +0000145bool MachineSinking::PerformTrivialForwardCoalescing(MachineInstr *MI,
146 MachineBasicBlock *MBB) {
147 if (!MI->isCopy())
148 return false;
149
150 unsigned SrcReg = MI->getOperand(1).getReg();
151 unsigned DstReg = MI->getOperand(0).getReg();
152 if (!TargetRegisterInfo::isVirtualRegister(SrcReg) ||
153 !TargetRegisterInfo::isVirtualRegister(DstReg) ||
154 !MRI->hasOneNonDBGUse(SrcReg))
155 return false;
156
157 const TargetRegisterClass *SRC = MRI->getRegClass(SrcReg);
158 const TargetRegisterClass *DRC = MRI->getRegClass(DstReg);
159 if (SRC != DRC)
160 return false;
161
162 MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
163 if (DefMI->isCopyLike())
164 return false;
165 DEBUG(dbgs() << "Coalescing: " << *DefMI);
166 DEBUG(dbgs() << "*** to: " << *MI);
167 MRI->replaceRegWith(DstReg, SrcReg);
168 MI->eraseFromParent();
Patrik Hagglund57d315b2014-09-09 07:47:00 +0000169
170 // Conservatively, clear any kill flags, since it's possible that they are no
171 // longer correct.
172 MRI->clearKillFlags(SrcReg);
173
Evan Chenge53ab6d2010-09-17 22:28:18 +0000174 ++NumCoalesces;
175 return true;
176}
177
Chris Lattnerf3edc092008-01-04 07:36:53 +0000178/// AllUsesDominatedByBlock - Return true if all uses of the specified register
Evan Cheng25b60682010-08-18 23:09:25 +0000179/// occur in blocks dominated by the specified block. If any use is in the
180/// definition block, then return false since it is never legal to move def
181/// after uses.
Evan Chenge53ab6d2010-09-17 22:28:18 +0000182bool
183MachineSinking::AllUsesDominatedByBlock(unsigned Reg,
184 MachineBasicBlock *MBB,
185 MachineBasicBlock *DefMBB,
Evan Cheng2031b762010-09-20 19:12:55 +0000186 bool &BreakPHIEdge,
187 bool &LocalUse) const {
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000188 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
189 "Only makes sense for vregs");
Evan Chengb339f3d2010-09-18 06:42:17 +0000190
Devang Patel706574a2011-12-09 01:25:04 +0000191 // Ignore debug uses because debug info doesn't affect the code.
Evan Chengb339f3d2010-09-18 06:42:17 +0000192 if (MRI->use_nodbg_empty(Reg))
193 return true;
194
Evan Cheng2031b762010-09-20 19:12:55 +0000195 // BreakPHIEdge is true if all the uses are in the successor MBB being sunken
196 // into and they are all PHI nodes. In this case, machine-sink must break
197 // the critical edge first. e.g.
198 //
Evan Chengb339f3d2010-09-18 06:42:17 +0000199 // BB#1: derived from LLVM BB %bb4.preheader
200 // Predecessors according to CFG: BB#0
201 // ...
202 // %reg16385<def> = DEC64_32r %reg16437, %EFLAGS<imp-def,dead>
203 // ...
204 // JE_4 <BB#37>, %EFLAGS<imp-use>
205 // Successors according to CFG: BB#37 BB#2
206 //
207 // BB#2: derived from LLVM BB %bb.nph
208 // Predecessors according to CFG: BB#0 BB#1
209 // %reg16386<def> = PHI %reg16434, <BB#0>, %reg16385, <BB#1>
Evan Cheng2031b762010-09-20 19:12:55 +0000210 BreakPHIEdge = true;
Owen Andersonb36376e2014-03-17 19:36:09 +0000211 for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
212 MachineInstr *UseInst = MO.getParent();
213 unsigned OpNo = &MO - &UseInst->getOperand(0);
Evan Chengb339f3d2010-09-18 06:42:17 +0000214 MachineBasicBlock *UseBlock = UseInst->getParent();
215 if (!(UseBlock == MBB && UseInst->isPHI() &&
Owen Andersonb36376e2014-03-17 19:36:09 +0000216 UseInst->getOperand(OpNo+1).getMBB() == DefMBB)) {
Evan Cheng2031b762010-09-20 19:12:55 +0000217 BreakPHIEdge = false;
Evan Chengb339f3d2010-09-18 06:42:17 +0000218 break;
219 }
220 }
Evan Cheng2031b762010-09-20 19:12:55 +0000221 if (BreakPHIEdge)
Evan Chengb339f3d2010-09-18 06:42:17 +0000222 return true;
223
Owen Andersonb36376e2014-03-17 19:36:09 +0000224 for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
Chris Lattnerf3edc092008-01-04 07:36:53 +0000225 // Determine the block of the use.
Owen Andersonb36376e2014-03-17 19:36:09 +0000226 MachineInstr *UseInst = MO.getParent();
227 unsigned OpNo = &MO - &UseInst->getOperand(0);
Chris Lattnerf3edc092008-01-04 07:36:53 +0000228 MachineBasicBlock *UseBlock = UseInst->getParent();
Evan Chengb339f3d2010-09-18 06:42:17 +0000229 if (UseInst->isPHI()) {
Chris Lattnerf3edc092008-01-04 07:36:53 +0000230 // PHI nodes use the operand in the predecessor block, not the block with
231 // the PHI.
Owen Andersonb36376e2014-03-17 19:36:09 +0000232 UseBlock = UseInst->getOperand(OpNo+1).getMBB();
Evan Cheng361b9be2010-08-19 18:33:29 +0000233 } else if (UseBlock == DefMBB) {
234 LocalUse = true;
235 return false;
Chris Lattnerf3edc092008-01-04 07:36:53 +0000236 }
Bill Wendling7ee730e2010-06-02 23:04:26 +0000237
Chris Lattnerf3edc092008-01-04 07:36:53 +0000238 // Check that it dominates.
239 if (!DT->dominates(MBB, UseBlock))
240 return false;
241 }
Bill Wendling7ee730e2010-06-02 23:04:26 +0000242
Chris Lattnerf3edc092008-01-04 07:36:53 +0000243 return true;
244}
245
Chris Lattnerf3edc092008-01-04 07:36:53 +0000246bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
Paul Robinson7c99ec52014-03-31 17:43:35 +0000247 if (skipOptnoneFunction(*MF.getFunction()))
248 return false;
249
David Greene4b7aa242010-01-05 01:26:00 +0000250 DEBUG(dbgs() << "******** Machine Sinking ********\n");
Jim Grosbach01edd682010-06-03 23:49:57 +0000251
Eric Christophereb9e87f2014-10-14 07:00:33 +0000252 TII = MF.getSubtarget().getInstrInfo();
253 TRI = MF.getSubtarget().getRegisterInfo();
Evan Chenge53ab6d2010-09-17 22:28:18 +0000254 MRI = &MF.getRegInfo();
Chris Lattnerf3edc092008-01-04 07:36:53 +0000255 DT = &getAnalysis<MachineDominatorTree>();
Jingyue Wu29542802014-10-15 03:27:43 +0000256 PDT = &getAnalysis<MachinePostDominatorTree>();
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +0000257 LI = &getAnalysis<MachineLoopInfo>();
Bruno Cardoso Lopesd04f7592014-09-25 23:14:26 +0000258 MBFI = UseBlockFreqInfo ? &getAnalysis<MachineBlockFrequencyInfo>() : nullptr;
Dan Gohman87b02d52009-10-09 23:27:56 +0000259 AA = &getAnalysis<AliasAnalysis>();
Chris Lattnerf3edc092008-01-04 07:36:53 +0000260
261 bool EverMadeChange = false;
Jim Grosbach01edd682010-06-03 23:49:57 +0000262
Chris Lattnerf3edc092008-01-04 07:36:53 +0000263 while (1) {
264 bool MadeChange = false;
265
266 // Process all basic blocks.
Evan Chenge53ab6d2010-09-17 22:28:18 +0000267 CEBCandidates.clear();
Quentin Colombet5cded892014-08-11 23:52:01 +0000268 ToSplit.clear();
Jim Grosbach01edd682010-06-03 23:49:57 +0000269 for (MachineFunction::iterator I = MF.begin(), E = MF.end();
Chris Lattnerf3edc092008-01-04 07:36:53 +0000270 I != E; ++I)
271 MadeChange |= ProcessBlock(*I);
Jim Grosbach01edd682010-06-03 23:49:57 +0000272
Quentin Colombet5cded892014-08-11 23:52:01 +0000273 // If we have anything we marked as toSplit, split it now.
274 for (auto &Pair : ToSplit) {
275 auto NewSucc = Pair.first->SplitCriticalEdge(Pair.second, this);
276 if (NewSucc != nullptr) {
277 DEBUG(dbgs() << " *** Splitting critical edge:"
278 " BB#" << Pair.first->getNumber()
279 << " -- BB#" << NewSucc->getNumber()
280 << " -- BB#" << Pair.second->getNumber() << '\n');
281 MadeChange = true;
282 ++NumSplit;
283 } else
284 DEBUG(dbgs() << " *** Not legal to break critical edge\n");
285 }
Chris Lattnerf3edc092008-01-04 07:36:53 +0000286 // If this iteration over the code changed anything, keep iterating.
287 if (!MadeChange) break;
288 EverMadeChange = true;
Jim Grosbach01edd682010-06-03 23:49:57 +0000289 }
Chris Lattnerf3edc092008-01-04 07:36:53 +0000290 return EverMadeChange;
291}
292
293bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
Chris Lattnerf3edc092008-01-04 07:36:53 +0000294 // Can't sink anything out of a block that has less than two successors.
Chris Lattner30c3de62009-04-10 16:38:36 +0000295 if (MBB.succ_size() <= 1 || MBB.empty()) return false;
296
Dan Gohman918a90a2010-04-05 19:17:22 +0000297 // Don't bother sinking code out of unreachable blocks. In addition to being
Jim Grosbach01edd682010-06-03 23:49:57 +0000298 // unprofitable, it can also lead to infinite looping, because in an
299 // unreachable loop there may be nowhere to stop.
Dan Gohman918a90a2010-04-05 19:17:22 +0000300 if (!DT->isReachableFromEntry(&MBB)) return false;
301
Chris Lattner30c3de62009-04-10 16:38:36 +0000302 bool MadeChange = false;
303
Chris Lattner08af5a92008-01-12 00:17:41 +0000304 // Walk the basic block bottom-up. Remember if we saw a store.
Chris Lattner30c3de62009-04-10 16:38:36 +0000305 MachineBasicBlock::iterator I = MBB.end();
306 --I;
307 bool ProcessedBegin, SawStore = false;
308 do {
309 MachineInstr *MI = I; // The instruction to sink.
Jim Grosbach01edd682010-06-03 23:49:57 +0000310
Chris Lattner30c3de62009-04-10 16:38:36 +0000311 // Predecrement I (if it's not begin) so that it isn't invalidated by
312 // sinking.
313 ProcessedBegin = I == MBB.begin();
314 if (!ProcessedBegin)
315 --I;
Dale Johannesen2061c842010-03-05 00:02:59 +0000316
317 if (MI->isDebugValue())
318 continue;
319
Evan Chengfe917ef2011-04-11 18:47:20 +0000320 bool Joined = PerformTrivialForwardCoalescing(MI, &MBB);
321 if (Joined) {
322 MadeChange = true;
Evan Chenge53ab6d2010-09-17 22:28:18 +0000323 continue;
Evan Chengfe917ef2011-04-11 18:47:20 +0000324 }
Evan Chenge53ab6d2010-09-17 22:28:18 +0000325
Chris Lattner30c3de62009-04-10 16:38:36 +0000326 if (SinkInstruction(MI, SawStore))
327 ++NumSunk, MadeChange = true;
Jim Grosbach01edd682010-06-03 23:49:57 +0000328
Chris Lattner30c3de62009-04-10 16:38:36 +0000329 // If we just processed the first instruction in the block, we're done.
330 } while (!ProcessedBegin);
Jim Grosbach01edd682010-06-03 23:49:57 +0000331
Chris Lattnerf3edc092008-01-04 07:36:53 +0000332 return MadeChange;
333}
334
Evan Chenge53ab6d2010-09-17 22:28:18 +0000335bool MachineSinking::isWorthBreakingCriticalEdge(MachineInstr *MI,
336 MachineBasicBlock *From,
337 MachineBasicBlock *To) {
338 // FIXME: Need much better heuristics.
339
340 // If the pass has already considered breaking this edge (during this pass
341 // through the function), then let's go ahead and break it. This means
342 // sinking multiple "cheap" instructions into the same block.
David Blaikie70573dc2014-11-19 07:49:26 +0000343 if (!CEBCandidates.insert(std::make_pair(From, To)).second)
Evan Chenge53ab6d2010-09-17 22:28:18 +0000344 return true;
345
Jiangning Liuc3053122014-07-29 01:55:19 +0000346 if (!MI->isCopy() && !TII->isAsCheapAsAMove(MI))
Evan Chenge53ab6d2010-09-17 22:28:18 +0000347 return true;
348
349 // MI is cheap, we probably don't want to break the critical edge for it.
350 // However, if this would allow some definitions of its source operands
351 // to be sunk then it's probably worth it.
352 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
353 const MachineOperand &MO = MI->getOperand(i);
Will Dietz5cb7f4e2013-10-14 16:57:17 +0000354 if (!MO.isReg() || !MO.isUse())
Evan Chenge53ab6d2010-09-17 22:28:18 +0000355 continue;
Will Dietz5cb7f4e2013-10-14 16:57:17 +0000356 unsigned Reg = MO.getReg();
357 if (Reg == 0)
358 continue;
359
360 // We don't move live definitions of physical registers,
361 // so sinking their uses won't enable any opportunities.
362 if (TargetRegisterInfo::isPhysicalRegister(Reg))
363 continue;
364
365 // If this instruction is the only user of a virtual register,
366 // check if breaking the edge will enable sinking
367 // both this instruction and the defining instruction.
368 if (MRI->hasOneNonDBGUse(Reg)) {
369 // If the definition resides in same MBB,
370 // claim it's likely we can sink these together.
371 // If definition resides elsewhere, we aren't
372 // blocking it from being sunk so don't break the edge.
373 MachineInstr *DefMI = MRI->getVRegDef(Reg);
374 if (DefMI->getParent() == MI->getParent())
375 return true;
376 }
Evan Chenge53ab6d2010-09-17 22:28:18 +0000377 }
378
379 return false;
380}
381
Quentin Colombet5cded892014-08-11 23:52:01 +0000382bool MachineSinking::PostponeSplitCriticalEdge(MachineInstr *MI,
383 MachineBasicBlock *FromBB,
384 MachineBasicBlock *ToBB,
385 bool BreakPHIEdge) {
Evan Chenge53ab6d2010-09-17 22:28:18 +0000386 if (!isWorthBreakingCriticalEdge(MI, FromBB, ToBB))
Quentin Colombet5cded892014-08-11 23:52:01 +0000387 return false;
Evan Chenge53ab6d2010-09-17 22:28:18 +0000388
Evan Chengae9939c2010-08-19 17:33:11 +0000389 // Avoid breaking back edge. From == To means backedge for single BB loop.
Evan Chengf3e9a482010-09-20 22:52:00 +0000390 if (!SplitEdges || FromBB == ToBB)
Quentin Colombet5cded892014-08-11 23:52:01 +0000391 return false;
Evan Chengae9939c2010-08-19 17:33:11 +0000392
Evan Chenge53ab6d2010-09-17 22:28:18 +0000393 // Check for backedges of more "complex" loops.
394 if (LI->getLoopFor(FromBB) == LI->getLoopFor(ToBB) &&
395 LI->isLoopHeader(ToBB))
Quentin Colombet5cded892014-08-11 23:52:01 +0000396 return false;
Evan Chenge53ab6d2010-09-17 22:28:18 +0000397
398 // It's not always legal to break critical edges and sink the computation
399 // to the edge.
400 //
401 // BB#1:
402 // v1024
403 // Beq BB#3
404 // <fallthrough>
405 // BB#2:
406 // ... no uses of v1024
407 // <fallthrough>
408 // BB#3:
409 // ...
410 // = v1024
411 //
412 // If BB#1 -> BB#3 edge is broken and computation of v1024 is inserted:
413 //
414 // BB#1:
415 // ...
416 // Bne BB#2
417 // BB#4:
418 // v1024 =
419 // B BB#3
420 // BB#2:
421 // ... no uses of v1024
422 // <fallthrough>
423 // BB#3:
424 // ...
425 // = v1024
426 //
427 // This is incorrect since v1024 is not computed along the BB#1->BB#2->BB#3
428 // flow. We need to ensure the new basic block where the computation is
429 // sunk to dominates all the uses.
430 // It's only legal to break critical edge and sink the computation to the
431 // new block if all the predecessors of "To", except for "From", are
432 // not dominated by "From". Given SSA property, this means these
433 // predecessors are dominated by "To".
434 //
435 // There is no need to do this check if all the uses are PHI nodes. PHI
436 // sources are only defined on the specific predecessor edges.
Evan Cheng2031b762010-09-20 19:12:55 +0000437 if (!BreakPHIEdge) {
Evan Chengae9939c2010-08-19 17:33:11 +0000438 for (MachineBasicBlock::pred_iterator PI = ToBB->pred_begin(),
439 E = ToBB->pred_end(); PI != E; ++PI) {
440 if (*PI == FromBB)
441 continue;
442 if (!DT->dominates(ToBB, *PI))
Quentin Colombet5cded892014-08-11 23:52:01 +0000443 return false;
Evan Chengae9939c2010-08-19 17:33:11 +0000444 }
Evan Chengae9939c2010-08-19 17:33:11 +0000445 }
446
Quentin Colombet5cded892014-08-11 23:52:01 +0000447 ToSplit.insert(std::make_pair(FromBB, ToBB));
448
449 return true;
Evan Chengae9939c2010-08-19 17:33:11 +0000450}
451
Evan Chengd4b31a72010-09-23 06:53:00 +0000452static bool AvoidsSinking(MachineInstr *MI, MachineRegisterInfo *MRI) {
453 return MI->isInsertSubreg() || MI->isSubregToReg() || MI->isRegSequence();
454}
455
Andrew Trick9e761992012-02-08 21:22:43 +0000456/// collectDebgValues - Scan instructions following MI and collect any
Devang Patel9de7a7d2011-09-07 00:07:58 +0000457/// matching DBG_VALUEs.
Andrew Trick9e761992012-02-08 21:22:43 +0000458static void collectDebugValues(MachineInstr *MI,
Craig Topperb94011f2013-07-14 04:42:23 +0000459 SmallVectorImpl<MachineInstr *> &DbgValues) {
Devang Patel9de7a7d2011-09-07 00:07:58 +0000460 DbgValues.clear();
461 if (!MI->getOperand(0).isReg())
462 return;
463
464 MachineBasicBlock::iterator DI = MI; ++DI;
465 for (MachineBasicBlock::iterator DE = MI->getParent()->end();
466 DI != DE; ++DI) {
467 if (!DI->isDebugValue())
468 return;
469 if (DI->getOperand(0).isReg() &&
470 DI->getOperand(0).getReg() == MI->getOperand(0).getReg())
471 DbgValues.push_back(DI);
472 }
473}
474
Devang Patelc2686882011-12-14 23:20:38 +0000475/// isProfitableToSinkTo - Return true if it is profitable to sink MI.
Andrew Trick9e761992012-02-08 21:22:43 +0000476bool MachineSinking::isProfitableToSinkTo(unsigned Reg, MachineInstr *MI,
Devang Patelc2686882011-12-14 23:20:38 +0000477 MachineBasicBlock *MBB,
478 MachineBasicBlock *SuccToSinkTo) {
479 assert (MI && "Invalid MachineInstr!");
480 assert (SuccToSinkTo && "Invalid SinkTo Candidate BB");
481
482 if (MBB == SuccToSinkTo)
483 return false;
484
485 // It is profitable if SuccToSinkTo does not post dominate current block.
Jingyue Wu29542802014-10-15 03:27:43 +0000486 if (!PDT->dominates(SuccToSinkTo, MBB))
487 return true;
488
489 // It is profitable to sink an instruction from a deeper loop to a shallower
490 // loop, even if the latter post-dominates the former (PR21115).
491 if (LI->getLoopDepth(MBB) > LI->getLoopDepth(SuccToSinkTo))
492 return true;
Devang Patelc2686882011-12-14 23:20:38 +0000493
494 // Check if only use in post dominated block is PHI instruction.
495 bool NonPHIUse = false;
Owen Andersonb36376e2014-03-17 19:36:09 +0000496 for (MachineInstr &UseInst : MRI->use_nodbg_instructions(Reg)) {
497 MachineBasicBlock *UseBlock = UseInst.getParent();
498 if (UseBlock == SuccToSinkTo && !UseInst.isPHI())
Devang Patelc2686882011-12-14 23:20:38 +0000499 NonPHIUse = true;
500 }
501 if (!NonPHIUse)
502 return true;
503
504 // If SuccToSinkTo post dominates then also it may be profitable if MI
505 // can further profitably sinked into another block in next round.
506 bool BreakPHIEdge = false;
Patrik Hagglundd06de4b2014-12-04 10:36:42 +0000507 // FIXME - If finding successor is compile time expensive then cache results.
Devang Patelc2686882011-12-14 23:20:38 +0000508 if (MachineBasicBlock *MBB2 = FindSuccToSinkTo(MI, SuccToSinkTo, BreakPHIEdge))
509 return isProfitableToSinkTo(Reg, MI, SuccToSinkTo, MBB2);
510
511 // If SuccToSinkTo is final destination and it is a post dominator of current
512 // block then it is not profitable to sink MI into SuccToSinkTo block.
513 return false;
514}
515
Devang Patelb94c9a42011-12-08 21:48:01 +0000516/// FindSuccToSinkTo - Find a successor to sink this instruction to.
517MachineBasicBlock *MachineSinking::FindSuccToSinkTo(MachineInstr *MI,
Devang Patelc2686882011-12-14 23:20:38 +0000518 MachineBasicBlock *MBB,
519 bool &BreakPHIEdge) {
520
521 assert (MI && "Invalid MachineInstr!");
522 assert (MBB && "Invalid MachineBasicBlock!");
Jim Grosbach01edd682010-06-03 23:49:57 +0000523
Chris Lattnerf3edc092008-01-04 07:36:53 +0000524 // Loop over all the operands of the specified instruction. If there is
525 // anything we can't handle, bail out.
Jim Grosbach01edd682010-06-03 23:49:57 +0000526
Chris Lattnerf3edc092008-01-04 07:36:53 +0000527 // SuccToSinkTo - This is the successor to sink this instruction to, once we
528 // decide.
Craig Topperc0196b12014-04-14 00:51:57 +0000529 MachineBasicBlock *SuccToSinkTo = nullptr;
Chris Lattnerf3edc092008-01-04 07:36:53 +0000530 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
531 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000532 if (!MO.isReg()) continue; // Ignore non-register operands.
Jim Grosbach01edd682010-06-03 23:49:57 +0000533
Chris Lattnerf3edc092008-01-04 07:36:53 +0000534 unsigned Reg = MO.getReg();
535 if (Reg == 0) continue;
Jim Grosbach01edd682010-06-03 23:49:57 +0000536
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000537 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Dan Gohmana3176872009-09-25 22:53:29 +0000538 if (MO.isUse()) {
539 // If the physreg has no defs anywhere, it's just an ambient register
Dan Gohman2f5bdcb2009-09-26 02:34:00 +0000540 // and we can freely move its uses. Alternatively, if it's allocatable,
541 // it could get allocated to something with a def during allocation.
Jakob Stoklund Olesen86ae07f2012-01-16 22:34:08 +0000542 if (!MRI->isConstantPhysReg(Reg, *MBB->getParent()))
Craig Topperc0196b12014-04-14 00:51:57 +0000543 return nullptr;
Bill Wendlinge41e40f2010-06-25 20:48:10 +0000544 } else if (!MO.isDead()) {
545 // A def that isn't dead. We can't move it.
Craig Topperc0196b12014-04-14 00:51:57 +0000546 return nullptr;
Dan Gohmana3176872009-09-25 22:53:29 +0000547 }
Chris Lattnerf3edc092008-01-04 07:36:53 +0000548 } else {
549 // Virtual register uses are always safe to sink.
550 if (MO.isUse()) continue;
Evan Cheng47a65a12009-02-07 01:21:47 +0000551
552 // If it's not safe to move defs of the register class, then abort.
Evan Chenge53ab6d2010-09-17 22:28:18 +0000553 if (!TII->isSafeToMoveRegClassDefs(MRI->getRegClass(Reg)))
Craig Topperc0196b12014-04-14 00:51:57 +0000554 return nullptr;
Jim Grosbach01edd682010-06-03 23:49:57 +0000555
Chris Lattnerf3edc092008-01-04 07:36:53 +0000556 // Virtual register defs can only be sunk if all their uses are in blocks
557 // dominated by one of the successors.
558 if (SuccToSinkTo) {
559 // If a previous operand picked a block to sink to, then this operand
560 // must be sinkable to the same block.
Evan Cheng361b9be2010-08-19 18:33:29 +0000561 bool LocalUse = false;
Devang Patelc2686882011-12-14 23:20:38 +0000562 if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, MBB,
Evan Cheng2031b762010-09-20 19:12:55 +0000563 BreakPHIEdge, LocalUse))
Craig Topperc0196b12014-04-14 00:51:57 +0000564 return nullptr;
Bill Wendling7ee730e2010-06-02 23:04:26 +0000565
Chris Lattnerf3edc092008-01-04 07:36:53 +0000566 continue;
567 }
Jim Grosbach01edd682010-06-03 23:49:57 +0000568
Chris Lattnerf3edc092008-01-04 07:36:53 +0000569 // Otherwise, we should look at all the successors and decide which one
Bruno Cardoso Lopesd04f7592014-09-25 23:14:26 +0000570 // we should sink to. If we have reliable block frequency information
571 // (frequency != 0) available, give successors with smaller frequencies
572 // higher priority, otherwise prioritize smaller loop depths.
573 SmallVector<MachineBasicBlock*, 4> Succs(MBB->succ_begin(),
574 MBB->succ_end());
Patrik Hagglundd06de4b2014-12-04 10:36:42 +0000575
576 // Handle cases where sinking can happen but where the sink point isn't a
577 // successor. For example:
578 //
579 // x = computation
580 // if () {} else {}
581 // use x
582 //
583 const std::vector<MachineDomTreeNode *> &Children =
584 DT->getNode(MBB)->getChildren();
585 for (const auto &DTChild : Children)
586 // DomTree children of MBB that have MBB as immediate dominator are added.
587 if (DTChild->getIDom()->getBlock() == MI->getParent() &&
588 // Skip MBBs already added to the Succs vector above.
589 !MBB->isSuccessor(DTChild->getBlock()))
590 Succs.push_back(DTChild->getBlock());
591
Bruno Cardoso Lopesd04f7592014-09-25 23:14:26 +0000592 // Sort Successors according to their loop depth or block frequency info.
Benjamin Kramer3a377bc2014-03-01 11:47:00 +0000593 std::stable_sort(
594 Succs.begin(), Succs.end(),
Bruno Cardoso Lopesd04f7592014-09-25 23:14:26 +0000595 [this](const MachineBasicBlock *L, const MachineBasicBlock *R) {
596 uint64_t LHSFreq = MBFI ? MBFI->getBlockFreq(L).getFrequency() : 0;
597 uint64_t RHSFreq = MBFI ? MBFI->getBlockFreq(R).getFrequency() : 0;
598 bool HasBlockFreq = LHSFreq != 0 && RHSFreq != 0;
599 return HasBlockFreq ? LHSFreq < RHSFreq
600 : LI->getLoopDepth(L) < LI->getLoopDepth(R);
Benjamin Kramer3a377bc2014-03-01 11:47:00 +0000601 });
Craig Toppere1c1d362013-07-03 05:11:49 +0000602 for (SmallVectorImpl<MachineBasicBlock *>::iterator SI = Succs.begin(),
603 E = Succs.end(); SI != E; ++SI) {
Devang Patelc2686882011-12-14 23:20:38 +0000604 MachineBasicBlock *SuccBlock = *SI;
Evan Cheng361b9be2010-08-19 18:33:29 +0000605 bool LocalUse = false;
Devang Patelc2686882011-12-14 23:20:38 +0000606 if (AllUsesDominatedByBlock(Reg, SuccBlock, MBB,
Evan Cheng2031b762010-09-20 19:12:55 +0000607 BreakPHIEdge, LocalUse)) {
Devang Patel1a3c1692011-12-08 21:33:23 +0000608 SuccToSinkTo = SuccBlock;
Chris Lattnerf3edc092008-01-04 07:36:53 +0000609 break;
610 }
Evan Cheng25b60682010-08-18 23:09:25 +0000611 if (LocalUse)
612 // Def is used locally, it's never safe to move this def.
Craig Topperc0196b12014-04-14 00:51:57 +0000613 return nullptr;
Chris Lattnerf3edc092008-01-04 07:36:53 +0000614 }
Jim Grosbach01edd682010-06-03 23:49:57 +0000615
Chris Lattnerf3edc092008-01-04 07:36:53 +0000616 // If we couldn't find a block to sink to, ignore this instruction.
Craig Topperc0196b12014-04-14 00:51:57 +0000617 if (!SuccToSinkTo)
618 return nullptr;
619 if (!isProfitableToSinkTo(Reg, MI, MBB, SuccToSinkTo))
620 return nullptr;
Chris Lattnerf3edc092008-01-04 07:36:53 +0000621 }
622 }
Devang Patel202cf2f2011-12-08 23:52:00 +0000623
624 // It is not possible to sink an instruction into its own block. This can
625 // happen with loops.
Devang Patelc2686882011-12-14 23:20:38 +0000626 if (MBB == SuccToSinkTo)
Craig Topperc0196b12014-04-14 00:51:57 +0000627 return nullptr;
Devang Patel202cf2f2011-12-08 23:52:00 +0000628
629 // It's not safe to sink instructions to EH landing pad. Control flow into
630 // landing pad is implicitly defined.
631 if (SuccToSinkTo && SuccToSinkTo->isLandingPad())
Craig Topperc0196b12014-04-14 00:51:57 +0000632 return nullptr;
Devang Patel202cf2f2011-12-08 23:52:00 +0000633
Devang Patelb94c9a42011-12-08 21:48:01 +0000634 return SuccToSinkTo;
635}
636
637/// SinkInstruction - Determine whether it is safe to sink the specified machine
638/// instruction out of its current block into a successor.
639bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) {
640 // Don't sink insert_subreg, subreg_to_reg, reg_sequence. These are meant to
641 // be close to the source to make it easier to coalesce.
642 if (AvoidsSinking(MI, MRI))
643 return false;
644
645 // Check if it's safe to move the instruction.
646 if (!MI->isSafeToMove(TII, AA, SawStore))
647 return false;
648
649 // FIXME: This should include support for sinking instructions within the
650 // block they are currently in to shorten the live ranges. We often get
651 // instructions sunk into the top of a large block, but it would be better to
652 // also sink them down before their first use in the block. This xform has to
653 // be careful not to *increase* register pressure though, e.g. sinking
654 // "x = y + z" down if it kills y and z would increase the live ranges of y
655 // and z and only shrink the live range of x.
656
657 bool BreakPHIEdge = false;
Devang Patelc2686882011-12-14 23:20:38 +0000658 MachineBasicBlock *ParentBlock = MI->getParent();
Pete Cooperff5064a2015-05-08 17:54:29 +0000659 MachineBasicBlock *SuccToSinkTo = FindSuccToSinkTo(MI, ParentBlock,
660 BreakPHIEdge);
Jim Grosbach01edd682010-06-03 23:49:57 +0000661
Chris Lattner6ec78272008-01-05 01:39:17 +0000662 // If there are no outputs, it must have side-effects.
Craig Topperc0196b12014-04-14 00:51:57 +0000663 if (!SuccToSinkTo)
Chris Lattner6ec78272008-01-05 01:39:17 +0000664 return false;
Evan Cheng25104362009-02-15 08:36:12 +0000665
Bill Wendlingf82aea62010-06-03 07:54:20 +0000666
Daniel Dunbaref5a4382010-06-23 00:48:25 +0000667 // If the instruction to move defines a dead physical register which is live
668 // when leaving the basic block, don't move it because it could turn into a
669 // "zombie" define of that preg. E.g., EFLAGS. (<rdar://problem/8030636>)
Bill Wendlinge41e40f2010-06-25 20:48:10 +0000670 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
671 const MachineOperand &MO = MI->getOperand(I);
672 if (!MO.isReg()) continue;
673 unsigned Reg = MO.getReg();
674 if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
675 if (SuccToSinkTo->isLiveIn(Reg))
Bill Wendlingf82aea62010-06-03 07:54:20 +0000676 return false;
Bill Wendlinge41e40f2010-06-25 20:48:10 +0000677 }
Bill Wendlingf82aea62010-06-03 07:54:20 +0000678
Bill Wendling7ee730e2010-06-02 23:04:26 +0000679 DEBUG(dbgs() << "Sink instr " << *MI << "\tinto block " << *SuccToSinkTo);
680
Will Dietz5cb7f4e2013-10-14 16:57:17 +0000681 // If the block has multiple predecessors, this is a critical edge.
682 // Decide if we can sink along it or need to break the edge.
Chris Lattnerf3edc092008-01-04 07:36:53 +0000683 if (SuccToSinkTo->pred_size() > 1) {
Jakob Stoklund Olesen20b71e22010-04-13 19:06:14 +0000684 // We cannot sink a load across a critical edge - there may be stores in
685 // other code paths.
Evan Chengae9939c2010-08-19 17:33:11 +0000686 bool TryBreak = false;
Jakob Stoklund Olesen20b71e22010-04-13 19:06:14 +0000687 bool store = true;
688 if (!MI->isSafeToMove(TII, AA, store)) {
Evan Chenge5af9302010-08-19 23:33:02 +0000689 DEBUG(dbgs() << " *** NOTE: Won't sink load along critical edge.\n");
Evan Chengae9939c2010-08-19 17:33:11 +0000690 TryBreak = true;
Jakob Stoklund Olesen20b71e22010-04-13 19:06:14 +0000691 }
692
693 // We don't want to sink across a critical edge if we don't dominate the
694 // successor. We could be introducing calculations to new code paths.
Evan Chengae9939c2010-08-19 17:33:11 +0000695 if (!TryBreak && !DT->dominates(ParentBlock, SuccToSinkTo)) {
Evan Chenge5af9302010-08-19 23:33:02 +0000696 DEBUG(dbgs() << " *** NOTE: Critical edge found\n");
Evan Chengae9939c2010-08-19 17:33:11 +0000697 TryBreak = true;
Jakob Stoklund Olesen20b71e22010-04-13 19:06:14 +0000698 }
699
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +0000700 // Don't sink instructions into a loop.
Evan Chengae9939c2010-08-19 17:33:11 +0000701 if (!TryBreak && LI->isLoopHeader(SuccToSinkTo)) {
Evan Chenge5af9302010-08-19 23:33:02 +0000702 DEBUG(dbgs() << " *** NOTE: Loop header found\n");
Evan Chengae9939c2010-08-19 17:33:11 +0000703 TryBreak = true;
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +0000704 }
705
Jakob Stoklund Olesen20b71e22010-04-13 19:06:14 +0000706 // Otherwise we are OK with sinking along a critical edge.
Evan Chengae9939c2010-08-19 17:33:11 +0000707 if (!TryBreak)
708 DEBUG(dbgs() << "Sinking along critical edge.\n");
709 else {
Quentin Colombet5cded892014-08-11 23:52:01 +0000710 // Mark this edge as to be split.
711 // If the edge can actually be split, the next iteration of the main loop
712 // will sink MI in the newly created block.
713 bool Status =
714 PostponeSplitCriticalEdge(MI, ParentBlock, SuccToSinkTo, BreakPHIEdge);
715 if (!Status)
Evan Chenge53ab6d2010-09-17 22:28:18 +0000716 DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
Quentin Colombet5cded892014-08-11 23:52:01 +0000717 "break critical edge\n");
718 // The instruction will not be sunk this time.
719 return false;
Evan Chengae9939c2010-08-19 17:33:11 +0000720 }
Chris Lattnerf3edc092008-01-04 07:36:53 +0000721 }
Jim Grosbach01edd682010-06-03 23:49:57 +0000722
Evan Cheng2031b762010-09-20 19:12:55 +0000723 if (BreakPHIEdge) {
724 // BreakPHIEdge is true if all the uses are in the successor MBB being
725 // sunken into and they are all PHI nodes. In this case, machine-sink must
726 // break the critical edge first.
Quentin Colombet5cded892014-08-11 23:52:01 +0000727 bool Status = PostponeSplitCriticalEdge(MI, ParentBlock,
728 SuccToSinkTo, BreakPHIEdge);
729 if (!Status)
Evan Chengb339f3d2010-09-18 06:42:17 +0000730 DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
731 "break critical edge\n");
Quentin Colombet5cded892014-08-11 23:52:01 +0000732 // The instruction will not be sunk this time.
733 return false;
Evan Chengb339f3d2010-09-18 06:42:17 +0000734 }
735
Bill Wendling7ee730e2010-06-02 23:04:26 +0000736 // Determine where to insert into. Skip phi nodes.
Chris Lattnerf3edc092008-01-04 07:36:53 +0000737 MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
Evan Chengb339f3d2010-09-18 06:42:17 +0000738 while (InsertPos != SuccToSinkTo->end() && InsertPos->isPHI())
Chris Lattnerf3edc092008-01-04 07:36:53 +0000739 ++InsertPos;
Jim Grosbach01edd682010-06-03 23:49:57 +0000740
Devang Patel9de7a7d2011-09-07 00:07:58 +0000741 // collect matching debug values.
742 SmallVector<MachineInstr *, 2> DbgValuesToSink;
743 collectDebugValues(MI, DbgValuesToSink);
744
Chris Lattnerf3edc092008-01-04 07:36:53 +0000745 // Move the instruction.
746 SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
747 ++MachineBasicBlock::iterator(MI));
Dan Gohmanc90f51c2010-05-13 20:34:42 +0000748
Devang Patel9de7a7d2011-09-07 00:07:58 +0000749 // Move debug values.
Craig Toppere1c1d362013-07-03 05:11:49 +0000750 for (SmallVectorImpl<MachineInstr *>::iterator DBI = DbgValuesToSink.begin(),
Devang Patel9de7a7d2011-09-07 00:07:58 +0000751 DBE = DbgValuesToSink.end(); DBI != DBE; ++DBI) {
752 MachineInstr *DbgMI = *DBI;
753 SuccToSinkTo->splice(InsertPos, ParentBlock, DbgMI,
754 ++MachineBasicBlock::iterator(DbgMI));
755 }
756
Juergen Ributzka4bea4942014-09-04 02:07:36 +0000757 // Conservatively, clear any kill flags, since it's possible that they are no
758 // longer correct.
Pete Cooper85b1c482015-05-08 17:54:32 +0000759 // Note that we have to clear the kill flags for any register this instruction
760 // uses as we may sink over another instruction which currently kills the
761 // used registers.
762 for (MachineOperand &MO : MI->operands()) {
763 if (MO.isReg() && MO.isUse())
764 MRI->clearKillFlags(MO.getReg());
765 }
Dan Gohmanc90f51c2010-05-13 20:34:42 +0000766
Chris Lattnerf3edc092008-01-04 07:36:53 +0000767 return true;
768}