1 ; RUN: opt -basicaa -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 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
19 define void @f(i32* %a1, i32* %a2,
26 %cond = icmp eq i32* %e, null
27 br i1 %cond, label %one, label %two
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.
38 ; These are the steps:
40 ; 1. After the bounds for A and C are first expanded:
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*
48 ; 2. After A is expanded again:
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
57 ; 3. Finally, when C is expanded again:
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*
67 %a = phi i32* [%a1, %one], [%a2, %two]
68 %c = phi i32* [%c1, %one], [%c2, %two]
72 ; CHECK: [[VALUE:%[0-9a-z]+]] = bitcast i32* undef to i8*
73 ; CHECK-NOT: [[VALUE]]
75 for.body: ; preds = %for.body, %entry
76 %ind = phi i64 [ 0, %join ], [ %add, %for.body ]
78 %arrayidxA = getelementptr inbounds i32, i32* %a, i64 %ind
79 %loadA = load i32, i32* %arrayidxA, align 4
81 %arrayidxB = getelementptr inbounds i32, i32* %b, i64 %ind
82 %loadB = load i32, i32* %arrayidxB, align 4
84 %mulA = mul i32 %loadB, %loadA
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
90 %arrayidxD = getelementptr inbounds i32, i32* %d, i64 %ind
91 %loadD = load i32, i32* %arrayidxD, align 4
93 %arrayidxE = getelementptr inbounds i32, i32* %e, i64 %ind
94 %loadE = load i32, i32* %arrayidxE, align 4
96 %mulC = mul i32 %loadD, %loadE
98 %arrayidxC = getelementptr inbounds i32, i32* %c, i64 %ind
99 store i32 %mulC, i32* %arrayidxC, align 4
101 %exitcond = icmp eq i64 %add, 20
102 br i1 %exitcond, label %for.end, label %for.body
104 for.end: ; preds = %for.body