Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 1 | //===- lib/CodeGen/MachineTraceMetrics.cpp ----------------------*- C++ -*-===// |
| 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 | |
Jakob Stoklund Olesen | 965665b | 2013-01-17 01:06:04 +0000 | [diff] [blame] | 10 | #include "llvm/CodeGen/MachineTraceMetrics.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/PostOrderIterator.h" |
| 12 | #include "llvm/ADT/SparseSet.h" |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 14 | #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" |
| 15 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 16 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 17 | #include "llvm/CodeGen/Passes.h" |
Jakob Stoklund Olesen | 8982222 | 2012-10-04 17:30:40 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCSubtargetInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Format.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetInstrInfo.h" |
| 23 | #include "llvm/Target/TargetRegisterInfo.h" |
Jakob Stoklund Olesen | 8982222 | 2012-10-04 17:30:40 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetSubtargetInfo.h" |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| 27 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 28 | #define DEBUG_TYPE "machine-trace-metrics" |
| 29 | |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 30 | char MachineTraceMetrics::ID = 0; |
| 31 | char &llvm::MachineTraceMetricsID = MachineTraceMetrics::ID; |
| 32 | |
| 33 | INITIALIZE_PASS_BEGIN(MachineTraceMetrics, |
| 34 | "machine-trace-metrics", "Machine Trace Metrics", false, true) |
| 35 | INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) |
| 36 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
| 37 | INITIALIZE_PASS_END(MachineTraceMetrics, |
| 38 | "machine-trace-metrics", "Machine Trace Metrics", false, true) |
| 39 | |
| 40 | MachineTraceMetrics::MachineTraceMetrics() |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 41 | : MachineFunctionPass(ID), MF(nullptr), TII(nullptr), TRI(nullptr), |
| 42 | MRI(nullptr), Loops(nullptr) { |
Benjamin Kramer | 502b9e1 | 2014-04-12 16:15:53 +0000 | [diff] [blame] | 43 | std::fill(std::begin(Ensembles), std::end(Ensembles), nullptr); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void MachineTraceMetrics::getAnalysisUsage(AnalysisUsage &AU) const { |
| 47 | AU.setPreservesAll(); |
| 48 | AU.addRequired<MachineBranchProbabilityInfo>(); |
| 49 | AU.addRequired<MachineLoopInfo>(); |
| 50 | MachineFunctionPass::getAnalysisUsage(AU); |
| 51 | } |
| 52 | |
Jakob Stoklund Olesen | 3df6c46 | 2012-07-30 18:34:11 +0000 | [diff] [blame] | 53 | bool MachineTraceMetrics::runOnMachineFunction(MachineFunction &Func) { |
| 54 | MF = &Func; |
Eric Christopher | 3d4276f | 2015-01-27 07:31:29 +0000 | [diff] [blame] | 55 | const TargetSubtargetInfo &ST = MF->getSubtarget(); |
| 56 | TII = ST.getInstrInfo(); |
| 57 | TRI = ST.getRegisterInfo(); |
Jakob Stoklund Olesen | 3df6c46 | 2012-07-30 18:34:11 +0000 | [diff] [blame] | 58 | MRI = &MF->getRegInfo(); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 59 | Loops = &getAnalysis<MachineLoopInfo>(); |
Pete Cooper | 1175945 | 2014-09-02 17:43:54 +0000 | [diff] [blame] | 60 | SchedModel.init(ST.getSchedModel(), &ST, TII); |
Jakob Stoklund Olesen | 3df6c46 | 2012-07-30 18:34:11 +0000 | [diff] [blame] | 61 | BlockInfo.resize(MF->getNumBlockIDs()); |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 62 | ProcResourceCycles.resize(MF->getNumBlockIDs() * |
| 63 | SchedModel.getNumProcResourceKinds()); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 64 | return false; |
| 65 | } |
| 66 | |
| 67 | void MachineTraceMetrics::releaseMemory() { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 68 | MF = nullptr; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 69 | BlockInfo.clear(); |
| 70 | for (unsigned i = 0; i != TS_NumStrategies; ++i) { |
| 71 | delete Ensembles[i]; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 72 | Ensembles[i] = nullptr; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | //===----------------------------------------------------------------------===// |
| 77 | // Fixed block information |
| 78 | //===----------------------------------------------------------------------===// |
| 79 | // |
| 80 | // The number of instructions in a basic block and the CPU resources used by |
| 81 | // those instructions don't depend on any given trace strategy. |
| 82 | |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 83 | /// Compute the resource usage in basic block MBB. |
| 84 | const MachineTraceMetrics::FixedBlockInfo* |
| 85 | MachineTraceMetrics::getResources(const MachineBasicBlock *MBB) { |
| 86 | assert(MBB && "No basic block"); |
| 87 | FixedBlockInfo *FBI = &BlockInfo[MBB->getNumber()]; |
| 88 | if (FBI->hasResources()) |
| 89 | return FBI; |
| 90 | |
| 91 | // Compute resource usage in the block. |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 92 | FBI->HasCalls = false; |
| 93 | unsigned InstrCount = 0; |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 94 | |
| 95 | // Add up per-processor resource cycles as well. |
| 96 | unsigned PRKinds = SchedModel.getNumProcResourceKinds(); |
| 97 | SmallVector<unsigned, 32> PRCycles(PRKinds); |
| 98 | |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 99 | for (const auto &MI : *MBB) { |
| 100 | if (MI.isTransient()) |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 101 | continue; |
| 102 | ++InstrCount; |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 103 | if (MI.isCall()) |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 104 | FBI->HasCalls = true; |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 105 | |
| 106 | // Count processor resources used. |
Jakob Stoklund Olesen | aeb69a5 | 2013-04-02 22:27:45 +0000 | [diff] [blame] | 107 | if (!SchedModel.hasInstrSchedModel()) |
| 108 | continue; |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 109 | const MCSchedClassDesc *SC = SchedModel.resolveSchedClass(&MI); |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 110 | if (!SC->isValid()) |
| 111 | continue; |
| 112 | |
| 113 | for (TargetSchedModel::ProcResIter |
| 114 | PI = SchedModel.getWriteProcResBegin(SC), |
| 115 | PE = SchedModel.getWriteProcResEnd(SC); PI != PE; ++PI) { |
| 116 | assert(PI->ProcResourceIdx < PRKinds && "Bad processor resource kind"); |
| 117 | PRCycles[PI->ProcResourceIdx] += PI->Cycles; |
| 118 | } |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 119 | } |
| 120 | FBI->InstrCount = InstrCount; |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 121 | |
| 122 | // Scale the resource cycles so they are comparable. |
| 123 | unsigned PROffset = MBB->getNumber() * PRKinds; |
| 124 | for (unsigned K = 0; K != PRKinds; ++K) |
| 125 | ProcResourceCycles[PROffset + K] = |
| 126 | PRCycles[K] * SchedModel.getResourceFactor(K); |
| 127 | |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 128 | return FBI; |
| 129 | } |
| 130 | |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 131 | ArrayRef<unsigned> |
| 132 | MachineTraceMetrics::getProcResourceCycles(unsigned MBBNum) const { |
| 133 | assert(BlockInfo[MBBNum].hasResources() && |
| 134 | "getResources() must be called before getProcResourceCycles()"); |
| 135 | unsigned PRKinds = SchedModel.getNumProcResourceKinds(); |
Jakob Stoklund Olesen | aeb69a5 | 2013-04-02 22:27:45 +0000 | [diff] [blame] | 136 | assert((MBBNum+1) * PRKinds <= ProcResourceCycles.size()); |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 137 | return makeArrayRef(ProcResourceCycles.data() + MBBNum * PRKinds, PRKinds); |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 141 | //===----------------------------------------------------------------------===// |
| 142 | // Ensemble utility functions |
| 143 | //===----------------------------------------------------------------------===// |
| 144 | |
| 145 | MachineTraceMetrics::Ensemble::Ensemble(MachineTraceMetrics *ct) |
Jakob Stoklund Olesen | 1dfb101 | 2012-07-31 20:25:13 +0000 | [diff] [blame] | 146 | : MTM(*ct) { |
| 147 | BlockInfo.resize(MTM.BlockInfo.size()); |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 148 | unsigned PRKinds = MTM.SchedModel.getNumProcResourceKinds(); |
| 149 | ProcResourceDepths.resize(MTM.BlockInfo.size() * PRKinds); |
| 150 | ProcResourceHeights.resize(MTM.BlockInfo.size() * PRKinds); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // Virtual destructor serves as an anchor. |
| 154 | MachineTraceMetrics::Ensemble::~Ensemble() {} |
| 155 | |
Jakob Stoklund Olesen | 3df6c46 | 2012-07-30 18:34:11 +0000 | [diff] [blame] | 156 | const MachineLoop* |
| 157 | MachineTraceMetrics::Ensemble::getLoopFor(const MachineBasicBlock *MBB) const { |
Jakob Stoklund Olesen | 1dfb101 | 2012-07-31 20:25:13 +0000 | [diff] [blame] | 158 | return MTM.Loops->getLoopFor(MBB); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // Update resource-related information in the TraceBlockInfo for MBB. |
| 162 | // Only update resources related to the trace above MBB. |
| 163 | void MachineTraceMetrics::Ensemble:: |
| 164 | computeDepthResources(const MachineBasicBlock *MBB) { |
| 165 | TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()]; |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 166 | unsigned PRKinds = MTM.SchedModel.getNumProcResourceKinds(); |
| 167 | unsigned PROffset = MBB->getNumber() * PRKinds; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 168 | |
| 169 | // Compute resources from trace above. The top block is simple. |
| 170 | if (!TBI->Pred) { |
| 171 | TBI->InstrDepth = 0; |
Jakob Stoklund Olesen | 1152202 | 2012-07-27 23:58:36 +0000 | [diff] [blame] | 172 | TBI->Head = MBB->getNumber(); |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 173 | std::fill(ProcResourceDepths.begin() + PROffset, |
| 174 | ProcResourceDepths.begin() + PROffset + PRKinds, 0); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | |
| 178 | // Compute from the block above. A post-order traversal ensures the |
| 179 | // predecessor is always computed first. |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 180 | unsigned PredNum = TBI->Pred->getNumber(); |
| 181 | TraceBlockInfo *PredTBI = &BlockInfo[PredNum]; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 182 | assert(PredTBI->hasValidDepth() && "Trace above has not been computed yet"); |
Jakob Stoklund Olesen | 1dfb101 | 2012-07-31 20:25:13 +0000 | [diff] [blame] | 183 | const FixedBlockInfo *PredFBI = MTM.getResources(TBI->Pred); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 184 | TBI->InstrDepth = PredTBI->InstrDepth + PredFBI->InstrCount; |
Jakob Stoklund Olesen | 1152202 | 2012-07-27 23:58:36 +0000 | [diff] [blame] | 185 | TBI->Head = PredTBI->Head; |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 186 | |
| 187 | // Compute per-resource depths. |
| 188 | ArrayRef<unsigned> PredPRDepths = getProcResourceDepths(PredNum); |
| 189 | ArrayRef<unsigned> PredPRCycles = MTM.getProcResourceCycles(PredNum); |
| 190 | for (unsigned K = 0; K != PRKinds; ++K) |
| 191 | ProcResourceDepths[PROffset + K] = PredPRDepths[K] + PredPRCycles[K]; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | // Update resource-related information in the TraceBlockInfo for MBB. |
| 195 | // Only update resources related to the trace below MBB. |
| 196 | void MachineTraceMetrics::Ensemble:: |
| 197 | computeHeightResources(const MachineBasicBlock *MBB) { |
| 198 | TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()]; |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 199 | unsigned PRKinds = MTM.SchedModel.getNumProcResourceKinds(); |
| 200 | unsigned PROffset = MBB->getNumber() * PRKinds; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 201 | |
| 202 | // Compute resources for the current block. |
Jakob Stoklund Olesen | 1dfb101 | 2012-07-31 20:25:13 +0000 | [diff] [blame] | 203 | TBI->InstrHeight = MTM.getResources(MBB)->InstrCount; |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 204 | ArrayRef<unsigned> PRCycles = MTM.getProcResourceCycles(MBB->getNumber()); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 205 | |
| 206 | // The trace tail is done. |
Jakob Stoklund Olesen | 1152202 | 2012-07-27 23:58:36 +0000 | [diff] [blame] | 207 | if (!TBI->Succ) { |
| 208 | TBI->Tail = MBB->getNumber(); |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 209 | std::copy(PRCycles.begin(), PRCycles.end(), |
| 210 | ProcResourceHeights.begin() + PROffset); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 211 | return; |
Jakob Stoklund Olesen | 1152202 | 2012-07-27 23:58:36 +0000 | [diff] [blame] | 212 | } |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 213 | |
| 214 | // Compute from the block below. A post-order traversal ensures the |
| 215 | // predecessor is always computed first. |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 216 | unsigned SuccNum = TBI->Succ->getNumber(); |
| 217 | TraceBlockInfo *SuccTBI = &BlockInfo[SuccNum]; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 218 | assert(SuccTBI->hasValidHeight() && "Trace below has not been computed yet"); |
| 219 | TBI->InstrHeight += SuccTBI->InstrHeight; |
Jakob Stoklund Olesen | 1152202 | 2012-07-27 23:58:36 +0000 | [diff] [blame] | 220 | TBI->Tail = SuccTBI->Tail; |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 221 | |
| 222 | // Compute per-resource heights. |
| 223 | ArrayRef<unsigned> SuccPRHeights = getProcResourceHeights(SuccNum); |
| 224 | for (unsigned K = 0; K != PRKinds; ++K) |
| 225 | ProcResourceHeights[PROffset + K] = SuccPRHeights[K] + PRCycles[K]; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // Check if depth resources for MBB are valid and return the TBI. |
| 229 | // Return NULL if the resources have been invalidated. |
| 230 | const MachineTraceMetrics::TraceBlockInfo* |
| 231 | MachineTraceMetrics::Ensemble:: |
| 232 | getDepthResources(const MachineBasicBlock *MBB) const { |
| 233 | const TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()]; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 234 | return TBI->hasValidDepth() ? TBI : nullptr; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | // Check if height resources for MBB are valid and return the TBI. |
| 238 | // Return NULL if the resources have been invalidated. |
| 239 | const MachineTraceMetrics::TraceBlockInfo* |
| 240 | MachineTraceMetrics::Ensemble:: |
| 241 | getHeightResources(const MachineBasicBlock *MBB) const { |
| 242 | const TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()]; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 243 | return TBI->hasValidHeight() ? TBI : nullptr; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 246 | /// Get an array of processor resource depths for MBB. Indexed by processor |
| 247 | /// resource kind, this array contains the scaled processor resources consumed |
| 248 | /// by all blocks preceding MBB in its trace. It does not include instructions |
| 249 | /// in MBB. |
| 250 | /// |
| 251 | /// Compare TraceBlockInfo::InstrDepth. |
| 252 | ArrayRef<unsigned> |
| 253 | MachineTraceMetrics::Ensemble:: |
| 254 | getProcResourceDepths(unsigned MBBNum) const { |
| 255 | unsigned PRKinds = MTM.SchedModel.getNumProcResourceKinds(); |
Jakob Stoklund Olesen | aeb69a5 | 2013-04-02 22:27:45 +0000 | [diff] [blame] | 256 | assert((MBBNum+1) * PRKinds <= ProcResourceDepths.size()); |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 257 | return makeArrayRef(ProcResourceDepths.data() + MBBNum * PRKinds, PRKinds); |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /// Get an array of processor resource heights for MBB. Indexed by processor |
| 261 | /// resource kind, this array contains the scaled processor resources consumed |
| 262 | /// by this block and all blocks following it in its trace. |
| 263 | /// |
| 264 | /// Compare TraceBlockInfo::InstrHeight. |
| 265 | ArrayRef<unsigned> |
| 266 | MachineTraceMetrics::Ensemble:: |
| 267 | getProcResourceHeights(unsigned MBBNum) const { |
| 268 | unsigned PRKinds = MTM.SchedModel.getNumProcResourceKinds(); |
Jakob Stoklund Olesen | aeb69a5 | 2013-04-02 22:27:45 +0000 | [diff] [blame] | 269 | assert((MBBNum+1) * PRKinds <= ProcResourceHeights.size()); |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 270 | return makeArrayRef(ProcResourceHeights.data() + MBBNum * PRKinds, PRKinds); |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 273 | //===----------------------------------------------------------------------===// |
| 274 | // Trace Selection Strategies |
| 275 | //===----------------------------------------------------------------------===// |
| 276 | // |
| 277 | // A trace selection strategy is implemented as a sub-class of Ensemble. The |
| 278 | // trace through a block B is computed by two DFS traversals of the CFG |
| 279 | // starting from B. One upwards, and one downwards. During the upwards DFS, |
| 280 | // pickTracePred() is called on the post-ordered blocks. During the downwards |
| 281 | // DFS, pickTraceSucc() is called in a post-order. |
| 282 | // |
| 283 | |
Jakob Stoklund Olesen | c14cf57 | 2012-07-30 23:15:10 +0000 | [diff] [blame] | 284 | // We never allow traces that leave loops, but we do allow traces to enter |
| 285 | // nested loops. We also never allow traces to contain back-edges. |
| 286 | // |
| 287 | // This means that a loop header can never appear above the center block of a |
| 288 | // trace, except as the trace head. Below the center block, loop exiting edges |
| 289 | // are banned. |
| 290 | // |
| 291 | // Return true if an edge from the From loop to the To loop is leaving a loop. |
| 292 | // Either of To and From can be null. |
| 293 | static bool isExitingLoop(const MachineLoop *From, const MachineLoop *To) { |
| 294 | return From && !From->contains(To); |
| 295 | } |
| 296 | |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 297 | // MinInstrCountEnsemble - Pick the trace that executes the least number of |
| 298 | // instructions. |
| 299 | namespace { |
| 300 | class MinInstrCountEnsemble : public MachineTraceMetrics::Ensemble { |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 301 | const char *getName() const override { return "MinInstr"; } |
| 302 | const MachineBasicBlock *pickTracePred(const MachineBasicBlock*) override; |
| 303 | const MachineBasicBlock *pickTraceSucc(const MachineBasicBlock*) override; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 304 | |
| 305 | public: |
Jakob Stoklund Olesen | 1dfb101 | 2012-07-31 20:25:13 +0000 | [diff] [blame] | 306 | MinInstrCountEnsemble(MachineTraceMetrics *mtm) |
| 307 | : MachineTraceMetrics::Ensemble(mtm) {} |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 308 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 309 | } |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 310 | |
| 311 | // Select the preferred predecessor for MBB. |
| 312 | const MachineBasicBlock* |
| 313 | MinInstrCountEnsemble::pickTracePred(const MachineBasicBlock *MBB) { |
| 314 | if (MBB->pred_empty()) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 315 | return nullptr; |
Jakob Stoklund Olesen | 3df6c46 | 2012-07-30 18:34:11 +0000 | [diff] [blame] | 316 | const MachineLoop *CurLoop = getLoopFor(MBB); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 317 | // Don't leave loops, and never follow back-edges. |
| 318 | if (CurLoop && MBB == CurLoop->getHeader()) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 319 | return nullptr; |
Jakob Stoklund Olesen | 1dfb101 | 2012-07-31 20:25:13 +0000 | [diff] [blame] | 320 | unsigned CurCount = MTM.getResources(MBB)->InstrCount; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 321 | const MachineBasicBlock *Best = nullptr; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 322 | unsigned BestDepth = 0; |
Sanjay Patel | 99b3aa3 | 2015-05-21 17:22:45 +0000 | [diff] [blame] | 323 | for (const MachineBasicBlock *Pred : MBB->predecessors()) { |
Jakob Stoklund Olesen | f308c12 | 2012-07-30 21:10:27 +0000 | [diff] [blame] | 324 | const MachineTraceMetrics::TraceBlockInfo *PredTBI = |
| 325 | getDepthResources(Pred); |
Jakob Stoklund Olesen | bf1ac4b | 2012-08-08 22:12:01 +0000 | [diff] [blame] | 326 | // Ignore cycles that aren't natural loops. |
| 327 | if (!PredTBI) |
| 328 | continue; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 329 | // Pick the predecessor that would give this block the smallest InstrDepth. |
| 330 | unsigned Depth = PredTBI->InstrDepth + CurCount; |
| 331 | if (!Best || Depth < BestDepth) |
| 332 | Best = Pred, BestDepth = Depth; |
| 333 | } |
| 334 | return Best; |
| 335 | } |
| 336 | |
| 337 | // Select the preferred successor for MBB. |
| 338 | const MachineBasicBlock* |
| 339 | MinInstrCountEnsemble::pickTraceSucc(const MachineBasicBlock *MBB) { |
| 340 | if (MBB->pred_empty()) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 341 | return nullptr; |
Jakob Stoklund Olesen | 3df6c46 | 2012-07-30 18:34:11 +0000 | [diff] [blame] | 342 | const MachineLoop *CurLoop = getLoopFor(MBB); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 343 | const MachineBasicBlock *Best = nullptr; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 344 | unsigned BestHeight = 0; |
Sanjay Patel | 99b3aa3 | 2015-05-21 17:22:45 +0000 | [diff] [blame] | 345 | for (const MachineBasicBlock *Succ : MBB->successors()) { |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 346 | // Don't consider back-edges. |
| 347 | if (CurLoop && Succ == CurLoop->getHeader()) |
| 348 | continue; |
Jakob Stoklund Olesen | c14cf57 | 2012-07-30 23:15:10 +0000 | [diff] [blame] | 349 | // Don't consider successors exiting CurLoop. |
| 350 | if (isExitingLoop(CurLoop, getLoopFor(Succ))) |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 351 | continue; |
Jakob Stoklund Olesen | f308c12 | 2012-07-30 21:10:27 +0000 | [diff] [blame] | 352 | const MachineTraceMetrics::TraceBlockInfo *SuccTBI = |
| 353 | getHeightResources(Succ); |
Jakob Stoklund Olesen | bf1ac4b | 2012-08-08 22:12:01 +0000 | [diff] [blame] | 354 | // Ignore cycles that aren't natural loops. |
| 355 | if (!SuccTBI) |
| 356 | continue; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 357 | // Pick the successor that would give this block the smallest InstrHeight. |
| 358 | unsigned Height = SuccTBI->InstrHeight; |
| 359 | if (!Best || Height < BestHeight) |
| 360 | Best = Succ, BestHeight = Height; |
| 361 | } |
| 362 | return Best; |
| 363 | } |
| 364 | |
| 365 | // Get an Ensemble sub-class for the requested trace strategy. |
| 366 | MachineTraceMetrics::Ensemble * |
| 367 | MachineTraceMetrics::getEnsemble(MachineTraceMetrics::Strategy strategy) { |
| 368 | assert(strategy < TS_NumStrategies && "Invalid trace strategy enum"); |
| 369 | Ensemble *&E = Ensembles[strategy]; |
| 370 | if (E) |
| 371 | return E; |
| 372 | |
| 373 | // Allocate new Ensemble on demand. |
| 374 | switch (strategy) { |
| 375 | case TS_MinInstrCount: return (E = new MinInstrCountEnsemble(this)); |
| 376 | default: llvm_unreachable("Invalid trace strategy enum"); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | void MachineTraceMetrics::invalidate(const MachineBasicBlock *MBB) { |
| 381 | DEBUG(dbgs() << "Invalidate traces through BB#" << MBB->getNumber() << '\n'); |
| 382 | BlockInfo[MBB->getNumber()].invalidate(); |
| 383 | for (unsigned i = 0; i != TS_NumStrategies; ++i) |
| 384 | if (Ensembles[i]) |
| 385 | Ensembles[i]->invalidate(MBB); |
| 386 | } |
| 387 | |
Jakob Stoklund Olesen | a12a7d5 | 2012-07-30 20:57:50 +0000 | [diff] [blame] | 388 | void MachineTraceMetrics::verifyAnalysis() const { |
Jakob Stoklund Olesen | 68c2cd0 | 2012-07-30 23:15:12 +0000 | [diff] [blame] | 389 | if (!MF) |
| 390 | return; |
Jakob Stoklund Olesen | 3df6c46 | 2012-07-30 18:34:11 +0000 | [diff] [blame] | 391 | #ifndef NDEBUG |
| 392 | assert(BlockInfo.size() == MF->getNumBlockIDs() && "Outdated BlockInfo size"); |
| 393 | for (unsigned i = 0; i != TS_NumStrategies; ++i) |
| 394 | if (Ensembles[i]) |
| 395 | Ensembles[i]->verify(); |
| 396 | #endif |
| 397 | } |
| 398 | |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 399 | //===----------------------------------------------------------------------===// |
| 400 | // Trace building |
| 401 | //===----------------------------------------------------------------------===// |
| 402 | // |
| 403 | // Traces are built by two CFG traversals. To avoid recomputing too much, use a |
| 404 | // set abstraction that confines the search to the current loop, and doesn't |
| 405 | // revisit blocks. |
| 406 | |
| 407 | namespace { |
| 408 | struct LoopBounds { |
| 409 | MutableArrayRef<MachineTraceMetrics::TraceBlockInfo> Blocks; |
Jakob Stoklund Olesen | bf1ac4b | 2012-08-08 22:12:01 +0000 | [diff] [blame] | 410 | SmallPtrSet<const MachineBasicBlock*, 8> Visited; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 411 | const MachineLoopInfo *Loops; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 412 | bool Downward; |
| 413 | LoopBounds(MutableArrayRef<MachineTraceMetrics::TraceBlockInfo> blocks, |
Jakob Stoklund Olesen | c14cf57 | 2012-07-30 23:15:10 +0000 | [diff] [blame] | 414 | const MachineLoopInfo *loops) |
| 415 | : Blocks(blocks), Loops(loops), Downward(false) {} |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 416 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 417 | } |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 418 | |
| 419 | // Specialize po_iterator_storage in order to prune the post-order traversal so |
| 420 | // it is limited to the current loop and doesn't traverse the loop back edges. |
| 421 | namespace llvm { |
| 422 | template<> |
| 423 | class po_iterator_storage<LoopBounds, true> { |
| 424 | LoopBounds &LB; |
| 425 | public: |
| 426 | po_iterator_storage(LoopBounds &lb) : LB(lb) {} |
| 427 | void finishPostorder(const MachineBasicBlock*) {} |
| 428 | |
| 429 | bool insertEdge(const MachineBasicBlock *From, const MachineBasicBlock *To) { |
| 430 | // Skip already visited To blocks. |
| 431 | MachineTraceMetrics::TraceBlockInfo &TBI = LB.Blocks[To->getNumber()]; |
| 432 | if (LB.Downward ? TBI.hasValidHeight() : TBI.hasValidDepth()) |
| 433 | return false; |
Jakob Stoklund Olesen | c14cf57 | 2012-07-30 23:15:10 +0000 | [diff] [blame] | 434 | // From is null once when To is the trace center block. |
Jakob Stoklund Olesen | bf1ac4b | 2012-08-08 22:12:01 +0000 | [diff] [blame] | 435 | if (From) { |
| 436 | if (const MachineLoop *FromLoop = LB.Loops->getLoopFor(From)) { |
| 437 | // Don't follow backedges, don't leave FromLoop when going upwards. |
| 438 | if ((LB.Downward ? To : From) == FromLoop->getHeader()) |
| 439 | return false; |
| 440 | // Don't leave FromLoop. |
| 441 | if (isExitingLoop(FromLoop, LB.Loops->getLoopFor(To))) |
| 442 | return false; |
| 443 | } |
| 444 | } |
| 445 | // To is a new block. Mark the block as visited in case the CFG has cycles |
| 446 | // that MachineLoopInfo didn't recognize as a natural loop. |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 447 | return LB.Visited.insert(To).second; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 448 | } |
| 449 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 450 | } |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 451 | |
| 452 | /// Compute the trace through MBB. |
| 453 | void MachineTraceMetrics::Ensemble::computeTrace(const MachineBasicBlock *MBB) { |
| 454 | DEBUG(dbgs() << "Computing " << getName() << " trace through BB#" |
| 455 | << MBB->getNumber() << '\n'); |
| 456 | // Set up loop bounds for the backwards post-order traversal. |
Jakob Stoklund Olesen | 1dfb101 | 2012-07-31 20:25:13 +0000 | [diff] [blame] | 457 | LoopBounds Bounds(BlockInfo, MTM.Loops); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 458 | |
| 459 | // Run an upwards post-order search for the trace start. |
| 460 | Bounds.Downward = false; |
Jakob Stoklund Olesen | bf1ac4b | 2012-08-08 22:12:01 +0000 | [diff] [blame] | 461 | Bounds.Visited.clear(); |
Daniel Berlin | 25db4f4 | 2015-04-15 17:41:42 +0000 | [diff] [blame] | 462 | for (auto I : inverse_post_order_ext(MBB, Bounds)) { |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 463 | DEBUG(dbgs() << " pred for BB#" << I->getNumber() << ": "); |
| 464 | TraceBlockInfo &TBI = BlockInfo[I->getNumber()]; |
| 465 | // All the predecessors have been visited, pick the preferred one. |
Daniel Berlin | 25db4f4 | 2015-04-15 17:41:42 +0000 | [diff] [blame] | 466 | TBI.Pred = pickTracePred(I); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 467 | DEBUG({ |
| 468 | if (TBI.Pred) |
| 469 | dbgs() << "BB#" << TBI.Pred->getNumber() << '\n'; |
| 470 | else |
| 471 | dbgs() << "null\n"; |
| 472 | }); |
| 473 | // The trace leading to I is now known, compute the depth resources. |
Daniel Berlin | 25db4f4 | 2015-04-15 17:41:42 +0000 | [diff] [blame] | 474 | computeDepthResources(I); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | // Run a downwards post-order search for the trace end. |
| 478 | Bounds.Downward = true; |
Jakob Stoklund Olesen | bf1ac4b | 2012-08-08 22:12:01 +0000 | [diff] [blame] | 479 | Bounds.Visited.clear(); |
Daniel Berlin | 25db4f4 | 2015-04-15 17:41:42 +0000 | [diff] [blame] | 480 | for (auto I : post_order_ext(MBB, Bounds)) { |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 481 | DEBUG(dbgs() << " succ for BB#" << I->getNumber() << ": "); |
| 482 | TraceBlockInfo &TBI = BlockInfo[I->getNumber()]; |
| 483 | // All the successors have been visited, pick the preferred one. |
Daniel Berlin | 25db4f4 | 2015-04-15 17:41:42 +0000 | [diff] [blame] | 484 | TBI.Succ = pickTraceSucc(I); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 485 | DEBUG({ |
Jakob Stoklund Olesen | c14cf57 | 2012-07-30 23:15:10 +0000 | [diff] [blame] | 486 | if (TBI.Succ) |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 487 | dbgs() << "BB#" << TBI.Succ->getNumber() << '\n'; |
| 488 | else |
| 489 | dbgs() << "null\n"; |
| 490 | }); |
| 491 | // The trace leaving I is now known, compute the height resources. |
Daniel Berlin | 25db4f4 | 2015-04-15 17:41:42 +0000 | [diff] [blame] | 492 | computeHeightResources(I); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
| 496 | /// Invalidate traces through BadMBB. |
| 497 | void |
| 498 | MachineTraceMetrics::Ensemble::invalidate(const MachineBasicBlock *BadMBB) { |
| 499 | SmallVector<const MachineBasicBlock*, 16> WorkList; |
| 500 | TraceBlockInfo &BadTBI = BlockInfo[BadMBB->getNumber()]; |
| 501 | |
| 502 | // Invalidate height resources of blocks above MBB. |
| 503 | if (BadTBI.hasValidHeight()) { |
| 504 | BadTBI.invalidateHeight(); |
| 505 | WorkList.push_back(BadMBB); |
| 506 | do { |
| 507 | const MachineBasicBlock *MBB = WorkList.pop_back_val(); |
| 508 | DEBUG(dbgs() << "Invalidate BB#" << MBB->getNumber() << ' ' << getName() |
| 509 | << " height.\n"); |
| 510 | // Find any MBB predecessors that have MBB as their preferred successor. |
| 511 | // They are the only ones that need to be invalidated. |
Sanjay Patel | d2b7144 | 2015-07-06 16:27:35 +0000 | [diff] [blame] | 512 | for (const MachineBasicBlock *Pred : MBB->predecessors()) { |
| 513 | TraceBlockInfo &TBI = BlockInfo[Pred->getNumber()]; |
Jakob Stoklund Olesen | eb488fe | 2012-07-30 17:36:49 +0000 | [diff] [blame] | 514 | if (!TBI.hasValidHeight()) |
| 515 | continue; |
| 516 | if (TBI.Succ == MBB) { |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 517 | TBI.invalidateHeight(); |
Sanjay Patel | d2b7144 | 2015-07-06 16:27:35 +0000 | [diff] [blame] | 518 | WorkList.push_back(Pred); |
Jakob Stoklund Olesen | eb488fe | 2012-07-30 17:36:49 +0000 | [diff] [blame] | 519 | continue; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 520 | } |
Jakob Stoklund Olesen | eb488fe | 2012-07-30 17:36:49 +0000 | [diff] [blame] | 521 | // Verify that TBI.Succ is actually a *I successor. |
Sanjay Patel | d2b7144 | 2015-07-06 16:27:35 +0000 | [diff] [blame] | 522 | assert((!TBI.Succ || Pred->isSuccessor(TBI.Succ)) && "CFG changed"); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 523 | } |
| 524 | } while (!WorkList.empty()); |
| 525 | } |
| 526 | |
| 527 | // Invalidate depth resources of blocks below MBB. |
| 528 | if (BadTBI.hasValidDepth()) { |
| 529 | BadTBI.invalidateDepth(); |
| 530 | WorkList.push_back(BadMBB); |
| 531 | do { |
| 532 | const MachineBasicBlock *MBB = WorkList.pop_back_val(); |
| 533 | DEBUG(dbgs() << "Invalidate BB#" << MBB->getNumber() << ' ' << getName() |
| 534 | << " depth.\n"); |
| 535 | // Find any MBB successors that have MBB as their preferred predecessor. |
| 536 | // They are the only ones that need to be invalidated. |
Sanjay Patel | d2b7144 | 2015-07-06 16:27:35 +0000 | [diff] [blame] | 537 | for (const MachineBasicBlock *Succ : MBB->successors()) { |
| 538 | TraceBlockInfo &TBI = BlockInfo[Succ->getNumber()]; |
Jakob Stoklund Olesen | eb488fe | 2012-07-30 17:36:49 +0000 | [diff] [blame] | 539 | if (!TBI.hasValidDepth()) |
| 540 | continue; |
| 541 | if (TBI.Pred == MBB) { |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 542 | TBI.invalidateDepth(); |
Sanjay Patel | d2b7144 | 2015-07-06 16:27:35 +0000 | [diff] [blame] | 543 | WorkList.push_back(Succ); |
Jakob Stoklund Olesen | eb488fe | 2012-07-30 17:36:49 +0000 | [diff] [blame] | 544 | continue; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 545 | } |
Jakob Stoklund Olesen | eb488fe | 2012-07-30 17:36:49 +0000 | [diff] [blame] | 546 | // Verify that TBI.Pred is actually a *I predecessor. |
Sanjay Patel | d2b7144 | 2015-07-06 16:27:35 +0000 | [diff] [blame] | 547 | assert((!TBI.Pred || Succ->isPredecessor(TBI.Pred)) && "CFG changed"); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 548 | } |
| 549 | } while (!WorkList.empty()); |
| 550 | } |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 551 | |
| 552 | // Clear any per-instruction data. We only have to do this for BadMBB itself |
| 553 | // because the instructions in that block may change. Other blocks may be |
| 554 | // invalidated, but their instructions will stay the same, so there is no |
| 555 | // need to erase the Cycle entries. They will be overwritten when we |
| 556 | // recompute. |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 557 | for (const auto &I : *BadMBB) |
| 558 | Cycles.erase(&I); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Jakob Stoklund Olesen | 3df6c46 | 2012-07-30 18:34:11 +0000 | [diff] [blame] | 561 | void MachineTraceMetrics::Ensemble::verify() const { |
| 562 | #ifndef NDEBUG |
Jakob Stoklund Olesen | 1dfb101 | 2012-07-31 20:25:13 +0000 | [diff] [blame] | 563 | assert(BlockInfo.size() == MTM.MF->getNumBlockIDs() && |
Jakob Stoklund Olesen | 3df6c46 | 2012-07-30 18:34:11 +0000 | [diff] [blame] | 564 | "Outdated BlockInfo size"); |
| 565 | for (unsigned Num = 0, e = BlockInfo.size(); Num != e; ++Num) { |
| 566 | const TraceBlockInfo &TBI = BlockInfo[Num]; |
| 567 | if (TBI.hasValidDepth() && TBI.Pred) { |
Jakob Stoklund Olesen | 1dfb101 | 2012-07-31 20:25:13 +0000 | [diff] [blame] | 568 | const MachineBasicBlock *MBB = MTM.MF->getBlockNumbered(Num); |
Jakob Stoklund Olesen | 3df6c46 | 2012-07-30 18:34:11 +0000 | [diff] [blame] | 569 | assert(MBB->isPredecessor(TBI.Pred) && "CFG doesn't match trace"); |
| 570 | assert(BlockInfo[TBI.Pred->getNumber()].hasValidDepth() && |
| 571 | "Trace is broken, depth should have been invalidated."); |
| 572 | const MachineLoop *Loop = getLoopFor(MBB); |
| 573 | assert(!(Loop && MBB == Loop->getHeader()) && "Trace contains backedge"); |
| 574 | } |
| 575 | if (TBI.hasValidHeight() && TBI.Succ) { |
Jakob Stoklund Olesen | 1dfb101 | 2012-07-31 20:25:13 +0000 | [diff] [blame] | 576 | const MachineBasicBlock *MBB = MTM.MF->getBlockNumbered(Num); |
Jakob Stoklund Olesen | 3df6c46 | 2012-07-30 18:34:11 +0000 | [diff] [blame] | 577 | assert(MBB->isSuccessor(TBI.Succ) && "CFG doesn't match trace"); |
| 578 | assert(BlockInfo[TBI.Succ->getNumber()].hasValidHeight() && |
| 579 | "Trace is broken, height should have been invalidated."); |
| 580 | const MachineLoop *Loop = getLoopFor(MBB); |
| 581 | const MachineLoop *SuccLoop = getLoopFor(TBI.Succ); |
| 582 | assert(!(Loop && Loop == SuccLoop && TBI.Succ == Loop->getHeader()) && |
| 583 | "Trace contains backedge"); |
| 584 | } |
| 585 | } |
| 586 | #endif |
| 587 | } |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 588 | |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 589 | //===----------------------------------------------------------------------===// |
| 590 | // Data Dependencies |
| 591 | //===----------------------------------------------------------------------===// |
| 592 | // |
| 593 | // Compute the depth and height of each instruction based on data dependencies |
| 594 | // and instruction latencies. These cycle numbers assume that the CPU can issue |
| 595 | // an infinite number of instructions per cycle as long as their dependencies |
| 596 | // are ready. |
| 597 | |
| 598 | // A data dependency is represented as a defining MI and operand numbers on the |
| 599 | // defining and using MI. |
| 600 | namespace { |
| 601 | struct DataDep { |
| 602 | const MachineInstr *DefMI; |
| 603 | unsigned DefOp; |
| 604 | unsigned UseOp; |
Jakob Stoklund Olesen | 5e19d35 | 2012-08-01 16:02:59 +0000 | [diff] [blame] | 605 | |
| 606 | DataDep(const MachineInstr *DefMI, unsigned DefOp, unsigned UseOp) |
| 607 | : DefMI(DefMI), DefOp(DefOp), UseOp(UseOp) {} |
| 608 | |
| 609 | /// Create a DataDep from an SSA form virtual register. |
| 610 | DataDep(const MachineRegisterInfo *MRI, unsigned VirtReg, unsigned UseOp) |
| 611 | : UseOp(UseOp) { |
| 612 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg)); |
| 613 | MachineRegisterInfo::def_iterator DefI = MRI->def_begin(VirtReg); |
| 614 | assert(!DefI.atEnd() && "Register has no defs"); |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 615 | DefMI = DefI->getParent(); |
Jakob Stoklund Olesen | 5e19d35 | 2012-08-01 16:02:59 +0000 | [diff] [blame] | 616 | DefOp = DefI.getOperandNo(); |
| 617 | assert((++DefI).atEnd() && "Register has multiple defs"); |
| 618 | } |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 619 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 620 | } |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 621 | |
| 622 | // Get the input data dependencies that must be ready before UseMI can issue. |
| 623 | // Return true if UseMI has any physreg operands. |
| 624 | static bool getDataDeps(const MachineInstr *UseMI, |
| 625 | SmallVectorImpl<DataDep> &Deps, |
| 626 | const MachineRegisterInfo *MRI) { |
Sanjay Patel | f2fa58e | 2015-07-23 22:56:53 +0000 | [diff] [blame] | 627 | // Debug values should not be included in any calculations. |
| 628 | if (UseMI->isDebugValue()) |
| 629 | return false; |
| 630 | |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 631 | bool HasPhysRegs = false; |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 632 | for (MachineInstr::const_mop_iterator I = UseMI->operands_begin(), |
| 633 | E = UseMI->operands_end(); I != E; ++I) { |
| 634 | const MachineOperand &MO = *I; |
| 635 | if (!MO.isReg()) |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 636 | continue; |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 637 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 638 | if (!Reg) |
| 639 | continue; |
| 640 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
| 641 | HasPhysRegs = true; |
| 642 | continue; |
| 643 | } |
| 644 | // Collect virtual register reads. |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 645 | if (MO.readsReg()) |
| 646 | Deps.push_back(DataDep(MRI, Reg, UseMI->getOperandNo(I))); |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 647 | } |
| 648 | return HasPhysRegs; |
| 649 | } |
| 650 | |
| 651 | // Get the input data dependencies of a PHI instruction, using Pred as the |
| 652 | // preferred predecessor. |
| 653 | // This will add at most one dependency to Deps. |
| 654 | static void getPHIDeps(const MachineInstr *UseMI, |
| 655 | SmallVectorImpl<DataDep> &Deps, |
| 656 | const MachineBasicBlock *Pred, |
| 657 | const MachineRegisterInfo *MRI) { |
| 658 | // No predecessor at the beginning of a trace. Ignore dependencies. |
| 659 | if (!Pred) |
| 660 | return; |
| 661 | assert(UseMI->isPHI() && UseMI->getNumOperands() % 2 && "Bad PHI"); |
| 662 | for (unsigned i = 1; i != UseMI->getNumOperands(); i += 2) { |
| 663 | if (UseMI->getOperand(i + 1).getMBB() == Pred) { |
| 664 | unsigned Reg = UseMI->getOperand(i).getReg(); |
Jakob Stoklund Olesen | 5e19d35 | 2012-08-01 16:02:59 +0000 | [diff] [blame] | 665 | Deps.push_back(DataDep(MRI, Reg, i)); |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 666 | return; |
| 667 | } |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | // Keep track of physreg data dependencies by recording each live register unit. |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 672 | // Associate each regunit with an instruction operand. Depending on the |
| 673 | // direction instructions are scanned, it could be the operand that defined the |
| 674 | // regunit, or the highest operand to read the regunit. |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 675 | namespace { |
| 676 | struct LiveRegUnit { |
| 677 | unsigned RegUnit; |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 678 | unsigned Cycle; |
| 679 | const MachineInstr *MI; |
| 680 | unsigned Op; |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 681 | |
| 682 | unsigned getSparseSetIndex() const { return RegUnit; } |
| 683 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 684 | LiveRegUnit(unsigned RU) : RegUnit(RU), Cycle(0), MI(nullptr), Op(0) {} |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 685 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 686 | } |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 687 | |
| 688 | // Identify physreg dependencies for UseMI, and update the live regunit |
| 689 | // tracking set when scanning instructions downwards. |
| 690 | static void updatePhysDepsDownwards(const MachineInstr *UseMI, |
| 691 | SmallVectorImpl<DataDep> &Deps, |
| 692 | SparseSet<LiveRegUnit> &RegUnits, |
| 693 | const TargetRegisterInfo *TRI) { |
| 694 | SmallVector<unsigned, 8> Kills; |
| 695 | SmallVector<unsigned, 8> LiveDefOps; |
| 696 | |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 697 | for (MachineInstr::const_mop_iterator MI = UseMI->operands_begin(), |
| 698 | ME = UseMI->operands_end(); MI != ME; ++MI) { |
| 699 | const MachineOperand &MO = *MI; |
| 700 | if (!MO.isReg()) |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 701 | continue; |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 702 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 703 | if (!TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 704 | continue; |
| 705 | // Track live defs and kills for updating RegUnits. |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 706 | if (MO.isDef()) { |
| 707 | if (MO.isDead()) |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 708 | Kills.push_back(Reg); |
| 709 | else |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 710 | LiveDefOps.push_back(UseMI->getOperandNo(MI)); |
| 711 | } else if (MO.isKill()) |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 712 | Kills.push_back(Reg); |
| 713 | // Identify dependencies. |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 714 | if (!MO.readsReg()) |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 715 | continue; |
| 716 | for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) { |
| 717 | SparseSet<LiveRegUnit>::iterator I = RegUnits.find(*Units); |
| 718 | if (I == RegUnits.end()) |
| 719 | continue; |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 720 | Deps.push_back(DataDep(I->MI, I->Op, UseMI->getOperandNo(MI))); |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 721 | break; |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | // Update RegUnits to reflect live registers after UseMI. |
| 726 | // First kills. |
Sanjay Patel | 87d2ae2 | 2015-12-09 22:45:45 +0000 | [diff] [blame^] | 727 | for (unsigned Kill : Kills) |
| 728 | for (MCRegUnitIterator Units(Kill, TRI); Units.isValid(); ++Units) |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 729 | RegUnits.erase(*Units); |
| 730 | |
| 731 | // Second, live defs. |
Sanjay Patel | 87d2ae2 | 2015-12-09 22:45:45 +0000 | [diff] [blame^] | 732 | for (unsigned DefOp : LiveDefOps) { |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 733 | for (MCRegUnitIterator Units(UseMI->getOperand(DefOp).getReg(), TRI); |
| 734 | Units.isValid(); ++Units) { |
| 735 | LiveRegUnit &LRU = RegUnits[*Units]; |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 736 | LRU.MI = UseMI; |
| 737 | LRU.Op = DefOp; |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | } |
| 741 | |
Jakob Stoklund Olesen | 5d30630 | 2012-08-02 18:45:54 +0000 | [diff] [blame] | 742 | /// The length of the critical path through a trace is the maximum of two path |
| 743 | /// lengths: |
| 744 | /// |
| 745 | /// 1. The maximum height+depth over all instructions in the trace center block. |
| 746 | /// |
| 747 | /// 2. The longest cross-block dependency chain. For small blocks, it is |
| 748 | /// possible that the critical path through the trace doesn't include any |
| 749 | /// instructions in the block. |
| 750 | /// |
| 751 | /// This function computes the second number from the live-in list of the |
| 752 | /// center block. |
| 753 | unsigned MachineTraceMetrics::Ensemble:: |
| 754 | computeCrossBlockCriticalPath(const TraceBlockInfo &TBI) { |
| 755 | assert(TBI.HasValidInstrDepths && "Missing depth info"); |
| 756 | assert(TBI.HasValidInstrHeights && "Missing height info"); |
| 757 | unsigned MaxLen = 0; |
Sanjay Patel | 87d2ae2 | 2015-12-09 22:45:45 +0000 | [diff] [blame^] | 758 | for (const LiveInReg &LIR : TBI.LiveIns) { |
Jakob Stoklund Olesen | 5d30630 | 2012-08-02 18:45:54 +0000 | [diff] [blame] | 759 | if (!TargetRegisterInfo::isVirtualRegister(LIR.Reg)) |
| 760 | continue; |
| 761 | const MachineInstr *DefMI = MTM.MRI->getVRegDef(LIR.Reg); |
| 762 | // Ignore dependencies outside the current trace. |
| 763 | const TraceBlockInfo &DefTBI = BlockInfo[DefMI->getParent()->getNumber()]; |
Jakob Stoklund Olesen | 299cedc | 2013-03-07 23:55:49 +0000 | [diff] [blame] | 764 | if (!DefTBI.isUsefulDominator(TBI)) |
Jakob Stoklund Olesen | 5d30630 | 2012-08-02 18:45:54 +0000 | [diff] [blame] | 765 | continue; |
| 766 | unsigned Len = LIR.Height + Cycles[DefMI].Depth; |
| 767 | MaxLen = std::max(MaxLen, Len); |
| 768 | } |
| 769 | return MaxLen; |
| 770 | } |
| 771 | |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 772 | /// Compute instruction depths for all instructions above or in MBB in its |
| 773 | /// trace. This assumes that the trace through MBB has already been computed. |
| 774 | void MachineTraceMetrics::Ensemble:: |
| 775 | computeInstrDepths(const MachineBasicBlock *MBB) { |
| 776 | // The top of the trace may already be computed, and HasValidInstrDepths |
| 777 | // implies Head->HasValidInstrDepths, so we only need to start from the first |
| 778 | // block in the trace that needs to be recomputed. |
| 779 | SmallVector<const MachineBasicBlock*, 8> Stack; |
| 780 | do { |
| 781 | TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()]; |
| 782 | assert(TBI.hasValidDepth() && "Incomplete trace"); |
| 783 | if (TBI.HasValidInstrDepths) |
| 784 | break; |
| 785 | Stack.push_back(MBB); |
| 786 | MBB = TBI.Pred; |
| 787 | } while (MBB); |
| 788 | |
| 789 | // FIXME: If MBB is non-null at this point, it is the last pre-computed block |
| 790 | // in the trace. We should track any live-out physregs that were defined in |
| 791 | // the trace. This is quite rare in SSA form, typically created by CSE |
| 792 | // hoisting a compare. |
| 793 | SparseSet<LiveRegUnit> RegUnits; |
| 794 | RegUnits.setUniverse(MTM.TRI->getNumRegUnits()); |
| 795 | |
| 796 | // Go through trace blocks in top-down order, stopping after the center block. |
| 797 | SmallVector<DataDep, 8> Deps; |
| 798 | while (!Stack.empty()) { |
| 799 | MBB = Stack.pop_back_val(); |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 800 | DEBUG(dbgs() << "\nDepths for BB#" << MBB->getNumber() << ":\n"); |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 801 | TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()]; |
| 802 | TBI.HasValidInstrDepths = true; |
Jakob Stoklund Olesen | 5d30630 | 2012-08-02 18:45:54 +0000 | [diff] [blame] | 803 | TBI.CriticalPath = 0; |
| 804 | |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 805 | // Print out resource depths here as well. |
| 806 | DEBUG({ |
| 807 | dbgs() << format("%7u Instructions\n", TBI.InstrDepth); |
| 808 | ArrayRef<unsigned> PRDepths = getProcResourceDepths(MBB->getNumber()); |
| 809 | for (unsigned K = 0; K != PRDepths.size(); ++K) |
| 810 | if (PRDepths[K]) { |
| 811 | unsigned Factor = MTM.SchedModel.getResourceFactor(K); |
| 812 | dbgs() << format("%6uc @ ", MTM.getCycles(PRDepths[K])) |
| 813 | << MTM.SchedModel.getProcResource(K)->Name << " (" |
| 814 | << PRDepths[K]/Factor << " ops x" << Factor << ")\n"; |
| 815 | } |
| 816 | }); |
| 817 | |
Jakob Stoklund Olesen | 5d30630 | 2012-08-02 18:45:54 +0000 | [diff] [blame] | 818 | // Also compute the critical path length through MBB when possible. |
| 819 | if (TBI.HasValidInstrHeights) |
| 820 | TBI.CriticalPath = computeCrossBlockCriticalPath(TBI); |
| 821 | |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 822 | for (const auto &UseMI : *MBB) { |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 823 | // Collect all data dependencies. |
| 824 | Deps.clear(); |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 825 | if (UseMI.isPHI()) |
| 826 | getPHIDeps(&UseMI, Deps, TBI.Pred, MTM.MRI); |
| 827 | else if (getDataDeps(&UseMI, Deps, MTM.MRI)) |
| 828 | updatePhysDepsDownwards(&UseMI, Deps, RegUnits, MTM.TRI); |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 829 | |
| 830 | // Filter and process dependencies, computing the earliest issue cycle. |
| 831 | unsigned Cycle = 0; |
Sanjay Patel | 0ca438c | 2015-06-30 16:30:22 +0000 | [diff] [blame] | 832 | for (const DataDep &Dep : Deps) { |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 833 | const TraceBlockInfo&DepTBI = |
| 834 | BlockInfo[Dep.DefMI->getParent()->getNumber()]; |
| 835 | // Ignore dependencies from outside the current trace. |
Jakob Stoklund Olesen | 299cedc | 2013-03-07 23:55:49 +0000 | [diff] [blame] | 836 | if (!DepTBI.isUsefulDominator(TBI)) |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 837 | continue; |
| 838 | assert(DepTBI.HasValidInstrDepths && "Inconsistent dependency"); |
| 839 | unsigned DepCycle = Cycles.lookup(Dep.DefMI).Depth; |
| 840 | // Add latency if DefMI is a real instruction. Transients get latency 0. |
| 841 | if (!Dep.DefMI->isTransient()) |
Jakob Stoklund Olesen | 8982222 | 2012-10-04 17:30:40 +0000 | [diff] [blame] | 842 | DepCycle += MTM.SchedModel |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 843 | .computeOperandLatency(Dep.DefMI, Dep.DefOp, &UseMI, Dep.UseOp); |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 844 | Cycle = std::max(Cycle, DepCycle); |
| 845 | } |
| 846 | // Remember the instruction depth. |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 847 | InstrCycles &MICycles = Cycles[&UseMI]; |
Jakob Stoklund Olesen | 5d30630 | 2012-08-02 18:45:54 +0000 | [diff] [blame] | 848 | MICycles.Depth = Cycle; |
| 849 | |
| 850 | if (!TBI.HasValidInstrHeights) { |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 851 | DEBUG(dbgs() << Cycle << '\t' << UseMI); |
Jakob Stoklund Olesen | 5d30630 | 2012-08-02 18:45:54 +0000 | [diff] [blame] | 852 | continue; |
| 853 | } |
| 854 | // Update critical path length. |
| 855 | TBI.CriticalPath = std::max(TBI.CriticalPath, Cycle + MICycles.Height); |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 856 | DEBUG(dbgs() << TBI.CriticalPath << '\t' << Cycle << '\t' << UseMI); |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | } |
| 860 | |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 861 | // Identify physreg dependencies for MI when scanning instructions upwards. |
| 862 | // Return the issue height of MI after considering any live regunits. |
| 863 | // Height is the issue height computed from virtual register dependencies alone. |
| 864 | static unsigned updatePhysDepsUpwards(const MachineInstr *MI, unsigned Height, |
| 865 | SparseSet<LiveRegUnit> &RegUnits, |
Jakob Stoklund Olesen | 8982222 | 2012-10-04 17:30:40 +0000 | [diff] [blame] | 866 | const TargetSchedModel &SchedModel, |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 867 | const TargetInstrInfo *TII, |
| 868 | const TargetRegisterInfo *TRI) { |
| 869 | SmallVector<unsigned, 8> ReadOps; |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 870 | |
| 871 | for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(), |
| 872 | MOE = MI->operands_end(); MOI != MOE; ++MOI) { |
| 873 | const MachineOperand &MO = *MOI; |
| 874 | if (!MO.isReg()) |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 875 | continue; |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 876 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 877 | if (!TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 878 | continue; |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 879 | if (MO.readsReg()) |
| 880 | ReadOps.push_back(MI->getOperandNo(MOI)); |
| 881 | if (!MO.isDef()) |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 882 | continue; |
| 883 | // This is a def of Reg. Remove corresponding entries from RegUnits, and |
| 884 | // update MI Height to consider the physreg dependencies. |
| 885 | for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) { |
| 886 | SparseSet<LiveRegUnit>::iterator I = RegUnits.find(*Units); |
| 887 | if (I == RegUnits.end()) |
| 888 | continue; |
| 889 | unsigned DepHeight = I->Cycle; |
| 890 | if (!MI->isTransient()) { |
| 891 | // We may not know the UseMI of this dependency, if it came from the |
Jakob Stoklund Olesen | 8982222 | 2012-10-04 17:30:40 +0000 | [diff] [blame] | 892 | // live-in list. SchedModel can handle a NULL UseMI. |
| 893 | DepHeight += SchedModel |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 894 | .computeOperandLatency(MI, MI->getOperandNo(MOI), I->MI, I->Op); |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 895 | } |
| 896 | Height = std::max(Height, DepHeight); |
| 897 | // This regunit is dead above MI. |
| 898 | RegUnits.erase(I); |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | // Now we know the height of MI. Update any regunits read. |
| 903 | for (unsigned i = 0, e = ReadOps.size(); i != e; ++i) { |
| 904 | unsigned Reg = MI->getOperand(ReadOps[i]).getReg(); |
| 905 | for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) { |
| 906 | LiveRegUnit &LRU = RegUnits[*Units]; |
| 907 | // Set the height to the highest reader of the unit. |
| 908 | if (LRU.Cycle <= Height && LRU.MI != MI) { |
| 909 | LRU.Cycle = Height; |
| 910 | LRU.MI = MI; |
| 911 | LRU.Op = ReadOps[i]; |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | return Height; |
| 917 | } |
| 918 | |
| 919 | |
| 920 | typedef DenseMap<const MachineInstr *, unsigned> MIHeightMap; |
| 921 | |
| 922 | // Push the height of DefMI upwards if required to match UseMI. |
| 923 | // Return true if this is the first time DefMI was seen. |
| 924 | static bool pushDepHeight(const DataDep &Dep, |
| 925 | const MachineInstr *UseMI, unsigned UseHeight, |
| 926 | MIHeightMap &Heights, |
Jakob Stoklund Olesen | 8982222 | 2012-10-04 17:30:40 +0000 | [diff] [blame] | 927 | const TargetSchedModel &SchedModel, |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 928 | const TargetInstrInfo *TII) { |
| 929 | // Adjust height by Dep.DefMI latency. |
| 930 | if (!Dep.DefMI->isTransient()) |
Jakob Stoklund Olesen | 8982222 | 2012-10-04 17:30:40 +0000 | [diff] [blame] | 931 | UseHeight += SchedModel.computeOperandLatency(Dep.DefMI, Dep.DefOp, |
Andrew Trick | de2109e | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 932 | UseMI, Dep.UseOp); |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 933 | |
| 934 | // Update Heights[DefMI] to be the maximum height seen. |
| 935 | MIHeightMap::iterator I; |
| 936 | bool New; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 937 | std::tie(I, New) = Heights.insert(std::make_pair(Dep.DefMI, UseHeight)); |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 938 | if (New) |
| 939 | return true; |
| 940 | |
| 941 | // DefMI has been pushed before. Give it the max height. |
| 942 | if (I->second < UseHeight) |
| 943 | I->second = UseHeight; |
| 944 | return false; |
| 945 | } |
| 946 | |
Jakob Stoklund Olesen | d0d7860 | 2012-10-11 16:46:07 +0000 | [diff] [blame] | 947 | /// Assuming that the virtual register defined by DefMI:DefOp was used by |
| 948 | /// Trace.back(), add it to the live-in lists of all the blocks in Trace. Stop |
| 949 | /// when reaching the block that contains DefMI. |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 950 | void MachineTraceMetrics::Ensemble:: |
Jakob Stoklund Olesen | d0d7860 | 2012-10-11 16:46:07 +0000 | [diff] [blame] | 951 | addLiveIns(const MachineInstr *DefMI, unsigned DefOp, |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 952 | ArrayRef<const MachineBasicBlock*> Trace) { |
| 953 | assert(!Trace.empty() && "Trace should contain at least one block"); |
Jakob Stoklund Olesen | d0d7860 | 2012-10-11 16:46:07 +0000 | [diff] [blame] | 954 | unsigned Reg = DefMI->getOperand(DefOp).getReg(); |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 955 | assert(TargetRegisterInfo::isVirtualRegister(Reg)); |
| 956 | const MachineBasicBlock *DefMBB = DefMI->getParent(); |
| 957 | |
| 958 | // Reg is live-in to all blocks in Trace that follow DefMBB. |
| 959 | for (unsigned i = Trace.size(); i; --i) { |
| 960 | const MachineBasicBlock *MBB = Trace[i-1]; |
| 961 | if (MBB == DefMBB) |
| 962 | return; |
| 963 | TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()]; |
| 964 | // Just add the register. The height will be updated later. |
| 965 | TBI.LiveIns.push_back(Reg); |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | /// Compute instruction heights in the trace through MBB. This updates MBB and |
| 970 | /// the blocks below it in the trace. It is assumed that the trace has already |
| 971 | /// been computed. |
| 972 | void MachineTraceMetrics::Ensemble:: |
| 973 | computeInstrHeights(const MachineBasicBlock *MBB) { |
| 974 | // The bottom of the trace may already be computed. |
| 975 | // Find the blocks that need updating. |
| 976 | SmallVector<const MachineBasicBlock*, 8> Stack; |
| 977 | do { |
| 978 | TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()]; |
| 979 | assert(TBI.hasValidHeight() && "Incomplete trace"); |
| 980 | if (TBI.HasValidInstrHeights) |
| 981 | break; |
| 982 | Stack.push_back(MBB); |
| 983 | TBI.LiveIns.clear(); |
| 984 | MBB = TBI.Succ; |
| 985 | } while (MBB); |
| 986 | |
| 987 | // As we move upwards in the trace, keep track of instructions that are |
| 988 | // required by deeper trace instructions. Map MI -> height required so far. |
| 989 | MIHeightMap Heights; |
| 990 | |
| 991 | // For physregs, the def isn't known when we see the use. |
| 992 | // Instead, keep track of the highest use of each regunit. |
| 993 | SparseSet<LiveRegUnit> RegUnits; |
| 994 | RegUnits.setUniverse(MTM.TRI->getNumRegUnits()); |
| 995 | |
| 996 | // If the bottom of the trace was already precomputed, initialize heights |
| 997 | // from its live-in list. |
| 998 | // MBB is the highest precomputed block in the trace. |
| 999 | if (MBB) { |
| 1000 | TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()]; |
Sanjay Patel | 6d4c3e3 | 2015-07-06 16:19:14 +0000 | [diff] [blame] | 1001 | for (LiveInReg &LI : TBI.LiveIns) { |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 1002 | if (TargetRegisterInfo::isVirtualRegister(LI.Reg)) { |
| 1003 | // For virtual registers, the def latency is included. |
| 1004 | unsigned &Height = Heights[MTM.MRI->getVRegDef(LI.Reg)]; |
| 1005 | if (Height < LI.Height) |
| 1006 | Height = LI.Height; |
| 1007 | } else { |
| 1008 | // For register units, the def latency is not included because we don't |
| 1009 | // know the def yet. |
| 1010 | RegUnits[LI.Reg].Cycle = LI.Height; |
| 1011 | } |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | // Go through the trace blocks in bottom-up order. |
| 1016 | SmallVector<DataDep, 8> Deps; |
| 1017 | for (;!Stack.empty(); Stack.pop_back()) { |
| 1018 | MBB = Stack.back(); |
| 1019 | DEBUG(dbgs() << "Heights for BB#" << MBB->getNumber() << ":\n"); |
| 1020 | TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()]; |
| 1021 | TBI.HasValidInstrHeights = true; |
Jakob Stoklund Olesen | 5d30630 | 2012-08-02 18:45:54 +0000 | [diff] [blame] | 1022 | TBI.CriticalPath = 0; |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 1023 | |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 1024 | DEBUG({ |
| 1025 | dbgs() << format("%7u Instructions\n", TBI.InstrHeight); |
| 1026 | ArrayRef<unsigned> PRHeights = getProcResourceHeights(MBB->getNumber()); |
| 1027 | for (unsigned K = 0; K != PRHeights.size(); ++K) |
| 1028 | if (PRHeights[K]) { |
| 1029 | unsigned Factor = MTM.SchedModel.getResourceFactor(K); |
| 1030 | dbgs() << format("%6uc @ ", MTM.getCycles(PRHeights[K])) |
| 1031 | << MTM.SchedModel.getProcResource(K)->Name << " (" |
| 1032 | << PRHeights[K]/Factor << " ops x" << Factor << ")\n"; |
| 1033 | } |
| 1034 | }); |
| 1035 | |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 1036 | // Get dependencies from PHIs in the trace successor. |
Jakob Stoklund Olesen | 0954d41 | 2012-08-10 20:11:38 +0000 | [diff] [blame] | 1037 | const MachineBasicBlock *Succ = TBI.Succ; |
| 1038 | // If MBB is the last block in the trace, and it has a back-edge to the |
| 1039 | // loop header, get loop-carried dependencies from PHIs in the header. For |
| 1040 | // that purpose, pretend that all the loop header PHIs have height 0. |
| 1041 | if (!Succ) |
| 1042 | if (const MachineLoop *Loop = getLoopFor(MBB)) |
| 1043 | if (MBB->isSuccessor(Loop->getHeader())) |
| 1044 | Succ = Loop->getHeader(); |
| 1045 | |
| 1046 | if (Succ) { |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 1047 | for (const auto &PHI : *Succ) { |
| 1048 | if (!PHI.isPHI()) |
| 1049 | break; |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 1050 | Deps.clear(); |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 1051 | getPHIDeps(&PHI, Deps, MBB, MTM.MRI); |
Jakob Stoklund Olesen | 0954d41 | 2012-08-10 20:11:38 +0000 | [diff] [blame] | 1052 | if (!Deps.empty()) { |
| 1053 | // Loop header PHI heights are all 0. |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 1054 | unsigned Height = TBI.Succ ? Cycles.lookup(&PHI).Height : 0; |
| 1055 | DEBUG(dbgs() << "pred\t" << Height << '\t' << PHI); |
| 1056 | if (pushDepHeight(Deps.front(), &PHI, Height, |
Jakob Stoklund Olesen | 8982222 | 2012-10-04 17:30:40 +0000 | [diff] [blame] | 1057 | Heights, MTM.SchedModel, MTM.TII)) |
Jakob Stoklund Olesen | d0d7860 | 2012-10-11 16:46:07 +0000 | [diff] [blame] | 1058 | addLiveIns(Deps.front().DefMI, Deps.front().DefOp, Stack); |
Jakob Stoklund Olesen | 0954d41 | 2012-08-10 20:11:38 +0000 | [diff] [blame] | 1059 | } |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | // Go through the block backwards. |
| 1064 | for (MachineBasicBlock::const_iterator BI = MBB->end(), BB = MBB->begin(); |
| 1065 | BI != BB;) { |
| 1066 | const MachineInstr *MI = --BI; |
| 1067 | |
| 1068 | // Find the MI height as determined by virtual register uses in the |
| 1069 | // trace below. |
| 1070 | unsigned Cycle = 0; |
| 1071 | MIHeightMap::iterator HeightI = Heights.find(MI); |
| 1072 | if (HeightI != Heights.end()) { |
| 1073 | Cycle = HeightI->second; |
| 1074 | // We won't be seeing any more MI uses. |
| 1075 | Heights.erase(HeightI); |
| 1076 | } |
| 1077 | |
| 1078 | // Don't process PHI deps. They depend on the specific predecessor, and |
| 1079 | // we'll get them when visiting the predecessor. |
| 1080 | Deps.clear(); |
| 1081 | bool HasPhysRegs = !MI->isPHI() && getDataDeps(MI, Deps, MTM.MRI); |
| 1082 | |
| 1083 | // There may also be regunit dependencies to include in the height. |
| 1084 | if (HasPhysRegs) |
| 1085 | Cycle = updatePhysDepsUpwards(MI, Cycle, RegUnits, |
Jakob Stoklund Olesen | 8982222 | 2012-10-04 17:30:40 +0000 | [diff] [blame] | 1086 | MTM.SchedModel, MTM.TII, MTM.TRI); |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 1087 | |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 1088 | // Update the required height of any virtual registers read by MI. |
Sanjay Patel | 0ca438c | 2015-06-30 16:30:22 +0000 | [diff] [blame] | 1089 | for (const DataDep &Dep : Deps) |
| 1090 | if (pushDepHeight(Dep, MI, Cycle, Heights, MTM.SchedModel, MTM.TII)) |
| 1091 | addLiveIns(Dep.DefMI, Dep.DefOp, Stack); |
Jakob Stoklund Olesen | 5d30630 | 2012-08-02 18:45:54 +0000 | [diff] [blame] | 1092 | |
| 1093 | InstrCycles &MICycles = Cycles[MI]; |
| 1094 | MICycles.Height = Cycle; |
| 1095 | if (!TBI.HasValidInstrDepths) { |
| 1096 | DEBUG(dbgs() << Cycle << '\t' << *MI); |
| 1097 | continue; |
| 1098 | } |
| 1099 | // Update critical path length. |
| 1100 | TBI.CriticalPath = std::max(TBI.CriticalPath, Cycle + MICycles.Depth); |
| 1101 | DEBUG(dbgs() << TBI.CriticalPath << '\t' << Cycle << '\t' << *MI); |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | // Update virtual live-in heights. They were added by addLiveIns() with a 0 |
| 1105 | // height because the final height isn't known until now. |
| 1106 | DEBUG(dbgs() << "BB#" << MBB->getNumber() << " Live-ins:"); |
Sanjay Patel | 0ca438c | 2015-06-30 16:30:22 +0000 | [diff] [blame] | 1107 | for (LiveInReg &LIR : TBI.LiveIns) { |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 1108 | const MachineInstr *DefMI = MTM.MRI->getVRegDef(LIR.Reg); |
| 1109 | LIR.Height = Heights.lookup(DefMI); |
| 1110 | DEBUG(dbgs() << ' ' << PrintReg(LIR.Reg) << '@' << LIR.Height); |
| 1111 | } |
| 1112 | |
| 1113 | // Transfer the live regunits to the live-in list. |
| 1114 | for (SparseSet<LiveRegUnit>::const_iterator |
| 1115 | RI = RegUnits.begin(), RE = RegUnits.end(); RI != RE; ++RI) { |
| 1116 | TBI.LiveIns.push_back(LiveInReg(RI->RegUnit, RI->Cycle)); |
| 1117 | DEBUG(dbgs() << ' ' << PrintRegUnit(RI->RegUnit, MTM.TRI) |
| 1118 | << '@' << RI->Cycle); |
| 1119 | } |
| 1120 | DEBUG(dbgs() << '\n'); |
Jakob Stoklund Olesen | 5d30630 | 2012-08-02 18:45:54 +0000 | [diff] [blame] | 1121 | |
| 1122 | if (!TBI.HasValidInstrDepths) |
| 1123 | continue; |
| 1124 | // Add live-ins to the critical path length. |
| 1125 | TBI.CriticalPath = std::max(TBI.CriticalPath, |
| 1126 | computeCrossBlockCriticalPath(TBI)); |
| 1127 | DEBUG(dbgs() << "Critical path: " << TBI.CriticalPath << '\n'); |
Jakob Stoklund Olesen | 2db6b65 | 2012-08-01 22:36:00 +0000 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 1131 | MachineTraceMetrics::Trace |
| 1132 | MachineTraceMetrics::Ensemble::getTrace(const MachineBasicBlock *MBB) { |
Sanjay Patel | 82db3b7 | 2015-07-04 15:00:28 +0000 | [diff] [blame] | 1133 | TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()]; |
| 1134 | |
| 1135 | if (!TBI.hasValidDepth() || !TBI.hasValidHeight()) |
| 1136 | computeTrace(MBB); |
| 1137 | if (!TBI.HasValidInstrDepths) |
| 1138 | computeInstrDepths(MBB); |
| 1139 | if (!TBI.HasValidInstrHeights) |
| 1140 | computeInstrHeights(MBB); |
| 1141 | |
| 1142 | return Trace(*this, TBI); |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Jakob Stoklund Olesen | 75d9d51 | 2012-08-07 18:02:19 +0000 | [diff] [blame] | 1145 | unsigned |
| 1146 | MachineTraceMetrics::Trace::getInstrSlack(const MachineInstr *MI) const { |
| 1147 | assert(MI && "Not an instruction."); |
| 1148 | assert(getBlockNum() == unsigned(MI->getParent()->getNumber()) && |
| 1149 | "MI must be in the trace center block"); |
| 1150 | InstrCycles Cyc = getInstrCycles(MI); |
| 1151 | return getCriticalPath() - (Cyc.Depth + Cyc.Height); |
| 1152 | } |
| 1153 | |
Jakob Stoklund Olesen | 3484420 | 2012-08-10 22:27:27 +0000 | [diff] [blame] | 1154 | unsigned |
| 1155 | MachineTraceMetrics::Trace::getPHIDepth(const MachineInstr *PHI) const { |
| 1156 | const MachineBasicBlock *MBB = TE.MTM.MF->getBlockNumbered(getBlockNum()); |
| 1157 | SmallVector<DataDep, 1> Deps; |
| 1158 | getPHIDeps(PHI, Deps, MBB, TE.MTM.MRI); |
| 1159 | assert(Deps.size() == 1 && "PHI doesn't have MBB as a predecessor"); |
| 1160 | DataDep &Dep = Deps.front(); |
| 1161 | unsigned DepCycle = getInstrCycles(Dep.DefMI).Depth; |
| 1162 | // Add latency if DefMI is a real instruction. Transients get latency 0. |
| 1163 | if (!Dep.DefMI->isTransient()) |
Jakob Stoklund Olesen | 8982222 | 2012-10-04 17:30:40 +0000 | [diff] [blame] | 1164 | DepCycle += TE.MTM.SchedModel |
Andrew Trick | de2109e | 2013-06-15 04:49:57 +0000 | [diff] [blame] | 1165 | .computeOperandLatency(Dep.DefMI, Dep.DefOp, PHI, Dep.UseOp); |
Jakob Stoklund Olesen | 3484420 | 2012-08-10 22:27:27 +0000 | [diff] [blame] | 1166 | return DepCycle; |
| 1167 | } |
| 1168 | |
Gerolf Hoflehner | 5e1207e | 2014-08-03 21:35:39 +0000 | [diff] [blame] | 1169 | /// When bottom is set include instructions in current block in estimate. |
Jakob Stoklund Olesen | 75d9d51 | 2012-08-07 18:02:19 +0000 | [diff] [blame] | 1170 | unsigned MachineTraceMetrics::Trace::getResourceDepth(bool Bottom) const { |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 1171 | // Find the limiting processor resource. |
| 1172 | // Numbers have been pre-scaled to be comparable. |
| 1173 | unsigned PRMax = 0; |
| 1174 | ArrayRef<unsigned> PRDepths = TE.getProcResourceDepths(getBlockNum()); |
| 1175 | if (Bottom) { |
| 1176 | ArrayRef<unsigned> PRCycles = TE.MTM.getProcResourceCycles(getBlockNum()); |
| 1177 | for (unsigned K = 0; K != PRDepths.size(); ++K) |
| 1178 | PRMax = std::max(PRMax, PRDepths[K] + PRCycles[K]); |
| 1179 | } else { |
| 1180 | for (unsigned K = 0; K != PRDepths.size(); ++K) |
| 1181 | PRMax = std::max(PRMax, PRDepths[K]); |
| 1182 | } |
| 1183 | // Convert to cycle count. |
| 1184 | PRMax = TE.MTM.getCycles(PRMax); |
| 1185 | |
Gerolf Hoflehner | 5e1207e | 2014-08-03 21:35:39 +0000 | [diff] [blame] | 1186 | /// All instructions before current block |
Jakob Stoklund Olesen | 75d9d51 | 2012-08-07 18:02:19 +0000 | [diff] [blame] | 1187 | unsigned Instrs = TBI.InstrDepth; |
Gerolf Hoflehner | 5e1207e | 2014-08-03 21:35:39 +0000 | [diff] [blame] | 1188 | // plus instructions in current block |
Jakob Stoklund Olesen | 75d9d51 | 2012-08-07 18:02:19 +0000 | [diff] [blame] | 1189 | if (Bottom) |
| 1190 | Instrs += TE.MTM.BlockInfo[getBlockNum()].InstrCount; |
Jakob Stoklund Olesen | 8982222 | 2012-10-04 17:30:40 +0000 | [diff] [blame] | 1191 | if (unsigned IW = TE.MTM.SchedModel.getIssueWidth()) |
| 1192 | Instrs /= IW; |
Jakob Stoklund Olesen | 75d9d51 | 2012-08-07 18:02:19 +0000 | [diff] [blame] | 1193 | // Assume issue width 1 without a schedule model. |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 1194 | return std::max(Instrs, PRMax); |
Jakob Stoklund Olesen | 75d9d51 | 2012-08-07 18:02:19 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
Gerolf Hoflehner | 5e1207e | 2014-08-03 21:35:39 +0000 | [diff] [blame] | 1197 | unsigned MachineTraceMetrics::Trace::getResourceLength( |
| 1198 | ArrayRef<const MachineBasicBlock *> Extrablocks, |
| 1199 | ArrayRef<const MCSchedClassDesc *> ExtraInstrs, |
| 1200 | ArrayRef<const MCSchedClassDesc *> RemoveInstrs) const { |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 1201 | // Add up resources above and below the center block. |
| 1202 | ArrayRef<unsigned> PRDepths = TE.getProcResourceDepths(getBlockNum()); |
| 1203 | ArrayRef<unsigned> PRHeights = TE.getProcResourceHeights(getBlockNum()); |
| 1204 | unsigned PRMax = 0; |
Gerolf Hoflehner | 5e1207e | 2014-08-03 21:35:39 +0000 | [diff] [blame] | 1205 | |
| 1206 | // Capture computing cycles from extra instructions |
| 1207 | auto extraCycles = [this](ArrayRef<const MCSchedClassDesc *> Instrs, |
| 1208 | unsigned ResourceIdx) |
| 1209 | ->unsigned { |
| 1210 | unsigned Cycles = 0; |
Sanjay Patel | 6d4c3e3 | 2015-07-06 16:19:14 +0000 | [diff] [blame] | 1211 | for (const MCSchedClassDesc *SC : Instrs) { |
Gerolf Hoflehner | 5e1207e | 2014-08-03 21:35:39 +0000 | [diff] [blame] | 1212 | if (!SC->isValid()) |
| 1213 | continue; |
| 1214 | for (TargetSchedModel::ProcResIter |
| 1215 | PI = TE.MTM.SchedModel.getWriteProcResBegin(SC), |
| 1216 | PE = TE.MTM.SchedModel.getWriteProcResEnd(SC); |
| 1217 | PI != PE; ++PI) { |
| 1218 | if (PI->ProcResourceIdx != ResourceIdx) |
| 1219 | continue; |
| 1220 | Cycles += |
| 1221 | (PI->Cycles * TE.MTM.SchedModel.getResourceFactor(ResourceIdx)); |
| 1222 | } |
| 1223 | } |
| 1224 | return Cycles; |
| 1225 | }; |
| 1226 | |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 1227 | for (unsigned K = 0; K != PRDepths.size(); ++K) { |
| 1228 | unsigned PRCycles = PRDepths[K] + PRHeights[K]; |
Sanjay Patel | 6d4c3e3 | 2015-07-06 16:19:14 +0000 | [diff] [blame] | 1229 | for (const MachineBasicBlock *MBB : Extrablocks) |
| 1230 | PRCycles += TE.MTM.getProcResourceCycles(MBB->getNumber())[K]; |
Gerolf Hoflehner | 5e1207e | 2014-08-03 21:35:39 +0000 | [diff] [blame] | 1231 | PRCycles += extraCycles(ExtraInstrs, K); |
| 1232 | PRCycles -= extraCycles(RemoveInstrs, K); |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 1233 | PRMax = std::max(PRMax, PRCycles); |
| 1234 | } |
| 1235 | // Convert to cycle count. |
| 1236 | PRMax = TE.MTM.getCycles(PRMax); |
| 1237 | |
Gerolf Hoflehner | 5e1207e | 2014-08-03 21:35:39 +0000 | [diff] [blame] | 1238 | // Instrs: #instructions in current trace outside current block. |
Jakob Stoklund Olesen | 3484420 | 2012-08-10 22:27:27 +0000 | [diff] [blame] | 1239 | unsigned Instrs = TBI.InstrDepth + TBI.InstrHeight; |
Gerolf Hoflehner | 5e1207e | 2014-08-03 21:35:39 +0000 | [diff] [blame] | 1240 | // Add instruction count from the extra blocks. |
Sanjay Patel | 6d4c3e3 | 2015-07-06 16:19:14 +0000 | [diff] [blame] | 1241 | for (const MachineBasicBlock *MBB : Extrablocks) |
| 1242 | Instrs += TE.MTM.getResources(MBB)->InstrCount; |
Gerolf Hoflehner | 5e1207e | 2014-08-03 21:35:39 +0000 | [diff] [blame] | 1243 | Instrs += ExtraInstrs.size(); |
| 1244 | Instrs -= RemoveInstrs.size(); |
Jakob Stoklund Olesen | 8982222 | 2012-10-04 17:30:40 +0000 | [diff] [blame] | 1245 | if (unsigned IW = TE.MTM.SchedModel.getIssueWidth()) |
| 1246 | Instrs /= IW; |
Jakob Stoklund Olesen | 3484420 | 2012-08-10 22:27:27 +0000 | [diff] [blame] | 1247 | // Assume issue width 1 without a schedule model. |
Jakob Stoklund Olesen | 3ca1477 | 2013-04-02 17:49:51 +0000 | [diff] [blame] | 1248 | return std::max(Instrs, PRMax); |
Jakob Stoklund Olesen | 3484420 | 2012-08-10 22:27:27 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Gerolf Hoflehner | 5e1207e | 2014-08-03 21:35:39 +0000 | [diff] [blame] | 1251 | bool MachineTraceMetrics::Trace::isDepInTrace(const MachineInstr *DefMI, |
| 1252 | const MachineInstr *UseMI) const { |
| 1253 | if (DefMI->getParent() == UseMI->getParent()) |
| 1254 | return true; |
| 1255 | |
| 1256 | const TraceBlockInfo &DepTBI = TE.BlockInfo[DefMI->getParent()->getNumber()]; |
| 1257 | const TraceBlockInfo &TBI = TE.BlockInfo[UseMI->getParent()->getNumber()]; |
| 1258 | |
| 1259 | return DepTBI.isUsefulDominator(TBI); |
| 1260 | } |
| 1261 | |
Jakob Stoklund Olesen | 0563369 | 2012-07-27 23:58:38 +0000 | [diff] [blame] | 1262 | void MachineTraceMetrics::Ensemble::print(raw_ostream &OS) const { |
| 1263 | OS << getName() << " ensemble:\n"; |
| 1264 | for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) { |
| 1265 | OS << " BB#" << i << '\t'; |
| 1266 | BlockInfo[i].print(OS); |
| 1267 | OS << '\n'; |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | void MachineTraceMetrics::TraceBlockInfo::print(raw_ostream &OS) const { |
| 1272 | if (hasValidDepth()) { |
| 1273 | OS << "depth=" << InstrDepth; |
| 1274 | if (Pred) |
| 1275 | OS << " pred=BB#" << Pred->getNumber(); |
| 1276 | else |
| 1277 | OS << " pred=null"; |
| 1278 | OS << " head=BB#" << Head; |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 1279 | if (HasValidInstrDepths) |
| 1280 | OS << " +instrs"; |
Jakob Stoklund Olesen | 0563369 | 2012-07-27 23:58:38 +0000 | [diff] [blame] | 1281 | } else |
| 1282 | OS << "depth invalid"; |
| 1283 | OS << ", "; |
| 1284 | if (hasValidHeight()) { |
| 1285 | OS << "height=" << InstrHeight; |
| 1286 | if (Succ) |
| 1287 | OS << " succ=BB#" << Succ->getNumber(); |
| 1288 | else |
| 1289 | OS << " succ=null"; |
| 1290 | OS << " tail=BB#" << Tail; |
Jakob Stoklund Olesen | 059e647 | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 1291 | if (HasValidInstrHeights) |
| 1292 | OS << " +instrs"; |
Jakob Stoklund Olesen | 0563369 | 2012-07-27 23:58:38 +0000 | [diff] [blame] | 1293 | } else |
| 1294 | OS << "height invalid"; |
Jakob Stoklund Olesen | 5d30630 | 2012-08-02 18:45:54 +0000 | [diff] [blame] | 1295 | if (HasValidInstrDepths && HasValidInstrHeights) |
| 1296 | OS << ", crit=" << CriticalPath; |
Jakob Stoklund Olesen | 0563369 | 2012-07-27 23:58:38 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 1299 | void MachineTraceMetrics::Trace::print(raw_ostream &OS) const { |
Jakob Stoklund Olesen | 1152202 | 2012-07-27 23:58:36 +0000 | [diff] [blame] | 1300 | unsigned MBBNum = &TBI - &TE.BlockInfo[0]; |
| 1301 | |
| 1302 | OS << TE.getName() << " trace BB#" << TBI.Head << " --> BB#" << MBBNum |
| 1303 | << " --> BB#" << TBI.Tail << ':'; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 1304 | if (TBI.hasValidHeight() && TBI.hasValidDepth()) |
| 1305 | OS << ' ' << getInstrCount() << " instrs."; |
Jakob Stoklund Olesen | 5d30630 | 2012-08-02 18:45:54 +0000 | [diff] [blame] | 1306 | if (TBI.HasValidInstrDepths && TBI.HasValidInstrHeights) |
| 1307 | OS << ' ' << TBI.CriticalPath << " cycles."; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 1308 | |
| 1309 | const MachineTraceMetrics::TraceBlockInfo *Block = &TBI; |
Jakob Stoklund Olesen | 1152202 | 2012-07-27 23:58:36 +0000 | [diff] [blame] | 1310 | OS << "\nBB#" << MBBNum; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 1311 | while (Block->hasValidDepth() && Block->Pred) { |
| 1312 | unsigned Num = Block->Pred->getNumber(); |
| 1313 | OS << " <- BB#" << Num; |
| 1314 | Block = &TE.BlockInfo[Num]; |
| 1315 | } |
| 1316 | |
| 1317 | Block = &TBI; |
Jakob Stoklund Olesen | 1152202 | 2012-07-27 23:58:36 +0000 | [diff] [blame] | 1318 | OS << "\n "; |
Jakob Stoklund Olesen | f9029fe | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 1319 | while (Block->hasValidHeight() && Block->Succ) { |
| 1320 | unsigned Num = Block->Succ->getNumber(); |
| 1321 | OS << " -> BB#" << Num; |
| 1322 | Block = &TE.BlockInfo[Num]; |
| 1323 | } |
| 1324 | OS << '\n'; |
| 1325 | } |