[CodeGenPrepare] Infinite loop due to GEP unmerging and AddrSinkUsingGEPs
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
Given the sample:
```llvm
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32"
target triple = "arm64-apple-macosx12.0.0"
@sometable = global [2 x ptr] [ptr blockaddress(@func, %5), ptr blockaddress(@func, %4)]
define void @func() {
%2 = alloca i8
%3 = getelementptr i8, ptr %2, i64 16
indirectbr ptr null, [label %4, label %5]
4: ; preds = %1
br label %6
5: ; preds = %1
call void @bar(ptr %3)
ret void
6: ; preds = %4
%7 = getelementptr i8, ptr %2, i64 40
store i32 0, ptr %7, align 8
ret void
}
declare void @bar(ptr)
```
GEP Optimizations in CodeGenPrepare will cause an infinite loop during lowering: https://godbolt.org/z/svP17G1Tv .
The first optimization (gep unmerging across indirectbr) will unmerge the second gep as follows:
```llvm
%7 = getelementptr i8, ptr %3, i64 24
```
The second optimization (AddrSinkUsingGEPs) will undo the optimization in order to sink the address calculation right before the store:
```llvm
%sunkaddr = getelementptr i8, ptr %2, i64 40
store i32 0, ptr %sunkaddr, align 8
```
With each transformation `optimizeBlock` will return true as a modification of the ir code occured, thus causing a never ending loop execution in the `while (MadeChange)` loop of `bool CodeGenPrepare::_run(Function &F)`.
Contributor guide
Assessment
This issue has not been assessed yet.