Bug type: crash
Component: LoopVectorize
Bug location: llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp: VPReplicateRecipe::computeCost
Issue title: Assertion `(BestFactor.Width == LegacyVF.Width || BestPlan.hasEarlyExit() || ... || planContainsAdditionalSimplifications( getPlanFor(LegacyVF.Width), CostCtx, OrigLoop, LegacyVF.Width)) && " VPlan cost model and legacy cost model disagreed"' failed.
Labels: vectorizers
crash-on-valid
Reported by: TatyanaDoubts

```
opt: /root/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp:7269: llvm::VectorizationFactor llvm::LoopVectorizationPlanner::computeBestVF(): Assertion `(BestFactor.Width == LegacyVF.Width || BestPlan.hasEarlyExit() || !Legal->getLAI()->getSymbolicStrides().empty() || UsesEVLGatherScatter || planContainsAdditionalSimplifications( getPlanFor(BestFactor.Width), CostCtx, OrigLoop, BestFactor.Width) || planContainsAdditionalSimplifications( getPlanFor(LegacyVF.Width), CostCtx, OrigLoop, LegacyVF.Width)) && " VPlan cost model and legacy cost model disagreed"' failed.
```

To reproduce run opt with the test below (-passes=loop-vectorize).
```
; ModuleID = './reduced.ll'
source_filename = "./reduced.ll"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128-ni:1-p2:32:8:8:32-ni:2"
target triple = "x86_64-unknown-linux-gnu"

define void @wombat() #0 gc "statepoint-example" {
bb:
  br label %bb2

bb1:                                              ; preds = %bb2
  %phi = phi i32 [ %or8, %bb2 ]
  ret void

bb2:                                              ; preds = %bb2, %bb
  %phi3 = phi i32 [ 0, %bb ], [ %sdiv, %bb2 ]
  %phi4 = phi i32 [ 0, %bb ], [ %add, %bb2 ]
  %or = or i32 0, 0
  %sdiv = sdiv i32 0, %or
  %sub = sub i32 0, %sdiv
  %or5 = or i32 %sub, %phi3
  %or6 = or i32 %sdiv, 1
  %sdiv7 = sdiv i32 %sdiv, %or6
  %or8 = or i32 %or5, %sdiv7
  %add = add i32 %phi4, 3
  %icmp = icmp ugt i32 %phi4, 35
  br i1 %icmp, label %bb1, label %bb2
}

attributes #0 = { "target-cpu"="skylake-avx512" }
```
Reproducer: https://godbolt.org/z/rzsds3acz

Stack dump:
```
0.	Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/opt -o /app/output.s -S -passes=loop-vectorize <source>
1.	Running pass "function(loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>)" on module "<source>"
2.	Running pass "loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>" on function "wombat"
 #0 0x0000000005a80c68 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x5a80c68)
 #1 0x0000000005a7db44 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #2 0x00007d0067042520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x00007d00670969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #4 0x00007d0067042476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #5 0x00007d00670287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #6 0x00007d006702871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #7 0x00007d0067039e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #8 0x0000000003749648 llvm::LoopVectorizationPlanner::computeBestVF() (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x3749648)
 #9 0x000000000374b5d2 llvm::LoopVectorizePass::processLoop(llvm::Loop*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x374b5d2)
#10 0x000000000374e770 llvm::LoopVectorizePass::runImpl(llvm::Function&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x374e770)
#11 0x000000000374eefd llvm::LoopVectorizePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x374eefd)
#12 0x000000000302291e llvm::detail::PassModel<llvm::Function, llvm::LoopVectorizePass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x302291e)
#13 0x00000000057f51c1 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x57f51c1)
#14 0x0000000000f07e1e llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xf07e1e)
#15 0x00000000057f377a llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x57f377a)
#16 0x000000000096470e llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x96470e)
#17 0x00000000057f3131 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x57f3131)
#18 0x000000000096e8ca 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, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x96e8ca)
#19 0x0000000000962978 optMain (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x962978)
#20 0x00007d0067029d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#21 0x00007d0067029e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#22 0x00000000009595f5 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x9595f5)
Program terminated with signal: SIGSEGV
Compiler returned: 139
```


Fix patch:
commit 69fbab2b72b1c9d5678ba6a6ad234e689ed9bce9
Author: Florian Hahn <flo@fhahn.com>
Date:   Mon Jan 19 21:25:52 2026 +0000

    [VPlan] Fall back to legacy cost if operands may be force-scalarized.
    
    If any of the operands of a VPReplicateRecipe have been
    force-scalarized, then the legacy cost model skips the scalarization
    overhead, but we cannot match this in the VPlan cost model.
    
    Bail out for now in those very rare cases.
    
    Fixes https://github.com/llvm/llvm-project/issues/176720.

diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index 5ef0c0d195eb..64890ac9e9b9 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -3420,6 +3420,20 @@ InstructionCost VPReplicateRecipe::computeCost(ElementCount VF,
     if (isSingleScalar())
       return ScalarCost;
 
+    // If any of the operands is from a different replicate region and has its
+    // cost skipped, it may have been forced to scalar. Fall back to legacy cost
+    // model to avoid cost mis-match.
+    if (any_of(operands(), [&Ctx, VF](VPValue *Op) {
+          auto *PredR = dyn_cast<VPPredInstPHIRecipe>(Op);
+          if (!PredR)
+            return false;
+          return Ctx.skipCostComputation(
+              dyn_cast_or_null<Instruction>(
+                  PredR->getOperand(0)->getUnderlyingValue()),
+              VF.isVector());
+        }))
+      break;
+
     ScalarCost = ScalarCost * VF.getFixedValue() +
                  Ctx.getScalarizationOverhead(Ctx.Types.inferScalarType(this),
                                               to_vector(operands()), VF);


Regression tests:
file: llvm/test/Transforms/LoopVectorize/X86/cost-model.ll
commands: opt < %s -passes=loop-vectorize -mtriple=x86_64-apple-macosx10.8.0 -mcpu=corei7-avx -S
tests: test_name: replicating_sdiv_operand_profitable_to_scalarize
test_body: target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.8.0"

define void @replicating_sdiv_operand_profitable_to_scalarize(i32 %x) #0 {
entry:
  br label %loop

loop:                                             ; preds = %loop, %entry
  %iv = phi i32 [ 0, %entry ], [ %div1, %loop ]
  %accum = phi i32 [ 0, %entry ], [ %next, %loop ]
  %div1 = sdiv i32 2, %x
  %sub = sub i32 %x, %div1
  %or1 = or i32 %sub, %iv
  %or2 = or i32 %div1, 3
  %div2 = sdiv i32 %div1, %or2
  %or3 = or i32 %or1, %div2
  %next = add i32 %accum, 3
  %cond = icmp ugt i32 %accum, 35
  br i1 %cond, label %exit, label %loop

exit:                                             ; preds = %loop
  %result = phi i32 [ %or3, %loop ]
  ret void
}

attributes #0 = { "target-cpu"="skylake-avx512" }