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] Fold identical BBs after call to jumps lowering #723

Open
akiramenai opened this issue Oct 24, 2024 · 1 comment
Open

[EVM] Fold identical BBs after call to jumps lowering #723

akiramenai opened this issue Oct 24, 2024 · 1 comment

Comments

@akiramenai
Copy link
Collaborator

In EVM necessity to mark jump destinations with JUMPDEST makes call a terminator, and after its expansion to

	PUSH4 @.RET_DEST
	PUSH4 @func
	JUMP
.RET_DEST:
	JUMPDEST
        <original BB remainder>

Identical basic blocks might appear.
For instance:

declare void @foo()
declare void @bar()
; CHECK-LABEL: diamond
define void @diamond(i1 %par) nounwind {
entry:
  br i1 %par, label %bb1, label %bb2
bb1:
  call void @foo()
  br label %exit
bb2:
  call void @bar()
  br label %exit
exit:
  ret void
}

produces

diamond:                                ; @diamond
; %bb.0:                                ; %entry
	JUMPDEST
	PUSH1 1
	AND
	ISZERO
	PUSH4 @.BB0_2
	JUMPI
.BB0_1:                                 ; %bb1
	JUMPDEST
	PUSH4 @.FUNC_RET0
	PUSH4 @foo
	JUMP
.FUNC_RET0:
	JUMPDEST
	JUMP
.BB0_2:                                 ; %bb2
	JUMPDEST
	PUSH4 @.FUNC_RET1
	PUSH4 @bar
	JUMP
.FUNC_RET1:
	JUMPDEST
	JUMP

and FUNC_RET0, FUNC_RET1 can be merged.
Also, consider moving #722 to MIR level.

@vladimirradosavljevic
Copy link
Contributor

vladimirradosavljevic commented Oct 25, 2024

For this,BranchFolding, TailDuplication and MachineBlockPlacement optimizations should help, so I don't think we need to move #722 to MIR level.
These optimizations should also help to optimize select function from #717 (I'm not sure why that example is in that ticket):

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

to

select:                                 ; @select
; %bb.0:
	JUMPDEST
	SWAP2
	SWAP1
	SWAP3
	EQ
	PUSH4 @.BB0_1
	JUMPI
.BB0_3:
	SWAP2
	SWAP1
	POP
	JUMP
.BB0_1:
	JUMPDEST
	POP
	SWAP1
	JUMP

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

2 participants