modref.ll 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. ; RUN: opt < %s -basicaa -gvn -dse -S | FileCheck %s
  2. target datalayout = "E-p: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"
  3. declare void @llvm.lifetime.end(i64, i8* nocapture)
  4. declare void @external(i32*)
  5. define i32 @test0(i8* %P) {
  6. %A = alloca i32
  7. call void @external(i32* %A)
  8. store i32 0, i32* %A
  9. call void @llvm.memset.p0i8.i32(i8* %P, i8 0, i32 42, i32 1, i1 false)
  10. %B = load i32, i32* %A
  11. ret i32 %B
  12. ; CHECK-LABEL: @test0
  13. ; CHECK: ret i32 0
  14. }
  15. define i8 @test1() {
  16. ; CHECK-LABEL: @test1
  17. %A = alloca i8
  18. %B = alloca i8
  19. store i8 2, i8* %B ;; Not written to by memcpy
  20. call void @llvm.memcpy.p0i8.p0i8.i8(i8* %A, i8* %B, i8 -1, i32 0, i1 false)
  21. %C = load i8, i8* %B
  22. ret i8 %C
  23. ; CHECK: ret i8 2
  24. }
  25. define i8 @test2(i8* %P) {
  26. ; CHECK-LABEL: @test2
  27. %P2 = getelementptr i8, i8* %P, i32 127
  28. store i8 1, i8* %P2 ;; Not dead across memset
  29. call void @llvm.memset.p0i8.i8(i8* %P, i8 2, i8 127, i32 0, i1 false)
  30. %A = load i8, i8* %P2
  31. ret i8 %A
  32. ; CHECK: ret i8 1
  33. }
  34. define i8 @test2a(i8* %P) {
  35. ; CHECK-LABEL: @test2
  36. %P2 = getelementptr i8, i8* %P, i32 126
  37. ;; FIXME: DSE isn't zapping this dead store.
  38. store i8 1, i8* %P2 ;; Dead, clobbered by memset.
  39. call void @llvm.memset.p0i8.i8(i8* %P, i8 2, i8 127, i32 0, i1 false)
  40. %A = load i8, i8* %P2
  41. ret i8 %A
  42. ; CHECK-NOT: load
  43. ; CHECK: ret i8 2
  44. }
  45. define void @test3(i8* %P, i8 %X) {
  46. ; CHECK-LABEL: @test3
  47. ; CHECK-NOT: store
  48. ; CHECK-NOT: %Y
  49. %Y = add i8 %X, 1 ;; Dead, because the only use (the store) is dead.
  50. %P2 = getelementptr i8, i8* %P, i32 2
  51. store i8 %Y, i8* %P2 ;; Not read by lifetime.end, should be removed.
  52. ; CHECK: store i8 2, i8* %P2
  53. call void @llvm.lifetime.end(i64 1, i8* %P)
  54. store i8 2, i8* %P2
  55. ; CHECK-NOT: store
  56. ret void
  57. ; CHECK: ret void
  58. }
  59. define void @test3a(i8* %P, i8 %X) {
  60. ; CHECK-LABEL: @test3a
  61. %Y = add i8 %X, 1 ;; Dead, because the only use (the store) is dead.
  62. %P2 = getelementptr i8, i8* %P, i32 2
  63. store i8 %Y, i8* %P2
  64. ; CHECK-NEXT: call void @llvm.lifetime.end
  65. call void @llvm.lifetime.end(i64 10, i8* %P)
  66. ret void
  67. ; CHECK-NEXT: ret void
  68. }
  69. @G1 = external global i32
  70. @G2 = external global [4000 x i32]
  71. define i32 @test4(i8* %P) {
  72. %tmp = load i32, i32* @G1
  73. call void @llvm.memset.p0i8.i32(i8* bitcast ([4000 x i32]* @G2 to i8*), i8 0, i32 4000, i32 1, i1 false)
  74. %tmp2 = load i32, i32* @G1
  75. %sub = sub i32 %tmp2, %tmp
  76. ret i32 %sub
  77. ; CHECK-LABEL: @test4
  78. ; CHECK-NOT: load
  79. ; CHECK: memset.p0i8.i32
  80. ; CHECK-NOT: load
  81. ; CHECK: ret i32 0
  82. }
  83. ; Verify that basicaa is handling variable length memcpy, knowing it doesn't
  84. ; write to G1.
  85. define i32 @test5(i8* %P, i32 %Len) {
  86. %tmp = load i32, i32* @G1
  87. call void @llvm.memcpy.p0i8.p0i8.i32(i8* bitcast ([4000 x i32]* @G2 to i8*), i8* bitcast (i32* @G1 to i8*), i32 %Len, i32 1, i1 false)
  88. %tmp2 = load i32, i32* @G1
  89. %sub = sub i32 %tmp2, %tmp
  90. ret i32 %sub
  91. ; CHECK: @test5
  92. ; CHECK-NOT: load
  93. ; CHECK: memcpy.p0i8.p0i8.i32
  94. ; CHECK-NOT: load
  95. ; CHECK: ret i32 0
  96. }
  97. define i8 @test6(i8* %p, i8* noalias %a) {
  98. %x = load i8, i8* %a
  99. %t = va_arg i8* %p, float
  100. %y = load i8, i8* %a
  101. %z = add i8 %x, %y
  102. ret i8 %z
  103. ; CHECK-LABEL: @test6
  104. ; CHECK: load i8, i8* %a
  105. ; CHECK-NOT: load
  106. ; CHECK: ret
  107. }
  108. ; PR10628
  109. declare void @test7decl(i32* nocapture %x)
  110. define i32 @test7() nounwind uwtable ssp {
  111. entry:
  112. %x = alloca i32, align 4
  113. store i32 0, i32* %x, align 4
  114. %add.ptr = getelementptr inbounds i32, i32* %x, i64 1
  115. call void @test7decl(i32* %add.ptr)
  116. %tmp = load i32, i32* %x, align 4
  117. ret i32 %tmp
  118. ; CHECK-LABEL: @test7(
  119. ; CHECK: store i32 0
  120. ; CHECK: call void @test7decl
  121. ; CHECK: load i32, i32*
  122. }
  123. ;; Check that aa correctly handles functions marked with argmemonly
  124. ;; attribute.
  125. declare i32 @func_argmemonly(i32 * %P) argmemonly
  126. ;; Can not remove redundant load, function may write to it.
  127. ; CHECK-LABEL: @test8(
  128. define i32 @test8(i32 *%P) {
  129. %V1 = load i32, i32* %P
  130. call i32 @func_argmemonly(i32* %P)
  131. %V2 = load i32, i32* %P
  132. %Diff = sub i32 %V1, %V2
  133. ret i32 %Diff
  134. ; CHECK: load
  135. ; CHECK: load
  136. ; CHECK: sub
  137. ; CHECK: ret i32 %Diff
  138. }
  139. ;; In this case load can be removed, function clobbers only %P2.
  140. ; CHECK-LABEL: @test9(
  141. define i32 @test9(i32* %P, i32* noalias %P2) {
  142. %V1 = load i32, i32* %P
  143. call i32 @func_argmemonly(i32* %P2)
  144. %V2 = load i32, i32* %P
  145. %Diff = sub i32 %V1, %V2
  146. ret i32 %Diff
  147. ; CHECK-NOT: load
  148. ; CHECK: ret i32 0
  149. }
  150. ;; In this case load can *not* be removed. Function clobers only %P2 but it may
  151. ;; alias with %P.
  152. ; CHECK-LABEL: @test10(
  153. define i32 @test10(i32* %P, i32* %P2) {
  154. %V1 = load i32, i32* %P
  155. call i32 @func_argmemonly(i32* %P2)
  156. %V2 = load i32, i32* %P
  157. %Diff = sub i32 %V1, %V2
  158. ret i32 %Diff
  159. ; CHECK: load
  160. ; CHECK: load
  161. ; CHECK: sub
  162. ; CHECK: ret i32 %Diff
  163. }
  164. declare void @llvm.memset.p0i8.i32(i8* nocapture, i8, i32, i32, i1) nounwind
  165. declare void @llvm.memset.p0i8.i8(i8* nocapture, i8, i8, i32, i1) nounwind
  166. declare void @llvm.memcpy.p0i8.p0i8.i8(i8* nocapture, i8* nocapture, i8, i32, i1) nounwind
  167. declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i32, i1) nounwind