Is function call a memory barrier?

后端 未结 5 2006
情深已故
情深已故 2020-12-24 13:52

Consider this C code:

extern volatile int hardware_reg;

void f(const void *src, size_t len)
{
    void *dst = ;

    hardware_reg = 1;    
         


        
5条回答
  •  青春惊慌失措
    2020-12-24 14:35

    Here is a slightly modified example, compiled with gcc 7.2.1 on x86-64:

    #include 
    static int temp;
    extern volatile int hardware_reg;
    int foo (int x)
    {
        hardware_reg = 0;
        memcpy(&temp, &x, sizeof(int));
        hardware_reg = 1;
        return temp;
    }
    

    gcc knows that the memcpy() is the same as an assignment, and knows that temp is not accessed anywhere else, so temp and the memcpy() disappear completely from the generated code:

    foo:
        movl    $0, hardware_reg(%rip)
        movl    %edi, %eax
        movl    $1, hardware_reg(%rip)
        ret
    

提交回复
热议问题