ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1 | /* |
| 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 Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/sksl/SkSLCFGGenerator.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
| 11 | #include "src/sksl/ir/SkSLConstructor.h" |
| 12 | #include "src/sksl/ir/SkSLDoStatement.h" |
| 13 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 14 | #include "src/sksl/ir/SkSLExternalFunctionCall.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/sksl/ir/SkSLFieldAccess.h" |
| 16 | #include "src/sksl/ir/SkSLForStatement.h" |
| 17 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 18 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 19 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 20 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 21 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 22 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 23 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
| 24 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 25 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 26 | #include "src/sksl/ir/SkSLVarDeclarationsStatement.h" |
| 27 | #include "src/sksl/ir/SkSLWhileStatement.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 28 | |
| 29 | namespace SkSL { |
| 30 | |
| 31 | BlockId CFG::newBlock() { |
| 32 | BlockId result = fBlocks.size(); |
| 33 | fBlocks.emplace_back(); |
| 34 | if (fBlocks.size() > 1) { |
| 35 | this->addExit(fCurrent, result); |
| 36 | } |
| 37 | fCurrent = result; |
| 38 | return result; |
| 39 | } |
| 40 | |
| 41 | BlockId CFG::newIsolatedBlock() { |
| 42 | BlockId result = fBlocks.size(); |
| 43 | fBlocks.emplace_back(); |
| 44 | return result; |
| 45 | } |
| 46 | |
| 47 | void CFG::addExit(BlockId from, BlockId to) { |
| 48 | if (from == 0 || fBlocks[from].fEntrances.size()) { |
| 49 | fBlocks[from].fExits.insert(to); |
| 50 | fBlocks[to].fEntrances.insert(from); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void CFG::dump() { |
| 55 | for (size_t i = 0; i < fBlocks.size(); i++) { |
| 56 | printf("Block %d\n-------\nBefore: ", (int) i); |
| 57 | const char* separator = ""; |
| 58 | for (auto iter = fBlocks[i].fBefore.begin(); iter != fBlocks[i].fBefore.end(); iter++) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 59 | printf("%s%s = %s", separator, iter->first->description().c_str(), |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 60 | iter->second ? (*iter->second)->description().c_str() : "<undefined>"); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 61 | separator = ", "; |
| 62 | } |
| 63 | printf("\nEntrances: "); |
| 64 | separator = ""; |
| 65 | for (BlockId b : fBlocks[i].fEntrances) { |
| 66 | printf("%s%d", separator, (int) b); |
| 67 | separator = ", "; |
| 68 | } |
| 69 | printf("\n"); |
| 70 | for (size_t j = 0; j < fBlocks[i].fNodes.size(); j++) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 71 | BasicBlock::Node& n = fBlocks[i].fNodes[j]; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 72 | printf("Node %d (%p): %s\n", (int) j, &n, n.fKind == BasicBlock::Node::kExpression_Kind |
| 73 | ? (*n.expression())->description().c_str() |
| 74 | : (*n.statement())->description().c_str()); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 75 | } |
| 76 | printf("Exits: "); |
| 77 | separator = ""; |
| 78 | for (BlockId b : fBlocks[i].fExits) { |
| 79 | printf("%s%d", separator, (int) b); |
| 80 | separator = ", "; |
| 81 | } |
| 82 | printf("\n\n"); |
| 83 | } |
| 84 | } |
| 85 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 86 | bool BasicBlock::tryRemoveExpressionBefore(std::vector<BasicBlock::Node>::iterator* iter, |
| 87 | Expression* e) { |
| 88 | if (e->fKind == Expression::kTernary_Kind) { |
| 89 | return false; |
| 90 | } |
| 91 | bool result; |
| 92 | if ((*iter)->fKind == BasicBlock::Node::kExpression_Kind) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 93 | SkASSERT((*iter)->expression()->get() != e); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 94 | Expression* old = (*iter)->expression()->get(); |
| 95 | do { |
| 96 | if ((*iter) == fNodes.begin()) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 97 | return false; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 98 | } |
| 99 | --(*iter); |
| 100 | } while ((*iter)->fKind != BasicBlock::Node::kExpression_Kind || |
| 101 | (*iter)->expression()->get() != e); |
| 102 | result = this->tryRemoveExpression(iter); |
| 103 | while ((*iter)->fKind != BasicBlock::Node::kExpression_Kind || |
| 104 | (*iter)->expression()->get() != old) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 105 | SkASSERT(*iter != fNodes.end()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 106 | ++(*iter); |
| 107 | } |
| 108 | } else { |
| 109 | Statement* old = (*iter)->statement()->get(); |
| 110 | do { |
| 111 | if ((*iter) == fNodes.begin()) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 112 | return false; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 113 | } |
| 114 | --(*iter); |
| 115 | } while ((*iter)->fKind != BasicBlock::Node::kExpression_Kind || |
| 116 | (*iter)->expression()->get() != e); |
| 117 | result = this->tryRemoveExpression(iter); |
| 118 | while ((*iter)->fKind != BasicBlock::Node::kStatement_Kind || |
| 119 | (*iter)->statement()->get() != old) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 120 | SkASSERT(*iter != fNodes.end()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 121 | ++(*iter); |
| 122 | } |
| 123 | } |
| 124 | return result; |
| 125 | } |
| 126 | |
| 127 | bool BasicBlock::tryRemoveLValueBefore(std::vector<BasicBlock::Node>::iterator* iter, |
| 128 | Expression* lvalue) { |
| 129 | switch (lvalue->fKind) { |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 130 | case Expression::kExternalValue_Kind: // fall through |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 131 | case Expression::kVariableReference_Kind: |
| 132 | return true; |
| 133 | case Expression::kSwizzle_Kind: |
| 134 | return this->tryRemoveLValueBefore(iter, ((Swizzle*) lvalue)->fBase.get()); |
| 135 | case Expression::kFieldAccess_Kind: |
| 136 | return this->tryRemoveLValueBefore(iter, ((FieldAccess*) lvalue)->fBase.get()); |
| 137 | case Expression::kIndex_Kind: |
| 138 | if (!this->tryRemoveLValueBefore(iter, ((IndexExpression*) lvalue)->fBase.get())) { |
| 139 | return false; |
| 140 | } |
| 141 | return this->tryRemoveExpressionBefore(iter, ((IndexExpression*) lvalue)->fIndex.get()); |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 142 | case Expression::kTernary_Kind: |
| 143 | if (!this->tryRemoveExpressionBefore(iter, |
| 144 | ((TernaryExpression*) lvalue)->fTest.get())) { |
| 145 | return false; |
| 146 | } |
| 147 | if (!this->tryRemoveLValueBefore(iter, ((TernaryExpression*) lvalue)->fIfTrue.get())) { |
| 148 | return false; |
| 149 | } |
| 150 | return this->tryRemoveLValueBefore(iter, ((TernaryExpression*) lvalue)->fIfFalse.get()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 151 | default: |
| 152 | ABORT("invalid lvalue: %s\n", lvalue->description().c_str()); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | bool BasicBlock::tryRemoveExpression(std::vector<BasicBlock::Node>::iterator* iter) { |
| 157 | Expression* expr = (*iter)->expression()->get(); |
| 158 | switch (expr->fKind) { |
| 159 | case Expression::kBinary_Kind: { |
| 160 | BinaryExpression* b = (BinaryExpression*) expr; |
| 161 | if (b->fOperator == Token::EQ) { |
| 162 | if (!this->tryRemoveLValueBefore(iter, b->fLeft.get())) { |
| 163 | return false; |
| 164 | } |
| 165 | } else if (!this->tryRemoveExpressionBefore(iter, b->fLeft.get())) { |
| 166 | return false; |
| 167 | } |
| 168 | if (!this->tryRemoveExpressionBefore(iter, b->fRight.get())) { |
| 169 | return false; |
| 170 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 171 | SkASSERT((*iter)->expression()->get() == expr); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 172 | *iter = fNodes.erase(*iter); |
| 173 | return true; |
| 174 | } |
| 175 | case Expression::kTernary_Kind: { |
| 176 | // ternaries cross basic block boundaries, must regenerate the CFG to remove it |
| 177 | return false; |
| 178 | } |
| 179 | case Expression::kFieldAccess_Kind: { |
| 180 | FieldAccess* f = (FieldAccess*) expr; |
| 181 | if (!this->tryRemoveExpressionBefore(iter, f->fBase.get())) { |
| 182 | return false; |
| 183 | } |
| 184 | *iter = fNodes.erase(*iter); |
| 185 | return true; |
| 186 | } |
| 187 | case Expression::kSwizzle_Kind: { |
| 188 | Swizzle* s = (Swizzle*) expr; |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 189 | if (s->fBase && !this->tryRemoveExpressionBefore(iter, s->fBase.get())) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 190 | return false; |
| 191 | } |
| 192 | *iter = fNodes.erase(*iter); |
| 193 | return true; |
| 194 | } |
| 195 | case Expression::kIndex_Kind: { |
| 196 | IndexExpression* idx = (IndexExpression*) expr; |
| 197 | if (!this->tryRemoveExpressionBefore(iter, idx->fBase.get())) { |
| 198 | return false; |
| 199 | } |
| 200 | if (!this->tryRemoveExpressionBefore(iter, idx->fIndex.get())) { |
| 201 | return false; |
| 202 | } |
| 203 | *iter = fNodes.erase(*iter); |
| 204 | return true; |
| 205 | } |
| 206 | case Expression::kConstructor_Kind: { |
| 207 | Constructor* c = (Constructor*) expr; |
| 208 | for (auto& arg : c->fArguments) { |
| 209 | if (!this->tryRemoveExpressionBefore(iter, arg.get())) { |
| 210 | return false; |
| 211 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 212 | SkASSERT((*iter)->expression()->get() == expr); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 213 | } |
| 214 | *iter = fNodes.erase(*iter); |
| 215 | return true; |
| 216 | } |
| 217 | case Expression::kFunctionCall_Kind: { |
| 218 | FunctionCall* f = (FunctionCall*) expr; |
| 219 | for (auto& arg : f->fArguments) { |
| 220 | if (!this->tryRemoveExpressionBefore(iter, arg.get())) { |
| 221 | return false; |
| 222 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 223 | SkASSERT((*iter)->expression()->get() == expr); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 224 | } |
| 225 | *iter = fNodes.erase(*iter); |
| 226 | return true; |
| 227 | } |
| 228 | case Expression::kPrefix_Kind: |
| 229 | if (!this->tryRemoveExpressionBefore(iter, |
| 230 | ((PrefixExpression*) expr)->fOperand.get())) { |
| 231 | return false; |
| 232 | } |
| 233 | *iter = fNodes.erase(*iter); |
| 234 | return true; |
| 235 | case Expression::kPostfix_Kind: |
| 236 | if (!this->tryRemoveExpressionBefore(iter, |
| 237 | ((PrefixExpression*) expr)->fOperand.get())) { |
| 238 | return false; |
| 239 | } |
| 240 | *iter = fNodes.erase(*iter); |
| 241 | return true; |
| 242 | case Expression::kBoolLiteral_Kind: // fall through |
| 243 | case Expression::kFloatLiteral_Kind: // fall through |
| 244 | case Expression::kIntLiteral_Kind: // fall through |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 245 | case Expression::kSetting_Kind: // fall through |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 246 | case Expression::kVariableReference_Kind: |
| 247 | *iter = fNodes.erase(*iter); |
| 248 | return true; |
| 249 | default: |
| 250 | ABORT("unhandled expression: %s\n", expr->description().c_str()); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | bool BasicBlock::tryInsertExpression(std::vector<BasicBlock::Node>::iterator* iter, |
| 255 | std::unique_ptr<Expression>* expr) { |
| 256 | switch ((*expr)->fKind) { |
| 257 | case Expression::kBinary_Kind: { |
| 258 | BinaryExpression* b = (BinaryExpression*) expr->get(); |
| 259 | if (!this->tryInsertExpression(iter, &b->fRight)) { |
| 260 | return false; |
| 261 | } |
| 262 | ++(*iter); |
| 263 | if (!this->tryInsertExpression(iter, &b->fLeft)) { |
| 264 | return false; |
| 265 | } |
| 266 | ++(*iter); |
| 267 | BasicBlock::Node node = { BasicBlock::Node::kExpression_Kind, true, expr, nullptr }; |
| 268 | *iter = fNodes.insert(*iter, node); |
| 269 | return true; |
| 270 | } |
| 271 | case Expression::kBoolLiteral_Kind: // fall through |
| 272 | case Expression::kFloatLiteral_Kind: // fall through |
| 273 | case Expression::kIntLiteral_Kind: // fall through |
| 274 | case Expression::kVariableReference_Kind: { |
| 275 | BasicBlock::Node node = { BasicBlock::Node::kExpression_Kind, true, expr, nullptr }; |
| 276 | *iter = fNodes.insert(*iter, node); |
| 277 | return true; |
| 278 | } |
| 279 | case Expression::kConstructor_Kind: { |
| 280 | Constructor* c = (Constructor*) expr->get(); |
| 281 | for (auto& arg : c->fArguments) { |
| 282 | if (!this->tryInsertExpression(iter, &arg)) { |
| 283 | return false; |
| 284 | } |
| 285 | ++(*iter); |
| 286 | } |
| 287 | BasicBlock::Node node = { BasicBlock::Node::kExpression_Kind, true, expr, nullptr }; |
| 288 | *iter = fNodes.insert(*iter, node); |
| 289 | return true; |
| 290 | } |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 291 | case Expression::kSwizzle_Kind: { |
| 292 | Swizzle* s = (Swizzle*) expr->get(); |
| 293 | if (!this->tryInsertExpression(iter, &s->fBase)) { |
| 294 | return false; |
| 295 | } |
| 296 | ++(*iter); |
| 297 | BasicBlock::Node node = { BasicBlock::Node::kExpression_Kind, true, expr, nullptr }; |
| 298 | *iter = fNodes.insert(*iter, node); |
| 299 | return true; |
| 300 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 301 | default: |
| 302 | return false; |
| 303 | } |
| 304 | } |
| 305 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 306 | void CFGGenerator::addExpression(CFG& cfg, std::unique_ptr<Expression>* e, bool constantPropagate) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 307 | SkASSERT(e); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 308 | switch ((*e)->fKind) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 309 | case Expression::kBinary_Kind: { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 310 | BinaryExpression* b = (BinaryExpression*) e->get(); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 311 | switch (b->fOperator) { |
| 312 | case Token::LOGICALAND: // fall through |
| 313 | case Token::LOGICALOR: { |
| 314 | // this isn't as precise as it could be -- we don't bother to track that if we |
| 315 | // early exit from a logical and/or, we know which branch of an 'if' we're going |
| 316 | // to hit -- but it won't make much difference in practice. |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 317 | this->addExpression(cfg, &b->fLeft, constantPropagate); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 318 | BlockId start = cfg.fCurrent; |
| 319 | cfg.newBlock(); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 320 | this->addExpression(cfg, &b->fRight, constantPropagate); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 321 | cfg.newBlock(); |
| 322 | cfg.addExit(start, cfg.fCurrent); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 323 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ |
| 324 | BasicBlock::Node::kExpression_Kind, |
| 325 | constantPropagate, |
| 326 | e, |
| 327 | nullptr |
| 328 | }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 329 | break; |
| 330 | } |
| 331 | case Token::EQ: { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 332 | this->addExpression(cfg, &b->fRight, constantPropagate); |
| 333 | this->addLValue(cfg, &b->fLeft); |
| 334 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ |
| 335 | BasicBlock::Node::kExpression_Kind, |
| 336 | constantPropagate, |
| 337 | e, |
| 338 | nullptr |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 339 | }); |
| 340 | break; |
| 341 | } |
| 342 | default: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 343 | this->addExpression(cfg, &b->fLeft, !Compiler::IsAssignment(b->fOperator)); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 344 | this->addExpression(cfg, &b->fRight, constantPropagate); |
| 345 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ |
| 346 | BasicBlock::Node::kExpression_Kind, |
| 347 | constantPropagate, |
| 348 | e, |
| 349 | nullptr |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 350 | }); |
| 351 | } |
| 352 | break; |
| 353 | } |
| 354 | case Expression::kConstructor_Kind: { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 355 | Constructor* c = (Constructor*) e->get(); |
| 356 | for (auto& arg : c->fArguments) { |
| 357 | this->addExpression(cfg, &arg, constantPropagate); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 358 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 359 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind, |
| 360 | constantPropagate, e, nullptr }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 361 | break; |
| 362 | } |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 363 | case Expression::kExternalFunctionCall_Kind: { |
| 364 | ExternalFunctionCall* c = (ExternalFunctionCall*) e->get(); |
| 365 | for (auto& arg : c->fArguments) { |
| 366 | this->addExpression(cfg, &arg, constantPropagate); |
| 367 | } |
| 368 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind, |
| 369 | constantPropagate, e, nullptr }); |
| 370 | break; |
| 371 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 372 | case Expression::kFunctionCall_Kind: { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 373 | FunctionCall* c = (FunctionCall*) e->get(); |
| 374 | for (auto& arg : c->fArguments) { |
| 375 | this->addExpression(cfg, &arg, constantPropagate); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 376 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 377 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind, |
| 378 | constantPropagate, e, nullptr }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 379 | break; |
| 380 | } |
| 381 | case Expression::kFieldAccess_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 382 | this->addExpression(cfg, &((FieldAccess*) e->get())->fBase, constantPropagate); |
| 383 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind, |
| 384 | constantPropagate, e, nullptr }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 385 | break; |
| 386 | case Expression::kIndex_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 387 | this->addExpression(cfg, &((IndexExpression*) e->get())->fBase, constantPropagate); |
| 388 | this->addExpression(cfg, &((IndexExpression*) e->get())->fIndex, constantPropagate); |
| 389 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind, |
| 390 | constantPropagate, e, nullptr }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 391 | break; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 392 | case Expression::kPrefix_Kind: { |
| 393 | PrefixExpression* p = (PrefixExpression*) e->get(); |
| 394 | this->addExpression(cfg, &p->fOperand, constantPropagate && |
| 395 | p->fOperator != Token::PLUSPLUS && |
| 396 | p->fOperator != Token::MINUSMINUS); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 397 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind, |
| 398 | constantPropagate, e, nullptr }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 399 | break; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 400 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 401 | case Expression::kPostfix_Kind: |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 402 | this->addExpression(cfg, &((PostfixExpression*) e->get())->fOperand, false); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 403 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind, |
| 404 | constantPropagate, e, nullptr }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 405 | break; |
| 406 | case Expression::kSwizzle_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 407 | this->addExpression(cfg, &((Swizzle*) e->get())->fBase, constantPropagate); |
| 408 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind, |
| 409 | constantPropagate, e, nullptr }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 410 | break; |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 411 | case Expression::kAppendStage_Kind: // fall through |
| 412 | case Expression::kBoolLiteral_Kind: // fall through |
| 413 | case Expression::kExternalValue_Kind: // fall through |
| 414 | case Expression::kFloatLiteral_Kind: // fall through |
| 415 | case Expression::kIntLiteral_Kind: // fall through |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 416 | case Expression::kNullLiteral_Kind: // fall through |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 417 | case Expression::kSetting_Kind: // fall through |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 418 | case Expression::kVariableReference_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 419 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind, |
| 420 | constantPropagate, e, nullptr }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 421 | break; |
| 422 | case Expression::kTernary_Kind: { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 423 | TernaryExpression* t = (TernaryExpression*) e->get(); |
| 424 | this->addExpression(cfg, &t->fTest, constantPropagate); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 425 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind, |
| 426 | constantPropagate, e, nullptr }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 427 | BlockId start = cfg.fCurrent; |
| 428 | cfg.newBlock(); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 429 | this->addExpression(cfg, &t->fIfTrue, constantPropagate); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 430 | BlockId next = cfg.newBlock(); |
| 431 | cfg.fCurrent = start; |
| 432 | cfg.newBlock(); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 433 | this->addExpression(cfg, &t->fIfFalse, constantPropagate); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 434 | cfg.addExit(cfg.fCurrent, next); |
| 435 | cfg.fCurrent = next; |
| 436 | break; |
| 437 | } |
| 438 | case Expression::kFunctionReference_Kind: // fall through |
| 439 | case Expression::kTypeReference_Kind: // fall through |
| 440 | case Expression::kDefined_Kind: |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 441 | SkASSERT(false); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 442 | break; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | // adds expressions that are evaluated as part of resolving an lvalue |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 447 | void CFGGenerator::addLValue(CFG& cfg, std::unique_ptr<Expression>* e) { |
| 448 | switch ((*e)->fKind) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 449 | case Expression::kFieldAccess_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 450 | this->addLValue(cfg, &((FieldAccess&) **e).fBase); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 451 | break; |
| 452 | case Expression::kIndex_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 453 | this->addLValue(cfg, &((IndexExpression&) **e).fBase); |
| 454 | this->addExpression(cfg, &((IndexExpression&) **e).fIndex, true); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 455 | break; |
| 456 | case Expression::kSwizzle_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 457 | this->addLValue(cfg, &((Swizzle&) **e).fBase); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 458 | break; |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 459 | case Expression::kExternalValue_Kind: // fall through |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 460 | case Expression::kVariableReference_Kind: |
| 461 | break; |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 462 | case Expression::kTernary_Kind: |
| 463 | this->addExpression(cfg, &((TernaryExpression&) **e).fTest, true); |
| 464 | // Technically we will of course only evaluate one or the other, but if the test turns |
| 465 | // out to be constant, the ternary will get collapsed down to just one branch anyway. So |
| 466 | // it should be ok to pretend that we always evaluate both branches here. |
| 467 | this->addLValue(cfg, &((TernaryExpression&) **e).fIfTrue); |
| 468 | this->addLValue(cfg, &((TernaryExpression&) **e).fIfFalse); |
| 469 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 470 | default: |
| 471 | // not an lvalue, can't happen |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 472 | SkASSERT(false); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 473 | break; |
| 474 | } |
| 475 | } |
| 476 | |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 477 | static bool is_true(Expression& expr) { |
| 478 | return expr.fKind == Expression::kBoolLiteral_Kind && ((BoolLiteral&) expr).fValue; |
| 479 | } |
| 480 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 481 | void CFGGenerator::addStatement(CFG& cfg, std::unique_ptr<Statement>* s) { |
| 482 | switch ((*s)->fKind) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 483 | case Statement::kBlock_Kind: |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 484 | for (auto& child : ((Block&) **s).fStatements) { |
| 485 | addStatement(cfg, &child); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 486 | } |
| 487 | break; |
| 488 | case Statement::kIf_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 489 | IfStatement& ifs = (IfStatement&) **s; |
| 490 | this->addExpression(cfg, &ifs.fTest, true); |
| 491 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false, |
| 492 | nullptr, s }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 493 | BlockId start = cfg.fCurrent; |
| 494 | cfg.newBlock(); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 495 | this->addStatement(cfg, &ifs.fIfTrue); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 496 | BlockId next = cfg.newBlock(); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 497 | if (ifs.fIfFalse) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 498 | cfg.fCurrent = start; |
| 499 | cfg.newBlock(); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 500 | this->addStatement(cfg, &ifs.fIfFalse); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 501 | cfg.addExit(cfg.fCurrent, next); |
| 502 | cfg.fCurrent = next; |
| 503 | } else { |
| 504 | cfg.addExit(start, next); |
| 505 | } |
| 506 | break; |
| 507 | } |
| 508 | case Statement::kExpression_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 509 | this->addExpression(cfg, &((ExpressionStatement&) **s).fExpression, true); |
| 510 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false, |
| 511 | nullptr, s }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 512 | break; |
| 513 | } |
| 514 | case Statement::kVarDeclarations_Kind: { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 515 | VarDeclarationsStatement& decls = ((VarDeclarationsStatement&) **s); |
| 516 | for (auto& stmt : decls.fDeclaration->fVars) { |
| 517 | if (stmt->fKind == Statement::kNop_Kind) { |
| 518 | continue; |
| 519 | } |
| 520 | VarDeclaration& vd = (VarDeclaration&) *stmt; |
| 521 | if (vd.fValue) { |
| 522 | this->addExpression(cfg, &vd.fValue, true); |
| 523 | } |
| 524 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, |
| 525 | false, nullptr, &stmt }); |
| 526 | } |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 527 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false, |
| 528 | nullptr, s }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 529 | break; |
| 530 | } |
| 531 | case Statement::kDiscard_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 532 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false, |
| 533 | nullptr, s }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 534 | cfg.fCurrent = cfg.newIsolatedBlock(); |
| 535 | break; |
| 536 | case Statement::kReturn_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 537 | ReturnStatement& r = ((ReturnStatement&) **s); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 538 | if (r.fExpression) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 539 | this->addExpression(cfg, &r.fExpression, true); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 540 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 541 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false, |
| 542 | nullptr, s }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 543 | cfg.fCurrent = cfg.newIsolatedBlock(); |
| 544 | break; |
| 545 | } |
| 546 | case Statement::kBreak_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 547 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false, |
| 548 | nullptr, s }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 549 | cfg.addExit(cfg.fCurrent, fLoopExits.top()); |
| 550 | cfg.fCurrent = cfg.newIsolatedBlock(); |
| 551 | break; |
| 552 | case Statement::kContinue_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 553 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false, |
| 554 | nullptr, s }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 555 | cfg.addExit(cfg.fCurrent, fLoopContinues.top()); |
| 556 | cfg.fCurrent = cfg.newIsolatedBlock(); |
| 557 | break; |
| 558 | case Statement::kWhile_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 559 | WhileStatement& w = (WhileStatement&) **s; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 560 | BlockId loopStart = cfg.newBlock(); |
| 561 | fLoopContinues.push(loopStart); |
| 562 | BlockId loopExit = cfg.newIsolatedBlock(); |
| 563 | fLoopExits.push(loopExit); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 564 | this->addExpression(cfg, &w.fTest, true); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 565 | BlockId test = cfg.fCurrent; |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 566 | if (!is_true(*w.fTest)) { |
| 567 | cfg.addExit(test, loopExit); |
| 568 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 569 | cfg.newBlock(); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 570 | this->addStatement(cfg, &w.fStatement); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 571 | cfg.addExit(cfg.fCurrent, loopStart); |
| 572 | fLoopContinues.pop(); |
| 573 | fLoopExits.pop(); |
| 574 | cfg.fCurrent = loopExit; |
| 575 | break; |
| 576 | } |
| 577 | case Statement::kDo_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 578 | DoStatement& d = (DoStatement&) **s; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 579 | BlockId loopStart = cfg.newBlock(); |
| 580 | fLoopContinues.push(loopStart); |
| 581 | BlockId loopExit = cfg.newIsolatedBlock(); |
| 582 | fLoopExits.push(loopExit); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 583 | this->addStatement(cfg, &d.fStatement); |
| 584 | this->addExpression(cfg, &d.fTest, true); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 585 | cfg.addExit(cfg.fCurrent, loopExit); |
| 586 | cfg.addExit(cfg.fCurrent, loopStart); |
| 587 | fLoopContinues.pop(); |
| 588 | fLoopExits.pop(); |
| 589 | cfg.fCurrent = loopExit; |
| 590 | break; |
| 591 | } |
| 592 | case Statement::kFor_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 593 | ForStatement& f = (ForStatement&) **s; |
| 594 | if (f.fInitializer) { |
| 595 | this->addStatement(cfg, &f.fInitializer); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 596 | } |
| 597 | BlockId loopStart = cfg.newBlock(); |
| 598 | BlockId next = cfg.newIsolatedBlock(); |
| 599 | fLoopContinues.push(next); |
| 600 | BlockId loopExit = cfg.newIsolatedBlock(); |
| 601 | fLoopExits.push(loopExit); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 602 | if (f.fTest) { |
| 603 | this->addExpression(cfg, &f.fTest, true); |
Ethan Nicholas | d9fe700 | 2017-05-30 10:15:34 -0400 | [diff] [blame] | 604 | // this isn't quite right; we should have an exit from here to the loop exit, and |
| 605 | // remove the exit from the loop body to the loop exit. Structuring it like this |
| 606 | // forces the optimizer to believe that the loop body is always executed at least |
| 607 | // once. While not strictly correct, this avoids incorrect "variable not assigned" |
| 608 | // errors on variables which are assigned within the loop. The correct solution to |
| 609 | // this is to analyze the loop to see whether or not at least one iteration is |
| 610 | // guaranteed to happen, but for the time being we take the easy way out. |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 611 | } |
| 612 | cfg.newBlock(); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 613 | this->addStatement(cfg, &f.fStatement); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 614 | cfg.addExit(cfg.fCurrent, next); |
| 615 | cfg.fCurrent = next; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 616 | if (f.fNext) { |
| 617 | this->addExpression(cfg, &f.fNext, true); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 618 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 619 | cfg.addExit(cfg.fCurrent, loopStart); |
Ethan Nicholas | d9fe700 | 2017-05-30 10:15:34 -0400 | [diff] [blame] | 620 | cfg.addExit(cfg.fCurrent, loopExit); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 621 | fLoopContinues.pop(); |
| 622 | fLoopExits.pop(); |
| 623 | cfg.fCurrent = loopExit; |
| 624 | break; |
| 625 | } |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 626 | case Statement::kSwitch_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 627 | SwitchStatement& ss = (SwitchStatement&) **s; |
| 628 | this->addExpression(cfg, &ss.fValue, true); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 629 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false, |
| 630 | nullptr, s }); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 631 | BlockId start = cfg.fCurrent; |
| 632 | BlockId switchExit = cfg.newIsolatedBlock(); |
| 633 | fLoopExits.push(switchExit); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 634 | for (const auto& c : ss.fCases) { |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 635 | cfg.newBlock(); |
| 636 | cfg.addExit(start, cfg.fCurrent); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 637 | if (c->fValue) { |
| 638 | // technically this should go in the start block, but it doesn't actually matter |
| 639 | // because it must be constant. Not worth running two loops for. |
| 640 | this->addExpression(cfg, &c->fValue, true); |
| 641 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 642 | for (auto& caseStatement : c->fStatements) { |
| 643 | this->addStatement(cfg, &caseStatement); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | cfg.addExit(cfg.fCurrent, switchExit); |
| 647 | // note that unlike GLSL, our grammar requires the default case to be last |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 648 | if (0 == ss.fCases.size() || ss.fCases[ss.fCases.size() - 1]->fValue) { |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 649 | // switch does not have a default clause, mark that it can skip straight to the end |
| 650 | cfg.addExit(start, switchExit); |
| 651 | } |
| 652 | fLoopExits.pop(); |
| 653 | cfg.fCurrent = switchExit; |
| 654 | break; |
| 655 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 656 | case Statement::kNop_Kind: |
| 657 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 658 | default: |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 659 | printf("statement: %s\n", (*s)->description().c_str()); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 660 | ABORT("unsupported statement kind"); |
| 661 | } |
| 662 | } |
| 663 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 664 | CFG CFGGenerator::getCFG(FunctionDefinition& f) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 665 | CFG result; |
| 666 | result.fStart = result.newBlock(); |
| 667 | result.fCurrent = result.fStart; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 668 | this->addStatement(result, &f.fBody); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 669 | result.newBlock(); |
| 670 | result.fExit = result.fCurrent; |
| 671 | return result; |
| 672 | } |
| 673 | |
| 674 | } // namespace |