xref: /llvm-project/clang/test/CodeGenCXX/copy-constructor-elim-2.cpp (revision 3512721d52b3380ea4d3f5b2419d0b7b072e7797)
1 // RUN: %clang_cc1 %std_cxx11-14 -no-enable-noundef-analysis -triple armv7-none-eabi -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,PRE17
2 // RUN: %clang_cc1 %std_cxx17- -no-enable-noundef-analysis -triple armv7-none-eabi -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,CXX17
3 
4 struct A { int x; A(int); ~A(); };
f()5 A f() { return A(0); }
6 // CHECK-LABEL: define{{.*}} void @_Z1fv
7 // CHECK: call {{.*}} @_ZN1AC1Ei
8 // CHECK-NEXT: ret void
9 
10 // Verify that we do not elide copies when constructing a base class before C++17.
11 namespace no_elide_base {
12   struct Base {
13     Base(const Base&);
14     ~Base();
15   };
16 
17   struct Other {
18     operator Base() const;
19   };
20 
21   struct Derived : public virtual Base {
22     Derived(const Other &O);
23   };
24 
25   // CHECK: define {{.*}} @_ZN13no_elide_base7DerivedC1ERKNS_5OtherE(ptr {{[^,]*}} returned {{[^,]*}} %this, ptr nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) %O) unnamed_addr
Derived(const Other & O)26   Derived::Derived(const Other &O)
27     // CHECK: call {{.*}} @_ZNK13no_elide_base5OthercvNS_4BaseEEv
28     // PRE17: call {{.*}} @_ZN13no_elide_base4BaseC2ERKS0_
29     // PRE17: call {{.*}} @_ZN13no_elide_base4BaseD1Ev
30     // CXX17-NOT: call
31     : Base(O)
32   {
33     // CHECK: ret
34   }
35 }
36 
37 // PR8683.
38 
39 namespace PR8683 {
40 
41 struct A {
42   A();
43   A(const A&);
44   A& operator=(const A&);
45 };
46 
47 struct B {
48   A a;
49 };
50 
f()51 void f() {
52   // Verify that we don't mark the copy constructor in this expression as elidable.
53   // CHECK: call {{.*}} @_ZN6PR86831AC1ERKS0_
54   A a = (B().a);
55 }
56 
57 }
58 
59 namespace PR12139 {
60   struct A {
APR12139::A61     A() : value(1) { }
APR12139::A62     A(A const &, int value = 2) : value(value) { }
63     int value;
64 
makeAPR12139::A65     static A makeA() { A a; a.value = 2; return a; }
66   };
67 
68   // CHECK-LABEL: define{{.*}} i32 @_ZN7PR121394testEv
test()69   int test() {
70     // CHECK: call void @_ZN7PR121391A5makeAEv
71     // CHECK-NEXT: call ptr @_ZN7PR121391AC1ERKS0_i
72     A a(A::makeA(), 3);
73     // CHECK-NEXT: getelementptr inbounds
74     // CHECK-NEXT: load
75     // CHECK-NEXT: ret i32
76     return a.value;
77   }
78 }
79 
80 namespace ElidableCallIsNotCopyCtor {
81   struct A { A(const A&); };
82   struct B : A {
83     B(B&);
84     B(A);
85     B(int);
86   };
f()87   void f() {
88     // Before C++17, we construct via B(int) then B(A). The B(A) construction is
89     // elidable, but we don't have an AST representation for the case where we
90     // must elide not only a constructor call but also some argument
91     // conversions, so we don't elide it.
92     // CHECK-LABEL: define{{.*}} void @_ZN25ElidableCallIsNotCopyCtor1fEv(
93     // CHECK: call {{.*}} @_ZN25ElidableCallIsNotCopyCtor1BC1Ei(
94     // PRE17: call {{.*}} @_ZN25ElidableCallIsNotCopyCtor1AC1ERKS0_(
95     // PRE17: call {{.*}} @_ZN25ElidableCallIsNotCopyCtor1BC1ENS_1AE(
96     // CXX17-NOT: call
97     // CHECK: ret
98     B b = 0;
99   }
100 }
101