1 ; RUN: opt -S -instcombine -o - %s | FileCheck %s 2 target datalayout = "e-p:32:32:32-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v64:64:64-v128:128:128-a0:0:64" 3 4 5 6 ; Cases that should be bitcast 7 8 ; Test cast between scalars with same bit sizes 9 @alias_i32_to_f32 = alias float (float), bitcast (i32 (i32)* @func_i32 to float (float)*) 10 11 ; Test cast between vectors with same number of elements and bit sizes 12 @alias_v2i32_to_v2f32 = alias <2 x float> (<2 x float>), bitcast (<2 x i32> (<2 x i32>)* @func_v2i32 to <2 x float> (<2 x float>)*) 13 14 ; Test cast from vector to scalar with same number of bits 15 @alias_v2f32_to_i64 = alias <2 x float> (<2 x float>), bitcast (i64 (i64)* @func_i64 to <2 x float> (<2 x float>)*) 16 17 ; Test cast from scalar to vector with same number of bits 18 @alias_i64_to_v2f32 = alias i64 (i64), bitcast (<2 x float> (<2 x float>)* @func_v2f32 to i64 (i64)*) 19 20 ; Test cast between vectors of pointers 21 @alias_v2i32p_to_v2i64p = alias <2 x i64*> (<2 x i64*>), bitcast (<2 x i32*> (<2 x i32*>)* @func_v2i32p to <2 x i64*> (<2 x i64*>)*) 22 23 24 ; Cases that should be invalid and unchanged 25 26 ; Test cast between scalars with different bit sizes 27 @alias_i64_to_f32 = alias float (float), bitcast (i64 (i64)* @func_i64 to float (float)*) 28 29 ; Test cast between vectors with different bit sizes but the 30 ; same number of elements 31 @alias_v2i64_to_v2f32 = alias <2 x float> (<2 x float>), bitcast (<2 x i64> (<2 x i64>)* @func_v2i64 to <2 x float> (<2 x float>)*) 32 33 ; Test cast between vectors with same number of bits and different 34 ; numbers of elements 35 @alias_v2i32_to_v4f32 = alias <4 x float> (<4 x float>), bitcast (<2 x i32> (<2 x i32>)* @func_v2i32 to <4 x float> (<4 x float>)*) 36 37 ; Test cast between scalar and vector with different number of bits 38 @alias_i64_to_v4f32 = alias i64 (i64), bitcast (<4 x float> (<4 x float>)* @func_v4f32 to i64 (i64)*) 39 40 ; Test cast between vector and scalar with different number of bits 41 @alias_v4f32_to_i64 = alias <4 x float> (<4 x float>), bitcast (i64 (i64)* @func_i64 to <4 x float> (<4 x float>)*) 42 43 ; Test cast from scalar to vector of pointers with same number of bits 44 ; We don't know the pointer size at this point, so this can't be done 45 @alias_i64_to_v2i32p = alias i64 (i64), bitcast (<2 x i32*> (<2 x i32*>)* @func_v2i32p to i64 (i64)*) 46 47 ; Test cast between vector of pointers and scalar with different number of bits 48 @alias_v4i32p_to_i64 = alias <4 x i32*> (<4 x i32*>), bitcast (i64 (i64)* @func_i64 to <4 x i32*> (<4 x i32*>)*) 49 50 51 52 define internal <2 x i32> @func_v2i32(<2 x i32> %v) noinline nounwind { 53 entry: 54 ret <2 x i32> %v 55 } 56 57 define internal <2 x float> @func_v2f32(<2 x float> %v) noinline nounwind { 58 entry: 59 ret <2 x float> %v 60 } 61 62 define internal <4 x float> @func_v4f32(<4 x float> %v) noinline nounwind { 63 entry: 64 ret <4 x float> %v 65 } 66 67 define internal i32 @func_i32(i32 %v) noinline nounwind { 68 entry: 69 ret i32 %v 70 } 71 72 define internal i64 @func_i64(i64 %v) noinline nounwind { 73 entry: 74 ret i64 %v 75 } 76 77 define internal <2 x i64> @func_v2i64(<2 x i64> %v) noinline nounwind { 78 entry: 79 ret <2 x i64> %v 80 } 81 82 define internal <2 x i32*> @func_v2i32p(<2 x i32*> %v) noinline nounwind { 83 entry: 84 ret <2 x i32*> %v 85 } 86 87 ; Valid cases, only bitcast for argument / return type and call underlying function 88 89 ; Sizes match, should only bitcast 90 define void @bitcast_alias_scalar(float* noalias %source, float* noalias %dest) nounwind { 91 entry: 92 ; CHECK-LABEL: @bitcast_alias_scalar 93 ; CHECK: bitcast float* %source to i32* 94 ; CHECK: load i32, i32* 95 ; CHECK-NOT: fptoui 96 ; CHECK-NOT: uitofp 97 ; CHECK: bitcast float* %dest to i32* 98 ; CHECK: store i32 99 %tmp = load float, float* %source, align 8 100 %call = call float @alias_i32_to_f32(float %tmp) nounwind 101 store float %call, float* %dest, align 8 102 ret void 103 } 104 105 ; Sizes match, should only bitcast 106 define void @bitcast_alias_vector(<2 x float>* noalias %source, <2 x float>* noalias %dest) nounwind { 107 entry: 108 ; CHECK-LABEL: @bitcast_alias_vector 109 ; CHECK: bitcast <2 x float>* %source to <2 x i32>* 110 ; CHECK: load <2 x i32>, <2 x i32>* 111 ; CHECK-NOT: fptoui 112 ; CHECK-NOT: uitofp 113 ; CHECK: bitcast <2 x float>* %dest to <2 x i32>* 114 ; CHECK: store <2 x i32> 115 %tmp = load <2 x float>, <2 x float>* %source, align 8 116 %call = call <2 x float> @alias_v2i32_to_v2f32(<2 x float> %tmp) nounwind 117 store <2 x float> %call, <2 x float>* %dest, align 8 118 ret void 119 } 120 121 ; Sizes match, should only bitcast 122 define void @bitcast_alias_vector_scalar_same_size(<2 x float>* noalias %source, <2 x float>* noalias %dest) nounwind { 123 entry: 124 ; CHECK-LABEL: @bitcast_alias_vector_scalar_same_size 125 ; CHECK: bitcast <2 x float>* %source to i64* 126 ; CHECK: load i64, i64* 127 ; CHECK: %call = call i64 @func_i64 128 ; CHECK: bitcast <2 x float>* %dest to i64* 129 ; CHECK: store i64 130 %tmp = load <2 x float>, <2 x float>* %source, align 8 131 %call = call <2 x float> @alias_v2f32_to_i64(<2 x float> %tmp) nounwind 132 store <2 x float> %call, <2 x float>* %dest, align 8 133 ret void 134 } 135 136 define void @bitcast_alias_scalar_vector_same_size(i64* noalias %source, i64* noalias %dest) nounwind { 137 entry: 138 ; CHECK-LABEL: @bitcast_alias_scalar_vector_same_size 139 ; CHECK: bitcast i64* %source to <2 x float>* 140 ; CHECK: load <2 x float>, <2 x float>* 141 ; CHECK: call <2 x float> @func_v2f32 142 ; CHECK: bitcast i64* %dest to <2 x float>* 143 ; CHECK: store <2 x float> 144 %tmp = load i64, i64* %source, align 8 145 %call = call i64 @alias_i64_to_v2f32(i64 %tmp) nounwind 146 store i64 %call, i64* %dest, align 8 147 ret void 148 } 149 150 define void @bitcast_alias_vector_ptrs_same_size(<2 x i64*>* noalias %source, <2 x i64*>* noalias %dest) nounwind { 151 entry: 152 ; CHECK-LABEL: @bitcast_alias_vector_ptrs_same_size 153 ; CHECK: bitcast <2 x i64*>* %source to <2 x i32*>* 154 ; CHECK: load <2 x i32*>, <2 x i32*>* 155 ; CHECK: call <2 x i32*> @func_v2i32p 156 ; CHECK: bitcast <2 x i64*>* %dest to <2 x i32*>* 157 ; CHECK: store <2 x i32*> 158 %tmp = load <2 x i64*>, <2 x i64*>* %source, align 8 159 %call = call <2 x i64*> @alias_v2i32p_to_v2i64p(<2 x i64*> %tmp) nounwind 160 store <2 x i64*> %call, <2 x i64*>* %dest, align 8 161 ret void 162 } 163 164 ; Invalid cases: 165 166 define void @bitcast_alias_mismatch_scalar_size(float* noalias %source, float* noalias %dest) nounwind { 167 entry: 168 ; CHECK-LABEL: @bitcast_alias_mismatch_scalar_size 169 ; CHECK-NOT: fptoui 170 ; CHECK: @alias_i64_to_f32 171 ; CHECK-NOT: uitofp 172 %tmp = load float, float* %source, align 8 173 %call = call float @alias_i64_to_f32(float %tmp) nounwind 174 store float %call, float* %dest, align 8 175 ret void 176 } 177 178 define void @bitcast_alias_mismatch_vector_element_and_bit_size(<2 x float>* noalias %source, <2 x float>* noalias %dest) nounwind { 179 entry: 180 ; CHECK-LABEL: @bitcast_alias_mismatch_vector_element_and_bit_size 181 ; CHECK-NOT: fptoui <2 x float> %tmp to <2 x i64> 182 ; CHECK: @alias_v2i64_to_v2f32 183 ; CHECK-NOT: uitofp <2 x i64> %call to <2 x float> 184 %tmp = load <2 x float>, <2 x float>* %source, align 8 185 %call = call <2 x float> @alias_v2i64_to_v2f32(<2 x float> %tmp) nounwind 186 store <2 x float> %call, <2 x float>* %dest, align 8 187 ret void 188 } 189 190 define void @bitcast_alias_vector_mismatched_number_elements(<4 x float>* noalias %source, <4 x float>* noalias %dest) nounwind { 191 entry: 192 ; CHECK-LABEL: @bitcast_alias_vector_mismatched_number_elements 193 ; CHECK: %call = call <4 x float> @alias_v2i32_to_v4f32 194 %tmp = load <4 x float>, <4 x float>* %source, align 8 195 %call = call <4 x float> @alias_v2i32_to_v4f32(<4 x float> %tmp) nounwind 196 store <4 x float> %call, <4 x float>* %dest, align 8 197 ret void 198 } 199 200 define void @bitcast_alias_vector_scalar_mismatched_bit_size(<4 x float>* noalias %source, <4 x float>* noalias %dest) nounwind { 201 entry: 202 ; CHECK-LABEL: @bitcast_alias_vector_scalar_mismatched_bit_size 203 ; CHECK: %call = call <4 x float> @alias_v4f32_to_i64 204 %tmp = load <4 x float>, <4 x float>* %source, align 8 205 %call = call <4 x float> @alias_v4f32_to_i64(<4 x float> %tmp) nounwind 206 store <4 x float> %call, <4 x float>* %dest, align 8 207 ret void 208 } 209 210 define void @bitcast_alias_vector_ptrs_scalar_mismatched_bit_size(<4 x i32*>* noalias %source, <4 x i32*>* noalias %dest) nounwind { 211 entry: 212 ; CHECK-LABEL: @bitcast_alias_vector_ptrs_scalar_mismatched_bit_size 213 ; CHECK: @alias_v4i32p_to_i64 214 %tmp = load <4 x i32*>, <4 x i32*>* %source, align 8 215 %call = call <4 x i32*> @alias_v4i32p_to_i64(<4 x i32*> %tmp) nounwind 216 store <4 x i32*> %call, <4 x i32*>* %dest, align 8 217 ret void 218 } 219 220 define void @bitcast_alias_scalar_vector_ptrs_same_size(i64* noalias %source, i64* noalias %dest) nounwind { 221 entry: 222 ; CHECK-LABEL: @bitcast_alias_scalar_vector_ptrs_same_size 223 ; CHECK: @alias_i64_to_v2i32p 224 %tmp = load i64, i64* %source, align 8 225 %call = call i64 @alias_i64_to_v2i32p(i64 %tmp) nounwind 226 store i64 %call, i64* %dest, align 8 227 ret void 228 } 229 230 define void @bitcast_alias_scalar_vector_mismatched_bit_size(i64* noalias %source, i64* noalias %dest) nounwind { 231 entry: 232 ; CHECK-LABEL: @bitcast_alias_scalar_vector_mismatched_bit_size 233 ; CHECK: call i64 @alias_i64_to_v4f32 234 %tmp = load i64, i64* %source, align 8 235 %call = call i64 @alias_i64_to_v4f32(i64 %tmp) nounwind 236 store i64 %call, i64* %dest, align 8 237 ret void 238 } 239 240