From: Max Kazantsev Date: Tue, 29 Aug 2017 07:32:20 +0000 (+0000) Subject: [LSR] Fix Shadow IV in case of integer overflow X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b074309e7051d5e5eddcacf317a50a2424e3dc41;p=llvm [LSR] Fix Shadow IV in case of integer overflow When LSR processes code like int accumulator = 0; for (int i = 0; i < N; i++) { accummulator += i; use((double) accummulator); } It may decide to replace integer `accumulator` with a double Shadow IV to get rid of casts. The problem with that is that the `accumulator`'s value may overflow. Starting from this moment, the behavior of integer and double accumulators will differ. This patch strenghtens up the conditions of Shadow IV mechanism applicability. We only allow it for IVs that are proved to be `AddRec`s with `nsw`/`nuw` flag. Differential Revision: https://reviews.llvm.org/D37209 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@311986 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/lib/Transforms/Scalar/LoopStrengthReduce.cpp index fdd71df2403..6462e3fb856 100644 --- a/lib/Transforms/Scalar/LoopStrengthReduce.cpp +++ b/lib/Transforms/Scalar/LoopStrengthReduce.cpp @@ -2027,6 +2027,14 @@ void LSRInstance::OptimizeShadowIV() { if (!PH) continue; if (PH->getNumIncomingValues() != 2) continue; + // If the calculation in integers overflows, the result in FP type will + // differ. So we only can do this transformation if we are guaranteed to not + // deal with overflowing values + const SCEVAddRecExpr *AR = dyn_cast(SE.getSCEV(PH)); + if (!AR) continue; + if (IsSigned && !AR->hasNoSignedWrap()) continue; + if (!IsSigned && !AR->hasNoUnsignedWrap()) continue; + Type *SrcTy = PH->getType(); int Mantissa = DestTy->getFPMantissaWidth(); if (Mantissa == -1) continue; diff --git a/test/Transforms/LoopStrengthReduce/X86/2008-08-14-ShadowIV.ll b/test/Transforms/LoopStrengthReduce/X86/2008-08-14-ShadowIV.ll index 858b61a9836..da14e631f51 100644 --- a/test/Transforms/LoopStrengthReduce/X86/2008-08-14-ShadowIV.ll +++ b/test/Transforms/LoopStrengthReduce/X86/2008-08-14-ShadowIV.ll @@ -114,6 +114,100 @@ return: ; preds = %bb, %entry ret void } +; Unable to eliminate cast because the integer IV overflows (accum exceeds +; SINT_MAX). + +define i32 @foobar5() { +; CHECK-LABEL: foobar5( +; CHECK-NOT: phi double +; CHECK-NOT: phi float +entry: + br label %loop + +loop: + %accum = phi i32 [ -3220, %entry ], [ %accum.next, %loop ] + %iv = phi i32 [ 12, %entry ], [ %iv.next, %loop ] + %tmp1 = sitofp i32 %accum to double + tail call void @foo( double %tmp1 ) nounwind + %accum.next = add i32 %accum, 9597741 + %iv.next = add nuw nsw i32 %iv, 1 + %exitcond = icmp ugt i32 %iv, 235 + br i1 %exitcond, label %exit, label %loop + +exit: ; preds = %loop + ret i32 %accum.next +} + +; Can eliminate if we set nsw and, thus, think that we don't overflow SINT_MAX. + +define i32 @foobar6() { +; CHECK-LABEL: foobar6( +; CHECK: phi double + +entry: + br label %loop + +loop: + %accum = phi i32 [ -3220, %entry ], [ %accum.next, %loop ] + %iv = phi i32 [ 12, %entry ], [ %iv.next, %loop ] + %tmp1 = sitofp i32 %accum to double + tail call void @foo( double %tmp1 ) nounwind + %accum.next = add nsw i32 %accum, 9597741 + %iv.next = add nuw nsw i32 %iv, 1 + %exitcond = icmp ugt i32 %iv, 235 + br i1 %exitcond, label %exit, label %loop + +exit: ; preds = %loop + ret i32 %accum.next +} + +; Unable to eliminate cast because the integer IV overflows (accum exceeds +; UINT_MAX). + +define i32 @foobar7() { +; CHECK-LABEL: foobar7( +; CHECK-NOT: phi double +; CHECK-NOT: phi float +entry: + br label %loop + +loop: + %accum = phi i32 [ -3220, %entry ], [ %accum.next, %loop ] + %iv = phi i32 [ 12, %entry ], [ %iv.next, %loop ] + %tmp1 = uitofp i32 %accum to double + tail call void @foo( double %tmp1 ) nounwind + %accum.next = add i32 %accum, 9597741 + %iv.next = add nuw nsw i32 %iv, 1 + %exitcond = icmp ugt i32 %iv, 235 + br i1 %exitcond, label %exit, label %loop + +exit: ; preds = %loop + ret i32 %accum.next +} + +; Can eliminate if we set nuw and, thus, think that we don't overflow UINT_MAX. + +define i32 @foobar8() { +; CHECK-LABEL: foobar8( +; CHECK: phi double + +entry: + br label %loop + +loop: + %accum = phi i32 [ -3220, %entry ], [ %accum.next, %loop ] + %iv = phi i32 [ 12, %entry ], [ %iv.next, %loop ] + %tmp1 = uitofp i32 %accum to double + tail call void @foo( double %tmp1 ) nounwind + %accum.next = add nuw i32 %accum, 9597741 + %iv.next = add nuw nsw i32 %iv, 1 + %exitcond = icmp ugt i32 %iv, 235 + br i1 %exitcond, label %exit, label %loop + +exit: ; preds = %loop + ret i32 %accum.next +} + declare void @bar(i32) declare void @foo(double)