xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGenCXX/virtual-base-cast.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -emit-llvm %s -o - -triple i686-pc-linux-gnu | FileCheck %s
2*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -cxx-abi microsoft -emit-llvm %s -o - -triple i686-pc-win32 | FileCheck -check-prefix MSVC %s
3*f4a2713aSLionel Sambuc 
4*f4a2713aSLionel Sambuc struct A { int a; virtual int aa(); };
5*f4a2713aSLionel Sambuc struct B { int b; virtual int bb(); };
6*f4a2713aSLionel Sambuc struct C : virtual A, virtual B { int c; virtual int aa(); virtual int bb(); };
7*f4a2713aSLionel Sambuc struct AA { int a; virtual int aa(); };
8*f4a2713aSLionel Sambuc struct BB { int b; virtual int bb(); };
9*f4a2713aSLionel Sambuc struct CC : AA, BB { virtual int aa(); virtual int bb(); virtual int cc(); };
10*f4a2713aSLionel Sambuc struct D : virtual C, virtual CC { int e; };
11*f4a2713aSLionel Sambuc 
12*f4a2713aSLionel Sambuc D* x;
13*f4a2713aSLionel Sambuc 
14*f4a2713aSLionel Sambuc A* a() { return x; }
15*f4a2713aSLionel Sambuc // CHECK: @_Z1av() [[NUW:#[0-9]+]]
16*f4a2713aSLionel Sambuc // CHECK: [[VBASEOFFSETPTRA:%[a-zA-Z0-9\.]+]] = getelementptr i8* {{.*}}, i64 -16
17*f4a2713aSLionel Sambuc // CHECK: [[CASTVBASEOFFSETPTRA:%[a-zA-Z0-9\.]+]] = bitcast i8* [[VBASEOFFSETPTRA]] to i32*
18*f4a2713aSLionel Sambuc // CHECK: load i32* [[CASTVBASEOFFSETPTRA]]
19*f4a2713aSLionel Sambuc // CHECK: }
20*f4a2713aSLionel Sambuc 
21*f4a2713aSLionel Sambuc // MSVC: @"\01?a@@YAPAUA@@XZ"() [[NUW:#[0-9]+]] {
22*f4a2713aSLionel Sambuc // MSVC:   %[[vbptr_off:.*]] = getelementptr inbounds i8* {{.*}}, i32 0
23*f4a2713aSLionel Sambuc // MSVC:   %[[vbptr:.*]] = bitcast i8* %[[vbptr_off]] to i8**
24*f4a2713aSLionel Sambuc // MSVC:   %[[vbtable:.*]] = load i8** %[[vbptr]]
25*f4a2713aSLionel Sambuc // MSVC:   %[[entry:.*]] = getelementptr inbounds i8* {{.*}}, i32 4
26*f4a2713aSLionel Sambuc // MSVC:   %[[entry_i32:.*]] = bitcast i8* %[[entry]] to i32*
27*f4a2713aSLionel Sambuc // MSVC:   %[[offset:.*]] = load i32* %[[entry_i32]]
28*f4a2713aSLionel Sambuc // MSVC:   add nsw i32 0, %[[offset]]
29*f4a2713aSLionel Sambuc // MSVC: }
30*f4a2713aSLionel Sambuc 
31*f4a2713aSLionel Sambuc B* b() { return x; }
32*f4a2713aSLionel Sambuc // CHECK: @_Z1bv() [[NUW]]
33*f4a2713aSLionel Sambuc // CHECK: [[VBASEOFFSETPTRA:%[a-zA-Z0-9\.]+]] = getelementptr i8* {{.*}}, i64 -20
34*f4a2713aSLionel Sambuc // CHECK: [[CASTVBASEOFFSETPTRA:%[a-zA-Z0-9\.]+]] = bitcast i8* [[VBASEOFFSETPTRA]] to i32*
35*f4a2713aSLionel Sambuc // CHECK: load i32* [[CASTVBASEOFFSETPTRA]]
36*f4a2713aSLionel Sambuc // CHECK: }
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc // Same as 'a' except we use a different vbtable offset.
39*f4a2713aSLionel Sambuc // MSVC: @"\01?b@@YAPAUB@@XZ"() [[NUW:#[0-9]+]] {
40*f4a2713aSLionel Sambuc // MSVC:   %[[vbptr_off:.*]] = getelementptr inbounds i8* {{.*}}, i32 0
41*f4a2713aSLionel Sambuc // MSVC:   %[[vbptr:.*]] = bitcast i8* %[[vbptr_off]] to i8**
42*f4a2713aSLionel Sambuc // MSVC:   %[[vbtable:.*]] = load i8** %[[vbptr]]
43*f4a2713aSLionel Sambuc // MSVC:   %[[entry:.*]] = getelementptr inbounds i8* {{.*}}, i32 8
44*f4a2713aSLionel Sambuc // MSVC:   %[[entry_i32:.*]] = bitcast i8* %[[entry]] to i32*
45*f4a2713aSLionel Sambuc // MSVC:   %[[offset:.*]] = load i32* %[[entry_i32]]
46*f4a2713aSLionel Sambuc // MSVC:   add nsw i32 0, %[[offset]]
47*f4a2713aSLionel Sambuc // MSVC: }
48*f4a2713aSLionel Sambuc 
49*f4a2713aSLionel Sambuc 
50*f4a2713aSLionel Sambuc BB* c() { return x; }
51*f4a2713aSLionel Sambuc // CHECK: @_Z1cv() [[NUW]]
52*f4a2713aSLionel Sambuc // CHECK: [[VBASEOFFSETPTRC:%[a-zA-Z0-9\.]+]] = getelementptr i8* {{.*}}, i64 -24
53*f4a2713aSLionel Sambuc // CHECK: [[CASTVBASEOFFSETPTRC:%[a-zA-Z0-9\.]+]] = bitcast i8* [[VBASEOFFSETPTRC]] to i32*
54*f4a2713aSLionel Sambuc // CHECK: [[VBASEOFFSETC:%[a-zA-Z0-9\.]+]] = load i32* [[CASTVBASEOFFSETPTRC]]
55*f4a2713aSLionel Sambuc // CHECK: add i32 [[VBASEOFFSETC]], 8
56*f4a2713aSLionel Sambuc // CHECK: }
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc // Same as 'a' except we use a different vbtable offset.
59*f4a2713aSLionel Sambuc // MSVC: @"\01?c@@YAPAUBB@@XZ"() [[NUW:#[0-9]+]] {
60*f4a2713aSLionel Sambuc // MSVC:   %[[vbptr_off:.*]] = getelementptr inbounds i8* {{.*}}, i32 0
61*f4a2713aSLionel Sambuc // MSVC:   %[[vbptr:.*]] = bitcast i8* %[[vbptr_off]] to i8**
62*f4a2713aSLionel Sambuc // MSVC:   %[[vbtable:.*]] = load i8** %[[vbptr]]
63*f4a2713aSLionel Sambuc // MSVC:   %[[entry:.*]] = getelementptr inbounds i8* {{.*}}, i32 16
64*f4a2713aSLionel Sambuc // MSVC:   %[[entry_i32:.*]] = bitcast i8* %[[entry]] to i32*
65*f4a2713aSLionel Sambuc // MSVC:   %[[offset:.*]] = load i32* %[[entry_i32]]
66*f4a2713aSLionel Sambuc // MSVC:   add nsw i32 0, %[[offset]]
67*f4a2713aSLionel Sambuc // MSVC: }
68*f4a2713aSLionel Sambuc 
69*f4a2713aSLionel Sambuc // Put the vbptr at a non-zero offset inside a non-virtual base.
70*f4a2713aSLionel Sambuc struct E { int e; };
71*f4a2713aSLionel Sambuc struct F : E, D { int f; };
72*f4a2713aSLionel Sambuc 
73*f4a2713aSLionel Sambuc F* y;
74*f4a2713aSLionel Sambuc 
75*f4a2713aSLionel Sambuc BB* d() { return y; }
76*f4a2713aSLionel Sambuc 
77*f4a2713aSLionel Sambuc // Same as 'c' except the vbptr offset is 4, changing the initial GEP and the
78*f4a2713aSLionel Sambuc // final add.
79*f4a2713aSLionel Sambuc // MSVC: @"\01?d@@YAPAUBB@@XZ"() [[NUW:#[0-9]+]] {
80*f4a2713aSLionel Sambuc // MSVC:   %[[vbptr_off:.*]] = getelementptr inbounds i8* {{.*}}, i32 4
81*f4a2713aSLionel Sambuc // MSVC:   %[[vbptr:.*]] = bitcast i8* %[[vbptr_off]] to i8**
82*f4a2713aSLionel Sambuc // MSVC:   %[[vbtable:.*]] = load i8** %[[vbptr]]
83*f4a2713aSLionel Sambuc // MSVC:   %[[entry:.*]] = getelementptr inbounds i8* {{.*}}, i32 16
84*f4a2713aSLionel Sambuc // MSVC:   %[[entry_i32:.*]] = bitcast i8* %[[entry]] to i32*
85*f4a2713aSLionel Sambuc // MSVC:   %[[offset:.*]] = load i32* %[[entry_i32]]
86*f4a2713aSLionel Sambuc // MSVC:   add nsw i32 4, %[[offset]]
87*f4a2713aSLionel Sambuc // MSVC: }
88*f4a2713aSLionel Sambuc 
89*f4a2713aSLionel Sambuc // CHECK: attributes [[NUW]] = { nounwind{{.*}} }
90