blob: ed85963cc8691ba876452d77d06d43d36212aa85 [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"
Andrew Trick006e1ab2012-04-24 17:56:43 +000016#include "RegisterPressure.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"
Andrew Tricked395c82012-03-07 23:01:06 +000025#include "llvm/CodeGen/ScheduleDAGInstrs.h"
Evan Chengab8be962011-06-29 01:14:12 +000026#include "llvm/MC/MCInstrItineraries.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000027#include "llvm/Target/TargetMachine.h"
28#include "llvm/Target/TargetInstrInfo.h"
29#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng5b1b44892011-07-01 21:01:15 +000030#include "llvm/Target/TargetSubtargetInfo.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/raw_ostream.h"
Dan Gohman3f237442008-12-16 03:25:46 +000033#include "llvm/ADT/SmallSet.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000034using namespace llvm;
35
Dan Gohman79ce2762009-01-15 19:20:50 +000036ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf,
Dan Gohman3f237442008-12-16 03:25:46 +000037 const MachineLoopInfo &mli,
Andrew Trick5e920d72012-01-14 02:17:12 +000038 const MachineDominatorTree &mdt,
Andrew Trickb4566a92012-02-22 06:08:11 +000039 bool IsPostRAFlag,
40 LiveIntervals *lis)
Evan Cheng3ef1c872010-09-10 01:29:16 +000041 : ScheduleDAG(mf), MLI(mli), MDT(mdt), MFI(mf.getFrameInfo()),
Andrew Trickd790cad2012-03-07 23:00:59 +000042 InstrItins(mf.getTarget().getInstrItineraryData()), LIS(lis),
Andrew Trick00707922012-04-13 23:29:54 +000043 IsPostRA(IsPostRAFlag), UnitLatencies(false), CanHandleTerminators(false),
44 LoopRegs(MLI, MDT), FirstDbgValue(0) {
Andrew Trickb4566a92012-02-22 06:08:11 +000045 assert((IsPostRA || LIS) && "PreRA scheduling requires LiveIntervals");
Devang Patelcf4cc842011-06-02 20:07:12 +000046 DbgValues.clear();
Andrew Trickcc77b542012-02-22 06:08:13 +000047 assert(!(IsPostRA && MRI.getNumVirtRegs()) &&
Andrew Trick19273ae2012-02-21 04:51:23 +000048 "Virtual registers must be removed prior to PostRA scheduling");
Evan Cheng38bdfc62009-10-18 19:58:47 +000049}
Dan Gohman343f0c02008-11-19 23:18:57 +000050
Dan Gohman3311a1f2009-01-30 02:49:14 +000051/// getUnderlyingObjectFromInt - This is the function that does the work of
52/// looking through basic ptrtoint+arithmetic+inttoptr sequences.
53static const Value *getUnderlyingObjectFromInt(const Value *V) {
54 do {
Dan Gohman8906f952009-07-17 20:58:59 +000055 if (const Operator *U = dyn_cast<Operator>(V)) {
Dan Gohman3311a1f2009-01-30 02:49:14 +000056 // If we find a ptrtoint, we can transfer control back to the
57 // regular getUnderlyingObjectFromInt.
Dan Gohman8906f952009-07-17 20:58:59 +000058 if (U->getOpcode() == Instruction::PtrToInt)
Dan Gohman3311a1f2009-01-30 02:49:14 +000059 return U->getOperand(0);
60 // If we find an add of a constant or a multiplied value, it's
61 // likely that the other operand will lead us to the base
62 // object. We don't have to worry about the case where the
Dan Gohman748f98f2009-08-07 01:26:06 +000063 // object address is somehow being computed by the multiply,
Dan Gohman3311a1f2009-01-30 02:49:14 +000064 // because our callers only care when the result is an
65 // identifibale object.
Dan Gohman8906f952009-07-17 20:58:59 +000066 if (U->getOpcode() != Instruction::Add ||
Dan Gohman3311a1f2009-01-30 02:49:14 +000067 (!isa<ConstantInt>(U->getOperand(1)) &&
Dan Gohman8906f952009-07-17 20:58:59 +000068 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul))
Dan Gohman3311a1f2009-01-30 02:49:14 +000069 return V;
70 V = U->getOperand(0);
71 } else {
72 return V;
73 }
Duncan Sands1df98592010-02-16 11:11:14 +000074 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
Dan Gohman3311a1f2009-01-30 02:49:14 +000075 } while (1);
76}
77
Dan Gohman5034dd32010-12-15 20:02:24 +000078/// getUnderlyingObject - This is a wrapper around GetUnderlyingObject
Dan Gohman3311a1f2009-01-30 02:49:14 +000079/// and adds support for basic ptrtoint+arithmetic+inttoptr sequences.
80static const Value *getUnderlyingObject(const Value *V) {
81 // First just call Value::getUnderlyingObject to let it do what it does.
82 do {
Dan Gohman5034dd32010-12-15 20:02:24 +000083 V = GetUnderlyingObject(V);
Dan Gohman3311a1f2009-01-30 02:49:14 +000084 // If it found an inttoptr, use special code to continue climing.
Dan Gohman8906f952009-07-17 20:58:59 +000085 if (Operator::getOpcode(V) != Instruction::IntToPtr)
Dan Gohman3311a1f2009-01-30 02:49:14 +000086 break;
87 const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
88 // If that succeeded in finding a pointer, continue the search.
Duncan Sands1df98592010-02-16 11:11:14 +000089 if (!O->getType()->isPointerTy())
Dan Gohman3311a1f2009-01-30 02:49:14 +000090 break;
91 V = O;
92 } while (1);
93 return V;
94}
95
96/// getUnderlyingObjectForInstr - If this machine instr has memory reference
97/// information and it can be tracked to a normal reference to a known
98/// object, return the Value for that object. Otherwise return null.
Evan Cheng38bdfc62009-10-18 19:58:47 +000099static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI,
David Goodwina9e61072009-11-03 20:15:00 +0000100 const MachineFrameInfo *MFI,
101 bool &MayAlias) {
102 MayAlias = true;
Dan Gohman3311a1f2009-01-30 02:49:14 +0000103 if (!MI->hasOneMemOperand() ||
Dan Gohmanc76909a2009-09-25 20:36:54 +0000104 !(*MI->memoperands_begin())->getValue() ||
105 (*MI->memoperands_begin())->isVolatile())
Dan Gohman3311a1f2009-01-30 02:49:14 +0000106 return 0;
107
Dan Gohmanc76909a2009-09-25 20:36:54 +0000108 const Value *V = (*MI->memoperands_begin())->getValue();
Dan Gohman3311a1f2009-01-30 02:49:14 +0000109 if (!V)
110 return 0;
111
112 V = getUnderlyingObject(V);
Evan Chengff89dcb2009-10-18 18:16:27 +0000113 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
114 // For now, ignore PseudoSourceValues which may alias LLVM IR values
115 // because the code that uses this function has no way to cope with
116 // such aliases.
Evan Cheng38bdfc62009-10-18 19:58:47 +0000117 if (PSV->isAliased(MFI))
Evan Chengff89dcb2009-10-18 18:16:27 +0000118 return 0;
Andrew Trickf405b1a2011-05-05 19:24:06 +0000119
David Goodwin980d4942009-11-09 19:22:17 +0000120 MayAlias = PSV->mayAlias(MFI);
Evan Chengff89dcb2009-10-18 18:16:27 +0000121 return V;
122 }
Dan Gohman3311a1f2009-01-30 02:49:14 +0000123
Evan Chengff89dcb2009-10-18 18:16:27 +0000124 if (isIdentifiedObject(V))
125 return V;
126
127 return 0;
Dan Gohman3311a1f2009-01-30 02:49:14 +0000128}
129
Andrew Trick918f38a2012-04-20 20:05:21 +0000130void ScheduleDAGInstrs::startBlock(MachineBasicBlock *bb) {
131 BB = bb;
Andrew Tricke8deca82011-10-07 06:33:09 +0000132 LoopRegs.Deps.clear();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000133 if (MachineLoop *ML = MLI.getLoopFor(BB))
Evan Cheng977679d2012-01-07 03:02:36 +0000134 if (BB == ML->getLoopLatch())
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000135 LoopRegs.VisitLoop(ML);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000136}
137
Andrew Trick953be892012-03-07 23:00:49 +0000138void ScheduleDAGInstrs::finishBlock() {
Andrew Tricka30444a2012-04-20 20:24:33 +0000139 // Subclasses should no longer refer to the old block.
Andrew Trick918f38a2012-04-20 20:05:21 +0000140 BB = 0;
Andrew Trick47c14452012-03-07 05:21:52 +0000141}
142
Andrew Trick702d4892012-02-24 07:04:55 +0000143/// Initialize the map with the number of registers.
Andrew Trick035ec402012-03-07 23:00:57 +0000144void Reg2SUnitsMap::setRegLimit(unsigned Limit) {
Andrew Trick702d4892012-02-24 07:04:55 +0000145 PhysRegSet.setUniverse(Limit);
146 SUnits.resize(Limit);
147}
148
149/// Clear the map without deallocating storage.
Andrew Trick035ec402012-03-07 23:00:57 +0000150void Reg2SUnitsMap::clear() {
Andrew Trick702d4892012-02-24 07:04:55 +0000151 for (const_iterator I = reg_begin(), E = reg_end(); I != E; ++I) {
152 SUnits[*I].clear();
153 }
154 PhysRegSet.clear();
155}
156
Andrew Trick47c14452012-03-07 05:21:52 +0000157/// Initialize the DAG and common scheduler state for the current scheduling
158/// region. This does not actually create the DAG, only clears it. The
159/// scheduling driver may call BuildSchedGraph multiple times per scheduling
160/// region.
161void ScheduleDAGInstrs::enterRegion(MachineBasicBlock *bb,
162 MachineBasicBlock::iterator begin,
163 MachineBasicBlock::iterator end,
164 unsigned endcount) {
Andrew Trick918f38a2012-04-20 20:05:21 +0000165 assert(bb == BB && "startBlock should set BB");
Andrew Trick68675c62012-03-09 04:29:02 +0000166 RegionBegin = begin;
167 RegionEnd = end;
Andrew Trickcf46b5a2012-03-07 23:00:52 +0000168 EndIndex = endcount;
Andrew Trick17d35e52012-03-14 04:00:41 +0000169 MISUnitMap.clear();
Andrew Trick47c14452012-03-07 05:21:52 +0000170
171 // Check to see if the scheduler cares about latencies.
Andrew Trick953be892012-03-07 23:00:49 +0000172 UnitLatencies = forceUnitLatencies();
Andrew Trick47c14452012-03-07 05:21:52 +0000173
174 ScheduleDAG::clearDAG();
175}
176
177/// Close the current scheduling region. Don't clear any state in case the
178/// driver wants to refer to the previous scheduling region.
179void ScheduleDAGInstrs::exitRegion() {
180 // Nothing to do.
181}
182
Andrew Trick953be892012-03-07 23:00:49 +0000183/// addSchedBarrierDeps - Add dependencies from instructions in the current
Evan Chengec6906b2010-10-23 02:10:46 +0000184/// list of instructions being scheduled to scheduling barrier by adding
185/// the exit SU to the register defs and use list. This is because we want to
186/// make sure instructions which define registers that are either used by
187/// the terminator or are live-out are properly scheduled. This is
188/// especially important when the definition latency of the return value(s)
189/// are too high to be hidden by the branch or when the liveout registers
190/// used by instructions in the fallthrough block.
Andrew Trick953be892012-03-07 23:00:49 +0000191void ScheduleDAGInstrs::addSchedBarrierDeps() {
Andrew Trick68675c62012-03-09 04:29:02 +0000192 MachineInstr *ExitMI = RegionEnd != BB->end() ? &*RegionEnd : 0;
Evan Chengec6906b2010-10-23 02:10:46 +0000193 ExitSU.setInstr(ExitMI);
194 bool AllDepKnown = ExitMI &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000195 (ExitMI->isCall() || ExitMI->isBarrier());
Evan Chengec6906b2010-10-23 02:10:46 +0000196 if (ExitMI && AllDepKnown) {
197 // If it's a call or a barrier, add dependencies on the defs and uses of
198 // instruction.
199 for (unsigned i = 0, e = ExitMI->getNumOperands(); i != e; ++i) {
200 const MachineOperand &MO = ExitMI->getOperand(i);
201 if (!MO.isReg() || MO.isDef()) continue;
202 unsigned Reg = MO.getReg();
203 if (Reg == 0) continue;
204
Andrew Trick3c58ba82012-01-14 02:17:18 +0000205 if (TRI->isPhysicalRegister(Reg))
Andrew Trick702d4892012-02-24 07:04:55 +0000206 Uses[Reg].push_back(&ExitSU);
Andrew Trickd3a74862012-03-16 05:04:25 +0000207 else {
Andrew Trick3c58ba82012-01-14 02:17:18 +0000208 assert(!IsPostRA && "Virtual register encountered after regalloc.");
Andrew Trickd3a74862012-03-16 05:04:25 +0000209 addVRegUseDeps(&ExitSU, i);
210 }
Evan Chengec6906b2010-10-23 02:10:46 +0000211 }
212 } else {
213 // For others, e.g. fallthrough, conditional branch, assume the exit
Evan Chengde5fa932010-10-27 23:17:17 +0000214 // uses all the registers that are livein to the successor blocks.
Benjamin Kramera82d5262012-03-16 17:38:19 +0000215 assert(Uses.empty() && "Uses in set before adding deps?");
Evan Chengde5fa932010-10-27 23:17:17 +0000216 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
217 SE = BB->succ_end(); SI != SE; ++SI)
218 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
Andrew Trickf405b1a2011-05-05 19:24:06 +0000219 E = (*SI)->livein_end(); I != E; ++I) {
Evan Chengde5fa932010-10-27 23:17:17 +0000220 unsigned Reg = *I;
Benjamin Kramera82d5262012-03-16 17:38:19 +0000221 if (!Uses.contains(Reg))
Andrew Trick702d4892012-02-24 07:04:55 +0000222 Uses[Reg].push_back(&ExitSU);
Evan Chengde5fa932010-10-27 23:17:17 +0000223 }
Evan Chengec6906b2010-10-23 02:10:46 +0000224 }
225}
226
Andrew Trick81a682a2012-02-23 01:52:38 +0000227/// MO is an operand of SU's instruction that defines a physical register. Add
228/// data dependencies from SU to any uses of the physical register.
229void ScheduleDAGInstrs::addPhysRegDataDeps(SUnit *SU,
230 const MachineOperand &MO) {
231 assert(MO.isDef() && "expect physreg def");
232
233 // Ask the target if address-backscheduling is desirable, and if so how much.
234 const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>();
235 unsigned SpecialAddressLatency = ST.getSpecialAddressLatency();
236 unsigned DataLatency = SU->Latency;
237
Craig Toppere4fd9072012-03-04 10:43:23 +0000238 for (const uint16_t *Alias = TRI->getOverlaps(MO.getReg()); *Alias; ++Alias) {
Andrew Trick702d4892012-02-24 07:04:55 +0000239 if (!Uses.contains(*Alias))
Andrew Trick81a682a2012-02-23 01:52:38 +0000240 continue;
Andrew Trick702d4892012-02-24 07:04:55 +0000241 std::vector<SUnit*> &UseList = Uses[*Alias];
Andrew Trick81a682a2012-02-23 01:52:38 +0000242 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
243 SUnit *UseSU = UseList[i];
244 if (UseSU == SU)
245 continue;
246 unsigned LDataLatency = DataLatency;
247 // Optionally add in a special extra latency for nodes that
248 // feed addresses.
249 // TODO: Perhaps we should get rid of
250 // SpecialAddressLatency and just move this into
251 // adjustSchedDependency for the targets that care about it.
252 if (SpecialAddressLatency != 0 && !UnitLatencies &&
253 UseSU != &ExitSU) {
254 MachineInstr *UseMI = UseSU->getInstr();
255 const MCInstrDesc &UseMCID = UseMI->getDesc();
256 int RegUseIndex = UseMI->findRegisterUseOperandIdx(*Alias);
257 assert(RegUseIndex >= 0 && "UseMI doesn't use register!");
258 if (RegUseIndex >= 0 &&
259 (UseMI->mayLoad() || UseMI->mayStore()) &&
260 (unsigned)RegUseIndex < UseMCID.getNumOperands() &&
261 UseMCID.OpInfo[RegUseIndex].isLookupPtrRegClass())
262 LDataLatency += SpecialAddressLatency;
263 }
264 // Adjust the dependence latency using operand def/use
265 // information (if any), and then allow the target to
266 // perform its own adjustments.
267 const SDep& dep = SDep(SU, SDep::Data, LDataLatency, *Alias);
268 if (!UnitLatencies) {
Andrew Trick953be892012-03-07 23:00:49 +0000269 computeOperandLatency(SU, UseSU, const_cast<SDep &>(dep));
Andrew Trick81a682a2012-02-23 01:52:38 +0000270 ST.adjustSchedDependency(SU, UseSU, const_cast<SDep &>(dep));
271 }
272 UseSU->addPred(dep);
273 }
274 }
275}
276
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000277/// addPhysRegDeps - Add register dependencies (data, anti, and output) from
278/// this SUnit to following instructions in the same scheduling region that
279/// depend the physical register referenced at OperIdx.
280void ScheduleDAGInstrs::addPhysRegDeps(SUnit *SU, unsigned OperIdx) {
281 const MachineInstr *MI = SU->getInstr();
282 const MachineOperand &MO = MI->getOperand(OperIdx);
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000283
284 // Optionally add output and anti dependencies. For anti
285 // dependencies we use a latency of 0 because for a multi-issue
286 // target we want to allow the defining instruction to issue
287 // in the same cycle as the using instruction.
288 // TODO: Using a latency of 1 here for output dependencies assumes
289 // there's no cost for reusing registers.
290 SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
Craig Toppere4fd9072012-03-04 10:43:23 +0000291 for (const uint16_t *Alias = TRI->getOverlaps(MO.getReg()); *Alias; ++Alias) {
Andrew Trick702d4892012-02-24 07:04:55 +0000292 if (!Defs.contains(*Alias))
Andrew Trick81a682a2012-02-23 01:52:38 +0000293 continue;
Andrew Trick702d4892012-02-24 07:04:55 +0000294 std::vector<SUnit *> &DefList = Defs[*Alias];
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000295 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
296 SUnit *DefSU = DefList[i];
297 if (DefSU == &ExitSU)
298 continue;
299 if (DefSU != SU &&
300 (Kind != SDep::Output || !MO.isDead() ||
301 !DefSU->getInstr()->registerDefIsDead(*Alias))) {
302 if (Kind == SDep::Anti)
303 DefSU->addPred(SDep(SU, Kind, 0, /*Reg=*/*Alias));
304 else {
305 unsigned AOLat = TII->getOutputLatency(InstrItins, MI, OperIdx,
306 DefSU->getInstr());
307 DefSU->addPred(SDep(SU, Kind, AOLat, /*Reg=*/*Alias));
308 }
309 }
310 }
311 }
312
Andrew Trick81a682a2012-02-23 01:52:38 +0000313 if (!MO.isDef()) {
314 // Either insert a new Reg2SUnits entry with an empty SUnits list, or
315 // retrieve the existing SUnits list for this register's uses.
316 // Push this SUnit on the use list.
Andrew Trick702d4892012-02-24 07:04:55 +0000317 Uses[MO.getReg()].push_back(SU);
Andrew Trick81a682a2012-02-23 01:52:38 +0000318 }
319 else {
320 addPhysRegDataDeps(SU, MO);
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000321
Andrew Trick81a682a2012-02-23 01:52:38 +0000322 // Either insert a new Reg2SUnits entry with an empty SUnits list, or
323 // retrieve the existing SUnits list for this register's defs.
Andrew Trick702d4892012-02-24 07:04:55 +0000324 std::vector<SUnit *> &DefList = Defs[MO.getReg()];
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000325
326 // If a def is going to wrap back around to the top of the loop,
327 // backschedule it.
328 if (!UnitLatencies && DefList.empty()) {
Andrew Trick81a682a2012-02-23 01:52:38 +0000329 LoopDependencies::LoopDeps::iterator I = LoopRegs.Deps.find(MO.getReg());
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000330 if (I != LoopRegs.Deps.end()) {
331 const MachineOperand *UseMO = I->second.first;
332 unsigned Count = I->second.second;
333 const MachineInstr *UseMI = UseMO->getParent();
334 unsigned UseMOIdx = UseMO - &UseMI->getOperand(0);
335 const MCInstrDesc &UseMCID = UseMI->getDesc();
Andrew Trick81a682a2012-02-23 01:52:38 +0000336 const TargetSubtargetInfo &ST =
337 TM.getSubtarget<TargetSubtargetInfo>();
338 unsigned SpecialAddressLatency = ST.getSpecialAddressLatency();
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000339 // TODO: If we knew the total depth of the region here, we could
340 // handle the case where the whole loop is inside the region but
341 // is large enough that the isScheduleHigh trick isn't needed.
342 if (UseMOIdx < UseMCID.getNumOperands()) {
343 // Currently, we only support scheduling regions consisting of
344 // single basic blocks. Check to see if the instruction is in
345 // the same region by checking to see if it has the same parent.
346 if (UseMI->getParent() != MI->getParent()) {
347 unsigned Latency = SU->Latency;
348 if (UseMCID.OpInfo[UseMOIdx].isLookupPtrRegClass())
349 Latency += SpecialAddressLatency;
350 // This is a wild guess as to the portion of the latency which
351 // will be overlapped by work done outside the current
352 // scheduling region.
353 Latency -= std::min(Latency, Count);
354 // Add the artificial edge.
355 ExitSU.addPred(SDep(SU, SDep::Order, Latency,
356 /*Reg=*/0, /*isNormalMemory=*/false,
357 /*isMustAlias=*/false,
358 /*isArtificial=*/true));
359 } else if (SpecialAddressLatency > 0 &&
360 UseMCID.OpInfo[UseMOIdx].isLookupPtrRegClass()) {
361 // The entire loop body is within the current scheduling region
362 // and the latency of this operation is assumed to be greater
363 // than the latency of the loop.
364 // TODO: Recursively mark data-edge predecessors as
365 // isScheduleHigh too.
366 SU->isScheduleHigh = true;
367 }
368 }
369 LoopRegs.Deps.erase(I);
370 }
371 }
372
Andrew Trick81a682a2012-02-23 01:52:38 +0000373 // clear this register's use list
Andrew Trick702d4892012-02-24 07:04:55 +0000374 if (Uses.contains(MO.getReg()))
375 Uses[MO.getReg()].clear();
Andrew Trick81a682a2012-02-23 01:52:38 +0000376
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000377 if (!MO.isDead())
378 DefList.clear();
379
380 // Calls will not be reordered because of chain dependencies (see
381 // below). Since call operands are dead, calls may continue to be added
382 // to the DefList making dependence checking quadratic in the size of
383 // the block. Instead, we leave only one call at the back of the
384 // DefList.
385 if (SU->isCall) {
386 while (!DefList.empty() && DefList.back()->isCall)
387 DefList.pop_back();
388 }
Andrew Trick81a682a2012-02-23 01:52:38 +0000389 // Defs are pushed in the order they are visited and never reordered.
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000390 DefList.push_back(SU);
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000391 }
392}
393
Andrew Trick3c58ba82012-01-14 02:17:18 +0000394/// addVRegDefDeps - Add register output and data dependencies from this SUnit
395/// to instructions that occur later in the same scheduling region if they read
396/// from or write to the virtual register defined at OperIdx.
397///
398/// TODO: Hoist loop induction variable increments. This has to be
399/// reevaluated. Generally, IV scheduling should be done before coalescing.
400void ScheduleDAGInstrs::addVRegDefDeps(SUnit *SU, unsigned OperIdx) {
401 const MachineInstr *MI = SU->getInstr();
402 unsigned Reg = MI->getOperand(OperIdx).getReg();
403
Andrew Trickcc77b542012-02-22 06:08:13 +0000404 // SSA defs do not have output/anti dependencies.
Andrew Trick2fc09772012-02-22 18:34:49 +0000405 // The current operand is a def, so we have at least one.
Andrew Trickcc77b542012-02-22 06:08:13 +0000406 if (llvm::next(MRI.def_begin(Reg)) == MRI.def_end())
407 return;
408
Andrew Trick3c58ba82012-01-14 02:17:18 +0000409 // Add output dependence to the next nearest def of this vreg.
410 //
411 // Unless this definition is dead, the output dependence should be
412 // transitively redundant with antidependencies from this definition's
413 // uses. We're conservative for now until we have a way to guarantee the uses
414 // are not eliminated sometime during scheduling. The output dependence edge
415 // is also useful if output latency exceeds def-use latency.
Andrew Trickc0ccb8b2012-04-20 20:05:28 +0000416 VReg2SUnitMap::iterator DefI = VRegDefs.find(Reg);
Andrew Trick8ae3ac72012-02-22 21:59:00 +0000417 if (DefI == VRegDefs.end())
418 VRegDefs.insert(VReg2SUnit(Reg, SU));
419 else {
420 SUnit *DefSU = DefI->SU;
421 if (DefSU != SU && DefSU != &ExitSU) {
422 unsigned OutLatency = TII->getOutputLatency(InstrItins, MI, OperIdx,
423 DefSU->getInstr());
424 DefSU->addPred(SDep(SU, SDep::Output, OutLatency, Reg));
425 }
426 DefI->SU = SU;
Andrew Trick3c58ba82012-01-14 02:17:18 +0000427 }
Andrew Trick3c58ba82012-01-14 02:17:18 +0000428}
429
Andrew Trickb4566a92012-02-22 06:08:11 +0000430/// addVRegUseDeps - Add a register data dependency if the instruction that
431/// defines the virtual register used at OperIdx is mapped to an SUnit. Add a
432/// register antidependency from this SUnit to instructions that occur later in
433/// the same scheduling region if they write the virtual register.
434///
435/// TODO: Handle ExitSU "uses" properly.
Andrew Trick3c58ba82012-01-14 02:17:18 +0000436void ScheduleDAGInstrs::addVRegUseDeps(SUnit *SU, unsigned OperIdx) {
Andrew Trickb4566a92012-02-22 06:08:11 +0000437 MachineInstr *MI = SU->getInstr();
438 unsigned Reg = MI->getOperand(OperIdx).getReg();
439
440 // Lookup this operand's reaching definition.
441 assert(LIS && "vreg dependencies requires LiveIntervals");
Andrew Trick63d578b2012-02-23 03:16:24 +0000442 SlotIndex UseIdx = LIS->getInstructionIndex(MI).getRegSlot();
Andrew Trickb4566a92012-02-22 06:08:11 +0000443 LiveInterval *LI = &LIS->getInterval(Reg);
Andrew Trick63d578b2012-02-23 03:16:24 +0000444 VNInfo *VNI = LI->getVNInfoBefore(UseIdx);
Andrew Trickc3ad8852012-04-24 18:04:41 +0000445
446 // Special case: An early-clobber tied operand reads and writes the
447 // register one slot early. e.g. InlineAsm.
448 //
449 // FIXME: Same special case is in shrinkToUses. Hide under an API.
450 if (SlotIndex::isSameInstr(VNI->def, UseIdx)) {
451 UseIdx = VNI->def;
452 VNI = LI->getVNInfoBefore(UseIdx);
453 }
Andrew Trick63d578b2012-02-23 03:16:24 +0000454 // VNI will be valid because MachineOperand::readsReg() is checked by caller.
Andrew Trickb4566a92012-02-22 06:08:11 +0000455 MachineInstr *Def = LIS->getInstructionFromIndex(VNI->def);
Andrew Trick63d578b2012-02-23 03:16:24 +0000456 // Phis and other noninstructions (after coalescing) have a NULL Def.
Andrew Trickb4566a92012-02-22 06:08:11 +0000457 if (Def) {
458 SUnit *DefSU = getSUnit(Def);
459 if (DefSU) {
460 // The reaching Def lives within this scheduling region.
461 // Create a data dependence.
462 //
463 // TODO: Handle "special" address latencies cleanly.
464 const SDep &dep = SDep(DefSU, SDep::Data, DefSU->Latency, Reg);
465 if (!UnitLatencies) {
466 // Adjust the dependence latency using operand def/use information, then
467 // allow the target to perform its own adjustments.
Andrew Trick953be892012-03-07 23:00:49 +0000468 computeOperandLatency(DefSU, SU, const_cast<SDep &>(dep));
Andrew Trickb4566a92012-02-22 06:08:11 +0000469 const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>();
470 ST.adjustSchedDependency(DefSU, SU, const_cast<SDep &>(dep));
471 }
472 SU->addPred(dep);
473 }
474 }
Andrew Trick3c58ba82012-01-14 02:17:18 +0000475
476 // Add antidependence to the following def of the vreg it uses.
Andrew Trickc0ccb8b2012-04-20 20:05:28 +0000477 VReg2SUnitMap::iterator DefI = VRegDefs.find(Reg);
Andrew Trick8ae3ac72012-02-22 21:59:00 +0000478 if (DefI != VRegDefs.end() && DefI->SU != SU)
479 DefI->SU->addPred(SDep(SU, SDep::Anti, 0, Reg));
Andrew Trickb4566a92012-02-22 06:08:11 +0000480}
Andrew Trick3c58ba82012-01-14 02:17:18 +0000481
Andrew Trickb4566a92012-02-22 06:08:11 +0000482/// Create an SUnit for each real instruction, numbered in top-down toplological
483/// order. The instruction order A < B, implies that no edge exists from B to A.
484///
485/// Map each real instruction to its SUnit.
486///
Andrew Trick17d35e52012-03-14 04:00:41 +0000487/// After initSUnits, the SUnits vector cannot be resized and the scheduler may
488/// hang onto SUnit pointers. We may relax this in the future by using SUnit IDs
489/// instead of pointers.
490///
491/// MachineScheduler relies on initSUnits numbering the nodes by their order in
492/// the original instruction list.
Andrew Trickb4566a92012-02-22 06:08:11 +0000493void ScheduleDAGInstrs::initSUnits() {
494 // We'll be allocating one SUnit for each real instruction in the region,
495 // which is contained within a basic block.
496 SUnits.reserve(BB->size());
497
Andrew Trick68675c62012-03-09 04:29:02 +0000498 for (MachineBasicBlock::iterator I = RegionBegin; I != RegionEnd; ++I) {
Andrew Trickb4566a92012-02-22 06:08:11 +0000499 MachineInstr *MI = I;
500 if (MI->isDebugValue())
501 continue;
502
Andrew Trick953be892012-03-07 23:00:49 +0000503 SUnit *SU = newSUnit(MI);
Andrew Trickb4566a92012-02-22 06:08:11 +0000504 MISUnitMap[MI] = SU;
505
506 SU->isCall = MI->isCall();
507 SU->isCommutable = MI->isCommutable();
508
509 // Assign the Latency field of SU using target-provided information.
510 if (UnitLatencies)
511 SU->Latency = 1;
512 else
Andrew Trick953be892012-03-07 23:00:49 +0000513 computeLatency(SU);
Andrew Trickb4566a92012-02-22 06:08:11 +0000514 }
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000515}
516
Andrew Trick006e1ab2012-04-24 17:56:43 +0000517/// If RegPressure is non null, compute register pressure as a side effect. The
518/// DAG builder is an efficient place to do it because it already visits
519/// operands.
520void ScheduleDAGInstrs::buildSchedGraph(AliasAnalysis *AA,
521 RegPressureTracker *RPTracker) {
Andrew Trickb4566a92012-02-22 06:08:11 +0000522 // Create an SUnit for each real instruction.
523 initSUnits();
Dan Gohman343f0c02008-11-19 23:18:57 +0000524
Dan Gohman6a9041e2008-12-04 01:35:46 +0000525 // We build scheduling units by walking a block's instruction list from bottom
526 // to top.
527
David Goodwin980d4942009-11-09 19:22:17 +0000528 // Remember where a generic side-effecting instruction is as we procede.
529 SUnit *BarrierChain = 0, *AliasChain = 0;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000530
David Goodwin980d4942009-11-09 19:22:17 +0000531 // Memory references to specific known memory locations are tracked
532 // so that they can be given more precise dependencies. We track
533 // separately the known memory locations that may alias and those
534 // that are known not to alias
535 std::map<const Value *, SUnit *> AliasMemDefs, NonAliasMemDefs;
536 std::map<const Value *, std::vector<SUnit *> > AliasMemUses, NonAliasMemUses;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000537
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000538 // Remove any stale debug info; sometimes BuildSchedGraph is called again
539 // without emitting the info from the previous call.
Devang Patelcf4cc842011-06-02 20:07:12 +0000540 DbgValues.clear();
541 FirstDbgValue = NULL;
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000542
Andrew Trick81a682a2012-02-23 01:52:38 +0000543 assert(Defs.empty() && Uses.empty() &&
544 "Only BuildGraph should update Defs/Uses");
Andrew Trick702d4892012-02-24 07:04:55 +0000545 Defs.setRegLimit(TRI->getNumRegs());
546 Uses.setRegLimit(TRI->getNumRegs());
Andrew Trick9b668532011-05-06 21:52:52 +0000547
Andrew Trick8ae3ac72012-02-22 21:59:00 +0000548 assert(VRegDefs.empty() && "Only BuildSchedGraph may access VRegDefs");
549 // FIXME: Allow SparseSet to reserve space for the creation of virtual
550 // registers during scheduling. Don't artificially inflate the Universe
551 // because we want to assert that vregs are not created during DAG building.
552 VRegDefs.setUniverse(MRI.getNumVirtRegs());
Andrew Trick3c58ba82012-01-14 02:17:18 +0000553
Andrew Trick81a682a2012-02-23 01:52:38 +0000554 // Model data dependencies between instructions being scheduled and the
555 // ExitSU.
Andrew Trick953be892012-03-07 23:00:49 +0000556 addSchedBarrierDeps();
Andrew Trick81a682a2012-02-23 01:52:38 +0000557
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000558 // Walk the list of instructions, from bottom moving up.
Devang Patelcf4cc842011-06-02 20:07:12 +0000559 MachineInstr *PrevMI = NULL;
Andrew Trick68675c62012-03-09 04:29:02 +0000560 for (MachineBasicBlock::iterator MII = RegionEnd, MIE = RegionBegin;
Dan Gohman343f0c02008-11-19 23:18:57 +0000561 MII != MIE; --MII) {
562 MachineInstr *MI = prior(MII);
Devang Patelcf4cc842011-06-02 20:07:12 +0000563 if (MI && PrevMI) {
564 DbgValues.push_back(std::make_pair(PrevMI, MI));
565 PrevMI = NULL;
566 }
567
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000568 if (MI->isDebugValue()) {
Devang Patelcf4cc842011-06-02 20:07:12 +0000569 PrevMI = MI;
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000570 continue;
571 }
Andrew Trick006e1ab2012-04-24 17:56:43 +0000572 if (RPTracker) {
573 RPTracker->recede();
574 assert(RPTracker->getPos() == prior(MII) && "RPTracker can't find MI");
575 }
Devang Patelcf4cc842011-06-02 20:07:12 +0000576
Andrew Trick00707922012-04-13 23:29:54 +0000577 assert((!MI->isTerminator() || CanHandleTerminators) && !MI->isLabel() &&
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000578 "Cannot schedule terminators or labels!");
Dan Gohman343f0c02008-11-19 23:18:57 +0000579
Andrew Trickb4566a92012-02-22 06:08:11 +0000580 SUnit *SU = MISUnitMap[MI];
581 assert(SU && "No SUnit mapped to this MI");
Dan Gohman54e4c362008-12-09 22:54:47 +0000582
Dan Gohman6a9041e2008-12-04 01:35:46 +0000583 // Add register-based dependencies (data, anti, and output).
Dan Gohman343f0c02008-11-19 23:18:57 +0000584 for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) {
585 const MachineOperand &MO = MI->getOperand(j);
586 if (!MO.isReg()) continue;
587 unsigned Reg = MO.getReg();
588 if (Reg == 0) continue;
589
Andrew Trick7ebcaf42012-01-14 02:17:15 +0000590 if (TRI->isPhysicalRegister(Reg))
591 addPhysRegDeps(SU, j);
592 else {
593 assert(!IsPostRA && "Virtual register encountered!");
Andrew Trick3c58ba82012-01-14 02:17:18 +0000594 if (MO.isDef())
595 addVRegDefDeps(SU, j);
Andrew Trick63d578b2012-02-23 03:16:24 +0000596 else if (MO.readsReg()) // ignore undef operands
Andrew Trick3c58ba82012-01-14 02:17:18 +0000597 addVRegUseDeps(SU, j);
Dan Gohman343f0c02008-11-19 23:18:57 +0000598 }
599 }
Dan Gohman6a9041e2008-12-04 01:35:46 +0000600
601 // Add chain dependencies.
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000602 // Chain dependencies used to enforce memory order should have
603 // latency of 0 (except for true dependency of Store followed by
604 // aliased Load... we estimate that with a single cycle of latency
605 // assuming the hardware will bypass)
Dan Gohman6a9041e2008-12-04 01:35:46 +0000606 // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable
607 // after stack slots are lowered to actual addresses.
608 // TODO: Use an AliasAnalysis and do real alias-analysis queries, and
609 // produce more precise dependence information.
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000610#define STORE_LOAD_LATENCY 1
611 unsigned TrueMemOrderLatency = 0;
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000612 if (MI->isCall() || MI->hasUnmodeledSideEffects() ||
Andrew Trickf405b1a2011-05-05 19:24:06 +0000613 (MI->hasVolatileMemoryRef() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000614 (!MI->mayLoad() || !MI->isInvariantLoad(AA)))) {
David Goodwin980d4942009-11-09 19:22:17 +0000615 // Be conservative with these and add dependencies on all memory
616 // references, even those that are known to not alias.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000617 for (std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000618 NonAliasMemDefs.begin(), E = NonAliasMemDefs.end(); I != E; ++I) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000619 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000620 }
621 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000622 NonAliasMemUses.begin(), E = NonAliasMemUses.end(); I != E; ++I) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000623 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000624 I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000625 }
David Goodwin980d4942009-11-09 19:22:17 +0000626 NonAliasMemDefs.clear();
627 NonAliasMemUses.clear();
628 // Add SU to the barrier chain.
629 if (BarrierChain)
630 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
631 BarrierChain = SU;
632
633 // fall-through
634 new_alias_chain:
635 // Chain all possibly aliasing memory references though SU.
636 if (AliasChain)
637 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
638 AliasChain = SU;
639 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
640 PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
641 for (std::map<const Value *, SUnit *>::iterator I = AliasMemDefs.begin(),
642 E = AliasMemDefs.end(); I != E; ++I) {
643 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
644 }
645 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
646 AliasMemUses.begin(), E = AliasMemUses.end(); I != E; ++I) {
647 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
648 I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
649 }
650 PendingLoads.clear();
651 AliasMemDefs.clear();
652 AliasMemUses.clear();
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000653 } else if (MI->mayStore()) {
David Goodwina9e61072009-11-03 20:15:00 +0000654 bool MayAlias = true;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000655 TrueMemOrderLatency = STORE_LOAD_LATENCY;
David Goodwina9e61072009-11-03 20:15:00 +0000656 if (const Value *V = getUnderlyingObjectForInstr(MI, MFI, MayAlias)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000657 // A store to a specific PseudoSourceValue. Add precise dependencies.
David Goodwin980d4942009-11-09 19:22:17 +0000658 // Record the def in MemDefs, first adding a dep if there is
659 // an existing def.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000660 std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000661 ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000662 std::map<const Value *, SUnit *>::iterator IE =
David Goodwin980d4942009-11-09 19:22:17 +0000663 ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end());
664 if (I != IE) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000665 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0,
Dan Gohman54e4c362008-12-09 22:54:47 +0000666 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000667 I->second = SU;
668 } else {
David Goodwin980d4942009-11-09 19:22:17 +0000669 if (MayAlias)
670 AliasMemDefs[V] = SU;
671 else
672 NonAliasMemDefs[V] = SU;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000673 }
674 // Handle the uses in MemUses, if there are any.
Dan Gohmana629b482008-12-08 17:50:35 +0000675 std::map<const Value *, std::vector<SUnit *> >::iterator J =
David Goodwin980d4942009-11-09 19:22:17 +0000676 ((MayAlias) ? AliasMemUses.find(V) : NonAliasMemUses.find(V));
677 std::map<const Value *, std::vector<SUnit *> >::iterator JE =
678 ((MayAlias) ? AliasMemUses.end() : NonAliasMemUses.end());
679 if (J != JE) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000680 for (unsigned i = 0, e = J->second.size(); i != e; ++i)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000681 J->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency,
682 /*Reg=*/0, /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000683 J->second.clear();
684 }
David Goodwina9e61072009-11-03 20:15:00 +0000685 if (MayAlias) {
David Goodwin980d4942009-11-09 19:22:17 +0000686 // Add dependencies from all the PendingLoads, i.e. loads
687 // with no underlying object.
David Goodwina9e61072009-11-03 20:15:00 +0000688 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
689 PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
David Goodwin980d4942009-11-09 19:22:17 +0000690 // Add dependence on alias chain, if needed.
691 if (AliasChain)
692 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
David Goodwina9e61072009-11-03 20:15:00 +0000693 }
David Goodwin980d4942009-11-09 19:22:17 +0000694 // Add dependence on barrier chain, if needed.
695 if (BarrierChain)
696 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
David Goodwin5be870a2009-11-05 00:16:44 +0000697 } else {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000698 // Treat all other stores conservatively.
David Goodwin980d4942009-11-09 19:22:17 +0000699 goto new_alias_chain;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000700 }
Evan Chengec6906b2010-10-23 02:10:46 +0000701
702 if (!ExitSU.isPred(SU))
703 // Push store's up a bit to avoid them getting in between cmp
704 // and branches.
705 ExitSU.addPred(SDep(SU, SDep::Order, 0,
706 /*Reg=*/0, /*isNormalMemory=*/false,
707 /*isMustAlias=*/false,
708 /*isArtificial=*/true));
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000709 } else if (MI->mayLoad()) {
David Goodwina9e61072009-11-03 20:15:00 +0000710 bool MayAlias = true;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000711 TrueMemOrderLatency = 0;
Dan Gohmana70dca12009-10-09 23:27:56 +0000712 if (MI->isInvariantLoad(AA)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000713 // Invariant load, no chain dependencies needed!
David Goodwin5be870a2009-11-05 00:16:44 +0000714 } else {
Andrew Trickf405b1a2011-05-05 19:24:06 +0000715 if (const Value *V =
David Goodwin980d4942009-11-09 19:22:17 +0000716 getUnderlyingObjectForInstr(MI, MFI, MayAlias)) {
717 // A load from a specific PseudoSourceValue. Add precise dependencies.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000718 std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000719 ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000720 std::map<const Value *, SUnit *>::iterator IE =
David Goodwin980d4942009-11-09 19:22:17 +0000721 ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end());
722 if (I != IE)
723 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0,
724 /*isNormalMemory=*/true));
725 if (MayAlias)
726 AliasMemUses[V].push_back(SU);
Andrew Trickf405b1a2011-05-05 19:24:06 +0000727 else
David Goodwin980d4942009-11-09 19:22:17 +0000728 NonAliasMemUses[V].push_back(SU);
729 } else {
730 // A load with no underlying object. Depend on all
731 // potentially aliasing stores.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000732 for (std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000733 AliasMemDefs.begin(), E = AliasMemDefs.end(); I != E; ++I)
734 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000735
David Goodwin980d4942009-11-09 19:22:17 +0000736 PendingLoads.push_back(SU);
737 MayAlias = true;
David Goodwina9e61072009-11-03 20:15:00 +0000738 }
Andrew Trickf405b1a2011-05-05 19:24:06 +0000739
David Goodwin980d4942009-11-09 19:22:17 +0000740 // Add dependencies on alias and barrier chains, if needed.
741 if (MayAlias && AliasChain)
742 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
743 if (BarrierChain)
744 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000745 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000746 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000747 }
Devang Patelcf4cc842011-06-02 20:07:12 +0000748 if (PrevMI)
749 FirstDbgValue = PrevMI;
Dan Gohman79ce2762009-01-15 19:20:50 +0000750
Andrew Trick81a682a2012-02-23 01:52:38 +0000751 Defs.clear();
752 Uses.clear();
Andrew Trick3c58ba82012-01-14 02:17:18 +0000753 VRegDefs.clear();
Dan Gohman79ce2762009-01-15 19:20:50 +0000754 PendingLoads.clear();
Dan Gohman343f0c02008-11-19 23:18:57 +0000755}
756
Andrew Trick953be892012-03-07 23:00:49 +0000757void ScheduleDAGInstrs::computeLatency(SUnit *SU) {
David Goodwind94a4e52009-08-10 15:55:25 +0000758 // Compute the latency for the node.
Evan Cheng3ef1c872010-09-10 01:29:16 +0000759 if (!InstrItins || InstrItins->isEmpty()) {
760 SU->Latency = 1;
Dan Gohman4ea8e852008-12-16 02:38:22 +0000761
Evan Cheng3ef1c872010-09-10 01:29:16 +0000762 // Simplistic target-independent heuristic: assume that loads take
763 // extra time.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000764 if (SU->getInstr()->mayLoad())
Dan Gohman4ea8e852008-12-16 02:38:22 +0000765 SU->Latency += 2;
Evan Cheng8239daf2010-11-03 00:45:17 +0000766 } else {
767 SU->Latency = TII->getInstrLatency(InstrItins, SU->getInstr());
768 }
Dan Gohmanc8c28272008-11-21 00:12:10 +0000769}
770
Andrew Trick953be892012-03-07 23:00:49 +0000771void ScheduleDAGInstrs::computeOperandLatency(SUnit *Def, SUnit *Use,
David Goodwindc4bdcd2009-08-19 16:08:58 +0000772 SDep& dep) const {
Evan Cheng3ef1c872010-09-10 01:29:16 +0000773 if (!InstrItins || InstrItins->isEmpty())
David Goodwindc4bdcd2009-08-19 16:08:58 +0000774 return;
Andrew Trickf405b1a2011-05-05 19:24:06 +0000775
David Goodwindc4bdcd2009-08-19 16:08:58 +0000776 // For a data dependency with a known register...
777 if ((dep.getKind() != SDep::Data) || (dep.getReg() == 0))
778 return;
779
780 const unsigned Reg = dep.getReg();
781
782 // ... find the definition of the register in the defining
783 // instruction
784 MachineInstr *DefMI = Def->getInstr();
785 int DefIdx = DefMI->findRegisterDefOperandIdx(Reg);
786 if (DefIdx != -1) {
Evan Cheng1aca5bc2010-10-08 18:42:25 +0000787 const MachineOperand &MO = DefMI->getOperand(DefIdx);
788 if (MO.isReg() && MO.isImplicit() &&
Evan Chengd82de832010-10-08 23:01:57 +0000789 DefIdx >= (int)DefMI->getDesc().getNumOperands()) {
Evan Cheng1aca5bc2010-10-08 18:42:25 +0000790 // This is an implicit def, getOperandLatency() won't return the correct
791 // latency. e.g.
792 // %D6<def>, %D7<def> = VLD1q16 %R2<kill>, 0, ..., %Q3<imp-def>
793 // %Q1<def> = VMULv8i16 %Q1<kill>, %Q3<kill>, ...
794 // What we want is to compute latency between def of %D6/%D7 and use of
795 // %Q3 instead.
Jakob Stoklund Olesen02634be2012-02-22 22:52:52 +0000796 unsigned Op2 = DefMI->findRegisterDefOperandIdx(Reg, false, true, TRI);
797 if (DefMI->getOperand(Op2).isReg())
798 DefIdx = Op2;
Evan Cheng1aca5bc2010-10-08 18:42:25 +0000799 }
Evan Chenga0792de2010-10-06 06:27:31 +0000800 MachineInstr *UseMI = Use->getInstr();
Evan Cheng3881cb72010-09-29 22:42:35 +0000801 // For all uses of the register, calculate the maxmimum latency
802 int Latency = -1;
Evan Chengec6906b2010-10-23 02:10:46 +0000803 if (UseMI) {
804 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
805 const MachineOperand &MO = UseMI->getOperand(i);
806 if (!MO.isReg() || !MO.isUse())
807 continue;
808 unsigned MOReg = MO.getReg();
809 if (MOReg != Reg)
810 continue;
David Goodwindc4bdcd2009-08-19 16:08:58 +0000811
Evan Chengec6906b2010-10-23 02:10:46 +0000812 int UseCycle = TII->getOperandLatency(InstrItins, DefMI, DefIdx,
813 UseMI, i);
814 Latency = std::max(Latency, UseCycle);
815 }
816 } else {
817 // UseMI is null, then it must be a scheduling barrier.
818 if (!InstrItins || InstrItins->isEmpty())
819 return;
820 unsigned DefClass = DefMI->getDesc().getSchedClass();
821 Latency = InstrItins->getOperandCycle(DefClass, DefIdx);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000822 }
Evan Chengec6906b2010-10-23 02:10:46 +0000823
824 // If we found a latency, then replace the existing dependence latency.
825 if (Latency >= 0)
826 dep.setLatency(Latency);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000827 }
828}
829
Dan Gohman343f0c02008-11-19 23:18:57 +0000830void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
831 SU->getInstr()->dump();
832}
833
834std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
835 std::string s;
836 raw_string_ostream oss(s);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000837 if (SU == &EntrySU)
838 oss << "<entry>";
839 else if (SU == &ExitSU)
840 oss << "<exit>";
841 else
842 SU->getInstr()->print(oss);
Dan Gohman343f0c02008-11-19 23:18:57 +0000843 return oss.str();
844}
845
Andrew Trick56b94c52012-03-07 00:18:22 +0000846/// Return the basic block label. It is not necessarilly unique because a block
847/// contains multiple scheduling regions. But it is fine for visualization.
848std::string ScheduleDAGInstrs::getDAGName() const {
849 return "dag." + BB->getFullName();
850}