staticanalysis / data-race-test

Automatically exported from code.google.com/p/data-race-test
0 stars 0 forks source link

tsanv2: instrumentation of reads of consts #97

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The following function ought to contain zero memory accesses:

void barfoo(int x, int y, int z);
void escape(const int *x);
extern const int g_x;

struct P {
  const int m_x;
  void foobar();

  P(int x)
    : m_x(x) {
  }
};

void P::foobar() {
  const int l_x = 42;
  escape(&l_x);
  barfoo(m_x, g_x, l_x);
}

void P::foobar() {
    27d0:       41 56                   push   %r14
    27d2:       53                      push   %rbx
    27d3:       50                      push   %rax
    27d4:       48 89 fb                mov    %rdi,%rbx
    27d7:       48 8b 7c 24 18          mov    0x18(%rsp),%rdi
    27dc:       e8 9f 03 00 00          callq  2b80 <__tsan_func_entry>
    27e1:       4c 8d 74 24 04          lea    0x4(%rsp),%r14
  const int l_x = 42;
    27e6:       4c 89 f7                mov    %r14,%rdi
    27e9:       e8 72 67 00 00          callq  8f60 <__tsan_write4>
    27ee:       c7 44 24 04 2a 00 00    movl   $0x2a,0x4(%rsp)
    27f5:       00 
  escape(&l_x);
    27f6:       4c 89 f7                mov    %r14,%rdi
    27f9:       e8 42 00 00 00          callq  2840 <escape(int const*)>
  barfoo(m_x, g_x, l_x);
    27fe:       48 89 df                mov    %rbx,%rdi
    2801:       e8 9a 85 00 00          callq  ada0 <__tsan_read4>
    2806:       4c 8b 35 73 e7 21 00    mov    0x21e773(%rip),%r14        # 220f80 <_DYNAMIC+0x208>
    280d:       8b 1b                   mov    (%rbx),%ebx
    280f:       4c 89 f7                mov    %r14,%rdi
    2812:       e8 89 85 00 00          callq  ada0 <__tsan_read4>
    2817:       41 8b 36                mov    (%r14),%esi
    281a:       89 df                   mov    %ebx,%edi
    281c:       ba 2a 00 00 00          mov    $0x2a,%edx
    2821:       e8 2a 00 00 00          callq  2850 <barfoo(int, int, int)>
}
    2826:       e8 95 02 00 00          callq  2ac0 <__tsan_func_exit>
    282b:       48 83 c4 08             add    $0x8,%rsp
    282f:       5b                      pop    %rbx
    2830:       41 5e                   pop    %r14
    2832:       c3                      retq   

Original issue reported on code.google.com by dvyu...@google.com on 2 Apr 2012 at 11:51

GoogleCodeExporter commented 9 years ago
The part about g_x (constant globals, both scalars and arrays)
is implemented in LLVM r154444.
Gives ~1.5% static instruction saving. 

I don't think you are right about m_x and l_x. 
In both cases the data can be const_casted and written to. 
Even if not, I see no way to check the constantness of these two in LLVM. 

Closing. Please reopen if you think I am wrong. 

Original comment by konstant...@gmail.com on 10 Apr 2012 at 10:35

GoogleCodeExporter commented 9 years ago
> In both cases the data can be const_casted and written to. 

They can't (mutable struct Foo aside).

> I see no way to check the constantness of these two in LLVM.

It's a pity.

Original comment by dvyu...@google.com on 11 Apr 2012 at 7:25