[cfe-commits] r43355 - in /cfe/trunk/CodeGen: CodeGenTypes.cpp CodeGenTypes.h

Devang Patel dpatel at apple.com
Thu Oct 25 11:32:37 PDT 2007


Author: dpatel
Date: Thu Oct 25 13:32:36 2007
New Revision: 43355

URL: http://llvm.org/viewvc/llvm-project?rev=43355&view=rev
Log:
Cache llvm::Type through PATypeHolder.

Modified:
    cfe/trunk/CodeGen/CodeGenTypes.cpp
    cfe/trunk/CodeGen/CodeGenTypes.h

Modified: cfe/trunk/CodeGen/CodeGenTypes.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenTypes.cpp?rev=43355&r1=43354&r2=43355&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CodeGenTypes.cpp (original)
+++ cfe/trunk/CodeGen/CodeGenTypes.cpp Thu Oct 25 13:32:36 2007
@@ -69,7 +69,21 @@
 
 /// ConvertType - Convert the specified type to its LLVM form.
 const llvm::Type *CodeGenTypes::ConvertType(QualType T) {
-  // FIXME: Cache these, move the CodeGenModule, expand, etc.
+  // See if type is already cached.
+  llvm::DenseMap<Type *, llvm::PATypeHolder *>::iterator
+    I = TypeHolderMap.find(T.getTypePtr());
+  if (I != TypeHolderMap.end()) {
+    llvm::PATypeHolder *PAT = I->second;
+    return PAT->get();
+  }
+
+  const llvm::Type *ResultType = ConvertNewType(T);
+  llvm::PATypeHolder *PAT = new llvm::PATypeHolder(ResultType);
+  TypeHolderMap[T.getTypePtr()] = PAT;
+  return ResultType;
+}
+
+const llvm::Type *CodeGenTypes::ConvertNewType(QualType T) {
   const clang::Type &Ty = *T.getCanonicalType();
   
   switch (Ty.getTypeClass()) {
@@ -166,7 +180,10 @@
     
     // Struct return passes the struct byref.
     if (!ResultType->isFirstClassType() && ResultType != llvm::Type::VoidTy) {
-      ArgTys.push_back(llvm::PointerType::get(ResultType));
+      const llvm::Type *RType = llvm::PointerType::get(ResultType);
+      QualType RTy = Context.getPointerType(FP.getResultType());
+      TypeHolderMap[RTy.getTypePtr()] = new llvm::PATypeHolder(RType);
+      ArgTys.push_back(RType);
       ResultType = llvm::Type::VoidTy;
     }
     
@@ -215,6 +232,8 @@
       // Reevaluate this when performance analyis finds tons of opaque types.
       llvm::OpaqueType *OpaqueTy =  llvm::OpaqueType::get();
       RecordTypesToResolve[RD] = OpaqueTy;
+      QualType Opq;
+      TypeHolderMap[Opq.getTypePtr()] = new llvm::PATypeHolder(OpaqueTy);
 
       // Layout fields.
       RecordOrganizer RO;
@@ -281,8 +300,12 @@
     const llvm::Type *Ty = ConvertType(FTP.getArgType(i));
     if (Ty->isFirstClassType())
       ArgTys.push_back(Ty);
-    else
-      ArgTys.push_back(llvm::PointerType::get(Ty));
+    else {
+      QualType PTy = Context.getPointerType(FTP.getArgType(i));
+      const llvm::Type *PtrTy = llvm::PointerType::get(Ty);
+      TypeHolderMap[PTy.getTypePtr()] = new llvm::PATypeHolder(PtrTy);
+      ArgTys.push_back(PtrTy);
+    }
   }
 }
 

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

==============================================================================
--- cfe/trunk/CodeGen/CodeGenTypes.h (original)
+++ cfe/trunk/CodeGen/CodeGenTypes.h Thu Oct 25 13:32:36 2007
@@ -20,6 +20,7 @@
 namespace llvm {
   class Module;
   class Type;
+  class PATypeHolder;
 }
 
 namespace clang {
@@ -27,6 +28,7 @@
   class TagDecl;
   class TargetInfo;
   class QualType;
+  class Type;
   class FunctionTypeProto;
   class FieldDecl;
   class RecordDecl;
@@ -77,12 +79,18 @@
   /// record.
   llvm::DenseMap<const RecordDecl *, llvm::Type *> RecordTypesToResolve;
 
+  /// TypeHolderMap - This map keeps cache of llvm::Types (through PATypeHolder)
+  /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
+  /// used instead of llvm::Type because it allows us to bypass potential 
+  /// dangling type pointers due to type refinement on llvm side.
+  llvm::DenseMap<Type *, llvm::PATypeHolder *> TypeHolderMap;
 public:
   CodeGenTypes(ASTContext &Ctx, llvm::Module &M);
   ~CodeGenTypes();
   
   TargetInfo &getTarget() const { return Target; }
   
+  const llvm::Type *ConvertNewType(QualType T);
   const llvm::Type *ConvertType(QualType T);
   void DecodeArgumentTypes(const FunctionTypeProto &FTP, 
                            std::vector<const llvm::Type*> &ArgTys);





More information about the cfe-commits mailing list