Bug type: crash
Component: SimpleLoopUnswitch
Bug location: llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp: buildClonedLoopBlocks
Issue title: [SCEV] Assertion `isAvailableAtLoopEntry(Op, L) && "SCEVAddRecExpr operand is not available at loop entry!"' failed.
Labels: crash
llvm:transforms
Reported by: Nirhar

Faulty IR:
```
; ModuleID = 'test.ll'
source_filename = "/nfs/build-cache/zvm-dev-4598/jdk11/x86_64/fastdebug/lib/server/boilerplate.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 @test(ptr addrspace(1) %p) {
entry:
  %check = icmp eq ptr addrspace(1) %p, null
  br label %bb0

bb0:                                              ; preds = %loop0, %entry
  br i1 %check, label %loop0, label %latch

latch:                                            ; preds = %bb0
  br i1 %check, label %exit0, label %loop0

exit0:                                            ; preds = %latch
  ret void

loop0:                                            ; preds = %latch, %bb0
  %0 = load atomic i32, ptr addrspace(1) %p unordered, align 8
  %add = add i32 %0, 3
  br i1 true, label %preheader, label %bb0

preheader:                                        ; preds = %loop0
  br label %loop1

loop1:                                            ; preds = %backedge, %preheader
  %iv1 = phi i32 [ %add, %preheader ], [ %1, %backedge ]
  %1 = add i32 %iv1, -33
  br label %loop2

backedge:                                         ; preds = %loop2
  br i1 true, label %exit1, label %loop1

loop2:                                            ; preds = %loop2, %loop1
  %iv0 = phi i32 [ %iv1, %loop1 ], [ %2, %loop2 ]
  %2 = add nsw i32 %iv0, 1
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %backedge, label %loop2

exit1:                                            ; preds = %backedge
  ret void
}
```
fails when run with `opt -passes='print<scalar-evolution>,simple-loop-unswitch<nontrivial>,print<scalar-evolution>' `
Demo of failure: https://godbolt.org/z/qhboK5r3j


Comments: author: Nirhar
body: Here is the IR after simple-loop-unswitch pass:
![res](https://github.com/user-attachments/assets/8a35df98-54f4-45f6-9e96-626fd6814e7f)


At the point of crash, the SCEV for Exit Value of `%iv0`(which I assume, as we crashed while printing it) is `(-4 + (-1 * %1) + %.us-phi)`, which is incorrect. The crash happens because `%1` is in a block that does not properly dominate `%iv0`. Looks like, simple-loop-unswitch is either incorrectly updating the SCEV in the pass, or is incorrectly reporting SCEV to be preserved. As of now, I'm not sure which is the case. 
author: Nirhar
body: @nikic @dtcxzyw @EugeneZelenko any thoughts? I'm inclined to having a patch that removes SCEV from simple-loop-unswitch 's list of preserved analyses.
author: dtcxzyw
body: > I'm inclined to having a patch that removes SCEV from simple-loop-unswitch 's list of preserved analyses.

Some SCEVs should be invalidated in `unswitchNontrivialInvariants`. Removing SCEV from the list of preserved analyses doesn't fix the issue.


Fix patch:
commit fc5c89900f2a4b50e0f3a88ef7c89115d93684f4
Author: Nikita Popov <npopov@redhat.com>
Date:   Wed Nov 27 11:47:22 2024 +0100

    [SimpleLoopUnswitch] Fix LCSSA phi node invalidation
    
    Fixes https://github.com/llvm/llvm-project/issues/117537.

diff --git a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
index aa3cbc5e4bdd..d8ef450eeb9a 100644
--- a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
+++ b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
@@ -1248,8 +1248,9 @@ static BasicBlock *buildClonedLoopBlocks(
       assert(VMap.lookup(&I) == &ClonedI && "Mismatch in the value map!");
 
       // Forget SCEVs based on exit phis in case SCEV looked through the phi.
-      if (SE && isa<PHINode>(I))
-        SE->forgetValue(&I);
+      if (SE)
+        if (auto *PN = dyn_cast<PHINode>(&I))
+          SE->forgetLcssaPhiWithNewPredecessor(&L, PN);
 
       BasicBlock::iterator InsertPt = MergeBB->getFirstInsertionPt();
 


Regression tests:
file: llvm/test/Transforms/SimpleLoopUnswitch/pr117537.ll
commands: opt -S -passes='print<scalar-evolution>,simple-loop-unswitch<nontrivial>,print<scalar-evolution>' -verify-scev < %s 2>&1
tests: test_name: test
test_body: define void @test(ptr %p) {
entry:
  %check = icmp eq ptr %p, null
  br label %bb0

bb0:                                              ; preds = %loop0, %entry
  br i1 %check, label %loop0, label %latch

latch:                                            ; preds = %bb0
  br i1 %check, label %exit0, label %loop0

exit0:                                            ; preds = %latch
  ret void

loop0:                                            ; preds = %latch, %bb0
  %v = load atomic i32, ptr %p unordered, align 8
  %add = add i32 %v, 3
  br i1 true, label %preheader, label %bb0

preheader:                                        ; preds = %loop0
  br label %loop1

loop1:                                            ; preds = %backedge, %preheader
  %iv1 = phi i32 [ %add, %preheader ], [ %iv1.next, %backedge ]
  %iv1.next = add i32 %iv1, -33
  br label %loop2

backedge:                                         ; preds = %loop2
  br i1 true, label %exit1, label %loop1

loop2:                                            ; preds = %loop2, %loop1
  %iv0 = phi i32 [ %iv1, %loop1 ], [ %iv0.next, %loop2 ]
  %iv0.next = add nsw i32 %iv0, 1
  %cmp = icmp sgt i32 %iv0.next, 0
  br i1 %cmp, label %backedge, label %loop2

exit1:                                            ; preds = %backedge
  ret void
}