LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 44321 - InstCombine incorrectly folds 'gep(bitcast ptr), idx' into 'gep ptr, idx'
Summary: InstCombine incorrectly folds 'gep(bitcast ptr), idx' into 'gep ptr, idx'
Status: RESOLVED FIXED
Alias: None
Product: libraries
Classification: Unclassified
Component: Transformation Utilities (show other bugs)
Version: trunk
Hardware: PC All
: P normal
Assignee: Unassigned LLVM Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-12-17 04:59 PST by Juneyoung Lee
Modified: 2019-12-21 07:37 PST (History)
4 users (show)

See Also:
Fixed By Commit(s): 79c7fa31f3aa


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Juneyoung Lee 2019-12-17 04:59:58 PST
```
$ cat gep-vector.ll
define i32* @bitcast_vec_to_array_gep(<7 x i32>* %x, i64 %y, i64 %z) {
  %arr_ptr = bitcast <7 x i32>* %x to [7 x i32]*
  %gep = getelementptr [7 x i32], [7 x i32]* %arr_ptr, i64 %y, i64 %z
  ret i32* %gep
}
$ opt -instcombine gep-vector.ll -S -o -
define i32* @bitcast_vec_to_array_gep(<7 x i32>* %x, i64 %y, i64 %z) {
  %gep = getelementptr <7 x i32>, <7 x i32>* %x, i64 %y, i64 %z
  ret i32* %gep
}
```

This is incorrect because DataLayout::getTypeAllocSize(< 7 x i32 >) and getTypeAllocSize([ 7 x i32 ]) may differ.

This can be double-checked by emitting assembly code before/after optimization.
https://godbolt.org/z/xB-p5u
Before optimization, rax = rdi + 28 * rsi + 4 * rdx
After instcombine, rax = rdi + 32 * rsi + 4 * rdx.
Comment 1 Juneyoung Lee 2019-12-17 05:00:16 PST
The t
Comment 2 Juneyoung Lee 2019-12-17 05:01:03 PST
Sorry for my mistake :(
The test was excerpted from test/Transforms/InstCombine/gep-vector.ll.
Comment 3 Eli Friedman 2019-12-17 13:23:06 PST
I agree this is a bug.
Comment 4 Sanjay Patel 2019-12-20 07:00:46 PST
I introduced the bug here:
https://reviews.llvm.org/D44833

Using weird types in the tests helped expose it. In the common case where we have a power-of-2 vector/array and the alloc-sizes are the same, I think the transform should still be ok. I'll post a patch for review.
Comment 5 Sanjay Patel 2019-12-20 08:13:19 PST
https://reviews.llvm.org/D71771
Comment 6 Sanjay Patel 2019-12-21 07:36:38 PST
Should be fixed with:
tps://reviews.llvm.org/rG79c7fa31f3aa
Comment 7 Sanjay Patel 2019-12-21 07:37:19 PST
Lost some chars on that link:
https://reviews.llvm.org/rG79c7fa31f3aa