Remove two generic configure files for libstdc++
[dragonfly.git] / contrib / gcc-5.0 / gcc / tsan.c
1 /* GCC instrumentation plugin for ThreadSanitizer.
2    Copyright (C) 2011-2015 Free Software Foundation, Inc.
3    Contributed by Dmitry Vyukov <dvyukov@google.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "options.h"
33 #include "wide-int.h"
34 #include "inchash.h"
35 #include "tree.h"
36 #include "fold-const.h"
37 #include "hashtab.h"
38 #include "tm.h"
39 #include "hard-reg-set.h"
40 #include "function.h"
41 #include "rtl.h"
42 #include "flags.h"
43 #include "statistics.h"
44 #include "real.h"
45 #include "fixed-value.h"
46 #include "insn-config.h"
47 #include "expmed.h"
48 #include "dojump.h"
49 #include "explow.h"
50 #include "calls.h"
51 #include "emit-rtl.h"
52 #include "varasm.h"
53 #include "stmt.h"
54 #include "expr.h"
55 #include "intl.h"
56 #include "predict.h"
57 #include "dominance.h"
58 #include "cfg.h"
59 #include "basic-block.h"
60 #include "tree-ssa-alias.h"
61 #include "internal-fn.h"
62 #include "gimple-expr.h"
63 #include "is-a.h"
64 #include "gimple.h"
65 #include "gimplify.h"
66 #include "gimple-iterator.h"
67 #include "gimplify-me.h"
68 #include "gimple-ssa.h"
69 #include "hash-map.h"
70 #include "plugin-api.h"
71 #include "ipa-ref.h"
72 #include "cgraph.h"
73 #include "tree-cfg.h"
74 #include "stringpool.h"
75 #include "tree-ssanames.h"
76 #include "tree-pass.h"
77 #include "tree-iterator.h"
78 #include "langhooks.h"
79 #include "output.h"
80 #include "target.h"
81 #include "diagnostic.h"
82 #include "tree-ssa-propagate.h"
83 #include "tree-ssa-loop-ivopts.h"
84 #include "tsan.h"
85 #include "asan.h"
86 #include "builtins.h"
87
88 /* Number of instrumented memory accesses in the current function.  */
89
90 /* Builds the following decl
91    void __tsan_read/writeX (void *addr);  */
92
93 static tree
94 get_memory_access_decl (bool is_write, unsigned size)
95 {
96   enum built_in_function fcode;
97
98   if (size <= 1)
99     fcode = is_write ? BUILT_IN_TSAN_WRITE1
100                      : BUILT_IN_TSAN_READ1;
101   else if (size <= 3)
102     fcode = is_write ? BUILT_IN_TSAN_WRITE2
103                      : BUILT_IN_TSAN_READ2;
104   else if (size <= 7)
105     fcode = is_write ? BUILT_IN_TSAN_WRITE4
106                      : BUILT_IN_TSAN_READ4;
107   else if (size <= 15)
108     fcode = is_write ? BUILT_IN_TSAN_WRITE8
109                      : BUILT_IN_TSAN_READ8;
110   else
111     fcode = is_write ? BUILT_IN_TSAN_WRITE16
112                      : BUILT_IN_TSAN_READ16;
113
114   return builtin_decl_implicit (fcode);
115 }
116
117 /* Check as to whether EXPR refers to a store to vptr.  */
118
119 static tree
120 is_vptr_store (gimple stmt, tree expr, bool is_write)
121 {
122   if (is_write == true
123       && gimple_assign_single_p (stmt)
124       && TREE_CODE (expr) == COMPONENT_REF)
125     {
126       tree field = TREE_OPERAND (expr, 1);
127       if (TREE_CODE (field) == FIELD_DECL
128           && DECL_VIRTUAL_P (field))
129         return gimple_assign_rhs1 (stmt);
130     }
131   return NULL;
132 }
133
134 /* Instruments EXPR if needed. If any instrumentation is inserted,
135    return true.  */
136
137 static bool
138 instrument_expr (gimple_stmt_iterator gsi, tree expr, bool is_write)
139 {
140   tree base, rhs, expr_ptr, builtin_decl;
141   basic_block bb;
142   HOST_WIDE_INT size;
143   gimple stmt, g;
144   gimple_seq seq;
145   location_t loc;
146   unsigned int align;
147
148   size = int_size_in_bytes (TREE_TYPE (expr));
149   if (size <= 0)
150     return false;
151
152   HOST_WIDE_INT bitsize, bitpos;
153   tree offset;
154   machine_mode mode;
155   int volatilep = 0, unsignedp = 0;
156   base = get_inner_reference (expr, &bitsize, &bitpos, &offset,
157                               &mode, &unsignedp, &volatilep, false);
158
159   /* No need to instrument accesses to decls that don't escape,
160      they can't escape to other threads then.  */
161   if (DECL_P (base) && !is_global_var (base))
162     {
163       struct pt_solution pt;
164       memset (&pt, 0, sizeof (pt));
165       pt.escaped = 1;
166       pt.ipa_escaped = flag_ipa_pta != 0;
167       if (!pt_solution_includes (&pt, base))
168         return false;
169       if (!may_be_aliased (base))
170         return false;
171     }
172
173   if (TREE_READONLY (base)
174       || (TREE_CODE (base) == VAR_DECL
175           && DECL_HARD_REGISTER (base)))
176     return false;
177
178   stmt = gsi_stmt (gsi);
179   loc = gimple_location (stmt);
180   rhs = is_vptr_store (stmt, expr, is_write);
181
182   if ((TREE_CODE (expr) == COMPONENT_REF
183        && DECL_BIT_FIELD_TYPE (TREE_OPERAND (expr, 1)))
184       || TREE_CODE (expr) == BIT_FIELD_REF)
185     {
186       base = TREE_OPERAND (expr, 0);
187       if (TREE_CODE (expr) == COMPONENT_REF)
188         {
189           expr = TREE_OPERAND (expr, 1);
190           if (is_write && DECL_BIT_FIELD_REPRESENTATIVE (expr))
191             expr = DECL_BIT_FIELD_REPRESENTATIVE (expr);
192           if (!tree_fits_uhwi_p (DECL_FIELD_OFFSET (expr))
193               || !tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (expr))
194               || !tree_fits_uhwi_p (DECL_SIZE (expr)))
195             return false;
196           bitpos = tree_to_uhwi (DECL_FIELD_OFFSET (expr)) * BITS_PER_UNIT
197                    + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (expr));
198           bitsize = tree_to_uhwi (DECL_SIZE (expr));
199         }
200       else
201         {
202           if (!tree_fits_uhwi_p (TREE_OPERAND (expr, 2))
203               || !tree_fits_uhwi_p (TREE_OPERAND (expr, 1)))
204             return false;
205           bitpos = tree_to_uhwi (TREE_OPERAND (expr, 2));
206           bitsize = tree_to_uhwi (TREE_OPERAND (expr, 1));
207         }
208       if (bitpos < 0 || bitsize <= 0)
209         return false;
210       size = (bitpos % BITS_PER_UNIT + bitsize + BITS_PER_UNIT - 1)
211              / BITS_PER_UNIT;
212       if (may_be_nonaddressable_p (base))
213         return false;
214       align = get_object_alignment (base);
215       if (align < BITS_PER_UNIT)
216         return false;
217       bitpos = bitpos & ~(BITS_PER_UNIT - 1);
218       if ((align - 1) & bitpos)
219         {
220           align = (align - 1) & bitpos;
221           align = align & -align;
222         }
223       expr = build_fold_addr_expr (unshare_expr (base));
224       expr = build2 (MEM_REF, char_type_node, expr,
225                      build_int_cst (TREE_TYPE (expr), bitpos / BITS_PER_UNIT));
226       expr_ptr = build_fold_addr_expr (expr);
227     }
228   else
229     {
230       if (may_be_nonaddressable_p (expr))
231         return false;
232       align = get_object_alignment (expr);
233       if (align < BITS_PER_UNIT)
234         return false;
235       expr_ptr = build_fold_addr_expr (unshare_expr (expr));
236     }
237   expr_ptr = force_gimple_operand (expr_ptr, &seq, true, NULL_TREE);
238   if ((size & (size - 1)) != 0 || size > 16
239       || align < MIN (size, 8) * BITS_PER_UNIT)
240     {
241       builtin_decl = builtin_decl_implicit (is_write
242                                             ? BUILT_IN_TSAN_WRITE_RANGE
243                                             : BUILT_IN_TSAN_READ_RANGE);
244       g = gimple_build_call (builtin_decl, 2, expr_ptr, size_int (size));
245     }
246   else if (rhs == NULL)
247     g = gimple_build_call (get_memory_access_decl (is_write, size),
248                            1, expr_ptr);
249   else
250     {
251       builtin_decl = builtin_decl_implicit (BUILT_IN_TSAN_VPTR_UPDATE);
252       g = gimple_build_call (builtin_decl, 2, expr_ptr, unshare_expr (rhs));
253     }
254   gimple_set_location (g, loc);
255   gimple_seq_add_stmt_without_update (&seq, g);
256   /* Instrumentation for assignment of a function result
257      must be inserted after the call.  Instrumentation for
258      reads of function arguments must be inserted before the call.
259      That's because the call can contain synchronization.  */
260   if (is_gimple_call (stmt) && is_write)
261     {
262       /* If the call can throw, it must be the last stmt in
263          a basic block, so the instrumented stmts need to be
264          inserted in successor bbs.  */
265       if (is_ctrl_altering_stmt (stmt))
266         {
267           edge e;
268
269           bb = gsi_bb (gsi);
270           e = find_fallthru_edge (bb->succs);
271           if (e)
272             gsi_insert_seq_on_edge_immediate (e, seq);
273         }
274       else
275         gsi_insert_seq_after (&gsi, seq, GSI_NEW_STMT);
276     }
277   else
278     gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
279
280   return true;
281 }
282
283 /* Actions for sync/atomic builtin transformations.  */
284 enum tsan_atomic_action
285 {
286   check_last, add_seq_cst, add_acquire, weak_cas, strong_cas,
287   bool_cas, val_cas, lock_release, fetch_op, fetch_op_seq_cst
288 };
289
290 /* Table how to map sync/atomic builtins to their corresponding
291    tsan equivalents.  */
292 static const struct tsan_map_atomic
293 {
294   enum built_in_function fcode, tsan_fcode;
295   enum tsan_atomic_action action;
296   enum tree_code code;
297 } tsan_atomic_table[] =
298 {
299 #define TRANSFORM(fcode, tsan_fcode, action, code) \
300   { BUILT_IN_##fcode, BUILT_IN_##tsan_fcode, action, code }
301 #define CHECK_LAST(fcode, tsan_fcode) \
302   TRANSFORM (fcode, tsan_fcode, check_last, ERROR_MARK)
303 #define ADD_SEQ_CST(fcode, tsan_fcode) \
304   TRANSFORM (fcode, tsan_fcode, add_seq_cst, ERROR_MARK)
305 #define ADD_ACQUIRE(fcode, tsan_fcode) \
306   TRANSFORM (fcode, tsan_fcode, add_acquire, ERROR_MARK)
307 #define WEAK_CAS(fcode, tsan_fcode) \
308   TRANSFORM (fcode, tsan_fcode, weak_cas, ERROR_MARK)
309 #define STRONG_CAS(fcode, tsan_fcode) \
310   TRANSFORM (fcode, tsan_fcode, strong_cas, ERROR_MARK)
311 #define BOOL_CAS(fcode, tsan_fcode) \
312   TRANSFORM (fcode, tsan_fcode, bool_cas, ERROR_MARK)
313 #define VAL_CAS(fcode, tsan_fcode) \
314   TRANSFORM (fcode, tsan_fcode, val_cas, ERROR_MARK)
315 #define LOCK_RELEASE(fcode, tsan_fcode) \
316   TRANSFORM (fcode, tsan_fcode, lock_release, ERROR_MARK)
317 #define FETCH_OP(fcode, tsan_fcode, code) \
318   TRANSFORM (fcode, tsan_fcode, fetch_op, code)
319 #define FETCH_OPS(fcode, tsan_fcode, code) \
320   TRANSFORM (fcode, tsan_fcode, fetch_op_seq_cst, code)
321
322   CHECK_LAST (ATOMIC_LOAD_1, TSAN_ATOMIC8_LOAD),
323   CHECK_LAST (ATOMIC_LOAD_2, TSAN_ATOMIC16_LOAD),
324   CHECK_LAST (ATOMIC_LOAD_4, TSAN_ATOMIC32_LOAD),
325   CHECK_LAST (ATOMIC_LOAD_8, TSAN_ATOMIC64_LOAD),
326   CHECK_LAST (ATOMIC_LOAD_16, TSAN_ATOMIC128_LOAD),
327   CHECK_LAST (ATOMIC_STORE_1, TSAN_ATOMIC8_STORE),
328   CHECK_LAST (ATOMIC_STORE_2, TSAN_ATOMIC16_STORE),
329   CHECK_LAST (ATOMIC_STORE_4, TSAN_ATOMIC32_STORE),
330   CHECK_LAST (ATOMIC_STORE_8, TSAN_ATOMIC64_STORE),
331   CHECK_LAST (ATOMIC_STORE_16, TSAN_ATOMIC128_STORE),
332   CHECK_LAST (ATOMIC_EXCHANGE_1, TSAN_ATOMIC8_EXCHANGE),
333   CHECK_LAST (ATOMIC_EXCHANGE_2, TSAN_ATOMIC16_EXCHANGE),
334   CHECK_LAST (ATOMIC_EXCHANGE_4, TSAN_ATOMIC32_EXCHANGE),
335   CHECK_LAST (ATOMIC_EXCHANGE_8, TSAN_ATOMIC64_EXCHANGE),
336   CHECK_LAST (ATOMIC_EXCHANGE_16, TSAN_ATOMIC128_EXCHANGE),
337   CHECK_LAST (ATOMIC_FETCH_ADD_1, TSAN_ATOMIC8_FETCH_ADD),
338   CHECK_LAST (ATOMIC_FETCH_ADD_2, TSAN_ATOMIC16_FETCH_ADD),
339   CHECK_LAST (ATOMIC_FETCH_ADD_4, TSAN_ATOMIC32_FETCH_ADD),
340   CHECK_LAST (ATOMIC_FETCH_ADD_8, TSAN_ATOMIC64_FETCH_ADD),
341   CHECK_LAST (ATOMIC_FETCH_ADD_16, TSAN_ATOMIC128_FETCH_ADD),
342   CHECK_LAST (ATOMIC_FETCH_SUB_1, TSAN_ATOMIC8_FETCH_SUB),
343   CHECK_LAST (ATOMIC_FETCH_SUB_2, TSAN_ATOMIC16_FETCH_SUB),
344   CHECK_LAST (ATOMIC_FETCH_SUB_4, TSAN_ATOMIC32_FETCH_SUB),
345   CHECK_LAST (ATOMIC_FETCH_SUB_8, TSAN_ATOMIC64_FETCH_SUB),
346   CHECK_LAST (ATOMIC_FETCH_SUB_16, TSAN_ATOMIC128_FETCH_SUB),
347   CHECK_LAST (ATOMIC_FETCH_AND_1, TSAN_ATOMIC8_FETCH_AND),
348   CHECK_LAST (ATOMIC_FETCH_AND_2, TSAN_ATOMIC16_FETCH_AND),
349   CHECK_LAST (ATOMIC_FETCH_AND_4, TSAN_ATOMIC32_FETCH_AND),
350   CHECK_LAST (ATOMIC_FETCH_AND_8, TSAN_ATOMIC64_FETCH_AND),
351   CHECK_LAST (ATOMIC_FETCH_AND_16, TSAN_ATOMIC128_FETCH_AND),
352   CHECK_LAST (ATOMIC_FETCH_OR_1, TSAN_ATOMIC8_FETCH_OR),
353   CHECK_LAST (ATOMIC_FETCH_OR_2, TSAN_ATOMIC16_FETCH_OR),
354   CHECK_LAST (ATOMIC_FETCH_OR_4, TSAN_ATOMIC32_FETCH_OR),
355   CHECK_LAST (ATOMIC_FETCH_OR_8, TSAN_ATOMIC64_FETCH_OR),
356   CHECK_LAST (ATOMIC_FETCH_OR_16, TSAN_ATOMIC128_FETCH_OR),
357   CHECK_LAST (ATOMIC_FETCH_XOR_1, TSAN_ATOMIC8_FETCH_XOR),
358   CHECK_LAST (ATOMIC_FETCH_XOR_2, TSAN_ATOMIC16_FETCH_XOR),
359   CHECK_LAST (ATOMIC_FETCH_XOR_4, TSAN_ATOMIC32_FETCH_XOR),
360   CHECK_LAST (ATOMIC_FETCH_XOR_8, TSAN_ATOMIC64_FETCH_XOR),
361   CHECK_LAST (ATOMIC_FETCH_XOR_16, TSAN_ATOMIC128_FETCH_XOR),
362   CHECK_LAST (ATOMIC_FETCH_NAND_1, TSAN_ATOMIC8_FETCH_NAND),
363   CHECK_LAST (ATOMIC_FETCH_NAND_2, TSAN_ATOMIC16_FETCH_NAND),
364   CHECK_LAST (ATOMIC_FETCH_NAND_4, TSAN_ATOMIC32_FETCH_NAND),
365   CHECK_LAST (ATOMIC_FETCH_NAND_8, TSAN_ATOMIC64_FETCH_NAND),
366   CHECK_LAST (ATOMIC_FETCH_NAND_16, TSAN_ATOMIC128_FETCH_NAND),
367
368   CHECK_LAST (ATOMIC_THREAD_FENCE, TSAN_ATOMIC_THREAD_FENCE),
369   CHECK_LAST (ATOMIC_SIGNAL_FENCE, TSAN_ATOMIC_SIGNAL_FENCE),
370
371   FETCH_OP (ATOMIC_ADD_FETCH_1, TSAN_ATOMIC8_FETCH_ADD, PLUS_EXPR),
372   FETCH_OP (ATOMIC_ADD_FETCH_2, TSAN_ATOMIC16_FETCH_ADD, PLUS_EXPR),
373   FETCH_OP (ATOMIC_ADD_FETCH_4, TSAN_ATOMIC32_FETCH_ADD, PLUS_EXPR),
374   FETCH_OP (ATOMIC_ADD_FETCH_8, TSAN_ATOMIC64_FETCH_ADD, PLUS_EXPR),
375   FETCH_OP (ATOMIC_ADD_FETCH_16, TSAN_ATOMIC128_FETCH_ADD, PLUS_EXPR),
376   FETCH_OP (ATOMIC_SUB_FETCH_1, TSAN_ATOMIC8_FETCH_SUB, MINUS_EXPR),
377   FETCH_OP (ATOMIC_SUB_FETCH_2, TSAN_ATOMIC16_FETCH_SUB, MINUS_EXPR),
378   FETCH_OP (ATOMIC_SUB_FETCH_4, TSAN_ATOMIC32_FETCH_SUB, MINUS_EXPR),
379   FETCH_OP (ATOMIC_SUB_FETCH_8, TSAN_ATOMIC64_FETCH_SUB, MINUS_EXPR),
380   FETCH_OP (ATOMIC_SUB_FETCH_16, TSAN_ATOMIC128_FETCH_SUB, MINUS_EXPR),
381   FETCH_OP (ATOMIC_AND_FETCH_1, TSAN_ATOMIC8_FETCH_AND, BIT_AND_EXPR),
382   FETCH_OP (ATOMIC_AND_FETCH_2, TSAN_ATOMIC16_FETCH_AND, BIT_AND_EXPR),
383   FETCH_OP (ATOMIC_AND_FETCH_4, TSAN_ATOMIC32_FETCH_AND, BIT_AND_EXPR),
384   FETCH_OP (ATOMIC_AND_FETCH_8, TSAN_ATOMIC64_FETCH_AND, BIT_AND_EXPR),
385   FETCH_OP (ATOMIC_AND_FETCH_16, TSAN_ATOMIC128_FETCH_AND, BIT_AND_EXPR),
386   FETCH_OP (ATOMIC_OR_FETCH_1, TSAN_ATOMIC8_FETCH_OR, BIT_IOR_EXPR),
387   FETCH_OP (ATOMIC_OR_FETCH_2, TSAN_ATOMIC16_FETCH_OR, BIT_IOR_EXPR),
388   FETCH_OP (ATOMIC_OR_FETCH_4, TSAN_ATOMIC32_FETCH_OR, BIT_IOR_EXPR),
389   FETCH_OP (ATOMIC_OR_FETCH_8, TSAN_ATOMIC64_FETCH_OR, BIT_IOR_EXPR),
390   FETCH_OP (ATOMIC_OR_FETCH_16, TSAN_ATOMIC128_FETCH_OR, BIT_IOR_EXPR),
391   FETCH_OP (ATOMIC_XOR_FETCH_1, TSAN_ATOMIC8_FETCH_XOR, BIT_XOR_EXPR),
392   FETCH_OP (ATOMIC_XOR_FETCH_2, TSAN_ATOMIC16_FETCH_XOR, BIT_XOR_EXPR),
393   FETCH_OP (ATOMIC_XOR_FETCH_4, TSAN_ATOMIC32_FETCH_XOR, BIT_XOR_EXPR),
394   FETCH_OP (ATOMIC_XOR_FETCH_8, TSAN_ATOMIC64_FETCH_XOR, BIT_XOR_EXPR),
395   FETCH_OP (ATOMIC_XOR_FETCH_16, TSAN_ATOMIC128_FETCH_XOR, BIT_XOR_EXPR),
396   FETCH_OP (ATOMIC_NAND_FETCH_1, TSAN_ATOMIC8_FETCH_NAND, BIT_NOT_EXPR),
397   FETCH_OP (ATOMIC_NAND_FETCH_2, TSAN_ATOMIC16_FETCH_NAND, BIT_NOT_EXPR),
398   FETCH_OP (ATOMIC_NAND_FETCH_4, TSAN_ATOMIC32_FETCH_NAND, BIT_NOT_EXPR),
399   FETCH_OP (ATOMIC_NAND_FETCH_8, TSAN_ATOMIC64_FETCH_NAND, BIT_NOT_EXPR),
400   FETCH_OP (ATOMIC_NAND_FETCH_16, TSAN_ATOMIC128_FETCH_NAND, BIT_NOT_EXPR),
401
402   ADD_ACQUIRE (SYNC_LOCK_TEST_AND_SET_1, TSAN_ATOMIC8_EXCHANGE),
403   ADD_ACQUIRE (SYNC_LOCK_TEST_AND_SET_2, TSAN_ATOMIC16_EXCHANGE),
404   ADD_ACQUIRE (SYNC_LOCK_TEST_AND_SET_4, TSAN_ATOMIC32_EXCHANGE),
405   ADD_ACQUIRE (SYNC_LOCK_TEST_AND_SET_8, TSAN_ATOMIC64_EXCHANGE),
406   ADD_ACQUIRE (SYNC_LOCK_TEST_AND_SET_16, TSAN_ATOMIC128_EXCHANGE),
407
408   ADD_SEQ_CST (SYNC_FETCH_AND_ADD_1, TSAN_ATOMIC8_FETCH_ADD),
409   ADD_SEQ_CST (SYNC_FETCH_AND_ADD_2, TSAN_ATOMIC16_FETCH_ADD),
410   ADD_SEQ_CST (SYNC_FETCH_AND_ADD_4, TSAN_ATOMIC32_FETCH_ADD),
411   ADD_SEQ_CST (SYNC_FETCH_AND_ADD_8, TSAN_ATOMIC64_FETCH_ADD),
412   ADD_SEQ_CST (SYNC_FETCH_AND_ADD_16, TSAN_ATOMIC128_FETCH_ADD),
413   ADD_SEQ_CST (SYNC_FETCH_AND_SUB_1, TSAN_ATOMIC8_FETCH_SUB),
414   ADD_SEQ_CST (SYNC_FETCH_AND_SUB_2, TSAN_ATOMIC16_FETCH_SUB),
415   ADD_SEQ_CST (SYNC_FETCH_AND_SUB_4, TSAN_ATOMIC32_FETCH_SUB),
416   ADD_SEQ_CST (SYNC_FETCH_AND_SUB_8, TSAN_ATOMIC64_FETCH_SUB),
417   ADD_SEQ_CST (SYNC_FETCH_AND_SUB_16, TSAN_ATOMIC128_FETCH_SUB),
418   ADD_SEQ_CST (SYNC_FETCH_AND_AND_1, TSAN_ATOMIC8_FETCH_AND),
419   ADD_SEQ_CST (SYNC_FETCH_AND_AND_2, TSAN_ATOMIC16_FETCH_AND),
420   ADD_SEQ_CST (SYNC_FETCH_AND_AND_4, TSAN_ATOMIC32_FETCH_AND),
421   ADD_SEQ_CST (SYNC_FETCH_AND_AND_8, TSAN_ATOMIC64_FETCH_AND),
422   ADD_SEQ_CST (SYNC_FETCH_AND_AND_16, TSAN_ATOMIC128_FETCH_AND),
423   ADD_SEQ_CST (SYNC_FETCH_AND_OR_1, TSAN_ATOMIC8_FETCH_OR),
424   ADD_SEQ_CST (SYNC_FETCH_AND_OR_2, TSAN_ATOMIC16_FETCH_OR),
425   ADD_SEQ_CST (SYNC_FETCH_AND_OR_4, TSAN_ATOMIC32_FETCH_OR),
426   ADD_SEQ_CST (SYNC_FETCH_AND_OR_8, TSAN_ATOMIC64_FETCH_OR),
427   ADD_SEQ_CST (SYNC_FETCH_AND_OR_16, TSAN_ATOMIC128_FETCH_OR),
428   ADD_SEQ_CST (SYNC_FETCH_AND_XOR_1, TSAN_ATOMIC8_FETCH_XOR),
429   ADD_SEQ_CST (SYNC_FETCH_AND_XOR_2, TSAN_ATOMIC16_FETCH_XOR),
430   ADD_SEQ_CST (SYNC_FETCH_AND_XOR_4, TSAN_ATOMIC32_FETCH_XOR),
431   ADD_SEQ_CST (SYNC_FETCH_AND_XOR_8, TSAN_ATOMIC64_FETCH_XOR),
432   ADD_SEQ_CST (SYNC_FETCH_AND_XOR_16, TSAN_ATOMIC128_FETCH_XOR),
433   ADD_SEQ_CST (SYNC_FETCH_AND_NAND_1, TSAN_ATOMIC8_FETCH_NAND),
434   ADD_SEQ_CST (SYNC_FETCH_AND_NAND_2, TSAN_ATOMIC16_FETCH_NAND),
435   ADD_SEQ_CST (SYNC_FETCH_AND_NAND_4, TSAN_ATOMIC32_FETCH_NAND),
436   ADD_SEQ_CST (SYNC_FETCH_AND_NAND_8, TSAN_ATOMIC64_FETCH_NAND),
437   ADD_SEQ_CST (SYNC_FETCH_AND_NAND_16, TSAN_ATOMIC128_FETCH_NAND),
438
439   ADD_SEQ_CST (SYNC_SYNCHRONIZE, TSAN_ATOMIC_THREAD_FENCE),
440
441   FETCH_OPS (SYNC_ADD_AND_FETCH_1, TSAN_ATOMIC8_FETCH_ADD, PLUS_EXPR),
442   FETCH_OPS (SYNC_ADD_AND_FETCH_2, TSAN_ATOMIC16_FETCH_ADD, PLUS_EXPR),
443   FETCH_OPS (SYNC_ADD_AND_FETCH_4, TSAN_ATOMIC32_FETCH_ADD, PLUS_EXPR),
444   FETCH_OPS (SYNC_ADD_AND_FETCH_8, TSAN_ATOMIC64_FETCH_ADD, PLUS_EXPR),
445   FETCH_OPS (SYNC_ADD_AND_FETCH_16, TSAN_ATOMIC128_FETCH_ADD, PLUS_EXPR),
446   FETCH_OPS (SYNC_SUB_AND_FETCH_1, TSAN_ATOMIC8_FETCH_SUB, MINUS_EXPR),
447   FETCH_OPS (SYNC_SUB_AND_FETCH_2, TSAN_ATOMIC16_FETCH_SUB, MINUS_EXPR),
448   FETCH_OPS (SYNC_SUB_AND_FETCH_4, TSAN_ATOMIC32_FETCH_SUB, MINUS_EXPR),
449   FETCH_OPS (SYNC_SUB_AND_FETCH_8, TSAN_ATOMIC64_FETCH_SUB, MINUS_EXPR),
450   FETCH_OPS (SYNC_SUB_AND_FETCH_16, TSAN_ATOMIC128_FETCH_SUB, MINUS_EXPR),
451   FETCH_OPS (SYNC_AND_AND_FETCH_1, TSAN_ATOMIC8_FETCH_AND, BIT_AND_EXPR),
452   FETCH_OPS (SYNC_AND_AND_FETCH_2, TSAN_ATOMIC16_FETCH_AND, BIT_AND_EXPR),
453   FETCH_OPS (SYNC_AND_AND_FETCH_4, TSAN_ATOMIC32_FETCH_AND, BIT_AND_EXPR),
454   FETCH_OPS (SYNC_AND_AND_FETCH_8, TSAN_ATOMIC64_FETCH_AND, BIT_AND_EXPR),
455   FETCH_OPS (SYNC_AND_AND_FETCH_16, TSAN_ATOMIC128_FETCH_AND, BIT_AND_EXPR),
456   FETCH_OPS (SYNC_OR_AND_FETCH_1, TSAN_ATOMIC8_FETCH_OR, BIT_IOR_EXPR),
457   FETCH_OPS (SYNC_OR_AND_FETCH_2, TSAN_ATOMIC16_FETCH_OR, BIT_IOR_EXPR),
458   FETCH_OPS (SYNC_OR_AND_FETCH_4, TSAN_ATOMIC32_FETCH_OR, BIT_IOR_EXPR),
459   FETCH_OPS (SYNC_OR_AND_FETCH_8, TSAN_ATOMIC64_FETCH_OR, BIT_IOR_EXPR),
460   FETCH_OPS (SYNC_OR_AND_FETCH_16, TSAN_ATOMIC128_FETCH_OR, BIT_IOR_EXPR),
461   FETCH_OPS (SYNC_XOR_AND_FETCH_1, TSAN_ATOMIC8_FETCH_XOR, BIT_XOR_EXPR),
462   FETCH_OPS (SYNC_XOR_AND_FETCH_2, TSAN_ATOMIC16_FETCH_XOR, BIT_XOR_EXPR),
463   FETCH_OPS (SYNC_XOR_AND_FETCH_4, TSAN_ATOMIC32_FETCH_XOR, BIT_XOR_EXPR),
464   FETCH_OPS (SYNC_XOR_AND_FETCH_8, TSAN_ATOMIC64_FETCH_XOR, BIT_XOR_EXPR),
465   FETCH_OPS (SYNC_XOR_AND_FETCH_16, TSAN_ATOMIC128_FETCH_XOR, BIT_XOR_EXPR),
466   FETCH_OPS (SYNC_NAND_AND_FETCH_1, TSAN_ATOMIC8_FETCH_NAND, BIT_NOT_EXPR),
467   FETCH_OPS (SYNC_NAND_AND_FETCH_2, TSAN_ATOMIC16_FETCH_NAND, BIT_NOT_EXPR),
468   FETCH_OPS (SYNC_NAND_AND_FETCH_4, TSAN_ATOMIC32_FETCH_NAND, BIT_NOT_EXPR),
469   FETCH_OPS (SYNC_NAND_AND_FETCH_8, TSAN_ATOMIC64_FETCH_NAND, BIT_NOT_EXPR),
470   FETCH_OPS (SYNC_NAND_AND_FETCH_16, TSAN_ATOMIC128_FETCH_NAND, BIT_NOT_EXPR),
471
472   WEAK_CAS (ATOMIC_COMPARE_EXCHANGE_1, TSAN_ATOMIC8_COMPARE_EXCHANGE_WEAK),
473   WEAK_CAS (ATOMIC_COMPARE_EXCHANGE_2, TSAN_ATOMIC16_COMPARE_EXCHANGE_WEAK),
474   WEAK_CAS (ATOMIC_COMPARE_EXCHANGE_4, TSAN_ATOMIC32_COMPARE_EXCHANGE_WEAK),
475   WEAK_CAS (ATOMIC_COMPARE_EXCHANGE_8, TSAN_ATOMIC64_COMPARE_EXCHANGE_WEAK),
476   WEAK_CAS (ATOMIC_COMPARE_EXCHANGE_16, TSAN_ATOMIC128_COMPARE_EXCHANGE_WEAK),
477
478   STRONG_CAS (ATOMIC_COMPARE_EXCHANGE_1, TSAN_ATOMIC8_COMPARE_EXCHANGE_STRONG),
479   STRONG_CAS (ATOMIC_COMPARE_EXCHANGE_2,
480               TSAN_ATOMIC16_COMPARE_EXCHANGE_STRONG),
481   STRONG_CAS (ATOMIC_COMPARE_EXCHANGE_4,
482               TSAN_ATOMIC32_COMPARE_EXCHANGE_STRONG),
483   STRONG_CAS (ATOMIC_COMPARE_EXCHANGE_8,
484               TSAN_ATOMIC64_COMPARE_EXCHANGE_STRONG),
485   STRONG_CAS (ATOMIC_COMPARE_EXCHANGE_16,
486               TSAN_ATOMIC128_COMPARE_EXCHANGE_STRONG),
487
488   BOOL_CAS (SYNC_BOOL_COMPARE_AND_SWAP_1,
489             TSAN_ATOMIC8_COMPARE_EXCHANGE_STRONG),
490   BOOL_CAS (SYNC_BOOL_COMPARE_AND_SWAP_2,
491             TSAN_ATOMIC16_COMPARE_EXCHANGE_STRONG),
492   BOOL_CAS (SYNC_BOOL_COMPARE_AND_SWAP_4,
493             TSAN_ATOMIC32_COMPARE_EXCHANGE_STRONG),
494   BOOL_CAS (SYNC_BOOL_COMPARE_AND_SWAP_8,
495             TSAN_ATOMIC64_COMPARE_EXCHANGE_STRONG),
496   BOOL_CAS (SYNC_BOOL_COMPARE_AND_SWAP_16,
497             TSAN_ATOMIC128_COMPARE_EXCHANGE_STRONG),
498
499   VAL_CAS (SYNC_VAL_COMPARE_AND_SWAP_1, TSAN_ATOMIC8_COMPARE_EXCHANGE_STRONG),
500   VAL_CAS (SYNC_VAL_COMPARE_AND_SWAP_2, TSAN_ATOMIC16_COMPARE_EXCHANGE_STRONG),
501   VAL_CAS (SYNC_VAL_COMPARE_AND_SWAP_4, TSAN_ATOMIC32_COMPARE_EXCHANGE_STRONG),
502   VAL_CAS (SYNC_VAL_COMPARE_AND_SWAP_8, TSAN_ATOMIC64_COMPARE_EXCHANGE_STRONG),
503   VAL_CAS (SYNC_VAL_COMPARE_AND_SWAP_16,
504            TSAN_ATOMIC128_COMPARE_EXCHANGE_STRONG),
505
506   LOCK_RELEASE (SYNC_LOCK_RELEASE_1, TSAN_ATOMIC8_STORE),
507   LOCK_RELEASE (SYNC_LOCK_RELEASE_2, TSAN_ATOMIC16_STORE),
508   LOCK_RELEASE (SYNC_LOCK_RELEASE_4, TSAN_ATOMIC32_STORE),
509   LOCK_RELEASE (SYNC_LOCK_RELEASE_8, TSAN_ATOMIC64_STORE),
510   LOCK_RELEASE (SYNC_LOCK_RELEASE_16, TSAN_ATOMIC128_STORE)
511 };
512
513 /* Instrument an atomic builtin.  */
514
515 static void
516 instrument_builtin_call (gimple_stmt_iterator *gsi)
517 {
518   gimple stmt = gsi_stmt (*gsi), g;
519   tree callee = gimple_call_fndecl (stmt), last_arg, args[6], t, lhs;
520   enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
521   unsigned int i, num = gimple_call_num_args (stmt), j;
522   for (j = 0; j < 6 && j < num; j++)
523     args[j] = gimple_call_arg (stmt, j);
524   for (i = 0; i < ARRAY_SIZE (tsan_atomic_table); i++)
525     if (fcode != tsan_atomic_table[i].fcode)
526       continue;
527     else
528       {
529         tree decl = builtin_decl_implicit (tsan_atomic_table[i].tsan_fcode);
530         if (decl == NULL_TREE)
531           return;
532         switch (tsan_atomic_table[i].action)
533           {
534           case check_last:
535           case fetch_op:
536             last_arg = gimple_call_arg (stmt, num - 1);
537             if (!tree_fits_uhwi_p (last_arg)
538                 || tree_to_uhwi (last_arg) > MEMMODEL_SEQ_CST)
539               return;
540             gimple_call_set_fndecl (stmt, decl);
541             update_stmt (stmt);
542             if (tsan_atomic_table[i].action == fetch_op)
543               {
544                 args[1] = gimple_call_arg (stmt, 1);
545                 goto adjust_result;
546               }
547             return;
548           case add_seq_cst:
549           case add_acquire:
550           case fetch_op_seq_cst:
551             gcc_assert (num <= 2);
552             for (j = 0; j < num; j++)
553               args[j] = gimple_call_arg (stmt, j);
554             for (; j < 2; j++)
555               args[j] = NULL_TREE;
556             args[num] = build_int_cst (NULL_TREE,
557                                        tsan_atomic_table[i].action
558                                        != add_acquire
559                                        ? MEMMODEL_SEQ_CST
560                                        : MEMMODEL_ACQUIRE);
561             update_gimple_call (gsi, decl, num + 1, args[0], args[1], args[2]);
562             stmt = gsi_stmt (*gsi);
563             if (tsan_atomic_table[i].action == fetch_op_seq_cst)
564               {
565               adjust_result:
566                 lhs = gimple_call_lhs (stmt);
567                 if (lhs == NULL_TREE)
568                   return;
569                 if (!useless_type_conversion_p (TREE_TYPE (lhs),
570                                                 TREE_TYPE (args[1])))
571                   {
572                     tree var = make_ssa_name (TREE_TYPE (lhs));
573                     g = gimple_build_assign (var, NOP_EXPR, args[1]);
574                     gsi_insert_after (gsi, g, GSI_NEW_STMT);
575                     args[1] = var;
576                   }
577                 gimple_call_set_lhs (stmt, make_ssa_name (TREE_TYPE (lhs)));
578                 /* BIT_NOT_EXPR stands for NAND.  */
579                 if (tsan_atomic_table[i].code == BIT_NOT_EXPR)
580                   {
581                     tree var = make_ssa_name (TREE_TYPE (lhs));
582                     g = gimple_build_assign (var, BIT_AND_EXPR,
583                                              gimple_call_lhs (stmt), args[1]);
584                     gsi_insert_after (gsi, g, GSI_NEW_STMT);
585                     g = gimple_build_assign (lhs, BIT_NOT_EXPR, var);
586                   }
587                 else
588                   g = gimple_build_assign (lhs, tsan_atomic_table[i].code,
589                                            gimple_call_lhs (stmt), args[1]);
590                 update_stmt (stmt);
591                 gsi_insert_after (gsi, g, GSI_NEW_STMT);
592               }
593             return;
594           case weak_cas:
595             if (!integer_nonzerop (gimple_call_arg (stmt, 3)))
596               continue;
597             /* FALLTHRU */
598           case strong_cas:
599             gcc_assert (num == 6);
600             for (j = 0; j < 6; j++)
601               args[j] = gimple_call_arg (stmt, j);
602             if (!tree_fits_uhwi_p (args[4])
603                 || tree_to_uhwi (args[4]) > MEMMODEL_SEQ_CST)
604               return;
605             if (!tree_fits_uhwi_p (args[5])
606                 || tree_to_uhwi (args[5]) > MEMMODEL_SEQ_CST)
607               return;
608             update_gimple_call (gsi, decl, 5, args[0], args[1], args[2],
609                                 args[4], args[5]);
610             return;
611           case bool_cas:
612           case val_cas:
613             gcc_assert (num == 3);
614             for (j = 0; j < 3; j++)
615               args[j] = gimple_call_arg (stmt, j);
616             t = TYPE_ARG_TYPES (TREE_TYPE (decl));
617             t = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (t)));
618             t = create_tmp_var (t);
619             mark_addressable (t);
620             if (!useless_type_conversion_p (TREE_TYPE (t),
621                                             TREE_TYPE (args[1])))
622               {
623                 g = gimple_build_assign (make_ssa_name (TREE_TYPE (t)),
624                                          NOP_EXPR, args[1]);
625                 gsi_insert_before (gsi, g, GSI_SAME_STMT);
626                 args[1] = gimple_assign_lhs (g);
627               }
628             g = gimple_build_assign (t, args[1]);
629             gsi_insert_before (gsi, g, GSI_SAME_STMT);
630             lhs = gimple_call_lhs (stmt);
631             update_gimple_call (gsi, decl, 5, args[0],
632                                 build_fold_addr_expr (t), args[2],
633                                 build_int_cst (NULL_TREE,
634                                                MEMMODEL_SEQ_CST),
635                                 build_int_cst (NULL_TREE,
636                                                MEMMODEL_SEQ_CST));
637             if (tsan_atomic_table[i].action == val_cas && lhs)
638               {
639                 tree cond;
640                 stmt = gsi_stmt (*gsi);
641                 g = gimple_build_assign (make_ssa_name (TREE_TYPE (t)), t);
642                 gsi_insert_after (gsi, g, GSI_NEW_STMT);
643                 t = make_ssa_name (TREE_TYPE (TREE_TYPE (decl)), stmt);
644                 cond = build2 (NE_EXPR, boolean_type_node, t,
645                                build_int_cst (TREE_TYPE (t), 0));
646                 g = gimple_build_assign (lhs, COND_EXPR, cond, args[1],
647                                          gimple_assign_lhs (g));
648                 gimple_call_set_lhs (stmt, t);
649                 update_stmt (stmt);
650                 gsi_insert_after (gsi, g, GSI_NEW_STMT);
651               }
652             return;
653           case lock_release:
654             gcc_assert (num == 1);
655             t = TYPE_ARG_TYPES (TREE_TYPE (decl));
656             t = TREE_VALUE (TREE_CHAIN (t));
657             update_gimple_call (gsi, decl, 3, gimple_call_arg (stmt, 0),
658                                 build_int_cst (t, 0),
659                                 build_int_cst (NULL_TREE,
660                                                MEMMODEL_RELEASE));
661             return;
662           default:
663             continue;
664           }
665       }
666 }
667
668 /* Instruments the gimple pointed to by GSI. Return
669    true if func entry/exit should be instrumented.  */
670
671 static bool
672 instrument_gimple (gimple_stmt_iterator *gsi)
673 {
674   gimple stmt;
675   tree rhs, lhs;
676   bool instrumented = false;
677
678   stmt = gsi_stmt (*gsi);
679   if (is_gimple_call (stmt)
680       && (gimple_call_fndecl (stmt)
681           != builtin_decl_implicit (BUILT_IN_TSAN_INIT)))
682     {
683       if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
684         instrument_builtin_call (gsi);
685       return true;
686     }
687   else if (is_gimple_assign (stmt)
688            && !gimple_clobber_p (stmt))
689     {
690       if (gimple_store_p (stmt))
691         {
692           lhs = gimple_assign_lhs (stmt);
693           instrumented = instrument_expr (*gsi, lhs, true);
694         }
695       if (gimple_assign_load_p (stmt))
696         {
697           rhs = gimple_assign_rhs1 (stmt);
698           instrumented = instrument_expr (*gsi, rhs, false);
699         }
700     }
701   return instrumented;
702 }
703
704 /* Replace TSAN_FUNC_EXIT internal call with function exit tsan builtin.  */
705
706 static void
707 replace_func_exit (gimple stmt)
708 {
709   tree builtin_decl = builtin_decl_implicit (BUILT_IN_TSAN_FUNC_EXIT);
710   gimple g = gimple_build_call (builtin_decl, 0);
711   gimple_set_location (g, cfun->function_end_locus);
712   gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
713   gsi_replace (&gsi, g, true);
714 }
715
716 /* Instrument function exit.  Used when TSAN_FUNC_EXIT does not exist.  */
717
718 static void
719 instrument_func_exit (void)
720 {
721   location_t loc;
722   basic_block exit_bb;
723   gimple_stmt_iterator gsi;
724   gimple stmt, g;
725   tree builtin_decl;
726   edge e;
727   edge_iterator ei;
728
729   /* Find all function exits.  */
730   exit_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
731   FOR_EACH_EDGE (e, ei, exit_bb->preds)
732     {
733       gsi = gsi_last_bb (e->src);
734       stmt = gsi_stmt (gsi);
735       gcc_assert (gimple_code (stmt) == GIMPLE_RETURN
736                   || gimple_call_builtin_p (stmt, BUILT_IN_RETURN));
737       loc = gimple_location (stmt);
738       builtin_decl = builtin_decl_implicit (BUILT_IN_TSAN_FUNC_EXIT);
739       g = gimple_build_call (builtin_decl, 0);
740       gimple_set_location (g, loc);
741       gsi_insert_before (&gsi, g, GSI_SAME_STMT);
742     }
743 }
744
745 /* Instruments all interesting memory accesses in the current function.
746    Return true if func entry/exit should be instrumented.  */
747
748 static bool
749 instrument_memory_accesses (void)
750 {
751   basic_block bb;
752   gimple_stmt_iterator gsi;
753   bool fentry_exit_instrument = false;
754   bool func_exit_seen = false;
755   auto_vec<gimple> tsan_func_exits;
756
757   FOR_EACH_BB_FN (bb, cfun)
758     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
759       {
760         gimple stmt = gsi_stmt (gsi);
761         if (is_gimple_call (stmt)
762             && gimple_call_internal_p (stmt)
763             && gimple_call_internal_fn (stmt) == IFN_TSAN_FUNC_EXIT)
764           {
765             if (fentry_exit_instrument)
766               replace_func_exit (stmt);
767             else
768               tsan_func_exits.safe_push (stmt);
769             func_exit_seen = true;
770           }
771         else
772           fentry_exit_instrument |= instrument_gimple (&gsi);
773       }
774   unsigned int i;
775   gimple stmt;
776   FOR_EACH_VEC_ELT (tsan_func_exits, i, stmt)
777     if (fentry_exit_instrument)
778       replace_func_exit (stmt);
779     else
780       {
781         gsi = gsi_for_stmt (stmt);
782         gsi_remove (&gsi, true);
783       }
784   if (fentry_exit_instrument && !func_exit_seen)
785     instrument_func_exit ();
786   return fentry_exit_instrument;
787 }
788
789 /* Instruments function entry.  */
790
791 static void
792 instrument_func_entry (void)
793 {
794   tree ret_addr, builtin_decl;
795   gimple g;
796   gimple_seq seq = NULL;
797
798   builtin_decl = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
799   g = gimple_build_call (builtin_decl, 1, integer_zero_node);
800   ret_addr = make_ssa_name (ptr_type_node);
801   gimple_call_set_lhs (g, ret_addr);
802   gimple_set_location (g, cfun->function_start_locus);
803   gimple_seq_add_stmt_without_update (&seq, g);
804
805   builtin_decl = builtin_decl_implicit (BUILT_IN_TSAN_FUNC_ENTRY);
806   g = gimple_build_call (builtin_decl, 1, ret_addr);
807   gimple_set_location (g, cfun->function_start_locus);
808   gimple_seq_add_stmt_without_update (&seq, g);
809
810   edge e = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
811   gsi_insert_seq_on_edge_immediate (e, seq);
812 }
813
814 /* ThreadSanitizer instrumentation pass.  */
815
816 static unsigned
817 tsan_pass (void)
818 {
819   initialize_sanitizer_builtins ();
820   if (instrument_memory_accesses ())
821     instrument_func_entry ();
822   return 0;
823 }
824
825 /* Inserts __tsan_init () into the list of CTORs.  */
826
827 void
828 tsan_finish_file (void)
829 {
830   tree ctor_statements = NULL_TREE;
831
832   initialize_sanitizer_builtins ();
833   tree init_decl = builtin_decl_implicit (BUILT_IN_TSAN_INIT);
834   append_to_statement_list (build_call_expr (init_decl, 0),
835                             &ctor_statements);
836   cgraph_build_static_cdtor ('I', ctor_statements,
837                              MAX_RESERVED_INIT_PRIORITY - 1);
838 }
839
840 /* The pass descriptor.  */
841
842 namespace {
843
844 const pass_data pass_data_tsan =
845 {
846   GIMPLE_PASS, /* type */
847   "tsan", /* name */
848   OPTGROUP_NONE, /* optinfo_flags */
849   TV_NONE, /* tv_id */
850   ( PROP_ssa | PROP_cfg ), /* properties_required */
851   0, /* properties_provided */
852   0, /* properties_destroyed */
853   0, /* todo_flags_start */
854   TODO_update_ssa, /* todo_flags_finish */
855 };
856
857 class pass_tsan : public gimple_opt_pass
858 {
859 public:
860   pass_tsan (gcc::context *ctxt)
861     : gimple_opt_pass (pass_data_tsan, ctxt)
862   {}
863
864   /* opt_pass methods: */
865   opt_pass * clone () { return new pass_tsan (m_ctxt); }
866   virtual bool gate (function *)
867 {
868   return ((flag_sanitize & SANITIZE_THREAD) != 0
869           && !lookup_attribute ("no_sanitize_thread",
870                                 DECL_ATTRIBUTES (current_function_decl)));
871 }
872
873   virtual unsigned int execute (function *) { return tsan_pass (); }
874
875 }; // class pass_tsan
876
877 } // anon namespace
878
879 gimple_opt_pass *
880 make_pass_tsan (gcc::context *ctxt)
881 {
882   return new pass_tsan (ctxt);
883 }
884
885 namespace {
886
887 const pass_data pass_data_tsan_O0 =
888 {
889   GIMPLE_PASS, /* type */
890   "tsan0", /* name */
891   OPTGROUP_NONE, /* optinfo_flags */
892   TV_NONE, /* tv_id */
893   ( PROP_ssa | PROP_cfg ), /* properties_required */
894   0, /* properties_provided */
895   0, /* properties_destroyed */
896   0, /* todo_flags_start */
897   TODO_update_ssa, /* todo_flags_finish */
898 };
899
900 class pass_tsan_O0 : public gimple_opt_pass
901 {
902 public:
903   pass_tsan_O0 (gcc::context *ctxt)
904     : gimple_opt_pass (pass_data_tsan_O0, ctxt)
905   {}
906
907   /* opt_pass methods: */
908   virtual bool gate (function *)
909     {
910       return ((flag_sanitize & SANITIZE_THREAD) != 0 && !optimize
911               && !lookup_attribute ("no_sanitize_thread",
912                                     DECL_ATTRIBUTES (current_function_decl)));
913     }
914
915   virtual unsigned int execute (function *) { return tsan_pass (); }
916
917 }; // class pass_tsan_O0
918
919 } // anon namespace
920
921 gimple_opt_pass *
922 make_pass_tsan_O0 (gcc::context *ctxt)
923 {
924   return new pass_tsan_O0 (ctxt);
925 }