blob: 8bcbf21fa44adae179a6840113bf37c9ef982662 [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 Gohman16a2c922009-07-13 22:02:44 +000017#include "llvm/Constants.h"
Dan Gohman3311a1f2009-01-30 02:49:14 +000018#include "llvm/Analysis/AliasAnalysis.h"
Dan Gohman3f237442008-12-16 03:25:46 +000019#include "llvm/CodeGen/MachineFunctionPass.h"
20#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman6a9041e2008-12-04 01:35:46 +000021#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000022#include "llvm/Target/TargetMachine.h"
23#include "llvm/Target/TargetInstrInfo.h"
24#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohman3f237442008-12-16 03:25:46 +000025#include "llvm/Target/TargetSubtarget.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000026#include "llvm/Support/Debug.h"
27#include "llvm/Support/raw_ostream.h"
Dan Gohman3f237442008-12-16 03:25:46 +000028#include "llvm/ADT/SmallSet.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000029using namespace llvm;
30
Dan Gohman79ce2762009-01-15 19:20:50 +000031ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf,
Dan Gohman3f237442008-12-16 03:25:46 +000032 const MachineLoopInfo &mli,
33 const MachineDominatorTree &mdt)
Dan Gohman9e64bbb2009-02-10 23:27:53 +000034 : ScheduleDAG(mf), MLI(mli), MDT(mdt), LoopRegs(MLI, MDT) {}
Dan Gohman343f0c02008-11-19 23:18:57 +000035
Dan Gohman47ac0f02009-02-11 04:27:20 +000036/// Run - perform scheduling.
37///
38void ScheduleDAGInstrs::Run(MachineBasicBlock *bb,
39 MachineBasicBlock::iterator begin,
40 MachineBasicBlock::iterator end,
41 unsigned endcount) {
42 BB = bb;
43 Begin = begin;
44 InsertPosIndex = endcount;
45
46 ScheduleDAG::Run(bb, end);
47}
48
Dan Gohman3311a1f2009-01-30 02:49:14 +000049/// getOpcode - If this is an Instruction or a ConstantExpr, return the
50/// opcode value. Otherwise return UserOp1.
51static unsigned getOpcode(const Value *V) {
52 if (const Instruction *I = dyn_cast<Instruction>(V))
53 return I->getOpcode();
54 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
55 return CE->getOpcode();
56 // Use UserOp1 to mean there's no opcode.
57 return Instruction::UserOp1;
58}
59
60/// getUnderlyingObjectFromInt - This is the function that does the work of
61/// looking through basic ptrtoint+arithmetic+inttoptr sequences.
62static const Value *getUnderlyingObjectFromInt(const Value *V) {
63 do {
64 if (const User *U = dyn_cast<User>(V)) {
65 // If we find a ptrtoint, we can transfer control back to the
66 // regular getUnderlyingObjectFromInt.
67 if (getOpcode(U) == Instruction::PtrToInt)
68 return U->getOperand(0);
69 // If we find an add of a constant or a multiplied value, it's
70 // likely that the other operand will lead us to the base
71 // object. We don't have to worry about the case where the
72 // object address is somehow being computed bt the multiply,
73 // because our callers only care when the result is an
74 // identifibale object.
75 if (getOpcode(U) != Instruction::Add ||
76 (!isa<ConstantInt>(U->getOperand(1)) &&
77 getOpcode(U->getOperand(1)) != Instruction::Mul))
78 return V;
79 V = U->getOperand(0);
80 } else {
81 return V;
82 }
83 assert(isa<IntegerType>(V->getType()) && "Unexpected operand type!");
84 } while (1);
85}
86
87/// getUnderlyingObject - This is a wrapper around Value::getUnderlyingObject
88/// and adds support for basic ptrtoint+arithmetic+inttoptr sequences.
89static const Value *getUnderlyingObject(const Value *V) {
90 // First just call Value::getUnderlyingObject to let it do what it does.
91 do {
92 V = V->getUnderlyingObject();
93 // If it found an inttoptr, use special code to continue climing.
94 if (getOpcode(V) != Instruction::IntToPtr)
95 break;
96 const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
97 // If that succeeded in finding a pointer, continue the search.
98 if (!isa<PointerType>(O->getType()))
99 break;
100 V = O;
101 } while (1);
102 return V;
103}
104
105/// getUnderlyingObjectForInstr - If this machine instr has memory reference
106/// information and it can be tracked to a normal reference to a known
107/// object, return the Value for that object. Otherwise return null.
108static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI) {
109 if (!MI->hasOneMemOperand() ||
110 !MI->memoperands_begin()->getValue() ||
111 MI->memoperands_begin()->isVolatile())
112 return 0;
113
114 const Value *V = MI->memoperands_begin()->getValue();
115 if (!V)
116 return 0;
117
118 V = getUnderlyingObject(V);
119 if (!isa<PseudoSourceValue>(V) && !isIdentifiedObject(V))
120 return 0;
121
122 return V;
123}
124
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000125void ScheduleDAGInstrs::StartBlock(MachineBasicBlock *BB) {
126 if (MachineLoop *ML = MLI.getLoopFor(BB))
127 if (BB == ML->getLoopLatch()) {
128 MachineBasicBlock *Header = ML->getHeader();
129 for (MachineBasicBlock::livein_iterator I = Header->livein_begin(),
130 E = Header->livein_end(); I != E; ++I)
131 LoopLiveInRegs.insert(*I);
132 LoopRegs.VisitLoop(ML);
133 }
134}
135
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000136void ScheduleDAGInstrs::BuildSchedGraph() {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000137 // We'll be allocating one SUnit for each instruction, plus one for
138 // the region exit node.
Dan Gohman343f0c02008-11-19 23:18:57 +0000139 SUnits.reserve(BB->size());
140
Dan Gohman6a9041e2008-12-04 01:35:46 +0000141 // We build scheduling units by walking a block's instruction list from bottom
142 // to top.
143
Dan Gohman6a9041e2008-12-04 01:35:46 +0000144 // Remember where a generic side-effecting instruction is as we procede. If
145 // ChainMMO is null, this is assumed to have arbitrary side-effects. If
146 // ChainMMO is non-null, then Chain makes only a single memory reference.
147 SUnit *Chain = 0;
148 MachineMemOperand *ChainMMO = 0;
149
150 // Memory references to specific known memory locations are tracked so that
151 // they can be given more precise dependencies.
152 std::map<const Value *, SUnit *> MemDefs;
153 std::map<const Value *, std::vector<SUnit *> > MemUses;
154
Dan Gohman3f237442008-12-16 03:25:46 +0000155 // Check to see if the scheduler cares about latencies.
156 bool UnitLatencies = ForceUnitLatencies();
157
Dan Gohman8749b612008-12-16 03:35:01 +0000158 // Ask the target if address-backscheduling is desirable, and if so how much.
159 unsigned SpecialAddressLatency =
160 TM.getSubtarget<TargetSubtarget>().getSpecialAddressLatency();
161
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000162 // Walk the list of instructions, from bottom moving up.
Dan Gohman47ac0f02009-02-11 04:27:20 +0000163 for (MachineBasicBlock::iterator MII = InsertPos, MIE = Begin;
Dan Gohman343f0c02008-11-19 23:18:57 +0000164 MII != MIE; --MII) {
165 MachineInstr *MI = prior(MII);
Dan Gohman3f237442008-12-16 03:25:46 +0000166 const TargetInstrDesc &TID = MI->getDesc();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000167 assert(!TID.isTerminator() && !MI->isLabel() &&
168 "Cannot schedule terminators or labels!");
169 // Create the SUnit for this MI.
Dan Gohman343f0c02008-11-19 23:18:57 +0000170 SUnit *SU = NewSUnit(MI);
171
Dan Gohman54e4c362008-12-09 22:54:47 +0000172 // Assign the Latency field of SU using target-provided information.
Dan Gohman3f237442008-12-16 03:25:46 +0000173 if (UnitLatencies)
174 SU->Latency = 1;
175 else
176 ComputeLatency(SU);
Dan Gohman54e4c362008-12-09 22:54:47 +0000177
Dan Gohman6a9041e2008-12-04 01:35:46 +0000178 // Add register-based dependencies (data, anti, and output).
Dan Gohman343f0c02008-11-19 23:18:57 +0000179 for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) {
180 const MachineOperand &MO = MI->getOperand(j);
181 if (!MO.isReg()) continue;
182 unsigned Reg = MO.getReg();
183 if (Reg == 0) continue;
184
185 assert(TRI->isPhysicalRegister(Reg) && "Virtual register encountered!");
186 std::vector<SUnit *> &UseList = Uses[Reg];
Dan Gohman3f237442008-12-16 03:25:46 +0000187 std::vector<SUnit *> &DefList = Defs[Reg];
Dan Gohmanfc626b62008-11-21 19:16:58 +0000188 // Optionally add output and anti dependencies.
Dan Gohman54e4c362008-12-09 22:54:47 +0000189 // TODO: Using a latency of 1 here assumes there's no cost for
190 // reusing registers.
191 SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
Dan Gohman3f237442008-12-16 03:25:46 +0000192 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
193 SUnit *DefSU = DefList[i];
194 if (DefSU != SU &&
195 (Kind != SDep::Output || !MO.isDead() ||
196 !DefSU->getInstr()->registerDefIsDead(Reg)))
197 DefSU->addPred(SDep(SU, Kind, /*Latency=*/1, /*Reg=*/Reg));
198 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000199 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
Dan Gohman3f237442008-12-16 03:25:46 +0000200 std::vector<SUnit *> &DefList = Defs[*Alias];
201 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
202 SUnit *DefSU = DefList[i];
203 if (DefSU != SU &&
204 (Kind != SDep::Output || !MO.isDead() ||
205 !DefSU->getInstr()->registerDefIsDead(Reg)))
206 DefSU->addPred(SDep(SU, Kind, /*Latency=*/1, /*Reg=*/ *Alias));
207 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000208 }
209
210 if (MO.isDef()) {
211 // Add any data dependencies.
Dan Gohman3f237442008-12-16 03:25:46 +0000212 unsigned DataLatency = SU->Latency;
213 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
214 SUnit *UseSU = UseList[i];
215 if (UseSU != SU) {
Dan Gohman8749b612008-12-16 03:35:01 +0000216 unsigned LDataLatency = DataLatency;
217 // Optionally add in a special extra latency for nodes that
218 // feed addresses.
219 // TODO: Do this for register aliases too.
220 if (SpecialAddressLatency != 0 && !UnitLatencies) {
221 MachineInstr *UseMI = UseSU->getInstr();
222 const TargetInstrDesc &UseTID = UseMI->getDesc();
223 int RegUseIndex = UseMI->findRegisterUseOperandIdx(Reg);
224 assert(RegUseIndex >= 0 && "UseMI doesn's use register!");
225 if ((UseTID.mayLoad() || UseTID.mayStore()) &&
226 (unsigned)RegUseIndex < UseTID.getNumOperands() &&
227 UseTID.OpInfo[RegUseIndex].isLookupPtrRegClass())
228 LDataLatency += SpecialAddressLatency;
229 }
230 UseSU->addPred(SDep(SU, SDep::Data, LDataLatency, Reg));
Dan Gohman3f237442008-12-16 03:25:46 +0000231 }
232 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000233 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
234 std::vector<SUnit *> &UseList = Uses[*Alias];
Dan Gohman3f237442008-12-16 03:25:46 +0000235 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
236 SUnit *UseSU = UseList[i];
237 if (UseSU != SU)
238 UseSU->addPred(SDep(SU, SDep::Data, DataLatency, *Alias));
239 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000240 }
241
Dan Gohman8749b612008-12-16 03:35:01 +0000242 // If a def is going to wrap back around to the top of the loop,
243 // backschedule it.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000244 if (!UnitLatencies && DefList.empty()) {
Dan Gohman8749b612008-12-16 03:35:01 +0000245 LoopDependencies::LoopDeps::iterator I = LoopRegs.Deps.find(Reg);
246 if (I != LoopRegs.Deps.end()) {
247 const MachineOperand *UseMO = I->second.first;
248 unsigned Count = I->second.second;
249 const MachineInstr *UseMI = UseMO->getParent();
250 unsigned UseMOIdx = UseMO - &UseMI->getOperand(0);
251 const TargetInstrDesc &UseTID = UseMI->getDesc();
252 // TODO: If we knew the total depth of the region here, we could
253 // handle the case where the whole loop is inside the region but
254 // is large enough that the isScheduleHigh trick isn't needed.
255 if (UseMOIdx < UseTID.getNumOperands()) {
256 // Currently, we only support scheduling regions consisting of
257 // single basic blocks. Check to see if the instruction is in
258 // the same region by checking to see if it has the same parent.
259 if (UseMI->getParent() != MI->getParent()) {
260 unsigned Latency = SU->Latency;
261 if (UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass())
262 Latency += SpecialAddressLatency;
263 // This is a wild guess as to the portion of the latency which
264 // will be overlapped by work done outside the current
265 // scheduling region.
266 Latency -= std::min(Latency, Count);
267 // Add the artifical edge.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000268 ExitSU.addPred(SDep(SU, SDep::Order, Latency,
269 /*Reg=*/0, /*isNormalMemory=*/false,
270 /*isMustAlias=*/false,
271 /*isArtificial=*/true));
Dan Gohman8749b612008-12-16 03:35:01 +0000272 } else if (SpecialAddressLatency > 0 &&
273 UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass()) {
274 // The entire loop body is within the current scheduling region
275 // and the latency of this operation is assumed to be greater
276 // than the latency of the loop.
277 // TODO: Recursively mark data-edge predecessors as
278 // isScheduleHigh too.
279 SU->isScheduleHigh = true;
280 }
281 }
282 LoopRegs.Deps.erase(I);
283 }
284 }
285
Dan Gohman343f0c02008-11-19 23:18:57 +0000286 UseList.clear();
Dan Gohman3f237442008-12-16 03:25:46 +0000287 if (!MO.isDead())
288 DefList.clear();
289 DefList.push_back(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000290 } else {
291 UseList.push_back(SU);
292 }
293 }
Dan Gohman6a9041e2008-12-04 01:35:46 +0000294
295 // Add chain dependencies.
296 // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable
297 // after stack slots are lowered to actual addresses.
298 // TODO: Use an AliasAnalysis and do real alias-analysis queries, and
299 // produce more precise dependence information.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000300 if (TID.isCall() || TID.hasUnmodeledSideEffects()) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000301 new_chain:
Dan Gohmana629b482008-12-08 17:50:35 +0000302 // This is the conservative case. Add dependencies on all memory
303 // references.
Dan Gohman343f0c02008-11-19 23:18:57 +0000304 if (Chain)
Dan Gohman54e4c362008-12-09 22:54:47 +0000305 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000306 Chain = SU;
Dan Gohman343f0c02008-11-19 23:18:57 +0000307 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
Dan Gohman54e4c362008-12-09 22:54:47 +0000308 PendingLoads[k]->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman343f0c02008-11-19 23:18:57 +0000309 PendingLoads.clear();
Dan Gohman6a9041e2008-12-04 01:35:46 +0000310 for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(),
311 E = MemDefs.end(); I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000312 I->second->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000313 I->second = SU;
314 }
315 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
316 MemUses.begin(), E = MemUses.end(); I != E; ++I) {
317 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Dan Gohman54e4c362008-12-09 22:54:47 +0000318 I->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000319 I->second.clear();
320 }
321 // See if it is known to just have a single memory reference.
322 MachineInstr *ChainMI = Chain->getInstr();
323 const TargetInstrDesc &ChainTID = ChainMI->getDesc();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000324 if (!ChainTID.isCall() &&
Dan Gohman6a9041e2008-12-04 01:35:46 +0000325 !ChainTID.hasUnmodeledSideEffects() &&
326 ChainMI->hasOneMemOperand() &&
327 !ChainMI->memoperands_begin()->isVolatile() &&
328 ChainMI->memoperands_begin()->getValue())
329 // We know that the Chain accesses one specific memory location.
330 ChainMMO = &*ChainMI->memoperands_begin();
331 else
332 // Unknown memory accesses. Assume the worst.
333 ChainMMO = 0;
334 } else if (TID.mayStore()) {
Dan Gohman3311a1f2009-01-30 02:49:14 +0000335 if (const Value *V = getUnderlyingObjectForInstr(MI)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000336 // A store to a specific PseudoSourceValue. Add precise dependencies.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000337 // Handle the def in MemDefs, if there is one.
338 std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V);
339 if (I != MemDefs.end()) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000340 I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
341 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000342 I->second = SU;
343 } else {
344 MemDefs[V] = SU;
345 }
346 // Handle the uses in MemUses, if there are any.
Dan Gohmana629b482008-12-08 17:50:35 +0000347 std::map<const Value *, std::vector<SUnit *> >::iterator J =
348 MemUses.find(V);
Dan Gohman6a9041e2008-12-04 01:35:46 +0000349 if (J != MemUses.end()) {
350 for (unsigned i = 0, e = J->second.size(); i != e; ++i)
Dan Gohman54e4c362008-12-09 22:54:47 +0000351 J->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
352 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000353 J->second.clear();
354 }
Dan Gohman3311a1f2009-01-30 02:49:14 +0000355 // Add dependencies from all the PendingLoads, since without
356 // memoperands we must assume they alias anything.
357 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
358 PendingLoads[k]->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000359 // Add a general dependence too, if needed.
360 if (Chain)
Dan Gohman54e4c362008-12-09 22:54:47 +0000361 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000362 } else
363 // Treat all other stores conservatively.
364 goto new_chain;
365 } else if (TID.mayLoad()) {
366 if (TII->isInvariantLoad(MI)) {
367 // Invariant load, no chain dependencies needed!
Dan Gohman3311a1f2009-01-30 02:49:14 +0000368 } else if (const Value *V = getUnderlyingObjectForInstr(MI)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000369 // A load from a specific PseudoSourceValue. Add precise dependencies.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000370 std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V);
371 if (I != MemDefs.end())
Dan Gohman54e4c362008-12-09 22:54:47 +0000372 I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
373 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000374 MemUses[V].push_back(SU);
375
376 // Add a general dependence too, if needed.
377 if (Chain && (!ChainMMO ||
378 (ChainMMO->isStore() || ChainMMO->isVolatile())))
Dan Gohman54e4c362008-12-09 22:54:47 +0000379 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000380 } else if (MI->hasVolatileMemoryRef()) {
381 // Treat volatile loads conservatively. Note that this includes
382 // cases where memoperand information is unavailable.
383 goto new_chain;
384 } else {
Dan Gohman3311a1f2009-01-30 02:49:14 +0000385 // A normal load. Depend on the general chain, as well as on
386 // all stores. In the absense of MachineMemOperand information,
387 // we can't even assume that the load doesn't alias well-behaved
388 // memory locations.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000389 if (Chain)
Dan Gohman54e4c362008-12-09 22:54:47 +0000390 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman3311a1f2009-01-30 02:49:14 +0000391 for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(),
392 E = MemDefs.end(); I != E; ++I)
393 I->second->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000394 PendingLoads.push_back(SU);
395 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000396 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000397 }
Dan Gohman79ce2762009-01-15 19:20:50 +0000398
399 for (int i = 0, e = TRI->getNumRegs(); i != e; ++i) {
400 Defs[i].clear();
401 Uses[i].clear();
402 }
403 PendingLoads.clear();
Dan Gohman343f0c02008-11-19 23:18:57 +0000404}
405
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000406void ScheduleDAGInstrs::FinishBlock() {
407 // Nothing to do.
408}
409
Dan Gohmanc8c28272008-11-21 00:12:10 +0000410void ScheduleDAGInstrs::ComputeLatency(SUnit *SU) {
411 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
412
413 // Compute the latency for the node. We use the sum of the latencies for
414 // all nodes flagged together into this SUnit.
415 SU->Latency =
416 InstrItins.getLatency(SU->getInstr()->getDesc().getSchedClass());
Dan Gohman4ea8e852008-12-16 02:38:22 +0000417
418 // Simplistic target-independent heuristic: assume that loads take
419 // extra time.
420 if (InstrItins.isEmpty())
421 if (SU->getInstr()->getDesc().mayLoad())
422 SU->Latency += 2;
Dan Gohmanc8c28272008-11-21 00:12:10 +0000423}
424
Dan Gohman343f0c02008-11-19 23:18:57 +0000425void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
426 SU->getInstr()->dump();
427}
428
429std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
430 std::string s;
431 raw_string_ostream oss(s);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000432 if (SU == &EntrySU)
433 oss << "<entry>";
434 else if (SU == &ExitSU)
435 oss << "<exit>";
436 else
437 SU->getInstr()->print(oss);
Dan Gohman343f0c02008-11-19 23:18:57 +0000438 return oss.str();
439}
440
441// EmitSchedule - Emit the machine code in scheduled order.
442MachineBasicBlock *ScheduleDAGInstrs::EmitSchedule() {
443 // For MachineInstr-based scheduling, we're rescheduling the instructions in
444 // the block, so start by removing them from the block.
Dan Gohman47ac0f02009-02-11 04:27:20 +0000445 while (Begin != InsertPos) {
Dan Gohmanf7119392009-01-16 22:10:20 +0000446 MachineBasicBlock::iterator I = Begin;
447 ++Begin;
448 BB->remove(I);
449 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000450
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000451 // Then re-insert them according to the given schedule.
Dan Gohman343f0c02008-11-19 23:18:57 +0000452 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
453 SUnit *SU = Sequence[i];
454 if (!SU) {
455 // Null SUnit* is a noop.
456 EmitNoop();
457 continue;
458 }
459
Dan Gohman47ac0f02009-02-11 04:27:20 +0000460 BB->insert(InsertPos, SU->getInstr());
Dan Gohman343f0c02008-11-19 23:18:57 +0000461 }
462
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000463 // Update the Begin iterator, as the first instruction in the block
464 // may have been scheduled later.
465 if (!Sequence.empty())
466 Begin = Sequence[0]->getInstr();
467
Dan Gohman343f0c02008-11-19 23:18:57 +0000468 return BB;
469}