1 /* Passes for transactional memory support.
2 Copyright (C) 2008-2013 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"
27 #include "tree-pass.h"
28 #include "tree-inline.h"
29 #include "diagnostic-core.h"
32 #include "trans-mem.h"
35 #include "langhooks.h"
36 #include "gimple-pretty-print.h"
40 #define PROB_VERY_UNLIKELY (REG_BR_PROB_BASE / 2000 - 1)
41 #define PROB_VERY_LIKELY (PROB_ALWAYS - PROB_VERY_UNLIKELY)
42 #define PROB_UNLIKELY (REG_BR_PROB_BASE / 5 - 1)
43 #define PROB_LIKELY (PROB_ALWAYS - PROB_VERY_LIKELY)
44 #define PROB_ALWAYS (REG_BR_PROB_BASE)
46 #define A_RUNINSTRUMENTEDCODE 0x0001
47 #define A_RUNUNINSTRUMENTEDCODE 0x0002
48 #define A_SAVELIVEVARIABLES 0x0004
49 #define A_RESTORELIVEVARIABLES 0x0008
50 #define A_ABORTTRANSACTION 0x0010
52 #define AR_USERABORT 0x0001
53 #define AR_USERRETRY 0x0002
54 #define AR_TMCONFLICT 0x0004
55 #define AR_EXCEPTIONBLOCKABORT 0x0008
56 #define AR_OUTERABORT 0x0010
58 #define MODE_SERIALIRREVOCABLE 0x0000
61 /* The representation of a transaction changes several times during the
62 lowering process. In the beginning, in the front-end we have the
63 GENERIC tree TRANSACTION_EXPR. For example,
71 During initial gimplification (gimplify.c) the TRANSACTION_EXPR node is
72 trivially replaced with a GIMPLE_TRANSACTION node.
74 During pass_lower_tm, we examine the body of transactions looking
75 for aborts. Transactions that do not contain an abort may be
76 merged into an outer transaction. We also add a TRY-FINALLY node
77 to arrange for the transaction to be committed on any exit.
79 [??? Think about how this arrangement affects throw-with-commit
80 and throw-with-abort operations. In this case we want the TRY to
81 handle gotos, but not to catch any exceptions because the transaction
82 will already be closed.]
84 GIMPLE_TRANSACTION [label=NULL] {
91 __builtin___tm_abort ();
93 __builtin___tm_commit ();
97 During pass_lower_eh, we create EH regions for the transactions,
98 intermixed with the regular EH stuff. This gives us a nice persistent
99 mapping (all the way through rtl) from transactional memory operation
100 back to the transaction, which allows us to get the abnormal edges
101 correct to model transaction aborts and restarts:
103 GIMPLE_TRANSACTION [label=over]
109 __builtin___tm_abort ();
110 __builtin___tm_commit ();
113 This is the end of all_lowering_passes, and so is what is present
114 during the IPA passes, and through all of the optimization passes.
116 During pass_ipa_tm, we examine all GIMPLE_TRANSACTION blocks in all
117 functions and mark functions for cloning.
119 At the end of gimple optimization, before exiting SSA form,
120 pass_tm_edges replaces statements that perform transactional
121 memory operations with the appropriate TM builtins, and swap
122 out function calls with their transactional clones. At this
123 point we introduce the abnormal transaction restart edges and
124 complete lowering of the GIMPLE_TRANSACTION node.
126 x = __builtin___tm_start (MAY_ABORT);
128 if (x & abort_transaction)
131 t0 = __builtin___tm_load (global);
133 __builtin___tm_store (&global, t1);
135 __builtin___tm_abort ();
136 __builtin___tm_commit ();
140 static void *expand_regions (struct tm_region
*,
141 void *(*callback
)(struct tm_region
*, void *),
145 /* Return the attributes we want to examine for X, or NULL if it's not
146 something we examine. We look at function types, but allow pointers
147 to function types and function decls and peek through. */
150 get_attrs_for (const_tree x
)
152 switch (TREE_CODE (x
))
155 return TYPE_ATTRIBUTES (TREE_TYPE (x
));
162 if (TREE_CODE (x
) != POINTER_TYPE
)
168 if (TREE_CODE (x
) != FUNCTION_TYPE
&& TREE_CODE (x
) != METHOD_TYPE
)
174 return TYPE_ATTRIBUTES (x
);
178 /* Return true if X has been marked TM_PURE. */
181 is_tm_pure (const_tree x
)
185 switch (TREE_CODE (x
))
196 if (TREE_CODE (x
) != POINTER_TYPE
)
202 if (TREE_CODE (x
) != FUNCTION_TYPE
&& TREE_CODE (x
) != METHOD_TYPE
)
207 flags
= flags_from_decl_or_type (x
);
208 return (flags
& ECF_TM_PURE
) != 0;
211 /* Return true if X has been marked TM_IRREVOCABLE. */
214 is_tm_irrevocable (tree x
)
216 tree attrs
= get_attrs_for (x
);
218 if (attrs
&& lookup_attribute ("transaction_unsafe", attrs
))
221 /* A call to the irrevocable builtin is by definition,
223 if (TREE_CODE (x
) == ADDR_EXPR
)
224 x
= TREE_OPERAND (x
, 0);
225 if (TREE_CODE (x
) == FUNCTION_DECL
226 && DECL_BUILT_IN_CLASS (x
) == BUILT_IN_NORMAL
227 && DECL_FUNCTION_CODE (x
) == BUILT_IN_TM_IRREVOCABLE
)
233 /* Return true if X has been marked TM_SAFE. */
236 is_tm_safe (const_tree x
)
240 tree attrs
= get_attrs_for (x
);
243 if (lookup_attribute ("transaction_safe", attrs
))
245 if (lookup_attribute ("transaction_may_cancel_outer", attrs
))
252 /* Return true if CALL is const, or tm_pure. */
255 is_tm_pure_call (gimple call
)
257 tree fn
= gimple_call_fn (call
);
259 if (TREE_CODE (fn
) == ADDR_EXPR
)
261 fn
= TREE_OPERAND (fn
, 0);
262 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
267 return is_tm_pure (fn
);
270 /* Return true if X has been marked TM_CALLABLE. */
273 is_tm_callable (tree x
)
275 tree attrs
= get_attrs_for (x
);
278 if (lookup_attribute ("transaction_callable", attrs
))
280 if (lookup_attribute ("transaction_safe", attrs
))
282 if (lookup_attribute ("transaction_may_cancel_outer", attrs
))
288 /* Return true if X has been marked TRANSACTION_MAY_CANCEL_OUTER. */
291 is_tm_may_cancel_outer (tree x
)
293 tree attrs
= get_attrs_for (x
);
295 return lookup_attribute ("transaction_may_cancel_outer", attrs
) != NULL
;
299 /* Return true for built in functions that "end" a transaction. */
302 is_tm_ending_fndecl (tree fndecl
)
304 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
305 switch (DECL_FUNCTION_CODE (fndecl
))
307 case BUILT_IN_TM_COMMIT
:
308 case BUILT_IN_TM_COMMIT_EH
:
309 case BUILT_IN_TM_ABORT
:
310 case BUILT_IN_TM_IRREVOCABLE
:
319 /* Return true if STMT is a TM load. */
322 is_tm_load (gimple stmt
)
326 if (gimple_code (stmt
) != GIMPLE_CALL
)
329 fndecl
= gimple_call_fndecl (stmt
);
330 return (fndecl
&& DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
331 && BUILTIN_TM_LOAD_P (DECL_FUNCTION_CODE (fndecl
)));
334 /* Same as above, but for simple TM loads, that is, not the
335 after-write, after-read, etc optimized variants. */
338 is_tm_simple_load (gimple stmt
)
342 if (gimple_code (stmt
) != GIMPLE_CALL
)
345 fndecl
= gimple_call_fndecl (stmt
);
346 if (fndecl
&& DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
348 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
349 return (fcode
== BUILT_IN_TM_LOAD_1
350 || fcode
== BUILT_IN_TM_LOAD_2
351 || fcode
== BUILT_IN_TM_LOAD_4
352 || fcode
== BUILT_IN_TM_LOAD_8
353 || fcode
== BUILT_IN_TM_LOAD_FLOAT
354 || fcode
== BUILT_IN_TM_LOAD_DOUBLE
355 || fcode
== BUILT_IN_TM_LOAD_LDOUBLE
356 || fcode
== BUILT_IN_TM_LOAD_M64
357 || fcode
== BUILT_IN_TM_LOAD_M128
358 || fcode
== BUILT_IN_TM_LOAD_M256
);
363 /* Return true if STMT is a TM store. */
366 is_tm_store (gimple stmt
)
370 if (gimple_code (stmt
) != GIMPLE_CALL
)
373 fndecl
= gimple_call_fndecl (stmt
);
374 return (fndecl
&& DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
375 && BUILTIN_TM_STORE_P (DECL_FUNCTION_CODE (fndecl
)));
378 /* Same as above, but for simple TM stores, that is, not the
379 after-write, after-read, etc optimized variants. */
382 is_tm_simple_store (gimple stmt
)
386 if (gimple_code (stmt
) != GIMPLE_CALL
)
389 fndecl
= gimple_call_fndecl (stmt
);
390 if (fndecl
&& DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
392 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
393 return (fcode
== BUILT_IN_TM_STORE_1
394 || fcode
== BUILT_IN_TM_STORE_2
395 || fcode
== BUILT_IN_TM_STORE_4
396 || fcode
== BUILT_IN_TM_STORE_8
397 || fcode
== BUILT_IN_TM_STORE_FLOAT
398 || fcode
== BUILT_IN_TM_STORE_DOUBLE
399 || fcode
== BUILT_IN_TM_STORE_LDOUBLE
400 || fcode
== BUILT_IN_TM_STORE_M64
401 || fcode
== BUILT_IN_TM_STORE_M128
402 || fcode
== BUILT_IN_TM_STORE_M256
);
407 /* Return true if FNDECL is BUILT_IN_TM_ABORT. */
410 is_tm_abort (tree fndecl
)
413 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
414 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_TM_ABORT
);
417 /* Build a GENERIC tree for a user abort. This is called by front ends
418 while transforming the __tm_abort statement. */
421 build_tm_abort_call (location_t loc
, bool is_outer
)
423 return build_call_expr_loc (loc
, builtin_decl_explicit (BUILT_IN_TM_ABORT
), 1,
424 build_int_cst (integer_type_node
,
426 | (is_outer
? AR_OUTERABORT
: 0)));
429 /* Common gateing function for several of the TM passes. */
437 /* Map for aribtrary function replacement under TM, as created
438 by the tm_wrap attribute. */
440 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map
)))
444 record_tm_replacement (tree from
, tree to
)
446 struct tree_map
**slot
, *h
;
448 /* Do not inline wrapper functions that will get replaced in the TM
451 Suppose you have foo() that will get replaced into tmfoo(). Make
452 sure the inliner doesn't try to outsmart us and inline foo()
453 before we get a chance to do the TM replacement. */
454 DECL_UNINLINABLE (from
) = 1;
456 if (tm_wrap_map
== NULL
)
457 tm_wrap_map
= htab_create_ggc (32, tree_map_hash
, tree_map_eq
, 0);
459 h
= ggc_alloc_tree_map ();
460 h
->hash
= htab_hash_pointer (from
);
464 slot
= (struct tree_map
**)
465 htab_find_slot_with_hash (tm_wrap_map
, h
, h
->hash
, INSERT
);
469 /* Return a TM-aware replacement function for DECL. */
472 find_tm_replacement_function (tree fndecl
)
476 struct tree_map
*h
, in
;
478 in
.base
.from
= fndecl
;
479 in
.hash
= htab_hash_pointer (fndecl
);
480 h
= (struct tree_map
*) htab_find_with_hash (tm_wrap_map
, &in
, in
.hash
);
485 /* ??? We may well want TM versions of most of the common <string.h>
486 functions. For now, we've already these two defined. */
487 /* Adjust expand_call_tm() attributes as necessary for the cases
489 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
490 switch (DECL_FUNCTION_CODE (fndecl
))
492 case BUILT_IN_MEMCPY
:
493 return builtin_decl_explicit (BUILT_IN_TM_MEMCPY
);
494 case BUILT_IN_MEMMOVE
:
495 return builtin_decl_explicit (BUILT_IN_TM_MEMMOVE
);
496 case BUILT_IN_MEMSET
:
497 return builtin_decl_explicit (BUILT_IN_TM_MEMSET
);
505 /* When appropriate, record TM replacement for memory allocation functions.
507 FROM is the FNDECL to wrap. */
509 tm_malloc_replacement (tree from
)
514 if (TREE_CODE (from
) != FUNCTION_DECL
)
517 /* If we have a previous replacement, the user must be explicitly
518 wrapping malloc/calloc/free. They better know what they're
520 if (find_tm_replacement_function (from
))
523 str
= IDENTIFIER_POINTER (DECL_NAME (from
));
525 if (!strcmp (str
, "malloc"))
526 to
= builtin_decl_explicit (BUILT_IN_TM_MALLOC
);
527 else if (!strcmp (str
, "calloc"))
528 to
= builtin_decl_explicit (BUILT_IN_TM_CALLOC
);
529 else if (!strcmp (str
, "free"))
530 to
= builtin_decl_explicit (BUILT_IN_TM_FREE
);
534 TREE_NOTHROW (to
) = 0;
536 record_tm_replacement (from
, to
);
539 /* Diagnostics for tm_safe functions/regions. Called by the front end
540 once we've lowered the function to high-gimple. */
542 /* Subroutine of diagnose_tm_safe_errors, called through walk_gimple_seq.
543 Process exactly one statement. WI->INFO is set to non-null when in
544 the context of a tm_safe function, and null for a __transaction block. */
546 #define DIAG_TM_OUTER 1
547 #define DIAG_TM_SAFE 2
548 #define DIAG_TM_RELAXED 4
552 unsigned int summary_flags
: 8;
553 unsigned int block_flags
: 8;
554 unsigned int func_flags
: 8;
555 unsigned int saw_volatile
: 1;
559 /* Return true if T is a volatile variable of some kind. */
562 volatile_var_p (tree t
)
564 return (SSA_VAR_P (t
)
565 && TREE_THIS_VOLATILE (TREE_TYPE (t
)));
568 /* Tree callback function for diagnose_tm pass. */
571 diagnose_tm_1_op (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
574 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
575 struct diagnose_tm
*d
= (struct diagnose_tm
*) wi
->info
;
577 if (volatile_var_p (*tp
)
578 && d
->block_flags
& DIAG_TM_SAFE
582 error_at (gimple_location (d
->stmt
),
583 "invalid volatile use of %qD inside transaction",
591 diagnose_tm_1 (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
592 struct walk_stmt_info
*wi
)
594 gimple stmt
= gsi_stmt (*gsi
);
595 struct diagnose_tm
*d
= (struct diagnose_tm
*) wi
->info
;
597 /* Save stmt for use in leaf analysis. */
600 switch (gimple_code (stmt
))
604 tree fn
= gimple_call_fn (stmt
);
606 if ((d
->summary_flags
& DIAG_TM_OUTER
) == 0
607 && is_tm_may_cancel_outer (fn
))
608 error_at (gimple_location (stmt
),
609 "%<transaction_may_cancel_outer%> function call not within"
610 " outer transaction or %<transaction_may_cancel_outer%>");
612 if (d
->summary_flags
& DIAG_TM_SAFE
)
614 bool is_safe
, direct_call_p
;
617 if (TREE_CODE (fn
) == ADDR_EXPR
618 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
)
620 direct_call_p
= true;
621 replacement
= TREE_OPERAND (fn
, 0);
622 replacement
= find_tm_replacement_function (replacement
);
628 direct_call_p
= false;
629 replacement
= NULL_TREE
;
632 if (is_tm_safe_or_pure (fn
))
634 else if (is_tm_callable (fn
) || is_tm_irrevocable (fn
))
636 /* A function explicitly marked transaction_callable as
637 opposed to transaction_safe is being defined to be
638 unsafe as part of its ABI, regardless of its contents. */
641 else if (direct_call_p
)
643 if (flags_from_decl_or_type (fn
) & ECF_TM_BUILTIN
)
645 else if (replacement
)
647 /* ??? At present we've been considering replacements
648 merely transaction_callable, and therefore might
649 enter irrevocable. The tm_wrap attribute has not
650 yet made it into the new language spec. */
655 /* ??? Diagnostics for unmarked direct calls moved into
656 the IPA pass. Section 3.2 of the spec details how
657 functions not marked should be considered "implicitly
658 safe" based on having examined the function body. */
664 /* An unmarked indirect call. Consider it unsafe even
665 though optimization may yet figure out how to inline. */
671 if (TREE_CODE (fn
) == ADDR_EXPR
)
672 fn
= TREE_OPERAND (fn
, 0);
673 if (d
->block_flags
& DIAG_TM_SAFE
)
676 error_at (gimple_location (stmt
),
677 "unsafe function call %qD within "
678 "atomic transaction", fn
);
681 if (!DECL_P (fn
) || DECL_NAME (fn
))
682 error_at (gimple_location (stmt
),
683 "unsafe function call %qE within "
684 "atomic transaction", fn
);
686 error_at (gimple_location (stmt
),
687 "unsafe indirect function call within "
688 "atomic transaction");
694 error_at (gimple_location (stmt
),
695 "unsafe function call %qD within "
696 "%<transaction_safe%> function", fn
);
699 if (!DECL_P (fn
) || DECL_NAME (fn
))
700 error_at (gimple_location (stmt
),
701 "unsafe function call %qE within "
702 "%<transaction_safe%> function", fn
);
704 error_at (gimple_location (stmt
),
705 "unsafe indirect function call within "
706 "%<transaction_safe%> function");
715 /* ??? We ought to come up with a way to add attributes to
716 asm statements, and then add "transaction_safe" to it.
717 Either that or get the language spec to resurrect __tm_waiver. */
718 if (d
->block_flags
& DIAG_TM_SAFE
)
719 error_at (gimple_location (stmt
),
720 "asm not allowed in atomic transaction");
721 else if (d
->func_flags
& DIAG_TM_SAFE
)
722 error_at (gimple_location (stmt
),
723 "asm not allowed in %<transaction_safe%> function");
726 case GIMPLE_TRANSACTION
:
728 unsigned char inner_flags
= DIAG_TM_SAFE
;
730 if (gimple_transaction_subcode (stmt
) & GTMA_IS_RELAXED
)
732 if (d
->block_flags
& DIAG_TM_SAFE
)
733 error_at (gimple_location (stmt
),
734 "relaxed transaction in atomic transaction");
735 else if (d
->func_flags
& DIAG_TM_SAFE
)
736 error_at (gimple_location (stmt
),
737 "relaxed transaction in %<transaction_safe%> function");
738 inner_flags
= DIAG_TM_RELAXED
;
740 else if (gimple_transaction_subcode (stmt
) & GTMA_IS_OUTER
)
743 error_at (gimple_location (stmt
),
744 "outer transaction in transaction");
745 else if (d
->func_flags
& DIAG_TM_OUTER
)
746 error_at (gimple_location (stmt
),
747 "outer transaction in "
748 "%<transaction_may_cancel_outer%> function");
749 else if (d
->func_flags
& DIAG_TM_SAFE
)
750 error_at (gimple_location (stmt
),
751 "outer transaction in %<transaction_safe%> function");
752 inner_flags
|= DIAG_TM_OUTER
;
755 *handled_ops_p
= true;
756 if (gimple_transaction_body (stmt
))
758 struct walk_stmt_info wi_inner
;
759 struct diagnose_tm d_inner
;
761 memset (&d_inner
, 0, sizeof (d_inner
));
762 d_inner
.func_flags
= d
->func_flags
;
763 d_inner
.block_flags
= d
->block_flags
| inner_flags
;
764 d_inner
.summary_flags
= d_inner
.func_flags
| d_inner
.block_flags
;
766 memset (&wi_inner
, 0, sizeof (wi_inner
));
767 wi_inner
.info
= &d_inner
;
769 walk_gimple_seq (gimple_transaction_body (stmt
),
770 diagnose_tm_1
, diagnose_tm_1_op
, &wi_inner
);
783 diagnose_tm_blocks (void)
785 struct walk_stmt_info wi
;
786 struct diagnose_tm d
;
788 memset (&d
, 0, sizeof (d
));
789 if (is_tm_may_cancel_outer (current_function_decl
))
790 d
.func_flags
= DIAG_TM_OUTER
| DIAG_TM_SAFE
;
791 else if (is_tm_safe (current_function_decl
))
792 d
.func_flags
= DIAG_TM_SAFE
;
793 d
.summary_flags
= d
.func_flags
;
795 memset (&wi
, 0, sizeof (wi
));
798 walk_gimple_seq (gimple_body (current_function_decl
),
799 diagnose_tm_1
, diagnose_tm_1_op
, &wi
);
806 const pass_data pass_data_diagnose_tm_blocks
=
808 GIMPLE_PASS
, /* type */
809 "*diagnose_tm_blocks", /* name */
810 OPTGROUP_NONE
, /* optinfo_flags */
812 true, /* has_execute */
813 TV_TRANS_MEM
, /* tv_id */
814 PROP_gimple_any
, /* properties_required */
815 0, /* properties_provided */
816 0, /* properties_destroyed */
817 0, /* todo_flags_start */
818 0, /* todo_flags_finish */
821 class pass_diagnose_tm_blocks
: public gimple_opt_pass
824 pass_diagnose_tm_blocks(gcc::context
*ctxt
)
825 : gimple_opt_pass(pass_data_diagnose_tm_blocks
, ctxt
)
828 /* opt_pass methods: */
829 bool gate () { return gate_tm (); }
830 unsigned int execute () { return diagnose_tm_blocks (); }
832 }; // class pass_diagnose_tm_blocks
837 make_pass_diagnose_tm_blocks (gcc::context
*ctxt
)
839 return new pass_diagnose_tm_blocks (ctxt
);
842 /* Instead of instrumenting thread private memory, we save the
843 addresses in a log which we later use to save/restore the addresses
844 upon transaction start/restart.
846 The log is keyed by address, where each element contains individual
847 statements among different code paths that perform the store.
849 This log is later used to generate either plain save/restore of the
850 addresses upon transaction start/restart, or calls to the ITM_L*
853 So for something like:
855 struct large { int x[1000]; };
856 struct large lala = { 0 };
862 We can either save/restore:
865 trxn = _ITM_startTransaction ();
866 if (trxn & a_saveLiveVariables)
867 tmp_lala1 = lala.x[i];
868 else if (a & a_restoreLiveVariables)
869 lala.x[i] = tmp_lala1;
871 or use the logging functions:
874 trxn = _ITM_startTransaction ();
875 _ITM_LU4 (&lala.x[i]);
877 Obviously, if we use _ITM_L* to log, we prefer to call _ITM_L* as
878 far up the dominator tree to shadow all of the writes to a given
879 location (thus reducing the total number of logging calls), but not
880 so high as to be called on a path that does not perform a
883 /* One individual log entry. We may have multiple statements for the
884 same location if neither dominate each other (on different
886 typedef struct tm_log_entry
888 /* Address to save. */
890 /* Entry block for the transaction this address occurs in. */
891 basic_block entry_block
;
892 /* Dominating statements the store occurs in. */
894 /* Initially, while we are building the log, we place a nonzero
895 value here to mean that this address *will* be saved with a
896 save/restore sequence. Later, when generating the save sequence
897 we place the SSA temp generated here. */
902 /* Log entry hashtable helpers. */
904 struct log_entry_hasher
906 typedef tm_log_entry value_type
;
907 typedef tm_log_entry compare_type
;
908 static inline hashval_t
hash (const value_type
*);
909 static inline bool equal (const value_type
*, const compare_type
*);
910 static inline void remove (value_type
*);
913 /* Htab support. Return hash value for a `tm_log_entry'. */
915 log_entry_hasher::hash (const value_type
*log
)
917 return iterative_hash_expr (log
->addr
, 0);
920 /* Htab support. Return true if two log entries are the same. */
922 log_entry_hasher::equal (const value_type
*log1
, const compare_type
*log2
)
926 rth: I suggest that we get rid of the component refs etc.
927 I.e. resolve the reference to base + offset.
929 We may need to actually finish a merge with mainline for this,
930 since we'd like to be presented with Richi's MEM_REF_EXPRs more
931 often than not. But in the meantime your tm_log_entry could save
932 the results of get_inner_reference.
934 See: g++.dg/tm/pr46653.C
937 /* Special case plain equality because operand_equal_p() below will
938 return FALSE if the addresses are equal but they have
939 side-effects (e.g. a volatile address). */
940 if (log1
->addr
== log2
->addr
)
943 return operand_equal_p (log1
->addr
, log2
->addr
, 0);
946 /* Htab support. Free one tm_log_entry. */
948 log_entry_hasher::remove (value_type
*lp
)
950 lp
->stmts
.release ();
955 /* The actual log. */
956 static hash_table
<log_entry_hasher
> tm_log
;
958 /* Addresses to log with a save/restore sequence. These should be in
960 static vec
<tree
> tm_log_save_addresses
;
962 enum thread_memory_type
966 mem_transaction_local
,
970 typedef struct tm_new_mem_map
972 /* SSA_NAME being dereferenced. */
974 enum thread_memory_type local_new_memory
;
977 /* Hashtable helpers. */
979 struct tm_mem_map_hasher
: typed_free_remove
<tm_new_mem_map_t
>
981 typedef tm_new_mem_map_t value_type
;
982 typedef tm_new_mem_map_t compare_type
;
983 static inline hashval_t
hash (const value_type
*);
984 static inline bool equal (const value_type
*, const compare_type
*);
988 tm_mem_map_hasher::hash (const value_type
*v
)
990 return (intptr_t)v
->val
>> 4;
994 tm_mem_map_hasher::equal (const value_type
*v
, const compare_type
*c
)
996 return v
->val
== c
->val
;
999 /* Map for an SSA_NAME originally pointing to a non aliased new piece
1000 of memory (malloc, alloc, etc). */
1001 static hash_table
<tm_mem_map_hasher
> tm_new_mem_hash
;
1003 /* Initialize logging data structures. */
1008 tm_new_mem_hash
.create (5);
1009 tm_log_save_addresses
.create (5);
1012 /* Free logging data structures. */
1014 tm_log_delete (void)
1017 tm_new_mem_hash
.dispose ();
1018 tm_log_save_addresses
.release ();
1021 /* Return true if MEM is a transaction invariant memory for the TM
1022 region starting at REGION_ENTRY_BLOCK. */
1024 transaction_invariant_address_p (const_tree mem
, basic_block region_entry_block
)
1026 if ((TREE_CODE (mem
) == INDIRECT_REF
|| TREE_CODE (mem
) == MEM_REF
)
1027 && TREE_CODE (TREE_OPERAND (mem
, 0)) == SSA_NAME
)
1031 def_bb
= gimple_bb (SSA_NAME_DEF_STMT (TREE_OPERAND (mem
, 0)));
1032 return def_bb
!= region_entry_block
1033 && dominated_by_p (CDI_DOMINATORS
, region_entry_block
, def_bb
);
1036 mem
= strip_invariant_refs (mem
);
1037 return mem
&& (CONSTANT_CLASS_P (mem
) || decl_address_invariant_p (mem
));
1040 /* Given an address ADDR in STMT, find it in the memory log or add it,
1041 making sure to keep only the addresses highest in the dominator
1044 ENTRY_BLOCK is the entry_block for the transaction.
1046 If we find the address in the log, make sure it's either the same
1047 address, or an equivalent one that dominates ADDR.
1049 If we find the address, but neither ADDR dominates the found
1050 address, nor the found one dominates ADDR, we're on different
1051 execution paths. Add it.
1053 If known, ENTRY_BLOCK is the entry block for the region, otherwise
1056 tm_log_add (basic_block entry_block
, tree addr
, gimple stmt
)
1058 tm_log_entry
**slot
;
1059 struct tm_log_entry l
, *lp
;
1062 slot
= tm_log
.find_slot (&l
, INSERT
);
1065 tree type
= TREE_TYPE (addr
);
1067 lp
= XNEW (struct tm_log_entry
);
1071 /* Small invariant addresses can be handled as save/restores. */
1073 && transaction_invariant_address_p (lp
->addr
, entry_block
)
1074 && TYPE_SIZE_UNIT (type
) != NULL
1075 && host_integerp (TYPE_SIZE_UNIT (type
), 1)
1076 && (tree_low_cst (TYPE_SIZE_UNIT (type
), 1)
1077 < PARAM_VALUE (PARAM_TM_MAX_AGGREGATE_SIZE
))
1078 /* We must be able to copy this type normally. I.e., no
1079 special constructors and the like. */
1080 && !TREE_ADDRESSABLE (type
))
1082 lp
->save_var
= create_tmp_reg (TREE_TYPE (lp
->addr
), "tm_save");
1083 lp
->stmts
.create (0);
1084 lp
->entry_block
= entry_block
;
1085 /* Save addresses separately in dominator order so we don't
1086 get confused by overlapping addresses in the save/restore
1088 tm_log_save_addresses
.safe_push (lp
->addr
);
1092 /* Use the logging functions. */
1093 lp
->stmts
.create (5);
1094 lp
->stmts
.quick_push (stmt
);
1095 lp
->save_var
= NULL
;
1105 /* If we're generating a save/restore sequence, we don't care
1106 about statements. */
1110 for (i
= 0; lp
->stmts
.iterate (i
, &oldstmt
); ++i
)
1112 if (stmt
== oldstmt
)
1114 /* We already have a store to the same address, higher up the
1115 dominator tree. Nothing to do. */
1116 if (dominated_by_p (CDI_DOMINATORS
,
1117 gimple_bb (stmt
), gimple_bb (oldstmt
)))
1119 /* We should be processing blocks in dominator tree order. */
1120 gcc_assert (!dominated_by_p (CDI_DOMINATORS
,
1121 gimple_bb (oldstmt
), gimple_bb (stmt
)));
1123 /* Store is on a different code path. */
1124 lp
->stmts
.safe_push (stmt
);
1128 /* Gimplify the address of a TARGET_MEM_REF. Return the SSA_NAME
1129 result, insert the new statements before GSI. */
1132 gimplify_addr (gimple_stmt_iterator
*gsi
, tree x
)
1134 if (TREE_CODE (x
) == TARGET_MEM_REF
)
1135 x
= tree_mem_ref_addr (build_pointer_type (TREE_TYPE (x
)), x
);
1137 x
= build_fold_addr_expr (x
);
1138 return force_gimple_operand_gsi (gsi
, x
, true, NULL
, true, GSI_SAME_STMT
);
1141 /* Instrument one address with the logging functions.
1142 ADDR is the address to save.
1143 STMT is the statement before which to place it. */
1145 tm_log_emit_stmt (tree addr
, gimple stmt
)
1147 tree type
= TREE_TYPE (addr
);
1148 tree size
= TYPE_SIZE_UNIT (type
);
1149 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
1151 enum built_in_function code
= BUILT_IN_TM_LOG
;
1153 if (type
== float_type_node
)
1154 code
= BUILT_IN_TM_LOG_FLOAT
;
1155 else if (type
== double_type_node
)
1156 code
= BUILT_IN_TM_LOG_DOUBLE
;
1157 else if (type
== long_double_type_node
)
1158 code
= BUILT_IN_TM_LOG_LDOUBLE
;
1159 else if (host_integerp (size
, 1))
1161 unsigned int n
= tree_low_cst (size
, 1);
1165 code
= BUILT_IN_TM_LOG_1
;
1168 code
= BUILT_IN_TM_LOG_2
;
1171 code
= BUILT_IN_TM_LOG_4
;
1174 code
= BUILT_IN_TM_LOG_8
;
1177 code
= BUILT_IN_TM_LOG
;
1178 if (TREE_CODE (type
) == VECTOR_TYPE
)
1180 if (n
== 8 && builtin_decl_explicit (BUILT_IN_TM_LOG_M64
))
1181 code
= BUILT_IN_TM_LOG_M64
;
1182 else if (n
== 16 && builtin_decl_explicit (BUILT_IN_TM_LOG_M128
))
1183 code
= BUILT_IN_TM_LOG_M128
;
1184 else if (n
== 32 && builtin_decl_explicit (BUILT_IN_TM_LOG_M256
))
1185 code
= BUILT_IN_TM_LOG_M256
;
1191 addr
= gimplify_addr (&gsi
, addr
);
1192 if (code
== BUILT_IN_TM_LOG
)
1193 log
= gimple_build_call (builtin_decl_explicit (code
), 2, addr
, size
);
1195 log
= gimple_build_call (builtin_decl_explicit (code
), 1, addr
);
1196 gsi_insert_before (&gsi
, log
, GSI_SAME_STMT
);
1199 /* Go through the log and instrument address that must be instrumented
1200 with the logging functions. Leave the save/restore addresses for
1205 hash_table
<log_entry_hasher
>::iterator hi
;
1206 struct tm_log_entry
*lp
;
1208 FOR_EACH_HASH_TABLE_ELEMENT (tm_log
, lp
, tm_log_entry_t
, hi
)
1215 fprintf (dump_file
, "TM thread private mem logging: ");
1216 print_generic_expr (dump_file
, lp
->addr
, 0);
1217 fprintf (dump_file
, "\n");
1223 fprintf (dump_file
, "DUMPING to variable\n");
1229 fprintf (dump_file
, "DUMPING with logging functions\n");
1230 for (i
= 0; lp
->stmts
.iterate (i
, &stmt
); ++i
)
1231 tm_log_emit_stmt (lp
->addr
, stmt
);
1236 /* Emit the save sequence for the corresponding addresses in the log.
1237 ENTRY_BLOCK is the entry block for the transaction.
1238 BB is the basic block to insert the code in. */
1240 tm_log_emit_saves (basic_block entry_block
, basic_block bb
)
1243 gimple_stmt_iterator gsi
= gsi_last_bb (bb
);
1245 struct tm_log_entry l
, *lp
;
1247 for (i
= 0; i
< tm_log_save_addresses
.length (); ++i
)
1249 l
.addr
= tm_log_save_addresses
[i
];
1250 lp
= *(tm_log
.find_slot (&l
, NO_INSERT
));
1251 gcc_assert (lp
->save_var
!= NULL
);
1253 /* We only care about variables in the current transaction. */
1254 if (lp
->entry_block
!= entry_block
)
1257 stmt
= gimple_build_assign (lp
->save_var
, unshare_expr (lp
->addr
));
1259 /* Make sure we can create an SSA_NAME for this type. For
1260 instance, aggregates aren't allowed, in which case the system
1261 will create a VOP for us and everything will just work. */
1262 if (is_gimple_reg_type (TREE_TYPE (lp
->save_var
)))
1264 lp
->save_var
= make_ssa_name (lp
->save_var
, stmt
);
1265 gimple_assign_set_lhs (stmt
, lp
->save_var
);
1268 gsi_insert_before (&gsi
, stmt
, GSI_SAME_STMT
);
1272 /* Emit the restore sequence for the corresponding addresses in the log.
1273 ENTRY_BLOCK is the entry block for the transaction.
1274 BB is the basic block to insert the code in. */
1276 tm_log_emit_restores (basic_block entry_block
, basic_block bb
)
1279 struct tm_log_entry l
, *lp
;
1280 gimple_stmt_iterator gsi
;
1283 for (i
= tm_log_save_addresses
.length () - 1; i
>= 0; i
--)
1285 l
.addr
= tm_log_save_addresses
[i
];
1286 lp
= *(tm_log
.find_slot (&l
, NO_INSERT
));
1287 gcc_assert (lp
->save_var
!= NULL
);
1289 /* We only care about variables in the current transaction. */
1290 if (lp
->entry_block
!= entry_block
)
1293 /* Restores are in LIFO order from the saves in case we have
1295 gsi
= gsi_start_bb (bb
);
1297 stmt
= gimple_build_assign (unshare_expr (lp
->addr
), lp
->save_var
);
1298 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
1303 static tree
lower_sequence_tm (gimple_stmt_iterator
*, bool *,
1304 struct walk_stmt_info
*);
1305 static tree
lower_sequence_no_tm (gimple_stmt_iterator
*, bool *,
1306 struct walk_stmt_info
*);
1308 /* Evaluate an address X being dereferenced and determine if it
1309 originally points to a non aliased new chunk of memory (malloc,
1312 Return MEM_THREAD_LOCAL if it points to a thread-local address.
1313 Return MEM_TRANSACTION_LOCAL if it points to a transaction-local address.
1314 Return MEM_NON_LOCAL otherwise.
1316 ENTRY_BLOCK is the entry block to the transaction containing the
1317 dereference of X. */
1318 static enum thread_memory_type
1319 thread_private_new_memory (basic_block entry_block
, tree x
)
1322 enum tree_code code
;
1323 tm_new_mem_map_t
**slot
;
1324 tm_new_mem_map_t elt
, *elt_p
;
1326 enum thread_memory_type retval
= mem_transaction_local
;
1329 || TREE_CODE (x
) != SSA_NAME
1330 /* Possible uninitialized use, or a function argument. In
1331 either case, we don't care. */
1332 || SSA_NAME_IS_DEFAULT_DEF (x
))
1333 return mem_non_local
;
1335 /* Look in cache first. */
1337 slot
= tm_new_mem_hash
.find_slot (&elt
, INSERT
);
1340 return elt_p
->local_new_memory
;
1342 /* Optimistically assume the memory is transaction local during
1343 processing. This catches recursion into this variable. */
1344 *slot
= elt_p
= XNEW (tm_new_mem_map_t
);
1346 elt_p
->local_new_memory
= mem_transaction_local
;
1348 /* Search DEF chain to find the original definition of this address. */
1351 if (ptr_deref_may_alias_global_p (x
))
1353 /* Address escapes. This is not thread-private. */
1354 retval
= mem_non_local
;
1355 goto new_memory_ret
;
1358 stmt
= SSA_NAME_DEF_STMT (x
);
1360 /* If the malloc call is outside the transaction, this is
1362 if (retval
!= mem_thread_local
1363 && !dominated_by_p (CDI_DOMINATORS
, gimple_bb (stmt
), entry_block
))
1364 retval
= mem_thread_local
;
1366 if (is_gimple_assign (stmt
))
1368 code
= gimple_assign_rhs_code (stmt
);
1369 /* x = foo ==> foo */
1370 if (code
== SSA_NAME
)
1371 x
= gimple_assign_rhs1 (stmt
);
1372 /* x = foo + n ==> foo */
1373 else if (code
== POINTER_PLUS_EXPR
)
1374 x
= gimple_assign_rhs1 (stmt
);
1375 /* x = (cast*) foo ==> foo */
1376 else if (code
== VIEW_CONVERT_EXPR
|| code
== NOP_EXPR
)
1377 x
= gimple_assign_rhs1 (stmt
);
1378 /* x = c ? op1 : op2 == > op1 or op2 just like a PHI */
1379 else if (code
== COND_EXPR
)
1381 tree op1
= gimple_assign_rhs2 (stmt
);
1382 tree op2
= gimple_assign_rhs3 (stmt
);
1383 enum thread_memory_type mem
;
1384 retval
= thread_private_new_memory (entry_block
, op1
);
1385 if (retval
== mem_non_local
)
1386 goto new_memory_ret
;
1387 mem
= thread_private_new_memory (entry_block
, op2
);
1388 retval
= MIN (retval
, mem
);
1389 goto new_memory_ret
;
1393 retval
= mem_non_local
;
1394 goto new_memory_ret
;
1399 if (gimple_code (stmt
) == GIMPLE_PHI
)
1402 enum thread_memory_type mem
;
1403 tree phi_result
= gimple_phi_result (stmt
);
1405 /* If any of the ancestors are non-local, we are sure to
1406 be non-local. Otherwise we can avoid doing anything
1407 and inherit what has already been generated. */
1409 for (i
= 0; i
< gimple_phi_num_args (stmt
); ++i
)
1411 tree op
= PHI_ARG_DEF (stmt
, i
);
1413 /* Exclude self-assignment. */
1414 if (phi_result
== op
)
1417 mem
= thread_private_new_memory (entry_block
, op
);
1418 if (mem
== mem_non_local
)
1421 goto new_memory_ret
;
1423 retval
= MIN (retval
, mem
);
1425 goto new_memory_ret
;
1430 while (TREE_CODE (x
) == SSA_NAME
);
1432 if (stmt
&& is_gimple_call (stmt
) && gimple_call_flags (stmt
) & ECF_MALLOC
)
1433 /* Thread-local or transaction-local. */
1436 retval
= mem_non_local
;
1439 elt_p
->local_new_memory
= retval
;
1443 /* Determine whether X has to be instrumented using a read
1446 ENTRY_BLOCK is the entry block for the region where stmt resides
1447 in. NULL if unknown.
1449 STMT is the statement in which X occurs in. It is used for thread
1450 private memory instrumentation. If no TPM instrumentation is
1451 desired, STMT should be null. */
1453 requires_barrier (basic_block entry_block
, tree x
, gimple stmt
)
1456 while (handled_component_p (x
))
1457 x
= TREE_OPERAND (x
, 0);
1459 switch (TREE_CODE (x
))
1464 enum thread_memory_type ret
;
1466 ret
= thread_private_new_memory (entry_block
, TREE_OPERAND (x
, 0));
1467 if (ret
== mem_non_local
)
1469 if (stmt
&& ret
== mem_thread_local
)
1470 /* ?? Should we pass `orig', or the INDIRECT_REF X. ?? */
1471 tm_log_add (entry_block
, orig
, stmt
);
1473 /* Transaction-locals require nothing at all. For malloc, a
1474 transaction restart frees the memory and we reallocate.
1475 For alloca, the stack pointer gets reset by the retry and
1480 case TARGET_MEM_REF
:
1481 if (TREE_CODE (TMR_BASE (x
)) != ADDR_EXPR
)
1483 x
= TREE_OPERAND (TMR_BASE (x
), 0);
1484 if (TREE_CODE (x
) == PARM_DECL
)
1486 gcc_assert (TREE_CODE (x
) == VAR_DECL
);
1492 if (DECL_BY_REFERENCE (x
))
1494 /* ??? This value is a pointer, but aggregate_value_p has been
1495 jigged to return true which confuses needs_to_live_in_memory.
1496 This ought to be cleaned up generically.
1498 FIXME: Verify this still happens after the next mainline
1499 merge. Testcase ie g++.dg/tm/pr47554.C.
1504 if (is_global_var (x
))
1505 return !TREE_READONLY (x
);
1506 if (/* FIXME: This condition should actually go below in the
1507 tm_log_add() call, however is_call_clobbered() depends on
1508 aliasing info which is not available during
1509 gimplification. Since requires_barrier() gets called
1510 during lower_sequence_tm/gimplification, leave the call
1511 to needs_to_live_in_memory until we eliminate
1512 lower_sequence_tm altogether. */
1513 needs_to_live_in_memory (x
))
1517 /* For local memory that doesn't escape (aka thread private
1518 memory), we can either save the value at the beginning of
1519 the transaction and restore on restart, or call a tm
1520 function to dynamically save and restore on restart
1523 tm_log_add (entry_block
, orig
, stmt
);
1532 /* Mark the GIMPLE_ASSIGN statement as appropriate for being inside
1533 a transaction region. */
1536 examine_assign_tm (unsigned *state
, gimple_stmt_iterator
*gsi
)
1538 gimple stmt
= gsi_stmt (*gsi
);
1540 if (requires_barrier (/*entry_block=*/NULL
, gimple_assign_rhs1 (stmt
), NULL
))
1541 *state
|= GTMA_HAVE_LOAD
;
1542 if (requires_barrier (/*entry_block=*/NULL
, gimple_assign_lhs (stmt
), NULL
))
1543 *state
|= GTMA_HAVE_STORE
;
1546 /* Mark a GIMPLE_CALL as appropriate for being inside a transaction. */
1549 examine_call_tm (unsigned *state
, gimple_stmt_iterator
*gsi
)
1551 gimple stmt
= gsi_stmt (*gsi
);
1554 if (is_tm_pure_call (stmt
))
1557 /* Check if this call is a transaction abort. */
1558 fn
= gimple_call_fndecl (stmt
);
1559 if (is_tm_abort (fn
))
1560 *state
|= GTMA_HAVE_ABORT
;
1562 /* Note that something may happen. */
1563 *state
|= GTMA_HAVE_LOAD
| GTMA_HAVE_STORE
;
1566 /* Lower a GIMPLE_TRANSACTION statement. */
1569 lower_transaction (gimple_stmt_iterator
*gsi
, struct walk_stmt_info
*wi
)
1571 gimple g
, stmt
= gsi_stmt (*gsi
);
1572 unsigned int *outer_state
= (unsigned int *) wi
->info
;
1573 unsigned int this_state
= 0;
1574 struct walk_stmt_info this_wi
;
1576 /* First, lower the body. The scanning that we do inside gives
1577 us some idea of what we're dealing with. */
1578 memset (&this_wi
, 0, sizeof (this_wi
));
1579 this_wi
.info
= (void *) &this_state
;
1580 walk_gimple_seq_mod (gimple_transaction_body_ptr (stmt
),
1581 lower_sequence_tm
, NULL
, &this_wi
);
1583 /* If there was absolutely nothing transaction related inside the
1584 transaction, we may elide it. Likewise if this is a nested
1585 transaction and does not contain an abort. */
1587 || (!(this_state
& GTMA_HAVE_ABORT
) && outer_state
!= NULL
))
1590 *outer_state
|= this_state
;
1592 gsi_insert_seq_before (gsi
, gimple_transaction_body (stmt
),
1594 gimple_transaction_set_body (stmt
, NULL
);
1596 gsi_remove (gsi
, true);
1597 wi
->removed_stmt
= true;
1601 /* Wrap the body of the transaction in a try-finally node so that
1602 the commit call is always properly called. */
1603 g
= gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_COMMIT
), 0);
1604 if (flag_exceptions
)
1607 gimple_seq n_seq
, e_seq
;
1609 n_seq
= gimple_seq_alloc_with_stmt (g
);
1612 g
= gimple_build_call (builtin_decl_explicit (BUILT_IN_EH_POINTER
),
1613 1, integer_zero_node
);
1614 ptr
= create_tmp_var (ptr_type_node
, NULL
);
1615 gimple_call_set_lhs (g
, ptr
);
1616 gimple_seq_add_stmt (&e_seq
, g
);
1618 g
= gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_COMMIT_EH
),
1620 gimple_seq_add_stmt (&e_seq
, g
);
1622 g
= gimple_build_eh_else (n_seq
, e_seq
);
1625 g
= gimple_build_try (gimple_transaction_body (stmt
),
1626 gimple_seq_alloc_with_stmt (g
), GIMPLE_TRY_FINALLY
);
1627 gsi_insert_after (gsi
, g
, GSI_CONTINUE_LINKING
);
1629 gimple_transaction_set_body (stmt
, NULL
);
1631 /* If the transaction calls abort or if this is an outer transaction,
1632 add an "over" label afterwards. */
1633 if ((this_state
& (GTMA_HAVE_ABORT
))
1634 || (gimple_transaction_subcode(stmt
) & GTMA_IS_OUTER
))
1636 tree label
= create_artificial_label (UNKNOWN_LOCATION
);
1637 gimple_transaction_set_label (stmt
, label
);
1638 gsi_insert_after (gsi
, gimple_build_label (label
), GSI_CONTINUE_LINKING
);
1641 /* Record the set of operations found for use later. */
1642 this_state
|= gimple_transaction_subcode (stmt
) & GTMA_DECLARATION_MASK
;
1643 gimple_transaction_set_subcode (stmt
, this_state
);
1646 /* Iterate through the statements in the sequence, lowering them all
1647 as appropriate for being in a transaction. */
1650 lower_sequence_tm (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1651 struct walk_stmt_info
*wi
)
1653 unsigned int *state
= (unsigned int *) wi
->info
;
1654 gimple stmt
= gsi_stmt (*gsi
);
1656 *handled_ops_p
= true;
1657 switch (gimple_code (stmt
))
1660 /* Only memory reads/writes need to be instrumented. */
1661 if (gimple_assign_single_p (stmt
))
1662 examine_assign_tm (state
, gsi
);
1666 examine_call_tm (state
, gsi
);
1670 *state
|= GTMA_MAY_ENTER_IRREVOCABLE
;
1673 case GIMPLE_TRANSACTION
:
1674 lower_transaction (gsi
, wi
);
1678 *handled_ops_p
= !gimple_has_substatements (stmt
);
1685 /* Iterate through the statements in the sequence, lowering them all
1686 as appropriate for being outside of a transaction. */
1689 lower_sequence_no_tm (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1690 struct walk_stmt_info
* wi
)
1692 gimple stmt
= gsi_stmt (*gsi
);
1694 if (gimple_code (stmt
) == GIMPLE_TRANSACTION
)
1696 *handled_ops_p
= true;
1697 lower_transaction (gsi
, wi
);
1700 *handled_ops_p
= !gimple_has_substatements (stmt
);
1705 /* Main entry point for flattening GIMPLE_TRANSACTION constructs. After
1706 this, GIMPLE_TRANSACTION nodes still exist, but the nested body has
1707 been moved out, and all the data required for constructing a proper
1708 CFG has been recorded. */
1711 execute_lower_tm (void)
1713 struct walk_stmt_info wi
;
1716 /* Transactional clones aren't created until a later pass. */
1717 gcc_assert (!decl_is_tm_clone (current_function_decl
));
1719 body
= gimple_body (current_function_decl
);
1720 memset (&wi
, 0, sizeof (wi
));
1721 walk_gimple_seq_mod (&body
, lower_sequence_no_tm
, NULL
, &wi
);
1722 gimple_set_body (current_function_decl
, body
);
1729 const pass_data pass_data_lower_tm
=
1731 GIMPLE_PASS
, /* type */
1732 "tmlower", /* name */
1733 OPTGROUP_NONE
, /* optinfo_flags */
1734 true, /* has_gate */
1735 true, /* has_execute */
1736 TV_TRANS_MEM
, /* tv_id */
1737 PROP_gimple_lcf
, /* properties_required */
1738 0, /* properties_provided */
1739 0, /* properties_destroyed */
1740 0, /* todo_flags_start */
1741 0, /* todo_flags_finish */
1744 class pass_lower_tm
: public gimple_opt_pass
1747 pass_lower_tm(gcc::context
*ctxt
)
1748 : gimple_opt_pass(pass_data_lower_tm
, ctxt
)
1751 /* opt_pass methods: */
1752 bool gate () { return gate_tm (); }
1753 unsigned int execute () { return execute_lower_tm (); }
1755 }; // class pass_lower_tm
1760 make_pass_lower_tm (gcc::context
*ctxt
)
1762 return new pass_lower_tm (ctxt
);
1765 /* Collect region information for each transaction. */
1769 /* Link to the next unnested transaction. */
1770 struct tm_region
*next
;
1772 /* Link to the next inner transaction. */
1773 struct tm_region
*inner
;
1775 /* Link to the next outer transaction. */
1776 struct tm_region
*outer
;
1778 /* The GIMPLE_TRANSACTION statement beginning this transaction.
1779 After TM_MARK, this gets replaced by a call to
1780 BUILT_IN_TM_START. */
1781 gimple transaction_stmt
;
1783 /* After TM_MARK expands the GIMPLE_TRANSACTION into a call to
1784 BUILT_IN_TM_START, this field is true if the transaction is an
1785 outer transaction. */
1786 bool original_transaction_was_outer
;
1788 /* Return value from BUILT_IN_TM_START. */
1791 /* The entry block to this region. This will always be the first
1792 block of the body of the transaction. */
1793 basic_block entry_block
;
1795 /* The first block after an expanded call to _ITM_beginTransaction. */
1796 basic_block restart_block
;
1798 /* The set of all blocks that end the region; NULL if only EXIT_BLOCK.
1799 These blocks are still a part of the region (i.e., the border is
1800 inclusive). Note that this set is only complete for paths in the CFG
1801 starting at ENTRY_BLOCK, and that there is no exit block recorded for
1802 the edge to the "over" label. */
1805 /* The set of all blocks that have an TM_IRREVOCABLE call. */
1809 typedef struct tm_region
*tm_region_p
;
1811 /* True if there are pending edge statements to be committed for the
1812 current function being scanned in the tmmark pass. */
1813 bool pending_edge_inserts_p
;
1815 static struct tm_region
*all_tm_regions
;
1816 static bitmap_obstack tm_obstack
;
1819 /* A subroutine of tm_region_init. Record the existence of the
1820 GIMPLE_TRANSACTION statement in a tree of tm_region elements. */
1822 static struct tm_region
*
1823 tm_region_init_0 (struct tm_region
*outer
, basic_block bb
, gimple stmt
)
1825 struct tm_region
*region
;
1827 region
= (struct tm_region
*)
1828 obstack_alloc (&tm_obstack
.obstack
, sizeof (struct tm_region
));
1832 region
->next
= outer
->inner
;
1833 outer
->inner
= region
;
1837 region
->next
= all_tm_regions
;
1838 all_tm_regions
= region
;
1840 region
->inner
= NULL
;
1841 region
->outer
= outer
;
1843 region
->transaction_stmt
= stmt
;
1844 region
->original_transaction_was_outer
= false;
1845 region
->tm_state
= NULL
;
1847 /* There are either one or two edges out of the block containing
1848 the GIMPLE_TRANSACTION, one to the actual region and one to the
1849 "over" label if the region contains an abort. The former will
1850 always be the one marked FALLTHRU. */
1851 region
->entry_block
= FALLTHRU_EDGE (bb
)->dest
;
1853 region
->exit_blocks
= BITMAP_ALLOC (&tm_obstack
);
1854 region
->irr_blocks
= BITMAP_ALLOC (&tm_obstack
);
1859 /* A subroutine of tm_region_init. Record all the exit and
1860 irrevocable blocks in BB into the region's exit_blocks and
1861 irr_blocks bitmaps. Returns the new region being scanned. */
1863 static struct tm_region
*
1864 tm_region_init_1 (struct tm_region
*region
, basic_block bb
)
1866 gimple_stmt_iterator gsi
;
1870 || (!region
->irr_blocks
&& !region
->exit_blocks
))
1873 /* Check to see if this is the end of a region by seeing if it
1874 contains a call to __builtin_tm_commit{,_eh}. Note that the
1875 outermost region for DECL_IS_TM_CLONE need not collect this. */
1876 for (gsi
= gsi_last_bb (bb
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
1879 if (gimple_code (g
) == GIMPLE_CALL
)
1881 tree fn
= gimple_call_fndecl (g
);
1882 if (fn
&& DECL_BUILT_IN_CLASS (fn
) == BUILT_IN_NORMAL
)
1884 if ((DECL_FUNCTION_CODE (fn
) == BUILT_IN_TM_COMMIT
1885 || DECL_FUNCTION_CODE (fn
) == BUILT_IN_TM_COMMIT_EH
)
1886 && region
->exit_blocks
)
1888 bitmap_set_bit (region
->exit_blocks
, bb
->index
);
1889 region
= region
->outer
;
1892 if (DECL_FUNCTION_CODE (fn
) == BUILT_IN_TM_IRREVOCABLE
)
1893 bitmap_set_bit (region
->irr_blocks
, bb
->index
);
1900 /* Collect all of the transaction regions within the current function
1901 and record them in ALL_TM_REGIONS. The REGION parameter may specify
1902 an "outermost" region for use by tm clones. */
1905 tm_region_init (struct tm_region
*region
)
1911 vec
<basic_block
> queue
= vNULL
;
1912 bitmap visited_blocks
= BITMAP_ALLOC (NULL
);
1913 struct tm_region
*old_region
;
1914 vec
<tm_region_p
> bb_regions
= vNULL
;
1916 all_tm_regions
= region
;
1917 bb
= single_succ (ENTRY_BLOCK_PTR
);
1919 /* We could store this information in bb->aux, but we may get called
1920 through get_all_tm_blocks() from another pass that may be already
1922 bb_regions
.safe_grow_cleared (last_basic_block
);
1924 queue
.safe_push (bb
);
1925 bb_regions
[bb
->index
] = region
;
1929 region
= bb_regions
[bb
->index
];
1930 bb_regions
[bb
->index
] = NULL
;
1932 /* Record exit and irrevocable blocks. */
1933 region
= tm_region_init_1 (region
, bb
);
1935 /* Check for the last statement in the block beginning a new region. */
1937 old_region
= region
;
1938 if (g
&& gimple_code (g
) == GIMPLE_TRANSACTION
)
1939 region
= tm_region_init_0 (region
, bb
, g
);
1941 /* Process subsequent blocks. */
1942 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1943 if (!bitmap_bit_p (visited_blocks
, e
->dest
->index
))
1945 bitmap_set_bit (visited_blocks
, e
->dest
->index
);
1946 queue
.safe_push (e
->dest
);
1948 /* If the current block started a new region, make sure that only
1949 the entry block of the new region is associated with this region.
1950 Other successors are still part of the old region. */
1951 if (old_region
!= region
&& e
->dest
!= region
->entry_block
)
1952 bb_regions
[e
->dest
->index
] = old_region
;
1954 bb_regions
[e
->dest
->index
] = region
;
1957 while (!queue
.is_empty ());
1959 BITMAP_FREE (visited_blocks
);
1960 bb_regions
.release ();
1963 /* The "gate" function for all transactional memory expansion and optimization
1964 passes. We collect region information for each top-level transaction, and
1965 if we don't find any, we skip all of the TM passes. Each region will have
1966 all of the exit blocks recorded, and the originating statement. */
1974 calculate_dominance_info (CDI_DOMINATORS
);
1975 bitmap_obstack_initialize (&tm_obstack
);
1977 /* If the function is a TM_CLONE, then the entire function is the region. */
1978 if (decl_is_tm_clone (current_function_decl
))
1980 struct tm_region
*region
= (struct tm_region
*)
1981 obstack_alloc (&tm_obstack
.obstack
, sizeof (struct tm_region
));
1982 memset (region
, 0, sizeof (*region
));
1983 region
->entry_block
= single_succ (ENTRY_BLOCK_PTR
);
1984 /* For a clone, the entire function is the region. But even if
1985 we don't need to record any exit blocks, we may need to
1986 record irrevocable blocks. */
1987 region
->irr_blocks
= BITMAP_ALLOC (&tm_obstack
);
1989 tm_region_init (region
);
1993 tm_region_init (NULL
);
1995 /* If we didn't find any regions, cleanup and skip the whole tree
1996 of tm-related optimizations. */
1997 if (all_tm_regions
== NULL
)
1999 bitmap_obstack_release (&tm_obstack
);
2009 const pass_data pass_data_tm_init
=
2011 GIMPLE_PASS
, /* type */
2012 "*tminit", /* name */
2013 OPTGROUP_NONE
, /* optinfo_flags */
2014 true, /* has_gate */
2015 false, /* has_execute */
2016 TV_TRANS_MEM
, /* tv_id */
2017 ( PROP_ssa
| PROP_cfg
), /* properties_required */
2018 0, /* properties_provided */
2019 0, /* properties_destroyed */
2020 0, /* todo_flags_start */
2021 0, /* todo_flags_finish */
2024 class pass_tm_init
: public gimple_opt_pass
2027 pass_tm_init(gcc::context
*ctxt
)
2028 : gimple_opt_pass(pass_data_tm_init
, ctxt
)
2031 /* opt_pass methods: */
2032 bool gate () { return gate_tm_init (); }
2034 }; // class pass_tm_init
2039 make_pass_tm_init (gcc::context
*ctxt
)
2041 return new pass_tm_init (ctxt
);
2044 /* Add FLAGS to the GIMPLE_TRANSACTION subcode for the transaction region
2045 represented by STATE. */
2048 transaction_subcode_ior (struct tm_region
*region
, unsigned flags
)
2050 if (region
&& region
->transaction_stmt
)
2052 flags
|= gimple_transaction_subcode (region
->transaction_stmt
);
2053 gimple_transaction_set_subcode (region
->transaction_stmt
, flags
);
2057 /* Construct a memory load in a transactional context. Return the
2058 gimple statement performing the load, or NULL if there is no
2059 TM_LOAD builtin of the appropriate size to do the load.
2061 LOC is the location to use for the new statement(s). */
2064 build_tm_load (location_t loc
, tree lhs
, tree rhs
, gimple_stmt_iterator
*gsi
)
2066 enum built_in_function code
= END_BUILTINS
;
2067 tree t
, type
= TREE_TYPE (rhs
), decl
;
2070 if (type
== float_type_node
)
2071 code
= BUILT_IN_TM_LOAD_FLOAT
;
2072 else if (type
== double_type_node
)
2073 code
= BUILT_IN_TM_LOAD_DOUBLE
;
2074 else if (type
== long_double_type_node
)
2075 code
= BUILT_IN_TM_LOAD_LDOUBLE
;
2076 else if (TYPE_SIZE_UNIT (type
) != NULL
2077 && host_integerp (TYPE_SIZE_UNIT (type
), 1))
2079 switch (tree_low_cst (TYPE_SIZE_UNIT (type
), 1))
2082 code
= BUILT_IN_TM_LOAD_1
;
2085 code
= BUILT_IN_TM_LOAD_2
;
2088 code
= BUILT_IN_TM_LOAD_4
;
2091 code
= BUILT_IN_TM_LOAD_8
;
2096 if (code
== END_BUILTINS
)
2098 decl
= targetm
.vectorize
.builtin_tm_load (type
);
2103 decl
= builtin_decl_explicit (code
);
2105 t
= gimplify_addr (gsi
, rhs
);
2106 gcall
= gimple_build_call (decl
, 1, t
);
2107 gimple_set_location (gcall
, loc
);
2109 t
= TREE_TYPE (TREE_TYPE (decl
));
2110 if (useless_type_conversion_p (type
, t
))
2112 gimple_call_set_lhs (gcall
, lhs
);
2113 gsi_insert_before (gsi
, gcall
, GSI_SAME_STMT
);
2120 temp
= create_tmp_reg (t
, NULL
);
2121 gimple_call_set_lhs (gcall
, temp
);
2122 gsi_insert_before (gsi
, gcall
, GSI_SAME_STMT
);
2124 t
= fold_build1 (VIEW_CONVERT_EXPR
, type
, temp
);
2125 g
= gimple_build_assign (lhs
, t
);
2126 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
2133 /* Similarly for storing TYPE in a transactional context. */
2136 build_tm_store (location_t loc
, tree lhs
, tree rhs
, gimple_stmt_iterator
*gsi
)
2138 enum built_in_function code
= END_BUILTINS
;
2139 tree t
, fn
, type
= TREE_TYPE (rhs
), simple_type
;
2142 if (type
== float_type_node
)
2143 code
= BUILT_IN_TM_STORE_FLOAT
;
2144 else if (type
== double_type_node
)
2145 code
= BUILT_IN_TM_STORE_DOUBLE
;
2146 else if (type
== long_double_type_node
)
2147 code
= BUILT_IN_TM_STORE_LDOUBLE
;
2148 else if (TYPE_SIZE_UNIT (type
) != NULL
2149 && host_integerp (TYPE_SIZE_UNIT (type
), 1))
2151 switch (tree_low_cst (TYPE_SIZE_UNIT (type
), 1))
2154 code
= BUILT_IN_TM_STORE_1
;
2157 code
= BUILT_IN_TM_STORE_2
;
2160 code
= BUILT_IN_TM_STORE_4
;
2163 code
= BUILT_IN_TM_STORE_8
;
2168 if (code
== END_BUILTINS
)
2170 fn
= targetm
.vectorize
.builtin_tm_store (type
);
2175 fn
= builtin_decl_explicit (code
);
2177 simple_type
= TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn
))));
2179 if (TREE_CODE (rhs
) == CONSTRUCTOR
)
2181 /* Handle the easy initialization to zero. */
2182 if (!CONSTRUCTOR_ELTS (rhs
))
2183 rhs
= build_int_cst (simple_type
, 0);
2186 /* ...otherwise punt to the caller and probably use
2187 BUILT_IN_TM_MEMMOVE, because we can't wrap a
2188 VIEW_CONVERT_EXPR around a CONSTRUCTOR (below) and produce
2193 else if (!useless_type_conversion_p (simple_type
, type
))
2198 temp
= create_tmp_reg (simple_type
, NULL
);
2199 t
= fold_build1 (VIEW_CONVERT_EXPR
, simple_type
, rhs
);
2200 g
= gimple_build_assign (temp
, t
);
2201 gimple_set_location (g
, loc
);
2202 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
2207 t
= gimplify_addr (gsi
, lhs
);
2208 gcall
= gimple_build_call (fn
, 2, t
, rhs
);
2209 gimple_set_location (gcall
, loc
);
2210 gsi_insert_before (gsi
, gcall
, GSI_SAME_STMT
);
2216 /* Expand an assignment statement into transactional builtins. */
2219 expand_assign_tm (struct tm_region
*region
, gimple_stmt_iterator
*gsi
)
2221 gimple stmt
= gsi_stmt (*gsi
);
2222 location_t loc
= gimple_location (stmt
);
2223 tree lhs
= gimple_assign_lhs (stmt
);
2224 tree rhs
= gimple_assign_rhs1 (stmt
);
2225 bool store_p
= requires_barrier (region
->entry_block
, lhs
, NULL
);
2226 bool load_p
= requires_barrier (region
->entry_block
, rhs
, NULL
);
2227 gimple gcall
= NULL
;
2229 if (!load_p
&& !store_p
)
2231 /* Add thread private addresses to log if applicable. */
2232 requires_barrier (region
->entry_block
, lhs
, stmt
);
2237 // Remove original load/store statement.
2238 gsi_remove (gsi
, true);
2240 if (load_p
&& !store_p
)
2242 transaction_subcode_ior (region
, GTMA_HAVE_LOAD
);
2243 gcall
= build_tm_load (loc
, lhs
, rhs
, gsi
);
2245 else if (store_p
&& !load_p
)
2247 transaction_subcode_ior (region
, GTMA_HAVE_STORE
);
2248 gcall
= build_tm_store (loc
, lhs
, rhs
, gsi
);
2252 tree lhs_addr
, rhs_addr
, tmp
;
2255 transaction_subcode_ior (region
, GTMA_HAVE_LOAD
);
2257 transaction_subcode_ior (region
, GTMA_HAVE_STORE
);
2259 /* ??? Figure out if there's any possible overlap between the LHS
2260 and the RHS and if not, use MEMCPY. */
2262 if (load_p
&& is_gimple_reg (lhs
))
2264 tmp
= create_tmp_var (TREE_TYPE (lhs
), NULL
);
2265 lhs_addr
= build_fold_addr_expr (tmp
);
2270 lhs_addr
= gimplify_addr (gsi
, lhs
);
2272 rhs_addr
= gimplify_addr (gsi
, rhs
);
2273 gcall
= gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_MEMMOVE
),
2274 3, lhs_addr
, rhs_addr
,
2275 TYPE_SIZE_UNIT (TREE_TYPE (lhs
)));
2276 gimple_set_location (gcall
, loc
);
2277 gsi_insert_before (gsi
, gcall
, GSI_SAME_STMT
);
2281 gcall
= gimple_build_assign (lhs
, tmp
);
2282 gsi_insert_before (gsi
, gcall
, GSI_SAME_STMT
);
2286 /* Now that we have the load/store in its instrumented form, add
2287 thread private addresses to the log if applicable. */
2289 requires_barrier (region
->entry_block
, lhs
, gcall
);
2291 // The calls to build_tm_{store,load} above inserted the instrumented
2292 // call into the stream.
2293 // gsi_insert_before (gsi, gcall, GSI_SAME_STMT);
2297 /* Expand a call statement as appropriate for a transaction. That is,
2298 either verify that the call does not affect the transaction, or
2299 redirect the call to a clone that handles transactions, or change
2300 the transaction state to IRREVOCABLE. Return true if the call is
2301 one of the builtins that end a transaction. */
2304 expand_call_tm (struct tm_region
*region
,
2305 gimple_stmt_iterator
*gsi
)
2307 gimple stmt
= gsi_stmt (*gsi
);
2308 tree lhs
= gimple_call_lhs (stmt
);
2310 struct cgraph_node
*node
;
2311 bool retval
= false;
2313 fn_decl
= gimple_call_fndecl (stmt
);
2315 if (fn_decl
== builtin_decl_explicit (BUILT_IN_TM_MEMCPY
)
2316 || fn_decl
== builtin_decl_explicit (BUILT_IN_TM_MEMMOVE
))
2317 transaction_subcode_ior (region
, GTMA_HAVE_STORE
| GTMA_HAVE_LOAD
);
2318 if (fn_decl
== builtin_decl_explicit (BUILT_IN_TM_MEMSET
))
2319 transaction_subcode_ior (region
, GTMA_HAVE_STORE
);
2321 if (is_tm_pure_call (stmt
))
2325 retval
= is_tm_ending_fndecl (fn_decl
);
2328 /* Assume all non-const/pure calls write to memory, except
2329 transaction ending builtins. */
2330 transaction_subcode_ior (region
, GTMA_HAVE_STORE
);
2333 /* For indirect calls, we already generated a call into the runtime. */
2336 tree fn
= gimple_call_fn (stmt
);
2338 /* We are guaranteed never to go irrevocable on a safe or pure
2339 call, and the pure call was handled above. */
2340 if (is_tm_safe (fn
))
2343 transaction_subcode_ior (region
, GTMA_MAY_ENTER_IRREVOCABLE
);
2348 node
= cgraph_get_node (fn_decl
);
2349 /* All calls should have cgraph here. */
2352 /* We can have a nodeless call here if some pass after IPA-tm
2353 added uninstrumented calls. For example, loop distribution
2354 can transform certain loop constructs into __builtin_mem*
2355 calls. In this case, see if we have a suitable TM
2356 replacement and fill in the gaps. */
2357 gcc_assert (DECL_BUILT_IN_CLASS (fn_decl
) == BUILT_IN_NORMAL
);
2358 enum built_in_function code
= DECL_FUNCTION_CODE (fn_decl
);
2359 gcc_assert (code
== BUILT_IN_MEMCPY
2360 || code
== BUILT_IN_MEMMOVE
2361 || code
== BUILT_IN_MEMSET
);
2363 tree repl
= find_tm_replacement_function (fn_decl
);
2366 gimple_call_set_fndecl (stmt
, repl
);
2368 node
= cgraph_create_node (repl
);
2369 node
->local
.tm_may_enter_irr
= false;
2370 return expand_call_tm (region
, gsi
);
2374 if (node
->local
.tm_may_enter_irr
)
2375 transaction_subcode_ior (region
, GTMA_MAY_ENTER_IRREVOCABLE
);
2377 if (is_tm_abort (fn_decl
))
2379 transaction_subcode_ior (region
, GTMA_HAVE_ABORT
);
2383 /* Instrument the store if needed.
2385 If the assignment happens inside the function call (return slot
2386 optimization), there is no instrumentation to be done, since
2387 the callee should have done the right thing. */
2388 if (lhs
&& requires_barrier (region
->entry_block
, lhs
, stmt
)
2389 && !gimple_call_return_slot_opt_p (stmt
))
2391 tree tmp
= create_tmp_reg (TREE_TYPE (lhs
), NULL
);
2392 location_t loc
= gimple_location (stmt
);
2393 edge fallthru_edge
= NULL
;
2395 /* Remember if the call was going to throw. */
2396 if (stmt_can_throw_internal (stmt
))
2400 basic_block bb
= gimple_bb (stmt
);
2402 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2403 if (e
->flags
& EDGE_FALLTHRU
)
2410 gimple_call_set_lhs (stmt
, tmp
);
2412 stmt
= gimple_build_assign (lhs
, tmp
);
2413 gimple_set_location (stmt
, loc
);
2415 /* We cannot throw in the middle of a BB. If the call was going
2416 to throw, place the instrumentation on the fallthru edge, so
2417 the call remains the last statement in the block. */
2420 gimple_seq fallthru_seq
= gimple_seq_alloc_with_stmt (stmt
);
2421 gimple_stmt_iterator fallthru_gsi
= gsi_start (fallthru_seq
);
2422 expand_assign_tm (region
, &fallthru_gsi
);
2423 gsi_insert_seq_on_edge (fallthru_edge
, fallthru_seq
);
2424 pending_edge_inserts_p
= true;
2428 gsi_insert_after (gsi
, stmt
, GSI_CONTINUE_LINKING
);
2429 expand_assign_tm (region
, gsi
);
2432 transaction_subcode_ior (region
, GTMA_HAVE_STORE
);
2439 /* Expand all statements in BB as appropriate for being inside
2443 expand_block_tm (struct tm_region
*region
, basic_block bb
)
2445 gimple_stmt_iterator gsi
;
2447 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); )
2449 gimple stmt
= gsi_stmt (gsi
);
2450 switch (gimple_code (stmt
))
2453 /* Only memory reads/writes need to be instrumented. */
2454 if (gimple_assign_single_p (stmt
)
2455 && !gimple_clobber_p (stmt
))
2457 expand_assign_tm (region
, &gsi
);
2463 if (expand_call_tm (region
, &gsi
))
2473 if (!gsi_end_p (gsi
))
2478 /* Return the list of basic-blocks in REGION.
2480 STOP_AT_IRREVOCABLE_P is true if caller is uninterested in blocks
2481 following a TM_IRREVOCABLE call.
2483 INCLUDE_UNINSTRUMENTED_P is TRUE if we should include the
2484 uninstrumented code path blocks in the list of basic blocks
2485 returned, false otherwise. */
2487 static vec
<basic_block
>
2488 get_tm_region_blocks (basic_block entry_block
,
2491 bitmap all_region_blocks
,
2492 bool stop_at_irrevocable_p
,
2493 bool include_uninstrumented_p
= true)
2495 vec
<basic_block
> bbs
= vNULL
;
2499 bitmap visited_blocks
= BITMAP_ALLOC (NULL
);
2502 bbs
.safe_push (entry_block
);
2503 bitmap_set_bit (visited_blocks
, entry_block
->index
);
2507 basic_block bb
= bbs
[i
++];
2510 bitmap_bit_p (exit_blocks
, bb
->index
))
2513 if (stop_at_irrevocable_p
2515 && bitmap_bit_p (irr_blocks
, bb
->index
))
2518 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2519 if ((include_uninstrumented_p
2520 || !(e
->flags
& EDGE_TM_UNINSTRUMENTED
))
2521 && !bitmap_bit_p (visited_blocks
, e
->dest
->index
))
2523 bitmap_set_bit (visited_blocks
, e
->dest
->index
);
2524 bbs
.safe_push (e
->dest
);
2527 while (i
< bbs
.length ());
2529 if (all_region_blocks
)
2530 bitmap_ior_into (all_region_blocks
, visited_blocks
);
2532 BITMAP_FREE (visited_blocks
);
2536 // Callback data for collect_bb2reg.
2539 vec
<tm_region_p
> *bb2reg
;
2540 bool include_uninstrumented_p
;
2543 // Callback for expand_regions, collect innermost region data for each bb.
2545 collect_bb2reg (struct tm_region
*region
, void *data
)
2547 struct bb2reg_stuff
*stuff
= (struct bb2reg_stuff
*)data
;
2548 vec
<tm_region_p
> *bb2reg
= stuff
->bb2reg
;
2549 vec
<basic_block
> queue
;
2553 queue
= get_tm_region_blocks (region
->entry_block
,
2554 region
->exit_blocks
,
2557 /*stop_at_irr_p=*/true,
2558 stuff
->include_uninstrumented_p
);
2560 // We expect expand_region to perform a post-order traversal of the region
2561 // tree. Therefore the last region seen for any bb is the innermost.
2562 FOR_EACH_VEC_ELT (queue
, i
, bb
)
2563 (*bb2reg
)[bb
->index
] = region
;
2569 // Returns a vector, indexed by BB->INDEX, of the innermost tm_region to
2570 // which a basic block belongs. Note that we only consider the instrumented
2571 // code paths for the region; the uninstrumented code paths are ignored if
2572 // INCLUDE_UNINSTRUMENTED_P is false.
2574 // ??? This data is very similar to the bb_regions array that is collected
2575 // during tm_region_init. Or, rather, this data is similar to what could
2576 // be used within tm_region_init. The actual computation in tm_region_init
2577 // begins and ends with bb_regions entirely full of NULL pointers, due to
2578 // the way in which pointers are swapped in and out of the array.
2580 // ??? Our callers expect that blocks are not shared between transactions.
2581 // When the optimizers get too smart, and blocks are shared, then during
2582 // the tm_mark phase we'll add log entries to only one of the two transactions,
2583 // and in the tm_edge phase we'll add edges to the CFG that create invalid
2584 // cycles. The symptom being SSA defs that do not dominate their uses.
2585 // Note that the optimizers were locally correct with their transformation,
2586 // as we have no info within the program that suggests that the blocks cannot
2589 // ??? There is currently a hack inside tree-ssa-pre.c to work around the
2590 // only known instance of this block sharing.
2592 static vec
<tm_region_p
>
2593 get_bb_regions_instrumented (bool traverse_clones
,
2594 bool include_uninstrumented_p
)
2596 unsigned n
= last_basic_block
;
2597 struct bb2reg_stuff stuff
;
2598 vec
<tm_region_p
> ret
;
2601 ret
.safe_grow_cleared (n
);
2602 stuff
.bb2reg
= &ret
;
2603 stuff
.include_uninstrumented_p
= include_uninstrumented_p
;
2604 expand_regions (all_tm_regions
, collect_bb2reg
, &stuff
, traverse_clones
);
2609 /* Set the IN_TRANSACTION for all gimple statements that appear in a
2613 compute_transaction_bits (void)
2615 struct tm_region
*region
;
2616 vec
<basic_block
> queue
;
2620 /* ?? Perhaps we need to abstract gate_tm_init further, because we
2621 certainly don't need it to calculate CDI_DOMINATOR info. */
2625 bb
->flags
&= ~BB_IN_TRANSACTION
;
2627 for (region
= all_tm_regions
; region
; region
= region
->next
)
2629 queue
= get_tm_region_blocks (region
->entry_block
,
2630 region
->exit_blocks
,
2633 /*stop_at_irr_p=*/true);
2634 for (i
= 0; queue
.iterate (i
, &bb
); ++i
)
2635 bb
->flags
|= BB_IN_TRANSACTION
;
2640 bitmap_obstack_release (&tm_obstack
);
2643 /* Replace the GIMPLE_TRANSACTION in this region with the corresponding
2644 call to BUILT_IN_TM_START. */
2647 expand_transaction (struct tm_region
*region
, void *data ATTRIBUTE_UNUSED
)
2649 tree tm_start
= builtin_decl_explicit (BUILT_IN_TM_START
);
2650 basic_block transaction_bb
= gimple_bb (region
->transaction_stmt
);
2651 tree tm_state
= region
->tm_state
;
2652 tree tm_state_type
= TREE_TYPE (tm_state
);
2653 edge abort_edge
= NULL
;
2654 edge inst_edge
= NULL
;
2655 edge uninst_edge
= NULL
;
2656 edge fallthru_edge
= NULL
;
2658 // Identify the various successors of the transaction start.
2662 FOR_EACH_EDGE (e
, i
, transaction_bb
->succs
)
2664 if (e
->flags
& EDGE_TM_ABORT
)
2666 else if (e
->flags
& EDGE_TM_UNINSTRUMENTED
)
2670 if (e
->flags
& EDGE_FALLTHRU
)
2675 /* ??? There are plenty of bits here we're not computing. */
2677 int subcode
= gimple_transaction_subcode (region
->transaction_stmt
);
2679 if (subcode
& GTMA_DOES_GO_IRREVOCABLE
)
2680 flags
|= PR_DOESGOIRREVOCABLE
;
2681 if ((subcode
& GTMA_MAY_ENTER_IRREVOCABLE
) == 0)
2682 flags
|= PR_HASNOIRREVOCABLE
;
2683 /* If the transaction does not have an abort in lexical scope and is not
2684 marked as an outer transaction, then it will never abort. */
2685 if ((subcode
& GTMA_HAVE_ABORT
) == 0 && (subcode
& GTMA_IS_OUTER
) == 0)
2686 flags
|= PR_HASNOABORT
;
2687 if ((subcode
& GTMA_HAVE_STORE
) == 0)
2688 flags
|= PR_READONLY
;
2689 if (inst_edge
&& !(subcode
& GTMA_HAS_NO_INSTRUMENTATION
))
2690 flags
|= PR_INSTRUMENTEDCODE
;
2692 flags
|= PR_UNINSTRUMENTEDCODE
;
2693 if (subcode
& GTMA_IS_OUTER
)
2694 region
->original_transaction_was_outer
= true;
2695 tree t
= build_int_cst (tm_state_type
, flags
);
2696 gimple call
= gimple_build_call (tm_start
, 1, t
);
2697 gimple_call_set_lhs (call
, tm_state
);
2698 gimple_set_location (call
, gimple_location (region
->transaction_stmt
));
2700 // Replace the GIMPLE_TRANSACTION with the call to BUILT_IN_TM_START.
2701 gimple_stmt_iterator gsi
= gsi_last_bb (transaction_bb
);
2702 gcc_assert (gsi_stmt (gsi
) == region
->transaction_stmt
);
2703 gsi_insert_before (&gsi
, call
, GSI_SAME_STMT
);
2704 gsi_remove (&gsi
, true);
2705 region
->transaction_stmt
= call
;
2708 // Generate log saves.
2709 if (!tm_log_save_addresses
.is_empty ())
2710 tm_log_emit_saves (region
->entry_block
, transaction_bb
);
2712 // In the beginning, we've no tests to perform on transaction restart.
2713 // Note that after this point, transaction_bb becomes the "most recent
2714 // block containing tests for the transaction".
2715 region
->restart_block
= region
->entry_block
;
2717 // Generate log restores.
2718 if (!tm_log_save_addresses
.is_empty ())
2720 basic_block test_bb
= create_empty_bb (transaction_bb
);
2721 basic_block code_bb
= create_empty_bb (test_bb
);
2722 basic_block join_bb
= create_empty_bb (code_bb
);
2723 if (current_loops
&& transaction_bb
->loop_father
)
2725 add_bb_to_loop (test_bb
, transaction_bb
->loop_father
);
2726 add_bb_to_loop (code_bb
, transaction_bb
->loop_father
);
2727 add_bb_to_loop (join_bb
, transaction_bb
->loop_father
);
2729 if (region
->restart_block
== region
->entry_block
)
2730 region
->restart_block
= test_bb
;
2732 tree t1
= create_tmp_reg (tm_state_type
, NULL
);
2733 tree t2
= build_int_cst (tm_state_type
, A_RESTORELIVEVARIABLES
);
2734 gimple stmt
= gimple_build_assign_with_ops (BIT_AND_EXPR
, t1
,
2736 gimple_stmt_iterator gsi
= gsi_last_bb (test_bb
);
2737 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2739 t2
= build_int_cst (tm_state_type
, 0);
2740 stmt
= gimple_build_cond (NE_EXPR
, t1
, t2
, NULL
, NULL
);
2741 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2743 tm_log_emit_restores (region
->entry_block
, code_bb
);
2745 edge ei
= make_edge (transaction_bb
, test_bb
, EDGE_FALLTHRU
);
2746 edge et
= make_edge (test_bb
, code_bb
, EDGE_TRUE_VALUE
);
2747 edge ef
= make_edge (test_bb
, join_bb
, EDGE_FALSE_VALUE
);
2748 redirect_edge_pred (fallthru_edge
, join_bb
);
2750 join_bb
->frequency
= test_bb
->frequency
= transaction_bb
->frequency
;
2751 join_bb
->count
= test_bb
->count
= transaction_bb
->count
;
2753 ei
->probability
= PROB_ALWAYS
;
2754 et
->probability
= PROB_LIKELY
;
2755 ef
->probability
= PROB_UNLIKELY
;
2756 et
->count
= apply_probability(test_bb
->count
, et
->probability
);
2757 ef
->count
= apply_probability(test_bb
->count
, ef
->probability
);
2759 code_bb
->count
= et
->count
;
2760 code_bb
->frequency
= EDGE_FREQUENCY (et
);
2762 transaction_bb
= join_bb
;
2765 // If we have an ABORT edge, create a test to perform the abort.
2768 basic_block test_bb
= create_empty_bb (transaction_bb
);
2769 if (current_loops
&& transaction_bb
->loop_father
)
2770 add_bb_to_loop (test_bb
, transaction_bb
->loop_father
);
2771 if (region
->restart_block
== region
->entry_block
)
2772 region
->restart_block
= test_bb
;
2774 tree t1
= create_tmp_reg (tm_state_type
, NULL
);
2775 tree t2
= build_int_cst (tm_state_type
, A_ABORTTRANSACTION
);
2776 gimple stmt
= gimple_build_assign_with_ops (BIT_AND_EXPR
, t1
,
2778 gimple_stmt_iterator gsi
= gsi_last_bb (test_bb
);
2779 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2781 t2
= build_int_cst (tm_state_type
, 0);
2782 stmt
= gimple_build_cond (NE_EXPR
, t1
, t2
, NULL
, NULL
);
2783 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2785 edge ei
= make_edge (transaction_bb
, test_bb
, EDGE_FALLTHRU
);
2786 test_bb
->frequency
= transaction_bb
->frequency
;
2787 test_bb
->count
= transaction_bb
->count
;
2788 ei
->probability
= PROB_ALWAYS
;
2790 // Not abort edge. If both are live, chose one at random as we'll
2791 // we'll be fixing that up below.
2792 redirect_edge_pred (fallthru_edge
, test_bb
);
2793 fallthru_edge
->flags
= EDGE_FALSE_VALUE
;
2794 fallthru_edge
->probability
= PROB_VERY_LIKELY
;
2795 fallthru_edge
->count
2796 = apply_probability(test_bb
->count
, fallthru_edge
->probability
);
2799 redirect_edge_pred (abort_edge
, test_bb
);
2800 abort_edge
->flags
= EDGE_TRUE_VALUE
;
2801 abort_edge
->probability
= PROB_VERY_UNLIKELY
;
2803 = apply_probability(test_bb
->count
, abort_edge
->probability
);
2805 transaction_bb
= test_bb
;
2808 // If we have both instrumented and uninstrumented code paths, select one.
2809 if (inst_edge
&& uninst_edge
)
2811 basic_block test_bb
= create_empty_bb (transaction_bb
);
2812 if (current_loops
&& transaction_bb
->loop_father
)
2813 add_bb_to_loop (test_bb
, transaction_bb
->loop_father
);
2814 if (region
->restart_block
== region
->entry_block
)
2815 region
->restart_block
= test_bb
;
2817 tree t1
= create_tmp_reg (tm_state_type
, NULL
);
2818 tree t2
= build_int_cst (tm_state_type
, A_RUNUNINSTRUMENTEDCODE
);
2820 gimple stmt
= gimple_build_assign_with_ops (BIT_AND_EXPR
, t1
,
2822 gimple_stmt_iterator gsi
= gsi_last_bb (test_bb
);
2823 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2825 t2
= build_int_cst (tm_state_type
, 0);
2826 stmt
= gimple_build_cond (NE_EXPR
, t1
, t2
, NULL
, NULL
);
2827 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2829 // Create the edge into test_bb first, as we want to copy values
2830 // out of the fallthru edge.
2831 edge e
= make_edge (transaction_bb
, test_bb
, fallthru_edge
->flags
);
2832 e
->probability
= fallthru_edge
->probability
;
2833 test_bb
->count
= e
->count
= fallthru_edge
->count
;
2834 test_bb
->frequency
= EDGE_FREQUENCY (e
);
2836 // Now update the edges to the inst/uninist implementations.
2837 // For now assume that the paths are equally likely. When using HTM,
2838 // we'll try the uninst path first and fallback to inst path if htm
2839 // buffers are exceeded. Without HTM we start with the inst path and
2840 // use the uninst path when falling back to serial mode.
2841 redirect_edge_pred (inst_edge
, test_bb
);
2842 inst_edge
->flags
= EDGE_FALSE_VALUE
;
2843 inst_edge
->probability
= REG_BR_PROB_BASE
/ 2;
2845 = apply_probability(test_bb
->count
, inst_edge
->probability
);
2847 redirect_edge_pred (uninst_edge
, test_bb
);
2848 uninst_edge
->flags
= EDGE_TRUE_VALUE
;
2849 uninst_edge
->probability
= REG_BR_PROB_BASE
/ 2;
2851 = apply_probability(test_bb
->count
, uninst_edge
->probability
);
2854 // If we have no previous special cases, and we have PHIs at the beginning
2855 // of the atomic region, this means we have a loop at the beginning of the
2856 // atomic region that shares the first block. This can cause problems with
2857 // the transaction restart abnormal edges to be added in the tm_edges pass.
2858 // Solve this by adding a new empty block to receive the abnormal edges.
2859 if (region
->restart_block
== region
->entry_block
2860 && phi_nodes (region
->entry_block
))
2862 basic_block empty_bb
= create_empty_bb (transaction_bb
);
2863 region
->restart_block
= empty_bb
;
2864 if (current_loops
&& transaction_bb
->loop_father
)
2865 add_bb_to_loop (empty_bb
, transaction_bb
->loop_father
);
2867 redirect_edge_pred (fallthru_edge
, empty_bb
);
2868 make_edge (transaction_bb
, empty_bb
, EDGE_FALLTHRU
);
2874 /* Generate the temporary to be used for the return value of
2875 BUILT_IN_TM_START. */
2878 generate_tm_state (struct tm_region
*region
, void *data ATTRIBUTE_UNUSED
)
2880 tree tm_start
= builtin_decl_explicit (BUILT_IN_TM_START
);
2882 create_tmp_reg (TREE_TYPE (TREE_TYPE (tm_start
)), "tm_state");
2884 // Reset the subcode, post optimizations. We'll fill this in
2885 // again as we process blocks.
2886 if (region
->exit_blocks
)
2888 unsigned int subcode
2889 = gimple_transaction_subcode (region
->transaction_stmt
);
2891 if (subcode
& GTMA_DOES_GO_IRREVOCABLE
)
2892 subcode
&= (GTMA_DECLARATION_MASK
| GTMA_DOES_GO_IRREVOCABLE
2893 | GTMA_MAY_ENTER_IRREVOCABLE
2894 | GTMA_HAS_NO_INSTRUMENTATION
);
2896 subcode
&= GTMA_DECLARATION_MASK
;
2897 gimple_transaction_set_subcode (region
->transaction_stmt
, subcode
);
2903 // Propagate flags from inner transactions outwards.
2905 propagate_tm_flags_out (struct tm_region
*region
)
2909 propagate_tm_flags_out (region
->inner
);
2911 if (region
->outer
&& region
->outer
->transaction_stmt
)
2913 unsigned s
= gimple_transaction_subcode (region
->transaction_stmt
);
2914 s
&= (GTMA_HAVE_ABORT
| GTMA_HAVE_LOAD
| GTMA_HAVE_STORE
2915 | GTMA_MAY_ENTER_IRREVOCABLE
);
2916 s
|= gimple_transaction_subcode (region
->outer
->transaction_stmt
);
2917 gimple_transaction_set_subcode (region
->outer
->transaction_stmt
, s
);
2920 propagate_tm_flags_out (region
->next
);
2923 /* Entry point to the MARK phase of TM expansion. Here we replace
2924 transactional memory statements with calls to builtins, and function
2925 calls with their transactional clones (if available). But we don't
2926 yet lower GIMPLE_TRANSACTION or add the transaction restart back-edges. */
2929 execute_tm_mark (void)
2931 pending_edge_inserts_p
= false;
2933 expand_regions (all_tm_regions
, generate_tm_state
, NULL
,
2934 /*traverse_clones=*/true);
2938 vec
<tm_region_p
> bb_regions
2939 = get_bb_regions_instrumented (/*traverse_clones=*/true,
2940 /*include_uninstrumented_p=*/false);
2941 struct tm_region
*r
;
2944 // Expand memory operations into calls into the runtime.
2945 // This collects log entries as well.
2946 FOR_EACH_VEC_ELT (bb_regions
, i
, r
)
2950 if (r
->transaction_stmt
)
2952 unsigned sub
= gimple_transaction_subcode (r
->transaction_stmt
);
2954 /* If we're sure to go irrevocable, there won't be
2955 anything to expand, since the run-time will go
2956 irrevocable right away. */
2957 if (sub
& GTMA_DOES_GO_IRREVOCABLE
2958 && sub
& GTMA_MAY_ENTER_IRREVOCABLE
)
2961 expand_block_tm (r
, BASIC_BLOCK (i
));
2965 bb_regions
.release ();
2967 // Propagate flags from inner transactions outwards.
2968 propagate_tm_flags_out (all_tm_regions
);
2970 // Expand GIMPLE_TRANSACTIONs into calls into the runtime.
2971 expand_regions (all_tm_regions
, expand_transaction
, NULL
,
2972 /*traverse_clones=*/false);
2977 if (pending_edge_inserts_p
)
2978 gsi_commit_edge_inserts ();
2979 free_dominance_info (CDI_DOMINATORS
);
2985 const pass_data pass_data_tm_mark
=
2987 GIMPLE_PASS
, /* type */
2988 "tmmark", /* name */
2989 OPTGROUP_NONE
, /* optinfo_flags */
2990 false, /* has_gate */
2991 true, /* has_execute */
2992 TV_TRANS_MEM
, /* tv_id */
2993 ( PROP_ssa
| PROP_cfg
), /* properties_required */
2994 0, /* properties_provided */
2995 0, /* properties_destroyed */
2996 0, /* todo_flags_start */
2997 ( TODO_update_ssa
| TODO_verify_ssa
), /* todo_flags_finish */
3000 class pass_tm_mark
: public gimple_opt_pass
3003 pass_tm_mark(gcc::context
*ctxt
)
3004 : gimple_opt_pass(pass_data_tm_mark
, ctxt
)
3007 /* opt_pass methods: */
3008 unsigned int execute () { return execute_tm_mark (); }
3010 }; // class pass_tm_mark
3015 make_pass_tm_mark (gcc::context
*ctxt
)
3017 return new pass_tm_mark (ctxt
);
3021 /* Create an abnormal edge from STMT at iter, splitting the block
3022 as necessary. Adjust *PNEXT as needed for the split block. */
3025 split_bb_make_tm_edge (gimple stmt
, basic_block dest_bb
,
3026 gimple_stmt_iterator iter
, gimple_stmt_iterator
*pnext
)
3028 basic_block bb
= gimple_bb (stmt
);
3029 if (!gsi_one_before_end_p (iter
))
3031 edge e
= split_block (bb
, stmt
);
3032 *pnext
= gsi_start_bb (e
->dest
);
3034 make_edge (bb
, dest_bb
, EDGE_ABNORMAL
);
3036 // Record the need for the edge for the benefit of the rtl passes.
3037 if (cfun
->gimple_df
->tm_restart
== NULL
)
3038 cfun
->gimple_df
->tm_restart
= htab_create_ggc (31, struct_ptr_hash
,
3039 struct_ptr_eq
, ggc_free
);
3041 struct tm_restart_node dummy
;
3043 dummy
.label_or_list
= gimple_block_label (dest_bb
);
3045 void **slot
= htab_find_slot (cfun
->gimple_df
->tm_restart
, &dummy
, INSERT
);
3046 struct tm_restart_node
*n
= (struct tm_restart_node
*) *slot
;
3049 n
= ggc_alloc_tm_restart_node ();
3054 tree old
= n
->label_or_list
;
3055 if (TREE_CODE (old
) == LABEL_DECL
)
3056 old
= tree_cons (NULL
, old
, NULL
);
3057 n
->label_or_list
= tree_cons (NULL
, dummy
.label_or_list
, old
);
3061 /* Split block BB as necessary for every builtin function we added, and
3062 wire up the abnormal back edges implied by the transaction restart. */
3065 expand_block_edges (struct tm_region
*const region
, basic_block bb
)
3067 gimple_stmt_iterator gsi
, next_gsi
;
3069 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi
= next_gsi
)
3071 gimple stmt
= gsi_stmt (gsi
);
3074 gsi_next (&next_gsi
);
3076 // ??? Shouldn't we split for any non-pure, non-irrevocable function?
3077 if (gimple_code (stmt
) != GIMPLE_CALL
3078 || (gimple_call_flags (stmt
) & ECF_TM_BUILTIN
) == 0)
3081 if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt
)) == BUILT_IN_TM_ABORT
)
3083 // If we have a ``_transaction_cancel [[outer]]'', there is only
3084 // one abnormal edge: to the transaction marked OUTER.
3085 // All compiler-generated instances of BUILT_IN_TM_ABORT have a
3086 // constant argument, which we can examine here. Users invoking
3087 // TM_ABORT directly get what they deserve.
3088 tree arg
= gimple_call_arg (stmt
, 0);
3089 if (TREE_CODE (arg
) == INTEGER_CST
3090 && (TREE_INT_CST_LOW (arg
) & AR_OUTERABORT
) != 0
3091 && !decl_is_tm_clone (current_function_decl
))
3093 // Find the GTMA_IS_OUTER transaction.
3094 for (struct tm_region
*o
= region
; o
; o
= o
->outer
)
3095 if (o
->original_transaction_was_outer
)
3097 split_bb_make_tm_edge (stmt
, o
->restart_block
,
3102 // Otherwise, the front-end should have semantically checked
3103 // outer aborts, but in either case the target region is not
3104 // within this function.
3108 // Non-outer, TM aborts have an abnormal edge to the inner-most
3109 // transaction, the one being aborted;
3110 split_bb_make_tm_edge (stmt
, region
->restart_block
, gsi
, &next_gsi
);
3113 // All TM builtins have an abnormal edge to the outer-most transaction.
3114 // We never restart inner transactions. For tm clones, we know a-priori
3115 // that the outer-most transaction is outside the function.
3116 if (decl_is_tm_clone (current_function_decl
))
3119 if (cfun
->gimple_df
->tm_restart
== NULL
)
3120 cfun
->gimple_df
->tm_restart
3121 = htab_create_ggc (31, struct_ptr_hash
, struct_ptr_eq
, ggc_free
);
3123 // All TM builtins have an abnormal edge to the outer-most transaction.
3124 // We never restart inner transactions.
3125 for (struct tm_region
*o
= region
; o
; o
= o
->outer
)
3128 split_bb_make_tm_edge (stmt
, o
->restart_block
, gsi
, &next_gsi
);
3132 // Delete any tail-call annotation that may have been added.
3133 // The tail-call pass may have mis-identified the commit as being
3134 // a candidate because we had not yet added this restart edge.
3135 gimple_call_set_tail (stmt
, false);
3139 /* Entry point to the final expansion of transactional nodes. */
3142 execute_tm_edges (void)
3144 vec
<tm_region_p
> bb_regions
3145 = get_bb_regions_instrumented (/*traverse_clones=*/false,
3146 /*include_uninstrumented_p=*/true);
3147 struct tm_region
*r
;
3150 FOR_EACH_VEC_ELT (bb_regions
, i
, r
)
3152 expand_block_edges (r
, BASIC_BLOCK (i
));
3154 bb_regions
.release ();
3156 /* We've got to release the dominance info now, to indicate that it
3157 must be rebuilt completely. Otherwise we'll crash trying to update
3158 the SSA web in the TODO section following this pass. */
3159 free_dominance_info (CDI_DOMINATORS
);
3160 bitmap_obstack_release (&tm_obstack
);
3161 all_tm_regions
= NULL
;
3168 const pass_data pass_data_tm_edges
=
3170 GIMPLE_PASS
, /* type */
3171 "tmedge", /* name */
3172 OPTGROUP_NONE
, /* optinfo_flags */
3173 false, /* has_gate */
3174 true, /* has_execute */
3175 TV_TRANS_MEM
, /* tv_id */
3176 ( PROP_ssa
| PROP_cfg
), /* properties_required */
3177 0, /* properties_provided */
3178 0, /* properties_destroyed */
3179 0, /* todo_flags_start */
3180 ( TODO_update_ssa
| TODO_verify_ssa
), /* todo_flags_finish */
3183 class pass_tm_edges
: public gimple_opt_pass
3186 pass_tm_edges(gcc::context
*ctxt
)
3187 : gimple_opt_pass(pass_data_tm_edges
, ctxt
)
3190 /* opt_pass methods: */
3191 unsigned int execute () { return execute_tm_edges (); }
3193 }; // class pass_tm_edges
3198 make_pass_tm_edges (gcc::context
*ctxt
)
3200 return new pass_tm_edges (ctxt
);
3203 /* Helper function for expand_regions. Expand REGION and recurse to
3204 the inner region. Call CALLBACK on each region. CALLBACK returns
3205 NULL to continue the traversal, otherwise a non-null value which
3206 this function will return as well. TRAVERSE_CLONES is true if we
3207 should traverse transactional clones. */
3210 expand_regions_1 (struct tm_region
*region
,
3211 void *(*callback
)(struct tm_region
*, void *),
3213 bool traverse_clones
)
3215 void *retval
= NULL
;
3216 if (region
->exit_blocks
3217 || (traverse_clones
&& decl_is_tm_clone (current_function_decl
)))
3219 retval
= callback (region
, data
);
3225 retval
= expand_regions (region
->inner
, callback
, data
, traverse_clones
);
3232 /* Traverse the regions enclosed and including REGION. Execute
3233 CALLBACK for each region, passing DATA. CALLBACK returns NULL to
3234 continue the traversal, otherwise a non-null value which this
3235 function will return as well. TRAVERSE_CLONES is true if we should
3236 traverse transactional clones. */
3239 expand_regions (struct tm_region
*region
,
3240 void *(*callback
)(struct tm_region
*, void *),
3242 bool traverse_clones
)
3244 void *retval
= NULL
;
3247 retval
= expand_regions_1 (region
, callback
, data
, traverse_clones
);
3250 region
= region
->next
;
3256 /* A unique TM memory operation. */
3257 typedef struct tm_memop
3259 /* Unique ID that all memory operations to the same location have. */
3260 unsigned int value_id
;
3261 /* Address of load/store. */
3265 /* TM memory operation hashtable helpers. */
3267 struct tm_memop_hasher
: typed_free_remove
<tm_memop
>
3269 typedef tm_memop value_type
;
3270 typedef tm_memop compare_type
;
3271 static inline hashval_t
hash (const value_type
*);
3272 static inline bool equal (const value_type
*, const compare_type
*);
3275 /* Htab support. Return a hash value for a `tm_memop'. */
3277 tm_memop_hasher::hash (const value_type
*mem
)
3279 tree addr
= mem
->addr
;
3280 /* We drill down to the SSA_NAME/DECL for the hash, but equality is
3281 actually done with operand_equal_p (see tm_memop_eq). */
3282 if (TREE_CODE (addr
) == ADDR_EXPR
)
3283 addr
= TREE_OPERAND (addr
, 0);
3284 return iterative_hash_expr (addr
, 0);
3287 /* Htab support. Return true if two tm_memop's are the same. */
3289 tm_memop_hasher::equal (const value_type
*mem1
, const compare_type
*mem2
)
3291 return operand_equal_p (mem1
->addr
, mem2
->addr
, 0);
3294 /* Sets for solving data flow equations in the memory optimization pass. */
3295 struct tm_memopt_bitmaps
3297 /* Stores available to this BB upon entry. Basically, stores that
3298 dominate this BB. */
3299 bitmap store_avail_in
;
3300 /* Stores available at the end of this BB. */
3301 bitmap store_avail_out
;
3302 bitmap store_antic_in
;
3303 bitmap store_antic_out
;
3304 /* Reads available to this BB upon entry. Basically, reads that
3305 dominate this BB. */
3306 bitmap read_avail_in
;
3307 /* Reads available at the end of this BB. */
3308 bitmap read_avail_out
;
3309 /* Reads performed in this BB. */
3311 /* Writes performed in this BB. */
3314 /* Temporary storage for pass. */
3315 /* Is the current BB in the worklist? */
3316 bool avail_in_worklist_p
;
3317 /* Have we visited this BB? */
3321 static bitmap_obstack tm_memopt_obstack
;
3323 /* Unique counter for TM loads and stores. Loads and stores of the
3324 same address get the same ID. */
3325 static unsigned int tm_memopt_value_id
;
3326 static hash_table
<tm_memop_hasher
> tm_memopt_value_numbers
;
3328 #define STORE_AVAIL_IN(BB) \
3329 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_avail_in
3330 #define STORE_AVAIL_OUT(BB) \
3331 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_avail_out
3332 #define STORE_ANTIC_IN(BB) \
3333 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_antic_in
3334 #define STORE_ANTIC_OUT(BB) \
3335 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_antic_out
3336 #define READ_AVAIL_IN(BB) \
3337 ((struct tm_memopt_bitmaps *) ((BB)->aux))->read_avail_in
3338 #define READ_AVAIL_OUT(BB) \
3339 ((struct tm_memopt_bitmaps *) ((BB)->aux))->read_avail_out
3340 #define READ_LOCAL(BB) \
3341 ((struct tm_memopt_bitmaps *) ((BB)->aux))->read_local
3342 #define STORE_LOCAL(BB) \
3343 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_local
3344 #define AVAIL_IN_WORKLIST_P(BB) \
3345 ((struct tm_memopt_bitmaps *) ((BB)->aux))->avail_in_worklist_p
3346 #define BB_VISITED_P(BB) \
3347 ((struct tm_memopt_bitmaps *) ((BB)->aux))->visited_p
3349 /* Given a TM load/store in STMT, return the value number for the address
3353 tm_memopt_value_number (gimple stmt
, enum insert_option op
)
3355 struct tm_memop tmpmem
, *mem
;
3358 gcc_assert (is_tm_load (stmt
) || is_tm_store (stmt
));
3359 tmpmem
.addr
= gimple_call_arg (stmt
, 0);
3360 slot
= tm_memopt_value_numbers
.find_slot (&tmpmem
, op
);
3363 else if (op
== INSERT
)
3365 mem
= XNEW (struct tm_memop
);
3367 mem
->value_id
= tm_memopt_value_id
++;
3368 mem
->addr
= tmpmem
.addr
;
3372 return mem
->value_id
;
3375 /* Accumulate TM memory operations in BB into STORE_LOCAL and READ_LOCAL. */
3378 tm_memopt_accumulate_memops (basic_block bb
)
3380 gimple_stmt_iterator gsi
;
3382 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3384 gimple stmt
= gsi_stmt (gsi
);
3388 if (is_tm_store (stmt
))
3389 bits
= STORE_LOCAL (bb
);
3390 else if (is_tm_load (stmt
))
3391 bits
= READ_LOCAL (bb
);
3395 loc
= tm_memopt_value_number (stmt
, INSERT
);
3396 bitmap_set_bit (bits
, loc
);
3399 fprintf (dump_file
, "TM memopt (%s): value num=%d, BB=%d, addr=",
3400 is_tm_load (stmt
) ? "LOAD" : "STORE", loc
,
3401 gimple_bb (stmt
)->index
);
3402 print_generic_expr (dump_file
, gimple_call_arg (stmt
, 0), 0);
3403 fprintf (dump_file
, "\n");
3408 /* Prettily dump one of the memopt sets. BITS is the bitmap to dump. */
3411 dump_tm_memopt_set (const char *set_name
, bitmap bits
)
3415 const char *comma
= "";
3417 fprintf (dump_file
, "TM memopt: %s: [", set_name
);
3418 EXECUTE_IF_SET_IN_BITMAP (bits
, 0, i
, bi
)
3420 hash_table
<tm_memop_hasher
>::iterator hi
;
3421 struct tm_memop
*mem
= NULL
;
3423 /* Yeah, yeah, yeah. Whatever. This is just for debugging. */
3424 FOR_EACH_HASH_TABLE_ELEMENT (tm_memopt_value_numbers
, mem
, tm_memop_t
, hi
)
3425 if (mem
->value_id
== i
)
3427 gcc_assert (mem
->value_id
== i
);
3428 fprintf (dump_file
, "%s", comma
);
3430 print_generic_expr (dump_file
, mem
->addr
, 0);
3432 fprintf (dump_file
, "]\n");
3435 /* Prettily dump all of the memopt sets in BLOCKS. */
3438 dump_tm_memopt_sets (vec
<basic_block
> blocks
)
3443 for (i
= 0; blocks
.iterate (i
, &bb
); ++i
)
3445 fprintf (dump_file
, "------------BB %d---------\n", bb
->index
);
3446 dump_tm_memopt_set ("STORE_LOCAL", STORE_LOCAL (bb
));
3447 dump_tm_memopt_set ("READ_LOCAL", READ_LOCAL (bb
));
3448 dump_tm_memopt_set ("STORE_AVAIL_IN", STORE_AVAIL_IN (bb
));
3449 dump_tm_memopt_set ("STORE_AVAIL_OUT", STORE_AVAIL_OUT (bb
));
3450 dump_tm_memopt_set ("READ_AVAIL_IN", READ_AVAIL_IN (bb
));
3451 dump_tm_memopt_set ("READ_AVAIL_OUT", READ_AVAIL_OUT (bb
));
3455 /* Compute {STORE,READ}_AVAIL_IN for the basic block BB. */
3458 tm_memopt_compute_avin (basic_block bb
)
3463 /* Seed with the AVOUT of any predecessor. */
3464 for (ix
= 0; ix
< EDGE_COUNT (bb
->preds
); ix
++)
3466 e
= EDGE_PRED (bb
, ix
);
3467 /* Make sure we have already visited this BB, and is thus
3470 If e->src->aux is NULL, this predecessor is actually on an
3471 enclosing transaction. We only care about the current
3472 transaction, so ignore it. */
3473 if (e
->src
->aux
&& BB_VISITED_P (e
->src
))
3475 bitmap_copy (STORE_AVAIL_IN (bb
), STORE_AVAIL_OUT (e
->src
));
3476 bitmap_copy (READ_AVAIL_IN (bb
), READ_AVAIL_OUT (e
->src
));
3481 for (; ix
< EDGE_COUNT (bb
->preds
); ix
++)
3483 e
= EDGE_PRED (bb
, ix
);
3484 if (e
->src
->aux
&& BB_VISITED_P (e
->src
))
3486 bitmap_and_into (STORE_AVAIL_IN (bb
), STORE_AVAIL_OUT (e
->src
));
3487 bitmap_and_into (READ_AVAIL_IN (bb
), READ_AVAIL_OUT (e
->src
));
3491 BB_VISITED_P (bb
) = true;
3494 /* Compute the STORE_ANTIC_IN for the basic block BB. */
3497 tm_memopt_compute_antin (basic_block bb
)
3502 /* Seed with the ANTIC_OUT of any successor. */
3503 for (ix
= 0; ix
< EDGE_COUNT (bb
->succs
); ix
++)
3505 e
= EDGE_SUCC (bb
, ix
);
3506 /* Make sure we have already visited this BB, and is thus
3508 if (BB_VISITED_P (e
->dest
))
3510 bitmap_copy (STORE_ANTIC_IN (bb
), STORE_ANTIC_OUT (e
->dest
));
3515 for (; ix
< EDGE_COUNT (bb
->succs
); ix
++)
3517 e
= EDGE_SUCC (bb
, ix
);
3518 if (BB_VISITED_P (e
->dest
))
3519 bitmap_and_into (STORE_ANTIC_IN (bb
), STORE_ANTIC_OUT (e
->dest
));
3522 BB_VISITED_P (bb
) = true;
3525 /* Compute the AVAIL sets for every basic block in BLOCKS.
3527 We compute {STORE,READ}_AVAIL_{OUT,IN} as follows:
3529 AVAIL_OUT[bb] = union (AVAIL_IN[bb], LOCAL[bb])
3530 AVAIL_IN[bb] = intersect (AVAIL_OUT[predecessors])
3532 This is basically what we do in lcm's compute_available(), but here
3533 we calculate two sets of sets (one for STOREs and one for READs),
3534 and we work on a region instead of the entire CFG.
3536 REGION is the TM region.
3537 BLOCKS are the basic blocks in the region. */
3540 tm_memopt_compute_available (struct tm_region
*region
,
3541 vec
<basic_block
> blocks
)
3544 basic_block
*worklist
, *qin
, *qout
, *qend
, bb
;
3545 unsigned int qlen
, i
;
3549 /* Allocate a worklist array/queue. Entries are only added to the
3550 list if they were not already on the list. So the size is
3551 bounded by the number of basic blocks in the region. */
3552 qlen
= blocks
.length () - 1;
3553 qin
= qout
= worklist
=
3554 XNEWVEC (basic_block
, qlen
);
3556 /* Put every block in the region on the worklist. */
3557 for (i
= 0; blocks
.iterate (i
, &bb
); ++i
)
3559 /* Seed AVAIL_OUT with the LOCAL set. */
3560 bitmap_ior_into (STORE_AVAIL_OUT (bb
), STORE_LOCAL (bb
));
3561 bitmap_ior_into (READ_AVAIL_OUT (bb
), READ_LOCAL (bb
));
3563 AVAIL_IN_WORKLIST_P (bb
) = true;
3564 /* No need to insert the entry block, since it has an AVIN of
3565 null, and an AVOUT that has already been seeded in. */
3566 if (bb
!= region
->entry_block
)
3570 /* The entry block has been initialized with the local sets. */
3571 BB_VISITED_P (region
->entry_block
) = true;
3574 qend
= &worklist
[qlen
];
3576 /* Iterate until the worklist is empty. */
3579 /* Take the first entry off the worklist. */
3586 /* This block can be added to the worklist again if necessary. */
3587 AVAIL_IN_WORKLIST_P (bb
) = false;
3588 tm_memopt_compute_avin (bb
);
3590 /* Note: We do not add the LOCAL sets here because we already
3591 seeded the AVAIL_OUT sets with them. */
3592 changed
= bitmap_ior_into (STORE_AVAIL_OUT (bb
), STORE_AVAIL_IN (bb
));
3593 changed
|= bitmap_ior_into (READ_AVAIL_OUT (bb
), READ_AVAIL_IN (bb
));
3595 && (region
->exit_blocks
== NULL
3596 || !bitmap_bit_p (region
->exit_blocks
, bb
->index
)))
3597 /* If the out state of this block changed, then we need to add
3598 its successors to the worklist if they are not already in. */
3599 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3600 if (!AVAIL_IN_WORKLIST_P (e
->dest
) && e
->dest
!= EXIT_BLOCK_PTR
)
3603 AVAIL_IN_WORKLIST_P (e
->dest
) = true;
3614 dump_tm_memopt_sets (blocks
);
3617 /* Compute ANTIC sets for every basic block in BLOCKS.
3619 We compute STORE_ANTIC_OUT as follows:
3621 STORE_ANTIC_OUT[bb] = union(STORE_ANTIC_IN[bb], STORE_LOCAL[bb])
3622 STORE_ANTIC_IN[bb] = intersect(STORE_ANTIC_OUT[successors])
3624 REGION is the TM region.
3625 BLOCKS are the basic blocks in the region. */
3628 tm_memopt_compute_antic (struct tm_region
*region
,
3629 vec
<basic_block
> blocks
)
3632 basic_block
*worklist
, *qin
, *qout
, *qend
, bb
;
3637 /* Allocate a worklist array/queue. Entries are only added to the
3638 list if they were not already on the list. So the size is
3639 bounded by the number of basic blocks in the region. */
3640 qin
= qout
= worklist
= XNEWVEC (basic_block
, blocks
.length ());
3642 for (qlen
= 0, i
= blocks
.length () - 1; i
>= 0; --i
)
3646 /* Seed ANTIC_OUT with the LOCAL set. */
3647 bitmap_ior_into (STORE_ANTIC_OUT (bb
), STORE_LOCAL (bb
));
3649 /* Put every block in the region on the worklist. */
3650 AVAIL_IN_WORKLIST_P (bb
) = true;
3651 /* No need to insert exit blocks, since their ANTIC_IN is NULL,
3652 and their ANTIC_OUT has already been seeded in. */
3653 if (region
->exit_blocks
3654 && !bitmap_bit_p (region
->exit_blocks
, bb
->index
))
3661 /* The exit blocks have been initialized with the local sets. */
3662 if (region
->exit_blocks
)
3666 EXECUTE_IF_SET_IN_BITMAP (region
->exit_blocks
, 0, i
, bi
)
3667 BB_VISITED_P (BASIC_BLOCK (i
)) = true;
3671 qend
= &worklist
[qlen
];
3673 /* Iterate until the worklist is empty. */
3676 /* Take the first entry off the worklist. */
3683 /* This block can be added to the worklist again if necessary. */
3684 AVAIL_IN_WORKLIST_P (bb
) = false;
3685 tm_memopt_compute_antin (bb
);
3687 /* Note: We do not add the LOCAL sets here because we already
3688 seeded the ANTIC_OUT sets with them. */
3689 if (bitmap_ior_into (STORE_ANTIC_OUT (bb
), STORE_ANTIC_IN (bb
))
3690 && bb
!= region
->entry_block
)
3691 /* If the out state of this block changed, then we need to add
3692 its predecessors to the worklist if they are not already in. */
3693 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3694 if (!AVAIL_IN_WORKLIST_P (e
->src
))
3697 AVAIL_IN_WORKLIST_P (e
->src
) = true;
3708 dump_tm_memopt_sets (blocks
);
3711 /* Offsets of load variants from TM_LOAD. For example,
3712 BUILT_IN_TM_LOAD_RAR* is an offset of 1 from BUILT_IN_TM_LOAD*.
3713 See gtm-builtins.def. */
3714 #define TRANSFORM_RAR 1
3715 #define TRANSFORM_RAW 2
3716 #define TRANSFORM_RFW 3
3717 /* Offsets of store variants from TM_STORE. */
3718 #define TRANSFORM_WAR 1
3719 #define TRANSFORM_WAW 2
3721 /* Inform about a load/store optimization. */
3724 dump_tm_memopt_transform (gimple stmt
)
3728 fprintf (dump_file
, "TM memopt: transforming: ");
3729 print_gimple_stmt (dump_file
, stmt
, 0, 0);
3730 fprintf (dump_file
, "\n");
3734 /* Perform a read/write optimization. Replaces the TM builtin in STMT
3735 by a builtin that is OFFSET entries down in the builtins table in
3736 gtm-builtins.def. */
3739 tm_memopt_transform_stmt (unsigned int offset
,
3741 gimple_stmt_iterator
*gsi
)
3743 tree fn
= gimple_call_fn (stmt
);
3744 gcc_assert (TREE_CODE (fn
) == ADDR_EXPR
);
3745 TREE_OPERAND (fn
, 0)
3746 = builtin_decl_explicit ((enum built_in_function
)
3747 (DECL_FUNCTION_CODE (TREE_OPERAND (fn
, 0))
3749 gimple_call_set_fn (stmt
, fn
);
3750 gsi_replace (gsi
, stmt
, true);
3751 dump_tm_memopt_transform (stmt
);
3754 /* Perform the actual TM memory optimization transformations in the
3755 basic blocks in BLOCKS. */
3758 tm_memopt_transform_blocks (vec
<basic_block
> blocks
)
3762 gimple_stmt_iterator gsi
;
3764 for (i
= 0; blocks
.iterate (i
, &bb
); ++i
)
3766 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3768 gimple stmt
= gsi_stmt (gsi
);
3769 bitmap read_avail
= READ_AVAIL_IN (bb
);
3770 bitmap store_avail
= STORE_AVAIL_IN (bb
);
3771 bitmap store_antic
= STORE_ANTIC_OUT (bb
);
3774 if (is_tm_simple_load (stmt
))
3776 loc
= tm_memopt_value_number (stmt
, NO_INSERT
);
3777 if (store_avail
&& bitmap_bit_p (store_avail
, loc
))
3778 tm_memopt_transform_stmt (TRANSFORM_RAW
, stmt
, &gsi
);
3779 else if (store_antic
&& bitmap_bit_p (store_antic
, loc
))
3781 tm_memopt_transform_stmt (TRANSFORM_RFW
, stmt
, &gsi
);
3782 bitmap_set_bit (store_avail
, loc
);
3784 else if (read_avail
&& bitmap_bit_p (read_avail
, loc
))
3785 tm_memopt_transform_stmt (TRANSFORM_RAR
, stmt
, &gsi
);
3787 bitmap_set_bit (read_avail
, loc
);
3789 else if (is_tm_simple_store (stmt
))
3791 loc
= tm_memopt_value_number (stmt
, NO_INSERT
);
3792 if (store_avail
&& bitmap_bit_p (store_avail
, loc
))
3793 tm_memopt_transform_stmt (TRANSFORM_WAW
, stmt
, &gsi
);
3796 if (read_avail
&& bitmap_bit_p (read_avail
, loc
))
3797 tm_memopt_transform_stmt (TRANSFORM_WAR
, stmt
, &gsi
);
3798 bitmap_set_bit (store_avail
, loc
);
3805 /* Return a new set of bitmaps for a BB. */
3807 static struct tm_memopt_bitmaps
*
3808 tm_memopt_init_sets (void)
3810 struct tm_memopt_bitmaps
*b
3811 = XOBNEW (&tm_memopt_obstack
.obstack
, struct tm_memopt_bitmaps
);
3812 b
->store_avail_in
= BITMAP_ALLOC (&tm_memopt_obstack
);
3813 b
->store_avail_out
= BITMAP_ALLOC (&tm_memopt_obstack
);
3814 b
->store_antic_in
= BITMAP_ALLOC (&tm_memopt_obstack
);
3815 b
->store_antic_out
= BITMAP_ALLOC (&tm_memopt_obstack
);
3816 b
->store_avail_out
= BITMAP_ALLOC (&tm_memopt_obstack
);
3817 b
->read_avail_in
= BITMAP_ALLOC (&tm_memopt_obstack
);
3818 b
->read_avail_out
= BITMAP_ALLOC (&tm_memopt_obstack
);
3819 b
->read_local
= BITMAP_ALLOC (&tm_memopt_obstack
);
3820 b
->store_local
= BITMAP_ALLOC (&tm_memopt_obstack
);
3824 /* Free sets computed for each BB. */
3827 tm_memopt_free_sets (vec
<basic_block
> blocks
)
3832 for (i
= 0; blocks
.iterate (i
, &bb
); ++i
)
3836 /* Clear the visited bit for every basic block in BLOCKS. */
3839 tm_memopt_clear_visited (vec
<basic_block
> blocks
)
3844 for (i
= 0; blocks
.iterate (i
, &bb
); ++i
)
3845 BB_VISITED_P (bb
) = false;
3848 /* Replace TM load/stores with hints for the runtime. We handle
3849 things like read-after-write, write-after-read, read-after-read,
3850 read-for-write, etc. */
3853 execute_tm_memopt (void)
3855 struct tm_region
*region
;
3856 vec
<basic_block
> bbs
;
3858 tm_memopt_value_id
= 0;
3859 tm_memopt_value_numbers
.create (10);
3861 for (region
= all_tm_regions
; region
; region
= region
->next
)
3863 /* All the TM stores/loads in the current region. */
3867 bitmap_obstack_initialize (&tm_memopt_obstack
);
3869 /* Save all BBs for the current region. */
3870 bbs
= get_tm_region_blocks (region
->entry_block
,
3871 region
->exit_blocks
,
3876 /* Collect all the memory operations. */
3877 for (i
= 0; bbs
.iterate (i
, &bb
); ++i
)
3879 bb
->aux
= tm_memopt_init_sets ();
3880 tm_memopt_accumulate_memops (bb
);
3883 /* Solve data flow equations and transform each block accordingly. */
3884 tm_memopt_clear_visited (bbs
);
3885 tm_memopt_compute_available (region
, bbs
);
3886 tm_memopt_clear_visited (bbs
);
3887 tm_memopt_compute_antic (region
, bbs
);
3888 tm_memopt_transform_blocks (bbs
);
3890 tm_memopt_free_sets (bbs
);
3892 bitmap_obstack_release (&tm_memopt_obstack
);
3893 tm_memopt_value_numbers
.empty ();
3896 tm_memopt_value_numbers
.dispose ();
3901 gate_tm_memopt (void)
3903 return flag_tm
&& optimize
> 0;
3908 const pass_data pass_data_tm_memopt
=
3910 GIMPLE_PASS
, /* type */
3911 "tmmemopt", /* name */
3912 OPTGROUP_NONE
, /* optinfo_flags */
3913 true, /* has_gate */
3914 true, /* has_execute */
3915 TV_TRANS_MEM
, /* tv_id */
3916 ( PROP_ssa
| PROP_cfg
), /* properties_required */
3917 0, /* properties_provided */
3918 0, /* properties_destroyed */
3919 0, /* todo_flags_start */
3920 0, /* todo_flags_finish */
3923 class pass_tm_memopt
: public gimple_opt_pass
3926 pass_tm_memopt(gcc::context
*ctxt
)
3927 : gimple_opt_pass(pass_data_tm_memopt
, ctxt
)
3930 /* opt_pass methods: */
3931 bool gate () { return gate_tm_memopt (); }
3932 unsigned int execute () { return execute_tm_memopt (); }
3934 }; // class pass_tm_memopt
3939 make_pass_tm_memopt (gcc::context
*ctxt
)
3941 return new pass_tm_memopt (ctxt
);
3945 /* Interprocedual analysis for the creation of transactional clones.
3946 The aim of this pass is to find which functions are referenced in
3947 a non-irrevocable transaction context, and for those over which
3948 we have control (or user directive), create a version of the
3949 function which uses only the transactional interface to reference
3950 protected memories. This analysis proceeds in several steps:
3952 (1) Collect the set of all possible transactional clones:
3954 (a) For all local public functions marked tm_callable, push
3955 it onto the tm_callee queue.
3957 (b) For all local functions, scan for calls in transaction blocks.
3958 Push the caller and callee onto the tm_caller and tm_callee
3959 queues. Count the number of callers for each callee.
3961 (c) For each local function on the callee list, assume we will
3962 create a transactional clone. Push *all* calls onto the
3963 callee queues; count the number of clone callers separately
3964 to the number of original callers.
3966 (2) Propagate irrevocable status up the dominator tree:
3968 (a) Any external function on the callee list that is not marked
3969 tm_callable is irrevocable. Push all callers of such onto
3972 (b) For each function on the worklist, mark each block that
3973 contains an irrevocable call. Use the AND operator to
3974 propagate that mark up the dominator tree.
3976 (c) If we reach the entry block for a possible transactional
3977 clone, then the transactional clone is irrevocable, and
3978 we should not create the clone after all. Push all
3979 callers onto the worklist.
3981 (d) Place tm_irrevocable calls at the beginning of the relevant
3982 blocks. Special case here is the entry block for the entire
3983 transaction region; there we mark it GTMA_DOES_GO_IRREVOCABLE for
3984 the library to begin the region in serial mode. Decrement
3985 the call count for all callees in the irrevocable region.
3987 (3) Create the transactional clones:
3989 Any tm_callee that still has a non-zero call count is cloned.
3992 /* This structure is stored in the AUX field of each cgraph_node. */
3993 struct tm_ipa_cg_data
3995 /* The clone of the function that got created. */
3996 struct cgraph_node
*clone
;
3998 /* The tm regions in the normal function. */
3999 struct tm_region
*all_tm_regions
;
4001 /* The blocks of the normal/clone functions that contain irrevocable
4002 calls, or blocks that are post-dominated by irrevocable calls. */
4003 bitmap irrevocable_blocks_normal
;
4004 bitmap irrevocable_blocks_clone
;
4006 /* The blocks of the normal function that are involved in transactions. */
4007 bitmap transaction_blocks_normal
;
4009 /* The number of callers to the transactional clone of this function
4010 from normal and transactional clones respectively. */
4011 unsigned tm_callers_normal
;
4012 unsigned tm_callers_clone
;
4014 /* True if all calls to this function's transactional clone
4015 are irrevocable. Also automatically true if the function
4016 has no transactional clone. */
4017 bool is_irrevocable
;
4019 /* Flags indicating the presence of this function in various queues. */
4020 bool in_callee_queue
;
4023 /* Flags indicating the kind of scan desired while in the worklist. */
4024 bool want_irr_scan_normal
;
4027 typedef vec
<cgraph_node_ptr
> cgraph_node_queue
;
4029 /* Return the ipa data associated with NODE, allocating zeroed memory
4030 if necessary. TRAVERSE_ALIASES is true if we must traverse aliases
4031 and set *NODE accordingly. */
4033 static struct tm_ipa_cg_data
*
4034 get_cg_data (struct cgraph_node
**node
, bool traverse_aliases
)
4036 struct tm_ipa_cg_data
*d
;
4038 if (traverse_aliases
&& (*node
)->symbol
.alias
)
4039 *node
= cgraph_alias_target (*node
);
4041 d
= (struct tm_ipa_cg_data
*) (*node
)->symbol
.aux
;
4045 d
= (struct tm_ipa_cg_data
*)
4046 obstack_alloc (&tm_obstack
.obstack
, sizeof (*d
));
4047 (*node
)->symbol
.aux
= (void *) d
;
4048 memset (d
, 0, sizeof (*d
));
4054 /* Add NODE to the end of QUEUE, unless IN_QUEUE_P indicates that
4055 it is already present. */
4058 maybe_push_queue (struct cgraph_node
*node
,
4059 cgraph_node_queue
*queue_p
, bool *in_queue_p
)
4064 queue_p
->safe_push (node
);
4068 /* Duplicate the basic blocks in QUEUE for use in the uninstrumented
4069 code path. QUEUE are the basic blocks inside the transaction
4070 represented in REGION.
4072 Later in split_code_paths() we will add the conditional to choose
4073 between the two alternatives. */
4076 ipa_uninstrument_transaction (struct tm_region
*region
,
4077 vec
<basic_block
> queue
)
4079 gimple transaction
= region
->transaction_stmt
;
4080 basic_block transaction_bb
= gimple_bb (transaction
);
4081 int n
= queue
.length ();
4082 basic_block
*new_bbs
= XNEWVEC (basic_block
, n
);
4084 copy_bbs (queue
.address (), n
, new_bbs
, NULL
, 0, NULL
, NULL
, transaction_bb
,
4086 edge e
= make_edge (transaction_bb
, new_bbs
[0], EDGE_TM_UNINSTRUMENTED
);
4087 add_phi_args_after_copy (new_bbs
, n
, e
);
4089 // Now we will have a GIMPLE_ATOMIC with 3 possible edges out of it.
4090 // a) EDGE_FALLTHRU into the transaction
4091 // b) EDGE_TM_ABORT out of the transaction
4092 // c) EDGE_TM_UNINSTRUMENTED into the uninstrumented blocks.
4097 /* A subroutine of ipa_tm_scan_calls_transaction and ipa_tm_scan_calls_clone.
4098 Queue all callees within block BB. */
4101 ipa_tm_scan_calls_block (cgraph_node_queue
*callees_p
,
4102 basic_block bb
, bool for_clone
)
4104 gimple_stmt_iterator gsi
;
4106 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4108 gimple stmt
= gsi_stmt (gsi
);
4109 if (is_gimple_call (stmt
) && !is_tm_pure_call (stmt
))
4111 tree fndecl
= gimple_call_fndecl (stmt
);
4114 struct tm_ipa_cg_data
*d
;
4116 struct cgraph_node
*node
;
4118 if (is_tm_ending_fndecl (fndecl
))
4120 if (find_tm_replacement_function (fndecl
))
4123 node
= cgraph_get_node (fndecl
);
4124 gcc_assert (node
!= NULL
);
4125 d
= get_cg_data (&node
, true);
4127 pcallers
= (for_clone
? &d
->tm_callers_clone
4128 : &d
->tm_callers_normal
);
4131 maybe_push_queue (node
, callees_p
, &d
->in_callee_queue
);
4137 /* Scan all calls in NODE that are within a transaction region,
4138 and push the resulting nodes into the callee queue. */
4141 ipa_tm_scan_calls_transaction (struct tm_ipa_cg_data
*d
,
4142 cgraph_node_queue
*callees_p
)
4144 struct tm_region
*r
;
4146 d
->transaction_blocks_normal
= BITMAP_ALLOC (&tm_obstack
);
4147 d
->all_tm_regions
= all_tm_regions
;
4149 for (r
= all_tm_regions
; r
; r
= r
->next
)
4151 vec
<basic_block
> bbs
;
4155 bbs
= get_tm_region_blocks (r
->entry_block
, r
->exit_blocks
, NULL
,
4156 d
->transaction_blocks_normal
, false);
4158 // Generate the uninstrumented code path for this transaction.
4159 ipa_uninstrument_transaction (r
, bbs
);
4161 FOR_EACH_VEC_ELT (bbs
, i
, bb
)
4162 ipa_tm_scan_calls_block (callees_p
, bb
, false);
4167 // ??? copy_bbs should maintain cgraph edges for the blocks as it is
4168 // copying them, rather than forcing us to do this externally.
4169 rebuild_cgraph_edges ();
4171 // ??? In ipa_uninstrument_transaction we don't try to update dominators
4172 // because copy_bbs doesn't return a VEC like iterate_fix_dominators expects.
4173 // Instead, just release dominators here so update_ssa recomputes them.
4174 free_dominance_info (CDI_DOMINATORS
);
4176 // When building the uninstrumented code path, copy_bbs will have invoked
4177 // create_new_def_for starting an "ssa update context". There is only one
4178 // instance of this context, so resolve ssa updates before moving on to
4179 // the next function.
4180 update_ssa (TODO_update_ssa
);
4183 /* Scan all calls in NODE as if this is the transactional clone,
4184 and push the destinations into the callee queue. */
4187 ipa_tm_scan_calls_clone (struct cgraph_node
*node
,
4188 cgraph_node_queue
*callees_p
)
4190 struct function
*fn
= DECL_STRUCT_FUNCTION (node
->symbol
.decl
);
4193 FOR_EACH_BB_FN (bb
, fn
)
4194 ipa_tm_scan_calls_block (callees_p
, bb
, true);
4197 /* The function NODE has been detected to be irrevocable. Push all
4198 of its callers onto WORKLIST for the purpose of re-scanning them. */
4201 ipa_tm_note_irrevocable (struct cgraph_node
*node
,
4202 cgraph_node_queue
*worklist_p
)
4204 struct tm_ipa_cg_data
*d
= get_cg_data (&node
, true);
4205 struct cgraph_edge
*e
;
4207 d
->is_irrevocable
= true;
4209 for (e
= node
->callers
; e
; e
= e
->next_caller
)
4212 struct cgraph_node
*caller
;
4214 /* Don't examine recursive calls. */
4215 if (e
->caller
== node
)
4217 /* Even if we think we can go irrevocable, believe the user
4219 if (is_tm_safe_or_pure (e
->caller
->symbol
.decl
))
4223 d
= get_cg_data (&caller
, true);
4225 /* Check if the callee is in a transactional region. If so,
4226 schedule the function for normal re-scan as well. */
4227 bb
= gimple_bb (e
->call_stmt
);
4228 gcc_assert (bb
!= NULL
);
4229 if (d
->transaction_blocks_normal
4230 && bitmap_bit_p (d
->transaction_blocks_normal
, bb
->index
))
4231 d
->want_irr_scan_normal
= true;
4233 maybe_push_queue (caller
, worklist_p
, &d
->in_worklist
);
4237 /* A subroutine of ipa_tm_scan_irr_blocks; return true iff any statement
4238 within the block is irrevocable. */
4241 ipa_tm_scan_irr_block (basic_block bb
)
4243 gimple_stmt_iterator gsi
;
4246 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4248 gimple stmt
= gsi_stmt (gsi
);
4249 switch (gimple_code (stmt
))
4252 if (gimple_assign_single_p (stmt
))
4254 tree lhs
= gimple_assign_lhs (stmt
);
4255 tree rhs
= gimple_assign_rhs1 (stmt
);
4256 if (volatile_var_p (lhs
) || volatile_var_p (rhs
))
4263 tree lhs
= gimple_call_lhs (stmt
);
4264 if (lhs
&& volatile_var_p (lhs
))
4267 if (is_tm_pure_call (stmt
))
4270 fn
= gimple_call_fn (stmt
);
4272 /* Functions with the attribute are by definition irrevocable. */
4273 if (is_tm_irrevocable (fn
))
4276 /* For direct function calls, go ahead and check for replacement
4277 functions, or transitive irrevocable functions. For indirect
4278 functions, we'll ask the runtime. */
4279 if (TREE_CODE (fn
) == ADDR_EXPR
)
4281 struct tm_ipa_cg_data
*d
;
4282 struct cgraph_node
*node
;
4284 fn
= TREE_OPERAND (fn
, 0);
4285 if (is_tm_ending_fndecl (fn
))
4287 if (find_tm_replacement_function (fn
))
4290 node
= cgraph_get_node(fn
);
4291 d
= get_cg_data (&node
, true);
4293 /* Return true if irrevocable, but above all, believe
4295 if (d
->is_irrevocable
4296 && !is_tm_safe_or_pure (fn
))
4303 /* ??? The Approved Method of indicating that an inline
4304 assembly statement is not relevant to the transaction
4305 is to wrap it in a __tm_waiver block. This is not
4306 yet implemented, so we can't check for it. */
4307 if (is_tm_safe (current_function_decl
))
4309 tree t
= build1 (NOP_EXPR
, void_type_node
, size_zero_node
);
4310 SET_EXPR_LOCATION (t
, gimple_location (stmt
));
4311 error ("%Kasm not allowed in %<transaction_safe%> function", t
);
4323 /* For each of the blocks seeded witin PQUEUE, walk the CFG looking
4324 for new irrevocable blocks, marking them in NEW_IRR. Don't bother
4325 scanning past OLD_IRR or EXIT_BLOCKS. */
4328 ipa_tm_scan_irr_blocks (vec
<basic_block
> *pqueue
, bitmap new_irr
,
4329 bitmap old_irr
, bitmap exit_blocks
)
4331 bool any_new_irr
= false;
4334 bitmap visited_blocks
= BITMAP_ALLOC (NULL
);
4338 basic_block bb
= pqueue
->pop ();
4340 /* Don't re-scan blocks we know already are irrevocable. */
4341 if (old_irr
&& bitmap_bit_p (old_irr
, bb
->index
))
4344 if (ipa_tm_scan_irr_block (bb
))
4346 bitmap_set_bit (new_irr
, bb
->index
);
4349 else if (exit_blocks
== NULL
|| !bitmap_bit_p (exit_blocks
, bb
->index
))
4351 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4352 if (!bitmap_bit_p (visited_blocks
, e
->dest
->index
))
4354 bitmap_set_bit (visited_blocks
, e
->dest
->index
);
4355 pqueue
->safe_push (e
->dest
);
4359 while (!pqueue
->is_empty ());
4361 BITMAP_FREE (visited_blocks
);
4366 /* Propagate the irrevocable property both up and down the dominator tree.
4367 BB is the current block being scanned; EXIT_BLOCKS are the edges of the
4368 TM regions; OLD_IRR are the results of a previous scan of the dominator
4369 tree which has been fully propagated; NEW_IRR is the set of new blocks
4370 which are gaining the irrevocable property during the current scan. */
4373 ipa_tm_propagate_irr (basic_block entry_block
, bitmap new_irr
,
4374 bitmap old_irr
, bitmap exit_blocks
)
4376 vec
<basic_block
> bbs
;
4377 bitmap all_region_blocks
;
4379 /* If this block is in the old set, no need to rescan. */
4380 if (old_irr
&& bitmap_bit_p (old_irr
, entry_block
->index
))
4383 all_region_blocks
= BITMAP_ALLOC (&tm_obstack
);
4384 bbs
= get_tm_region_blocks (entry_block
, exit_blocks
, NULL
,
4385 all_region_blocks
, false);
4388 basic_block bb
= bbs
.pop ();
4389 bool this_irr
= bitmap_bit_p (new_irr
, bb
->index
);
4390 bool all_son_irr
= false;
4394 /* Propagate up. If my children are, I am too, but we must have
4395 at least one child that is. */
4398 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4400 if (!bitmap_bit_p (new_irr
, e
->dest
->index
))
4402 all_son_irr
= false;
4410 /* Add block to new_irr if it hasn't already been processed. */
4411 if (!old_irr
|| !bitmap_bit_p (old_irr
, bb
->index
))
4413 bitmap_set_bit (new_irr
, bb
->index
);
4419 /* Propagate down to everyone we immediately dominate. */
4423 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
4425 son
= next_dom_son (CDI_DOMINATORS
, son
))
4427 /* Make sure block is actually in a TM region, and it
4428 isn't already in old_irr. */
4429 if ((!old_irr
|| !bitmap_bit_p (old_irr
, son
->index
))
4430 && bitmap_bit_p (all_region_blocks
, son
->index
))
4431 bitmap_set_bit (new_irr
, son
->index
);
4435 while (!bbs
.is_empty ());
4437 BITMAP_FREE (all_region_blocks
);
4442 ipa_tm_decrement_clone_counts (basic_block bb
, bool for_clone
)
4444 gimple_stmt_iterator gsi
;
4446 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4448 gimple stmt
= gsi_stmt (gsi
);
4449 if (is_gimple_call (stmt
) && !is_tm_pure_call (stmt
))
4451 tree fndecl
= gimple_call_fndecl (stmt
);
4454 struct tm_ipa_cg_data
*d
;
4456 struct cgraph_node
*tnode
;
4458 if (is_tm_ending_fndecl (fndecl
))
4460 if (find_tm_replacement_function (fndecl
))
4463 tnode
= cgraph_get_node (fndecl
);
4464 d
= get_cg_data (&tnode
, true);
4466 pcallers
= (for_clone
? &d
->tm_callers_clone
4467 : &d
->tm_callers_normal
);
4469 gcc_assert (*pcallers
> 0);
4476 /* (Re-)Scan the transaction blocks in NODE for calls to irrevocable functions,
4477 as well as other irrevocable actions such as inline assembly. Mark all
4478 such blocks as irrevocable and decrement the number of calls to
4479 transactional clones. Return true if, for the transactional clone, the
4480 entire function is irrevocable. */
4483 ipa_tm_scan_irr_function (struct cgraph_node
*node
, bool for_clone
)
4485 struct tm_ipa_cg_data
*d
;
4486 bitmap new_irr
, old_irr
;
4487 vec
<basic_block
> queue
;
4490 /* Builtin operators (operator new, and such). */
4491 if (DECL_STRUCT_FUNCTION (node
->symbol
.decl
) == NULL
4492 || DECL_STRUCT_FUNCTION (node
->symbol
.decl
)->cfg
== NULL
)
4495 push_cfun (DECL_STRUCT_FUNCTION (node
->symbol
.decl
));
4496 calculate_dominance_info (CDI_DOMINATORS
);
4498 d
= get_cg_data (&node
, true);
4500 new_irr
= BITMAP_ALLOC (&tm_obstack
);
4502 /* Scan each tm region, propagating irrevocable status through the tree. */
4505 old_irr
= d
->irrevocable_blocks_clone
;
4506 queue
.quick_push (single_succ (ENTRY_BLOCK_PTR
));
4507 if (ipa_tm_scan_irr_blocks (&queue
, new_irr
, old_irr
, NULL
))
4509 ipa_tm_propagate_irr (single_succ (ENTRY_BLOCK_PTR
), new_irr
,
4511 ret
= bitmap_bit_p (new_irr
, single_succ (ENTRY_BLOCK_PTR
)->index
);
4516 struct tm_region
*region
;
4518 old_irr
= d
->irrevocable_blocks_normal
;
4519 for (region
= d
->all_tm_regions
; region
; region
= region
->next
)
4521 queue
.quick_push (region
->entry_block
);
4522 if (ipa_tm_scan_irr_blocks (&queue
, new_irr
, old_irr
,
4523 region
->exit_blocks
))
4524 ipa_tm_propagate_irr (region
->entry_block
, new_irr
, old_irr
,
4525 region
->exit_blocks
);
4529 /* If we found any new irrevocable blocks, reduce the call count for
4530 transactional clones within the irrevocable blocks. Save the new
4531 set of irrevocable blocks for next time. */
4532 if (!bitmap_empty_p (new_irr
))
4534 bitmap_iterator bmi
;
4537 EXECUTE_IF_SET_IN_BITMAP (new_irr
, 0, i
, bmi
)
4538 ipa_tm_decrement_clone_counts (BASIC_BLOCK (i
), for_clone
);
4542 bitmap_ior_into (old_irr
, new_irr
);
4543 BITMAP_FREE (new_irr
);
4546 d
->irrevocable_blocks_clone
= new_irr
;
4548 d
->irrevocable_blocks_normal
= new_irr
;
4550 if (dump_file
&& new_irr
)
4553 bitmap_iterator bmi
;
4556 dname
= lang_hooks
.decl_printable_name (current_function_decl
, 2);
4557 EXECUTE_IF_SET_IN_BITMAP (new_irr
, 0, i
, bmi
)
4558 fprintf (dump_file
, "%s: bb %d goes irrevocable\n", dname
, i
);
4562 BITMAP_FREE (new_irr
);
4570 /* Return true if, for the transactional clone of NODE, any call
4571 may enter irrevocable mode. */
4574 ipa_tm_mayenterirr_function (struct cgraph_node
*node
)
4576 struct tm_ipa_cg_data
*d
;
4580 d
= get_cg_data (&node
, true);
4581 decl
= node
->symbol
.decl
;
4582 flags
= flags_from_decl_or_type (decl
);
4584 /* Handle some TM builtins. Ordinarily these aren't actually generated
4585 at this point, but handling these functions when written in by the
4586 user makes it easier to build unit tests. */
4587 if (flags
& ECF_TM_BUILTIN
)
4590 /* Filter out all functions that are marked. */
4591 if (flags
& ECF_TM_PURE
)
4593 if (is_tm_safe (decl
))
4595 if (is_tm_irrevocable (decl
))
4597 if (is_tm_callable (decl
))
4599 if (find_tm_replacement_function (decl
))
4602 /* If we aren't seeing the final version of the function we don't
4603 know what it will contain at runtime. */
4604 if (cgraph_function_body_availability (node
) < AVAIL_AVAILABLE
)
4607 /* If the function must go irrevocable, then of course true. */
4608 if (d
->is_irrevocable
)
4611 /* If there are any blocks marked irrevocable, then the function
4612 as a whole may enter irrevocable. */
4613 if (d
->irrevocable_blocks_clone
)
4616 /* We may have previously marked this function as tm_may_enter_irr;
4617 see pass_diagnose_tm_blocks. */
4618 if (node
->local
.tm_may_enter_irr
)
4621 /* Recurse on the main body for aliases. In general, this will
4622 result in one of the bits above being set so that we will not
4623 have to recurse next time. */
4624 if (node
->symbol
.alias
)
4625 return ipa_tm_mayenterirr_function (cgraph_get_node (node
->thunk
.alias
));
4627 /* What remains is unmarked local functions without items that force
4628 the function to go irrevocable. */
4632 /* Diagnose calls from transaction_safe functions to unmarked
4633 functions that are determined to not be safe. */
4636 ipa_tm_diagnose_tm_safe (struct cgraph_node
*node
)
4638 struct cgraph_edge
*e
;
4640 for (e
= node
->callees
; e
; e
= e
->next_callee
)
4641 if (!is_tm_callable (e
->callee
->symbol
.decl
)
4642 && e
->callee
->local
.tm_may_enter_irr
)
4643 error_at (gimple_location (e
->call_stmt
),
4644 "unsafe function call %qD within "
4645 "%<transaction_safe%> function", e
->callee
->symbol
.decl
);
4648 /* Diagnose call from atomic transactions to unmarked functions
4649 that are determined to not be safe. */
4652 ipa_tm_diagnose_transaction (struct cgraph_node
*node
,
4653 struct tm_region
*all_tm_regions
)
4655 struct tm_region
*r
;
4657 for (r
= all_tm_regions
; r
; r
= r
->next
)
4658 if (gimple_transaction_subcode (r
->transaction_stmt
) & GTMA_IS_RELAXED
)
4660 /* Atomic transactions can be nested inside relaxed. */
4662 ipa_tm_diagnose_transaction (node
, r
->inner
);
4666 vec
<basic_block
> bbs
;
4667 gimple_stmt_iterator gsi
;
4671 bbs
= get_tm_region_blocks (r
->entry_block
, r
->exit_blocks
,
4672 r
->irr_blocks
, NULL
, false);
4674 for (i
= 0; bbs
.iterate (i
, &bb
); ++i
)
4675 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4677 gimple stmt
= gsi_stmt (gsi
);
4680 if (gimple_code (stmt
) == GIMPLE_ASM
)
4682 error_at (gimple_location (stmt
),
4683 "asm not allowed in atomic transaction");
4687 if (!is_gimple_call (stmt
))
4689 fndecl
= gimple_call_fndecl (stmt
);
4691 /* Indirect function calls have been diagnosed already. */
4695 /* Stop at the end of the transaction. */
4696 if (is_tm_ending_fndecl (fndecl
))
4698 if (bitmap_bit_p (r
->exit_blocks
, bb
->index
))
4703 /* Marked functions have been diagnosed already. */
4704 if (is_tm_pure_call (stmt
))
4706 if (is_tm_callable (fndecl
))
4709 if (cgraph_local_info (fndecl
)->tm_may_enter_irr
)
4710 error_at (gimple_location (stmt
),
4711 "unsafe function call %qD within "
4712 "atomic transaction", fndecl
);
4719 /* Return a transactional mangled name for the DECL_ASSEMBLER_NAME in
4720 OLD_DECL. The returned value is a freshly malloced pointer that
4721 should be freed by the caller. */
4724 tm_mangle (tree old_asm_id
)
4726 const char *old_asm_name
;
4729 struct demangle_component
*dc
;
4732 /* Determine if the symbol is already a valid C++ mangled name. Do this
4733 even for C, which might be interfacing with C++ code via appropriately
4734 ugly identifiers. */
4735 /* ??? We could probably do just as well checking for "_Z" and be done. */
4736 old_asm_name
= IDENTIFIER_POINTER (old_asm_id
);
4737 dc
= cplus_demangle_v3_components (old_asm_name
, DMGL_NO_OPTS
, &alloc
);
4744 sprintf (length
, "%u", IDENTIFIER_LENGTH (old_asm_id
));
4745 tm_name
= concat ("_ZGTt", length
, old_asm_name
, NULL
);
4749 old_asm_name
+= 2; /* Skip _Z */
4753 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
4754 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
4755 /* Don't play silly games, you! */
4758 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
4759 /* I'd really like to know if we can ever be passed one of
4760 these from the C++ front end. The Logical Thing would
4761 seem that hidden-alias should be outer-most, so that we
4762 get hidden-alias of a transaction-clone and not vice-versa. */
4770 tm_name
= concat ("_ZGTt", old_asm_name
, NULL
);
4774 new_asm_id
= get_identifier (tm_name
);
4781 ipa_tm_mark_force_output_node (struct cgraph_node
*node
)
4783 cgraph_mark_force_output_node (node
);
4784 node
->symbol
.analyzed
= true;
4788 ipa_tm_mark_forced_by_abi_node (struct cgraph_node
*node
)
4790 node
->symbol
.forced_by_abi
= true;
4791 node
->symbol
.analyzed
= true;
4794 /* Callback data for ipa_tm_create_version_alias. */
4795 struct create_version_alias_info
4797 struct cgraph_node
*old_node
;
4801 /* A subroutine of ipa_tm_create_version, called via
4802 cgraph_for_node_and_aliases. Create new tm clones for each of
4803 the existing aliases. */
4805 ipa_tm_create_version_alias (struct cgraph_node
*node
, void *data
)
4807 struct create_version_alias_info
*info
4808 = (struct create_version_alias_info
*)data
;
4809 tree old_decl
, new_decl
, tm_name
;
4810 struct cgraph_node
*new_node
;
4812 if (!node
->symbol
.cpp_implicit_alias
)
4815 old_decl
= node
->symbol
.decl
;
4816 tm_name
= tm_mangle (DECL_ASSEMBLER_NAME (old_decl
));
4817 new_decl
= build_decl (DECL_SOURCE_LOCATION (old_decl
),
4818 TREE_CODE (old_decl
), tm_name
,
4819 TREE_TYPE (old_decl
));
4821 SET_DECL_ASSEMBLER_NAME (new_decl
, tm_name
);
4822 SET_DECL_RTL (new_decl
, NULL
);
4824 /* Based loosely on C++'s make_alias_for(). */
4825 TREE_PUBLIC (new_decl
) = TREE_PUBLIC (old_decl
);
4826 DECL_CONTEXT (new_decl
) = DECL_CONTEXT (old_decl
);
4827 DECL_LANG_SPECIFIC (new_decl
) = DECL_LANG_SPECIFIC (old_decl
);
4828 TREE_READONLY (new_decl
) = TREE_READONLY (old_decl
);
4829 DECL_EXTERNAL (new_decl
) = 0;
4830 DECL_ARTIFICIAL (new_decl
) = 1;
4831 TREE_ADDRESSABLE (new_decl
) = 1;
4832 TREE_USED (new_decl
) = 1;
4833 TREE_SYMBOL_REFERENCED (tm_name
) = 1;
4835 /* Perform the same remapping to the comdat group. */
4836 if (DECL_ONE_ONLY (new_decl
))
4837 DECL_COMDAT_GROUP (new_decl
) = tm_mangle (DECL_COMDAT_GROUP (old_decl
));
4839 new_node
= cgraph_same_body_alias (NULL
, new_decl
, info
->new_decl
);
4840 new_node
->tm_clone
= true;
4841 new_node
->symbol
.externally_visible
= info
->old_node
->symbol
.externally_visible
;
4842 /* ?? Do not traverse aliases here. */
4843 get_cg_data (&node
, false)->clone
= new_node
;
4845 record_tm_clone_pair (old_decl
, new_decl
);
4847 if (info
->old_node
->symbol
.force_output
4848 || ipa_ref_list_first_referring (&info
->old_node
->symbol
.ref_list
))
4849 ipa_tm_mark_force_output_node (new_node
);
4850 if (info
->old_node
->symbol
.forced_by_abi
)
4851 ipa_tm_mark_forced_by_abi_node (new_node
);
4855 /* Create a copy of the function (possibly declaration only) of OLD_NODE,
4856 appropriate for the transactional clone. */
4859 ipa_tm_create_version (struct cgraph_node
*old_node
)
4861 tree new_decl
, old_decl
, tm_name
;
4862 struct cgraph_node
*new_node
;
4864 old_decl
= old_node
->symbol
.decl
;
4865 new_decl
= copy_node (old_decl
);
4867 /* DECL_ASSEMBLER_NAME needs to be set before we call
4868 cgraph_copy_node_for_versioning below, because cgraph_node will
4869 fill the assembler_name_hash. */
4870 tm_name
= tm_mangle (DECL_ASSEMBLER_NAME (old_decl
));
4871 SET_DECL_ASSEMBLER_NAME (new_decl
, tm_name
);
4872 SET_DECL_RTL (new_decl
, NULL
);
4873 TREE_SYMBOL_REFERENCED (tm_name
) = 1;
4875 /* Perform the same remapping to the comdat group. */
4876 if (DECL_ONE_ONLY (new_decl
))
4877 DECL_COMDAT_GROUP (new_decl
) = tm_mangle (DECL_COMDAT_GROUP (old_decl
));
4879 new_node
= cgraph_copy_node_for_versioning (old_node
, new_decl
, vNULL
, NULL
);
4880 new_node
->local
.local
= false;
4881 new_node
->symbol
.externally_visible
= old_node
->symbol
.externally_visible
;
4882 new_node
->lowered
= true;
4883 new_node
->tm_clone
= 1;
4884 get_cg_data (&old_node
, true)->clone
= new_node
;
4886 if (cgraph_function_body_availability (old_node
) >= AVAIL_OVERWRITABLE
)
4888 /* Remap extern inline to static inline. */
4889 /* ??? Is it worth trying to use make_decl_one_only? */
4890 if (DECL_DECLARED_INLINE_P (new_decl
) && DECL_EXTERNAL (new_decl
))
4892 DECL_EXTERNAL (new_decl
) = 0;
4893 TREE_PUBLIC (new_decl
) = 0;
4894 DECL_WEAK (new_decl
) = 0;
4897 tree_function_versioning (old_decl
, new_decl
,
4902 record_tm_clone_pair (old_decl
, new_decl
);
4904 cgraph_call_function_insertion_hooks (new_node
);
4905 if (old_node
->symbol
.force_output
4906 || ipa_ref_list_first_referring (&old_node
->symbol
.ref_list
))
4907 ipa_tm_mark_force_output_node (new_node
);
4908 if (old_node
->symbol
.forced_by_abi
)
4909 ipa_tm_mark_forced_by_abi_node (new_node
);
4911 /* Do the same thing, but for any aliases of the original node. */
4913 struct create_version_alias_info data
;
4914 data
.old_node
= old_node
;
4915 data
.new_decl
= new_decl
;
4916 cgraph_for_node_and_aliases (old_node
, ipa_tm_create_version_alias
,
4921 /* Construct a call to TM_IRREVOCABLE and insert it at the beginning of BB. */
4924 ipa_tm_insert_irr_call (struct cgraph_node
*node
, struct tm_region
*region
,
4927 gimple_stmt_iterator gsi
;
4930 transaction_subcode_ior (region
, GTMA_MAY_ENTER_IRREVOCABLE
);
4932 g
= gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_IRREVOCABLE
),
4933 1, build_int_cst (NULL_TREE
, MODE_SERIALIRREVOCABLE
));
4935 split_block_after_labels (bb
);
4936 gsi
= gsi_after_labels (bb
);
4937 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
4939 cgraph_create_edge (node
,
4940 cgraph_get_create_node
4941 (builtin_decl_explicit (BUILT_IN_TM_IRREVOCABLE
)),
4943 compute_call_stmt_bb_frequency (node
->symbol
.decl
,
4947 /* Construct a call to TM_GETTMCLONE and insert it before GSI. */
4950 ipa_tm_insert_gettmclone_call (struct cgraph_node
*node
,
4951 struct tm_region
*region
,
4952 gimple_stmt_iterator
*gsi
, gimple stmt
)
4954 tree gettm_fn
, ret
, old_fn
, callfn
;
4958 old_fn
= gimple_call_fn (stmt
);
4960 if (TREE_CODE (old_fn
) == ADDR_EXPR
)
4962 tree fndecl
= TREE_OPERAND (old_fn
, 0);
4963 tree clone
= get_tm_clone_pair (fndecl
);
4965 /* By transforming the call into a TM_GETTMCLONE, we are
4966 technically taking the address of the original function and
4967 its clone. Explain this so inlining will know this function
4969 cgraph_mark_address_taken_node (cgraph_get_node (fndecl
));
4971 cgraph_mark_address_taken_node (cgraph_get_node (clone
));
4974 safe
= is_tm_safe (TREE_TYPE (old_fn
));
4975 gettm_fn
= builtin_decl_explicit (safe
? BUILT_IN_TM_GETTMCLONE_SAFE
4976 : BUILT_IN_TM_GETTMCLONE_IRR
);
4977 ret
= create_tmp_var (ptr_type_node
, NULL
);
4980 transaction_subcode_ior (region
, GTMA_MAY_ENTER_IRREVOCABLE
);
4982 /* Discard OBJ_TYPE_REF, since we weren't able to fold it. */
4983 if (TREE_CODE (old_fn
) == OBJ_TYPE_REF
)
4984 old_fn
= OBJ_TYPE_REF_EXPR (old_fn
);
4986 g
= gimple_build_call (gettm_fn
, 1, old_fn
);
4987 ret
= make_ssa_name (ret
, g
);
4988 gimple_call_set_lhs (g
, ret
);
4990 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4992 cgraph_create_edge (node
, cgraph_get_create_node (gettm_fn
), g
, 0,
4993 compute_call_stmt_bb_frequency (node
->symbol
.decl
,
4996 /* Cast return value from tm_gettmclone* into appropriate function
4998 callfn
= create_tmp_var (TREE_TYPE (old_fn
), NULL
);
4999 g2
= gimple_build_assign (callfn
,
5000 fold_build1 (NOP_EXPR
, TREE_TYPE (callfn
), ret
));
5001 callfn
= make_ssa_name (callfn
, g2
);
5002 gimple_assign_set_lhs (g2
, callfn
);
5003 gsi_insert_before (gsi
, g2
, GSI_SAME_STMT
);
5005 /* ??? This is a hack to preserve the NOTHROW bit on the call,
5006 which we would have derived from the decl. Failure to save
5007 this bit means we might have to split the basic block. */
5008 if (gimple_call_nothrow_p (stmt
))
5009 gimple_call_set_nothrow (stmt
, true);
5011 gimple_call_set_fn (stmt
, callfn
);
5013 /* Discarding OBJ_TYPE_REF above may produce incompatible LHS and RHS
5014 for a call statement. Fix it. */
5016 tree lhs
= gimple_call_lhs (stmt
);
5017 tree rettype
= TREE_TYPE (gimple_call_fntype (stmt
));
5019 && !useless_type_conversion_p (TREE_TYPE (lhs
), rettype
))
5023 temp
= create_tmp_reg (rettype
, 0);
5024 gimple_call_set_lhs (stmt
, temp
);
5026 g2
= gimple_build_assign (lhs
,
5027 fold_build1 (VIEW_CONVERT_EXPR
,
5028 TREE_TYPE (lhs
), temp
));
5029 gsi_insert_after (gsi
, g2
, GSI_SAME_STMT
);
5038 /* Helper function for ipa_tm_transform_calls*. Given a call
5039 statement in GSI which resides inside transaction REGION, redirect
5040 the call to either its wrapper function, or its clone. */
5043 ipa_tm_transform_calls_redirect (struct cgraph_node
*node
,
5044 struct tm_region
*region
,
5045 gimple_stmt_iterator
*gsi
,
5046 bool *need_ssa_rename_p
)
5048 gimple stmt
= gsi_stmt (*gsi
);
5049 struct cgraph_node
*new_node
;
5050 struct cgraph_edge
*e
= cgraph_edge (node
, stmt
);
5051 tree fndecl
= gimple_call_fndecl (stmt
);
5053 /* For indirect calls, pass the address through the runtime. */
5056 *need_ssa_rename_p
|=
5057 ipa_tm_insert_gettmclone_call (node
, region
, gsi
, stmt
);
5061 /* Handle some TM builtins. Ordinarily these aren't actually generated
5062 at this point, but handling these functions when written in by the
5063 user makes it easier to build unit tests. */
5064 if (flags_from_decl_or_type (fndecl
) & ECF_TM_BUILTIN
)
5067 /* Fixup recursive calls inside clones. */
5068 /* ??? Why did cgraph_copy_node_for_versioning update the call edges
5069 for recursion but not update the call statements themselves? */
5070 if (e
->caller
== e
->callee
&& decl_is_tm_clone (current_function_decl
))
5072 gimple_call_set_fndecl (stmt
, current_function_decl
);
5076 /* If there is a replacement, use it. */
5077 fndecl
= find_tm_replacement_function (fndecl
);
5080 new_node
= cgraph_get_create_node (fndecl
);
5082 /* ??? Mark all transaction_wrap functions tm_may_enter_irr.
5084 We can't do this earlier in record_tm_replacement because
5085 cgraph_remove_unreachable_nodes is called before we inject
5086 references to the node. Further, we can't do this in some
5087 nice central place in ipa_tm_execute because we don't have
5088 the exact list of wrapper functions that would be used.
5089 Marking more wrappers than necessary results in the creation
5090 of unnecessary cgraph_nodes, which can cause some of the
5091 other IPA passes to crash.
5093 We do need to mark these nodes so that we get the proper
5094 result in expand_call_tm. */
5095 /* ??? This seems broken. How is it that we're marking the
5096 CALLEE as may_enter_irr? Surely we should be marking the
5097 CALLER. Also note that find_tm_replacement_function also
5098 contains mappings into the TM runtime, e.g. memcpy. These
5099 we know won't go irrevocable. */
5100 new_node
->local
.tm_may_enter_irr
= 1;
5104 struct tm_ipa_cg_data
*d
;
5105 struct cgraph_node
*tnode
= e
->callee
;
5107 d
= get_cg_data (&tnode
, true);
5108 new_node
= d
->clone
;
5110 /* As we've already skipped pure calls and appropriate builtins,
5111 and we've already marked irrevocable blocks, if we can't come
5112 up with a static replacement, then ask the runtime. */
5113 if (new_node
== NULL
)
5115 *need_ssa_rename_p
|=
5116 ipa_tm_insert_gettmclone_call (node
, region
, gsi
, stmt
);
5120 fndecl
= new_node
->symbol
.decl
;
5123 cgraph_redirect_edge_callee (e
, new_node
);
5124 gimple_call_set_fndecl (stmt
, fndecl
);
5127 /* Helper function for ipa_tm_transform_calls. For a given BB,
5128 install calls to tm_irrevocable when IRR_BLOCKS are reached,
5129 redirect other calls to the generated transactional clone. */
5132 ipa_tm_transform_calls_1 (struct cgraph_node
*node
, struct tm_region
*region
,
5133 basic_block bb
, bitmap irr_blocks
)
5135 gimple_stmt_iterator gsi
;
5136 bool need_ssa_rename
= false;
5138 if (irr_blocks
&& bitmap_bit_p (irr_blocks
, bb
->index
))
5140 ipa_tm_insert_irr_call (node
, region
, bb
);
5144 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
5146 gimple stmt
= gsi_stmt (gsi
);
5148 if (!is_gimple_call (stmt
))
5150 if (is_tm_pure_call (stmt
))
5153 /* Redirect edges to the appropriate replacement or clone. */
5154 ipa_tm_transform_calls_redirect (node
, region
, &gsi
, &need_ssa_rename
);
5157 return need_ssa_rename
;
5160 /* Walk the CFG for REGION, beginning at BB. Install calls to
5161 tm_irrevocable when IRR_BLOCKS are reached, redirect other calls to
5162 the generated transactional clone. */
5165 ipa_tm_transform_calls (struct cgraph_node
*node
, struct tm_region
*region
,
5166 basic_block bb
, bitmap irr_blocks
)
5168 bool need_ssa_rename
= false;
5171 vec
<basic_block
> queue
= vNULL
;
5172 bitmap visited_blocks
= BITMAP_ALLOC (NULL
);
5174 queue
.safe_push (bb
);
5180 ipa_tm_transform_calls_1 (node
, region
, bb
, irr_blocks
);
5182 if (irr_blocks
&& bitmap_bit_p (irr_blocks
, bb
->index
))
5185 if (region
&& bitmap_bit_p (region
->exit_blocks
, bb
->index
))
5188 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5189 if (!bitmap_bit_p (visited_blocks
, e
->dest
->index
))
5191 bitmap_set_bit (visited_blocks
, e
->dest
->index
);
5192 queue
.safe_push (e
->dest
);
5195 while (!queue
.is_empty ());
5198 BITMAP_FREE (visited_blocks
);
5200 return need_ssa_rename
;
5203 /* Transform the calls within the TM regions within NODE. */
5206 ipa_tm_transform_transaction (struct cgraph_node
*node
)
5208 struct tm_ipa_cg_data
*d
;
5209 struct tm_region
*region
;
5210 bool need_ssa_rename
= false;
5212 d
= get_cg_data (&node
, true);
5214 push_cfun (DECL_STRUCT_FUNCTION (node
->symbol
.decl
));
5215 calculate_dominance_info (CDI_DOMINATORS
);
5217 for (region
= d
->all_tm_regions
; region
; region
= region
->next
)
5219 /* If we're sure to go irrevocable, don't transform anything. */
5220 if (d
->irrevocable_blocks_normal
5221 && bitmap_bit_p (d
->irrevocable_blocks_normal
,
5222 region
->entry_block
->index
))
5224 transaction_subcode_ior (region
, GTMA_DOES_GO_IRREVOCABLE
5225 | GTMA_MAY_ENTER_IRREVOCABLE
5226 | GTMA_HAS_NO_INSTRUMENTATION
);
5231 ipa_tm_transform_calls (node
, region
, region
->entry_block
,
5232 d
->irrevocable_blocks_normal
);
5235 if (need_ssa_rename
)
5236 update_ssa (TODO_update_ssa_only_virtuals
);
5241 /* Transform the calls within the transactional clone of NODE. */
5244 ipa_tm_transform_clone (struct cgraph_node
*node
)
5246 struct tm_ipa_cg_data
*d
;
5247 bool need_ssa_rename
;
5249 d
= get_cg_data (&node
, true);
5251 /* If this function makes no calls and has no irrevocable blocks,
5252 then there's nothing to do. */
5253 /* ??? Remove non-aborting top-level transactions. */
5254 if (!node
->callees
&& !node
->indirect_calls
&& !d
->irrevocable_blocks_clone
)
5257 push_cfun (DECL_STRUCT_FUNCTION (d
->clone
->symbol
.decl
));
5258 calculate_dominance_info (CDI_DOMINATORS
);
5261 ipa_tm_transform_calls (d
->clone
, NULL
, single_succ (ENTRY_BLOCK_PTR
),
5262 d
->irrevocable_blocks_clone
);
5264 if (need_ssa_rename
)
5265 update_ssa (TODO_update_ssa_only_virtuals
);
5270 /* Main entry point for the transactional memory IPA pass. */
5273 ipa_tm_execute (void)
5275 cgraph_node_queue tm_callees
= cgraph_node_queue();
5276 /* List of functions that will go irrevocable. */
5277 cgraph_node_queue irr_worklist
= cgraph_node_queue();
5279 struct cgraph_node
*node
;
5280 struct tm_ipa_cg_data
*d
;
5281 enum availability a
;
5284 #ifdef ENABLE_CHECKING
5288 bitmap_obstack_initialize (&tm_obstack
);
5289 initialize_original_copy_tables ();
5291 /* For all local functions marked tm_callable, queue them. */
5292 FOR_EACH_DEFINED_FUNCTION (node
)
5293 if (is_tm_callable (node
->symbol
.decl
)
5294 && cgraph_function_body_availability (node
) >= AVAIL_OVERWRITABLE
)
5296 d
= get_cg_data (&node
, true);
5297 maybe_push_queue (node
, &tm_callees
, &d
->in_callee_queue
);
5300 /* For all local reachable functions... */
5301 FOR_EACH_DEFINED_FUNCTION (node
)
5303 && cgraph_function_body_availability (node
) >= AVAIL_OVERWRITABLE
)
5305 /* ... marked tm_pure, record that fact for the runtime by
5306 indicating that the pure function is its own tm_callable.
5307 No need to do this if the function's address can't be taken. */
5308 if (is_tm_pure (node
->symbol
.decl
))
5310 if (!node
->local
.local
)
5311 record_tm_clone_pair (node
->symbol
.decl
, node
->symbol
.decl
);
5315 push_cfun (DECL_STRUCT_FUNCTION (node
->symbol
.decl
));
5316 calculate_dominance_info (CDI_DOMINATORS
);
5318 tm_region_init (NULL
);
5321 d
= get_cg_data (&node
, true);
5323 /* Scan for calls that are in each transaction, and
5324 generate the uninstrumented code path. */
5325 ipa_tm_scan_calls_transaction (d
, &tm_callees
);
5327 /* Put it in the worklist so we can scan the function
5328 later (ipa_tm_scan_irr_function) and mark the
5329 irrevocable blocks. */
5330 maybe_push_queue (node
, &irr_worklist
, &d
->in_worklist
);
5331 d
->want_irr_scan_normal
= true;
5337 /* For every local function on the callee list, scan as if we will be
5338 creating a transactional clone, queueing all new functions we find
5340 for (i
= 0; i
< tm_callees
.length (); ++i
)
5342 node
= tm_callees
[i
];
5343 a
= cgraph_function_body_availability (node
);
5344 d
= get_cg_data (&node
, true);
5346 /* Put it in the worklist so we can scan the function later
5347 (ipa_tm_scan_irr_function) and mark the irrevocable
5349 maybe_push_queue (node
, &irr_worklist
, &d
->in_worklist
);
5351 /* Some callees cannot be arbitrarily cloned. These will always be
5352 irrevocable. Mark these now, so that we need not scan them. */
5353 if (is_tm_irrevocable (node
->symbol
.decl
))
5354 ipa_tm_note_irrevocable (node
, &irr_worklist
);
5355 else if (a
<= AVAIL_NOT_AVAILABLE
5356 && !is_tm_safe_or_pure (node
->symbol
.decl
))
5357 ipa_tm_note_irrevocable (node
, &irr_worklist
);
5358 else if (a
>= AVAIL_OVERWRITABLE
)
5360 if (!tree_versionable_function_p (node
->symbol
.decl
))
5361 ipa_tm_note_irrevocable (node
, &irr_worklist
);
5362 else if (!d
->is_irrevocable
)
5364 /* If this is an alias, make sure its base is queued as well.
5365 we need not scan the callees now, as the base will do. */
5366 if (node
->symbol
.alias
)
5368 node
= cgraph_get_node (node
->thunk
.alias
);
5369 d
= get_cg_data (&node
, true);
5370 maybe_push_queue (node
, &tm_callees
, &d
->in_callee_queue
);
5374 /* Add all nodes called by this function into
5375 tm_callees as well. */
5376 ipa_tm_scan_calls_clone (node
, &tm_callees
);
5381 /* Iterate scans until no more work to be done. Prefer not to use
5382 vec::pop because the worklist tends to follow a breadth-first
5383 search of the callgraph, which should allow convergance with a
5384 minimum number of scans. But we also don't want the worklist
5385 array to grow without bound, so we shift the array up periodically. */
5386 for (i
= 0; i
< irr_worklist
.length (); ++i
)
5388 if (i
> 256 && i
== irr_worklist
.length () / 8)
5390 irr_worklist
.block_remove (0, i
);
5394 node
= irr_worklist
[i
];
5395 d
= get_cg_data (&node
, true);
5396 d
->in_worklist
= false;
5398 if (d
->want_irr_scan_normal
)
5400 d
->want_irr_scan_normal
= false;
5401 ipa_tm_scan_irr_function (node
, false);
5403 if (d
->in_callee_queue
&& ipa_tm_scan_irr_function (node
, true))
5404 ipa_tm_note_irrevocable (node
, &irr_worklist
);
5407 /* For every function on the callee list, collect the tm_may_enter_irr
5409 irr_worklist
.truncate (0);
5410 for (i
= 0; i
< tm_callees
.length (); ++i
)
5412 node
= tm_callees
[i
];
5413 if (ipa_tm_mayenterirr_function (node
))
5415 d
= get_cg_data (&node
, true);
5416 gcc_assert (d
->in_worklist
== false);
5417 maybe_push_queue (node
, &irr_worklist
, &d
->in_worklist
);
5421 /* Propagate the tm_may_enter_irr bit to callers until stable. */
5422 for (i
= 0; i
< irr_worklist
.length (); ++i
)
5424 struct cgraph_node
*caller
;
5425 struct cgraph_edge
*e
;
5426 struct ipa_ref
*ref
;
5429 if (i
> 256 && i
== irr_worklist
.length () / 8)
5431 irr_worklist
.block_remove (0, i
);
5435 node
= irr_worklist
[i
];
5436 d
= get_cg_data (&node
, true);
5437 d
->in_worklist
= false;
5438 node
->local
.tm_may_enter_irr
= true;
5440 /* Propagate back to normal callers. */
5441 for (e
= node
->callers
; e
; e
= e
->next_caller
)
5444 if (!is_tm_safe_or_pure (caller
->symbol
.decl
)
5445 && !caller
->local
.tm_may_enter_irr
)
5447 d
= get_cg_data (&caller
, true);
5448 maybe_push_queue (caller
, &irr_worklist
, &d
->in_worklist
);
5452 /* Propagate back to referring aliases as well. */
5453 for (j
= 0; ipa_ref_list_referring_iterate (&node
->symbol
.ref_list
, j
, ref
); j
++)
5455 caller
= cgraph (ref
->referring
);
5456 if (ref
->use
== IPA_REF_ALIAS
5457 && !caller
->local
.tm_may_enter_irr
)
5459 /* ?? Do not traverse aliases here. */
5460 d
= get_cg_data (&caller
, false);
5461 maybe_push_queue (caller
, &irr_worklist
, &d
->in_worklist
);
5466 /* Now validate all tm_safe functions, and all atomic regions in
5468 FOR_EACH_DEFINED_FUNCTION (node
)
5470 && cgraph_function_body_availability (node
) >= AVAIL_OVERWRITABLE
)
5472 d
= get_cg_data (&node
, true);
5473 if (is_tm_safe (node
->symbol
.decl
))
5474 ipa_tm_diagnose_tm_safe (node
);
5475 else if (d
->all_tm_regions
)
5476 ipa_tm_diagnose_transaction (node
, d
->all_tm_regions
);
5479 /* Create clones. Do those that are not irrevocable and have a
5480 positive call count. Do those publicly visible functions that
5481 the user directed us to clone. */
5482 for (i
= 0; i
< tm_callees
.length (); ++i
)
5486 node
= tm_callees
[i
];
5487 if (node
->symbol
.cpp_implicit_alias
)
5490 a
= cgraph_function_body_availability (node
);
5491 d
= get_cg_data (&node
, true);
5493 if (a
<= AVAIL_NOT_AVAILABLE
)
5494 doit
= is_tm_callable (node
->symbol
.decl
);
5495 else if (a
<= AVAIL_AVAILABLE
&& is_tm_callable (node
->symbol
.decl
))
5497 else if (!d
->is_irrevocable
5498 && d
->tm_callers_normal
+ d
->tm_callers_clone
> 0)
5502 ipa_tm_create_version (node
);
5505 /* Redirect calls to the new clones, and insert irrevocable marks. */
5506 for (i
= 0; i
< tm_callees
.length (); ++i
)
5508 node
= tm_callees
[i
];
5509 if (node
->symbol
.analyzed
)
5511 d
= get_cg_data (&node
, true);
5513 ipa_tm_transform_clone (node
);
5516 FOR_EACH_DEFINED_FUNCTION (node
)
5518 && cgraph_function_body_availability (node
) >= AVAIL_OVERWRITABLE
)
5520 d
= get_cg_data (&node
, true);
5521 if (d
->all_tm_regions
)
5522 ipa_tm_transform_transaction (node
);
5525 /* Free and clear all data structures. */
5526 tm_callees
.release ();
5527 irr_worklist
.release ();
5528 bitmap_obstack_release (&tm_obstack
);
5529 free_original_copy_tables ();
5531 FOR_EACH_FUNCTION (node
)
5532 node
->symbol
.aux
= NULL
;
5534 #ifdef ENABLE_CHECKING
5543 const pass_data pass_data_ipa_tm
=
5545 SIMPLE_IPA_PASS
, /* type */
5547 OPTGROUP_NONE
, /* optinfo_flags */
5548 true, /* has_gate */
5549 true, /* has_execute */
5550 TV_TRANS_MEM
, /* tv_id */
5551 ( PROP_ssa
| PROP_cfg
), /* properties_required */
5552 0, /* properties_provided */
5553 0, /* properties_destroyed */
5554 0, /* todo_flags_start */
5555 0, /* todo_flags_finish */
5558 class pass_ipa_tm
: public simple_ipa_opt_pass
5561 pass_ipa_tm(gcc::context
*ctxt
)
5562 : simple_ipa_opt_pass(pass_data_ipa_tm
, ctxt
)
5565 /* opt_pass methods: */
5566 bool gate () { return gate_tm (); }
5567 unsigned int execute () { return ipa_tm_execute (); }
5569 }; // class pass_ipa_tm
5573 simple_ipa_opt_pass
*
5574 make_pass_ipa_tm (gcc::context
*ctxt
)
5576 return new pass_ipa_tm (ctxt
);
5579 #include "gt-trans-mem.h"