simple.ll 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. ; RUN: opt < %s -basicaa -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.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1) nounwind
  4. declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind
  5. declare void @llvm.init.trampoline(i8*, i8*, i8*)
  6. define void @test1(i32* %Q, i32* %P) {
  7. %DEAD = load i32, i32* %Q
  8. store i32 %DEAD, i32* %P
  9. store i32 0, i32* %P
  10. ret void
  11. ; CHECK-LABEL: @test1(
  12. ; CHECK-NEXT: store i32 0, i32* %P
  13. ; CHECK-NEXT: ret void
  14. }
  15. ; PR8576 - Should delete store of 10 even though p/q are may aliases.
  16. define void @test2(i32 *%p, i32 *%q) {
  17. store i32 10, i32* %p, align 4
  18. store i32 20, i32* %q, align 4
  19. store i32 30, i32* %p, align 4
  20. ret void
  21. ; CHECK-LABEL: @test2(
  22. ; CHECK-NEXT: store i32 20
  23. }
  24. ; PR8677
  25. @g = global i32 1
  26. define i32 @test3(i32* %g_addr) nounwind {
  27. ; CHECK-LABEL: @test3(
  28. ; CHECK: load i32, i32* %g_addr
  29. %g_value = load i32, i32* %g_addr, align 4
  30. store i32 -1, i32* @g, align 4
  31. store i32 %g_value, i32* %g_addr, align 4
  32. %tmp3 = load i32, i32* @g, align 4
  33. ret i32 %tmp3
  34. }
  35. define void @test4(i32* %Q) {
  36. %a = load i32, i32* %Q
  37. store volatile i32 %a, i32* %Q
  38. ret void
  39. ; CHECK-LABEL: @test4(
  40. ; CHECK-NEXT: load i32
  41. ; CHECK-NEXT: store volatile
  42. ; CHECK-NEXT: ret void
  43. }
  44. define void @test5(i32* %Q) {
  45. %a = load volatile i32, i32* %Q
  46. store i32 %a, i32* %Q
  47. ret void
  48. ; CHECK-LABEL: @test5(
  49. ; CHECK-NEXT: load volatile
  50. ; CHECK-NEXT: ret void
  51. }
  52. ; Should delete store of 10 even though memset is a may-store to P (P and Q may
  53. ; alias).
  54. define void @test6(i32 *%p, i8 *%q) {
  55. store i32 10, i32* %p, align 4 ;; dead.
  56. call void @llvm.memset.p0i8.i64(i8* %q, i8 42, i64 900, i32 1, i1 false)
  57. store i32 30, i32* %p, align 4
  58. ret void
  59. ; CHECK-LABEL: @test6(
  60. ; CHECK-NEXT: call void @llvm.memset
  61. }
  62. ; Should delete store of 10 even though memcpy is a may-store to P (P and Q may
  63. ; alias).
  64. define void @test7(i32 *%p, i8 *%q, i8* noalias %r) {
  65. store i32 10, i32* %p, align 4 ;; dead.
  66. call void @llvm.memcpy.p0i8.p0i8.i64(i8* %q, i8* %r, i64 900, i32 1, i1 false)
  67. store i32 30, i32* %p, align 4
  68. ret void
  69. ; CHECK-LABEL: @test7(
  70. ; CHECK-NEXT: call void @llvm.memcpy
  71. }
  72. ; Do not delete stores that are only partially killed.
  73. define i32 @test8() {
  74. %V = alloca i32
  75. store i32 1234567, i32* %V
  76. %V2 = bitcast i32* %V to i8*
  77. store i8 0, i8* %V2
  78. %X = load i32, i32* %V
  79. ret i32 %X
  80. ; CHECK-LABEL: @test8(
  81. ; CHECK: store i32 1234567
  82. }
  83. ; Test for byval handling.
  84. %struct.x = type { i32, i32, i32, i32 }
  85. define void @test9(%struct.x* byval %a) nounwind {
  86. %tmp2 = getelementptr %struct.x, %struct.x* %a, i32 0, i32 0
  87. store i32 1, i32* %tmp2, align 4
  88. ret void
  89. ; CHECK-LABEL: @test9(
  90. ; CHECK-NEXT: ret void
  91. }
  92. ; Test for inalloca handling.
  93. define void @test9_2(%struct.x* inalloca %a) nounwind {
  94. %tmp2 = getelementptr %struct.x, %struct.x* %a, i32 0, i32 0
  95. store i32 1, i32* %tmp2, align 4
  96. ret void
  97. ; CHECK-LABEL: @test9_2(
  98. ; CHECK-NEXT: ret void
  99. }
  100. ; va_arg has fuzzy dependence, the store shouldn't be zapped.
  101. define double @test10(i8* %X) {
  102. %X_addr = alloca i8*
  103. store i8* %X, i8** %X_addr
  104. %tmp.0 = va_arg i8** %X_addr, double
  105. ret double %tmp.0
  106. ; CHECK-LABEL: @test10(
  107. ; CHECK: store
  108. }
  109. ; DSE should delete the dead trampoline.
  110. declare void @test11f()
  111. define void @test11() {
  112. ; CHECK-LABEL: @test11(
  113. %storage = alloca [10 x i8], align 16 ; <[10 x i8]*> [#uses=1]
  114. ; CHECK-NOT: alloca
  115. %cast = getelementptr [10 x i8], [10 x i8]* %storage, i32 0, i32 0 ; <i8*> [#uses=1]
  116. call void @llvm.init.trampoline( i8* %cast, i8* bitcast (void ()* @test11f to i8*), i8* null ) ; <i8*> [#uses=1]
  117. ; CHECK-NOT: trampoline
  118. ret void
  119. ; CHECK: ret void
  120. }
  121. ; PR2599 - load -> store to same address.
  122. define void @test12({ i32, i32 }* %x) nounwind {
  123. %tmp4 = getelementptr { i32, i32 }, { i32, i32 }* %x, i32 0, i32 0
  124. %tmp5 = load i32, i32* %tmp4, align 4
  125. %tmp7 = getelementptr { i32, i32 }, { i32, i32 }* %x, i32 0, i32 1
  126. %tmp8 = load i32, i32* %tmp7, align 4
  127. %tmp17 = sub i32 0, %tmp8
  128. store i32 %tmp5, i32* %tmp4, align 4
  129. store i32 %tmp17, i32* %tmp7, align 4
  130. ret void
  131. ; CHECK-LABEL: @test12(
  132. ; CHECK-NOT: tmp5
  133. ; CHECK: ret void
  134. }
  135. ; %P doesn't escape, the DEAD instructions should be removed.
  136. declare void @test13f()
  137. define i32* @test13() {
  138. %p = tail call i8* @malloc(i32 4)
  139. %P = bitcast i8* %p to i32*
  140. %DEAD = load i32, i32* %P
  141. %DEAD2 = add i32 %DEAD, 1
  142. store i32 %DEAD2, i32* %P
  143. call void @test13f( )
  144. store i32 0, i32* %P
  145. ret i32* %P
  146. ; CHECK: @test13()
  147. ; CHECK-NEXT: malloc
  148. ; CHECK-NEXT: bitcast
  149. ; CHECK-NEXT: call void
  150. }
  151. define i32 addrspace(1)* @test13_addrspacecast() {
  152. %p = tail call i8* @malloc(i32 4)
  153. %p.bc = bitcast i8* %p to i32*
  154. %P = addrspacecast i32* %p.bc to i32 addrspace(1)*
  155. %DEAD = load i32, i32 addrspace(1)* %P
  156. %DEAD2 = add i32 %DEAD, 1
  157. store i32 %DEAD2, i32 addrspace(1)* %P
  158. call void @test13f( )
  159. store i32 0, i32 addrspace(1)* %P
  160. ret i32 addrspace(1)* %P
  161. ; CHECK: @test13_addrspacecast()
  162. ; CHECK-NEXT: malloc
  163. ; CHECK-NEXT: bitcast
  164. ; CHECK-NEXT: addrspacecast
  165. ; CHECK-NEXT: call void
  166. }
  167. declare noalias i8* @malloc(i32)
  168. declare noalias i8* @calloc(i32, i32)
  169. define void @test14(i32* %Q) {
  170. %P = alloca i32
  171. %DEAD = load i32, i32* %Q
  172. store i32 %DEAD, i32* %P
  173. ret void
  174. ; CHECK-LABEL: @test14(
  175. ; CHECK-NEXT: ret void
  176. }
  177. ; PR8701
  178. ;; Fully dead overwrite of memcpy.
  179. define void @test15(i8* %P, i8* %Q) nounwind ssp {
  180. tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
  181. tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
  182. ret void
  183. ; CHECK-LABEL: @test15(
  184. ; CHECK-NEXT: call void @llvm.memcpy
  185. ; CHECK-NEXT: ret
  186. }
  187. ;; Full overwrite of smaller memcpy.
  188. define void @test16(i8* %P, i8* %Q) nounwind ssp {
  189. tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 8, i32 1, i1 false)
  190. tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
  191. ret void
  192. ; CHECK-LABEL: @test16(
  193. ; CHECK-NEXT: call void @llvm.memcpy
  194. ; CHECK-NEXT: ret
  195. }
  196. ;; Overwrite of memset by memcpy.
  197. define void @test17(i8* %P, i8* noalias %Q) nounwind ssp {
  198. tail call void @llvm.memset.p0i8.i64(i8* %P, i8 42, i64 8, i32 1, i1 false)
  199. tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
  200. ret void
  201. ; CHECK-LABEL: @test17(
  202. ; CHECK-NEXT: call void @llvm.memcpy
  203. ; CHECK-NEXT: ret
  204. }
  205. ; Should not delete the volatile memset.
  206. define void @test17v(i8* %P, i8* %Q) nounwind ssp {
  207. tail call void @llvm.memset.p0i8.i64(i8* %P, i8 42, i64 8, i32 1, i1 true)
  208. tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
  209. ret void
  210. ; CHECK-LABEL: @test17v(
  211. ; CHECK-NEXT: call void @llvm.memset
  212. ; CHECK-NEXT: call void @llvm.memcpy
  213. ; CHECK-NEXT: ret
  214. }
  215. ; PR8728
  216. ; Do not delete instruction where possible situation is:
  217. ; A = B
  218. ; A = A
  219. define void @test18(i8* %P, i8* %Q, i8* %R) nounwind ssp {
  220. tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
  221. tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %R, i64 12, i32 1, i1 false)
  222. ret void
  223. ; CHECK-LABEL: @test18(
  224. ; CHECK-NEXT: call void @llvm.memcpy
  225. ; CHECK-NEXT: call void @llvm.memcpy
  226. ; CHECK-NEXT: ret
  227. }
  228. ; The store here is not dead because the byval call reads it.
  229. declare void @test19f({i32}* byval align 4 %P)
  230. define void @test19({i32} * nocapture byval align 4 %arg5) nounwind ssp {
  231. bb:
  232. %tmp7 = getelementptr inbounds {i32}, {i32}* %arg5, i32 0, i32 0
  233. store i32 912, i32* %tmp7
  234. call void @test19f({i32}* byval align 4 %arg5)
  235. ret void
  236. ; CHECK-LABEL: @test19(
  237. ; CHECK: store i32 912
  238. ; CHECK: call void @test19f
  239. }
  240. define void @test20() {
  241. %m = call i8* @malloc(i32 24)
  242. store i8 0, i8* %m
  243. ret void
  244. }
  245. ; CHECK-LABEL: @test20(
  246. ; CHECK-NEXT: ret void
  247. ; CHECK-LABEL: @test21(
  248. define void @test21() {
  249. %m = call i8* @calloc(i32 9, i32 7)
  250. store i8 0, i8* %m
  251. ; CHECK-NEXT: ret void
  252. ret void
  253. }
  254. ; CHECK-LABEL: @test22(
  255. define void @test22(i1 %i, i32 %k, i32 %m) nounwind {
  256. %k.addr = alloca i32
  257. %m.addr = alloca i32
  258. %k.addr.m.addr = select i1 %i, i32* %k.addr, i32* %m.addr
  259. store i32 0, i32* %k.addr.m.addr, align 4
  260. ; CHECK-NEXT: ret void
  261. ret void
  262. }
  263. ; PR13547
  264. ; CHECK-LABEL: @test23(
  265. ; CHECK: store i8 97
  266. ; CHECK: store i8 0
  267. declare noalias i8* @strdup(i8* nocapture) nounwind
  268. define noalias i8* @test23() nounwind uwtable ssp {
  269. %x = alloca [2 x i8], align 1
  270. %arrayidx = getelementptr inbounds [2 x i8], [2 x i8]* %x, i64 0, i64 0
  271. store i8 97, i8* %arrayidx, align 1
  272. %arrayidx1 = getelementptr inbounds [2 x i8], [2 x i8]* %x, i64 0, i64 1
  273. store i8 0, i8* %arrayidx1, align 1
  274. %call = call i8* @strdup(i8* %arrayidx) nounwind
  275. ret i8* %call
  276. }
  277. ; Make sure same sized store to later element is deleted
  278. ; CHECK-LABEL: @test24(
  279. ; CHECK-NOT: store i32 0
  280. ; CHECK-NOT: store i32 0
  281. ; CHECK: store i32 %b
  282. ; CHECK: store i32 %c
  283. ; CHECK: ret void
  284. define void @test24([2 x i32]* %a, i32 %b, i32 %c) nounwind {
  285. %1 = getelementptr inbounds [2 x i32], [2 x i32]* %a, i64 0, i64 0
  286. store i32 0, i32* %1, align 4
  287. %2 = getelementptr inbounds [2 x i32], [2 x i32]* %a, i64 0, i64 1
  288. store i32 0, i32* %2, align 4
  289. %3 = getelementptr inbounds [2 x i32], [2 x i32]* %a, i64 0, i64 0
  290. store i32 %b, i32* %3, align 4
  291. %4 = getelementptr inbounds [2 x i32], [2 x i32]* %a, i64 0, i64 1
  292. store i32 %c, i32* %4, align 4
  293. ret void
  294. }
  295. ; Check another case like PR13547 where strdup is not like malloc.
  296. ; CHECK-LABEL: @test25(
  297. ; CHECK: load i8
  298. ; CHECK: store i8 0
  299. ; CHECK: store i8 %tmp
  300. define i8* @test25(i8* %p) nounwind {
  301. %p.4 = getelementptr i8, i8* %p, i64 4
  302. %tmp = load i8, i8* %p.4, align 1
  303. store i8 0, i8* %p.4, align 1
  304. %q = call i8* @strdup(i8* %p) nounwind optsize
  305. store i8 %tmp, i8* %p.4, align 1
  306. ret i8* %q
  307. }