blob: 5f03390f146ce95d29b9122d32547cf7242e9b1b [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"
Matthias Braun352b89c2015-05-16 03:11:07 +000022#include "llvm/ADT/SparseBitVector.h"
Chris Lattnerf3edc092008-01-04 07:36:53 +000023#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Analysis/AliasAnalysis.h"
Bruno Cardoso Lopesd04f7592014-09-25 23:14:26 +000025#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/CodeGen/MachineDominators.h"
27#include "llvm/CodeGen/MachineLoopInfo.h"
Jingyue Wu29542802014-10-15 03:27:43 +000028#include "llvm/CodeGen/MachinePostDominators.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengae9939c2010-08-19 17:33:11 +000030#include "llvm/Support/CommandLine.h"
Chris Lattnerf3edc092008-01-04 07:36:53 +000031#include "llvm/Support/Debug.h"
Bill Wendling63aa0002009-08-22 20:26:23 +000032#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/Target/TargetInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000035#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattnerf3edc092008-01-04 07:36:53 +000036using namespace llvm;
37
Chandler Carruth1b9dde02014-04-22 02:02:50 +000038#define DEBUG_TYPE "machine-sink"
39
Andrew Trick9e761992012-02-08 21:22:43 +000040static cl::opt<bool>
Evan Chengae9939c2010-08-19 17:33:11 +000041SplitEdges("machine-sink-split",
42 cl::desc("Split critical edges during machine sinking"),
Evan Chengf3e9a482010-09-20 22:52:00 +000043 cl::init(true), cl::Hidden);
Evan Chengae9939c2010-08-19 17:33:11 +000044
Bruno Cardoso Lopesd04f7592014-09-25 23:14:26 +000045static cl::opt<bool>
46UseBlockFreqInfo("machine-sink-bfi",
47 cl::desc("Use block frequency info to find successors to sink"),
48 cl::init(true), cl::Hidden);
49
50
Evan Chenge53ab6d2010-09-17 22:28:18 +000051STATISTIC(NumSunk, "Number of machine instructions sunk");
52STATISTIC(NumSplit, "Number of critical edges split");
53STATISTIC(NumCoalesces, "Number of copies coalesced");
Chris Lattnerf3edc092008-01-04 07:36:53 +000054
55namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000056 class MachineSinking : public MachineFunctionPass {
Chris Lattnerf3edc092008-01-04 07:36:53 +000057 const TargetInstrInfo *TII;
Dan Gohmana3176872009-09-25 22:53:29 +000058 const TargetRegisterInfo *TRI;
Jingyue Wu29542802014-10-15 03:27:43 +000059 MachineRegisterInfo *MRI; // Machine register information
60 MachineDominatorTree *DT; // Machine dominator tree
61 MachinePostDominatorTree *PDT; // Machine post dominator tree
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +000062 MachineLoopInfo *LI;
Bruno Cardoso Lopesd04f7592014-09-25 23:14:26 +000063 const MachineBlockFrequencyInfo *MBFI;
Dan Gohman87b02d52009-10-09 23:27:56 +000064 AliasAnalysis *AA;
Chris Lattnerf3edc092008-01-04 07:36:53 +000065
Evan Chenge53ab6d2010-09-17 22:28:18 +000066 // Remember which edges have been considered for breaking.
67 SmallSet<std::pair<MachineBasicBlock*,MachineBasicBlock*>, 8>
68 CEBCandidates;
Quentin Colombet5cded892014-08-11 23:52:01 +000069 // Remember which edges we are about to split.
70 // This is different from CEBCandidates since those edges
71 // will be split.
72 SetVector<std::pair<MachineBasicBlock*,MachineBasicBlock*> > ToSplit;
Evan Chenge53ab6d2010-09-17 22:28:18 +000073
Matthias Braun352b89c2015-05-16 03:11:07 +000074 SparseBitVector<> RegsToClearKillFlags;
75
Chris Lattnerf3edc092008-01-04 07:36:53 +000076 public:
77 static char ID; // Pass identification
Owen Anderson6c18d1a2010-10-19 17:21:58 +000078 MachineSinking() : MachineFunctionPass(ID) {
79 initializeMachineSinkingPass(*PassRegistry::getPassRegistry());
80 }
Jim Grosbach01edd682010-06-03 23:49:57 +000081
Craig Topper4584cd52014-03-07 09:26:03 +000082 bool runOnMachineFunction(MachineFunction &MF) override;
Jim Grosbach01edd682010-06-03 23:49:57 +000083
Craig Topper4584cd52014-03-07 09:26:03 +000084 void getAnalysisUsage(AnalysisUsage &AU) const override {
Dan Gohman04023152009-07-31 23:37:33 +000085 AU.setPreservesCFG();
Chris Lattnerf3edc092008-01-04 07:36:53 +000086 MachineFunctionPass::getAnalysisUsage(AU);
Dan Gohman87b02d52009-10-09 23:27:56 +000087 AU.addRequired<AliasAnalysis>();
Chris Lattnerf3edc092008-01-04 07:36:53 +000088 AU.addRequired<MachineDominatorTree>();
Jingyue Wu29542802014-10-15 03:27:43 +000089 AU.addRequired<MachinePostDominatorTree>();
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +000090 AU.addRequired<MachineLoopInfo>();
Chris Lattnerf3edc092008-01-04 07:36:53 +000091 AU.addPreserved<MachineDominatorTree>();
Jingyue Wu29542802014-10-15 03:27:43 +000092 AU.addPreserved<MachinePostDominatorTree>();
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +000093 AU.addPreserved<MachineLoopInfo>();
Bruno Cardoso Lopesd04f7592014-09-25 23:14:26 +000094 if (UseBlockFreqInfo)
95 AU.addRequired<MachineBlockFrequencyInfo>();
Chris Lattnerf3edc092008-01-04 07:36:53 +000096 }
Evan Chenge53ab6d2010-09-17 22:28:18 +000097
Craig Topper4584cd52014-03-07 09:26:03 +000098 void releaseMemory() override {
Evan Chenge53ab6d2010-09-17 22:28:18 +000099 CEBCandidates.clear();
100 }
101
Chris Lattnerf3edc092008-01-04 07:36:53 +0000102 private:
103 bool ProcessBlock(MachineBasicBlock &MBB);
Evan Chenge53ab6d2010-09-17 22:28:18 +0000104 bool isWorthBreakingCriticalEdge(MachineInstr *MI,
105 MachineBasicBlock *From,
106 MachineBasicBlock *To);
Quentin Colombet5cded892014-08-11 23:52:01 +0000107 /// \brief Postpone the splitting of the given critical
108 /// edge (\p From, \p To).
109 ///
110 /// We do not split the edges on the fly. Indeed, this invalidates
111 /// the dominance information and thus triggers a lot of updates
112 /// of that information underneath.
113 /// Instead, we postpone all the splits after each iteration of
114 /// the main loop. That way, the information is at least valid
115 /// for the lifetime of an iteration.
116 ///
117 /// \return True if the edge is marked as toSplit, false otherwise.
Patrik Hagglundd06de4b2014-12-04 10:36:42 +0000118 /// False can be returned if, for instance, this is not profitable.
Quentin Colombet5cded892014-08-11 23:52:01 +0000119 bool PostponeSplitCriticalEdge(MachineInstr *MI,
120 MachineBasicBlock *From,
121 MachineBasicBlock *To,
122 bool BreakPHIEdge);
Chris Lattner08af5a92008-01-12 00:17:41 +0000123 bool SinkInstruction(MachineInstr *MI, bool &SawStore);
Evan Cheng25b60682010-08-18 23:09:25 +0000124 bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB,
Evan Chenge53ab6d2010-09-17 22:28:18 +0000125 MachineBasicBlock *DefMBB,
Evan Cheng2031b762010-09-20 19:12:55 +0000126 bool &BreakPHIEdge, bool &LocalUse) const;
Devang Patelc2686882011-12-14 23:20:38 +0000127 MachineBasicBlock *FindSuccToSinkTo(MachineInstr *MI, MachineBasicBlock *MBB,
128 bool &BreakPHIEdge);
Andrew Trick9e761992012-02-08 21:22:43 +0000129 bool isProfitableToSinkTo(unsigned Reg, MachineInstr *MI,
Devang Patelc2686882011-12-14 23:20:38 +0000130 MachineBasicBlock *MBB,
131 MachineBasicBlock *SuccToSinkTo);
Devang Patelb94c9a42011-12-08 21:48:01 +0000132
Evan Chenge53ab6d2010-09-17 22:28:18 +0000133 bool PerformTrivialForwardCoalescing(MachineInstr *MI,
134 MachineBasicBlock *MBB);
Chris Lattnerf3edc092008-01-04 07:36:53 +0000135 };
Chris Lattnerf3edc092008-01-04 07:36:53 +0000136} // end anonymous namespace
Jim Grosbach01edd682010-06-03 23:49:57 +0000137
Dan Gohmand78c4002008-05-13 00:00:25 +0000138char MachineSinking::ID = 0;
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000139char &llvm::MachineSinkingID = MachineSinking::ID;
Owen Anderson8ac477f2010-10-12 19:48:12 +0000140INITIALIZE_PASS_BEGIN(MachineSinking, "machine-sink",
141 "Machine code sinking", false, false)
142INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
143INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
144INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
145INITIALIZE_PASS_END(MachineSinking, "machine-sink",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000146 "Machine code sinking", false, false)
Chris Lattnerf3edc092008-01-04 07:36:53 +0000147
Evan Chenge53ab6d2010-09-17 22:28:18 +0000148bool MachineSinking::PerformTrivialForwardCoalescing(MachineInstr *MI,
149 MachineBasicBlock *MBB) {
150 if (!MI->isCopy())
151 return false;
152
153 unsigned SrcReg = MI->getOperand(1).getReg();
154 unsigned DstReg = MI->getOperand(0).getReg();
155 if (!TargetRegisterInfo::isVirtualRegister(SrcReg) ||
156 !TargetRegisterInfo::isVirtualRegister(DstReg) ||
157 !MRI->hasOneNonDBGUse(SrcReg))
158 return false;
159
160 const TargetRegisterClass *SRC = MRI->getRegClass(SrcReg);
161 const TargetRegisterClass *DRC = MRI->getRegClass(DstReg);
162 if (SRC != DRC)
163 return false;
164
165 MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
166 if (DefMI->isCopyLike())
167 return false;
168 DEBUG(dbgs() << "Coalescing: " << *DefMI);
169 DEBUG(dbgs() << "*** to: " << *MI);
170 MRI->replaceRegWith(DstReg, SrcReg);
171 MI->eraseFromParent();
Patrik Hagglund57d315b2014-09-09 07:47:00 +0000172
173 // Conservatively, clear any kill flags, since it's possible that they are no
174 // longer correct.
175 MRI->clearKillFlags(SrcReg);
176
Evan Chenge53ab6d2010-09-17 22:28:18 +0000177 ++NumCoalesces;
178 return true;
179}
180
Chris Lattnerf3edc092008-01-04 07:36:53 +0000181/// AllUsesDominatedByBlock - Return true if all uses of the specified register
Evan Cheng25b60682010-08-18 23:09:25 +0000182/// occur in blocks dominated by the specified block. If any use is in the
183/// definition block, then return false since it is never legal to move def
184/// after uses.
Evan Chenge53ab6d2010-09-17 22:28:18 +0000185bool
186MachineSinking::AllUsesDominatedByBlock(unsigned Reg,
187 MachineBasicBlock *MBB,
188 MachineBasicBlock *DefMBB,
Evan Cheng2031b762010-09-20 19:12:55 +0000189 bool &BreakPHIEdge,
190 bool &LocalUse) const {
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000191 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
192 "Only makes sense for vregs");
Evan Chengb339f3d2010-09-18 06:42:17 +0000193
Devang Patel706574a2011-12-09 01:25:04 +0000194 // Ignore debug uses because debug info doesn't affect the code.
Evan Chengb339f3d2010-09-18 06:42:17 +0000195 if (MRI->use_nodbg_empty(Reg))
196 return true;
197
Evan Cheng2031b762010-09-20 19:12:55 +0000198 // BreakPHIEdge is true if all the uses are in the successor MBB being sunken
199 // into and they are all PHI nodes. In this case, machine-sink must break
200 // the critical edge first. e.g.
201 //
Evan Chengb339f3d2010-09-18 06:42:17 +0000202 // BB#1: derived from LLVM BB %bb4.preheader
203 // Predecessors according to CFG: BB#0
204 // ...
205 // %reg16385<def> = DEC64_32r %reg16437, %EFLAGS<imp-def,dead>
206 // ...
207 // JE_4 <BB#37>, %EFLAGS<imp-use>
208 // Successors according to CFG: BB#37 BB#2
209 //
210 // BB#2: derived from LLVM BB %bb.nph
211 // Predecessors according to CFG: BB#0 BB#1
212 // %reg16386<def> = PHI %reg16434, <BB#0>, %reg16385, <BB#1>
Evan Cheng2031b762010-09-20 19:12:55 +0000213 BreakPHIEdge = true;
Owen Andersonb36376e2014-03-17 19:36:09 +0000214 for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
215 MachineInstr *UseInst = MO.getParent();
216 unsigned OpNo = &MO - &UseInst->getOperand(0);
Evan Chengb339f3d2010-09-18 06:42:17 +0000217 MachineBasicBlock *UseBlock = UseInst->getParent();
218 if (!(UseBlock == MBB && UseInst->isPHI() &&
Owen Andersonb36376e2014-03-17 19:36:09 +0000219 UseInst->getOperand(OpNo+1).getMBB() == DefMBB)) {
Evan Cheng2031b762010-09-20 19:12:55 +0000220 BreakPHIEdge = false;
Evan Chengb339f3d2010-09-18 06:42:17 +0000221 break;
222 }
223 }
Evan Cheng2031b762010-09-20 19:12:55 +0000224 if (BreakPHIEdge)
Evan Chengb339f3d2010-09-18 06:42:17 +0000225 return true;
226
Owen Andersonb36376e2014-03-17 19:36:09 +0000227 for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
Chris Lattnerf3edc092008-01-04 07:36:53 +0000228 // Determine the block of the use.
Owen Andersonb36376e2014-03-17 19:36:09 +0000229 MachineInstr *UseInst = MO.getParent();
230 unsigned OpNo = &MO - &UseInst->getOperand(0);
Chris Lattnerf3edc092008-01-04 07:36:53 +0000231 MachineBasicBlock *UseBlock = UseInst->getParent();
Evan Chengb339f3d2010-09-18 06:42:17 +0000232 if (UseInst->isPHI()) {
Chris Lattnerf3edc092008-01-04 07:36:53 +0000233 // PHI nodes use the operand in the predecessor block, not the block with
234 // the PHI.
Owen Andersonb36376e2014-03-17 19:36:09 +0000235 UseBlock = UseInst->getOperand(OpNo+1).getMBB();
Evan Cheng361b9be2010-08-19 18:33:29 +0000236 } else if (UseBlock == DefMBB) {
237 LocalUse = true;
238 return false;
Chris Lattnerf3edc092008-01-04 07:36:53 +0000239 }
Bill Wendling7ee730e2010-06-02 23:04:26 +0000240
Chris Lattnerf3edc092008-01-04 07:36:53 +0000241 // Check that it dominates.
242 if (!DT->dominates(MBB, UseBlock))
243 return false;
244 }
Bill Wendling7ee730e2010-06-02 23:04:26 +0000245
Chris Lattnerf3edc092008-01-04 07:36:53 +0000246 return true;
247}
248
Chris Lattnerf3edc092008-01-04 07:36:53 +0000249bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
Paul Robinson7c99ec52014-03-31 17:43:35 +0000250 if (skipOptnoneFunction(*MF.getFunction()))
251 return false;
252
David Greene4b7aa242010-01-05 01:26:00 +0000253 DEBUG(dbgs() << "******** Machine Sinking ********\n");
Jim Grosbach01edd682010-06-03 23:49:57 +0000254
Eric Christophereb9e87f2014-10-14 07:00:33 +0000255 TII = MF.getSubtarget().getInstrInfo();
256 TRI = MF.getSubtarget().getRegisterInfo();
Evan Chenge53ab6d2010-09-17 22:28:18 +0000257 MRI = &MF.getRegInfo();
Chris Lattnerf3edc092008-01-04 07:36:53 +0000258 DT = &getAnalysis<MachineDominatorTree>();
Jingyue Wu29542802014-10-15 03:27:43 +0000259 PDT = &getAnalysis<MachinePostDominatorTree>();
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +0000260 LI = &getAnalysis<MachineLoopInfo>();
Bruno Cardoso Lopesd04f7592014-09-25 23:14:26 +0000261 MBFI = UseBlockFreqInfo ? &getAnalysis<MachineBlockFrequencyInfo>() : nullptr;
Dan Gohman87b02d52009-10-09 23:27:56 +0000262 AA = &getAnalysis<AliasAnalysis>();
Chris Lattnerf3edc092008-01-04 07:36:53 +0000263
264 bool EverMadeChange = false;
Jim Grosbach01edd682010-06-03 23:49:57 +0000265
Chris Lattnerf3edc092008-01-04 07:36:53 +0000266 while (1) {
267 bool MadeChange = false;
268
269 // Process all basic blocks.
Evan Chenge53ab6d2010-09-17 22:28:18 +0000270 CEBCandidates.clear();
Quentin Colombet5cded892014-08-11 23:52:01 +0000271 ToSplit.clear();
Jim Grosbach01edd682010-06-03 23:49:57 +0000272 for (MachineFunction::iterator I = MF.begin(), E = MF.end();
Chris Lattnerf3edc092008-01-04 07:36:53 +0000273 I != E; ++I)
274 MadeChange |= ProcessBlock(*I);
Jim Grosbach01edd682010-06-03 23:49:57 +0000275
Quentin Colombet5cded892014-08-11 23:52:01 +0000276 // If we have anything we marked as toSplit, split it now.
277 for (auto &Pair : ToSplit) {
278 auto NewSucc = Pair.first->SplitCriticalEdge(Pair.second, this);
279 if (NewSucc != nullptr) {
280 DEBUG(dbgs() << " *** Splitting critical edge:"
281 " BB#" << Pair.first->getNumber()
282 << " -- BB#" << NewSucc->getNumber()
283 << " -- BB#" << Pair.second->getNumber() << '\n');
284 MadeChange = true;
285 ++NumSplit;
286 } else
287 DEBUG(dbgs() << " *** Not legal to break critical edge\n");
288 }
Chris Lattnerf3edc092008-01-04 07:36:53 +0000289 // If this iteration over the code changed anything, keep iterating.
290 if (!MadeChange) break;
291 EverMadeChange = true;
Jim Grosbach01edd682010-06-03 23:49:57 +0000292 }
Matthias Braun352b89c2015-05-16 03:11:07 +0000293
294 // Now clear any kill flags for recorded registers.
295 for (auto I : RegsToClearKillFlags)
296 MRI->clearKillFlags(I);
297 RegsToClearKillFlags.clear();
298
Chris Lattnerf3edc092008-01-04 07:36:53 +0000299 return EverMadeChange;
300}
301
302bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
Chris Lattnerf3edc092008-01-04 07:36:53 +0000303 // Can't sink anything out of a block that has less than two successors.
Chris Lattner30c3de62009-04-10 16:38:36 +0000304 if (MBB.succ_size() <= 1 || MBB.empty()) return false;
305
Dan Gohman918a90a2010-04-05 19:17:22 +0000306 // Don't bother sinking code out of unreachable blocks. In addition to being
Jim Grosbach01edd682010-06-03 23:49:57 +0000307 // unprofitable, it can also lead to infinite looping, because in an
308 // unreachable loop there may be nowhere to stop.
Dan Gohman918a90a2010-04-05 19:17:22 +0000309 if (!DT->isReachableFromEntry(&MBB)) return false;
310
Chris Lattner30c3de62009-04-10 16:38:36 +0000311 bool MadeChange = false;
312
Chris Lattner08af5a92008-01-12 00:17:41 +0000313 // Walk the basic block bottom-up. Remember if we saw a store.
Chris Lattner30c3de62009-04-10 16:38:36 +0000314 MachineBasicBlock::iterator I = MBB.end();
315 --I;
316 bool ProcessedBegin, SawStore = false;
317 do {
318 MachineInstr *MI = I; // The instruction to sink.
Jim Grosbach01edd682010-06-03 23:49:57 +0000319
Chris Lattner30c3de62009-04-10 16:38:36 +0000320 // Predecrement I (if it's not begin) so that it isn't invalidated by
321 // sinking.
322 ProcessedBegin = I == MBB.begin();
323 if (!ProcessedBegin)
324 --I;
Dale Johannesen2061c842010-03-05 00:02:59 +0000325
326 if (MI->isDebugValue())
327 continue;
328
Evan Chengfe917ef2011-04-11 18:47:20 +0000329 bool Joined = PerformTrivialForwardCoalescing(MI, &MBB);
330 if (Joined) {
331 MadeChange = true;
Evan Chenge53ab6d2010-09-17 22:28:18 +0000332 continue;
Evan Chengfe917ef2011-04-11 18:47:20 +0000333 }
Evan Chenge53ab6d2010-09-17 22:28:18 +0000334
Chris Lattner30c3de62009-04-10 16:38:36 +0000335 if (SinkInstruction(MI, SawStore))
336 ++NumSunk, MadeChange = true;
Jim Grosbach01edd682010-06-03 23:49:57 +0000337
Chris Lattner30c3de62009-04-10 16:38:36 +0000338 // If we just processed the first instruction in the block, we're done.
339 } while (!ProcessedBegin);
Jim Grosbach01edd682010-06-03 23:49:57 +0000340
Chris Lattnerf3edc092008-01-04 07:36:53 +0000341 return MadeChange;
342}
343
Evan Chenge53ab6d2010-09-17 22:28:18 +0000344bool MachineSinking::isWorthBreakingCriticalEdge(MachineInstr *MI,
345 MachineBasicBlock *From,
346 MachineBasicBlock *To) {
347 // FIXME: Need much better heuristics.
348
349 // If the pass has already considered breaking this edge (during this pass
350 // through the function), then let's go ahead and break it. This means
351 // sinking multiple "cheap" instructions into the same block.
David Blaikie70573dc2014-11-19 07:49:26 +0000352 if (!CEBCandidates.insert(std::make_pair(From, To)).second)
Evan Chenge53ab6d2010-09-17 22:28:18 +0000353 return true;
354
Jiangning Liuc3053122014-07-29 01:55:19 +0000355 if (!MI->isCopy() && !TII->isAsCheapAsAMove(MI))
Evan Chenge53ab6d2010-09-17 22:28:18 +0000356 return true;
357
358 // MI is cheap, we probably don't want to break the critical edge for it.
359 // However, if this would allow some definitions of its source operands
360 // to be sunk then it's probably worth it.
361 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
362 const MachineOperand &MO = MI->getOperand(i);
Will Dietz5cb7f4e2013-10-14 16:57:17 +0000363 if (!MO.isReg() || !MO.isUse())
Evan Chenge53ab6d2010-09-17 22:28:18 +0000364 continue;
Will Dietz5cb7f4e2013-10-14 16:57:17 +0000365 unsigned Reg = MO.getReg();
366 if (Reg == 0)
367 continue;
368
369 // We don't move live definitions of physical registers,
370 // so sinking their uses won't enable any opportunities.
371 if (TargetRegisterInfo::isPhysicalRegister(Reg))
372 continue;
373
374 // If this instruction is the only user of a virtual register,
375 // check if breaking the edge will enable sinking
376 // both this instruction and the defining instruction.
377 if (MRI->hasOneNonDBGUse(Reg)) {
378 // If the definition resides in same MBB,
379 // claim it's likely we can sink these together.
380 // If definition resides elsewhere, we aren't
381 // blocking it from being sunk so don't break the edge.
382 MachineInstr *DefMI = MRI->getVRegDef(Reg);
383 if (DefMI->getParent() == MI->getParent())
384 return true;
385 }
Evan Chenge53ab6d2010-09-17 22:28:18 +0000386 }
387
388 return false;
389}
390
Quentin Colombet5cded892014-08-11 23:52:01 +0000391bool MachineSinking::PostponeSplitCriticalEdge(MachineInstr *MI,
392 MachineBasicBlock *FromBB,
393 MachineBasicBlock *ToBB,
394 bool BreakPHIEdge) {
Evan Chenge53ab6d2010-09-17 22:28:18 +0000395 if (!isWorthBreakingCriticalEdge(MI, FromBB, ToBB))
Quentin Colombet5cded892014-08-11 23:52:01 +0000396 return false;
Evan Chenge53ab6d2010-09-17 22:28:18 +0000397
Evan Chengae9939c2010-08-19 17:33:11 +0000398 // Avoid breaking back edge. From == To means backedge for single BB loop.
Evan Chengf3e9a482010-09-20 22:52:00 +0000399 if (!SplitEdges || FromBB == ToBB)
Quentin Colombet5cded892014-08-11 23:52:01 +0000400 return false;
Evan Chengae9939c2010-08-19 17:33:11 +0000401
Evan Chenge53ab6d2010-09-17 22:28:18 +0000402 // Check for backedges of more "complex" loops.
403 if (LI->getLoopFor(FromBB) == LI->getLoopFor(ToBB) &&
404 LI->isLoopHeader(ToBB))
Quentin Colombet5cded892014-08-11 23:52:01 +0000405 return false;
Evan Chenge53ab6d2010-09-17 22:28:18 +0000406
407 // It's not always legal to break critical edges and sink the computation
408 // to the edge.
409 //
410 // BB#1:
411 // v1024
412 // Beq BB#3
413 // <fallthrough>
414 // BB#2:
415 // ... no uses of v1024
416 // <fallthrough>
417 // BB#3:
418 // ...
419 // = v1024
420 //
421 // If BB#1 -> BB#3 edge is broken and computation of v1024 is inserted:
422 //
423 // BB#1:
424 // ...
425 // Bne BB#2
426 // BB#4:
427 // v1024 =
428 // B BB#3
429 // BB#2:
430 // ... no uses of v1024
431 // <fallthrough>
432 // BB#3:
433 // ...
434 // = v1024
435 //
436 // This is incorrect since v1024 is not computed along the BB#1->BB#2->BB#3
437 // flow. We need to ensure the new basic block where the computation is
438 // sunk to dominates all the uses.
439 // It's only legal to break critical edge and sink the computation to the
440 // new block if all the predecessors of "To", except for "From", are
441 // not dominated by "From". Given SSA property, this means these
442 // predecessors are dominated by "To".
443 //
444 // There is no need to do this check if all the uses are PHI nodes. PHI
445 // sources are only defined on the specific predecessor edges.
Evan Cheng2031b762010-09-20 19:12:55 +0000446 if (!BreakPHIEdge) {
Evan Chengae9939c2010-08-19 17:33:11 +0000447 for (MachineBasicBlock::pred_iterator PI = ToBB->pred_begin(),
448 E = ToBB->pred_end(); PI != E; ++PI) {
449 if (*PI == FromBB)
450 continue;
451 if (!DT->dominates(ToBB, *PI))
Quentin Colombet5cded892014-08-11 23:52:01 +0000452 return false;
Evan Chengae9939c2010-08-19 17:33:11 +0000453 }
Evan Chengae9939c2010-08-19 17:33:11 +0000454 }
455
Quentin Colombet5cded892014-08-11 23:52:01 +0000456 ToSplit.insert(std::make_pair(FromBB, ToBB));
457
458 return true;
Evan Chengae9939c2010-08-19 17:33:11 +0000459}
460
Evan Chengd4b31a72010-09-23 06:53:00 +0000461static bool AvoidsSinking(MachineInstr *MI, MachineRegisterInfo *MRI) {
462 return MI->isInsertSubreg() || MI->isSubregToReg() || MI->isRegSequence();
463}
464
Andrew Trick9e761992012-02-08 21:22:43 +0000465/// collectDebgValues - Scan instructions following MI and collect any
Devang Patel9de7a7d2011-09-07 00:07:58 +0000466/// matching DBG_VALUEs.
Andrew Trick9e761992012-02-08 21:22:43 +0000467static void collectDebugValues(MachineInstr *MI,
Craig Topperb94011f2013-07-14 04:42:23 +0000468 SmallVectorImpl<MachineInstr *> &DbgValues) {
Devang Patel9de7a7d2011-09-07 00:07:58 +0000469 DbgValues.clear();
470 if (!MI->getOperand(0).isReg())
471 return;
472
473 MachineBasicBlock::iterator DI = MI; ++DI;
474 for (MachineBasicBlock::iterator DE = MI->getParent()->end();
475 DI != DE; ++DI) {
476 if (!DI->isDebugValue())
477 return;
478 if (DI->getOperand(0).isReg() &&
479 DI->getOperand(0).getReg() == MI->getOperand(0).getReg())
480 DbgValues.push_back(DI);
481 }
482}
483
Devang Patelc2686882011-12-14 23:20:38 +0000484/// isProfitableToSinkTo - Return true if it is profitable to sink MI.
Andrew Trick9e761992012-02-08 21:22:43 +0000485bool MachineSinking::isProfitableToSinkTo(unsigned Reg, MachineInstr *MI,
Devang Patelc2686882011-12-14 23:20:38 +0000486 MachineBasicBlock *MBB,
487 MachineBasicBlock *SuccToSinkTo) {
488 assert (MI && "Invalid MachineInstr!");
489 assert (SuccToSinkTo && "Invalid SinkTo Candidate BB");
490
491 if (MBB == SuccToSinkTo)
492 return false;
493
494 // It is profitable if SuccToSinkTo does not post dominate current block.
Jingyue Wu29542802014-10-15 03:27:43 +0000495 if (!PDT->dominates(SuccToSinkTo, MBB))
496 return true;
497
498 // It is profitable to sink an instruction from a deeper loop to a shallower
499 // loop, even if the latter post-dominates the former (PR21115).
500 if (LI->getLoopDepth(MBB) > LI->getLoopDepth(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;
Patrik Hagglundd06de4b2014-12-04 10:36:42 +0000516 // FIXME - If finding successor is compile time expensive then cache results.
Devang Patelc2686882011-12-14 23:20:38 +0000517 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 Lattnerf3edc092008-01-04 07:36:53 +0000565 // Virtual register defs can only be sunk if all their uses are in blocks
566 // dominated by one of the successors.
567 if (SuccToSinkTo) {
568 // If a previous operand picked a block to sink to, then this operand
569 // must be sinkable to the same block.
Evan Cheng361b9be2010-08-19 18:33:29 +0000570 bool LocalUse = false;
Devang Patelc2686882011-12-14 23:20:38 +0000571 if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, MBB,
Evan Cheng2031b762010-09-20 19:12:55 +0000572 BreakPHIEdge, LocalUse))
Craig Topperc0196b12014-04-14 00:51:57 +0000573 return nullptr;
Bill Wendling7ee730e2010-06-02 23:04:26 +0000574
Chris Lattnerf3edc092008-01-04 07:36:53 +0000575 continue;
576 }
Jim Grosbach01edd682010-06-03 23:49:57 +0000577
Chris Lattnerf3edc092008-01-04 07:36:53 +0000578 // Otherwise, we should look at all the successors and decide which one
Bruno Cardoso Lopesd04f7592014-09-25 23:14:26 +0000579 // we should sink to. If we have reliable block frequency information
580 // (frequency != 0) available, give successors with smaller frequencies
581 // higher priority, otherwise prioritize smaller loop depths.
582 SmallVector<MachineBasicBlock*, 4> Succs(MBB->succ_begin(),
583 MBB->succ_end());
Patrik Hagglundd06de4b2014-12-04 10:36:42 +0000584
585 // Handle cases where sinking can happen but where the sink point isn't a
586 // successor. For example:
587 //
588 // x = computation
589 // if () {} else {}
590 // use x
591 //
592 const std::vector<MachineDomTreeNode *> &Children =
593 DT->getNode(MBB)->getChildren();
594 for (const auto &DTChild : Children)
595 // DomTree children of MBB that have MBB as immediate dominator are added.
596 if (DTChild->getIDom()->getBlock() == MI->getParent() &&
597 // Skip MBBs already added to the Succs vector above.
598 !MBB->isSuccessor(DTChild->getBlock()))
599 Succs.push_back(DTChild->getBlock());
600
Bruno Cardoso Lopesd04f7592014-09-25 23:14:26 +0000601 // Sort Successors according to their loop depth or block frequency info.
Benjamin Kramer3a377bc2014-03-01 11:47:00 +0000602 std::stable_sort(
603 Succs.begin(), Succs.end(),
Bruno Cardoso Lopesd04f7592014-09-25 23:14:26 +0000604 [this](const MachineBasicBlock *L, const MachineBasicBlock *R) {
605 uint64_t LHSFreq = MBFI ? MBFI->getBlockFreq(L).getFrequency() : 0;
606 uint64_t RHSFreq = MBFI ? MBFI->getBlockFreq(R).getFrequency() : 0;
607 bool HasBlockFreq = LHSFreq != 0 && RHSFreq != 0;
608 return HasBlockFreq ? LHSFreq < RHSFreq
609 : LI->getLoopDepth(L) < LI->getLoopDepth(R);
Benjamin Kramer3a377bc2014-03-01 11:47:00 +0000610 });
Craig Toppere1c1d362013-07-03 05:11:49 +0000611 for (SmallVectorImpl<MachineBasicBlock *>::iterator SI = Succs.begin(),
612 E = Succs.end(); SI != E; ++SI) {
Devang Patelc2686882011-12-14 23:20:38 +0000613 MachineBasicBlock *SuccBlock = *SI;
Evan Cheng361b9be2010-08-19 18:33:29 +0000614 bool LocalUse = false;
Devang Patelc2686882011-12-14 23:20:38 +0000615 if (AllUsesDominatedByBlock(Reg, SuccBlock, MBB,
Evan Cheng2031b762010-09-20 19:12:55 +0000616 BreakPHIEdge, LocalUse)) {
Devang Patel1a3c1692011-12-08 21:33:23 +0000617 SuccToSinkTo = SuccBlock;
Chris Lattnerf3edc092008-01-04 07:36:53 +0000618 break;
619 }
Evan Cheng25b60682010-08-18 23:09:25 +0000620 if (LocalUse)
621 // Def is used locally, it's never safe to move this def.
Craig Topperc0196b12014-04-14 00:51:57 +0000622 return nullptr;
Chris Lattnerf3edc092008-01-04 07:36:53 +0000623 }
Jim Grosbach01edd682010-06-03 23:49:57 +0000624
Chris Lattnerf3edc092008-01-04 07:36:53 +0000625 // If we couldn't find a block to sink to, ignore this instruction.
Craig Topperc0196b12014-04-14 00:51:57 +0000626 if (!SuccToSinkTo)
627 return nullptr;
628 if (!isProfitableToSinkTo(Reg, MI, MBB, SuccToSinkTo))
629 return nullptr;
Chris Lattnerf3edc092008-01-04 07:36:53 +0000630 }
631 }
Devang Patel202cf2f2011-12-08 23:52:00 +0000632
633 // It is not possible to sink an instruction into its own block. This can
634 // happen with loops.
Devang Patelc2686882011-12-14 23:20:38 +0000635 if (MBB == SuccToSinkTo)
Craig Topperc0196b12014-04-14 00:51:57 +0000636 return nullptr;
Devang Patel202cf2f2011-12-08 23:52:00 +0000637
638 // It's not safe to sink instructions to EH landing pad. Control flow into
639 // landing pad is implicitly defined.
640 if (SuccToSinkTo && SuccToSinkTo->isLandingPad())
Craig Topperc0196b12014-04-14 00:51:57 +0000641 return nullptr;
Devang Patel202cf2f2011-12-08 23:52:00 +0000642
Devang Patelb94c9a42011-12-08 21:48:01 +0000643 return SuccToSinkTo;
644}
645
646/// SinkInstruction - Determine whether it is safe to sink the specified machine
647/// instruction out of its current block into a successor.
648bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) {
649 // Don't sink insert_subreg, subreg_to_reg, reg_sequence. These are meant to
650 // be close to the source to make it easier to coalesce.
651 if (AvoidsSinking(MI, MRI))
652 return false;
653
654 // Check if it's safe to move the instruction.
Matthias Braun07066cc2015-05-19 21:22:20 +0000655 if (!MI->isSafeToMove(AA, SawStore))
Devang Patelb94c9a42011-12-08 21:48:01 +0000656 return false;
657
658 // FIXME: This should include support for sinking instructions within the
659 // block they are currently in to shorten the live ranges. We often get
660 // instructions sunk into the top of a large block, but it would be better to
661 // also sink them down before their first use in the block. This xform has to
662 // be careful not to *increase* register pressure though, e.g. sinking
663 // "x = y + z" down if it kills y and z would increase the live ranges of y
664 // and z and only shrink the live range of x.
665
666 bool BreakPHIEdge = false;
Devang Patelc2686882011-12-14 23:20:38 +0000667 MachineBasicBlock *ParentBlock = MI->getParent();
Pete Cooperff5064a2015-05-08 17:54:29 +0000668 MachineBasicBlock *SuccToSinkTo = FindSuccToSinkTo(MI, ParentBlock,
669 BreakPHIEdge);
Jim Grosbach01edd682010-06-03 23:49:57 +0000670
Chris Lattner6ec78272008-01-05 01:39:17 +0000671 // If there are no outputs, it must have side-effects.
Craig Topperc0196b12014-04-14 00:51:57 +0000672 if (!SuccToSinkTo)
Chris Lattner6ec78272008-01-05 01:39:17 +0000673 return false;
Evan Cheng25104362009-02-15 08:36:12 +0000674
Bill Wendlingf82aea62010-06-03 07:54:20 +0000675
Daniel Dunbaref5a4382010-06-23 00:48:25 +0000676 // If the instruction to move defines a dead physical register which is live
677 // when leaving the basic block, don't move it because it could turn into a
678 // "zombie" define of that preg. E.g., EFLAGS. (<rdar://problem/8030636>)
Bill Wendlinge41e40f2010-06-25 20:48:10 +0000679 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
680 const MachineOperand &MO = MI->getOperand(I);
681 if (!MO.isReg()) continue;
682 unsigned Reg = MO.getReg();
683 if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
684 if (SuccToSinkTo->isLiveIn(Reg))
Bill Wendlingf82aea62010-06-03 07:54:20 +0000685 return false;
Bill Wendlinge41e40f2010-06-25 20:48:10 +0000686 }
Bill Wendlingf82aea62010-06-03 07:54:20 +0000687
Bill Wendling7ee730e2010-06-02 23:04:26 +0000688 DEBUG(dbgs() << "Sink instr " << *MI << "\tinto block " << *SuccToSinkTo);
689
Will Dietz5cb7f4e2013-10-14 16:57:17 +0000690 // If the block has multiple predecessors, this is a critical edge.
691 // Decide if we can sink along it or need to break the edge.
Chris Lattnerf3edc092008-01-04 07:36:53 +0000692 if (SuccToSinkTo->pred_size() > 1) {
Jakob Stoklund Olesen20b71e22010-04-13 19:06:14 +0000693 // We cannot sink a load across a critical edge - there may be stores in
694 // other code paths.
Evan Chengae9939c2010-08-19 17:33:11 +0000695 bool TryBreak = false;
Jakob Stoklund Olesen20b71e22010-04-13 19:06:14 +0000696 bool store = true;
Matthias Braun07066cc2015-05-19 21:22:20 +0000697 if (!MI->isSafeToMove(AA, store)) {
Evan Chenge5af9302010-08-19 23:33:02 +0000698 DEBUG(dbgs() << " *** NOTE: Won't sink load along critical edge.\n");
Evan Chengae9939c2010-08-19 17:33:11 +0000699 TryBreak = true;
Jakob Stoklund Olesen20b71e22010-04-13 19:06:14 +0000700 }
701
702 // We don't want to sink across a critical edge if we don't dominate the
703 // successor. We could be introducing calculations to new code paths.
Evan Chengae9939c2010-08-19 17:33:11 +0000704 if (!TryBreak && !DT->dominates(ParentBlock, SuccToSinkTo)) {
Evan Chenge5af9302010-08-19 23:33:02 +0000705 DEBUG(dbgs() << " *** NOTE: Critical edge found\n");
Evan Chengae9939c2010-08-19 17:33:11 +0000706 TryBreak = true;
Jakob Stoklund Olesen20b71e22010-04-13 19:06:14 +0000707 }
708
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +0000709 // Don't sink instructions into a loop.
Evan Chengae9939c2010-08-19 17:33:11 +0000710 if (!TryBreak && LI->isLoopHeader(SuccToSinkTo)) {
Evan Chenge5af9302010-08-19 23:33:02 +0000711 DEBUG(dbgs() << " *** NOTE: Loop header found\n");
Evan Chengae9939c2010-08-19 17:33:11 +0000712 TryBreak = true;
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +0000713 }
714
Jakob Stoklund Olesen20b71e22010-04-13 19:06:14 +0000715 // Otherwise we are OK with sinking along a critical edge.
Evan Chengae9939c2010-08-19 17:33:11 +0000716 if (!TryBreak)
717 DEBUG(dbgs() << "Sinking along critical edge.\n");
718 else {
Quentin Colombet5cded892014-08-11 23:52:01 +0000719 // Mark this edge as to be split.
720 // If the edge can actually be split, the next iteration of the main loop
721 // will sink MI in the newly created block.
722 bool Status =
723 PostponeSplitCriticalEdge(MI, ParentBlock, SuccToSinkTo, BreakPHIEdge);
724 if (!Status)
Evan Chenge53ab6d2010-09-17 22:28:18 +0000725 DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
Quentin Colombet5cded892014-08-11 23:52:01 +0000726 "break critical edge\n");
727 // The instruction will not be sunk this time.
728 return false;
Evan Chengae9939c2010-08-19 17:33:11 +0000729 }
Chris Lattnerf3edc092008-01-04 07:36:53 +0000730 }
Jim Grosbach01edd682010-06-03 23:49:57 +0000731
Evan Cheng2031b762010-09-20 19:12:55 +0000732 if (BreakPHIEdge) {
733 // BreakPHIEdge is true if all the uses are in the successor MBB being
734 // sunken into and they are all PHI nodes. In this case, machine-sink must
735 // break the critical edge first.
Quentin Colombet5cded892014-08-11 23:52:01 +0000736 bool Status = PostponeSplitCriticalEdge(MI, ParentBlock,
737 SuccToSinkTo, BreakPHIEdge);
738 if (!Status)
Evan Chengb339f3d2010-09-18 06:42:17 +0000739 DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
740 "break critical edge\n");
Quentin Colombet5cded892014-08-11 23:52:01 +0000741 // The instruction will not be sunk this time.
742 return false;
Evan Chengb339f3d2010-09-18 06:42:17 +0000743 }
744
Bill Wendling7ee730e2010-06-02 23:04:26 +0000745 // Determine where to insert into. Skip phi nodes.
Chris Lattnerf3edc092008-01-04 07:36:53 +0000746 MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
Evan Chengb339f3d2010-09-18 06:42:17 +0000747 while (InsertPos != SuccToSinkTo->end() && InsertPos->isPHI())
Chris Lattnerf3edc092008-01-04 07:36:53 +0000748 ++InsertPos;
Jim Grosbach01edd682010-06-03 23:49:57 +0000749
Devang Patel9de7a7d2011-09-07 00:07:58 +0000750 // collect matching debug values.
751 SmallVector<MachineInstr *, 2> DbgValuesToSink;
752 collectDebugValues(MI, DbgValuesToSink);
753
Chris Lattnerf3edc092008-01-04 07:36:53 +0000754 // Move the instruction.
755 SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
756 ++MachineBasicBlock::iterator(MI));
Dan Gohmanc90f51c2010-05-13 20:34:42 +0000757
Devang Patel9de7a7d2011-09-07 00:07:58 +0000758 // Move debug values.
Craig Toppere1c1d362013-07-03 05:11:49 +0000759 for (SmallVectorImpl<MachineInstr *>::iterator DBI = DbgValuesToSink.begin(),
Devang Patel9de7a7d2011-09-07 00:07:58 +0000760 DBE = DbgValuesToSink.end(); DBI != DBE; ++DBI) {
761 MachineInstr *DbgMI = *DBI;
762 SuccToSinkTo->splice(InsertPos, ParentBlock, DbgMI,
763 ++MachineBasicBlock::iterator(DbgMI));
764 }
765
Juergen Ributzka4bea4942014-09-04 02:07:36 +0000766 // Conservatively, clear any kill flags, since it's possible that they are no
767 // longer correct.
Pete Cooper85b1c482015-05-08 17:54:32 +0000768 // Note that we have to clear the kill flags for any register this instruction
769 // uses as we may sink over another instruction which currently kills the
770 // used registers.
771 for (MachineOperand &MO : MI->operands()) {
772 if (MO.isReg() && MO.isUse())
Matthias Braun352b89c2015-05-16 03:11:07 +0000773 RegsToClearKillFlags.set(MO.getReg()); // Remember to clear kill flags.
Pete Cooper85b1c482015-05-08 17:54:32 +0000774 }
Dan Gohmanc90f51c2010-05-13 20:34:42 +0000775
Chris Lattnerf3edc092008-01-04 07:36:53 +0000776 return true;
777}