aptos-core
aptos-core copied to clipboard
[Feature Request][compiler-v2] Extend peephole optimizations from fixed window to arbitrary length
🚀 Feature Request
Implement the generalized version of the "inefficient binops" peephole optimization (added in #13894).
The generalized pattern is:
load constant;
store to local i;
... <any code not using i>
move i to stack;
optimizes to
... <any code not using i>
load constant
A test case has been added in #13894 to show an instance where this generalized pattern is applicable (third_party/move/move-compiler-v2/tests/file-format-generator/multi_use.move
).
In this test case,
module 0xc0ffee::m {
public fun consume(_a: u64, _b: u64, _c: u64, _d: u64) {
}
public fun test(x: u64) {
consume(x, x, 1, x);
}
}
compiles down to:
0: LdU64(1)
1: StLoc[1](loc0: u64)
2: CopyLoc[0](Arg0: u64)
3: CopyLoc[0](Arg0: u64)
4: MoveLoc[1](loc0: u64)
5: MoveLoc[0](Arg0: u64)
6: Call consume(u64, u64, u64, u64)
7: Ret
but can be optimized to:
0: CopyLoc[0](Arg0: u64)
1: CopyLoc[0](Arg0: u64)
2: LdU64(1)
3: MoveLoc[0](Arg0: u64)
4: Call consume(u64, u64, u64, u64)
5: Ret