Skip to content

Commit 3d6b218

Browse files
committed
refactor an optimizeBoolean method
1 parent 7c0edee commit 3d6b218

3 files changed

Lines changed: 24 additions & 50 deletions

File tree

src/passes/OptimizeInstructions.cpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -214,37 +214,41 @@ struct OptimizeInstructions : public WalkerPass<PostWalker<OptimizeInstructions,
214214
if (get && get->name == set->name) {
215215
ExpressionManipulator::nop(curr);
216216
}
217+
} else if (auto* iff = curr->dynCast<If>()) {
218+
iff->condition = optimizeBoolean(iff->condition);
217219
} else if (auto* select = curr->dynCast<Select>()) {
220+
select->condition = optimizeBoolean(select->condition);
218221
auto* condition = select->condition->dynCast<Unary>();
219222
if (condition && condition->op == EqZInt32) {
220-
auto* condition2 = condition->value->dynCast<Unary>();
221-
if (condition2 && condition2->op == EqZInt32) {
222-
// double eqz
223-
select->condition = condition2->value;
224-
} else {
225-
// flip select, eqz input and flippable
226-
EffectAnalyzer ifTrue(select->ifTrue);
227-
EffectAnalyzer ifFalse(select->ifFalse);
228-
if (!ifTrue.invalidates(ifFalse)) {
229-
select->condition = condition->value;
230-
std::swap(select->ifTrue, select->ifFalse);
231-
}
223+
// flip select to remove eqz, if we can reorder
224+
EffectAnalyzer ifTrue(select->ifTrue);
225+
EffectAnalyzer ifFalse(select->ifFalse);
226+
if (!ifTrue.invalidates(ifFalse)) {
227+
select->condition = condition->value;
228+
std::swap(select->ifTrue, select->ifFalse);
232229
}
233230
}
234231
} else if (auto* br = curr->dynCast<Break>()) {
235232
if (br->condition) {
236-
auto* condition = br->condition->dynCast<Unary>();
237-
if (condition && condition->op == EqZInt32) {
238-
auto* condition2 = condition->value->dynCast<Unary>();
239-
if (condition2 && condition2->op == EqZInt32) {
240-
// double eqz
241-
br->condition = condition2->value;
242-
}
243-
}
233+
br->condition = optimizeBoolean(br->condition);
244234
}
245235
}
246236
return nullptr;
247237
}
238+
239+
private:
240+
241+
Expression* optimizeBoolean(Expression* boolean) {
242+
auto* condition = boolean->dynCast<Unary>();
243+
if (condition && condition->op == EqZInt32) {
244+
auto* condition2 = condition->value->dynCast<Unary>();
245+
if (condition2 && condition2->op == EqZInt32) {
246+
// double eqz
247+
return condition2->value;
248+
}
249+
}
250+
return boolean;
251+
}
248252
};
249253

250254
Pass *createOptimizeInstructionsPass() {

src/passes/OptimizeInstructions.wast

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,6 @@
3434
(call_import $any.expr (i32.const 1))
3535
)
3636
)
37-
;; eqz^2 is eliminatable if the output is boolean (note that for if-else, the above rule handles it in two operations)
38-
(block
39-
(if
40-
(i32.eqz
41-
(i32.eqz
42-
(call_import $i32.expr (i32.const 0))
43-
)
44-
)
45-
(call_import $any.expr (i32.const 1))
46-
)
47-
(if
48-
(call_import $i32.expr (i32.const 0))
49-
(call_import $any.expr (i32.const 1))
50-
)
51-
)
5237
;; equal 0 => eqz
5338
(block
5439
(i32.eq

src/passes/OptimizeInstructions.wast.processed

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,6 @@
3434
"(call_import $any.expr (i32.const 1))\n"
3535
")\n"
3636
")\n"
37-
";; eqz^2 is eliminatable if the output is boolean (note that for if-else, the above rule handles it in two operations)\n"
38-
"(block\n"
39-
"(if\n"
40-
"(i32.eqz\n"
41-
"(i32.eqz\n"
42-
"(call_import $i32.expr (i32.const 0))\n"
43-
")\n"
44-
")\n"
45-
"(call_import $any.expr (i32.const 1))\n"
46-
")\n"
47-
"(if\n"
48-
"(call_import $i32.expr (i32.const 0))\n"
49-
"(call_import $any.expr (i32.const 1))\n"
50-
")\n"
51-
")\n"
5237
";; equal 0 => eqz\n"
5338
"(block\n"
5439
"(i32.eq\n"

0 commit comments

Comments
 (0)