1 ; Tests to make sure elimination of casts is working correctly 2 ; RUN: opt < %s -instcombine -S | FileCheck %s 3 target datalayout = "E-p:64:64:64-p1:32:32:32-p2:64:64:64-p3:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128-n8:16:32:64" 4 5 @inbuf = external global [32832 x i8] ; <[32832 x i8]*> [#uses=1] 6 7 define i32 @test1(i32 %A) { 8 %c1 = bitcast i32 %A to i32 ; <i32> [#uses=1] 9 %c2 = bitcast i32 %c1 to i32 ; <i32> [#uses=1] 10 ret i32 %c2 11 ; CHECK: ret i32 %A 12 } 13 14 define i64 @test2(i8 %A) { 15 %c1 = zext i8 %A to i16 ; <i16> [#uses=1] 16 %c2 = zext i16 %c1 to i32 ; <i32> [#uses=1] 17 %Ret = zext i32 %c2 to i64 ; <i64> [#uses=1] 18 ret i64 %Ret 19 ; CHECK: %Ret = zext i8 %A to i64 20 ; CHECK: ret i64 %Ret 21 } 22 23 ; This function should just use bitwise AND 24 define i64 @test3(i64 %A) { 25 %c1 = trunc i64 %A to i8 ; <i8> [#uses=1] 26 %c2 = zext i8 %c1 to i64 ; <i64> [#uses=1] 27 ret i64 %c2 28 ; CHECK: %c2 = and i64 %A, 255 29 ; CHECK: ret i64 %c2 30 } 31 32 define i32 @test4(i32 %A, i32 %B) { 33 %COND = icmp slt i32 %A, %B ; <i1> [#uses=1] 34 ; Booleans are unsigned integrals 35 %c = zext i1 %COND to i8 ; <i8> [#uses=1] 36 ; for the cast elim purpose 37 %result = zext i8 %c to i32 ; <i32> [#uses=1] 38 ret i32 %result 39 ; CHECK: %COND = icmp slt i32 %A, %B 40 ; CHECK: %result = zext i1 %COND to i32 41 ; CHECK: ret i32 %result 42 } 43 44 define i32 @test5(i1 %B) { 45 ; This cast should get folded into 46 %c = zext i1 %B to i8 ; <i8> [#uses=1] 47 ; this cast 48 %result = zext i8 %c to i32 ; <i32> [#uses=1] 49 ret i32 %result 50 ; CHECK: %result = zext i1 %B to i32 51 ; CHECK: ret i32 %result 52 } 53 54 define i32 @test6(i64 %A) { 55 %c1 = trunc i64 %A to i32 ; <i32> [#uses=1] 56 %res = bitcast i32 %c1 to i32 ; <i32> [#uses=1] 57 ret i32 %res 58 ; CHECK: trunc i64 %A to i32 59 ; CHECK-NEXT: ret i32 60 } 61 62 define i64 @test7(i1 %A) { 63 %c1 = zext i1 %A to i32 ; <i32> [#uses=1] 64 %res = sext i32 %c1 to i64 ; <i64> [#uses=1] 65 ret i64 %res 66 ; CHECK: %res = zext i1 %A to i64 67 ; CHECK: ret i64 %res 68 } 69 70 define i64 @test8(i8 %A) { 71 %c1 = sext i8 %A to i64 ; <i64> [#uses=1] 72 %res = bitcast i64 %c1 to i64 ; <i64> [#uses=1] 73 ret i64 %res 74 ; CHECK: = sext i8 %A to i64 75 ; CHECK-NEXT: ret i64 76 } 77 78 define i16 @test9(i16 %A) { 79 %c1 = sext i16 %A to i32 ; <i32> [#uses=1] 80 %c2 = trunc i32 %c1 to i16 ; <i16> [#uses=1] 81 ret i16 %c2 82 ; CHECK: ret i16 %A 83 } 84 85 define i16 @test10(i16 %A) { 86 %c1 = sext i16 %A to i32 ; <i32> [#uses=1] 87 %c2 = trunc i32 %c1 to i16 ; <i16> [#uses=1] 88 ret i16 %c2 89 ; CHECK: ret i16 %A 90 } 91 92 declare void @varargs(i32, ...) 93 94 define void @test11(i32* %P) { 95 %c = bitcast i32* %P to i16* ; <i16*> [#uses=1] 96 call void (i32, ...) @varargs( i32 5, i16* %c ) 97 ret void 98 ; CHECK: call void (i32, ...) @varargs(i32 5, i32* %P) 99 ; CHECK: ret void 100 } 101 102 declare i32 @__gxx_personality_v0(...) 103 define void @test_invoke_vararg_cast(i32* %a, i32* %b) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) { 104 entry: 105 %0 = bitcast i32* %b to i8* 106 %1 = bitcast i32* %a to i64* 107 invoke void (i32, ...) @varargs(i32 1, i8* %0, i64* %1) 108 to label %invoke.cont unwind label %lpad 109 110 invoke.cont: ; preds = %entry 111 ret void 112 113 lpad: ; preds = %entry 114 %2 = landingpad { i8*, i32 } 115 cleanup 116 ret void 117 ; CHECK-LABEL: test_invoke_vararg_cast 118 ; CHECK-LABEL: entry: 119 ; CHECK: invoke void (i32, ...) @varargs(i32 1, i32* %b, i32* %a) 120 } 121 122 define i8* @test13(i64 %A) { 123 %c = getelementptr [0 x i8], [0 x i8]* bitcast ([32832 x i8]* @inbuf to [0 x i8]*), i64 0, i64 %A ; <i8*> [#uses=1] 124 ret i8* %c 125 ; CHECK: %c = getelementptr [32832 x i8], [32832 x i8]* @inbuf, i64 0, i64 %A 126 ; CHECK: ret i8* %c 127 } 128 129 define i1 @test14(i8 %A) { 130 %c = bitcast i8 %A to i8 ; <i8> [#uses=1] 131 %X = icmp ult i8 %c, -128 ; <i1> [#uses=1] 132 ret i1 %X 133 ; CHECK: %X = icmp sgt i8 %A, -1 134 ; CHECK: ret i1 %X 135 } 136 137 138 ; This just won't occur when there's no difference between ubyte and sbyte 139 ;bool %test15(ubyte %A) { 140 ; %c = cast ubyte %A to sbyte 141 ; %X = setlt sbyte %c, 0 ; setgt %A, 127 142 ; ret bool %X 143 ;} 144 145 define i1 @test16(i32* %P) { 146 %c = icmp ne i32* %P, null ; <i1> [#uses=1] 147 ret i1 %c 148 ; CHECK: %c = icmp ne i32* %P, null 149 ; CHECK: ret i1 %c 150 } 151 152 define i16 @test17(i1 %tmp3) { 153 %c = zext i1 %tmp3 to i32 ; <i32> [#uses=1] 154 %t86 = trunc i32 %c to i16 ; <i16> [#uses=1] 155 ret i16 %t86 156 ; CHECK: %t86 = zext i1 %tmp3 to i16 157 ; CHECK: ret i16 %t86 158 } 159 160 define i16 @test18(i8 %tmp3) { 161 %c = sext i8 %tmp3 to i32 ; <i32> [#uses=1] 162 %t86 = trunc i32 %c to i16 ; <i16> [#uses=1] 163 ret i16 %t86 164 ; CHECK: %t86 = sext i8 %tmp3 to i16 165 ; CHECK: ret i16 %t86 166 } 167 168 define i1 @test19(i32 %X) { 169 %c = sext i32 %X to i64 ; <i64> [#uses=1] 170 %Z = icmp slt i64 %c, 12345 ; <i1> [#uses=1] 171 ret i1 %Z 172 ; CHECK: %Z = icmp slt i32 %X, 12345 173 ; CHECK: ret i1 %Z 174 } 175 176 define i1 @test20(i1 %B) { 177 %c = zext i1 %B to i32 ; <i32> [#uses=1] 178 %D = icmp slt i32 %c, -1 ; <i1> [#uses=1] 179 ;; false 180 ret i1 %D 181 ; CHECK: ret i1 false 182 } 183 184 define i32 @test21(i32 %X) { 185 %c1 = trunc i32 %X to i8 ; <i8> [#uses=1] 186 ;; sext -> zext -> and -> nop 187 %c2 = sext i8 %c1 to i32 ; <i32> [#uses=1] 188 %RV = and i32 %c2, 255 ; <i32> [#uses=1] 189 ret i32 %RV 190 ; CHECK: %c21 = and i32 %X, 255 191 ; CHECK: ret i32 %c21 192 } 193 194 define i32 @test22(i32 %X) { 195 %c1 = trunc i32 %X to i8 ; <i8> [#uses=1] 196 ;; sext -> zext -> and -> nop 197 %c2 = sext i8 %c1 to i32 ; <i32> [#uses=1] 198 %RV = shl i32 %c2, 24 ; <i32> [#uses=1] 199 ret i32 %RV 200 ; CHECK: shl i32 %X, 24 201 ; CHECK-NEXT: ret i32 202 } 203 204 define i32 @test23(i32 %X) { 205 ;; Turn into an AND even though X 206 %c1 = trunc i32 %X to i16 ; <i16> [#uses=1] 207 ;; and Z are signed. 208 %c2 = zext i16 %c1 to i32 ; <i32> [#uses=1] 209 ret i32 %c2 210 ; CHECK: %c2 = and i32 %X, 65535 211 ; CHECK: ret i32 %c2 212 } 213 214 define i1 @test24(i1 %C) { 215 %X = select i1 %C, i32 14, i32 1234 ; <i32> [#uses=1] 216 ;; Fold cast into select 217 %c = icmp ne i32 %X, 0 ; <i1> [#uses=1] 218 ret i1 %c 219 ; CHECK: ret i1 true 220 } 221 222 define i32 @test26(float %F) { 223 ;; no need to cast from float->double. 224 %c = fpext float %F to double ; <double> [#uses=1] 225 %D = fptosi double %c to i32 ; <i32> [#uses=1] 226 ret i32 %D 227 ; CHECK: %D = fptosi float %F to i32 228 ; CHECK: ret i32 %D 229 } 230 231 define [4 x float]* @test27([9 x [4 x float]]* %A) { 232 %c = bitcast [9 x [4 x float]]* %A to [4 x float]* ; <[4 x float]*> [#uses=1] 233 ret [4 x float]* %c 234 ; CHECK: %c = getelementptr inbounds [9 x [4 x float]], [9 x [4 x float]]* %A, i64 0, i64 0 235 ; CHECK: ret [4 x float]* %c 236 } 237 238 define float* @test28([4 x float]* %A) { 239 %c = bitcast [4 x float]* %A to float* ; <float*> [#uses=1] 240 ret float* %c 241 ; CHECK: %c = getelementptr inbounds [4 x float], [4 x float]* %A, i64 0, i64 0 242 ; CHECK: ret float* %c 243 } 244 245 define i32 @test29(i32 %c1, i32 %c2) { 246 %tmp1 = trunc i32 %c1 to i8 ; <i8> [#uses=1] 247 %tmp4.mask = trunc i32 %c2 to i8 ; <i8> [#uses=1] 248 %tmp = or i8 %tmp4.mask, %tmp1 ; <i8> [#uses=1] 249 %tmp10 = zext i8 %tmp to i32 ; <i32> [#uses=1] 250 ret i32 %tmp10 251 ; CHECK: %tmp2 = or i32 %c2, %c1 252 ; CHECK: %tmp10 = and i32 %tmp2, 255 253 ; CHECK: ret i32 %tmp10 254 } 255 256 define i32 @test30(i32 %c1) { 257 %c2 = trunc i32 %c1 to i8 ; <i8> [#uses=1] 258 %c3 = xor i8 %c2, 1 ; <i8> [#uses=1] 259 %c4 = zext i8 %c3 to i32 ; <i32> [#uses=1] 260 ret i32 %c4 261 ; CHECK: %c3 = and i32 %c1, 255 262 ; CHECK: %c4 = xor i32 %c3, 1 263 ; CHECK: ret i32 %c4 264 } 265 266 define i1 @test31(i64 %A) { 267 %B = trunc i64 %A to i32 ; <i32> [#uses=1] 268 %C = and i32 %B, 42 ; <i32> [#uses=1] 269 %D = icmp eq i32 %C, 10 ; <i1> [#uses=1] 270 ret i1 %D 271 ; CHECK: %C = and i64 %A, 42 272 ; CHECK: %D = icmp eq i64 %C, 10 273 ; CHECK: ret i1 %D 274 } 275 276 define i32 @test33(i32 %c1) { 277 %x = bitcast i32 %c1 to float ; <float> [#uses=1] 278 %y = bitcast float %x to i32 ; <i32> [#uses=1] 279 ret i32 %y 280 ; CHECK: ret i32 %c1 281 } 282 283 define i16 @test34(i16 %a) { 284 %c1 = zext i16 %a to i32 ; <i32> [#uses=1] 285 %tmp21 = lshr i32 %c1, 8 ; <i32> [#uses=1] 286 %c2 = trunc i32 %tmp21 to i16 ; <i16> [#uses=1] 287 ret i16 %c2 288 ; CHECK: %tmp21 = lshr i16 %a, 8 289 ; CHECK: ret i16 %tmp21 290 } 291 292 define i16 @test35(i16 %a) { 293 %c1 = bitcast i16 %a to i16 ; <i16> [#uses=1] 294 %tmp2 = lshr i16 %c1, 8 ; <i16> [#uses=1] 295 %c2 = bitcast i16 %tmp2 to i16 ; <i16> [#uses=1] 296 ret i16 %c2 297 ; CHECK: %tmp2 = lshr i16 %a, 8 298 ; CHECK: ret i16 %tmp2 299 } 300 301 ; icmp sgt i32 %a, -1 302 ; rdar://6480391 303 define i1 @test36(i32 %a) { 304 %b = lshr i32 %a, 31 305 %c = trunc i32 %b to i8 306 %d = icmp eq i8 %c, 0 307 ret i1 %d 308 ; CHECK: %d = icmp sgt i32 %a, -1 309 ; CHECK: ret i1 %d 310 } 311 312 ; ret i1 false 313 define i1 @test37(i32 %a) { 314 %b = lshr i32 %a, 31 315 %c = or i32 %b, 512 316 %d = trunc i32 %c to i8 317 %e = icmp eq i8 %d, 11 318 ret i1 %e 319 ; CHECK: ret i1 false 320 } 321 322 define i64 @test38(i32 %a) { 323 %1 = icmp eq i32 %a, -2 324 %2 = zext i1 %1 to i8 325 %3 = xor i8 %2, 1 326 %4 = zext i8 %3 to i64 327 ret i64 %4 328 ; CHECK: %1 = icmp ne i32 %a, -2 329 ; CHECK: %2 = zext i1 %1 to i64 330 ; CHECK: ret i64 %2 331 } 332 333 define i16 @test39(i16 %a) { 334 %tmp = zext i16 %a to i32 335 %tmp21 = lshr i32 %tmp, 8 336 %tmp5 = shl i32 %tmp, 8 337 %tmp.upgrd.32 = or i32 %tmp21, %tmp5 338 %tmp.upgrd.3 = trunc i32 %tmp.upgrd.32 to i16 339 ret i16 %tmp.upgrd.3 340 ; CHECK-LABEL: @test39( 341 ; CHECK: %tmp.upgrd.32 = call i16 @llvm.bswap.i16(i16 %a) 342 ; CHECK: ret i16 %tmp.upgrd.32 343 } 344 345 define i16 @test40(i16 %a) { 346 %tmp = zext i16 %a to i32 347 %tmp21 = lshr i32 %tmp, 9 348 %tmp5 = shl i32 %tmp, 8 349 %tmp.upgrd.32 = or i32 %tmp21, %tmp5 350 %tmp.upgrd.3 = trunc i32 %tmp.upgrd.32 to i16 351 ret i16 %tmp.upgrd.3 352 ; CHECK-LABEL: @test40( 353 ; CHECK: %tmp21 = lshr i16 %a, 9 354 ; CHECK: %tmp5 = shl i16 %a, 8 355 ; CHECK: %tmp.upgrd.32 = or i16 %tmp21, %tmp5 356 ; CHECK: ret i16 %tmp.upgrd.32 357 } 358 359 ; PR1263 360 define i32* @test41(i32* %tmp1) { 361 %tmp64 = bitcast i32* %tmp1 to { i32 }* 362 %tmp65 = getelementptr { i32 }, { i32 }* %tmp64, i32 0, i32 0 363 ret i32* %tmp65 364 ; CHECK-LABEL: @test41( 365 ; CHECK: ret i32* %tmp1 366 } 367 368 define i32 addrspace(1)* @test41_addrspacecast_smaller(i32* %tmp1) { 369 %tmp64 = addrspacecast i32* %tmp1 to { i32 } addrspace(1)* 370 %tmp65 = getelementptr { i32 }, { i32 } addrspace(1)* %tmp64, i32 0, i32 0 371 ret i32 addrspace(1)* %tmp65 372 ; CHECK-LABEL: @test41_addrspacecast_smaller( 373 ; CHECK: addrspacecast i32* %tmp1 to i32 addrspace(1)* 374 ; CHECK-NEXT: ret i32 addrspace(1)* 375 } 376 377 define i32* @test41_addrspacecast_larger(i32 addrspace(1)* %tmp1) { 378 %tmp64 = addrspacecast i32 addrspace(1)* %tmp1 to { i32 }* 379 %tmp65 = getelementptr { i32 }, { i32 }* %tmp64, i32 0, i32 0 380 ret i32* %tmp65 381 ; CHECK-LABEL: @test41_addrspacecast_larger( 382 ; CHECK: addrspacecast i32 addrspace(1)* %tmp1 to i32* 383 ; CHECK-NEXT: ret i32* 384 } 385 386 define i32 @test42(i32 %X) { 387 %Y = trunc i32 %X to i8 ; <i8> [#uses=1] 388 %Z = zext i8 %Y to i32 ; <i32> [#uses=1] 389 ret i32 %Z 390 ; CHECK-LABEL: @test42( 391 ; CHECK: %Z = and i32 %X, 255 392 } 393 394 ; rdar://6598839 395 define zeroext i64 @test43(i8 zeroext %on_off) nounwind readonly { 396 %A = zext i8 %on_off to i32 397 %B = add i32 %A, -1 398 %C = sext i32 %B to i64 399 ret i64 %C ;; Should be (add (zext i8 -> i64), -1) 400 ; CHECK-LABEL: @test43( 401 ; CHECK-NEXT: %A = zext i8 %on_off to i64 402 ; CHECK-NEXT: %B = add nsw i64 %A, -1 403 ; CHECK-NEXT: ret i64 %B 404 } 405 406 define i64 @test44(i8 %T) { 407 %A = zext i8 %T to i16 408 %B = or i16 %A, 1234 409 %C = zext i16 %B to i64 410 ret i64 %C 411 ; CHECK-LABEL: @test44( 412 ; CHECK-NEXT: %A = zext i8 %T to i64 413 ; CHECK-NEXT: %B = or i64 %A, 1234 414 ; CHECK-NEXT: ret i64 %B 415 } 416 417 define i64 @test45(i8 %A, i64 %Q) { 418 %D = trunc i64 %Q to i32 ;; should be removed 419 %B = sext i8 %A to i32 420 %C = or i32 %B, %D 421 %E = zext i32 %C to i64 422 ret i64 %E 423 ; CHECK-LABEL: @test45( 424 ; CHECK-NEXT: %B = sext i8 %A to i64 425 ; CHECK-NEXT: %C = or i64 %B, %Q 426 ; CHECK-NEXT: %E = and i64 %C, 4294967295 427 ; CHECK-NEXT: ret i64 %E 428 } 429 430 431 define i64 @test46(i64 %A) { 432 %B = trunc i64 %A to i32 433 %C = and i32 %B, 42 434 %D = shl i32 %C, 8 435 %E = zext i32 %D to i64 436 ret i64 %E 437 ; CHECK-LABEL: @test46( 438 ; CHECK-NEXT: %C = shl i64 %A, 8 439 ; CHECK-NEXT: %D = and i64 %C, 10752 440 ; CHECK-NEXT: ret i64 %D 441 } 442 443 define i64 @test47(i8 %A) { 444 %B = sext i8 %A to i32 445 %C = or i32 %B, 42 446 %E = zext i32 %C to i64 447 ret i64 %E 448 ; CHECK-LABEL: @test47( 449 ; CHECK-NEXT: %B = sext i8 %A to i64 450 ; CHECK-NEXT: %C = and i64 %B, 4294967253 451 ; CHECK-NEXT: %E = or i64 %C, 42 452 ; CHECK-NEXT: ret i64 %E 453 } 454 455 define i64 @test48(i8 %A, i8 %a) { 456 %b = zext i8 %a to i32 457 %B = zext i8 %A to i32 458 %C = shl i32 %B, 8 459 %D = or i32 %C, %b 460 %E = zext i32 %D to i64 461 ret i64 %E 462 ; CHECK-LABEL: @test48( 463 ; CHECK-NEXT: %b = zext i8 %a to i64 464 ; CHECK-NEXT: %B = zext i8 %A to i64 465 ; CHECK-NEXT: %C = shl nuw nsw i64 %B, 8 466 ; CHECK-NEXT: %D = or i64 %C, %b 467 ; CHECK-NEXT: ret i64 %D 468 } 469 470 define i64 @test49(i64 %A) { 471 %B = trunc i64 %A to i32 472 %C = or i32 %B, 1 473 %D = sext i32 %C to i64 474 ret i64 %D 475 ; CHECK-LABEL: @test49( 476 ; CHECK-NEXT: %C = shl i64 %A, 32 477 ; CHECK-NEXT: ashr exact i64 %C, 32 478 ; CHECK-NEXT: %D = or i64 {{.*}}, 1 479 ; CHECK-NEXT: ret i64 %D 480 } 481 482 define i64 @test50(i64 %A) { 483 %a = lshr i64 %A, 2 484 %B = trunc i64 %a to i32 485 %D = add i32 %B, -1 486 %E = sext i32 %D to i64 487 ret i64 %E 488 ; CHECK-LABEL: @test50( 489 ; lshr+shl will be handled by DAGCombine. 490 ; CHECK-NEXT: lshr i64 %A, 2 491 ; CHECK-NEXT: shl i64 %a, 32 492 ; CHECK-NEXT: add i64 {{.*}}, -4294967296 493 ; CHECK-NEXT: %E = ashr exact i64 {{.*}}, 32 494 ; CHECK-NEXT: ret i64 %E 495 } 496 497 define i64 @test51(i64 %A, i1 %cond) { 498 %B = trunc i64 %A to i32 499 %C = and i32 %B, -2 500 %D = or i32 %B, 1 501 %E = select i1 %cond, i32 %C, i32 %D 502 %F = sext i32 %E to i64 503 ret i64 %F 504 ; CHECK-LABEL: @test51( 505 ; CHECK-NEXT: %C = and i64 %A, 4294967294 506 ; CHECK-NEXT: %D = or i64 %A, 1 507 ; CHECK-NEXT: %E = select i1 %cond, i64 %C, i64 %D 508 ; CHECK-NEXT: %sext = shl i64 %E, 32 509 ; CHECK-NEXT: %F = ashr exact i64 %sext, 32 510 ; CHECK-NEXT: ret i64 %F 511 } 512 513 define i32 @test52(i64 %A) { 514 %B = trunc i64 %A to i16 515 %C = or i16 %B, -32574 516 %D = and i16 %C, -25350 517 %E = zext i16 %D to i32 518 ret i32 %E 519 ; CHECK-LABEL: @test52( 520 ; CHECK-NEXT: %B = trunc i64 %A to i32 521 ; CHECK-NEXT: %C = and i32 %B, 7224 522 ; CHECK-NEXT: %D = or i32 %C, 32962 523 ; CHECK-NEXT: ret i32 %D 524 } 525 526 define i64 @test53(i32 %A) { 527 %B = trunc i32 %A to i16 528 %C = or i16 %B, -32574 529 %D = and i16 %C, -25350 530 %E = zext i16 %D to i64 531 ret i64 %E 532 ; CHECK-LABEL: @test53( 533 ; CHECK-NEXT: %B = zext i32 %A to i64 534 ; CHECK-NEXT: %C = and i64 %B, 7224 535 ; CHECK-NEXT: %D = or i64 %C, 32962 536 ; CHECK-NEXT: ret i64 %D 537 } 538 539 define i32 @test54(i64 %A) { 540 %B = trunc i64 %A to i16 541 %C = or i16 %B, -32574 542 %D = and i16 %C, -25350 543 %E = sext i16 %D to i32 544 ret i32 %E 545 ; CHECK-LABEL: @test54( 546 ; CHECK-NEXT: %B = trunc i64 %A to i32 547 ; CHECK-NEXT: %C = and i32 %B, 7224 548 ; CHECK-NEXT: %D = or i32 %C, -32574 549 ; CHECK-NEXT: ret i32 %D 550 } 551 552 define i64 @test55(i32 %A) { 553 %B = trunc i32 %A to i16 554 %C = or i16 %B, -32574 555 %D = and i16 %C, -25350 556 %E = sext i16 %D to i64 557 ret i64 %E 558 ; CHECK-LABEL: @test55( 559 ; CHECK-NEXT: %B = zext i32 %A to i64 560 ; CHECK-NEXT: %C = and i64 %B, 7224 561 ; CHECK-NEXT: %D = or i64 %C, -32574 562 ; CHECK-NEXT: ret i64 %D 563 } 564 565 define i64 @test56(i16 %A) nounwind { 566 %tmp353 = sext i16 %A to i32 567 %tmp354 = lshr i32 %tmp353, 5 568 %tmp355 = zext i32 %tmp354 to i64 569 ret i64 %tmp355 570 ; CHECK-LABEL: @test56( 571 ; CHECK-NEXT: %tmp353 = sext i16 %A to i64 572 ; CHECK-NEXT: %tmp354 = lshr i64 %tmp353, 5 573 ; CHECK-NEXT: %tmp355 = and i64 %tmp354, 134217727 574 ; CHECK-NEXT: ret i64 %tmp355 575 } 576 577 define i64 @test57(i64 %A) nounwind { 578 %B = trunc i64 %A to i32 579 %C = lshr i32 %B, 8 580 %E = zext i32 %C to i64 581 ret i64 %E 582 ; CHECK-LABEL: @test57( 583 ; CHECK-NEXT: %C = lshr i64 %A, 8 584 ; CHECK-NEXT: %E = and i64 %C, 16777215 585 ; CHECK-NEXT: ret i64 %E 586 } 587 588 define i64 @test58(i64 %A) nounwind { 589 %B = trunc i64 %A to i32 590 %C = lshr i32 %B, 8 591 %D = or i32 %C, 128 592 %E = zext i32 %D to i64 593 ret i64 %E 594 595 ; CHECK-LABEL: @test58( 596 ; CHECK-NEXT: %C = lshr i64 %A, 8 597 ; CHECK-NEXT: %D = and i64 %C, 16777087 598 ; CHECK-NEXT: %E = or i64 %D, 128 599 ; CHECK-NEXT: ret i64 %E 600 } 601 602 define i64 @test59(i8 %A, i8 %B) nounwind { 603 %C = zext i8 %A to i32 604 %D = shl i32 %C, 4 605 %E = and i32 %D, 48 606 %F = zext i8 %B to i32 607 %G = lshr i32 %F, 4 608 %H = or i32 %G, %E 609 %I = zext i32 %H to i64 610 ret i64 %I 611 ; CHECK-LABEL: @test59( 612 ; CHECK-NEXT: %C = zext i8 %A to i64 613 ; CHECK-NOT: i32 614 ; CHECK: %F = zext i8 %B to i64 615 ; CHECK-NOT: i32 616 ; CHECK: ret i64 %H 617 } 618 619 define <3 x i32> @test60(<4 x i32> %call4) nounwind { 620 %tmp11 = bitcast <4 x i32> %call4 to i128 621 %tmp9 = trunc i128 %tmp11 to i96 622 %tmp10 = bitcast i96 %tmp9 to <3 x i32> 623 ret <3 x i32> %tmp10 624 625 ; CHECK-LABEL: @test60( 626 ; CHECK-NEXT: shufflevector 627 ; CHECK-NEXT: ret 628 } 629 630 define <4 x i32> @test61(<3 x i32> %call4) nounwind { 631 %tmp11 = bitcast <3 x i32> %call4 to i96 632 %tmp9 = zext i96 %tmp11 to i128 633 %tmp10 = bitcast i128 %tmp9 to <4 x i32> 634 ret <4 x i32> %tmp10 635 ; CHECK-LABEL: @test61( 636 ; CHECK-NEXT: shufflevector 637 ; CHECK-NEXT: ret 638 } 639 640 define <4 x i32> @test62(<3 x float> %call4) nounwind { 641 %tmp11 = bitcast <3 x float> %call4 to i96 642 %tmp9 = zext i96 %tmp11 to i128 643 %tmp10 = bitcast i128 %tmp9 to <4 x i32> 644 ret <4 x i32> %tmp10 645 ; CHECK-LABEL: @test62( 646 ; CHECK-NEXT: bitcast 647 ; CHECK-NEXT: shufflevector 648 ; CHECK-NEXT: ret 649 } 650 651 ; PR7311 - Don't create invalid IR on scalar->vector cast. 652 define <2 x float> @test63(i64 %tmp8) nounwind { 653 entry: 654 %a = bitcast i64 %tmp8 to <2 x i32> 655 %vcvt.i = uitofp <2 x i32> %a to <2 x float> 656 ret <2 x float> %vcvt.i 657 ; CHECK-LABEL: @test63( 658 ; CHECK: bitcast 659 ; CHECK: uitofp 660 } 661 662 define <4 x float> @test64(<4 x float> %c) nounwind { 663 %t0 = bitcast <4 x float> %c to <4 x i32> 664 %t1 = bitcast <4 x i32> %t0 to <4 x float> 665 ret <4 x float> %t1 666 ; CHECK-LABEL: @test64( 667 ; CHECK-NEXT: ret <4 x float> %c 668 } 669 670 define <4 x float> @test65(<4 x float> %c) nounwind { 671 %t0 = bitcast <4 x float> %c to <2 x double> 672 %t1 = bitcast <2 x double> %t0 to <4 x float> 673 ret <4 x float> %t1 674 ; CHECK-LABEL: @test65( 675 ; CHECK-NEXT: ret <4 x float> %c 676 } 677 678 define <2 x float> @test66(<2 x float> %c) nounwind { 679 %t0 = bitcast <2 x float> %c to double 680 %t1 = bitcast double %t0 to <2 x float> 681 ret <2 x float> %t1 682 ; CHECK-LABEL: @test66( 683 ; CHECK-NEXT: ret <2 x float> %c 684 } 685 686 define float @test2c() { 687 ret float extractelement (<2 x float> bitcast (double bitcast (<2 x float> <float -1.000000e+00, float -1.000000e+00> to double) to <2 x float>), i32 0) 688 ; CHECK-LABEL: @test2c( 689 ; CHECK-NOT: extractelement 690 } 691 692 define i64 @test_mmx(<2 x i32> %c) nounwind { 693 %A = bitcast <2 x i32> %c to x86_mmx 694 %B = bitcast x86_mmx %A to <2 x i32> 695 %C = bitcast <2 x i32> %B to i64 696 ret i64 %C 697 ; CHECK-LABEL: @test_mmx( 698 ; CHECK-NOT: x86_mmx 699 } 700 701 define i64 @test_mmx_const(<2 x i32> %c) nounwind { 702 %A = bitcast <2 x i32> zeroinitializer to x86_mmx 703 %B = bitcast x86_mmx %A to <2 x i32> 704 %C = bitcast <2 x i32> %B to i64 705 ret i64 %C 706 ; CHECK-LABEL: @test_mmx_const( 707 ; CHECK-NOT: x86_mmx 708 } 709 710 ; PR12514 711 define i1 @test67(i1 %a, i32 %b) { 712 %tmp2 = zext i1 %a to i32 713 %conv6 = xor i32 %tmp2, 1 714 %and = and i32 %b, %conv6 715 %sext = shl nuw nsw i32 %and, 24 716 %neg.i = xor i32 %sext, -16777216 717 %conv.i.i = ashr exact i32 %neg.i, 24 718 %trunc = trunc i32 %conv.i.i to i8 719 %tobool.i = icmp eq i8 %trunc, 0 720 ret i1 %tobool.i 721 ; CHECK-LABEL: @test67( 722 ; CHECK: ret i1 false 723 } 724 725 %s = type { i32, i32, i16 } 726 727 define %s @test68(%s *%p, i64 %i) { 728 ; CHECK-LABEL: @test68( 729 %o = mul i64 %i, 12 730 %q = bitcast %s* %p to i8* 731 %pp = getelementptr inbounds i8, i8* %q, i64 %o 732 ; CHECK-NEXT: getelementptr %s, %s* 733 %r = bitcast i8* %pp to %s* 734 %l = load %s, %s* %r 735 ; CHECK-NEXT: load %s, %s* 736 ret %s %l 737 ; CHECK-NEXT: ret %s 738 } 739 740 ; addrspacecasts should be eliminated. 741 define %s @test68_addrspacecast(%s* %p, i64 %i) { 742 ; CHECK-LABEL: @test68_addrspacecast( 743 ; CHECK-NEXT: getelementptr %s, %s* 744 ; CHECK-NEXT: load %s, %s* 745 ; CHECK-NEXT: ret %s 746 %o = mul i64 %i, 12 747 %q = addrspacecast %s* %p to i8 addrspace(2)* 748 %pp = getelementptr inbounds i8, i8 addrspace(2)* %q, i64 %o 749 %r = addrspacecast i8 addrspace(2)* %pp to %s* 750 %l = load %s, %s* %r 751 ret %s %l 752 } 753 754 define %s @test68_addrspacecast_2(%s* %p, i64 %i) { 755 ; CHECK-LABEL: @test68_addrspacecast_2( 756 ; CHECK-NEXT: getelementptr %s, %s* %p 757 ; CHECK-NEXT: addrspacecast 758 ; CHECK-NEXT: load %s, %s addrspace(1)* 759 ; CHECK-NEXT: ret %s 760 %o = mul i64 %i, 12 761 %q = addrspacecast %s* %p to i8 addrspace(2)* 762 %pp = getelementptr inbounds i8, i8 addrspace(2)* %q, i64 %o 763 %r = addrspacecast i8 addrspace(2)* %pp to %s addrspace(1)* 764 %l = load %s, %s addrspace(1)* %r 765 ret %s %l 766 } 767 768 define %s @test68_as1(%s addrspace(1)* %p, i32 %i) { 769 ; CHECK-LABEL: @test68_as1( 770 %o = mul i32 %i, 12 771 %q = bitcast %s addrspace(1)* %p to i8 addrspace(1)* 772 %pp = getelementptr inbounds i8, i8 addrspace(1)* %q, i32 %o 773 ; CHECK-NEXT: getelementptr %s, %s addrspace(1)* 774 %r = bitcast i8 addrspace(1)* %pp to %s addrspace(1)* 775 %l = load %s, %s addrspace(1)* %r 776 ; CHECK-NEXT: load %s, %s addrspace(1)* 777 ret %s %l 778 ; CHECK-NEXT: ret %s 779 } 780 781 define double @test69(double *%p, i64 %i) { 782 ; CHECK-LABEL: @test69( 783 %o = shl nsw i64 %i, 3 784 %q = bitcast double* %p to i8* 785 %pp = getelementptr inbounds i8, i8* %q, i64 %o 786 ; CHECK-NEXT: getelementptr inbounds double, double* 787 %r = bitcast i8* %pp to double* 788 %l = load double, double* %r 789 ; CHECK-NEXT: load double, double* 790 ret double %l 791 ; CHECK-NEXT: ret double 792 } 793 794 define %s @test70(%s *%p, i64 %i) { 795 ; CHECK-LABEL: @test70( 796 %o = mul nsw i64 %i, 36 797 ; CHECK-NEXT: mul nsw i64 %i, 3 798 %q = bitcast %s* %p to i8* 799 %pp = getelementptr inbounds i8, i8* %q, i64 %o 800 ; CHECK-NEXT: getelementptr inbounds %s, %s* 801 %r = bitcast i8* %pp to %s* 802 %l = load %s, %s* %r 803 ; CHECK-NEXT: load %s, %s* 804 ret %s %l 805 ; CHECK-NEXT: ret %s 806 } 807 808 define double @test71(double *%p, i64 %i) { 809 ; CHECK-LABEL: @test71( 810 %o = shl i64 %i, 5 811 ; CHECK-NEXT: shl i64 %i, 2 812 %q = bitcast double* %p to i8* 813 %pp = getelementptr i8, i8* %q, i64 %o 814 ; CHECK-NEXT: getelementptr double, double* 815 %r = bitcast i8* %pp to double* 816 %l = load double, double* %r 817 ; CHECK-NEXT: load double, double* 818 ret double %l 819 ; CHECK-NEXT: ret double 820 } 821 822 define double @test72(double *%p, i32 %i) { 823 ; CHECK-LABEL: @test72( 824 %so = shl nsw i32 %i, 3 825 %o = sext i32 %so to i64 826 ; CHECK-NEXT: sext i32 %i to i64 827 %q = bitcast double* %p to i8* 828 %pp = getelementptr inbounds i8, i8* %q, i64 %o 829 ; CHECK-NEXT: getelementptr inbounds double, double* 830 %r = bitcast i8* %pp to double* 831 %l = load double, double* %r 832 ; CHECK-NEXT: load double, double* 833 ret double %l 834 ; CHECK-NEXT: ret double 835 } 836 837 define double @test73(double *%p, i128 %i) { 838 ; CHECK-LABEL: @test73( 839 %lo = shl nsw i128 %i, 3 840 %o = trunc i128 %lo to i64 841 ; CHECK-NEXT: trunc i128 %i to i64 842 %q = bitcast double* %p to i8* 843 %pp = getelementptr inbounds i8, i8* %q, i64 %o 844 ; CHECK-NEXT: getelementptr double, double* 845 %r = bitcast i8* %pp to double* 846 %l = load double, double* %r 847 ; CHECK-NEXT: load double, double* 848 ret double %l 849 ; CHECK-NEXT: ret double 850 } 851 852 define double @test74(double *%p, i64 %i) { 853 ; CHECK-LABEL: @test74( 854 %q = bitcast double* %p to i64* 855 %pp = getelementptr inbounds i64, i64* %q, i64 %i 856 ; CHECK-NEXT: getelementptr inbounds double, double* 857 %r = bitcast i64* %pp to double* 858 %l = load double, double* %r 859 ; CHECK-NEXT: load double, double* 860 ret double %l 861 ; CHECK-NEXT: ret double 862 } 863 864 define i32* @test75(i32* %p, i32 %x) { 865 ; CHECK-LABEL: @test75( 866 %y = shl i32 %x, 3 867 ; CHECK-NEXT: shl i32 %x, 3 868 %z = sext i32 %y to i64 869 ; CHECK-NEXT: sext i32 %y to i64 870 %q = bitcast i32* %p to i8* 871 %r = getelementptr i8, i8* %q, i64 %z 872 %s = bitcast i8* %r to i32* 873 ret i32* %s 874 } 875 876 define %s @test76(%s *%p, i64 %i, i64 %j) { 877 ; CHECK-LABEL: @test76( 878 %o = mul i64 %i, 12 879 %o2 = mul nsw i64 %o, %j 880 ; CHECK-NEXT: %o2 = mul i64 %i, %j 881 %q = bitcast %s* %p to i8* 882 %pp = getelementptr inbounds i8, i8* %q, i64 %o2 883 ; CHECK-NEXT: getelementptr %s, %s* %p, i64 %o2 884 %r = bitcast i8* %pp to %s* 885 %l = load %s, %s* %r 886 ; CHECK-NEXT: load %s, %s* 887 ret %s %l 888 ; CHECK-NEXT: ret %s 889 } 890 891 define %s @test77(%s *%p, i64 %i, i64 %j) { 892 ; CHECK-LABEL: @test77( 893 %o = mul nsw i64 %i, 36 894 %o2 = mul nsw i64 %o, %j 895 ; CHECK-NEXT: %o = mul nsw i64 %i, 3 896 ; CHECK-NEXT: %o2 = mul nsw i64 %o, %j 897 %q = bitcast %s* %p to i8* 898 %pp = getelementptr inbounds i8, i8* %q, i64 %o2 899 ; CHECK-NEXT: getelementptr inbounds %s, %s* %p, i64 %o2 900 %r = bitcast i8* %pp to %s* 901 %l = load %s, %s* %r 902 ; CHECK-NEXT: load %s, %s* 903 ret %s %l 904 ; CHECK-NEXT: ret %s 905 } 906 907 define %s @test78(%s *%p, i64 %i, i64 %j, i32 %k, i32 %l, i128 %m, i128 %n) { 908 ; CHECK-LABEL: @test78( 909 %a = mul nsw i32 %k, 36 910 ; CHECK-NEXT: mul nsw i32 %k, 3 911 %b = mul nsw i32 %a, %l 912 ; CHECK-NEXT: mul nsw i32 %a, %l 913 %c = sext i32 %b to i128 914 ; CHECK-NEXT: sext i32 %b to i128 915 %d = mul nsw i128 %c, %m 916 ; CHECK-NEXT: mul nsw i128 %c, %m 917 %e = mul i128 %d, %n 918 ; CHECK-NEXT: mul i128 %d, %n 919 %f = trunc i128 %e to i64 920 ; CHECK-NEXT: trunc i128 %e to i64 921 %g = mul nsw i64 %f, %i 922 ; CHECK-NEXT: mul i64 %f, %i 923 %h = mul nsw i64 %g, %j 924 ; CHECK-NEXT: mul i64 %g, %j 925 %q = bitcast %s* %p to i8* 926 %pp = getelementptr inbounds i8, i8* %q, i64 %h 927 ; CHECK-NEXT: getelementptr %s, %s* %p, i64 %h 928 %r = bitcast i8* %pp to %s* 929 %load = load %s, %s* %r 930 ; CHECK-NEXT: load %s, %s* 931 ret %s %load 932 ; CHECK-NEXT: ret %s 933 } 934 935 define %s @test79(%s *%p, i64 %i, i32 %j) { 936 ; CHECK-LABEL: @test79( 937 %a = mul nsw i64 %i, 36 938 ; CHECK: mul nsw i64 %i, 36 939 %b = trunc i64 %a to i32 940 %c = mul i32 %b, %j 941 %q = bitcast %s* %p to i8* 942 ; CHECK: bitcast 943 %pp = getelementptr inbounds i8, i8* %q, i32 %c 944 %r = bitcast i8* %pp to %s* 945 %l = load %s, %s* %r 946 ret %s %l 947 } 948 949 define double @test80([100 x double]* %p, i32 %i) { 950 ; CHECK-LABEL: @test80( 951 %tmp = shl nsw i32 %i, 3 952 ; CHECK-NEXT: sext i32 %i to i64 953 %q = bitcast [100 x double]* %p to i8* 954 %pp = getelementptr i8, i8* %q, i32 %tmp 955 ; CHECK-NEXT: getelementptr [100 x double], [100 x double]* 956 %r = bitcast i8* %pp to double* 957 %l = load double, double* %r 958 ; CHECK-NEXT: load double, double* 959 ret double %l 960 ; CHECK-NEXT: ret double 961 } 962 963 define double @test80_addrspacecast([100 x double] addrspace(1)* %p, i32 %i) { 964 ; CHECK-LABEL: @test80_addrspacecast( 965 ; CHECK-NEXT: getelementptr [100 x double], [100 x double] addrspace(1)* %p 966 ; CHECK-NEXT: load double, double addrspace(1)* 967 ; CHECK-NEXT: ret double 968 %tmp = shl nsw i32 %i, 3 969 %q = addrspacecast [100 x double] addrspace(1)* %p to i8 addrspace(2)* 970 %pp = getelementptr i8, i8 addrspace(2)* %q, i32 %tmp 971 %r = addrspacecast i8 addrspace(2)* %pp to double addrspace(1)* 972 %l = load double, double addrspace(1)* %r 973 ret double %l 974 } 975 976 define double @test80_addrspacecast_2([100 x double] addrspace(1)* %p, i32 %i) { 977 ; CHECK-LABEL: @test80_addrspacecast_2( 978 ; CHECK-NEXT: getelementptr [100 x double], [100 x double] addrspace(1)* 979 ; CHECK-NEXT: addrspacecast double addrspace(1)* 980 ; CHECK-NEXT: load double, double addrspace(3)* 981 ; CHECK-NEXT: ret double 982 %tmp = shl nsw i32 %i, 3 983 %q = addrspacecast [100 x double] addrspace(1)* %p to i8 addrspace(2)* 984 %pp = getelementptr i8, i8 addrspace(2)* %q, i32 %tmp 985 %r = addrspacecast i8 addrspace(2)* %pp to double addrspace(3)* 986 %l = load double, double addrspace(3)* %r 987 ret double %l 988 } 989 990 define double @test80_as1([100 x double] addrspace(1)* %p, i16 %i) { 991 ; CHECK-LABEL: @test80_as1( 992 %tmp = shl nsw i16 %i, 3 993 ; CHECK-NEXT: sext i16 %i to i32 994 %q = bitcast [100 x double] addrspace(1)* %p to i8 addrspace(1)* 995 %pp = getelementptr i8, i8 addrspace(1)* %q, i16 %tmp 996 ; CHECK-NEXT: getelementptr [100 x double], [100 x double] addrspace(1)* 997 %r = bitcast i8 addrspace(1)* %pp to double addrspace(1)* 998 %l = load double, double addrspace(1)* %r 999 ; CHECK-NEXT: load double, double addrspace(1)* 1000 ret double %l 1001 ; CHECK-NEXT: ret double 1002 } 1003 1004 define double @test81(double *%p, float %f) { 1005 %i = fptosi float %f to i64 1006 %q = bitcast double* %p to i8* 1007 %pp = getelementptr i8, i8* %q, i64 %i 1008 %r = bitcast i8* %pp to double* 1009 %l = load double, double* %r 1010 ret double %l 1011 } 1012 1013 define i64 @test82(i64 %A) nounwind { 1014 %B = trunc i64 %A to i32 1015 %C = lshr i32 %B, 8 1016 %D = shl i32 %C, 9 1017 %E = zext i32 %D to i64 1018 ret i64 %E 1019 1020 ; CHECK-LABEL: @test82( 1021 ; CHECK-NEXT: [[REG:%[0-9]*]] = shl i64 %A, 1 1022 ; CHECK-NEXT: %E = and i64 [[REG]], 4294966784 1023 ; CHECK-NEXT: ret i64 %E 1024 } 1025 1026 ; PR15959 1027 define i64 @test83(i16 %a, i64 %k) { 1028 %conv = sext i16 %a to i32 1029 %sub = add nsw i64 %k, -1 1030 %sh_prom = trunc i64 %sub to i32 1031 %shl = shl i32 %conv, %sh_prom 1032 %sh_prom1 = zext i32 %shl to i64 1033 ret i64 %sh_prom1 1034 1035 ; CHECK-LABEL: @test83( 1036 ; CHECK: %sub = add i64 %k, 4294967295 1037 ; CHECK: %sh_prom = trunc i64 %sub to i32 1038 ; CHECK: %shl = shl i32 %conv, %sh_prom 1039 } 1040 1041 define i8 @test84(i32 %a) { 1042 %add = add nsw i32 %a, -16777216 1043 %shr = lshr exact i32 %add, 23 1044 %trunc = trunc i32 %shr to i8 1045 ret i8 %trunc 1046 1047 ; CHECK-LABEL: @test84( 1048 ; CHECK: [[ADD:%.*]] = add i32 %a, 2130706432 1049 ; CHECK: [[SHR:%.*]] = lshr exact i32 [[ADD]], 23 1050 ; CHECK: [[CST:%.*]] = trunc i32 [[SHR]] to i8 1051 } 1052 1053 define i8 @test85(i32 %a) { 1054 %add = add nuw i32 %a, -16777216 1055 %shr = lshr exact i32 %add, 23 1056 %trunc = trunc i32 %shr to i8 1057 ret i8 %trunc 1058 1059 ; CHECK-LABEL: @test85( 1060 ; CHECK: [[ADD:%.*]] = add i32 %a, 2130706432 1061 ; CHECK: [[SHR:%.*]] = lshr exact i32 [[ADD]], 23 1062 ; CHECK: [[CST:%.*]] = trunc i32 [[SHR]] to i8 1063 } 1064 1065 define i16 @test86(i16 %v) { 1066 %a = sext i16 %v to i32 1067 %s = ashr i32 %a, 4 1068 %t = trunc i32 %s to i16 1069 ret i16 %t 1070 1071 ; CHECK-LABEL: @test86( 1072 ; CHECK: [[ASHR:%.*]] = ashr i16 %v, 4 1073 ; CHECK-NEXT: ret i16 [[ASHR]] 1074 } 1075 1076 define i16 @test87(i16 %v) { 1077 %c = sext i16 %v to i32 1078 %m = mul nsw i32 %c, 16 1079 %a = ashr i32 %m, 16 1080 %t = trunc i32 %a to i16 1081 ret i16 %t 1082 1083 ; CHECK-LABEL: @test87( 1084 ; CHECK: [[ASHR:%.*]] = ashr i16 %v, 12 1085 ; CHECK-NEXT: ret i16 [[ASHR]] 1086 } 1087 1088 define i16 @test88(i16 %v) { 1089 %a = sext i16 %v to i32 1090 %s = ashr i32 %a, 18 1091 %t = trunc i32 %s to i16 1092 ret i16 %t 1093 1094 ; Do not optimize to ashr i16 (shift by 18) 1095 ; CHECK-LABEL: @test88( 1096 ; CHECK: [[SEXT:%.*]] = sext i16 %v to i32 1097 ; CHECK-NEXT: [[ASHR:%.*]] = ashr i32 [[SEXT]], 18 1098 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 [[ASHR]] to i16 1099 ; CHECK-NEXT: ret i16 [[TRUNC]] 1100 } 1101 1102 ; Overflow on a float to int or int to float conversion is undefined (PR21130). 1103 1104 define i8 @overflow_fptosi() { 1105 %i = fptosi double 1.56e+02 to i8 1106 ret i8 %i 1107 ; CHECK-LABEL: @overflow_fptosi( 1108 ; CHECK-NEXT: ret i8 undef 1109 } 1110 1111 define i8 @overflow_fptoui() { 1112 %i = fptoui double 2.56e+02 to i8 1113 ret i8 %i 1114 ; CHECK-LABEL: @overflow_fptoui( 1115 ; CHECK-NEXT: ret i8 undef 1116 } 1117 1118 ; The maximum float is approximately 2 ** 128 which is 3.4E38. 1119 ; The constant below is 4E38. Use a 130 bit integer to hold that 1120 ; number; 129-bits for the value + 1 bit for the sign. 1121 define float @overflow_uitofp() { 1122 %i = uitofp i130 400000000000000000000000000000000000000 to float 1123 ret float %i 1124 ; CHECK-LABEL: @overflow_uitofp( 1125 ; CHECK-NEXT: ret float undef 1126 } 1127 1128 define float @overflow_sitofp() { 1129 %i = sitofp i130 400000000000000000000000000000000000000 to float 1130 ret float %i 1131 ; CHECK-LABEL: @overflow_sitofp( 1132 ; CHECK-NEXT: ret float undef 1133 } 1134 1135 define i32 @PR21388(i32* %v) { 1136 %icmp = icmp slt i32* %v, null 1137 %sext = sext i1 %icmp to i32 1138 ret i32 %sext 1139 ; CHECK-LABEL: @PR21388( 1140 ; CHECK-NEXT: %[[icmp:.*]] = icmp slt i32* %v, null 1141 ; CHECK-NEXT: %[[sext:.*]] = sext i1 %[[icmp]] to i32 1142 ; CHECK-NEXT: ret i32 %[[sext]] 1143 } 1144 1145 define float @sitofp_zext(i16 %a) { 1146 ; CHECK-LABEL: @sitofp_zext( 1147 ; CHECK-NEXT: %[[sitofp:.*]] = uitofp i16 %a to float 1148 ; CHECK-NEXT: ret float %[[sitofp]] 1149 %zext = zext i16 %a to i32 1150 %sitofp = sitofp i32 %zext to float 1151 ret float %sitofp 1152 } 1153 1154 define i1 @PR23309(i32 %A, i32 %B) { 1155 ; CHECK-LABEL: @PR23309( 1156 ; CHECK-NEXT: %[[sub:.*]] = sub i32 %A, %B 1157 ; CHECK-NEXT: %[[and:.*]] = and i32 %[[sub]], 1 1158 ; CHECK-NEXT: %[[cmp:.*]] = icmp ne i32 %[[and]], 0 1159 ; CHECK-NEXT: ret i1 %[[cmp]] 1160 %add = add i32 %A, -4 1161 %sub = sub nsw i32 %add, %B 1162 %trunc = trunc i32 %sub to i1 1163 ret i1 %trunc 1164 } 1165 1166 define i1 @PR23309v2(i32 %A, i32 %B) { 1167 ; CHECK-LABEL: @PR23309v2( 1168 ; CHECK-NEXT: %[[sub:.*]] = add i32 %A, %B 1169 ; CHECK-NEXT: %[[and:.*]] = and i32 %[[sub]], 1 1170 ; CHECK-NEXT: %[[cmp:.*]] = icmp ne i32 %[[and]], 0 1171 ; CHECK-NEXT: ret i1 %[[cmp]] 1172 %add = add i32 %A, -4 1173 %sub = add nuw i32 %add, %B 1174 %trunc = trunc i32 %sub to i1 1175 ret i1 %trunc 1176 } 1177 1178 define i16 @PR24763(i8 %V) { 1179 ; CHECK-LABEL: @PR24763( 1180 ; CHECK-NEXT: %[[sh:.*]] = ashr i8 1181 ; CHECK-NEXT: %[[ext:.*]] = sext i8 %[[sh]] to i16 1182 ; CHECK-NEXT: ret i16 %[[ext]] 1183 %conv = sext i8 %V to i32 1184 %l = lshr i32 %conv, 1 1185 %t = trunc i32 %l to i16 1186 ret i16 %t 1187 } 1188