blob: 880782e28176bee610cbcaeada68e8f29efa28e9 [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)
Evan Cheng38bdfc62009-10-18 19:58:47 +000035 : ScheduleDAG(mf), MLI(mli), MDT(mdt), LoopRegs(MLI, MDT) {
36 MFI = mf.getFrameInfo();
37}
Dan Gohman343f0c02008-11-19 23:18:57 +000038
Dan Gohman47ac0f02009-02-11 04:27:20 +000039/// Run - perform scheduling.
40///
41void ScheduleDAGInstrs::Run(MachineBasicBlock *bb,
42 MachineBasicBlock::iterator begin,
43 MachineBasicBlock::iterator end,
44 unsigned endcount) {
45 BB = bb;
46 Begin = begin;
47 InsertPosIndex = endcount;
48
49 ScheduleDAG::Run(bb, end);
50}
51
Dan Gohman3311a1f2009-01-30 02:49:14 +000052/// getUnderlyingObjectFromInt - This is the function that does the work of
53/// looking through basic ptrtoint+arithmetic+inttoptr sequences.
54static const Value *getUnderlyingObjectFromInt(const Value *V) {
55 do {
Dan Gohman8906f952009-07-17 20:58:59 +000056 if (const Operator *U = dyn_cast<Operator>(V)) {
Dan Gohman3311a1f2009-01-30 02:49:14 +000057 // If we find a ptrtoint, we can transfer control back to the
58 // regular getUnderlyingObjectFromInt.
Dan Gohman8906f952009-07-17 20:58:59 +000059 if (U->getOpcode() == Instruction::PtrToInt)
Dan Gohman3311a1f2009-01-30 02:49:14 +000060 return U->getOperand(0);
61 // If we find an add of a constant or a multiplied value, it's
62 // likely that the other operand will lead us to the base
63 // object. We don't have to worry about the case where the
Dan Gohman748f98f2009-08-07 01:26:06 +000064 // object address is somehow being computed by the multiply,
Dan Gohman3311a1f2009-01-30 02:49:14 +000065 // because our callers only care when the result is an
66 // identifibale object.
Dan Gohman8906f952009-07-17 20:58:59 +000067 if (U->getOpcode() != Instruction::Add ||
Dan Gohman3311a1f2009-01-30 02:49:14 +000068 (!isa<ConstantInt>(U->getOperand(1)) &&
Dan Gohman8906f952009-07-17 20:58:59 +000069 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul))
Dan Gohman3311a1f2009-01-30 02:49:14 +000070 return V;
71 V = U->getOperand(0);
72 } else {
73 return V;
74 }
75 assert(isa<IntegerType>(V->getType()) && "Unexpected operand type!");
76 } while (1);
77}
78
79/// getUnderlyingObject - This is a wrapper around Value::getUnderlyingObject
80/// and adds support for basic ptrtoint+arithmetic+inttoptr sequences.
81static const Value *getUnderlyingObject(const Value *V) {
82 // First just call Value::getUnderlyingObject to let it do what it does.
83 do {
84 V = V->getUnderlyingObject();
85 // If it found an inttoptr, use special code to continue climing.
Dan Gohman8906f952009-07-17 20:58:59 +000086 if (Operator::getOpcode(V) != Instruction::IntToPtr)
Dan Gohman3311a1f2009-01-30 02:49:14 +000087 break;
88 const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
89 // If that succeeded in finding a pointer, continue the search.
90 if (!isa<PointerType>(O->getType()))
91 break;
92 V = O;
93 } while (1);
94 return V;
95}
96
97/// getUnderlyingObjectForInstr - If this machine instr has memory reference
98/// information and it can be tracked to a normal reference to a known
99/// object, return the Value for that object. Otherwise return null.
Evan Cheng38bdfc62009-10-18 19:58:47 +0000100static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI,
101 const MachineFrameInfo *MFI) {
Dan Gohman3311a1f2009-01-30 02:49:14 +0000102 if (!MI->hasOneMemOperand() ||
Dan Gohmanc76909a2009-09-25 20:36:54 +0000103 !(*MI->memoperands_begin())->getValue() ||
104 (*MI->memoperands_begin())->isVolatile())
Dan Gohman3311a1f2009-01-30 02:49:14 +0000105 return 0;
106
Dan Gohmanc76909a2009-09-25 20:36:54 +0000107 const Value *V = (*MI->memoperands_begin())->getValue();
Dan Gohman3311a1f2009-01-30 02:49:14 +0000108 if (!V)
109 return 0;
110
111 V = getUnderlyingObject(V);
Evan Chengff89dcb2009-10-18 18:16:27 +0000112 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
113 // For now, ignore PseudoSourceValues which may alias LLVM IR values
114 // because the code that uses this function has no way to cope with
115 // such aliases.
Evan Cheng38bdfc62009-10-18 19:58:47 +0000116 if (PSV->isAliased(MFI))
Evan Chengff89dcb2009-10-18 18:16:27 +0000117 return 0;
118 return V;
119 }
Dan Gohman3311a1f2009-01-30 02:49:14 +0000120
Evan Chengff89dcb2009-10-18 18:16:27 +0000121 if (isIdentifiedObject(V))
122 return V;
123
124 return 0;
Dan Gohman3311a1f2009-01-30 02:49:14 +0000125}
126
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000127void ScheduleDAGInstrs::StartBlock(MachineBasicBlock *BB) {
128 if (MachineLoop *ML = MLI.getLoopFor(BB))
129 if (BB == ML->getLoopLatch()) {
130 MachineBasicBlock *Header = ML->getHeader();
131 for (MachineBasicBlock::livein_iterator I = Header->livein_begin(),
132 E = Header->livein_end(); I != E; ++I)
133 LoopLiveInRegs.insert(*I);
134 LoopRegs.VisitLoop(ML);
135 }
136}
137
Dan Gohmana70dca12009-10-09 23:27:56 +0000138void ScheduleDAGInstrs::BuildSchedGraph(AliasAnalysis *AA) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000139 // We'll be allocating one SUnit for each instruction, plus one for
140 // the region exit node.
Dan Gohman343f0c02008-11-19 23:18:57 +0000141 SUnits.reserve(BB->size());
142
Dan Gohman6a9041e2008-12-04 01:35:46 +0000143 // We build scheduling units by walking a block's instruction list from bottom
144 // to top.
145
Dan Gohman6a9041e2008-12-04 01:35:46 +0000146 // Remember where a generic side-effecting instruction is as we procede. If
147 // ChainMMO is null, this is assumed to have arbitrary side-effects. If
148 // ChainMMO is non-null, then Chain makes only a single memory reference.
149 SUnit *Chain = 0;
150 MachineMemOperand *ChainMMO = 0;
151
152 // Memory references to specific known memory locations are tracked so that
153 // they can be given more precise dependencies.
154 std::map<const Value *, SUnit *> MemDefs;
155 std::map<const Value *, std::vector<SUnit *> > MemUses;
156
Dan Gohman3f237442008-12-16 03:25:46 +0000157 // Check to see if the scheduler cares about latencies.
158 bool UnitLatencies = ForceUnitLatencies();
159
Dan Gohman8749b612008-12-16 03:35:01 +0000160 // Ask the target if address-backscheduling is desirable, and if so how much.
David Goodwin71046162009-08-13 16:05:04 +0000161 const TargetSubtarget &ST = TM.getSubtarget<TargetSubtarget>();
162 unsigned SpecialAddressLatency = ST.getSpecialAddressLatency();
Dan Gohman8749b612008-12-16 03:35:01 +0000163
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000164 // Walk the list of instructions, from bottom moving up.
Dan Gohman47ac0f02009-02-11 04:27:20 +0000165 for (MachineBasicBlock::iterator MII = InsertPos, MIE = Begin;
Dan Gohman343f0c02008-11-19 23:18:57 +0000166 MII != MIE; --MII) {
167 MachineInstr *MI = prior(MII);
Dan Gohman3f237442008-12-16 03:25:46 +0000168 const TargetInstrDesc &TID = MI->getDesc();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000169 assert(!TID.isTerminator() && !MI->isLabel() &&
170 "Cannot schedule terminators or labels!");
171 // Create the SUnit for this MI.
Dan Gohman343f0c02008-11-19 23:18:57 +0000172 SUnit *SU = NewSUnit(MI);
173
Dan Gohman54e4c362008-12-09 22:54:47 +0000174 // Assign the Latency field of SU using target-provided information.
Dan Gohman3f237442008-12-16 03:25:46 +0000175 if (UnitLatencies)
176 SU->Latency = 1;
177 else
178 ComputeLatency(SU);
Dan Gohman54e4c362008-12-09 22:54:47 +0000179
Dan Gohman6a9041e2008-12-04 01:35:46 +0000180 // Add register-based dependencies (data, anti, and output).
Dan Gohman343f0c02008-11-19 23:18:57 +0000181 for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) {
182 const MachineOperand &MO = MI->getOperand(j);
183 if (!MO.isReg()) continue;
184 unsigned Reg = MO.getReg();
185 if (Reg == 0) continue;
186
187 assert(TRI->isPhysicalRegister(Reg) && "Virtual register encountered!");
188 std::vector<SUnit *> &UseList = Uses[Reg];
Dan Gohman3f237442008-12-16 03:25:46 +0000189 std::vector<SUnit *> &DefList = Defs[Reg];
David Goodwind94a4e52009-08-10 15:55:25 +0000190 // Optionally add output and anti dependencies. For anti
191 // dependencies we use a latency of 0 because for a multi-issue
192 // target we want to allow the defining instruction to issue
193 // in the same cycle as the using instruction.
194 // TODO: Using a latency of 1 here for output dependencies assumes
195 // there's no cost for reusing registers.
Dan Gohman54e4c362008-12-09 22:54:47 +0000196 SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
David Goodwind94a4e52009-08-10 15:55:25 +0000197 unsigned AOLatency = (Kind == SDep::Anti) ? 0 : 1;
Dan Gohman3f237442008-12-16 03:25:46 +0000198 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
199 SUnit *DefSU = DefList[i];
200 if (DefSU != SU &&
201 (Kind != SDep::Output || !MO.isDead() ||
202 !DefSU->getInstr()->registerDefIsDead(Reg)))
David Goodwind94a4e52009-08-10 15:55:25 +0000203 DefSU->addPred(SDep(SU, Kind, AOLatency, /*Reg=*/Reg));
Dan Gohman3f237442008-12-16 03:25:46 +0000204 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000205 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
Dan Gohman3f237442008-12-16 03:25:46 +0000206 std::vector<SUnit *> &DefList = Defs[*Alias];
207 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
208 SUnit *DefSU = DefList[i];
209 if (DefSU != SU &&
210 (Kind != SDep::Output || !MO.isDead() ||
Dan Gohman91203cf2009-10-26 18:26:18 +0000211 !DefSU->getInstr()->registerDefIsDead(*Alias)))
David Goodwind94a4e52009-08-10 15:55:25 +0000212 DefSU->addPred(SDep(SU, Kind, AOLatency, /*Reg=*/ *Alias));
Dan Gohman3f237442008-12-16 03:25:46 +0000213 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000214 }
215
216 if (MO.isDef()) {
217 // Add any data dependencies.
Dan Gohman3f237442008-12-16 03:25:46 +0000218 unsigned DataLatency = SU->Latency;
219 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
220 SUnit *UseSU = UseList[i];
221 if (UseSU != SU) {
Dan Gohman8749b612008-12-16 03:35:01 +0000222 unsigned LDataLatency = DataLatency;
223 // Optionally add in a special extra latency for nodes that
224 // feed addresses.
225 // TODO: Do this for register aliases too.
David Goodwindc4bdcd2009-08-19 16:08:58 +0000226 // TODO: Perhaps we should get rid of
227 // SpecialAddressLatency and just move this into
228 // adjustSchedDependency for the targets that care about
229 // it.
Dan Gohman8749b612008-12-16 03:35:01 +0000230 if (SpecialAddressLatency != 0 && !UnitLatencies) {
231 MachineInstr *UseMI = UseSU->getInstr();
232 const TargetInstrDesc &UseTID = UseMI->getDesc();
233 int RegUseIndex = UseMI->findRegisterUseOperandIdx(Reg);
234 assert(RegUseIndex >= 0 && "UseMI doesn's use register!");
235 if ((UseTID.mayLoad() || UseTID.mayStore()) &&
236 (unsigned)RegUseIndex < UseTID.getNumOperands() &&
237 UseTID.OpInfo[RegUseIndex].isLookupPtrRegClass())
238 LDataLatency += SpecialAddressLatency;
239 }
David Goodwindc4bdcd2009-08-19 16:08:58 +0000240 // Adjust the dependence latency using operand def/use
241 // information (if any), and then allow the target to
242 // perform its own adjustments.
David Goodwin71046162009-08-13 16:05:04 +0000243 const SDep& dep = SDep(SU, SDep::Data, LDataLatency, Reg);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000244 if (!UnitLatencies) {
245 ComputeOperandLatency(SU, UseSU, (SDep &)dep);
246 ST.adjustSchedDependency(SU, UseSU, (SDep &)dep);
247 }
David Goodwin71046162009-08-13 16:05:04 +0000248 UseSU->addPred(dep);
Dan Gohman3f237442008-12-16 03:25:46 +0000249 }
250 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000251 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
252 std::vector<SUnit *> &UseList = Uses[*Alias];
Dan Gohman3f237442008-12-16 03:25:46 +0000253 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
254 SUnit *UseSU = UseList[i];
David Goodwin71046162009-08-13 16:05:04 +0000255 if (UseSU != SU) {
256 const SDep& dep = SDep(SU, SDep::Data, DataLatency, *Alias);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000257 if (!UnitLatencies) {
258 ComputeOperandLatency(SU, UseSU, (SDep &)dep);
259 ST.adjustSchedDependency(SU, UseSU, (SDep &)dep);
260 }
David Goodwin71046162009-08-13 16:05:04 +0000261 UseSU->addPred(dep);
262 }
Dan Gohman3f237442008-12-16 03:25:46 +0000263 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000264 }
265
Dan Gohman8749b612008-12-16 03:35:01 +0000266 // If a def is going to wrap back around to the top of the loop,
267 // backschedule it.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000268 if (!UnitLatencies && DefList.empty()) {
Dan Gohman8749b612008-12-16 03:35:01 +0000269 LoopDependencies::LoopDeps::iterator I = LoopRegs.Deps.find(Reg);
270 if (I != LoopRegs.Deps.end()) {
271 const MachineOperand *UseMO = I->second.first;
272 unsigned Count = I->second.second;
273 const MachineInstr *UseMI = UseMO->getParent();
274 unsigned UseMOIdx = UseMO - &UseMI->getOperand(0);
275 const TargetInstrDesc &UseTID = UseMI->getDesc();
276 // TODO: If we knew the total depth of the region here, we could
277 // handle the case where the whole loop is inside the region but
278 // is large enough that the isScheduleHigh trick isn't needed.
279 if (UseMOIdx < UseTID.getNumOperands()) {
280 // Currently, we only support scheduling regions consisting of
281 // single basic blocks. Check to see if the instruction is in
282 // the same region by checking to see if it has the same parent.
283 if (UseMI->getParent() != MI->getParent()) {
284 unsigned Latency = SU->Latency;
285 if (UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass())
286 Latency += SpecialAddressLatency;
287 // This is a wild guess as to the portion of the latency which
288 // will be overlapped by work done outside the current
289 // scheduling region.
290 Latency -= std::min(Latency, Count);
291 // Add the artifical edge.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000292 ExitSU.addPred(SDep(SU, SDep::Order, Latency,
293 /*Reg=*/0, /*isNormalMemory=*/false,
294 /*isMustAlias=*/false,
295 /*isArtificial=*/true));
Dan Gohman8749b612008-12-16 03:35:01 +0000296 } else if (SpecialAddressLatency > 0 &&
297 UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass()) {
298 // The entire loop body is within the current scheduling region
299 // and the latency of this operation is assumed to be greater
300 // than the latency of the loop.
301 // TODO: Recursively mark data-edge predecessors as
302 // isScheduleHigh too.
303 SU->isScheduleHigh = true;
304 }
305 }
306 LoopRegs.Deps.erase(I);
307 }
308 }
309
Dan Gohman343f0c02008-11-19 23:18:57 +0000310 UseList.clear();
Dan Gohman3f237442008-12-16 03:25:46 +0000311 if (!MO.isDead())
312 DefList.clear();
313 DefList.push_back(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000314 } else {
315 UseList.push_back(SU);
316 }
317 }
Dan Gohman6a9041e2008-12-04 01:35:46 +0000318
319 // Add chain dependencies.
320 // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable
321 // after stack slots are lowered to actual addresses.
322 // TODO: Use an AliasAnalysis and do real alias-analysis queries, and
323 // produce more precise dependence information.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000324 if (TID.isCall() || TID.hasUnmodeledSideEffects()) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000325 new_chain:
Dan Gohmana629b482008-12-08 17:50:35 +0000326 // This is the conservative case. Add dependencies on all memory
327 // references.
Dan Gohman343f0c02008-11-19 23:18:57 +0000328 if (Chain)
Dan Gohman54e4c362008-12-09 22:54:47 +0000329 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000330 Chain = SU;
Dan Gohman343f0c02008-11-19 23:18:57 +0000331 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
Dan Gohman54e4c362008-12-09 22:54:47 +0000332 PendingLoads[k]->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman343f0c02008-11-19 23:18:57 +0000333 PendingLoads.clear();
Dan Gohman6a9041e2008-12-04 01:35:46 +0000334 for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(),
335 E = MemDefs.end(); I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000336 I->second->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000337 I->second = SU;
338 }
339 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
340 MemUses.begin(), E = MemUses.end(); I != E; ++I) {
341 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Dan Gohman54e4c362008-12-09 22:54:47 +0000342 I->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000343 I->second.clear();
344 }
345 // See if it is known to just have a single memory reference.
346 MachineInstr *ChainMI = Chain->getInstr();
347 const TargetInstrDesc &ChainTID = ChainMI->getDesc();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000348 if (!ChainTID.isCall() &&
Dan Gohman6a9041e2008-12-04 01:35:46 +0000349 !ChainTID.hasUnmodeledSideEffects() &&
350 ChainMI->hasOneMemOperand() &&
Dan Gohmanc76909a2009-09-25 20:36:54 +0000351 !(*ChainMI->memoperands_begin())->isVolatile() &&
352 (*ChainMI->memoperands_begin())->getValue())
Dan Gohman6a9041e2008-12-04 01:35:46 +0000353 // We know that the Chain accesses one specific memory location.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000354 ChainMMO = *ChainMI->memoperands_begin();
Dan Gohman6a9041e2008-12-04 01:35:46 +0000355 else
356 // Unknown memory accesses. Assume the worst.
357 ChainMMO = 0;
358 } else if (TID.mayStore()) {
Evan Cheng38bdfc62009-10-18 19:58:47 +0000359 if (const Value *V = getUnderlyingObjectForInstr(MI, MFI)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000360 // A store to a specific PseudoSourceValue. Add precise dependencies.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000361 // Handle the def in MemDefs, if there is one.
362 std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V);
363 if (I != MemDefs.end()) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000364 I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
365 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000366 I->second = SU;
367 } else {
368 MemDefs[V] = SU;
369 }
370 // Handle the uses in MemUses, if there are any.
Dan Gohmana629b482008-12-08 17:50:35 +0000371 std::map<const Value *, std::vector<SUnit *> >::iterator J =
372 MemUses.find(V);
Dan Gohman6a9041e2008-12-04 01:35:46 +0000373 if (J != MemUses.end()) {
374 for (unsigned i = 0, e = J->second.size(); i != e; ++i)
Dan Gohman54e4c362008-12-09 22:54:47 +0000375 J->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
376 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000377 J->second.clear();
378 }
Dan Gohman3311a1f2009-01-30 02:49:14 +0000379 // Add dependencies from all the PendingLoads, since without
380 // memoperands we must assume they alias anything.
381 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
382 PendingLoads[k]->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000383 // Add a general dependence too, if needed.
384 if (Chain)
Dan Gohman54e4c362008-12-09 22:54:47 +0000385 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000386 } else
387 // Treat all other stores conservatively.
388 goto new_chain;
389 } else if (TID.mayLoad()) {
Dan Gohmana70dca12009-10-09 23:27:56 +0000390 if (MI->isInvariantLoad(AA)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000391 // Invariant load, no chain dependencies needed!
Evan Cheng38bdfc62009-10-18 19:58:47 +0000392 } else if (const Value *V = getUnderlyingObjectForInstr(MI, MFI)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000393 // A load from a specific PseudoSourceValue. Add precise dependencies.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000394 std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V);
395 if (I != MemDefs.end())
Dan Gohman54e4c362008-12-09 22:54:47 +0000396 I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
397 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000398 MemUses[V].push_back(SU);
399
400 // Add a general dependence too, if needed.
401 if (Chain && (!ChainMMO ||
402 (ChainMMO->isStore() || ChainMMO->isVolatile())))
Dan Gohman54e4c362008-12-09 22:54:47 +0000403 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000404 } else if (MI->hasVolatileMemoryRef()) {
405 // Treat volatile loads conservatively. Note that this includes
406 // cases where memoperand information is unavailable.
407 goto new_chain;
408 } else {
Dan Gohman3311a1f2009-01-30 02:49:14 +0000409 // A normal load. Depend on the general chain, as well as on
410 // all stores. In the absense of MachineMemOperand information,
411 // we can't even assume that the load doesn't alias well-behaved
412 // memory locations.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000413 if (Chain)
Dan Gohman54e4c362008-12-09 22:54:47 +0000414 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman3311a1f2009-01-30 02:49:14 +0000415 for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(),
416 E = MemDefs.end(); I != E; ++I)
417 I->second->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000418 PendingLoads.push_back(SU);
419 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000420 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000421 }
Dan Gohman79ce2762009-01-15 19:20:50 +0000422
423 for (int i = 0, e = TRI->getNumRegs(); i != e; ++i) {
424 Defs[i].clear();
425 Uses[i].clear();
426 }
427 PendingLoads.clear();
Dan Gohman343f0c02008-11-19 23:18:57 +0000428}
429
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000430void ScheduleDAGInstrs::FinishBlock() {
431 // Nothing to do.
432}
433
Dan Gohmanc8c28272008-11-21 00:12:10 +0000434void ScheduleDAGInstrs::ComputeLatency(SUnit *SU) {
435 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
436
David Goodwind94a4e52009-08-10 15:55:25 +0000437 // Compute the latency for the node.
Dan Gohmanc8c28272008-11-21 00:12:10 +0000438 SU->Latency =
David Goodwindc4bdcd2009-08-19 16:08:58 +0000439 InstrItins.getStageLatency(SU->getInstr()->getDesc().getSchedClass());
Dan Gohman4ea8e852008-12-16 02:38:22 +0000440
441 // Simplistic target-independent heuristic: assume that loads take
442 // extra time.
443 if (InstrItins.isEmpty())
444 if (SU->getInstr()->getDesc().mayLoad())
445 SU->Latency += 2;
Dan Gohmanc8c28272008-11-21 00:12:10 +0000446}
447
David Goodwindc4bdcd2009-08-19 16:08:58 +0000448void ScheduleDAGInstrs::ComputeOperandLatency(SUnit *Def, SUnit *Use,
449 SDep& dep) const {
450 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
451 if (InstrItins.isEmpty())
452 return;
453
454 // For a data dependency with a known register...
455 if ((dep.getKind() != SDep::Data) || (dep.getReg() == 0))
456 return;
457
458 const unsigned Reg = dep.getReg();
459
460 // ... find the definition of the register in the defining
461 // instruction
462 MachineInstr *DefMI = Def->getInstr();
463 int DefIdx = DefMI->findRegisterDefOperandIdx(Reg);
464 if (DefIdx != -1) {
465 int DefCycle = InstrItins.getOperandCycle(DefMI->getDesc().getSchedClass(), DefIdx);
466 if (DefCycle >= 0) {
467 MachineInstr *UseMI = Use->getInstr();
468 const unsigned UseClass = UseMI->getDesc().getSchedClass();
469
470 // For all uses of the register, calculate the maxmimum latency
471 int Latency = -1;
472 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
473 const MachineOperand &MO = UseMI->getOperand(i);
474 if (!MO.isReg() || !MO.isUse())
475 continue;
476 unsigned MOReg = MO.getReg();
477 if (MOReg != Reg)
478 continue;
479
480 int UseCycle = InstrItins.getOperandCycle(UseClass, i);
481 if (UseCycle >= 0)
482 Latency = std::max(Latency, DefCycle - UseCycle + 1);
483 }
484
485 // If we found a latency, then replace the existing dependence latency.
486 if (Latency >= 0)
487 dep.setLatency(Latency);
488 }
489 }
490}
491
Dan Gohman343f0c02008-11-19 23:18:57 +0000492void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
493 SU->getInstr()->dump();
494}
495
496std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
497 std::string s;
498 raw_string_ostream oss(s);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000499 if (SU == &EntrySU)
500 oss << "<entry>";
501 else if (SU == &ExitSU)
502 oss << "<exit>";
503 else
504 SU->getInstr()->print(oss);
Dan Gohman343f0c02008-11-19 23:18:57 +0000505 return oss.str();
506}
507
508// EmitSchedule - Emit the machine code in scheduled order.
Evan Chengfb2e7522009-09-18 21:02:19 +0000509MachineBasicBlock *ScheduleDAGInstrs::
510EmitSchedule(DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000511 // For MachineInstr-based scheduling, we're rescheduling the instructions in
512 // the block, so start by removing them from the block.
Dan Gohman47ac0f02009-02-11 04:27:20 +0000513 while (Begin != InsertPos) {
Dan Gohmanf7119392009-01-16 22:10:20 +0000514 MachineBasicBlock::iterator I = Begin;
515 ++Begin;
516 BB->remove(I);
517 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000518
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000519 // Then re-insert them according to the given schedule.
Dan Gohman343f0c02008-11-19 23:18:57 +0000520 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
521 SUnit *SU = Sequence[i];
522 if (!SU) {
523 // Null SUnit* is a noop.
524 EmitNoop();
525 continue;
526 }
527
Dan Gohman47ac0f02009-02-11 04:27:20 +0000528 BB->insert(InsertPos, SU->getInstr());
Dan Gohman343f0c02008-11-19 23:18:57 +0000529 }
530
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000531 // Update the Begin iterator, as the first instruction in the block
532 // may have been scheduled later.
533 if (!Sequence.empty())
534 Begin = Sequence[0]->getInstr();
535
Dan Gohman343f0c02008-11-19 23:18:57 +0000536 return BB;
537}