blob: 3950bb156a4972644652bcb8954b746800b8bfc6 [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"
Eugene Zelenko7ea69232017-06-01 23:25:02 +000041#include "llvm/IR/Constants.h"
Jonas Paulssonac29f012016-02-03 17:52:29 +000042#include "llvm/IR/Function.h"
Eugene Zelenko7ea69232017-06-01 23:25:02 +000043#include "llvm/IR/Instruction.h"
44#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000045#include "llvm/IR/Operator.h"
Eugene Zelenko7ea69232017-06-01 23:25:02 +000046#include "llvm/IR/Type.h"
47#include "llvm/IR/Value.h"
48#include "llvm/MC/LaneBitmask.h"
49#include "llvm/MC/MCRegisterInfo.h"
50#include "llvm/Support/Casting.h"
Andrew Trickda01ba32012-05-15 18:59:41 +000051#include "llvm/Support/CommandLine.h"
Eugene Zelenko7ea69232017-06-01 23:25:02 +000052#include "llvm/Support/Compiler.h"
Dan Gohman60cb69e2008-11-19 23:18:57 +000053#include "llvm/Support/Debug.h"
Eugene Zelenko7ea69232017-06-01 23:25:02 +000054#include "llvm/Support/ErrorHandling.h"
Andrew Trick90f711d2012-10-15 18:02:27 +000055#include "llvm/Support/Format.h"
Dan Gohman60cb69e2008-11-19 23:18:57 +000056#include "llvm/Support/raw_ostream.h"
Eugene Zelenko7ea69232017-06-01 23:25:02 +000057#include <algorithm>
58#include <cassert>
59#include <iterator>
60#include <string>
61#include <utility>
62#include <vector>
Andrew Trickc01b0042013-08-23 17:48:43 +000063
Dan Gohman60cb69e2008-11-19 23:18:57 +000064using namespace llvm;
65
Evandro Menezes0cd23f562017-07-11 22:08:28 +000066#define DEBUG_TYPE "machine-scheduler"
Chandler Carruth1b9dde02014-04-22 02:02:50 +000067
Andrew Trickda01ba32012-05-15 18:59:41 +000068static cl::opt<bool> EnableAASchedMI("enable-aa-sched-mi", cl::Hidden,
69 cl::ZeroOrMore, cl::init(false),
Jonas Paulssonbf408bb2015-01-07 13:20:57 +000070 cl::desc("Enable use of AA during MI DAG construction"));
Andrew Trickda01ba32012-05-15 18:59:41 +000071
Hal Finkeldbebb522014-01-25 19:24:54 +000072static cl::opt<bool> UseTBAA("use-tbaa-in-sched-mi", cl::Hidden,
Jonas Paulssonbf408bb2015-01-07 13:20:57 +000073 cl::init(true), cl::desc("Enable use of TBAA during MI DAG construction"));
Hal Finkeldbebb522014-01-25 19:24:54 +000074
Jonas Paulssonac29f012016-02-03 17:52:29 +000075// Note: the two options below might be used in tuning compile time vs
76// output quality. Setting HugeRegion so large that it will never be
77// reached means best-effort, but may be slow.
78
79// When Stores and Loads maps (or NonAliasStores and NonAliasLoads)
80// together hold this many SUs, a reduction of maps will be done.
81static cl::opt<unsigned> HugeRegion("dag-maps-huge-region", cl::Hidden,
82 cl::init(1000), cl::desc("The limit to use while constructing the DAG "
83 "prior to scheduling, at which point a trade-off "
84 "is made to avoid excessive compile time."));
85
Mehdi Amini59ae8542016-04-16 04:58:30 +000086static cl::opt<unsigned> ReductionSize(
87 "dag-maps-reduction-size", cl::Hidden,
Jonas Paulssonac29f012016-02-03 17:52:29 +000088 cl::desc("A huge scheduling region will have maps reduced by this many "
Mehdi Amini59ae8542016-04-16 04:58:30 +000089 "nodes at a time. Defaults to HugeRegion / 2."));
90
91static unsigned getReductionSize() {
92 // Always reduce a huge region with half of the elements, except
93 // when user sets this number explicitly.
94 if (ReductionSize.getNumOccurrences() == 0)
95 return HugeRegion / 2;
96 return ReductionSize;
97}
Jonas Paulssonac29f012016-02-03 17:52:29 +000098
99static void dumpSUList(ScheduleDAGInstrs::SUList &L) {
Aaron Ballman615eb472017-10-15 14:32:27 +0000100#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Jonas Paulssonac29f012016-02-03 17:52:29 +0000101 dbgs() << "{ ";
Matthias Braun298e0072016-09-30 23:08:07 +0000102 for (const SUnit *su : L) {
Jonas Paulssonac29f012016-02-03 17:52:29 +0000103 dbgs() << "SU(" << su->NodeNum << ")";
104 if (su != L.back())
105 dbgs() << ", ";
106 }
107 dbgs() << "}\n";
108#endif
109}
110
Dan Gohman619ef482009-01-15 19:20:50 +0000111ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf,
Alexey Samsonov8968e6d2014-08-20 19:36:05 +0000112 const MachineLoopInfo *mli,
Matthias Braun93563e72015-11-03 01:53:29 +0000113 bool RemoveKillFlags)
Matthias Braunb17e8b12015-12-04 19:54:24 +0000114 : ScheduleDAG(mf), MLI(mli), MFI(mf.getFrameInfo()),
Eugene Zelenko7ea69232017-06-01 23:25:02 +0000115 RemoveKillFlags(RemoveKillFlags),
Jonas Paulssonac29f012016-02-03 17:52:29 +0000116 UnknownValue(UndefValue::get(
Matthias Braunf1caa282017-12-15 22:22:58 +0000117 Type::getVoidTy(mf.getFunction().getContext()))) {
Devang Patele5feef02011-06-02 20:07:12 +0000118 DbgValues.clear();
Andrew Trick9b635132012-09-18 18:20:00 +0000119
Eric Christopher2c635492015-01-27 07:54:39 +0000120 const TargetSubtargetInfo &ST = mf.getSubtarget();
Sanjay Patel0d7df362018-04-08 19:56:04 +0000121 SchedModel.init(&ST);
Evan Chengf0236e02009-10-18 19:58:47 +0000122}
Dan Gohman60cb69e2008-11-19 23:18:57 +0000123
Hiroshi Inoueb49b0152017-10-12 06:26:04 +0000124/// If this machine instr has memory reference information and it can be
125/// tracked to a normal reference to a known object, return the Value
126/// for that object. This function returns false the memory location is
127/// unknown or may alias anything.
128static bool getUnderlyingObjectsForInstr(const MachineInstr *MI,
Matthias Braun941a7052016-07-28 18:40:00 +0000129 const MachineFrameInfo &MFI,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000130 UnderlyingObjectsVector &Objects,
131 const DataLayout &DL) {
Geoff Berry63817132016-04-14 21:31:07 +0000132 auto allMMOsOkay = [&]() {
133 for (const MachineMemOperand *MMO : MI->memoperands()) {
134 if (MMO->isVolatile())
135 return false;
Hal Finkel66859ae2012-12-10 18:49:16 +0000136
Geoff Berry63817132016-04-14 21:31:07 +0000137 if (const PseudoSourceValue *PSV = MMO->getPseudoValue()) {
138 // Function that contain tail calls don't have unique PseudoSourceValue
139 // objects. Two PseudoSourceValues might refer to the same or
140 // overlapping locations. The client code calling this function assumes
141 // this is not the case. So return a conservative answer of no known
142 // object.
Matthias Braun941a7052016-07-28 18:40:00 +0000143 if (MFI.hasTailCall())
Geoff Berry63817132016-04-14 21:31:07 +0000144 return false;
Geoff Berryc0739d82016-04-12 15:50:19 +0000145
Geoff Berry63817132016-04-14 21:31:07 +0000146 // For now, ignore PseudoSourceValues which may alias LLVM IR values
147 // because the code that uses this function has no way to cope with
148 // such aliases.
Matthias Braun941a7052016-07-28 18:40:00 +0000149 if (PSV->isAliased(&MFI))
Geoff Berry63817132016-04-14 21:31:07 +0000150 return false;
Geoff Berryc0739d82016-04-12 15:50:19 +0000151
Matthias Braun941a7052016-07-28 18:40:00 +0000152 bool MayAlias = PSV->mayAlias(&MFI);
Geoff Berry63817132016-04-14 21:31:07 +0000153 Objects.push_back(UnderlyingObjectsVector::value_type(PSV, MayAlias));
154 } else if (const Value *V = MMO->getValue()) {
155 SmallVector<Value *, 4> Objs;
Hiroshi Inoueb49b0152017-10-12 06:26:04 +0000156 if (!getUnderlyingObjectsForCodeGen(V, Objs, DL))
157 return false;
Geoff Berryc0739d82016-04-12 15:50:19 +0000158
Geoff Berry63817132016-04-14 21:31:07 +0000159 for (Value *V : Objs) {
Hiroshi Inoueb9417db2017-08-01 03:32:15 +0000160 assert(isIdentifiedObject(V));
Geoff Berry63817132016-04-14 21:31:07 +0000161 Objects.push_back(UnderlyingObjectsVector::value_type(V, true));
Geoff Berryc0739d82016-04-12 15:50:19 +0000162 }
Geoff Berry63817132016-04-14 21:31:07 +0000163 } else
164 return false;
Geoff Berryc0739d82016-04-12 15:50:19 +0000165 }
Geoff Berry63817132016-04-14 21:31:07 +0000166 return true;
167 };
168
Hiroshi Inoueb49b0152017-10-12 06:26:04 +0000169 if (!allMMOsOkay()) {
Geoff Berry63817132016-04-14 21:31:07 +0000170 Objects.clear();
Hiroshi Inoueb49b0152017-10-12 06:26:04 +0000171 return false;
172 }
173
174 return true;
Dan Gohman1ee0d412009-01-30 02:49:14 +0000175}
176
Andrew Trick7405c6d2012-04-20 20:05:21 +0000177void ScheduleDAGInstrs::startBlock(MachineBasicBlock *bb) {
178 BB = bb;
Dan Gohmanb9543432009-02-10 23:27:53 +0000179}
180
Andrew Trick52226d42012-03-07 23:00:49 +0000181void ScheduleDAGInstrs::finishBlock() {
Andrew Trick51ee9362012-04-20 20:24:33 +0000182 // Subclasses should no longer refer to the old block.
Craig Topperc0196b12014-04-14 00:51:57 +0000183 BB = nullptr;
Andrew Trick60cf03e2012-03-07 05:21:52 +0000184}
185
Andrew Trick60cf03e2012-03-07 05:21:52 +0000186void ScheduleDAGInstrs::enterRegion(MachineBasicBlock *bb,
187 MachineBasicBlock::iterator begin,
188 MachineBasicBlock::iterator end,
Andrew Tricka53e1012013-08-23 17:48:33 +0000189 unsigned regioninstrs) {
Andrew Trick7405c6d2012-04-20 20:05:21 +0000190 assert(bb == BB && "startBlock should set BB");
Andrew Trick8c207e42012-03-09 04:29:02 +0000191 RegionBegin = begin;
192 RegionEnd = end;
Andrew Tricka53e1012013-08-23 17:48:33 +0000193 NumRegionInstrs = regioninstrs;
Andrew Trick60cf03e2012-03-07 05:21:52 +0000194}
195
Andrew Trick60cf03e2012-03-07 05:21:52 +0000196void ScheduleDAGInstrs::exitRegion() {
197 // Nothing to do.
198}
199
Andrew Trick52226d42012-03-07 23:00:49 +0000200void ScheduleDAGInstrs::addSchedBarrierDeps() {
Craig Topperc0196b12014-04-14 00:51:57 +0000201 MachineInstr *ExitMI = RegionEnd != BB->end() ? &*RegionEnd : nullptr;
Evan Cheng15459b62010-10-23 02:10:46 +0000202 ExitSU.setInstr(ExitMI);
Matthias Braun325cd2c2016-11-11 01:34:21 +0000203 // Add dependencies on the defs and uses of the instruction.
204 if (ExitMI) {
Matthias Braun298e0072016-09-30 23:08:07 +0000205 for (const MachineOperand &MO : ExitMI->operands()) {
Evan Cheng15459b62010-10-23 02:10:46 +0000206 if (!MO.isReg() || MO.isDef()) continue;
207 unsigned Reg = MO.getReg();
Matthias Braun111603f2016-11-10 22:11:00 +0000208 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000209 Uses.insert(PhysRegSUOper(&ExitSU, -1, Reg));
Matthias Braun111603f2016-11-10 22:11:00 +0000210 } else if (TargetRegisterInfo::isVirtualRegister(Reg) && MO.readsReg()) {
Matthias Braun298e0072016-09-30 23:08:07 +0000211 addVRegUseDeps(&ExitSU, ExitMI->getOperandNo(&MO));
Matthias Braun111603f2016-11-10 22:11:00 +0000212 }
Evan Cheng15459b62010-10-23 02:10:46 +0000213 }
Matthias Braun325cd2c2016-11-11 01:34:21 +0000214 }
215 if (!ExitMI || (!ExitMI->isCall() && !ExitMI->isBarrier())) {
Evan Cheng15459b62010-10-23 02:10:46 +0000216 // For others, e.g. fallthrough, conditional branch, assume the exit
Evan Chengcbdf7e82010-10-27 23:17:17 +0000217 // uses all the registers that are livein to the successor blocks.
Matthias Braun298e0072016-09-30 23:08:07 +0000218 for (const MachineBasicBlock *Succ : BB->successors()) {
219 for (const auto &LI : Succ->liveins()) {
Matthias Braund9da1622015-09-09 18:08:03 +0000220 if (!Uses.contains(LI.PhysReg))
221 Uses.insert(PhysRegSUOper(&ExitSU, -1, LI.PhysReg));
Evan Chengcbdf7e82010-10-27 23:17:17 +0000222 }
Matthias Braun298e0072016-09-30 23:08:07 +0000223 }
Evan Cheng15459b62010-10-23 02:10:46 +0000224 }
225}
226
Matthias Braunbd7d9182017-01-27 18:53:00 +0000227/// MO is an operand of SU's instruction that defines a physical register. Adds
Andrew Trickd675a4c2012-02-23 01:52:38 +0000228/// data dependencies from SU to any uses of the physical register.
Andrew Trickae535612012-08-23 00:39:43 +0000229void ScheduleDAGInstrs::addPhysRegDataDeps(SUnit *SU, unsigned OperIdx) {
230 const MachineOperand &MO = SU->getInstr()->getOperand(OperIdx);
Andrew Trickd675a4c2012-02-23 01:52:38 +0000231 assert(MO.isDef() && "expect physreg def");
232
233 // Ask the target if address-backscheduling is desirable, and if so how much.
Eric Christopher2c635492015-01-27 07:54:39 +0000234 const TargetSubtargetInfo &ST = MF.getSubtarget();
Andrew Trickd675a4c2012-02-23 01:52:38 +0000235
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000236 for (MCRegAliasIterator Alias(MO.getReg(), TRI, true);
237 Alias.isValid(); ++Alias) {
Andrew Trick9dbbd3e2012-02-24 07:04:55 +0000238 if (!Uses.contains(*Alias))
Andrew Trickd675a4c2012-02-23 01:52:38 +0000239 continue;
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000240 for (Reg2SUnitsMap::iterator I = Uses.find(*Alias); I != Uses.end(); ++I) {
241 SUnit *UseSU = I->SU;
Andrew Trickd675a4c2012-02-23 01:52:38 +0000242 if (UseSU == SU)
243 continue;
Andrew Trick07dced62012-10-08 18:54:00 +0000244
Andrew Trick07dced62012-10-08 18:54:00 +0000245 // Adjust the dependence latency using operand def/use information,
246 // then allow the target to perform its own adjustments.
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000247 int UseOp = I->OpIdx;
Craig Topperc0196b12014-04-14 00:51:57 +0000248 MachineInstr *RegUse = nullptr;
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000249 SDep Dep;
250 if (UseOp < 0)
251 Dep = SDep(SU, SDep::Artificial);
252 else {
Andrew Tricke833e1c2013-04-13 06:07:40 +0000253 // Set the hasPhysRegDefs only for physreg defs that have a use within
254 // the scheduling region.
255 SU->hasPhysRegDefs = true;
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000256 Dep = SDep(SU, SDep::Data, *Alias);
257 RegUse = UseSU->getInstr();
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000258 }
259 Dep.setLatency(
Andrew Trickde2109e2013-06-15 04:49:57 +0000260 SchedModel.computeOperandLatency(SU->getInstr(), OperIdx, RegUse,
261 UseOp));
Andrew Trick45446062012-06-05 21:11:27 +0000262
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000263 ST.adjustSchedDependency(SU, UseSU, Dep);
264 UseSU->addPred(Dep);
Andrew Trickd675a4c2012-02-23 01:52:38 +0000265 }
266 }
267}
268
Matthias Braunbd7d9182017-01-27 18:53:00 +0000269/// \brief Adds register dependencies (data, anti, and output) from this SUnit
270/// to following instructions in the same scheduling region that depend the
271/// physical register referenced at OperIdx.
Andrew Trickdbee9d82012-01-14 02:17:15 +0000272void ScheduleDAGInstrs::addPhysRegDeps(SUnit *SU, unsigned OperIdx) {
Andrew Trick6b104f82013-12-28 21:56:55 +0000273 MachineInstr *MI = SU->getInstr();
274 MachineOperand &MO = MI->getOperand(OperIdx);
Matthias Braun111603f2016-11-10 22:11:00 +0000275 unsigned Reg = MO.getReg();
Matthias Braunf29b12d2016-11-10 23:46:44 +0000276 // We do not need to track any dependencies for constant registers.
277 if (MRI.isConstantPhysReg(Reg))
278 return;
Andrew Trickdbee9d82012-01-14 02:17:15 +0000279
280 // Optionally add output and anti dependencies. For anti
281 // dependencies we use a latency of 0 because for a multi-issue
282 // target we want to allow the defining instruction to issue
283 // in the same cycle as the using instruction.
284 // TODO: Using a latency of 1 here for output dependencies assumes
285 // there's no cost for reusing registers.
286 SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
Matthias Braun111603f2016-11-10 22:11:00 +0000287 for (MCRegAliasIterator Alias(Reg, TRI, true); Alias.isValid(); ++Alias) {
Andrew Trick9dbbd3e2012-02-24 07:04:55 +0000288 if (!Defs.contains(*Alias))
Andrew Trickd675a4c2012-02-23 01:52:38 +0000289 continue;
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000290 for (Reg2SUnitsMap::iterator I = Defs.find(*Alias); I != Defs.end(); ++I) {
291 SUnit *DefSU = I->SU;
Andrew Trickdbee9d82012-01-14 02:17:15 +0000292 if (DefSU == &ExitSU)
293 continue;
294 if (DefSU != SU &&
295 (Kind != SDep::Output || !MO.isDead() ||
Hal Finkel66d77912014-12-05 02:07:35 +0000296 !DefSU->getInstr()->registerDefIsDead(*Alias))) {
Andrew Trickdbee9d82012-01-14 02:17:15 +0000297 if (Kind == SDep::Anti)
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000298 DefSU->addPred(SDep(SU, Kind, /*Reg=*/*Alias));
Andrew Trickdbee9d82012-01-14 02:17:15 +0000299 else {
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000300 SDep Dep(SU, Kind, /*Reg=*/*Alias);
Andrew Trickde2109e2013-06-15 04:49:57 +0000301 Dep.setLatency(
302 SchedModel.computeOutputLatency(MI, OperIdx, DefSU->getInstr()));
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000303 DefSU->addPred(Dep);
Andrew Trickdbee9d82012-01-14 02:17:15 +0000304 }
305 }
306 }
307 }
308
Andrew Trickd675a4c2012-02-23 01:52:38 +0000309 if (!MO.isDef()) {
Andrew Tricke833e1c2013-04-13 06:07:40 +0000310 SU->hasPhysRegUses = true;
Andrew Trickd675a4c2012-02-23 01:52:38 +0000311 // Either insert a new Reg2SUnits entry with an empty SUnits list, or
312 // retrieve the existing SUnits list for this register's uses.
313 // Push this SUnit on the use list.
Matthias Braun111603f2016-11-10 22:11:00 +0000314 Uses.insert(PhysRegSUOper(SU, OperIdx, Reg));
Andrew Trick6b104f82013-12-28 21:56:55 +0000315 if (RemoveKillFlags)
316 MO.setIsKill(false);
Matthias Braun111603f2016-11-10 22:11:00 +0000317 } else {
Andrew Trickae535612012-08-23 00:39:43 +0000318 addPhysRegDataDeps(SU, OperIdx);
Andrew Trickdbee9d82012-01-14 02:17:15 +0000319
Andrew Trickd675a4c2012-02-23 01:52:38 +0000320 // clear this register's use list
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000321 if (Uses.contains(Reg))
322 Uses.eraseAll(Reg);
Andrew Trickd675a4c2012-02-23 01:52:38 +0000323
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000324 if (!MO.isDead()) {
325 Defs.eraseAll(Reg);
326 } else if (SU->isCall) {
327 // Calls will not be reordered because of chain dependencies (see
328 // below). Since call operands are dead, calls may continue to be added
329 // to the DefList making dependence checking quadratic in the size of
330 // the block. Instead, we leave only one call at the back of the
331 // DefList.
332 Reg2SUnitsMap::RangePair P = Defs.equal_range(Reg);
333 Reg2SUnitsMap::iterator B = P.first;
334 Reg2SUnitsMap::iterator I = P.second;
335 for (bool isBegin = I == B; !isBegin; /* empty */) {
336 isBegin = (--I) == B;
337 if (!I->SU->isCall)
338 break;
339 I = Defs.erase(I);
340 }
Andrew Trickdbee9d82012-01-14 02:17:15 +0000341 }
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000342
Andrew Trickd675a4c2012-02-23 01:52:38 +0000343 // Defs are pushed in the order they are visited and never reordered.
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000344 Defs.insert(PhysRegSUOper(SU, OperIdx, Reg));
Andrew Trickdbee9d82012-01-14 02:17:15 +0000345 }
346}
347
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000348LaneBitmask ScheduleDAGInstrs::getLaneMaskForMO(const MachineOperand &MO) const
349{
350 unsigned Reg = MO.getReg();
351 // No point in tracking lanemasks if we don't have interesting subregisters.
352 const TargetRegisterClass &RC = *MRI.getRegClass(Reg);
353 if (!RC.HasDisjunctSubRegs)
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000354 return LaneBitmask::getAll();
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000355
356 unsigned SubReg = MO.getSubReg();
357 if (SubReg == 0)
358 return RC.getLaneMask();
359 return TRI->getSubRegIndexLaneMask(SubReg);
360}
361
Matthias Braunbd7d9182017-01-27 18:53:00 +0000362/// Adds register output and data dependencies from this SUnit to instructions
363/// that occur later in the same scheduling region if they read from or write to
364/// the virtual register defined at OperIdx.
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000365///
366/// TODO: Hoist loop induction variable increments. This has to be
367/// reevaluated. Generally, IV scheduling should be done before coalescing.
368void ScheduleDAGInstrs::addVRegDefDeps(SUnit *SU, unsigned OperIdx) {
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000369 MachineInstr *MI = SU->getInstr();
370 MachineOperand &MO = MI->getOperand(OperIdx);
371 unsigned Reg = MO.getReg();
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000372
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000373 LaneBitmask DefLaneMask;
374 LaneBitmask KillLaneMask;
375 if (TrackLaneMasks) {
376 bool IsKill = MO.getSubReg() == 0 || MO.isUndef();
377 DefLaneMask = getLaneMaskForMO(MO);
378 // If we have a <read-undef> flag, none of the lane values comes from an
379 // earlier instruction.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000380 KillLaneMask = IsKill ? LaneBitmask::getAll() : DefLaneMask;
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000381
382 // Clear undef flag, we'll re-add it later once we know which subregister
383 // Def is first.
384 MO.setIsUndef(false);
385 } else {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000386 DefLaneMask = LaneBitmask::getAll();
387 KillLaneMask = LaneBitmask::getAll();
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000388 }
389
390 if (MO.isDead()) {
391 assert(CurrentVRegUses.find(Reg) == CurrentVRegUses.end() &&
392 "Dead defs should have no uses");
393 } else {
394 // Add data dependence to all uses we found so far.
395 const TargetSubtargetInfo &ST = MF.getSubtarget();
396 for (VReg2SUnitOperIdxMultiMap::iterator I = CurrentVRegUses.find(Reg),
397 E = CurrentVRegUses.end(); I != E; /*empty*/) {
398 LaneBitmask LaneMask = I->LaneMask;
399 // Ignore uses of other lanes.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000400 if ((LaneMask & KillLaneMask).none()) {
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000401 ++I;
402 continue;
403 }
404
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000405 if ((LaneMask & DefLaneMask).any()) {
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000406 SUnit *UseSU = I->SU;
407 MachineInstr *Use = UseSU->getInstr();
408 SDep Dep(SU, SDep::Data, Reg);
409 Dep.setLatency(SchedModel.computeOperandLatency(MI, OperIdx, Use,
410 I->OperandIndex));
411 ST.adjustSchedDependency(SU, UseSU, Dep);
412 UseSU->addPred(Dep);
413 }
414
415 LaneMask &= ~KillLaneMask;
416 // If we found a Def for all lanes of this use, remove it from the list.
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000417 if (LaneMask.any()) {
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000418 I->LaneMask = LaneMask;
419 ++I;
420 } else
421 I = CurrentVRegUses.erase(I);
422 }
423 }
424
425 // Shortcut: Singly defined vregs do not have output/anti dependencies.
Andrew Trick79795892012-07-30 23:48:17 +0000426 if (MRI.hasOneDef(Reg))
Andrew Trick94053432012-07-28 01:48:15 +0000427 return;
Andrew Trickdb42c6f2012-02-22 06:08:13 +0000428
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000429 // Add output dependence to the next nearest defs of this vreg.
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000430 //
431 // Unless this definition is dead, the output dependence should be
432 // transitively redundant with antidependencies from this definition's
433 // uses. We're conservative for now until we have a way to guarantee the uses
434 // are not eliminated sometime during scheduling. The output dependence edge
435 // is also useful if output latency exceeds def-use latency.
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000436 LaneBitmask LaneMask = DefLaneMask;
437 for (VReg2SUnit &V2SU : make_range(CurrentVRegDefs.find(Reg),
438 CurrentVRegDefs.end())) {
439 // Ignore defs for other lanes.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000440 if ((V2SU.LaneMask & LaneMask).none())
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000441 continue;
442 // Add an output dependence.
443 SUnit *DefSU = V2SU.SU;
444 // Ignore additional defs of the same lanes in one instruction. This can
445 // happen because lanemasks are shared for targets with too many
446 // subregisters. We also use some representration tricks/hacks where we
447 // add super-register defs/uses, to imply that although we only access parts
448 // of the reg we care about the full one.
449 if (DefSU == SU)
450 continue;
451 SDep Dep(SU, SDep::Output, Reg);
452 Dep.setLatency(
453 SchedModel.computeOutputLatency(MI, OperIdx, DefSU->getInstr()));
454 DefSU->addPred(Dep);
455
456 // Update current definition. This can get tricky if the def was about a
457 // bigger lanemask before. We then have to shrink it and create a new
458 // VReg2SUnit for the non-overlapping part.
459 LaneBitmask OverlapMask = V2SU.LaneMask & LaneMask;
460 LaneBitmask NonOverlapMask = V2SU.LaneMask & ~LaneMask;
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000461 V2SU.SU = SU;
462 V2SU.LaneMask = OverlapMask;
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000463 if (NonOverlapMask.any())
Matthias Braun4c994ee2016-05-25 01:18:00 +0000464 CurrentVRegDefs.insert(VReg2SUnit(Reg, NonOverlapMask, DefSU));
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000465 }
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000466 // If there was no CurrentVRegDefs entry for some lanes yet, create one.
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000467 if (LaneMask.any())
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000468 CurrentVRegDefs.insert(VReg2SUnit(Reg, LaneMask, SU));
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000469}
470
Matthias Braunbd7d9182017-01-27 18:53:00 +0000471/// \brief Adds a register data dependency if the instruction that defines the
472/// virtual register used at OperIdx is mapped to an SUnit. Add a register
473/// antidependency from this SUnit to instructions that occur later in the same
474/// scheduling region if they write the virtual register.
Andrew Trick46cc9a42012-02-22 06:08:11 +0000475///
476/// TODO: Handle ExitSU "uses" properly.
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000477void ScheduleDAGInstrs::addVRegUseDeps(SUnit *SU, unsigned OperIdx) {
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000478 const MachineInstr *MI = SU->getInstr();
479 const MachineOperand &MO = MI->getOperand(OperIdx);
480 unsigned Reg = MO.getReg();
Andrew Trick46cc9a42012-02-22 06:08:11 +0000481
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000482 // Remember the use. Data dependencies will be added when we find the def.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000483 LaneBitmask LaneMask = TrackLaneMasks ? getLaneMaskForMO(MO)
484 : LaneBitmask::getAll();
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000485 CurrentVRegUses.insert(VReg2SUnitOperIdx(Reg, LaneMask, OperIdx, SU));
486
487 // Add antidependences to the following defs of the vreg.
488 for (VReg2SUnit &V2SU : make_range(CurrentVRegDefs.find(Reg),
489 CurrentVRegDefs.end())) {
490 // Ignore defs for unrelated lanes.
491 LaneBitmask PrevDefLaneMask = V2SU.LaneMask;
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000492 if ((PrevDefLaneMask & LaneMask).none())
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000493 continue;
494 if (V2SU.SU == SU)
495 continue;
496
497 V2SU.SU->addPred(SDep(SU, SDep::Anti, Reg));
Andrew Trick2bc74c22013-08-30 04:36:57 +0000498 }
Andrew Trick46cc9a42012-02-22 06:08:11 +0000499}
Andrew Trick59ac4fb2012-01-14 02:17:18 +0000500
Matthias Braunbd7d9182017-01-27 18:53:00 +0000501/// Returns true if MI is an instruction we are unable to reason about
Andrew Trickda01ba32012-05-15 18:59:41 +0000502/// (like a call or something with unmodeled side effects).
503static inline bool isGlobalMemoryObject(AliasAnalysis *AA, MachineInstr *MI) {
Rafael Espindola84921b92015-10-24 23:11:13 +0000504 return MI->isCall() || MI->hasUnmodeledSideEffects() ||
Justin Lebard98cf002016-09-10 01:03:20 +0000505 (MI->hasOrderedMemoryRef() && !MI->isDereferenceableInvariantLoad(AA));
Andrew Trickda01ba32012-05-15 18:59:41 +0000506}
507
Jonas Paulssonac29f012016-02-03 17:52:29 +0000508void ScheduleDAGInstrs::addChainDependency (SUnit *SUa, SUnit *SUb,
509 unsigned Latency) {
Eli Friedman93f47e52017-03-09 23:33:36 +0000510 if (SUa->getInstr()->mayAlias(AAForDep, *SUb->getInstr(), UseTBAA)) {
Jonas Paulssonac29f012016-02-03 17:52:29 +0000511 SDep Dep(SUa, SDep::MayAliasMem);
512 Dep.setLatency(Latency);
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000513 SUb->addPred(Dep);
514 }
Andrew Trickda01ba32012-05-15 18:59:41 +0000515}
516
Matthias Braunbd7d9182017-01-27 18:53:00 +0000517/// \brief Creates an SUnit for each real instruction, numbered in top-down
518/// topological order. The instruction order A < B, implies that no edge exists
519/// from B to A.
Andrew Trick46cc9a42012-02-22 06:08:11 +0000520///
521/// Map each real instruction to its SUnit.
522///
Andrew Trick8823dec2012-03-14 04:00:41 +0000523/// After initSUnits, the SUnits vector cannot be resized and the scheduler may
524/// hang onto SUnit pointers. We may relax this in the future by using SUnit IDs
525/// instead of pointers.
526///
527/// MachineScheduler relies on initSUnits numbering the nodes by their order in
528/// the original instruction list.
Andrew Trick46cc9a42012-02-22 06:08:11 +0000529void ScheduleDAGInstrs::initSUnits() {
530 // We'll be allocating one SUnit for each real instruction in the region,
531 // which is contained within a basic block.
Andrew Tricka53e1012013-08-23 17:48:33 +0000532 SUnits.reserve(NumRegionInstrs);
Andrew Trick46cc9a42012-02-22 06:08:11 +0000533
Eugene Zelenko7ea69232017-06-01 23:25:02 +0000534 for (MachineInstr &MI : make_range(RegionBegin, RegionEnd)) {
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000535 if (MI.isDebugValue())
Andrew Trick46cc9a42012-02-22 06:08:11 +0000536 continue;
537
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000538 SUnit *SU = newSUnit(&MI);
539 MISUnitMap[&MI] = SU;
Andrew Trick46cc9a42012-02-22 06:08:11 +0000540
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000541 SU->isCall = MI.isCall();
542 SU->isCommutable = MI.isCommutable();
Andrew Trick46cc9a42012-02-22 06:08:11 +0000543
544 // Assign the Latency field of SU using target-provided information.
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000545 SU->Latency = SchedModel.computeInstrLatency(SU->getInstr());
Andrew Trick880e5732013-12-05 17:55:58 +0000546
Andrew Trick1766f932014-04-18 17:35:08 +0000547 // If this SUnit uses a reserved or unbuffered resource, mark it as such.
548 //
Alp Tokerbeaca192014-05-15 01:52:21 +0000549 // Reserved resources block an instruction from issuing and stall the
Andrew Trick1766f932014-04-18 17:35:08 +0000550 // entire pipeline. These are identified by BufferSize=0.
551 //
Alp Tokerbeaca192014-05-15 01:52:21 +0000552 // Unbuffered resources prevent execution of subsequent instructions that
Andrew Trick1766f932014-04-18 17:35:08 +0000553 // require the same resources. This is used for in-order execution pipelines
554 // within an out-of-order core. These are identified by BufferSize=1.
Andrew Trick880e5732013-12-05 17:55:58 +0000555 if (SchedModel.hasInstrSchedModel()) {
556 const MCSchedClassDesc *SC = getSchedClass(SU);
Matthias Braun298e0072016-09-30 23:08:07 +0000557 for (const MCWriteProcResEntry &PRE :
558 make_range(SchedModel.getWriteProcResBegin(SC),
559 SchedModel.getWriteProcResEnd(SC))) {
560 switch (SchedModel.getProcResource(PRE.ProcResourceIdx)->BufferSize) {
Andrew Trick5a22df42013-12-05 17:56:02 +0000561 case 0:
562 SU->hasReservedResource = true;
563 break;
564 case 1:
Andrew Trick880e5732013-12-05 17:55:58 +0000565 SU->isUnbuffered = true;
566 break;
Andrew Trick5a22df42013-12-05 17:56:02 +0000567 default:
568 break;
Andrew Trick880e5732013-12-05 17:55:58 +0000569 }
570 }
571 }
Andrew Trick46cc9a42012-02-22 06:08:11 +0000572 }
Andrew Trickdbee9d82012-01-14 02:17:15 +0000573}
574
Jonas Paulssonac29f012016-02-03 17:52:29 +0000575class ScheduleDAGInstrs::Value2SUsMap : public MapVector<ValueType, SUList> {
Jonas Paulssonac29f012016-02-03 17:52:29 +0000576 /// Current total number of SUs in map.
Eugene Zelenko7ea69232017-06-01 23:25:02 +0000577 unsigned NumNodes = 0;
Jonas Paulssonac29f012016-02-03 17:52:29 +0000578
579 /// 1 for loads, 0 for stores. (see comment in SUList)
580 unsigned TrueMemOrderLatency;
Jonas Paulssonac29f012016-02-03 17:52:29 +0000581
Matthias Braunbd7d9182017-01-27 18:53:00 +0000582public:
Eugene Zelenko7ea69232017-06-01 23:25:02 +0000583 Value2SUsMap(unsigned lat = 0) : TrueMemOrderLatency(lat) {}
Jonas Paulssonac29f012016-02-03 17:52:29 +0000584
585 /// To keep NumNodes up to date, insert() is used instead of
586 /// this operator w/ push_back().
587 ValueType &operator[](const SUList &Key) {
588 llvm_unreachable("Don't use. Use insert() instead."); };
589
Matthias Braunbd7d9182017-01-27 18:53:00 +0000590 /// Adds SU to the SUList of V. If Map grows huge, reduce its size by calling
591 /// reduce().
Jonas Paulssonac29f012016-02-03 17:52:29 +0000592 void inline insert(SUnit *SU, ValueType V) {
593 MapVector::operator[](V).push_back(SU);
594 NumNodes++;
595 }
596
597 /// Clears the list of SUs mapped to V.
598 void inline clearList(ValueType V) {
599 iterator Itr = find(V);
600 if (Itr != end()) {
Eugene Zelenko7ea69232017-06-01 23:25:02 +0000601 assert(NumNodes >= Itr->second.size());
Jonas Paulssonac29f012016-02-03 17:52:29 +0000602 NumNodes -= Itr->second.size();
603
604 Itr->second.clear();
605 }
606 }
607
608 /// Clears map from all contents.
609 void clear() {
610 MapVector<ValueType, SUList>::clear();
611 NumNodes = 0;
612 }
613
614 unsigned inline size() const { return NumNodes; }
615
Matthias Braunbd7d9182017-01-27 18:53:00 +0000616 /// Counts the number of SUs in this map after a reduction.
Eugene Zelenko7ea69232017-06-01 23:25:02 +0000617 void reComputeSize() {
Jonas Paulssonac29f012016-02-03 17:52:29 +0000618 NumNodes = 0;
619 for (auto &I : *this)
620 NumNodes += I.second.size();
621 }
622
623 unsigned inline getTrueMemOrderLatency() const {
624 return TrueMemOrderLatency;
625 }
626
627 void dump();
628};
629
630void ScheduleDAGInstrs::addChainDependencies(SUnit *SU,
631 Value2SUsMap &Val2SUsMap) {
632 for (auto &I : Val2SUsMap)
633 addChainDependencies(SU, I.second,
634 Val2SUsMap.getTrueMemOrderLatency());
635}
636
637void ScheduleDAGInstrs::addChainDependencies(SUnit *SU,
638 Value2SUsMap &Val2SUsMap,
639 ValueType V) {
640 Value2SUsMap::iterator Itr = Val2SUsMap.find(V);
641 if (Itr != Val2SUsMap.end())
642 addChainDependencies(SU, Itr->second,
643 Val2SUsMap.getTrueMemOrderLatency());
644}
645
646void ScheduleDAGInstrs::addBarrierChain(Value2SUsMap &map) {
Eugene Zelenko7ea69232017-06-01 23:25:02 +0000647 assert(BarrierChain != nullptr);
Jonas Paulssonac29f012016-02-03 17:52:29 +0000648
649 for (auto &I : map) {
650 SUList &sus = I.second;
651 for (auto *SU : sus)
652 SU->addPredBarrier(BarrierChain);
653 }
654 map.clear();
655}
656
657void ScheduleDAGInstrs::insertBarrierChain(Value2SUsMap &map) {
Eugene Zelenko7ea69232017-06-01 23:25:02 +0000658 assert(BarrierChain != nullptr);
Jonas Paulssonac29f012016-02-03 17:52:29 +0000659
660 // Go through all lists of SUs.
661 for (Value2SUsMap::iterator I = map.begin(), EE = map.end(); I != EE;) {
662 Value2SUsMap::iterator CurrItr = I++;
663 SUList &sus = CurrItr->second;
664 SUList::iterator SUItr = sus.begin(), SUEE = sus.end();
665 for (; SUItr != SUEE; ++SUItr) {
666 // Stop on BarrierChain or any instruction above it.
667 if ((*SUItr)->NodeNum <= BarrierChain->NodeNum)
668 break;
669
670 (*SUItr)->addPredBarrier(BarrierChain);
671 }
672
673 // Remove also the BarrierChain from list if present.
NAKAMURA Takumibc46f622016-05-02 17:29:55 +0000674 if (SUItr != SUEE && *SUItr == BarrierChain)
Jonas Paulssonac29f012016-02-03 17:52:29 +0000675 SUItr++;
676
677 // Remove all SUs that are now successors of BarrierChain.
678 if (SUItr != sus.begin())
679 sus.erase(sus.begin(), SUItr);
680 }
681
682 // Remove all entries with empty su lists.
683 map.remove_if([&](std::pair<ValueType, SUList> &mapEntry) {
684 return (mapEntry.second.empty()); });
685
686 // Recompute the size of the map (NumNodes).
687 map.reComputeSize();
688}
689
Andrew Trick88639922012-04-24 17:56:43 +0000690void ScheduleDAGInstrs::buildSchedGraph(AliasAnalysis *AA,
Andrew Trick1a831342013-08-30 03:49:48 +0000691 RegPressureTracker *RPTracker,
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000692 PressureDiffs *PDiffs,
Matthias Braund4f64092016-01-20 00:23:32 +0000693 LiveIntervals *LIS,
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000694 bool TrackLaneMasks) {
Eric Christopher2c635492015-01-27 07:54:39 +0000695 const TargetSubtargetInfo &ST = MF.getSubtarget();
Hal Finkelb350ffd2013-08-29 03:25:05 +0000696 bool UseAA = EnableAASchedMI.getNumOccurrences() > 0 ? EnableAASchedMI
697 : ST.useAA();
Jonas Paulssonac29f012016-02-03 17:52:29 +0000698 AAForDep = UseAA ? AA : nullptr;
699
700 BarrierChain = nullptr;
Hal Finkelb350ffd2013-08-29 03:25:05 +0000701
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000702 this->TrackLaneMasks = TrackLaneMasks;
Andrew Trick310190e2013-09-04 21:00:02 +0000703 MISUnitMap.clear();
704 ScheduleDAG::clearDAG();
705
Andrew Trick46cc9a42012-02-22 06:08:11 +0000706 // Create an SUnit for each real instruction.
707 initSUnits();
Dan Gohman60cb69e2008-11-19 23:18:57 +0000708
Andrew Trick1a831342013-08-30 03:49:48 +0000709 if (PDiffs)
710 PDiffs->init(SUnits.size());
711
Jonas Paulssonac29f012016-02-03 17:52:29 +0000712 // We build scheduling units by walking a block's instruction list
713 // from bottom to top.
Dan Gohman3aab10b2008-12-04 01:35:46 +0000714
Jonas Paulssonac29f012016-02-03 17:52:29 +0000715 // Each MIs' memory operand(s) is analyzed to a list of underlying
Jonas Paulsson22936852016-02-04 13:08:48 +0000716 // objects. The SU is then inserted in the SUList(s) mapped from the
717 // Value(s). Each Value thus gets mapped to lists of SUs depending
718 // on it, stores and loads kept separately. Two SUs are trivially
719 // non-aliasing if they both depend on only identified Values and do
720 // not share any common Value.
Jonas Paulssonac29f012016-02-03 17:52:29 +0000721 Value2SUsMap Stores, Loads(1 /*TrueMemOrderLatency*/);
Dan Gohman3aab10b2008-12-04 01:35:46 +0000722
Jonas Paulssonac29f012016-02-03 17:52:29 +0000723 // Certain memory accesses are known to not alias any SU in Stores
724 // or Loads, and have therefore their own 'NonAlias'
725 // domain. E.g. spill / reload instructions never alias LLVM I/R
Jonas Paulsson22936852016-02-04 13:08:48 +0000726 // Values. It would be nice to assume that this type of memory
727 // accesses always have a proper memory operand modelling, and are
728 // therefore never unanalyzable, but this is conservatively not
729 // done.
Jonas Paulssonac29f012016-02-03 17:52:29 +0000730 Value2SUsMap NonAliasStores, NonAliasLoads(1 /*TrueMemOrderLatency*/);
731
Dale Johannesen49de0602010-03-10 22:13:47 +0000732 // Remove any stale debug info; sometimes BuildSchedGraph is called again
733 // without emitting the info from the previous call.
Devang Patele5feef02011-06-02 20:07:12 +0000734 DbgValues.clear();
Craig Topperc0196b12014-04-14 00:51:57 +0000735 FirstDbgValue = nullptr;
Dale Johannesen49de0602010-03-10 22:13:47 +0000736
Andrew Trickd675a4c2012-02-23 01:52:38 +0000737 assert(Defs.empty() && Uses.empty() &&
738 "Only BuildGraph should update Defs/Uses");
Michael Ilseman3e3194f2013-01-21 18:18:53 +0000739 Defs.setUniverse(TRI->getNumRegs());
740 Uses.setUniverse(TRI->getNumRegs());
Andrew Trick2e116a42011-05-06 21:52:52 +0000741
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000742 assert(CurrentVRegDefs.empty() && "nobody else should use CurrentVRegDefs");
743 assert(CurrentVRegUses.empty() && "nobody else should use CurrentVRegUses");
744 unsigned NumVirtRegs = MRI.getNumVirtRegs();
745 CurrentVRegDefs.setUniverse(NumVirtRegs);
746 CurrentVRegUses.setUniverse(NumVirtRegs);
747
Andrew Trickd675a4c2012-02-23 01:52:38 +0000748 // Model data dependencies between instructions being scheduled and the
749 // ExitSU.
Andrew Trick52226d42012-03-07 23:00:49 +0000750 addSchedBarrierDeps();
Andrew Trickd675a4c2012-02-23 01:52:38 +0000751
Dan Gohmanb9543432009-02-10 23:27:53 +0000752 // Walk the list of instructions, from bottom moving up.
Craig Topperc0196b12014-04-14 00:51:57 +0000753 MachineInstr *DbgMI = nullptr;
Andrew Trick8c207e42012-03-09 04:29:02 +0000754 for (MachineBasicBlock::iterator MII = RegionEnd, MIE = RegionBegin;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000755 MII != MIE; --MII) {
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000756 MachineInstr &MI = *std::prev(MII);
757 if (DbgMI) {
758 DbgValues.push_back(std::make_pair(DbgMI, &MI));
Craig Topperc0196b12014-04-14 00:51:57 +0000759 DbgMI = nullptr;
Devang Patele5feef02011-06-02 20:07:12 +0000760 }
761
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000762 if (MI.isDebugValue()) {
763 DbgMI = &MI;
Dale Johannesen49de0602010-03-10 22:13:47 +0000764 continue;
765 }
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000766 SUnit *SU = MISUnitMap[&MI];
Andrew Trick1a831342013-08-30 03:49:48 +0000767 assert(SU && "No SUnit mapped to this MI");
768
Andrew Trick88639922012-04-24 17:56:43 +0000769 if (RPTracker) {
Matthias Braunb505c762016-01-12 22:57:35 +0000770 RegisterOperands RegOpers;
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000771 RegOpers.collect(MI, *TRI, MRI, TrackLaneMasks, false);
Matthias Braund4f64092016-01-20 00:23:32 +0000772 if (TrackLaneMasks) {
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000773 SlotIndex SlotIdx = LIS->getInstructionIndex(MI);
Matthias Braund4f64092016-01-20 00:23:32 +0000774 RegOpers.adjustLaneLiveness(*LIS, MRI, SlotIdx);
775 }
Matthias Braunb505c762016-01-12 22:57:35 +0000776 if (PDiffs != nullptr)
777 PDiffs->addInstruction(SU->NodeNum, RegOpers, MRI);
778
Yaxun Liuc41e2f62017-12-15 03:56:57 +0000779 if (RPTracker->getPos() == RegionEnd || &*RPTracker->getPos() != &MI)
780 RPTracker->recedeSkipDebugValues();
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000781 assert(&*RPTracker->getPos() == &MI && "RPTracker in sync");
Matthias Braunb505c762016-01-12 22:57:35 +0000782 RPTracker->recede(RegOpers);
Andrew Trick88639922012-04-24 17:56:43 +0000783 }
Devang Patele5feef02011-06-02 20:07:12 +0000784
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000785 assert(
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000786 (CanHandleTerminators || (!MI.isTerminator() && !MI.isPosition())) &&
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000787 "Cannot schedule terminators or labels!");
Dan Gohman60cb69e2008-11-19 23:18:57 +0000788
Dan Gohman3aab10b2008-12-04 01:35:46 +0000789 // Add register-based dependencies (data, anti, and output).
Krzysztof Parzyszeka356bb72016-05-10 16:50:30 +0000790 // For some instructions (calls, returns, inline-asm, etc.) there can
791 // be explicit uses and implicit defs, in which case the use will appear
792 // on the operand list before the def. Do two passes over the operand
793 // list to make sure that defs are processed before any uses.
Andrew Trickec256482012-12-18 20:53:01 +0000794 bool HasVRegDef = false;
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000795 for (unsigned j = 0, n = MI.getNumOperands(); j != n; ++j) {
796 const MachineOperand &MO = MI.getOperand(j);
Krzysztof Parzyszeka356bb72016-05-10 16:50:30 +0000797 if (!MO.isReg() || !MO.isDef())
798 continue;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000799 unsigned Reg = MO.getReg();
Matthias Braun111603f2016-11-10 22:11:00 +0000800 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Andrew Trickdbee9d82012-01-14 02:17:15 +0000801 addPhysRegDeps(SU, j);
Matthias Braun111603f2016-11-10 22:11:00 +0000802 } else if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Krzysztof Parzyszeka356bb72016-05-10 16:50:30 +0000803 HasVRegDef = true;
804 addVRegDefDeps(SU, j);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000805 }
806 }
Krzysztof Parzyszeka356bb72016-05-10 16:50:30 +0000807 // Now process all uses.
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000808 for (unsigned j = 0, n = MI.getNumOperands(); j != n; ++j) {
809 const MachineOperand &MO = MI.getOperand(j);
Matthias Braun8a5b4672016-05-10 20:11:58 +0000810 // Only look at use operands.
811 // We do not need to check for MO.readsReg() here because subsequent
812 // subregister defs will get output dependence edges and need no
813 // additional use dependencies.
Krzysztof Parzyszeka356bb72016-05-10 16:50:30 +0000814 if (!MO.isReg() || !MO.isUse())
815 continue;
816 unsigned Reg = MO.getReg();
Matthias Braun111603f2016-11-10 22:11:00 +0000817 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Krzysztof Parzyszeka356bb72016-05-10 16:50:30 +0000818 addPhysRegDeps(SU, j);
Matthias Braun111603f2016-11-10 22:11:00 +0000819 } else if (TargetRegisterInfo::isVirtualRegister(Reg) && MO.readsReg()) {
Krzysztof Parzyszeka356bb72016-05-10 16:50:30 +0000820 addVRegUseDeps(SU, j);
Matthias Braun111603f2016-11-10 22:11:00 +0000821 }
Krzysztof Parzyszeka356bb72016-05-10 16:50:30 +0000822 }
823
Andrew Trickec256482012-12-18 20:53:01 +0000824 // If we haven't seen any uses in this scheduling region, create a
825 // dependence edge to ExitSU to model the live-out latency. This is required
826 // for vreg defs with no in-region use, and prefetches with no vreg def.
827 //
828 // FIXME: NumDataSuccs would be more precise than NumSuccs here. This
829 // check currently relies on being called before adding chain deps.
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000830 if (SU->NumSuccs == 0 && SU->Latency > 1 && (HasVRegDef || MI.mayLoad())) {
Andrew Trickec256482012-12-18 20:53:01 +0000831 SDep Dep(SU, SDep::Artificial);
832 Dep.setLatency(SU->Latency - 1);
833 ExitSU.addPred(Dep);
834 }
Dan Gohman3aab10b2008-12-04 01:35:46 +0000835
Jonas Paulssonac29f012016-02-03 17:52:29 +0000836 // Add memory dependencies (Note: isStoreToStackSlot and
837 // isLoadFromStackSLot are not usable after stack slots are lowered to
838 // actual addresses).
839
840 // This is a barrier event that acts as a pivotal node in the DAG.
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000841 if (isGlobalMemoryObject(AA, &MI)) {
Jonas Paulssonac29f012016-02-03 17:52:29 +0000842
843 // Become the barrier chain.
David Goodwind2f9c042009-11-09 19:22:17 +0000844 if (BarrierChain)
Jonas Paulssonac29f012016-02-03 17:52:29 +0000845 BarrierChain->addPredBarrier(SU);
David Goodwind2f9c042009-11-09 19:22:17 +0000846 BarrierChain = SU;
847
Jonas Paulssonac29f012016-02-03 17:52:29 +0000848 DEBUG(dbgs() << "Global memory object and new barrier chain: SU("
849 << BarrierChain->NodeNum << ").\n";);
Tom Stellard3e01d472014-12-08 23:36:48 +0000850
Jonas Paulssonac29f012016-02-03 17:52:29 +0000851 // Add dependencies against everything below it and clear maps.
852 addBarrierChain(Stores);
853 addBarrierChain(Loads);
854 addBarrierChain(NonAliasStores);
855 addBarrierChain(NonAliasLoads);
Hal Finkel66859ae2012-12-10 18:49:16 +0000856
Jonas Paulssonac29f012016-02-03 17:52:29 +0000857 continue;
858 }
859
860 // If it's not a store or a variant load, we're done.
Justin Lebard98cf002016-09-10 01:03:20 +0000861 if (!MI.mayStore() &&
862 !(MI.mayLoad() && !MI.isDereferenceableInvariantLoad(AA)))
Jonas Paulssonac29f012016-02-03 17:52:29 +0000863 continue;
864
865 // Always add dependecy edge to BarrierChain if present.
866 if (BarrierChain)
867 BarrierChain->addPredBarrier(SU);
868
869 // Find the underlying objects for MI. The Objs vector is either
870 // empty, or filled with the Values of memory locations which this
Hiroshi Inoueb49b0152017-10-12 06:26:04 +0000871 // SU depends on.
Jonas Paulssonac29f012016-02-03 17:52:29 +0000872 UnderlyingObjectsVector Objs;
Hiroshi Inoueb49b0152017-10-12 06:26:04 +0000873 bool ObjsFound = getUnderlyingObjectsForInstr(&MI, MFI, Objs,
874 MF.getDataLayout());
Jonas Paulssonac29f012016-02-03 17:52:29 +0000875
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +0000876 if (MI.mayStore()) {
Hiroshi Inoueb49b0152017-10-12 06:26:04 +0000877 if (!ObjsFound) {
Jonas Paulssonac29f012016-02-03 17:52:29 +0000878 // An unknown store depends on all stores and loads.
879 addChainDependencies(SU, Stores);
880 addChainDependencies(SU, NonAliasStores);
881 addChainDependencies(SU, Loads);
882 addChainDependencies(SU, NonAliasLoads);
883
884 // Map this store to 'UnknownValue'.
885 Stores.insert(SU, UnknownValue);
Chandler Carruthb4728562016-03-31 21:55:58 +0000886 } else {
887 // Add precise dependencies against all previously seen memory
888 // accesses mapped to the same Value(s).
Geoff Berry63817132016-04-14 21:31:07 +0000889 for (const UnderlyingObject &UnderlObj : Objs) {
890 ValueType V = UnderlObj.getValue();
891 bool ThisMayAlias = UnderlObj.mayAlias();
Chandler Carruthb4728562016-03-31 21:55:58 +0000892
893 // Add dependencies to previous stores and loads mapped to V.
Geoff Berry63817132016-04-14 21:31:07 +0000894 addChainDependencies(SU, (ThisMayAlias ? Stores : NonAliasStores), V);
Chandler Carruthb4728562016-03-31 21:55:58 +0000895 addChainDependencies(SU, (ThisMayAlias ? Loads : NonAliasLoads), V);
Geoff Berryc0739d82016-04-12 15:50:19 +0000896 }
897 // Update the store map after all chains have been added to avoid adding
898 // self-loop edge if multiple underlying objects are present.
Geoff Berry63817132016-04-14 21:31:07 +0000899 for (const UnderlyingObject &UnderlObj : Objs) {
900 ValueType V = UnderlObj.getValue();
901 bool ThisMayAlias = UnderlObj.mayAlias();
Chandler Carruthb4728562016-03-31 21:55:58 +0000902
903 // Map this store to V.
Geoff Berry63817132016-04-14 21:31:07 +0000904 (ThisMayAlias ? Stores : NonAliasStores).insert(SU, V);
Chandler Carruthb4728562016-03-31 21:55:58 +0000905 }
906 // The store may have dependencies to unanalyzable loads and
907 // stores.
908 addChainDependencies(SU, Loads, UnknownValue);
909 addChainDependencies(SU, Stores, UnknownValue);
Hal Finkel66859ae2012-12-10 18:49:16 +0000910 }
Chandler Carruthb4728562016-03-31 21:55:58 +0000911 } else { // SU is a load.
Hiroshi Inoueb49b0152017-10-12 06:26:04 +0000912 if (!ObjsFound) {
Jonas Paulssonac29f012016-02-03 17:52:29 +0000913 // An unknown load depends on all stores.
914 addChainDependencies(SU, Stores);
915 addChainDependencies(SU, NonAliasStores);
916
917 Loads.insert(SU, UnknownValue);
Chandler Carruthb4728562016-03-31 21:55:58 +0000918 } else {
Geoff Berry63817132016-04-14 21:31:07 +0000919 for (const UnderlyingObject &UnderlObj : Objs) {
920 ValueType V = UnderlObj.getValue();
921 bool ThisMayAlias = UnderlObj.mayAlias();
Chandler Carruthb4728562016-03-31 21:55:58 +0000922
923 // Add precise dependencies against all previously seen stores
924 // mapping to the same Value(s).
925 addChainDependencies(SU, (ThisMayAlias ? Stores : NonAliasStores), V);
926
927 // Map this load to V.
928 (ThisMayAlias ? Loads : NonAliasLoads).insert(SU, V);
929 }
930 // The load may have dependencies to unanalyzable stores.
931 addChainDependencies(SU, Stores, UnknownValue);
Hal Finkel66859ae2012-12-10 18:49:16 +0000932 }
Jonas Paulssonac29f012016-02-03 17:52:29 +0000933 }
934
935 // Reduce maps if they grow huge.
936 if (Stores.size() + Loads.size() >= HugeRegion) {
937 DEBUG(dbgs() << "Reducing Stores and Loads maps.\n";);
Mehdi Amini59ae8542016-04-16 04:58:30 +0000938 reduceHugeMemNodeMaps(Stores, Loads, getReductionSize());
Jonas Paulssonac29f012016-02-03 17:52:29 +0000939 }
940 if (NonAliasStores.size() + NonAliasLoads.size() >= HugeRegion) {
941 DEBUG(dbgs() << "Reducing NonAliasStores and NonAliasLoads maps.\n";);
Mehdi Amini59ae8542016-04-16 04:58:30 +0000942 reduceHugeMemNodeMaps(NonAliasStores, NonAliasLoads, getReductionSize());
Dan Gohman60cb69e2008-11-19 23:18:57 +0000943 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000944 }
Jonas Paulssonac29f012016-02-03 17:52:29 +0000945
Andrew Trickb767d1e2012-12-01 01:22:49 +0000946 if (DbgMI)
947 FirstDbgValue = DbgMI;
Dan Gohman619ef482009-01-15 19:20:50 +0000948
Andrew Trickd675a4c2012-02-23 01:52:38 +0000949 Defs.clear();
950 Uses.clear();
Matthias Braun97d0ffb2015-12-04 01:51:19 +0000951 CurrentVRegDefs.clear();
952 CurrentVRegUses.clear();
Jonas Paulssonac29f012016-02-03 17:52:29 +0000953}
954
955raw_ostream &llvm::operator<<(raw_ostream &OS, const PseudoSourceValue* PSV) {
956 PSV->printCustom(OS);
957 return OS;
958}
959
960void ScheduleDAGInstrs::Value2SUsMap::dump() {
961 for (auto &Itr : *this) {
962 if (Itr.first.is<const Value*>()) {
963 const Value *V = Itr.first.get<const Value*>();
964 if (isa<UndefValue>(V))
965 dbgs() << "Unknown";
966 else
967 V->printAsOperand(dbgs());
968 }
969 else if (Itr.first.is<const PseudoSourceValue*>())
970 dbgs() << Itr.first.get<const PseudoSourceValue*>();
971 else
972 llvm_unreachable("Unknown Value type.");
973
974 dbgs() << " : ";
975 dumpSUList(Itr.second);
976 }
977}
978
Jonas Paulssonac29f012016-02-03 17:52:29 +0000979void ScheduleDAGInstrs::reduceHugeMemNodeMaps(Value2SUsMap &stores,
980 Value2SUsMap &loads, unsigned N) {
981 DEBUG(dbgs() << "Before reduction:\nStoring SUnits:\n";
982 stores.dump();
983 dbgs() << "Loading SUnits:\n";
984 loads.dump());
985
986 // Insert all SU's NodeNums into a vector and sort it.
987 std::vector<unsigned> NodeNums;
988 NodeNums.reserve(stores.size() + loads.size());
989 for (auto &I : stores)
990 for (auto *SU : I.second)
991 NodeNums.push_back(SU->NodeNum);
992 for (auto &I : loads)
993 for (auto *SU : I.second)
994 NodeNums.push_back(SU->NodeNum);
Mandeep Singh Grange92f0cf2018-04-06 18:08:42 +0000995 llvm::sort(NodeNums.begin(), NodeNums.end());
Jonas Paulssonac29f012016-02-03 17:52:29 +0000996
997 // The N last elements in NodeNums will be removed, and the SU with
998 // the lowest NodeNum of them will become the new BarrierChain to
999 // let the not yet seen SUs have a dependency to the removed SUs.
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001000 assert(N <= NodeNums.size());
Jonas Paulssonac29f012016-02-03 17:52:29 +00001001 SUnit *newBarrierChain = &SUnits[*(NodeNums.end() - N)];
1002 if (BarrierChain) {
1003 // The aliasing and non-aliasing maps reduce independently of each
1004 // other, but share a common BarrierChain. Check if the
1005 // newBarrierChain is above the former one. If it is not, it may
1006 // introduce a loop to use newBarrierChain, so keep the old one.
1007 if (newBarrierChain->NodeNum < BarrierChain->NodeNum) {
1008 BarrierChain->addPredBarrier(newBarrierChain);
1009 BarrierChain = newBarrierChain;
1010 DEBUG(dbgs() << "Inserting new barrier chain: SU("
1011 << BarrierChain->NodeNum << ").\n";);
1012 }
1013 else
1014 DEBUG(dbgs() << "Keeping old barrier chain: SU("
1015 << BarrierChain->NodeNum << ").\n";);
1016 }
1017 else
1018 BarrierChain = newBarrierChain;
1019
1020 insertBarrierChain(stores);
1021 insertBarrierChain(loads);
1022
1023 DEBUG(dbgs() << "After reduction:\nStoring SUnits:\n";
1024 stores.dump();
1025 dbgs() << "Loading SUnits:\n";
1026 loads.dump());
Dan Gohman60cb69e2008-11-19 23:18:57 +00001027}
1028
Matthias Braun868bbd42017-05-27 02:50:50 +00001029static void toggleKills(const MachineRegisterInfo &MRI, LivePhysRegs &LiveRegs,
1030 MachineInstr &MI, bool addToLiveRegs) {
1031 for (MachineOperand &MO : MI.operands()) {
1032 if (!MO.isReg() || !MO.readsReg())
1033 continue;
1034 unsigned Reg = MO.getReg();
1035 if (!Reg)
1036 continue;
Andrew Trick6b104f82013-12-28 21:56:55 +00001037
Matthias Braun868bbd42017-05-27 02:50:50 +00001038 // Things that are available after the instruction are killed by it.
1039 bool IsKill = LiveRegs.available(MRI, Reg);
1040 MO.setIsKill(IsKill);
Matthias Braune2ae0012017-06-27 00:58:48 +00001041 if (addToLiveRegs)
Matthias Braun868bbd42017-05-27 02:50:50 +00001042 LiveRegs.addReg(Reg);
Andrew Trick6b104f82013-12-28 21:56:55 +00001043 }
1044}
1045
Matthias Braun868bbd42017-05-27 02:50:50 +00001046void ScheduleDAGInstrs::fixupKills(MachineBasicBlock &MBB) {
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00001047 DEBUG(dbgs() << "Fixup kills for " << printMBBReference(MBB) << '\n');
Pete Cooper300069a2015-05-04 16:52:06 +00001048
Matthias Braun868bbd42017-05-27 02:50:50 +00001049 LiveRegs.init(*TRI);
1050 LiveRegs.addLiveOuts(MBB);
Andrew Trick6b104f82013-12-28 21:56:55 +00001051
1052 // Examine block from end to start...
Matthias Braun868bbd42017-05-27 02:50:50 +00001053 for (MachineInstr &MI : make_range(MBB.rbegin(), MBB.rend())) {
Duncan P. N. Exon Smithb77911b2016-07-01 16:21:48 +00001054 if (MI.isDebugValue())
Andrew Trick6b104f82013-12-28 21:56:55 +00001055 continue;
1056
1057 // Update liveness. Registers that are defed but not used in this
1058 // instruction are now dead. Mark register and all subregs as they
1059 // are completely defined.
Matthias Braun868bbd42017-05-27 02:50:50 +00001060 for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
1061 const MachineOperand &MO = *O;
1062 if (MO.isReg()) {
1063 if (!MO.isDef())
1064 continue;
1065 unsigned Reg = MO.getReg();
1066 if (!Reg)
1067 continue;
1068 LiveRegs.removeReg(Reg);
1069 } else if (MO.isRegMask()) {
1070 LiveRegs.removeRegsInMask(MO);
1071 }
Andrew Trick6b104f82013-12-28 21:56:55 +00001072 }
1073
Matthias Braun868bbd42017-05-27 02:50:50 +00001074 // If there is a bundle header fix it up first.
1075 if (!MI.isBundled()) {
1076 toggleKills(MRI, LiveRegs, MI, true);
1077 } else {
1078 MachineBasicBlock::instr_iterator First = MI.getIterator();
1079 if (MI.isBundle()) {
1080 toggleKills(MRI, LiveRegs, MI, false);
1081 ++First;
Andrew Trick6b104f82013-12-28 21:56:55 +00001082 }
Matthias Braun868bbd42017-05-27 02:50:50 +00001083 // Some targets make the (questionable) assumtion that the instructions
1084 // inside the bundle are ordered and consequently only the last use of
1085 // a register inside the bundle can kill it.
1086 MachineBasicBlock::instr_iterator I = std::next(First);
1087 while (I->isBundledWithSucc())
1088 ++I;
1089 do {
1090 if (!I->isDebugValue())
1091 toggleKills(MRI, LiveRegs, *I, true);
1092 --I;
1093 } while(I != First);
Andrew Trick6b104f82013-12-28 21:56:55 +00001094 }
1095 }
1096}
1097
Dan Gohman60cb69e2008-11-19 23:18:57 +00001098void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
Matthias Braun8c209aa2017-01-28 02:02:38 +00001099 // Cannot completely remove virtual function even in release mode.
Aaron Ballman615eb472017-10-15 14:32:27 +00001100#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Dan Gohman60cb69e2008-11-19 23:18:57 +00001101 SU->getInstr()->dump();
Manman Ren742534c2012-09-06 19:06:06 +00001102#endif
Dan Gohman60cb69e2008-11-19 23:18:57 +00001103}
1104
1105std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
Alp Tokere69170a2014-06-26 22:52:05 +00001106 std::string s;
1107 raw_string_ostream oss(s);
Dan Gohmanb9543432009-02-10 23:27:53 +00001108 if (SU == &EntrySU)
1109 oss << "<entry>";
1110 else if (SU == &ExitSU)
1111 oss << "<exit>";
1112 else
Eric Christopher1cdefae2015-02-27 00:11:34 +00001113 SU->getInstr()->print(oss, /*SkipOpers=*/true);
Dan Gohman60cb69e2008-11-19 23:18:57 +00001114 return oss.str();
1115}
1116
Andrew Trick1b2324d2012-03-07 00:18:22 +00001117/// Return the basic block label. It is not necessarilly unique because a block
1118/// contains multiple scheduling regions. But it is fine for visualization.
1119std::string ScheduleDAGInstrs::getDAGName() const {
1120 return "dag." + BB->getFullName();
1121}
Andrew Trick90f711d2012-10-15 18:02:27 +00001122
Andrew Trick48d392e2012-11-28 05:13:28 +00001123//===----------------------------------------------------------------------===//
1124// SchedDFSResult Implementation
1125//===----------------------------------------------------------------------===//
1126
1127namespace llvm {
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001128
Matthias Braunbd7d9182017-01-27 18:53:00 +00001129/// Internal state used to compute SchedDFSResult.
Andrew Trick48d392e2012-11-28 05:13:28 +00001130class SchedDFSImpl {
1131 SchedDFSResult &R;
1132
1133 /// Join DAG nodes into equivalence classes by their subtree.
1134 IntEqClasses SubtreeClasses;
1135 /// List PredSU, SuccSU pairs that represent data edges between subtrees.
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001136 std::vector<std::pair<const SUnit *, const SUnit*>> ConnectionPairs;
Andrew Trick48d392e2012-11-28 05:13:28 +00001137
Andrew Trickffc80972013-01-25 06:52:27 +00001138 struct RootData {
1139 unsigned NodeID;
Matthias Braunbd7d9182017-01-27 18:53:00 +00001140 unsigned ParentNodeID; ///< Parent node (member of the parent subtree).
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001141 unsigned SubInstrCount = 0; ///< Instr count in this tree only, not
1142 /// children.
Andrew Trickffc80972013-01-25 06:52:27 +00001143
1144 RootData(unsigned id): NodeID(id),
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001145 ParentNodeID(SchedDFSResult::InvalidSubtreeID) {}
Andrew Trickffc80972013-01-25 06:52:27 +00001146
1147 unsigned getSparseSetIndex() const { return NodeID; }
1148 };
1149
1150 SparseSet<RootData> RootSet;
1151
Andrew Trick48d392e2012-11-28 05:13:28 +00001152public:
Andrew Trickffc80972013-01-25 06:52:27 +00001153 SchedDFSImpl(SchedDFSResult &r): R(r), SubtreeClasses(R.DFSNodeData.size()) {
1154 RootSet.setUniverse(R.DFSNodeData.size());
1155 }
Andrew Trick48d392e2012-11-28 05:13:28 +00001156
Matthias Braunbd7d9182017-01-27 18:53:00 +00001157 /// Returns true if this node been visited by the DFS traversal.
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001158 ///
1159 /// During visitPostorderNode the Node's SubtreeID is assigned to the Node
1160 /// ID. Later, SubtreeID is updated but remains valid.
Andrew Trick48d392e2012-11-28 05:13:28 +00001161 bool isVisited(const SUnit *SU) const {
Andrew Trickffc80972013-01-25 06:52:27 +00001162 return R.DFSNodeData[SU->NodeNum].SubtreeID
1163 != SchedDFSResult::InvalidSubtreeID;
Andrew Trick48d392e2012-11-28 05:13:28 +00001164 }
1165
Matthias Braunbd7d9182017-01-27 18:53:00 +00001166 /// Initializes this node's instruction count. We don't need to flag the node
Andrew Trick48d392e2012-11-28 05:13:28 +00001167 /// visited until visitPostorder because the DAG cannot have cycles.
1168 void visitPreorder(const SUnit *SU) {
Andrew Trickffc80972013-01-25 06:52:27 +00001169 R.DFSNodeData[SU->NodeNum].InstrCount =
1170 SU->getInstr()->isTransient() ? 0 : 1;
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001171 }
1172
1173 /// Called once for each node after all predecessors are visited. Revisit this
1174 /// node's predecessors and potentially join them now that we know the ILP of
1175 /// the other predecessors.
1176 void visitPostorderNode(const SUnit *SU) {
1177 // Mark this node as the root of a subtree. It may be joined with its
1178 // successors later.
Andrew Trickffc80972013-01-25 06:52:27 +00001179 R.DFSNodeData[SU->NodeNum].SubtreeID = SU->NodeNum;
1180 RootData RData(SU->NodeNum);
1181 RData.SubInstrCount = SU->getInstr()->isTransient() ? 0 : 1;
Andrew Trick48d392e2012-11-28 05:13:28 +00001182
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001183 // If any predecessors are still in their own subtree, they either cannot be
1184 // joined or are large enough to remain separate. If this parent node's
1185 // total instruction count is not greater than a child subtree by at least
1186 // the subtree limit, then try to join it now since splitting subtrees is
1187 // only useful if multiple high-pressure paths are possible.
Andrew Trickffc80972013-01-25 06:52:27 +00001188 unsigned InstrCount = R.DFSNodeData[SU->NodeNum].InstrCount;
Matthias Braun298e0072016-09-30 23:08:07 +00001189 for (const SDep &PredDep : SU->Preds) {
1190 if (PredDep.getKind() != SDep::Data)
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001191 continue;
Matthias Braun298e0072016-09-30 23:08:07 +00001192 unsigned PredNum = PredDep.getSUnit()->NodeNum;
Andrew Trickffc80972013-01-25 06:52:27 +00001193 if ((InstrCount - R.DFSNodeData[PredNum].InstrCount) < R.SubtreeLimit)
Matthias Braun298e0072016-09-30 23:08:07 +00001194 joinPredSubtree(PredDep, SU, /*CheckLimit=*/false);
Andrew Trickffc80972013-01-25 06:52:27 +00001195
1196 // Either link or merge the TreeData entry from the child to the parent.
Andrew Trick646eeb62013-01-25 06:52:30 +00001197 if (R.DFSNodeData[PredNum].SubtreeID == PredNum) {
1198 // If the predecessor's parent is invalid, this is a tree edge and the
1199 // current node is the parent.
1200 if (RootSet[PredNum].ParentNodeID == SchedDFSResult::InvalidSubtreeID)
1201 RootSet[PredNum].ParentNodeID = SU->NodeNum;
1202 }
1203 else if (RootSet.count(PredNum)) {
1204 // The predecessor is not a root, but is still in the root set. This
1205 // must be the new parent that it was just joined to. Note that
1206 // RootSet[PredNum].ParentNodeID may either be invalid or may still be
1207 // set to the original parent.
Andrew Trickffc80972013-01-25 06:52:27 +00001208 RData.SubInstrCount += RootSet[PredNum].SubInstrCount;
1209 RootSet.erase(PredNum);
1210 }
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001211 }
Andrew Trickffc80972013-01-25 06:52:27 +00001212 RootSet[SU->NodeNum] = RData;
1213 }
1214
Matthias Braunbd7d9182017-01-27 18:53:00 +00001215 /// \brief Called once for each tree edge after calling visitPostOrderNode on
1216 /// the predecessor. Increment the parent node's instruction count and
Andrew Trickffc80972013-01-25 06:52:27 +00001217 /// preemptively join this subtree to its parent's if it is small enough.
1218 void visitPostorderEdge(const SDep &PredDep, const SUnit *Succ) {
1219 R.DFSNodeData[Succ->NodeNum].InstrCount
1220 += R.DFSNodeData[PredDep.getSUnit()->NodeNum].InstrCount;
1221 joinPredSubtree(PredDep, Succ);
Andrew Trick48d392e2012-11-28 05:13:28 +00001222 }
1223
Matthias Braunbd7d9182017-01-27 18:53:00 +00001224 /// Adds a connection for cross edges.
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001225 void visitCrossEdge(const SDep &PredDep, const SUnit *Succ) {
Andrew Trick48d392e2012-11-28 05:13:28 +00001226 ConnectionPairs.push_back(std::make_pair(PredDep.getSUnit(), Succ));
1227 }
1228
Matthias Braunbd7d9182017-01-27 18:53:00 +00001229 /// Sets each node's subtree ID to the representative ID and record
1230 /// connections between trees.
Andrew Trick48d392e2012-11-28 05:13:28 +00001231 void finalize() {
1232 SubtreeClasses.compress();
Andrew Trickffc80972013-01-25 06:52:27 +00001233 R.DFSTreeData.resize(SubtreeClasses.getNumClasses());
1234 assert(SubtreeClasses.getNumClasses() == RootSet.size()
1235 && "number of roots should match trees");
Matthias Braun298e0072016-09-30 23:08:07 +00001236 for (const RootData &Root : RootSet) {
1237 unsigned TreeID = SubtreeClasses[Root.NodeID];
1238 if (Root.ParentNodeID != SchedDFSResult::InvalidSubtreeID)
1239 R.DFSTreeData[TreeID].ParentTreeID = SubtreeClasses[Root.ParentNodeID];
1240 R.DFSTreeData[TreeID].SubInstrCount = Root.SubInstrCount;
Andrew Trick646eeb62013-01-25 06:52:30 +00001241 // Note that SubInstrCount may be greater than InstrCount if we joined
1242 // subtrees across a cross edge. InstrCount will be attributed to the
1243 // original parent, while SubInstrCount will be attributed to the joined
1244 // parent.
Andrew Trickffc80972013-01-25 06:52:27 +00001245 }
Andrew Trick48d392e2012-11-28 05:13:28 +00001246 R.SubtreeConnections.resize(SubtreeClasses.getNumClasses());
1247 R.SubtreeConnectLevels.resize(SubtreeClasses.getNumClasses());
1248 DEBUG(dbgs() << R.getNumSubtrees() << " subtrees:\n");
Andrew Trickffc80972013-01-25 06:52:27 +00001249 for (unsigned Idx = 0, End = R.DFSNodeData.size(); Idx != End; ++Idx) {
1250 R.DFSNodeData[Idx].SubtreeID = SubtreeClasses[Idx];
Andrew Trick48d392e2012-11-28 05:13:28 +00001251 DEBUG(dbgs() << " SU(" << Idx << ") in tree "
Andrew Trickffc80972013-01-25 06:52:27 +00001252 << R.DFSNodeData[Idx].SubtreeID << '\n');
Andrew Trick48d392e2012-11-28 05:13:28 +00001253 }
Matthias Braun298e0072016-09-30 23:08:07 +00001254 for (const std::pair<const SUnit*, const SUnit*> &P : ConnectionPairs) {
1255 unsigned PredTree = SubtreeClasses[P.first->NodeNum];
1256 unsigned SuccTree = SubtreeClasses[P.second->NodeNum];
Andrew Trick48d392e2012-11-28 05:13:28 +00001257 if (PredTree == SuccTree)
1258 continue;
Matthias Braun298e0072016-09-30 23:08:07 +00001259 unsigned Depth = P.first->getDepth();
Andrew Trick48d392e2012-11-28 05:13:28 +00001260 addConnection(PredTree, SuccTree, Depth);
1261 addConnection(SuccTree, PredTree, Depth);
1262 }
1263 }
1264
1265protected:
Matthias Braunbd7d9182017-01-27 18:53:00 +00001266 /// Joins the predecessor subtree with the successor that is its DFS parent.
1267 /// Applies some heuristics before joining.
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001268 bool joinPredSubtree(const SDep &PredDep, const SUnit *Succ,
1269 bool CheckLimit = true) {
1270 assert(PredDep.getKind() == SDep::Data && "Subtrees are for data edges");
1271
1272 // Check if the predecessor is already joined.
1273 const SUnit *PredSU = PredDep.getSUnit();
1274 unsigned PredNum = PredSU->NodeNum;
Andrew Trickffc80972013-01-25 06:52:27 +00001275 if (R.DFSNodeData[PredNum].SubtreeID != PredNum)
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001276 return false;
Andrew Trickb52a8562013-01-25 00:12:57 +00001277
1278 // Four is the magic number of successors before a node is considered a
1279 // pinch point.
1280 unsigned NumDataSucs = 0;
Matthias Braun298e0072016-09-30 23:08:07 +00001281 for (const SDep &SuccDep : PredSU->Succs) {
1282 if (SuccDep.getKind() == SDep::Data) {
Andrew Trickb52a8562013-01-25 00:12:57 +00001283 if (++NumDataSucs >= 4)
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001284 return false;
Andrew Trickb52a8562013-01-25 00:12:57 +00001285 }
1286 }
Andrew Trickffc80972013-01-25 06:52:27 +00001287 if (CheckLimit && R.DFSNodeData[PredNum].InstrCount > R.SubtreeLimit)
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001288 return false;
Andrew Trickffc80972013-01-25 06:52:27 +00001289 R.DFSNodeData[PredNum].SubtreeID = Succ->NodeNum;
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001290 SubtreeClasses.join(Succ->NodeNum, PredNum);
1291 return true;
Andrew Trickb52a8562013-01-25 00:12:57 +00001292 }
1293
Andrew Trick48d392e2012-11-28 05:13:28 +00001294 /// Called by finalize() to record a connection between trees.
1295 void addConnection(unsigned FromTree, unsigned ToTree, unsigned Depth) {
1296 if (!Depth)
1297 return;
1298
Andrew Trickffc80972013-01-25 06:52:27 +00001299 do {
1300 SmallVectorImpl<SchedDFSResult::Connection> &Connections =
1301 R.SubtreeConnections[FromTree];
Matthias Braun298e0072016-09-30 23:08:07 +00001302 for (SchedDFSResult::Connection &C : Connections) {
1303 if (C.TreeID == ToTree) {
1304 C.Level = std::max(C.Level, Depth);
Andrew Trickffc80972013-01-25 06:52:27 +00001305 return;
1306 }
Andrew Trick48d392e2012-11-28 05:13:28 +00001307 }
Andrew Trickffc80972013-01-25 06:52:27 +00001308 Connections.push_back(SchedDFSResult::Connection(ToTree, Depth));
1309 FromTree = R.DFSTreeData[FromTree].ParentTreeID;
1310 } while (FromTree != SchedDFSResult::InvalidSubtreeID);
Andrew Trick48d392e2012-11-28 05:13:28 +00001311 }
1312};
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001313
Matthias Braunbd7d9182017-01-27 18:53:00 +00001314} // end namespace llvm
Andrew Trick48d392e2012-11-28 05:13:28 +00001315
Andrew Trick90f711d2012-10-15 18:02:27 +00001316namespace {
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001317
Matthias Braunbd7d9182017-01-27 18:53:00 +00001318/// Manage the stack used by a reverse depth-first search over the DAG.
Andrew Trick90f711d2012-10-15 18:02:27 +00001319class SchedDAGReverseDFS {
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001320 std::vector<std::pair<const SUnit *, SUnit::const_pred_iterator>> DFSStack;
1321
Andrew Trick90f711d2012-10-15 18:02:27 +00001322public:
1323 bool isComplete() const { return DFSStack.empty(); }
1324
1325 void follow(const SUnit *SU) {
1326 DFSStack.push_back(std::make_pair(SU, SU->Preds.begin()));
1327 }
1328 void advance() { ++DFSStack.back().second; }
1329
Andrew Trick48d392e2012-11-28 05:13:28 +00001330 const SDep *backtrack() {
1331 DFSStack.pop_back();
Craig Topperc0196b12014-04-14 00:51:57 +00001332 return DFSStack.empty() ? nullptr : std::prev(DFSStack.back().second);
Andrew Trick48d392e2012-11-28 05:13:28 +00001333 }
Andrew Trick90f711d2012-10-15 18:02:27 +00001334
1335 const SUnit *getCurr() const { return DFSStack.back().first; }
1336
1337 SUnit::const_pred_iterator getPred() const { return DFSStack.back().second; }
1338
1339 SUnit::const_pred_iterator getPredEnd() const {
1340 return getCurr()->Preds.end();
1341 }
1342};
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001343
1344} // end anonymous namespace
Andrew Trick90f711d2012-10-15 18:02:27 +00001345
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001346static bool hasDataSucc(const SUnit *SU) {
Matthias Braun298e0072016-09-30 23:08:07 +00001347 for (const SDep &SuccDep : SU->Succs) {
1348 if (SuccDep.getKind() == SDep::Data &&
1349 !SuccDep.getSUnit()->isBoundaryNode())
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001350 return true;
1351 }
1352 return false;
1353}
1354
Matthias Braunbd7d9182017-01-27 18:53:00 +00001355/// Computes an ILP metric for all nodes in the subDAG reachable via depth-first
Andrew Trick90f711d2012-10-15 18:02:27 +00001356/// search from this root.
Andrew Tricke2c3f5c2013-01-25 06:33:57 +00001357void SchedDFSResult::compute(ArrayRef<SUnit> SUnits) {
Andrew Trick90f711d2012-10-15 18:02:27 +00001358 if (!IsBottomUp)
Eric Christopher52854dc2017-08-03 22:41:12 +00001359 llvm_unreachable("Top-down ILP metric is unimplemented");
Andrew Trick90f711d2012-10-15 18:02:27 +00001360
Andrew Trick48d392e2012-11-28 05:13:28 +00001361 SchedDFSImpl Impl(*this);
Matthias Braun298e0072016-09-30 23:08:07 +00001362 for (const SUnit &SU : SUnits) {
1363 if (Impl.isVisited(&SU) || hasDataSucc(&SU))
Andrew Tricke2c3f5c2013-01-25 06:33:57 +00001364 continue;
1365
Andrew Trick48d392e2012-11-28 05:13:28 +00001366 SchedDAGReverseDFS DFS;
Matthias Braun298e0072016-09-30 23:08:07 +00001367 Impl.visitPreorder(&SU);
1368 DFS.follow(&SU);
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001369 while (true) {
Andrew Trick48d392e2012-11-28 05:13:28 +00001370 // Traverse the leftmost path as far as possible.
1371 while (DFS.getPred() != DFS.getPredEnd()) {
1372 const SDep &PredDep = *DFS.getPred();
1373 DFS.advance();
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001374 // Ignore non-data edges.
Andrew Trick646eeb62013-01-25 06:52:30 +00001375 if (PredDep.getKind() != SDep::Data
1376 || PredDep.getSUnit()->isBoundaryNode()) {
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001377 continue;
Andrew Trick646eeb62013-01-25 06:52:30 +00001378 }
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001379 // An already visited edge is a cross edge, assuming an acyclic DAG.
Andrew Trick48d392e2012-11-28 05:13:28 +00001380 if (Impl.isVisited(PredDep.getSUnit())) {
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001381 Impl.visitCrossEdge(PredDep, DFS.getCurr());
Andrew Trick48d392e2012-11-28 05:13:28 +00001382 continue;
1383 }
1384 Impl.visitPreorder(PredDep.getSUnit());
1385 DFS.follow(PredDep.getSUnit());
1386 }
1387 // Visit the top of the stack in postorder and backtrack.
1388 const SUnit *Child = DFS.getCurr();
1389 const SDep *PredDep = DFS.backtrack();
Andrew Trick5b07eeb2013-01-25 06:02:44 +00001390 Impl.visitPostorderNode(Child);
1391 if (PredDep)
1392 Impl.visitPostorderEdge(*PredDep, DFS.getCurr());
Andrew Trick48d392e2012-11-28 05:13:28 +00001393 if (DFS.isComplete())
1394 break;
Andrew Trick90f711d2012-10-15 18:02:27 +00001395 }
Andrew Trick48d392e2012-11-28 05:13:28 +00001396 }
1397 Impl.finalize();
1398}
1399
1400/// The root of the given SubtreeID was just scheduled. For all subtrees
1401/// connected to this tree, record the depth of the connection so that the
1402/// nearest connected subtrees can be prioritized.
1403void SchedDFSResult::scheduleTree(unsigned SubtreeID) {
Matthias Braun298e0072016-09-30 23:08:07 +00001404 for (const Connection &C : SubtreeConnections[SubtreeID]) {
1405 SubtreeConnectLevels[C.TreeID] =
1406 std::max(SubtreeConnectLevels[C.TreeID], C.Level);
1407 DEBUG(dbgs() << " Tree: " << C.TreeID
1408 << " @" << SubtreeConnectLevels[C.TreeID] << '\n');
Andrew Trick90f711d2012-10-15 18:02:27 +00001409 }
1410}
1411
Aaron Ballman615eb472017-10-15 14:32:27 +00001412#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braun8c209aa2017-01-28 02:02:38 +00001413LLVM_DUMP_METHOD void ILPValue::print(raw_ostream &OS) const {
Andrew Trick48d392e2012-11-28 05:13:28 +00001414 OS << InstrCount << " / " << Length << " = ";
1415 if (!Length)
Andrew Trick90f711d2012-10-15 18:02:27 +00001416 OS << "BADILP";
Andrew Trick48d392e2012-11-28 05:13:28 +00001417 else
1418 OS << format("%g", ((double)InstrCount / Length));
Andrew Trick90f711d2012-10-15 18:02:27 +00001419}
1420
Matthias Braun8c209aa2017-01-28 02:02:38 +00001421LLVM_DUMP_METHOD void ILPValue::dump() const {
Andrew Trick90f711d2012-10-15 18:02:27 +00001422 dbgs() << *this << '\n';
1423}
1424
1425namespace llvm {
1426
Alp Tokerd8d510a2014-07-01 21:19:13 +00001427LLVM_DUMP_METHOD
Andrew Trick90f711d2012-10-15 18:02:27 +00001428raw_ostream &operator<<(raw_ostream &OS, const ILPValue &Val) {
1429 Val.print(OS);
1430 return OS;
1431}
1432
Matthias Braunbd7d9182017-01-27 18:53:00 +00001433} // end namespace llvm
Eugene Zelenko7ea69232017-06-01 23:25:02 +00001434
Matthias Braun8c209aa2017-01-28 02:02:38 +00001435#endif