blob: 45273a0c249e7511c8f03ae726eb311d6302cfd5 [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
Corentin Wallez71d147f2015-02-11 11:15:24 -0800125 // Aggregates the AST node for each function as well as the name of the functions called by it
126 bool visitAggregate(Visit visit, TIntermAggregate *node) override
127 {
128 switch (node->getOp())
129 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500130 case EOpPrototype:
131 if (visit == PreVisit)
132 {
133 // Function declaration, create an empty record.
134 auto &record = mFunctions[node->getFunctionSymbolInfo()->getName()];
135 record.name = node->getFunctionSymbolInfo()->getName();
136 }
137 break;
138 case EOpFunctionCall:
Corentin Wallez71d147f2015-02-11 11:15:24 -0800139 {
140 // Function call, add the callees
141 if (visit == PreVisit)
142 {
143 // Do not handle calls to builtin functions
144 if (node->isUserDefined())
145 {
Olli Etuahobd674552016-10-06 13:28:42 +0100146 auto it = mFunctions.find(node->getFunctionSymbolInfo()->getName());
Corentin Wallez71d147f2015-02-11 11:15:24 -0800147 ASSERT(it != mFunctions.end());
148
149 // We might be in a top-level function call to set a global variable
150 if (mCurrentFunction)
151 {
152 mCurrentFunction->callees.insert(&it->second);
153 }
154 }
155 }
156 break;
157 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500158 default:
159 break;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800160 }
161 return true;
162 }
163
164 // Recursively assigns indices to a sub DAG
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400165 InitResult assignIndicesInternal(CreatorFunctionData *root)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800166 {
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400167 // Iterative implementation of the index assignment algorithm. A recursive version
168 // would be prettier but since the CallDAG creation runs before the limiting of the
169 // call depth, we might get stack overflows (computation of the call depth uses the
170 // CallDAG).
Corentin Wallez71d147f2015-02-11 11:15:24 -0800171
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400172 ASSERT(root);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800173
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400174 if (root->indexAssigned)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800175 {
176 return INITDAG_SUCCESS;
177 }
178
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400179 // If we didn't have to detect recursion, functionsToProcess could be a simple queue
180 // in which we add the function being processed's callees. However in order to detect
181 // recursion we need to know which functions we are currently visiting. For that reason
182 // functionsToProcess will look like a concatenation of segments of the form
183 // [F visiting = true, subset of F callees with visiting = false] and the following
184 // segment (if any) will be start with a callee of F.
185 // This way we can remember when we started visiting a function, to put visiting back
186 // to false.
187 TVector<CreatorFunctionData *> functionsToProcess;
188 functionsToProcess.push_back(root);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800189
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400190 InitResult result = INITDAG_SUCCESS;
191
Olli Etuaho77ba4082016-12-16 12:01:18 +0000192 std::stringstream errorStream;
193
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400194 while (!functionsToProcess.empty())
Corentin Wallez71d147f2015-02-11 11:15:24 -0800195 {
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400196 CreatorFunctionData *function = functionsToProcess.back();
197
198 if (function->visiting)
199 {
200 function->visiting = false;
201 function->index = mCurrentIndex++;
202 function->indexAssigned = true;
203
204 functionsToProcess.pop_back();
205 continue;
206 }
207
208 if (!function->node)
209 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000210 errorStream << "Undefined function '" << function->name
211 << ")' used in the following call chain:";
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400212 result = INITDAG_UNDEFINED;
213 break;
214 }
215
216 if (function->indexAssigned)
217 {
218 functionsToProcess.pop_back();
219 continue;
220 }
221
222 function->visiting = true;
223
224 for (auto callee : function->callees)
225 {
226 functionsToProcess.push_back(callee);
227
228 // Check if the callee is already being visited after pushing it so that it appears
229 // in the chain printed in the info log.
230 if (callee->visiting)
231 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000232 errorStream << "Recursive function call in the following call chain:";
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400233 result = INITDAG_RECURSION;
234 break;
235 }
236 }
237
Corentin Wallez91dec842015-12-08 15:18:08 -0500238 if (result != INITDAG_SUCCESS)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800239 {
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400240 break;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800241 }
242 }
243
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400244 // The call chain is made of the function we were visiting when the error was detected.
245 if (result != INITDAG_SUCCESS)
246 {
247 bool first = true;
248 for (auto function : functionsToProcess)
249 {
250 if (function->visiting)
251 {
252 if (!first)
253 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000254 errorStream << " -> ";
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400255 }
Olli Etuaho77ba4082016-12-16 12:01:18 +0000256 errorStream << function->name << ")";
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400257 first = false;
258 }
259 }
Olli Etuaho77ba4082016-12-16 12:01:18 +0000260 if (mDiagnostics)
261 {
262 std::string errorStr = errorStream.str();
263 mDiagnostics->globalError(errorStr.c_str());
264 }
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400265 }
Corentin Wallez71d147f2015-02-11 11:15:24 -0800266
Corentin Walleza59fcdf2016-09-14 10:52:14 -0400267 return result;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800268 }
269
Olli Etuaho77ba4082016-12-16 12:01:18 +0000270 TDiagnostics *mDiagnostics;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800271
272 std::map<TString, CreatorFunctionData> mFunctions;
273 CreatorFunctionData *mCurrentFunction;
274 size_t mCurrentIndex;
275};
276
277// CallDAG
278
279CallDAG::CallDAG()
280{
281}
282
283CallDAG::~CallDAG()
284{
285}
286
287const size_t CallDAG::InvalidIndex = std::numeric_limits<size_t>::max();
288
Olli Etuahobd674552016-10-06 13:28:42 +0100289size_t CallDAG::findIndex(const TFunctionSymbolInfo *functionInfo) const
Corentin Wallez71d147f2015-02-11 11:15:24 -0800290{
Olli Etuahobd674552016-10-06 13:28:42 +0100291 auto it = mFunctionIdToIndex.find(functionInfo->getId());
Corentin Wallez71d147f2015-02-11 11:15:24 -0800292
293 if (it == mFunctionIdToIndex.end())
294 {
295 return InvalidIndex;
296 }
297 else
298 {
299 return it->second;
300 }
301}
302
303const CallDAG::Record &CallDAG::getRecordFromIndex(size_t index) const
304{
305 ASSERT(index != InvalidIndex && index < mRecords.size());
306 return mRecords[index];
307}
308
309const CallDAG::Record &CallDAG::getRecord(const TIntermAggregate *function) const
310{
Olli Etuahobd674552016-10-06 13:28:42 +0100311 size_t index = findIndex(function->getFunctionSymbolInfo());
Corentin Wallez71d147f2015-02-11 11:15:24 -0800312 ASSERT(index != InvalidIndex && index < mRecords.size());
313 return mRecords[index];
314}
315
316size_t CallDAG::size() const
317{
318 return mRecords.size();
319}
320
321void CallDAG::clear()
322{
323 mRecords.clear();
324 mFunctionIdToIndex.clear();
325}
326
Olli Etuaho77ba4082016-12-16 12:01:18 +0000327CallDAG::InitResult CallDAG::init(TIntermNode *root, TDiagnostics *diagnostics)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800328{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000329 CallDAGCreator creator(diagnostics);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800330
331 // Creates the mapping of functions to callees
332 root->traverse(&creator);
333
334 // Does the topological sort and detects recursions
335 InitResult result = creator.assignIndices();
336 if (result != INITDAG_SUCCESS)
337 {
338 return result;
339 }
340
341 creator.fillDataStructures(&mRecords, &mFunctionIdToIndex);
342 return INITDAG_SUCCESS;
343}
Jamie Madill45bcc782016-11-07 13:58:48 -0500344
345} // namespace sh