| 1 |
// RUN: %clang_cc1 -std=c++17 -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 -Wno-unused-value -fclangir -emit-llvm %s -o %t-cir.ll |
| 4 |
// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM |
| 5 |
|
| 6 |
unsigned char cxxstaticcast_0(unsigned int x) { |
| 7 |
return static_cast<unsigned char>(x); |
| 8 |
} |
| 9 |
|
| 10 |
// CIR: cir.func{{.*}} @_Z15cxxstaticcast_0j |
| 11 |
// 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>, !u32i |
| 15 |
// CIR: %[[CASTED:[0-9]+]] = cir.cast integral %[[XVAL]] : !u32i -> !u8i |
| 16 |
// CIR: cir.store %[[CASTED]], %[[RV]] : !u8i, !cir.ptr<!u8i> |
| 17 |
// CIR: %[[R:[0-9]+]] = cir.load{{.*}} %1 : !cir.ptr<!u8i>, !u8i |
| 18 |
// CIR: cir.return %[[R]] : !u8i |
| 19 |
// CIR: } |
| 20 |
|
| 21 |
// LLVM: define{{.*}} i8 @_Z15cxxstaticcast_0j(i32 %{{[0-9]+}}) |
| 22 |
// LLVM: %[[LOAD:[0-9]+]] = load i32, ptr %{{[0-9]+}}, align 4 |
| 23 |
// LLVM: %[[TRUNC:[0-9]+]] = trunc i32 %[[LOAD]] to i8 |
| 24 |
// LLVM: store i8 %[[TRUNC]], ptr %[[RV:[0-9]+]], align 1 |
| 25 |
// LLVM: %[[R:[0-9]+]] = load i8, ptr %[[RV]], align 1 |
| 26 |
// LLVM: ret i8 %[[R]] |
| 27 |
|
| 28 |
int cStyleCasts_0(unsigned x1, int x2, float x3, short x4, double x5) { |
| 29 |
// CIR: cir.func{{.*}} @_Z13cStyleCasts_0jifsd |
| 30 |
// LLVM: define{{.*}} i32 @_Z13cStyleCasts_0jifsd |
| 31 |
|
| 32 |
char a = (char)x1; // truncate |
| 33 |
// CIR: %{{[0-9]+}} = cir.cast integral %{{[0-9]+}} : !u32i -> !s8i |
| 34 |
// LLVM: %{{[0-9]+}} = trunc i32 %{{[0-9]+}} to i8 |
| 35 |
|
| 36 |
short b = (short)x2; // truncate with sign |
| 37 |
// CIR: %{{[0-9]+}} = cir.cast integral %{{[0-9]+}} : !s32i -> !s16i |
| 38 |
// LLVM: %{{[0-9]+}} = trunc i32 %{{[0-9]+}} to i16 |
| 39 |
|
| 40 |
long long c = (long long)x1; // zero extend |
| 41 |
// CIR: %{{[0-9]+}} = cir.cast integral %{{[0-9]+}} : !u32i -> !s64i |
| 42 |
// LLVM: %{{[0-9]+}} = zext i32 %{{[0-9]+}} to i64 |
| 43 |
|
| 44 |
long long d = (long long)x2; // sign extend |
| 45 |
// CIR: %{{[0-9]+}} = cir.cast integral %{{[0-9]+}} : !s32i -> !s64i |
| 46 |
// LLVM: %{{[0-9]+}} = sext i32 %{{[0-9]+}} to i64 |
| 47 |
|
| 48 |
unsigned ui = (unsigned)x2; // sign drop |
| 49 |
// CIR: %{{[0-9]+}} = cir.cast integral %{{[0-9]+}} : !s32i -> !u32i |
| 50 |
|
| 51 |
int si = (int)x1; // sign add |
| 52 |
// CIR: %{{[0-9]+}} = cir.cast integral %{{[0-9]+}} : !u32i -> !s32i |
| 53 |
|
| 54 |
bool ib; |
| 55 |
int bi = (int)ib; // bool to int |
| 56 |
// CIR: %{{[0-9]+}} = cir.cast bool_to_int %{{[0-9]+}} : !cir.bool -> !s32i |
| 57 |
// LLVM: %{{[0-9]+}} = zext i1 %{{[0-9]+}} to i32 |
| 58 |
|
| 59 |
bool b2 = x2; // int to bool |
| 60 |
// CIR: %{{[0-9]+}} = cir.cast int_to_bool %{{[0-9]+}} : !s32i -> !cir.bool |
| 61 |
// LLVM: %[[INTTOBOOL:[0-9]+]] = icmp ne i32 %{{[0-9]+}}, 0 |
| 62 |
// LLVM: zext i1 %[[INTTOBOOL]] to i8 |
| 63 |
|
| 64 |
void *p; |
| 65 |
bool b3 = p; // ptr to bool |
| 66 |
// CIR: %{{[0-9]+}} = cir.cast ptr_to_bool %{{[0-9]+}} : !cir.ptr<!void> -> !cir.bool |
| 67 |
// LLVM: %[[PTRTOBOOL:[0-9]+]] = icmp ne ptr %{{[0-9]+}}, null |
| 68 |
// LLVM: zext i1 %[[PTRTOBOOL]] to i8 |
| 69 |
|
| 70 |
float f; |
| 71 |
bool b4 = f; // float to bool |
| 72 |
// CIR: %{{[0-9]+}} = cir.cast float_to_bool %{{[0-9]+}} : !cir.float -> !cir.bool |
| 73 |
// LLVM: %{{[0-9]+}} = fcmp une float %{{[0-9]+}}, 0.000000e+00 |
| 74 |
// LLVM: %{{[0-9]+}} = zext i1 %{{[0-9]+}} to i8 |
| 75 |
|
| 76 |
double d2 = f; // float to double |
| 77 |
// CIR: %{{[0-9]+}} = cir.cast floating %{{[0-9]+}} : !cir.float -> !cir.double |
| 78 |
// LLVM: %{{[0-9]+}} = fpext float %{{[0-9]+}} to double |
| 79 |
|
| 80 |
f = d2; // double to float |
| 81 |
// CIR: %{{[0-9]+}} = cir.cast floating %{{[0-9]+}} : !cir.double -> !cir.float |
| 82 |
// LLVM: %{{[0-9]+}} = fptrunc double %{{[0-9]+}} to float |
| 83 |
|
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
bool 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.bool |
| 97 |
|
| 98 |
// LLVM-LABEL: define{{.*}} i1 @_Z4cptrPv(ptr %0) |
| 99 |
// LLVM: %[[ARG_STORAGE:.*]] = alloca ptr, i64 1 |
| 100 |
// LLVM: %[[RETVAL:.*]] = alloca i8, i64 1 |
| 101 |
// LLVM: %[[X_STORAGE:.*]] = alloca i8, i64 1 |
| 102 |
// 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]], null |
| 105 |
// LLVM: ret i1 |
| 106 |
|
| 107 |
void should_not_cast() { |
| 108 |
unsigned x1; |
| 109 |
unsigned uu = (unsigned)x1; // identity |
| 110 |
|
| 111 |
bool x2; |
| 112 |
bool ib = (bool)x2; // identity |
| 113 |
|
| 114 |
(void) ib; // void cast |
| 115 |
} |
| 116 |
|
| 117 |
// CIR: cir.func{{.*}} @_Z15should_not_castv |
| 118 |
// CIR-NOT: cir.cast |
| 119 |
// CIR: cir.return |
| 120 |
|
| 121 |
typedef int vi4 __attribute__((vector_size(16))); |
| 122 |
typedef double vd2 __attribute__((vector_size(16))); |
| 123 |
|
| 124 |
void 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 16 |
| 133 |
// LLVM: %[[I_VEC:.*]] = bitcast <2 x double> %[[D_VEC]] to <4 x i32> |
| 134 |
|
| 135 |
void f(long int start) { |
| 136 |
void *p = (void*)start; |
| 137 |
} |
| 138 |
// CIR: %[[L:.*]] = cir.load {{.*}} : !cir.ptr<!s64i>, !s64i |
| 139 |
// CIR: %[[MID:.*]] = cir.cast integral %[[L]] : !s64i -> !u64i |
| 140 |
// 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 8 |
| 144 |
// LLVM: %[[PADDR:.*]] = alloca ptr, i64 1, align 8 |
| 145 |
// LLVM: store i64 %0, ptr %[[ADDR]], align 8 |
| 146 |
// LLVM: %[[L:.*]] = load i64, ptr %[[ADDR]], align 8 |
| 147 |
// LLVM: %[[PTR:.*]] = inttoptr i64 %[[L]] to ptr |
| 148 |
// LLVM: store ptr %[[PTR]], ptr %[[PADDR]], align 8 |
| 149 |
// LLVM: ret void |
| 150 |
|
| 151 |
struct A { int x; }; |
| 152 |
|
| 153 |
void 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 ptr |
| 158 |
|
| 159 |
void 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> |