blob: 6daa778a7a6caee2487ce898a788f1fbb04fb092 [file] [log] [blame]
Corentin Wallez71d147f2015-02-11 11:15:24 -08001//
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"
Olli Etuaho77ba4082016-12-16 12:01:18 +000012#include "compiler/translator/Diagnostics.h"
Corentin Wallez71d147f2015-02-11 11:15:24 -080013
Jamie Madill45bcc782016-11-07 13:58:48 -050014namespace sh
15{
16
Corentin Wallez71d147f2015-02-11 11:15:24 -080017// The CallDAGCreator does all the processing required to create the CallDAG
18// structure so that the latter contains only the necessary variables.
19class CallDAG::CallDAGCreator : public TIntermTraverser
20{
21 public:
Olli Etuaho77ba4082016-12-16 12:01:18 +000022 CallDAGCreator(TDiagnostics *diagnostics)
Corentin Wallez71d147f2015-02-11 11:15:24 -080023 : TIntermTraverser(true, false, true),
Olli Etuaho77ba4082016-12-16 12:01:18 +000024 mDiagnostics(diagnostics),
Corentin Wallez71d147f2015-02-11 11:15:24 -080025 mCurrentFunction(nullptr),
Corentin Wallezbc99bb62015-05-14 17:42:20 -040026 mCurrentIndex(0)
Corentin Wallez71d147f2015-02-11 11:15:24 -080027 {
28 }
29
30 InitResult assignIndices()
31 {
32 int skipped = 0;
33 for (auto &it : mFunctions)
34 {
35 // Skip unimplemented functions
36 if (it.second.node)
37 {
38 InitResult result = assignIndicesInternal(&it.second);
39 if (result != INITDAG_SUCCESS)
40 {
41 return result;
42 }
43 }
44 else
45 {
46 skipped++;
47 }
48 }
Corentin Walleza59fcdf2016-09-14 10:52:14 -040049
Corentin Wallez71d147f2015-02-11 11:15:24 -080050 ASSERT(mFunctions.size() == mCurrentIndex + skipped);
51 return INITDAG_SUCCESS;
52 }
53
54 void fillDataStructures(std::vector<Record> *records, std::map<int, int> *idToIndex)
55 {
56 ASSERT(records->empty());
57 ASSERT(idToIndex->empty());
58
59 records->resize(mCurrentIndex);
60
61 for (auto &it : mFunctions)
62 {
63 CreatorFunctionData &data = it.second;
64 // Skip unimplemented functions
65 if (!data.node)
66 {
67 continue;
68 }
69 ASSERT(data.index < records->size());
70 Record &record = (*records)[data.index];
71
72 record.name = data.name.data();
73 record.node = data.node;
74
75 record.callees.reserve(data.callees.size());
76 for (auto &callee : data.callees)
77 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -070078 record.callees.push_back(static_cast<int>(callee->index));
Corentin Wallez71d147f2015-02-11 11:15:24 -080079 }
80
Olli Etuahobd674552016-10-06 13:28:42 +010081 (*idToIndex)[data.node->getFunctionSymbolInfo()->getId()] =
82 static_cast<int>(data.index);
Corentin Wallez71d147f2015-02-11 11:15:24 -080083 }
84 }
85
86 private:
Corentin Wallez71d147f2015-02-11 11:15:24 -080087 struct CreatorFunctionData
88 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -050089 CreatorFunctionData() : node(nullptr), index(0), indexAssigned(false), visiting(false) {}
Corentin Wallez71d147f2015-02-11 11:15:24 -080090
Jamie Madilld7b1ab52016-12-12 14:42:19 -050091 std::set<CreatorFunctionData *> callees;
Olli Etuaho336b1472016-10-05 16:37:55 +010092 TIntermFunctionDefinition *node;
Corentin Wallez71d147f2015-02-11 11:15:24 -080093 TString name;
94 size_t index;
95 bool indexAssigned;
96 bool visiting;
97 };
98
Olli Etuaho336b1472016-10-05 16:37:55 +010099 bool visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node) override
100 {
101 // Create the record if need be and remember the node.
102 if (visit == PreVisit)
103 {
104 auto it = mFunctions.find(node->getFunctionSymbolInfo()->getName());
105
106 if (it == mFunctions.end())
107 {
108 mCurrentFunction = &mFunctions[node->getFunctionSymbolInfo()->getName()];
109 }
110 else
111 {
112 mCurrentFunction = &it->second;
113 }
114
115 mCurrentFunction->node = node;
116 mCurrentFunction->name = node->getFunctionSymbolInfo()->getName();
117 }
118 else if (visit == PostVisit)
119 {
120 mCurrentFunction = nullptr;
121 }
122 return true;
123 }
124
Olli Etuaho16c745a2017-01-16 17:02:27 +0000125 bool visitFunctionPrototype(Visit visit, TIntermFunctionPrototype *node) override
126 {
127 ASSERT(visit == PreVisit);
128 // Function declaration, create an empty record.
129 auto &record = mFunctions[node->getFunctionSymbolInfo()->getName()];
130 record.name = node->getFunctionSymbolInfo()->getName();
131
132 // No need to traverse the parameters.
133 return false;
134 }
135
Corentin Wallez71d147f2015-02-11 11:15:24 -0800136 // Aggregates the AST node for each function as well as the name of the functions called by it
137 bool visitAggregate(Visit visit, TIntermAggregate *node) override
138 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800139 if (visit == PreVisit && node->getOp() == EOpCallFunctionInAST)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800140 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800141 // Function call, add the callees
142 auto it = mFunctions.find(node->getFunctionSymbolInfo()->getName());
143 ASSERT(it != mFunctions.end());
Corentin Wallez71d147f2015-02-11 11:15:24 -0800144
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800145 // We might be in a top-level function call to set a global variable
146 if (mCurrentFunction)
147 {
148 mCurrentFunction->callees.insert(&it->second);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800149 }
Corentin Wallez71d147f2015-02-11 11:15:24 -0800150 }
151 return true;
152 }
153
154 // Recursively assigns indices to a sub DAG
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400155 InitResult assignIndicesInternal(CreatorFunctionData *root)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800156 {
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400157 // Iterative implementation of the index assignment algorithm. A recursive version
158 // would be prettier but since the CallDAG creation runs before the limiting of the
159 // call depth, we might get stack overflows (computation of the call depth uses the
160 // CallDAG).
Corentin Wallez71d147f2015-02-11 11:15:24 -0800161
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400162 ASSERT(root);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800163
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400164 if (root->indexAssigned)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800165 {
166 return INITDAG_SUCCESS;
167 }
168
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400169 // If we didn't have to detect recursion, functionsToProcess could be a simple queue
170 // in which we add the function being processed's callees. However in order to detect
171 // recursion we need to know which functions we are currently visiting. For that reason
172 // functionsToProcess will look like a concatenation of segments of the form
173 // [F visiting = true, subset of F callees with visiting = false] and the following
174 // segment (if any) will be start with a callee of F.
175 // This way we can remember when we started visiting a function, to put visiting back
176 // to false.
177 TVector<CreatorFunctionData *> functionsToProcess;
178 functionsToProcess.push_back(root);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800179
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400180 InitResult result = INITDAG_SUCCESS;
181
Olli Etuaho77ba4082016-12-16 12:01:18 +0000182 std::stringstream errorStream;
183
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400184 while (!functionsToProcess.empty())
Corentin Wallez71d147f2015-02-11 11:15:24 -0800185 {
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400186 CreatorFunctionData *function = functionsToProcess.back();
187
188 if (function->visiting)
189 {
190 function->visiting = false;
191 function->index = mCurrentIndex++;
192 function->indexAssigned = true;
193
194 functionsToProcess.pop_back();
195 continue;
196 }
197
198 if (!function->node)
199 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000200 errorStream << "Undefined function '" << function->name
201 << ")' used in the following call chain:";
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400202 result = INITDAG_UNDEFINED;
203 break;
204 }
205
206 if (function->indexAssigned)
207 {
208 functionsToProcess.pop_back();
209 continue;
210 }
211
212 function->visiting = true;
213
214 for (auto callee : function->callees)
215 {
216 functionsToProcess.push_back(callee);
217
218 // Check if the callee is already being visited after pushing it so that it appears
219 // in the chain printed in the info log.
220 if (callee->visiting)
221 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000222 errorStream << "Recursive function call in the following call chain:";
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400223 result = INITDAG_RECURSION;
224 break;
225 }
226 }
227
Corentin Wallez91dec842015-12-08 15:18:08 -0500228 if (result != INITDAG_SUCCESS)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800229 {
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400230 break;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800231 }
232 }
233
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400234 // The call chain is made of the function we were visiting when the error was detected.
235 if (result != INITDAG_SUCCESS)
236 {
237 bool first = true;
238 for (auto function : functionsToProcess)
239 {
240 if (function->visiting)
241 {
242 if (!first)
243 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000244 errorStream << " -> ";
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400245 }
Olli Etuaho77ba4082016-12-16 12:01:18 +0000246 errorStream << function->name << ")";
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400247 first = false;
248 }
249 }
Olli Etuaho77ba4082016-12-16 12:01:18 +0000250 if (mDiagnostics)
251 {
252 std::string errorStr = errorStream.str();
253 mDiagnostics->globalError(errorStr.c_str());
254 }
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400255 }
Corentin Wallez71d147f2015-02-11 11:15:24 -0800256
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400257 return result;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800258 }
259
Olli Etuaho77ba4082016-12-16 12:01:18 +0000260 TDiagnostics *mDiagnostics;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800261
262 std::map<TString, CreatorFunctionData> mFunctions;
263 CreatorFunctionData *mCurrentFunction;
264 size_t mCurrentIndex;
265};
266
267// CallDAG
268
269CallDAG::CallDAG()
270{
271}
272
273CallDAG::~CallDAG()
274{
275}
276
277const size_t CallDAG::InvalidIndex = std::numeric_limits<size_t>::max();
278
Olli Etuahobd674552016-10-06 13:28:42 +0100279size_t CallDAG::findIndex(const TFunctionSymbolInfo *functionInfo) const
Corentin Wallez71d147f2015-02-11 11:15:24 -0800280{
Olli Etuahobd674552016-10-06 13:28:42 +0100281 auto it = mFunctionIdToIndex.find(functionInfo->getId());
Corentin Wallez71d147f2015-02-11 11:15:24 -0800282
283 if (it == mFunctionIdToIndex.end())
284 {
285 return InvalidIndex;
286 }
287 else
288 {
289 return it->second;
290 }
291}
292
293const CallDAG::Record &CallDAG::getRecordFromIndex(size_t index) const
294{
295 ASSERT(index != InvalidIndex && index < mRecords.size());
296 return mRecords[index];
297}
298
299const CallDAG::Record &CallDAG::getRecord(const TIntermAggregate *function) const
300{
Olli Etuahobd674552016-10-06 13:28:42 +0100301 size_t index = findIndex(function->getFunctionSymbolInfo());
Corentin Wallez71d147f2015-02-11 11:15:24 -0800302 ASSERT(index != InvalidIndex && index < mRecords.size());
303 return mRecords[index];
304}
305
306size_t CallDAG::size() const
307{
308 return mRecords.size();
309}
310
311void CallDAG::clear()
312{
313 mRecords.clear();
314 mFunctionIdToIndex.clear();
315}
316
Olli Etuaho77ba4082016-12-16 12:01:18 +0000317CallDAG::InitResult CallDAG::init(TIntermNode *root, TDiagnostics *diagnostics)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800318{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000319 CallDAGCreator creator(diagnostics);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800320
321 // Creates the mapping of functions to callees
322 root->traverse(&creator);
323
324 // Does the topological sort and detects recursions
325 InitResult result = creator.assignIndices();
326 if (result != INITDAG_SUCCESS)
327 {
328 return result;
329 }
330
331 creator.fillDataStructures(&mRecords, &mFunctionIdToIndex);
332 return INITDAG_SUCCESS;
333}
Jamie Madill45bcc782016-11-07 13:58:48 -0500334
335} // namespace sh