Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2015 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // CallDAG.h: Implements a call graph DAG of functions to be re-used accross |
| 8 | // analyses, allows to efficiently traverse the functions in topological |
| 9 | // order. |
| 10 | |
| 11 | #include "compiler/translator/CallDAG.h" |
| 12 | #include "compiler/translator/InfoSink.h" |
| 13 | |
| 14 | // The CallDAGCreator does all the processing required to create the CallDAG |
| 15 | // structure so that the latter contains only the necessary variables. |
| 16 | class CallDAG::CallDAGCreator : public TIntermTraverser |
| 17 | { |
| 18 | public: |
| 19 | CallDAGCreator(TInfoSinkBase *info) |
| 20 | : TIntermTraverser(true, false, true), |
Corentin Wallez | bc99bb6 | 2015-05-14 17:42:20 -0400 | [diff] [blame] | 21 | mCreationInfo(info), |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 22 | mCurrentFunction(nullptr), |
Corentin Wallez | bc99bb6 | 2015-05-14 17:42:20 -0400 | [diff] [blame] | 23 | mCurrentIndex(0) |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 24 | { |
| 25 | } |
| 26 | |
| 27 | InitResult assignIndices() |
| 28 | { |
| 29 | int skipped = 0; |
| 30 | for (auto &it : mFunctions) |
| 31 | { |
| 32 | // Skip unimplemented functions |
| 33 | if (it.second.node) |
| 34 | { |
| 35 | InitResult result = assignIndicesInternal(&it.second); |
| 36 | if (result != INITDAG_SUCCESS) |
| 37 | { |
Corentin Wallez | 91dec84 | 2015-12-08 15:18:08 -0500 | [diff] [blame] | 38 | *mCreationInfo << "\n"; |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 39 | return result; |
| 40 | } |
| 41 | } |
| 42 | else |
| 43 | { |
| 44 | skipped++; |
| 45 | } |
| 46 | } |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 47 | |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 48 | ASSERT(mFunctions.size() == mCurrentIndex + skipped); |
| 49 | return INITDAG_SUCCESS; |
| 50 | } |
| 51 | |
| 52 | void fillDataStructures(std::vector<Record> *records, std::map<int, int> *idToIndex) |
| 53 | { |
| 54 | ASSERT(records->empty()); |
| 55 | ASSERT(idToIndex->empty()); |
| 56 | |
| 57 | records->resize(mCurrentIndex); |
| 58 | |
| 59 | for (auto &it : mFunctions) |
| 60 | { |
| 61 | CreatorFunctionData &data = it.second; |
| 62 | // Skip unimplemented functions |
| 63 | if (!data.node) |
| 64 | { |
| 65 | continue; |
| 66 | } |
| 67 | ASSERT(data.index < records->size()); |
| 68 | Record &record = (*records)[data.index]; |
| 69 | |
| 70 | record.name = data.name.data(); |
| 71 | record.node = data.node; |
| 72 | |
| 73 | record.callees.reserve(data.callees.size()); |
| 74 | for (auto &callee : data.callees) |
| 75 | { |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 76 | record.callees.push_back(static_cast<int>(callee->index)); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Olli Etuaho | bd67455 | 2016-10-06 13:28:42 +0100 | [diff] [blame^] | 79 | (*idToIndex)[data.node->getFunctionSymbolInfo()->getId()] = |
| 80 | static_cast<int>(data.index); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | |
| 86 | struct CreatorFunctionData |
| 87 | { |
| 88 | CreatorFunctionData() |
| 89 | : node(nullptr), |
| 90 | index(0), |
| 91 | indexAssigned(false), |
| 92 | visiting(false) |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | std::set<CreatorFunctionData*> callees; |
| 97 | TIntermAggregate *node; |
| 98 | TString name; |
| 99 | size_t index; |
| 100 | bool indexAssigned; |
| 101 | bool visiting; |
| 102 | }; |
| 103 | |
| 104 | // Aggregates the AST node for each function as well as the name of the functions called by it |
| 105 | bool visitAggregate(Visit visit, TIntermAggregate *node) override |
| 106 | { |
| 107 | switch (node->getOp()) |
| 108 | { |
| 109 | case EOpPrototype: |
| 110 | if (visit == PreVisit) |
| 111 | { |
| 112 | // Function declaration, create an empty record. |
Olli Etuaho | bd67455 | 2016-10-06 13:28:42 +0100 | [diff] [blame^] | 113 | auto &record = mFunctions[node->getFunctionSymbolInfo()->getName()]; |
| 114 | record.name = node->getFunctionSymbolInfo()->getName(); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 115 | } |
| 116 | break; |
| 117 | case EOpFunction: |
| 118 | { |
| 119 | // Function definition, create the record if need be and remember the node. |
| 120 | if (visit == PreVisit) |
| 121 | { |
Olli Etuaho | bd67455 | 2016-10-06 13:28:42 +0100 | [diff] [blame^] | 122 | auto it = mFunctions.find(node->getFunctionSymbolInfo()->getName()); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 123 | |
| 124 | if (it == mFunctions.end()) |
| 125 | { |
Olli Etuaho | bd67455 | 2016-10-06 13:28:42 +0100 | [diff] [blame^] | 126 | mCurrentFunction = &mFunctions[node->getFunctionSymbolInfo()->getName()]; |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 127 | } |
| 128 | else |
| 129 | { |
| 130 | mCurrentFunction = &it->second; |
| 131 | } |
| 132 | |
| 133 | mCurrentFunction->node = node; |
Olli Etuaho | bd67455 | 2016-10-06 13:28:42 +0100 | [diff] [blame^] | 134 | mCurrentFunction->name = node->getFunctionSymbolInfo()->getName(); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 135 | } |
| 136 | else if (visit == PostVisit) |
| 137 | { |
| 138 | mCurrentFunction = nullptr; |
| 139 | } |
| 140 | break; |
| 141 | } |
| 142 | case EOpFunctionCall: |
| 143 | { |
| 144 | // Function call, add the callees |
| 145 | if (visit == PreVisit) |
| 146 | { |
| 147 | // Do not handle calls to builtin functions |
| 148 | if (node->isUserDefined()) |
| 149 | { |
Olli Etuaho | bd67455 | 2016-10-06 13:28:42 +0100 | [diff] [blame^] | 150 | auto it = mFunctions.find(node->getFunctionSymbolInfo()->getName()); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 151 | ASSERT(it != mFunctions.end()); |
| 152 | |
| 153 | // We might be in a top-level function call to set a global variable |
| 154 | if (mCurrentFunction) |
| 155 | { |
| 156 | mCurrentFunction->callees.insert(&it->second); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | break; |
| 161 | } |
| 162 | default: |
| 163 | break; |
| 164 | } |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | // Recursively assigns indices to a sub DAG |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 169 | InitResult assignIndicesInternal(CreatorFunctionData *root) |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 170 | { |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 171 | // Iterative implementation of the index assignment algorithm. A recursive version |
| 172 | // would be prettier but since the CallDAG creation runs before the limiting of the |
| 173 | // call depth, we might get stack overflows (computation of the call depth uses the |
| 174 | // CallDAG). |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 175 | |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 176 | ASSERT(root); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 177 | |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 178 | if (root->indexAssigned) |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 179 | { |
| 180 | return INITDAG_SUCCESS; |
| 181 | } |
| 182 | |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 183 | // If we didn't have to detect recursion, functionsToProcess could be a simple queue |
| 184 | // in which we add the function being processed's callees. However in order to detect |
| 185 | // recursion we need to know which functions we are currently visiting. For that reason |
| 186 | // functionsToProcess will look like a concatenation of segments of the form |
| 187 | // [F visiting = true, subset of F callees with visiting = false] and the following |
| 188 | // segment (if any) will be start with a callee of F. |
| 189 | // This way we can remember when we started visiting a function, to put visiting back |
| 190 | // to false. |
| 191 | TVector<CreatorFunctionData *> functionsToProcess; |
| 192 | functionsToProcess.push_back(root); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 193 | |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 194 | InitResult result = INITDAG_SUCCESS; |
| 195 | |
| 196 | while (!functionsToProcess.empty()) |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 197 | { |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 198 | CreatorFunctionData *function = functionsToProcess.back(); |
| 199 | |
| 200 | if (function->visiting) |
| 201 | { |
| 202 | function->visiting = false; |
| 203 | function->index = mCurrentIndex++; |
| 204 | function->indexAssigned = true; |
| 205 | |
| 206 | functionsToProcess.pop_back(); |
| 207 | continue; |
| 208 | } |
| 209 | |
| 210 | if (!function->node) |
| 211 | { |
| 212 | *mCreationInfo << "Undefined function '" << function->name |
| 213 | << ")' used in the following call chain:"; |
| 214 | result = INITDAG_UNDEFINED; |
| 215 | break; |
| 216 | } |
| 217 | |
| 218 | if (function->indexAssigned) |
| 219 | { |
| 220 | functionsToProcess.pop_back(); |
| 221 | continue; |
| 222 | } |
| 223 | |
| 224 | function->visiting = true; |
| 225 | |
| 226 | for (auto callee : function->callees) |
| 227 | { |
| 228 | functionsToProcess.push_back(callee); |
| 229 | |
| 230 | // Check if the callee is already being visited after pushing it so that it appears |
| 231 | // in the chain printed in the info log. |
| 232 | if (callee->visiting) |
| 233 | { |
| 234 | *mCreationInfo << "Recursive function call in the following call chain:"; |
| 235 | result = INITDAG_RECURSION; |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | |
Corentin Wallez | 91dec84 | 2015-12-08 15:18:08 -0500 | [diff] [blame] | 240 | if (result != INITDAG_SUCCESS) |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 241 | { |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 242 | break; |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 246 | // The call chain is made of the function we were visiting when the error was detected. |
| 247 | if (result != INITDAG_SUCCESS) |
| 248 | { |
| 249 | bool first = true; |
| 250 | for (auto function : functionsToProcess) |
| 251 | { |
| 252 | if (function->visiting) |
| 253 | { |
| 254 | if (!first) |
| 255 | { |
| 256 | *mCreationInfo << " -> "; |
| 257 | } |
| 258 | *mCreationInfo << function->name << ")"; |
| 259 | first = false; |
| 260 | } |
| 261 | } |
| 262 | } |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 263 | |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 264 | return result; |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | TInfoSinkBase *mCreationInfo; |
| 268 | |
| 269 | std::map<TString, CreatorFunctionData> mFunctions; |
| 270 | CreatorFunctionData *mCurrentFunction; |
| 271 | size_t mCurrentIndex; |
| 272 | }; |
| 273 | |
| 274 | // CallDAG |
| 275 | |
| 276 | CallDAG::CallDAG() |
| 277 | { |
| 278 | } |
| 279 | |
| 280 | CallDAG::~CallDAG() |
| 281 | { |
| 282 | } |
| 283 | |
| 284 | const size_t CallDAG::InvalidIndex = std::numeric_limits<size_t>::max(); |
| 285 | |
Olli Etuaho | bd67455 | 2016-10-06 13:28:42 +0100 | [diff] [blame^] | 286 | size_t CallDAG::findIndex(const TFunctionSymbolInfo *functionInfo) const |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 287 | { |
Olli Etuaho | bd67455 | 2016-10-06 13:28:42 +0100 | [diff] [blame^] | 288 | auto it = mFunctionIdToIndex.find(functionInfo->getId()); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 289 | |
| 290 | if (it == mFunctionIdToIndex.end()) |
| 291 | { |
| 292 | return InvalidIndex; |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | return it->second; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | const CallDAG::Record &CallDAG::getRecordFromIndex(size_t index) const |
| 301 | { |
| 302 | ASSERT(index != InvalidIndex && index < mRecords.size()); |
| 303 | return mRecords[index]; |
| 304 | } |
| 305 | |
| 306 | const CallDAG::Record &CallDAG::getRecord(const TIntermAggregate *function) const |
| 307 | { |
Olli Etuaho | bd67455 | 2016-10-06 13:28:42 +0100 | [diff] [blame^] | 308 | size_t index = findIndex(function->getFunctionSymbolInfo()); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 309 | ASSERT(index != InvalidIndex && index < mRecords.size()); |
| 310 | return mRecords[index]; |
| 311 | } |
| 312 | |
| 313 | size_t CallDAG::size() const |
| 314 | { |
| 315 | return mRecords.size(); |
| 316 | } |
| 317 | |
| 318 | void CallDAG::clear() |
| 319 | { |
| 320 | mRecords.clear(); |
| 321 | mFunctionIdToIndex.clear(); |
| 322 | } |
| 323 | |
| 324 | CallDAG::InitResult CallDAG::init(TIntermNode *root, TInfoSinkBase *info) |
| 325 | { |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 326 | ASSERT(info); |
| 327 | |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 328 | CallDAGCreator creator(info); |
| 329 | |
| 330 | // Creates the mapping of functions to callees |
| 331 | root->traverse(&creator); |
| 332 | |
| 333 | // Does the topological sort and detects recursions |
| 334 | InitResult result = creator.assignIndices(); |
| 335 | if (result != INITDAG_SUCCESS) |
| 336 | { |
| 337 | return result; |
| 338 | } |
| 339 | |
| 340 | creator.fillDataStructures(&mRecords, &mFunctionIdToIndex); |
| 341 | return INITDAG_SUCCESS; |
| 342 | } |