Home | History | Annotate | Download | only in CodeGenCXX
      1 // RUN: %clang_cc1 %s -triple x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s
      2 
      3 struct A {
      4   virtual ~A();
      5 };
      6 
      7 struct B : A { };
      8 
      9 struct C {
     10   int i;
     11   B b;
     12 };
     13 
     14 // CHECK: _Z15test_value_initv
     15 void test_value_init() {
     16   // This value initialization requires zero initialization of the 'B'
     17   // subobject followed by a call to its constructor.
     18   // PR5800
     19 
     20   // CHECK: store i32 17
     21   // CHECK: call void @llvm.memset.p0i8.i64
     22   // CHECK: call void @_ZN1BC1Ev
     23   C c = { 17 } ;
     24   // CHECK: call void @_ZN1CD1Ev
     25 }
     26 
     27 enum enum_type { negative_number = -1, magic_number = 42 };
     28 
     29 class enum_holder
     30 {
     31   enum_type m_enum;
     32 
     33 public:
     34   enum_holder() : m_enum(magic_number) { }
     35 };
     36 
     37 struct enum_holder_and_int
     38 {
     39   enum_holder e;
     40   int i;
     41 };
     42 
     43 // CHECK: _Z24test_enum_holder_and_intv()
     44 void test_enum_holder_and_int() {
     45   // CHECK: alloca
     46   // CHECK-NEXT: bitcast
     47   // CHECK-NEXT: call void @llvm.memset
     48   // CHECK-NEXT: call void @_ZN19enum_holder_and_intC1Ev
     49   enum_holder_and_int();
     50   // CHECK-NEXT: ret void
     51 }
     52 
     53 // PR7834: don't crash.
     54 namespace test1 {
     55   struct A {
     56     int A::*f;
     57     A();
     58     A(const A&);
     59     A &operator=(const A &);
     60   };
     61 
     62   struct B {
     63     A base;
     64   };
     65 
     66   void foo() {
     67     B();
     68   }
     69 }
     70 
     71 namespace ptrmem {
     72   struct S {
     73     int mem1;
     74     int S::*mem2;
     75   };
     76 
     77   // CHECK-LABEL: define i32 @_ZN6ptrmem4testEPNS_1SE
     78   int test(S *s) {
     79     // CHECK: call void @llvm.memcpy.p0i8.p0i8.i64
     80     // CHECK: getelementptr
     81     // CHECK: ret
     82     return s->*S().mem2;
     83   }
     84 }
     85 
     86 namespace PR9801 {
     87 
     88 struct Test {
     89   Test() : i(10) {}
     90   Test(int i) : i(i) {}
     91   int i;
     92 private:
     93   int j;
     94 };
     95 
     96 struct Test2 {
     97   Test t;
     98 };
     99 
    100 struct Test3 : public Test { };
    101 
    102 // CHECK-LABEL: define void @_ZN6PR98011fEv
    103 void f() {
    104   // CHECK-NOT: call void @llvm.memset.p0i8.i64
    105   // CHECK: call void @_ZN6PR98014TestC1Ei
    106   // CHECK-NOT: call void @llvm.memset.p0i8.i64
    107   // CHECK: call void @_ZN6PR98014TestC1Ev
    108   Test partial[3] = { 1 };
    109 
    110   // CHECK-NOT: call void @llvm.memset.p0i8.i64
    111   // CHECK: call void @_ZN6PR98014TestC1Ev
    112   // CHECK-NOT: call void @_ZN6PR98014TestC1Ev
    113   Test empty[3] = {};
    114 
    115   // CHECK: call void @llvm.memset.p0i8.i64
    116   // CHECK-NOT: call void @llvm.memset.p0i8.i64
    117   // CHECK: call void @_ZN6PR98015Test2C1Ev
    118   // CHECK-NOT: call void @_ZN6PR98015Test2C1Ev
    119   Test2 empty2[3] = {};
    120 
    121   // CHECK: call void @llvm.memset.p0i8.i64
    122   // CHECK-NOT: call void @llvm.memset.p0i8.i64
    123   // CHECK: call void @_ZN6PR98015Test3C1Ev
    124   // CHECK-NOT: call void @llvm.memset.p0i8.i64
    125   // CHECK-NOT: call void @_ZN6PR98015Test3C1Ev
    126   Test3 empty3[3] = {};
    127 }
    128 
    129 }
    130 
    131 namespace zeroinit {
    132   struct S { int i; };
    133 
    134   // CHECK-LABEL: define i32 @_ZN8zeroinit4testEv()
    135   int test() {
    136     // CHECK: call void @llvm.memset.p0i8.i64
    137     // CHECK: ret i32 0
    138     return S().i;
    139   }
    140 
    141   struct X0 {
    142     X0() { }
    143     int x;
    144   };
    145 
    146   struct X1 : X0 {
    147     int x1;
    148     void f();
    149   };
    150 
    151   // CHECK-LABEL: define void @_ZN8zeroinit9testX0_X1Ev
    152   void testX0_X1() {
    153     // CHECK: call void @llvm.memset.p0i8.i64
    154     // CHECK-NEXT: call void @_ZN8zeroinit2X1C1Ev
    155     // CHECK-NEXT: call void @_ZN8zeroinit2X11fEv
    156     X1().f();
    157   }
    158 
    159   template<typename>
    160   struct X2 : X0 {
    161     int x2;
    162     void f();
    163   };
    164 
    165   template<typename>
    166   struct X3 : X2<int> {
    167     X3() : X2<int>() { }
    168     int i;
    169   };
    170 
    171 
    172   // CHECK-LABEL: define void @_ZN8zeroinit9testX0_X3Ev
    173   void testX0_X3() {
    174     // CHECK-NOT: call void @llvm.memset
    175     // CHECK: call void @_ZN8zeroinit2X3IiEC1Ev
    176     // CHECK: call void @_ZN8zeroinit2X2IiE1fEv
    177     // CHECK-NEXT: ret void
    178     X3<int>().f();
    179   }
    180 
    181   // More checks at EOF
    182 }
    183 
    184 namespace PR8726 {
    185 class C;
    186 struct S {
    187   const C &c1;
    188   int i;
    189   const C &c2;
    190 };
    191 void f(const C& c) {
    192   S s = {c, 42, c};
    193 }
    194 
    195 }
    196 
    197 // rdar://problem/9355931
    198 namespace test6 {
    199   struct A { A(); A(int); };
    200 
    201   void test() {
    202     A arr[10][20] = { 5 };
    203   };
    204   // CHECK-LABEL:    define void @_ZN5test64testEv()
    205   // CHECK:      [[ARR:%.*]] = alloca [10 x [20 x [[A:%.*]]]],
    206 
    207   // CHECK-NEXT: [[INNER:%.*]] = getelementptr inbounds [10 x [20 x [[A]]]]* [[ARR]], i64 0, i64 0
    208   // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [20 x [[A]]]* [[INNER]], i64 0, i64 0
    209   // CHECK-NEXT: call void @_ZN5test61AC1Ei([[A]]* [[T0]], i32 5)
    210   // CHECK-NEXT: [[BEGIN:%.*]] = getelementptr inbounds [[A]]* [[T0]], i64 1
    211   // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [[A]]* [[T0]], i64 20
    212   // CHECK-NEXT: br label
    213   // CHECK:      [[CUR:%.*]] = phi [[A]]* [ [[BEGIN]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ]
    214   // CHECK-NEXT: call void @_ZN5test61AC1Ev([[A]]* [[CUR]])
    215   // CHECK-NEXT: [[NEXT]] = getelementptr inbounds [[A]]* [[CUR]], i64 1
    216   // CHECK-NEXT: [[T0:%.*]] = icmp eq [[A]]* [[NEXT]], [[END]]
    217   // CHECK-NEXT: br i1
    218 
    219   // CHECK:      [[BEGIN:%.*]] = getelementptr inbounds [20 x [[A]]]* [[INNER]], i64 1
    220   // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [20 x [[A]]]* [[INNER]], i64 10
    221   // CHECK-NEXT: br label
    222   // CHECK:      [[CUR:%.*]] = phi [20 x [[A]]]* [ [[BEGIN]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ]
    223 
    224   // Inner loop.
    225   // CHECK-NEXT: [[IBEGIN:%.*]] = getelementptr inbounds [20 x [[A]]]* [[CUR]], i32 0, i32 0
    226   // CHECK-NEXT: [[IEND:%.*]] = getelementptr inbounds [[A]]* [[IBEGIN]], i64 20
    227   // CHECK-NEXT: br label
    228   // CHECK:      [[ICUR:%.*]] = phi [[A]]* [ [[IBEGIN]], {{%.*}} ], [ [[INEXT:%.*]], {{%.*}} ]
    229   // CHECK-NEXT: call void @_ZN5test61AC1Ev([[A]]* [[ICUR]])
    230   // CHECK-NEXT: [[INEXT:%.*]] = getelementptr inbounds [[A]]* [[ICUR]], i64 1
    231   // CHECK-NEXT: [[T0:%.*]] = icmp eq [[A]]* [[INEXT]], [[IEND]]
    232   // CHECK-NEXT: br i1 [[T0]],
    233 
    234   // CHECK:      [[NEXT]] = getelementptr inbounds [20 x [[A]]]* [[CUR]], i64 1
    235   // CHECK-NEXT: [[T0:%.*]] = icmp eq [20 x [[A]]]* [[NEXT]], [[END]]
    236   // CHECK-NEXT: br i1 [[T0]]
    237   // CHECK:      ret void
    238 }
    239 
    240 namespace PR11124 {
    241   // Make sure C::C doesn't overwrite parts of A while it is zero-initializing B
    242   struct A { int a; A(); A(int); };
    243   struct B : virtual A { int b; };
    244   struct C : B { C(); };
    245   C::C() : A(3), B() {}
    246   // CHECK-LABEL: define void @_ZN7PR111241CC1Ev
    247   // CHECK: call void @llvm.memset.p0i8.i64(i8* {{.*}}, i8 0, i64 12, i32 8, i1 false)
    248   // CHECK-NEXT: call void @_ZN7PR111241BC2Ev
    249   // Make sure C::C doesn't overwrite parts of A while it is zero-initializing B
    250 
    251   struct B2 : virtual A { int B::*b; };
    252   struct C2 : B2 { C2(); };
    253   C2::C2() : A(3), B2() {}
    254   // CHECK-LABEL: define void @_ZN7PR111242C2C1Ev
    255   // CHECK: call void @llvm.memcpy.p0i8.p0i8.i64(i8* %{{.*}}, i8* {{.*}}, i64 16, i32 8, i1 false)
    256   // CHECK-NEXT: call void @_ZN7PR111242B2C2Ev
    257 }
    258 
    259 // Ensure we produce an i1 here, and don't assert.
    260 // CHECK-LABEL: define void @_Z9r170806_bv(
    261 // CHECK: call void @_Z9r170806_ab(i1 zeroext false)
    262 void r170806_a(bool b = bool());
    263 void r170806_b() { r170806_a(); }
    264 
    265 // CHECK-LABEL: define linkonce_odr void @_ZN8zeroinit2X3IiEC2Ev(%"struct.zeroinit::X3"* %this) unnamed_addr
    266 // CHECK: call void @llvm.memset.p0i8.i64
    267 // CHECK-NEXT: call void @_ZN8zeroinit2X2IiEC2Ev
    268 // CHECK-NEXT: ret void
    269