[LAA] Hold bounds via ValueHandles during SCEV expansion
[oota-llvm.git] / test / Transforms / LoopDistribute / bounds-expansion-bug.ll
1 ; RUN: opt -basicaa -loop-distribute -S < %s | FileCheck %s
2
3 ; When emitting the memchecks for:
4 ;
5 ;   for (i = 0; i < n; i++) {
6 ;     A[i + 1] = A[i] * B[i];
7 ;     =======================
8 ;     C[i] = D[i] * E[i];
9 ;   }
10 ;
11 ; we had a bug when expanding the bounds for A and C.  These are expanded
12 ; multiple times and rely on the caching in SCEV expansion to avoid any
13 ; redundancy.  However, due to logic in SCEVExpander::ReuseOrCreateCast, we
14 ; can get earlier expanded values invalidated when casts are used.  This test
15 ; ensure that we are not using the invalidated values.
16
17 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
18
19 define void @f(i32* %a1, i32* %a2,
20                i32* %b,
21                i32* %c1, i32* %c2,
22                i32* %d,
23                i32* %e) {
24 entry:
25
26   %cond = icmp eq i32* %e, null
27   br i1 %cond, label %one, label %two
28 one:
29   br label %join
30 two:
31   br label %join
32 join:
33
34 ; The pointers need to be defined by PHIs in order for the bug to trigger.
35 ; Because of the PHIs the existing casts won't be at the desired location so a
36 ; new cast will be emitted and the old cast will get invalidated.
37 ;
38 ; These are the steps:
39 ;
40 ; 1. After the bounds for A and C are first expanded:
41 ;
42 ;   join:
43 ;     %a = phi i32* [ %a1, %one ], [ %a2, %two ]
44 ;     %c = phi i32* [ %c1, %one ], [ %c2, %two ]
45 ;     %c5 = bitcast i32* %c to i8*
46 ;     %a3 = bitcast i32* %a to i8*
47 ;
48 ; 2. After A is expanded again:
49 ;
50 ;   join:                                             ; preds = %two, %one
51 ;     %a = phi i32* [ %a1, %one ], [ %a2, %two ]
52 ;     %c = phi i32* [ %c1, %one ], [ %c2, %two ]
53 ;     %a3 = bitcast i32* %a to i8*                   <--- new
54 ;     %c5 = bitcast i32* %c to i8*
55 ;     %0 = bitcast i32* undef to i8*                 <--- old, invalidated
56 ;
57 ; 3. Finally, when C is expanded again:
58 ;
59 ;   join:                                             ; preds = %two, %one
60 ;     %a = phi i32* [ %a1, %one ], [ %a2, %two ]
61 ;     %c = phi i32* [ %c1, %one ], [ %c2, %two ]
62 ;     %c5 = bitcast i32* %c to i8*                   <--- new
63 ;     %a3 = bitcast i32* %a to i8*
64 ;     %0 = bitcast i32* undef to i8*                 <--- old, invalidated
65 ;     %1 = bitcast i32* undef to i8*
66
67   %a = phi i32* [%a1, %one], [%a2, %two]
68   %c = phi i32* [%c1, %one], [%c2, %two]
69   br label %for.body
70
71
72 ; CHECK: [[VALUE:%[0-9a-z]+]] = bitcast i32* undef to i8*
73 ; CHECK-NOT: [[VALUE]]
74
75 for.body:                                         ; preds = %for.body, %entry
76   %ind = phi i64 [ 0, %join ], [ %add, %for.body ]
77
78   %arrayidxA = getelementptr inbounds i32, i32* %a, i64 %ind
79   %loadA = load i32, i32* %arrayidxA, align 4
80
81   %arrayidxB = getelementptr inbounds i32, i32* %b, i64 %ind
82   %loadB = load i32, i32* %arrayidxB, align 4
83
84   %mulA = mul i32 %loadB, %loadA
85
86   %add = add nuw nsw i64 %ind, 1
87   %arrayidxA_plus_4 = getelementptr inbounds i32, i32* %a, i64 %add
88   store i32 %mulA, i32* %arrayidxA_plus_4, align 4
89
90   %arrayidxD = getelementptr inbounds i32, i32* %d, i64 %ind
91   %loadD = load i32, i32* %arrayidxD, align 4
92
93   %arrayidxE = getelementptr inbounds i32, i32* %e, i64 %ind
94   %loadE = load i32, i32* %arrayidxE, align 4
95
96   %mulC = mul i32 %loadD, %loadE
97
98   %arrayidxC = getelementptr inbounds i32, i32* %c, i64 %ind
99   store i32 %mulC, i32* %arrayidxC, align 4
100
101   %exitcond = icmp eq i64 %add, 20
102   br i1 %exitcond, label %for.end, label %for.body
103
104 for.end:                                          ; preds = %for.body
105   ret void
106 }