1; RUN: opt -basic-aa -loop-distribute -enable-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 17target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" 18 19define void @f(i32* %a1, i32* %a2, 20 i32* %b, 21 i32* %c1, i32* %c2, 22 i32* %d, 23 i32* %e) { 24entry: 25 26 %cond = icmp eq i32* %e, null 27 br i1 %cond, label %one, label %two 28one: 29 br label %join 30two: 31 br label %join 32join: 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* %a 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* %c to i8* <--- old, invalidated 65; %1 = bitcast i32* %a 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; CHECK: join 72; CHECK: {{%[0-9a-z]+}} = bitcast i32* %a to i8* 73; CHECK: {{%[0-9a-z]+}} = bitcast i32* %c to i8* 74; CHECK-NOT: bitcast i32* %c to i8* 75; CHECK-NOT: bitcast i32* %a to i8* 76 77for.body: ; preds = %for.body, %entry 78 %ind = phi i64 [ 0, %join ], [ %add, %for.body ] 79 80 %arrayidxA = getelementptr inbounds i32, i32* %a, i64 %ind 81 %loadA = load i32, i32* %arrayidxA, align 4 82 83 %arrayidxB = getelementptr inbounds i32, i32* %b, i64 %ind 84 %loadB = load i32, i32* %arrayidxB, align 4 85 86 %mulA = mul i32 %loadB, %loadA 87 88 %add = add nuw nsw i64 %ind, 1 89 %arrayidxA_plus_4 = getelementptr inbounds i32, i32* %a, i64 %add 90 store i32 %mulA, i32* %arrayidxA_plus_4, align 4 91 92 %arrayidxD = getelementptr inbounds i32, i32* %d, i64 %ind 93 %loadD = load i32, i32* %arrayidxD, align 4 94 95 %arrayidxE = getelementptr inbounds i32, i32* %e, i64 %ind 96 %loadE = load i32, i32* %arrayidxE, align 4 97 98 %mulC = mul i32 %loadD, %loadE 99 100 %arrayidxC = getelementptr inbounds i32, i32* %c, i64 %ind 101 store i32 %mulC, i32* %arrayidxC, align 4 102 103 %exitcond = icmp eq i64 %add, 20 104 br i1 %exitcond, label %for.end, label %for.body 105 106for.end: ; preds = %for.body 107 ret void 108} 109