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

Adjust CMP with immediate to CSE opportunities with other CMP #655

Open
vladimirradosavljevic opened this issue Jul 10, 2024 · 0 comments
Open

Comments

@vladimirradosavljevic
Copy link
Contributor

For the following test:

declare void @use1()
declare void @use2()
define i256 @test(i256 %arg, i256 %arg1, i256 %arg2, i256 %arg3) {
entry:
  %icmp = icmp ult i256 %arg, 5
  br i1 %icmp, label %bb1, label %bb2

bb1:
  call void @use1()
  br label %bb4

bb2:
  %icmp2 = icmp ugt i256 %arg, 5
  br i1 %icmp2, label %bb3, label %bb4

bb3:
  call void @use2()
  br label %bb4

bb4:
  %phi = phi i256 [ 152, %bb1 ], [ 42, %bb3 ], [ 0, %bb2 ]
  ret i256 %phi
}

Following assembly is generated:

test:                                   ; @test
	sub.s!	4, r1, r0
	jump.gt	@.BB0_2
	near_call	r0, @use1, @DEFAULT_UNWIND
	add	152, r0, r1
	ret
.BB0_2:                                 ; %bb2
	sub.s!	6, r1, r0
	add	r0, r0, r1
	jump.lt	@.BB0_4
	near_call	r0, @use2, @DEFAULT_UNWIND
	add	42, r0, r1
.BB0_4:                                 ; %bb4
	ret

In this case, if we adjust both compares to:

	sub.s!	5, r1, r0
	jump.ge	@.BB0_2
        ...
	sub.s!	5, r1, r0
	jump.le	@.BB0_4

Second sub.s! 5, r1, r0 is not needed, and MachineCSE would eliminate it.

This optimization opportunity is inspired by AArch64ConditionOptimizer, so implement the same optimization for EraVM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant