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