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* %a 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* %c to i8* <--- old, invalidated
65 ; %1 = bitcast i32* %a to i8*
67 %a = phi i32* [%a1, %one], [%a2, %two]
68 %c = phi i32* [%c1, %one], [%c2, %two]
72 ; CHECK: {{%[0-9a-z]+}} = bitcast i32* %c to i8*
73 ; CHECK: {{%[0-9a-z]+}} = bitcast i32* %a to i8*
74 ; CHECK: [[OLD_C:%[0-9a-z]+]] = bitcast i32* %c to i8*
75 ; CHECK: [[OLD_A:%[0-9a-z]+]] = bitcast i32* %a to i8*
76 ; CHECK-NOT: [[OLD_C]]
77 ; CHECK-NOT: [[OLD_A]]
79 for.body: ; preds = %for.body, %entry
80 %ind = phi i64 [ 0, %join ], [ %add, %for.body ]
82 %arrayidxA = getelementptr inbounds i32, i32* %a, i64 %ind
83 %loadA = load i32, i32* %arrayidxA, align 4
85 %arrayidxB = getelementptr inbounds i32, i32* %b, i64 %ind
86 %loadB = load i32, i32* %arrayidxB, align 4
88 %mulA = mul i32 %loadB, %loadA
90 %add = add nuw nsw i64 %ind, 1
91 %arrayidxA_plus_4 = getelementptr inbounds i32, i32* %a, i64 %add
92 store i32 %mulA, i32* %arrayidxA_plus_4, align 4
94 %arrayidxD = getelementptr inbounds i32, i32* %d, i64 %ind
95 %loadD = load i32, i32* %arrayidxD, align 4
97 %arrayidxE = getelementptr inbounds i32, i32* %e, i64 %ind
98 %loadE = load i32, i32* %arrayidxE, align 4
100 %mulC = mul i32 %loadD, %loadE
102 %arrayidxC = getelementptr inbounds i32, i32* %c, i64 %ind
103 store i32 %mulC, i32* %arrayidxC, align 4
105 %exitcond = icmp eq i64 %add, 20
106 br i1 %exitcond, label %for.end, label %for.body
108 for.end: ; preds = %for.body