overflow.ll 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ; RUN: opt -S -instcombine < %s | FileCheck %s
  2. ; <rdar://problem/8558713>
  3. declare void @throwAnExceptionOrWhatever()
  4. ; CHECK-LABEL: @test1(
  5. define i32 @test1(i32 %a, i32 %b) nounwind ssp {
  6. entry:
  7. ; CHECK-NOT: sext
  8. %conv = sext i32 %a to i64
  9. %conv2 = sext i32 %b to i64
  10. %add = add nsw i64 %conv2, %conv
  11. %add.off = add i64 %add, 2147483648
  12. ; CHECK: llvm.sadd.with.overflow.i32
  13. %0 = icmp ugt i64 %add.off, 4294967295
  14. br i1 %0, label %if.then, label %if.end
  15. if.then:
  16. tail call void @throwAnExceptionOrWhatever() nounwind
  17. br label %if.end
  18. if.end:
  19. ; CHECK-NOT: trunc
  20. %conv9 = trunc i64 %add to i32
  21. ; CHECK: ret i32
  22. ret i32 %conv9
  23. }
  24. ; CHECK-LABEL: @test2(
  25. ; This form should not be promoted for two reasons: 1) it is unprofitable to
  26. ; promote it since the add.off instruction has another use, and 2) it is unsafe
  27. ; because the add-with-off makes the high bits of the original add live.
  28. define i32 @test2(i32 %a, i32 %b, i64* %P) nounwind ssp {
  29. entry:
  30. %conv = sext i32 %a to i64
  31. %conv2 = sext i32 %b to i64
  32. %add = add nsw i64 %conv2, %conv
  33. %add.off = add i64 %add, 2147483648
  34. store i64 %add.off, i64* %P
  35. ; CHECK-NOT: llvm.sadd.with.overflow
  36. %0 = icmp ugt i64 %add.off, 4294967295
  37. br i1 %0, label %if.then, label %if.end
  38. if.then:
  39. tail call void @throwAnExceptionOrWhatever() nounwind
  40. br label %if.end
  41. if.end:
  42. %conv9 = trunc i64 %add to i32
  43. ; CHECK: ret i32
  44. ret i32 %conv9
  45. }
  46. ; CHECK: test3
  47. ; PR8816
  48. ; This is illegal to transform because the high bits of the original add are
  49. ; live out.
  50. define i64 @test3(i32 %a, i32 %b) nounwind ssp {
  51. entry:
  52. %conv = sext i32 %a to i64
  53. %conv2 = sext i32 %b to i64
  54. %add = add nsw i64 %conv2, %conv
  55. %add.off = add i64 %add, 2147483648
  56. ; CHECK-NOT: llvm.sadd.with.overflow
  57. %0 = icmp ugt i64 %add.off, 4294967295
  58. br i1 %0, label %if.then, label %if.end
  59. if.then:
  60. tail call void @throwAnExceptionOrWhatever() nounwind
  61. br label %if.end
  62. if.end:
  63. ret i64 %add
  64. ; CHECK: ret i64
  65. }
  66. ; CHECK-LABEL: @test4(
  67. ; Should be able to form an i8 sadd computed in an i32.
  68. define zeroext i8 @test4(i8 signext %a, i8 signext %b) nounwind ssp {
  69. entry:
  70. %conv = sext i8 %a to i32
  71. %conv2 = sext i8 %b to i32
  72. %add = add nsw i32 %conv2, %conv
  73. %add4 = add nsw i32 %add, 128
  74. %cmp = icmp ugt i32 %add4, 255
  75. br i1 %cmp, label %if.then, label %if.end
  76. ; CHECK: llvm.sadd.with.overflow.i8
  77. if.then: ; preds = %entry
  78. tail call void @throwAnExceptionOrWhatever() nounwind
  79. unreachable
  80. if.end: ; preds = %entry
  81. %conv7 = trunc i32 %add to i8
  82. ret i8 %conv7
  83. ; CHECK: ret i8
  84. }
  85. ; CHECK-LABEL: @test8(
  86. ; PR11438
  87. ; This is @test1, but the operands are not sign-extended. Make sure
  88. ; we don't transform this case.
  89. define i32 @test8(i64 %a, i64 %b) nounwind ssp {
  90. entry:
  91. ; CHECK-NOT: llvm.sadd
  92. ; CHECK: add i64 %a, %b
  93. ; CHECK-NOT: llvm.sadd
  94. ; CHECK: ret
  95. %add = add i64 %a, %b
  96. %add.off = add i64 %add, 2147483648
  97. %0 = icmp ugt i64 %add.off, 4294967295
  98. br i1 %0, label %if.then, label %if.end
  99. if.then:
  100. tail call void @throwAnExceptionOrWhatever() nounwind
  101. br label %if.end
  102. if.end:
  103. %conv9 = trunc i64 %add to i32
  104. ret i32 %conv9
  105. }