Skip to content

Commit 73c0495

Browse files
authored
Use the type system to check if something is flowed out of (#1224)
now that the type system has a proper unreachable, we don't need obviouslyDoesNotFlowOut
1 parent faaa20b commit 73c0495

4 files changed

Lines changed: 53 additions & 27 deletions

File tree

src/ast_utils.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,6 @@ struct ExpressionAnalyzer {
5555
return !curr->condition && !curr->value;
5656
}
5757

58-
// Checks if an expression does not flow out in an obvious way.
59-
// We return true if it cannot flow out. If it can flow out, we
60-
// might still return true, as the analysis here is simple and fast.
61-
static bool obviouslyDoesNotFlowOut(Expression* curr) {
62-
if (auto* br = curr->dynCast<Break>()) {
63-
if (!br->condition) return true;
64-
} else if (auto* block = curr->dynCast<Block>()) {
65-
if (block->list.size() > 0 && obviouslyDoesNotFlowOut(block->list.back()) && !BranchUtils::BranchSeeker::hasReachable(block, block->name)) return true;
66-
}
67-
return false;
68-
}
69-
7058
using ExprComparer = std::function<bool(Expression*, Expression*)>;
7159
static bool flexibleEqual(Expression* left, Expression* right, ExprComparer comparer);
7260

src/passes/RemoveUnusedBrs.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
250250
// let's try to move the code going to the top of the loop into the if-else
251251
if (!iff->ifFalse) {
252252
// we need the ifTrue to break, so it cannot reach the code we want to move
253-
if (ExpressionAnalyzer::obviouslyDoesNotFlowOut(iff->ifTrue)) {
253+
if (iff->ifTrue->type == unreachable) {
254254
iff->ifFalse = builder.stealSlice(block, i + 1, list.size());
255255
iff->finalize();
256256
block->finalize();
@@ -288,12 +288,12 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
288288
return block;
289289
};
290290

291-
if (ExpressionAnalyzer::obviouslyDoesNotFlowOut(iff->ifTrue)) {
291+
if (iff->ifTrue->type == unreachable) {
292292
iff->ifFalse = blockifyMerge(iff->ifFalse, builder.stealSlice(block, i + 1, list.size()));
293293
iff->finalize();
294294
block->finalize();
295295
return true;
296-
} else if (ExpressionAnalyzer::obviouslyDoesNotFlowOut(iff->ifFalse)) {
296+
} else if (iff->ifFalse->type == unreachable) {
297297
iff->ifTrue = blockifyMerge(iff->ifTrue, builder.stealSlice(block, i + 1, list.size()));
298298
iff->finalize();
299299
block->finalize();

test/passes/remove-unused-brs.txt

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
(type $6 (func (param i32) (result i64)))
99
(type $7 (func (result i64)))
1010
(type $8 (func (result f32)))
11+
(type $9 (func (param i32) (result f32)))
1112
(memory $0 256 256)
1213
(func $b0-yes (type $0) (param $i1 i32)
1314
(block $topmost
@@ -483,11 +484,12 @@
483484
)
484485
(loop $in45
485486
(block $out46
486-
(if
487-
(i32.const 0)
488-
(unreachable)
487+
(br_if $in45
488+
(i32.eqz
489+
(i32.const 0)
490+
)
489491
)
490-
(br $in45)
492+
(unreachable)
491493
)
492494
)
493495
(loop $in48
@@ -1123,18 +1125,35 @@
11231125
)
11241126
(func $unreachable-return-loop-value (type $7) (result i64)
11251127
(loop $loop
1126-
(br_if $loop
1127-
(i32.eqz
1128-
(i32.const 1)
1128+
(if
1129+
(i32.const 1)
1130+
(block $block
1131+
(br_if $block
1132+
(br $loop)
1133+
)
1134+
(br $loop)
11291135
)
11301136
)
1131-
(block $block
1132-
(br_if $block
1133-
(br $loop)
1137+
(br $loop)
1138+
)
1139+
)
1140+
(func $obviously-flows-out-maybe (type $9) (param $var$0 i32) (result f32)
1141+
(block $label$1 (result f32)
1142+
(br $label$1
1143+
(f32.const 1)
1144+
)
1145+
(loop $label$5
1146+
(if
1147+
(i32.const 11)
1148+
(block $label$8
1149+
(br_if $label$8
1150+
(unreachable)
1151+
)
1152+
(br $label$5)
1153+
)
11341154
)
1135-
(br $loop)
1155+
(br $label$5)
11361156
)
11371157
)
1138-
(unreachable)
11391158
)
11401159
)

test/passes/remove-unused-brs.wast

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,5 +1020,24 @@
10201020
(br $loop) ;; we 100% go back to the loop top, the loop is never exited. but opts move code around so that is not obvious anymore, and the loop becomes a nop, but the func has a return value
10211021
)
10221022
)
1023+
(func $obviously-flows-out-maybe (param $var$0 i32) (result f32)
1024+
(block $label$1 (result f32)
1025+
(br $label$1
1026+
(f32.const 1)
1027+
)
1028+
(loop $label$5
1029+
(if
1030+
(i32.const 11)
1031+
(block $label$8 ;; this block is none - it has a break, even if not taken - and so looks like it might flow out,
1032+
(br_if $label$8 ;; and so we can't move it outside to be the end of the loop's block
1033+
(unreachable)
1034+
)
1035+
(br $label$5)
1036+
)
1037+
)
1038+
(br $label$5)
1039+
)
1040+
)
1041+
)
10231042
)
10241043

0 commit comments

Comments
 (0)