1 ; RUN: opt < %s -instcombine -S | FileCheck %s 2 3 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64" 4 target triple = "x86_64-apple-darwin10.0.0" 5 6 ; Bitcasts between vectors and scalars are valid. 7 ; PR4487 8 define i32 @test1(i64 %a) { 9 %t1 = bitcast i64 %a to <2 x i32> 10 %t2 = bitcast i64 %a to <2 x i32> 11 %t3 = xor <2 x i32> %t1, %t2 12 %t4 = extractelement <2 x i32> %t3, i32 0 13 ret i32 %t4 14 15 ; CHECK-LABEL: @test1( 16 ; CHECK: ret i32 0 17 } 18 19 ; Perform the bitwise logic in the source type of the operands to eliminate bitcasts. 20 21 define <2 x i32> @xor_two_vector_bitcasts(<1 x i64> %a, <1 x i64> %b) { 22 %t1 = bitcast <1 x i64> %a to <2 x i32> 23 %t2 = bitcast <1 x i64> %b to <2 x i32> 24 %t3 = xor <2 x i32> %t1, %t2 25 ret <2 x i32> %t3 26 27 ; CHECK-LABEL: @xor_two_vector_bitcasts( 28 ; CHECK-NEXT: %t31 = xor <1 x i64> %a, %b 29 ; CHECK-NEXT: %t3 = bitcast <1 x i64> %t31 to <2 x i32> 30 ; CHECK-NEXT: ret <2 x i32> %t3 31 } 32 33 ; Verify that 'xor' of vector and constant is done as a vector bitwise op before the bitcast. 34 35 define <2 x i32> @xor_bitcast_vec_to_vec(<1 x i64> %a) { 36 %t1 = bitcast <1 x i64> %a to <2 x i32> 37 %t2 = xor <2 x i32> <i32 1, i32 2>, %t1 38 ret <2 x i32> %t2 39 40 ; CHECK-LABEL: @xor_bitcast_vec_to_vec( 41 ; CHECK-NEXT: %t21 = xor <1 x i64> %a, <i64 8589934593> 42 ; CHECK-NEXT: %t2 = bitcast <1 x i64> %t21 to <2 x i32> 43 ; CHECK-NEXT: ret <2 x i32> %t2 44 } 45 46 ; Verify that 'and' of integer and constant is done as a vector bitwise op before the bitcast. 47 48 define i64 @and_bitcast_vec_to_int(<2 x i32> %a) { 49 %t1 = bitcast <2 x i32> %a to i64 50 %t2 = and i64 %t1, 3 51 ret i64 %t2 52 53 ; CHECK-LABEL: @and_bitcast_vec_to_int( 54 ; CHECK-NEXT: %t21 = and <2 x i32> %a, <i32 3, i32 0> 55 ; CHECK-NEXT: %t2 = bitcast <2 x i32> %t21 to i64 56 ; CHECK-NEXT: ret i64 %t2 57 } 58 59 ; Verify that 'or' of vector and constant is done as an integer bitwise op before the bitcast. 60 61 define <2 x i32> @or_bitcast_int_to_vec(i64 %a) { 62 %t1 = bitcast i64 %a to <2 x i32> 63 %t2 = or <2 x i32> %t1, <i32 1, i32 2> 64 ret <2 x i32> %t2 65 66 ; CHECK-LABEL: @or_bitcast_int_to_vec( 67 ; CHECK-NEXT: %t21 = or i64 %a, 8589934593 68 ; CHECK-NEXT: %t2 = bitcast i64 %t21 to <2 x i32> 69 ; CHECK-NEXT: ret <2 x i32> %t2 70 } 71 72 ; Optimize bitcasts that are extracting low element of vector. This happens 73 ; because of SRoA. 74 ; rdar://7892780 75 define float @test2(<2 x float> %A, <2 x i32> %B) { 76 %tmp28 = bitcast <2 x float> %A to i64 ; <i64> [#uses=2] 77 %tmp23 = trunc i64 %tmp28 to i32 ; <i32> [#uses=1] 78 %tmp24 = bitcast i32 %tmp23 to float ; <float> [#uses=1] 79 80 %tmp = bitcast <2 x i32> %B to i64 81 %tmp2 = trunc i64 %tmp to i32 ; <i32> [#uses=1] 82 %tmp4 = bitcast i32 %tmp2 to float ; <float> [#uses=1] 83 84 %add = fadd float %tmp24, %tmp4 85 ret float %add 86 87 ; CHECK-LABEL: @test2( 88 ; CHECK-NEXT: %tmp24 = extractelement <2 x float> %A, i32 0 89 ; CHECK-NEXT: bitcast <2 x i32> %B to <2 x float> 90 ; CHECK-NEXT: %tmp4 = extractelement <2 x float> {{.*}}, i32 0 91 ; CHECK-NEXT: %add = fadd float %tmp24, %tmp4 92 ; CHECK-NEXT: ret float %add 93 } 94 95 ; Optimize bitcasts that are extracting other elements of a vector. This 96 ; happens because of SRoA. 97 ; rdar://7892780 98 define float @test3(<2 x float> %A, <2 x i64> %B) { 99 %tmp28 = bitcast <2 x float> %A to i64 100 %tmp29 = lshr i64 %tmp28, 32 101 %tmp23 = trunc i64 %tmp29 to i32 102 %tmp24 = bitcast i32 %tmp23 to float 103 104 %tmp = bitcast <2 x i64> %B to i128 105 %tmp1 = lshr i128 %tmp, 64 106 %tmp2 = trunc i128 %tmp1 to i32 107 %tmp4 = bitcast i32 %tmp2 to float 108 109 %add = fadd float %tmp24, %tmp4 110 ret float %add 111 112 ; CHECK-LABEL: @test3( 113 ; CHECK-NEXT: %tmp24 = extractelement <2 x float> %A, i32 1 114 ; CHECK-NEXT: bitcast <2 x i64> %B to <4 x float> 115 ; CHECK-NEXT: %tmp4 = extractelement <4 x float> {{.*}}, i32 2 116 ; CHECK-NEXT: %add = fadd float %tmp24, %tmp4 117 ; CHECK-NEXT: ret float %add 118 } 119 120 ; Both bitcasts are unnecessary; change the extractelement. 121 122 define float @bitcast_extelt1(<2 x float> %A) { 123 %bc1 = bitcast <2 x float> %A to <2 x i32> 124 %ext = extractelement <2 x i32> %bc1, i32 0 125 %bc2 = bitcast i32 %ext to float 126 ret float %bc2 127 128 ; CHECK-LABEL: @bitcast_extelt1( 129 ; CHECK-NEXT: %bc2 = extractelement <2 x float> %A, i32 0 130 ; CHECK-NEXT: ret float %bc2 131 } 132 133 ; Second bitcast can be folded into the first. 134 135 define i64 @bitcast_extelt2(<4 x float> %A) { 136 %bc1 = bitcast <4 x float> %A to <2 x double> 137 %ext = extractelement <2 x double> %bc1, i32 1 138 %bc2 = bitcast double %ext to i64 139 ret i64 %bc2 140 141 ; CHECK-LABEL: @bitcast_extelt2( 142 ; CHECK-NEXT: %bc = bitcast <4 x float> %A to <2 x i64> 143 ; CHECK-NEXT: %bc2 = extractelement <2 x i64> %bc, i32 1 144 ; CHECK-NEXT: ret i64 %bc2 145 } 146 147 ; TODO: This should return %A. 148 149 define <2 x i32> @bitcast_extelt3(<2 x i32> %A) { 150 %bc1 = bitcast <2 x i32> %A to <1 x i64> 151 %ext = extractelement <1 x i64> %bc1, i32 0 152 %bc2 = bitcast i64 %ext to <2 x i32> 153 ret <2 x i32> %bc2 154 155 ; CHECK-LABEL: @bitcast_extelt3( 156 ; CHECK-NEXT: %bc1 = bitcast <2 x i32> %A to <1 x i64> 157 ; CHECK-NEXT: %ext = extractelement <1 x i64> %bc1, i32 0 158 ; CHECK-NEXT: %bc2 = bitcast i64 %ext to <2 x i32> 159 ; CHECK-NEXT: ret <2 x i32> %bc2 160 } 161 162 ; Handle the case where the input is not a vector. 163 164 define double @bitcast_extelt4(i128 %A) { 165 %bc1 = bitcast i128 %A to <2 x i64> 166 %ext = extractelement <2 x i64> %bc1, i32 0 167 %bc2 = bitcast i64 %ext to double 168 ret double %bc2 169 170 ; CHECK-LABEL: @bitcast_extelt4( 171 ; CHECK-NEXT: %bc = bitcast i128 %A to <2 x double> 172 ; CHECK-NEXT: %bc2 = extractelement <2 x double> %bc, i32 0 173 ; CHECK-NEXT: ret double %bc2 174 } 175 176 define <2 x i32> @test4(i32 %A, i32 %B){ 177 %tmp38 = zext i32 %A to i64 178 %tmp32 = zext i32 %B to i64 179 %tmp33 = shl i64 %tmp32, 32 180 %ins35 = or i64 %tmp33, %tmp38 181 %tmp43 = bitcast i64 %ins35 to <2 x i32> 182 ret <2 x i32> %tmp43 183 ; CHECK-LABEL: @test4( 184 ; CHECK-NEXT: insertelement <2 x i32> undef, i32 %A, i32 0 185 ; CHECK-NEXT: insertelement <2 x i32> {{.*}}, i32 %B, i32 1 186 ; CHECK-NEXT: ret <2 x i32> 187 188 } 189 190 ; rdar://8360454 191 define <2 x float> @test5(float %A, float %B) { 192 %tmp37 = bitcast float %A to i32 193 %tmp38 = zext i32 %tmp37 to i64 194 %tmp31 = bitcast float %B to i32 195 %tmp32 = zext i32 %tmp31 to i64 196 %tmp33 = shl i64 %tmp32, 32 197 %ins35 = or i64 %tmp33, %tmp38 198 %tmp43 = bitcast i64 %ins35 to <2 x float> 199 ret <2 x float> %tmp43 200 ; CHECK-LABEL: @test5( 201 ; CHECK-NEXT: insertelement <2 x float> undef, float %A, i32 0 202 ; CHECK-NEXT: insertelement <2 x float> {{.*}}, float %B, i32 1 203 ; CHECK-NEXT: ret <2 x float> 204 } 205 206 define <2 x float> @test6(float %A){ 207 %tmp23 = bitcast float %A to i32 ; <i32> [#uses=1] 208 %tmp24 = zext i32 %tmp23 to i64 ; <i64> [#uses=1] 209 %tmp25 = shl i64 %tmp24, 32 ; <i64> [#uses=1] 210 %mask20 = or i64 %tmp25, 1109917696 ; <i64> [#uses=1] 211 %tmp35 = bitcast i64 %mask20 to <2 x float> ; <<2 x float>> [#uses=1] 212 ret <2 x float> %tmp35 213 ; CHECK-LABEL: @test6( 214 ; CHECK-NEXT: insertelement <2 x float> <float 4.200000e+01, float undef>, float %A, i32 1 215 ; CHECK: ret 216 } 217 218 define i64 @ISPC0(i64 %in) { 219 %out = and i64 %in, xor (i64 bitcast (<4 x i16> <i16 -1, i16 -1, i16 -1, i16 -1> to i64), i64 -1) 220 ret i64 %out 221 ; CHECK-LABEL: @ISPC0( 222 ; CHECK: ret i64 0 223 } 224 225 226 define i64 @Vec2(i64 %in) { 227 %out = and i64 %in, xor (i64 bitcast (<4 x i16> <i16 0, i16 0, i16 0, i16 0> to i64), i64 0) 228 ret i64 %out 229 ; CHECK-LABEL: @Vec2( 230 ; CHECK: ret i64 0 231 } 232 233 define i64 @All11(i64 %in) { 234 %out = and i64 %in, xor (i64 bitcast (<2 x float> bitcast (i64 -1 to <2 x float>) to i64), i64 -1) 235 ret i64 %out 236 ; CHECK-LABEL: @All11( 237 ; CHECK: ret i64 0 238 } 239 240 241 define i32 @All111(i32 %in) { 242 %out = and i32 %in, xor (i32 bitcast (<1 x float> bitcast (i32 -1 to <1 x float>) to i32), i32 -1) 243 ret i32 %out 244 ; CHECK-LABEL: @All111( 245 ; CHECK: ret i32 0 246 } 247 248 define <2 x i16> @BitcastInsert(i32 %a) { 249 %v = insertelement <1 x i32> undef, i32 %a, i32 0 250 %r = bitcast <1 x i32> %v to <2 x i16> 251 ret <2 x i16> %r 252 ; CHECK-LABEL: @BitcastInsert( 253 ; CHECK: bitcast i32 %a to <2 x i16> 254 } 255 256 ; PR17293 257 define <2 x i64> @test7(<2 x i8*>* %arg) nounwind { 258 %cast = bitcast <2 x i8*>* %arg to <2 x i64>* 259 %load = load <2 x i64>, <2 x i64>* %cast, align 16 260 ret <2 x i64> %load 261 ; CHECK: @test7 262 ; CHECK: bitcast 263 ; CHECK: load 264 } 265 266 define i8 @test8() { 267 %res = bitcast <8 x i1> <i1 true, i1 true, i1 false, i1 true, i1 false, i1 true, i1 false, i1 true> to i8 268 ret i8 %res 269 ; CHECK: @test8 270 ; CHECK: ret i8 -85 271 } 272