1 // RUN: %clang_cc1 -Wno-unused-value -triple %itanium_abi_triple -emit-llvm -o - %s | FileCheck %s 2 // rdar: //8540501 3 extern "C" int printf(...); 4 extern "C" void abort(); 5 6 struct A 7 { 8 int i; 9 A (int j) : i(j) {printf("this = %p A(%d)\n", this, j);} 10 A (const A &j) : i(j.i) {printf("this = %p const A&(%d)\n", this, i);} 11 A& operator= (const A &j) { i = j.i; abort(); return *this; } 12 ~A() { printf("this = %p ~A(%d)\n", this, i); } 13 }; 14 15 struct B 16 { 17 int i; 18 B (const A& a) { i = a.i; } 19 B() {printf("this = %p B()\n", this);} 20 B (const B &j) : i(j.i) {printf("this = %p const B&(%d)\n", this, i);} 21 ~B() { printf("this = %p ~B(%d)\n", this, i); } 22 }; 23 24 A foo(int j) 25 { 26 return ({ j ? A(1) : A(0); }); 27 } 28 29 30 void foo2() 31 { 32 A b = ({ A a(1); A a1(2); A a2(3); a1; a2; a; }); 33 if (b.i != 1) 34 abort(); 35 A c = ({ A a(1); A a1(2); A a2(3); a1; a2; a; A a3(4); a2; a3; }); 36 if (c.i != 4) 37 abort(); 38 } 39 40 void foo3() 41 { 42 const A &b = ({ A a(1); a; }); 43 if (b.i != 1) 44 abort(); 45 } 46 47 void foo4() 48 { 49 // CHECK: call {{.*}} @_ZN1AC1Ei 50 // CHECK: call {{.*}} @_ZN1AC1ERKS_ 51 // CHECK: call {{.*}} @_ZN1AD1Ev 52 // CHECK: call {{.*}} @_ZN1BC1ERK1A 53 // CHECK: call {{.*}} @_ZN1AD1Ev 54 const B &b = ({ A a(1); a; }); 55 if (b.i != 1) 56 abort(); 57 } 58 59 int main() 60 { 61 foo2(); 62 foo3(); 63 foo4(); 64 return foo(1).i-1; 65 } 66 67 // rdar: // 8600553 68 int a[128]; 69 int* foo5() { 70 // CHECK-NOT: memcpy 71 // Check that array-to-pointer conversion occurs in a 72 // statement-expression. 73 return (({ a; })); 74 } 75 76 // <rdar://problem/14074868> 77 // Make sure this doesn't crash. 78 int foo5(bool b) { 79 int y = 0; 80 y = ({ A a(1); if (b) goto G; a.i; }); 81 G: return y; 82 } 83 84 // When we emit a full expression with cleanups that contains branches out of 85 // the full expression, the result of the inner expression (the call to 86 // call_with_cleanups in this case) may not dominate the fallthrough destination 87 // of the shared cleanup block. 88 // 89 // In this case the CFG will be a sequence of two diamonds, but the only 90 // dynamically possible execution paths are both left hand branches and both 91 // right hand branches. The first diamond LHS will call bar, and the second 92 // diamond LHS will assign the result to v, but the call to bar does not 93 // dominate the assignment. 94 int bar(A, int); 95 extern "C" int cleanup_exit_scalar(bool b) { 96 int v = bar(A(1), ({ if (b) return 42; 13; })); 97 return v; 98 } 99 100 // CHECK-LABEL: define{{.*}} i32 @cleanup_exit_scalar({{.*}}) 101 // CHECK: call {{.*}} @_ZN1AC1Ei 102 // Spill after bar. 103 // CHECK: %[[v:[^ ]*]] = call{{.*}} i32 @_Z3bar1Ai({{.*}}) 104 // CHECK-NEXT: store i32 %[[v]], i32* %[[tmp:[^, ]*]] 105 // Do cleanup. 106 // CHECK: call {{.*}} @_ZN1AD1Ev 107 // CHECK: switch 108 // Reload before v assignment. 109 // CHECK: %[[v:[^ ]*]] = load i32, i32* %[[tmp]] 110 // CHECK-NEXT: store i32 %[[v]], i32* %v 111 112 // No need to spill when the expression result is a constant, constants don't 113 // have dominance problems. 114 extern "C" int cleanup_exit_scalar_constant(bool b) { 115 int v = (A(1), (void)({ if (b) return 42; 0; }), 13); 116 return v; 117 } 118 119 // CHECK-LABEL: define{{.*}} i32 @cleanup_exit_scalar_constant({{.*}}) 120 // CHECK: store i32 13, i32* %v 121 122 // Check for the same bug for lvalue expression evaluation kind. 123 // FIXME: What about non-reference lvalues, like bitfield lvalues and vector 124 // lvalues? 125 int &getref(); 126 extern "C" int cleanup_exit_lvalue(bool cond) { 127 int &r = (A(1), ({ if (cond) return 0; (void)0; }), getref()); 128 return r; 129 } 130 // CHECK-LABEL: define{{.*}} i32 @cleanup_exit_lvalue({{.*}}) 131 // CHECK: call {{.*}} @_ZN1AC1Ei 132 // Spill after bar. 133 // CHECK: %[[v:[^ ]*]] = call dereferenceable(4) i32* @_Z6getrefv({{.*}}) 134 // CHECK-NEXT: store i32* %[[v]], i32** %[[tmp:[^, ]*]] 135 // Do cleanup. 136 // CHECK: call {{.*}} @_ZN1AD1Ev 137 // CHECK: switch 138 // Reload before v assignment. 139 // CHECK: %[[v:[^ ]*]] = load i32*, i32** %[[tmp]] 140 // CHECK-NEXT: store i32* %[[v]], i32** %r 141 142 143 // We handle ExprWithCleanups for complex evaluation type separately, and it had 144 // the same bug. 145 _Complex float bar_complex(A, int); 146 extern "C" int cleanup_exit_complex(bool b) { 147 _Complex float v = bar_complex(A(1), ({ if (b) return 42; 13; })); 148 return v; 149 } 150 151 // CHECK-LABEL: define{{.*}} i32 @cleanup_exit_complex({{.*}}) 152 // CHECK: call {{.*}} @_ZN1AC1Ei 153 // Spill after bar. 154 // CHECK: call {{.*}} @_Z11bar_complex1Ai({{.*}}) 155 // CHECK: store float %{{.*}}, float* %[[tmp1:[^, ]*]] 156 // CHECK: store float %{{.*}}, float* %[[tmp2:[^, ]*]] 157 // Do cleanup. 158 // CHECK: call {{.*}} @_ZN1AD1Ev 159 // CHECK: switch 160 // Reload before v assignment. 161 // CHECK: %[[v1:[^ ]*]] = load float, float* %[[tmp1]] 162 // CHECK: %[[v2:[^ ]*]] = load float, float* %[[tmp2]] 163 // CHECK: store float %[[v1]], float* %v.realp 164 // CHECK: store float %[[v2]], float* %v.imagp 165