blob: 8e18b3d17fda28e37b3a04a825690cdf6e5ffbd5 [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 Gohman47ac0f02009-02-11 04:27:20 +000035/// Run - perform scheduling.
36///
37void ScheduleDAGInstrs::Run(MachineBasicBlock *bb,
38 MachineBasicBlock::iterator begin,
39 MachineBasicBlock::iterator end,
40 unsigned endcount) {
41 BB = bb;
42 Begin = begin;
43 InsertPosIndex = endcount;
44
45 ScheduleDAG::Run(bb, end);
46}
47
Dan Gohman3311a1f2009-01-30 02:49:14 +000048/// getOpcode - If this is an Instruction or a ConstantExpr, return the
49/// opcode value. Otherwise return UserOp1.
50static unsigned getOpcode(const Value *V) {
51 if (const Instruction *I = dyn_cast<Instruction>(V))
52 return I->getOpcode();
53 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
54 return CE->getOpcode();
55 // Use UserOp1 to mean there's no opcode.
56 return Instruction::UserOp1;
57}
58
59/// getUnderlyingObjectFromInt - This is the function that does the work of
60/// looking through basic ptrtoint+arithmetic+inttoptr sequences.
61static const Value *getUnderlyingObjectFromInt(const Value *V) {
62 do {
63 if (const User *U = dyn_cast<User>(V)) {
64 // If we find a ptrtoint, we can transfer control back to the
65 // regular getUnderlyingObjectFromInt.
66 if (getOpcode(U) == Instruction::PtrToInt)
67 return U->getOperand(0);
68 // If we find an add of a constant or a multiplied value, it's
69 // likely that the other operand will lead us to the base
70 // object. We don't have to worry about the case where the
71 // object address is somehow being computed bt the multiply,
72 // because our callers only care when the result is an
73 // identifibale object.
74 if (getOpcode(U) != Instruction::Add ||
75 (!isa<ConstantInt>(U->getOperand(1)) &&
76 getOpcode(U->getOperand(1)) != Instruction::Mul))
77 return V;
78 V = U->getOperand(0);
79 } else {
80 return V;
81 }
82 assert(isa<IntegerType>(V->getType()) && "Unexpected operand type!");
83 } while (1);
84}
85
86/// getUnderlyingObject - This is a wrapper around Value::getUnderlyingObject
87/// and adds support for basic ptrtoint+arithmetic+inttoptr sequences.
88static const Value *getUnderlyingObject(const Value *V) {
89 // First just call Value::getUnderlyingObject to let it do what it does.
90 do {
91 V = V->getUnderlyingObject();
92 // If it found an inttoptr, use special code to continue climing.
93 if (getOpcode(V) != Instruction::IntToPtr)
94 break;
95 const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
96 // If that succeeded in finding a pointer, continue the search.
97 if (!isa<PointerType>(O->getType()))
98 break;
99 V = O;
100 } while (1);
101 return V;
102}
103
104/// getUnderlyingObjectForInstr - If this machine instr has memory reference
105/// information and it can be tracked to a normal reference to a known
106/// object, return the Value for that object. Otherwise return null.
107static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI) {
108 if (!MI->hasOneMemOperand() ||
109 !MI->memoperands_begin()->getValue() ||
110 MI->memoperands_begin()->isVolatile())
111 return 0;
112
113 const Value *V = MI->memoperands_begin()->getValue();
114 if (!V)
115 return 0;
116
117 V = getUnderlyingObject(V);
118 if (!isa<PseudoSourceValue>(V) && !isIdentifiedObject(V))
119 return 0;
120
121 return V;
122}
123
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000124void ScheduleDAGInstrs::StartBlock(MachineBasicBlock *BB) {
125 if (MachineLoop *ML = MLI.getLoopFor(BB))
126 if (BB == ML->getLoopLatch()) {
127 MachineBasicBlock *Header = ML->getHeader();
128 for (MachineBasicBlock::livein_iterator I = Header->livein_begin(),
129 E = Header->livein_end(); I != E; ++I)
130 LoopLiveInRegs.insert(*I);
131 LoopRegs.VisitLoop(ML);
132 }
133}
134
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000135void ScheduleDAGInstrs::BuildSchedGraph() {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000136 // We'll be allocating one SUnit for each instruction, plus one for
137 // the region exit node.
Dan Gohman343f0c02008-11-19 23:18:57 +0000138 SUnits.reserve(BB->size());
139
Dan Gohman6a9041e2008-12-04 01:35:46 +0000140 // We build scheduling units by walking a block's instruction list from bottom
141 // to top.
142
Dan Gohman6a9041e2008-12-04 01:35:46 +0000143 // Remember where a generic side-effecting instruction is as we procede. If
144 // ChainMMO is null, this is assumed to have arbitrary side-effects. If
145 // ChainMMO is non-null, then Chain makes only a single memory reference.
146 SUnit *Chain = 0;
147 MachineMemOperand *ChainMMO = 0;
148
149 // Memory references to specific known memory locations are tracked so that
150 // they can be given more precise dependencies.
151 std::map<const Value *, SUnit *> MemDefs;
152 std::map<const Value *, std::vector<SUnit *> > MemUses;
153
Dan Gohman3f237442008-12-16 03:25:46 +0000154 // Check to see if the scheduler cares about latencies.
155 bool UnitLatencies = ForceUnitLatencies();
156
Dan Gohman8749b612008-12-16 03:35:01 +0000157 // Ask the target if address-backscheduling is desirable, and if so how much.
158 unsigned SpecialAddressLatency =
159 TM.getSubtarget<TargetSubtarget>().getSpecialAddressLatency();
160
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000161 // Walk the list of instructions, from bottom moving up.
Dan Gohman47ac0f02009-02-11 04:27:20 +0000162 for (MachineBasicBlock::iterator MII = InsertPos, MIE = Begin;
Dan Gohman343f0c02008-11-19 23:18:57 +0000163 MII != MIE; --MII) {
164 MachineInstr *MI = prior(MII);
Dan Gohman3f237442008-12-16 03:25:46 +0000165 const TargetInstrDesc &TID = MI->getDesc();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000166 assert(!TID.isTerminator() && !MI->isLabel() &&
167 "Cannot schedule terminators or labels!");
168 // Create the SUnit for this MI.
Dan Gohman343f0c02008-11-19 23:18:57 +0000169 SUnit *SU = NewSUnit(MI);
170
Dan Gohman54e4c362008-12-09 22:54:47 +0000171 // Assign the Latency field of SU using target-provided information.
Dan Gohman3f237442008-12-16 03:25:46 +0000172 if (UnitLatencies)
173 SU->Latency = 1;
174 else
175 ComputeLatency(SU);
Dan Gohman54e4c362008-12-09 22:54:47 +0000176
Dan Gohman6a9041e2008-12-04 01:35:46 +0000177 // Add register-based dependencies (data, anti, and output).
Dan Gohman343f0c02008-11-19 23:18:57 +0000178 for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) {
179 const MachineOperand &MO = MI->getOperand(j);
180 if (!MO.isReg()) continue;
181 unsigned Reg = MO.getReg();
182 if (Reg == 0) continue;
183
184 assert(TRI->isPhysicalRegister(Reg) && "Virtual register encountered!");
185 std::vector<SUnit *> &UseList = Uses[Reg];
Dan Gohman3f237442008-12-16 03:25:46 +0000186 std::vector<SUnit *> &DefList = Defs[Reg];
Dan Gohmanfc626b62008-11-21 19:16:58 +0000187 // Optionally add output and anti dependencies.
Dan Gohman54e4c362008-12-09 22:54:47 +0000188 // TODO: Using a latency of 1 here assumes there's no cost for
189 // reusing registers.
190 SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
Dan Gohman3f237442008-12-16 03:25:46 +0000191 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
192 SUnit *DefSU = DefList[i];
193 if (DefSU != SU &&
194 (Kind != SDep::Output || !MO.isDead() ||
195 !DefSU->getInstr()->registerDefIsDead(Reg)))
196 DefSU->addPred(SDep(SU, Kind, /*Latency=*/1, /*Reg=*/Reg));
197 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000198 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
Dan Gohman3f237442008-12-16 03:25:46 +0000199 std::vector<SUnit *> &DefList = Defs[*Alias];
200 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
201 SUnit *DefSU = DefList[i];
202 if (DefSU != SU &&
203 (Kind != SDep::Output || !MO.isDead() ||
204 !DefSU->getInstr()->registerDefIsDead(Reg)))
205 DefSU->addPred(SDep(SU, Kind, /*Latency=*/1, /*Reg=*/ *Alias));
206 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000207 }
208
209 if (MO.isDef()) {
210 // Add any data dependencies.
Dan Gohman3f237442008-12-16 03:25:46 +0000211 unsigned DataLatency = SU->Latency;
212 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
213 SUnit *UseSU = UseList[i];
214 if (UseSU != SU) {
Dan Gohman8749b612008-12-16 03:35:01 +0000215 unsigned LDataLatency = DataLatency;
216 // Optionally add in a special extra latency for nodes that
217 // feed addresses.
218 // TODO: Do this for register aliases too.
219 if (SpecialAddressLatency != 0 && !UnitLatencies) {
220 MachineInstr *UseMI = UseSU->getInstr();
221 const TargetInstrDesc &UseTID = UseMI->getDesc();
222 int RegUseIndex = UseMI->findRegisterUseOperandIdx(Reg);
223 assert(RegUseIndex >= 0 && "UseMI doesn's use register!");
224 if ((UseTID.mayLoad() || UseTID.mayStore()) &&
225 (unsigned)RegUseIndex < UseTID.getNumOperands() &&
226 UseTID.OpInfo[RegUseIndex].isLookupPtrRegClass())
227 LDataLatency += SpecialAddressLatency;
228 }
229 UseSU->addPred(SDep(SU, SDep::Data, LDataLatency, Reg));
Dan Gohman3f237442008-12-16 03:25:46 +0000230 }
231 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000232 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
233 std::vector<SUnit *> &UseList = Uses[*Alias];
Dan Gohman3f237442008-12-16 03:25:46 +0000234 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
235 SUnit *UseSU = UseList[i];
236 if (UseSU != SU)
237 UseSU->addPred(SDep(SU, SDep::Data, DataLatency, *Alias));
238 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000239 }
240
Dan Gohman8749b612008-12-16 03:35:01 +0000241 // If a def is going to wrap back around to the top of the loop,
242 // backschedule it.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000243 if (!UnitLatencies && DefList.empty()) {
Dan Gohman8749b612008-12-16 03:35:01 +0000244 LoopDependencies::LoopDeps::iterator I = LoopRegs.Deps.find(Reg);
245 if (I != LoopRegs.Deps.end()) {
246 const MachineOperand *UseMO = I->second.first;
247 unsigned Count = I->second.second;
248 const MachineInstr *UseMI = UseMO->getParent();
249 unsigned UseMOIdx = UseMO - &UseMI->getOperand(0);
250 const TargetInstrDesc &UseTID = UseMI->getDesc();
251 // TODO: If we knew the total depth of the region here, we could
252 // handle the case where the whole loop is inside the region but
253 // is large enough that the isScheduleHigh trick isn't needed.
254 if (UseMOIdx < UseTID.getNumOperands()) {
255 // Currently, we only support scheduling regions consisting of
256 // single basic blocks. Check to see if the instruction is in
257 // the same region by checking to see if it has the same parent.
258 if (UseMI->getParent() != MI->getParent()) {
259 unsigned Latency = SU->Latency;
260 if (UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass())
261 Latency += SpecialAddressLatency;
262 // This is a wild guess as to the portion of the latency which
263 // will be overlapped by work done outside the current
264 // scheduling region.
265 Latency -= std::min(Latency, Count);
266 // Add the artifical edge.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000267 ExitSU.addPred(SDep(SU, SDep::Order, Latency,
268 /*Reg=*/0, /*isNormalMemory=*/false,
269 /*isMustAlias=*/false,
270 /*isArtificial=*/true));
Dan Gohman8749b612008-12-16 03:35:01 +0000271 } else if (SpecialAddressLatency > 0 &&
272 UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass()) {
273 // The entire loop body is within the current scheduling region
274 // and the latency of this operation is assumed to be greater
275 // than the latency of the loop.
276 // TODO: Recursively mark data-edge predecessors as
277 // isScheduleHigh too.
278 SU->isScheduleHigh = true;
279 }
280 }
281 LoopRegs.Deps.erase(I);
282 }
283 }
284
Dan Gohman343f0c02008-11-19 23:18:57 +0000285 UseList.clear();
Dan Gohman3f237442008-12-16 03:25:46 +0000286 if (!MO.isDead())
287 DefList.clear();
288 DefList.push_back(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000289 } else {
290 UseList.push_back(SU);
291 }
292 }
Dan Gohman6a9041e2008-12-04 01:35:46 +0000293
294 // Add chain dependencies.
295 // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable
296 // after stack slots are lowered to actual addresses.
297 // TODO: Use an AliasAnalysis and do real alias-analysis queries, and
298 // produce more precise dependence information.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000299 if (TID.isCall() || TID.hasUnmodeledSideEffects()) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000300 new_chain:
Dan Gohmana629b482008-12-08 17:50:35 +0000301 // This is the conservative case. Add dependencies on all memory
302 // references.
Dan Gohman343f0c02008-11-19 23:18:57 +0000303 if (Chain)
Dan Gohman54e4c362008-12-09 22:54:47 +0000304 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000305 Chain = SU;
Dan Gohman343f0c02008-11-19 23:18:57 +0000306 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
Dan Gohman54e4c362008-12-09 22:54:47 +0000307 PendingLoads[k]->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman343f0c02008-11-19 23:18:57 +0000308 PendingLoads.clear();
Dan Gohman6a9041e2008-12-04 01:35:46 +0000309 for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(),
310 E = MemDefs.end(); I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000311 I->second->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000312 I->second = SU;
313 }
314 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
315 MemUses.begin(), E = MemUses.end(); I != E; ++I) {
316 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Dan Gohman54e4c362008-12-09 22:54:47 +0000317 I->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000318 I->second.clear();
319 }
320 // See if it is known to just have a single memory reference.
321 MachineInstr *ChainMI = Chain->getInstr();
322 const TargetInstrDesc &ChainTID = ChainMI->getDesc();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000323 if (!ChainTID.isCall() &&
Dan Gohman6a9041e2008-12-04 01:35:46 +0000324 !ChainTID.hasUnmodeledSideEffects() &&
325 ChainMI->hasOneMemOperand() &&
326 !ChainMI->memoperands_begin()->isVolatile() &&
327 ChainMI->memoperands_begin()->getValue())
328 // We know that the Chain accesses one specific memory location.
329 ChainMMO = &*ChainMI->memoperands_begin();
330 else
331 // Unknown memory accesses. Assume the worst.
332 ChainMMO = 0;
333 } else if (TID.mayStore()) {
Dan Gohman3311a1f2009-01-30 02:49:14 +0000334 if (const Value *V = getUnderlyingObjectForInstr(MI)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000335 // A store to a specific PseudoSourceValue. Add precise dependencies.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000336 // Handle the def in MemDefs, if there is one.
337 std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V);
338 if (I != MemDefs.end()) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000339 I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
340 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000341 I->second = SU;
342 } else {
343 MemDefs[V] = SU;
344 }
345 // Handle the uses in MemUses, if there are any.
Dan Gohmana629b482008-12-08 17:50:35 +0000346 std::map<const Value *, std::vector<SUnit *> >::iterator J =
347 MemUses.find(V);
Dan Gohman6a9041e2008-12-04 01:35:46 +0000348 if (J != MemUses.end()) {
349 for (unsigned i = 0, e = J->second.size(); i != e; ++i)
Dan Gohman54e4c362008-12-09 22:54:47 +0000350 J->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
351 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000352 J->second.clear();
353 }
Dan Gohman3311a1f2009-01-30 02:49:14 +0000354 // Add dependencies from all the PendingLoads, since without
355 // memoperands we must assume they alias anything.
356 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
357 PendingLoads[k]->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000358 // Add a general dependence too, if needed.
359 if (Chain)
Dan Gohman54e4c362008-12-09 22:54:47 +0000360 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000361 } else
362 // Treat all other stores conservatively.
363 goto new_chain;
364 } else if (TID.mayLoad()) {
365 if (TII->isInvariantLoad(MI)) {
366 // Invariant load, no chain dependencies needed!
Dan Gohman3311a1f2009-01-30 02:49:14 +0000367 } else if (const Value *V = getUnderlyingObjectForInstr(MI)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000368 // A load from a specific PseudoSourceValue. Add precise dependencies.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000369 std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V);
370 if (I != MemDefs.end())
Dan Gohman54e4c362008-12-09 22:54:47 +0000371 I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
372 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000373 MemUses[V].push_back(SU);
374
375 // Add a general dependence too, if needed.
376 if (Chain && (!ChainMMO ||
377 (ChainMMO->isStore() || ChainMMO->isVolatile())))
Dan Gohman54e4c362008-12-09 22:54:47 +0000378 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000379 } else if (MI->hasVolatileMemoryRef()) {
380 // Treat volatile loads conservatively. Note that this includes
381 // cases where memoperand information is unavailable.
382 goto new_chain;
383 } else {
Dan Gohman3311a1f2009-01-30 02:49:14 +0000384 // A normal load. Depend on the general chain, as well as on
385 // all stores. In the absense of MachineMemOperand information,
386 // we can't even assume that the load doesn't alias well-behaved
387 // memory locations.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000388 if (Chain)
Dan Gohman54e4c362008-12-09 22:54:47 +0000389 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman3311a1f2009-01-30 02:49:14 +0000390 for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(),
391 E = MemDefs.end(); I != E; ++I)
392 I->second->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000393 PendingLoads.push_back(SU);
394 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000395 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000396 }
Dan Gohman79ce2762009-01-15 19:20:50 +0000397
398 for (int i = 0, e = TRI->getNumRegs(); i != e; ++i) {
399 Defs[i].clear();
400 Uses[i].clear();
401 }
402 PendingLoads.clear();
Dan Gohman343f0c02008-11-19 23:18:57 +0000403}
404
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000405void ScheduleDAGInstrs::FinishBlock() {
406 // Nothing to do.
407}
408
Dan Gohmanc8c28272008-11-21 00:12:10 +0000409void ScheduleDAGInstrs::ComputeLatency(SUnit *SU) {
410 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
411
412 // Compute the latency for the node. We use the sum of the latencies for
413 // all nodes flagged together into this SUnit.
414 SU->Latency =
415 InstrItins.getLatency(SU->getInstr()->getDesc().getSchedClass());
Dan Gohman4ea8e852008-12-16 02:38:22 +0000416
417 // Simplistic target-independent heuristic: assume that loads take
418 // extra time.
419 if (InstrItins.isEmpty())
420 if (SU->getInstr()->getDesc().mayLoad())
421 SU->Latency += 2;
Dan Gohmanc8c28272008-11-21 00:12:10 +0000422}
423
Dan Gohman343f0c02008-11-19 23:18:57 +0000424void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
425 SU->getInstr()->dump();
426}
427
428std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
429 std::string s;
430 raw_string_ostream oss(s);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000431 if (SU == &EntrySU)
432 oss << "<entry>";
433 else if (SU == &ExitSU)
434 oss << "<exit>";
435 else
436 SU->getInstr()->print(oss);
Dan Gohman343f0c02008-11-19 23:18:57 +0000437 return oss.str();
438}
439
440// EmitSchedule - Emit the machine code in scheduled order.
441MachineBasicBlock *ScheduleDAGInstrs::EmitSchedule() {
442 // For MachineInstr-based scheduling, we're rescheduling the instructions in
443 // the block, so start by removing them from the block.
Dan Gohman47ac0f02009-02-11 04:27:20 +0000444 while (Begin != InsertPos) {
Dan Gohmanf7119392009-01-16 22:10:20 +0000445 MachineBasicBlock::iterator I = Begin;
446 ++Begin;
447 BB->remove(I);
448 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000449
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000450 // Then re-insert them according to the given schedule.
Dan Gohman343f0c02008-11-19 23:18:57 +0000451 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
452 SUnit *SU = Sequence[i];
453 if (!SU) {
454 // Null SUnit* is a noop.
455 EmitNoop();
456 continue;
457 }
458
Dan Gohman47ac0f02009-02-11 04:27:20 +0000459 BB->insert(InsertPos, SU->getInstr());
Dan Gohman343f0c02008-11-19 23:18:57 +0000460 }
461
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000462 // Update the Begin iterator, as the first instruction in the block
463 // may have been scheduled later.
464 if (!Sequence.empty())
465 Begin = Sequence[0]->getInstr();
466
Dan Gohman343f0c02008-11-19 23:18:57 +0000467 return BB;
468}