From 31f8649f839ff3e8238543a07ec0bc3ee1213e57 Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Thu, 5 Feb 2009 19:43:10 +0000 Subject: [PATCH] Follow Eli's advice and store the VLA size with the native size_t type. Fixes PR3491. llvm-svn: 63879 --- clang/lib/CodeGen/CGDecl.cpp | 3 +++ clang/lib/CodeGen/CGExprScalar.cpp | 4 +--- clang/lib/CodeGen/CodeGenFunction.cpp | 11 ++++++----- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp index 898c1bfd80db..73d3e2103c1f 100644 --- a/clang/lib/CodeGen/CGDecl.cpp +++ b/clang/lib/CodeGen/CGDecl.cpp @@ -197,6 +197,9 @@ void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) { llvm::Value *VLASize = EmitVLASize(Ty); + // Downcast the VLA size expression + VLASize = Builder.CreateIntCast(VLASize, llvm::Type::Int32Ty, false, "tmp"); + // Allocate memory for the array. llvm::Value *VLA = Builder.CreateAlloca(llvm::Type::Int8Ty, VLASize, "vla"); DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp"); diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp index 44eefea71dbd..b9dcc078d4db 100644 --- a/clang/lib/CodeGen/CGExprScalar.cpp +++ b/clang/lib/CodeGen/CGExprScalar.cpp @@ -685,9 +685,7 @@ ScalarExprEmitter::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) { CGF.EmitVLASize(TypeToSize); } - llvm::Value *VLASize = CGF.GetVLASize(VAT); - return Builder.CreateIntCast(VLASize, ConvertType(E->getType()), - false, "conv"); + return CGF.GetVLASize(VAT); } } diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index 58559e524964..2b365c9ccab5 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -480,18 +480,19 @@ llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) llvm::Value *ElemSize; QualType ElemTy = VAT->getElementType(); - + + const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); + if (ElemTy->isVariableArrayType()) ElemSize = EmitVLASize(ElemTy); else { - // FIXME: We use Int32Ty here because the alloca instruction takes a - // 32-bit integer. What should we do about overflow? - ElemSize = llvm::ConstantInt::get(llvm::Type::Int32Ty, + ElemSize = llvm::ConstantInt::get(SizeTy, getContext().getTypeSize(ElemTy) / 8); } llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr()); - + NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp"); + SizeEntry = Builder.CreateMul(ElemSize, NumElements); }