简体   繁体   English

如何在此指针上应用restrict限定符

[英]How to apply restrict qualifier on this pointer

How can I apply GCC's/Clang's __restrict__ qualifier to the this pointer of a class? 如何将GCC的/ Clang的__restrict__限定符应用于类的this指针?
This question was inspired by Richard Powell's CppCon 2018 talk, " How to Argue(ment). " I saw a similar question " restrict qualifier on member functions (restrict this pointer). " (All code can be found on Compiler Explorer ) 这个问题的灵感来自Richard Powell的CppCon 2018演讲,“ How to Argue(ment)。 ”我看到了类似的问题“ 限制成员函数的限定符(限制此指针)。 ”(所有代码都可以在Compiler Explorer上找到)

void bar();

class Foo {
 public:
  int this_example() const {
    if (value > 0) {
      bar();
      return value;
    } else {
      return value;
    }
  }

 private:
  int value;
};

The above code generates the following assembly. 上面的代码生成以下程序集。 In it, we can see that value has to be loaded twice, via the this pointer. 在其中,我们可以看到value必须通过this指针加载两次。 This makes sense, it is a consequence that C++ inherited from C and the restrict qualifier allows the programmer to turn off the behavior. 这是有道理的,这是C ++继承自C和限制限定符允许程序员关闭行为的结果。 I can find no way to enable the restrict functionality for the this pointer. 我找不到为this指针启用restrict功能的方法。

Foo::this_example() const:               # @Foo::this_example() const
        push    rbx
        mov     eax, dword ptr [rdi]
        test    eax, eax
        jle     .LBB2_2
        mov     rbx, rdi
        call    bar()
        mov     eax, dword ptr [rbx]
.LBB2_2:
        pop     rbx
        ret

On the Compiler Explorer page, I show examples of method arguments using __restrict__ to elide the second load. Compiler Explorer页面上,我展示了使用__restrict__来消除第二次加载的方法参数的示例。 There is also an example of passing a struct reference to a function and using __restrict__ to elide the second load. 还有一个将struct引用传递给函数并使用__restrict__来消除第二个加载的示例。

I can imagine a world where the compiler would allow the programmer to mention the implicit this pointer in the arguments of a method. 我可以想象一个编译器允许程序员在方法的参数中提到隐式this指针的世界。 The compiler could then allow application of qualifiers to the this pointer. 然后,编译器可以允许将限定符应用于this指针。 See code below for an example. 请参阅下面的代码示例。

class Foo {
 public:
  int unrestricted(Foo *this);
  int restricted(Foo *__restrict__ this);
};

As a follow up question, is there something in the C++ standard or a C++ guideline that would make it so that this could never have the restrict qualifier? 作为后续行动的问题,是有什么在C ++标准或C ++指南,将让这个this可能永远不会有限制预选赛?

GCC's documentation for __restrict__ (as well as the linked question) mentions that you can actually restrict this : GCC的__restrict__文档 (以及链接的问题)提到你实际上可以限制this

You may also specify whether a member function's this pointer is unaliased by using __restrict__ as a member function qualifier. 您还可以使用__restrict__作为成员函数限定符来指定成员函数的this指针是否为unaliased。

 void T::fn () __restrict__ { /* … */ } 

Within the body of T::fn , this has the effective definition T *__restrict__ const this . T::fn的主体内, this具有有效的定义T *__restrict__ const this Notice that the interpretation of a __restrict__ member function qualifier is different to that of const or volatile qualifier, in that it is applied to the pointer rather than the object. 请注意, __restrict__成员函数限定符的解释与constvolatile限定符的解释不同,因为它应用于指针而不是对象。 This is consistent with other compilers that implement restricted pointers. 这与实现受限指针的其他编译器一致。

Note, however, that marking the this pointer as such does not prevent the second load. 但请注意, this指针标记为不会阻止第二次加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM