[SCEV] Disallow simplifying phi(undef, X) to X (#115109)

See the following case:
```
@GlobIntONE = global i32 0, align 4

define ptr @src() {
entry:
  br label %for.body.peel.begin

for.body.peel.begin:                              ; preds = %entry
  br label %for.body.peel

for.body.peel:                                    ; preds = %for.body.peel.begin
  br i1 true, label %cleanup.peel, label %cleanup.loopexit.peel

cleanup.loopexit.peel:                            ; preds = %for.body.peel
  br label %cleanup.peel

cleanup.peel:                                     ; preds = %cleanup.loopexit.peel, %for.body.peel
  %retval.2.peel = phi ptr [ undef, %for.body.peel ], [ @GlobIntONE, %cleanup.loopexit.peel ]
  br i1 true, label %for.body.peel.next, label %cleanup7

for.body.peel.next:                               ; preds = %cleanup.peel
  br label %for.body.peel.next1

for.body.peel.next1:                              ; preds = %for.body.peel.next
  br label %entry.peel.newph

entry.peel.newph:                                 ; preds = %for.body.peel.next1
  br label %for.body

for.body:                                         ; preds = %cleanup, %entry.peel.newph
  %retval.0 = phi ptr [ %retval.2.peel, %entry.peel.newph ], [ %retval.2, %cleanup ]
  br i1 false, label %cleanup, label %cleanup.loopexit

cleanup.loopexit:                                 ; preds = %for.body
  br label %cleanup

cleanup:                                          ; preds = %cleanup.loopexit, %for.body
  %retval.2 = phi ptr [ %retval.0, %for.body ], [ @GlobIntONE, %cleanup.loopexit ]
  br i1 false, label %for.body, label %cleanup7.loopexit

cleanup7.loopexit:                                ; preds = %cleanup
  %retval.2.lcssa.ph = phi ptr [ %retval.2, %cleanup ]
  br label %cleanup7

cleanup7:                                         ; preds = %cleanup7.loopexit, %cleanup.peel
  %retval.2.lcssa = phi ptr [ %retval.2.peel, %cleanup.peel ], [ %retval.2.lcssa.ph, %cleanup7.loopexit ]
  ret ptr %retval.2.lcssa
}

define ptr @tgt() {
entry:
  br label %for.body.peel.begin

for.body.peel.begin:                              ; preds = %entry
  br label %for.body.peel

for.body.peel:                                    ; preds = %for.body.peel.begin
  br i1 true, label %cleanup.peel, label %cleanup.loopexit.peel

cleanup.loopexit.peel:                            ; preds = %for.body.peel
  br label %cleanup.peel

cleanup.peel:                                     ; preds = %cleanup.loopexit.peel, %for.body.peel
  %retval.2.peel = phi ptr [ undef, %for.body.peel ], [ @GlobIntONE, %cleanup.loopexit.peel ]
  br i1 true, label %for.body.peel.next, label %cleanup7

for.body.peel.next:                               ; preds = %cleanup.peel
  br label %for.body.peel.next1

for.body.peel.next1:                              ; preds = %for.body.peel.next
  br label %entry.peel.newph

entry.peel.newph:                                 ; preds = %for.body.peel.next1
  br label %for.body

for.body:                                         ; preds = %cleanup, %entry.peel.newph
  br i1 false, label %cleanup, label %cleanup.loopexit

cleanup.loopexit:                                 ; preds = %for.body
  br label %cleanup

cleanup:                                          ; preds = %cleanup.loopexit, %for.body
  br i1 false, label %for.body, label %cleanup7.loopexit

cleanup7.loopexit:                                ; preds = %cleanup
  %retval.2.lcssa.ph = phi ptr [ %retval.2.peel, %cleanup ]
  br label %cleanup7

cleanup7:                                         ; preds = %cleanup7.loopexit, %cleanup.peel
  %retval.2.lcssa = phi ptr [ %retval.2.peel, %cleanup.peel ], [ %retval.2.lcssa.ph, %cleanup7.loopexit ]
  ret ptr %retval.2.lcssa
}
```
1. `simplifyInstruction(%retval.2.peel)` returns `@GlobIntONE`. Thus,
`ScalarEvolution::createNodeForPHI` returns SCEV expr `@GlobIntONE` for
`%retval.2.peel`.
2. `SimplifyIndvar::replaceIVUserWithLoopInvariant` tries to replace the
use of `%retval.2.peel` in `%retval.2.lcssa.ph` with `@GlobIntONE`.
3. `simplifyLoopAfterUnroll -> simplifyLoopIVs -> SCEVExpander::expand`
reuses `%retval.2.peel = phi ptr [ undef, %for.body.peel ], [
@GlobIntONE, %cleanup.loopexit.peel ]` to generate code for
`@GlobIntONE`. It is incorrect.

This patch disallows simplifying `phi(undef, X)` to `X` by setting
`CanUseUndef` to false.
Closes https://github.com/llvm/llvm-project/issues/114879.

GitOrigin-RevId: 0b9f1cc024ca6c7e8d60524be07c0ddfcd08b23c
diff --git a/test/CodeGen/OpenMP/floord-as-argument-to-subfunction.ll b/test/CodeGen/OpenMP/floord-as-argument-to-subfunction.ll
index 9eb7f5f..7177ae0 100644
--- a/test/CodeGen/OpenMP/floord-as-argument-to-subfunction.ll
+++ b/test/CodeGen/OpenMP/floord-as-argument-to-subfunction.ll
@@ -20,9 +20,9 @@
   br label %for.cond66.preheader
 
 for.cond66.preheader:                             ; preds = %for.cond39.for.cond66.preheader.loopexit67_crit_edge, %if.end
-  %rawout1.3.ph = phi ptr [ %add.ptr62.lcssa, %for.cond39.for.cond66.preheader.loopexit67_crit_edge ], [ undef, %if.end ]
+  %rawout1.3.ph = phi ptr [ %add.ptr62.lcssa, %for.cond39.for.cond66.preheader.loopexit67_crit_edge ], [ poison, %if.end ]
   %sb.3.ph = phi i32 [ 0, %for.cond39.for.cond66.preheader.loopexit67_crit_edge ], [ 0, %if.end ]
-  %tspnt.3.ph = phi ptr [ undef, %for.cond39.for.cond66.preheader.loopexit67_crit_edge ], [ %tsOut, %if.end ]
+  %tspnt.3.ph = phi ptr [ poison, %for.cond39.for.cond66.preheader.loopexit67_crit_edge ], [ %tsOut, %if.end ]
   br label %for.cond69.preheader
 
 for.body42:                                       ; preds = %if.end
@@ -31,12 +31,12 @@
 for.cond69.preheader:                             ; preds = %for.end76, %for.cond66.preheader
   %tspnt.375 = phi ptr [ %incdec.ptr79, %for.end76 ], [ %tspnt.3.ph, %for.cond66.preheader ]
   %sb.374 = phi i32 [ %inc78, %for.end76 ], [ %sb.3.ph, %for.cond66.preheader ]
-  %rawout1.373 = phi ptr [ undef, %for.end76 ], [ %rawout1.3.ph, %for.cond66.preheader ]
+  %rawout1.373 = phi ptr [ poison, %for.end76 ], [ %rawout1.3.ph, %for.cond66.preheader ]
   br label %for.body71
 
 for.body71:                                       ; preds = %for.body71, %for.cond69.preheader
   %indvars.iv = phi i64 [ 0, %for.cond69.preheader ], [ %indvars.iv.next, %for.body71 ]
-  %rawout1.469 = phi ptr [ %rawout1.373, %for.cond69.preheader ], [ undef, %for.body71 ]
+  %rawout1.469 = phi ptr [ %rawout1.373, %for.cond69.preheader ], [ poison, %for.body71 ]
   %0 = load i64, ptr %rawout1.469, align 8
   %1 = shl nsw i64 %indvars.iv, 5
   %arrayidx73 = getelementptr inbounds double, ptr %tspnt.375, i64 %1
diff --git a/test/CodeGen/inner_scev_sdiv_2.ll b/test/CodeGen/inner_scev_sdiv_2.ll
index 74b914d..33233fe 100644
--- a/test/CodeGen/inner_scev_sdiv_2.ll
+++ b/test/CodeGen/inner_scev_sdiv_2.ll
@@ -34,7 +34,7 @@
   ret void
 
 for.body.51:                                      ; preds = %for.body.51, %for.body.51.lr.ph
-  %indvars.iv86 = phi i64 [ %2, %for.body.51.lr.ph ], [ undef, %for.body.51 ]
+  %indvars.iv86 = phi i64 [ %2, %for.body.51.lr.ph ], [ poison, %for.body.51 ]
   %arrayidx53 = getelementptr inbounds float, ptr %0, i64 %indvars.iv86
   %3 = load float, ptr %arrayidx53, align 4
   %mul56 = fmul float %3, undef
diff --git a/test/CodeGen/inner_scev_sdiv_3.ll b/test/CodeGen/inner_scev_sdiv_3.ll
index 3344045..a8c6263 100644
--- a/test/CodeGen/inner_scev_sdiv_3.ll
+++ b/test/CodeGen/inner_scev_sdiv_3.ll
@@ -36,7 +36,7 @@
   ret i64 %div44.m
 
 for.body.51:                                      ; preds = %for.body.51, %for.body.51.lr.ph
-  %indvars.iv86 = phi i64 [ %2, %for.body.51.lr.ph ], [ undef, %for.body.51 ]
+  %indvars.iv86 = phi i64 [ %2, %for.body.51.lr.ph ], [ poison, %for.body.51 ]
   %arrayidx53 = getelementptr inbounds float, ptr %0, i64 %indvars.iv86
   %3 = load float, ptr %arrayidx53, align 4
   %mul56 = fmul float %3, undef
diff --git a/test/CodeGen/non-affine-phi-node-expansion.ll b/test/CodeGen/non-affine-phi-node-expansion.ll
index 8fd8cc1..1b6802f 100644
--- a/test/CodeGen/non-affine-phi-node-expansion.ll
+++ b/test/CodeGen/non-affine-phi-node-expansion.ll
@@ -31,7 +31,7 @@
   br label %bb2
 
 bb2:                                              ; preds = %bb1, %bb
-  %tmp = phi i1 [ true, %bb ], [ undef, %bb1 ]
+  %tmp = phi i1 [ true, %bb ], [ poison, %bb1 ]
   br label %bb3
 
 bb3:                                              ; preds = %bb13, %bb2
diff --git a/test/CodeGen/phi-defined-before-scop.ll b/test/CodeGen/phi-defined-before-scop.ll
index f083222..2361206 100644
--- a/test/CodeGen/phi-defined-before-scop.ll
+++ b/test/CodeGen/phi-defined-before-scop.ll
@@ -19,7 +19,7 @@
   br label %bb1
 
 bb1:                                              ; preds = %bb6, %bb
-  %tmp2 = phi ptr [ %tmp7, %bb6 ], [ undef, %bb ]
+  %tmp2 = phi ptr [ %tmp7, %bb6 ], [ poison, %bb ]
   %tmp = load ptr, ptr @global, align 8, !tbaa !1
   br label %bb3
 
@@ -31,7 +31,7 @@
   br label %bb6
 
 bb6:                                              ; preds = %bb5, %bb3
-  %tmp7 = phi ptr [ %tmp2, %bb3 ], [ undef, %bb5 ]
+  %tmp7 = phi ptr [ %tmp2, %bb3 ], [ poison, %bb5 ]
   br i1 undef, label %bb8, label %bb1
 
 bb8:                                              ; preds = %bb6
diff --git a/test/CodeGen/scop_expander_insert_point.ll b/test/CodeGen/scop_expander_insert_point.ll
index 92f2772..fd73132 100644
--- a/test/CodeGen/scop_expander_insert_point.ll
+++ b/test/CodeGen/scop_expander_insert_point.ll
@@ -27,8 +27,8 @@
   br label %for.body17
 
 for.body17:                                       ; preds = %for.body17, %for.body17.lr.ph
-  %outvalue.141 = phi i64 [ undef, %for.body17.lr.ph ], [ %add19, %for.body17 ]
-  %inptr.040 = phi ptr [ %add.ptr, %for.body17.lr.ph ], [ undef, %for.body17 ]
+  %outvalue.141 = phi i64 [ poison, %for.body17.lr.ph ], [ %add19, %for.body17 ]
+  %inptr.040 = phi ptr [ %add.ptr, %for.body17.lr.ph ], [ poison, %for.body17 ]
   %1 = load i8, ptr %inptr.040
   %add19 = mul nsw i64 0, %outvalue.141
   br i1 false, label %for.body17, label %for.end22
diff --git a/test/CodeGen/stack-overflow-in-load-hoisting.ll b/test/CodeGen/stack-overflow-in-load-hoisting.ll
index cb9d9a2..b49c4e1 100644
--- a/test/CodeGen/stack-overflow-in-load-hoisting.ll
+++ b/test/CodeGen/stack-overflow-in-load-hoisting.ll
@@ -42,7 +42,7 @@
   br label %for.body.14.lr.ph
 
 for.body.14.lr.ph:                                ; preds = %for.end, %for.cond.12.preheader.lr.ph
-  %d.050 = phi ptr [ %tmp3, %for.cond.12.preheader.lr.ph ], [ undef, %for.end ]
+  %d.050 = phi ptr [ %tmp3, %for.cond.12.preheader.lr.ph ], [ poison, %for.end ]
   %w = getelementptr inbounds %struct.AudioVectorScopeContext.21.43.879.1209.1297.1319.1573, ptr %s, i32 0, i32 2
   %tmp4 = load i32, ptr %w, align 4
   %cmp13.46 = icmp sgt i32 %tmp4, 0
diff --git a/test/ScopInfo/complex_domain_binary_condition.ll b/test/ScopInfo/complex_domain_binary_condition.ll
index 6091e3b..6e28c9d 100644
--- a/test/ScopInfo/complex_domain_binary_condition.ll
+++ b/test/ScopInfo/complex_domain_binary_condition.ll
@@ -35,7 +35,7 @@
   br label %for.body104
 
 for.body104:                                      ; preds = %while.end146, %for.body104.lr.ph
-  %indvars.iv = phi i64 [ %6, %for.body104.lr.ph ], [ undef, %while.end146 ]
+  %indvars.iv = phi i64 [ %6, %for.body104.lr.ph ], [ poison, %while.end146 ]
   %7 = sub nsw i64 %indvars.iv, %idx.ext62
   %cmp107 = icmp slt i64 %7, -1
   %.op = xor i64 %7, -1
diff --git a/test/ScopInfo/scev-div-with-evaluatable-divisor.ll b/test/ScopInfo/scev-div-with-evaluatable-divisor.ll
index d14569c..55192b5 100644
--- a/test/ScopInfo/scev-div-with-evaluatable-divisor.ll
+++ b/test/ScopInfo/scev-div-with-evaluatable-divisor.ll
@@ -17,7 +17,7 @@
   br label %for.body
 
 for.body:                                         ; preds = %for.inc, %for.body.lr.ph
-  %mj.017 = phi i32 [ 1, %for.body.lr.ph ], [ undef, %for.inc ]
+  %mj.017 = phi i32 [ 1, %for.body.lr.ph ], [ poison, %for.inc ]
   br i1 true, label %if.else, label %if.then
 
 if.then:                                          ; preds = %for.body