blob: 9d6fb066073da848b75d2a8c2b80f3d664782215 [file] [log] [blame]
ethannicholas22f939e2016-10-13 13:25:34 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
Ethan Nicholas0df1b042017-03-31 13:56:23 -04007
ethannicholas22f939e2016-10-13 13:25:34 -07008#include "SkSLCFGGenerator.h"
9
10#include "ir/SkSLConstructor.h"
11#include "ir/SkSLBinaryExpression.h"
12#include "ir/SkSLDoStatement.h"
13#include "ir/SkSLExpressionStatement.h"
14#include "ir/SkSLFieldAccess.h"
15#include "ir/SkSLForStatement.h"
16#include "ir/SkSLFunctionCall.h"
17#include "ir/SkSLIfStatement.h"
18#include "ir/SkSLIndexExpression.h"
19#include "ir/SkSLPostfixExpression.h"
20#include "ir/SkSLPrefixExpression.h"
21#include "ir/SkSLReturnStatement.h"
22#include "ir/SkSLSwizzle.h"
Ethan Nicholasaf197692017-02-27 13:26:45 -050023#include "ir/SkSLSwitchStatement.h"
ethannicholas22f939e2016-10-13 13:25:34 -070024#include "ir/SkSLTernaryExpression.h"
25#include "ir/SkSLVarDeclarationsStatement.h"
26#include "ir/SkSLWhileStatement.h"
27
28namespace SkSL {
29
30BlockId CFG::newBlock() {
31 BlockId result = fBlocks.size();
32 fBlocks.emplace_back();
33 if (fBlocks.size() > 1) {
34 this->addExit(fCurrent, result);
35 }
36 fCurrent = result;
37 return result;
38}
39
40BlockId CFG::newIsolatedBlock() {
41 BlockId result = fBlocks.size();
42 fBlocks.emplace_back();
43 return result;
44}
45
46void CFG::addExit(BlockId from, BlockId to) {
47 if (from == 0 || fBlocks[from].fEntrances.size()) {
48 fBlocks[from].fExits.insert(to);
49 fBlocks[to].fEntrances.insert(from);
50 }
51}
52
53void CFG::dump() {
54 for (size_t i = 0; i < fBlocks.size(); i++) {
55 printf("Block %d\n-------\nBefore: ", (int) i);
56 const char* separator = "";
57 for (auto iter = fBlocks[i].fBefore.begin(); iter != fBlocks[i].fBefore.end(); iter++) {
Ethan Nicholas86a43402017-01-19 13:32:00 -050058 printf("%s%s = %s", separator, iter->first->description().c_str(),
Ethan Nicholascb670962017-04-20 19:31:52 -040059 iter->second ? (*iter->second)->description().c_str() : "<undefined>");
ethannicholas22f939e2016-10-13 13:25:34 -070060 separator = ", ";
61 }
62 printf("\nEntrances: ");
63 separator = "";
64 for (BlockId b : fBlocks[i].fEntrances) {
65 printf("%s%d", separator, (int) b);
66 separator = ", ";
67 }
68 printf("\n");
69 for (size_t j = 0; j < fBlocks[i].fNodes.size(); j++) {
Ethan Nicholas86a43402017-01-19 13:32:00 -050070 BasicBlock::Node& n = fBlocks[i].fNodes[j];
Ethan Nicholascb670962017-04-20 19:31:52 -040071 printf("Node %d (%p): %s\n", (int) j, &n, n.fKind == BasicBlock::Node::kExpression_Kind
72 ? (*n.expression())->description().c_str()
73 : (*n.statement())->description().c_str());
ethannicholas22f939e2016-10-13 13:25:34 -070074 }
75 printf("Exits: ");
76 separator = "";
77 for (BlockId b : fBlocks[i].fExits) {
78 printf("%s%d", separator, (int) b);
79 separator = ", ";
80 }
81 printf("\n\n");
82 }
83}
84
Ethan Nicholascb670962017-04-20 19:31:52 -040085bool BasicBlock::tryRemoveExpressionBefore(std::vector<BasicBlock::Node>::iterator* iter,
86 Expression* e) {
87 if (e->fKind == Expression::kTernary_Kind) {
88 return false;
89 }
90 bool result;
91 if ((*iter)->fKind == BasicBlock::Node::kExpression_Kind) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -040092 SkASSERT((*iter)->expression()->get() != e);
Ethan Nicholascb670962017-04-20 19:31:52 -040093 Expression* old = (*iter)->expression()->get();
94 do {
95 if ((*iter) == fNodes.begin()) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -040096 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -040097 }
98 --(*iter);
99 } while ((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
100 (*iter)->expression()->get() != e);
101 result = this->tryRemoveExpression(iter);
102 while ((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
103 (*iter)->expression()->get() != old) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400104 SkASSERT(*iter != fNodes.end());
Ethan Nicholascb670962017-04-20 19:31:52 -0400105 ++(*iter);
106 }
107 } else {
108 Statement* old = (*iter)->statement()->get();
109 do {
110 if ((*iter) == fNodes.begin()) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400111 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400112 }
113 --(*iter);
114 } while ((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
115 (*iter)->expression()->get() != e);
116 result = this->tryRemoveExpression(iter);
117 while ((*iter)->fKind != BasicBlock::Node::kStatement_Kind ||
118 (*iter)->statement()->get() != old) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400119 SkASSERT(*iter != fNodes.end());
Ethan Nicholascb670962017-04-20 19:31:52 -0400120 ++(*iter);
121 }
122 }
123 return result;
124}
125
126bool BasicBlock::tryRemoveLValueBefore(std::vector<BasicBlock::Node>::iterator* iter,
127 Expression* lvalue) {
128 switch (lvalue->fKind) {
129 case Expression::kVariableReference_Kind:
130 return true;
131 case Expression::kSwizzle_Kind:
132 return this->tryRemoveLValueBefore(iter, ((Swizzle*) lvalue)->fBase.get());
133 case Expression::kFieldAccess_Kind:
134 return this->tryRemoveLValueBefore(iter, ((FieldAccess*) lvalue)->fBase.get());
135 case Expression::kIndex_Kind:
136 if (!this->tryRemoveLValueBefore(iter, ((IndexExpression*) lvalue)->fBase.get())) {
137 return false;
138 }
139 return this->tryRemoveExpressionBefore(iter, ((IndexExpression*) lvalue)->fIndex.get());
Ethan Nicholasa583b812018-01-18 13:32:11 -0500140 case Expression::kTernary_Kind:
141 if (!this->tryRemoveExpressionBefore(iter,
142 ((TernaryExpression*) lvalue)->fTest.get())) {
143 return false;
144 }
145 if (!this->tryRemoveLValueBefore(iter, ((TernaryExpression*) lvalue)->fIfTrue.get())) {
146 return false;
147 }
148 return this->tryRemoveLValueBefore(iter, ((TernaryExpression*) lvalue)->fIfFalse.get());
Ethan Nicholascb670962017-04-20 19:31:52 -0400149 default:
150 ABORT("invalid lvalue: %s\n", lvalue->description().c_str());
151 }
152}
153
154bool BasicBlock::tryRemoveExpression(std::vector<BasicBlock::Node>::iterator* iter) {
155 Expression* expr = (*iter)->expression()->get();
156 switch (expr->fKind) {
157 case Expression::kBinary_Kind: {
158 BinaryExpression* b = (BinaryExpression*) expr;
159 if (b->fOperator == Token::EQ) {
160 if (!this->tryRemoveLValueBefore(iter, b->fLeft.get())) {
161 return false;
162 }
163 } else if (!this->tryRemoveExpressionBefore(iter, b->fLeft.get())) {
164 return false;
165 }
166 if (!this->tryRemoveExpressionBefore(iter, b->fRight.get())) {
167 return false;
168 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400169 SkASSERT((*iter)->expression()->get() == expr);
Ethan Nicholascb670962017-04-20 19:31:52 -0400170 *iter = fNodes.erase(*iter);
171 return true;
172 }
173 case Expression::kTernary_Kind: {
174 // ternaries cross basic block boundaries, must regenerate the CFG to remove it
175 return false;
176 }
177 case Expression::kFieldAccess_Kind: {
178 FieldAccess* f = (FieldAccess*) expr;
179 if (!this->tryRemoveExpressionBefore(iter, f->fBase.get())) {
180 return false;
181 }
182 *iter = fNodes.erase(*iter);
183 return true;
184 }
185 case Expression::kSwizzle_Kind: {
186 Swizzle* s = (Swizzle*) expr;
187 if (!this->tryRemoveExpressionBefore(iter, s->fBase.get())) {
188 return false;
189 }
190 *iter = fNodes.erase(*iter);
191 return true;
192 }
193 case Expression::kIndex_Kind: {
194 IndexExpression* idx = (IndexExpression*) expr;
195 if (!this->tryRemoveExpressionBefore(iter, idx->fBase.get())) {
196 return false;
197 }
198 if (!this->tryRemoveExpressionBefore(iter, idx->fIndex.get())) {
199 return false;
200 }
201 *iter = fNodes.erase(*iter);
202 return true;
203 }
204 case Expression::kConstructor_Kind: {
205 Constructor* c = (Constructor*) expr;
206 for (auto& arg : c->fArguments) {
207 if (!this->tryRemoveExpressionBefore(iter, arg.get())) {
208 return false;
209 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400210 SkASSERT((*iter)->expression()->get() == expr);
Ethan Nicholascb670962017-04-20 19:31:52 -0400211 }
212 *iter = fNodes.erase(*iter);
213 return true;
214 }
215 case Expression::kFunctionCall_Kind: {
216 FunctionCall* f = (FunctionCall*) expr;
217 for (auto& arg : f->fArguments) {
218 if (!this->tryRemoveExpressionBefore(iter, arg.get())) {
219 return false;
220 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400221 SkASSERT((*iter)->expression()->get() == expr);
Ethan Nicholascb670962017-04-20 19:31:52 -0400222 }
223 *iter = fNodes.erase(*iter);
224 return true;
225 }
226 case Expression::kPrefix_Kind:
227 if (!this->tryRemoveExpressionBefore(iter,
228 ((PrefixExpression*) expr)->fOperand.get())) {
229 return false;
230 }
231 *iter = fNodes.erase(*iter);
232 return true;
233 case Expression::kPostfix_Kind:
234 if (!this->tryRemoveExpressionBefore(iter,
235 ((PrefixExpression*) expr)->fOperand.get())) {
236 return false;
237 }
238 *iter = fNodes.erase(*iter);
239 return true;
240 case Expression::kBoolLiteral_Kind: // fall through
241 case Expression::kFloatLiteral_Kind: // fall through
242 case Expression::kIntLiteral_Kind: // fall through
Ethan Nicholas762466e2017-06-29 10:03:38 -0400243 case Expression::kSetting_Kind: // fall through
Ethan Nicholascb670962017-04-20 19:31:52 -0400244 case Expression::kVariableReference_Kind:
245 *iter = fNodes.erase(*iter);
246 return true;
247 default:
248 ABORT("unhandled expression: %s\n", expr->description().c_str());
249 }
250}
251
252bool BasicBlock::tryInsertExpression(std::vector<BasicBlock::Node>::iterator* iter,
253 std::unique_ptr<Expression>* expr) {
254 switch ((*expr)->fKind) {
255 case Expression::kBinary_Kind: {
256 BinaryExpression* b = (BinaryExpression*) expr->get();
257 if (!this->tryInsertExpression(iter, &b->fRight)) {
258 return false;
259 }
260 ++(*iter);
261 if (!this->tryInsertExpression(iter, &b->fLeft)) {
262 return false;
263 }
264 ++(*iter);
265 BasicBlock::Node node = { BasicBlock::Node::kExpression_Kind, true, expr, nullptr };
266 *iter = fNodes.insert(*iter, node);
267 return true;
268 }
269 case Expression::kBoolLiteral_Kind: // fall through
270 case Expression::kFloatLiteral_Kind: // fall through
271 case Expression::kIntLiteral_Kind: // fall through
272 case Expression::kVariableReference_Kind: {
273 BasicBlock::Node node = { BasicBlock::Node::kExpression_Kind, true, expr, nullptr };
274 *iter = fNodes.insert(*iter, node);
275 return true;
276 }
277 case Expression::kConstructor_Kind: {
278 Constructor* c = (Constructor*) expr->get();
279 for (auto& arg : c->fArguments) {
280 if (!this->tryInsertExpression(iter, &arg)) {
281 return false;
282 }
283 ++(*iter);
284 }
285 BasicBlock::Node node = { BasicBlock::Node::kExpression_Kind, true, expr, nullptr };
286 *iter = fNodes.insert(*iter, node);
287 return true;
288 }
289 default:
290 return false;
291 }
292}
293
Ethan Nicholas86a43402017-01-19 13:32:00 -0500294void CFGGenerator::addExpression(CFG& cfg, std::unique_ptr<Expression>* e, bool constantPropagate) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400295 SkASSERT(e);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500296 switch ((*e)->fKind) {
ethannicholas22f939e2016-10-13 13:25:34 -0700297 case Expression::kBinary_Kind: {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500298 BinaryExpression* b = (BinaryExpression*) e->get();
ethannicholas22f939e2016-10-13 13:25:34 -0700299 switch (b->fOperator) {
300 case Token::LOGICALAND: // fall through
301 case Token::LOGICALOR: {
302 // this isn't as precise as it could be -- we don't bother to track that if we
303 // early exit from a logical and/or, we know which branch of an 'if' we're going
304 // to hit -- but it won't make much difference in practice.
Ethan Nicholas86a43402017-01-19 13:32:00 -0500305 this->addExpression(cfg, &b->fLeft, constantPropagate);
ethannicholas22f939e2016-10-13 13:25:34 -0700306 BlockId start = cfg.fCurrent;
307 cfg.newBlock();
Ethan Nicholas86a43402017-01-19 13:32:00 -0500308 this->addExpression(cfg, &b->fRight, constantPropagate);
ethannicholas22f939e2016-10-13 13:25:34 -0700309 cfg.newBlock();
310 cfg.addExit(start, cfg.fCurrent);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400311 cfg.fBlocks[cfg.fCurrent].fNodes.push_back({
312 BasicBlock::Node::kExpression_Kind,
313 constantPropagate,
314 e,
315 nullptr
316 });
ethannicholas22f939e2016-10-13 13:25:34 -0700317 break;
318 }
319 case Token::EQ: {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500320 this->addExpression(cfg, &b->fRight, constantPropagate);
321 this->addLValue(cfg, &b->fLeft);
322 cfg.fBlocks[cfg.fCurrent].fNodes.push_back({
323 BasicBlock::Node::kExpression_Kind,
324 constantPropagate,
325 e,
326 nullptr
ethannicholas22f939e2016-10-13 13:25:34 -0700327 });
328 break;
329 }
330 default:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700331 this->addExpression(cfg, &b->fLeft, !Compiler::IsAssignment(b->fOperator));
Ethan Nicholas86a43402017-01-19 13:32:00 -0500332 this->addExpression(cfg, &b->fRight, constantPropagate);
333 cfg.fBlocks[cfg.fCurrent].fNodes.push_back({
334 BasicBlock::Node::kExpression_Kind,
335 constantPropagate,
336 e,
337 nullptr
ethannicholas22f939e2016-10-13 13:25:34 -0700338 });
339 }
340 break;
341 }
342 case Expression::kConstructor_Kind: {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500343 Constructor* c = (Constructor*) e->get();
344 for (auto& arg : c->fArguments) {
345 this->addExpression(cfg, &arg, constantPropagate);
ethannicholas22f939e2016-10-13 13:25:34 -0700346 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500347 cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind,
348 constantPropagate, e, nullptr });
ethannicholas22f939e2016-10-13 13:25:34 -0700349 break;
350 }
351 case Expression::kFunctionCall_Kind: {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500352 FunctionCall* c = (FunctionCall*) e->get();
353 for (auto& arg : c->fArguments) {
354 this->addExpression(cfg, &arg, constantPropagate);
ethannicholas22f939e2016-10-13 13:25:34 -0700355 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500356 cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind,
357 constantPropagate, e, nullptr });
ethannicholas22f939e2016-10-13 13:25:34 -0700358 break;
359 }
360 case Expression::kFieldAccess_Kind:
Ethan Nicholas86a43402017-01-19 13:32:00 -0500361 this->addExpression(cfg, &((FieldAccess*) e->get())->fBase, constantPropagate);
362 cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind,
363 constantPropagate, e, nullptr });
ethannicholas22f939e2016-10-13 13:25:34 -0700364 break;
365 case Expression::kIndex_Kind:
Ethan Nicholas86a43402017-01-19 13:32:00 -0500366 this->addExpression(cfg, &((IndexExpression*) e->get())->fBase, constantPropagate);
367 this->addExpression(cfg, &((IndexExpression*) e->get())->fIndex, constantPropagate);
368 cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind,
369 constantPropagate, e, nullptr });
ethannicholas22f939e2016-10-13 13:25:34 -0700370 break;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500371 case Expression::kPrefix_Kind: {
372 PrefixExpression* p = (PrefixExpression*) e->get();
373 this->addExpression(cfg, &p->fOperand, constantPropagate &&
374 p->fOperator != Token::PLUSPLUS &&
375 p->fOperator != Token::MINUSMINUS);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500376 cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind,
377 constantPropagate, e, nullptr });
ethannicholas22f939e2016-10-13 13:25:34 -0700378 break;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500379 }
ethannicholas22f939e2016-10-13 13:25:34 -0700380 case Expression::kPostfix_Kind:
Ethan Nicholasaf197692017-02-27 13:26:45 -0500381 this->addExpression(cfg, &((PostfixExpression*) e->get())->fOperand, false);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500382 cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind,
383 constantPropagate, e, nullptr });
ethannicholas22f939e2016-10-13 13:25:34 -0700384 break;
385 case Expression::kSwizzle_Kind:
Ethan Nicholas86a43402017-01-19 13:32:00 -0500386 this->addExpression(cfg, &((Swizzle*) e->get())->fBase, constantPropagate);
387 cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind,
388 constantPropagate, e, nullptr });
ethannicholas22f939e2016-10-13 13:25:34 -0700389 break;
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400390 case Expression::kAppendStage_Kind: // fall through
ethannicholas22f939e2016-10-13 13:25:34 -0700391 case Expression::kBoolLiteral_Kind: // fall through
392 case Expression::kFloatLiteral_Kind: // fall through
393 case Expression::kIntLiteral_Kind: // fall through
Ethan Nicholasee1c8a72019-02-22 10:50:47 -0500394 case Expression::kNullLiteral_Kind: // fall through
Ethan Nicholas762466e2017-06-29 10:03:38 -0400395 case Expression::kSetting_Kind: // fall through
ethannicholas22f939e2016-10-13 13:25:34 -0700396 case Expression::kVariableReference_Kind:
Ethan Nicholas86a43402017-01-19 13:32:00 -0500397 cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind,
398 constantPropagate, e, nullptr });
ethannicholas22f939e2016-10-13 13:25:34 -0700399 break;
400 case Expression::kTernary_Kind: {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500401 TernaryExpression* t = (TernaryExpression*) e->get();
402 this->addExpression(cfg, &t->fTest, constantPropagate);
Ethan Nicholascb670962017-04-20 19:31:52 -0400403 cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind,
404 constantPropagate, e, nullptr });
ethannicholas22f939e2016-10-13 13:25:34 -0700405 BlockId start = cfg.fCurrent;
406 cfg.newBlock();
Ethan Nicholas86a43402017-01-19 13:32:00 -0500407 this->addExpression(cfg, &t->fIfTrue, constantPropagate);
ethannicholas22f939e2016-10-13 13:25:34 -0700408 BlockId next = cfg.newBlock();
409 cfg.fCurrent = start;
410 cfg.newBlock();
Ethan Nicholas86a43402017-01-19 13:32:00 -0500411 this->addExpression(cfg, &t->fIfFalse, constantPropagate);
ethannicholas22f939e2016-10-13 13:25:34 -0700412 cfg.addExit(cfg.fCurrent, next);
413 cfg.fCurrent = next;
414 break;
415 }
416 case Expression::kFunctionReference_Kind: // fall through
417 case Expression::kTypeReference_Kind: // fall through
418 case Expression::kDefined_Kind:
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400419 SkASSERT(false);
ethannicholas22f939e2016-10-13 13:25:34 -0700420 break;
421 }
422}
423
424// adds expressions that are evaluated as part of resolving an lvalue
Ethan Nicholas86a43402017-01-19 13:32:00 -0500425void CFGGenerator::addLValue(CFG& cfg, std::unique_ptr<Expression>* e) {
426 switch ((*e)->fKind) {
ethannicholas22f939e2016-10-13 13:25:34 -0700427 case Expression::kFieldAccess_Kind:
Ethan Nicholas86a43402017-01-19 13:32:00 -0500428 this->addLValue(cfg, &((FieldAccess&) **e).fBase);
ethannicholas22f939e2016-10-13 13:25:34 -0700429 break;
430 case Expression::kIndex_Kind:
Ethan Nicholas86a43402017-01-19 13:32:00 -0500431 this->addLValue(cfg, &((IndexExpression&) **e).fBase);
432 this->addExpression(cfg, &((IndexExpression&) **e).fIndex, true);
ethannicholas22f939e2016-10-13 13:25:34 -0700433 break;
434 case Expression::kSwizzle_Kind:
Ethan Nicholas86a43402017-01-19 13:32:00 -0500435 this->addLValue(cfg, &((Swizzle&) **e).fBase);
ethannicholas22f939e2016-10-13 13:25:34 -0700436 break;
437 case Expression::kVariableReference_Kind:
438 break;
Ethan Nicholasa583b812018-01-18 13:32:11 -0500439 case Expression::kTernary_Kind:
440 this->addExpression(cfg, &((TernaryExpression&) **e).fTest, true);
441 // Technically we will of course only evaluate one or the other, but if the test turns
442 // out to be constant, the ternary will get collapsed down to just one branch anyway. So
443 // it should be ok to pretend that we always evaluate both branches here.
444 this->addLValue(cfg, &((TernaryExpression&) **e).fIfTrue);
445 this->addLValue(cfg, &((TernaryExpression&) **e).fIfFalse);
446 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700447 default:
448 // not an lvalue, can't happen
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400449 SkASSERT(false);
ethannicholas22f939e2016-10-13 13:25:34 -0700450 break;
451 }
452}
453
Ethan Nicholascb670962017-04-20 19:31:52 -0400454void CFGGenerator::addStatement(CFG& cfg, std::unique_ptr<Statement>* s) {
455 switch ((*s)->fKind) {
ethannicholas22f939e2016-10-13 13:25:34 -0700456 case Statement::kBlock_Kind:
Ethan Nicholascb670962017-04-20 19:31:52 -0400457 for (auto& child : ((Block&) **s).fStatements) {
458 addStatement(cfg, &child);
ethannicholas22f939e2016-10-13 13:25:34 -0700459 }
460 break;
461 case Statement::kIf_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400462 IfStatement& ifs = (IfStatement&) **s;
463 this->addExpression(cfg, &ifs.fTest, true);
464 cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false,
465 nullptr, s });
ethannicholas22f939e2016-10-13 13:25:34 -0700466 BlockId start = cfg.fCurrent;
467 cfg.newBlock();
Ethan Nicholascb670962017-04-20 19:31:52 -0400468 this->addStatement(cfg, &ifs.fIfTrue);
ethannicholas22f939e2016-10-13 13:25:34 -0700469 BlockId next = cfg.newBlock();
Ethan Nicholascb670962017-04-20 19:31:52 -0400470 if (ifs.fIfFalse) {
ethannicholas22f939e2016-10-13 13:25:34 -0700471 cfg.fCurrent = start;
472 cfg.newBlock();
Ethan Nicholascb670962017-04-20 19:31:52 -0400473 this->addStatement(cfg, &ifs.fIfFalse);
ethannicholas22f939e2016-10-13 13:25:34 -0700474 cfg.addExit(cfg.fCurrent, next);
475 cfg.fCurrent = next;
476 } else {
477 cfg.addExit(start, next);
478 }
479 break;
480 }
481 case Statement::kExpression_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400482 this->addExpression(cfg, &((ExpressionStatement&) **s).fExpression, true);
483 cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false,
484 nullptr, s });
ethannicholas22f939e2016-10-13 13:25:34 -0700485 break;
486 }
487 case Statement::kVarDeclarations_Kind: {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000488 VarDeclarationsStatement& decls = ((VarDeclarationsStatement&) **s);
489 for (auto& stmt : decls.fDeclaration->fVars) {
490 if (stmt->fKind == Statement::kNop_Kind) {
491 continue;
492 }
493 VarDeclaration& vd = (VarDeclaration&) *stmt;
494 if (vd.fValue) {
495 this->addExpression(cfg, &vd.fValue, true);
496 }
497 cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind,
498 false, nullptr, &stmt });
499 }
Ethan Nicholas91a10532017-06-22 11:24:38 -0400500 cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false,
501 nullptr, s });
ethannicholas22f939e2016-10-13 13:25:34 -0700502 break;
503 }
504 case Statement::kDiscard_Kind:
Ethan Nicholas86a43402017-01-19 13:32:00 -0500505 cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false,
506 nullptr, s });
ethannicholas22f939e2016-10-13 13:25:34 -0700507 cfg.fCurrent = cfg.newIsolatedBlock();
508 break;
509 case Statement::kReturn_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400510 ReturnStatement& r = ((ReturnStatement&) **s);
ethannicholas22f939e2016-10-13 13:25:34 -0700511 if (r.fExpression) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500512 this->addExpression(cfg, &r.fExpression, true);
ethannicholas22f939e2016-10-13 13:25:34 -0700513 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500514 cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false,
515 nullptr, s });
ethannicholas22f939e2016-10-13 13:25:34 -0700516 cfg.fCurrent = cfg.newIsolatedBlock();
517 break;
518 }
519 case Statement::kBreak_Kind:
Ethan Nicholas86a43402017-01-19 13:32:00 -0500520 cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false,
521 nullptr, s });
ethannicholas22f939e2016-10-13 13:25:34 -0700522 cfg.addExit(cfg.fCurrent, fLoopExits.top());
523 cfg.fCurrent = cfg.newIsolatedBlock();
524 break;
525 case Statement::kContinue_Kind:
Ethan Nicholas86a43402017-01-19 13:32:00 -0500526 cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false,
527 nullptr, s });
ethannicholas22f939e2016-10-13 13:25:34 -0700528 cfg.addExit(cfg.fCurrent, fLoopContinues.top());
529 cfg.fCurrent = cfg.newIsolatedBlock();
530 break;
531 case Statement::kWhile_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400532 WhileStatement& w = (WhileStatement&) **s;
ethannicholas22f939e2016-10-13 13:25:34 -0700533 BlockId loopStart = cfg.newBlock();
534 fLoopContinues.push(loopStart);
535 BlockId loopExit = cfg.newIsolatedBlock();
536 fLoopExits.push(loopExit);
Ethan Nicholascb670962017-04-20 19:31:52 -0400537 this->addExpression(cfg, &w.fTest, true);
ethannicholas22f939e2016-10-13 13:25:34 -0700538 BlockId test = cfg.fCurrent;
539 cfg.addExit(test, loopExit);
540 cfg.newBlock();
Ethan Nicholascb670962017-04-20 19:31:52 -0400541 this->addStatement(cfg, &w.fStatement);
ethannicholas22f939e2016-10-13 13:25:34 -0700542 cfg.addExit(cfg.fCurrent, loopStart);
543 fLoopContinues.pop();
544 fLoopExits.pop();
545 cfg.fCurrent = loopExit;
546 break;
547 }
548 case Statement::kDo_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400549 DoStatement& d = (DoStatement&) **s;
ethannicholas22f939e2016-10-13 13:25:34 -0700550 BlockId loopStart = cfg.newBlock();
551 fLoopContinues.push(loopStart);
552 BlockId loopExit = cfg.newIsolatedBlock();
553 fLoopExits.push(loopExit);
Ethan Nicholascb670962017-04-20 19:31:52 -0400554 this->addStatement(cfg, &d.fStatement);
555 this->addExpression(cfg, &d.fTest, true);
ethannicholas22f939e2016-10-13 13:25:34 -0700556 cfg.addExit(cfg.fCurrent, loopExit);
557 cfg.addExit(cfg.fCurrent, loopStart);
558 fLoopContinues.pop();
559 fLoopExits.pop();
560 cfg.fCurrent = loopExit;
561 break;
562 }
563 case Statement::kFor_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400564 ForStatement& f = (ForStatement&) **s;
565 if (f.fInitializer) {
566 this->addStatement(cfg, &f.fInitializer);
ethannicholas22f939e2016-10-13 13:25:34 -0700567 }
568 BlockId loopStart = cfg.newBlock();
569 BlockId next = cfg.newIsolatedBlock();
570 fLoopContinues.push(next);
571 BlockId loopExit = cfg.newIsolatedBlock();
572 fLoopExits.push(loopExit);
Ethan Nicholascb670962017-04-20 19:31:52 -0400573 if (f.fTest) {
574 this->addExpression(cfg, &f.fTest, true);
Ethan Nicholasd9fe7002017-05-30 10:15:34 -0400575 // this isn't quite right; we should have an exit from here to the loop exit, and
576 // remove the exit from the loop body to the loop exit. Structuring it like this
577 // forces the optimizer to believe that the loop body is always executed at least
578 // once. While not strictly correct, this avoids incorrect "variable not assigned"
579 // errors on variables which are assigned within the loop. The correct solution to
580 // this is to analyze the loop to see whether or not at least one iteration is
581 // guaranteed to happen, but for the time being we take the easy way out.
ethannicholas22f939e2016-10-13 13:25:34 -0700582 }
583 cfg.newBlock();
Ethan Nicholascb670962017-04-20 19:31:52 -0400584 this->addStatement(cfg, &f.fStatement);
ethannicholas22f939e2016-10-13 13:25:34 -0700585 cfg.addExit(cfg.fCurrent, next);
586 cfg.fCurrent = next;
Ethan Nicholascb670962017-04-20 19:31:52 -0400587 if (f.fNext) {
588 this->addExpression(cfg, &f.fNext, true);
ethannicholas22f939e2016-10-13 13:25:34 -0700589 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500590 cfg.addExit(cfg.fCurrent, loopStart);
Ethan Nicholasd9fe7002017-05-30 10:15:34 -0400591 cfg.addExit(cfg.fCurrent, loopExit);
ethannicholas22f939e2016-10-13 13:25:34 -0700592 fLoopContinues.pop();
593 fLoopExits.pop();
594 cfg.fCurrent = loopExit;
595 break;
596 }
Ethan Nicholasaf197692017-02-27 13:26:45 -0500597 case Statement::kSwitch_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400598 SwitchStatement& ss = (SwitchStatement&) **s;
599 this->addExpression(cfg, &ss.fValue, true);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400600 cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false,
601 nullptr, s });
Ethan Nicholasaf197692017-02-27 13:26:45 -0500602 BlockId start = cfg.fCurrent;
603 BlockId switchExit = cfg.newIsolatedBlock();
604 fLoopExits.push(switchExit);
Ethan Nicholascb670962017-04-20 19:31:52 -0400605 for (const auto& c : ss.fCases) {
Ethan Nicholasaf197692017-02-27 13:26:45 -0500606 cfg.newBlock();
607 cfg.addExit(start, cfg.fCurrent);
Ethan Nicholaseace9352018-10-15 20:09:54 +0000608 if (c->fValue) {
609 // technically this should go in the start block, but it doesn't actually matter
610 // because it must be constant. Not worth running two loops for.
611 this->addExpression(cfg, &c->fValue, true);
612 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400613 for (auto& caseStatement : c->fStatements) {
614 this->addStatement(cfg, &caseStatement);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500615 }
616 }
617 cfg.addExit(cfg.fCurrent, switchExit);
618 // note that unlike GLSL, our grammar requires the default case to be last
Ethan Nicholascb670962017-04-20 19:31:52 -0400619 if (0 == ss.fCases.size() || ss.fCases[ss.fCases.size() - 1]->fValue) {
Ethan Nicholasaf197692017-02-27 13:26:45 -0500620 // switch does not have a default clause, mark that it can skip straight to the end
621 cfg.addExit(start, switchExit);
622 }
623 fLoopExits.pop();
624 cfg.fCurrent = switchExit;
625 break;
626 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400627 case Statement::kNop_Kind:
628 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700629 default:
Ethan Nicholascb670962017-04-20 19:31:52 -0400630 printf("statement: %s\n", (*s)->description().c_str());
ethannicholas22f939e2016-10-13 13:25:34 -0700631 ABORT("unsupported statement kind");
632 }
633}
634
Ethan Nicholascb670962017-04-20 19:31:52 -0400635CFG CFGGenerator::getCFG(FunctionDefinition& f) {
ethannicholas22f939e2016-10-13 13:25:34 -0700636 CFG result;
637 result.fStart = result.newBlock();
638 result.fCurrent = result.fStart;
Ethan Nicholascb670962017-04-20 19:31:52 -0400639 this->addStatement(result, &f.fBody);
ethannicholas22f939e2016-10-13 13:25:34 -0700640 result.newBlock();
641 result.fExit = result.fCurrent;
642 return result;
643}
644
645} // namespace