1 /* Passes for transactional memory support.
2 Copyright (C) 2008-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
23 #include "hash-table.h"
25 #include "basic-block.h"
26 #include "tree-ssa-alias.h"
27 #include "internal-fn.h"
29 #include "gimple-expr.h"
37 #include "gimple-iterator.h"
38 #include "gimplify-me.h"
39 #include "gimple-walk.h"
40 #include "gimple-ssa.h"
43 #include "stringpool.h"
44 #include "tree-ssanames.h"
45 #include "tree-into-ssa.h"
46 #include "tree-pass.h"
47 #include "tree-inline.h"
48 #include "diagnostic-core.h"
51 #include "trans-mem.h"
54 #include "langhooks.h"
55 #include "gimple-pretty-print.h"
57 #include "tree-ssa-address.h"
61 #define A_RUNINSTRUMENTEDCODE 0x0001
62 #define A_RUNUNINSTRUMENTEDCODE 0x0002
63 #define A_SAVELIVEVARIABLES 0x0004
64 #define A_RESTORELIVEVARIABLES 0x0008
65 #define A_ABORTTRANSACTION 0x0010
67 #define AR_USERABORT 0x0001
68 #define AR_USERRETRY 0x0002
69 #define AR_TMCONFLICT 0x0004
70 #define AR_EXCEPTIONBLOCKABORT 0x0008
71 #define AR_OUTERABORT 0x0010
73 #define MODE_SERIALIRREVOCABLE 0x0000
76 /* The representation of a transaction changes several times during the
77 lowering process. In the beginning, in the front-end we have the
78 GENERIC tree TRANSACTION_EXPR. For example,
86 During initial gimplification (gimplify.c) the TRANSACTION_EXPR node is
87 trivially replaced with a GIMPLE_TRANSACTION node.
89 During pass_lower_tm, we examine the body of transactions looking
90 for aborts. Transactions that do not contain an abort may be
91 merged into an outer transaction. We also add a TRY-FINALLY node
92 to arrange for the transaction to be committed on any exit.
94 [??? Think about how this arrangement affects throw-with-commit
95 and throw-with-abort operations. In this case we want the TRY to
96 handle gotos, but not to catch any exceptions because the transaction
97 will already be closed.]
99 GIMPLE_TRANSACTION [label=NULL] {
106 __builtin___tm_abort ();
108 __builtin___tm_commit ();
112 During pass_lower_eh, we create EH regions for the transactions,
113 intermixed with the regular EH stuff. This gives us a nice persistent
114 mapping (all the way through rtl) from transactional memory operation
115 back to the transaction, which allows us to get the abnormal edges
116 correct to model transaction aborts and restarts:
118 GIMPLE_TRANSACTION [label=over]
124 __builtin___tm_abort ();
125 __builtin___tm_commit ();
128 This is the end of all_lowering_passes, and so is what is present
129 during the IPA passes, and through all of the optimization passes.
131 During pass_ipa_tm, we examine all GIMPLE_TRANSACTION blocks in all
132 functions and mark functions for cloning.
134 At the end of gimple optimization, before exiting SSA form,
135 pass_tm_edges replaces statements that perform transactional
136 memory operations with the appropriate TM builtins, and swap
137 out function calls with their transactional clones. At this
138 point we introduce the abnormal transaction restart edges and
139 complete lowering of the GIMPLE_TRANSACTION node.
141 x = __builtin___tm_start (MAY_ABORT);
143 if (x & abort_transaction)
146 t0 = __builtin___tm_load (global);
148 __builtin___tm_store (&global, t1);
150 __builtin___tm_abort ();
151 __builtin___tm_commit ();
155 static void *expand_regions (struct tm_region
*,
156 void *(*callback
)(struct tm_region
*, void *),
160 /* Return the attributes we want to examine for X, or NULL if it's not
161 something we examine. We look at function types, but allow pointers
162 to function types and function decls and peek through. */
165 get_attrs_for (const_tree x
)
167 switch (TREE_CODE (x
))
170 return TYPE_ATTRIBUTES (TREE_TYPE (x
));
177 if (TREE_CODE (x
) != POINTER_TYPE
)
183 if (TREE_CODE (x
) != FUNCTION_TYPE
&& TREE_CODE (x
) != METHOD_TYPE
)
189 return TYPE_ATTRIBUTES (x
);
193 /* Return true if X has been marked TM_PURE. */
196 is_tm_pure (const_tree x
)
200 switch (TREE_CODE (x
))
211 if (TREE_CODE (x
) != POINTER_TYPE
)
217 if (TREE_CODE (x
) != FUNCTION_TYPE
&& TREE_CODE (x
) != METHOD_TYPE
)
222 flags
= flags_from_decl_or_type (x
);
223 return (flags
& ECF_TM_PURE
) != 0;
226 /* Return true if X has been marked TM_IRREVOCABLE. */
229 is_tm_irrevocable (tree x
)
231 tree attrs
= get_attrs_for (x
);
233 if (attrs
&& lookup_attribute ("transaction_unsafe", attrs
))
236 /* A call to the irrevocable builtin is by definition,
238 if (TREE_CODE (x
) == ADDR_EXPR
)
239 x
= TREE_OPERAND (x
, 0);
240 if (TREE_CODE (x
) == FUNCTION_DECL
241 && DECL_BUILT_IN_CLASS (x
) == BUILT_IN_NORMAL
242 && DECL_FUNCTION_CODE (x
) == BUILT_IN_TM_IRREVOCABLE
)
248 /* Return true if X has been marked TM_SAFE. */
251 is_tm_safe (const_tree x
)
255 tree attrs
= get_attrs_for (x
);
258 if (lookup_attribute ("transaction_safe", attrs
))
260 if (lookup_attribute ("transaction_may_cancel_outer", attrs
))
267 /* Return true if CALL is const, or tm_pure. */
270 is_tm_pure_call (gimple call
)
272 tree fn
= gimple_call_fn (call
);
274 if (TREE_CODE (fn
) == ADDR_EXPR
)
276 fn
= TREE_OPERAND (fn
, 0);
277 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
282 return is_tm_pure (fn
);
285 /* Return true if X has been marked TM_CALLABLE. */
288 is_tm_callable (tree x
)
290 tree attrs
= get_attrs_for (x
);
293 if (lookup_attribute ("transaction_callable", attrs
))
295 if (lookup_attribute ("transaction_safe", attrs
))
297 if (lookup_attribute ("transaction_may_cancel_outer", attrs
))
303 /* Return true if X has been marked TRANSACTION_MAY_CANCEL_OUTER. */
306 is_tm_may_cancel_outer (tree x
)
308 tree attrs
= get_attrs_for (x
);
310 return lookup_attribute ("transaction_may_cancel_outer", attrs
) != NULL
;
314 /* Return true for built in functions that "end" a transaction. */
317 is_tm_ending_fndecl (tree fndecl
)
319 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
320 switch (DECL_FUNCTION_CODE (fndecl
))
322 case BUILT_IN_TM_COMMIT
:
323 case BUILT_IN_TM_COMMIT_EH
:
324 case BUILT_IN_TM_ABORT
:
325 case BUILT_IN_TM_IRREVOCABLE
:
334 /* Return true if STMT is a built in function call that "ends" a
338 is_tm_ending (gimple stmt
)
342 if (gimple_code (stmt
) != GIMPLE_CALL
)
345 fndecl
= gimple_call_fndecl (stmt
);
346 return (fndecl
!= NULL_TREE
347 && is_tm_ending_fndecl (fndecl
));
350 /* Return true if STMT is a TM load. */
353 is_tm_load (gimple stmt
)
357 if (gimple_code (stmt
) != GIMPLE_CALL
)
360 fndecl
= gimple_call_fndecl (stmt
);
361 return (fndecl
&& DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
362 && BUILTIN_TM_LOAD_P (DECL_FUNCTION_CODE (fndecl
)));
365 /* Same as above, but for simple TM loads, that is, not the
366 after-write, after-read, etc optimized variants. */
369 is_tm_simple_load (gimple stmt
)
373 if (gimple_code (stmt
) != GIMPLE_CALL
)
376 fndecl
= gimple_call_fndecl (stmt
);
377 if (fndecl
&& DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
379 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
380 return (fcode
== BUILT_IN_TM_LOAD_1
381 || fcode
== BUILT_IN_TM_LOAD_2
382 || fcode
== BUILT_IN_TM_LOAD_4
383 || fcode
== BUILT_IN_TM_LOAD_8
384 || fcode
== BUILT_IN_TM_LOAD_FLOAT
385 || fcode
== BUILT_IN_TM_LOAD_DOUBLE
386 || fcode
== BUILT_IN_TM_LOAD_LDOUBLE
387 || fcode
== BUILT_IN_TM_LOAD_M64
388 || fcode
== BUILT_IN_TM_LOAD_M128
389 || fcode
== BUILT_IN_TM_LOAD_M256
);
394 /* Return true if STMT is a TM store. */
397 is_tm_store (gimple stmt
)
401 if (gimple_code (stmt
) != GIMPLE_CALL
)
404 fndecl
= gimple_call_fndecl (stmt
);
405 return (fndecl
&& DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
406 && BUILTIN_TM_STORE_P (DECL_FUNCTION_CODE (fndecl
)));
409 /* Same as above, but for simple TM stores, that is, not the
410 after-write, after-read, etc optimized variants. */
413 is_tm_simple_store (gimple stmt
)
417 if (gimple_code (stmt
) != GIMPLE_CALL
)
420 fndecl
= gimple_call_fndecl (stmt
);
421 if (fndecl
&& DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
423 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
424 return (fcode
== BUILT_IN_TM_STORE_1
425 || fcode
== BUILT_IN_TM_STORE_2
426 || fcode
== BUILT_IN_TM_STORE_4
427 || fcode
== BUILT_IN_TM_STORE_8
428 || fcode
== BUILT_IN_TM_STORE_FLOAT
429 || fcode
== BUILT_IN_TM_STORE_DOUBLE
430 || fcode
== BUILT_IN_TM_STORE_LDOUBLE
431 || fcode
== BUILT_IN_TM_STORE_M64
432 || fcode
== BUILT_IN_TM_STORE_M128
433 || fcode
== BUILT_IN_TM_STORE_M256
);
438 /* Return true if FNDECL is BUILT_IN_TM_ABORT. */
441 is_tm_abort (tree fndecl
)
444 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
445 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_TM_ABORT
);
448 /* Build a GENERIC tree for a user abort. This is called by front ends
449 while transforming the __tm_abort statement. */
452 build_tm_abort_call (location_t loc
, bool is_outer
)
454 return build_call_expr_loc (loc
, builtin_decl_explicit (BUILT_IN_TM_ABORT
), 1,
455 build_int_cst (integer_type_node
,
457 | (is_outer
? AR_OUTERABORT
: 0)));
460 /* Map for aribtrary function replacement under TM, as created
461 by the tm_wrap attribute. */
463 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map
)))
467 record_tm_replacement (tree from
, tree to
)
469 struct tree_map
**slot
, *h
;
471 /* Do not inline wrapper functions that will get replaced in the TM
474 Suppose you have foo() that will get replaced into tmfoo(). Make
475 sure the inliner doesn't try to outsmart us and inline foo()
476 before we get a chance to do the TM replacement. */
477 DECL_UNINLINABLE (from
) = 1;
479 if (tm_wrap_map
== NULL
)
480 tm_wrap_map
= htab_create_ggc (32, tree_map_hash
, tree_map_eq
, 0);
482 h
= ggc_alloc
<tree_map
> ();
483 h
->hash
= htab_hash_pointer (from
);
487 slot
= (struct tree_map
**)
488 htab_find_slot_with_hash (tm_wrap_map
, h
, h
->hash
, INSERT
);
492 /* Return a TM-aware replacement function for DECL. */
495 find_tm_replacement_function (tree fndecl
)
499 struct tree_map
*h
, in
;
501 in
.base
.from
= fndecl
;
502 in
.hash
= htab_hash_pointer (fndecl
);
503 h
= (struct tree_map
*) htab_find_with_hash (tm_wrap_map
, &in
, in
.hash
);
508 /* ??? We may well want TM versions of most of the common <string.h>
509 functions. For now, we've already these two defined. */
510 /* Adjust expand_call_tm() attributes as necessary for the cases
512 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
513 switch (DECL_FUNCTION_CODE (fndecl
))
515 case BUILT_IN_MEMCPY
:
516 return builtin_decl_explicit (BUILT_IN_TM_MEMCPY
);
517 case BUILT_IN_MEMMOVE
:
518 return builtin_decl_explicit (BUILT_IN_TM_MEMMOVE
);
519 case BUILT_IN_MEMSET
:
520 return builtin_decl_explicit (BUILT_IN_TM_MEMSET
);
528 /* When appropriate, record TM replacement for memory allocation functions.
530 FROM is the FNDECL to wrap. */
532 tm_malloc_replacement (tree from
)
537 if (TREE_CODE (from
) != FUNCTION_DECL
)
540 /* If we have a previous replacement, the user must be explicitly
541 wrapping malloc/calloc/free. They better know what they're
543 if (find_tm_replacement_function (from
))
546 str
= IDENTIFIER_POINTER (DECL_NAME (from
));
548 if (!strcmp (str
, "malloc"))
549 to
= builtin_decl_explicit (BUILT_IN_TM_MALLOC
);
550 else if (!strcmp (str
, "calloc"))
551 to
= builtin_decl_explicit (BUILT_IN_TM_CALLOC
);
552 else if (!strcmp (str
, "free"))
553 to
= builtin_decl_explicit (BUILT_IN_TM_FREE
);
557 TREE_NOTHROW (to
) = 0;
559 record_tm_replacement (from
, to
);
562 /* Diagnostics for tm_safe functions/regions. Called by the front end
563 once we've lowered the function to high-gimple. */
565 /* Subroutine of diagnose_tm_safe_errors, called through walk_gimple_seq.
566 Process exactly one statement. WI->INFO is set to non-null when in
567 the context of a tm_safe function, and null for a __transaction block. */
569 #define DIAG_TM_OUTER 1
570 #define DIAG_TM_SAFE 2
571 #define DIAG_TM_RELAXED 4
575 unsigned int summary_flags
: 8;
576 unsigned int block_flags
: 8;
577 unsigned int func_flags
: 8;
578 unsigned int saw_volatile
: 1;
582 /* Return true if T is a volatile variable of some kind. */
585 volatile_var_p (tree t
)
587 return (SSA_VAR_P (t
)
588 && TREE_THIS_VOLATILE (TREE_TYPE (t
)));
591 /* Tree callback function for diagnose_tm pass. */
594 diagnose_tm_1_op (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
597 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
598 struct diagnose_tm
*d
= (struct diagnose_tm
*) wi
->info
;
600 if (volatile_var_p (*tp
)
601 && d
->block_flags
& DIAG_TM_SAFE
605 error_at (gimple_location (d
->stmt
),
606 "invalid volatile use of %qD inside transaction",
614 is_tm_safe_or_pure (const_tree x
)
616 return is_tm_safe (x
) || is_tm_pure (x
);
620 diagnose_tm_1 (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
621 struct walk_stmt_info
*wi
)
623 gimple stmt
= gsi_stmt (*gsi
);
624 struct diagnose_tm
*d
= (struct diagnose_tm
*) wi
->info
;
626 /* Save stmt for use in leaf analysis. */
629 switch (gimple_code (stmt
))
633 tree fn
= gimple_call_fn (stmt
);
635 if ((d
->summary_flags
& DIAG_TM_OUTER
) == 0
636 && is_tm_may_cancel_outer (fn
))
637 error_at (gimple_location (stmt
),
638 "%<transaction_may_cancel_outer%> function call not within"
639 " outer transaction or %<transaction_may_cancel_outer%>");
641 if (d
->summary_flags
& DIAG_TM_SAFE
)
643 bool is_safe
, direct_call_p
;
646 if (TREE_CODE (fn
) == ADDR_EXPR
647 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
)
649 direct_call_p
= true;
650 replacement
= TREE_OPERAND (fn
, 0);
651 replacement
= find_tm_replacement_function (replacement
);
657 direct_call_p
= false;
658 replacement
= NULL_TREE
;
661 if (is_tm_safe_or_pure (fn
))
663 else if (is_tm_callable (fn
) || is_tm_irrevocable (fn
))
665 /* A function explicitly marked transaction_callable as
666 opposed to transaction_safe is being defined to be
667 unsafe as part of its ABI, regardless of its contents. */
670 else if (direct_call_p
)
672 if (IS_TYPE_OR_DECL_P (fn
)
673 && flags_from_decl_or_type (fn
) & ECF_TM_BUILTIN
)
675 else if (replacement
)
677 /* ??? At present we've been considering replacements
678 merely transaction_callable, and therefore might
679 enter irrevocable. The tm_wrap attribute has not
680 yet made it into the new language spec. */
685 /* ??? Diagnostics for unmarked direct calls moved into
686 the IPA pass. Section 3.2 of the spec details how
687 functions not marked should be considered "implicitly
688 safe" based on having examined the function body. */
694 /* An unmarked indirect call. Consider it unsafe even
695 though optimization may yet figure out how to inline. */
701 if (TREE_CODE (fn
) == ADDR_EXPR
)
702 fn
= TREE_OPERAND (fn
, 0);
703 if (d
->block_flags
& DIAG_TM_SAFE
)
706 error_at (gimple_location (stmt
),
707 "unsafe function call %qD within "
708 "atomic transaction", fn
);
711 if (!DECL_P (fn
) || DECL_NAME (fn
))
712 error_at (gimple_location (stmt
),
713 "unsafe function call %qE within "
714 "atomic transaction", fn
);
716 error_at (gimple_location (stmt
),
717 "unsafe indirect function call within "
718 "atomic transaction");
724 error_at (gimple_location (stmt
),
725 "unsafe function call %qD within "
726 "%<transaction_safe%> function", fn
);
729 if (!DECL_P (fn
) || DECL_NAME (fn
))
730 error_at (gimple_location (stmt
),
731 "unsafe function call %qE within "
732 "%<transaction_safe%> function", fn
);
734 error_at (gimple_location (stmt
),
735 "unsafe indirect function call within "
736 "%<transaction_safe%> function");
745 /* ??? We ought to come up with a way to add attributes to
746 asm statements, and then add "transaction_safe" to it.
747 Either that or get the language spec to resurrect __tm_waiver. */
748 if (d
->block_flags
& DIAG_TM_SAFE
)
749 error_at (gimple_location (stmt
),
750 "asm not allowed in atomic transaction");
751 else if (d
->func_flags
& DIAG_TM_SAFE
)
752 error_at (gimple_location (stmt
),
753 "asm not allowed in %<transaction_safe%> function");
756 case GIMPLE_TRANSACTION
:
758 gimple_transaction trans_stmt
= as_a
<gimple_transaction
> (stmt
);
759 unsigned char inner_flags
= DIAG_TM_SAFE
;
761 if (gimple_transaction_subcode (trans_stmt
) & GTMA_IS_RELAXED
)
763 if (d
->block_flags
& DIAG_TM_SAFE
)
764 error_at (gimple_location (stmt
),
765 "relaxed transaction in atomic transaction");
766 else if (d
->func_flags
& DIAG_TM_SAFE
)
767 error_at (gimple_location (stmt
),
768 "relaxed transaction in %<transaction_safe%> function");
769 inner_flags
= DIAG_TM_RELAXED
;
771 else if (gimple_transaction_subcode (trans_stmt
) & GTMA_IS_OUTER
)
774 error_at (gimple_location (stmt
),
775 "outer transaction in transaction");
776 else if (d
->func_flags
& DIAG_TM_OUTER
)
777 error_at (gimple_location (stmt
),
778 "outer transaction in "
779 "%<transaction_may_cancel_outer%> function");
780 else if (d
->func_flags
& DIAG_TM_SAFE
)
781 error_at (gimple_location (stmt
),
782 "outer transaction in %<transaction_safe%> function");
783 inner_flags
|= DIAG_TM_OUTER
;
786 *handled_ops_p
= true;
787 if (gimple_transaction_body (trans_stmt
))
789 struct walk_stmt_info wi_inner
;
790 struct diagnose_tm d_inner
;
792 memset (&d_inner
, 0, sizeof (d_inner
));
793 d_inner
.func_flags
= d
->func_flags
;
794 d_inner
.block_flags
= d
->block_flags
| inner_flags
;
795 d_inner
.summary_flags
= d_inner
.func_flags
| d_inner
.block_flags
;
797 memset (&wi_inner
, 0, sizeof (wi_inner
));
798 wi_inner
.info
= &d_inner
;
800 walk_gimple_seq (gimple_transaction_body (trans_stmt
),
801 diagnose_tm_1
, diagnose_tm_1_op
, &wi_inner
);
814 diagnose_tm_blocks (void)
816 struct walk_stmt_info wi
;
817 struct diagnose_tm d
;
819 memset (&d
, 0, sizeof (d
));
820 if (is_tm_may_cancel_outer (current_function_decl
))
821 d
.func_flags
= DIAG_TM_OUTER
| DIAG_TM_SAFE
;
822 else if (is_tm_safe (current_function_decl
))
823 d
.func_flags
= DIAG_TM_SAFE
;
824 d
.summary_flags
= d
.func_flags
;
826 memset (&wi
, 0, sizeof (wi
));
829 walk_gimple_seq (gimple_body (current_function_decl
),
830 diagnose_tm_1
, diagnose_tm_1_op
, &wi
);
837 const pass_data pass_data_diagnose_tm_blocks
=
839 GIMPLE_PASS
, /* type */
840 "*diagnose_tm_blocks", /* name */
841 OPTGROUP_NONE
, /* optinfo_flags */
842 TV_TRANS_MEM
, /* tv_id */
843 PROP_gimple_any
, /* properties_required */
844 0, /* properties_provided */
845 0, /* properties_destroyed */
846 0, /* todo_flags_start */
847 0, /* todo_flags_finish */
850 class pass_diagnose_tm_blocks
: public gimple_opt_pass
853 pass_diagnose_tm_blocks (gcc::context
*ctxt
)
854 : gimple_opt_pass (pass_data_diagnose_tm_blocks
, ctxt
)
857 /* opt_pass methods: */
858 virtual bool gate (function
*) { return flag_tm
; }
859 virtual unsigned int execute (function
*) { return diagnose_tm_blocks (); }
861 }; // class pass_diagnose_tm_blocks
866 make_pass_diagnose_tm_blocks (gcc::context
*ctxt
)
868 return new pass_diagnose_tm_blocks (ctxt
);
871 /* Instead of instrumenting thread private memory, we save the
872 addresses in a log which we later use to save/restore the addresses
873 upon transaction start/restart.
875 The log is keyed by address, where each element contains individual
876 statements among different code paths that perform the store.
878 This log is later used to generate either plain save/restore of the
879 addresses upon transaction start/restart, or calls to the ITM_L*
882 So for something like:
884 struct large { int x[1000]; };
885 struct large lala = { 0 };
891 We can either save/restore:
894 trxn = _ITM_startTransaction ();
895 if (trxn & a_saveLiveVariables)
896 tmp_lala1 = lala.x[i];
897 else if (a & a_restoreLiveVariables)
898 lala.x[i] = tmp_lala1;
900 or use the logging functions:
903 trxn = _ITM_startTransaction ();
904 _ITM_LU4 (&lala.x[i]);
906 Obviously, if we use _ITM_L* to log, we prefer to call _ITM_L* as
907 far up the dominator tree to shadow all of the writes to a given
908 location (thus reducing the total number of logging calls), but not
909 so high as to be called on a path that does not perform a
912 /* One individual log entry. We may have multiple statements for the
913 same location if neither dominate each other (on different
915 typedef struct tm_log_entry
917 /* Address to save. */
919 /* Entry block for the transaction this address occurs in. */
920 basic_block entry_block
;
921 /* Dominating statements the store occurs in. */
923 /* Initially, while we are building the log, we place a nonzero
924 value here to mean that this address *will* be saved with a
925 save/restore sequence. Later, when generating the save sequence
926 we place the SSA temp generated here. */
931 /* Log entry hashtable helpers. */
933 struct log_entry_hasher
935 typedef tm_log_entry value_type
;
936 typedef tm_log_entry compare_type
;
937 static inline hashval_t
hash (const value_type
*);
938 static inline bool equal (const value_type
*, const compare_type
*);
939 static inline void remove (value_type
*);
942 /* Htab support. Return hash value for a `tm_log_entry'. */
944 log_entry_hasher::hash (const value_type
*log
)
946 return iterative_hash_expr (log
->addr
, 0);
949 /* Htab support. Return true if two log entries are the same. */
951 log_entry_hasher::equal (const value_type
*log1
, const compare_type
*log2
)
955 rth: I suggest that we get rid of the component refs etc.
956 I.e. resolve the reference to base + offset.
958 We may need to actually finish a merge with mainline for this,
959 since we'd like to be presented with Richi's MEM_REF_EXPRs more
960 often than not. But in the meantime your tm_log_entry could save
961 the results of get_inner_reference.
963 See: g++.dg/tm/pr46653.C
966 /* Special case plain equality because operand_equal_p() below will
967 return FALSE if the addresses are equal but they have
968 side-effects (e.g. a volatile address). */
969 if (log1
->addr
== log2
->addr
)
972 return operand_equal_p (log1
->addr
, log2
->addr
, 0);
975 /* Htab support. Free one tm_log_entry. */
977 log_entry_hasher::remove (value_type
*lp
)
979 lp
->stmts
.release ();
984 /* The actual log. */
985 static hash_table
<log_entry_hasher
> *tm_log
;
987 /* Addresses to log with a save/restore sequence. These should be in
989 static vec
<tree
> tm_log_save_addresses
;
991 enum thread_memory_type
995 mem_transaction_local
,
999 typedef struct tm_new_mem_map
1001 /* SSA_NAME being dereferenced. */
1003 enum thread_memory_type local_new_memory
;
1006 /* Hashtable helpers. */
1008 struct tm_mem_map_hasher
: typed_free_remove
<tm_new_mem_map_t
>
1010 typedef tm_new_mem_map_t value_type
;
1011 typedef tm_new_mem_map_t compare_type
;
1012 static inline hashval_t
hash (const value_type
*);
1013 static inline bool equal (const value_type
*, const compare_type
*);
1017 tm_mem_map_hasher::hash (const value_type
*v
)
1019 return (intptr_t)v
->val
>> 4;
1023 tm_mem_map_hasher::equal (const value_type
*v
, const compare_type
*c
)
1025 return v
->val
== c
->val
;
1028 /* Map for an SSA_NAME originally pointing to a non aliased new piece
1029 of memory (malloc, alloc, etc). */
1030 static hash_table
<tm_mem_map_hasher
> *tm_new_mem_hash
;
1032 /* Initialize logging data structures. */
1036 tm_log
= new hash_table
<log_entry_hasher
> (10);
1037 tm_new_mem_hash
= new hash_table
<tm_mem_map_hasher
> (5);
1038 tm_log_save_addresses
.create (5);
1041 /* Free logging data structures. */
1043 tm_log_delete (void)
1047 delete tm_new_mem_hash
;
1048 tm_new_mem_hash
= NULL
;
1049 tm_log_save_addresses
.release ();
1052 /* Return true if MEM is a transaction invariant memory for the TM
1053 region starting at REGION_ENTRY_BLOCK. */
1055 transaction_invariant_address_p (const_tree mem
, basic_block region_entry_block
)
1057 if ((TREE_CODE (mem
) == INDIRECT_REF
|| TREE_CODE (mem
) == MEM_REF
)
1058 && TREE_CODE (TREE_OPERAND (mem
, 0)) == SSA_NAME
)
1062 def_bb
= gimple_bb (SSA_NAME_DEF_STMT (TREE_OPERAND (mem
, 0)));
1063 return def_bb
!= region_entry_block
1064 && dominated_by_p (CDI_DOMINATORS
, region_entry_block
, def_bb
);
1067 mem
= strip_invariant_refs (mem
);
1068 return mem
&& (CONSTANT_CLASS_P (mem
) || decl_address_invariant_p (mem
));
1071 /* Given an address ADDR in STMT, find it in the memory log or add it,
1072 making sure to keep only the addresses highest in the dominator
1075 ENTRY_BLOCK is the entry_block for the transaction.
1077 If we find the address in the log, make sure it's either the same
1078 address, or an equivalent one that dominates ADDR.
1080 If we find the address, but neither ADDR dominates the found
1081 address, nor the found one dominates ADDR, we're on different
1082 execution paths. Add it.
1084 If known, ENTRY_BLOCK is the entry block for the region, otherwise
1087 tm_log_add (basic_block entry_block
, tree addr
, gimple stmt
)
1089 tm_log_entry
**slot
;
1090 struct tm_log_entry l
, *lp
;
1093 slot
= tm_log
->find_slot (&l
, INSERT
);
1096 tree type
= TREE_TYPE (addr
);
1098 lp
= XNEW (struct tm_log_entry
);
1102 /* Small invariant addresses can be handled as save/restores. */
1104 && transaction_invariant_address_p (lp
->addr
, entry_block
)
1105 && TYPE_SIZE_UNIT (type
) != NULL
1106 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type
))
1107 && ((HOST_WIDE_INT
) tree_to_uhwi (TYPE_SIZE_UNIT (type
))
1108 < PARAM_VALUE (PARAM_TM_MAX_AGGREGATE_SIZE
))
1109 /* We must be able to copy this type normally. I.e., no
1110 special constructors and the like. */
1111 && !TREE_ADDRESSABLE (type
))
1113 lp
->save_var
= create_tmp_reg (TREE_TYPE (lp
->addr
), "tm_save");
1114 lp
->stmts
.create (0);
1115 lp
->entry_block
= entry_block
;
1116 /* Save addresses separately in dominator order so we don't
1117 get confused by overlapping addresses in the save/restore
1119 tm_log_save_addresses
.safe_push (lp
->addr
);
1123 /* Use the logging functions. */
1124 lp
->stmts
.create (5);
1125 lp
->stmts
.quick_push (stmt
);
1126 lp
->save_var
= NULL
;
1136 /* If we're generating a save/restore sequence, we don't care
1137 about statements. */
1141 for (i
= 0; lp
->stmts
.iterate (i
, &oldstmt
); ++i
)
1143 if (stmt
== oldstmt
)
1145 /* We already have a store to the same address, higher up the
1146 dominator tree. Nothing to do. */
1147 if (dominated_by_p (CDI_DOMINATORS
,
1148 gimple_bb (stmt
), gimple_bb (oldstmt
)))
1150 /* We should be processing blocks in dominator tree order. */
1151 gcc_assert (!dominated_by_p (CDI_DOMINATORS
,
1152 gimple_bb (oldstmt
), gimple_bb (stmt
)));
1154 /* Store is on a different code path. */
1155 lp
->stmts
.safe_push (stmt
);
1159 /* Gimplify the address of a TARGET_MEM_REF. Return the SSA_NAME
1160 result, insert the new statements before GSI. */
1163 gimplify_addr (gimple_stmt_iterator
*gsi
, tree x
)
1165 if (TREE_CODE (x
) == TARGET_MEM_REF
)
1166 x
= tree_mem_ref_addr (build_pointer_type (TREE_TYPE (x
)), x
);
1168 x
= build_fold_addr_expr (x
);
1169 return force_gimple_operand_gsi (gsi
, x
, true, NULL
, true, GSI_SAME_STMT
);
1172 /* Instrument one address with the logging functions.
1173 ADDR is the address to save.
1174 STMT is the statement before which to place it. */
1176 tm_log_emit_stmt (tree addr
, gimple stmt
)
1178 tree type
= TREE_TYPE (addr
);
1179 tree size
= TYPE_SIZE_UNIT (type
);
1180 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
1182 enum built_in_function code
= BUILT_IN_TM_LOG
;
1184 if (type
== float_type_node
)
1185 code
= BUILT_IN_TM_LOG_FLOAT
;
1186 else if (type
== double_type_node
)
1187 code
= BUILT_IN_TM_LOG_DOUBLE
;
1188 else if (type
== long_double_type_node
)
1189 code
= BUILT_IN_TM_LOG_LDOUBLE
;
1190 else if (tree_fits_uhwi_p (size
))
1192 unsigned int n
= tree_to_uhwi (size
);
1196 code
= BUILT_IN_TM_LOG_1
;
1199 code
= BUILT_IN_TM_LOG_2
;
1202 code
= BUILT_IN_TM_LOG_4
;
1205 code
= BUILT_IN_TM_LOG_8
;
1208 code
= BUILT_IN_TM_LOG
;
1209 if (TREE_CODE (type
) == VECTOR_TYPE
)
1211 if (n
== 8 && builtin_decl_explicit (BUILT_IN_TM_LOG_M64
))
1212 code
= BUILT_IN_TM_LOG_M64
;
1213 else if (n
== 16 && builtin_decl_explicit (BUILT_IN_TM_LOG_M128
))
1214 code
= BUILT_IN_TM_LOG_M128
;
1215 else if (n
== 32 && builtin_decl_explicit (BUILT_IN_TM_LOG_M256
))
1216 code
= BUILT_IN_TM_LOG_M256
;
1222 addr
= gimplify_addr (&gsi
, addr
);
1223 if (code
== BUILT_IN_TM_LOG
)
1224 log
= gimple_build_call (builtin_decl_explicit (code
), 2, addr
, size
);
1226 log
= gimple_build_call (builtin_decl_explicit (code
), 1, addr
);
1227 gsi_insert_before (&gsi
, log
, GSI_SAME_STMT
);
1230 /* Go through the log and instrument address that must be instrumented
1231 with the logging functions. Leave the save/restore addresses for
1236 hash_table
<log_entry_hasher
>::iterator hi
;
1237 struct tm_log_entry
*lp
;
1239 FOR_EACH_HASH_TABLE_ELEMENT (*tm_log
, lp
, tm_log_entry_t
, hi
)
1246 fprintf (dump_file
, "TM thread private mem logging: ");
1247 print_generic_expr (dump_file
, lp
->addr
, 0);
1248 fprintf (dump_file
, "\n");
1254 fprintf (dump_file
, "DUMPING to variable\n");
1260 fprintf (dump_file
, "DUMPING with logging functions\n");
1261 for (i
= 0; lp
->stmts
.iterate (i
, &stmt
); ++i
)
1262 tm_log_emit_stmt (lp
->addr
, stmt
);
1267 /* Emit the save sequence for the corresponding addresses in the log.
1268 ENTRY_BLOCK is the entry block for the transaction.
1269 BB is the basic block to insert the code in. */
1271 tm_log_emit_saves (basic_block entry_block
, basic_block bb
)
1274 gimple_stmt_iterator gsi
= gsi_last_bb (bb
);
1276 struct tm_log_entry l
, *lp
;
1278 for (i
= 0; i
< tm_log_save_addresses
.length (); ++i
)
1280 l
.addr
= tm_log_save_addresses
[i
];
1281 lp
= *(tm_log
->find_slot (&l
, NO_INSERT
));
1282 gcc_assert (lp
->save_var
!= NULL
);
1284 /* We only care about variables in the current transaction. */
1285 if (lp
->entry_block
!= entry_block
)
1288 stmt
= gimple_build_assign (lp
->save_var
, unshare_expr (lp
->addr
));
1290 /* Make sure we can create an SSA_NAME for this type. For
1291 instance, aggregates aren't allowed, in which case the system
1292 will create a VOP for us and everything will just work. */
1293 if (is_gimple_reg_type (TREE_TYPE (lp
->save_var
)))
1295 lp
->save_var
= make_ssa_name (lp
->save_var
, stmt
);
1296 gimple_assign_set_lhs (stmt
, lp
->save_var
);
1299 gsi_insert_before (&gsi
, stmt
, GSI_SAME_STMT
);
1303 /* Emit the restore sequence for the corresponding addresses in the log.
1304 ENTRY_BLOCK is the entry block for the transaction.
1305 BB is the basic block to insert the code in. */
1307 tm_log_emit_restores (basic_block entry_block
, basic_block bb
)
1310 struct tm_log_entry l
, *lp
;
1311 gimple_stmt_iterator gsi
;
1314 for (i
= tm_log_save_addresses
.length () - 1; i
>= 0; i
--)
1316 l
.addr
= tm_log_save_addresses
[i
];
1317 lp
= *(tm_log
->find_slot (&l
, NO_INSERT
));
1318 gcc_assert (lp
->save_var
!= NULL
);
1320 /* We only care about variables in the current transaction. */
1321 if (lp
->entry_block
!= entry_block
)
1324 /* Restores are in LIFO order from the saves in case we have
1326 gsi
= gsi_start_bb (bb
);
1328 stmt
= gimple_build_assign (unshare_expr (lp
->addr
), lp
->save_var
);
1329 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
1334 static tree
lower_sequence_tm (gimple_stmt_iterator
*, bool *,
1335 struct walk_stmt_info
*);
1336 static tree
lower_sequence_no_tm (gimple_stmt_iterator
*, bool *,
1337 struct walk_stmt_info
*);
1339 /* Evaluate an address X being dereferenced and determine if it
1340 originally points to a non aliased new chunk of memory (malloc,
1343 Return MEM_THREAD_LOCAL if it points to a thread-local address.
1344 Return MEM_TRANSACTION_LOCAL if it points to a transaction-local address.
1345 Return MEM_NON_LOCAL otherwise.
1347 ENTRY_BLOCK is the entry block to the transaction containing the
1348 dereference of X. */
1349 static enum thread_memory_type
1350 thread_private_new_memory (basic_block entry_block
, tree x
)
1353 enum tree_code code
;
1354 tm_new_mem_map_t
**slot
;
1355 tm_new_mem_map_t elt
, *elt_p
;
1357 enum thread_memory_type retval
= mem_transaction_local
;
1360 || TREE_CODE (x
) != SSA_NAME
1361 /* Possible uninitialized use, or a function argument. In
1362 either case, we don't care. */
1363 || SSA_NAME_IS_DEFAULT_DEF (x
))
1364 return mem_non_local
;
1366 /* Look in cache first. */
1368 slot
= tm_new_mem_hash
->find_slot (&elt
, INSERT
);
1371 return elt_p
->local_new_memory
;
1373 /* Optimistically assume the memory is transaction local during
1374 processing. This catches recursion into this variable. */
1375 *slot
= elt_p
= XNEW (tm_new_mem_map_t
);
1377 elt_p
->local_new_memory
= mem_transaction_local
;
1379 /* Search DEF chain to find the original definition of this address. */
1382 if (ptr_deref_may_alias_global_p (x
))
1384 /* Address escapes. This is not thread-private. */
1385 retval
= mem_non_local
;
1386 goto new_memory_ret
;
1389 stmt
= SSA_NAME_DEF_STMT (x
);
1391 /* If the malloc call is outside the transaction, this is
1393 if (retval
!= mem_thread_local
1394 && !dominated_by_p (CDI_DOMINATORS
, gimple_bb (stmt
), entry_block
))
1395 retval
= mem_thread_local
;
1397 if (is_gimple_assign (stmt
))
1399 code
= gimple_assign_rhs_code (stmt
);
1400 /* x = foo ==> foo */
1401 if (code
== SSA_NAME
)
1402 x
= gimple_assign_rhs1 (stmt
);
1403 /* x = foo + n ==> foo */
1404 else if (code
== POINTER_PLUS_EXPR
)
1405 x
= gimple_assign_rhs1 (stmt
);
1406 /* x = (cast*) foo ==> foo */
1407 else if (code
== VIEW_CONVERT_EXPR
|| code
== NOP_EXPR
)
1408 x
= gimple_assign_rhs1 (stmt
);
1409 /* x = c ? op1 : op2 == > op1 or op2 just like a PHI */
1410 else if (code
== COND_EXPR
)
1412 tree op1
= gimple_assign_rhs2 (stmt
);
1413 tree op2
= gimple_assign_rhs3 (stmt
);
1414 enum thread_memory_type mem
;
1415 retval
= thread_private_new_memory (entry_block
, op1
);
1416 if (retval
== mem_non_local
)
1417 goto new_memory_ret
;
1418 mem
= thread_private_new_memory (entry_block
, op2
);
1419 retval
= MIN (retval
, mem
);
1420 goto new_memory_ret
;
1424 retval
= mem_non_local
;
1425 goto new_memory_ret
;
1430 if (gimple_code (stmt
) == GIMPLE_PHI
)
1433 enum thread_memory_type mem
;
1434 tree phi_result
= gimple_phi_result (stmt
);
1436 /* If any of the ancestors are non-local, we are sure to
1437 be non-local. Otherwise we can avoid doing anything
1438 and inherit what has already been generated. */
1440 for (i
= 0; i
< gimple_phi_num_args (stmt
); ++i
)
1442 tree op
= PHI_ARG_DEF (stmt
, i
);
1444 /* Exclude self-assignment. */
1445 if (phi_result
== op
)
1448 mem
= thread_private_new_memory (entry_block
, op
);
1449 if (mem
== mem_non_local
)
1452 goto new_memory_ret
;
1454 retval
= MIN (retval
, mem
);
1456 goto new_memory_ret
;
1461 while (TREE_CODE (x
) == SSA_NAME
);
1463 if (stmt
&& is_gimple_call (stmt
) && gimple_call_flags (stmt
) & ECF_MALLOC
)
1464 /* Thread-local or transaction-local. */
1467 retval
= mem_non_local
;
1470 elt_p
->local_new_memory
= retval
;
1474 /* Determine whether X has to be instrumented using a read
1477 ENTRY_BLOCK is the entry block for the region where stmt resides
1478 in. NULL if unknown.
1480 STMT is the statement in which X occurs in. It is used for thread
1481 private memory instrumentation. If no TPM instrumentation is
1482 desired, STMT should be null. */
1484 requires_barrier (basic_block entry_block
, tree x
, gimple stmt
)
1487 while (handled_component_p (x
))
1488 x
= TREE_OPERAND (x
, 0);
1490 switch (TREE_CODE (x
))
1495 enum thread_memory_type ret
;
1497 ret
= thread_private_new_memory (entry_block
, TREE_OPERAND (x
, 0));
1498 if (ret
== mem_non_local
)
1500 if (stmt
&& ret
== mem_thread_local
)
1501 /* ?? Should we pass `orig', or the INDIRECT_REF X. ?? */
1502 tm_log_add (entry_block
, orig
, stmt
);
1504 /* Transaction-locals require nothing at all. For malloc, a
1505 transaction restart frees the memory and we reallocate.
1506 For alloca, the stack pointer gets reset by the retry and
1511 case TARGET_MEM_REF
:
1512 if (TREE_CODE (TMR_BASE (x
)) != ADDR_EXPR
)
1514 x
= TREE_OPERAND (TMR_BASE (x
), 0);
1515 if (TREE_CODE (x
) == PARM_DECL
)
1517 gcc_assert (TREE_CODE (x
) == VAR_DECL
);
1523 if (DECL_BY_REFERENCE (x
))
1525 /* ??? This value is a pointer, but aggregate_value_p has been
1526 jigged to return true which confuses needs_to_live_in_memory.
1527 This ought to be cleaned up generically.
1529 FIXME: Verify this still happens after the next mainline
1530 merge. Testcase ie g++.dg/tm/pr47554.C.
1535 if (is_global_var (x
))
1536 return !TREE_READONLY (x
);
1537 if (/* FIXME: This condition should actually go below in the
1538 tm_log_add() call, however is_call_clobbered() depends on
1539 aliasing info which is not available during
1540 gimplification. Since requires_barrier() gets called
1541 during lower_sequence_tm/gimplification, leave the call
1542 to needs_to_live_in_memory until we eliminate
1543 lower_sequence_tm altogether. */
1544 needs_to_live_in_memory (x
))
1548 /* For local memory that doesn't escape (aka thread private
1549 memory), we can either save the value at the beginning of
1550 the transaction and restore on restart, or call a tm
1551 function to dynamically save and restore on restart
1554 tm_log_add (entry_block
, orig
, stmt
);
1563 /* Mark the GIMPLE_ASSIGN statement as appropriate for being inside
1564 a transaction region. */
1567 examine_assign_tm (unsigned *state
, gimple_stmt_iterator
*gsi
)
1569 gimple stmt
= gsi_stmt (*gsi
);
1571 if (requires_barrier (/*entry_block=*/NULL
, gimple_assign_rhs1 (stmt
), NULL
))
1572 *state
|= GTMA_HAVE_LOAD
;
1573 if (requires_barrier (/*entry_block=*/NULL
, gimple_assign_lhs (stmt
), NULL
))
1574 *state
|= GTMA_HAVE_STORE
;
1577 /* Mark a GIMPLE_CALL as appropriate for being inside a transaction. */
1580 examine_call_tm (unsigned *state
, gimple_stmt_iterator
*gsi
)
1582 gimple stmt
= gsi_stmt (*gsi
);
1585 if (is_tm_pure_call (stmt
))
1588 /* Check if this call is a transaction abort. */
1589 fn
= gimple_call_fndecl (stmt
);
1590 if (is_tm_abort (fn
))
1591 *state
|= GTMA_HAVE_ABORT
;
1593 /* Note that something may happen. */
1594 *state
|= GTMA_HAVE_LOAD
| GTMA_HAVE_STORE
;
1597 /* Lower a GIMPLE_TRANSACTION statement. */
1600 lower_transaction (gimple_stmt_iterator
*gsi
, struct walk_stmt_info
*wi
)
1603 gimple_transaction stmt
= as_a
<gimple_transaction
> (gsi_stmt (*gsi
));
1604 unsigned int *outer_state
= (unsigned int *) wi
->info
;
1605 unsigned int this_state
= 0;
1606 struct walk_stmt_info this_wi
;
1608 /* First, lower the body. The scanning that we do inside gives
1609 us some idea of what we're dealing with. */
1610 memset (&this_wi
, 0, sizeof (this_wi
));
1611 this_wi
.info
= (void *) &this_state
;
1612 walk_gimple_seq_mod (gimple_transaction_body_ptr (stmt
),
1613 lower_sequence_tm
, NULL
, &this_wi
);
1615 /* If there was absolutely nothing transaction related inside the
1616 transaction, we may elide it. Likewise if this is a nested
1617 transaction and does not contain an abort. */
1619 || (!(this_state
& GTMA_HAVE_ABORT
) && outer_state
!= NULL
))
1622 *outer_state
|= this_state
;
1624 gsi_insert_seq_before (gsi
, gimple_transaction_body (stmt
),
1626 gimple_transaction_set_body (stmt
, NULL
);
1628 gsi_remove (gsi
, true);
1629 wi
->removed_stmt
= true;
1633 /* Wrap the body of the transaction in a try-finally node so that
1634 the commit call is always properly called. */
1635 g
= gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_COMMIT
), 0);
1636 if (flag_exceptions
)
1639 gimple_seq n_seq
, e_seq
;
1641 n_seq
= gimple_seq_alloc_with_stmt (g
);
1644 g
= gimple_build_call (builtin_decl_explicit (BUILT_IN_EH_POINTER
),
1645 1, integer_zero_node
);
1646 ptr
= create_tmp_var (ptr_type_node
, NULL
);
1647 gimple_call_set_lhs (g
, ptr
);
1648 gimple_seq_add_stmt (&e_seq
, g
);
1650 g
= gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_COMMIT_EH
),
1652 gimple_seq_add_stmt (&e_seq
, g
);
1654 g
= gimple_build_eh_else (n_seq
, e_seq
);
1657 g
= gimple_build_try (gimple_transaction_body (stmt
),
1658 gimple_seq_alloc_with_stmt (g
), GIMPLE_TRY_FINALLY
);
1659 gsi_insert_after (gsi
, g
, GSI_CONTINUE_LINKING
);
1661 gimple_transaction_set_body (stmt
, NULL
);
1663 /* If the transaction calls abort or if this is an outer transaction,
1664 add an "over" label afterwards. */
1665 if ((this_state
& (GTMA_HAVE_ABORT
))
1666 || (gimple_transaction_subcode (stmt
) & GTMA_IS_OUTER
))
1668 tree label
= create_artificial_label (UNKNOWN_LOCATION
);
1669 gimple_transaction_set_label (stmt
, label
);
1670 gsi_insert_after (gsi
, gimple_build_label (label
), GSI_CONTINUE_LINKING
);
1673 /* Record the set of operations found for use later. */
1674 this_state
|= gimple_transaction_subcode (stmt
) & GTMA_DECLARATION_MASK
;
1675 gimple_transaction_set_subcode (stmt
, this_state
);
1678 /* Iterate through the statements in the sequence, lowering them all
1679 as appropriate for being in a transaction. */
1682 lower_sequence_tm (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1683 struct walk_stmt_info
*wi
)
1685 unsigned int *state
= (unsigned int *) wi
->info
;
1686 gimple stmt
= gsi_stmt (*gsi
);
1688 *handled_ops_p
= true;
1689 switch (gimple_code (stmt
))
1692 /* Only memory reads/writes need to be instrumented. */
1693 if (gimple_assign_single_p (stmt
))
1694 examine_assign_tm (state
, gsi
);
1698 examine_call_tm (state
, gsi
);
1702 *state
|= GTMA_MAY_ENTER_IRREVOCABLE
;
1705 case GIMPLE_TRANSACTION
:
1706 lower_transaction (gsi
, wi
);
1710 *handled_ops_p
= !gimple_has_substatements (stmt
);
1717 /* Iterate through the statements in the sequence, lowering them all
1718 as appropriate for being outside of a transaction. */
1721 lower_sequence_no_tm (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1722 struct walk_stmt_info
* wi
)
1724 gimple stmt
= gsi_stmt (*gsi
);
1726 if (gimple_code (stmt
) == GIMPLE_TRANSACTION
)
1728 *handled_ops_p
= true;
1729 lower_transaction (gsi
, wi
);
1732 *handled_ops_p
= !gimple_has_substatements (stmt
);
1737 /* Main entry point for flattening GIMPLE_TRANSACTION constructs. After
1738 this, GIMPLE_TRANSACTION nodes still exist, but the nested body has
1739 been moved out, and all the data required for constructing a proper
1740 CFG has been recorded. */
1743 execute_lower_tm (void)
1745 struct walk_stmt_info wi
;
1748 /* Transactional clones aren't created until a later pass. */
1749 gcc_assert (!decl_is_tm_clone (current_function_decl
));
1751 body
= gimple_body (current_function_decl
);
1752 memset (&wi
, 0, sizeof (wi
));
1753 walk_gimple_seq_mod (&body
, lower_sequence_no_tm
, NULL
, &wi
);
1754 gimple_set_body (current_function_decl
, body
);
1761 const pass_data pass_data_lower_tm
=
1763 GIMPLE_PASS
, /* type */
1764 "tmlower", /* name */
1765 OPTGROUP_NONE
, /* optinfo_flags */
1766 TV_TRANS_MEM
, /* tv_id */
1767 PROP_gimple_lcf
, /* properties_required */
1768 0, /* properties_provided */
1769 0, /* properties_destroyed */
1770 0, /* todo_flags_start */
1771 0, /* todo_flags_finish */
1774 class pass_lower_tm
: public gimple_opt_pass
1777 pass_lower_tm (gcc::context
*ctxt
)
1778 : gimple_opt_pass (pass_data_lower_tm
, ctxt
)
1781 /* opt_pass methods: */
1782 virtual bool gate (function
*) { return flag_tm
; }
1783 virtual unsigned int execute (function
*) { return execute_lower_tm (); }
1785 }; // class pass_lower_tm
1790 make_pass_lower_tm (gcc::context
*ctxt
)
1792 return new pass_lower_tm (ctxt
);
1795 /* Collect region information for each transaction. */
1801 /* The field "transaction_stmt" is initially a gimple_transaction,
1802 but eventually gets lowered to a gimple_call (to BUILT_IN_TM_START).
1804 Helper method to get it as a gimple_transaction, with code-checking
1805 in a checked-build. */
1808 get_transaction_stmt () const
1810 return as_a
<gimple_transaction
> (transaction_stmt
);
1815 /* Link to the next unnested transaction. */
1816 struct tm_region
*next
;
1818 /* Link to the next inner transaction. */
1819 struct tm_region
*inner
;
1821 /* Link to the next outer transaction. */
1822 struct tm_region
*outer
;
1824 /* The GIMPLE_TRANSACTION statement beginning this transaction.
1825 After TM_MARK, this gets replaced by a call to
1827 Hence this will be either a gimple_transaction or a gimple_call. */
1828 gimple transaction_stmt
;
1830 /* After TM_MARK expands the GIMPLE_TRANSACTION into a call to
1831 BUILT_IN_TM_START, this field is true if the transaction is an
1832 outer transaction. */
1833 bool original_transaction_was_outer
;
1835 /* Return value from BUILT_IN_TM_START. */
1838 /* The entry block to this region. This will always be the first
1839 block of the body of the transaction. */
1840 basic_block entry_block
;
1842 /* The first block after an expanded call to _ITM_beginTransaction. */
1843 basic_block restart_block
;
1845 /* The set of all blocks that end the region; NULL if only EXIT_BLOCK.
1846 These blocks are still a part of the region (i.e., the border is
1847 inclusive). Note that this set is only complete for paths in the CFG
1848 starting at ENTRY_BLOCK, and that there is no exit block recorded for
1849 the edge to the "over" label. */
1852 /* The set of all blocks that have an TM_IRREVOCABLE call. */
1856 typedef struct tm_region
*tm_region_p
;
1858 /* True if there are pending edge statements to be committed for the
1859 current function being scanned in the tmmark pass. */
1860 bool pending_edge_inserts_p
;
1862 static struct tm_region
*all_tm_regions
;
1863 static bitmap_obstack tm_obstack
;
1866 /* A subroutine of tm_region_init. Record the existence of the
1867 GIMPLE_TRANSACTION statement in a tree of tm_region elements. */
1869 static struct tm_region
*
1870 tm_region_init_0 (struct tm_region
*outer
, basic_block bb
,
1871 gimple_transaction stmt
)
1873 struct tm_region
*region
;
1875 region
= (struct tm_region
*)
1876 obstack_alloc (&tm_obstack
.obstack
, sizeof (struct tm_region
));
1880 region
->next
= outer
->inner
;
1881 outer
->inner
= region
;
1885 region
->next
= all_tm_regions
;
1886 all_tm_regions
= region
;
1888 region
->inner
= NULL
;
1889 region
->outer
= outer
;
1891 region
->transaction_stmt
= stmt
;
1892 region
->original_transaction_was_outer
= false;
1893 region
->tm_state
= NULL
;
1895 /* There are either one or two edges out of the block containing
1896 the GIMPLE_TRANSACTION, one to the actual region and one to the
1897 "over" label if the region contains an abort. The former will
1898 always be the one marked FALLTHRU. */
1899 region
->entry_block
= FALLTHRU_EDGE (bb
)->dest
;
1901 region
->exit_blocks
= BITMAP_ALLOC (&tm_obstack
);
1902 region
->irr_blocks
= BITMAP_ALLOC (&tm_obstack
);
1907 /* A subroutine of tm_region_init. Record all the exit and
1908 irrevocable blocks in BB into the region's exit_blocks and
1909 irr_blocks bitmaps. Returns the new region being scanned. */
1911 static struct tm_region
*
1912 tm_region_init_1 (struct tm_region
*region
, basic_block bb
)
1914 gimple_stmt_iterator gsi
;
1918 || (!region
->irr_blocks
&& !region
->exit_blocks
))
1921 /* Check to see if this is the end of a region by seeing if it
1922 contains a call to __builtin_tm_commit{,_eh}. Note that the
1923 outermost region for DECL_IS_TM_CLONE need not collect this. */
1924 for (gsi
= gsi_last_bb (bb
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
1927 if (gimple_code (g
) == GIMPLE_CALL
)
1929 tree fn
= gimple_call_fndecl (g
);
1930 if (fn
&& DECL_BUILT_IN_CLASS (fn
) == BUILT_IN_NORMAL
)
1932 if ((DECL_FUNCTION_CODE (fn
) == BUILT_IN_TM_COMMIT
1933 || DECL_FUNCTION_CODE (fn
) == BUILT_IN_TM_COMMIT_EH
)
1934 && region
->exit_blocks
)
1936 bitmap_set_bit (region
->exit_blocks
, bb
->index
);
1937 region
= region
->outer
;
1940 if (DECL_FUNCTION_CODE (fn
) == BUILT_IN_TM_IRREVOCABLE
)
1941 bitmap_set_bit (region
->irr_blocks
, bb
->index
);
1948 /* Collect all of the transaction regions within the current function
1949 and record them in ALL_TM_REGIONS. The REGION parameter may specify
1950 an "outermost" region for use by tm clones. */
1953 tm_region_init (struct tm_region
*region
)
1959 auto_vec
<basic_block
> queue
;
1960 bitmap visited_blocks
= BITMAP_ALLOC (NULL
);
1961 struct tm_region
*old_region
;
1962 auto_vec
<tm_region_p
> bb_regions
;
1964 all_tm_regions
= region
;
1965 bb
= single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
1967 /* We could store this information in bb->aux, but we may get called
1968 through get_all_tm_blocks() from another pass that may be already
1970 bb_regions
.safe_grow_cleared (last_basic_block_for_fn (cfun
));
1972 queue
.safe_push (bb
);
1973 bb_regions
[bb
->index
] = region
;
1977 region
= bb_regions
[bb
->index
];
1978 bb_regions
[bb
->index
] = NULL
;
1980 /* Record exit and irrevocable blocks. */
1981 region
= tm_region_init_1 (region
, bb
);
1983 /* Check for the last statement in the block beginning a new region. */
1985 old_region
= region
;
1987 if (gimple_transaction trans_stmt
= dyn_cast
<gimple_transaction
> (g
))
1988 region
= tm_region_init_0 (region
, bb
, trans_stmt
);
1990 /* Process subsequent blocks. */
1991 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1992 if (!bitmap_bit_p (visited_blocks
, e
->dest
->index
))
1994 bitmap_set_bit (visited_blocks
, e
->dest
->index
);
1995 queue
.safe_push (e
->dest
);
1997 /* If the current block started a new region, make sure that only
1998 the entry block of the new region is associated with this region.
1999 Other successors are still part of the old region. */
2000 if (old_region
!= region
&& e
->dest
!= region
->entry_block
)
2001 bb_regions
[e
->dest
->index
] = old_region
;
2003 bb_regions
[e
->dest
->index
] = region
;
2006 while (!queue
.is_empty ());
2007 BITMAP_FREE (visited_blocks
);
2010 /* The "gate" function for all transactional memory expansion and optimization
2011 passes. We collect region information for each top-level transaction, and
2012 if we don't find any, we skip all of the TM passes. Each region will have
2013 all of the exit blocks recorded, and the originating statement. */
2021 calculate_dominance_info (CDI_DOMINATORS
);
2022 bitmap_obstack_initialize (&tm_obstack
);
2024 /* If the function is a TM_CLONE, then the entire function is the region. */
2025 if (decl_is_tm_clone (current_function_decl
))
2027 struct tm_region
*region
= (struct tm_region
*)
2028 obstack_alloc (&tm_obstack
.obstack
, sizeof (struct tm_region
));
2029 memset (region
, 0, sizeof (*region
));
2030 region
->entry_block
= single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
2031 /* For a clone, the entire function is the region. But even if
2032 we don't need to record any exit blocks, we may need to
2033 record irrevocable blocks. */
2034 region
->irr_blocks
= BITMAP_ALLOC (&tm_obstack
);
2036 tm_region_init (region
);
2040 tm_region_init (NULL
);
2042 /* If we didn't find any regions, cleanup and skip the whole tree
2043 of tm-related optimizations. */
2044 if (all_tm_regions
== NULL
)
2046 bitmap_obstack_release (&tm_obstack
);
2056 const pass_data pass_data_tm_init
=
2058 GIMPLE_PASS
, /* type */
2059 "*tminit", /* name */
2060 OPTGROUP_NONE
, /* optinfo_flags */
2061 TV_TRANS_MEM
, /* tv_id */
2062 ( PROP_ssa
| PROP_cfg
), /* properties_required */
2063 0, /* properties_provided */
2064 0, /* properties_destroyed */
2065 0, /* todo_flags_start */
2066 0, /* todo_flags_finish */
2069 class pass_tm_init
: public gimple_opt_pass
2072 pass_tm_init (gcc::context
*ctxt
)
2073 : gimple_opt_pass (pass_data_tm_init
, ctxt
)
2076 /* opt_pass methods: */
2077 virtual bool gate (function
*) { return gate_tm_init (); }
2079 }; // class pass_tm_init
2084 make_pass_tm_init (gcc::context
*ctxt
)
2086 return new pass_tm_init (ctxt
);
2089 /* Add FLAGS to the GIMPLE_TRANSACTION subcode for the transaction region
2090 represented by STATE. */
2093 transaction_subcode_ior (struct tm_region
*region
, unsigned flags
)
2095 if (region
&& region
->transaction_stmt
)
2097 gimple_transaction transaction_stmt
= region
->get_transaction_stmt ();
2098 flags
|= gimple_transaction_subcode (transaction_stmt
);
2099 gimple_transaction_set_subcode (transaction_stmt
, flags
);
2103 /* Construct a memory load in a transactional context. Return the
2104 gimple statement performing the load, or NULL if there is no
2105 TM_LOAD builtin of the appropriate size to do the load.
2107 LOC is the location to use for the new statement(s). */
2110 build_tm_load (location_t loc
, tree lhs
, tree rhs
, gimple_stmt_iterator
*gsi
)
2112 enum built_in_function code
= END_BUILTINS
;
2113 tree t
, type
= TREE_TYPE (rhs
), decl
;
2116 if (type
== float_type_node
)
2117 code
= BUILT_IN_TM_LOAD_FLOAT
;
2118 else if (type
== double_type_node
)
2119 code
= BUILT_IN_TM_LOAD_DOUBLE
;
2120 else if (type
== long_double_type_node
)
2121 code
= BUILT_IN_TM_LOAD_LDOUBLE
;
2122 else if (TYPE_SIZE_UNIT (type
) != NULL
2123 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type
)))
2125 switch (tree_to_uhwi (TYPE_SIZE_UNIT (type
)))
2128 code
= BUILT_IN_TM_LOAD_1
;
2131 code
= BUILT_IN_TM_LOAD_2
;
2134 code
= BUILT_IN_TM_LOAD_4
;
2137 code
= BUILT_IN_TM_LOAD_8
;
2142 if (code
== END_BUILTINS
)
2144 decl
= targetm
.vectorize
.builtin_tm_load (type
);
2149 decl
= builtin_decl_explicit (code
);
2151 t
= gimplify_addr (gsi
, rhs
);
2152 gcall
= gimple_build_call (decl
, 1, t
);
2153 gimple_set_location (gcall
, loc
);
2155 t
= TREE_TYPE (TREE_TYPE (decl
));
2156 if (useless_type_conversion_p (type
, t
))
2158 gimple_call_set_lhs (gcall
, lhs
);
2159 gsi_insert_before (gsi
, gcall
, GSI_SAME_STMT
);
2166 temp
= create_tmp_reg (t
, NULL
);
2167 gimple_call_set_lhs (gcall
, temp
);
2168 gsi_insert_before (gsi
, gcall
, GSI_SAME_STMT
);
2170 t
= fold_build1 (VIEW_CONVERT_EXPR
, type
, temp
);
2171 g
= gimple_build_assign (lhs
, t
);
2172 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
2179 /* Similarly for storing TYPE in a transactional context. */
2182 build_tm_store (location_t loc
, tree lhs
, tree rhs
, gimple_stmt_iterator
*gsi
)
2184 enum built_in_function code
= END_BUILTINS
;
2185 tree t
, fn
, type
= TREE_TYPE (rhs
), simple_type
;
2188 if (type
== float_type_node
)
2189 code
= BUILT_IN_TM_STORE_FLOAT
;
2190 else if (type
== double_type_node
)
2191 code
= BUILT_IN_TM_STORE_DOUBLE
;
2192 else if (type
== long_double_type_node
)
2193 code
= BUILT_IN_TM_STORE_LDOUBLE
;
2194 else if (TYPE_SIZE_UNIT (type
) != NULL
2195 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type
)))
2197 switch (tree_to_uhwi (TYPE_SIZE_UNIT (type
)))
2200 code
= BUILT_IN_TM_STORE_1
;
2203 code
= BUILT_IN_TM_STORE_2
;
2206 code
= BUILT_IN_TM_STORE_4
;
2209 code
= BUILT_IN_TM_STORE_8
;
2214 if (code
== END_BUILTINS
)
2216 fn
= targetm
.vectorize
.builtin_tm_store (type
);
2221 fn
= builtin_decl_explicit (code
);
2223 simple_type
= TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn
))));
2225 if (TREE_CODE (rhs
) == CONSTRUCTOR
)
2227 /* Handle the easy initialization to zero. */
2228 if (!CONSTRUCTOR_ELTS (rhs
))
2229 rhs
= build_int_cst (simple_type
, 0);
2232 /* ...otherwise punt to the caller and probably use
2233 BUILT_IN_TM_MEMMOVE, because we can't wrap a
2234 VIEW_CONVERT_EXPR around a CONSTRUCTOR (below) and produce
2239 else if (!useless_type_conversion_p (simple_type
, type
))
2244 temp
= create_tmp_reg (simple_type
, NULL
);
2245 t
= fold_build1 (VIEW_CONVERT_EXPR
, simple_type
, rhs
);
2246 g
= gimple_build_assign (temp
, t
);
2247 gimple_set_location (g
, loc
);
2248 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
2253 t
= gimplify_addr (gsi
, lhs
);
2254 gcall
= gimple_build_call (fn
, 2, t
, rhs
);
2255 gimple_set_location (gcall
, loc
);
2256 gsi_insert_before (gsi
, gcall
, GSI_SAME_STMT
);
2262 /* Expand an assignment statement into transactional builtins. */
2265 expand_assign_tm (struct tm_region
*region
, gimple_stmt_iterator
*gsi
)
2267 gimple stmt
= gsi_stmt (*gsi
);
2268 location_t loc
= gimple_location (stmt
);
2269 tree lhs
= gimple_assign_lhs (stmt
);
2270 tree rhs
= gimple_assign_rhs1 (stmt
);
2271 bool store_p
= requires_barrier (region
->entry_block
, lhs
, NULL
);
2272 bool load_p
= requires_barrier (region
->entry_block
, rhs
, NULL
);
2273 gimple gcall
= NULL
;
2275 if (!load_p
&& !store_p
)
2277 /* Add thread private addresses to log if applicable. */
2278 requires_barrier (region
->entry_block
, lhs
, stmt
);
2283 // Remove original load/store statement.
2284 gsi_remove (gsi
, true);
2286 if (load_p
&& !store_p
)
2288 transaction_subcode_ior (region
, GTMA_HAVE_LOAD
);
2289 gcall
= build_tm_load (loc
, lhs
, rhs
, gsi
);
2291 else if (store_p
&& !load_p
)
2293 transaction_subcode_ior (region
, GTMA_HAVE_STORE
);
2294 gcall
= build_tm_store (loc
, lhs
, rhs
, gsi
);
2298 tree lhs_addr
, rhs_addr
, tmp
;
2301 transaction_subcode_ior (region
, GTMA_HAVE_LOAD
);
2303 transaction_subcode_ior (region
, GTMA_HAVE_STORE
);
2305 /* ??? Figure out if there's any possible overlap between the LHS
2306 and the RHS and if not, use MEMCPY. */
2308 if (load_p
&& is_gimple_reg (lhs
))
2310 tmp
= create_tmp_var (TREE_TYPE (lhs
), NULL
);
2311 lhs_addr
= build_fold_addr_expr (tmp
);
2316 lhs_addr
= gimplify_addr (gsi
, lhs
);
2318 rhs_addr
= gimplify_addr (gsi
, rhs
);
2319 gcall
= gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_MEMMOVE
),
2320 3, lhs_addr
, rhs_addr
,
2321 TYPE_SIZE_UNIT (TREE_TYPE (lhs
)));
2322 gimple_set_location (gcall
, loc
);
2323 gsi_insert_before (gsi
, gcall
, GSI_SAME_STMT
);
2327 gcall
= gimple_build_assign (lhs
, tmp
);
2328 gsi_insert_before (gsi
, gcall
, GSI_SAME_STMT
);
2332 /* Now that we have the load/store in its instrumented form, add
2333 thread private addresses to the log if applicable. */
2335 requires_barrier (region
->entry_block
, lhs
, gcall
);
2337 // The calls to build_tm_{store,load} above inserted the instrumented
2338 // call into the stream.
2339 // gsi_insert_before (gsi, gcall, GSI_SAME_STMT);
2343 /* Expand a call statement as appropriate for a transaction. That is,
2344 either verify that the call does not affect the transaction, or
2345 redirect the call to a clone that handles transactions, or change
2346 the transaction state to IRREVOCABLE. Return true if the call is
2347 one of the builtins that end a transaction. */
2350 expand_call_tm (struct tm_region
*region
,
2351 gimple_stmt_iterator
*gsi
)
2353 gimple stmt
= gsi_stmt (*gsi
);
2354 tree lhs
= gimple_call_lhs (stmt
);
2356 struct cgraph_node
*node
;
2357 bool retval
= false;
2359 fn_decl
= gimple_call_fndecl (stmt
);
2361 if (fn_decl
== builtin_decl_explicit (BUILT_IN_TM_MEMCPY
)
2362 || fn_decl
== builtin_decl_explicit (BUILT_IN_TM_MEMMOVE
))
2363 transaction_subcode_ior (region
, GTMA_HAVE_STORE
| GTMA_HAVE_LOAD
);
2364 if (fn_decl
== builtin_decl_explicit (BUILT_IN_TM_MEMSET
))
2365 transaction_subcode_ior (region
, GTMA_HAVE_STORE
);
2367 if (is_tm_pure_call (stmt
))
2371 retval
= is_tm_ending_fndecl (fn_decl
);
2374 /* Assume all non-const/pure calls write to memory, except
2375 transaction ending builtins. */
2376 transaction_subcode_ior (region
, GTMA_HAVE_STORE
);
2379 /* For indirect calls, we already generated a call into the runtime. */
2382 tree fn
= gimple_call_fn (stmt
);
2384 /* We are guaranteed never to go irrevocable on a safe or pure
2385 call, and the pure call was handled above. */
2386 if (is_tm_safe (fn
))
2389 transaction_subcode_ior (region
, GTMA_MAY_ENTER_IRREVOCABLE
);
2394 node
= cgraph_node::get (fn_decl
);
2395 /* All calls should have cgraph here. */
2398 /* We can have a nodeless call here if some pass after IPA-tm
2399 added uninstrumented calls. For example, loop distribution
2400 can transform certain loop constructs into __builtin_mem*
2401 calls. In this case, see if we have a suitable TM
2402 replacement and fill in the gaps. */
2403 gcc_assert (DECL_BUILT_IN_CLASS (fn_decl
) == BUILT_IN_NORMAL
);
2404 enum built_in_function code
= DECL_FUNCTION_CODE (fn_decl
);
2405 gcc_assert (code
== BUILT_IN_MEMCPY
2406 || code
== BUILT_IN_MEMMOVE
2407 || code
== BUILT_IN_MEMSET
);
2409 tree repl
= find_tm_replacement_function (fn_decl
);
2412 gimple_call_set_fndecl (stmt
, repl
);
2414 node
= cgraph_node::create (repl
);
2415 node
->local
.tm_may_enter_irr
= false;
2416 return expand_call_tm (region
, gsi
);
2420 if (node
->local
.tm_may_enter_irr
)
2421 transaction_subcode_ior (region
, GTMA_MAY_ENTER_IRREVOCABLE
);
2423 if (is_tm_abort (fn_decl
))
2425 transaction_subcode_ior (region
, GTMA_HAVE_ABORT
);
2429 /* Instrument the store if needed.
2431 If the assignment happens inside the function call (return slot
2432 optimization), there is no instrumentation to be done, since
2433 the callee should have done the right thing. */
2434 if (lhs
&& requires_barrier (region
->entry_block
, lhs
, stmt
)
2435 && !gimple_call_return_slot_opt_p (stmt
))
2437 tree tmp
= create_tmp_reg (TREE_TYPE (lhs
), NULL
);
2438 location_t loc
= gimple_location (stmt
);
2439 edge fallthru_edge
= NULL
;
2441 /* Remember if the call was going to throw. */
2442 if (stmt_can_throw_internal (stmt
))
2446 basic_block bb
= gimple_bb (stmt
);
2448 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2449 if (e
->flags
& EDGE_FALLTHRU
)
2456 gimple_call_set_lhs (stmt
, tmp
);
2458 stmt
= gimple_build_assign (lhs
, tmp
);
2459 gimple_set_location (stmt
, loc
);
2461 /* We cannot throw in the middle of a BB. If the call was going
2462 to throw, place the instrumentation on the fallthru edge, so
2463 the call remains the last statement in the block. */
2466 gimple_seq fallthru_seq
= gimple_seq_alloc_with_stmt (stmt
);
2467 gimple_stmt_iterator fallthru_gsi
= gsi_start (fallthru_seq
);
2468 expand_assign_tm (region
, &fallthru_gsi
);
2469 gsi_insert_seq_on_edge (fallthru_edge
, fallthru_seq
);
2470 pending_edge_inserts_p
= true;
2474 gsi_insert_after (gsi
, stmt
, GSI_CONTINUE_LINKING
);
2475 expand_assign_tm (region
, gsi
);
2478 transaction_subcode_ior (region
, GTMA_HAVE_STORE
);
2485 /* Expand all statements in BB as appropriate for being inside
2489 expand_block_tm (struct tm_region
*region
, basic_block bb
)
2491 gimple_stmt_iterator gsi
;
2493 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); )
2495 gimple stmt
= gsi_stmt (gsi
);
2496 switch (gimple_code (stmt
))
2499 /* Only memory reads/writes need to be instrumented. */
2500 if (gimple_assign_single_p (stmt
)
2501 && !gimple_clobber_p (stmt
))
2503 expand_assign_tm (region
, &gsi
);
2509 if (expand_call_tm (region
, &gsi
))
2519 if (!gsi_end_p (gsi
))
2524 /* Return the list of basic-blocks in REGION.
2526 STOP_AT_IRREVOCABLE_P is true if caller is uninterested in blocks
2527 following a TM_IRREVOCABLE call.
2529 INCLUDE_UNINSTRUMENTED_P is TRUE if we should include the
2530 uninstrumented code path blocks in the list of basic blocks
2531 returned, false otherwise. */
2533 static vec
<basic_block
>
2534 get_tm_region_blocks (basic_block entry_block
,
2537 bitmap all_region_blocks
,
2538 bool stop_at_irrevocable_p
,
2539 bool include_uninstrumented_p
= true)
2541 vec
<basic_block
> bbs
= vNULL
;
2545 bitmap visited_blocks
= BITMAP_ALLOC (NULL
);
2548 bbs
.safe_push (entry_block
);
2549 bitmap_set_bit (visited_blocks
, entry_block
->index
);
2553 basic_block bb
= bbs
[i
++];
2556 bitmap_bit_p (exit_blocks
, bb
->index
))
2559 if (stop_at_irrevocable_p
2561 && bitmap_bit_p (irr_blocks
, bb
->index
))
2564 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2565 if ((include_uninstrumented_p
2566 || !(e
->flags
& EDGE_TM_UNINSTRUMENTED
))
2567 && !bitmap_bit_p (visited_blocks
, e
->dest
->index
))
2569 bitmap_set_bit (visited_blocks
, e
->dest
->index
);
2570 bbs
.safe_push (e
->dest
);
2573 while (i
< bbs
.length ());
2575 if (all_region_blocks
)
2576 bitmap_ior_into (all_region_blocks
, visited_blocks
);
2578 BITMAP_FREE (visited_blocks
);
2582 // Callback data for collect_bb2reg.
2585 vec
<tm_region_p
> *bb2reg
;
2586 bool include_uninstrumented_p
;
2589 // Callback for expand_regions, collect innermost region data for each bb.
2591 collect_bb2reg (struct tm_region
*region
, void *data
)
2593 struct bb2reg_stuff
*stuff
= (struct bb2reg_stuff
*)data
;
2594 vec
<tm_region_p
> *bb2reg
= stuff
->bb2reg
;
2595 vec
<basic_block
> queue
;
2599 queue
= get_tm_region_blocks (region
->entry_block
,
2600 region
->exit_blocks
,
2603 /*stop_at_irr_p=*/true,
2604 stuff
->include_uninstrumented_p
);
2606 // We expect expand_region to perform a post-order traversal of the region
2607 // tree. Therefore the last region seen for any bb is the innermost.
2608 FOR_EACH_VEC_ELT (queue
, i
, bb
)
2609 (*bb2reg
)[bb
->index
] = region
;
2615 // Returns a vector, indexed by BB->INDEX, of the innermost tm_region to
2616 // which a basic block belongs. Note that we only consider the instrumented
2617 // code paths for the region; the uninstrumented code paths are ignored if
2618 // INCLUDE_UNINSTRUMENTED_P is false.
2620 // ??? This data is very similar to the bb_regions array that is collected
2621 // during tm_region_init. Or, rather, this data is similar to what could
2622 // be used within tm_region_init. The actual computation in tm_region_init
2623 // begins and ends with bb_regions entirely full of NULL pointers, due to
2624 // the way in which pointers are swapped in and out of the array.
2626 // ??? Our callers expect that blocks are not shared between transactions.
2627 // When the optimizers get too smart, and blocks are shared, then during
2628 // the tm_mark phase we'll add log entries to only one of the two transactions,
2629 // and in the tm_edge phase we'll add edges to the CFG that create invalid
2630 // cycles. The symptom being SSA defs that do not dominate their uses.
2631 // Note that the optimizers were locally correct with their transformation,
2632 // as we have no info within the program that suggests that the blocks cannot
2635 // ??? There is currently a hack inside tree-ssa-pre.c to work around the
2636 // only known instance of this block sharing.
2638 static vec
<tm_region_p
>
2639 get_bb_regions_instrumented (bool traverse_clones
,
2640 bool include_uninstrumented_p
)
2642 unsigned n
= last_basic_block_for_fn (cfun
);
2643 struct bb2reg_stuff stuff
;
2644 vec
<tm_region_p
> ret
;
2647 ret
.safe_grow_cleared (n
);
2648 stuff
.bb2reg
= &ret
;
2649 stuff
.include_uninstrumented_p
= include_uninstrumented_p
;
2650 expand_regions (all_tm_regions
, collect_bb2reg
, &stuff
, traverse_clones
);
2655 /* Set the IN_TRANSACTION for all gimple statements that appear in a
2659 compute_transaction_bits (void)
2661 struct tm_region
*region
;
2662 vec
<basic_block
> queue
;
2666 /* ?? Perhaps we need to abstract gate_tm_init further, because we
2667 certainly don't need it to calculate CDI_DOMINATOR info. */
2670 FOR_EACH_BB_FN (bb
, cfun
)
2671 bb
->flags
&= ~BB_IN_TRANSACTION
;
2673 for (region
= all_tm_regions
; region
; region
= region
->next
)
2675 queue
= get_tm_region_blocks (region
->entry_block
,
2676 region
->exit_blocks
,
2679 /*stop_at_irr_p=*/true);
2680 for (i
= 0; queue
.iterate (i
, &bb
); ++i
)
2681 bb
->flags
|= BB_IN_TRANSACTION
;
2686 bitmap_obstack_release (&tm_obstack
);
2689 /* Replace the GIMPLE_TRANSACTION in this region with the corresponding
2690 call to BUILT_IN_TM_START. */
2693 expand_transaction (struct tm_region
*region
, void *data ATTRIBUTE_UNUSED
)
2695 tree tm_start
= builtin_decl_explicit (BUILT_IN_TM_START
);
2696 basic_block transaction_bb
= gimple_bb (region
->transaction_stmt
);
2697 tree tm_state
= region
->tm_state
;
2698 tree tm_state_type
= TREE_TYPE (tm_state
);
2699 edge abort_edge
= NULL
;
2700 edge inst_edge
= NULL
;
2701 edge uninst_edge
= NULL
;
2702 edge fallthru_edge
= NULL
;
2704 // Identify the various successors of the transaction start.
2708 FOR_EACH_EDGE (e
, i
, transaction_bb
->succs
)
2710 if (e
->flags
& EDGE_TM_ABORT
)
2712 else if (e
->flags
& EDGE_TM_UNINSTRUMENTED
)
2716 if (e
->flags
& EDGE_FALLTHRU
)
2721 /* ??? There are plenty of bits here we're not computing. */
2723 int subcode
= gimple_transaction_subcode (region
->get_transaction_stmt ());
2725 if (subcode
& GTMA_DOES_GO_IRREVOCABLE
)
2726 flags
|= PR_DOESGOIRREVOCABLE
;
2727 if ((subcode
& GTMA_MAY_ENTER_IRREVOCABLE
) == 0)
2728 flags
|= PR_HASNOIRREVOCABLE
;
2729 /* If the transaction does not have an abort in lexical scope and is not
2730 marked as an outer transaction, then it will never abort. */
2731 if ((subcode
& GTMA_HAVE_ABORT
) == 0 && (subcode
& GTMA_IS_OUTER
) == 0)
2732 flags
|= PR_HASNOABORT
;
2733 if ((subcode
& GTMA_HAVE_STORE
) == 0)
2734 flags
|= PR_READONLY
;
2735 if (inst_edge
&& !(subcode
& GTMA_HAS_NO_INSTRUMENTATION
))
2736 flags
|= PR_INSTRUMENTEDCODE
;
2738 flags
|= PR_UNINSTRUMENTEDCODE
;
2739 if (subcode
& GTMA_IS_OUTER
)
2740 region
->original_transaction_was_outer
= true;
2741 tree t
= build_int_cst (tm_state_type
, flags
);
2742 gimple_call call
= gimple_build_call (tm_start
, 1, t
);
2743 gimple_call_set_lhs (call
, tm_state
);
2744 gimple_set_location (call
, gimple_location (region
->transaction_stmt
));
2746 // Replace the GIMPLE_TRANSACTION with the call to BUILT_IN_TM_START.
2747 gimple_stmt_iterator gsi
= gsi_last_bb (transaction_bb
);
2748 gcc_assert (gsi_stmt (gsi
) == region
->transaction_stmt
);
2749 gsi_insert_before (&gsi
, call
, GSI_SAME_STMT
);
2750 gsi_remove (&gsi
, true);
2751 region
->transaction_stmt
= call
;
2754 // Generate log saves.
2755 if (!tm_log_save_addresses
.is_empty ())
2756 tm_log_emit_saves (region
->entry_block
, transaction_bb
);
2758 // In the beginning, we've no tests to perform on transaction restart.
2759 // Note that after this point, transaction_bb becomes the "most recent
2760 // block containing tests for the transaction".
2761 region
->restart_block
= region
->entry_block
;
2763 // Generate log restores.
2764 if (!tm_log_save_addresses
.is_empty ())
2766 basic_block test_bb
= create_empty_bb (transaction_bb
);
2767 basic_block code_bb
= create_empty_bb (test_bb
);
2768 basic_block join_bb
= create_empty_bb (code_bb
);
2769 add_bb_to_loop (test_bb
, transaction_bb
->loop_father
);
2770 add_bb_to_loop (code_bb
, transaction_bb
->loop_father
);
2771 add_bb_to_loop (join_bb
, transaction_bb
->loop_father
);
2772 if (region
->restart_block
== region
->entry_block
)
2773 region
->restart_block
= test_bb
;
2775 tree t1
= create_tmp_reg (tm_state_type
, NULL
);
2776 tree t2
= build_int_cst (tm_state_type
, A_RESTORELIVEVARIABLES
);
2777 gimple stmt
= gimple_build_assign_with_ops (BIT_AND_EXPR
, t1
,
2779 gimple_stmt_iterator gsi
= gsi_last_bb (test_bb
);
2780 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2782 t2
= build_int_cst (tm_state_type
, 0);
2783 stmt
= gimple_build_cond (NE_EXPR
, t1
, t2
, NULL
, NULL
);
2784 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2786 tm_log_emit_restores (region
->entry_block
, code_bb
);
2788 edge ei
= make_edge (transaction_bb
, test_bb
, EDGE_FALLTHRU
);
2789 edge et
= make_edge (test_bb
, code_bb
, EDGE_TRUE_VALUE
);
2790 edge ef
= make_edge (test_bb
, join_bb
, EDGE_FALSE_VALUE
);
2791 redirect_edge_pred (fallthru_edge
, join_bb
);
2793 join_bb
->frequency
= test_bb
->frequency
= transaction_bb
->frequency
;
2794 join_bb
->count
= test_bb
->count
= transaction_bb
->count
;
2796 ei
->probability
= PROB_ALWAYS
;
2797 et
->probability
= PROB_LIKELY
;
2798 ef
->probability
= PROB_UNLIKELY
;
2799 et
->count
= apply_probability (test_bb
->count
, et
->probability
);
2800 ef
->count
= apply_probability (test_bb
->count
, ef
->probability
);
2802 code_bb
->count
= et
->count
;
2803 code_bb
->frequency
= EDGE_FREQUENCY (et
);
2805 transaction_bb
= join_bb
;
2808 // If we have an ABORT edge, create a test to perform the abort.
2811 basic_block test_bb
= create_empty_bb (transaction_bb
);
2812 add_bb_to_loop (test_bb
, transaction_bb
->loop_father
);
2813 if (region
->restart_block
== region
->entry_block
)
2814 region
->restart_block
= test_bb
;
2816 tree t1
= create_tmp_reg (tm_state_type
, NULL
);
2817 tree t2
= build_int_cst (tm_state_type
, A_ABORTTRANSACTION
);
2818 gimple stmt
= gimple_build_assign_with_ops (BIT_AND_EXPR
, t1
,
2820 gimple_stmt_iterator gsi
= gsi_last_bb (test_bb
);
2821 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2823 t2
= build_int_cst (tm_state_type
, 0);
2824 stmt
= gimple_build_cond (NE_EXPR
, t1
, t2
, NULL
, NULL
);
2825 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2827 edge ei
= make_edge (transaction_bb
, test_bb
, EDGE_FALLTHRU
);
2828 test_bb
->frequency
= transaction_bb
->frequency
;
2829 test_bb
->count
= transaction_bb
->count
;
2830 ei
->probability
= PROB_ALWAYS
;
2832 // Not abort edge. If both are live, chose one at random as we'll
2833 // we'll be fixing that up below.
2834 redirect_edge_pred (fallthru_edge
, test_bb
);
2835 fallthru_edge
->flags
= EDGE_FALSE_VALUE
;
2836 fallthru_edge
->probability
= PROB_VERY_LIKELY
;
2837 fallthru_edge
->count
2838 = apply_probability (test_bb
->count
, fallthru_edge
->probability
);
2841 redirect_edge_pred (abort_edge
, test_bb
);
2842 abort_edge
->flags
= EDGE_TRUE_VALUE
;
2843 abort_edge
->probability
= PROB_VERY_UNLIKELY
;
2845 = apply_probability (test_bb
->count
, abort_edge
->probability
);
2847 transaction_bb
= test_bb
;
2850 // If we have both instrumented and uninstrumented code paths, select one.
2851 if (inst_edge
&& uninst_edge
)
2853 basic_block test_bb
= create_empty_bb (transaction_bb
);
2854 add_bb_to_loop (test_bb
, transaction_bb
->loop_father
);
2855 if (region
->restart_block
== region
->entry_block
)
2856 region
->restart_block
= test_bb
;
2858 tree t1
= create_tmp_reg (tm_state_type
, NULL
);
2859 tree t2
= build_int_cst (tm_state_type
, A_RUNUNINSTRUMENTEDCODE
);
2861 gimple stmt
= gimple_build_assign_with_ops (BIT_AND_EXPR
, t1
,
2863 gimple_stmt_iterator gsi
= gsi_last_bb (test_bb
);
2864 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2866 t2
= build_int_cst (tm_state_type
, 0);
2867 stmt
= gimple_build_cond (NE_EXPR
, t1
, t2
, NULL
, NULL
);
2868 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2870 // Create the edge into test_bb first, as we want to copy values
2871 // out of the fallthru edge.
2872 edge e
= make_edge (transaction_bb
, test_bb
, fallthru_edge
->flags
);
2873 e
->probability
= fallthru_edge
->probability
;
2874 test_bb
->count
= e
->count
= fallthru_edge
->count
;
2875 test_bb
->frequency
= EDGE_FREQUENCY (e
);
2877 // Now update the edges to the inst/uninist implementations.
2878 // For now assume that the paths are equally likely. When using HTM,
2879 // we'll try the uninst path first and fallback to inst path if htm
2880 // buffers are exceeded. Without HTM we start with the inst path and
2881 // use the uninst path when falling back to serial mode.
2882 redirect_edge_pred (inst_edge
, test_bb
);
2883 inst_edge
->flags
= EDGE_FALSE_VALUE
;
2884 inst_edge
->probability
= REG_BR_PROB_BASE
/ 2;
2886 = apply_probability (test_bb
->count
, inst_edge
->probability
);
2888 redirect_edge_pred (uninst_edge
, test_bb
);
2889 uninst_edge
->flags
= EDGE_TRUE_VALUE
;
2890 uninst_edge
->probability
= REG_BR_PROB_BASE
/ 2;
2892 = apply_probability (test_bb
->count
, uninst_edge
->probability
);
2895 // If we have no previous special cases, and we have PHIs at the beginning
2896 // of the atomic region, this means we have a loop at the beginning of the
2897 // atomic region that shares the first block. This can cause problems with
2898 // the transaction restart abnormal edges to be added in the tm_edges pass.
2899 // Solve this by adding a new empty block to receive the abnormal edges.
2900 if (region
->restart_block
== region
->entry_block
2901 && phi_nodes (region
->entry_block
))
2903 basic_block empty_bb
= create_empty_bb (transaction_bb
);
2904 region
->restart_block
= empty_bb
;
2905 add_bb_to_loop (empty_bb
, transaction_bb
->loop_father
);
2907 redirect_edge_pred (fallthru_edge
, empty_bb
);
2908 make_edge (transaction_bb
, empty_bb
, EDGE_FALLTHRU
);
2914 /* Generate the temporary to be used for the return value of
2915 BUILT_IN_TM_START. */
2918 generate_tm_state (struct tm_region
*region
, void *data ATTRIBUTE_UNUSED
)
2920 tree tm_start
= builtin_decl_explicit (BUILT_IN_TM_START
);
2922 create_tmp_reg (TREE_TYPE (TREE_TYPE (tm_start
)), "tm_state");
2924 // Reset the subcode, post optimizations. We'll fill this in
2925 // again as we process blocks.
2926 if (region
->exit_blocks
)
2928 gimple_transaction transaction_stmt
= region
->get_transaction_stmt ();
2929 unsigned int subcode
= gimple_transaction_subcode (transaction_stmt
);
2931 if (subcode
& GTMA_DOES_GO_IRREVOCABLE
)
2932 subcode
&= (GTMA_DECLARATION_MASK
| GTMA_DOES_GO_IRREVOCABLE
2933 | GTMA_MAY_ENTER_IRREVOCABLE
2934 | GTMA_HAS_NO_INSTRUMENTATION
);
2936 subcode
&= GTMA_DECLARATION_MASK
;
2937 gimple_transaction_set_subcode (transaction_stmt
, subcode
);
2943 // Propagate flags from inner transactions outwards.
2945 propagate_tm_flags_out (struct tm_region
*region
)
2949 propagate_tm_flags_out (region
->inner
);
2951 if (region
->outer
&& region
->outer
->transaction_stmt
)
2954 gimple_transaction_subcode (region
->get_transaction_stmt ());
2955 s
&= (GTMA_HAVE_ABORT
| GTMA_HAVE_LOAD
| GTMA_HAVE_STORE
2956 | GTMA_MAY_ENTER_IRREVOCABLE
);
2957 s
|= gimple_transaction_subcode (region
->outer
->get_transaction_stmt ());
2958 gimple_transaction_set_subcode (region
->outer
->get_transaction_stmt (),
2962 propagate_tm_flags_out (region
->next
);
2965 /* Entry point to the MARK phase of TM expansion. Here we replace
2966 transactional memory statements with calls to builtins, and function
2967 calls with their transactional clones (if available). But we don't
2968 yet lower GIMPLE_TRANSACTION or add the transaction restart back-edges. */
2971 execute_tm_mark (void)
2973 pending_edge_inserts_p
= false;
2975 expand_regions (all_tm_regions
, generate_tm_state
, NULL
,
2976 /*traverse_clones=*/true);
2980 vec
<tm_region_p
> bb_regions
2981 = get_bb_regions_instrumented (/*traverse_clones=*/true,
2982 /*include_uninstrumented_p=*/false);
2983 struct tm_region
*r
;
2986 // Expand memory operations into calls into the runtime.
2987 // This collects log entries as well.
2988 FOR_EACH_VEC_ELT (bb_regions
, i
, r
)
2992 if (r
->transaction_stmt
)
2995 gimple_transaction_subcode (r
->get_transaction_stmt ());
2997 /* If we're sure to go irrevocable, there won't be
2998 anything to expand, since the run-time will go
2999 irrevocable right away. */
3000 if (sub
& GTMA_DOES_GO_IRREVOCABLE
3001 && sub
& GTMA_MAY_ENTER_IRREVOCABLE
)
3004 expand_block_tm (r
, BASIC_BLOCK_FOR_FN (cfun
, i
));
3008 bb_regions
.release ();
3010 // Propagate flags from inner transactions outwards.
3011 propagate_tm_flags_out (all_tm_regions
);
3013 // Expand GIMPLE_TRANSACTIONs into calls into the runtime.
3014 expand_regions (all_tm_regions
, expand_transaction
, NULL
,
3015 /*traverse_clones=*/false);
3020 if (pending_edge_inserts_p
)
3021 gsi_commit_edge_inserts ();
3022 free_dominance_info (CDI_DOMINATORS
);
3028 const pass_data pass_data_tm_mark
=
3030 GIMPLE_PASS
, /* type */
3031 "tmmark", /* name */
3032 OPTGROUP_NONE
, /* optinfo_flags */
3033 TV_TRANS_MEM
, /* tv_id */
3034 ( PROP_ssa
| PROP_cfg
), /* properties_required */
3035 0, /* properties_provided */
3036 0, /* properties_destroyed */
3037 0, /* todo_flags_start */
3038 TODO_update_ssa
, /* todo_flags_finish */
3041 class pass_tm_mark
: public gimple_opt_pass
3044 pass_tm_mark (gcc::context
*ctxt
)
3045 : gimple_opt_pass (pass_data_tm_mark
, ctxt
)
3048 /* opt_pass methods: */
3049 virtual unsigned int execute (function
*) { return execute_tm_mark (); }
3051 }; // class pass_tm_mark
3056 make_pass_tm_mark (gcc::context
*ctxt
)
3058 return new pass_tm_mark (ctxt
);
3062 /* Create an abnormal edge from STMT at iter, splitting the block
3063 as necessary. Adjust *PNEXT as needed for the split block. */
3066 split_bb_make_tm_edge (gimple stmt
, basic_block dest_bb
,
3067 gimple_stmt_iterator iter
, gimple_stmt_iterator
*pnext
)
3069 basic_block bb
= gimple_bb (stmt
);
3070 if (!gsi_one_before_end_p (iter
))
3072 edge e
= split_block (bb
, stmt
);
3073 *pnext
= gsi_start_bb (e
->dest
);
3075 make_edge (bb
, dest_bb
, EDGE_ABNORMAL
);
3077 // Record the need for the edge for the benefit of the rtl passes.
3078 if (cfun
->gimple_df
->tm_restart
== NULL
)
3079 cfun
->gimple_df
->tm_restart
= htab_create_ggc (31, struct_ptr_hash
,
3080 struct_ptr_eq
, ggc_free
);
3082 struct tm_restart_node dummy
;
3084 dummy
.label_or_list
= gimple_block_label (dest_bb
);
3086 void **slot
= htab_find_slot (cfun
->gimple_df
->tm_restart
, &dummy
, INSERT
);
3087 struct tm_restart_node
*n
= (struct tm_restart_node
*) *slot
;
3090 n
= ggc_alloc
<tm_restart_node
> ();
3095 tree old
= n
->label_or_list
;
3096 if (TREE_CODE (old
) == LABEL_DECL
)
3097 old
= tree_cons (NULL
, old
, NULL
);
3098 n
->label_or_list
= tree_cons (NULL
, dummy
.label_or_list
, old
);
3102 /* Split block BB as necessary for every builtin function we added, and
3103 wire up the abnormal back edges implied by the transaction restart. */
3106 expand_block_edges (struct tm_region
*const region
, basic_block bb
)
3108 gimple_stmt_iterator gsi
, next_gsi
;
3110 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi
= next_gsi
)
3112 gimple stmt
= gsi_stmt (gsi
);
3115 gsi_next (&next_gsi
);
3117 // ??? Shouldn't we split for any non-pure, non-irrevocable function?
3118 if (gimple_code (stmt
) != GIMPLE_CALL
3119 || (gimple_call_flags (stmt
) & ECF_TM_BUILTIN
) == 0)
3122 if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt
)) == BUILT_IN_TM_ABORT
)
3124 // If we have a ``_transaction_cancel [[outer]]'', there is only
3125 // one abnormal edge: to the transaction marked OUTER.
3126 // All compiler-generated instances of BUILT_IN_TM_ABORT have a
3127 // constant argument, which we can examine here. Users invoking
3128 // TM_ABORT directly get what they deserve.
3129 tree arg
= gimple_call_arg (stmt
, 0);
3130 if (TREE_CODE (arg
) == INTEGER_CST
3131 && (TREE_INT_CST_LOW (arg
) & AR_OUTERABORT
) != 0
3132 && !decl_is_tm_clone (current_function_decl
))
3134 // Find the GTMA_IS_OUTER transaction.
3135 for (struct tm_region
*o
= region
; o
; o
= o
->outer
)
3136 if (o
->original_transaction_was_outer
)
3138 split_bb_make_tm_edge (stmt
, o
->restart_block
,
3143 // Otherwise, the front-end should have semantically checked
3144 // outer aborts, but in either case the target region is not
3145 // within this function.
3149 // Non-outer, TM aborts have an abnormal edge to the inner-most
3150 // transaction, the one being aborted;
3151 split_bb_make_tm_edge (stmt
, region
->restart_block
, gsi
, &next_gsi
);
3154 // All TM builtins have an abnormal edge to the outer-most transaction.
3155 // We never restart inner transactions. For tm clones, we know a-priori
3156 // that the outer-most transaction is outside the function.
3157 if (decl_is_tm_clone (current_function_decl
))
3160 if (cfun
->gimple_df
->tm_restart
== NULL
)
3161 cfun
->gimple_df
->tm_restart
3162 = htab_create_ggc (31, struct_ptr_hash
, struct_ptr_eq
, ggc_free
);
3164 // All TM builtins have an abnormal edge to the outer-most transaction.
3165 // We never restart inner transactions.
3166 for (struct tm_region
*o
= region
; o
; o
= o
->outer
)
3169 split_bb_make_tm_edge (stmt
, o
->restart_block
, gsi
, &next_gsi
);
3173 // Delete any tail-call annotation that may have been added.
3174 // The tail-call pass may have mis-identified the commit as being
3175 // a candidate because we had not yet added this restart edge.
3176 gimple_call_set_tail (stmt
, false);
3180 /* Entry point to the final expansion of transactional nodes. */
3184 const pass_data pass_data_tm_edges
=
3186 GIMPLE_PASS
, /* type */
3187 "tmedge", /* name */
3188 OPTGROUP_NONE
, /* optinfo_flags */
3189 TV_TRANS_MEM
, /* tv_id */
3190 ( PROP_ssa
| PROP_cfg
), /* properties_required */
3191 0, /* properties_provided */
3192 0, /* properties_destroyed */
3193 0, /* todo_flags_start */
3194 TODO_update_ssa
, /* todo_flags_finish */
3197 class pass_tm_edges
: public gimple_opt_pass
3200 pass_tm_edges (gcc::context
*ctxt
)
3201 : gimple_opt_pass (pass_data_tm_edges
, ctxt
)
3204 /* opt_pass methods: */
3205 virtual unsigned int execute (function
*);
3207 }; // class pass_tm_edges
3210 pass_tm_edges::execute (function
*fun
)
3212 vec
<tm_region_p
> bb_regions
3213 = get_bb_regions_instrumented (/*traverse_clones=*/false,
3214 /*include_uninstrumented_p=*/true);
3215 struct tm_region
*r
;
3218 FOR_EACH_VEC_ELT (bb_regions
, i
, r
)
3220 expand_block_edges (r
, BASIC_BLOCK_FOR_FN (fun
, i
));
3222 bb_regions
.release ();
3224 /* We've got to release the dominance info now, to indicate that it
3225 must be rebuilt completely. Otherwise we'll crash trying to update
3226 the SSA web in the TODO section following this pass. */
3227 free_dominance_info (CDI_DOMINATORS
);
3228 bitmap_obstack_release (&tm_obstack
);
3229 all_tm_regions
= NULL
;
3237 make_pass_tm_edges (gcc::context
*ctxt
)
3239 return new pass_tm_edges (ctxt
);
3242 /* Helper function for expand_regions. Expand REGION and recurse to
3243 the inner region. Call CALLBACK on each region. CALLBACK returns
3244 NULL to continue the traversal, otherwise a non-null value which
3245 this function will return as well. TRAVERSE_CLONES is true if we
3246 should traverse transactional clones. */
3249 expand_regions_1 (struct tm_region
*region
,
3250 void *(*callback
)(struct tm_region
*, void *),
3252 bool traverse_clones
)
3254 void *retval
= NULL
;
3255 if (region
->exit_blocks
3256 || (traverse_clones
&& decl_is_tm_clone (current_function_decl
)))
3258 retval
= callback (region
, data
);
3264 retval
= expand_regions (region
->inner
, callback
, data
, traverse_clones
);
3271 /* Traverse the regions enclosed and including REGION. Execute
3272 CALLBACK for each region, passing DATA. CALLBACK returns NULL to
3273 continue the traversal, otherwise a non-null value which this
3274 function will return as well. TRAVERSE_CLONES is true if we should
3275 traverse transactional clones. */
3278 expand_regions (struct tm_region
*region
,
3279 void *(*callback
)(struct tm_region
*, void *),
3281 bool traverse_clones
)
3283 void *retval
= NULL
;
3286 retval
= expand_regions_1 (region
, callback
, data
, traverse_clones
);
3289 region
= region
->next
;
3295 /* A unique TM memory operation. */
3296 typedef struct tm_memop
3298 /* Unique ID that all memory operations to the same location have. */
3299 unsigned int value_id
;
3300 /* Address of load/store. */
3304 /* TM memory operation hashtable helpers. */
3306 struct tm_memop_hasher
: typed_free_remove
<tm_memop
>
3308 typedef tm_memop value_type
;
3309 typedef tm_memop compare_type
;
3310 static inline hashval_t
hash (const value_type
*);
3311 static inline bool equal (const value_type
*, const compare_type
*);
3314 /* Htab support. Return a hash value for a `tm_memop'. */
3316 tm_memop_hasher::hash (const value_type
*mem
)
3318 tree addr
= mem
->addr
;
3319 /* We drill down to the SSA_NAME/DECL for the hash, but equality is
3320 actually done with operand_equal_p (see tm_memop_eq). */
3321 if (TREE_CODE (addr
) == ADDR_EXPR
)
3322 addr
= TREE_OPERAND (addr
, 0);
3323 return iterative_hash_expr (addr
, 0);
3326 /* Htab support. Return true if two tm_memop's are the same. */
3328 tm_memop_hasher::equal (const value_type
*mem1
, const compare_type
*mem2
)
3330 return operand_equal_p (mem1
->addr
, mem2
->addr
, 0);
3333 /* Sets for solving data flow equations in the memory optimization pass. */
3334 struct tm_memopt_bitmaps
3336 /* Stores available to this BB upon entry. Basically, stores that
3337 dominate this BB. */
3338 bitmap store_avail_in
;
3339 /* Stores available at the end of this BB. */
3340 bitmap store_avail_out
;
3341 bitmap store_antic_in
;
3342 bitmap store_antic_out
;
3343 /* Reads available to this BB upon entry. Basically, reads that
3344 dominate this BB. */
3345 bitmap read_avail_in
;
3346 /* Reads available at the end of this BB. */
3347 bitmap read_avail_out
;
3348 /* Reads performed in this BB. */
3350 /* Writes performed in this BB. */
3353 /* Temporary storage for pass. */
3354 /* Is the current BB in the worklist? */
3355 bool avail_in_worklist_p
;
3356 /* Have we visited this BB? */
3360 static bitmap_obstack tm_memopt_obstack
;
3362 /* Unique counter for TM loads and stores. Loads and stores of the
3363 same address get the same ID. */
3364 static unsigned int tm_memopt_value_id
;
3365 static hash_table
<tm_memop_hasher
> *tm_memopt_value_numbers
;
3367 #define STORE_AVAIL_IN(BB) \
3368 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_avail_in
3369 #define STORE_AVAIL_OUT(BB) \
3370 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_avail_out
3371 #define STORE_ANTIC_IN(BB) \
3372 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_antic_in
3373 #define STORE_ANTIC_OUT(BB) \
3374 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_antic_out
3375 #define READ_AVAIL_IN(BB) \
3376 ((struct tm_memopt_bitmaps *) ((BB)->aux))->read_avail_in
3377 #define READ_AVAIL_OUT(BB) \
3378 ((struct tm_memopt_bitmaps *) ((BB)->aux))->read_avail_out
3379 #define READ_LOCAL(BB) \
3380 ((struct tm_memopt_bitmaps *) ((BB)->aux))->read_local
3381 #define STORE_LOCAL(BB) \
3382 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_local
3383 #define AVAIL_IN_WORKLIST_P(BB) \
3384 ((struct tm_memopt_bitmaps *) ((BB)->aux))->avail_in_worklist_p
3385 #define BB_VISITED_P(BB) \
3386 ((struct tm_memopt_bitmaps *) ((BB)->aux))->visited_p
3388 /* Given a TM load/store in STMT, return the value number for the address
3392 tm_memopt_value_number (gimple stmt
, enum insert_option op
)
3394 struct tm_memop tmpmem
, *mem
;
3397 gcc_assert (is_tm_load (stmt
) || is_tm_store (stmt
));
3398 tmpmem
.addr
= gimple_call_arg (stmt
, 0);
3399 slot
= tm_memopt_value_numbers
->find_slot (&tmpmem
, op
);
3402 else if (op
== INSERT
)
3404 mem
= XNEW (struct tm_memop
);
3406 mem
->value_id
= tm_memopt_value_id
++;
3407 mem
->addr
= tmpmem
.addr
;
3411 return mem
->value_id
;
3414 /* Accumulate TM memory operations in BB into STORE_LOCAL and READ_LOCAL. */
3417 tm_memopt_accumulate_memops (basic_block bb
)
3419 gimple_stmt_iterator gsi
;
3421 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3423 gimple stmt
= gsi_stmt (gsi
);
3427 if (is_tm_store (stmt
))
3428 bits
= STORE_LOCAL (bb
);
3429 else if (is_tm_load (stmt
))
3430 bits
= READ_LOCAL (bb
);
3434 loc
= tm_memopt_value_number (stmt
, INSERT
);
3435 bitmap_set_bit (bits
, loc
);
3438 fprintf (dump_file
, "TM memopt (%s): value num=%d, BB=%d, addr=",
3439 is_tm_load (stmt
) ? "LOAD" : "STORE", loc
,
3440 gimple_bb (stmt
)->index
);
3441 print_generic_expr (dump_file
, gimple_call_arg (stmt
, 0), 0);
3442 fprintf (dump_file
, "\n");
3447 /* Prettily dump one of the memopt sets. BITS is the bitmap to dump. */
3450 dump_tm_memopt_set (const char *set_name
, bitmap bits
)
3454 const char *comma
= "";
3456 fprintf (dump_file
, "TM memopt: %s: [", set_name
);
3457 EXECUTE_IF_SET_IN_BITMAP (bits
, 0, i
, bi
)
3459 hash_table
<tm_memop_hasher
>::iterator hi
;
3460 struct tm_memop
*mem
= NULL
;
3462 /* Yeah, yeah, yeah. Whatever. This is just for debugging. */
3463 FOR_EACH_HASH_TABLE_ELEMENT (*tm_memopt_value_numbers
, mem
, tm_memop_t
, hi
)
3464 if (mem
->value_id
== i
)
3466 gcc_assert (mem
->value_id
== i
);
3467 fprintf (dump_file
, "%s", comma
);
3469 print_generic_expr (dump_file
, mem
->addr
, 0);
3471 fprintf (dump_file
, "]\n");
3474 /* Prettily dump all of the memopt sets in BLOCKS. */
3477 dump_tm_memopt_sets (vec
<basic_block
> blocks
)
3482 for (i
= 0; blocks
.iterate (i
, &bb
); ++i
)
3484 fprintf (dump_file
, "------------BB %d---------\n", bb
->index
);
3485 dump_tm_memopt_set ("STORE_LOCAL", STORE_LOCAL (bb
));
3486 dump_tm_memopt_set ("READ_LOCAL", READ_LOCAL (bb
));
3487 dump_tm_memopt_set ("STORE_AVAIL_IN", STORE_AVAIL_IN (bb
));
3488 dump_tm_memopt_set ("STORE_AVAIL_OUT", STORE_AVAIL_OUT (bb
));
3489 dump_tm_memopt_set ("READ_AVAIL_IN", READ_AVAIL_IN (bb
));
3490 dump_tm_memopt_set ("READ_AVAIL_OUT", READ_AVAIL_OUT (bb
));
3494 /* Compute {STORE,READ}_AVAIL_IN for the basic block BB. */
3497 tm_memopt_compute_avin (basic_block bb
)
3502 /* Seed with the AVOUT of any predecessor. */
3503 for (ix
= 0; ix
< EDGE_COUNT (bb
->preds
); ix
++)
3505 e
= EDGE_PRED (bb
, ix
);
3506 /* Make sure we have already visited this BB, and is thus
3509 If e->src->aux is NULL, this predecessor is actually on an
3510 enclosing transaction. We only care about the current
3511 transaction, so ignore it. */
3512 if (e
->src
->aux
&& BB_VISITED_P (e
->src
))
3514 bitmap_copy (STORE_AVAIL_IN (bb
), STORE_AVAIL_OUT (e
->src
));
3515 bitmap_copy (READ_AVAIL_IN (bb
), READ_AVAIL_OUT (e
->src
));
3520 for (; ix
< EDGE_COUNT (bb
->preds
); ix
++)
3522 e
= EDGE_PRED (bb
, ix
);
3523 if (e
->src
->aux
&& BB_VISITED_P (e
->src
))
3525 bitmap_and_into (STORE_AVAIL_IN (bb
), STORE_AVAIL_OUT (e
->src
));
3526 bitmap_and_into (READ_AVAIL_IN (bb
), READ_AVAIL_OUT (e
->src
));
3530 BB_VISITED_P (bb
) = true;
3533 /* Compute the STORE_ANTIC_IN for the basic block BB. */
3536 tm_memopt_compute_antin (basic_block bb
)
3541 /* Seed with the ANTIC_OUT of any successor. */
3542 for (ix
= 0; ix
< EDGE_COUNT (bb
->succs
); ix
++)
3544 e
= EDGE_SUCC (bb
, ix
);
3545 /* Make sure we have already visited this BB, and is thus
3547 if (BB_VISITED_P (e
->dest
))
3549 bitmap_copy (STORE_ANTIC_IN (bb
), STORE_ANTIC_OUT (e
->dest
));
3554 for (; ix
< EDGE_COUNT (bb
->succs
); ix
++)
3556 e
= EDGE_SUCC (bb
, ix
);
3557 if (BB_VISITED_P (e
->dest
))
3558 bitmap_and_into (STORE_ANTIC_IN (bb
), STORE_ANTIC_OUT (e
->dest
));
3561 BB_VISITED_P (bb
) = true;
3564 /* Compute the AVAIL sets for every basic block in BLOCKS.
3566 We compute {STORE,READ}_AVAIL_{OUT,IN} as follows:
3568 AVAIL_OUT[bb] = union (AVAIL_IN[bb], LOCAL[bb])
3569 AVAIL_IN[bb] = intersect (AVAIL_OUT[predecessors])
3571 This is basically what we do in lcm's compute_available(), but here
3572 we calculate two sets of sets (one for STOREs and one for READs),
3573 and we work on a region instead of the entire CFG.
3575 REGION is the TM region.
3576 BLOCKS are the basic blocks in the region. */
3579 tm_memopt_compute_available (struct tm_region
*region
,
3580 vec
<basic_block
> blocks
)
3583 basic_block
*worklist
, *qin
, *qout
, *qend
, bb
;
3584 unsigned int qlen
, i
;
3588 /* Allocate a worklist array/queue. Entries are only added to the
3589 list if they were not already on the list. So the size is
3590 bounded by the number of basic blocks in the region. */
3591 qlen
= blocks
.length () - 1;
3592 qin
= qout
= worklist
=
3593 XNEWVEC (basic_block
, qlen
);
3595 /* Put every block in the region on the worklist. */
3596 for (i
= 0; blocks
.iterate (i
, &bb
); ++i
)
3598 /* Seed AVAIL_OUT with the LOCAL set. */
3599 bitmap_ior_into (STORE_AVAIL_OUT (bb
), STORE_LOCAL (bb
));
3600 bitmap_ior_into (READ_AVAIL_OUT (bb
), READ_LOCAL (bb
));
3602 AVAIL_IN_WORKLIST_P (bb
) = true;
3603 /* No need to insert the entry block, since it has an AVIN of
3604 null, and an AVOUT that has already been seeded in. */
3605 if (bb
!= region
->entry_block
)
3609 /* The entry block has been initialized with the local sets. */
3610 BB_VISITED_P (region
->entry_block
) = true;
3613 qend
= &worklist
[qlen
];
3615 /* Iterate until the worklist is empty. */
3618 /* Take the first entry off the worklist. */
3625 /* This block can be added to the worklist again if necessary. */
3626 AVAIL_IN_WORKLIST_P (bb
) = false;
3627 tm_memopt_compute_avin (bb
);
3629 /* Note: We do not add the LOCAL sets here because we already
3630 seeded the AVAIL_OUT sets with them. */
3631 changed
= bitmap_ior_into (STORE_AVAIL_OUT (bb
), STORE_AVAIL_IN (bb
));
3632 changed
|= bitmap_ior_into (READ_AVAIL_OUT (bb
), READ_AVAIL_IN (bb
));
3634 && (region
->exit_blocks
== NULL
3635 || !bitmap_bit_p (region
->exit_blocks
, bb
->index
)))
3636 /* If the out state of this block changed, then we need to add
3637 its successors to the worklist if they are not already in. */
3638 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3639 if (!AVAIL_IN_WORKLIST_P (e
->dest
)
3640 && e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
3643 AVAIL_IN_WORKLIST_P (e
->dest
) = true;
3654 dump_tm_memopt_sets (blocks
);
3657 /* Compute ANTIC sets for every basic block in BLOCKS.
3659 We compute STORE_ANTIC_OUT as follows:
3661 STORE_ANTIC_OUT[bb] = union(STORE_ANTIC_IN[bb], STORE_LOCAL[bb])
3662 STORE_ANTIC_IN[bb] = intersect(STORE_ANTIC_OUT[successors])
3664 REGION is the TM region.
3665 BLOCKS are the basic blocks in the region. */
3668 tm_memopt_compute_antic (struct tm_region
*region
,
3669 vec
<basic_block
> blocks
)
3672 basic_block
*worklist
, *qin
, *qout
, *qend
, bb
;
3677 /* Allocate a worklist array/queue. Entries are only added to the
3678 list if they were not already on the list. So the size is
3679 bounded by the number of basic blocks in the region. */
3680 qin
= qout
= worklist
= XNEWVEC (basic_block
, blocks
.length ());
3682 for (qlen
= 0, i
= blocks
.length () - 1; i
>= 0; --i
)
3686 /* Seed ANTIC_OUT with the LOCAL set. */
3687 bitmap_ior_into (STORE_ANTIC_OUT (bb
), STORE_LOCAL (bb
));
3689 /* Put every block in the region on the worklist. */
3690 AVAIL_IN_WORKLIST_P (bb
) = true;
3691 /* No need to insert exit blocks, since their ANTIC_IN is NULL,
3692 and their ANTIC_OUT has already been seeded in. */
3693 if (region
->exit_blocks
3694 && !bitmap_bit_p (region
->exit_blocks
, bb
->index
))
3701 /* The exit blocks have been initialized with the local sets. */
3702 if (region
->exit_blocks
)
3706 EXECUTE_IF_SET_IN_BITMAP (region
->exit_blocks
, 0, i
, bi
)
3707 BB_VISITED_P (BASIC_BLOCK_FOR_FN (cfun
, i
)) = true;
3711 qend
= &worklist
[qlen
];
3713 /* Iterate until the worklist is empty. */
3716 /* Take the first entry off the worklist. */
3723 /* This block can be added to the worklist again if necessary. */
3724 AVAIL_IN_WORKLIST_P (bb
) = false;
3725 tm_memopt_compute_antin (bb
);
3727 /* Note: We do not add the LOCAL sets here because we already
3728 seeded the ANTIC_OUT sets with them. */
3729 if (bitmap_ior_into (STORE_ANTIC_OUT (bb
), STORE_ANTIC_IN (bb
))
3730 && bb
!= region
->entry_block
)
3731 /* If the out state of this block changed, then we need to add
3732 its predecessors to the worklist if they are not already in. */
3733 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3734 if (!AVAIL_IN_WORKLIST_P (e
->src
))
3737 AVAIL_IN_WORKLIST_P (e
->src
) = true;
3748 dump_tm_memopt_sets (blocks
);
3751 /* Offsets of load variants from TM_LOAD. For example,
3752 BUILT_IN_TM_LOAD_RAR* is an offset of 1 from BUILT_IN_TM_LOAD*.
3753 See gtm-builtins.def. */
3754 #define TRANSFORM_RAR 1
3755 #define TRANSFORM_RAW 2
3756 #define TRANSFORM_RFW 3
3757 /* Offsets of store variants from TM_STORE. */
3758 #define TRANSFORM_WAR 1
3759 #define TRANSFORM_WAW 2
3761 /* Inform about a load/store optimization. */
3764 dump_tm_memopt_transform (gimple stmt
)
3768 fprintf (dump_file
, "TM memopt: transforming: ");
3769 print_gimple_stmt (dump_file
, stmt
, 0, 0);
3770 fprintf (dump_file
, "\n");
3774 /* Perform a read/write optimization. Replaces the TM builtin in STMT
3775 by a builtin that is OFFSET entries down in the builtins table in
3776 gtm-builtins.def. */
3779 tm_memopt_transform_stmt (unsigned int offset
,
3781 gimple_stmt_iterator
*gsi
)
3783 tree fn
= gimple_call_fn (stmt
);
3784 gcc_assert (TREE_CODE (fn
) == ADDR_EXPR
);
3785 TREE_OPERAND (fn
, 0)
3786 = builtin_decl_explicit ((enum built_in_function
)
3787 (DECL_FUNCTION_CODE (TREE_OPERAND (fn
, 0))
3789 gimple_call_set_fn (stmt
, fn
);
3790 gsi_replace (gsi
, stmt
, true);
3791 dump_tm_memopt_transform (stmt
);
3794 /* Perform the actual TM memory optimization transformations in the
3795 basic blocks in BLOCKS. */
3798 tm_memopt_transform_blocks (vec
<basic_block
> blocks
)
3802 gimple_stmt_iterator gsi
;
3804 for (i
= 0; blocks
.iterate (i
, &bb
); ++i
)
3806 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3808 gimple stmt
= gsi_stmt (gsi
);
3809 bitmap read_avail
= READ_AVAIL_IN (bb
);
3810 bitmap store_avail
= STORE_AVAIL_IN (bb
);
3811 bitmap store_antic
= STORE_ANTIC_OUT (bb
);
3814 if (is_tm_simple_load (stmt
))
3816 loc
= tm_memopt_value_number (stmt
, NO_INSERT
);
3817 if (store_avail
&& bitmap_bit_p (store_avail
, loc
))
3818 tm_memopt_transform_stmt (TRANSFORM_RAW
, stmt
, &gsi
);
3819 else if (store_antic
&& bitmap_bit_p (store_antic
, loc
))
3821 tm_memopt_transform_stmt (TRANSFORM_RFW
, stmt
, &gsi
);
3822 bitmap_set_bit (store_avail
, loc
);
3824 else if (read_avail
&& bitmap_bit_p (read_avail
, loc
))
3825 tm_memopt_transform_stmt (TRANSFORM_RAR
, stmt
, &gsi
);
3827 bitmap_set_bit (read_avail
, loc
);
3829 else if (is_tm_simple_store (stmt
))
3831 loc
= tm_memopt_value_number (stmt
, NO_INSERT
);
3832 if (store_avail
&& bitmap_bit_p (store_avail
, loc
))
3833 tm_memopt_transform_stmt (TRANSFORM_WAW
, stmt
, &gsi
);
3836 if (read_avail
&& bitmap_bit_p (read_avail
, loc
))
3837 tm_memopt_transform_stmt (TRANSFORM_WAR
, stmt
, &gsi
);
3838 bitmap_set_bit (store_avail
, loc
);
3845 /* Return a new set of bitmaps for a BB. */
3847 static struct tm_memopt_bitmaps
*
3848 tm_memopt_init_sets (void)
3850 struct tm_memopt_bitmaps
*b
3851 = XOBNEW (&tm_memopt_obstack
.obstack
, struct tm_memopt_bitmaps
);
3852 b
->store_avail_in
= BITMAP_ALLOC (&tm_memopt_obstack
);
3853 b
->store_avail_out
= BITMAP_ALLOC (&tm_memopt_obstack
);
3854 b
->store_antic_in
= BITMAP_ALLOC (&tm_memopt_obstack
);
3855 b
->store_antic_out
= BITMAP_ALLOC (&tm_memopt_obstack
);
3856 b
->store_avail_out
= BITMAP_ALLOC (&tm_memopt_obstack
);
3857 b
->read_avail_in
= BITMAP_ALLOC (&tm_memopt_obstack
);
3858 b
->read_avail_out
= BITMAP_ALLOC (&tm_memopt_obstack
);
3859 b
->read_local
= BITMAP_ALLOC (&tm_memopt_obstack
);
3860 b
->store_local
= BITMAP_ALLOC (&tm_memopt_obstack
);
3864 /* Free sets computed for each BB. */
3867 tm_memopt_free_sets (vec
<basic_block
> blocks
)
3872 for (i
= 0; blocks
.iterate (i
, &bb
); ++i
)
3876 /* Clear the visited bit for every basic block in BLOCKS. */
3879 tm_memopt_clear_visited (vec
<basic_block
> blocks
)
3884 for (i
= 0; blocks
.iterate (i
, &bb
); ++i
)
3885 BB_VISITED_P (bb
) = false;
3888 /* Replace TM load/stores with hints for the runtime. We handle
3889 things like read-after-write, write-after-read, read-after-read,
3890 read-for-write, etc. */
3893 execute_tm_memopt (void)
3895 struct tm_region
*region
;
3896 vec
<basic_block
> bbs
;
3898 tm_memopt_value_id
= 0;
3899 tm_memopt_value_numbers
= new hash_table
<tm_memop_hasher
> (10);
3901 for (region
= all_tm_regions
; region
; region
= region
->next
)
3903 /* All the TM stores/loads in the current region. */
3907 bitmap_obstack_initialize (&tm_memopt_obstack
);
3909 /* Save all BBs for the current region. */
3910 bbs
= get_tm_region_blocks (region
->entry_block
,
3911 region
->exit_blocks
,
3916 /* Collect all the memory operations. */
3917 for (i
= 0; bbs
.iterate (i
, &bb
); ++i
)
3919 bb
->aux
= tm_memopt_init_sets ();
3920 tm_memopt_accumulate_memops (bb
);
3923 /* Solve data flow equations and transform each block accordingly. */
3924 tm_memopt_clear_visited (bbs
);
3925 tm_memopt_compute_available (region
, bbs
);
3926 tm_memopt_clear_visited (bbs
);
3927 tm_memopt_compute_antic (region
, bbs
);
3928 tm_memopt_transform_blocks (bbs
);
3930 tm_memopt_free_sets (bbs
);
3932 bitmap_obstack_release (&tm_memopt_obstack
);
3933 tm_memopt_value_numbers
->empty ();
3936 delete tm_memopt_value_numbers
;
3937 tm_memopt_value_numbers
= NULL
;
3943 const pass_data pass_data_tm_memopt
=
3945 GIMPLE_PASS
, /* type */
3946 "tmmemopt", /* name */
3947 OPTGROUP_NONE
, /* optinfo_flags */
3948 TV_TRANS_MEM
, /* tv_id */
3949 ( PROP_ssa
| PROP_cfg
), /* properties_required */
3950 0, /* properties_provided */
3951 0, /* properties_destroyed */
3952 0, /* todo_flags_start */
3953 0, /* todo_flags_finish */
3956 class pass_tm_memopt
: public gimple_opt_pass
3959 pass_tm_memopt (gcc::context
*ctxt
)
3960 : gimple_opt_pass (pass_data_tm_memopt
, ctxt
)
3963 /* opt_pass methods: */
3964 virtual bool gate (function
*) { return flag_tm
&& optimize
> 0; }
3965 virtual unsigned int execute (function
*) { return execute_tm_memopt (); }
3967 }; // class pass_tm_memopt
3972 make_pass_tm_memopt (gcc::context
*ctxt
)
3974 return new pass_tm_memopt (ctxt
);
3978 /* Interprocedual analysis for the creation of transactional clones.
3979 The aim of this pass is to find which functions are referenced in
3980 a non-irrevocable transaction context, and for those over which
3981 we have control (or user directive), create a version of the
3982 function which uses only the transactional interface to reference
3983 protected memories. This analysis proceeds in several steps:
3985 (1) Collect the set of all possible transactional clones:
3987 (a) For all local public functions marked tm_callable, push
3988 it onto the tm_callee queue.
3990 (b) For all local functions, scan for calls in transaction blocks.
3991 Push the caller and callee onto the tm_caller and tm_callee
3992 queues. Count the number of callers for each callee.
3994 (c) For each local function on the callee list, assume we will
3995 create a transactional clone. Push *all* calls onto the
3996 callee queues; count the number of clone callers separately
3997 to the number of original callers.
3999 (2) Propagate irrevocable status up the dominator tree:
4001 (a) Any external function on the callee list that is not marked
4002 tm_callable is irrevocable. Push all callers of such onto
4005 (b) For each function on the worklist, mark each block that
4006 contains an irrevocable call. Use the AND operator to
4007 propagate that mark up the dominator tree.
4009 (c) If we reach the entry block for a possible transactional
4010 clone, then the transactional clone is irrevocable, and
4011 we should not create the clone after all. Push all
4012 callers onto the worklist.
4014 (d) Place tm_irrevocable calls at the beginning of the relevant
4015 blocks. Special case here is the entry block for the entire
4016 transaction region; there we mark it GTMA_DOES_GO_IRREVOCABLE for
4017 the library to begin the region in serial mode. Decrement
4018 the call count for all callees in the irrevocable region.
4020 (3) Create the transactional clones:
4022 Any tm_callee that still has a non-zero call count is cloned.
4025 /* This structure is stored in the AUX field of each cgraph_node. */
4026 struct tm_ipa_cg_data
4028 /* The clone of the function that got created. */
4029 struct cgraph_node
*clone
;
4031 /* The tm regions in the normal function. */
4032 struct tm_region
*all_tm_regions
;
4034 /* The blocks of the normal/clone functions that contain irrevocable
4035 calls, or blocks that are post-dominated by irrevocable calls. */
4036 bitmap irrevocable_blocks_normal
;
4037 bitmap irrevocable_blocks_clone
;
4039 /* The blocks of the normal function that are involved in transactions. */
4040 bitmap transaction_blocks_normal
;
4042 /* The number of callers to the transactional clone of this function
4043 from normal and transactional clones respectively. */
4044 unsigned tm_callers_normal
;
4045 unsigned tm_callers_clone
;
4047 /* True if all calls to this function's transactional clone
4048 are irrevocable. Also automatically true if the function
4049 has no transactional clone. */
4050 bool is_irrevocable
;
4052 /* Flags indicating the presence of this function in various queues. */
4053 bool in_callee_queue
;
4056 /* Flags indicating the kind of scan desired while in the worklist. */
4057 bool want_irr_scan_normal
;
4060 typedef vec
<cgraph_node
*> cgraph_node_queue
;
4062 /* Return the ipa data associated with NODE, allocating zeroed memory
4063 if necessary. TRAVERSE_ALIASES is true if we must traverse aliases
4064 and set *NODE accordingly. */
4066 static struct tm_ipa_cg_data
*
4067 get_cg_data (struct cgraph_node
**node
, bool traverse_aliases
)
4069 struct tm_ipa_cg_data
*d
;
4071 if (traverse_aliases
&& (*node
)->alias
)
4072 *node
= (*node
)->get_alias_target ();
4074 d
= (struct tm_ipa_cg_data
*) (*node
)->aux
;
4078 d
= (struct tm_ipa_cg_data
*)
4079 obstack_alloc (&tm_obstack
.obstack
, sizeof (*d
));
4080 (*node
)->aux
= (void *) d
;
4081 memset (d
, 0, sizeof (*d
));
4087 /* Add NODE to the end of QUEUE, unless IN_QUEUE_P indicates that
4088 it is already present. */
4091 maybe_push_queue (struct cgraph_node
*node
,
4092 cgraph_node_queue
*queue_p
, bool *in_queue_p
)
4097 queue_p
->safe_push (node
);
4101 /* Duplicate the basic blocks in QUEUE for use in the uninstrumented
4102 code path. QUEUE are the basic blocks inside the transaction
4103 represented in REGION.
4105 Later in split_code_paths() we will add the conditional to choose
4106 between the two alternatives. */
4109 ipa_uninstrument_transaction (struct tm_region
*region
,
4110 vec
<basic_block
> queue
)
4112 gimple transaction
= region
->transaction_stmt
;
4113 basic_block transaction_bb
= gimple_bb (transaction
);
4114 int n
= queue
.length ();
4115 basic_block
*new_bbs
= XNEWVEC (basic_block
, n
);
4117 copy_bbs (queue
.address (), n
, new_bbs
, NULL
, 0, NULL
, NULL
, transaction_bb
,
4119 edge e
= make_edge (transaction_bb
, new_bbs
[0], EDGE_TM_UNINSTRUMENTED
);
4120 add_phi_args_after_copy (new_bbs
, n
, e
);
4122 // Now we will have a GIMPLE_ATOMIC with 3 possible edges out of it.
4123 // a) EDGE_FALLTHRU into the transaction
4124 // b) EDGE_TM_ABORT out of the transaction
4125 // c) EDGE_TM_UNINSTRUMENTED into the uninstrumented blocks.
4130 /* A subroutine of ipa_tm_scan_calls_transaction and ipa_tm_scan_calls_clone.
4131 Queue all callees within block BB. */
4134 ipa_tm_scan_calls_block (cgraph_node_queue
*callees_p
,
4135 basic_block bb
, bool for_clone
)
4137 gimple_stmt_iterator gsi
;
4139 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4141 gimple stmt
= gsi_stmt (gsi
);
4142 if (is_gimple_call (stmt
) && !is_tm_pure_call (stmt
))
4144 tree fndecl
= gimple_call_fndecl (stmt
);
4147 struct tm_ipa_cg_data
*d
;
4149 struct cgraph_node
*node
;
4151 if (is_tm_ending_fndecl (fndecl
))
4153 if (find_tm_replacement_function (fndecl
))
4156 node
= cgraph_node::get (fndecl
);
4157 gcc_assert (node
!= NULL
);
4158 d
= get_cg_data (&node
, true);
4160 pcallers
= (for_clone
? &d
->tm_callers_clone
4161 : &d
->tm_callers_normal
);
4164 maybe_push_queue (node
, callees_p
, &d
->in_callee_queue
);
4170 /* Scan all calls in NODE that are within a transaction region,
4171 and push the resulting nodes into the callee queue. */
4174 ipa_tm_scan_calls_transaction (struct tm_ipa_cg_data
*d
,
4175 cgraph_node_queue
*callees_p
)
4177 struct tm_region
*r
;
4179 d
->transaction_blocks_normal
= BITMAP_ALLOC (&tm_obstack
);
4180 d
->all_tm_regions
= all_tm_regions
;
4182 for (r
= all_tm_regions
; r
; r
= r
->next
)
4184 vec
<basic_block
> bbs
;
4188 bbs
= get_tm_region_blocks (r
->entry_block
, r
->exit_blocks
, NULL
,
4189 d
->transaction_blocks_normal
, false);
4191 // Generate the uninstrumented code path for this transaction.
4192 ipa_uninstrument_transaction (r
, bbs
);
4194 FOR_EACH_VEC_ELT (bbs
, i
, bb
)
4195 ipa_tm_scan_calls_block (callees_p
, bb
, false);
4200 // ??? copy_bbs should maintain cgraph edges for the blocks as it is
4201 // copying them, rather than forcing us to do this externally.
4202 cgraph_edge::rebuild_edges ();
4204 // ??? In ipa_uninstrument_transaction we don't try to update dominators
4205 // because copy_bbs doesn't return a VEC like iterate_fix_dominators expects.
4206 // Instead, just release dominators here so update_ssa recomputes them.
4207 free_dominance_info (CDI_DOMINATORS
);
4209 // When building the uninstrumented code path, copy_bbs will have invoked
4210 // create_new_def_for starting an "ssa update context". There is only one
4211 // instance of this context, so resolve ssa updates before moving on to
4212 // the next function.
4213 update_ssa (TODO_update_ssa
);
4216 /* Scan all calls in NODE as if this is the transactional clone,
4217 and push the destinations into the callee queue. */
4220 ipa_tm_scan_calls_clone (struct cgraph_node
*node
,
4221 cgraph_node_queue
*callees_p
)
4223 struct function
*fn
= DECL_STRUCT_FUNCTION (node
->decl
);
4226 FOR_EACH_BB_FN (bb
, fn
)
4227 ipa_tm_scan_calls_block (callees_p
, bb
, true);
4230 /* The function NODE has been detected to be irrevocable. Push all
4231 of its callers onto WORKLIST for the purpose of re-scanning them. */
4234 ipa_tm_note_irrevocable (struct cgraph_node
*node
,
4235 cgraph_node_queue
*worklist_p
)
4237 struct tm_ipa_cg_data
*d
= get_cg_data (&node
, true);
4238 struct cgraph_edge
*e
;
4240 d
->is_irrevocable
= true;
4242 for (e
= node
->callers
; e
; e
= e
->next_caller
)
4245 struct cgraph_node
*caller
;
4247 /* Don't examine recursive calls. */
4248 if (e
->caller
== node
)
4250 /* Even if we think we can go irrevocable, believe the user
4252 if (is_tm_safe_or_pure (e
->caller
->decl
))
4256 d
= get_cg_data (&caller
, true);
4258 /* Check if the callee is in a transactional region. If so,
4259 schedule the function for normal re-scan as well. */
4260 bb
= gimple_bb (e
->call_stmt
);
4261 gcc_assert (bb
!= NULL
);
4262 if (d
->transaction_blocks_normal
4263 && bitmap_bit_p (d
->transaction_blocks_normal
, bb
->index
))
4264 d
->want_irr_scan_normal
= true;
4266 maybe_push_queue (caller
, worklist_p
, &d
->in_worklist
);
4270 /* A subroutine of ipa_tm_scan_irr_blocks; return true iff any statement
4271 within the block is irrevocable. */
4274 ipa_tm_scan_irr_block (basic_block bb
)
4276 gimple_stmt_iterator gsi
;
4279 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4281 gimple stmt
= gsi_stmt (gsi
);
4282 switch (gimple_code (stmt
))
4285 if (gimple_assign_single_p (stmt
))
4287 tree lhs
= gimple_assign_lhs (stmt
);
4288 tree rhs
= gimple_assign_rhs1 (stmt
);
4289 if (volatile_var_p (lhs
) || volatile_var_p (rhs
))
4296 tree lhs
= gimple_call_lhs (stmt
);
4297 if (lhs
&& volatile_var_p (lhs
))
4300 if (is_tm_pure_call (stmt
))
4303 fn
= gimple_call_fn (stmt
);
4305 /* Functions with the attribute are by definition irrevocable. */
4306 if (is_tm_irrevocable (fn
))
4309 /* For direct function calls, go ahead and check for replacement
4310 functions, or transitive irrevocable functions. For indirect
4311 functions, we'll ask the runtime. */
4312 if (TREE_CODE (fn
) == ADDR_EXPR
)
4314 struct tm_ipa_cg_data
*d
;
4315 struct cgraph_node
*node
;
4317 fn
= TREE_OPERAND (fn
, 0);
4318 if (is_tm_ending_fndecl (fn
))
4320 if (find_tm_replacement_function (fn
))
4323 node
= cgraph_node::get (fn
);
4324 d
= get_cg_data (&node
, true);
4326 /* Return true if irrevocable, but above all, believe
4328 if (d
->is_irrevocable
4329 && !is_tm_safe_or_pure (fn
))
4336 /* ??? The Approved Method of indicating that an inline
4337 assembly statement is not relevant to the transaction
4338 is to wrap it in a __tm_waiver block. This is not
4339 yet implemented, so we can't check for it. */
4340 if (is_tm_safe (current_function_decl
))
4342 tree t
= build1 (NOP_EXPR
, void_type_node
, size_zero_node
);
4343 SET_EXPR_LOCATION (t
, gimple_location (stmt
));
4344 error ("%Kasm not allowed in %<transaction_safe%> function", t
);
4356 /* For each of the blocks seeded witin PQUEUE, walk the CFG looking
4357 for new irrevocable blocks, marking them in NEW_IRR. Don't bother
4358 scanning past OLD_IRR or EXIT_BLOCKS. */
4361 ipa_tm_scan_irr_blocks (vec
<basic_block
> *pqueue
, bitmap new_irr
,
4362 bitmap old_irr
, bitmap exit_blocks
)
4364 bool any_new_irr
= false;
4367 bitmap visited_blocks
= BITMAP_ALLOC (NULL
);
4371 basic_block bb
= pqueue
->pop ();
4373 /* Don't re-scan blocks we know already are irrevocable. */
4374 if (old_irr
&& bitmap_bit_p (old_irr
, bb
->index
))
4377 if (ipa_tm_scan_irr_block (bb
))
4379 bitmap_set_bit (new_irr
, bb
->index
);
4382 else if (exit_blocks
== NULL
|| !bitmap_bit_p (exit_blocks
, bb
->index
))
4384 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4385 if (!bitmap_bit_p (visited_blocks
, e
->dest
->index
))
4387 bitmap_set_bit (visited_blocks
, e
->dest
->index
);
4388 pqueue
->safe_push (e
->dest
);
4392 while (!pqueue
->is_empty ());
4394 BITMAP_FREE (visited_blocks
);
4399 /* Propagate the irrevocable property both up and down the dominator tree.
4400 BB is the current block being scanned; EXIT_BLOCKS are the edges of the
4401 TM regions; OLD_IRR are the results of a previous scan of the dominator
4402 tree which has been fully propagated; NEW_IRR is the set of new blocks
4403 which are gaining the irrevocable property during the current scan. */
4406 ipa_tm_propagate_irr (basic_block entry_block
, bitmap new_irr
,
4407 bitmap old_irr
, bitmap exit_blocks
)
4409 vec
<basic_block
> bbs
;
4410 bitmap all_region_blocks
;
4412 /* If this block is in the old set, no need to rescan. */
4413 if (old_irr
&& bitmap_bit_p (old_irr
, entry_block
->index
))
4416 all_region_blocks
= BITMAP_ALLOC (&tm_obstack
);
4417 bbs
= get_tm_region_blocks (entry_block
, exit_blocks
, NULL
,
4418 all_region_blocks
, false);
4421 basic_block bb
= bbs
.pop ();
4422 bool this_irr
= bitmap_bit_p (new_irr
, bb
->index
);
4423 bool all_son_irr
= false;
4427 /* Propagate up. If my children are, I am too, but we must have
4428 at least one child that is. */
4431 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4433 if (!bitmap_bit_p (new_irr
, e
->dest
->index
))
4435 all_son_irr
= false;
4443 /* Add block to new_irr if it hasn't already been processed. */
4444 if (!old_irr
|| !bitmap_bit_p (old_irr
, bb
->index
))
4446 bitmap_set_bit (new_irr
, bb
->index
);
4452 /* Propagate down to everyone we immediately dominate. */
4456 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
4458 son
= next_dom_son (CDI_DOMINATORS
, son
))
4460 /* Make sure block is actually in a TM region, and it
4461 isn't already in old_irr. */
4462 if ((!old_irr
|| !bitmap_bit_p (old_irr
, son
->index
))
4463 && bitmap_bit_p (all_region_blocks
, son
->index
))
4464 bitmap_set_bit (new_irr
, son
->index
);
4468 while (!bbs
.is_empty ());
4470 BITMAP_FREE (all_region_blocks
);
4475 ipa_tm_decrement_clone_counts (basic_block bb
, bool for_clone
)
4477 gimple_stmt_iterator gsi
;
4479 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4481 gimple stmt
= gsi_stmt (gsi
);
4482 if (is_gimple_call (stmt
) && !is_tm_pure_call (stmt
))
4484 tree fndecl
= gimple_call_fndecl (stmt
);
4487 struct tm_ipa_cg_data
*d
;
4489 struct cgraph_node
*tnode
;
4491 if (is_tm_ending_fndecl (fndecl
))
4493 if (find_tm_replacement_function (fndecl
))
4496 tnode
= cgraph_node::get (fndecl
);
4497 d
= get_cg_data (&tnode
, true);
4499 pcallers
= (for_clone
? &d
->tm_callers_clone
4500 : &d
->tm_callers_normal
);
4502 gcc_assert (*pcallers
> 0);
4509 /* (Re-)Scan the transaction blocks in NODE for calls to irrevocable functions,
4510 as well as other irrevocable actions such as inline assembly. Mark all
4511 such blocks as irrevocable and decrement the number of calls to
4512 transactional clones. Return true if, for the transactional clone, the
4513 entire function is irrevocable. */
4516 ipa_tm_scan_irr_function (struct cgraph_node
*node
, bool for_clone
)
4518 struct tm_ipa_cg_data
*d
;
4519 bitmap new_irr
, old_irr
;
4522 /* Builtin operators (operator new, and such). */
4523 if (DECL_STRUCT_FUNCTION (node
->decl
) == NULL
4524 || DECL_STRUCT_FUNCTION (node
->decl
)->cfg
== NULL
)
4527 push_cfun (DECL_STRUCT_FUNCTION (node
->decl
));
4528 calculate_dominance_info (CDI_DOMINATORS
);
4530 d
= get_cg_data (&node
, true);
4531 auto_vec
<basic_block
, 10> queue
;
4532 new_irr
= BITMAP_ALLOC (&tm_obstack
);
4534 /* Scan each tm region, propagating irrevocable status through the tree. */
4537 old_irr
= d
->irrevocable_blocks_clone
;
4538 queue
.quick_push (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)));
4539 if (ipa_tm_scan_irr_blocks (&queue
, new_irr
, old_irr
, NULL
))
4541 ipa_tm_propagate_irr (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
4544 ret
= bitmap_bit_p (new_irr
,
4545 single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
))->index
);
4550 struct tm_region
*region
;
4552 old_irr
= d
->irrevocable_blocks_normal
;
4553 for (region
= d
->all_tm_regions
; region
; region
= region
->next
)
4555 queue
.quick_push (region
->entry_block
);
4556 if (ipa_tm_scan_irr_blocks (&queue
, new_irr
, old_irr
,
4557 region
->exit_blocks
))
4558 ipa_tm_propagate_irr (region
->entry_block
, new_irr
, old_irr
,
4559 region
->exit_blocks
);
4563 /* If we found any new irrevocable blocks, reduce the call count for
4564 transactional clones within the irrevocable blocks. Save the new
4565 set of irrevocable blocks for next time. */
4566 if (!bitmap_empty_p (new_irr
))
4568 bitmap_iterator bmi
;
4571 EXECUTE_IF_SET_IN_BITMAP (new_irr
, 0, i
, bmi
)
4572 ipa_tm_decrement_clone_counts (BASIC_BLOCK_FOR_FN (cfun
, i
),
4577 bitmap_ior_into (old_irr
, new_irr
);
4578 BITMAP_FREE (new_irr
);
4581 d
->irrevocable_blocks_clone
= new_irr
;
4583 d
->irrevocable_blocks_normal
= new_irr
;
4585 if (dump_file
&& new_irr
)
4588 bitmap_iterator bmi
;
4591 dname
= lang_hooks
.decl_printable_name (current_function_decl
, 2);
4592 EXECUTE_IF_SET_IN_BITMAP (new_irr
, 0, i
, bmi
)
4593 fprintf (dump_file
, "%s: bb %d goes irrevocable\n", dname
, i
);
4597 BITMAP_FREE (new_irr
);
4604 /* Return true if, for the transactional clone of NODE, any call
4605 may enter irrevocable mode. */
4608 ipa_tm_mayenterirr_function (struct cgraph_node
*node
)
4610 struct tm_ipa_cg_data
*d
;
4614 d
= get_cg_data (&node
, true);
4616 flags
= flags_from_decl_or_type (decl
);
4618 /* Handle some TM builtins. Ordinarily these aren't actually generated
4619 at this point, but handling these functions when written in by the
4620 user makes it easier to build unit tests. */
4621 if (flags
& ECF_TM_BUILTIN
)
4624 /* Filter out all functions that are marked. */
4625 if (flags
& ECF_TM_PURE
)
4627 if (is_tm_safe (decl
))
4629 if (is_tm_irrevocable (decl
))
4631 if (is_tm_callable (decl
))
4633 if (find_tm_replacement_function (decl
))
4636 /* If we aren't seeing the final version of the function we don't
4637 know what it will contain at runtime. */
4638 if (node
->get_availability () < AVAIL_AVAILABLE
)
4641 /* If the function must go irrevocable, then of course true. */
4642 if (d
->is_irrevocable
)
4645 /* If there are any blocks marked irrevocable, then the function
4646 as a whole may enter irrevocable. */
4647 if (d
->irrevocable_blocks_clone
)
4650 /* We may have previously marked this function as tm_may_enter_irr;
4651 see pass_diagnose_tm_blocks. */
4652 if (node
->local
.tm_may_enter_irr
)
4655 /* Recurse on the main body for aliases. In general, this will
4656 result in one of the bits above being set so that we will not
4657 have to recurse next time. */
4659 return ipa_tm_mayenterirr_function (cgraph_node::get (node
->thunk
.alias
));
4661 /* What remains is unmarked local functions without items that force
4662 the function to go irrevocable. */
4666 /* Diagnose calls from transaction_safe functions to unmarked
4667 functions that are determined to not be safe. */
4670 ipa_tm_diagnose_tm_safe (struct cgraph_node
*node
)
4672 struct cgraph_edge
*e
;
4674 for (e
= node
->callees
; e
; e
= e
->next_callee
)
4675 if (!is_tm_callable (e
->callee
->decl
)
4676 && e
->callee
->local
.tm_may_enter_irr
)
4677 error_at (gimple_location (e
->call_stmt
),
4678 "unsafe function call %qD within "
4679 "%<transaction_safe%> function", e
->callee
->decl
);
4682 /* Diagnose call from atomic transactions to unmarked functions
4683 that are determined to not be safe. */
4686 ipa_tm_diagnose_transaction (struct cgraph_node
*node
,
4687 struct tm_region
*all_tm_regions
)
4689 struct tm_region
*r
;
4691 for (r
= all_tm_regions
; r
; r
= r
->next
)
4692 if (gimple_transaction_subcode (r
->get_transaction_stmt ())
4695 /* Atomic transactions can be nested inside relaxed. */
4697 ipa_tm_diagnose_transaction (node
, r
->inner
);
4701 vec
<basic_block
> bbs
;
4702 gimple_stmt_iterator gsi
;
4706 bbs
= get_tm_region_blocks (r
->entry_block
, r
->exit_blocks
,
4707 r
->irr_blocks
, NULL
, false);
4709 for (i
= 0; bbs
.iterate (i
, &bb
); ++i
)
4710 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4712 gimple stmt
= gsi_stmt (gsi
);
4715 if (gimple_code (stmt
) == GIMPLE_ASM
)
4717 error_at (gimple_location (stmt
),
4718 "asm not allowed in atomic transaction");
4722 if (!is_gimple_call (stmt
))
4724 fndecl
= gimple_call_fndecl (stmt
);
4726 /* Indirect function calls have been diagnosed already. */
4730 /* Stop at the end of the transaction. */
4731 if (is_tm_ending_fndecl (fndecl
))
4733 if (bitmap_bit_p (r
->exit_blocks
, bb
->index
))
4738 /* Marked functions have been diagnosed already. */
4739 if (is_tm_pure_call (stmt
))
4741 if (is_tm_callable (fndecl
))
4744 if (cgraph_node::local_info (fndecl
)->tm_may_enter_irr
)
4745 error_at (gimple_location (stmt
),
4746 "unsafe function call %qD within "
4747 "atomic transaction", fndecl
);
4754 /* Return a transactional mangled name for the DECL_ASSEMBLER_NAME in
4755 OLD_DECL. The returned value is a freshly malloced pointer that
4756 should be freed by the caller. */
4759 tm_mangle (tree old_asm_id
)
4761 const char *old_asm_name
;
4764 struct demangle_component
*dc
;
4767 /* Determine if the symbol is already a valid C++ mangled name. Do this
4768 even for C, which might be interfacing with C++ code via appropriately
4769 ugly identifiers. */
4770 /* ??? We could probably do just as well checking for "_Z" and be done. */
4771 old_asm_name
= IDENTIFIER_POINTER (old_asm_id
);
4772 dc
= cplus_demangle_v3_components (old_asm_name
, DMGL_NO_OPTS
, &alloc
);
4779 sprintf (length
, "%u", IDENTIFIER_LENGTH (old_asm_id
));
4780 tm_name
= concat ("_ZGTt", length
, old_asm_name
, NULL
);
4784 old_asm_name
+= 2; /* Skip _Z */
4788 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
4789 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
4790 /* Don't play silly games, you! */
4793 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
4794 /* I'd really like to know if we can ever be passed one of
4795 these from the C++ front end. The Logical Thing would
4796 seem that hidden-alias should be outer-most, so that we
4797 get hidden-alias of a transaction-clone and not vice-versa. */
4805 tm_name
= concat ("_ZGTt", old_asm_name
, NULL
);
4809 new_asm_id
= get_identifier (tm_name
);
4816 ipa_tm_mark_force_output_node (struct cgraph_node
*node
)
4818 node
->mark_force_output ();
4819 node
->analyzed
= true;
4823 ipa_tm_mark_forced_by_abi_node (struct cgraph_node
*node
)
4825 node
->forced_by_abi
= true;
4826 node
->analyzed
= true;
4829 /* Callback data for ipa_tm_create_version_alias. */
4830 struct create_version_alias_info
4832 struct cgraph_node
*old_node
;
4836 /* A subroutine of ipa_tm_create_version, called via
4837 cgraph_for_node_and_aliases. Create new tm clones for each of
4838 the existing aliases. */
4840 ipa_tm_create_version_alias (struct cgraph_node
*node
, void *data
)
4842 struct create_version_alias_info
*info
4843 = (struct create_version_alias_info
*)data
;
4844 tree old_decl
, new_decl
, tm_name
;
4845 struct cgraph_node
*new_node
;
4847 if (!node
->cpp_implicit_alias
)
4850 old_decl
= node
->decl
;
4851 tm_name
= tm_mangle (DECL_ASSEMBLER_NAME (old_decl
));
4852 new_decl
= build_decl (DECL_SOURCE_LOCATION (old_decl
),
4853 TREE_CODE (old_decl
), tm_name
,
4854 TREE_TYPE (old_decl
));
4856 SET_DECL_ASSEMBLER_NAME (new_decl
, tm_name
);
4857 SET_DECL_RTL (new_decl
, NULL
);
4859 /* Based loosely on C++'s make_alias_for(). */
4860 TREE_PUBLIC (new_decl
) = TREE_PUBLIC (old_decl
);
4861 DECL_CONTEXT (new_decl
) = DECL_CONTEXT (old_decl
);
4862 DECL_LANG_SPECIFIC (new_decl
) = DECL_LANG_SPECIFIC (old_decl
);
4863 TREE_READONLY (new_decl
) = TREE_READONLY (old_decl
);
4864 DECL_EXTERNAL (new_decl
) = 0;
4865 DECL_ARTIFICIAL (new_decl
) = 1;
4866 TREE_ADDRESSABLE (new_decl
) = 1;
4867 TREE_USED (new_decl
) = 1;
4868 TREE_SYMBOL_REFERENCED (tm_name
) = 1;
4870 /* Perform the same remapping to the comdat group. */
4871 if (DECL_ONE_ONLY (new_decl
))
4872 varpool_node::get (new_decl
)->set_comdat_group
4873 (tm_mangle (decl_comdat_group_id (old_decl
)));
4875 new_node
= cgraph_node::create_same_body_alias (new_decl
, info
->new_decl
);
4876 new_node
->tm_clone
= true;
4877 new_node
->externally_visible
= info
->old_node
->externally_visible
;
4878 new_node
->no_reorder
= info
->old_node
->no_reorder
;
4879 /* ?? Do not traverse aliases here. */
4880 get_cg_data (&node
, false)->clone
= new_node
;
4882 record_tm_clone_pair (old_decl
, new_decl
);
4884 if (info
->old_node
->force_output
4885 || info
->old_node
->ref_list
.first_referring ())
4886 ipa_tm_mark_force_output_node (new_node
);
4887 if (info
->old_node
->forced_by_abi
)
4888 ipa_tm_mark_forced_by_abi_node (new_node
);
4892 /* Create a copy of the function (possibly declaration only) of OLD_NODE,
4893 appropriate for the transactional clone. */
4896 ipa_tm_create_version (struct cgraph_node
*old_node
)
4898 tree new_decl
, old_decl
, tm_name
;
4899 struct cgraph_node
*new_node
;
4901 old_decl
= old_node
->decl
;
4902 new_decl
= copy_node (old_decl
);
4904 /* DECL_ASSEMBLER_NAME needs to be set before we call
4905 cgraph_copy_node_for_versioning below, because cgraph_node will
4906 fill the assembler_name_hash. */
4907 tm_name
= tm_mangle (DECL_ASSEMBLER_NAME (old_decl
));
4908 SET_DECL_ASSEMBLER_NAME (new_decl
, tm_name
);
4909 SET_DECL_RTL (new_decl
, NULL
);
4910 TREE_SYMBOL_REFERENCED (tm_name
) = 1;
4912 /* Perform the same remapping to the comdat group. */
4913 if (DECL_ONE_ONLY (new_decl
))
4914 varpool_node::get (new_decl
)->set_comdat_group
4915 (tm_mangle (DECL_COMDAT_GROUP (old_decl
)));
4917 gcc_assert (!old_node
->ipa_transforms_to_apply
.exists ());
4918 new_node
= old_node
->create_version_clone (new_decl
, vNULL
, NULL
);
4919 new_node
->local
.local
= false;
4920 new_node
->externally_visible
= old_node
->externally_visible
;
4921 new_node
->lowered
= true;
4922 new_node
->tm_clone
= 1;
4923 get_cg_data (&old_node
, true)->clone
= new_node
;
4925 if (old_node
->get_availability () >= AVAIL_INTERPOSABLE
)
4927 /* Remap extern inline to static inline. */
4928 /* ??? Is it worth trying to use make_decl_one_only? */
4929 if (DECL_DECLARED_INLINE_P (new_decl
) && DECL_EXTERNAL (new_decl
))
4931 DECL_EXTERNAL (new_decl
) = 0;
4932 TREE_PUBLIC (new_decl
) = 0;
4933 DECL_WEAK (new_decl
) = 0;
4936 tree_function_versioning (old_decl
, new_decl
,
4941 record_tm_clone_pair (old_decl
, new_decl
);
4943 symtab
->call_cgraph_insertion_hooks (new_node
);
4944 if (old_node
->force_output
4945 || old_node
->ref_list
.first_referring ())
4946 ipa_tm_mark_force_output_node (new_node
);
4947 if (old_node
->forced_by_abi
)
4948 ipa_tm_mark_forced_by_abi_node (new_node
);
4950 /* Do the same thing, but for any aliases of the original node. */
4952 struct create_version_alias_info data
;
4953 data
.old_node
= old_node
;
4954 data
.new_decl
= new_decl
;
4955 old_node
->call_for_symbol_thunks_and_aliases (ipa_tm_create_version_alias
,
4960 /* Construct a call to TM_IRREVOCABLE and insert it at the beginning of BB. */
4963 ipa_tm_insert_irr_call (struct cgraph_node
*node
, struct tm_region
*region
,
4966 gimple_stmt_iterator gsi
;
4969 transaction_subcode_ior (region
, GTMA_MAY_ENTER_IRREVOCABLE
);
4971 g
= gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_IRREVOCABLE
),
4972 1, build_int_cst (NULL_TREE
, MODE_SERIALIRREVOCABLE
));
4974 split_block_after_labels (bb
);
4975 gsi
= gsi_after_labels (bb
);
4976 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
4978 node
->create_edge (cgraph_node::get_create
4979 (builtin_decl_explicit (BUILT_IN_TM_IRREVOCABLE
)),
4981 compute_call_stmt_bb_frequency (node
->decl
,
4985 /* Construct a call to TM_GETTMCLONE and insert it before GSI. */
4988 ipa_tm_insert_gettmclone_call (struct cgraph_node
*node
,
4989 struct tm_region
*region
,
4990 gimple_stmt_iterator
*gsi
, gimple stmt
)
4992 tree gettm_fn
, ret
, old_fn
, callfn
;
4996 old_fn
= gimple_call_fn (stmt
);
4998 if (TREE_CODE (old_fn
) == ADDR_EXPR
)
5000 tree fndecl
= TREE_OPERAND (old_fn
, 0);
5001 tree clone
= get_tm_clone_pair (fndecl
);
5003 /* By transforming the call into a TM_GETTMCLONE, we are
5004 technically taking the address of the original function and
5005 its clone. Explain this so inlining will know this function
5007 cgraph_node::get (fndecl
)->mark_address_taken () ;
5009 cgraph_node::get (clone
)->mark_address_taken ();
5012 safe
= is_tm_safe (TREE_TYPE (old_fn
));
5013 gettm_fn
= builtin_decl_explicit (safe
? BUILT_IN_TM_GETTMCLONE_SAFE
5014 : BUILT_IN_TM_GETTMCLONE_IRR
);
5015 ret
= create_tmp_var (ptr_type_node
, NULL
);
5018 transaction_subcode_ior (region
, GTMA_MAY_ENTER_IRREVOCABLE
);
5020 /* Discard OBJ_TYPE_REF, since we weren't able to fold it. */
5021 if (TREE_CODE (old_fn
) == OBJ_TYPE_REF
)
5022 old_fn
= OBJ_TYPE_REF_EXPR (old_fn
);
5024 g
= gimple_build_call (gettm_fn
, 1, old_fn
);
5025 ret
= make_ssa_name (ret
, g
);
5026 gimple_call_set_lhs (g
, ret
);
5028 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
5030 node
->create_edge (cgraph_node::get_create (gettm_fn
), g
, 0,
5031 compute_call_stmt_bb_frequency (node
->decl
,
5034 /* Cast return value from tm_gettmclone* into appropriate function
5036 callfn
= create_tmp_var (TREE_TYPE (old_fn
), NULL
);
5037 g2
= gimple_build_assign (callfn
,
5038 fold_build1 (NOP_EXPR
, TREE_TYPE (callfn
), ret
));
5039 callfn
= make_ssa_name (callfn
, g2
);
5040 gimple_assign_set_lhs (g2
, callfn
);
5041 gsi_insert_before (gsi
, g2
, GSI_SAME_STMT
);
5043 /* ??? This is a hack to preserve the NOTHROW bit on the call,
5044 which we would have derived from the decl. Failure to save
5045 this bit means we might have to split the basic block. */
5046 if (gimple_call_nothrow_p (stmt
))
5047 gimple_call_set_nothrow (stmt
, true);
5049 gimple_call_set_fn (stmt
, callfn
);
5051 /* Discarding OBJ_TYPE_REF above may produce incompatible LHS and RHS
5052 for a call statement. Fix it. */
5054 tree lhs
= gimple_call_lhs (stmt
);
5055 tree rettype
= TREE_TYPE (gimple_call_fntype (stmt
));
5057 && !useless_type_conversion_p (TREE_TYPE (lhs
), rettype
))
5061 temp
= create_tmp_reg (rettype
, 0);
5062 gimple_call_set_lhs (stmt
, temp
);
5064 g2
= gimple_build_assign (lhs
,
5065 fold_build1 (VIEW_CONVERT_EXPR
,
5066 TREE_TYPE (lhs
), temp
));
5067 gsi_insert_after (gsi
, g2
, GSI_SAME_STMT
);
5072 cgraph_edge
*e
= cgraph_node::get (current_function_decl
)->get_edge (stmt
);
5073 if (e
&& e
->indirect_info
)
5074 e
->indirect_info
->polymorphic
= false;
5079 /* Helper function for ipa_tm_transform_calls*. Given a call
5080 statement in GSI which resides inside transaction REGION, redirect
5081 the call to either its wrapper function, or its clone. */
5084 ipa_tm_transform_calls_redirect (struct cgraph_node
*node
,
5085 struct tm_region
*region
,
5086 gimple_stmt_iterator
*gsi
,
5087 bool *need_ssa_rename_p
)
5089 gimple stmt
= gsi_stmt (*gsi
);
5090 struct cgraph_node
*new_node
;
5091 struct cgraph_edge
*e
= node
->get_edge (stmt
);
5092 tree fndecl
= gimple_call_fndecl (stmt
);
5094 /* For indirect calls, pass the address through the runtime. */
5097 *need_ssa_rename_p
|=
5098 ipa_tm_insert_gettmclone_call (node
, region
, gsi
, stmt
);
5102 /* Handle some TM builtins. Ordinarily these aren't actually generated
5103 at this point, but handling these functions when written in by the
5104 user makes it easier to build unit tests. */
5105 if (flags_from_decl_or_type (fndecl
) & ECF_TM_BUILTIN
)
5108 /* Fixup recursive calls inside clones. */
5109 /* ??? Why did cgraph_copy_node_for_versioning update the call edges
5110 for recursion but not update the call statements themselves? */
5111 if (e
->caller
== e
->callee
&& decl_is_tm_clone (current_function_decl
))
5113 gimple_call_set_fndecl (stmt
, current_function_decl
);
5117 /* If there is a replacement, use it. */
5118 fndecl
= find_tm_replacement_function (fndecl
);
5121 new_node
= cgraph_node::get_create (fndecl
);
5123 /* ??? Mark all transaction_wrap functions tm_may_enter_irr.
5125 We can't do this earlier in record_tm_replacement because
5126 cgraph_remove_unreachable_nodes is called before we inject
5127 references to the node. Further, we can't do this in some
5128 nice central place in ipa_tm_execute because we don't have
5129 the exact list of wrapper functions that would be used.
5130 Marking more wrappers than necessary results in the creation
5131 of unnecessary cgraph_nodes, which can cause some of the
5132 other IPA passes to crash.
5134 We do need to mark these nodes so that we get the proper
5135 result in expand_call_tm. */
5136 /* ??? This seems broken. How is it that we're marking the
5137 CALLEE as may_enter_irr? Surely we should be marking the
5138 CALLER. Also note that find_tm_replacement_function also
5139 contains mappings into the TM runtime, e.g. memcpy. These
5140 we know won't go irrevocable. */
5141 new_node
->local
.tm_may_enter_irr
= 1;
5145 struct tm_ipa_cg_data
*d
;
5146 struct cgraph_node
*tnode
= e
->callee
;
5148 d
= get_cg_data (&tnode
, true);
5149 new_node
= d
->clone
;
5151 /* As we've already skipped pure calls and appropriate builtins,
5152 and we've already marked irrevocable blocks, if we can't come
5153 up with a static replacement, then ask the runtime. */
5154 if (new_node
== NULL
)
5156 *need_ssa_rename_p
|=
5157 ipa_tm_insert_gettmclone_call (node
, region
, gsi
, stmt
);
5161 fndecl
= new_node
->decl
;
5164 e
->redirect_callee (new_node
);
5165 gimple_call_set_fndecl (stmt
, fndecl
);
5168 /* Helper function for ipa_tm_transform_calls. For a given BB,
5169 install calls to tm_irrevocable when IRR_BLOCKS are reached,
5170 redirect other calls to the generated transactional clone. */
5173 ipa_tm_transform_calls_1 (struct cgraph_node
*node
, struct tm_region
*region
,
5174 basic_block bb
, bitmap irr_blocks
)
5176 gimple_stmt_iterator gsi
;
5177 bool need_ssa_rename
= false;
5179 if (irr_blocks
&& bitmap_bit_p (irr_blocks
, bb
->index
))
5181 ipa_tm_insert_irr_call (node
, region
, bb
);
5185 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
5187 gimple stmt
= gsi_stmt (gsi
);
5189 if (!is_gimple_call (stmt
))
5191 if (is_tm_pure_call (stmt
))
5194 /* Redirect edges to the appropriate replacement or clone. */
5195 ipa_tm_transform_calls_redirect (node
, region
, &gsi
, &need_ssa_rename
);
5198 return need_ssa_rename
;
5201 /* Walk the CFG for REGION, beginning at BB. Install calls to
5202 tm_irrevocable when IRR_BLOCKS are reached, redirect other calls to
5203 the generated transactional clone. */
5206 ipa_tm_transform_calls (struct cgraph_node
*node
, struct tm_region
*region
,
5207 basic_block bb
, bitmap irr_blocks
)
5209 bool need_ssa_rename
= false;
5212 auto_vec
<basic_block
> queue
;
5213 bitmap visited_blocks
= BITMAP_ALLOC (NULL
);
5215 queue
.safe_push (bb
);
5221 ipa_tm_transform_calls_1 (node
, region
, bb
, irr_blocks
);
5223 if (irr_blocks
&& bitmap_bit_p (irr_blocks
, bb
->index
))
5226 if (region
&& bitmap_bit_p (region
->exit_blocks
, bb
->index
))
5229 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5230 if (!bitmap_bit_p (visited_blocks
, e
->dest
->index
))
5232 bitmap_set_bit (visited_blocks
, e
->dest
->index
);
5233 queue
.safe_push (e
->dest
);
5236 while (!queue
.is_empty ());
5238 BITMAP_FREE (visited_blocks
);
5240 return need_ssa_rename
;
5243 /* Transform the calls within the TM regions within NODE. */
5246 ipa_tm_transform_transaction (struct cgraph_node
*node
)
5248 struct tm_ipa_cg_data
*d
;
5249 struct tm_region
*region
;
5250 bool need_ssa_rename
= false;
5252 d
= get_cg_data (&node
, true);
5254 push_cfun (DECL_STRUCT_FUNCTION (node
->decl
));
5255 calculate_dominance_info (CDI_DOMINATORS
);
5257 for (region
= d
->all_tm_regions
; region
; region
= region
->next
)
5259 /* If we're sure to go irrevocable, don't transform anything. */
5260 if (d
->irrevocable_blocks_normal
5261 && bitmap_bit_p (d
->irrevocable_blocks_normal
,
5262 region
->entry_block
->index
))
5264 transaction_subcode_ior (region
, GTMA_DOES_GO_IRREVOCABLE
5265 | GTMA_MAY_ENTER_IRREVOCABLE
5266 | GTMA_HAS_NO_INSTRUMENTATION
);
5271 ipa_tm_transform_calls (node
, region
, region
->entry_block
,
5272 d
->irrevocable_blocks_normal
);
5275 if (need_ssa_rename
)
5276 update_ssa (TODO_update_ssa_only_virtuals
);
5281 /* Transform the calls within the transactional clone of NODE. */
5284 ipa_tm_transform_clone (struct cgraph_node
*node
)
5286 struct tm_ipa_cg_data
*d
;
5287 bool need_ssa_rename
;
5289 d
= get_cg_data (&node
, true);
5291 /* If this function makes no calls and has no irrevocable blocks,
5292 then there's nothing to do. */
5293 /* ??? Remove non-aborting top-level transactions. */
5294 if (!node
->callees
&& !node
->indirect_calls
&& !d
->irrevocable_blocks_clone
)
5297 push_cfun (DECL_STRUCT_FUNCTION (d
->clone
->decl
));
5298 calculate_dominance_info (CDI_DOMINATORS
);
5301 ipa_tm_transform_calls (d
->clone
, NULL
,
5302 single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
5303 d
->irrevocable_blocks_clone
);
5305 if (need_ssa_rename
)
5306 update_ssa (TODO_update_ssa_only_virtuals
);
5311 /* Main entry point for the transactional memory IPA pass. */
5314 ipa_tm_execute (void)
5316 cgraph_node_queue tm_callees
= cgraph_node_queue ();
5317 /* List of functions that will go irrevocable. */
5318 cgraph_node_queue irr_worklist
= cgraph_node_queue ();
5320 struct cgraph_node
*node
;
5321 struct tm_ipa_cg_data
*d
;
5322 enum availability a
;
5325 #ifdef ENABLE_CHECKING
5326 cgraph_node::verify_cgraph_nodes ();
5329 bitmap_obstack_initialize (&tm_obstack
);
5330 initialize_original_copy_tables ();
5332 /* For all local functions marked tm_callable, queue them. */
5333 FOR_EACH_DEFINED_FUNCTION (node
)
5334 if (is_tm_callable (node
->decl
)
5335 && node
->get_availability () >= AVAIL_INTERPOSABLE
)
5337 d
= get_cg_data (&node
, true);
5338 maybe_push_queue (node
, &tm_callees
, &d
->in_callee_queue
);
5341 /* For all local reachable functions... */
5342 FOR_EACH_DEFINED_FUNCTION (node
)
5344 && node
->get_availability () >= AVAIL_INTERPOSABLE
)
5346 /* ... marked tm_pure, record that fact for the runtime by
5347 indicating that the pure function is its own tm_callable.
5348 No need to do this if the function's address can't be taken. */
5349 if (is_tm_pure (node
->decl
))
5351 if (!node
->local
.local
)
5352 record_tm_clone_pair (node
->decl
, node
->decl
);
5356 push_cfun (DECL_STRUCT_FUNCTION (node
->decl
));
5357 calculate_dominance_info (CDI_DOMINATORS
);
5359 tm_region_init (NULL
);
5362 d
= get_cg_data (&node
, true);
5364 /* Scan for calls that are in each transaction, and
5365 generate the uninstrumented code path. */
5366 ipa_tm_scan_calls_transaction (d
, &tm_callees
);
5368 /* Put it in the worklist so we can scan the function
5369 later (ipa_tm_scan_irr_function) and mark the
5370 irrevocable blocks. */
5371 maybe_push_queue (node
, &irr_worklist
, &d
->in_worklist
);
5372 d
->want_irr_scan_normal
= true;
5378 /* For every local function on the callee list, scan as if we will be
5379 creating a transactional clone, queueing all new functions we find
5381 for (i
= 0; i
< tm_callees
.length (); ++i
)
5383 node
= tm_callees
[i
];
5384 a
= node
->get_availability ();
5385 d
= get_cg_data (&node
, true);
5387 /* Put it in the worklist so we can scan the function later
5388 (ipa_tm_scan_irr_function) and mark the irrevocable
5390 maybe_push_queue (node
, &irr_worklist
, &d
->in_worklist
);
5392 /* Some callees cannot be arbitrarily cloned. These will always be
5393 irrevocable. Mark these now, so that we need not scan them. */
5394 if (is_tm_irrevocable (node
->decl
))
5395 ipa_tm_note_irrevocable (node
, &irr_worklist
);
5396 else if (a
<= AVAIL_NOT_AVAILABLE
5397 && !is_tm_safe_or_pure (node
->decl
))
5398 ipa_tm_note_irrevocable (node
, &irr_worklist
);
5399 else if (a
>= AVAIL_INTERPOSABLE
)
5401 if (!tree_versionable_function_p (node
->decl
))
5402 ipa_tm_note_irrevocable (node
, &irr_worklist
);
5403 else if (!d
->is_irrevocable
)
5405 /* If this is an alias, make sure its base is queued as well.
5406 we need not scan the callees now, as the base will do. */
5409 node
= cgraph_node::get (node
->thunk
.alias
);
5410 d
= get_cg_data (&node
, true);
5411 maybe_push_queue (node
, &tm_callees
, &d
->in_callee_queue
);
5415 /* Add all nodes called by this function into
5416 tm_callees as well. */
5417 ipa_tm_scan_calls_clone (node
, &tm_callees
);
5422 /* Iterate scans until no more work to be done. Prefer not to use
5423 vec::pop because the worklist tends to follow a breadth-first
5424 search of the callgraph, which should allow convergance with a
5425 minimum number of scans. But we also don't want the worklist
5426 array to grow without bound, so we shift the array up periodically. */
5427 for (i
= 0; i
< irr_worklist
.length (); ++i
)
5429 if (i
> 256 && i
== irr_worklist
.length () / 8)
5431 irr_worklist
.block_remove (0, i
);
5435 node
= irr_worklist
[i
];
5436 d
= get_cg_data (&node
, true);
5437 d
->in_worklist
= false;
5439 if (d
->want_irr_scan_normal
)
5441 d
->want_irr_scan_normal
= false;
5442 ipa_tm_scan_irr_function (node
, false);
5444 if (d
->in_callee_queue
&& ipa_tm_scan_irr_function (node
, true))
5445 ipa_tm_note_irrevocable (node
, &irr_worklist
);
5448 /* For every function on the callee list, collect the tm_may_enter_irr
5450 irr_worklist
.truncate (0);
5451 for (i
= 0; i
< tm_callees
.length (); ++i
)
5453 node
= tm_callees
[i
];
5454 if (ipa_tm_mayenterirr_function (node
))
5456 d
= get_cg_data (&node
, true);
5457 gcc_assert (d
->in_worklist
== false);
5458 maybe_push_queue (node
, &irr_worklist
, &d
->in_worklist
);
5462 /* Propagate the tm_may_enter_irr bit to callers until stable. */
5463 for (i
= 0; i
< irr_worklist
.length (); ++i
)
5465 struct cgraph_node
*caller
;
5466 struct cgraph_edge
*e
;
5467 struct ipa_ref
*ref
;
5469 if (i
> 256 && i
== irr_worklist
.length () / 8)
5471 irr_worklist
.block_remove (0, i
);
5475 node
= irr_worklist
[i
];
5476 d
= get_cg_data (&node
, true);
5477 d
->in_worklist
= false;
5478 node
->local
.tm_may_enter_irr
= true;
5480 /* Propagate back to normal callers. */
5481 for (e
= node
->callers
; e
; e
= e
->next_caller
)
5484 if (!is_tm_safe_or_pure (caller
->decl
)
5485 && !caller
->local
.tm_may_enter_irr
)
5487 d
= get_cg_data (&caller
, true);
5488 maybe_push_queue (caller
, &irr_worklist
, &d
->in_worklist
);
5492 /* Propagate back to referring aliases as well. */
5493 FOR_EACH_ALIAS (node
, ref
)
5495 caller
= dyn_cast
<cgraph_node
*> (ref
->referring
);
5496 if (!caller
->local
.tm_may_enter_irr
)
5498 /* ?? Do not traverse aliases here. */
5499 d
= get_cg_data (&caller
, false);
5500 maybe_push_queue (caller
, &irr_worklist
, &d
->in_worklist
);
5505 /* Now validate all tm_safe functions, and all atomic regions in
5507 FOR_EACH_DEFINED_FUNCTION (node
)
5509 && node
->get_availability () >= AVAIL_INTERPOSABLE
)
5511 d
= get_cg_data (&node
, true);
5512 if (is_tm_safe (node
->decl
))
5513 ipa_tm_diagnose_tm_safe (node
);
5514 else if (d
->all_tm_regions
)
5515 ipa_tm_diagnose_transaction (node
, d
->all_tm_regions
);
5518 /* Create clones. Do those that are not irrevocable and have a
5519 positive call count. Do those publicly visible functions that
5520 the user directed us to clone. */
5521 for (i
= 0; i
< tm_callees
.length (); ++i
)
5525 node
= tm_callees
[i
];
5526 if (node
->cpp_implicit_alias
)
5529 a
= node
->get_availability ();
5530 d
= get_cg_data (&node
, true);
5532 if (a
<= AVAIL_NOT_AVAILABLE
)
5533 doit
= is_tm_callable (node
->decl
);
5534 else if (a
<= AVAIL_AVAILABLE
&& is_tm_callable (node
->decl
))
5536 else if (!d
->is_irrevocable
5537 && d
->tm_callers_normal
+ d
->tm_callers_clone
> 0)
5541 ipa_tm_create_version (node
);
5544 /* Redirect calls to the new clones, and insert irrevocable marks. */
5545 for (i
= 0; i
< tm_callees
.length (); ++i
)
5547 node
= tm_callees
[i
];
5550 d
= get_cg_data (&node
, true);
5552 ipa_tm_transform_clone (node
);
5555 FOR_EACH_DEFINED_FUNCTION (node
)
5557 && node
->get_availability () >= AVAIL_INTERPOSABLE
)
5559 d
= get_cg_data (&node
, true);
5560 if (d
->all_tm_regions
)
5561 ipa_tm_transform_transaction (node
);
5564 /* Free and clear all data structures. */
5565 tm_callees
.release ();
5566 irr_worklist
.release ();
5567 bitmap_obstack_release (&tm_obstack
);
5568 free_original_copy_tables ();
5570 FOR_EACH_FUNCTION (node
)
5573 #ifdef ENABLE_CHECKING
5574 cgraph_node::verify_cgraph_nodes ();
5582 const pass_data pass_data_ipa_tm
=
5584 SIMPLE_IPA_PASS
, /* type */
5586 OPTGROUP_NONE
, /* optinfo_flags */
5587 TV_TRANS_MEM
, /* tv_id */
5588 ( PROP_ssa
| PROP_cfg
), /* properties_required */
5589 0, /* properties_provided */
5590 0, /* properties_destroyed */
5591 0, /* todo_flags_start */
5592 0, /* todo_flags_finish */
5595 class pass_ipa_tm
: public simple_ipa_opt_pass
5598 pass_ipa_tm (gcc::context
*ctxt
)
5599 : simple_ipa_opt_pass (pass_data_ipa_tm
, ctxt
)
5602 /* opt_pass methods: */
5603 virtual bool gate (function
*) { return flag_tm
; }
5604 virtual unsigned int execute (function
*) { return ipa_tm_execute (); }
5606 }; // class pass_ipa_tm
5610 simple_ipa_opt_pass
*
5611 make_pass_ipa_tm (gcc::context
*ctxt
)
5613 return new pass_ipa_tm (ctxt
);
5616 #include "gt-trans-mem.h"