[PATCH] D59218: [LSR] Fix signed overflow in GenerateCrossUseConstantOffsets.
Florian Hahn via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 11 09:42:12 PDT 2019
fhahn created this revision.
fhahn added reviewers: qcolombet, gilr.
Herald added subscribers: jdoerfert, hiraditya.
Herald added a project: LLVM.
For the attached test case, unchecked addition of immediate starts and
ends overflows, as they can be arbitrary i64 constants.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D59218
Files:
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
llvm/test/Transforms/LoopStrengthReduce/lsr-overflow2.ll
Index: llvm/test/Transforms/LoopStrengthReduce/lsr-overflow2.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/LoopStrengthReduce/lsr-overflow2.ll
@@ -0,0 +1,39 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -lsr-complexity-limit=50 -loop-reduce -S %s | FileCheck %s
+
+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: [[TMP0:%.*]] = add i64 [[A:%.*]], -1
+; CHECK-NEXT: [[TMP1:%.*]] = add i64 [[A]], -9223372036854775808
+; CHECK-NEXT: br label [[BB1:%.*]]
+; CHECK: bb1:
+; CHECK-NEXT: [[LSR_IV1:%.*]] = phi i64 [ [[LSR_IV_NEXT2:%.*]], [[BB1]] ], [ [[TMP1]], [[BB:%.*]] ]
+; CHECK-NEXT: [[LSR_IV:%.*]] = phi i64 [ [[LSR_IV_NEXT:%.*]], [[BB1]] ], [ [[TMP0]], [[BB]] ]
+; CHECK-NEXT: [[TMP4:%.*]] = icmp ne i64 [[LSR_IV1]], 0
+; CHECK-NEXT: [[TMP5:%.*]] = and i1 [[TMP4]], true
+; CHECK-NEXT: [[LSR_IV_NEXT]] = add i64 [[LSR_IV]], 1
+; CHECK-NEXT: [[LSR_IV_NEXT2]] = add i64 [[LSR_IV1]], 1
+; CHECK-NEXT: br i1 [[TMP5]], label [[BB1]], label [[BB7:%.*]]
+; CHECK: bb7:
+; CHECK-NEXT: [[TMP9:%.*]] = and i64 [[LSR_IV_NEXT]], 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
+}
Index: llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
+++ llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
@@ -4146,12 +4146,13 @@
// Conservatively examine offsets between this orig reg a few selected
// other orig regs.
- ImmMapTy::const_iterator OtherImms[] = {
- Imms.begin(), std::prev(Imms.end()),
- Imms.lower_bound((Imms.begin()->first + std::prev(Imms.end())->first) /
- 2)
- };
- for (size_t i = 0, e = array_lengthof(OtherImms); i != e; ++i) {
+ SmallVector<ImmMapTy::const_iterator, 3> OtherImms = {
+ Imms.begin(), std::prev(Imms.end())};
+ Optional<int64_t> MaybeResult =
+ checkedAdd(Imms.begin()->first, std::prev(Imms.end())->first);
+ if (MaybeResult)
+ OtherImms.push_back(Imms.lower_bound(*MaybeResult / 2));
+ for (size_t i = 0, e = OtherImms.size(); i != e; ++i) {
ImmMapTy::const_iterator M = OtherImms[i];
if (M == J || M == JE) continue;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59218.190110.patch
Type: text/x-patch
Size: 2929 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190311/a39ee0c3/attachment.bin>
More information about the llvm-commits
mailing list