brintos

brintos / llvm-project-archived public Read only

0
0
Text · 9.9 KiB · 74168c1 Raw
241 lines · cpp
1// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fexceptions -fcxx-exceptions -emit-cir %s -o %t.cir2// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s3// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fexceptions -fcxx-exceptions -emit-llvm %s -o %t-cir.ll4// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s5// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fexceptions -fcxx-exceptions -emit-llvm %s -o %t.ll6// RUN: FileCheck --input-file=%t.ll %s --check-prefix=OGCG7 8const 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> : !s32i17// CIR: cir.store{{.*}} %[[TEN]], %[[A]] : !s32i, !cir.ptr<!s32i>18// CIR: %[[FLAG_VAL:.*]] = cir.load{{.*}} %[[FLAG]] : !cir.ptr<!cir.bool>, !cir.bool19// 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> : !s32i24// CIR:   cir.store{{.*}} %[[ZERO]], %[[EXCEPTION]] : !s32i, !cir.ptr<!s32i>25// CIR:   cir.throw %[[EXCEPTION]] : !cir.ptr<!s32i>, @_ZTIi26// CIR:   cir.unreachable27// CIR: }) : (!cir.bool) -> !cir.ptr<!s32i>28 29// LLVM-LABEL: define{{.*}} ptr @_Z21test_cond_throw_falseb(30// LLVM: %[[FLAG_ALLOCA:.*]] = alloca i831// LLVM: %[[RET_ALLOCA:.*]] = alloca ptr32// LLVM: %[[A_ALLOCA:.*]] = alloca i3233// LLVM: %[[ZEXT:.*]] = zext i1 %{{.*}} to i834// 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 i138// 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_exception43// LLVM:   store i32 0, ptr %[[EXC]]44// LLVM:   call void @__cxa_throw(ptr %[[EXC]], ptr @_ZTIi45// LLVM:   unreachable46// 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 i856// OGCG: %[[A:.*]] = alloca i3257// OGCG: store i32 10, ptr %[[A]]58// OGCG: %{{.*}} = load i8, ptr %{{.*}}59// OGCG: %[[BOOL:.*]] = trunc i8 %{{.*}} to i160// 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_exception65// OGCG:   store i32 0, ptr %{{.*}}66// OGCG:   call void @__cxa_throw(ptr %{{.*}}, ptr @_ZTIi67// OGCG:   unreachable68// OGCG: [[END]]:69// OGCG:   ret ptr %[[A]]70 71const 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> : !s32i80// CIR: cir.store{{.*}} %[[TEN]], %[[A]] : !s32i, !cir.ptr<!s32i>81// CIR: %[[FLAG_VAL:.*]] = cir.load{{.*}} %[[FLAG]] : !cir.ptr<!cir.bool>, !cir.bool82// CIR: %[[RESULT:.*]] = cir.ternary(%[[FLAG_VAL]], true {83// CIR:   %[[EXCEPTION:.*]] = cir.alloc.exception{{.*}} -> !cir.ptr<!s32i>84// CIR:   %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i85// CIR:   cir.store{{.*}} %[[ZERO]], %[[EXCEPTION]] : !s32i, !cir.ptr<!s32i>86// CIR:   cir.throw %[[EXCEPTION]] : !cir.ptr<!s32i>, @_ZTIi87// CIR:   cir.unreachable88// 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 i894// LLVM: %[[RET_ALLOCA:.*]] = alloca ptr95// LLVM: %[[A_ALLOCA:.*]] = alloca i3296// LLVM: %[[ZEXT:.*]] = zext i1 %{{.*}} to i897// 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 i1101// LLVM: br i1 %[[BOOL]], label %[[TRUE_BB:.*]], label %[[FALSE_BB:.*]]102// LLVM: [[TRUE_BB]]:103// LLVM:   %[[EXC:.*]] = call{{.*}} ptr @__cxa_allocate_exception104// LLVM:   store i32 0, ptr %[[EXC]]105// LLVM:   call void @__cxa_throw(ptr %[[EXC]], ptr @_ZTIi106// LLVM:   unreachable107// 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 i8119// OGCG: %[[A:.*]] = alloca i32120// OGCG: store i32 10, ptr %[[A]]121// OGCG: %{{.*}} = load i8, ptr %{{.*}}122// OGCG: %[[BOOL:.*]] = trunc i8 %{{.*}} to i1123// OGCG: br i1 %[[BOOL]], label %[[TRUE_BB:.*]], label %[[FALSE_BB:.*]]124// OGCG: [[TRUE_BB]]:125// OGCG:   %{{.*}} = call{{.*}} ptr @__cxa_allocate_exception126// OGCG:   store i32 0, ptr %{{.*}}127// OGCG:   call void @__cxa_throw(ptr %{{.*}}, ptr @_ZTIi128// OGCG:   unreachable129// 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 branch135const 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> : !s32i143// CIR: cir.store{{.*}} %[[TWENTY]], %[[A]] : !s32i, !cir.ptr<!s32i>144// CIR-NOT: cir.ternary145// CIR-NOT: cir.throw146// CIR: cir.store %[[A]]147// CIR: %[[RET:.*]] = cir.load148// CIR: cir.return %[[RET]] : !cir.ptr<!s32i>149 150// LLVM-LABEL: define{{.*}} ptr @_Z32test_cond_const_true_throw_falsev(151// LLVM: %[[A:.*]] = alloca i32152// LLVM: store i32 20, ptr %[[A]]153// LLVM-NOT: br i1154// LLVM-NOT: __cxa_throw155// LLVM: store ptr %[[A]]156// LLVM: %[[RET:.*]] = load ptr157// LLVM: ret ptr %[[RET]]158 159// OGCG-LABEL: define{{.*}} ptr @_Z32test_cond_const_true_throw_falsev(160// OGCG: %[[A:.*]] = alloca i32161// OGCG: store i32 20, ptr %[[A]]162// OGCG-NOT: br i1163// OGCG-NOT: __cxa_throw164// OGCG: ret ptr %[[A]]165 166// Test constant folding with throw - compile-time false condition, dead throw in true branch167const 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> : !s32i175// CIR: cir.store{{.*}} %[[THIRTY]], %[[A]] : !s32i, !cir.ptr<!s32i>176// CIR-NOT: cir.ternary177// CIR-NOT: cir.throw178// CIR: cir.store %[[A]]179// CIR: %[[RET:.*]] = cir.load180// CIR: cir.return %[[RET]] : !cir.ptr<!s32i>181 182// LLVM-LABEL: define{{.*}} ptr @_Z32test_cond_const_false_throw_truev(183// LLVM: %[[A:.*]] = alloca i32184// LLVM: store i32 30, ptr %[[A]]185// LLVM-NOT: br i1186// LLVM-NOT: __cxa_throw187// LLVM: store ptr %[[A]]188// LLVM: %[[RET:.*]] = load ptr189// LLVM: ret ptr %[[RET]]190 191// OGCG-LABEL: define{{.*}} ptr @_Z32test_cond_const_false_throw_truev(192// OGCG: %[[A:.*]] = alloca i32193// OGCG: store i32 30, ptr %[[A]]194// OGCG-NOT: br i1195// OGCG-NOT: __cxa_throw196// OGCG: ret ptr %[[A]]197 198const 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> : !s32i207// 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> : !s32i210// CIR:  cir.store{{.*}} %[[CONST_0]], %[[EXCEPTION]] : !s32i, !cir.ptr<!s32i>211// CIR:  cir.throw %[[EXCEPTION]] : !cir.ptr<!s32i>, @_ZTIi212// CIR:  cir.unreachable213// 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 8221// LLVM:  %[[A_ADDR:.*]] = alloca i32, i64 1, align 4222// LLVM:  store i32 30, ptr %[[A_ADDR]], align 4223// LLVM:  %[[EXCEPTION:.*]] = call ptr @__cxa_allocate_exception(i64 4)224// LLVM:  store i32 0, ptr %[[EXCEPTION]], align 16225// LLVM:  call void @__cxa_throw(ptr %[[EXCEPTION]], ptr @_ZTIi, ptr null)226// LLVM:  unreachable227// LLVM: [[NO_PRED_LABEL:.*]]:228// LLVM:  store ptr null, ptr %[[RET_ADDR]], align 8229// LLVM:  %[[TMP_RET:.*]] = load ptr, ptr %[[RET_ADDR]], align 8230// LLVM:  ret ptr %[[TMP_RET]]231 232// OGCG-LABEL: define{{.*}} ptr @_Z31test_cond_const_true_throw_truev(233// OGCG:  %[[A_ADDR:.*]] = alloca i32, align 4234// OGCG:  store i32 30, ptr %[[A_ADDR]], align 4235// OGCG:  %[[EXCEPTION:.*]] = call ptr @__cxa_allocate_exception(i64 4)236// OGCG:  store i32 0, ptr %[[EXCEPTION]], align 16237// OGCG:  call void @__cxa_throw(ptr %[[EXCEPTION]], ptr @_ZTIi, ptr null)238// OGCG:  unreachable239// OGCG: [[NO_PRED_LABEL:.*]]:240// OGCG:  ret ptr [[UNDEF:.*]]241