1; RUN: opt -passes=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 17define void @f(ptr %a1, ptr %a2, ptr %b, ptr %c1, ptr %c2, ptr %d, ptr %e) { 18entry: 19 20 %cond = icmp eq ptr %e, null 21 br i1 %cond, label %one, label %two 22one: 23 br label %join 24two: 25 br label %join 26join: 27 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. 31; 32; These are the steps: 33; 34; 1. After the bounds for A and C are first expanded: 35; 36; join: 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* 41; 42; 2. After A is expanded again: 43; 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 50; 51; 3. Finally, when C is expanded again: 52; 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* 60 61 %a = phi ptr [%a1, %one], [%a2, %two] 62 %c = phi ptr [%c1, %one], [%c2, %two] 63 br label %for.body 64 65; CHECK: join 66; CHECK-NOT: bitcast ptr %c to ptr 67; CHECK-NOT: bitcast ptr %a to ptr 68 69for.body: ; preds = %for.body, %entry 70 %ind = phi i64 [ 0, %join ], [ %add, %for.body ] 71 72 %arrayidxA = getelementptr inbounds i32, ptr %a, i64 %ind 73 %loadA = load i32, ptr %arrayidxA, align 4 74 75 %arrayidxB = getelementptr inbounds i32, ptr %b, i64 %ind 76 %loadB = load i32, ptr %arrayidxB, align 4 77 78 %mulA = mul i32 %loadB, %loadA 79 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 83 84 %arrayidxD = getelementptr inbounds i32, ptr %d, i64 %ind 85 %loadD = load i32, ptr %arrayidxD, align 4 86 87 %arrayidxE = getelementptr inbounds i32, ptr %e, i64 %ind 88 %loadE = load i32, ptr %arrayidxE, align 4 89 90 %mulC = mul i32 %loadD, %loadE 91 92 %arrayidxC = getelementptr inbounds i32, ptr %c, i64 %ind 93 store i32 %mulC, ptr %arrayidxC, align 4 94 95 %exitcond = icmp eq i64 %add, 20 96 br i1 %exitcond, label %for.end, label %for.body 97 98for.end: ; preds = %for.body 99 ret void 100} 101