xref: /llvm-project/llvm/test/Transforms/LoopVectorize/pr31190.ll (revision dd116369f646d023a2e7e5c145a1bed5dcf9a45c)
1; RUN: opt -passes='loop-vectorize' -debug -S < %s 2>&1 | FileCheck %s
2; REQUIRES: asserts
3
4; This checks we don't crash when the inner loop we're trying to vectorize
5; is a SCEV AddRec with respect to an outer loop.
6
7; In this case, the problematic PHI is:
8; %0 = phi i32 [ poison, %for.cond1.preheader ], [ %inc54, %for.body3 ]
9; Since %inc54 is the IV of the outer loop, and %0 equivalent to it,
10; we get the situation described above.
11
12; Code that leads to this situation can look something like:
13;
14; int a, b[1], c;
15; void fn1 ()
16; {
17;  for (; c; c++)
18;    for (a = 0; a; a++)
19;      b[c] = 4;
20; }
21;
22; The PHI is an artifact of the register promotion of c.
23
24; Note that we can no longer get the vectorizer to actually see such PHIs,
25; because LV now simplifies the loop internally, but the test is still
26; useful as a regression test, and in case loop-simplify behavior changes.
27
28@c = external global i32, align 4
29@a = external global i32, align 4
30@b = external global [1 x i32], align 4
31
32; We can vectorize this loop because we are storing an invariant value into an
33; invariant address.
34
35; CHECK: LV: We can vectorize this loop!
36; CHECK-LABEL: @test
37define void @test() {
38entry:
39  %a.promoted2 = load i32, ptr @a, align 1
40  %c.promoted = load i32, ptr @c, align 1
41  br label %for.cond1.preheader
42
43for.cond1.preheader:                              ; preds = %for.cond1.for.inc4_crit_edge, %entry
44  %inc54 = phi i32 [ %inc5, %for.cond1.for.inc4_crit_edge ], [ %c.promoted, %entry ]
45  %inc.lcssa3 = phi i32 [ %inc.lcssa, %for.cond1.for.inc4_crit_edge ], [ %a.promoted2, %entry ]
46  br label %for.body3
47
48for.body3:                                        ; preds = %for.body3, %for.cond1.preheader
49  %inc1 = phi i32 [ %inc.lcssa3, %for.cond1.preheader ], [ %inc, %for.body3 ]
50  %0 = phi i32 [ poison, %for.cond1.preheader ], [ %inc54, %for.body3 ]
51  %idxprom = sext i32 %0 to i64
52  %arrayidx = getelementptr inbounds [1 x i32], ptr @b, i64 0, i64 %idxprom
53  store i32 4, ptr %arrayidx, align 4
54  %inc = add nsw i32 %inc1, 1
55  %tobool2 = icmp eq i32 %inc, 0
56  br i1 %tobool2, label %for.cond1.for.inc4_crit_edge, label %for.body3
57
58for.cond1.for.inc4_crit_edge:                     ; preds = %for.body3
59  %inc.lcssa = phi i32 [ %inc, %for.body3 ]
60  %.lcssa = phi i32 [ %inc54, %for.body3 ]
61  %inc5 = add nsw i32 %.lcssa, 1
62  br label %for.cond1.preheader
63}
64