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