Skip to content

Commit

Permalink
feat: simplify more conditions when we're in a ConditionalBranch context
Browse files Browse the repository at this point in the history
  • Loading branch information
achidlow committed Mar 22, 2024
1 parent 7f89b6c commit 80f0167
Show file tree
Hide file tree
Showing 55 changed files with 2,202 additions and 2,460 deletions.
10 changes: 5 additions & 5 deletions examples/sizes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@
merkle/MerkleTree 217 210 7 210 0
nested_loops/Nested 243 201 42 201 0
regression_118 135 100 35 100 0
reversed_iteration 765 549 216 549 0
reversed_iteration 765 547 218 547 0
scratch_slots 70 67 3 67 0
scratch_slots/2 70 67 3 67 0
scratch_slots/MyOther 8 8 0 8 0
simple 81 29 52 29 0
simplish/Simplish 747 719 28 720 -1
simplish/Simplish 747 713 34 714 -1
ssa 286 230 56 230 0
ssa2 95 87 8 87 0
state_proxies/StateProxy 87 83 4 83 0
Expand All @@ -80,7 +80,7 @@
typed_abi_call/Greeter 1285 1168 117 1168 0
typed_abi_call/Logger 548 463 85 463 0
unary/Unary 134 96 38 96 0
undefined_phi_args/Baddie 350 286 64 286 0
unssa/UnSSA 446 375 71 375 0
voting/VotingRoundApp 1618 1514 104 1514 0
undefined_phi_args/Baddie 350 284 66 284 0
unssa/UnSSA 446 369 77 369 0
voting/VotingRoundApp 1618 1510 108 1510 0
with_reentrancy/WithReentrancy 264 242 22 242 0
20 changes: 6 additions & 14 deletions examples/voting/out/VotingRoundApp.approval.mir

Large diffs are not rendered by default.

12 changes: 4 additions & 8 deletions examples/voting/out/VotingRoundApp.approval.teal
Original file line number Diff line number Diff line change
Expand Up @@ -577,12 +577,10 @@ close_for_header@1:
int 1
extract3
frame_bury 1
// voting/voting.py:131
// if question_index > 0:
int 0
>
frame_dig 5
frame_bury 0
// voting/voting.py:131
// if question_index > 0:
bz close_after_if_else@4
// voting/voting.py:132
// note += ","
Expand Down Expand Up @@ -626,13 +624,11 @@ close_for_header@6:
frame_dig 4
<
bz close_after_for@12
frame_dig 5
frame_bury 0
// voting/voting.py:136
// if option_index > 0:
frame_dig 3
int 0
>
frame_dig 5
frame_bury 0
bz close_after_if_else@9
// voting/voting.py:137
// note += ","
Expand Down
2 changes: 1 addition & 1 deletion examples/voting/out/VotingRoundApp.arc32.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions examples/voting/out/VotingRoundApp.destructured.ir

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions examples/voting/out/VotingRoundApp.ssa.opt_pass_1.ir

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions examples/voting/out/VotingRoundApp.ssa.opt_pass_2.ir

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions examples/voting/out_O2/VotingRoundApp.approval.teal
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,6 @@ close_for_header@1:
int 1
extract3
frame_bury 1
int 0
>
frame_dig 5
frame_bury 0
bz close_after_if_else@4
Expand Down Expand Up @@ -361,11 +359,9 @@ close_for_header@6:
frame_dig 4
<
bz close_after_for@12
frame_dig 3
int 0
>
frame_dig 5
frame_bury 0
frame_dig 3
bz close_after_if_else@9
frame_dig 5
byte ","
Expand Down
6 changes: 2 additions & 4 deletions examples/voting/out_O2/VotingRoundApp.destructured.ir

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions examples/voting/puya.log

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/puya/ir/optimize/intrinsic_simplification.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,26 @@ def visit_intrinsic_op(self, intrinsic: Intrinsic) -> Intrinsic | None:
return simplified
return intrinsic

def visit_conditional_branch(self, branch: models.ConditionalBranch) -> models.ControlOp:
if isinstance(branch.condition, models.Register):
branch_cond_defn = get_definition(self.subroutine, branch.condition)
match branch_cond_defn:
case models.Assignment(
source=models.Intrinsic(
args=[
models.Value(atype=AVMType.uint64) as a,
models.Value(atype=AVMType.uint64) as b,
]
) as assert_cond_op
):
branch_cond_simplified = _try_simplify_uint64_binary_op(
assert_cond_op, a, b, bool_context=True
)
if isinstance(branch_cond_simplified, models.Value):
self.modified += 1
return attrs.evolve(branch, condition=branch_cond_simplified)
return branch


def _try_convert_stack_args_to_immediates(intrinsic: Intrinsic) -> Intrinsic | None:
match intrinsic:
Expand Down
Loading

0 comments on commit 80f0167

Please sign in to comment.