Dan Gohman | f90d3b0 | 2008-12-08 17:50:35 +0000 | [diff] [blame] | 1 | //===---- ScheduleDAGInstrs.cpp - MachineInstr Rescheduling ---------------===// |
Dan Gohman | 60cb69e | 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 | // |
Dan Gohman | f90d3b0 | 2008-12-08 17:50:35 +0000 | [diff] [blame] | 10 | // This implements the ScheduleDAGInstrs class, which implements re-scheduling |
| 11 | // of MachineInstrs. |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/ScheduleDAGInstrs.h" |
Matthias Braun | 97d0ffb | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/IntEqClasses.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallPtrSet.h" |
| 18 | #include "llvm/ADT/SmallSet.h" |
Dan Gohman | 1ee0d41 | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/AliasAnalysis.h" |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/ValueTracking.h" |
Matthias Braun | d4f6409 | 2016-01-20 00:23:32 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Arnold Schwaighofer | f54b73d | 2015-05-08 23:52:00 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Andrew Trick | 6b104f8 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineMemOperand.h" |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 3aab10b | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Andrew Trick | 88517f6 | 2012-06-06 19:47:35 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/RegisterPressure.h" |
Andrew Trick | cd1c2f9 | 2012-11-28 05:13:24 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/ScheduleDFS.h" |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Function.h" |
| 31 | #include "llvm/IR/Type.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Operator.h" |
Andrew Trick | da01ba3 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Format.h" |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 36 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetInstrInfo.h" |
| 38 | #include "llvm/Target/TargetMachine.h" |
| 39 | #include "llvm/Target/TargetRegisterInfo.h" |
| 40 | #include "llvm/Target/TargetSubtargetInfo.h" |
Andrew Trick | c01b004 | 2013-08-23 17:48:43 +0000 | [diff] [blame] | 41 | #include <queue> |
| 42 | |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 43 | using namespace llvm; |
| 44 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 45 | #define DEBUG_TYPE "misched" |
| 46 | |
Andrew Trick | da01ba3 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 47 | static cl::opt<bool> EnableAASchedMI("enable-aa-sched-mi", cl::Hidden, |
| 48 | cl::ZeroOrMore, cl::init(false), |
Jonas Paulsson | bf408bb | 2015-01-07 13:20:57 +0000 | [diff] [blame] | 49 | cl::desc("Enable use of AA during MI DAG construction")); |
Andrew Trick | da01ba3 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 50 | |
Hal Finkel | dbebb52 | 2014-01-25 19:24:54 +0000 | [diff] [blame] | 51 | static cl::opt<bool> UseTBAA("use-tbaa-in-sched-mi", cl::Hidden, |
Jonas Paulsson | bf408bb | 2015-01-07 13:20:57 +0000 | [diff] [blame] | 52 | cl::init(true), cl::desc("Enable use of TBAA during MI DAG construction")); |
Hal Finkel | dbebb52 | 2014-01-25 19:24:54 +0000 | [diff] [blame] | 53 | |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 54 | // Note: the two options below might be used in tuning compile time vs |
| 55 | // output quality. Setting HugeRegion so large that it will never be |
| 56 | // reached means best-effort, but may be slow. |
| 57 | |
| 58 | // When Stores and Loads maps (or NonAliasStores and NonAliasLoads) |
| 59 | // together hold this many SUs, a reduction of maps will be done. |
| 60 | static cl::opt<unsigned> HugeRegion("dag-maps-huge-region", cl::Hidden, |
| 61 | cl::init(1000), cl::desc("The limit to use while constructing the DAG " |
| 62 | "prior to scheduling, at which point a trade-off " |
| 63 | "is made to avoid excessive compile time.")); |
| 64 | |
| 65 | static cl::opt<unsigned> ReductionSize("dag-maps-reduction-size", cl::Hidden, |
| 66 | cl::desc("A huge scheduling region will have maps reduced by this many " |
| 67 | "nodes at a time. Defaults to HugeRegion / 2.")); |
| 68 | |
| 69 | static void dumpSUList(ScheduleDAGInstrs::SUList &L) { |
| 70 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 71 | dbgs() << "{ "; |
| 72 | for (auto *su : L) { |
| 73 | dbgs() << "SU(" << su->NodeNum << ")"; |
| 74 | if (su != L.back()) |
| 75 | dbgs() << ", "; |
| 76 | } |
| 77 | dbgs() << "}\n"; |
| 78 | #endif |
| 79 | } |
| 80 | |
Dan Gohman | 619ef48 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 81 | ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf, |
Alexey Samsonov | 8968e6d | 2014-08-20 19:36:05 +0000 | [diff] [blame] | 82 | const MachineLoopInfo *mli, |
Matthias Braun | 93563e7 | 2015-11-03 01:53:29 +0000 | [diff] [blame] | 83 | bool RemoveKillFlags) |
Matthias Braun | b17e8b1 | 2015-12-04 19:54:24 +0000 | [diff] [blame] | 84 | : ScheduleDAG(mf), MLI(mli), MFI(mf.getFrameInfo()), |
Matthias Braun | 93563e7 | 2015-11-03 01:53:29 +0000 | [diff] [blame] | 85 | RemoveKillFlags(RemoveKillFlags), CanHandleTerminators(false), |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 86 | TrackLaneMasks(false), AAForDep(nullptr), BarrierChain(nullptr), |
| 87 | UnknownValue(UndefValue::get( |
| 88 | Type::getVoidTy(mf.getFunction()->getContext()))), |
| 89 | FirstDbgValue(nullptr) { |
Devang Patel | e5feef0 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 90 | DbgValues.clear(); |
Andrew Trick | 9b63513 | 2012-09-18 18:20:00 +0000 | [diff] [blame] | 91 | |
Eric Christopher | 2c63549 | 2015-01-27 07:54:39 +0000 | [diff] [blame] | 92 | const TargetSubtargetInfo &ST = mf.getSubtarget(); |
Pete Cooper | 1175945 | 2014-09-02 17:43:54 +0000 | [diff] [blame] | 93 | SchedModel.init(ST.getSchedModel(), &ST, TII); |
Evan Cheng | f0236e0 | 2009-10-18 19:58:47 +0000 | [diff] [blame] | 94 | } |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 95 | |
Dan Gohman | 1ee0d41 | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 96 | /// getUnderlyingObjectFromInt - This is the function that does the work of |
| 97 | /// looking through basic ptrtoint+arithmetic+inttoptr sequences. |
| 98 | static const Value *getUnderlyingObjectFromInt(const Value *V) { |
| 99 | do { |
Dan Gohman | 58b0e71 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 100 | if (const Operator *U = dyn_cast<Operator>(V)) { |
Dan Gohman | 1ee0d41 | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 101 | // If we find a ptrtoint, we can transfer control back to the |
| 102 | // regular getUnderlyingObjectFromInt. |
Dan Gohman | 58b0e71 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 103 | if (U->getOpcode() == Instruction::PtrToInt) |
Dan Gohman | 1ee0d41 | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 104 | return U->getOperand(0); |
Andrew Trick | 0be1936 | 2012-11-28 03:42:49 +0000 | [diff] [blame] | 105 | // If we find an add of a constant, a multiplied value, or a phi, it's |
Dan Gohman | 1ee0d41 | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 106 | // likely that the other operand will lead us to the base |
| 107 | // object. We don't have to worry about the case where the |
Dan Gohman | 6c0c219 | 2009-08-07 01:26:06 +0000 | [diff] [blame] | 108 | // object address is somehow being computed by the multiply, |
Dan Gohman | 1ee0d41 | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 109 | // because our callers only care when the result is an |
Nick Lewycky | 1a32954 | 2012-10-26 04:27:49 +0000 | [diff] [blame] | 110 | // identifiable object. |
Dan Gohman | 58b0e71 | 2009-07-17 20:58:59 +0000 | [diff] [blame] | 111 | if (U->getOpcode() != Instruction::Add || |
Dan Gohman | 1ee0d41 | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 112 | (!isa<ConstantInt>(U->getOperand(1)) && |
Andrew Trick | 0be1936 | 2012-11-28 03:42:49 +0000 | [diff] [blame] | 113 | Operator::getOpcode(U->getOperand(1)) != Instruction::Mul && |
| 114 | !isa<PHINode>(U->getOperand(1)))) |
Dan Gohman | 1ee0d41 | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 115 | return V; |
| 116 | V = U->getOperand(0); |
| 117 | } else { |
| 118 | return V; |
| 119 | } |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 120 | assert(V->getType()->isIntegerTy() && "Unexpected operand type!"); |
Dan Gohman | 1ee0d41 | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 121 | } while (1); |
| 122 | } |
| 123 | |
Hal Finkel | 66859ae | 2012-12-10 18:49:16 +0000 | [diff] [blame] | 124 | /// getUnderlyingObjects - This is a wrapper around GetUnderlyingObjects |
Dan Gohman | 1ee0d41 | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 125 | /// and adds support for basic ptrtoint+arithmetic+inttoptr sequences. |
Hal Finkel | 66859ae | 2012-12-10 18:49:16 +0000 | [diff] [blame] | 126 | static void getUnderlyingObjects(const Value *V, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 127 | SmallVectorImpl<Value *> &Objects, |
| 128 | const DataLayout &DL) { |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 129 | SmallPtrSet<const Value *, 16> Visited; |
Hal Finkel | 66859ae | 2012-12-10 18:49:16 +0000 | [diff] [blame] | 130 | SmallVector<const Value *, 4> Working(1, V); |
Dan Gohman | 1ee0d41 | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 131 | do { |
Hal Finkel | 66859ae | 2012-12-10 18:49:16 +0000 | [diff] [blame] | 132 | V = Working.pop_back_val(); |
| 133 | |
| 134 | SmallVector<Value *, 4> Objs; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 135 | GetUnderlyingObjects(const_cast<Value *>(V), Objs, DL); |
Hal Finkel | 66859ae | 2012-12-10 18:49:16 +0000 | [diff] [blame] | 136 | |
Craig Topper | e1c1d36 | 2013-07-03 05:11:49 +0000 | [diff] [blame] | 137 | for (SmallVectorImpl<Value *>::iterator I = Objs.begin(), IE = Objs.end(); |
Hal Finkel | 66859ae | 2012-12-10 18:49:16 +0000 | [diff] [blame] | 138 | I != IE; ++I) { |
| 139 | V = *I; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 140 | if (!Visited.insert(V).second) |
Hal Finkel | 66859ae | 2012-12-10 18:49:16 +0000 | [diff] [blame] | 141 | continue; |
| 142 | if (Operator::getOpcode(V) == Instruction::IntToPtr) { |
| 143 | const Value *O = |
| 144 | getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0)); |
| 145 | if (O->getType()->isPointerTy()) { |
| 146 | Working.push_back(O); |
| 147 | continue; |
| 148 | } |
| 149 | } |
| 150 | Objects.push_back(const_cast<Value *>(V)); |
| 151 | } |
| 152 | } while (!Working.empty()); |
Dan Gohman | 1ee0d41 | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Hal Finkel | 66859ae | 2012-12-10 18:49:16 +0000 | [diff] [blame] | 155 | /// getUnderlyingObjectsForInstr - If this machine instr has memory reference |
Dan Gohman | 1ee0d41 | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 156 | /// information and it can be tracked to a normal reference to a known |
Hal Finkel | 66859ae | 2012-12-10 18:49:16 +0000 | [diff] [blame] | 157 | /// object, return the Value for that object. |
| 158 | static void getUnderlyingObjectsForInstr(const MachineInstr *MI, |
Benjamin Kramer | fd51092 | 2013-06-29 18:41:17 +0000 | [diff] [blame] | 159 | const MachineFrameInfo *MFI, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 160 | UnderlyingObjectsVector &Objects, |
| 161 | const DataLayout &DL) { |
Geoff Berry | 6381713 | 2016-04-14 21:31:07 +0000 | [diff] [blame^] | 162 | auto allMMOsOkay = [&]() { |
| 163 | for (const MachineMemOperand *MMO : MI->memoperands()) { |
| 164 | if (MMO->isVolatile()) |
| 165 | return false; |
Hal Finkel | 66859ae | 2012-12-10 18:49:16 +0000 | [diff] [blame] | 166 | |
Geoff Berry | 6381713 | 2016-04-14 21:31:07 +0000 | [diff] [blame^] | 167 | if (const PseudoSourceValue *PSV = MMO->getPseudoValue()) { |
| 168 | // Function that contain tail calls don't have unique PseudoSourceValue |
| 169 | // objects. Two PseudoSourceValues might refer to the same or |
| 170 | // overlapping locations. The client code calling this function assumes |
| 171 | // this is not the case. So return a conservative answer of no known |
| 172 | // object. |
| 173 | if (MFI->hasTailCall()) |
| 174 | return false; |
Geoff Berry | c0739d8 | 2016-04-12 15:50:19 +0000 | [diff] [blame] | 175 | |
Geoff Berry | 6381713 | 2016-04-14 21:31:07 +0000 | [diff] [blame^] | 176 | // For now, ignore PseudoSourceValues which may alias LLVM IR values |
| 177 | // because the code that uses this function has no way to cope with |
| 178 | // such aliases. |
| 179 | if (PSV->isAliased(MFI)) |
| 180 | return false; |
Geoff Berry | c0739d8 | 2016-04-12 15:50:19 +0000 | [diff] [blame] | 181 | |
Geoff Berry | 6381713 | 2016-04-14 21:31:07 +0000 | [diff] [blame^] | 182 | bool MayAlias = PSV->mayAlias(MFI); |
| 183 | Objects.push_back(UnderlyingObjectsVector::value_type(PSV, MayAlias)); |
| 184 | } else if (const Value *V = MMO->getValue()) { |
| 185 | SmallVector<Value *, 4> Objs; |
| 186 | getUnderlyingObjects(V, Objs, DL); |
Geoff Berry | c0739d8 | 2016-04-12 15:50:19 +0000 | [diff] [blame] | 187 | |
Geoff Berry | 6381713 | 2016-04-14 21:31:07 +0000 | [diff] [blame^] | 188 | for (Value *V : Objs) { |
| 189 | if (!isIdentifiedObject(V)) |
| 190 | return false; |
| 191 | |
| 192 | Objects.push_back(UnderlyingObjectsVector::value_type(V, true)); |
Geoff Berry | c0739d8 | 2016-04-12 15:50:19 +0000 | [diff] [blame] | 193 | } |
Geoff Berry | 6381713 | 2016-04-14 21:31:07 +0000 | [diff] [blame^] | 194 | } else |
| 195 | return false; |
Geoff Berry | c0739d8 | 2016-04-12 15:50:19 +0000 | [diff] [blame] | 196 | } |
Geoff Berry | 6381713 | 2016-04-14 21:31:07 +0000 | [diff] [blame^] | 197 | return true; |
| 198 | }; |
| 199 | |
| 200 | if (!allMMOsOkay()) |
| 201 | Objects.clear(); |
Dan Gohman | 1ee0d41 | 2009-01-30 02:49:14 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Andrew Trick | 7405c6d | 2012-04-20 20:05:21 +0000 | [diff] [blame] | 204 | void ScheduleDAGInstrs::startBlock(MachineBasicBlock *bb) { |
| 205 | BB = bb; |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 208 | void ScheduleDAGInstrs::finishBlock() { |
Andrew Trick | 51ee936 | 2012-04-20 20:24:33 +0000 | [diff] [blame] | 209 | // Subclasses should no longer refer to the old block. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 210 | BB = nullptr; |
Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 213 | /// Initialize the DAG and common scheduler state for the current scheduling |
| 214 | /// region. This does not actually create the DAG, only clears it. The |
| 215 | /// scheduling driver may call BuildSchedGraph multiple times per scheduling |
| 216 | /// region. |
| 217 | void ScheduleDAGInstrs::enterRegion(MachineBasicBlock *bb, |
| 218 | MachineBasicBlock::iterator begin, |
| 219 | MachineBasicBlock::iterator end, |
Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 220 | unsigned regioninstrs) { |
Andrew Trick | 7405c6d | 2012-04-20 20:05:21 +0000 | [diff] [blame] | 221 | assert(bb == BB && "startBlock should set BB"); |
Andrew Trick | 8c207e4 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 222 | RegionBegin = begin; |
| 223 | RegionEnd = end; |
Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 224 | NumRegionInstrs = regioninstrs; |
Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /// Close the current scheduling region. Don't clear any state in case the |
| 228 | /// driver wants to refer to the previous scheduling region. |
| 229 | void ScheduleDAGInstrs::exitRegion() { |
| 230 | // Nothing to do. |
| 231 | } |
| 232 | |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 233 | /// addSchedBarrierDeps - Add dependencies from instructions in the current |
Evan Cheng | 15459b6 | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 234 | /// list of instructions being scheduled to scheduling barrier by adding |
| 235 | /// the exit SU to the register defs and use list. This is because we want to |
| 236 | /// make sure instructions which define registers that are either used by |
| 237 | /// the terminator or are live-out are properly scheduled. This is |
| 238 | /// especially important when the definition latency of the return value(s) |
| 239 | /// are too high to be hidden by the branch or when the liveout registers |
| 240 | /// used by instructions in the fallthrough block. |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 241 | void ScheduleDAGInstrs::addSchedBarrierDeps() { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 242 | MachineInstr *ExitMI = RegionEnd != BB->end() ? &*RegionEnd : nullptr; |
Evan Cheng | 15459b6 | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 243 | ExitSU.setInstr(ExitMI); |
| 244 | bool AllDepKnown = ExitMI && |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 245 | (ExitMI->isCall() || ExitMI->isBarrier()); |
Evan Cheng | 15459b6 | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 246 | if (ExitMI && AllDepKnown) { |
| 247 | // If it's a call or a barrier, add dependencies on the defs and uses of |
| 248 | // instruction. |
| 249 | for (unsigned i = 0, e = ExitMI->getNumOperands(); i != e; ++i) { |
| 250 | const MachineOperand &MO = ExitMI->getOperand(i); |
| 251 | if (!MO.isReg() || MO.isDef()) continue; |
| 252 | unsigned Reg = MO.getReg(); |
| 253 | if (Reg == 0) continue; |
| 254 | |
Andrew Trick | 59ac4fb | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 255 | if (TRI->isPhysicalRegister(Reg)) |
Michael Ilseman | 3e3194f | 2013-01-21 18:18:53 +0000 | [diff] [blame] | 256 | Uses.insert(PhysRegSUOper(&ExitSU, -1, Reg)); |
Matthias Braun | 93563e7 | 2015-11-03 01:53:29 +0000 | [diff] [blame] | 257 | else if (MO.readsReg()) // ignore undef operands |
| 258 | addVRegUseDeps(&ExitSU, i); |
Evan Cheng | 15459b6 | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 259 | } |
| 260 | } else { |
| 261 | // For others, e.g. fallthrough, conditional branch, assume the exit |
Evan Cheng | cbdf7e8 | 2010-10-27 23:17:17 +0000 | [diff] [blame] | 262 | // uses all the registers that are livein to the successor blocks. |
Benjamin Kramer | 411d5a2 | 2012-03-16 17:38:19 +0000 | [diff] [blame] | 263 | assert(Uses.empty() && "Uses in set before adding deps?"); |
Evan Cheng | cbdf7e8 | 2010-10-27 23:17:17 +0000 | [diff] [blame] | 264 | for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), |
| 265 | SE = BB->succ_end(); SI != SE; ++SI) |
Matthias Braun | d9da162 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 266 | for (const auto &LI : (*SI)->liveins()) { |
| 267 | if (!Uses.contains(LI.PhysReg)) |
| 268 | Uses.insert(PhysRegSUOper(&ExitSU, -1, LI.PhysReg)); |
Evan Cheng | cbdf7e8 | 2010-10-27 23:17:17 +0000 | [diff] [blame] | 269 | } |
Evan Cheng | 15459b6 | 2010-10-23 02:10:46 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
Andrew Trick | d675a4c | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 273 | /// MO is an operand of SU's instruction that defines a physical register. Add |
| 274 | /// data dependencies from SU to any uses of the physical register. |
Andrew Trick | ae53561 | 2012-08-23 00:39:43 +0000 | [diff] [blame] | 275 | void ScheduleDAGInstrs::addPhysRegDataDeps(SUnit *SU, unsigned OperIdx) { |
| 276 | const MachineOperand &MO = SU->getInstr()->getOperand(OperIdx); |
Andrew Trick | d675a4c | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 277 | assert(MO.isDef() && "expect physreg def"); |
| 278 | |
| 279 | // Ask the target if address-backscheduling is desirable, and if so how much. |
Eric Christopher | 2c63549 | 2015-01-27 07:54:39 +0000 | [diff] [blame] | 280 | const TargetSubtargetInfo &ST = MF.getSubtarget(); |
Andrew Trick | d675a4c | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 281 | |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 282 | for (MCRegAliasIterator Alias(MO.getReg(), TRI, true); |
| 283 | Alias.isValid(); ++Alias) { |
Andrew Trick | 9dbbd3e | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 284 | if (!Uses.contains(*Alias)) |
Andrew Trick | d675a4c | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 285 | continue; |
Michael Ilseman | 3e3194f | 2013-01-21 18:18:53 +0000 | [diff] [blame] | 286 | for (Reg2SUnitsMap::iterator I = Uses.find(*Alias); I != Uses.end(); ++I) { |
| 287 | SUnit *UseSU = I->SU; |
Andrew Trick | d675a4c | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 288 | if (UseSU == SU) |
| 289 | continue; |
Andrew Trick | 07dced6 | 2012-10-08 18:54:00 +0000 | [diff] [blame] | 290 | |
Andrew Trick | 07dced6 | 2012-10-08 18:54:00 +0000 | [diff] [blame] | 291 | // Adjust the dependence latency using operand def/use information, |
| 292 | // then allow the target to perform its own adjustments. |
Michael Ilseman | 3e3194f | 2013-01-21 18:18:53 +0000 | [diff] [blame] | 293 | int UseOp = I->OpIdx; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 294 | MachineInstr *RegUse = nullptr; |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 295 | SDep Dep; |
| 296 | if (UseOp < 0) |
| 297 | Dep = SDep(SU, SDep::Artificial); |
| 298 | else { |
Andrew Trick | e833e1c | 2013-04-13 06:07:40 +0000 | [diff] [blame] | 299 | // Set the hasPhysRegDefs only for physreg defs that have a use within |
| 300 | // the scheduling region. |
| 301 | SU->hasPhysRegDefs = true; |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 302 | Dep = SDep(SU, SDep::Data, *Alias); |
| 303 | RegUse = UseSU->getInstr(); |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 304 | } |
| 305 | Dep.setLatency( |
Andrew Trick | de2109e | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 306 | SchedModel.computeOperandLatency(SU->getInstr(), OperIdx, RegUse, |
| 307 | UseOp)); |
Andrew Trick | 4544606 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 308 | |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 309 | ST.adjustSchedDependency(SU, UseSU, Dep); |
| 310 | UseSU->addPred(Dep); |
Andrew Trick | d675a4c | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
Andrew Trick | dbee9d8 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 315 | /// addPhysRegDeps - Add register dependencies (data, anti, and output) from |
| 316 | /// this SUnit to following instructions in the same scheduling region that |
| 317 | /// depend the physical register referenced at OperIdx. |
| 318 | void ScheduleDAGInstrs::addPhysRegDeps(SUnit *SU, unsigned OperIdx) { |
Andrew Trick | 6b104f8 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 319 | MachineInstr *MI = SU->getInstr(); |
| 320 | MachineOperand &MO = MI->getOperand(OperIdx); |
Andrew Trick | dbee9d8 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 321 | |
| 322 | // Optionally add output and anti dependencies. For anti |
| 323 | // dependencies we use a latency of 0 because for a multi-issue |
| 324 | // target we want to allow the defining instruction to issue |
| 325 | // in the same cycle as the using instruction. |
| 326 | // TODO: Using a latency of 1 here for output dependencies assumes |
| 327 | // there's no cost for reusing registers. |
| 328 | SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output; |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 329 | for (MCRegAliasIterator Alias(MO.getReg(), TRI, true); |
| 330 | Alias.isValid(); ++Alias) { |
Andrew Trick | 9dbbd3e | 2012-02-24 07:04:55 +0000 | [diff] [blame] | 331 | if (!Defs.contains(*Alias)) |
Andrew Trick | d675a4c | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 332 | continue; |
Michael Ilseman | 3e3194f | 2013-01-21 18:18:53 +0000 | [diff] [blame] | 333 | for (Reg2SUnitsMap::iterator I = Defs.find(*Alias); I != Defs.end(); ++I) { |
| 334 | SUnit *DefSU = I->SU; |
Andrew Trick | dbee9d8 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 335 | if (DefSU == &ExitSU) |
| 336 | continue; |
| 337 | if (DefSU != SU && |
| 338 | (Kind != SDep::Output || !MO.isDead() || |
Hal Finkel | 66d7791 | 2014-12-05 02:07:35 +0000 | [diff] [blame] | 339 | !DefSU->getInstr()->registerDefIsDead(*Alias))) { |
Andrew Trick | dbee9d8 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 340 | if (Kind == SDep::Anti) |
Andrew Trick | baeaabb | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 341 | DefSU->addPred(SDep(SU, Kind, /*Reg=*/*Alias)); |
Andrew Trick | dbee9d8 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 342 | else { |
Andrew Trick | baeaabb | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 343 | SDep Dep(SU, Kind, /*Reg=*/*Alias); |
Andrew Trick | de2109e | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 344 | Dep.setLatency( |
| 345 | SchedModel.computeOutputLatency(MI, OperIdx, DefSU->getInstr())); |
Andrew Trick | baeaabb | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 346 | DefSU->addPred(Dep); |
Andrew Trick | dbee9d8 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
Andrew Trick | d675a4c | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 352 | if (!MO.isDef()) { |
Andrew Trick | e833e1c | 2013-04-13 06:07:40 +0000 | [diff] [blame] | 353 | SU->hasPhysRegUses = true; |
Andrew Trick | d675a4c | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 354 | // Either insert a new Reg2SUnits entry with an empty SUnits list, or |
| 355 | // retrieve the existing SUnits list for this register's uses. |
| 356 | // Push this SUnit on the use list. |
Michael Ilseman | 3e3194f | 2013-01-21 18:18:53 +0000 | [diff] [blame] | 357 | Uses.insert(PhysRegSUOper(SU, OperIdx, MO.getReg())); |
Andrew Trick | 6b104f8 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 358 | if (RemoveKillFlags) |
| 359 | MO.setIsKill(false); |
Andrew Trick | d675a4c | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 360 | } |
| 361 | else { |
Andrew Trick | ae53561 | 2012-08-23 00:39:43 +0000 | [diff] [blame] | 362 | addPhysRegDataDeps(SU, OperIdx); |
Michael Ilseman | 3e3194f | 2013-01-21 18:18:53 +0000 | [diff] [blame] | 363 | unsigned Reg = MO.getReg(); |
Andrew Trick | dbee9d8 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 364 | |
Andrew Trick | d675a4c | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 365 | // clear this register's use list |
Michael Ilseman | 3e3194f | 2013-01-21 18:18:53 +0000 | [diff] [blame] | 366 | if (Uses.contains(Reg)) |
| 367 | Uses.eraseAll(Reg); |
Andrew Trick | d675a4c | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 368 | |
Michael Ilseman | 3e3194f | 2013-01-21 18:18:53 +0000 | [diff] [blame] | 369 | if (!MO.isDead()) { |
| 370 | Defs.eraseAll(Reg); |
| 371 | } else if (SU->isCall) { |
| 372 | // Calls will not be reordered because of chain dependencies (see |
| 373 | // below). Since call operands are dead, calls may continue to be added |
| 374 | // to the DefList making dependence checking quadratic in the size of |
| 375 | // the block. Instead, we leave only one call at the back of the |
| 376 | // DefList. |
| 377 | Reg2SUnitsMap::RangePair P = Defs.equal_range(Reg); |
| 378 | Reg2SUnitsMap::iterator B = P.first; |
| 379 | Reg2SUnitsMap::iterator I = P.second; |
| 380 | for (bool isBegin = I == B; !isBegin; /* empty */) { |
| 381 | isBegin = (--I) == B; |
| 382 | if (!I->SU->isCall) |
| 383 | break; |
| 384 | I = Defs.erase(I); |
| 385 | } |
Andrew Trick | dbee9d8 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 386 | } |
Michael Ilseman | 3e3194f | 2013-01-21 18:18:53 +0000 | [diff] [blame] | 387 | |
Andrew Trick | d675a4c | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 388 | // Defs are pushed in the order they are visited and never reordered. |
Michael Ilseman | 3e3194f | 2013-01-21 18:18:53 +0000 | [diff] [blame] | 389 | Defs.insert(PhysRegSUOper(SU, OperIdx, Reg)); |
Andrew Trick | dbee9d8 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Matthias Braun | 97d0ffb | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 393 | LaneBitmask ScheduleDAGInstrs::getLaneMaskForMO(const MachineOperand &MO) const |
| 394 | { |
| 395 | unsigned Reg = MO.getReg(); |
| 396 | // No point in tracking lanemasks if we don't have interesting subregisters. |
| 397 | const TargetRegisterClass &RC = *MRI.getRegClass(Reg); |
| 398 | if (!RC.HasDisjunctSubRegs) |
| 399 | return ~0u; |
| 400 | |
| 401 | unsigned SubReg = MO.getSubReg(); |
| 402 | if (SubReg == 0) |
| 403 | return RC.getLaneMask(); |
| 404 | return TRI->getSubRegIndexLaneMask(SubReg); |
| 405 | } |
| 406 | |
Andrew Trick | 59ac4fb | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 407 | /// addVRegDefDeps - Add register output and data dependencies from this SUnit |
| 408 | /// to instructions that occur later in the same scheduling region if they read |
| 409 | /// from or write to the virtual register defined at OperIdx. |
| 410 | /// |
| 411 | /// TODO: Hoist loop induction variable increments. This has to be |
| 412 | /// reevaluated. Generally, IV scheduling should be done before coalescing. |
| 413 | void ScheduleDAGInstrs::addVRegDefDeps(SUnit *SU, unsigned OperIdx) { |
Matthias Braun | 97d0ffb | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 414 | MachineInstr *MI = SU->getInstr(); |
| 415 | MachineOperand &MO = MI->getOperand(OperIdx); |
| 416 | unsigned Reg = MO.getReg(); |
Andrew Trick | 59ac4fb | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 417 | |
Matthias Braun | 97d0ffb | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 418 | LaneBitmask DefLaneMask; |
| 419 | LaneBitmask KillLaneMask; |
| 420 | if (TrackLaneMasks) { |
| 421 | bool IsKill = MO.getSubReg() == 0 || MO.isUndef(); |
| 422 | DefLaneMask = getLaneMaskForMO(MO); |
| 423 | // If we have a <read-undef> flag, none of the lane values comes from an |
| 424 | // earlier instruction. |
| 425 | KillLaneMask = IsKill ? ~0u : DefLaneMask; |
| 426 | |
| 427 | // Clear undef flag, we'll re-add it later once we know which subregister |
| 428 | // Def is first. |
| 429 | MO.setIsUndef(false); |
| 430 | } else { |
| 431 | DefLaneMask = ~0u; |
| 432 | KillLaneMask = ~0u; |
| 433 | } |
| 434 | |
| 435 | if (MO.isDead()) { |
| 436 | assert(CurrentVRegUses.find(Reg) == CurrentVRegUses.end() && |
| 437 | "Dead defs should have no uses"); |
| 438 | } else { |
| 439 | // Add data dependence to all uses we found so far. |
| 440 | const TargetSubtargetInfo &ST = MF.getSubtarget(); |
| 441 | for (VReg2SUnitOperIdxMultiMap::iterator I = CurrentVRegUses.find(Reg), |
| 442 | E = CurrentVRegUses.end(); I != E; /*empty*/) { |
| 443 | LaneBitmask LaneMask = I->LaneMask; |
| 444 | // Ignore uses of other lanes. |
| 445 | if ((LaneMask & KillLaneMask) == 0) { |
| 446 | ++I; |
| 447 | continue; |
| 448 | } |
| 449 | |
| 450 | if ((LaneMask & DefLaneMask) != 0) { |
| 451 | SUnit *UseSU = I->SU; |
| 452 | MachineInstr *Use = UseSU->getInstr(); |
| 453 | SDep Dep(SU, SDep::Data, Reg); |
| 454 | Dep.setLatency(SchedModel.computeOperandLatency(MI, OperIdx, Use, |
| 455 | I->OperandIndex)); |
| 456 | ST.adjustSchedDependency(SU, UseSU, Dep); |
| 457 | UseSU->addPred(Dep); |
| 458 | } |
| 459 | |
| 460 | LaneMask &= ~KillLaneMask; |
| 461 | // If we found a Def for all lanes of this use, remove it from the list. |
| 462 | if (LaneMask != 0) { |
| 463 | I->LaneMask = LaneMask; |
| 464 | ++I; |
| 465 | } else |
| 466 | I = CurrentVRegUses.erase(I); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | // Shortcut: Singly defined vregs do not have output/anti dependencies. |
Andrew Trick | 7979589 | 2012-07-30 23:48:17 +0000 | [diff] [blame] | 471 | if (MRI.hasOneDef(Reg)) |
Andrew Trick | 9405343 | 2012-07-28 01:48:15 +0000 | [diff] [blame] | 472 | return; |
Andrew Trick | db42c6f | 2012-02-22 06:08:13 +0000 | [diff] [blame] | 473 | |
Matthias Braun | 97d0ffb | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 474 | // Add output dependence to the next nearest defs of this vreg. |
Andrew Trick | 59ac4fb | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 475 | // |
| 476 | // Unless this definition is dead, the output dependence should be |
| 477 | // transitively redundant with antidependencies from this definition's |
| 478 | // uses. We're conservative for now until we have a way to guarantee the uses |
| 479 | // are not eliminated sometime during scheduling. The output dependence edge |
| 480 | // is also useful if output latency exceeds def-use latency. |
Matthias Braun | 97d0ffb | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 481 | LaneBitmask LaneMask = DefLaneMask; |
| 482 | for (VReg2SUnit &V2SU : make_range(CurrentVRegDefs.find(Reg), |
| 483 | CurrentVRegDefs.end())) { |
| 484 | // Ignore defs for other lanes. |
| 485 | if ((V2SU.LaneMask & LaneMask) == 0) |
| 486 | continue; |
| 487 | // Add an output dependence. |
| 488 | SUnit *DefSU = V2SU.SU; |
| 489 | // Ignore additional defs of the same lanes in one instruction. This can |
| 490 | // happen because lanemasks are shared for targets with too many |
| 491 | // subregisters. We also use some representration tricks/hacks where we |
| 492 | // add super-register defs/uses, to imply that although we only access parts |
| 493 | // of the reg we care about the full one. |
| 494 | if (DefSU == SU) |
| 495 | continue; |
| 496 | SDep Dep(SU, SDep::Output, Reg); |
| 497 | Dep.setLatency( |
| 498 | SchedModel.computeOutputLatency(MI, OperIdx, DefSU->getInstr())); |
| 499 | DefSU->addPred(Dep); |
| 500 | |
| 501 | // Update current definition. This can get tricky if the def was about a |
| 502 | // bigger lanemask before. We then have to shrink it and create a new |
| 503 | // VReg2SUnit for the non-overlapping part. |
| 504 | LaneBitmask OverlapMask = V2SU.LaneMask & LaneMask; |
| 505 | LaneBitmask NonOverlapMask = V2SU.LaneMask & ~LaneMask; |
| 506 | if (NonOverlapMask != 0) |
| 507 | CurrentVRegDefs.insert(VReg2SUnit(Reg, NonOverlapMask, V2SU.SU)); |
| 508 | V2SU.SU = SU; |
| 509 | V2SU.LaneMask = OverlapMask; |
Andrew Trick | 59ac4fb | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 510 | } |
Matthias Braun | 97d0ffb | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 511 | // If there was no CurrentVRegDefs entry for some lanes yet, create one. |
| 512 | if (LaneMask != 0) |
| 513 | CurrentVRegDefs.insert(VReg2SUnit(Reg, LaneMask, SU)); |
Andrew Trick | 59ac4fb | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Andrew Trick | 46cc9a4 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 516 | /// addVRegUseDeps - Add a register data dependency if the instruction that |
| 517 | /// defines the virtual register used at OperIdx is mapped to an SUnit. Add a |
| 518 | /// register antidependency from this SUnit to instructions that occur later in |
| 519 | /// the same scheduling region if they write the virtual register. |
| 520 | /// |
| 521 | /// TODO: Handle ExitSU "uses" properly. |
Andrew Trick | 59ac4fb | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 522 | void ScheduleDAGInstrs::addVRegUseDeps(SUnit *SU, unsigned OperIdx) { |
Matthias Braun | 97d0ffb | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 523 | const MachineInstr *MI = SU->getInstr(); |
| 524 | const MachineOperand &MO = MI->getOperand(OperIdx); |
| 525 | unsigned Reg = MO.getReg(); |
Andrew Trick | 46cc9a4 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 526 | |
Matthias Braun | 97d0ffb | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 527 | // Remember the use. Data dependencies will be added when we find the def. |
| 528 | LaneBitmask LaneMask = TrackLaneMasks ? getLaneMaskForMO(MO) : ~0u; |
| 529 | CurrentVRegUses.insert(VReg2SUnitOperIdx(Reg, LaneMask, OperIdx, SU)); |
| 530 | |
| 531 | // Add antidependences to the following defs of the vreg. |
| 532 | for (VReg2SUnit &V2SU : make_range(CurrentVRegDefs.find(Reg), |
| 533 | CurrentVRegDefs.end())) { |
| 534 | // Ignore defs for unrelated lanes. |
| 535 | LaneBitmask PrevDefLaneMask = V2SU.LaneMask; |
| 536 | if ((PrevDefLaneMask & LaneMask) == 0) |
| 537 | continue; |
| 538 | if (V2SU.SU == SU) |
| 539 | continue; |
| 540 | |
| 541 | V2SU.SU->addPred(SDep(SU, SDep::Anti, Reg)); |
Andrew Trick | 2bc74c2 | 2013-08-30 04:36:57 +0000 | [diff] [blame] | 542 | } |
Andrew Trick | 46cc9a4 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 543 | } |
Andrew Trick | 59ac4fb | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 544 | |
Andrew Trick | da01ba3 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 545 | /// Return true if MI is an instruction we are unable to reason about |
| 546 | /// (like a call or something with unmodeled side effects). |
| 547 | static inline bool isGlobalMemoryObject(AliasAnalysis *AA, MachineInstr *MI) { |
Rafael Espindola | 84921b9 | 2015-10-24 23:11:13 +0000 | [diff] [blame] | 548 | return MI->isCall() || MI->hasUnmodeledSideEffects() || |
Chad Rosier | b46d0f9 | 2016-01-26 19:33:57 +0000 | [diff] [blame] | 549 | (MI->hasOrderedMemoryRef() && !MI->isInvariantLoad(AA)); |
Andrew Trick | da01ba3 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 552 | /// This returns true if the two MIs need a chain edge between them. |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 553 | /// This is called on normal stores and loads. |
Andrew Trick | da01ba3 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 554 | static bool MIsNeedChainEdge(AliasAnalysis *AA, const MachineFrameInfo *MFI, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 555 | const DataLayout &DL, MachineInstr *MIa, |
Andrew Trick | da01ba3 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 556 | MachineInstr *MIb) { |
Chad Rosier | 3528c1e | 2014-09-08 14:43:48 +0000 | [diff] [blame] | 557 | const MachineFunction *MF = MIa->getParent()->getParent(); |
| 558 | const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); |
| 559 | |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 560 | assert ((MIa->mayStore() || MIb->mayStore()) && |
| 561 | "Dependency checked between two loads"); |
| 562 | |
Jonas Paulsson | 8c73863 | 2016-01-29 17:22:43 +0000 | [diff] [blame] | 563 | // Let the target decide if memory accesses cannot possibly overlap. |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 564 | if (TII->areMemAccessesTriviallyDisjoint(MIa, MIb, AA)) |
| 565 | return false; |
Andrew Trick | da01ba3 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 566 | |
Andrew Trick | da01ba3 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 567 | // To this point analysis is generic. From here on we do need AA. |
| 568 | if (!AA) |
| 569 | return true; |
| 570 | |
Jonas Paulsson | 98963fe | 2016-02-15 16:43:15 +0000 | [diff] [blame] | 571 | // FIXME: Need to handle multiple memory operands to support all targets. |
| 572 | if (!MIa->hasOneMemOperand() || !MIb->hasOneMemOperand()) |
| 573 | return true; |
| 574 | |
Andrew Trick | da01ba3 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 575 | MachineMemOperand *MMOa = *MIa->memoperands_begin(); |
| 576 | MachineMemOperand *MMOb = *MIb->memoperands_begin(); |
| 577 | |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 578 | if (!MMOa->getValue() || !MMOb->getValue()) |
| 579 | return true; |
| 580 | |
Andrew Trick | da01ba3 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 581 | // The following interface to AA is fashioned after DAGCombiner::isAlias |
| 582 | // and operates with MachineMemOperand offset with some important |
| 583 | // assumptions: |
| 584 | // - LLVM fundamentally assumes flat address spaces. |
| 585 | // - MachineOperand offset can *only* result from legalization and |
| 586 | // cannot affect queries other than the trivial case of overlap |
| 587 | // checking. |
| 588 | // - These offsets never wrap and never step outside |
| 589 | // of allocated objects. |
| 590 | // - There should never be any negative offsets here. |
| 591 | // |
| 592 | // FIXME: Modify API to hide this math from "user" |
| 593 | // FIXME: Even before we go to AA we can reason locally about some |
| 594 | // memory objects. It can save compile time, and possibly catch some |
| 595 | // corner cases not currently covered. |
| 596 | |
| 597 | assert ((MMOa->getOffset() >= 0) && "Negative MachineMemOperand offset"); |
| 598 | assert ((MMOb->getOffset() >= 0) && "Negative MachineMemOperand offset"); |
| 599 | |
| 600 | int64_t MinOffset = std::min(MMOa->getOffset(), MMOb->getOffset()); |
| 601 | int64_t Overlapa = MMOa->getSize() + MMOa->getOffset() - MinOffset; |
| 602 | int64_t Overlapb = MMOb->getSize() + MMOb->getOffset() - MinOffset; |
| 603 | |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 604 | AliasResult AAResult = |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 605 | AA->alias(MemoryLocation(MMOa->getValue(), Overlapa, |
| 606 | UseTBAA ? MMOa->getAAInfo() : AAMDNodes()), |
| 607 | MemoryLocation(MMOb->getValue(), Overlapb, |
| 608 | UseTBAA ? MMOb->getAAInfo() : AAMDNodes())); |
Andrew Trick | da01ba3 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 609 | |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 610 | return (AAResult != NoAlias); |
Andrew Trick | da01ba3 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 613 | /// Check whether two objects need a chain edge and add it if needed. |
| 614 | void ScheduleDAGInstrs::addChainDependency (SUnit *SUa, SUnit *SUb, |
| 615 | unsigned Latency) { |
| 616 | if (MIsNeedChainEdge(AAForDep, MFI, MF.getDataLayout(), SUa->getInstr(), |
| 617 | SUb->getInstr())) { |
| 618 | SDep Dep(SUa, SDep::MayAliasMem); |
| 619 | Dep.setLatency(Latency); |
Andrew Trick | baeaabb | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 620 | SUb->addPred(Dep); |
| 621 | } |
Andrew Trick | da01ba3 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 624 | /// Create an SUnit for each real instruction, numbered in top-down topological |
Andrew Trick | 46cc9a4 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 625 | /// order. The instruction order A < B, implies that no edge exists from B to A. |
| 626 | /// |
| 627 | /// Map each real instruction to its SUnit. |
| 628 | /// |
Andrew Trick | 8823dec | 2012-03-14 04:00:41 +0000 | [diff] [blame] | 629 | /// After initSUnits, the SUnits vector cannot be resized and the scheduler may |
| 630 | /// hang onto SUnit pointers. We may relax this in the future by using SUnit IDs |
| 631 | /// instead of pointers. |
| 632 | /// |
| 633 | /// MachineScheduler relies on initSUnits numbering the nodes by their order in |
| 634 | /// the original instruction list. |
Andrew Trick | 46cc9a4 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 635 | void ScheduleDAGInstrs::initSUnits() { |
| 636 | // We'll be allocating one SUnit for each real instruction in the region, |
| 637 | // which is contained within a basic block. |
Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 638 | SUnits.reserve(NumRegionInstrs); |
Andrew Trick | 46cc9a4 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 639 | |
Andrew Trick | 8c207e4 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 640 | for (MachineBasicBlock::iterator I = RegionBegin; I != RegionEnd; ++I) { |
Andrew Trick | 46cc9a4 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 641 | MachineInstr *MI = I; |
| 642 | if (MI->isDebugValue()) |
| 643 | continue; |
| 644 | |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 645 | SUnit *SU = newSUnit(MI); |
Andrew Trick | 46cc9a4 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 646 | MISUnitMap[MI] = SU; |
| 647 | |
| 648 | SU->isCall = MI->isCall(); |
| 649 | SU->isCommutable = MI->isCommutable(); |
| 650 | |
| 651 | // Assign the Latency field of SU using target-provided information. |
Andrew Trick | dd79f0f | 2012-10-10 05:43:09 +0000 | [diff] [blame] | 652 | SU->Latency = SchedModel.computeInstrLatency(SU->getInstr()); |
Andrew Trick | 880e573 | 2013-12-05 17:55:58 +0000 | [diff] [blame] | 653 | |
Andrew Trick | 1766f93 | 2014-04-18 17:35:08 +0000 | [diff] [blame] | 654 | // If this SUnit uses a reserved or unbuffered resource, mark it as such. |
| 655 | // |
Alp Toker | beaca19 | 2014-05-15 01:52:21 +0000 | [diff] [blame] | 656 | // Reserved resources block an instruction from issuing and stall the |
Andrew Trick | 1766f93 | 2014-04-18 17:35:08 +0000 | [diff] [blame] | 657 | // entire pipeline. These are identified by BufferSize=0. |
| 658 | // |
Alp Toker | beaca19 | 2014-05-15 01:52:21 +0000 | [diff] [blame] | 659 | // Unbuffered resources prevent execution of subsequent instructions that |
Andrew Trick | 1766f93 | 2014-04-18 17:35:08 +0000 | [diff] [blame] | 660 | // require the same resources. This is used for in-order execution pipelines |
| 661 | // within an out-of-order core. These are identified by BufferSize=1. |
Andrew Trick | 880e573 | 2013-12-05 17:55:58 +0000 | [diff] [blame] | 662 | if (SchedModel.hasInstrSchedModel()) { |
| 663 | const MCSchedClassDesc *SC = getSchedClass(SU); |
| 664 | for (TargetSchedModel::ProcResIter |
| 665 | PI = SchedModel.getWriteProcResBegin(SC), |
| 666 | PE = SchedModel.getWriteProcResEnd(SC); PI != PE; ++PI) { |
Andrew Trick | 5a22df4 | 2013-12-05 17:56:02 +0000 | [diff] [blame] | 667 | switch (SchedModel.getProcResource(PI->ProcResourceIdx)->BufferSize) { |
| 668 | case 0: |
| 669 | SU->hasReservedResource = true; |
| 670 | break; |
| 671 | case 1: |
Andrew Trick | 880e573 | 2013-12-05 17:55:58 +0000 | [diff] [blame] | 672 | SU->isUnbuffered = true; |
| 673 | break; |
Andrew Trick | 5a22df4 | 2013-12-05 17:56:02 +0000 | [diff] [blame] | 674 | default: |
| 675 | break; |
Andrew Trick | 880e573 | 2013-12-05 17:55:58 +0000 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | } |
Andrew Trick | 46cc9a4 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 679 | } |
Andrew Trick | dbee9d8 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Matthias Braun | 97d0ffb | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 682 | void ScheduleDAGInstrs::collectVRegUses(SUnit *SU) { |
| 683 | const MachineInstr *MI = SU->getInstr(); |
| 684 | for (const MachineOperand &MO : MI->operands()) { |
| 685 | if (!MO.isReg()) |
| 686 | continue; |
| 687 | if (!MO.readsReg()) |
| 688 | continue; |
| 689 | if (TrackLaneMasks && !MO.isUse()) |
| 690 | continue; |
| 691 | |
| 692 | unsigned Reg = MO.getReg(); |
| 693 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
| 694 | continue; |
| 695 | |
Matthias Braun | d4f6409 | 2016-01-20 00:23:32 +0000 | [diff] [blame] | 696 | // Ignore re-defs. |
| 697 | if (TrackLaneMasks) { |
| 698 | bool FoundDef = false; |
| 699 | for (const MachineOperand &MO2 : MI->operands()) { |
| 700 | if (MO2.isReg() && MO2.isDef() && MO2.getReg() == Reg && !MO2.isDead()) { |
| 701 | FoundDef = true; |
| 702 | break; |
| 703 | } |
| 704 | } |
| 705 | if (FoundDef) |
| 706 | continue; |
| 707 | } |
| 708 | |
Matthias Braun | 97d0ffb | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 709 | // Record this local VReg use. |
| 710 | VReg2SUnitMultiMap::iterator UI = VRegUses.find(Reg); |
| 711 | for (; UI != VRegUses.end(); ++UI) { |
| 712 | if (UI->SU == SU) |
| 713 | break; |
| 714 | } |
| 715 | if (UI == VRegUses.end()) |
| 716 | VRegUses.insert(VReg2SUnit(Reg, 0, SU)); |
| 717 | } |
| 718 | } |
| 719 | |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 720 | class ScheduleDAGInstrs::Value2SUsMap : public MapVector<ValueType, SUList> { |
| 721 | |
| 722 | /// Current total number of SUs in map. |
| 723 | unsigned NumNodes; |
| 724 | |
| 725 | /// 1 for loads, 0 for stores. (see comment in SUList) |
| 726 | unsigned TrueMemOrderLatency; |
| 727 | public: |
| 728 | |
| 729 | Value2SUsMap(unsigned lat = 0) : NumNodes(0), TrueMemOrderLatency(lat) {} |
| 730 | |
| 731 | /// To keep NumNodes up to date, insert() is used instead of |
| 732 | /// this operator w/ push_back(). |
| 733 | ValueType &operator[](const SUList &Key) { |
| 734 | llvm_unreachable("Don't use. Use insert() instead."); }; |
| 735 | |
| 736 | /// Add SU to the SUList of V. If Map grows huge, reduce its size |
| 737 | /// by calling reduce(). |
| 738 | void inline insert(SUnit *SU, ValueType V) { |
| 739 | MapVector::operator[](V).push_back(SU); |
| 740 | NumNodes++; |
| 741 | } |
| 742 | |
| 743 | /// Clears the list of SUs mapped to V. |
| 744 | void inline clearList(ValueType V) { |
| 745 | iterator Itr = find(V); |
| 746 | if (Itr != end()) { |
| 747 | assert (NumNodes >= Itr->second.size()); |
| 748 | NumNodes -= Itr->second.size(); |
| 749 | |
| 750 | Itr->second.clear(); |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | /// Clears map from all contents. |
| 755 | void clear() { |
| 756 | MapVector<ValueType, SUList>::clear(); |
| 757 | NumNodes = 0; |
| 758 | } |
| 759 | |
| 760 | unsigned inline size() const { return NumNodes; } |
| 761 | |
| 762 | /// Count the number of SUs in this map after a reduction. |
| 763 | void reComputeSize(void) { |
| 764 | NumNodes = 0; |
| 765 | for (auto &I : *this) |
| 766 | NumNodes += I.second.size(); |
| 767 | } |
| 768 | |
| 769 | unsigned inline getTrueMemOrderLatency() const { |
| 770 | return TrueMemOrderLatency; |
| 771 | } |
| 772 | |
| 773 | void dump(); |
| 774 | }; |
| 775 | |
| 776 | void ScheduleDAGInstrs::addChainDependencies(SUnit *SU, |
| 777 | Value2SUsMap &Val2SUsMap) { |
| 778 | for (auto &I : Val2SUsMap) |
| 779 | addChainDependencies(SU, I.second, |
| 780 | Val2SUsMap.getTrueMemOrderLatency()); |
| 781 | } |
| 782 | |
| 783 | void ScheduleDAGInstrs::addChainDependencies(SUnit *SU, |
| 784 | Value2SUsMap &Val2SUsMap, |
| 785 | ValueType V) { |
| 786 | Value2SUsMap::iterator Itr = Val2SUsMap.find(V); |
| 787 | if (Itr != Val2SUsMap.end()) |
| 788 | addChainDependencies(SU, Itr->second, |
| 789 | Val2SUsMap.getTrueMemOrderLatency()); |
| 790 | } |
| 791 | |
| 792 | void ScheduleDAGInstrs::addBarrierChain(Value2SUsMap &map) { |
| 793 | assert (BarrierChain != nullptr); |
| 794 | |
| 795 | for (auto &I : map) { |
| 796 | SUList &sus = I.second; |
| 797 | for (auto *SU : sus) |
| 798 | SU->addPredBarrier(BarrierChain); |
| 799 | } |
| 800 | map.clear(); |
| 801 | } |
| 802 | |
| 803 | void ScheduleDAGInstrs::insertBarrierChain(Value2SUsMap &map) { |
| 804 | assert (BarrierChain != nullptr); |
| 805 | |
| 806 | // Go through all lists of SUs. |
| 807 | for (Value2SUsMap::iterator I = map.begin(), EE = map.end(); I != EE;) { |
| 808 | Value2SUsMap::iterator CurrItr = I++; |
| 809 | SUList &sus = CurrItr->second; |
| 810 | SUList::iterator SUItr = sus.begin(), SUEE = sus.end(); |
| 811 | for (; SUItr != SUEE; ++SUItr) { |
| 812 | // Stop on BarrierChain or any instruction above it. |
| 813 | if ((*SUItr)->NodeNum <= BarrierChain->NodeNum) |
| 814 | break; |
| 815 | |
| 816 | (*SUItr)->addPredBarrier(BarrierChain); |
| 817 | } |
| 818 | |
| 819 | // Remove also the BarrierChain from list if present. |
| 820 | if (*SUItr == BarrierChain) |
| 821 | SUItr++; |
| 822 | |
| 823 | // Remove all SUs that are now successors of BarrierChain. |
| 824 | if (SUItr != sus.begin()) |
| 825 | sus.erase(sus.begin(), SUItr); |
| 826 | } |
| 827 | |
| 828 | // Remove all entries with empty su lists. |
| 829 | map.remove_if([&](std::pair<ValueType, SUList> &mapEntry) { |
| 830 | return (mapEntry.second.empty()); }); |
| 831 | |
| 832 | // Recompute the size of the map (NumNodes). |
| 833 | map.reComputeSize(); |
| 834 | } |
| 835 | |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 836 | /// If RegPressure is non-null, compute register pressure as a side effect. The |
Andrew Trick | 8863992 | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 837 | /// DAG builder is an efficient place to do it because it already visits |
| 838 | /// operands. |
| 839 | void ScheduleDAGInstrs::buildSchedGraph(AliasAnalysis *AA, |
Andrew Trick | 1a83134 | 2013-08-30 03:49:48 +0000 | [diff] [blame] | 840 | RegPressureTracker *RPTracker, |
Matthias Braun | 97d0ffb | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 841 | PressureDiffs *PDiffs, |
Matthias Braun | d4f6409 | 2016-01-20 00:23:32 +0000 | [diff] [blame] | 842 | LiveIntervals *LIS, |
Matthias Braun | 97d0ffb | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 843 | bool TrackLaneMasks) { |
Eric Christopher | 2c63549 | 2015-01-27 07:54:39 +0000 | [diff] [blame] | 844 | const TargetSubtargetInfo &ST = MF.getSubtarget(); |
Hal Finkel | b350ffd | 2013-08-29 03:25:05 +0000 | [diff] [blame] | 845 | bool UseAA = EnableAASchedMI.getNumOccurrences() > 0 ? EnableAASchedMI |
| 846 | : ST.useAA(); |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 847 | AAForDep = UseAA ? AA : nullptr; |
| 848 | |
| 849 | BarrierChain = nullptr; |
Hal Finkel | b350ffd | 2013-08-29 03:25:05 +0000 | [diff] [blame] | 850 | |
Matthias Braun | 97d0ffb | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 851 | this->TrackLaneMasks = TrackLaneMasks; |
Andrew Trick | 310190e | 2013-09-04 21:00:02 +0000 | [diff] [blame] | 852 | MISUnitMap.clear(); |
| 853 | ScheduleDAG::clearDAG(); |
| 854 | |
Andrew Trick | 46cc9a4 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 855 | // Create an SUnit for each real instruction. |
| 856 | initSUnits(); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 857 | |
Andrew Trick | 1a83134 | 2013-08-30 03:49:48 +0000 | [diff] [blame] | 858 | if (PDiffs) |
| 859 | PDiffs->init(SUnits.size()); |
| 860 | |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 861 | // We build scheduling units by walking a block's instruction list |
| 862 | // from bottom to top. |
Dan Gohman | 3aab10b | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 863 | |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 864 | // Each MIs' memory operand(s) is analyzed to a list of underlying |
Jonas Paulsson | 2293685 | 2016-02-04 13:08:48 +0000 | [diff] [blame] | 865 | // objects. The SU is then inserted in the SUList(s) mapped from the |
| 866 | // Value(s). Each Value thus gets mapped to lists of SUs depending |
| 867 | // on it, stores and loads kept separately. Two SUs are trivially |
| 868 | // non-aliasing if they both depend on only identified Values and do |
| 869 | // not share any common Value. |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 870 | Value2SUsMap Stores, Loads(1 /*TrueMemOrderLatency*/); |
Dan Gohman | 3aab10b | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 871 | |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 872 | // Certain memory accesses are known to not alias any SU in Stores |
| 873 | // or Loads, and have therefore their own 'NonAlias' |
| 874 | // domain. E.g. spill / reload instructions never alias LLVM I/R |
Jonas Paulsson | 2293685 | 2016-02-04 13:08:48 +0000 | [diff] [blame] | 875 | // Values. It would be nice to assume that this type of memory |
| 876 | // accesses always have a proper memory operand modelling, and are |
| 877 | // therefore never unanalyzable, but this is conservatively not |
| 878 | // done. |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 879 | Value2SUsMap NonAliasStores, NonAliasLoads(1 /*TrueMemOrderLatency*/); |
| 880 | |
| 881 | // Always reduce a huge region with half of the elements, except |
| 882 | // when user sets this number explicitly. |
| 883 | if (ReductionSize.getNumOccurrences() == 0) |
| 884 | ReductionSize = (HugeRegion / 2); |
Dan Gohman | 3aab10b | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 885 | |
Dale Johannesen | 49de060 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 886 | // Remove any stale debug info; sometimes BuildSchedGraph is called again |
| 887 | // without emitting the info from the previous call. |
Devang Patel | e5feef0 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 888 | DbgValues.clear(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 889 | FirstDbgValue = nullptr; |
Dale Johannesen | 49de060 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 890 | |
Andrew Trick | d675a4c | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 891 | assert(Defs.empty() && Uses.empty() && |
| 892 | "Only BuildGraph should update Defs/Uses"); |
Michael Ilseman | 3e3194f | 2013-01-21 18:18:53 +0000 | [diff] [blame] | 893 | Defs.setUniverse(TRI->getNumRegs()); |
| 894 | Uses.setUniverse(TRI->getNumRegs()); |
Andrew Trick | 2e116a4 | 2011-05-06 21:52:52 +0000 | [diff] [blame] | 895 | |
Matthias Braun | 97d0ffb | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 896 | assert(CurrentVRegDefs.empty() && "nobody else should use CurrentVRegDefs"); |
| 897 | assert(CurrentVRegUses.empty() && "nobody else should use CurrentVRegUses"); |
| 898 | unsigned NumVirtRegs = MRI.getNumVirtRegs(); |
| 899 | CurrentVRegDefs.setUniverse(NumVirtRegs); |
| 900 | CurrentVRegUses.setUniverse(NumVirtRegs); |
| 901 | |
Andrew Trick | 8dd26f0 | 2013-08-23 17:48:39 +0000 | [diff] [blame] | 902 | VRegUses.clear(); |
Matthias Braun | 97d0ffb | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 903 | VRegUses.setUniverse(NumVirtRegs); |
Andrew Trick | 59ac4fb | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 904 | |
Andrew Trick | d675a4c | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 905 | // Model data dependencies between instructions being scheduled and the |
| 906 | // ExitSU. |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 907 | addSchedBarrierDeps(); |
Andrew Trick | d675a4c | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 908 | |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 909 | // Walk the list of instructions, from bottom moving up. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 910 | MachineInstr *DbgMI = nullptr; |
Andrew Trick | 8c207e4 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 911 | for (MachineBasicBlock::iterator MII = RegionEnd, MIE = RegionBegin; |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 912 | MII != MIE; --MII) { |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 913 | MachineInstr *MI = std::prev(MII); |
Andrew Trick | b767d1e | 2012-12-01 01:22:49 +0000 | [diff] [blame] | 914 | if (MI && DbgMI) { |
| 915 | DbgValues.push_back(std::make_pair(DbgMI, MI)); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 916 | DbgMI = nullptr; |
Devang Patel | e5feef0 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Dale Johannesen | 49de060 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 919 | if (MI->isDebugValue()) { |
Andrew Trick | b767d1e | 2012-12-01 01:22:49 +0000 | [diff] [blame] | 920 | DbgMI = MI; |
Dale Johannesen | 49de060 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 921 | continue; |
| 922 | } |
Andrew Trick | 1a83134 | 2013-08-30 03:49:48 +0000 | [diff] [blame] | 923 | SUnit *SU = MISUnitMap[MI]; |
| 924 | assert(SU && "No SUnit mapped to this MI"); |
| 925 | |
Andrew Trick | 8863992 | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 926 | if (RPTracker) { |
Matthias Braun | 97d0ffb | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 927 | collectVRegUses(SU); |
Matthias Braun | b505c76 | 2016-01-12 22:57:35 +0000 | [diff] [blame] | 928 | |
| 929 | RegisterOperands RegOpers; |
Matthias Braun | 5d45861 | 2016-01-20 00:23:26 +0000 | [diff] [blame] | 930 | RegOpers.collect(*MI, *TRI, MRI, TrackLaneMasks, false); |
Matthias Braun | d4f6409 | 2016-01-20 00:23:32 +0000 | [diff] [blame] | 931 | if (TrackLaneMasks) { |
Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 932 | SlotIndex SlotIdx = LIS->getInstructionIndex(*MI); |
Matthias Braun | d4f6409 | 2016-01-20 00:23:32 +0000 | [diff] [blame] | 933 | RegOpers.adjustLaneLiveness(*LIS, MRI, SlotIdx); |
| 934 | } |
Matthias Braun | b505c76 | 2016-01-12 22:57:35 +0000 | [diff] [blame] | 935 | if (PDiffs != nullptr) |
| 936 | PDiffs->addInstruction(SU->NodeNum, RegOpers, MRI); |
| 937 | |
| 938 | RPTracker->recedeSkipDebugValues(); |
| 939 | assert(&*RPTracker->getPos() == MI && "RPTracker in sync"); |
| 940 | RPTracker->recede(RegOpers); |
Andrew Trick | 8863992 | 2012-04-24 17:56:43 +0000 | [diff] [blame] | 941 | } |
Devang Patel | e5feef0 | 2011-06-02 20:07:12 +0000 | [diff] [blame] | 942 | |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 943 | assert( |
| 944 | (CanHandleTerminators || (!MI->isTerminator() && !MI->isPosition())) && |
| 945 | "Cannot schedule terminators or labels!"); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 946 | |
Dan Gohman | 3aab10b | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 947 | // Add register-based dependencies (data, anti, and output). |
Andrew Trick | ec25648 | 2012-12-18 20:53:01 +0000 | [diff] [blame] | 948 | bool HasVRegDef = false; |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 949 | for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) { |
| 950 | const MachineOperand &MO = MI->getOperand(j); |
| 951 | if (!MO.isReg()) continue; |
| 952 | unsigned Reg = MO.getReg(); |
| 953 | if (Reg == 0) continue; |
| 954 | |
Andrew Trick | dbee9d8 | 2012-01-14 02:17:15 +0000 | [diff] [blame] | 955 | if (TRI->isPhysicalRegister(Reg)) |
| 956 | addPhysRegDeps(SU, j); |
| 957 | else { |
Andrew Trick | ec25648 | 2012-12-18 20:53:01 +0000 | [diff] [blame] | 958 | if (MO.isDef()) { |
| 959 | HasVRegDef = true; |
Andrew Trick | 59ac4fb | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 960 | addVRegDefDeps(SU, j); |
Andrew Trick | ec25648 | 2012-12-18 20:53:01 +0000 | [diff] [blame] | 961 | } |
Andrew Trick | da6a15d | 2012-02-23 03:16:24 +0000 | [diff] [blame] | 962 | else if (MO.readsReg()) // ignore undef operands |
Andrew Trick | 59ac4fb | 2012-01-14 02:17:18 +0000 | [diff] [blame] | 963 | addVRegUseDeps(SU, j); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 964 | } |
| 965 | } |
Andrew Trick | ec25648 | 2012-12-18 20:53:01 +0000 | [diff] [blame] | 966 | // If we haven't seen any uses in this scheduling region, create a |
| 967 | // dependence edge to ExitSU to model the live-out latency. This is required |
| 968 | // for vreg defs with no in-region use, and prefetches with no vreg def. |
| 969 | // |
| 970 | // FIXME: NumDataSuccs would be more precise than NumSuccs here. This |
| 971 | // check currently relies on being called before adding chain deps. |
| 972 | if (SU->NumSuccs == 0 && SU->Latency > 1 |
| 973 | && (HasVRegDef || MI->mayLoad())) { |
| 974 | SDep Dep(SU, SDep::Artificial); |
| 975 | Dep.setLatency(SU->Latency - 1); |
| 976 | ExitSU.addPred(Dep); |
| 977 | } |
Dan Gohman | 3aab10b | 2008-12-04 01:35:46 +0000 | [diff] [blame] | 978 | |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 979 | // Add memory dependencies (Note: isStoreToStackSlot and |
| 980 | // isLoadFromStackSLot are not usable after stack slots are lowered to |
| 981 | // actual addresses). |
| 982 | |
| 983 | // This is a barrier event that acts as a pivotal node in the DAG. |
Andrew Trick | da01ba3 | 2012-05-15 18:59:41 +0000 | [diff] [blame] | 984 | if (isGlobalMemoryObject(AA, MI)) { |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 985 | |
| 986 | // Become the barrier chain. |
David Goodwin | d2f9c04 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 987 | if (BarrierChain) |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 988 | BarrierChain->addPredBarrier(SU); |
David Goodwin | d2f9c04 | 2009-11-09 19:22:17 +0000 | [diff] [blame] | 989 | BarrierChain = SU; |
| 990 | |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 991 | DEBUG(dbgs() << "Global memory object and new barrier chain: SU(" |
| 992 | << BarrierChain->NodeNum << ").\n";); |
Tom Stellard | 3e01d47 | 2014-12-08 23:36:48 +0000 | [diff] [blame] | 993 | |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 994 | // Add dependencies against everything below it and clear maps. |
| 995 | addBarrierChain(Stores); |
| 996 | addBarrierChain(Loads); |
| 997 | addBarrierChain(NonAliasStores); |
| 998 | addBarrierChain(NonAliasLoads); |
Hal Finkel | 66859ae | 2012-12-10 18:49:16 +0000 | [diff] [blame] | 999 | |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 1000 | continue; |
| 1001 | } |
| 1002 | |
| 1003 | // If it's not a store or a variant load, we're done. |
| 1004 | if (!MI->mayStore() && !(MI->mayLoad() && !MI->isInvariantLoad(AA))) |
| 1005 | continue; |
| 1006 | |
| 1007 | // Always add dependecy edge to BarrierChain if present. |
| 1008 | if (BarrierChain) |
| 1009 | BarrierChain->addPredBarrier(SU); |
| 1010 | |
| 1011 | // Find the underlying objects for MI. The Objs vector is either |
| 1012 | // empty, or filled with the Values of memory locations which this |
| 1013 | // SU depends on. An empty vector means the memory location is |
Jonas Paulsson | 98963fe | 2016-02-15 16:43:15 +0000 | [diff] [blame] | 1014 | // unknown, and may alias anything. |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 1015 | UnderlyingObjectsVector Objs; |
| 1016 | getUnderlyingObjectsForInstr(MI, MFI, Objs, MF.getDataLayout()); |
| 1017 | |
| 1018 | if (MI->mayStore()) { |
Hal Finkel | 66859ae | 2012-12-10 18:49:16 +0000 | [diff] [blame] | 1019 | if (Objs.empty()) { |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 1020 | // An unknown store depends on all stores and loads. |
| 1021 | addChainDependencies(SU, Stores); |
| 1022 | addChainDependencies(SU, NonAliasStores); |
| 1023 | addChainDependencies(SU, Loads); |
| 1024 | addChainDependencies(SU, NonAliasLoads); |
| 1025 | |
| 1026 | // Map this store to 'UnknownValue'. |
| 1027 | Stores.insert(SU, UnknownValue); |
Chandler Carruth | b472856 | 2016-03-31 21:55:58 +0000 | [diff] [blame] | 1028 | } else { |
| 1029 | // Add precise dependencies against all previously seen memory |
| 1030 | // accesses mapped to the same Value(s). |
Geoff Berry | 6381713 | 2016-04-14 21:31:07 +0000 | [diff] [blame^] | 1031 | for (const UnderlyingObject &UnderlObj : Objs) { |
| 1032 | ValueType V = UnderlObj.getValue(); |
| 1033 | bool ThisMayAlias = UnderlObj.mayAlias(); |
Chandler Carruth | b472856 | 2016-03-31 21:55:58 +0000 | [diff] [blame] | 1034 | |
| 1035 | // Add dependencies to previous stores and loads mapped to V. |
Geoff Berry | 6381713 | 2016-04-14 21:31:07 +0000 | [diff] [blame^] | 1036 | addChainDependencies(SU, (ThisMayAlias ? Stores : NonAliasStores), V); |
Chandler Carruth | b472856 | 2016-03-31 21:55:58 +0000 | [diff] [blame] | 1037 | addChainDependencies(SU, (ThisMayAlias ? Loads : NonAliasLoads), V); |
Geoff Berry | c0739d8 | 2016-04-12 15:50:19 +0000 | [diff] [blame] | 1038 | } |
| 1039 | // Update the store map after all chains have been added to avoid adding |
| 1040 | // self-loop edge if multiple underlying objects are present. |
Geoff Berry | 6381713 | 2016-04-14 21:31:07 +0000 | [diff] [blame^] | 1041 | for (const UnderlyingObject &UnderlObj : Objs) { |
| 1042 | ValueType V = UnderlObj.getValue(); |
| 1043 | bool ThisMayAlias = UnderlObj.mayAlias(); |
Chandler Carruth | b472856 | 2016-03-31 21:55:58 +0000 | [diff] [blame] | 1044 | |
| 1045 | // Map this store to V. |
Geoff Berry | 6381713 | 2016-04-14 21:31:07 +0000 | [diff] [blame^] | 1046 | (ThisMayAlias ? Stores : NonAliasStores).insert(SU, V); |
Chandler Carruth | b472856 | 2016-03-31 21:55:58 +0000 | [diff] [blame] | 1047 | } |
| 1048 | // The store may have dependencies to unanalyzable loads and |
| 1049 | // stores. |
| 1050 | addChainDependencies(SU, Loads, UnknownValue); |
| 1051 | addChainDependencies(SU, Stores, UnknownValue); |
Hal Finkel | 66859ae | 2012-12-10 18:49:16 +0000 | [diff] [blame] | 1052 | } |
Chandler Carruth | b472856 | 2016-03-31 21:55:58 +0000 | [diff] [blame] | 1053 | } else { // SU is a load. |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 1054 | if (Objs.empty()) { |
| 1055 | // An unknown load depends on all stores. |
| 1056 | addChainDependencies(SU, Stores); |
| 1057 | addChainDependencies(SU, NonAliasStores); |
| 1058 | |
| 1059 | Loads.insert(SU, UnknownValue); |
Chandler Carruth | b472856 | 2016-03-31 21:55:58 +0000 | [diff] [blame] | 1060 | } else { |
Geoff Berry | 6381713 | 2016-04-14 21:31:07 +0000 | [diff] [blame^] | 1061 | for (const UnderlyingObject &UnderlObj : Objs) { |
| 1062 | ValueType V = UnderlObj.getValue(); |
| 1063 | bool ThisMayAlias = UnderlObj.mayAlias(); |
Chandler Carruth | b472856 | 2016-03-31 21:55:58 +0000 | [diff] [blame] | 1064 | |
| 1065 | // Add precise dependencies against all previously seen stores |
| 1066 | // mapping to the same Value(s). |
| 1067 | addChainDependencies(SU, (ThisMayAlias ? Stores : NonAliasStores), V); |
| 1068 | |
| 1069 | // Map this load to V. |
| 1070 | (ThisMayAlias ? Loads : NonAliasLoads).insert(SU, V); |
| 1071 | } |
| 1072 | // The load may have dependencies to unanalyzable stores. |
| 1073 | addChainDependencies(SU, Stores, UnknownValue); |
Hal Finkel | 66859ae | 2012-12-10 18:49:16 +0000 | [diff] [blame] | 1074 | } |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | // Reduce maps if they grow huge. |
| 1078 | if (Stores.size() + Loads.size() >= HugeRegion) { |
| 1079 | DEBUG(dbgs() << "Reducing Stores and Loads maps.\n";); |
| 1080 | reduceHugeMemNodeMaps(Stores, Loads, ReductionSize); |
| 1081 | } |
| 1082 | if (NonAliasStores.size() + NonAliasLoads.size() >= HugeRegion) { |
| 1083 | DEBUG(dbgs() << "Reducing NonAliasStores and NonAliasLoads maps.\n";); |
| 1084 | reduceHugeMemNodeMaps(NonAliasStores, NonAliasLoads, ReductionSize); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1085 | } |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1086 | } |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 1087 | |
Andrew Trick | b767d1e | 2012-12-01 01:22:49 +0000 | [diff] [blame] | 1088 | if (DbgMI) |
| 1089 | FirstDbgValue = DbgMI; |
Dan Gohman | 619ef48 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 1090 | |
Andrew Trick | d675a4c | 2012-02-23 01:52:38 +0000 | [diff] [blame] | 1091 | Defs.clear(); |
| 1092 | Uses.clear(); |
Matthias Braun | 97d0ffb | 2015-12-04 01:51:19 +0000 | [diff] [blame] | 1093 | CurrentVRegDefs.clear(); |
| 1094 | CurrentVRegUses.clear(); |
Jonas Paulsson | ac29f01 | 2016-02-03 17:52:29 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | raw_ostream &llvm::operator<<(raw_ostream &OS, const PseudoSourceValue* PSV) { |
| 1098 | PSV->printCustom(OS); |
| 1099 | return OS; |
| 1100 | } |
| 1101 | |
| 1102 | void ScheduleDAGInstrs::Value2SUsMap::dump() { |
| 1103 | for (auto &Itr : *this) { |
| 1104 | if (Itr.first.is<const Value*>()) { |
| 1105 | const Value *V = Itr.first.get<const Value*>(); |
| 1106 | if (isa<UndefValue>(V)) |
| 1107 | dbgs() << "Unknown"; |
| 1108 | else |
| 1109 | V->printAsOperand(dbgs()); |
| 1110 | } |
| 1111 | else if (Itr.first.is<const PseudoSourceValue*>()) |
| 1112 | dbgs() << Itr.first.get<const PseudoSourceValue*>(); |
| 1113 | else |
| 1114 | llvm_unreachable("Unknown Value type."); |
| 1115 | |
| 1116 | dbgs() << " : "; |
| 1117 | dumpSUList(Itr.second); |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | /// Reduce maps in FIFO order, by N SUs. This is better than turning |
| 1122 | /// every Nth memory SU into BarrierChain in buildSchedGraph(), since |
| 1123 | /// it avoids unnecessary edges between seen SUs above the new |
| 1124 | /// BarrierChain, and those below it. |
| 1125 | void ScheduleDAGInstrs::reduceHugeMemNodeMaps(Value2SUsMap &stores, |
| 1126 | Value2SUsMap &loads, unsigned N) { |
| 1127 | DEBUG(dbgs() << "Before reduction:\nStoring SUnits:\n"; |
| 1128 | stores.dump(); |
| 1129 | dbgs() << "Loading SUnits:\n"; |
| 1130 | loads.dump()); |
| 1131 | |
| 1132 | // Insert all SU's NodeNums into a vector and sort it. |
| 1133 | std::vector<unsigned> NodeNums; |
| 1134 | NodeNums.reserve(stores.size() + loads.size()); |
| 1135 | for (auto &I : stores) |
| 1136 | for (auto *SU : I.second) |
| 1137 | NodeNums.push_back(SU->NodeNum); |
| 1138 | for (auto &I : loads) |
| 1139 | for (auto *SU : I.second) |
| 1140 | NodeNums.push_back(SU->NodeNum); |
| 1141 | std::sort(NodeNums.begin(), NodeNums.end()); |
| 1142 | |
| 1143 | // The N last elements in NodeNums will be removed, and the SU with |
| 1144 | // the lowest NodeNum of them will become the new BarrierChain to |
| 1145 | // let the not yet seen SUs have a dependency to the removed SUs. |
| 1146 | assert (N <= NodeNums.size()); |
| 1147 | SUnit *newBarrierChain = &SUnits[*(NodeNums.end() - N)]; |
| 1148 | if (BarrierChain) { |
| 1149 | // The aliasing and non-aliasing maps reduce independently of each |
| 1150 | // other, but share a common BarrierChain. Check if the |
| 1151 | // newBarrierChain is above the former one. If it is not, it may |
| 1152 | // introduce a loop to use newBarrierChain, so keep the old one. |
| 1153 | if (newBarrierChain->NodeNum < BarrierChain->NodeNum) { |
| 1154 | BarrierChain->addPredBarrier(newBarrierChain); |
| 1155 | BarrierChain = newBarrierChain; |
| 1156 | DEBUG(dbgs() << "Inserting new barrier chain: SU(" |
| 1157 | << BarrierChain->NodeNum << ").\n";); |
| 1158 | } |
| 1159 | else |
| 1160 | DEBUG(dbgs() << "Keeping old barrier chain: SU(" |
| 1161 | << BarrierChain->NodeNum << ").\n";); |
| 1162 | } |
| 1163 | else |
| 1164 | BarrierChain = newBarrierChain; |
| 1165 | |
| 1166 | insertBarrierChain(stores); |
| 1167 | insertBarrierChain(loads); |
| 1168 | |
| 1169 | DEBUG(dbgs() << "After reduction:\nStoring SUnits:\n"; |
| 1170 | stores.dump(); |
| 1171 | dbgs() << "Loading SUnits:\n"; |
| 1172 | loads.dump()); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Andrew Trick | 6b104f8 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 1175 | /// \brief Initialize register live-range state for updating kills. |
| 1176 | void ScheduleDAGInstrs::startBlockForKills(MachineBasicBlock *BB) { |
| 1177 | // Start with no live registers. |
| 1178 | LiveRegs.reset(); |
| 1179 | |
| 1180 | // Examine the live-in regs of all successors. |
| 1181 | for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), |
| 1182 | SE = BB->succ_end(); SI != SE; ++SI) { |
Matthias Braun | d9da162 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 1183 | for (const auto &LI : (*SI)->liveins()) { |
Andrew Trick | 6b104f8 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 1184 | // Repeat, for reg and all subregs. |
Matthias Braun | d9da162 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 1185 | for (MCSubRegIterator SubRegs(LI.PhysReg, TRI, /*IncludeSelf=*/true); |
Andrew Trick | 6b104f8 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 1186 | SubRegs.isValid(); ++SubRegs) |
| 1187 | LiveRegs.set(*SubRegs); |
| 1188 | } |
| 1189 | } |
| 1190 | } |
| 1191 | |
Pete Cooper | 300069a | 2015-05-04 16:52:06 +0000 | [diff] [blame] | 1192 | /// \brief If we change a kill flag on the bundle instruction implicit register |
| 1193 | /// operands, then we also need to propagate that to any instructions inside |
| 1194 | /// the bundle which had the same kill state. |
| 1195 | static void toggleBundleKillFlag(MachineInstr *MI, unsigned Reg, |
| 1196 | bool NewKillState) { |
| 1197 | if (MI->getOpcode() != TargetOpcode::BUNDLE) |
| 1198 | return; |
| 1199 | |
| 1200 | // Walk backwards from the last instruction in the bundle to the first. |
| 1201 | // Once we set a kill flag on an instruction, we bail out, as otherwise we |
| 1202 | // might set it on too many operands. We will clear as many flags as we |
| 1203 | // can though. |
Duncan P. N. Exon Smith | c5b668d | 2016-02-22 20:49:58 +0000 | [diff] [blame] | 1204 | MachineBasicBlock::instr_iterator Begin = MI->getIterator(); |
Duncan P. N. Exon Smith | f9ab416 | 2016-02-27 17:05:33 +0000 | [diff] [blame] | 1205 | MachineBasicBlock::instr_iterator End = getBundleEnd(*MI); |
Pete Cooper | 300069a | 2015-05-04 16:52:06 +0000 | [diff] [blame] | 1206 | while (Begin != End) { |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 1207 | for (MachineOperand &MO : (--End)->operands()) { |
| 1208 | if (!MO.isReg() || MO.isDef() || Reg != MO.getReg()) |
Pete Cooper | 300069a | 2015-05-04 16:52:06 +0000 | [diff] [blame] | 1209 | continue; |
| 1210 | |
Saleem Abdulrasool | ee13fbe | 2015-05-12 23:36:18 +0000 | [diff] [blame] | 1211 | // DEBUG_VALUE nodes do not contribute to code generation and should |
| 1212 | // always be ignored. Failure to do so may result in trying to modify |
| 1213 | // KILL flags on DEBUG_VALUE nodes, which is distressing. |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 1214 | if (MO.isDebug()) |
Saleem Abdulrasool | ee13fbe | 2015-05-12 23:36:18 +0000 | [diff] [blame] | 1215 | continue; |
| 1216 | |
Pete Cooper | 300069a | 2015-05-04 16:52:06 +0000 | [diff] [blame] | 1217 | // If the register has the internal flag then it could be killing an |
| 1218 | // internal def of the register. In this case, just skip. We only want |
| 1219 | // to toggle the flag on operands visible outside the bundle. |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 1220 | if (MO.isInternalRead()) |
Pete Cooper | 300069a | 2015-05-04 16:52:06 +0000 | [diff] [blame] | 1221 | continue; |
| 1222 | |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 1223 | if (MO.isKill() == NewKillState) |
Pete Cooper | 300069a | 2015-05-04 16:52:06 +0000 | [diff] [blame] | 1224 | continue; |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 1225 | MO.setIsKill(NewKillState); |
Pete Cooper | 300069a | 2015-05-04 16:52:06 +0000 | [diff] [blame] | 1226 | if (NewKillState) |
| 1227 | return; |
| 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | |
Andrew Trick | 6b104f8 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 1232 | bool ScheduleDAGInstrs::toggleKillFlag(MachineInstr *MI, MachineOperand &MO) { |
| 1233 | // Setting kill flag... |
| 1234 | if (!MO.isKill()) { |
| 1235 | MO.setIsKill(true); |
Pete Cooper | 300069a | 2015-05-04 16:52:06 +0000 | [diff] [blame] | 1236 | toggleBundleKillFlag(MI, MO.getReg(), true); |
Andrew Trick | 6b104f8 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 1237 | return false; |
| 1238 | } |
| 1239 | |
| 1240 | // If MO itself is live, clear the kill flag... |
| 1241 | if (LiveRegs.test(MO.getReg())) { |
| 1242 | MO.setIsKill(false); |
Pete Cooper | 300069a | 2015-05-04 16:52:06 +0000 | [diff] [blame] | 1243 | toggleBundleKillFlag(MI, MO.getReg(), false); |
Andrew Trick | 6b104f8 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 1244 | return false; |
| 1245 | } |
| 1246 | |
| 1247 | // If any subreg of MO is live, then create an imp-def for that |
| 1248 | // subreg and keep MO marked as killed. |
| 1249 | MO.setIsKill(false); |
Pete Cooper | 300069a | 2015-05-04 16:52:06 +0000 | [diff] [blame] | 1250 | toggleBundleKillFlag(MI, MO.getReg(), false); |
Andrew Trick | 6b104f8 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 1251 | bool AllDead = true; |
| 1252 | const unsigned SuperReg = MO.getReg(); |
| 1253 | MachineInstrBuilder MIB(MF, MI); |
| 1254 | for (MCSubRegIterator SubRegs(SuperReg, TRI); SubRegs.isValid(); ++SubRegs) { |
| 1255 | if (LiveRegs.test(*SubRegs)) { |
| 1256 | MIB.addReg(*SubRegs, RegState::ImplicitDefine); |
| 1257 | AllDead = false; |
| 1258 | } |
| 1259 | } |
| 1260 | |
Pete Cooper | 300069a | 2015-05-04 16:52:06 +0000 | [diff] [blame] | 1261 | if(AllDead) { |
Andrew Trick | 6b104f8 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 1262 | MO.setIsKill(true); |
Pete Cooper | 300069a | 2015-05-04 16:52:06 +0000 | [diff] [blame] | 1263 | toggleBundleKillFlag(MI, MO.getReg(), true); |
| 1264 | } |
Andrew Trick | 6b104f8 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 1265 | return false; |
| 1266 | } |
| 1267 | |
| 1268 | // FIXME: Reuse the LivePhysRegs utility for this. |
| 1269 | void ScheduleDAGInstrs::fixupKills(MachineBasicBlock *MBB) { |
| 1270 | DEBUG(dbgs() << "Fixup kills for BB#" << MBB->getNumber() << '\n'); |
| 1271 | |
| 1272 | LiveRegs.resize(TRI->getNumRegs()); |
| 1273 | BitVector killedRegs(TRI->getNumRegs()); |
| 1274 | |
| 1275 | startBlockForKills(MBB); |
| 1276 | |
| 1277 | // Examine block from end to start... |
| 1278 | unsigned Count = MBB->size(); |
| 1279 | for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin(); |
| 1280 | I != E; --Count) { |
| 1281 | MachineInstr *MI = --I; |
| 1282 | if (MI->isDebugValue()) |
| 1283 | continue; |
| 1284 | |
| 1285 | // Update liveness. Registers that are defed but not used in this |
| 1286 | // instruction are now dead. Mark register and all subregs as they |
| 1287 | // are completely defined. |
| 1288 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1289 | MachineOperand &MO = MI->getOperand(i); |
| 1290 | if (MO.isRegMask()) |
| 1291 | LiveRegs.clearBitsNotInMask(MO.getRegMask()); |
| 1292 | if (!MO.isReg()) continue; |
| 1293 | unsigned Reg = MO.getReg(); |
| 1294 | if (Reg == 0) continue; |
| 1295 | if (!MO.isDef()) continue; |
| 1296 | // Ignore two-addr defs. |
| 1297 | if (MI->isRegTiedToUseOperand(i)) continue; |
| 1298 | |
| 1299 | // Repeat for reg and all subregs. |
| 1300 | for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); |
| 1301 | SubRegs.isValid(); ++SubRegs) |
| 1302 | LiveRegs.reset(*SubRegs); |
| 1303 | } |
| 1304 | |
| 1305 | // Examine all used registers and set/clear kill flag. When a |
| 1306 | // register is used multiple times we only set the kill flag on |
| 1307 | // the first use. Don't set kill flags on undef operands. |
| 1308 | killedRegs.reset(); |
| 1309 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1310 | MachineOperand &MO = MI->getOperand(i); |
| 1311 | if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue; |
| 1312 | unsigned Reg = MO.getReg(); |
| 1313 | if ((Reg == 0) || MRI.isReserved(Reg)) continue; |
| 1314 | |
| 1315 | bool kill = false; |
| 1316 | if (!killedRegs.test(Reg)) { |
| 1317 | kill = true; |
| 1318 | // A register is not killed if any subregs are live... |
| 1319 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { |
| 1320 | if (LiveRegs.test(*SubRegs)) { |
| 1321 | kill = false; |
| 1322 | break; |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | // If subreg is not live, then register is killed if it became |
| 1327 | // live in this instruction |
| 1328 | if (kill) |
| 1329 | kill = !LiveRegs.test(Reg); |
| 1330 | } |
| 1331 | |
| 1332 | if (MO.isKill() != kill) { |
| 1333 | DEBUG(dbgs() << "Fixing " << MO << " in "); |
| 1334 | // Warning: toggleKillFlag may invalidate MO. |
| 1335 | toggleKillFlag(MI, MO); |
| 1336 | DEBUG(MI->dump()); |
Pete Cooper | 300069a | 2015-05-04 16:52:06 +0000 | [diff] [blame] | 1337 | DEBUG(if (MI->getOpcode() == TargetOpcode::BUNDLE) { |
Duncan P. N. Exon Smith | c5b668d | 2016-02-22 20:49:58 +0000 | [diff] [blame] | 1338 | MachineBasicBlock::instr_iterator Begin = MI->getIterator(); |
Duncan P. N. Exon Smith | f9ab416 | 2016-02-27 17:05:33 +0000 | [diff] [blame] | 1339 | MachineBasicBlock::instr_iterator End = getBundleEnd(*MI); |
Pete Cooper | 300069a | 2015-05-04 16:52:06 +0000 | [diff] [blame] | 1340 | while (++Begin != End) |
| 1341 | DEBUG(Begin->dump()); |
| 1342 | }); |
Andrew Trick | 6b104f8 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | killedRegs.set(Reg); |
| 1346 | } |
| 1347 | |
| 1348 | // Mark any used register (that is not using undef) and subregs as |
| 1349 | // now live... |
| 1350 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1351 | MachineOperand &MO = MI->getOperand(i); |
| 1352 | if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue; |
| 1353 | unsigned Reg = MO.getReg(); |
| 1354 | if ((Reg == 0) || MRI.isReserved(Reg)) continue; |
| 1355 | |
| 1356 | for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); |
| 1357 | SubRegs.isValid(); ++SubRegs) |
| 1358 | LiveRegs.set(*SubRegs); |
| 1359 | } |
| 1360 | } |
| 1361 | } |
| 1362 | |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1363 | void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const { |
Manman Ren | 19f49ac | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 1364 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1365 | SU->getInstr()->dump(); |
Manman Ren | 742534c | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 1366 | #endif |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const { |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 1370 | std::string s; |
| 1371 | raw_string_ostream oss(s); |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 1372 | if (SU == &EntrySU) |
| 1373 | oss << "<entry>"; |
| 1374 | else if (SU == &ExitSU) |
| 1375 | oss << "<exit>"; |
| 1376 | else |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 1377 | SU->getInstr()->print(oss, /*SkipOpers=*/true); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1378 | return oss.str(); |
| 1379 | } |
| 1380 | |
Andrew Trick | 1b2324d | 2012-03-07 00:18:22 +0000 | [diff] [blame] | 1381 | /// Return the basic block label. It is not necessarilly unique because a block |
| 1382 | /// contains multiple scheduling regions. But it is fine for visualization. |
| 1383 | std::string ScheduleDAGInstrs::getDAGName() const { |
| 1384 | return "dag." + BB->getFullName(); |
| 1385 | } |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1386 | |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1387 | //===----------------------------------------------------------------------===// |
| 1388 | // SchedDFSResult Implementation |
| 1389 | //===----------------------------------------------------------------------===// |
| 1390 | |
| 1391 | namespace llvm { |
| 1392 | /// \brief Internal state used to compute SchedDFSResult. |
| 1393 | class SchedDFSImpl { |
| 1394 | SchedDFSResult &R; |
| 1395 | |
| 1396 | /// Join DAG nodes into equivalence classes by their subtree. |
| 1397 | IntEqClasses SubtreeClasses; |
| 1398 | /// List PredSU, SuccSU pairs that represent data edges between subtrees. |
| 1399 | std::vector<std::pair<const SUnit*, const SUnit*> > ConnectionPairs; |
| 1400 | |
Andrew Trick | ffc8097 | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1401 | struct RootData { |
| 1402 | unsigned NodeID; |
| 1403 | unsigned ParentNodeID; // Parent node (member of the parent subtree). |
| 1404 | unsigned SubInstrCount; // Instr count in this tree only, not children. |
| 1405 | |
| 1406 | RootData(unsigned id): NodeID(id), |
| 1407 | ParentNodeID(SchedDFSResult::InvalidSubtreeID), |
| 1408 | SubInstrCount(0) {} |
| 1409 | |
| 1410 | unsigned getSparseSetIndex() const { return NodeID; } |
| 1411 | }; |
| 1412 | |
| 1413 | SparseSet<RootData> RootSet; |
| 1414 | |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1415 | public: |
Andrew Trick | ffc8097 | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1416 | SchedDFSImpl(SchedDFSResult &r): R(r), SubtreeClasses(R.DFSNodeData.size()) { |
| 1417 | RootSet.setUniverse(R.DFSNodeData.size()); |
| 1418 | } |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1419 | |
Andrew Trick | 5b07eeb | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1420 | /// Return true if this node been visited by the DFS traversal. |
| 1421 | /// |
| 1422 | /// During visitPostorderNode the Node's SubtreeID is assigned to the Node |
| 1423 | /// ID. Later, SubtreeID is updated but remains valid. |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1424 | bool isVisited(const SUnit *SU) const { |
Andrew Trick | ffc8097 | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1425 | return R.DFSNodeData[SU->NodeNum].SubtreeID |
| 1426 | != SchedDFSResult::InvalidSubtreeID; |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | /// Initialize this node's instruction count. We don't need to flag the node |
| 1430 | /// visited until visitPostorder because the DAG cannot have cycles. |
| 1431 | void visitPreorder(const SUnit *SU) { |
Andrew Trick | ffc8097 | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1432 | R.DFSNodeData[SU->NodeNum].InstrCount = |
| 1433 | SU->getInstr()->isTransient() ? 0 : 1; |
Andrew Trick | 5b07eeb | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | /// Called once for each node after all predecessors are visited. Revisit this |
| 1437 | /// node's predecessors and potentially join them now that we know the ILP of |
| 1438 | /// the other predecessors. |
| 1439 | void visitPostorderNode(const SUnit *SU) { |
| 1440 | // Mark this node as the root of a subtree. It may be joined with its |
| 1441 | // successors later. |
Andrew Trick | ffc8097 | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1442 | R.DFSNodeData[SU->NodeNum].SubtreeID = SU->NodeNum; |
| 1443 | RootData RData(SU->NodeNum); |
| 1444 | RData.SubInstrCount = SU->getInstr()->isTransient() ? 0 : 1; |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1445 | |
Andrew Trick | 5b07eeb | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1446 | // If any predecessors are still in their own subtree, they either cannot be |
| 1447 | // joined or are large enough to remain separate. If this parent node's |
| 1448 | // total instruction count is not greater than a child subtree by at least |
| 1449 | // the subtree limit, then try to join it now since splitting subtrees is |
| 1450 | // only useful if multiple high-pressure paths are possible. |
Andrew Trick | ffc8097 | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1451 | unsigned InstrCount = R.DFSNodeData[SU->NodeNum].InstrCount; |
Andrew Trick | 5b07eeb | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1452 | for (SUnit::const_pred_iterator |
| 1453 | PI = SU->Preds.begin(), PE = SU->Preds.end(); PI != PE; ++PI) { |
| 1454 | if (PI->getKind() != SDep::Data) |
| 1455 | continue; |
| 1456 | unsigned PredNum = PI->getSUnit()->NodeNum; |
Andrew Trick | ffc8097 | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1457 | if ((InstrCount - R.DFSNodeData[PredNum].InstrCount) < R.SubtreeLimit) |
Andrew Trick | 5b07eeb | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1458 | joinPredSubtree(*PI, SU, /*CheckLimit=*/false); |
Andrew Trick | ffc8097 | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1459 | |
| 1460 | // Either link or merge the TreeData entry from the child to the parent. |
Andrew Trick | 646eeb6 | 2013-01-25 06:52:30 +0000 | [diff] [blame] | 1461 | if (R.DFSNodeData[PredNum].SubtreeID == PredNum) { |
| 1462 | // If the predecessor's parent is invalid, this is a tree edge and the |
| 1463 | // current node is the parent. |
| 1464 | if (RootSet[PredNum].ParentNodeID == SchedDFSResult::InvalidSubtreeID) |
| 1465 | RootSet[PredNum].ParentNodeID = SU->NodeNum; |
| 1466 | } |
| 1467 | else if (RootSet.count(PredNum)) { |
| 1468 | // The predecessor is not a root, but is still in the root set. This |
| 1469 | // must be the new parent that it was just joined to. Note that |
| 1470 | // RootSet[PredNum].ParentNodeID may either be invalid or may still be |
| 1471 | // set to the original parent. |
Andrew Trick | ffc8097 | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1472 | RData.SubInstrCount += RootSet[PredNum].SubInstrCount; |
| 1473 | RootSet.erase(PredNum); |
| 1474 | } |
Andrew Trick | 5b07eeb | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1475 | } |
Andrew Trick | ffc8097 | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1476 | RootSet[SU->NodeNum] = RData; |
| 1477 | } |
| 1478 | |
| 1479 | /// Called once for each tree edge after calling visitPostOrderNode on the |
| 1480 | /// predecessor. Increment the parent node's instruction count and |
| 1481 | /// preemptively join this subtree to its parent's if it is small enough. |
| 1482 | void visitPostorderEdge(const SDep &PredDep, const SUnit *Succ) { |
| 1483 | R.DFSNodeData[Succ->NodeNum].InstrCount |
| 1484 | += R.DFSNodeData[PredDep.getSUnit()->NodeNum].InstrCount; |
| 1485 | joinPredSubtree(PredDep, Succ); |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
Andrew Trick | 5b07eeb | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1488 | /// Add a connection for cross edges. |
| 1489 | void visitCrossEdge(const SDep &PredDep, const SUnit *Succ) { |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1490 | ConnectionPairs.push_back(std::make_pair(PredDep.getSUnit(), Succ)); |
| 1491 | } |
| 1492 | |
| 1493 | /// Set each node's subtree ID to the representative ID and record connections |
| 1494 | /// between trees. |
| 1495 | void finalize() { |
| 1496 | SubtreeClasses.compress(); |
Andrew Trick | ffc8097 | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1497 | R.DFSTreeData.resize(SubtreeClasses.getNumClasses()); |
| 1498 | assert(SubtreeClasses.getNumClasses() == RootSet.size() |
| 1499 | && "number of roots should match trees"); |
| 1500 | for (SparseSet<RootData>::const_iterator |
| 1501 | RI = RootSet.begin(), RE = RootSet.end(); RI != RE; ++RI) { |
| 1502 | unsigned TreeID = SubtreeClasses[RI->NodeID]; |
| 1503 | if (RI->ParentNodeID != SchedDFSResult::InvalidSubtreeID) |
| 1504 | R.DFSTreeData[TreeID].ParentTreeID = SubtreeClasses[RI->ParentNodeID]; |
| 1505 | R.DFSTreeData[TreeID].SubInstrCount = RI->SubInstrCount; |
Andrew Trick | 646eeb6 | 2013-01-25 06:52:30 +0000 | [diff] [blame] | 1506 | // Note that SubInstrCount may be greater than InstrCount if we joined |
| 1507 | // subtrees across a cross edge. InstrCount will be attributed to the |
| 1508 | // original parent, while SubInstrCount will be attributed to the joined |
| 1509 | // parent. |
Andrew Trick | ffc8097 | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1510 | } |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1511 | R.SubtreeConnections.resize(SubtreeClasses.getNumClasses()); |
| 1512 | R.SubtreeConnectLevels.resize(SubtreeClasses.getNumClasses()); |
| 1513 | DEBUG(dbgs() << R.getNumSubtrees() << " subtrees:\n"); |
Andrew Trick | ffc8097 | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1514 | for (unsigned Idx = 0, End = R.DFSNodeData.size(); Idx != End; ++Idx) { |
| 1515 | R.DFSNodeData[Idx].SubtreeID = SubtreeClasses[Idx]; |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1516 | DEBUG(dbgs() << " SU(" << Idx << ") in tree " |
Andrew Trick | ffc8097 | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1517 | << R.DFSNodeData[Idx].SubtreeID << '\n'); |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1518 | } |
| 1519 | for (std::vector<std::pair<const SUnit*, const SUnit*> >::const_iterator |
| 1520 | I = ConnectionPairs.begin(), E = ConnectionPairs.end(); |
| 1521 | I != E; ++I) { |
| 1522 | unsigned PredTree = SubtreeClasses[I->first->NodeNum]; |
| 1523 | unsigned SuccTree = SubtreeClasses[I->second->NodeNum]; |
| 1524 | if (PredTree == SuccTree) |
| 1525 | continue; |
| 1526 | unsigned Depth = I->first->getDepth(); |
| 1527 | addConnection(PredTree, SuccTree, Depth); |
| 1528 | addConnection(SuccTree, PredTree, Depth); |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | protected: |
Andrew Trick | 5b07eeb | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1533 | /// Join the predecessor subtree with the successor that is its DFS |
| 1534 | /// parent. Apply some heuristics before joining. |
| 1535 | bool joinPredSubtree(const SDep &PredDep, const SUnit *Succ, |
| 1536 | bool CheckLimit = true) { |
| 1537 | assert(PredDep.getKind() == SDep::Data && "Subtrees are for data edges"); |
| 1538 | |
| 1539 | // Check if the predecessor is already joined. |
| 1540 | const SUnit *PredSU = PredDep.getSUnit(); |
| 1541 | unsigned PredNum = PredSU->NodeNum; |
Andrew Trick | ffc8097 | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1542 | if (R.DFSNodeData[PredNum].SubtreeID != PredNum) |
Andrew Trick | 5b07eeb | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1543 | return false; |
Andrew Trick | b52a856 | 2013-01-25 00:12:57 +0000 | [diff] [blame] | 1544 | |
| 1545 | // Four is the magic number of successors before a node is considered a |
| 1546 | // pinch point. |
| 1547 | unsigned NumDataSucs = 0; |
Andrew Trick | b52a856 | 2013-01-25 00:12:57 +0000 | [diff] [blame] | 1548 | for (SUnit::const_succ_iterator SI = PredSU->Succs.begin(), |
| 1549 | SE = PredSU->Succs.end(); SI != SE; ++SI) { |
| 1550 | if (SI->getKind() == SDep::Data) { |
| 1551 | if (++NumDataSucs >= 4) |
Andrew Trick | 5b07eeb | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1552 | return false; |
Andrew Trick | b52a856 | 2013-01-25 00:12:57 +0000 | [diff] [blame] | 1553 | } |
| 1554 | } |
Andrew Trick | ffc8097 | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1555 | if (CheckLimit && R.DFSNodeData[PredNum].InstrCount > R.SubtreeLimit) |
Andrew Trick | 5b07eeb | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1556 | return false; |
Andrew Trick | ffc8097 | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1557 | R.DFSNodeData[PredNum].SubtreeID = Succ->NodeNum; |
Andrew Trick | 5b07eeb | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1558 | SubtreeClasses.join(Succ->NodeNum, PredNum); |
| 1559 | return true; |
Andrew Trick | b52a856 | 2013-01-25 00:12:57 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1562 | /// Called by finalize() to record a connection between trees. |
| 1563 | void addConnection(unsigned FromTree, unsigned ToTree, unsigned Depth) { |
| 1564 | if (!Depth) |
| 1565 | return; |
| 1566 | |
Andrew Trick | ffc8097 | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1567 | do { |
| 1568 | SmallVectorImpl<SchedDFSResult::Connection> &Connections = |
| 1569 | R.SubtreeConnections[FromTree]; |
| 1570 | for (SmallVectorImpl<SchedDFSResult::Connection>::iterator |
| 1571 | I = Connections.begin(), E = Connections.end(); I != E; ++I) { |
| 1572 | if (I->TreeID == ToTree) { |
| 1573 | I->Level = std::max(I->Level, Depth); |
| 1574 | return; |
| 1575 | } |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1576 | } |
Andrew Trick | ffc8097 | 2013-01-25 06:52:27 +0000 | [diff] [blame] | 1577 | Connections.push_back(SchedDFSResult::Connection(ToTree, Depth)); |
| 1578 | FromTree = R.DFSTreeData[FromTree].ParentTreeID; |
| 1579 | } while (FromTree != SchedDFSResult::InvalidSubtreeID); |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1580 | } |
| 1581 | }; |
| 1582 | } // namespace llvm |
| 1583 | |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1584 | namespace { |
| 1585 | /// \brief Manage the stack used by a reverse depth-first search over the DAG. |
| 1586 | class SchedDAGReverseDFS { |
| 1587 | std::vector<std::pair<const SUnit*, SUnit::const_pred_iterator> > DFSStack; |
| 1588 | public: |
| 1589 | bool isComplete() const { return DFSStack.empty(); } |
| 1590 | |
| 1591 | void follow(const SUnit *SU) { |
| 1592 | DFSStack.push_back(std::make_pair(SU, SU->Preds.begin())); |
| 1593 | } |
| 1594 | void advance() { ++DFSStack.back().second; } |
| 1595 | |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1596 | const SDep *backtrack() { |
| 1597 | DFSStack.pop_back(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1598 | return DFSStack.empty() ? nullptr : std::prev(DFSStack.back().second); |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1599 | } |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1600 | |
| 1601 | const SUnit *getCurr() const { return DFSStack.back().first; } |
| 1602 | |
| 1603 | SUnit::const_pred_iterator getPred() const { return DFSStack.back().second; } |
| 1604 | |
| 1605 | SUnit::const_pred_iterator getPredEnd() const { |
| 1606 | return getCurr()->Preds.end(); |
| 1607 | } |
| 1608 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 1609 | } // anonymous |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1610 | |
Andrew Trick | 5b07eeb | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1611 | static bool hasDataSucc(const SUnit *SU) { |
| 1612 | for (SUnit::const_succ_iterator |
| 1613 | SI = SU->Succs.begin(), SE = SU->Succs.end(); SI != SE; ++SI) { |
Andrew Trick | 646eeb6 | 2013-01-25 06:52:30 +0000 | [diff] [blame] | 1614 | if (SI->getKind() == SDep::Data && !SI->getSUnit()->isBoundaryNode()) |
Andrew Trick | 5b07eeb | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1615 | return true; |
| 1616 | } |
| 1617 | return false; |
| 1618 | } |
| 1619 | |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1620 | /// Compute an ILP metric for all nodes in the subDAG reachable via depth-first |
| 1621 | /// search from this root. |
Andrew Trick | e2c3f5c | 2013-01-25 06:33:57 +0000 | [diff] [blame] | 1622 | void SchedDFSResult::compute(ArrayRef<SUnit> SUnits) { |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1623 | if (!IsBottomUp) |
| 1624 | llvm_unreachable("Top-down ILP metric is unimplemnted"); |
| 1625 | |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1626 | SchedDFSImpl Impl(*this); |
Andrew Trick | e2c3f5c | 2013-01-25 06:33:57 +0000 | [diff] [blame] | 1627 | for (ArrayRef<SUnit>::const_iterator |
| 1628 | SI = SUnits.begin(), SE = SUnits.end(); SI != SE; ++SI) { |
| 1629 | const SUnit *SU = &*SI; |
| 1630 | if (Impl.isVisited(SU) || hasDataSucc(SU)) |
| 1631 | continue; |
| 1632 | |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1633 | SchedDAGReverseDFS DFS; |
Andrew Trick | e2c3f5c | 2013-01-25 06:33:57 +0000 | [diff] [blame] | 1634 | Impl.visitPreorder(SU); |
| 1635 | DFS.follow(SU); |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1636 | for (;;) { |
| 1637 | // Traverse the leftmost path as far as possible. |
| 1638 | while (DFS.getPred() != DFS.getPredEnd()) { |
| 1639 | const SDep &PredDep = *DFS.getPred(); |
| 1640 | DFS.advance(); |
Andrew Trick | 5b07eeb | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1641 | // Ignore non-data edges. |
Andrew Trick | 646eeb6 | 2013-01-25 06:52:30 +0000 | [diff] [blame] | 1642 | if (PredDep.getKind() != SDep::Data |
| 1643 | || PredDep.getSUnit()->isBoundaryNode()) { |
Andrew Trick | 5b07eeb | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1644 | continue; |
Andrew Trick | 646eeb6 | 2013-01-25 06:52:30 +0000 | [diff] [blame] | 1645 | } |
Andrew Trick | 5b07eeb | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1646 | // An already visited edge is a cross edge, assuming an acyclic DAG. |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1647 | if (Impl.isVisited(PredDep.getSUnit())) { |
Andrew Trick | 5b07eeb | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1648 | Impl.visitCrossEdge(PredDep, DFS.getCurr()); |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1649 | continue; |
| 1650 | } |
| 1651 | Impl.visitPreorder(PredDep.getSUnit()); |
| 1652 | DFS.follow(PredDep.getSUnit()); |
| 1653 | } |
| 1654 | // Visit the top of the stack in postorder and backtrack. |
| 1655 | const SUnit *Child = DFS.getCurr(); |
| 1656 | const SDep *PredDep = DFS.backtrack(); |
Andrew Trick | 5b07eeb | 2013-01-25 06:02:44 +0000 | [diff] [blame] | 1657 | Impl.visitPostorderNode(Child); |
| 1658 | if (PredDep) |
| 1659 | Impl.visitPostorderEdge(*PredDep, DFS.getCurr()); |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1660 | if (DFS.isComplete()) |
| 1661 | break; |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1662 | } |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1663 | } |
| 1664 | Impl.finalize(); |
| 1665 | } |
| 1666 | |
| 1667 | /// The root of the given SubtreeID was just scheduled. For all subtrees |
| 1668 | /// connected to this tree, record the depth of the connection so that the |
| 1669 | /// nearest connected subtrees can be prioritized. |
| 1670 | void SchedDFSResult::scheduleTree(unsigned SubtreeID) { |
| 1671 | for (SmallVectorImpl<Connection>::const_iterator |
| 1672 | I = SubtreeConnections[SubtreeID].begin(), |
| 1673 | E = SubtreeConnections[SubtreeID].end(); I != E; ++I) { |
| 1674 | SubtreeConnectLevels[I->TreeID] = |
| 1675 | std::max(SubtreeConnectLevels[I->TreeID], I->Level); |
| 1676 | DEBUG(dbgs() << " Tree: " << I->TreeID |
| 1677 | << " @" << SubtreeConnectLevels[I->TreeID] << '\n'); |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1678 | } |
| 1679 | } |
| 1680 | |
Alp Toker | d8d510a | 2014-07-01 21:19:13 +0000 | [diff] [blame] | 1681 | LLVM_DUMP_METHOD |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1682 | void ILPValue::print(raw_ostream &OS) const { |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1683 | OS << InstrCount << " / " << Length << " = "; |
| 1684 | if (!Length) |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1685 | OS << "BADILP"; |
Andrew Trick | 48d392e | 2012-11-28 05:13:28 +0000 | [diff] [blame] | 1686 | else |
| 1687 | OS << format("%g", ((double)InstrCount / Length)); |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
Alp Toker | d8d510a | 2014-07-01 21:19:13 +0000 | [diff] [blame] | 1690 | LLVM_DUMP_METHOD |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1691 | void ILPValue::dump() const { |
| 1692 | dbgs() << *this << '\n'; |
| 1693 | } |
| 1694 | |
| 1695 | namespace llvm { |
| 1696 | |
Alp Toker | d8d510a | 2014-07-01 21:19:13 +0000 | [diff] [blame] | 1697 | LLVM_DUMP_METHOD |
Andrew Trick | 90f711d | 2012-10-15 18:02:27 +0000 | [diff] [blame] | 1698 | raw_ostream &operator<<(raw_ostream &OS, const ILPValue &Val) { |
| 1699 | Val.print(OS); |
| 1700 | return OS; |
| 1701 | } |
| 1702 | |
| 1703 | } // namespace llvm |