Issue 243593
Summary [InstCombine] UNREACHABLE "Unhandled icmp predicate!" in foldICmpDivConstant for `icmp samesign sge (sdiv X, C), 0`
Labels new issue
Assignees
Reporter arie2026
    An `icmp samesign sge` of an `sdiv` by a constant against 0 reaches `foldICmpDivConstant` with the non-strict predicate
SGE, and the final `switch (Pred)` there hits `llvm_unreachable`.

Reproducer (`opt -passes=instcombine`):
```llvm
define i1 @f(i32 %x) {
  %q = sdiv i32 %x, -2
  %c = icmp samesign sge i32 %q, 0
  ret i1 %c
}
```

```
$ opt -passes=instcombine -S test.ll
Unhandled icmp predicate!
UNREACHABLE executed at llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp:3015!
Stack dump:
1.      Running pass "function(instcombine<max-iterations=1;verify-fixpoint>)" on module "test.ll"
2.      Running pass "instcombine<max-iterations=1;verify-fixpoint>" on function "f"
 #9 llvm::llvm_unreachable_internal(char const*, char const*, unsigned int)
#10 llvm::InstCombinerImpl::foldICmpDivConstant(llvm::ICmpInst&, llvm::BinaryOperator*, llvm::APInt const&)
#11 llvm::InstCombinerImpl::foldICmpBinOpWithConstant(llvm::ICmpInst&, llvm::BinaryOperator*, llvm::APInt const&)
#12 llvm::InstCombinerImpl::foldICmpInstWithConstant(llvm::ICmpInst&)
#13 llvm::InstCombinerImpl::visitICmpInst(llvm::ICmpInst&)
#14 llvm::InstCombinerImpl::run()
#15 combineInstructionsOverFunction(...)
#16 llvm::InstCombinePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&)
```

`sdiv i32 %x, 2` crashes the same way. These fold without crashing: the same compare without `samesign`,
`samesign sgt %q, -1`, `samesign sle %q, 0`, and `samesign uge (udiv %x, 3), 1`.

Expected: no crash (the compare folds like the non-`samesign` version, or the fold bails out).

Notes: `getFlippedStrictnessPredicateAndConstant` now refuses to flip `samesign sge X, 0` to `sgt X, -1`, because that
would cross the sign boundary. So `canonicalizeCmpWithConstant` leaves the non-strict predicate in place, but
`foldICmpDivConstant` only handles EQ/NE/ULT/SLT/UGT/SGT. This may have been exposed by #209097 (not bisected).
Possible fixes: bail out in `foldICmpDivConstant` for non-strict predicates, or drop `samesign` and canonicalize.

Found with a mutation-based IR fuzzer. Triage, test-case minimization and parts of this write-up were assisted by
an LLM; verified the reproducer on b23814056.

Assisted-by: Claude (Anthropic)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to