blob: cb9d9cf8a43478cca0e3540ba7e7be4a3d31433c [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"
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.
16class CallDAG::CallDAGCreator : public TIntermTraverser
17{
18 public:
19 CallDAGCreator(TInfoSinkBase *info)
20 : TIntermTraverser(true, false, true),
Corentin Wallezbc99bb62015-05-14 17:42:20 -040021 mCreationInfo(info),
Corentin Wallez71d147f2015-02-11 11:15:24 -080022 mCurrentFunction(nullptr),
Corentin Wallezbc99bb62015-05-14 17:42:20 -040023 mCurrentIndex(0)
Corentin Wallez71d147f2015-02-11 11:15:24 -080024 {
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 Wallez91dec842015-12-08 15:18:08 -050038 *mCreationInfo << "\n";
Corentin Wallez71d147f2015-02-11 11:15:24 -080039 return result;
40 }
41 }
42 else
43 {
44 skipped++;
45 }
46 }
Corentin Walleza59fcdf2016-09-14 10:52:14 -040047
Corentin Wallez71d147f2015-02-11 11:15:24 -080048 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 Partin4d61f7e2015-08-12 10:56:50 -070076 record.callees.push_back(static_cast<int>(callee->index));
Corentin Wallez71d147f2015-02-11 11:15:24 -080077 }
78
Olli Etuahobd674552016-10-06 13:28:42 +010079 (*idToIndex)[data.node->getFunctionSymbolInfo()->getId()] =
80 static_cast<int>(data.index);
Corentin Wallez71d147f2015-02-11 11:15:24 -080081 }
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 Etuahobd674552016-10-06 13:28:42 +0100113 auto &record = mFunctions[node->getFunctionSymbolInfo()->getName()];
114 record.name = node->getFunctionSymbolInfo()->getName();
Corentin Wallez71d147f2015-02-11 11:15:24 -0800115 }
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 Etuahobd674552016-10-06 13:28:42 +0100122 auto it = mFunctions.find(node->getFunctionSymbolInfo()->getName());
Corentin Wallez71d147f2015-02-11 11:15:24 -0800123
124 if (it == mFunctions.end())
125 {
Olli Etuahobd674552016-10-06 13:28:42 +0100126 mCurrentFunction = &mFunctions[node->getFunctionSymbolInfo()->getName()];
Corentin Wallez71d147f2015-02-11 11:15:24 -0800127 }
128 else
129 {
130 mCurrentFunction = &it->second;
131 }
132
133 mCurrentFunction->node = node;
Olli Etuahobd674552016-10-06 13:28:42 +0100134 mCurrentFunction->name = node->getFunctionSymbolInfo()->getName();
Corentin Wallez71d147f2015-02-11 11:15:24 -0800135 }
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 Etuahobd674552016-10-06 13:28:42 +0100150 auto it = mFunctions.find(node->getFunctionSymbolInfo()->getName());
Corentin Wallez71d147f2015-02-11 11:15:24 -0800151 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 Walleza59fcdf2016-09-14 10:52:14 -0400169 InitResult assignIndicesInternal(CreatorFunctionData *root)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800170 {
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400171 // 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 Wallez71d147f2015-02-11 11:15:24 -0800175
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400176 ASSERT(root);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800177
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400178 if (root->indexAssigned)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800179 {
180 return INITDAG_SUCCESS;
181 }
182
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400183 // 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 Wallez71d147f2015-02-11 11:15:24 -0800193
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400194 InitResult result = INITDAG_SUCCESS;
195
196 while (!functionsToProcess.empty())
Corentin Wallez71d147f2015-02-11 11:15:24 -0800197 {
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400198 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 Wallez91dec842015-12-08 15:18:08 -0500240 if (result != INITDAG_SUCCESS)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800241 {
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400242 break;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800243 }
244 }
245
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400246 // 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 Wallez71d147f2015-02-11 11:15:24 -0800263
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400264 return result;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800265 }
266
267 TInfoSinkBase *mCreationInfo;
268
269 std::map<TString, CreatorFunctionData> mFunctions;
270 CreatorFunctionData *mCurrentFunction;
271 size_t mCurrentIndex;
272};
273
274// CallDAG
275
276CallDAG::CallDAG()
277{
278}
279
280CallDAG::~CallDAG()
281{
282}
283
284const size_t CallDAG::InvalidIndex = std::numeric_limits<size_t>::max();
285
Olli Etuahobd674552016-10-06 13:28:42 +0100286size_t CallDAG::findIndex(const TFunctionSymbolInfo *functionInfo) const
Corentin Wallez71d147f2015-02-11 11:15:24 -0800287{
Olli Etuahobd674552016-10-06 13:28:42 +0100288 auto it = mFunctionIdToIndex.find(functionInfo->getId());
Corentin Wallez71d147f2015-02-11 11:15:24 -0800289
290 if (it == mFunctionIdToIndex.end())
291 {
292 return InvalidIndex;
293 }
294 else
295 {
296 return it->second;
297 }
298}
299
300const CallDAG::Record &CallDAG::getRecordFromIndex(size_t index) const
301{
302 ASSERT(index != InvalidIndex && index < mRecords.size());
303 return mRecords[index];
304}
305
306const CallDAG::Record &CallDAG::getRecord(const TIntermAggregate *function) const
307{
Olli Etuahobd674552016-10-06 13:28:42 +0100308 size_t index = findIndex(function->getFunctionSymbolInfo());
Corentin Wallez71d147f2015-02-11 11:15:24 -0800309 ASSERT(index != InvalidIndex && index < mRecords.size());
310 return mRecords[index];
311}
312
313size_t CallDAG::size() const
314{
315 return mRecords.size();
316}
317
318void CallDAG::clear()
319{
320 mRecords.clear();
321 mFunctionIdToIndex.clear();
322}
323
324CallDAG::InitResult CallDAG::init(TIntermNode *root, TInfoSinkBase *info)
325{
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400326 ASSERT(info);
327
Corentin Wallez71d147f2015-02-11 11:15:24 -0800328 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}