From 3a168569fcd4ed97d5dc75d56dd425319f22bce6 Mon Sep 17 00:00:00 2001 From: James Molloy Date: Wed, 5 Apr 2017 09:24:26 +0000 Subject: [PATCH] [LAA] Correctly return a half-open range in expandBounds This is a latent bug that's been hanging around for a while. For a loop-invariant pointer, expandBounds would return the range {Ptr, Ptr}, but this was interpreted as a half-open range, not a closed range. So we ended up planting incorrect bounds checks. Even worse, they were tautological, so we ended up incorrectly executing the optimized loop. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@299526 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/LoopAccessAnalysis.cpp | 5 ++++- test/Transforms/LoopVersioning/loop-invariant-bound.ll | 7 ++++--- test/Transforms/LoopVersioningLICM/loopversioningLICM1.ll | 2 +- test/Transforms/LoopVersioningLICM/loopversioningLICM2.ll | 2 +- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/Analysis/LoopAccessAnalysis.cpp b/lib/Analysis/LoopAccessAnalysis.cpp index 72488f1f080..4ba12583ff8 100644 --- a/lib/Analysis/LoopAccessAnalysis.cpp +++ b/lib/Analysis/LoopAccessAnalysis.cpp @@ -1939,7 +1939,10 @@ expandBounds(const RuntimePointerChecking::CheckingPtrGroup *CG, Loop *TheLoop, Value *NewPtr = (Inst && TheLoop->contains(Inst)) ? Exp.expandCodeFor(Sc, PtrArithTy, Loc) : Ptr; - return {NewPtr, NewPtr}; + // We must return a half-open range, which means incrementing Sc. + const SCEV *ScPlusOne = SE->getAddExpr(Sc, SE->getOne(PtrArithTy)); + Value *NewPtrPlusOne = Exp.expandCodeFor(ScPlusOne, PtrArithTy, Loc); + return {NewPtr, NewPtrPlusOne}; } else { Value *Start = nullptr, *End = nullptr; DEBUG(dbgs() << "LAA: Adding RT check for range:\n"); diff --git a/test/Transforms/LoopVersioning/loop-invariant-bound.ll b/test/Transforms/LoopVersioning/loop-invariant-bound.ll index 3411adbf245..01c5a55bd5b 100644 --- a/test/Transforms/LoopVersioning/loop-invariant-bound.ll +++ b/test/Transforms/LoopVersioning/loop-invariant-bound.ll @@ -8,12 +8,13 @@ target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" %Partials.215 = type { [2 x %Dual.213] } ; Function Attrs: sspreq -define void @"julia_axpy!_65480"(%Dual.212*) { +define void @"julia_axpy!_65480"(%Dual.212*, %Dual.212* %other) { top: br label %if24 ; CHECK-NOT: %bc = bitcast i64* %v2.sroa.0.0..sroa_cast -; CHECK: %bound0 +; CHECK: %bound0 = icmp ult i8* %[[x:[a-z0-9]+]], %[[y:[a-z0-9]+]] +; CHECK-NOT: %bound1 = icmp ult i8* %[[y]], %[[x]] if24: ; preds = %if24, %top %"#temp#1.sroa.3.02" = phi i64 [ undef, %top ], [ %2, %if24 ] @@ -24,7 +25,7 @@ if24: ; preds = %if24, %top %v2.sroa.0.0..sroa_cast = bitcast %Dual.212* %0 to i64* %v2.sroa.0.0.copyload = load i64, i64* %v2.sroa.0.0..sroa_cast, align 1 %3 = add i64 %"#temp#1.sroa.0.01", -1 - %4 = getelementptr inbounds %Dual.212, %Dual.212* undef, i64 %3, i32 1, i32 0, i64 0, i32 1, i32 0, i64 0 + %4 = getelementptr inbounds %Dual.212, %Dual.212* %other, i64 0, i32 1, i32 0, i64 0, i32 1, i32 0, i64 0 %5 = bitcast double* %4 to i64* store i64 undef, i64* %5, align 8 %notlhs27 = icmp eq i64 %2, undef diff --git a/test/Transforms/LoopVersioningLICM/loopversioningLICM1.ll b/test/Transforms/LoopVersioningLICM/loopversioningLICM1.ll index ff6c25087aa..791c2e3210c 100644 --- a/test/Transforms/LoopVersioningLICM/loopversioningLICM1.ll +++ b/test/Transforms/LoopVersioningLICM/loopversioningLICM1.ll @@ -16,7 +16,7 @@ ; CHECK-NEXT: %add8 = add nsw i32 %[[induction]], %add ; CHECK-NEXT: %inc = add nuw i32 %j.113, 1 ; CHECK-NEXT: %cmp2 = icmp ult i32 %inc, %itr -; CHECK-NEXT: br i1 %cmp2, label %for.body3, label %for.inc11.loopexit.loopexit6, !llvm.loop !5 +; CHECK-NEXT: br i1 %cmp2, label %for.body3, label %for.inc11.loopexit.loopexit7, !llvm.loop !5 define i32 @foo(i32* nocapture %var1, i32* nocapture readnone %var2, i32* nocapture %var3, i32 %itr) #0 { entry: %cmp14 = icmp eq i32 %itr, 0 diff --git a/test/Transforms/LoopVersioningLICM/loopversioningLICM2.ll b/test/Transforms/LoopVersioningLICM/loopversioningLICM2.ll index 928a6527bad..53add633802 100644 --- a/test/Transforms/LoopVersioningLICM/loopversioningLICM2.ll +++ b/test/Transforms/LoopVersioningLICM/loopversioningLICM2.ll @@ -7,7 +7,7 @@ ; CHECK: Loop: Loop at depth 2 containing: %for.body3.us
; CHECK-NEXT: Loop Versioning found to be beneficial ; -; CHECK: for.cond1.for.inc17_crit_edge.us.loopexit5: ; preds = %for.body3.us +; CHECK: for.cond1.for.inc17_crit_edge.us.loopexit6: ; preds = %for.body3.us ; CHECK-NEXT: %add14.us.lcssa = phi float [ %add14.us, %for.body3.us ] ; CHECK-NEXT: store float %add14.us.lcssa, float* %arrayidx.us, align 4, !alias.scope !0, !noalias !0 ; CHECK-NEXT: br label %for.cond1.for.inc17_crit_edge.us -- 2.50.1