r334650 - Implement constexpr __builtin_*_overflow
Erich Keane via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 13 13:43:27 PDT 2018
Author: erichkeane
Date: Wed Jun 13 13:43:27 2018
New Revision: 334650
URL: http://llvm.org/viewvc/llvm-project?rev=334650&view=rev
Log:
Implement constexpr __builtin_*_overflow
As requested here:https://bugs.llvm.org/show_bug.cgi?id=37633
permit the __builtin_*_overflow builtins in constexpr functions.
Differential Revision: https://reviews.llvm.org/D48040
Modified:
cfe/trunk/lib/AST/ExprConstant.cpp
cfe/trunk/test/SemaCXX/builtins-overflow.cpp
Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=334650&r1=334649&r2=334650&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Wed Jun 13 13:43:27 2018
@@ -8155,6 +8155,124 @@ bool IntExprEvaluator::VisitBuiltinCallE
case Builtin::BIomp_is_initial_device:
// We can decide statically which value the runtime would return if called.
return Success(Info.getLangOpts().OpenMPIsDevice ? 0 : 1, E);
+ case Builtin::BI__builtin_add_overflow:
+ case Builtin::BI__builtin_sub_overflow:
+ case Builtin::BI__builtin_mul_overflow:
+ case Builtin::BI__builtin_sadd_overflow:
+ case Builtin::BI__builtin_uadd_overflow:
+ case Builtin::BI__builtin_uaddl_overflow:
+ case Builtin::BI__builtin_uaddll_overflow:
+ case Builtin::BI__builtin_usub_overflow:
+ case Builtin::BI__builtin_usubl_overflow:
+ case Builtin::BI__builtin_usubll_overflow:
+ case Builtin::BI__builtin_umul_overflow:
+ case Builtin::BI__builtin_umull_overflow:
+ case Builtin::BI__builtin_umulll_overflow:
+ case Builtin::BI__builtin_saddl_overflow:
+ case Builtin::BI__builtin_saddll_overflow:
+ case Builtin::BI__builtin_ssub_overflow:
+ case Builtin::BI__builtin_ssubl_overflow:
+ case Builtin::BI__builtin_ssubll_overflow:
+ case Builtin::BI__builtin_smul_overflow:
+ case Builtin::BI__builtin_smull_overflow:
+ case Builtin::BI__builtin_smulll_overflow: {
+ LValue ResultLValue;
+ APSInt LHS, RHS;
+
+ QualType ResultType = E->getArg(2)->getType()->getPointeeType();
+ if (!EvaluateInteger(E->getArg(0), LHS, Info) ||
+ !EvaluateInteger(E->getArg(1), RHS, Info) ||
+ !EvaluatePointer(E->getArg(2), ResultLValue, Info))
+ return false;
+
+ APSInt Result;
+ bool DidOverflow = false;
+
+ // If the types don't have to match, enlarge all 3 to the largest of them.
+ if (BuiltinOp == Builtin::BI__builtin_add_overflow ||
+ BuiltinOp == Builtin::BI__builtin_sub_overflow ||
+ BuiltinOp == Builtin::BI__builtin_mul_overflow) {
+ bool IsSigned = LHS.isSigned() || RHS.isSigned() ||
+ ResultType->isSignedIntegerOrEnumerationType();
+ bool AllSigned = LHS.isSigned() && RHS.isSigned() &&
+ ResultType->isSignedIntegerOrEnumerationType();
+ uint64_t LHSSize = LHS.getBitWidth();
+ uint64_t RHSSize = RHS.getBitWidth();
+ uint64_t ResultSize = Info.Ctx.getTypeSize(ResultType);
+ uint64_t MaxBits = std::max(std::max(LHSSize, RHSSize), ResultSize);
+
+ // Add an additional bit if the signedness isn't uniformly agreed to. We
+ // could do this ONLY if there is a signed and an unsigned that both have
+ // MaxBits, but the code to check that is pretty nasty. The issue will be
+ // caught in the shrink-to-result later anyway.
+ if (IsSigned && !AllSigned)
+ ++MaxBits;
+
+ LHS = APSInt(IsSigned ? LHS.sextOrSelf(MaxBits) : LHS.zextOrSelf(MaxBits),
+ !IsSigned);
+ RHS = APSInt(IsSigned ? RHS.sextOrSelf(MaxBits) : RHS.zextOrSelf(MaxBits),
+ !IsSigned);
+ Result = APSInt(MaxBits, !IsSigned);
+ }
+
+ // Find largest int.
+ switch (BuiltinOp) {
+ default:
+ llvm_unreachable("Invalid value for BuiltinOp");
+ case Builtin::BI__builtin_add_overflow:
+ case Builtin::BI__builtin_sadd_overflow:
+ case Builtin::BI__builtin_saddl_overflow:
+ case Builtin::BI__builtin_saddll_overflow:
+ case Builtin::BI__builtin_uadd_overflow:
+ case Builtin::BI__builtin_uaddl_overflow:
+ case Builtin::BI__builtin_uaddll_overflow:
+ Result = LHS.isSigned() ? LHS.sadd_ov(RHS, DidOverflow)
+ : LHS.uadd_ov(RHS, DidOverflow);
+ break;
+ case Builtin::BI__builtin_sub_overflow:
+ case Builtin::BI__builtin_ssub_overflow:
+ case Builtin::BI__builtin_ssubl_overflow:
+ case Builtin::BI__builtin_ssubll_overflow:
+ case Builtin::BI__builtin_usub_overflow:
+ case Builtin::BI__builtin_usubl_overflow:
+ case Builtin::BI__builtin_usubll_overflow:
+ Result = LHS.isSigned() ? LHS.ssub_ov(RHS, DidOverflow)
+ : LHS.usub_ov(RHS, DidOverflow);
+ break;
+ case Builtin::BI__builtin_mul_overflow:
+ case Builtin::BI__builtin_smul_overflow:
+ case Builtin::BI__builtin_smull_overflow:
+ case Builtin::BI__builtin_smulll_overflow:
+ case Builtin::BI__builtin_umul_overflow:
+ case Builtin::BI__builtin_umull_overflow:
+ case Builtin::BI__builtin_umulll_overflow:
+ Result = LHS.isSigned() ? LHS.smul_ov(RHS, DidOverflow)
+ : LHS.umul_ov(RHS, DidOverflow);
+ break;
+ }
+
+ // In the case where multiple sizes are allowed, truncate and see if
+ // the values are the same.
+ if (BuiltinOp == Builtin::BI__builtin_add_overflow ||
+ BuiltinOp == Builtin::BI__builtin_sub_overflow ||
+ BuiltinOp == Builtin::BI__builtin_mul_overflow) {
+ // APSInt doesn't have a TruncOrSelf, so we use extOrTrunc instead,
+ // since it will give us the behavior of a TruncOrSelf in the case where
+ // its parameter <= its size. We previously set Result to be at least the
+ // type-size of the result, so getTypeSize(ResultType) <= Result.BitWidth
+ // will work exactly like TruncOrSelf.
+ APSInt Temp = Result.extOrTrunc(Info.Ctx.getTypeSize(ResultType));
+ Temp.setIsSigned(ResultType->isSignedIntegerOrEnumerationType());
+
+ if (!APSInt::isSameValue(Temp, Result))
+ DidOverflow = true;
+ Result = Temp;
+ }
+
+ APValue APV{Result};
+ handleAssignment(Info, E, ResultLValue, ResultType, APV);
+ return Success(DidOverflow, E);
+ }
}
}
Modified: cfe/trunk/test/SemaCXX/builtins-overflow.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/builtins-overflow.cpp?rev=334650&r1=334649&r2=334650&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/builtins-overflow.cpp (original)
+++ cfe/trunk/test/SemaCXX/builtins-overflow.cpp Wed Jun 13 13:43:27 2018
@@ -1,6 +1,8 @@
// RUN: %clang_cc1 -fsyntax-only -std=c++17 -verify %s
// expected-no-diagnostics
+#include <limits.h>
+
int a() {
const int x = 3;
static int z;
@@ -13,3 +15,85 @@ int a2() {
constexpr int *y = &z;
return []() { return __builtin_sub_overflow(x, x, y); }();
}
+
+template<typename T>
+struct Result {
+ bool B;
+ T Value;
+ constexpr bool operator==(const Result<T> &Other) {
+ return B == Other.B && Value == Other.Value;
+ }
+};
+
+
+template <typename RET, typename LHS, typename RHS>
+constexpr Result<RET> add(LHS &&lhs, RHS &&rhs) {
+ RET sum{};
+ bool b = __builtin_add_overflow(lhs, rhs, &sum);
+ return {b, sum};
+}
+
+static_assert(add<short>(static_cast<char>(120), static_cast<char>(10)) == Result<short>{false, 130});
+static_assert(add<char>(static_cast<short>(120), static_cast<short>(10)) == Result<char>{true, -126});
+static_assert(add<unsigned int>(INT_MAX, INT_MAX) == Result<unsigned int>{false, static_cast<unsigned int>(INT_MAX) * 2u});
+static_assert(add<int>(static_cast<unsigned int>(INT_MAX), 1u) == Result<int>{true, INT_MIN});
+
+static_assert(add<int>(17, 22) == Result<int>{false, 39});
+static_assert(add<int>(INT_MAX - 22, 24) == Result<int>{true, INT_MIN + 1});
+static_assert(add<int>(INT_MIN + 22, -23) == Result<int>{true, INT_MAX});
+
+template <typename RET, typename LHS, typename RHS>
+constexpr Result<RET> sub(LHS &&lhs, RHS &&rhs) {
+ RET sum{};
+ bool b = __builtin_sub_overflow(lhs, rhs, &sum);
+ return {b, sum};
+}
+
+static_assert(sub<unsigned char>(static_cast<char>(0),static_cast<char>(1)) == Result<unsigned char>{true, UCHAR_MAX});
+static_assert(sub<char>(static_cast<unsigned char>(0),static_cast<unsigned char>(1)) == Result<char>{false, -1});
+static_assert(sub<unsigned short>(static_cast<short>(0),static_cast<short>(1)) == Result<unsigned short>{true, USHRT_MAX});
+
+static_assert(sub<int>(17,22) == Result<int>{false, -5});
+static_assert(sub<int>(INT_MAX - 22, -23) == Result<int>{true, INT_MIN});
+static_assert(sub<int>(INT_MIN + 22, 23) == Result<int>{true, INT_MAX});
+
+template <typename RET, typename LHS, typename RHS>
+constexpr Result<RET> mul(LHS &&lhs, RHS &&rhs) {
+ RET sum{};
+ bool b = __builtin_mul_overflow(lhs, rhs, &sum);
+ return {b, sum};
+}
+
+static_assert(mul<int>(17,22) == Result<int>{false, 374});
+static_assert(mul<int>(INT_MAX / 22, 23) == Result<int>{true, -2049870757});
+static_assert(mul<int>(INT_MIN / 22, -23) == Result<int>{true, -2049870757});
+
+constexpr Result<int> sadd(int lhs, int rhs) {
+ int sum{};
+ bool b = __builtin_sadd_overflow(lhs, rhs, &sum);
+ return {b, sum};
+}
+
+static_assert(sadd(17,22) == Result<int>{false, 39});
+static_assert(sadd(INT_MAX - 22, 23) == Result<int>{true, INT_MIN});
+static_assert(sadd(INT_MIN + 22, -23) == Result<int>{true, INT_MAX});
+
+constexpr Result<int> ssub(int lhs, int rhs) {
+ int sum{};
+ bool b = __builtin_ssub_overflow(lhs, rhs, &sum);
+ return {b, sum};
+}
+
+static_assert(ssub(17,22) == Result<int>{false, -5});
+static_assert(ssub(INT_MAX - 22, -23) == Result<int>{true, INT_MIN});
+static_assert(ssub(INT_MIN + 22, 23) == Result<int>{true, INT_MAX});
+
+constexpr Result<int> smul(int lhs, int rhs) {
+ int sum{};
+ bool b = __builtin_smul_overflow(lhs, rhs, &sum);
+ return {b, sum};
+}
+
+static_assert(smul(17,22) == Result<int>{false, 374});
+static_assert(smul(INT_MAX / 22, 23) == Result<int>{true, -2049870757});
+static_assert(smul(INT_MIN / 22, -23) == Result<int>{true, -2049870757});
More information about the cfe-commits
mailing list