blob: 45df5c9d0b57531fe1e417fab47447136783c7c1 [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//
Matthias Braunbd7d9182017-01-27 18:53:00 +000010/// \file This implements the ScheduleDAGInstrs class, which implements
11/// re-scheduling of MachineInstrs.
Dan Gohman60cb69e2008-11-19 23:18:57 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/CodeGen/ScheduleDAGInstrs.h"
Matthias Braun97d0ffb2015-12-04 01:51:19 +000016#include "llvm/ADT/IntEqClasses.h"
Eugene Zelenko7ea69232017-06-01 23:25:02 +000017#include "llvm/ADT/MapVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenko7ea69232017-06-01 23:25:02 +000019#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/SparseSet.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000021#include "llvm/ADT/iterator_range.h"
Dan Gohman1ee0d412009-01-30 02:49:14 +000022#include "llvm/Analysis/AliasAnalysis.h"
Dan Gohmana4fcd242010-12-15 20:02:24 +000023#include "llvm/Analysis/ValueTracking.h"
Matthias Braunf8422972017-12-13 02:51:04 +000024#include "llvm/CodeGen/LiveIntervals.h"
Eugene Zelenko7ea69232017-06-01 23:25:02 +000025#include "llvm/CodeGen/LivePhysRegs.h"
26#include "llvm/CodeGen/MachineBasicBlock.h"
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000028#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko7ea69232017-06-01 23:25:02 +000029#include "llvm/CodeGen/MachineInstr.h"
30#include "llvm/CodeGen/MachineInstrBundle.h"
Dan Gohman48b185d2009-09-25 20:36:54 +000031#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenko7ea69232017-06-01 23:25:02 +000032#include "llvm/CodeGen/MachineOperand.h"
Dan Gohmandddc1ac2008-12-16 03:25:46 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman3aab10b2008-12-04 01:35:46 +000034#include "llvm/CodeGen/PseudoSourceValue.h"
Andrew Trick88517f62012-06-06 19:47:35 +000035#include "llvm/CodeGen/RegisterPressure.h"
Eugene Zelenko7ea69232017-06-01 23:25:02 +000036#include "llvm/CodeGen/ScheduleDAG.h"
Andrew Trickcd1c2f92012-11-28 05:13:24 +000037#include "llvm/CodeGen/ScheduleDFS.h"
Eugene Zelenko7ea69232017-06-01 23:25:02 +000038#include "llvm/CodeGen/SlotIndexes.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000039#include "llvm/CodeGen/TargetRegisterInfo.h"
40#include "llvm/CodeGen/TargetSubtargetInfo.h"
Nico Weber432a3882018-04-30 14:59:11 +000041#include "llvm/Config/llvm-config.h"
Eugene Zelenko7ea69232017-06-01 23:25:02 +000042#include "llvm/IR/Constants.h"
Jonas Paulssonac29f012016-02-03 17:52:29 +000043#include "llvm/IR/Function.h"
Eugene Zelenko7ea69232017-06-01 23:25:02 +000044#include "llvm/IR/Instruction.h"
45#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000046#include "llvm/IR/Operator.h"
Eugene Zelenko7ea69232017-06-01 23:25:02 +000047#include "llvm/IR/Type.h"
48#include "llvm/IR/Value.h"
49#include "llvm/MC/LaneBitmask.h"
50#include "llvm/MC/MCRegisterInfo.h"
51#include "llvm/Support/Casting.h"
Andrew Trickda01ba32012-05-15 18:59:41 +000052#include "llvm/Support/CommandLine.h"
Eugene Zelenko7ea69232017-06-01 23:25:02 +000053#include "llvm/Support/Compiler.h"
Dan Gohman60cb69e2008-11-19 23:18:57 +000054#include "llvm/Support/Debug.h"
Eugene Zelenko7ea69232017-06-01 23:25:02 +000055#include "llvm/Support/ErrorHandling.h"
Andrew Trick90f711d2012-10-15 18:02:27 +000056#include "llvm/Support/Format.h"
Dan Gohman60cb69e2008-11-19 23:18:57 +000057#include "llvm/Support/raw_ostream.h"
Eugene Zelenko7ea69232017-06-01 23:25:02 +000058#include <algorithm>
59#include <cassert>
60#include <iterator>
61#include <string>
62#include <utility>
63#include <vector>
Andrew Trickc01b0042013-08-23 17:48:43 +000064
Dan Gohman60cb69e2008-11-19 23:18:57 +000065using namespace llvm;
66
Evandro Menezes0cd23f562017-07-11 22:08:28 +000067#define DEBUG_TYPE "machine-scheduler"
Chandler Carruth1b9dde02014-04-22 02:02:50 +000068
Andrew Trickda01ba32012-05-15 18:59:41 +000069static cl::opt<bool> EnableAASchedMI("enable-aa-sched-mi", cl::Hidden,
70 cl::ZeroOrMore, cl::init(false),
Jonas Paulssonbf408bb2015-01-07 13:20:57 +000071 cl::desc("Enable use of AA during MI DAG construction"));
Andrew Trickda01ba32012-05-15 18:59:41 +000072
Hal Finkeldbebb522014-01-25 19:24:54 +000073static cl::opt<bool> UseTBAA("use-tbaa-in-sched-mi", cl::Hidden,
Jonas Paulssonbf408bb2015-01-07 13:20:57 +000074 cl::init(true), cl::desc("Enable use of TBAA during MI DAG construction"));
Hal Finkeldbebb522014-01-25 19:24:54 +000075
Jonas Paulssonac29f012016-02-03 17:52:29 +000076// Note: the two options below might be used in tuning compile time vs
77// output quality. Setting HugeRegion so large that it will never be
78// reached means best-effort, but may be slow.
79
80// When Stores and Loads maps (or NonAliasStores and NonAliasLoads)
81// together hold this many SUs, a reduction of maps will be done.
82static cl::opt<unsigned> HugeRegion("dag-maps-huge-region", cl::Hidden,
83 cl::init(1000), cl::desc("The limit to use while constructing the DAG "
84 "prior to scheduling, at which point a trade-off "
85 "is made to avoid excessive compile time."));
86
Mehdi Amini59ae8542016-04-16 04:58:30 +000087static cl::opt<unsigned> ReductionSize(
88 "dag-maps-reduction-size", cl::Hidden,
Jonas Paulssonac29f012016-02-03 17:52:29 +000089 cl::desc("A huge scheduling region will have maps reduced by this many "
Mehdi Amini59ae8542016-04-16 04:58:30 +000090 "nodes at a time. Defaults to HugeRegion / 2."));
91
92static unsigned getReductionSize() {
93 // Always reduce a huge region with half of the elements, except
94 // when user sets this number explicitly.
95 if (ReductionSize.getNumOccurrences() == 0)
96 return HugeRegion / 2;
97 return ReductionSize;
98}
Jonas Paulssonac29f012016-02-03 17:52:29 +000099
100static void dumpSUList(ScheduleDAGInstrs::SUList &L) {
Aaron Ballman615eb472017-10-15 14:32:27 +0000101#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Jonas Paulssonac29f012016-02-03 17:52:29 +0000102 dbgs() << "{ ";
Matthias Braun298e0072016-09-30 23:08:07 +0000103 for (const SUnit *su : L) {
Jonas Paulssonac29f012016-02-03 17:52:29 +0000104 dbgs() << "SU(" << su->NodeNum << ")";
105 if (su != L.back())
106 dbgs() << ", ";
107 }
108 dbgs() << "}\n";
109#endif
110}
111
Dan Gohman619ef482009-01-15 19:20:50 +0000112ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf,
Alexey Samsonov8968e6d2014-08-20 19:36:05 +0000113 const MachineLoopInfo *mli,
Matthias Braun93563e72015-11-03 01:53:29 +0000114 bool RemoveKillFlags)
Matthias Braunb17e8b12015-12-04 19:54:24 +0000115 : ScheduleDAG(mf), MLI(mli), MFI(mf.getFrameInfo()),
Eugene Zelenko7ea69232017-06-01 23:25:02 +0000116 RemoveKillFlags(RemoveKillFlags),
Jonas Paulssonac29f012016-02-03 17:52:29 +0000117 UnknownValue(UndefValue::get(
Matthias Braunf1caa282017-12-15 22:22:58 +0000118 Type::getVoidTy(mf.getFunction().getContext()))) {
Devang Patele5feef02011-06-02 20:07:12 +0000119 DbgValues.clear();
Andrew Trick9b635132012-09-18 18:20:00 +0000120
Eric Christopher2c635492015-01-27 07:54:39 +0000121 const TargetSubtargetInfo &ST = mf.getSubtarget();
Sanjay Patel0d7df362018-04-08 19:56:04 +0000122 SchedModel.init(&ST);
Evan Chengf0236e02009-10-18 19:58:47 +0000123}
Dan Gohman60cb69e2008-11-19 23:18:57 +0000124
Hiroshi Inoueb49b0152017-10-12 06:26:04 +0000125/// If this machine instr has memory reference information and it can be
126/// tracked to a normal reference to a known object, return the Value
127/// for that object. This function returns false the memory location is
128/// unknown or may alias anything.
129static bool getUnderlyingObjectsForInstr(const MachineInstr *MI,
Matthias Braun941a7052016-07-28 18:40:00 +0000130 const MachineFrameInfo &MFI,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000131 UnderlyingObjectsVector &Objects,
132 const DataLayout &DL) {
Geoff Berry63817132016-04-14 21:31:07 +0000133 auto allMMOsOkay = [&]() {
134 for (const MachineMemOperand *MMO : MI->memoperands()) {
135 if (MMO->isVolatile())
136 return false;
Hal Finkel66859ae2012-12-10 18:49:16 +0000137
Geoff Berry63817132016-04-14 21:31:07 +0000138 if (const PseudoSourceValue *PSV = MMO->getPseudoValue()) {
139 // Function that contain tail calls don't have unique PseudoSourceValue
140 // objects. Two PseudoSourceValues might refer to the same or
141 // overlapping locations. The client code calling this function assumes
142 // this is not the case. So return a conservative answer of no known
143 // object.
Matthias Braun941a7052016-07-28 18:40:00 +0000144 if (MFI.hasTailCall())
Geoff Berry63817132016-04-14 21:31:07 +0000145 return false;
Geoff Berryc0739d82016-04-12 15:50:19 +0000146
Geoff Berry63817132016-04-14 21:31:07 +0000147 // For now, ignore PseudoSourceValues which may alias LLVM IR values
148 // because the code that uses this function has no way to cope with
149 // such aliases.
Matthias Braun941a7052016-07-28 18:40:00 +0000150 if (PSV->isAliased(&MFI))
Geoff Berry63817132016-04-14 21:31:07 +0000151 return false;
Geoff Berryc0739d82016-04-12 15:50:19 +0000152
Matthias Braun941a7052016-07-28 18:40:00 +0000153 bool MayAlias = PSV->mayAlias(&MFI);
Geoff Berry63817132016-04-14 21:31:07 +0000154 Objects.push_back(UnderlyingObjectsVector::value_type(PSV, MayAlias));
155 } else if (const Value *V = MMO->getValue()) {
156 SmallVector<Value *, 4> Objs;
Hiroshi Inoueb49b0152017-10-12 06:26:04 +0000157 if (!getUnderlyingObjectsForCodeGen(V, Objs, DL))
158 return false;
Geoff Berryc0739d82016-04-12 15:50:19 +0000159
Geoff Berry63817132016-04-14 21:31:07 +0000160 for (Value *V : Objs) {
Hiroshi Inoueb9417db2017-08-01 03:32:15 +0000161 assert(isIdentifiedObject(V));
Geoff Berry63817132016-04-14 21:31:07 +0000162 Objects.push_back(UnderlyingObjectsVector::value_type(V, true));
Geoff Berryc0739d82016-04-12 15:50:19 +0000163 }
Geoff Berry63817132016-04-14 21:31:07 +0000164 } else
165 return false;
Geoff Berryc0739d82016-04-12 15:50:19 +0000166 }
Geoff Berry63817132016-04-14 21:31:07 +0000167 return true;
168 };
169
Hiroshi Inoueb49b0152017-10-12 06:26:04 +0000170 if (!allMMOsOkay()) {
Geoff Berry63817132016-04-14 21:31:07 +0000171 Objects.clear();
Hiroshi Inoueb49b0152017-10-12 06:26:04 +0000172 return false;
173 }
174
175 return true;
Dan Gohman1ee0d412009-01-30 02:49:14 +0000176}
177
Andrew Trick7405c6d2012-04-20 20:05:21 +0000178void ScheduleDAGInstrs::startBlock(MachineBasicBlock *bb) {
179 BB = bb;
Dan Gohmanb9543432009-02-10 23:27:53 +0000180}
181
Andrew Trick52226d42012-03-07 23:00:49 +0000182void ScheduleDAGInstrs::finishBlock() {
Andrew Trick51ee9362012-04-20 20:24:33 +0000183 // Subclasses should no longer refer to the old block.
Craig Topperc0196b12014-04-14 00:51:57 +0000184 BB = nullptr;
Andrew Trick60cf03e2012-03-07 05:21:52 +0000185}
186
Andrew Trick60cf03e2012-03-07 05:21:52 +0000187void ScheduleDAGInstrs::enterRegion(MachineBasicBlock *bb,
188 MachineBasicBlock::iterator begin,
189 MachineBasicBlock::iterator end,
Andrew Tricka53e1012013-08-23 17:48:33 +0000190 unsigned regioninstrs) {
Andrew Trick7405c6d2012-04-20 20:05:21 +0000191 assert(bb == BB && "startBlock should set BB");
Andrew Trick8c207e42012-03-09 04:29:02 +0000192 RegionBegin = begin;
193 RegionEnd = end;
Andrew Tricka53e1012013-08-23 17:48:33 +0000194 NumRegionInstrs = regioninstrs;
Andrew Trick60cf03e2012-03-07 05:21:52 +0000195}
196
Andrew Trick60cf03e2012-03-07 05:21:52 +0000197void ScheduleDAGInstrs::exitRegion() {
198 // Nothing to do.
199}
200
Andrew Trick52226d42012-03-07 23:00:49 +0000201void ScheduleDAGInstrs::addSchedBarrierDeps() {
Craig Topperc0196b12014-04-14 00:51:57 +0000202 MachineInstr *ExitMI = RegionEnd != BB->end() ? &*RegionEnd : nullptr;
Evan Cheng15459b62010-10-23 02:10:46 +0000203 ExitSU.setInstr(ExitMI);
Matthias Braun325cd2c2016-11-11 01:34:21 +0000204 // Add dependencies on the defs and uses of the instruction.
205 if (ExitMI) {
Matthias Braun298e0072016-09-30 23:08:07 +0000206 for (const MachineOperand &MO : ExitMI->operands()) {
Evan Cheng15459b62010-10-23 02:10:46 +0000207 if (!MO.isReg() || MO.isDef()) continue;
208 unsigned Reg = MO.getReg();
Matthias Braun111603f2016-11-10 22:11:00 +0000209 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000210 Uses.insert(PhysRegSUOper(&ExitSU, -1, Reg));
Matthias Braun111603f2016-11-10 22:11:00 +0000211 } else if (TargetRegisterInfo::isVirtualRegister(Reg) && MO.readsReg()) {
Matthias Braun298e0072016-09-30 23:08:07 +0000212 addVRegUseDeps(&ExitSU, ExitMI->getOperandNo(&MO));
Matthias Braun111603f2016-11-10 22:11:00 +0000213 }
Evan Cheng15459b62010-10-23 02:10:46 +0000214 }
Matthias Braun325cd2c2016-11-11 01:34:21 +0000215 }
216 if (!ExitMI || (!ExitMI->isCall() && !ExitMI->isBarrier())) {
Evan Cheng15459b62010-10-23 02:10:46 +0000217 // For others, e.g. fallthrough, conditional branch, assume the exit
Evan Chengcbdf7e82010-10-27 23:17:17 +0000218 // uses all the registers that are livein to the successor blocks.
Matthias Braun298e0072016-09-30 23:08:07 +0000219 for (const MachineBasicBlock *Succ : BB->successors()) {
220 for (const auto &LI : Succ->liveins()) {
Matthias Braund9da1622015-09-09 18:08:03 +0000221 if (!Uses.contains(LI.PhysReg))
222 Uses.insert(PhysRegSUOper(&ExitSU, -1, LI.PhysReg));
Evan Chengcbdf7e82010-10-27 23:17:17 +0000223 }
Matthias Braun298e0072016-09-30 23:08:07 +0000224 }
Evan Cheng15459b62010-10-23 02:10:46 +0000225 }
226}
227
Matthias Braunbd7d9182017-01-27 18:53:00 +0000228/// MO is an operand of SU's instruction that defines a physical register. Adds
Andrew Trickd675a4c2012-02-23 01:52:38 +0000229/// data dependencies from SU to any uses of the physical register.
Andrew Trickae535612012-08-23 00:39:43 +0000230void ScheduleDAGInstrs::addPhysRegDataDeps(SUnit *SU, unsigned OperIdx) {
231 const MachineOperand &MO = SU->getInstr()->getOperand(OperIdx);
Andrew Trickd675a4c2012-02-23 01:52:38 +0000232 assert(MO.isDef() && "expect physreg def");
233
234 // Ask the target if address-backscheduling is desirable, and if so how much.
Eric Christopher2c635492015-01-27 07:54:39 +0000235 const TargetSubtargetInfo &ST = MF.getSubtarget();
Andrew Trickd675a4c2012-02-23 01:52:38 +0000236
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000237 for (MCRegAliasIterator Alias(MO.getReg(), TRI, true);
238 Alias.isValid(); ++Alias) {
Andrew Trick9dbbd3e2012-02-24 07:04:55 +0000239 if (!Uses.contains(*Alias))
Andrew Trickd675a4c2012-02-23 01:52:38 +0000240 continue;
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000241 for (Reg2SUnitsMap::iterator I = Uses.find(*Alias); I != Uses.end(); ++I) {
242 SUnit *UseSU = I->SU;
Andrew Trickd675a4c2012-02-23 01:52:38 +0000243 if (UseSU == SU)
244 continue;
Andrew Trick07dced62012-10-08 18:54:00 +0000245
Andrew Trick07dced62012-10-08 18:54:00 +0000246 // Adjust the dependence latency using operand def/use information,
247 // then allow the target to perform its own adjustments.
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000248 int UseOp = I->OpIdx;
Craig Topperc0196b12014-04-14 00:51:57 +0000249 MachineInstr *RegUse = nullptr;
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000250 SDep Dep;
251 if (UseOp < 0)
252 Dep = SDep(SU, SDep::Artificial);
253 else {
Andrew Tricke833e1c2013-04-13 06:07:40 +0000254 // Set the hasPhysRegDefs only for physreg defs that have a use within
255 // the scheduling region.
256 SU->hasPhysRegDefs = true;
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000257 Dep = SDep(SU, SDep::Data, *Alias);
258 RegUse = UseSU->getInstr();
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000259 }
260 Dep.setLatency(
Andrew Trickde2109e2013-06-15 04:49:57 +0000261 SchedModel.computeOperandLatency(SU->getInstr(), OperIdx, RegUse,
262 UseOp));
Andrew Trick45446062012-06-05 21:11:27 +0000263
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000264 ST.adjustSchedDependency(SU, UseSU, Dep);
265 UseSU->addPred(Dep);
Andrew Trickd675a4c2012-02-23 01:52:38 +0000266 }
267 }
268}
269
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000270/// Adds register dependencies (data, anti, and output) from this SUnit
Matthias Braunbd7d9182017-01-27 18:53:00 +0000271/// to following instructions in the same scheduling region that depend the
272/// physical register referenced at OperIdx.
Andrew Trickdbee9d82012-01-14 02:17:15 +0000273void ScheduleDAGInstrs::addPhysRegDeps(SUnit *SU, unsigned OperIdx) {
Andrew Trick6b104f82013-12-28 21:56:55 +0000274 MachineInstr *MI = SU->getInstr();
275 MachineOperand &MO = MI->getOperand(OperIdx);
Matthias Braun111603f2016-11-10 22:11:00 +0000276 unsigned Reg = MO.getReg();
Matthias Braunf29b12d2016-11-10 23:46:44 +0000277 // We do not need to track any dependencies for constant registers.
278 if (MRI.isConstantPhysReg(Reg))
279 return;
Andrew Trickdbee9d82012-01-14 02:17:15 +0000280
281 // Optionally add output and anti dependencies. For anti
282 // dependencies we use a latency of 0 because for a multi-issue
283 // target we want to allow the defining instruction to issue
284 // in the same cycle as the using instruction.
285 // TODO: Using a latency of 1 here for output dependencies assumes
286 // there's no cost for reusing registers.
287 SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
Matthias Braun111603f2016-11-10 22:11:00 +0000288 for (MCRegAliasIterator Alias(Reg, TRI, true); Alias.isValid(); ++Alias) {
Andrew Trick9dbbd3e2012-02-24 07:04:55 +0000289 if (!Defs.contains(*Alias))
Andrew Trickd675a4c2012-02-23 01:52:38 +0000290 continue;
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000291 for (Reg2SUnitsMap::iterator I = Defs.find(*Alias); I != Defs.end(); ++I) {
292 SUnit *DefSU = I->SU;
Andrew Trickdbee9d82012-01-14 02:17:15 +0000293 if (DefSU == &ExitSU)
294 continue;
295 if (DefSU != SU &&
296 (Kind != SDep::Output || !MO.isDead() ||
Hal Finkel66d77912014-12-05 02:07:35 +0000297 !DefSU->getInstr()->registerDefIsDead(*Alias))) {
Andrew Trickdbee9d82012-01-14 02:17:15 +0000298 if (Kind == SDep::Anti)
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000299 DefSU->addPred(SDep(SU, Kind, /*Reg=*/*Alias));
Andrew Trickdbee9d82012-01-14 02:17:15 +0000300 else {
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000301 SDep Dep(SU, Kind, /*Reg=*/*Alias);
Andrew Trickde2109e2013-06-15 04:49:57 +0000302 Dep.setLatency(
303 SchedModel.computeOutputLatency(MI, OperIdx, DefSU->getInstr()));
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000304 DefSU->addPred(Dep);
Andrew Trickdbee9d82012-01-14 02:17:15 +0000305 }
306 }
307 }
308 }
309
Andrew Trickd675a4c2012-02-23 01:52:38 +0000310 if (!MO.isDef()) {
Andrew Tricke833e1c2013-04-13 06:07:40 +0000311 SU->hasPhysRegUses = true;
Andrew Trickd675a4c2012-02-23 01:52:38 +0000312 // Either insert a new Reg2SUnits entry with an empty SUnits list, or
313 // retrieve the existing SUnits list for this register's uses.
314 // Push this SUnit on the use list.
Matthias Braun111603f2016-11-10 22:11:00 +0000315 Uses.insert(PhysRegSUOper(SU, OperIdx, Reg));
Andrew Trick6b104f82013-12-28 21:56:55 +0000316 if (RemoveKillFlags)
317 MO.setIsKill(false);
Matthias Braun111603f2016-11-10 22:11:00 +0000318 } else {
Andrew Trickae535612012-08-23 00:39:43 +0000319 addPhysRegDataDeps(SU, OperIdx);
Andrew Trickdbee9d82012-01-14 02:17:15 +0000320
Andrew Trickd675a4c2012-02-23 01:52:38 +0000321 // clear this register's use list
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000322 if (Uses.contains(Reg))
323 Uses.eraseAll(Reg);
Andrew Trickd675a4c2012-02-23 01:52:38 +0000324
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000325 if (!MO.isDead()) {
326 Defs.eraseAll(Reg);
327 } else if (SU->isCall) {
328 // Calls will not be reordered because of chain dependencies (see
329 // below). Since call operands are dead, calls may continue to be added
330 // to the DefList making dependence checking quadratic in the size of
331 // the block. Instead, we leave only one call at the back of the
332 // DefList.
333 Reg2SUnitsMap::RangePair P = Defs.equal_range(Reg);
334 Reg2SUnitsMap::iterator B = P.first;
335 Reg2SUnitsMap::iterator I = P.second;
336 for (bool isBegin = I == B; !isBegin; /* empty */) {
337 isBegin = (--I) == B;
338 if (!I->SU->isCall)
339 break;
340 I = Defs.erase(I);
341 }
Andrew Trickdbee9d82012-01-14 02:17:15 +0000342 }
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000343
Andrew Trickd675a4c2012-02-23 01:52:38 +0000344 // Defs are pushed in the order they are visited and never reordered.
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000345 Defs.insert(PhysRegSUOper(SU, OperIdx, Reg));
Andrew Trickdbee9d82012-01-14 02:17:15 +0000346 }
347}
348
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000349LaneBitmask ScheduleDAGInstrs::getLaneMaskForMO(const MachineOperand &MO) const
350{
351 unsigned Reg = MO.getReg();
352 // No point in tracking lanemasks if we don't have interesting subregisters.
353 const TargetRegisterClass &RC = *MRI.getRegClass(Reg);
354 if (!RC.HasDisjunctSubRegs)
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000355 return LaneBitmask::getAll();
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000356
357 unsigned SubReg = MO.getSubReg();
358 if (SubReg == 0)
359 return RC.getLaneMask();
360 return TRI->getSubRegIndexLaneMask(SubReg);
361}
362
Matthias Braunbd7d9182017-01-27 18:53:00 +0000363/// Adds register output and data dependencies from this SUnit to instructions
364/// that occur later in the same scheduling region if they read from or write to
365/// the virtual register defined at OperIdx.
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000366///
367/// TODO: Hoist loop induction variable increments. This has to be
368/// reevaluated. Generally, IV scheduling should be done before coalescing.
369void ScheduleDAGInstrs::addVRegDefDeps(SUnit *SU, unsigned OperIdx) {
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000370 MachineInstr *MI = SU->getInstr();
371 MachineOperand &MO = MI->getOperand(OperIdx);
372 unsigned Reg = MO.getReg();
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000373
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000374 LaneBitmask DefLaneMask;
375 LaneBitmask KillLaneMask;
376 if (TrackLaneMasks) {
377 bool IsKill = MO.getSubReg() == 0 || MO.isUndef();
378 DefLaneMask = getLaneMaskForMO(MO);
379 // If we have a <read-undef> flag, none of the lane values comes from an
380 // earlier instruction.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000381 KillLaneMask = IsKill ? LaneBitmask::getAll() : DefLaneMask;
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000382
383 // Clear undef flag, we'll re-add it later once we know which subregister
384 // Def is first.
385 MO.setIsUndef(false);
386 } else {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000387 DefLaneMask = LaneBitmask::getAll();
388 KillLaneMask = LaneBitmask::getAll();
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000389 }
390
391 if (MO.isDead()) {
392 assert(CurrentVRegUses.find(Reg) == CurrentVRegUses.end() &&
393 "Dead defs should have no uses");
394 } else {
395 // Add data dependence to all uses we found so far.
396 const TargetSubtargetInfo &ST = MF.getSubtarget();
397 for (VReg2SUnitOperIdxMultiMap::iterator I = CurrentVRegUses.find(Reg),
398 E = CurrentVRegUses.end(); I != E; /*empty*/) {
399 LaneBitmask LaneMask = I->LaneMask;
400 // Ignore uses of other lanes.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000401 if ((LaneMask & KillLaneMask).none()) {
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000402 ++I;
403 continue;
404 }
405
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000406 if ((LaneMask & DefLaneMask).any()) {
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000407 SUnit *UseSU = I->SU;
408 MachineInstr *Use = UseSU->getInstr();
409 SDep Dep(SU, SDep::Data, Reg);
410 Dep.setLatency(SchedModel.computeOperandLatency(MI, OperIdx, Use,
411 I->OperandIndex));
412 ST.adjustSchedDependency(SU, UseSU, Dep);
413 UseSU->addPred(Dep);
414 }
415
416 LaneMask &= ~KillLaneMask;
417 // If we found a Def for all lanes of this use, remove it from the list.
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000418 if (LaneMask.any()) {
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000419 I->LaneMask = LaneMask;
420 ++I;
421 } else
422 I = CurrentVRegUses.erase(I);
423 }
424 }
425
426 // Shortcut: Singly defined vregs do not have output/anti dependencies.
Andrew Trick79795892012-07-30 23:48:17 +0000427 if (MRI.hasOneDef(Reg))
Andrew Trick94053432012-07-28 01:48:15 +0000428 return;
Andrew Trickdb42c6f2012-02-22 06:08:13 +0000429
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000430 // Add output dependence to the next nearest defs of this vreg.
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000431 //
432 // Unless this definition is dead, the output dependence should be
433 // transitively redundant with antidependencies from this definition's
434 // uses. We're conservative for now until we have a way to guarantee the uses
435 // are not eliminated sometime during scheduling. The output dependence edge
436 // is also useful if output latency exceeds def-use latency.
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000437 LaneBitmask LaneMask = DefLaneMask;
438 for (VReg2SUnit &V2SU : make_range(CurrentVRegDefs.find(Reg),
439 CurrentVRegDefs.end())) {
440 // Ignore defs for other lanes.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000441 if ((V2SU.LaneMask & LaneMask).none())
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000442 continue;
443 // Add an output dependence.
444 SUnit *DefSU = V2SU.SU;
445 // Ignore additional defs of the same lanes in one instruction. This can
446 // happen because lanemasks are shared for targets with too many
447 // subregisters. We also use some representration tricks/hacks where we
448 // add super-register defs/uses, to imply that although we only access parts
449 // of the reg we care about the full one.
450 if (DefSU == SU)
451 continue;
452 SDep Dep(SU, SDep::Output, Reg);
453 Dep.setLatency(
454 SchedModel.computeOutputLatency(MI, OperIdx, DefSU->getInstr()));
455 DefSU->addPred(Dep);
456
457 // Update current definition. This can get tricky if the def was about a
458 // bigger lanemask before. We then have to shrink it and create a new
459 // VReg2SUnit for the non-overlapping part.
460 LaneBitmask OverlapMask = V2SU.LaneMask & LaneMask;
461 LaneBitmask NonOverlapMask = V2SU.LaneMask & ~LaneMask;
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000462 V2SU.SU = SU;
463 V2SU.LaneMask = OverlapMask;
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000464 if (NonOverlapMask.any())
Matthias Braun4c994ee2016-05-25 01:18:00 +0000465 CurrentVRegDefs.insert(VReg2SUnit(Reg, NonOverlapMask, DefSU));
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000466 }
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000467 // If there was no CurrentVRegDefs entry for some lanes yet, create one.
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000468 if (LaneMask.any())
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000469 CurrentVRegDefs.insert(VReg2SUnit(Reg, LaneMask, SU));
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000470}
471
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000472/// Adds a register data dependency if the instruction that defines the
Matthias Braunbd7d9182017-01-27 18:53:00 +0000473/// virtual register used at OperIdx is mapped to an SUnit. Add a register
474/// antidependency from this SUnit to instructions that occur later in the same
475/// scheduling region if they write the virtual register.
Andrew Trick46cc9a42012-02-22 06:08:11 +0000476///
477/// TODO: Handle ExitSU "uses" properly.
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000478void ScheduleDAGInstrs::addVRegUseDeps(SUnit *SU, unsigned OperIdx) {
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000479 const MachineInstr *MI = SU->getInstr();
480 const MachineOperand &MO = MI->getOperand(OperIdx);
481 unsigned Reg = MO.getReg();
Andrew Trick46cc9a42012-02-22 06:08:11 +0000482
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000483 // Remember the use. Data dependencies will be added when we find the def.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000484 LaneBitmask LaneMask = TrackLaneMasks ? getLaneMaskForMO(MO)
485 : LaneBitmask::getAll();
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000486 CurrentVRegUses.insert(VReg2SUnitOperIdx(Reg, LaneMask, OperIdx, SU));
487
488 // Add antidependences to the following defs of the vreg.
489 for (VReg2SUnit &V2SU : make_range(CurrentVRegDefs.find(Reg),
490 CurrentVRegDefs.end())) {
491 // Ignore defs for unrelated lanes.
492 LaneBitmask PrevDefLaneMask = V2SU.LaneMask;
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000493 if ((PrevDefLaneMask & LaneMask).none())
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000494 continue;
495 if (V2SU.SU == SU)
496 continue;
497
498 V2SU.SU->addPred(SDep(SU, SDep::Anti, Reg));
Andrew Trick2bc74c22013-08-30 04:36:57 +0000499 }
Andrew Trick46cc9a42012-02-22 06:08:11 +0000500}
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000501
Matthias Braunbd7d9182017-01-27 18:53:00 +0000502/// Returns true if MI is an instruction we are unable to reason about
Andrew Trickda01ba32012-05-15 18:59:41 +0000503/// (like a call or something with unmodeled side effects).
504static inline bool isGlobalMemoryObject(AliasAnalysis *AA, MachineInstr *MI) {
Rafael Espindola84921b92015-10-24 23:11:13 +0000505 return MI->isCall() || MI->hasUnmodeledSideEffects() ||
Justin Lebard98cf002016-09-10 01:03:20 +0000506 (MI->hasOrderedMemoryRef() && !MI->isDereferenceableInvariantLoad(AA));
Andrew Trickda01ba32012-05-15 18:59:41 +0000507}
508
Jonas Paulssonac29f012016-02-03 17:52:29 +0000509void ScheduleDAGInstrs::addChainDependency (SUnit *SUa, SUnit *SUb,
510 unsigned Latency) {
Eli Friedman93f47e52017-03-09 23:33:36 +0000511 if (SUa->getInstr()->mayAlias(AAForDep, *SUb->getInstr(), UseTBAA)) {
Jonas Paulssonac29f012016-02-03 17:52:29 +0000512 SDep Dep(SUa, SDep::MayAliasMem);
513 Dep.setLatency(Latency);
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000514 SUb->addPred(Dep);
515 }
Andrew Trickda01ba32012-05-15 18:59:41 +0000516}
517
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000518/// Creates an SUnit for each real instruction, numbered in top-down
Matthias Braunbd7d9182017-01-27 18:53:00 +0000519/// topological order. The instruction order A < B, implies that no edge exists
520/// from B to A.
Andrew Trick46cc9a42012-02-22 06:08:11 +0000521///
522/// Map each real instruction to its SUnit.
523///
Andrew Trick8823dec2012-03-14 04:00:41 +0000524/// After initSUnits, the SUnits vector cannot be resized and the scheduler may
525/// hang onto SUnit pointers. We may relax this in the future by using SUnit IDs
526/// instead of pointers.
527///
528/// MachineScheduler relies on initSUnits numbering the nodes by their order in
529/// the original instruction list.
Andrew Trick46cc9a42012-02-22 06:08:11 +0000530void ScheduleDAGInstrs::initSUnits() {
531 // We'll be allocating one SUnit for each real instruction in the region,
532 // which is contained within a basic block.
Andrew Tricka53e1012013-08-23 17:48:33 +0000533 SUnits.reserve(NumRegionInstrs);
Andrew Trick46cc9a42012-02-22 06:08:11 +0000534
Eugene Zelenko7ea69232017-06-01 23:25:02 +0000535 for (MachineInstr &MI : make_range(RegionBegin, RegionEnd)) {
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000536 if (MI.isDebugValue())
Andrew Trick46cc9a42012-02-22 06:08:11 +0000537 continue;
538
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000539 SUnit *SU = newSUnit(&MI);
540 MISUnitMap[&MI] = SU;
Andrew Trick46cc9a42012-02-22 06:08:11 +0000541
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000542 SU->isCall = MI.isCall();
543 SU->isCommutable = MI.isCommutable();
Andrew Trick46cc9a42012-02-22 06:08:11 +0000544
545 // Assign the Latency field of SU using target-provided information.
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000546 SU->Latency = SchedModel.computeInstrLatency(SU->getInstr());
Andrew Trick880e5732013-12-05 17:55:58 +0000547
Andrew Trick1766f932014-04-18 17:35:08 +0000548 // If this SUnit uses a reserved or unbuffered resource, mark it as such.
549 //
Alp Tokerbeaca192014-05-15 01:52:21 +0000550 // Reserved resources block an instruction from issuing and stall the
Andrew Trick1766f932014-04-18 17:35:08 +0000551 // entire pipeline. These are identified by BufferSize=0.
552 //
Alp Tokerbeaca192014-05-15 01:52:21 +0000553 // Unbuffered resources prevent execution of subsequent instructions that
Andrew Trick1766f932014-04-18 17:35:08 +0000554 // require the same resources. This is used for in-order execution pipelines
555 // within an out-of-order core. These are identified by BufferSize=1.
Andrew Trick880e5732013-12-05 17:55:58 +0000556 if (SchedModel.hasInstrSchedModel()) {
557 const MCSchedClassDesc *SC = getSchedClass(SU);
Matthias Braun298e0072016-09-30 23:08:07 +0000558 for (const MCWriteProcResEntry &PRE :
559 make_range(SchedModel.getWriteProcResBegin(SC),
560 SchedModel.getWriteProcResEnd(SC))) {
561 switch (SchedModel.getProcResource(PRE.ProcResourceIdx)->BufferSize) {
Andrew Trick5a22df42013-12-05 17:56:02 +0000562 case 0:
563 SU->hasReservedResource = true;
564 break;
565 case 1:
Andrew Trick880e5732013-12-05 17:55:58 +0000566 SU->isUnbuffered = true;
567 break;
Andrew Trick5a22df42013-12-05 17:56:02 +0000568 default:
569 break;
Andrew Trick880e5732013-12-05 17:55:58 +0000570 }
571 }
572 }
Andrew Trick46cc9a42012-02-22 06:08:11 +0000573 }
Andrew Trickdbee9d82012-01-14 02:17:15 +0000574}
575
Jonas Paulssonac29f012016-02-03 17:52:29 +0000576class ScheduleDAGInstrs::Value2SUsMap : public MapVector<ValueType, SUList> {
Jonas Paulssonac29f012016-02-03 17:52:29 +0000577 /// Current total number of SUs in map.
Eugene Zelenko7ea69232017-06-01 23:25:02 +0000578 unsigned NumNodes = 0;
Jonas Paulssonac29f012016-02-03 17:52:29 +0000579
580 /// 1 for loads, 0 for stores. (see comment in SUList)
581 unsigned TrueMemOrderLatency;
Jonas Paulssonac29f012016-02-03 17:52:29 +0000582
Matthias Braunbd7d9182017-01-27 18:53:00 +0000583public:
Eugene Zelenko7ea69232017-06-01 23:25:02 +0000584 Value2SUsMap(unsigned lat = 0) : TrueMemOrderLatency(lat) {}
Jonas Paulssonac29f012016-02-03 17:52:29 +0000585
586 /// To keep NumNodes up to date, insert() is used instead of
587 /// this operator w/ push_back().
588 ValueType &operator[](const SUList &Key) {
589 llvm_unreachable("Don't use. Use insert() instead."); };
590
Matthias Braunbd7d9182017-01-27 18:53:00 +0000591 /// Adds SU to the SUList of V. If Map grows huge, reduce its size by calling
592 /// reduce().
Jonas Paulssonac29f012016-02-03 17:52:29 +0000593 void inline insert(SUnit *SU, ValueType V) {
594 MapVector::operator[](V).push_back(SU);
595 NumNodes++;
596 }
597
598 /// Clears the list of SUs mapped to V.
599 void inline clearList(ValueType V) {
600 iterator Itr = find(V);
601 if (Itr != end()) {
Eugene Zelenko7ea69232017-06-01 23:25:02 +0000602 assert(NumNodes >= Itr->second.size());
Jonas Paulssonac29f012016-02-03 17:52:29 +0000603 NumNodes -= Itr->second.size();
604
605 Itr->second.clear();
606 }
607 }
608
609 /// Clears map from all contents.
610 void clear() {
611 MapVector<ValueType, SUList>::clear();
612 NumNodes = 0;
613 }
614
615 unsigned inline size() const { return NumNodes; }
616
Matthias Braunbd7d9182017-01-27 18:53:00 +0000617 /// Counts the number of SUs in this map after a reduction.
Eugene Zelenko7ea69232017-06-01 23:25:02 +0000618 void reComputeSize() {
Jonas Paulssonac29f012016-02-03 17:52:29 +0000619 NumNodes = 0;
620 for (auto &I : *this)
621 NumNodes += I.second.size();
622 }
623
624 unsigned inline getTrueMemOrderLatency() const {
625 return TrueMemOrderLatency;
626 }
627
628 void dump();
629};
630
631void ScheduleDAGInstrs::addChainDependencies(SUnit *SU,
632 Value2SUsMap &Val2SUsMap) {
633 for (auto &I : Val2SUsMap)
634 addChainDependencies(SU, I.second,
635 Val2SUsMap.getTrueMemOrderLatency());
636}
637
638void ScheduleDAGInstrs::addChainDependencies(SUnit *SU,
639 Value2SUsMap &Val2SUsMap,
640 ValueType V) {
641 Value2SUsMap::iterator Itr = Val2SUsMap.find(V);
642 if (Itr != Val2SUsMap.end())
643 addChainDependencies(SU, Itr->second,
644 Val2SUsMap.getTrueMemOrderLatency());
645}
646
647void ScheduleDAGInstrs::addBarrierChain(Value2SUsMap &map) {
Eugene Zelenko7ea69232017-06-01 23:25:02 +0000648 assert(BarrierChain != nullptr);
Jonas Paulssonac29f012016-02-03 17:52:29 +0000649
650 for (auto &I : map) {
651 SUList &sus = I.second;
652 for (auto *SU : sus)
653 SU->addPredBarrier(BarrierChain);
654 }
655 map.clear();
656}
657
658void ScheduleDAGInstrs::insertBarrierChain(Value2SUsMap &map) {
Eugene Zelenko7ea69232017-06-01 23:25:02 +0000659 assert(BarrierChain != nullptr);
Jonas Paulssonac29f012016-02-03 17:52:29 +0000660
661 // Go through all lists of SUs.
662 for (Value2SUsMap::iterator I = map.begin(), EE = map.end(); I != EE;) {
663 Value2SUsMap::iterator CurrItr = I++;
664 SUList &sus = CurrItr->second;
665 SUList::iterator SUItr = sus.begin(), SUEE = sus.end();
666 for (; SUItr != SUEE; ++SUItr) {
667 // Stop on BarrierChain or any instruction above it.
668 if ((*SUItr)->NodeNum <= BarrierChain->NodeNum)
669 break;
670
671 (*SUItr)->addPredBarrier(BarrierChain);
672 }
673
674 // Remove also the BarrierChain from list if present.
NAKAMURA Takumibc46f622016-05-02 17:29:55 +0000675 if (SUItr != SUEE && *SUItr == BarrierChain)
Jonas Paulssonac29f012016-02-03 17:52:29 +0000676 SUItr++;
677
678 // Remove all SUs that are now successors of BarrierChain.
679 if (SUItr != sus.begin())
680 sus.erase(sus.begin(), SUItr);
681 }
682
683 // Remove all entries with empty su lists.
684 map.remove_if([&](std::pair<ValueType, SUList> &mapEntry) {
685 return (mapEntry.second.empty()); });
686
687 // Recompute the size of the map (NumNodes).
688 map.reComputeSize();
689}
690
Andrew Trick88639922012-04-24 17:56:43 +0000691void ScheduleDAGInstrs::buildSchedGraph(AliasAnalysis *AA,
Andrew Trick1a831342013-08-30 03:49:48 +0000692 RegPressureTracker *RPTracker,
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000693 PressureDiffs *PDiffs,
Matthias Braund4f64092016-01-20 00:23:32 +0000694 LiveIntervals *LIS,
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000695 bool TrackLaneMasks) {
Eric Christopher2c635492015-01-27 07:54:39 +0000696 const TargetSubtargetInfo &ST = MF.getSubtarget();
Hal Finkelb350ffd2013-08-29 03:25:05 +0000697 bool UseAA = EnableAASchedMI.getNumOccurrences() > 0 ? EnableAASchedMI
698 : ST.useAA();
Jonas Paulssonac29f012016-02-03 17:52:29 +0000699 AAForDep = UseAA ? AA : nullptr;
700
701 BarrierChain = nullptr;
Hal Finkelb350ffd2013-08-29 03:25:05 +0000702
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000703 this->TrackLaneMasks = TrackLaneMasks;
Andrew Trick310190e2013-09-04 21:00:02 +0000704 MISUnitMap.clear();
705 ScheduleDAG::clearDAG();
706
Andrew Trick46cc9a42012-02-22 06:08:11 +0000707 // Create an SUnit for each real instruction.
708 initSUnits();
Dan Gohman60cb69e2008-11-19 23:18:57 +0000709
Andrew Trick1a831342013-08-30 03:49:48 +0000710 if (PDiffs)
711 PDiffs->init(SUnits.size());
712
Jonas Paulssonac29f012016-02-03 17:52:29 +0000713 // We build scheduling units by walking a block's instruction list
714 // from bottom to top.
Dan Gohman3aab10b2008-12-04 01:35:46 +0000715
Jonas Paulssonac29f012016-02-03 17:52:29 +0000716 // Each MIs' memory operand(s) is analyzed to a list of underlying
Jonas Paulsson22936852016-02-04 13:08:48 +0000717 // objects. The SU is then inserted in the SUList(s) mapped from the
718 // Value(s). Each Value thus gets mapped to lists of SUs depending
719 // on it, stores and loads kept separately. Two SUs are trivially
720 // non-aliasing if they both depend on only identified Values and do
721 // not share any common Value.
Jonas Paulssonac29f012016-02-03 17:52:29 +0000722 Value2SUsMap Stores, Loads(1 /*TrueMemOrderLatency*/);
Dan Gohman3aab10b2008-12-04 01:35:46 +0000723
Jonas Paulssonac29f012016-02-03 17:52:29 +0000724 // Certain memory accesses are known to not alias any SU in Stores
725 // or Loads, and have therefore their own 'NonAlias'
726 // domain. E.g. spill / reload instructions never alias LLVM I/R
Jonas Paulsson22936852016-02-04 13:08:48 +0000727 // Values. It would be nice to assume that this type of memory
728 // accesses always have a proper memory operand modelling, and are
729 // therefore never unanalyzable, but this is conservatively not
730 // done.
Jonas Paulssonac29f012016-02-03 17:52:29 +0000731 Value2SUsMap NonAliasStores, NonAliasLoads(1 /*TrueMemOrderLatency*/);
732
Dale Johannesen49de0602010-03-10 22:13:47 +0000733 // Remove any stale debug info; sometimes BuildSchedGraph is called again
734 // without emitting the info from the previous call.
Devang Patele5feef02011-06-02 20:07:12 +0000735 DbgValues.clear();
Craig Topperc0196b12014-04-14 00:51:57 +0000736 FirstDbgValue = nullptr;
Dale Johannesen49de0602010-03-10 22:13:47 +0000737
Andrew Trickd675a4c2012-02-23 01:52:38 +0000738 assert(Defs.empty() && Uses.empty() &&
739 "Only BuildGraph should update Defs/Uses");
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000740 Defs.setUniverse(TRI->getNumRegs());
741 Uses.setUniverse(TRI->getNumRegs());
Andrew Trick2e116a42011-05-06 21:52:52 +0000742
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000743 assert(CurrentVRegDefs.empty() && "nobody else should use CurrentVRegDefs");
744 assert(CurrentVRegUses.empty() && "nobody else should use CurrentVRegUses");
745 unsigned NumVirtRegs = MRI.getNumVirtRegs();
746 CurrentVRegDefs.setUniverse(NumVirtRegs);
747 CurrentVRegUses.setUniverse(NumVirtRegs);
748
Andrew Trickd675a4c2012-02-23 01:52:38 +0000749 // Model data dependencies between instructions being scheduled and the
750 // ExitSU.
Andrew Trick52226d42012-03-07 23:00:49 +0000751 addSchedBarrierDeps();
Andrew Trickd675a4c2012-02-23 01:52:38 +0000752
Dan Gohmanb9543432009-02-10 23:27:53 +0000753 // Walk the list of instructions, from bottom moving up.
Craig Topperc0196b12014-04-14 00:51:57 +0000754 MachineInstr *DbgMI = nullptr;
Andrew Trick8c207e42012-03-09 04:29:02 +0000755 for (MachineBasicBlock::iterator MII = RegionEnd, MIE = RegionBegin;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000756 MII != MIE; --MII) {
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000757 MachineInstr &MI = *std::prev(MII);
758 if (DbgMI) {
759 DbgValues.push_back(std::make_pair(DbgMI, &MI));
Craig Topperc0196b12014-04-14 00:51:57 +0000760 DbgMI = nullptr;
Devang Patele5feef02011-06-02 20:07:12 +0000761 }
762
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000763 if (MI.isDebugValue()) {
764 DbgMI = &MI;
Dale Johannesen49de0602010-03-10 22:13:47 +0000765 continue;
766 }
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000767 SUnit *SU = MISUnitMap[&MI];
Andrew Trick1a831342013-08-30 03:49:48 +0000768 assert(SU && "No SUnit mapped to this MI");
769
Andrew Trick88639922012-04-24 17:56:43 +0000770 if (RPTracker) {
Matthias Braunb505c762016-01-12 22:57:35 +0000771 RegisterOperands RegOpers;
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000772 RegOpers.collect(MI, *TRI, MRI, TrackLaneMasks, false);
Matthias Braund4f64092016-01-20 00:23:32 +0000773 if (TrackLaneMasks) {
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000774 SlotIndex SlotIdx = LIS->getInstructionIndex(MI);
Matthias Braund4f64092016-01-20 00:23:32 +0000775 RegOpers.adjustLaneLiveness(*LIS, MRI, SlotIdx);
776 }
Matthias Braunb505c762016-01-12 22:57:35 +0000777 if (PDiffs != nullptr)
778 PDiffs->addInstruction(SU->NodeNum, RegOpers, MRI);
779
Yaxun Liuc41e2f62017-12-15 03:56:57 +0000780 if (RPTracker->getPos() == RegionEnd || &*RPTracker->getPos() != &MI)
781 RPTracker->recedeSkipDebugValues();
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000782 assert(&*RPTracker->getPos() == &MI && "RPTracker in sync");
Matthias Braunb505c762016-01-12 22:57:35 +0000783 RPTracker->recede(RegOpers);
Andrew Trick88639922012-04-24 17:56:43 +0000784 }
Devang Patele5feef02011-06-02 20:07:12 +0000785
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000786 assert(
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000787 (CanHandleTerminators || (!MI.isTerminator() && !MI.isPosition())) &&
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000788 "Cannot schedule terminators or labels!");
Dan Gohman60cb69e2008-11-19 23:18:57 +0000789
Dan Gohman3aab10b2008-12-04 01:35:46 +0000790 // Add register-based dependencies (data, anti, and output).
Krzysztof Parzyszeka356bb72016-05-10 16:50:30 +0000791 // For some instructions (calls, returns, inline-asm, etc.) there can
792 // be explicit uses and implicit defs, in which case the use will appear
793 // on the operand list before the def. Do two passes over the operand
794 // list to make sure that defs are processed before any uses.
Andrew Trickec256482012-12-18 20:53:01 +0000795 bool HasVRegDef = false;
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000796 for (unsigned j = 0, n = MI.getNumOperands(); j != n; ++j) {
797 const MachineOperand &MO = MI.getOperand(j);
Krzysztof Parzyszeka356bb72016-05-10 16:50:30 +0000798 if (!MO.isReg() || !MO.isDef())
799 continue;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000800 unsigned Reg = MO.getReg();
Matthias Braun111603f2016-11-10 22:11:00 +0000801 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Andrew Trickdbee9d82012-01-14 02:17:15 +0000802 addPhysRegDeps(SU, j);
Matthias Braun111603f2016-11-10 22:11:00 +0000803 } else if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Krzysztof Parzyszeka356bb72016-05-10 16:50:30 +0000804 HasVRegDef = true;
805 addVRegDefDeps(SU, j);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000806 }
807 }
Krzysztof Parzyszeka356bb72016-05-10 16:50:30 +0000808 // Now process all uses.
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000809 for (unsigned j = 0, n = MI.getNumOperands(); j != n; ++j) {
810 const MachineOperand &MO = MI.getOperand(j);
Matthias Braun8a5b4672016-05-10 20:11:58 +0000811 // Only look at use operands.
812 // We do not need to check for MO.readsReg() here because subsequent
813 // subregister defs will get output dependence edges and need no
814 // additional use dependencies.
Krzysztof Parzyszeka356bb72016-05-10 16:50:30 +0000815 if (!MO.isReg() || !MO.isUse())
816 continue;
817 unsigned Reg = MO.getReg();
Matthias Braun111603f2016-11-10 22:11:00 +0000818 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Krzysztof Parzyszeka356bb72016-05-10 16:50:30 +0000819 addPhysRegDeps(SU, j);
Matthias Braun111603f2016-11-10 22:11:00 +0000820 } else if (TargetRegisterInfo::isVirtualRegister(Reg) && MO.readsReg()) {
Krzysztof Parzyszeka356bb72016-05-10 16:50:30 +0000821 addVRegUseDeps(SU, j);
Matthias Braun111603f2016-11-10 22:11:00 +0000822 }
Krzysztof Parzyszeka356bb72016-05-10 16:50:30 +0000823 }
824
Andrew Trickec256482012-12-18 20:53:01 +0000825 // If we haven't seen any uses in this scheduling region, create a
826 // dependence edge to ExitSU to model the live-out latency. This is required
827 // for vreg defs with no in-region use, and prefetches with no vreg def.
828 //
829 // FIXME: NumDataSuccs would be more precise than NumSuccs here. This
830 // check currently relies on being called before adding chain deps.
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000831 if (SU->NumSuccs == 0 && SU->Latency > 1 && (HasVRegDef || MI.mayLoad())) {
Andrew Trickec256482012-12-18 20:53:01 +0000832 SDep Dep(SU, SDep::Artificial);
833 Dep.setLatency(SU->Latency - 1);
834 ExitSU.addPred(Dep);
835 }
Dan Gohman3aab10b2008-12-04 01:35:46 +0000836
Jonas Paulssonac29f012016-02-03 17:52:29 +0000837 // Add memory dependencies (Note: isStoreToStackSlot and
838 // isLoadFromStackSLot are not usable after stack slots are lowered to
839 // actual addresses).
840
841 // This is a barrier event that acts as a pivotal node in the DAG.
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000842 if (isGlobalMemoryObject(AA, &MI)) {
Jonas Paulssonac29f012016-02-03 17:52:29 +0000843
844 // Become the barrier chain.
David Goodwind2f9c042009-11-09 19:22:17 +0000845 if (BarrierChain)
Jonas Paulssonac29f012016-02-03 17:52:29 +0000846 BarrierChain->addPredBarrier(SU);
David Goodwind2f9c042009-11-09 19:22:17 +0000847 BarrierChain = SU;
848
Jonas Paulssonac29f012016-02-03 17:52:29 +0000849 DEBUG(dbgs() << "Global memory object and new barrier chain: SU("
850 << BarrierChain->NodeNum << ").\n";);
Tom Stellard3e01d472014-12-08 23:36:48 +0000851
Jonas Paulssonac29f012016-02-03 17:52:29 +0000852 // Add dependencies against everything below it and clear maps.
853 addBarrierChain(Stores);
854 addBarrierChain(Loads);
855 addBarrierChain(NonAliasStores);
856 addBarrierChain(NonAliasLoads);
Hal Finkel66859ae2012-12-10 18:49:16 +0000857
Jonas Paulssonac29f012016-02-03 17:52:29 +0000858 continue;
859 }
860
861 // If it's not a store or a variant load, we're done.
Justin Lebard98cf002016-09-10 01:03:20 +0000862 if (!MI.mayStore() &&
863 !(MI.mayLoad() && !MI.isDereferenceableInvariantLoad(AA)))
Jonas Paulssonac29f012016-02-03 17:52:29 +0000864 continue;
865
866 // Always add dependecy edge to BarrierChain if present.
867 if (BarrierChain)
868 BarrierChain->addPredBarrier(SU);
869
870 // Find the underlying objects for MI. The Objs vector is either
871 // empty, or filled with the Values of memory locations which this
Hiroshi Inoueb49b0152017-10-12 06:26:04 +0000872 // SU depends on.
Jonas Paulssonac29f012016-02-03 17:52:29 +0000873 UnderlyingObjectsVector Objs;
Hiroshi Inoueb49b0152017-10-12 06:26:04 +0000874 bool ObjsFound = getUnderlyingObjectsForInstr(&MI, MFI, Objs,
875 MF.getDataLayout());
Jonas Paulssonac29f012016-02-03 17:52:29 +0000876
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000877 if (MI.mayStore()) {
Hiroshi Inoueb49b0152017-10-12 06:26:04 +0000878 if (!ObjsFound) {
Jonas Paulssonac29f012016-02-03 17:52:29 +0000879 // An unknown store depends on all stores and loads.
880 addChainDependencies(SU, Stores);
881 addChainDependencies(SU, NonAliasStores);
882 addChainDependencies(SU, Loads);
883 addChainDependencies(SU, NonAliasLoads);
884
885 // Map this store to 'UnknownValue'.
886 Stores.insert(SU, UnknownValue);
Chandler Carruthb4728562016-03-31 21:55:58 +0000887 } else {
888 // Add precise dependencies against all previously seen memory
889 // accesses mapped to the same Value(s).
Geoff Berry63817132016-04-14 21:31:07 +0000890 for (const UnderlyingObject &UnderlObj : Objs) {
891 ValueType V = UnderlObj.getValue();
892 bool ThisMayAlias = UnderlObj.mayAlias();
Chandler Carruthb4728562016-03-31 21:55:58 +0000893
894 // Add dependencies to previous stores and loads mapped to V.
Geoff Berry63817132016-04-14 21:31:07 +0000895 addChainDependencies(SU, (ThisMayAlias ? Stores : NonAliasStores), V);
Chandler Carruthb4728562016-03-31 21:55:58 +0000896 addChainDependencies(SU, (ThisMayAlias ? Loads : NonAliasLoads), V);
Geoff Berryc0739d82016-04-12 15:50:19 +0000897 }
898 // Update the store map after all chains have been added to avoid adding
899 // self-loop edge if multiple underlying objects are present.
Geoff Berry63817132016-04-14 21:31:07 +0000900 for (const UnderlyingObject &UnderlObj : Objs) {
901 ValueType V = UnderlObj.getValue();
902 bool ThisMayAlias = UnderlObj.mayAlias();
Chandler Carruthb4728562016-03-31 21:55:58 +0000903
904 // Map this store to V.
Geoff Berry63817132016-04-14 21:31:07 +0000905 (ThisMayAlias ? Stores : NonAliasStores).insert(SU, V);
Chandler Carruthb4728562016-03-31 21:55:58 +0000906 }
907 // The store may have dependencies to unanalyzable loads and
908 // stores.
909 addChainDependencies(SU, Loads, UnknownValue);
910 addChainDependencies(SU, Stores, UnknownValue);
Hal Finkel66859ae2012-12-10 18:49:16 +0000911 }
Chandler Carruthb4728562016-03-31 21:55:58 +0000912 } else { // SU is a load.
Hiroshi Inoueb49b0152017-10-12 06:26:04 +0000913 if (!ObjsFound) {
Jonas Paulssonac29f012016-02-03 17:52:29 +0000914 // An unknown load depends on all stores.
915 addChainDependencies(SU, Stores);
916 addChainDependencies(SU, NonAliasStores);
917
918 Loads.insert(SU, UnknownValue);
Chandler Carruthb4728562016-03-31 21:55:58 +0000919 } else {
Geoff Berry63817132016-04-14 21:31:07 +0000920 for (const UnderlyingObject &UnderlObj : Objs) {
921 ValueType V = UnderlObj.getValue();
922 bool ThisMayAlias = UnderlObj.mayAlias();
Chandler Carruthb4728562016-03-31 21:55:58 +0000923
924 // Add precise dependencies against all previously seen stores
925 // mapping to the same Value(s).
926 addChainDependencies(SU, (ThisMayAlias ? Stores : NonAliasStores), V);
927
928 // Map this load to V.
929 (ThisMayAlias ? Loads : NonAliasLoads).insert(SU, V);
930 }
931 // The load may have dependencies to unanalyzable stores.
932 addChainDependencies(SU, Stores, UnknownValue);
Hal Finkel66859ae2012-12-10 18:49:16 +0000933 }
Jonas Paulssonac29f012016-02-03 17:52:29 +0000934 }
935
936 // Reduce maps if they grow huge.
937 if (Stores.size() + Loads.size() >= HugeRegion) {
938 DEBUG(dbgs() << "Reducing Stores and Loads maps.\n";);
Mehdi Amini59ae8542016-04-16 04:58:30 +0000939 reduceHugeMemNodeMaps(Stores, Loads, getReductionSize());
Jonas Paulssonac29f012016-02-03 17:52:29 +0000940 }
941 if (NonAliasStores.size() + NonAliasLoads.size() >= HugeRegion) {
942 DEBUG(dbgs() << "Reducing NonAliasStores and NonAliasLoads maps.\n";);
Mehdi Amini59ae8542016-04-16 04:58:30 +0000943 reduceHugeMemNodeMaps(NonAliasStores, NonAliasLoads, getReductionSize());
Dan Gohman60cb69e2008-11-19 23:18:57 +0000944 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000945 }
Jonas Paulssonac29f012016-02-03 17:52:29 +0000946
Andrew Trickb767d1e2012-12-01 01:22:49 +0000947 if (DbgMI)
948 FirstDbgValue = DbgMI;
Dan Gohman619ef482009-01-15 19:20:50 +0000949
Andrew Trickd675a4c2012-02-23 01:52:38 +0000950 Defs.clear();
951 Uses.clear();
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000952 CurrentVRegDefs.clear();
953 CurrentVRegUses.clear();
Jonas Paulssonac29f012016-02-03 17:52:29 +0000954}
955
956raw_ostream &llvm::operator<<(raw_ostream &OS, const PseudoSourceValue* PSV) {
957 PSV->printCustom(OS);
958 return OS;
959}
960
961void ScheduleDAGInstrs::Value2SUsMap::dump() {
962 for (auto &Itr : *this) {
963 if (Itr.first.is<const Value*>()) {
964 const Value *V = Itr.first.get<const Value*>();
965 if (isa<UndefValue>(V))
966 dbgs() << "Unknown";
967 else
968 V->printAsOperand(dbgs());
969 }
970 else if (Itr.first.is<const PseudoSourceValue*>())
971 dbgs() << Itr.first.get<const PseudoSourceValue*>();
972 else
973 llvm_unreachable("Unknown Value type.");
974
975 dbgs() << " : ";
976 dumpSUList(Itr.second);
977 }
978}
979
Jonas Paulssonac29f012016-02-03 17:52:29 +0000980void ScheduleDAGInstrs::reduceHugeMemNodeMaps(Value2SUsMap &stores,
981 Value2SUsMap &loads, unsigned N) {
982 DEBUG(dbgs() << "Before reduction:\nStoring SUnits:\n";
983 stores.dump();
984 dbgs() << "Loading SUnits:\n";
985 loads.dump());
986
987 // Insert all SU's NodeNums into a vector and sort it.
988 std::vector<unsigned> NodeNums;
989 NodeNums.reserve(stores.size() + loads.size());
990 for (auto &I : stores)
991 for (auto *SU : I.second)
992 NodeNums.push_back(SU->NodeNum);
993 for (auto &I : loads)
994 for (auto *SU : I.second)
995 NodeNums.push_back(SU->NodeNum);
Mandeep Singh Grange92f0cf2018-04-06 18:08:42 +0000996 llvm::sort(NodeNums.begin(), NodeNums.end());
Jonas Paulssonac29f012016-02-03 17:52:29 +0000997
998 // The N last elements in NodeNums will be removed, and the SU with
999 // the lowest NodeNum of them will become the new BarrierChain to
1000 // let the not yet seen SUs have a dependency to the removed SUs.
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001001 assert(N <= NodeNums.size());
Jonas Paulssonac29f012016-02-03 17:52:29 +00001002 SUnit *newBarrierChain = &SUnits[*(NodeNums.end() - N)];
1003 if (BarrierChain) {
1004 // The aliasing and non-aliasing maps reduce independently of each
1005 // other, but share a common BarrierChain. Check if the
1006 // newBarrierChain is above the former one. If it is not, it may
1007 // introduce a loop to use newBarrierChain, so keep the old one.
1008 if (newBarrierChain->NodeNum < BarrierChain->NodeNum) {
1009 BarrierChain->addPredBarrier(newBarrierChain);
1010 BarrierChain = newBarrierChain;
1011 DEBUG(dbgs() << "Inserting new barrier chain: SU("
1012 << BarrierChain->NodeNum << ").\n";);
1013 }
1014 else
1015 DEBUG(dbgs() << "Keeping old barrier chain: SU("
1016 << BarrierChain->NodeNum << ").\n";);
1017 }
1018 else
1019 BarrierChain = newBarrierChain;
1020
1021 insertBarrierChain(stores);
1022 insertBarrierChain(loads);
1023
1024 DEBUG(dbgs() << "After reduction:\nStoring SUnits:\n";
1025 stores.dump();
1026 dbgs() << "Loading SUnits:\n";
1027 loads.dump());
Dan Gohman60cb69e2008-11-19 23:18:57 +00001028}
1029
Matthias Braun868bbd42017-05-27 02:50:50 +00001030static void toggleKills(const MachineRegisterInfo &MRI, LivePhysRegs &LiveRegs,
1031 MachineInstr &MI, bool addToLiveRegs) {
1032 for (MachineOperand &MO : MI.operands()) {
1033 if (!MO.isReg() || !MO.readsReg())
1034 continue;
1035 unsigned Reg = MO.getReg();
1036 if (!Reg)
1037 continue;
Andrew Trick6b104f82013-12-28 21:56:55 +00001038
Matthias Braun868bbd42017-05-27 02:50:50 +00001039 // Things that are available after the instruction are killed by it.
1040 bool IsKill = LiveRegs.available(MRI, Reg);
1041 MO.setIsKill(IsKill);
Matthias Braune2ae0012017-06-27 00:58:48 +00001042 if (addToLiveRegs)
Matthias Braun868bbd42017-05-27 02:50:50 +00001043 LiveRegs.addReg(Reg);
Andrew Trick6b104f82013-12-28 21:56:55 +00001044 }
1045}
1046
Matthias Braun868bbd42017-05-27 02:50:50 +00001047void ScheduleDAGInstrs::fixupKills(MachineBasicBlock &MBB) {
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00001048 DEBUG(dbgs() << "Fixup kills for " << printMBBReference(MBB) << '\n');
Pete Cooper300069a2015-05-04 16:52:06 +00001049
Matthias Braun868bbd42017-05-27 02:50:50 +00001050 LiveRegs.init(*TRI);
1051 LiveRegs.addLiveOuts(MBB);
Andrew Trick6b104f82013-12-28 21:56:55 +00001052
1053 // Examine block from end to start...
Matthias Braun868bbd42017-05-27 02:50:50 +00001054 for (MachineInstr &MI : make_range(MBB.rbegin(), MBB.rend())) {
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +00001055 if (MI.isDebugValue())
Andrew Trick6b104f82013-12-28 21:56:55 +00001056 continue;
1057
1058 // Update liveness. Registers that are defed but not used in this
1059 // instruction are now dead. Mark register and all subregs as they
1060 // are completely defined.
Matthias Braun868bbd42017-05-27 02:50:50 +00001061 for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
1062 const MachineOperand &MO = *O;
1063 if (MO.isReg()) {
1064 if (!MO.isDef())
1065 continue;
1066 unsigned Reg = MO.getReg();
1067 if (!Reg)
1068 continue;
1069 LiveRegs.removeReg(Reg);
1070 } else if (MO.isRegMask()) {
1071 LiveRegs.removeRegsInMask(MO);
1072 }
Andrew Trick6b104f82013-12-28 21:56:55 +00001073 }
1074
Matthias Braun868bbd42017-05-27 02:50:50 +00001075 // If there is a bundle header fix it up first.
1076 if (!MI.isBundled()) {
1077 toggleKills(MRI, LiveRegs, MI, true);
1078 } else {
1079 MachineBasicBlock::instr_iterator First = MI.getIterator();
1080 if (MI.isBundle()) {
1081 toggleKills(MRI, LiveRegs, MI, false);
1082 ++First;
Andrew Trick6b104f82013-12-28 21:56:55 +00001083 }
Matthias Braun868bbd42017-05-27 02:50:50 +00001084 // Some targets make the (questionable) assumtion that the instructions
1085 // inside the bundle are ordered and consequently only the last use of
1086 // a register inside the bundle can kill it.
1087 MachineBasicBlock::instr_iterator I = std::next(First);
1088 while (I->isBundledWithSucc())
1089 ++I;
1090 do {
1091 if (!I->isDebugValue())
1092 toggleKills(MRI, LiveRegs, *I, true);
1093 --I;
1094 } while(I != First);
Andrew Trick6b104f82013-12-28 21:56:55 +00001095 }
1096 }
1097}
1098
Dan Gohman60cb69e2008-11-19 23:18:57 +00001099void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
Matthias Braun8c209aa2017-01-28 02:02:38 +00001100 // Cannot completely remove virtual function even in release mode.
Aaron Ballman615eb472017-10-15 14:32:27 +00001101#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Dan Gohman60cb69e2008-11-19 23:18:57 +00001102 SU->getInstr()->dump();
Manman Ren742534c2012-09-06 19:06:06 +00001103#endif
Dan Gohman60cb69e2008-11-19 23:18:57 +00001104}
1105
1106std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
Alp Tokere69170a2014-06-26 22:52:05 +00001107 std::string s;
1108 raw_string_ostream oss(s);
Dan Gohmanb9543432009-02-10 23:27:53 +00001109 if (SU == &EntrySU)
1110 oss << "<entry>";
1111 else if (SU == &ExitSU)
1112 oss << "<exit>";
1113 else
Eric Christopher1cdefae2015-02-27 00:11:34 +00001114 SU->getInstr()->print(oss, /*SkipOpers=*/true);
Dan Gohman60cb69e2008-11-19 23:18:57 +00001115 return oss.str();
1116}
1117
Andrew Trick1b2324d2012-03-07 00:18:22 +00001118/// Return the basic block label. It is not necessarilly unique because a block
1119/// contains multiple scheduling regions. But it is fine for visualization.
1120std::string ScheduleDAGInstrs::getDAGName() const {
1121 return "dag." + BB->getFullName();
1122}
Andrew Trick90f711d2012-10-15 18:02:27 +00001123
Andrew Trick48d392e2012-11-28 05:13:28 +00001124//===----------------------------------------------------------------------===//
1125// SchedDFSResult Implementation
1126//===----------------------------------------------------------------------===//
1127
1128namespace llvm {
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001129
Matthias Braunbd7d9182017-01-27 18:53:00 +00001130/// Internal state used to compute SchedDFSResult.
Andrew Trick48d392e2012-11-28 05:13:28 +00001131class SchedDFSImpl {
1132 SchedDFSResult &R;
1133
1134 /// Join DAG nodes into equivalence classes by their subtree.
1135 IntEqClasses SubtreeClasses;
1136 /// List PredSU, SuccSU pairs that represent data edges between subtrees.
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001137 std::vector<std::pair<const SUnit *, const SUnit*>> ConnectionPairs;
Andrew Trick48d392e2012-11-28 05:13:28 +00001138
Andrew Trickffc80972013-01-25 06:52:27 +00001139 struct RootData {
1140 unsigned NodeID;
Matthias Braunbd7d9182017-01-27 18:53:00 +00001141 unsigned ParentNodeID; ///< Parent node (member of the parent subtree).
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001142 unsigned SubInstrCount = 0; ///< Instr count in this tree only, not
1143 /// children.
Andrew Trickffc80972013-01-25 06:52:27 +00001144
1145 RootData(unsigned id): NodeID(id),
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001146 ParentNodeID(SchedDFSResult::InvalidSubtreeID) {}
Andrew Trickffc80972013-01-25 06:52:27 +00001147
1148 unsigned getSparseSetIndex() const { return NodeID; }
1149 };
1150
1151 SparseSet<RootData> RootSet;
1152
Andrew Trick48d392e2012-11-28 05:13:28 +00001153public:
Andrew Trickffc80972013-01-25 06:52:27 +00001154 SchedDFSImpl(SchedDFSResult &r): R(r), SubtreeClasses(R.DFSNodeData.size()) {
1155 RootSet.setUniverse(R.DFSNodeData.size());
1156 }
Andrew Trick48d392e2012-11-28 05:13:28 +00001157
Matthias Braunbd7d9182017-01-27 18:53:00 +00001158 /// Returns true if this node been visited by the DFS traversal.
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001159 ///
1160 /// During visitPostorderNode the Node's SubtreeID is assigned to the Node
1161 /// ID. Later, SubtreeID is updated but remains valid.
Andrew Trick48d392e2012-11-28 05:13:28 +00001162 bool isVisited(const SUnit *SU) const {
Andrew Trickffc80972013-01-25 06:52:27 +00001163 return R.DFSNodeData[SU->NodeNum].SubtreeID
1164 != SchedDFSResult::InvalidSubtreeID;
Andrew Trick48d392e2012-11-28 05:13:28 +00001165 }
1166
Matthias Braunbd7d9182017-01-27 18:53:00 +00001167 /// Initializes this node's instruction count. We don't need to flag the node
Andrew Trick48d392e2012-11-28 05:13:28 +00001168 /// visited until visitPostorder because the DAG cannot have cycles.
1169 void visitPreorder(const SUnit *SU) {
Andrew Trickffc80972013-01-25 06:52:27 +00001170 R.DFSNodeData[SU->NodeNum].InstrCount =
1171 SU->getInstr()->isTransient() ? 0 : 1;
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001172 }
1173
1174 /// Called once for each node after all predecessors are visited. Revisit this
1175 /// node's predecessors and potentially join them now that we know the ILP of
1176 /// the other predecessors.
1177 void visitPostorderNode(const SUnit *SU) {
1178 // Mark this node as the root of a subtree. It may be joined with its
1179 // successors later.
Andrew Trickffc80972013-01-25 06:52:27 +00001180 R.DFSNodeData[SU->NodeNum].SubtreeID = SU->NodeNum;
1181 RootData RData(SU->NodeNum);
1182 RData.SubInstrCount = SU->getInstr()->isTransient() ? 0 : 1;
Andrew Trick48d392e2012-11-28 05:13:28 +00001183
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001184 // If any predecessors are still in their own subtree, they either cannot be
1185 // joined or are large enough to remain separate. If this parent node's
1186 // total instruction count is not greater than a child subtree by at least
1187 // the subtree limit, then try to join it now since splitting subtrees is
1188 // only useful if multiple high-pressure paths are possible.
Andrew Trickffc80972013-01-25 06:52:27 +00001189 unsigned InstrCount = R.DFSNodeData[SU->NodeNum].InstrCount;
Matthias Braun298e0072016-09-30 23:08:07 +00001190 for (const SDep &PredDep : SU->Preds) {
1191 if (PredDep.getKind() != SDep::Data)
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001192 continue;
Matthias Braun298e0072016-09-30 23:08:07 +00001193 unsigned PredNum = PredDep.getSUnit()->NodeNum;
Andrew Trickffc80972013-01-25 06:52:27 +00001194 if ((InstrCount - R.DFSNodeData[PredNum].InstrCount) < R.SubtreeLimit)
Matthias Braun298e0072016-09-30 23:08:07 +00001195 joinPredSubtree(PredDep, SU, /*CheckLimit=*/false);
Andrew Trickffc80972013-01-25 06:52:27 +00001196
1197 // Either link or merge the TreeData entry from the child to the parent.
Andrew Trick646eeb62013-01-25 06:52:30 +00001198 if (R.DFSNodeData[PredNum].SubtreeID == PredNum) {
1199 // If the predecessor's parent is invalid, this is a tree edge and the
1200 // current node is the parent.
1201 if (RootSet[PredNum].ParentNodeID == SchedDFSResult::InvalidSubtreeID)
1202 RootSet[PredNum].ParentNodeID = SU->NodeNum;
1203 }
1204 else if (RootSet.count(PredNum)) {
1205 // The predecessor is not a root, but is still in the root set. This
1206 // must be the new parent that it was just joined to. Note that
1207 // RootSet[PredNum].ParentNodeID may either be invalid or may still be
1208 // set to the original parent.
Andrew Trickffc80972013-01-25 06:52:27 +00001209 RData.SubInstrCount += RootSet[PredNum].SubInstrCount;
1210 RootSet.erase(PredNum);
1211 }
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001212 }
Andrew Trickffc80972013-01-25 06:52:27 +00001213 RootSet[SU->NodeNum] = RData;
1214 }
1215
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001216 /// Called once for each tree edge after calling visitPostOrderNode on
Matthias Braunbd7d9182017-01-27 18:53:00 +00001217 /// the predecessor. Increment the parent node's instruction count and
Andrew Trickffc80972013-01-25 06:52:27 +00001218 /// preemptively join this subtree to its parent's if it is small enough.
1219 void visitPostorderEdge(const SDep &PredDep, const SUnit *Succ) {
1220 R.DFSNodeData[Succ->NodeNum].InstrCount
1221 += R.DFSNodeData[PredDep.getSUnit()->NodeNum].InstrCount;
1222 joinPredSubtree(PredDep, Succ);
Andrew Trick48d392e2012-11-28 05:13:28 +00001223 }
1224
Matthias Braunbd7d9182017-01-27 18:53:00 +00001225 /// Adds a connection for cross edges.
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001226 void visitCrossEdge(const SDep &PredDep, const SUnit *Succ) {
Andrew Trick48d392e2012-11-28 05:13:28 +00001227 ConnectionPairs.push_back(std::make_pair(PredDep.getSUnit(), Succ));
1228 }
1229
Matthias Braunbd7d9182017-01-27 18:53:00 +00001230 /// Sets each node's subtree ID to the representative ID and record
1231 /// connections between trees.
Andrew Trick48d392e2012-11-28 05:13:28 +00001232 void finalize() {
1233 SubtreeClasses.compress();
Andrew Trickffc80972013-01-25 06:52:27 +00001234 R.DFSTreeData.resize(SubtreeClasses.getNumClasses());
1235 assert(SubtreeClasses.getNumClasses() == RootSet.size()
1236 && "number of roots should match trees");
Matthias Braun298e0072016-09-30 23:08:07 +00001237 for (const RootData &Root : RootSet) {
1238 unsigned TreeID = SubtreeClasses[Root.NodeID];
1239 if (Root.ParentNodeID != SchedDFSResult::InvalidSubtreeID)
1240 R.DFSTreeData[TreeID].ParentTreeID = SubtreeClasses[Root.ParentNodeID];
1241 R.DFSTreeData[TreeID].SubInstrCount = Root.SubInstrCount;
Andrew Trick646eeb62013-01-25 06:52:30 +00001242 // Note that SubInstrCount may be greater than InstrCount if we joined
1243 // subtrees across a cross edge. InstrCount will be attributed to the
1244 // original parent, while SubInstrCount will be attributed to the joined
1245 // parent.
Andrew Trickffc80972013-01-25 06:52:27 +00001246 }
Andrew Trick48d392e2012-11-28 05:13:28 +00001247 R.SubtreeConnections.resize(SubtreeClasses.getNumClasses());
1248 R.SubtreeConnectLevels.resize(SubtreeClasses.getNumClasses());
1249 DEBUG(dbgs() << R.getNumSubtrees() << " subtrees:\n");
Andrew Trickffc80972013-01-25 06:52:27 +00001250 for (unsigned Idx = 0, End = R.DFSNodeData.size(); Idx != End; ++Idx) {
1251 R.DFSNodeData[Idx].SubtreeID = SubtreeClasses[Idx];
Andrew Trick48d392e2012-11-28 05:13:28 +00001252 DEBUG(dbgs() << " SU(" << Idx << ") in tree "
Andrew Trickffc80972013-01-25 06:52:27 +00001253 << R.DFSNodeData[Idx].SubtreeID << '\n');
Andrew Trick48d392e2012-11-28 05:13:28 +00001254 }
Matthias Braun298e0072016-09-30 23:08:07 +00001255 for (const std::pair<const SUnit*, const SUnit*> &P : ConnectionPairs) {
1256 unsigned PredTree = SubtreeClasses[P.first->NodeNum];
1257 unsigned SuccTree = SubtreeClasses[P.second->NodeNum];
Andrew Trick48d392e2012-11-28 05:13:28 +00001258 if (PredTree == SuccTree)
1259 continue;
Matthias Braun298e0072016-09-30 23:08:07 +00001260 unsigned Depth = P.first->getDepth();
Andrew Trick48d392e2012-11-28 05:13:28 +00001261 addConnection(PredTree, SuccTree, Depth);
1262 addConnection(SuccTree, PredTree, Depth);
1263 }
1264 }
1265
1266protected:
Matthias Braunbd7d9182017-01-27 18:53:00 +00001267 /// Joins the predecessor subtree with the successor that is its DFS parent.
1268 /// Applies some heuristics before joining.
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001269 bool joinPredSubtree(const SDep &PredDep, const SUnit *Succ,
1270 bool CheckLimit = true) {
1271 assert(PredDep.getKind() == SDep::Data && "Subtrees are for data edges");
1272
1273 // Check if the predecessor is already joined.
1274 const SUnit *PredSU = PredDep.getSUnit();
1275 unsigned PredNum = PredSU->NodeNum;
Andrew Trickffc80972013-01-25 06:52:27 +00001276 if (R.DFSNodeData[PredNum].SubtreeID != PredNum)
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001277 return false;
Andrew Trickb52a8562013-01-25 00:12:57 +00001278
1279 // Four is the magic number of successors before a node is considered a
1280 // pinch point.
1281 unsigned NumDataSucs = 0;
Matthias Braun298e0072016-09-30 23:08:07 +00001282 for (const SDep &SuccDep : PredSU->Succs) {
1283 if (SuccDep.getKind() == SDep::Data) {
Andrew Trickb52a8562013-01-25 00:12:57 +00001284 if (++NumDataSucs >= 4)
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001285 return false;
Andrew Trickb52a8562013-01-25 00:12:57 +00001286 }
1287 }
Andrew Trickffc80972013-01-25 06:52:27 +00001288 if (CheckLimit && R.DFSNodeData[PredNum].InstrCount > R.SubtreeLimit)
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001289 return false;
Andrew Trickffc80972013-01-25 06:52:27 +00001290 R.DFSNodeData[PredNum].SubtreeID = Succ->NodeNum;
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001291 SubtreeClasses.join(Succ->NodeNum, PredNum);
1292 return true;
Andrew Trickb52a8562013-01-25 00:12:57 +00001293 }
1294
Andrew Trick48d392e2012-11-28 05:13:28 +00001295 /// Called by finalize() to record a connection between trees.
1296 void addConnection(unsigned FromTree, unsigned ToTree, unsigned Depth) {
1297 if (!Depth)
1298 return;
1299
Andrew Trickffc80972013-01-25 06:52:27 +00001300 do {
1301 SmallVectorImpl<SchedDFSResult::Connection> &Connections =
1302 R.SubtreeConnections[FromTree];
Matthias Braun298e0072016-09-30 23:08:07 +00001303 for (SchedDFSResult::Connection &C : Connections) {
1304 if (C.TreeID == ToTree) {
1305 C.Level = std::max(C.Level, Depth);
Andrew Trickffc80972013-01-25 06:52:27 +00001306 return;
1307 }
Andrew Trick48d392e2012-11-28 05:13:28 +00001308 }
Andrew Trickffc80972013-01-25 06:52:27 +00001309 Connections.push_back(SchedDFSResult::Connection(ToTree, Depth));
1310 FromTree = R.DFSTreeData[FromTree].ParentTreeID;
1311 } while (FromTree != SchedDFSResult::InvalidSubtreeID);
Andrew Trick48d392e2012-11-28 05:13:28 +00001312 }
1313};
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001314
Matthias Braunbd7d9182017-01-27 18:53:00 +00001315} // end namespace llvm
Andrew Trick48d392e2012-11-28 05:13:28 +00001316
Andrew Trick90f711d2012-10-15 18:02:27 +00001317namespace {
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001318
Matthias Braunbd7d9182017-01-27 18:53:00 +00001319/// Manage the stack used by a reverse depth-first search over the DAG.
Andrew Trick90f711d2012-10-15 18:02:27 +00001320class SchedDAGReverseDFS {
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001321 std::vector<std::pair<const SUnit *, SUnit::const_pred_iterator>> DFSStack;
1322
Andrew Trick90f711d2012-10-15 18:02:27 +00001323public:
1324 bool isComplete() const { return DFSStack.empty(); }
1325
1326 void follow(const SUnit *SU) {
1327 DFSStack.push_back(std::make_pair(SU, SU->Preds.begin()));
1328 }
1329 void advance() { ++DFSStack.back().second; }
1330
Andrew Trick48d392e2012-11-28 05:13:28 +00001331 const SDep *backtrack() {
1332 DFSStack.pop_back();
Craig Topperc0196b12014-04-14 00:51:57 +00001333 return DFSStack.empty() ? nullptr : std::prev(DFSStack.back().second);
Andrew Trick48d392e2012-11-28 05:13:28 +00001334 }
Andrew Trick90f711d2012-10-15 18:02:27 +00001335
1336 const SUnit *getCurr() const { return DFSStack.back().first; }
1337
1338 SUnit::const_pred_iterator getPred() const { return DFSStack.back().second; }
1339
1340 SUnit::const_pred_iterator getPredEnd() const {
1341 return getCurr()->Preds.end();
1342 }
1343};
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001344
1345} // end anonymous namespace
Andrew Trick90f711d2012-10-15 18:02:27 +00001346
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001347static bool hasDataSucc(const SUnit *SU) {
Matthias Braun298e0072016-09-30 23:08:07 +00001348 for (const SDep &SuccDep : SU->Succs) {
1349 if (SuccDep.getKind() == SDep::Data &&
1350 !SuccDep.getSUnit()->isBoundaryNode())
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001351 return true;
1352 }
1353 return false;
1354}
1355
Matthias Braunbd7d9182017-01-27 18:53:00 +00001356/// Computes an ILP metric for all nodes in the subDAG reachable via depth-first
Andrew Trick90f711d2012-10-15 18:02:27 +00001357/// search from this root.
Andrew Tricke2c3f5c2013-01-25 06:33:57 +00001358void SchedDFSResult::compute(ArrayRef<SUnit> SUnits) {
Andrew Trick90f711d2012-10-15 18:02:27 +00001359 if (!IsBottomUp)
Eric Christopher52854dc2017-08-03 22:41:12 +00001360 llvm_unreachable("Top-down ILP metric is unimplemented");
Andrew Trick90f711d2012-10-15 18:02:27 +00001361
Andrew Trick48d392e2012-11-28 05:13:28 +00001362 SchedDFSImpl Impl(*this);
Matthias Braun298e0072016-09-30 23:08:07 +00001363 for (const SUnit &SU : SUnits) {
1364 if (Impl.isVisited(&SU) || hasDataSucc(&SU))
Andrew Tricke2c3f5c2013-01-25 06:33:57 +00001365 continue;
1366
Andrew Trick48d392e2012-11-28 05:13:28 +00001367 SchedDAGReverseDFS DFS;
Matthias Braun298e0072016-09-30 23:08:07 +00001368 Impl.visitPreorder(&SU);
1369 DFS.follow(&SU);
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001370 while (true) {
Andrew Trick48d392e2012-11-28 05:13:28 +00001371 // Traverse the leftmost path as far as possible.
1372 while (DFS.getPred() != DFS.getPredEnd()) {
1373 const SDep &PredDep = *DFS.getPred();
1374 DFS.advance();
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001375 // Ignore non-data edges.
Andrew Trick646eeb62013-01-25 06:52:30 +00001376 if (PredDep.getKind() != SDep::Data
1377 || PredDep.getSUnit()->isBoundaryNode()) {
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001378 continue;
Andrew Trick646eeb62013-01-25 06:52:30 +00001379 }
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001380 // An already visited edge is a cross edge, assuming an acyclic DAG.
Andrew Trick48d392e2012-11-28 05:13:28 +00001381 if (Impl.isVisited(PredDep.getSUnit())) {
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001382 Impl.visitCrossEdge(PredDep, DFS.getCurr());
Andrew Trick48d392e2012-11-28 05:13:28 +00001383 continue;
1384 }
1385 Impl.visitPreorder(PredDep.getSUnit());
1386 DFS.follow(PredDep.getSUnit());
1387 }
1388 // Visit the top of the stack in postorder and backtrack.
1389 const SUnit *Child = DFS.getCurr();
1390 const SDep *PredDep = DFS.backtrack();
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001391 Impl.visitPostorderNode(Child);
1392 if (PredDep)
1393 Impl.visitPostorderEdge(*PredDep, DFS.getCurr());
Andrew Trick48d392e2012-11-28 05:13:28 +00001394 if (DFS.isComplete())
1395 break;
Andrew Trick90f711d2012-10-15 18:02:27 +00001396 }
Andrew Trick48d392e2012-11-28 05:13:28 +00001397 }
1398 Impl.finalize();
1399}
1400
1401/// The root of the given SubtreeID was just scheduled. For all subtrees
1402/// connected to this tree, record the depth of the connection so that the
1403/// nearest connected subtrees can be prioritized.
1404void SchedDFSResult::scheduleTree(unsigned SubtreeID) {
Matthias Braun298e0072016-09-30 23:08:07 +00001405 for (const Connection &C : SubtreeConnections[SubtreeID]) {
1406 SubtreeConnectLevels[C.TreeID] =
1407 std::max(SubtreeConnectLevels[C.TreeID], C.Level);
1408 DEBUG(dbgs() << " Tree: " << C.TreeID
1409 << " @" << SubtreeConnectLevels[C.TreeID] << '\n');
Andrew Trick90f711d2012-10-15 18:02:27 +00001410 }
1411}
1412
Aaron Ballman615eb472017-10-15 14:32:27 +00001413#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braun8c209aa2017-01-28 02:02:38 +00001414LLVM_DUMP_METHOD void ILPValue::print(raw_ostream &OS) const {
Andrew Trick48d392e2012-11-28 05:13:28 +00001415 OS << InstrCount << " / " << Length << " = ";
1416 if (!Length)
Andrew Trick90f711d2012-10-15 18:02:27 +00001417 OS << "BADILP";
Andrew Trick48d392e2012-11-28 05:13:28 +00001418 else
1419 OS << format("%g", ((double)InstrCount / Length));
Andrew Trick90f711d2012-10-15 18:02:27 +00001420}
1421
Matthias Braun8c209aa2017-01-28 02:02:38 +00001422LLVM_DUMP_METHOD void ILPValue::dump() const {
Andrew Trick90f711d2012-10-15 18:02:27 +00001423 dbgs() << *this << '\n';
1424}
1425
1426namespace llvm {
1427
Alp Tokerd8d510a2014-07-01 21:19:13 +00001428LLVM_DUMP_METHOD
Andrew Trick90f711d2012-10-15 18:02:27 +00001429raw_ostream &operator<<(raw_ostream &OS, const ILPValue &Val) {
1430 Val.print(OS);
1431 return OS;
1432}
1433
Matthias Braunbd7d9182017-01-27 18:53:00 +00001434} // end namespace llvm
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001435
Matthias Braun8c209aa2017-01-28 02:02:38 +00001436#endif