Skip to content

Commit

Permalink
feat: simplify more conditions when we're in a select context
Browse files Browse the repository at this point in the history
  • Loading branch information
achidlow committed Mar 22, 2024
1 parent 3331cff commit 7403e7b
Show file tree
Hide file tree
Showing 19 changed files with 229 additions and 257 deletions.
2 changes: 1 addition & 1 deletion examples/sizes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
state_totals 46 34 12 34 0
string_ops 157 152 5 152 0
stubs/BigUInt 172 112 60 112 0
stubs/Bytes 1769 258 1511 258 0
stubs/Bytes 1769 253 1516 253 0
stubs/String 777 141 636 141 0
stubs/Uint64 371 8 363 8 0
template_variables/TemplateVariables 168 155 13 155 0
Expand Down
3 changes: 3 additions & 0 deletions src/puya/ir/optimize/intrinsic_simplification.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ def _try_fold_intrinsic(
selector_const = _get_int_constant(selector)
if selector_const is not None:
return true if selector_const else false
maybe_simplified_select_cond = _try_simplify_bool_condition(subroutine, selector)
if maybe_simplified_select_cond is not None:
return attrs.evolve(intrinsic, args=[false, true, maybe_simplified_select_cond])
elif intrinsic.op is AVMOp.getbit:
match intrinsic.args:
case [
Expand Down
10 changes: 3 additions & 7 deletions test_cases/stubs/out/BytesContract.approval.mir
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,9 @@ check_slicing_with_uint64_block@0:
// virtual: store tmp%73#0 to l-stack (no copy) (𝕡) abc#0 | tmp%1#0,tmp%73#0 one_to_seven()[one:-1] == b"23456" stubs/bytes.py:84
// virtual: load tmp%73#0 from l-stack (no copy) (𝕡) abc#0 | tmp%1#0,tmp%73#0 assert one_to_seven()[one:-1] == b"23456" stubs/bytes.py:84
assert // (𝕡) abc#0 | tmp%1#0 assert one_to_seven()[one:-1] == b"23456" stubs/bytes.py:84
int 0 // (𝕡) abc#0 | tmp%1#0,0 0 stubs/bytes.py:85
dig 1 // load tmp%1#0 from l-stack (copy) (𝕡) abc#0 | tmp%1#0,0,tmp%1#0 abc[UInt64(0) : ten] stubs/bytes.py:85
< // (𝕡) abc#0 | tmp%1#0,{<} abc[UInt64(0) : ten] stubs/bytes.py:85
swap // store tmp%78#0 to l-stack (no copy) (𝕡) abc#0 | tmp%78#0,tmp%1#0 abc[UInt64(0) : ten] stubs/bytes.py:85
dup // load tmp%1#0 from l-stack (copy) (𝕡) abc#0 | tmp%78#0,tmp%1#0,tmp%1#0 abc[UInt64(0) : ten] stubs/bytes.py:85
int 0 // (𝕡) abc#0 | tmp%78#0,tmp%1#0,tmp%1#0,0 0 stubs/bytes.py:85
uncover 3 // load tmp%78#0 from l-stack (no copy) (𝕡) abc#0 | tmp%1#0,tmp%1#0,0,tmp%78#0 abc[UInt64(0) : ten] stubs/bytes.py:85
dup // load tmp%1#0 from l-stack (copy) (𝕡) abc#0 | tmp%1#0,tmp%1#0 abc[UInt64(0) : ten] stubs/bytes.py:85
int 0 // (𝕡) abc#0 | tmp%1#0,tmp%1#0,0 0 stubs/bytes.py:85
dig 2 // load tmp%1#0 from l-stack (copy) (𝕡) abc#0 | tmp%1#0,tmp%1#0,0,tmp%1#0 abc[UInt64(0) : ten] stubs/bytes.py:85
select // (𝕡) abc#0 | tmp%1#0,{select} abc[UInt64(0) : ten] stubs/bytes.py:85
swap // store tmp%79#0 to l-stack (no copy) (𝕡) abc#0 | tmp%79#0,tmp%1#0 abc[UInt64(0) : ten] stubs/bytes.py:85
int 10 // (𝕡) abc#0 | tmp%79#0,tmp%1#0,10 10 stubs/bytes.py:80
Expand Down
6 changes: 1 addition & 5 deletions test_cases/stubs/out/BytesContract.approval.teal
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,9 @@ check_slicing_with_uint64:
assert
// stubs/bytes.py:85
// assert abc[UInt64(0) : ten] == b"abc"
int 0
dig 1
<
swap
dup
int 0
uncover 3
dig 2
select
swap
// stubs/bytes.py:80
Expand Down
3 changes: 1 addition & 2 deletions test_cases/stubs/out/BytesContract.destructured.ir
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ contract test_cases.stubs.bytes.BytesContract:
let tmp%72#0: bytes = (substring3 awst_tmp%44#0 tmp%49#0 tmp%71#0)
let tmp%73#0: uint64 = (== tmp%72#0 "23456")
(assert tmp%73#0)
let tmp%78#0: uint64 = (< 0u tmp%1#0)
let tmp%79#0: uint64 = (select tmp%1#0 0u tmp%78#0)
let tmp%79#0: uint64 = (select tmp%1#0 0u tmp%1#0)
let tmp%83#0: uint64 = (< 10u tmp%1#0)
let tmp%84#0: uint64 = (select tmp%1#0 10u tmp%83#0)
let tmp%97#0: uint64 = (> tmp%79#0 tmp%84#0)
Expand Down
3 changes: 1 addition & 2 deletions test_cases/stubs/out/BytesContract.ssa.opt_pass_1.ir
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,7 @@ contract test_cases.stubs.bytes.BytesContract:
let tmp%72#0: bytes = (substring3 awst_tmp%44#0 tmp%49#0 tmp%71#0)
let tmp%73#0: uint64 = (== tmp%72#0 "23456")
(assert tmp%73#0)
let tmp%78#0: uint64 = (< 0u tmp%1#0)
let tmp%79#0: uint64 = (select tmp%1#0 0u tmp%78#0)
let tmp%79#0: uint64 = (select tmp%1#0 0u tmp%1#0)
let tmp%83#0: uint64 = (< 10u tmp%1#0)
let tmp%84#0: uint64 = (select tmp%1#0 10u tmp%83#0)
let tmp%97#0: uint64 = (> tmp%79#0 tmp%84#0)
Expand Down
3 changes: 1 addition & 2 deletions test_cases/stubs/out/BytesContract.ssa.opt_pass_2.ir
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ contract test_cases.stubs.bytes.BytesContract:
let tmp%72#0: bytes = (substring3 awst_tmp%44#0 tmp%49#0 tmp%71#0)
let tmp%73#0: uint64 = (== tmp%72#0 "23456")
(assert tmp%73#0)
let tmp%78#0: uint64 = (< 0u tmp%1#0)
let tmp%79#0: uint64 = (select tmp%1#0 0u tmp%78#0)
let tmp%79#0: uint64 = (select tmp%1#0 0u tmp%1#0)
let tmp%83#0: uint64 = (< 10u tmp%1#0)
let tmp%84#0: uint64 = (select tmp%1#0 10u tmp%83#0)
let tmp%97#0: uint64 = (> tmp%79#0 tmp%84#0)
Expand Down
3 changes: 1 addition & 2 deletions test_cases/stubs/out/BytesContract.ssa.opt_pass_3.ir
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ contract test_cases.stubs.bytes.BytesContract:
let tmp%72#0: bytes = (substring3 awst_tmp%44#0 tmp%49#0 tmp%71#0)
let tmp%73#0: uint64 = (== tmp%72#0 "23456")
(assert tmp%73#0)
let tmp%78#0: uint64 = (< 0u tmp%1#0)
let tmp%79#0: uint64 = (select tmp%1#0 0u tmp%78#0)
let tmp%79#0: uint64 = (select tmp%1#0 0u tmp%1#0)
let tmp%83#0: uint64 = (< 10u tmp%1#0)
let tmp%84#0: uint64 = (select tmp%1#0 10u tmp%83#0)
let tmp%97#0: uint64 = (> tmp%79#0 tmp%84#0)
Expand Down
3 changes: 1 addition & 2 deletions test_cases/stubs/out/BytesContract.ssa.opt_pass_4.ir
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ contract test_cases.stubs.bytes.BytesContract:
let tmp%72#0: bytes = (substring3 awst_tmp%44#0 tmp%49#0 tmp%71#0)
let tmp%73#0: uint64 = (== tmp%72#0 "23456")
(assert tmp%73#0)
let tmp%78#0: uint64 = (< 0u tmp%1#0)
let tmp%79#0: uint64 = (select tmp%1#0 0u tmp%78#0)
let tmp%79#0: uint64 = (select tmp%1#0 0u tmp%1#0)
let tmp%83#0: uint64 = (< 10u tmp%1#0)
let tmp%84#0: uint64 = (select tmp%1#0 10u tmp%83#0)
let tmp%97#0: uint64 = (> tmp%79#0 tmp%84#0)
Expand Down
3 changes: 1 addition & 2 deletions test_cases/stubs/out/BytesContract.ssa.opt_pass_5.ir
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ contract test_cases.stubs.bytes.BytesContract:
let tmp%72#0: bytes = (substring3 awst_tmp%44#0 tmp%49#0 tmp%71#0)
let tmp%73#0: uint64 = (== tmp%72#0 "23456")
(assert tmp%73#0)
let tmp%78#0: uint64 = (< 0u tmp%1#0)
let tmp%79#0: uint64 = (select tmp%1#0 0u tmp%78#0)
let tmp%79#0: uint64 = (select tmp%1#0 0u tmp%1#0)
let tmp%83#0: uint64 = (< 10u tmp%1#0)
let tmp%84#0: uint64 = (select tmp%1#0 10u tmp%83#0)
let tmp%97#0: uint64 = (> tmp%79#0 tmp%84#0)
Expand Down
3 changes: 1 addition & 2 deletions test_cases/stubs/out/BytesContract.ssa.opt_pass_6.ir
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ contract test_cases.stubs.bytes.BytesContract:
let tmp%72#0: bytes = (substring3 awst_tmp%44#0 tmp%49#0 tmp%71#0)
let tmp%73#0: uint64 = (== tmp%72#0 "23456")
(assert tmp%73#0)
let tmp%78#0: uint64 = (< 0u tmp%1#0)
let tmp%79#0: uint64 = (select tmp%1#0 0u tmp%78#0)
let tmp%79#0: uint64 = (select tmp%1#0 0u tmp%1#0)
let tmp%83#0: uint64 = (< 10u tmp%1#0)
let tmp%84#0: uint64 = (select tmp%1#0 10u tmp%83#0)
let tmp%97#0: uint64 = (> tmp%79#0 tmp%84#0)
Expand Down
3 changes: 1 addition & 2 deletions test_cases/stubs/out/BytesContract.ssa.opt_pass_7.ir
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ contract test_cases.stubs.bytes.BytesContract:
let tmp%72#0: bytes = (substring3 awst_tmp%44#0 tmp%49#0 tmp%71#0)
let tmp%73#0: uint64 = (== tmp%72#0 "23456")
(assert tmp%73#0)
let tmp%78#0: uint64 = (< 0u tmp%1#0)
let tmp%79#0: uint64 = (select tmp%1#0 0u tmp%78#0)
let tmp%79#0: uint64 = (select tmp%1#0 0u tmp%1#0)
let tmp%83#0: uint64 = (< 10u tmp%1#0)
let tmp%84#0: uint64 = (select tmp%1#0 10u tmp%83#0)
let tmp%97#0: uint64 = (> tmp%79#0 tmp%84#0)
Expand Down
3 changes: 1 addition & 2 deletions test_cases/stubs/out/BytesContract.ssa.opt_pass_8.ir
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ contract test_cases.stubs.bytes.BytesContract:
let tmp%72#0: bytes = (substring3 awst_tmp%44#0 tmp%49#0 tmp%71#0)
let tmp%73#0: uint64 = (== tmp%72#0 "23456")
(assert tmp%73#0)
let tmp%78#0: uint64 = (< 0u tmp%1#0)
let tmp%79#0: uint64 = (select tmp%1#0 0u tmp%78#0)
let tmp%79#0: uint64 = (select tmp%1#0 0u tmp%1#0)
let tmp%83#0: uint64 = (< 10u tmp%1#0)
let tmp%84#0: uint64 = (select tmp%1#0 10u tmp%83#0)
let tmp%97#0: uint64 = (> tmp%79#0 tmp%84#0)
Expand Down
3 changes: 1 addition & 2 deletions test_cases/stubs/out/BytesContract.ssa.opt_pass_9.ir
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ contract test_cases.stubs.bytes.BytesContract:
let tmp%72#0: bytes = (substring3 awst_tmp%44#0 tmp%49#0 tmp%71#0)
let tmp%73#0: uint64 = (== tmp%72#0 "23456")
(assert tmp%73#0)
let tmp%78#0: uint64 = (< 0u tmp%1#0)
let tmp%79#0: uint64 = (select tmp%1#0 0u tmp%78#0)
let tmp%79#0: uint64 = (select tmp%1#0 0u tmp%1#0)
let tmp%83#0: uint64 = (< 10u tmp%1#0)
let tmp%84#0: uint64 = (select tmp%1#0 10u tmp%83#0)
let tmp%97#0: uint64 = (> tmp%79#0 tmp%84#0)
Expand Down
Loading

0 comments on commit 7403e7b

Please sign in to comment.