Home | History | Annotate | Download | only in InstCombine
      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 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 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 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 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 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 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 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 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 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 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 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 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 %tmp to i32
     94 ; CHECK-NOT: fptoui
     95 ; CHECK-NOT: uitofp
     96 ; CHECK: bitcast i32 %call to float
     97   %tmp = load float* %source, align 8
     98   %call = call float @alias_i32_to_f32(float %tmp) nounwind
     99   store float %call, float* %dest, align 8
    100   ret void
    101 }
    102 
    103 ; Sizes match, should only bitcast
    104 define void @bitcast_alias_vector(<2 x float>* noalias %source, <2 x float>* noalias %dest) nounwind {
    105 entry:
    106 ; CHECK-LABEL: @bitcast_alias_vector
    107 ; CHECK: bitcast <2 x float> %tmp to <2 x i32>
    108 ; CHECK-NOT: fptoui
    109 ; CHECK-NOT: uitofp
    110 ; CHECK: bitcast <2 x i32> %call to <2 x float>
    111   %tmp = load <2 x float>* %source, align 8
    112   %call = call <2 x float> @alias_v2i32_to_v2f32(<2 x float> %tmp) nounwind
    113   store <2 x float> %call, <2 x float>* %dest, align 8
    114   ret void
    115 }
    116 
    117 ; Sizes match, should only bitcast
    118 define void @bitcast_alias_vector_scalar_same_size(<2 x float>* noalias %source, <2 x float>* noalias %dest) nounwind {
    119 entry:
    120 ; CHECK-LABEL: @bitcast_alias_vector_scalar_same_size
    121 ; CHECK: bitcast <2 x float> %tmp to i64
    122 ; CHECK: %call = call i64 @func_i64
    123 ; CHECK: bitcast i64 %call to <2 x float>
    124   %tmp = load <2 x float>* %source, align 8
    125   %call = call <2 x float> @alias_v2f32_to_i64(<2 x float> %tmp) nounwind
    126   store <2 x float> %call, <2 x float>* %dest, align 8
    127   ret void
    128 }
    129 
    130 define void @bitcast_alias_scalar_vector_same_size(i64* noalias %source, i64* noalias %dest) nounwind {
    131 entry:
    132 ; CHECK-LABEL: @bitcast_alias_scalar_vector_same_size
    133 ; CHECK: bitcast i64 %tmp to <2 x float>
    134 ; CHECK: call <2 x float> @func_v2f32
    135 ; CHECK: bitcast <2 x float> %call to i64
    136   %tmp = load i64* %source, align 8
    137   %call = call i64 @alias_i64_to_v2f32(i64 %tmp) nounwind
    138   store i64 %call, i64* %dest, align 8
    139   ret void
    140 }
    141 
    142 define void @bitcast_alias_vector_ptrs_same_size(<2 x i64*>* noalias %source, <2 x i64*>* noalias %dest) nounwind {
    143 entry:
    144 ; CHECK-LABEL: @bitcast_alias_vector_ptrs_same_size
    145 ; CHECK: bitcast <2 x i64*> %tmp to <2 x i32*>
    146 ; CHECK: call <2 x i32*> @func_v2i32p
    147 ; CHECK: bitcast <2 x i32*> %call to <2 x i64*>
    148   %tmp = load <2 x i64*>* %source, align 8
    149   %call = call <2 x i64*> @alias_v2i32p_to_v2i64p(<2 x i64*> %tmp) nounwind
    150   store <2 x i64*> %call, <2 x i64*>* %dest, align 8
    151   ret void
    152 }
    153 
    154 ; Invalid cases:
    155 
    156 define void @bitcast_alias_mismatch_scalar_size(float* noalias %source, float* noalias %dest) nounwind {
    157 entry:
    158 ; CHECK-LABEL: @bitcast_alias_mismatch_scalar_size
    159 ; CHECK-NOT: fptoui
    160 ; CHECK: @alias_i64_to_f32
    161 ; CHECK-NOT: uitofp
    162   %tmp = load float* %source, align 8
    163   %call = call float @alias_i64_to_f32(float %tmp) nounwind
    164   store float %call, float* %dest, align 8
    165   ret void
    166 }
    167 
    168 define void @bitcast_alias_mismatch_vector_element_and_bit_size(<2 x float>* noalias %source, <2 x float>* noalias %dest) nounwind {
    169 entry:
    170 ; CHECK-LABEL: @bitcast_alias_mismatch_vector_element_and_bit_size
    171 ; CHECK-NOT: fptoui <2 x float> %tmp to <2 x i64>
    172 ; CHECK: @alias_v2i64_to_v2f32
    173 ; CHECK-NOT: uitofp <2 x i64> %call to <2 x float>
    174   %tmp = load <2 x float>* %source, align 8
    175   %call = call <2 x float> @alias_v2i64_to_v2f32(<2 x float> %tmp) nounwind
    176   store <2 x float> %call, <2 x float>* %dest, align 8
    177   ret void
    178 }
    179 
    180 define void @bitcast_alias_vector_mismatched_number_elements(<4 x float>* noalias %source, <4 x float>* noalias %dest) nounwind {
    181 entry:
    182 ; CHECK-LABEL: @bitcast_alias_vector_mismatched_number_elements
    183 ; CHECK:  %call = call <4 x float> @alias_v2i32_to_v4f32
    184   %tmp = load <4 x float>* %source, align 8
    185   %call = call <4 x float> @alias_v2i32_to_v4f32(<4 x float> %tmp) nounwind
    186   store <4 x float> %call, <4 x float>* %dest, align 8
    187   ret void
    188 }
    189 
    190 define void @bitcast_alias_vector_scalar_mismatched_bit_size(<4 x float>* noalias %source, <4 x float>* noalias %dest) nounwind {
    191 entry:
    192 ; CHECK-LABEL: @bitcast_alias_vector_scalar_mismatched_bit_size
    193 ; CHECK:  %call = call <4 x float> @alias_v4f32_to_i64
    194   %tmp = load <4 x float>* %source, align 8
    195   %call = call <4 x float> @alias_v4f32_to_i64(<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_ptrs_scalar_mismatched_bit_size(<4 x i32*>* noalias %source, <4 x i32*>* noalias %dest) nounwind {
    201 entry:
    202 ; CHECK-LABEL: @bitcast_alias_vector_ptrs_scalar_mismatched_bit_size
    203 ; CHECK: @alias_v4i32p_to_i64
    204   %tmp = load <4 x i32*>* %source, align 8
    205   %call = call <4 x i32*> @alias_v4i32p_to_i64(<4 x i32*> %tmp) nounwind
    206   store <4 x i32*> %call, <4 x i32*>* %dest, align 8
    207   ret void
    208 }
    209 
    210 define void @bitcast_alias_scalar_vector_ptrs_same_size(i64* noalias %source, i64* noalias %dest) nounwind {
    211 entry:
    212 ; CHECK-LABEL: @bitcast_alias_scalar_vector_ptrs_same_size
    213 ; CHECK: @alias_i64_to_v2i32p
    214   %tmp = load i64* %source, align 8
    215   %call = call i64 @alias_i64_to_v2i32p(i64 %tmp) nounwind
    216   store i64 %call, i64* %dest, align 8
    217   ret void
    218 }
    219 
    220 define void @bitcast_alias_scalar_vector_mismatched_bit_size(i64* noalias %source, i64* noalias %dest) nounwind {
    221 entry:
    222 ; CHECK-LABEL: @bitcast_alias_scalar_vector_mismatched_bit_size
    223 ; CHECK: call i64 @alias_i64_to_v4f32
    224   %tmp = load i64* %source, align 8
    225   %call = call i64 @alias_i64_to_v4f32(i64 %tmp) nounwind
    226   store i64 %call, i64* %dest, align 8
    227   ret void
    228 }
    229 
    230