<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">Fixed in R336364. Thank you very much for the heads up!<o:p></o:p></span></p>
<p class="MsoNormal"><a name="_MailEndCompose"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></a></p>
<p class="MsoNormal"><b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> Evgenii Stepanov [mailto:eugeni.stepanov@gmail.com]
<br>
<b>Sent:</b> Tuesday, July 3, 2018 12:59 PM<br>
<b>To:</b> Keane, Erich <erich.keane@intel.com><br>
<b>Cc:</b> cfe-commits <cfe-commits@lists.llvm.org><br>
<b>Subject:</b> Re: r334650 - Implement constexpr __builtin_*_overflow<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">Hi,<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">with this change, the following compiles to "ret i32 undef":<o:p></o:p></p>
</div>
<div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">int main(int argc, char **argv) { <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> constexpr int x = 1; <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> constexpr int y = 2; <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> int z; <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> __builtin_sadd_overflow(x, y, &z); <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> return z; <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">}<o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">On Wed, Jun 13, 2018 at 1:43 PM, Erich Keane via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>> wrote:<o:p></o:p></p>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<p class="MsoNormal">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" target="_blank">
http://llvm.org/viewvc/llvm-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" target="_blank">https://bugs.llvm.org/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" target="_blank">
https://reviews.llvm.org/D48040</a><br>
<br>
Modified:<br>
cfe/trunk/lib/AST/ExprConstant.cpp<br>
cfe/trunk/test/SemaCXX/builtins-overflow.cpp<br>
<br>
Modified: cfe/trunk/lib/AST/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" target="_blank">
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=334650&r1=334649&r2=334650&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)<br>
+++ cfe/trunk/lib/AST/ExprConstant.cpp Wed Jun 13 13:43:27 2018<br>
@@ -8155,6 +8155,124 @@ bool IntExprEvaluator::VisitBuiltinCallE<br>
case Builtin::BIomp_is_initial_device:<br>
// We can decide statically which value the runtime would return if called.<br>
return Success(Info.getLangOpts().OpenMPIsDevice ? 0 : 1, E);<br>
+ case Builtin::BI__builtin_add_overflow:<br>
+ case Builtin::BI__builtin_sub_overflow:<br>
+ case Builtin::BI__builtin_mul_overflow:<br>
+ case Builtin::BI__builtin_sadd_overflow:<br>
+ case Builtin::BI__builtin_uadd_overflow:<br>
+ case Builtin::BI__builtin_uaddl_overflow:<br>
+ case Builtin::BI__builtin_uaddll_overflow:<br>
+ case Builtin::BI__builtin_usub_overflow:<br>
+ case Builtin::BI__builtin_usubl_overflow:<br>
+ case Builtin::BI__builtin_usubll_overflow:<br>
+ case Builtin::BI__builtin_umul_overflow:<br>
+ case Builtin::BI__builtin_umull_overflow:<br>
+ case Builtin::BI__builtin_umulll_overflow:<br>
+ case Builtin::BI__builtin_saddl_overflow:<br>
+ case Builtin::BI__builtin_saddll_overflow:<br>
+ case Builtin::BI__builtin_ssub_overflow:<br>
+ case Builtin::BI__builtin_ssubl_overflow:<br>
+ case Builtin::BI__builtin_ssubll_overflow:<br>
+ case Builtin::BI__builtin_smul_overflow:<br>
+ case Builtin::BI__builtin_smull_overflow:<br>
+ case Builtin::BI__builtin_smulll_overflow: {<br>
+ LValue ResultLValue;<br>
+ APSInt LHS, RHS;<br>
+<br>
+ QualType ResultType = E->getArg(2)->getType()->getPointeeType();<br>
+ if (!EvaluateInteger(E->getArg(0), 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_overflow ||<br>
+ BuiltinOp == Builtin::BI__builtin_sub_overflow ||<br>
+ BuiltinOp == Builtin::BI__builtin_mul_overflow) {<br>
+ bool IsSigned = LHS.isSigned() || RHS.isSigned() ||<br>
+ ResultType->isSignedIntegerOrEnumerationType();<br>
+ bool AllSigned = LHS.isSigned() && RHS.isSigned() &&<br>
+ ResultType->isSignedIntegerOrEnumerationType();<br>
+ uint64_t LHSSize = LHS.getBitWidth();<br>
+ uint64_t RHSSize = RHS.getBitWidth();<br>
+ uint64_t ResultSize = Info.Ctx.getTypeSize(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_overflow:<br>
+ case Builtin::BI__builtin_sadd_overflow:<br>
+ case Builtin::BI__builtin_saddl_overflow:<br>
+ case Builtin::BI__builtin_saddll_overflow:<br>
+ case Builtin::BI__builtin_uadd_overflow:<br>
+ case Builtin::BI__builtin_uaddl_overflow:<br>
+ case Builtin::BI__builtin_uaddll_overflow:<br>
+ Result = LHS.isSigned() ? LHS.sadd_ov(RHS, DidOverflow)<br>
+ : LHS.uadd_ov(RHS, DidOverflow);<br>
+ break;<br>
+ case Builtin::BI__builtin_sub_overflow:<br>
+ case Builtin::BI__builtin_ssub_overflow:<br>
+ case Builtin::BI__builtin_ssubl_overflow:<br>
+ case Builtin::BI__builtin_ssubll_overflow:<br>
+ case Builtin::BI__builtin_usub_overflow:<br>
+ case Builtin::BI__builtin_usubl_overflow:<br>
+ case Builtin::BI__builtin_usubll_overflow:<br>
+ Result = LHS.isSigned() ? LHS.ssub_ov(RHS, DidOverflow)<br>
+ : LHS.usub_ov(RHS, DidOverflow);<br>
+ break;<br>
+ case Builtin::BI__builtin_mul_overflow:<br>
+ case Builtin::BI__builtin_smul_overflow:<br>
+ case Builtin::BI__builtin_smull_overflow:<br>
+ case Builtin::BI__builtin_smulll_overflow:<br>
+ case Builtin::BI__builtin_umul_overflow:<br>
+ case Builtin::BI__builtin_umull_overflow:<br>
+ case Builtin::BI__builtin_umulll_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_overflow ||<br>
+ BuiltinOp == Builtin::BI__builtin_sub_overflow ||<br>
+ BuiltinOp == Builtin::BI__builtin_mul_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.getTypeSize(ResultType));<br>
+ Temp.setIsSigned(ResultType->isSignedIntegerOrEnumerationType());<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/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" target="_blank">
http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/builtins-overflow.cpp?rev=334650&r1=334649&r2=334650&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/test/SemaCXX/builtins-overflow.cpp (original)<br>
+++ cfe/trunk/test/SemaCXX/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>(static_cast<char>(120), static_cast<char>(10)) == Result<short>{false, 130});<br>
+static_assert(add<char>(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>(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_MAX - 22, 24) == Result<int>{true, INT_MIN + 1});<br>
+static_assert(add<int>(INT_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),static_cast<char>(1)) == Result<unsigned char>{true, UCHAR_MAX});<br>
+static_assert(sub<char>(static_cast<unsigned char>(0),static_cast<unsigned char>(1)) == Result<char>{false, -1});<br>
+static_assert(sub<unsigned short>(static_cast<short>(0),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_MAX - 22, -23) == Result<int>{true, INT_MIN});<br>
+static_assert(sub<int>(INT_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_MAX / 22, 23) == Result<int>{true, -2049870757});<br>
+static_assert(mul<int>(INT_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>
_______________________________________________<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" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><o:p></o:p></p>
</blockquote>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</body>
</html>