<div dir="ltr">Discovered by MemorySanitizer, btw.</div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jul 3, 2018 at 12:59 PM, Evgenii Stepanov <span dir="ltr"><<a href="mailto:eugeni.stepanov@gmail.com" target="_blank">eugeni.stepanov@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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="HOEnZb"><div class="h5"><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-pr<wbr>oject?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/sho<wbr>w_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/D4804<wbr>0</a><br>
<br>
Modified:<br>
    cfe/trunk/lib/AST/ExprConstant<wbr>.cpp<br>
    cfe/trunk/test/SemaCXX/builtin<wbr>s-overflow.cpp<br>
<br>
Modified: cfe/trunk/lib/AST/ExprConstant<wbr>.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-pr<wbr>oject/cfe/trunk/lib/AST/ExprCo<wbr>nstant.cpp?rev=334650&r1=<wbr>334649&r2=334650&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- cfe/trunk/lib/AST/ExprConstant<wbr>.cpp (original)<br>
+++ cfe/trunk/lib/AST/ExprConstant<wbr>.cpp Wed Jun 13 13:43:27 2018<br>
@@ -8155,6 +8155,124 @@ bool IntExprEvaluator::VisitBuiltin<wbr>CallE<br>
   case Builtin::BIomp_is_initial_devi<wbr>ce:<br>
     // We can decide statically which value the runtime would return if called.<br>
     return Success(Info.getLangOpts().Ope<wbr>nMPIsDevice ? 0 : 1, E);<br>
+  case Builtin::BI__builtin_add_overf<wbr>low:<br>
+  case Builtin::BI__builtin_sub_overf<wbr>low:<br>
+  case Builtin::BI__builtin_mul_overf<wbr>low:<br>
+  case Builtin::BI__builtin_sadd_over<wbr>flow:<br>
+  case Builtin::BI__builtin_uadd_over<wbr>flow:<br>
+  case Builtin::BI__builtin_uaddl_ove<wbr>rflow:<br>
+  case Builtin::BI__builtin_uaddll_ov<wbr>erflow:<br>
+  case Builtin::BI__builtin_usub_over<wbr>flow:<br>
+  case Builtin::BI__builtin_usubl_ove<wbr>rflow:<br>
+  case Builtin::BI__builtin_usubll_ov<wbr>erflow:<br>
+  case Builtin::BI__builtin_umul_over<wbr>flow:<br>
+  case Builtin::BI__builtin_umull_ove<wbr>rflow:<br>
+  case Builtin::BI__builtin_umulll_ov<wbr>erflow:<br>
+  case Builtin::BI__builtin_saddl_ove<wbr>rflow:<br>
+  case Builtin::BI__builtin_saddll_ov<wbr>erflow:<br>
+  case Builtin::BI__builtin_ssub_over<wbr>flow:<br>
+  case Builtin::BI__builtin_ssubl_ove<wbr>rflow:<br>
+  case Builtin::BI__builtin_ssubll_ov<wbr>erflow:<br>
+  case Builtin::BI__builtin_smul_over<wbr>flow:<br>
+  case Builtin::BI__builtin_smull_ove<wbr>rflow:<br>
+  case Builtin::BI__builtin_smulll_ov<wbr>erflow: {<br>
+    LValue ResultLValue;<br>
+    APSInt LHS, RHS;<br>
+<br>
+    QualType ResultType = E->getArg(2)->getType()->getPo<wbr>inteeType();<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_overf<wbr>low ||<br>
+        BuiltinOp == Builtin::BI__builtin_sub_overf<wbr>low ||<br>
+        BuiltinOp == Builtin::BI__builtin_mul_overf<wbr>low) {<br>
+      bool IsSigned = LHS.isSigned() || RHS.isSigned() ||<br>
+                      ResultType->isSignedIntegerOrE<wbr>numerationType();<br>
+      bool AllSigned = LHS.isSigned() && RHS.isSigned() &&<br>
+                      ResultType->isSignedIntegerOrE<wbr>numerationType();<br>
+      uint64_t LHSSize = LHS.getBitWidth();<br>
+      uint64_t RHSSize = RHS.getBitWidth();<br>
+      uint64_t ResultSize = Info.Ctx.getTypeSize(ResultTyp<wbr>e);<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_overf<wbr>low:<br>
+    case Builtin::BI__builtin_sadd_over<wbr>flow:<br>
+    case Builtin::BI__builtin_saddl_ove<wbr>rflow:<br>
+    case Builtin::BI__builtin_saddll_ov<wbr>erflow:<br>
+    case Builtin::BI__builtin_uadd_over<wbr>flow:<br>
+    case Builtin::BI__builtin_uaddl_ove<wbr>rflow:<br>
+    case Builtin::BI__builtin_uaddll_ov<wbr>erflow:<br>
+      Result = LHS.isSigned() ? LHS.sadd_ov(RHS, DidOverflow)<br>
+                              : LHS.uadd_ov(RHS, DidOverflow);<br>
+      break;<br>
+    case Builtin::BI__builtin_sub_overf<wbr>low:<br>
+    case Builtin::BI__builtin_ssub_over<wbr>flow:<br>
+    case Builtin::BI__builtin_ssubl_ove<wbr>rflow:<br>
+    case Builtin::BI__builtin_ssubll_ov<wbr>erflow:<br>
+    case Builtin::BI__builtin_usub_over<wbr>flow:<br>
+    case Builtin::BI__builtin_usubl_ove<wbr>rflow:<br>
+    case Builtin::BI__builtin_usubll_ov<wbr>erflow:<br>
+      Result = LHS.isSigned() ? LHS.ssub_ov(RHS, DidOverflow)<br>
+                              : LHS.usub_ov(RHS, DidOverflow);<br>
+      break;<br>
+    case Builtin::BI__builtin_mul_overf<wbr>low:<br>
+    case Builtin::BI__builtin_smul_over<wbr>flow:<br>
+    case Builtin::BI__builtin_smull_ove<wbr>rflow:<br>
+    case Builtin::BI__builtin_smulll_ov<wbr>erflow:<br>
+    case Builtin::BI__builtin_umul_over<wbr>flow:<br>
+    case Builtin::BI__builtin_umull_ove<wbr>rflow:<br>
+    case Builtin::BI__builtin_umulll_ov<wbr>erflow:<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_overf<wbr>low ||<br>
+        BuiltinOp == Builtin::BI__builtin_sub_overf<wbr>low ||<br>
+        BuiltinOp == Builtin::BI__builtin_mul_overf<wbr>low) {<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.get<wbr>TypeSize(ResultType));<br>
+      Temp.setIsSigned(ResultType->i<wbr>sSignedIntegerOrEnumerationTyp<wbr>e());<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/builtin<wbr>s-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-pr<wbr>oject/cfe/trunk/test/SemaCXX/<wbr>builtins-overflow.cpp?rev=<wbr>334650&r1=334649&r2=334650&<wbr>view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- cfe/trunk/test/SemaCXX/builtin<wbr>s-overflow.cpp (original)<br>
+++ cfe/trunk/test/SemaCXX/builtin<wbr>s-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>(stat<wbr>ic_cast<char>(120), static_cast<char>(10)) == Result<short>{false, 130});<br>
+static_assert(add<char>(stati<wbr>c_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>(static<wbr>_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_MA<wbr>X - 22, 24) == Result<int>{true, INT_MIN + 1});<br>
+static_assert(add<int>(INT_MI<wbr>N + 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),sta<wbr>tic_cast<char>(1)) == Result<unsigned char>{true, UCHAR_MAX});<br>
+static_assert(sub<char>(stati<wbr>c_cast<unsigned char>(0),static_cast<unsigned char>(1)) == Result<char>{false, -1});<br>
+static_assert(sub<unsigned short>(static_cast<short>(0),s<wbr>tatic_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_MA<wbr>X - 22, -23) == Result<int>{true, INT_MIN});<br>
+static_assert(sub<int>(INT_MI<wbr>N + 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_MA<wbr>X / 22, 23) == Result<int>{true, -2049870757});<br>
+static_assert(mul<int>(INT_MI<wbr>N / 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" target="_blank">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>
</div></div></blockquote></div><br></div>