From e3e5f77d77e7ccc5c966e242f0cd0ccf6d3dae7f Mon Sep 17 00:00:00 2001 From: Matt Arsenault Date: Mon, 26 Aug 2013 23:29:33 +0000 Subject: [PATCH] Fix lint assert on integer vector division git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@189290 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/Lint.cpp | 40 +++++++++++-- test/Analysis/Lint/check-zero-divide.ll | 78 +++++++++++++++++++++++++ test/Analysis/Lint/lit.local.cfg | 1 + 3 files changed, 113 insertions(+), 6 deletions(-) create mode 100644 test/Analysis/Lint/check-zero-divide.ll create mode 100644 test/Analysis/Lint/lit.local.cfg diff --git a/lib/Analysis/Lint.cpp b/lib/Analysis/Lint.cpp index 9393508a9e6..2c8fc0bba41 100644 --- a/lib/Analysis/Lint.cpp +++ b/lib/Analysis/Lint.cpp @@ -504,14 +504,42 @@ void Lint::visitShl(BinaryOperator &I) { "Undefined result: Shift count out of range", &I); } -static bool isZero(Value *V, DataLayout *TD) { +static bool isZero(Value *V, DataLayout *DL) { // Assume undef could be zero. - if (isa(V)) return true; + if (isa(V)) + return true; - unsigned BitWidth = cast(V->getType())->getBitWidth(); - APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); - ComputeMaskedBits(V, KnownZero, KnownOne, TD); - return KnownZero.isAllOnesValue(); + VectorType *VecTy = dyn_cast(V->getType()); + if (!VecTy) { + unsigned BitWidth = V->getType()->getIntegerBitWidth(); + APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); + ComputeMaskedBits(V, KnownZero, KnownOne, DL); + return KnownZero.isAllOnesValue(); + } + + // Per-component check doesn't work with zeroinitializer + Constant *C = dyn_cast(V); + if (!C) + return false; + + if (C->isZeroValue()) + return true; + + // For a vector, KnownZero will only be true if all values are zero, so check + // this per component + unsigned BitWidth = VecTy->getElementType()->getIntegerBitWidth(); + for (unsigned I = 0, N = VecTy->getNumElements(); I != N; ++I) { + Constant *Elem = C->getAggregateElement(I); + if (isa(Elem)) + return true; + + APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); + ComputeMaskedBits(Elem, KnownZero, KnownOne, DL); + if (KnownZero.isAllOnesValue()) + return true; + } + + return false; } void Lint::visitSDiv(BinaryOperator &I) { diff --git a/test/Analysis/Lint/check-zero-divide.ll b/test/Analysis/Lint/check-zero-divide.ll new file mode 100644 index 00000000000..f4e79ed95f8 --- /dev/null +++ b/test/Analysis/Lint/check-zero-divide.ll @@ -0,0 +1,78 @@ +; RUN: opt -lint -disable-output %s 2>&1 | FileCheck %s + +define <2 x i32> @use_vector_sdiv(<2 x i32> %a) nounwind { + %b = sdiv <2 x i32> %a, + ret <2 x i32> %b +} + +define <2 x i32> @use_vector_srem(<2 x i32> %a) nounwind { + %b = srem <2 x i32> %a, + ret <2 x i32> %b +} + +define <2 x i32> @use_vector_udiv(<2 x i32> %a) nounwind { + %b = udiv <2 x i32> %a, + ret <2 x i32> %b +} + +define <2 x i32> @use_vector_urem(<2 x i32> %a) nounwind { + %b = urem <2 x i32> %a, + ret <2 x i32> %b +} + +define i32 @use_sdiv_by_zero(i32 %a) nounwind { +; CHECK: Undefined behavior: Division by zero +; CHECK-NEXT: %b = sdiv i32 %a, 0 + %b = sdiv i32 %a, 0 + ret i32 %b +} + +define i32 @use_sdiv_by_zeroinitializer(i32 %a) nounwind { +; CHECK: Undefined behavior: Division by zero +; CHECK-NEXT: %b = sdiv i32 %a, 0 + %b = sdiv i32 %a, zeroinitializer + ret i32 %b +} + +define <2 x i32> @use_vector_sdiv_by_zero_x(<2 x i32> %a) nounwind { +; CHECK: Undefined behavior: Division by zero +; CHECK-NEXT: %b = sdiv <2 x i32> %a, + %b = sdiv <2 x i32> %a, + ret <2 x i32> %b +} + +define <2 x i32> @use_vector_sdiv_by_zero_y(<2 x i32> %a) nounwind { +; CHECK: Undefined behavior: Division by zero +; CHECK-NEXT: %b = sdiv <2 x i32> %a, + %b = sdiv <2 x i32> %a, + ret <2 x i32> %b +} + +define <2 x i32> @use_vector_sdiv_by_zero_xy(<2 x i32> %a) nounwind { +; CHECK: Undefined behavior: Division by zero +; CHECK-NEXT: %b = sdiv <2 x i32> %a, zeroinitializer + %b = sdiv <2 x i32> %a, + ret <2 x i32> %b +} + +define <2 x i32> @use_vector_sdiv_by_undef_x(<2 x i32> %a) nounwind { +; CHECK: Undefined behavior: Division by zero +; CHECK-NEXT: %b = sdiv <2 x i32> %a, + %b = sdiv <2 x i32> %a, + ret <2 x i32> %b +} + +define <2 x i32> @use_vector_sdiv_by_undef_y(<2 x i32> %a) nounwind { +; CHECK: Undefined behavior: Division by zero +; CHECK-NEXT: %b = sdiv <2 x i32> %a, + %b = sdiv <2 x i32> %a, + ret <2 x i32> %b +} + +define <2 x i32> @use_vector_sdiv_by_undef_xy(<2 x i32> %a) nounwind { +; CHECK: Undefined behavior: Division by zero +; CHECK-NEXT: %b = sdiv <2 x i32> %a, undef + %b = sdiv <2 x i32> %a, + ret <2 x i32> %b +} + diff --git a/test/Analysis/Lint/lit.local.cfg b/test/Analysis/Lint/lit.local.cfg new file mode 100644 index 00000000000..c6106e4746f --- /dev/null +++ b/test/Analysis/Lint/lit.local.cfg @@ -0,0 +1 @@ +config.suffixes = ['.ll']