1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-01-31 20:51:52 +01:00

[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

llvm-svn: 311986
This commit is contained in:
Max Kazantsev 2017-08-29 07:32:20 +00:00
parent 65a3350eeb
commit 4e5297ed76
2 changed files with 102 additions and 0 deletions

View File

@ -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<SCEVAddRecExpr>(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;

View File

@ -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)