[cfe-commits] r134972 - in /cfe/trunk: lib/CodeGen/CGExpr.cpp lib/CodeGen/CodeGenTypes.cpp test/CodeGenCXX/init-incomplete-type.cpp

Chris Lattner sabre at nondot.org
Mon Jul 11 23:52:18 PDT 2011


Author: lattner
Date: Tue Jul 12 01:52:18 2011
New Revision: 134972

URL: http://llvm.org/viewvc/llvm-project?rev=134972&view=rev
Log:
Fix a problem Eli ran into where we now reject incomplete arrays of 
uncompleted struct types.  We now do what llvm-gcc does and compile
them into [i8 x 0].  If the type is later completed, we make sure that
it is appropriately cast.

We compile the terrible example to something like this now:

%struct.A = type { i32, i32, i32 }

@g = external global [0 x i8]

define void @_Z1fv() nounwind {
entry:
  call void @_Z3fooP1A(%struct.A* bitcast ([0 x i8]* @g to %struct.A*))
  ret void
}

declare void @_Z3fooP1A(%struct.A*)

define %struct.A* @_Z2f2v() nounwind {
entry:
  ret %struct.A* getelementptr inbounds ([0 x %struct.A]* bitcast ([0 x i8]* @g to [0 x %struct.A]*), i32 0, i64 1)
}




Modified:
    cfe/trunk/lib/CodeGen/CGExpr.cpp
    cfe/trunk/lib/CodeGen/CodeGenTypes.cpp
    cfe/trunk/test/CodeGenCXX/init-incomplete-type.cpp

Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=134972&r1=134971&r2=134972&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Tue Jul 12 01:52:18 2011
@@ -1274,6 +1274,14 @@
   }
 }
 
+static llvm::Value *
+EmitBitCastOfLValueToProperType(llvm::IRBuilder<> &Builder, 
+                                llvm::Value *V, llvm::Type *IRType,
+                                llvm::StringRef Name = llvm::StringRef()) {
+  unsigned AS = cast<llvm::PointerType>(V->getType())->getAddressSpace();
+  return Builder.CreateBitCast(V, IRType->getPointerTo(AS), Name);
+}
+
 static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
                                       const Expr *E, const VarDecl *VD) {
   assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
@@ -1282,8 +1290,11 @@
   llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
   if (VD->getType()->isReferenceType())
     V = CGF.Builder.CreateLoad(V, "tmp");
-  unsigned Alignment = CGF.getContext().getDeclAlign(VD).getQuantity();
   
+  V = EmitBitCastOfLValueToProperType(CGF.Builder, V,
+                                CGF.getTypes().ConvertTypeForMem(E->getType()));
+
+  unsigned Alignment = CGF.getContext().getDeclAlign(VD).getQuantity();
   LValue LV = CGF.MakeAddrLValue(V, E->getType(), Alignment);
   setObjCGCLValueClass(CGF.getContext(), E, LV);
   return LV;
@@ -1339,6 +1350,9 @@
     if (VD->getType()->isReferenceType())
       V = Builder.CreateLoad(V, "tmp");
 
+    V = EmitBitCastOfLValueToProperType(Builder, V,
+                                    getTypes().ConvertTypeForMem(E->getType()));
+
     LValue LV = MakeAddrLValue(V, E->getType(), Alignment);
     if (NonGCable) {
       LV.getQuals().removeObjCGCAttr();
@@ -1836,11 +1850,9 @@
   // for both unions and structs.  A union needs a bitcast, a struct element
   // will need a bitcast if the LLVM type laid out doesn't match the desired
   // type.
-  const llvm::Type *llvmType = CGM.getTypes().ConvertTypeForMem(type);
-  unsigned AS = cast<llvm::PointerType>(baseAddr->getType())->getAddressSpace();
-  addr = Builder.CreateBitCast(addr, llvmType->getPointerTo(AS),
-                               field->getName());
-
+  addr = EmitBitCastOfLValueToProperType(Builder, addr,
+                                         CGM.getTypes().ConvertTypeForMem(type),
+                                         field->getName());
 
   unsigned alignment = getContext().getDeclAlign(field).getQuantity();
   LValue LV = MakeAddrLValue(addr, type, alignment);

Modified: cfe/trunk/lib/CodeGen/CodeGenTypes.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenTypes.cpp?rev=134972&r1=134971&r2=134972&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenTypes.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenTypes.cpp Tue Jul 12 01:52:18 2011
@@ -92,7 +92,6 @@
   // Otherwise, return an integer of the target-specified size.
   return llvm::IntegerType::get(getLLVMContext(),
                                 (unsigned)Context.getTypeSize(T));
-
 }
 
 /// isFuncTypeArgumentConvertible - Return true if the specified type in a 
@@ -318,8 +317,14 @@
     const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
     assert(A->getIndexTypeCVRQualifiers() == 0 &&
            "FIXME: We only handle trivial array types so far!");
-    // int X[] -> [0 x int]
-    ResultType = llvm::ArrayType::get(ConvertTypeForMem(A->getElementType()),0);
+    // int X[] -> [0 x int], unless the element type is not sized.  If it is
+    // unsized (e.g. an incomplete struct) just use [0 x i8].
+    ResultType = ConvertTypeForMem(A->getElementType());
+    if (!ResultType->isSized()) {
+      SkippedLayout = true;
+      ResultType = llvm::Type::getInt8Ty(getLLVMContext());
+    }
+    ResultType = llvm::ArrayType::get(ResultType, 0);
     break;
   }
   case Type::ConstantArray: {

Modified: cfe/trunk/test/CodeGenCXX/init-incomplete-type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/init-incomplete-type.cpp?rev=134972&r1=134971&r2=134972&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/init-incomplete-type.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/init-incomplete-type.cpp Tue Jul 12 01:52:18 2011
@@ -10,3 +10,22 @@
   { 0 }
 };
 
+
+
+namespace incomplete_type_refs {
+  struct A;
+  extern A g[];
+  void foo(A*);
+  void f(void) {
+    foo(g);    // Reference to array with unknown element type.
+  }
+
+  struct A {   // define the element type.
+    int a,b,c;
+  };
+
+  A *f2() {
+    return &g[1];
+  }
+
+}
\ No newline at end of file





More information about the cfe-commits mailing list