[cfe-commits] r61284 - in /cfe/trunk/lib/CodeGen: CGDecl.cpp CodeGenFunction.cpp CodeGenFunction.h

Anders Carlsson andersca at mac.com
Sat Dec 20 12:27:18 PST 2008


Author: andersca
Date: Sat Dec 20 14:27:15 2008
New Revision: 61284

URL: http://llvm.org/viewvc/llvm-project?rev=61284&view=rev
Log:
Split up emitting of VLA sizes and getting the size of a VLA.

Modified:
    cfe/trunk/lib/CodeGen/CGDecl.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h

Modified: cfe/trunk/lib/CodeGen/CGDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDecl.cpp?rev=61284&r1=61283&r2=61284&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGDecl.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDecl.cpp Sat Dec 20 14:27:15 2008
@@ -180,7 +180,7 @@
     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");

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.cpp?rev=61284&r1=61283&r2=61284&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.cpp Sat Dec 20 14:27:15 2008
@@ -401,30 +401,38 @@
   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();
+  assert(SizeEntry && "Did not emit size for type");
+  return SizeEntry;
+}
 
-    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);
-    }
+llvm::Value *CodeGenFunction::EmitVLASize(const VariableArrayType *VAT)
+{
+  llvm::Value *&SizeEntry = VLASizeMap[VAT];
 
-    llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
+  assert(!SizeEntry && "Must not emit the same VLA size more than once!");
+  // Get the element size;
+  llvm::Value *ElemSize;
+  
+  QualType ElemTy = VAT->getElementType();
 
-    SizeEntry = Builder.CreateMul(ElemSize, NumElements);
+  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;
 }

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=61284&r1=61283&r2=61284&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Sat Dec 20 14:27:15 2008
@@ -349,7 +349,11 @@
   // 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 *);





More information about the cfe-commits mailing list