|
| 1 | +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:kernel/ast.dart'; |
| 6 | + |
| 7 | +/// Simple unreachable code elimination: removes asserts and if statements |
| 8 | +/// with constant conditions. Does a very limited constant folding of |
| 9 | +/// logical expressions. |
| 10 | +Component transformComponent(Component component, bool enableAsserts) { |
| 11 | + new SimpleUnreachableCodeElimination(enableAsserts).visitComponent(component); |
| 12 | + return component; |
| 13 | +} |
| 14 | + |
| 15 | +class SimpleUnreachableCodeElimination extends Transformer { |
| 16 | + final bool enableAsserts; |
| 17 | + |
| 18 | + SimpleUnreachableCodeElimination(this.enableAsserts); |
| 19 | + |
| 20 | + bool _isBoolConstant(Expression node) => |
| 21 | + node is BoolLiteral || |
| 22 | + (node is ConstantExpression && node.constant is BoolConstant); |
| 23 | + |
| 24 | + bool _getBoolConstantValue(Expression node) { |
| 25 | + if (node is BoolLiteral) { |
| 26 | + return node.value; |
| 27 | + } |
| 28 | + if (node is ConstantExpression) { |
| 29 | + final constant = node.constant; |
| 30 | + if (constant is BoolConstant) { |
| 31 | + return constant.value; |
| 32 | + } |
| 33 | + } |
| 34 | + throw 'Expected bool constant: $node'; |
| 35 | + } |
| 36 | + |
| 37 | + Expression _createBoolLiteral(bool value, int fileOffset) => |
| 38 | + new BoolLiteral(value)..fileOffset = fileOffset; |
| 39 | + |
| 40 | + Statement _makeEmptyBlockIfNull(Statement node) => |
| 41 | + node == null ? Block(<Statement>[]) : node; |
| 42 | + |
| 43 | + @override |
| 44 | + TreeNode visitIfStatement(IfStatement node) { |
| 45 | + node.transformChildren(this); |
| 46 | + final condition = node.condition; |
| 47 | + if (_isBoolConstant(condition)) { |
| 48 | + final value = _getBoolConstantValue(condition); |
| 49 | + return value ? node.then : node.otherwise; |
| 50 | + } |
| 51 | + node.then = _makeEmptyBlockIfNull(node.then); |
| 52 | + return node; |
| 53 | + } |
| 54 | + |
| 55 | + @override |
| 56 | + visitConditionalExpression(ConditionalExpression node) { |
| 57 | + node.transformChildren(this); |
| 58 | + final condition = node.condition; |
| 59 | + if (_isBoolConstant(condition)) { |
| 60 | + final value = _getBoolConstantValue(condition); |
| 61 | + return value ? node.then : node.otherwise; |
| 62 | + } |
| 63 | + return node; |
| 64 | + } |
| 65 | + |
| 66 | + @override |
| 67 | + TreeNode visitNot(Not node) { |
| 68 | + node.transformChildren(this); |
| 69 | + final operand = node.operand; |
| 70 | + if (_isBoolConstant(operand)) { |
| 71 | + return _createBoolLiteral( |
| 72 | + !_getBoolConstantValue(operand), node.fileOffset); |
| 73 | + } |
| 74 | + return node; |
| 75 | + } |
| 76 | + |
| 77 | + @override |
| 78 | + TreeNode visitLogicalExpression(LogicalExpression node) { |
| 79 | + node.transformChildren(this); |
| 80 | + final left = node.left; |
| 81 | + final right = node.right; |
| 82 | + final operator = node.operator; |
| 83 | + if (_isBoolConstant(left)) { |
| 84 | + final leftValue = _getBoolConstantValue(left); |
| 85 | + if (_isBoolConstant(right)) { |
| 86 | + final rightValue = _getBoolConstantValue(right); |
| 87 | + if (operator == '||') { |
| 88 | + return _createBoolLiteral(leftValue || rightValue, node.fileOffset); |
| 89 | + } else if (operator == '&&') { |
| 90 | + return _createBoolLiteral(leftValue && rightValue, node.fileOffset); |
| 91 | + } else { |
| 92 | + throw 'Unexpected LogicalExpression operator ${operator}: $node'; |
| 93 | + } |
| 94 | + } else { |
| 95 | + if (leftValue && operator == '||') { |
| 96 | + return _createBoolLiteral(true, node.fileOffset); |
| 97 | + } else if (!leftValue && operator == '&&') { |
| 98 | + return _createBoolLiteral(false, node.fileOffset); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + return node; |
| 103 | + } |
| 104 | + |
| 105 | + @override |
| 106 | + visitStaticGet(StaticGet node) { |
| 107 | + node.transformChildren(this); |
| 108 | + final target = node.target; |
| 109 | + if (target is Field && target.isConst) { |
| 110 | + throw 'StaticGet from const field $target should be evaluated by front-end: $node'; |
| 111 | + } |
| 112 | + return node; |
| 113 | + } |
| 114 | + |
| 115 | + @override |
| 116 | + TreeNode visitAssertStatement(AssertStatement node) { |
| 117 | + if (!enableAsserts) { |
| 118 | + return null; |
| 119 | + } |
| 120 | + return super.visitAssertStatement(node); |
| 121 | + } |
| 122 | + |
| 123 | + @override |
| 124 | + TreeNode visitAssertBlock(AssertBlock node) { |
| 125 | + if (!enableAsserts) { |
| 126 | + return null; |
| 127 | + } |
| 128 | + return super.visitAssertBlock(node); |
| 129 | + } |
| 130 | + |
| 131 | + @override |
| 132 | + TreeNode visitAssertInitializer(AssertInitializer node) { |
| 133 | + if (!enableAsserts) { |
| 134 | + return null; |
| 135 | + } |
| 136 | + return super.visitAssertInitializer(node); |
| 137 | + } |
| 138 | + |
| 139 | + // Make sure we're not generating `null` bodies. |
| 140 | + // Try/catch, try/finally and switch/case statements |
| 141 | + // always have a Block in a body, so there is no |
| 142 | + // need to guard against null. |
| 143 | + |
| 144 | + @override |
| 145 | + TreeNode visitWhileStatement(WhileStatement node) { |
| 146 | + node.transformChildren(this); |
| 147 | + node.body = _makeEmptyBlockIfNull(node.body); |
| 148 | + return node; |
| 149 | + } |
| 150 | + |
| 151 | + @override |
| 152 | + TreeNode visitDoStatement(DoStatement node) { |
| 153 | + node.transformChildren(this); |
| 154 | + node.body = _makeEmptyBlockIfNull(node.body); |
| 155 | + return node; |
| 156 | + } |
| 157 | + |
| 158 | + @override |
| 159 | + TreeNode visitForStatement(ForStatement node) { |
| 160 | + node.transformChildren(this); |
| 161 | + node.body = _makeEmptyBlockIfNull(node.body); |
| 162 | + return node; |
| 163 | + } |
| 164 | + |
| 165 | + @override |
| 166 | + TreeNode visitForInStatement(ForInStatement node) { |
| 167 | + node.transformChildren(this); |
| 168 | + node.body = _makeEmptyBlockIfNull(node.body); |
| 169 | + return node; |
| 170 | + } |
| 171 | +} |
0 commit comments