| 1 |
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fexceptions -fcxx-exceptions -emit-cir %s -o %t.cir |
| 2 |
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s |
| 3 |
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fexceptions -fcxx-exceptions -emit-llvm %s -o %t-cir.ll |
| 4 |
// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s |
| 5 |
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fexceptions -fcxx-exceptions -emit-llvm %s -o %t.ll |
| 6 |
// RUN: FileCheck --input-file=%t.ll %s --check-prefix=OGCG |
| 7 |
|
| 8 |
const int& test_cond_throw_false(bool flag) { |
| 9 |
const int a = 10; |
| 10 |
return flag ? a : throw 0; |
| 11 |
} |
| 12 |
|
| 13 |
// CIR-LABEL: cir.func{{.*}} @_Z21test_cond_throw_falseb( |
| 14 |
// CIR: %[[FLAG:.*]] = cir.alloca !cir.bool, !cir.ptr<!cir.bool>, ["flag", init] |
| 15 |
// CIR: %[[A:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["a", init, const] |
| 16 |
// CIR: %[[TEN:.*]] = cir.const #cir.int<10> : !s32i |
| 17 |
// CIR: cir.store{{.*}} %[[TEN]], %[[A]] : !s32i, !cir.ptr<!s32i> |
| 18 |
// CIR: %[[FLAG_VAL:.*]] = cir.load{{.*}} %[[FLAG]] : !cir.ptr<!cir.bool>, !cir.bool |
| 19 |
// CIR: %[[RESULT:.*]] = cir.ternary(%[[FLAG_VAL]], true { |
| 20 |
// CIR: cir.yield %[[A]] : !cir.ptr<!s32i> |
| 21 |
// CIR: }, false { |
| 22 |
// CIR: %[[EXCEPTION:.*]] = cir.alloc.exception{{.*}} -> !cir.ptr<!s32i> |
| 23 |
// CIR: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i |
| 24 |
// CIR: cir.store{{.*}} %[[ZERO]], %[[EXCEPTION]] : !s32i, !cir.ptr<!s32i> |
| 25 |
// CIR: cir.throw %[[EXCEPTION]] : !cir.ptr<!s32i>, @_ZTIi |
| 26 |
// CIR: cir.unreachable |
| 27 |
// CIR: }) : (!cir.bool) -> !cir.ptr<!s32i> |
| 28 |
|
| 29 |
// LLVM-LABEL: define{{.*}} ptr @_Z21test_cond_throw_falseb( |
| 30 |
// LLVM: %[[FLAG_ALLOCA:.*]] = alloca i8 |
| 31 |
// LLVM: %[[RET_ALLOCA:.*]] = alloca ptr |
| 32 |
// LLVM: %[[A_ALLOCA:.*]] = alloca i32 |
| 33 |
// LLVM: %[[ZEXT:.*]] = zext i1 %{{.*}} to i8 |
| 34 |
// LLVM: store i8 %[[ZEXT]], ptr %[[FLAG_ALLOCA]] |
| 35 |
// LLVM: store i32 10, ptr %[[A_ALLOCA]] |
| 36 |
// LLVM: %[[LOAD:.*]] = load i8, ptr %[[FLAG_ALLOCA]] |
| 37 |
// LLVM: %[[BOOL:.*]] = trunc i8 %[[LOAD]] to i1 |
| 38 |
// LLVM: br i1 %[[BOOL]], label %[[TRUE_BB:.*]], label %[[FALSE_BB:.*]] |
| 39 |
// LLVM: [[TRUE_BB]]: |
| 40 |
// LLVM: br label %[[PHI_BB:.*]] |
| 41 |
// LLVM: [[FALSE_BB]]: |
| 42 |
// LLVM: %[[EXC:.*]] = call{{.*}} ptr @__cxa_allocate_exception |
| 43 |
// LLVM: store i32 0, ptr %[[EXC]] |
| 44 |
// LLVM: call void @__cxa_throw(ptr %[[EXC]], ptr @_ZTIi |
| 45 |
// LLVM: unreachable |
| 46 |
// LLVM: [[PHI_BB]]: |
| 47 |
// LLVM: %[[PHI:.*]] = phi ptr [ %[[A_ALLOCA]], %[[TRUE_BB]] ] |
| 48 |
// LLVM: br label %[[CONT_BB:.*]] |
| 49 |
// LLVM: [[CONT_BB]]: |
| 50 |
// LLVM: store ptr %[[A_ALLOCA]], ptr %[[RET_ALLOCA]] |
| 51 |
// LLVM: %[[RET:.*]] = load ptr, ptr %[[RET_ALLOCA]] |
| 52 |
// LLVM: ret ptr %[[RET]] |
| 53 |
|
| 54 |
// OGCG-LABEL: define{{.*}} ptr @_Z21test_cond_throw_falseb( |
| 55 |
// OGCG: %{{.*}} = alloca i8 |
| 56 |
// OGCG: %[[A:.*]] = alloca i32 |
| 57 |
// OGCG: store i32 10, ptr %[[A]] |
| 58 |
// OGCG: %{{.*}} = load i8, ptr %{{.*}} |
| 59 |
// OGCG: %[[BOOL:.*]] = trunc i8 %{{.*}} to i1 |
| 60 |
// OGCG: br i1 %[[BOOL]], label %[[TRUE_BB:.*]], label %[[FALSE_BB:.*]] |
| 61 |
// OGCG: [[TRUE_BB]]: |
| 62 |
// OGCG: br label %[[END:.*]] |
| 63 |
// OGCG: [[FALSE_BB]]: |
| 64 |
// OGCG: %{{.*}} = call{{.*}} ptr @__cxa_allocate_exception |
| 65 |
// OGCG: store i32 0, ptr %{{.*}} |
| 66 |
// OGCG: call void @__cxa_throw(ptr %{{.*}}, ptr @_ZTIi |
| 67 |
// OGCG: unreachable |
| 68 |
// OGCG: [[END]]: |
| 69 |
// OGCG: ret ptr %[[A]] |
| 70 |
|
| 71 |
const int& test_cond_throw_true(bool flag) { |
| 72 |
const int a = 10; |
| 73 |
return flag ? throw 0 : a; |
| 74 |
} |
| 75 |
|
| 76 |
// CIR-LABEL: cir.func{{.*}} @_Z20test_cond_throw_trueb( |
| 77 |
// CIR: %[[FLAG:.*]] = cir.alloca !cir.bool, !cir.ptr<!cir.bool>, ["flag", init] |
| 78 |
// CIR: %[[A:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["a", init, const] |
| 79 |
// CIR: %[[TEN:.*]] = cir.const #cir.int<10> : !s32i |
| 80 |
// CIR: cir.store{{.*}} %[[TEN]], %[[A]] : !s32i, !cir.ptr<!s32i> |
| 81 |
// CIR: %[[FLAG_VAL:.*]] = cir.load{{.*}} %[[FLAG]] : !cir.ptr<!cir.bool>, !cir.bool |
| 82 |
// CIR: %[[RESULT:.*]] = cir.ternary(%[[FLAG_VAL]], true { |
| 83 |
// CIR: %[[EXCEPTION:.*]] = cir.alloc.exception{{.*}} -> !cir.ptr<!s32i> |
| 84 |
// CIR: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i |
| 85 |
// CIR: cir.store{{.*}} %[[ZERO]], %[[EXCEPTION]] : !s32i, !cir.ptr<!s32i> |
| 86 |
// CIR: cir.throw %[[EXCEPTION]] : !cir.ptr<!s32i>, @_ZTIi |
| 87 |
// CIR: cir.unreachable |
| 88 |
// CIR: }, false { |
| 89 |
// CIR: cir.yield %[[A]] : !cir.ptr<!s32i> |
| 90 |
// CIR: }) : (!cir.bool) -> !cir.ptr<!s32i> |
| 91 |
|
| 92 |
// LLVM-LABEL: define{{.*}} ptr @_Z20test_cond_throw_trueb( |
| 93 |
// LLVM: %[[FLAG_ALLOCA:.*]] = alloca i8 |
| 94 |
// LLVM: %[[RET_ALLOCA:.*]] = alloca ptr |
| 95 |
// LLVM: %[[A_ALLOCA:.*]] = alloca i32 |
| 96 |
// LLVM: %[[ZEXT:.*]] = zext i1 %{{.*}} to i8 |
| 97 |
// LLVM: store i8 %[[ZEXT]], ptr %[[FLAG_ALLOCA]] |
| 98 |
// LLVM: store i32 10, ptr %[[A_ALLOCA]] |
| 99 |
// LLVM: %[[LOAD:.*]] = load i8, ptr %[[FLAG_ALLOCA]] |
| 100 |
// LLVM: %[[BOOL:.*]] = trunc i8 %[[LOAD]] to i1 |
| 101 |
// LLVM: br i1 %[[BOOL]], label %[[TRUE_BB:.*]], label %[[FALSE_BB:.*]] |
| 102 |
// LLVM: [[TRUE_BB]]: |
| 103 |
// LLVM: %[[EXC:.*]] = call{{.*}} ptr @__cxa_allocate_exception |
| 104 |
// LLVM: store i32 0, ptr %[[EXC]] |
| 105 |
// LLVM: call void @__cxa_throw(ptr %[[EXC]], ptr @_ZTIi |
| 106 |
// LLVM: unreachable |
| 107 |
// LLVM: [[FALSE_BB]]: |
| 108 |
// LLVM: br label %[[PHI_BB:.*]] |
| 109 |
// LLVM: [[PHI_BB]]: |
| 110 |
// LLVM: %[[PHI:.*]] = phi ptr [ %[[A_ALLOCA]], %[[FALSE_BB]] ] |
| 111 |
// LLVM: br label %[[CONT_BB:.*]] |
| 112 |
// LLVM: [[CONT_BB]]: |
| 113 |
// LLVM: store ptr %[[A_ALLOCA]], ptr %[[RET_ALLOCA]] |
| 114 |
// LLVM: %[[RET:.*]] = load ptr, ptr %[[RET_ALLOCA]] |
| 115 |
// LLVM: ret ptr %[[RET]] |
| 116 |
|
| 117 |
// OGCG-LABEL: define{{.*}} ptr @_Z20test_cond_throw_trueb( |
| 118 |
// OGCG: %{{.*}} = alloca i8 |
| 119 |
// OGCG: %[[A:.*]] = alloca i32 |
| 120 |
// OGCG: store i32 10, ptr %[[A]] |
| 121 |
// OGCG: %{{.*}} = load i8, ptr %{{.*}} |
| 122 |
// OGCG: %[[BOOL:.*]] = trunc i8 %{{.*}} to i1 |
| 123 |
// OGCG: br i1 %[[BOOL]], label %[[TRUE_BB:.*]], label %[[FALSE_BB:.*]] |
| 124 |
// OGCG: [[TRUE_BB]]: |
| 125 |
// OGCG: %{{.*}} = call{{.*}} ptr @__cxa_allocate_exception |
| 126 |
// OGCG: store i32 0, ptr %{{.*}} |
| 127 |
// OGCG: call void @__cxa_throw(ptr %{{.*}}, ptr @_ZTIi |
| 128 |
// OGCG: unreachable |
| 129 |
// OGCG: [[FALSE_BB]]: |
| 130 |
// OGCG: br label %[[END:.*]] |
| 131 |
// OGCG: [[END]]: |
| 132 |
// OGCG: ret ptr %[[A]] |
| 133 |
|
| 134 |
// Test constant folding with throw - compile-time true condition, dead throw in false branch |
| 135 |
const int& test_cond_const_true_throw_false() { |
| 136 |
const int a = 20; |
| 137 |
return true ? a : throw 0; |
| 138 |
} |
| 139 |
|
| 140 |
// CIR-LABEL: cir.func{{.*}} @_Z32test_cond_const_true_throw_falsev( |
| 141 |
// CIR: %[[A:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["a", init, const] |
| 142 |
// CIR: %[[TWENTY:.*]] = cir.const #cir.int<20> : !s32i |
| 143 |
// CIR: cir.store{{.*}} %[[TWENTY]], %[[A]] : !s32i, !cir.ptr<!s32i> |
| 144 |
// CIR-NOT: cir.ternary |
| 145 |
// CIR-NOT: cir.throw |
| 146 |
// CIR: cir.store %[[A]] |
| 147 |
// CIR: %[[RET:.*]] = cir.load |
| 148 |
// CIR: cir.return %[[RET]] : !cir.ptr<!s32i> |
| 149 |
|
| 150 |
// LLVM-LABEL: define{{.*}} ptr @_Z32test_cond_const_true_throw_falsev( |
| 151 |
// LLVM: %[[A:.*]] = alloca i32 |
| 152 |
// LLVM: store i32 20, ptr %[[A]] |
| 153 |
// LLVM-NOT: br i1 |
| 154 |
// LLVM-NOT: __cxa_throw |
| 155 |
// LLVM: store ptr %[[A]] |
| 156 |
// LLVM: %[[RET:.*]] = load ptr |
| 157 |
// LLVM: ret ptr %[[RET]] |
| 158 |
|
| 159 |
// OGCG-LABEL: define{{.*}} ptr @_Z32test_cond_const_true_throw_falsev( |
| 160 |
// OGCG: %[[A:.*]] = alloca i32 |
| 161 |
// OGCG: store i32 20, ptr %[[A]] |
| 162 |
// OGCG-NOT: br i1 |
| 163 |
// OGCG-NOT: __cxa_throw |
| 164 |
// OGCG: ret ptr %[[A]] |
| 165 |
|
| 166 |
// Test constant folding with throw - compile-time false condition, dead throw in true branch |
| 167 |
const int& test_cond_const_false_throw_true() { |
| 168 |
const int a = 30; |
| 169 |
return false ? throw 0 : a; |
| 170 |
} |
| 171 |
|
| 172 |
// CIR-LABEL: cir.func{{.*}} @_Z32test_cond_const_false_throw_truev( |
| 173 |
// CIR: %[[A:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["a", init, const] |
| 174 |
// CIR: %[[THIRTY:.*]] = cir.const #cir.int<30> : !s32i |
| 175 |
// CIR: cir.store{{.*}} %[[THIRTY]], %[[A]] : !s32i, !cir.ptr<!s32i> |
| 176 |
// CIR-NOT: cir.ternary |
| 177 |
// CIR-NOT: cir.throw |
| 178 |
// CIR: cir.store %[[A]] |
| 179 |
// CIR: %[[RET:.*]] = cir.load |
| 180 |
// CIR: cir.return %[[RET]] : !cir.ptr<!s32i> |
| 181 |
|
| 182 |
// LLVM-LABEL: define{{.*}} ptr @_Z32test_cond_const_false_throw_truev( |
| 183 |
// LLVM: %[[A:.*]] = alloca i32 |
| 184 |
// LLVM: store i32 30, ptr %[[A]] |
| 185 |
// LLVM-NOT: br i1 |
| 186 |
// LLVM-NOT: __cxa_throw |
| 187 |
// LLVM: store ptr %[[A]] |
| 188 |
// LLVM: %[[RET:.*]] = load ptr |
| 189 |
// LLVM: ret ptr %[[RET]] |
| 190 |
|
| 191 |
// OGCG-LABEL: define{{.*}} ptr @_Z32test_cond_const_false_throw_truev( |
| 192 |
// OGCG: %[[A:.*]] = alloca i32 |
| 193 |
// OGCG: store i32 30, ptr %[[A]] |
| 194 |
// OGCG-NOT: br i1 |
| 195 |
// OGCG-NOT: __cxa_throw |
| 196 |
// OGCG: ret ptr %[[A]] |
| 197 |
|
| 198 |
const int &test_cond_const_true_throw_true() { |
| 199 |
const int a = 30; |
| 200 |
return true ? throw 0 : a; |
| 201 |
} |
| 202 |
|
| 203 |
// CIR-LABEL: cir.func{{.*}} @_Z31test_cond_const_true_throw_truev( |
| 204 |
// CIR: %[[RET_ADDR:.*]] = cir.alloca !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>, ["__retval"] |
| 205 |
// CIR: %[[A_ADDR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["a", init, const] |
| 206 |
// CIR: %[[CONST_30:.*]] = cir.const #cir.int<30> : !s32i |
| 207 |
// CIR: cir.store{{.*}} %[[CONST_30]], %[[A_ADDR]] : !s32i, !cir.ptr<!s32i> |
| 208 |
// CIR: %[[EXCEPTION:.*]] = cir.alloc.exception 4 -> !cir.ptr<!s32i> |
| 209 |
// CIR: %[[CONST_0:.*]] = cir.const #cir.int<0> : !s32i |
| 210 |
// CIR: cir.store{{.*}} %[[CONST_0]], %[[EXCEPTION]] : !s32i, !cir.ptr<!s32i> |
| 211 |
// CIR: cir.throw %[[EXCEPTION]] : !cir.ptr<!s32i>, @_ZTIi |
| 212 |
// CIR: cir.unreachable |
| 213 |
// CIR: ^[[NO_PRED_LABEL:.*]]: |
| 214 |
// CIR: %[[CONST_NULL:.*]] = cir.const #cir.ptr<null> : !cir.ptr<!s32i> |
| 215 |
// CIR: cir.store %[[CONST_NULL]], %[[RET_ADDR]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>> |
| 216 |
// CIR: %[[TMP_RET:.*]] = cir.load %[[RET_ADDR]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i> |
| 217 |
// CIR: cir.return %[[TMP_RET]] : !cir.ptr<!s32i> |
| 218 |
|
| 219 |
// LLVM-LABEL: define{{.*}} ptr @_Z31test_cond_const_true_throw_truev( |
| 220 |
// LLVM: %[[RET_ADDR:.*]] = alloca ptr, i64 1, align 8 |
| 221 |
// LLVM: %[[A_ADDR:.*]] = alloca i32, i64 1, align 4 |
| 222 |
// LLVM: store i32 30, ptr %[[A_ADDR]], align 4 |
| 223 |
// LLVM: %[[EXCEPTION:.*]] = call ptr @__cxa_allocate_exception(i64 4) |
| 224 |
// LLVM: store i32 0, ptr %[[EXCEPTION]], align 16 |
| 225 |
// LLVM: call void @__cxa_throw(ptr %[[EXCEPTION]], ptr @_ZTIi, ptr null) |
| 226 |
// LLVM: unreachable |
| 227 |
// LLVM: [[NO_PRED_LABEL:.*]]: |
| 228 |
// LLVM: store ptr null, ptr %[[RET_ADDR]], align 8 |
| 229 |
// LLVM: %[[TMP_RET:.*]] = load ptr, ptr %[[RET_ADDR]], align 8 |
| 230 |
// LLVM: ret ptr %[[TMP_RET]] |
| 231 |
|
| 232 |
// OGCG-LABEL: define{{.*}} ptr @_Z31test_cond_const_true_throw_truev( |
| 233 |
// OGCG: %[[A_ADDR:.*]] = alloca i32, align 4 |
| 234 |
// OGCG: store i32 30, ptr %[[A_ADDR]], align 4 |
| 235 |
// OGCG: %[[EXCEPTION:.*]] = call ptr @__cxa_allocate_exception(i64 4) |
| 236 |
// OGCG: store i32 0, ptr %[[EXCEPTION]], align 16 |
| 237 |
// OGCG: call void @__cxa_throw(ptr %[[EXCEPTION]], ptr @_ZTIi, ptr null) |
| 238 |
// OGCG: unreachable |
| 239 |
// OGCG: [[NO_PRED_LABEL:.*]]: |
| 240 |
// OGCG: ret ptr [[UNDEF:.*]] |