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 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 8 | #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 Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 23 | #include "ir/SkSLSwitchStatement.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 24 | #include "ir/SkSLTernaryExpression.h" |
| 25 | #include "ir/SkSLVarDeclarationsStatement.h" |
| 26 | #include "ir/SkSLWhileStatement.h" |
| 27 | |
| 28 | namespace SkSL { |
| 29 | |
| 30 | BlockId 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 | |
| 40 | BlockId CFG::newIsolatedBlock() { |
| 41 | BlockId result = fBlocks.size(); |
| 42 | fBlocks.emplace_back(); |
| 43 | return result; |
| 44 | } |
| 45 | |
| 46 | void 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 | |
| 53 | void 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 Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 58 | printf("%s%s = %s", separator, iter->first->description().c_str(), |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 59 | iter->second ? (*iter->second)->description().c_str() : "<undefined>"); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 60 | 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 Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 70 | BasicBlock::Node& n = fBlocks[i].fNodes[j]; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 71 | 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()); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 74 | } |
| 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 Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 85 | bool 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) { |
| 92 | ASSERT((*iter)->expression()->get() != e); |
| 93 | Expression* old = (*iter)->expression()->get(); |
| 94 | do { |
| 95 | if ((*iter) == fNodes.begin()) { |
| 96 | ABORT("couldn't find %s before %s\n", e->description().c_str(), |
| 97 | old->description().c_str()); |
| 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) { |
| 105 | ASSERT(*iter != fNodes.end()); |
| 106 | ++(*iter); |
| 107 | } |
| 108 | } else { |
| 109 | Statement* old = (*iter)->statement()->get(); |
| 110 | do { |
| 111 | if ((*iter) == fNodes.begin()) { |
| 112 | ABORT("couldn't find %s before %s\n", e->description().c_str(), |
| 113 | old->description().c_str()); |
| 114 | } |
| 115 | --(*iter); |
| 116 | } while ((*iter)->fKind != BasicBlock::Node::kExpression_Kind || |
| 117 | (*iter)->expression()->get() != e); |
| 118 | result = this->tryRemoveExpression(iter); |
| 119 | while ((*iter)->fKind != BasicBlock::Node::kStatement_Kind || |
| 120 | (*iter)->statement()->get() != old) { |
| 121 | ASSERT(*iter != fNodes.end()); |
| 122 | ++(*iter); |
| 123 | } |
| 124 | } |
| 125 | return result; |
| 126 | } |
| 127 | |
| 128 | bool BasicBlock::tryRemoveLValueBefore(std::vector<BasicBlock::Node>::iterator* iter, |
| 129 | Expression* lvalue) { |
| 130 | switch (lvalue->fKind) { |
| 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()); |
| 142 | default: |
| 143 | ABORT("invalid lvalue: %s\n", lvalue->description().c_str()); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | bool BasicBlock::tryRemoveExpression(std::vector<BasicBlock::Node>::iterator* iter) { |
| 148 | Expression* expr = (*iter)->expression()->get(); |
| 149 | switch (expr->fKind) { |
| 150 | case Expression::kBinary_Kind: { |
| 151 | BinaryExpression* b = (BinaryExpression*) expr; |
| 152 | if (b->fOperator == Token::EQ) { |
| 153 | if (!this->tryRemoveLValueBefore(iter, b->fLeft.get())) { |
| 154 | return false; |
| 155 | } |
| 156 | } else if (!this->tryRemoveExpressionBefore(iter, b->fLeft.get())) { |
| 157 | return false; |
| 158 | } |
| 159 | if (!this->tryRemoveExpressionBefore(iter, b->fRight.get())) { |
| 160 | return false; |
| 161 | } |
| 162 | ASSERT((*iter)->expression()->get() == expr); |
| 163 | *iter = fNodes.erase(*iter); |
| 164 | return true; |
| 165 | } |
| 166 | case Expression::kTernary_Kind: { |
| 167 | // ternaries cross basic block boundaries, must regenerate the CFG to remove it |
| 168 | return false; |
| 169 | } |
| 170 | case Expression::kFieldAccess_Kind: { |
| 171 | FieldAccess* f = (FieldAccess*) expr; |
| 172 | if (!this->tryRemoveExpressionBefore(iter, f->fBase.get())) { |
| 173 | return false; |
| 174 | } |
| 175 | *iter = fNodes.erase(*iter); |
| 176 | return true; |
| 177 | } |
| 178 | case Expression::kSwizzle_Kind: { |
| 179 | Swizzle* s = (Swizzle*) expr; |
| 180 | if (!this->tryRemoveExpressionBefore(iter, s->fBase.get())) { |
| 181 | return false; |
| 182 | } |
| 183 | *iter = fNodes.erase(*iter); |
| 184 | return true; |
| 185 | } |
| 186 | case Expression::kIndex_Kind: { |
| 187 | IndexExpression* idx = (IndexExpression*) expr; |
| 188 | if (!this->tryRemoveExpressionBefore(iter, idx->fBase.get())) { |
| 189 | return false; |
| 190 | } |
| 191 | if (!this->tryRemoveExpressionBefore(iter, idx->fIndex.get())) { |
| 192 | return false; |
| 193 | } |
| 194 | *iter = fNodes.erase(*iter); |
| 195 | return true; |
| 196 | } |
| 197 | case Expression::kConstructor_Kind: { |
| 198 | Constructor* c = (Constructor*) expr; |
| 199 | for (auto& arg : c->fArguments) { |
| 200 | if (!this->tryRemoveExpressionBefore(iter, arg.get())) { |
| 201 | return false; |
| 202 | } |
| 203 | ASSERT((*iter)->expression()->get() == expr); |
| 204 | } |
| 205 | *iter = fNodes.erase(*iter); |
| 206 | return true; |
| 207 | } |
| 208 | case Expression::kFunctionCall_Kind: { |
| 209 | FunctionCall* f = (FunctionCall*) expr; |
| 210 | for (auto& arg : f->fArguments) { |
| 211 | if (!this->tryRemoveExpressionBefore(iter, arg.get())) { |
| 212 | return false; |
| 213 | } |
| 214 | ASSERT((*iter)->expression()->get() == expr); |
| 215 | } |
| 216 | *iter = fNodes.erase(*iter); |
| 217 | return true; |
| 218 | } |
| 219 | case Expression::kPrefix_Kind: |
| 220 | if (!this->tryRemoveExpressionBefore(iter, |
| 221 | ((PrefixExpression*) expr)->fOperand.get())) { |
| 222 | return false; |
| 223 | } |
| 224 | *iter = fNodes.erase(*iter); |
| 225 | return true; |
| 226 | case Expression::kPostfix_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::kBoolLiteral_Kind: // fall through |
| 234 | case Expression::kFloatLiteral_Kind: // fall through |
| 235 | case Expression::kIntLiteral_Kind: // fall through |
| 236 | case Expression::kVariableReference_Kind: |
| 237 | *iter = fNodes.erase(*iter); |
| 238 | return true; |
| 239 | default: |
| 240 | ABORT("unhandled expression: %s\n", expr->description().c_str()); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | bool BasicBlock::tryInsertExpression(std::vector<BasicBlock::Node>::iterator* iter, |
| 245 | std::unique_ptr<Expression>* expr) { |
| 246 | switch ((*expr)->fKind) { |
| 247 | case Expression::kBinary_Kind: { |
| 248 | BinaryExpression* b = (BinaryExpression*) expr->get(); |
| 249 | if (!this->tryInsertExpression(iter, &b->fRight)) { |
| 250 | return false; |
| 251 | } |
| 252 | ++(*iter); |
| 253 | if (!this->tryInsertExpression(iter, &b->fLeft)) { |
| 254 | return false; |
| 255 | } |
| 256 | ++(*iter); |
| 257 | BasicBlock::Node node = { BasicBlock::Node::kExpression_Kind, true, expr, nullptr }; |
| 258 | *iter = fNodes.insert(*iter, node); |
| 259 | return true; |
| 260 | } |
| 261 | case Expression::kBoolLiteral_Kind: // fall through |
| 262 | case Expression::kFloatLiteral_Kind: // fall through |
| 263 | case Expression::kIntLiteral_Kind: // fall through |
| 264 | case Expression::kVariableReference_Kind: { |
| 265 | BasicBlock::Node node = { BasicBlock::Node::kExpression_Kind, true, expr, nullptr }; |
| 266 | *iter = fNodes.insert(*iter, node); |
| 267 | return true; |
| 268 | } |
| 269 | case Expression::kConstructor_Kind: { |
| 270 | Constructor* c = (Constructor*) expr->get(); |
| 271 | for (auto& arg : c->fArguments) { |
| 272 | if (!this->tryInsertExpression(iter, &arg)) { |
| 273 | return false; |
| 274 | } |
| 275 | ++(*iter); |
| 276 | } |
| 277 | BasicBlock::Node node = { BasicBlock::Node::kExpression_Kind, true, expr, nullptr }; |
| 278 | *iter = fNodes.insert(*iter, node); |
| 279 | return true; |
| 280 | } |
| 281 | default: |
| 282 | return false; |
| 283 | } |
| 284 | } |
| 285 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 286 | void CFGGenerator::addExpression(CFG& cfg, std::unique_ptr<Expression>* e, bool constantPropagate) { |
| 287 | ASSERT(e); |
| 288 | switch ((*e)->fKind) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 289 | case Expression::kBinary_Kind: { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 290 | BinaryExpression* b = (BinaryExpression*) e->get(); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 291 | switch (b->fOperator) { |
| 292 | case Token::LOGICALAND: // fall through |
| 293 | case Token::LOGICALOR: { |
| 294 | // this isn't as precise as it could be -- we don't bother to track that if we |
| 295 | // early exit from a logical and/or, we know which branch of an 'if' we're going |
| 296 | // to hit -- but it won't make much difference in practice. |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 297 | this->addExpression(cfg, &b->fLeft, constantPropagate); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 298 | BlockId start = cfg.fCurrent; |
| 299 | cfg.newBlock(); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 300 | this->addExpression(cfg, &b->fRight, constantPropagate); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 301 | cfg.newBlock(); |
| 302 | cfg.addExit(start, cfg.fCurrent); |
| 303 | break; |
| 304 | } |
| 305 | case Token::EQ: { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 306 | this->addExpression(cfg, &b->fRight, constantPropagate); |
| 307 | this->addLValue(cfg, &b->fLeft); |
| 308 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ |
| 309 | BasicBlock::Node::kExpression_Kind, |
| 310 | constantPropagate, |
| 311 | e, |
| 312 | nullptr |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 313 | }); |
| 314 | break; |
| 315 | } |
| 316 | default: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 317 | this->addExpression(cfg, &b->fLeft, !Token::IsAssignment(b->fOperator)); |
| 318 | this->addExpression(cfg, &b->fRight, constantPropagate); |
| 319 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ |
| 320 | BasicBlock::Node::kExpression_Kind, |
| 321 | constantPropagate, |
| 322 | e, |
| 323 | nullptr |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 324 | }); |
| 325 | } |
| 326 | break; |
| 327 | } |
| 328 | case Expression::kConstructor_Kind: { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 329 | Constructor* c = (Constructor*) e->get(); |
| 330 | for (auto& arg : c->fArguments) { |
| 331 | this->addExpression(cfg, &arg, constantPropagate); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 332 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 333 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind, |
| 334 | constantPropagate, e, nullptr }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 335 | break; |
| 336 | } |
| 337 | case Expression::kFunctionCall_Kind: { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 338 | FunctionCall* c = (FunctionCall*) e->get(); |
| 339 | for (auto& arg : c->fArguments) { |
| 340 | this->addExpression(cfg, &arg, constantPropagate); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 341 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 342 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind, |
| 343 | constantPropagate, e, nullptr }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 344 | break; |
| 345 | } |
| 346 | case Expression::kFieldAccess_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 347 | this->addExpression(cfg, &((FieldAccess*) e->get())->fBase, constantPropagate); |
| 348 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind, |
| 349 | constantPropagate, e, nullptr }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 350 | break; |
| 351 | case Expression::kIndex_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 352 | this->addExpression(cfg, &((IndexExpression*) e->get())->fBase, constantPropagate); |
| 353 | this->addExpression(cfg, &((IndexExpression*) e->get())->fIndex, constantPropagate); |
| 354 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind, |
| 355 | constantPropagate, e, nullptr }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 356 | break; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 357 | case Expression::kPrefix_Kind: { |
| 358 | PrefixExpression* p = (PrefixExpression*) e->get(); |
| 359 | this->addExpression(cfg, &p->fOperand, constantPropagate && |
| 360 | p->fOperator != Token::PLUSPLUS && |
| 361 | p->fOperator != Token::MINUSMINUS); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 362 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind, |
| 363 | constantPropagate, e, nullptr }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 364 | break; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 365 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 366 | case Expression::kPostfix_Kind: |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 367 | this->addExpression(cfg, &((PostfixExpression*) e->get())->fOperand, false); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 368 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind, |
| 369 | constantPropagate, e, nullptr }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 370 | break; |
| 371 | case Expression::kSwizzle_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 372 | this->addExpression(cfg, &((Swizzle*) e->get())->fBase, constantPropagate); |
| 373 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind, |
| 374 | constantPropagate, e, nullptr }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 375 | break; |
| 376 | case Expression::kBoolLiteral_Kind: // fall through |
| 377 | case Expression::kFloatLiteral_Kind: // fall through |
| 378 | case Expression::kIntLiteral_Kind: // fall through |
| 379 | case Expression::kVariableReference_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 380 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind, |
| 381 | constantPropagate, e, nullptr }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 382 | break; |
| 383 | case Expression::kTernary_Kind: { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 384 | TernaryExpression* t = (TernaryExpression*) e->get(); |
| 385 | this->addExpression(cfg, &t->fTest, constantPropagate); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 386 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kExpression_Kind, |
| 387 | constantPropagate, e, nullptr }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 388 | BlockId start = cfg.fCurrent; |
| 389 | cfg.newBlock(); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 390 | this->addExpression(cfg, &t->fIfTrue, constantPropagate); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 391 | BlockId next = cfg.newBlock(); |
| 392 | cfg.fCurrent = start; |
| 393 | cfg.newBlock(); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 394 | this->addExpression(cfg, &t->fIfFalse, constantPropagate); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 395 | cfg.addExit(cfg.fCurrent, next); |
| 396 | cfg.fCurrent = next; |
| 397 | break; |
| 398 | } |
| 399 | case Expression::kFunctionReference_Kind: // fall through |
| 400 | case Expression::kTypeReference_Kind: // fall through |
| 401 | case Expression::kDefined_Kind: |
| 402 | ASSERT(false); |
| 403 | break; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | // adds expressions that are evaluated as part of resolving an lvalue |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 408 | void CFGGenerator::addLValue(CFG& cfg, std::unique_ptr<Expression>* e) { |
| 409 | switch ((*e)->fKind) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 410 | case Expression::kFieldAccess_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 411 | this->addLValue(cfg, &((FieldAccess&) **e).fBase); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 412 | break; |
| 413 | case Expression::kIndex_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 414 | this->addLValue(cfg, &((IndexExpression&) **e).fBase); |
| 415 | this->addExpression(cfg, &((IndexExpression&) **e).fIndex, true); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 416 | break; |
| 417 | case Expression::kSwizzle_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 418 | this->addLValue(cfg, &((Swizzle&) **e).fBase); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 419 | break; |
| 420 | case Expression::kVariableReference_Kind: |
| 421 | break; |
| 422 | default: |
| 423 | // not an lvalue, can't happen |
| 424 | ASSERT(false); |
| 425 | break; |
| 426 | } |
| 427 | } |
| 428 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 429 | void CFGGenerator::addStatement(CFG& cfg, std::unique_ptr<Statement>* s) { |
| 430 | switch ((*s)->fKind) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 431 | case Statement::kBlock_Kind: |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 432 | for (auto& child : ((Block&) **s).fStatements) { |
| 433 | addStatement(cfg, &child); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 434 | } |
| 435 | break; |
| 436 | case Statement::kIf_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 437 | IfStatement& ifs = (IfStatement&) **s; |
| 438 | this->addExpression(cfg, &ifs.fTest, true); |
| 439 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false, |
| 440 | nullptr, s }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 441 | BlockId start = cfg.fCurrent; |
| 442 | cfg.newBlock(); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 443 | this->addStatement(cfg, &ifs.fIfTrue); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 444 | BlockId next = cfg.newBlock(); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 445 | if (ifs.fIfFalse) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 446 | cfg.fCurrent = start; |
| 447 | cfg.newBlock(); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 448 | this->addStatement(cfg, &ifs.fIfFalse); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 449 | cfg.addExit(cfg.fCurrent, next); |
| 450 | cfg.fCurrent = next; |
| 451 | } else { |
| 452 | cfg.addExit(start, next); |
| 453 | } |
| 454 | break; |
| 455 | } |
| 456 | case Statement::kExpression_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 457 | this->addExpression(cfg, &((ExpressionStatement&) **s).fExpression, true); |
| 458 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false, |
| 459 | nullptr, s }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 460 | break; |
| 461 | } |
| 462 | case Statement::kVarDeclarations_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 463 | VarDeclarationsStatement& decls = ((VarDeclarationsStatement&) **s); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 464 | for (auto& vd : decls.fDeclaration->fVars) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 465 | if (vd->fValue) { |
| 466 | this->addExpression(cfg, &vd->fValue, true); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 467 | } |
| 468 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 469 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false, |
| 470 | nullptr, s }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 471 | break; |
| 472 | } |
| 473 | case Statement::kDiscard_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 474 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false, |
| 475 | nullptr, s }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 476 | cfg.fCurrent = cfg.newIsolatedBlock(); |
| 477 | break; |
| 478 | case Statement::kReturn_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 479 | ReturnStatement& r = ((ReturnStatement&) **s); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 480 | if (r.fExpression) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 481 | this->addExpression(cfg, &r.fExpression, true); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 482 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 483 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false, |
| 484 | nullptr, s }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 485 | cfg.fCurrent = cfg.newIsolatedBlock(); |
| 486 | break; |
| 487 | } |
| 488 | case Statement::kBreak_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 489 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false, |
| 490 | nullptr, s }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 491 | cfg.addExit(cfg.fCurrent, fLoopExits.top()); |
| 492 | cfg.fCurrent = cfg.newIsolatedBlock(); |
| 493 | break; |
| 494 | case Statement::kContinue_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 495 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false, |
| 496 | nullptr, s }); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 497 | cfg.addExit(cfg.fCurrent, fLoopContinues.top()); |
| 498 | cfg.fCurrent = cfg.newIsolatedBlock(); |
| 499 | break; |
| 500 | case Statement::kWhile_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 501 | WhileStatement& w = (WhileStatement&) **s; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 502 | BlockId loopStart = cfg.newBlock(); |
| 503 | fLoopContinues.push(loopStart); |
| 504 | BlockId loopExit = cfg.newIsolatedBlock(); |
| 505 | fLoopExits.push(loopExit); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 506 | this->addExpression(cfg, &w.fTest, true); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 507 | BlockId test = cfg.fCurrent; |
| 508 | cfg.addExit(test, loopExit); |
| 509 | cfg.newBlock(); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 510 | this->addStatement(cfg, &w.fStatement); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 511 | cfg.addExit(cfg.fCurrent, loopStart); |
| 512 | fLoopContinues.pop(); |
| 513 | fLoopExits.pop(); |
| 514 | cfg.fCurrent = loopExit; |
| 515 | break; |
| 516 | } |
| 517 | case Statement::kDo_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 518 | DoStatement& d = (DoStatement&) **s; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 519 | BlockId loopStart = cfg.newBlock(); |
| 520 | fLoopContinues.push(loopStart); |
| 521 | BlockId loopExit = cfg.newIsolatedBlock(); |
| 522 | fLoopExits.push(loopExit); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 523 | this->addStatement(cfg, &d.fStatement); |
| 524 | this->addExpression(cfg, &d.fTest, true); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 525 | cfg.addExit(cfg.fCurrent, loopExit); |
| 526 | cfg.addExit(cfg.fCurrent, loopStart); |
| 527 | fLoopContinues.pop(); |
| 528 | fLoopExits.pop(); |
| 529 | cfg.fCurrent = loopExit; |
| 530 | break; |
| 531 | } |
| 532 | case Statement::kFor_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 533 | ForStatement& f = (ForStatement&) **s; |
| 534 | if (f.fInitializer) { |
| 535 | this->addStatement(cfg, &f.fInitializer); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 536 | } |
| 537 | BlockId loopStart = cfg.newBlock(); |
| 538 | BlockId next = cfg.newIsolatedBlock(); |
| 539 | fLoopContinues.push(next); |
| 540 | BlockId loopExit = cfg.newIsolatedBlock(); |
| 541 | fLoopExits.push(loopExit); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 542 | if (f.fTest) { |
| 543 | this->addExpression(cfg, &f.fTest, true); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 544 | BlockId test = cfg.fCurrent; |
| 545 | cfg.addExit(test, loopExit); |
| 546 | } |
| 547 | cfg.newBlock(); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 548 | this->addStatement(cfg, &f.fStatement); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 549 | cfg.addExit(cfg.fCurrent, next); |
| 550 | cfg.fCurrent = next; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 551 | if (f.fNext) { |
| 552 | this->addExpression(cfg, &f.fNext, true); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 553 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 554 | cfg.addExit(cfg.fCurrent, loopStart); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 555 | fLoopContinues.pop(); |
| 556 | fLoopExits.pop(); |
| 557 | cfg.fCurrent = loopExit; |
| 558 | break; |
| 559 | } |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 560 | case Statement::kSwitch_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 561 | SwitchStatement& ss = (SwitchStatement&) **s; |
| 562 | this->addExpression(cfg, &ss.fValue, true); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 563 | cfg.fBlocks[cfg.fCurrent].fNodes.push_back({ BasicBlock::Node::kStatement_Kind, false, |
| 564 | nullptr, s }); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 565 | BlockId start = cfg.fCurrent; |
| 566 | BlockId switchExit = cfg.newIsolatedBlock(); |
| 567 | fLoopExits.push(switchExit); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 568 | for (const auto& c : ss.fCases) { |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 569 | cfg.newBlock(); |
| 570 | cfg.addExit(start, cfg.fCurrent); |
| 571 | if (c->fValue) { |
| 572 | // technically this should go in the start block, but it doesn't actually matter |
| 573 | // because it must be constant. Not worth running two loops for. |
| 574 | this->addExpression(cfg, &c->fValue, true); |
| 575 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 576 | for (auto& caseStatement : c->fStatements) { |
| 577 | this->addStatement(cfg, &caseStatement); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | cfg.addExit(cfg.fCurrent, switchExit); |
| 581 | // 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] | 582 | if (0 == ss.fCases.size() || ss.fCases[ss.fCases.size() - 1]->fValue) { |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 583 | // switch does not have a default clause, mark that it can skip straight to the end |
| 584 | cfg.addExit(start, switchExit); |
| 585 | } |
| 586 | fLoopExits.pop(); |
| 587 | cfg.fCurrent = switchExit; |
| 588 | break; |
| 589 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 590 | case Statement::kNop_Kind: |
| 591 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 592 | default: |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 593 | printf("statement: %s\n", (*s)->description().c_str()); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 594 | ABORT("unsupported statement kind"); |
| 595 | } |
| 596 | } |
| 597 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 598 | CFG CFGGenerator::getCFG(FunctionDefinition& f) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 599 | CFG result; |
| 600 | result.fStart = result.newBlock(); |
| 601 | result.fCurrent = result.fStart; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 602 | this->addStatement(result, &f.fBody); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 603 | result.newBlock(); |
| 604 | result.fExit = result.fCurrent; |
| 605 | return result; |
| 606 | } |
| 607 | |
| 608 | } // namespace |