-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Missing fold &a[i] - &a[j] => i - j #46774
Comments
In this case, instcombine is converting pointer math to straight math as: define i64 @src(i64 %x, i64 %y) { define i64 @tgt(i64 %x, i64 %y) { https://alive2.llvm.org/ce/z/VFCZgB And that's correct...but once we do that, it seems like there's no way to recover the lost information that we don't the need shifts? We need to get to the 'sub'-only form directly. |
Yeah, I was playing with that and seems like we need to do it on GEP-level. |
I would need to re-read the rules for 'inbounds' but if that translates to 'nuw nsw' on the mul/shift, then we should be able to get this in 2 steps. We seem to also miss this: |
@a = dso_local global [64 x i64] zeroinitializer define i64 @src(i64 %x, i64 %y) { define i64 @tgt(i64 %x, i64 %y) { So yeah, NSW is correct here. And then... this fold kicks in. define i64 @src(i64 %x, i64 %y) { define i64 @tgt(i64 %x, i64 %y) { So to sum up, we just need to put nsw flags on those ops? |
That would be good, but I suspect that we still have multiple points of failure on this example - a first hack in "OptimizePointerDifference" didn't do the job for me. |
Hm, that is strange. Current codegen + nsw on shl: define i64 @src(i64 %x, i64 %y) { opt -instsimplify produces: define i64 @src(i64 %x, i64 %y) local_unnamed_addr #0 { |
Probably won't do anything by itself, but here's one step: |
And seemingly one step back: |
Re-did the wrapping flags patch: And re-arranged the folds (we still miss the example in comment 3): So that should take care of the problem in the description. |
Extended Description
typedef SIZE_TYPE size_t;
size_t a[64];
size_t f(size_t b, size_t c)
{
return &a[b] - &a[c];
}
Clang:
f(unsigned long, unsigned long): # @f(unsigned long, unsigned long)
sub rdi, rsi
lea rax, [8*rdi]
sar rax, 3
ret
a:
.zero 512
GCC:
f(unsigned long, unsigned long):
mov rax, rdi
sub rax, rsi
ret
a:
.zero 512
https://godbolt.org/z/7M8o5a
The text was updated successfully, but these errors were encountered: