blob: 93ee38bc0bf596318a3f7ebf0001763cf236e823 [file] [log] [blame]
Dan Gohmanf90d3b02008-12-08 17:50:35 +00001//===---- ScheduleDAGInstrs.cpp - MachineInstr Rescheduling ---------------===//
Dan Gohman60cb69e2008-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 Gohmanf90d3b02008-12-08 17:50:35 +000010// This implements the ScheduleDAGInstrs class, which implements re-scheduling
11// of MachineInstrs.
Dan Gohman60cb69e2008-11-19 23:18:57 +000012//
13//===----------------------------------------------------------------------===//
14
Andrew Trick48d392e2012-11-28 05:13:28 +000015#define DEBUG_TYPE "misched"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/CodeGen/ScheduleDAGInstrs.h"
17#include "llvm/ADT/MapVector.h"
18#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/ADT/SmallSet.h"
Dan Gohman1ee0d412009-01-30 02:49:14 +000020#include "llvm/Analysis/AliasAnalysis.h"
Dan Gohmana4fcd242010-12-15 20:02:24 +000021#include "llvm/Analysis/ValueTracking.h"
Andrew Trick46cc9a42012-02-22 06:08:11 +000022#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Dan Gohmandddc1ac2008-12-16 03:25:46 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
Andrew Trick6b104f82013-12-28 21:56:55 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman48b185d2009-09-25 20:36:54 +000025#include "llvm/CodeGen/MachineMemOperand.h"
Dan Gohmandddc1ac2008-12-16 03:25:46 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman3aab10b2008-12-04 01:35:46 +000027#include "llvm/CodeGen/PseudoSourceValue.h"
Andrew Trick88517f62012-06-06 19:47:35 +000028#include "llvm/CodeGen/RegisterPressure.h"
Andrew Trickcd1c2f92012-11-28 05:13:24 +000029#include "llvm/CodeGen/ScheduleDFS.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/Operator.h"
Evan Cheng8264e272011-06-29 01:14:12 +000031#include "llvm/MC/MCInstrItineraries.h"
Andrew Trickda01ba32012-05-15 18:59:41 +000032#include "llvm/Support/CommandLine.h"
Dan Gohman60cb69e2008-11-19 23:18:57 +000033#include "llvm/Support/Debug.h"
Andrew Trick90f711d2012-10-15 18:02:27 +000034#include "llvm/Support/Format.h"
Dan Gohman60cb69e2008-11-19 23:18:57 +000035#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/Target/TargetInstrInfo.h"
37#include "llvm/Target/TargetMachine.h"
38#include "llvm/Target/TargetRegisterInfo.h"
39#include "llvm/Target/TargetSubtargetInfo.h"
Andrew Trickc01b0042013-08-23 17:48:43 +000040#include <queue>
41
Dan Gohman60cb69e2008-11-19 23:18:57 +000042using namespace llvm;
43
Andrew Trickda01ba32012-05-15 18:59:41 +000044static cl::opt<bool> EnableAASchedMI("enable-aa-sched-mi", cl::Hidden,
45 cl::ZeroOrMore, cl::init(false),
46 cl::desc("Enable use of AA during MI GAD construction"));
47
Dan Gohman619ef482009-01-15 19:20:50 +000048ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf,
Dan Gohmandddc1ac2008-12-16 03:25:46 +000049 const MachineLoopInfo &mli,
Andrew Trick1d028a32012-01-14 02:17:12 +000050 const MachineDominatorTree &mdt,
Andrew Trick46cc9a42012-02-22 06:08:11 +000051 bool IsPostRAFlag,
Andrew Trick6b104f82013-12-28 21:56:55 +000052 bool RemoveKillFlags,
Andrew Trick46cc9a42012-02-22 06:08:11 +000053 LiveIntervals *lis)
Andrew Trickdd79f0f2012-10-10 05:43:09 +000054 : ScheduleDAG(mf), MLI(mli), MDT(mdt), MFI(mf.getFrameInfo()), LIS(lis),
Andrew Trick6b104f82013-12-28 21:56:55 +000055 IsPostRA(IsPostRAFlag), RemoveKillFlags(RemoveKillFlags),
56 CanHandleTerminators(false), FirstDbgValue(0) {
Andrew Trick46cc9a42012-02-22 06:08:11 +000057 assert((IsPostRA || LIS) && "PreRA scheduling requires LiveIntervals");
Devang Patele5feef02011-06-02 20:07:12 +000058 DbgValues.clear();
Andrew Trickdb42c6f2012-02-22 06:08:13 +000059 assert(!(IsPostRA && MRI.getNumVirtRegs()) &&
Andrew Trickda84e642012-02-21 04:51:23 +000060 "Virtual registers must be removed prior to PostRA scheduling");
Andrew Trick9b635132012-09-18 18:20:00 +000061
62 const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>();
63 SchedModel.init(*ST.getSchedModel(), &ST, TII);
Evan Chengf0236e02009-10-18 19:58:47 +000064}
Dan Gohman60cb69e2008-11-19 23:18:57 +000065
Dan Gohman1ee0d412009-01-30 02:49:14 +000066/// getUnderlyingObjectFromInt - This is the function that does the work of
67/// looking through basic ptrtoint+arithmetic+inttoptr sequences.
68static const Value *getUnderlyingObjectFromInt(const Value *V) {
69 do {
Dan Gohman58b0e712009-07-17 20:58:59 +000070 if (const Operator *U = dyn_cast<Operator>(V)) {
Dan Gohman1ee0d412009-01-30 02:49:14 +000071 // If we find a ptrtoint, we can transfer control back to the
72 // regular getUnderlyingObjectFromInt.
Dan Gohman58b0e712009-07-17 20:58:59 +000073 if (U->getOpcode() == Instruction::PtrToInt)
Dan Gohman1ee0d412009-01-30 02:49:14 +000074 return U->getOperand(0);
Andrew Trick0be19362012-11-28 03:42:49 +000075 // If we find an add of a constant, a multiplied value, or a phi, it's
Dan Gohman1ee0d412009-01-30 02:49:14 +000076 // likely that the other operand will lead us to the base
77 // object. We don't have to worry about the case where the
Dan Gohman6c0c2192009-08-07 01:26:06 +000078 // object address is somehow being computed by the multiply,
Dan Gohman1ee0d412009-01-30 02:49:14 +000079 // because our callers only care when the result is an
Nick Lewycky1a329542012-10-26 04:27:49 +000080 // identifiable object.
Dan Gohman58b0e712009-07-17 20:58:59 +000081 if (U->getOpcode() != Instruction::Add ||
Dan Gohman1ee0d412009-01-30 02:49:14 +000082 (!isa<ConstantInt>(U->getOperand(1)) &&
Andrew Trick0be19362012-11-28 03:42:49 +000083 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
84 !isa<PHINode>(U->getOperand(1))))
Dan Gohman1ee0d412009-01-30 02:49:14 +000085 return V;
86 V = U->getOperand(0);
87 } else {
88 return V;
89 }
Duncan Sands19d0b472010-02-16 11:11:14 +000090 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
Dan Gohman1ee0d412009-01-30 02:49:14 +000091 } while (1);
92}
93
Hal Finkel66859ae2012-12-10 18:49:16 +000094/// getUnderlyingObjects - This is a wrapper around GetUnderlyingObjects
Dan Gohman1ee0d412009-01-30 02:49:14 +000095/// and adds support for basic ptrtoint+arithmetic+inttoptr sequences.
Hal Finkel66859ae2012-12-10 18:49:16 +000096static void getUnderlyingObjects(const Value *V,
97 SmallVectorImpl<Value *> &Objects) {
98 SmallPtrSet<const Value*, 16> Visited;
99 SmallVector<const Value *, 4> Working(1, V);
Dan Gohman1ee0d412009-01-30 02:49:14 +0000100 do {
Hal Finkel66859ae2012-12-10 18:49:16 +0000101 V = Working.pop_back_val();
102
103 SmallVector<Value *, 4> Objs;
104 GetUnderlyingObjects(const_cast<Value *>(V), Objs);
105
Craig Toppere1c1d362013-07-03 05:11:49 +0000106 for (SmallVectorImpl<Value *>::iterator I = Objs.begin(), IE = Objs.end();
Hal Finkel66859ae2012-12-10 18:49:16 +0000107 I != IE; ++I) {
108 V = *I;
109 if (!Visited.insert(V))
110 continue;
111 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
112 const Value *O =
113 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
114 if (O->getType()->isPointerTy()) {
115 Working.push_back(O);
116 continue;
117 }
118 }
119 Objects.push_back(const_cast<Value *>(V));
120 }
121 } while (!Working.empty());
Dan Gohman1ee0d412009-01-30 02:49:14 +0000122}
123
Benjamin Kramerfd510922013-06-29 18:41:17 +0000124typedef SmallVector<PointerIntPair<const Value *, 1, bool>, 4>
125UnderlyingObjectsVector;
126
Hal Finkel66859ae2012-12-10 18:49:16 +0000127/// getUnderlyingObjectsForInstr - If this machine instr has memory reference
Dan Gohman1ee0d412009-01-30 02:49:14 +0000128/// information and it can be tracked to a normal reference to a known
Hal Finkel66859ae2012-12-10 18:49:16 +0000129/// object, return the Value for that object.
130static void getUnderlyingObjectsForInstr(const MachineInstr *MI,
Benjamin Kramerfd510922013-06-29 18:41:17 +0000131 const MachineFrameInfo *MFI,
132 UnderlyingObjectsVector &Objects) {
Dan Gohman1ee0d412009-01-30 02:49:14 +0000133 if (!MI->hasOneMemOperand() ||
Dan Gohman48b185d2009-09-25 20:36:54 +0000134 !(*MI->memoperands_begin())->getValue() ||
135 (*MI->memoperands_begin())->isVolatile())
Hal Finkel66859ae2012-12-10 18:49:16 +0000136 return;
Dan Gohman1ee0d412009-01-30 02:49:14 +0000137
Dan Gohman48b185d2009-09-25 20:36:54 +0000138 const Value *V = (*MI->memoperands_begin())->getValue();
Dan Gohman1ee0d412009-01-30 02:49:14 +0000139 if (!V)
Hal Finkel66859ae2012-12-10 18:49:16 +0000140 return;
Dan Gohman1ee0d412009-01-30 02:49:14 +0000141
Hal Finkel66859ae2012-12-10 18:49:16 +0000142 SmallVector<Value *, 4> Objs;
143 getUnderlyingObjects(V, Objs);
Andrew Trick24b1c482011-05-05 19:24:06 +0000144
Craig Toppere1c1d362013-07-03 05:11:49 +0000145 for (SmallVectorImpl<Value *>::iterator I = Objs.begin(), IE = Objs.end();
146 I != IE; ++I) {
Hal Finkel66859ae2012-12-10 18:49:16 +0000147 bool MayAlias = true;
148 V = *I;
149
150 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
151 // For now, ignore PseudoSourceValues which may alias LLVM IR values
152 // because the code that uses this function has no way to cope with
153 // such aliases.
154
155 if (PSV->isAliased(MFI)) {
156 Objects.clear();
157 return;
158 }
159
160 MayAlias = PSV->mayAlias(MFI);
161 } else if (!isIdentifiedObject(V)) {
162 Objects.clear();
163 return;
164 }
165
Benjamin Kramerfd510922013-06-29 18:41:17 +0000166 Objects.push_back(UnderlyingObjectsVector::value_type(V, MayAlias));
Evan Cheng0e9d9ca2009-10-18 18:16:27 +0000167 }
Dan Gohman1ee0d412009-01-30 02:49:14 +0000168}
169
Andrew Trick7405c6d2012-04-20 20:05:21 +0000170void ScheduleDAGInstrs::startBlock(MachineBasicBlock *bb) {
171 BB = bb;
Dan Gohmanb9543432009-02-10 23:27:53 +0000172}
173
Andrew Trick52226d42012-03-07 23:00:49 +0000174void ScheduleDAGInstrs::finishBlock() {
Andrew Trick51ee9362012-04-20 20:24:33 +0000175 // Subclasses should no longer refer to the old block.
Andrew Trick7405c6d2012-04-20 20:05:21 +0000176 BB = 0;
Andrew Trick60cf03e2012-03-07 05:21:52 +0000177}
178
Andrew Trick60cf03e2012-03-07 05:21:52 +0000179/// Initialize the DAG and common scheduler state for the current scheduling
180/// region. This does not actually create the DAG, only clears it. The
181/// scheduling driver may call BuildSchedGraph multiple times per scheduling
182/// region.
183void ScheduleDAGInstrs::enterRegion(MachineBasicBlock *bb,
184 MachineBasicBlock::iterator begin,
185 MachineBasicBlock::iterator end,
Andrew Tricka53e1012013-08-23 17:48:33 +0000186 unsigned regioninstrs) {
Andrew Trick7405c6d2012-04-20 20:05:21 +0000187 assert(bb == BB && "startBlock should set BB");
Andrew Trick8c207e42012-03-09 04:29:02 +0000188 RegionBegin = begin;
189 RegionEnd = end;
Andrew Tricka53e1012013-08-23 17:48:33 +0000190 NumRegionInstrs = regioninstrs;
Andrew Trick60cf03e2012-03-07 05:21:52 +0000191}
192
193/// Close the current scheduling region. Don't clear any state in case the
194/// driver wants to refer to the previous scheduling region.
195void ScheduleDAGInstrs::exitRegion() {
196 // Nothing to do.
197}
198
Andrew Trick52226d42012-03-07 23:00:49 +0000199/// addSchedBarrierDeps - Add dependencies from instructions in the current
Evan Cheng15459b62010-10-23 02:10:46 +0000200/// list of instructions being scheduled to scheduling barrier by adding
201/// the exit SU to the register defs and use list. This is because we want to
202/// make sure instructions which define registers that are either used by
203/// the terminator or are live-out are properly scheduled. This is
204/// especially important when the definition latency of the return value(s)
205/// are too high to be hidden by the branch or when the liveout registers
206/// used by instructions in the fallthrough block.
Andrew Trick52226d42012-03-07 23:00:49 +0000207void ScheduleDAGInstrs::addSchedBarrierDeps() {
Andrew Trick8c207e42012-03-09 04:29:02 +0000208 MachineInstr *ExitMI = RegionEnd != BB->end() ? &*RegionEnd : 0;
Evan Cheng15459b62010-10-23 02:10:46 +0000209 ExitSU.setInstr(ExitMI);
210 bool AllDepKnown = ExitMI &&
Evan Cheng7f8e5632011-12-07 07:15:52 +0000211 (ExitMI->isCall() || ExitMI->isBarrier());
Evan Cheng15459b62010-10-23 02:10:46 +0000212 if (ExitMI && AllDepKnown) {
213 // If it's a call or a barrier, add dependencies on the defs and uses of
214 // instruction.
215 for (unsigned i = 0, e = ExitMI->getNumOperands(); i != e; ++i) {
216 const MachineOperand &MO = ExitMI->getOperand(i);
217 if (!MO.isReg() || MO.isDef()) continue;
218 unsigned Reg = MO.getReg();
219 if (Reg == 0) continue;
220
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000221 if (TRI->isPhysicalRegister(Reg))
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000222 Uses.insert(PhysRegSUOper(&ExitSU, -1, Reg));
Andrew Tricke6913c72012-03-16 05:04:25 +0000223 else {
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000224 assert(!IsPostRA && "Virtual register encountered after regalloc.");
Andrew Trickd5953622012-12-01 01:22:44 +0000225 if (MO.readsReg()) // ignore undef operands
226 addVRegUseDeps(&ExitSU, i);
Andrew Tricke6913c72012-03-16 05:04:25 +0000227 }
Evan Cheng15459b62010-10-23 02:10:46 +0000228 }
229 } else {
230 // For others, e.g. fallthrough, conditional branch, assume the exit
Evan Chengcbdf7e82010-10-27 23:17:17 +0000231 // uses all the registers that are livein to the successor blocks.
Benjamin Kramer411d5a22012-03-16 17:38:19 +0000232 assert(Uses.empty() && "Uses in set before adding deps?");
Evan Chengcbdf7e82010-10-27 23:17:17 +0000233 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
234 SE = BB->succ_end(); SI != SE; ++SI)
235 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
Andrew Trick24b1c482011-05-05 19:24:06 +0000236 E = (*SI)->livein_end(); I != E; ++I) {
Evan Chengcbdf7e82010-10-27 23:17:17 +0000237 unsigned Reg = *I;
Benjamin Kramer411d5a22012-03-16 17:38:19 +0000238 if (!Uses.contains(Reg))
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000239 Uses.insert(PhysRegSUOper(&ExitSU, -1, Reg));
Evan Chengcbdf7e82010-10-27 23:17:17 +0000240 }
Evan Cheng15459b62010-10-23 02:10:46 +0000241 }
242}
243
Andrew Trickd675a4c2012-02-23 01:52:38 +0000244/// MO is an operand of SU's instruction that defines a physical register. Add
245/// data dependencies from SU to any uses of the physical register.
Andrew Trickae535612012-08-23 00:39:43 +0000246void ScheduleDAGInstrs::addPhysRegDataDeps(SUnit *SU, unsigned OperIdx) {
247 const MachineOperand &MO = SU->getInstr()->getOperand(OperIdx);
Andrew Trickd675a4c2012-02-23 01:52:38 +0000248 assert(MO.isDef() && "expect physreg def");
249
250 // Ask the target if address-backscheduling is desirable, and if so how much.
251 const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>();
Andrew Trickd675a4c2012-02-23 01:52:38 +0000252
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000253 for (MCRegAliasIterator Alias(MO.getReg(), TRI, true);
254 Alias.isValid(); ++Alias) {
Andrew Trick9dbbd3e2012-02-24 07:04:55 +0000255 if (!Uses.contains(*Alias))
Andrew Trickd675a4c2012-02-23 01:52:38 +0000256 continue;
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000257 for (Reg2SUnitsMap::iterator I = Uses.find(*Alias); I != Uses.end(); ++I) {
258 SUnit *UseSU = I->SU;
Andrew Trickd675a4c2012-02-23 01:52:38 +0000259 if (UseSU == SU)
260 continue;
Andrew Trick07dced62012-10-08 18:54:00 +0000261
Andrew Trick07dced62012-10-08 18:54:00 +0000262 // Adjust the dependence latency using operand def/use information,
263 // then allow the target to perform its own adjustments.
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000264 int UseOp = I->OpIdx;
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000265 MachineInstr *RegUse = 0;
266 SDep Dep;
267 if (UseOp < 0)
268 Dep = SDep(SU, SDep::Artificial);
269 else {
Andrew Tricke833e1c2013-04-13 06:07:40 +0000270 // Set the hasPhysRegDefs only for physreg defs that have a use within
271 // the scheduling region.
272 SU->hasPhysRegDefs = true;
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000273 Dep = SDep(SU, SDep::Data, *Alias);
274 RegUse = UseSU->getInstr();
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000275 }
276 Dep.setLatency(
Andrew Trickde2109e2013-06-15 04:49:57 +0000277 SchedModel.computeOperandLatency(SU->getInstr(), OperIdx, RegUse,
278 UseOp));
Andrew Trick45446062012-06-05 21:11:27 +0000279
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000280 ST.adjustSchedDependency(SU, UseSU, Dep);
281 UseSU->addPred(Dep);
Andrew Trickd675a4c2012-02-23 01:52:38 +0000282 }
283 }
284}
285
Andrew Trickdbee9d82012-01-14 02:17:15 +0000286/// addPhysRegDeps - Add register dependencies (data, anti, and output) from
287/// this SUnit to following instructions in the same scheduling region that
288/// depend the physical register referenced at OperIdx.
289void ScheduleDAGInstrs::addPhysRegDeps(SUnit *SU, unsigned OperIdx) {
Andrew Trick6b104f82013-12-28 21:56:55 +0000290 MachineInstr *MI = SU->getInstr();
291 MachineOperand &MO = MI->getOperand(OperIdx);
Andrew Trickdbee9d82012-01-14 02:17:15 +0000292
293 // Optionally add output and anti dependencies. For anti
294 // dependencies we use a latency of 0 because for a multi-issue
295 // target we want to allow the defining instruction to issue
296 // in the same cycle as the using instruction.
297 // TODO: Using a latency of 1 here for output dependencies assumes
298 // there's no cost for reusing registers.
299 SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000300 for (MCRegAliasIterator Alias(MO.getReg(), TRI, true);
301 Alias.isValid(); ++Alias) {
Andrew Trick9dbbd3e2012-02-24 07:04:55 +0000302 if (!Defs.contains(*Alias))
Andrew Trickd675a4c2012-02-23 01:52:38 +0000303 continue;
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000304 for (Reg2SUnitsMap::iterator I = Defs.find(*Alias); I != Defs.end(); ++I) {
305 SUnit *DefSU = I->SU;
Andrew Trickdbee9d82012-01-14 02:17:15 +0000306 if (DefSU == &ExitSU)
307 continue;
308 if (DefSU != SU &&
309 (Kind != SDep::Output || !MO.isDead() ||
310 !DefSU->getInstr()->registerDefIsDead(*Alias))) {
311 if (Kind == SDep::Anti)
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000312 DefSU->addPred(SDep(SU, Kind, /*Reg=*/*Alias));
Andrew Trickdbee9d82012-01-14 02:17:15 +0000313 else {
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000314 SDep Dep(SU, Kind, /*Reg=*/*Alias);
Andrew Trickde2109e2013-06-15 04:49:57 +0000315 Dep.setLatency(
316 SchedModel.computeOutputLatency(MI, OperIdx, DefSU->getInstr()));
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000317 DefSU->addPred(Dep);
Andrew Trickdbee9d82012-01-14 02:17:15 +0000318 }
319 }
320 }
321 }
322
Andrew Trickd675a4c2012-02-23 01:52:38 +0000323 if (!MO.isDef()) {
Andrew Tricke833e1c2013-04-13 06:07:40 +0000324 SU->hasPhysRegUses = true;
Andrew Trickd675a4c2012-02-23 01:52:38 +0000325 // Either insert a new Reg2SUnits entry with an empty SUnits list, or
326 // retrieve the existing SUnits list for this register's uses.
327 // Push this SUnit on the use list.
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000328 Uses.insert(PhysRegSUOper(SU, OperIdx, MO.getReg()));
Andrew Trick6b104f82013-12-28 21:56:55 +0000329 if (RemoveKillFlags)
330 MO.setIsKill(false);
Andrew Trickd675a4c2012-02-23 01:52:38 +0000331 }
332 else {
Andrew Trickae535612012-08-23 00:39:43 +0000333 addPhysRegDataDeps(SU, OperIdx);
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000334 unsigned Reg = MO.getReg();
Andrew Trickdbee9d82012-01-14 02:17:15 +0000335
Andrew Trickd675a4c2012-02-23 01:52:38 +0000336 // clear this register's use list
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000337 if (Uses.contains(Reg))
338 Uses.eraseAll(Reg);
Andrew Trickd675a4c2012-02-23 01:52:38 +0000339
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000340 if (!MO.isDead()) {
341 Defs.eraseAll(Reg);
342 } else if (SU->isCall) {
343 // Calls will not be reordered because of chain dependencies (see
344 // below). Since call operands are dead, calls may continue to be added
345 // to the DefList making dependence checking quadratic in the size of
346 // the block. Instead, we leave only one call at the back of the
347 // DefList.
348 Reg2SUnitsMap::RangePair P = Defs.equal_range(Reg);
349 Reg2SUnitsMap::iterator B = P.first;
350 Reg2SUnitsMap::iterator I = P.second;
351 for (bool isBegin = I == B; !isBegin; /* empty */) {
352 isBegin = (--I) == B;
353 if (!I->SU->isCall)
354 break;
355 I = Defs.erase(I);
356 }
Andrew Trickdbee9d82012-01-14 02:17:15 +0000357 }
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000358
Andrew Trickd675a4c2012-02-23 01:52:38 +0000359 // Defs are pushed in the order they are visited and never reordered.
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000360 Defs.insert(PhysRegSUOper(SU, OperIdx, Reg));
Andrew Trickdbee9d82012-01-14 02:17:15 +0000361 }
362}
363
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000364/// addVRegDefDeps - Add register output and data dependencies from this SUnit
365/// to instructions that occur later in the same scheduling region if they read
366/// from or write to the virtual register defined at OperIdx.
367///
368/// TODO: Hoist loop induction variable increments. This has to be
369/// reevaluated. Generally, IV scheduling should be done before coalescing.
370void ScheduleDAGInstrs::addVRegDefDeps(SUnit *SU, unsigned OperIdx) {
371 const MachineInstr *MI = SU->getInstr();
372 unsigned Reg = MI->getOperand(OperIdx).getReg();
373
Andrew Trick94053432012-07-28 01:48:15 +0000374 // Singly defined vregs do not have output/anti dependencies.
Andrew Trick64ca16e2012-02-22 18:34:49 +0000375 // The current operand is a def, so we have at least one.
Andrew Trick94053432012-07-28 01:48:15 +0000376 // Check here if there are any others...
Andrew Trick79795892012-07-30 23:48:17 +0000377 if (MRI.hasOneDef(Reg))
Andrew Trick94053432012-07-28 01:48:15 +0000378 return;
Andrew Trickdb42c6f2012-02-22 06:08:13 +0000379
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000380 // Add output dependence to the next nearest def of this vreg.
381 //
382 // Unless this definition is dead, the output dependence should be
383 // transitively redundant with antidependencies from this definition's
384 // uses. We're conservative for now until we have a way to guarantee the uses
385 // are not eliminated sometime during scheduling. The output dependence edge
386 // is also useful if output latency exceeds def-use latency.
Andrew Trick1eb4a0d2012-04-20 20:05:28 +0000387 VReg2SUnitMap::iterator DefI = VRegDefs.find(Reg);
Andrew Trickd458e2d2012-02-22 21:59:00 +0000388 if (DefI == VRegDefs.end())
389 VRegDefs.insert(VReg2SUnit(Reg, SU));
390 else {
391 SUnit *DefSU = DefI->SU;
392 if (DefSU != SU && DefSU != &ExitSU) {
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000393 SDep Dep(SU, SDep::Output, Reg);
Andrew Trickde2109e2013-06-15 04:49:57 +0000394 Dep.setLatency(
395 SchedModel.computeOutputLatency(MI, OperIdx, DefSU->getInstr()));
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000396 DefSU->addPred(Dep);
Andrew Trickd458e2d2012-02-22 21:59:00 +0000397 }
398 DefI->SU = SU;
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000399 }
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000400}
401
Andrew Trick46cc9a42012-02-22 06:08:11 +0000402/// addVRegUseDeps - Add a register data dependency if the instruction that
403/// defines the virtual register used at OperIdx is mapped to an SUnit. Add a
404/// register antidependency from this SUnit to instructions that occur later in
405/// the same scheduling region if they write the virtual register.
406///
407/// TODO: Handle ExitSU "uses" properly.
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000408void ScheduleDAGInstrs::addVRegUseDeps(SUnit *SU, unsigned OperIdx) {
Andrew Trick46cc9a42012-02-22 06:08:11 +0000409 MachineInstr *MI = SU->getInstr();
410 unsigned Reg = MI->getOperand(OperIdx).getReg();
411
Andrew Trick8dd26f02013-08-23 17:48:39 +0000412 // Record this local VReg use.
Andrew Trick2bc74c22013-08-30 04:36:57 +0000413 VReg2UseMap::iterator UI = VRegUses.find(Reg);
414 for (; UI != VRegUses.end(); ++UI) {
415 if (UI->SU == SU)
416 break;
417 }
418 if (UI == VRegUses.end())
419 VRegUses.insert(VReg2SUnit(Reg, SU));
Andrew Trick8dd26f02013-08-23 17:48:39 +0000420
Andrew Trick46cc9a42012-02-22 06:08:11 +0000421 // Lookup this operand's reaching definition.
422 assert(LIS && "vreg dependencies requires LiveIntervals");
Matthias Braun88dd0ab2013-10-10 21:28:52 +0000423 LiveQueryResult LRQ
424 = LIS->getInterval(Reg).Query(LIS->getInstructionIndex(MI));
Jakob Stoklund Olesenabc8c3d2012-05-20 02:44:38 +0000425 VNInfo *VNI = LRQ.valueIn();
Andrew Trick9e9a9f12012-04-24 18:04:41 +0000426
Andrew Trickda6a15d2012-02-23 03:16:24 +0000427 // VNI will be valid because MachineOperand::readsReg() is checked by caller.
Jakob Stoklund Olesenabc8c3d2012-05-20 02:44:38 +0000428 assert(VNI && "No value to read by operand");
Andrew Trick46cc9a42012-02-22 06:08:11 +0000429 MachineInstr *Def = LIS->getInstructionFromIndex(VNI->def);
Andrew Trickda6a15d2012-02-23 03:16:24 +0000430 // Phis and other noninstructions (after coalescing) have a NULL Def.
Andrew Trick46cc9a42012-02-22 06:08:11 +0000431 if (Def) {
432 SUnit *DefSU = getSUnit(Def);
433 if (DefSU) {
434 // The reaching Def lives within this scheduling region.
435 // Create a data dependence.
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000436 SDep dep(DefSU, SDep::Data, Reg);
Andrew Trick09650df2012-10-08 18:53:57 +0000437 // Adjust the dependence latency using operand def/use information, then
438 // allow the target to perform its own adjustments.
439 int DefOp = Def->findRegisterDefOperandIdx(Reg);
Andrew Trickde2109e2013-06-15 04:49:57 +0000440 dep.setLatency(SchedModel.computeOperandLatency(Def, DefOp, MI, OperIdx));
Andrew Trick45446062012-06-05 21:11:27 +0000441
Andrew Trick09650df2012-10-08 18:53:57 +0000442 const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>();
443 ST.adjustSchedDependency(DefSU, SU, const_cast<SDep &>(dep));
Andrew Trick46cc9a42012-02-22 06:08:11 +0000444 SU->addPred(dep);
445 }
446 }
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000447
448 // Add antidependence to the following def of the vreg it uses.
Andrew Trick1eb4a0d2012-04-20 20:05:28 +0000449 VReg2SUnitMap::iterator DefI = VRegDefs.find(Reg);
Andrew Trickd458e2d2012-02-22 21:59:00 +0000450 if (DefI != VRegDefs.end() && DefI->SU != SU)
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000451 DefI->SU->addPred(SDep(SU, SDep::Anti, Reg));
Andrew Trick46cc9a42012-02-22 06:08:11 +0000452}
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000453
Andrew Trickda01ba32012-05-15 18:59:41 +0000454/// Return true if MI is an instruction we are unable to reason about
455/// (like a call or something with unmodeled side effects).
456static inline bool isGlobalMemoryObject(AliasAnalysis *AA, MachineInstr *MI) {
457 if (MI->isCall() || MI->hasUnmodeledSideEffects() ||
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +0000458 (MI->hasOrderedMemoryRef() &&
Andrew Trickda01ba32012-05-15 18:59:41 +0000459 (!MI->mayLoad() || !MI->isInvariantLoad(AA))))
460 return true;
461 return false;
462}
463
464// This MI might have either incomplete info, or known to be unsafe
465// to deal with (i.e. volatile object).
466static inline bool isUnsafeMemoryObject(MachineInstr *MI,
467 const MachineFrameInfo *MFI) {
468 if (!MI || MI->memoperands_empty())
469 return true;
470 // We purposefully do no check for hasOneMemOperand() here
471 // in hope to trigger an assert downstream in order to
472 // finish implementation.
473 if ((*MI->memoperands_begin())->isVolatile() ||
474 MI->hasUnmodeledSideEffects())
475 return true;
Andrew Trickda01ba32012-05-15 18:59:41 +0000476 const Value *V = (*MI->memoperands_begin())->getValue();
477 if (!V)
478 return true;
479
Hal Finkel66859ae2012-12-10 18:49:16 +0000480 SmallVector<Value *, 4> Objs;
481 getUnderlyingObjects(V, Objs);
Craig Toppere1c1d362013-07-03 05:11:49 +0000482 for (SmallVectorImpl<Value *>::iterator I = Objs.begin(),
483 IE = Objs.end(); I != IE; ++I) {
Hal Finkel66859ae2012-12-10 18:49:16 +0000484 V = *I;
485
486 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
487 // Similarly to getUnderlyingObjectForInstr:
488 // For now, ignore PseudoSourceValues which may alias LLVM IR values
489 // because the code that uses this function has no way to cope with
490 // such aliases.
491 if (PSV->isAliased(MFI))
492 return true;
493 }
494
495 // Does this pointer refer to a distinct and identifiable object?
496 if (!isIdentifiedObject(V))
Andrew Trickda01ba32012-05-15 18:59:41 +0000497 return true;
498 }
Andrew Trickda01ba32012-05-15 18:59:41 +0000499
500 return false;
501}
502
503/// This returns true if the two MIs need a chain edge betwee them.
504/// If these are not even memory operations, we still may need
505/// chain deps between them. The question really is - could
506/// these two MIs be reordered during scheduling from memory dependency
507/// point of view.
508static bool MIsNeedChainEdge(AliasAnalysis *AA, const MachineFrameInfo *MFI,
509 MachineInstr *MIa,
510 MachineInstr *MIb) {
511 // Cover a trivial case - no edge is need to itself.
512 if (MIa == MIb)
513 return false;
514
Hal Finkel2150e3a2014-01-08 21:52:02 +0000515 // FIXME: Need to handle multiple memory operands to support all targets.
516 if (!MIa->hasOneMemOperand() || !MIb->hasOneMemOperand())
517 return true;
518
Andrew Trickda01ba32012-05-15 18:59:41 +0000519 if (isUnsafeMemoryObject(MIa, MFI) || isUnsafeMemoryObject(MIb, MFI))
520 return true;
521
522 // If we are dealing with two "normal" loads, we do not need an edge
523 // between them - they could be reordered.
524 if (!MIa->mayStore() && !MIb->mayStore())
525 return false;
526
527 // To this point analysis is generic. From here on we do need AA.
528 if (!AA)
529 return true;
530
531 MachineMemOperand *MMOa = *MIa->memoperands_begin();
532 MachineMemOperand *MMOb = *MIb->memoperands_begin();
533
Andrew Trickda01ba32012-05-15 18:59:41 +0000534 // The following interface to AA is fashioned after DAGCombiner::isAlias
535 // and operates with MachineMemOperand offset with some important
536 // assumptions:
537 // - LLVM fundamentally assumes flat address spaces.
538 // - MachineOperand offset can *only* result from legalization and
539 // cannot affect queries other than the trivial case of overlap
540 // checking.
541 // - These offsets never wrap and never step outside
542 // of allocated objects.
543 // - There should never be any negative offsets here.
544 //
545 // FIXME: Modify API to hide this math from "user"
546 // FIXME: Even before we go to AA we can reason locally about some
547 // memory objects. It can save compile time, and possibly catch some
548 // corner cases not currently covered.
549
550 assert ((MMOa->getOffset() >= 0) && "Negative MachineMemOperand offset");
551 assert ((MMOb->getOffset() >= 0) && "Negative MachineMemOperand offset");
552
553 int64_t MinOffset = std::min(MMOa->getOffset(), MMOb->getOffset());
554 int64_t Overlapa = MMOa->getSize() + MMOa->getOffset() - MinOffset;
555 int64_t Overlapb = MMOb->getSize() + MMOb->getOffset() - MinOffset;
556
557 AliasAnalysis::AliasResult AAResult = AA->alias(
558 AliasAnalysis::Location(MMOa->getValue(), Overlapa,
559 MMOa->getTBAAInfo()),
560 AliasAnalysis::Location(MMOb->getValue(), Overlapb,
561 MMOb->getTBAAInfo()));
562
563 return (AAResult != AliasAnalysis::NoAlias);
564}
565
566/// This recursive function iterates over chain deps of SUb looking for
567/// "latest" node that needs a chain edge to SUa.
568static unsigned
569iterateChainSucc(AliasAnalysis *AA, const MachineFrameInfo *MFI,
570 SUnit *SUa, SUnit *SUb, SUnit *ExitSU, unsigned *Depth,
571 SmallPtrSet<const SUnit*, 16> &Visited) {
572 if (!SUa || !SUb || SUb == ExitSU)
573 return *Depth;
574
575 // Remember visited nodes.
576 if (!Visited.insert(SUb))
577 return *Depth;
578 // If there is _some_ dependency already in place, do not
579 // descend any further.
580 // TODO: Need to make sure that if that dependency got eliminated or ignored
581 // for any reason in the future, we would not violate DAG topology.
582 // Currently it does not happen, but makes an implicit assumption about
583 // future implementation.
584 //
585 // Independently, if we encounter node that is some sort of global
586 // object (like a call) we already have full set of dependencies to it
587 // and we can stop descending.
588 if (SUa->isSucc(SUb) ||
589 isGlobalMemoryObject(AA, SUb->getInstr()))
590 return *Depth;
591
592 // If we do need an edge, or we have exceeded depth budget,
593 // add that edge to the predecessors chain of SUb,
594 // and stop descending.
595 if (*Depth > 200 ||
596 MIsNeedChainEdge(AA, MFI, SUa->getInstr(), SUb->getInstr())) {
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000597 SUb->addPred(SDep(SUa, SDep::MayAliasMem));
Andrew Trickda01ba32012-05-15 18:59:41 +0000598 return *Depth;
599 }
600 // Track current depth.
601 (*Depth)++;
602 // Iterate over chain dependencies only.
603 for (SUnit::const_succ_iterator I = SUb->Succs.begin(), E = SUb->Succs.end();
604 I != E; ++I)
605 if (I->isCtrl())
606 iterateChainSucc (AA, MFI, SUa, I->getSUnit(), ExitSU, Depth, Visited);
607 return *Depth;
608}
609
610/// This function assumes that "downward" from SU there exist
611/// tail/leaf of already constructed DAG. It iterates downward and
612/// checks whether SU can be aliasing any node dominated
613/// by it.
614static void adjustChainDeps(AliasAnalysis *AA, const MachineFrameInfo *MFI,
Andrew Trick344fb642012-06-13 02:39:03 +0000615 SUnit *SU, SUnit *ExitSU, std::set<SUnit *> &CheckList,
616 unsigned LatencyToLoad) {
Andrew Trickda01ba32012-05-15 18:59:41 +0000617 if (!SU)
618 return;
619
620 SmallPtrSet<const SUnit*, 16> Visited;
621 unsigned Depth = 0;
622
623 for (std::set<SUnit *>::iterator I = CheckList.begin(), IE = CheckList.end();
624 I != IE; ++I) {
625 if (SU == *I)
626 continue;
Andrew Trick344fb642012-06-13 02:39:03 +0000627 if (MIsNeedChainEdge(AA, MFI, SU->getInstr(), (*I)->getInstr())) {
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000628 SDep Dep(SU, SDep::MayAliasMem);
629 Dep.setLatency(((*I)->getInstr()->mayLoad()) ? LatencyToLoad : 0);
630 (*I)->addPred(Dep);
Andrew Trick344fb642012-06-13 02:39:03 +0000631 }
Andrew Trickda01ba32012-05-15 18:59:41 +0000632 // Now go through all the chain successors and iterate from them.
633 // Keep track of visited nodes.
634 for (SUnit::const_succ_iterator J = (*I)->Succs.begin(),
635 JE = (*I)->Succs.end(); J != JE; ++J)
636 if (J->isCtrl())
637 iterateChainSucc (AA, MFI, SU, J->getSUnit(),
638 ExitSU, &Depth, Visited);
639 }
640}
641
642/// Check whether two objects need a chain edge, if so, add it
643/// otherwise remember the rejected SU.
644static inline
645void addChainDependency (AliasAnalysis *AA, const MachineFrameInfo *MFI,
646 SUnit *SUa, SUnit *SUb,
647 std::set<SUnit *> &RejectList,
648 unsigned TrueMemOrderLatency = 0,
649 bool isNormalMemory = false) {
650 // If this is a false dependency,
651 // do not add the edge, but rememeber the rejected node.
Hal Finkelb350ffd2013-08-29 03:25:05 +0000652 if (!AA || MIsNeedChainEdge(AA, MFI, SUa->getInstr(), SUb->getInstr())) {
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000653 SDep Dep(SUa, isNormalMemory ? SDep::MayAliasMem : SDep::Barrier);
654 Dep.setLatency(TrueMemOrderLatency);
655 SUb->addPred(Dep);
656 }
Andrew Trickda01ba32012-05-15 18:59:41 +0000657 else {
658 // Duplicate entries should be ignored.
659 RejectList.insert(SUb);
660 DEBUG(dbgs() << "\tReject chain dep between SU("
661 << SUa->NodeNum << ") and SU("
662 << SUb->NodeNum << ")\n");
663 }
664}
665
Andrew Trick46cc9a42012-02-22 06:08:11 +0000666/// Create an SUnit for each real instruction, numbered in top-down toplological
667/// order. The instruction order A < B, implies that no edge exists from B to A.
668///
669/// Map each real instruction to its SUnit.
670///
Andrew Trick8823dec2012-03-14 04:00:41 +0000671/// After initSUnits, the SUnits vector cannot be resized and the scheduler may
672/// hang onto SUnit pointers. We may relax this in the future by using SUnit IDs
673/// instead of pointers.
674///
675/// MachineScheduler relies on initSUnits numbering the nodes by their order in
676/// the original instruction list.
Andrew Trick46cc9a42012-02-22 06:08:11 +0000677void ScheduleDAGInstrs::initSUnits() {
678 // We'll be allocating one SUnit for each real instruction in the region,
679 // which is contained within a basic block.
Andrew Tricka53e1012013-08-23 17:48:33 +0000680 SUnits.reserve(NumRegionInstrs);
Andrew Trick46cc9a42012-02-22 06:08:11 +0000681
Andrew Trick8c207e42012-03-09 04:29:02 +0000682 for (MachineBasicBlock::iterator I = RegionBegin; I != RegionEnd; ++I) {
Andrew Trick46cc9a42012-02-22 06:08:11 +0000683 MachineInstr *MI = I;
684 if (MI->isDebugValue())
685 continue;
686
Andrew Trick52226d42012-03-07 23:00:49 +0000687 SUnit *SU = newSUnit(MI);
Andrew Trick46cc9a42012-02-22 06:08:11 +0000688 MISUnitMap[MI] = SU;
689
690 SU->isCall = MI->isCall();
691 SU->isCommutable = MI->isCommutable();
692
693 // Assign the Latency field of SU using target-provided information.
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000694 SU->Latency = SchedModel.computeInstrLatency(SU->getInstr());
Andrew Trick880e5732013-12-05 17:55:58 +0000695
696 // If this SUnit uses an unbuffered resource, mark it as such.
697 // These resources are used for in-order execution pipelines within an
698 // out-of-order core and are identified by BufferSize=1. BufferSize=0 is
699 // used for dispatch/issue groups and is not considered here.
700 if (SchedModel.hasInstrSchedModel()) {
701 const MCSchedClassDesc *SC = getSchedClass(SU);
702 for (TargetSchedModel::ProcResIter
703 PI = SchedModel.getWriteProcResBegin(SC),
704 PE = SchedModel.getWriteProcResEnd(SC); PI != PE; ++PI) {
Andrew Trick5a22df42013-12-05 17:56:02 +0000705 switch (SchedModel.getProcResource(PI->ProcResourceIdx)->BufferSize) {
706 case 0:
707 SU->hasReservedResource = true;
708 break;
709 case 1:
Andrew Trick880e5732013-12-05 17:55:58 +0000710 SU->isUnbuffered = true;
711 break;
Andrew Trick5a22df42013-12-05 17:56:02 +0000712 default:
713 break;
Andrew Trick880e5732013-12-05 17:55:58 +0000714 }
715 }
716 }
Andrew Trick46cc9a42012-02-22 06:08:11 +0000717 }
Andrew Trickdbee9d82012-01-14 02:17:15 +0000718}
719
Alp Tokerf907b892013-12-05 05:44:44 +0000720/// If RegPressure is non-null, compute register pressure as a side effect. The
Andrew Trick88639922012-04-24 17:56:43 +0000721/// DAG builder is an efficient place to do it because it already visits
722/// operands.
723void ScheduleDAGInstrs::buildSchedGraph(AliasAnalysis *AA,
Andrew Trick1a831342013-08-30 03:49:48 +0000724 RegPressureTracker *RPTracker,
725 PressureDiffs *PDiffs) {
Hal Finkelb350ffd2013-08-29 03:25:05 +0000726 const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>();
727 bool UseAA = EnableAASchedMI.getNumOccurrences() > 0 ? EnableAASchedMI
728 : ST.useAA();
729 AliasAnalysis *AAForDep = UseAA ? AA : 0;
730
Andrew Trick310190e2013-09-04 21:00:02 +0000731 MISUnitMap.clear();
732 ScheduleDAG::clearDAG();
733
Andrew Trick46cc9a42012-02-22 06:08:11 +0000734 // Create an SUnit for each real instruction.
735 initSUnits();
Dan Gohman60cb69e2008-11-19 23:18:57 +0000736
Andrew Trick1a831342013-08-30 03:49:48 +0000737 if (PDiffs)
738 PDiffs->init(SUnits.size());
739
Dan Gohman3aab10b2008-12-04 01:35:46 +0000740 // We build scheduling units by walking a block's instruction list from bottom
741 // to top.
742
David Goodwind2f9c042009-11-09 19:22:17 +0000743 // Remember where a generic side-effecting instruction is as we procede.
744 SUnit *BarrierChain = 0, *AliasChain = 0;
Dan Gohman3aab10b2008-12-04 01:35:46 +0000745
David Goodwind2f9c042009-11-09 19:22:17 +0000746 // Memory references to specific known memory locations are tracked
747 // so that they can be given more precise dependencies. We track
748 // separately the known memory locations that may alias and those
749 // that are known not to alias
Hal Finkela228a812014-01-20 14:03:02 +0000750 MapVector<const Value *, std::vector<SUnit *> > AliasMemDefs, NonAliasMemDefs;
Sergei Larine8221482012-11-15 17:45:50 +0000751 MapVector<const Value *, std::vector<SUnit *> > AliasMemUses, NonAliasMemUses;
Andrew Trickda01ba32012-05-15 18:59:41 +0000752 std::set<SUnit*> RejectMemNodes;
Dan Gohman3aab10b2008-12-04 01:35:46 +0000753
Dale Johannesen49de0602010-03-10 22:13:47 +0000754 // Remove any stale debug info; sometimes BuildSchedGraph is called again
755 // without emitting the info from the previous call.
Devang Patele5feef02011-06-02 20:07:12 +0000756 DbgValues.clear();
757 FirstDbgValue = NULL;
Dale Johannesen49de0602010-03-10 22:13:47 +0000758
Andrew Trickd675a4c2012-02-23 01:52:38 +0000759 assert(Defs.empty() && Uses.empty() &&
760 "Only BuildGraph should update Defs/Uses");
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000761 Defs.setUniverse(TRI->getNumRegs());
762 Uses.setUniverse(TRI->getNumRegs());
Andrew Trick2e116a42011-05-06 21:52:52 +0000763
Andrew Trickd458e2d2012-02-22 21:59:00 +0000764 assert(VRegDefs.empty() && "Only BuildSchedGraph may access VRegDefs");
Andrew Trick8dd26f02013-08-23 17:48:39 +0000765 VRegUses.clear();
Andrew Trickd458e2d2012-02-22 21:59:00 +0000766 VRegDefs.setUniverse(MRI.getNumVirtRegs());
Andrew Trick8dd26f02013-08-23 17:48:39 +0000767 VRegUses.setUniverse(MRI.getNumVirtRegs());
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000768
Andrew Trickd675a4c2012-02-23 01:52:38 +0000769 // Model data dependencies between instructions being scheduled and the
770 // ExitSU.
Andrew Trick52226d42012-03-07 23:00:49 +0000771 addSchedBarrierDeps();
Andrew Trickd675a4c2012-02-23 01:52:38 +0000772
Dan Gohmanb9543432009-02-10 23:27:53 +0000773 // Walk the list of instructions, from bottom moving up.
Andrew Trickb767d1e2012-12-01 01:22:49 +0000774 MachineInstr *DbgMI = NULL;
Andrew Trick8c207e42012-03-09 04:29:02 +0000775 for (MachineBasicBlock::iterator MII = RegionEnd, MIE = RegionBegin;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000776 MII != MIE; --MII) {
777 MachineInstr *MI = prior(MII);
Andrew Trickb767d1e2012-12-01 01:22:49 +0000778 if (MI && DbgMI) {
779 DbgValues.push_back(std::make_pair(DbgMI, MI));
780 DbgMI = NULL;
Devang Patele5feef02011-06-02 20:07:12 +0000781 }
782
Dale Johannesen49de0602010-03-10 22:13:47 +0000783 if (MI->isDebugValue()) {
Andrew Trickb767d1e2012-12-01 01:22:49 +0000784 DbgMI = MI;
Dale Johannesen49de0602010-03-10 22:13:47 +0000785 continue;
786 }
Andrew Trick1a831342013-08-30 03:49:48 +0000787 SUnit *SU = MISUnitMap[MI];
788 assert(SU && "No SUnit mapped to this MI");
789
Andrew Trick88639922012-04-24 17:56:43 +0000790 if (RPTracker) {
Andrew Trick1a831342013-08-30 03:49:48 +0000791 PressureDiff *PDiff = PDiffs ? &(*PDiffs)[SU->NodeNum] : 0;
Andrew Trick2bc74c22013-08-30 04:36:57 +0000792 RPTracker->recede(/*LiveUses=*/0, PDiff);
Andrew Trick88639922012-04-24 17:56:43 +0000793 assert(RPTracker->getPos() == prior(MII) && "RPTracker can't find MI");
794 }
Devang Patele5feef02011-06-02 20:07:12 +0000795
Sergei Larin5e76aa92013-02-12 16:36:03 +0000796 assert((CanHandleTerminators || (!MI->isTerminator() && !MI->isLabel())) &&
Dan Gohmanb9543432009-02-10 23:27:53 +0000797 "Cannot schedule terminators or labels!");
Dan Gohman60cb69e2008-11-19 23:18:57 +0000798
Dan Gohman3aab10b2008-12-04 01:35:46 +0000799 // Add register-based dependencies (data, anti, and output).
Andrew Trickec256482012-12-18 20:53:01 +0000800 bool HasVRegDef = false;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000801 for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) {
802 const MachineOperand &MO = MI->getOperand(j);
803 if (!MO.isReg()) continue;
804 unsigned Reg = MO.getReg();
805 if (Reg == 0) continue;
806
Andrew Trickdbee9d82012-01-14 02:17:15 +0000807 if (TRI->isPhysicalRegister(Reg))
808 addPhysRegDeps(SU, j);
809 else {
810 assert(!IsPostRA && "Virtual register encountered!");
Andrew Trickec256482012-12-18 20:53:01 +0000811 if (MO.isDef()) {
812 HasVRegDef = true;
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000813 addVRegDefDeps(SU, j);
Andrew Trickec256482012-12-18 20:53:01 +0000814 }
Andrew Trickda6a15d2012-02-23 03:16:24 +0000815 else if (MO.readsReg()) // ignore undef operands
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000816 addVRegUseDeps(SU, j);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000817 }
818 }
Andrew Trickec256482012-12-18 20:53:01 +0000819 // If we haven't seen any uses in this scheduling region, create a
820 // dependence edge to ExitSU to model the live-out latency. This is required
821 // for vreg defs with no in-region use, and prefetches with no vreg def.
822 //
823 // FIXME: NumDataSuccs would be more precise than NumSuccs here. This
824 // check currently relies on being called before adding chain deps.
825 if (SU->NumSuccs == 0 && SU->Latency > 1
826 && (HasVRegDef || MI->mayLoad())) {
827 SDep Dep(SU, SDep::Artificial);
828 Dep.setLatency(SU->Latency - 1);
829 ExitSU.addPred(Dep);
830 }
Dan Gohman3aab10b2008-12-04 01:35:46 +0000831
832 // Add chain dependencies.
David Goodwin00822aa2009-11-02 17:06:28 +0000833 // Chain dependencies used to enforce memory order should have
834 // latency of 0 (except for true dependency of Store followed by
835 // aliased Load... we estimate that with a single cycle of latency
836 // assuming the hardware will bypass)
Dan Gohman3aab10b2008-12-04 01:35:46 +0000837 // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable
838 // after stack slots are lowered to actual addresses.
839 // TODO: Use an AliasAnalysis and do real alias-analysis queries, and
840 // produce more precise dependence information.
Andrew Trick344fb642012-06-13 02:39:03 +0000841 unsigned TrueMemOrderLatency = MI->mayStore() ? 1 : 0;
Andrew Trickda01ba32012-05-15 18:59:41 +0000842 if (isGlobalMemoryObject(AA, MI)) {
David Goodwind2f9c042009-11-09 19:22:17 +0000843 // Be conservative with these and add dependencies on all memory
844 // references, even those that are known to not alias.
Hal Finkela228a812014-01-20 14:03:02 +0000845 for (MapVector<const Value *, std::vector<SUnit *> >::iterator I =
David Goodwind2f9c042009-11-09 19:22:17 +0000846 NonAliasMemDefs.begin(), E = NonAliasMemDefs.end(); I != E; ++I) {
Hal Finkela228a812014-01-20 14:03:02 +0000847 for (unsigned i = 0, e = I->second.size(); i != e; ++i) {
848 I->second[i]->addPred(SDep(SU, SDep::Barrier));
849 }
Dan Gohman3aab10b2008-12-04 01:35:46 +0000850 }
Sergei Larine8221482012-11-15 17:45:50 +0000851 for (MapVector<const Value *, std::vector<SUnit *> >::iterator I =
David Goodwind2f9c042009-11-09 19:22:17 +0000852 NonAliasMemUses.begin(), E = NonAliasMemUses.end(); I != E; ++I) {
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000853 for (unsigned i = 0, e = I->second.size(); i != e; ++i) {
854 SDep Dep(SU, SDep::Barrier);
855 Dep.setLatency(TrueMemOrderLatency);
856 I->second[i]->addPred(Dep);
857 }
Dan Gohman3aab10b2008-12-04 01:35:46 +0000858 }
David Goodwind2f9c042009-11-09 19:22:17 +0000859 // Add SU to the barrier chain.
860 if (BarrierChain)
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000861 BarrierChain->addPred(SDep(SU, SDep::Barrier));
David Goodwind2f9c042009-11-09 19:22:17 +0000862 BarrierChain = SU;
Andrew Trickda01ba32012-05-15 18:59:41 +0000863 // This is a barrier event that acts as a pivotal node in the DAG,
864 // so it is safe to clear list of exposed nodes.
Andrew Trick344fb642012-06-13 02:39:03 +0000865 adjustChainDeps(AA, MFI, SU, &ExitSU, RejectMemNodes,
866 TrueMemOrderLatency);
Andrew Trickda01ba32012-05-15 18:59:41 +0000867 RejectMemNodes.clear();
868 NonAliasMemDefs.clear();
869 NonAliasMemUses.clear();
David Goodwind2f9c042009-11-09 19:22:17 +0000870
871 // fall-through
872 new_alias_chain:
873 // Chain all possibly aliasing memory references though SU.
Andrew Trick344fb642012-06-13 02:39:03 +0000874 if (AliasChain) {
875 unsigned ChainLatency = 0;
876 if (AliasChain->getInstr()->mayLoad())
877 ChainLatency = TrueMemOrderLatency;
Hal Finkelb350ffd2013-08-29 03:25:05 +0000878 addChainDependency(AAForDep, MFI, SU, AliasChain, RejectMemNodes,
Andrew Trick344fb642012-06-13 02:39:03 +0000879 ChainLatency);
880 }
David Goodwind2f9c042009-11-09 19:22:17 +0000881 AliasChain = SU;
882 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
Hal Finkelb350ffd2013-08-29 03:25:05 +0000883 addChainDependency(AAForDep, MFI, SU, PendingLoads[k], RejectMemNodes,
Andrew Trickda01ba32012-05-15 18:59:41 +0000884 TrueMemOrderLatency);
Hal Finkela228a812014-01-20 14:03:02 +0000885 for (MapVector<const Value *, std::vector<SUnit *> >::iterator I =
886 AliasMemDefs.begin(), E = AliasMemDefs.end(); I != E; ++I) {
887 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
888 addChainDependency(AAForDep, MFI, SU, I->second[i], RejectMemNodes);
889 }
Sergei Larine8221482012-11-15 17:45:50 +0000890 for (MapVector<const Value *, std::vector<SUnit *> >::iterator I =
David Goodwind2f9c042009-11-09 19:22:17 +0000891 AliasMemUses.begin(), E = AliasMemUses.end(); I != E; ++I) {
892 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Hal Finkelb350ffd2013-08-29 03:25:05 +0000893 addChainDependency(AAForDep, MFI, SU, I->second[i], RejectMemNodes,
Andrew Trickda01ba32012-05-15 18:59:41 +0000894 TrueMemOrderLatency);
David Goodwind2f9c042009-11-09 19:22:17 +0000895 }
Andrew Trick344fb642012-06-13 02:39:03 +0000896 adjustChainDeps(AA, MFI, SU, &ExitSU, RejectMemNodes,
897 TrueMemOrderLatency);
David Goodwind2f9c042009-11-09 19:22:17 +0000898 PendingLoads.clear();
899 AliasMemDefs.clear();
900 AliasMemUses.clear();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000901 } else if (MI->mayStore()) {
Benjamin Kramerfd510922013-06-29 18:41:17 +0000902 UnderlyingObjectsVector Objs;
Hal Finkel66859ae2012-12-10 18:49:16 +0000903 getUnderlyingObjectsForInstr(MI, MFI, Objs);
904
905 if (Objs.empty()) {
906 // Treat all other stores conservatively.
907 goto new_alias_chain;
908 }
909
910 bool MayAlias = false;
Benjamin Kramerfd510922013-06-29 18:41:17 +0000911 for (UnderlyingObjectsVector::iterator K = Objs.begin(), KE = Objs.end();
912 K != KE; ++K) {
913 const Value *V = K->getPointer();
914 bool ThisMayAlias = K->getInt();
Hal Finkel66859ae2012-12-10 18:49:16 +0000915 if (ThisMayAlias)
916 MayAlias = true;
917
Dan Gohman3aab10b2008-12-04 01:35:46 +0000918 // A store to a specific PseudoSourceValue. Add precise dependencies.
David Goodwind2f9c042009-11-09 19:22:17 +0000919 // Record the def in MemDefs, first adding a dep if there is
920 // an existing def.
Hal Finkela228a812014-01-20 14:03:02 +0000921 MapVector<const Value *, std::vector<SUnit *> >::iterator I =
Hal Finkel66859ae2012-12-10 18:49:16 +0000922 ((ThisMayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V));
Hal Finkela228a812014-01-20 14:03:02 +0000923 MapVector<const Value *, std::vector<SUnit *> >::iterator IE =
Hal Finkel66859ae2012-12-10 18:49:16 +0000924 ((ThisMayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end());
David Goodwind2f9c042009-11-09 19:22:17 +0000925 if (I != IE) {
Hal Finkela228a812014-01-20 14:03:02 +0000926 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
927 addChainDependency(AAForDep, MFI, SU, I->second[i], RejectMemNodes,
928 0, true);
929
930 // If we're not using AA, then we only need one store per object.
931 if (!AAForDep)
932 I->second.clear();
933 I->second.push_back(SU);
Dan Gohman3aab10b2008-12-04 01:35:46 +0000934 } else {
Hal Finkela228a812014-01-20 14:03:02 +0000935 if (ThisMayAlias) {
936 if (!AAForDep)
937 AliasMemDefs[V].clear();
938 AliasMemDefs[V].push_back(SU);
939 } else {
940 if (!AAForDep)
941 NonAliasMemDefs[V].clear();
942 NonAliasMemDefs[V].push_back(SU);
943 }
Dan Gohman3aab10b2008-12-04 01:35:46 +0000944 }
945 // Handle the uses in MemUses, if there are any.
Sergei Larine8221482012-11-15 17:45:50 +0000946 MapVector<const Value *, std::vector<SUnit *> >::iterator J =
Hal Finkel66859ae2012-12-10 18:49:16 +0000947 ((ThisMayAlias) ? AliasMemUses.find(V) : NonAliasMemUses.find(V));
Sergei Larine8221482012-11-15 17:45:50 +0000948 MapVector<const Value *, std::vector<SUnit *> >::iterator JE =
Hal Finkel66859ae2012-12-10 18:49:16 +0000949 ((ThisMayAlias) ? AliasMemUses.end() : NonAliasMemUses.end());
David Goodwind2f9c042009-11-09 19:22:17 +0000950 if (J != JE) {
Dan Gohman3aab10b2008-12-04 01:35:46 +0000951 for (unsigned i = 0, e = J->second.size(); i != e; ++i)
Hal Finkelb350ffd2013-08-29 03:25:05 +0000952 addChainDependency(AAForDep, MFI, SU, J->second[i], RejectMemNodes,
Andrew Trickda01ba32012-05-15 18:59:41 +0000953 TrueMemOrderLatency, true);
Dan Gohman3aab10b2008-12-04 01:35:46 +0000954 J->second.clear();
955 }
David Goodwin00822aa2009-11-02 17:06:28 +0000956 }
Hal Finkel66859ae2012-12-10 18:49:16 +0000957 if (MayAlias) {
958 // Add dependencies from all the PendingLoads, i.e. loads
959 // with no underlying object.
960 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
Hal Finkelb350ffd2013-08-29 03:25:05 +0000961 addChainDependency(AAForDep, MFI, SU, PendingLoads[k], RejectMemNodes,
Hal Finkel66859ae2012-12-10 18:49:16 +0000962 TrueMemOrderLatency);
963 // Add dependence on alias chain, if needed.
964 if (AliasChain)
Hal Finkelb350ffd2013-08-29 03:25:05 +0000965 addChainDependency(AAForDep, MFI, SU, AliasChain, RejectMemNodes);
Hal Finkel66859ae2012-12-10 18:49:16 +0000966 // But we also should check dependent instructions for the
967 // SU in question.
968 adjustChainDeps(AA, MFI, SU, &ExitSU, RejectMemNodes,
969 TrueMemOrderLatency);
970 }
971 // Add dependence on barrier chain, if needed.
972 // There is no point to check aliasing on barrier event. Even if
973 // SU and barrier _could_ be reordered, they should not. In addition,
974 // we have lost all RejectMemNodes below barrier.
975 if (BarrierChain)
976 BarrierChain->addPred(SDep(SU, SDep::Barrier));
Evan Cheng15459b62010-10-23 02:10:46 +0000977
978 if (!ExitSU.isPred(SU))
979 // Push store's up a bit to avoid them getting in between cmp
980 // and branches.
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000981 ExitSU.addPred(SDep(SU, SDep::Artificial));
Evan Cheng7f8e5632011-12-07 07:15:52 +0000982 } else if (MI->mayLoad()) {
David Goodwina86f9192009-11-03 20:15:00 +0000983 bool MayAlias = true;
Dan Gohman87b02d52009-10-09 23:27:56 +0000984 if (MI->isInvariantLoad(AA)) {
Dan Gohman3aab10b2008-12-04 01:35:46 +0000985 // Invariant load, no chain dependencies needed!
David Goodwin28ba4f22009-11-05 00:16:44 +0000986 } else {
Benjamin Kramerfd510922013-06-29 18:41:17 +0000987 UnderlyingObjectsVector Objs;
Hal Finkel66859ae2012-12-10 18:49:16 +0000988 getUnderlyingObjectsForInstr(MI, MFI, Objs);
989
990 if (Objs.empty()) {
David Goodwind2f9c042009-11-09 19:22:17 +0000991 // A load with no underlying object. Depend on all
992 // potentially aliasing stores.
Hal Finkela228a812014-01-20 14:03:02 +0000993 for (MapVector<const Value *, std::vector<SUnit *> >::iterator I =
David Goodwind2f9c042009-11-09 19:22:17 +0000994 AliasMemDefs.begin(), E = AliasMemDefs.end(); I != E; ++I)
Hal Finkela228a812014-01-20 14:03:02 +0000995 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
996 addChainDependency(AAForDep, MFI, SU, I->second[i],
997 RejectMemNodes);
Andrew Trick24b1c482011-05-05 19:24:06 +0000998
David Goodwind2f9c042009-11-09 19:22:17 +0000999 PendingLoads.push_back(SU);
1000 MayAlias = true;
Hal Finkel66859ae2012-12-10 18:49:16 +00001001 } else {
1002 MayAlias = false;
1003 }
1004
Benjamin Kramerfd510922013-06-29 18:41:17 +00001005 for (UnderlyingObjectsVector::iterator
Hal Finkel66859ae2012-12-10 18:49:16 +00001006 J = Objs.begin(), JE = Objs.end(); J != JE; ++J) {
Benjamin Kramerfd510922013-06-29 18:41:17 +00001007 const Value *V = J->getPointer();
1008 bool ThisMayAlias = J->getInt();
Hal Finkel66859ae2012-12-10 18:49:16 +00001009
1010 if (ThisMayAlias)
1011 MayAlias = true;
1012
1013 // A load from a specific PseudoSourceValue. Add precise dependencies.
Hal Finkela228a812014-01-20 14:03:02 +00001014 MapVector<const Value *, std::vector<SUnit *> >::iterator I =
Hal Finkel66859ae2012-12-10 18:49:16 +00001015 ((ThisMayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V));
Hal Finkela228a812014-01-20 14:03:02 +00001016 MapVector<const Value *, std::vector<SUnit *> >::iterator IE =
Hal Finkel66859ae2012-12-10 18:49:16 +00001017 ((ThisMayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end());
1018 if (I != IE)
Hal Finkela228a812014-01-20 14:03:02 +00001019 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
1020 addChainDependency(AAForDep, MFI, SU, I->second[i],
1021 RejectMemNodes, 0, true);
Hal Finkel66859ae2012-12-10 18:49:16 +00001022 if (ThisMayAlias)
1023 AliasMemUses[V].push_back(SU);
1024 else
1025 NonAliasMemUses[V].push_back(SU);
David Goodwina86f9192009-11-03 20:15:00 +00001026 }
Andrew Trickda01ba32012-05-15 18:59:41 +00001027 if (MayAlias)
Andrew Trick344fb642012-06-13 02:39:03 +00001028 adjustChainDeps(AA, MFI, SU, &ExitSU, RejectMemNodes, /*Latency=*/0);
David Goodwind2f9c042009-11-09 19:22:17 +00001029 // Add dependencies on alias and barrier chains, if needed.
1030 if (MayAlias && AliasChain)
Hal Finkelb350ffd2013-08-29 03:25:05 +00001031 addChainDependency(AAForDep, MFI, SU, AliasChain, RejectMemNodes);
David Goodwind2f9c042009-11-09 19:22:17 +00001032 if (BarrierChain)
Andrew Trickbaeaabb2012-11-06 03:13:46 +00001033 BarrierChain->addPred(SDep(SU, SDep::Barrier));
Andrew Trick24b1c482011-05-05 19:24:06 +00001034 }
Dan Gohman60cb69e2008-11-19 23:18:57 +00001035 }
Dan Gohman60cb69e2008-11-19 23:18:57 +00001036 }
Andrew Trickb767d1e2012-12-01 01:22:49 +00001037 if (DbgMI)
1038 FirstDbgValue = DbgMI;
Dan Gohman619ef482009-01-15 19:20:50 +00001039
Andrew Trickd675a4c2012-02-23 01:52:38 +00001040 Defs.clear();
1041 Uses.clear();
Andrew Trick59ac4fb2012-01-14 02:17:18 +00001042 VRegDefs.clear();
Dan Gohman619ef482009-01-15 19:20:50 +00001043 PendingLoads.clear();
Dan Gohman60cb69e2008-11-19 23:18:57 +00001044}
1045
Andrew Trick6b104f82013-12-28 21:56:55 +00001046/// \brief Initialize register live-range state for updating kills.
1047void ScheduleDAGInstrs::startBlockForKills(MachineBasicBlock *BB) {
1048 // Start with no live registers.
1049 LiveRegs.reset();
1050
1051 // Examine the live-in regs of all successors.
1052 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
1053 SE = BB->succ_end(); SI != SE; ++SI) {
1054 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
1055 E = (*SI)->livein_end(); I != E; ++I) {
1056 unsigned Reg = *I;
1057 // Repeat, for reg and all subregs.
1058 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
1059 SubRegs.isValid(); ++SubRegs)
1060 LiveRegs.set(*SubRegs);
1061 }
1062 }
1063}
1064
1065bool ScheduleDAGInstrs::toggleKillFlag(MachineInstr *MI, MachineOperand &MO) {
1066 // Setting kill flag...
1067 if (!MO.isKill()) {
1068 MO.setIsKill(true);
1069 return false;
1070 }
1071
1072 // If MO itself is live, clear the kill flag...
1073 if (LiveRegs.test(MO.getReg())) {
1074 MO.setIsKill(false);
1075 return false;
1076 }
1077
1078 // If any subreg of MO is live, then create an imp-def for that
1079 // subreg and keep MO marked as killed.
1080 MO.setIsKill(false);
1081 bool AllDead = true;
1082 const unsigned SuperReg = MO.getReg();
1083 MachineInstrBuilder MIB(MF, MI);
1084 for (MCSubRegIterator SubRegs(SuperReg, TRI); SubRegs.isValid(); ++SubRegs) {
1085 if (LiveRegs.test(*SubRegs)) {
1086 MIB.addReg(*SubRegs, RegState::ImplicitDefine);
1087 AllDead = false;
1088 }
1089 }
1090
1091 if(AllDead)
1092 MO.setIsKill(true);
1093 return false;
1094}
1095
1096// FIXME: Reuse the LivePhysRegs utility for this.
1097void ScheduleDAGInstrs::fixupKills(MachineBasicBlock *MBB) {
1098 DEBUG(dbgs() << "Fixup kills for BB#" << MBB->getNumber() << '\n');
1099
1100 LiveRegs.resize(TRI->getNumRegs());
1101 BitVector killedRegs(TRI->getNumRegs());
1102
1103 startBlockForKills(MBB);
1104
1105 // Examine block from end to start...
1106 unsigned Count = MBB->size();
1107 for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin();
1108 I != E; --Count) {
1109 MachineInstr *MI = --I;
1110 if (MI->isDebugValue())
1111 continue;
1112
1113 // Update liveness. Registers that are defed but not used in this
1114 // instruction are now dead. Mark register and all subregs as they
1115 // are completely defined.
1116 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1117 MachineOperand &MO = MI->getOperand(i);
1118 if (MO.isRegMask())
1119 LiveRegs.clearBitsNotInMask(MO.getRegMask());
1120 if (!MO.isReg()) continue;
1121 unsigned Reg = MO.getReg();
1122 if (Reg == 0) continue;
1123 if (!MO.isDef()) continue;
1124 // Ignore two-addr defs.
1125 if (MI->isRegTiedToUseOperand(i)) continue;
1126
1127 // Repeat for reg and all subregs.
1128 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
1129 SubRegs.isValid(); ++SubRegs)
1130 LiveRegs.reset(*SubRegs);
1131 }
1132
1133 // Examine all used registers and set/clear kill flag. When a
1134 // register is used multiple times we only set the kill flag on
1135 // the first use. Don't set kill flags on undef operands.
1136 killedRegs.reset();
1137 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1138 MachineOperand &MO = MI->getOperand(i);
1139 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
1140 unsigned Reg = MO.getReg();
1141 if ((Reg == 0) || MRI.isReserved(Reg)) continue;
1142
1143 bool kill = false;
1144 if (!killedRegs.test(Reg)) {
1145 kill = true;
1146 // A register is not killed if any subregs are live...
1147 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
1148 if (LiveRegs.test(*SubRegs)) {
1149 kill = false;
1150 break;
1151 }
1152 }
1153
1154 // If subreg is not live, then register is killed if it became
1155 // live in this instruction
1156 if (kill)
1157 kill = !LiveRegs.test(Reg);
1158 }
1159
1160 if (MO.isKill() != kill) {
1161 DEBUG(dbgs() << "Fixing " << MO << " in ");
1162 // Warning: toggleKillFlag may invalidate MO.
1163 toggleKillFlag(MI, MO);
1164 DEBUG(MI->dump());
1165 }
1166
1167 killedRegs.set(Reg);
1168 }
1169
1170 // Mark any used register (that is not using undef) and subregs as
1171 // now live...
1172 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1173 MachineOperand &MO = MI->getOperand(i);
1174 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
1175 unsigned Reg = MO.getReg();
1176 if ((Reg == 0) || MRI.isReserved(Reg)) continue;
1177
1178 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
1179 SubRegs.isValid(); ++SubRegs)
1180 LiveRegs.set(*SubRegs);
1181 }
1182 }
1183}
1184
Dan Gohman60cb69e2008-11-19 23:18:57 +00001185void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
Manman Ren19f49ac2012-09-11 22:23:19 +00001186#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Dan Gohman60cb69e2008-11-19 23:18:57 +00001187 SU->getInstr()->dump();
Manman Ren742534c2012-09-06 19:06:06 +00001188#endif
Dan Gohman60cb69e2008-11-19 23:18:57 +00001189}
1190
1191std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
1192 std::string s;
1193 raw_string_ostream oss(s);
Dan Gohmanb9543432009-02-10 23:27:53 +00001194 if (SU == &EntrySU)
1195 oss << "<entry>";
1196 else if (SU == &ExitSU)
1197 oss << "<exit>";
1198 else
Andrew Trickb36388a2013-01-25 07:45:25 +00001199 SU->getInstr()->print(oss, &TM, /*SkipOpers=*/true);
Dan Gohman60cb69e2008-11-19 23:18:57 +00001200 return oss.str();
1201}
1202
Andrew Trick1b2324d2012-03-07 00:18:22 +00001203/// Return the basic block label. It is not necessarilly unique because a block
1204/// contains multiple scheduling regions. But it is fine for visualization.
1205std::string ScheduleDAGInstrs::getDAGName() const {
1206 return "dag." + BB->getFullName();
1207}
Andrew Trick90f711d2012-10-15 18:02:27 +00001208
Andrew Trick48d392e2012-11-28 05:13:28 +00001209//===----------------------------------------------------------------------===//
1210// SchedDFSResult Implementation
1211//===----------------------------------------------------------------------===//
1212
1213namespace llvm {
1214/// \brief Internal state used to compute SchedDFSResult.
1215class SchedDFSImpl {
1216 SchedDFSResult &R;
1217
1218 /// Join DAG nodes into equivalence classes by their subtree.
1219 IntEqClasses SubtreeClasses;
1220 /// List PredSU, SuccSU pairs that represent data edges between subtrees.
1221 std::vector<std::pair<const SUnit*, const SUnit*> > ConnectionPairs;
1222
Andrew Trickffc80972013-01-25 06:52:27 +00001223 struct RootData {
1224 unsigned NodeID;
1225 unsigned ParentNodeID; // Parent node (member of the parent subtree).
1226 unsigned SubInstrCount; // Instr count in this tree only, not children.
1227
1228 RootData(unsigned id): NodeID(id),
1229 ParentNodeID(SchedDFSResult::InvalidSubtreeID),
1230 SubInstrCount(0) {}
1231
1232 unsigned getSparseSetIndex() const { return NodeID; }
1233 };
1234
1235 SparseSet<RootData> RootSet;
1236
Andrew Trick48d392e2012-11-28 05:13:28 +00001237public:
Andrew Trickffc80972013-01-25 06:52:27 +00001238 SchedDFSImpl(SchedDFSResult &r): R(r), SubtreeClasses(R.DFSNodeData.size()) {
1239 RootSet.setUniverse(R.DFSNodeData.size());
1240 }
Andrew Trick48d392e2012-11-28 05:13:28 +00001241
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001242 /// Return true if this node been visited by the DFS traversal.
1243 ///
1244 /// During visitPostorderNode the Node's SubtreeID is assigned to the Node
1245 /// ID. Later, SubtreeID is updated but remains valid.
Andrew Trick48d392e2012-11-28 05:13:28 +00001246 bool isVisited(const SUnit *SU) const {
Andrew Trickffc80972013-01-25 06:52:27 +00001247 return R.DFSNodeData[SU->NodeNum].SubtreeID
1248 != SchedDFSResult::InvalidSubtreeID;
Andrew Trick48d392e2012-11-28 05:13:28 +00001249 }
1250
1251 /// Initialize this node's instruction count. We don't need to flag the node
1252 /// visited until visitPostorder because the DAG cannot have cycles.
1253 void visitPreorder(const SUnit *SU) {
Andrew Trickffc80972013-01-25 06:52:27 +00001254 R.DFSNodeData[SU->NodeNum].InstrCount =
1255 SU->getInstr()->isTransient() ? 0 : 1;
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001256 }
1257
1258 /// Called once for each node after all predecessors are visited. Revisit this
1259 /// node's predecessors and potentially join them now that we know the ILP of
1260 /// the other predecessors.
1261 void visitPostorderNode(const SUnit *SU) {
1262 // Mark this node as the root of a subtree. It may be joined with its
1263 // successors later.
Andrew Trickffc80972013-01-25 06:52:27 +00001264 R.DFSNodeData[SU->NodeNum].SubtreeID = SU->NodeNum;
1265 RootData RData(SU->NodeNum);
1266 RData.SubInstrCount = SU->getInstr()->isTransient() ? 0 : 1;
Andrew Trick48d392e2012-11-28 05:13:28 +00001267
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001268 // If any predecessors are still in their own subtree, they either cannot be
1269 // joined or are large enough to remain separate. If this parent node's
1270 // total instruction count is not greater than a child subtree by at least
1271 // the subtree limit, then try to join it now since splitting subtrees is
1272 // only useful if multiple high-pressure paths are possible.
Andrew Trickffc80972013-01-25 06:52:27 +00001273 unsigned InstrCount = R.DFSNodeData[SU->NodeNum].InstrCount;
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001274 for (SUnit::const_pred_iterator
1275 PI = SU->Preds.begin(), PE = SU->Preds.end(); PI != PE; ++PI) {
1276 if (PI->getKind() != SDep::Data)
1277 continue;
1278 unsigned PredNum = PI->getSUnit()->NodeNum;
Andrew Trickffc80972013-01-25 06:52:27 +00001279 if ((InstrCount - R.DFSNodeData[PredNum].InstrCount) < R.SubtreeLimit)
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001280 joinPredSubtree(*PI, SU, /*CheckLimit=*/false);
Andrew Trickffc80972013-01-25 06:52:27 +00001281
1282 // Either link or merge the TreeData entry from the child to the parent.
Andrew Trick646eeb62013-01-25 06:52:30 +00001283 if (R.DFSNodeData[PredNum].SubtreeID == PredNum) {
1284 // If the predecessor's parent is invalid, this is a tree edge and the
1285 // current node is the parent.
1286 if (RootSet[PredNum].ParentNodeID == SchedDFSResult::InvalidSubtreeID)
1287 RootSet[PredNum].ParentNodeID = SU->NodeNum;
1288 }
1289 else if (RootSet.count(PredNum)) {
1290 // The predecessor is not a root, but is still in the root set. This
1291 // must be the new parent that it was just joined to. Note that
1292 // RootSet[PredNum].ParentNodeID may either be invalid or may still be
1293 // set to the original parent.
Andrew Trickffc80972013-01-25 06:52:27 +00001294 RData.SubInstrCount += RootSet[PredNum].SubInstrCount;
1295 RootSet.erase(PredNum);
1296 }
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001297 }
Andrew Trickffc80972013-01-25 06:52:27 +00001298 RootSet[SU->NodeNum] = RData;
1299 }
1300
1301 /// Called once for each tree edge after calling visitPostOrderNode on the
1302 /// predecessor. Increment the parent node's instruction count and
1303 /// preemptively join this subtree to its parent's if it is small enough.
1304 void visitPostorderEdge(const SDep &PredDep, const SUnit *Succ) {
1305 R.DFSNodeData[Succ->NodeNum].InstrCount
1306 += R.DFSNodeData[PredDep.getSUnit()->NodeNum].InstrCount;
1307 joinPredSubtree(PredDep, Succ);
Andrew Trick48d392e2012-11-28 05:13:28 +00001308 }
1309
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001310 /// Add a connection for cross edges.
1311 void visitCrossEdge(const SDep &PredDep, const SUnit *Succ) {
Andrew Trick48d392e2012-11-28 05:13:28 +00001312 ConnectionPairs.push_back(std::make_pair(PredDep.getSUnit(), Succ));
1313 }
1314
1315 /// Set each node's subtree ID to the representative ID and record connections
1316 /// between trees.
1317 void finalize() {
1318 SubtreeClasses.compress();
Andrew Trickffc80972013-01-25 06:52:27 +00001319 R.DFSTreeData.resize(SubtreeClasses.getNumClasses());
1320 assert(SubtreeClasses.getNumClasses() == RootSet.size()
1321 && "number of roots should match trees");
1322 for (SparseSet<RootData>::const_iterator
1323 RI = RootSet.begin(), RE = RootSet.end(); RI != RE; ++RI) {
1324 unsigned TreeID = SubtreeClasses[RI->NodeID];
1325 if (RI->ParentNodeID != SchedDFSResult::InvalidSubtreeID)
1326 R.DFSTreeData[TreeID].ParentTreeID = SubtreeClasses[RI->ParentNodeID];
1327 R.DFSTreeData[TreeID].SubInstrCount = RI->SubInstrCount;
Andrew Trick646eeb62013-01-25 06:52:30 +00001328 // Note that SubInstrCount may be greater than InstrCount if we joined
1329 // subtrees across a cross edge. InstrCount will be attributed to the
1330 // original parent, while SubInstrCount will be attributed to the joined
1331 // parent.
Andrew Trickffc80972013-01-25 06:52:27 +00001332 }
Andrew Trick48d392e2012-11-28 05:13:28 +00001333 R.SubtreeConnections.resize(SubtreeClasses.getNumClasses());
1334 R.SubtreeConnectLevels.resize(SubtreeClasses.getNumClasses());
1335 DEBUG(dbgs() << R.getNumSubtrees() << " subtrees:\n");
Andrew Trickffc80972013-01-25 06:52:27 +00001336 for (unsigned Idx = 0, End = R.DFSNodeData.size(); Idx != End; ++Idx) {
1337 R.DFSNodeData[Idx].SubtreeID = SubtreeClasses[Idx];
Andrew Trick48d392e2012-11-28 05:13:28 +00001338 DEBUG(dbgs() << " SU(" << Idx << ") in tree "
Andrew Trickffc80972013-01-25 06:52:27 +00001339 << R.DFSNodeData[Idx].SubtreeID << '\n');
Andrew Trick48d392e2012-11-28 05:13:28 +00001340 }
1341 for (std::vector<std::pair<const SUnit*, const SUnit*> >::const_iterator
1342 I = ConnectionPairs.begin(), E = ConnectionPairs.end();
1343 I != E; ++I) {
1344 unsigned PredTree = SubtreeClasses[I->first->NodeNum];
1345 unsigned SuccTree = SubtreeClasses[I->second->NodeNum];
1346 if (PredTree == SuccTree)
1347 continue;
1348 unsigned Depth = I->first->getDepth();
1349 addConnection(PredTree, SuccTree, Depth);
1350 addConnection(SuccTree, PredTree, Depth);
1351 }
1352 }
1353
1354protected:
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001355 /// Join the predecessor subtree with the successor that is its DFS
1356 /// parent. Apply some heuristics before joining.
1357 bool joinPredSubtree(const SDep &PredDep, const SUnit *Succ,
1358 bool CheckLimit = true) {
1359 assert(PredDep.getKind() == SDep::Data && "Subtrees are for data edges");
1360
1361 // Check if the predecessor is already joined.
1362 const SUnit *PredSU = PredDep.getSUnit();
1363 unsigned PredNum = PredSU->NodeNum;
Andrew Trickffc80972013-01-25 06:52:27 +00001364 if (R.DFSNodeData[PredNum].SubtreeID != PredNum)
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001365 return false;
Andrew Trickb52a8562013-01-25 00:12:57 +00001366
1367 // Four is the magic number of successors before a node is considered a
1368 // pinch point.
1369 unsigned NumDataSucs = 0;
Andrew Trickb52a8562013-01-25 00:12:57 +00001370 for (SUnit::const_succ_iterator SI = PredSU->Succs.begin(),
1371 SE = PredSU->Succs.end(); SI != SE; ++SI) {
1372 if (SI->getKind() == SDep::Data) {
1373 if (++NumDataSucs >= 4)
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001374 return false;
Andrew Trickb52a8562013-01-25 00:12:57 +00001375 }
1376 }
Andrew Trickffc80972013-01-25 06:52:27 +00001377 if (CheckLimit && R.DFSNodeData[PredNum].InstrCount > R.SubtreeLimit)
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001378 return false;
Andrew Trickffc80972013-01-25 06:52:27 +00001379 R.DFSNodeData[PredNum].SubtreeID = Succ->NodeNum;
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001380 SubtreeClasses.join(Succ->NodeNum, PredNum);
1381 return true;
Andrew Trickb52a8562013-01-25 00:12:57 +00001382 }
1383
Andrew Trick48d392e2012-11-28 05:13:28 +00001384 /// Called by finalize() to record a connection between trees.
1385 void addConnection(unsigned FromTree, unsigned ToTree, unsigned Depth) {
1386 if (!Depth)
1387 return;
1388
Andrew Trickffc80972013-01-25 06:52:27 +00001389 do {
1390 SmallVectorImpl<SchedDFSResult::Connection> &Connections =
1391 R.SubtreeConnections[FromTree];
1392 for (SmallVectorImpl<SchedDFSResult::Connection>::iterator
1393 I = Connections.begin(), E = Connections.end(); I != E; ++I) {
1394 if (I->TreeID == ToTree) {
1395 I->Level = std::max(I->Level, Depth);
1396 return;
1397 }
Andrew Trick48d392e2012-11-28 05:13:28 +00001398 }
Andrew Trickffc80972013-01-25 06:52:27 +00001399 Connections.push_back(SchedDFSResult::Connection(ToTree, Depth));
1400 FromTree = R.DFSTreeData[FromTree].ParentTreeID;
1401 } while (FromTree != SchedDFSResult::InvalidSubtreeID);
Andrew Trick48d392e2012-11-28 05:13:28 +00001402 }
1403};
1404} // namespace llvm
1405
Andrew Trick90f711d2012-10-15 18:02:27 +00001406namespace {
1407/// \brief Manage the stack used by a reverse depth-first search over the DAG.
1408class SchedDAGReverseDFS {
1409 std::vector<std::pair<const SUnit*, SUnit::const_pred_iterator> > DFSStack;
1410public:
1411 bool isComplete() const { return DFSStack.empty(); }
1412
1413 void follow(const SUnit *SU) {
1414 DFSStack.push_back(std::make_pair(SU, SU->Preds.begin()));
1415 }
1416 void advance() { ++DFSStack.back().second; }
1417
Andrew Trick48d392e2012-11-28 05:13:28 +00001418 const SDep *backtrack() {
1419 DFSStack.pop_back();
1420 return DFSStack.empty() ? 0 : llvm::prior(DFSStack.back().second);
1421 }
Andrew Trick90f711d2012-10-15 18:02:27 +00001422
1423 const SUnit *getCurr() const { return DFSStack.back().first; }
1424
1425 SUnit::const_pred_iterator getPred() const { return DFSStack.back().second; }
1426
1427 SUnit::const_pred_iterator getPredEnd() const {
1428 return getCurr()->Preds.end();
1429 }
1430};
1431} // anonymous
1432
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001433static bool hasDataSucc(const SUnit *SU) {
1434 for (SUnit::const_succ_iterator
1435 SI = SU->Succs.begin(), SE = SU->Succs.end(); SI != SE; ++SI) {
Andrew Trick646eeb62013-01-25 06:52:30 +00001436 if (SI->getKind() == SDep::Data && !SI->getSUnit()->isBoundaryNode())
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001437 return true;
1438 }
1439 return false;
1440}
1441
Andrew Trick90f711d2012-10-15 18:02:27 +00001442/// Compute an ILP metric for all nodes in the subDAG reachable via depth-first
1443/// search from this root.
Andrew Tricke2c3f5c2013-01-25 06:33:57 +00001444void SchedDFSResult::compute(ArrayRef<SUnit> SUnits) {
Andrew Trick90f711d2012-10-15 18:02:27 +00001445 if (!IsBottomUp)
1446 llvm_unreachable("Top-down ILP metric is unimplemnted");
1447
Andrew Trick48d392e2012-11-28 05:13:28 +00001448 SchedDFSImpl Impl(*this);
Andrew Tricke2c3f5c2013-01-25 06:33:57 +00001449 for (ArrayRef<SUnit>::const_iterator
1450 SI = SUnits.begin(), SE = SUnits.end(); SI != SE; ++SI) {
1451 const SUnit *SU = &*SI;
1452 if (Impl.isVisited(SU) || hasDataSucc(SU))
1453 continue;
1454
Andrew Trick48d392e2012-11-28 05:13:28 +00001455 SchedDAGReverseDFS DFS;
Andrew Tricke2c3f5c2013-01-25 06:33:57 +00001456 Impl.visitPreorder(SU);
1457 DFS.follow(SU);
Andrew Trick48d392e2012-11-28 05:13:28 +00001458 for (;;) {
1459 // Traverse the leftmost path as far as possible.
1460 while (DFS.getPred() != DFS.getPredEnd()) {
1461 const SDep &PredDep = *DFS.getPred();
1462 DFS.advance();
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001463 // Ignore non-data edges.
Andrew Trick646eeb62013-01-25 06:52:30 +00001464 if (PredDep.getKind() != SDep::Data
1465 || PredDep.getSUnit()->isBoundaryNode()) {
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001466 continue;
Andrew Trick646eeb62013-01-25 06:52:30 +00001467 }
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001468 // An already visited edge is a cross edge, assuming an acyclic DAG.
Andrew Trick48d392e2012-11-28 05:13:28 +00001469 if (Impl.isVisited(PredDep.getSUnit())) {
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001470 Impl.visitCrossEdge(PredDep, DFS.getCurr());
Andrew Trick48d392e2012-11-28 05:13:28 +00001471 continue;
1472 }
1473 Impl.visitPreorder(PredDep.getSUnit());
1474 DFS.follow(PredDep.getSUnit());
1475 }
1476 // Visit the top of the stack in postorder and backtrack.
1477 const SUnit *Child = DFS.getCurr();
1478 const SDep *PredDep = DFS.backtrack();
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001479 Impl.visitPostorderNode(Child);
1480 if (PredDep)
1481 Impl.visitPostorderEdge(*PredDep, DFS.getCurr());
Andrew Trick48d392e2012-11-28 05:13:28 +00001482 if (DFS.isComplete())
1483 break;
Andrew Trick90f711d2012-10-15 18:02:27 +00001484 }
Andrew Trick48d392e2012-11-28 05:13:28 +00001485 }
1486 Impl.finalize();
1487}
1488
1489/// The root of the given SubtreeID was just scheduled. For all subtrees
1490/// connected to this tree, record the depth of the connection so that the
1491/// nearest connected subtrees can be prioritized.
1492void SchedDFSResult::scheduleTree(unsigned SubtreeID) {
1493 for (SmallVectorImpl<Connection>::const_iterator
1494 I = SubtreeConnections[SubtreeID].begin(),
1495 E = SubtreeConnections[SubtreeID].end(); I != E; ++I) {
1496 SubtreeConnectLevels[I->TreeID] =
1497 std::max(SubtreeConnectLevels[I->TreeID], I->Level);
1498 DEBUG(dbgs() << " Tree: " << I->TreeID
1499 << " @" << SubtreeConnectLevels[I->TreeID] << '\n');
Andrew Trick90f711d2012-10-15 18:02:27 +00001500 }
1501}
1502
1503#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1504void ILPValue::print(raw_ostream &OS) const {
Andrew Trick48d392e2012-11-28 05:13:28 +00001505 OS << InstrCount << " / " << Length << " = ";
1506 if (!Length)
Andrew Trick90f711d2012-10-15 18:02:27 +00001507 OS << "BADILP";
Andrew Trick48d392e2012-11-28 05:13:28 +00001508 else
1509 OS << format("%g", ((double)InstrCount / Length));
Andrew Trick90f711d2012-10-15 18:02:27 +00001510}
1511
1512void ILPValue::dump() const {
1513 dbgs() << *this << '\n';
1514}
1515
1516namespace llvm {
1517
1518raw_ostream &operator<<(raw_ostream &OS, const ILPValue &Val) {
1519 Val.print(OS);
1520 return OS;
1521}
1522
1523} // namespace llvm
1524#endif // !NDEBUG || LLVM_ENABLE_DUMP