101 lines · plain
1; RUN: opt -passes=loop-distribute -enable-loop-distribute -S < %s | FileCheck %s2 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 expanded12; multiple times and rely on the caching in SCEV expansion to avoid any13; redundancy. However, due to logic in SCEVExpander::ReuseOrCreateCast, we14; can get earlier expanded values invalidated when casts are used. This test15; 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, null21 br i1 %cond, label %one, label %two22one:23 br label %join24two:25 br label %join26join: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 a30; 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, %one45; %a = phi i32* [ %a1, %one ], [ %a2, %two ]46; %c = phi i32* [ %c1, %one ], [ %c2, %two ]47; %a3 = bitcast i32* %a to i8* <--- new48; %c5 = bitcast i32* %c to i8*49; %0 = bitcast i32* %a to i8* <--- old, invalidated50;51; 3. Finally, when C is expanded again:52;53; join: ; preds = %two, %one54; %a = phi i32* [ %a1, %one ], [ %a2, %two ]55; %c = phi i32* [ %c1, %one ], [ %c2, %two ]56; %c5 = bitcast i32* %c to i8* <--- new57; %a3 = bitcast i32* %a to i8*58; %0 = bitcast i32* %c to i8* <--- old, invalidated59; %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.body64 65; CHECK: join66; CHECK-NOT: bitcast ptr %c to ptr67; CHECK-NOT: bitcast ptr %a to ptr68 69for.body: ; preds = %for.body, %entry70 %ind = phi i64 [ 0, %join ], [ %add, %for.body ]71 72 %arrayidxA = getelementptr inbounds i32, ptr %a, i64 %ind73 %loadA = load i32, ptr %arrayidxA, align 474 75 %arrayidxB = getelementptr inbounds i32, ptr %b, i64 %ind76 %loadB = load i32, ptr %arrayidxB, align 477 78 %mulA = mul i32 %loadB, %loadA79 80 %add = add nuw nsw i64 %ind, 181 %arrayidxA_plus_4 = getelementptr inbounds i32, ptr %a, i64 %add82 store i32 %mulA, ptr %arrayidxA_plus_4, align 483 84 %arrayidxD = getelementptr inbounds i32, ptr %d, i64 %ind85 %loadD = load i32, ptr %arrayidxD, align 486 87 %arrayidxE = getelementptr inbounds i32, ptr %e, i64 %ind88 %loadE = load i32, ptr %arrayidxE, align 489 90 %mulC = mul i32 %loadD, %loadE91 92 %arrayidxC = getelementptr inbounds i32, ptr %c, i64 %ind93 store i32 %mulC, ptr %arrayidxC, align 494 95 %exitcond = icmp eq i64 %add, 2096 br i1 %exitcond, label %for.end, label %for.body97 98for.end: ; preds = %for.body99 ret void100}101