Abstract out member-pointer creation.  I'm really unhappy about the current
duplication between the constant and non-constant paths in all of this.

Implement ARM ABI semantics for member pointer constants and conversion.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@111772 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGCXX.cpp b/lib/CodeGen/CGCXX.cpp
index f56ea42..2934657 100644
--- a/lib/CodeGen/CGCXX.cpp
+++ b/lib/CodeGen/CGCXX.cpp
@@ -287,44 +287,6 @@
   return cast<llvm::Function>(GetOrCreateLLVMFunction(Name, FTy, GD));
 }
 
-llvm::Constant *
-CodeGenModule::GetCXXMemberFunctionPointerValue(const CXXMethodDecl *MD) {
-  assert(MD->isInstance() && "Member function must not be static!");
-    
-  MD = MD->getCanonicalDecl();
-
-  const llvm::Type *PtrDiffTy = Types.ConvertType(Context.getPointerDiffType());
-
-  // Get the function pointer (or index if this is a virtual function).
-  if (MD->isVirtual()) {
-    uint64_t Index = VTables.getMethodVTableIndex(MD);
-
-    // FIXME: We shouldn't use / 8 here.
-    uint64_t PointerWidthInBytes = Context.Target.getPointerWidth(0) / 8;
-
-    // Itanium C++ ABI 2.3:
-    //   For a non-virtual function, this field is a simple function pointer. 
-    //   For a virtual function, it is 1 plus the virtual table offset 
-    //   (in bytes) of the function, represented as a ptrdiff_t. 
-    return llvm::ConstantInt::get(PtrDiffTy, (Index * PointerWidthInBytes) + 1);
-  }
-
-  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
-  const llvm::Type *Ty;
-  // Check whether the function has a computable LLVM signature.
-  if (!CodeGenTypes::VerifyFuncTypeComplete(FPT)) {
-    // The function has a computable LLVM signature; use the correct type.
-    Ty = Types.GetFunctionType(Types.getFunctionInfo(MD), FPT->isVariadic());
-  } else {
-    // Use an arbitrary non-function type to tell GetAddrOfFunction that the
-    // function type is incomplete.
-    Ty = PtrDiffTy;
-  }
-
-  llvm::Constant *FuncPtr = GetAddrOfFunction(MD, Ty);
-  return llvm::ConstantExpr::getPtrToInt(FuncPtr, PtrDiffTy);
-}
-
 static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VTableIndex, 
                                      llvm::Value *This, const llvm::Type *Ty) {
   Ty = Ty->getPointerTo()->getPointerTo()->getPointerTo();
@@ -400,6 +362,13 @@
   ErrorUnsupportedABI(CGF, "null member function pointers");
 }
 
+void CGCXXABI::EmitMemberFunctionPointer(CodeGenFunction &CGF,
+                                         const CXXMethodDecl *MD,
+                                         llvm::Value *DestPtr,
+                                         bool VolatileDest) {
+  ErrorUnsupportedABI(CGF, "member function pointers");
+}
+
 llvm::Constant *
 CGCXXABI::EmitMemberFunctionPointerConversion(llvm::Constant *C,
                                               const CastExpr *E) {
@@ -411,6 +380,10 @@
   return 0;
 }
 
+llvm::Constant *CGCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl *MD) {
+  return 0;
+}
+
 bool CGCXXABI::RequiresNonZeroInitializer(QualType T) {
   return false;
 }
diff --git a/lib/CodeGen/CGCXXABI.h b/lib/CodeGen/CGCXXABI.h
index 36f8230..a07b98c 100644
--- a/lib/CodeGen/CGCXXABI.h
+++ b/lib/CodeGen/CGCXXABI.h
@@ -22,6 +22,7 @@
 
 namespace clang {
   class CastExpr;
+  class CXXMethodDecl;
   class CXXRecordDecl;
   class MemberPointerType;
   class QualType;
@@ -70,6 +71,12 @@
 
   virtual llvm::Constant *
   EmitNullMemberFunctionPointer(const MemberPointerType *MPT);
+
+  virtual llvm::Constant *EmitMemberFunctionPointer(const CXXMethodDecl *MD);
+  virtual void EmitMemberFunctionPointer(CodeGenFunction &CGF,
+                                         const CXXMethodDecl *MD,
+                                         llvm::Value *DestPtr,
+                                         bool VolatileDest);
 };
 
 /// Creates an instance of a C++ ABI class.
diff --git a/lib/CodeGen/CGExprAgg.cpp b/lib/CodeGen/CGExprAgg.cpp
index 1f8964d..b75aeb1 100644
--- a/lib/CodeGen/CGExprAgg.cpp
+++ b/lib/CodeGen/CGExprAgg.cpp
@@ -364,9 +364,8 @@
 
 void AggExprEmitter::VisitUnaryAddrOf(const UnaryOperator *E) {
   // We have a member function pointer.
-  const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>();
-  (void) MPT;
-  assert(MPT->getPointeeType()->isFunctionProtoType() &&
+  assert(E->getType()->getAs<MemberPointerType>()
+          ->getPointeeType()->isFunctionProtoType() &&
          "Unexpected member pointer type!");
 
   // The creation of member function pointers has no side effects; if
@@ -375,20 +374,9 @@
     return;
   
   const DeclRefExpr *DRE = cast<DeclRefExpr>(E->getSubExpr());
-  const CXXMethodDecl *MD = 
-    cast<CXXMethodDecl>(DRE->getDecl())->getCanonicalDecl();
+  const CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl());
 
-  const llvm::Type *PtrDiffTy = 
-    CGF.ConvertType(CGF.getContext().getPointerDiffType());
-
-  llvm::Value *DstPtr = Builder.CreateStructGEP(DestPtr, 0, "dst.ptr");
-  llvm::Value *FuncPtr = CGF.CGM.GetCXXMemberFunctionPointerValue(MD);
-  Builder.CreateStore(FuncPtr, DstPtr, VolatileDest);
-
-  llvm::Value *AdjPtr = Builder.CreateStructGEP(DestPtr, 1, "dst.adj");
-  // The adjustment will always be 0.
-  Builder.CreateStore(llvm::ConstantInt::get(PtrDiffTy, 0), AdjPtr,
-                      VolatileDest);
+  CGF.CGM.getCXXABI().EmitMemberFunctionPointer(CGF, MD, DestPtr, VolatileDest);
 }
 
 void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
diff --git a/lib/CodeGen/CGExprConstant.cpp b/lib/CodeGen/CGExprConstant.cpp
index e72e7fd..e0335b6 100644
--- a/lib/CodeGen/CGExprConstant.cpp
+++ b/lib/CodeGen/CGExprConstant.cpp
@@ -455,22 +455,7 @@
   }
     
   llvm::Constant *EmitMemberFunctionPointer(CXXMethodDecl *MD) {
-    assert(MD->isInstance() && "Member function must not be static!");
-    
-    MD = MD->getCanonicalDecl();
-
-    const llvm::Type *PtrDiffTy = 
-      CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
-    
-    llvm::Constant *Values[2];
-
-    Values[0] = CGM.GetCXXMemberFunctionPointerValue(MD);
-    
-    // The adjustment will always be 0.
-    Values[1] = llvm::ConstantInt::get(PtrDiffTy, 0);
-    
-    return llvm::ConstantStruct::get(CGM.getLLVMContext(),
-                                     Values, 2, /*Packed=*/false);
+    return CGM.getCXXABI().EmitMemberFunctionPointer(MD);
   }
 
   llvm::Constant *VisitUnaryAddrOf(UnaryOperator *E) {
diff --git a/lib/CodeGen/CodeGenModule.h b/lib/CodeGen/CodeGenModule.h
index eca449c..426499d 100644
--- a/lib/CodeGen/CodeGenModule.h
+++ b/lib/CodeGen/CodeGenModule.h
@@ -371,10 +371,6 @@
   llvm::GlobalValue *GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
                                             CXXDtorType Type);
 
-  // GetCXXMemberFunctionPointerValue - Given a method declaration, return the
-  // integer used in a member function pointer to refer to that value.
-  llvm::Constant *GetCXXMemberFunctionPointerValue(const CXXMethodDecl *MD);
-
   /// getBuiltinLibFunction - Given a builtin id for a function like
   /// "__builtin_fabsf", return a Function* for "fabsf".
   llvm::Value *getBuiltinLibFunction(const FunctionDecl *FD,
diff --git a/lib/CodeGen/ItaniumCXXABI.cpp b/lib/CodeGen/ItaniumCXXABI.cpp
index b721105..ac39495 100644
--- a/lib/CodeGen/ItaniumCXXABI.cpp
+++ b/lib/CodeGen/ItaniumCXXABI.cpp
@@ -66,6 +66,16 @@
 
   llvm::Constant *EmitNullMemberFunctionPointer(const MemberPointerType *MPT);
 
+  void EmitMemberFunctionPointer(CodeGenFunction &CGF,
+                                 const CXXMethodDecl *MD,
+                                 llvm::Value *Dest,
+                                 bool VolatileDest);
+
+  llvm::Constant *EmitMemberFunctionPointer(const CXXMethodDecl *MD);
+
+private:
+  void GetMemberFunctionPointer(const CXXMethodDecl *MD,
+                                llvm::Constant *(&Array)[2]);
 };
 
 class ARMCXXABI : public ItaniumCXXABI {
@@ -82,6 +92,62 @@
   return new ARMCXXABI(CGM);
 }
 
+void ItaniumCXXABI::GetMemberFunctionPointer(const CXXMethodDecl *MD,
+                                             llvm::Constant *(&MemPtr)[2]) {
+  assert(MD->isInstance() && "Member function must not be static!");
+    
+  MD = MD->getCanonicalDecl();
+
+  CodeGenTypes &Types = CGM.getTypes();
+  const llvm::Type *ptrdiff_t = 
+    Types.ConvertType(CGM.getContext().getPointerDiffType());
+
+  // Get the function pointer (or index if this is a virtual function).
+  if (MD->isVirtual()) {
+    uint64_t Index = CGM.getVTables().getMethodVTableIndex(MD);
+
+    // FIXME: We shouldn't use / 8 here.
+    uint64_t PointerWidthInBytes =
+      CGM.getContext().Target.getPointerWidth(0) / 8;
+    uint64_t VTableOffset = (Index * PointerWidthInBytes);
+
+    if (IsARM) {
+      // ARM C++ ABI 3.2.1:
+      //   This ABI specifies that adj contains twice the this
+      //   adjustment, plus 1 if the member function is virtual. The
+      //   least significant bit of adj then makes exactly the same
+      //   discrimination as the least significant bit of ptr does for
+      //   Itanium.
+      MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset);
+      MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 1);
+    } else {
+      // Itanium C++ ABI 2.3:
+      //   For a virtual function, [the pointer field] is 1 plus the
+      //   virtual table offset (in bytes) of the function,
+      //   represented as a ptrdiff_t.
+      MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1);
+      MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
+    }
+  } else {
+    const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
+    const llvm::Type *Ty;
+    // Check whether the function has a computable LLVM signature.
+    if (!CodeGenTypes::VerifyFuncTypeComplete(FPT)) {
+      // The function has a computable LLVM signature; use the correct type.
+      Ty = Types.GetFunctionType(Types.getFunctionInfo(MD), FPT->isVariadic());
+    } else {
+      // Use an arbitrary non-function type to tell GetAddrOfFunction that the
+      // function type is incomplete.
+      Ty = ptrdiff_t;
+    }
+
+    llvm::Constant *Addr = CGM.GetAddrOfFunction(MD, Ty);
+    MemPtr[0] = llvm::ConstantExpr::getPtrToInt(Addr, ptrdiff_t);
+    MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
+  }
+}
+
+
 /// In the Itanium and ARM ABIs, method pointers have the form:
 ///   struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
 ///
@@ -231,6 +297,13 @@
         CGF.CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
                                              E->path_begin(),
                                              E->path_end())) {
+    // The this-adjustment is left-shifted by 1 on ARM.
+    if (IsARM) {
+      uint64_t Offset = cast<llvm::ConstantInt>(Adj)->getZExtValue();
+      Offset <<= 1;
+      Adj = llvm::ConstantInt::get(Adj->getType(), Offset);
+    }
+
     if (DerivedToBase)
       SrcAdj = Builder.CreateSub(SrcAdj, Adj, "adj");
     else
@@ -265,6 +338,13 @@
   // If there's no offset, we're done.
   if (!Offset) return C;
 
+  // The this-adjustment is left-shifted by 1 on ARM.
+  if (IsARM) {
+    uint64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getZExtValue();
+    OffsetV <<= 1;
+    Offset = llvm::ConstantInt::get(Offset->getType(), OffsetV);
+  }
+
   llvm::ConstantStruct *CS = cast<llvm::ConstantStruct>(C);
 
   llvm::Constant *Values[2] = {
@@ -300,6 +380,32 @@
   return CGM.EmitNullConstant(QualType(MPT, 0));
 }
 
+llvm::Constant *
+ItaniumCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl *MD) {
+  llvm::Constant *Values[2];
+  GetMemberFunctionPointer(MD, Values);
+  
+  return llvm::ConstantStruct::get(CGM.getLLVMContext(),
+                                   Values, 2, /*Packed=*/false);
+}
+
+void ItaniumCXXABI::EmitMemberFunctionPointer(CodeGenFunction &CGF,
+                                              const CXXMethodDecl *MD,
+                                              llvm::Value *DestPtr,
+                                              bool VolatileDest) {
+  llvm::Constant *Values[2];
+  GetMemberFunctionPointer(MD, Values);
+
+  CGBuilderTy &Builder = CGF.Builder;
+  
+  llvm::Value *DstPtr = Builder.CreateStructGEP(DestPtr, 0, "memptr.ptr");
+  Builder.CreateStore(Values[0], DstPtr, VolatileDest);
+
+  llvm::Value *AdjPtr = Builder.CreateStructGEP(DestPtr, 1, "memptr.adj");
+  Builder.CreateStore(Values[1], AdjPtr, VolatileDest);
+}
+
+
 bool ItaniumCXXABI::RequiresNonZeroInitializer(QualType T) {
   return CGM.getTypes().ContainsPointerToDataMember(T);
 }