invariant-load.ll 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ; Test if the !invariant.load metadata is maintained by GVN.
  2. ; RUN: opt -basicaa -gvn -S < %s | FileCheck %s
  3. define i32 @test1(i32* nocapture %p, i8* nocapture %q) {
  4. ; CHECK-LABEL: test1
  5. ; CHECK: %x = load i32, i32* %p, align 4, !invariant.load !0
  6. ; CHECK-NOT: %y = load
  7. entry:
  8. %x = load i32, i32* %p, align 4, !invariant.load !0
  9. %conv = trunc i32 %x to i8
  10. store i8 %conv, i8* %q, align 1
  11. %y = load i32, i32* %p, align 4, !invariant.load !0
  12. %add = add i32 %y, 1
  13. ret i32 %add
  14. }
  15. define i32 @test2(i32* nocapture %p, i8* nocapture %q) {
  16. ; CHECK-LABEL: test2
  17. ; CHECK-NOT: !invariant.load
  18. ; CHECK-NOT: %y = load
  19. entry:
  20. %x = load i32, i32* %p, align 4
  21. %conv = trunc i32 %x to i8
  22. store i8 %conv, i8* %q, align 1
  23. %y = load i32, i32* %p, align 4, !invariant.load !0
  24. %add = add i32 %y, 1
  25. ret i32 %add
  26. }
  27. ; With the invariant.load metadata, what would otherwise
  28. ; be a case for PRE becomes a full redundancy.
  29. define i32 @test3(i1 %cnd, i32* %p, i32* %q) {
  30. ; CHECK-LABEL: test3
  31. ; CHECK-NOT: load
  32. entry:
  33. %v1 = load i32, i32* %p
  34. br i1 %cnd, label %bb1, label %bb2
  35. bb1:
  36. store i32 5, i32* %q
  37. br label %bb2
  38. bb2:
  39. %v2 = load i32, i32* %p, !invariant.load !0
  40. %res = sub i32 %v1, %v2
  41. ret i32 %res
  42. }
  43. ; This test is here to document a case which doesn't optimize
  44. ; as well as it could.
  45. define i32 @test4(i1 %cnd, i32* %p, i32* %q) {
  46. ; CHECK-LABEL: test4
  47. ; %v2 is redundant, but GVN currently doesn't catch that
  48. entry:
  49. %v1 = load i32, i32* %p, !invariant.load !0
  50. br i1 %cnd, label %bb1, label %bb2
  51. bb1:
  52. store i32 5, i32* %q
  53. br label %bb2
  54. bb2:
  55. %v2 = load i32, i32* %p
  56. %res = sub i32 %v1, %v2
  57. ret i32 %res
  58. }
  59. ; Checks that we return the mustalias store as a def
  60. ; so that it contributes to value forwarding. Note
  61. ; that we could and should remove the store too.
  62. define i32 @test5(i1 %cnd, i32* %p) {
  63. ; CHECK-LABEL: test5
  64. ; CHECK-LABEL: entry:
  65. ; CHECK-NEXT: store i32 5, i32* %p
  66. ; CHECK-NEXT: ret i32 5
  67. entry:
  68. %v1 = load i32, i32* %p, !invariant.load !0
  69. store i32 5, i32* %p ;; must alias store, want to exploit
  70. %v2 = load i32, i32* %p, !invariant.load !0
  71. ret i32 %v2
  72. }
  73. declare void @foo()
  74. ; Clobbering (mayalias) stores, even in function calls, can be ignored
  75. define i32 @test6(i1 %cnd, i32* %p) {
  76. ; CHECK-LABEL: test6
  77. ; CHECK-LABEL: entry:
  78. ; CHECK-NEXT: @foo
  79. ; CHECK-NEXT: ret i32 0
  80. entry:
  81. %v1 = load i32, i32* %p, !invariant.load !0
  82. call void @foo()
  83. %v2 = load i32, i32* %p, !invariant.load !0
  84. %res = sub i32 %v1, %v2
  85. ret i32 %res
  86. }
  87. declare noalias i32* @bar(...)
  88. ; Same as previous, but a function with a noalias result (since they're handled
  89. ; differently in MDA)
  90. define i32 @test7(i1 %cnd, i32* %p) {
  91. ; CHECK-LABEL: test7
  92. ; CHECK-LABEL: entry:
  93. ; CHECK-NEXT: @bar
  94. ; CHECK-NEXT: ret i32 0
  95. entry:
  96. %v1 = load i32, i32* %p, !invariant.load !0
  97. call i32* (...) @bar(i32* %p)
  98. %v2 = load i32, i32* %p, !invariant.load !0
  99. %res = sub i32 %v1, %v2
  100. ret i32 %res
  101. }
  102. !0 = !{ }