Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize conditional expressions #183

Merged
merged 1 commit into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ let (|NoParen|_|) = function
| Int _ | Float _ | Dot _ | Var _ | FunCall (Var _, _) | Subscript _ as x -> Some x
| _ -> None

/// Expression that statically evaluates to boolean value.
let (|True|False|NotABool|) = function
| Int (i, _) when i <> 0 -> True
| Int (i, _) when i = 0 -> False
| Float (f, _) when f <> 0.M -> True
| Float (f, _) when f = 0.M -> False
| Var var when var.Name = "true" -> True
| Var var when var.Name = "false" -> False
| _ -> NotABool

let rec private simplifyExpr (didInline: bool ref) env = function
| FunCall(Var v, passedArgs) as e when v.ToBeInlined ->
match env.fns.TryFind v.Name with
Expand Down Expand Up @@ -136,6 +146,16 @@ let rec private simplifyExpr (didInline: bool ref) env = function
| FunCall(Op "==", [Float (i1,_); Float (i2,_)]) -> bool(i1 = i2)
| FunCall(Op "!=", [Float (i1,_); Float (i2,_)]) -> bool(i1 <> i2)

// Conditionals
| FunCall(Op "?:", [True; x; _]) -> x
| FunCall(Op "?:", [False; _; x]) -> x
| FunCall(Op "&&", [True; x]) -> x
| FunCall(Op "&&", [False; _]) -> bool false
| FunCall(Op "&&", [x; True]) -> x
| FunCall(Op "||", [True; _]) -> bool true
| FunCall(Op "||", [False; x]) -> x
| FunCall(Op "||", [x; False]) -> x

// Stupid simplifications (they can be useful to simplify rewritten code)
| FunCall(Op "/", [e; Float (1.M,_)]) -> e
| FunCall(Op "*", [e; Float (1.M,_)]) -> e
Expand Down Expand Up @@ -250,10 +270,9 @@ let private simplifyStmt = function
| stmts -> Block stmts
| Decl (ty, li) -> Decl (rwType ty, declsNotToInline li)
| ForD((ty, d), cond, inc, body) -> ForD((rwType ty, declsNotToInline d), cond, inc, body)
// FIXME: properly handle booleans
| If(Var var, e1, _) when var.Name = "true" -> e1
| If(Var var, _, Some e2) when var.Name = "false" -> e2
| If(Var var, _, None) when var.Name = "false" -> Block []
| If(True, e1, _) -> e1
| If(False, _, Some e2) -> e2
| If(False, _, None) -> Block []
| If(c, b, Some (Block [])) -> If(c, b, None)
| Verbatim s -> Verbatim (stripSpaces s)
| e -> e
Expand Down
1 change: 1 addition & 0 deletions tests/commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
--format indented -o tests/unit/precision.frag.expected tests/unit/precision.frag
--no-remove-unused --format c-array --no-inlining --no-renaming -o tests/unit/verbatim.frag.expected tests/unit/verbatim.frag
--no-remove-unused --format indented --no-renaming -o tests/unit/forward_declaration.frag.expected tests/unit/forward_declaration.frag
--no-remove-unused --format indented --no-renaming -o tests/unit/conditionals.frag.expected tests/unit/conditionals.frag

# Inlining unit tests

Expand Down
38 changes: 38 additions & 0 deletions tests/unit/conditionals.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
bool success() { return true; }
bool fail() { return false; }

// After optimization, no call to fail should remain.

bool ternary() {
return 1 < 2 ? success() : fail();
}

bool ternary2() {
return 2 < 1 ? fail() : success();
}

bool or() {
return true || fail();
}

bool or2() {
return false || success();
}

bool or3() {
return success() || false;
}

bool and() {
int a = -2;
return a && success();
}

bool and2() {
int a = 0;
return a && fail();
}

bool and3() {
return success() && true;
}
40 changes: 40 additions & 0 deletions tests/unit/conditionals.frag.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
bool success()
{
return true;
}
bool fail()
{
return false;
}
bool ternary()
{
return success();
}
bool ternary2()
{
return success();
}
bool or()
{
return true;
}
bool or2()
{
return success();
}
bool or3()
{
return success();
}
bool and()
{
return success();
}
bool and2()
{
return false;
}
bool and3()
{
return success();
}