blob: 768ca1631a83b3ea2cdf86f39f2fd23e3cccdec7 [file] [log] [blame]
Dan Gohmana629b482008-12-08 17:50:35 +00001//===---- ScheduleDAGInstrs.cpp - MachineInstr Rescheduling ---------------===//
Dan Gohman343f0c02008-11-19 23:18:57 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Dan Gohmana629b482008-12-08 17:50:35 +000010// This implements the ScheduleDAGInstrs class, which implements re-scheduling
11// of MachineInstrs.
Dan Gohman343f0c02008-11-19 23:18:57 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "sched-instrs"
Dan Gohman6dc75fe2009-02-06 17:12:10 +000016#include "ScheduleDAGInstrs.h"
Dan Gohman8906f952009-07-17 20:58:59 +000017#include "llvm/Operator.h"
Dan Gohman3311a1f2009-01-30 02:49:14 +000018#include "llvm/Analysis/AliasAnalysis.h"
Dan Gohman3f237442008-12-16 03:25:46 +000019#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohmanc76909a2009-09-25 20:36:54 +000020#include "llvm/CodeGen/MachineMemOperand.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"
Dan Gohman343f0c02008-11-19 23:18:57 +000027#include "llvm/Support/Debug.h"
28#include "llvm/Support/raw_ostream.h"
Dan Gohman3f237442008-12-16 03:25:46 +000029#include "llvm/ADT/SmallSet.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000030using namespace llvm;
31
Dan Gohman79ce2762009-01-15 19:20:50 +000032ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf,
Dan Gohman3f237442008-12-16 03:25:46 +000033 const MachineLoopInfo &mli,
34 const MachineDominatorTree &mdt)
Dan Gohman9e64bbb2009-02-10 23:27:53 +000035 : ScheduleDAG(mf), MLI(mli), MDT(mdt), LoopRegs(MLI, MDT) {}
Dan Gohman343f0c02008-11-19 23:18:57 +000036
Dan Gohman47ac0f02009-02-11 04:27:20 +000037/// Run - perform scheduling.
38///
39void ScheduleDAGInstrs::Run(MachineBasicBlock *bb,
40 MachineBasicBlock::iterator begin,
41 MachineBasicBlock::iterator end,
42 unsigned endcount) {
43 BB = bb;
44 Begin = begin;
45 InsertPosIndex = endcount;
46
47 ScheduleDAG::Run(bb, end);
48}
49
Dan Gohman3311a1f2009-01-30 02:49:14 +000050/// getUnderlyingObjectFromInt - This is the function that does the work of
51/// looking through basic ptrtoint+arithmetic+inttoptr sequences.
52static const Value *getUnderlyingObjectFromInt(const Value *V) {
53 do {
Dan Gohman8906f952009-07-17 20:58:59 +000054 if (const Operator *U = dyn_cast<Operator>(V)) {
Dan Gohman3311a1f2009-01-30 02:49:14 +000055 // If we find a ptrtoint, we can transfer control back to the
56 // regular getUnderlyingObjectFromInt.
Dan Gohman8906f952009-07-17 20:58:59 +000057 if (U->getOpcode() == Instruction::PtrToInt)
Dan Gohman3311a1f2009-01-30 02:49:14 +000058 return U->getOperand(0);
59 // If we find an add of a constant or a multiplied value, it's
60 // likely that the other operand will lead us to the base
61 // object. We don't have to worry about the case where the
Dan Gohman748f98f2009-08-07 01:26:06 +000062 // object address is somehow being computed by the multiply,
Dan Gohman3311a1f2009-01-30 02:49:14 +000063 // because our callers only care when the result is an
64 // identifibale object.
Dan Gohman8906f952009-07-17 20:58:59 +000065 if (U->getOpcode() != Instruction::Add ||
Dan Gohman3311a1f2009-01-30 02:49:14 +000066 (!isa<ConstantInt>(U->getOperand(1)) &&
Dan Gohman8906f952009-07-17 20:58:59 +000067 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul))
Dan Gohman3311a1f2009-01-30 02:49:14 +000068 return V;
69 V = U->getOperand(0);
70 } else {
71 return V;
72 }
73 assert(isa<IntegerType>(V->getType()) && "Unexpected operand type!");
74 } while (1);
75}
76
77/// getUnderlyingObject - This is a wrapper around Value::getUnderlyingObject
78/// and adds support for basic ptrtoint+arithmetic+inttoptr sequences.
79static const Value *getUnderlyingObject(const Value *V) {
80 // First just call Value::getUnderlyingObject to let it do what it does.
81 do {
82 V = V->getUnderlyingObject();
83 // If it found an inttoptr, use special code to continue climing.
Dan Gohman8906f952009-07-17 20:58:59 +000084 if (Operator::getOpcode(V) != Instruction::IntToPtr)
Dan Gohman3311a1f2009-01-30 02:49:14 +000085 break;
86 const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
87 // If that succeeded in finding a pointer, continue the search.
88 if (!isa<PointerType>(O->getType()))
89 break;
90 V = O;
91 } while (1);
92 return V;
93}
94
95/// getUnderlyingObjectForInstr - If this machine instr has memory reference
96/// information and it can be tracked to a normal reference to a known
97/// object, return the Value for that object. Otherwise return null.
98static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI) {
99 if (!MI->hasOneMemOperand() ||
Dan Gohmanc76909a2009-09-25 20:36:54 +0000100 !(*MI->memoperands_begin())->getValue() ||
101 (*MI->memoperands_begin())->isVolatile())
Dan Gohman3311a1f2009-01-30 02:49:14 +0000102 return 0;
103
Dan Gohmanc76909a2009-09-25 20:36:54 +0000104 const Value *V = (*MI->memoperands_begin())->getValue();
Dan Gohman3311a1f2009-01-30 02:49:14 +0000105 if (!V)
106 return 0;
107
108 V = getUnderlyingObject(V);
Evan Chengff89dcb2009-10-18 18:16:27 +0000109 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
110 // For now, ignore PseudoSourceValues which may alias LLVM IR values
111 // because the code that uses this function has no way to cope with
112 // such aliases.
113 if (PSV->isAliased())
114 return 0;
115 return V;
116 }
Dan Gohman3311a1f2009-01-30 02:49:14 +0000117
Evan Chengff89dcb2009-10-18 18:16:27 +0000118 if (isIdentifiedObject(V))
119 return V;
120
121 return 0;
Dan Gohman3311a1f2009-01-30 02:49:14 +0000122}
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 Gohmana70dca12009-10-09 23:27:56 +0000135void ScheduleDAGInstrs::BuildSchedGraph(AliasAnalysis *AA) {
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.
David Goodwin71046162009-08-13 16:05:04 +0000158 const TargetSubtarget &ST = TM.getSubtarget<TargetSubtarget>();
159 unsigned SpecialAddressLatency = ST.getSpecialAddressLatency();
Dan Gohman8749b612008-12-16 03:35:01 +0000160
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];
David Goodwind94a4e52009-08-10 15:55:25 +0000187 // Optionally add output and anti dependencies. For anti
188 // dependencies we use a latency of 0 because for a multi-issue
189 // target we want to allow the defining instruction to issue
190 // in the same cycle as the using instruction.
191 // TODO: Using a latency of 1 here for output dependencies assumes
192 // there's no cost for reusing registers.
Dan Gohman54e4c362008-12-09 22:54:47 +0000193 SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
David Goodwind94a4e52009-08-10 15:55:25 +0000194 unsigned AOLatency = (Kind == SDep::Anti) ? 0 : 1;
Dan Gohman3f237442008-12-16 03:25:46 +0000195 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
196 SUnit *DefSU = DefList[i];
197 if (DefSU != SU &&
198 (Kind != SDep::Output || !MO.isDead() ||
199 !DefSU->getInstr()->registerDefIsDead(Reg)))
David Goodwind94a4e52009-08-10 15:55:25 +0000200 DefSU->addPred(SDep(SU, Kind, AOLatency, /*Reg=*/Reg));
Dan Gohman3f237442008-12-16 03:25:46 +0000201 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000202 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
Dan Gohman3f237442008-12-16 03:25:46 +0000203 std::vector<SUnit *> &DefList = Defs[*Alias];
204 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
205 SUnit *DefSU = DefList[i];
206 if (DefSU != SU &&
207 (Kind != SDep::Output || !MO.isDead() ||
208 !DefSU->getInstr()->registerDefIsDead(Reg)))
David Goodwind94a4e52009-08-10 15:55:25 +0000209 DefSU->addPred(SDep(SU, Kind, AOLatency, /*Reg=*/ *Alias));
Dan Gohman3f237442008-12-16 03:25:46 +0000210 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000211 }
212
213 if (MO.isDef()) {
214 // Add any data dependencies.
Dan Gohman3f237442008-12-16 03:25:46 +0000215 unsigned DataLatency = SU->Latency;
216 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
217 SUnit *UseSU = UseList[i];
218 if (UseSU != SU) {
Dan Gohman8749b612008-12-16 03:35:01 +0000219 unsigned LDataLatency = DataLatency;
220 // Optionally add in a special extra latency for nodes that
221 // feed addresses.
222 // TODO: Do this for register aliases too.
David Goodwindc4bdcd2009-08-19 16:08:58 +0000223 // TODO: Perhaps we should get rid of
224 // SpecialAddressLatency and just move this into
225 // adjustSchedDependency for the targets that care about
226 // it.
Dan Gohman8749b612008-12-16 03:35:01 +0000227 if (SpecialAddressLatency != 0 && !UnitLatencies) {
228 MachineInstr *UseMI = UseSU->getInstr();
229 const TargetInstrDesc &UseTID = UseMI->getDesc();
230 int RegUseIndex = UseMI->findRegisterUseOperandIdx(Reg);
231 assert(RegUseIndex >= 0 && "UseMI doesn's use register!");
232 if ((UseTID.mayLoad() || UseTID.mayStore()) &&
233 (unsigned)RegUseIndex < UseTID.getNumOperands() &&
234 UseTID.OpInfo[RegUseIndex].isLookupPtrRegClass())
235 LDataLatency += SpecialAddressLatency;
236 }
David Goodwindc4bdcd2009-08-19 16:08:58 +0000237 // Adjust the dependence latency using operand def/use
238 // information (if any), and then allow the target to
239 // perform its own adjustments.
David Goodwin71046162009-08-13 16:05:04 +0000240 const SDep& dep = SDep(SU, SDep::Data, LDataLatency, Reg);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000241 if (!UnitLatencies) {
242 ComputeOperandLatency(SU, UseSU, (SDep &)dep);
243 ST.adjustSchedDependency(SU, UseSU, (SDep &)dep);
244 }
David Goodwin71046162009-08-13 16:05:04 +0000245 UseSU->addPred(dep);
Dan Gohman3f237442008-12-16 03:25:46 +0000246 }
247 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000248 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
249 std::vector<SUnit *> &UseList = Uses[*Alias];
Dan Gohman3f237442008-12-16 03:25:46 +0000250 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
251 SUnit *UseSU = UseList[i];
David Goodwin71046162009-08-13 16:05:04 +0000252 if (UseSU != SU) {
253 const SDep& dep = SDep(SU, SDep::Data, DataLatency, *Alias);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000254 if (!UnitLatencies) {
255 ComputeOperandLatency(SU, UseSU, (SDep &)dep);
256 ST.adjustSchedDependency(SU, UseSU, (SDep &)dep);
257 }
David Goodwin71046162009-08-13 16:05:04 +0000258 UseSU->addPred(dep);
259 }
Dan Gohman3f237442008-12-16 03:25:46 +0000260 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000261 }
262
Dan Gohman8749b612008-12-16 03:35:01 +0000263 // If a def is going to wrap back around to the top of the loop,
264 // backschedule it.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000265 if (!UnitLatencies && DefList.empty()) {
Dan Gohman8749b612008-12-16 03:35:01 +0000266 LoopDependencies::LoopDeps::iterator I = LoopRegs.Deps.find(Reg);
267 if (I != LoopRegs.Deps.end()) {
268 const MachineOperand *UseMO = I->second.first;
269 unsigned Count = I->second.second;
270 const MachineInstr *UseMI = UseMO->getParent();
271 unsigned UseMOIdx = UseMO - &UseMI->getOperand(0);
272 const TargetInstrDesc &UseTID = UseMI->getDesc();
273 // TODO: If we knew the total depth of the region here, we could
274 // handle the case where the whole loop is inside the region but
275 // is large enough that the isScheduleHigh trick isn't needed.
276 if (UseMOIdx < UseTID.getNumOperands()) {
277 // Currently, we only support scheduling regions consisting of
278 // single basic blocks. Check to see if the instruction is in
279 // the same region by checking to see if it has the same parent.
280 if (UseMI->getParent() != MI->getParent()) {
281 unsigned Latency = SU->Latency;
282 if (UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass())
283 Latency += SpecialAddressLatency;
284 // This is a wild guess as to the portion of the latency which
285 // will be overlapped by work done outside the current
286 // scheduling region.
287 Latency -= std::min(Latency, Count);
288 // Add the artifical edge.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000289 ExitSU.addPred(SDep(SU, SDep::Order, Latency,
290 /*Reg=*/0, /*isNormalMemory=*/false,
291 /*isMustAlias=*/false,
292 /*isArtificial=*/true));
Dan Gohman8749b612008-12-16 03:35:01 +0000293 } else if (SpecialAddressLatency > 0 &&
294 UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass()) {
295 // The entire loop body is within the current scheduling region
296 // and the latency of this operation is assumed to be greater
297 // than the latency of the loop.
298 // TODO: Recursively mark data-edge predecessors as
299 // isScheduleHigh too.
300 SU->isScheduleHigh = true;
301 }
302 }
303 LoopRegs.Deps.erase(I);
304 }
305 }
306
Dan Gohman343f0c02008-11-19 23:18:57 +0000307 UseList.clear();
Dan Gohman3f237442008-12-16 03:25:46 +0000308 if (!MO.isDead())
309 DefList.clear();
310 DefList.push_back(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000311 } else {
312 UseList.push_back(SU);
313 }
314 }
Dan Gohman6a9041e2008-12-04 01:35:46 +0000315
316 // Add chain dependencies.
317 // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable
318 // after stack slots are lowered to actual addresses.
319 // TODO: Use an AliasAnalysis and do real alias-analysis queries, and
320 // produce more precise dependence information.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000321 if (TID.isCall() || TID.hasUnmodeledSideEffects()) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000322 new_chain:
Dan Gohmana629b482008-12-08 17:50:35 +0000323 // This is the conservative case. Add dependencies on all memory
324 // references.
Dan Gohman343f0c02008-11-19 23:18:57 +0000325 if (Chain)
Dan Gohman54e4c362008-12-09 22:54:47 +0000326 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000327 Chain = SU;
Dan Gohman343f0c02008-11-19 23:18:57 +0000328 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
Dan Gohman54e4c362008-12-09 22:54:47 +0000329 PendingLoads[k]->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman343f0c02008-11-19 23:18:57 +0000330 PendingLoads.clear();
Dan Gohman6a9041e2008-12-04 01:35:46 +0000331 for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(),
332 E = MemDefs.end(); I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000333 I->second->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000334 I->second = SU;
335 }
336 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
337 MemUses.begin(), E = MemUses.end(); I != E; ++I) {
338 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Dan Gohman54e4c362008-12-09 22:54:47 +0000339 I->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000340 I->second.clear();
341 }
342 // See if it is known to just have a single memory reference.
343 MachineInstr *ChainMI = Chain->getInstr();
344 const TargetInstrDesc &ChainTID = ChainMI->getDesc();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000345 if (!ChainTID.isCall() &&
Dan Gohman6a9041e2008-12-04 01:35:46 +0000346 !ChainTID.hasUnmodeledSideEffects() &&
347 ChainMI->hasOneMemOperand() &&
Dan Gohmanc76909a2009-09-25 20:36:54 +0000348 !(*ChainMI->memoperands_begin())->isVolatile() &&
349 (*ChainMI->memoperands_begin())->getValue())
Dan Gohman6a9041e2008-12-04 01:35:46 +0000350 // We know that the Chain accesses one specific memory location.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000351 ChainMMO = *ChainMI->memoperands_begin();
Dan Gohman6a9041e2008-12-04 01:35:46 +0000352 else
353 // Unknown memory accesses. Assume the worst.
354 ChainMMO = 0;
355 } else if (TID.mayStore()) {
Dan Gohman3311a1f2009-01-30 02:49:14 +0000356 if (const Value *V = getUnderlyingObjectForInstr(MI)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000357 // A store to a specific PseudoSourceValue. Add precise dependencies.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000358 // Handle the def in MemDefs, if there is one.
359 std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V);
360 if (I != MemDefs.end()) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000361 I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
362 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000363 I->second = SU;
364 } else {
365 MemDefs[V] = SU;
366 }
367 // Handle the uses in MemUses, if there are any.
Dan Gohmana629b482008-12-08 17:50:35 +0000368 std::map<const Value *, std::vector<SUnit *> >::iterator J =
369 MemUses.find(V);
Dan Gohman6a9041e2008-12-04 01:35:46 +0000370 if (J != MemUses.end()) {
371 for (unsigned i = 0, e = J->second.size(); i != e; ++i)
Dan Gohman54e4c362008-12-09 22:54:47 +0000372 J->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
373 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000374 J->second.clear();
375 }
Dan Gohman3311a1f2009-01-30 02:49:14 +0000376 // Add dependencies from all the PendingLoads, since without
377 // memoperands we must assume they alias anything.
378 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
379 PendingLoads[k]->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000380 // Add a general dependence too, if needed.
381 if (Chain)
Dan Gohman54e4c362008-12-09 22:54:47 +0000382 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000383 } else
384 // Treat all other stores conservatively.
385 goto new_chain;
386 } else if (TID.mayLoad()) {
Dan Gohmana70dca12009-10-09 23:27:56 +0000387 if (MI->isInvariantLoad(AA)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000388 // Invariant load, no chain dependencies needed!
Dan Gohman3311a1f2009-01-30 02:49:14 +0000389 } else if (const Value *V = getUnderlyingObjectForInstr(MI)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000390 // A load from a specific PseudoSourceValue. Add precise dependencies.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000391 std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V);
392 if (I != MemDefs.end())
Dan Gohman54e4c362008-12-09 22:54:47 +0000393 I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
394 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000395 MemUses[V].push_back(SU);
396
397 // Add a general dependence too, if needed.
398 if (Chain && (!ChainMMO ||
399 (ChainMMO->isStore() || ChainMMO->isVolatile())))
Dan Gohman54e4c362008-12-09 22:54:47 +0000400 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000401 } else if (MI->hasVolatileMemoryRef()) {
402 // Treat volatile loads conservatively. Note that this includes
403 // cases where memoperand information is unavailable.
404 goto new_chain;
405 } else {
Dan Gohman3311a1f2009-01-30 02:49:14 +0000406 // A normal load. Depend on the general chain, as well as on
407 // all stores. In the absense of MachineMemOperand information,
408 // we can't even assume that the load doesn't alias well-behaved
409 // memory locations.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000410 if (Chain)
Dan Gohman54e4c362008-12-09 22:54:47 +0000411 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman3311a1f2009-01-30 02:49:14 +0000412 for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(),
413 E = MemDefs.end(); I != E; ++I)
414 I->second->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000415 PendingLoads.push_back(SU);
416 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000417 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000418 }
Dan Gohman79ce2762009-01-15 19:20:50 +0000419
420 for (int i = 0, e = TRI->getNumRegs(); i != e; ++i) {
421 Defs[i].clear();
422 Uses[i].clear();
423 }
424 PendingLoads.clear();
Dan Gohman343f0c02008-11-19 23:18:57 +0000425}
426
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000427void ScheduleDAGInstrs::FinishBlock() {
428 // Nothing to do.
429}
430
Dan Gohmanc8c28272008-11-21 00:12:10 +0000431void ScheduleDAGInstrs::ComputeLatency(SUnit *SU) {
432 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
433
David Goodwind94a4e52009-08-10 15:55:25 +0000434 // Compute the latency for the node.
Dan Gohmanc8c28272008-11-21 00:12:10 +0000435 SU->Latency =
David Goodwindc4bdcd2009-08-19 16:08:58 +0000436 InstrItins.getStageLatency(SU->getInstr()->getDesc().getSchedClass());
Dan Gohman4ea8e852008-12-16 02:38:22 +0000437
438 // Simplistic target-independent heuristic: assume that loads take
439 // extra time.
440 if (InstrItins.isEmpty())
441 if (SU->getInstr()->getDesc().mayLoad())
442 SU->Latency += 2;
Dan Gohmanc8c28272008-11-21 00:12:10 +0000443}
444
David Goodwindc4bdcd2009-08-19 16:08:58 +0000445void ScheduleDAGInstrs::ComputeOperandLatency(SUnit *Def, SUnit *Use,
446 SDep& dep) const {
447 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
448 if (InstrItins.isEmpty())
449 return;
450
451 // For a data dependency with a known register...
452 if ((dep.getKind() != SDep::Data) || (dep.getReg() == 0))
453 return;
454
455 const unsigned Reg = dep.getReg();
456
457 // ... find the definition of the register in the defining
458 // instruction
459 MachineInstr *DefMI = Def->getInstr();
460 int DefIdx = DefMI->findRegisterDefOperandIdx(Reg);
461 if (DefIdx != -1) {
462 int DefCycle = InstrItins.getOperandCycle(DefMI->getDesc().getSchedClass(), DefIdx);
463 if (DefCycle >= 0) {
464 MachineInstr *UseMI = Use->getInstr();
465 const unsigned UseClass = UseMI->getDesc().getSchedClass();
466
467 // For all uses of the register, calculate the maxmimum latency
468 int Latency = -1;
469 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
470 const MachineOperand &MO = UseMI->getOperand(i);
471 if (!MO.isReg() || !MO.isUse())
472 continue;
473 unsigned MOReg = MO.getReg();
474 if (MOReg != Reg)
475 continue;
476
477 int UseCycle = InstrItins.getOperandCycle(UseClass, i);
478 if (UseCycle >= 0)
479 Latency = std::max(Latency, DefCycle - UseCycle + 1);
480 }
481
482 // If we found a latency, then replace the existing dependence latency.
483 if (Latency >= 0)
484 dep.setLatency(Latency);
485 }
486 }
487}
488
Dan Gohman343f0c02008-11-19 23:18:57 +0000489void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
490 SU->getInstr()->dump();
491}
492
493std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
494 std::string s;
495 raw_string_ostream oss(s);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000496 if (SU == &EntrySU)
497 oss << "<entry>";
498 else if (SU == &ExitSU)
499 oss << "<exit>";
500 else
501 SU->getInstr()->print(oss);
Dan Gohman343f0c02008-11-19 23:18:57 +0000502 return oss.str();
503}
504
505// EmitSchedule - Emit the machine code in scheduled order.
Evan Chengfb2e7522009-09-18 21:02:19 +0000506MachineBasicBlock *ScheduleDAGInstrs::
507EmitSchedule(DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000508 // For MachineInstr-based scheduling, we're rescheduling the instructions in
509 // the block, so start by removing them from the block.
Dan Gohman47ac0f02009-02-11 04:27:20 +0000510 while (Begin != InsertPos) {
Dan Gohmanf7119392009-01-16 22:10:20 +0000511 MachineBasicBlock::iterator I = Begin;
512 ++Begin;
513 BB->remove(I);
514 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000515
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000516 // Then re-insert them according to the given schedule.
Dan Gohman343f0c02008-11-19 23:18:57 +0000517 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
518 SUnit *SU = Sequence[i];
519 if (!SU) {
520 // Null SUnit* is a noop.
521 EmitNoop();
522 continue;
523 }
524
Dan Gohman47ac0f02009-02-11 04:27:20 +0000525 BB->insert(InsertPos, SU->getInstr());
Dan Gohman343f0c02008-11-19 23:18:57 +0000526 }
527
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000528 // Update the Begin iterator, as the first instruction in the block
529 // may have been scheduled later.
530 if (!Sequence.empty())
531 Begin = Sequence[0]->getInstr();
532
Dan Gohman343f0c02008-11-19 23:18:57 +0000533 return BB;
534}