blob: c4addeea25e8a65fc233fc9a975714fd912be8d8 [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"
Andrew Trickb4566a92012-02-22 06:08:11 +000020#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Dan Gohman3f237442008-12-16 03:25:46 +000021#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohmanc76909a2009-09-25 20:36:54 +000022#include "llvm/CodeGen/MachineMemOperand.h"
Dan Gohman3f237442008-12-16 03:25:46 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman6a9041e2008-12-04 01:35:46 +000024#include "llvm/CodeGen/PseudoSourceValue.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 Trick5e920d72012-01-14 02:17:12 +000041 InstrItins(mf.getTarget().getInstrItineraryData()), IsPostRA(IsPostRAFlag),
Andrew Trick81a682a2012-02-23 01:52:38 +000042 LIS(lis), UnitLatencies(false), LoopRegs(MLI, MDT), FirstDbgValue(0) {
Andrew Trickb4566a92012-02-22 06:08:11 +000043 assert((IsPostRA || LIS) && "PreRA scheduling requires LiveIntervals");
Devang Patelcf4cc842011-06-02 20:07:12 +000044 DbgValues.clear();
Andrew Trickcc77b542012-02-22 06:08:13 +000045 assert(!(IsPostRA && MRI.getNumVirtRegs()) &&
Andrew Trick19273ae2012-02-21 04:51:23 +000046 "Virtual registers must be removed prior to PostRA scheduling");
Evan Cheng38bdfc62009-10-18 19:58:47 +000047}
Dan Gohman343f0c02008-11-19 23:18:57 +000048
Dan Gohman3311a1f2009-01-30 02:49:14 +000049/// getUnderlyingObjectFromInt - This is the function that does the work of
50/// looking through basic ptrtoint+arithmetic+inttoptr sequences.
51static const Value *getUnderlyingObjectFromInt(const Value *V) {
52 do {
Dan Gohman8906f952009-07-17 20:58:59 +000053 if (const Operator *U = dyn_cast<Operator>(V)) {
Dan Gohman3311a1f2009-01-30 02:49:14 +000054 // If we find a ptrtoint, we can transfer control back to the
55 // regular getUnderlyingObjectFromInt.
Dan Gohman8906f952009-07-17 20:58:59 +000056 if (U->getOpcode() == Instruction::PtrToInt)
Dan Gohman3311a1f2009-01-30 02:49:14 +000057 return U->getOperand(0);
58 // If we find an add of a constant or a multiplied value, it's
59 // likely that the other operand will lead us to the base
60 // object. We don't have to worry about the case where the
Dan Gohman748f98f2009-08-07 01:26:06 +000061 // object address is somehow being computed by the multiply,
Dan Gohman3311a1f2009-01-30 02:49:14 +000062 // because our callers only care when the result is an
63 // identifibale object.
Dan Gohman8906f952009-07-17 20:58:59 +000064 if (U->getOpcode() != Instruction::Add ||
Dan Gohman3311a1f2009-01-30 02:49:14 +000065 (!isa<ConstantInt>(U->getOperand(1)) &&
Dan Gohman8906f952009-07-17 20:58:59 +000066 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul))
Dan Gohman3311a1f2009-01-30 02:49:14 +000067 return V;
68 V = U->getOperand(0);
69 } else {
70 return V;
71 }
Duncan Sands1df98592010-02-16 11:11:14 +000072 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
Dan Gohman3311a1f2009-01-30 02:49:14 +000073 } while (1);
74}
75
Dan Gohman5034dd32010-12-15 20:02:24 +000076/// getUnderlyingObject - This is a wrapper around GetUnderlyingObject
Dan Gohman3311a1f2009-01-30 02:49:14 +000077/// and adds support for basic ptrtoint+arithmetic+inttoptr sequences.
78static const Value *getUnderlyingObject(const Value *V) {
79 // First just call Value::getUnderlyingObject to let it do what it does.
80 do {
Dan Gohman5034dd32010-12-15 20:02:24 +000081 V = GetUnderlyingObject(V);
Dan Gohman3311a1f2009-01-30 02:49:14 +000082 // If it found an inttoptr, use special code to continue climing.
Dan Gohman8906f952009-07-17 20:58:59 +000083 if (Operator::getOpcode(V) != Instruction::IntToPtr)
Dan Gohman3311a1f2009-01-30 02:49:14 +000084 break;
85 const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
86 // If that succeeded in finding a pointer, continue the search.
Duncan Sands1df98592010-02-16 11:11:14 +000087 if (!O->getType()->isPointerTy())
Dan Gohman3311a1f2009-01-30 02:49:14 +000088 break;
89 V = O;
90 } while (1);
91 return V;
92}
93
94/// getUnderlyingObjectForInstr - If this machine instr has memory reference
95/// information and it can be tracked to a normal reference to a known
96/// object, return the Value for that object. Otherwise return null.
Evan Cheng38bdfc62009-10-18 19:58:47 +000097static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI,
David Goodwina9e61072009-11-03 20:15:00 +000098 const MachineFrameInfo *MFI,
99 bool &MayAlias) {
100 MayAlias = true;
Dan Gohman3311a1f2009-01-30 02:49:14 +0000101 if (!MI->hasOneMemOperand() ||
Dan Gohmanc76909a2009-09-25 20:36:54 +0000102 !(*MI->memoperands_begin())->getValue() ||
103 (*MI->memoperands_begin())->isVolatile())
Dan Gohman3311a1f2009-01-30 02:49:14 +0000104 return 0;
105
Dan Gohmanc76909a2009-09-25 20:36:54 +0000106 const Value *V = (*MI->memoperands_begin())->getValue();
Dan Gohman3311a1f2009-01-30 02:49:14 +0000107 if (!V)
108 return 0;
109
110 V = getUnderlyingObject(V);
Evan Chengff89dcb2009-10-18 18:16:27 +0000111 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
112 // For now, ignore PseudoSourceValues which may alias LLVM IR values
113 // because the code that uses this function has no way to cope with
114 // such aliases.
Evan Cheng38bdfc62009-10-18 19:58:47 +0000115 if (PSV->isAliased(MFI))
Evan Chengff89dcb2009-10-18 18:16:27 +0000116 return 0;
Andrew Trickf405b1a2011-05-05 19:24:06 +0000117
David Goodwin980d4942009-11-09 19:22:17 +0000118 MayAlias = PSV->mayAlias(MFI);
Evan Chengff89dcb2009-10-18 18:16:27 +0000119 return V;
120 }
Dan Gohman3311a1f2009-01-30 02:49:14 +0000121
Evan Chengff89dcb2009-10-18 18:16:27 +0000122 if (isIdentifiedObject(V))
123 return V;
124
125 return 0;
Dan Gohman3311a1f2009-01-30 02:49:14 +0000126}
127
Andrew Trick953be892012-03-07 23:00:49 +0000128void ScheduleDAGInstrs::startBlock(MachineBasicBlock *BB) {
Andrew Tricke8deca82011-10-07 06:33:09 +0000129 LoopRegs.Deps.clear();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000130 if (MachineLoop *ML = MLI.getLoopFor(BB))
Evan Cheng977679d2012-01-07 03:02:36 +0000131 if (BB == ML->getLoopLatch())
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000132 LoopRegs.VisitLoop(ML);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000133}
134
Andrew Trick953be892012-03-07 23:00:49 +0000135void ScheduleDAGInstrs::finishBlock() {
Andrew Trick47c14452012-03-07 05:21:52 +0000136 // Nothing to do.
137}
138
Andrew Trick702d4892012-02-24 07:04:55 +0000139/// Initialize the map with the number of registers.
Andrew Trick035ec402012-03-07 23:00:57 +0000140void Reg2SUnitsMap::setRegLimit(unsigned Limit) {
Andrew Trick702d4892012-02-24 07:04:55 +0000141 PhysRegSet.setUniverse(Limit);
142 SUnits.resize(Limit);
143}
144
145/// Clear the map without deallocating storage.
Andrew Trick035ec402012-03-07 23:00:57 +0000146void Reg2SUnitsMap::clear() {
Andrew Trick702d4892012-02-24 07:04:55 +0000147 for (const_iterator I = reg_begin(), E = reg_end(); I != E; ++I) {
148 SUnits[*I].clear();
149 }
150 PhysRegSet.clear();
151}
152
Andrew Trick47c14452012-03-07 05:21:52 +0000153/// Initialize the DAG and common scheduler state for the current scheduling
154/// region. This does not actually create the DAG, only clears it. The
155/// scheduling driver may call BuildSchedGraph multiple times per scheduling
156/// region.
157void ScheduleDAGInstrs::enterRegion(MachineBasicBlock *bb,
158 MachineBasicBlock::iterator begin,
159 MachineBasicBlock::iterator end,
160 unsigned endcount) {
161 BB = bb;
162 Begin = begin;
Andrew Trickcf46b5a2012-03-07 23:00:52 +0000163 End = end;
164 EndIndex = endcount;
Andrew Trick47c14452012-03-07 05:21:52 +0000165
166 // Check to see if the scheduler cares about latencies.
Andrew Trick953be892012-03-07 23:00:49 +0000167 UnitLatencies = forceUnitLatencies();
Andrew Trick47c14452012-03-07 05:21:52 +0000168
169 ScheduleDAG::clearDAG();
170}
171
172/// Close the current scheduling region. Don't clear any state in case the
173/// driver wants to refer to the previous scheduling region.
174void ScheduleDAGInstrs::exitRegion() {
175 // Nothing to do.
176}
177
Andrew Trick953be892012-03-07 23:00:49 +0000178/// addSchedBarrierDeps - Add dependencies from instructions in the current
Evan Chengec6906b2010-10-23 02:10:46 +0000179/// list of instructions being scheduled to scheduling barrier by adding
180/// the exit SU to the register defs and use list. This is because we want to
181/// make sure instructions which define registers that are either used by
182/// the terminator or are live-out are properly scheduled. This is
183/// especially important when the definition latency of the return value(s)
184/// are too high to be hidden by the branch or when the liveout registers
185/// used by instructions in the fallthrough block.
Andrew Trick953be892012-03-07 23:00:49 +0000186void ScheduleDAGInstrs::addSchedBarrierDeps() {
Andrew Trickcf46b5a2012-03-07 23:00:52 +0000187 MachineInstr *ExitMI = End != BB->end() ? &*End : 0;
Evan Chengec6906b2010-10-23 02:10:46 +0000188 ExitSU.setInstr(ExitMI);
189 bool AllDepKnown = ExitMI &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000190 (ExitMI->isCall() || ExitMI->isBarrier());
Evan Chengec6906b2010-10-23 02:10:46 +0000191 if (ExitMI && AllDepKnown) {
192 // If it's a call or a barrier, add dependencies on the defs and uses of
193 // instruction.
194 for (unsigned i = 0, e = ExitMI->getNumOperands(); i != e; ++i) {
195 const MachineOperand &MO = ExitMI->getOperand(i);
196 if (!MO.isReg() || MO.isDef()) continue;
197 unsigned Reg = MO.getReg();
198 if (Reg == 0) continue;
199
Andrew Trick3c58ba82012-01-14 02:17:18 +0000200 if (TRI->isPhysicalRegister(Reg))
Andrew Trick702d4892012-02-24 07:04:55 +0000201 Uses[Reg].push_back(&ExitSU);
Andrew Trick3c58ba82012-01-14 02:17:18 +0000202 else
203 assert(!IsPostRA && "Virtual register encountered after regalloc.");
Evan Chengec6906b2010-10-23 02:10:46 +0000204 }
205 } else {
206 // For others, e.g. fallthrough, conditional branch, assume the exit
Evan Chengde5fa932010-10-27 23:17:17 +0000207 // uses all the registers that are livein to the successor blocks.
208 SmallSet<unsigned, 8> Seen;
209 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
210 SE = BB->succ_end(); SI != SE; ++SI)
211 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
Andrew Trickf405b1a2011-05-05 19:24:06 +0000212 E = (*SI)->livein_end(); I != E; ++I) {
Evan Chengde5fa932010-10-27 23:17:17 +0000213 unsigned Reg = *I;
214 if (Seen.insert(Reg))
Andrew Trick702d4892012-02-24 07:04:55 +0000215 Uses[Reg].push_back(&ExitSU);
Evan Chengde5fa932010-10-27 23:17:17 +0000216 }
Evan Chengec6906b2010-10-23 02:10:46 +0000217 }
218}
219
Andrew Trick81a682a2012-02-23 01:52:38 +0000220/// MO is an operand of SU's instruction that defines a physical register. Add
221/// data dependencies from SU to any uses of the physical register.
222void ScheduleDAGInstrs::addPhysRegDataDeps(SUnit *SU,
223 const MachineOperand &MO) {
224 assert(MO.isDef() && "expect physreg def");
225
226 // Ask the target if address-backscheduling is desirable, and if so how much.
227 const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>();
228 unsigned SpecialAddressLatency = ST.getSpecialAddressLatency();
229 unsigned DataLatency = SU->Latency;
230
Craig Toppere4fd9072012-03-04 10:43:23 +0000231 for (const uint16_t *Alias = TRI->getOverlaps(MO.getReg()); *Alias; ++Alias) {
Andrew Trick702d4892012-02-24 07:04:55 +0000232 if (!Uses.contains(*Alias))
Andrew Trick81a682a2012-02-23 01:52:38 +0000233 continue;
Andrew Trick702d4892012-02-24 07:04:55 +0000234 std::vector<SUnit*> &UseList = Uses[*Alias];
Andrew Trick81a682a2012-02-23 01:52:38 +0000235 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: Perhaps we should get rid of
243 // SpecialAddressLatency and just move this into
244 // adjustSchedDependency for the targets that care about it.
245 if (SpecialAddressLatency != 0 && !UnitLatencies &&
246 UseSU != &ExitSU) {
247 MachineInstr *UseMI = UseSU->getInstr();
248 const MCInstrDesc &UseMCID = UseMI->getDesc();
249 int RegUseIndex = UseMI->findRegisterUseOperandIdx(*Alias);
250 assert(RegUseIndex >= 0 && "UseMI doesn't use register!");
251 if (RegUseIndex >= 0 &&
252 (UseMI->mayLoad() || UseMI->mayStore()) &&
253 (unsigned)RegUseIndex < UseMCID.getNumOperands() &&
254 UseMCID.OpInfo[RegUseIndex].isLookupPtrRegClass())
255 LDataLatency += SpecialAddressLatency;
256 }
257 // Adjust the dependence latency using operand def/use
258 // information (if any), and then allow the target to
259 // perform its own adjustments.
260 const SDep& dep = SDep(SU, SDep::Data, LDataLatency, *Alias);
261 if (!UnitLatencies) {
Andrew Trick953be892012-03-07 23:00:49 +0000262 computeOperandLatency(SU, UseSU, const_cast<SDep &>(dep));
Andrew Trick81a682a2012-02-23 01:52:38 +0000263 ST.adjustSchedDependency(SU, UseSU, const_cast<SDep &>(dep));
264 }
265 UseSU->addPred(dep);
266 }
267 }
268}
269
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000270/// addPhysRegDeps - Add register dependencies (data, anti, and output) from
271/// this SUnit to following instructions in the same scheduling region that
272/// depend the physical register referenced at OperIdx.
273void ScheduleDAGInstrs::addPhysRegDeps(SUnit *SU, unsigned OperIdx) {
274 const MachineInstr *MI = SU->getInstr();
275 const MachineOperand &MO = MI->getOperand(OperIdx);
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000276
277 // Optionally add output and anti dependencies. For anti
278 // dependencies we use a latency of 0 because for a multi-issue
279 // target we want to allow the defining instruction to issue
280 // in the same cycle as the using instruction.
281 // TODO: Using a latency of 1 here for output dependencies assumes
282 // there's no cost for reusing registers.
283 SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
Craig Toppere4fd9072012-03-04 10:43:23 +0000284 for (const uint16_t *Alias = TRI->getOverlaps(MO.getReg()); *Alias; ++Alias) {
Andrew Trick702d4892012-02-24 07:04:55 +0000285 if (!Defs.contains(*Alias))
Andrew Trick81a682a2012-02-23 01:52:38 +0000286 continue;
Andrew Trick702d4892012-02-24 07:04:55 +0000287 std::vector<SUnit *> &DefList = Defs[*Alias];
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000288 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
289 SUnit *DefSU = DefList[i];
290 if (DefSU == &ExitSU)
291 continue;
292 if (DefSU != SU &&
293 (Kind != SDep::Output || !MO.isDead() ||
294 !DefSU->getInstr()->registerDefIsDead(*Alias))) {
295 if (Kind == SDep::Anti)
296 DefSU->addPred(SDep(SU, Kind, 0, /*Reg=*/*Alias));
297 else {
298 unsigned AOLat = TII->getOutputLatency(InstrItins, MI, OperIdx,
299 DefSU->getInstr());
300 DefSU->addPred(SDep(SU, Kind, AOLat, /*Reg=*/*Alias));
301 }
302 }
303 }
304 }
305
Andrew Trick81a682a2012-02-23 01:52:38 +0000306 if (!MO.isDef()) {
307 // Either insert a new Reg2SUnits entry with an empty SUnits list, or
308 // retrieve the existing SUnits list for this register's uses.
309 // Push this SUnit on the use list.
Andrew Trick702d4892012-02-24 07:04:55 +0000310 Uses[MO.getReg()].push_back(SU);
Andrew Trick81a682a2012-02-23 01:52:38 +0000311 }
312 else {
313 addPhysRegDataDeps(SU, MO);
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000314
Andrew Trick81a682a2012-02-23 01:52:38 +0000315 // Either insert a new Reg2SUnits entry with an empty SUnits list, or
316 // retrieve the existing SUnits list for this register's defs.
Andrew Trick702d4892012-02-24 07:04:55 +0000317 std::vector<SUnit *> &DefList = Defs[MO.getReg()];
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000318
319 // If a def is going to wrap back around to the top of the loop,
320 // backschedule it.
321 if (!UnitLatencies && DefList.empty()) {
Andrew Trick81a682a2012-02-23 01:52:38 +0000322 LoopDependencies::LoopDeps::iterator I = LoopRegs.Deps.find(MO.getReg());
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000323 if (I != LoopRegs.Deps.end()) {
324 const MachineOperand *UseMO = I->second.first;
325 unsigned Count = I->second.second;
326 const MachineInstr *UseMI = UseMO->getParent();
327 unsigned UseMOIdx = UseMO - &UseMI->getOperand(0);
328 const MCInstrDesc &UseMCID = UseMI->getDesc();
Andrew Trick81a682a2012-02-23 01:52:38 +0000329 const TargetSubtargetInfo &ST =
330 TM.getSubtarget<TargetSubtargetInfo>();
331 unsigned SpecialAddressLatency = ST.getSpecialAddressLatency();
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000332 // TODO: If we knew the total depth of the region here, we could
333 // handle the case where the whole loop is inside the region but
334 // is large enough that the isScheduleHigh trick isn't needed.
335 if (UseMOIdx < UseMCID.getNumOperands()) {
336 // Currently, we only support scheduling regions consisting of
337 // single basic blocks. Check to see if the instruction is in
338 // the same region by checking to see if it has the same parent.
339 if (UseMI->getParent() != MI->getParent()) {
340 unsigned Latency = SU->Latency;
341 if (UseMCID.OpInfo[UseMOIdx].isLookupPtrRegClass())
342 Latency += SpecialAddressLatency;
343 // This is a wild guess as to the portion of the latency which
344 // will be overlapped by work done outside the current
345 // scheduling region.
346 Latency -= std::min(Latency, Count);
347 // Add the artificial edge.
348 ExitSU.addPred(SDep(SU, SDep::Order, Latency,
349 /*Reg=*/0, /*isNormalMemory=*/false,
350 /*isMustAlias=*/false,
351 /*isArtificial=*/true));
352 } else if (SpecialAddressLatency > 0 &&
353 UseMCID.OpInfo[UseMOIdx].isLookupPtrRegClass()) {
354 // The entire loop body is within the current scheduling region
355 // and the latency of this operation is assumed to be greater
356 // than the latency of the loop.
357 // TODO: Recursively mark data-edge predecessors as
358 // isScheduleHigh too.
359 SU->isScheduleHigh = true;
360 }
361 }
362 LoopRegs.Deps.erase(I);
363 }
364 }
365
Andrew Trick81a682a2012-02-23 01:52:38 +0000366 // clear this register's use list
Andrew Trick702d4892012-02-24 07:04:55 +0000367 if (Uses.contains(MO.getReg()))
368 Uses[MO.getReg()].clear();
Andrew Trick81a682a2012-02-23 01:52:38 +0000369
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000370 if (!MO.isDead())
371 DefList.clear();
372
373 // Calls will not be reordered because of chain dependencies (see
374 // below). Since call operands are dead, calls may continue to be added
375 // to the DefList making dependence checking quadratic in the size of
376 // the block. Instead, we leave only one call at the back of the
377 // DefList.
378 if (SU->isCall) {
379 while (!DefList.empty() && DefList.back()->isCall)
380 DefList.pop_back();
381 }
Andrew Trick81a682a2012-02-23 01:52:38 +0000382 // Defs are pushed in the order they are visited and never reordered.
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000383 DefList.push_back(SU);
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000384 }
385}
386
Andrew Trick3c58ba82012-01-14 02:17:18 +0000387/// addVRegDefDeps - Add register output and data dependencies from this SUnit
388/// to instructions that occur later in the same scheduling region if they read
389/// from or write to the virtual register defined at OperIdx.
390///
391/// TODO: Hoist loop induction variable increments. This has to be
392/// reevaluated. Generally, IV scheduling should be done before coalescing.
393void ScheduleDAGInstrs::addVRegDefDeps(SUnit *SU, unsigned OperIdx) {
394 const MachineInstr *MI = SU->getInstr();
395 unsigned Reg = MI->getOperand(OperIdx).getReg();
396
Andrew Trickcc77b542012-02-22 06:08:13 +0000397 // SSA defs do not have output/anti dependencies.
Andrew Trick2fc09772012-02-22 18:34:49 +0000398 // The current operand is a def, so we have at least one.
Andrew Trickcc77b542012-02-22 06:08:13 +0000399 if (llvm::next(MRI.def_begin(Reg)) == MRI.def_end())
400 return;
401
Andrew Trick3c58ba82012-01-14 02:17:18 +0000402 // Add output dependence to the next nearest def of this vreg.
403 //
404 // Unless this definition is dead, the output dependence should be
405 // transitively redundant with antidependencies from this definition's
406 // uses. We're conservative for now until we have a way to guarantee the uses
407 // are not eliminated sometime during scheduling. The output dependence edge
408 // is also useful if output latency exceeds def-use latency.
Andrew Trick8ae3ac72012-02-22 21:59:00 +0000409 VReg2SUnitMap::iterator DefI = findVRegDef(Reg);
410 if (DefI == VRegDefs.end())
411 VRegDefs.insert(VReg2SUnit(Reg, SU));
412 else {
413 SUnit *DefSU = DefI->SU;
414 if (DefSU != SU && DefSU != &ExitSU) {
415 unsigned OutLatency = TII->getOutputLatency(InstrItins, MI, OperIdx,
416 DefSU->getInstr());
417 DefSU->addPred(SDep(SU, SDep::Output, OutLatency, Reg));
418 }
419 DefI->SU = SU;
Andrew Trick3c58ba82012-01-14 02:17:18 +0000420 }
Andrew Trick3c58ba82012-01-14 02:17:18 +0000421}
422
Andrew Trickb4566a92012-02-22 06:08:11 +0000423/// addVRegUseDeps - Add a register data dependency if the instruction that
424/// defines the virtual register used at OperIdx is mapped to an SUnit. Add a
425/// register antidependency from this SUnit to instructions that occur later in
426/// the same scheduling region if they write the virtual register.
427///
428/// TODO: Handle ExitSU "uses" properly.
Andrew Trick3c58ba82012-01-14 02:17:18 +0000429void ScheduleDAGInstrs::addVRegUseDeps(SUnit *SU, unsigned OperIdx) {
Andrew Trickb4566a92012-02-22 06:08:11 +0000430 MachineInstr *MI = SU->getInstr();
431 unsigned Reg = MI->getOperand(OperIdx).getReg();
432
433 // Lookup this operand's reaching definition.
434 assert(LIS && "vreg dependencies requires LiveIntervals");
Andrew Trick63d578b2012-02-23 03:16:24 +0000435 SlotIndex UseIdx = LIS->getInstructionIndex(MI).getRegSlot();
Andrew Trickb4566a92012-02-22 06:08:11 +0000436 LiveInterval *LI = &LIS->getInterval(Reg);
Andrew Trick63d578b2012-02-23 03:16:24 +0000437 VNInfo *VNI = LI->getVNInfoBefore(UseIdx);
438 // VNI will be valid because MachineOperand::readsReg() is checked by caller.
Andrew Trickb4566a92012-02-22 06:08:11 +0000439 MachineInstr *Def = LIS->getInstructionFromIndex(VNI->def);
Andrew Trick63d578b2012-02-23 03:16:24 +0000440 // Phis and other noninstructions (after coalescing) have a NULL Def.
Andrew Trickb4566a92012-02-22 06:08:11 +0000441 if (Def) {
442 SUnit *DefSU = getSUnit(Def);
443 if (DefSU) {
444 // The reaching Def lives within this scheduling region.
445 // Create a data dependence.
446 //
447 // TODO: Handle "special" address latencies cleanly.
448 const SDep &dep = SDep(DefSU, SDep::Data, DefSU->Latency, Reg);
449 if (!UnitLatencies) {
450 // Adjust the dependence latency using operand def/use information, then
451 // allow the target to perform its own adjustments.
Andrew Trick953be892012-03-07 23:00:49 +0000452 computeOperandLatency(DefSU, SU, const_cast<SDep &>(dep));
Andrew Trickb4566a92012-02-22 06:08:11 +0000453 const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>();
454 ST.adjustSchedDependency(DefSU, SU, const_cast<SDep &>(dep));
455 }
456 SU->addPred(dep);
457 }
458 }
Andrew Trick3c58ba82012-01-14 02:17:18 +0000459
460 // Add antidependence to the following def of the vreg it uses.
Andrew Trick8ae3ac72012-02-22 21:59:00 +0000461 VReg2SUnitMap::iterator DefI = findVRegDef(Reg);
462 if (DefI != VRegDefs.end() && DefI->SU != SU)
463 DefI->SU->addPred(SDep(SU, SDep::Anti, 0, Reg));
Andrew Trickb4566a92012-02-22 06:08:11 +0000464}
Andrew Trick3c58ba82012-01-14 02:17:18 +0000465
Andrew Trickb4566a92012-02-22 06:08:11 +0000466/// Create an SUnit for each real instruction, numbered in top-down toplological
467/// order. The instruction order A < B, implies that no edge exists from B to A.
468///
469/// Map each real instruction to its SUnit.
470///
471/// After initSUnits, the SUnits vector is cannot be resized and the scheduler
472/// may hang onto SUnit pointers. We may relax this in the future by using SUnit
473/// IDs instead of pointers.
474void ScheduleDAGInstrs::initSUnits() {
475 // We'll be allocating one SUnit for each real instruction in the region,
476 // which is contained within a basic block.
477 SUnits.reserve(BB->size());
478
Andrew Trickcf46b5a2012-03-07 23:00:52 +0000479 for (MachineBasicBlock::iterator I = Begin; I != End; ++I) {
Andrew Trickb4566a92012-02-22 06:08:11 +0000480 MachineInstr *MI = I;
481 if (MI->isDebugValue())
482 continue;
483
Andrew Trick953be892012-03-07 23:00:49 +0000484 SUnit *SU = newSUnit(MI);
Andrew Trickb4566a92012-02-22 06:08:11 +0000485 MISUnitMap[MI] = SU;
486
487 SU->isCall = MI->isCall();
488 SU->isCommutable = MI->isCommutable();
489
490 // Assign the Latency field of SU using target-provided information.
491 if (UnitLatencies)
492 SU->Latency = 1;
493 else
Andrew Trick953be892012-03-07 23:00:49 +0000494 computeLatency(SU);
Andrew Trickb4566a92012-02-22 06:08:11 +0000495 }
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000496}
497
Andrew Trick953be892012-03-07 23:00:49 +0000498void ScheduleDAGInstrs::buildSchedGraph(AliasAnalysis *AA) {
Andrew Trickb4566a92012-02-22 06:08:11 +0000499 // Create an SUnit for each real instruction.
500 initSUnits();
Dan Gohman343f0c02008-11-19 23:18:57 +0000501
Dan Gohman6a9041e2008-12-04 01:35:46 +0000502 // We build scheduling units by walking a block's instruction list from bottom
503 // to top.
504
David Goodwin980d4942009-11-09 19:22:17 +0000505 // Remember where a generic side-effecting instruction is as we procede.
506 SUnit *BarrierChain = 0, *AliasChain = 0;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000507
David Goodwin980d4942009-11-09 19:22:17 +0000508 // Memory references to specific known memory locations are tracked
509 // so that they can be given more precise dependencies. We track
510 // separately the known memory locations that may alias and those
511 // that are known not to alias
512 std::map<const Value *, SUnit *> AliasMemDefs, NonAliasMemDefs;
513 std::map<const Value *, std::vector<SUnit *> > AliasMemUses, NonAliasMemUses;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000514
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000515 // Remove any stale debug info; sometimes BuildSchedGraph is called again
516 // without emitting the info from the previous call.
Devang Patelcf4cc842011-06-02 20:07:12 +0000517 DbgValues.clear();
518 FirstDbgValue = NULL;
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000519
Andrew Trick81a682a2012-02-23 01:52:38 +0000520 assert(Defs.empty() && Uses.empty() &&
521 "Only BuildGraph should update Defs/Uses");
Andrew Trick702d4892012-02-24 07:04:55 +0000522 Defs.setRegLimit(TRI->getNumRegs());
523 Uses.setRegLimit(TRI->getNumRegs());
Andrew Trick9b668532011-05-06 21:52:52 +0000524
Andrew Trick8ae3ac72012-02-22 21:59:00 +0000525 assert(VRegDefs.empty() && "Only BuildSchedGraph may access VRegDefs");
526 // FIXME: Allow SparseSet to reserve space for the creation of virtual
527 // registers during scheduling. Don't artificially inflate the Universe
528 // because we want to assert that vregs are not created during DAG building.
529 VRegDefs.setUniverse(MRI.getNumVirtRegs());
Andrew Trick3c58ba82012-01-14 02:17:18 +0000530
Andrew Trick81a682a2012-02-23 01:52:38 +0000531 // Model data dependencies between instructions being scheduled and the
532 // ExitSU.
Andrew Trick953be892012-03-07 23:00:49 +0000533 addSchedBarrierDeps();
Andrew Trick81a682a2012-02-23 01:52:38 +0000534
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000535 // Walk the list of instructions, from bottom moving up.
Devang Patelcf4cc842011-06-02 20:07:12 +0000536 MachineInstr *PrevMI = NULL;
Andrew Trickcf46b5a2012-03-07 23:00:52 +0000537 for (MachineBasicBlock::iterator MII = End, MIE = Begin;
Dan Gohman343f0c02008-11-19 23:18:57 +0000538 MII != MIE; --MII) {
539 MachineInstr *MI = prior(MII);
Devang Patelcf4cc842011-06-02 20:07:12 +0000540 if (MI && PrevMI) {
541 DbgValues.push_back(std::make_pair(PrevMI, MI));
542 PrevMI = NULL;
543 }
544
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000545 if (MI->isDebugValue()) {
Devang Patelcf4cc842011-06-02 20:07:12 +0000546 PrevMI = MI;
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000547 continue;
548 }
Devang Patelcf4cc842011-06-02 20:07:12 +0000549
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000550 assert(!MI->isTerminator() && !MI->isLabel() &&
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000551 "Cannot schedule terminators or labels!");
Dan Gohman343f0c02008-11-19 23:18:57 +0000552
Andrew Trickb4566a92012-02-22 06:08:11 +0000553 SUnit *SU = MISUnitMap[MI];
554 assert(SU && "No SUnit mapped to this MI");
Dan Gohman54e4c362008-12-09 22:54:47 +0000555
Dan Gohman6a9041e2008-12-04 01:35:46 +0000556 // Add register-based dependencies (data, anti, and output).
Dan Gohman343f0c02008-11-19 23:18:57 +0000557 for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) {
558 const MachineOperand &MO = MI->getOperand(j);
559 if (!MO.isReg()) continue;
560 unsigned Reg = MO.getReg();
561 if (Reg == 0) continue;
562
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000563 if (TRI->isPhysicalRegister(Reg))
564 addPhysRegDeps(SU, j);
565 else {
566 assert(!IsPostRA && "Virtual register encountered!");
Andrew Trick3c58ba82012-01-14 02:17:18 +0000567 if (MO.isDef())
568 addVRegDefDeps(SU, j);
Andrew Trick63d578b2012-02-23 03:16:24 +0000569 else if (MO.readsReg()) // ignore undef operands
Andrew Trick3c58ba82012-01-14 02:17:18 +0000570 addVRegUseDeps(SU, j);
Dan Gohman343f0c02008-11-19 23:18:57 +0000571 }
572 }
Dan Gohman6a9041e2008-12-04 01:35:46 +0000573
574 // Add chain dependencies.
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000575 // Chain dependencies used to enforce memory order should have
576 // latency of 0 (except for true dependency of Store followed by
577 // aliased Load... we estimate that with a single cycle of latency
578 // assuming the hardware will bypass)
Dan Gohman6a9041e2008-12-04 01:35:46 +0000579 // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable
580 // after stack slots are lowered to actual addresses.
581 // TODO: Use an AliasAnalysis and do real alias-analysis queries, and
582 // produce more precise dependence information.
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000583#define STORE_LOAD_LATENCY 1
584 unsigned TrueMemOrderLatency = 0;
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000585 if (MI->isCall() || MI->hasUnmodeledSideEffects() ||
Andrew Trickf405b1a2011-05-05 19:24:06 +0000586 (MI->hasVolatileMemoryRef() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000587 (!MI->mayLoad() || !MI->isInvariantLoad(AA)))) {
David Goodwin980d4942009-11-09 19:22:17 +0000588 // Be conservative with these and add dependencies on all memory
589 // references, even those that are known to not alias.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000590 for (std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000591 NonAliasMemDefs.begin(), E = NonAliasMemDefs.end(); I != E; ++I) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000592 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000593 }
594 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000595 NonAliasMemUses.begin(), E = NonAliasMemUses.end(); I != E; ++I) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000596 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000597 I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000598 }
David Goodwin980d4942009-11-09 19:22:17 +0000599 NonAliasMemDefs.clear();
600 NonAliasMemUses.clear();
601 // Add SU to the barrier chain.
602 if (BarrierChain)
603 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
604 BarrierChain = SU;
605
606 // fall-through
607 new_alias_chain:
608 // Chain all possibly aliasing memory references though SU.
609 if (AliasChain)
610 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
611 AliasChain = SU;
612 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
613 PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
614 for (std::map<const Value *, SUnit *>::iterator I = AliasMemDefs.begin(),
615 E = AliasMemDefs.end(); I != E; ++I) {
616 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
617 }
618 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
619 AliasMemUses.begin(), E = AliasMemUses.end(); I != E; ++I) {
620 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
621 I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
622 }
623 PendingLoads.clear();
624 AliasMemDefs.clear();
625 AliasMemUses.clear();
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000626 } else if (MI->mayStore()) {
David Goodwina9e61072009-11-03 20:15:00 +0000627 bool MayAlias = true;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000628 TrueMemOrderLatency = STORE_LOAD_LATENCY;
David Goodwina9e61072009-11-03 20:15:00 +0000629 if (const Value *V = getUnderlyingObjectForInstr(MI, MFI, MayAlias)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000630 // A store to a specific PseudoSourceValue. Add precise dependencies.
David Goodwin980d4942009-11-09 19:22:17 +0000631 // Record the def in MemDefs, first adding a dep if there is
632 // an existing def.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000633 std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000634 ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000635 std::map<const Value *, SUnit *>::iterator IE =
David Goodwin980d4942009-11-09 19:22:17 +0000636 ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end());
637 if (I != IE) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000638 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0,
Dan Gohman54e4c362008-12-09 22:54:47 +0000639 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000640 I->second = SU;
641 } else {
David Goodwin980d4942009-11-09 19:22:17 +0000642 if (MayAlias)
643 AliasMemDefs[V] = SU;
644 else
645 NonAliasMemDefs[V] = SU;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000646 }
647 // Handle the uses in MemUses, if there are any.
Dan Gohmana629b482008-12-08 17:50:35 +0000648 std::map<const Value *, std::vector<SUnit *> >::iterator J =
David Goodwin980d4942009-11-09 19:22:17 +0000649 ((MayAlias) ? AliasMemUses.find(V) : NonAliasMemUses.find(V));
650 std::map<const Value *, std::vector<SUnit *> >::iterator JE =
651 ((MayAlias) ? AliasMemUses.end() : NonAliasMemUses.end());
652 if (J != JE) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000653 for (unsigned i = 0, e = J->second.size(); i != e; ++i)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000654 J->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency,
655 /*Reg=*/0, /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000656 J->second.clear();
657 }
David Goodwina9e61072009-11-03 20:15:00 +0000658 if (MayAlias) {
David Goodwin980d4942009-11-09 19:22:17 +0000659 // Add dependencies from all the PendingLoads, i.e. loads
660 // with no underlying object.
David Goodwina9e61072009-11-03 20:15:00 +0000661 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
662 PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
David Goodwin980d4942009-11-09 19:22:17 +0000663 // Add dependence on alias chain, if needed.
664 if (AliasChain)
665 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
David Goodwina9e61072009-11-03 20:15:00 +0000666 }
David Goodwin980d4942009-11-09 19:22:17 +0000667 // Add dependence on barrier chain, if needed.
668 if (BarrierChain)
669 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
David Goodwin5be870a2009-11-05 00:16:44 +0000670 } else {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000671 // Treat all other stores conservatively.
David Goodwin980d4942009-11-09 19:22:17 +0000672 goto new_alias_chain;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000673 }
Evan Chengec6906b2010-10-23 02:10:46 +0000674
675 if (!ExitSU.isPred(SU))
676 // Push store's up a bit to avoid them getting in between cmp
677 // and branches.
678 ExitSU.addPred(SDep(SU, SDep::Order, 0,
679 /*Reg=*/0, /*isNormalMemory=*/false,
680 /*isMustAlias=*/false,
681 /*isArtificial=*/true));
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000682 } else if (MI->mayLoad()) {
David Goodwina9e61072009-11-03 20:15:00 +0000683 bool MayAlias = true;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000684 TrueMemOrderLatency = 0;
Dan Gohmana70dca12009-10-09 23:27:56 +0000685 if (MI->isInvariantLoad(AA)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000686 // Invariant load, no chain dependencies needed!
David Goodwin5be870a2009-11-05 00:16:44 +0000687 } else {
Andrew Trickf405b1a2011-05-05 19:24:06 +0000688 if (const Value *V =
David Goodwin980d4942009-11-09 19:22:17 +0000689 getUnderlyingObjectForInstr(MI, MFI, MayAlias)) {
690 // A load from a specific PseudoSourceValue. Add precise dependencies.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000691 std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000692 ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000693 std::map<const Value *, SUnit *>::iterator IE =
David Goodwin980d4942009-11-09 19:22:17 +0000694 ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end());
695 if (I != IE)
696 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0,
697 /*isNormalMemory=*/true));
698 if (MayAlias)
699 AliasMemUses[V].push_back(SU);
Andrew Trickf405b1a2011-05-05 19:24:06 +0000700 else
David Goodwin980d4942009-11-09 19:22:17 +0000701 NonAliasMemUses[V].push_back(SU);
702 } else {
703 // A load with no underlying object. Depend on all
704 // potentially aliasing stores.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000705 for (std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000706 AliasMemDefs.begin(), E = AliasMemDefs.end(); I != E; ++I)
707 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000708
David Goodwin980d4942009-11-09 19:22:17 +0000709 PendingLoads.push_back(SU);
710 MayAlias = true;
David Goodwina9e61072009-11-03 20:15:00 +0000711 }
Andrew Trickf405b1a2011-05-05 19:24:06 +0000712
David Goodwin980d4942009-11-09 19:22:17 +0000713 // Add dependencies on alias and barrier chains, if needed.
714 if (MayAlias && AliasChain)
715 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
716 if (BarrierChain)
717 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000718 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000719 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000720 }
Devang Patelcf4cc842011-06-02 20:07:12 +0000721 if (PrevMI)
722 FirstDbgValue = PrevMI;
Dan Gohman79ce2762009-01-15 19:20:50 +0000723
Andrew Trick81a682a2012-02-23 01:52:38 +0000724 Defs.clear();
725 Uses.clear();
Andrew Trick3c58ba82012-01-14 02:17:18 +0000726 VRegDefs.clear();
Dan Gohman79ce2762009-01-15 19:20:50 +0000727 PendingLoads.clear();
Andrew Trickb4566a92012-02-22 06:08:11 +0000728 MISUnitMap.clear();
Dan Gohman343f0c02008-11-19 23:18:57 +0000729}
730
Andrew Trick953be892012-03-07 23:00:49 +0000731void ScheduleDAGInstrs::computeLatency(SUnit *SU) {
David Goodwind94a4e52009-08-10 15:55:25 +0000732 // Compute the latency for the node.
Evan Cheng3ef1c872010-09-10 01:29:16 +0000733 if (!InstrItins || InstrItins->isEmpty()) {
734 SU->Latency = 1;
Dan Gohman4ea8e852008-12-16 02:38:22 +0000735
Evan Cheng3ef1c872010-09-10 01:29:16 +0000736 // Simplistic target-independent heuristic: assume that loads take
737 // extra time.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000738 if (SU->getInstr()->mayLoad())
Dan Gohman4ea8e852008-12-16 02:38:22 +0000739 SU->Latency += 2;
Evan Cheng8239daf2010-11-03 00:45:17 +0000740 } else {
741 SU->Latency = TII->getInstrLatency(InstrItins, SU->getInstr());
742 }
Dan Gohmanc8c28272008-11-21 00:12:10 +0000743}
744
Andrew Trick953be892012-03-07 23:00:49 +0000745void ScheduleDAGInstrs::computeOperandLatency(SUnit *Def, SUnit *Use,
David Goodwindc4bdcd2009-08-19 16:08:58 +0000746 SDep& dep) const {
Evan Cheng3ef1c872010-09-10 01:29:16 +0000747 if (!InstrItins || InstrItins->isEmpty())
David Goodwindc4bdcd2009-08-19 16:08:58 +0000748 return;
Andrew Trickf405b1a2011-05-05 19:24:06 +0000749
David Goodwindc4bdcd2009-08-19 16:08:58 +0000750 // For a data dependency with a known register...
751 if ((dep.getKind() != SDep::Data) || (dep.getReg() == 0))
752 return;
753
754 const unsigned Reg = dep.getReg();
755
756 // ... find the definition of the register in the defining
757 // instruction
758 MachineInstr *DefMI = Def->getInstr();
759 int DefIdx = DefMI->findRegisterDefOperandIdx(Reg);
760 if (DefIdx != -1) {
Evan Cheng1aca5bc2010-10-08 18:42:25 +0000761 const MachineOperand &MO = DefMI->getOperand(DefIdx);
762 if (MO.isReg() && MO.isImplicit() &&
Evan Chengd82de832010-10-08 23:01:57 +0000763 DefIdx >= (int)DefMI->getDesc().getNumOperands()) {
Evan Cheng1aca5bc2010-10-08 18:42:25 +0000764 // This is an implicit def, getOperandLatency() won't return the correct
765 // latency. e.g.
766 // %D6<def>, %D7<def> = VLD1q16 %R2<kill>, 0, ..., %Q3<imp-def>
767 // %Q1<def> = VMULv8i16 %Q1<kill>, %Q3<kill>, ...
768 // What we want is to compute latency between def of %D6/%D7 and use of
769 // %Q3 instead.
Jakob Stoklund Olesen02634be2012-02-22 22:52:52 +0000770 unsigned Op2 = DefMI->findRegisterDefOperandIdx(Reg, false, true, TRI);
771 if (DefMI->getOperand(Op2).isReg())
772 DefIdx = Op2;
Evan Cheng1aca5bc2010-10-08 18:42:25 +0000773 }
Evan Chenga0792de2010-10-06 06:27:31 +0000774 MachineInstr *UseMI = Use->getInstr();
Evan Cheng3881cb72010-09-29 22:42:35 +0000775 // For all uses of the register, calculate the maxmimum latency
776 int Latency = -1;
Evan Chengec6906b2010-10-23 02:10:46 +0000777 if (UseMI) {
778 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
779 const MachineOperand &MO = UseMI->getOperand(i);
780 if (!MO.isReg() || !MO.isUse())
781 continue;
782 unsigned MOReg = MO.getReg();
783 if (MOReg != Reg)
784 continue;
David Goodwindc4bdcd2009-08-19 16:08:58 +0000785
Evan Chengec6906b2010-10-23 02:10:46 +0000786 int UseCycle = TII->getOperandLatency(InstrItins, DefMI, DefIdx,
787 UseMI, i);
788 Latency = std::max(Latency, UseCycle);
789 }
790 } else {
791 // UseMI is null, then it must be a scheduling barrier.
792 if (!InstrItins || InstrItins->isEmpty())
793 return;
794 unsigned DefClass = DefMI->getDesc().getSchedClass();
795 Latency = InstrItins->getOperandCycle(DefClass, DefIdx);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000796 }
Evan Chengec6906b2010-10-23 02:10:46 +0000797
798 // If we found a latency, then replace the existing dependence latency.
799 if (Latency >= 0)
800 dep.setLatency(Latency);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000801 }
802}
803
Dan Gohman343f0c02008-11-19 23:18:57 +0000804void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
805 SU->getInstr()->dump();
806}
807
808std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
809 std::string s;
810 raw_string_ostream oss(s);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000811 if (SU == &EntrySU)
812 oss << "<entry>";
813 else if (SU == &ExitSU)
814 oss << "<exit>";
815 else
816 SU->getInstr()->print(oss);
Dan Gohman343f0c02008-11-19 23:18:57 +0000817 return oss.str();
818}
819
Andrew Trick56b94c52012-03-07 00:18:22 +0000820/// Return the basic block label. It is not necessarilly unique because a block
821/// contains multiple scheduling regions. But it is fine for visualization.
822std::string ScheduleDAGInstrs::getDAGName() const {
823 return "dag." + BB->getFullName();
824}