Bug type: crash
Component: LoopVectorize
Bug location: llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp: VPTypeAnalysis::inferScalarTypeForRecipe
Issue title: [LoopVectorize][VPlan] Unreachable executed "Unhandled opcode!"
Labels: vectorizers
crash
Reported by: patrick-rivos

Reduced LLVM IR:
```llvm ir
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
target triple = "riscv64-unknown-linux-gnu"

define i32 @main() #0 {
entry:
  br label %for.body

for.cond.cleanup.loopexit:                        ; preds = %cond.end
  ret i32 0

for.body:                                         ; preds = %cond.end, %entry
  %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %cond.end ]
  %conv3 = zext i1 false to i64
  %conv4 = trunc i64 %conv3 to i16
  br i1 false, label %cond.false, label %cond.end

cond.false:                                       ; preds = %for.body
  br label %cond.end

cond.end:                                         ; preds = %cond.false, %for.body
  %indvars.iv.next = add i64 %indvars.iv, 1
  %cmp = icmp ult i64 %indvars.iv, 1
  br i1 %cmp, label %for.body, label %for.cond.cleanup.loopexit
}

attributes #0 = { "target-features"="+64bit,+v" }
```

Backtrace:
```
Unhandled opcode!
UNREACHABLE executed at /scratch/tc-testing/tc-apr-2/llvm/llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp:58!
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /scratch/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt --passes=loop-vectorize reduced.ll
 #0 0x00005f05c836eb60 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/scratch/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt+0x2d84b60)
 #1 0x00005f05c836bf6f llvm::sys::RunSignalHandlers() (/scratch/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt+0x2d81f6f)
 #2 0x00005f05c836c0c5 SignalHandler(int) Signals.cpp:0:0
 #3 0x000071a702442520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x000071a7024969fc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #5 0x000071a7024969fc __pthread_kill_internal ./nptl/pthread_kill.c:78:10
 #6 0x000071a7024969fc pthread_kill ./nptl/pthread_kill.c:89:10
 #7 0x000071a702442476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
 #8 0x000071a7024287f3 abort ./stdlib/abort.c:81:7
 #9 0x00005f05c82a2afe (/scratch/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt+0x2cb8afe)
#10 0x00005f05c74dfe67 llvm::VPTypeAnalysis::inferScalarTypeForRecipe(llvm::VPInstruction const*) (/scratch/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt+0x1ef5e67)
#11 0x00005f05c74dc88c llvm::VPTypeAnalysis::inferScalarType(llvm::VPValue const*) (/scratch/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt+0x1ef288c)
#12 0x00005f05c750790e simplifyRecipes(llvm::VPlan&, llvm::LLVMContext&) VPlanTransforms.cpp:0:0
#13 0x00005f05c750e606 llvm::VPlanTransforms::optimize(llvm::VPlan&, llvm::ScalarEvolution&) (/scratch/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt+0x1f24606)
#14 0x00005f05c73ded66 llvm::LoopVectorizationPlanner::buildVPlansWithVPRecipes(llvm::ElementCount, llvm::ElementCount) (/scratch/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt+0x1df4d66)
#15 0x00005f05c73e5319 llvm::LoopVectorizationPlanner::plan(llvm::ElementCount, unsigned int) (/scratch/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt+0x1dfb319)
#16 0x00005f05c73e88a9 llvm::LoopVectorizePass::processLoop(llvm::Loop*) (/scratch/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt+0x1dfe8a9)
#17 0x00005f05c73eb73e llvm::LoopVectorizePass::runImpl(llvm::Function&, llvm::ScalarEvolution&, llvm::LoopInfo&, llvm::TargetTransformInfo&, llvm::DominatorTree&, llvm::BlockFrequencyInfo*, llvm::TargetLibraryInfo*, llvm::DemandedBits&, llvm::AssumptionCache&, llvm::LoopAccessInfoManager&, llvm::OptimizationRemarkEmitter&, llvm::ProfileSummaryInfo*) (/scratch/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt+0x1e0173e)
#18 0x00005f05c73ec88d llvm::LoopVectorizePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/scratch/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt+0x1e0288d)
#19 0x00005f05c62f3276 llvm::detail::PassModel<llvm::Function, llvm::LoopVectorizePass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/scratch/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt+0xd09276)
#20 0x00005f05c8195141 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/scratch/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt+0x2bab141)
#21 0x00005f05c62eabd6 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/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt+0xd00bd6)
#22 0x00005f05c8193e3b llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/scratch/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt+0x2ba9e3b)
#23 0x00005f05c62f2dc6 llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/scratch/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt+0xd08dc6)
#24 0x00005f05c8191cb1 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/scratch/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt+0x2ba7cb1)
#25 0x00005f05c5b6cce5 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/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt+0x582ce5)
#26 0x00005f05c5b5f316 optMain (/scratch/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt+0x575316)
#27 0x000071a702429d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#28 0x000071a702429e40 call_init ./csu/../csu/libc-start.c:128:20
#29 0x000071a702429e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#30 0x00005f05c5b54f95 _start (/scratch/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt+0x56af95)
zsh: IOT instruction (core dumped)  /scratch/tc-testing/tc-apr-2/build-rv64gcv/build-llvm-linux/bin/opt
```
Godbolt: https://godbolt.org/z/bvY138WfG

Triggered assertion: https://github.com/llvm/llvm-project/blob/f5991161529511ca6ebc058da2a0507c2fc5283e/llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp#L58

Found via fuzzer


Fix patch:
commit adb0126ef11b0083d5a78be1534ccefa53def1cf
Author: Patrick O'Neill <patrick@rivosinc.com>
Date:   Tue Apr 23 07:48:43 2024 -0700

    [VPlan] Add scalar inferencing support for Not and Or insns (#89160)
    
    Fixes #87394.
    
    PR: https://github.com/llvm/llvm-project/pull/89160

diff --git a/llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp b/llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp
index ad4ea648cd61..5f93339083f0 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp
@@ -35,6 +35,7 @@ Type *VPTypeAnalysis::inferScalarTypeForRecipe(const VPInstruction *R) {
     CachedTypes[OtherV] = ResTy;
     return ResTy;
   }
+  case Instruction::Or:
   case Instruction::ICmp:
   case VPInstruction::FirstOrderRecurrenceSplice: {
     Type *ResTy = inferScalarType(R->getOperand(0));
@@ -44,6 +45,12 @@ Type *VPTypeAnalysis::inferScalarTypeForRecipe(const VPInstruction *R) {
     CachedTypes[OtherV] = ResTy;
     return ResTy;
   }
+  case VPInstruction::Not: {
+    Type *ResTy = inferScalarType(R->getOperand(0));
+    assert(IntegerType::get(Ctx, 1) == ResTy &&
+           "unexpected scalar type inferred for operand");
+    return ResTy;
+  }
   case VPInstruction::PtrAdd:
     // Return the type based on the pointer argument (i.e. first operand).
     return inferScalarType(R->getOperand(0));


Regression tests:
file: llvm/test/Transforms/LoopVectorize/vplan-infer-not-or-type.ll
commands: opt < %s -passes=loop-vectorize -S
tests: test_name: foo
test_body: define void @foo(i8 %arg.0, i8 %arg.1) {
entry:
  br label %loop.header

loop.header:                                      ; preds = %latch, %entry
  %incrementor = phi i8 [ %add, %latch ], [ 0, %entry ]
  %and = and i8 %arg.0, %arg.1
  %extract.t = trunc i8 %and to i1
  br i1 %extract.t, label %latch, label %indirect.latch

indirect.latch:                                   ; preds = %loop.header
  br label %latch

latch:                                            ; preds = %indirect.latch, %loop.header
  %add = add i8 %incrementor, 1
  %conv = zext i8 %incrementor to i32
  %cmp = icmp ult i32 %conv, 1
  br i1 %cmp, label %loop.header, label %loop.exit

loop.exit:                                        ; preds = %latch
  ret void
}