<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/58580>58580</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[SimplyCFG] Assertion `IterCnt++ < 1000 && "Iterative simplification didn't converge!"` Fails
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
HazyFish
</td>
</tr>
</table>
<pre>
## Description
Assertion `IterCnt++ < 1000 && "Iterative simplification didn't converge!"` fails during pass `SimplyCFG` for the following input when targeting `aarch64`.
The problem does not exist when targeting `x86_64` / `riscv64` / `nvptx64`.
## Minimal Reproduction
https://godbolt.org/z/6dc6hchqG
### Code
```ll
define void @f(i64 %0) {
BB:
br label %BB1
BB1: ; preds = %BB5, %BB1, %BB
%C = icmp eq i64 %0, -9223372036854775808
br i1 %C, label %BB1, label %BB2
BB2: ; preds = %BB4, %BB3, %BB2, %BB1
%C1 = icmp eq i64 %0, 9223372036854775807
br i1 %C1, label %BB2, label %BB3
BB3: ; preds = %BB2
br i1 %C, label %BB2, label %BB4
BB4: ; preds = %BB3
%C2 = icmp ne i64 %0, 1
br i1 %C2, label %BB2, label %BB5
BB5: ; preds = %BB4
%S = shufflevector <2 x i32> zeroinitializer, <2 x i32> zeroinitializer, <2 x i32> <i32 1, i32 0>
store <2 x i32> %S, ptr undef
ret void
}
```
### Stack Trace
```
llc: /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp:240: bool iterativelySimplifyCFG(llvm::Function &, const llvm::TargetTransformInfo &, llvm::DomTreeUpdater *, const llvm::SimplifyCFGOptions &): Assertion `IterCnt++ < 1000 && "Iterative simplification didn't converge!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0. Program arguments: ./llvm-project/build-debug/bin/llc -mtriple=aarch64 ./crash-reports/dagisel-aarch64/2.ll
1. Running pass 'Function Pass Manager' on module './crash-reports/dagisel-aarch64/2.ll'.
2. Running pass 'Simplify the CFG' on function '@f'
#0 0x000000000477b25a llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Support/Unix/Signals.inc:569:11
#1 0x000000000477b40b PrintStackTraceSignalHandler(void*) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Support/Unix/Signals.inc:636:1
#2 0x0000000004779a56 llvm::sys::RunSignalHandlers() /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Support/Signals.cpp:104:5
#3 0x000000000477bb35 SignalHandler(int) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Support/Unix/Signals.inc:407:1
#4 0x00007f12b7104980 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12980)
#5 0x00007f12b5ff4e87 raise /build/glibc-CVJwZb/glibc-2.27/signal/../sysdeps/unix/sysv/linux/raise.c:51:0
#6 0x00007f12b5ff67f1 abort /build/glibc-CVJwZb/glibc-2.27/stdlib/abort.c:81:0
#7 0x00007f12b5fe63fa __assert_fail_base /build/glibc-CVJwZb/glibc-2.27/assert/assert.c:89:0
#8 0x00007f12b5fe6472 (/lib/x86_64-linux-gnu/libc.so.6+0x30472)
#9 0x000000000402db98 iterativelySimplifyCFG(llvm::Function&, llvm::TargetTransformInfo const&, llvm::DomTreeUpdater*, llvm::SimplifyCFGOptions const&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp:241:17
#10 0x000000000402d3ed simplifyFunctionCFGImpl(llvm::Function&, llvm::TargetTransformInfo const&, llvm::DominatorTree*, llvm::SimplifyCFGOptions const&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp:273:18
#11 0x000000000402cfae simplifyFunctionCFG(llvm::Function&, llvm::TargetTransformInfo const&, llvm::DominatorTree*, llvm::SimplifyCFGOptions const&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp:301:8
#12 0x000000000402ebbc (anonymous namespace)::CFGSimplifyPass::runOnFunction(llvm::Function&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp:407:5
#13 0x0000000003c2ae86 llvm::FPPassManager::runOnFunction(llvm::Function&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1430:23
#14 0x0000000003c2fcb2 llvm::FPPassManager::runOnModule(llvm::Module&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1476:16
#15 0x0000000003c2b759 (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1545:23
#16 0x0000000003c2b2cd llvm::legacy::PassManagerImpl::run(llvm::Module&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:535:16
#17 0x0000000003c2ff91 llvm::legacy::PassManager::run(llvm::Module&) /home/henry/aflplusplus-isel/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1672:3
#18 0x0000000000d3f3fc compileModule(char**, llvm::LLVMContext&) /home/henry/aflplusplus-isel/llvm-project/llvm/tools/llc/llc.cpp:737:41
#19 0x0000000000d3d7a2 main /home/henry/aflplusplus-isel/llvm-project/llvm/tools/llc/llc.cpp:418:13
#20 0x00007f12b5fd7c87 __libc_start_main /build/glibc-CVJwZb/glibc-2.27/csu/../csu/libc-start.c:344:0
#21 0x0000000000d3cfaa _start (./llvm-project/build-debug/bin/llc+0xd3cfaa)
```
## Cause
[`SimplifyCFGOpt::FoldValueComparisonIntoPredecessors`](https://github.com/llvm/llvm-project/blob/dd00c4db747e27f53a5621a8b9c50711f00894ef/llvm/lib/Transforms/Utils/SimplifyCFG.cpp#L1405), [`SimplifyCFGOpt::TurnSwitchRangeIntoICmp`](https://github.com/llvm/llvm-project/blob/dd00c4db747e27f53a5621a8b9c50711f00894ef/llvm/lib/Transforms/Utils/SimplifyCFG.cpp#L5254), [`SimplifyCondBranchToCondBranch`](https://github.com/llvm/llvm-project/blob/dd00c4db747e27f53a5621a8b9c50711f00894ef/llvm/lib/Transforms/Utils/SimplifyCFG.cpp#L4161) are called for infinite number of times, switching IR between 2 forms (after iteration 2 and after iteration 3) shown below.
```ll
; before Simply CFG
define void @f(i64 %0) {
BB:
br label %BB1
BB1: ; preds = %BB1, %BB
%C = icmp eq i64 %0, -9223372036854775808
br i1 %C, label %BB1, label %BB2
BB2: ; preds = %BB4, %BB3, %BB2, %BB1
%C1 = icmp eq i64 %0, 9223372036854775807
br i1 %C1, label %BB2, label %BB3
BB3: ; preds = %BB2
br i1 %C, label %BB2, label %BB4
BB4: ; preds = %BB3
%C2 = icmp ne i64 %0, 1
br i1 %C2, label %BB2, label %BB5
BB5: ; preds = %BB4
%S = shufflevector <2 x i32> zeroinitializer, <2 x i32> zeroinitializer, <2 x i32> <i32 1, i32 0>
unreachable
}
; iteration 1:
; SimplifyCFGOpt::FoldValueComparisonIntoPredecessors returned true
; SimplifyCondBranchToCondBranch returned true
; after iteration 1
define void @f(i64 %0) {
BB:
br label %BB1
BB1: ; preds = %BB1, %BB
%C = icmp eq i64 %0, -9223372036854775808
br i1 %C, label %BB1, label %BB2
BB2: ; preds = %BB2, %BB3, %BB1
%C1 = icmp eq i64 %0, 9223372036854775807
%brmerge = or i1 %C1, %C
br i1 %brmerge, label %BB2, label %BB4
BB3: ; preds = %BB4
br i1 %C, label %BB2, label %BB4
BB4: ; preds = %BB2, %BB3
switch i64 %0, label %BB3 [
i64 9223372036854775807, label %infloop
]
infloop: ; preds = %BB4, %infloop
br label %infloop
}
; iteration 2:
; SimplifyCFGOpt::FoldValueComparisonIntoPredecessors returned true
; after iteration 2
define void @f(i64 %0) {
BB:
br label %BB1
BB1: ; preds = %BB1, %BB
%C = icmp eq i64 %0, -9223372036854775808
br i1 %C, label %BB1, label %BB2
BB2: ; preds = %BB2, %BB2, %BB3, %BB1
switch i64 %0, label %BB3 [
i64 -9223372036854775808, label %BB2
i64 9223372036854775807, label %BB2
]
BB3: ; preds = %BB2, %BB4
br i1 %C, label %BB2, label %BB4
BB4: ; preds = %BB3
%cond = icmp eq i64 %0, 9223372036854775807
br i1 %cond, label %infloop, label %BB3
infloop: ; preds = %BB4, %infloop
br label %infloop
}
; iteration 3:
; SimplifyCFGOpt::TurnSwitchRangeIntoICmp returned true
; SimplifyCondBranchToCondBranch returned true
; after iteration 3
define void @f(i64 %0) {
BB:
br label %BB1
BB1: ; preds = %BB1, %BB
%C = icmp eq i64 %0, -9223372036854775808
br i1 %C, label %BB1, label %BB2
BB2: ; preds = %BB2, %BB3, %BB1
%.off = add i64 %0, -9223372036854775807
%switch = icmp ult i64 %.off, 2
%brmerge = or i1 %switch, %C
br i1 %brmerge, label %BB2, label %BB4
BB3: ; preds = %BB4
br i1 %C, label %BB2, label %BB4
BB4: ; preds = %BB2, %BB3
%cond = icmp eq i64 %0, 9223372036854775807
br i1 %cond, label %infloop, label %BB3
infloop: ; preds = %BB4, %infloop
br label %infloop
}
; iteration 4:
; SimplifyCFGOpt::FoldValueComparisonIntoPredecessors returned true
; after iteration 4, the code is the same as after iteration 2
; loop forever until assert(IterCnt++ < 1000 && "Iterative simplification didn't converge!") breaks
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztW1tT47gS_jXhRUXKlu8PPEAYdjk1U0sNzDycF0q25URnbSlryVzm12-3HMcXwm0GGKoOqZDIcqu7v1aruy2FVOW3BzPqwZscc53VYm2EkjPneOYctp-HWvMaO8ksdE4NrxfSzOgRvMnMWxDXcRwyoyG84YsiATPiihMtqnUpCpExOzgXuZzRyJBMySteL_mMukAPPEnBRKlJ3tRCLsmaaY2SznH47eLkD0uhamJWHL7LUl0jmZDrxpDrFZfEMOBmsBNIGauzVehDaz4EcQGD17VKS16RXHFNpDKE3wi9i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