Home | History | Annotate | Download | only in CodeGenCXX
      1 // RUN: %clang_cc1 -emit-llvm -O1 -o - -triple=i386-pc-win32 %s | FileCheck %s
      2 
      3 struct S { char a; };
      4 struct V { virtual void f(); };
      5 struct A : virtual V {};
      6 struct B : S, virtual V {};
      7 struct T {};
      8 
      9 T* test0() { return dynamic_cast<T*>((B*)0); }
     10 // CHECK-LABEL: define noalias %struct.T* @"\01?test0@@YAPAUT@@XZ"()
     11 // CHECK:   ret %struct.T* null
     12 
     13 T* test1(V* x) { return &dynamic_cast<T&>(*x); }
     14 // CHECK-LABEL: define %struct.T* @"\01?test1@@YAPAUT@@PAUV@@@Z"(%struct.V* %x)
     15 // CHECK:        [[CAST:%.*]] = bitcast %struct.V* %x to i8*
     16 // CHECK-NEXT:   [[CALL:%.*]] = tail call i8* @__RTDynamicCast(i8* [[CAST]], i32 0, i8* bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUV@@@8" to i8*), i8* bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUT@@@8" to i8*), i32 1)
     17 // CHECK-NEXT:   [[RET:%.*]] = bitcast i8* [[CALL]] to %struct.T*
     18 // CHECK-NEXT:   ret %struct.T* [[RET]]
     19 
     20 T* test2(A* x) { return &dynamic_cast<T&>(*x); }
     21 // CHECK-LABEL: define %struct.T* @"\01?test2@@YAPAUT@@PAUA@@@Z"(%struct.A* %x)
     22 // CHECK:        [[CAST:%.*]] = bitcast %struct.A* %x to i8*
     23 // CHECK-NEXT:   [[BITCAST:%.*]] = bitcast %struct.A* %x to i8**
     24 // CHECK-NEXT:   [[VBTBL:%.*]] = load i8** [[BITCAST]], align 4
     25 // CHECK-NEXT:   [[VBOFFP:%.*]] = getelementptr inbounds i8* [[VBTBL]], i32 4
     26 // CHECK-NEXT:   [[VBOFFPCAST:%.*]] = bitcast i8* [[VBOFFP]] to i32*
     27 // CHECK-NEXT:   [[VBOFFS:%.*]] = load i32* [[VBOFFPCAST]], align 4
     28 // CHECK-NEXT:   [[ADJ:%.*]] = getelementptr inbounds i8* [[CAST]], i32 [[VBOFFS]]
     29 // CHECK-NEXT:   [[CALL:%.*]] = tail call i8* @__RTDynamicCast(i8* [[ADJ]], i32 [[VBOFFS]], i8* bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUA@@@8" to i8*), i8* bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUT@@@8" to i8*), i32 1)
     30 // CHECK-NEXT:   [[RET:%.*]] = bitcast i8* [[CALL]] to %struct.T*
     31 // CHECK-NEXT:   ret %struct.T* [[RET]]
     32 
     33 T* test3(B* x) { return &dynamic_cast<T&>(*x); }
     34 // CHECK-LABEL: define %struct.T* @"\01?test3@@YAPAUT@@PAUB@@@Z"(%struct.B* %x)
     35 // CHECK:        [[VOIDP:%.*]] = getelementptr inbounds %struct.B* %x, i32 0, i32 0, i32 0
     36 // CHECK-NEXT:   [[VBPTR:%.*]] = getelementptr inbounds i8* [[VOIDP]], i32 4
     37 // CHECK-NEXT:   [[BITCAST:%.*]] = bitcast i8* [[VBPTR:%.*]] to i8**
     38 // CHECK-NEXT:   [[VBTBL:%.*]] = load i8** [[BITCAST]], align 4
     39 // CHECK-NEXT:   [[VBOFFP:%.*]] = getelementptr inbounds i8* [[VBTBL]], i32 4
     40 // CHECK-NEXT:   [[VBOFFPCAST:%.*]] = bitcast i8* [[VBOFFP]] to i32*
     41 // CHECK-NEXT:   [[VBOFFS:%.*]] = load i32* [[VBOFFPCAST]], align 4
     42 // CHECK-NEXT:   [[DELTA:%.*]] = add nsw i32 [[VBOFFS]], 4
     43 // CHECK-NEXT:   [[ADJ:%.*]] = getelementptr inbounds i8* [[VOIDP]], i32 [[DELTA]]
     44 // CHECK-NEXT:   [[CALL:%.*]] = tail call i8* @__RTDynamicCast(i8* [[ADJ]], i32 [[DELTA]], i8* bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUB@@@8" to i8*), i8* bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUT@@@8" to i8*), i32 1)
     45 // CHECK-NEXT:   [[RET:%.*]] = bitcast i8* [[CALL]] to %struct.T*
     46 // CHECK-NEXT:   ret %struct.T* [[RET]]
     47 
     48 T* test4(V* x) { return dynamic_cast<T*>(x); }
     49 // CHECK-LABEL: define %struct.T* @"\01?test4@@YAPAUT@@PAUV@@@Z"(%struct.V* %x)
     50 // CHECK:        [[CAST:%.*]] = bitcast %struct.V* %x to i8*
     51 // CHECK-NEXT:   [[CALL:%.*]] = tail call i8* @__RTDynamicCast(i8* [[CAST]], i32 0, i8* bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUV@@@8" to i8*), i8* bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUT@@@8" to i8*), i32 0)
     52 // CHECK-NEXT:   [[RET:%.*]] = bitcast i8* [[CALL]] to %struct.T*
     53 // CHECK-NEXT:   ret %struct.T* [[RET]]
     54 
     55 T* test5(A* x) { return dynamic_cast<T*>(x); }
     56 // CHECK-LABEL: define %struct.T* @"\01?test5@@YAPAUT@@PAUA@@@Z"(%struct.A* %x)
     57 // CHECK:        [[CHECK:%.*]] = icmp eq %struct.A* %x, null
     58 // CHECK-NEXT:   br i1 [[CHECK]]
     59 // CHECK:        [[VOIDP:%.*]] = bitcast %struct.A* %x to i8*
     60 // CHECK-NEXT:   [[BITCAST:%.*]] = bitcast %struct.A* %x to i8**
     61 // CHECK-NEXT:   [[VBTBL:%.*]] = load i8** [[BITCAST]], align 4
     62 // CHECK-NEXT:   [[VBOFFP:%.*]] = getelementptr inbounds i8* [[VBTBL]], i32 4
     63 // CHECK-NEXT:   [[VBOFFPCAST:%.*]] = bitcast i8* [[VBOFFP]] to i32*
     64 // CHECK-NEXT:   [[VBOFFS:%.*]] = load i32* [[VBOFFPCAST:%.*]], align 4
     65 // CHECK-NEXT:   [[ADJ:%.*]] = getelementptr inbounds i8* [[VOIDP]], i32 [[VBOFFS]]
     66 // CHECK-NEXT:   [[CALL:%.*]] = tail call i8* @__RTDynamicCast(i8* [[ADJ]], i32 [[VBOFFS]], i8* bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUA@@@8" to i8*), i8* bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUT@@@8" to i8*), i32 0)
     67 // CHECK-NEXT:   [[RES:%.*]] = bitcast i8* [[CALL]] to %struct.T*
     68 // CHECK-NEXT:   br label
     69 // CHECK:        [[RET:%.*]] = phi %struct.T*
     70 // CHECK-NEXT:   ret %struct.T* [[RET]]
     71 
     72 T* test6(B* x) { return dynamic_cast<T*>(x); }
     73 // CHECK-LABEL: define %struct.T* @"\01?test6@@YAPAUT@@PAUB@@@Z"(%struct.B* %x)
     74 // CHECK:        [[CHECK:%.*]] = icmp eq %struct.B* %x, null
     75 // CHECK-NEXT:   br i1 [[CHECK]]
     76 // CHECK:        [[CAST:%.*]] = getelementptr inbounds %struct.B* %x, i32 0, i32 0, i32 0
     77 // CHECK-NEXT:   [[VBPTR:%.*]] = getelementptr inbounds i8* [[CAST]], i32 4
     78 // CHECK-NEXT:   [[BITCAST:%.*]] = bitcast i8* [[VBPTR]] to i8**
     79 // CHECK-NEXT:   [[VBTBL:%.*]] = load i8** [[BITCAST]], align 4
     80 // CHECK-NEXT:   [[VBOFFP:%.*]] = getelementptr inbounds i8* [[VBTBL]], i32 4
     81 // CHECK-NEXT:   [[VBOFFPCAST:%.*]] = bitcast i8* [[VBOFFP]] to i32*
     82 // CHECK-NEXT:   [[VBOFFS:%.*]] = load i32* [[VBOFFPCAST:%.*]], align 4
     83 // CHECK-NEXT:   [[DELTA:%.*]] = add nsw i32 [[VBOFFS]], 4
     84 // CHECK-NEXT:   [[ADJ:%.*]] = getelementptr inbounds i8* [[CAST]], i32 [[DELTA]]
     85 // CHECK-NEXT:   [[CALL:%.*]] = tail call i8* @__RTDynamicCast(i8* [[ADJ]], i32 [[DELTA]], i8* bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUB@@@8" to i8*), i8* bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUT@@@8" to i8*), i32 0)
     86 // CHECK-NEXT:   [[RES:%.*]] = bitcast i8* [[CALL]] to %struct.T*
     87 // CHECK-NEXT:   br label
     88 // CHECK:        [[RET:%.*]] = phi %struct.T*
     89 // CHECK-NEXT:   ret %struct.T* [[RET]]
     90 
     91 void* test7(V* x) { return dynamic_cast<void*>(x); }
     92 // CHECK-LABEL: define i8* @"\01?test7@@YAPAXPAUV@@@Z"(%struct.V* %x)
     93 // CHECK:        [[CAST:%.*]] = bitcast %struct.V* %x to i8*
     94 // CHECK-NEXT:   [[RET:%.*]] = tail call i8* @__RTCastToVoid(i8* [[CAST]])
     95 // CHECK-NEXT:   ret i8* [[RET]]
     96 
     97 void* test8(A* x) { return dynamic_cast<void*>(x); }
     98 // CHECK-LABEL: define i8* @"\01?test8@@YAPAXPAUA@@@Z"(%struct.A* %x)
     99 // CHECK:        [[CHECK:%.*]] = icmp eq %struct.A* %x, null
    100 // CHECK-NEXT:   br i1 [[CHECK]]
    101 // CHECK:        [[VOIDP:%.*]] = bitcast %struct.A* %x to i8*
    102 // CHECK-NEXT:   [[BITCAST:%.*]] = bitcast %struct.A* %x to i8**
    103 // CHECK-NEXT:   [[VBTBL:%.*]] = load i8** [[BITCAST]], align 4
    104 // CHECK-NEXT:   [[VBOFFP:%.*]] = getelementptr inbounds i8* [[VBTBL]], i32 4
    105 // CHECK-NEXT:   [[VBOFFPCAST:%.*]] = bitcast i8* [[VBOFFP]] to i32*
    106 // CHECK-NEXT:   [[VBOFFS:%.*]] = load i32* [[VBOFFPCAST:%.*]], align 4
    107 // CHECK-NEXT:   [[ADJ:%.*]] = getelementptr inbounds i8* [[VOIDP]], i32 [[VBOFFS]]
    108 // CHECK-NEXT:   [[RES:%.*]] = tail call i8* @__RTCastToVoid(i8* [[ADJ]])
    109 // CHECK-NEXT:   br label
    110 // CHECK:        [[RET:%.*]] = phi i8*
    111 // CHECK-NEXT:   ret i8* [[RET]]
    112 
    113 void* test9(B* x) { return dynamic_cast<void*>(x); }
    114 // CHECK-LABEL: define i8* @"\01?test9@@YAPAXPAUB@@@Z"(%struct.B* %x)
    115 // CHECK:        [[CHECK:%.*]] = icmp eq %struct.B* %x, null
    116 // CHECK-NEXT:   br i1 [[CHECK]]
    117 // CHECK:        [[CAST:%.*]] = getelementptr inbounds %struct.B* %x, i32 0, i32 0, i32 0
    118 // CHECK-NEXT:   [[VBPTR:%.*]] = getelementptr inbounds i8* [[CAST]], i32 4
    119 // CHECK-NEXT:   [[BITCAST:%.*]] = bitcast i8* [[VBPTR]] to i8**
    120 // CHECK-NEXT:   [[VBTBL:%.*]] = load i8** [[BITCAST]], align 4
    121 // CHECK-NEXT:   [[VBOFFP:%.*]] = getelementptr inbounds i8* [[VBTBL]], i32 4
    122 // CHECK-NEXT:   [[VBOFFPCAST:%.*]] = bitcast i8* [[VBOFFP]] to i32*
    123 // CHECK-NEXT:   [[VBOFFS:%.*]] = load i32* [[VBOFFPCAST:%.*]], align 4
    124 // CHECK-NEXT:   [[DELTA:%.*]] = add nsw i32 [[VBOFFS]], 4
    125 // CHECK-NEXT:   [[ADJ:%.*]] = getelementptr inbounds i8* [[CAST]], i32 [[DELTA]]
    126 // CHECK-NEXT:   [[CALL:%.*]] = tail call i8* @__RTCastToVoid(i8* [[ADJ]])
    127 // CHECK-NEXT:   br label
    128 // CHECK:        [[RET:%.*]] = phi i8*
    129 // CHECK-NEXT:   ret i8* [[RET]]
    130 
    131