Skip to content

Commit 8a66e9f

Browse files
committed
SSA: Prefer swapping instead of shrinking followed by dupping
Previously, if a slot is desirable to be dupped, but is out of reach, we would try to shrink the stack and then bringing it to reachable region. However, even if it is out-of-reach for dup, it might still be in reach for swap, so if it is safe to swap, we can do it directly.
1 parent b5e2e78 commit 8a66e9f

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

libyul/backends/evm/ssa/StackShuffler.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,12 @@ class StackShuffler
347347
}
348348
else
349349
{
350+
// even if it is not dup reachable, it still might be swappable
351+
if (_stack.swapReachable(sourceOffset) && _state.isSafeToSwapWithTop(sourceOffset))
352+
{
353+
_stack.swap(sourceOffset);
354+
return true;
355+
}
350356
// the slot we need something in the args region of is unreachable, try compressing the stack,
351357
// first looking at the top
352358
if (shrinkStack(_stack, _state))

test/libyul/ssa/stackShuffler/deep_argument_shrink.stack

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@ targetStackSize: 19
99
// (initial)| v12 * * * * * * * * * * * * v68 * phi111 v84 | v86
1010
// SWAP3| v12 * * * * * * * * * * * * v68 v86 phi111 v84 | *
1111
// POP| v12 * * * * * * * * * * * * v68 v86 phi111 v84
12-
// SWAP2| v12 * * * * * * * * * * * * v68 v84 phi111 v86
13-
// SWAP4| v12 * * * * * * * * * * * v86 v68 v84 phi111 *
14-
// POP| v12 * * * * * * * * * * * v86 v68 v84 phi111
15-
// SWAP15| phi111 * * * * * * * * * * * v86 v68 v84 v12
16-
// DUP4| phi111 * * * * * * * * * * * v86 v68 v84 v12 v86
17-
// DUP2| phi111 * * * * * * * * * * * v86 v68 v84 v12 v86 | v12
18-
// DUP2| phi111 * * * * * * * * * * * v86 v68 v84 v12 v86 | v12 v86
12+
// SWAP16| v84 * * * * * * * * * * * * v68 v86 phi111 v12
13+
// DUP1| v84 * * * * * * * * * * * * v68 v86 phi111 v12 | v12
14+
// DUP4| v84 * * * * * * * * * * * * v68 v86 phi111 v12 | v12 v86
1915
// +----------------------------------------------------------------------------------------------------------------------- +--------------
2016
// (target)| {v68, v84, phi111} | v12 v86

0 commit comments

Comments
 (0)