r211223 - [MS-ABI] Implement typeid

Warren Hunt whunt at google.com
Wed Jun 18 14:15:56 PDT 2014


Author: whunt
Date: Wed Jun 18 16:15:55 2014
New Revision: 211223

URL: http://llvm.org/viewvc/llvm-project?rev=211223&view=rev
Log:
[MS-ABI] Implement typeid
This patch enables clang to generate calls to __RTtypeid when lowering 
typeid on win32 targets.  Test cases are included.


Added:
    cfe/trunk/test/CodeGenCXX/microsoft-abi-typeid.cpp
Modified:
    cfe/trunk/lib/CodeGen/CGExprCXX.cpp

Modified: cfe/trunk/lib/CodeGen/CGExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprCXX.cpp?rev=211223&r1=211222&r2=211223&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprCXX.cpp Wed Jun 18 16:15:55 2014
@@ -1642,12 +1642,45 @@ static llvm::Value *getPolymorphicOffset
   llvm_unreachable("One of our vbases should be polymorphic.");
 }
 
-static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF,
-                                         const Expr *E, 
+static llvm::Value *emitRTtypeidCall(CodeGenFunction &CGF,
+                                     llvm::Value *Argument) {
+  llvm::Type *ArgTypes[] = {CGF.Int8PtrTy};
+  llvm::Constant *Function = CGF.CGM.CreateRuntimeFunction(
+      llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false), "__RTtypeid");
+  llvm::Value *Args[] = {Argument};
+  return CGF.EmitRuntimeCall(Function, Args);
+}
+
+static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF, const Expr *E,
                                          llvm::Type *StdTypeInfoPtrTy) {
   // Get the vtable pointer.
   llvm::Value *ThisPtr = CGF.EmitLValue(E).getAddress();
 
+  if (CGF.getTarget().getCXXABI().isMicrosoft()) {
+    llvm::Value *CastPtr = CGF.Builder.CreateBitCast(ThisPtr, CGF.Int8PtrTy);
+    const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl();
+    if (CGF.getContext().getASTRecordLayout(RD).hasExtendableVFPtr())
+      return CGF.Builder.CreateBitCast(emitRTtypeidCall(CGF, CastPtr),
+                                       StdTypeInfoPtrTy);
+    llvm::BasicBlock *EntryBlock = CGF.Builder.GetInsertBlock();
+    llvm::BasicBlock *AdjustBlock = CGF.createBasicBlock("type_id.valid");
+    llvm::BasicBlock *ExitBlock = CGF.createBasicBlock("type_id.call");
+    CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNull(CastPtr), ExitBlock,
+                             AdjustBlock);
+    // Emit the call block and code for it.
+    CGF.EmitBlock(AdjustBlock);
+    llvm::Value *AdjustedThisPtr = CGF.Builder.CreateInBoundsGEP(
+        CastPtr, getPolymorphicOffset(CGF, RD, CastPtr));
+    // Emit the call block and the phi nodes for it.
+    CGF.EmitBlock(ExitBlock);
+    llvm::PHINode *ValuePHI = CGF.Builder.CreatePHI(CGF.Int8PtrTy, 2);
+    ValuePHI->addIncoming(AdjustedThisPtr, AdjustBlock);
+    ValuePHI->addIncoming(llvm::Constant::getNullValue(CGF.Int8PtrTy),
+                          EntryBlock);
+    return CGF.Builder.CreateBitCast(emitRTtypeidCall(CGF, ValuePHI),
+                                     StdTypeInfoPtrTy);
+  }
+
   // C++ [expr.typeid]p2:
   //   If the glvalue expression is obtained by applying the unary * operator to
   //   a pointer and the pointer is a null pointer value, the typeid expression

Added: cfe/trunk/test/CodeGenCXX/microsoft-abi-typeid.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/microsoft-abi-typeid.cpp?rev=211223&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/microsoft-abi-typeid.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/microsoft-abi-typeid.cpp Wed Jun 18 16:15:55 2014
@@ -0,0 +1,58 @@
+// RUN: %clang_cc1 -emit-llvm -O2 -optzns -o - -triple=i386-pc-win32 2>/dev/null %s | FileCheck %s
+// REQUIRES: asserts
+
+struct type_info { const char* raw_name() const; };
+namespace std { using ::type_info; }
+
+struct V { virtual void f() {}; };
+struct A : virtual V {};
+
+A a;
+int b;
+A* fn();
+
+const std::type_info* test0_typeid() { return &typeid(int); }
+// CHECK: define %struct.type_info* @"\01?test0_typeid@@YAPBUtype_info@@XZ"() #0 {
+// CHECK-NEXT: entry:
+// CHECK-NEXT:   ret %struct.type_info* bitcast (%"MSRTTITypeDescriptor\02"* @"\01??_R0H at 8" to %struct.type_info*)
+// CHECK-NEXT: }
+
+const std::type_info* test1_typeid() { return &typeid(A); }
+// CHECK: define %struct.type_info* @"\01?test1_typeid@@YAPBUtype_info@@XZ"() #0 {
+// CHECK-NEXT: entry:
+// CHECK-NEXT:   ret %struct.type_info* bitcast (%"MSRTTITypeDescriptor\07"* @"\01??_R0?AUA@@@8" to %struct.type_info*)
+// CHECK-NEXT: }
+
+const std::type_info* test2_typeid() { return &typeid(&a); }
+// CHECK: define %struct.type_info* @"\01?test2_typeid@@YAPBUtype_info@@XZ"() #0 {
+// CHECK-NEXT: entry:
+// CHECK-NEXT:   ret %struct.type_info* bitcast (%"MSRTTITypeDescriptor\07"* @"\01??_R0PAUA@@@8" to %struct.type_info*)
+// CHECK-NEXT: }
+
+const std::type_info* test3_typeid() { return &typeid(*fn()); }
+// CHECK: define %struct.type_info* @"\01?test3_typeid@@YAPBUtype_info@@XZ"() #1 {
+// CHECK-NEXT: entry:
+// CHECK-NEXT:   %call = tail call %struct.A* @"\01?fn@@YAPAUA@@XZ"() #3
+// CHECK-NEXT:   %0 = icmp eq %struct.A* %call, null
+// CHECK-NEXT:   br i1 %0, label %type_id.call, label %type_id.valid
+// CHECK: type_id.valid:                                    ; preds = %entry
+// CHECK-NEXT:   %1 = bitcast %struct.A* %call to i8*
+// CHECK-NEXT:   %2 = bitcast %struct.A* %call to i8**
+// CHECK-NEXT:   %vbtable = load i8** %2, align 4
+// CHECK-NEXT:   %3 = getelementptr inbounds i8* %vbtable, i32 4
+// CHECK-NEXT:   %4 = bitcast i8* %3 to i32*
+// CHECK-NEXT:   %vbase_offs = load i32* %4, align 4
+// CHECK-NEXT:   %5 = getelementptr inbounds i8* %1, i32 %vbase_offs
+// CHECK-NEXT:   br label %type_id.call
+// CHECK: type_id.call:                                     ; preds = %type_id.valid, %entry
+// CHECK-NEXT:   %6 = phi i8* [ %5, %type_id.valid ], [ null, %entry ]
+// CHECK-NEXT:   %7 = tail call i8* @__RTtypeid(i8* %6) #3
+// CHECK-NEXT:   %8 = bitcast i8* %7 to %struct.type_info*
+// CHECK-NEXT:   ret %struct.type_info* %8
+// CHECK-NEXT: }
+
+const std::type_info* test4_typeid() { return &typeid(b); }
+// CHECK: define %struct.type_info* @"\01?test4_typeid@@YAPBUtype_info@@XZ"() #0 {
+// CHECK-NEXT: entry:
+// CHECK-NEXT:   ret %struct.type_info* bitcast (%"MSRTTITypeDescriptor\02"* @"\01??_R0H at 8" to %struct.type_info*)
+// CHECK-NEXT: }





More information about the cfe-commits mailing list