speculate-store.ll 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. ; RUN: opt -simplifycfg -S < %s | FileCheck %s
  2. define void @ifconvertstore(i32 %m, i32* %A, i32* %B, i32 %C, i32 %D) {
  3. entry:
  4. %arrayidx = getelementptr inbounds i32, i32* %B, i64 0
  5. %0 = load i32, i32* %arrayidx, align 4
  6. %add = add nsw i32 %0, %C
  7. %arrayidx2 = getelementptr inbounds i32, i32* %A, i64 0
  8. ; First store to the location.
  9. store i32 %add, i32* %arrayidx2, align 4
  10. %arrayidx4 = getelementptr inbounds i32, i32* %B, i64 1
  11. %1 = load i32, i32* %arrayidx4, align 4
  12. %add5 = add nsw i32 %1, %D
  13. %cmp6 = icmp sgt i32 %add5, %C
  14. br i1 %cmp6, label %if.then, label %ret.end
  15. ; Make sure we speculate stores like the following one. It is cheap compared to
  16. ; a mispredicated branch.
  17. ; CHECK-LABEL: @ifconvertstore(
  18. ; CHECK: %add5.add = select i1 %cmp6, i32 %add5, i32 %add
  19. ; CHECK: store i32 %add5.add, i32* %arrayidx2, align 4
  20. if.then:
  21. store i32 %add5, i32* %arrayidx2, align 4
  22. br label %ret.end
  23. ret.end:
  24. ret void
  25. }
  26. define void @noifconvertstore1(i32 %m, i32* %A, i32* %B, i32 %C, i32 %D) {
  27. entry:
  28. %arrayidx = getelementptr inbounds i32, i32* %B, i64 0
  29. %0 = load i32, i32* %arrayidx, align 4
  30. %add = add nsw i32 %0, %C
  31. %arrayidx2 = getelementptr inbounds i32, i32* %A, i64 0
  32. ; Store to a different location.
  33. store i32 %add, i32* %arrayidx, align 4
  34. %arrayidx4 = getelementptr inbounds i32, i32* %B, i64 1
  35. %1 = load i32, i32* %arrayidx4, align 4
  36. %add5 = add nsw i32 %1, %D
  37. %cmp6 = icmp sgt i32 %add5, %C
  38. br i1 %cmp6, label %if.then, label %ret.end
  39. ; CHECK-LABEL: @noifconvertstore1(
  40. ; CHECK-NOT: select
  41. if.then:
  42. store i32 %add5, i32* %arrayidx2, align 4
  43. br label %ret.end
  44. ret.end:
  45. ret void
  46. }
  47. declare void @unknown_fun()
  48. define void @noifconvertstore2(i32 %m, i32* %A, i32* %B, i32 %C, i32 %D) {
  49. entry:
  50. %arrayidx = getelementptr inbounds i32, i32* %B, i64 0
  51. %0 = load i32, i32* %arrayidx, align 4
  52. %add = add nsw i32 %0, %C
  53. %arrayidx2 = getelementptr inbounds i32, i32* %A, i64 0
  54. ; First store to the location.
  55. store i32 %add, i32* %arrayidx2, align 4
  56. call void @unknown_fun()
  57. %arrayidx4 = getelementptr inbounds i32, i32* %B, i64 1
  58. %1 = load i32, i32* %arrayidx4, align 4
  59. %add5 = add nsw i32 %1, %D
  60. %cmp6 = icmp sgt i32 %add5, %C
  61. br i1 %cmp6, label %if.then, label %ret.end
  62. ; CHECK-LABEL: @noifconvertstore2(
  63. ; CHECK-NOT: select
  64. if.then:
  65. store i32 %add5, i32* %arrayidx2, align 4
  66. br label %ret.end
  67. ret.end:
  68. ret void
  69. }
  70. define void @noifconvertstore_volatile(i32 %m, i32* %A, i32* %B, i32 %C, i32 %D) {
  71. entry:
  72. %arrayidx = getelementptr inbounds i32, i32* %B, i64 0
  73. %0 = load i32, i32* %arrayidx, align 4
  74. %add = add nsw i32 %0, %C
  75. %arrayidx2 = getelementptr inbounds i32, i32* %A, i64 0
  76. ; First store to the location.
  77. store i32 %add, i32* %arrayidx2, align 4
  78. %arrayidx4 = getelementptr inbounds i32, i32* %B, i64 1
  79. %1 = load i32, i32* %arrayidx4, align 4
  80. %add5 = add nsw i32 %1, %D
  81. %cmp6 = icmp sgt i32 %add5, %C
  82. br i1 %cmp6, label %if.then, label %ret.end
  83. ; Make sure we don't speculate volatile stores.
  84. ; CHECK-LABEL: @noifconvertstore_volatile(
  85. ; CHECK-NOT: select
  86. if.then:
  87. store volatile i32 %add5, i32* %arrayidx2, align 4
  88. br label %ret.end
  89. ret.end:
  90. ret void
  91. }