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