[llvm-branch-commits] [llvm-branch] r232512 - Merging r229352:

Tom Stellard thomas.stellard at amd.com
Tue Mar 17 11:09:58 PDT 2015


Author: tstellar
Date: Tue Mar 17 13:09:58 2015
New Revision: 232512

URL: http://llvm.org/viewvc/llvm-project?rev=232512&view=rev
Log:
Merging r229352:

------------------------------------------------------------------------
r229352 | david.majnemer | 2015-02-15 23:02:09 -0500 (Sun, 15 Feb 2015) | 9 lines

IR: Properly return nullptr when getAggregateElement is out-of-bounds

We didn't properly handle the out-of-bounds case for
ConstantAggregateZero and UndefValue.  This would manifest as a crash
when the constant folder was asked to fold a load of a constant global
whose struct type has no operands.

This fixes PR22595.

------------------------------------------------------------------------

Added:
    llvm/branches/release_35/test/Transforms/InstSimplify/load.ll
Modified:
    llvm/branches/release_35/include/llvm/IR/Constants.h
    llvm/branches/release_35/lib/IR/Constants.cpp

Modified: llvm/branches/release_35/include/llvm/IR/Constants.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_35/include/llvm/IR/Constants.h?rev=232512&r1=232511&r2=232512&view=diff
==============================================================================
--- llvm/branches/release_35/include/llvm/IR/Constants.h (original)
+++ llvm/branches/release_35/include/llvm/IR/Constants.h Tue Mar 17 13:09:58 2015
@@ -326,6 +326,9 @@ public:
   /// index.
   Constant *getElementValue(unsigned Idx) const;
 
+  /// \brief Return the number of elements in the array, vector, or struct.
+  unsigned getNumElements() const;
+
   /// Methods for support type inquiry through isa, cast, and dyn_cast:
   ///
   static bool classof(const Value *V) {
@@ -1164,6 +1167,9 @@ public:
   /// index.
   UndefValue *getElementValue(unsigned Idx) const;
 
+  /// \brief Return the number of elements in the array, vector, or struct.
+  unsigned getNumElements() const;
+
   void destroyConstant() override;
 
   /// Methods for support type inquiry through isa, cast, and dyn_cast:

Modified: llvm/branches/release_35/lib/IR/Constants.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_35/lib/IR/Constants.cpp?rev=232512&r1=232511&r2=232512&view=diff
==============================================================================
--- llvm/branches/release_35/lib/IR/Constants.cpp (original)
+++ llvm/branches/release_35/lib/IR/Constants.cpp Tue Mar 17 13:09:58 2015
@@ -212,11 +212,11 @@ Constant *Constant::getAggregateElement(
   if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
     return Elt < CV->getNumOperands() ? CV->getOperand(Elt) : nullptr;
 
-  if (const ConstantAggregateZero *CAZ =dyn_cast<ConstantAggregateZero>(this))
-    return CAZ->getElementValue(Elt);
+  if (const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(this))
+    return Elt < CAZ->getNumElements() ? CAZ->getElementValue(Elt) : nullptr;
 
   if (const UndefValue *UV = dyn_cast<UndefValue>(this))
-    return UV->getElementValue(Elt);
+    return Elt < UV->getNumElements() ? UV->getElementValue(Elt) : nullptr;
 
   if (const ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(this))
     return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt)
@@ -721,6 +721,14 @@ Constant *ConstantAggregateZero::getElem
   return getStructElement(Idx);
 }
 
+unsigned ConstantAggregateZero::getNumElements() const {
+  const Type *Ty = getType();
+  if (const auto *AT = dyn_cast<ArrayType>(Ty))
+    return AT->getNumElements();
+  if (const auto *VT = dyn_cast<VectorType>(Ty))
+    return VT->getNumElements();
+  return Ty->getStructNumElements();
+}
 
 //===----------------------------------------------------------------------===//
 //                         UndefValue Implementation
@@ -754,7 +762,14 @@ UndefValue *UndefValue::getElementValue(
   return getStructElement(Idx);
 }
 
-
+unsigned UndefValue::getNumElements() const {
+  const Type *Ty = getType();
+  if (const auto *AT = dyn_cast<ArrayType>(Ty))
+    return AT->getNumElements();
+  if (const auto *VT = dyn_cast<VectorType>(Ty))
+    return VT->getNumElements();
+  return Ty->getStructNumElements();
+}
 
 //===----------------------------------------------------------------------===//
 //                            ConstantXXX Classes

Added: llvm/branches/release_35/test/Transforms/InstSimplify/load.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_35/test/Transforms/InstSimplify/load.ll?rev=232512&view=auto
==============================================================================
--- llvm/branches/release_35/test/Transforms/InstSimplify/load.ll (added)
+++ llvm/branches/release_35/test/Transforms/InstSimplify/load.ll Tue Mar 17 13:09:58 2015
@@ -0,0 +1,19 @@
+; RUN: opt < %s -instsimplify -S | FileCheck %s
+
+ at zeroinit = constant {} zeroinitializer
+ at undef = constant {} undef
+
+define i32 @crash_on_zeroinit() {
+; CHECK-LABEL: @crash_on_zeroinit
+; CHECK: ret i32 0
+  %load = load i32* bitcast ({}* @zeroinit to i32*)
+  ret i32 %load
+}
+
+define i32 @crash_on_undef() {
+; CHECK-LABEL: @crash_on_undef
+; CHECK: ret i32 undef
+  %load = load i32* bitcast ({}* @undef to i32*)
+  ret i32 %load
+}
+





More information about the llvm-branch-commits mailing list