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