speculate.ll 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. ; RUN: opt -S -licm < %s | FileCheck %s
  2. ; UDiv is safe to speculate if the denominator is known non-zero.
  3. ; CHECK-LABEL: @safe_udiv(
  4. ; CHECK: %div = udiv i64 %x, 2
  5. ; CHECK-NEXT: br label %for.body
  6. define void @safe_udiv(i64 %x, i64 %m, i64 %n, i32* %p, i64* %q) nounwind {
  7. entry:
  8. br label %for.body
  9. for.body: ; preds = %entry, %for.inc
  10. %i.02 = phi i64 [ %inc, %for.inc ], [ 0, %entry ]
  11. %arrayidx = getelementptr inbounds i32, i32* %p, i64 %i.02
  12. %0 = load i32, i32* %arrayidx, align 4
  13. %tobool = icmp eq i32 %0, 0
  14. br i1 %tobool, label %for.inc, label %if.then
  15. if.then: ; preds = %for.body
  16. %div = udiv i64 %x, 2
  17. %arrayidx1 = getelementptr inbounds i64, i64* %q, i64 %i.02
  18. store i64 %div, i64* %arrayidx1, align 8
  19. br label %for.inc
  20. for.inc: ; preds = %if.then, %for.body
  21. %inc = add i64 %i.02, 1
  22. %cmp = icmp slt i64 %inc, %n
  23. br i1 %cmp, label %for.body, label %for.end
  24. for.end: ; preds = %for.inc, %entry
  25. ret void
  26. }
  27. ; UDiv is unsafe to speculate if the denominator is not known non-zero.
  28. ; CHECK-LABEL: @unsafe_udiv(
  29. ; CHECK-NOT: udiv
  30. ; CHECK: for.body:
  31. define void @unsafe_udiv(i64 %x, i64 %m, i64 %n, i32* %p, i64* %q) nounwind {
  32. entry:
  33. br label %for.body
  34. for.body: ; preds = %entry, %for.inc
  35. %i.02 = phi i64 [ %inc, %for.inc ], [ 0, %entry ]
  36. %arrayidx = getelementptr inbounds i32, i32* %p, i64 %i.02
  37. %0 = load i32, i32* %arrayidx, align 4
  38. %tobool = icmp eq i32 %0, 0
  39. br i1 %tobool, label %for.inc, label %if.then
  40. if.then: ; preds = %for.body
  41. %div = udiv i64 %x, %m
  42. %arrayidx1 = getelementptr inbounds i64, i64* %q, i64 %i.02
  43. store i64 %div, i64* %arrayidx1, align 8
  44. br label %for.inc
  45. for.inc: ; preds = %if.then, %for.body
  46. %inc = add i64 %i.02, 1
  47. %cmp = icmp slt i64 %inc, %n
  48. br i1 %cmp, label %for.body, label %for.end
  49. for.end: ; preds = %for.inc, %entry
  50. ret void
  51. }
  52. ; SDiv is safe to speculate if the denominator is known non-zero and
  53. ; known to have at least one zero bit.
  54. ; CHECK-LABEL: @safe_sdiv(
  55. ; CHECK: %div = sdiv i64 %x, 2
  56. ; CHECK-NEXT: br label %for.body
  57. define void @safe_sdiv(i64 %x, i64 %m, i64 %n, i32* %p, i64* %q) nounwind {
  58. entry:
  59. %and = and i64 %m, -3
  60. br label %for.body
  61. for.body: ; preds = %entry, %for.inc
  62. %i.02 = phi i64 [ %inc, %for.inc ], [ 0, %entry ]
  63. %arrayidx = getelementptr inbounds i32, i32* %p, i64 %i.02
  64. %0 = load i32, i32* %arrayidx, align 4
  65. %tobool = icmp eq i32 %0, 0
  66. br i1 %tobool, label %for.inc, label %if.then
  67. if.then: ; preds = %for.body
  68. %div = sdiv i64 %x, 2
  69. %arrayidx1 = getelementptr inbounds i64, i64* %q, i64 %i.02
  70. store i64 %div, i64* %arrayidx1, align 8
  71. br label %for.inc
  72. for.inc: ; preds = %if.then, %for.body
  73. %inc = add i64 %i.02, 1
  74. %cmp = icmp slt i64 %inc, %n
  75. br i1 %cmp, label %for.body, label %for.end
  76. for.end: ; preds = %for.inc, %entry
  77. ret void
  78. }
  79. ; SDiv is unsafe to speculate if the denominator is not known non-zero.
  80. ; CHECK-LABEL: @unsafe_sdiv_a(
  81. ; CHECK-NOT: sdiv
  82. ; CHECK: for.body:
  83. define void @unsafe_sdiv_a(i64 %x, i64 %m, i64 %n, i32* %p, i64* %q) nounwind {
  84. entry:
  85. %or = or i64 %m, 1
  86. br label %for.body
  87. for.body: ; preds = %entry, %for.inc
  88. %i.02 = phi i64 [ %inc, %for.inc ], [ 0, %entry ]
  89. %arrayidx = getelementptr inbounds i32, i32* %p, i64 %i.02
  90. %0 = load i32, i32* %arrayidx, align 4
  91. %tobool = icmp eq i32 %0, 0
  92. br i1 %tobool, label %for.inc, label %if.then
  93. if.then: ; preds = %for.body
  94. %div = sdiv i64 %x, %or
  95. %arrayidx1 = getelementptr inbounds i64, i64* %q, i64 %i.02
  96. store i64 %div, i64* %arrayidx1, align 8
  97. br label %for.inc
  98. for.inc: ; preds = %if.then, %for.body
  99. %inc = add i64 %i.02, 1
  100. %cmp = icmp slt i64 %inc, %n
  101. br i1 %cmp, label %for.body, label %for.end
  102. for.end: ; preds = %for.inc, %entry
  103. ret void
  104. }
  105. ; SDiv is unsafe to speculate if the denominator is not known to have a zero bit.
  106. ; CHECK-LABEL: @unsafe_sdiv_b(
  107. ; CHECK-NOT: sdiv
  108. ; CHECK: for.body:
  109. define void @unsafe_sdiv_b(i64 %x, i64 %m, i64 %n, i32* %p, i64* %q) nounwind {
  110. entry:
  111. %and = and i64 %m, -3
  112. br label %for.body
  113. for.body: ; preds = %entry, %for.inc
  114. %i.02 = phi i64 [ %inc, %for.inc ], [ 0, %entry ]
  115. %arrayidx = getelementptr inbounds i32, i32* %p, i64 %i.02
  116. %0 = load i32, i32* %arrayidx, align 4
  117. %tobool = icmp eq i32 %0, 0
  118. br i1 %tobool, label %for.inc, label %if.then
  119. if.then: ; preds = %for.body
  120. %div = sdiv i64 %x, %and
  121. %arrayidx1 = getelementptr inbounds i64, i64* %q, i64 %i.02
  122. store i64 %div, i64* %arrayidx1, align 8
  123. br label %for.inc
  124. for.inc: ; preds = %if.then, %for.body
  125. %inc = add i64 %i.02, 1
  126. %cmp = icmp slt i64 %inc, %n
  127. br i1 %cmp, label %for.body, label %for.end
  128. for.end: ; preds = %for.inc, %entry
  129. ret void
  130. }
  131. ; SDiv is unsafe to speculate inside an infinite loop.
  132. define void @unsafe_sdiv_c(i64 %a, i64 %b, i64* %p) {
  133. entry:
  134. ; CHECK: entry:
  135. ; CHECK-NOT: sdiv
  136. ; CHECK: br label %for.body
  137. br label %for.body
  138. for.body:
  139. %c = icmp eq i64 %b, 0
  140. br i1 %c, label %backedge, label %if.then
  141. if.then:
  142. %d = sdiv i64 %a, %b
  143. store i64 %d, i64* %p
  144. br label %backedge
  145. backedge:
  146. br label %for.body
  147. }