Bug type: hang
Component: ScalarEvolution
Bug location: llvm/lib/Analysis/ScalarEvolution.cpp: ScalarEvolution::LoopGuards::collectFromBlock
Issue title: Program terminated with signal: SIGKILL Compiler returned: 137
Labels: llvm:SCEV
crash
Reported by: TatyanaDoubts

To reproduce run the following test with -passes=loop-vectorize on opt:
```
; 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:
  call void @llvm.assume(i1 false)
  call void @llvm.assume(i1 false)
  br label %bb6

bb1:                                              ; preds = %bb2, %bb6
  br label %bb2

bb2:                                              ; preds = %bb2, %bb1
  %phi = phi i32 [ %add4, %bb2 ], [ 0, %bb1 ]
  %phi3 = phi i32 [ 0, %bb2 ], [ 0, %bb1 ]
  %add = add i32 0, 0
  %add4 = add i32 %phi, 1
  %icmp = icmp ugt i32 %phi, 0
  br i1 %icmp, label %bb1, label %bb2

bb5:                                              ; preds = %bb5
  br i1 false, label %bb6, label %bb5

bb6:                                              ; preds = %bb5, %bb
  %phi7 = phi i32 [ 0, %bb5 ], [ 0, %bb ]
  br label %bb1
}

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

attributes #0 = { "target-features"="+prfchw,-cldemote,+avx,+aes,+sahf,+pclmul,-xop,+crc32,-amx-fp8,+xsaves,-avx512fp16,-usermsr,-sm4,-egpr,+sse4.1,-avx512ifma,+xsave,+sse4.2,-tsxldtrk,-sm3,-ptwrite,-widekl,-movrs,+invpcid,+64bit,+xsavec,-avx10.1-512,-avx512vpopcntdq,+cmov,-avx512vp2intersect,+avx512cd,+movbe,-avxvnniint8,-ccmp,-amx-int8,-kl,-avx10.1-256,+evex512,-avxvnni,+rtm,+adx,+avx2,-hreset,-movdiri,-serialize,-sha512,-vpclmulqdq,+avx512vl,-uintr,-cf,+clflushopt,-raoint,-cmpccxadd,+bmi,-amx-tile,+sse,-avx10.2-256,-gfni,-avxvnniint16,-amx-fp16,-zu,-ndd,+xsaveopt,+rdrnd,+avx512f,-amx-bf16,-avx512bf16,-avx512vnni,-push2pop2,+cx8,+avx512bw,+sse3,+pku,-nf,-amx-tf32,-amx-avx512,+fsgsbase,-clzero,-mwaitx,-lwp,+lzcnt,-sha,-movdir64b,-ppx,-wbnoinvd,-enqcmd,-amx-transpose,-avx10.2-512,-avxneconvert,-tbm,-pconfig,-amx-complex,+ssse3,+cx16,+bmi2,+fma,+popcnt,-avxifma,+f16c,-avx512bitalg,-rdpru,+clwb,+mmx,+sse2,+rdseed,-avx512vbmi2,-prefetchi,-amx-movrs,-rdpid,-fma4,-avx512vbmi,-shstk,-vaes,-waitpkg,-sgx,+fxsr,+avx512dq,-sse4a,-avx512f" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write) "target-features"="+prfchw,-cldemote,+avx,+aes,+sahf,+pclmul,-xop,+crc32,-amx-fp8,+xsaves,-avx512fp16,-usermsr,-sm4,-egpr,+sse4.1,-avx512ifma,+xsave,+sse4.2,-tsxldtrk,-sm3,-ptwrite,-widekl,-movrs,+invpcid,+64bit,+xsavec,-avx10.1-512,-avx512vpopcntdq,+cmov,-avx512vp2intersect,+avx512cd,+movbe,-avxvnniint8,-ccmp,-amx-int8,-kl,-avx10.1-256,+evex512,-avxvnni,+rtm,+adx,+avx2,-hreset,-movdiri,-serialize,-sha512,-vpclmulqdq,+avx512vl,-uintr,-cf,+clflushopt,-raoint,-cmpccxadd,+bmi,-amx-tile,+sse,-avx10.2-256,-gfni,-avxvnniint16,-amx-fp16,-zu,-ndd,+xsaveopt,+rdrnd,+avx512f,-amx-bf16,-avx512bf16,-avx512vnni,-push2pop2,+cx8,+avx512bw,+sse3,+pku,-nf,-amx-tf32,-amx-avx512,+fsgsbase,-clzero,-mwaitx,-lwp,+lzcnt,-sha,-movdir64b,-ppx,-wbnoinvd,-enqcmd,-amx-transpose,-avx10.2-512,-avxneconvert,-tbm,-pconfig,-amx-complex,+ssse3,+cx16,+bmi2,+fma,+popcnt,-avxifma,+f16c,-avx512bitalg,-rdpru,+clwb,+mmx,+sse2,+rdseed,-avx512vbmi2,-prefetchi,-amx-movrs,-rdpid,-fma4,-avx512vbmi,-shstk,-vaes,-waitpkg,-sgx,+fxsr,+avx512dq,-sse4a,-avx512f" }
```

Reproducer: https://godbolt.org/z/x7WrWvoxa



Comments: author: TatyanaDoubts
body: The godbolt is not informative. But locally I see the following stack dump:
```
bin/opt -S -passes=loop-vectorize reduced-stripped.ll
LLVM ERROR: SmallVector unable to grow. Requested capacity (4294967296) is larger than maximum value for size type (4294967295)
0.      Program arguments: bin/opt -S -passes=loop-vectorize reduced-stripped.ll
1.      Running pass "function(loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>)" on module "reduced-stripped.ll"
2.      Running pass "loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>" on function "wombat"
 #0 0x00007f945d6be47b llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) llvm/lib/Support/Unix/Signals.inc:727:3
 #1 0x00007f945d6bbfbb llvm::sys::RunSignalHandlers() llvm/lib/Support/Signals.cpp:105:20
 #2 0x00007f945d6bc2fe SignalHandler(int) llvm/lib/Support/Unix/Signals.inc:413:1
 #3 0x00007f9462a89980 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12980)
 #4 0x00007f945c0fbe87 raise (/lib/x86_64-linux-gnu/libc.so.6+0x3ee87)
 #5 0x00007f945c0fd7f1 abort (/lib/x86_64-linux-gnu/libc.so.6+0x407f1)
 #6 0x00007f945d435674 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>::_M_replace(unsigned long, unsigned long, char const*, unsigned long) gcc-13.3.0-rhel6/include/c++/13.3.0/bits/basic_string.tcc:545:21
 #7 0x00007f945d435674 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>::assign(char const*) (.isra.0.cold) gcc-13.3.0-rhel6/include/c++/13.3.0/bits
/basic_string.h:1684:19
 #8 0x00007f945d63c540 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>::replace(unsigned long, unsigned long, char const*, unsigned long) gcc-13.3.0-rhel6/include/c++/13.3.0/bits/basic_string.h:2208:19
 #9 0x00007f945d63c540 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>::insert(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const
&) gcc-13.3.0-rhel6/include/c++/13.3.0/bits/basic_string.h:1895:29
#10 0x00007f945d63c540 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> std::operator+<char, std::char_traits<char>, std::allocator<char>>(std::__cxx11::basic_string<char, std::ch
ar_traits<char>, std::allocator<char>>&&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&&) gcc-13.3.0-rhel6/include/c++/13.3.0/bits/basic_string.
h:3666:35
#11 0x00007f945d63c540 report_size_overflow(unsigned long, unsigned long) (.constprop.0) llvm/lib/Support/SmallVector.cpp:65:73
#12 0x00007f945d63c79e ......
#13 0x00007f945f6b3c9c llvm::PointerIntPairInfo<llvm::Value*, 1u, llvm::PointerLikeTypeTraits<llvm::Value*>>::updateInt(long, long)
llvm/include/llvm/ADT/PointerIntPair.h:213:55
#14 0x00007f945f6b3c9c llvm::PointerIntPair<llvm::Value*, 1u, bool, llvm::PointerLikeTypeTraits<llvm::Value*>, llvm::PointerIntPairInfo<llvm::Value*, 1u, llvm::PointerLikeTypeTraits<llvm::Value*>>>::setPointer
AndInt(llvm::Value*, bool) & llvm/include/llvm/ADT/PointerIntPair.h:111:11
#15 0x00007f945f6b3c9c llvm::PointerIntPair<llvm::Value*, 1u, bool, llvm::PointerLikeTypeTraits<llvm::Value*>, llvm::PointerIntPairInfo<llvm::Value*, 1u, llvm::PointerLikeTypeTraits<llvm::Value*>>>::PointerInt
Pair(llvm::Value*, bool) llvm/include/llvm/ADT/PointerIntPair.h:89:21
#16 0x00007f945f6b3c9c llvm::PointerIntPair<llvm::Value*, 1u, bool, llvm::PointerLikeTypeTraits<llvm::Value*>, llvm::PointerIntPairInfo<llvm::Value*, 1u, llvm::PointerLikeTypeTraits<llvm::Value*>>>& llvm::Smal
lVectorTemplateBase<llvm::PointerIntPair<llvm::Value*, 1u, bool, llvm::PointerLikeTypeTraits<llvm::Value*>, llvm::PointerIntPairInfo<llvm::Value*, 1u, llvm::PointerLikeTypeTraits<llvm::Value*>>>, true>::growAn
dEmplaceBack<llvm::Value*, bool>(llvm::Value*&&, bool&&) llvm/include/llvm/ADT/SmallVector.h:559:14
#17 0x00007f945f6b3c9c llvm::PointerIntPair<llvm::Value*, 1u, bool, llvm::PointerLikeTypeTraits<llvm::Value*>, llvm::PointerIntPairInfo<llvm::Value*, 1u, llvm::PointerLikeTypeTraits<llvm::Value*>>>& llvm::Smal
lVectorImpl<llvm::PointerIntPair<llvm::Value*, 1u, bool, llvm::PointerLikeTypeTraits<llvm::Value*>, llvm::PointerIntPairInfo<llvm::Value*, 1u, llvm::PointerLikeTypeTraits<llvm::Value*>>>>::emplace_back<llvm::V
alue*, bool>(llvm::Value*&&, bool&&) llvm/include/llvm/ADT/SmallVector.h:946:38
#18 0x00007f945f6b3c9c llvm::ScalarEvolution::LoopGuards::collectFromBlock(llvm::ScalarEvolution&, llvm::ScalarEvolution::LoopGuards&, llvm::BasicBlock const*, llvm::BasicBlock const*, llvm::SmallPtrSetImpl<llvm::BasicBlock const*>&, unsigned int) llvm/lib/Analysis/ScalarEvolution.cpp:15708:23
#19 0x00007f945f6b51fa llvm::DenseMapIterator<llvm::BasicBlock const*, llvm::ScalarEvolution::LoopGuards, llvm::DenseMapInfo<llvm::BasicBlock const*, void>, llvm::detail::DenseMapPair<llvm::BasicBlock const*, llvm::ScalarEvolution::LoopGuards>, false>::operator->() const llvm/include/llvm/ADT/DenseMap.h:1238:5
#20 0x00007f945f6b51fa llvm::ScalarEvolution::LoopGuards::collectFromPHI(llvm::ScalarEvolution&, llvm::ScalarEvolution::LoopGuards&, llvm::PHINode const&, llvm::SmallPtrSetImpl<llvm::BasicBlock const*>&, llvm::SmallDenseMap<llvm::BasicBlock const*, llvm::ScalarEvolution::LoopGuards, 4u, llvm::DenseMapInfo<llvm::BasicBlock const*, void>, llvm::detail::DenseMapPair<llvm::BasicBlock const*, llvm::ScalarEvolution::LoopGuards>>&, unsigned int)::'lambda'(unsigned int)::operator()(unsigned int) const llvm/lib/Analysis/ScalarEvolution.cpp:15285:25
#21 0x00007f945f6b200c llvm::ScalarEvolution::LoopGuards::collectFromPHI(llvm::ScalarEvolution&, llvm::ScalarEvolution::LoopGuards&, llvm::PHINode const&, llvm::SmallPtrSetImpl<llvm::BasicBlock const*>&, llvm::SmallDenseMap<llvm::BasicBlock const*, llvm::ScalarEvolution::LoopGuards, 4u, llvm::DenseMapInfo<llvm::BasicBlock const*, void>, llvm::detail::DenseMapPair<llvm::BasicBlock const*, llvm::ScalarEvolution::LoopGuards>>&, unsigned int) llvm/lib/Analysis/ScalarEvolution.cpp:15317:26
#22 0x00007f945f6b40af llvm::BasicBlock::phi_iterator_impl<llvm::PHINode const, llvm::ilist_iterator_w_bits<llvm::ilist_detail::node_options<llvm::Instruction, true, false, void, true, llvm::BasicBlock>, false, true>>::operator++() llvm/include/llvm/IR/BasicBlock.h:505:7
#23 0x00007f945f6b40af llvm::ScalarEvolution::LoopGuards::collectFromBlock(llvm::ScalarEvolution&, llvm::ScalarEvolution::LoopGuards&, llvm::BasicBlock const*, llvm::BasicBlock const*, llvm::SmallPtrSetImpl<llvm::BasicBlock const*>&, unsigned int) llvm/lib/Analysis/ScalarEvolution.cpp:15724:40
#24 0x00007f945f6b534b llvm::SmallPtrSetImplBase::~SmallPtrSetImplBase() llvm/include/llvm/ADT/SmallPtrSet.h:87:5
#25 0x00007f945f6b534b llvm::SmallPtrSetImpl<llvm::BasicBlock const*>::~SmallPtrSetImpl() llvm/include/llvm/ADT/SmallPtrSet.h:368:7
#26 0x00007f945f6b534b llvm::SmallPtrSet<llvm::BasicBlock const*, 8u>::~SmallPtrSet() llvm/include/llvm/ADT/SmallPtrSet.h:524:7
#27 0x00007f945f6b534b llvm::ScalarEvolution::LoopGuards::collect(llvm::Loop const*, llvm::ScalarEvolution&) llvm/lib/Analysis/ScalarEvolution.cpp:15266:1
#28 0x00007f945f6b539a llvm::ScalarEvolution::LoopGuards::rewrite(llvm::SCEV const*) const llvm/lib/Analysis/ScalarEvolution.cpp:15892:3
#29 0x00007f945f6b539a llvm::ScalarEvolution::applyLoopGuards(llvm::SCEV const*, llvm::ScalarEvolution::LoopGuards const&) llvm/lib/Analysis/ScalarEvolution.cpp:15905:24
#30 0x00007f945f6b539a llvm::ScalarEvolution::applyLoopGuards(llvm::SCEV const*, llvm::Loop const*) llvm/lib/Analysis/ScalarEvolution.cpp:15900:25
#31 0x00007f945f1e1c35 llvm::LoopVectorizationCostModel::computeMaxVF(llvm::ElementCount, unsigned int) llvm/lib/Transforms/Vectorize/LoopVectorize.cpp:4119:38
#32 0x00007f945f1e5bfb llvm::LoopVectorizationPlanner::plan(llvm::ElementCount, unsigned int) llvm/lib/Transforms/Vectorize/LoopVectorize.cpp:7150:51
#33 0x00007f945f1e7b71 llvm::LoopVectorizePass::processLoop(llvm::Loop*) llvm/lib/Transforms/Vectorize/LoopVectorize.cpp:10095:45
#34 0x00007f945f1eb181 llvm::LoopVectorizePass::runImpl(llvm::Function&) llvm/lib/Transforms/Vectorize/LoopVectorize.cpp:10441:27
#35 0x00007f945f1eb777 llvm::LoopVectorizePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) llvm/lib/Transforms/Vectorize/LoopVectorize.cpp:10478:39
#36 0x00007f94606acedd llvm::detail::PassModel<llvm::Function, llvm::LoopVectorizePass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) llvm/include/llvm/IR/PassManagerInternal.h:91:3
#37 0x00007f945d8d889f llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) llvm/include/llvm/IR/PassManagerImpl.h:85:18
#38 0x00007f94601642ed llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) llvm/include/llvm/IR/PassManagerInternal.h:91:3
#39 0x00007f945d8d8dea llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) llvm/lib/IR/PassManager.cpp:129:41
#40 0x00007f94601646dd llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) llvm/include/llvm/IR/PassManagerInternal.h:91:3
#41 0x00007f945d8da12f llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) llvm/include/llvm/IR/PassManagerImpl.h:85:18
#42 0x000000000042ccba llvm::SmallPtrSetImplBase::~SmallPtrSetImplBase() llvm/include/llvm/ADT/SmallPtrSet.h:87:5
#43 0x000000000042ccba llvm::SmallPtrSetImpl<llvm::AnalysisKey*>::~SmallPtrSetImpl() llvm/include/llvm/ADT/SmallPtrSet.h:368:7
#44 0x000000000042ccba llvm::SmallPtrSet<llvm::AnalysisKey*, 2u>::~SmallPtrSet() llvm/include/llvm/ADT/SmallPtrSet.h:524:7
#45 0x000000000042ccba llvm::PreservedAnalyses::~PreservedAnalyses() llvm/include/llvm/IR/Analysis.h:111:7
#46 0x000000000042ccba 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) llvm/tools/opt/NewPMDriver.cpp:553:10
#47 0x00000000004207e3 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>::~basic_string() gcc-13.3.0-rhel6/include/c++/13.3.0/bits/basic_string.h:804:19
#48 0x00000000004207e3 optMain llvm/tools/opt/optdriver.cpp:765:3
#49 0x00007f945c0dec87 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21c87)
......
```
author: fhahn
body: @juliannagele could you take a look? Seems like a crash in `collectFromPHI` you recently added
author: juliannagele
body: > [@juliannagele](https://github.com/juliannagele) could you take a look? Seems like a crash in `collectFromPHI` you recently added

Sure, checking!
author: juliannagele
body: Thanks for the report, should be fixed here: https://github.com/llvm/llvm-project/pull/120442

Fix patch:
commit acfd26a93be3fb70013560f3fb894eb9086e7e32
Author: Julian Nagele <j.nagele@apple.com>
Date:   Fri Dec 20 15:44:15 2024 +0100

    [SCEV] Fix exit condition for recursive loop guard collection (#120442)
    
    When assumptions are present `Terms.size()` does not actually count the
    number of conditions collected from dominating branches; introduce a
    separate counter.
    
    Fixes https://github.com/llvm/llvm-project/issues/120237

diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index d55d09020fc1..1e4bb1d606cd 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -15753,6 +15753,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
   // predecessors that can be found that have unique successors leading to the
   // original header.
   // TODO: share this logic with isLoopEntryGuardedByCond.
+  unsigned NumCollectedConditions = 0;
   std::pair<const BasicBlock *, const BasicBlock *> Pair(Pred, Block);
   for (; Pair.first;
        Pair = SE.getPredecessorWithUniqueSuccessorForBB(Pair.first)) {
@@ -15764,10 +15765,11 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
 
     Terms.emplace_back(LoopEntryPredicate->getCondition(),
                        LoopEntryPredicate->getSuccessor(0) == Pair.second);
+    NumCollectedConditions++;
 
     // If we are recursively collecting guards stop after 2
-    // predecessors to limit compile-time impact for now.
-    if (Depth > 0 && Terms.size() == 2)
+    // conditions to limit compile-time impact for now.
+    if (Depth > 0 && NumCollectedConditions == 2)
       break;
   }
   // Finally, if we stopped climbing the predecessor chain because


Regression tests:
file: llvm/test/Analysis/ScalarEvolution/backedge-taken-count-guard-info-with-multiple-predecessors.ll
commands: opt < %s -disable-output "-passes=print<scalar-evolution>" -scalar-evolution-max-iterations=0  -scalar-evolution-classify-expressions=0  2>&1
tests: test_name: pr120442
test_body: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write)
declare void @llvm.assume(i1 noundef) #0

define void @pr120442(i1 %c.1, i1 %c.2) {
entry:
  call void @llvm.assume(i1 %c.1)
  call void @llvm.assume(i1 %c.2)
  br label %outer.header

outer.header:                                     ; preds = %bb, %entry
  %phi7 = phi i32 [ 0, %bb ], [ 0, %entry ]
  br label %inner.header

bb:                                               ; preds = %bb
  br i1 false, label %outer.header, label %bb

inner.header:                                     ; preds = %inner.header, %outer.header
  %phi = phi i32 [ %add, %inner.header ], [ 0, %outer.header ]
  %add = add i32 %phi, 1
  %icmp = icmp ugt i32 %add, 0
  br i1 %icmp, label %exit, label %inner.header

exit:                                             ; preds = %inner.header
  ret void
}

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