]> granicus.if.org Git - clang/commitdiff
Split up emitting of VLA sizes and getting the size of a VLA.
authorAnders Carlsson <andersca@mac.com>
Sat, 20 Dec 2008 20:27:15 +0000 (20:27 +0000)
committerAnders Carlsson <andersca@mac.com>
Sat, 20 Dec 2008 20:27:15 +0000 (20:27 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61284 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGDecl.cpp
lib/CodeGen/CodeGenFunction.cpp
lib/CodeGen/CodeGenFunction.h

index 62684bae496360b81dfa6721d4d17cf02567fb9a..7f4e34d0f9a29a18acc41907bc723f2d8037180b 100644 (file)
@@ -180,7 +180,7 @@ void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
     const llvm::Type *LElemPtrTy =
       llvm::PointerType::get(LElemTy, D.getType().getAddressSpace());
 
-    llvm::Value *VLASize = GetVLASize(VAT);
+    llvm::Value *VLASize = EmitVLASize(VAT);
 
     // Allocate memory for the array.
     llvm::Value *VLA = Builder.CreateAlloca(llvm::Type::Int8Ty, VLASize, "vla");
index bcb51a3efc65708fae95e215bd7797a4fa4fe8dd..4a3bb3f6caa6ac7eb8680d5a6823962cb4d7e404 100644 (file)
@@ -401,30 +401,38 @@ llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty)
   return AddrTyped;
 }
 
+
 llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT)
 {
   llvm::Value *&SizeEntry = VLASizeMap[VAT];
-
-  if (!SizeEntry) {
-    // Get the element size;
-    llvm::Value *ElemSize;
   
-    QualType ElemTy = VAT->getElementType();
-
-    if (const VariableArrayType *ElemVAT = 
-        getContext().getAsVariableArrayType(ElemTy))
-      ElemSize = GetVLASize(ElemVAT);
-    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, 
-                                        getContext().getTypeSize(ElemTy) / 8);
-    }
+  assert(SizeEntry && "Did not emit size for type");
+  return SizeEntry;
+}
 
-    llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
+llvm::Value *CodeGenFunction::EmitVLASize(const VariableArrayType *VAT)
+{
+  llvm::Value *&SizeEntry = VLASizeMap[VAT];
 
-    SizeEntry = Builder.CreateMul(ElemSize, NumElements);
+  assert(!SizeEntry && "Must not emit the same VLA size more than once!");
+  // Get the element size;
+  llvm::Value *ElemSize;
+  
+  QualType ElemTy = VAT->getElementType();
+
+  if (const VariableArrayType *ElemVAT = 
+      getContext().getAsVariableArrayType(ElemTy))
+    ElemSize = EmitVLASize(ElemVAT);
+  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, 
+                                      getContext().getTypeSize(ElemTy) / 8);
   }
 
+  llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
+
+  SizeEntry = Builder.CreateMul(ElemSize, NumElements);
+
   return SizeEntry;
 }
index a92bac7f1eb6bf190feaba3ffceb5aa5ef515801..950006881477de508d287de1961d6b0473a3a71f 100644 (file)
@@ -349,7 +349,11 @@ public:
   // FIXME: We should be able to get rid of this method and use the va_arg
   // instruction in LLVM instead once it works well enough.  
   llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty);
-  
+
+  // EmitVLASize - Generate code for the VLA type. Returns an
+  // lLVM value that corresponds to the size in bytes of the 
+  llvm::Value *EmitVLASize(const VariableArrayType *);
+                           
   // GetVLASize - Returns an LLVM value that corresponds to the size in bytes
   // of a variable length array type.
   llvm::Value *GetVLASize(const VariableArrayType *);