blob: 0c29c76309e973eae950c76f7b1f15394fb5c9b7 [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"
24#include "llvm/CodeGen/MachineDominators.h"
25#include "llvm/CodeGen/MachineLoopInfo.h"
26#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengae9939c2010-08-19 17:33:11 +000027#include "llvm/Support/CommandLine.h"
Chris Lattnerf3edc092008-01-04 07:36:53 +000028#include "llvm/Support/Debug.h"
Bill Wendling63aa0002009-08-22 20:26:23 +000029#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/Target/TargetInstrInfo.h"
31#include "llvm/Target/TargetMachine.h"
32#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000033#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattnerf3edc092008-01-04 07:36:53 +000034using namespace llvm;
35
Chandler Carruth1b9dde02014-04-22 02:02:50 +000036#define DEBUG_TYPE "machine-sink"
37
Andrew Trick9e761992012-02-08 21:22:43 +000038static cl::opt<bool>
Evan Chengae9939c2010-08-19 17:33:11 +000039SplitEdges("machine-sink-split",
40 cl::desc("Split critical edges during machine sinking"),
Evan Chengf3e9a482010-09-20 22:52:00 +000041 cl::init(true), cl::Hidden);
Evan Chengae9939c2010-08-19 17:33:11 +000042
Evan Chenge53ab6d2010-09-17 22:28:18 +000043STATISTIC(NumSunk, "Number of machine instructions sunk");
44STATISTIC(NumSplit, "Number of critical edges split");
45STATISTIC(NumCoalesces, "Number of copies coalesced");
Chris Lattnerf3edc092008-01-04 07:36:53 +000046
47namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000048 class MachineSinking : public MachineFunctionPass {
Chris Lattnerf3edc092008-01-04 07:36:53 +000049 const TargetInstrInfo *TII;
Dan Gohmana3176872009-09-25 22:53:29 +000050 const TargetRegisterInfo *TRI;
Evan Chenge53ab6d2010-09-17 22:28:18 +000051 MachineRegisterInfo *MRI; // Machine register information
Dan Gohman5d79a2c2009-08-05 01:19:01 +000052 MachineDominatorTree *DT; // Machine dominator tree
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +000053 MachineLoopInfo *LI;
Dan Gohman87b02d52009-10-09 23:27:56 +000054 AliasAnalysis *AA;
Chris Lattnerf3edc092008-01-04 07:36:53 +000055
Evan Chenge53ab6d2010-09-17 22:28:18 +000056 // Remember which edges have been considered for breaking.
57 SmallSet<std::pair<MachineBasicBlock*,MachineBasicBlock*>, 8>
58 CEBCandidates;
Quentin Colombet5cded892014-08-11 23:52:01 +000059 // Remember which edges we are about to split.
60 // This is different from CEBCandidates since those edges
61 // will be split.
62 SetVector<std::pair<MachineBasicBlock*,MachineBasicBlock*> > ToSplit;
Evan Chenge53ab6d2010-09-17 22:28:18 +000063
Chris Lattnerf3edc092008-01-04 07:36:53 +000064 public:
65 static char ID; // Pass identification
Owen Anderson6c18d1a2010-10-19 17:21:58 +000066 MachineSinking() : MachineFunctionPass(ID) {
67 initializeMachineSinkingPass(*PassRegistry::getPassRegistry());
68 }
Jim Grosbach01edd682010-06-03 23:49:57 +000069
Craig Topper4584cd52014-03-07 09:26:03 +000070 bool runOnMachineFunction(MachineFunction &MF) override;
Jim Grosbach01edd682010-06-03 23:49:57 +000071
Craig Topper4584cd52014-03-07 09:26:03 +000072 void getAnalysisUsage(AnalysisUsage &AU) const override {
Dan Gohman04023152009-07-31 23:37:33 +000073 AU.setPreservesCFG();
Chris Lattnerf3edc092008-01-04 07:36:53 +000074 MachineFunctionPass::getAnalysisUsage(AU);
Dan Gohman87b02d52009-10-09 23:27:56 +000075 AU.addRequired<AliasAnalysis>();
Chris Lattnerf3edc092008-01-04 07:36:53 +000076 AU.addRequired<MachineDominatorTree>();
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +000077 AU.addRequired<MachineLoopInfo>();
Chris Lattnerf3edc092008-01-04 07:36:53 +000078 AU.addPreserved<MachineDominatorTree>();
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +000079 AU.addPreserved<MachineLoopInfo>();
Chris Lattnerf3edc092008-01-04 07:36:53 +000080 }
Evan Chenge53ab6d2010-09-17 22:28:18 +000081
Craig Topper4584cd52014-03-07 09:26:03 +000082 void releaseMemory() override {
Evan Chenge53ab6d2010-09-17 22:28:18 +000083 CEBCandidates.clear();
84 }
85
Chris Lattnerf3edc092008-01-04 07:36:53 +000086 private:
87 bool ProcessBlock(MachineBasicBlock &MBB);
Evan Chenge53ab6d2010-09-17 22:28:18 +000088 bool isWorthBreakingCriticalEdge(MachineInstr *MI,
89 MachineBasicBlock *From,
90 MachineBasicBlock *To);
Quentin Colombet5cded892014-08-11 23:52:01 +000091 /// \brief Postpone the splitting of the given critical
92 /// edge (\p From, \p To).
93 ///
94 /// We do not split the edges on the fly. Indeed, this invalidates
95 /// the dominance information and thus triggers a lot of updates
96 /// of that information underneath.
97 /// Instead, we postpone all the splits after each iteration of
98 /// the main loop. That way, the information is at least valid
99 /// for the lifetime of an iteration.
100 ///
101 /// \return True if the edge is marked as toSplit, false otherwise.
102 /// False can be retruned if, for instance, this is not profitable.
103 bool PostponeSplitCriticalEdge(MachineInstr *MI,
104 MachineBasicBlock *From,
105 MachineBasicBlock *To,
106 bool BreakPHIEdge);
Chris Lattner08af5a92008-01-12 00:17:41 +0000107 bool SinkInstruction(MachineInstr *MI, bool &SawStore);
Evan Cheng25b60682010-08-18 23:09:25 +0000108 bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB,
Evan Chenge53ab6d2010-09-17 22:28:18 +0000109 MachineBasicBlock *DefMBB,
Evan Cheng2031b762010-09-20 19:12:55 +0000110 bool &BreakPHIEdge, bool &LocalUse) const;
Devang Patelc2686882011-12-14 23:20:38 +0000111 MachineBasicBlock *FindSuccToSinkTo(MachineInstr *MI, MachineBasicBlock *MBB,
112 bool &BreakPHIEdge);
Andrew Trick9e761992012-02-08 21:22:43 +0000113 bool isProfitableToSinkTo(unsigned Reg, MachineInstr *MI,
Devang Patelc2686882011-12-14 23:20:38 +0000114 MachineBasicBlock *MBB,
115 MachineBasicBlock *SuccToSinkTo);
Devang Patelb94c9a42011-12-08 21:48:01 +0000116
Evan Chenge53ab6d2010-09-17 22:28:18 +0000117 bool PerformTrivialForwardCoalescing(MachineInstr *MI,
118 MachineBasicBlock *MBB);
Chris Lattnerf3edc092008-01-04 07:36:53 +0000119 };
Chris Lattnerf3edc092008-01-04 07:36:53 +0000120} // end anonymous namespace
Jim Grosbach01edd682010-06-03 23:49:57 +0000121
Dan Gohmand78c4002008-05-13 00:00:25 +0000122char MachineSinking::ID = 0;
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000123char &llvm::MachineSinkingID = MachineSinking::ID;
Owen Anderson8ac477f2010-10-12 19:48:12 +0000124INITIALIZE_PASS_BEGIN(MachineSinking, "machine-sink",
125 "Machine code sinking", false, false)
126INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
127INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
128INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
129INITIALIZE_PASS_END(MachineSinking, "machine-sink",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000130 "Machine code sinking", false, false)
Chris Lattnerf3edc092008-01-04 07:36:53 +0000131
Evan Chenge53ab6d2010-09-17 22:28:18 +0000132bool MachineSinking::PerformTrivialForwardCoalescing(MachineInstr *MI,
133 MachineBasicBlock *MBB) {
134 if (!MI->isCopy())
135 return false;
136
137 unsigned SrcReg = MI->getOperand(1).getReg();
138 unsigned DstReg = MI->getOperand(0).getReg();
139 if (!TargetRegisterInfo::isVirtualRegister(SrcReg) ||
140 !TargetRegisterInfo::isVirtualRegister(DstReg) ||
141 !MRI->hasOneNonDBGUse(SrcReg))
142 return false;
143
144 const TargetRegisterClass *SRC = MRI->getRegClass(SrcReg);
145 const TargetRegisterClass *DRC = MRI->getRegClass(DstReg);
146 if (SRC != DRC)
147 return false;
148
149 MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
150 if (DefMI->isCopyLike())
151 return false;
152 DEBUG(dbgs() << "Coalescing: " << *DefMI);
153 DEBUG(dbgs() << "*** to: " << *MI);
154 MRI->replaceRegWith(DstReg, SrcReg);
155 MI->eraseFromParent();
156 ++NumCoalesces;
157 return true;
158}
159
Chris Lattnerf3edc092008-01-04 07:36:53 +0000160/// AllUsesDominatedByBlock - Return true if all uses of the specified register
Evan Cheng25b60682010-08-18 23:09:25 +0000161/// occur in blocks dominated by the specified block. If any use is in the
162/// definition block, then return false since it is never legal to move def
163/// after uses.
Evan Chenge53ab6d2010-09-17 22:28:18 +0000164bool
165MachineSinking::AllUsesDominatedByBlock(unsigned Reg,
166 MachineBasicBlock *MBB,
167 MachineBasicBlock *DefMBB,
Evan Cheng2031b762010-09-20 19:12:55 +0000168 bool &BreakPHIEdge,
169 bool &LocalUse) const {
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000170 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
171 "Only makes sense for vregs");
Evan Chengb339f3d2010-09-18 06:42:17 +0000172
Devang Patel706574a2011-12-09 01:25:04 +0000173 // Ignore debug uses because debug info doesn't affect the code.
Evan Chengb339f3d2010-09-18 06:42:17 +0000174 if (MRI->use_nodbg_empty(Reg))
175 return true;
176
Evan Cheng2031b762010-09-20 19:12:55 +0000177 // BreakPHIEdge is true if all the uses are in the successor MBB being sunken
178 // into and they are all PHI nodes. In this case, machine-sink must break
179 // the critical edge first. e.g.
180 //
Evan Chengb339f3d2010-09-18 06:42:17 +0000181 // BB#1: derived from LLVM BB %bb4.preheader
182 // Predecessors according to CFG: BB#0
183 // ...
184 // %reg16385<def> = DEC64_32r %reg16437, %EFLAGS<imp-def,dead>
185 // ...
186 // JE_4 <BB#37>, %EFLAGS<imp-use>
187 // Successors according to CFG: BB#37 BB#2
188 //
189 // BB#2: derived from LLVM BB %bb.nph
190 // Predecessors according to CFG: BB#0 BB#1
191 // %reg16386<def> = PHI %reg16434, <BB#0>, %reg16385, <BB#1>
Evan Cheng2031b762010-09-20 19:12:55 +0000192 BreakPHIEdge = true;
Owen Andersonb36376e2014-03-17 19:36:09 +0000193 for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
194 MachineInstr *UseInst = MO.getParent();
195 unsigned OpNo = &MO - &UseInst->getOperand(0);
Evan Chengb339f3d2010-09-18 06:42:17 +0000196 MachineBasicBlock *UseBlock = UseInst->getParent();
197 if (!(UseBlock == MBB && UseInst->isPHI() &&
Owen Andersonb36376e2014-03-17 19:36:09 +0000198 UseInst->getOperand(OpNo+1).getMBB() == DefMBB)) {
Evan Cheng2031b762010-09-20 19:12:55 +0000199 BreakPHIEdge = false;
Evan Chengb339f3d2010-09-18 06:42:17 +0000200 break;
201 }
202 }
Evan Cheng2031b762010-09-20 19:12:55 +0000203 if (BreakPHIEdge)
Evan Chengb339f3d2010-09-18 06:42:17 +0000204 return true;
205
Owen Andersonb36376e2014-03-17 19:36:09 +0000206 for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
Chris Lattnerf3edc092008-01-04 07:36:53 +0000207 // Determine the block of the use.
Owen Andersonb36376e2014-03-17 19:36:09 +0000208 MachineInstr *UseInst = MO.getParent();
209 unsigned OpNo = &MO - &UseInst->getOperand(0);
Chris Lattnerf3edc092008-01-04 07:36:53 +0000210 MachineBasicBlock *UseBlock = UseInst->getParent();
Evan Chengb339f3d2010-09-18 06:42:17 +0000211 if (UseInst->isPHI()) {
Chris Lattnerf3edc092008-01-04 07:36:53 +0000212 // PHI nodes use the operand in the predecessor block, not the block with
213 // the PHI.
Owen Andersonb36376e2014-03-17 19:36:09 +0000214 UseBlock = UseInst->getOperand(OpNo+1).getMBB();
Evan Cheng361b9be2010-08-19 18:33:29 +0000215 } else if (UseBlock == DefMBB) {
216 LocalUse = true;
217 return false;
Chris Lattnerf3edc092008-01-04 07:36:53 +0000218 }
Bill Wendling7ee730e2010-06-02 23:04:26 +0000219
Chris Lattnerf3edc092008-01-04 07:36:53 +0000220 // Check that it dominates.
221 if (!DT->dominates(MBB, UseBlock))
222 return false;
223 }
Bill Wendling7ee730e2010-06-02 23:04:26 +0000224
Chris Lattnerf3edc092008-01-04 07:36:53 +0000225 return true;
226}
227
Chris Lattnerf3edc092008-01-04 07:36:53 +0000228bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
Paul Robinson7c99ec52014-03-31 17:43:35 +0000229 if (skipOptnoneFunction(*MF.getFunction()))
230 return false;
231
David Greene4b7aa242010-01-05 01:26:00 +0000232 DEBUG(dbgs() << "******** Machine Sinking ********\n");
Jim Grosbach01edd682010-06-03 23:49:57 +0000233
Dan Gohman20a327b2009-10-19 14:52:05 +0000234 const TargetMachine &TM = MF.getTarget();
Eric Christopherd9134482014-08-04 21:25:23 +0000235 TII = TM.getSubtargetImpl()->getInstrInfo();
236 TRI = TM.getSubtargetImpl()->getRegisterInfo();
Evan Chenge53ab6d2010-09-17 22:28:18 +0000237 MRI = &MF.getRegInfo();
Chris Lattnerf3edc092008-01-04 07:36:53 +0000238 DT = &getAnalysis<MachineDominatorTree>();
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +0000239 LI = &getAnalysis<MachineLoopInfo>();
Dan Gohman87b02d52009-10-09 23:27:56 +0000240 AA = &getAnalysis<AliasAnalysis>();
Chris Lattnerf3edc092008-01-04 07:36:53 +0000241
242 bool EverMadeChange = false;
Jim Grosbach01edd682010-06-03 23:49:57 +0000243
Chris Lattnerf3edc092008-01-04 07:36:53 +0000244 while (1) {
245 bool MadeChange = false;
246
247 // Process all basic blocks.
Evan Chenge53ab6d2010-09-17 22:28:18 +0000248 CEBCandidates.clear();
Quentin Colombet5cded892014-08-11 23:52:01 +0000249 ToSplit.clear();
Jim Grosbach01edd682010-06-03 23:49:57 +0000250 for (MachineFunction::iterator I = MF.begin(), E = MF.end();
Chris Lattnerf3edc092008-01-04 07:36:53 +0000251 I != E; ++I)
252 MadeChange |= ProcessBlock(*I);
Jim Grosbach01edd682010-06-03 23:49:57 +0000253
Quentin Colombet5cded892014-08-11 23:52:01 +0000254 // If we have anything we marked as toSplit, split it now.
255 for (auto &Pair : ToSplit) {
256 auto NewSucc = Pair.first->SplitCriticalEdge(Pair.second, this);
257 if (NewSucc != nullptr) {
258 DEBUG(dbgs() << " *** Splitting critical edge:"
259 " BB#" << Pair.first->getNumber()
260 << " -- BB#" << NewSucc->getNumber()
261 << " -- BB#" << Pair.second->getNumber() << '\n');
262 MadeChange = true;
263 ++NumSplit;
264 } else
265 DEBUG(dbgs() << " *** Not legal to break critical edge\n");
266 }
Chris Lattnerf3edc092008-01-04 07:36:53 +0000267 // If this iteration over the code changed anything, keep iterating.
268 if (!MadeChange) break;
269 EverMadeChange = true;
Jim Grosbach01edd682010-06-03 23:49:57 +0000270 }
Chris Lattnerf3edc092008-01-04 07:36:53 +0000271 return EverMadeChange;
272}
273
274bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
Chris Lattnerf3edc092008-01-04 07:36:53 +0000275 // Can't sink anything out of a block that has less than two successors.
Chris Lattner30c3de62009-04-10 16:38:36 +0000276 if (MBB.succ_size() <= 1 || MBB.empty()) return false;
277
Dan Gohman918a90a2010-04-05 19:17:22 +0000278 // Don't bother sinking code out of unreachable blocks. In addition to being
Jim Grosbach01edd682010-06-03 23:49:57 +0000279 // unprofitable, it can also lead to infinite looping, because in an
280 // unreachable loop there may be nowhere to stop.
Dan Gohman918a90a2010-04-05 19:17:22 +0000281 if (!DT->isReachableFromEntry(&MBB)) return false;
282
Chris Lattner30c3de62009-04-10 16:38:36 +0000283 bool MadeChange = false;
284
Chris Lattner08af5a92008-01-12 00:17:41 +0000285 // Walk the basic block bottom-up. Remember if we saw a store.
Chris Lattner30c3de62009-04-10 16:38:36 +0000286 MachineBasicBlock::iterator I = MBB.end();
287 --I;
288 bool ProcessedBegin, SawStore = false;
289 do {
290 MachineInstr *MI = I; // The instruction to sink.
Jim Grosbach01edd682010-06-03 23:49:57 +0000291
Chris Lattner30c3de62009-04-10 16:38:36 +0000292 // Predecrement I (if it's not begin) so that it isn't invalidated by
293 // sinking.
294 ProcessedBegin = I == MBB.begin();
295 if (!ProcessedBegin)
296 --I;
Dale Johannesen2061c842010-03-05 00:02:59 +0000297
298 if (MI->isDebugValue())
299 continue;
300
Evan Chengfe917ef2011-04-11 18:47:20 +0000301 bool Joined = PerformTrivialForwardCoalescing(MI, &MBB);
302 if (Joined) {
303 MadeChange = true;
Evan Chenge53ab6d2010-09-17 22:28:18 +0000304 continue;
Evan Chengfe917ef2011-04-11 18:47:20 +0000305 }
Evan Chenge53ab6d2010-09-17 22:28:18 +0000306
Chris Lattner30c3de62009-04-10 16:38:36 +0000307 if (SinkInstruction(MI, SawStore))
308 ++NumSunk, MadeChange = true;
Jim Grosbach01edd682010-06-03 23:49:57 +0000309
Chris Lattner30c3de62009-04-10 16:38:36 +0000310 // If we just processed the first instruction in the block, we're done.
311 } while (!ProcessedBegin);
Jim Grosbach01edd682010-06-03 23:49:57 +0000312
Chris Lattnerf3edc092008-01-04 07:36:53 +0000313 return MadeChange;
314}
315
Evan Chenge53ab6d2010-09-17 22:28:18 +0000316bool MachineSinking::isWorthBreakingCriticalEdge(MachineInstr *MI,
317 MachineBasicBlock *From,
318 MachineBasicBlock *To) {
319 // FIXME: Need much better heuristics.
320
321 // If the pass has already considered breaking this edge (during this pass
322 // through the function), then let's go ahead and break it. This means
323 // sinking multiple "cheap" instructions into the same block.
324 if (!CEBCandidates.insert(std::make_pair(From, To)))
325 return true;
326
Jiangning Liuc3053122014-07-29 01:55:19 +0000327 if (!MI->isCopy() && !TII->isAsCheapAsAMove(MI))
Evan Chenge53ab6d2010-09-17 22:28:18 +0000328 return true;
329
330 // MI is cheap, we probably don't want to break the critical edge for it.
331 // However, if this would allow some definitions of its source operands
332 // to be sunk then it's probably worth it.
333 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
334 const MachineOperand &MO = MI->getOperand(i);
Will Dietz5cb7f4e2013-10-14 16:57:17 +0000335 if (!MO.isReg() || !MO.isUse())
Evan Chenge53ab6d2010-09-17 22:28:18 +0000336 continue;
Will Dietz5cb7f4e2013-10-14 16:57:17 +0000337 unsigned Reg = MO.getReg();
338 if (Reg == 0)
339 continue;
340
341 // We don't move live definitions of physical registers,
342 // so sinking their uses won't enable any opportunities.
343 if (TargetRegisterInfo::isPhysicalRegister(Reg))
344 continue;
345
346 // If this instruction is the only user of a virtual register,
347 // check if breaking the edge will enable sinking
348 // both this instruction and the defining instruction.
349 if (MRI->hasOneNonDBGUse(Reg)) {
350 // If the definition resides in same MBB,
351 // claim it's likely we can sink these together.
352 // If definition resides elsewhere, we aren't
353 // blocking it from being sunk so don't break the edge.
354 MachineInstr *DefMI = MRI->getVRegDef(Reg);
355 if (DefMI->getParent() == MI->getParent())
356 return true;
357 }
Evan Chenge53ab6d2010-09-17 22:28:18 +0000358 }
359
360 return false;
361}
362
Quentin Colombet5cded892014-08-11 23:52:01 +0000363bool MachineSinking::PostponeSplitCriticalEdge(MachineInstr *MI,
364 MachineBasicBlock *FromBB,
365 MachineBasicBlock *ToBB,
366 bool BreakPHIEdge) {
Evan Chenge53ab6d2010-09-17 22:28:18 +0000367 if (!isWorthBreakingCriticalEdge(MI, FromBB, ToBB))
Quentin Colombet5cded892014-08-11 23:52:01 +0000368 return false;
Evan Chenge53ab6d2010-09-17 22:28:18 +0000369
Evan Chengae9939c2010-08-19 17:33:11 +0000370 // Avoid breaking back edge. From == To means backedge for single BB loop.
Evan Chengf3e9a482010-09-20 22:52:00 +0000371 if (!SplitEdges || FromBB == ToBB)
Quentin Colombet5cded892014-08-11 23:52:01 +0000372 return false;
Evan Chengae9939c2010-08-19 17:33:11 +0000373
Evan Chenge53ab6d2010-09-17 22:28:18 +0000374 // Check for backedges of more "complex" loops.
375 if (LI->getLoopFor(FromBB) == LI->getLoopFor(ToBB) &&
376 LI->isLoopHeader(ToBB))
Quentin Colombet5cded892014-08-11 23:52:01 +0000377 return false;
Evan Chenge53ab6d2010-09-17 22:28:18 +0000378
379 // It's not always legal to break critical edges and sink the computation
380 // to the edge.
381 //
382 // BB#1:
383 // v1024
384 // Beq BB#3
385 // <fallthrough>
386 // BB#2:
387 // ... no uses of v1024
388 // <fallthrough>
389 // BB#3:
390 // ...
391 // = v1024
392 //
393 // If BB#1 -> BB#3 edge is broken and computation of v1024 is inserted:
394 //
395 // BB#1:
396 // ...
397 // Bne BB#2
398 // BB#4:
399 // v1024 =
400 // B BB#3
401 // BB#2:
402 // ... no uses of v1024
403 // <fallthrough>
404 // BB#3:
405 // ...
406 // = v1024
407 //
408 // This is incorrect since v1024 is not computed along the BB#1->BB#2->BB#3
409 // flow. We need to ensure the new basic block where the computation is
410 // sunk to dominates all the uses.
411 // It's only legal to break critical edge and sink the computation to the
412 // new block if all the predecessors of "To", except for "From", are
413 // not dominated by "From". Given SSA property, this means these
414 // predecessors are dominated by "To".
415 //
416 // There is no need to do this check if all the uses are PHI nodes. PHI
417 // sources are only defined on the specific predecessor edges.
Evan Cheng2031b762010-09-20 19:12:55 +0000418 if (!BreakPHIEdge) {
Evan Chengae9939c2010-08-19 17:33:11 +0000419 for (MachineBasicBlock::pred_iterator PI = ToBB->pred_begin(),
420 E = ToBB->pred_end(); PI != E; ++PI) {
421 if (*PI == FromBB)
422 continue;
423 if (!DT->dominates(ToBB, *PI))
Quentin Colombet5cded892014-08-11 23:52:01 +0000424 return false;
Evan Chengae9939c2010-08-19 17:33:11 +0000425 }
Evan Chengae9939c2010-08-19 17:33:11 +0000426 }
427
Quentin Colombet5cded892014-08-11 23:52:01 +0000428 ToSplit.insert(std::make_pair(FromBB, ToBB));
429
430 return true;
Evan Chengae9939c2010-08-19 17:33:11 +0000431}
432
Evan Chengd4b31a72010-09-23 06:53:00 +0000433static bool AvoidsSinking(MachineInstr *MI, MachineRegisterInfo *MRI) {
434 return MI->isInsertSubreg() || MI->isSubregToReg() || MI->isRegSequence();
435}
436
Andrew Trick9e761992012-02-08 21:22:43 +0000437/// collectDebgValues - Scan instructions following MI and collect any
Devang Patel9de7a7d2011-09-07 00:07:58 +0000438/// matching DBG_VALUEs.
Andrew Trick9e761992012-02-08 21:22:43 +0000439static void collectDebugValues(MachineInstr *MI,
Craig Topperb94011f2013-07-14 04:42:23 +0000440 SmallVectorImpl<MachineInstr *> &DbgValues) {
Devang Patel9de7a7d2011-09-07 00:07:58 +0000441 DbgValues.clear();
442 if (!MI->getOperand(0).isReg())
443 return;
444
445 MachineBasicBlock::iterator DI = MI; ++DI;
446 for (MachineBasicBlock::iterator DE = MI->getParent()->end();
447 DI != DE; ++DI) {
448 if (!DI->isDebugValue())
449 return;
450 if (DI->getOperand(0).isReg() &&
451 DI->getOperand(0).getReg() == MI->getOperand(0).getReg())
452 DbgValues.push_back(DI);
453 }
454}
455
Devang Patelc2686882011-12-14 23:20:38 +0000456/// isPostDominatedBy - Return true if A is post dominated by B.
457static bool isPostDominatedBy(MachineBasicBlock *A, MachineBasicBlock *B) {
458
459 // FIXME - Use real post dominator.
460 if (A->succ_size() != 2)
461 return false;
462 MachineBasicBlock::succ_iterator I = A->succ_begin();
463 if (B == *I)
464 ++I;
465 MachineBasicBlock *OtherSuccBlock = *I;
466 if (OtherSuccBlock->succ_size() != 1 ||
467 *(OtherSuccBlock->succ_begin()) != B)
468 return false;
469
470 return true;
471}
472
473/// isProfitableToSinkTo - Return true if it is profitable to sink MI.
Andrew Trick9e761992012-02-08 21:22:43 +0000474bool MachineSinking::isProfitableToSinkTo(unsigned Reg, MachineInstr *MI,
Devang Patelc2686882011-12-14 23:20:38 +0000475 MachineBasicBlock *MBB,
476 MachineBasicBlock *SuccToSinkTo) {
477 assert (MI && "Invalid MachineInstr!");
478 assert (SuccToSinkTo && "Invalid SinkTo Candidate BB");
479
480 if (MBB == SuccToSinkTo)
481 return false;
482
483 // It is profitable if SuccToSinkTo does not post dominate current block.
484 if (!isPostDominatedBy(MBB, SuccToSinkTo))
485 return true;
486
487 // Check if only use in post dominated block is PHI instruction.
488 bool NonPHIUse = false;
Owen Andersonb36376e2014-03-17 19:36:09 +0000489 for (MachineInstr &UseInst : MRI->use_nodbg_instructions(Reg)) {
490 MachineBasicBlock *UseBlock = UseInst.getParent();
491 if (UseBlock == SuccToSinkTo && !UseInst.isPHI())
Devang Patelc2686882011-12-14 23:20:38 +0000492 NonPHIUse = true;
493 }
494 if (!NonPHIUse)
495 return true;
496
497 // If SuccToSinkTo post dominates then also it may be profitable if MI
498 // can further profitably sinked into another block in next round.
499 bool BreakPHIEdge = false;
500 // FIXME - If finding successor is compile time expensive then catch results.
501 if (MachineBasicBlock *MBB2 = FindSuccToSinkTo(MI, SuccToSinkTo, BreakPHIEdge))
502 return isProfitableToSinkTo(Reg, MI, SuccToSinkTo, MBB2);
503
504 // If SuccToSinkTo is final destination and it is a post dominator of current
505 // block then it is not profitable to sink MI into SuccToSinkTo block.
506 return false;
507}
508
Devang Patelb94c9a42011-12-08 21:48:01 +0000509/// FindSuccToSinkTo - Find a successor to sink this instruction to.
510MachineBasicBlock *MachineSinking::FindSuccToSinkTo(MachineInstr *MI,
Devang Patelc2686882011-12-14 23:20:38 +0000511 MachineBasicBlock *MBB,
512 bool &BreakPHIEdge) {
513
514 assert (MI && "Invalid MachineInstr!");
515 assert (MBB && "Invalid MachineBasicBlock!");
Jim Grosbach01edd682010-06-03 23:49:57 +0000516
Chris Lattnerf3edc092008-01-04 07:36:53 +0000517 // Loop over all the operands of the specified instruction. If there is
518 // anything we can't handle, bail out.
Jim Grosbach01edd682010-06-03 23:49:57 +0000519
Chris Lattnerf3edc092008-01-04 07:36:53 +0000520 // SuccToSinkTo - This is the successor to sink this instruction to, once we
521 // decide.
Craig Topperc0196b12014-04-14 00:51:57 +0000522 MachineBasicBlock *SuccToSinkTo = nullptr;
Chris Lattnerf3edc092008-01-04 07:36:53 +0000523 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
524 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000525 if (!MO.isReg()) continue; // Ignore non-register operands.
Jim Grosbach01edd682010-06-03 23:49:57 +0000526
Chris Lattnerf3edc092008-01-04 07:36:53 +0000527 unsigned Reg = MO.getReg();
528 if (Reg == 0) continue;
Jim Grosbach01edd682010-06-03 23:49:57 +0000529
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000530 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Dan Gohmana3176872009-09-25 22:53:29 +0000531 if (MO.isUse()) {
532 // If the physreg has no defs anywhere, it's just an ambient register
Dan Gohman2f5bdcb2009-09-26 02:34:00 +0000533 // and we can freely move its uses. Alternatively, if it's allocatable,
534 // it could get allocated to something with a def during allocation.
Jakob Stoklund Olesen86ae07f2012-01-16 22:34:08 +0000535 if (!MRI->isConstantPhysReg(Reg, *MBB->getParent()))
Craig Topperc0196b12014-04-14 00:51:57 +0000536 return nullptr;
Bill Wendlinge41e40f2010-06-25 20:48:10 +0000537 } else if (!MO.isDead()) {
538 // A def that isn't dead. We can't move it.
Craig Topperc0196b12014-04-14 00:51:57 +0000539 return nullptr;
Dan Gohmana3176872009-09-25 22:53:29 +0000540 }
Chris Lattnerf3edc092008-01-04 07:36:53 +0000541 } else {
542 // Virtual register uses are always safe to sink.
543 if (MO.isUse()) continue;
Evan Cheng47a65a12009-02-07 01:21:47 +0000544
545 // If it's not safe to move defs of the register class, then abort.
Evan Chenge53ab6d2010-09-17 22:28:18 +0000546 if (!TII->isSafeToMoveRegClassDefs(MRI->getRegClass(Reg)))
Craig Topperc0196b12014-04-14 00:51:57 +0000547 return nullptr;
Jim Grosbach01edd682010-06-03 23:49:57 +0000548
Chris Lattneree61d142008-01-05 06:47:58 +0000549 // FIXME: This picks a successor to sink into based on having one
550 // successor that dominates all the uses. However, there are cases where
551 // sinking can happen but where the sink point isn't a successor. For
552 // example:
Jim Grosbach01edd682010-06-03 23:49:57 +0000553 //
Chris Lattneree61d142008-01-05 06:47:58 +0000554 // x = computation
555 // if () {} else {}
556 // use x
Jim Grosbach01edd682010-06-03 23:49:57 +0000557 //
Bill Wendling7ee730e2010-06-02 23:04:26 +0000558 // the instruction could be sunk over the whole diamond for the
Chris Lattneree61d142008-01-05 06:47:58 +0000559 // if/then/else (or loop, etc), allowing it to be sunk into other blocks
560 // after that.
Jim Grosbach01edd682010-06-03 23:49:57 +0000561
Chris Lattnerf3edc092008-01-04 07:36:53 +0000562 // Virtual register defs can only be sunk if all their uses are in blocks
563 // dominated by one of the successors.
564 if (SuccToSinkTo) {
565 // If a previous operand picked a block to sink to, then this operand
566 // must be sinkable to the same block.
Evan Cheng361b9be2010-08-19 18:33:29 +0000567 bool LocalUse = false;
Devang Patelc2686882011-12-14 23:20:38 +0000568 if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, MBB,
Evan Cheng2031b762010-09-20 19:12:55 +0000569 BreakPHIEdge, LocalUse))
Craig Topperc0196b12014-04-14 00:51:57 +0000570 return nullptr;
Bill Wendling7ee730e2010-06-02 23:04:26 +0000571
Chris Lattnerf3edc092008-01-04 07:36:53 +0000572 continue;
573 }
Jim Grosbach01edd682010-06-03 23:49:57 +0000574
Chris Lattnerf3edc092008-01-04 07:36:53 +0000575 // Otherwise, we should look at all the successors and decide which one
576 // we should sink to.
Manman Ren8c549b52012-07-31 18:10:39 +0000577 // We give successors with smaller loop depth higher priority.
578 SmallVector<MachineBasicBlock*, 4> Succs(MBB->succ_begin(), MBB->succ_end());
Benjamin Kramer3a377bc2014-03-01 11:47:00 +0000579 // Sort Successors according to their loop depth.
580 std::stable_sort(
581 Succs.begin(), Succs.end(),
582 [this](const MachineBasicBlock *LHS, const MachineBasicBlock *RHS) {
583 return LI->getLoopDepth(LHS) < LI->getLoopDepth(RHS);
584 });
Craig Toppere1c1d362013-07-03 05:11:49 +0000585 for (SmallVectorImpl<MachineBasicBlock *>::iterator SI = Succs.begin(),
586 E = Succs.end(); SI != E; ++SI) {
Devang Patelc2686882011-12-14 23:20:38 +0000587 MachineBasicBlock *SuccBlock = *SI;
Evan Cheng361b9be2010-08-19 18:33:29 +0000588 bool LocalUse = false;
Devang Patelc2686882011-12-14 23:20:38 +0000589 if (AllUsesDominatedByBlock(Reg, SuccBlock, MBB,
Evan Cheng2031b762010-09-20 19:12:55 +0000590 BreakPHIEdge, LocalUse)) {
Devang Patel1a3c1692011-12-08 21:33:23 +0000591 SuccToSinkTo = SuccBlock;
Chris Lattnerf3edc092008-01-04 07:36:53 +0000592 break;
593 }
Evan Cheng25b60682010-08-18 23:09:25 +0000594 if (LocalUse)
595 // Def is used locally, it's never safe to move this def.
Craig Topperc0196b12014-04-14 00:51:57 +0000596 return nullptr;
Chris Lattnerf3edc092008-01-04 07:36:53 +0000597 }
Jim Grosbach01edd682010-06-03 23:49:57 +0000598
Chris Lattnerf3edc092008-01-04 07:36:53 +0000599 // If we couldn't find a block to sink to, ignore this instruction.
Craig Topperc0196b12014-04-14 00:51:57 +0000600 if (!SuccToSinkTo)
601 return nullptr;
602 if (!isProfitableToSinkTo(Reg, MI, MBB, SuccToSinkTo))
603 return nullptr;
Chris Lattnerf3edc092008-01-04 07:36:53 +0000604 }
605 }
Devang Patel202cf2f2011-12-08 23:52:00 +0000606
607 // It is not possible to sink an instruction into its own block. This can
608 // happen with loops.
Devang Patelc2686882011-12-14 23:20:38 +0000609 if (MBB == SuccToSinkTo)
Craig Topperc0196b12014-04-14 00:51:57 +0000610 return nullptr;
Devang Patel202cf2f2011-12-08 23:52:00 +0000611
612 // It's not safe to sink instructions to EH landing pad. Control flow into
613 // landing pad is implicitly defined.
614 if (SuccToSinkTo && SuccToSinkTo->isLandingPad())
Craig Topperc0196b12014-04-14 00:51:57 +0000615 return nullptr;
Devang Patel202cf2f2011-12-08 23:52:00 +0000616
Devang Patelb94c9a42011-12-08 21:48:01 +0000617 return SuccToSinkTo;
618}
619
620/// SinkInstruction - Determine whether it is safe to sink the specified machine
621/// instruction out of its current block into a successor.
622bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) {
623 // Don't sink insert_subreg, subreg_to_reg, reg_sequence. These are meant to
624 // be close to the source to make it easier to coalesce.
625 if (AvoidsSinking(MI, MRI))
626 return false;
627
628 // Check if it's safe to move the instruction.
629 if (!MI->isSafeToMove(TII, AA, SawStore))
630 return false;
631
632 // FIXME: This should include support for sinking instructions within the
633 // block they are currently in to shorten the live ranges. We often get
634 // instructions sunk into the top of a large block, but it would be better to
635 // also sink them down before their first use in the block. This xform has to
636 // be careful not to *increase* register pressure though, e.g. sinking
637 // "x = y + z" down if it kills y and z would increase the live ranges of y
638 // and z and only shrink the live range of x.
639
640 bool BreakPHIEdge = false;
Devang Patelc2686882011-12-14 23:20:38 +0000641 MachineBasicBlock *ParentBlock = MI->getParent();
642 MachineBasicBlock *SuccToSinkTo = FindSuccToSinkTo(MI, ParentBlock, BreakPHIEdge);
Jim Grosbach01edd682010-06-03 23:49:57 +0000643
Chris Lattner6ec78272008-01-05 01:39:17 +0000644 // If there are no outputs, it must have side-effects.
Craig Topperc0196b12014-04-14 00:51:57 +0000645 if (!SuccToSinkTo)
Chris Lattner6ec78272008-01-05 01:39:17 +0000646 return false;
Evan Cheng25104362009-02-15 08:36:12 +0000647
Bill Wendlingf82aea62010-06-03 07:54:20 +0000648
Daniel Dunbaref5a4382010-06-23 00:48:25 +0000649 // If the instruction to move defines a dead physical register which is live
650 // when leaving the basic block, don't move it because it could turn into a
651 // "zombie" define of that preg. E.g., EFLAGS. (<rdar://problem/8030636>)
Bill Wendlinge41e40f2010-06-25 20:48:10 +0000652 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
653 const MachineOperand &MO = MI->getOperand(I);
654 if (!MO.isReg()) continue;
655 unsigned Reg = MO.getReg();
656 if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
657 if (SuccToSinkTo->isLiveIn(Reg))
Bill Wendlingf82aea62010-06-03 07:54:20 +0000658 return false;
Bill Wendlinge41e40f2010-06-25 20:48:10 +0000659 }
Bill Wendlingf82aea62010-06-03 07:54:20 +0000660
Bill Wendling7ee730e2010-06-02 23:04:26 +0000661 DEBUG(dbgs() << "Sink instr " << *MI << "\tinto block " << *SuccToSinkTo);
662
Will Dietz5cb7f4e2013-10-14 16:57:17 +0000663 // If the block has multiple predecessors, this is a critical edge.
664 // Decide if we can sink along it or need to break the edge.
Chris Lattnerf3edc092008-01-04 07:36:53 +0000665 if (SuccToSinkTo->pred_size() > 1) {
Jakob Stoklund Olesen20b71e22010-04-13 19:06:14 +0000666 // We cannot sink a load across a critical edge - there may be stores in
667 // other code paths.
Evan Chengae9939c2010-08-19 17:33:11 +0000668 bool TryBreak = false;
Jakob Stoklund Olesen20b71e22010-04-13 19:06:14 +0000669 bool store = true;
670 if (!MI->isSafeToMove(TII, AA, store)) {
Evan Chenge5af9302010-08-19 23:33:02 +0000671 DEBUG(dbgs() << " *** NOTE: Won't sink load along critical edge.\n");
Evan Chengae9939c2010-08-19 17:33:11 +0000672 TryBreak = true;
Jakob Stoklund Olesen20b71e22010-04-13 19:06:14 +0000673 }
674
675 // We don't want to sink across a critical edge if we don't dominate the
676 // successor. We could be introducing calculations to new code paths.
Evan Chengae9939c2010-08-19 17:33:11 +0000677 if (!TryBreak && !DT->dominates(ParentBlock, SuccToSinkTo)) {
Evan Chenge5af9302010-08-19 23:33:02 +0000678 DEBUG(dbgs() << " *** NOTE: Critical edge found\n");
Evan Chengae9939c2010-08-19 17:33:11 +0000679 TryBreak = true;
Jakob Stoklund Olesen20b71e22010-04-13 19:06:14 +0000680 }
681
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +0000682 // Don't sink instructions into a loop.
Evan Chengae9939c2010-08-19 17:33:11 +0000683 if (!TryBreak && LI->isLoopHeader(SuccToSinkTo)) {
Evan Chenge5af9302010-08-19 23:33:02 +0000684 DEBUG(dbgs() << " *** NOTE: Loop header found\n");
Evan Chengae9939c2010-08-19 17:33:11 +0000685 TryBreak = true;
Jakob Stoklund Olesencdc3df42010-04-15 23:41:02 +0000686 }
687
Jakob Stoklund Olesen20b71e22010-04-13 19:06:14 +0000688 // Otherwise we are OK with sinking along a critical edge.
Evan Chengae9939c2010-08-19 17:33:11 +0000689 if (!TryBreak)
690 DEBUG(dbgs() << "Sinking along critical edge.\n");
691 else {
Quentin Colombet5cded892014-08-11 23:52:01 +0000692 // Mark this edge as to be split.
693 // If the edge can actually be split, the next iteration of the main loop
694 // will sink MI in the newly created block.
695 bool Status =
696 PostponeSplitCriticalEdge(MI, ParentBlock, SuccToSinkTo, BreakPHIEdge);
697 if (!Status)
Evan Chenge53ab6d2010-09-17 22:28:18 +0000698 DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
Quentin Colombet5cded892014-08-11 23:52:01 +0000699 "break critical edge\n");
700 // The instruction will not be sunk this time.
701 return false;
Evan Chengae9939c2010-08-19 17:33:11 +0000702 }
Chris Lattnerf3edc092008-01-04 07:36:53 +0000703 }
Jim Grosbach01edd682010-06-03 23:49:57 +0000704
Evan Cheng2031b762010-09-20 19:12:55 +0000705 if (BreakPHIEdge) {
706 // BreakPHIEdge is true if all the uses are in the successor MBB being
707 // sunken into and they are all PHI nodes. In this case, machine-sink must
708 // break the critical edge first.
Quentin Colombet5cded892014-08-11 23:52:01 +0000709 bool Status = PostponeSplitCriticalEdge(MI, ParentBlock,
710 SuccToSinkTo, BreakPHIEdge);
711 if (!Status)
Evan Chengb339f3d2010-09-18 06:42:17 +0000712 DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
713 "break critical edge\n");
Quentin Colombet5cded892014-08-11 23:52:01 +0000714 // The instruction will not be sunk this time.
715 return false;
Evan Chengb339f3d2010-09-18 06:42:17 +0000716 }
717
Bill Wendling7ee730e2010-06-02 23:04:26 +0000718 // Determine where to insert into. Skip phi nodes.
Chris Lattnerf3edc092008-01-04 07:36:53 +0000719 MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
Evan Chengb339f3d2010-09-18 06:42:17 +0000720 while (InsertPos != SuccToSinkTo->end() && InsertPos->isPHI())
Chris Lattnerf3edc092008-01-04 07:36:53 +0000721 ++InsertPos;
Jim Grosbach01edd682010-06-03 23:49:57 +0000722
Devang Patel9de7a7d2011-09-07 00:07:58 +0000723 // collect matching debug values.
724 SmallVector<MachineInstr *, 2> DbgValuesToSink;
725 collectDebugValues(MI, DbgValuesToSink);
726
Chris Lattnerf3edc092008-01-04 07:36:53 +0000727 // Move the instruction.
728 SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
729 ++MachineBasicBlock::iterator(MI));
Dan Gohmanc90f51c2010-05-13 20:34:42 +0000730
Devang Patel9de7a7d2011-09-07 00:07:58 +0000731 // Move debug values.
Craig Toppere1c1d362013-07-03 05:11:49 +0000732 for (SmallVectorImpl<MachineInstr *>::iterator DBI = DbgValuesToSink.begin(),
Devang Patel9de7a7d2011-09-07 00:07:58 +0000733 DBE = DbgValuesToSink.end(); DBI != DBE; ++DBI) {
734 MachineInstr *DbgMI = *DBI;
735 SuccToSinkTo->splice(InsertPos, ParentBlock, DbgMI,
736 ++MachineBasicBlock::iterator(DbgMI));
737 }
738
Juergen Ributzka00d78222014-08-29 23:48:03 +0000739 // When sinking the instruction the live time of its operands can be extended
740 // bejond their original last use (marked with a kill flag). Conservatively
741 // clear the kill flag in all instructions that use the same operand
742 // registers.
743 for (auto &MO : MI->uses())
744 if (MO.isReg() && MO.isUse()) {
745 // Preserve the kill flag for this instruction.
746 bool IsKill = MO.isKill();
747 // Clear the kill flag in all instruction that use this operand.
748 MRI->clearKillFlags(MO.getReg());
749 // Restore the kill flag for only this instruction.
750 MO.setIsKill(IsKill);
751 }
Dan Gohmanc90f51c2010-05-13 20:34:42 +0000752
Chris Lattnerf3edc092008-01-04 07:36:53 +0000753 return true;
754}