Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/topic/bbannier/fold_while'
Browse files Browse the repository at this point in the history
  • Loading branch information
bbannier committed Mar 1, 2024
2 parents 031ca00 + 77e0ba9 commit 68ffe6c
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
1.10.0-dev.158 | 2024-03-01 18:10:49 +0100

* Simplify `while` loops over constant conditions. (Benjamin Bannier, Corelight)

While loops with constant conditions are unlikely to occur in code
generated by us, but could appear in user code. Cleaning them up is
simple and cheap, so this patch implements a pass which simplifies them.

1.10.0-dev.156 | 2024-02-27 17:44:22 +0100

* GH-1624: Enable optimizations when running `spicy-build`. (Benjamin Bannier, Corelight)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.10.0-dev.156
1.10.0-dev.158
38 changes: 38 additions & 0 deletions hilti/toolchain/src/compiler/optimizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@
#include <hilti/ast/builder/expression.h>
#include <hilti/ast/ctors/default.h>
#include <hilti/ast/declaration.h>
#include <hilti/ast/declarations/constant.h>
#include <hilti/ast/declarations/function.h>
#include <hilti/ast/declarations/imported-module.h>
#include <hilti/ast/detail/visitor.h>
#include <hilti/ast/expressions/ctor.h>
#include <hilti/ast/expressions/id.h>
#include <hilti/ast/expressions/logical-and.h>
#include <hilti/ast/expressions/logical-not.h>
#include <hilti/ast/expressions/logical-or.h>
#include <hilti/ast/expressions/ternary.h>
#include <hilti/ast/node.h>
#include <hilti/ast/scope-lookup.h>
#include <hilti/ast/statements/block.h>
#include <hilti/ast/statements/while.h>
#include <hilti/ast/type.h>
#include <hilti/ast/types/bool.h>
#include <hilti/ast/types/enum.h>
Expand Down Expand Up @@ -898,6 +901,41 @@ struct ConstantFoldingVisitor : OptimizerVisitor, visitor::PreOrder<bool, Consta

return false;
}

bool operator()(const statement::While& x, position_t p) {
switch ( _stage ) {
case Stage::COLLECT:
case Stage::PRUNE_DECLS: return false;
case Stage::PRUNE_USES: {
const auto& cond = x.condition();
if ( ! cond )
return false;

const auto val = tryAsBoolLiteral(*cond);
if ( ! val )
return false;

// If the `while` condition is true we never run the `else` block.
if ( *val && x.else_() ) {
// Remove child for `else`.
p.node.as<statement::While>().children()[3] = node::none;
return true;
}
// If the `while` condition is false we never enter the loop, and
// run either the `else` block if it is present or nothing.
else if ( ! *val ) {
if ( const auto& else_ = x.else_() )
replaceNode(p, else_->_clone());
else
removeNode(p);

return true;
}

return false;
}
}
}
};

// This visitor collects requirement attributes in the AST and toggles unused features.
Expand Down
24 changes: 24 additions & 0 deletions tests/Baseline/hilti.optimization.const/noopt.hlt
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,28 @@ True;
t ? 1 : 0;
f ? 0 : 1;

while ( False ) {
}


while ( False ) {
False;
}
else {
True;
}


while ( True ) {
}


while ( True ) {
True;
}
else {
False;
}


}
12 changes: 12 additions & 0 deletions tests/Baseline/hilti.optimization.const/opt.hlt
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,17 @@ False;
True;
1;
1;
{
True;
}

while ( True ) {
}


while ( True ) {
True;
}


}
17 changes: 17 additions & 0 deletions tests/hilti/optimization/const.hlt
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,21 @@ f && f;
t ? 1: 0;
f ? 0: 1;

# While loops over constants.
while (False) {}

while (False) {
False;
} else {
True;
}

while (True) {}

while (True) {
True;
} else {
False;
}

}

0 comments on commit 68ffe6c

Please sign in to comment.