blob: 1c455b95ab004c3545f4a899c5f85a0dfe98407f [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 Gohman8906f952009-07-17 20:58:59 +000016#include "llvm/Operator.h"
Dan Gohman3311a1f2009-01-30 02:49:14 +000017#include "llvm/Analysis/AliasAnalysis.h"
Dan Gohman5034dd32010-12-15 20:02:24 +000018#include "llvm/Analysis/ValueTracking.h"
Andrew Trickb4566a92012-02-22 06:08:11 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.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"
Andrew Tricked395c82012-03-07 23:01:06 +000024#include "llvm/CodeGen/ScheduleDAGInstrs.h"
Evan Chengab8be962011-06-29 01:14:12 +000025#include "llvm/MC/MCInstrItineraries.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000026#include "llvm/Target/TargetMachine.h"
27#include "llvm/Target/TargetInstrInfo.h"
28#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng5b1b44892011-07-01 21:01:15 +000029#include "llvm/Target/TargetSubtargetInfo.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000030#include "llvm/Support/Debug.h"
31#include "llvm/Support/raw_ostream.h"
Dan Gohman3f237442008-12-16 03:25:46 +000032#include "llvm/ADT/SmallSet.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000033using namespace llvm;
34
Dan Gohman79ce2762009-01-15 19:20:50 +000035ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf,
Dan Gohman3f237442008-12-16 03:25:46 +000036 const MachineLoopInfo &mli,
Andrew Trick5e920d72012-01-14 02:17:12 +000037 const MachineDominatorTree &mdt,
Andrew Trickb4566a92012-02-22 06:08:11 +000038 bool IsPostRAFlag,
39 LiveIntervals *lis)
Evan Cheng3ef1c872010-09-10 01:29:16 +000040 : ScheduleDAG(mf), MLI(mli), MDT(mdt), MFI(mf.getFrameInfo()),
Andrew Trickd790cad2012-03-07 23:00:59 +000041 InstrItins(mf.getTarget().getInstrItineraryData()), LIS(lis),
42 IsPostRA(IsPostRAFlag), UnitLatencies(false), LoopRegs(MLI, MDT),
43 FirstDbgValue(0) {
Andrew Trickb4566a92012-02-22 06:08:11 +000044 assert((IsPostRA || LIS) && "PreRA scheduling requires LiveIntervals");
Devang Patelcf4cc842011-06-02 20:07:12 +000045 DbgValues.clear();
Andrew Trickcc77b542012-02-22 06:08:13 +000046 assert(!(IsPostRA && MRI.getNumVirtRegs()) &&
Andrew Trick19273ae2012-02-21 04:51:23 +000047 "Virtual registers must be removed prior to PostRA scheduling");
Evan Cheng38bdfc62009-10-18 19:58:47 +000048}
Dan Gohman343f0c02008-11-19 23:18:57 +000049
Dan Gohman3311a1f2009-01-30 02:49:14 +000050/// getUnderlyingObjectFromInt - This is the function that does the work of
51/// looking through basic ptrtoint+arithmetic+inttoptr sequences.
52static const Value *getUnderlyingObjectFromInt(const Value *V) {
53 do {
Dan Gohman8906f952009-07-17 20:58:59 +000054 if (const Operator *U = dyn_cast<Operator>(V)) {
Dan Gohman3311a1f2009-01-30 02:49:14 +000055 // If we find a ptrtoint, we can transfer control back to the
56 // regular getUnderlyingObjectFromInt.
Dan Gohman8906f952009-07-17 20:58:59 +000057 if (U->getOpcode() == Instruction::PtrToInt)
Dan Gohman3311a1f2009-01-30 02:49:14 +000058 return U->getOperand(0);
59 // If we find an add of a constant or a multiplied value, it's
60 // likely that the other operand will lead us to the base
61 // object. We don't have to worry about the case where the
Dan Gohman748f98f2009-08-07 01:26:06 +000062 // object address is somehow being computed by the multiply,
Dan Gohman3311a1f2009-01-30 02:49:14 +000063 // because our callers only care when the result is an
64 // identifibale object.
Dan Gohman8906f952009-07-17 20:58:59 +000065 if (U->getOpcode() != Instruction::Add ||
Dan Gohman3311a1f2009-01-30 02:49:14 +000066 (!isa<ConstantInt>(U->getOperand(1)) &&
Dan Gohman8906f952009-07-17 20:58:59 +000067 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul))
Dan Gohman3311a1f2009-01-30 02:49:14 +000068 return V;
69 V = U->getOperand(0);
70 } else {
71 return V;
72 }
Duncan Sands1df98592010-02-16 11:11:14 +000073 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
Dan Gohman3311a1f2009-01-30 02:49:14 +000074 } while (1);
75}
76
Dan Gohman5034dd32010-12-15 20:02:24 +000077/// getUnderlyingObject - This is a wrapper around GetUnderlyingObject
Dan Gohman3311a1f2009-01-30 02:49:14 +000078/// and adds support for basic ptrtoint+arithmetic+inttoptr sequences.
79static const Value *getUnderlyingObject(const Value *V) {
80 // First just call Value::getUnderlyingObject to let it do what it does.
81 do {
Dan Gohman5034dd32010-12-15 20:02:24 +000082 V = GetUnderlyingObject(V);
Dan Gohman3311a1f2009-01-30 02:49:14 +000083 // If it found an inttoptr, use special code to continue climing.
Dan Gohman8906f952009-07-17 20:58:59 +000084 if (Operator::getOpcode(V) != Instruction::IntToPtr)
Dan Gohman3311a1f2009-01-30 02:49:14 +000085 break;
86 const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
87 // If that succeeded in finding a pointer, continue the search.
Duncan Sands1df98592010-02-16 11:11:14 +000088 if (!O->getType()->isPointerTy())
Dan Gohman3311a1f2009-01-30 02:49:14 +000089 break;
90 V = O;
91 } while (1);
92 return V;
93}
94
95/// getUnderlyingObjectForInstr - If this machine instr has memory reference
96/// information and it can be tracked to a normal reference to a known
97/// object, return the Value for that object. Otherwise return null.
Evan Cheng38bdfc62009-10-18 19:58:47 +000098static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI,
David Goodwina9e61072009-11-03 20:15:00 +000099 const MachineFrameInfo *MFI,
100 bool &MayAlias) {
101 MayAlias = true;
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;
Andrew Trickf405b1a2011-05-05 19:24:06 +0000118
David Goodwin980d4942009-11-09 19:22:17 +0000119 MayAlias = PSV->mayAlias(MFI);
Evan Chengff89dcb2009-10-18 18:16:27 +0000120 return V;
121 }
Dan Gohman3311a1f2009-01-30 02:49:14 +0000122
Evan Chengff89dcb2009-10-18 18:16:27 +0000123 if (isIdentifiedObject(V))
124 return V;
125
126 return 0;
Dan Gohman3311a1f2009-01-30 02:49:14 +0000127}
128
Andrew Trick953be892012-03-07 23:00:49 +0000129void ScheduleDAGInstrs::startBlock(MachineBasicBlock *BB) {
Andrew Tricke8deca82011-10-07 06:33:09 +0000130 LoopRegs.Deps.clear();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000131 if (MachineLoop *ML = MLI.getLoopFor(BB))
Evan Cheng977679d2012-01-07 03:02:36 +0000132 if (BB == ML->getLoopLatch())
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000133 LoopRegs.VisitLoop(ML);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000134}
135
Andrew Trick953be892012-03-07 23:00:49 +0000136void ScheduleDAGInstrs::finishBlock() {
Andrew Trick47c14452012-03-07 05:21:52 +0000137 // Nothing to do.
138}
139
Andrew Trick702d4892012-02-24 07:04:55 +0000140/// Initialize the map with the number of registers.
Andrew Trick035ec402012-03-07 23:00:57 +0000141void Reg2SUnitsMap::setRegLimit(unsigned Limit) {
Andrew Trick702d4892012-02-24 07:04:55 +0000142 PhysRegSet.setUniverse(Limit);
143 SUnits.resize(Limit);
144}
145
146/// Clear the map without deallocating storage.
Andrew Trick035ec402012-03-07 23:00:57 +0000147void Reg2SUnitsMap::clear() {
Andrew Trick702d4892012-02-24 07:04:55 +0000148 for (const_iterator I = reg_begin(), E = reg_end(); I != E; ++I) {
149 SUnits[*I].clear();
150 }
151 PhysRegSet.clear();
152}
153
Andrew Trick47c14452012-03-07 05:21:52 +0000154/// Initialize the DAG and common scheduler state for the current scheduling
155/// region. This does not actually create the DAG, only clears it. The
156/// scheduling driver may call BuildSchedGraph multiple times per scheduling
157/// region.
158void ScheduleDAGInstrs::enterRegion(MachineBasicBlock *bb,
159 MachineBasicBlock::iterator begin,
160 MachineBasicBlock::iterator end,
161 unsigned endcount) {
162 BB = bb;
Andrew Trick68675c62012-03-09 04:29:02 +0000163 RegionBegin = begin;
164 RegionEnd = end;
Andrew Trickcf46b5a2012-03-07 23:00:52 +0000165 EndIndex = endcount;
Andrew Trick17d35e52012-03-14 04:00:41 +0000166 MISUnitMap.clear();
Andrew Trick47c14452012-03-07 05:21:52 +0000167
168 // Check to see if the scheduler cares about latencies.
Andrew Trick953be892012-03-07 23:00:49 +0000169 UnitLatencies = forceUnitLatencies();
Andrew Trick47c14452012-03-07 05:21:52 +0000170
171 ScheduleDAG::clearDAG();
172}
173
174/// Close the current scheduling region. Don't clear any state in case the
175/// driver wants to refer to the previous scheduling region.
176void ScheduleDAGInstrs::exitRegion() {
177 // Nothing to do.
178}
179
Andrew Trick953be892012-03-07 23:00:49 +0000180/// addSchedBarrierDeps - Add dependencies from instructions in the current
Evan Chengec6906b2010-10-23 02:10:46 +0000181/// list of instructions being scheduled to scheduling barrier by adding
182/// the exit SU to the register defs and use list. This is because we want to
183/// make sure instructions which define registers that are either used by
184/// the terminator or are live-out are properly scheduled. This is
185/// especially important when the definition latency of the return value(s)
186/// are too high to be hidden by the branch or when the liveout registers
187/// used by instructions in the fallthrough block.
Andrew Trick953be892012-03-07 23:00:49 +0000188void ScheduleDAGInstrs::addSchedBarrierDeps() {
Andrew Trick68675c62012-03-09 04:29:02 +0000189 MachineInstr *ExitMI = RegionEnd != BB->end() ? &*RegionEnd : 0;
Evan Chengec6906b2010-10-23 02:10:46 +0000190 ExitSU.setInstr(ExitMI);
191 bool AllDepKnown = ExitMI &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000192 (ExitMI->isCall() || ExitMI->isBarrier());
Evan Chengec6906b2010-10-23 02:10:46 +0000193 if (ExitMI && AllDepKnown) {
194 // If it's a call or a barrier, add dependencies on the defs and uses of
195 // instruction.
196 for (unsigned i = 0, e = ExitMI->getNumOperands(); i != e; ++i) {
197 const MachineOperand &MO = ExitMI->getOperand(i);
198 if (!MO.isReg() || MO.isDef()) continue;
199 unsigned Reg = MO.getReg();
200 if (Reg == 0) continue;
201
Andrew Trick3c58ba82012-01-14 02:17:18 +0000202 if (TRI->isPhysicalRegister(Reg))
Andrew Trick702d4892012-02-24 07:04:55 +0000203 Uses[Reg].push_back(&ExitSU);
Andrew Trickd3a74862012-03-16 05:04:25 +0000204 else {
Andrew Trick3c58ba82012-01-14 02:17:18 +0000205 assert(!IsPostRA && "Virtual register encountered after regalloc.");
Andrew Trickd3a74862012-03-16 05:04:25 +0000206 addVRegUseDeps(&ExitSU, i);
207 }
Evan Chengec6906b2010-10-23 02:10:46 +0000208 }
209 } else {
210 // For others, e.g. fallthrough, conditional branch, assume the exit
Evan Chengde5fa932010-10-27 23:17:17 +0000211 // uses all the registers that are livein to the successor blocks.
212 SmallSet<unsigned, 8> Seen;
213 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
214 SE = BB->succ_end(); SI != SE; ++SI)
215 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
Andrew Trickf405b1a2011-05-05 19:24:06 +0000216 E = (*SI)->livein_end(); I != E; ++I) {
Evan Chengde5fa932010-10-27 23:17:17 +0000217 unsigned Reg = *I;
218 if (Seen.insert(Reg))
Andrew Trick702d4892012-02-24 07:04:55 +0000219 Uses[Reg].push_back(&ExitSU);
Evan Chengde5fa932010-10-27 23:17:17 +0000220 }
Evan Chengec6906b2010-10-23 02:10:46 +0000221 }
222}
223
Andrew Trick81a682a2012-02-23 01:52:38 +0000224/// MO is an operand of SU's instruction that defines a physical register. Add
225/// data dependencies from SU to any uses of the physical register.
226void ScheduleDAGInstrs::addPhysRegDataDeps(SUnit *SU,
227 const MachineOperand &MO) {
228 assert(MO.isDef() && "expect physreg def");
229
230 // Ask the target if address-backscheduling is desirable, and if so how much.
231 const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>();
232 unsigned SpecialAddressLatency = ST.getSpecialAddressLatency();
233 unsigned DataLatency = SU->Latency;
234
Craig Toppere4fd9072012-03-04 10:43:23 +0000235 for (const uint16_t *Alias = TRI->getOverlaps(MO.getReg()); *Alias; ++Alias) {
Andrew Trick702d4892012-02-24 07:04:55 +0000236 if (!Uses.contains(*Alias))
Andrew Trick81a682a2012-02-23 01:52:38 +0000237 continue;
Andrew Trick702d4892012-02-24 07:04:55 +0000238 std::vector<SUnit*> &UseList = Uses[*Alias];
Andrew Trick81a682a2012-02-23 01:52:38 +0000239 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
240 SUnit *UseSU = UseList[i];
241 if (UseSU == SU)
242 continue;
243 unsigned LDataLatency = DataLatency;
244 // Optionally add in a special extra latency for nodes that
245 // feed addresses.
246 // TODO: Perhaps we should get rid of
247 // SpecialAddressLatency and just move this into
248 // adjustSchedDependency for the targets that care about it.
249 if (SpecialAddressLatency != 0 && !UnitLatencies &&
250 UseSU != &ExitSU) {
251 MachineInstr *UseMI = UseSU->getInstr();
252 const MCInstrDesc &UseMCID = UseMI->getDesc();
253 int RegUseIndex = UseMI->findRegisterUseOperandIdx(*Alias);
254 assert(RegUseIndex >= 0 && "UseMI doesn't use register!");
255 if (RegUseIndex >= 0 &&
256 (UseMI->mayLoad() || UseMI->mayStore()) &&
257 (unsigned)RegUseIndex < UseMCID.getNumOperands() &&
258 UseMCID.OpInfo[RegUseIndex].isLookupPtrRegClass())
259 LDataLatency += SpecialAddressLatency;
260 }
261 // Adjust the dependence latency using operand def/use
262 // information (if any), and then allow the target to
263 // perform its own adjustments.
264 const SDep& dep = SDep(SU, SDep::Data, LDataLatency, *Alias);
265 if (!UnitLatencies) {
Andrew Trick953be892012-03-07 23:00:49 +0000266 computeOperandLatency(SU, UseSU, const_cast<SDep &>(dep));
Andrew Trick81a682a2012-02-23 01:52:38 +0000267 ST.adjustSchedDependency(SU, UseSU, const_cast<SDep &>(dep));
268 }
269 UseSU->addPred(dep);
270 }
271 }
272}
273
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000274/// addPhysRegDeps - Add register dependencies (data, anti, and output) from
275/// this SUnit to following instructions in the same scheduling region that
276/// depend the physical register referenced at OperIdx.
277void ScheduleDAGInstrs::addPhysRegDeps(SUnit *SU, unsigned OperIdx) {
278 const MachineInstr *MI = SU->getInstr();
279 const MachineOperand &MO = MI->getOperand(OperIdx);
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000280
281 // Optionally add output and anti dependencies. For anti
282 // dependencies we use a latency of 0 because for a multi-issue
283 // target we want to allow the defining instruction to issue
284 // in the same cycle as the using instruction.
285 // TODO: Using a latency of 1 here for output dependencies assumes
286 // there's no cost for reusing registers.
287 SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
Craig Toppere4fd9072012-03-04 10:43:23 +0000288 for (const uint16_t *Alias = TRI->getOverlaps(MO.getReg()); *Alias; ++Alias) {
Andrew Trick702d4892012-02-24 07:04:55 +0000289 if (!Defs.contains(*Alias))
Andrew Trick81a682a2012-02-23 01:52:38 +0000290 continue;
Andrew Trick702d4892012-02-24 07:04:55 +0000291 std::vector<SUnit *> &DefList = Defs[*Alias];
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000292 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
293 SUnit *DefSU = DefList[i];
294 if (DefSU == &ExitSU)
295 continue;
296 if (DefSU != SU &&
297 (Kind != SDep::Output || !MO.isDead() ||
298 !DefSU->getInstr()->registerDefIsDead(*Alias))) {
299 if (Kind == SDep::Anti)
300 DefSU->addPred(SDep(SU, Kind, 0, /*Reg=*/*Alias));
301 else {
302 unsigned AOLat = TII->getOutputLatency(InstrItins, MI, OperIdx,
303 DefSU->getInstr());
304 DefSU->addPred(SDep(SU, Kind, AOLat, /*Reg=*/*Alias));
305 }
306 }
307 }
308 }
309
Andrew Trick81a682a2012-02-23 01:52:38 +0000310 if (!MO.isDef()) {
311 // Either insert a new Reg2SUnits entry with an empty SUnits list, or
312 // retrieve the existing SUnits list for this register's uses.
313 // Push this SUnit on the use list.
Andrew Trick702d4892012-02-24 07:04:55 +0000314 Uses[MO.getReg()].push_back(SU);
Andrew Trick81a682a2012-02-23 01:52:38 +0000315 }
316 else {
317 addPhysRegDataDeps(SU, MO);
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000318
Andrew Trick81a682a2012-02-23 01:52:38 +0000319 // Either insert a new Reg2SUnits entry with an empty SUnits list, or
320 // retrieve the existing SUnits list for this register's defs.
Andrew Trick702d4892012-02-24 07:04:55 +0000321 std::vector<SUnit *> &DefList = Defs[MO.getReg()];
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000322
323 // If a def is going to wrap back around to the top of the loop,
324 // backschedule it.
325 if (!UnitLatencies && DefList.empty()) {
Andrew Trick81a682a2012-02-23 01:52:38 +0000326 LoopDependencies::LoopDeps::iterator I = LoopRegs.Deps.find(MO.getReg());
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000327 if (I != LoopRegs.Deps.end()) {
328 const MachineOperand *UseMO = I->second.first;
329 unsigned Count = I->second.second;
330 const MachineInstr *UseMI = UseMO->getParent();
331 unsigned UseMOIdx = UseMO - &UseMI->getOperand(0);
332 const MCInstrDesc &UseMCID = UseMI->getDesc();
Andrew Trick81a682a2012-02-23 01:52:38 +0000333 const TargetSubtargetInfo &ST =
334 TM.getSubtarget<TargetSubtargetInfo>();
335 unsigned SpecialAddressLatency = ST.getSpecialAddressLatency();
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000336 // TODO: If we knew the total depth of the region here, we could
337 // handle the case where the whole loop is inside the region but
338 // is large enough that the isScheduleHigh trick isn't needed.
339 if (UseMOIdx < UseMCID.getNumOperands()) {
340 // Currently, we only support scheduling regions consisting of
341 // single basic blocks. Check to see if the instruction is in
342 // the same region by checking to see if it has the same parent.
343 if (UseMI->getParent() != MI->getParent()) {
344 unsigned Latency = SU->Latency;
345 if (UseMCID.OpInfo[UseMOIdx].isLookupPtrRegClass())
346 Latency += SpecialAddressLatency;
347 // This is a wild guess as to the portion of the latency which
348 // will be overlapped by work done outside the current
349 // scheduling region.
350 Latency -= std::min(Latency, Count);
351 // Add the artificial edge.
352 ExitSU.addPred(SDep(SU, SDep::Order, Latency,
353 /*Reg=*/0, /*isNormalMemory=*/false,
354 /*isMustAlias=*/false,
355 /*isArtificial=*/true));
356 } else if (SpecialAddressLatency > 0 &&
357 UseMCID.OpInfo[UseMOIdx].isLookupPtrRegClass()) {
358 // The entire loop body is within the current scheduling region
359 // and the latency of this operation is assumed to be greater
360 // than the latency of the loop.
361 // TODO: Recursively mark data-edge predecessors as
362 // isScheduleHigh too.
363 SU->isScheduleHigh = true;
364 }
365 }
366 LoopRegs.Deps.erase(I);
367 }
368 }
369
Andrew Trick81a682a2012-02-23 01:52:38 +0000370 // clear this register's use list
Andrew Trick702d4892012-02-24 07:04:55 +0000371 if (Uses.contains(MO.getReg()))
372 Uses[MO.getReg()].clear();
Andrew Trick81a682a2012-02-23 01:52:38 +0000373
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000374 if (!MO.isDead())
375 DefList.clear();
376
377 // Calls will not be reordered because of chain dependencies (see
378 // below). Since call operands are dead, calls may continue to be added
379 // to the DefList making dependence checking quadratic in the size of
380 // the block. Instead, we leave only one call at the back of the
381 // DefList.
382 if (SU->isCall) {
383 while (!DefList.empty() && DefList.back()->isCall)
384 DefList.pop_back();
385 }
Andrew Trick81a682a2012-02-23 01:52:38 +0000386 // Defs are pushed in the order they are visited and never reordered.
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000387 DefList.push_back(SU);
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000388 }
389}
390
Andrew Trick3c58ba82012-01-14 02:17:18 +0000391/// addVRegDefDeps - Add register output and data dependencies from this SUnit
392/// to instructions that occur later in the same scheduling region if they read
393/// from or write to the virtual register defined at OperIdx.
394///
395/// TODO: Hoist loop induction variable increments. This has to be
396/// reevaluated. Generally, IV scheduling should be done before coalescing.
397void ScheduleDAGInstrs::addVRegDefDeps(SUnit *SU, unsigned OperIdx) {
398 const MachineInstr *MI = SU->getInstr();
399 unsigned Reg = MI->getOperand(OperIdx).getReg();
400
Andrew Trickcc77b542012-02-22 06:08:13 +0000401 // SSA defs do not have output/anti dependencies.
Andrew Trick2fc09772012-02-22 18:34:49 +0000402 // The current operand is a def, so we have at least one.
Andrew Trickcc77b542012-02-22 06:08:13 +0000403 if (llvm::next(MRI.def_begin(Reg)) == MRI.def_end())
404 return;
405
Andrew Trick3c58ba82012-01-14 02:17:18 +0000406 // Add output dependence to the next nearest def of this vreg.
407 //
408 // Unless this definition is dead, the output dependence should be
409 // transitively redundant with antidependencies from this definition's
410 // uses. We're conservative for now until we have a way to guarantee the uses
411 // are not eliminated sometime during scheduling. The output dependence edge
412 // is also useful if output latency exceeds def-use latency.
Andrew Trick8ae3ac72012-02-22 21:59:00 +0000413 VReg2SUnitMap::iterator DefI = findVRegDef(Reg);
414 if (DefI == VRegDefs.end())
415 VRegDefs.insert(VReg2SUnit(Reg, SU));
416 else {
417 SUnit *DefSU = DefI->SU;
418 if (DefSU != SU && DefSU != &ExitSU) {
419 unsigned OutLatency = TII->getOutputLatency(InstrItins, MI, OperIdx,
420 DefSU->getInstr());
421 DefSU->addPred(SDep(SU, SDep::Output, OutLatency, Reg));
422 }
423 DefI->SU = SU;
Andrew Trick3c58ba82012-01-14 02:17:18 +0000424 }
Andrew Trick3c58ba82012-01-14 02:17:18 +0000425}
426
Andrew Trickb4566a92012-02-22 06:08:11 +0000427/// addVRegUseDeps - Add a register data dependency if the instruction that
428/// defines the virtual register used at OperIdx is mapped to an SUnit. Add a
429/// register antidependency from this SUnit to instructions that occur later in
430/// the same scheduling region if they write the virtual register.
431///
432/// TODO: Handle ExitSU "uses" properly.
Andrew Trick3c58ba82012-01-14 02:17:18 +0000433void ScheduleDAGInstrs::addVRegUseDeps(SUnit *SU, unsigned OperIdx) {
Andrew Trickb4566a92012-02-22 06:08:11 +0000434 MachineInstr *MI = SU->getInstr();
435 unsigned Reg = MI->getOperand(OperIdx).getReg();
436
437 // Lookup this operand's reaching definition.
438 assert(LIS && "vreg dependencies requires LiveIntervals");
Andrew Trick63d578b2012-02-23 03:16:24 +0000439 SlotIndex UseIdx = LIS->getInstructionIndex(MI).getRegSlot();
Andrew Trickb4566a92012-02-22 06:08:11 +0000440 LiveInterval *LI = &LIS->getInterval(Reg);
Andrew Trick63d578b2012-02-23 03:16:24 +0000441 VNInfo *VNI = LI->getVNInfoBefore(UseIdx);
442 // VNI will be valid because MachineOperand::readsReg() is checked by caller.
Andrew Trickb4566a92012-02-22 06:08:11 +0000443 MachineInstr *Def = LIS->getInstructionFromIndex(VNI->def);
Andrew Trick63d578b2012-02-23 03:16:24 +0000444 // Phis and other noninstructions (after coalescing) have a NULL Def.
Andrew Trickb4566a92012-02-22 06:08:11 +0000445 if (Def) {
446 SUnit *DefSU = getSUnit(Def);
447 if (DefSU) {
448 // The reaching Def lives within this scheduling region.
449 // Create a data dependence.
450 //
451 // TODO: Handle "special" address latencies cleanly.
452 const SDep &dep = SDep(DefSU, SDep::Data, DefSU->Latency, Reg);
453 if (!UnitLatencies) {
454 // Adjust the dependence latency using operand def/use information, then
455 // allow the target to perform its own adjustments.
Andrew Trick953be892012-03-07 23:00:49 +0000456 computeOperandLatency(DefSU, SU, const_cast<SDep &>(dep));
Andrew Trickb4566a92012-02-22 06:08:11 +0000457 const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>();
458 ST.adjustSchedDependency(DefSU, SU, const_cast<SDep &>(dep));
459 }
460 SU->addPred(dep);
461 }
462 }
Andrew Trick3c58ba82012-01-14 02:17:18 +0000463
464 // Add antidependence to the following def of the vreg it uses.
Andrew Trick8ae3ac72012-02-22 21:59:00 +0000465 VReg2SUnitMap::iterator DefI = findVRegDef(Reg);
466 if (DefI != VRegDefs.end() && DefI->SU != SU)
467 DefI->SU->addPred(SDep(SU, SDep::Anti, 0, Reg));
Andrew Trickb4566a92012-02-22 06:08:11 +0000468}
Andrew Trick3c58ba82012-01-14 02:17:18 +0000469
Andrew Trickb4566a92012-02-22 06:08:11 +0000470/// Create an SUnit for each real instruction, numbered in top-down toplological
471/// order. The instruction order A < B, implies that no edge exists from B to A.
472///
473/// Map each real instruction to its SUnit.
474///
Andrew Trick17d35e52012-03-14 04:00:41 +0000475/// After initSUnits, the SUnits vector cannot be resized and the scheduler may
476/// hang onto SUnit pointers. We may relax this in the future by using SUnit IDs
477/// instead of pointers.
478///
479/// MachineScheduler relies on initSUnits numbering the nodes by their order in
480/// the original instruction list.
Andrew Trickb4566a92012-02-22 06:08:11 +0000481void ScheduleDAGInstrs::initSUnits() {
482 // We'll be allocating one SUnit for each real instruction in the region,
483 // which is contained within a basic block.
484 SUnits.reserve(BB->size());
485
Andrew Trick68675c62012-03-09 04:29:02 +0000486 for (MachineBasicBlock::iterator I = RegionBegin; I != RegionEnd; ++I) {
Andrew Trickb4566a92012-02-22 06:08:11 +0000487 MachineInstr *MI = I;
488 if (MI->isDebugValue())
489 continue;
490
Andrew Trick953be892012-03-07 23:00:49 +0000491 SUnit *SU = newSUnit(MI);
Andrew Trickb4566a92012-02-22 06:08:11 +0000492 MISUnitMap[MI] = SU;
493
494 SU->isCall = MI->isCall();
495 SU->isCommutable = MI->isCommutable();
496
497 // Assign the Latency field of SU using target-provided information.
498 if (UnitLatencies)
499 SU->Latency = 1;
500 else
Andrew Trick953be892012-03-07 23:00:49 +0000501 computeLatency(SU);
Andrew Trickb4566a92012-02-22 06:08:11 +0000502 }
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000503}
504
Andrew Trick953be892012-03-07 23:00:49 +0000505void ScheduleDAGInstrs::buildSchedGraph(AliasAnalysis *AA) {
Andrew Trickb4566a92012-02-22 06:08:11 +0000506 // Create an SUnit for each real instruction.
507 initSUnits();
Dan Gohman343f0c02008-11-19 23:18:57 +0000508
Dan Gohman6a9041e2008-12-04 01:35:46 +0000509 // We build scheduling units by walking a block's instruction list from bottom
510 // to top.
511
David Goodwin980d4942009-11-09 19:22:17 +0000512 // Remember where a generic side-effecting instruction is as we procede.
513 SUnit *BarrierChain = 0, *AliasChain = 0;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000514
David Goodwin980d4942009-11-09 19:22:17 +0000515 // Memory references to specific known memory locations are tracked
516 // so that they can be given more precise dependencies. We track
517 // separately the known memory locations that may alias and those
518 // that are known not to alias
519 std::map<const Value *, SUnit *> AliasMemDefs, NonAliasMemDefs;
520 std::map<const Value *, std::vector<SUnit *> > AliasMemUses, NonAliasMemUses;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000521
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000522 // Remove any stale debug info; sometimes BuildSchedGraph is called again
523 // without emitting the info from the previous call.
Devang Patelcf4cc842011-06-02 20:07:12 +0000524 DbgValues.clear();
525 FirstDbgValue = NULL;
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000526
Andrew Trick81a682a2012-02-23 01:52:38 +0000527 assert(Defs.empty() && Uses.empty() &&
528 "Only BuildGraph should update Defs/Uses");
Andrew Trick702d4892012-02-24 07:04:55 +0000529 Defs.setRegLimit(TRI->getNumRegs());
530 Uses.setRegLimit(TRI->getNumRegs());
Andrew Trick9b668532011-05-06 21:52:52 +0000531
Andrew Trick8ae3ac72012-02-22 21:59:00 +0000532 assert(VRegDefs.empty() && "Only BuildSchedGraph may access VRegDefs");
533 // FIXME: Allow SparseSet to reserve space for the creation of virtual
534 // registers during scheduling. Don't artificially inflate the Universe
535 // because we want to assert that vregs are not created during DAG building.
536 VRegDefs.setUniverse(MRI.getNumVirtRegs());
Andrew Trick3c58ba82012-01-14 02:17:18 +0000537
Andrew Trick81a682a2012-02-23 01:52:38 +0000538 // Model data dependencies between instructions being scheduled and the
539 // ExitSU.
Andrew Trick953be892012-03-07 23:00:49 +0000540 addSchedBarrierDeps();
Andrew Trick81a682a2012-02-23 01:52:38 +0000541
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000542 // Walk the list of instructions, from bottom moving up.
Devang Patelcf4cc842011-06-02 20:07:12 +0000543 MachineInstr *PrevMI = NULL;
Andrew Trick68675c62012-03-09 04:29:02 +0000544 for (MachineBasicBlock::iterator MII = RegionEnd, MIE = RegionBegin;
Dan Gohman343f0c02008-11-19 23:18:57 +0000545 MII != MIE; --MII) {
546 MachineInstr *MI = prior(MII);
Devang Patelcf4cc842011-06-02 20:07:12 +0000547 if (MI && PrevMI) {
548 DbgValues.push_back(std::make_pair(PrevMI, MI));
549 PrevMI = NULL;
550 }
551
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000552 if (MI->isDebugValue()) {
Devang Patelcf4cc842011-06-02 20:07:12 +0000553 PrevMI = MI;
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000554 continue;
555 }
Devang Patelcf4cc842011-06-02 20:07:12 +0000556
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000557 assert(!MI->isTerminator() && !MI->isLabel() &&
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000558 "Cannot schedule terminators or labels!");
Dan Gohman343f0c02008-11-19 23:18:57 +0000559
Andrew Trickb4566a92012-02-22 06:08:11 +0000560 SUnit *SU = MISUnitMap[MI];
561 assert(SU && "No SUnit mapped to this MI");
Dan Gohman54e4c362008-12-09 22:54:47 +0000562
Dan Gohman6a9041e2008-12-04 01:35:46 +0000563 // Add register-based dependencies (data, anti, and output).
Dan Gohman343f0c02008-11-19 23:18:57 +0000564 for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) {
565 const MachineOperand &MO = MI->getOperand(j);
566 if (!MO.isReg()) continue;
567 unsigned Reg = MO.getReg();
568 if (Reg == 0) continue;
569
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000570 if (TRI->isPhysicalRegister(Reg))
571 addPhysRegDeps(SU, j);
572 else {
573 assert(!IsPostRA && "Virtual register encountered!");
Andrew Trick3c58ba82012-01-14 02:17:18 +0000574 if (MO.isDef())
575 addVRegDefDeps(SU, j);
Andrew Trick63d578b2012-02-23 03:16:24 +0000576 else if (MO.readsReg()) // ignore undef operands
Andrew Trick3c58ba82012-01-14 02:17:18 +0000577 addVRegUseDeps(SU, j);
Dan Gohman343f0c02008-11-19 23:18:57 +0000578 }
579 }
Dan Gohman6a9041e2008-12-04 01:35:46 +0000580
581 // Add chain dependencies.
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000582 // Chain dependencies used to enforce memory order should have
583 // latency of 0 (except for true dependency of Store followed by
584 // aliased Load... we estimate that with a single cycle of latency
585 // assuming the hardware will bypass)
Dan Gohman6a9041e2008-12-04 01:35:46 +0000586 // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable
587 // after stack slots are lowered to actual addresses.
588 // TODO: Use an AliasAnalysis and do real alias-analysis queries, and
589 // produce more precise dependence information.
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000590#define STORE_LOAD_LATENCY 1
591 unsigned TrueMemOrderLatency = 0;
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000592 if (MI->isCall() || MI->hasUnmodeledSideEffects() ||
Andrew Trickf405b1a2011-05-05 19:24:06 +0000593 (MI->hasVolatileMemoryRef() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000594 (!MI->mayLoad() || !MI->isInvariantLoad(AA)))) {
David Goodwin980d4942009-11-09 19:22:17 +0000595 // Be conservative with these and add dependencies on all memory
596 // references, even those that are known to not alias.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000597 for (std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000598 NonAliasMemDefs.begin(), E = NonAliasMemDefs.end(); I != E; ++I) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000599 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000600 }
601 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000602 NonAliasMemUses.begin(), E = NonAliasMemUses.end(); I != E; ++I) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000603 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000604 I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000605 }
David Goodwin980d4942009-11-09 19:22:17 +0000606 NonAliasMemDefs.clear();
607 NonAliasMemUses.clear();
608 // Add SU to the barrier chain.
609 if (BarrierChain)
610 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
611 BarrierChain = SU;
612
613 // fall-through
614 new_alias_chain:
615 // Chain all possibly aliasing memory references though SU.
616 if (AliasChain)
617 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
618 AliasChain = SU;
619 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
620 PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
621 for (std::map<const Value *, SUnit *>::iterator I = AliasMemDefs.begin(),
622 E = AliasMemDefs.end(); I != E; ++I) {
623 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
624 }
625 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
626 AliasMemUses.begin(), E = AliasMemUses.end(); I != E; ++I) {
627 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
628 I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
629 }
630 PendingLoads.clear();
631 AliasMemDefs.clear();
632 AliasMemUses.clear();
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000633 } else if (MI->mayStore()) {
David Goodwina9e61072009-11-03 20:15:00 +0000634 bool MayAlias = true;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000635 TrueMemOrderLatency = STORE_LOAD_LATENCY;
David Goodwina9e61072009-11-03 20:15:00 +0000636 if (const Value *V = getUnderlyingObjectForInstr(MI, MFI, MayAlias)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000637 // A store to a specific PseudoSourceValue. Add precise dependencies.
David Goodwin980d4942009-11-09 19:22:17 +0000638 // Record the def in MemDefs, first adding a dep if there is
639 // an existing def.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000640 std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000641 ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000642 std::map<const Value *, SUnit *>::iterator IE =
David Goodwin980d4942009-11-09 19:22:17 +0000643 ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end());
644 if (I != IE) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000645 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0,
Dan Gohman54e4c362008-12-09 22:54:47 +0000646 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000647 I->second = SU;
648 } else {
David Goodwin980d4942009-11-09 19:22:17 +0000649 if (MayAlias)
650 AliasMemDefs[V] = SU;
651 else
652 NonAliasMemDefs[V] = SU;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000653 }
654 // Handle the uses in MemUses, if there are any.
Dan Gohmana629b482008-12-08 17:50:35 +0000655 std::map<const Value *, std::vector<SUnit *> >::iterator J =
David Goodwin980d4942009-11-09 19:22:17 +0000656 ((MayAlias) ? AliasMemUses.find(V) : NonAliasMemUses.find(V));
657 std::map<const Value *, std::vector<SUnit *> >::iterator JE =
658 ((MayAlias) ? AliasMemUses.end() : NonAliasMemUses.end());
659 if (J != JE) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000660 for (unsigned i = 0, e = J->second.size(); i != e; ++i)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000661 J->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency,
662 /*Reg=*/0, /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000663 J->second.clear();
664 }
David Goodwina9e61072009-11-03 20:15:00 +0000665 if (MayAlias) {
David Goodwin980d4942009-11-09 19:22:17 +0000666 // Add dependencies from all the PendingLoads, i.e. loads
667 // with no underlying object.
David Goodwina9e61072009-11-03 20:15:00 +0000668 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
669 PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
David Goodwin980d4942009-11-09 19:22:17 +0000670 // Add dependence on alias chain, if needed.
671 if (AliasChain)
672 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
David Goodwina9e61072009-11-03 20:15:00 +0000673 }
David Goodwin980d4942009-11-09 19:22:17 +0000674 // Add dependence on barrier chain, if needed.
675 if (BarrierChain)
676 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
David Goodwin5be870a2009-11-05 00:16:44 +0000677 } else {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000678 // Treat all other stores conservatively.
David Goodwin980d4942009-11-09 19:22:17 +0000679 goto new_alias_chain;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000680 }
Evan Chengec6906b2010-10-23 02:10:46 +0000681
682 if (!ExitSU.isPred(SU))
683 // Push store's up a bit to avoid them getting in between cmp
684 // and branches.
685 ExitSU.addPred(SDep(SU, SDep::Order, 0,
686 /*Reg=*/0, /*isNormalMemory=*/false,
687 /*isMustAlias=*/false,
688 /*isArtificial=*/true));
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000689 } else if (MI->mayLoad()) {
David Goodwina9e61072009-11-03 20:15:00 +0000690 bool MayAlias = true;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000691 TrueMemOrderLatency = 0;
Dan Gohmana70dca12009-10-09 23:27:56 +0000692 if (MI->isInvariantLoad(AA)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000693 // Invariant load, no chain dependencies needed!
David Goodwin5be870a2009-11-05 00:16:44 +0000694 } else {
Andrew Trickf405b1a2011-05-05 19:24:06 +0000695 if (const Value *V =
David Goodwin980d4942009-11-09 19:22:17 +0000696 getUnderlyingObjectForInstr(MI, MFI, MayAlias)) {
697 // A load from a specific PseudoSourceValue. Add precise dependencies.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000698 std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000699 ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000700 std::map<const Value *, SUnit *>::iterator IE =
David Goodwin980d4942009-11-09 19:22:17 +0000701 ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end());
702 if (I != IE)
703 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0,
704 /*isNormalMemory=*/true));
705 if (MayAlias)
706 AliasMemUses[V].push_back(SU);
Andrew Trickf405b1a2011-05-05 19:24:06 +0000707 else
David Goodwin980d4942009-11-09 19:22:17 +0000708 NonAliasMemUses[V].push_back(SU);
709 } else {
710 // A load with no underlying object. Depend on all
711 // potentially aliasing stores.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000712 for (std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000713 AliasMemDefs.begin(), E = AliasMemDefs.end(); I != E; ++I)
714 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000715
David Goodwin980d4942009-11-09 19:22:17 +0000716 PendingLoads.push_back(SU);
717 MayAlias = true;
David Goodwina9e61072009-11-03 20:15:00 +0000718 }
Andrew Trickf405b1a2011-05-05 19:24:06 +0000719
David Goodwin980d4942009-11-09 19:22:17 +0000720 // Add dependencies on alias and barrier chains, if needed.
721 if (MayAlias && AliasChain)
722 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
723 if (BarrierChain)
724 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000725 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000726 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000727 }
Devang Patelcf4cc842011-06-02 20:07:12 +0000728 if (PrevMI)
729 FirstDbgValue = PrevMI;
Dan Gohman79ce2762009-01-15 19:20:50 +0000730
Andrew Trick81a682a2012-02-23 01:52:38 +0000731 Defs.clear();
732 Uses.clear();
Andrew Trick3c58ba82012-01-14 02:17:18 +0000733 VRegDefs.clear();
Dan Gohman79ce2762009-01-15 19:20:50 +0000734 PendingLoads.clear();
Dan Gohman343f0c02008-11-19 23:18:57 +0000735}
736
Andrew Trick953be892012-03-07 23:00:49 +0000737void ScheduleDAGInstrs::computeLatency(SUnit *SU) {
David Goodwind94a4e52009-08-10 15:55:25 +0000738 // Compute the latency for the node.
Evan Cheng3ef1c872010-09-10 01:29:16 +0000739 if (!InstrItins || InstrItins->isEmpty()) {
740 SU->Latency = 1;
Dan Gohman4ea8e852008-12-16 02:38:22 +0000741
Evan Cheng3ef1c872010-09-10 01:29:16 +0000742 // Simplistic target-independent heuristic: assume that loads take
743 // extra time.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000744 if (SU->getInstr()->mayLoad())
Dan Gohman4ea8e852008-12-16 02:38:22 +0000745 SU->Latency += 2;
Evan Cheng8239daf2010-11-03 00:45:17 +0000746 } else {
747 SU->Latency = TII->getInstrLatency(InstrItins, SU->getInstr());
748 }
Dan Gohmanc8c28272008-11-21 00:12:10 +0000749}
750
Andrew Trick953be892012-03-07 23:00:49 +0000751void ScheduleDAGInstrs::computeOperandLatency(SUnit *Def, SUnit *Use,
David Goodwindc4bdcd2009-08-19 16:08:58 +0000752 SDep& dep) const {
Evan Cheng3ef1c872010-09-10 01:29:16 +0000753 if (!InstrItins || InstrItins->isEmpty())
David Goodwindc4bdcd2009-08-19 16:08:58 +0000754 return;
Andrew Trickf405b1a2011-05-05 19:24:06 +0000755
David Goodwindc4bdcd2009-08-19 16:08:58 +0000756 // For a data dependency with a known register...
757 if ((dep.getKind() != SDep::Data) || (dep.getReg() == 0))
758 return;
759
760 const unsigned Reg = dep.getReg();
761
762 // ... find the definition of the register in the defining
763 // instruction
764 MachineInstr *DefMI = Def->getInstr();
765 int DefIdx = DefMI->findRegisterDefOperandIdx(Reg);
766 if (DefIdx != -1) {
Evan Cheng1aca5bc2010-10-08 18:42:25 +0000767 const MachineOperand &MO = DefMI->getOperand(DefIdx);
768 if (MO.isReg() && MO.isImplicit() &&
Evan Chengd82de832010-10-08 23:01:57 +0000769 DefIdx >= (int)DefMI->getDesc().getNumOperands()) {
Evan Cheng1aca5bc2010-10-08 18:42:25 +0000770 // This is an implicit def, getOperandLatency() won't return the correct
771 // latency. e.g.
772 // %D6<def>, %D7<def> = VLD1q16 %R2<kill>, 0, ..., %Q3<imp-def>
773 // %Q1<def> = VMULv8i16 %Q1<kill>, %Q3<kill>, ...
774 // What we want is to compute latency between def of %D6/%D7 and use of
775 // %Q3 instead.
Jakob Stoklund Olesen02634be2012-02-22 22:52:52 +0000776 unsigned Op2 = DefMI->findRegisterDefOperandIdx(Reg, false, true, TRI);
777 if (DefMI->getOperand(Op2).isReg())
778 DefIdx = Op2;
Evan Cheng1aca5bc2010-10-08 18:42:25 +0000779 }
Evan Chenga0792de2010-10-06 06:27:31 +0000780 MachineInstr *UseMI = Use->getInstr();
Evan Cheng3881cb72010-09-29 22:42:35 +0000781 // For all uses of the register, calculate the maxmimum latency
782 int Latency = -1;
Evan Chengec6906b2010-10-23 02:10:46 +0000783 if (UseMI) {
784 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
785 const MachineOperand &MO = UseMI->getOperand(i);
786 if (!MO.isReg() || !MO.isUse())
787 continue;
788 unsigned MOReg = MO.getReg();
789 if (MOReg != Reg)
790 continue;
David Goodwindc4bdcd2009-08-19 16:08:58 +0000791
Evan Chengec6906b2010-10-23 02:10:46 +0000792 int UseCycle = TII->getOperandLatency(InstrItins, DefMI, DefIdx,
793 UseMI, i);
794 Latency = std::max(Latency, UseCycle);
795 }
796 } else {
797 // UseMI is null, then it must be a scheduling barrier.
798 if (!InstrItins || InstrItins->isEmpty())
799 return;
800 unsigned DefClass = DefMI->getDesc().getSchedClass();
801 Latency = InstrItins->getOperandCycle(DefClass, DefIdx);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000802 }
Evan Chengec6906b2010-10-23 02:10:46 +0000803
804 // If we found a latency, then replace the existing dependence latency.
805 if (Latency >= 0)
806 dep.setLatency(Latency);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000807 }
808}
809
Dan Gohman343f0c02008-11-19 23:18:57 +0000810void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
811 SU->getInstr()->dump();
812}
813
814std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
815 std::string s;
816 raw_string_ostream oss(s);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000817 if (SU == &EntrySU)
818 oss << "<entry>";
819 else if (SU == &ExitSU)
820 oss << "<exit>";
821 else
822 SU->getInstr()->print(oss);
Dan Gohman343f0c02008-11-19 23:18:57 +0000823 return oss.str();
824}
825
Andrew Trick56b94c52012-03-07 00:18:22 +0000826/// Return the basic block label. It is not necessarilly unique because a block
827/// contains multiple scheduling regions. But it is fine for visualization.
828std::string ScheduleDAGInstrs::getDAGName() const {
829 return "dag." + BB->getFullName();
830}