basic.ll 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. ; RUN: opt < %s -basicaa -sink -S | FileCheck %s
  2. @A = external global i32
  3. @B = external global i32
  4. ; Sink should sink the load past the store (which doesn't overlap) into
  5. ; the block that uses it.
  6. ; CHECK-LABEL: @foo(
  7. ; CHECK: true:
  8. ; CHECK-NEXT: %l = load i32, i32* @A
  9. ; CHECK-NEXT: ret i32 %l
  10. define i32 @foo(i1 %z) {
  11. %l = load i32, i32* @A
  12. store i32 0, i32* @B
  13. br i1 %z, label %true, label %false
  14. true:
  15. ret i32 %l
  16. false:
  17. ret i32 0
  18. }
  19. ; But don't sink load volatiles...
  20. ; CHECK-LABEL: @foo2(
  21. ; CHECK: load volatile
  22. ; CHECK-NEXT: store i32
  23. define i32 @foo2(i1 %z) {
  24. %l = load volatile i32, i32* @A
  25. store i32 0, i32* @B
  26. br i1 %z, label %true, label %false
  27. true:
  28. ret i32 %l
  29. false:
  30. ret i32 0
  31. }
  32. ; Sink to the nearest post-dominator
  33. ; CHECK-LABEL: @diamond(
  34. ; CHECK: X:
  35. ; CHECK-NEXT: phi
  36. ; CHECK-NEXT: mul nsw
  37. ; CHECK-NEXT: sub
  38. define i32 @diamond(i32 %a, i32 %b, i32 %c) {
  39. %1 = mul nsw i32 %c, %b
  40. %2 = icmp sgt i32 %a, 0
  41. br i1 %2, label %B0, label %B1
  42. B0: ; preds = %0
  43. br label %X
  44. B1: ; preds = %0
  45. br label %X
  46. X: ; preds = %5, %3
  47. %.01 = phi i32 [ %c, %B0 ], [ %a, %B1 ]
  48. %R = sub i32 %1, %.01
  49. ret i32 %R
  50. }
  51. ; We shouldn't sink constant sized allocas from the entry block, since CodeGen
  52. ; interprets allocas outside the entry block as dynamically sized stack objects.
  53. ; CHECK-LABEL: @alloca_nosink
  54. ; CHECK: entry:
  55. ; CHECK-NEXT: alloca
  56. define i32 @alloca_nosink(i32 %a, i32 %b) {
  57. entry:
  58. %0 = alloca i32
  59. %1 = icmp ne i32 %a, 0
  60. br i1 %1, label %if, label %endif
  61. if:
  62. %2 = getelementptr i32, i32* %0, i32 1
  63. store i32 0, i32* %0
  64. store i32 1, i32* %2
  65. %3 = getelementptr i32, i32* %0, i32 %b
  66. %4 = load i32, i32* %3
  67. ret i32 %4
  68. endif:
  69. ret i32 0
  70. }
  71. ; Make sure we sink dynamic sized allocas
  72. ; CHECK-LABEL: @alloca_sink_dynamic
  73. ; CHECK: entry:
  74. ; CHECK-NOT: alloca
  75. ; CHECK: if:
  76. ; CHECK-NEXT: alloca
  77. define i32 @alloca_sink_dynamic(i32 %a, i32 %b, i32 %size) {
  78. entry:
  79. %0 = alloca i32, i32 %size
  80. %1 = icmp ne i32 %a, 0
  81. br i1 %1, label %if, label %endif
  82. if:
  83. %2 = getelementptr i32, i32* %0, i32 1
  84. store i32 0, i32* %0
  85. store i32 1, i32* %2
  86. %3 = getelementptr i32, i32* %0, i32 %b
  87. %4 = load i32, i32* %3
  88. ret i32 %4
  89. endif:
  90. ret i32 0
  91. }
  92. ; We also want to sink allocas that are not in the entry block. These
  93. ; will already be considered as dynamically sized stack objects, so sinking
  94. ; them does no further damage.
  95. ; CHECK-LABEL: @alloca_sink_nonentry
  96. ; CHECK: if0:
  97. ; CHECK-NOT: alloca
  98. ; CHECK: if:
  99. ; CHECK-NEXT: alloca
  100. define i32 @alloca_sink_nonentry(i32 %a, i32 %b, i32 %c) {
  101. entry:
  102. %cmp = icmp ne i32 %c, 0
  103. br i1 %cmp, label %endif, label %if0
  104. if0:
  105. %0 = alloca i32
  106. %1 = icmp ne i32 %a, 0
  107. br i1 %1, label %if, label %endif
  108. if:
  109. %2 = getelementptr i32, i32* %0, i32 1
  110. store i32 0, i32* %0
  111. store i32 1, i32* %2
  112. %3 = getelementptr i32, i32* %0, i32 %b
  113. %4 = load i32, i32* %3
  114. ret i32 %4
  115. endif:
  116. ret i32 0
  117. }