Bug type: crash
Component: VectorCombine
Bug location: llvm/lib/Transforms/Vectorize/VectorCombine.cpp: VectorCombine::foldSelectShuffle
Issue title: [VectorCombine] llvm/test/CodeGen/PowerPC/swaps-le-7.ll crashes with -passes=vector-combine
Labels: crash
llvm::vectorcombine
Reported by: k-arrows

The following reproducer comes from https://github.com/llvm/llvm-project/blob/main/llvm/test/CodeGen/PowerPC/swaps-le-7.ll

Reproducer:
https://godbolt.org/z/5c69hEYGf

Backtrace:
```console
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
0.	Program arguments: /opt/compiler-explorer/clang-trunk/bin/opt -o /app/output.s -S -passes=vector-combine <source>
1.	Running pass "function(vector-combine)" on module "<source>"
2.	Running pass "vector-combine" on function "zg"
 #0 0x00000000051c5948 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-trunk/bin/opt+0x51c5948)
 #1 0x00000000051c2b14 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #2 0x000074409f442520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x000000000333c02a (anonymous namespace)::VectorCombine::foldSelectShuffle(llvm::Instruction&, bool) VectorCombine.cpp:0:0
 #4 0x000000000333e071 (anonymous namespace)::VectorCombine::run()::'lambda'(llvm::Instruction&)::operator()(llvm::Instruction&) const (.isra.0) VectorCombine.cpp:0:0
 #5 0x000000000333f12c llvm::VectorCombinePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-trunk/bin/opt+0x333f12c)
 #6 0x0000000002b961ae llvm::detail::PassModel<llvm::Function, llvm::VectorCombinePass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-trunk/bin/opt+0x2b961ae)
 #7 0x0000000004fb33b7 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-trunk/bin/opt+0x4fb33b7)
 #8 0x0000000000e50cbe 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-trunk/bin/opt+0xe50cbe)
 #9 0x0000000004fb7018 llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-trunk/bin/opt+0x4fb7018)
#10 0x00000000009802ae llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-trunk/bin/opt+0x9802ae)
#11 0x0000000004fadbd7 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-trunk/bin/opt+0x4fadbd7)
#12 0x0000000000989dfa 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-trunk/bin/opt+0x989dfa)
#13 0x000000000097e4b3 optMain (/opt/compiler-explorer/clang-trunk/bin/opt+0x97e4b3)
#14 0x000074409f429d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#15 0x000074409f429e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#16 0x00000000009764c5 _start (/opt/compiler-explorer/clang-trunk/bin/opt+0x9764c5)
Program terminated with signal: SIGFPE
Compiler returned: 136
```


Comments: author: k-arrows
body: This crash happens with opt(trunk) and opt(assertions trunk), and does not with opt 21.1.0
https://godbolt.org/z/8bhzhKMvY
author: dtcxzyw
body: This crash disappears after specifying `target triple = "powerpc64le-unknown-linux-gnu"`. I think VectorCombine/SLPVectorizer are not expected to work without TTI.


Fix patch:
commit ad3a0ae9e15f102e2cdc37e34d47b5b9d6e09497
Author: Simon Pilgrim <llvm-dev@redking.me.uk>
Date:   Mon Sep 8 13:04:23 2025 +0100

    [VectorCombine] foldSelectShuffle - early-out cases where the max vector register width isn't large enough (#157430)
    
    Technically this could happen with vector units that can't handle all legal scalar widths - but its good enough to use a generic crash test without a suitable target
    
    Fixes #157335

diff --git a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
index 7a0b7ad57a49..9dd1532d1b23 100644
--- a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
+++ b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
@@ -3903,6 +3903,8 @@ bool VectorCombine::foldSelectShuffle(Instruction &I, bool FromReduction) {
   unsigned MaxVectorSize =
       TTI.getRegisterBitWidth(TargetTransformInfo::RGK_FixedWidthVector);
   unsigned MaxElementsInVector = MaxVectorSize / ElementSize;
+  if (MaxElementsInVector == 0)
+    return false;
   // When there are multiple shufflevector operations on the same input,
   // especially when the vector length is larger than the register size,
   // identical shuffle patterns may occur across different groups of elements.


Regression tests:
file: llvm/test/Transforms/VectorCombine/pr157335.ll
commands: opt -passes=vector-combine -S %s
tests: test_name: <module>
test_body: 
define <2 x double> @PR157335() {
;
  %v0 = fmul <2 x double> zeroinitializer, zeroinitializer
  %v1 = fmul <2 x double> zeroinitializer, zeroinitializer
  %v2 = fsub <2 x double> %v0, %v1
  %v3 = fadd <2 x double> %v0, %v1
  %v4 = shufflevector <2 x double> %v2, <2 x double> %v3, <2 x i32> <i32 0, i32 3>
  ret <2 x double> %v4
}