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