| 1 |
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir |
| 2 |
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR |
| 3 |
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-elide-constructors -fclangir -emit-cir %s -o %t-noelide.cir |
| 4 |
// RUN: FileCheck --input-file=%t-noelide.cir %s --check-prefix=CIR-NOELIDE |
| 5 |
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll |
| 6 |
// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefix=LLVM |
| 7 |
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll |
| 8 |
// RUN: FileCheck --input-file=%t.ll %s --check-prefix=OGCG |
| 9 |
|
| 10 |
// There are no LLVM and OGCG tests with -fno-elide-constructors because the |
| 11 |
// lowering isn't of interest for this test. We just need to see that the |
| 12 |
// copy constructor is elided without -fno-elide-constructors but not with it. |
| 13 |
|
| 14 |
struct S { |
| 15 |
S(); |
| 16 |
int a; |
| 17 |
int b; |
| 18 |
}; |
| 19 |
|
| 20 |
struct S f1() { |
| 21 |
S s; |
| 22 |
return s; |
| 23 |
} |
| 24 |
|
| 25 |
// CIR: cir.func{{.*}} @_Z2f1v() -> !rec_S |
| 26 |
// CIR-NEXT: %[[RETVAL:.*]] = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["__retval", init] |
| 27 |
// CIR-NEXT: cir.call @_ZN1SC1Ev(%[[RETVAL]]) : (!cir.ptr<!rec_S>) -> () |
| 28 |
// CIR-NEXT: %[[RET:.*]] = cir.load %[[RETVAL]] : !cir.ptr<!rec_S>, !rec_S |
| 29 |
// CIR-NEXT: cir.return %[[RET]] |
| 30 |
|
| 31 |
// CIR-NOELIDE: cir.func{{.*}} @_Z2f1v() -> !rec_S |
| 32 |
// CIR-NOELIDE-NEXT: %[[RETVAL:.*]] = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["__retval"] |
| 33 |
// CIR-NOELIDE-NEXT: %[[S:.*]] = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["s", init] |
| 34 |
// CIR-NOELIDE-NEXT: cir.call @_ZN1SC1Ev(%[[S]]) : (!cir.ptr<!rec_S>) -> () |
| 35 |
// CIR-NOELIDE-NEXT: cir.call @_ZN1SC1EOS_(%[[RETVAL]], %[[S]]){{.*}} : (!cir.ptr<!rec_S>, !cir.ptr<!rec_S>) -> () |
| 36 |
// CIR-NOELIDE-NEXT: %[[RET:.*]] = cir.load %[[RETVAL]] : !cir.ptr<!rec_S>, !rec_S |
| 37 |
// CIR-NOELIDE-NEXT: cir.return %[[RET]] |
| 38 |
|
| 39 |
// FIXME: Update this when calling convetnion lowering is implemented. |
| 40 |
// LLVM: define{{.*}} %struct.S @_Z2f1v() |
| 41 |
// LLVM-NEXT: %[[RETVAL:.*]] = alloca %struct.S |
| 42 |
// LLVM-NEXT: call void @_ZN1SC1Ev(ptr %[[RETVAL]]) |
| 43 |
// LLVM-NEXT: %[[RET:.*]] = load %struct.S, ptr %[[RETVAL]] |
| 44 |
// LLVM-NEXT: ret %struct.S %[[RET]] |
| 45 |
|
| 46 |
// OGCG: define{{.*}} i64 @_Z2f1v() |
| 47 |
// OGCG-NEXT: entry: |
| 48 |
// OGCG-NEXT: %[[RETVAL:.*]] = alloca %struct.S |
| 49 |
// OGCG-NEXT: call void @_ZN1SC1Ev(ptr {{.*}} %[[RETVAL]]) |
| 50 |
// OGCG-NEXT: %[[RET:.*]] = load i64, ptr %[[RETVAL]] |
| 51 |
// OGCG-NEXT: ret i64 %[[RET]] |
| 52 |
|
| 53 |
struct NonTrivial { |
| 54 |
~NonTrivial(); |
| 55 |
}; |
| 56 |
|
| 57 |
void maybeThrow(); |
| 58 |
|
| 59 |
NonTrivial test_nrvo() { |
| 60 |
NonTrivial result; |
| 61 |
maybeThrow(); |
| 62 |
return result; |
| 63 |
} |
| 64 |
|
| 65 |
// TODO(cir): Handle normal cleanup properly. |
| 66 |
|
| 67 |
// CIR: cir.func {{.*}} @_Z9test_nrvov() |
| 68 |
// CIR: %[[RESULT:.*]] = cir.alloca !rec_NonTrivial, !cir.ptr<!rec_NonTrivial>, ["__retval"] |
| 69 |
// CIR: %[[NRVO_FLAG:.*]] = cir.alloca !cir.bool, !cir.ptr<!cir.bool>, ["nrvo"] |
| 70 |
// CIR: %[[FALSE:.*]] = cir.const #false |
| 71 |
// CIR: cir.store{{.*}} %[[FALSE]], %[[NRVO_FLAG]] |
| 72 |
// CIR: cir.call @_Z10maybeThrowv() : () -> () |
| 73 |
// CIR: %[[TRUE:.*]] = cir.const #true |
| 74 |
// CIR: cir.store{{.*}} %[[TRUE]], %[[NRVO_FLAG]] |
| 75 |
// CIR: %[[NRVO_FLAG_VAL:.*]] = cir.load{{.*}} %[[NRVO_FLAG]] |
| 76 |
// CIR: %[[NOT_NRVO_VAL:.*]] = cir.unary(not, %[[NRVO_FLAG_VAL]]) |
| 77 |
// CIR: cir.if %[[NOT_NRVO_VAL]] { |
| 78 |
// CIR: cir.call @_ZN10NonTrivialD1Ev(%[[RESULT]]) |
| 79 |
// CIR: } |
| 80 |
// CIR: %[[RET:.*]] = cir.load %[[RESULT]] |
| 81 |
// CIR: cir.return %[[RET]] |
| 82 |
|
| 83 |
// LLVM: define {{.*}} %struct.NonTrivial @_Z9test_nrvov() |
| 84 |
// LLVM: %[[RESULT:.*]] = alloca %struct.NonTrivial |
| 85 |
// LLVM: %[[NRVO_FLAG:.*]] = alloca i8 |
| 86 |
// LLVM: store i8 0, ptr %[[NRVO_FLAG]] |
| 87 |
// LLVM: call void @_Z10maybeThrowv() |
| 88 |
// LLVM: store i8 1, ptr %[[NRVO_FLAG]] |
| 89 |
// LLVM: %[[NRVO_VAL:.*]] = load i8, ptr %[[NRVO_FLAG]] |
| 90 |
// LLVM: %[[NRVO_VAL_TRUNC:.*]] = trunc i8 %[[NRVO_VAL]] to i1 |
| 91 |
// LLVM: %[[NOT_NRVO_VAL:.*]] = xor i1 %[[NRVO_VAL_TRUNC]], true |
| 92 |
// LLVM: br i1 %[[NOT_NRVO_VAL]], label %[[NRVO_UNUSED:.*]], label %[[NRVO_USED:.*]] |
| 93 |
// LLVM: [[NRVO_UNUSED]]: |
| 94 |
// LLVM: call void @_ZN10NonTrivialD1Ev(ptr %[[RESULT]]) |
| 95 |
// LLVM: br label %[[NRVO_USED]] |
| 96 |
// LLVM: [[NRVO_USED]]: |
| 97 |
// LLVM: %[[RET:.*]] = load %struct.NonTrivial, ptr %[[RESULT]] |
| 98 |
// LLVM: ret %struct.NonTrivial %[[RET]] |
| 99 |
|
| 100 |
// OGCG: define {{.*}} void @_Z9test_nrvov(ptr {{.*}} sret(%struct.NonTrivial) {{.*}} %[[RESULT:.*]]) |
| 101 |
// OGCG: %[[RESULT_ADDR:.*]] = alloca ptr |
| 102 |
// OGCG: %[[NRVO_FLAG:.*]] = alloca i1, align 1 |
| 103 |
// OGCG: store ptr %[[RESULT]], ptr %[[RESULT_ADDR]] |
| 104 |
// OGCG: store i1 false, ptr %[[NRVO_FLAG]] |
| 105 |
// OGCG: call void @_Z10maybeThrowv() |
| 106 |
// OGCG: store i1 true, ptr %[[NRVO_FLAG]] |
| 107 |
// OGCG: %[[NRVO_VAL:.*]] = load i1, ptr %[[NRVO_FLAG]] |
| 108 |
// OGCG: br i1 %[[NRVO_VAL]], label %[[SKIPDTOR:.*]], label %[[NRVO_UNUSED:.*]] |
| 109 |
// OGCG: [[NRVO_UNUSED]]: |
| 110 |
// OGCG: call void @_ZN10NonTrivialD1Ev(ptr {{.*}} %[[RESULT]]) |
| 111 |
// OGCG: br label %[[SKIPDTOR]] |
| 112 |
// OGCG: [[SKIPDTOR]]: |
| 113 |
// OGCG: ret void |