minor optimization of boolean comparisons

This commit is contained in:
Vern Paxson 2024-07-23 15:31:59 -07:00
parent e960c29acb
commit ff7466df6e
2 changed files with 28 additions and 4 deletions

View file

@ -1238,12 +1238,30 @@ ExprPtr EqExpr::Reduce(Reducer* c, StmtPtr& red_stmt) {
if ( IsHasElementsTest() )
return BuildHasElementsTest()->Reduce(c, red_stmt);
if ( GetType()->Tag() == TYPE_BOOL && same_singletons(op1, op2) ) {
if ( GetType()->Tag() == TYPE_BOOL ) {
if ( same_singletons(op1, op2) ) {
bool t = Tag() == EXPR_EQ;
auto res = with_location_of(make_intrusive<ConstExpr>(val_mgr->Bool(t)), this);
return res->Reduce(c, red_stmt);
}
if ( op1->GetType()->Tag() == TYPE_BOOL ) {
if ( op1->Tag() == EXPR_CONST )
std::swap(op1, op2);
if ( op2->Tag() == EXPR_CONST ) {
bool t = Tag() == EXPR_EQ;
if ( op2->AsConstExpr()->Value()->IsZero() )
t = ! t;
if ( t )
return op1->Reduce(c, red_stmt);
auto res = with_location_of(make_intrusive<NotExpr>(op1), this);
return res->Reduce(c, red_stmt);
}
}
}
return BinaryExpr::Reduce(c, red_stmt);
}

View file

@ -245,6 +245,12 @@ StmtPtr IfStmt::DoReduce(Reducer* c) {
red_e_stmt = cond_red_stmt;
}
// Check again for negation given above reductions/replacements.
if ( e->Tag() == EXPR_NOT ) {
std::swap(s1, s2);
e = e->GetOp1();
}
StmtPtr sl;
if ( e->IsConst() ) {