ptr-diff.ll 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ; RUN: opt -inline < %s -S -o - -inline-threshold=10 | FileCheck %s
  2. target datalayout = "p:32:32-p1:64:64-p2:16:16-n16:32:64"
  3. define i32 @outer1() {
  4. ; CHECK-LABEL: @outer1(
  5. ; CHECK-NOT: call
  6. ; CHECK: ret i32
  7. %ptr = alloca i32
  8. %ptr1 = getelementptr inbounds i32, i32* %ptr, i32 0
  9. %ptr2 = getelementptr inbounds i32, i32* %ptr, i32 42
  10. %result = call i32 @inner1(i32* %ptr1, i32* %ptr2)
  11. ret i32 %result
  12. }
  13. define i32 @inner1(i32* %begin, i32* %end) {
  14. %begin.i = ptrtoint i32* %begin to i32
  15. %end.i = ptrtoint i32* %end to i32
  16. %distance = sub i32 %end.i, %begin.i
  17. %icmp = icmp sle i32 %distance, 42
  18. br i1 %icmp, label %then, label %else
  19. then:
  20. ret i32 3
  21. else:
  22. %t = load i32, i32* %begin
  23. ret i32 %t
  24. }
  25. define i32 @outer2(i32* %ptr) {
  26. ; Test that an inbounds GEP disables this -- it isn't safe in general as
  27. ; wrapping changes the behavior of lessthan and greaterthan comparisons.
  28. ; CHECK-LABEL: @outer2(
  29. ; CHECK: call i32 @inner2
  30. ; CHECK: ret i32
  31. %ptr1 = getelementptr i32, i32* %ptr, i32 0
  32. %ptr2 = getelementptr i32, i32* %ptr, i32 42
  33. %result = call i32 @inner2(i32* %ptr1, i32* %ptr2)
  34. ret i32 %result
  35. }
  36. define i32 @inner2(i32* %begin, i32* %end) {
  37. %begin.i = ptrtoint i32* %begin to i32
  38. %end.i = ptrtoint i32* %end to i32
  39. %distance = sub i32 %end.i, %begin.i
  40. %icmp = icmp sle i32 %distance, 42
  41. br i1 %icmp, label %then, label %else
  42. then:
  43. ret i32 3
  44. else:
  45. %t = load i32, i32* %begin
  46. ret i32 %t
  47. }
  48. ; The inttoptrs are free since it is a smaller integer to a larger
  49. ; pointer size
  50. define i32 @inttoptr_free_cost(i32 %a, i32 %b, i32 %c) {
  51. %p1 = inttoptr i32 %a to i32 addrspace(1)*
  52. %p2 = inttoptr i32 %b to i32 addrspace(1)*
  53. %p3 = inttoptr i32 %c to i32 addrspace(1)*
  54. %t1 = load i32, i32 addrspace(1)* %p1
  55. %t2 = load i32, i32 addrspace(1)* %p2
  56. %t3 = load i32, i32 addrspace(1)* %p3
  57. %s = add i32 %t1, %t2
  58. %s1 = add i32 %s, %t3
  59. ret i32 %s1
  60. }
  61. define i32 @inttoptr_free_cost_user(i32 %begin, i32 %end) {
  62. ; CHECK-LABEL: @inttoptr_free_cost_user(
  63. ; CHECK-NOT: call
  64. %x = call i32 @inttoptr_free_cost(i32 %begin, i32 %end, i32 9)
  65. ret i32 %x
  66. }
  67. ; The inttoptrs have a cost since it is a larger integer to a smaller
  68. ; pointer size
  69. define i32 @inttoptr_cost_smaller_ptr(i32 %a, i32 %b, i32 %c) {
  70. %p1 = inttoptr i32 %a to i32 addrspace(2)*
  71. %p2 = inttoptr i32 %b to i32 addrspace(2)*
  72. %p3 = inttoptr i32 %c to i32 addrspace(2)*
  73. %t1 = load i32, i32 addrspace(2)* %p1
  74. %t2 = load i32, i32 addrspace(2)* %p2
  75. %t3 = load i32, i32 addrspace(2)* %p3
  76. %s = add i32 %t1, %t2
  77. %s1 = add i32 %s, %t3
  78. ret i32 %s1
  79. }
  80. define i32 @inttoptr_cost_smaller_ptr_user(i32 %begin, i32 %end) {
  81. ; CHECK-LABEL: @inttoptr_cost_smaller_ptr_user(
  82. ; CHECK: call
  83. %x = call i32 @inttoptr_cost_smaller_ptr(i32 %begin, i32 %end, i32 9)
  84. ret i32 %x
  85. }