load.ll 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. ; RUN: opt -instcombine -S < %s | FileCheck %s
  2. ; RUN: opt -passes=instcombine -S < %s | FileCheck %s
  3. ; This test makes sure that these instructions are properly eliminated.
  4. target datalayout = "e-m:e-p:64:64:64-i64:64-f80:128-n8:16:32:64-S128"
  5. @X = constant i32 42 ; <i32*> [#uses=2]
  6. @X2 = constant i32 47 ; <i32*> [#uses=1]
  7. @Y = constant [2 x { i32, float }] [ { i32, float } { i32 12, float 1.000000e+00 }, { i32, float } { i32 37, float 0x3FF3B2FEC0000000 } ] ; <[2 x { i32, float }]*> [#uses=2]
  8. @Z = constant [2 x { i32, float }] zeroinitializer ; <[2 x { i32, float }]*> [#uses=1]
  9. @GLOBAL = internal constant [4 x i32] zeroinitializer
  10. ; CHECK-LABEL: @test1(
  11. ; CHECK-NOT: load
  12. define i32 @test1() {
  13. %B = load i32, i32* @X ; <i32> [#uses=1]
  14. ret i32 %B
  15. }
  16. ; CHECK-LABEL: @test2(
  17. ; CHECK-NOT: load
  18. define float @test2() {
  19. %A = getelementptr [2 x { i32, float }], [2 x { i32, float }]* @Y, i64 0, i64 1, i32 1 ; <float*> [#uses=1]
  20. %B = load float, float* %A ; <float> [#uses=1]
  21. ret float %B
  22. }
  23. ; CHECK-LABEL: @test3(
  24. ; CHECK-NOT: load
  25. define i32 @test3() {
  26. %A = getelementptr [2 x { i32, float }], [2 x { i32, float }]* @Y, i64 0, i64 0, i32 0 ; <i32*> [#uses=1]
  27. %B = load i32, i32* %A ; <i32> [#uses=1]
  28. ret i32 %B
  29. }
  30. ; CHECK-LABEL: @test4(
  31. ; CHECK-NOT: load
  32. define i32 @test4() {
  33. %A = getelementptr [2 x { i32, float }], [2 x { i32, float }]* @Z, i64 0, i64 1, i32 0 ; <i32*> [#uses=1]
  34. %B = load i32, i32* %A ; <i32> [#uses=1]
  35. ret i32 %B
  36. }
  37. ; CHECK-LABEL: @test5(
  38. ; CHECK-NOT: load
  39. define i32 @test5(i1 %C) {
  40. %Y = select i1 %C, i32* @X, i32* @X2 ; <i32*> [#uses=1]
  41. %Z = load i32, i32* %Y ; <i32> [#uses=1]
  42. ret i32 %Z
  43. }
  44. ; CHECK-LABEL: @test7(
  45. ; CHECK-NOT: load
  46. define i32 @test7(i32 %X) {
  47. %V = getelementptr i32, i32* null, i32 %X ; <i32*> [#uses=1]
  48. %R = load i32, i32* %V ; <i32> [#uses=1]
  49. ret i32 %R
  50. }
  51. ; CHECK-LABEL: @test8(
  52. ; CHECK-NOT: load
  53. define i32 @test8(i32* %P) {
  54. store i32 1, i32* %P
  55. %X = load i32, i32* %P ; <i32> [#uses=1]
  56. ret i32 %X
  57. }
  58. ; CHECK-LABEL: @test9(
  59. ; CHECK-NOT: load
  60. define i32 @test9(i32* %P) {
  61. %X = load i32, i32* %P ; <i32> [#uses=1]
  62. %Y = load i32, i32* %P ; <i32> [#uses=1]
  63. %Z = sub i32 %X, %Y ; <i32> [#uses=1]
  64. ret i32 %Z
  65. }
  66. ; CHECK-LABEL: @test10(
  67. ; CHECK-NOT: load
  68. define i32 @test10(i1 %C.upgrd.1, i32* %P, i32* %Q) {
  69. br i1 %C.upgrd.1, label %T, label %F
  70. T: ; preds = %0
  71. store i32 1, i32* %Q
  72. store i32 0, i32* %P
  73. br label %C
  74. F: ; preds = %0
  75. store i32 0, i32* %P
  76. br label %C
  77. C: ; preds = %F, %T
  78. %V = load i32, i32* %P ; <i32> [#uses=1]
  79. ret i32 %V
  80. }
  81. ; CHECK-LABEL: @test11(
  82. ; CHECK-NOT: load
  83. define double @test11(double* %p) {
  84. %t0 = getelementptr double, double* %p, i32 1
  85. store double 2.0, double* %t0
  86. %t1 = getelementptr double, double* %p, i32 1
  87. %x = load double, double* %t1
  88. ret double %x
  89. }
  90. ; CHECK-LABEL: @test12(
  91. ; CHECK-NOT: load
  92. define i32 @test12(i32* %P) {
  93. %A = alloca i32
  94. store i32 123, i32* %A
  95. ; Cast the result of the load not the source
  96. %Q = bitcast i32* %A to i32*
  97. %V = load i32, i32* %Q
  98. ret i32 %V
  99. }
  100. ; CHECK-LABEL: @test13(
  101. ; CHECK-NOT: load
  102. define <16 x i8> @test13(<2 x i64> %x) {
  103. %tmp = load <16 x i8>, <16 x i8>* bitcast ([4 x i32]* @GLOBAL to <16 x i8>*)
  104. ret <16 x i8> %tmp
  105. }
  106. define i8 @test14(i8 %x, i32 %y) {
  107. ; This test must not have the store of %x forwarded to the load -- there is an
  108. ; intervening store if %y. However, the intervening store occurs with a different
  109. ; type and size and to a different pointer value. This is ensuring that none of
  110. ; those confuse the analysis into thinking that the second store does not alias
  111. ; the first.
  112. ; CHECK-LABEL: @test14(
  113. ; CHECK: %[[R:.*]] = load i8, i8*
  114. ; CHECK-NEXT: ret i8 %[[R]]
  115. %a = alloca i32
  116. %a.i8 = bitcast i32* %a to i8*
  117. store i8 %x, i8* %a.i8
  118. store i32 %y, i32* %a
  119. %r = load i8, i8* %a.i8
  120. ret i8 %r
  121. }
  122. @test15_global = external global i32
  123. define i8 @test15(i8 %x, i32 %y) {
  124. ; Same test as @test14 essentially, but using a global instead of an alloca.
  125. ; CHECK-LABEL: @test15(
  126. ; CHECK: %[[R:.*]] = load i8, i8*
  127. ; CHECK-NEXT: ret i8 %[[R]]
  128. %g.i8 = bitcast i32* @test15_global to i8*
  129. store i8 %x, i8* %g.i8
  130. store i32 %y, i32* @test15_global
  131. %r = load i8, i8* %g.i8
  132. ret i8 %r
  133. }
  134. define void @test16(i8* %x, i8* %a, i8* %b, i8* %c) {
  135. ; Check that we canonicalize loads which are only stored to use integer types
  136. ; when there is a valid integer type.
  137. ; CHECK-LABEL: @test16(
  138. ; CHECK: %[[L1:.*]] = load i32, i32*
  139. ; CHECK-NOT: load
  140. ; CHECK: store i32 %[[L1]], i32*
  141. ; CHECK: store i32 %[[L1]], i32*
  142. ; CHECK-NOT: store
  143. ; CHECK: %[[L1:.*]] = load i32, i32*
  144. ; CHECK-NOT: load
  145. ; CHECK: store i32 %[[L1]], i32*
  146. ; CHECK: store i32 %[[L1]], i32*
  147. ; CHECK-NOT: store
  148. ; CHECK: ret
  149. entry:
  150. %x.cast = bitcast i8* %x to float*
  151. %a.cast = bitcast i8* %a to float*
  152. %b.cast = bitcast i8* %b to float*
  153. %c.cast = bitcast i8* %c to i32*
  154. %x1 = load float, float* %x.cast
  155. store float %x1, float* %a.cast
  156. store float %x1, float* %b.cast
  157. %x2 = load float, float* %x.cast
  158. store float %x2, float* %b.cast
  159. %x2.cast = bitcast float %x2 to i32
  160. store i32 %x2.cast, i32* %c.cast
  161. ret void
  162. }
  163. define void @test17(i8** %x, i8 %y) {
  164. ; Check that in cases similar to @test16 we don't try to rewrite a load when
  165. ; its only use is a store but it is used as the pointer to that store rather
  166. ; than the value.
  167. ;
  168. ; CHECK-LABEL: @test17(
  169. ; CHECK: %[[L:.*]] = load i8*, i8**
  170. ; CHECK: store i8 %y, i8* %[[L]]
  171. entry:
  172. %x.load = load i8*, i8** %x
  173. store i8 %y, i8* %x.load
  174. ret void
  175. }