Dan Gohman | a629b48 | 2008-12-08 17:50:35 +0000 | [diff] [blame] | 1 | //===---- ScheduleDAGInstrs.cpp - MachineInstr Rescheduling ---------------===// |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 2 | // |
| 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 Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 10 | /// \file This implements the ScheduleDAGInstrs class, which implements |
| 11 | /// re-scheduling of MachineInstrs. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/ScheduleDAGInstrs.h" |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/IntEqClasses.h" |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/MapVector.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallPtrSet.h" |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallVector.h" |
| 20 | #include "llvm/ADT/SparseSet.h" |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/iterator_range.h" |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/AliasAnalysis.h" |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/ValueTracking.h" |
Matthias Braun | fa621d2 | 2017-12-13 02:51:04 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/LiveIntervals.h" |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/LivePhysRegs.h" |
| 26 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Arnold Schwaighofer | 75e36e8 | 2015-05-08 23:52:00 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineFunction.h" |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineInstr.h" |
| 30 | #include "llvm/CodeGen/MachineInstrBundle.h" |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineMemOperand.h" |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineOperand.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Andrew Trick | afc2657 | 2012-06-06 19:47:35 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/RegisterPressure.h" |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/ScheduleDAG.h" |
Andrew Trick | 53e98a2 | 2012-11-28 05:13:24 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/ScheduleDFS.h" |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/SlotIndexes.h" |
David Blaikie | e3a9b4c | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 40 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Nico Weber | 0f38c60 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 41 | #include "llvm/Config/llvm-config.h" |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 42 | #include "llvm/IR/Constants.h" |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 43 | #include "llvm/IR/Function.h" |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 44 | #include "llvm/IR/Instruction.h" |
| 45 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 46 | #include "llvm/IR/Operator.h" |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 47 | #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 Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 52 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 53 | #include "llvm/Support/Compiler.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 54 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 55 | #include "llvm/Support/ErrorHandling.h" |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 56 | #include "llvm/Support/Format.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 57 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 58 | #include <algorithm> |
| 59 | #include <cassert> |
| 60 | #include <iterator> |
| 61 | #include <string> |
| 62 | #include <utility> |
| 63 | #include <vector> |
Andrew Trick | ea57433 | 2013-08-23 17:48:43 +0000 | [diff] [blame] | 64 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 65 | using namespace llvm; |
| 66 | |
Evandro Menezes | fdda7ea | 2017-07-11 22:08:28 +0000 | [diff] [blame] | 67 | #define DEBUG_TYPE "machine-scheduler" |
Chandler Carruth | 8677f2f | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 68 | |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 69 | static cl::opt<bool> EnableAASchedMI("enable-aa-sched-mi", cl::Hidden, |
| 70 | cl::ZeroOrMore, cl::init(false), |
Jonas Paulsson | 650c3a4 | 2015-01-07 13:20:57 +0000 | [diff] [blame] | 71 | cl::desc("Enable use of AA during MI DAG construction")); |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 72 | |
Hal Finkel | 397cd32 | 2014-01-25 19:24:54 +0000 | [diff] [blame] | 73 | static cl::opt<bool> UseTBAA("use-tbaa-in-sched-mi", cl::Hidden, |
Jonas Paulsson | 650c3a4 | 2015-01-07 13:20:57 +0000 | [diff] [blame] | 74 | cl::init(true), cl::desc("Enable use of TBAA during MI DAG construction")); |
Hal Finkel | 397cd32 | 2014-01-25 19:24:54 +0000 | [diff] [blame] | 75 | |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 76 | // 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. |
| 82 | static 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 Amini | d7628da | 2016-04-16 04:58:30 +0000 | [diff] [blame] | 87 | static cl::opt<unsigned> ReductionSize( |
| 88 | "dag-maps-reduction-size", cl::Hidden, |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 89 | cl::desc("A huge scheduling region will have maps reduced by this many " |
Mehdi Amini | d7628da | 2016-04-16 04:58:30 +0000 | [diff] [blame] | 90 | "nodes at a time. Defaults to HugeRegion / 2.")); |
| 91 | |
| 92 | static 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 Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 99 | |
| 100 | static void dumpSUList(ScheduleDAGInstrs::SUList &L) { |
Aaron Ballman | 1d03d38 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 101 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 102 | dbgs() << "{ "; |
Matthias Braun | a179dcf | 2016-09-30 23:08:07 +0000 | [diff] [blame] | 103 | for (const SUnit *su : L) { |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 104 | dbgs() << "SU(" << su->NodeNum << ")"; |
| 105 | if (su != L.back()) |
| 106 | dbgs() << ", "; |
| 107 | } |
| 108 | dbgs() << "}\n"; |
| 109 | #endif |
| 110 | } |
| 111 | |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 112 | ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf, |
Alexey Samsonov | ada5f2a | 2014-08-20 19:36:05 +0000 | [diff] [blame] | 113 | const MachineLoopInfo *mli, |
Matthias Braun | e9564b2 | 2015-11-03 01:53:29 +0000 | [diff] [blame] | 114 | bool RemoveKillFlags) |
Matthias Braun | c57a70c | 2015-12-04 19:54:24 +0000 | [diff] [blame] | 115 | : ScheduleDAG(mf), MLI(mli), MFI(mf.getFrameInfo()), |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 116 | RemoveKillFlags(RemoveKillFlags), |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 117 | UnknownValue(UndefValue::get( |
Matthias Braun | d318139 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 118 | Type::getVoidTy(mf.getFunction().getContext()))) { |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 119 | DbgValues.clear(); |
Andrew Trick | 781ab47 | 2012-09-18 18:20:00 +0000 | [diff] [blame] | 120 | |
Eric Christopher | fd0f792 | 2015-01-27 07:54:39 +0000 | [diff] [blame] | 121 | const TargetSubtargetInfo &ST = mf.getSubtarget(); |
Sanjay Patel | e599cea | 2018-04-08 19:56:04 +0000 | [diff] [blame] | 122 | SchedModel.init(&ST); |
Evan Cheng | 38bdfc6 | 2009-10-18 19:58:47 +0000 | [diff] [blame] | 123 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 124 | |
Hiroshi Inoue | 0f48afc | 2017-10-12 06:26:04 +0000 | [diff] [blame] | 125 | /// 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. |
| 129 | static bool getUnderlyingObjectsForInstr(const MachineInstr *MI, |
Matthias Braun | f79c57a | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 130 | const MachineFrameInfo &MFI, |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 131 | UnderlyingObjectsVector &Objects, |
| 132 | const DataLayout &DL) { |
Geoff Berry | 1a49060 | 2016-04-14 21:31:07 +0000 | [diff] [blame] | 133 | auto allMMOsOkay = [&]() { |
| 134 | for (const MachineMemOperand *MMO : MI->memoperands()) { |
| 135 | if (MMO->isVolatile()) |
| 136 | return false; |
Hal Finkel | f218310 | 2012-12-10 18:49:16 +0000 | [diff] [blame] | 137 | |
Geoff Berry | 1a49060 | 2016-04-14 21:31:07 +0000 | [diff] [blame] | 138 | 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 Braun | f79c57a | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 144 | if (MFI.hasTailCall()) |
Geoff Berry | 1a49060 | 2016-04-14 21:31:07 +0000 | [diff] [blame] | 145 | return false; |
Geoff Berry | 3c28f60 | 2016-04-12 15:50:19 +0000 | [diff] [blame] | 146 | |
Geoff Berry | 1a49060 | 2016-04-14 21:31:07 +0000 | [diff] [blame] | 147 | // 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 Braun | f79c57a | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 150 | if (PSV->isAliased(&MFI)) |
Geoff Berry | 1a49060 | 2016-04-14 21:31:07 +0000 | [diff] [blame] | 151 | return false; |
Geoff Berry | 3c28f60 | 2016-04-12 15:50:19 +0000 | [diff] [blame] | 152 | |
Matthias Braun | f79c57a | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 153 | bool MayAlias = PSV->mayAlias(&MFI); |
Geoff Berry | 1a49060 | 2016-04-14 21:31:07 +0000 | [diff] [blame] | 154 | Objects.push_back(UnderlyingObjectsVector::value_type(PSV, MayAlias)); |
| 155 | } else if (const Value *V = MMO->getValue()) { |
| 156 | SmallVector<Value *, 4> Objs; |
Hiroshi Inoue | 0f48afc | 2017-10-12 06:26:04 +0000 | [diff] [blame] | 157 | if (!getUnderlyingObjectsForCodeGen(V, Objs, DL)) |
| 158 | return false; |
Geoff Berry | 3c28f60 | 2016-04-12 15:50:19 +0000 | [diff] [blame] | 159 | |
Geoff Berry | 1a49060 | 2016-04-14 21:31:07 +0000 | [diff] [blame] | 160 | for (Value *V : Objs) { |
Hiroshi Inoue | eeca49d | 2017-08-01 03:32:15 +0000 | [diff] [blame] | 161 | assert(isIdentifiedObject(V)); |
Geoff Berry | 1a49060 | 2016-04-14 21:31:07 +0000 | [diff] [blame] | 162 | Objects.push_back(UnderlyingObjectsVector::value_type(V, true)); |
Geoff Berry | 3c28f60 | 2016-04-12 15:50:19 +0000 | [diff] [blame] | 163 | } |
Geoff Berry | 1a49060 | 2016-04-14 21:31:07 +0000 | [diff] [blame] | 164 | } else |
| 165 | return false; |
Geoff Berry | 3c28f60 | 2016-04-12 15:50:19 +0000 | [diff] [blame] | 166 | } |
Geoff Berry | 1a49060 | 2016-04-14 21:31:07 +0000 | [diff] [blame] | 167 | return true; |
| 168 | }; |
| 169 | |
Hiroshi Inoue | 0f48afc | 2017-10-12 06:26:04 +0000 | [diff] [blame] | 170 | if (!allMMOsOkay()) { |
Geoff Berry | 1a49060 | 2016-04-14 21:31:07 +0000 | [diff] [blame] | 171 | Objects.clear(); |
Hiroshi Inoue | 0f48afc | 2017-10-12 06:26:04 +0000 | [diff] [blame] | 172 | return false; |
| 173 | } |
| 174 | |
| 175 | return true; |
Dan Gohman | 3311a1f | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Andrew Trick | 918f38a | 2012-04-20 20:05:21 +0000 | [diff] [blame] | 178 | void ScheduleDAGInstrs::startBlock(MachineBasicBlock *bb) { |
| 179 | BB = bb; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 182 | void ScheduleDAGInstrs::finishBlock() { |
Andrew Trick | a30444a | 2012-04-20 20:24:33 +0000 | [diff] [blame] | 183 | // Subclasses should no longer refer to the old block. |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 184 | BB = nullptr; |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 187 | void ScheduleDAGInstrs::enterRegion(MachineBasicBlock *bb, |
| 188 | MachineBasicBlock::iterator begin, |
| 189 | MachineBasicBlock::iterator end, |
Andrew Trick | d2763f6 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 190 | unsigned regioninstrs) { |
Andrew Trick | 918f38a | 2012-04-20 20:05:21 +0000 | [diff] [blame] | 191 | assert(bb == BB && "startBlock should set BB"); |
Andrew Trick | 68675c6 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 192 | RegionBegin = begin; |
| 193 | RegionEnd = end; |
Andrew Trick | d2763f6 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 194 | NumRegionInstrs = regioninstrs; |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 197 | void ScheduleDAGInstrs::exitRegion() { |
| 198 | // Nothing to do. |
| 199 | } |
| 200 | |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 201 | void ScheduleDAGInstrs::addSchedBarrierDeps() { |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 202 | MachineInstr *ExitMI = RegionEnd != BB->end() ? &*RegionEnd : nullptr; |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 203 | ExitSU.setInstr(ExitMI); |
Matthias Braun | ee5205b | 2016-11-11 01:34:21 +0000 | [diff] [blame] | 204 | // Add dependencies on the defs and uses of the instruction. |
| 205 | if (ExitMI) { |
Matthias Braun | a179dcf | 2016-09-30 23:08:07 +0000 | [diff] [blame] | 206 | for (const MachineOperand &MO : ExitMI->operands()) { |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 207 | if (!MO.isReg() || MO.isDef()) continue; |
| 208 | unsigned Reg = MO.getReg(); |
Matthias Braun | fc1b3b3 | 2016-11-10 22:11:00 +0000 | [diff] [blame] | 209 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Michael Ilseman | afe77f3 | 2013-01-21 18:18:53 +0000 | [diff] [blame] | 210 | Uses.insert(PhysRegSUOper(&ExitSU, -1, Reg)); |
Matthias Braun | fc1b3b3 | 2016-11-10 22:11:00 +0000 | [diff] [blame] | 211 | } else if (TargetRegisterInfo::isVirtualRegister(Reg) && MO.readsReg()) { |
Matthias Braun | a179dcf | 2016-09-30 23:08:07 +0000 | [diff] [blame] | 212 | addVRegUseDeps(&ExitSU, ExitMI->getOperandNo(&MO)); |
Matthias Braun | fc1b3b3 | 2016-11-10 22:11:00 +0000 | [diff] [blame] | 213 | } |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 214 | } |
Matthias Braun | ee5205b | 2016-11-11 01:34:21 +0000 | [diff] [blame] | 215 | } |
| 216 | if (!ExitMI || (!ExitMI->isCall() && !ExitMI->isBarrier())) { |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 217 | // For others, e.g. fallthrough, conditional branch, assume the exit |
Evan Cheng | de5fa93 | 2010-10-27 23:17:17 +0000 | [diff] [blame] | 218 | // uses all the registers that are livein to the successor blocks. |
Matthias Braun | a179dcf | 2016-09-30 23:08:07 +0000 | [diff] [blame] | 219 | for (const MachineBasicBlock *Succ : BB->successors()) { |
| 220 | for (const auto &LI : Succ->liveins()) { |
Matthias Braun | af5ff60 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 221 | if (!Uses.contains(LI.PhysReg)) |
| 222 | Uses.insert(PhysRegSUOper(&ExitSU, -1, LI.PhysReg)); |
Evan Cheng | de5fa93 | 2010-10-27 23:17:17 +0000 | [diff] [blame] | 223 | } |
Matthias Braun | a179dcf | 2016-09-30 23:08:07 +0000 | [diff] [blame] | 224 | } |
Evan Cheng | ec6906b | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Matthias Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 228 | /// MO is an operand of SU's instruction that defines a physical register. Adds |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 229 | /// data dependencies from SU to any uses of the physical register. |
Andrew Trick | ffd2526 | 2012-08-23 00:39:43 +0000 | [diff] [blame] | 230 | void ScheduleDAGInstrs::addPhysRegDataDeps(SUnit *SU, unsigned OperIdx) { |
| 231 | const MachineOperand &MO = SU->getInstr()->getOperand(OperIdx); |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 232 | assert(MO.isDef() && "expect physreg def"); |
| 233 | |
| 234 | // Ask the target if address-backscheduling is desirable, and if so how much. |
Eric Christopher | fd0f792 | 2015-01-27 07:54:39 +0000 | [diff] [blame] | 235 | const TargetSubtargetInfo &ST = MF.getSubtarget(); |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 236 | |
Jonas Paulsson | 78f5683 | 2018-10-30 15:04:40 +0000 | [diff] [blame] | 237 | // Only use any non-zero latency for real defs/uses, in contrast to |
| 238 | // "fake" operands added by regalloc. |
| 239 | const MCInstrDesc *DefMIDesc = &SU->getInstr()->getDesc(); |
| 240 | bool ImplicitPseudoDef = (OperIdx >= DefMIDesc->getNumOperands() && |
| 241 | !DefMIDesc->hasImplicitDefOfPhysReg(MO.getReg())); |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 242 | for (MCRegAliasIterator Alias(MO.getReg(), TRI, true); |
| 243 | Alias.isValid(); ++Alias) { |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 244 | if (!Uses.contains(*Alias)) |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 245 | continue; |
Michael Ilseman | afe77f3 | 2013-01-21 18:18:53 +0000 | [diff] [blame] | 246 | for (Reg2SUnitsMap::iterator I = Uses.find(*Alias); I != Uses.end(); ++I) { |
| 247 | SUnit *UseSU = I->SU; |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 248 | if (UseSU == SU) |
| 249 | continue; |
Andrew Trick | 39817f9 | 2012-10-08 18:54:00 +0000 | [diff] [blame] | 250 | |
Andrew Trick | 39817f9 | 2012-10-08 18:54:00 +0000 | [diff] [blame] | 251 | // Adjust the dependence latency using operand def/use information, |
| 252 | // then allow the target to perform its own adjustments. |
Michael Ilseman | afe77f3 | 2013-01-21 18:18:53 +0000 | [diff] [blame] | 253 | int UseOp = I->OpIdx; |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 254 | MachineInstr *RegUse = nullptr; |
Andrew Trick | ae692f2 | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 255 | SDep Dep; |
| 256 | if (UseOp < 0) |
| 257 | Dep = SDep(SU, SDep::Artificial); |
| 258 | else { |
Andrew Trick | 4392f0f | 2013-04-13 06:07:40 +0000 | [diff] [blame] | 259 | // Set the hasPhysRegDefs only for physreg defs that have a use within |
| 260 | // the scheduling region. |
| 261 | SU->hasPhysRegDefs = true; |
Andrew Trick | ae692f2 | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 262 | Dep = SDep(SU, SDep::Data, *Alias); |
| 263 | RegUse = UseSU->getInstr(); |
Andrew Trick | ae692f2 | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 264 | } |
Jonas Paulsson | 78f5683 | 2018-10-30 15:04:40 +0000 | [diff] [blame] | 265 | const MCInstrDesc *UseMIDesc = |
| 266 | (RegUse ? &UseSU->getInstr()->getDesc() : nullptr); |
| 267 | bool ImplicitPseudoUse = |
| 268 | (UseMIDesc && UseOp >= ((int)UseMIDesc->getNumOperands()) && |
| 269 | !UseMIDesc->hasImplicitUseOfPhysReg(*Alias)); |
| 270 | if (!ImplicitPseudoDef && !ImplicitPseudoUse) { |
| 271 | Dep.setLatency(SchedModel.computeOperandLatency(SU->getInstr(), OperIdx, |
| 272 | RegUse, UseOp)); |
| 273 | ST.adjustSchedDependency(SU, UseSU, Dep); |
| 274 | } else |
| 275 | Dep.setLatency(0); |
Andrew Trick | b7e0289 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 276 | |
Andrew Trick | ae692f2 | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 277 | UseSU->addPred(Dep); |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 282 | /// Adds register dependencies (data, anti, and output) from this SUnit |
Matthias Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 283 | /// to following instructions in the same scheduling region that depend the |
| 284 | /// physical register referenced at OperIdx. |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 285 | void ScheduleDAGInstrs::addPhysRegDeps(SUnit *SU, unsigned OperIdx) { |
Andrew Trick | 8e62b29 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 286 | MachineInstr *MI = SU->getInstr(); |
| 287 | MachineOperand &MO = MI->getOperand(OperIdx); |
Matthias Braun | fc1b3b3 | 2016-11-10 22:11:00 +0000 | [diff] [blame] | 288 | unsigned Reg = MO.getReg(); |
Matthias Braun | 773603e | 2016-11-10 23:46:44 +0000 | [diff] [blame] | 289 | // We do not need to track any dependencies for constant registers. |
| 290 | if (MRI.isConstantPhysReg(Reg)) |
| 291 | return; |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 292 | |
| 293 | // Optionally add output and anti dependencies. For anti |
| 294 | // dependencies we use a latency of 0 because for a multi-issue |
| 295 | // target we want to allow the defining instruction to issue |
| 296 | // in the same cycle as the using instruction. |
| 297 | // TODO: Using a latency of 1 here for output dependencies assumes |
| 298 | // there's no cost for reusing registers. |
| 299 | SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output; |
Matthias Braun | fc1b3b3 | 2016-11-10 22:11:00 +0000 | [diff] [blame] | 300 | for (MCRegAliasIterator Alias(Reg, TRI, true); Alias.isValid(); ++Alias) { |
Andrew Trick | 702d489 | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 301 | if (!Defs.contains(*Alias)) |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 302 | continue; |
Michael Ilseman | afe77f3 | 2013-01-21 18:18:53 +0000 | [diff] [blame] | 303 | for (Reg2SUnitsMap::iterator I = Defs.find(*Alias); I != Defs.end(); ++I) { |
| 304 | SUnit *DefSU = I->SU; |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 305 | if (DefSU == &ExitSU) |
| 306 | continue; |
| 307 | if (DefSU != SU && |
| 308 | (Kind != SDep::Output || !MO.isDead() || |
Hal Finkel | 138d5bf | 2014-12-05 02:07:35 +0000 | [diff] [blame] | 309 | !DefSU->getInstr()->registerDefIsDead(*Alias))) { |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 310 | if (Kind == SDep::Anti) |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 311 | DefSU->addPred(SDep(SU, Kind, /*Reg=*/*Alias)); |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 312 | else { |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 313 | SDep Dep(SU, Kind, /*Reg=*/*Alias); |
Andrew Trick | b86a0cd | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 314 | Dep.setLatency( |
| 315 | SchedModel.computeOutputLatency(MI, OperIdx, DefSU->getInstr())); |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 316 | DefSU->addPred(Dep); |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 322 | if (!MO.isDef()) { |
Andrew Trick | 4392f0f | 2013-04-13 06:07:40 +0000 | [diff] [blame] | 323 | SU->hasPhysRegUses = true; |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 324 | // Either insert a new Reg2SUnits entry with an empty SUnits list, or |
| 325 | // retrieve the existing SUnits list for this register's uses. |
| 326 | // Push this SUnit on the use list. |
Matthias Braun | fc1b3b3 | 2016-11-10 22:11:00 +0000 | [diff] [blame] | 327 | Uses.insert(PhysRegSUOper(SU, OperIdx, Reg)); |
Andrew Trick | 8e62b29 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 328 | if (RemoveKillFlags) |
| 329 | MO.setIsKill(false); |
Matthias Braun | fc1b3b3 | 2016-11-10 22:11:00 +0000 | [diff] [blame] | 330 | } else { |
Andrew Trick | ffd2526 | 2012-08-23 00:39:43 +0000 | [diff] [blame] | 331 | addPhysRegDataDeps(SU, OperIdx); |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 332 | |
Jonas Paulsson | 4a65e92 | 2018-05-24 08:38:06 +0000 | [diff] [blame] | 333 | // Clear previous uses and defs of this register and its subergisters. |
| 334 | for (MCSubRegIterator SubReg(Reg, TRI, true); SubReg.isValid(); ++SubReg) { |
| 335 | if (Uses.contains(*SubReg)) |
| 336 | Uses.eraseAll(*SubReg); |
| 337 | if (!MO.isDead()) |
| 338 | Defs.eraseAll(*SubReg); |
| 339 | } |
| 340 | if (MO.isDead() && SU->isCall) { |
Michael Ilseman | afe77f3 | 2013-01-21 18:18:53 +0000 | [diff] [blame] | 341 | // Calls will not be reordered because of chain dependencies (see |
| 342 | // below). Since call operands are dead, calls may continue to be added |
| 343 | // to the DefList making dependence checking quadratic in the size of |
| 344 | // the block. Instead, we leave only one call at the back of the |
| 345 | // DefList. |
| 346 | Reg2SUnitsMap::RangePair P = Defs.equal_range(Reg); |
| 347 | Reg2SUnitsMap::iterator B = P.first; |
| 348 | Reg2SUnitsMap::iterator I = P.second; |
| 349 | for (bool isBegin = I == B; !isBegin; /* empty */) { |
| 350 | isBegin = (--I) == B; |
| 351 | if (!I->SU->isCall) |
| 352 | break; |
| 353 | I = Defs.erase(I); |
| 354 | } |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 355 | } |
Michael Ilseman | afe77f3 | 2013-01-21 18:18:53 +0000 | [diff] [blame] | 356 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 357 | // Defs are pushed in the order they are visited and never reordered. |
Michael Ilseman | afe77f3 | 2013-01-21 18:18:53 +0000 | [diff] [blame] | 358 | Defs.insert(PhysRegSUOper(SU, OperIdx, Reg)); |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 362 | LaneBitmask ScheduleDAGInstrs::getLaneMaskForMO(const MachineOperand &MO) const |
| 363 | { |
| 364 | unsigned Reg = MO.getReg(); |
| 365 | // No point in tracking lanemasks if we don't have interesting subregisters. |
| 366 | const TargetRegisterClass &RC = *MRI.getRegClass(Reg); |
| 367 | if (!RC.HasDisjunctSubRegs) |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 368 | return LaneBitmask::getAll(); |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 369 | |
| 370 | unsigned SubReg = MO.getSubReg(); |
| 371 | if (SubReg == 0) |
| 372 | return RC.getLaneMask(); |
| 373 | return TRI->getSubRegIndexLaneMask(SubReg); |
| 374 | } |
| 375 | |
Matthias Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 376 | /// Adds register output and data dependencies from this SUnit to instructions |
| 377 | /// that occur later in the same scheduling region if they read from or write to |
| 378 | /// the virtual register defined at OperIdx. |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 379 | /// |
| 380 | /// TODO: Hoist loop induction variable increments. This has to be |
| 381 | /// reevaluated. Generally, IV scheduling should be done before coalescing. |
| 382 | void ScheduleDAGInstrs::addVRegDefDeps(SUnit *SU, unsigned OperIdx) { |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 383 | MachineInstr *MI = SU->getInstr(); |
| 384 | MachineOperand &MO = MI->getOperand(OperIdx); |
| 385 | unsigned Reg = MO.getReg(); |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 386 | |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 387 | LaneBitmask DefLaneMask; |
| 388 | LaneBitmask KillLaneMask; |
| 389 | if (TrackLaneMasks) { |
| 390 | bool IsKill = MO.getSubReg() == 0 || MO.isUndef(); |
| 391 | DefLaneMask = getLaneMaskForMO(MO); |
| 392 | // If we have a <read-undef> flag, none of the lane values comes from an |
| 393 | // earlier instruction. |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 394 | KillLaneMask = IsKill ? LaneBitmask::getAll() : DefLaneMask; |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 395 | |
| 396 | // Clear undef flag, we'll re-add it later once we know which subregister |
| 397 | // Def is first. |
| 398 | MO.setIsUndef(false); |
| 399 | } else { |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 400 | DefLaneMask = LaneBitmask::getAll(); |
| 401 | KillLaneMask = LaneBitmask::getAll(); |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | if (MO.isDead()) { |
| 405 | assert(CurrentVRegUses.find(Reg) == CurrentVRegUses.end() && |
| 406 | "Dead defs should have no uses"); |
| 407 | } else { |
| 408 | // Add data dependence to all uses we found so far. |
| 409 | const TargetSubtargetInfo &ST = MF.getSubtarget(); |
| 410 | for (VReg2SUnitOperIdxMultiMap::iterator I = CurrentVRegUses.find(Reg), |
| 411 | E = CurrentVRegUses.end(); I != E; /*empty*/) { |
| 412 | LaneBitmask LaneMask = I->LaneMask; |
| 413 | // Ignore uses of other lanes. |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 414 | if ((LaneMask & KillLaneMask).none()) { |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 415 | ++I; |
| 416 | continue; |
| 417 | } |
| 418 | |
Krzysztof Parzyszek | 308c60d | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 419 | if ((LaneMask & DefLaneMask).any()) { |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 420 | SUnit *UseSU = I->SU; |
| 421 | MachineInstr *Use = UseSU->getInstr(); |
| 422 | SDep Dep(SU, SDep::Data, Reg); |
| 423 | Dep.setLatency(SchedModel.computeOperandLatency(MI, OperIdx, Use, |
| 424 | I->OperandIndex)); |
| 425 | ST.adjustSchedDependency(SU, UseSU, Dep); |
| 426 | UseSU->addPred(Dep); |
| 427 | } |
| 428 | |
| 429 | LaneMask &= ~KillLaneMask; |
| 430 | // If we found a Def for all lanes of this use, remove it from the list. |
Krzysztof Parzyszek | 308c60d | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 431 | if (LaneMask.any()) { |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 432 | I->LaneMask = LaneMask; |
| 433 | ++I; |
| 434 | } else |
| 435 | I = CurrentVRegUses.erase(I); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | // Shortcut: Singly defined vregs do not have output/anti dependencies. |
Andrew Trick | 8b5704f | 2012-07-30 23:48:17 +0000 | [diff] [blame] | 440 | if (MRI.hasOneDef(Reg)) |
Andrew Trick | 4b72ada | 2012-07-28 01:48:15 +0000 | [diff] [blame] | 441 | return; |
Andrew Trick | cc77b54 | 2012-02-22 06:08:13 +0000 | [diff] [blame] | 442 | |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 443 | // Add output dependence to the next nearest defs of this vreg. |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 444 | // |
| 445 | // Unless this definition is dead, the output dependence should be |
| 446 | // transitively redundant with antidependencies from this definition's |
| 447 | // uses. We're conservative for now until we have a way to guarantee the uses |
| 448 | // are not eliminated sometime during scheduling. The output dependence edge |
| 449 | // is also useful if output latency exceeds def-use latency. |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 450 | LaneBitmask LaneMask = DefLaneMask; |
| 451 | for (VReg2SUnit &V2SU : make_range(CurrentVRegDefs.find(Reg), |
| 452 | CurrentVRegDefs.end())) { |
| 453 | // Ignore defs for other lanes. |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 454 | if ((V2SU.LaneMask & LaneMask).none()) |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 455 | continue; |
| 456 | // Add an output dependence. |
| 457 | SUnit *DefSU = V2SU.SU; |
| 458 | // Ignore additional defs of the same lanes in one instruction. This can |
| 459 | // happen because lanemasks are shared for targets with too many |
| 460 | // subregisters. We also use some representration tricks/hacks where we |
| 461 | // add super-register defs/uses, to imply that although we only access parts |
| 462 | // of the reg we care about the full one. |
| 463 | if (DefSU == SU) |
| 464 | continue; |
| 465 | SDep Dep(SU, SDep::Output, Reg); |
| 466 | Dep.setLatency( |
| 467 | SchedModel.computeOutputLatency(MI, OperIdx, DefSU->getInstr())); |
| 468 | DefSU->addPred(Dep); |
| 469 | |
| 470 | // Update current definition. This can get tricky if the def was about a |
| 471 | // bigger lanemask before. We then have to shrink it and create a new |
| 472 | // VReg2SUnit for the non-overlapping part. |
| 473 | LaneBitmask OverlapMask = V2SU.LaneMask & LaneMask; |
| 474 | LaneBitmask NonOverlapMask = V2SU.LaneMask & ~LaneMask; |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 475 | V2SU.SU = SU; |
| 476 | V2SU.LaneMask = OverlapMask; |
Krzysztof Parzyszek | 308c60d | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 477 | if (NonOverlapMask.any()) |
Matthias Braun | 20c7e33 | 2016-05-25 01:18:00 +0000 | [diff] [blame] | 478 | CurrentVRegDefs.insert(VReg2SUnit(Reg, NonOverlapMask, DefSU)); |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 479 | } |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 480 | // If there was no CurrentVRegDefs entry for some lanes yet, create one. |
Krzysztof Parzyszek | 308c60d | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 481 | if (LaneMask.any()) |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 482 | CurrentVRegDefs.insert(VReg2SUnit(Reg, LaneMask, SU)); |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 485 | /// Adds a register data dependency if the instruction that defines the |
Matthias Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 486 | /// virtual register used at OperIdx is mapped to an SUnit. Add a register |
| 487 | /// antidependency from this SUnit to instructions that occur later in the same |
| 488 | /// scheduling region if they write the virtual register. |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 489 | /// |
| 490 | /// TODO: Handle ExitSU "uses" properly. |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 491 | void ScheduleDAGInstrs::addVRegUseDeps(SUnit *SU, unsigned OperIdx) { |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 492 | const MachineInstr *MI = SU->getInstr(); |
| 493 | const MachineOperand &MO = MI->getOperand(OperIdx); |
| 494 | unsigned Reg = MO.getReg(); |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 495 | |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 496 | // Remember the use. Data dependencies will be added when we find the def. |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 497 | LaneBitmask LaneMask = TrackLaneMasks ? getLaneMaskForMO(MO) |
| 498 | : LaneBitmask::getAll(); |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 499 | CurrentVRegUses.insert(VReg2SUnitOperIdx(Reg, LaneMask, OperIdx, SU)); |
| 500 | |
| 501 | // Add antidependences to the following defs of the vreg. |
| 502 | for (VReg2SUnit &V2SU : make_range(CurrentVRegDefs.find(Reg), |
| 503 | CurrentVRegDefs.end())) { |
| 504 | // Ignore defs for unrelated lanes. |
| 505 | LaneBitmask PrevDefLaneMask = V2SU.LaneMask; |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 506 | if ((PrevDefLaneMask & LaneMask).none()) |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 507 | continue; |
| 508 | if (V2SU.SU == SU) |
| 509 | continue; |
| 510 | |
| 511 | V2SU.SU->addPred(SDep(SU, SDep::Anti, Reg)); |
Andrew Trick | 663bd99 | 2013-08-30 04:36:57 +0000 | [diff] [blame] | 512 | } |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 513 | } |
Andrew Trick | 3c58ba8 | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 514 | |
Matthias Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 515 | /// Returns true if MI is an instruction we are unable to reason about |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 516 | /// (like a call or something with unmodeled side effects). |
| 517 | static inline bool isGlobalMemoryObject(AliasAnalysis *AA, MachineInstr *MI) { |
Rafael Espindola | 2fe94b6 | 2015-10-24 23:11:13 +0000 | [diff] [blame] | 518 | return MI->isCall() || MI->hasUnmodeledSideEffects() || |
Justin Lebar | e7555f0 | 2016-09-10 01:03:20 +0000 | [diff] [blame] | 519 | (MI->hasOrderedMemoryRef() && !MI->isDereferenceableInvariantLoad(AA)); |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 522 | void ScheduleDAGInstrs::addChainDependency (SUnit *SUa, SUnit *SUb, |
| 523 | unsigned Latency) { |
Eli Friedman | bfa1145 | 2017-03-09 23:33:36 +0000 | [diff] [blame] | 524 | if (SUa->getInstr()->mayAlias(AAForDep, *SUb->getInstr(), UseTBAA)) { |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 525 | SDep Dep(SUa, SDep::MayAliasMem); |
| 526 | Dep.setLatency(Latency); |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 527 | SUb->addPred(Dep); |
| 528 | } |
Andrew Trick | eb05b97 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 531 | /// Creates an SUnit for each real instruction, numbered in top-down |
Matthias Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 532 | /// topological order. The instruction order A < B, implies that no edge exists |
| 533 | /// from B to A. |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 534 | /// |
| 535 | /// Map each real instruction to its SUnit. |
| 536 | /// |
Andrew Trick | 17d35e5 | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 537 | /// After initSUnits, the SUnits vector cannot be resized and the scheduler may |
| 538 | /// hang onto SUnit pointers. We may relax this in the future by using SUnit IDs |
| 539 | /// instead of pointers. |
| 540 | /// |
| 541 | /// MachineScheduler relies on initSUnits numbering the nodes by their order in |
| 542 | /// the original instruction list. |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 543 | void ScheduleDAGInstrs::initSUnits() { |
| 544 | // We'll be allocating one SUnit for each real instruction in the region, |
| 545 | // which is contained within a basic block. |
Andrew Trick | d2763f6 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 546 | SUnits.reserve(NumRegionInstrs); |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 547 | |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 548 | for (MachineInstr &MI : make_range(RegionBegin, RegionEnd)) { |
Shiva Chen | 24abe71 | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 549 | if (MI.isDebugInstr()) |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 550 | continue; |
| 551 | |
Duncan P. N. Exon Smith | f634f61 | 2016-07-01 16:21:48 +0000 | [diff] [blame] | 552 | SUnit *SU = newSUnit(&MI); |
| 553 | MISUnitMap[&MI] = SU; |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 554 | |
Duncan P. N. Exon Smith | f634f61 | 2016-07-01 16:21:48 +0000 | [diff] [blame] | 555 | SU->isCall = MI.isCall(); |
| 556 | SU->isCommutable = MI.isCommutable(); |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 557 | |
| 558 | // Assign the Latency field of SU using target-provided information. |
Andrew Trick | 412cd2f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 559 | SU->Latency = SchedModel.computeInstrLatency(SU->getInstr()); |
Andrew Trick | 5739313 | 2013-12-05 17:55:58 +0000 | [diff] [blame] | 560 | |
Andrew Trick | 939bc92 | 2014-04-18 17:35:08 +0000 | [diff] [blame] | 561 | // If this SUnit uses a reserved or unbuffered resource, mark it as such. |
| 562 | // |
Alp Toker | 727273b | 2014-05-15 01:52:21 +0000 | [diff] [blame] | 563 | // Reserved resources block an instruction from issuing and stall the |
Andrew Trick | 939bc92 | 2014-04-18 17:35:08 +0000 | [diff] [blame] | 564 | // entire pipeline. These are identified by BufferSize=0. |
| 565 | // |
Alp Toker | 727273b | 2014-05-15 01:52:21 +0000 | [diff] [blame] | 566 | // Unbuffered resources prevent execution of subsequent instructions that |
Andrew Trick | 939bc92 | 2014-04-18 17:35:08 +0000 | [diff] [blame] | 567 | // require the same resources. This is used for in-order execution pipelines |
| 568 | // within an out-of-order core. These are identified by BufferSize=1. |
Andrew Trick | 5739313 | 2013-12-05 17:55:58 +0000 | [diff] [blame] | 569 | if (SchedModel.hasInstrSchedModel()) { |
| 570 | const MCSchedClassDesc *SC = getSchedClass(SU); |
Matthias Braun | a179dcf | 2016-09-30 23:08:07 +0000 | [diff] [blame] | 571 | for (const MCWriteProcResEntry &PRE : |
| 572 | make_range(SchedModel.getWriteProcResBegin(SC), |
| 573 | SchedModel.getWriteProcResEnd(SC))) { |
| 574 | switch (SchedModel.getProcResource(PRE.ProcResourceIdx)->BufferSize) { |
Andrew Trick | 6606ef0 | 2013-12-05 17:56:02 +0000 | [diff] [blame] | 575 | case 0: |
| 576 | SU->hasReservedResource = true; |
| 577 | break; |
| 578 | case 1: |
Andrew Trick | 5739313 | 2013-12-05 17:55:58 +0000 | [diff] [blame] | 579 | SU->isUnbuffered = true; |
| 580 | break; |
Andrew Trick | 6606ef0 | 2013-12-05 17:56:02 +0000 | [diff] [blame] | 581 | default: |
| 582 | break; |
Andrew Trick | 5739313 | 2013-12-05 17:55:58 +0000 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | } |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 586 | } |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 589 | class ScheduleDAGInstrs::Value2SUsMap : public MapVector<ValueType, SUList> { |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 590 | /// Current total number of SUs in map. |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 591 | unsigned NumNodes = 0; |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 592 | |
| 593 | /// 1 for loads, 0 for stores. (see comment in SUList) |
| 594 | unsigned TrueMemOrderLatency; |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 595 | |
Matthias Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 596 | public: |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 597 | Value2SUsMap(unsigned lat = 0) : TrueMemOrderLatency(lat) {} |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 598 | |
| 599 | /// To keep NumNodes up to date, insert() is used instead of |
| 600 | /// this operator w/ push_back(). |
| 601 | ValueType &operator[](const SUList &Key) { |
| 602 | llvm_unreachable("Don't use. Use insert() instead."); }; |
| 603 | |
Matthias Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 604 | /// Adds SU to the SUList of V. If Map grows huge, reduce its size by calling |
| 605 | /// reduce(). |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 606 | void inline insert(SUnit *SU, ValueType V) { |
| 607 | MapVector::operator[](V).push_back(SU); |
| 608 | NumNodes++; |
| 609 | } |
| 610 | |
| 611 | /// Clears the list of SUs mapped to V. |
| 612 | void inline clearList(ValueType V) { |
| 613 | iterator Itr = find(V); |
| 614 | if (Itr != end()) { |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 615 | assert(NumNodes >= Itr->second.size()); |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 616 | NumNodes -= Itr->second.size(); |
| 617 | |
| 618 | Itr->second.clear(); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | /// Clears map from all contents. |
| 623 | void clear() { |
| 624 | MapVector<ValueType, SUList>::clear(); |
| 625 | NumNodes = 0; |
| 626 | } |
| 627 | |
| 628 | unsigned inline size() const { return NumNodes; } |
| 629 | |
Matthias Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 630 | /// Counts the number of SUs in this map after a reduction. |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 631 | void reComputeSize() { |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 632 | NumNodes = 0; |
| 633 | for (auto &I : *this) |
| 634 | NumNodes += I.second.size(); |
| 635 | } |
| 636 | |
| 637 | unsigned inline getTrueMemOrderLatency() const { |
| 638 | return TrueMemOrderLatency; |
| 639 | } |
| 640 | |
| 641 | void dump(); |
| 642 | }; |
| 643 | |
| 644 | void ScheduleDAGInstrs::addChainDependencies(SUnit *SU, |
| 645 | Value2SUsMap &Val2SUsMap) { |
| 646 | for (auto &I : Val2SUsMap) |
| 647 | addChainDependencies(SU, I.second, |
| 648 | Val2SUsMap.getTrueMemOrderLatency()); |
| 649 | } |
| 650 | |
| 651 | void ScheduleDAGInstrs::addChainDependencies(SUnit *SU, |
| 652 | Value2SUsMap &Val2SUsMap, |
| 653 | ValueType V) { |
| 654 | Value2SUsMap::iterator Itr = Val2SUsMap.find(V); |
| 655 | if (Itr != Val2SUsMap.end()) |
| 656 | addChainDependencies(SU, Itr->second, |
| 657 | Val2SUsMap.getTrueMemOrderLatency()); |
| 658 | } |
| 659 | |
| 660 | void ScheduleDAGInstrs::addBarrierChain(Value2SUsMap &map) { |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 661 | assert(BarrierChain != nullptr); |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 662 | |
| 663 | for (auto &I : map) { |
| 664 | SUList &sus = I.second; |
| 665 | for (auto *SU : sus) |
| 666 | SU->addPredBarrier(BarrierChain); |
| 667 | } |
| 668 | map.clear(); |
| 669 | } |
| 670 | |
| 671 | void ScheduleDAGInstrs::insertBarrierChain(Value2SUsMap &map) { |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 672 | assert(BarrierChain != nullptr); |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 673 | |
| 674 | // Go through all lists of SUs. |
| 675 | for (Value2SUsMap::iterator I = map.begin(), EE = map.end(); I != EE;) { |
| 676 | Value2SUsMap::iterator CurrItr = I++; |
| 677 | SUList &sus = CurrItr->second; |
| 678 | SUList::iterator SUItr = sus.begin(), SUEE = sus.end(); |
| 679 | for (; SUItr != SUEE; ++SUItr) { |
| 680 | // Stop on BarrierChain or any instruction above it. |
| 681 | if ((*SUItr)->NodeNum <= BarrierChain->NodeNum) |
| 682 | break; |
| 683 | |
| 684 | (*SUItr)->addPredBarrier(BarrierChain); |
| 685 | } |
| 686 | |
| 687 | // Remove also the BarrierChain from list if present. |
NAKAMURA Takumi | 1be8ec9 | 2016-05-02 17:29:55 +0000 | [diff] [blame] | 688 | if (SUItr != SUEE && *SUItr == BarrierChain) |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 689 | SUItr++; |
| 690 | |
| 691 | // Remove all SUs that are now successors of BarrierChain. |
| 692 | if (SUItr != sus.begin()) |
| 693 | sus.erase(sus.begin(), SUItr); |
| 694 | } |
| 695 | |
| 696 | // Remove all entries with empty su lists. |
| 697 | map.remove_if([&](std::pair<ValueType, SUList> &mapEntry) { |
| 698 | return (mapEntry.second.empty()); }); |
| 699 | |
| 700 | // Recompute the size of the map (NumNodes). |
| 701 | map.reComputeSize(); |
| 702 | } |
| 703 | |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 704 | void ScheduleDAGInstrs::buildSchedGraph(AliasAnalysis *AA, |
Andrew Trick | 4c60b8a | 2013-08-30 03:49:48 +0000 | [diff] [blame] | 705 | RegPressureTracker *RPTracker, |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 706 | PressureDiffs *PDiffs, |
Matthias Braun | d267d37 | 2016-01-20 00:23:32 +0000 | [diff] [blame] | 707 | LiveIntervals *LIS, |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 708 | bool TrackLaneMasks) { |
Eric Christopher | fd0f792 | 2015-01-27 07:54:39 +0000 | [diff] [blame] | 709 | const TargetSubtargetInfo &ST = MF.getSubtarget(); |
Hal Finkel | 738073c | 2013-08-29 03:25:05 +0000 | [diff] [blame] | 710 | bool UseAA = EnableAASchedMI.getNumOccurrences() > 0 ? EnableAASchedMI |
| 711 | : ST.useAA(); |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 712 | AAForDep = UseAA ? AA : nullptr; |
| 713 | |
| 714 | BarrierChain = nullptr; |
Hal Finkel | 738073c | 2013-08-29 03:25:05 +0000 | [diff] [blame] | 715 | |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 716 | this->TrackLaneMasks = TrackLaneMasks; |
Andrew Trick | 40b52bb | 2013-09-04 21:00:02 +0000 | [diff] [blame] | 717 | MISUnitMap.clear(); |
| 718 | ScheduleDAG::clearDAG(); |
| 719 | |
Andrew Trick | b4566a9 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 720 | // Create an SUnit for each real instruction. |
| 721 | initSUnits(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 722 | |
Andrew Trick | 4c60b8a | 2013-08-30 03:49:48 +0000 | [diff] [blame] | 723 | if (PDiffs) |
| 724 | PDiffs->init(SUnits.size()); |
| 725 | |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 726 | // We build scheduling units by walking a block's instruction list |
| 727 | // from bottom to top. |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 728 | |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 729 | // Each MIs' memory operand(s) is analyzed to a list of underlying |
Jonas Paulsson | ebc5f75 | 2016-02-04 13:08:48 +0000 | [diff] [blame] | 730 | // objects. The SU is then inserted in the SUList(s) mapped from the |
| 731 | // Value(s). Each Value thus gets mapped to lists of SUs depending |
| 732 | // on it, stores and loads kept separately. Two SUs are trivially |
| 733 | // non-aliasing if they both depend on only identified Values and do |
| 734 | // not share any common Value. |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 735 | Value2SUsMap Stores, Loads(1 /*TrueMemOrderLatency*/); |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 736 | |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 737 | // Certain memory accesses are known to not alias any SU in Stores |
| 738 | // or Loads, and have therefore their own 'NonAlias' |
| 739 | // domain. E.g. spill / reload instructions never alias LLVM I/R |
Jonas Paulsson | ebc5f75 | 2016-02-04 13:08:48 +0000 | [diff] [blame] | 740 | // Values. It would be nice to assume that this type of memory |
| 741 | // accesses always have a proper memory operand modelling, and are |
| 742 | // therefore never unanalyzable, but this is conservatively not |
| 743 | // done. |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 744 | Value2SUsMap NonAliasStores, NonAliasLoads(1 /*TrueMemOrderLatency*/); |
| 745 | |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 746 | // Remove any stale debug info; sometimes BuildSchedGraph is called again |
| 747 | // without emitting the info from the previous call. |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 748 | DbgValues.clear(); |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 749 | FirstDbgValue = nullptr; |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 750 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 751 | assert(Defs.empty() && Uses.empty() && |
| 752 | "Only BuildGraph should update Defs/Uses"); |
Michael Ilseman | afe77f3 | 2013-01-21 18:18:53 +0000 | [diff] [blame] | 753 | Defs.setUniverse(TRI->getNumRegs()); |
| 754 | Uses.setUniverse(TRI->getNumRegs()); |
Andrew Trick | 9b66853 | 2011-05-06 21:52:52 +0000 | [diff] [blame] | 755 | |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 756 | assert(CurrentVRegDefs.empty() && "nobody else should use CurrentVRegDefs"); |
| 757 | assert(CurrentVRegUses.empty() && "nobody else should use CurrentVRegUses"); |
| 758 | unsigned NumVirtRegs = MRI.getNumVirtRegs(); |
| 759 | CurrentVRegDefs.setUniverse(NumVirtRegs); |
| 760 | CurrentVRegUses.setUniverse(NumVirtRegs); |
| 761 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 762 | // Model data dependencies between instructions being scheduled and the |
| 763 | // ExitSU. |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 764 | addSchedBarrierDeps(); |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 765 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 766 | // Walk the list of instructions, from bottom moving up. |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 767 | MachineInstr *DbgMI = nullptr; |
Andrew Trick | 68675c6 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 768 | for (MachineBasicBlock::iterator MII = RegionEnd, MIE = RegionBegin; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 769 | MII != MIE; --MII) { |
Duncan P. N. Exon Smith | f634f61 | 2016-07-01 16:21:48 +0000 | [diff] [blame] | 770 | MachineInstr &MI = *std::prev(MII); |
| 771 | if (DbgMI) { |
| 772 | DbgValues.push_back(std::make_pair(DbgMI, &MI)); |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 773 | DbgMI = nullptr; |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Duncan P. N. Exon Smith | f634f61 | 2016-07-01 16:21:48 +0000 | [diff] [blame] | 776 | if (MI.isDebugValue()) { |
| 777 | DbgMI = &MI; |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 778 | continue; |
| 779 | } |
Shiva Chen | 24abe71 | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 780 | if (MI.isDebugLabel()) |
| 781 | continue; |
| 782 | |
Duncan P. N. Exon Smith | f634f61 | 2016-07-01 16:21:48 +0000 | [diff] [blame] | 783 | SUnit *SU = MISUnitMap[&MI]; |
Andrew Trick | 4c60b8a | 2013-08-30 03:49:48 +0000 | [diff] [blame] | 784 | assert(SU && "No SUnit mapped to this MI"); |
| 785 | |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 786 | if (RPTracker) { |
Matthias Braun | 4adfe41 | 2016-01-12 22:57:35 +0000 | [diff] [blame] | 787 | RegisterOperands RegOpers; |
Duncan P. N. Exon Smith | f634f61 | 2016-07-01 16:21:48 +0000 | [diff] [blame] | 788 | RegOpers.collect(MI, *TRI, MRI, TrackLaneMasks, false); |
Matthias Braun | d267d37 | 2016-01-20 00:23:32 +0000 | [diff] [blame] | 789 | if (TrackLaneMasks) { |
Duncan P. N. Exon Smith | f634f61 | 2016-07-01 16:21:48 +0000 | [diff] [blame] | 790 | SlotIndex SlotIdx = LIS->getInstructionIndex(MI); |
Matthias Braun | d267d37 | 2016-01-20 00:23:32 +0000 | [diff] [blame] | 791 | RegOpers.adjustLaneLiveness(*LIS, MRI, SlotIdx); |
| 792 | } |
Matthias Braun | 4adfe41 | 2016-01-12 22:57:35 +0000 | [diff] [blame] | 793 | if (PDiffs != nullptr) |
| 794 | PDiffs->addInstruction(SU->NodeNum, RegOpers, MRI); |
| 795 | |
Yaxun Liu | 06d39e2 | 2017-12-15 03:56:57 +0000 | [diff] [blame] | 796 | if (RPTracker->getPos() == RegionEnd || &*RPTracker->getPos() != &MI) |
| 797 | RPTracker->recedeSkipDebugValues(); |
Duncan P. N. Exon Smith | f634f61 | 2016-07-01 16:21:48 +0000 | [diff] [blame] | 798 | assert(&*RPTracker->getPos() == &MI && "RPTracker in sync"); |
Matthias Braun | 4adfe41 | 2016-01-12 22:57:35 +0000 | [diff] [blame] | 799 | RPTracker->recede(RegOpers); |
Andrew Trick | 006e1ab | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 800 | } |
Devang Patel | cf4cc84 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 801 | |
Rafael Espindola | 7d7d996 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 802 | assert( |
Duncan P. N. Exon Smith | f634f61 | 2016-07-01 16:21:48 +0000 | [diff] [blame] | 803 | (CanHandleTerminators || (!MI.isTerminator() && !MI.isPosition())) && |
Rafael Espindola | 7d7d996 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 804 | "Cannot schedule terminators or labels!"); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 805 | |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 806 | // Add register-based dependencies (data, anti, and output). |
Krzysztof Parzyszek | 3c0f002 | 2016-05-10 16:50:30 +0000 | [diff] [blame] | 807 | // For some instructions (calls, returns, inline-asm, etc.) there can |
| 808 | // be explicit uses and implicit defs, in which case the use will appear |
| 809 | // on the operand list before the def. Do two passes over the operand |
| 810 | // list to make sure that defs are processed before any uses. |
Andrew Trick | 04f52e1 | 2012-12-18 20:53:01 +0000 | [diff] [blame] | 811 | bool HasVRegDef = false; |
Duncan P. N. Exon Smith | f634f61 | 2016-07-01 16:21:48 +0000 | [diff] [blame] | 812 | for (unsigned j = 0, n = MI.getNumOperands(); j != n; ++j) { |
| 813 | const MachineOperand &MO = MI.getOperand(j); |
Krzysztof Parzyszek | 3c0f002 | 2016-05-10 16:50:30 +0000 | [diff] [blame] | 814 | if (!MO.isReg() || !MO.isDef()) |
| 815 | continue; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 816 | unsigned Reg = MO.getReg(); |
Matthias Braun | fc1b3b3 | 2016-11-10 22:11:00 +0000 | [diff] [blame] | 817 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Andrew Trick | 7ebcaf4 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 818 | addPhysRegDeps(SU, j); |
Matthias Braun | fc1b3b3 | 2016-11-10 22:11:00 +0000 | [diff] [blame] | 819 | } else if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
Krzysztof Parzyszek | 3c0f002 | 2016-05-10 16:50:30 +0000 | [diff] [blame] | 820 | HasVRegDef = true; |
| 821 | addVRegDefDeps(SU, j); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 822 | } |
| 823 | } |
Krzysztof Parzyszek | 3c0f002 | 2016-05-10 16:50:30 +0000 | [diff] [blame] | 824 | // Now process all uses. |
Duncan P. N. Exon Smith | f634f61 | 2016-07-01 16:21:48 +0000 | [diff] [blame] | 825 | for (unsigned j = 0, n = MI.getNumOperands(); j != n; ++j) { |
| 826 | const MachineOperand &MO = MI.getOperand(j); |
Matthias Braun | 014fc12 | 2016-05-10 20:11:58 +0000 | [diff] [blame] | 827 | // Only look at use operands. |
| 828 | // We do not need to check for MO.readsReg() here because subsequent |
| 829 | // subregister defs will get output dependence edges and need no |
| 830 | // additional use dependencies. |
Krzysztof Parzyszek | 3c0f002 | 2016-05-10 16:50:30 +0000 | [diff] [blame] | 831 | if (!MO.isReg() || !MO.isUse()) |
| 832 | continue; |
| 833 | unsigned Reg = MO.getReg(); |
Matthias Braun | fc1b3b3 | 2016-11-10 22:11:00 +0000 | [diff] [blame] | 834 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Krzysztof Parzyszek | 3c0f002 | 2016-05-10 16:50:30 +0000 | [diff] [blame] | 835 | addPhysRegDeps(SU, j); |
Matthias Braun | fc1b3b3 | 2016-11-10 22:11:00 +0000 | [diff] [blame] | 836 | } else if (TargetRegisterInfo::isVirtualRegister(Reg) && MO.readsReg()) { |
Krzysztof Parzyszek | 3c0f002 | 2016-05-10 16:50:30 +0000 | [diff] [blame] | 837 | addVRegUseDeps(SU, j); |
Matthias Braun | fc1b3b3 | 2016-11-10 22:11:00 +0000 | [diff] [blame] | 838 | } |
Krzysztof Parzyszek | 3c0f002 | 2016-05-10 16:50:30 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Andrew Trick | 04f52e1 | 2012-12-18 20:53:01 +0000 | [diff] [blame] | 841 | // If we haven't seen any uses in this scheduling region, create a |
| 842 | // dependence edge to ExitSU to model the live-out latency. This is required |
| 843 | // for vreg defs with no in-region use, and prefetches with no vreg def. |
| 844 | // |
| 845 | // FIXME: NumDataSuccs would be more precise than NumSuccs here. This |
| 846 | // check currently relies on being called before adding chain deps. |
Duncan P. N. Exon Smith | f634f61 | 2016-07-01 16:21:48 +0000 | [diff] [blame] | 847 | if (SU->NumSuccs == 0 && SU->Latency > 1 && (HasVRegDef || MI.mayLoad())) { |
Andrew Trick | 04f52e1 | 2012-12-18 20:53:01 +0000 | [diff] [blame] | 848 | SDep Dep(SU, SDep::Artificial); |
| 849 | Dep.setLatency(SU->Latency - 1); |
| 850 | ExitSU.addPred(Dep); |
| 851 | } |
Dan Gohman | 6a9041e | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 852 | |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 853 | // Add memory dependencies (Note: isStoreToStackSlot and |
| 854 | // isLoadFromStackSLot are not usable after stack slots are lowered to |
| 855 | // actual addresses). |
| 856 | |
| 857 | // This is a barrier event that acts as a pivotal node in the DAG. |
Duncan P. N. Exon Smith | f634f61 | 2016-07-01 16:21:48 +0000 | [diff] [blame] | 858 | if (isGlobalMemoryObject(AA, &MI)) { |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 859 | |
| 860 | // Become the barrier chain. |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 861 | if (BarrierChain) |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 862 | BarrierChain->addPredBarrier(SU); |
David Goodwin | 980d494 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 863 | BarrierChain = SU; |
| 864 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 865 | LLVM_DEBUG(dbgs() << "Global memory object and new barrier chain: SU(" |
| 866 | << BarrierChain->NodeNum << ").\n";); |
Tom Stellard | 653ef32 | 2014-12-08 23:36:48 +0000 | [diff] [blame] | 867 | |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 868 | // Add dependencies against everything below it and clear maps. |
| 869 | addBarrierChain(Stores); |
| 870 | addBarrierChain(Loads); |
| 871 | addBarrierChain(NonAliasStores); |
| 872 | addBarrierChain(NonAliasLoads); |
Hal Finkel | f218310 | 2012-12-10 18:49:16 +0000 | [diff] [blame] | 873 | |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 874 | continue; |
| 875 | } |
| 876 | |
| 877 | // If it's not a store or a variant load, we're done. |
Justin Lebar | e7555f0 | 2016-09-10 01:03:20 +0000 | [diff] [blame] | 878 | if (!MI.mayStore() && |
| 879 | !(MI.mayLoad() && !MI.isDereferenceableInvariantLoad(AA))) |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 880 | continue; |
| 881 | |
| 882 | // Always add dependecy edge to BarrierChain if present. |
| 883 | if (BarrierChain) |
| 884 | BarrierChain->addPredBarrier(SU); |
| 885 | |
| 886 | // Find the underlying objects for MI. The Objs vector is either |
| 887 | // empty, or filled with the Values of memory locations which this |
Hiroshi Inoue | 0f48afc | 2017-10-12 06:26:04 +0000 | [diff] [blame] | 888 | // SU depends on. |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 889 | UnderlyingObjectsVector Objs; |
Hiroshi Inoue | 0f48afc | 2017-10-12 06:26:04 +0000 | [diff] [blame] | 890 | bool ObjsFound = getUnderlyingObjectsForInstr(&MI, MFI, Objs, |
| 891 | MF.getDataLayout()); |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 892 | |
Duncan P. N. Exon Smith | f634f61 | 2016-07-01 16:21:48 +0000 | [diff] [blame] | 893 | if (MI.mayStore()) { |
Hiroshi Inoue | 0f48afc | 2017-10-12 06:26:04 +0000 | [diff] [blame] | 894 | if (!ObjsFound) { |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 895 | // An unknown store depends on all stores and loads. |
| 896 | addChainDependencies(SU, Stores); |
| 897 | addChainDependencies(SU, NonAliasStores); |
| 898 | addChainDependencies(SU, Loads); |
| 899 | addChainDependencies(SU, NonAliasLoads); |
| 900 | |
| 901 | // Map this store to 'UnknownValue'. |
| 902 | Stores.insert(SU, UnknownValue); |
Chandler Carruth | e6ec1e3 | 2016-03-31 21:55:58 +0000 | [diff] [blame] | 903 | } else { |
| 904 | // Add precise dependencies against all previously seen memory |
| 905 | // accesses mapped to the same Value(s). |
Geoff Berry | 1a49060 | 2016-04-14 21:31:07 +0000 | [diff] [blame] | 906 | for (const UnderlyingObject &UnderlObj : Objs) { |
| 907 | ValueType V = UnderlObj.getValue(); |
| 908 | bool ThisMayAlias = UnderlObj.mayAlias(); |
Chandler Carruth | e6ec1e3 | 2016-03-31 21:55:58 +0000 | [diff] [blame] | 909 | |
| 910 | // Add dependencies to previous stores and loads mapped to V. |
Geoff Berry | 1a49060 | 2016-04-14 21:31:07 +0000 | [diff] [blame] | 911 | addChainDependencies(SU, (ThisMayAlias ? Stores : NonAliasStores), V); |
Chandler Carruth | e6ec1e3 | 2016-03-31 21:55:58 +0000 | [diff] [blame] | 912 | addChainDependencies(SU, (ThisMayAlias ? Loads : NonAliasLoads), V); |
Geoff Berry | 3c28f60 | 2016-04-12 15:50:19 +0000 | [diff] [blame] | 913 | } |
| 914 | // Update the store map after all chains have been added to avoid adding |
| 915 | // self-loop edge if multiple underlying objects are present. |
Geoff Berry | 1a49060 | 2016-04-14 21:31:07 +0000 | [diff] [blame] | 916 | for (const UnderlyingObject &UnderlObj : Objs) { |
| 917 | ValueType V = UnderlObj.getValue(); |
| 918 | bool ThisMayAlias = UnderlObj.mayAlias(); |
Chandler Carruth | e6ec1e3 | 2016-03-31 21:55:58 +0000 | [diff] [blame] | 919 | |
| 920 | // Map this store to V. |
Geoff Berry | 1a49060 | 2016-04-14 21:31:07 +0000 | [diff] [blame] | 921 | (ThisMayAlias ? Stores : NonAliasStores).insert(SU, V); |
Chandler Carruth | e6ec1e3 | 2016-03-31 21:55:58 +0000 | [diff] [blame] | 922 | } |
| 923 | // The store may have dependencies to unanalyzable loads and |
| 924 | // stores. |
| 925 | addChainDependencies(SU, Loads, UnknownValue); |
| 926 | addChainDependencies(SU, Stores, UnknownValue); |
Hal Finkel | f218310 | 2012-12-10 18:49:16 +0000 | [diff] [blame] | 927 | } |
Chandler Carruth | e6ec1e3 | 2016-03-31 21:55:58 +0000 | [diff] [blame] | 928 | } else { // SU is a load. |
Hiroshi Inoue | 0f48afc | 2017-10-12 06:26:04 +0000 | [diff] [blame] | 929 | if (!ObjsFound) { |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 930 | // An unknown load depends on all stores. |
| 931 | addChainDependencies(SU, Stores); |
| 932 | addChainDependencies(SU, NonAliasStores); |
| 933 | |
| 934 | Loads.insert(SU, UnknownValue); |
Chandler Carruth | e6ec1e3 | 2016-03-31 21:55:58 +0000 | [diff] [blame] | 935 | } else { |
Geoff Berry | 1a49060 | 2016-04-14 21:31:07 +0000 | [diff] [blame] | 936 | for (const UnderlyingObject &UnderlObj : Objs) { |
| 937 | ValueType V = UnderlObj.getValue(); |
| 938 | bool ThisMayAlias = UnderlObj.mayAlias(); |
Chandler Carruth | e6ec1e3 | 2016-03-31 21:55:58 +0000 | [diff] [blame] | 939 | |
| 940 | // Add precise dependencies against all previously seen stores |
| 941 | // mapping to the same Value(s). |
| 942 | addChainDependencies(SU, (ThisMayAlias ? Stores : NonAliasStores), V); |
| 943 | |
| 944 | // Map this load to V. |
| 945 | (ThisMayAlias ? Loads : NonAliasLoads).insert(SU, V); |
| 946 | } |
| 947 | // The load may have dependencies to unanalyzable stores. |
| 948 | addChainDependencies(SU, Stores, UnknownValue); |
Hal Finkel | f218310 | 2012-12-10 18:49:16 +0000 | [diff] [blame] | 949 | } |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | // Reduce maps if they grow huge. |
| 953 | if (Stores.size() + Loads.size() >= HugeRegion) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 954 | LLVM_DEBUG(dbgs() << "Reducing Stores and Loads maps.\n";); |
Mehdi Amini | d7628da | 2016-04-16 04:58:30 +0000 | [diff] [blame] | 955 | reduceHugeMemNodeMaps(Stores, Loads, getReductionSize()); |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 956 | } |
| 957 | if (NonAliasStores.size() + NonAliasLoads.size() >= HugeRegion) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 958 | LLVM_DEBUG( |
| 959 | dbgs() << "Reducing NonAliasStores and NonAliasLoads maps.\n";); |
Mehdi Amini | d7628da | 2016-04-16 04:58:30 +0000 | [diff] [blame] | 960 | reduceHugeMemNodeMaps(NonAliasStores, NonAliasLoads, getReductionSize()); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 961 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 962 | } |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 963 | |
Andrew Trick | 657b75b | 2012-12-01 01:22:49 +0000 | [diff] [blame] | 964 | if (DbgMI) |
| 965 | FirstDbgValue = DbgMI; |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 966 | |
Andrew Trick | 81a682a | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 967 | Defs.clear(); |
| 968 | Uses.clear(); |
Matthias Braun | 7adbf11 | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 969 | CurrentVRegDefs.clear(); |
| 970 | CurrentVRegUses.clear(); |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | raw_ostream &llvm::operator<<(raw_ostream &OS, const PseudoSourceValue* PSV) { |
| 974 | PSV->printCustom(OS); |
| 975 | return OS; |
| 976 | } |
| 977 | |
| 978 | void ScheduleDAGInstrs::Value2SUsMap::dump() { |
| 979 | for (auto &Itr : *this) { |
| 980 | if (Itr.first.is<const Value*>()) { |
| 981 | const Value *V = Itr.first.get<const Value*>(); |
| 982 | if (isa<UndefValue>(V)) |
| 983 | dbgs() << "Unknown"; |
| 984 | else |
| 985 | V->printAsOperand(dbgs()); |
| 986 | } |
| 987 | else if (Itr.first.is<const PseudoSourceValue*>()) |
| 988 | dbgs() << Itr.first.get<const PseudoSourceValue*>(); |
| 989 | else |
| 990 | llvm_unreachable("Unknown Value type."); |
| 991 | |
| 992 | dbgs() << " : "; |
| 993 | dumpSUList(Itr.second); |
| 994 | } |
| 995 | } |
| 996 | |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 997 | void ScheduleDAGInstrs::reduceHugeMemNodeMaps(Value2SUsMap &stores, |
| 998 | Value2SUsMap &loads, unsigned N) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 999 | LLVM_DEBUG(dbgs() << "Before reduction:\nStoring SUnits:\n"; stores.dump(); |
| 1000 | dbgs() << "Loading SUnits:\n"; loads.dump()); |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 1001 | |
| 1002 | // Insert all SU's NodeNums into a vector and sort it. |
| 1003 | std::vector<unsigned> NodeNums; |
| 1004 | NodeNums.reserve(stores.size() + loads.size()); |
| 1005 | for (auto &I : stores) |
| 1006 | for (auto *SU : I.second) |
| 1007 | NodeNums.push_back(SU->NodeNum); |
| 1008 | for (auto &I : loads) |
| 1009 | for (auto *SU : I.second) |
| 1010 | NodeNums.push_back(SU->NodeNum); |
Fangrui Song | 3b35e17 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 1011 | llvm::sort(NodeNums); |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 1012 | |
| 1013 | // The N last elements in NodeNums will be removed, and the SU with |
| 1014 | // the lowest NodeNum of them will become the new BarrierChain to |
| 1015 | // let the not yet seen SUs have a dependency to the removed SUs. |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 1016 | assert(N <= NodeNums.size()); |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 1017 | SUnit *newBarrierChain = &SUnits[*(NodeNums.end() - N)]; |
| 1018 | if (BarrierChain) { |
| 1019 | // The aliasing and non-aliasing maps reduce independently of each |
| 1020 | // other, but share a common BarrierChain. Check if the |
| 1021 | // newBarrierChain is above the former one. If it is not, it may |
| 1022 | // introduce a loop to use newBarrierChain, so keep the old one. |
| 1023 | if (newBarrierChain->NodeNum < BarrierChain->NodeNum) { |
| 1024 | BarrierChain->addPredBarrier(newBarrierChain); |
| 1025 | BarrierChain = newBarrierChain; |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1026 | LLVM_DEBUG(dbgs() << "Inserting new barrier chain: SU(" |
| 1027 | << BarrierChain->NodeNum << ").\n";); |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 1028 | } |
| 1029 | else |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1030 | LLVM_DEBUG(dbgs() << "Keeping old barrier chain: SU(" |
| 1031 | << BarrierChain->NodeNum << ").\n";); |
Jonas Paulsson | 8e9339f | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 1032 | } |
| 1033 | else |
| 1034 | BarrierChain = newBarrierChain; |
| 1035 | |
| 1036 | insertBarrierChain(stores); |
| 1037 | insertBarrierChain(loads); |
| 1038 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1039 | LLVM_DEBUG(dbgs() << "After reduction:\nStoring SUnits:\n"; stores.dump(); |
| 1040 | dbgs() << "Loading SUnits:\n"; loads.dump()); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Matthias Braun | 0248ff9 | 2017-05-27 02:50:50 +0000 | [diff] [blame] | 1043 | static void toggleKills(const MachineRegisterInfo &MRI, LivePhysRegs &LiveRegs, |
| 1044 | MachineInstr &MI, bool addToLiveRegs) { |
| 1045 | for (MachineOperand &MO : MI.operands()) { |
| 1046 | if (!MO.isReg() || !MO.readsReg()) |
| 1047 | continue; |
| 1048 | unsigned Reg = MO.getReg(); |
| 1049 | if (!Reg) |
| 1050 | continue; |
Andrew Trick | 8e62b29 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 1051 | |
Matthias Braun | 0248ff9 | 2017-05-27 02:50:50 +0000 | [diff] [blame] | 1052 | // Things that are available after the instruction are killed by it. |
| 1053 | bool IsKill = LiveRegs.available(MRI, Reg); |
| 1054 | MO.setIsKill(IsKill); |
Matthias Braun | ea254cb | 2017-06-27 00:58:48 +0000 | [diff] [blame] | 1055 | if (addToLiveRegs) |
Matthias Braun | 0248ff9 | 2017-05-27 02:50:50 +0000 | [diff] [blame] | 1056 | LiveRegs.addReg(Reg); |
Andrew Trick | 8e62b29 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 1057 | } |
| 1058 | } |
| 1059 | |
Matthias Braun | 0248ff9 | 2017-05-27 02:50:50 +0000 | [diff] [blame] | 1060 | void ScheduleDAGInstrs::fixupKills(MachineBasicBlock &MBB) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1061 | LLVM_DEBUG(dbgs() << "Fixup kills for " << printMBBReference(MBB) << '\n'); |
Pete Cooper | ba99a57 | 2015-05-04 16:52:06 +0000 | [diff] [blame] | 1062 | |
Matthias Braun | 0248ff9 | 2017-05-27 02:50:50 +0000 | [diff] [blame] | 1063 | LiveRegs.init(*TRI); |
| 1064 | LiveRegs.addLiveOuts(MBB); |
Andrew Trick | 8e62b29 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 1065 | |
| 1066 | // Examine block from end to start... |
Matthias Braun | 0248ff9 | 2017-05-27 02:50:50 +0000 | [diff] [blame] | 1067 | for (MachineInstr &MI : make_range(MBB.rbegin(), MBB.rend())) { |
Shiva Chen | 24abe71 | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 1068 | if (MI.isDebugInstr()) |
Andrew Trick | 8e62b29 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 1069 | continue; |
| 1070 | |
| 1071 | // Update liveness. Registers that are defed but not used in this |
| 1072 | // instruction are now dead. Mark register and all subregs as they |
| 1073 | // are completely defined. |
Matthias Braun | 0248ff9 | 2017-05-27 02:50:50 +0000 | [diff] [blame] | 1074 | for (ConstMIBundleOperands O(MI); O.isValid(); ++O) { |
| 1075 | const MachineOperand &MO = *O; |
| 1076 | if (MO.isReg()) { |
| 1077 | if (!MO.isDef()) |
| 1078 | continue; |
| 1079 | unsigned Reg = MO.getReg(); |
| 1080 | if (!Reg) |
| 1081 | continue; |
| 1082 | LiveRegs.removeReg(Reg); |
| 1083 | } else if (MO.isRegMask()) { |
| 1084 | LiveRegs.removeRegsInMask(MO); |
| 1085 | } |
Andrew Trick | 8e62b29 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Matthias Braun | 0248ff9 | 2017-05-27 02:50:50 +0000 | [diff] [blame] | 1088 | // If there is a bundle header fix it up first. |
| 1089 | if (!MI.isBundled()) { |
| 1090 | toggleKills(MRI, LiveRegs, MI, true); |
| 1091 | } else { |
| 1092 | MachineBasicBlock::instr_iterator First = MI.getIterator(); |
| 1093 | if (MI.isBundle()) { |
| 1094 | toggleKills(MRI, LiveRegs, MI, false); |
| 1095 | ++First; |
Andrew Trick | 8e62b29 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 1096 | } |
Matthias Braun | 0248ff9 | 2017-05-27 02:50:50 +0000 | [diff] [blame] | 1097 | // Some targets make the (questionable) assumtion that the instructions |
| 1098 | // inside the bundle are ordered and consequently only the last use of |
| 1099 | // a register inside the bundle can kill it. |
| 1100 | MachineBasicBlock::instr_iterator I = std::next(First); |
| 1101 | while (I->isBundledWithSucc()) |
| 1102 | ++I; |
| 1103 | do { |
Shiva Chen | 24abe71 | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 1104 | if (!I->isDebugInstr()) |
Matthias Braun | 0248ff9 | 2017-05-27 02:50:50 +0000 | [diff] [blame] | 1105 | toggleKills(MRI, LiveRegs, *I, true); |
| 1106 | --I; |
| 1107 | } while(I != First); |
Andrew Trick | 8e62b29 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 1108 | } |
| 1109 | } |
| 1110 | } |
| 1111 | |
Matthias Braun | b064c24 | 2018-09-19 00:23:35 +0000 | [diff] [blame] | 1112 | void ScheduleDAGInstrs::dumpNode(const SUnit &SU) const { |
Aaron Ballman | 1d03d38 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 1113 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Matthias Braun | b064c24 | 2018-09-19 00:23:35 +0000 | [diff] [blame] | 1114 | dumpNodeName(SU); |
| 1115 | dbgs() << ": "; |
| 1116 | SU.getInstr()->dump(); |
| 1117 | #endif |
| 1118 | } |
| 1119 | |
| 1120 | void ScheduleDAGInstrs::dump() const { |
| 1121 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 1122 | if (EntrySU.getInstr() != nullptr) |
| 1123 | dumpNodeAll(EntrySU); |
| 1124 | for (const SUnit &SU : SUnits) |
| 1125 | dumpNodeAll(SU); |
| 1126 | if (ExitSU.getInstr() != nullptr) |
| 1127 | dumpNodeAll(ExitSU); |
Manman Ren | 77e300e | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 1128 | #endif |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const { |
Alp Toker | 8dd8d5c | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 1132 | std::string s; |
| 1133 | raw_string_ostream oss(s); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 1134 | if (SU == &EntrySU) |
| 1135 | oss << "<entry>"; |
| 1136 | else if (SU == &ExitSU) |
| 1137 | oss << "<exit>"; |
| 1138 | else |
Eric Christopher | 9656d2d | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 1139 | SU->getInstr()->print(oss, /*SkipOpers=*/true); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1140 | return oss.str(); |
| 1141 | } |
| 1142 | |
Andrew Trick | 56b94c5 | 2012-03-07 00:18:22 +0000 | [diff] [blame] | 1143 | /// Return the basic block label. It is not necessarilly unique because a block |
| 1144 | /// contains multiple scheduling regions. But it is fine for visualization. |
| 1145 | std::string ScheduleDAGInstrs::getDAGName() const { |
| 1146 | return "dag." + BB->getFullName(); |
| 1147 | } |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1148 | |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1149 | //===----------------------------------------------------------------------===// |
| 1150 | // SchedDFSResult Implementation |
| 1151 | //===----------------------------------------------------------------------===// |
| 1152 | |
| 1153 | namespace llvm { |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 1154 | |
Matthias Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 1155 | /// Internal state used to compute SchedDFSResult. |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1156 | class SchedDFSImpl { |
| 1157 | SchedDFSResult &R; |
| 1158 | |
| 1159 | /// Join DAG nodes into equivalence classes by their subtree. |
| 1160 | IntEqClasses SubtreeClasses; |
| 1161 | /// List PredSU, SuccSU pairs that represent data edges between subtrees. |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 1162 | std::vector<std::pair<const SUnit *, const SUnit*>> ConnectionPairs; |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1163 | |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1164 | struct RootData { |
| 1165 | unsigned NodeID; |
Matthias Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 1166 | unsigned ParentNodeID; ///< Parent node (member of the parent subtree). |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 1167 | unsigned SubInstrCount = 0; ///< Instr count in this tree only, not |
| 1168 | /// children. |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1169 | |
| 1170 | RootData(unsigned id): NodeID(id), |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 1171 | ParentNodeID(SchedDFSResult::InvalidSubtreeID) {} |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1172 | |
| 1173 | unsigned getSparseSetIndex() const { return NodeID; } |
| 1174 | }; |
| 1175 | |
| 1176 | SparseSet<RootData> RootSet; |
| 1177 | |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1178 | public: |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1179 | SchedDFSImpl(SchedDFSResult &r): R(r), SubtreeClasses(R.DFSNodeData.size()) { |
| 1180 | RootSet.setUniverse(R.DFSNodeData.size()); |
| 1181 | } |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1182 | |
Matthias Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 1183 | /// Returns true if this node been visited by the DFS traversal. |
Andrew Trick | bfb8223 | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1184 | /// |
| 1185 | /// During visitPostorderNode the Node's SubtreeID is assigned to the Node |
| 1186 | /// ID. Later, SubtreeID is updated but remains valid. |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1187 | bool isVisited(const SUnit *SU) const { |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1188 | return R.DFSNodeData[SU->NodeNum].SubtreeID |
| 1189 | != SchedDFSResult::InvalidSubtreeID; |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Matthias Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 1192 | /// Initializes this node's instruction count. We don't need to flag the node |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1193 | /// visited until visitPostorder because the DAG cannot have cycles. |
| 1194 | void visitPreorder(const SUnit *SU) { |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1195 | R.DFSNodeData[SU->NodeNum].InstrCount = |
| 1196 | SU->getInstr()->isTransient() ? 0 : 1; |
Andrew Trick | bfb8223 | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | /// Called once for each node after all predecessors are visited. Revisit this |
| 1200 | /// node's predecessors and potentially join them now that we know the ILP of |
| 1201 | /// the other predecessors. |
| 1202 | void visitPostorderNode(const SUnit *SU) { |
| 1203 | // Mark this node as the root of a subtree. It may be joined with its |
| 1204 | // successors later. |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1205 | R.DFSNodeData[SU->NodeNum].SubtreeID = SU->NodeNum; |
| 1206 | RootData RData(SU->NodeNum); |
| 1207 | RData.SubInstrCount = SU->getInstr()->isTransient() ? 0 : 1; |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1208 | |
Andrew Trick | bfb8223 | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1209 | // If any predecessors are still in their own subtree, they either cannot be |
| 1210 | // joined or are large enough to remain separate. If this parent node's |
| 1211 | // total instruction count is not greater than a child subtree by at least |
| 1212 | // the subtree limit, then try to join it now since splitting subtrees is |
| 1213 | // only useful if multiple high-pressure paths are possible. |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1214 | unsigned InstrCount = R.DFSNodeData[SU->NodeNum].InstrCount; |
Matthias Braun | a179dcf | 2016-09-30 23:08:07 +0000 | [diff] [blame] | 1215 | for (const SDep &PredDep : SU->Preds) { |
| 1216 | if (PredDep.getKind() != SDep::Data) |
Andrew Trick | bfb8223 | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1217 | continue; |
Matthias Braun | a179dcf | 2016-09-30 23:08:07 +0000 | [diff] [blame] | 1218 | unsigned PredNum = PredDep.getSUnit()->NodeNum; |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1219 | if ((InstrCount - R.DFSNodeData[PredNum].InstrCount) < R.SubtreeLimit) |
Matthias Braun | a179dcf | 2016-09-30 23:08:07 +0000 | [diff] [blame] | 1220 | joinPredSubtree(PredDep, SU, /*CheckLimit=*/false); |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1221 | |
| 1222 | // Either link or merge the TreeData entry from the child to the parent. |
Andrew Trick | a5a73ad | 2013-01-25 06:52:30 +0000 | [diff] [blame] | 1223 | if (R.DFSNodeData[PredNum].SubtreeID == PredNum) { |
| 1224 | // If the predecessor's parent is invalid, this is a tree edge and the |
| 1225 | // current node is the parent. |
| 1226 | if (RootSet[PredNum].ParentNodeID == SchedDFSResult::InvalidSubtreeID) |
| 1227 | RootSet[PredNum].ParentNodeID = SU->NodeNum; |
| 1228 | } |
| 1229 | else if (RootSet.count(PredNum)) { |
| 1230 | // The predecessor is not a root, but is still in the root set. This |
| 1231 | // must be the new parent that it was just joined to. Note that |
| 1232 | // RootSet[PredNum].ParentNodeID may either be invalid or may still be |
| 1233 | // set to the original parent. |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1234 | RData.SubInstrCount += RootSet[PredNum].SubInstrCount; |
| 1235 | RootSet.erase(PredNum); |
| 1236 | } |
Andrew Trick | bfb8223 | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1237 | } |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1238 | RootSet[SU->NodeNum] = RData; |
| 1239 | } |
| 1240 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1241 | /// Called once for each tree edge after calling visitPostOrderNode on |
Matthias Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 1242 | /// the predecessor. Increment the parent node's instruction count and |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1243 | /// preemptively join this subtree to its parent's if it is small enough. |
| 1244 | void visitPostorderEdge(const SDep &PredDep, const SUnit *Succ) { |
| 1245 | R.DFSNodeData[Succ->NodeNum].InstrCount |
| 1246 | += R.DFSNodeData[PredDep.getSUnit()->NodeNum].InstrCount; |
| 1247 | joinPredSubtree(PredDep, Succ); |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
Matthias Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 1250 | /// Adds a connection for cross edges. |
Andrew Trick | bfb8223 | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1251 | void visitCrossEdge(const SDep &PredDep, const SUnit *Succ) { |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1252 | ConnectionPairs.push_back(std::make_pair(PredDep.getSUnit(), Succ)); |
| 1253 | } |
| 1254 | |
Matthias Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 1255 | /// Sets each node's subtree ID to the representative ID and record |
| 1256 | /// connections between trees. |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1257 | void finalize() { |
| 1258 | SubtreeClasses.compress(); |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1259 | R.DFSTreeData.resize(SubtreeClasses.getNumClasses()); |
| 1260 | assert(SubtreeClasses.getNumClasses() == RootSet.size() |
| 1261 | && "number of roots should match trees"); |
Matthias Braun | a179dcf | 2016-09-30 23:08:07 +0000 | [diff] [blame] | 1262 | for (const RootData &Root : RootSet) { |
| 1263 | unsigned TreeID = SubtreeClasses[Root.NodeID]; |
| 1264 | if (Root.ParentNodeID != SchedDFSResult::InvalidSubtreeID) |
| 1265 | R.DFSTreeData[TreeID].ParentTreeID = SubtreeClasses[Root.ParentNodeID]; |
| 1266 | R.DFSTreeData[TreeID].SubInstrCount = Root.SubInstrCount; |
Andrew Trick | a5a73ad | 2013-01-25 06:52:30 +0000 | [diff] [blame] | 1267 | // Note that SubInstrCount may be greater than InstrCount if we joined |
| 1268 | // subtrees across a cross edge. InstrCount will be attributed to the |
| 1269 | // original parent, while SubInstrCount will be attributed to the joined |
| 1270 | // parent. |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1271 | } |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1272 | R.SubtreeConnections.resize(SubtreeClasses.getNumClasses()); |
| 1273 | R.SubtreeConnectLevels.resize(SubtreeClasses.getNumClasses()); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1274 | LLVM_DEBUG(dbgs() << R.getNumSubtrees() << " subtrees:\n"); |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1275 | for (unsigned Idx = 0, End = R.DFSNodeData.size(); Idx != End; ++Idx) { |
| 1276 | R.DFSNodeData[Idx].SubtreeID = SubtreeClasses[Idx]; |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1277 | LLVM_DEBUG(dbgs() << " SU(" << Idx << ") in tree " |
| 1278 | << R.DFSNodeData[Idx].SubtreeID << '\n'); |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1279 | } |
Matthias Braun | a179dcf | 2016-09-30 23:08:07 +0000 | [diff] [blame] | 1280 | for (const std::pair<const SUnit*, const SUnit*> &P : ConnectionPairs) { |
| 1281 | unsigned PredTree = SubtreeClasses[P.first->NodeNum]; |
| 1282 | unsigned SuccTree = SubtreeClasses[P.second->NodeNum]; |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1283 | if (PredTree == SuccTree) |
| 1284 | continue; |
Matthias Braun | a179dcf | 2016-09-30 23:08:07 +0000 | [diff] [blame] | 1285 | unsigned Depth = P.first->getDepth(); |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1286 | addConnection(PredTree, SuccTree, Depth); |
| 1287 | addConnection(SuccTree, PredTree, Depth); |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | protected: |
Matthias Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 1292 | /// Joins the predecessor subtree with the successor that is its DFS parent. |
| 1293 | /// Applies some heuristics before joining. |
Andrew Trick | bfb8223 | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1294 | bool joinPredSubtree(const SDep &PredDep, const SUnit *Succ, |
| 1295 | bool CheckLimit = true) { |
| 1296 | assert(PredDep.getKind() == SDep::Data && "Subtrees are for data edges"); |
| 1297 | |
| 1298 | // Check if the predecessor is already joined. |
| 1299 | const SUnit *PredSU = PredDep.getSUnit(); |
| 1300 | unsigned PredNum = PredSU->NodeNum; |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1301 | if (R.DFSNodeData[PredNum].SubtreeID != PredNum) |
Andrew Trick | bfb8223 | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1302 | return false; |
Andrew Trick | b12a771 | 2013-01-25 00:12:57 +0000 | [diff] [blame] | 1303 | |
| 1304 | // Four is the magic number of successors before a node is considered a |
| 1305 | // pinch point. |
| 1306 | unsigned NumDataSucs = 0; |
Matthias Braun | a179dcf | 2016-09-30 23:08:07 +0000 | [diff] [blame] | 1307 | for (const SDep &SuccDep : PredSU->Succs) { |
| 1308 | if (SuccDep.getKind() == SDep::Data) { |
Andrew Trick | b12a771 | 2013-01-25 00:12:57 +0000 | [diff] [blame] | 1309 | if (++NumDataSucs >= 4) |
Andrew Trick | bfb8223 | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1310 | return false; |
Andrew Trick | b12a771 | 2013-01-25 00:12:57 +0000 | [diff] [blame] | 1311 | } |
| 1312 | } |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1313 | if (CheckLimit && R.DFSNodeData[PredNum].InstrCount > R.SubtreeLimit) |
Andrew Trick | bfb8223 | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1314 | return false; |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1315 | R.DFSNodeData[PredNum].SubtreeID = Succ->NodeNum; |
Andrew Trick | bfb8223 | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1316 | SubtreeClasses.join(Succ->NodeNum, PredNum); |
| 1317 | return true; |
Andrew Trick | b12a771 | 2013-01-25 00:12:57 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1320 | /// Called by finalize() to record a connection between trees. |
| 1321 | void addConnection(unsigned FromTree, unsigned ToTree, unsigned Depth) { |
| 1322 | if (!Depth) |
| 1323 | return; |
| 1324 | |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1325 | do { |
| 1326 | SmallVectorImpl<SchedDFSResult::Connection> &Connections = |
| 1327 | R.SubtreeConnections[FromTree]; |
Matthias Braun | a179dcf | 2016-09-30 23:08:07 +0000 | [diff] [blame] | 1328 | for (SchedDFSResult::Connection &C : Connections) { |
| 1329 | if (C.TreeID == ToTree) { |
| 1330 | C.Level = std::max(C.Level, Depth); |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1331 | return; |
| 1332 | } |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1333 | } |
Andrew Trick | 988d06b | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1334 | Connections.push_back(SchedDFSResult::Connection(ToTree, Depth)); |
| 1335 | FromTree = R.DFSTreeData[FromTree].ParentTreeID; |
| 1336 | } while (FromTree != SchedDFSResult::InvalidSubtreeID); |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1337 | } |
| 1338 | }; |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 1339 | |
Matthias Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 1340 | } // end namespace llvm |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1341 | |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1342 | namespace { |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 1343 | |
Matthias Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 1344 | /// Manage the stack used by a reverse depth-first search over the DAG. |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1345 | class SchedDAGReverseDFS { |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 1346 | std::vector<std::pair<const SUnit *, SUnit::const_pred_iterator>> DFSStack; |
| 1347 | |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1348 | public: |
| 1349 | bool isComplete() const { return DFSStack.empty(); } |
| 1350 | |
| 1351 | void follow(const SUnit *SU) { |
| 1352 | DFSStack.push_back(std::make_pair(SU, SU->Preds.begin())); |
| 1353 | } |
| 1354 | void advance() { ++DFSStack.back().second; } |
| 1355 | |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1356 | const SDep *backtrack() { |
| 1357 | DFSStack.pop_back(); |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1358 | return DFSStack.empty() ? nullptr : std::prev(DFSStack.back().second); |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1359 | } |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1360 | |
| 1361 | const SUnit *getCurr() const { return DFSStack.back().first; } |
| 1362 | |
| 1363 | SUnit::const_pred_iterator getPred() const { return DFSStack.back().second; } |
| 1364 | |
| 1365 | SUnit::const_pred_iterator getPredEnd() const { |
| 1366 | return getCurr()->Preds.end(); |
| 1367 | } |
| 1368 | }; |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 1369 | |
| 1370 | } // end anonymous namespace |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1371 | |
Andrew Trick | bfb8223 | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1372 | static bool hasDataSucc(const SUnit *SU) { |
Matthias Braun | a179dcf | 2016-09-30 23:08:07 +0000 | [diff] [blame] | 1373 | for (const SDep &SuccDep : SU->Succs) { |
| 1374 | if (SuccDep.getKind() == SDep::Data && |
| 1375 | !SuccDep.getSUnit()->isBoundaryNode()) |
Andrew Trick | bfb8223 | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1376 | return true; |
| 1377 | } |
| 1378 | return false; |
| 1379 | } |
| 1380 | |
Matthias Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 1381 | /// Computes an ILP metric for all nodes in the subDAG reachable via depth-first |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1382 | /// search from this root. |
Andrew Trick | 4e1fb18 | 2013-01-25 06:33:57 +0000 | [diff] [blame] | 1383 | void SchedDFSResult::compute(ArrayRef<SUnit> SUnits) { |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1384 | if (!IsBottomUp) |
Eric Christopher | 6b658aa | 2017-08-03 22:41:12 +0000 | [diff] [blame] | 1385 | llvm_unreachable("Top-down ILP metric is unimplemented"); |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1386 | |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1387 | SchedDFSImpl Impl(*this); |
Matthias Braun | a179dcf | 2016-09-30 23:08:07 +0000 | [diff] [blame] | 1388 | for (const SUnit &SU : SUnits) { |
| 1389 | if (Impl.isVisited(&SU) || hasDataSucc(&SU)) |
Andrew Trick | 4e1fb18 | 2013-01-25 06:33:57 +0000 | [diff] [blame] | 1390 | continue; |
| 1391 | |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1392 | SchedDAGReverseDFS DFS; |
Matthias Braun | a179dcf | 2016-09-30 23:08:07 +0000 | [diff] [blame] | 1393 | Impl.visitPreorder(&SU); |
| 1394 | DFS.follow(&SU); |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 1395 | while (true) { |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1396 | // Traverse the leftmost path as far as possible. |
| 1397 | while (DFS.getPred() != DFS.getPredEnd()) { |
| 1398 | const SDep &PredDep = *DFS.getPred(); |
| 1399 | DFS.advance(); |
Andrew Trick | bfb8223 | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1400 | // Ignore non-data edges. |
Andrew Trick | a5a73ad | 2013-01-25 06:52:30 +0000 | [diff] [blame] | 1401 | if (PredDep.getKind() != SDep::Data |
| 1402 | || PredDep.getSUnit()->isBoundaryNode()) { |
Andrew Trick | bfb8223 | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1403 | continue; |
Andrew Trick | a5a73ad | 2013-01-25 06:52:30 +0000 | [diff] [blame] | 1404 | } |
Andrew Trick | bfb8223 | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1405 | // An already visited edge is a cross edge, assuming an acyclic DAG. |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1406 | if (Impl.isVisited(PredDep.getSUnit())) { |
Andrew Trick | bfb8223 | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1407 | Impl.visitCrossEdge(PredDep, DFS.getCurr()); |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1408 | continue; |
| 1409 | } |
| 1410 | Impl.visitPreorder(PredDep.getSUnit()); |
| 1411 | DFS.follow(PredDep.getSUnit()); |
| 1412 | } |
| 1413 | // Visit the top of the stack in postorder and backtrack. |
| 1414 | const SUnit *Child = DFS.getCurr(); |
| 1415 | const SDep *PredDep = DFS.backtrack(); |
Andrew Trick | bfb8223 | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1416 | Impl.visitPostorderNode(Child); |
| 1417 | if (PredDep) |
| 1418 | Impl.visitPostorderEdge(*PredDep, DFS.getCurr()); |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1419 | if (DFS.isComplete()) |
| 1420 | break; |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1421 | } |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1422 | } |
| 1423 | Impl.finalize(); |
| 1424 | } |
| 1425 | |
| 1426 | /// The root of the given SubtreeID was just scheduled. For all subtrees |
| 1427 | /// connected to this tree, record the depth of the connection so that the |
| 1428 | /// nearest connected subtrees can be prioritized. |
| 1429 | void SchedDFSResult::scheduleTree(unsigned SubtreeID) { |
Matthias Braun | a179dcf | 2016-09-30 23:08:07 +0000 | [diff] [blame] | 1430 | for (const Connection &C : SubtreeConnections[SubtreeID]) { |
| 1431 | SubtreeConnectLevels[C.TreeID] = |
| 1432 | std::max(SubtreeConnectLevels[C.TreeID], C.Level); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1433 | LLVM_DEBUG(dbgs() << " Tree: " << C.TreeID << " @" |
| 1434 | << SubtreeConnectLevels[C.TreeID] << '\n'); |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1435 | } |
| 1436 | } |
| 1437 | |
Aaron Ballman | 1d03d38 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 1438 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Matthias Braun | 88d2075 | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 1439 | LLVM_DUMP_METHOD void ILPValue::print(raw_ostream &OS) const { |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1440 | OS << InstrCount << " / " << Length << " = "; |
| 1441 | if (!Length) |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1442 | OS << "BADILP"; |
Andrew Trick | 8b1496c | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1443 | else |
| 1444 | OS << format("%g", ((double)InstrCount / Length)); |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
Matthias Braun | 88d2075 | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 1447 | LLVM_DUMP_METHOD void ILPValue::dump() const { |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1448 | dbgs() << *this << '\n'; |
| 1449 | } |
| 1450 | |
| 1451 | namespace llvm { |
| 1452 | |
Alp Toker | a96332b | 2014-07-01 21:19:13 +0000 | [diff] [blame] | 1453 | LLVM_DUMP_METHOD |
Andrew Trick | 1e94e98 | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1454 | raw_ostream &operator<<(raw_ostream &OS, const ILPValue &Val) { |
| 1455 | Val.print(OS); |
| 1456 | return OS; |
| 1457 | } |
| 1458 | |
Matthias Braun | bb2a4cb | 2017-01-27 18:53:00 +0000 | [diff] [blame] | 1459 | } // end namespace llvm |
Eugene Zelenko | ff49b83 | 2017-06-01 23:25:02 +0000 | [diff] [blame] | 1460 | |
Matthias Braun | 88d2075 | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 1461 | #endif |