blob: 879b65f9d089c1044c7c365a0910226b7b17f1bf [file] [log] [blame]
Dan Gohmana629b482008-12-08 17:50:35 +00001//===---- ScheduleDAGInstrs.cpp - MachineInstr Rescheduling ---------------===//
Dan Gohman343f0c02008-11-19 23:18:57 +00002//
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//
Dan Gohmana629b482008-12-08 17:50:35 +000010// This implements the ScheduleDAGInstrs class, which implements re-scheduling
11// of MachineInstrs.
Dan Gohman343f0c02008-11-19 23:18:57 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "sched-instrs"
Dan Gohman6dc75fe2009-02-06 17:12:10 +000016#include "ScheduleDAGInstrs.h"
Dan Gohman8906f952009-07-17 20:58:59 +000017#include "llvm/Operator.h"
Dan Gohman3311a1f2009-01-30 02:49:14 +000018#include "llvm/Analysis/AliasAnalysis.h"
Dan Gohman5034dd32010-12-15 20:02:24 +000019#include "llvm/Analysis/ValueTracking.h"
Dan Gohman3f237442008-12-16 03:25:46 +000020#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohmanc76909a2009-09-25 20:36:54 +000021#include "llvm/CodeGen/MachineMemOperand.h"
Dan Gohman3f237442008-12-16 03:25:46 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman6a9041e2008-12-04 01:35:46 +000023#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chengab8be962011-06-29 01:14:12 +000024#include "llvm/MC/MCInstrItineraries.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000025#include "llvm/Target/TargetMachine.h"
26#include "llvm/Target/TargetInstrInfo.h"
27#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng5b1b44892011-07-01 21:01:15 +000028#include "llvm/Target/TargetSubtargetInfo.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/raw_ostream.h"
Dan Gohman3f237442008-12-16 03:25:46 +000031#include "llvm/ADT/SmallSet.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000032using namespace llvm;
33
Dan Gohman79ce2762009-01-15 19:20:50 +000034ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf,
Dan Gohman3f237442008-12-16 03:25:46 +000035 const MachineLoopInfo &mli,
Andrew Trick5e920d72012-01-14 02:17:12 +000036 const MachineDominatorTree &mdt,
37 bool IsPostRAFlag)
Evan Cheng3ef1c872010-09-10 01:29:16 +000038 : ScheduleDAG(mf), MLI(mli), MDT(mdt), MFI(mf.getFrameInfo()),
Andrew Trick5e920d72012-01-14 02:17:12 +000039 InstrItins(mf.getTarget().getInstrItineraryData()), IsPostRA(IsPostRAFlag),
Andrew Trick7ebcaf42012-01-14 02:17:15 +000040 UnitLatencies(false), Defs(TRI->getNumRegs()), Uses(TRI->getNumRegs()),
Devang Patele29e8e12011-06-02 21:26:52 +000041 LoopRegs(MLI, MDT), FirstDbgValue(0) {
Devang Patelcf4cc842011-06-02 20:07:12 +000042 DbgValues.clear();
Andrew Trick19273ae2012-02-21 04:51:23 +000043 assert(!(IsPostRA && MF.getRegInfo().getNumVirtRegs()) &&
44 "Virtual registers must be removed prior to PostRA scheduling");
Evan Cheng38bdfc62009-10-18 19:58:47 +000045}
Dan Gohman343f0c02008-11-19 23:18:57 +000046
Dan Gohman47ac0f02009-02-11 04:27:20 +000047/// Run - perform scheduling.
48///
49void ScheduleDAGInstrs::Run(MachineBasicBlock *bb,
50 MachineBasicBlock::iterator begin,
51 MachineBasicBlock::iterator end,
52 unsigned endcount) {
53 BB = bb;
54 Begin = begin;
55 InsertPosIndex = endcount;
56
Andrew Trick7ebcaf42012-01-14 02:17:15 +000057 // Check to see if the scheduler cares about latencies.
58 UnitLatencies = ForceUnitLatencies();
59
Dan Gohman47ac0f02009-02-11 04:27:20 +000060 ScheduleDAG::Run(bb, end);
61}
62
Dan Gohman3311a1f2009-01-30 02:49:14 +000063/// getUnderlyingObjectFromInt - This is the function that does the work of
64/// looking through basic ptrtoint+arithmetic+inttoptr sequences.
65static const Value *getUnderlyingObjectFromInt(const Value *V) {
66 do {
Dan Gohman8906f952009-07-17 20:58:59 +000067 if (const Operator *U = dyn_cast<Operator>(V)) {
Dan Gohman3311a1f2009-01-30 02:49:14 +000068 // If we find a ptrtoint, we can transfer control back to the
69 // regular getUnderlyingObjectFromInt.
Dan Gohman8906f952009-07-17 20:58:59 +000070 if (U->getOpcode() == Instruction::PtrToInt)
Dan Gohman3311a1f2009-01-30 02:49:14 +000071 return U->getOperand(0);
72 // If we find an add of a constant or a multiplied value, it's
73 // likely that the other operand will lead us to the base
74 // object. We don't have to worry about the case where the
Dan Gohman748f98f2009-08-07 01:26:06 +000075 // object address is somehow being computed by the multiply,
Dan Gohman3311a1f2009-01-30 02:49:14 +000076 // because our callers only care when the result is an
77 // identifibale object.
Dan Gohman8906f952009-07-17 20:58:59 +000078 if (U->getOpcode() != Instruction::Add ||
Dan Gohman3311a1f2009-01-30 02:49:14 +000079 (!isa<ConstantInt>(U->getOperand(1)) &&
Dan Gohman8906f952009-07-17 20:58:59 +000080 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul))
Dan Gohman3311a1f2009-01-30 02:49:14 +000081 return V;
82 V = U->getOperand(0);
83 } else {
84 return V;
85 }
Duncan Sands1df98592010-02-16 11:11:14 +000086 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
Dan Gohman3311a1f2009-01-30 02:49:14 +000087 } while (1);
88}
89
Dan Gohman5034dd32010-12-15 20:02:24 +000090/// getUnderlyingObject - This is a wrapper around GetUnderlyingObject
Dan Gohman3311a1f2009-01-30 02:49:14 +000091/// and adds support for basic ptrtoint+arithmetic+inttoptr sequences.
92static const Value *getUnderlyingObject(const Value *V) {
93 // First just call Value::getUnderlyingObject to let it do what it does.
94 do {
Dan Gohman5034dd32010-12-15 20:02:24 +000095 V = GetUnderlyingObject(V);
Dan Gohman3311a1f2009-01-30 02:49:14 +000096 // If it found an inttoptr, use special code to continue climing.
Dan Gohman8906f952009-07-17 20:58:59 +000097 if (Operator::getOpcode(V) != Instruction::IntToPtr)
Dan Gohman3311a1f2009-01-30 02:49:14 +000098 break;
99 const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
100 // If that succeeded in finding a pointer, continue the search.
Duncan Sands1df98592010-02-16 11:11:14 +0000101 if (!O->getType()->isPointerTy())
Dan Gohman3311a1f2009-01-30 02:49:14 +0000102 break;
103 V = O;
104 } while (1);
105 return V;
106}
107
108/// getUnderlyingObjectForInstr - If this machine instr has memory reference
109/// information and it can be tracked to a normal reference to a known
110/// object, return the Value for that object. Otherwise return null.
Evan Cheng38bdfc62009-10-18 19:58:47 +0000111static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI,
David Goodwina9e61072009-11-03 20:15:00 +0000112 const MachineFrameInfo *MFI,
113 bool &MayAlias) {
114 MayAlias = true;
Dan Gohman3311a1f2009-01-30 02:49:14 +0000115 if (!MI->hasOneMemOperand() ||
Dan Gohmanc76909a2009-09-25 20:36:54 +0000116 !(*MI->memoperands_begin())->getValue() ||
117 (*MI->memoperands_begin())->isVolatile())
Dan Gohman3311a1f2009-01-30 02:49:14 +0000118 return 0;
119
Dan Gohmanc76909a2009-09-25 20:36:54 +0000120 const Value *V = (*MI->memoperands_begin())->getValue();
Dan Gohman3311a1f2009-01-30 02:49:14 +0000121 if (!V)
122 return 0;
123
124 V = getUnderlyingObject(V);
Evan Chengff89dcb2009-10-18 18:16:27 +0000125 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
126 // For now, ignore PseudoSourceValues which may alias LLVM IR values
127 // because the code that uses this function has no way to cope with
128 // such aliases.
Evan Cheng38bdfc62009-10-18 19:58:47 +0000129 if (PSV->isAliased(MFI))
Evan Chengff89dcb2009-10-18 18:16:27 +0000130 return 0;
Andrew Trickf405b1a2011-05-05 19:24:06 +0000131
David Goodwin980d4942009-11-09 19:22:17 +0000132 MayAlias = PSV->mayAlias(MFI);
Evan Chengff89dcb2009-10-18 18:16:27 +0000133 return V;
134 }
Dan Gohman3311a1f2009-01-30 02:49:14 +0000135
Evan Chengff89dcb2009-10-18 18:16:27 +0000136 if (isIdentifiedObject(V))
137 return V;
138
139 return 0;
Dan Gohman3311a1f2009-01-30 02:49:14 +0000140}
141
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000142void ScheduleDAGInstrs::StartBlock(MachineBasicBlock *BB) {
Andrew Tricke8deca82011-10-07 06:33:09 +0000143 LoopRegs.Deps.clear();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000144 if (MachineLoop *ML = MLI.getLoopFor(BB))
Evan Cheng977679d2012-01-07 03:02:36 +0000145 if (BB == ML->getLoopLatch())
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000146 LoopRegs.VisitLoop(ML);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000147}
148
Evan Chengec6906b2010-10-23 02:10:46 +0000149/// AddSchedBarrierDeps - Add dependencies from instructions in the current
150/// list of instructions being scheduled to scheduling barrier by adding
151/// the exit SU to the register defs and use list. This is because we want to
152/// make sure instructions which define registers that are either used by
153/// the terminator or are live-out are properly scheduled. This is
154/// especially important when the definition latency of the return value(s)
155/// are too high to be hidden by the branch or when the liveout registers
156/// used by instructions in the fallthrough block.
157void ScheduleDAGInstrs::AddSchedBarrierDeps() {
158 MachineInstr *ExitMI = InsertPos != BB->end() ? &*InsertPos : 0;
159 ExitSU.setInstr(ExitMI);
160 bool AllDepKnown = ExitMI &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000161 (ExitMI->isCall() || ExitMI->isBarrier());
Evan Chengec6906b2010-10-23 02:10:46 +0000162 if (ExitMI && AllDepKnown) {
163 // If it's a call or a barrier, add dependencies on the defs and uses of
164 // instruction.
165 for (unsigned i = 0, e = ExitMI->getNumOperands(); i != e; ++i) {
166 const MachineOperand &MO = ExitMI->getOperand(i);
167 if (!MO.isReg() || MO.isDef()) continue;
168 unsigned Reg = MO.getReg();
169 if (Reg == 0) continue;
170
Andrew Trick3c58ba82012-01-14 02:17:18 +0000171 if (TRI->isPhysicalRegister(Reg))
172 Uses[Reg].push_back(&ExitSU);
173 else
174 assert(!IsPostRA && "Virtual register encountered after regalloc.");
Evan Chengec6906b2010-10-23 02:10:46 +0000175 }
176 } else {
177 // For others, e.g. fallthrough, conditional branch, assume the exit
Evan Chengde5fa932010-10-27 23:17:17 +0000178 // uses all the registers that are livein to the successor blocks.
179 SmallSet<unsigned, 8> Seen;
180 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
181 SE = BB->succ_end(); SI != SE; ++SI)
182 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
Andrew Trickf405b1a2011-05-05 19:24:06 +0000183 E = (*SI)->livein_end(); I != E; ++I) {
Evan Chengde5fa932010-10-27 23:17:17 +0000184 unsigned Reg = *I;
185 if (Seen.insert(Reg))
186 Uses[Reg].push_back(&ExitSU);
187 }
Evan Chengec6906b2010-10-23 02:10:46 +0000188 }
189}
190
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000191/// addPhysRegDeps - Add register dependencies (data, anti, and output) from
192/// this SUnit to following instructions in the same scheduling region that
193/// depend the physical register referenced at OperIdx.
194void ScheduleDAGInstrs::addPhysRegDeps(SUnit *SU, unsigned OperIdx) {
195 const MachineInstr *MI = SU->getInstr();
196 const MachineOperand &MO = MI->getOperand(OperIdx);
197 unsigned Reg = MO.getReg();
198
199 // Ask the target if address-backscheduling is desirable, and if so how much.
200 const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>();
201 unsigned SpecialAddressLatency = ST.getSpecialAddressLatency();
202
203 // Optionally add output and anti dependencies. For anti
204 // dependencies we use a latency of 0 because for a multi-issue
205 // target we want to allow the defining instruction to issue
206 // in the same cycle as the using instruction.
207 // TODO: Using a latency of 1 here for output dependencies assumes
208 // there's no cost for reusing registers.
209 SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
210 for (const unsigned *Alias = TRI->getOverlaps(Reg); *Alias; ++Alias) {
211 std::vector<SUnit *> &DefList = Defs[*Alias];
212 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
213 SUnit *DefSU = DefList[i];
214 if (DefSU == &ExitSU)
215 continue;
216 if (DefSU != SU &&
217 (Kind != SDep::Output || !MO.isDead() ||
218 !DefSU->getInstr()->registerDefIsDead(*Alias))) {
219 if (Kind == SDep::Anti)
220 DefSU->addPred(SDep(SU, Kind, 0, /*Reg=*/*Alias));
221 else {
222 unsigned AOLat = TII->getOutputLatency(InstrItins, MI, OperIdx,
223 DefSU->getInstr());
224 DefSU->addPred(SDep(SU, Kind, AOLat, /*Reg=*/*Alias));
225 }
226 }
227 }
228 }
229
230 // Retrieve the UseList to add data dependencies and update uses.
231 std::vector<SUnit *> &UseList = Uses[Reg];
232 if (MO.isDef()) {
233 // Update DefList. Defs are pushed in the order they are visited and
234 // never reordered.
235 std::vector<SUnit *> &DefList = Defs[Reg];
236
237 // Add any data dependencies.
238 unsigned DataLatency = SU->Latency;
239 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
240 SUnit *UseSU = UseList[i];
241 if (UseSU == SU)
242 continue;
243 unsigned LDataLatency = DataLatency;
244 // Optionally add in a special extra latency for nodes that
245 // feed addresses.
246 // TODO: Do this for register aliases too.
247 // TODO: Perhaps we should get rid of
248 // SpecialAddressLatency and just move this into
249 // adjustSchedDependency for the targets that care about it.
250 if (SpecialAddressLatency != 0 && !UnitLatencies &&
251 UseSU != &ExitSU) {
252 MachineInstr *UseMI = UseSU->getInstr();
253 const MCInstrDesc &UseMCID = UseMI->getDesc();
254 int RegUseIndex = UseMI->findRegisterUseOperandIdx(Reg);
255 assert(RegUseIndex >= 0 && "UseMI doesn's use register!");
256 if (RegUseIndex >= 0 &&
257 (UseMI->mayLoad() || UseMI->mayStore()) &&
258 (unsigned)RegUseIndex < UseMCID.getNumOperands() &&
259 UseMCID.OpInfo[RegUseIndex].isLookupPtrRegClass())
260 LDataLatency += SpecialAddressLatency;
261 }
262 // Adjust the dependence latency using operand def/use
263 // information (if any), and then allow the target to
264 // perform its own adjustments.
265 const SDep& dep = SDep(SU, SDep::Data, LDataLatency, Reg);
266 if (!UnitLatencies) {
267 ComputeOperandLatency(SU, UseSU, const_cast<SDep &>(dep));
268 ST.adjustSchedDependency(SU, UseSU, const_cast<SDep &>(dep));
269 }
270 UseSU->addPred(dep);
271 }
272 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
273 std::vector<SUnit *> &UseList = Uses[*Alias];
274 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
275 SUnit *UseSU = UseList[i];
276 if (UseSU == SU)
277 continue;
278 const SDep& dep = SDep(SU, SDep::Data, DataLatency, *Alias);
279 if (!UnitLatencies) {
280 ComputeOperandLatency(SU, UseSU, const_cast<SDep &>(dep));
281 ST.adjustSchedDependency(SU, UseSU, const_cast<SDep &>(dep));
282 }
283 UseSU->addPred(dep);
284 }
285 }
286
287 // If a def is going to wrap back around to the top of the loop,
288 // backschedule it.
289 if (!UnitLatencies && DefList.empty()) {
290 LoopDependencies::LoopDeps::iterator I = LoopRegs.Deps.find(Reg);
291 if (I != LoopRegs.Deps.end()) {
292 const MachineOperand *UseMO = I->second.first;
293 unsigned Count = I->second.second;
294 const MachineInstr *UseMI = UseMO->getParent();
295 unsigned UseMOIdx = UseMO - &UseMI->getOperand(0);
296 const MCInstrDesc &UseMCID = UseMI->getDesc();
297 // TODO: If we knew the total depth of the region here, we could
298 // handle the case where the whole loop is inside the region but
299 // is large enough that the isScheduleHigh trick isn't needed.
300 if (UseMOIdx < UseMCID.getNumOperands()) {
301 // Currently, we only support scheduling regions consisting of
302 // single basic blocks. Check to see if the instruction is in
303 // the same region by checking to see if it has the same parent.
304 if (UseMI->getParent() != MI->getParent()) {
305 unsigned Latency = SU->Latency;
306 if (UseMCID.OpInfo[UseMOIdx].isLookupPtrRegClass())
307 Latency += SpecialAddressLatency;
308 // This is a wild guess as to the portion of the latency which
309 // will be overlapped by work done outside the current
310 // scheduling region.
311 Latency -= std::min(Latency, Count);
312 // Add the artificial edge.
313 ExitSU.addPred(SDep(SU, SDep::Order, Latency,
314 /*Reg=*/0, /*isNormalMemory=*/false,
315 /*isMustAlias=*/false,
316 /*isArtificial=*/true));
317 } else if (SpecialAddressLatency > 0 &&
318 UseMCID.OpInfo[UseMOIdx].isLookupPtrRegClass()) {
319 // The entire loop body is within the current scheduling region
320 // and the latency of this operation is assumed to be greater
321 // than the latency of the loop.
322 // TODO: Recursively mark data-edge predecessors as
323 // isScheduleHigh too.
324 SU->isScheduleHigh = true;
325 }
326 }
327 LoopRegs.Deps.erase(I);
328 }
329 }
330
331 UseList.clear();
332 if (!MO.isDead())
333 DefList.clear();
334
335 // Calls will not be reordered because of chain dependencies (see
336 // below). Since call operands are dead, calls may continue to be added
337 // to the DefList making dependence checking quadratic in the size of
338 // the block. Instead, we leave only one call at the back of the
339 // DefList.
340 if (SU->isCall) {
341 while (!DefList.empty() && DefList.back()->isCall)
342 DefList.pop_back();
343 }
344 DefList.push_back(SU);
345 } else {
346 UseList.push_back(SU);
347 }
348}
349
Andrew Trick3c58ba82012-01-14 02:17:18 +0000350/// addVRegDefDeps - Add register output and data dependencies from this SUnit
351/// to instructions that occur later in the same scheduling region if they read
352/// from or write to the virtual register defined at OperIdx.
353///
354/// TODO: Hoist loop induction variable increments. This has to be
355/// reevaluated. Generally, IV scheduling should be done before coalescing.
356void ScheduleDAGInstrs::addVRegDefDeps(SUnit *SU, unsigned OperIdx) {
357 const MachineInstr *MI = SU->getInstr();
358 unsigned Reg = MI->getOperand(OperIdx).getReg();
359
360 const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>();
361
362 // Add output dependence to the next nearest def of this vreg.
363 //
364 // Unless this definition is dead, the output dependence should be
365 // transitively redundant with antidependencies from this definition's
366 // uses. We're conservative for now until we have a way to guarantee the uses
367 // are not eliminated sometime during scheduling. The output dependence edge
368 // is also useful if output latency exceeds def-use latency.
369 SUnit *DefSU = VRegDefs[Reg];
370 if (DefSU && DefSU != SU && DefSU != &ExitSU) {
371 unsigned OutLatency = TII->getOutputLatency(InstrItins, MI, OperIdx,
372 DefSU->getInstr());
373 DefSU->addPred(SDep(SU, SDep::Output, OutLatency, Reg));
374 }
375 VRegDefs[Reg] = SU;
376
377 // Add data dependence to any uses of this vreg before the next nearest def.
378 //
379 // TODO: Handle ExitSU properly.
380 //
381 // TODO: Data dependence could be handled more efficiently at the use-side.
382 std::vector<SUnit*> &UseList = VRegUses[Reg];
383 for (std::vector<SUnit*>::const_iterator UI = UseList.begin(),
384 UE = UseList.end(); UI != UE; ++UI) {
385 SUnit *UseSU = *UI;
386 if (UseSU == SU) continue;
387
388 // TODO: Handle "special" address latencies cleanly.
389 const SDep& dep = SDep(SU, SDep::Data, SU->Latency, Reg);
390 if (!UnitLatencies) {
391 // Adjust the dependence latency using operand def/use information, then
392 // allow the target to perform its own adjustments.
393 ComputeOperandLatency(SU, UseSU, const_cast<SDep &>(dep));
394 ST.adjustSchedDependency(SU, UseSU, const_cast<SDep &>(dep));
395 }
396 UseSU->addPred(dep);
397 }
398 UseList.clear();
399}
400
401/// addVRegUseDeps - Add register antidependencies from this SUnit to
402/// instructions that occur later in the same scheduling region if they
403/// write the virtual register referenced at OperIdx.
404void ScheduleDAGInstrs::addVRegUseDeps(SUnit *SU, unsigned OperIdx) {
405 unsigned Reg = SU->getInstr()->getOperand(OperIdx).getReg();
406
407 // Add antidependence to the following def of the vreg it uses.
408 SUnit *DefSU = VRegDefs[Reg];
409 if (DefSU && DefSU != SU)
410 DefSU->addPred(SDep(SU, SDep::Anti, 0, Reg));
411
412 // Add this SUnit to the use list of the vreg it uses.
413 //
414 // TODO: pinch the DAG before we see too many uses to avoid quadratic
415 // behavior. Limiting the scheduling window can accomplish the same thing.
416 VRegUses[Reg].push_back(SU);
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000417}
418
Dan Gohmana70dca12009-10-09 23:27:56 +0000419void ScheduleDAGInstrs::BuildSchedGraph(AliasAnalysis *AA) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000420 // We'll be allocating one SUnit for each instruction, plus one for
421 // the region exit node.
Dan Gohman343f0c02008-11-19 23:18:57 +0000422 SUnits.reserve(BB->size());
423
Dan Gohman6a9041e2008-12-04 01:35:46 +0000424 // We build scheduling units by walking a block's instruction list from bottom
425 // to top.
426
David Goodwin980d4942009-11-09 19:22:17 +0000427 // Remember where a generic side-effecting instruction is as we procede.
428 SUnit *BarrierChain = 0, *AliasChain = 0;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000429
David Goodwin980d4942009-11-09 19:22:17 +0000430 // Memory references to specific known memory locations are tracked
431 // so that they can be given more precise dependencies. We track
432 // separately the known memory locations that may alias and those
433 // that are known not to alias
434 std::map<const Value *, SUnit *> AliasMemDefs, NonAliasMemDefs;
435 std::map<const Value *, std::vector<SUnit *> > AliasMemUses, NonAliasMemUses;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000436
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000437 // Remove any stale debug info; sometimes BuildSchedGraph is called again
438 // without emitting the info from the previous call.
Devang Patelcf4cc842011-06-02 20:07:12 +0000439 DbgValues.clear();
440 FirstDbgValue = NULL;
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000441
Evan Chengec6906b2010-10-23 02:10:46 +0000442 // Model data dependencies between instructions being scheduled and the
443 // ExitSU.
444 AddSchedBarrierDeps();
445
Andrew Trick9b668532011-05-06 21:52:52 +0000446 for (int i = 0, e = TRI->getNumRegs(); i != e; ++i) {
447 assert(Defs[i].empty() && "Only BuildGraph should push/pop Defs");
448 }
449
Andrew Trick3c58ba82012-01-14 02:17:18 +0000450 // Reinitialize the large VReg vectors, while reusing the memory.
451 //
452 // Note: this can be an expensive part of DAG building. We may want to be more
453 // clever. Reevaluate after VRegUses goes away.
454 assert(VRegDefs.size() == 0 && VRegUses.size() == 0 &&
455 "Only BuildSchedGraph should access VRegDefs/Uses");
456 VRegDefs.resize(MF.getRegInfo().getNumVirtRegs());
457 VRegUses.resize(MF.getRegInfo().getNumVirtRegs());
458
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000459 // Walk the list of instructions, from bottom moving up.
Devang Patelcf4cc842011-06-02 20:07:12 +0000460 MachineInstr *PrevMI = NULL;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000461 for (MachineBasicBlock::iterator MII = InsertPos, MIE = Begin;
Dan Gohman343f0c02008-11-19 23:18:57 +0000462 MII != MIE; --MII) {
463 MachineInstr *MI = prior(MII);
Devang Patelcf4cc842011-06-02 20:07:12 +0000464 if (MI && PrevMI) {
465 DbgValues.push_back(std::make_pair(PrevMI, MI));
466 PrevMI = NULL;
467 }
468
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000469 if (MI->isDebugValue()) {
Devang Patelcf4cc842011-06-02 20:07:12 +0000470 PrevMI = MI;
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000471 continue;
472 }
Devang Patelcf4cc842011-06-02 20:07:12 +0000473
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000474 assert(!MI->isTerminator() && !MI->isLabel() &&
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000475 "Cannot schedule terminators or labels!");
476 // Create the SUnit for this MI.
Dan Gohman343f0c02008-11-19 23:18:57 +0000477 SUnit *SU = NewSUnit(MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000478 SU->isCall = MI->isCall();
479 SU->isCommutable = MI->isCommutable();
Dan Gohman343f0c02008-11-19 23:18:57 +0000480
Dan Gohman54e4c362008-12-09 22:54:47 +0000481 // Assign the Latency field of SU using target-provided information.
Dan Gohman3f237442008-12-16 03:25:46 +0000482 if (UnitLatencies)
483 SU->Latency = 1;
484 else
485 ComputeLatency(SU);
Dan Gohman54e4c362008-12-09 22:54:47 +0000486
Dan Gohman6a9041e2008-12-04 01:35:46 +0000487 // Add register-based dependencies (data, anti, and output).
Dan Gohman343f0c02008-11-19 23:18:57 +0000488 for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) {
489 const MachineOperand &MO = MI->getOperand(j);
490 if (!MO.isReg()) continue;
491 unsigned Reg = MO.getReg();
492 if (Reg == 0) continue;
493
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000494 if (TRI->isPhysicalRegister(Reg))
495 addPhysRegDeps(SU, j);
496 else {
497 assert(!IsPostRA && "Virtual register encountered!");
Andrew Trick3c58ba82012-01-14 02:17:18 +0000498 if (MO.isDef())
499 addVRegDefDeps(SU, j);
500 else
501 addVRegUseDeps(SU, j);
Dan Gohman343f0c02008-11-19 23:18:57 +0000502 }
503 }
Dan Gohman6a9041e2008-12-04 01:35:46 +0000504
505 // Add chain dependencies.
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000506 // Chain dependencies used to enforce memory order should have
507 // latency of 0 (except for true dependency of Store followed by
508 // aliased Load... we estimate that with a single cycle of latency
509 // assuming the hardware will bypass)
Dan Gohman6a9041e2008-12-04 01:35:46 +0000510 // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable
511 // after stack slots are lowered to actual addresses.
512 // TODO: Use an AliasAnalysis and do real alias-analysis queries, and
513 // produce more precise dependence information.
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000514#define STORE_LOAD_LATENCY 1
515 unsigned TrueMemOrderLatency = 0;
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000516 if (MI->isCall() || MI->hasUnmodeledSideEffects() ||
Andrew Trickf405b1a2011-05-05 19:24:06 +0000517 (MI->hasVolatileMemoryRef() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000518 (!MI->mayLoad() || !MI->isInvariantLoad(AA)))) {
David Goodwin980d4942009-11-09 19:22:17 +0000519 // Be conservative with these and add dependencies on all memory
520 // references, even those that are known to not alias.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000521 for (std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000522 NonAliasMemDefs.begin(), E = NonAliasMemDefs.end(); I != E; ++I) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000523 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000524 }
525 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000526 NonAliasMemUses.begin(), E = NonAliasMemUses.end(); I != E; ++I) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000527 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000528 I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000529 }
David Goodwin980d4942009-11-09 19:22:17 +0000530 NonAliasMemDefs.clear();
531 NonAliasMemUses.clear();
532 // Add SU to the barrier chain.
533 if (BarrierChain)
534 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
535 BarrierChain = SU;
536
537 // fall-through
538 new_alias_chain:
539 // Chain all possibly aliasing memory references though SU.
540 if (AliasChain)
541 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
542 AliasChain = SU;
543 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
544 PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
545 for (std::map<const Value *, SUnit *>::iterator I = AliasMemDefs.begin(),
546 E = AliasMemDefs.end(); I != E; ++I) {
547 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
548 }
549 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
550 AliasMemUses.begin(), E = AliasMemUses.end(); I != E; ++I) {
551 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
552 I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
553 }
554 PendingLoads.clear();
555 AliasMemDefs.clear();
556 AliasMemUses.clear();
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000557 } else if (MI->mayStore()) {
David Goodwina9e61072009-11-03 20:15:00 +0000558 bool MayAlias = true;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000559 TrueMemOrderLatency = STORE_LOAD_LATENCY;
David Goodwina9e61072009-11-03 20:15:00 +0000560 if (const Value *V = getUnderlyingObjectForInstr(MI, MFI, MayAlias)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000561 // A store to a specific PseudoSourceValue. Add precise dependencies.
David Goodwin980d4942009-11-09 19:22:17 +0000562 // Record the def in MemDefs, first adding a dep if there is
563 // an existing def.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000564 std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000565 ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000566 std::map<const Value *, SUnit *>::iterator IE =
David Goodwin980d4942009-11-09 19:22:17 +0000567 ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end());
568 if (I != IE) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000569 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0,
Dan Gohman54e4c362008-12-09 22:54:47 +0000570 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000571 I->second = SU;
572 } else {
David Goodwin980d4942009-11-09 19:22:17 +0000573 if (MayAlias)
574 AliasMemDefs[V] = SU;
575 else
576 NonAliasMemDefs[V] = SU;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000577 }
578 // Handle the uses in MemUses, if there are any.
Dan Gohmana629b482008-12-08 17:50:35 +0000579 std::map<const Value *, std::vector<SUnit *> >::iterator J =
David Goodwin980d4942009-11-09 19:22:17 +0000580 ((MayAlias) ? AliasMemUses.find(V) : NonAliasMemUses.find(V));
581 std::map<const Value *, std::vector<SUnit *> >::iterator JE =
582 ((MayAlias) ? AliasMemUses.end() : NonAliasMemUses.end());
583 if (J != JE) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000584 for (unsigned i = 0, e = J->second.size(); i != e; ++i)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000585 J->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency,
586 /*Reg=*/0, /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000587 J->second.clear();
588 }
David Goodwina9e61072009-11-03 20:15:00 +0000589 if (MayAlias) {
David Goodwin980d4942009-11-09 19:22:17 +0000590 // Add dependencies from all the PendingLoads, i.e. loads
591 // with no underlying object.
David Goodwina9e61072009-11-03 20:15:00 +0000592 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
593 PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
David Goodwin980d4942009-11-09 19:22:17 +0000594 // Add dependence on alias chain, if needed.
595 if (AliasChain)
596 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
David Goodwina9e61072009-11-03 20:15:00 +0000597 }
David Goodwin980d4942009-11-09 19:22:17 +0000598 // Add dependence on barrier chain, if needed.
599 if (BarrierChain)
600 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
David Goodwin5be870a2009-11-05 00:16:44 +0000601 } else {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000602 // Treat all other stores conservatively.
David Goodwin980d4942009-11-09 19:22:17 +0000603 goto new_alias_chain;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000604 }
Evan Chengec6906b2010-10-23 02:10:46 +0000605
606 if (!ExitSU.isPred(SU))
607 // Push store's up a bit to avoid them getting in between cmp
608 // and branches.
609 ExitSU.addPred(SDep(SU, SDep::Order, 0,
610 /*Reg=*/0, /*isNormalMemory=*/false,
611 /*isMustAlias=*/false,
612 /*isArtificial=*/true));
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000613 } else if (MI->mayLoad()) {
David Goodwina9e61072009-11-03 20:15:00 +0000614 bool MayAlias = true;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000615 TrueMemOrderLatency = 0;
Dan Gohmana70dca12009-10-09 23:27:56 +0000616 if (MI->isInvariantLoad(AA)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000617 // Invariant load, no chain dependencies needed!
David Goodwin5be870a2009-11-05 00:16:44 +0000618 } else {
Andrew Trickf405b1a2011-05-05 19:24:06 +0000619 if (const Value *V =
David Goodwin980d4942009-11-09 19:22:17 +0000620 getUnderlyingObjectForInstr(MI, MFI, MayAlias)) {
621 // A load from a specific PseudoSourceValue. Add precise dependencies.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000622 std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000623 ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000624 std::map<const Value *, SUnit *>::iterator IE =
David Goodwin980d4942009-11-09 19:22:17 +0000625 ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end());
626 if (I != IE)
627 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0,
628 /*isNormalMemory=*/true));
629 if (MayAlias)
630 AliasMemUses[V].push_back(SU);
Andrew Trickf405b1a2011-05-05 19:24:06 +0000631 else
David Goodwin980d4942009-11-09 19:22:17 +0000632 NonAliasMemUses[V].push_back(SU);
633 } else {
634 // A load with no underlying object. Depend on all
635 // potentially aliasing stores.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000636 for (std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000637 AliasMemDefs.begin(), E = AliasMemDefs.end(); I != E; ++I)
638 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000639
David Goodwin980d4942009-11-09 19:22:17 +0000640 PendingLoads.push_back(SU);
641 MayAlias = true;
David Goodwina9e61072009-11-03 20:15:00 +0000642 }
Andrew Trickf405b1a2011-05-05 19:24:06 +0000643
David Goodwin980d4942009-11-09 19:22:17 +0000644 // Add dependencies on alias and barrier chains, if needed.
645 if (MayAlias && AliasChain)
646 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
647 if (BarrierChain)
648 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000649 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000650 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000651 }
Devang Patelcf4cc842011-06-02 20:07:12 +0000652 if (PrevMI)
653 FirstDbgValue = PrevMI;
Dan Gohman79ce2762009-01-15 19:20:50 +0000654
655 for (int i = 0, e = TRI->getNumRegs(); i != e; ++i) {
656 Defs[i].clear();
657 Uses[i].clear();
658 }
Andrew Trick3c58ba82012-01-14 02:17:18 +0000659 VRegDefs.clear();
660 VRegUses.clear();
Dan Gohman79ce2762009-01-15 19:20:50 +0000661 PendingLoads.clear();
Dan Gohman343f0c02008-11-19 23:18:57 +0000662}
663
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000664void ScheduleDAGInstrs::FinishBlock() {
665 // Nothing to do.
666}
667
Dan Gohmanc8c28272008-11-21 00:12:10 +0000668void ScheduleDAGInstrs::ComputeLatency(SUnit *SU) {
David Goodwind94a4e52009-08-10 15:55:25 +0000669 // Compute the latency for the node.
Evan Cheng3ef1c872010-09-10 01:29:16 +0000670 if (!InstrItins || InstrItins->isEmpty()) {
671 SU->Latency = 1;
Dan Gohman4ea8e852008-12-16 02:38:22 +0000672
Evan Cheng3ef1c872010-09-10 01:29:16 +0000673 // Simplistic target-independent heuristic: assume that loads take
674 // extra time.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000675 if (SU->getInstr()->mayLoad())
Dan Gohman4ea8e852008-12-16 02:38:22 +0000676 SU->Latency += 2;
Evan Cheng8239daf2010-11-03 00:45:17 +0000677 } else {
678 SU->Latency = TII->getInstrLatency(InstrItins, SU->getInstr());
679 }
Dan Gohmanc8c28272008-11-21 00:12:10 +0000680}
681
Andrew Trickf405b1a2011-05-05 19:24:06 +0000682void ScheduleDAGInstrs::ComputeOperandLatency(SUnit *Def, SUnit *Use,
David Goodwindc4bdcd2009-08-19 16:08:58 +0000683 SDep& dep) const {
Evan Cheng3ef1c872010-09-10 01:29:16 +0000684 if (!InstrItins || InstrItins->isEmpty())
David Goodwindc4bdcd2009-08-19 16:08:58 +0000685 return;
Andrew Trickf405b1a2011-05-05 19:24:06 +0000686
David Goodwindc4bdcd2009-08-19 16:08:58 +0000687 // For a data dependency with a known register...
688 if ((dep.getKind() != SDep::Data) || (dep.getReg() == 0))
689 return;
690
691 const unsigned Reg = dep.getReg();
692
693 // ... find the definition of the register in the defining
694 // instruction
695 MachineInstr *DefMI = Def->getInstr();
696 int DefIdx = DefMI->findRegisterDefOperandIdx(Reg);
697 if (DefIdx != -1) {
Evan Cheng1aca5bc2010-10-08 18:42:25 +0000698 const MachineOperand &MO = DefMI->getOperand(DefIdx);
699 if (MO.isReg() && MO.isImplicit() &&
Evan Chengd82de832010-10-08 23:01:57 +0000700 DefIdx >= (int)DefMI->getDesc().getNumOperands()) {
Evan Cheng1aca5bc2010-10-08 18:42:25 +0000701 // This is an implicit def, getOperandLatency() won't return the correct
702 // latency. e.g.
703 // %D6<def>, %D7<def> = VLD1q16 %R2<kill>, 0, ..., %Q3<imp-def>
704 // %Q1<def> = VMULv8i16 %Q1<kill>, %Q3<kill>, ...
705 // What we want is to compute latency between def of %D6/%D7 and use of
706 // %Q3 instead.
707 DefIdx = DefMI->findRegisterDefOperandIdx(Reg, false, true, TRI);
708 }
Evan Chenga0792de2010-10-06 06:27:31 +0000709 MachineInstr *UseMI = Use->getInstr();
Evan Cheng3881cb72010-09-29 22:42:35 +0000710 // For all uses of the register, calculate the maxmimum latency
711 int Latency = -1;
Evan Chengec6906b2010-10-23 02:10:46 +0000712 if (UseMI) {
713 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
714 const MachineOperand &MO = UseMI->getOperand(i);
715 if (!MO.isReg() || !MO.isUse())
716 continue;
717 unsigned MOReg = MO.getReg();
718 if (MOReg != Reg)
719 continue;
David Goodwindc4bdcd2009-08-19 16:08:58 +0000720
Evan Chengec6906b2010-10-23 02:10:46 +0000721 int UseCycle = TII->getOperandLatency(InstrItins, DefMI, DefIdx,
722 UseMI, i);
723 Latency = std::max(Latency, UseCycle);
724 }
725 } else {
726 // UseMI is null, then it must be a scheduling barrier.
727 if (!InstrItins || InstrItins->isEmpty())
728 return;
729 unsigned DefClass = DefMI->getDesc().getSchedClass();
730 Latency = InstrItins->getOperandCycle(DefClass, DefIdx);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000731 }
Evan Chengec6906b2010-10-23 02:10:46 +0000732
733 // If we found a latency, then replace the existing dependence latency.
734 if (Latency >= 0)
735 dep.setLatency(Latency);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000736 }
737}
738
Dan Gohman343f0c02008-11-19 23:18:57 +0000739void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
740 SU->getInstr()->dump();
741}
742
743std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
744 std::string s;
745 raw_string_ostream oss(s);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000746 if (SU == &EntrySU)
747 oss << "<entry>";
748 else if (SU == &ExitSU)
749 oss << "<exit>";
750 else
751 SU->getInstr()->print(oss);
Dan Gohman343f0c02008-11-19 23:18:57 +0000752 return oss.str();
753}
754
755// EmitSchedule - Emit the machine code in scheduled order.
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000756MachineBasicBlock *ScheduleDAGInstrs::EmitSchedule() {
Evan Chengddfd1372011-12-14 02:11:42 +0000757 Begin = InsertPos;
Dan Gohman343f0c02008-11-19 23:18:57 +0000758
Devang Patelcf4cc842011-06-02 20:07:12 +0000759 // If first instruction was a DBG_VALUE then put it back.
760 if (FirstDbgValue)
Evan Chengddfd1372011-12-14 02:11:42 +0000761 BB->splice(InsertPos, BB, FirstDbgValue);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000762
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000763 // Then re-insert them according to the given schedule.
Dan Gohman343f0c02008-11-19 23:18:57 +0000764 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
Devang Patelee1f8782011-06-02 21:31:00 +0000765 if (SUnit *SU = Sequence[i])
Evan Chengddfd1372011-12-14 02:11:42 +0000766 BB->splice(InsertPos, BB, SU->getInstr());
Devang Patelee1f8782011-06-02 21:31:00 +0000767 else
Dan Gohman343f0c02008-11-19 23:18:57 +0000768 // Null SUnit* is a noop.
769 EmitNoop();
Dan Gohman343f0c02008-11-19 23:18:57 +0000770
Hal Finkeldb809e02011-12-02 04:58:07 +0000771 // Update the Begin iterator, as the first instruction in the block
772 // may have been scheduled later.
773 if (i == 0)
774 Begin = prior(InsertPos);
775 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000776
Devang Patelcf4cc842011-06-02 20:07:12 +0000777 // Reinsert any remaining debug_values.
778 for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator
779 DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
780 std::pair<MachineInstr *, MachineInstr *> P = *prior(DI);
781 MachineInstr *DbgValue = P.first;
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000782 MachineBasicBlock::iterator OrigPrivMI = P.second;
Evan Chengddfd1372011-12-14 02:11:42 +0000783 BB->splice(++OrigPrivMI, BB, DbgValue);
Devang Patelcf4cc842011-06-02 20:07:12 +0000784 }
785 DbgValues.clear();
786 FirstDbgValue = NULL;
Dan Gohman343f0c02008-11-19 23:18:57 +0000787 return BB;
788}