From ff7466df6e54a548fa58303993cad8fcf9728a1b Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Tue, 23 Jul 2024 15:31:59 -0700 Subject: [PATCH] minor optimization of boolean comparisons --- src/script_opt/Expr.cc | 26 ++++++++++++++++++++++---- src/script_opt/Stmt.cc | 6 ++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/script_opt/Expr.cc b/src/script_opt/Expr.cc index 1ad24c5277..353604f1a7 100644 --- a/src/script_opt/Expr.cc +++ b/src/script_opt/Expr.cc @@ -1238,10 +1238,28 @@ 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) ) { - bool t = Tag() == EXPR_EQ; - auto res = with_location_of(make_intrusive(val_mgr->Bool(t)), this); - return res->Reduce(c, red_stmt); + if ( GetType()->Tag() == TYPE_BOOL ) { + if ( same_singletons(op1, op2) ) { + bool t = Tag() == EXPR_EQ; + auto res = with_location_of(make_intrusive(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(op1), this); + return res->Reduce(c, red_stmt); + } + } } return BinaryExpr::Reduce(c, red_stmt); diff --git a/src/script_opt/Stmt.cc b/src/script_opt/Stmt.cc index 618d52ebda..8ef72c60b4 100644 --- a/src/script_opt/Stmt.cc +++ b/src/script_opt/Stmt.cc @@ -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() ) {