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

Daniel Dunbar daniel at zuster.org
Tue Sep 16 17:51:38 PDT 2008


Author: ddunbar
Date: Tue Sep 16 19:51:38 2008
New Revision: 56269

URL: http://llvm.org/viewvc/llvm-project?rev=56269&view=rev
Log:
Add support for ABIArgInfo::Expand
 - No functionality change.

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

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGCall.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGCall.cpp Tue Sep 16 19:51:38 2008
@@ -18,6 +18,7 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/DeclObjC.h"
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/ParameterAttributes.h"
 using namespace clang;
 using namespace CodeGen;
@@ -102,8 +103,8 @@
     Default,
     StructRet, /// Only valid for aggregate return types.
 
-    Coerce,    /// Only valid for aggregate types, the argument should
-               /// be accessed by coercion to a provided type.
+    Coerce,    /// Only valid for aggregate return types, the argument
+               /// should be accessed by coercion to a provided type.
 
     ByVal,     /// Only valid for aggregate argument types. The
                /// structure should be passed "byval" with the
@@ -112,7 +113,10 @@
 
     Expand,    /// Only valid for aggregate argument types. The
                /// structure should be expanded into consecutive
-               /// arguments for its constituent fields.
+               /// arguments for its constituent fields. Currently
+               /// expand is only allowed on structures whose fields
+               /// are all scalar types or are themselves expandable
+               /// types.
 
     KindFirst=Default, KindLast=Expand
   };
@@ -140,12 +144,16 @@
   static ABIArgInfo getByVal(unsigned Alignment) {
     return ABIArgInfo(ByVal, 0, Alignment);
   }
+  static ABIArgInfo getExpand() {
+    return ABIArgInfo(Expand);
+  }
 
   Kind getKind() const { return TheKind; }
   bool isDefault() const { return TheKind == Default; }
   bool isStructRet() const { return TheKind == StructRet; }
   bool isCoerce() const { return TheKind == Coerce; }
   bool isByVal() const { return TheKind == ByVal; }
+  bool isExpand() const { return TheKind == Expand; }
 
   // Coerce accessors
   const llvm::Type *getCoerceToType() const {
@@ -198,6 +206,86 @@
 
 /***/
 
+void CodeGenTypes::GetExpandedTypes(QualType Ty, 
+                                    std::vector<const llvm::Type*> &ArgTys) {
+  const RecordType *RT = Ty->getAsStructureType();
+  assert(RT && "Can only expand structure types.");
+  const RecordDecl *RD = RT->getDecl();
+  assert(!RD->hasFlexibleArrayMember() && 
+         "Cannot expand structure with flexible array.");
+  
+  for (RecordDecl::field_const_iterator i = RD->field_begin(), 
+         e = RD->field_end(); i != e; ++i) {
+    const FieldDecl *FD = *i;
+    assert(!FD->isBitField() && 
+           "Cannot expand structure with bit-field members.");
+    
+    QualType FT = FD->getType();
+    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
+      GetExpandedTypes(FT, ArgTys);
+    } else {
+      ArgTys.push_back(ConvertType(FT));
+    }
+  }
+}
+
+llvm::Function::arg_iterator 
+CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
+                                    llvm::Function::arg_iterator AI) {
+  const RecordType *RT = Ty->getAsStructureType();
+  assert(RT && "Can only expand structure types.");
+
+  RecordDecl *RD = RT->getDecl();
+  assert(LV.isSimple() && 
+         "Unexpected non-simple lvalue during struct expansion.");  
+  llvm::Value *Addr = LV.getAddress();
+  for (RecordDecl::field_iterator i = RD->field_begin(), 
+         e = RD->field_end(); i != e; ++i) {
+    FieldDecl *FD = *i;    
+    QualType FT = FD->getType();
+
+    // FIXME: What are the right qualifiers here?
+    LValue LV = EmitLValueForField(Addr, FD, false, 0);
+    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
+      AI = ExpandTypeFromArgs(FT, LV, AI);
+    } else {
+      EmitStoreThroughLValue(RValue::get(AI), LV, FT);
+      ++AI;
+    }
+  }
+
+  return AI;
+}
+
+void 
+CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV, 
+                                  llvm::SmallVector<llvm::Value*, 16> &Args) {
+  const RecordType *RT = Ty->getAsStructureType();
+  assert(RT && "Can only expand structure types.");
+
+  RecordDecl *RD = RT->getDecl();
+  assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
+  llvm::Value *Addr = RV.getAggregateAddr();
+  for (RecordDecl::field_iterator i = RD->field_begin(), 
+         e = RD->field_end(); i != e; ++i) {
+    FieldDecl *FD = *i;    
+    QualType FT = FD->getType();
+    
+    // FIXME: What are the right qualifiers here?
+    LValue LV = EmitLValueForField(Addr, FD, false, 0);
+    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
+      ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
+    } else {
+      RValue RV = EmitLoadOfLValue(LV, FT);
+      assert(RV.isScalar() && 
+             "Unexpected non-scalar rvalue during struct expansion.");
+      Args.push_back(RV.getScalarVal());
+    }
+  }
+}
+
+/***/
+
 const llvm::FunctionType *
 CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) {
   return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic);
@@ -247,6 +335,7 @@
     const llvm::Type *Ty = ConvertType(*begin);
     
     switch (AI.getKind()) {
+    case ABIArgInfo::Coerce:
     case ABIArgInfo::StructRet:
       assert(0 && "Invalid ABI kind for non-return argument");
     
@@ -259,13 +348,9 @@
     case ABIArgInfo::Default:
       ArgTys.push_back(Ty);
       break;
-
-    case ABIArgInfo::Coerce:
-      assert(0 && "FIXME: ABIArgInfo::Coerce unhandled for arguments");
-      break;
      
     case ABIArgInfo::Expand:
-      assert(0 && "FIXME: ABIArgInfo::Expand unhandled for arguments");
+      GetExpandedTypes(*begin, ArgTys);
       break;
     }
   }
@@ -320,13 +405,14 @@
 
   if (FuncAttrs)
     PAL.push_back(llvm::ParamAttrsWithIndex::get(0, FuncAttrs));
-  for (++begin; begin != end; ++begin, ++Index) {
+  for (++begin; begin != end; ++begin) {
     QualType ParamType = *begin;
     unsigned ParamAttrs = 0;
     ABIArgInfo AI = classifyArgumentType(ParamType, getContext(), getTypes());
     
     switch (AI.getKind()) {
     case ABIArgInfo::StructRet:
+    case ABIArgInfo::Coerce:
       assert(0 && "Invalid ABI kind for non-return argument");
     
     case ABIArgInfo::ByVal:
@@ -343,18 +429,21 @@
         }
       }
       break;
-
-    case ABIArgInfo::Coerce:
-      assert(0 && "FIXME: ABIArgInfo::Coerce unhandled for arguments");
-      break;
      
-    case ABIArgInfo::Expand:
-      assert(0 && "FIXME: ABIArgInfo::Expand unhandled for arguments");
-      break;
+    case ABIArgInfo::Expand: {
+      std::vector<const llvm::Type*> Tys;  
+      // FIXME: This is rather inefficient. Do we ever actually need
+      // to do anything here? The result should be just reconstructed
+      // on the other side, so extension should be a non-issue.
+      getTypes().GetExpandedTypes(ParamType, Tys);
+      Index += Tys.size();
+      continue;
+    }
     }
       
     if (ParamAttrs)
       PAL.push_back(llvm::ParamAttrsWithIndex::get(Index, ParamAttrs));
+    ++Index;
   }
 }
 
@@ -371,36 +460,50 @@
   }
      
   for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
-       i != e; ++i, ++AI) {
+       i != e; ++i) {
     const VarDecl *Arg = i->first;
-    QualType T = i->second;
-    ABIArgInfo ArgI = classifyArgumentType(T, getContext(), CGM.getTypes());
+    QualType Ty = i->second;
+    ABIArgInfo ArgI = classifyArgumentType(Ty, getContext(), CGM.getTypes());
 
     switch (ArgI.getKind()) {
     case ABIArgInfo::ByVal: 
     case ABIArgInfo::Default: {
       assert(AI != Fn->arg_end() && "Argument mismatch!");
       llvm::Value* V = AI;
-      if (!getContext().typesAreCompatible(T, Arg->getType())) {
+      if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
         // This must be a promotion, for something like
         // "void a(x) short x; {..."
-        V = EmitScalarConversion(V, T, Arg->getType());
+        V = EmitScalarConversion(V, Ty, Arg->getType());
       }
       EmitParmDecl(*Arg, V);
       break;
     }
-
-    case ABIArgInfo::Coerce:
-      assert(0 && "FIXME: ABIArgInfo::Coerce unhandled for arguments");
-      break;
       
-    case ABIArgInfo::Expand:
-      assert(0 && "FIXME: ABIArgInfo::Expand unhandled for arguments");
-      break;
+    case ABIArgInfo::Expand: {
+      // If this was structure was expand into multiple arguments then
+      // we need to create a temporary and reconstruct it from the
+      // arguments.
+      std::string Name(Arg->getName());
+      llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty), 
+                                           (Name + ".addr").c_str());
+      // FIXME: What are the right qualifiers here?
+      llvm::Function::arg_iterator End = 
+        ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);      
+      EmitParmDecl(*Arg, Temp);
+
+      // Name the arguments used in expansion and increment AI.
+      unsigned Index = 0;
+      for (; AI != End; ++AI, ++Index)
+        AI->setName(Name + "." + llvm::utostr(Index));
+      continue;
+    }
       
+    case ABIArgInfo::Coerce:
     case ABIArgInfo::StructRet:
       assert(0 && "Invalid ABI kind for non-return argument");        
     }
+
+    ++AI;
   }
   assert(AI == Fn->arg_end() && "Argument mismatch!");
 }
@@ -445,9 +548,7 @@
 RValue CodeGenFunction::EmitCall(llvm::Value *Callee, 
                                  QualType RetTy, 
                                  const CallArgList &CallArgs) {
-  // FIXME: Factor out code to load from args into locals into target.
   llvm::SmallVector<llvm::Value*, 16> Args;
-  llvm::Value *TempArg0 = 0;
 
   // Handle struct-return functions by passing a pointer to the
   // location that we would like to return into.
@@ -455,8 +556,7 @@
   switch (RetAI.getKind()) {
   case ABIArgInfo::StructRet:
     // Create a temporary alloca to hold the result of the call. :(
-    TempArg0 = CreateTempAlloca(ConvertType(RetTy));
-    Args.push_back(TempArg0);
+    Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
     break;
     
   case ABIArgInfo::Default:
@@ -470,15 +570,32 @@
   
   for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); 
        I != E; ++I) {
+    ABIArgInfo ArgInfo = classifyArgumentType(I->second, getContext(), 
+                                              CGM.getTypes());
     RValue RV = I->first;
-    if (RV.isScalar()) {
-      Args.push_back(RV.getScalarVal());
-    } else if (RV.isComplex()) {
-      // Make a temporary alloca to pass the argument.
-      Args.push_back(CreateTempAlloca(ConvertType(I->second)));
-      StoreComplexToAddr(RV.getComplexVal(), Args.back(), false); 
-    } else {
-      Args.push_back(RV.getAggregateAddr());
+
+    switch (ArgInfo.getKind()) {
+    case ABIArgInfo::ByVal: // Default is byval
+    case ABIArgInfo::Default:      
+      if (RV.isScalar()) {
+        Args.push_back(RV.getScalarVal());
+      } else if (RV.isComplex()) {
+        // Make a temporary alloca to pass the argument.
+        Args.push_back(CreateTempAlloca(ConvertType(I->second)));
+        StoreComplexToAddr(RV.getComplexVal(), Args.back(), false); 
+      } else {
+        Args.push_back(RV.getAggregateAddr());
+      }
+      break;
+     
+    case ABIArgInfo::StructRet:
+    case ABIArgInfo::Coerce:
+      assert(0 && "Invalid ABI kind for non-return argument");
+      break;
+
+    case ABIArgInfo::Expand:
+      ExpandTypeToArgs(I->second, RV, Args);
+      break;
     }
   }
   
@@ -501,10 +618,10 @@
   switch (RetAI.getKind()) {
   case ABIArgInfo::StructRet:
     if (RetTy->isAnyComplexType())
-      return RValue::getComplex(LoadComplexFromAddr(TempArg0, false));
+      return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
     else 
       // Struct return.
-      return RValue::getAggregate(TempArg0);
+      return RValue::getAggregate(Args[0]);
 
   case ABIArgInfo::Default:
     return RValue::get(RetTy->isVoidType() ? 0 : CI);

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGDecl.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDecl.cpp Tue Sep 16 19:51:38 2008
@@ -219,8 +219,9 @@
     const llvm::Type *LTy = ConvertType(Ty);
     if (LTy->isSingleValueType()) {
       // TODO: Alignment
-      DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr",
-                                     AllocaInsertPt);
+      std::string Name(D.getName());
+      Name += ".addr";
+      DeclPtr = CreateTempAlloca(LTy, Name.c_str());
       
       // Store the initial value into the alloca.
       Builder.CreateStore(Arg, DeclPtr,Ty.isVolatileQualified());

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Tue Sep 16 19:51:38 2008
@@ -441,6 +441,22 @@
   /// EmitIndirectSwitches - Emit code for all of the switch
   /// instructions in IndirectSwitches.
   void EmitIndirectSwitches();
+
+  /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty
+  /// from function arguments into \arg Dst. See ABIArgInfo::Expand.
+  ///
+  /// \param AI - The first function argument of the expansion.
+  /// \return The argument following the last expanded function
+  /// argument.
+  llvm::Function::arg_iterator 
+  ExpandTypeFromArgs(QualType Ty, LValue Dst,
+                     llvm::Function::arg_iterator AI);
+
+  /// ExpandTypeToArgs - Expand an RValue \arg Src, with the LLVM type
+  /// for \arg Ty, into individual arguments on the provided vector
+  /// \arg Args. See ABIArgInfo::Expand.
+  void ExpandTypeToArgs(QualType Ty, RValue Src, 
+                        llvm::SmallVector<llvm::Value*, 16> &Args);
 };
 }  // end namespace CodeGen
 }  // end namespace clang

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenTypes.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenTypes.h Tue Sep 16 19:51:38 2008
@@ -187,6 +187,11 @@
   /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
   /// enum.
   const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
+
+  /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
+  /// argument types it would be passed as on the provided vector \arg
+  /// ArgTys. See ABIArgInfo::Expand.
+  void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys);
 };
 
 }  // end namespace CodeGen





More information about the cfe-commits mailing list