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: Compute known bits doesn't leverage range information on gep #46585

Open
qcolombet opened this issue Aug 19, 2020 · 4 comments
Open
Labels
bugzilla Issues migrated from bugzilla llvm:optimizations

Comments

@qcolombet
Copy link
Collaborator

Bugzilla Link 47241
Version trunk
OS All
Attachments Case that gets simplified, Case that doesn't get simplified (reproducer)
CC @fhahn

Extended Description

The ValueTracking analysis only tries to fill up trailing zeros when going through GEPs.
In particular, the analysis doesn't take advantage of the range information that may be available, resulting and suboptimal code.

  • Example *

Consider the following snippet:

define i1 @​test4_gep(i64* %ptr) {
entry:
  %val = load volatile i64, i64* %ptr, !range !{i64 64, i64 65536}
  %valptr = inttoptr i64 %val to float*
  %gep = getelementptr float, float* %valptr, i64 128 
  %res = icmp ugt float* %gep, inttoptr (i64 523 to float*)
  ret i1 %res
}

Using the range information this example can be simplified into the following code, since 64 + 4*128 is guaranteed to be bigger than 523:

define i1 @​test4_gep(i64* %ptr) {
entry:
  %val = load volatile i64, i64* %ptr, !range !{i64 64, i64 65536}
  ret i1 true
}

But given ValueTracking doesn't leverage the range information, no simplification happens.

Now, rewrite this code with an add like so:

define i1 @​test3_add(i64* %ptr) {
entry:
  %val = load volatile i64, i64* %ptr, !range !{i64 64, i64 65536}
  %valPlus512 = add i64 %val, 512
  %res = icmp ugt i64 %valPlus512, 523
  ret i1 %res
}

And ValueTracking provides the proper known bits and the simplification can happen.

  • To Reproduce *

Not simplified.

opt -S -instcombine value_tracking_gep.ll -o -

Simplified.

opt -S -instcombine value_tracking_add.ll -o -

Note: I have to use a volatile load to prevent instcombine for dropping the range information. I'll file a separate PR for that.

@qcolombet
Copy link
Collaborator Author

File llvm/llvm-bugzilla-archive#47243 for the other issue.

@qcolombet
Copy link
Collaborator Author

@qcolombet
Copy link
Collaborator Author

Created attachment 23869 [details]
Tentative fix for the computeKnownBits part

That's https://reviews.llvm.org/D86364

@qcolombet
Copy link
Collaborator Author

As of e97e985, compute known bits performs the proper analysis on gep.
Next step is to teach instcombine that inttoptr (i64 523 to float*) is a compile time constant.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla llvm:optimizations
Projects
None yet
Development

No branches or pull requests

2 participants