1 ; RUN: opt -passes=loop-distribute -enable-loop-distribute -S < %s | FileCheck %s
3 ; When emitting the memchecks for:
5 ; for (i = 0; i < n; i++) {
6 ; A[i + 1] = A[i] * B[i];
7 ; =======================
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.
17 define void @f(ptr %a1, ptr %a2, ptr %b, ptr %c1, ptr %c2, ptr %d, ptr %e) {
20 %cond = icmp eq ptr %e, null
21 br i1 %cond, label %one, label %two
28 ; The pointers need to be defined by PHIs in order for the bug to trigger.
29 ; Because of the PHIs the existing casts won't be at the desired location so a
30 ; new cast will be emitted and the old cast will get invalidated.
32 ; These are the steps:
34 ; 1. After the bounds for A and C are first expanded:
37 ; %a = phi i32* [ %a1, %one ], [ %a2, %two ]
38 ; %c = phi i32* [ %c1, %one ], [ %c2, %two ]
39 ; %c5 = bitcast i32* %c to i8*
40 ; %a3 = bitcast i32* %a to i8*
42 ; 2. After A is expanded again:
44 ; join: ; preds = %two, %one
45 ; %a = phi i32* [ %a1, %one ], [ %a2, %two ]
46 ; %c = phi i32* [ %c1, %one ], [ %c2, %two ]
47 ; %a3 = bitcast i32* %a to i8* <--- new
48 ; %c5 = bitcast i32* %c to i8*
49 ; %0 = bitcast i32* %a to i8* <--- old, invalidated
51 ; 3. Finally, when C is expanded again:
53 ; join: ; preds = %two, %one
54 ; %a = phi i32* [ %a1, %one ], [ %a2, %two ]
55 ; %c = phi i32* [ %c1, %one ], [ %c2, %two ]
56 ; %c5 = bitcast i32* %c to i8* <--- new
57 ; %a3 = bitcast i32* %a to i8*
58 ; %0 = bitcast i32* %c to i8* <--- old, invalidated
59 ; %1 = bitcast i32* %a to i8*
61 %a = phi ptr [%a1, %one], [%a2, %two]
62 %c = phi ptr [%c1, %one], [%c2, %two]
66 ; CHECK-NOT: bitcast ptr %c to ptr
67 ; CHECK-NOT: bitcast ptr %a to ptr
69 for.body: ; preds = %for.body, %entry
70 %ind = phi i64 [ 0, %join ], [ %add, %for.body ]
72 %arrayidxA = getelementptr inbounds i32, ptr %a, i64 %ind
73 %loadA = load i32, ptr %arrayidxA, align 4
75 %arrayidxB = getelementptr inbounds i32, ptr %b, i64 %ind
76 %loadB = load i32, ptr %arrayidxB, align 4
78 %mulA = mul i32 %loadB, %loadA
80 %add = add nuw nsw i64 %ind, 1
81 %arrayidxA_plus_4 = getelementptr inbounds i32, ptr %a, i64 %add
82 store i32 %mulA, ptr %arrayidxA_plus_4, align 4
84 %arrayidxD = getelementptr inbounds i32, ptr %d, i64 %ind
85 %loadD = load i32, ptr %arrayidxD, align 4
87 %arrayidxE = getelementptr inbounds i32, ptr %e, i64 %ind
88 %loadE = load i32, ptr %arrayidxE, align 4
90 %mulC = mul i32 %loadD, %loadE
92 %arrayidxC = getelementptr inbounds i32, ptr %c, i64 %ind
93 store i32 %mulC, ptr %arrayidxC, align 4
95 %exitcond = icmp eq i64 %add, 20
96 br i1 %exitcond, label %for.end, label %for.body
98 for.end: ; preds = %for.body