blob: 955fcc9f57b55d7f57b23f46847cbe39a64854a3 [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 Gohman5034dd32010-12-15 20:02:24 +000019#include "llvm/Analysis/ValueTracking.h"
Dan Gohman3f237442008-12-16 03:25:46 +000020#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohmanc76909a2009-09-25 20:36:54 +000021#include "llvm/CodeGen/MachineMemOperand.h"
Dan Gohman3f237442008-12-16 03:25:46 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman6a9041e2008-12-04 01:35:46 +000023#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chengab8be962011-06-29 01:14:12 +000024#include "llvm/MC/MCInstrItineraries.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000025#include "llvm/Target/TargetMachine.h"
26#include "llvm/Target/TargetInstrInfo.h"
27#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng5b1b44892011-07-01 21:01:15 +000028#include "llvm/Target/TargetSubtargetInfo.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/raw_ostream.h"
Dan Gohman3f237442008-12-16 03:25:46 +000031#include "llvm/ADT/SmallSet.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000032using namespace llvm;
33
Dan Gohman79ce2762009-01-15 19:20:50 +000034ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf,
Dan Gohman3f237442008-12-16 03:25:46 +000035 const MachineLoopInfo &mli,
Andrew Trick5e920d72012-01-14 02:17:12 +000036 const MachineDominatorTree &mdt,
37 bool IsPostRAFlag)
Evan Cheng3ef1c872010-09-10 01:29:16 +000038 : ScheduleDAG(mf), MLI(mli), MDT(mdt), MFI(mf.getFrameInfo()),
Andrew Trick5e920d72012-01-14 02:17:12 +000039 InstrItins(mf.getTarget().getInstrItineraryData()), IsPostRA(IsPostRAFlag),
Andrew Trick7ebcaf42012-01-14 02:17:15 +000040 UnitLatencies(false), Defs(TRI->getNumRegs()), Uses(TRI->getNumRegs()),
Devang Patele29e8e12011-06-02 21:26:52 +000041 LoopRegs(MLI, MDT), FirstDbgValue(0) {
Devang Patelcf4cc842011-06-02 20:07:12 +000042 DbgValues.clear();
Evan Cheng38bdfc62009-10-18 19:58:47 +000043}
Dan Gohman343f0c02008-11-19 23:18:57 +000044
Dan Gohman47ac0f02009-02-11 04:27:20 +000045/// Run - perform scheduling.
46///
47void ScheduleDAGInstrs::Run(MachineBasicBlock *bb,
48 MachineBasicBlock::iterator begin,
49 MachineBasicBlock::iterator end,
50 unsigned endcount) {
51 BB = bb;
52 Begin = begin;
53 InsertPosIndex = endcount;
54
Andrew Trick7ebcaf42012-01-14 02:17:15 +000055 // Check to see if the scheduler cares about latencies.
56 UnitLatencies = ForceUnitLatencies();
57
Dan Gohman47ac0f02009-02-11 04:27:20 +000058 ScheduleDAG::Run(bb, end);
59}
60
Dan Gohman3311a1f2009-01-30 02:49:14 +000061/// getUnderlyingObjectFromInt - This is the function that does the work of
62/// looking through basic ptrtoint+arithmetic+inttoptr sequences.
63static const Value *getUnderlyingObjectFromInt(const Value *V) {
64 do {
Dan Gohman8906f952009-07-17 20:58:59 +000065 if (const Operator *U = dyn_cast<Operator>(V)) {
Dan Gohman3311a1f2009-01-30 02:49:14 +000066 // If we find a ptrtoint, we can transfer control back to the
67 // regular getUnderlyingObjectFromInt.
Dan Gohman8906f952009-07-17 20:58:59 +000068 if (U->getOpcode() == Instruction::PtrToInt)
Dan Gohman3311a1f2009-01-30 02:49:14 +000069 return U->getOperand(0);
70 // If we find an add of a constant or a multiplied value, it's
71 // likely that the other operand will lead us to the base
72 // object. We don't have to worry about the case where the
Dan Gohman748f98f2009-08-07 01:26:06 +000073 // object address is somehow being computed by the multiply,
Dan Gohman3311a1f2009-01-30 02:49:14 +000074 // because our callers only care when the result is an
75 // identifibale object.
Dan Gohman8906f952009-07-17 20:58:59 +000076 if (U->getOpcode() != Instruction::Add ||
Dan Gohman3311a1f2009-01-30 02:49:14 +000077 (!isa<ConstantInt>(U->getOperand(1)) &&
Dan Gohman8906f952009-07-17 20:58:59 +000078 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul))
Dan Gohman3311a1f2009-01-30 02:49:14 +000079 return V;
80 V = U->getOperand(0);
81 } else {
82 return V;
83 }
Duncan Sands1df98592010-02-16 11:11:14 +000084 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
Dan Gohman3311a1f2009-01-30 02:49:14 +000085 } while (1);
86}
87
Dan Gohman5034dd32010-12-15 20:02:24 +000088/// getUnderlyingObject - This is a wrapper around GetUnderlyingObject
Dan Gohman3311a1f2009-01-30 02:49:14 +000089/// and adds support for basic ptrtoint+arithmetic+inttoptr sequences.
90static const Value *getUnderlyingObject(const Value *V) {
91 // First just call Value::getUnderlyingObject to let it do what it does.
92 do {
Dan Gohman5034dd32010-12-15 20:02:24 +000093 V = GetUnderlyingObject(V);
Dan Gohman3311a1f2009-01-30 02:49:14 +000094 // If it found an inttoptr, use special code to continue climing.
Dan Gohman8906f952009-07-17 20:58:59 +000095 if (Operator::getOpcode(V) != Instruction::IntToPtr)
Dan Gohman3311a1f2009-01-30 02:49:14 +000096 break;
97 const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
98 // If that succeeded in finding a pointer, continue the search.
Duncan Sands1df98592010-02-16 11:11:14 +000099 if (!O->getType()->isPointerTy())
Dan Gohman3311a1f2009-01-30 02:49:14 +0000100 break;
101 V = O;
102 } while (1);
103 return V;
104}
105
106/// getUnderlyingObjectForInstr - If this machine instr has memory reference
107/// information and it can be tracked to a normal reference to a known
108/// object, return the Value for that object. Otherwise return null.
Evan Cheng38bdfc62009-10-18 19:58:47 +0000109static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI,
David Goodwina9e61072009-11-03 20:15:00 +0000110 const MachineFrameInfo *MFI,
111 bool &MayAlias) {
112 MayAlias = true;
Dan Gohman3311a1f2009-01-30 02:49:14 +0000113 if (!MI->hasOneMemOperand() ||
Dan Gohmanc76909a2009-09-25 20:36:54 +0000114 !(*MI->memoperands_begin())->getValue() ||
115 (*MI->memoperands_begin())->isVolatile())
Dan Gohman3311a1f2009-01-30 02:49:14 +0000116 return 0;
117
Dan Gohmanc76909a2009-09-25 20:36:54 +0000118 const Value *V = (*MI->memoperands_begin())->getValue();
Dan Gohman3311a1f2009-01-30 02:49:14 +0000119 if (!V)
120 return 0;
121
122 V = getUnderlyingObject(V);
Evan Chengff89dcb2009-10-18 18:16:27 +0000123 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
124 // For now, ignore PseudoSourceValues which may alias LLVM IR values
125 // because the code that uses this function has no way to cope with
126 // such aliases.
Evan Cheng38bdfc62009-10-18 19:58:47 +0000127 if (PSV->isAliased(MFI))
Evan Chengff89dcb2009-10-18 18:16:27 +0000128 return 0;
Andrew Trickf405b1a2011-05-05 19:24:06 +0000129
David Goodwin980d4942009-11-09 19:22:17 +0000130 MayAlias = PSV->mayAlias(MFI);
Evan Chengff89dcb2009-10-18 18:16:27 +0000131 return V;
132 }
Dan Gohman3311a1f2009-01-30 02:49:14 +0000133
Evan Chengff89dcb2009-10-18 18:16:27 +0000134 if (isIdentifiedObject(V))
135 return V;
136
137 return 0;
Dan Gohman3311a1f2009-01-30 02:49:14 +0000138}
139
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000140void ScheduleDAGInstrs::StartBlock(MachineBasicBlock *BB) {
Andrew Tricke8deca82011-10-07 06:33:09 +0000141 LoopRegs.Deps.clear();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000142 if (MachineLoop *ML = MLI.getLoopFor(BB))
Evan Cheng977679d2012-01-07 03:02:36 +0000143 if (BB == ML->getLoopLatch())
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000144 LoopRegs.VisitLoop(ML);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000145}
146
Evan Chengec6906b2010-10-23 02:10:46 +0000147/// AddSchedBarrierDeps - Add dependencies from instructions in the current
148/// list of instructions being scheduled to scheduling barrier by adding
149/// the exit SU to the register defs and use list. This is because we want to
150/// make sure instructions which define registers that are either used by
151/// the terminator or are live-out are properly scheduled. This is
152/// especially important when the definition latency of the return value(s)
153/// are too high to be hidden by the branch or when the liveout registers
154/// used by instructions in the fallthrough block.
155void ScheduleDAGInstrs::AddSchedBarrierDeps() {
156 MachineInstr *ExitMI = InsertPos != BB->end() ? &*InsertPos : 0;
157 ExitSU.setInstr(ExitMI);
158 bool AllDepKnown = ExitMI &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000159 (ExitMI->isCall() || ExitMI->isBarrier());
Evan Chengec6906b2010-10-23 02:10:46 +0000160 if (ExitMI && AllDepKnown) {
161 // If it's a call or a barrier, add dependencies on the defs and uses of
162 // instruction.
163 for (unsigned i = 0, e = ExitMI->getNumOperands(); i != e; ++i) {
164 const MachineOperand &MO = ExitMI->getOperand(i);
165 if (!MO.isReg() || MO.isDef()) continue;
166 unsigned Reg = MO.getReg();
167 if (Reg == 0) continue;
168
169 assert(TRI->isPhysicalRegister(Reg) && "Virtual register encountered!");
170 Uses[Reg].push_back(&ExitSU);
171 }
172 } else {
173 // For others, e.g. fallthrough, conditional branch, assume the exit
Evan Chengde5fa932010-10-27 23:17:17 +0000174 // uses all the registers that are livein to the successor blocks.
175 SmallSet<unsigned, 8> Seen;
176 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
177 SE = BB->succ_end(); SI != SE; ++SI)
178 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
Andrew Trickf405b1a2011-05-05 19:24:06 +0000179 E = (*SI)->livein_end(); I != E; ++I) {
Evan Chengde5fa932010-10-27 23:17:17 +0000180 unsigned Reg = *I;
181 if (Seen.insert(Reg))
182 Uses[Reg].push_back(&ExitSU);
183 }
Evan Chengec6906b2010-10-23 02:10:46 +0000184 }
185}
186
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000187/// addPhysRegDeps - Add register dependencies (data, anti, and output) from
188/// this SUnit to following instructions in the same scheduling region that
189/// depend the physical register referenced at OperIdx.
190void ScheduleDAGInstrs::addPhysRegDeps(SUnit *SU, unsigned OperIdx) {
191 const MachineInstr *MI = SU->getInstr();
192 const MachineOperand &MO = MI->getOperand(OperIdx);
193 unsigned Reg = MO.getReg();
194
195 // Ask the target if address-backscheduling is desirable, and if so how much.
196 const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>();
197 unsigned SpecialAddressLatency = ST.getSpecialAddressLatency();
198
199 // Optionally add output and anti dependencies. For anti
200 // dependencies we use a latency of 0 because for a multi-issue
201 // target we want to allow the defining instruction to issue
202 // in the same cycle as the using instruction.
203 // TODO: Using a latency of 1 here for output dependencies assumes
204 // there's no cost for reusing registers.
205 SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
206 for (const unsigned *Alias = TRI->getOverlaps(Reg); *Alias; ++Alias) {
207 std::vector<SUnit *> &DefList = Defs[*Alias];
208 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
209 SUnit *DefSU = DefList[i];
210 if (DefSU == &ExitSU)
211 continue;
212 if (DefSU != SU &&
213 (Kind != SDep::Output || !MO.isDead() ||
214 !DefSU->getInstr()->registerDefIsDead(*Alias))) {
215 if (Kind == SDep::Anti)
216 DefSU->addPred(SDep(SU, Kind, 0, /*Reg=*/*Alias));
217 else {
218 unsigned AOLat = TII->getOutputLatency(InstrItins, MI, OperIdx,
219 DefSU->getInstr());
220 DefSU->addPred(SDep(SU, Kind, AOLat, /*Reg=*/*Alias));
221 }
222 }
223 }
224 }
225
226 // Retrieve the UseList to add data dependencies and update uses.
227 std::vector<SUnit *> &UseList = Uses[Reg];
228 if (MO.isDef()) {
229 // Update DefList. Defs are pushed in the order they are visited and
230 // never reordered.
231 std::vector<SUnit *> &DefList = Defs[Reg];
232
233 // Add any data dependencies.
234 unsigned DataLatency = SU->Latency;
235 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
236 SUnit *UseSU = UseList[i];
237 if (UseSU == SU)
238 continue;
239 unsigned LDataLatency = DataLatency;
240 // Optionally add in a special extra latency for nodes that
241 // feed addresses.
242 // TODO: Do this for register aliases too.
243 // TODO: Perhaps we should get rid of
244 // SpecialAddressLatency and just move this into
245 // adjustSchedDependency for the targets that care about it.
246 if (SpecialAddressLatency != 0 && !UnitLatencies &&
247 UseSU != &ExitSU) {
248 MachineInstr *UseMI = UseSU->getInstr();
249 const MCInstrDesc &UseMCID = UseMI->getDesc();
250 int RegUseIndex = UseMI->findRegisterUseOperandIdx(Reg);
251 assert(RegUseIndex >= 0 && "UseMI doesn's use register!");
252 if (RegUseIndex >= 0 &&
253 (UseMI->mayLoad() || UseMI->mayStore()) &&
254 (unsigned)RegUseIndex < UseMCID.getNumOperands() &&
255 UseMCID.OpInfo[RegUseIndex].isLookupPtrRegClass())
256 LDataLatency += SpecialAddressLatency;
257 }
258 // Adjust the dependence latency using operand def/use
259 // information (if any), and then allow the target to
260 // perform its own adjustments.
261 const SDep& dep = SDep(SU, SDep::Data, LDataLatency, Reg);
262 if (!UnitLatencies) {
263 ComputeOperandLatency(SU, UseSU, const_cast<SDep &>(dep));
264 ST.adjustSchedDependency(SU, UseSU, const_cast<SDep &>(dep));
265 }
266 UseSU->addPred(dep);
267 }
268 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
269 std::vector<SUnit *> &UseList = Uses[*Alias];
270 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
271 SUnit *UseSU = UseList[i];
272 if (UseSU == SU)
273 continue;
274 const SDep& dep = SDep(SU, SDep::Data, DataLatency, *Alias);
275 if (!UnitLatencies) {
276 ComputeOperandLatency(SU, UseSU, const_cast<SDep &>(dep));
277 ST.adjustSchedDependency(SU, UseSU, const_cast<SDep &>(dep));
278 }
279 UseSU->addPred(dep);
280 }
281 }
282
283 // If a def is going to wrap back around to the top of the loop,
284 // backschedule it.
285 if (!UnitLatencies && DefList.empty()) {
286 LoopDependencies::LoopDeps::iterator I = LoopRegs.Deps.find(Reg);
287 if (I != LoopRegs.Deps.end()) {
288 const MachineOperand *UseMO = I->second.first;
289 unsigned Count = I->second.second;
290 const MachineInstr *UseMI = UseMO->getParent();
291 unsigned UseMOIdx = UseMO - &UseMI->getOperand(0);
292 const MCInstrDesc &UseMCID = UseMI->getDesc();
293 // TODO: If we knew the total depth of the region here, we could
294 // handle the case where the whole loop is inside the region but
295 // is large enough that the isScheduleHigh trick isn't needed.
296 if (UseMOIdx < UseMCID.getNumOperands()) {
297 // Currently, we only support scheduling regions consisting of
298 // single basic blocks. Check to see if the instruction is in
299 // the same region by checking to see if it has the same parent.
300 if (UseMI->getParent() != MI->getParent()) {
301 unsigned Latency = SU->Latency;
302 if (UseMCID.OpInfo[UseMOIdx].isLookupPtrRegClass())
303 Latency += SpecialAddressLatency;
304 // This is a wild guess as to the portion of the latency which
305 // will be overlapped by work done outside the current
306 // scheduling region.
307 Latency -= std::min(Latency, Count);
308 // Add the artificial edge.
309 ExitSU.addPred(SDep(SU, SDep::Order, Latency,
310 /*Reg=*/0, /*isNormalMemory=*/false,
311 /*isMustAlias=*/false,
312 /*isArtificial=*/true));
313 } else if (SpecialAddressLatency > 0 &&
314 UseMCID.OpInfo[UseMOIdx].isLookupPtrRegClass()) {
315 // The entire loop body is within the current scheduling region
316 // and the latency of this operation is assumed to be greater
317 // than the latency of the loop.
318 // TODO: Recursively mark data-edge predecessors as
319 // isScheduleHigh too.
320 SU->isScheduleHigh = true;
321 }
322 }
323 LoopRegs.Deps.erase(I);
324 }
325 }
326
327 UseList.clear();
328 if (!MO.isDead())
329 DefList.clear();
330
331 // Calls will not be reordered because of chain dependencies (see
332 // below). Since call operands are dead, calls may continue to be added
333 // to the DefList making dependence checking quadratic in the size of
334 // the block. Instead, we leave only one call at the back of the
335 // DefList.
336 if (SU->isCall) {
337 while (!DefList.empty() && DefList.back()->isCall)
338 DefList.pop_back();
339 }
340 DefList.push_back(SU);
341 } else {
342 UseList.push_back(SU);
343 }
344}
345
346/// addVirtRegDeps - Add register dependencies (data, anti, and output) from
347/// this SUnit to following instructions in the same scheduling region that
348/// depend the virtual register referenced at OperIdx.
349void ScheduleDAGInstrs::addVirtRegDeps(SUnit *SU, unsigned OperIdx) {
350 assert(false && "unimplemented");
351}
352
Dan Gohmana70dca12009-10-09 23:27:56 +0000353void ScheduleDAGInstrs::BuildSchedGraph(AliasAnalysis *AA) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000354 // We'll be allocating one SUnit for each instruction, plus one for
355 // the region exit node.
Dan Gohman343f0c02008-11-19 23:18:57 +0000356 SUnits.reserve(BB->size());
357
Dan Gohman6a9041e2008-12-04 01:35:46 +0000358 // We build scheduling units by walking a block's instruction list from bottom
359 // to top.
360
David Goodwin980d4942009-11-09 19:22:17 +0000361 // Remember where a generic side-effecting instruction is as we procede.
362 SUnit *BarrierChain = 0, *AliasChain = 0;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000363
David Goodwin980d4942009-11-09 19:22:17 +0000364 // Memory references to specific known memory locations are tracked
365 // so that they can be given more precise dependencies. We track
366 // separately the known memory locations that may alias and those
367 // that are known not to alias
368 std::map<const Value *, SUnit *> AliasMemDefs, NonAliasMemDefs;
369 std::map<const Value *, std::vector<SUnit *> > AliasMemUses, NonAliasMemUses;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000370
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000371 // Remove any stale debug info; sometimes BuildSchedGraph is called again
372 // without emitting the info from the previous call.
Devang Patelcf4cc842011-06-02 20:07:12 +0000373 DbgValues.clear();
374 FirstDbgValue = NULL;
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000375
Evan Chengec6906b2010-10-23 02:10:46 +0000376 // Model data dependencies between instructions being scheduled and the
377 // ExitSU.
378 AddSchedBarrierDeps();
379
Andrew Trick9b668532011-05-06 21:52:52 +0000380 for (int i = 0, e = TRI->getNumRegs(); i != e; ++i) {
381 assert(Defs[i].empty() && "Only BuildGraph should push/pop Defs");
382 }
383
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000384 // Walk the list of instructions, from bottom moving up.
Devang Patelcf4cc842011-06-02 20:07:12 +0000385 MachineInstr *PrevMI = NULL;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000386 for (MachineBasicBlock::iterator MII = InsertPos, MIE = Begin;
Dan Gohman343f0c02008-11-19 23:18:57 +0000387 MII != MIE; --MII) {
388 MachineInstr *MI = prior(MII);
Devang Patelcf4cc842011-06-02 20:07:12 +0000389 if (MI && PrevMI) {
390 DbgValues.push_back(std::make_pair(PrevMI, MI));
391 PrevMI = NULL;
392 }
393
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000394 if (MI->isDebugValue()) {
Devang Patelcf4cc842011-06-02 20:07:12 +0000395 PrevMI = MI;
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000396 continue;
397 }
Devang Patelcf4cc842011-06-02 20:07:12 +0000398
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000399 assert(!MI->isTerminator() && !MI->isLabel() &&
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000400 "Cannot schedule terminators or labels!");
401 // Create the SUnit for this MI.
Dan Gohman343f0c02008-11-19 23:18:57 +0000402 SUnit *SU = NewSUnit(MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000403 SU->isCall = MI->isCall();
404 SU->isCommutable = MI->isCommutable();
Dan Gohman343f0c02008-11-19 23:18:57 +0000405
Dan Gohman54e4c362008-12-09 22:54:47 +0000406 // Assign the Latency field of SU using target-provided information.
Dan Gohman3f237442008-12-16 03:25:46 +0000407 if (UnitLatencies)
408 SU->Latency = 1;
409 else
410 ComputeLatency(SU);
Dan Gohman54e4c362008-12-09 22:54:47 +0000411
Dan Gohman6a9041e2008-12-04 01:35:46 +0000412 // Add register-based dependencies (data, anti, and output).
Dan Gohman343f0c02008-11-19 23:18:57 +0000413 for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) {
414 const MachineOperand &MO = MI->getOperand(j);
415 if (!MO.isReg()) continue;
416 unsigned Reg = MO.getReg();
417 if (Reg == 0) continue;
418
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000419 if (TRI->isPhysicalRegister(Reg))
420 addPhysRegDeps(SU, j);
421 else {
422 assert(!IsPostRA && "Virtual register encountered!");
423 addVirtRegDeps(SU, j);
Dan Gohman343f0c02008-11-19 23:18:57 +0000424 }
425 }
Dan Gohman6a9041e2008-12-04 01:35:46 +0000426
427 // Add chain dependencies.
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000428 // Chain dependencies used to enforce memory order should have
429 // latency of 0 (except for true dependency of Store followed by
430 // aliased Load... we estimate that with a single cycle of latency
431 // assuming the hardware will bypass)
Dan Gohman6a9041e2008-12-04 01:35:46 +0000432 // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable
433 // after stack slots are lowered to actual addresses.
434 // TODO: Use an AliasAnalysis and do real alias-analysis queries, and
435 // produce more precise dependence information.
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000436#define STORE_LOAD_LATENCY 1
437 unsigned TrueMemOrderLatency = 0;
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000438 if (MI->isCall() || MI->hasUnmodeledSideEffects() ||
Andrew Trickf405b1a2011-05-05 19:24:06 +0000439 (MI->hasVolatileMemoryRef() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000440 (!MI->mayLoad() || !MI->isInvariantLoad(AA)))) {
David Goodwin980d4942009-11-09 19:22:17 +0000441 // Be conservative with these and add dependencies on all memory
442 // references, even those that are known to not alias.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000443 for (std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000444 NonAliasMemDefs.begin(), E = NonAliasMemDefs.end(); I != E; ++I) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000445 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000446 }
447 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000448 NonAliasMemUses.begin(), E = NonAliasMemUses.end(); I != E; ++I) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000449 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000450 I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000451 }
David Goodwin980d4942009-11-09 19:22:17 +0000452 NonAliasMemDefs.clear();
453 NonAliasMemUses.clear();
454 // Add SU to the barrier chain.
455 if (BarrierChain)
456 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
457 BarrierChain = SU;
458
459 // fall-through
460 new_alias_chain:
461 // Chain all possibly aliasing memory references though SU.
462 if (AliasChain)
463 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
464 AliasChain = SU;
465 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
466 PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
467 for (std::map<const Value *, SUnit *>::iterator I = AliasMemDefs.begin(),
468 E = AliasMemDefs.end(); I != E; ++I) {
469 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
470 }
471 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
472 AliasMemUses.begin(), E = AliasMemUses.end(); I != E; ++I) {
473 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
474 I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
475 }
476 PendingLoads.clear();
477 AliasMemDefs.clear();
478 AliasMemUses.clear();
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000479 } else if (MI->mayStore()) {
David Goodwina9e61072009-11-03 20:15:00 +0000480 bool MayAlias = true;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000481 TrueMemOrderLatency = STORE_LOAD_LATENCY;
David Goodwina9e61072009-11-03 20:15:00 +0000482 if (const Value *V = getUnderlyingObjectForInstr(MI, MFI, MayAlias)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000483 // A store to a specific PseudoSourceValue. Add precise dependencies.
David Goodwin980d4942009-11-09 19:22:17 +0000484 // Record the def in MemDefs, first adding a dep if there is
485 // an existing def.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000486 std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000487 ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000488 std::map<const Value *, SUnit *>::iterator IE =
David Goodwin980d4942009-11-09 19:22:17 +0000489 ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end());
490 if (I != IE) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000491 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0,
Dan Gohman54e4c362008-12-09 22:54:47 +0000492 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000493 I->second = SU;
494 } else {
David Goodwin980d4942009-11-09 19:22:17 +0000495 if (MayAlias)
496 AliasMemDefs[V] = SU;
497 else
498 NonAliasMemDefs[V] = SU;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000499 }
500 // Handle the uses in MemUses, if there are any.
Dan Gohmana629b482008-12-08 17:50:35 +0000501 std::map<const Value *, std::vector<SUnit *> >::iterator J =
David Goodwin980d4942009-11-09 19:22:17 +0000502 ((MayAlias) ? AliasMemUses.find(V) : NonAliasMemUses.find(V));
503 std::map<const Value *, std::vector<SUnit *> >::iterator JE =
504 ((MayAlias) ? AliasMemUses.end() : NonAliasMemUses.end());
505 if (J != JE) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000506 for (unsigned i = 0, e = J->second.size(); i != e; ++i)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000507 J->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency,
508 /*Reg=*/0, /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000509 J->second.clear();
510 }
David Goodwina9e61072009-11-03 20:15:00 +0000511 if (MayAlias) {
David Goodwin980d4942009-11-09 19:22:17 +0000512 // Add dependencies from all the PendingLoads, i.e. loads
513 // with no underlying object.
David Goodwina9e61072009-11-03 20:15:00 +0000514 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
515 PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
David Goodwin980d4942009-11-09 19:22:17 +0000516 // Add dependence on alias chain, if needed.
517 if (AliasChain)
518 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
David Goodwina9e61072009-11-03 20:15:00 +0000519 }
David Goodwin980d4942009-11-09 19:22:17 +0000520 // Add dependence on barrier chain, if needed.
521 if (BarrierChain)
522 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
David Goodwin5be870a2009-11-05 00:16:44 +0000523 } else {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000524 // Treat all other stores conservatively.
David Goodwin980d4942009-11-09 19:22:17 +0000525 goto new_alias_chain;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000526 }
Evan Chengec6906b2010-10-23 02:10:46 +0000527
528 if (!ExitSU.isPred(SU))
529 // Push store's up a bit to avoid them getting in between cmp
530 // and branches.
531 ExitSU.addPred(SDep(SU, SDep::Order, 0,
532 /*Reg=*/0, /*isNormalMemory=*/false,
533 /*isMustAlias=*/false,
534 /*isArtificial=*/true));
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000535 } else if (MI->mayLoad()) {
David Goodwina9e61072009-11-03 20:15:00 +0000536 bool MayAlias = true;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000537 TrueMemOrderLatency = 0;
Dan Gohmana70dca12009-10-09 23:27:56 +0000538 if (MI->isInvariantLoad(AA)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000539 // Invariant load, no chain dependencies needed!
David Goodwin5be870a2009-11-05 00:16:44 +0000540 } else {
Andrew Trickf405b1a2011-05-05 19:24:06 +0000541 if (const Value *V =
David Goodwin980d4942009-11-09 19:22:17 +0000542 getUnderlyingObjectForInstr(MI, MFI, MayAlias)) {
543 // A load from a specific PseudoSourceValue. Add precise dependencies.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000544 std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000545 ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000546 std::map<const Value *, SUnit *>::iterator IE =
David Goodwin980d4942009-11-09 19:22:17 +0000547 ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end());
548 if (I != IE)
549 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0,
550 /*isNormalMemory=*/true));
551 if (MayAlias)
552 AliasMemUses[V].push_back(SU);
Andrew Trickf405b1a2011-05-05 19:24:06 +0000553 else
David Goodwin980d4942009-11-09 19:22:17 +0000554 NonAliasMemUses[V].push_back(SU);
555 } else {
556 // A load with no underlying object. Depend on all
557 // potentially aliasing stores.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000558 for (std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000559 AliasMemDefs.begin(), E = AliasMemDefs.end(); I != E; ++I)
560 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000561
David Goodwin980d4942009-11-09 19:22:17 +0000562 PendingLoads.push_back(SU);
563 MayAlias = true;
David Goodwina9e61072009-11-03 20:15:00 +0000564 }
Andrew Trickf405b1a2011-05-05 19:24:06 +0000565
David Goodwin980d4942009-11-09 19:22:17 +0000566 // Add dependencies on alias and barrier chains, if needed.
567 if (MayAlias && AliasChain)
568 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
569 if (BarrierChain)
570 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000571 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000572 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000573 }
Devang Patelcf4cc842011-06-02 20:07:12 +0000574 if (PrevMI)
575 FirstDbgValue = PrevMI;
Dan Gohman79ce2762009-01-15 19:20:50 +0000576
577 for (int i = 0, e = TRI->getNumRegs(); i != e; ++i) {
578 Defs[i].clear();
579 Uses[i].clear();
580 }
581 PendingLoads.clear();
Dan Gohman343f0c02008-11-19 23:18:57 +0000582}
583
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000584void ScheduleDAGInstrs::FinishBlock() {
585 // Nothing to do.
586}
587
Dan Gohmanc8c28272008-11-21 00:12:10 +0000588void ScheduleDAGInstrs::ComputeLatency(SUnit *SU) {
David Goodwind94a4e52009-08-10 15:55:25 +0000589 // Compute the latency for the node.
Evan Cheng3ef1c872010-09-10 01:29:16 +0000590 if (!InstrItins || InstrItins->isEmpty()) {
591 SU->Latency = 1;
Dan Gohman4ea8e852008-12-16 02:38:22 +0000592
Evan Cheng3ef1c872010-09-10 01:29:16 +0000593 // Simplistic target-independent heuristic: assume that loads take
594 // extra time.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000595 if (SU->getInstr()->mayLoad())
Dan Gohman4ea8e852008-12-16 02:38:22 +0000596 SU->Latency += 2;
Evan Cheng8239daf2010-11-03 00:45:17 +0000597 } else {
598 SU->Latency = TII->getInstrLatency(InstrItins, SU->getInstr());
599 }
Dan Gohmanc8c28272008-11-21 00:12:10 +0000600}
601
Andrew Trickf405b1a2011-05-05 19:24:06 +0000602void ScheduleDAGInstrs::ComputeOperandLatency(SUnit *Def, SUnit *Use,
David Goodwindc4bdcd2009-08-19 16:08:58 +0000603 SDep& dep) const {
Evan Cheng3ef1c872010-09-10 01:29:16 +0000604 if (!InstrItins || InstrItins->isEmpty())
David Goodwindc4bdcd2009-08-19 16:08:58 +0000605 return;
Andrew Trickf405b1a2011-05-05 19:24:06 +0000606
David Goodwindc4bdcd2009-08-19 16:08:58 +0000607 // For a data dependency with a known register...
608 if ((dep.getKind() != SDep::Data) || (dep.getReg() == 0))
609 return;
610
611 const unsigned Reg = dep.getReg();
612
613 // ... find the definition of the register in the defining
614 // instruction
615 MachineInstr *DefMI = Def->getInstr();
616 int DefIdx = DefMI->findRegisterDefOperandIdx(Reg);
617 if (DefIdx != -1) {
Evan Cheng1aca5bc2010-10-08 18:42:25 +0000618 const MachineOperand &MO = DefMI->getOperand(DefIdx);
619 if (MO.isReg() && MO.isImplicit() &&
Evan Chengd82de832010-10-08 23:01:57 +0000620 DefIdx >= (int)DefMI->getDesc().getNumOperands()) {
Evan Cheng1aca5bc2010-10-08 18:42:25 +0000621 // This is an implicit def, getOperandLatency() won't return the correct
622 // latency. e.g.
623 // %D6<def>, %D7<def> = VLD1q16 %R2<kill>, 0, ..., %Q3<imp-def>
624 // %Q1<def> = VMULv8i16 %Q1<kill>, %Q3<kill>, ...
625 // What we want is to compute latency between def of %D6/%D7 and use of
626 // %Q3 instead.
627 DefIdx = DefMI->findRegisterDefOperandIdx(Reg, false, true, TRI);
628 }
Evan Chenga0792de2010-10-06 06:27:31 +0000629 MachineInstr *UseMI = Use->getInstr();
Evan Cheng3881cb72010-09-29 22:42:35 +0000630 // For all uses of the register, calculate the maxmimum latency
631 int Latency = -1;
Evan Chengec6906b2010-10-23 02:10:46 +0000632 if (UseMI) {
633 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
634 const MachineOperand &MO = UseMI->getOperand(i);
635 if (!MO.isReg() || !MO.isUse())
636 continue;
637 unsigned MOReg = MO.getReg();
638 if (MOReg != Reg)
639 continue;
David Goodwindc4bdcd2009-08-19 16:08:58 +0000640
Evan Chengec6906b2010-10-23 02:10:46 +0000641 int UseCycle = TII->getOperandLatency(InstrItins, DefMI, DefIdx,
642 UseMI, i);
643 Latency = std::max(Latency, UseCycle);
644 }
645 } else {
646 // UseMI is null, then it must be a scheduling barrier.
647 if (!InstrItins || InstrItins->isEmpty())
648 return;
649 unsigned DefClass = DefMI->getDesc().getSchedClass();
650 Latency = InstrItins->getOperandCycle(DefClass, DefIdx);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000651 }
Evan Chengec6906b2010-10-23 02:10:46 +0000652
653 // If we found a latency, then replace the existing dependence latency.
654 if (Latency >= 0)
655 dep.setLatency(Latency);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000656 }
657}
658
Dan Gohman343f0c02008-11-19 23:18:57 +0000659void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
660 SU->getInstr()->dump();
661}
662
663std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
664 std::string s;
665 raw_string_ostream oss(s);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000666 if (SU == &EntrySU)
667 oss << "<entry>";
668 else if (SU == &ExitSU)
669 oss << "<exit>";
670 else
671 SU->getInstr()->print(oss);
Dan Gohman343f0c02008-11-19 23:18:57 +0000672 return oss.str();
673}
674
675// EmitSchedule - Emit the machine code in scheduled order.
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000676MachineBasicBlock *ScheduleDAGInstrs::EmitSchedule() {
Evan Chengddfd1372011-12-14 02:11:42 +0000677 Begin = InsertPos;
Dan Gohman343f0c02008-11-19 23:18:57 +0000678
Devang Patelcf4cc842011-06-02 20:07:12 +0000679 // If first instruction was a DBG_VALUE then put it back.
680 if (FirstDbgValue)
Evan Chengddfd1372011-12-14 02:11:42 +0000681 BB->splice(InsertPos, BB, FirstDbgValue);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000682
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000683 // Then re-insert them according to the given schedule.
Dan Gohman343f0c02008-11-19 23:18:57 +0000684 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
Devang Patelee1f8782011-06-02 21:31:00 +0000685 if (SUnit *SU = Sequence[i])
Evan Chengddfd1372011-12-14 02:11:42 +0000686 BB->splice(InsertPos, BB, SU->getInstr());
Devang Patelee1f8782011-06-02 21:31:00 +0000687 else
Dan Gohman343f0c02008-11-19 23:18:57 +0000688 // Null SUnit* is a noop.
689 EmitNoop();
Dan Gohman343f0c02008-11-19 23:18:57 +0000690
Hal Finkeldb809e02011-12-02 04:58:07 +0000691 // Update the Begin iterator, as the first instruction in the block
692 // may have been scheduled later.
693 if (i == 0)
694 Begin = prior(InsertPos);
695 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000696
Devang Patelcf4cc842011-06-02 20:07:12 +0000697 // Reinsert any remaining debug_values.
698 for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator
699 DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
700 std::pair<MachineInstr *, MachineInstr *> P = *prior(DI);
701 MachineInstr *DbgValue = P.first;
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000702 MachineBasicBlock::iterator OrigPrivMI = P.second;
Evan Chengddfd1372011-12-14 02:11:42 +0000703 BB->splice(++OrigPrivMI, BB, DbgValue);
Devang Patelcf4cc842011-06-02 20:07:12 +0000704 }
705 DbgValues.clear();
706 FirstDbgValue = NULL;
Dan Gohman343f0c02008-11-19 23:18:57 +0000707 return BB;
708}