blob: 29e4df670c33f1a3e1e96d74baf39d18c47e0bb7 [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 Trick81a682a2012-02-23 01:52:38 +000042 LIS(lis), UnitLatencies(false), LoopRegs(MLI, MDT), FirstDbgValue(0) {
Andrew Trickb4566a92012-02-22 06:08:11 +000043 assert((IsPostRA || LIS) && "PreRA scheduling requires LiveIntervals");
Devang Patelcf4cc842011-06-02 20:07:12 +000044 DbgValues.clear();
Andrew Trickcc77b542012-02-22 06:08:13 +000045 assert(!(IsPostRA && MRI.getNumVirtRegs()) &&
Andrew Trick19273ae2012-02-21 04:51:23 +000046 "Virtual registers must be removed prior to PostRA scheduling");
Evan Cheng38bdfc62009-10-18 19:58:47 +000047}
Dan Gohman343f0c02008-11-19 23:18:57 +000048
Dan Gohman47ac0f02009-02-11 04:27:20 +000049/// Run - perform scheduling.
50///
51void ScheduleDAGInstrs::Run(MachineBasicBlock *bb,
52 MachineBasicBlock::iterator begin,
53 MachineBasicBlock::iterator end,
54 unsigned endcount) {
55 BB = bb;
56 Begin = begin;
57 InsertPosIndex = endcount;
58
Andrew Trick7ebcaf42012-01-14 02:17:15 +000059 // Check to see if the scheduler cares about latencies.
60 UnitLatencies = ForceUnitLatencies();
61
Dan Gohman47ac0f02009-02-11 04:27:20 +000062 ScheduleDAG::Run(bb, end);
63}
64
Dan Gohman3311a1f2009-01-30 02:49:14 +000065/// getUnderlyingObjectFromInt - This is the function that does the work of
66/// looking through basic ptrtoint+arithmetic+inttoptr sequences.
67static const Value *getUnderlyingObjectFromInt(const Value *V) {
68 do {
Dan Gohman8906f952009-07-17 20:58:59 +000069 if (const Operator *U = dyn_cast<Operator>(V)) {
Dan Gohman3311a1f2009-01-30 02:49:14 +000070 // If we find a ptrtoint, we can transfer control back to the
71 // regular getUnderlyingObjectFromInt.
Dan Gohman8906f952009-07-17 20:58:59 +000072 if (U->getOpcode() == Instruction::PtrToInt)
Dan Gohman3311a1f2009-01-30 02:49:14 +000073 return U->getOperand(0);
74 // If we find an add of a constant or a multiplied value, it's
75 // likely that the other operand will lead us to the base
76 // object. We don't have to worry about the case where the
Dan Gohman748f98f2009-08-07 01:26:06 +000077 // object address is somehow being computed by the multiply,
Dan Gohman3311a1f2009-01-30 02:49:14 +000078 // because our callers only care when the result is an
79 // identifibale object.
Dan Gohman8906f952009-07-17 20:58:59 +000080 if (U->getOpcode() != Instruction::Add ||
Dan Gohman3311a1f2009-01-30 02:49:14 +000081 (!isa<ConstantInt>(U->getOperand(1)) &&
Dan Gohman8906f952009-07-17 20:58:59 +000082 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul))
Dan Gohman3311a1f2009-01-30 02:49:14 +000083 return V;
84 V = U->getOperand(0);
85 } else {
86 return V;
87 }
Duncan Sands1df98592010-02-16 11:11:14 +000088 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
Dan Gohman3311a1f2009-01-30 02:49:14 +000089 } while (1);
90}
91
Dan Gohman5034dd32010-12-15 20:02:24 +000092/// getUnderlyingObject - This is a wrapper around GetUnderlyingObject
Dan Gohman3311a1f2009-01-30 02:49:14 +000093/// and adds support for basic ptrtoint+arithmetic+inttoptr sequences.
94static const Value *getUnderlyingObject(const Value *V) {
95 // First just call Value::getUnderlyingObject to let it do what it does.
96 do {
Dan Gohman5034dd32010-12-15 20:02:24 +000097 V = GetUnderlyingObject(V);
Dan Gohman3311a1f2009-01-30 02:49:14 +000098 // If it found an inttoptr, use special code to continue climing.
Dan Gohman8906f952009-07-17 20:58:59 +000099 if (Operator::getOpcode(V) != Instruction::IntToPtr)
Dan Gohman3311a1f2009-01-30 02:49:14 +0000100 break;
101 const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
102 // If that succeeded in finding a pointer, continue the search.
Duncan Sands1df98592010-02-16 11:11:14 +0000103 if (!O->getType()->isPointerTy())
Dan Gohman3311a1f2009-01-30 02:49:14 +0000104 break;
105 V = O;
106 } while (1);
107 return V;
108}
109
110/// getUnderlyingObjectForInstr - If this machine instr has memory reference
111/// information and it can be tracked to a normal reference to a known
112/// object, return the Value for that object. Otherwise return null.
Evan Cheng38bdfc62009-10-18 19:58:47 +0000113static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI,
David Goodwina9e61072009-11-03 20:15:00 +0000114 const MachineFrameInfo *MFI,
115 bool &MayAlias) {
116 MayAlias = true;
Dan Gohman3311a1f2009-01-30 02:49:14 +0000117 if (!MI->hasOneMemOperand() ||
Dan Gohmanc76909a2009-09-25 20:36:54 +0000118 !(*MI->memoperands_begin())->getValue() ||
119 (*MI->memoperands_begin())->isVolatile())
Dan Gohman3311a1f2009-01-30 02:49:14 +0000120 return 0;
121
Dan Gohmanc76909a2009-09-25 20:36:54 +0000122 const Value *V = (*MI->memoperands_begin())->getValue();
Dan Gohman3311a1f2009-01-30 02:49:14 +0000123 if (!V)
124 return 0;
125
126 V = getUnderlyingObject(V);
Evan Chengff89dcb2009-10-18 18:16:27 +0000127 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
128 // For now, ignore PseudoSourceValues which may alias LLVM IR values
129 // because the code that uses this function has no way to cope with
130 // such aliases.
Evan Cheng38bdfc62009-10-18 19:58:47 +0000131 if (PSV->isAliased(MFI))
Evan Chengff89dcb2009-10-18 18:16:27 +0000132 return 0;
Andrew Trickf405b1a2011-05-05 19:24:06 +0000133
David Goodwin980d4942009-11-09 19:22:17 +0000134 MayAlias = PSV->mayAlias(MFI);
Evan Chengff89dcb2009-10-18 18:16:27 +0000135 return V;
136 }
Dan Gohman3311a1f2009-01-30 02:49:14 +0000137
Evan Chengff89dcb2009-10-18 18:16:27 +0000138 if (isIdentifiedObject(V))
139 return V;
140
141 return 0;
Dan Gohman3311a1f2009-01-30 02:49:14 +0000142}
143
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000144void ScheduleDAGInstrs::StartBlock(MachineBasicBlock *BB) {
Andrew Tricke8deca82011-10-07 06:33:09 +0000145 LoopRegs.Deps.clear();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000146 if (MachineLoop *ML = MLI.getLoopFor(BB))
Evan Cheng977679d2012-01-07 03:02:36 +0000147 if (BB == ML->getLoopLatch())
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000148 LoopRegs.VisitLoop(ML);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000149}
150
Evan Chengec6906b2010-10-23 02:10:46 +0000151/// AddSchedBarrierDeps - Add dependencies from instructions in the current
152/// list of instructions being scheduled to scheduling barrier by adding
153/// the exit SU to the register defs and use list. This is because we want to
154/// make sure instructions which define registers that are either used by
155/// the terminator or are live-out are properly scheduled. This is
156/// especially important when the definition latency of the return value(s)
157/// are too high to be hidden by the branch or when the liveout registers
158/// used by instructions in the fallthrough block.
159void ScheduleDAGInstrs::AddSchedBarrierDeps() {
160 MachineInstr *ExitMI = InsertPos != BB->end() ? &*InsertPos : 0;
161 ExitSU.setInstr(ExitMI);
162 bool AllDepKnown = ExitMI &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000163 (ExitMI->isCall() || ExitMI->isBarrier());
Evan Chengec6906b2010-10-23 02:10:46 +0000164 if (ExitMI && AllDepKnown) {
165 // If it's a call or a barrier, add dependencies on the defs and uses of
166 // instruction.
167 for (unsigned i = 0, e = ExitMI->getNumOperands(); i != e; ++i) {
168 const MachineOperand &MO = ExitMI->getOperand(i);
169 if (!MO.isReg() || MO.isDef()) continue;
170 unsigned Reg = MO.getReg();
171 if (Reg == 0) continue;
172
Andrew Trick3c58ba82012-01-14 02:17:18 +0000173 if (TRI->isPhysicalRegister(Reg))
Andrew Trick81a682a2012-02-23 01:52:38 +0000174 Uses[Reg].SUnits.push_back(&ExitSU);
Andrew Trick3c58ba82012-01-14 02:17:18 +0000175 else
176 assert(!IsPostRA && "Virtual register encountered after regalloc.");
Evan Chengec6906b2010-10-23 02:10:46 +0000177 }
178 } else {
179 // For others, e.g. fallthrough, conditional branch, assume the exit
Evan Chengde5fa932010-10-27 23:17:17 +0000180 // uses all the registers that are livein to the successor blocks.
181 SmallSet<unsigned, 8> Seen;
182 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
183 SE = BB->succ_end(); SI != SE; ++SI)
184 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
Andrew Trickf405b1a2011-05-05 19:24:06 +0000185 E = (*SI)->livein_end(); I != E; ++I) {
Evan Chengde5fa932010-10-27 23:17:17 +0000186 unsigned Reg = *I;
187 if (Seen.insert(Reg))
Andrew Trick81a682a2012-02-23 01:52:38 +0000188 Uses[Reg].SUnits.push_back(&ExitSU);
Evan Chengde5fa932010-10-27 23:17:17 +0000189 }
Evan Chengec6906b2010-10-23 02:10:46 +0000190 }
191}
192
Andrew Trick81a682a2012-02-23 01:52:38 +0000193/// MO is an operand of SU's instruction that defines a physical register. Add
194/// data dependencies from SU to any uses of the physical register.
195void ScheduleDAGInstrs::addPhysRegDataDeps(SUnit *SU,
196 const MachineOperand &MO) {
197 assert(MO.isDef() && "expect physreg def");
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 unsigned DataLatency = SU->Latency;
203
204 for (const unsigned *Alias = TRI->getOverlaps(MO.getReg()); *Alias; ++Alias) {
205 Reg2SUnitsMap::iterator UsesI = Uses.find(*Alias);
206 if (UsesI == Uses.end())
207 continue;
208 std::vector<SUnit*> &UseList = UsesI->SUnits;
209 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
210 SUnit *UseSU = UseList[i];
211 if (UseSU == SU)
212 continue;
213 unsigned LDataLatency = DataLatency;
214 // Optionally add in a special extra latency for nodes that
215 // feed addresses.
216 // TODO: Perhaps we should get rid of
217 // SpecialAddressLatency and just move this into
218 // adjustSchedDependency for the targets that care about it.
219 if (SpecialAddressLatency != 0 && !UnitLatencies &&
220 UseSU != &ExitSU) {
221 MachineInstr *UseMI = UseSU->getInstr();
222 const MCInstrDesc &UseMCID = UseMI->getDesc();
223 int RegUseIndex = UseMI->findRegisterUseOperandIdx(*Alias);
224 assert(RegUseIndex >= 0 && "UseMI doesn't use register!");
225 if (RegUseIndex >= 0 &&
226 (UseMI->mayLoad() || UseMI->mayStore()) &&
227 (unsigned)RegUseIndex < UseMCID.getNumOperands() &&
228 UseMCID.OpInfo[RegUseIndex].isLookupPtrRegClass())
229 LDataLatency += SpecialAddressLatency;
230 }
231 // Adjust the dependence latency using operand def/use
232 // information (if any), and then allow the target to
233 // perform its own adjustments.
234 const SDep& dep = SDep(SU, SDep::Data, LDataLatency, *Alias);
235 if (!UnitLatencies) {
236 ComputeOperandLatency(SU, UseSU, const_cast<SDep &>(dep));
237 ST.adjustSchedDependency(SU, UseSU, const_cast<SDep &>(dep));
238 }
239 UseSU->addPred(dep);
240 }
241 }
242}
243
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000244/// addPhysRegDeps - Add register dependencies (data, anti, and output) from
245/// this SUnit to following instructions in the same scheduling region that
246/// depend the physical register referenced at OperIdx.
247void ScheduleDAGInstrs::addPhysRegDeps(SUnit *SU, unsigned OperIdx) {
248 const MachineInstr *MI = SU->getInstr();
249 const MachineOperand &MO = MI->getOperand(OperIdx);
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000250
251 // Optionally add output and anti dependencies. For anti
252 // dependencies we use a latency of 0 because for a multi-issue
253 // target we want to allow the defining instruction to issue
254 // in the same cycle as the using instruction.
255 // TODO: Using a latency of 1 here for output dependencies assumes
256 // there's no cost for reusing registers.
257 SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
Andrew Trick81a682a2012-02-23 01:52:38 +0000258 for (const unsigned *Alias = TRI->getOverlaps(MO.getReg()); *Alias; ++Alias) {
259 Reg2SUnitsMap::iterator DefI = Defs.find(*Alias);
260 if (DefI == Defs.end())
261 continue;
262 std::vector<SUnit *> &DefList = DefI->SUnits;
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000263 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
264 SUnit *DefSU = DefList[i];
265 if (DefSU == &ExitSU)
266 continue;
267 if (DefSU != SU &&
268 (Kind != SDep::Output || !MO.isDead() ||
269 !DefSU->getInstr()->registerDefIsDead(*Alias))) {
270 if (Kind == SDep::Anti)
271 DefSU->addPred(SDep(SU, Kind, 0, /*Reg=*/*Alias));
272 else {
273 unsigned AOLat = TII->getOutputLatency(InstrItins, MI, OperIdx,
274 DefSU->getInstr());
275 DefSU->addPred(SDep(SU, Kind, AOLat, /*Reg=*/*Alias));
276 }
277 }
278 }
279 }
280
Andrew Trick81a682a2012-02-23 01:52:38 +0000281 if (!MO.isDef()) {
282 // Either insert a new Reg2SUnits entry with an empty SUnits list, or
283 // retrieve the existing SUnits list for this register's uses.
284 // Push this SUnit on the use list.
285 Uses[MO.getReg()].SUnits.push_back(SU);
286 }
287 else {
288 addPhysRegDataDeps(SU, MO);
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000289
Andrew Trick81a682a2012-02-23 01:52:38 +0000290 // Either insert a new Reg2SUnits entry with an empty SUnits list, or
291 // retrieve the existing SUnits list for this register's defs.
292 std::vector<SUnit *> &DefList = Defs[MO.getReg()].SUnits;
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000293
294 // If a def is going to wrap back around to the top of the loop,
295 // backschedule it.
296 if (!UnitLatencies && DefList.empty()) {
Andrew Trick81a682a2012-02-23 01:52:38 +0000297 LoopDependencies::LoopDeps::iterator I = LoopRegs.Deps.find(MO.getReg());
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000298 if (I != LoopRegs.Deps.end()) {
299 const MachineOperand *UseMO = I->second.first;
300 unsigned Count = I->second.second;
301 const MachineInstr *UseMI = UseMO->getParent();
302 unsigned UseMOIdx = UseMO - &UseMI->getOperand(0);
303 const MCInstrDesc &UseMCID = UseMI->getDesc();
Andrew Trick81a682a2012-02-23 01:52:38 +0000304 const TargetSubtargetInfo &ST =
305 TM.getSubtarget<TargetSubtargetInfo>();
306 unsigned SpecialAddressLatency = ST.getSpecialAddressLatency();
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000307 // TODO: If we knew the total depth of the region here, we could
308 // handle the case where the whole loop is inside the region but
309 // is large enough that the isScheduleHigh trick isn't needed.
310 if (UseMOIdx < UseMCID.getNumOperands()) {
311 // Currently, we only support scheduling regions consisting of
312 // single basic blocks. Check to see if the instruction is in
313 // the same region by checking to see if it has the same parent.
314 if (UseMI->getParent() != MI->getParent()) {
315 unsigned Latency = SU->Latency;
316 if (UseMCID.OpInfo[UseMOIdx].isLookupPtrRegClass())
317 Latency += SpecialAddressLatency;
318 // This is a wild guess as to the portion of the latency which
319 // will be overlapped by work done outside the current
320 // scheduling region.
321 Latency -= std::min(Latency, Count);
322 // Add the artificial edge.
323 ExitSU.addPred(SDep(SU, SDep::Order, Latency,
324 /*Reg=*/0, /*isNormalMemory=*/false,
325 /*isMustAlias=*/false,
326 /*isArtificial=*/true));
327 } else if (SpecialAddressLatency > 0 &&
328 UseMCID.OpInfo[UseMOIdx].isLookupPtrRegClass()) {
329 // The entire loop body is within the current scheduling region
330 // and the latency of this operation is assumed to be greater
331 // than the latency of the loop.
332 // TODO: Recursively mark data-edge predecessors as
333 // isScheduleHigh too.
334 SU->isScheduleHigh = true;
335 }
336 }
337 LoopRegs.Deps.erase(I);
338 }
339 }
340
Andrew Trick81a682a2012-02-23 01:52:38 +0000341 // clear this register's use list
342 Reg2SUnitsMap::iterator UsesI = Uses.find(MO.getReg());
343 if (UsesI != Uses.end())
344 UsesI->SUnits.clear();
345
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000346 if (!MO.isDead())
347 DefList.clear();
348
349 // Calls will not be reordered because of chain dependencies (see
350 // below). Since call operands are dead, calls may continue to be added
351 // to the DefList making dependence checking quadratic in the size of
352 // the block. Instead, we leave only one call at the back of the
353 // DefList.
354 if (SU->isCall) {
355 while (!DefList.empty() && DefList.back()->isCall)
356 DefList.pop_back();
357 }
Andrew Trick81a682a2012-02-23 01:52:38 +0000358 // Defs are pushed in the order they are visited and never reordered.
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000359 DefList.push_back(SU);
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000360 }
361}
362
Andrew Trick3c58ba82012-01-14 02:17:18 +0000363/// addVRegDefDeps - Add register output and data dependencies from this SUnit
364/// to instructions that occur later in the same scheduling region if they read
365/// from or write to the virtual register defined at OperIdx.
366///
367/// TODO: Hoist loop induction variable increments. This has to be
368/// reevaluated. Generally, IV scheduling should be done before coalescing.
369void ScheduleDAGInstrs::addVRegDefDeps(SUnit *SU, unsigned OperIdx) {
370 const MachineInstr *MI = SU->getInstr();
371 unsigned Reg = MI->getOperand(OperIdx).getReg();
372
Andrew Trickcc77b542012-02-22 06:08:13 +0000373 // SSA defs do not have output/anti dependencies.
Andrew Trick2fc09772012-02-22 18:34:49 +0000374 // The current operand is a def, so we have at least one.
Andrew Trickcc77b542012-02-22 06:08:13 +0000375 if (llvm::next(MRI.def_begin(Reg)) == MRI.def_end())
376 return;
377
Andrew Trick3c58ba82012-01-14 02:17:18 +0000378 // Add output dependence to the next nearest def of this vreg.
379 //
380 // Unless this definition is dead, the output dependence should be
381 // transitively redundant with antidependencies from this definition's
382 // uses. We're conservative for now until we have a way to guarantee the uses
383 // are not eliminated sometime during scheduling. The output dependence edge
384 // is also useful if output latency exceeds def-use latency.
Andrew Trick8ae3ac72012-02-22 21:59:00 +0000385 VReg2SUnitMap::iterator DefI = findVRegDef(Reg);
386 if (DefI == VRegDefs.end())
387 VRegDefs.insert(VReg2SUnit(Reg, SU));
388 else {
389 SUnit *DefSU = DefI->SU;
390 if (DefSU != SU && DefSU != &ExitSU) {
391 unsigned OutLatency = TII->getOutputLatency(InstrItins, MI, OperIdx,
392 DefSU->getInstr());
393 DefSU->addPred(SDep(SU, SDep::Output, OutLatency, Reg));
394 }
395 DefI->SU = SU;
Andrew Trick3c58ba82012-01-14 02:17:18 +0000396 }
Andrew Trick3c58ba82012-01-14 02:17:18 +0000397}
398
Andrew Trickb4566a92012-02-22 06:08:11 +0000399/// addVRegUseDeps - Add a register data dependency if the instruction that
400/// defines the virtual register used at OperIdx is mapped to an SUnit. Add a
401/// register antidependency from this SUnit to instructions that occur later in
402/// the same scheduling region if they write the virtual register.
403///
404/// TODO: Handle ExitSU "uses" properly.
Andrew Trick3c58ba82012-01-14 02:17:18 +0000405void ScheduleDAGInstrs::addVRegUseDeps(SUnit *SU, unsigned OperIdx) {
Andrew Trickb4566a92012-02-22 06:08:11 +0000406 MachineInstr *MI = SU->getInstr();
407 unsigned Reg = MI->getOperand(OperIdx).getReg();
408
409 // Lookup this operand's reaching definition.
410 assert(LIS && "vreg dependencies requires LiveIntervals");
411 SlotIndex UseIdx = LIS->getSlotIndexes()->getInstructionIndex(MI);
412 LiveInterval *LI = &LIS->getInterval(Reg);
413 VNInfo *VNI = LI->getVNInfoAt(UseIdx);
414 MachineInstr *Def = LIS->getInstructionFromIndex(VNI->def);
415 if (Def) {
416 SUnit *DefSU = getSUnit(Def);
417 if (DefSU) {
418 // The reaching Def lives within this scheduling region.
419 // Create a data dependence.
420 //
421 // TODO: Handle "special" address latencies cleanly.
422 const SDep &dep = SDep(DefSU, SDep::Data, DefSU->Latency, Reg);
423 if (!UnitLatencies) {
424 // Adjust the dependence latency using operand def/use information, then
425 // allow the target to perform its own adjustments.
426 ComputeOperandLatency(DefSU, SU, const_cast<SDep &>(dep));
427 const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>();
428 ST.adjustSchedDependency(DefSU, SU, const_cast<SDep &>(dep));
429 }
430 SU->addPred(dep);
431 }
432 }
Andrew Trick3c58ba82012-01-14 02:17:18 +0000433
434 // Add antidependence to the following def of the vreg it uses.
Andrew Trick8ae3ac72012-02-22 21:59:00 +0000435 VReg2SUnitMap::iterator DefI = findVRegDef(Reg);
436 if (DefI != VRegDefs.end() && DefI->SU != SU)
437 DefI->SU->addPred(SDep(SU, SDep::Anti, 0, Reg));
Andrew Trickb4566a92012-02-22 06:08:11 +0000438}
Andrew Trick3c58ba82012-01-14 02:17:18 +0000439
Andrew Trickb4566a92012-02-22 06:08:11 +0000440/// Create an SUnit for each real instruction, numbered in top-down toplological
441/// order. The instruction order A < B, implies that no edge exists from B to A.
442///
443/// Map each real instruction to its SUnit.
444///
445/// After initSUnits, the SUnits vector is cannot be resized and the scheduler
446/// may hang onto SUnit pointers. We may relax this in the future by using SUnit
447/// IDs instead of pointers.
448void ScheduleDAGInstrs::initSUnits() {
449 // We'll be allocating one SUnit for each real instruction in the region,
450 // which is contained within a basic block.
451 SUnits.reserve(BB->size());
452
453 for (MachineBasicBlock::iterator I = Begin; I != InsertPos; ++I) {
454 MachineInstr *MI = I;
455 if (MI->isDebugValue())
456 continue;
457
458 SUnit *SU = NewSUnit(MI);
459 MISUnitMap[MI] = SU;
460
461 SU->isCall = MI->isCall();
462 SU->isCommutable = MI->isCommutable();
463
464 // Assign the Latency field of SU using target-provided information.
465 if (UnitLatencies)
466 SU->Latency = 1;
467 else
468 ComputeLatency(SU);
469 }
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000470}
471
Dan Gohmana70dca12009-10-09 23:27:56 +0000472void ScheduleDAGInstrs::BuildSchedGraph(AliasAnalysis *AA) {
Andrew Trickb4566a92012-02-22 06:08:11 +0000473 // Create an SUnit for each real instruction.
474 initSUnits();
Dan Gohman343f0c02008-11-19 23:18:57 +0000475
Dan Gohman6a9041e2008-12-04 01:35:46 +0000476 // We build scheduling units by walking a block's instruction list from bottom
477 // to top.
478
David Goodwin980d4942009-11-09 19:22:17 +0000479 // Remember where a generic side-effecting instruction is as we procede.
480 SUnit *BarrierChain = 0, *AliasChain = 0;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000481
David Goodwin980d4942009-11-09 19:22:17 +0000482 // Memory references to specific known memory locations are tracked
483 // so that they can be given more precise dependencies. We track
484 // separately the known memory locations that may alias and those
485 // that are known not to alias
486 std::map<const Value *, SUnit *> AliasMemDefs, NonAliasMemDefs;
487 std::map<const Value *, std::vector<SUnit *> > AliasMemUses, NonAliasMemUses;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000488
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000489 // Remove any stale debug info; sometimes BuildSchedGraph is called again
490 // without emitting the info from the previous call.
Devang Patelcf4cc842011-06-02 20:07:12 +0000491 DbgValues.clear();
492 FirstDbgValue = NULL;
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000493
Andrew Trick81a682a2012-02-23 01:52:38 +0000494 assert(Defs.empty() && Uses.empty() &&
495 "Only BuildGraph should update Defs/Uses");
496 Defs.setUniverse(TRI->getNumRegs());
497 Uses.setUniverse(TRI->getNumRegs());
Andrew Trick9b668532011-05-06 21:52:52 +0000498
Andrew Trick8ae3ac72012-02-22 21:59:00 +0000499 assert(VRegDefs.empty() && "Only BuildSchedGraph may access VRegDefs");
500 // FIXME: Allow SparseSet to reserve space for the creation of virtual
501 // registers during scheduling. Don't artificially inflate the Universe
502 // because we want to assert that vregs are not created during DAG building.
503 VRegDefs.setUniverse(MRI.getNumVirtRegs());
Andrew Trick3c58ba82012-01-14 02:17:18 +0000504
Andrew Trick81a682a2012-02-23 01:52:38 +0000505 // Model data dependencies between instructions being scheduled and the
506 // ExitSU.
507 AddSchedBarrierDeps();
508
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000509 // Walk the list of instructions, from bottom moving up.
Devang Patelcf4cc842011-06-02 20:07:12 +0000510 MachineInstr *PrevMI = NULL;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000511 for (MachineBasicBlock::iterator MII = InsertPos, MIE = Begin;
Dan Gohman343f0c02008-11-19 23:18:57 +0000512 MII != MIE; --MII) {
513 MachineInstr *MI = prior(MII);
Devang Patelcf4cc842011-06-02 20:07:12 +0000514 if (MI && PrevMI) {
515 DbgValues.push_back(std::make_pair(PrevMI, MI));
516 PrevMI = NULL;
517 }
518
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000519 if (MI->isDebugValue()) {
Devang Patelcf4cc842011-06-02 20:07:12 +0000520 PrevMI = MI;
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000521 continue;
522 }
Devang Patelcf4cc842011-06-02 20:07:12 +0000523
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000524 assert(!MI->isTerminator() && !MI->isLabel() &&
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000525 "Cannot schedule terminators or labels!");
Dan Gohman343f0c02008-11-19 23:18:57 +0000526
Andrew Trickb4566a92012-02-22 06:08:11 +0000527 SUnit *SU = MISUnitMap[MI];
528 assert(SU && "No SUnit mapped to this MI");
Dan Gohman54e4c362008-12-09 22:54:47 +0000529
Dan Gohman6a9041e2008-12-04 01:35:46 +0000530 // Add register-based dependencies (data, anti, and output).
Dan Gohman343f0c02008-11-19 23:18:57 +0000531 for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) {
532 const MachineOperand &MO = MI->getOperand(j);
533 if (!MO.isReg()) continue;
534 unsigned Reg = MO.getReg();
535 if (Reg == 0) continue;
536
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000537 if (TRI->isPhysicalRegister(Reg))
538 addPhysRegDeps(SU, j);
539 else {
540 assert(!IsPostRA && "Virtual register encountered!");
Andrew Trick3c58ba82012-01-14 02:17:18 +0000541 if (MO.isDef())
542 addVRegDefDeps(SU, j);
543 else
544 addVRegUseDeps(SU, j);
Dan Gohman343f0c02008-11-19 23:18:57 +0000545 }
546 }
Dan Gohman6a9041e2008-12-04 01:35:46 +0000547
548 // Add chain dependencies.
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000549 // Chain dependencies used to enforce memory order should have
550 // latency of 0 (except for true dependency of Store followed by
551 // aliased Load... we estimate that with a single cycle of latency
552 // assuming the hardware will bypass)
Dan Gohman6a9041e2008-12-04 01:35:46 +0000553 // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable
554 // after stack slots are lowered to actual addresses.
555 // TODO: Use an AliasAnalysis and do real alias-analysis queries, and
556 // produce more precise dependence information.
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000557#define STORE_LOAD_LATENCY 1
558 unsigned TrueMemOrderLatency = 0;
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000559 if (MI->isCall() || MI->hasUnmodeledSideEffects() ||
Andrew Trickf405b1a2011-05-05 19:24:06 +0000560 (MI->hasVolatileMemoryRef() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000561 (!MI->mayLoad() || !MI->isInvariantLoad(AA)))) {
David Goodwin980d4942009-11-09 19:22:17 +0000562 // Be conservative with these and add dependencies on all memory
563 // references, even those that are known to not alias.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000564 for (std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000565 NonAliasMemDefs.begin(), E = NonAliasMemDefs.end(); I != E; ++I) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000566 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000567 }
568 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000569 NonAliasMemUses.begin(), E = NonAliasMemUses.end(); I != E; ++I) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000570 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000571 I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000572 }
David Goodwin980d4942009-11-09 19:22:17 +0000573 NonAliasMemDefs.clear();
574 NonAliasMemUses.clear();
575 // Add SU to the barrier chain.
576 if (BarrierChain)
577 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
578 BarrierChain = SU;
579
580 // fall-through
581 new_alias_chain:
582 // Chain all possibly aliasing memory references though SU.
583 if (AliasChain)
584 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
585 AliasChain = SU;
586 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
587 PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
588 for (std::map<const Value *, SUnit *>::iterator I = AliasMemDefs.begin(),
589 E = AliasMemDefs.end(); I != E; ++I) {
590 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
591 }
592 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
593 AliasMemUses.begin(), E = AliasMemUses.end(); I != E; ++I) {
594 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
595 I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
596 }
597 PendingLoads.clear();
598 AliasMemDefs.clear();
599 AliasMemUses.clear();
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000600 } else if (MI->mayStore()) {
David Goodwina9e61072009-11-03 20:15:00 +0000601 bool MayAlias = true;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000602 TrueMemOrderLatency = STORE_LOAD_LATENCY;
David Goodwina9e61072009-11-03 20:15:00 +0000603 if (const Value *V = getUnderlyingObjectForInstr(MI, MFI, MayAlias)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000604 // A store to a specific PseudoSourceValue. Add precise dependencies.
David Goodwin980d4942009-11-09 19:22:17 +0000605 // Record the def in MemDefs, first adding a dep if there is
606 // an existing def.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000607 std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000608 ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000609 std::map<const Value *, SUnit *>::iterator IE =
David Goodwin980d4942009-11-09 19:22:17 +0000610 ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end());
611 if (I != IE) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000612 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0,
Dan Gohman54e4c362008-12-09 22:54:47 +0000613 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000614 I->second = SU;
615 } else {
David Goodwin980d4942009-11-09 19:22:17 +0000616 if (MayAlias)
617 AliasMemDefs[V] = SU;
618 else
619 NonAliasMemDefs[V] = SU;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000620 }
621 // Handle the uses in MemUses, if there are any.
Dan Gohmana629b482008-12-08 17:50:35 +0000622 std::map<const Value *, std::vector<SUnit *> >::iterator J =
David Goodwin980d4942009-11-09 19:22:17 +0000623 ((MayAlias) ? AliasMemUses.find(V) : NonAliasMemUses.find(V));
624 std::map<const Value *, std::vector<SUnit *> >::iterator JE =
625 ((MayAlias) ? AliasMemUses.end() : NonAliasMemUses.end());
626 if (J != JE) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000627 for (unsigned i = 0, e = J->second.size(); i != e; ++i)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000628 J->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency,
629 /*Reg=*/0, /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000630 J->second.clear();
631 }
David Goodwina9e61072009-11-03 20:15:00 +0000632 if (MayAlias) {
David Goodwin980d4942009-11-09 19:22:17 +0000633 // Add dependencies from all the PendingLoads, i.e. loads
634 // with no underlying object.
David Goodwina9e61072009-11-03 20:15:00 +0000635 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
636 PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
David Goodwin980d4942009-11-09 19:22:17 +0000637 // Add dependence on alias chain, if needed.
638 if (AliasChain)
639 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
David Goodwina9e61072009-11-03 20:15:00 +0000640 }
David Goodwin980d4942009-11-09 19:22:17 +0000641 // Add dependence on barrier chain, if needed.
642 if (BarrierChain)
643 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
David Goodwin5be870a2009-11-05 00:16:44 +0000644 } else {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000645 // Treat all other stores conservatively.
David Goodwin980d4942009-11-09 19:22:17 +0000646 goto new_alias_chain;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000647 }
Evan Chengec6906b2010-10-23 02:10:46 +0000648
649 if (!ExitSU.isPred(SU))
650 // Push store's up a bit to avoid them getting in between cmp
651 // and branches.
652 ExitSU.addPred(SDep(SU, SDep::Order, 0,
653 /*Reg=*/0, /*isNormalMemory=*/false,
654 /*isMustAlias=*/false,
655 /*isArtificial=*/true));
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000656 } else if (MI->mayLoad()) {
David Goodwina9e61072009-11-03 20:15:00 +0000657 bool MayAlias = true;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000658 TrueMemOrderLatency = 0;
Dan Gohmana70dca12009-10-09 23:27:56 +0000659 if (MI->isInvariantLoad(AA)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000660 // Invariant load, no chain dependencies needed!
David Goodwin5be870a2009-11-05 00:16:44 +0000661 } else {
Andrew Trickf405b1a2011-05-05 19:24:06 +0000662 if (const Value *V =
David Goodwin980d4942009-11-09 19:22:17 +0000663 getUnderlyingObjectForInstr(MI, MFI, MayAlias)) {
664 // A load from a specific PseudoSourceValue. Add precise dependencies.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000665 std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000666 ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000667 std::map<const Value *, SUnit *>::iterator IE =
David Goodwin980d4942009-11-09 19:22:17 +0000668 ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end());
669 if (I != IE)
670 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0,
671 /*isNormalMemory=*/true));
672 if (MayAlias)
673 AliasMemUses[V].push_back(SU);
Andrew Trickf405b1a2011-05-05 19:24:06 +0000674 else
David Goodwin980d4942009-11-09 19:22:17 +0000675 NonAliasMemUses[V].push_back(SU);
676 } else {
677 // A load with no underlying object. Depend on all
678 // potentially aliasing stores.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000679 for (std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000680 AliasMemDefs.begin(), E = AliasMemDefs.end(); I != E; ++I)
681 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000682
David Goodwin980d4942009-11-09 19:22:17 +0000683 PendingLoads.push_back(SU);
684 MayAlias = true;
David Goodwina9e61072009-11-03 20:15:00 +0000685 }
Andrew Trickf405b1a2011-05-05 19:24:06 +0000686
David Goodwin980d4942009-11-09 19:22:17 +0000687 // Add dependencies on alias and barrier chains, if needed.
688 if (MayAlias && AliasChain)
689 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
690 if (BarrierChain)
691 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000692 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000693 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000694 }
Devang Patelcf4cc842011-06-02 20:07:12 +0000695 if (PrevMI)
696 FirstDbgValue = PrevMI;
Dan Gohman79ce2762009-01-15 19:20:50 +0000697
Andrew Trick81a682a2012-02-23 01:52:38 +0000698 Defs.clear();
699 Uses.clear();
Andrew Trick3c58ba82012-01-14 02:17:18 +0000700 VRegDefs.clear();
Dan Gohman79ce2762009-01-15 19:20:50 +0000701 PendingLoads.clear();
Andrew Trickb4566a92012-02-22 06:08:11 +0000702 MISUnitMap.clear();
Dan Gohman343f0c02008-11-19 23:18:57 +0000703}
704
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000705void ScheduleDAGInstrs::FinishBlock() {
706 // Nothing to do.
707}
708
Dan Gohmanc8c28272008-11-21 00:12:10 +0000709void ScheduleDAGInstrs::ComputeLatency(SUnit *SU) {
David Goodwind94a4e52009-08-10 15:55:25 +0000710 // Compute the latency for the node.
Evan Cheng3ef1c872010-09-10 01:29:16 +0000711 if (!InstrItins || InstrItins->isEmpty()) {
712 SU->Latency = 1;
Dan Gohman4ea8e852008-12-16 02:38:22 +0000713
Evan Cheng3ef1c872010-09-10 01:29:16 +0000714 // Simplistic target-independent heuristic: assume that loads take
715 // extra time.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000716 if (SU->getInstr()->mayLoad())
Dan Gohman4ea8e852008-12-16 02:38:22 +0000717 SU->Latency += 2;
Evan Cheng8239daf2010-11-03 00:45:17 +0000718 } else {
719 SU->Latency = TII->getInstrLatency(InstrItins, SU->getInstr());
720 }
Dan Gohmanc8c28272008-11-21 00:12:10 +0000721}
722
Andrew Trickf405b1a2011-05-05 19:24:06 +0000723void ScheduleDAGInstrs::ComputeOperandLatency(SUnit *Def, SUnit *Use,
David Goodwindc4bdcd2009-08-19 16:08:58 +0000724 SDep& dep) const {
Evan Cheng3ef1c872010-09-10 01:29:16 +0000725 if (!InstrItins || InstrItins->isEmpty())
David Goodwindc4bdcd2009-08-19 16:08:58 +0000726 return;
Andrew Trickf405b1a2011-05-05 19:24:06 +0000727
David Goodwindc4bdcd2009-08-19 16:08:58 +0000728 // For a data dependency with a known register...
729 if ((dep.getKind() != SDep::Data) || (dep.getReg() == 0))
730 return;
731
732 const unsigned Reg = dep.getReg();
733
734 // ... find the definition of the register in the defining
735 // instruction
736 MachineInstr *DefMI = Def->getInstr();
737 int DefIdx = DefMI->findRegisterDefOperandIdx(Reg);
738 if (DefIdx != -1) {
Evan Cheng1aca5bc2010-10-08 18:42:25 +0000739 const MachineOperand &MO = DefMI->getOperand(DefIdx);
740 if (MO.isReg() && MO.isImplicit() &&
Evan Chengd82de832010-10-08 23:01:57 +0000741 DefIdx >= (int)DefMI->getDesc().getNumOperands()) {
Evan Cheng1aca5bc2010-10-08 18:42:25 +0000742 // This is an implicit def, getOperandLatency() won't return the correct
743 // latency. e.g.
744 // %D6<def>, %D7<def> = VLD1q16 %R2<kill>, 0, ..., %Q3<imp-def>
745 // %Q1<def> = VMULv8i16 %Q1<kill>, %Q3<kill>, ...
746 // What we want is to compute latency between def of %D6/%D7 and use of
747 // %Q3 instead.
Jakob Stoklund Olesen02634be2012-02-22 22:52:52 +0000748 unsigned Op2 = DefMI->findRegisterDefOperandIdx(Reg, false, true, TRI);
749 if (DefMI->getOperand(Op2).isReg())
750 DefIdx = Op2;
Evan Cheng1aca5bc2010-10-08 18:42:25 +0000751 }
Evan Chenga0792de2010-10-06 06:27:31 +0000752 MachineInstr *UseMI = Use->getInstr();
Evan Cheng3881cb72010-09-29 22:42:35 +0000753 // For all uses of the register, calculate the maxmimum latency
754 int Latency = -1;
Evan Chengec6906b2010-10-23 02:10:46 +0000755 if (UseMI) {
756 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
757 const MachineOperand &MO = UseMI->getOperand(i);
758 if (!MO.isReg() || !MO.isUse())
759 continue;
760 unsigned MOReg = MO.getReg();
761 if (MOReg != Reg)
762 continue;
David Goodwindc4bdcd2009-08-19 16:08:58 +0000763
Evan Chengec6906b2010-10-23 02:10:46 +0000764 int UseCycle = TII->getOperandLatency(InstrItins, DefMI, DefIdx,
765 UseMI, i);
766 Latency = std::max(Latency, UseCycle);
767 }
768 } else {
769 // UseMI is null, then it must be a scheduling barrier.
770 if (!InstrItins || InstrItins->isEmpty())
771 return;
772 unsigned DefClass = DefMI->getDesc().getSchedClass();
773 Latency = InstrItins->getOperandCycle(DefClass, DefIdx);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000774 }
Evan Chengec6906b2010-10-23 02:10:46 +0000775
776 // If we found a latency, then replace the existing dependence latency.
777 if (Latency >= 0)
778 dep.setLatency(Latency);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000779 }
780}
781
Dan Gohman343f0c02008-11-19 23:18:57 +0000782void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
783 SU->getInstr()->dump();
784}
785
786std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
787 std::string s;
788 raw_string_ostream oss(s);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000789 if (SU == &EntrySU)
790 oss << "<entry>";
791 else if (SU == &ExitSU)
792 oss << "<exit>";
793 else
794 SU->getInstr()->print(oss);
Dan Gohman343f0c02008-11-19 23:18:57 +0000795 return oss.str();
796}
797
798// EmitSchedule - Emit the machine code in scheduled order.
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000799MachineBasicBlock *ScheduleDAGInstrs::EmitSchedule() {
Evan Chengddfd1372011-12-14 02:11:42 +0000800 Begin = InsertPos;
Dan Gohman343f0c02008-11-19 23:18:57 +0000801
Devang Patelcf4cc842011-06-02 20:07:12 +0000802 // If first instruction was a DBG_VALUE then put it back.
803 if (FirstDbgValue)
Evan Chengddfd1372011-12-14 02:11:42 +0000804 BB->splice(InsertPos, BB, FirstDbgValue);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000805
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000806 // Then re-insert them according to the given schedule.
Dan Gohman343f0c02008-11-19 23:18:57 +0000807 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
Devang Patelee1f8782011-06-02 21:31:00 +0000808 if (SUnit *SU = Sequence[i])
Evan Chengddfd1372011-12-14 02:11:42 +0000809 BB->splice(InsertPos, BB, SU->getInstr());
Devang Patelee1f8782011-06-02 21:31:00 +0000810 else
Dan Gohman343f0c02008-11-19 23:18:57 +0000811 // Null SUnit* is a noop.
812 EmitNoop();
Dan Gohman343f0c02008-11-19 23:18:57 +0000813
Hal Finkeldb809e02011-12-02 04:58:07 +0000814 // Update the Begin iterator, as the first instruction in the block
815 // may have been scheduled later.
816 if (i == 0)
817 Begin = prior(InsertPos);
818 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000819
Devang Patelcf4cc842011-06-02 20:07:12 +0000820 // Reinsert any remaining debug_values.
821 for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator
822 DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
823 std::pair<MachineInstr *, MachineInstr *> P = *prior(DI);
824 MachineInstr *DbgValue = P.first;
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000825 MachineBasicBlock::iterator OrigPrivMI = P.second;
Evan Chengddfd1372011-12-14 02:11:42 +0000826 BB->splice(++OrigPrivMI, BB, DbgValue);
Devang Patelcf4cc842011-06-02 20:07:12 +0000827 }
828 DbgValues.clear();
829 FirstDbgValue = NULL;
Dan Gohman343f0c02008-11-19 23:18:57 +0000830 return BB;
831}