cast_ptr.ll 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ; Tests to make sure elimination of casts is working correctly
  2. ; RUN: opt < %s -instcombine -S | FileCheck %s
  3. target datalayout = "p:32:32-p1:32:32-p2:16:16"
  4. @global = global i8 0
  5. ; This shouldn't convert to getelementptr because the relationship
  6. ; between the arithmetic and the layout of allocated memory is
  7. ; entirely unknown.
  8. ; CHECK-LABEL: @test1(
  9. ; CHECK: ptrtoint
  10. ; CHECK: add
  11. ; CHECK: inttoptr
  12. define i8* @test1(i8* %t) {
  13. %tmpc = ptrtoint i8* %t to i32 ; <i32> [#uses=1]
  14. %tmpa = add i32 %tmpc, 32 ; <i32> [#uses=1]
  15. %tv = inttoptr i32 %tmpa to i8* ; <i8*> [#uses=1]
  16. ret i8* %tv
  17. }
  18. ; These casts should be folded away.
  19. ; CHECK-LABEL: @test2(
  20. ; CHECK: icmp eq i8* %a, %b
  21. define i1 @test2(i8* %a, i8* %b) {
  22. %tmpa = ptrtoint i8* %a to i32 ; <i32> [#uses=1]
  23. %tmpb = ptrtoint i8* %b to i32 ; <i32> [#uses=1]
  24. %r = icmp eq i32 %tmpa, %tmpb ; <i1> [#uses=1]
  25. ret i1 %r
  26. }
  27. ; These casts should be folded away.
  28. ; CHECK-LABEL: @test2_as2_same_int(
  29. ; CHECK: icmp eq i8 addrspace(2)* %a, %b
  30. define i1 @test2_as2_same_int(i8 addrspace(2)* %a, i8 addrspace(2)* %b) {
  31. %tmpa = ptrtoint i8 addrspace(2)* %a to i16
  32. %tmpb = ptrtoint i8 addrspace(2)* %b to i16
  33. %r = icmp eq i16 %tmpa, %tmpb
  34. ret i1 %r
  35. }
  36. ; These casts should be folded away.
  37. ; CHECK-LABEL: @test2_as2_larger(
  38. ; CHECK: icmp eq i8 addrspace(2)* %a, %b
  39. define i1 @test2_as2_larger(i8 addrspace(2)* %a, i8 addrspace(2)* %b) {
  40. %tmpa = ptrtoint i8 addrspace(2)* %a to i32
  41. %tmpb = ptrtoint i8 addrspace(2)* %b to i32
  42. %r = icmp eq i32 %tmpa, %tmpb
  43. ret i1 %r
  44. }
  45. ; These casts should not be folded away.
  46. ; CHECK-LABEL: @test2_diff_as
  47. ; CHECK: icmp sge i32 %i0, %i1
  48. define i1 @test2_diff_as(i8* %p, i8 addrspace(1)* %q) {
  49. %i0 = ptrtoint i8* %p to i32
  50. %i1 = ptrtoint i8 addrspace(1)* %q to i32
  51. %r0 = icmp sge i32 %i0, %i1
  52. ret i1 %r0
  53. }
  54. ; These casts should not be folded away.
  55. ; CHECK-LABEL: @test2_diff_as_global
  56. ; CHECK: icmp sge i32 %i1
  57. define i1 @test2_diff_as_global(i8 addrspace(1)* %q) {
  58. %i0 = ptrtoint i8* @global to i32
  59. %i1 = ptrtoint i8 addrspace(1)* %q to i32
  60. %r0 = icmp sge i32 %i1, %i0
  61. ret i1 %r0
  62. }
  63. ; These casts should also be folded away.
  64. ; CHECK-LABEL: @test3(
  65. ; CHECK: icmp eq i8* %a, @global
  66. define i1 @test3(i8* %a) {
  67. %tmpa = ptrtoint i8* %a to i32
  68. %r = icmp eq i32 %tmpa, ptrtoint (i8* @global to i32)
  69. ret i1 %r
  70. }
  71. define i1 @test4(i32 %A) {
  72. %B = inttoptr i32 %A to i8*
  73. %C = icmp eq i8* %B, null
  74. ret i1 %C
  75. ; CHECK-LABEL: @test4(
  76. ; CHECK-NEXT: %C = icmp eq i32 %A, 0
  77. ; CHECK-NEXT: ret i1 %C
  78. }
  79. define i1 @test4_as2(i16 %A) {
  80. ; CHECK-LABEL: @test4_as2(
  81. ; CHECK-NEXT: %C = icmp eq i16 %A, 0
  82. ; CHECK-NEXT: ret i1 %C
  83. %B = inttoptr i16 %A to i8 addrspace(2)*
  84. %C = icmp eq i8 addrspace(2)* %B, null
  85. ret i1 %C
  86. }
  87. ; Pulling the cast out of the load allows us to eliminate the load, and then
  88. ; the whole array.
  89. %op = type { float }
  90. %unop = type { i32 }
  91. @Array = internal constant [1 x %op* (%op*)*] [ %op* (%op*)* @foo ] ; <[1 x %op* (%op*)*]*> [#uses=1]
  92. declare %op* @foo(%op* %X)
  93. define %unop* @test5(%op* %O) {
  94. %tmp = load %unop* (%op*)*, %unop* (%op*)** bitcast ([1 x %op* (%op*)*]* @Array to %unop* (%op*)**); <%unop* (%op*)*> [#uses=1]
  95. %tmp.2 = call %unop* %tmp( %op* %O ) ; <%unop*> [#uses=1]
  96. ret %unop* %tmp.2
  97. ; CHECK-LABEL: @test5(
  98. ; CHECK: call %op* @foo(%op* %O)
  99. }
  100. ; InstCombine can not 'load (cast P)' -> cast (load P)' if the cast changes
  101. ; the address space.
  102. define i8 @test6(i8 addrspace(1)* %source) {
  103. entry:
  104. %arrayidx223 = addrspacecast i8 addrspace(1)* %source to i8*
  105. %tmp4 = load i8, i8* %arrayidx223
  106. ret i8 %tmp4
  107. ; CHECK-LABEL: @test6(
  108. ; CHECK: load i8, i8* %arrayidx223
  109. }