From 4e93a29f7ed1a0055b4cc745328a4109df31b451 Mon Sep 17 00:00:00 2001 From: Jens Maurer Date: Thu, 28 Jul 2022 21:27:16 +0200 Subject: [PATCH] P1774R8 Portable assumptions --- source/declarations.tex | 37 +++++++++++++++++++++++++++++++++++++ source/expressions.tex | 31 ++++++++++++++++++++++++------- 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/source/declarations.tex b/source/declarations.tex index 0b2e7d86b1..a903060bfd 100644 --- a/source/declarations.tex +++ b/source/declarations.tex @@ -8529,6 +8529,43 @@ \end{codeblock} \end{example} +\rSec2[dcl.attr.assume]{Assumption attribute} + +The \grammarterm{attribute-token} \tcode{assume} may be applied to a null statement; +such a statement is an \defn{assumption}. +An \grammarterm{attribute-argument-clause} shall be present and +shall have the form: +\begin{ncsimplebnf} +\terminal{(} conditional-expression \terminal{)} +\end{ncsimplebnf} +The expression is contextually converted to \tcode{bool}\iref{conv.general}. +The expression is not evaluated. +If the converted expression would evaluate to \tcode{true} +at the point where the assumption appears, +the assumption has no effect. +Otherwise, the behavior is undefined. +\begin{note} +The expression is potentially evaluated\iref{basic.def.odr}. +The use of assumptions is intended to allow implementations +to analyze the form of the expression and +deduce information used to optimize the program. +Implementations are not required to deduce +any information from any particular assumption. +\end{note} +\begin{example} +\begin{codeblock} +int divide_by_32(int x) { + [[assume(x >= 0)]]; + return x/32; // The instructions produced for the division + // may omit handling of negative values. +} +int f(int y) { + [[assume(++y == 43)]]; // \tcode{y} is not incremented + return y; // statement may be replaced with \tcode{return 42;} +} +\end{codeblock} +\end{example} + \rSec2[dcl.attr.depend]{Carries dependency attribute}% \indextext{attribute!carries dependency} diff --git a/source/expressions.tex b/source/expressions.tex index 71906feade..49fdf39fe2 100644 --- a/source/expressions.tex +++ b/source/expressions.tex @@ -7359,7 +7359,8 @@ \item an operation that would have undefined behavior -as specified in \ref{intro} through \ref{cpp}; +as specified in \ref{intro} through \ref{cpp}, +excluding \ref{dcl.attr.assume}; \begin{footnote} This includes, for example, signed integer overflow\iref{expr.pre}, certain @@ -7506,12 +7507,28 @@ a \keyword{goto} statement\iref{stmt.goto}. \end{itemize} -If $E$ satisfies the constraints of a core constant expression, but -evaluation of $E$ would evaluate an operation that has undefined behavior -as specified in \ref{library} through \ref{\lastlibchapter}, or -an invocation of the \tcode{va_start} macro\iref{cstdarg.syn}, -it is unspecified whether $E$ is a core constant expression. - +It is unspecified whether $E$ is a core constant expression +if $E$ satisfies the constraints of a core constant expression, but +evaluation of $E$ would evaluate +\begin{itemize} +\item +an operation that has undefined behavior +as specified in \ref{library} through \ref{\lastlibchapter}, +\item +an invocation of the \tcode{va_start} macro\iref{cstdarg.syn}, or +\item +a statement with an assumption\iref{dcl.attr.assume} +whose converted \grammarterm{conditional-expression}, +if evaluated where the assumption appears, +would not disqualify $E$ from being a core constant expression and +would not evaluate to \tcode{true}. +\begin{note} +$E$ is not disqualified from being a core constant expression +if the hypothetical evaluation of +the converted \grammarterm{conditional-expression} +would disqualify $E$ from being a core constant expression. +\end{note} +\end{itemize} \begin{example} \begin{codeblock} int x; // not constant