8sa1-gcc/gcc/tsan.c
David Malcolm 11cd3bed28 Eliminate FOR_EACH_BB macro.
gcc/
	* basic-block.h (FOR_EACH_BB): Eliminate macro.

	* asan.c (transform_statements, execute_sanopt): Eliminate
	use of FOR_EACH_BB in favor of FOR_EACH_BB_FN, to make use of cfun
	explicit.
	* auto-inc-dec.c (rest_of_handle_auto_inc_dec): Likewise.
	* bb-reorder.c (find_rarely_executed_basic_blocks_and_crossing_edges,
	set_edge_can_fallthru_flag, fix_up_fall_thru_edges,
	fix_crossing_unconditional_branches, add_reg_crossing_jump_notes,
	insert_section_boundary_note, rest_of_handle_reorder_blocks,
	duplicate_computed_gotos): Likewise.
	* cfg.c (clear_edges, compact_blocks, brief_dump_cfg): Likewise.
	* cfganal.c (find_unreachable_blocks, add_noreturn_fake_exit_edges,
	compute_dominance_frontiers_1, single_pred_before_succ_order): Likewise.
	* cfgbuild.c (find_many_sub_basic_blocks): Likewise.
	* cfgcleanup.c (try_optimize_cfg, delete_dead_jumptables): Likewise.
	* cfgexpand.c (add_scope_conflicts, discover_nonconstant_array_refs):
	Likewise.
	* cfgloop.c (flow_loops_cfg_dump, get_loop_body, record_loop_exits,
	verify_loop_structure): Likewise.
	* cfgloopanal.c (mark_loop_exit_edges): Likewise.
	* cfgrtl.c (compute_bb_for_insn, find_partition_fixes,
	verify_hot_cold_block_grouping, purge_all_dead_edges,
	fixup_abnormal_edges, record_effective_endpoints,
	outof_cfg_layout_mode, fixup_reorder_chain, force_one_exit_fallthru,
	break_superblocks): Likewise.
	* cgraphbuild.c (build_cgraph_edges, rebuild_cgraph_edges,
	cgraph_rebuild_references): Likewise.
	* combine-stack-adj.c (combine_stack_adjustments): Likewise.
	* combine.c (delete_noop_moves, create_log_links,
	combine_instructions): Likewise.
	* config/arm/arm.c (thumb1_reorg, thumb2_reorg): Likewise.
	* config/bfin/bfin.c (bfin_gen_bundles, reorder_var_tracking_notes):
	Likewise.
	* config/c6x/c6x.c (c6x_gen_bundles, conditionalize_after_sched,
	c6x_reorg): Likewise.
	* config/epiphany/resolve-sw-modes.c (resolve_sw_modes): Likewise.
	* config/frv/frv.c (frv_optimize_membar): Likewise.
	* config/i386/i386.c (ix86_finalize_stack_realign_flags): Likewise.
	* config/ia64/ia64.c (ia64_reorg): Likewise.
	* config/mips/mips.c (mips_annotate_pic_calls): Likewise.
	* config/picochip/picochip.c (reorder_var_tracking_notes): Likewise.
	* config/rs6000/rs6000.c (rs6000_alloc_sdmode_stack_slot): Likewise.
	* config/s390/s390.c (s390_regs_ever_clobbered): Likewise.
	* config/sh/sh_treg_combine.cc (sh_treg_combine::execute): Likewise.
	* config/spu/spu.c (spu_machine_dependent_reorg): Likewise.
	* config/tilegx/tilegx.c (tilegx_gen_bundles,
	reorder_var_tracking_notes): Likewise.
	* config/tilepro/tilepro.c (tilepro_gen_bundles,
	reorder_var_tracking_notes): Likewise.
	* coverage.c (coverage_compute_cfg_checksum): Likewise.
	* cprop.c (compute_hash_table_work, compute_cprop_data,
	local_cprop_pass, find_implicit_sets): Likewise.
	* cse.c (cse_condition_code_reg): Likewise.
	* dce.c (prescan_insns_for_dce): Likewise.
	* df-core.c (df_compact_blocks): Likewise.
	* df-problems.c (df_word_lr_alloc): Likewise.
	* df-scan.c (df_scan_start_dump, df_scan_blocks, df_insn_rescan_all,
	df_update_entry_exit_and_calls): Likewise.
	* dominance.c (calculate_dominance_info, verify_dominators,
	debug_dominance_info): Likewise.
	* dse.c (dse_step5_nospill): Likewise.
	* except.c (finish_eh_generation): Likewise.
	* final.c (compute_alignments): Likewise.
	* function.c (thread_prologue_and_epilogue_insns,
	rest_of_match_asm_constraints): Likewise.
	* gcse.c (compute_hash_table_work, prune_expressions,
	compute_pre_data, compute_code_hoist_vbeinout, hoist_code,
	calculate_bb_reg_pressure, compute_ld_motion_mems): Likewise.
	* gimple-iterator.c (gsi_commit_edge_inserts): Likewise.
	* gimple-ssa-isolate-paths.c (find_implicit_erroneous_behaviour,
	find_explicit_erroneous_behaviour): Likewise.
	* graphite-sese-to-poly.c (rewrite_reductions_out_of_ssa,
	rewrite_cross_bb_scalar_deps_out_of_ssa): Likewise.
	* haifa-sched.c (haifa_sched_init): Likewise.
	* hw-doloop.c (discover_loops, set_bb_indices, reorder_loops):
	Likewise.
	* ifcvt.c (if_convert): Likewise.
	* init-regs.c (initialize_uninitialized_regs): Likewise.
	* ipa-prop.c (ipcp_transform_function): Likewise.
	* ipa-pure-const.c (analyze_function): Likewise.
	* ipa-split.c (find_split_points, execute_split_functions): Likewise.
	* ira-build.c (form_loop_tree): Likewise.
	* ira-costs.c (find_costs_and_classes): Likewise.
	* ira-emit.c (emit_moves, add_ranges_and_copies, ira_emit): Likewise.
	* ira.c (decrease_live_ranges_number, compute_regs_asm_clobbered,
	mark_elimination, update_equiv_regs, find_moveable_pseudos,
	split_live_ranges_for_shrink_wrap, allocate_initial_values): Likewise.
	* jump.c (mark_all_labels): Likewise.
	* lcm.c (compute_laterin, compute_insert_delete, compute_available,
	compute_nearerout, compute_rev_insert_delete): Likewise.
	* loop-init.c (fix_loop_structure): Likewise.
	* loop-invariant.c (calculate_loop_reg_pressure): Likewise.
	* lower-subreg.c (decompose_multiword_subregs,
	decompose_multiword_subregs): Likewise.
	* lra-assigns.c (assign_by_spills): Likewise.
	* lra-coalesce.c (lra_coalesce): Likewise.
	* lra-constraints.c (lra_inheritance, remove_inheritance_pseudos):
	Likewise.
	* lra-eliminations.c (lra_init_elimination): Likewise.
	* lra-spills.c (assign_spill_hard_regs, spill_pseudos,
	lra_final_code_change): Likewise.
	* lra.c (remove_scratches, check_rtl, has_nonexceptional_receiver,
	update_inc_notes): Likewise.
	* mcf.c (adjust_cfg_counts): Likewise.
	* mode-switching.c (optimize_mode_switching): Likewise.
	* modulo-sched.c (rest_of_handle_sms): Likewise.
	* omp-low.c (optimize_omp_library_calls, expand_omp_taskreg,
	expand_omp_target): Likewise.
	* postreload-gcse.c (alloc_mem, compute_hash_table): Likewise.
	* postreload.c (reload_cse_regs_1): Likewise.
	* predict.c (strip_predict_hints, tree_bb_level_predictions,
	tree_estimate_probability, expensive_function_p,
	estimate_bb_frequencies, compute_function_frequency): Likewise.
	* profile.c (is_inconsistent, compute_branch_probabilities,
	branch_prob): Likewise.
	* ree.c (find_removable_extensions): Likewise.
	* reg-stack.c (compensate_edges, convert_regs, reg_to_stack): Likewise.
	* regcprop.c (copyprop_hardreg_forward): Likewise.
	* reginfo.c (init_subregs_of_mode): Likewise.
	* regrename.c (regrename_analyze): Likewise.
	* regstat.c (regstat_compute_ri, regstat_compute_calls_crossed):
	Likewise.
	* reload1.c (has_nonexceptional_receiver, reload,
	calculate_elim_costs_all_insns): Likewise.
	* resource.c (init_resource_info, free_resource_info): Likewise.
	* sched-ebb.c (schedule_ebbs): Likewise.
	* sched-rgn.c (is_cfg_nonregular, find_single_block_region,
	haifa_find_rgns, sched_rgn_local_init): Likewise.
	* sel-sched-dump.c (sel_dump_cfg_2): Likewise.
	* sel-sched-ir.c (init_lv_sets, free_lv_sets,
	make_regions_from_the_rest): Likewise.
	* sese.c (build_sese_loop_nests, sese_build_liveouts): Likewise.
	* stack-ptr-mod.c (notice_stack_pointer_modification): Likewise.
	* store-motion.c (compute_store_table, build_store_vectors,
	one_store_motion_pass): Likewise.
	* tracer.c (tail_duplicate): Likewise.
	* trans-mem.c (compute_transaction_bits): Likewise.
	* tree-call-cdce.c (tree_call_cdce): Likewise.
	* tree-cfg.c (replace_loop_annotate, factor_computed_gotos,
	fold_cond_expr_cond, make_edges, assign_discriminators,
	make_abnormal_goto_edges, cleanup_dead_labels, group_case_labels,
	dump_cfg_stats, gimple_verify_flow_info, print_loop,
	execute_fixup_cfg): Likewise.
	* tree-cfgcleanup.c (cleanup_tree_cfg_1, merge_phi_nodes): Likewise.
	* tree-complex.c (init_dont_simulate_again, tree_lower_complex):
	Likewise.
	* tree-dfa.c (collect_dfa_stats, dump_enumerated_decls): Likewise.
	* tree-eh.c (execute_lower_resx, execute_lower_eh_dispatch,
	mark_reachable_handlers): Likewise.
	* tree-emutls.c (lower_emutls_function_body): Likewise.
	* tree-if-conv.c (main_tree_if_conversion): Likewise.
	* tree-inline.c (optimize_inline_calls): Likewise.
	* tree-into-ssa.c (rewrite_into_ssa, update_ssa): Likewise.
	* tree-nrv.c (tree_nrv, execute_return_slot_opt): Likewise.
	* tree-object-size.c (compute_object_sizes): Likewise.
	* tree-outof-ssa.c (eliminate_useless_phis, rewrite_trees,
	insert_backedge_copies, tree_profiling): Likewise.
	* tree-scalar-evolution.c (scev_const_prop): Likewise.
	* tree-sra.c (scan_function, sra_modify_function_body,
	propagate_dereference_distances, ipa_sra_modify_function_body,
	convert_callers): Likewise.
	* tree-ssa-ccp.c (ccp_initialize, execute_fold_all_builtins): Likewise.
	* tree-ssa-coalesce.c (build_ssa_conflict_graph): Likewise.
	create_outofssa_var_map, coalesce_partitions): Likewise.
	* tree-ssa-copy.c (init_copy_prop): Likewise.
	* tree-ssa-copyrename.c (rename_ssa_copies): Likewise.
	* tree-ssa-dce.c (find_obviously_necessary_stmts,
	eliminate_unnecessary_stmts): Likewise.
	* tree-ssa-dom.c (free_all_edge_infos, tree_ssa_dominator_optimize):
	Likewise.
	* tree-ssa-forwprop.c (ssa_forward_propagate_and_combine): Likewise.
	* tree-ssa-live.c (clear_unused_block_pointer, remove_unused_locals,
	new_tree_live_info, calculate_live_on_exit, dump_live_info,
	analyze_memory_references, fill_always_executed_in,
	tree_ssa_lim_finalize): Likewise.
	* tree-ssa-loop-manip.c (find_uses_to_rename, verify_loop_closed_ssa):
	Likewise.
	* tree-ssa-math-opts.c (execute_cse_reciprocals, execute_cse_sincos,
	execute_optimize_bswap, execute_optimize_widening_mul): Likewise.
	* tree-ssa-propagate.c (substitute_and_fold): Likewise.
	* tree-ssa-structalias.c (compute_points_to_sets): Likewise.
	* tree-ssa-tail-merge.c (find_same_succ, reset_cluster_vectors):
	Likewise.
	* tree-ssa-ter.c (find_replaceable_exprs): Likewise.
	* tree-ssa-threadupdate.c (thread_through_all_blocks): Likewise.
	* tree-ssa-uncprop.c (associate_equivalences_with_edges,
	tree_ssa_uncprop): Likewise.
	* tree-ssa-uninit.c (warn_uninitialized_vars,
	execute_late_warn_uninitialized): Likewise.
	* tree-ssa.c (verify_ssa, execute_update_addresses_taken): Likewise.
	* tree-stdarg.c (check_all_va_list_escapes, execute_optimize_stdarg):
	Likewise.
	* tree-switch-conversion.c (do_switchconv): Likewise.
	* tree-vect-generic.c (expand_vector_operations): Likewise.
	* tree-vectorizer.c (adjust_simduid_builtins, note_simd_array_uses,
	execute_vect_slp): Likewise.
	* tree-vrp.c (check_all_array_refs, remove_range_assertions,
	vrp_initialize, identify_jump_threads, instrument_memory_accesses):
	Likewise.
	* ubsan.c (ubsan_pass): Likewise.
	* value-prof.c (verify_histograms, gimple_value_profile_transformations,
	gimple_find_values_to_profile): Likewise.
	* var-tracking.c (vt_find_locations, dump_dataflow_sets, vt_emit_notes,
	vt_initialize, delete_debug_insns, vt_finalize): Likewise.

gcc/testsuite/
	* g++.dg/plugin/selfassign.c (execute_warn_self_assign): Eliminate
	use of FOR_EACH_BB in favor of FOR_EACH_BB_FN, to make use of cfun
	explicit.
	* gcc.dg/plugin/selfassign.c (execute_warn_self_assign): Likewise.

From-SVN: r205828
2013-12-09 21:06:06 +00:00

825 lines
28 KiB
C

/* GCC instrumentation plugin for ThreadSanitizer.
Copyright (C) 2011-2013 Free Software Foundation, Inc.
Contributed by Dmitry Vyukov <dvyukov@google.com>
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tree.h"
#include "expr.h"
#include "intl.h"
#include "tm.h"
#include "basic-block.h"
#include "tree-ssa-alias.h"
#include "internal-fn.h"
#include "gimple-expr.h"
#include "is-a.h"
#include "gimple.h"
#include "gimplify.h"
#include "gimple-iterator.h"
#include "function.h"
#include "gimple-ssa.h"
#include "cgraph.h"
#include "tree-cfg.h"
#include "stringpool.h"
#include "tree-ssanames.h"
#include "tree-pass.h"
#include "tree-iterator.h"
#include "langhooks.h"
#include "output.h"
#include "options.h"
#include "target.h"
#include "diagnostic.h"
#include "tree-ssa-propagate.h"
#include "tsan.h"
#include "asan.h"
/* Number of instrumented memory accesses in the current function. */
/* Builds the following decl
void __tsan_read/writeX (void *addr); */
static tree
get_memory_access_decl (bool is_write, unsigned size)
{
enum built_in_function fcode;
if (size <= 1)
fcode = is_write ? BUILT_IN_TSAN_WRITE1
: BUILT_IN_TSAN_READ1;
else if (size <= 3)
fcode = is_write ? BUILT_IN_TSAN_WRITE2
: BUILT_IN_TSAN_READ2;
else if (size <= 7)
fcode = is_write ? BUILT_IN_TSAN_WRITE4
: BUILT_IN_TSAN_READ4;
else if (size <= 15)
fcode = is_write ? BUILT_IN_TSAN_WRITE8
: BUILT_IN_TSAN_READ8;
else
fcode = is_write ? BUILT_IN_TSAN_WRITE16
: BUILT_IN_TSAN_READ16;
return builtin_decl_implicit (fcode);
}
/* Check as to whether EXPR refers to a store to vptr. */
static tree
is_vptr_store (gimple stmt, tree expr, bool is_write)
{
if (is_write == true
&& gimple_assign_single_p (stmt)
&& TREE_CODE (expr) == COMPONENT_REF)
{
tree field = TREE_OPERAND (expr, 1);
if (TREE_CODE (field) == FIELD_DECL
&& DECL_VIRTUAL_P (field))
return gimple_assign_rhs1 (stmt);
}
return NULL;
}
/* Instruments EXPR if needed. If any instrumentation is inserted,
return true. */
static bool
instrument_expr (gimple_stmt_iterator gsi, tree expr, bool is_write)
{
tree base, rhs, expr_ptr, builtin_decl;
basic_block bb;
HOST_WIDE_INT size;
gimple stmt, g;
gimple_seq seq;
location_t loc;
size = int_size_in_bytes (TREE_TYPE (expr));
if (size == -1)
return false;
/* For now just avoid instrumenting bit field acceses.
TODO: handle bit-fields as if touching the whole field. */
HOST_WIDE_INT bitsize, bitpos;
tree offset;
enum machine_mode mode;
int volatilep = 0, unsignedp = 0;
base = get_inner_reference (expr, &bitsize, &bitpos, &offset,
&mode, &unsignedp, &volatilep, false);
/* No need to instrument accesses to decls that don't escape,
they can't escape to other threads then. */
if (DECL_P (base))
{
struct pt_solution pt;
memset (&pt, 0, sizeof (pt));
pt.escaped = 1;
pt.ipa_escaped = flag_ipa_pta != 0;
pt.nonlocal = 1;
if (!pt_solution_includes (&pt, base))
return false;
if (!is_global_var (base) && !may_be_aliased (base))
return false;
}
if (TREE_READONLY (base)
|| (TREE_CODE (base) == VAR_DECL
&& DECL_HARD_REGISTER (base)))
return false;
if (size == 0
|| bitpos % (size * BITS_PER_UNIT)
|| bitsize != size * BITS_PER_UNIT)
return false;
stmt = gsi_stmt (gsi);
loc = gimple_location (stmt);
rhs = is_vptr_store (stmt, expr, is_write);
gcc_checking_assert (rhs != NULL || is_gimple_addressable (expr));
expr_ptr = build_fold_addr_expr (unshare_expr (expr));
seq = NULL;
if (!is_gimple_val (expr_ptr))
{
g = gimple_build_assign (make_ssa_name (TREE_TYPE (expr_ptr), NULL),
expr_ptr);
expr_ptr = gimple_assign_lhs (g);
gimple_set_location (g, loc);
gimple_seq_add_stmt_without_update (&seq, g);
}
if (rhs == NULL)
g = gimple_build_call (get_memory_access_decl (is_write, size),
1, expr_ptr);
else
{
builtin_decl = builtin_decl_implicit (BUILT_IN_TSAN_VPTR_UPDATE);
g = gimple_build_call (builtin_decl, 1, expr_ptr);
}
gimple_set_location (g, loc);
gimple_seq_add_stmt_without_update (&seq, g);
/* Instrumentation for assignment of a function result
must be inserted after the call. Instrumentation for
reads of function arguments must be inserted before the call.
That's because the call can contain synchronization. */
if (is_gimple_call (stmt) && is_write)
{
/* If the call can throw, it must be the last stmt in
a basic block, so the instrumented stmts need to be
inserted in successor bbs. */
if (is_ctrl_altering_stmt (stmt))
{
edge e;
bb = gsi_bb (gsi);
e = find_fallthru_edge (bb->succs);
if (e)
gsi_insert_seq_on_edge_immediate (e, seq);
}
else
gsi_insert_seq_after (&gsi, seq, GSI_NEW_STMT);
}
else
gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
return true;
}
/* Actions for sync/atomic builtin transformations. */
enum tsan_atomic_action
{
check_last, add_seq_cst, add_acquire, weak_cas, strong_cas,
bool_cas, val_cas, lock_release, fetch_op, fetch_op_seq_cst
};
/* Table how to map sync/atomic builtins to their corresponding
tsan equivalents. */
static const struct tsan_map_atomic
{
enum built_in_function fcode, tsan_fcode;
enum tsan_atomic_action action;
enum tree_code code;
} tsan_atomic_table[] =
{
#define TRANSFORM(fcode, tsan_fcode, action, code) \
{ BUILT_IN_##fcode, BUILT_IN_##tsan_fcode, action, code }
#define CHECK_LAST(fcode, tsan_fcode) \
TRANSFORM (fcode, tsan_fcode, check_last, ERROR_MARK)
#define ADD_SEQ_CST(fcode, tsan_fcode) \
TRANSFORM (fcode, tsan_fcode, add_seq_cst, ERROR_MARK)
#define ADD_ACQUIRE(fcode, tsan_fcode) \
TRANSFORM (fcode, tsan_fcode, add_acquire, ERROR_MARK)
#define WEAK_CAS(fcode, tsan_fcode) \
TRANSFORM (fcode, tsan_fcode, weak_cas, ERROR_MARK)
#define STRONG_CAS(fcode, tsan_fcode) \
TRANSFORM (fcode, tsan_fcode, strong_cas, ERROR_MARK)
#define BOOL_CAS(fcode, tsan_fcode) \
TRANSFORM (fcode, tsan_fcode, bool_cas, ERROR_MARK)
#define VAL_CAS(fcode, tsan_fcode) \
TRANSFORM (fcode, tsan_fcode, val_cas, ERROR_MARK)
#define LOCK_RELEASE(fcode, tsan_fcode) \
TRANSFORM (fcode, tsan_fcode, lock_release, ERROR_MARK)
#define FETCH_OP(fcode, tsan_fcode, code) \
TRANSFORM (fcode, tsan_fcode, fetch_op, code)
#define FETCH_OPS(fcode, tsan_fcode, code) \
TRANSFORM (fcode, tsan_fcode, fetch_op_seq_cst, code)
CHECK_LAST (ATOMIC_LOAD_1, TSAN_ATOMIC8_LOAD),
CHECK_LAST (ATOMIC_LOAD_2, TSAN_ATOMIC16_LOAD),
CHECK_LAST (ATOMIC_LOAD_4, TSAN_ATOMIC32_LOAD),
CHECK_LAST (ATOMIC_LOAD_8, TSAN_ATOMIC64_LOAD),
CHECK_LAST (ATOMIC_LOAD_16, TSAN_ATOMIC128_LOAD),
CHECK_LAST (ATOMIC_STORE_1, TSAN_ATOMIC8_STORE),
CHECK_LAST (ATOMIC_STORE_2, TSAN_ATOMIC16_STORE),
CHECK_LAST (ATOMIC_STORE_4, TSAN_ATOMIC32_STORE),
CHECK_LAST (ATOMIC_STORE_8, TSAN_ATOMIC64_STORE),
CHECK_LAST (ATOMIC_STORE_16, TSAN_ATOMIC128_STORE),
CHECK_LAST (ATOMIC_EXCHANGE_1, TSAN_ATOMIC8_EXCHANGE),
CHECK_LAST (ATOMIC_EXCHANGE_2, TSAN_ATOMIC16_EXCHANGE),
CHECK_LAST (ATOMIC_EXCHANGE_4, TSAN_ATOMIC32_EXCHANGE),
CHECK_LAST (ATOMIC_EXCHANGE_8, TSAN_ATOMIC64_EXCHANGE),
CHECK_LAST (ATOMIC_EXCHANGE_16, TSAN_ATOMIC128_EXCHANGE),
CHECK_LAST (ATOMIC_FETCH_ADD_1, TSAN_ATOMIC8_FETCH_ADD),
CHECK_LAST (ATOMIC_FETCH_ADD_2, TSAN_ATOMIC16_FETCH_ADD),
CHECK_LAST (ATOMIC_FETCH_ADD_4, TSAN_ATOMIC32_FETCH_ADD),
CHECK_LAST (ATOMIC_FETCH_ADD_8, TSAN_ATOMIC64_FETCH_ADD),
CHECK_LAST (ATOMIC_FETCH_ADD_16, TSAN_ATOMIC128_FETCH_ADD),
CHECK_LAST (ATOMIC_FETCH_SUB_1, TSAN_ATOMIC8_FETCH_SUB),
CHECK_LAST (ATOMIC_FETCH_SUB_2, TSAN_ATOMIC16_FETCH_SUB),
CHECK_LAST (ATOMIC_FETCH_SUB_4, TSAN_ATOMIC32_FETCH_SUB),
CHECK_LAST (ATOMIC_FETCH_SUB_8, TSAN_ATOMIC64_FETCH_SUB),
CHECK_LAST (ATOMIC_FETCH_SUB_16, TSAN_ATOMIC128_FETCH_SUB),
CHECK_LAST (ATOMIC_FETCH_AND_1, TSAN_ATOMIC8_FETCH_AND),
CHECK_LAST (ATOMIC_FETCH_AND_2, TSAN_ATOMIC16_FETCH_AND),
CHECK_LAST (ATOMIC_FETCH_AND_4, TSAN_ATOMIC32_FETCH_AND),
CHECK_LAST (ATOMIC_FETCH_AND_8, TSAN_ATOMIC64_FETCH_AND),
CHECK_LAST (ATOMIC_FETCH_AND_16, TSAN_ATOMIC128_FETCH_AND),
CHECK_LAST (ATOMIC_FETCH_OR_1, TSAN_ATOMIC8_FETCH_OR),
CHECK_LAST (ATOMIC_FETCH_OR_2, TSAN_ATOMIC16_FETCH_OR),
CHECK_LAST (ATOMIC_FETCH_OR_4, TSAN_ATOMIC32_FETCH_OR),
CHECK_LAST (ATOMIC_FETCH_OR_8, TSAN_ATOMIC64_FETCH_OR),
CHECK_LAST (ATOMIC_FETCH_OR_16, TSAN_ATOMIC128_FETCH_OR),
CHECK_LAST (ATOMIC_FETCH_XOR_1, TSAN_ATOMIC8_FETCH_XOR),
CHECK_LAST (ATOMIC_FETCH_XOR_2, TSAN_ATOMIC16_FETCH_XOR),
CHECK_LAST (ATOMIC_FETCH_XOR_4, TSAN_ATOMIC32_FETCH_XOR),
CHECK_LAST (ATOMIC_FETCH_XOR_8, TSAN_ATOMIC64_FETCH_XOR),
CHECK_LAST (ATOMIC_FETCH_XOR_16, TSAN_ATOMIC128_FETCH_XOR),
CHECK_LAST (ATOMIC_FETCH_NAND_1, TSAN_ATOMIC8_FETCH_NAND),
CHECK_LAST (ATOMIC_FETCH_NAND_2, TSAN_ATOMIC16_FETCH_NAND),
CHECK_LAST (ATOMIC_FETCH_NAND_4, TSAN_ATOMIC32_FETCH_NAND),
CHECK_LAST (ATOMIC_FETCH_NAND_8, TSAN_ATOMIC64_FETCH_NAND),
CHECK_LAST (ATOMIC_FETCH_NAND_16, TSAN_ATOMIC128_FETCH_NAND),
CHECK_LAST (ATOMIC_THREAD_FENCE, TSAN_ATOMIC_THREAD_FENCE),
CHECK_LAST (ATOMIC_SIGNAL_FENCE, TSAN_ATOMIC_SIGNAL_FENCE),
FETCH_OP (ATOMIC_ADD_FETCH_1, TSAN_ATOMIC8_FETCH_ADD, PLUS_EXPR),
FETCH_OP (ATOMIC_ADD_FETCH_2, TSAN_ATOMIC16_FETCH_ADD, PLUS_EXPR),
FETCH_OP (ATOMIC_ADD_FETCH_4, TSAN_ATOMIC32_FETCH_ADD, PLUS_EXPR),
FETCH_OP (ATOMIC_ADD_FETCH_8, TSAN_ATOMIC64_FETCH_ADD, PLUS_EXPR),
FETCH_OP (ATOMIC_ADD_FETCH_16, TSAN_ATOMIC128_FETCH_ADD, PLUS_EXPR),
FETCH_OP (ATOMIC_SUB_FETCH_1, TSAN_ATOMIC8_FETCH_SUB, MINUS_EXPR),
FETCH_OP (ATOMIC_SUB_FETCH_2, TSAN_ATOMIC16_FETCH_SUB, MINUS_EXPR),
FETCH_OP (ATOMIC_SUB_FETCH_4, TSAN_ATOMIC32_FETCH_SUB, MINUS_EXPR),
FETCH_OP (ATOMIC_SUB_FETCH_8, TSAN_ATOMIC64_FETCH_SUB, MINUS_EXPR),
FETCH_OP (ATOMIC_SUB_FETCH_16, TSAN_ATOMIC128_FETCH_SUB, MINUS_EXPR),
FETCH_OP (ATOMIC_AND_FETCH_1, TSAN_ATOMIC8_FETCH_AND, BIT_AND_EXPR),
FETCH_OP (ATOMIC_AND_FETCH_2, TSAN_ATOMIC16_FETCH_AND, BIT_AND_EXPR),
FETCH_OP (ATOMIC_AND_FETCH_4, TSAN_ATOMIC32_FETCH_AND, BIT_AND_EXPR),
FETCH_OP (ATOMIC_AND_FETCH_8, TSAN_ATOMIC64_FETCH_AND, BIT_AND_EXPR),
FETCH_OP (ATOMIC_AND_FETCH_16, TSAN_ATOMIC128_FETCH_AND, BIT_AND_EXPR),
FETCH_OP (ATOMIC_OR_FETCH_1, TSAN_ATOMIC8_FETCH_OR, BIT_IOR_EXPR),
FETCH_OP (ATOMIC_OR_FETCH_2, TSAN_ATOMIC16_FETCH_OR, BIT_IOR_EXPR),
FETCH_OP (ATOMIC_OR_FETCH_4, TSAN_ATOMIC32_FETCH_OR, BIT_IOR_EXPR),
FETCH_OP (ATOMIC_OR_FETCH_8, TSAN_ATOMIC64_FETCH_OR, BIT_IOR_EXPR),
FETCH_OP (ATOMIC_OR_FETCH_16, TSAN_ATOMIC128_FETCH_OR, BIT_IOR_EXPR),
FETCH_OP (ATOMIC_XOR_FETCH_1, TSAN_ATOMIC8_FETCH_XOR, BIT_XOR_EXPR),
FETCH_OP (ATOMIC_XOR_FETCH_2, TSAN_ATOMIC16_FETCH_XOR, BIT_XOR_EXPR),
FETCH_OP (ATOMIC_XOR_FETCH_4, TSAN_ATOMIC32_FETCH_XOR, BIT_XOR_EXPR),
FETCH_OP (ATOMIC_XOR_FETCH_8, TSAN_ATOMIC64_FETCH_XOR, BIT_XOR_EXPR),
FETCH_OP (ATOMIC_XOR_FETCH_16, TSAN_ATOMIC128_FETCH_XOR, BIT_XOR_EXPR),
FETCH_OP (ATOMIC_NAND_FETCH_1, TSAN_ATOMIC8_FETCH_NAND, BIT_NOT_EXPR),
FETCH_OP (ATOMIC_NAND_FETCH_2, TSAN_ATOMIC16_FETCH_NAND, BIT_NOT_EXPR),
FETCH_OP (ATOMIC_NAND_FETCH_4, TSAN_ATOMIC32_FETCH_NAND, BIT_NOT_EXPR),
FETCH_OP (ATOMIC_NAND_FETCH_8, TSAN_ATOMIC64_FETCH_NAND, BIT_NOT_EXPR),
FETCH_OP (ATOMIC_NAND_FETCH_16, TSAN_ATOMIC128_FETCH_NAND, BIT_NOT_EXPR),
ADD_ACQUIRE (SYNC_LOCK_TEST_AND_SET_1, TSAN_ATOMIC8_EXCHANGE),
ADD_ACQUIRE (SYNC_LOCK_TEST_AND_SET_2, TSAN_ATOMIC16_EXCHANGE),
ADD_ACQUIRE (SYNC_LOCK_TEST_AND_SET_4, TSAN_ATOMIC32_EXCHANGE),
ADD_ACQUIRE (SYNC_LOCK_TEST_AND_SET_8, TSAN_ATOMIC64_EXCHANGE),
ADD_ACQUIRE (SYNC_LOCK_TEST_AND_SET_16, TSAN_ATOMIC128_EXCHANGE),
ADD_SEQ_CST (SYNC_FETCH_AND_ADD_1, TSAN_ATOMIC8_FETCH_ADD),
ADD_SEQ_CST (SYNC_FETCH_AND_ADD_2, TSAN_ATOMIC16_FETCH_ADD),
ADD_SEQ_CST (SYNC_FETCH_AND_ADD_4, TSAN_ATOMIC32_FETCH_ADD),
ADD_SEQ_CST (SYNC_FETCH_AND_ADD_8, TSAN_ATOMIC64_FETCH_ADD),
ADD_SEQ_CST (SYNC_FETCH_AND_ADD_16, TSAN_ATOMIC128_FETCH_ADD),
ADD_SEQ_CST (SYNC_FETCH_AND_SUB_1, TSAN_ATOMIC8_FETCH_SUB),
ADD_SEQ_CST (SYNC_FETCH_AND_SUB_2, TSAN_ATOMIC16_FETCH_SUB),
ADD_SEQ_CST (SYNC_FETCH_AND_SUB_4, TSAN_ATOMIC32_FETCH_SUB),
ADD_SEQ_CST (SYNC_FETCH_AND_SUB_8, TSAN_ATOMIC64_FETCH_SUB),
ADD_SEQ_CST (SYNC_FETCH_AND_SUB_16, TSAN_ATOMIC128_FETCH_SUB),
ADD_SEQ_CST (SYNC_FETCH_AND_AND_1, TSAN_ATOMIC8_FETCH_AND),
ADD_SEQ_CST (SYNC_FETCH_AND_AND_2, TSAN_ATOMIC16_FETCH_AND),
ADD_SEQ_CST (SYNC_FETCH_AND_AND_4, TSAN_ATOMIC32_FETCH_AND),
ADD_SEQ_CST (SYNC_FETCH_AND_AND_8, TSAN_ATOMIC64_FETCH_AND),
ADD_SEQ_CST (SYNC_FETCH_AND_AND_16, TSAN_ATOMIC128_FETCH_AND),
ADD_SEQ_CST (SYNC_FETCH_AND_OR_1, TSAN_ATOMIC8_FETCH_OR),
ADD_SEQ_CST (SYNC_FETCH_AND_OR_2, TSAN_ATOMIC16_FETCH_OR),
ADD_SEQ_CST (SYNC_FETCH_AND_OR_4, TSAN_ATOMIC32_FETCH_OR),
ADD_SEQ_CST (SYNC_FETCH_AND_OR_8, TSAN_ATOMIC64_FETCH_OR),
ADD_SEQ_CST (SYNC_FETCH_AND_OR_16, TSAN_ATOMIC128_FETCH_OR),
ADD_SEQ_CST (SYNC_FETCH_AND_XOR_1, TSAN_ATOMIC8_FETCH_XOR),
ADD_SEQ_CST (SYNC_FETCH_AND_XOR_2, TSAN_ATOMIC16_FETCH_XOR),
ADD_SEQ_CST (SYNC_FETCH_AND_XOR_4, TSAN_ATOMIC32_FETCH_XOR),
ADD_SEQ_CST (SYNC_FETCH_AND_XOR_8, TSAN_ATOMIC64_FETCH_XOR),
ADD_SEQ_CST (SYNC_FETCH_AND_XOR_16, TSAN_ATOMIC128_FETCH_XOR),
ADD_SEQ_CST (SYNC_FETCH_AND_NAND_1, TSAN_ATOMIC8_FETCH_NAND),
ADD_SEQ_CST (SYNC_FETCH_AND_NAND_2, TSAN_ATOMIC16_FETCH_NAND),
ADD_SEQ_CST (SYNC_FETCH_AND_NAND_4, TSAN_ATOMIC32_FETCH_NAND),
ADD_SEQ_CST (SYNC_FETCH_AND_NAND_8, TSAN_ATOMIC64_FETCH_NAND),
ADD_SEQ_CST (SYNC_FETCH_AND_NAND_16, TSAN_ATOMIC128_FETCH_NAND),
ADD_SEQ_CST (SYNC_SYNCHRONIZE, TSAN_ATOMIC_THREAD_FENCE),
FETCH_OPS (SYNC_ADD_AND_FETCH_1, TSAN_ATOMIC8_FETCH_ADD, PLUS_EXPR),
FETCH_OPS (SYNC_ADD_AND_FETCH_2, TSAN_ATOMIC16_FETCH_ADD, PLUS_EXPR),
FETCH_OPS (SYNC_ADD_AND_FETCH_4, TSAN_ATOMIC32_FETCH_ADD, PLUS_EXPR),
FETCH_OPS (SYNC_ADD_AND_FETCH_8, TSAN_ATOMIC64_FETCH_ADD, PLUS_EXPR),
FETCH_OPS (SYNC_ADD_AND_FETCH_16, TSAN_ATOMIC128_FETCH_ADD, PLUS_EXPR),
FETCH_OPS (SYNC_SUB_AND_FETCH_1, TSAN_ATOMIC8_FETCH_SUB, MINUS_EXPR),
FETCH_OPS (SYNC_SUB_AND_FETCH_2, TSAN_ATOMIC16_FETCH_SUB, MINUS_EXPR),
FETCH_OPS (SYNC_SUB_AND_FETCH_4, TSAN_ATOMIC32_FETCH_SUB, MINUS_EXPR),
FETCH_OPS (SYNC_SUB_AND_FETCH_8, TSAN_ATOMIC64_FETCH_SUB, MINUS_EXPR),
FETCH_OPS (SYNC_SUB_AND_FETCH_16, TSAN_ATOMIC128_FETCH_SUB, MINUS_EXPR),
FETCH_OPS (SYNC_AND_AND_FETCH_1, TSAN_ATOMIC8_FETCH_AND, BIT_AND_EXPR),
FETCH_OPS (SYNC_AND_AND_FETCH_2, TSAN_ATOMIC16_FETCH_AND, BIT_AND_EXPR),
FETCH_OPS (SYNC_AND_AND_FETCH_4, TSAN_ATOMIC32_FETCH_AND, BIT_AND_EXPR),
FETCH_OPS (SYNC_AND_AND_FETCH_8, TSAN_ATOMIC64_FETCH_AND, BIT_AND_EXPR),
FETCH_OPS (SYNC_AND_AND_FETCH_16, TSAN_ATOMIC128_FETCH_AND, BIT_AND_EXPR),
FETCH_OPS (SYNC_OR_AND_FETCH_1, TSAN_ATOMIC8_FETCH_OR, BIT_IOR_EXPR),
FETCH_OPS (SYNC_OR_AND_FETCH_2, TSAN_ATOMIC16_FETCH_OR, BIT_IOR_EXPR),
FETCH_OPS (SYNC_OR_AND_FETCH_4, TSAN_ATOMIC32_FETCH_OR, BIT_IOR_EXPR),
FETCH_OPS (SYNC_OR_AND_FETCH_8, TSAN_ATOMIC64_FETCH_OR, BIT_IOR_EXPR),
FETCH_OPS (SYNC_OR_AND_FETCH_16, TSAN_ATOMIC128_FETCH_OR, BIT_IOR_EXPR),
FETCH_OPS (SYNC_XOR_AND_FETCH_1, TSAN_ATOMIC8_FETCH_XOR, BIT_XOR_EXPR),
FETCH_OPS (SYNC_XOR_AND_FETCH_2, TSAN_ATOMIC16_FETCH_XOR, BIT_XOR_EXPR),
FETCH_OPS (SYNC_XOR_AND_FETCH_4, TSAN_ATOMIC32_FETCH_XOR, BIT_XOR_EXPR),
FETCH_OPS (SYNC_XOR_AND_FETCH_8, TSAN_ATOMIC64_FETCH_XOR, BIT_XOR_EXPR),
FETCH_OPS (SYNC_XOR_AND_FETCH_16, TSAN_ATOMIC128_FETCH_XOR, BIT_XOR_EXPR),
FETCH_OPS (SYNC_NAND_AND_FETCH_1, TSAN_ATOMIC8_FETCH_NAND, BIT_NOT_EXPR),
FETCH_OPS (SYNC_NAND_AND_FETCH_2, TSAN_ATOMIC16_FETCH_NAND, BIT_NOT_EXPR),
FETCH_OPS (SYNC_NAND_AND_FETCH_4, TSAN_ATOMIC32_FETCH_NAND, BIT_NOT_EXPR),
FETCH_OPS (SYNC_NAND_AND_FETCH_8, TSAN_ATOMIC64_FETCH_NAND, BIT_NOT_EXPR),
FETCH_OPS (SYNC_NAND_AND_FETCH_16, TSAN_ATOMIC128_FETCH_NAND, BIT_NOT_EXPR),
WEAK_CAS (ATOMIC_COMPARE_EXCHANGE_1, TSAN_ATOMIC8_COMPARE_EXCHANGE_WEAK),
WEAK_CAS (ATOMIC_COMPARE_EXCHANGE_2, TSAN_ATOMIC16_COMPARE_EXCHANGE_WEAK),
WEAK_CAS (ATOMIC_COMPARE_EXCHANGE_4, TSAN_ATOMIC32_COMPARE_EXCHANGE_WEAK),
WEAK_CAS (ATOMIC_COMPARE_EXCHANGE_8, TSAN_ATOMIC64_COMPARE_EXCHANGE_WEAK),
WEAK_CAS (ATOMIC_COMPARE_EXCHANGE_16, TSAN_ATOMIC128_COMPARE_EXCHANGE_WEAK),
STRONG_CAS (ATOMIC_COMPARE_EXCHANGE_1, TSAN_ATOMIC8_COMPARE_EXCHANGE_STRONG),
STRONG_CAS (ATOMIC_COMPARE_EXCHANGE_2,
TSAN_ATOMIC16_COMPARE_EXCHANGE_STRONG),
STRONG_CAS (ATOMIC_COMPARE_EXCHANGE_4,
TSAN_ATOMIC32_COMPARE_EXCHANGE_STRONG),
STRONG_CAS (ATOMIC_COMPARE_EXCHANGE_8,
TSAN_ATOMIC64_COMPARE_EXCHANGE_STRONG),
STRONG_CAS (ATOMIC_COMPARE_EXCHANGE_16,
TSAN_ATOMIC128_COMPARE_EXCHANGE_STRONG),
BOOL_CAS (SYNC_BOOL_COMPARE_AND_SWAP_1,
TSAN_ATOMIC8_COMPARE_EXCHANGE_STRONG),
BOOL_CAS (SYNC_BOOL_COMPARE_AND_SWAP_2,
TSAN_ATOMIC16_COMPARE_EXCHANGE_STRONG),
BOOL_CAS (SYNC_BOOL_COMPARE_AND_SWAP_4,
TSAN_ATOMIC32_COMPARE_EXCHANGE_STRONG),
BOOL_CAS (SYNC_BOOL_COMPARE_AND_SWAP_8,
TSAN_ATOMIC64_COMPARE_EXCHANGE_STRONG),
BOOL_CAS (SYNC_BOOL_COMPARE_AND_SWAP_16,
TSAN_ATOMIC128_COMPARE_EXCHANGE_STRONG),
VAL_CAS (SYNC_VAL_COMPARE_AND_SWAP_1, TSAN_ATOMIC8_COMPARE_EXCHANGE_STRONG),
VAL_CAS (SYNC_VAL_COMPARE_AND_SWAP_2, TSAN_ATOMIC16_COMPARE_EXCHANGE_STRONG),
VAL_CAS (SYNC_VAL_COMPARE_AND_SWAP_4, TSAN_ATOMIC32_COMPARE_EXCHANGE_STRONG),
VAL_CAS (SYNC_VAL_COMPARE_AND_SWAP_8, TSAN_ATOMIC64_COMPARE_EXCHANGE_STRONG),
VAL_CAS (SYNC_VAL_COMPARE_AND_SWAP_16,
TSAN_ATOMIC128_COMPARE_EXCHANGE_STRONG),
LOCK_RELEASE (SYNC_LOCK_RELEASE_1, TSAN_ATOMIC8_STORE),
LOCK_RELEASE (SYNC_LOCK_RELEASE_2, TSAN_ATOMIC16_STORE),
LOCK_RELEASE (SYNC_LOCK_RELEASE_4, TSAN_ATOMIC32_STORE),
LOCK_RELEASE (SYNC_LOCK_RELEASE_8, TSAN_ATOMIC64_STORE),
LOCK_RELEASE (SYNC_LOCK_RELEASE_16, TSAN_ATOMIC128_STORE)
};
/* Instrument an atomic builtin. */
static void
instrument_builtin_call (gimple_stmt_iterator *gsi)
{
gimple stmt = gsi_stmt (*gsi), g;
tree callee = gimple_call_fndecl (stmt), last_arg, args[6], t, lhs;
enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
unsigned int i, num = gimple_call_num_args (stmt), j;
for (j = 0; j < 6 && j < num; j++)
args[j] = gimple_call_arg (stmt, j);
for (i = 0; i < ARRAY_SIZE (tsan_atomic_table); i++)
if (fcode != tsan_atomic_table[i].fcode)
continue;
else
{
tree decl = builtin_decl_implicit (tsan_atomic_table[i].tsan_fcode);
if (decl == NULL_TREE)
return;
switch (tsan_atomic_table[i].action)
{
case check_last:
case fetch_op:
last_arg = gimple_call_arg (stmt, num - 1);
if (!tree_fits_uhwi_p (last_arg)
|| tree_to_uhwi (last_arg) > MEMMODEL_SEQ_CST)
return;
gimple_call_set_fndecl (stmt, decl);
update_stmt (stmt);
if (tsan_atomic_table[i].action == fetch_op)
{
args[1] = gimple_call_arg (stmt, 1);
goto adjust_result;
}
return;
case add_seq_cst:
case add_acquire:
case fetch_op_seq_cst:
gcc_assert (num <= 2);
for (j = 0; j < num; j++)
args[j] = gimple_call_arg (stmt, j);
for (; j < 2; j++)
args[j] = NULL_TREE;
args[num] = build_int_cst (NULL_TREE,
tsan_atomic_table[i].action
!= add_acquire
? MEMMODEL_SEQ_CST
: MEMMODEL_ACQUIRE);
update_gimple_call (gsi, decl, num + 1, args[0], args[1], args[2]);
stmt = gsi_stmt (*gsi);
if (tsan_atomic_table[i].action == fetch_op_seq_cst)
{
adjust_result:
lhs = gimple_call_lhs (stmt);
if (lhs == NULL_TREE)
return;
if (!useless_type_conversion_p (TREE_TYPE (lhs),
TREE_TYPE (args[1])))
{
tree var = make_ssa_name (TREE_TYPE (lhs), NULL);
g = gimple_build_assign_with_ops (NOP_EXPR, var,
args[1], NULL_TREE);
gsi_insert_after (gsi, g, GSI_NEW_STMT);
args[1] = var;
}
gimple_call_set_lhs (stmt,
make_ssa_name (TREE_TYPE (lhs), NULL));
/* BIT_NOT_EXPR stands for NAND. */
if (tsan_atomic_table[i].code == BIT_NOT_EXPR)
{
tree var = make_ssa_name (TREE_TYPE (lhs), NULL);
g = gimple_build_assign_with_ops (BIT_AND_EXPR, var,
gimple_call_lhs (stmt),
args[1]);
gsi_insert_after (gsi, g, GSI_NEW_STMT);
g = gimple_build_assign_with_ops (BIT_NOT_EXPR, lhs, var,
NULL_TREE);
}
else
g = gimple_build_assign_with_ops (tsan_atomic_table[i].code,
lhs,
gimple_call_lhs (stmt),
args[1]);
update_stmt (stmt);
gsi_insert_after (gsi, g, GSI_NEW_STMT);
}
return;
case weak_cas:
if (!integer_nonzerop (gimple_call_arg (stmt, 3)))
continue;
/* FALLTHRU */
case strong_cas:
gcc_assert (num == 6);
for (j = 0; j < 6; j++)
args[j] = gimple_call_arg (stmt, j);
if (!tree_fits_uhwi_p (args[4])
|| tree_to_uhwi (args[4]) > MEMMODEL_SEQ_CST)
return;
if (!tree_fits_uhwi_p (args[5])
|| tree_to_uhwi (args[5]) > MEMMODEL_SEQ_CST)
return;
update_gimple_call (gsi, decl, 5, args[0], args[1], args[2],
args[4], args[5]);
return;
case bool_cas:
case val_cas:
gcc_assert (num == 3);
for (j = 0; j < 3; j++)
args[j] = gimple_call_arg (stmt, j);
t = TYPE_ARG_TYPES (TREE_TYPE (decl));
t = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (t)));
t = create_tmp_var (t, NULL);
mark_addressable (t);
if (!useless_type_conversion_p (TREE_TYPE (t),
TREE_TYPE (args[1])))
{
g = gimple_build_assign_with_ops (NOP_EXPR,
make_ssa_name (TREE_TYPE (t),
NULL),
args[1], NULL_TREE);
gsi_insert_before (gsi, g, GSI_SAME_STMT);
args[1] = gimple_assign_lhs (g);
}
g = gimple_build_assign (t, args[1]);
gsi_insert_before (gsi, g, GSI_SAME_STMT);
lhs = gimple_call_lhs (stmt);
update_gimple_call (gsi, decl, 5, args[0],
build_fold_addr_expr (t), args[2],
build_int_cst (NULL_TREE,
MEMMODEL_SEQ_CST),
build_int_cst (NULL_TREE,
MEMMODEL_SEQ_CST));
if (tsan_atomic_table[i].action == val_cas && lhs)
{
tree cond;
stmt = gsi_stmt (*gsi);
g = gimple_build_assign (make_ssa_name (TREE_TYPE (t), NULL),
t);
gsi_insert_after (gsi, g, GSI_NEW_STMT);
t = make_ssa_name (TREE_TYPE (TREE_TYPE (decl)), stmt);
cond = build2 (NE_EXPR, boolean_type_node, t,
build_int_cst (TREE_TYPE (t), 0));
g = gimple_build_assign_with_ops (COND_EXPR, lhs, cond,
args[1],
gimple_assign_lhs (g));
gimple_call_set_lhs (stmt, t);
update_stmt (stmt);
gsi_insert_after (gsi, g, GSI_NEW_STMT);
}
return;
case lock_release:
gcc_assert (num == 1);
t = TYPE_ARG_TYPES (TREE_TYPE (decl));
t = TREE_VALUE (TREE_CHAIN (t));
update_gimple_call (gsi, decl, 3, gimple_call_arg (stmt, 0),
build_int_cst (t, 0),
build_int_cst (NULL_TREE,
MEMMODEL_RELEASE));
return;
default:
continue;
}
}
}
/* Instruments the gimple pointed to by GSI. Return
true if func entry/exit should be instrumented. */
static bool
instrument_gimple (gimple_stmt_iterator *gsi)
{
gimple stmt;
tree rhs, lhs;
bool instrumented = false;
stmt = gsi_stmt (*gsi);
if (is_gimple_call (stmt)
&& (gimple_call_fndecl (stmt)
!= builtin_decl_implicit (BUILT_IN_TSAN_INIT)))
{
if (is_gimple_builtin_call (stmt))
instrument_builtin_call (gsi);
return true;
}
else if (is_gimple_assign (stmt)
&& !gimple_clobber_p (stmt))
{
if (gimple_store_p (stmt))
{
lhs = gimple_assign_lhs (stmt);
instrumented = instrument_expr (*gsi, lhs, true);
}
if (gimple_assign_load_p (stmt))
{
rhs = gimple_assign_rhs1 (stmt);
instrumented = instrument_expr (*gsi, rhs, false);
}
}
return instrumented;
}
/* Instruments all interesting memory accesses in the current function.
Return true if func entry/exit should be instrumented. */
static bool
instrument_memory_accesses (void)
{
basic_block bb;
gimple_stmt_iterator gsi;
bool fentry_exit_instrument = false;
FOR_EACH_BB_FN (bb, cfun)
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
fentry_exit_instrument |= instrument_gimple (&gsi);
return fentry_exit_instrument;
}
/* Instruments function entry. */
static void
instrument_func_entry (void)
{
basic_block succ_bb;
gimple_stmt_iterator gsi;
tree ret_addr, builtin_decl;
gimple g;
succ_bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
gsi = gsi_after_labels (succ_bb);
builtin_decl = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
g = gimple_build_call (builtin_decl, 1, integer_zero_node);
ret_addr = make_ssa_name (ptr_type_node, NULL);
gimple_call_set_lhs (g, ret_addr);
gimple_set_location (g, cfun->function_start_locus);
gsi_insert_before (&gsi, g, GSI_SAME_STMT);
builtin_decl = builtin_decl_implicit (BUILT_IN_TSAN_FUNC_ENTRY);
g = gimple_build_call (builtin_decl, 1, ret_addr);
gimple_set_location (g, cfun->function_start_locus);
gsi_insert_before (&gsi, g, GSI_SAME_STMT);
}
/* Instruments function exits. */
static void
instrument_func_exit (void)
{
location_t loc;
basic_block exit_bb;
gimple_stmt_iterator gsi;
gimple stmt, g;
tree builtin_decl;
edge e;
edge_iterator ei;
/* Find all function exits. */
exit_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
FOR_EACH_EDGE (e, ei, exit_bb->preds)
{
gsi = gsi_last_bb (e->src);
stmt = gsi_stmt (gsi);
gcc_assert (gimple_code (stmt) == GIMPLE_RETURN
|| gimple_call_builtin_p (stmt, BUILT_IN_RETURN));
loc = gimple_location (stmt);
builtin_decl = builtin_decl_implicit (BUILT_IN_TSAN_FUNC_EXIT);
g = gimple_build_call (builtin_decl, 0);
gimple_set_location (g, loc);
gsi_insert_before (&gsi, g, GSI_SAME_STMT);
}
}
/* ThreadSanitizer instrumentation pass. */
static unsigned
tsan_pass (void)
{
initialize_sanitizer_builtins ();
if (instrument_memory_accesses ())
{
instrument_func_entry ();
instrument_func_exit ();
}
return 0;
}
/* The pass's gate. */
static bool
tsan_gate (void)
{
return (flag_sanitize & SANITIZE_THREAD) != 0;
}
/* Inserts __tsan_init () into the list of CTORs. */
void
tsan_finish_file (void)
{
tree ctor_statements = NULL_TREE;
initialize_sanitizer_builtins ();
tree init_decl = builtin_decl_implicit (BUILT_IN_TSAN_INIT);
append_to_statement_list (build_call_expr (init_decl, 0),
&ctor_statements);
cgraph_build_static_cdtor ('I', ctor_statements,
MAX_RESERVED_INIT_PRIORITY - 1);
}
/* The pass descriptor. */
namespace {
const pass_data pass_data_tsan =
{
GIMPLE_PASS, /* type */
"tsan", /* name */
OPTGROUP_NONE, /* optinfo_flags */
true, /* has_gate */
true, /* has_execute */
TV_NONE, /* tv_id */
( PROP_ssa | PROP_cfg ), /* properties_required */
0, /* properties_provided */
0, /* properties_destroyed */
0, /* todo_flags_start */
( TODO_verify_all | TODO_update_ssa ), /* todo_flags_finish */
};
class pass_tsan : public gimple_opt_pass
{
public:
pass_tsan (gcc::context *ctxt)
: gimple_opt_pass (pass_data_tsan, ctxt)
{}
/* opt_pass methods: */
opt_pass * clone () { return new pass_tsan (m_ctxt); }
bool gate () { return tsan_gate (); }
unsigned int execute () { return tsan_pass (); }
}; // class pass_tsan
} // anon namespace
gimple_opt_pass *
make_pass_tsan (gcc::context *ctxt)
{
return new pass_tsan (ctxt);
}
static bool
tsan_gate_O0 (void)
{
return (flag_sanitize & SANITIZE_THREAD) != 0 && !optimize;
}
namespace {
const pass_data pass_data_tsan_O0 =
{
GIMPLE_PASS, /* type */
"tsan0", /* name */
OPTGROUP_NONE, /* optinfo_flags */
true, /* has_gate */
true, /* has_execute */
TV_NONE, /* tv_id */
( PROP_ssa | PROP_cfg ), /* properties_required */
0, /* properties_provided */
0, /* properties_destroyed */
0, /* todo_flags_start */
( TODO_verify_all | TODO_update_ssa ), /* todo_flags_finish */
};
class pass_tsan_O0 : public gimple_opt_pass
{
public:
pass_tsan_O0 (gcc::context *ctxt)
: gimple_opt_pass (pass_data_tsan_O0, ctxt)
{}
/* opt_pass methods: */
bool gate () { return tsan_gate_O0 (); }
unsigned int execute () { return tsan_pass (); }
}; // class pass_tsan_O0
} // anon namespace
gimple_opt_pass *
make_pass_tsan_O0 (gcc::context *ctxt)
{
return new pass_tsan_O0 (ctxt);
}