Bug type: crash
Component: IR
Bug location: llvm/lib/IR/ConstantFold.cpp: llvm::ConstantFoldCompareInstruction
Issue title: [InstCombine] ICmp i1 X, C not simplified as expected. with opt "-passes=lto<O3>"
Labels: crash-on-valid
llvm:ir
Reported by: ewlu

Full error message:
```
ICmp i1 X, C not simplified as expected.
UNREACHABLE executed at /scratch/ewlu/daily-upstream-build/llvm/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp:7182!
```

Testcase:
```llvm ir
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
target triple = "riscv64-unknown-linux-gnu"

@a = external global i8

define void @c() #0 {
entry:
  %0 = zext <vscale x 8 x i1> insertelement (<vscale x 8 x i1> poison, i1 true, i64 0) to <vscale x 8 x i32>
  %1 = icmp ne <vscale x 8 x i32> %0, zeroinitializer
  %2 = zext <vscale x 8 x i1> %1 to <vscale x 8 x i8>
  %3 = call i8 @llvm.vector.reduce.xor.nxv8i8(<vscale x 8 x i8> %2)
  store i8 %3, ptr @a, align 1
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(read)
declare <vscale x 8 x i16> @llvm.masked.gather.nxv8i16.nxv8p0(<vscale x 8 x ptr>, i32 immarg, <vscale x 8 x i1>, <vscale x 8 x i16>) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <vscale x 8 x i32> @llvm.smin.nxv8i32(<vscale x 8 x i32>, <vscale x 8 x i32>) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i8 @llvm.vector.reduce.xor.nxv8i8(<vscale x 8 x i8>) #2

define i32 @main() {
entry:
  ret i32 0
}

attributes #0 = { "target-features"="+v" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(read) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
```
This might not be the fully reduced version. `llvm-reduce` aborted mid reduce for me

Commands/backtrace:
```
$ /scratch/ewlu/daily-upstream-build/build-gcv/build-llvm-linux/bin/opt "-passes=lto<O3>" reduced.ll
WARNING: You're attempting to print out a bitcode file.
This is inadvisable as it may cause display problems. If
you REALLY want to taste LLVM bitcode first-hand, you
can force output with the `-f' option.

ICmp i1 X, C not simplified as expected.
UNREACHABLE executed at /scratch/ewlu/daily-upstream-build/llvm/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp:7182!
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /scratch/ewlu/daily-upstream-build/build-gcv/build-llvm-linux/bin/opt -passes=lto<O3> reduced.ll
1.      Running pass "function<eager-inv>(instcombine<max-iterations=1;no-verify-fixpoint>,aggressive-instcombine)" on module "reduced.ll"
2.      Running pass "instcombine<max-iterations=1;no-verify-fixpoint>" on function "c"
 #0 0x000058be7f45a172 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/scratch/ewlu/daily-upstream-build/build-gcv/build-llvm-linux/bin/opt+0x35a5172)
 #1 0x000058be7f4571df llvm::sys::RunSignalHandlers() (/scratch/ewlu/daily-upstream-build/build-gcv/build-llvm-linux/bin/opt+0x35a21df)
 #2 0x000058be7f457324 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #3 0x000077fe5fe45330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
 #4 0x000077fe5fe9eb2c __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #5 0x000077fe5fe9eb2c __pthread_kill_internal ./nptl/pthread_kill.c:78:10
 #6 0x000077fe5fe9eb2c pthread_kill ./nptl/pthread_kill.c:89:10
 #7 0x000077fe5fe4527e raise ./signal/../sysdeps/posix/raise.c:27:6
 #8 0x000077fe5fe288ff abort ./stdlib/abort.c:81:7
 #9 0x000058be7f38585e (/scratch/ewlu/daily-upstream-build/build-gcv/build-llvm-linux/bin/opt+0x34d085e)
#10 0x000058be7e3f2501 llvm::InstCombinerImpl::visitICmpInst(llvm::ICmpInst&) (/scratch/ewlu/daily-upstream-build/build-gcv/build-llvm-linux/bin/opt+0x253d501)
#11 0x000058be7e3464fa llvm::InstCombinerImpl::run() (/scratch/ewlu/daily-upstream-build/build-gcv/build-llvm-linux/bin/opt+0x24914fa)
#12 0x000058be7e347d90 combineInstructionsOverFunction(llvm::Function&, llvm::InstructionWorklist&, llvm::AAResults*, llvm::AssumptionCache&, llvm::TargetLibraryInfo&, llvm::TargetTransformInfo&, llvm::DominatorTree&, llvm::OptimizationRemarkEmitter&, llvm::BlockFrequencyInfo*, llvm::BranchProbabilityInfo*, llvm::ProfileSummaryInfo*, llvm::InstCombineOptions const&) InstructionCombining.cpp:0:0
#13 0x000058be7e3491e2 llvm::InstCombinePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/scratch/ewlu/daily-upstream-build/build-gcv/build-llvm-linux/bin/opt+0x24941e2)
#14 0x000058be7c9bb605 llvm::detail::PassModel<llvm::Function, llvm::InstCombinePass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/scratch/ewlu/daily-upstream-build/build-gcv/build-llvm-linux/bin/opt+0xb06605)
#15 0x000058be7f2430fa llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/scratch/ewlu/daily-upstream-build/build-gcv/build-llvm-linux/bin/opt+0x338e0fa)
#16 0x000058be7c9bdc55 llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/scratch/ewlu/daily-upstream-build/build-gcv/build-llvm-linux/bin/opt+0xb08c55)
#17 0x000058be7f2435e4 llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/scratch/ewlu/daily-upstream-build/build-gcv/build-llvm-linux/bin/opt+0x338e5e4)
#18 0x000058be7c9b9895 llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/scratch/ewlu/daily-upstream-build/build-gcv/build-llvm-linux/bin/opt+0xb04895)
#19 0x000058be7f241f6d llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/scratch/ewlu/daily-upstream-build/build-gcv/build-llvm-linux/bin/opt+0x338cf6d)
#20 0x000058be7c66b241 llvm::runPassPipeline(llvm::StringRef, llvm::Module&, llvm::TargetMachine*, llvm::TargetLibraryInfoImpl*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::StringRef, llvm::ArrayRef<llvm::PassPlugin>, llvm::ArrayRef<std::function<void (llvm::PassBuilder&)>>, llvm::opt_tool::OutputKind, llvm::opt_tool::VerifierKind, bool, bool, bool, bool, bool, bool, bool) (/scratch/ewlu/daily-upstream-build/build-gcv/build-llvm-linux/bin/opt+0x7b6241)
#21 0x000058be7c65cd7c optMain (/scratch/ewlu/daily-upstream-build/build-gcv/build-llvm-linux/bin/opt+0x7a7d7c)
#22 0x000077fe5fe2a1ca __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#23 0x000077fe5fe2a28b call_init ./csu/../csu/libc-start.c:128:20
#24 0x000077fe5fe2a28b __libc_start_main ./csu/../csu/libc-start.c:347:5
#25 0x000058be7c6533d5 _start (/scratch/ewlu/daily-upstream-build/build-gcv/build-llvm-linux/bin/opt+0x79e3d5)
Aborted (core dumped)
```

Godbolt: https://godbolt.org/z/5cxdvhhGK

Found via fuzzer (C program before reduction)


Fix patch:
commit d9a5f7b118bfc9ab4b7325fae25073729d8e08be
Author: Yingwei Zheng <dtcxzyw2333@gmail.com>
Date:   Tue Jun 3 19:51:21 2025 +0800

    [ConstantFold] Constant fold icmp of boolean scalable vectors (#142528)
    
    Closes https://github.com/llvm/llvm-project/issues/142447.

diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index 7e5fda229b85..b9db402fe956 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -1149,10 +1149,10 @@ Constant *llvm::ConstantFoldCompareInstruction(CmpInst::Predicate Predicate,
   }
 
   // If the comparison is a comparison between two i1's, simplify it.
-  if (C1->getType()->isIntegerTy(1)) {
+  if (C1->getType()->isIntOrIntVectorTy(1)) {
     switch (Predicate) {
     case ICmpInst::ICMP_EQ:
-      if (isa<ConstantInt>(C2))
+      if (isa<ConstantExpr>(C1))
         return ConstantExpr::getXor(C1, ConstantExpr::getNot(C2));
       return ConstantExpr::getXor(ConstantExpr::getNot(C1), C2);
     case ICmpInst::ICMP_NE:


Regression tests:
file: llvm/test/Transforms/InstSimplify/compare.ll
commands: opt < %s -passes=instcombine<no-verify-fixpoint> -S
tests: test_name: icmp_ne_i1_vec_constant_expr
test_body: target datalayout = "p:32:32-p1:64:64"

define <vscale x 8 x i1> @icmp_ne_i1_vec_constant_expr() {
entry:
  %cmp = icmp ne <vscale x 8 x i1> insertelement (<vscale x 8 x i1> poison, i1 true, i64 0), zeroinitializer
  ret <vscale x 8 x i1> %cmp
}

test_name: icmp_eq_i1_vec_constant_expr_commuted
test_body: target datalayout = "p:32:32-p1:64:64"

define <vscale x 8 x i1> @icmp_eq_i1_vec_constant_expr_commuted() {
entry:
  %cmp = icmp eq <vscale x 8 x i1> zeroinitializer, insertelement (<vscale x 8 x i1> poison, i1 true, i64 0)
  ret <vscale x 8 x i1> %cmp
}

test_name: icmp_eq_false_by_trunc
test_body: target datalayout = "p:32:32-p1:64:64"

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write)
declare void @llvm.assume(i1 noundef) #0

define i1 @icmp_eq_false_by_trunc(i8 %x) {
  %trunc = trunc i8 %x to i1
  %not = xor i1 %trunc, true
  call void @llvm.assume(i1 %not)
  %cmp = icmp eq i8 %x, 1
  ret i1 %cmp
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write) }