1 /* Passes for transactional memory support.
2 Copyright (C) 2008-2015 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 "double-int.h"
35 #include "fold-const.h"
38 #include "hard-reg-set.h"
41 #include "dominance.h"
43 #include "basic-block.h"
44 #include "tree-ssa-alias.h"
45 #include "internal-fn.h"
47 #include "gimple-expr.h"
54 #include "gimple-iterator.h"
55 #include "gimplify-me.h"
56 #include "gimple-walk.h"
57 #include "gimple-ssa.h"
59 #include "plugin-api.h"
63 #include "stringpool.h"
64 #include "tree-ssanames.h"
65 #include "tree-into-ssa.h"
66 #include "tree-pass.h"
67 #include "tree-inline.h"
68 #include "diagnostic-core.h"
71 #include "trans-mem.h"
74 #include "langhooks.h"
75 #include "gimple-pretty-print.h"
77 #include "tree-ssa-address.h"
80 #define A_RUNINSTRUMENTEDCODE 0x0001
81 #define A_RUNUNINSTRUMENTEDCODE 0x0002
82 #define A_SAVELIVEVARIABLES 0x0004
83 #define A_RESTORELIVEVARIABLES 0x0008
84 #define A_ABORTTRANSACTION 0x0010
86 #define AR_USERABORT 0x0001
87 #define AR_USERRETRY 0x0002
88 #define AR_TMCONFLICT 0x0004
89 #define AR_EXCEPTIONBLOCKABORT 0x0008
90 #define AR_OUTERABORT 0x0010
92 #define MODE_SERIALIRREVOCABLE 0x0000
95 /* The representation of a transaction changes several times during the
96 lowering process. In the beginning, in the front-end we have the
97 GENERIC tree TRANSACTION_EXPR. For example,
105 During initial gimplification (gimplify.c) the TRANSACTION_EXPR node is
106 trivially replaced with a GIMPLE_TRANSACTION node.
108 During pass_lower_tm, we examine the body of transactions looking
109 for aborts. Transactions that do not contain an abort may be
110 merged into an outer transaction. We also add a TRY-FINALLY node
111 to arrange for the transaction to be committed on any exit.
113 [??? Think about how this arrangement affects throw-with-commit
114 and throw-with-abort operations. In this case we want the TRY to
115 handle gotos, but not to catch any exceptions because the transaction
116 will already be closed.]
118 GIMPLE_TRANSACTION [label=NULL] {
125 __builtin___tm_abort ();
127 __builtin___tm_commit ();
131 During pass_lower_eh, we create EH regions for the transactions,
132 intermixed with the regular EH stuff. This gives us a nice persistent
133 mapping (all the way through rtl) from transactional memory operation
134 back to the transaction, which allows us to get the abnormal edges
135 correct to model transaction aborts and restarts:
137 GIMPLE_TRANSACTION [label=over]
143 __builtin___tm_abort ();
144 __builtin___tm_commit ();
147 This is the end of all_lowering_passes, and so is what is present
148 during the IPA passes, and through all of the optimization passes.
150 During pass_ipa_tm, we examine all GIMPLE_TRANSACTION blocks in all
151 functions and mark functions for cloning.
153 At the end of gimple optimization, before exiting SSA form,
154 pass_tm_edges replaces statements that perform transactional
155 memory operations with the appropriate TM builtins, and swap
156 out function calls with their transactional clones. At this
157 point we introduce the abnormal transaction restart edges and
158 complete lowering of the GIMPLE_TRANSACTION node.
160 x = __builtin___tm_start (MAY_ABORT);
162 if (x & abort_transaction)
165 t0 = __builtin___tm_load (global);
167 __builtin___tm_store (&global, t1);
169 __builtin___tm_abort ();
170 __builtin___tm_commit ();
174 static void *expand_regions (struct tm_region
*,
175 void *(*callback
)(struct tm_region
*, void *),
179 /* Return the attributes we want to examine for X, or NULL if it's not
180 something we examine. We look at function types, but allow pointers
181 to function types and function decls and peek through. */
184 get_attrs_for (const_tree x
)
189 switch (TREE_CODE (x
))
192 return TYPE_ATTRIBUTES (TREE_TYPE (x
));
199 if (TREE_CODE (x
) != POINTER_TYPE
)
205 if (TREE_CODE (x
) != FUNCTION_TYPE
&& TREE_CODE (x
) != METHOD_TYPE
)
211 return TYPE_ATTRIBUTES (x
);
215 /* Return true if X has been marked TM_PURE. */
218 is_tm_pure (const_tree x
)
222 switch (TREE_CODE (x
))
233 if (TREE_CODE (x
) != POINTER_TYPE
)
239 if (TREE_CODE (x
) != FUNCTION_TYPE
&& TREE_CODE (x
) != METHOD_TYPE
)
244 flags
= flags_from_decl_or_type (x
);
245 return (flags
& ECF_TM_PURE
) != 0;
248 /* Return true if X has been marked TM_IRREVOCABLE. */
251 is_tm_irrevocable (tree x
)
253 tree attrs
= get_attrs_for (x
);
255 if (attrs
&& lookup_attribute ("transaction_unsafe", attrs
))
258 /* A call to the irrevocable builtin is by definition,
260 if (TREE_CODE (x
) == ADDR_EXPR
)
261 x
= TREE_OPERAND (x
, 0);
262 if (TREE_CODE (x
) == FUNCTION_DECL
263 && DECL_BUILT_IN_CLASS (x
) == BUILT_IN_NORMAL
264 && DECL_FUNCTION_CODE (x
) == BUILT_IN_TM_IRREVOCABLE
)
270 /* Return true if X has been marked TM_SAFE. */
273 is_tm_safe (const_tree x
)
277 tree attrs
= get_attrs_for (x
);
280 if (lookup_attribute ("transaction_safe", attrs
))
282 if (lookup_attribute ("transaction_may_cancel_outer", attrs
))
289 /* Return true if CALL is const, or tm_pure. */
292 is_tm_pure_call (gimple call
)
294 tree fn
= gimple_call_fn (call
);
296 if (TREE_CODE (fn
) == ADDR_EXPR
)
298 fn
= TREE_OPERAND (fn
, 0);
299 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
304 return is_tm_pure (fn
);
307 /* Return true if X has been marked TM_CALLABLE. */
310 is_tm_callable (tree x
)
312 tree attrs
= get_attrs_for (x
);
315 if (lookup_attribute ("transaction_callable", attrs
))
317 if (lookup_attribute ("transaction_safe", attrs
))
319 if (lookup_attribute ("transaction_may_cancel_outer", attrs
))
325 /* Return true if X has been marked TRANSACTION_MAY_CANCEL_OUTER. */
328 is_tm_may_cancel_outer (tree x
)
330 tree attrs
= get_attrs_for (x
);
332 return lookup_attribute ("transaction_may_cancel_outer", attrs
) != NULL
;
336 /* Return true for built in functions that "end" a transaction. */
339 is_tm_ending_fndecl (tree fndecl
)
341 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
342 switch (DECL_FUNCTION_CODE (fndecl
))
344 case BUILT_IN_TM_COMMIT
:
345 case BUILT_IN_TM_COMMIT_EH
:
346 case BUILT_IN_TM_ABORT
:
347 case BUILT_IN_TM_IRREVOCABLE
:
356 /* Return true if STMT is a built in function call that "ends" a
360 is_tm_ending (gimple stmt
)
364 if (gimple_code (stmt
) != GIMPLE_CALL
)
367 fndecl
= gimple_call_fndecl (stmt
);
368 return (fndecl
!= NULL_TREE
369 && is_tm_ending_fndecl (fndecl
));
372 /* Return true if STMT is a TM load. */
375 is_tm_load (gimple stmt
)
379 if (gimple_code (stmt
) != GIMPLE_CALL
)
382 fndecl
= gimple_call_fndecl (stmt
);
383 return (fndecl
&& DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
384 && BUILTIN_TM_LOAD_P (DECL_FUNCTION_CODE (fndecl
)));
387 /* Same as above, but for simple TM loads, that is, not the
388 after-write, after-read, etc optimized variants. */
391 is_tm_simple_load (gimple stmt
)
395 if (gimple_code (stmt
) != GIMPLE_CALL
)
398 fndecl
= gimple_call_fndecl (stmt
);
399 if (fndecl
&& DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
401 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
402 return (fcode
== BUILT_IN_TM_LOAD_1
403 || fcode
== BUILT_IN_TM_LOAD_2
404 || fcode
== BUILT_IN_TM_LOAD_4
405 || fcode
== BUILT_IN_TM_LOAD_8
406 || fcode
== BUILT_IN_TM_LOAD_FLOAT
407 || fcode
== BUILT_IN_TM_LOAD_DOUBLE
408 || fcode
== BUILT_IN_TM_LOAD_LDOUBLE
409 || fcode
== BUILT_IN_TM_LOAD_M64
410 || fcode
== BUILT_IN_TM_LOAD_M128
411 || fcode
== BUILT_IN_TM_LOAD_M256
);
416 /* Return true if STMT is a TM store. */
419 is_tm_store (gimple stmt
)
423 if (gimple_code (stmt
) != GIMPLE_CALL
)
426 fndecl
= gimple_call_fndecl (stmt
);
427 return (fndecl
&& DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
428 && BUILTIN_TM_STORE_P (DECL_FUNCTION_CODE (fndecl
)));
431 /* Same as above, but for simple TM stores, that is, not the
432 after-write, after-read, etc optimized variants. */
435 is_tm_simple_store (gimple stmt
)
439 if (gimple_code (stmt
) != GIMPLE_CALL
)
442 fndecl
= gimple_call_fndecl (stmt
);
443 if (fndecl
&& DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
445 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
446 return (fcode
== BUILT_IN_TM_STORE_1
447 || fcode
== BUILT_IN_TM_STORE_2
448 || fcode
== BUILT_IN_TM_STORE_4
449 || fcode
== BUILT_IN_TM_STORE_8
450 || fcode
== BUILT_IN_TM_STORE_FLOAT
451 || fcode
== BUILT_IN_TM_STORE_DOUBLE
452 || fcode
== BUILT_IN_TM_STORE_LDOUBLE
453 || fcode
== BUILT_IN_TM_STORE_M64
454 || fcode
== BUILT_IN_TM_STORE_M128
455 || fcode
== BUILT_IN_TM_STORE_M256
);
460 /* Return true if FNDECL is BUILT_IN_TM_ABORT. */
463 is_tm_abort (tree fndecl
)
466 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
467 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_TM_ABORT
);
470 /* Build a GENERIC tree for a user abort. This is called by front ends
471 while transforming the __tm_abort statement. */
474 build_tm_abort_call (location_t loc
, bool is_outer
)
476 return build_call_expr_loc (loc
, builtin_decl_explicit (BUILT_IN_TM_ABORT
), 1,
477 build_int_cst (integer_type_node
,
479 | (is_outer
? AR_OUTERABORT
: 0)));
482 /* Map for aribtrary function replacement under TM, as created
483 by the tm_wrap attribute. */
485 struct tm_wrapper_hasher
: ggc_cache_hasher
<tree_map
*>
487 static inline hashval_t
hash (tree_map
*m
) { return m
->hash
; }
489 equal (tree_map
*a
, tree_map
*b
)
491 return a
->base
.from
== b
->base
.from
;
495 handle_cache_entry (tree_map
*&m
)
497 extern void gt_ggc_mx (tree_map
*&);
498 if (m
== HTAB_EMPTY_ENTRY
|| m
== HTAB_DELETED_ENTRY
)
500 else if (ggc_marked_p (m
->base
.from
))
503 m
= static_cast<tree_map
*> (HTAB_DELETED_ENTRY
);
507 static GTY((cache
)) hash_table
<tm_wrapper_hasher
> *tm_wrap_map
;
510 record_tm_replacement (tree from
, tree to
)
512 struct tree_map
**slot
, *h
;
514 /* Do not inline wrapper functions that will get replaced in the TM
517 Suppose you have foo() that will get replaced into tmfoo(). Make
518 sure the inliner doesn't try to outsmart us and inline foo()
519 before we get a chance to do the TM replacement. */
520 DECL_UNINLINABLE (from
) = 1;
522 if (tm_wrap_map
== NULL
)
523 tm_wrap_map
= hash_table
<tm_wrapper_hasher
>::create_ggc (32);
525 h
= ggc_alloc
<tree_map
> ();
526 h
->hash
= htab_hash_pointer (from
);
530 slot
= tm_wrap_map
->find_slot_with_hash (h
, h
->hash
, INSERT
);
534 /* Return a TM-aware replacement function for DECL. */
537 find_tm_replacement_function (tree fndecl
)
541 struct tree_map
*h
, in
;
543 in
.base
.from
= fndecl
;
544 in
.hash
= htab_hash_pointer (fndecl
);
545 h
= tm_wrap_map
->find_with_hash (&in
, in
.hash
);
550 /* ??? We may well want TM versions of most of the common <string.h>
551 functions. For now, we've already these two defined. */
552 /* Adjust expand_call_tm() attributes as necessary for the cases
554 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
555 switch (DECL_FUNCTION_CODE (fndecl
))
557 case BUILT_IN_MEMCPY
:
558 return builtin_decl_explicit (BUILT_IN_TM_MEMCPY
);
559 case BUILT_IN_MEMMOVE
:
560 return builtin_decl_explicit (BUILT_IN_TM_MEMMOVE
);
561 case BUILT_IN_MEMSET
:
562 return builtin_decl_explicit (BUILT_IN_TM_MEMSET
);
570 /* When appropriate, record TM replacement for memory allocation functions.
572 FROM is the FNDECL to wrap. */
574 tm_malloc_replacement (tree from
)
579 if (TREE_CODE (from
) != FUNCTION_DECL
)
582 /* If we have a previous replacement, the user must be explicitly
583 wrapping malloc/calloc/free. They better know what they're
585 if (find_tm_replacement_function (from
))
588 str
= IDENTIFIER_POINTER (DECL_NAME (from
));
590 if (!strcmp (str
, "malloc"))
591 to
= builtin_decl_explicit (BUILT_IN_TM_MALLOC
);
592 else if (!strcmp (str
, "calloc"))
593 to
= builtin_decl_explicit (BUILT_IN_TM_CALLOC
);
594 else if (!strcmp (str
, "free"))
595 to
= builtin_decl_explicit (BUILT_IN_TM_FREE
);
599 TREE_NOTHROW (to
) = 0;
601 record_tm_replacement (from
, to
);
604 /* Diagnostics for tm_safe functions/regions. Called by the front end
605 once we've lowered the function to high-gimple. */
607 /* Subroutine of diagnose_tm_safe_errors, called through walk_gimple_seq.
608 Process exactly one statement. WI->INFO is set to non-null when in
609 the context of a tm_safe function, and null for a __transaction block. */
611 #define DIAG_TM_OUTER 1
612 #define DIAG_TM_SAFE 2
613 #define DIAG_TM_RELAXED 4
617 unsigned int summary_flags
: 8;
618 unsigned int block_flags
: 8;
619 unsigned int func_flags
: 8;
620 unsigned int saw_volatile
: 1;
624 /* Return true if T is a volatile variable of some kind. */
627 volatile_var_p (tree t
)
629 return (SSA_VAR_P (t
)
630 && TREE_THIS_VOLATILE (TREE_TYPE (t
)));
633 /* Tree callback function for diagnose_tm pass. */
636 diagnose_tm_1_op (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
639 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
640 struct diagnose_tm
*d
= (struct diagnose_tm
*) wi
->info
;
642 if (volatile_var_p (*tp
)
643 && d
->block_flags
& DIAG_TM_SAFE
647 error_at (gimple_location (d
->stmt
),
648 "invalid volatile use of %qD inside transaction",
656 is_tm_safe_or_pure (const_tree x
)
658 return is_tm_safe (x
) || is_tm_pure (x
);
662 diagnose_tm_1 (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
663 struct walk_stmt_info
*wi
)
665 gimple stmt
= gsi_stmt (*gsi
);
666 struct diagnose_tm
*d
= (struct diagnose_tm
*) wi
->info
;
668 /* Save stmt for use in leaf analysis. */
671 switch (gimple_code (stmt
))
675 tree fn
= gimple_call_fn (stmt
);
677 if ((d
->summary_flags
& DIAG_TM_OUTER
) == 0
678 && is_tm_may_cancel_outer (fn
))
679 error_at (gimple_location (stmt
),
680 "%<transaction_may_cancel_outer%> function call not within"
681 " outer transaction or %<transaction_may_cancel_outer%>");
683 if (d
->summary_flags
& DIAG_TM_SAFE
)
685 bool is_safe
, direct_call_p
;
688 if (TREE_CODE (fn
) == ADDR_EXPR
689 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
)
691 direct_call_p
= true;
692 replacement
= TREE_OPERAND (fn
, 0);
693 replacement
= find_tm_replacement_function (replacement
);
699 direct_call_p
= false;
700 replacement
= NULL_TREE
;
703 if (is_tm_safe_or_pure (fn
))
705 else if (is_tm_callable (fn
) || is_tm_irrevocable (fn
))
707 /* A function explicitly marked transaction_callable as
708 opposed to transaction_safe is being defined to be
709 unsafe as part of its ABI, regardless of its contents. */
712 else if (direct_call_p
)
714 if (IS_TYPE_OR_DECL_P (fn
)
715 && flags_from_decl_or_type (fn
) & ECF_TM_BUILTIN
)
717 else if (replacement
)
719 /* ??? At present we've been considering replacements
720 merely transaction_callable, and therefore might
721 enter irrevocable. The tm_wrap attribute has not
722 yet made it into the new language spec. */
727 /* ??? Diagnostics for unmarked direct calls moved into
728 the IPA pass. Section 3.2 of the spec details how
729 functions not marked should be considered "implicitly
730 safe" based on having examined the function body. */
736 /* An unmarked indirect call. Consider it unsafe even
737 though optimization may yet figure out how to inline. */
743 if (TREE_CODE (fn
) == ADDR_EXPR
)
744 fn
= TREE_OPERAND (fn
, 0);
745 if (d
->block_flags
& DIAG_TM_SAFE
)
748 error_at (gimple_location (stmt
),
749 "unsafe function call %qD within "
750 "atomic transaction", fn
);
753 if (!DECL_P (fn
) || DECL_NAME (fn
))
754 error_at (gimple_location (stmt
),
755 "unsafe function call %qE within "
756 "atomic transaction", fn
);
758 error_at (gimple_location (stmt
),
759 "unsafe indirect function call within "
760 "atomic transaction");
766 error_at (gimple_location (stmt
),
767 "unsafe function call %qD within "
768 "%<transaction_safe%> function", fn
);
771 if (!DECL_P (fn
) || DECL_NAME (fn
))
772 error_at (gimple_location (stmt
),
773 "unsafe function call %qE within "
774 "%<transaction_safe%> function", fn
);
776 error_at (gimple_location (stmt
),
777 "unsafe indirect function call within "
778 "%<transaction_safe%> function");
787 /* ??? We ought to come up with a way to add attributes to
788 asm statements, and then add "transaction_safe" to it.
789 Either that or get the language spec to resurrect __tm_waiver. */
790 if (d
->block_flags
& DIAG_TM_SAFE
)
791 error_at (gimple_location (stmt
),
792 "asm not allowed in atomic transaction");
793 else if (d
->func_flags
& DIAG_TM_SAFE
)
794 error_at (gimple_location (stmt
),
795 "asm not allowed in %<transaction_safe%> function");
798 case GIMPLE_TRANSACTION
:
800 gtransaction
*trans_stmt
= as_a
<gtransaction
*> (stmt
);
801 unsigned char inner_flags
= DIAG_TM_SAFE
;
803 if (gimple_transaction_subcode (trans_stmt
) & GTMA_IS_RELAXED
)
805 if (d
->block_flags
& DIAG_TM_SAFE
)
806 error_at (gimple_location (stmt
),
807 "relaxed transaction in atomic transaction");
808 else if (d
->func_flags
& DIAG_TM_SAFE
)
809 error_at (gimple_location (stmt
),
810 "relaxed transaction in %<transaction_safe%> function");
811 inner_flags
= DIAG_TM_RELAXED
;
813 else if (gimple_transaction_subcode (trans_stmt
) & GTMA_IS_OUTER
)
816 error_at (gimple_location (stmt
),
817 "outer transaction in transaction");
818 else if (d
->func_flags
& DIAG_TM_OUTER
)
819 error_at (gimple_location (stmt
),
820 "outer transaction in "
821 "%<transaction_may_cancel_outer%> function");
822 else if (d
->func_flags
& DIAG_TM_SAFE
)
823 error_at (gimple_location (stmt
),
824 "outer transaction in %<transaction_safe%> function");
825 inner_flags
|= DIAG_TM_OUTER
;
828 *handled_ops_p
= true;
829 if (gimple_transaction_body (trans_stmt
))
831 struct walk_stmt_info wi_inner
;
832 struct diagnose_tm d_inner
;
834 memset (&d_inner
, 0, sizeof (d_inner
));
835 d_inner
.func_flags
= d
->func_flags
;
836 d_inner
.block_flags
= d
->block_flags
| inner_flags
;
837 d_inner
.summary_flags
= d_inner
.func_flags
| d_inner
.block_flags
;
839 memset (&wi_inner
, 0, sizeof (wi_inner
));
840 wi_inner
.info
= &d_inner
;
842 walk_gimple_seq (gimple_transaction_body (trans_stmt
),
843 diagnose_tm_1
, diagnose_tm_1_op
, &wi_inner
);
856 diagnose_tm_blocks (void)
858 struct walk_stmt_info wi
;
859 struct diagnose_tm d
;
861 memset (&d
, 0, sizeof (d
));
862 if (is_tm_may_cancel_outer (current_function_decl
))
863 d
.func_flags
= DIAG_TM_OUTER
| DIAG_TM_SAFE
;
864 else if (is_tm_safe (current_function_decl
))
865 d
.func_flags
= DIAG_TM_SAFE
;
866 d
.summary_flags
= d
.func_flags
;
868 memset (&wi
, 0, sizeof (wi
));
871 walk_gimple_seq (gimple_body (current_function_decl
),
872 diagnose_tm_1
, diagnose_tm_1_op
, &wi
);
879 const pass_data pass_data_diagnose_tm_blocks
=
881 GIMPLE_PASS
, /* type */
882 "*diagnose_tm_blocks", /* name */
883 OPTGROUP_NONE
, /* optinfo_flags */
884 TV_TRANS_MEM
, /* tv_id */
885 PROP_gimple_any
, /* properties_required */
886 0, /* properties_provided */
887 0, /* properties_destroyed */
888 0, /* todo_flags_start */
889 0, /* todo_flags_finish */
892 class pass_diagnose_tm_blocks
: public gimple_opt_pass
895 pass_diagnose_tm_blocks (gcc::context
*ctxt
)
896 : gimple_opt_pass (pass_data_diagnose_tm_blocks
, ctxt
)
899 /* opt_pass methods: */
900 virtual bool gate (function
*) { return flag_tm
; }
901 virtual unsigned int execute (function
*) { return diagnose_tm_blocks (); }
903 }; // class pass_diagnose_tm_blocks
908 make_pass_diagnose_tm_blocks (gcc::context
*ctxt
)
910 return new pass_diagnose_tm_blocks (ctxt
);
913 /* Instead of instrumenting thread private memory, we save the
914 addresses in a log which we later use to save/restore the addresses
915 upon transaction start/restart.
917 The log is keyed by address, where each element contains individual
918 statements among different code paths that perform the store.
920 This log is later used to generate either plain save/restore of the
921 addresses upon transaction start/restart, or calls to the ITM_L*
924 So for something like:
926 struct large { int x[1000]; };
927 struct large lala = { 0 };
933 We can either save/restore:
936 trxn = _ITM_startTransaction ();
937 if (trxn & a_saveLiveVariables)
938 tmp_lala1 = lala.x[i];
939 else if (a & a_restoreLiveVariables)
940 lala.x[i] = tmp_lala1;
942 or use the logging functions:
945 trxn = _ITM_startTransaction ();
946 _ITM_LU4 (&lala.x[i]);
948 Obviously, if we use _ITM_L* to log, we prefer to call _ITM_L* as
949 far up the dominator tree to shadow all of the writes to a given
950 location (thus reducing the total number of logging calls), but not
951 so high as to be called on a path that does not perform a
954 /* One individual log entry. We may have multiple statements for the
955 same location if neither dominate each other (on different
957 typedef struct tm_log_entry
959 /* Address to save. */
961 /* Entry block for the transaction this address occurs in. */
962 basic_block entry_block
;
963 /* Dominating statements the store occurs in. */
965 /* Initially, while we are building the log, we place a nonzero
966 value here to mean that this address *will* be saved with a
967 save/restore sequence. Later, when generating the save sequence
968 we place the SSA temp generated here. */
973 /* Log entry hashtable helpers. */
975 struct log_entry_hasher
977 typedef tm_log_entry value_type
;
978 typedef tm_log_entry compare_type
;
979 static inline hashval_t
hash (const value_type
*);
980 static inline bool equal (const value_type
*, const compare_type
*);
981 static inline void remove (value_type
*);
984 /* Htab support. Return hash value for a `tm_log_entry'. */
986 log_entry_hasher::hash (const value_type
*log
)
988 return iterative_hash_expr (log
->addr
, 0);
991 /* Htab support. Return true if two log entries are the same. */
993 log_entry_hasher::equal (const value_type
*log1
, const compare_type
*log2
)
997 rth: I suggest that we get rid of the component refs etc.
998 I.e. resolve the reference to base + offset.
1000 We may need to actually finish a merge with mainline for this,
1001 since we'd like to be presented with Richi's MEM_REF_EXPRs more
1002 often than not. But in the meantime your tm_log_entry could save
1003 the results of get_inner_reference.
1005 See: g++.dg/tm/pr46653.C
1008 /* Special case plain equality because operand_equal_p() below will
1009 return FALSE if the addresses are equal but they have
1010 side-effects (e.g. a volatile address). */
1011 if (log1
->addr
== log2
->addr
)
1014 return operand_equal_p (log1
->addr
, log2
->addr
, 0);
1017 /* Htab support. Free one tm_log_entry. */
1019 log_entry_hasher::remove (value_type
*lp
)
1021 lp
->stmts
.release ();
1026 /* The actual log. */
1027 static hash_table
<log_entry_hasher
> *tm_log
;
1029 /* Addresses to log with a save/restore sequence. These should be in
1031 static vec
<tree
> tm_log_save_addresses
;
1033 enum thread_memory_type
1037 mem_transaction_local
,
1041 typedef struct tm_new_mem_map
1043 /* SSA_NAME being dereferenced. */
1045 enum thread_memory_type local_new_memory
;
1048 /* Hashtable helpers. */
1050 struct tm_mem_map_hasher
: typed_free_remove
<tm_new_mem_map_t
>
1052 typedef tm_new_mem_map_t value_type
;
1053 typedef tm_new_mem_map_t compare_type
;
1054 static inline hashval_t
hash (const value_type
*);
1055 static inline bool equal (const value_type
*, const compare_type
*);
1059 tm_mem_map_hasher::hash (const value_type
*v
)
1061 return (intptr_t)v
->val
>> 4;
1065 tm_mem_map_hasher::equal (const value_type
*v
, const compare_type
*c
)
1067 return v
->val
== c
->val
;
1070 /* Map for an SSA_NAME originally pointing to a non aliased new piece
1071 of memory (malloc, alloc, etc). */
1072 static hash_table
<tm_mem_map_hasher
> *tm_new_mem_hash
;
1074 /* Initialize logging data structures. */
1078 tm_log
= new hash_table
<log_entry_hasher
> (10);
1079 tm_new_mem_hash
= new hash_table
<tm_mem_map_hasher
> (5);
1080 tm_log_save_addresses
.create (5);
1083 /* Free logging data structures. */
1085 tm_log_delete (void)
1089 delete tm_new_mem_hash
;
1090 tm_new_mem_hash
= NULL
;
1091 tm_log_save_addresses
.release ();
1094 /* Return true if MEM is a transaction invariant memory for the TM
1095 region starting at REGION_ENTRY_BLOCK. */
1097 transaction_invariant_address_p (const_tree mem
, basic_block region_entry_block
)
1099 if ((TREE_CODE (mem
) == INDIRECT_REF
|| TREE_CODE (mem
) == MEM_REF
)
1100 && TREE_CODE (TREE_OPERAND (mem
, 0)) == SSA_NAME
)
1104 def_bb
= gimple_bb (SSA_NAME_DEF_STMT (TREE_OPERAND (mem
, 0)));
1105 return def_bb
!= region_entry_block
1106 && dominated_by_p (CDI_DOMINATORS
, region_entry_block
, def_bb
);
1109 mem
= strip_invariant_refs (mem
);
1110 return mem
&& (CONSTANT_CLASS_P (mem
) || decl_address_invariant_p (mem
));
1113 /* Given an address ADDR in STMT, find it in the memory log or add it,
1114 making sure to keep only the addresses highest in the dominator
1117 ENTRY_BLOCK is the entry_block for the transaction.
1119 If we find the address in the log, make sure it's either the same
1120 address, or an equivalent one that dominates ADDR.
1122 If we find the address, but neither ADDR dominates the found
1123 address, nor the found one dominates ADDR, we're on different
1124 execution paths. Add it.
1126 If known, ENTRY_BLOCK is the entry block for the region, otherwise
1129 tm_log_add (basic_block entry_block
, tree addr
, gimple stmt
)
1131 tm_log_entry
**slot
;
1132 struct tm_log_entry l
, *lp
;
1135 slot
= tm_log
->find_slot (&l
, INSERT
);
1138 tree type
= TREE_TYPE (addr
);
1140 lp
= XNEW (struct tm_log_entry
);
1144 /* Small invariant addresses can be handled as save/restores. */
1146 && transaction_invariant_address_p (lp
->addr
, entry_block
)
1147 && TYPE_SIZE_UNIT (type
) != NULL
1148 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type
))
1149 && ((HOST_WIDE_INT
) tree_to_uhwi (TYPE_SIZE_UNIT (type
))
1150 < PARAM_VALUE (PARAM_TM_MAX_AGGREGATE_SIZE
))
1151 /* We must be able to copy this type normally. I.e., no
1152 special constructors and the like. */
1153 && !TREE_ADDRESSABLE (type
))
1155 lp
->save_var
= create_tmp_reg (TREE_TYPE (lp
->addr
), "tm_save");
1156 lp
->stmts
.create (0);
1157 lp
->entry_block
= entry_block
;
1158 /* Save addresses separately in dominator order so we don't
1159 get confused by overlapping addresses in the save/restore
1161 tm_log_save_addresses
.safe_push (lp
->addr
);
1165 /* Use the logging functions. */
1166 lp
->stmts
.create (5);
1167 lp
->stmts
.quick_push (stmt
);
1168 lp
->save_var
= NULL
;
1178 /* If we're generating a save/restore sequence, we don't care
1179 about statements. */
1183 for (i
= 0; lp
->stmts
.iterate (i
, &oldstmt
); ++i
)
1185 if (stmt
== oldstmt
)
1187 /* We already have a store to the same address, higher up the
1188 dominator tree. Nothing to do. */
1189 if (dominated_by_p (CDI_DOMINATORS
,
1190 gimple_bb (stmt
), gimple_bb (oldstmt
)))
1192 /* We should be processing blocks in dominator tree order. */
1193 gcc_assert (!dominated_by_p (CDI_DOMINATORS
,
1194 gimple_bb (oldstmt
), gimple_bb (stmt
)));
1196 /* Store is on a different code path. */
1197 lp
->stmts
.safe_push (stmt
);
1201 /* Gimplify the address of a TARGET_MEM_REF. Return the SSA_NAME
1202 result, insert the new statements before GSI. */
1205 gimplify_addr (gimple_stmt_iterator
*gsi
, tree x
)
1207 if (TREE_CODE (x
) == TARGET_MEM_REF
)
1208 x
= tree_mem_ref_addr (build_pointer_type (TREE_TYPE (x
)), x
);
1210 x
= build_fold_addr_expr (x
);
1211 return force_gimple_operand_gsi (gsi
, x
, true, NULL
, true, GSI_SAME_STMT
);
1214 /* Instrument one address with the logging functions.
1215 ADDR is the address to save.
1216 STMT is the statement before which to place it. */
1218 tm_log_emit_stmt (tree addr
, gimple stmt
)
1220 tree type
= TREE_TYPE (addr
);
1221 tree size
= TYPE_SIZE_UNIT (type
);
1222 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
1224 enum built_in_function code
= BUILT_IN_TM_LOG
;
1226 if (type
== float_type_node
)
1227 code
= BUILT_IN_TM_LOG_FLOAT
;
1228 else if (type
== double_type_node
)
1229 code
= BUILT_IN_TM_LOG_DOUBLE
;
1230 else if (type
== long_double_type_node
)
1231 code
= BUILT_IN_TM_LOG_LDOUBLE
;
1232 else if (tree_fits_uhwi_p (size
))
1234 unsigned int n
= tree_to_uhwi (size
);
1238 code
= BUILT_IN_TM_LOG_1
;
1241 code
= BUILT_IN_TM_LOG_2
;
1244 code
= BUILT_IN_TM_LOG_4
;
1247 code
= BUILT_IN_TM_LOG_8
;
1250 code
= BUILT_IN_TM_LOG
;
1251 if (TREE_CODE (type
) == VECTOR_TYPE
)
1253 if (n
== 8 && builtin_decl_explicit (BUILT_IN_TM_LOG_M64
))
1254 code
= BUILT_IN_TM_LOG_M64
;
1255 else if (n
== 16 && builtin_decl_explicit (BUILT_IN_TM_LOG_M128
))
1256 code
= BUILT_IN_TM_LOG_M128
;
1257 else if (n
== 32 && builtin_decl_explicit (BUILT_IN_TM_LOG_M256
))
1258 code
= BUILT_IN_TM_LOG_M256
;
1264 addr
= gimplify_addr (&gsi
, addr
);
1265 if (code
== BUILT_IN_TM_LOG
)
1266 log
= gimple_build_call (builtin_decl_explicit (code
), 2, addr
, size
);
1268 log
= gimple_build_call (builtin_decl_explicit (code
), 1, addr
);
1269 gsi_insert_before (&gsi
, log
, GSI_SAME_STMT
);
1272 /* Go through the log and instrument address that must be instrumented
1273 with the logging functions. Leave the save/restore addresses for
1278 hash_table
<log_entry_hasher
>::iterator hi
;
1279 struct tm_log_entry
*lp
;
1281 FOR_EACH_HASH_TABLE_ELEMENT (*tm_log
, lp
, tm_log_entry_t
, hi
)
1288 fprintf (dump_file
, "TM thread private mem logging: ");
1289 print_generic_expr (dump_file
, lp
->addr
, 0);
1290 fprintf (dump_file
, "\n");
1296 fprintf (dump_file
, "DUMPING to variable\n");
1302 fprintf (dump_file
, "DUMPING with logging functions\n");
1303 for (i
= 0; lp
->stmts
.iterate (i
, &stmt
); ++i
)
1304 tm_log_emit_stmt (lp
->addr
, stmt
);
1309 /* Emit the save sequence for the corresponding addresses in the log.
1310 ENTRY_BLOCK is the entry block for the transaction.
1311 BB is the basic block to insert the code in. */
1313 tm_log_emit_saves (basic_block entry_block
, basic_block bb
)
1316 gimple_stmt_iterator gsi
= gsi_last_bb (bb
);
1318 struct tm_log_entry l
, *lp
;
1320 for (i
= 0; i
< tm_log_save_addresses
.length (); ++i
)
1322 l
.addr
= tm_log_save_addresses
[i
];
1323 lp
= *(tm_log
->find_slot (&l
, NO_INSERT
));
1324 gcc_assert (lp
->save_var
!= NULL
);
1326 /* We only care about variables in the current transaction. */
1327 if (lp
->entry_block
!= entry_block
)
1330 stmt
= gimple_build_assign (lp
->save_var
, unshare_expr (lp
->addr
));
1332 /* Make sure we can create an SSA_NAME for this type. For
1333 instance, aggregates aren't allowed, in which case the system
1334 will create a VOP for us and everything will just work. */
1335 if (is_gimple_reg_type (TREE_TYPE (lp
->save_var
)))
1337 lp
->save_var
= make_ssa_name (lp
->save_var
, stmt
);
1338 gimple_assign_set_lhs (stmt
, lp
->save_var
);
1341 gsi_insert_before (&gsi
, stmt
, GSI_SAME_STMT
);
1345 /* Emit the restore sequence for the corresponding addresses in the log.
1346 ENTRY_BLOCK is the entry block for the transaction.
1347 BB is the basic block to insert the code in. */
1349 tm_log_emit_restores (basic_block entry_block
, basic_block bb
)
1352 struct tm_log_entry l
, *lp
;
1353 gimple_stmt_iterator gsi
;
1356 for (i
= tm_log_save_addresses
.length () - 1; i
>= 0; i
--)
1358 l
.addr
= tm_log_save_addresses
[i
];
1359 lp
= *(tm_log
->find_slot (&l
, NO_INSERT
));
1360 gcc_assert (lp
->save_var
!= NULL
);
1362 /* We only care about variables in the current transaction. */
1363 if (lp
->entry_block
!= entry_block
)
1366 /* Restores are in LIFO order from the saves in case we have
1368 gsi
= gsi_start_bb (bb
);
1370 stmt
= gimple_build_assign (unshare_expr (lp
->addr
), lp
->save_var
);
1371 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
1376 static tree
lower_sequence_tm (gimple_stmt_iterator
*, bool *,
1377 struct walk_stmt_info
*);
1378 static tree
lower_sequence_no_tm (gimple_stmt_iterator
*, bool *,
1379 struct walk_stmt_info
*);
1381 /* Evaluate an address X being dereferenced and determine if it
1382 originally points to a non aliased new chunk of memory (malloc,
1385 Return MEM_THREAD_LOCAL if it points to a thread-local address.
1386 Return MEM_TRANSACTION_LOCAL if it points to a transaction-local address.
1387 Return MEM_NON_LOCAL otherwise.
1389 ENTRY_BLOCK is the entry block to the transaction containing the
1390 dereference of X. */
1391 static enum thread_memory_type
1392 thread_private_new_memory (basic_block entry_block
, tree x
)
1395 enum tree_code code
;
1396 tm_new_mem_map_t
**slot
;
1397 tm_new_mem_map_t elt
, *elt_p
;
1399 enum thread_memory_type retval
= mem_transaction_local
;
1402 || TREE_CODE (x
) != SSA_NAME
1403 /* Possible uninitialized use, or a function argument. In
1404 either case, we don't care. */
1405 || SSA_NAME_IS_DEFAULT_DEF (x
))
1406 return mem_non_local
;
1408 /* Look in cache first. */
1410 slot
= tm_new_mem_hash
->find_slot (&elt
, INSERT
);
1413 return elt_p
->local_new_memory
;
1415 /* Optimistically assume the memory is transaction local during
1416 processing. This catches recursion into this variable. */
1417 *slot
= elt_p
= XNEW (tm_new_mem_map_t
);
1419 elt_p
->local_new_memory
= mem_transaction_local
;
1421 /* Search DEF chain to find the original definition of this address. */
1424 if (ptr_deref_may_alias_global_p (x
))
1426 /* Address escapes. This is not thread-private. */
1427 retval
= mem_non_local
;
1428 goto new_memory_ret
;
1431 stmt
= SSA_NAME_DEF_STMT (x
);
1433 /* If the malloc call is outside the transaction, this is
1435 if (retval
!= mem_thread_local
1436 && !dominated_by_p (CDI_DOMINATORS
, gimple_bb (stmt
), entry_block
))
1437 retval
= mem_thread_local
;
1439 if (is_gimple_assign (stmt
))
1441 code
= gimple_assign_rhs_code (stmt
);
1442 /* x = foo ==> foo */
1443 if (code
== SSA_NAME
)
1444 x
= gimple_assign_rhs1 (stmt
);
1445 /* x = foo + n ==> foo */
1446 else if (code
== POINTER_PLUS_EXPR
)
1447 x
= gimple_assign_rhs1 (stmt
);
1448 /* x = (cast*) foo ==> foo */
1449 else if (code
== VIEW_CONVERT_EXPR
|| CONVERT_EXPR_CODE_P (code
))
1450 x
= gimple_assign_rhs1 (stmt
);
1451 /* x = c ? op1 : op2 == > op1 or op2 just like a PHI */
1452 else if (code
== COND_EXPR
)
1454 tree op1
= gimple_assign_rhs2 (stmt
);
1455 tree op2
= gimple_assign_rhs3 (stmt
);
1456 enum thread_memory_type mem
;
1457 retval
= thread_private_new_memory (entry_block
, op1
);
1458 if (retval
== mem_non_local
)
1459 goto new_memory_ret
;
1460 mem
= thread_private_new_memory (entry_block
, op2
);
1461 retval
= MIN (retval
, mem
);
1462 goto new_memory_ret
;
1466 retval
= mem_non_local
;
1467 goto new_memory_ret
;
1472 if (gimple_code (stmt
) == GIMPLE_PHI
)
1475 enum thread_memory_type mem
;
1476 tree phi_result
= gimple_phi_result (stmt
);
1478 /* If any of the ancestors are non-local, we are sure to
1479 be non-local. Otherwise we can avoid doing anything
1480 and inherit what has already been generated. */
1482 for (i
= 0; i
< gimple_phi_num_args (stmt
); ++i
)
1484 tree op
= PHI_ARG_DEF (stmt
, i
);
1486 /* Exclude self-assignment. */
1487 if (phi_result
== op
)
1490 mem
= thread_private_new_memory (entry_block
, op
);
1491 if (mem
== mem_non_local
)
1494 goto new_memory_ret
;
1496 retval
= MIN (retval
, mem
);
1498 goto new_memory_ret
;
1503 while (TREE_CODE (x
) == SSA_NAME
);
1505 if (stmt
&& is_gimple_call (stmt
) && gimple_call_flags (stmt
) & ECF_MALLOC
)
1506 /* Thread-local or transaction-local. */
1509 retval
= mem_non_local
;
1512 elt_p
->local_new_memory
= retval
;
1516 /* Determine whether X has to be instrumented using a read
1519 ENTRY_BLOCK is the entry block for the region where stmt resides
1520 in. NULL if unknown.
1522 STMT is the statement in which X occurs in. It is used for thread
1523 private memory instrumentation. If no TPM instrumentation is
1524 desired, STMT should be null. */
1526 requires_barrier (basic_block entry_block
, tree x
, gimple stmt
)
1529 while (handled_component_p (x
))
1530 x
= TREE_OPERAND (x
, 0);
1532 switch (TREE_CODE (x
))
1537 enum thread_memory_type ret
;
1539 ret
= thread_private_new_memory (entry_block
, TREE_OPERAND (x
, 0));
1540 if (ret
== mem_non_local
)
1542 if (stmt
&& ret
== mem_thread_local
)
1543 /* ?? Should we pass `orig', or the INDIRECT_REF X. ?? */
1544 tm_log_add (entry_block
, orig
, stmt
);
1546 /* Transaction-locals require nothing at all. For malloc, a
1547 transaction restart frees the memory and we reallocate.
1548 For alloca, the stack pointer gets reset by the retry and
1553 case TARGET_MEM_REF
:
1554 if (TREE_CODE (TMR_BASE (x
)) != ADDR_EXPR
)
1556 x
= TREE_OPERAND (TMR_BASE (x
), 0);
1557 if (TREE_CODE (x
) == PARM_DECL
)
1559 gcc_assert (TREE_CODE (x
) == VAR_DECL
);
1565 if (DECL_BY_REFERENCE (x
))
1567 /* ??? This value is a pointer, but aggregate_value_p has been
1568 jigged to return true which confuses needs_to_live_in_memory.
1569 This ought to be cleaned up generically.
1571 FIXME: Verify this still happens after the next mainline
1572 merge. Testcase ie g++.dg/tm/pr47554.C.
1577 if (is_global_var (x
))
1578 return !TREE_READONLY (x
);
1579 if (/* FIXME: This condition should actually go below in the
1580 tm_log_add() call, however is_call_clobbered() depends on
1581 aliasing info which is not available during
1582 gimplification. Since requires_barrier() gets called
1583 during lower_sequence_tm/gimplification, leave the call
1584 to needs_to_live_in_memory until we eliminate
1585 lower_sequence_tm altogether. */
1586 needs_to_live_in_memory (x
))
1590 /* For local memory that doesn't escape (aka thread private
1591 memory), we can either save the value at the beginning of
1592 the transaction and restore on restart, or call a tm
1593 function to dynamically save and restore on restart
1596 tm_log_add (entry_block
, orig
, stmt
);
1605 /* Mark the GIMPLE_ASSIGN statement as appropriate for being inside
1606 a transaction region. */
1609 examine_assign_tm (unsigned *state
, gimple_stmt_iterator
*gsi
)
1611 gimple stmt
= gsi_stmt (*gsi
);
1613 if (requires_barrier (/*entry_block=*/NULL
, gimple_assign_rhs1 (stmt
), NULL
))
1614 *state
|= GTMA_HAVE_LOAD
;
1615 if (requires_barrier (/*entry_block=*/NULL
, gimple_assign_lhs (stmt
), NULL
))
1616 *state
|= GTMA_HAVE_STORE
;
1619 /* Mark a GIMPLE_CALL as appropriate for being inside a transaction. */
1622 examine_call_tm (unsigned *state
, gimple_stmt_iterator
*gsi
)
1624 gimple stmt
= gsi_stmt (*gsi
);
1627 if (is_tm_pure_call (stmt
))
1630 /* Check if this call is a transaction abort. */
1631 fn
= gimple_call_fndecl (stmt
);
1632 if (is_tm_abort (fn
))
1633 *state
|= GTMA_HAVE_ABORT
;
1635 /* Note that something may happen. */
1636 *state
|= GTMA_HAVE_LOAD
| GTMA_HAVE_STORE
;
1639 /* Lower a GIMPLE_TRANSACTION statement. */
1642 lower_transaction (gimple_stmt_iterator
*gsi
, struct walk_stmt_info
*wi
)
1645 gtransaction
*stmt
= as_a
<gtransaction
*> (gsi_stmt (*gsi
));
1646 unsigned int *outer_state
= (unsigned int *) wi
->info
;
1647 unsigned int this_state
= 0;
1648 struct walk_stmt_info this_wi
;
1650 /* First, lower the body. The scanning that we do inside gives
1651 us some idea of what we're dealing with. */
1652 memset (&this_wi
, 0, sizeof (this_wi
));
1653 this_wi
.info
= (void *) &this_state
;
1654 walk_gimple_seq_mod (gimple_transaction_body_ptr (stmt
),
1655 lower_sequence_tm
, NULL
, &this_wi
);
1657 /* If there was absolutely nothing transaction related inside the
1658 transaction, we may elide it. Likewise if this is a nested
1659 transaction and does not contain an abort. */
1661 || (!(this_state
& GTMA_HAVE_ABORT
) && outer_state
!= NULL
))
1664 *outer_state
|= this_state
;
1666 gsi_insert_seq_before (gsi
, gimple_transaction_body (stmt
),
1668 gimple_transaction_set_body (stmt
, NULL
);
1670 gsi_remove (gsi
, true);
1671 wi
->removed_stmt
= true;
1675 /* Wrap the body of the transaction in a try-finally node so that
1676 the commit call is always properly called. */
1677 g
= gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_COMMIT
), 0);
1678 if (flag_exceptions
)
1681 gimple_seq n_seq
, e_seq
;
1683 n_seq
= gimple_seq_alloc_with_stmt (g
);
1686 g
= gimple_build_call (builtin_decl_explicit (BUILT_IN_EH_POINTER
),
1687 1, integer_zero_node
);
1688 ptr
= create_tmp_var (ptr_type_node
);
1689 gimple_call_set_lhs (g
, ptr
);
1690 gimple_seq_add_stmt (&e_seq
, g
);
1692 g
= gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_COMMIT_EH
),
1694 gimple_seq_add_stmt (&e_seq
, g
);
1696 g
= gimple_build_eh_else (n_seq
, e_seq
);
1699 g
= gimple_build_try (gimple_transaction_body (stmt
),
1700 gimple_seq_alloc_with_stmt (g
), GIMPLE_TRY_FINALLY
);
1701 gsi_insert_after (gsi
, g
, GSI_CONTINUE_LINKING
);
1703 gimple_transaction_set_body (stmt
, NULL
);
1705 /* If the transaction calls abort or if this is an outer transaction,
1706 add an "over" label afterwards. */
1707 if ((this_state
& (GTMA_HAVE_ABORT
))
1708 || (gimple_transaction_subcode (stmt
) & GTMA_IS_OUTER
))
1710 tree label
= create_artificial_label (UNKNOWN_LOCATION
);
1711 gimple_transaction_set_label (stmt
, label
);
1712 gsi_insert_after (gsi
, gimple_build_label (label
), GSI_CONTINUE_LINKING
);
1715 /* Record the set of operations found for use later. */
1716 this_state
|= gimple_transaction_subcode (stmt
) & GTMA_DECLARATION_MASK
;
1717 gimple_transaction_set_subcode (stmt
, this_state
);
1720 /* Iterate through the statements in the sequence, lowering them all
1721 as appropriate for being in a transaction. */
1724 lower_sequence_tm (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1725 struct walk_stmt_info
*wi
)
1727 unsigned int *state
= (unsigned int *) wi
->info
;
1728 gimple stmt
= gsi_stmt (*gsi
);
1730 *handled_ops_p
= true;
1731 switch (gimple_code (stmt
))
1734 /* Only memory reads/writes need to be instrumented. */
1735 if (gimple_assign_single_p (stmt
))
1736 examine_assign_tm (state
, gsi
);
1740 examine_call_tm (state
, gsi
);
1744 *state
|= GTMA_MAY_ENTER_IRREVOCABLE
;
1747 case GIMPLE_TRANSACTION
:
1748 lower_transaction (gsi
, wi
);
1752 *handled_ops_p
= !gimple_has_substatements (stmt
);
1759 /* Iterate through the statements in the sequence, lowering them all
1760 as appropriate for being outside of a transaction. */
1763 lower_sequence_no_tm (gimple_stmt_iterator
*gsi
, bool *handled_ops_p
,
1764 struct walk_stmt_info
* wi
)
1766 gimple stmt
= gsi_stmt (*gsi
);
1768 if (gimple_code (stmt
) == GIMPLE_TRANSACTION
)
1770 *handled_ops_p
= true;
1771 lower_transaction (gsi
, wi
);
1774 *handled_ops_p
= !gimple_has_substatements (stmt
);
1779 /* Main entry point for flattening GIMPLE_TRANSACTION constructs. After
1780 this, GIMPLE_TRANSACTION nodes still exist, but the nested body has
1781 been moved out, and all the data required for constructing a proper
1782 CFG has been recorded. */
1785 execute_lower_tm (void)
1787 struct walk_stmt_info wi
;
1790 /* Transactional clones aren't created until a later pass. */
1791 gcc_assert (!decl_is_tm_clone (current_function_decl
));
1793 body
= gimple_body (current_function_decl
);
1794 memset (&wi
, 0, sizeof (wi
));
1795 walk_gimple_seq_mod (&body
, lower_sequence_no_tm
, NULL
, &wi
);
1796 gimple_set_body (current_function_decl
, body
);
1803 const pass_data pass_data_lower_tm
=
1805 GIMPLE_PASS
, /* type */
1806 "tmlower", /* name */
1807 OPTGROUP_NONE
, /* optinfo_flags */
1808 TV_TRANS_MEM
, /* tv_id */
1809 PROP_gimple_lcf
, /* properties_required */
1810 0, /* properties_provided */
1811 0, /* properties_destroyed */
1812 0, /* todo_flags_start */
1813 0, /* todo_flags_finish */
1816 class pass_lower_tm
: public gimple_opt_pass
1819 pass_lower_tm (gcc::context
*ctxt
)
1820 : gimple_opt_pass (pass_data_lower_tm
, ctxt
)
1823 /* opt_pass methods: */
1824 virtual bool gate (function
*) { return flag_tm
; }
1825 virtual unsigned int execute (function
*) { return execute_lower_tm (); }
1827 }; // class pass_lower_tm
1832 make_pass_lower_tm (gcc::context
*ctxt
)
1834 return new pass_lower_tm (ctxt
);
1837 /* Collect region information for each transaction. */
1843 /* The field "transaction_stmt" is initially a gtransaction *,
1844 but eventually gets lowered to a gcall *(to BUILT_IN_TM_START).
1846 Helper method to get it as a gtransaction *, with code-checking
1847 in a checked-build. */
1850 get_transaction_stmt () const
1852 return as_a
<gtransaction
*> (transaction_stmt
);
1857 /* Link to the next unnested transaction. */
1858 struct tm_region
*next
;
1860 /* Link to the next inner transaction. */
1861 struct tm_region
*inner
;
1863 /* Link to the next outer transaction. */
1864 struct tm_region
*outer
;
1866 /* The GIMPLE_TRANSACTION statement beginning this transaction.
1867 After TM_MARK, this gets replaced by a call to
1869 Hence this will be either a gtransaction *or a gcall *. */
1870 gimple transaction_stmt
;
1872 /* After TM_MARK expands the GIMPLE_TRANSACTION into a call to
1873 BUILT_IN_TM_START, this field is true if the transaction is an
1874 outer transaction. */
1875 bool original_transaction_was_outer
;
1877 /* Return value from BUILT_IN_TM_START. */
1880 /* The entry block to this region. This will always be the first
1881 block of the body of the transaction. */
1882 basic_block entry_block
;
1884 /* The first block after an expanded call to _ITM_beginTransaction. */
1885 basic_block restart_block
;
1887 /* The set of all blocks that end the region; NULL if only EXIT_BLOCK.
1888 These blocks are still a part of the region (i.e., the border is
1889 inclusive). Note that this set is only complete for paths in the CFG
1890 starting at ENTRY_BLOCK, and that there is no exit block recorded for
1891 the edge to the "over" label. */
1894 /* The set of all blocks that have an TM_IRREVOCABLE call. */
1898 typedef struct tm_region
*tm_region_p
;
1900 /* True if there are pending edge statements to be committed for the
1901 current function being scanned in the tmmark pass. */
1902 bool pending_edge_inserts_p
;
1904 static struct tm_region
*all_tm_regions
;
1905 static bitmap_obstack tm_obstack
;
1908 /* A subroutine of tm_region_init. Record the existence of the
1909 GIMPLE_TRANSACTION statement in a tree of tm_region elements. */
1911 static struct tm_region
*
1912 tm_region_init_0 (struct tm_region
*outer
, basic_block bb
,
1915 struct tm_region
*region
;
1917 region
= (struct tm_region
*)
1918 obstack_alloc (&tm_obstack
.obstack
, sizeof (struct tm_region
));
1922 region
->next
= outer
->inner
;
1923 outer
->inner
= region
;
1927 region
->next
= all_tm_regions
;
1928 all_tm_regions
= region
;
1930 region
->inner
= NULL
;
1931 region
->outer
= outer
;
1933 region
->transaction_stmt
= stmt
;
1934 region
->original_transaction_was_outer
= false;
1935 region
->tm_state
= NULL
;
1937 /* There are either one or two edges out of the block containing
1938 the GIMPLE_TRANSACTION, one to the actual region and one to the
1939 "over" label if the region contains an abort. The former will
1940 always be the one marked FALLTHRU. */
1941 region
->entry_block
= FALLTHRU_EDGE (bb
)->dest
;
1943 region
->exit_blocks
= BITMAP_ALLOC (&tm_obstack
);
1944 region
->irr_blocks
= BITMAP_ALLOC (&tm_obstack
);
1949 /* A subroutine of tm_region_init. Record all the exit and
1950 irrevocable blocks in BB into the region's exit_blocks and
1951 irr_blocks bitmaps. Returns the new region being scanned. */
1953 static struct tm_region
*
1954 tm_region_init_1 (struct tm_region
*region
, basic_block bb
)
1956 gimple_stmt_iterator gsi
;
1960 || (!region
->irr_blocks
&& !region
->exit_blocks
))
1963 /* Check to see if this is the end of a region by seeing if it
1964 contains a call to __builtin_tm_commit{,_eh}. Note that the
1965 outermost region for DECL_IS_TM_CLONE need not collect this. */
1966 for (gsi
= gsi_last_bb (bb
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
1969 if (gimple_code (g
) == GIMPLE_CALL
)
1971 tree fn
= gimple_call_fndecl (g
);
1972 if (fn
&& DECL_BUILT_IN_CLASS (fn
) == BUILT_IN_NORMAL
)
1974 if ((DECL_FUNCTION_CODE (fn
) == BUILT_IN_TM_COMMIT
1975 || DECL_FUNCTION_CODE (fn
) == BUILT_IN_TM_COMMIT_EH
)
1976 && region
->exit_blocks
)
1978 bitmap_set_bit (region
->exit_blocks
, bb
->index
);
1979 region
= region
->outer
;
1982 if (DECL_FUNCTION_CODE (fn
) == BUILT_IN_TM_IRREVOCABLE
)
1983 bitmap_set_bit (region
->irr_blocks
, bb
->index
);
1990 /* Collect all of the transaction regions within the current function
1991 and record them in ALL_TM_REGIONS. The REGION parameter may specify
1992 an "outermost" region for use by tm clones. */
1995 tm_region_init (struct tm_region
*region
)
2001 auto_vec
<basic_block
> queue
;
2002 bitmap visited_blocks
= BITMAP_ALLOC (NULL
);
2003 struct tm_region
*old_region
;
2004 auto_vec
<tm_region_p
> bb_regions
;
2006 all_tm_regions
= region
;
2007 bb
= single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
2009 /* We could store this information in bb->aux, but we may get called
2010 through get_all_tm_blocks() from another pass that may be already
2012 bb_regions
.safe_grow_cleared (last_basic_block_for_fn (cfun
));
2014 queue
.safe_push (bb
);
2015 bb_regions
[bb
->index
] = region
;
2019 region
= bb_regions
[bb
->index
];
2020 bb_regions
[bb
->index
] = NULL
;
2022 /* Record exit and irrevocable blocks. */
2023 region
= tm_region_init_1 (region
, bb
);
2025 /* Check for the last statement in the block beginning a new region. */
2027 old_region
= region
;
2029 if (gtransaction
*trans_stmt
= dyn_cast
<gtransaction
*> (g
))
2030 region
= tm_region_init_0 (region
, bb
, trans_stmt
);
2032 /* Process subsequent blocks. */
2033 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2034 if (!bitmap_bit_p (visited_blocks
, e
->dest
->index
))
2036 bitmap_set_bit (visited_blocks
, e
->dest
->index
);
2037 queue
.safe_push (e
->dest
);
2039 /* If the current block started a new region, make sure that only
2040 the entry block of the new region is associated with this region.
2041 Other successors are still part of the old region. */
2042 if (old_region
!= region
&& e
->dest
!= region
->entry_block
)
2043 bb_regions
[e
->dest
->index
] = old_region
;
2045 bb_regions
[e
->dest
->index
] = region
;
2048 while (!queue
.is_empty ());
2049 BITMAP_FREE (visited_blocks
);
2052 /* The "gate" function for all transactional memory expansion and optimization
2053 passes. We collect region information for each top-level transaction, and
2054 if we don't find any, we skip all of the TM passes. Each region will have
2055 all of the exit blocks recorded, and the originating statement. */
2063 calculate_dominance_info (CDI_DOMINATORS
);
2064 bitmap_obstack_initialize (&tm_obstack
);
2066 /* If the function is a TM_CLONE, then the entire function is the region. */
2067 if (decl_is_tm_clone (current_function_decl
))
2069 struct tm_region
*region
= (struct tm_region
*)
2070 obstack_alloc (&tm_obstack
.obstack
, sizeof (struct tm_region
));
2071 memset (region
, 0, sizeof (*region
));
2072 region
->entry_block
= single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
2073 /* For a clone, the entire function is the region. But even if
2074 we don't need to record any exit blocks, we may need to
2075 record irrevocable blocks. */
2076 region
->irr_blocks
= BITMAP_ALLOC (&tm_obstack
);
2078 tm_region_init (region
);
2082 tm_region_init (NULL
);
2084 /* If we didn't find any regions, cleanup and skip the whole tree
2085 of tm-related optimizations. */
2086 if (all_tm_regions
== NULL
)
2088 bitmap_obstack_release (&tm_obstack
);
2098 const pass_data pass_data_tm_init
=
2100 GIMPLE_PASS
, /* type */
2101 "*tminit", /* name */
2102 OPTGROUP_NONE
, /* optinfo_flags */
2103 TV_TRANS_MEM
, /* tv_id */
2104 ( PROP_ssa
| PROP_cfg
), /* properties_required */
2105 0, /* properties_provided */
2106 0, /* properties_destroyed */
2107 0, /* todo_flags_start */
2108 0, /* todo_flags_finish */
2111 class pass_tm_init
: public gimple_opt_pass
2114 pass_tm_init (gcc::context
*ctxt
)
2115 : gimple_opt_pass (pass_data_tm_init
, ctxt
)
2118 /* opt_pass methods: */
2119 virtual bool gate (function
*) { return gate_tm_init (); }
2121 }; // class pass_tm_init
2126 make_pass_tm_init (gcc::context
*ctxt
)
2128 return new pass_tm_init (ctxt
);
2131 /* Add FLAGS to the GIMPLE_TRANSACTION subcode for the transaction region
2132 represented by STATE. */
2135 transaction_subcode_ior (struct tm_region
*region
, unsigned flags
)
2137 if (region
&& region
->transaction_stmt
)
2139 gtransaction
*transaction_stmt
= region
->get_transaction_stmt ();
2140 flags
|= gimple_transaction_subcode (transaction_stmt
);
2141 gimple_transaction_set_subcode (transaction_stmt
, flags
);
2145 /* Construct a memory load in a transactional context. Return the
2146 gimple statement performing the load, or NULL if there is no
2147 TM_LOAD builtin of the appropriate size to do the load.
2149 LOC is the location to use for the new statement(s). */
2152 build_tm_load (location_t loc
, tree lhs
, tree rhs
, gimple_stmt_iterator
*gsi
)
2154 enum built_in_function code
= END_BUILTINS
;
2155 tree t
, type
= TREE_TYPE (rhs
), decl
;
2158 if (type
== float_type_node
)
2159 code
= BUILT_IN_TM_LOAD_FLOAT
;
2160 else if (type
== double_type_node
)
2161 code
= BUILT_IN_TM_LOAD_DOUBLE
;
2162 else if (type
== long_double_type_node
)
2163 code
= BUILT_IN_TM_LOAD_LDOUBLE
;
2164 else if (TYPE_SIZE_UNIT (type
) != NULL
2165 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type
)))
2167 switch (tree_to_uhwi (TYPE_SIZE_UNIT (type
)))
2170 code
= BUILT_IN_TM_LOAD_1
;
2173 code
= BUILT_IN_TM_LOAD_2
;
2176 code
= BUILT_IN_TM_LOAD_4
;
2179 code
= BUILT_IN_TM_LOAD_8
;
2184 if (code
== END_BUILTINS
)
2186 decl
= targetm
.vectorize
.builtin_tm_load (type
);
2191 decl
= builtin_decl_explicit (code
);
2193 t
= gimplify_addr (gsi
, rhs
);
2194 gcall
= gimple_build_call (decl
, 1, t
);
2195 gimple_set_location (gcall
, loc
);
2197 t
= TREE_TYPE (TREE_TYPE (decl
));
2198 if (useless_type_conversion_p (type
, t
))
2200 gimple_call_set_lhs (gcall
, lhs
);
2201 gsi_insert_before (gsi
, gcall
, GSI_SAME_STMT
);
2208 temp
= create_tmp_reg (t
);
2209 gimple_call_set_lhs (gcall
, temp
);
2210 gsi_insert_before (gsi
, gcall
, GSI_SAME_STMT
);
2212 t
= fold_build1 (VIEW_CONVERT_EXPR
, type
, temp
);
2213 g
= gimple_build_assign (lhs
, t
);
2214 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
2221 /* Similarly for storing TYPE in a transactional context. */
2224 build_tm_store (location_t loc
, tree lhs
, tree rhs
, gimple_stmt_iterator
*gsi
)
2226 enum built_in_function code
= END_BUILTINS
;
2227 tree t
, fn
, type
= TREE_TYPE (rhs
), simple_type
;
2230 if (type
== float_type_node
)
2231 code
= BUILT_IN_TM_STORE_FLOAT
;
2232 else if (type
== double_type_node
)
2233 code
= BUILT_IN_TM_STORE_DOUBLE
;
2234 else if (type
== long_double_type_node
)
2235 code
= BUILT_IN_TM_STORE_LDOUBLE
;
2236 else if (TYPE_SIZE_UNIT (type
) != NULL
2237 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type
)))
2239 switch (tree_to_uhwi (TYPE_SIZE_UNIT (type
)))
2242 code
= BUILT_IN_TM_STORE_1
;
2245 code
= BUILT_IN_TM_STORE_2
;
2248 code
= BUILT_IN_TM_STORE_4
;
2251 code
= BUILT_IN_TM_STORE_8
;
2256 if (code
== END_BUILTINS
)
2258 fn
= targetm
.vectorize
.builtin_tm_store (type
);
2263 fn
= builtin_decl_explicit (code
);
2265 simple_type
= TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn
))));
2267 if (TREE_CODE (rhs
) == CONSTRUCTOR
)
2269 /* Handle the easy initialization to zero. */
2270 if (!CONSTRUCTOR_ELTS (rhs
))
2271 rhs
= build_int_cst (simple_type
, 0);
2274 /* ...otherwise punt to the caller and probably use
2275 BUILT_IN_TM_MEMMOVE, because we can't wrap a
2276 VIEW_CONVERT_EXPR around a CONSTRUCTOR (below) and produce
2281 else if (!useless_type_conversion_p (simple_type
, type
))
2286 temp
= create_tmp_reg (simple_type
);
2287 t
= fold_build1 (VIEW_CONVERT_EXPR
, simple_type
, rhs
);
2288 g
= gimple_build_assign (temp
, t
);
2289 gimple_set_location (g
, loc
);
2290 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
2295 t
= gimplify_addr (gsi
, lhs
);
2296 gcall
= gimple_build_call (fn
, 2, t
, rhs
);
2297 gimple_set_location (gcall
, loc
);
2298 gsi_insert_before (gsi
, gcall
, GSI_SAME_STMT
);
2304 /* Expand an assignment statement into transactional builtins. */
2307 expand_assign_tm (struct tm_region
*region
, gimple_stmt_iterator
*gsi
)
2309 gimple stmt
= gsi_stmt (*gsi
);
2310 location_t loc
= gimple_location (stmt
);
2311 tree lhs
= gimple_assign_lhs (stmt
);
2312 tree rhs
= gimple_assign_rhs1 (stmt
);
2313 bool store_p
= requires_barrier (region
->entry_block
, lhs
, NULL
);
2314 bool load_p
= requires_barrier (region
->entry_block
, rhs
, NULL
);
2315 gimple gcall
= NULL
;
2317 if (!load_p
&& !store_p
)
2319 /* Add thread private addresses to log if applicable. */
2320 requires_barrier (region
->entry_block
, lhs
, stmt
);
2325 // Remove original load/store statement.
2326 gsi_remove (gsi
, true);
2328 if (load_p
&& !store_p
)
2330 transaction_subcode_ior (region
, GTMA_HAVE_LOAD
);
2331 gcall
= build_tm_load (loc
, lhs
, rhs
, gsi
);
2333 else if (store_p
&& !load_p
)
2335 transaction_subcode_ior (region
, GTMA_HAVE_STORE
);
2336 gcall
= build_tm_store (loc
, lhs
, rhs
, gsi
);
2340 tree lhs_addr
, rhs_addr
, tmp
;
2343 transaction_subcode_ior (region
, GTMA_HAVE_LOAD
);
2345 transaction_subcode_ior (region
, GTMA_HAVE_STORE
);
2347 /* ??? Figure out if there's any possible overlap between the LHS
2348 and the RHS and if not, use MEMCPY. */
2350 if (load_p
&& is_gimple_reg (lhs
))
2352 tmp
= create_tmp_var (TREE_TYPE (lhs
));
2353 lhs_addr
= build_fold_addr_expr (tmp
);
2358 lhs_addr
= gimplify_addr (gsi
, lhs
);
2360 rhs_addr
= gimplify_addr (gsi
, rhs
);
2361 gcall
= gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_MEMMOVE
),
2362 3, lhs_addr
, rhs_addr
,
2363 TYPE_SIZE_UNIT (TREE_TYPE (lhs
)));
2364 gimple_set_location (gcall
, loc
);
2365 gsi_insert_before (gsi
, gcall
, GSI_SAME_STMT
);
2369 gcall
= gimple_build_assign (lhs
, tmp
);
2370 gsi_insert_before (gsi
, gcall
, GSI_SAME_STMT
);
2374 /* Now that we have the load/store in its instrumented form, add
2375 thread private addresses to the log if applicable. */
2377 requires_barrier (region
->entry_block
, lhs
, gcall
);
2379 // The calls to build_tm_{store,load} above inserted the instrumented
2380 // call into the stream.
2381 // gsi_insert_before (gsi, gcall, GSI_SAME_STMT);
2385 /* Expand a call statement as appropriate for a transaction. That is,
2386 either verify that the call does not affect the transaction, or
2387 redirect the call to a clone that handles transactions, or change
2388 the transaction state to IRREVOCABLE. Return true if the call is
2389 one of the builtins that end a transaction. */
2392 expand_call_tm (struct tm_region
*region
,
2393 gimple_stmt_iterator
*gsi
)
2395 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
2396 tree lhs
= gimple_call_lhs (stmt
);
2398 struct cgraph_node
*node
;
2399 bool retval
= false;
2401 fn_decl
= gimple_call_fndecl (stmt
);
2403 if (fn_decl
== builtin_decl_explicit (BUILT_IN_TM_MEMCPY
)
2404 || fn_decl
== builtin_decl_explicit (BUILT_IN_TM_MEMMOVE
))
2405 transaction_subcode_ior (region
, GTMA_HAVE_STORE
| GTMA_HAVE_LOAD
);
2406 if (fn_decl
== builtin_decl_explicit (BUILT_IN_TM_MEMSET
))
2407 transaction_subcode_ior (region
, GTMA_HAVE_STORE
);
2409 if (is_tm_pure_call (stmt
))
2413 retval
= is_tm_ending_fndecl (fn_decl
);
2416 /* Assume all non-const/pure calls write to memory, except
2417 transaction ending builtins. */
2418 transaction_subcode_ior (region
, GTMA_HAVE_STORE
);
2421 /* For indirect calls, we already generated a call into the runtime. */
2424 tree fn
= gimple_call_fn (stmt
);
2426 /* We are guaranteed never to go irrevocable on a safe or pure
2427 call, and the pure call was handled above. */
2428 if (is_tm_safe (fn
))
2431 transaction_subcode_ior (region
, GTMA_MAY_ENTER_IRREVOCABLE
);
2436 node
= cgraph_node::get (fn_decl
);
2437 /* All calls should have cgraph here. */
2440 /* We can have a nodeless call here if some pass after IPA-tm
2441 added uninstrumented calls. For example, loop distribution
2442 can transform certain loop constructs into __builtin_mem*
2443 calls. In this case, see if we have a suitable TM
2444 replacement and fill in the gaps. */
2445 gcc_assert (DECL_BUILT_IN_CLASS (fn_decl
) == BUILT_IN_NORMAL
);
2446 enum built_in_function code
= DECL_FUNCTION_CODE (fn_decl
);
2447 gcc_assert (code
== BUILT_IN_MEMCPY
2448 || code
== BUILT_IN_MEMMOVE
2449 || code
== BUILT_IN_MEMSET
);
2451 tree repl
= find_tm_replacement_function (fn_decl
);
2454 gimple_call_set_fndecl (stmt
, repl
);
2456 node
= cgraph_node::create (repl
);
2457 node
->local
.tm_may_enter_irr
= false;
2458 return expand_call_tm (region
, gsi
);
2462 if (node
->local
.tm_may_enter_irr
)
2463 transaction_subcode_ior (region
, GTMA_MAY_ENTER_IRREVOCABLE
);
2465 if (is_tm_abort (fn_decl
))
2467 transaction_subcode_ior (region
, GTMA_HAVE_ABORT
);
2471 /* Instrument the store if needed.
2473 If the assignment happens inside the function call (return slot
2474 optimization), there is no instrumentation to be done, since
2475 the callee should have done the right thing. */
2476 if (lhs
&& requires_barrier (region
->entry_block
, lhs
, stmt
)
2477 && !gimple_call_return_slot_opt_p (stmt
))
2479 tree tmp
= create_tmp_reg (TREE_TYPE (lhs
));
2480 location_t loc
= gimple_location (stmt
);
2481 edge fallthru_edge
= NULL
;
2482 gassign
*assign_stmt
;
2484 /* Remember if the call was going to throw. */
2485 if (stmt_can_throw_internal (stmt
))
2489 basic_block bb
= gimple_bb (stmt
);
2491 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2492 if (e
->flags
& EDGE_FALLTHRU
)
2499 gimple_call_set_lhs (stmt
, tmp
);
2501 assign_stmt
= gimple_build_assign (lhs
, tmp
);
2502 gimple_set_location (assign_stmt
, loc
);
2504 /* We cannot throw in the middle of a BB. If the call was going
2505 to throw, place the instrumentation on the fallthru edge, so
2506 the call remains the last statement in the block. */
2509 gimple_seq fallthru_seq
= gimple_seq_alloc_with_stmt (assign_stmt
);
2510 gimple_stmt_iterator fallthru_gsi
= gsi_start (fallthru_seq
);
2511 expand_assign_tm (region
, &fallthru_gsi
);
2512 gsi_insert_seq_on_edge (fallthru_edge
, fallthru_seq
);
2513 pending_edge_inserts_p
= true;
2517 gsi_insert_after (gsi
, assign_stmt
, GSI_CONTINUE_LINKING
);
2518 expand_assign_tm (region
, gsi
);
2521 transaction_subcode_ior (region
, GTMA_HAVE_STORE
);
2528 /* Expand all statements in BB as appropriate for being inside
2532 expand_block_tm (struct tm_region
*region
, basic_block bb
)
2534 gimple_stmt_iterator gsi
;
2536 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); )
2538 gimple stmt
= gsi_stmt (gsi
);
2539 switch (gimple_code (stmt
))
2542 /* Only memory reads/writes need to be instrumented. */
2543 if (gimple_assign_single_p (stmt
)
2544 && !gimple_clobber_p (stmt
))
2546 expand_assign_tm (region
, &gsi
);
2552 if (expand_call_tm (region
, &gsi
))
2562 if (!gsi_end_p (gsi
))
2567 /* Return the list of basic-blocks in REGION.
2569 STOP_AT_IRREVOCABLE_P is true if caller is uninterested in blocks
2570 following a TM_IRREVOCABLE call.
2572 INCLUDE_UNINSTRUMENTED_P is TRUE if we should include the
2573 uninstrumented code path blocks in the list of basic blocks
2574 returned, false otherwise. */
2576 static vec
<basic_block
>
2577 get_tm_region_blocks (basic_block entry_block
,
2580 bitmap all_region_blocks
,
2581 bool stop_at_irrevocable_p
,
2582 bool include_uninstrumented_p
= true)
2584 vec
<basic_block
> bbs
= vNULL
;
2588 bitmap visited_blocks
= BITMAP_ALLOC (NULL
);
2591 bbs
.safe_push (entry_block
);
2592 bitmap_set_bit (visited_blocks
, entry_block
->index
);
2596 basic_block bb
= bbs
[i
++];
2599 bitmap_bit_p (exit_blocks
, bb
->index
))
2602 if (stop_at_irrevocable_p
2604 && bitmap_bit_p (irr_blocks
, bb
->index
))
2607 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2608 if ((include_uninstrumented_p
2609 || !(e
->flags
& EDGE_TM_UNINSTRUMENTED
))
2610 && !bitmap_bit_p (visited_blocks
, e
->dest
->index
))
2612 bitmap_set_bit (visited_blocks
, e
->dest
->index
);
2613 bbs
.safe_push (e
->dest
);
2616 while (i
< bbs
.length ());
2618 if (all_region_blocks
)
2619 bitmap_ior_into (all_region_blocks
, visited_blocks
);
2621 BITMAP_FREE (visited_blocks
);
2625 // Callback data for collect_bb2reg.
2628 vec
<tm_region_p
> *bb2reg
;
2629 bool include_uninstrumented_p
;
2632 // Callback for expand_regions, collect innermost region data for each bb.
2634 collect_bb2reg (struct tm_region
*region
, void *data
)
2636 struct bb2reg_stuff
*stuff
= (struct bb2reg_stuff
*)data
;
2637 vec
<tm_region_p
> *bb2reg
= stuff
->bb2reg
;
2638 vec
<basic_block
> queue
;
2642 queue
= get_tm_region_blocks (region
->entry_block
,
2643 region
->exit_blocks
,
2646 /*stop_at_irr_p=*/true,
2647 stuff
->include_uninstrumented_p
);
2649 // We expect expand_region to perform a post-order traversal of the region
2650 // tree. Therefore the last region seen for any bb is the innermost.
2651 FOR_EACH_VEC_ELT (queue
, i
, bb
)
2652 (*bb2reg
)[bb
->index
] = region
;
2658 // Returns a vector, indexed by BB->INDEX, of the innermost tm_region to
2659 // which a basic block belongs. Note that we only consider the instrumented
2660 // code paths for the region; the uninstrumented code paths are ignored if
2661 // INCLUDE_UNINSTRUMENTED_P is false.
2663 // ??? This data is very similar to the bb_regions array that is collected
2664 // during tm_region_init. Or, rather, this data is similar to what could
2665 // be used within tm_region_init. The actual computation in tm_region_init
2666 // begins and ends with bb_regions entirely full of NULL pointers, due to
2667 // the way in which pointers are swapped in and out of the array.
2669 // ??? Our callers expect that blocks are not shared between transactions.
2670 // When the optimizers get too smart, and blocks are shared, then during
2671 // the tm_mark phase we'll add log entries to only one of the two transactions,
2672 // and in the tm_edge phase we'll add edges to the CFG that create invalid
2673 // cycles. The symptom being SSA defs that do not dominate their uses.
2674 // Note that the optimizers were locally correct with their transformation,
2675 // as we have no info within the program that suggests that the blocks cannot
2678 // ??? There is currently a hack inside tree-ssa-pre.c to work around the
2679 // only known instance of this block sharing.
2681 static vec
<tm_region_p
>
2682 get_bb_regions_instrumented (bool traverse_clones
,
2683 bool include_uninstrumented_p
)
2685 unsigned n
= last_basic_block_for_fn (cfun
);
2686 struct bb2reg_stuff stuff
;
2687 vec
<tm_region_p
> ret
;
2690 ret
.safe_grow_cleared (n
);
2691 stuff
.bb2reg
= &ret
;
2692 stuff
.include_uninstrumented_p
= include_uninstrumented_p
;
2693 expand_regions (all_tm_regions
, collect_bb2reg
, &stuff
, traverse_clones
);
2698 /* Set the IN_TRANSACTION for all gimple statements that appear in a
2702 compute_transaction_bits (void)
2704 struct tm_region
*region
;
2705 vec
<basic_block
> queue
;
2709 /* ?? Perhaps we need to abstract gate_tm_init further, because we
2710 certainly don't need it to calculate CDI_DOMINATOR info. */
2713 FOR_EACH_BB_FN (bb
, cfun
)
2714 bb
->flags
&= ~BB_IN_TRANSACTION
;
2716 for (region
= all_tm_regions
; region
; region
= region
->next
)
2718 queue
= get_tm_region_blocks (region
->entry_block
,
2719 region
->exit_blocks
,
2722 /*stop_at_irr_p=*/true);
2723 for (i
= 0; queue
.iterate (i
, &bb
); ++i
)
2724 bb
->flags
|= BB_IN_TRANSACTION
;
2729 bitmap_obstack_release (&tm_obstack
);
2732 /* Replace the GIMPLE_TRANSACTION in this region with the corresponding
2733 call to BUILT_IN_TM_START. */
2736 expand_transaction (struct tm_region
*region
, void *data ATTRIBUTE_UNUSED
)
2738 tree tm_start
= builtin_decl_explicit (BUILT_IN_TM_START
);
2739 basic_block transaction_bb
= gimple_bb (region
->transaction_stmt
);
2740 tree tm_state
= region
->tm_state
;
2741 tree tm_state_type
= TREE_TYPE (tm_state
);
2742 edge abort_edge
= NULL
;
2743 edge inst_edge
= NULL
;
2744 edge uninst_edge
= NULL
;
2745 edge fallthru_edge
= NULL
;
2747 // Identify the various successors of the transaction start.
2751 FOR_EACH_EDGE (e
, i
, transaction_bb
->succs
)
2753 if (e
->flags
& EDGE_TM_ABORT
)
2755 else if (e
->flags
& EDGE_TM_UNINSTRUMENTED
)
2759 if (e
->flags
& EDGE_FALLTHRU
)
2764 /* ??? There are plenty of bits here we're not computing. */
2766 int subcode
= gimple_transaction_subcode (region
->get_transaction_stmt ());
2768 if (subcode
& GTMA_DOES_GO_IRREVOCABLE
)
2769 flags
|= PR_DOESGOIRREVOCABLE
;
2770 if ((subcode
& GTMA_MAY_ENTER_IRREVOCABLE
) == 0)
2771 flags
|= PR_HASNOIRREVOCABLE
;
2772 /* If the transaction does not have an abort in lexical scope and is not
2773 marked as an outer transaction, then it will never abort. */
2774 if ((subcode
& GTMA_HAVE_ABORT
) == 0 && (subcode
& GTMA_IS_OUTER
) == 0)
2775 flags
|= PR_HASNOABORT
;
2776 if ((subcode
& GTMA_HAVE_STORE
) == 0)
2777 flags
|= PR_READONLY
;
2778 if (inst_edge
&& !(subcode
& GTMA_HAS_NO_INSTRUMENTATION
))
2779 flags
|= PR_INSTRUMENTEDCODE
;
2781 flags
|= PR_UNINSTRUMENTEDCODE
;
2782 if (subcode
& GTMA_IS_OUTER
)
2783 region
->original_transaction_was_outer
= true;
2784 tree t
= build_int_cst (tm_state_type
, flags
);
2785 gcall
*call
= gimple_build_call (tm_start
, 1, t
);
2786 gimple_call_set_lhs (call
, tm_state
);
2787 gimple_set_location (call
, gimple_location (region
->transaction_stmt
));
2789 // Replace the GIMPLE_TRANSACTION with the call to BUILT_IN_TM_START.
2790 gimple_stmt_iterator gsi
= gsi_last_bb (transaction_bb
);
2791 gcc_assert (gsi_stmt (gsi
) == region
->transaction_stmt
);
2792 gsi_insert_before (&gsi
, call
, GSI_SAME_STMT
);
2793 gsi_remove (&gsi
, true);
2794 region
->transaction_stmt
= call
;
2797 // Generate log saves.
2798 if (!tm_log_save_addresses
.is_empty ())
2799 tm_log_emit_saves (region
->entry_block
, transaction_bb
);
2801 // In the beginning, we've no tests to perform on transaction restart.
2802 // Note that after this point, transaction_bb becomes the "most recent
2803 // block containing tests for the transaction".
2804 region
->restart_block
= region
->entry_block
;
2806 // Generate log restores.
2807 if (!tm_log_save_addresses
.is_empty ())
2809 basic_block test_bb
= create_empty_bb (transaction_bb
);
2810 basic_block code_bb
= create_empty_bb (test_bb
);
2811 basic_block join_bb
= create_empty_bb (code_bb
);
2812 add_bb_to_loop (test_bb
, transaction_bb
->loop_father
);
2813 add_bb_to_loop (code_bb
, transaction_bb
->loop_father
);
2814 add_bb_to_loop (join_bb
, transaction_bb
->loop_father
);
2815 if (region
->restart_block
== region
->entry_block
)
2816 region
->restart_block
= test_bb
;
2818 tree t1
= create_tmp_reg (tm_state_type
);
2819 tree t2
= build_int_cst (tm_state_type
, A_RESTORELIVEVARIABLES
);
2820 gimple stmt
= gimple_build_assign (t1
, BIT_AND_EXPR
, tm_state
, t2
);
2821 gimple_stmt_iterator gsi
= gsi_last_bb (test_bb
);
2822 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2824 t2
= build_int_cst (tm_state_type
, 0);
2825 stmt
= gimple_build_cond (NE_EXPR
, t1
, t2
, NULL
, NULL
);
2826 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2828 tm_log_emit_restores (region
->entry_block
, code_bb
);
2830 edge ei
= make_edge (transaction_bb
, test_bb
, EDGE_FALLTHRU
);
2831 edge et
= make_edge (test_bb
, code_bb
, EDGE_TRUE_VALUE
);
2832 edge ef
= make_edge (test_bb
, join_bb
, EDGE_FALSE_VALUE
);
2833 redirect_edge_pred (fallthru_edge
, join_bb
);
2835 join_bb
->frequency
= test_bb
->frequency
= transaction_bb
->frequency
;
2836 join_bb
->count
= test_bb
->count
= transaction_bb
->count
;
2838 ei
->probability
= PROB_ALWAYS
;
2839 et
->probability
= PROB_LIKELY
;
2840 ef
->probability
= PROB_UNLIKELY
;
2841 et
->count
= apply_probability (test_bb
->count
, et
->probability
);
2842 ef
->count
= apply_probability (test_bb
->count
, ef
->probability
);
2844 code_bb
->count
= et
->count
;
2845 code_bb
->frequency
= EDGE_FREQUENCY (et
);
2847 transaction_bb
= join_bb
;
2850 // If we have an ABORT edge, create a test to perform the abort.
2853 basic_block test_bb
= create_empty_bb (transaction_bb
);
2854 add_bb_to_loop (test_bb
, transaction_bb
->loop_father
);
2855 if (region
->restart_block
== region
->entry_block
)
2856 region
->restart_block
= test_bb
;
2858 tree t1
= create_tmp_reg (tm_state_type
);
2859 tree t2
= build_int_cst (tm_state_type
, A_ABORTTRANSACTION
);
2860 gimple stmt
= gimple_build_assign (t1
, BIT_AND_EXPR
, tm_state
, t2
);
2861 gimple_stmt_iterator gsi
= gsi_last_bb (test_bb
);
2862 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2864 t2
= build_int_cst (tm_state_type
, 0);
2865 stmt
= gimple_build_cond (NE_EXPR
, t1
, t2
, NULL
, NULL
);
2866 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2868 edge ei
= make_edge (transaction_bb
, test_bb
, EDGE_FALLTHRU
);
2869 test_bb
->frequency
= transaction_bb
->frequency
;
2870 test_bb
->count
= transaction_bb
->count
;
2871 ei
->probability
= PROB_ALWAYS
;
2873 // Not abort edge. If both are live, chose one at random as we'll
2874 // we'll be fixing that up below.
2875 redirect_edge_pred (fallthru_edge
, test_bb
);
2876 fallthru_edge
->flags
= EDGE_FALSE_VALUE
;
2877 fallthru_edge
->probability
= PROB_VERY_LIKELY
;
2878 fallthru_edge
->count
2879 = apply_probability (test_bb
->count
, fallthru_edge
->probability
);
2882 redirect_edge_pred (abort_edge
, test_bb
);
2883 abort_edge
->flags
= EDGE_TRUE_VALUE
;
2884 abort_edge
->probability
= PROB_VERY_UNLIKELY
;
2886 = apply_probability (test_bb
->count
, abort_edge
->probability
);
2888 transaction_bb
= test_bb
;
2891 // If we have both instrumented and uninstrumented code paths, select one.
2892 if (inst_edge
&& uninst_edge
)
2894 basic_block test_bb
= create_empty_bb (transaction_bb
);
2895 add_bb_to_loop (test_bb
, transaction_bb
->loop_father
);
2896 if (region
->restart_block
== region
->entry_block
)
2897 region
->restart_block
= test_bb
;
2899 tree t1
= create_tmp_reg (tm_state_type
);
2900 tree t2
= build_int_cst (tm_state_type
, A_RUNUNINSTRUMENTEDCODE
);
2902 gimple stmt
= gimple_build_assign (t1
, BIT_AND_EXPR
, tm_state
, t2
);
2903 gimple_stmt_iterator gsi
= gsi_last_bb (test_bb
);
2904 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2906 t2
= build_int_cst (tm_state_type
, 0);
2907 stmt
= gimple_build_cond (NE_EXPR
, t1
, t2
, NULL
, NULL
);
2908 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2910 // Create the edge into test_bb first, as we want to copy values
2911 // out of the fallthru edge.
2912 edge e
= make_edge (transaction_bb
, test_bb
, fallthru_edge
->flags
);
2913 e
->probability
= fallthru_edge
->probability
;
2914 test_bb
->count
= e
->count
= fallthru_edge
->count
;
2915 test_bb
->frequency
= EDGE_FREQUENCY (e
);
2917 // Now update the edges to the inst/uninist implementations.
2918 // For now assume that the paths are equally likely. When using HTM,
2919 // we'll try the uninst path first and fallback to inst path if htm
2920 // buffers are exceeded. Without HTM we start with the inst path and
2921 // use the uninst path when falling back to serial mode.
2922 redirect_edge_pred (inst_edge
, test_bb
);
2923 inst_edge
->flags
= EDGE_FALSE_VALUE
;
2924 inst_edge
->probability
= REG_BR_PROB_BASE
/ 2;
2926 = apply_probability (test_bb
->count
, inst_edge
->probability
);
2928 redirect_edge_pred (uninst_edge
, test_bb
);
2929 uninst_edge
->flags
= EDGE_TRUE_VALUE
;
2930 uninst_edge
->probability
= REG_BR_PROB_BASE
/ 2;
2932 = apply_probability (test_bb
->count
, uninst_edge
->probability
);
2935 // If we have no previous special cases, and we have PHIs at the beginning
2936 // of the atomic region, this means we have a loop at the beginning of the
2937 // atomic region that shares the first block. This can cause problems with
2938 // the transaction restart abnormal edges to be added in the tm_edges pass.
2939 // Solve this by adding a new empty block to receive the abnormal edges.
2940 if (region
->restart_block
== region
->entry_block
2941 && phi_nodes (region
->entry_block
))
2943 basic_block empty_bb
= create_empty_bb (transaction_bb
);
2944 region
->restart_block
= empty_bb
;
2945 add_bb_to_loop (empty_bb
, transaction_bb
->loop_father
);
2947 redirect_edge_pred (fallthru_edge
, empty_bb
);
2948 make_edge (transaction_bb
, empty_bb
, EDGE_FALLTHRU
);
2954 /* Generate the temporary to be used for the return value of
2955 BUILT_IN_TM_START. */
2958 generate_tm_state (struct tm_region
*region
, void *data ATTRIBUTE_UNUSED
)
2960 tree tm_start
= builtin_decl_explicit (BUILT_IN_TM_START
);
2962 create_tmp_reg (TREE_TYPE (TREE_TYPE (tm_start
)), "tm_state");
2964 // Reset the subcode, post optimizations. We'll fill this in
2965 // again as we process blocks.
2966 if (region
->exit_blocks
)
2968 gtransaction
*transaction_stmt
= region
->get_transaction_stmt ();
2969 unsigned int subcode
= gimple_transaction_subcode (transaction_stmt
);
2971 if (subcode
& GTMA_DOES_GO_IRREVOCABLE
)
2972 subcode
&= (GTMA_DECLARATION_MASK
| GTMA_DOES_GO_IRREVOCABLE
2973 | GTMA_MAY_ENTER_IRREVOCABLE
2974 | GTMA_HAS_NO_INSTRUMENTATION
);
2976 subcode
&= GTMA_DECLARATION_MASK
;
2977 gimple_transaction_set_subcode (transaction_stmt
, subcode
);
2983 // Propagate flags from inner transactions outwards.
2985 propagate_tm_flags_out (struct tm_region
*region
)
2989 propagate_tm_flags_out (region
->inner
);
2991 if (region
->outer
&& region
->outer
->transaction_stmt
)
2994 = gimple_transaction_subcode (region
->get_transaction_stmt ());
2995 s
&= (GTMA_HAVE_ABORT
| GTMA_HAVE_LOAD
| GTMA_HAVE_STORE
2996 | GTMA_MAY_ENTER_IRREVOCABLE
);
2997 s
|= gimple_transaction_subcode (region
->outer
->get_transaction_stmt ());
2998 gimple_transaction_set_subcode (region
->outer
->get_transaction_stmt (),
3002 propagate_tm_flags_out (region
->next
);
3005 /* Entry point to the MARK phase of TM expansion. Here we replace
3006 transactional memory statements with calls to builtins, and function
3007 calls with their transactional clones (if available). But we don't
3008 yet lower GIMPLE_TRANSACTION or add the transaction restart back-edges. */
3011 execute_tm_mark (void)
3013 pending_edge_inserts_p
= false;
3015 expand_regions (all_tm_regions
, generate_tm_state
, NULL
,
3016 /*traverse_clones=*/true);
3020 vec
<tm_region_p
> bb_regions
3021 = get_bb_regions_instrumented (/*traverse_clones=*/true,
3022 /*include_uninstrumented_p=*/false);
3023 struct tm_region
*r
;
3026 // Expand memory operations into calls into the runtime.
3027 // This collects log entries as well.
3028 FOR_EACH_VEC_ELT (bb_regions
, i
, r
)
3032 if (r
->transaction_stmt
)
3035 = gimple_transaction_subcode (r
->get_transaction_stmt ());
3037 /* If we're sure to go irrevocable, there won't be
3038 anything to expand, since the run-time will go
3039 irrevocable right away. */
3040 if (sub
& GTMA_DOES_GO_IRREVOCABLE
3041 && sub
& GTMA_MAY_ENTER_IRREVOCABLE
)
3044 expand_block_tm (r
, BASIC_BLOCK_FOR_FN (cfun
, i
));
3048 bb_regions
.release ();
3050 // Propagate flags from inner transactions outwards.
3051 propagate_tm_flags_out (all_tm_regions
);
3053 // Expand GIMPLE_TRANSACTIONs into calls into the runtime.
3054 expand_regions (all_tm_regions
, expand_transaction
, NULL
,
3055 /*traverse_clones=*/false);
3060 if (pending_edge_inserts_p
)
3061 gsi_commit_edge_inserts ();
3062 free_dominance_info (CDI_DOMINATORS
);
3068 const pass_data pass_data_tm_mark
=
3070 GIMPLE_PASS
, /* type */
3071 "tmmark", /* name */
3072 OPTGROUP_NONE
, /* optinfo_flags */
3073 TV_TRANS_MEM
, /* tv_id */
3074 ( PROP_ssa
| PROP_cfg
), /* properties_required */
3075 0, /* properties_provided */
3076 0, /* properties_destroyed */
3077 0, /* todo_flags_start */
3078 TODO_update_ssa
, /* todo_flags_finish */
3081 class pass_tm_mark
: public gimple_opt_pass
3084 pass_tm_mark (gcc::context
*ctxt
)
3085 : gimple_opt_pass (pass_data_tm_mark
, ctxt
)
3088 /* opt_pass methods: */
3089 virtual unsigned int execute (function
*) { return execute_tm_mark (); }
3091 }; // class pass_tm_mark
3096 make_pass_tm_mark (gcc::context
*ctxt
)
3098 return new pass_tm_mark (ctxt
);
3102 /* Create an abnormal edge from STMT at iter, splitting the block
3103 as necessary. Adjust *PNEXT as needed for the split block. */
3106 split_bb_make_tm_edge (gimple stmt
, basic_block dest_bb
,
3107 gimple_stmt_iterator iter
, gimple_stmt_iterator
*pnext
)
3109 basic_block bb
= gimple_bb (stmt
);
3110 if (!gsi_one_before_end_p (iter
))
3112 edge e
= split_block (bb
, stmt
);
3113 *pnext
= gsi_start_bb (e
->dest
);
3115 make_edge (bb
, dest_bb
, EDGE_ABNORMAL
);
3117 // Record the need for the edge for the benefit of the rtl passes.
3118 if (cfun
->gimple_df
->tm_restart
== NULL
)
3119 cfun
->gimple_df
->tm_restart
3120 = hash_table
<tm_restart_hasher
>::create_ggc (31);
3122 struct tm_restart_node dummy
;
3124 dummy
.label_or_list
= gimple_block_label (dest_bb
);
3126 tm_restart_node
**slot
= cfun
->gimple_df
->tm_restart
->find_slot (&dummy
,
3128 struct tm_restart_node
*n
= *slot
;
3131 n
= ggc_alloc
<tm_restart_node
> ();
3136 tree old
= n
->label_or_list
;
3137 if (TREE_CODE (old
) == LABEL_DECL
)
3138 old
= tree_cons (NULL
, old
, NULL
);
3139 n
->label_or_list
= tree_cons (NULL
, dummy
.label_or_list
, old
);
3143 /* Split block BB as necessary for every builtin function we added, and
3144 wire up the abnormal back edges implied by the transaction restart. */
3147 expand_block_edges (struct tm_region
*const region
, basic_block bb
)
3149 gimple_stmt_iterator gsi
, next_gsi
;
3151 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi
= next_gsi
)
3153 gimple stmt
= gsi_stmt (gsi
);
3157 gsi_next (&next_gsi
);
3159 // ??? Shouldn't we split for any non-pure, non-irrevocable function?
3160 call_stmt
= dyn_cast
<gcall
*> (stmt
);
3162 || (gimple_call_flags (call_stmt
) & ECF_TM_BUILTIN
) == 0)
3165 if (DECL_FUNCTION_CODE (gimple_call_fndecl (call_stmt
))
3166 == BUILT_IN_TM_ABORT
)
3168 // If we have a ``_transaction_cancel [[outer]]'', there is only
3169 // one abnormal edge: to the transaction marked OUTER.
3170 // All compiler-generated instances of BUILT_IN_TM_ABORT have a
3171 // constant argument, which we can examine here. Users invoking
3172 // TM_ABORT directly get what they deserve.
3173 tree arg
= gimple_call_arg (call_stmt
, 0);
3174 if (TREE_CODE (arg
) == INTEGER_CST
3175 && (TREE_INT_CST_LOW (arg
) & AR_OUTERABORT
) != 0
3176 && !decl_is_tm_clone (current_function_decl
))
3178 // Find the GTMA_IS_OUTER transaction.
3179 for (struct tm_region
*o
= region
; o
; o
= o
->outer
)
3180 if (o
->original_transaction_was_outer
)
3182 split_bb_make_tm_edge (call_stmt
, o
->restart_block
,
3187 // Otherwise, the front-end should have semantically checked
3188 // outer aborts, but in either case the target region is not
3189 // within this function.
3193 // Non-outer, TM aborts have an abnormal edge to the inner-most
3194 // transaction, the one being aborted;
3195 split_bb_make_tm_edge (call_stmt
, region
->restart_block
, gsi
,
3199 // All TM builtins have an abnormal edge to the outer-most transaction.
3200 // We never restart inner transactions. For tm clones, we know a-priori
3201 // that the outer-most transaction is outside the function.
3202 if (decl_is_tm_clone (current_function_decl
))
3205 if (cfun
->gimple_df
->tm_restart
== NULL
)
3206 cfun
->gimple_df
->tm_restart
3207 = hash_table
<tm_restart_hasher
>::create_ggc (31);
3209 // All TM builtins have an abnormal edge to the outer-most transaction.
3210 // We never restart inner transactions.
3211 for (struct tm_region
*o
= region
; o
; o
= o
->outer
)
3214 split_bb_make_tm_edge (call_stmt
, o
->restart_block
, gsi
, &next_gsi
);
3218 // Delete any tail-call annotation that may have been added.
3219 // The tail-call pass may have mis-identified the commit as being
3220 // a candidate because we had not yet added this restart edge.
3221 gimple_call_set_tail (call_stmt
, false);
3225 /* Entry point to the final expansion of transactional nodes. */
3229 const pass_data pass_data_tm_edges
=
3231 GIMPLE_PASS
, /* type */
3232 "tmedge", /* name */
3233 OPTGROUP_NONE
, /* optinfo_flags */
3234 TV_TRANS_MEM
, /* tv_id */
3235 ( PROP_ssa
| PROP_cfg
), /* properties_required */
3236 0, /* properties_provided */
3237 0, /* properties_destroyed */
3238 0, /* todo_flags_start */
3239 TODO_update_ssa
, /* todo_flags_finish */
3242 class pass_tm_edges
: public gimple_opt_pass
3245 pass_tm_edges (gcc::context
*ctxt
)
3246 : gimple_opt_pass (pass_data_tm_edges
, ctxt
)
3249 /* opt_pass methods: */
3250 virtual unsigned int execute (function
*);
3252 }; // class pass_tm_edges
3255 pass_tm_edges::execute (function
*fun
)
3257 vec
<tm_region_p
> bb_regions
3258 = get_bb_regions_instrumented (/*traverse_clones=*/false,
3259 /*include_uninstrumented_p=*/true);
3260 struct tm_region
*r
;
3263 FOR_EACH_VEC_ELT (bb_regions
, i
, r
)
3265 expand_block_edges (r
, BASIC_BLOCK_FOR_FN (fun
, i
));
3267 bb_regions
.release ();
3269 /* We've got to release the dominance info now, to indicate that it
3270 must be rebuilt completely. Otherwise we'll crash trying to update
3271 the SSA web in the TODO section following this pass. */
3272 free_dominance_info (CDI_DOMINATORS
);
3273 bitmap_obstack_release (&tm_obstack
);
3274 all_tm_regions
= NULL
;
3282 make_pass_tm_edges (gcc::context
*ctxt
)
3284 return new pass_tm_edges (ctxt
);
3287 /* Helper function for expand_regions. Expand REGION and recurse to
3288 the inner region. Call CALLBACK on each region. CALLBACK returns
3289 NULL to continue the traversal, otherwise a non-null value which
3290 this function will return as well. TRAVERSE_CLONES is true if we
3291 should traverse transactional clones. */
3294 expand_regions_1 (struct tm_region
*region
,
3295 void *(*callback
)(struct tm_region
*, void *),
3297 bool traverse_clones
)
3299 void *retval
= NULL
;
3300 if (region
->exit_blocks
3301 || (traverse_clones
&& decl_is_tm_clone (current_function_decl
)))
3303 retval
= callback (region
, data
);
3309 retval
= expand_regions (region
->inner
, callback
, data
, traverse_clones
);
3316 /* Traverse the regions enclosed and including REGION. Execute
3317 CALLBACK for each region, passing DATA. CALLBACK returns NULL to
3318 continue the traversal, otherwise a non-null value which this
3319 function will return as well. TRAVERSE_CLONES is true if we should
3320 traverse transactional clones. */
3323 expand_regions (struct tm_region
*region
,
3324 void *(*callback
)(struct tm_region
*, void *),
3326 bool traverse_clones
)
3328 void *retval
= NULL
;
3331 retval
= expand_regions_1 (region
, callback
, data
, traverse_clones
);
3334 region
= region
->next
;
3340 /* A unique TM memory operation. */
3341 typedef struct tm_memop
3343 /* Unique ID that all memory operations to the same location have. */
3344 unsigned int value_id
;
3345 /* Address of load/store. */
3349 /* TM memory operation hashtable helpers. */
3351 struct tm_memop_hasher
: typed_free_remove
<tm_memop
>
3353 typedef tm_memop value_type
;
3354 typedef tm_memop compare_type
;
3355 static inline hashval_t
hash (const value_type
*);
3356 static inline bool equal (const value_type
*, const compare_type
*);
3359 /* Htab support. Return a hash value for a `tm_memop'. */
3361 tm_memop_hasher::hash (const value_type
*mem
)
3363 tree addr
= mem
->addr
;
3364 /* We drill down to the SSA_NAME/DECL for the hash, but equality is
3365 actually done with operand_equal_p (see tm_memop_eq). */
3366 if (TREE_CODE (addr
) == ADDR_EXPR
)
3367 addr
= TREE_OPERAND (addr
, 0);
3368 return iterative_hash_expr (addr
, 0);
3371 /* Htab support. Return true if two tm_memop's are the same. */
3373 tm_memop_hasher::equal (const value_type
*mem1
, const compare_type
*mem2
)
3375 return operand_equal_p (mem1
->addr
, mem2
->addr
, 0);
3378 /* Sets for solving data flow equations in the memory optimization pass. */
3379 struct tm_memopt_bitmaps
3381 /* Stores available to this BB upon entry. Basically, stores that
3382 dominate this BB. */
3383 bitmap store_avail_in
;
3384 /* Stores available at the end of this BB. */
3385 bitmap store_avail_out
;
3386 bitmap store_antic_in
;
3387 bitmap store_antic_out
;
3388 /* Reads available to this BB upon entry. Basically, reads that
3389 dominate this BB. */
3390 bitmap read_avail_in
;
3391 /* Reads available at the end of this BB. */
3392 bitmap read_avail_out
;
3393 /* Reads performed in this BB. */
3395 /* Writes performed in this BB. */
3398 /* Temporary storage for pass. */
3399 /* Is the current BB in the worklist? */
3400 bool avail_in_worklist_p
;
3401 /* Have we visited this BB? */
3405 static bitmap_obstack tm_memopt_obstack
;
3407 /* Unique counter for TM loads and stores. Loads and stores of the
3408 same address get the same ID. */
3409 static unsigned int tm_memopt_value_id
;
3410 static hash_table
<tm_memop_hasher
> *tm_memopt_value_numbers
;
3412 #define STORE_AVAIL_IN(BB) \
3413 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_avail_in
3414 #define STORE_AVAIL_OUT(BB) \
3415 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_avail_out
3416 #define STORE_ANTIC_IN(BB) \
3417 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_antic_in
3418 #define STORE_ANTIC_OUT(BB) \
3419 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_antic_out
3420 #define READ_AVAIL_IN(BB) \
3421 ((struct tm_memopt_bitmaps *) ((BB)->aux))->read_avail_in
3422 #define READ_AVAIL_OUT(BB) \
3423 ((struct tm_memopt_bitmaps *) ((BB)->aux))->read_avail_out
3424 #define READ_LOCAL(BB) \
3425 ((struct tm_memopt_bitmaps *) ((BB)->aux))->read_local
3426 #define STORE_LOCAL(BB) \
3427 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_local
3428 #define AVAIL_IN_WORKLIST_P(BB) \
3429 ((struct tm_memopt_bitmaps *) ((BB)->aux))->avail_in_worklist_p
3430 #define BB_VISITED_P(BB) \
3431 ((struct tm_memopt_bitmaps *) ((BB)->aux))->visited_p
3433 /* Given a TM load/store in STMT, return the value number for the address
3437 tm_memopt_value_number (gimple stmt
, enum insert_option op
)
3439 struct tm_memop tmpmem
, *mem
;
3442 gcc_assert (is_tm_load (stmt
) || is_tm_store (stmt
));
3443 tmpmem
.addr
= gimple_call_arg (stmt
, 0);
3444 slot
= tm_memopt_value_numbers
->find_slot (&tmpmem
, op
);
3447 else if (op
== INSERT
)
3449 mem
= XNEW (struct tm_memop
);
3451 mem
->value_id
= tm_memopt_value_id
++;
3452 mem
->addr
= tmpmem
.addr
;
3456 return mem
->value_id
;
3459 /* Accumulate TM memory operations in BB into STORE_LOCAL and READ_LOCAL. */
3462 tm_memopt_accumulate_memops (basic_block bb
)
3464 gimple_stmt_iterator gsi
;
3466 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3468 gimple stmt
= gsi_stmt (gsi
);
3472 if (is_tm_store (stmt
))
3473 bits
= STORE_LOCAL (bb
);
3474 else if (is_tm_load (stmt
))
3475 bits
= READ_LOCAL (bb
);
3479 loc
= tm_memopt_value_number (stmt
, INSERT
);
3480 bitmap_set_bit (bits
, loc
);
3483 fprintf (dump_file
, "TM memopt (%s): value num=%d, BB=%d, addr=",
3484 is_tm_load (stmt
) ? "LOAD" : "STORE", loc
,
3485 gimple_bb (stmt
)->index
);
3486 print_generic_expr (dump_file
, gimple_call_arg (stmt
, 0), 0);
3487 fprintf (dump_file
, "\n");
3492 /* Prettily dump one of the memopt sets. BITS is the bitmap to dump. */
3495 dump_tm_memopt_set (const char *set_name
, bitmap bits
)
3499 const char *comma
= "";
3501 fprintf (dump_file
, "TM memopt: %s: [", set_name
);
3502 EXECUTE_IF_SET_IN_BITMAP (bits
, 0, i
, bi
)
3504 hash_table
<tm_memop_hasher
>::iterator hi
;
3505 struct tm_memop
*mem
= NULL
;
3507 /* Yeah, yeah, yeah. Whatever. This is just for debugging. */
3508 FOR_EACH_HASH_TABLE_ELEMENT (*tm_memopt_value_numbers
, mem
, tm_memop_t
, hi
)
3509 if (mem
->value_id
== i
)
3511 gcc_assert (mem
->value_id
== i
);
3512 fprintf (dump_file
, "%s", comma
);
3514 print_generic_expr (dump_file
, mem
->addr
, 0);
3516 fprintf (dump_file
, "]\n");
3519 /* Prettily dump all of the memopt sets in BLOCKS. */
3522 dump_tm_memopt_sets (vec
<basic_block
> blocks
)
3527 for (i
= 0; blocks
.iterate (i
, &bb
); ++i
)
3529 fprintf (dump_file
, "------------BB %d---------\n", bb
->index
);
3530 dump_tm_memopt_set ("STORE_LOCAL", STORE_LOCAL (bb
));
3531 dump_tm_memopt_set ("READ_LOCAL", READ_LOCAL (bb
));
3532 dump_tm_memopt_set ("STORE_AVAIL_IN", STORE_AVAIL_IN (bb
));
3533 dump_tm_memopt_set ("STORE_AVAIL_OUT", STORE_AVAIL_OUT (bb
));
3534 dump_tm_memopt_set ("READ_AVAIL_IN", READ_AVAIL_IN (bb
));
3535 dump_tm_memopt_set ("READ_AVAIL_OUT", READ_AVAIL_OUT (bb
));
3539 /* Compute {STORE,READ}_AVAIL_IN for the basic block BB. */
3542 tm_memopt_compute_avin (basic_block bb
)
3547 /* Seed with the AVOUT of any predecessor. */
3548 for (ix
= 0; ix
< EDGE_COUNT (bb
->preds
); ix
++)
3550 e
= EDGE_PRED (bb
, ix
);
3551 /* Make sure we have already visited this BB, and is thus
3554 If e->src->aux is NULL, this predecessor is actually on an
3555 enclosing transaction. We only care about the current
3556 transaction, so ignore it. */
3557 if (e
->src
->aux
&& BB_VISITED_P (e
->src
))
3559 bitmap_copy (STORE_AVAIL_IN (bb
), STORE_AVAIL_OUT (e
->src
));
3560 bitmap_copy (READ_AVAIL_IN (bb
), READ_AVAIL_OUT (e
->src
));
3565 for (; ix
< EDGE_COUNT (bb
->preds
); ix
++)
3567 e
= EDGE_PRED (bb
, ix
);
3568 if (e
->src
->aux
&& BB_VISITED_P (e
->src
))
3570 bitmap_and_into (STORE_AVAIL_IN (bb
), STORE_AVAIL_OUT (e
->src
));
3571 bitmap_and_into (READ_AVAIL_IN (bb
), READ_AVAIL_OUT (e
->src
));
3575 BB_VISITED_P (bb
) = true;
3578 /* Compute the STORE_ANTIC_IN for the basic block BB. */
3581 tm_memopt_compute_antin (basic_block bb
)
3586 /* Seed with the ANTIC_OUT of any successor. */
3587 for (ix
= 0; ix
< EDGE_COUNT (bb
->succs
); ix
++)
3589 e
= EDGE_SUCC (bb
, ix
);
3590 /* Make sure we have already visited this BB, and is thus
3592 if (BB_VISITED_P (e
->dest
))
3594 bitmap_copy (STORE_ANTIC_IN (bb
), STORE_ANTIC_OUT (e
->dest
));
3599 for (; ix
< EDGE_COUNT (bb
->succs
); ix
++)
3601 e
= EDGE_SUCC (bb
, ix
);
3602 if (BB_VISITED_P (e
->dest
))
3603 bitmap_and_into (STORE_ANTIC_IN (bb
), STORE_ANTIC_OUT (e
->dest
));
3606 BB_VISITED_P (bb
) = true;
3609 /* Compute the AVAIL sets for every basic block in BLOCKS.
3611 We compute {STORE,READ}_AVAIL_{OUT,IN} as follows:
3613 AVAIL_OUT[bb] = union (AVAIL_IN[bb], LOCAL[bb])
3614 AVAIL_IN[bb] = intersect (AVAIL_OUT[predecessors])
3616 This is basically what we do in lcm's compute_available(), but here
3617 we calculate two sets of sets (one for STOREs and one for READs),
3618 and we work on a region instead of the entire CFG.
3620 REGION is the TM region.
3621 BLOCKS are the basic blocks in the region. */
3624 tm_memopt_compute_available (struct tm_region
*region
,
3625 vec
<basic_block
> blocks
)
3628 basic_block
*worklist
, *qin
, *qout
, *qend
, bb
;
3629 unsigned int qlen
, i
;
3633 /* Allocate a worklist array/queue. Entries are only added to the
3634 list if they were not already on the list. So the size is
3635 bounded by the number of basic blocks in the region. */
3636 qlen
= blocks
.length () - 1;
3637 qin
= qout
= worklist
=
3638 XNEWVEC (basic_block
, qlen
);
3640 /* Put every block in the region on the worklist. */
3641 for (i
= 0; blocks
.iterate (i
, &bb
); ++i
)
3643 /* Seed AVAIL_OUT with the LOCAL set. */
3644 bitmap_ior_into (STORE_AVAIL_OUT (bb
), STORE_LOCAL (bb
));
3645 bitmap_ior_into (READ_AVAIL_OUT (bb
), READ_LOCAL (bb
));
3647 AVAIL_IN_WORKLIST_P (bb
) = true;
3648 /* No need to insert the entry block, since it has an AVIN of
3649 null, and an AVOUT that has already been seeded in. */
3650 if (bb
!= region
->entry_block
)
3654 /* The entry block has been initialized with the local sets. */
3655 BB_VISITED_P (region
->entry_block
) = true;
3658 qend
= &worklist
[qlen
];
3660 /* Iterate until the worklist is empty. */
3663 /* Take the first entry off the worklist. */
3670 /* This block can be added to the worklist again if necessary. */
3671 AVAIL_IN_WORKLIST_P (bb
) = false;
3672 tm_memopt_compute_avin (bb
);
3674 /* Note: We do not add the LOCAL sets here because we already
3675 seeded the AVAIL_OUT sets with them. */
3676 changed
= bitmap_ior_into (STORE_AVAIL_OUT (bb
), STORE_AVAIL_IN (bb
));
3677 changed
|= bitmap_ior_into (READ_AVAIL_OUT (bb
), READ_AVAIL_IN (bb
));
3679 && (region
->exit_blocks
== NULL
3680 || !bitmap_bit_p (region
->exit_blocks
, bb
->index
)))
3681 /* If the out state of this block changed, then we need to add
3682 its successors to the worklist if they are not already in. */
3683 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3684 if (!AVAIL_IN_WORKLIST_P (e
->dest
)
3685 && e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
3688 AVAIL_IN_WORKLIST_P (e
->dest
) = true;
3699 dump_tm_memopt_sets (blocks
);
3702 /* Compute ANTIC sets for every basic block in BLOCKS.
3704 We compute STORE_ANTIC_OUT as follows:
3706 STORE_ANTIC_OUT[bb] = union(STORE_ANTIC_IN[bb], STORE_LOCAL[bb])
3707 STORE_ANTIC_IN[bb] = intersect(STORE_ANTIC_OUT[successors])
3709 REGION is the TM region.
3710 BLOCKS are the basic blocks in the region. */
3713 tm_memopt_compute_antic (struct tm_region
*region
,
3714 vec
<basic_block
> blocks
)
3717 basic_block
*worklist
, *qin
, *qout
, *qend
, bb
;
3722 /* Allocate a worklist array/queue. Entries are only added to the
3723 list if they were not already on the list. So the size is
3724 bounded by the number of basic blocks in the region. */
3725 qin
= qout
= worklist
= XNEWVEC (basic_block
, blocks
.length ());
3727 for (qlen
= 0, i
= blocks
.length () - 1; i
>= 0; --i
)
3731 /* Seed ANTIC_OUT with the LOCAL set. */
3732 bitmap_ior_into (STORE_ANTIC_OUT (bb
), STORE_LOCAL (bb
));
3734 /* Put every block in the region on the worklist. */
3735 AVAIL_IN_WORKLIST_P (bb
) = true;
3736 /* No need to insert exit blocks, since their ANTIC_IN is NULL,
3737 and their ANTIC_OUT has already been seeded in. */
3738 if (region
->exit_blocks
3739 && !bitmap_bit_p (region
->exit_blocks
, bb
->index
))
3746 /* The exit blocks have been initialized with the local sets. */
3747 if (region
->exit_blocks
)
3751 EXECUTE_IF_SET_IN_BITMAP (region
->exit_blocks
, 0, i
, bi
)
3752 BB_VISITED_P (BASIC_BLOCK_FOR_FN (cfun
, i
)) = true;
3756 qend
= &worklist
[qlen
];
3758 /* Iterate until the worklist is empty. */
3761 /* Take the first entry off the worklist. */
3768 /* This block can be added to the worklist again if necessary. */
3769 AVAIL_IN_WORKLIST_P (bb
) = false;
3770 tm_memopt_compute_antin (bb
);
3772 /* Note: We do not add the LOCAL sets here because we already
3773 seeded the ANTIC_OUT sets with them. */
3774 if (bitmap_ior_into (STORE_ANTIC_OUT (bb
), STORE_ANTIC_IN (bb
))
3775 && bb
!= region
->entry_block
)
3776 /* If the out state of this block changed, then we need to add
3777 its predecessors to the worklist if they are not already in. */
3778 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3779 if (!AVAIL_IN_WORKLIST_P (e
->src
))
3782 AVAIL_IN_WORKLIST_P (e
->src
) = true;
3793 dump_tm_memopt_sets (blocks
);
3796 /* Offsets of load variants from TM_LOAD. For example,
3797 BUILT_IN_TM_LOAD_RAR* is an offset of 1 from BUILT_IN_TM_LOAD*.
3798 See gtm-builtins.def. */
3799 #define TRANSFORM_RAR 1
3800 #define TRANSFORM_RAW 2
3801 #define TRANSFORM_RFW 3
3802 /* Offsets of store variants from TM_STORE. */
3803 #define TRANSFORM_WAR 1
3804 #define TRANSFORM_WAW 2
3806 /* Inform about a load/store optimization. */
3809 dump_tm_memopt_transform (gimple stmt
)
3813 fprintf (dump_file
, "TM memopt: transforming: ");
3814 print_gimple_stmt (dump_file
, stmt
, 0, 0);
3815 fprintf (dump_file
, "\n");
3819 /* Perform a read/write optimization. Replaces the TM builtin in STMT
3820 by a builtin that is OFFSET entries down in the builtins table in
3821 gtm-builtins.def. */
3824 tm_memopt_transform_stmt (unsigned int offset
,
3826 gimple_stmt_iterator
*gsi
)
3828 tree fn
= gimple_call_fn (stmt
);
3829 gcc_assert (TREE_CODE (fn
) == ADDR_EXPR
);
3830 TREE_OPERAND (fn
, 0)
3831 = builtin_decl_explicit ((enum built_in_function
)
3832 (DECL_FUNCTION_CODE (TREE_OPERAND (fn
, 0))
3834 gimple_call_set_fn (stmt
, fn
);
3835 gsi_replace (gsi
, stmt
, true);
3836 dump_tm_memopt_transform (stmt
);
3839 /* Perform the actual TM memory optimization transformations in the
3840 basic blocks in BLOCKS. */
3843 tm_memopt_transform_blocks (vec
<basic_block
> blocks
)
3847 gimple_stmt_iterator gsi
;
3849 for (i
= 0; blocks
.iterate (i
, &bb
); ++i
)
3851 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3853 gimple stmt
= gsi_stmt (gsi
);
3854 bitmap read_avail
= READ_AVAIL_IN (bb
);
3855 bitmap store_avail
= STORE_AVAIL_IN (bb
);
3856 bitmap store_antic
= STORE_ANTIC_OUT (bb
);
3859 if (is_tm_simple_load (stmt
))
3861 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
3862 loc
= tm_memopt_value_number (stmt
, NO_INSERT
);
3863 if (store_avail
&& bitmap_bit_p (store_avail
, loc
))
3864 tm_memopt_transform_stmt (TRANSFORM_RAW
, call_stmt
, &gsi
);
3865 else if (store_antic
&& bitmap_bit_p (store_antic
, loc
))
3867 tm_memopt_transform_stmt (TRANSFORM_RFW
, call_stmt
, &gsi
);
3868 bitmap_set_bit (store_avail
, loc
);
3870 else if (read_avail
&& bitmap_bit_p (read_avail
, loc
))
3871 tm_memopt_transform_stmt (TRANSFORM_RAR
, call_stmt
, &gsi
);
3873 bitmap_set_bit (read_avail
, loc
);
3875 else if (is_tm_simple_store (stmt
))
3877 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
3878 loc
= tm_memopt_value_number (stmt
, NO_INSERT
);
3879 if (store_avail
&& bitmap_bit_p (store_avail
, loc
))
3880 tm_memopt_transform_stmt (TRANSFORM_WAW
, call_stmt
, &gsi
);
3883 if (read_avail
&& bitmap_bit_p (read_avail
, loc
))
3884 tm_memopt_transform_stmt (TRANSFORM_WAR
, call_stmt
, &gsi
);
3885 bitmap_set_bit (store_avail
, loc
);
3892 /* Return a new set of bitmaps for a BB. */
3894 static struct tm_memopt_bitmaps
*
3895 tm_memopt_init_sets (void)
3897 struct tm_memopt_bitmaps
*b
3898 = XOBNEW (&tm_memopt_obstack
.obstack
, struct tm_memopt_bitmaps
);
3899 b
->store_avail_in
= BITMAP_ALLOC (&tm_memopt_obstack
);
3900 b
->store_avail_out
= BITMAP_ALLOC (&tm_memopt_obstack
);
3901 b
->store_antic_in
= BITMAP_ALLOC (&tm_memopt_obstack
);
3902 b
->store_antic_out
= BITMAP_ALLOC (&tm_memopt_obstack
);
3903 b
->store_avail_out
= BITMAP_ALLOC (&tm_memopt_obstack
);
3904 b
->read_avail_in
= BITMAP_ALLOC (&tm_memopt_obstack
);
3905 b
->read_avail_out
= BITMAP_ALLOC (&tm_memopt_obstack
);
3906 b
->read_local
= BITMAP_ALLOC (&tm_memopt_obstack
);
3907 b
->store_local
= BITMAP_ALLOC (&tm_memopt_obstack
);
3911 /* Free sets computed for each BB. */
3914 tm_memopt_free_sets (vec
<basic_block
> blocks
)
3919 for (i
= 0; blocks
.iterate (i
, &bb
); ++i
)
3923 /* Clear the visited bit for every basic block in BLOCKS. */
3926 tm_memopt_clear_visited (vec
<basic_block
> blocks
)
3931 for (i
= 0; blocks
.iterate (i
, &bb
); ++i
)
3932 BB_VISITED_P (bb
) = false;
3935 /* Replace TM load/stores with hints for the runtime. We handle
3936 things like read-after-write, write-after-read, read-after-read,
3937 read-for-write, etc. */
3940 execute_tm_memopt (void)
3942 struct tm_region
*region
;
3943 vec
<basic_block
> bbs
;
3945 tm_memopt_value_id
= 0;
3946 tm_memopt_value_numbers
= new hash_table
<tm_memop_hasher
> (10);
3948 for (region
= all_tm_regions
; region
; region
= region
->next
)
3950 /* All the TM stores/loads in the current region. */
3954 bitmap_obstack_initialize (&tm_memopt_obstack
);
3956 /* Save all BBs for the current region. */
3957 bbs
= get_tm_region_blocks (region
->entry_block
,
3958 region
->exit_blocks
,
3963 /* Collect all the memory operations. */
3964 for (i
= 0; bbs
.iterate (i
, &bb
); ++i
)
3966 bb
->aux
= tm_memopt_init_sets ();
3967 tm_memopt_accumulate_memops (bb
);
3970 /* Solve data flow equations and transform each block accordingly. */
3971 tm_memopt_clear_visited (bbs
);
3972 tm_memopt_compute_available (region
, bbs
);
3973 tm_memopt_clear_visited (bbs
);
3974 tm_memopt_compute_antic (region
, bbs
);
3975 tm_memopt_transform_blocks (bbs
);
3977 tm_memopt_free_sets (bbs
);
3979 bitmap_obstack_release (&tm_memopt_obstack
);
3980 tm_memopt_value_numbers
->empty ();
3983 delete tm_memopt_value_numbers
;
3984 tm_memopt_value_numbers
= NULL
;
3990 const pass_data pass_data_tm_memopt
=
3992 GIMPLE_PASS
, /* type */
3993 "tmmemopt", /* name */
3994 OPTGROUP_NONE
, /* optinfo_flags */
3995 TV_TRANS_MEM
, /* tv_id */
3996 ( PROP_ssa
| PROP_cfg
), /* properties_required */
3997 0, /* properties_provided */
3998 0, /* properties_destroyed */
3999 0, /* todo_flags_start */
4000 0, /* todo_flags_finish */
4003 class pass_tm_memopt
: public gimple_opt_pass
4006 pass_tm_memopt (gcc::context
*ctxt
)
4007 : gimple_opt_pass (pass_data_tm_memopt
, ctxt
)
4010 /* opt_pass methods: */
4011 virtual bool gate (function
*) { return flag_tm
&& optimize
> 0; }
4012 virtual unsigned int execute (function
*) { return execute_tm_memopt (); }
4014 }; // class pass_tm_memopt
4019 make_pass_tm_memopt (gcc::context
*ctxt
)
4021 return new pass_tm_memopt (ctxt
);
4025 /* Interprocedual analysis for the creation of transactional clones.
4026 The aim of this pass is to find which functions are referenced in
4027 a non-irrevocable transaction context, and for those over which
4028 we have control (or user directive), create a version of the
4029 function which uses only the transactional interface to reference
4030 protected memories. This analysis proceeds in several steps:
4032 (1) Collect the set of all possible transactional clones:
4034 (a) For all local public functions marked tm_callable, push
4035 it onto the tm_callee queue.
4037 (b) For all local functions, scan for calls in transaction blocks.
4038 Push the caller and callee onto the tm_caller and tm_callee
4039 queues. Count the number of callers for each callee.
4041 (c) For each local function on the callee list, assume we will
4042 create a transactional clone. Push *all* calls onto the
4043 callee queues; count the number of clone callers separately
4044 to the number of original callers.
4046 (2) Propagate irrevocable status up the dominator tree:
4048 (a) Any external function on the callee list that is not marked
4049 tm_callable is irrevocable. Push all callers of such onto
4052 (b) For each function on the worklist, mark each block that
4053 contains an irrevocable call. Use the AND operator to
4054 propagate that mark up the dominator tree.
4056 (c) If we reach the entry block for a possible transactional
4057 clone, then the transactional clone is irrevocable, and
4058 we should not create the clone after all. Push all
4059 callers onto the worklist.
4061 (d) Place tm_irrevocable calls at the beginning of the relevant
4062 blocks. Special case here is the entry block for the entire
4063 transaction region; there we mark it GTMA_DOES_GO_IRREVOCABLE for
4064 the library to begin the region in serial mode. Decrement
4065 the call count for all callees in the irrevocable region.
4067 (3) Create the transactional clones:
4069 Any tm_callee that still has a non-zero call count is cloned.
4072 /* This structure is stored in the AUX field of each cgraph_node. */
4073 struct tm_ipa_cg_data
4075 /* The clone of the function that got created. */
4076 struct cgraph_node
*clone
;
4078 /* The tm regions in the normal function. */
4079 struct tm_region
*all_tm_regions
;
4081 /* The blocks of the normal/clone functions that contain irrevocable
4082 calls, or blocks that are post-dominated by irrevocable calls. */
4083 bitmap irrevocable_blocks_normal
;
4084 bitmap irrevocable_blocks_clone
;
4086 /* The blocks of the normal function that are involved in transactions. */
4087 bitmap transaction_blocks_normal
;
4089 /* The number of callers to the transactional clone of this function
4090 from normal and transactional clones respectively. */
4091 unsigned tm_callers_normal
;
4092 unsigned tm_callers_clone
;
4094 /* True if all calls to this function's transactional clone
4095 are irrevocable. Also automatically true if the function
4096 has no transactional clone. */
4097 bool is_irrevocable
;
4099 /* Flags indicating the presence of this function in various queues. */
4100 bool in_callee_queue
;
4103 /* Flags indicating the kind of scan desired while in the worklist. */
4104 bool want_irr_scan_normal
;
4107 typedef vec
<cgraph_node
*> cgraph_node_queue
;
4109 /* Return the ipa data associated with NODE, allocating zeroed memory
4110 if necessary. TRAVERSE_ALIASES is true if we must traverse aliases
4111 and set *NODE accordingly. */
4113 static struct tm_ipa_cg_data
*
4114 get_cg_data (struct cgraph_node
**node
, bool traverse_aliases
)
4116 struct tm_ipa_cg_data
*d
;
4118 if (traverse_aliases
&& (*node
)->alias
)
4119 *node
= (*node
)->get_alias_target ();
4121 d
= (struct tm_ipa_cg_data
*) (*node
)->aux
;
4125 d
= (struct tm_ipa_cg_data
*)
4126 obstack_alloc (&tm_obstack
.obstack
, sizeof (*d
));
4127 (*node
)->aux
= (void *) d
;
4128 memset (d
, 0, sizeof (*d
));
4134 /* Add NODE to the end of QUEUE, unless IN_QUEUE_P indicates that
4135 it is already present. */
4138 maybe_push_queue (struct cgraph_node
*node
,
4139 cgraph_node_queue
*queue_p
, bool *in_queue_p
)
4144 queue_p
->safe_push (node
);
4148 /* Duplicate the basic blocks in QUEUE for use in the uninstrumented
4149 code path. QUEUE are the basic blocks inside the transaction
4150 represented in REGION.
4152 Later in split_code_paths() we will add the conditional to choose
4153 between the two alternatives. */
4156 ipa_uninstrument_transaction (struct tm_region
*region
,
4157 vec
<basic_block
> queue
)
4159 gimple transaction
= region
->transaction_stmt
;
4160 basic_block transaction_bb
= gimple_bb (transaction
);
4161 int n
= queue
.length ();
4162 basic_block
*new_bbs
= XNEWVEC (basic_block
, n
);
4164 copy_bbs (queue
.address (), n
, new_bbs
, NULL
, 0, NULL
, NULL
, transaction_bb
,
4166 edge e
= make_edge (transaction_bb
, new_bbs
[0], EDGE_TM_UNINSTRUMENTED
);
4167 add_phi_args_after_copy (new_bbs
, n
, e
);
4169 // Now we will have a GIMPLE_ATOMIC with 3 possible edges out of it.
4170 // a) EDGE_FALLTHRU into the transaction
4171 // b) EDGE_TM_ABORT out of the transaction
4172 // c) EDGE_TM_UNINSTRUMENTED into the uninstrumented blocks.
4177 /* A subroutine of ipa_tm_scan_calls_transaction and ipa_tm_scan_calls_clone.
4178 Queue all callees within block BB. */
4181 ipa_tm_scan_calls_block (cgraph_node_queue
*callees_p
,
4182 basic_block bb
, bool for_clone
)
4184 gimple_stmt_iterator gsi
;
4186 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4188 gimple stmt
= gsi_stmt (gsi
);
4189 if (is_gimple_call (stmt
) && !is_tm_pure_call (stmt
))
4191 tree fndecl
= gimple_call_fndecl (stmt
);
4194 struct tm_ipa_cg_data
*d
;
4196 struct cgraph_node
*node
;
4198 if (is_tm_ending_fndecl (fndecl
))
4200 if (find_tm_replacement_function (fndecl
))
4203 node
= cgraph_node::get (fndecl
);
4204 gcc_assert (node
!= NULL
);
4205 d
= get_cg_data (&node
, true);
4207 pcallers
= (for_clone
? &d
->tm_callers_clone
4208 : &d
->tm_callers_normal
);
4211 maybe_push_queue (node
, callees_p
, &d
->in_callee_queue
);
4217 /* Scan all calls in NODE that are within a transaction region,
4218 and push the resulting nodes into the callee queue. */
4221 ipa_tm_scan_calls_transaction (struct tm_ipa_cg_data
*d
,
4222 cgraph_node_queue
*callees_p
)
4224 struct tm_region
*r
;
4226 d
->transaction_blocks_normal
= BITMAP_ALLOC (&tm_obstack
);
4227 d
->all_tm_regions
= all_tm_regions
;
4229 for (r
= all_tm_regions
; r
; r
= r
->next
)
4231 vec
<basic_block
> bbs
;
4235 bbs
= get_tm_region_blocks (r
->entry_block
, r
->exit_blocks
, NULL
,
4236 d
->transaction_blocks_normal
, false);
4238 // Generate the uninstrumented code path for this transaction.
4239 ipa_uninstrument_transaction (r
, bbs
);
4241 FOR_EACH_VEC_ELT (bbs
, i
, bb
)
4242 ipa_tm_scan_calls_block (callees_p
, bb
, false);
4247 // ??? copy_bbs should maintain cgraph edges for the blocks as it is
4248 // copying them, rather than forcing us to do this externally.
4249 cgraph_edge::rebuild_edges ();
4251 // ??? In ipa_uninstrument_transaction we don't try to update dominators
4252 // because copy_bbs doesn't return a VEC like iterate_fix_dominators expects.
4253 // Instead, just release dominators here so update_ssa recomputes them.
4254 free_dominance_info (CDI_DOMINATORS
);
4256 // When building the uninstrumented code path, copy_bbs will have invoked
4257 // create_new_def_for starting an "ssa update context". There is only one
4258 // instance of this context, so resolve ssa updates before moving on to
4259 // the next function.
4260 update_ssa (TODO_update_ssa
);
4263 /* Scan all calls in NODE as if this is the transactional clone,
4264 and push the destinations into the callee queue. */
4267 ipa_tm_scan_calls_clone (struct cgraph_node
*node
,
4268 cgraph_node_queue
*callees_p
)
4270 struct function
*fn
= DECL_STRUCT_FUNCTION (node
->decl
);
4273 FOR_EACH_BB_FN (bb
, fn
)
4274 ipa_tm_scan_calls_block (callees_p
, bb
, true);
4277 /* The function NODE has been detected to be irrevocable. Push all
4278 of its callers onto WORKLIST for the purpose of re-scanning them. */
4281 ipa_tm_note_irrevocable (struct cgraph_node
*node
,
4282 cgraph_node_queue
*worklist_p
)
4284 struct tm_ipa_cg_data
*d
= get_cg_data (&node
, true);
4285 struct cgraph_edge
*e
;
4287 d
->is_irrevocable
= true;
4289 for (e
= node
->callers
; e
; e
= e
->next_caller
)
4292 struct cgraph_node
*caller
;
4294 /* Don't examine recursive calls. */
4295 if (e
->caller
== node
)
4297 /* Even if we think we can go irrevocable, believe the user
4299 if (is_tm_safe_or_pure (e
->caller
->decl
))
4303 d
= get_cg_data (&caller
, true);
4305 /* Check if the callee is in a transactional region. If so,
4306 schedule the function for normal re-scan as well. */
4307 bb
= gimple_bb (e
->call_stmt
);
4308 gcc_assert (bb
!= NULL
);
4309 if (d
->transaction_blocks_normal
4310 && bitmap_bit_p (d
->transaction_blocks_normal
, bb
->index
))
4311 d
->want_irr_scan_normal
= true;
4313 maybe_push_queue (caller
, worklist_p
, &d
->in_worklist
);
4317 /* A subroutine of ipa_tm_scan_irr_blocks; return true iff any statement
4318 within the block is irrevocable. */
4321 ipa_tm_scan_irr_block (basic_block bb
)
4323 gimple_stmt_iterator gsi
;
4326 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4328 gimple stmt
= gsi_stmt (gsi
);
4329 switch (gimple_code (stmt
))
4332 if (gimple_assign_single_p (stmt
))
4334 tree lhs
= gimple_assign_lhs (stmt
);
4335 tree rhs
= gimple_assign_rhs1 (stmt
);
4336 if (volatile_var_p (lhs
) || volatile_var_p (rhs
))
4343 tree lhs
= gimple_call_lhs (stmt
);
4344 if (lhs
&& volatile_var_p (lhs
))
4347 if (is_tm_pure_call (stmt
))
4350 fn
= gimple_call_fn (stmt
);
4352 /* Functions with the attribute are by definition irrevocable. */
4353 if (is_tm_irrevocable (fn
))
4356 /* For direct function calls, go ahead and check for replacement
4357 functions, or transitive irrevocable functions. For indirect
4358 functions, we'll ask the runtime. */
4359 if (TREE_CODE (fn
) == ADDR_EXPR
)
4361 struct tm_ipa_cg_data
*d
;
4362 struct cgraph_node
*node
;
4364 fn
= TREE_OPERAND (fn
, 0);
4365 if (is_tm_ending_fndecl (fn
))
4367 if (find_tm_replacement_function (fn
))
4370 node
= cgraph_node::get (fn
);
4371 d
= get_cg_data (&node
, true);
4373 /* Return true if irrevocable, but above all, believe
4375 if (d
->is_irrevocable
4376 && !is_tm_safe_or_pure (fn
))
4383 /* ??? The Approved Method of indicating that an inline
4384 assembly statement is not relevant to the transaction
4385 is to wrap it in a __tm_waiver block. This is not
4386 yet implemented, so we can't check for it. */
4387 if (is_tm_safe (current_function_decl
))
4389 tree t
= build1 (NOP_EXPR
, void_type_node
, size_zero_node
);
4390 SET_EXPR_LOCATION (t
, gimple_location (stmt
));
4391 error ("%Kasm not allowed in %<transaction_safe%> function", t
);
4403 /* For each of the blocks seeded witin PQUEUE, walk the CFG looking
4404 for new irrevocable blocks, marking them in NEW_IRR. Don't bother
4405 scanning past OLD_IRR or EXIT_BLOCKS. */
4408 ipa_tm_scan_irr_blocks (vec
<basic_block
> *pqueue
, bitmap new_irr
,
4409 bitmap old_irr
, bitmap exit_blocks
)
4411 bool any_new_irr
= false;
4414 bitmap visited_blocks
= BITMAP_ALLOC (NULL
);
4418 basic_block bb
= pqueue
->pop ();
4420 /* Don't re-scan blocks we know already are irrevocable. */
4421 if (old_irr
&& bitmap_bit_p (old_irr
, bb
->index
))
4424 if (ipa_tm_scan_irr_block (bb
))
4426 bitmap_set_bit (new_irr
, bb
->index
);
4429 else if (exit_blocks
== NULL
|| !bitmap_bit_p (exit_blocks
, bb
->index
))
4431 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4432 if (!bitmap_bit_p (visited_blocks
, e
->dest
->index
))
4434 bitmap_set_bit (visited_blocks
, e
->dest
->index
);
4435 pqueue
->safe_push (e
->dest
);
4439 while (!pqueue
->is_empty ());
4441 BITMAP_FREE (visited_blocks
);
4446 /* Propagate the irrevocable property both up and down the dominator tree.
4447 BB is the current block being scanned; EXIT_BLOCKS are the edges of the
4448 TM regions; OLD_IRR are the results of a previous scan of the dominator
4449 tree which has been fully propagated; NEW_IRR is the set of new blocks
4450 which are gaining the irrevocable property during the current scan. */
4453 ipa_tm_propagate_irr (basic_block entry_block
, bitmap new_irr
,
4454 bitmap old_irr
, bitmap exit_blocks
)
4456 vec
<basic_block
> bbs
;
4457 bitmap all_region_blocks
;
4459 /* If this block is in the old set, no need to rescan. */
4460 if (old_irr
&& bitmap_bit_p (old_irr
, entry_block
->index
))
4463 all_region_blocks
= BITMAP_ALLOC (&tm_obstack
);
4464 bbs
= get_tm_region_blocks (entry_block
, exit_blocks
, NULL
,
4465 all_region_blocks
, false);
4468 basic_block bb
= bbs
.pop ();
4469 bool this_irr
= bitmap_bit_p (new_irr
, bb
->index
);
4470 bool all_son_irr
= false;
4474 /* Propagate up. If my children are, I am too, but we must have
4475 at least one child that is. */
4478 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4480 if (!bitmap_bit_p (new_irr
, e
->dest
->index
))
4482 all_son_irr
= false;
4490 /* Add block to new_irr if it hasn't already been processed. */
4491 if (!old_irr
|| !bitmap_bit_p (old_irr
, bb
->index
))
4493 bitmap_set_bit (new_irr
, bb
->index
);
4499 /* Propagate down to everyone we immediately dominate. */
4503 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
4505 son
= next_dom_son (CDI_DOMINATORS
, son
))
4507 /* Make sure block is actually in a TM region, and it
4508 isn't already in old_irr. */
4509 if ((!old_irr
|| !bitmap_bit_p (old_irr
, son
->index
))
4510 && bitmap_bit_p (all_region_blocks
, son
->index
))
4511 bitmap_set_bit (new_irr
, son
->index
);
4515 while (!bbs
.is_empty ());
4517 BITMAP_FREE (all_region_blocks
);
4522 ipa_tm_decrement_clone_counts (basic_block bb
, bool for_clone
)
4524 gimple_stmt_iterator gsi
;
4526 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4528 gimple stmt
= gsi_stmt (gsi
);
4529 if (is_gimple_call (stmt
) && !is_tm_pure_call (stmt
))
4531 tree fndecl
= gimple_call_fndecl (stmt
);
4534 struct tm_ipa_cg_data
*d
;
4536 struct cgraph_node
*tnode
;
4538 if (is_tm_ending_fndecl (fndecl
))
4540 if (find_tm_replacement_function (fndecl
))
4543 tnode
= cgraph_node::get (fndecl
);
4544 d
= get_cg_data (&tnode
, true);
4546 pcallers
= (for_clone
? &d
->tm_callers_clone
4547 : &d
->tm_callers_normal
);
4549 gcc_assert (*pcallers
> 0);
4556 /* (Re-)Scan the transaction blocks in NODE for calls to irrevocable functions,
4557 as well as other irrevocable actions such as inline assembly. Mark all
4558 such blocks as irrevocable and decrement the number of calls to
4559 transactional clones. Return true if, for the transactional clone, the
4560 entire function is irrevocable. */
4563 ipa_tm_scan_irr_function (struct cgraph_node
*node
, bool for_clone
)
4565 struct tm_ipa_cg_data
*d
;
4566 bitmap new_irr
, old_irr
;
4569 /* Builtin operators (operator new, and such). */
4570 if (DECL_STRUCT_FUNCTION (node
->decl
) == NULL
4571 || DECL_STRUCT_FUNCTION (node
->decl
)->cfg
== NULL
)
4574 push_cfun (DECL_STRUCT_FUNCTION (node
->decl
));
4575 calculate_dominance_info (CDI_DOMINATORS
);
4577 d
= get_cg_data (&node
, true);
4578 auto_vec
<basic_block
, 10> queue
;
4579 new_irr
= BITMAP_ALLOC (&tm_obstack
);
4581 /* Scan each tm region, propagating irrevocable status through the tree. */
4584 old_irr
= d
->irrevocable_blocks_clone
;
4585 queue
.quick_push (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)));
4586 if (ipa_tm_scan_irr_blocks (&queue
, new_irr
, old_irr
, NULL
))
4588 ipa_tm_propagate_irr (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
4591 ret
= bitmap_bit_p (new_irr
,
4592 single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
))->index
);
4597 struct tm_region
*region
;
4599 old_irr
= d
->irrevocable_blocks_normal
;
4600 for (region
= d
->all_tm_regions
; region
; region
= region
->next
)
4602 queue
.quick_push (region
->entry_block
);
4603 if (ipa_tm_scan_irr_blocks (&queue
, new_irr
, old_irr
,
4604 region
->exit_blocks
))
4605 ipa_tm_propagate_irr (region
->entry_block
, new_irr
, old_irr
,
4606 region
->exit_blocks
);
4610 /* If we found any new irrevocable blocks, reduce the call count for
4611 transactional clones within the irrevocable blocks. Save the new
4612 set of irrevocable blocks for next time. */
4613 if (!bitmap_empty_p (new_irr
))
4615 bitmap_iterator bmi
;
4618 EXECUTE_IF_SET_IN_BITMAP (new_irr
, 0, i
, bmi
)
4619 ipa_tm_decrement_clone_counts (BASIC_BLOCK_FOR_FN (cfun
, i
),
4624 bitmap_ior_into (old_irr
, new_irr
);
4625 BITMAP_FREE (new_irr
);
4628 d
->irrevocable_blocks_clone
= new_irr
;
4630 d
->irrevocable_blocks_normal
= new_irr
;
4632 if (dump_file
&& new_irr
)
4635 bitmap_iterator bmi
;
4638 dname
= lang_hooks
.decl_printable_name (current_function_decl
, 2);
4639 EXECUTE_IF_SET_IN_BITMAP (new_irr
, 0, i
, bmi
)
4640 fprintf (dump_file
, "%s: bb %d goes irrevocable\n", dname
, i
);
4644 BITMAP_FREE (new_irr
);
4651 /* Return true if, for the transactional clone of NODE, any call
4652 may enter irrevocable mode. */
4655 ipa_tm_mayenterirr_function (struct cgraph_node
*node
)
4657 struct tm_ipa_cg_data
*d
;
4661 d
= get_cg_data (&node
, true);
4663 flags
= flags_from_decl_or_type (decl
);
4665 /* Handle some TM builtins. Ordinarily these aren't actually generated
4666 at this point, but handling these functions when written in by the
4667 user makes it easier to build unit tests. */
4668 if (flags
& ECF_TM_BUILTIN
)
4671 /* Filter out all functions that are marked. */
4672 if (flags
& ECF_TM_PURE
)
4674 if (is_tm_safe (decl
))
4676 if (is_tm_irrevocable (decl
))
4678 if (is_tm_callable (decl
))
4680 if (find_tm_replacement_function (decl
))
4683 /* If we aren't seeing the final version of the function we don't
4684 know what it will contain at runtime. */
4685 if (node
->get_availability () < AVAIL_AVAILABLE
)
4688 /* If the function must go irrevocable, then of course true. */
4689 if (d
->is_irrevocable
)
4692 /* If there are any blocks marked irrevocable, then the function
4693 as a whole may enter irrevocable. */
4694 if (d
->irrevocable_blocks_clone
)
4697 /* We may have previously marked this function as tm_may_enter_irr;
4698 see pass_diagnose_tm_blocks. */
4699 if (node
->local
.tm_may_enter_irr
)
4702 /* Recurse on the main body for aliases. In general, this will
4703 result in one of the bits above being set so that we will not
4704 have to recurse next time. */
4706 return ipa_tm_mayenterirr_function (cgraph_node::get (node
->thunk
.alias
));
4708 /* What remains is unmarked local functions without items that force
4709 the function to go irrevocable. */
4713 /* Diagnose calls from transaction_safe functions to unmarked
4714 functions that are determined to not be safe. */
4717 ipa_tm_diagnose_tm_safe (struct cgraph_node
*node
)
4719 struct cgraph_edge
*e
;
4721 for (e
= node
->callees
; e
; e
= e
->next_callee
)
4722 if (!is_tm_callable (e
->callee
->decl
)
4723 && e
->callee
->local
.tm_may_enter_irr
)
4724 error_at (gimple_location (e
->call_stmt
),
4725 "unsafe function call %qD within "
4726 "%<transaction_safe%> function", e
->callee
->decl
);
4729 /* Diagnose call from atomic transactions to unmarked functions
4730 that are determined to not be safe. */
4733 ipa_tm_diagnose_transaction (struct cgraph_node
*node
,
4734 struct tm_region
*all_tm_regions
)
4736 struct tm_region
*r
;
4738 for (r
= all_tm_regions
; r
; r
= r
->next
)
4739 if (gimple_transaction_subcode (r
->get_transaction_stmt ())
4742 /* Atomic transactions can be nested inside relaxed. */
4744 ipa_tm_diagnose_transaction (node
, r
->inner
);
4748 vec
<basic_block
> bbs
;
4749 gimple_stmt_iterator gsi
;
4753 bbs
= get_tm_region_blocks (r
->entry_block
, r
->exit_blocks
,
4754 r
->irr_blocks
, NULL
, false);
4756 for (i
= 0; bbs
.iterate (i
, &bb
); ++i
)
4757 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4759 gimple stmt
= gsi_stmt (gsi
);
4762 if (gimple_code (stmt
) == GIMPLE_ASM
)
4764 error_at (gimple_location (stmt
),
4765 "asm not allowed in atomic transaction");
4769 if (!is_gimple_call (stmt
))
4771 fndecl
= gimple_call_fndecl (stmt
);
4773 /* Indirect function calls have been diagnosed already. */
4777 /* Stop at the end of the transaction. */
4778 if (is_tm_ending_fndecl (fndecl
))
4780 if (bitmap_bit_p (r
->exit_blocks
, bb
->index
))
4785 /* Marked functions have been diagnosed already. */
4786 if (is_tm_pure_call (stmt
))
4788 if (is_tm_callable (fndecl
))
4791 if (cgraph_node::local_info (fndecl
)->tm_may_enter_irr
)
4792 error_at (gimple_location (stmt
),
4793 "unsafe function call %qD within "
4794 "atomic transaction", fndecl
);
4801 /* Return a transactional mangled name for the DECL_ASSEMBLER_NAME in
4802 OLD_DECL. The returned value is a freshly malloced pointer that
4803 should be freed by the caller. */
4806 tm_mangle (tree old_asm_id
)
4808 const char *old_asm_name
;
4811 struct demangle_component
*dc
;
4814 /* Determine if the symbol is already a valid C++ mangled name. Do this
4815 even for C, which might be interfacing with C++ code via appropriately
4816 ugly identifiers. */
4817 /* ??? We could probably do just as well checking for "_Z" and be done. */
4818 old_asm_name
= IDENTIFIER_POINTER (old_asm_id
);
4819 dc
= cplus_demangle_v3_components (old_asm_name
, DMGL_NO_OPTS
, &alloc
);
4826 sprintf (length
, "%u", IDENTIFIER_LENGTH (old_asm_id
));
4827 tm_name
= concat ("_ZGTt", length
, old_asm_name
, NULL
);
4831 old_asm_name
+= 2; /* Skip _Z */
4835 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
4836 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
4837 /* Don't play silly games, you! */
4840 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
4841 /* I'd really like to know if we can ever be passed one of
4842 these from the C++ front end. The Logical Thing would
4843 seem that hidden-alias should be outer-most, so that we
4844 get hidden-alias of a transaction-clone and not vice-versa. */
4852 tm_name
= concat ("_ZGTt", old_asm_name
, NULL
);
4856 new_asm_id
= get_identifier (tm_name
);
4863 ipa_tm_mark_force_output_node (struct cgraph_node
*node
)
4865 node
->mark_force_output ();
4866 node
->analyzed
= true;
4870 ipa_tm_mark_forced_by_abi_node (struct cgraph_node
*node
)
4872 node
->forced_by_abi
= true;
4873 node
->analyzed
= true;
4876 /* Callback data for ipa_tm_create_version_alias. */
4877 struct create_version_alias_info
4879 struct cgraph_node
*old_node
;
4883 /* A subroutine of ipa_tm_create_version, called via
4884 cgraph_for_node_and_aliases. Create new tm clones for each of
4885 the existing aliases. */
4887 ipa_tm_create_version_alias (struct cgraph_node
*node
, void *data
)
4889 struct create_version_alias_info
*info
4890 = (struct create_version_alias_info
*)data
;
4891 tree old_decl
, new_decl
, tm_name
;
4892 struct cgraph_node
*new_node
;
4894 if (!node
->cpp_implicit_alias
)
4897 old_decl
= node
->decl
;
4898 tm_name
= tm_mangle (DECL_ASSEMBLER_NAME (old_decl
));
4899 new_decl
= build_decl (DECL_SOURCE_LOCATION (old_decl
),
4900 TREE_CODE (old_decl
), tm_name
,
4901 TREE_TYPE (old_decl
));
4903 SET_DECL_ASSEMBLER_NAME (new_decl
, tm_name
);
4904 SET_DECL_RTL (new_decl
, NULL
);
4906 /* Based loosely on C++'s make_alias_for(). */
4907 TREE_PUBLIC (new_decl
) = TREE_PUBLIC (old_decl
);
4908 DECL_CONTEXT (new_decl
) = DECL_CONTEXT (old_decl
);
4909 DECL_LANG_SPECIFIC (new_decl
) = DECL_LANG_SPECIFIC (old_decl
);
4910 TREE_READONLY (new_decl
) = TREE_READONLY (old_decl
);
4911 DECL_EXTERNAL (new_decl
) = 0;
4912 DECL_ARTIFICIAL (new_decl
) = 1;
4913 TREE_ADDRESSABLE (new_decl
) = 1;
4914 TREE_USED (new_decl
) = 1;
4915 TREE_SYMBOL_REFERENCED (tm_name
) = 1;
4917 /* Perform the same remapping to the comdat group. */
4918 if (DECL_ONE_ONLY (new_decl
))
4919 varpool_node::get (new_decl
)->set_comdat_group
4920 (tm_mangle (decl_comdat_group_id (old_decl
)));
4922 new_node
= cgraph_node::create_same_body_alias (new_decl
, info
->new_decl
);
4923 new_node
->tm_clone
= true;
4924 new_node
->externally_visible
= info
->old_node
->externally_visible
;
4925 new_node
->no_reorder
= info
->old_node
->no_reorder
;
4926 /* ?? Do not traverse aliases here. */
4927 get_cg_data (&node
, false)->clone
= new_node
;
4929 record_tm_clone_pair (old_decl
, new_decl
);
4931 if (info
->old_node
->force_output
4932 || info
->old_node
->ref_list
.first_referring ())
4933 ipa_tm_mark_force_output_node (new_node
);
4934 if (info
->old_node
->forced_by_abi
)
4935 ipa_tm_mark_forced_by_abi_node (new_node
);
4939 /* Create a copy of the function (possibly declaration only) of OLD_NODE,
4940 appropriate for the transactional clone. */
4943 ipa_tm_create_version (struct cgraph_node
*old_node
)
4945 tree new_decl
, old_decl
, tm_name
;
4946 struct cgraph_node
*new_node
;
4948 old_decl
= old_node
->decl
;
4949 new_decl
= copy_node (old_decl
);
4951 /* DECL_ASSEMBLER_NAME needs to be set before we call
4952 cgraph_copy_node_for_versioning below, because cgraph_node will
4953 fill the assembler_name_hash. */
4954 tm_name
= tm_mangle (DECL_ASSEMBLER_NAME (old_decl
));
4955 SET_DECL_ASSEMBLER_NAME (new_decl
, tm_name
);
4956 SET_DECL_RTL (new_decl
, NULL
);
4957 TREE_SYMBOL_REFERENCED (tm_name
) = 1;
4959 /* Perform the same remapping to the comdat group. */
4960 if (DECL_ONE_ONLY (new_decl
))
4961 varpool_node::get (new_decl
)->set_comdat_group
4962 (tm_mangle (DECL_COMDAT_GROUP (old_decl
)));
4964 gcc_assert (!old_node
->ipa_transforms_to_apply
.exists ());
4965 new_node
= old_node
->create_version_clone (new_decl
, vNULL
, NULL
);
4966 new_node
->local
.local
= false;
4967 new_node
->externally_visible
= old_node
->externally_visible
;
4968 new_node
->lowered
= true;
4969 new_node
->tm_clone
= 1;
4970 if (!old_node
->implicit_section
)
4971 new_node
->set_section (old_node
->get_section ());
4972 get_cg_data (&old_node
, true)->clone
= new_node
;
4974 if (old_node
->get_availability () >= AVAIL_INTERPOSABLE
)
4976 /* Remap extern inline to static inline. */
4977 /* ??? Is it worth trying to use make_decl_one_only? */
4978 if (DECL_DECLARED_INLINE_P (new_decl
) && DECL_EXTERNAL (new_decl
))
4980 DECL_EXTERNAL (new_decl
) = 0;
4981 TREE_PUBLIC (new_decl
) = 0;
4982 DECL_WEAK (new_decl
) = 0;
4985 tree_function_versioning (old_decl
, new_decl
,
4990 record_tm_clone_pair (old_decl
, new_decl
);
4992 symtab
->call_cgraph_insertion_hooks (new_node
);
4993 if (old_node
->force_output
4994 || old_node
->ref_list
.first_referring ())
4995 ipa_tm_mark_force_output_node (new_node
);
4996 if (old_node
->forced_by_abi
)
4997 ipa_tm_mark_forced_by_abi_node (new_node
);
4999 /* Do the same thing, but for any aliases of the original node. */
5001 struct create_version_alias_info data
;
5002 data
.old_node
= old_node
;
5003 data
.new_decl
= new_decl
;
5004 old_node
->call_for_symbol_thunks_and_aliases (ipa_tm_create_version_alias
,
5009 /* Construct a call to TM_IRREVOCABLE and insert it at the beginning of BB. */
5012 ipa_tm_insert_irr_call (struct cgraph_node
*node
, struct tm_region
*region
,
5015 gimple_stmt_iterator gsi
;
5018 transaction_subcode_ior (region
, GTMA_MAY_ENTER_IRREVOCABLE
);
5020 g
= gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_IRREVOCABLE
),
5021 1, build_int_cst (NULL_TREE
, MODE_SERIALIRREVOCABLE
));
5023 split_block_after_labels (bb
);
5024 gsi
= gsi_after_labels (bb
);
5025 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
5027 node
->create_edge (cgraph_node::get_create
5028 (builtin_decl_explicit (BUILT_IN_TM_IRREVOCABLE
)),
5030 compute_call_stmt_bb_frequency (node
->decl
,
5034 /* Construct a call to TM_GETTMCLONE and insert it before GSI. */
5037 ipa_tm_insert_gettmclone_call (struct cgraph_node
*node
,
5038 struct tm_region
*region
,
5039 gimple_stmt_iterator
*gsi
, gcall
*stmt
)
5041 tree gettm_fn
, ret
, old_fn
, callfn
;
5046 old_fn
= gimple_call_fn (stmt
);
5048 if (TREE_CODE (old_fn
) == ADDR_EXPR
)
5050 tree fndecl
= TREE_OPERAND (old_fn
, 0);
5051 tree clone
= get_tm_clone_pair (fndecl
);
5053 /* By transforming the call into a TM_GETTMCLONE, we are
5054 technically taking the address of the original function and
5055 its clone. Explain this so inlining will know this function
5057 cgraph_node::get (fndecl
)->mark_address_taken () ;
5059 cgraph_node::get (clone
)->mark_address_taken ();
5062 safe
= is_tm_safe (TREE_TYPE (old_fn
));
5063 gettm_fn
= builtin_decl_explicit (safe
? BUILT_IN_TM_GETTMCLONE_SAFE
5064 : BUILT_IN_TM_GETTMCLONE_IRR
);
5065 ret
= create_tmp_var (ptr_type_node
);
5068 transaction_subcode_ior (region
, GTMA_MAY_ENTER_IRREVOCABLE
);
5070 /* Discard OBJ_TYPE_REF, since we weren't able to fold it. */
5071 if (TREE_CODE (old_fn
) == OBJ_TYPE_REF
)
5072 old_fn
= OBJ_TYPE_REF_EXPR (old_fn
);
5074 g
= gimple_build_call (gettm_fn
, 1, old_fn
);
5075 ret
= make_ssa_name (ret
, g
);
5076 gimple_call_set_lhs (g
, ret
);
5078 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
5080 node
->create_edge (cgraph_node::get_create (gettm_fn
), g
, 0,
5081 compute_call_stmt_bb_frequency (node
->decl
,
5084 /* Cast return value from tm_gettmclone* into appropriate function
5086 callfn
= create_tmp_var (TREE_TYPE (old_fn
));
5087 g2
= gimple_build_assign (callfn
,
5088 fold_build1 (NOP_EXPR
, TREE_TYPE (callfn
), ret
));
5089 callfn
= make_ssa_name (callfn
, g2
);
5090 gimple_assign_set_lhs (g2
, callfn
);
5091 gsi_insert_before (gsi
, g2
, GSI_SAME_STMT
);
5093 /* ??? This is a hack to preserve the NOTHROW bit on the call,
5094 which we would have derived from the decl. Failure to save
5095 this bit means we might have to split the basic block. */
5096 if (gimple_call_nothrow_p (stmt
))
5097 gimple_call_set_nothrow (stmt
, true);
5099 gimple_call_set_fn (stmt
, callfn
);
5101 /* Discarding OBJ_TYPE_REF above may produce incompatible LHS and RHS
5102 for a call statement. Fix it. */
5104 tree lhs
= gimple_call_lhs (stmt
);
5105 tree rettype
= TREE_TYPE (gimple_call_fntype (stmt
));
5107 && !useless_type_conversion_p (TREE_TYPE (lhs
), rettype
))
5111 temp
= create_tmp_reg (rettype
);
5112 gimple_call_set_lhs (stmt
, temp
);
5114 g2
= gimple_build_assign (lhs
,
5115 fold_build1 (VIEW_CONVERT_EXPR
,
5116 TREE_TYPE (lhs
), temp
));
5117 gsi_insert_after (gsi
, g2
, GSI_SAME_STMT
);
5122 cgraph_edge
*e
= cgraph_node::get (current_function_decl
)->get_edge (stmt
);
5123 if (e
&& e
->indirect_info
)
5124 e
->indirect_info
->polymorphic
= false;
5129 /* Helper function for ipa_tm_transform_calls*. Given a call
5130 statement in GSI which resides inside transaction REGION, redirect
5131 the call to either its wrapper function, or its clone. */
5134 ipa_tm_transform_calls_redirect (struct cgraph_node
*node
,
5135 struct tm_region
*region
,
5136 gimple_stmt_iterator
*gsi
,
5137 bool *need_ssa_rename_p
)
5139 gcall
*stmt
= as_a
<gcall
*> (gsi_stmt (*gsi
));
5140 struct cgraph_node
*new_node
;
5141 struct cgraph_edge
*e
= node
->get_edge (stmt
);
5142 tree fndecl
= gimple_call_fndecl (stmt
);
5144 /* For indirect calls, pass the address through the runtime. */
5147 *need_ssa_rename_p
|=
5148 ipa_tm_insert_gettmclone_call (node
, region
, gsi
, stmt
);
5152 /* Handle some TM builtins. Ordinarily these aren't actually generated
5153 at this point, but handling these functions when written in by the
5154 user makes it easier to build unit tests. */
5155 if (flags_from_decl_or_type (fndecl
) & ECF_TM_BUILTIN
)
5158 /* Fixup recursive calls inside clones. */
5159 /* ??? Why did cgraph_copy_node_for_versioning update the call edges
5160 for recursion but not update the call statements themselves? */
5161 if (e
->caller
== e
->callee
&& decl_is_tm_clone (current_function_decl
))
5163 gimple_call_set_fndecl (stmt
, current_function_decl
);
5167 /* If there is a replacement, use it. */
5168 fndecl
= find_tm_replacement_function (fndecl
);
5171 new_node
= cgraph_node::get_create (fndecl
);
5173 /* ??? Mark all transaction_wrap functions tm_may_enter_irr.
5175 We can't do this earlier in record_tm_replacement because
5176 cgraph_remove_unreachable_nodes is called before we inject
5177 references to the node. Further, we can't do this in some
5178 nice central place in ipa_tm_execute because we don't have
5179 the exact list of wrapper functions that would be used.
5180 Marking more wrappers than necessary results in the creation
5181 of unnecessary cgraph_nodes, which can cause some of the
5182 other IPA passes to crash.
5184 We do need to mark these nodes so that we get the proper
5185 result in expand_call_tm. */
5186 /* ??? This seems broken. How is it that we're marking the
5187 CALLEE as may_enter_irr? Surely we should be marking the
5188 CALLER. Also note that find_tm_replacement_function also
5189 contains mappings into the TM runtime, e.g. memcpy. These
5190 we know won't go irrevocable. */
5191 new_node
->local
.tm_may_enter_irr
= 1;
5195 struct tm_ipa_cg_data
*d
;
5196 struct cgraph_node
*tnode
= e
->callee
;
5198 d
= get_cg_data (&tnode
, true);
5199 new_node
= d
->clone
;
5201 /* As we've already skipped pure calls and appropriate builtins,
5202 and we've already marked irrevocable blocks, if we can't come
5203 up with a static replacement, then ask the runtime. */
5204 if (new_node
== NULL
)
5206 *need_ssa_rename_p
|=
5207 ipa_tm_insert_gettmclone_call (node
, region
, gsi
, stmt
);
5211 fndecl
= new_node
->decl
;
5214 e
->redirect_callee (new_node
);
5215 gimple_call_set_fndecl (stmt
, fndecl
);
5218 /* Helper function for ipa_tm_transform_calls. For a given BB,
5219 install calls to tm_irrevocable when IRR_BLOCKS are reached,
5220 redirect other calls to the generated transactional clone. */
5223 ipa_tm_transform_calls_1 (struct cgraph_node
*node
, struct tm_region
*region
,
5224 basic_block bb
, bitmap irr_blocks
)
5226 gimple_stmt_iterator gsi
;
5227 bool need_ssa_rename
= false;
5229 if (irr_blocks
&& bitmap_bit_p (irr_blocks
, bb
->index
))
5231 ipa_tm_insert_irr_call (node
, region
, bb
);
5235 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
5237 gimple stmt
= gsi_stmt (gsi
);
5239 if (!is_gimple_call (stmt
))
5241 if (is_tm_pure_call (stmt
))
5244 /* Redirect edges to the appropriate replacement or clone. */
5245 ipa_tm_transform_calls_redirect (node
, region
, &gsi
, &need_ssa_rename
);
5248 return need_ssa_rename
;
5251 /* Walk the CFG for REGION, beginning at BB. Install calls to
5252 tm_irrevocable when IRR_BLOCKS are reached, redirect other calls to
5253 the generated transactional clone. */
5256 ipa_tm_transform_calls (struct cgraph_node
*node
, struct tm_region
*region
,
5257 basic_block bb
, bitmap irr_blocks
)
5259 bool need_ssa_rename
= false;
5262 auto_vec
<basic_block
> queue
;
5263 bitmap visited_blocks
= BITMAP_ALLOC (NULL
);
5265 queue
.safe_push (bb
);
5271 ipa_tm_transform_calls_1 (node
, region
, bb
, irr_blocks
);
5273 if (irr_blocks
&& bitmap_bit_p (irr_blocks
, bb
->index
))
5276 if (region
&& bitmap_bit_p (region
->exit_blocks
, bb
->index
))
5279 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5280 if (!bitmap_bit_p (visited_blocks
, e
->dest
->index
))
5282 bitmap_set_bit (visited_blocks
, e
->dest
->index
);
5283 queue
.safe_push (e
->dest
);
5286 while (!queue
.is_empty ());
5288 BITMAP_FREE (visited_blocks
);
5290 return need_ssa_rename
;
5293 /* Transform the calls within the TM regions within NODE. */
5296 ipa_tm_transform_transaction (struct cgraph_node
*node
)
5298 struct tm_ipa_cg_data
*d
;
5299 struct tm_region
*region
;
5300 bool need_ssa_rename
= false;
5302 d
= get_cg_data (&node
, true);
5304 push_cfun (DECL_STRUCT_FUNCTION (node
->decl
));
5305 calculate_dominance_info (CDI_DOMINATORS
);
5307 for (region
= d
->all_tm_regions
; region
; region
= region
->next
)
5309 /* If we're sure to go irrevocable, don't transform anything. */
5310 if (d
->irrevocable_blocks_normal
5311 && bitmap_bit_p (d
->irrevocable_blocks_normal
,
5312 region
->entry_block
->index
))
5314 transaction_subcode_ior (region
, GTMA_DOES_GO_IRREVOCABLE
5315 | GTMA_MAY_ENTER_IRREVOCABLE
5316 | GTMA_HAS_NO_INSTRUMENTATION
);
5321 ipa_tm_transform_calls (node
, region
, region
->entry_block
,
5322 d
->irrevocable_blocks_normal
);
5325 if (need_ssa_rename
)
5326 update_ssa (TODO_update_ssa_only_virtuals
);
5331 /* Transform the calls within the transactional clone of NODE. */
5334 ipa_tm_transform_clone (struct cgraph_node
*node
)
5336 struct tm_ipa_cg_data
*d
;
5337 bool need_ssa_rename
;
5339 d
= get_cg_data (&node
, true);
5341 /* If this function makes no calls and has no irrevocable blocks,
5342 then there's nothing to do. */
5343 /* ??? Remove non-aborting top-level transactions. */
5344 if (!node
->callees
&& !node
->indirect_calls
&& !d
->irrevocable_blocks_clone
)
5347 push_cfun (DECL_STRUCT_FUNCTION (d
->clone
->decl
));
5348 calculate_dominance_info (CDI_DOMINATORS
);
5351 ipa_tm_transform_calls (d
->clone
, NULL
,
5352 single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
5353 d
->irrevocable_blocks_clone
);
5355 if (need_ssa_rename
)
5356 update_ssa (TODO_update_ssa_only_virtuals
);
5361 /* Main entry point for the transactional memory IPA pass. */
5364 ipa_tm_execute (void)
5366 cgraph_node_queue tm_callees
= cgraph_node_queue ();
5367 /* List of functions that will go irrevocable. */
5368 cgraph_node_queue irr_worklist
= cgraph_node_queue ();
5370 struct cgraph_node
*node
;
5371 struct tm_ipa_cg_data
*d
;
5372 enum availability a
;
5375 #ifdef ENABLE_CHECKING
5376 cgraph_node::verify_cgraph_nodes ();
5379 bitmap_obstack_initialize (&tm_obstack
);
5380 initialize_original_copy_tables ();
5382 /* For all local functions marked tm_callable, queue them. */
5383 FOR_EACH_DEFINED_FUNCTION (node
)
5384 if (is_tm_callable (node
->decl
)
5385 && node
->get_availability () >= AVAIL_INTERPOSABLE
)
5387 d
= get_cg_data (&node
, true);
5388 maybe_push_queue (node
, &tm_callees
, &d
->in_callee_queue
);
5391 /* For all local reachable functions... */
5392 FOR_EACH_DEFINED_FUNCTION (node
)
5394 && node
->get_availability () >= AVAIL_INTERPOSABLE
)
5396 /* ... marked tm_pure, record that fact for the runtime by
5397 indicating that the pure function is its own tm_callable.
5398 No need to do this if the function's address can't be taken. */
5399 if (is_tm_pure (node
->decl
))
5401 if (!node
->local
.local
)
5402 record_tm_clone_pair (node
->decl
, node
->decl
);
5406 push_cfun (DECL_STRUCT_FUNCTION (node
->decl
));
5407 calculate_dominance_info (CDI_DOMINATORS
);
5409 tm_region_init (NULL
);
5412 d
= get_cg_data (&node
, true);
5414 /* Scan for calls that are in each transaction, and
5415 generate the uninstrumented code path. */
5416 ipa_tm_scan_calls_transaction (d
, &tm_callees
);
5418 /* Put it in the worklist so we can scan the function
5419 later (ipa_tm_scan_irr_function) and mark the
5420 irrevocable blocks. */
5421 maybe_push_queue (node
, &irr_worklist
, &d
->in_worklist
);
5422 d
->want_irr_scan_normal
= true;
5428 /* For every local function on the callee list, scan as if we will be
5429 creating a transactional clone, queueing all new functions we find
5431 for (i
= 0; i
< tm_callees
.length (); ++i
)
5433 node
= tm_callees
[i
];
5434 a
= node
->get_availability ();
5435 d
= get_cg_data (&node
, true);
5437 /* Put it in the worklist so we can scan the function later
5438 (ipa_tm_scan_irr_function) and mark the irrevocable
5440 maybe_push_queue (node
, &irr_worklist
, &d
->in_worklist
);
5442 /* Some callees cannot be arbitrarily cloned. These will always be
5443 irrevocable. Mark these now, so that we need not scan them. */
5444 if (is_tm_irrevocable (node
->decl
))
5445 ipa_tm_note_irrevocable (node
, &irr_worklist
);
5446 else if (a
<= AVAIL_NOT_AVAILABLE
5447 && !is_tm_safe_or_pure (node
->decl
))
5448 ipa_tm_note_irrevocable (node
, &irr_worklist
);
5449 else if (a
>= AVAIL_INTERPOSABLE
)
5451 if (!tree_versionable_function_p (node
->decl
))
5452 ipa_tm_note_irrevocable (node
, &irr_worklist
);
5453 else if (!d
->is_irrevocable
)
5455 /* If this is an alias, make sure its base is queued as well.
5456 we need not scan the callees now, as the base will do. */
5459 node
= cgraph_node::get (node
->thunk
.alias
);
5460 d
= get_cg_data (&node
, true);
5461 maybe_push_queue (node
, &tm_callees
, &d
->in_callee_queue
);
5465 /* Add all nodes called by this function into
5466 tm_callees as well. */
5467 ipa_tm_scan_calls_clone (node
, &tm_callees
);
5472 /* Iterate scans until no more work to be done. Prefer not to use
5473 vec::pop because the worklist tends to follow a breadth-first
5474 search of the callgraph, which should allow convergance with a
5475 minimum number of scans. But we also don't want the worklist
5476 array to grow without bound, so we shift the array up periodically. */
5477 for (i
= 0; i
< irr_worklist
.length (); ++i
)
5479 if (i
> 256 && i
== irr_worklist
.length () / 8)
5481 irr_worklist
.block_remove (0, i
);
5485 node
= irr_worklist
[i
];
5486 d
= get_cg_data (&node
, true);
5487 d
->in_worklist
= false;
5489 if (d
->want_irr_scan_normal
)
5491 d
->want_irr_scan_normal
= false;
5492 ipa_tm_scan_irr_function (node
, false);
5494 if (d
->in_callee_queue
&& ipa_tm_scan_irr_function (node
, true))
5495 ipa_tm_note_irrevocable (node
, &irr_worklist
);
5498 /* For every function on the callee list, collect the tm_may_enter_irr
5500 irr_worklist
.truncate (0);
5501 for (i
= 0; i
< tm_callees
.length (); ++i
)
5503 node
= tm_callees
[i
];
5504 if (ipa_tm_mayenterirr_function (node
))
5506 d
= get_cg_data (&node
, true);
5507 gcc_assert (d
->in_worklist
== false);
5508 maybe_push_queue (node
, &irr_worklist
, &d
->in_worklist
);
5512 /* Propagate the tm_may_enter_irr bit to callers until stable. */
5513 for (i
= 0; i
< irr_worklist
.length (); ++i
)
5515 struct cgraph_node
*caller
;
5516 struct cgraph_edge
*e
;
5517 struct ipa_ref
*ref
;
5519 if (i
> 256 && i
== irr_worklist
.length () / 8)
5521 irr_worklist
.block_remove (0, i
);
5525 node
= irr_worklist
[i
];
5526 d
= get_cg_data (&node
, true);
5527 d
->in_worklist
= false;
5528 node
->local
.tm_may_enter_irr
= true;
5530 /* Propagate back to normal callers. */
5531 for (e
= node
->callers
; e
; e
= e
->next_caller
)
5534 if (!is_tm_safe_or_pure (caller
->decl
)
5535 && !caller
->local
.tm_may_enter_irr
)
5537 d
= get_cg_data (&caller
, true);
5538 maybe_push_queue (caller
, &irr_worklist
, &d
->in_worklist
);
5542 /* Propagate back to referring aliases as well. */
5543 FOR_EACH_ALIAS (node
, ref
)
5545 caller
= dyn_cast
<cgraph_node
*> (ref
->referring
);
5546 if (!caller
->local
.tm_may_enter_irr
)
5548 /* ?? Do not traverse aliases here. */
5549 d
= get_cg_data (&caller
, false);
5550 maybe_push_queue (caller
, &irr_worklist
, &d
->in_worklist
);
5555 /* Now validate all tm_safe functions, and all atomic regions in
5557 FOR_EACH_DEFINED_FUNCTION (node
)
5559 && node
->get_availability () >= AVAIL_INTERPOSABLE
)
5561 d
= get_cg_data (&node
, true);
5562 if (is_tm_safe (node
->decl
))
5563 ipa_tm_diagnose_tm_safe (node
);
5564 else if (d
->all_tm_regions
)
5565 ipa_tm_diagnose_transaction (node
, d
->all_tm_regions
);
5568 /* Create clones. Do those that are not irrevocable and have a
5569 positive call count. Do those publicly visible functions that
5570 the user directed us to clone. */
5571 for (i
= 0; i
< tm_callees
.length (); ++i
)
5575 node
= tm_callees
[i
];
5576 if (node
->cpp_implicit_alias
)
5579 a
= node
->get_availability ();
5580 d
= get_cg_data (&node
, true);
5582 if (a
<= AVAIL_NOT_AVAILABLE
)
5583 doit
= is_tm_callable (node
->decl
);
5584 else if (a
<= AVAIL_AVAILABLE
&& is_tm_callable (node
->decl
))
5586 else if (!d
->is_irrevocable
5587 && d
->tm_callers_normal
+ d
->tm_callers_clone
> 0)
5591 ipa_tm_create_version (node
);
5594 /* Redirect calls to the new clones, and insert irrevocable marks. */
5595 for (i
= 0; i
< tm_callees
.length (); ++i
)
5597 node
= tm_callees
[i
];
5600 d
= get_cg_data (&node
, true);
5602 ipa_tm_transform_clone (node
);
5605 FOR_EACH_DEFINED_FUNCTION (node
)
5607 && node
->get_availability () >= AVAIL_INTERPOSABLE
)
5609 d
= get_cg_data (&node
, true);
5610 if (d
->all_tm_regions
)
5611 ipa_tm_transform_transaction (node
);
5614 /* Free and clear all data structures. */
5615 tm_callees
.release ();
5616 irr_worklist
.release ();
5617 bitmap_obstack_release (&tm_obstack
);
5618 free_original_copy_tables ();
5620 FOR_EACH_FUNCTION (node
)
5623 #ifdef ENABLE_CHECKING
5624 cgraph_node::verify_cgraph_nodes ();
5632 const pass_data pass_data_ipa_tm
=
5634 SIMPLE_IPA_PASS
, /* type */
5636 OPTGROUP_NONE
, /* optinfo_flags */
5637 TV_TRANS_MEM
, /* tv_id */
5638 ( PROP_ssa
| PROP_cfg
), /* properties_required */
5639 0, /* properties_provided */
5640 0, /* properties_destroyed */
5641 0, /* todo_flags_start */
5642 0, /* todo_flags_finish */
5645 class pass_ipa_tm
: public simple_ipa_opt_pass
5648 pass_ipa_tm (gcc::context
*ctxt
)
5649 : simple_ipa_opt_pass (pass_data_ipa_tm
, ctxt
)
5652 /* opt_pass methods: */
5653 virtual bool gate (function
*) { return flag_tm
; }
5654 virtual unsigned int execute (function
*) { return ipa_tm_execute (); }
5656 }; // class pass_ipa_tm
5660 simple_ipa_opt_pass
*
5661 make_pass_ipa_tm (gcc::context
*ctxt
)
5663 return new pass_ipa_tm (ctxt
);
5666 #include "gt-trans-mem.h"