blob: 5c1247f987aca82a73688529dbf91caf6b0d54a9 [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"
Andrew Trickb4566a92012-02-22 06:08:11 +000020#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Dan Gohman3f237442008-12-16 03:25:46 +000021#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohmanc76909a2009-09-25 20:36:54 +000022#include "llvm/CodeGen/MachineMemOperand.h"
Dan Gohman3f237442008-12-16 03:25:46 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman6a9041e2008-12-04 01:35:46 +000024#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chengab8be962011-06-29 01:14:12 +000025#include "llvm/MC/MCInstrItineraries.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000026#include "llvm/Target/TargetMachine.h"
27#include "llvm/Target/TargetInstrInfo.h"
28#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng5b1b44892011-07-01 21:01:15 +000029#include "llvm/Target/TargetSubtargetInfo.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000030#include "llvm/Support/Debug.h"
31#include "llvm/Support/raw_ostream.h"
Dan Gohman3f237442008-12-16 03:25:46 +000032#include "llvm/ADT/SmallSet.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000033using namespace llvm;
34
Dan Gohman79ce2762009-01-15 19:20:50 +000035ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf,
Dan Gohman3f237442008-12-16 03:25:46 +000036 const MachineLoopInfo &mli,
Andrew Trick5e920d72012-01-14 02:17:12 +000037 const MachineDominatorTree &mdt,
Andrew Trickb4566a92012-02-22 06:08:11 +000038 bool IsPostRAFlag,
39 LiveIntervals *lis)
Evan Cheng3ef1c872010-09-10 01:29:16 +000040 : ScheduleDAG(mf), MLI(mli), MDT(mdt), MFI(mf.getFrameInfo()),
Andrew Trick5e920d72012-01-14 02:17:12 +000041 InstrItins(mf.getTarget().getInstrItineraryData()), IsPostRA(IsPostRAFlag),
Andrew Trickb4566a92012-02-22 06:08:11 +000042 LIS(lis), UnitLatencies(false),
43 Defs(TRI->getNumRegs()), Uses(TRI->getNumRegs()),
Devang Patele29e8e12011-06-02 21:26:52 +000044 LoopRegs(MLI, MDT), FirstDbgValue(0) {
Andrew Trickb4566a92012-02-22 06:08:11 +000045 assert((IsPostRA || LIS) && "PreRA scheduling requires LiveIntervals");
Devang Patelcf4cc842011-06-02 20:07:12 +000046 DbgValues.clear();
Andrew Trickcc77b542012-02-22 06:08:13 +000047 assert(!(IsPostRA && MRI.getNumVirtRegs()) &&
Andrew Trick19273ae2012-02-21 04:51:23 +000048 "Virtual registers must be removed prior to PostRA scheduling");
Evan Cheng38bdfc62009-10-18 19:58:47 +000049}
Dan Gohman343f0c02008-11-19 23:18:57 +000050
Dan Gohman47ac0f02009-02-11 04:27:20 +000051/// Run - perform scheduling.
52///
53void ScheduleDAGInstrs::Run(MachineBasicBlock *bb,
54 MachineBasicBlock::iterator begin,
55 MachineBasicBlock::iterator end,
56 unsigned endcount) {
57 BB = bb;
58 Begin = begin;
59 InsertPosIndex = endcount;
60
Andrew Trick7ebcaf42012-01-14 02:17:15 +000061 // Check to see if the scheduler cares about latencies.
62 UnitLatencies = ForceUnitLatencies();
63
Dan Gohman47ac0f02009-02-11 04:27:20 +000064 ScheduleDAG::Run(bb, end);
65}
66
Dan Gohman3311a1f2009-01-30 02:49:14 +000067/// getUnderlyingObjectFromInt - This is the function that does the work of
68/// looking through basic ptrtoint+arithmetic+inttoptr sequences.
69static const Value *getUnderlyingObjectFromInt(const Value *V) {
70 do {
Dan Gohman8906f952009-07-17 20:58:59 +000071 if (const Operator *U = dyn_cast<Operator>(V)) {
Dan Gohman3311a1f2009-01-30 02:49:14 +000072 // If we find a ptrtoint, we can transfer control back to the
73 // regular getUnderlyingObjectFromInt.
Dan Gohman8906f952009-07-17 20:58:59 +000074 if (U->getOpcode() == Instruction::PtrToInt)
Dan Gohman3311a1f2009-01-30 02:49:14 +000075 return U->getOperand(0);
76 // If we find an add of a constant or a multiplied value, it's
77 // likely that the other operand will lead us to the base
78 // object. We don't have to worry about the case where the
Dan Gohman748f98f2009-08-07 01:26:06 +000079 // object address is somehow being computed by the multiply,
Dan Gohman3311a1f2009-01-30 02:49:14 +000080 // because our callers only care when the result is an
81 // identifibale object.
Dan Gohman8906f952009-07-17 20:58:59 +000082 if (U->getOpcode() != Instruction::Add ||
Dan Gohman3311a1f2009-01-30 02:49:14 +000083 (!isa<ConstantInt>(U->getOperand(1)) &&
Dan Gohman8906f952009-07-17 20:58:59 +000084 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul))
Dan Gohman3311a1f2009-01-30 02:49:14 +000085 return V;
86 V = U->getOperand(0);
87 } else {
88 return V;
89 }
Duncan Sands1df98592010-02-16 11:11:14 +000090 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
Dan Gohman3311a1f2009-01-30 02:49:14 +000091 } while (1);
92}
93
Dan Gohman5034dd32010-12-15 20:02:24 +000094/// getUnderlyingObject - This is a wrapper around GetUnderlyingObject
Dan Gohman3311a1f2009-01-30 02:49:14 +000095/// and adds support for basic ptrtoint+arithmetic+inttoptr sequences.
96static const Value *getUnderlyingObject(const Value *V) {
97 // First just call Value::getUnderlyingObject to let it do what it does.
98 do {
Dan Gohman5034dd32010-12-15 20:02:24 +000099 V = GetUnderlyingObject(V);
Dan Gohman3311a1f2009-01-30 02:49:14 +0000100 // If it found an inttoptr, use special code to continue climing.
Dan Gohman8906f952009-07-17 20:58:59 +0000101 if (Operator::getOpcode(V) != Instruction::IntToPtr)
Dan Gohman3311a1f2009-01-30 02:49:14 +0000102 break;
103 const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
104 // If that succeeded in finding a pointer, continue the search.
Duncan Sands1df98592010-02-16 11:11:14 +0000105 if (!O->getType()->isPointerTy())
Dan Gohman3311a1f2009-01-30 02:49:14 +0000106 break;
107 V = O;
108 } while (1);
109 return V;
110}
111
112/// getUnderlyingObjectForInstr - If this machine instr has memory reference
113/// information and it can be tracked to a normal reference to a known
114/// object, return the Value for that object. Otherwise return null.
Evan Cheng38bdfc62009-10-18 19:58:47 +0000115static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI,
David Goodwina9e61072009-11-03 20:15:00 +0000116 const MachineFrameInfo *MFI,
117 bool &MayAlias) {
118 MayAlias = true;
Dan Gohman3311a1f2009-01-30 02:49:14 +0000119 if (!MI->hasOneMemOperand() ||
Dan Gohmanc76909a2009-09-25 20:36:54 +0000120 !(*MI->memoperands_begin())->getValue() ||
121 (*MI->memoperands_begin())->isVolatile())
Dan Gohman3311a1f2009-01-30 02:49:14 +0000122 return 0;
123
Dan Gohmanc76909a2009-09-25 20:36:54 +0000124 const Value *V = (*MI->memoperands_begin())->getValue();
Dan Gohman3311a1f2009-01-30 02:49:14 +0000125 if (!V)
126 return 0;
127
128 V = getUnderlyingObject(V);
Evan Chengff89dcb2009-10-18 18:16:27 +0000129 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
130 // For now, ignore PseudoSourceValues which may alias LLVM IR values
131 // because the code that uses this function has no way to cope with
132 // such aliases.
Evan Cheng38bdfc62009-10-18 19:58:47 +0000133 if (PSV->isAliased(MFI))
Evan Chengff89dcb2009-10-18 18:16:27 +0000134 return 0;
Andrew Trickf405b1a2011-05-05 19:24:06 +0000135
David Goodwin980d4942009-11-09 19:22:17 +0000136 MayAlias = PSV->mayAlias(MFI);
Evan Chengff89dcb2009-10-18 18:16:27 +0000137 return V;
138 }
Dan Gohman3311a1f2009-01-30 02:49:14 +0000139
Evan Chengff89dcb2009-10-18 18:16:27 +0000140 if (isIdentifiedObject(V))
141 return V;
142
143 return 0;
Dan Gohman3311a1f2009-01-30 02:49:14 +0000144}
145
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000146void ScheduleDAGInstrs::StartBlock(MachineBasicBlock *BB) {
Andrew Tricke8deca82011-10-07 06:33:09 +0000147 LoopRegs.Deps.clear();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000148 if (MachineLoop *ML = MLI.getLoopFor(BB))
Evan Cheng977679d2012-01-07 03:02:36 +0000149 if (BB == ML->getLoopLatch())
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000150 LoopRegs.VisitLoop(ML);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000151}
152
Evan Chengec6906b2010-10-23 02:10:46 +0000153/// AddSchedBarrierDeps - Add dependencies from instructions in the current
154/// list of instructions being scheduled to scheduling barrier by adding
155/// the exit SU to the register defs and use list. This is because we want to
156/// make sure instructions which define registers that are either used by
157/// the terminator or are live-out are properly scheduled. This is
158/// especially important when the definition latency of the return value(s)
159/// are too high to be hidden by the branch or when the liveout registers
160/// used by instructions in the fallthrough block.
161void ScheduleDAGInstrs::AddSchedBarrierDeps() {
162 MachineInstr *ExitMI = InsertPos != BB->end() ? &*InsertPos : 0;
163 ExitSU.setInstr(ExitMI);
164 bool AllDepKnown = ExitMI &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000165 (ExitMI->isCall() || ExitMI->isBarrier());
Evan Chengec6906b2010-10-23 02:10:46 +0000166 if (ExitMI && AllDepKnown) {
167 // If it's a call or a barrier, add dependencies on the defs and uses of
168 // instruction.
169 for (unsigned i = 0, e = ExitMI->getNumOperands(); i != e; ++i) {
170 const MachineOperand &MO = ExitMI->getOperand(i);
171 if (!MO.isReg() || MO.isDef()) continue;
172 unsigned Reg = MO.getReg();
173 if (Reg == 0) continue;
174
Andrew Trick3c58ba82012-01-14 02:17:18 +0000175 if (TRI->isPhysicalRegister(Reg))
176 Uses[Reg].push_back(&ExitSU);
177 else
178 assert(!IsPostRA && "Virtual register encountered after regalloc.");
Evan Chengec6906b2010-10-23 02:10:46 +0000179 }
180 } else {
181 // For others, e.g. fallthrough, conditional branch, assume the exit
Evan Chengde5fa932010-10-27 23:17:17 +0000182 // uses all the registers that are livein to the successor blocks.
183 SmallSet<unsigned, 8> Seen;
184 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
185 SE = BB->succ_end(); SI != SE; ++SI)
186 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
Andrew Trickf405b1a2011-05-05 19:24:06 +0000187 E = (*SI)->livein_end(); I != E; ++I) {
Evan Chengde5fa932010-10-27 23:17:17 +0000188 unsigned Reg = *I;
189 if (Seen.insert(Reg))
190 Uses[Reg].push_back(&ExitSU);
191 }
Evan Chengec6906b2010-10-23 02:10:46 +0000192 }
193}
194
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000195/// addPhysRegDeps - Add register dependencies (data, anti, and output) from
196/// this SUnit to following instructions in the same scheduling region that
197/// depend the physical register referenced at OperIdx.
198void ScheduleDAGInstrs::addPhysRegDeps(SUnit *SU, unsigned OperIdx) {
199 const MachineInstr *MI = SU->getInstr();
200 const MachineOperand &MO = MI->getOperand(OperIdx);
201 unsigned Reg = MO.getReg();
202
203 // Ask the target if address-backscheduling is desirable, and if so how much.
204 const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>();
205 unsigned SpecialAddressLatency = ST.getSpecialAddressLatency();
206
207 // Optionally add output and anti dependencies. For anti
208 // dependencies we use a latency of 0 because for a multi-issue
209 // target we want to allow the defining instruction to issue
210 // in the same cycle as the using instruction.
211 // TODO: Using a latency of 1 here for output dependencies assumes
212 // there's no cost for reusing registers.
213 SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
214 for (const unsigned *Alias = TRI->getOverlaps(Reg); *Alias; ++Alias) {
215 std::vector<SUnit *> &DefList = Defs[*Alias];
216 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
217 SUnit *DefSU = DefList[i];
218 if (DefSU == &ExitSU)
219 continue;
220 if (DefSU != SU &&
221 (Kind != SDep::Output || !MO.isDead() ||
222 !DefSU->getInstr()->registerDefIsDead(*Alias))) {
223 if (Kind == SDep::Anti)
224 DefSU->addPred(SDep(SU, Kind, 0, /*Reg=*/*Alias));
225 else {
226 unsigned AOLat = TII->getOutputLatency(InstrItins, MI, OperIdx,
227 DefSU->getInstr());
228 DefSU->addPred(SDep(SU, Kind, AOLat, /*Reg=*/*Alias));
229 }
230 }
231 }
232 }
233
234 // Retrieve the UseList to add data dependencies and update uses.
235 std::vector<SUnit *> &UseList = Uses[Reg];
236 if (MO.isDef()) {
237 // Update DefList. Defs are pushed in the order they are visited and
238 // never reordered.
239 std::vector<SUnit *> &DefList = Defs[Reg];
240
241 // Add any data dependencies.
242 unsigned DataLatency = SU->Latency;
243 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
244 SUnit *UseSU = UseList[i];
245 if (UseSU == SU)
246 continue;
247 unsigned LDataLatency = DataLatency;
248 // Optionally add in a special extra latency for nodes that
249 // feed addresses.
250 // TODO: Do this for register aliases too.
251 // TODO: Perhaps we should get rid of
252 // SpecialAddressLatency and just move this into
253 // adjustSchedDependency for the targets that care about it.
254 if (SpecialAddressLatency != 0 && !UnitLatencies &&
255 UseSU != &ExitSU) {
256 MachineInstr *UseMI = UseSU->getInstr();
257 const MCInstrDesc &UseMCID = UseMI->getDesc();
258 int RegUseIndex = UseMI->findRegisterUseOperandIdx(Reg);
259 assert(RegUseIndex >= 0 && "UseMI doesn's use register!");
260 if (RegUseIndex >= 0 &&
261 (UseMI->mayLoad() || UseMI->mayStore()) &&
262 (unsigned)RegUseIndex < UseMCID.getNumOperands() &&
263 UseMCID.OpInfo[RegUseIndex].isLookupPtrRegClass())
264 LDataLatency += SpecialAddressLatency;
265 }
266 // Adjust the dependence latency using operand def/use
267 // information (if any), and then allow the target to
268 // perform its own adjustments.
269 const SDep& dep = SDep(SU, SDep::Data, LDataLatency, Reg);
270 if (!UnitLatencies) {
271 ComputeOperandLatency(SU, UseSU, const_cast<SDep &>(dep));
272 ST.adjustSchedDependency(SU, UseSU, const_cast<SDep &>(dep));
273 }
274 UseSU->addPred(dep);
275 }
276 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
277 std::vector<SUnit *> &UseList = Uses[*Alias];
278 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
279 SUnit *UseSU = UseList[i];
280 if (UseSU == SU)
281 continue;
282 const SDep& dep = SDep(SU, SDep::Data, DataLatency, *Alias);
283 if (!UnitLatencies) {
284 ComputeOperandLatency(SU, UseSU, const_cast<SDep &>(dep));
285 ST.adjustSchedDependency(SU, UseSU, const_cast<SDep &>(dep));
286 }
287 UseSU->addPred(dep);
288 }
289 }
290
291 // If a def is going to wrap back around to the top of the loop,
292 // backschedule it.
293 if (!UnitLatencies && DefList.empty()) {
294 LoopDependencies::LoopDeps::iterator I = LoopRegs.Deps.find(Reg);
295 if (I != LoopRegs.Deps.end()) {
296 const MachineOperand *UseMO = I->second.first;
297 unsigned Count = I->second.second;
298 const MachineInstr *UseMI = UseMO->getParent();
299 unsigned UseMOIdx = UseMO - &UseMI->getOperand(0);
300 const MCInstrDesc &UseMCID = UseMI->getDesc();
301 // TODO: If we knew the total depth of the region here, we could
302 // handle the case where the whole loop is inside the region but
303 // is large enough that the isScheduleHigh trick isn't needed.
304 if (UseMOIdx < UseMCID.getNumOperands()) {
305 // Currently, we only support scheduling regions consisting of
306 // single basic blocks. Check to see if the instruction is in
307 // the same region by checking to see if it has the same parent.
308 if (UseMI->getParent() != MI->getParent()) {
309 unsigned Latency = SU->Latency;
310 if (UseMCID.OpInfo[UseMOIdx].isLookupPtrRegClass())
311 Latency += SpecialAddressLatency;
312 // This is a wild guess as to the portion of the latency which
313 // will be overlapped by work done outside the current
314 // scheduling region.
315 Latency -= std::min(Latency, Count);
316 // Add the artificial edge.
317 ExitSU.addPred(SDep(SU, SDep::Order, Latency,
318 /*Reg=*/0, /*isNormalMemory=*/false,
319 /*isMustAlias=*/false,
320 /*isArtificial=*/true));
321 } else if (SpecialAddressLatency > 0 &&
322 UseMCID.OpInfo[UseMOIdx].isLookupPtrRegClass()) {
323 // The entire loop body is within the current scheduling region
324 // and the latency of this operation is assumed to be greater
325 // than the latency of the loop.
326 // TODO: Recursively mark data-edge predecessors as
327 // isScheduleHigh too.
328 SU->isScheduleHigh = true;
329 }
330 }
331 LoopRegs.Deps.erase(I);
332 }
333 }
334
335 UseList.clear();
336 if (!MO.isDead())
337 DefList.clear();
338
339 // Calls will not be reordered because of chain dependencies (see
340 // below). Since call operands are dead, calls may continue to be added
341 // to the DefList making dependence checking quadratic in the size of
342 // the block. Instead, we leave only one call at the back of the
343 // DefList.
344 if (SU->isCall) {
345 while (!DefList.empty() && DefList.back()->isCall)
346 DefList.pop_back();
347 }
348 DefList.push_back(SU);
349 } else {
350 UseList.push_back(SU);
351 }
352}
353
Andrew Trick3c58ba82012-01-14 02:17:18 +0000354/// addVRegDefDeps - Add register output and data dependencies from this SUnit
355/// to instructions that occur later in the same scheduling region if they read
356/// from or write to the virtual register defined at OperIdx.
357///
358/// TODO: Hoist loop induction variable increments. This has to be
359/// reevaluated. Generally, IV scheduling should be done before coalescing.
360void ScheduleDAGInstrs::addVRegDefDeps(SUnit *SU, unsigned OperIdx) {
361 const MachineInstr *MI = SU->getInstr();
362 unsigned Reg = MI->getOperand(OperIdx).getReg();
363
Andrew Trickcc77b542012-02-22 06:08:13 +0000364 // SSA defs do not have output/anti dependencies.
Andrew Trick2fc09772012-02-22 18:34:49 +0000365 // The current operand is a def, so we have at least one.
Andrew Trickcc77b542012-02-22 06:08:13 +0000366 if (llvm::next(MRI.def_begin(Reg)) == MRI.def_end())
367 return;
368
Andrew Trick3c58ba82012-01-14 02:17:18 +0000369 // Add output dependence to the next nearest def of this vreg.
370 //
371 // Unless this definition is dead, the output dependence should be
372 // transitively redundant with antidependencies from this definition's
373 // uses. We're conservative for now until we have a way to guarantee the uses
374 // are not eliminated sometime during scheduling. The output dependence edge
375 // is also useful if output latency exceeds def-use latency.
Andrew Trick8ae3ac72012-02-22 21:59:00 +0000376 VReg2SUnitMap::iterator DefI = findVRegDef(Reg);
377 if (DefI == VRegDefs.end())
378 VRegDefs.insert(VReg2SUnit(Reg, SU));
379 else {
380 SUnit *DefSU = DefI->SU;
381 if (DefSU != SU && DefSU != &ExitSU) {
382 unsigned OutLatency = TII->getOutputLatency(InstrItins, MI, OperIdx,
383 DefSU->getInstr());
384 DefSU->addPred(SDep(SU, SDep::Output, OutLatency, Reg));
385 }
386 DefI->SU = SU;
Andrew Trick3c58ba82012-01-14 02:17:18 +0000387 }
Andrew Trick3c58ba82012-01-14 02:17:18 +0000388}
389
Andrew Trickb4566a92012-02-22 06:08:11 +0000390/// addVRegUseDeps - Add a register data dependency if the instruction that
391/// defines the virtual register used at OperIdx is mapped to an SUnit. Add a
392/// register antidependency from this SUnit to instructions that occur later in
393/// the same scheduling region if they write the virtual register.
394///
395/// TODO: Handle ExitSU "uses" properly.
Andrew Trick3c58ba82012-01-14 02:17:18 +0000396void ScheduleDAGInstrs::addVRegUseDeps(SUnit *SU, unsigned OperIdx) {
Andrew Trickb4566a92012-02-22 06:08:11 +0000397 MachineInstr *MI = SU->getInstr();
398 unsigned Reg = MI->getOperand(OperIdx).getReg();
399
400 // Lookup this operand's reaching definition.
401 assert(LIS && "vreg dependencies requires LiveIntervals");
402 SlotIndex UseIdx = LIS->getSlotIndexes()->getInstructionIndex(MI);
403 LiveInterval *LI = &LIS->getInterval(Reg);
404 VNInfo *VNI = LI->getVNInfoAt(UseIdx);
405 MachineInstr *Def = LIS->getInstructionFromIndex(VNI->def);
406 if (Def) {
407 SUnit *DefSU = getSUnit(Def);
408 if (DefSU) {
409 // The reaching Def lives within this scheduling region.
410 // Create a data dependence.
411 //
412 // TODO: Handle "special" address latencies cleanly.
413 const SDep &dep = SDep(DefSU, SDep::Data, DefSU->Latency, Reg);
414 if (!UnitLatencies) {
415 // Adjust the dependence latency using operand def/use information, then
416 // allow the target to perform its own adjustments.
417 ComputeOperandLatency(DefSU, SU, const_cast<SDep &>(dep));
418 const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>();
419 ST.adjustSchedDependency(DefSU, SU, const_cast<SDep &>(dep));
420 }
421 SU->addPred(dep);
422 }
423 }
Andrew Trick3c58ba82012-01-14 02:17:18 +0000424
425 // Add antidependence to the following def of the vreg it uses.
Andrew Trick8ae3ac72012-02-22 21:59:00 +0000426 VReg2SUnitMap::iterator DefI = findVRegDef(Reg);
427 if (DefI != VRegDefs.end() && DefI->SU != SU)
428 DefI->SU->addPred(SDep(SU, SDep::Anti, 0, Reg));
Andrew Trickb4566a92012-02-22 06:08:11 +0000429}
Andrew Trick3c58ba82012-01-14 02:17:18 +0000430
Andrew Trickb4566a92012-02-22 06:08:11 +0000431/// Create an SUnit for each real instruction, numbered in top-down toplological
432/// order. The instruction order A < B, implies that no edge exists from B to A.
433///
434/// Map each real instruction to its SUnit.
435///
436/// After initSUnits, the SUnits vector is cannot be resized and the scheduler
437/// may hang onto SUnit pointers. We may relax this in the future by using SUnit
438/// IDs instead of pointers.
439void ScheduleDAGInstrs::initSUnits() {
440 // We'll be allocating one SUnit for each real instruction in the region,
441 // which is contained within a basic block.
442 SUnits.reserve(BB->size());
443
444 for (MachineBasicBlock::iterator I = Begin; I != InsertPos; ++I) {
445 MachineInstr *MI = I;
446 if (MI->isDebugValue())
447 continue;
448
449 SUnit *SU = NewSUnit(MI);
450 MISUnitMap[MI] = SU;
451
452 SU->isCall = MI->isCall();
453 SU->isCommutable = MI->isCommutable();
454
455 // Assign the Latency field of SU using target-provided information.
456 if (UnitLatencies)
457 SU->Latency = 1;
458 else
459 ComputeLatency(SU);
460 }
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000461}
462
Dan Gohmana70dca12009-10-09 23:27:56 +0000463void ScheduleDAGInstrs::BuildSchedGraph(AliasAnalysis *AA) {
Andrew Trickb4566a92012-02-22 06:08:11 +0000464 // Create an SUnit for each real instruction.
465 initSUnits();
Dan Gohman343f0c02008-11-19 23:18:57 +0000466
Dan Gohman6a9041e2008-12-04 01:35:46 +0000467 // We build scheduling units by walking a block's instruction list from bottom
468 // to top.
469
David Goodwin980d4942009-11-09 19:22:17 +0000470 // Remember where a generic side-effecting instruction is as we procede.
471 SUnit *BarrierChain = 0, *AliasChain = 0;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000472
David Goodwin980d4942009-11-09 19:22:17 +0000473 // Memory references to specific known memory locations are tracked
474 // so that they can be given more precise dependencies. We track
475 // separately the known memory locations that may alias and those
476 // that are known not to alias
477 std::map<const Value *, SUnit *> AliasMemDefs, NonAliasMemDefs;
478 std::map<const Value *, std::vector<SUnit *> > AliasMemUses, NonAliasMemUses;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000479
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000480 // Remove any stale debug info; sometimes BuildSchedGraph is called again
481 // without emitting the info from the previous call.
Devang Patelcf4cc842011-06-02 20:07:12 +0000482 DbgValues.clear();
483 FirstDbgValue = NULL;
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000484
Evan Chengec6906b2010-10-23 02:10:46 +0000485 // Model data dependencies between instructions being scheduled and the
486 // ExitSU.
487 AddSchedBarrierDeps();
488
Andrew Trick9b668532011-05-06 21:52:52 +0000489 for (int i = 0, e = TRI->getNumRegs(); i != e; ++i) {
490 assert(Defs[i].empty() && "Only BuildGraph should push/pop Defs");
491 }
492
Andrew Trick8ae3ac72012-02-22 21:59:00 +0000493 assert(VRegDefs.empty() && "Only BuildSchedGraph may access VRegDefs");
494 // FIXME: Allow SparseSet to reserve space for the creation of virtual
495 // registers during scheduling. Don't artificially inflate the Universe
496 // because we want to assert that vregs are not created during DAG building.
497 VRegDefs.setUniverse(MRI.getNumVirtRegs());
Andrew Trick3c58ba82012-01-14 02:17:18 +0000498
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000499 // Walk the list of instructions, from bottom moving up.
Devang Patelcf4cc842011-06-02 20:07:12 +0000500 MachineInstr *PrevMI = NULL;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000501 for (MachineBasicBlock::iterator MII = InsertPos, MIE = Begin;
Dan Gohman343f0c02008-11-19 23:18:57 +0000502 MII != MIE; --MII) {
503 MachineInstr *MI = prior(MII);
Devang Patelcf4cc842011-06-02 20:07:12 +0000504 if (MI && PrevMI) {
505 DbgValues.push_back(std::make_pair(PrevMI, MI));
506 PrevMI = NULL;
507 }
508
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000509 if (MI->isDebugValue()) {
Devang Patelcf4cc842011-06-02 20:07:12 +0000510 PrevMI = MI;
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000511 continue;
512 }
Devang Patelcf4cc842011-06-02 20:07:12 +0000513
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000514 assert(!MI->isTerminator() && !MI->isLabel() &&
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000515 "Cannot schedule terminators or labels!");
Dan Gohman343f0c02008-11-19 23:18:57 +0000516
Andrew Trickb4566a92012-02-22 06:08:11 +0000517 SUnit *SU = MISUnitMap[MI];
518 assert(SU && "No SUnit mapped to this MI");
Dan Gohman54e4c362008-12-09 22:54:47 +0000519
Dan Gohman6a9041e2008-12-04 01:35:46 +0000520 // Add register-based dependencies (data, anti, and output).
Dan Gohman343f0c02008-11-19 23:18:57 +0000521 for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) {
522 const MachineOperand &MO = MI->getOperand(j);
523 if (!MO.isReg()) continue;
524 unsigned Reg = MO.getReg();
525 if (Reg == 0) continue;
526
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000527 if (TRI->isPhysicalRegister(Reg))
528 addPhysRegDeps(SU, j);
529 else {
530 assert(!IsPostRA && "Virtual register encountered!");
Andrew Trick3c58ba82012-01-14 02:17:18 +0000531 if (MO.isDef())
532 addVRegDefDeps(SU, j);
533 else
534 addVRegUseDeps(SU, j);
Dan Gohman343f0c02008-11-19 23:18:57 +0000535 }
536 }
Dan Gohman6a9041e2008-12-04 01:35:46 +0000537
538 // Add chain dependencies.
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000539 // Chain dependencies used to enforce memory order should have
540 // latency of 0 (except for true dependency of Store followed by
541 // aliased Load... we estimate that with a single cycle of latency
542 // assuming the hardware will bypass)
Dan Gohman6a9041e2008-12-04 01:35:46 +0000543 // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable
544 // after stack slots are lowered to actual addresses.
545 // TODO: Use an AliasAnalysis and do real alias-analysis queries, and
546 // produce more precise dependence information.
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000547#define STORE_LOAD_LATENCY 1
548 unsigned TrueMemOrderLatency = 0;
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000549 if (MI->isCall() || MI->hasUnmodeledSideEffects() ||
Andrew Trickf405b1a2011-05-05 19:24:06 +0000550 (MI->hasVolatileMemoryRef() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000551 (!MI->mayLoad() || !MI->isInvariantLoad(AA)))) {
David Goodwin980d4942009-11-09 19:22:17 +0000552 // Be conservative with these and add dependencies on all memory
553 // references, even those that are known to not alias.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000554 for (std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000555 NonAliasMemDefs.begin(), E = NonAliasMemDefs.end(); I != E; ++I) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000556 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000557 }
558 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000559 NonAliasMemUses.begin(), E = NonAliasMemUses.end(); I != E; ++I) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000560 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000561 I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000562 }
David Goodwin980d4942009-11-09 19:22:17 +0000563 NonAliasMemDefs.clear();
564 NonAliasMemUses.clear();
565 // Add SU to the barrier chain.
566 if (BarrierChain)
567 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
568 BarrierChain = SU;
569
570 // fall-through
571 new_alias_chain:
572 // Chain all possibly aliasing memory references though SU.
573 if (AliasChain)
574 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
575 AliasChain = SU;
576 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
577 PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
578 for (std::map<const Value *, SUnit *>::iterator I = AliasMemDefs.begin(),
579 E = AliasMemDefs.end(); I != E; ++I) {
580 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
581 }
582 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
583 AliasMemUses.begin(), E = AliasMemUses.end(); I != E; ++I) {
584 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
585 I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
586 }
587 PendingLoads.clear();
588 AliasMemDefs.clear();
589 AliasMemUses.clear();
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000590 } else if (MI->mayStore()) {
David Goodwina9e61072009-11-03 20:15:00 +0000591 bool MayAlias = true;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000592 TrueMemOrderLatency = STORE_LOAD_LATENCY;
David Goodwina9e61072009-11-03 20:15:00 +0000593 if (const Value *V = getUnderlyingObjectForInstr(MI, MFI, MayAlias)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000594 // A store to a specific PseudoSourceValue. Add precise dependencies.
David Goodwin980d4942009-11-09 19:22:17 +0000595 // Record the def in MemDefs, first adding a dep if there is
596 // an existing def.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000597 std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000598 ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000599 std::map<const Value *, SUnit *>::iterator IE =
David Goodwin980d4942009-11-09 19:22:17 +0000600 ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end());
601 if (I != IE) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000602 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0,
Dan Gohman54e4c362008-12-09 22:54:47 +0000603 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000604 I->second = SU;
605 } else {
David Goodwin980d4942009-11-09 19:22:17 +0000606 if (MayAlias)
607 AliasMemDefs[V] = SU;
608 else
609 NonAliasMemDefs[V] = SU;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000610 }
611 // Handle the uses in MemUses, if there are any.
Dan Gohmana629b482008-12-08 17:50:35 +0000612 std::map<const Value *, std::vector<SUnit *> >::iterator J =
David Goodwin980d4942009-11-09 19:22:17 +0000613 ((MayAlias) ? AliasMemUses.find(V) : NonAliasMemUses.find(V));
614 std::map<const Value *, std::vector<SUnit *> >::iterator JE =
615 ((MayAlias) ? AliasMemUses.end() : NonAliasMemUses.end());
616 if (J != JE) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000617 for (unsigned i = 0, e = J->second.size(); i != e; ++i)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000618 J->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency,
619 /*Reg=*/0, /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000620 J->second.clear();
621 }
David Goodwina9e61072009-11-03 20:15:00 +0000622 if (MayAlias) {
David Goodwin980d4942009-11-09 19:22:17 +0000623 // Add dependencies from all the PendingLoads, i.e. loads
624 // with no underlying object.
David Goodwina9e61072009-11-03 20:15:00 +0000625 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
626 PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
David Goodwin980d4942009-11-09 19:22:17 +0000627 // Add dependence on alias chain, if needed.
628 if (AliasChain)
629 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
David Goodwina9e61072009-11-03 20:15:00 +0000630 }
David Goodwin980d4942009-11-09 19:22:17 +0000631 // Add dependence on barrier chain, if needed.
632 if (BarrierChain)
633 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
David Goodwin5be870a2009-11-05 00:16:44 +0000634 } else {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000635 // Treat all other stores conservatively.
David Goodwin980d4942009-11-09 19:22:17 +0000636 goto new_alias_chain;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000637 }
Evan Chengec6906b2010-10-23 02:10:46 +0000638
639 if (!ExitSU.isPred(SU))
640 // Push store's up a bit to avoid them getting in between cmp
641 // and branches.
642 ExitSU.addPred(SDep(SU, SDep::Order, 0,
643 /*Reg=*/0, /*isNormalMemory=*/false,
644 /*isMustAlias=*/false,
645 /*isArtificial=*/true));
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000646 } else if (MI->mayLoad()) {
David Goodwina9e61072009-11-03 20:15:00 +0000647 bool MayAlias = true;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000648 TrueMemOrderLatency = 0;
Dan Gohmana70dca12009-10-09 23:27:56 +0000649 if (MI->isInvariantLoad(AA)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000650 // Invariant load, no chain dependencies needed!
David Goodwin5be870a2009-11-05 00:16:44 +0000651 } else {
Andrew Trickf405b1a2011-05-05 19:24:06 +0000652 if (const Value *V =
David Goodwin980d4942009-11-09 19:22:17 +0000653 getUnderlyingObjectForInstr(MI, MFI, MayAlias)) {
654 // A load from a specific PseudoSourceValue. Add precise dependencies.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000655 std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000656 ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000657 std::map<const Value *, SUnit *>::iterator IE =
David Goodwin980d4942009-11-09 19:22:17 +0000658 ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end());
659 if (I != IE)
660 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0,
661 /*isNormalMemory=*/true));
662 if (MayAlias)
663 AliasMemUses[V].push_back(SU);
Andrew Trickf405b1a2011-05-05 19:24:06 +0000664 else
David Goodwin980d4942009-11-09 19:22:17 +0000665 NonAliasMemUses[V].push_back(SU);
666 } else {
667 // A load with no underlying object. Depend on all
668 // potentially aliasing stores.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000669 for (std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000670 AliasMemDefs.begin(), E = AliasMemDefs.end(); I != E; ++I)
671 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000672
David Goodwin980d4942009-11-09 19:22:17 +0000673 PendingLoads.push_back(SU);
674 MayAlias = true;
David Goodwina9e61072009-11-03 20:15:00 +0000675 }
Andrew Trickf405b1a2011-05-05 19:24:06 +0000676
David Goodwin980d4942009-11-09 19:22:17 +0000677 // Add dependencies on alias and barrier chains, if needed.
678 if (MayAlias && AliasChain)
679 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
680 if (BarrierChain)
681 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000682 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000683 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000684 }
Devang Patelcf4cc842011-06-02 20:07:12 +0000685 if (PrevMI)
686 FirstDbgValue = PrevMI;
Dan Gohman79ce2762009-01-15 19:20:50 +0000687
688 for (int i = 0, e = TRI->getNumRegs(); i != e; ++i) {
689 Defs[i].clear();
690 Uses[i].clear();
691 }
Andrew Trick3c58ba82012-01-14 02:17:18 +0000692 VRegDefs.clear();
Dan Gohman79ce2762009-01-15 19:20:50 +0000693 PendingLoads.clear();
Andrew Trickb4566a92012-02-22 06:08:11 +0000694 MISUnitMap.clear();
Dan Gohman343f0c02008-11-19 23:18:57 +0000695}
696
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000697void ScheduleDAGInstrs::FinishBlock() {
698 // Nothing to do.
699}
700
Dan Gohmanc8c28272008-11-21 00:12:10 +0000701void ScheduleDAGInstrs::ComputeLatency(SUnit *SU) {
David Goodwind94a4e52009-08-10 15:55:25 +0000702 // Compute the latency for the node.
Evan Cheng3ef1c872010-09-10 01:29:16 +0000703 if (!InstrItins || InstrItins->isEmpty()) {
704 SU->Latency = 1;
Dan Gohman4ea8e852008-12-16 02:38:22 +0000705
Evan Cheng3ef1c872010-09-10 01:29:16 +0000706 // Simplistic target-independent heuristic: assume that loads take
707 // extra time.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000708 if (SU->getInstr()->mayLoad())
Dan Gohman4ea8e852008-12-16 02:38:22 +0000709 SU->Latency += 2;
Evan Cheng8239daf2010-11-03 00:45:17 +0000710 } else {
711 SU->Latency = TII->getInstrLatency(InstrItins, SU->getInstr());
712 }
Dan Gohmanc8c28272008-11-21 00:12:10 +0000713}
714
Andrew Trickf405b1a2011-05-05 19:24:06 +0000715void ScheduleDAGInstrs::ComputeOperandLatency(SUnit *Def, SUnit *Use,
David Goodwindc4bdcd2009-08-19 16:08:58 +0000716 SDep& dep) const {
Evan Cheng3ef1c872010-09-10 01:29:16 +0000717 if (!InstrItins || InstrItins->isEmpty())
David Goodwindc4bdcd2009-08-19 16:08:58 +0000718 return;
Andrew Trickf405b1a2011-05-05 19:24:06 +0000719
David Goodwindc4bdcd2009-08-19 16:08:58 +0000720 // For a data dependency with a known register...
721 if ((dep.getKind() != SDep::Data) || (dep.getReg() == 0))
722 return;
723
724 const unsigned Reg = dep.getReg();
725
726 // ... find the definition of the register in the defining
727 // instruction
728 MachineInstr *DefMI = Def->getInstr();
729 int DefIdx = DefMI->findRegisterDefOperandIdx(Reg);
730 if (DefIdx != -1) {
Evan Cheng1aca5bc2010-10-08 18:42:25 +0000731 const MachineOperand &MO = DefMI->getOperand(DefIdx);
732 if (MO.isReg() && MO.isImplicit() &&
Evan Chengd82de832010-10-08 23:01:57 +0000733 DefIdx >= (int)DefMI->getDesc().getNumOperands()) {
Evan Cheng1aca5bc2010-10-08 18:42:25 +0000734 // This is an implicit def, getOperandLatency() won't return the correct
735 // latency. e.g.
736 // %D6<def>, %D7<def> = VLD1q16 %R2<kill>, 0, ..., %Q3<imp-def>
737 // %Q1<def> = VMULv8i16 %Q1<kill>, %Q3<kill>, ...
738 // What we want is to compute latency between def of %D6/%D7 and use of
739 // %Q3 instead.
Jakob Stoklund Olesen02634be2012-02-22 22:52:52 +0000740 unsigned Op2 = DefMI->findRegisterDefOperandIdx(Reg, false, true, TRI);
741 if (DefMI->getOperand(Op2).isReg())
742 DefIdx = Op2;
Evan Cheng1aca5bc2010-10-08 18:42:25 +0000743 }
Evan Chenga0792de2010-10-06 06:27:31 +0000744 MachineInstr *UseMI = Use->getInstr();
Evan Cheng3881cb72010-09-29 22:42:35 +0000745 // For all uses of the register, calculate the maxmimum latency
746 int Latency = -1;
Evan Chengec6906b2010-10-23 02:10:46 +0000747 if (UseMI) {
748 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
749 const MachineOperand &MO = UseMI->getOperand(i);
750 if (!MO.isReg() || !MO.isUse())
751 continue;
752 unsigned MOReg = MO.getReg();
753 if (MOReg != Reg)
754 continue;
David Goodwindc4bdcd2009-08-19 16:08:58 +0000755
Evan Chengec6906b2010-10-23 02:10:46 +0000756 int UseCycle = TII->getOperandLatency(InstrItins, DefMI, DefIdx,
757 UseMI, i);
758 Latency = std::max(Latency, UseCycle);
759 }
760 } else {
761 // UseMI is null, then it must be a scheduling barrier.
762 if (!InstrItins || InstrItins->isEmpty())
763 return;
764 unsigned DefClass = DefMI->getDesc().getSchedClass();
765 Latency = InstrItins->getOperandCycle(DefClass, DefIdx);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000766 }
Evan Chengec6906b2010-10-23 02:10:46 +0000767
768 // If we found a latency, then replace the existing dependence latency.
769 if (Latency >= 0)
770 dep.setLatency(Latency);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000771 }
772}
773
Dan Gohman343f0c02008-11-19 23:18:57 +0000774void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
775 SU->getInstr()->dump();
776}
777
778std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
779 std::string s;
780 raw_string_ostream oss(s);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000781 if (SU == &EntrySU)
782 oss << "<entry>";
783 else if (SU == &ExitSU)
784 oss << "<exit>";
785 else
786 SU->getInstr()->print(oss);
Dan Gohman343f0c02008-11-19 23:18:57 +0000787 return oss.str();
788}
789
790// EmitSchedule - Emit the machine code in scheduled order.
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000791MachineBasicBlock *ScheduleDAGInstrs::EmitSchedule() {
Evan Chengddfd1372011-12-14 02:11:42 +0000792 Begin = InsertPos;
Dan Gohman343f0c02008-11-19 23:18:57 +0000793
Devang Patelcf4cc842011-06-02 20:07:12 +0000794 // If first instruction was a DBG_VALUE then put it back.
795 if (FirstDbgValue)
Evan Chengddfd1372011-12-14 02:11:42 +0000796 BB->splice(InsertPos, BB, FirstDbgValue);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000797
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000798 // Then re-insert them according to the given schedule.
Dan Gohman343f0c02008-11-19 23:18:57 +0000799 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
Devang Patelee1f8782011-06-02 21:31:00 +0000800 if (SUnit *SU = Sequence[i])
Evan Chengddfd1372011-12-14 02:11:42 +0000801 BB->splice(InsertPos, BB, SU->getInstr());
Devang Patelee1f8782011-06-02 21:31:00 +0000802 else
Dan Gohman343f0c02008-11-19 23:18:57 +0000803 // Null SUnit* is a noop.
804 EmitNoop();
Dan Gohman343f0c02008-11-19 23:18:57 +0000805
Hal Finkeldb809e02011-12-02 04:58:07 +0000806 // Update the Begin iterator, as the first instruction in the block
807 // may have been scheduled later.
808 if (i == 0)
809 Begin = prior(InsertPos);
810 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000811
Devang Patelcf4cc842011-06-02 20:07:12 +0000812 // Reinsert any remaining debug_values.
813 for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator
814 DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
815 std::pair<MachineInstr *, MachineInstr *> P = *prior(DI);
816 MachineInstr *DbgValue = P.first;
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000817 MachineBasicBlock::iterator OrigPrivMI = P.second;
Evan Chengddfd1372011-12-14 02:11:42 +0000818 BB->splice(++OrigPrivMI, BB, DbgValue);
Devang Patelcf4cc842011-06-02 20:07:12 +0000819 }
820 DbgValues.clear();
821 FirstDbgValue = NULL;
Dan Gohman343f0c02008-11-19 23:18:57 +0000822 return BB;
823}