blob: dac29f1161d1a357134899a06c34fdcd3ef0a59a [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 Gohman3311a1f2009-01-30 02:49:14 +000017#include "llvm/Analysis/AliasAnalysis.h"
Dan Gohman3f237442008-12-16 03:25:46 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
19#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman6a9041e2008-12-04 01:35:46 +000020#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000021#include "llvm/Target/TargetMachine.h"
22#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohman3f237442008-12-16 03:25:46 +000024#include "llvm/Target/TargetSubtarget.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000025#include "llvm/Support/Debug.h"
26#include "llvm/Support/raw_ostream.h"
Dan Gohman3f237442008-12-16 03:25:46 +000027#include "llvm/ADT/SmallSet.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000028using namespace llvm;
29
Dan Gohman79ce2762009-01-15 19:20:50 +000030ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf,
Dan Gohman3f237442008-12-16 03:25:46 +000031 const MachineLoopInfo &mli,
32 const MachineDominatorTree &mdt)
Dan Gohman9e64bbb2009-02-10 23:27:53 +000033 : ScheduleDAG(mf), MLI(mli), MDT(mdt), LoopRegs(MLI, MDT) {}
Dan Gohman343f0c02008-11-19 23:18:57 +000034
Dan Gohman3311a1f2009-01-30 02:49:14 +000035/// getOpcode - If this is an Instruction or a ConstantExpr, return the
36/// opcode value. Otherwise return UserOp1.
37static unsigned getOpcode(const Value *V) {
38 if (const Instruction *I = dyn_cast<Instruction>(V))
39 return I->getOpcode();
40 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
41 return CE->getOpcode();
42 // Use UserOp1 to mean there's no opcode.
43 return Instruction::UserOp1;
44}
45
46/// getUnderlyingObjectFromInt - This is the function that does the work of
47/// looking through basic ptrtoint+arithmetic+inttoptr sequences.
48static const Value *getUnderlyingObjectFromInt(const Value *V) {
49 do {
50 if (const User *U = dyn_cast<User>(V)) {
51 // If we find a ptrtoint, we can transfer control back to the
52 // regular getUnderlyingObjectFromInt.
53 if (getOpcode(U) == Instruction::PtrToInt)
54 return U->getOperand(0);
55 // If we find an add of a constant or a multiplied value, it's
56 // likely that the other operand will lead us to the base
57 // object. We don't have to worry about the case where the
58 // object address is somehow being computed bt the multiply,
59 // because our callers only care when the result is an
60 // identifibale object.
61 if (getOpcode(U) != Instruction::Add ||
62 (!isa<ConstantInt>(U->getOperand(1)) &&
63 getOpcode(U->getOperand(1)) != Instruction::Mul))
64 return V;
65 V = U->getOperand(0);
66 } else {
67 return V;
68 }
69 assert(isa<IntegerType>(V->getType()) && "Unexpected operand type!");
70 } while (1);
71}
72
73/// getUnderlyingObject - This is a wrapper around Value::getUnderlyingObject
74/// and adds support for basic ptrtoint+arithmetic+inttoptr sequences.
75static const Value *getUnderlyingObject(const Value *V) {
76 // First just call Value::getUnderlyingObject to let it do what it does.
77 do {
78 V = V->getUnderlyingObject();
79 // If it found an inttoptr, use special code to continue climing.
80 if (getOpcode(V) != Instruction::IntToPtr)
81 break;
82 const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
83 // If that succeeded in finding a pointer, continue the search.
84 if (!isa<PointerType>(O->getType()))
85 break;
86 V = O;
87 } while (1);
88 return V;
89}
90
91/// getUnderlyingObjectForInstr - If this machine instr has memory reference
92/// information and it can be tracked to a normal reference to a known
93/// object, return the Value for that object. Otherwise return null.
94static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI) {
95 if (!MI->hasOneMemOperand() ||
96 !MI->memoperands_begin()->getValue() ||
97 MI->memoperands_begin()->isVolatile())
98 return 0;
99
100 const Value *V = MI->memoperands_begin()->getValue();
101 if (!V)
102 return 0;
103
104 V = getUnderlyingObject(V);
105 if (!isa<PseudoSourceValue>(V) && !isIdentifiedObject(V))
106 return 0;
107
108 return V;
109}
110
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000111void ScheduleDAGInstrs::StartBlock(MachineBasicBlock *BB) {
112 if (MachineLoop *ML = MLI.getLoopFor(BB))
113 if (BB == ML->getLoopLatch()) {
114 MachineBasicBlock *Header = ML->getHeader();
115 for (MachineBasicBlock::livein_iterator I = Header->livein_begin(),
116 E = Header->livein_end(); I != E; ++I)
117 LoopLiveInRegs.insert(*I);
118 LoopRegs.VisitLoop(ML);
119 }
120}
121
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000122void ScheduleDAGInstrs::BuildSchedGraph() {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000123 // We'll be allocating one SUnit for each instruction, plus one for
124 // the region exit node.
Dan Gohman343f0c02008-11-19 23:18:57 +0000125 SUnits.reserve(BB->size());
126
Dan Gohman6a9041e2008-12-04 01:35:46 +0000127 // We build scheduling units by walking a block's instruction list from bottom
128 // to top.
129
Dan Gohman6a9041e2008-12-04 01:35:46 +0000130 // Remember where a generic side-effecting instruction is as we procede. If
131 // ChainMMO is null, this is assumed to have arbitrary side-effects. If
132 // ChainMMO is non-null, then Chain makes only a single memory reference.
133 SUnit *Chain = 0;
134 MachineMemOperand *ChainMMO = 0;
135
136 // Memory references to specific known memory locations are tracked so that
137 // they can be given more precise dependencies.
138 std::map<const Value *, SUnit *> MemDefs;
139 std::map<const Value *, std::vector<SUnit *> > MemUses;
140
Dan Gohman3f237442008-12-16 03:25:46 +0000141 // Check to see if the scheduler cares about latencies.
142 bool UnitLatencies = ForceUnitLatencies();
143
Dan Gohman8749b612008-12-16 03:35:01 +0000144 // Ask the target if address-backscheduling is desirable, and if so how much.
145 unsigned SpecialAddressLatency =
146 TM.getSubtarget<TargetSubtarget>().getSpecialAddressLatency();
147
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000148 // Walk the list of instructions, from bottom moving up.
Dan Gohmanf7119392009-01-16 22:10:20 +0000149 for (MachineBasicBlock::iterator MII = End, MIE = Begin;
Dan Gohman343f0c02008-11-19 23:18:57 +0000150 MII != MIE; --MII) {
151 MachineInstr *MI = prior(MII);
Dan Gohman3f237442008-12-16 03:25:46 +0000152 const TargetInstrDesc &TID = MI->getDesc();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000153 assert(!TID.isTerminator() && !MI->isLabel() &&
154 "Cannot schedule terminators or labels!");
155 // Create the SUnit for this MI.
Dan Gohman343f0c02008-11-19 23:18:57 +0000156 SUnit *SU = NewSUnit(MI);
157
Dan Gohman54e4c362008-12-09 22:54:47 +0000158 // Assign the Latency field of SU using target-provided information.
Dan Gohman3f237442008-12-16 03:25:46 +0000159 if (UnitLatencies)
160 SU->Latency = 1;
161 else
162 ComputeLatency(SU);
Dan Gohman54e4c362008-12-09 22:54:47 +0000163
Dan Gohman6a9041e2008-12-04 01:35:46 +0000164 // Add register-based dependencies (data, anti, and output).
Dan Gohman343f0c02008-11-19 23:18:57 +0000165 for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) {
166 const MachineOperand &MO = MI->getOperand(j);
167 if (!MO.isReg()) continue;
168 unsigned Reg = MO.getReg();
169 if (Reg == 0) continue;
170
171 assert(TRI->isPhysicalRegister(Reg) && "Virtual register encountered!");
172 std::vector<SUnit *> &UseList = Uses[Reg];
Dan Gohman3f237442008-12-16 03:25:46 +0000173 std::vector<SUnit *> &DefList = Defs[Reg];
Dan Gohmanfc626b62008-11-21 19:16:58 +0000174 // Optionally add output and anti dependencies.
Dan Gohman54e4c362008-12-09 22:54:47 +0000175 // TODO: Using a latency of 1 here assumes there's no cost for
176 // reusing registers.
177 SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
Dan Gohman3f237442008-12-16 03:25:46 +0000178 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
179 SUnit *DefSU = DefList[i];
180 if (DefSU != SU &&
181 (Kind != SDep::Output || !MO.isDead() ||
182 !DefSU->getInstr()->registerDefIsDead(Reg)))
183 DefSU->addPred(SDep(SU, Kind, /*Latency=*/1, /*Reg=*/Reg));
184 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000185 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
Dan Gohman3f237442008-12-16 03:25:46 +0000186 std::vector<SUnit *> &DefList = Defs[*Alias];
187 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
188 SUnit *DefSU = DefList[i];
189 if (DefSU != SU &&
190 (Kind != SDep::Output || !MO.isDead() ||
191 !DefSU->getInstr()->registerDefIsDead(Reg)))
192 DefSU->addPred(SDep(SU, Kind, /*Latency=*/1, /*Reg=*/ *Alias));
193 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000194 }
195
196 if (MO.isDef()) {
197 // Add any data dependencies.
Dan Gohman3f237442008-12-16 03:25:46 +0000198 unsigned DataLatency = SU->Latency;
199 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
200 SUnit *UseSU = UseList[i];
201 if (UseSU != SU) {
Dan Gohman8749b612008-12-16 03:35:01 +0000202 unsigned LDataLatency = DataLatency;
203 // Optionally add in a special extra latency for nodes that
204 // feed addresses.
205 // TODO: Do this for register aliases too.
206 if (SpecialAddressLatency != 0 && !UnitLatencies) {
207 MachineInstr *UseMI = UseSU->getInstr();
208 const TargetInstrDesc &UseTID = UseMI->getDesc();
209 int RegUseIndex = UseMI->findRegisterUseOperandIdx(Reg);
210 assert(RegUseIndex >= 0 && "UseMI doesn's use register!");
211 if ((UseTID.mayLoad() || UseTID.mayStore()) &&
212 (unsigned)RegUseIndex < UseTID.getNumOperands() &&
213 UseTID.OpInfo[RegUseIndex].isLookupPtrRegClass())
214 LDataLatency += SpecialAddressLatency;
215 }
216 UseSU->addPred(SDep(SU, SDep::Data, LDataLatency, Reg));
Dan Gohman3f237442008-12-16 03:25:46 +0000217 }
218 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000219 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
220 std::vector<SUnit *> &UseList = Uses[*Alias];
Dan Gohman3f237442008-12-16 03:25:46 +0000221 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
222 SUnit *UseSU = UseList[i];
223 if (UseSU != SU)
224 UseSU->addPred(SDep(SU, SDep::Data, DataLatency, *Alias));
225 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000226 }
227
Dan Gohman8749b612008-12-16 03:35:01 +0000228 // If a def is going to wrap back around to the top of the loop,
229 // backschedule it.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000230 if (!UnitLatencies && DefList.empty()) {
Dan Gohman8749b612008-12-16 03:35:01 +0000231 LoopDependencies::LoopDeps::iterator I = LoopRegs.Deps.find(Reg);
232 if (I != LoopRegs.Deps.end()) {
233 const MachineOperand *UseMO = I->second.first;
234 unsigned Count = I->second.second;
235 const MachineInstr *UseMI = UseMO->getParent();
236 unsigned UseMOIdx = UseMO - &UseMI->getOperand(0);
237 const TargetInstrDesc &UseTID = UseMI->getDesc();
238 // TODO: If we knew the total depth of the region here, we could
239 // handle the case where the whole loop is inside the region but
240 // is large enough that the isScheduleHigh trick isn't needed.
241 if (UseMOIdx < UseTID.getNumOperands()) {
242 // Currently, we only support scheduling regions consisting of
243 // single basic blocks. Check to see if the instruction is in
244 // the same region by checking to see if it has the same parent.
245 if (UseMI->getParent() != MI->getParent()) {
246 unsigned Latency = SU->Latency;
247 if (UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass())
248 Latency += SpecialAddressLatency;
249 // This is a wild guess as to the portion of the latency which
250 // will be overlapped by work done outside the current
251 // scheduling region.
252 Latency -= std::min(Latency, Count);
253 // Add the artifical edge.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000254 ExitSU.addPred(SDep(SU, SDep::Order, Latency,
255 /*Reg=*/0, /*isNormalMemory=*/false,
256 /*isMustAlias=*/false,
257 /*isArtificial=*/true));
Dan Gohman8749b612008-12-16 03:35:01 +0000258 } else if (SpecialAddressLatency > 0 &&
259 UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass()) {
260 // The entire loop body is within the current scheduling region
261 // and the latency of this operation is assumed to be greater
262 // than the latency of the loop.
263 // TODO: Recursively mark data-edge predecessors as
264 // isScheduleHigh too.
265 SU->isScheduleHigh = true;
266 }
267 }
268 LoopRegs.Deps.erase(I);
269 }
270 }
271
Dan Gohman343f0c02008-11-19 23:18:57 +0000272 UseList.clear();
Dan Gohman3f237442008-12-16 03:25:46 +0000273 if (!MO.isDead())
274 DefList.clear();
275 DefList.push_back(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000276 } else {
277 UseList.push_back(SU);
278 }
279 }
Dan Gohman6a9041e2008-12-04 01:35:46 +0000280
281 // Add chain dependencies.
282 // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable
283 // after stack slots are lowered to actual addresses.
284 // TODO: Use an AliasAnalysis and do real alias-analysis queries, and
285 // produce more precise dependence information.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000286 if (TID.isCall() || TID.hasUnmodeledSideEffects()) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000287 new_chain:
Dan Gohmana629b482008-12-08 17:50:35 +0000288 // This is the conservative case. Add dependencies on all memory
289 // references.
Dan Gohman343f0c02008-11-19 23:18:57 +0000290 if (Chain)
Dan Gohman54e4c362008-12-09 22:54:47 +0000291 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000292 Chain = SU;
Dan Gohman343f0c02008-11-19 23:18:57 +0000293 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
Dan Gohman54e4c362008-12-09 22:54:47 +0000294 PendingLoads[k]->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman343f0c02008-11-19 23:18:57 +0000295 PendingLoads.clear();
Dan Gohman6a9041e2008-12-04 01:35:46 +0000296 for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(),
297 E = MemDefs.end(); I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000298 I->second->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000299 I->second = SU;
300 }
301 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
302 MemUses.begin(), E = MemUses.end(); I != E; ++I) {
303 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Dan Gohman54e4c362008-12-09 22:54:47 +0000304 I->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000305 I->second.clear();
306 }
307 // See if it is known to just have a single memory reference.
308 MachineInstr *ChainMI = Chain->getInstr();
309 const TargetInstrDesc &ChainTID = ChainMI->getDesc();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000310 if (!ChainTID.isCall() &&
Dan Gohman6a9041e2008-12-04 01:35:46 +0000311 !ChainTID.hasUnmodeledSideEffects() &&
312 ChainMI->hasOneMemOperand() &&
313 !ChainMI->memoperands_begin()->isVolatile() &&
314 ChainMI->memoperands_begin()->getValue())
315 // We know that the Chain accesses one specific memory location.
316 ChainMMO = &*ChainMI->memoperands_begin();
317 else
318 // Unknown memory accesses. Assume the worst.
319 ChainMMO = 0;
320 } else if (TID.mayStore()) {
Dan Gohman3311a1f2009-01-30 02:49:14 +0000321 if (const Value *V = getUnderlyingObjectForInstr(MI)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000322 // A store to a specific PseudoSourceValue. Add precise dependencies.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000323 // Handle the def in MemDefs, if there is one.
324 std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V);
325 if (I != MemDefs.end()) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000326 I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
327 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000328 I->second = SU;
329 } else {
330 MemDefs[V] = SU;
331 }
332 // Handle the uses in MemUses, if there are any.
Dan Gohmana629b482008-12-08 17:50:35 +0000333 std::map<const Value *, std::vector<SUnit *> >::iterator J =
334 MemUses.find(V);
Dan Gohman6a9041e2008-12-04 01:35:46 +0000335 if (J != MemUses.end()) {
336 for (unsigned i = 0, e = J->second.size(); i != e; ++i)
Dan Gohman54e4c362008-12-09 22:54:47 +0000337 J->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
338 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000339 J->second.clear();
340 }
Dan Gohman3311a1f2009-01-30 02:49:14 +0000341 // Add dependencies from all the PendingLoads, since without
342 // memoperands we must assume they alias anything.
343 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
344 PendingLoads[k]->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000345 // Add a general dependence too, if needed.
346 if (Chain)
Dan Gohman54e4c362008-12-09 22:54:47 +0000347 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000348 } else
349 // Treat all other stores conservatively.
350 goto new_chain;
351 } else if (TID.mayLoad()) {
352 if (TII->isInvariantLoad(MI)) {
353 // Invariant load, no chain dependencies needed!
Dan Gohman3311a1f2009-01-30 02:49:14 +0000354 } else if (const Value *V = getUnderlyingObjectForInstr(MI)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000355 // A load from a specific PseudoSourceValue. Add precise dependencies.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000356 std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V);
357 if (I != MemDefs.end())
Dan Gohman54e4c362008-12-09 22:54:47 +0000358 I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
359 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000360 MemUses[V].push_back(SU);
361
362 // Add a general dependence too, if needed.
363 if (Chain && (!ChainMMO ||
364 (ChainMMO->isStore() || ChainMMO->isVolatile())))
Dan Gohman54e4c362008-12-09 22:54:47 +0000365 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000366 } else if (MI->hasVolatileMemoryRef()) {
367 // Treat volatile loads conservatively. Note that this includes
368 // cases where memoperand information is unavailable.
369 goto new_chain;
370 } else {
Dan Gohman3311a1f2009-01-30 02:49:14 +0000371 // A normal load. Depend on the general chain, as well as on
372 // all stores. In the absense of MachineMemOperand information,
373 // we can't even assume that the load doesn't alias well-behaved
374 // memory locations.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000375 if (Chain)
Dan Gohman54e4c362008-12-09 22:54:47 +0000376 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman3311a1f2009-01-30 02:49:14 +0000377 for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(),
378 E = MemDefs.end(); I != E; ++I)
379 I->second->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000380 PendingLoads.push_back(SU);
381 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000382 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000383 }
Dan Gohman79ce2762009-01-15 19:20:50 +0000384
385 for (int i = 0, e = TRI->getNumRegs(); i != e; ++i) {
386 Defs[i].clear();
387 Uses[i].clear();
388 }
389 PendingLoads.clear();
Dan Gohman343f0c02008-11-19 23:18:57 +0000390}
391
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000392void ScheduleDAGInstrs::FinishBlock() {
393 // Nothing to do.
394}
395
Dan Gohmanc8c28272008-11-21 00:12:10 +0000396void ScheduleDAGInstrs::ComputeLatency(SUnit *SU) {
397 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
398
399 // Compute the latency for the node. We use the sum of the latencies for
400 // all nodes flagged together into this SUnit.
401 SU->Latency =
402 InstrItins.getLatency(SU->getInstr()->getDesc().getSchedClass());
Dan Gohman4ea8e852008-12-16 02:38:22 +0000403
404 // Simplistic target-independent heuristic: assume that loads take
405 // extra time.
406 if (InstrItins.isEmpty())
407 if (SU->getInstr()->getDesc().mayLoad())
408 SU->Latency += 2;
Dan Gohmanc8c28272008-11-21 00:12:10 +0000409}
410
Dan Gohman343f0c02008-11-19 23:18:57 +0000411void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
412 SU->getInstr()->dump();
413}
414
415std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
416 std::string s;
417 raw_string_ostream oss(s);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000418 if (SU == &EntrySU)
419 oss << "<entry>";
420 else if (SU == &ExitSU)
421 oss << "<exit>";
422 else
423 SU->getInstr()->print(oss);
Dan Gohman343f0c02008-11-19 23:18:57 +0000424 return oss.str();
425}
426
427// EmitSchedule - Emit the machine code in scheduled order.
428MachineBasicBlock *ScheduleDAGInstrs::EmitSchedule() {
429 // For MachineInstr-based scheduling, we're rescheduling the instructions in
430 // the block, so start by removing them from the block.
Dan Gohmanf7119392009-01-16 22:10:20 +0000431 while (Begin != End) {
432 MachineBasicBlock::iterator I = Begin;
433 ++Begin;
434 BB->remove(I);
435 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000436
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000437 // Then re-insert them according to the given schedule.
Dan Gohman343f0c02008-11-19 23:18:57 +0000438 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
439 SUnit *SU = Sequence[i];
440 if (!SU) {
441 // Null SUnit* is a noop.
442 EmitNoop();
443 continue;
444 }
445
Dan Gohmanf7119392009-01-16 22:10:20 +0000446 BB->insert(End, SU->getInstr());
Dan Gohman343f0c02008-11-19 23:18:57 +0000447 }
448
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000449 // Update the Begin iterator, as the first instruction in the block
450 // may have been scheduled later.
451 if (!Sequence.empty())
452 Begin = Sequence[0]->getInstr();
453
Dan Gohman343f0c02008-11-19 23:18:57 +0000454 return BB;
455}