Skip to content

[SimplifyCFG] Hoist common instructions on the switch instruction. #64031

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

Closed
dianqk opened this issue Jul 22, 2023 · 1 comment
Closed

[SimplifyCFG] Hoist common instructions on the switch instruction. #64031

dianqk opened this issue Jul 22, 2023 · 1 comment

Comments

@dianqk
Copy link
Member

dianqk commented Jul 22, 2023

https://alive2.llvm.org/ce/z/wGyVbz

define i1 @src(i64 %a, i64 %b, i64 %c, i64 %d) {
start:
  %_5 = icmp eq i64 %a, %c
  br i1 %_5, label %bb2, label %bb3

bb2:                                              ; preds = %start
  switch i64 %a, label %bb5 [
    i64 0, label %bb4
    i64 1, label %bb6
    i64 2, label %bb7
  ]

bb3:                                              ; preds = %bb4, %bb6, %bb7, %start
  %_0.0 = phi i1 [ false, %start ], [ %2, %bb7 ], [ %1, %bb6 ], [ %0, %bb4 ]
  ret i1 %_0.0

bb5:                                              ; preds = %bb2
  unreachable

bb4:                                              ; preds = %bb2
  %0 = icmp eq i64 %b, %d
  br label %bb3

bb6:                                              ; preds = %bb2
  %1 = icmp eq i64 %b, %d
  br label %bb3

bb7:                                              ; preds = %bb2
  %2 = icmp eq i64 %b, %d
  br label %bb3
}

We could hoist common instructions, like this:

define i1 @tgt(i64 %a, i64 %b, i64 %c, i64 %d) {
start:
  %_5 = icmp eq i64 %a, %c
  br i1 %_5, label %bb2, label %bb3

bb2:                                              ; preds = %start
  %0 = icmp eq i64 %b, %d
  switch i64 %a, label %bb5 [
    i64 0, label %bb4
    i64 1, label %bb6
    i64 2, label %bb7
  ]

bb3:                                              ; preds = %bb4, %bb6, %bb7, %start
  %_0.0 = phi i1 [ false, %start ], [ %0, %bb7 ], [ %0, %bb6 ], [ %0, %bb4 ]
  ret i1 %_0.0

bb5:                                              ; preds = %bb2
  unreachable

bb4:                                              ; preds = %bb2
  br label %bb3

bb6:                                              ; preds = %bb2
  br label %bb3

bb7:                                              ; preds = %bb2
  br label %bb3
}

Final optimization is:

define i1 @tgt(i64 %a, i64 %b, i64 %c, i64 %d) {
  %_5 = icmp eq i64 %a, %c
  %0 = icmp eq i64 %b, %d
  %_0.0 = select i1 %_5, i1 %0, i1 false
  ret i1 %_0.0
}
@dianqk
Copy link
Member Author

dianqk commented Sep 20, 2023

96ea48f

@dianqk dianqk closed this as completed Sep 20, 2023
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