Julian Seward 0e7c46401b Fix this test to work properly with accurate CmpEQ/NE definedness tracking
Memcheck reports an error on "if (n == 42)" in this test.  Unless, that is,
accurate CmpEQ/NE definedness tracking is enabled.  If you stare at this
long enough it is possible to see that the test "n == 42" isn't actually
undefined, because |n| is only ever zero or one, and only its least
significant bit is undefined.  So the equality comparison against 42 is
defined because there are corresponding bits in the two operands that are
different and are both defined.

This commit fixes that by comparing with 1, which forces the result to
really depend on the only undefined bit in |n|.

I also added robustification:

* return arbitrary values from gcc_cant_inline_me(), so as to avoid gcc
  simply copying the input to the output or otherwise deleting the
  conditional branch.

* marking gcc_cant_inline_me() as un-inlineable

* Putting compiler barriers in the second conditional in main(), so gcc
  can't simply ignore the result of the call to gcc_cant_inline_me() and
  then delete the call entirely.
2017-12-07 13:31:38 +01:00

29 lines
494 B
C

#include <stdio.h>
#include <stdlib.h>
int foo, bar;
#define CBAR do { __asm__ __volatile__("":::"cc","memory"); } while (0)
int* gcc_cant_inline_me ( int );
int main ()
{
int *x, y;
x = (int *) malloc (sizeof (int));
y = *x == 173;
if (gcc_cant_inline_me(y) == &foo) { CBAR; } else { CBAR; }
return 0;
}
/* must be AFTER main */
__attribute__((noinline)) int* gcc_cant_inline_me ( int n )
{
if (n == 1)
return &foo; /* foo! */
else
return &bar; /* bar! */
}