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

[ValueTracking] Missed optimization: indicate knownbits from dominating condition switch(trunc(a)) #84937

Open
XChy opened this issue Mar 12, 2024 · 4 comments

Comments

@XChy
Copy link
Member

XChy commented Mar 12, 2024

Alive2 proof: https://alive2.llvm.org/ce/z/NdBYVJ

Motivating example

define i32 @src(i64 %addr, i32 %0) {
entry:
  %trunc = trunc i32 %0 to i16
  switch i16 %trunc, label %fail [
    i16 267, label %common.ret
    i16 204, label %common.ret
    i16 263, label %common.ret
    i16 1, label %sw.bb
  ]

common.ret:
  ret i32 0

sw.bb:
  %and75 = and i32 %0, 65535
  ret i32 %and75

fail:
  call void @dummy()
  br label %common.ret
}

can be folded to:

define i32 @tgt(i64 %addr, i32 %0) {
entry:
  %trunc = trunc i32 %0 to i16
  switch i16 %trunc, label %fail [
    i16 267, label %common.ret
    i16 204, label %common.ret
    i16 263, label %common.ret
    i16 1, label %sw.bb
  ]

common.ret:
  ret i32 0

sw.bb:
  ret i32 1

fail:
  call void @dummy()
  br label %common.ret
}

Real-world motivation

This snippet of IR is derived from qemu/hw/core/loader.c@load_aout (after O3 pipeline).
The example above is a reduced version. If you're interested in the original suboptimal IR and optimal IR, see also:https://godbolt.org/z/Woa6n1rW1

Let me know if you can confirm that it's an optimization opportunity, thanks.

@nikic
Copy link
Contributor

nikic commented Mar 12, 2024

Hm, I thought that @dtcxzyw recently added support for this.

@XChy
Copy link
Member Author

XChy commented Mar 12, 2024

Hm, I thought that @dtcxzyw recently added support for this.

Oh, sorry for duplicating it. It seems that opt on Alive2 doesn't optimize the case without switch, but godbolt does: https://godbolt.org/z/KxMn1Mbnd
However, the switch case in real world isn't optimized: https://godbolt.org/z/Wcz73rdP6

Edit: And could you post the link of patch posted by @dtcxzyw? I can't find it.

@nikic
Copy link
Contributor

nikic commented Mar 12, 2024

@XChy The patch is #82803. It landed recently, and I think alive2 updates fairly rarely.

@XChy XChy changed the title Missed optimization: indicate knownbits from dominating condition cmp(trunc(a)) [ValueTracking] Missed optimization: indicate knownbits from dominating condition switch(trunc(a)) Mar 12, 2024
@dtcxzyw
Copy link
Member

dtcxzyw commented Mar 12, 2024

Hm, I thought that @dtcxzyw recently added support for this.

Emm, currently DomConditionCache doesn't support switches. @nikic Do you plan to fix it?

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

4 participants