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

[EVM] Don't reorder stack operands for a commutable instruction #717

Open
akiramenai opened this issue Oct 23, 2024 · 0 comments
Open

[EVM] Don't reorder stack operands for a commutable instruction #717

akiramenai opened this issue Oct 23, 2024 · 0 comments

Comments

@akiramenai
Copy link
Collaborator

The current EF stackification algorithm does not account for commutability. While EVMSingleUseExpression was designed to mitigate this issue, it is insufficient for handling cases like calling conventions. To fully resolve the problem of optimal stack configuration computations, functions such as combineStack and other layout creation logic must compare stacks not for strict equality but for equality up to commutable operations. However, this is complex and out of scope for this ticket.

A simpler approach to address the most obvious cases of suboptimal code generation is to maintain the current stack layout. However, when inserting stack permutations, we should evaluate whether it would be more efficient to shuffle the stack in cases where operands can be swapped.

LLVM IR:
define i256 @addrrr(i256 %rs1, i256 %rs2) nounwind {
  %res = add i256 %rs1, %rs2
  ret i256 %res
}

ASM:
addrrr:                                 ; @addrrr
; %bb.0:
	JUMPDEST
	SWAP1          <- This swap is not needed
	ADD
	SWAP1
	JUMP
LLVM IR:
define i256 @select(i256 %v1, i256 %v2, i256 %v3, i256 %v4) {
  %1 = icmp ne i256 %v3, %v4
  %2 = select i1 %1, i256 %v1, i256 %v2
  ret i256 %2
}

ASM:
select:                                 ; @select
; %bb.0:
	JUMPDEST
	SWAP2
	SWAP1
	SWAP3
	EQ
	ISZERO
	PUSH4 @.BB0_3
	JUMPI
	PUSH4 @.BB0_1
	JUMP
.BB0_3:
	JUMPDEST
	SWAP2
	SWAP1
	POP
	PUSH4 @.BB0_2
	JUMP
.BB0_1:
	JUMPDEST
	POP
	SWAP1
.BB0_2:
	JUMPDEST
	JUMP
@akiramenai akiramenai changed the title Don't reorder stack operands for a commutable instruction [EVM] Don't reorder stack operands for a commutable instruction Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant