AddOverFlow.ll 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. ; RUN: opt < %s -instcombine -S | FileCheck %s
  2. target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
  3. ; CHECK-LABEL: @oppositesign
  4. ; CHECK: add nsw i16 %a, %b
  5. define i16 @oppositesign(i16 %x, i16 %y) {
  6. ; %a is negative, %b is positive
  7. %a = or i16 %x, 32768
  8. %b = and i16 %y, 32767
  9. %c = add i16 %a, %b
  10. ret i16 %c
  11. }
  12. define i16 @zero_sign_bit(i16 %a) {
  13. ; CHECK-LABEL: @zero_sign_bit(
  14. ; CHECK-NEXT: and
  15. ; CHECK-NEXT: add nuw
  16. ; CHECK-NEXT: ret
  17. %1 = and i16 %a, 32767
  18. %2 = add i16 %1, 512
  19. ret i16 %2
  20. }
  21. define i16 @zero_sign_bit2(i16 %a, i16 %b) {
  22. ; CHECK-LABEL: @zero_sign_bit2(
  23. ; CHECK-NEXT: and
  24. ; CHECK-NEXT: and
  25. ; CHECK-NEXT: add nuw
  26. ; CHECK-NEXT: ret
  27. %1 = and i16 %a, 32767
  28. %2 = and i16 %b, 32767
  29. %3 = add i16 %1, %2
  30. ret i16 %3
  31. }
  32. declare i16 @bounded(i16 %input);
  33. declare i32 @__gxx_personality_v0(...);
  34. !0 = !{i16 0, i16 32768} ; [0, 32767]
  35. !1 = !{i16 0, i16 32769} ; [0, 32768]
  36. define i16 @add_bounded_values(i16 %a, i16 %b) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
  37. ; CHECK-LABEL: @add_bounded_values(
  38. entry:
  39. %c = call i16 @bounded(i16 %a), !range !0
  40. %d = invoke i16 @bounded(i16 %b) to label %cont unwind label %lpad, !range !0
  41. cont:
  42. ; %c and %d are in [0, 32767]. Therefore, %c + %d doesn't unsigned overflow.
  43. %e = add i16 %c, %d
  44. ; CHECK: add nuw i16 %c, %d
  45. ret i16 %e
  46. lpad:
  47. %0 = landingpad { i8*, i32 }
  48. filter [0 x i8*] zeroinitializer
  49. ret i16 42
  50. }
  51. define i16 @add_bounded_values_2(i16 %a, i16 %b) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
  52. ; CHECK-LABEL: @add_bounded_values_2(
  53. entry:
  54. %c = call i16 @bounded(i16 %a), !range !1
  55. %d = invoke i16 @bounded(i16 %b) to label %cont unwind label %lpad, !range !1
  56. cont:
  57. ; Similar to add_bounded_values, but %c and %d are in [0, 32768]. Therefore,
  58. ; %c + %d may unsigned overflow and we cannot add NUW.
  59. %e = add i16 %c, %d
  60. ; CHECK: add i16 %c, %d
  61. ret i16 %e
  62. lpad:
  63. %0 = landingpad { i8*, i32 }
  64. filter [0 x i8*] zeroinitializer
  65. ret i16 42
  66. }
  67. ; CHECK-LABEL: @ripple_nsw1
  68. ; CHECK: add nsw i16 %a, %b
  69. define i16 @ripple_nsw1(i16 %x, i16 %y) {
  70. ; %a has at most one bit set
  71. %a = and i16 %y, 1
  72. ; %b has a 0 bit other than the sign bit
  73. %b = and i16 %x, 49151
  74. %c = add i16 %a, %b
  75. ret i16 %c
  76. }
  77. ; Like the previous test, but flip %a and %b
  78. ; CHECK-LABEL: @ripple_nsw2
  79. ; CHECK: add nsw i16 %b, %a
  80. define i16 @ripple_nsw2(i16 %x, i16 %y) {
  81. %a = and i16 %y, 1
  82. %b = and i16 %x, 49151
  83. %c = add i16 %b, %a
  84. ret i16 %c
  85. }
  86. ; CHECK-LABEL: @ripple_no_nsw1
  87. ; CHECK: add i32 %a, %x
  88. define i32 @ripple_no_nsw1(i32 %x, i32 %y) {
  89. ; We know nothing about %x
  90. %a = and i32 %y, 1
  91. %b = add i32 %a, %x
  92. ret i32 %b
  93. }
  94. ; CHECK-LABEL: @ripple_no_nsw2
  95. ; CHECK: add nuw i16 %a, %b
  96. define i16 @ripple_no_nsw2(i16 %x, i16 %y) {
  97. ; %a has at most one bit set
  98. %a = and i16 %y, 1
  99. ; %b has a 0 bit, but it is the sign bit
  100. %b = and i16 %x, 32767
  101. %c = add i16 %a, %b
  102. ret i16 %c
  103. }