Bug type: crash
Component: ScalarEvolution
Bug location: llvm/lib/Analysis/ScalarEvolution.cpp: ScalarEvolution::LoopGuards::collect
Issue title: [SCEV] Another SEGV/stack overflow in LoopGuards
Labels: llvm:SCEV
crash-on-valid
Reported by: danilaml

Similar to https://github.com/llvm/llvm-project/issues/120615. Looks like the fix wasn't a complete one. Here is an example:

```llvm
target triple = "x86_64-unknown-linux-gnu"

define ptr @f(i32 %0) {
  switch i32 0, label %bb4 [
    i32 1, label %bb4
    i32 2, label %bb4
    i32 3, label %bb4
    i32 4, label %bb1
    i32 5, label %bb4
    i32 6, label %bb4
  ]

bb:                                               ; No predecessors!
  switch i32 0, label %bb4 [
    i32 0, label %bb4
    i32 1, label %bb1
  ]

bb1:                                              ; preds = %bb2, %bb, %1
  %2 = phi i32 [ %3, %bb2 ], [ 0, %bb ], [ 0, %1 ]
  switch i32 %0, label %bb3 [
    i32 0, label %bb2
    i32 1, label %bb2
    i32 2, label %bb2
  ]

bb2:                                              ; preds = %bb1, %bb1, %bb1
  %3 = add i32 %2, 1
  %4 = icmp ult i32 %0, 0
  br i1 %4, label %bb1, label %bb4

bb3:                                              ; preds = %bb1
  unreachable

bb4:                                              ; preds = %bb2, %bb, %bb, %1, %1, %1, %1, %1, %1
  ret ptr null
}
```
Crashes with the same command line `opt -passes=nary-reassociate --scalar-evolution-use-expensive-range-sharpening`
godbolt: https://godbolt.org/z/4d3jo8jTz

Comments: author: juliannagele
body: Thanks, checking!
author: danilaml
body: @juliannagele Thanks for looking into it! Do you need any additional info?

Fix patch:
commit 137d706739653304294adef84ed758e3e498d975
Author: Julian Nagele <j.nagele@apple.com>
Date:   Wed Jan 22 18:36:37 2025 +0000

    [SCEV] Do not attempt to collect loop guards for loops without predecessor. (#123662)
    
    Attempting to collect loop guards for loops without a predecessor can
    lead to non-terminating recursion trying to construct a SCEV.
    
    Fixes https://github.com/llvm/llvm-project/issues/122913.

diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 7673c3548175..210c7cab965e 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -15328,6 +15328,8 @@ ScalarEvolution::LoopGuards::collect(const Loop *L, ScalarEvolution &SE) {
   BasicBlock *Header = L->getHeader();
   BasicBlock *Pred = L->getLoopPredecessor();
   LoopGuards Guards(SE);
+  if (!Pred)
+    return Guards;
   SmallPtrSet<const BasicBlock *, 8> VisitedBlocks;
   collectFromBlock(SE, Guards, Header, Pred, VisitedBlocks);
   return Guards;


Regression tests:
file: llvm/test/Analysis/ScalarEvolution/backedge-taken-count-guard-info-with-multiple-predecessors.ll
commands: opt < %s -disable-output -passes=nary-reassociate --scalar-evolution-use-expensive-range-sharpening 2>&1
tests: test_name: pr122913
test_body: target triple = "x86_64-unknown-linux-gnu"

define ptr @f(i32 %0) {
  switch i32 0, label %bb4 [
    i32 1, label %bb4
    i32 2, label %bb4
    i32 3, label %bb4
    i32 4, label %bb1
    i32 5, label %bb4
    i32 6, label %bb4
  ]

bb:                                               ; No predecessors!
  switch i32 0, label %bb4 [
    i32 0, label %bb4
    i32 1, label %bb1
  ]

bb1:                                              ; preds = %bb2, %bb, %1
  %2 = phi i32 [ %3, %bb2 ], [ 0, %bb ], [ 0, %1 ]
  switch i32 %0, label %bb3 [
    i32 0, label %bb2
    i32 1, label %bb2
    i32 2, label %bb2
  ]

bb2:                                              ; preds = %bb1, %bb1, %bb1
  %3 = add i32 %2, 1
  %4 = icmp ult i32 %0, 0
  br i1 %4, label %bb1, label %bb4

bb3:                                              ; preds = %bb1
  unreachable

bb4:                                              ; preds = %bb2, %bb, %bb, %1, %1, %1, %1, %1, %1
  ret ptr null
}