llvm.org GIT mirror llvm / 3f8396f
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. ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_35@232512 91177308-0d34-0410-b5e6-96231b3b80d8 Tom Stellard 5 years ago
3 changed file(s) with 44 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
324324 /// getElementValue - Return a zero of the right value for the specified GEP
325325 /// index.
326326 Constant *getElementValue(unsigned Idx) const;
327
328 /// \brief Return the number of elements in the array, vector, or struct.
329 unsigned getNumElements() const;
327330
328331 /// Methods for support type inquiry through isa, cast, and dyn_cast:
329332 ///
11631166 /// index.
11641167 UndefValue *getElementValue(unsigned Idx) const;
11651168
1169 /// \brief Return the number of elements in the array, vector, or struct.
1170 unsigned getNumElements() const;
1171
11661172 void destroyConstant() override;
11671173
11681174 /// Methods for support type inquiry through isa, cast, and dyn_cast:
211211 if (const ConstantVector *CV = dyn_cast(this))
212212 return Elt < CV->getNumOperands() ? CV->getOperand(Elt) : nullptr;
213213
214 if (const ConstantAggregateZero *CAZ =dyn_cast(this))
215 return CAZ->getElementValue(Elt);
214 if (const ConstantAggregateZero *CAZ = dyn_cast(this))
215 return Elt < CAZ->getNumElements() ? CAZ->getElementValue(Elt) : nullptr;
216216
217217 if (const UndefValue *UV = dyn_cast(this))
218 return UV->getElementValue(Elt);
218 return Elt < UV->getNumElements() ? UV->getElementValue(Elt) : nullptr;
219219
220220 if (const ConstantDataSequential *CDS =dyn_cast(this))
221221 return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt)
720720 return getStructElement(Idx);
721721 }
722722
723 unsigned ConstantAggregateZero::getNumElements() const {
724 const Type *Ty = getType();
725 if (const auto *AT = dyn_cast(Ty))
726 return AT->getNumElements();
727 if (const auto *VT = dyn_cast(Ty))
728 return VT->getNumElements();
729 return Ty->getStructNumElements();
730 }
723731
724732 //===----------------------------------------------------------------------===//
725733 // UndefValue Implementation
753761 return getStructElement(Idx);
754762 }
755763
756
764 unsigned UndefValue::getNumElements() const {
765 const Type *Ty = getType();
766 if (const auto *AT = dyn_cast(Ty))
767 return AT->getNumElements();
768 if (const auto *VT = dyn_cast(Ty))
769 return VT->getNumElements();
770 return Ty->getStructNumElements();
771 }
757772
758773 //===----------------------------------------------------------------------===//
759774 // ConstantXXX Classes
0 ; RUN: opt < %s -instsimplify -S | FileCheck %s
1
2 @zeroinit = constant {} zeroinitializer
3 @undef = constant {} undef
4
5 define i32 @crash_on_zeroinit() {
6 ; CHECK-LABEL: @crash_on_zeroinit
7 ; CHECK: ret i32 0
8 %load = load i32* bitcast ({}* @zeroinit to i32*)
9 ret i32 %load
10 }
11
12 define i32 @crash_on_undef() {
13 ; CHECK-LABEL: @crash_on_undef
14 ; CHECK: ret i32 undef
15 %load = load i32* bitcast ({}* @undef to i32*)
16 ret i32 %load
17 }
18