basic.ll 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. ; RUN: opt < %s -S -early-cse | FileCheck %s
  2. ; RUN: opt < %s -S -passes=early-cse | FileCheck %s
  3. declare void @llvm.assume(i1) nounwind
  4. ; CHECK-LABEL: @test1(
  5. define void @test1(i8 %V, i32 *%P) {
  6. %A = bitcast i64 42 to double ;; dead
  7. %B = add i32 4, 19 ;; constant folds
  8. store i32 %B, i32* %P
  9. ; CHECK-NEXT: store i32 23, i32* %P
  10. %C = zext i8 %V to i32
  11. %D = zext i8 %V to i32 ;; CSE
  12. store volatile i32 %C, i32* %P
  13. store volatile i32 %D, i32* %P
  14. ; CHECK-NEXT: %C = zext i8 %V to i32
  15. ; CHECK-NEXT: store volatile i32 %C
  16. ; CHECK-NEXT: store volatile i32 %C
  17. %E = add i32 %C, %C
  18. %F = add i32 %C, %C
  19. store volatile i32 %E, i32* %P
  20. store volatile i32 %F, i32* %P
  21. ; CHECK-NEXT: %E = add i32 %C, %C
  22. ; CHECK-NEXT: store volatile i32 %E
  23. ; CHECK-NEXT: store volatile i32 %E
  24. %G = add nuw i32 %C, %C ;; not a CSE with E
  25. store volatile i32 %G, i32* %P
  26. ; CHECK-NEXT: %G = add nuw i32 %C, %C
  27. ; CHECK-NEXT: store volatile i32 %G
  28. ret void
  29. }
  30. ;; Simple load value numbering.
  31. ; CHECK-LABEL: @test2(
  32. define i32 @test2(i32 *%P) {
  33. %V1 = load i32, i32* %P
  34. %V2 = load i32, i32* %P
  35. %Diff = sub i32 %V1, %V2
  36. ret i32 %Diff
  37. ; CHECK: ret i32 0
  38. }
  39. ; CHECK-LABEL: @test2a(
  40. define i32 @test2a(i32 *%P, i1 %b) {
  41. %V1 = load i32, i32* %P
  42. tail call void @llvm.assume(i1 %b)
  43. %V2 = load i32, i32* %P
  44. %Diff = sub i32 %V1, %V2
  45. ret i32 %Diff
  46. ; CHECK: ret i32 0
  47. }
  48. ;; Cross block load value numbering.
  49. ; CHECK-LABEL: @test3(
  50. define i32 @test3(i32 *%P, i1 %Cond) {
  51. %V1 = load i32, i32* %P
  52. br i1 %Cond, label %T, label %F
  53. T:
  54. store i32 4, i32* %P
  55. ret i32 42
  56. F:
  57. %V2 = load i32, i32* %P
  58. %Diff = sub i32 %V1, %V2
  59. ret i32 %Diff
  60. ; CHECK: F:
  61. ; CHECK: ret i32 0
  62. }
  63. ; CHECK-LABEL: @test3a(
  64. define i32 @test3a(i32 *%P, i1 %Cond, i1 %b) {
  65. %V1 = load i32, i32* %P
  66. br i1 %Cond, label %T, label %F
  67. T:
  68. store i32 4, i32* %P
  69. ret i32 42
  70. F:
  71. tail call void @llvm.assume(i1 %b)
  72. %V2 = load i32, i32* %P
  73. %Diff = sub i32 %V1, %V2
  74. ret i32 %Diff
  75. ; CHECK: F:
  76. ; CHECK: ret i32 0
  77. }
  78. ;; Cross block load value numbering stops when stores happen.
  79. ; CHECK-LABEL: @test4(
  80. define i32 @test4(i32 *%P, i1 %Cond) {
  81. %V1 = load i32, i32* %P
  82. br i1 %Cond, label %T, label %F
  83. T:
  84. ret i32 42
  85. F:
  86. ; Clobbers V1
  87. store i32 42, i32* %P
  88. %V2 = load i32, i32* %P
  89. %Diff = sub i32 %V1, %V2
  90. ret i32 %Diff
  91. ; CHECK: F:
  92. ; CHECK: ret i32 %Diff
  93. }
  94. declare i32 @func(i32 *%P) readonly
  95. ;; Simple call CSE'ing.
  96. ; CHECK-LABEL: @test5(
  97. define i32 @test5(i32 *%P) {
  98. %V1 = call i32 @func(i32* %P)
  99. %V2 = call i32 @func(i32* %P)
  100. %Diff = sub i32 %V1, %V2
  101. ret i32 %Diff
  102. ; CHECK: ret i32 0
  103. }
  104. ;; Trivial Store->load forwarding
  105. ; CHECK-LABEL: @test6(
  106. define i32 @test6(i32 *%P) {
  107. store i32 42, i32* %P
  108. %V1 = load i32, i32* %P
  109. ret i32 %V1
  110. ; CHECK: ret i32 42
  111. }
  112. ; CHECK-LABEL: @test6a(
  113. define i32 @test6a(i32 *%P, i1 %b) {
  114. store i32 42, i32* %P
  115. tail call void @llvm.assume(i1 %b)
  116. %V1 = load i32, i32* %P
  117. ret i32 %V1
  118. ; CHECK: ret i32 42
  119. }
  120. ;; Trivial dead store elimination.
  121. ; CHECK-LABEL: @test7(
  122. define void @test7(i32 *%P) {
  123. store i32 42, i32* %P
  124. store i32 45, i32* %P
  125. ret void
  126. ; CHECK-NEXT: store i32 45
  127. ; CHECK-NEXT: ret void
  128. }
  129. ;; Readnone functions aren't invalidated by stores.
  130. ; CHECK-LABEL: @test8(
  131. define i32 @test8(i32 *%P) {
  132. %V1 = call i32 @func(i32* %P) readnone
  133. store i32 4, i32* %P
  134. %V2 = call i32 @func(i32* %P) readnone
  135. %Diff = sub i32 %V1, %V2
  136. ret i32 %Diff
  137. ; CHECK: ret i32 0
  138. }
  139. ;; Trivial DSE can't be performed across a readonly call. The call
  140. ;; can observe the earlier write.
  141. ; CHECK-LABEL: @test9(
  142. define i32 @test9(i32 *%P) {
  143. store i32 4, i32* %P
  144. %V1 = call i32 @func(i32* %P) readonly
  145. store i32 5, i32* %P
  146. ret i32 %V1
  147. ; CHECK: store i32 4, i32* %P
  148. ; CHECK-NEXT: %V1 = call i32 @func(i32* %P)
  149. ; CHECK-NEXT: store i32 5, i32* %P
  150. ; CHECK-NEXT: ret i32 %V1
  151. }
  152. ;; Trivial DSE can be performed across a readnone call.
  153. ; CHECK-LABEL: @test10
  154. define i32 @test10(i32 *%P) {
  155. store i32 4, i32* %P
  156. %V1 = call i32 @func(i32* %P) readnone
  157. store i32 5, i32* %P
  158. ret i32 %V1
  159. ; CHECK-NEXT: %V1 = call i32 @func(i32* %P)
  160. ; CHECK-NEXT: store i32 5, i32* %P
  161. ; CHECK-NEXT: ret i32 %V1
  162. }
  163. ;; Trivial dead store elimination - should work for an entire series of dead stores too.
  164. ; CHECK-LABEL: @test11(
  165. define void @test11(i32 *%P) {
  166. store i32 42, i32* %P
  167. store i32 43, i32* %P
  168. store i32 44, i32* %P
  169. store i32 45, i32* %P
  170. ret void
  171. ; CHECK-NEXT: store i32 45
  172. ; CHECK-NEXT: ret void
  173. }
  174. ; CHECK-LABEL: @test12(
  175. define i32 @test12(i1 %B, i32* %P1, i32* %P2) {
  176. %load0 = load i32, i32* %P1
  177. %1 = load atomic i32, i32* %P2 seq_cst, align 4
  178. %load1 = load i32, i32* %P1
  179. %sel = select i1 %B, i32 %load0, i32 %load1
  180. ret i32 %sel
  181. ; CHECK: load i32, i32* %P1
  182. ; CHECK: load i32, i32* %P1
  183. }