Skip to content

Commit aa06410

Browse files
authored
recurse in optimizeBoolean (WebAssembly#809)
1 parent 82639c7 commit aa06410

5 files changed

Lines changed: 50 additions & 12 deletions

File tree

src/passes/OptimizeInstructions.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,17 @@ struct OptimizeInstructions : public WalkerPass<PostWalker<OptimizeInstructions,
371371
}
372372
}
373373
}
374+
} else if (auto* block = boolean->dynCast<Block>()) {
375+
if (block->type == i32 && block->list.size() > 0) {
376+
block->list.back() = optimizeBoolean(block->list.back());
377+
}
378+
} else if (auto* iff = boolean->dynCast<If>()) {
379+
if (iff->type == i32) {
380+
iff->ifTrue = optimizeBoolean(iff->ifTrue);
381+
iff->ifFalse = optimizeBoolean(iff->ifFalse);
382+
}
374383
}
384+
// TODO: recurse into br values?
375385
return boolean;
376386
}
377387

test/emcc_O2_hello_world.fromasm

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9600,12 +9600,9 @@
96009600
(i32.const 12)
96019601
)
96029602
)
9603-
(i32.ne
9604-
(call $___syscall54
9605-
(i32.const 54)
9606-
(get_local $3)
9607-
)
9608-
(i32.const 0)
9603+
(call $___syscall54
9604+
(i32.const 54)
9605+
(get_local $3)
96099606
)
96109607
)
96119608
)

test/emcc_O2_hello_world.fromasm.imprecise

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9598,12 +9598,9 @@
95989598
(i32.const 12)
95999599
)
96009600
)
9601-
(i32.ne
9602-
(call $___syscall54
9603-
(i32.const 54)
9604-
(get_local $3)
9605-
)
9606-
(i32.const 0)
9601+
(call $___syscall54
9602+
(i32.const 54)
9603+
(get_local $3)
96079604
)
96089605
)
96099606
)

test/passes/optimize-instructions.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,4 +375,21 @@
375375
)
376376
(i32.const 1)
377377
)
378+
(func $recurse-bool (type $1)
379+
(if
380+
(if i32
381+
(i32.const 1)
382+
(call $ne0)
383+
(call $ne0)
384+
)
385+
(nop)
386+
)
387+
(if
388+
(block $block i32
389+
(nop)
390+
(call $ne0)
391+
)
392+
(nop)
393+
)
394+
)
378395
)

test/passes/optimize-instructions.wast

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,21 @@
296296
)
297297
(i32.const 1)
298298
)
299+
(func $recurse-bool
300+
(if
301+
(if i32
302+
(i32.const 1)
303+
(i32.ne (call $ne0) (i32.const 0))
304+
(i32.ne (call $ne0) (i32.const 0))
305+
)
306+
(nop)
307+
)
308+
(if
309+
(block i32
310+
(nop)
311+
(i32.ne (call $ne0) (i32.const 0))
312+
)
313+
(nop)
314+
)
315+
)
299316
)

0 commit comments

Comments
 (0)