<div dir="ltr">Hi,<div><br></div><div>with this change, the following compiles to "ret i32 undef":</div><div><div><br></div><div>int main(int argc, char **argv) {                                                                                               </div><div>  constexpr int x = 1;                                                                                                          </div><div>  constexpr int y = 2;                                                                                                          </div><div>  int z;                                                                                                                        </div><div>                                                                                                                                </div><div>  __builtin_sadd_overflow(x, y, &z);                                                                                            </div><div>  return z;                                                                                                                     </div><div>}</div></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jun 13, 2018 at 1:43 PM, Erich Keane via cfe-commits <span dir="ltr"><<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: erichkeane<br>
Date: Wed Jun 13 13:43:27 2018<br>
New Revision: 334650<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=334650&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project?rev=334650&view=rev</a><br>
Log:<br>
Implement constexpr __builtin_*_overflow<br>
<br>
As requested here:<a href="https://bugs.llvm.org/show_bug.cgi?id=37633" rel="noreferrer" target="_blank">https://bugs.llvm.org/<wbr>show_bug.cgi?id=37633</a><br>
permit the __builtin_*_overflow builtins in constexpr functions.<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D48040" rel="noreferrer" target="_blank">https://reviews.llvm.org/<wbr>D48040</a><br>
<br>
Modified:<br>
    cfe/trunk/lib/AST/<wbr>ExprConstant.cpp<br>
    cfe/trunk/test/SemaCXX/<wbr>builtins-overflow.cpp<br>
<br>
Modified: cfe/trunk/lib/AST/<wbr>ExprConstant.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=334650&r1=334649&r2=334650&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/lib/AST/<wbr>ExprConstant.cpp?rev=334650&<wbr>r1=334649&r2=334650&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- cfe/trunk/lib/AST/<wbr>ExprConstant.cpp (original)<br>
+++ cfe/trunk/lib/AST/<wbr>ExprConstant.cpp Wed Jun 13 13:43:27 2018<br>
@@ -8155,6 +8155,124 @@ bool IntExprEvaluator::<wbr>VisitBuiltinCallE<br>
   case Builtin::BIomp_is_initial_<wbr>device:<br>
     // We can decide statically which value the runtime would return if called.<br>
     return Success(Info.getLangOpts().<wbr>OpenMPIsDevice ? 0 : 1, E);<br>
+  case Builtin::BI__builtin_add_<wbr>overflow:<br>
+  case Builtin::BI__builtin_sub_<wbr>overflow:<br>
+  case Builtin::BI__builtin_mul_<wbr>overflow:<br>
+  case Builtin::BI__builtin_sadd_<wbr>overflow:<br>
+  case Builtin::BI__builtin_uadd_<wbr>overflow:<br>
+  case Builtin::BI__builtin_uaddl_<wbr>overflow:<br>
+  case Builtin::BI__builtin_uaddll_<wbr>overflow:<br>
+  case Builtin::BI__builtin_usub_<wbr>overflow:<br>
+  case Builtin::BI__builtin_usubl_<wbr>overflow:<br>
+  case Builtin::BI__builtin_usubll_<wbr>overflow:<br>
+  case Builtin::BI__builtin_umul_<wbr>overflow:<br>
+  case Builtin::BI__builtin_umull_<wbr>overflow:<br>
+  case Builtin::BI__builtin_umulll_<wbr>overflow:<br>
+  case Builtin::BI__builtin_saddl_<wbr>overflow:<br>
+  case Builtin::BI__builtin_saddll_<wbr>overflow:<br>
+  case Builtin::BI__builtin_ssub_<wbr>overflow:<br>
+  case Builtin::BI__builtin_ssubl_<wbr>overflow:<br>
+  case Builtin::BI__builtin_ssubll_<wbr>overflow:<br>
+  case Builtin::BI__builtin_smul_<wbr>overflow:<br>
+  case Builtin::BI__builtin_smull_<wbr>overflow:<br>
+  case Builtin::BI__builtin_smulll_<wbr>overflow: {<br>
+    LValue ResultLValue;<br>
+    APSInt LHS, RHS;<br>
+<br>
+    QualType ResultType = E->getArg(2)->getType()-><wbr>getPointeeType();<br>
+    if (!EvaluateInteger(E->getArg(0)<wbr>, LHS, Info) ||<br>
+        !EvaluateInteger(E->getArg(1), RHS, Info) ||<br>
+        !EvaluatePointer(E->getArg(2), ResultLValue, Info))<br>
+      return false;<br>
+<br>
+    APSInt Result;<br>
+    bool DidOverflow = false;<br>
+<br>
+    // If the types don't have to match, enlarge all 3 to the largest of them.<br>
+    if (BuiltinOp == Builtin::BI__builtin_add_<wbr>overflow ||<br>
+        BuiltinOp == Builtin::BI__builtin_sub_<wbr>overflow ||<br>
+        BuiltinOp == Builtin::BI__builtin_mul_<wbr>overflow) {<br>
+      bool IsSigned = LHS.isSigned() || RHS.isSigned() ||<br>
+                      ResultType-><wbr>isSignedIntegerOrEnumerationTy<wbr>pe();<br>
+      bool AllSigned = LHS.isSigned() && RHS.isSigned() &&<br>
+                      ResultType-><wbr>isSignedIntegerOrEnumerationTy<wbr>pe();<br>
+      uint64_t LHSSize = LHS.getBitWidth();<br>
+      uint64_t RHSSize = RHS.getBitWidth();<br>
+      uint64_t ResultSize = Info.Ctx.getTypeSize(<wbr>ResultType);<br>
+      uint64_t MaxBits = std::max(std::max(LHSSize, RHSSize), ResultSize);<br>
+<br>
+      // Add an additional bit if the signedness isn't uniformly agreed to. We<br>
+      // could do this ONLY if there is a signed and an unsigned that both have<br>
+      // MaxBits, but the code to check that is pretty nasty.  The issue will be<br>
+      // caught in the shrink-to-result later anyway.<br>
+      if (IsSigned && !AllSigned)<br>
+        ++MaxBits;<br>
+<br>
+      LHS = APSInt(IsSigned ? LHS.sextOrSelf(MaxBits) : LHS.zextOrSelf(MaxBits),<br>
+                   !IsSigned);<br>
+      RHS = APSInt(IsSigned ? RHS.sextOrSelf(MaxBits) : RHS.zextOrSelf(MaxBits),<br>
+                   !IsSigned);<br>
+      Result = APSInt(MaxBits, !IsSigned);<br>
+    }<br>
+<br>
+    // Find largest int.<br>
+    switch (BuiltinOp) {<br>
+    default:<br>
+      llvm_unreachable("Invalid value for BuiltinOp");<br>
+    case Builtin::BI__builtin_add_<wbr>overflow:<br>
+    case Builtin::BI__builtin_sadd_<wbr>overflow:<br>
+    case Builtin::BI__builtin_saddl_<wbr>overflow:<br>
+    case Builtin::BI__builtin_saddll_<wbr>overflow:<br>
+    case Builtin::BI__builtin_uadd_<wbr>overflow:<br>
+    case Builtin::BI__builtin_uaddl_<wbr>overflow:<br>
+    case Builtin::BI__builtin_uaddll_<wbr>overflow:<br>
+      Result = LHS.isSigned() ? LHS.sadd_ov(RHS, DidOverflow)<br>
+                              : LHS.uadd_ov(RHS, DidOverflow);<br>
+      break;<br>
+    case Builtin::BI__builtin_sub_<wbr>overflow:<br>
+    case Builtin::BI__builtin_ssub_<wbr>overflow:<br>
+    case Builtin::BI__builtin_ssubl_<wbr>overflow:<br>
+    case Builtin::BI__builtin_ssubll_<wbr>overflow:<br>
+    case Builtin::BI__builtin_usub_<wbr>overflow:<br>
+    case Builtin::BI__builtin_usubl_<wbr>overflow:<br>
+    case Builtin::BI__builtin_usubll_<wbr>overflow:<br>
+      Result = LHS.isSigned() ? LHS.ssub_ov(RHS, DidOverflow)<br>
+                              : LHS.usub_ov(RHS, DidOverflow);<br>
+      break;<br>
+    case Builtin::BI__builtin_mul_<wbr>overflow:<br>
+    case Builtin::BI__builtin_smul_<wbr>overflow:<br>
+    case Builtin::BI__builtin_smull_<wbr>overflow:<br>
+    case Builtin::BI__builtin_smulll_<wbr>overflow:<br>
+    case Builtin::BI__builtin_umul_<wbr>overflow:<br>
+    case Builtin::BI__builtin_umull_<wbr>overflow:<br>
+    case Builtin::BI__builtin_umulll_<wbr>overflow:<br>
+      Result = LHS.isSigned() ? LHS.smul_ov(RHS, DidOverflow)<br>
+                              : LHS.umul_ov(RHS, DidOverflow);<br>
+      break;<br>
+    }<br>
+<br>
+    // In the case where multiple sizes are allowed, truncate and see if<br>
+    // the values are the same.<br>
+    if (BuiltinOp == Builtin::BI__builtin_add_<wbr>overflow ||<br>
+        BuiltinOp == Builtin::BI__builtin_sub_<wbr>overflow ||<br>
+        BuiltinOp == Builtin::BI__builtin_mul_<wbr>overflow) {<br>
+      // APSInt doesn't have a TruncOrSelf, so we use extOrTrunc instead,<br>
+      // since it will give us the behavior of a TruncOrSelf in the case where<br>
+      // its parameter <= its size.  We previously set Result to be at least the<br>
+      // type-size of the result, so getTypeSize(ResultType) <= Result.BitWidth<br>
+      // will work exactly like TruncOrSelf.<br>
+      APSInt Temp = Result.extOrTrunc(Info.Ctx.<wbr>getTypeSize(ResultType));<br>
+      Temp.setIsSigned(ResultType-><wbr>isSignedIntegerOrEnumerationTy<wbr>pe());<br>
+<br>
+      if (!APSInt::isSameValue(Temp, Result))<br>
+        DidOverflow = true;<br>
+      Result = Temp;<br>
+    }<br>
+<br>
+    APValue APV{Result};<br>
+    handleAssignment(Info, E, ResultLValue, ResultType, APV);<br>
+    return Success(DidOverflow, E);<br>
+  }<br>
   }<br>
 }<br>
<br>
<br>
Modified: cfe/trunk/test/SemaCXX/<wbr>builtins-overflow.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/builtins-overflow.cpp?rev=334650&r1=334649&r2=334650&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/test/<wbr>SemaCXX/builtins-overflow.cpp?<wbr>rev=334650&r1=334649&r2=<wbr>334650&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- cfe/trunk/test/SemaCXX/<wbr>builtins-overflow.cpp (original)<br>
+++ cfe/trunk/test/SemaCXX/<wbr>builtins-overflow.cpp Wed Jun 13 13:43:27 2018<br>
@@ -1,6 +1,8 @@<br>
 // RUN: %clang_cc1 -fsyntax-only -std=c++17 -verify %s<br>
 // expected-no-diagnostics<br>
<br>
+#include <limits.h><br>
+<br>
 int a() {<br>
   const int x = 3;<br>
   static int z;<br>
@@ -13,3 +15,85 @@ int a2() {<br>
   constexpr int *y = &z;<br>
   return []() { return __builtin_sub_overflow(x, x, y); }();<br>
 }<br>
+<br>
+template<typename T><br>
+struct Result {<br>
+  bool B;<br>
+  T Value;<br>
+  constexpr bool operator==(const Result<T> &Other) {<br>
+    return B == Other.B && Value == Other.Value;<br>
+  }<br>
+};<br>
+<br>
+<br>
+template <typename RET, typename LHS, typename RHS><br>
+constexpr Result<RET> add(LHS &&lhs, RHS &&rhs) {<br>
+  RET sum{};<br>
+  bool b = __builtin_add_overflow(lhs, rhs, &sum);<br>
+  return {b, sum};<br>
+}<br>
+<br>
+static_assert(add<short>(<wbr>static_cast<char>(120), static_cast<char>(10)) == Result<short>{false, 130});<br>
+static_assert(add<char>(<wbr>static_cast<short>(120), static_cast<short>(10)) == Result<char>{true, -126});<br>
+static_assert(add<unsigned int>(INT_MAX, INT_MAX) == Result<unsigned int>{false, static_cast<unsigned int>(INT_MAX) * 2u});<br>
+static_assert(add<int>(<wbr>static_cast<unsigned int>(INT_MAX), 1u) == Result<int>{true, INT_MIN});<br>
+<br>
+static_assert(add<int>(17, 22) == Result<int>{false, 39});<br>
+static_assert(add<int>(INT_<wbr>MAX - 22, 24) == Result<int>{true, INT_MIN + 1});<br>
+static_assert(add<int>(INT_<wbr>MIN + 22, -23) == Result<int>{true, INT_MAX});<br>
+<br>
+template <typename RET, typename LHS, typename RHS><br>
+constexpr Result<RET> sub(LHS &&lhs, RHS &&rhs) {<br>
+  RET sum{};<br>
+  bool b = __builtin_sub_overflow(lhs, rhs, &sum);<br>
+  return {b, sum};<br>
+}<br>
+<br>
+static_assert(sub<unsigned char>(static_cast<char>(0),<wbr>static_cast<char>(1)) == Result<unsigned char>{true, UCHAR_MAX});<br>
+static_assert(sub<char>(<wbr>static_cast<unsigned char>(0),static_cast<unsigned char>(1)) == Result<char>{false, -1});<br>
+static_assert(sub<unsigned short>(static_cast<short>(0),<wbr>static_cast<short>(1)) == Result<unsigned short>{true, USHRT_MAX});<br>
+<br>
+static_assert(sub<int>(17,22) == Result<int>{false, -5});<br>
+static_assert(sub<int>(INT_<wbr>MAX - 22, -23) == Result<int>{true, INT_MIN});<br>
+static_assert(sub<int>(INT_<wbr>MIN + 22, 23) == Result<int>{true, INT_MAX});<br>
+<br>
+template <typename RET, typename LHS, typename RHS><br>
+constexpr Result<RET> mul(LHS &&lhs, RHS &&rhs) {<br>
+  RET sum{};<br>
+  bool b  = __builtin_mul_overflow(lhs, rhs, &sum);<br>
+  return {b, sum};<br>
+}<br>
+<br>
+static_assert(mul<int>(17,22) == Result<int>{false, 374});<br>
+static_assert(mul<int>(INT_<wbr>MAX / 22, 23) == Result<int>{true, -2049870757});<br>
+static_assert(mul<int>(INT_<wbr>MIN / 22, -23) == Result<int>{true, -2049870757});<br>
+<br>
+constexpr Result<int> sadd(int lhs, int rhs) {<br>
+  int sum{};<br>
+  bool b = __builtin_sadd_overflow(lhs, rhs, &sum);<br>
+  return {b, sum};<br>
+}<br>
+<br>
+static_assert(sadd(17,22) == Result<int>{false, 39});<br>
+static_assert(sadd(INT_MAX - 22, 23) == Result<int>{true, INT_MIN});<br>
+static_assert(sadd(INT_MIN + 22, -23) == Result<int>{true, INT_MAX});<br>
+<br>
+constexpr Result<int> ssub(int lhs, int rhs) {<br>
+  int sum{};<br>
+  bool b = __builtin_ssub_overflow(lhs, rhs, &sum);<br>
+  return {b, sum};<br>
+}<br>
+<br>
+static_assert(ssub(17,22) == Result<int>{false, -5});<br>
+static_assert(ssub(INT_MAX - 22, -23) == Result<int>{true, INT_MIN});<br>
+static_assert(ssub(INT_MIN + 22, 23) == Result<int>{true, INT_MAX});<br>
+<br>
+constexpr Result<int> smul(int lhs, int rhs) {<br>
+  int sum{};<br>
+  bool b = __builtin_smul_overflow(lhs, rhs, &sum);<br>
+  return {b, sum};<br>
+}<br>
+<br>
+static_assert(smul(17,22) == Result<int>{false, 374});<br>
+static_assert(smul(INT_MAX / 22, 23) == Result<int>{true, -2049870757});<br>
+static_assert(smul(INT_MIN / 22, -23) == Result<int>{true, -2049870757});<br>
<br>
<br>
______________________________<wbr>_________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br></div>