Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / gcc / testsuite / gcc.dg / simulate-thread / speculative-store-2.c
1 /* { dg-do link } */
2 /* { dg-options "--param allow-store-data-races=0 -O2" } */
3 /* { dg-final { simulate-thread } } */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include "simulate-thread.h"
9
10 /* Test that speculative stores do not happen for --param
11    allow-store-data-races=0.  */
12
13 int count, insns;
14
15 struct obj {
16     int data;
17     struct obj *next;
18 } *q;
19
20 void simulate_thread_other_threads ()
21 {
22   ++insns;
23   ++count;
24 }
25
26 int simulate_thread_step_verify ()
27 {
28   return 0;
29 }
30
31 int simulate_thread_final_verify ()
32 {
33   /* If count != insns, someone must have cached `count' and stored a
34      racy value into it.  */
35   if (count != insns)
36     {
37       printf("FAIL: count was incorrectly cached\n");
38       return 1;
39     }
40   return 0;
41 }
42
43 /* Test that `count' is not written to unless p->data > 0.  */
44
45 __attribute__((noinline))
46 void simulate_thread_main()
47 {
48   struct obj *p;
49   for (p = q; p; p = p->next)
50     if (p->data > 0)
51       count++;
52 }
53
54 struct obj *
55 insert(struct obj *head, int data)
56 {
57   struct obj *t = (struct obj *) malloc (sizeof (struct obj));
58   t->next = head;
59   t->data = data;
60   return t;
61 }
62
63 int main()
64 {
65   q = insert (0, 0);
66   q = insert (q, 0);
67   q = insert (q, 0);
68   q = insert (q, 0);
69   q = insert (q, 0);
70
71   simulate_thread_main ();
72   simulate_thread_done ();
73   return 0;
74 }