Commit da2fc4c9 authored by Hans Wennborg's avatar Hans Wennborg
Browse files

Merging r229352:

------------------------------------------------------------------------
r229352 | majnemer | 2015-02-15 20:02:09 -0800 (Sun, 15 Feb 2015) | 8 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.
------------------------------------------------------------------------

llvm-svn: 229588
parent a2c28378
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -325,6 +325,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) {
@@ -1196,6 +1199,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:
+19 −4
Original line number Diff line number Diff line
@@ -258,10 +258,10 @@ Constant *Constant::getAggregateElement(unsigned Elt) const {
    return Elt < CV->getNumOperands() ? CV->getOperand(Elt) : nullptr;

  if (const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(this))
    return CAZ->getElementValue(Elt);
    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)
@@ -764,6 +764,14 @@ Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const {
  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
@@ -797,7 +805,14 @@ UndefValue *UndefValue::getElementValue(unsigned Idx) const {
  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
+19 −0
Original line number Diff line number Diff line
; RUN: opt < %s -instsimplify -S | FileCheck %s

@zeroinit = constant {} zeroinitializer
@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
}