From bc8d40d85f3fa1e34569834916f18fecaa635152 Mon Sep 17 00:00:00 2001 From: John McCall Date: Fri, 24 Jun 2011 21:55:10 +0000 Subject: Change the IR-generation of VLAs so that we capture bounds, not sizes; so that we use well-typed allocas; and so that we properly recurse through the full set of variably-modified types. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@133827 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGExpr.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'lib/CodeGen/CGExpr.cpp') diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index af0af7fbef..e8d156ea64 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -1579,21 +1579,22 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) { // size is a VLA or Objective-C interface. llvm::Value *Address = 0; unsigned ArrayAlignment = 0; - if (const VariableArrayType *VAT = + if (const VariableArrayType *vla = getContext().getAsVariableArrayType(E->getType())) { - llvm::Value *VLASize = GetVLASize(VAT); + // The base must be a pointer, which is not an aggregate. Emit + // it. It needs to be emitted first in case it's what captures + // the VLA bounds. + Address = EmitScalarExpr(E->getBase()); - Idx = Builder.CreateMul(Idx, VLASize); + // The element count here is the total number of non-VLA elements. + llvm::Value *numElements = getVLASize(vla).first; - // The base must be a pointer, which is not an aggregate. Emit it. - llvm::Value *Base = EmitScalarExpr(E->getBase()); + Idx = Builder.CreateMul(Idx, numElements); - Address = EmitCastToVoidPtr(Base); if (getContext().getLangOptions().isSignedOverflowDefined()) Address = Builder.CreateGEP(Address, Idx, "arrayidx"); else Address = Builder.CreateInBoundsGEP(Address, Idx, "arrayidx"); - Address = Builder.CreateBitCast(Address, Base->getType()); } else if (const ObjCObjectType *OIT = E->getType()->getAs()){ // Indexing over an interface, as in "NSString *P; P[4];" llvm::Value *InterfaceSize = -- cgit v1.2.3