blob: eee398ea16faf755c9a789a6aa3ea7f9b33fb2f7 [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/MachineDominators.h"
19#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohman8749b612008-12-16 03:35:01 +000020#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohman3f237442008-12-16 03:25:46 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman6a9041e2008-12-04 01:35:46 +000022#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000023#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetInstrInfo.h"
25#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohman3f237442008-12-16 03:25:46 +000026#include "llvm/Target/TargetSubtarget.h"
27#include "llvm/Support/Compiler.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/raw_ostream.h"
Dan Gohman3f237442008-12-16 03:25:46 +000030#include "llvm/ADT/SmallSet.h"
Dan Gohman6a9041e2008-12-04 01:35:46 +000031#include <map>
Dan Gohman343f0c02008-11-19 23:18:57 +000032using namespace llvm;
33
Dan Gohman8749b612008-12-16 03:35:01 +000034namespace {
35 class VISIBILITY_HIDDEN LoopDependencies {
36 const MachineLoopInfo &MLI;
37 const MachineDominatorTree &MDT;
38
39 public:
40 typedef std::map<unsigned, std::pair<const MachineOperand *, unsigned> >
41 LoopDeps;
42 LoopDeps Deps;
43
44 LoopDependencies(const MachineLoopInfo &mli,
45 const MachineDominatorTree &mdt) :
46 MLI(mli), MDT(mdt) {}
47
48 void VisitLoop(const MachineLoop *Loop) {
49 Deps.clear();
50 MachineBasicBlock *Header = Loop->getHeader();
51 SmallSet<unsigned, 8> LoopLiveIns;
52 for (MachineBasicBlock::livein_iterator LI = Header->livein_begin(),
53 LE = Header->livein_end(); LI != LE; ++LI)
54 LoopLiveIns.insert(*LI);
55
Dan Gohman79ce2762009-01-15 19:20:50 +000056 const MachineDomTreeNode *Node = MDT.getNode(Header);
57 const MachineBasicBlock *MBB = Node->getBlock();
58 assert(Loop->contains(MBB) &&
59 "Loop does not contain header!");
60 VisitRegion(Node, MBB, Loop, LoopLiveIns);
Dan Gohman8749b612008-12-16 03:35:01 +000061 }
62
63 private:
64 void VisitRegion(const MachineDomTreeNode *Node,
Dan Gohman79ce2762009-01-15 19:20:50 +000065 const MachineBasicBlock *MBB,
Dan Gohman8749b612008-12-16 03:35:01 +000066 const MachineLoop *Loop,
67 const SmallSet<unsigned, 8> &LoopLiveIns) {
Dan Gohman8749b612008-12-16 03:35:01 +000068 unsigned Count = 0;
69 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
70 I != E; ++I, ++Count) {
71 const MachineInstr *MI = I;
72 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
73 const MachineOperand &MO = MI->getOperand(i);
74 if (!MO.isReg() || !MO.isUse())
75 continue;
76 unsigned MOReg = MO.getReg();
77 if (LoopLiveIns.count(MOReg))
78 Deps.insert(std::make_pair(MOReg, std::make_pair(&MO, Count)));
79 }
80 }
81
82 const std::vector<MachineDomTreeNode*> &Children = Node->getChildren();
Dan Gohman79ce2762009-01-15 19:20:50 +000083 for (std::vector<MachineDomTreeNode*>::const_iterator I =
84 Children.begin(), E = Children.end(); I != E; ++I) {
85 const MachineDomTreeNode *ChildNode = *I;
86 MachineBasicBlock *ChildBlock = ChildNode->getBlock();
87 if (Loop->contains(ChildBlock))
88 VisitRegion(ChildNode, ChildBlock, Loop, LoopLiveIns);
89 }
Dan Gohman8749b612008-12-16 03:35:01 +000090 }
91 };
92}
93
Dan Gohman79ce2762009-01-15 19:20:50 +000094ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf,
Dan Gohman3f237442008-12-16 03:25:46 +000095 const MachineLoopInfo &mli,
96 const MachineDominatorTree &mdt)
Dan Gohman79ce2762009-01-15 19:20:50 +000097 : ScheduleDAG(mf), MLI(mli), MDT(mdt) {}
Dan Gohman343f0c02008-11-19 23:18:57 +000098
Dan Gohman3311a1f2009-01-30 02:49:14 +000099/// getOpcode - If this is an Instruction or a ConstantExpr, return the
100/// opcode value. Otherwise return UserOp1.
101static unsigned getOpcode(const Value *V) {
102 if (const Instruction *I = dyn_cast<Instruction>(V))
103 return I->getOpcode();
104 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
105 return CE->getOpcode();
106 // Use UserOp1 to mean there's no opcode.
107 return Instruction::UserOp1;
108}
109
110/// getUnderlyingObjectFromInt - This is the function that does the work of
111/// looking through basic ptrtoint+arithmetic+inttoptr sequences.
112static const Value *getUnderlyingObjectFromInt(const Value *V) {
113 do {
114 if (const User *U = dyn_cast<User>(V)) {
115 // If we find a ptrtoint, we can transfer control back to the
116 // regular getUnderlyingObjectFromInt.
117 if (getOpcode(U) == Instruction::PtrToInt)
118 return U->getOperand(0);
119 // If we find an add of a constant or a multiplied value, it's
120 // likely that the other operand will lead us to the base
121 // object. We don't have to worry about the case where the
122 // object address is somehow being computed bt the multiply,
123 // because our callers only care when the result is an
124 // identifibale object.
125 if (getOpcode(U) != Instruction::Add ||
126 (!isa<ConstantInt>(U->getOperand(1)) &&
127 getOpcode(U->getOperand(1)) != Instruction::Mul))
128 return V;
129 V = U->getOperand(0);
130 } else {
131 return V;
132 }
133 assert(isa<IntegerType>(V->getType()) && "Unexpected operand type!");
134 } while (1);
135}
136
137/// getUnderlyingObject - This is a wrapper around Value::getUnderlyingObject
138/// and adds support for basic ptrtoint+arithmetic+inttoptr sequences.
139static const Value *getUnderlyingObject(const Value *V) {
140 // First just call Value::getUnderlyingObject to let it do what it does.
141 do {
142 V = V->getUnderlyingObject();
143 // If it found an inttoptr, use special code to continue climing.
144 if (getOpcode(V) != Instruction::IntToPtr)
145 break;
146 const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
147 // If that succeeded in finding a pointer, continue the search.
148 if (!isa<PointerType>(O->getType()))
149 break;
150 V = O;
151 } while (1);
152 return V;
153}
154
155/// getUnderlyingObjectForInstr - If this machine instr has memory reference
156/// information and it can be tracked to a normal reference to a known
157/// object, return the Value for that object. Otherwise return null.
158static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI) {
159 if (!MI->hasOneMemOperand() ||
160 !MI->memoperands_begin()->getValue() ||
161 MI->memoperands_begin()->isVolatile())
162 return 0;
163
164 const Value *V = MI->memoperands_begin()->getValue();
165 if (!V)
166 return 0;
167
168 V = getUnderlyingObject(V);
169 if (!isa<PseudoSourceValue>(V) && !isIdentifiedObject(V))
170 return 0;
171
172 return V;
173}
174
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000175void ScheduleDAGInstrs::BuildSchedGraph() {
Dan Gohman343f0c02008-11-19 23:18:57 +0000176 SUnits.reserve(BB->size());
177
Dan Gohman6a9041e2008-12-04 01:35:46 +0000178 // We build scheduling units by walking a block's instruction list from bottom
179 // to top.
180
Dan Gohman6a9041e2008-12-04 01:35:46 +0000181 // Remember where a generic side-effecting instruction is as we procede. If
182 // ChainMMO is null, this is assumed to have arbitrary side-effects. If
183 // ChainMMO is non-null, then Chain makes only a single memory reference.
184 SUnit *Chain = 0;
185 MachineMemOperand *ChainMMO = 0;
186
187 // Memory references to specific known memory locations are tracked so that
188 // they can be given more precise dependencies.
189 std::map<const Value *, SUnit *> MemDefs;
190 std::map<const Value *, std::vector<SUnit *> > MemUses;
191
Dan Gohmanf7119392009-01-16 22:10:20 +0000192 // If we have an SUnit which is representing a terminator instruction, we
193 // can use it as a place-holder successor for inter-block dependencies.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000194 SUnit *Terminator = 0;
Dan Gohman343f0c02008-11-19 23:18:57 +0000195
Dan Gohmanf7119392009-01-16 22:10:20 +0000196 // Terminators can perform control transfers, we we need to make sure that
197 // all the work of the block is done before the terminator. Labels can
198 // mark points of interest for various types of meta-data (eg. EH data),
199 // and we need to make sure nothing is scheduled around them.
200 SUnit *SchedulingBarrier = 0;
201
Dan Gohman8749b612008-12-16 03:35:01 +0000202 LoopDependencies LoopRegs(MLI, MDT);
203
204 // Track which regs are live into a loop, to help guide back-edge-aware
205 // scheduling.
206 SmallSet<unsigned, 8> LoopLiveInRegs;
207 if (MachineLoop *ML = MLI.getLoopFor(BB))
208 if (BB == ML->getLoopLatch()) {
209 MachineBasicBlock *Header = ML->getHeader();
210 for (MachineBasicBlock::livein_iterator I = Header->livein_begin(),
211 E = Header->livein_end(); I != E; ++I)
212 LoopLiveInRegs.insert(*I);
213 LoopRegs.VisitLoop(ML);
214 }
215
Dan Gohman3f237442008-12-16 03:25:46 +0000216 // Check to see if the scheduler cares about latencies.
217 bool UnitLatencies = ForceUnitLatencies();
218
Dan Gohman8749b612008-12-16 03:35:01 +0000219 // Ask the target if address-backscheduling is desirable, and if so how much.
220 unsigned SpecialAddressLatency =
221 TM.getSubtarget<TargetSubtarget>().getSpecialAddressLatency();
222
Dan Gohmanf7119392009-01-16 22:10:20 +0000223 for (MachineBasicBlock::iterator MII = End, MIE = Begin;
Dan Gohman343f0c02008-11-19 23:18:57 +0000224 MII != MIE; --MII) {
225 MachineInstr *MI = prior(MII);
Dan Gohman3f237442008-12-16 03:25:46 +0000226 const TargetInstrDesc &TID = MI->getDesc();
Dan Gohman343f0c02008-11-19 23:18:57 +0000227 SUnit *SU = NewSUnit(MI);
228
Dan Gohman54e4c362008-12-09 22:54:47 +0000229 // Assign the Latency field of SU using target-provided information.
Dan Gohman3f237442008-12-16 03:25:46 +0000230 if (UnitLatencies)
231 SU->Latency = 1;
232 else
233 ComputeLatency(SU);
Dan Gohman54e4c362008-12-09 22:54:47 +0000234
Dan Gohman6a9041e2008-12-04 01:35:46 +0000235 // Add register-based dependencies (data, anti, and output).
Dan Gohman343f0c02008-11-19 23:18:57 +0000236 for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) {
237 const MachineOperand &MO = MI->getOperand(j);
238 if (!MO.isReg()) continue;
239 unsigned Reg = MO.getReg();
240 if (Reg == 0) continue;
241
242 assert(TRI->isPhysicalRegister(Reg) && "Virtual register encountered!");
243 std::vector<SUnit *> &UseList = Uses[Reg];
Dan Gohman3f237442008-12-16 03:25:46 +0000244 std::vector<SUnit *> &DefList = Defs[Reg];
Dan Gohmanfc626b62008-11-21 19:16:58 +0000245 // Optionally add output and anti dependencies.
Dan Gohman54e4c362008-12-09 22:54:47 +0000246 // TODO: Using a latency of 1 here assumes there's no cost for
247 // reusing registers.
248 SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
Dan Gohman3f237442008-12-16 03:25:46 +0000249 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
250 SUnit *DefSU = DefList[i];
251 if (DefSU != SU &&
252 (Kind != SDep::Output || !MO.isDead() ||
253 !DefSU->getInstr()->registerDefIsDead(Reg)))
254 DefSU->addPred(SDep(SU, Kind, /*Latency=*/1, /*Reg=*/Reg));
255 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000256 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
Dan Gohman3f237442008-12-16 03:25:46 +0000257 std::vector<SUnit *> &DefList = Defs[*Alias];
258 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
259 SUnit *DefSU = DefList[i];
260 if (DefSU != SU &&
261 (Kind != SDep::Output || !MO.isDead() ||
262 !DefSU->getInstr()->registerDefIsDead(Reg)))
263 DefSU->addPred(SDep(SU, Kind, /*Latency=*/1, /*Reg=*/ *Alias));
264 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000265 }
266
267 if (MO.isDef()) {
268 // Add any data dependencies.
Dan Gohman3f237442008-12-16 03:25:46 +0000269 unsigned DataLatency = SU->Latency;
270 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
271 SUnit *UseSU = UseList[i];
272 if (UseSU != SU) {
Dan Gohman8749b612008-12-16 03:35:01 +0000273 unsigned LDataLatency = DataLatency;
274 // Optionally add in a special extra latency for nodes that
275 // feed addresses.
276 // TODO: Do this for register aliases too.
277 if (SpecialAddressLatency != 0 && !UnitLatencies) {
278 MachineInstr *UseMI = UseSU->getInstr();
279 const TargetInstrDesc &UseTID = UseMI->getDesc();
280 int RegUseIndex = UseMI->findRegisterUseOperandIdx(Reg);
281 assert(RegUseIndex >= 0 && "UseMI doesn's use register!");
282 if ((UseTID.mayLoad() || UseTID.mayStore()) &&
283 (unsigned)RegUseIndex < UseTID.getNumOperands() &&
284 UseTID.OpInfo[RegUseIndex].isLookupPtrRegClass())
285 LDataLatency += SpecialAddressLatency;
286 }
287 UseSU->addPred(SDep(SU, SDep::Data, LDataLatency, Reg));
Dan Gohman3f237442008-12-16 03:25:46 +0000288 }
289 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000290 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
291 std::vector<SUnit *> &UseList = Uses[*Alias];
Dan Gohman3f237442008-12-16 03:25:46 +0000292 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
293 SUnit *UseSU = UseList[i];
294 if (UseSU != SU)
295 UseSU->addPred(SDep(SU, SDep::Data, DataLatency, *Alias));
296 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000297 }
298
Dan Gohman8749b612008-12-16 03:35:01 +0000299 // If a def is going to wrap back around to the top of the loop,
300 // backschedule it.
301 // TODO: Blocks in loops without terminators can benefit too.
302 if (!UnitLatencies && Terminator && DefList.empty()) {
303 LoopDependencies::LoopDeps::iterator I = LoopRegs.Deps.find(Reg);
304 if (I != LoopRegs.Deps.end()) {
305 const MachineOperand *UseMO = I->second.first;
306 unsigned Count = I->second.second;
307 const MachineInstr *UseMI = UseMO->getParent();
308 unsigned UseMOIdx = UseMO - &UseMI->getOperand(0);
309 const TargetInstrDesc &UseTID = UseMI->getDesc();
310 // TODO: If we knew the total depth of the region here, we could
311 // handle the case where the whole loop is inside the region but
312 // is large enough that the isScheduleHigh trick isn't needed.
313 if (UseMOIdx < UseTID.getNumOperands()) {
314 // Currently, we only support scheduling regions consisting of
315 // single basic blocks. Check to see if the instruction is in
316 // the same region by checking to see if it has the same parent.
317 if (UseMI->getParent() != MI->getParent()) {
318 unsigned Latency = SU->Latency;
319 if (UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass())
320 Latency += SpecialAddressLatency;
321 // This is a wild guess as to the portion of the latency which
322 // will be overlapped by work done outside the current
323 // scheduling region.
324 Latency -= std::min(Latency, Count);
325 // Add the artifical edge.
326 Terminator->addPred(SDep(SU, SDep::Order, Latency,
327 /*Reg=*/0, /*isNormalMemory=*/false,
328 /*isMustAlias=*/false,
329 /*isArtificial=*/true));
330 } else if (SpecialAddressLatency > 0 &&
331 UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass()) {
332 // The entire loop body is within the current scheduling region
333 // and the latency of this operation is assumed to be greater
334 // than the latency of the loop.
335 // TODO: Recursively mark data-edge predecessors as
336 // isScheduleHigh too.
337 SU->isScheduleHigh = true;
338 }
339 }
340 LoopRegs.Deps.erase(I);
341 }
342 }
343
Dan Gohman343f0c02008-11-19 23:18:57 +0000344 UseList.clear();
Dan Gohman3f237442008-12-16 03:25:46 +0000345 if (!MO.isDead())
346 DefList.clear();
347 DefList.push_back(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000348 } else {
349 UseList.push_back(SU);
350 }
351 }
Dan Gohman6a9041e2008-12-04 01:35:46 +0000352
353 // Add chain dependencies.
354 // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable
355 // after stack slots are lowered to actual addresses.
356 // TODO: Use an AliasAnalysis and do real alias-analysis queries, and
357 // produce more precise dependence information.
Dan Gohman237dee12008-12-23 17:28:50 +0000358 if (TID.isCall() || TID.isTerminator() || TID.hasUnmodeledSideEffects()) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000359 new_chain:
Dan Gohmana629b482008-12-08 17:50:35 +0000360 // This is the conservative case. Add dependencies on all memory
361 // references.
Dan Gohman343f0c02008-11-19 23:18:57 +0000362 if (Chain)
Dan Gohman54e4c362008-12-09 22:54:47 +0000363 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000364 Chain = SU;
Dan Gohman343f0c02008-11-19 23:18:57 +0000365 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
Dan Gohman54e4c362008-12-09 22:54:47 +0000366 PendingLoads[k]->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman343f0c02008-11-19 23:18:57 +0000367 PendingLoads.clear();
Dan Gohman6a9041e2008-12-04 01:35:46 +0000368 for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(),
369 E = MemDefs.end(); I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000370 I->second->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000371 I->second = SU;
372 }
373 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
374 MemUses.begin(), E = MemUses.end(); I != E; ++I) {
375 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Dan Gohman54e4c362008-12-09 22:54:47 +0000376 I->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000377 I->second.clear();
378 }
379 // See if it is known to just have a single memory reference.
380 MachineInstr *ChainMI = Chain->getInstr();
381 const TargetInstrDesc &ChainTID = ChainMI->getDesc();
Dan Gohman237dee12008-12-23 17:28:50 +0000382 if (!ChainTID.isCall() && !ChainTID.isTerminator() &&
Dan Gohman6a9041e2008-12-04 01:35:46 +0000383 !ChainTID.hasUnmodeledSideEffects() &&
384 ChainMI->hasOneMemOperand() &&
385 !ChainMI->memoperands_begin()->isVolatile() &&
386 ChainMI->memoperands_begin()->getValue())
387 // We know that the Chain accesses one specific memory location.
388 ChainMMO = &*ChainMI->memoperands_begin();
389 else
390 // Unknown memory accesses. Assume the worst.
391 ChainMMO = 0;
392 } else if (TID.mayStore()) {
Dan Gohman3311a1f2009-01-30 02:49:14 +0000393 if (const Value *V = getUnderlyingObjectForInstr(MI)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000394 // A store to a specific PseudoSourceValue. Add precise dependencies.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000395 // Handle the def in MemDefs, if there is one.
396 std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V);
397 if (I != MemDefs.end()) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000398 I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
399 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000400 I->second = SU;
401 } else {
402 MemDefs[V] = SU;
403 }
404 // Handle the uses in MemUses, if there are any.
Dan Gohmana629b482008-12-08 17:50:35 +0000405 std::map<const Value *, std::vector<SUnit *> >::iterator J =
406 MemUses.find(V);
Dan Gohman6a9041e2008-12-04 01:35:46 +0000407 if (J != MemUses.end()) {
408 for (unsigned i = 0, e = J->second.size(); i != e; ++i)
Dan Gohman54e4c362008-12-09 22:54:47 +0000409 J->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
410 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000411 J->second.clear();
412 }
Dan Gohman3311a1f2009-01-30 02:49:14 +0000413 // Add dependencies from all the PendingLoads, since without
414 // memoperands we must assume they alias anything.
415 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
416 PendingLoads[k]->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000417 // Add a general dependence too, if needed.
418 if (Chain)
Dan Gohman54e4c362008-12-09 22:54:47 +0000419 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000420 } else
421 // Treat all other stores conservatively.
422 goto new_chain;
423 } else if (TID.mayLoad()) {
424 if (TII->isInvariantLoad(MI)) {
425 // Invariant load, no chain dependencies needed!
Dan Gohman3311a1f2009-01-30 02:49:14 +0000426 } else if (const Value *V = getUnderlyingObjectForInstr(MI)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000427 // A load from a specific PseudoSourceValue. Add precise dependencies.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000428 std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V);
429 if (I != MemDefs.end())
Dan Gohman54e4c362008-12-09 22:54:47 +0000430 I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
431 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000432 MemUses[V].push_back(SU);
433
434 // Add a general dependence too, if needed.
435 if (Chain && (!ChainMMO ||
436 (ChainMMO->isStore() || ChainMMO->isVolatile())))
Dan Gohman54e4c362008-12-09 22:54:47 +0000437 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000438 } else if (MI->hasVolatileMemoryRef()) {
439 // Treat volatile loads conservatively. Note that this includes
440 // cases where memoperand information is unavailable.
441 goto new_chain;
442 } else {
Dan Gohman3311a1f2009-01-30 02:49:14 +0000443 // A normal load. Depend on the general chain, as well as on
444 // all stores. In the absense of MachineMemOperand information,
445 // we can't even assume that the load doesn't alias well-behaved
446 // memory locations.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000447 if (Chain)
Dan Gohman54e4c362008-12-09 22:54:47 +0000448 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman3311a1f2009-01-30 02:49:14 +0000449 for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(),
450 E = MemDefs.end(); I != E; ++I)
451 I->second->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000452 PendingLoads.push_back(SU);
453 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000454 }
Dan Gohman6a9041e2008-12-04 01:35:46 +0000455
Dan Gohmanf7119392009-01-16 22:10:20 +0000456 // Add chain edges from terminators and labels to ensure that no
457 // instructions are scheduled past them.
458 if (SchedulingBarrier && SU->Succs.empty())
459 SchedulingBarrier->addPred(SDep(SU, SDep::Order, SU->Latency));
460 // If we encounter a mid-block label, we need to go back and add
461 // dependencies on SUnits we've already processed to prevent the
462 // label from moving downward.
463 if (MI->isLabel())
464 for (SUnit *I = SU; I != &SUnits[0]; --I) {
465 SUnit *SuccSU = SU-1;
466 SuccSU->addPred(SDep(SU, SDep::Order, SU->Latency));
467 MachineInstr *SuccMI = SuccSU->getInstr();
468 if (SuccMI->getDesc().isTerminator() || SuccMI->isLabel())
469 break;
470 }
471 // If this instruction obstructs all scheduling, remember it.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000472 if (TID.isTerminator() || MI->isLabel())
Dan Gohmanf7119392009-01-16 22:10:20 +0000473 SchedulingBarrier = SU;
474 // If this instruction is a terminator, remember it.
475 if (TID.isTerminator())
Dan Gohman343f0c02008-11-19 23:18:57 +0000476 Terminator = SU;
477 }
Dan Gohman79ce2762009-01-15 19:20:50 +0000478
479 for (int i = 0, e = TRI->getNumRegs(); i != e; ++i) {
480 Defs[i].clear();
481 Uses[i].clear();
482 }
483 PendingLoads.clear();
Dan Gohman343f0c02008-11-19 23:18:57 +0000484}
485
Dan Gohmanc8c28272008-11-21 00:12:10 +0000486void ScheduleDAGInstrs::ComputeLatency(SUnit *SU) {
487 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
488
489 // Compute the latency for the node. We use the sum of the latencies for
490 // all nodes flagged together into this SUnit.
491 SU->Latency =
492 InstrItins.getLatency(SU->getInstr()->getDesc().getSchedClass());
Dan Gohman4ea8e852008-12-16 02:38:22 +0000493
494 // Simplistic target-independent heuristic: assume that loads take
495 // extra time.
496 if (InstrItins.isEmpty())
497 if (SU->getInstr()->getDesc().mayLoad())
498 SU->Latency += 2;
Dan Gohmanc8c28272008-11-21 00:12:10 +0000499}
500
Dan Gohman343f0c02008-11-19 23:18:57 +0000501void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
502 SU->getInstr()->dump();
503}
504
505std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
506 std::string s;
507 raw_string_ostream oss(s);
508 SU->getInstr()->print(oss);
509 return oss.str();
510}
511
512// EmitSchedule - Emit the machine code in scheduled order.
513MachineBasicBlock *ScheduleDAGInstrs::EmitSchedule() {
514 // For MachineInstr-based scheduling, we're rescheduling the instructions in
515 // the block, so start by removing them from the block.
Dan Gohmanf7119392009-01-16 22:10:20 +0000516 while (Begin != End) {
517 MachineBasicBlock::iterator I = Begin;
518 ++Begin;
519 BB->remove(I);
520 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000521
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000522 // Then re-insert them according to the given schedule.
Dan Gohman343f0c02008-11-19 23:18:57 +0000523 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
524 SUnit *SU = Sequence[i];
525 if (!SU) {
526 // Null SUnit* is a noop.
527 EmitNoop();
528 continue;
529 }
530
Dan Gohmanf7119392009-01-16 22:10:20 +0000531 BB->insert(End, SU->getInstr());
Dan Gohman343f0c02008-11-19 23:18:57 +0000532 }
533
534 return BB;
535}