brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.3 KiB · 844d4df Raw
167 lines · cpp
1// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir2// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR3// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-llvm %s -o %t-cir.ll4// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM5 6unsigned char cxxstaticcast_0(unsigned int x) {7  return static_cast<unsigned char>(x);8}9 10// CIR: cir.func{{.*}} @_Z15cxxstaticcast_0j11// CIR:    %[[XPTR:[0-9]+]] = cir.alloca !u32i, !cir.ptr<!u32i>, ["x", init] {alignment = 4 : i64}12// CIR:    %[[RV:[0-9]+]] = cir.alloca !u8i, !cir.ptr<!u8i>, ["__retval"] {alignment = 1 : i64}13// CIR:    cir.store %arg0, %[[XPTR]] : !u32i, !cir.ptr<!u32i>14// CIR:    %[[XVAL:[0-9]+]] = cir.load{{.*}} %[[XPTR]] : !cir.ptr<!u32i>, !u32i15// CIR:    %[[CASTED:[0-9]+]] = cir.cast integral %[[XVAL]] : !u32i -> !u8i16// CIR:    cir.store %[[CASTED]], %[[RV]] : !u8i, !cir.ptr<!u8i>17// CIR:    %[[R:[0-9]+]] = cir.load{{.*}} %1 : !cir.ptr<!u8i>, !u8i18// CIR:    cir.return %[[R]] : !u8i19// CIR:  }20 21// LLVM: define{{.*}} i8 @_Z15cxxstaticcast_0j(i32 %{{[0-9]+}})22// LLVM: %[[LOAD:[0-9]+]] = load i32, ptr %{{[0-9]+}}, align 423// LLVM: %[[TRUNC:[0-9]+]] = trunc i32 %[[LOAD]] to i824// LLVM: store i8 %[[TRUNC]], ptr %[[RV:[0-9]+]], align 125// LLVM: %[[R:[0-9]+]] = load i8, ptr %[[RV]], align 126// LLVM: ret i8 %[[R]]27 28int cStyleCasts_0(unsigned x1, int x2, float x3, short x4, double x5) {29// CIR: cir.func{{.*}} @_Z13cStyleCasts_0jifsd30// LLVM: define{{.*}} i32 @_Z13cStyleCasts_0jifsd31 32  char a = (char)x1; // truncate33  // CIR: %{{[0-9]+}} = cir.cast integral %{{[0-9]+}} : !u32i -> !s8i34  // LLVM: %{{[0-9]+}} = trunc i32 %{{[0-9]+}} to i835 36  short b = (short)x2; // truncate with sign37  // CIR: %{{[0-9]+}} = cir.cast integral %{{[0-9]+}} : !s32i -> !s16i38  // LLVM: %{{[0-9]+}} = trunc i32 %{{[0-9]+}} to i1639 40  long long c = (long long)x1; // zero extend41  // CIR: %{{[0-9]+}} = cir.cast integral %{{[0-9]+}} : !u32i -> !s64i42  // LLVM: %{{[0-9]+}} = zext i32 %{{[0-9]+}} to i6443 44  long long d = (long long)x2; // sign extend45  // CIR: %{{[0-9]+}} = cir.cast integral %{{[0-9]+}} : !s32i -> !s64i46  // LLVM: %{{[0-9]+}} = sext i32 %{{[0-9]+}} to i6447 48  unsigned ui = (unsigned)x2; // sign drop49  // CIR: %{{[0-9]+}} = cir.cast integral %{{[0-9]+}} : !s32i -> !u32i50 51  int si = (int)x1; // sign add52  // CIR: %{{[0-9]+}} = cir.cast integral %{{[0-9]+}} : !u32i -> !s32i53 54  bool ib;55  int bi = (int)ib; // bool to int56  // CIR: %{{[0-9]+}} = cir.cast bool_to_int %{{[0-9]+}} : !cir.bool -> !s32i57  // LLVM: %{{[0-9]+}} = zext i1 %{{[0-9]+}} to i3258 59  bool b2 = x2; // int to bool60  // CIR: %{{[0-9]+}} = cir.cast int_to_bool %{{[0-9]+}} : !s32i -> !cir.bool61  // LLVM: %[[INTTOBOOL:[0-9]+]]  = icmp ne i32 %{{[0-9]+}}, 062  // LLVM: zext i1 %[[INTTOBOOL]] to i863 64  void *p;65  bool b3 = p; // ptr to bool66  // CIR: %{{[0-9]+}} = cir.cast ptr_to_bool %{{[0-9]+}} : !cir.ptr<!void> -> !cir.bool67  // LLVM: %[[PTRTOBOOL:[0-9]+]]  = icmp ne ptr %{{[0-9]+}}, null68  // LLVM: zext i1 %[[PTRTOBOOL]] to i869 70  float f;71  bool b4 = f; // float to bool72  // CIR: %{{[0-9]+}} = cir.cast float_to_bool %{{[0-9]+}} : !cir.float -> !cir.bool73  // LLVM: %{{[0-9]+}} = fcmp une float %{{[0-9]+}}, 0.000000e+0074  // LLVM: %{{[0-9]+}} = zext i1 %{{[0-9]+}} to i875 76  double d2 = f; // float to double77  // CIR: %{{[0-9]+}} = cir.cast floating %{{[0-9]+}} : !cir.float -> !cir.double78  // LLVM: %{{[0-9]+}} = fpext float %{{[0-9]+}} to double79 80  f = d2; // double to float81  // CIR: %{{[0-9]+}} = cir.cast floating %{{[0-9]+}} : !cir.double -> !cir.float82  // LLVM: %{{[0-9]+}} = fptrunc double %{{[0-9]+}} to float83 84  return 0;85}86 87bool cptr(void *d) {88  bool x = d;89  return x;90}91 92// CIR: cir.func{{.*}} @_Z4cptrPv(%arg0: !cir.ptr<!void>93// CIR:   %[[DPTR:[0-9]+]] = cir.alloca !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>, ["d", init] {alignment = 8 : i64}94 95// CIR:   %[[DVAL:[0-9]+]] = cir.load{{.*}} %[[DPTR]] : !cir.ptr<!cir.ptr<!void>>, !cir.ptr<!void>96// CIR:   %{{[0-9]+}} = cir.cast ptr_to_bool %[[DVAL]] : !cir.ptr<!void> -> !cir.bool97 98// LLVM-LABEL: define{{.*}} i1 @_Z4cptrPv(ptr %0)99// LLVM:         %[[ARG_STORAGE:.*]] = alloca ptr, i64 1100// LLVM:         %[[RETVAL:.*]] = alloca i8, i64 1101// LLVM:         %[[X_STORAGE:.*]] = alloca i8, i64 1102// LLVM:         store ptr %0, ptr %[[ARG_STORAGE]]103// LLVM:         %[[LOADED_PTR:.*]] = load ptr, ptr %[[ARG_STORAGE]]104// LLVM:         %[[NULL_CHECK:.*]] = icmp ne ptr %[[LOADED_PTR]], null105// LLVM:         ret i1106 107void should_not_cast() {108  unsigned x1;109  unsigned uu = (unsigned)x1; // identity110 111  bool x2;112  bool ib = (bool)x2; // identity113 114  (void) ib; // void cast115}116 117// CIR:     cir.func{{.*}} @_Z15should_not_castv118// CIR-NOT:   cir.cast119// CIR:     cir.return120 121typedef int vi4 __attribute__((vector_size(16)));122typedef double vd2 __attribute__((vector_size(16)));123 124void bitcast() {125  vd2 a = {};126  vi4 b = (vi4)a;127}128 129// CIR: %[[D_VEC:.*]] = cir.load{{.*}} {{.*}} : !cir.ptr<!cir.vector<2 x !cir.double>>, !cir.vector<2 x !cir.double>130// CIR: %[[I_VEC:.*]] = cir.cast bitcast %[[D_VEC]] : !cir.vector<2 x !cir.double> -> !cir.vector<4 x !s32i>131 132// LLVM: %[[D_VEC:.*]] = load <2 x double>, ptr {{.*}}, align 16133// LLVM: %[[I_VEC:.*]] = bitcast <2 x double> %[[D_VEC]] to <4 x i32>134 135void f(long int start) {136  void *p = (void*)start;137}138// CIR: %[[L:.*]] = cir.load {{.*}} : !cir.ptr<!s64i>, !s64i139// CIR: %[[MID:.*]] = cir.cast integral %[[L]] : !s64i -> !u64i140// CIR:          cir.cast int_to_ptr %[[MID]] : !u64i -> !cir.ptr<!void>141 142// LLVM-LABEL: define{{.*}} void @_Z1fl(i64 %0)143// LLVM: %[[ADDR:.*]] = alloca i64, i64 1, align 8144// LLVM: %[[PADDR:.*]] = alloca ptr, i64 1, align 8145// LLVM: store i64 %0, ptr %[[ADDR]], align 8146// LLVM: %[[L:.*]] = load i64, ptr %[[ADDR]], align 8147// LLVM: %[[PTR:.*]] = inttoptr i64 %[[L]] to ptr148// LLVM: store ptr %[[PTR]], ptr %[[PADDR]], align 8149// LLVM: ret void150 151struct A { int x; };152 153void int_cast(long ptr) {154  ((A *)ptr)->x = 0;155}156// CIR: cir.cast int_to_ptr {{.*}} : !u64i -> !cir.ptr<!rec_A>157// LLVM: inttoptr {{.*}} to ptr158 159void null_cast(long) {160  *(int *)0 = 0;161  ((A *)0)->x = 0;162}163// CIR:    %[[NULLPTR:.*]] = cir.const #cir.ptr<null> : !cir.ptr<!s32i>164// CIR:    cir.store{{.*}} %{{.*}}, %[[NULLPTR]] : !s32i, !cir.ptr<!s32i>165// CIR:    %[[NULLPTR_A:.*]] = cir.const #cir.ptr<null> : !cir.ptr<!rec_A>166// CIR:    %[[A_X:.*]] = cir.get_member %[[NULLPTR_A]][0] {name = "x"} : !cir.ptr<!rec_A> -> !cir.ptr<!s32i>167