How to make rvalue link be a "restricted" alias?

The C ++ standard (N4618, $ 17.5.4.9, [res.on.argument], (1.3)) says that the implementation is free so as not to check the aliases on the rvalue link:

If a function argument is associated with an rvalue reference parameter, the implementation may assume that this parameter is a unique reference to this argument. [Note: If the parameter is a general parameter of the form T & & and the type lvalue is associated, the argument is associated with the lvalue reference (14.8.2.1) and, thus, it does not fall under the previous sentence. - final note] [Note: If a program passes the value l to x by passing this lvalue to the library function (for example, by calling a function with the argument stand :: move (x)), the program effectively requests this function to treat this lvalue as temporary. Implementation is free to optimize alias checks that may be required if the argument was lvalue. - final note]

I would like this assumption to apply to the entire translation unit. For example, the function f:

 class A{
    int a;
      //....
 };
 int f(A&& r1,A&& r2){
   r1.a=10;
   r2.a=3;
   return r1.a;
 }

Performs the following assembly:

    movl    $10, (%rdi)
    movl    $3, (%rsi)
    movl    (%rdi), %eax
    retq

If the return value is a load r1.a, since the compiler takes into account what fcan be called as follows:f(move(an_A),move(an_A))

Now, if I declare one of the parameters as limited:

int f(A&& __restrict__ r1,A&& r2){
   r1.a=10;
   r2.a=3;
   return r1.a;
 }

We get the following assembly:

    movl    $10, (%rdi)
    movl    $3, (%rsi)
    movl    $10, %eax
    retq

In this case, we say to the compiler, it is assumed that the parameter r1and r2never limited to one and the same object. Thus, it directly returns the value stored in a.a.

This is exactly the kind of optimization that is allowed by the C ++ standard for any library function that uses the rvalue reference parameters.

, ( ) , f(move(an_A),move(an_A)), , " ", , , , rvalue, .

, :

  • , f(move(an_A),move(an_A)), f ?
  • , , rvalue ""?
  • - , , rvalue ""?
+4

Source: https://habr.com/ru/post/1669161/


All Articles