blob: a65e8a006dbf0845f4ed822250420b2d313d81bb [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.
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000320 // Chain dependencies used to enforce memory order should have
321 // latency of 0 (except for true dependency of Store followed by
322 // aliased Load... we estimate that with a single cycle of latency
323 // assuming the hardware will bypass)
Dan Gohman6a9041e2008-12-04 01:35:46 +0000324 // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable
325 // after stack slots are lowered to actual addresses.
326 // TODO: Use an AliasAnalysis and do real alias-analysis queries, and
327 // produce more precise dependence information.
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000328#define STORE_LOAD_LATENCY 1
329 unsigned TrueMemOrderLatency = 0;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000330 if (TID.isCall() || TID.hasUnmodeledSideEffects()) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000331 new_chain:
Dan Gohmana629b482008-12-08 17:50:35 +0000332 // This is the conservative case. Add dependencies on all memory
333 // references.
Dan Gohman343f0c02008-11-19 23:18:57 +0000334 if (Chain)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000335 Chain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000336 Chain = SU;
Dan Gohman343f0c02008-11-19 23:18:57 +0000337 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000338 PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
Dan Gohman343f0c02008-11-19 23:18:57 +0000339 PendingLoads.clear();
Dan Gohman6a9041e2008-12-04 01:35:46 +0000340 for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(),
341 E = MemDefs.end(); I != E; ++I) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000342 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000343 I->second = SU;
344 }
345 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
346 MemUses.begin(), E = MemUses.end(); I != E; ++I) {
347 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000348 I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000349 I->second.clear();
350 }
351 // See if it is known to just have a single memory reference.
352 MachineInstr *ChainMI = Chain->getInstr();
353 const TargetInstrDesc &ChainTID = ChainMI->getDesc();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000354 if (!ChainTID.isCall() &&
Dan Gohman6a9041e2008-12-04 01:35:46 +0000355 !ChainTID.hasUnmodeledSideEffects() &&
356 ChainMI->hasOneMemOperand() &&
Dan Gohmanc76909a2009-09-25 20:36:54 +0000357 !(*ChainMI->memoperands_begin())->isVolatile() &&
358 (*ChainMI->memoperands_begin())->getValue())
Dan Gohman6a9041e2008-12-04 01:35:46 +0000359 // We know that the Chain accesses one specific memory location.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000360 ChainMMO = *ChainMI->memoperands_begin();
Dan Gohman6a9041e2008-12-04 01:35:46 +0000361 else
362 // Unknown memory accesses. Assume the worst.
363 ChainMMO = 0;
364 } else if (TID.mayStore()) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000365 TrueMemOrderLatency = STORE_LOAD_LATENCY;
Evan Cheng38bdfc62009-10-18 19:58:47 +0000366 if (const Value *V = getUnderlyingObjectForInstr(MI, MFI)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000367 // A store to a specific PseudoSourceValue. Add precise dependencies.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000368 // Handle the def in MemDefs, if there is one.
369 std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V);
370 if (I != MemDefs.end()) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000371 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0,
Dan Gohman54e4c362008-12-09 22:54:47 +0000372 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000373 I->second = SU;
374 } else {
375 MemDefs[V] = SU;
376 }
377 // Handle the uses in MemUses, if there are any.
Dan Gohmana629b482008-12-08 17:50:35 +0000378 std::map<const Value *, std::vector<SUnit *> >::iterator J =
379 MemUses.find(V);
Dan Gohman6a9041e2008-12-04 01:35:46 +0000380 if (J != MemUses.end()) {
381 for (unsigned i = 0, e = J->second.size(); i != e; ++i)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000382 J->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency,
383 /*Reg=*/0, /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000384 J->second.clear();
385 }
Dan Gohman3311a1f2009-01-30 02:49:14 +0000386 // Add dependencies from all the PendingLoads, since without
387 // memoperands we must assume they alias anything.
388 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000389 PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000390 // Add a general dependence too, if needed.
391 if (Chain)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000392 Chain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
393 } else {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000394 // Treat all other stores conservatively.
395 goto new_chain;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000396 }
Dan Gohman6a9041e2008-12-04 01:35:46 +0000397 } else if (TID.mayLoad()) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000398 TrueMemOrderLatency = 0;
Dan Gohmana70dca12009-10-09 23:27:56 +0000399 if (MI->isInvariantLoad(AA)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000400 // Invariant load, no chain dependencies needed!
Evan Cheng38bdfc62009-10-18 19:58:47 +0000401 } else if (const Value *V = getUnderlyingObjectForInstr(MI, MFI)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000402 // A load from a specific PseudoSourceValue. Add precise dependencies.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000403 std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V);
404 if (I != MemDefs.end())
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000405 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0,
Dan Gohman54e4c362008-12-09 22:54:47 +0000406 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000407 MemUses[V].push_back(SU);
408
409 // Add a general dependence too, if needed.
410 if (Chain && (!ChainMMO ||
411 (ChainMMO->isStore() || ChainMMO->isVolatile())))
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000412 Chain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000413 } else if (MI->hasVolatileMemoryRef()) {
414 // Treat volatile loads conservatively. Note that this includes
415 // cases where memoperand information is unavailable.
416 goto new_chain;
417 } else {
Dan Gohman3311a1f2009-01-30 02:49:14 +0000418 // A normal load. Depend on the general chain, as well as on
419 // all stores. In the absense of MachineMemOperand information,
420 // we can't even assume that the load doesn't alias well-behaved
421 // memory locations.
Dan Gohman6a9041e2008-12-04 01:35:46 +0000422 if (Chain)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000423 Chain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Dan Gohman3311a1f2009-01-30 02:49:14 +0000424 for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(),
425 E = MemDefs.end(); I != E; ++I)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000426 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000427 PendingLoads.push_back(SU);
428 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000429 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000430 }
Dan Gohman79ce2762009-01-15 19:20:50 +0000431
432 for (int i = 0, e = TRI->getNumRegs(); i != e; ++i) {
433 Defs[i].clear();
434 Uses[i].clear();
435 }
436 PendingLoads.clear();
Dan Gohman343f0c02008-11-19 23:18:57 +0000437}
438
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000439void ScheduleDAGInstrs::FinishBlock() {
440 // Nothing to do.
441}
442
Dan Gohmanc8c28272008-11-21 00:12:10 +0000443void ScheduleDAGInstrs::ComputeLatency(SUnit *SU) {
444 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
445
David Goodwind94a4e52009-08-10 15:55:25 +0000446 // Compute the latency for the node.
Dan Gohmanc8c28272008-11-21 00:12:10 +0000447 SU->Latency =
David Goodwindc4bdcd2009-08-19 16:08:58 +0000448 InstrItins.getStageLatency(SU->getInstr()->getDesc().getSchedClass());
Dan Gohman4ea8e852008-12-16 02:38:22 +0000449
450 // Simplistic target-independent heuristic: assume that loads take
451 // extra time.
452 if (InstrItins.isEmpty())
453 if (SU->getInstr()->getDesc().mayLoad())
454 SU->Latency += 2;
Dan Gohmanc8c28272008-11-21 00:12:10 +0000455}
456
David Goodwindc4bdcd2009-08-19 16:08:58 +0000457void ScheduleDAGInstrs::ComputeOperandLatency(SUnit *Def, SUnit *Use,
458 SDep& dep) const {
459 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
460 if (InstrItins.isEmpty())
461 return;
462
463 // For a data dependency with a known register...
464 if ((dep.getKind() != SDep::Data) || (dep.getReg() == 0))
465 return;
466
467 const unsigned Reg = dep.getReg();
468
469 // ... find the definition of the register in the defining
470 // instruction
471 MachineInstr *DefMI = Def->getInstr();
472 int DefIdx = DefMI->findRegisterDefOperandIdx(Reg);
473 if (DefIdx != -1) {
474 int DefCycle = InstrItins.getOperandCycle(DefMI->getDesc().getSchedClass(), DefIdx);
475 if (DefCycle >= 0) {
476 MachineInstr *UseMI = Use->getInstr();
477 const unsigned UseClass = UseMI->getDesc().getSchedClass();
478
479 // For all uses of the register, calculate the maxmimum latency
480 int Latency = -1;
481 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
482 const MachineOperand &MO = UseMI->getOperand(i);
483 if (!MO.isReg() || !MO.isUse())
484 continue;
485 unsigned MOReg = MO.getReg();
486 if (MOReg != Reg)
487 continue;
488
489 int UseCycle = InstrItins.getOperandCycle(UseClass, i);
490 if (UseCycle >= 0)
491 Latency = std::max(Latency, DefCycle - UseCycle + 1);
492 }
493
494 // If we found a latency, then replace the existing dependence latency.
495 if (Latency >= 0)
496 dep.setLatency(Latency);
497 }
498 }
499}
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);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000508 if (SU == &EntrySU)
509 oss << "<entry>";
510 else if (SU == &ExitSU)
511 oss << "<exit>";
512 else
513 SU->getInstr()->print(oss);
Dan Gohman343f0c02008-11-19 23:18:57 +0000514 return oss.str();
515}
516
517// EmitSchedule - Emit the machine code in scheduled order.
Evan Chengfb2e7522009-09-18 21:02:19 +0000518MachineBasicBlock *ScheduleDAGInstrs::
519EmitSchedule(DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000520 // For MachineInstr-based scheduling, we're rescheduling the instructions in
521 // the block, so start by removing them from the block.
Dan Gohman47ac0f02009-02-11 04:27:20 +0000522 while (Begin != InsertPos) {
Dan Gohmanf7119392009-01-16 22:10:20 +0000523 MachineBasicBlock::iterator I = Begin;
524 ++Begin;
525 BB->remove(I);
526 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000527
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000528 // Then re-insert them according to the given schedule.
Dan Gohman343f0c02008-11-19 23:18:57 +0000529 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
530 SUnit *SU = Sequence[i];
531 if (!SU) {
532 // Null SUnit* is a noop.
533 EmitNoop();
534 continue;
535 }
536
Dan Gohman47ac0f02009-02-11 04:27:20 +0000537 BB->insert(InsertPos, SU->getInstr());
Dan Gohman343f0c02008-11-19 23:18:57 +0000538 }
539
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000540 // Update the Begin iterator, as the first instruction in the block
541 // may have been scheduled later.
542 if (!Sequence.empty())
543 Begin = Sequence[0]->getInstr();
544
Dan Gohman343f0c02008-11-19 23:18:57 +0000545 return BB;
546}