[llvm] r356256 - [LSR] Check for signed overflow in NarrowSearchSpaceByDetectingSupersets.
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 15 05:17:36 PDT 2019
Author: fhahn
Date: Fri Mar 15 05:17:36 2019
New Revision: 356256
URL: http://llvm.org/viewvc/llvm-project?rev=356256&view=rev
Log:
[LSR] Check for signed overflow in NarrowSearchSpaceByDetectingSupersets.
We are adding a sign extended IR value to an int64_t, which can cause
signed overflows, as in the attached test case, where we have a formula
with BaseOffset = -1 and a constant with numeric_limits<int64_t>::min().
If the addition would overflow, skip the simplification for this
formula. Note that the target triple is required to trigger the failure.
Reviewers: qcolombet, gilr, kparzysz, efriedma
Reviewed By: efriedma
Differential Revision: https://reviews.llvm.org/D59211
Added:
llvm/trunk/test/Transforms/LoopStrengthReduce/X86/lsr-overflow.ll
Modified:
llvm/trunk/lib/Transforms/Scalar/LoopStrengthReduce.cpp
Modified: llvm/trunk/lib/Transforms/Scalar/LoopStrengthReduce.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopStrengthReduce.cpp?rev=356256&r1=356255&r2=356256&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/LoopStrengthReduce.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/LoopStrengthReduce.cpp Fri Mar 15 05:17:36 2019
@@ -4423,7 +4423,9 @@ void LSRInstance::NarrowSearchSpaceByDet
I = F.BaseRegs.begin(), E = F.BaseRegs.end(); I != E; ++I) {
if (const SCEVConstant *C = dyn_cast<SCEVConstant>(*I)) {
Formula NewF = F;
- NewF.BaseOffset += C->getValue()->getSExtValue();
+ //FIXME: Formulas should store bitwidth to do wrapping properly.
+ // See PR41034.
+ NewF.BaseOffset += (uint64_t)C->getValue()->getSExtValue();
NewF.BaseRegs.erase(NewF.BaseRegs.begin() +
(I - F.BaseRegs.begin()));
if (LU.HasFormulaWithSameRegs(NewF)) {
Added: llvm/trunk/test/Transforms/LoopStrengthReduce/X86/lsr-overflow.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LoopStrengthReduce/X86/lsr-overflow.ll?rev=356256&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/LoopStrengthReduce/X86/lsr-overflow.ll (added)
+++ llvm/trunk/test/Transforms/LoopStrengthReduce/X86/lsr-overflow.ll Fri Mar 15 05:17:36 2019
@@ -0,0 +1,38 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -lsr-complexity-limit=50 -loop-reduce -S %s | FileCheck %s
+
+target triple = "x86_64-apple-macosx10.14.0"
+target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
+
+define void @overflow1(i64 %a) {
+; CHECK-LABEL: @overflow1(
+; CHECK-NEXT: bb:
+; CHECK-NEXT: br label [[BB1:%.*]]
+; CHECK: bb1:
+; CHECK-NEXT: [[TMP:%.*]] = phi i64 [ [[A:%.*]], [[BB:%.*]] ], [ [[TMP6:%.*]], [[BB1]] ]
+; CHECK-NEXT: [[TMP0:%.*]] = add i64 [[TMP]], -9223372036854775808
+; CHECK-NEXT: [[TMP4:%.*]] = icmp ne i64 [[TMP0]], 0
+; CHECK-NEXT: [[TMP5:%.*]] = and i1 [[TMP4]], true
+; CHECK-NEXT: [[TMP6]] = add i64 [[TMP]], 1
+; CHECK-NEXT: br i1 [[TMP5]], label [[BB1]], label [[BB7:%.*]]
+; CHECK: bb7:
+; CHECK-NEXT: [[TMP1:%.*]] = add i64 [[TMP6]], -1
+; CHECK-NEXT: [[TMP9:%.*]] = and i64 [[TMP1]], 1
+; CHECK-NEXT: [[TMP10:%.*]] = icmp eq i64 [[TMP9]], 0
+; CHECK-NEXT: unreachable
+;
+bb:
+ br label %bb1
+
+bb1: ; preds = %bb1, %bb
+ %tmp = phi i64 [ %a, %bb ], [ %tmp6, %bb1 ]
+ %tmp4 = icmp ne i64 %tmp, -9223372036854775808
+ %tmp5 = and i1 %tmp4, 1
+ %tmp6 = add i64 %tmp, 1
+ br i1 %tmp5, label %bb1, label %bb7
+
+bb7: ; preds = %bb1
+ %tmp9 = and i64 %tmp, 1
+ %tmp10 = icmp eq i64 %tmp9, 0
+ unreachable
+}
More information about the llvm-commits
mailing list