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 unsigned char inner_flags
= DIAG_TM_SAFE
;
760 if (gimple_transaction_subcode (stmt
) & GTMA_IS_RELAXED
)
762 if (d
->block_flags
& DIAG_TM_SAFE
)
763 error_at (gimple_location (stmt
),
764 "relaxed transaction in atomic transaction");
765 else if (d
->func_flags
& DIAG_TM_SAFE
)
766 error_at (gimple_location (stmt
),
767 "relaxed transaction in %<transaction_safe%> function");
768 inner_flags
= DIAG_TM_RELAXED
;
770 else if (gimple_transaction_subcode (stmt
) & GTMA_IS_OUTER
)
773 error_at (gimple_location (stmt
),
774 "outer transaction in transaction");
775 else if (d
->func_flags
& DIAG_TM_OUTER
)
776 error_at (gimple_location (stmt
),
777 "outer transaction in "
778 "%<transaction_may_cancel_outer%> function");
779 else if (d
->func_flags
& DIAG_TM_SAFE
)
780 error_at (gimple_location (stmt
),
781 "outer transaction in %<transaction_safe%> function");
782 inner_flags
|= DIAG_TM_OUTER
;
785 *handled_ops_p
= true;
786 if (gimple_transaction_body (stmt
))
788 struct walk_stmt_info wi_inner
;
789 struct diagnose_tm d_inner
;
791 memset (&d_inner
, 0, sizeof (d_inner
));
792 d_inner
.func_flags
= d
->func_flags
;
793 d_inner
.block_flags
= d
->block_flags
| inner_flags
;
794 d_inner
.summary_flags
= d_inner
.func_flags
| d_inner
.block_flags
;
796 memset (&wi_inner
, 0, sizeof (wi_inner
));
797 wi_inner
.info
= &d_inner
;
799 walk_gimple_seq (gimple_transaction_body (stmt
),
800 diagnose_tm_1
, diagnose_tm_1_op
, &wi_inner
);
813 diagnose_tm_blocks (void)
815 struct walk_stmt_info wi
;
816 struct diagnose_tm d
;
818 memset (&d
, 0, sizeof (d
));
819 if (is_tm_may_cancel_outer (current_function_decl
))
820 d
.func_flags
= DIAG_TM_OUTER
| DIAG_TM_SAFE
;
821 else if (is_tm_safe (current_function_decl
))
822 d
.func_flags
= DIAG_TM_SAFE
;
823 d
.summary_flags
= d
.func_flags
;
825 memset (&wi
, 0, sizeof (wi
));
828 walk_gimple_seq (gimple_body (current_function_decl
),
829 diagnose_tm_1
, diagnose_tm_1_op
, &wi
);
836 const pass_data pass_data_diagnose_tm_blocks
=
838 GIMPLE_PASS
, /* type */
839 "*diagnose_tm_blocks", /* name */
840 OPTGROUP_NONE
, /* optinfo_flags */
841 true, /* has_execute */
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
)
1602 gimple g
, stmt
= gsi_stmt (*gsi
);
1603 unsigned int *outer_state
= (unsigned int *) wi
->info
;
1604 unsigned int this_state
= 0;
1605 struct walk_stmt_info this_wi
;
1607 /* First, lower the body. The scanning that we do inside gives
1608 us some idea of what we're dealing with. */
1609 memset (&this_wi
, 0, sizeof (this_wi
));
1610 this_wi
.info
= (void *) &this_state
;
1611 walk_gimple_seq_mod (gimple_transaction_body_ptr (stmt
),
1612 lower_sequence_tm
, NULL
, &this_wi
);
1614 /* If there was absolutely nothing transaction related inside the
1615 transaction, we may elide it. Likewise if this is a nested
1616 transaction and does not contain an abort. */
1618 || (!(this_state
& GTMA_HAVE_ABORT
) && outer_state
!= NULL
))
1621 *outer_state
|= this_state
;
1623 gsi_insert_seq_before (gsi
, gimple_transaction_body (stmt
),
1625 gimple_transaction_set_body (stmt
, NULL
);
1627 gsi_remove (gsi
, true);
1628 wi
->removed_stmt
= true;
1632 /* Wrap the body of the transaction in a try-finally node so that
1633 the commit call is always properly called. */
1634 g
= gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_COMMIT
), 0);
1635 if (flag_exceptions
)
1638 gimple_seq n_seq
, e_seq
;
1640 n_seq
= gimple_seq_alloc_with_stmt (g
);
1643 g
= gimple_build_call (builtin_decl_explicit (BUILT_IN_EH_POINTER
),
1644 1, integer_zero_node
);
1645 ptr
= create_tmp_var (ptr_type_node
, NULL
);
1646 gimple_call_set_lhs (g
, ptr
);
1647 gimple_seq_add_stmt (&e_seq
, g
);
1649 g
= gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_COMMIT_EH
),
1651 gimple_seq_add_stmt (&e_seq
, g
);
1653 g
= gimple_build_eh_else (n_seq
, e_seq
);
1656 g
= gimple_build_try (gimple_transaction_body (stmt
),
1657 gimple_seq_alloc_with_stmt (g
), GIMPLE_TRY_FINALLY
);
1658 gsi_insert_after (gsi
, g
, GSI_CONTINUE_LINKING
);
1660 gimple_transaction_set_body (stmt
, NULL
);
1662 /* If the transaction calls abort or if this is an outer transaction,
1663 add an "over" label afterwards. */
1664 if ((this_state
& (GTMA_HAVE_ABORT
))
1665 || (gimple_transaction_subcode (stmt
) & GTMA_IS_OUTER
))
1667 tree label
= create_artificial_label (UNKNOWN_LOCATION
);
1668 gimple_transaction_set_label (stmt
, label
);
1669 gsi_insert_after (gsi
, gimple_build_label (label
), GSI_CONTINUE_LINKING
);
1672 /* Record the set of operations found for use later. */
1673 this_state
|= gimple_transaction_subcode (stmt
) & GTMA_DECLARATION_MASK
;
1674 gimple_transaction_set_subcode (stmt
, this_state
);
1677 /* Iterate through the statements in the sequence, lowering them all
1678 as appropriate for being in a transaction. */
1681 lower_sequence_tm (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1682 struct walk_stmt_info
*wi
)
1684 unsigned int *state
= (unsigned int *) wi
->info
;
1685 gimple stmt
= gsi_stmt (*gsi
);
1687 *handled_ops_p
= true;
1688 switch (gimple_code (stmt
))
1691 /* Only memory reads/writes need to be instrumented. */
1692 if (gimple_assign_single_p (stmt
))
1693 examine_assign_tm (state
, gsi
);
1697 examine_call_tm (state
, gsi
);
1701 *state
|= GTMA_MAY_ENTER_IRREVOCABLE
;
1704 case GIMPLE_TRANSACTION
:
1705 lower_transaction (gsi
, wi
);
1709 *handled_ops_p
= !gimple_has_substatements (stmt
);
1716 /* Iterate through the statements in the sequence, lowering them all
1717 as appropriate for being outside of a transaction. */
1720 lower_sequence_no_tm (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1721 struct walk_stmt_info
* wi
)
1723 gimple stmt
= gsi_stmt (*gsi
);
1725 if (gimple_code (stmt
) == GIMPLE_TRANSACTION
)
1727 *handled_ops_p
= true;
1728 lower_transaction (gsi
, wi
);
1731 *handled_ops_p
= !gimple_has_substatements (stmt
);
1736 /* Main entry point for flattening GIMPLE_TRANSACTION constructs. After
1737 this, GIMPLE_TRANSACTION nodes still exist, but the nested body has
1738 been moved out, and all the data required for constructing a proper
1739 CFG has been recorded. */
1742 execute_lower_tm (void)
1744 struct walk_stmt_info wi
;
1747 /* Transactional clones aren't created until a later pass. */
1748 gcc_assert (!decl_is_tm_clone (current_function_decl
));
1750 body
= gimple_body (current_function_decl
);
1751 memset (&wi
, 0, sizeof (wi
));
1752 walk_gimple_seq_mod (&body
, lower_sequence_no_tm
, NULL
, &wi
);
1753 gimple_set_body (current_function_decl
, body
);
1760 const pass_data pass_data_lower_tm
=
1762 GIMPLE_PASS
, /* type */
1763 "tmlower", /* name */
1764 OPTGROUP_NONE
, /* optinfo_flags */
1765 true, /* has_execute */
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. */
1799 /* Link to the next unnested transaction. */
1800 struct tm_region
*next
;
1802 /* Link to the next inner transaction. */
1803 struct tm_region
*inner
;
1805 /* Link to the next outer transaction. */
1806 struct tm_region
*outer
;
1808 /* The GIMPLE_TRANSACTION statement beginning this transaction.
1809 After TM_MARK, this gets replaced by a call to
1810 BUILT_IN_TM_START. */
1811 gimple transaction_stmt
;
1813 /* After TM_MARK expands the GIMPLE_TRANSACTION into a call to
1814 BUILT_IN_TM_START, this field is true if the transaction is an
1815 outer transaction. */
1816 bool original_transaction_was_outer
;
1818 /* Return value from BUILT_IN_TM_START. */
1821 /* The entry block to this region. This will always be the first
1822 block of the body of the transaction. */
1823 basic_block entry_block
;
1825 /* The first block after an expanded call to _ITM_beginTransaction. */
1826 basic_block restart_block
;
1828 /* The set of all blocks that end the region; NULL if only EXIT_BLOCK.
1829 These blocks are still a part of the region (i.e., the border is
1830 inclusive). Note that this set is only complete for paths in the CFG
1831 starting at ENTRY_BLOCK, and that there is no exit block recorded for
1832 the edge to the "over" label. */
1835 /* The set of all blocks that have an TM_IRREVOCABLE call. */
1839 typedef struct tm_region
*tm_region_p
;
1841 /* True if there are pending edge statements to be committed for the
1842 current function being scanned in the tmmark pass. */
1843 bool pending_edge_inserts_p
;
1845 static struct tm_region
*all_tm_regions
;
1846 static bitmap_obstack tm_obstack
;
1849 /* A subroutine of tm_region_init. Record the existence of the
1850 GIMPLE_TRANSACTION statement in a tree of tm_region elements. */
1852 static struct tm_region
*
1853 tm_region_init_0 (struct tm_region
*outer
, basic_block bb
, gimple stmt
)
1855 struct tm_region
*region
;
1857 region
= (struct tm_region
*)
1858 obstack_alloc (&tm_obstack
.obstack
, sizeof (struct tm_region
));
1862 region
->next
= outer
->inner
;
1863 outer
->inner
= region
;
1867 region
->next
= all_tm_regions
;
1868 all_tm_regions
= region
;
1870 region
->inner
= NULL
;
1871 region
->outer
= outer
;
1873 region
->transaction_stmt
= stmt
;
1874 region
->original_transaction_was_outer
= false;
1875 region
->tm_state
= NULL
;
1877 /* There are either one or two edges out of the block containing
1878 the GIMPLE_TRANSACTION, one to the actual region and one to the
1879 "over" label if the region contains an abort. The former will
1880 always be the one marked FALLTHRU. */
1881 region
->entry_block
= FALLTHRU_EDGE (bb
)->dest
;
1883 region
->exit_blocks
= BITMAP_ALLOC (&tm_obstack
);
1884 region
->irr_blocks
= BITMAP_ALLOC (&tm_obstack
);
1889 /* A subroutine of tm_region_init. Record all the exit and
1890 irrevocable blocks in BB into the region's exit_blocks and
1891 irr_blocks bitmaps. Returns the new region being scanned. */
1893 static struct tm_region
*
1894 tm_region_init_1 (struct tm_region
*region
, basic_block bb
)
1896 gimple_stmt_iterator gsi
;
1900 || (!region
->irr_blocks
&& !region
->exit_blocks
))
1903 /* Check to see if this is the end of a region by seeing if it
1904 contains a call to __builtin_tm_commit{,_eh}. Note that the
1905 outermost region for DECL_IS_TM_CLONE need not collect this. */
1906 for (gsi
= gsi_last_bb (bb
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
1909 if (gimple_code (g
) == GIMPLE_CALL
)
1911 tree fn
= gimple_call_fndecl (g
);
1912 if (fn
&& DECL_BUILT_IN_CLASS (fn
) == BUILT_IN_NORMAL
)
1914 if ((DECL_FUNCTION_CODE (fn
) == BUILT_IN_TM_COMMIT
1915 || DECL_FUNCTION_CODE (fn
) == BUILT_IN_TM_COMMIT_EH
)
1916 && region
->exit_blocks
)
1918 bitmap_set_bit (region
->exit_blocks
, bb
->index
);
1919 region
= region
->outer
;
1922 if (DECL_FUNCTION_CODE (fn
) == BUILT_IN_TM_IRREVOCABLE
)
1923 bitmap_set_bit (region
->irr_blocks
, bb
->index
);
1930 /* Collect all of the transaction regions within the current function
1931 and record them in ALL_TM_REGIONS. The REGION parameter may specify
1932 an "outermost" region for use by tm clones. */
1935 tm_region_init (struct tm_region
*region
)
1941 auto_vec
<basic_block
> queue
;
1942 bitmap visited_blocks
= BITMAP_ALLOC (NULL
);
1943 struct tm_region
*old_region
;
1944 auto_vec
<tm_region_p
> bb_regions
;
1946 all_tm_regions
= region
;
1947 bb
= single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
1949 /* We could store this information in bb->aux, but we may get called
1950 through get_all_tm_blocks() from another pass that may be already
1952 bb_regions
.safe_grow_cleared (last_basic_block_for_fn (cfun
));
1954 queue
.safe_push (bb
);
1955 bb_regions
[bb
->index
] = region
;
1959 region
= bb_regions
[bb
->index
];
1960 bb_regions
[bb
->index
] = NULL
;
1962 /* Record exit and irrevocable blocks. */
1963 region
= tm_region_init_1 (region
, bb
);
1965 /* Check for the last statement in the block beginning a new region. */
1967 old_region
= region
;
1968 if (g
&& gimple_code (g
) == GIMPLE_TRANSACTION
)
1969 region
= tm_region_init_0 (region
, bb
, g
);
1971 /* Process subsequent blocks. */
1972 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1973 if (!bitmap_bit_p (visited_blocks
, e
->dest
->index
))
1975 bitmap_set_bit (visited_blocks
, e
->dest
->index
);
1976 queue
.safe_push (e
->dest
);
1978 /* If the current block started a new region, make sure that only
1979 the entry block of the new region is associated with this region.
1980 Other successors are still part of the old region. */
1981 if (old_region
!= region
&& e
->dest
!= region
->entry_block
)
1982 bb_regions
[e
->dest
->index
] = old_region
;
1984 bb_regions
[e
->dest
->index
] = region
;
1987 while (!queue
.is_empty ());
1988 BITMAP_FREE (visited_blocks
);
1991 /* The "gate" function for all transactional memory expansion and optimization
1992 passes. We collect region information for each top-level transaction, and
1993 if we don't find any, we skip all of the TM passes. Each region will have
1994 all of the exit blocks recorded, and the originating statement. */
2002 calculate_dominance_info (CDI_DOMINATORS
);
2003 bitmap_obstack_initialize (&tm_obstack
);
2005 /* If the function is a TM_CLONE, then the entire function is the region. */
2006 if (decl_is_tm_clone (current_function_decl
))
2008 struct tm_region
*region
= (struct tm_region
*)
2009 obstack_alloc (&tm_obstack
.obstack
, sizeof (struct tm_region
));
2010 memset (region
, 0, sizeof (*region
));
2011 region
->entry_block
= single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
2012 /* For a clone, the entire function is the region. But even if
2013 we don't need to record any exit blocks, we may need to
2014 record irrevocable blocks. */
2015 region
->irr_blocks
= BITMAP_ALLOC (&tm_obstack
);
2017 tm_region_init (region
);
2021 tm_region_init (NULL
);
2023 /* If we didn't find any regions, cleanup and skip the whole tree
2024 of tm-related optimizations. */
2025 if (all_tm_regions
== NULL
)
2027 bitmap_obstack_release (&tm_obstack
);
2037 const pass_data pass_data_tm_init
=
2039 GIMPLE_PASS
, /* type */
2040 "*tminit", /* name */
2041 OPTGROUP_NONE
, /* optinfo_flags */
2042 false, /* has_execute */
2043 TV_TRANS_MEM
, /* tv_id */
2044 ( PROP_ssa
| PROP_cfg
), /* properties_required */
2045 0, /* properties_provided */
2046 0, /* properties_destroyed */
2047 0, /* todo_flags_start */
2048 0, /* todo_flags_finish */
2051 class pass_tm_init
: public gimple_opt_pass
2054 pass_tm_init (gcc::context
*ctxt
)
2055 : gimple_opt_pass (pass_data_tm_init
, ctxt
)
2058 /* opt_pass methods: */
2059 virtual bool gate (function
*) { return gate_tm_init (); }
2061 }; // class pass_tm_init
2066 make_pass_tm_init (gcc::context
*ctxt
)
2068 return new pass_tm_init (ctxt
);
2071 /* Add FLAGS to the GIMPLE_TRANSACTION subcode for the transaction region
2072 represented by STATE. */
2075 transaction_subcode_ior (struct tm_region
*region
, unsigned flags
)
2077 if (region
&& region
->transaction_stmt
)
2079 flags
|= gimple_transaction_subcode (region
->transaction_stmt
);
2080 gimple_transaction_set_subcode (region
->transaction_stmt
, flags
);
2084 /* Construct a memory load in a transactional context. Return the
2085 gimple statement performing the load, or NULL if there is no
2086 TM_LOAD builtin of the appropriate size to do the load.
2088 LOC is the location to use for the new statement(s). */
2091 build_tm_load (location_t loc
, tree lhs
, tree rhs
, gimple_stmt_iterator
*gsi
)
2093 enum built_in_function code
= END_BUILTINS
;
2094 tree t
, type
= TREE_TYPE (rhs
), decl
;
2097 if (type
== float_type_node
)
2098 code
= BUILT_IN_TM_LOAD_FLOAT
;
2099 else if (type
== double_type_node
)
2100 code
= BUILT_IN_TM_LOAD_DOUBLE
;
2101 else if (type
== long_double_type_node
)
2102 code
= BUILT_IN_TM_LOAD_LDOUBLE
;
2103 else if (TYPE_SIZE_UNIT (type
) != NULL
2104 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type
)))
2106 switch (tree_to_uhwi (TYPE_SIZE_UNIT (type
)))
2109 code
= BUILT_IN_TM_LOAD_1
;
2112 code
= BUILT_IN_TM_LOAD_2
;
2115 code
= BUILT_IN_TM_LOAD_4
;
2118 code
= BUILT_IN_TM_LOAD_8
;
2123 if (code
== END_BUILTINS
)
2125 decl
= targetm
.vectorize
.builtin_tm_load (type
);
2130 decl
= builtin_decl_explicit (code
);
2132 t
= gimplify_addr (gsi
, rhs
);
2133 gcall
= gimple_build_call (decl
, 1, t
);
2134 gimple_set_location (gcall
, loc
);
2136 t
= TREE_TYPE (TREE_TYPE (decl
));
2137 if (useless_type_conversion_p (type
, t
))
2139 gimple_call_set_lhs (gcall
, lhs
);
2140 gsi_insert_before (gsi
, gcall
, GSI_SAME_STMT
);
2147 temp
= create_tmp_reg (t
, NULL
);
2148 gimple_call_set_lhs (gcall
, temp
);
2149 gsi_insert_before (gsi
, gcall
, GSI_SAME_STMT
);
2151 t
= fold_build1 (VIEW_CONVERT_EXPR
, type
, temp
);
2152 g
= gimple_build_assign (lhs
, t
);
2153 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
2160 /* Similarly for storing TYPE in a transactional context. */
2163 build_tm_store (location_t loc
, tree lhs
, tree rhs
, gimple_stmt_iterator
*gsi
)
2165 enum built_in_function code
= END_BUILTINS
;
2166 tree t
, fn
, type
= TREE_TYPE (rhs
), simple_type
;
2169 if (type
== float_type_node
)
2170 code
= BUILT_IN_TM_STORE_FLOAT
;
2171 else if (type
== double_type_node
)
2172 code
= BUILT_IN_TM_STORE_DOUBLE
;
2173 else if (type
== long_double_type_node
)
2174 code
= BUILT_IN_TM_STORE_LDOUBLE
;
2175 else if (TYPE_SIZE_UNIT (type
) != NULL
2176 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type
)))
2178 switch (tree_to_uhwi (TYPE_SIZE_UNIT (type
)))
2181 code
= BUILT_IN_TM_STORE_1
;
2184 code
= BUILT_IN_TM_STORE_2
;
2187 code
= BUILT_IN_TM_STORE_4
;
2190 code
= BUILT_IN_TM_STORE_8
;
2195 if (code
== END_BUILTINS
)
2197 fn
= targetm
.vectorize
.builtin_tm_store (type
);
2202 fn
= builtin_decl_explicit (code
);
2204 simple_type
= TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn
))));
2206 if (TREE_CODE (rhs
) == CONSTRUCTOR
)
2208 /* Handle the easy initialization to zero. */
2209 if (!CONSTRUCTOR_ELTS (rhs
))
2210 rhs
= build_int_cst (simple_type
, 0);
2213 /* ...otherwise punt to the caller and probably use
2214 BUILT_IN_TM_MEMMOVE, because we can't wrap a
2215 VIEW_CONVERT_EXPR around a CONSTRUCTOR (below) and produce
2220 else if (!useless_type_conversion_p (simple_type
, type
))
2225 temp
= create_tmp_reg (simple_type
, NULL
);
2226 t
= fold_build1 (VIEW_CONVERT_EXPR
, simple_type
, rhs
);
2227 g
= gimple_build_assign (temp
, t
);
2228 gimple_set_location (g
, loc
);
2229 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
2234 t
= gimplify_addr (gsi
, lhs
);
2235 gcall
= gimple_build_call (fn
, 2, t
, rhs
);
2236 gimple_set_location (gcall
, loc
);
2237 gsi_insert_before (gsi
, gcall
, GSI_SAME_STMT
);
2243 /* Expand an assignment statement into transactional builtins. */
2246 expand_assign_tm (struct tm_region
*region
, gimple_stmt_iterator
*gsi
)
2248 gimple stmt
= gsi_stmt (*gsi
);
2249 location_t loc
= gimple_location (stmt
);
2250 tree lhs
= gimple_assign_lhs (stmt
);
2251 tree rhs
= gimple_assign_rhs1 (stmt
);
2252 bool store_p
= requires_barrier (region
->entry_block
, lhs
, NULL
);
2253 bool load_p
= requires_barrier (region
->entry_block
, rhs
, NULL
);
2254 gimple gcall
= NULL
;
2256 if (!load_p
&& !store_p
)
2258 /* Add thread private addresses to log if applicable. */
2259 requires_barrier (region
->entry_block
, lhs
, stmt
);
2264 // Remove original load/store statement.
2265 gsi_remove (gsi
, true);
2267 if (load_p
&& !store_p
)
2269 transaction_subcode_ior (region
, GTMA_HAVE_LOAD
);
2270 gcall
= build_tm_load (loc
, lhs
, rhs
, gsi
);
2272 else if (store_p
&& !load_p
)
2274 transaction_subcode_ior (region
, GTMA_HAVE_STORE
);
2275 gcall
= build_tm_store (loc
, lhs
, rhs
, gsi
);
2279 tree lhs_addr
, rhs_addr
, tmp
;
2282 transaction_subcode_ior (region
, GTMA_HAVE_LOAD
);
2284 transaction_subcode_ior (region
, GTMA_HAVE_STORE
);
2286 /* ??? Figure out if there's any possible overlap between the LHS
2287 and the RHS and if not, use MEMCPY. */
2289 if (load_p
&& is_gimple_reg (lhs
))
2291 tmp
= create_tmp_var (TREE_TYPE (lhs
), NULL
);
2292 lhs_addr
= build_fold_addr_expr (tmp
);
2297 lhs_addr
= gimplify_addr (gsi
, lhs
);
2299 rhs_addr
= gimplify_addr (gsi
, rhs
);
2300 gcall
= gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_MEMMOVE
),
2301 3, lhs_addr
, rhs_addr
,
2302 TYPE_SIZE_UNIT (TREE_TYPE (lhs
)));
2303 gimple_set_location (gcall
, loc
);
2304 gsi_insert_before (gsi
, gcall
, GSI_SAME_STMT
);
2308 gcall
= gimple_build_assign (lhs
, tmp
);
2309 gsi_insert_before (gsi
, gcall
, GSI_SAME_STMT
);
2313 /* Now that we have the load/store in its instrumented form, add
2314 thread private addresses to the log if applicable. */
2316 requires_barrier (region
->entry_block
, lhs
, gcall
);
2318 // The calls to build_tm_{store,load} above inserted the instrumented
2319 // call into the stream.
2320 // gsi_insert_before (gsi, gcall, GSI_SAME_STMT);
2324 /* Expand a call statement as appropriate for a transaction. That is,
2325 either verify that the call does not affect the transaction, or
2326 redirect the call to a clone that handles transactions, or change
2327 the transaction state to IRREVOCABLE. Return true if the call is
2328 one of the builtins that end a transaction. */
2331 expand_call_tm (struct tm_region
*region
,
2332 gimple_stmt_iterator
*gsi
)
2334 gimple stmt
= gsi_stmt (*gsi
);
2335 tree lhs
= gimple_call_lhs (stmt
);
2337 struct cgraph_node
*node
;
2338 bool retval
= false;
2340 fn_decl
= gimple_call_fndecl (stmt
);
2342 if (fn_decl
== builtin_decl_explicit (BUILT_IN_TM_MEMCPY
)
2343 || fn_decl
== builtin_decl_explicit (BUILT_IN_TM_MEMMOVE
))
2344 transaction_subcode_ior (region
, GTMA_HAVE_STORE
| GTMA_HAVE_LOAD
);
2345 if (fn_decl
== builtin_decl_explicit (BUILT_IN_TM_MEMSET
))
2346 transaction_subcode_ior (region
, GTMA_HAVE_STORE
);
2348 if (is_tm_pure_call (stmt
))
2352 retval
= is_tm_ending_fndecl (fn_decl
);
2355 /* Assume all non-const/pure calls write to memory, except
2356 transaction ending builtins. */
2357 transaction_subcode_ior (region
, GTMA_HAVE_STORE
);
2360 /* For indirect calls, we already generated a call into the runtime. */
2363 tree fn
= gimple_call_fn (stmt
);
2365 /* We are guaranteed never to go irrevocable on a safe or pure
2366 call, and the pure call was handled above. */
2367 if (is_tm_safe (fn
))
2370 transaction_subcode_ior (region
, GTMA_MAY_ENTER_IRREVOCABLE
);
2375 node
= cgraph_get_node (fn_decl
);
2376 /* All calls should have cgraph here. */
2379 /* We can have a nodeless call here if some pass after IPA-tm
2380 added uninstrumented calls. For example, loop distribution
2381 can transform certain loop constructs into __builtin_mem*
2382 calls. In this case, see if we have a suitable TM
2383 replacement and fill in the gaps. */
2384 gcc_assert (DECL_BUILT_IN_CLASS (fn_decl
) == BUILT_IN_NORMAL
);
2385 enum built_in_function code
= DECL_FUNCTION_CODE (fn_decl
);
2386 gcc_assert (code
== BUILT_IN_MEMCPY
2387 || code
== BUILT_IN_MEMMOVE
2388 || code
== BUILT_IN_MEMSET
);
2390 tree repl
= find_tm_replacement_function (fn_decl
);
2393 gimple_call_set_fndecl (stmt
, repl
);
2395 node
= cgraph_create_node (repl
);
2396 node
->local
.tm_may_enter_irr
= false;
2397 return expand_call_tm (region
, gsi
);
2401 if (node
->local
.tm_may_enter_irr
)
2402 transaction_subcode_ior (region
, GTMA_MAY_ENTER_IRREVOCABLE
);
2404 if (is_tm_abort (fn_decl
))
2406 transaction_subcode_ior (region
, GTMA_HAVE_ABORT
);
2410 /* Instrument the store if needed.
2412 If the assignment happens inside the function call (return slot
2413 optimization), there is no instrumentation to be done, since
2414 the callee should have done the right thing. */
2415 if (lhs
&& requires_barrier (region
->entry_block
, lhs
, stmt
)
2416 && !gimple_call_return_slot_opt_p (stmt
))
2418 tree tmp
= create_tmp_reg (TREE_TYPE (lhs
), NULL
);
2419 location_t loc
= gimple_location (stmt
);
2420 edge fallthru_edge
= NULL
;
2422 /* Remember if the call was going to throw. */
2423 if (stmt_can_throw_internal (stmt
))
2427 basic_block bb
= gimple_bb (stmt
);
2429 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2430 if (e
->flags
& EDGE_FALLTHRU
)
2437 gimple_call_set_lhs (stmt
, tmp
);
2439 stmt
= gimple_build_assign (lhs
, tmp
);
2440 gimple_set_location (stmt
, loc
);
2442 /* We cannot throw in the middle of a BB. If the call was going
2443 to throw, place the instrumentation on the fallthru edge, so
2444 the call remains the last statement in the block. */
2447 gimple_seq fallthru_seq
= gimple_seq_alloc_with_stmt (stmt
);
2448 gimple_stmt_iterator fallthru_gsi
= gsi_start (fallthru_seq
);
2449 expand_assign_tm (region
, &fallthru_gsi
);
2450 gsi_insert_seq_on_edge (fallthru_edge
, fallthru_seq
);
2451 pending_edge_inserts_p
= true;
2455 gsi_insert_after (gsi
, stmt
, GSI_CONTINUE_LINKING
);
2456 expand_assign_tm (region
, gsi
);
2459 transaction_subcode_ior (region
, GTMA_HAVE_STORE
);
2466 /* Expand all statements in BB as appropriate for being inside
2470 expand_block_tm (struct tm_region
*region
, basic_block bb
)
2472 gimple_stmt_iterator gsi
;
2474 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); )
2476 gimple stmt
= gsi_stmt (gsi
);
2477 switch (gimple_code (stmt
))
2480 /* Only memory reads/writes need to be instrumented. */
2481 if (gimple_assign_single_p (stmt
)
2482 && !gimple_clobber_p (stmt
))
2484 expand_assign_tm (region
, &gsi
);
2490 if (expand_call_tm (region
, &gsi
))
2500 if (!gsi_end_p (gsi
))
2505 /* Return the list of basic-blocks in REGION.
2507 STOP_AT_IRREVOCABLE_P is true if caller is uninterested in blocks
2508 following a TM_IRREVOCABLE call.
2510 INCLUDE_UNINSTRUMENTED_P is TRUE if we should include the
2511 uninstrumented code path blocks in the list of basic blocks
2512 returned, false otherwise. */
2514 static vec
<basic_block
>
2515 get_tm_region_blocks (basic_block entry_block
,
2518 bitmap all_region_blocks
,
2519 bool stop_at_irrevocable_p
,
2520 bool include_uninstrumented_p
= true)
2522 vec
<basic_block
> bbs
= vNULL
;
2526 bitmap visited_blocks
= BITMAP_ALLOC (NULL
);
2529 bbs
.safe_push (entry_block
);
2530 bitmap_set_bit (visited_blocks
, entry_block
->index
);
2534 basic_block bb
= bbs
[i
++];
2537 bitmap_bit_p (exit_blocks
, bb
->index
))
2540 if (stop_at_irrevocable_p
2542 && bitmap_bit_p (irr_blocks
, bb
->index
))
2545 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2546 if ((include_uninstrumented_p
2547 || !(e
->flags
& EDGE_TM_UNINSTRUMENTED
))
2548 && !bitmap_bit_p (visited_blocks
, e
->dest
->index
))
2550 bitmap_set_bit (visited_blocks
, e
->dest
->index
);
2551 bbs
.safe_push (e
->dest
);
2554 while (i
< bbs
.length ());
2556 if (all_region_blocks
)
2557 bitmap_ior_into (all_region_blocks
, visited_blocks
);
2559 BITMAP_FREE (visited_blocks
);
2563 // Callback data for collect_bb2reg.
2566 vec
<tm_region_p
> *bb2reg
;
2567 bool include_uninstrumented_p
;
2570 // Callback for expand_regions, collect innermost region data for each bb.
2572 collect_bb2reg (struct tm_region
*region
, void *data
)
2574 struct bb2reg_stuff
*stuff
= (struct bb2reg_stuff
*)data
;
2575 vec
<tm_region_p
> *bb2reg
= stuff
->bb2reg
;
2576 vec
<basic_block
> queue
;
2580 queue
= get_tm_region_blocks (region
->entry_block
,
2581 region
->exit_blocks
,
2584 /*stop_at_irr_p=*/true,
2585 stuff
->include_uninstrumented_p
);
2587 // We expect expand_region to perform a post-order traversal of the region
2588 // tree. Therefore the last region seen for any bb is the innermost.
2589 FOR_EACH_VEC_ELT (queue
, i
, bb
)
2590 (*bb2reg
)[bb
->index
] = region
;
2596 // Returns a vector, indexed by BB->INDEX, of the innermost tm_region to
2597 // which a basic block belongs. Note that we only consider the instrumented
2598 // code paths for the region; the uninstrumented code paths are ignored if
2599 // INCLUDE_UNINSTRUMENTED_P is false.
2601 // ??? This data is very similar to the bb_regions array that is collected
2602 // during tm_region_init. Or, rather, this data is similar to what could
2603 // be used within tm_region_init. The actual computation in tm_region_init
2604 // begins and ends with bb_regions entirely full of NULL pointers, due to
2605 // the way in which pointers are swapped in and out of the array.
2607 // ??? Our callers expect that blocks are not shared between transactions.
2608 // When the optimizers get too smart, and blocks are shared, then during
2609 // the tm_mark phase we'll add log entries to only one of the two transactions,
2610 // and in the tm_edge phase we'll add edges to the CFG that create invalid
2611 // cycles. The symptom being SSA defs that do not dominate their uses.
2612 // Note that the optimizers were locally correct with their transformation,
2613 // as we have no info within the program that suggests that the blocks cannot
2616 // ??? There is currently a hack inside tree-ssa-pre.c to work around the
2617 // only known instance of this block sharing.
2619 static vec
<tm_region_p
>
2620 get_bb_regions_instrumented (bool traverse_clones
,
2621 bool include_uninstrumented_p
)
2623 unsigned n
= last_basic_block_for_fn (cfun
);
2624 struct bb2reg_stuff stuff
;
2625 vec
<tm_region_p
> ret
;
2628 ret
.safe_grow_cleared (n
);
2629 stuff
.bb2reg
= &ret
;
2630 stuff
.include_uninstrumented_p
= include_uninstrumented_p
;
2631 expand_regions (all_tm_regions
, collect_bb2reg
, &stuff
, traverse_clones
);
2636 /* Set the IN_TRANSACTION for all gimple statements that appear in a
2640 compute_transaction_bits (void)
2642 struct tm_region
*region
;
2643 vec
<basic_block
> queue
;
2647 /* ?? Perhaps we need to abstract gate_tm_init further, because we
2648 certainly don't need it to calculate CDI_DOMINATOR info. */
2651 FOR_EACH_BB_FN (bb
, cfun
)
2652 bb
->flags
&= ~BB_IN_TRANSACTION
;
2654 for (region
= all_tm_regions
; region
; region
= region
->next
)
2656 queue
= get_tm_region_blocks (region
->entry_block
,
2657 region
->exit_blocks
,
2660 /*stop_at_irr_p=*/true);
2661 for (i
= 0; queue
.iterate (i
, &bb
); ++i
)
2662 bb
->flags
|= BB_IN_TRANSACTION
;
2667 bitmap_obstack_release (&tm_obstack
);
2670 /* Replace the GIMPLE_TRANSACTION in this region with the corresponding
2671 call to BUILT_IN_TM_START. */
2674 expand_transaction (struct tm_region
*region
, void *data ATTRIBUTE_UNUSED
)
2676 tree tm_start
= builtin_decl_explicit (BUILT_IN_TM_START
);
2677 basic_block transaction_bb
= gimple_bb (region
->transaction_stmt
);
2678 tree tm_state
= region
->tm_state
;
2679 tree tm_state_type
= TREE_TYPE (tm_state
);
2680 edge abort_edge
= NULL
;
2681 edge inst_edge
= NULL
;
2682 edge uninst_edge
= NULL
;
2683 edge fallthru_edge
= NULL
;
2685 // Identify the various successors of the transaction start.
2689 FOR_EACH_EDGE (e
, i
, transaction_bb
->succs
)
2691 if (e
->flags
& EDGE_TM_ABORT
)
2693 else if (e
->flags
& EDGE_TM_UNINSTRUMENTED
)
2697 if (e
->flags
& EDGE_FALLTHRU
)
2702 /* ??? There are plenty of bits here we're not computing. */
2704 int subcode
= gimple_transaction_subcode (region
->transaction_stmt
);
2706 if (subcode
& GTMA_DOES_GO_IRREVOCABLE
)
2707 flags
|= PR_DOESGOIRREVOCABLE
;
2708 if ((subcode
& GTMA_MAY_ENTER_IRREVOCABLE
) == 0)
2709 flags
|= PR_HASNOIRREVOCABLE
;
2710 /* If the transaction does not have an abort in lexical scope and is not
2711 marked as an outer transaction, then it will never abort. */
2712 if ((subcode
& GTMA_HAVE_ABORT
) == 0 && (subcode
& GTMA_IS_OUTER
) == 0)
2713 flags
|= PR_HASNOABORT
;
2714 if ((subcode
& GTMA_HAVE_STORE
) == 0)
2715 flags
|= PR_READONLY
;
2716 if (inst_edge
&& !(subcode
& GTMA_HAS_NO_INSTRUMENTATION
))
2717 flags
|= PR_INSTRUMENTEDCODE
;
2719 flags
|= PR_UNINSTRUMENTEDCODE
;
2720 if (subcode
& GTMA_IS_OUTER
)
2721 region
->original_transaction_was_outer
= true;
2722 tree t
= build_int_cst (tm_state_type
, flags
);
2723 gimple call
= gimple_build_call (tm_start
, 1, t
);
2724 gimple_call_set_lhs (call
, tm_state
);
2725 gimple_set_location (call
, gimple_location (region
->transaction_stmt
));
2727 // Replace the GIMPLE_TRANSACTION with the call to BUILT_IN_TM_START.
2728 gimple_stmt_iterator gsi
= gsi_last_bb (transaction_bb
);
2729 gcc_assert (gsi_stmt (gsi
) == region
->transaction_stmt
);
2730 gsi_insert_before (&gsi
, call
, GSI_SAME_STMT
);
2731 gsi_remove (&gsi
, true);
2732 region
->transaction_stmt
= call
;
2735 // Generate log saves.
2736 if (!tm_log_save_addresses
.is_empty ())
2737 tm_log_emit_saves (region
->entry_block
, transaction_bb
);
2739 // In the beginning, we've no tests to perform on transaction restart.
2740 // Note that after this point, transaction_bb becomes the "most recent
2741 // block containing tests for the transaction".
2742 region
->restart_block
= region
->entry_block
;
2744 // Generate log restores.
2745 if (!tm_log_save_addresses
.is_empty ())
2747 basic_block test_bb
= create_empty_bb (transaction_bb
);
2748 basic_block code_bb
= create_empty_bb (test_bb
);
2749 basic_block join_bb
= create_empty_bb (code_bb
);
2750 add_bb_to_loop (test_bb
, transaction_bb
->loop_father
);
2751 add_bb_to_loop (code_bb
, transaction_bb
->loop_father
);
2752 add_bb_to_loop (join_bb
, transaction_bb
->loop_father
);
2753 if (region
->restart_block
== region
->entry_block
)
2754 region
->restart_block
= test_bb
;
2756 tree t1
= create_tmp_reg (tm_state_type
, NULL
);
2757 tree t2
= build_int_cst (tm_state_type
, A_RESTORELIVEVARIABLES
);
2758 gimple stmt
= gimple_build_assign_with_ops (BIT_AND_EXPR
, t1
,
2760 gimple_stmt_iterator gsi
= gsi_last_bb (test_bb
);
2761 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2763 t2
= build_int_cst (tm_state_type
, 0);
2764 stmt
= gimple_build_cond (NE_EXPR
, t1
, t2
, NULL
, NULL
);
2765 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2767 tm_log_emit_restores (region
->entry_block
, code_bb
);
2769 edge ei
= make_edge (transaction_bb
, test_bb
, EDGE_FALLTHRU
);
2770 edge et
= make_edge (test_bb
, code_bb
, EDGE_TRUE_VALUE
);
2771 edge ef
= make_edge (test_bb
, join_bb
, EDGE_FALSE_VALUE
);
2772 redirect_edge_pred (fallthru_edge
, join_bb
);
2774 join_bb
->frequency
= test_bb
->frequency
= transaction_bb
->frequency
;
2775 join_bb
->count
= test_bb
->count
= transaction_bb
->count
;
2777 ei
->probability
= PROB_ALWAYS
;
2778 et
->probability
= PROB_LIKELY
;
2779 ef
->probability
= PROB_UNLIKELY
;
2780 et
->count
= apply_probability (test_bb
->count
, et
->probability
);
2781 ef
->count
= apply_probability (test_bb
->count
, ef
->probability
);
2783 code_bb
->count
= et
->count
;
2784 code_bb
->frequency
= EDGE_FREQUENCY (et
);
2786 transaction_bb
= join_bb
;
2789 // If we have an ABORT edge, create a test to perform the abort.
2792 basic_block test_bb
= create_empty_bb (transaction_bb
);
2793 add_bb_to_loop (test_bb
, transaction_bb
->loop_father
);
2794 if (region
->restart_block
== region
->entry_block
)
2795 region
->restart_block
= test_bb
;
2797 tree t1
= create_tmp_reg (tm_state_type
, NULL
);
2798 tree t2
= build_int_cst (tm_state_type
, A_ABORTTRANSACTION
);
2799 gimple stmt
= gimple_build_assign_with_ops (BIT_AND_EXPR
, t1
,
2801 gimple_stmt_iterator gsi
= gsi_last_bb (test_bb
);
2802 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2804 t2
= build_int_cst (tm_state_type
, 0);
2805 stmt
= gimple_build_cond (NE_EXPR
, t1
, t2
, NULL
, NULL
);
2806 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2808 edge ei
= make_edge (transaction_bb
, test_bb
, EDGE_FALLTHRU
);
2809 test_bb
->frequency
= transaction_bb
->frequency
;
2810 test_bb
->count
= transaction_bb
->count
;
2811 ei
->probability
= PROB_ALWAYS
;
2813 // Not abort edge. If both are live, chose one at random as we'll
2814 // we'll be fixing that up below.
2815 redirect_edge_pred (fallthru_edge
, test_bb
);
2816 fallthru_edge
->flags
= EDGE_FALSE_VALUE
;
2817 fallthru_edge
->probability
= PROB_VERY_LIKELY
;
2818 fallthru_edge
->count
2819 = apply_probability (test_bb
->count
, fallthru_edge
->probability
);
2822 redirect_edge_pred (abort_edge
, test_bb
);
2823 abort_edge
->flags
= EDGE_TRUE_VALUE
;
2824 abort_edge
->probability
= PROB_VERY_UNLIKELY
;
2826 = apply_probability (test_bb
->count
, abort_edge
->probability
);
2828 transaction_bb
= test_bb
;
2831 // If we have both instrumented and uninstrumented code paths, select one.
2832 if (inst_edge
&& uninst_edge
)
2834 basic_block test_bb
= create_empty_bb (transaction_bb
);
2835 add_bb_to_loop (test_bb
, transaction_bb
->loop_father
);
2836 if (region
->restart_block
== region
->entry_block
)
2837 region
->restart_block
= test_bb
;
2839 tree t1
= create_tmp_reg (tm_state_type
, NULL
);
2840 tree t2
= build_int_cst (tm_state_type
, A_RUNUNINSTRUMENTEDCODE
);
2842 gimple stmt
= gimple_build_assign_with_ops (BIT_AND_EXPR
, t1
,
2844 gimple_stmt_iterator gsi
= gsi_last_bb (test_bb
);
2845 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2847 t2
= build_int_cst (tm_state_type
, 0);
2848 stmt
= gimple_build_cond (NE_EXPR
, t1
, t2
, NULL
, NULL
);
2849 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2851 // Create the edge into test_bb first, as we want to copy values
2852 // out of the fallthru edge.
2853 edge e
= make_edge (transaction_bb
, test_bb
, fallthru_edge
->flags
);
2854 e
->probability
= fallthru_edge
->probability
;
2855 test_bb
->count
= e
->count
= fallthru_edge
->count
;
2856 test_bb
->frequency
= EDGE_FREQUENCY (e
);
2858 // Now update the edges to the inst/uninist implementations.
2859 // For now assume that the paths are equally likely. When using HTM,
2860 // we'll try the uninst path first and fallback to inst path if htm
2861 // buffers are exceeded. Without HTM we start with the inst path and
2862 // use the uninst path when falling back to serial mode.
2863 redirect_edge_pred (inst_edge
, test_bb
);
2864 inst_edge
->flags
= EDGE_FALSE_VALUE
;
2865 inst_edge
->probability
= REG_BR_PROB_BASE
/ 2;
2867 = apply_probability (test_bb
->count
, inst_edge
->probability
);
2869 redirect_edge_pred (uninst_edge
, test_bb
);
2870 uninst_edge
->flags
= EDGE_TRUE_VALUE
;
2871 uninst_edge
->probability
= REG_BR_PROB_BASE
/ 2;
2873 = apply_probability (test_bb
->count
, uninst_edge
->probability
);
2876 // If we have no previous special cases, and we have PHIs at the beginning
2877 // of the atomic region, this means we have a loop at the beginning of the
2878 // atomic region that shares the first block. This can cause problems with
2879 // the transaction restart abnormal edges to be added in the tm_edges pass.
2880 // Solve this by adding a new empty block to receive the abnormal edges.
2881 if (region
->restart_block
== region
->entry_block
2882 && phi_nodes (region
->entry_block
))
2884 basic_block empty_bb
= create_empty_bb (transaction_bb
);
2885 region
->restart_block
= empty_bb
;
2886 add_bb_to_loop (empty_bb
, transaction_bb
->loop_father
);
2888 redirect_edge_pred (fallthru_edge
, empty_bb
);
2889 make_edge (transaction_bb
, empty_bb
, EDGE_FALLTHRU
);
2895 /* Generate the temporary to be used for the return value of
2896 BUILT_IN_TM_START. */
2899 generate_tm_state (struct tm_region
*region
, void *data ATTRIBUTE_UNUSED
)
2901 tree tm_start
= builtin_decl_explicit (BUILT_IN_TM_START
);
2903 create_tmp_reg (TREE_TYPE (TREE_TYPE (tm_start
)), "tm_state");
2905 // Reset the subcode, post optimizations. We'll fill this in
2906 // again as we process blocks.
2907 if (region
->exit_blocks
)
2909 unsigned int subcode
2910 = gimple_transaction_subcode (region
->transaction_stmt
);
2912 if (subcode
& GTMA_DOES_GO_IRREVOCABLE
)
2913 subcode
&= (GTMA_DECLARATION_MASK
| GTMA_DOES_GO_IRREVOCABLE
2914 | GTMA_MAY_ENTER_IRREVOCABLE
2915 | GTMA_HAS_NO_INSTRUMENTATION
);
2917 subcode
&= GTMA_DECLARATION_MASK
;
2918 gimple_transaction_set_subcode (region
->transaction_stmt
, subcode
);
2924 // Propagate flags from inner transactions outwards.
2926 propagate_tm_flags_out (struct tm_region
*region
)
2930 propagate_tm_flags_out (region
->inner
);
2932 if (region
->outer
&& region
->outer
->transaction_stmt
)
2934 unsigned s
= gimple_transaction_subcode (region
->transaction_stmt
);
2935 s
&= (GTMA_HAVE_ABORT
| GTMA_HAVE_LOAD
| GTMA_HAVE_STORE
2936 | GTMA_MAY_ENTER_IRREVOCABLE
);
2937 s
|= gimple_transaction_subcode (region
->outer
->transaction_stmt
);
2938 gimple_transaction_set_subcode (region
->outer
->transaction_stmt
, s
);
2941 propagate_tm_flags_out (region
->next
);
2944 /* Entry point to the MARK phase of TM expansion. Here we replace
2945 transactional memory statements with calls to builtins, and function
2946 calls with their transactional clones (if available). But we don't
2947 yet lower GIMPLE_TRANSACTION or add the transaction restart back-edges. */
2950 execute_tm_mark (void)
2952 pending_edge_inserts_p
= false;
2954 expand_regions (all_tm_regions
, generate_tm_state
, NULL
,
2955 /*traverse_clones=*/true);
2959 vec
<tm_region_p
> bb_regions
2960 = get_bb_regions_instrumented (/*traverse_clones=*/true,
2961 /*include_uninstrumented_p=*/false);
2962 struct tm_region
*r
;
2965 // Expand memory operations into calls into the runtime.
2966 // This collects log entries as well.
2967 FOR_EACH_VEC_ELT (bb_regions
, i
, r
)
2971 if (r
->transaction_stmt
)
2973 unsigned sub
= gimple_transaction_subcode (r
->transaction_stmt
);
2975 /* If we're sure to go irrevocable, there won't be
2976 anything to expand, since the run-time will go
2977 irrevocable right away. */
2978 if (sub
& GTMA_DOES_GO_IRREVOCABLE
2979 && sub
& GTMA_MAY_ENTER_IRREVOCABLE
)
2982 expand_block_tm (r
, BASIC_BLOCK_FOR_FN (cfun
, i
));
2986 bb_regions
.release ();
2988 // Propagate flags from inner transactions outwards.
2989 propagate_tm_flags_out (all_tm_regions
);
2991 // Expand GIMPLE_TRANSACTIONs into calls into the runtime.
2992 expand_regions (all_tm_regions
, expand_transaction
, NULL
,
2993 /*traverse_clones=*/false);
2998 if (pending_edge_inserts_p
)
2999 gsi_commit_edge_inserts ();
3000 free_dominance_info (CDI_DOMINATORS
);
3006 const pass_data pass_data_tm_mark
=
3008 GIMPLE_PASS
, /* type */
3009 "tmmark", /* name */
3010 OPTGROUP_NONE
, /* optinfo_flags */
3011 true, /* has_execute */
3012 TV_TRANS_MEM
, /* tv_id */
3013 ( PROP_ssa
| PROP_cfg
), /* properties_required */
3014 0, /* properties_provided */
3015 0, /* properties_destroyed */
3016 0, /* todo_flags_start */
3017 TODO_update_ssa
, /* todo_flags_finish */
3020 class pass_tm_mark
: public gimple_opt_pass
3023 pass_tm_mark (gcc::context
*ctxt
)
3024 : gimple_opt_pass (pass_data_tm_mark
, ctxt
)
3027 /* opt_pass methods: */
3028 virtual unsigned int execute (function
*) { return execute_tm_mark (); }
3030 }; // class pass_tm_mark
3035 make_pass_tm_mark (gcc::context
*ctxt
)
3037 return new pass_tm_mark (ctxt
);
3041 /* Create an abnormal edge from STMT at iter, splitting the block
3042 as necessary. Adjust *PNEXT as needed for the split block. */
3045 split_bb_make_tm_edge (gimple stmt
, basic_block dest_bb
,
3046 gimple_stmt_iterator iter
, gimple_stmt_iterator
*pnext
)
3048 basic_block bb
= gimple_bb (stmt
);
3049 if (!gsi_one_before_end_p (iter
))
3051 edge e
= split_block (bb
, stmt
);
3052 *pnext
= gsi_start_bb (e
->dest
);
3054 make_edge (bb
, dest_bb
, EDGE_ABNORMAL
);
3056 // Record the need for the edge for the benefit of the rtl passes.
3057 if (cfun
->gimple_df
->tm_restart
== NULL
)
3058 cfun
->gimple_df
->tm_restart
= htab_create_ggc (31, struct_ptr_hash
,
3059 struct_ptr_eq
, ggc_free
);
3061 struct tm_restart_node dummy
;
3063 dummy
.label_or_list
= gimple_block_label (dest_bb
);
3065 void **slot
= htab_find_slot (cfun
->gimple_df
->tm_restart
, &dummy
, INSERT
);
3066 struct tm_restart_node
*n
= (struct tm_restart_node
*) *slot
;
3069 n
= ggc_alloc
<tm_restart_node
> ();
3074 tree old
= n
->label_or_list
;
3075 if (TREE_CODE (old
) == LABEL_DECL
)
3076 old
= tree_cons (NULL
, old
, NULL
);
3077 n
->label_or_list
= tree_cons (NULL
, dummy
.label_or_list
, old
);
3081 /* Split block BB as necessary for every builtin function we added, and
3082 wire up the abnormal back edges implied by the transaction restart. */
3085 expand_block_edges (struct tm_region
*const region
, basic_block bb
)
3087 gimple_stmt_iterator gsi
, next_gsi
;
3089 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi
= next_gsi
)
3091 gimple stmt
= gsi_stmt (gsi
);
3094 gsi_next (&next_gsi
);
3096 // ??? Shouldn't we split for any non-pure, non-irrevocable function?
3097 if (gimple_code (stmt
) != GIMPLE_CALL
3098 || (gimple_call_flags (stmt
) & ECF_TM_BUILTIN
) == 0)
3101 if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt
)) == BUILT_IN_TM_ABORT
)
3103 // If we have a ``_transaction_cancel [[outer]]'', there is only
3104 // one abnormal edge: to the transaction marked OUTER.
3105 // All compiler-generated instances of BUILT_IN_TM_ABORT have a
3106 // constant argument, which we can examine here. Users invoking
3107 // TM_ABORT directly get what they deserve.
3108 tree arg
= gimple_call_arg (stmt
, 0);
3109 if (TREE_CODE (arg
) == INTEGER_CST
3110 && (TREE_INT_CST_LOW (arg
) & AR_OUTERABORT
) != 0
3111 && !decl_is_tm_clone (current_function_decl
))
3113 // Find the GTMA_IS_OUTER transaction.
3114 for (struct tm_region
*o
= region
; o
; o
= o
->outer
)
3115 if (o
->original_transaction_was_outer
)
3117 split_bb_make_tm_edge (stmt
, o
->restart_block
,
3122 // Otherwise, the front-end should have semantically checked
3123 // outer aborts, but in either case the target region is not
3124 // within this function.
3128 // Non-outer, TM aborts have an abnormal edge to the inner-most
3129 // transaction, the one being aborted;
3130 split_bb_make_tm_edge (stmt
, region
->restart_block
, gsi
, &next_gsi
);
3133 // All TM builtins have an abnormal edge to the outer-most transaction.
3134 // We never restart inner transactions. For tm clones, we know a-priori
3135 // that the outer-most transaction is outside the function.
3136 if (decl_is_tm_clone (current_function_decl
))
3139 if (cfun
->gimple_df
->tm_restart
== NULL
)
3140 cfun
->gimple_df
->tm_restart
3141 = htab_create_ggc (31, struct_ptr_hash
, struct_ptr_eq
, ggc_free
);
3143 // All TM builtins have an abnormal edge to the outer-most transaction.
3144 // We never restart inner transactions.
3145 for (struct tm_region
*o
= region
; o
; o
= o
->outer
)
3148 split_bb_make_tm_edge (stmt
, o
->restart_block
, gsi
, &next_gsi
);
3152 // Delete any tail-call annotation that may have been added.
3153 // The tail-call pass may have mis-identified the commit as being
3154 // a candidate because we had not yet added this restart edge.
3155 gimple_call_set_tail (stmt
, false);
3159 /* Entry point to the final expansion of transactional nodes. */
3163 const pass_data pass_data_tm_edges
=
3165 GIMPLE_PASS
, /* type */
3166 "tmedge", /* name */
3167 OPTGROUP_NONE
, /* optinfo_flags */
3168 true, /* has_execute */
3169 TV_TRANS_MEM
, /* tv_id */
3170 ( PROP_ssa
| PROP_cfg
), /* properties_required */
3171 0, /* properties_provided */
3172 0, /* properties_destroyed */
3173 0, /* todo_flags_start */
3174 TODO_update_ssa
, /* todo_flags_finish */
3177 class pass_tm_edges
: public gimple_opt_pass
3180 pass_tm_edges (gcc::context
*ctxt
)
3181 : gimple_opt_pass (pass_data_tm_edges
, ctxt
)
3184 /* opt_pass methods: */
3185 virtual unsigned int execute (function
*);
3187 }; // class pass_tm_edges
3190 pass_tm_edges::execute (function
*fun
)
3192 vec
<tm_region_p
> bb_regions
3193 = get_bb_regions_instrumented (/*traverse_clones=*/false,
3194 /*include_uninstrumented_p=*/true);
3195 struct tm_region
*r
;
3198 FOR_EACH_VEC_ELT (bb_regions
, i
, r
)
3200 expand_block_edges (r
, BASIC_BLOCK_FOR_FN (fun
, i
));
3202 bb_regions
.release ();
3204 /* We've got to release the dominance info now, to indicate that it
3205 must be rebuilt completely. Otherwise we'll crash trying to update
3206 the SSA web in the TODO section following this pass. */
3207 free_dominance_info (CDI_DOMINATORS
);
3208 bitmap_obstack_release (&tm_obstack
);
3209 all_tm_regions
= NULL
;
3217 make_pass_tm_edges (gcc::context
*ctxt
)
3219 return new pass_tm_edges (ctxt
);
3222 /* Helper function for expand_regions. Expand REGION and recurse to
3223 the inner region. Call CALLBACK on each region. CALLBACK returns
3224 NULL to continue the traversal, otherwise a non-null value which
3225 this function will return as well. TRAVERSE_CLONES is true if we
3226 should traverse transactional clones. */
3229 expand_regions_1 (struct tm_region
*region
,
3230 void *(*callback
)(struct tm_region
*, void *),
3232 bool traverse_clones
)
3234 void *retval
= NULL
;
3235 if (region
->exit_blocks
3236 || (traverse_clones
&& decl_is_tm_clone (current_function_decl
)))
3238 retval
= callback (region
, data
);
3244 retval
= expand_regions (region
->inner
, callback
, data
, traverse_clones
);
3251 /* Traverse the regions enclosed and including REGION. Execute
3252 CALLBACK for each region, passing DATA. CALLBACK returns NULL to
3253 continue the traversal, otherwise a non-null value which this
3254 function will return as well. TRAVERSE_CLONES is true if we should
3255 traverse transactional clones. */
3258 expand_regions (struct tm_region
*region
,
3259 void *(*callback
)(struct tm_region
*, void *),
3261 bool traverse_clones
)
3263 void *retval
= NULL
;
3266 retval
= expand_regions_1 (region
, callback
, data
, traverse_clones
);
3269 region
= region
->next
;
3275 /* A unique TM memory operation. */
3276 typedef struct tm_memop
3278 /* Unique ID that all memory operations to the same location have. */
3279 unsigned int value_id
;
3280 /* Address of load/store. */
3284 /* TM memory operation hashtable helpers. */
3286 struct tm_memop_hasher
: typed_free_remove
<tm_memop
>
3288 typedef tm_memop value_type
;
3289 typedef tm_memop compare_type
;
3290 static inline hashval_t
hash (const value_type
*);
3291 static inline bool equal (const value_type
*, const compare_type
*);
3294 /* Htab support. Return a hash value for a `tm_memop'. */
3296 tm_memop_hasher::hash (const value_type
*mem
)
3298 tree addr
= mem
->addr
;
3299 /* We drill down to the SSA_NAME/DECL for the hash, but equality is
3300 actually done with operand_equal_p (see tm_memop_eq). */
3301 if (TREE_CODE (addr
) == ADDR_EXPR
)
3302 addr
= TREE_OPERAND (addr
, 0);
3303 return iterative_hash_expr (addr
, 0);
3306 /* Htab support. Return true if two tm_memop's are the same. */
3308 tm_memop_hasher::equal (const value_type
*mem1
, const compare_type
*mem2
)
3310 return operand_equal_p (mem1
->addr
, mem2
->addr
, 0);
3313 /* Sets for solving data flow equations in the memory optimization pass. */
3314 struct tm_memopt_bitmaps
3316 /* Stores available to this BB upon entry. Basically, stores that
3317 dominate this BB. */
3318 bitmap store_avail_in
;
3319 /* Stores available at the end of this BB. */
3320 bitmap store_avail_out
;
3321 bitmap store_antic_in
;
3322 bitmap store_antic_out
;
3323 /* Reads available to this BB upon entry. Basically, reads that
3324 dominate this BB. */
3325 bitmap read_avail_in
;
3326 /* Reads available at the end of this BB. */
3327 bitmap read_avail_out
;
3328 /* Reads performed in this BB. */
3330 /* Writes performed in this BB. */
3333 /* Temporary storage for pass. */
3334 /* Is the current BB in the worklist? */
3335 bool avail_in_worklist_p
;
3336 /* Have we visited this BB? */
3340 static bitmap_obstack tm_memopt_obstack
;
3342 /* Unique counter for TM loads and stores. Loads and stores of the
3343 same address get the same ID. */
3344 static unsigned int tm_memopt_value_id
;
3345 static hash_table
<tm_memop_hasher
> *tm_memopt_value_numbers
;
3347 #define STORE_AVAIL_IN(BB) \
3348 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_avail_in
3349 #define STORE_AVAIL_OUT(BB) \
3350 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_avail_out
3351 #define STORE_ANTIC_IN(BB) \
3352 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_antic_in
3353 #define STORE_ANTIC_OUT(BB) \
3354 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_antic_out
3355 #define READ_AVAIL_IN(BB) \
3356 ((struct tm_memopt_bitmaps *) ((BB)->aux))->read_avail_in
3357 #define READ_AVAIL_OUT(BB) \
3358 ((struct tm_memopt_bitmaps *) ((BB)->aux))->read_avail_out
3359 #define READ_LOCAL(BB) \
3360 ((struct tm_memopt_bitmaps *) ((BB)->aux))->read_local
3361 #define STORE_LOCAL(BB) \
3362 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_local
3363 #define AVAIL_IN_WORKLIST_P(BB) \
3364 ((struct tm_memopt_bitmaps *) ((BB)->aux))->avail_in_worklist_p
3365 #define BB_VISITED_P(BB) \
3366 ((struct tm_memopt_bitmaps *) ((BB)->aux))->visited_p
3368 /* Given a TM load/store in STMT, return the value number for the address
3372 tm_memopt_value_number (gimple stmt
, enum insert_option op
)
3374 struct tm_memop tmpmem
, *mem
;
3377 gcc_assert (is_tm_load (stmt
) || is_tm_store (stmt
));
3378 tmpmem
.addr
= gimple_call_arg (stmt
, 0);
3379 slot
= tm_memopt_value_numbers
->find_slot (&tmpmem
, op
);
3382 else if (op
== INSERT
)
3384 mem
= XNEW (struct tm_memop
);
3386 mem
->value_id
= tm_memopt_value_id
++;
3387 mem
->addr
= tmpmem
.addr
;
3391 return mem
->value_id
;
3394 /* Accumulate TM memory operations in BB into STORE_LOCAL and READ_LOCAL. */
3397 tm_memopt_accumulate_memops (basic_block bb
)
3399 gimple_stmt_iterator gsi
;
3401 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3403 gimple stmt
= gsi_stmt (gsi
);
3407 if (is_tm_store (stmt
))
3408 bits
= STORE_LOCAL (bb
);
3409 else if (is_tm_load (stmt
))
3410 bits
= READ_LOCAL (bb
);
3414 loc
= tm_memopt_value_number (stmt
, INSERT
);
3415 bitmap_set_bit (bits
, loc
);
3418 fprintf (dump_file
, "TM memopt (%s): value num=%d, BB=%d, addr=",
3419 is_tm_load (stmt
) ? "LOAD" : "STORE", loc
,
3420 gimple_bb (stmt
)->index
);
3421 print_generic_expr (dump_file
, gimple_call_arg (stmt
, 0), 0);
3422 fprintf (dump_file
, "\n");
3427 /* Prettily dump one of the memopt sets. BITS is the bitmap to dump. */
3430 dump_tm_memopt_set (const char *set_name
, bitmap bits
)
3434 const char *comma
= "";
3436 fprintf (dump_file
, "TM memopt: %s: [", set_name
);
3437 EXECUTE_IF_SET_IN_BITMAP (bits
, 0, i
, bi
)
3439 hash_table
<tm_memop_hasher
>::iterator hi
;
3440 struct tm_memop
*mem
= NULL
;
3442 /* Yeah, yeah, yeah. Whatever. This is just for debugging. */
3443 FOR_EACH_HASH_TABLE_ELEMENT (*tm_memopt_value_numbers
, mem
, tm_memop_t
, hi
)
3444 if (mem
->value_id
== i
)
3446 gcc_assert (mem
->value_id
== i
);
3447 fprintf (dump_file
, "%s", comma
);
3449 print_generic_expr (dump_file
, mem
->addr
, 0);
3451 fprintf (dump_file
, "]\n");
3454 /* Prettily dump all of the memopt sets in BLOCKS. */
3457 dump_tm_memopt_sets (vec
<basic_block
> blocks
)
3462 for (i
= 0; blocks
.iterate (i
, &bb
); ++i
)
3464 fprintf (dump_file
, "------------BB %d---------\n", bb
->index
);
3465 dump_tm_memopt_set ("STORE_LOCAL", STORE_LOCAL (bb
));
3466 dump_tm_memopt_set ("READ_LOCAL", READ_LOCAL (bb
));
3467 dump_tm_memopt_set ("STORE_AVAIL_IN", STORE_AVAIL_IN (bb
));
3468 dump_tm_memopt_set ("STORE_AVAIL_OUT", STORE_AVAIL_OUT (bb
));
3469 dump_tm_memopt_set ("READ_AVAIL_IN", READ_AVAIL_IN (bb
));
3470 dump_tm_memopt_set ("READ_AVAIL_OUT", READ_AVAIL_OUT (bb
));
3474 /* Compute {STORE,READ}_AVAIL_IN for the basic block BB. */
3477 tm_memopt_compute_avin (basic_block bb
)
3482 /* Seed with the AVOUT of any predecessor. */
3483 for (ix
= 0; ix
< EDGE_COUNT (bb
->preds
); ix
++)
3485 e
= EDGE_PRED (bb
, ix
);
3486 /* Make sure we have already visited this BB, and is thus
3489 If e->src->aux is NULL, this predecessor is actually on an
3490 enclosing transaction. We only care about the current
3491 transaction, so ignore it. */
3492 if (e
->src
->aux
&& BB_VISITED_P (e
->src
))
3494 bitmap_copy (STORE_AVAIL_IN (bb
), STORE_AVAIL_OUT (e
->src
));
3495 bitmap_copy (READ_AVAIL_IN (bb
), READ_AVAIL_OUT (e
->src
));
3500 for (; ix
< EDGE_COUNT (bb
->preds
); ix
++)
3502 e
= EDGE_PRED (bb
, ix
);
3503 if (e
->src
->aux
&& BB_VISITED_P (e
->src
))
3505 bitmap_and_into (STORE_AVAIL_IN (bb
), STORE_AVAIL_OUT (e
->src
));
3506 bitmap_and_into (READ_AVAIL_IN (bb
), READ_AVAIL_OUT (e
->src
));
3510 BB_VISITED_P (bb
) = true;
3513 /* Compute the STORE_ANTIC_IN for the basic block BB. */
3516 tm_memopt_compute_antin (basic_block bb
)
3521 /* Seed with the ANTIC_OUT of any successor. */
3522 for (ix
= 0; ix
< EDGE_COUNT (bb
->succs
); ix
++)
3524 e
= EDGE_SUCC (bb
, ix
);
3525 /* Make sure we have already visited this BB, and is thus
3527 if (BB_VISITED_P (e
->dest
))
3529 bitmap_copy (STORE_ANTIC_IN (bb
), STORE_ANTIC_OUT (e
->dest
));
3534 for (; ix
< EDGE_COUNT (bb
->succs
); ix
++)
3536 e
= EDGE_SUCC (bb
, ix
);
3537 if (BB_VISITED_P (e
->dest
))
3538 bitmap_and_into (STORE_ANTIC_IN (bb
), STORE_ANTIC_OUT (e
->dest
));
3541 BB_VISITED_P (bb
) = true;
3544 /* Compute the AVAIL sets for every basic block in BLOCKS.
3546 We compute {STORE,READ}_AVAIL_{OUT,IN} as follows:
3548 AVAIL_OUT[bb] = union (AVAIL_IN[bb], LOCAL[bb])
3549 AVAIL_IN[bb] = intersect (AVAIL_OUT[predecessors])
3551 This is basically what we do in lcm's compute_available(), but here
3552 we calculate two sets of sets (one for STOREs and one for READs),
3553 and we work on a region instead of the entire CFG.
3555 REGION is the TM region.
3556 BLOCKS are the basic blocks in the region. */
3559 tm_memopt_compute_available (struct tm_region
*region
,
3560 vec
<basic_block
> blocks
)
3563 basic_block
*worklist
, *qin
, *qout
, *qend
, bb
;
3564 unsigned int qlen
, i
;
3568 /* Allocate a worklist array/queue. Entries are only added to the
3569 list if they were not already on the list. So the size is
3570 bounded by the number of basic blocks in the region. */
3571 qlen
= blocks
.length () - 1;
3572 qin
= qout
= worklist
=
3573 XNEWVEC (basic_block
, qlen
);
3575 /* Put every block in the region on the worklist. */
3576 for (i
= 0; blocks
.iterate (i
, &bb
); ++i
)
3578 /* Seed AVAIL_OUT with the LOCAL set. */
3579 bitmap_ior_into (STORE_AVAIL_OUT (bb
), STORE_LOCAL (bb
));
3580 bitmap_ior_into (READ_AVAIL_OUT (bb
), READ_LOCAL (bb
));
3582 AVAIL_IN_WORKLIST_P (bb
) = true;
3583 /* No need to insert the entry block, since it has an AVIN of
3584 null, and an AVOUT that has already been seeded in. */
3585 if (bb
!= region
->entry_block
)
3589 /* The entry block has been initialized with the local sets. */
3590 BB_VISITED_P (region
->entry_block
) = true;
3593 qend
= &worklist
[qlen
];
3595 /* Iterate until the worklist is empty. */
3598 /* Take the first entry off the worklist. */
3605 /* This block can be added to the worklist again if necessary. */
3606 AVAIL_IN_WORKLIST_P (bb
) = false;
3607 tm_memopt_compute_avin (bb
);
3609 /* Note: We do not add the LOCAL sets here because we already
3610 seeded the AVAIL_OUT sets with them. */
3611 changed
= bitmap_ior_into (STORE_AVAIL_OUT (bb
), STORE_AVAIL_IN (bb
));
3612 changed
|= bitmap_ior_into (READ_AVAIL_OUT (bb
), READ_AVAIL_IN (bb
));
3614 && (region
->exit_blocks
== NULL
3615 || !bitmap_bit_p (region
->exit_blocks
, bb
->index
)))
3616 /* If the out state of this block changed, then we need to add
3617 its successors to the worklist if they are not already in. */
3618 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3619 if (!AVAIL_IN_WORKLIST_P (e
->dest
)
3620 && e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
3623 AVAIL_IN_WORKLIST_P (e
->dest
) = true;
3634 dump_tm_memopt_sets (blocks
);
3637 /* Compute ANTIC sets for every basic block in BLOCKS.
3639 We compute STORE_ANTIC_OUT as follows:
3641 STORE_ANTIC_OUT[bb] = union(STORE_ANTIC_IN[bb], STORE_LOCAL[bb])
3642 STORE_ANTIC_IN[bb] = intersect(STORE_ANTIC_OUT[successors])
3644 REGION is the TM region.
3645 BLOCKS are the basic blocks in the region. */
3648 tm_memopt_compute_antic (struct tm_region
*region
,
3649 vec
<basic_block
> blocks
)
3652 basic_block
*worklist
, *qin
, *qout
, *qend
, bb
;
3657 /* Allocate a worklist array/queue. Entries are only added to the
3658 list if they were not already on the list. So the size is
3659 bounded by the number of basic blocks in the region. */
3660 qin
= qout
= worklist
= XNEWVEC (basic_block
, blocks
.length ());
3662 for (qlen
= 0, i
= blocks
.length () - 1; i
>= 0; --i
)
3666 /* Seed ANTIC_OUT with the LOCAL set. */
3667 bitmap_ior_into (STORE_ANTIC_OUT (bb
), STORE_LOCAL (bb
));
3669 /* Put every block in the region on the worklist. */
3670 AVAIL_IN_WORKLIST_P (bb
) = true;
3671 /* No need to insert exit blocks, since their ANTIC_IN is NULL,
3672 and their ANTIC_OUT has already been seeded in. */
3673 if (region
->exit_blocks
3674 && !bitmap_bit_p (region
->exit_blocks
, bb
->index
))
3681 /* The exit blocks have been initialized with the local sets. */
3682 if (region
->exit_blocks
)
3686 EXECUTE_IF_SET_IN_BITMAP (region
->exit_blocks
, 0, i
, bi
)
3687 BB_VISITED_P (BASIC_BLOCK_FOR_FN (cfun
, i
)) = true;
3691 qend
= &worklist
[qlen
];
3693 /* Iterate until the worklist is empty. */
3696 /* Take the first entry off the worklist. */
3703 /* This block can be added to the worklist again if necessary. */
3704 AVAIL_IN_WORKLIST_P (bb
) = false;
3705 tm_memopt_compute_antin (bb
);
3707 /* Note: We do not add the LOCAL sets here because we already
3708 seeded the ANTIC_OUT sets with them. */
3709 if (bitmap_ior_into (STORE_ANTIC_OUT (bb
), STORE_ANTIC_IN (bb
))
3710 && bb
!= region
->entry_block
)
3711 /* If the out state of this block changed, then we need to add
3712 its predecessors to the worklist if they are not already in. */
3713 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3714 if (!AVAIL_IN_WORKLIST_P (e
->src
))
3717 AVAIL_IN_WORKLIST_P (e
->src
) = true;
3728 dump_tm_memopt_sets (blocks
);
3731 /* Offsets of load variants from TM_LOAD. For example,
3732 BUILT_IN_TM_LOAD_RAR* is an offset of 1 from BUILT_IN_TM_LOAD*.
3733 See gtm-builtins.def. */
3734 #define TRANSFORM_RAR 1
3735 #define TRANSFORM_RAW 2
3736 #define TRANSFORM_RFW 3
3737 /* Offsets of store variants from TM_STORE. */
3738 #define TRANSFORM_WAR 1
3739 #define TRANSFORM_WAW 2
3741 /* Inform about a load/store optimization. */
3744 dump_tm_memopt_transform (gimple stmt
)
3748 fprintf (dump_file
, "TM memopt: transforming: ");
3749 print_gimple_stmt (dump_file
, stmt
, 0, 0);
3750 fprintf (dump_file
, "\n");
3754 /* Perform a read/write optimization. Replaces the TM builtin in STMT
3755 by a builtin that is OFFSET entries down in the builtins table in
3756 gtm-builtins.def. */
3759 tm_memopt_transform_stmt (unsigned int offset
,
3761 gimple_stmt_iterator
*gsi
)
3763 tree fn
= gimple_call_fn (stmt
);
3764 gcc_assert (TREE_CODE (fn
) == ADDR_EXPR
);
3765 TREE_OPERAND (fn
, 0)
3766 = builtin_decl_explicit ((enum built_in_function
)
3767 (DECL_FUNCTION_CODE (TREE_OPERAND (fn
, 0))
3769 gimple_call_set_fn (stmt
, fn
);
3770 gsi_replace (gsi
, stmt
, true);
3771 dump_tm_memopt_transform (stmt
);
3774 /* Perform the actual TM memory optimization transformations in the
3775 basic blocks in BLOCKS. */
3778 tm_memopt_transform_blocks (vec
<basic_block
> blocks
)
3782 gimple_stmt_iterator gsi
;
3784 for (i
= 0; blocks
.iterate (i
, &bb
); ++i
)
3786 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3788 gimple stmt
= gsi_stmt (gsi
);
3789 bitmap read_avail
= READ_AVAIL_IN (bb
);
3790 bitmap store_avail
= STORE_AVAIL_IN (bb
);
3791 bitmap store_antic
= STORE_ANTIC_OUT (bb
);
3794 if (is_tm_simple_load (stmt
))
3796 loc
= tm_memopt_value_number (stmt
, NO_INSERT
);
3797 if (store_avail
&& bitmap_bit_p (store_avail
, loc
))
3798 tm_memopt_transform_stmt (TRANSFORM_RAW
, stmt
, &gsi
);
3799 else if (store_antic
&& bitmap_bit_p (store_antic
, loc
))
3801 tm_memopt_transform_stmt (TRANSFORM_RFW
, stmt
, &gsi
);
3802 bitmap_set_bit (store_avail
, loc
);
3804 else if (read_avail
&& bitmap_bit_p (read_avail
, loc
))
3805 tm_memopt_transform_stmt (TRANSFORM_RAR
, stmt
, &gsi
);
3807 bitmap_set_bit (read_avail
, loc
);
3809 else if (is_tm_simple_store (stmt
))
3811 loc
= tm_memopt_value_number (stmt
, NO_INSERT
);
3812 if (store_avail
&& bitmap_bit_p (store_avail
, loc
))
3813 tm_memopt_transform_stmt (TRANSFORM_WAW
, stmt
, &gsi
);
3816 if (read_avail
&& bitmap_bit_p (read_avail
, loc
))
3817 tm_memopt_transform_stmt (TRANSFORM_WAR
, stmt
, &gsi
);
3818 bitmap_set_bit (store_avail
, loc
);
3825 /* Return a new set of bitmaps for a BB. */
3827 static struct tm_memopt_bitmaps
*
3828 tm_memopt_init_sets (void)
3830 struct tm_memopt_bitmaps
*b
3831 = XOBNEW (&tm_memopt_obstack
.obstack
, struct tm_memopt_bitmaps
);
3832 b
->store_avail_in
= BITMAP_ALLOC (&tm_memopt_obstack
);
3833 b
->store_avail_out
= BITMAP_ALLOC (&tm_memopt_obstack
);
3834 b
->store_antic_in
= BITMAP_ALLOC (&tm_memopt_obstack
);
3835 b
->store_antic_out
= BITMAP_ALLOC (&tm_memopt_obstack
);
3836 b
->store_avail_out
= BITMAP_ALLOC (&tm_memopt_obstack
);
3837 b
->read_avail_in
= BITMAP_ALLOC (&tm_memopt_obstack
);
3838 b
->read_avail_out
= BITMAP_ALLOC (&tm_memopt_obstack
);
3839 b
->read_local
= BITMAP_ALLOC (&tm_memopt_obstack
);
3840 b
->store_local
= BITMAP_ALLOC (&tm_memopt_obstack
);
3844 /* Free sets computed for each BB. */
3847 tm_memopt_free_sets (vec
<basic_block
> blocks
)
3852 for (i
= 0; blocks
.iterate (i
, &bb
); ++i
)
3856 /* Clear the visited bit for every basic block in BLOCKS. */
3859 tm_memopt_clear_visited (vec
<basic_block
> blocks
)
3864 for (i
= 0; blocks
.iterate (i
, &bb
); ++i
)
3865 BB_VISITED_P (bb
) = false;
3868 /* Replace TM load/stores with hints for the runtime. We handle
3869 things like read-after-write, write-after-read, read-after-read,
3870 read-for-write, etc. */
3873 execute_tm_memopt (void)
3875 struct tm_region
*region
;
3876 vec
<basic_block
> bbs
;
3878 tm_memopt_value_id
= 0;
3879 tm_memopt_value_numbers
= new hash_table
<tm_memop_hasher
> (10);
3881 for (region
= all_tm_regions
; region
; region
= region
->next
)
3883 /* All the TM stores/loads in the current region. */
3887 bitmap_obstack_initialize (&tm_memopt_obstack
);
3889 /* Save all BBs for the current region. */
3890 bbs
= get_tm_region_blocks (region
->entry_block
,
3891 region
->exit_blocks
,
3896 /* Collect all the memory operations. */
3897 for (i
= 0; bbs
.iterate (i
, &bb
); ++i
)
3899 bb
->aux
= tm_memopt_init_sets ();
3900 tm_memopt_accumulate_memops (bb
);
3903 /* Solve data flow equations and transform each block accordingly. */
3904 tm_memopt_clear_visited (bbs
);
3905 tm_memopt_compute_available (region
, bbs
);
3906 tm_memopt_clear_visited (bbs
);
3907 tm_memopt_compute_antic (region
, bbs
);
3908 tm_memopt_transform_blocks (bbs
);
3910 tm_memopt_free_sets (bbs
);
3912 bitmap_obstack_release (&tm_memopt_obstack
);
3913 tm_memopt_value_numbers
->empty ();
3916 delete tm_memopt_value_numbers
;
3917 tm_memopt_value_numbers
= NULL
;
3923 const pass_data pass_data_tm_memopt
=
3925 GIMPLE_PASS
, /* type */
3926 "tmmemopt", /* name */
3927 OPTGROUP_NONE
, /* optinfo_flags */
3928 true, /* has_execute */
3929 TV_TRANS_MEM
, /* tv_id */
3930 ( PROP_ssa
| PROP_cfg
), /* properties_required */
3931 0, /* properties_provided */
3932 0, /* properties_destroyed */
3933 0, /* todo_flags_start */
3934 0, /* todo_flags_finish */
3937 class pass_tm_memopt
: public gimple_opt_pass
3940 pass_tm_memopt (gcc::context
*ctxt
)
3941 : gimple_opt_pass (pass_data_tm_memopt
, ctxt
)
3944 /* opt_pass methods: */
3945 virtual bool gate (function
*) { return flag_tm
&& optimize
> 0; }
3946 virtual unsigned int execute (function
*) { return execute_tm_memopt (); }
3948 }; // class pass_tm_memopt
3953 make_pass_tm_memopt (gcc::context
*ctxt
)
3955 return new pass_tm_memopt (ctxt
);
3959 /* Interprocedual analysis for the creation of transactional clones.
3960 The aim of this pass is to find which functions are referenced in
3961 a non-irrevocable transaction context, and for those over which
3962 we have control (or user directive), create a version of the
3963 function which uses only the transactional interface to reference
3964 protected memories. This analysis proceeds in several steps:
3966 (1) Collect the set of all possible transactional clones:
3968 (a) For all local public functions marked tm_callable, push
3969 it onto the tm_callee queue.
3971 (b) For all local functions, scan for calls in transaction blocks.
3972 Push the caller and callee onto the tm_caller and tm_callee
3973 queues. Count the number of callers for each callee.
3975 (c) For each local function on the callee list, assume we will
3976 create a transactional clone. Push *all* calls onto the
3977 callee queues; count the number of clone callers separately
3978 to the number of original callers.
3980 (2) Propagate irrevocable status up the dominator tree:
3982 (a) Any external function on the callee list that is not marked
3983 tm_callable is irrevocable. Push all callers of such onto
3986 (b) For each function on the worklist, mark each block that
3987 contains an irrevocable call. Use the AND operator to
3988 propagate that mark up the dominator tree.
3990 (c) If we reach the entry block for a possible transactional
3991 clone, then the transactional clone is irrevocable, and
3992 we should not create the clone after all. Push all
3993 callers onto the worklist.
3995 (d) Place tm_irrevocable calls at the beginning of the relevant
3996 blocks. Special case here is the entry block for the entire
3997 transaction region; there we mark it GTMA_DOES_GO_IRREVOCABLE for
3998 the library to begin the region in serial mode. Decrement
3999 the call count for all callees in the irrevocable region.
4001 (3) Create the transactional clones:
4003 Any tm_callee that still has a non-zero call count is cloned.
4006 /* This structure is stored in the AUX field of each cgraph_node. */
4007 struct tm_ipa_cg_data
4009 /* The clone of the function that got created. */
4010 struct cgraph_node
*clone
;
4012 /* The tm regions in the normal function. */
4013 struct tm_region
*all_tm_regions
;
4015 /* The blocks of the normal/clone functions that contain irrevocable
4016 calls, or blocks that are post-dominated by irrevocable calls. */
4017 bitmap irrevocable_blocks_normal
;
4018 bitmap irrevocable_blocks_clone
;
4020 /* The blocks of the normal function that are involved in transactions. */
4021 bitmap transaction_blocks_normal
;
4023 /* The number of callers to the transactional clone of this function
4024 from normal and transactional clones respectively. */
4025 unsigned tm_callers_normal
;
4026 unsigned tm_callers_clone
;
4028 /* True if all calls to this function's transactional clone
4029 are irrevocable. Also automatically true if the function
4030 has no transactional clone. */
4031 bool is_irrevocable
;
4033 /* Flags indicating the presence of this function in various queues. */
4034 bool in_callee_queue
;
4037 /* Flags indicating the kind of scan desired while in the worklist. */
4038 bool want_irr_scan_normal
;
4041 typedef vec
<cgraph_node_ptr
> cgraph_node_queue
;
4043 /* Return the ipa data associated with NODE, allocating zeroed memory
4044 if necessary. TRAVERSE_ALIASES is true if we must traverse aliases
4045 and set *NODE accordingly. */
4047 static struct tm_ipa_cg_data
*
4048 get_cg_data (struct cgraph_node
**node
, bool traverse_aliases
)
4050 struct tm_ipa_cg_data
*d
;
4052 if (traverse_aliases
&& (*node
)->alias
)
4053 *node
= cgraph_alias_target (*node
);
4055 d
= (struct tm_ipa_cg_data
*) (*node
)->aux
;
4059 d
= (struct tm_ipa_cg_data
*)
4060 obstack_alloc (&tm_obstack
.obstack
, sizeof (*d
));
4061 (*node
)->aux
= (void *) d
;
4062 memset (d
, 0, sizeof (*d
));
4068 /* Add NODE to the end of QUEUE, unless IN_QUEUE_P indicates that
4069 it is already present. */
4072 maybe_push_queue (struct cgraph_node
*node
,
4073 cgraph_node_queue
*queue_p
, bool *in_queue_p
)
4078 queue_p
->safe_push (node
);
4082 /* Duplicate the basic blocks in QUEUE for use in the uninstrumented
4083 code path. QUEUE are the basic blocks inside the transaction
4084 represented in REGION.
4086 Later in split_code_paths() we will add the conditional to choose
4087 between the two alternatives. */
4090 ipa_uninstrument_transaction (struct tm_region
*region
,
4091 vec
<basic_block
> queue
)
4093 gimple transaction
= region
->transaction_stmt
;
4094 basic_block transaction_bb
= gimple_bb (transaction
);
4095 int n
= queue
.length ();
4096 basic_block
*new_bbs
= XNEWVEC (basic_block
, n
);
4098 copy_bbs (queue
.address (), n
, new_bbs
, NULL
, 0, NULL
, NULL
, transaction_bb
,
4100 edge e
= make_edge (transaction_bb
, new_bbs
[0], EDGE_TM_UNINSTRUMENTED
);
4101 add_phi_args_after_copy (new_bbs
, n
, e
);
4103 // Now we will have a GIMPLE_ATOMIC with 3 possible edges out of it.
4104 // a) EDGE_FALLTHRU into the transaction
4105 // b) EDGE_TM_ABORT out of the transaction
4106 // c) EDGE_TM_UNINSTRUMENTED into the uninstrumented blocks.
4111 /* A subroutine of ipa_tm_scan_calls_transaction and ipa_tm_scan_calls_clone.
4112 Queue all callees within block BB. */
4115 ipa_tm_scan_calls_block (cgraph_node_queue
*callees_p
,
4116 basic_block bb
, bool for_clone
)
4118 gimple_stmt_iterator gsi
;
4120 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4122 gimple stmt
= gsi_stmt (gsi
);
4123 if (is_gimple_call (stmt
) && !is_tm_pure_call (stmt
))
4125 tree fndecl
= gimple_call_fndecl (stmt
);
4128 struct tm_ipa_cg_data
*d
;
4130 struct cgraph_node
*node
;
4132 if (is_tm_ending_fndecl (fndecl
))
4134 if (find_tm_replacement_function (fndecl
))
4137 node
= cgraph_get_node (fndecl
);
4138 gcc_assert (node
!= NULL
);
4139 d
= get_cg_data (&node
, true);
4141 pcallers
= (for_clone
? &d
->tm_callers_clone
4142 : &d
->tm_callers_normal
);
4145 maybe_push_queue (node
, callees_p
, &d
->in_callee_queue
);
4151 /* Scan all calls in NODE that are within a transaction region,
4152 and push the resulting nodes into the callee queue. */
4155 ipa_tm_scan_calls_transaction (struct tm_ipa_cg_data
*d
,
4156 cgraph_node_queue
*callees_p
)
4158 struct tm_region
*r
;
4160 d
->transaction_blocks_normal
= BITMAP_ALLOC (&tm_obstack
);
4161 d
->all_tm_regions
= all_tm_regions
;
4163 for (r
= all_tm_regions
; r
; r
= r
->next
)
4165 vec
<basic_block
> bbs
;
4169 bbs
= get_tm_region_blocks (r
->entry_block
, r
->exit_blocks
, NULL
,
4170 d
->transaction_blocks_normal
, false);
4172 // Generate the uninstrumented code path for this transaction.
4173 ipa_uninstrument_transaction (r
, bbs
);
4175 FOR_EACH_VEC_ELT (bbs
, i
, bb
)
4176 ipa_tm_scan_calls_block (callees_p
, bb
, false);
4181 // ??? copy_bbs should maintain cgraph edges for the blocks as it is
4182 // copying them, rather than forcing us to do this externally.
4183 rebuild_cgraph_edges ();
4185 // ??? In ipa_uninstrument_transaction we don't try to update dominators
4186 // because copy_bbs doesn't return a VEC like iterate_fix_dominators expects.
4187 // Instead, just release dominators here so update_ssa recomputes them.
4188 free_dominance_info (CDI_DOMINATORS
);
4190 // When building the uninstrumented code path, copy_bbs will have invoked
4191 // create_new_def_for starting an "ssa update context". There is only one
4192 // instance of this context, so resolve ssa updates before moving on to
4193 // the next function.
4194 update_ssa (TODO_update_ssa
);
4197 /* Scan all calls in NODE as if this is the transactional clone,
4198 and push the destinations into the callee queue. */
4201 ipa_tm_scan_calls_clone (struct cgraph_node
*node
,
4202 cgraph_node_queue
*callees_p
)
4204 struct function
*fn
= DECL_STRUCT_FUNCTION (node
->decl
);
4207 FOR_EACH_BB_FN (bb
, fn
)
4208 ipa_tm_scan_calls_block (callees_p
, bb
, true);
4211 /* The function NODE has been detected to be irrevocable. Push all
4212 of its callers onto WORKLIST for the purpose of re-scanning them. */
4215 ipa_tm_note_irrevocable (struct cgraph_node
*node
,
4216 cgraph_node_queue
*worklist_p
)
4218 struct tm_ipa_cg_data
*d
= get_cg_data (&node
, true);
4219 struct cgraph_edge
*e
;
4221 d
->is_irrevocable
= true;
4223 for (e
= node
->callers
; e
; e
= e
->next_caller
)
4226 struct cgraph_node
*caller
;
4228 /* Don't examine recursive calls. */
4229 if (e
->caller
== node
)
4231 /* Even if we think we can go irrevocable, believe the user
4233 if (is_tm_safe_or_pure (e
->caller
->decl
))
4237 d
= get_cg_data (&caller
, true);
4239 /* Check if the callee is in a transactional region. If so,
4240 schedule the function for normal re-scan as well. */
4241 bb
= gimple_bb (e
->call_stmt
);
4242 gcc_assert (bb
!= NULL
);
4243 if (d
->transaction_blocks_normal
4244 && bitmap_bit_p (d
->transaction_blocks_normal
, bb
->index
))
4245 d
->want_irr_scan_normal
= true;
4247 maybe_push_queue (caller
, worklist_p
, &d
->in_worklist
);
4251 /* A subroutine of ipa_tm_scan_irr_blocks; return true iff any statement
4252 within the block is irrevocable. */
4255 ipa_tm_scan_irr_block (basic_block bb
)
4257 gimple_stmt_iterator gsi
;
4260 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4262 gimple stmt
= gsi_stmt (gsi
);
4263 switch (gimple_code (stmt
))
4266 if (gimple_assign_single_p (stmt
))
4268 tree lhs
= gimple_assign_lhs (stmt
);
4269 tree rhs
= gimple_assign_rhs1 (stmt
);
4270 if (volatile_var_p (lhs
) || volatile_var_p (rhs
))
4277 tree lhs
= gimple_call_lhs (stmt
);
4278 if (lhs
&& volatile_var_p (lhs
))
4281 if (is_tm_pure_call (stmt
))
4284 fn
= gimple_call_fn (stmt
);
4286 /* Functions with the attribute are by definition irrevocable. */
4287 if (is_tm_irrevocable (fn
))
4290 /* For direct function calls, go ahead and check for replacement
4291 functions, or transitive irrevocable functions. For indirect
4292 functions, we'll ask the runtime. */
4293 if (TREE_CODE (fn
) == ADDR_EXPR
)
4295 struct tm_ipa_cg_data
*d
;
4296 struct cgraph_node
*node
;
4298 fn
= TREE_OPERAND (fn
, 0);
4299 if (is_tm_ending_fndecl (fn
))
4301 if (find_tm_replacement_function (fn
))
4304 node
= cgraph_get_node (fn
);
4305 d
= get_cg_data (&node
, true);
4307 /* Return true if irrevocable, but above all, believe
4309 if (d
->is_irrevocable
4310 && !is_tm_safe_or_pure (fn
))
4317 /* ??? The Approved Method of indicating that an inline
4318 assembly statement is not relevant to the transaction
4319 is to wrap it in a __tm_waiver block. This is not
4320 yet implemented, so we can't check for it. */
4321 if (is_tm_safe (current_function_decl
))
4323 tree t
= build1 (NOP_EXPR
, void_type_node
, size_zero_node
);
4324 SET_EXPR_LOCATION (t
, gimple_location (stmt
));
4325 error ("%Kasm not allowed in %<transaction_safe%> function", t
);
4337 /* For each of the blocks seeded witin PQUEUE, walk the CFG looking
4338 for new irrevocable blocks, marking them in NEW_IRR. Don't bother
4339 scanning past OLD_IRR or EXIT_BLOCKS. */
4342 ipa_tm_scan_irr_blocks (vec
<basic_block
> *pqueue
, bitmap new_irr
,
4343 bitmap old_irr
, bitmap exit_blocks
)
4345 bool any_new_irr
= false;
4348 bitmap visited_blocks
= BITMAP_ALLOC (NULL
);
4352 basic_block bb
= pqueue
->pop ();
4354 /* Don't re-scan blocks we know already are irrevocable. */
4355 if (old_irr
&& bitmap_bit_p (old_irr
, bb
->index
))
4358 if (ipa_tm_scan_irr_block (bb
))
4360 bitmap_set_bit (new_irr
, bb
->index
);
4363 else if (exit_blocks
== NULL
|| !bitmap_bit_p (exit_blocks
, bb
->index
))
4365 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4366 if (!bitmap_bit_p (visited_blocks
, e
->dest
->index
))
4368 bitmap_set_bit (visited_blocks
, e
->dest
->index
);
4369 pqueue
->safe_push (e
->dest
);
4373 while (!pqueue
->is_empty ());
4375 BITMAP_FREE (visited_blocks
);
4380 /* Propagate the irrevocable property both up and down the dominator tree.
4381 BB is the current block being scanned; EXIT_BLOCKS are the edges of the
4382 TM regions; OLD_IRR are the results of a previous scan of the dominator
4383 tree which has been fully propagated; NEW_IRR is the set of new blocks
4384 which are gaining the irrevocable property during the current scan. */
4387 ipa_tm_propagate_irr (basic_block entry_block
, bitmap new_irr
,
4388 bitmap old_irr
, bitmap exit_blocks
)
4390 vec
<basic_block
> bbs
;
4391 bitmap all_region_blocks
;
4393 /* If this block is in the old set, no need to rescan. */
4394 if (old_irr
&& bitmap_bit_p (old_irr
, entry_block
->index
))
4397 all_region_blocks
= BITMAP_ALLOC (&tm_obstack
);
4398 bbs
= get_tm_region_blocks (entry_block
, exit_blocks
, NULL
,
4399 all_region_blocks
, false);
4402 basic_block bb
= bbs
.pop ();
4403 bool this_irr
= bitmap_bit_p (new_irr
, bb
->index
);
4404 bool all_son_irr
= false;
4408 /* Propagate up. If my children are, I am too, but we must have
4409 at least one child that is. */
4412 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4414 if (!bitmap_bit_p (new_irr
, e
->dest
->index
))
4416 all_son_irr
= false;
4424 /* Add block to new_irr if it hasn't already been processed. */
4425 if (!old_irr
|| !bitmap_bit_p (old_irr
, bb
->index
))
4427 bitmap_set_bit (new_irr
, bb
->index
);
4433 /* Propagate down to everyone we immediately dominate. */
4437 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
4439 son
= next_dom_son (CDI_DOMINATORS
, son
))
4441 /* Make sure block is actually in a TM region, and it
4442 isn't already in old_irr. */
4443 if ((!old_irr
|| !bitmap_bit_p (old_irr
, son
->index
))
4444 && bitmap_bit_p (all_region_blocks
, son
->index
))
4445 bitmap_set_bit (new_irr
, son
->index
);
4449 while (!bbs
.is_empty ());
4451 BITMAP_FREE (all_region_blocks
);
4456 ipa_tm_decrement_clone_counts (basic_block bb
, bool for_clone
)
4458 gimple_stmt_iterator gsi
;
4460 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4462 gimple stmt
= gsi_stmt (gsi
);
4463 if (is_gimple_call (stmt
) && !is_tm_pure_call (stmt
))
4465 tree fndecl
= gimple_call_fndecl (stmt
);
4468 struct tm_ipa_cg_data
*d
;
4470 struct cgraph_node
*tnode
;
4472 if (is_tm_ending_fndecl (fndecl
))
4474 if (find_tm_replacement_function (fndecl
))
4477 tnode
= cgraph_get_node (fndecl
);
4478 d
= get_cg_data (&tnode
, true);
4480 pcallers
= (for_clone
? &d
->tm_callers_clone
4481 : &d
->tm_callers_normal
);
4483 gcc_assert (*pcallers
> 0);
4490 /* (Re-)Scan the transaction blocks in NODE for calls to irrevocable functions,
4491 as well as other irrevocable actions such as inline assembly. Mark all
4492 such blocks as irrevocable and decrement the number of calls to
4493 transactional clones. Return true if, for the transactional clone, the
4494 entire function is irrevocable. */
4497 ipa_tm_scan_irr_function (struct cgraph_node
*node
, bool for_clone
)
4499 struct tm_ipa_cg_data
*d
;
4500 bitmap new_irr
, old_irr
;
4503 /* Builtin operators (operator new, and such). */
4504 if (DECL_STRUCT_FUNCTION (node
->decl
) == NULL
4505 || DECL_STRUCT_FUNCTION (node
->decl
)->cfg
== NULL
)
4508 push_cfun (DECL_STRUCT_FUNCTION (node
->decl
));
4509 calculate_dominance_info (CDI_DOMINATORS
);
4511 d
= get_cg_data (&node
, true);
4512 auto_vec
<basic_block
, 10> queue
;
4513 new_irr
= BITMAP_ALLOC (&tm_obstack
);
4515 /* Scan each tm region, propagating irrevocable status through the tree. */
4518 old_irr
= d
->irrevocable_blocks_clone
;
4519 queue
.quick_push (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)));
4520 if (ipa_tm_scan_irr_blocks (&queue
, new_irr
, old_irr
, NULL
))
4522 ipa_tm_propagate_irr (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
4525 ret
= bitmap_bit_p (new_irr
,
4526 single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
))->index
);
4531 struct tm_region
*region
;
4533 old_irr
= d
->irrevocable_blocks_normal
;
4534 for (region
= d
->all_tm_regions
; region
; region
= region
->next
)
4536 queue
.quick_push (region
->entry_block
);
4537 if (ipa_tm_scan_irr_blocks (&queue
, new_irr
, old_irr
,
4538 region
->exit_blocks
))
4539 ipa_tm_propagate_irr (region
->entry_block
, new_irr
, old_irr
,
4540 region
->exit_blocks
);
4544 /* If we found any new irrevocable blocks, reduce the call count for
4545 transactional clones within the irrevocable blocks. Save the new
4546 set of irrevocable blocks for next time. */
4547 if (!bitmap_empty_p (new_irr
))
4549 bitmap_iterator bmi
;
4552 EXECUTE_IF_SET_IN_BITMAP (new_irr
, 0, i
, bmi
)
4553 ipa_tm_decrement_clone_counts (BASIC_BLOCK_FOR_FN (cfun
, i
),
4558 bitmap_ior_into (old_irr
, new_irr
);
4559 BITMAP_FREE (new_irr
);
4562 d
->irrevocable_blocks_clone
= new_irr
;
4564 d
->irrevocable_blocks_normal
= new_irr
;
4566 if (dump_file
&& new_irr
)
4569 bitmap_iterator bmi
;
4572 dname
= lang_hooks
.decl_printable_name (current_function_decl
, 2);
4573 EXECUTE_IF_SET_IN_BITMAP (new_irr
, 0, i
, bmi
)
4574 fprintf (dump_file
, "%s: bb %d goes irrevocable\n", dname
, i
);
4578 BITMAP_FREE (new_irr
);
4585 /* Return true if, for the transactional clone of NODE, any call
4586 may enter irrevocable mode. */
4589 ipa_tm_mayenterirr_function (struct cgraph_node
*node
)
4591 struct tm_ipa_cg_data
*d
;
4595 d
= get_cg_data (&node
, true);
4597 flags
= flags_from_decl_or_type (decl
);
4599 /* Handle some TM builtins. Ordinarily these aren't actually generated
4600 at this point, but handling these functions when written in by the
4601 user makes it easier to build unit tests. */
4602 if (flags
& ECF_TM_BUILTIN
)
4605 /* Filter out all functions that are marked. */
4606 if (flags
& ECF_TM_PURE
)
4608 if (is_tm_safe (decl
))
4610 if (is_tm_irrevocable (decl
))
4612 if (is_tm_callable (decl
))
4614 if (find_tm_replacement_function (decl
))
4617 /* If we aren't seeing the final version of the function we don't
4618 know what it will contain at runtime. */
4619 if (cgraph_function_body_availability (node
) < AVAIL_AVAILABLE
)
4622 /* If the function must go irrevocable, then of course true. */
4623 if (d
->is_irrevocable
)
4626 /* If there are any blocks marked irrevocable, then the function
4627 as a whole may enter irrevocable. */
4628 if (d
->irrevocable_blocks_clone
)
4631 /* We may have previously marked this function as tm_may_enter_irr;
4632 see pass_diagnose_tm_blocks. */
4633 if (node
->local
.tm_may_enter_irr
)
4636 /* Recurse on the main body for aliases. In general, this will
4637 result in one of the bits above being set so that we will not
4638 have to recurse next time. */
4640 return ipa_tm_mayenterirr_function (cgraph_get_node (node
->thunk
.alias
));
4642 /* What remains is unmarked local functions without items that force
4643 the function to go irrevocable. */
4647 /* Diagnose calls from transaction_safe functions to unmarked
4648 functions that are determined to not be safe. */
4651 ipa_tm_diagnose_tm_safe (struct cgraph_node
*node
)
4653 struct cgraph_edge
*e
;
4655 for (e
= node
->callees
; e
; e
= e
->next_callee
)
4656 if (!is_tm_callable (e
->callee
->decl
)
4657 && e
->callee
->local
.tm_may_enter_irr
)
4658 error_at (gimple_location (e
->call_stmt
),
4659 "unsafe function call %qD within "
4660 "%<transaction_safe%> function", e
->callee
->decl
);
4663 /* Diagnose call from atomic transactions to unmarked functions
4664 that are determined to not be safe. */
4667 ipa_tm_diagnose_transaction (struct cgraph_node
*node
,
4668 struct tm_region
*all_tm_regions
)
4670 struct tm_region
*r
;
4672 for (r
= all_tm_regions
; r
; r
= r
->next
)
4673 if (gimple_transaction_subcode (r
->transaction_stmt
) & GTMA_IS_RELAXED
)
4675 /* Atomic transactions can be nested inside relaxed. */
4677 ipa_tm_diagnose_transaction (node
, r
->inner
);
4681 vec
<basic_block
> bbs
;
4682 gimple_stmt_iterator gsi
;
4686 bbs
= get_tm_region_blocks (r
->entry_block
, r
->exit_blocks
,
4687 r
->irr_blocks
, NULL
, false);
4689 for (i
= 0; bbs
.iterate (i
, &bb
); ++i
)
4690 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4692 gimple stmt
= gsi_stmt (gsi
);
4695 if (gimple_code (stmt
) == GIMPLE_ASM
)
4697 error_at (gimple_location (stmt
),
4698 "asm not allowed in atomic transaction");
4702 if (!is_gimple_call (stmt
))
4704 fndecl
= gimple_call_fndecl (stmt
);
4706 /* Indirect function calls have been diagnosed already. */
4710 /* Stop at the end of the transaction. */
4711 if (is_tm_ending_fndecl (fndecl
))
4713 if (bitmap_bit_p (r
->exit_blocks
, bb
->index
))
4718 /* Marked functions have been diagnosed already. */
4719 if (is_tm_pure_call (stmt
))
4721 if (is_tm_callable (fndecl
))
4724 if (cgraph_local_info (fndecl
)->tm_may_enter_irr
)
4725 error_at (gimple_location (stmt
),
4726 "unsafe function call %qD within "
4727 "atomic transaction", fndecl
);
4734 /* Return a transactional mangled name for the DECL_ASSEMBLER_NAME in
4735 OLD_DECL. The returned value is a freshly malloced pointer that
4736 should be freed by the caller. */
4739 tm_mangle (tree old_asm_id
)
4741 const char *old_asm_name
;
4744 struct demangle_component
*dc
;
4747 /* Determine if the symbol is already a valid C++ mangled name. Do this
4748 even for C, which might be interfacing with C++ code via appropriately
4749 ugly identifiers. */
4750 /* ??? We could probably do just as well checking for "_Z" and be done. */
4751 old_asm_name
= IDENTIFIER_POINTER (old_asm_id
);
4752 dc
= cplus_demangle_v3_components (old_asm_name
, DMGL_NO_OPTS
, &alloc
);
4759 sprintf (length
, "%u", IDENTIFIER_LENGTH (old_asm_id
));
4760 tm_name
= concat ("_ZGTt", length
, old_asm_name
, NULL
);
4764 old_asm_name
+= 2; /* Skip _Z */
4768 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
4769 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
4770 /* Don't play silly games, you! */
4773 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
4774 /* I'd really like to know if we can ever be passed one of
4775 these from the C++ front end. The Logical Thing would
4776 seem that hidden-alias should be outer-most, so that we
4777 get hidden-alias of a transaction-clone and not vice-versa. */
4785 tm_name
= concat ("_ZGTt", old_asm_name
, NULL
);
4789 new_asm_id
= get_identifier (tm_name
);
4796 ipa_tm_mark_force_output_node (struct cgraph_node
*node
)
4798 cgraph_mark_force_output_node (node
);
4799 node
->analyzed
= true;
4803 ipa_tm_mark_forced_by_abi_node (struct cgraph_node
*node
)
4805 node
->forced_by_abi
= true;
4806 node
->analyzed
= true;
4809 /* Callback data for ipa_tm_create_version_alias. */
4810 struct create_version_alias_info
4812 struct cgraph_node
*old_node
;
4816 /* A subroutine of ipa_tm_create_version, called via
4817 cgraph_for_node_and_aliases. Create new tm clones for each of
4818 the existing aliases. */
4820 ipa_tm_create_version_alias (struct cgraph_node
*node
, void *data
)
4822 struct create_version_alias_info
*info
4823 = (struct create_version_alias_info
*)data
;
4824 tree old_decl
, new_decl
, tm_name
;
4825 struct cgraph_node
*new_node
;
4827 if (!node
->cpp_implicit_alias
)
4830 old_decl
= node
->decl
;
4831 tm_name
= tm_mangle (DECL_ASSEMBLER_NAME (old_decl
));
4832 new_decl
= build_decl (DECL_SOURCE_LOCATION (old_decl
),
4833 TREE_CODE (old_decl
), tm_name
,
4834 TREE_TYPE (old_decl
));
4836 SET_DECL_ASSEMBLER_NAME (new_decl
, tm_name
);
4837 SET_DECL_RTL (new_decl
, NULL
);
4839 /* Based loosely on C++'s make_alias_for(). */
4840 TREE_PUBLIC (new_decl
) = TREE_PUBLIC (old_decl
);
4841 DECL_CONTEXT (new_decl
) = DECL_CONTEXT (old_decl
);
4842 DECL_LANG_SPECIFIC (new_decl
) = DECL_LANG_SPECIFIC (old_decl
);
4843 TREE_READONLY (new_decl
) = TREE_READONLY (old_decl
);
4844 DECL_EXTERNAL (new_decl
) = 0;
4845 DECL_ARTIFICIAL (new_decl
) = 1;
4846 TREE_ADDRESSABLE (new_decl
) = 1;
4847 TREE_USED (new_decl
) = 1;
4848 TREE_SYMBOL_REFERENCED (tm_name
) = 1;
4850 /* Perform the same remapping to the comdat group. */
4851 if (DECL_ONE_ONLY (new_decl
))
4852 varpool_get_node (new_decl
)->set_comdat_group (tm_mangle (decl_comdat_group_id (old_decl
)));
4854 new_node
= cgraph_same_body_alias (NULL
, new_decl
, info
->new_decl
);
4855 new_node
->tm_clone
= true;
4856 new_node
->externally_visible
= info
->old_node
->externally_visible
;
4857 /* ?? Do not traverse aliases here. */
4858 get_cg_data (&node
, false)->clone
= new_node
;
4860 record_tm_clone_pair (old_decl
, new_decl
);
4862 if (info
->old_node
->force_output
4863 || info
->old_node
->ref_list
.first_referring ())
4864 ipa_tm_mark_force_output_node (new_node
);
4865 if (info
->old_node
->forced_by_abi
)
4866 ipa_tm_mark_forced_by_abi_node (new_node
);
4870 /* Create a copy of the function (possibly declaration only) of OLD_NODE,
4871 appropriate for the transactional clone. */
4874 ipa_tm_create_version (struct cgraph_node
*old_node
)
4876 tree new_decl
, old_decl
, tm_name
;
4877 struct cgraph_node
*new_node
;
4879 old_decl
= old_node
->decl
;
4880 new_decl
= copy_node (old_decl
);
4882 /* DECL_ASSEMBLER_NAME needs to be set before we call
4883 cgraph_copy_node_for_versioning below, because cgraph_node will
4884 fill the assembler_name_hash. */
4885 tm_name
= tm_mangle (DECL_ASSEMBLER_NAME (old_decl
));
4886 SET_DECL_ASSEMBLER_NAME (new_decl
, tm_name
);
4887 SET_DECL_RTL (new_decl
, NULL
);
4888 TREE_SYMBOL_REFERENCED (tm_name
) = 1;
4890 /* Perform the same remapping to the comdat group. */
4891 if (DECL_ONE_ONLY (new_decl
))
4892 varpool_get_node (new_decl
)->set_comdat_group (tm_mangle (DECL_COMDAT_GROUP (old_decl
)));
4894 gcc_assert (!old_node
->ipa_transforms_to_apply
.exists ());
4895 new_node
= cgraph_copy_node_for_versioning (old_node
, new_decl
, vNULL
, NULL
);
4896 new_node
->local
.local
= false;
4897 new_node
->externally_visible
= old_node
->externally_visible
;
4898 new_node
->lowered
= true;
4899 new_node
->tm_clone
= 1;
4900 get_cg_data (&old_node
, true)->clone
= new_node
;
4902 if (cgraph_function_body_availability (old_node
) >= AVAIL_OVERWRITABLE
)
4904 /* Remap extern inline to static inline. */
4905 /* ??? Is it worth trying to use make_decl_one_only? */
4906 if (DECL_DECLARED_INLINE_P (new_decl
) && DECL_EXTERNAL (new_decl
))
4908 DECL_EXTERNAL (new_decl
) = 0;
4909 TREE_PUBLIC (new_decl
) = 0;
4910 DECL_WEAK (new_decl
) = 0;
4913 tree_function_versioning (old_decl
, new_decl
,
4918 record_tm_clone_pair (old_decl
, new_decl
);
4920 cgraph_call_function_insertion_hooks (new_node
);
4921 if (old_node
->force_output
4922 || old_node
->ref_list
.first_referring ())
4923 ipa_tm_mark_force_output_node (new_node
);
4924 if (old_node
->forced_by_abi
)
4925 ipa_tm_mark_forced_by_abi_node (new_node
);
4927 /* Do the same thing, but for any aliases of the original node. */
4929 struct create_version_alias_info data
;
4930 data
.old_node
= old_node
;
4931 data
.new_decl
= new_decl
;
4932 cgraph_for_node_and_aliases (old_node
, ipa_tm_create_version_alias
,
4937 /* Construct a call to TM_IRREVOCABLE and insert it at the beginning of BB. */
4940 ipa_tm_insert_irr_call (struct cgraph_node
*node
, struct tm_region
*region
,
4943 gimple_stmt_iterator gsi
;
4946 transaction_subcode_ior (region
, GTMA_MAY_ENTER_IRREVOCABLE
);
4948 g
= gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_IRREVOCABLE
),
4949 1, build_int_cst (NULL_TREE
, MODE_SERIALIRREVOCABLE
));
4951 split_block_after_labels (bb
);
4952 gsi
= gsi_after_labels (bb
);
4953 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
4955 cgraph_create_edge (node
,
4956 cgraph_get_create_node
4957 (builtin_decl_explicit (BUILT_IN_TM_IRREVOCABLE
)),
4959 compute_call_stmt_bb_frequency (node
->decl
,
4963 /* Construct a call to TM_GETTMCLONE and insert it before GSI. */
4966 ipa_tm_insert_gettmclone_call (struct cgraph_node
*node
,
4967 struct tm_region
*region
,
4968 gimple_stmt_iterator
*gsi
, gimple stmt
)
4970 tree gettm_fn
, ret
, old_fn
, callfn
;
4974 old_fn
= gimple_call_fn (stmt
);
4976 if (TREE_CODE (old_fn
) == ADDR_EXPR
)
4978 tree fndecl
= TREE_OPERAND (old_fn
, 0);
4979 tree clone
= get_tm_clone_pair (fndecl
);
4981 /* By transforming the call into a TM_GETTMCLONE, we are
4982 technically taking the address of the original function and
4983 its clone. Explain this so inlining will know this function
4985 cgraph_mark_address_taken_node (cgraph_get_node (fndecl
));
4987 cgraph_mark_address_taken_node (cgraph_get_node (clone
));
4990 safe
= is_tm_safe (TREE_TYPE (old_fn
));
4991 gettm_fn
= builtin_decl_explicit (safe
? BUILT_IN_TM_GETTMCLONE_SAFE
4992 : BUILT_IN_TM_GETTMCLONE_IRR
);
4993 ret
= create_tmp_var (ptr_type_node
, NULL
);
4996 transaction_subcode_ior (region
, GTMA_MAY_ENTER_IRREVOCABLE
);
4998 /* Discard OBJ_TYPE_REF, since we weren't able to fold it. */
4999 if (TREE_CODE (old_fn
) == OBJ_TYPE_REF
)
5000 old_fn
= OBJ_TYPE_REF_EXPR (old_fn
);
5002 g
= gimple_build_call (gettm_fn
, 1, old_fn
);
5003 ret
= make_ssa_name (ret
, g
);
5004 gimple_call_set_lhs (g
, ret
);
5006 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
5008 cgraph_create_edge (node
, cgraph_get_create_node (gettm_fn
), g
, 0,
5009 compute_call_stmt_bb_frequency (node
->decl
,
5012 /* Cast return value from tm_gettmclone* into appropriate function
5014 callfn
= create_tmp_var (TREE_TYPE (old_fn
), NULL
);
5015 g2
= gimple_build_assign (callfn
,
5016 fold_build1 (NOP_EXPR
, TREE_TYPE (callfn
), ret
));
5017 callfn
= make_ssa_name (callfn
, g2
);
5018 gimple_assign_set_lhs (g2
, callfn
);
5019 gsi_insert_before (gsi
, g2
, GSI_SAME_STMT
);
5021 /* ??? This is a hack to preserve the NOTHROW bit on the call,
5022 which we would have derived from the decl. Failure to save
5023 this bit means we might have to split the basic block. */
5024 if (gimple_call_nothrow_p (stmt
))
5025 gimple_call_set_nothrow (stmt
, true);
5027 gimple_call_set_fn (stmt
, callfn
);
5029 /* Discarding OBJ_TYPE_REF above may produce incompatible LHS and RHS
5030 for a call statement. Fix it. */
5032 tree lhs
= gimple_call_lhs (stmt
);
5033 tree rettype
= TREE_TYPE (gimple_call_fntype (stmt
));
5035 && !useless_type_conversion_p (TREE_TYPE (lhs
), rettype
))
5039 temp
= create_tmp_reg (rettype
, 0);
5040 gimple_call_set_lhs (stmt
, temp
);
5042 g2
= gimple_build_assign (lhs
,
5043 fold_build1 (VIEW_CONVERT_EXPR
,
5044 TREE_TYPE (lhs
), temp
));
5045 gsi_insert_after (gsi
, g2
, GSI_SAME_STMT
);
5054 /* Helper function for ipa_tm_transform_calls*. Given a call
5055 statement in GSI which resides inside transaction REGION, redirect
5056 the call to either its wrapper function, or its clone. */
5059 ipa_tm_transform_calls_redirect (struct cgraph_node
*node
,
5060 struct tm_region
*region
,
5061 gimple_stmt_iterator
*gsi
,
5062 bool *need_ssa_rename_p
)
5064 gimple stmt
= gsi_stmt (*gsi
);
5065 struct cgraph_node
*new_node
;
5066 struct cgraph_edge
*e
= cgraph_edge (node
, stmt
);
5067 tree fndecl
= gimple_call_fndecl (stmt
);
5069 /* For indirect calls, pass the address through the runtime. */
5072 *need_ssa_rename_p
|=
5073 ipa_tm_insert_gettmclone_call (node
, region
, gsi
, stmt
);
5077 /* Handle some TM builtins. Ordinarily these aren't actually generated
5078 at this point, but handling these functions when written in by the
5079 user makes it easier to build unit tests. */
5080 if (flags_from_decl_or_type (fndecl
) & ECF_TM_BUILTIN
)
5083 /* Fixup recursive calls inside clones. */
5084 /* ??? Why did cgraph_copy_node_for_versioning update the call edges
5085 for recursion but not update the call statements themselves? */
5086 if (e
->caller
== e
->callee
&& decl_is_tm_clone (current_function_decl
))
5088 gimple_call_set_fndecl (stmt
, current_function_decl
);
5092 /* If there is a replacement, use it. */
5093 fndecl
= find_tm_replacement_function (fndecl
);
5096 new_node
= cgraph_get_create_node (fndecl
);
5098 /* ??? Mark all transaction_wrap functions tm_may_enter_irr.
5100 We can't do this earlier in record_tm_replacement because
5101 cgraph_remove_unreachable_nodes is called before we inject
5102 references to the node. Further, we can't do this in some
5103 nice central place in ipa_tm_execute because we don't have
5104 the exact list of wrapper functions that would be used.
5105 Marking more wrappers than necessary results in the creation
5106 of unnecessary cgraph_nodes, which can cause some of the
5107 other IPA passes to crash.
5109 We do need to mark these nodes so that we get the proper
5110 result in expand_call_tm. */
5111 /* ??? This seems broken. How is it that we're marking the
5112 CALLEE as may_enter_irr? Surely we should be marking the
5113 CALLER. Also note that find_tm_replacement_function also
5114 contains mappings into the TM runtime, e.g. memcpy. These
5115 we know won't go irrevocable. */
5116 new_node
->local
.tm_may_enter_irr
= 1;
5120 struct tm_ipa_cg_data
*d
;
5121 struct cgraph_node
*tnode
= e
->callee
;
5123 d
= get_cg_data (&tnode
, true);
5124 new_node
= d
->clone
;
5126 /* As we've already skipped pure calls and appropriate builtins,
5127 and we've already marked irrevocable blocks, if we can't come
5128 up with a static replacement, then ask the runtime. */
5129 if (new_node
== NULL
)
5131 *need_ssa_rename_p
|=
5132 ipa_tm_insert_gettmclone_call (node
, region
, gsi
, stmt
);
5136 fndecl
= new_node
->decl
;
5139 cgraph_redirect_edge_callee (e
, new_node
);
5140 gimple_call_set_fndecl (stmt
, fndecl
);
5143 /* Helper function for ipa_tm_transform_calls. For a given BB,
5144 install calls to tm_irrevocable when IRR_BLOCKS are reached,
5145 redirect other calls to the generated transactional clone. */
5148 ipa_tm_transform_calls_1 (struct cgraph_node
*node
, struct tm_region
*region
,
5149 basic_block bb
, bitmap irr_blocks
)
5151 gimple_stmt_iterator gsi
;
5152 bool need_ssa_rename
= false;
5154 if (irr_blocks
&& bitmap_bit_p (irr_blocks
, bb
->index
))
5156 ipa_tm_insert_irr_call (node
, region
, bb
);
5160 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
5162 gimple stmt
= gsi_stmt (gsi
);
5164 if (!is_gimple_call (stmt
))
5166 if (is_tm_pure_call (stmt
))
5169 /* Redirect edges to the appropriate replacement or clone. */
5170 ipa_tm_transform_calls_redirect (node
, region
, &gsi
, &need_ssa_rename
);
5173 return need_ssa_rename
;
5176 /* Walk the CFG for REGION, beginning at BB. Install calls to
5177 tm_irrevocable when IRR_BLOCKS are reached, redirect other calls to
5178 the generated transactional clone. */
5181 ipa_tm_transform_calls (struct cgraph_node
*node
, struct tm_region
*region
,
5182 basic_block bb
, bitmap irr_blocks
)
5184 bool need_ssa_rename
= false;
5187 auto_vec
<basic_block
> queue
;
5188 bitmap visited_blocks
= BITMAP_ALLOC (NULL
);
5190 queue
.safe_push (bb
);
5196 ipa_tm_transform_calls_1 (node
, region
, bb
, irr_blocks
);
5198 if (irr_blocks
&& bitmap_bit_p (irr_blocks
, bb
->index
))
5201 if (region
&& bitmap_bit_p (region
->exit_blocks
, bb
->index
))
5204 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5205 if (!bitmap_bit_p (visited_blocks
, e
->dest
->index
))
5207 bitmap_set_bit (visited_blocks
, e
->dest
->index
);
5208 queue
.safe_push (e
->dest
);
5211 while (!queue
.is_empty ());
5213 BITMAP_FREE (visited_blocks
);
5215 return need_ssa_rename
;
5218 /* Transform the calls within the TM regions within NODE. */
5221 ipa_tm_transform_transaction (struct cgraph_node
*node
)
5223 struct tm_ipa_cg_data
*d
;
5224 struct tm_region
*region
;
5225 bool need_ssa_rename
= false;
5227 d
= get_cg_data (&node
, true);
5229 push_cfun (DECL_STRUCT_FUNCTION (node
->decl
));
5230 calculate_dominance_info (CDI_DOMINATORS
);
5232 for (region
= d
->all_tm_regions
; region
; region
= region
->next
)
5234 /* If we're sure to go irrevocable, don't transform anything. */
5235 if (d
->irrevocable_blocks_normal
5236 && bitmap_bit_p (d
->irrevocable_blocks_normal
,
5237 region
->entry_block
->index
))
5239 transaction_subcode_ior (region
, GTMA_DOES_GO_IRREVOCABLE
5240 | GTMA_MAY_ENTER_IRREVOCABLE
5241 | GTMA_HAS_NO_INSTRUMENTATION
);
5246 ipa_tm_transform_calls (node
, region
, region
->entry_block
,
5247 d
->irrevocable_blocks_normal
);
5250 if (need_ssa_rename
)
5251 update_ssa (TODO_update_ssa_only_virtuals
);
5256 /* Transform the calls within the transactional clone of NODE. */
5259 ipa_tm_transform_clone (struct cgraph_node
*node
)
5261 struct tm_ipa_cg_data
*d
;
5262 bool need_ssa_rename
;
5264 d
= get_cg_data (&node
, true);
5266 /* If this function makes no calls and has no irrevocable blocks,
5267 then there's nothing to do. */
5268 /* ??? Remove non-aborting top-level transactions. */
5269 if (!node
->callees
&& !node
->indirect_calls
&& !d
->irrevocable_blocks_clone
)
5272 push_cfun (DECL_STRUCT_FUNCTION (d
->clone
->decl
));
5273 calculate_dominance_info (CDI_DOMINATORS
);
5276 ipa_tm_transform_calls (d
->clone
, NULL
,
5277 single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
5278 d
->irrevocable_blocks_clone
);
5280 if (need_ssa_rename
)
5281 update_ssa (TODO_update_ssa_only_virtuals
);
5286 /* Main entry point for the transactional memory IPA pass. */
5289 ipa_tm_execute (void)
5291 cgraph_node_queue tm_callees
= cgraph_node_queue ();
5292 /* List of functions that will go irrevocable. */
5293 cgraph_node_queue irr_worklist
= cgraph_node_queue ();
5295 struct cgraph_node
*node
;
5296 struct tm_ipa_cg_data
*d
;
5297 enum availability a
;
5300 #ifdef ENABLE_CHECKING
5304 bitmap_obstack_initialize (&tm_obstack
);
5305 initialize_original_copy_tables ();
5307 /* For all local functions marked tm_callable, queue them. */
5308 FOR_EACH_DEFINED_FUNCTION (node
)
5309 if (is_tm_callable (node
->decl
)
5310 && cgraph_function_body_availability (node
) >= AVAIL_OVERWRITABLE
)
5312 d
= get_cg_data (&node
, true);
5313 maybe_push_queue (node
, &tm_callees
, &d
->in_callee_queue
);
5316 /* For all local reachable functions... */
5317 FOR_EACH_DEFINED_FUNCTION (node
)
5319 && cgraph_function_body_availability (node
) >= AVAIL_OVERWRITABLE
)
5321 /* ... marked tm_pure, record that fact for the runtime by
5322 indicating that the pure function is its own tm_callable.
5323 No need to do this if the function's address can't be taken. */
5324 if (is_tm_pure (node
->decl
))
5326 if (!node
->local
.local
)
5327 record_tm_clone_pair (node
->decl
, node
->decl
);
5331 push_cfun (DECL_STRUCT_FUNCTION (node
->decl
));
5332 calculate_dominance_info (CDI_DOMINATORS
);
5334 tm_region_init (NULL
);
5337 d
= get_cg_data (&node
, true);
5339 /* Scan for calls that are in each transaction, and
5340 generate the uninstrumented code path. */
5341 ipa_tm_scan_calls_transaction (d
, &tm_callees
);
5343 /* Put it in the worklist so we can scan the function
5344 later (ipa_tm_scan_irr_function) and mark the
5345 irrevocable blocks. */
5346 maybe_push_queue (node
, &irr_worklist
, &d
->in_worklist
);
5347 d
->want_irr_scan_normal
= true;
5353 /* For every local function on the callee list, scan as if we will be
5354 creating a transactional clone, queueing all new functions we find
5356 for (i
= 0; i
< tm_callees
.length (); ++i
)
5358 node
= tm_callees
[i
];
5359 a
= cgraph_function_body_availability (node
);
5360 d
= get_cg_data (&node
, true);
5362 /* Put it in the worklist so we can scan the function later
5363 (ipa_tm_scan_irr_function) and mark the irrevocable
5365 maybe_push_queue (node
, &irr_worklist
, &d
->in_worklist
);
5367 /* Some callees cannot be arbitrarily cloned. These will always be
5368 irrevocable. Mark these now, so that we need not scan them. */
5369 if (is_tm_irrevocable (node
->decl
))
5370 ipa_tm_note_irrevocable (node
, &irr_worklist
);
5371 else if (a
<= AVAIL_NOT_AVAILABLE
5372 && !is_tm_safe_or_pure (node
->decl
))
5373 ipa_tm_note_irrevocable (node
, &irr_worklist
);
5374 else if (a
>= AVAIL_OVERWRITABLE
)
5376 if (!tree_versionable_function_p (node
->decl
))
5377 ipa_tm_note_irrevocable (node
, &irr_worklist
);
5378 else if (!d
->is_irrevocable
)
5380 /* If this is an alias, make sure its base is queued as well.
5381 we need not scan the callees now, as the base will do. */
5384 node
= cgraph_get_node (node
->thunk
.alias
);
5385 d
= get_cg_data (&node
, true);
5386 maybe_push_queue (node
, &tm_callees
, &d
->in_callee_queue
);
5390 /* Add all nodes called by this function into
5391 tm_callees as well. */
5392 ipa_tm_scan_calls_clone (node
, &tm_callees
);
5397 /* Iterate scans until no more work to be done. Prefer not to use
5398 vec::pop because the worklist tends to follow a breadth-first
5399 search of the callgraph, which should allow convergance with a
5400 minimum number of scans. But we also don't want the worklist
5401 array to grow without bound, so we shift the array up periodically. */
5402 for (i
= 0; i
< irr_worklist
.length (); ++i
)
5404 if (i
> 256 && i
== irr_worklist
.length () / 8)
5406 irr_worklist
.block_remove (0, i
);
5410 node
= irr_worklist
[i
];
5411 d
= get_cg_data (&node
, true);
5412 d
->in_worklist
= false;
5414 if (d
->want_irr_scan_normal
)
5416 d
->want_irr_scan_normal
= false;
5417 ipa_tm_scan_irr_function (node
, false);
5419 if (d
->in_callee_queue
&& ipa_tm_scan_irr_function (node
, true))
5420 ipa_tm_note_irrevocable (node
, &irr_worklist
);
5423 /* For every function on the callee list, collect the tm_may_enter_irr
5425 irr_worklist
.truncate (0);
5426 for (i
= 0; i
< tm_callees
.length (); ++i
)
5428 node
= tm_callees
[i
];
5429 if (ipa_tm_mayenterirr_function (node
))
5431 d
= get_cg_data (&node
, true);
5432 gcc_assert (d
->in_worklist
== false);
5433 maybe_push_queue (node
, &irr_worklist
, &d
->in_worklist
);
5437 /* Propagate the tm_may_enter_irr bit to callers until stable. */
5438 for (i
= 0; i
< irr_worklist
.length (); ++i
)
5440 struct cgraph_node
*caller
;
5441 struct cgraph_edge
*e
;
5442 struct ipa_ref
*ref
= NULL
;
5445 if (i
> 256 && i
== irr_worklist
.length () / 8)
5447 irr_worklist
.block_remove (0, i
);
5451 node
= irr_worklist
[i
];
5452 d
= get_cg_data (&node
, true);
5453 d
->in_worklist
= false;
5454 node
->local
.tm_may_enter_irr
= true;
5456 /* Propagate back to normal callers. */
5457 for (e
= node
->callers
; e
; e
= e
->next_caller
)
5460 if (!is_tm_safe_or_pure (caller
->decl
)
5461 && !caller
->local
.tm_may_enter_irr
)
5463 d
= get_cg_data (&caller
, true);
5464 maybe_push_queue (caller
, &irr_worklist
, &d
->in_worklist
);
5468 /* Propagate back to referring aliases as well. */
5469 for (j
= 0; node
->iterate_referring (j
, ref
); j
++)
5471 caller
= cgraph (ref
->referring
);
5472 if (ref
->use
== IPA_REF_ALIAS
5473 && !caller
->local
.tm_may_enter_irr
)
5475 /* ?? Do not traverse aliases here. */
5476 d
= get_cg_data (&caller
, false);
5477 maybe_push_queue (caller
, &irr_worklist
, &d
->in_worklist
);
5482 /* Now validate all tm_safe functions, and all atomic regions in
5484 FOR_EACH_DEFINED_FUNCTION (node
)
5486 && cgraph_function_body_availability (node
) >= AVAIL_OVERWRITABLE
)
5488 d
= get_cg_data (&node
, true);
5489 if (is_tm_safe (node
->decl
))
5490 ipa_tm_diagnose_tm_safe (node
);
5491 else if (d
->all_tm_regions
)
5492 ipa_tm_diagnose_transaction (node
, d
->all_tm_regions
);
5495 /* Create clones. Do those that are not irrevocable and have a
5496 positive call count. Do those publicly visible functions that
5497 the user directed us to clone. */
5498 for (i
= 0; i
< tm_callees
.length (); ++i
)
5502 node
= tm_callees
[i
];
5503 if (node
->cpp_implicit_alias
)
5506 a
= cgraph_function_body_availability (node
);
5507 d
= get_cg_data (&node
, true);
5509 if (a
<= AVAIL_NOT_AVAILABLE
)
5510 doit
= is_tm_callable (node
->decl
);
5511 else if (a
<= AVAIL_AVAILABLE
&& is_tm_callable (node
->decl
))
5513 else if (!d
->is_irrevocable
5514 && d
->tm_callers_normal
+ d
->tm_callers_clone
> 0)
5518 ipa_tm_create_version (node
);
5521 /* Redirect calls to the new clones, and insert irrevocable marks. */
5522 for (i
= 0; i
< tm_callees
.length (); ++i
)
5524 node
= tm_callees
[i
];
5527 d
= get_cg_data (&node
, true);
5529 ipa_tm_transform_clone (node
);
5532 FOR_EACH_DEFINED_FUNCTION (node
)
5534 && cgraph_function_body_availability (node
) >= AVAIL_OVERWRITABLE
)
5536 d
= get_cg_data (&node
, true);
5537 if (d
->all_tm_regions
)
5538 ipa_tm_transform_transaction (node
);
5541 /* Free and clear all data structures. */
5542 tm_callees
.release ();
5543 irr_worklist
.release ();
5544 bitmap_obstack_release (&tm_obstack
);
5545 free_original_copy_tables ();
5547 FOR_EACH_FUNCTION (node
)
5550 #ifdef ENABLE_CHECKING
5559 const pass_data pass_data_ipa_tm
=
5561 SIMPLE_IPA_PASS
, /* type */
5563 OPTGROUP_NONE
, /* optinfo_flags */
5564 true, /* has_execute */
5565 TV_TRANS_MEM
, /* tv_id */
5566 ( PROP_ssa
| PROP_cfg
), /* properties_required */
5567 0, /* properties_provided */
5568 0, /* properties_destroyed */
5569 0, /* todo_flags_start */
5570 0, /* todo_flags_finish */
5573 class pass_ipa_tm
: public simple_ipa_opt_pass
5576 pass_ipa_tm (gcc::context
*ctxt
)
5577 : simple_ipa_opt_pass (pass_data_ipa_tm
, ctxt
)
5580 /* opt_pass methods: */
5581 virtual bool gate (function
*) { return flag_tm
; }
5582 virtual unsigned int execute (function
*) { return ipa_tm_execute (); }
5584 }; // class pass_ipa_tm
5588 simple_ipa_opt_pass
*
5589 make_pass_ipa_tm (gcc::context
*ctxt
)
5591 return new pass_ipa_tm (ctxt
);
5594 #include "gt-trans-mem.h"