* configure: Regenerated.
[official-gcc.git] / gcc / trans-mem.c
blob9ce646a6af0b7dfe7e8dc6462fbdb13a801b7813
1 /* Passes for transactional memory support.
2 Copyright (C) 2008, 2009, 2010, 2011, 2012 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
9 version.
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
14 for more details.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tree.h"
24 #include "gimple.h"
25 #include "tree-flow.h"
26 #include "tree-pass.h"
27 #include "tree-inline.h"
28 #include "diagnostic-core.h"
29 #include "demangle.h"
30 #include "output.h"
31 #include "trans-mem.h"
32 #include "params.h"
33 #include "target.h"
34 #include "langhooks.h"
35 #include "gimple-pretty-print.h"
36 #include "cfgloop.h"
39 #define PROB_VERY_UNLIKELY (REG_BR_PROB_BASE / 2000 - 1)
40 #define PROB_ALWAYS (REG_BR_PROB_BASE)
42 #define A_RUNINSTRUMENTEDCODE 0x0001
43 #define A_RUNUNINSTRUMENTEDCODE 0x0002
44 #define A_SAVELIVEVARIABLES 0x0004
45 #define A_RESTORELIVEVARIABLES 0x0008
46 #define A_ABORTTRANSACTION 0x0010
48 #define AR_USERABORT 0x0001
49 #define AR_USERRETRY 0x0002
50 #define AR_TMCONFLICT 0x0004
51 #define AR_EXCEPTIONBLOCKABORT 0x0008
52 #define AR_OUTERABORT 0x0010
54 #define MODE_SERIALIRREVOCABLE 0x0000
57 /* The representation of a transaction changes several times during the
58 lowering process. In the beginning, in the front-end we have the
59 GENERIC tree TRANSACTION_EXPR. For example,
61 __transaction {
62 local++;
63 if (++global == 10)
64 __tm_abort;
67 During initial gimplification (gimplify.c) the TRANSACTION_EXPR node is
68 trivially replaced with a GIMPLE_TRANSACTION node.
70 During pass_lower_tm, we examine the body of transactions looking
71 for aborts. Transactions that do not contain an abort may be
72 merged into an outer transaction. We also add a TRY-FINALLY node
73 to arrange for the transaction to be committed on any exit.
75 [??? Think about how this arrangement affects throw-with-commit
76 and throw-with-abort operations. In this case we want the TRY to
77 handle gotos, but not to catch any exceptions because the transaction
78 will already be closed.]
80 GIMPLE_TRANSACTION [label=NULL] {
81 try {
82 local = local + 1;
83 t0 = global;
84 t1 = t0 + 1;
85 global = t1;
86 if (t1 == 10)
87 __builtin___tm_abort ();
88 } finally {
89 __builtin___tm_commit ();
93 During pass_lower_eh, we create EH regions for the transactions,
94 intermixed with the regular EH stuff. This gives us a nice persistent
95 mapping (all the way through rtl) from transactional memory operation
96 back to the transaction, which allows us to get the abnormal edges
97 correct to model transaction aborts and restarts:
99 GIMPLE_TRANSACTION [label=over]
100 local = local + 1;
101 t0 = global;
102 t1 = t0 + 1;
103 global = t1;
104 if (t1 == 10)
105 __builtin___tm_abort ();
106 __builtin___tm_commit ();
107 over:
109 This is the end of all_lowering_passes, and so is what is present
110 during the IPA passes, and through all of the optimization passes.
112 During pass_ipa_tm, we examine all GIMPLE_TRANSACTION blocks in all
113 functions and mark functions for cloning.
115 At the end of gimple optimization, before exiting SSA form,
116 pass_tm_edges replaces statements that perform transactional
117 memory operations with the appropriate TM builtins, and swap
118 out function calls with their transactional clones. At this
119 point we introduce the abnormal transaction restart edges and
120 complete lowering of the GIMPLE_TRANSACTION node.
122 x = __builtin___tm_start (MAY_ABORT);
123 eh_label:
124 if (x & abort_transaction)
125 goto over;
126 local = local + 1;
127 t0 = __builtin___tm_load (global);
128 t1 = t0 + 1;
129 __builtin___tm_store (&global, t1);
130 if (t1 == 10)
131 __builtin___tm_abort ();
132 __builtin___tm_commit ();
133 over:
137 /* Return the attributes we want to examine for X, or NULL if it's not
138 something we examine. We look at function types, but allow pointers
139 to function types and function decls and peek through. */
141 static tree
142 get_attrs_for (const_tree x)
144 switch (TREE_CODE (x))
146 case FUNCTION_DECL:
147 return TYPE_ATTRIBUTES (TREE_TYPE (x));
148 break;
150 default:
151 if (TYPE_P (x))
152 return NULL;
153 x = TREE_TYPE (x);
154 if (TREE_CODE (x) != POINTER_TYPE)
155 return NULL;
156 /* FALLTHRU */
158 case POINTER_TYPE:
159 x = TREE_TYPE (x);
160 if (TREE_CODE (x) != FUNCTION_TYPE && TREE_CODE (x) != METHOD_TYPE)
161 return NULL;
162 /* FALLTHRU */
164 case FUNCTION_TYPE:
165 case METHOD_TYPE:
166 return TYPE_ATTRIBUTES (x);
170 /* Return true if X has been marked TM_PURE. */
172 bool
173 is_tm_pure (const_tree x)
175 unsigned flags;
177 switch (TREE_CODE (x))
179 case FUNCTION_DECL:
180 case FUNCTION_TYPE:
181 case METHOD_TYPE:
182 break;
184 default:
185 if (TYPE_P (x))
186 return false;
187 x = TREE_TYPE (x);
188 if (TREE_CODE (x) != POINTER_TYPE)
189 return false;
190 /* FALLTHRU */
192 case POINTER_TYPE:
193 x = TREE_TYPE (x);
194 if (TREE_CODE (x) != FUNCTION_TYPE && TREE_CODE (x) != METHOD_TYPE)
195 return false;
196 break;
199 flags = flags_from_decl_or_type (x);
200 return (flags & ECF_TM_PURE) != 0;
203 /* Return true if X has been marked TM_IRREVOCABLE. */
205 static bool
206 is_tm_irrevocable (tree x)
208 tree attrs = get_attrs_for (x);
210 if (attrs && lookup_attribute ("transaction_unsafe", attrs))
211 return true;
213 /* A call to the irrevocable builtin is by definition,
214 irrevocable. */
215 if (TREE_CODE (x) == ADDR_EXPR)
216 x = TREE_OPERAND (x, 0);
217 if (TREE_CODE (x) == FUNCTION_DECL
218 && DECL_BUILT_IN_CLASS (x) == BUILT_IN_NORMAL
219 && DECL_FUNCTION_CODE (x) == BUILT_IN_TM_IRREVOCABLE)
220 return true;
222 return false;
225 /* Return true if X has been marked TM_SAFE. */
227 bool
228 is_tm_safe (const_tree x)
230 if (flag_tm)
232 tree attrs = get_attrs_for (x);
233 if (attrs)
235 if (lookup_attribute ("transaction_safe", attrs))
236 return true;
237 if (lookup_attribute ("transaction_may_cancel_outer", attrs))
238 return true;
241 return false;
244 /* Return true if CALL is const, or tm_pure. */
246 static bool
247 is_tm_pure_call (gimple call)
249 tree fn = gimple_call_fn (call);
251 if (TREE_CODE (fn) == ADDR_EXPR)
253 fn = TREE_OPERAND (fn, 0);
254 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
256 else
257 fn = TREE_TYPE (fn);
259 return is_tm_pure (fn);
262 /* Return true if X has been marked TM_CALLABLE. */
264 static bool
265 is_tm_callable (tree x)
267 tree attrs = get_attrs_for (x);
268 if (attrs)
270 if (lookup_attribute ("transaction_callable", attrs))
271 return true;
272 if (lookup_attribute ("transaction_safe", attrs))
273 return true;
274 if (lookup_attribute ("transaction_may_cancel_outer", attrs))
275 return true;
277 return false;
280 /* Return true if X has been marked TRANSACTION_MAY_CANCEL_OUTER. */
282 bool
283 is_tm_may_cancel_outer (tree x)
285 tree attrs = get_attrs_for (x);
286 if (attrs)
287 return lookup_attribute ("transaction_may_cancel_outer", attrs) != NULL;
288 return false;
291 /* Return true for built in functions that "end" a transaction. */
293 bool
294 is_tm_ending_fndecl (tree fndecl)
296 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
297 switch (DECL_FUNCTION_CODE (fndecl))
299 case BUILT_IN_TM_COMMIT:
300 case BUILT_IN_TM_COMMIT_EH:
301 case BUILT_IN_TM_ABORT:
302 case BUILT_IN_TM_IRREVOCABLE:
303 return true;
304 default:
305 break;
308 return false;
311 /* Return true if STMT is a TM load. */
313 static bool
314 is_tm_load (gimple stmt)
316 tree fndecl;
318 if (gimple_code (stmt) != GIMPLE_CALL)
319 return false;
321 fndecl = gimple_call_fndecl (stmt);
322 return (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
323 && BUILTIN_TM_LOAD_P (DECL_FUNCTION_CODE (fndecl)));
326 /* Same as above, but for simple TM loads, that is, not the
327 after-write, after-read, etc optimized variants. */
329 static bool
330 is_tm_simple_load (gimple stmt)
332 tree fndecl;
334 if (gimple_code (stmt) != GIMPLE_CALL)
335 return false;
337 fndecl = gimple_call_fndecl (stmt);
338 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
340 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
341 return (fcode == BUILT_IN_TM_LOAD_1
342 || fcode == BUILT_IN_TM_LOAD_2
343 || fcode == BUILT_IN_TM_LOAD_4
344 || fcode == BUILT_IN_TM_LOAD_8
345 || fcode == BUILT_IN_TM_LOAD_FLOAT
346 || fcode == BUILT_IN_TM_LOAD_DOUBLE
347 || fcode == BUILT_IN_TM_LOAD_LDOUBLE
348 || fcode == BUILT_IN_TM_LOAD_M64
349 || fcode == BUILT_IN_TM_LOAD_M128
350 || fcode == BUILT_IN_TM_LOAD_M256);
352 return false;
355 /* Return true if STMT is a TM store. */
357 static bool
358 is_tm_store (gimple stmt)
360 tree fndecl;
362 if (gimple_code (stmt) != GIMPLE_CALL)
363 return false;
365 fndecl = gimple_call_fndecl (stmt);
366 return (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
367 && BUILTIN_TM_STORE_P (DECL_FUNCTION_CODE (fndecl)));
370 /* Same as above, but for simple TM stores, that is, not the
371 after-write, after-read, etc optimized variants. */
373 static bool
374 is_tm_simple_store (gimple stmt)
376 tree fndecl;
378 if (gimple_code (stmt) != GIMPLE_CALL)
379 return false;
381 fndecl = gimple_call_fndecl (stmt);
382 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
384 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
385 return (fcode == BUILT_IN_TM_STORE_1
386 || fcode == BUILT_IN_TM_STORE_2
387 || fcode == BUILT_IN_TM_STORE_4
388 || fcode == BUILT_IN_TM_STORE_8
389 || fcode == BUILT_IN_TM_STORE_FLOAT
390 || fcode == BUILT_IN_TM_STORE_DOUBLE
391 || fcode == BUILT_IN_TM_STORE_LDOUBLE
392 || fcode == BUILT_IN_TM_STORE_M64
393 || fcode == BUILT_IN_TM_STORE_M128
394 || fcode == BUILT_IN_TM_STORE_M256);
396 return false;
399 /* Return true if FNDECL is BUILT_IN_TM_ABORT. */
401 static bool
402 is_tm_abort (tree fndecl)
404 return (fndecl
405 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
406 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_TM_ABORT);
409 /* Build a GENERIC tree for a user abort. This is called by front ends
410 while transforming the __tm_abort statement. */
412 tree
413 build_tm_abort_call (location_t loc, bool is_outer)
415 return build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TM_ABORT), 1,
416 build_int_cst (integer_type_node,
417 AR_USERABORT
418 | (is_outer ? AR_OUTERABORT : 0)));
421 /* Common gateing function for several of the TM passes. */
423 static bool
424 gate_tm (void)
426 return flag_tm;
429 /* Map for aribtrary function replacement under TM, as created
430 by the tm_wrap attribute. */
432 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
433 htab_t tm_wrap_map;
435 void
436 record_tm_replacement (tree from, tree to)
438 struct tree_map **slot, *h;
440 /* Do not inline wrapper functions that will get replaced in the TM
441 pass.
443 Suppose you have foo() that will get replaced into tmfoo(). Make
444 sure the inliner doesn't try to outsmart us and inline foo()
445 before we get a chance to do the TM replacement. */
446 DECL_UNINLINABLE (from) = 1;
448 if (tm_wrap_map == NULL)
449 tm_wrap_map = htab_create_ggc (32, tree_map_hash, tree_map_eq, 0);
451 h = ggc_alloc_tree_map ();
452 h->hash = htab_hash_pointer (from);
453 h->base.from = from;
454 h->to = to;
456 slot = (struct tree_map **)
457 htab_find_slot_with_hash (tm_wrap_map, h, h->hash, INSERT);
458 *slot = h;
461 /* Return a TM-aware replacement function for DECL. */
463 static tree
464 find_tm_replacement_function (tree fndecl)
466 if (tm_wrap_map)
468 struct tree_map *h, in;
470 in.base.from = fndecl;
471 in.hash = htab_hash_pointer (fndecl);
472 h = (struct tree_map *) htab_find_with_hash (tm_wrap_map, &in, in.hash);
473 if (h)
474 return h->to;
477 /* ??? We may well want TM versions of most of the common <string.h>
478 functions. For now, we've already these two defined. */
479 /* Adjust expand_call_tm() attributes as necessary for the cases
480 handled here: */
481 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
482 switch (DECL_FUNCTION_CODE (fndecl))
484 case BUILT_IN_MEMCPY:
485 return builtin_decl_explicit (BUILT_IN_TM_MEMCPY);
486 case BUILT_IN_MEMMOVE:
487 return builtin_decl_explicit (BUILT_IN_TM_MEMMOVE);
488 case BUILT_IN_MEMSET:
489 return builtin_decl_explicit (BUILT_IN_TM_MEMSET);
490 default:
491 return NULL;
494 return NULL;
497 /* When appropriate, record TM replacement for memory allocation functions.
499 FROM is the FNDECL to wrap. */
500 void
501 tm_malloc_replacement (tree from)
503 const char *str;
504 tree to;
506 if (TREE_CODE (from) != FUNCTION_DECL)
507 return;
509 /* If we have a previous replacement, the user must be explicitly
510 wrapping malloc/calloc/free. They better know what they're
511 doing... */
512 if (find_tm_replacement_function (from))
513 return;
515 str = IDENTIFIER_POINTER (DECL_NAME (from));
517 if (!strcmp (str, "malloc"))
518 to = builtin_decl_explicit (BUILT_IN_TM_MALLOC);
519 else if (!strcmp (str, "calloc"))
520 to = builtin_decl_explicit (BUILT_IN_TM_CALLOC);
521 else if (!strcmp (str, "free"))
522 to = builtin_decl_explicit (BUILT_IN_TM_FREE);
523 else
524 return;
526 TREE_NOTHROW (to) = 0;
528 record_tm_replacement (from, to);
531 /* Diagnostics for tm_safe functions/regions. Called by the front end
532 once we've lowered the function to high-gimple. */
534 /* Subroutine of diagnose_tm_safe_errors, called through walk_gimple_seq.
535 Process exactly one statement. WI->INFO is set to non-null when in
536 the context of a tm_safe function, and null for a __transaction block. */
538 #define DIAG_TM_OUTER 1
539 #define DIAG_TM_SAFE 2
540 #define DIAG_TM_RELAXED 4
542 struct diagnose_tm
544 unsigned int summary_flags : 8;
545 unsigned int block_flags : 8;
546 unsigned int func_flags : 8;
547 unsigned int saw_volatile : 1;
548 gimple stmt;
551 /* Tree callback function for diagnose_tm pass. */
553 static tree
554 diagnose_tm_1_op (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
555 void *data)
557 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
558 struct diagnose_tm *d = (struct diagnose_tm *) wi->info;
559 enum tree_code code = TREE_CODE (*tp);
561 if ((code == VAR_DECL
562 || code == RESULT_DECL
563 || code == PARM_DECL)
564 && d->block_flags & (DIAG_TM_SAFE | DIAG_TM_RELAXED)
565 && TREE_THIS_VOLATILE (TREE_TYPE (*tp))
566 && !d->saw_volatile)
568 d->saw_volatile = 1;
569 error_at (gimple_location (d->stmt),
570 "invalid volatile use of %qD inside transaction",
571 *tp);
574 return NULL_TREE;
577 static tree
578 diagnose_tm_1 (gimple_stmt_iterator *gsi, bool *handled_ops_p,
579 struct walk_stmt_info *wi)
581 gimple stmt = gsi_stmt (*gsi);
582 struct diagnose_tm *d = (struct diagnose_tm *) wi->info;
584 /* Save stmt for use in leaf analysis. */
585 d->stmt = stmt;
587 switch (gimple_code (stmt))
589 case GIMPLE_CALL:
591 tree fn = gimple_call_fn (stmt);
593 if ((d->summary_flags & DIAG_TM_OUTER) == 0
594 && is_tm_may_cancel_outer (fn))
595 error_at (gimple_location (stmt),
596 "%<transaction_may_cancel_outer%> function call not within"
597 " outer transaction or %<transaction_may_cancel_outer%>");
599 if (d->summary_flags & DIAG_TM_SAFE)
601 bool is_safe, direct_call_p;
602 tree replacement;
604 if (TREE_CODE (fn) == ADDR_EXPR
605 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL)
607 direct_call_p = true;
608 replacement = TREE_OPERAND (fn, 0);
609 replacement = find_tm_replacement_function (replacement);
610 if (replacement)
611 fn = replacement;
613 else
615 direct_call_p = false;
616 replacement = NULL_TREE;
619 if (is_tm_safe_or_pure (fn))
620 is_safe = true;
621 else if (is_tm_callable (fn) || is_tm_irrevocable (fn))
623 /* A function explicitly marked transaction_callable as
624 opposed to transaction_safe is being defined to be
625 unsafe as part of its ABI, regardless of its contents. */
626 is_safe = false;
628 else if (direct_call_p)
630 if (flags_from_decl_or_type (fn) & ECF_TM_BUILTIN)
631 is_safe = true;
632 else if (replacement)
634 /* ??? At present we've been considering replacements
635 merely transaction_callable, and therefore might
636 enter irrevocable. The tm_wrap attribute has not
637 yet made it into the new language spec. */
638 is_safe = false;
640 else
642 /* ??? Diagnostics for unmarked direct calls moved into
643 the IPA pass. Section 3.2 of the spec details how
644 functions not marked should be considered "implicitly
645 safe" based on having examined the function body. */
646 is_safe = true;
649 else
651 /* An unmarked indirect call. Consider it unsafe even
652 though optimization may yet figure out how to inline. */
653 is_safe = false;
656 if (!is_safe)
658 if (TREE_CODE (fn) == ADDR_EXPR)
659 fn = TREE_OPERAND (fn, 0);
660 if (d->block_flags & DIAG_TM_SAFE)
662 if (direct_call_p)
663 error_at (gimple_location (stmt),
664 "unsafe function call %qD within "
665 "atomic transaction", fn);
666 else
668 if (!DECL_P (fn) || DECL_NAME (fn))
669 error_at (gimple_location (stmt),
670 "unsafe function call %qE within "
671 "atomic transaction", fn);
672 else
673 error_at (gimple_location (stmt),
674 "unsafe indirect function call within "
675 "atomic transaction");
678 else
680 if (direct_call_p)
681 error_at (gimple_location (stmt),
682 "unsafe function call %qD within "
683 "%<transaction_safe%> function", fn);
684 else
686 if (!DECL_P (fn) || DECL_NAME (fn))
687 error_at (gimple_location (stmt),
688 "unsafe function call %qE within "
689 "%<transaction_safe%> function", fn);
690 else
691 error_at (gimple_location (stmt),
692 "unsafe indirect function call within "
693 "%<transaction_safe%> function");
699 break;
701 case GIMPLE_ASM:
702 /* ??? We ought to come up with a way to add attributes to
703 asm statements, and then add "transaction_safe" to it.
704 Either that or get the language spec to resurrect __tm_waiver. */
705 if (d->block_flags & DIAG_TM_SAFE)
706 error_at (gimple_location (stmt),
707 "asm not allowed in atomic transaction");
708 else if (d->func_flags & DIAG_TM_SAFE)
709 error_at (gimple_location (stmt),
710 "asm not allowed in %<transaction_safe%> function");
711 break;
713 case GIMPLE_TRANSACTION:
715 unsigned char inner_flags = DIAG_TM_SAFE;
717 if (gimple_transaction_subcode (stmt) & GTMA_IS_RELAXED)
719 if (d->block_flags & DIAG_TM_SAFE)
720 error_at (gimple_location (stmt),
721 "relaxed transaction in atomic transaction");
722 else if (d->func_flags & DIAG_TM_SAFE)
723 error_at (gimple_location (stmt),
724 "relaxed transaction in %<transaction_safe%> function");
725 inner_flags = DIAG_TM_RELAXED;
727 else if (gimple_transaction_subcode (stmt) & GTMA_IS_OUTER)
729 if (d->block_flags)
730 error_at (gimple_location (stmt),
731 "outer transaction in transaction");
732 else if (d->func_flags & DIAG_TM_OUTER)
733 error_at (gimple_location (stmt),
734 "outer transaction in "
735 "%<transaction_may_cancel_outer%> function");
736 else if (d->func_flags & DIAG_TM_SAFE)
737 error_at (gimple_location (stmt),
738 "outer transaction in %<transaction_safe%> function");
739 inner_flags |= DIAG_TM_OUTER;
742 *handled_ops_p = true;
743 if (gimple_transaction_body (stmt))
745 struct walk_stmt_info wi_inner;
746 struct diagnose_tm d_inner;
748 memset (&d_inner, 0, sizeof (d_inner));
749 d_inner.func_flags = d->func_flags;
750 d_inner.block_flags = d->block_flags | inner_flags;
751 d_inner.summary_flags = d_inner.func_flags | d_inner.block_flags;
753 memset (&wi_inner, 0, sizeof (wi_inner));
754 wi_inner.info = &d_inner;
756 walk_gimple_seq (gimple_transaction_body (stmt),
757 diagnose_tm_1, diagnose_tm_1_op, &wi_inner);
760 break;
762 default:
763 break;
766 return NULL_TREE;
769 static unsigned int
770 diagnose_tm_blocks (void)
772 struct walk_stmt_info wi;
773 struct diagnose_tm d;
775 memset (&d, 0, sizeof (d));
776 if (is_tm_may_cancel_outer (current_function_decl))
777 d.func_flags = DIAG_TM_OUTER | DIAG_TM_SAFE;
778 else if (is_tm_safe (current_function_decl))
779 d.func_flags = DIAG_TM_SAFE;
780 d.summary_flags = d.func_flags;
782 memset (&wi, 0, sizeof (wi));
783 wi.info = &d;
785 walk_gimple_seq (gimple_body (current_function_decl),
786 diagnose_tm_1, diagnose_tm_1_op, &wi);
788 return 0;
791 struct gimple_opt_pass pass_diagnose_tm_blocks =
794 GIMPLE_PASS,
795 "*diagnose_tm_blocks", /* name */
796 gate_tm, /* gate */
797 diagnose_tm_blocks, /* execute */
798 NULL, /* sub */
799 NULL, /* next */
800 0, /* static_pass_number */
801 TV_TRANS_MEM, /* tv_id */
802 PROP_gimple_any, /* properties_required */
803 0, /* properties_provided */
804 0, /* properties_destroyed */
805 0, /* todo_flags_start */
806 0, /* todo_flags_finish */
810 /* Instead of instrumenting thread private memory, we save the
811 addresses in a log which we later use to save/restore the addresses
812 upon transaction start/restart.
814 The log is keyed by address, where each element contains individual
815 statements among different code paths that perform the store.
817 This log is later used to generate either plain save/restore of the
818 addresses upon transaction start/restart, or calls to the ITM_L*
819 logging functions.
821 So for something like:
823 struct large { int x[1000]; };
824 struct large lala = { 0 };
825 __transaction {
826 lala.x[i] = 123;
830 We can either save/restore:
832 lala = { 0 };
833 trxn = _ITM_startTransaction ();
834 if (trxn & a_saveLiveVariables)
835 tmp_lala1 = lala.x[i];
836 else if (a & a_restoreLiveVariables)
837 lala.x[i] = tmp_lala1;
839 or use the logging functions:
841 lala = { 0 };
842 trxn = _ITM_startTransaction ();
843 _ITM_LU4 (&lala.x[i]);
845 Obviously, if we use _ITM_L* to log, we prefer to call _ITM_L* as
846 far up the dominator tree to shadow all of the writes to a given
847 location (thus reducing the total number of logging calls), but not
848 so high as to be called on a path that does not perform a
849 write. */
851 /* One individual log entry. We may have multiple statements for the
852 same location if neither dominate each other (on different
853 execution paths). */
854 typedef struct tm_log_entry
856 /* Address to save. */
857 tree addr;
858 /* Entry block for the transaction this address occurs in. */
859 basic_block entry_block;
860 /* Dominating statements the store occurs in. */
861 gimple_vec stmts;
862 /* Initially, while we are building the log, we place a nonzero
863 value here to mean that this address *will* be saved with a
864 save/restore sequence. Later, when generating the save sequence
865 we place the SSA temp generated here. */
866 tree save_var;
867 } *tm_log_entry_t;
869 /* The actual log. */
870 static htab_t tm_log;
872 /* Addresses to log with a save/restore sequence. These should be in
873 dominator order. */
874 static VEC(tree,heap) *tm_log_save_addresses;
876 /* Map for an SSA_NAME originally pointing to a non aliased new piece
877 of memory (malloc, alloc, etc). */
878 static htab_t tm_new_mem_hash;
880 enum thread_memory_type
882 mem_non_local = 0,
883 mem_thread_local,
884 mem_transaction_local,
885 mem_max
888 typedef struct tm_new_mem_map
890 /* SSA_NAME being dereferenced. */
891 tree val;
892 enum thread_memory_type local_new_memory;
893 } tm_new_mem_map_t;
895 /* Htab support. Return hash value for a `tm_log_entry'. */
896 static hashval_t
897 tm_log_hash (const void *p)
899 const struct tm_log_entry *log = (const struct tm_log_entry *) p;
900 return iterative_hash_expr (log->addr, 0);
903 /* Htab support. Return true if two log entries are the same. */
904 static int
905 tm_log_eq (const void *p1, const void *p2)
907 const struct tm_log_entry *log1 = (const struct tm_log_entry *) p1;
908 const struct tm_log_entry *log2 = (const struct tm_log_entry *) p2;
910 /* FIXME:
912 rth: I suggest that we get rid of the component refs etc.
913 I.e. resolve the reference to base + offset.
915 We may need to actually finish a merge with mainline for this,
916 since we'd like to be presented with Richi's MEM_REF_EXPRs more
917 often than not. But in the meantime your tm_log_entry could save
918 the results of get_inner_reference.
920 See: g++.dg/tm/pr46653.C
923 /* Special case plain equality because operand_equal_p() below will
924 return FALSE if the addresses are equal but they have
925 side-effects (e.g. a volatile address). */
926 if (log1->addr == log2->addr)
927 return true;
929 return operand_equal_p (log1->addr, log2->addr, 0);
932 /* Htab support. Free one tm_log_entry. */
933 static void
934 tm_log_free (void *p)
936 struct tm_log_entry *lp = (struct tm_log_entry *) p;
937 VEC_free (gimple, heap, lp->stmts);
938 free (lp);
941 /* Initialize logging data structures. */
942 static void
943 tm_log_init (void)
945 tm_log = htab_create (10, tm_log_hash, tm_log_eq, tm_log_free);
946 tm_new_mem_hash = htab_create (5, struct_ptr_hash, struct_ptr_eq, free);
947 tm_log_save_addresses = VEC_alloc (tree, heap, 5);
950 /* Free logging data structures. */
951 static void
952 tm_log_delete (void)
954 htab_delete (tm_log);
955 htab_delete (tm_new_mem_hash);
956 VEC_free (tree, heap, tm_log_save_addresses);
959 /* Return true if MEM is a transaction invariant memory for the TM
960 region starting at REGION_ENTRY_BLOCK. */
961 static bool
962 transaction_invariant_address_p (const_tree mem, basic_block region_entry_block)
964 if ((TREE_CODE (mem) == INDIRECT_REF || TREE_CODE (mem) == MEM_REF)
965 && TREE_CODE (TREE_OPERAND (mem, 0)) == SSA_NAME)
967 basic_block def_bb;
969 def_bb = gimple_bb (SSA_NAME_DEF_STMT (TREE_OPERAND (mem, 0)));
970 return def_bb != region_entry_block
971 && dominated_by_p (CDI_DOMINATORS, region_entry_block, def_bb);
974 mem = strip_invariant_refs (mem);
975 return mem && (CONSTANT_CLASS_P (mem) || decl_address_invariant_p (mem));
978 /* Given an address ADDR in STMT, find it in the memory log or add it,
979 making sure to keep only the addresses highest in the dominator
980 tree.
982 ENTRY_BLOCK is the entry_block for the transaction.
984 If we find the address in the log, make sure it's either the same
985 address, or an equivalent one that dominates ADDR.
987 If we find the address, but neither ADDR dominates the found
988 address, nor the found one dominates ADDR, we're on different
989 execution paths. Add it.
991 If known, ENTRY_BLOCK is the entry block for the region, otherwise
992 NULL. */
993 static void
994 tm_log_add (basic_block entry_block, tree addr, gimple stmt)
996 void **slot;
997 struct tm_log_entry l, *lp;
999 l.addr = addr;
1000 slot = htab_find_slot (tm_log, &l, INSERT);
1001 if (!*slot)
1003 tree type = TREE_TYPE (addr);
1005 lp = XNEW (struct tm_log_entry);
1006 lp->addr = addr;
1007 *slot = lp;
1009 /* Small invariant addresses can be handled as save/restores. */
1010 if (entry_block
1011 && transaction_invariant_address_p (lp->addr, entry_block)
1012 && TYPE_SIZE_UNIT (type) != NULL
1013 && host_integerp (TYPE_SIZE_UNIT (type), 1)
1014 && (tree_low_cst (TYPE_SIZE_UNIT (type), 1)
1015 < PARAM_VALUE (PARAM_TM_MAX_AGGREGATE_SIZE))
1016 /* We must be able to copy this type normally. I.e., no
1017 special constructors and the like. */
1018 && !TREE_ADDRESSABLE (type))
1020 lp->save_var = create_tmp_reg (TREE_TYPE (lp->addr), "tm_save");
1021 lp->stmts = NULL;
1022 lp->entry_block = entry_block;
1023 /* Save addresses separately in dominator order so we don't
1024 get confused by overlapping addresses in the save/restore
1025 sequence. */
1026 VEC_safe_push (tree, heap, tm_log_save_addresses, lp->addr);
1028 else
1030 /* Use the logging functions. */
1031 lp->stmts = VEC_alloc (gimple, heap, 5);
1032 VEC_quick_push (gimple, lp->stmts, stmt);
1033 lp->save_var = NULL;
1036 else
1038 size_t i;
1039 gimple oldstmt;
1041 lp = (struct tm_log_entry *) *slot;
1043 /* If we're generating a save/restore sequence, we don't care
1044 about statements. */
1045 if (lp->save_var)
1046 return;
1048 for (i = 0; VEC_iterate (gimple, lp->stmts, i, oldstmt); ++i)
1050 if (stmt == oldstmt)
1051 return;
1052 /* We already have a store to the same address, higher up the
1053 dominator tree. Nothing to do. */
1054 if (dominated_by_p (CDI_DOMINATORS,
1055 gimple_bb (stmt), gimple_bb (oldstmt)))
1056 return;
1057 /* We should be processing blocks in dominator tree order. */
1058 gcc_assert (!dominated_by_p (CDI_DOMINATORS,
1059 gimple_bb (oldstmt), gimple_bb (stmt)));
1061 /* Store is on a different code path. */
1062 VEC_safe_push (gimple, heap, lp->stmts, stmt);
1066 /* Gimplify the address of a TARGET_MEM_REF. Return the SSA_NAME
1067 result, insert the new statements before GSI. */
1069 static tree
1070 gimplify_addr (gimple_stmt_iterator *gsi, tree x)
1072 if (TREE_CODE (x) == TARGET_MEM_REF)
1073 x = tree_mem_ref_addr (build_pointer_type (TREE_TYPE (x)), x);
1074 else
1075 x = build_fold_addr_expr (x);
1076 return force_gimple_operand_gsi (gsi, x, true, NULL, true, GSI_SAME_STMT);
1079 /* Instrument one address with the logging functions.
1080 ADDR is the address to save.
1081 STMT is the statement before which to place it. */
1082 static void
1083 tm_log_emit_stmt (tree addr, gimple stmt)
1085 tree type = TREE_TYPE (addr);
1086 tree size = TYPE_SIZE_UNIT (type);
1087 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1088 gimple log;
1089 enum built_in_function code = BUILT_IN_TM_LOG;
1091 if (type == float_type_node)
1092 code = BUILT_IN_TM_LOG_FLOAT;
1093 else if (type == double_type_node)
1094 code = BUILT_IN_TM_LOG_DOUBLE;
1095 else if (type == long_double_type_node)
1096 code = BUILT_IN_TM_LOG_LDOUBLE;
1097 else if (host_integerp (size, 1))
1099 unsigned int n = tree_low_cst (size, 1);
1100 switch (n)
1102 case 1:
1103 code = BUILT_IN_TM_LOG_1;
1104 break;
1105 case 2:
1106 code = BUILT_IN_TM_LOG_2;
1107 break;
1108 case 4:
1109 code = BUILT_IN_TM_LOG_4;
1110 break;
1111 case 8:
1112 code = BUILT_IN_TM_LOG_8;
1113 break;
1114 default:
1115 code = BUILT_IN_TM_LOG;
1116 if (TREE_CODE (type) == VECTOR_TYPE)
1118 if (n == 8 && builtin_decl_explicit (BUILT_IN_TM_LOG_M64))
1119 code = BUILT_IN_TM_LOG_M64;
1120 else if (n == 16 && builtin_decl_explicit (BUILT_IN_TM_LOG_M128))
1121 code = BUILT_IN_TM_LOG_M128;
1122 else if (n == 32 && builtin_decl_explicit (BUILT_IN_TM_LOG_M256))
1123 code = BUILT_IN_TM_LOG_M256;
1125 break;
1129 addr = gimplify_addr (&gsi, addr);
1130 if (code == BUILT_IN_TM_LOG)
1131 log = gimple_build_call (builtin_decl_explicit (code), 2, addr, size);
1132 else
1133 log = gimple_build_call (builtin_decl_explicit (code), 1, addr);
1134 gsi_insert_before (&gsi, log, GSI_SAME_STMT);
1137 /* Go through the log and instrument address that must be instrumented
1138 with the logging functions. Leave the save/restore addresses for
1139 later. */
1140 static void
1141 tm_log_emit (void)
1143 htab_iterator hi;
1144 struct tm_log_entry *lp;
1146 FOR_EACH_HTAB_ELEMENT (tm_log, lp, tm_log_entry_t, hi)
1148 size_t i;
1149 gimple stmt;
1151 if (dump_file)
1153 fprintf (dump_file, "TM thread private mem logging: ");
1154 print_generic_expr (dump_file, lp->addr, 0);
1155 fprintf (dump_file, "\n");
1158 if (lp->save_var)
1160 if (dump_file)
1161 fprintf (dump_file, "DUMPING to variable\n");
1162 continue;
1164 else
1166 if (dump_file)
1167 fprintf (dump_file, "DUMPING with logging functions\n");
1168 for (i = 0; VEC_iterate (gimple, lp->stmts, i, stmt); ++i)
1169 tm_log_emit_stmt (lp->addr, stmt);
1174 /* Emit the save sequence for the corresponding addresses in the log.
1175 ENTRY_BLOCK is the entry block for the transaction.
1176 BB is the basic block to insert the code in. */
1177 static void
1178 tm_log_emit_saves (basic_block entry_block, basic_block bb)
1180 size_t i;
1181 gimple_stmt_iterator gsi = gsi_last_bb (bb);
1182 gimple stmt;
1183 struct tm_log_entry l, *lp;
1185 for (i = 0; i < VEC_length (tree, tm_log_save_addresses); ++i)
1187 l.addr = VEC_index (tree, tm_log_save_addresses, i);
1188 lp = (struct tm_log_entry *) *htab_find_slot (tm_log, &l, NO_INSERT);
1189 gcc_assert (lp->save_var != NULL);
1191 /* We only care about variables in the current transaction. */
1192 if (lp->entry_block != entry_block)
1193 continue;
1195 stmt = gimple_build_assign (lp->save_var, unshare_expr (lp->addr));
1197 /* Make sure we can create an SSA_NAME for this type. For
1198 instance, aggregates aren't allowed, in which case the system
1199 will create a VOP for us and everything will just work. */
1200 if (is_gimple_reg_type (TREE_TYPE (lp->save_var)))
1202 lp->save_var = make_ssa_name (lp->save_var, stmt);
1203 gimple_assign_set_lhs (stmt, lp->save_var);
1206 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
1210 /* Emit the restore sequence for the corresponding addresses in the log.
1211 ENTRY_BLOCK is the entry block for the transaction.
1212 BB is the basic block to insert the code in. */
1213 static void
1214 tm_log_emit_restores (basic_block entry_block, basic_block bb)
1216 int i;
1217 struct tm_log_entry l, *lp;
1218 gimple_stmt_iterator gsi;
1219 gimple stmt;
1221 for (i = VEC_length (tree, tm_log_save_addresses) - 1; i >= 0; i--)
1223 l.addr = VEC_index (tree, tm_log_save_addresses, i);
1224 lp = (struct tm_log_entry *) *htab_find_slot (tm_log, &l, NO_INSERT);
1225 gcc_assert (lp->save_var != NULL);
1227 /* We only care about variables in the current transaction. */
1228 if (lp->entry_block != entry_block)
1229 continue;
1231 /* Restores are in LIFO order from the saves in case we have
1232 overlaps. */
1233 gsi = gsi_start_bb (bb);
1235 stmt = gimple_build_assign (unshare_expr (lp->addr), lp->save_var);
1236 gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
1240 /* Emit the checks for performing either a save or a restore sequence.
1242 TRXN_PROP is either A_SAVELIVEVARIABLES or A_RESTORELIVEVARIABLES.
1244 The code sequence is inserted in a new basic block created in
1245 END_BB which is inserted between BEFORE_BB and the destination of
1246 FALLTHRU_EDGE.
1248 STATUS is the return value from _ITM_beginTransaction.
1249 ENTRY_BLOCK is the entry block for the transaction.
1250 EMITF is a callback to emit the actual save/restore code.
1252 The basic block containing the conditional checking for TRXN_PROP
1253 is returned. */
1254 static basic_block
1255 tm_log_emit_save_or_restores (basic_block entry_block,
1256 unsigned trxn_prop,
1257 tree status,
1258 void (*emitf)(basic_block, basic_block),
1259 basic_block before_bb,
1260 edge fallthru_edge,
1261 basic_block *end_bb)
1263 basic_block cond_bb, code_bb;
1264 gimple cond_stmt, stmt;
1265 gimple_stmt_iterator gsi;
1266 tree t1, t2;
1267 int old_flags = fallthru_edge->flags;
1269 cond_bb = create_empty_bb (before_bb);
1270 code_bb = create_empty_bb (cond_bb);
1271 *end_bb = create_empty_bb (code_bb);
1272 if (current_loops && before_bb->loop_father)
1274 add_bb_to_loop (cond_bb, before_bb->loop_father);
1275 add_bb_to_loop (code_bb, before_bb->loop_father);
1276 add_bb_to_loop (*end_bb, before_bb->loop_father);
1278 redirect_edge_pred (fallthru_edge, *end_bb);
1279 fallthru_edge->flags = EDGE_FALLTHRU;
1280 make_edge (before_bb, cond_bb, old_flags);
1282 set_immediate_dominator (CDI_DOMINATORS, cond_bb, before_bb);
1283 set_immediate_dominator (CDI_DOMINATORS, code_bb, cond_bb);
1285 gsi = gsi_last_bb (cond_bb);
1287 /* t1 = status & A_{property}. */
1288 t1 = create_tmp_reg (TREE_TYPE (status), NULL);
1289 t2 = build_int_cst (TREE_TYPE (status), trxn_prop);
1290 stmt = gimple_build_assign_with_ops (BIT_AND_EXPR, t1, status, t2);
1291 gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
1293 /* if (t1). */
1294 t2 = build_int_cst (TREE_TYPE (status), 0);
1295 cond_stmt = gimple_build_cond (NE_EXPR, t1, t2, NULL, NULL);
1296 gsi_insert_after (&gsi, cond_stmt, GSI_CONTINUE_LINKING);
1298 emitf (entry_block, code_bb);
1300 make_edge (cond_bb, code_bb, EDGE_TRUE_VALUE);
1301 make_edge (cond_bb, *end_bb, EDGE_FALSE_VALUE);
1302 make_edge (code_bb, *end_bb, EDGE_FALLTHRU);
1304 return cond_bb;
1307 static tree lower_sequence_tm (gimple_stmt_iterator *, bool *,
1308 struct walk_stmt_info *);
1309 static tree lower_sequence_no_tm (gimple_stmt_iterator *, bool *,
1310 struct walk_stmt_info *);
1312 /* Evaluate an address X being dereferenced and determine if it
1313 originally points to a non aliased new chunk of memory (malloc,
1314 alloca, etc).
1316 Return MEM_THREAD_LOCAL if it points to a thread-local address.
1317 Return MEM_TRANSACTION_LOCAL if it points to a transaction-local address.
1318 Return MEM_NON_LOCAL otherwise.
1320 ENTRY_BLOCK is the entry block to the transaction containing the
1321 dereference of X. */
1322 static enum thread_memory_type
1323 thread_private_new_memory (basic_block entry_block, tree x)
1325 gimple stmt = NULL;
1326 enum tree_code code;
1327 void **slot;
1328 tm_new_mem_map_t elt, *elt_p;
1329 tree val = x;
1330 enum thread_memory_type retval = mem_transaction_local;
1332 if (!entry_block
1333 || TREE_CODE (x) != SSA_NAME
1334 /* Possible uninitialized use, or a function argument. In
1335 either case, we don't care. */
1336 || SSA_NAME_IS_DEFAULT_DEF (x))
1337 return mem_non_local;
1339 /* Look in cache first. */
1340 elt.val = x;
1341 slot = htab_find_slot (tm_new_mem_hash, &elt, INSERT);
1342 elt_p = (tm_new_mem_map_t *) *slot;
1343 if (elt_p)
1344 return elt_p->local_new_memory;
1346 /* Optimistically assume the memory is transaction local during
1347 processing. This catches recursion into this variable. */
1348 *slot = elt_p = XNEW (tm_new_mem_map_t);
1349 elt_p->val = val;
1350 elt_p->local_new_memory = mem_transaction_local;
1352 /* Search DEF chain to find the original definition of this address. */
1355 if (ptr_deref_may_alias_global_p (x))
1357 /* Address escapes. This is not thread-private. */
1358 retval = mem_non_local;
1359 goto new_memory_ret;
1362 stmt = SSA_NAME_DEF_STMT (x);
1364 /* If the malloc call is outside the transaction, this is
1365 thread-local. */
1366 if (retval != mem_thread_local
1367 && !dominated_by_p (CDI_DOMINATORS, gimple_bb (stmt), entry_block))
1368 retval = mem_thread_local;
1370 if (is_gimple_assign (stmt))
1372 code = gimple_assign_rhs_code (stmt);
1373 /* x = foo ==> foo */
1374 if (code == SSA_NAME)
1375 x = gimple_assign_rhs1 (stmt);
1376 /* x = foo + n ==> foo */
1377 else if (code == POINTER_PLUS_EXPR)
1378 x = gimple_assign_rhs1 (stmt);
1379 /* x = (cast*) foo ==> foo */
1380 else if (code == VIEW_CONVERT_EXPR || code == NOP_EXPR)
1381 x = gimple_assign_rhs1 (stmt);
1382 /* x = c ? op1 : op2 == > op1 or op2 just like a PHI */
1383 else if (code == COND_EXPR)
1385 tree op1 = gimple_assign_rhs2 (stmt);
1386 tree op2 = gimple_assign_rhs3 (stmt);
1387 enum thread_memory_type mem;
1388 retval = thread_private_new_memory (entry_block, op1);
1389 if (retval == mem_non_local)
1390 goto new_memory_ret;
1391 mem = thread_private_new_memory (entry_block, op2);
1392 retval = MIN (retval, mem);
1393 goto new_memory_ret;
1395 else
1397 retval = mem_non_local;
1398 goto new_memory_ret;
1401 else
1403 if (gimple_code (stmt) == GIMPLE_PHI)
1405 unsigned int i;
1406 enum thread_memory_type mem;
1407 tree phi_result = gimple_phi_result (stmt);
1409 /* If any of the ancestors are non-local, we are sure to
1410 be non-local. Otherwise we can avoid doing anything
1411 and inherit what has already been generated. */
1412 retval = mem_max;
1413 for (i = 0; i < gimple_phi_num_args (stmt); ++i)
1415 tree op = PHI_ARG_DEF (stmt, i);
1417 /* Exclude self-assignment. */
1418 if (phi_result == op)
1419 continue;
1421 mem = thread_private_new_memory (entry_block, op);
1422 if (mem == mem_non_local)
1424 retval = mem;
1425 goto new_memory_ret;
1427 retval = MIN (retval, mem);
1429 goto new_memory_ret;
1431 break;
1434 while (TREE_CODE (x) == SSA_NAME);
1436 if (stmt && is_gimple_call (stmt) && gimple_call_flags (stmt) & ECF_MALLOC)
1437 /* Thread-local or transaction-local. */
1439 else
1440 retval = mem_non_local;
1442 new_memory_ret:
1443 elt_p->local_new_memory = retval;
1444 return retval;
1447 /* Determine whether X has to be instrumented using a read
1448 or write barrier.
1450 ENTRY_BLOCK is the entry block for the region where stmt resides
1451 in. NULL if unknown.
1453 STMT is the statement in which X occurs in. It is used for thread
1454 private memory instrumentation. If no TPM instrumentation is
1455 desired, STMT should be null. */
1456 static bool
1457 requires_barrier (basic_block entry_block, tree x, gimple stmt)
1459 tree orig = x;
1460 while (handled_component_p (x))
1461 x = TREE_OPERAND (x, 0);
1463 switch (TREE_CODE (x))
1465 case INDIRECT_REF:
1466 case MEM_REF:
1468 enum thread_memory_type ret;
1470 ret = thread_private_new_memory (entry_block, TREE_OPERAND (x, 0));
1471 if (ret == mem_non_local)
1472 return true;
1473 if (stmt && ret == mem_thread_local)
1474 /* ?? Should we pass `orig', or the INDIRECT_REF X. ?? */
1475 tm_log_add (entry_block, orig, stmt);
1477 /* Transaction-locals require nothing at all. For malloc, a
1478 transaction restart frees the memory and we reallocate.
1479 For alloca, the stack pointer gets reset by the retry and
1480 we reallocate. */
1481 return false;
1484 case TARGET_MEM_REF:
1485 if (TREE_CODE (TMR_BASE (x)) != ADDR_EXPR)
1486 return true;
1487 x = TREE_OPERAND (TMR_BASE (x), 0);
1488 if (TREE_CODE (x) == PARM_DECL)
1489 return false;
1490 gcc_assert (TREE_CODE (x) == VAR_DECL);
1491 /* FALLTHRU */
1493 case PARM_DECL:
1494 case RESULT_DECL:
1495 case VAR_DECL:
1496 if (DECL_BY_REFERENCE (x))
1498 /* ??? This value is a pointer, but aggregate_value_p has been
1499 jigged to return true which confuses needs_to_live_in_memory.
1500 This ought to be cleaned up generically.
1502 FIXME: Verify this still happens after the next mainline
1503 merge. Testcase ie g++.dg/tm/pr47554.C.
1505 return false;
1508 if (is_global_var (x))
1509 return !TREE_READONLY (x);
1510 if (/* FIXME: This condition should actually go below in the
1511 tm_log_add() call, however is_call_clobbered() depends on
1512 aliasing info which is not available during
1513 gimplification. Since requires_barrier() gets called
1514 during lower_sequence_tm/gimplification, leave the call
1515 to needs_to_live_in_memory until we eliminate
1516 lower_sequence_tm altogether. */
1517 needs_to_live_in_memory (x))
1518 return true;
1519 else
1521 /* For local memory that doesn't escape (aka thread private
1522 memory), we can either save the value at the beginning of
1523 the transaction and restore on restart, or call a tm
1524 function to dynamically save and restore on restart
1525 (ITM_L*). */
1526 if (stmt)
1527 tm_log_add (entry_block, orig, stmt);
1528 return false;
1531 default:
1532 return false;
1536 /* Mark the GIMPLE_ASSIGN statement as appropriate for being inside
1537 a transaction region. */
1539 static void
1540 examine_assign_tm (unsigned *state, gimple_stmt_iterator *gsi)
1542 gimple stmt = gsi_stmt (*gsi);
1544 if (requires_barrier (/*entry_block=*/NULL, gimple_assign_rhs1 (stmt), NULL))
1545 *state |= GTMA_HAVE_LOAD;
1546 if (requires_barrier (/*entry_block=*/NULL, gimple_assign_lhs (stmt), NULL))
1547 *state |= GTMA_HAVE_STORE;
1550 /* Mark a GIMPLE_CALL as appropriate for being inside a transaction. */
1552 static void
1553 examine_call_tm (unsigned *state, gimple_stmt_iterator *gsi)
1555 gimple stmt = gsi_stmt (*gsi);
1556 tree fn;
1558 if (is_tm_pure_call (stmt))
1559 return;
1561 /* Check if this call is a transaction abort. */
1562 fn = gimple_call_fndecl (stmt);
1563 if (is_tm_abort (fn))
1564 *state |= GTMA_HAVE_ABORT;
1566 /* Note that something may happen. */
1567 *state |= GTMA_HAVE_LOAD | GTMA_HAVE_STORE;
1570 /* Lower a GIMPLE_TRANSACTION statement. */
1572 static void
1573 lower_transaction (gimple_stmt_iterator *gsi, struct walk_stmt_info *wi)
1575 gimple g, stmt = gsi_stmt (*gsi);
1576 unsigned int *outer_state = (unsigned int *) wi->info;
1577 unsigned int this_state = 0;
1578 struct walk_stmt_info this_wi;
1580 /* First, lower the body. The scanning that we do inside gives
1581 us some idea of what we're dealing with. */
1582 memset (&this_wi, 0, sizeof (this_wi));
1583 this_wi.info = (void *) &this_state;
1584 walk_gimple_seq_mod (gimple_transaction_body_ptr (stmt),
1585 lower_sequence_tm, NULL, &this_wi);
1587 /* If there was absolutely nothing transaction related inside the
1588 transaction, we may elide it. Likewise if this is a nested
1589 transaction and does not contain an abort. */
1590 if (this_state == 0
1591 || (!(this_state & GTMA_HAVE_ABORT) && outer_state != NULL))
1593 if (outer_state)
1594 *outer_state |= this_state;
1596 gsi_insert_seq_before (gsi, gimple_transaction_body (stmt),
1597 GSI_SAME_STMT);
1598 gimple_transaction_set_body (stmt, NULL);
1600 gsi_remove (gsi, true);
1601 wi->removed_stmt = true;
1602 return;
1605 /* Wrap the body of the transaction in a try-finally node so that
1606 the commit call is always properly called. */
1607 g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_COMMIT), 0);
1608 if (flag_exceptions)
1610 tree ptr;
1611 gimple_seq n_seq, e_seq;
1613 n_seq = gimple_seq_alloc_with_stmt (g);
1614 e_seq = NULL;
1616 g = gimple_build_call (builtin_decl_explicit (BUILT_IN_EH_POINTER),
1617 1, integer_zero_node);
1618 ptr = create_tmp_var (ptr_type_node, NULL);
1619 gimple_call_set_lhs (g, ptr);
1620 gimple_seq_add_stmt (&e_seq, g);
1622 g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_COMMIT_EH),
1623 1, ptr);
1624 gimple_seq_add_stmt (&e_seq, g);
1626 g = gimple_build_eh_else (n_seq, e_seq);
1629 g = gimple_build_try (gimple_transaction_body (stmt),
1630 gimple_seq_alloc_with_stmt (g), GIMPLE_TRY_FINALLY);
1631 gsi_insert_after (gsi, g, GSI_CONTINUE_LINKING);
1633 gimple_transaction_set_body (stmt, NULL);
1635 /* If the transaction calls abort or if this is an outer transaction,
1636 add an "over" label afterwards. */
1637 if ((this_state & (GTMA_HAVE_ABORT))
1638 || (gimple_transaction_subcode(stmt) & GTMA_IS_OUTER))
1640 tree label = create_artificial_label (UNKNOWN_LOCATION);
1641 gimple_transaction_set_label (stmt, label);
1642 gsi_insert_after (gsi, gimple_build_label (label), GSI_CONTINUE_LINKING);
1645 /* Record the set of operations found for use later. */
1646 this_state |= gimple_transaction_subcode (stmt) & GTMA_DECLARATION_MASK;
1647 gimple_transaction_set_subcode (stmt, this_state);
1650 /* Iterate through the statements in the sequence, lowering them all
1651 as appropriate for being in a transaction. */
1653 static tree
1654 lower_sequence_tm (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1655 struct walk_stmt_info *wi)
1657 unsigned int *state = (unsigned int *) wi->info;
1658 gimple stmt = gsi_stmt (*gsi);
1660 *handled_ops_p = true;
1661 switch (gimple_code (stmt))
1663 case GIMPLE_ASSIGN:
1664 /* Only memory reads/writes need to be instrumented. */
1665 if (gimple_assign_single_p (stmt))
1666 examine_assign_tm (state, gsi);
1667 break;
1669 case GIMPLE_CALL:
1670 examine_call_tm (state, gsi);
1671 break;
1673 case GIMPLE_ASM:
1674 *state |= GTMA_MAY_ENTER_IRREVOCABLE;
1675 break;
1677 case GIMPLE_TRANSACTION:
1678 lower_transaction (gsi, wi);
1679 break;
1681 default:
1682 *handled_ops_p = !gimple_has_substatements (stmt);
1683 break;
1686 return NULL_TREE;
1689 /* Iterate through the statements in the sequence, lowering them all
1690 as appropriate for being outside of a transaction. */
1692 static tree
1693 lower_sequence_no_tm (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1694 struct walk_stmt_info * wi)
1696 gimple stmt = gsi_stmt (*gsi);
1698 if (gimple_code (stmt) == GIMPLE_TRANSACTION)
1700 *handled_ops_p = true;
1701 lower_transaction (gsi, wi);
1703 else
1704 *handled_ops_p = !gimple_has_substatements (stmt);
1706 return NULL_TREE;
1709 /* Main entry point for flattening GIMPLE_TRANSACTION constructs. After
1710 this, GIMPLE_TRANSACTION nodes still exist, but the nested body has
1711 been moved out, and all the data required for constructing a proper
1712 CFG has been recorded. */
1714 static unsigned int
1715 execute_lower_tm (void)
1717 struct walk_stmt_info wi;
1718 gimple_seq body;
1720 /* Transactional clones aren't created until a later pass. */
1721 gcc_assert (!decl_is_tm_clone (current_function_decl));
1723 body = gimple_body (current_function_decl);
1724 memset (&wi, 0, sizeof (wi));
1725 walk_gimple_seq_mod (&body, lower_sequence_no_tm, NULL, &wi);
1726 gimple_set_body (current_function_decl, body);
1728 return 0;
1731 struct gimple_opt_pass pass_lower_tm =
1734 GIMPLE_PASS,
1735 "tmlower", /* name */
1736 gate_tm, /* gate */
1737 execute_lower_tm, /* execute */
1738 NULL, /* sub */
1739 NULL, /* next */
1740 0, /* static_pass_number */
1741 TV_TRANS_MEM, /* tv_id */
1742 PROP_gimple_lcf, /* properties_required */
1743 0, /* properties_provided */
1744 0, /* properties_destroyed */
1745 0, /* todo_flags_start */
1746 0, /* todo_flags_finish */
1750 /* Collect region information for each transaction. */
1752 struct tm_region
1754 /* Link to the next unnested transaction. */
1755 struct tm_region *next;
1757 /* Link to the next inner transaction. */
1758 struct tm_region *inner;
1760 /* Link to the next outer transaction. */
1761 struct tm_region *outer;
1763 /* The GIMPLE_TRANSACTION statement beginning this transaction. */
1764 gimple transaction_stmt;
1766 /* The entry block to this region. */
1767 basic_block entry_block;
1769 /* The set of all blocks that end the region; NULL if only EXIT_BLOCK.
1770 These blocks are still a part of the region (i.e., the border is
1771 inclusive). Note that this set is only complete for paths in the CFG
1772 starting at ENTRY_BLOCK, and that there is no exit block recorded for
1773 the edge to the "over" label. */
1774 bitmap exit_blocks;
1776 /* The set of all blocks that have an TM_IRREVOCABLE call. */
1777 bitmap irr_blocks;
1780 typedef struct tm_region *tm_region_p;
1781 DEF_VEC_P (tm_region_p);
1782 DEF_VEC_ALLOC_P (tm_region_p, heap);
1784 /* True if there are pending edge statements to be committed for the
1785 current function being scanned in the tmmark pass. */
1786 bool pending_edge_inserts_p;
1788 static struct tm_region *all_tm_regions;
1789 static bitmap_obstack tm_obstack;
1792 /* A subroutine of tm_region_init. Record the existence of the
1793 GIMPLE_TRANSACTION statement in a tree of tm_region elements. */
1795 static struct tm_region *
1796 tm_region_init_0 (struct tm_region *outer, basic_block bb, gimple stmt)
1798 struct tm_region *region;
1800 region = (struct tm_region *)
1801 obstack_alloc (&tm_obstack.obstack, sizeof (struct tm_region));
1803 if (outer)
1805 region->next = outer->inner;
1806 outer->inner = region;
1808 else
1810 region->next = all_tm_regions;
1811 all_tm_regions = region;
1813 region->inner = NULL;
1814 region->outer = outer;
1816 region->transaction_stmt = stmt;
1818 /* There are either one or two edges out of the block containing
1819 the GIMPLE_TRANSACTION, one to the actual region and one to the
1820 "over" label if the region contains an abort. The former will
1821 always be the one marked FALLTHRU. */
1822 region->entry_block = FALLTHRU_EDGE (bb)->dest;
1824 region->exit_blocks = BITMAP_ALLOC (&tm_obstack);
1825 region->irr_blocks = BITMAP_ALLOC (&tm_obstack);
1827 return region;
1830 /* A subroutine of tm_region_init. Record all the exit and
1831 irrevocable blocks in BB into the region's exit_blocks and
1832 irr_blocks bitmaps. Returns the new region being scanned. */
1834 static struct tm_region *
1835 tm_region_init_1 (struct tm_region *region, basic_block bb)
1837 gimple_stmt_iterator gsi;
1838 gimple g;
1840 if (!region
1841 || (!region->irr_blocks && !region->exit_blocks))
1842 return region;
1844 /* Check to see if this is the end of a region by seeing if it
1845 contains a call to __builtin_tm_commit{,_eh}. Note that the
1846 outermost region for DECL_IS_TM_CLONE need not collect this. */
1847 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
1849 g = gsi_stmt (gsi);
1850 if (gimple_code (g) == GIMPLE_CALL)
1852 tree fn = gimple_call_fndecl (g);
1853 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
1855 if ((DECL_FUNCTION_CODE (fn) == BUILT_IN_TM_COMMIT
1856 || DECL_FUNCTION_CODE (fn) == BUILT_IN_TM_COMMIT_EH)
1857 && region->exit_blocks)
1859 bitmap_set_bit (region->exit_blocks, bb->index);
1860 region = region->outer;
1861 break;
1863 if (DECL_FUNCTION_CODE (fn) == BUILT_IN_TM_IRREVOCABLE)
1864 bitmap_set_bit (region->irr_blocks, bb->index);
1868 return region;
1871 /* Collect all of the transaction regions within the current function
1872 and record them in ALL_TM_REGIONS. The REGION parameter may specify
1873 an "outermost" region for use by tm clones. */
1875 static void
1876 tm_region_init (struct tm_region *region)
1878 gimple g;
1879 edge_iterator ei;
1880 edge e;
1881 basic_block bb;
1882 VEC(basic_block, heap) *queue = NULL;
1883 bitmap visited_blocks = BITMAP_ALLOC (NULL);
1884 struct tm_region *old_region;
1885 VEC(tm_region_p, heap) *bb_regions = NULL;
1887 all_tm_regions = region;
1888 bb = single_succ (ENTRY_BLOCK_PTR);
1890 /* We could store this information in bb->aux, but we may get called
1891 through get_all_tm_blocks() from another pass that may be already
1892 using bb->aux. */
1893 VEC_safe_grow_cleared (tm_region_p, heap, bb_regions, last_basic_block);
1895 VEC_safe_push (basic_block, heap, queue, bb);
1896 VEC_replace (tm_region_p, bb_regions, bb->index, region);
1899 bb = VEC_pop (basic_block, queue);
1900 region = VEC_index (tm_region_p, bb_regions, bb->index);
1901 VEC_replace (tm_region_p, bb_regions, bb->index, NULL);
1903 /* Record exit and irrevocable blocks. */
1904 region = tm_region_init_1 (region, bb);
1906 /* Check for the last statement in the block beginning a new region. */
1907 g = last_stmt (bb);
1908 old_region = region;
1909 if (g && gimple_code (g) == GIMPLE_TRANSACTION)
1910 region = tm_region_init_0 (region, bb, g);
1912 /* Process subsequent blocks. */
1913 FOR_EACH_EDGE (e, ei, bb->succs)
1914 if (!bitmap_bit_p (visited_blocks, e->dest->index))
1916 bitmap_set_bit (visited_blocks, e->dest->index);
1917 VEC_safe_push (basic_block, heap, queue, e->dest);
1919 /* If the current block started a new region, make sure that only
1920 the entry block of the new region is associated with this region.
1921 Other successors are still part of the old region. */
1922 if (old_region != region && e->dest != region->entry_block)
1923 VEC_replace (tm_region_p, bb_regions, e->dest->index, old_region);
1924 else
1925 VEC_replace (tm_region_p, bb_regions, e->dest->index, region);
1928 while (!VEC_empty (basic_block, queue));
1929 VEC_free (basic_block, heap, queue);
1930 BITMAP_FREE (visited_blocks);
1931 VEC_free (tm_region_p, heap, bb_regions);
1934 /* The "gate" function for all transactional memory expansion and optimization
1935 passes. We collect region information for each top-level transaction, and
1936 if we don't find any, we skip all of the TM passes. Each region will have
1937 all of the exit blocks recorded, and the originating statement. */
1939 static bool
1940 gate_tm_init (void)
1942 if (!flag_tm)
1943 return false;
1945 calculate_dominance_info (CDI_DOMINATORS);
1946 bitmap_obstack_initialize (&tm_obstack);
1948 /* If the function is a TM_CLONE, then the entire function is the region. */
1949 if (decl_is_tm_clone (current_function_decl))
1951 struct tm_region *region = (struct tm_region *)
1952 obstack_alloc (&tm_obstack.obstack, sizeof (struct tm_region));
1953 memset (region, 0, sizeof (*region));
1954 region->entry_block = single_succ (ENTRY_BLOCK_PTR);
1955 /* For a clone, the entire function is the region. But even if
1956 we don't need to record any exit blocks, we may need to
1957 record irrevocable blocks. */
1958 region->irr_blocks = BITMAP_ALLOC (&tm_obstack);
1960 tm_region_init (region);
1962 else
1964 tm_region_init (NULL);
1966 /* If we didn't find any regions, cleanup and skip the whole tree
1967 of tm-related optimizations. */
1968 if (all_tm_regions == NULL)
1970 bitmap_obstack_release (&tm_obstack);
1971 return false;
1975 return true;
1978 struct gimple_opt_pass pass_tm_init =
1981 GIMPLE_PASS,
1982 "*tminit", /* name */
1983 gate_tm_init, /* gate */
1984 NULL, /* execute */
1985 NULL, /* sub */
1986 NULL, /* next */
1987 0, /* static_pass_number */
1988 TV_TRANS_MEM, /* tv_id */
1989 PROP_ssa | PROP_cfg, /* properties_required */
1990 0, /* properties_provided */
1991 0, /* properties_destroyed */
1992 0, /* todo_flags_start */
1993 0, /* todo_flags_finish */
1997 /* Add FLAGS to the GIMPLE_TRANSACTION subcode for the transaction region
1998 represented by STATE. */
2000 static inline void
2001 transaction_subcode_ior (struct tm_region *region, unsigned flags)
2003 if (region && region->transaction_stmt)
2005 flags |= gimple_transaction_subcode (region->transaction_stmt);
2006 gimple_transaction_set_subcode (region->transaction_stmt, flags);
2010 /* Construct a memory load in a transactional context. Return the
2011 gimple statement performing the load, or NULL if there is no
2012 TM_LOAD builtin of the appropriate size to do the load.
2014 LOC is the location to use for the new statement(s). */
2016 static gimple
2017 build_tm_load (location_t loc, tree lhs, tree rhs, gimple_stmt_iterator *gsi)
2019 enum built_in_function code = END_BUILTINS;
2020 tree t, type = TREE_TYPE (rhs), decl;
2021 gimple gcall;
2023 if (type == float_type_node)
2024 code = BUILT_IN_TM_LOAD_FLOAT;
2025 else if (type == double_type_node)
2026 code = BUILT_IN_TM_LOAD_DOUBLE;
2027 else if (type == long_double_type_node)
2028 code = BUILT_IN_TM_LOAD_LDOUBLE;
2029 else if (TYPE_SIZE_UNIT (type) != NULL
2030 && host_integerp (TYPE_SIZE_UNIT (type), 1))
2032 switch (tree_low_cst (TYPE_SIZE_UNIT (type), 1))
2034 case 1:
2035 code = BUILT_IN_TM_LOAD_1;
2036 break;
2037 case 2:
2038 code = BUILT_IN_TM_LOAD_2;
2039 break;
2040 case 4:
2041 code = BUILT_IN_TM_LOAD_4;
2042 break;
2043 case 8:
2044 code = BUILT_IN_TM_LOAD_8;
2045 break;
2049 if (code == END_BUILTINS)
2051 decl = targetm.vectorize.builtin_tm_load (type);
2052 if (!decl)
2053 return NULL;
2055 else
2056 decl = builtin_decl_explicit (code);
2058 t = gimplify_addr (gsi, rhs);
2059 gcall = gimple_build_call (decl, 1, t);
2060 gimple_set_location (gcall, loc);
2062 t = TREE_TYPE (TREE_TYPE (decl));
2063 if (useless_type_conversion_p (type, t))
2065 gimple_call_set_lhs (gcall, lhs);
2066 gsi_insert_before (gsi, gcall, GSI_SAME_STMT);
2068 else
2070 gimple g;
2071 tree temp;
2073 temp = create_tmp_reg (t, NULL);
2074 gimple_call_set_lhs (gcall, temp);
2075 gsi_insert_before (gsi, gcall, GSI_SAME_STMT);
2077 t = fold_build1 (VIEW_CONVERT_EXPR, type, temp);
2078 g = gimple_build_assign (lhs, t);
2079 gsi_insert_before (gsi, g, GSI_SAME_STMT);
2082 return gcall;
2086 /* Similarly for storing TYPE in a transactional context. */
2088 static gimple
2089 build_tm_store (location_t loc, tree lhs, tree rhs, gimple_stmt_iterator *gsi)
2091 enum built_in_function code = END_BUILTINS;
2092 tree t, fn, type = TREE_TYPE (rhs), simple_type;
2093 gimple gcall;
2095 if (type == float_type_node)
2096 code = BUILT_IN_TM_STORE_FLOAT;
2097 else if (type == double_type_node)
2098 code = BUILT_IN_TM_STORE_DOUBLE;
2099 else if (type == long_double_type_node)
2100 code = BUILT_IN_TM_STORE_LDOUBLE;
2101 else if (TYPE_SIZE_UNIT (type) != NULL
2102 && host_integerp (TYPE_SIZE_UNIT (type), 1))
2104 switch (tree_low_cst (TYPE_SIZE_UNIT (type), 1))
2106 case 1:
2107 code = BUILT_IN_TM_STORE_1;
2108 break;
2109 case 2:
2110 code = BUILT_IN_TM_STORE_2;
2111 break;
2112 case 4:
2113 code = BUILT_IN_TM_STORE_4;
2114 break;
2115 case 8:
2116 code = BUILT_IN_TM_STORE_8;
2117 break;
2121 if (code == END_BUILTINS)
2123 fn = targetm.vectorize.builtin_tm_store (type);
2124 if (!fn)
2125 return NULL;
2127 else
2128 fn = builtin_decl_explicit (code);
2130 simple_type = TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn))));
2132 if (TREE_CODE (rhs) == CONSTRUCTOR)
2134 /* Handle the easy initialization to zero. */
2135 if (CONSTRUCTOR_ELTS (rhs) == 0)
2136 rhs = build_int_cst (simple_type, 0);
2137 else
2139 /* ...otherwise punt to the caller and probably use
2140 BUILT_IN_TM_MEMMOVE, because we can't wrap a
2141 VIEW_CONVERT_EXPR around a CONSTRUCTOR (below) and produce
2142 valid gimple. */
2143 return NULL;
2146 else if (!useless_type_conversion_p (simple_type, type))
2148 gimple g;
2149 tree temp;
2151 temp = create_tmp_reg (simple_type, NULL);
2152 t = fold_build1 (VIEW_CONVERT_EXPR, simple_type, rhs);
2153 g = gimple_build_assign (temp, t);
2154 gimple_set_location (g, loc);
2155 gsi_insert_before (gsi, g, GSI_SAME_STMT);
2157 rhs = temp;
2160 t = gimplify_addr (gsi, lhs);
2161 gcall = gimple_build_call (fn, 2, t, rhs);
2162 gimple_set_location (gcall, loc);
2163 gsi_insert_before (gsi, gcall, GSI_SAME_STMT);
2165 return gcall;
2169 /* Expand an assignment statement into transactional builtins. */
2171 static void
2172 expand_assign_tm (struct tm_region *region, gimple_stmt_iterator *gsi)
2174 gimple stmt = gsi_stmt (*gsi);
2175 location_t loc = gimple_location (stmt);
2176 tree lhs = gimple_assign_lhs (stmt);
2177 tree rhs = gimple_assign_rhs1 (stmt);
2178 bool store_p = requires_barrier (region->entry_block, lhs, NULL);
2179 bool load_p = requires_barrier (region->entry_block, rhs, NULL);
2180 gimple gcall = NULL;
2182 if (!load_p && !store_p)
2184 /* Add thread private addresses to log if applicable. */
2185 requires_barrier (region->entry_block, lhs, stmt);
2186 gsi_next (gsi);
2187 return;
2190 gsi_remove (gsi, true);
2192 if (load_p && !store_p)
2194 transaction_subcode_ior (region, GTMA_HAVE_LOAD);
2195 gcall = build_tm_load (loc, lhs, rhs, gsi);
2197 else if (store_p && !load_p)
2199 transaction_subcode_ior (region, GTMA_HAVE_STORE);
2200 gcall = build_tm_store (loc, lhs, rhs, gsi);
2202 if (!gcall)
2204 tree lhs_addr, rhs_addr, tmp;
2206 if (load_p)
2207 transaction_subcode_ior (region, GTMA_HAVE_LOAD);
2208 if (store_p)
2209 transaction_subcode_ior (region, GTMA_HAVE_STORE);
2211 /* ??? Figure out if there's any possible overlap between the LHS
2212 and the RHS and if not, use MEMCPY. */
2214 if (load_p && is_gimple_reg (lhs))
2216 tmp = create_tmp_var (TREE_TYPE (lhs), NULL);
2217 lhs_addr = build_fold_addr_expr (tmp);
2219 else
2221 tmp = NULL_TREE;
2222 lhs_addr = gimplify_addr (gsi, lhs);
2224 rhs_addr = gimplify_addr (gsi, rhs);
2225 gcall = gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_MEMMOVE),
2226 3, lhs_addr, rhs_addr,
2227 TYPE_SIZE_UNIT (TREE_TYPE (lhs)));
2228 gimple_set_location (gcall, loc);
2229 gsi_insert_before (gsi, gcall, GSI_SAME_STMT);
2231 if (tmp)
2233 gcall = gimple_build_assign (lhs, tmp);
2234 gsi_insert_before (gsi, gcall, GSI_SAME_STMT);
2238 /* Now that we have the load/store in its instrumented form, add
2239 thread private addresses to the log if applicable. */
2240 if (!store_p)
2241 requires_barrier (region->entry_block, lhs, gcall);
2243 /* add_stmt_to_tm_region (region, gcall); */
2247 /* Expand a call statement as appropriate for a transaction. That is,
2248 either verify that the call does not affect the transaction, or
2249 redirect the call to a clone that handles transactions, or change
2250 the transaction state to IRREVOCABLE. Return true if the call is
2251 one of the builtins that end a transaction. */
2253 static bool
2254 expand_call_tm (struct tm_region *region,
2255 gimple_stmt_iterator *gsi)
2257 gimple stmt = gsi_stmt (*gsi);
2258 tree lhs = gimple_call_lhs (stmt);
2259 tree fn_decl;
2260 struct cgraph_node *node;
2261 bool retval = false;
2263 fn_decl = gimple_call_fndecl (stmt);
2265 if (fn_decl == builtin_decl_explicit (BUILT_IN_TM_MEMCPY)
2266 || fn_decl == builtin_decl_explicit (BUILT_IN_TM_MEMMOVE))
2267 transaction_subcode_ior (region, GTMA_HAVE_STORE | GTMA_HAVE_LOAD);
2268 if (fn_decl == builtin_decl_explicit (BUILT_IN_TM_MEMSET))
2269 transaction_subcode_ior (region, GTMA_HAVE_STORE);
2271 if (is_tm_pure_call (stmt))
2272 return false;
2274 if (fn_decl)
2275 retval = is_tm_ending_fndecl (fn_decl);
2276 if (!retval)
2278 /* Assume all non-const/pure calls write to memory, except
2279 transaction ending builtins. */
2280 transaction_subcode_ior (region, GTMA_HAVE_STORE);
2283 /* For indirect calls, we already generated a call into the runtime. */
2284 if (!fn_decl)
2286 tree fn = gimple_call_fn (stmt);
2288 /* We are guaranteed never to go irrevocable on a safe or pure
2289 call, and the pure call was handled above. */
2290 if (is_tm_safe (fn))
2291 return false;
2292 else
2293 transaction_subcode_ior (region, GTMA_MAY_ENTER_IRREVOCABLE);
2295 return false;
2298 node = cgraph_get_node (fn_decl);
2299 /* All calls should have cgraph here. */
2300 gcc_assert (node);
2301 if (node->local.tm_may_enter_irr)
2302 transaction_subcode_ior (region, GTMA_MAY_ENTER_IRREVOCABLE);
2304 if (is_tm_abort (fn_decl))
2306 transaction_subcode_ior (region, GTMA_HAVE_ABORT);
2307 return true;
2310 /* Instrument the store if needed.
2312 If the assignment happens inside the function call (return slot
2313 optimization), there is no instrumentation to be done, since
2314 the callee should have done the right thing. */
2315 if (lhs && requires_barrier (region->entry_block, lhs, stmt)
2316 && !gimple_call_return_slot_opt_p (stmt))
2318 tree tmp = create_tmp_reg (TREE_TYPE (lhs), NULL);
2319 location_t loc = gimple_location (stmt);
2320 edge fallthru_edge = NULL;
2322 /* Remember if the call was going to throw. */
2323 if (stmt_can_throw_internal (stmt))
2325 edge_iterator ei;
2326 edge e;
2327 basic_block bb = gimple_bb (stmt);
2329 FOR_EACH_EDGE (e, ei, bb->succs)
2330 if (e->flags & EDGE_FALLTHRU)
2332 fallthru_edge = e;
2333 break;
2337 gimple_call_set_lhs (stmt, tmp);
2338 update_stmt (stmt);
2339 stmt = gimple_build_assign (lhs, tmp);
2340 gimple_set_location (stmt, loc);
2342 /* We cannot throw in the middle of a BB. If the call was going
2343 to throw, place the instrumentation on the fallthru edge, so
2344 the call remains the last statement in the block. */
2345 if (fallthru_edge)
2347 gimple_seq fallthru_seq = gimple_seq_alloc_with_stmt (stmt);
2348 gimple_stmt_iterator fallthru_gsi = gsi_start (fallthru_seq);
2349 expand_assign_tm (region, &fallthru_gsi);
2350 gsi_insert_seq_on_edge (fallthru_edge, fallthru_seq);
2351 pending_edge_inserts_p = true;
2353 else
2355 gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
2356 expand_assign_tm (region, gsi);
2359 transaction_subcode_ior (region, GTMA_HAVE_STORE);
2362 return retval;
2366 /* Expand all statements in BB as appropriate for being inside
2367 a transaction. */
2369 static void
2370 expand_block_tm (struct tm_region *region, basic_block bb)
2372 gimple_stmt_iterator gsi;
2374 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
2376 gimple stmt = gsi_stmt (gsi);
2377 switch (gimple_code (stmt))
2379 case GIMPLE_ASSIGN:
2380 /* Only memory reads/writes need to be instrumented. */
2381 if (gimple_assign_single_p (stmt)
2382 && !gimple_clobber_p (stmt))
2384 expand_assign_tm (region, &gsi);
2385 continue;
2387 break;
2389 case GIMPLE_CALL:
2390 if (expand_call_tm (region, &gsi))
2391 return;
2392 break;
2394 case GIMPLE_ASM:
2395 gcc_unreachable ();
2397 default:
2398 break;
2400 if (!gsi_end_p (gsi))
2401 gsi_next (&gsi);
2405 /* Return the list of basic-blocks in REGION.
2407 STOP_AT_IRREVOCABLE_P is true if caller is uninterested in blocks
2408 following a TM_IRREVOCABLE call. */
2410 static VEC (basic_block, heap) *
2411 get_tm_region_blocks (basic_block entry_block,
2412 bitmap exit_blocks,
2413 bitmap irr_blocks,
2414 bitmap all_region_blocks,
2415 bool stop_at_irrevocable_p)
2417 VEC(basic_block, heap) *bbs = NULL;
2418 unsigned i;
2419 edge e;
2420 edge_iterator ei;
2421 bitmap visited_blocks = BITMAP_ALLOC (NULL);
2423 i = 0;
2424 VEC_safe_push (basic_block, heap, bbs, entry_block);
2425 bitmap_set_bit (visited_blocks, entry_block->index);
2429 basic_block bb = VEC_index (basic_block, bbs, i++);
2431 if (exit_blocks &&
2432 bitmap_bit_p (exit_blocks, bb->index))
2433 continue;
2435 if (stop_at_irrevocable_p
2436 && irr_blocks
2437 && bitmap_bit_p (irr_blocks, bb->index))
2438 continue;
2440 FOR_EACH_EDGE (e, ei, bb->succs)
2441 if (!bitmap_bit_p (visited_blocks, e->dest->index))
2443 bitmap_set_bit (visited_blocks, e->dest->index);
2444 VEC_safe_push (basic_block, heap, bbs, e->dest);
2447 while (i < VEC_length (basic_block, bbs));
2449 if (all_region_blocks)
2450 bitmap_ior_into (all_region_blocks, visited_blocks);
2452 BITMAP_FREE (visited_blocks);
2453 return bbs;
2456 /* Set the IN_TRANSACTION for all gimple statements that appear in a
2457 transaction. */
2459 void
2460 compute_transaction_bits (void)
2462 struct tm_region *region;
2463 VEC (basic_block, heap) *queue;
2464 unsigned int i;
2465 basic_block bb;
2467 /* ?? Perhaps we need to abstract gate_tm_init further, because we
2468 certainly don't need it to calculate CDI_DOMINATOR info. */
2469 gate_tm_init ();
2471 FOR_EACH_BB (bb)
2472 bb->flags &= ~BB_IN_TRANSACTION;
2474 for (region = all_tm_regions; region; region = region->next)
2476 queue = get_tm_region_blocks (region->entry_block,
2477 region->exit_blocks,
2478 region->irr_blocks,
2479 NULL,
2480 /*stop_at_irr_p=*/true);
2481 for (i = 0; VEC_iterate (basic_block, queue, i, bb); ++i)
2482 bb->flags |= BB_IN_TRANSACTION;
2483 VEC_free (basic_block, heap, queue);
2486 if (all_tm_regions)
2487 bitmap_obstack_release (&tm_obstack);
2490 /* Entry point to the MARK phase of TM expansion. Here we replace
2491 transactional memory statements with calls to builtins, and function
2492 calls with their transactional clones (if available). But we don't
2493 yet lower GIMPLE_TRANSACTION or add the transaction restart back-edges. */
2495 static unsigned int
2496 execute_tm_mark (void)
2498 struct tm_region *region;
2499 basic_block bb;
2500 VEC (basic_block, heap) *queue;
2501 size_t i;
2503 queue = VEC_alloc (basic_block, heap, 10);
2504 pending_edge_inserts_p = false;
2506 for (region = all_tm_regions; region ; region = region->next)
2508 tm_log_init ();
2509 /* If we have a transaction... */
2510 if (region->exit_blocks)
2512 unsigned int subcode
2513 = gimple_transaction_subcode (region->transaction_stmt);
2515 /* Collect a new SUBCODE set, now that optimizations are done... */
2516 if (subcode & GTMA_DOES_GO_IRREVOCABLE)
2517 subcode &= (GTMA_DECLARATION_MASK | GTMA_DOES_GO_IRREVOCABLE
2518 | GTMA_MAY_ENTER_IRREVOCABLE);
2519 else
2520 subcode &= GTMA_DECLARATION_MASK;
2521 gimple_transaction_set_subcode (region->transaction_stmt, subcode);
2524 queue = get_tm_region_blocks (region->entry_block,
2525 region->exit_blocks,
2526 region->irr_blocks,
2527 NULL,
2528 /*stop_at_irr_p=*/true);
2529 for (i = 0; VEC_iterate (basic_block, queue, i, bb); ++i)
2530 expand_block_tm (region, bb);
2531 VEC_free (basic_block, heap, queue);
2533 tm_log_emit ();
2536 if (pending_edge_inserts_p)
2537 gsi_commit_edge_inserts ();
2538 return 0;
2541 struct gimple_opt_pass pass_tm_mark =
2544 GIMPLE_PASS,
2545 "tmmark", /* name */
2546 NULL, /* gate */
2547 execute_tm_mark, /* execute */
2548 NULL, /* sub */
2549 NULL, /* next */
2550 0, /* static_pass_number */
2551 TV_TRANS_MEM, /* tv_id */
2552 PROP_ssa | PROP_cfg, /* properties_required */
2553 0, /* properties_provided */
2554 0, /* properties_destroyed */
2555 0, /* todo_flags_start */
2556 TODO_update_ssa
2557 | TODO_verify_ssa, /* todo_flags_finish */
2561 /* Create an abnormal call edge from BB to the first block of the region
2562 represented by STATE. Also record the edge in the TM_RESTART map. */
2564 static inline void
2565 make_tm_edge (gimple stmt, basic_block bb, struct tm_region *region)
2567 void **slot;
2568 struct tm_restart_node *n, dummy;
2570 if (cfun->gimple_df->tm_restart == NULL)
2571 cfun->gimple_df->tm_restart = htab_create_ggc (31, struct_ptr_hash,
2572 struct_ptr_eq, ggc_free);
2574 dummy.stmt = stmt;
2575 dummy.label_or_list = gimple_block_label (region->entry_block);
2576 slot = htab_find_slot (cfun->gimple_df->tm_restart, &dummy, INSERT);
2577 n = (struct tm_restart_node *) *slot;
2578 if (n == NULL)
2580 n = ggc_alloc_tm_restart_node ();
2581 *n = dummy;
2583 else
2585 tree old = n->label_or_list;
2586 if (TREE_CODE (old) == LABEL_DECL)
2587 old = tree_cons (NULL, old, NULL);
2588 n->label_or_list = tree_cons (NULL, dummy.label_or_list, old);
2591 make_edge (bb, region->entry_block, EDGE_ABNORMAL);
2595 /* Split block BB as necessary for every builtin function we added, and
2596 wire up the abnormal back edges implied by the transaction restart. */
2598 static void
2599 expand_block_edges (struct tm_region *region, basic_block bb)
2601 gimple_stmt_iterator gsi;
2603 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
2605 bool do_next = true;
2606 gimple stmt = gsi_stmt (gsi);
2608 /* ??? TM_COMMIT (and any other tm builtin function) in a nested
2609 transaction has an abnormal edge back to the outer-most transaction
2610 (there are no nested retries), while a TM_ABORT also has an abnormal
2611 backedge to the inner-most transaction. We haven't actually saved
2612 the inner-most transaction here. We should be able to get to it
2613 via the region_nr saved on STMT, and read the transaction_stmt from
2614 that, and find the first region block from there. */
2615 /* ??? Shouldn't we split for any non-pure, non-irrevocable function? */
2616 if (gimple_code (stmt) == GIMPLE_CALL
2617 && (gimple_call_flags (stmt) & ECF_TM_BUILTIN) != 0)
2619 if (gsi_one_before_end_p (gsi))
2620 make_tm_edge (stmt, bb, region);
2621 else
2623 edge e = split_block (bb, stmt);
2624 make_tm_edge (stmt, bb, region);
2625 bb = e->dest;
2626 gsi = gsi_start_bb (bb);
2627 do_next = false;
2630 /* Delete any tail-call annotation that may have been added.
2631 The tail-call pass may have mis-identified the commit as being
2632 a candidate because we had not yet added this restart edge. */
2633 gimple_call_set_tail (stmt, false);
2636 if (do_next)
2637 gsi_next (&gsi);
2641 /* Expand the GIMPLE_TRANSACTION statement into the STM library call. */
2643 static void
2644 expand_transaction (struct tm_region *region)
2646 tree status, tm_start;
2647 basic_block atomic_bb, slice_bb;
2648 gimple_stmt_iterator gsi;
2649 tree t1, t2;
2650 gimple g;
2651 int flags, subcode;
2653 tm_start = builtin_decl_explicit (BUILT_IN_TM_START);
2654 status = create_tmp_reg (TREE_TYPE (TREE_TYPE (tm_start)), "tm_state");
2656 /* ??? There are plenty of bits here we're not computing. */
2657 subcode = gimple_transaction_subcode (region->transaction_stmt);
2658 if (subcode & GTMA_DOES_GO_IRREVOCABLE)
2659 flags = PR_DOESGOIRREVOCABLE | PR_UNINSTRUMENTEDCODE;
2660 else
2661 flags = PR_INSTRUMENTEDCODE;
2662 if ((subcode & GTMA_MAY_ENTER_IRREVOCABLE) == 0)
2663 flags |= PR_HASNOIRREVOCABLE;
2664 /* If the transaction does not have an abort in lexical scope and is not
2665 marked as an outer transaction, then it will never abort. */
2666 if ((subcode & GTMA_HAVE_ABORT) == 0
2667 && (subcode & GTMA_IS_OUTER) == 0)
2668 flags |= PR_HASNOABORT;
2669 if ((subcode & GTMA_HAVE_STORE) == 0)
2670 flags |= PR_READONLY;
2671 t2 = build_int_cst (TREE_TYPE (status), flags);
2672 g = gimple_build_call (tm_start, 1, t2);
2673 gimple_call_set_lhs (g, status);
2674 gimple_set_location (g, gimple_location (region->transaction_stmt));
2676 atomic_bb = gimple_bb (region->transaction_stmt);
2678 if (!VEC_empty (tree, tm_log_save_addresses))
2679 tm_log_emit_saves (region->entry_block, atomic_bb);
2681 gsi = gsi_last_bb (atomic_bb);
2682 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
2683 gsi_remove (&gsi, true);
2685 if (!VEC_empty (tree, tm_log_save_addresses))
2686 region->entry_block =
2687 tm_log_emit_save_or_restores (region->entry_block,
2688 A_RESTORELIVEVARIABLES,
2689 status,
2690 tm_log_emit_restores,
2691 atomic_bb,
2692 FALLTHRU_EDGE (atomic_bb),
2693 &slice_bb);
2694 else
2695 slice_bb = atomic_bb;
2697 /* If we have an ABORT statement, create a test following the start
2698 call to perform the abort. */
2699 if (gimple_transaction_label (region->transaction_stmt))
2701 edge e;
2702 basic_block test_bb;
2704 test_bb = create_empty_bb (slice_bb);
2705 if (current_loops && slice_bb->loop_father)
2706 add_bb_to_loop (test_bb, slice_bb->loop_father);
2707 if (VEC_empty (tree, tm_log_save_addresses))
2708 region->entry_block = test_bb;
2709 gsi = gsi_last_bb (test_bb);
2711 t1 = create_tmp_reg (TREE_TYPE (status), NULL);
2712 t2 = build_int_cst (TREE_TYPE (status), A_ABORTTRANSACTION);
2713 g = gimple_build_assign_with_ops (BIT_AND_EXPR, t1, status, t2);
2714 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
2716 t2 = build_int_cst (TREE_TYPE (status), 0);
2717 g = gimple_build_cond (NE_EXPR, t1, t2, NULL, NULL);
2718 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
2720 e = FALLTHRU_EDGE (slice_bb);
2721 redirect_edge_pred (e, test_bb);
2722 e->flags = EDGE_FALSE_VALUE;
2723 e->probability = PROB_ALWAYS - PROB_VERY_UNLIKELY;
2725 e = BRANCH_EDGE (atomic_bb);
2726 redirect_edge_pred (e, test_bb);
2727 e->flags = EDGE_TRUE_VALUE;
2728 e->probability = PROB_VERY_UNLIKELY;
2730 e = make_edge (slice_bb, test_bb, EDGE_FALLTHRU);
2733 /* If we've no abort, but we do have PHIs at the beginning of the atomic
2734 region, that means we've a loop at the beginning of the atomic region
2735 that shares the first block. This can cause problems with the abnormal
2736 edges we're about to add for the transaction restart. Solve this by
2737 adding a new empty block to receive the abnormal edges. */
2738 else if (phi_nodes (region->entry_block))
2740 edge e;
2741 basic_block empty_bb;
2743 region->entry_block = empty_bb = create_empty_bb (atomic_bb);
2744 if (current_loops && atomic_bb->loop_father)
2745 add_bb_to_loop (empty_bb, atomic_bb->loop_father);
2747 e = FALLTHRU_EDGE (atomic_bb);
2748 redirect_edge_pred (e, empty_bb);
2750 e = make_edge (atomic_bb, empty_bb, EDGE_FALLTHRU);
2753 /* The GIMPLE_TRANSACTION statement no longer exists. */
2754 region->transaction_stmt = NULL;
2757 static void expand_regions (struct tm_region *);
2759 /* Helper function for expand_regions. Expand REGION and recurse to
2760 the inner region. */
2762 static void
2763 expand_regions_1 (struct tm_region *region)
2765 if (region->exit_blocks)
2767 unsigned int i;
2768 basic_block bb;
2769 VEC (basic_block, heap) *queue;
2771 /* Collect the set of blocks in this region. Do this before
2772 splitting edges, so that we don't have to play with the
2773 dominator tree in the middle. */
2774 queue = get_tm_region_blocks (region->entry_block,
2775 region->exit_blocks,
2776 region->irr_blocks,
2777 NULL,
2778 /*stop_at_irr_p=*/false);
2779 expand_transaction (region);
2780 for (i = 0; VEC_iterate (basic_block, queue, i, bb); ++i)
2781 expand_block_edges (region, bb);
2782 VEC_free (basic_block, heap, queue);
2784 if (region->inner)
2785 expand_regions (region->inner);
2788 /* Expand regions starting at REGION. */
2790 static void
2791 expand_regions (struct tm_region *region)
2793 while (region)
2795 expand_regions_1 (region);
2796 region = region->next;
2800 /* Entry point to the final expansion of transactional nodes. */
2802 static unsigned int
2803 execute_tm_edges (void)
2805 expand_regions (all_tm_regions);
2806 tm_log_delete ();
2808 /* We've got to release the dominance info now, to indicate that it
2809 must be rebuilt completely. Otherwise we'll crash trying to update
2810 the SSA web in the TODO section following this pass. */
2811 free_dominance_info (CDI_DOMINATORS);
2812 bitmap_obstack_release (&tm_obstack);
2813 all_tm_regions = NULL;
2815 return 0;
2818 struct gimple_opt_pass pass_tm_edges =
2821 GIMPLE_PASS,
2822 "tmedge", /* name */
2823 NULL, /* gate */
2824 execute_tm_edges, /* execute */
2825 NULL, /* sub */
2826 NULL, /* next */
2827 0, /* static_pass_number */
2828 TV_TRANS_MEM, /* tv_id */
2829 PROP_ssa | PROP_cfg, /* properties_required */
2830 0, /* properties_provided */
2831 0, /* properties_destroyed */
2832 0, /* todo_flags_start */
2833 TODO_update_ssa
2834 | TODO_verify_ssa, /* todo_flags_finish */
2838 /* A unique TM memory operation. */
2839 typedef struct tm_memop
2841 /* Unique ID that all memory operations to the same location have. */
2842 unsigned int value_id;
2843 /* Address of load/store. */
2844 tree addr;
2845 } *tm_memop_t;
2847 /* Sets for solving data flow equations in the memory optimization pass. */
2848 struct tm_memopt_bitmaps
2850 /* Stores available to this BB upon entry. Basically, stores that
2851 dominate this BB. */
2852 bitmap store_avail_in;
2853 /* Stores available at the end of this BB. */
2854 bitmap store_avail_out;
2855 bitmap store_antic_in;
2856 bitmap store_antic_out;
2857 /* Reads available to this BB upon entry. Basically, reads that
2858 dominate this BB. */
2859 bitmap read_avail_in;
2860 /* Reads available at the end of this BB. */
2861 bitmap read_avail_out;
2862 /* Reads performed in this BB. */
2863 bitmap read_local;
2864 /* Writes performed in this BB. */
2865 bitmap store_local;
2867 /* Temporary storage for pass. */
2868 /* Is the current BB in the worklist? */
2869 bool avail_in_worklist_p;
2870 /* Have we visited this BB? */
2871 bool visited_p;
2874 static bitmap_obstack tm_memopt_obstack;
2876 /* Unique counter for TM loads and stores. Loads and stores of the
2877 same address get the same ID. */
2878 static unsigned int tm_memopt_value_id;
2879 static htab_t tm_memopt_value_numbers;
2881 #define STORE_AVAIL_IN(BB) \
2882 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_avail_in
2883 #define STORE_AVAIL_OUT(BB) \
2884 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_avail_out
2885 #define STORE_ANTIC_IN(BB) \
2886 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_antic_in
2887 #define STORE_ANTIC_OUT(BB) \
2888 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_antic_out
2889 #define READ_AVAIL_IN(BB) \
2890 ((struct tm_memopt_bitmaps *) ((BB)->aux))->read_avail_in
2891 #define READ_AVAIL_OUT(BB) \
2892 ((struct tm_memopt_bitmaps *) ((BB)->aux))->read_avail_out
2893 #define READ_LOCAL(BB) \
2894 ((struct tm_memopt_bitmaps *) ((BB)->aux))->read_local
2895 #define STORE_LOCAL(BB) \
2896 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_local
2897 #define AVAIL_IN_WORKLIST_P(BB) \
2898 ((struct tm_memopt_bitmaps *) ((BB)->aux))->avail_in_worklist_p
2899 #define BB_VISITED_P(BB) \
2900 ((struct tm_memopt_bitmaps *) ((BB)->aux))->visited_p
2902 /* Htab support. Return a hash value for a `tm_memop'. */
2903 static hashval_t
2904 tm_memop_hash (const void *p)
2906 const struct tm_memop *mem = (const struct tm_memop *) p;
2907 tree addr = mem->addr;
2908 /* We drill down to the SSA_NAME/DECL for the hash, but equality is
2909 actually done with operand_equal_p (see tm_memop_eq). */
2910 if (TREE_CODE (addr) == ADDR_EXPR)
2911 addr = TREE_OPERAND (addr, 0);
2912 return iterative_hash_expr (addr, 0);
2915 /* Htab support. Return true if two tm_memop's are the same. */
2916 static int
2917 tm_memop_eq (const void *p1, const void *p2)
2919 const struct tm_memop *mem1 = (const struct tm_memop *) p1;
2920 const struct tm_memop *mem2 = (const struct tm_memop *) p2;
2922 return operand_equal_p (mem1->addr, mem2->addr, 0);
2925 /* Given a TM load/store in STMT, return the value number for the address
2926 it accesses. */
2928 static unsigned int
2929 tm_memopt_value_number (gimple stmt, enum insert_option op)
2931 struct tm_memop tmpmem, *mem;
2932 void **slot;
2934 gcc_assert (is_tm_load (stmt) || is_tm_store (stmt));
2935 tmpmem.addr = gimple_call_arg (stmt, 0);
2936 slot = htab_find_slot (tm_memopt_value_numbers, &tmpmem, op);
2937 if (*slot)
2938 mem = (struct tm_memop *) *slot;
2939 else if (op == INSERT)
2941 mem = XNEW (struct tm_memop);
2942 *slot = mem;
2943 mem->value_id = tm_memopt_value_id++;
2944 mem->addr = tmpmem.addr;
2946 else
2947 gcc_unreachable ();
2948 return mem->value_id;
2951 /* Accumulate TM memory operations in BB into STORE_LOCAL and READ_LOCAL. */
2953 static void
2954 tm_memopt_accumulate_memops (basic_block bb)
2956 gimple_stmt_iterator gsi;
2958 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2960 gimple stmt = gsi_stmt (gsi);
2961 bitmap bits;
2962 unsigned int loc;
2964 if (is_tm_store (stmt))
2965 bits = STORE_LOCAL (bb);
2966 else if (is_tm_load (stmt))
2967 bits = READ_LOCAL (bb);
2968 else
2969 continue;
2971 loc = tm_memopt_value_number (stmt, INSERT);
2972 bitmap_set_bit (bits, loc);
2973 if (dump_file)
2975 fprintf (dump_file, "TM memopt (%s): value num=%d, BB=%d, addr=",
2976 is_tm_load (stmt) ? "LOAD" : "STORE", loc,
2977 gimple_bb (stmt)->index);
2978 print_generic_expr (dump_file, gimple_call_arg (stmt, 0), 0);
2979 fprintf (dump_file, "\n");
2984 /* Prettily dump one of the memopt sets. BITS is the bitmap to dump. */
2986 static void
2987 dump_tm_memopt_set (const char *set_name, bitmap bits)
2989 unsigned i;
2990 bitmap_iterator bi;
2991 const char *comma = "";
2993 fprintf (dump_file, "TM memopt: %s: [", set_name);
2994 EXECUTE_IF_SET_IN_BITMAP (bits, 0, i, bi)
2996 htab_iterator hi;
2997 struct tm_memop *mem;
2999 /* Yeah, yeah, yeah. Whatever. This is just for debugging. */
3000 FOR_EACH_HTAB_ELEMENT (tm_memopt_value_numbers, mem, tm_memop_t, hi)
3001 if (mem->value_id == i)
3002 break;
3003 gcc_assert (mem->value_id == i);
3004 fprintf (dump_file, "%s", comma);
3005 comma = ", ";
3006 print_generic_expr (dump_file, mem->addr, 0);
3008 fprintf (dump_file, "]\n");
3011 /* Prettily dump all of the memopt sets in BLOCKS. */
3013 static void
3014 dump_tm_memopt_sets (VEC (basic_block, heap) *blocks)
3016 size_t i;
3017 basic_block bb;
3019 for (i = 0; VEC_iterate (basic_block, blocks, i, bb); ++i)
3021 fprintf (dump_file, "------------BB %d---------\n", bb->index);
3022 dump_tm_memopt_set ("STORE_LOCAL", STORE_LOCAL (bb));
3023 dump_tm_memopt_set ("READ_LOCAL", READ_LOCAL (bb));
3024 dump_tm_memopt_set ("STORE_AVAIL_IN", STORE_AVAIL_IN (bb));
3025 dump_tm_memopt_set ("STORE_AVAIL_OUT", STORE_AVAIL_OUT (bb));
3026 dump_tm_memopt_set ("READ_AVAIL_IN", READ_AVAIL_IN (bb));
3027 dump_tm_memopt_set ("READ_AVAIL_OUT", READ_AVAIL_OUT (bb));
3031 /* Compute {STORE,READ}_AVAIL_IN for the basic block BB. */
3033 static void
3034 tm_memopt_compute_avin (basic_block bb)
3036 edge e;
3037 unsigned ix;
3039 /* Seed with the AVOUT of any predecessor. */
3040 for (ix = 0; ix < EDGE_COUNT (bb->preds); ix++)
3042 e = EDGE_PRED (bb, ix);
3043 /* Make sure we have already visited this BB, and is thus
3044 initialized.
3046 If e->src->aux is NULL, this predecessor is actually on an
3047 enclosing transaction. We only care about the current
3048 transaction, so ignore it. */
3049 if (e->src->aux && BB_VISITED_P (e->src))
3051 bitmap_copy (STORE_AVAIL_IN (bb), STORE_AVAIL_OUT (e->src));
3052 bitmap_copy (READ_AVAIL_IN (bb), READ_AVAIL_OUT (e->src));
3053 break;
3057 for (; ix < EDGE_COUNT (bb->preds); ix++)
3059 e = EDGE_PRED (bb, ix);
3060 if (e->src->aux && BB_VISITED_P (e->src))
3062 bitmap_and_into (STORE_AVAIL_IN (bb), STORE_AVAIL_OUT (e->src));
3063 bitmap_and_into (READ_AVAIL_IN (bb), READ_AVAIL_OUT (e->src));
3067 BB_VISITED_P (bb) = true;
3070 /* Compute the STORE_ANTIC_IN for the basic block BB. */
3072 static void
3073 tm_memopt_compute_antin (basic_block bb)
3075 edge e;
3076 unsigned ix;
3078 /* Seed with the ANTIC_OUT of any successor. */
3079 for (ix = 0; ix < EDGE_COUNT (bb->succs); ix++)
3081 e = EDGE_SUCC (bb, ix);
3082 /* Make sure we have already visited this BB, and is thus
3083 initialized. */
3084 if (BB_VISITED_P (e->dest))
3086 bitmap_copy (STORE_ANTIC_IN (bb), STORE_ANTIC_OUT (e->dest));
3087 break;
3091 for (; ix < EDGE_COUNT (bb->succs); ix++)
3093 e = EDGE_SUCC (bb, ix);
3094 if (BB_VISITED_P (e->dest))
3095 bitmap_and_into (STORE_ANTIC_IN (bb), STORE_ANTIC_OUT (e->dest));
3098 BB_VISITED_P (bb) = true;
3101 /* Compute the AVAIL sets for every basic block in BLOCKS.
3103 We compute {STORE,READ}_AVAIL_{OUT,IN} as follows:
3105 AVAIL_OUT[bb] = union (AVAIL_IN[bb], LOCAL[bb])
3106 AVAIL_IN[bb] = intersect (AVAIL_OUT[predecessors])
3108 This is basically what we do in lcm's compute_available(), but here
3109 we calculate two sets of sets (one for STOREs and one for READs),
3110 and we work on a region instead of the entire CFG.
3112 REGION is the TM region.
3113 BLOCKS are the basic blocks in the region. */
3115 static void
3116 tm_memopt_compute_available (struct tm_region *region,
3117 VEC (basic_block, heap) *blocks)
3119 edge e;
3120 basic_block *worklist, *qin, *qout, *qend, bb;
3121 unsigned int qlen, i;
3122 edge_iterator ei;
3123 bool changed;
3125 /* Allocate a worklist array/queue. Entries are only added to the
3126 list if they were not already on the list. So the size is
3127 bounded by the number of basic blocks in the region. */
3128 qlen = VEC_length (basic_block, blocks) - 1;
3129 qin = qout = worklist =
3130 XNEWVEC (basic_block, qlen);
3132 /* Put every block in the region on the worklist. */
3133 for (i = 0; VEC_iterate (basic_block, blocks, i, bb); ++i)
3135 /* Seed AVAIL_OUT with the LOCAL set. */
3136 bitmap_ior_into (STORE_AVAIL_OUT (bb), STORE_LOCAL (bb));
3137 bitmap_ior_into (READ_AVAIL_OUT (bb), READ_LOCAL (bb));
3139 AVAIL_IN_WORKLIST_P (bb) = true;
3140 /* No need to insert the entry block, since it has an AVIN of
3141 null, and an AVOUT that has already been seeded in. */
3142 if (bb != region->entry_block)
3143 *qin++ = bb;
3146 /* The entry block has been initialized with the local sets. */
3147 BB_VISITED_P (region->entry_block) = true;
3149 qin = worklist;
3150 qend = &worklist[qlen];
3152 /* Iterate until the worklist is empty. */
3153 while (qlen)
3155 /* Take the first entry off the worklist. */
3156 bb = *qout++;
3157 qlen--;
3159 if (qout >= qend)
3160 qout = worklist;
3162 /* This block can be added to the worklist again if necessary. */
3163 AVAIL_IN_WORKLIST_P (bb) = false;
3164 tm_memopt_compute_avin (bb);
3166 /* Note: We do not add the LOCAL sets here because we already
3167 seeded the AVAIL_OUT sets with them. */
3168 changed = bitmap_ior_into (STORE_AVAIL_OUT (bb), STORE_AVAIL_IN (bb));
3169 changed |= bitmap_ior_into (READ_AVAIL_OUT (bb), READ_AVAIL_IN (bb));
3170 if (changed
3171 && (region->exit_blocks == NULL
3172 || !bitmap_bit_p (region->exit_blocks, bb->index)))
3173 /* If the out state of this block changed, then we need to add
3174 its successors to the worklist if they are not already in. */
3175 FOR_EACH_EDGE (e, ei, bb->succs)
3176 if (!AVAIL_IN_WORKLIST_P (e->dest) && e->dest != EXIT_BLOCK_PTR)
3178 *qin++ = e->dest;
3179 AVAIL_IN_WORKLIST_P (e->dest) = true;
3180 qlen++;
3182 if (qin >= qend)
3183 qin = worklist;
3187 free (worklist);
3189 if (dump_file)
3190 dump_tm_memopt_sets (blocks);
3193 /* Compute ANTIC sets for every basic block in BLOCKS.
3195 We compute STORE_ANTIC_OUT as follows:
3197 STORE_ANTIC_OUT[bb] = union(STORE_ANTIC_IN[bb], STORE_LOCAL[bb])
3198 STORE_ANTIC_IN[bb] = intersect(STORE_ANTIC_OUT[successors])
3200 REGION is the TM region.
3201 BLOCKS are the basic blocks in the region. */
3203 static void
3204 tm_memopt_compute_antic (struct tm_region *region,
3205 VEC (basic_block, heap) *blocks)
3207 edge e;
3208 basic_block *worklist, *qin, *qout, *qend, bb;
3209 unsigned int qlen;
3210 int i;
3211 edge_iterator ei;
3213 /* Allocate a worklist array/queue. Entries are only added to the
3214 list if they were not already on the list. So the size is
3215 bounded by the number of basic blocks in the region. */
3216 qin = qout = worklist =
3217 XNEWVEC (basic_block, VEC_length (basic_block, blocks));
3219 for (qlen = 0, i = VEC_length (basic_block, blocks) - 1; i >= 0; --i)
3221 bb = VEC_index (basic_block, blocks, i);
3223 /* Seed ANTIC_OUT with the LOCAL set. */
3224 bitmap_ior_into (STORE_ANTIC_OUT (bb), STORE_LOCAL (bb));
3226 /* Put every block in the region on the worklist. */
3227 AVAIL_IN_WORKLIST_P (bb) = true;
3228 /* No need to insert exit blocks, since their ANTIC_IN is NULL,
3229 and their ANTIC_OUT has already been seeded in. */
3230 if (region->exit_blocks
3231 && !bitmap_bit_p (region->exit_blocks, bb->index))
3233 qlen++;
3234 *qin++ = bb;
3238 /* The exit blocks have been initialized with the local sets. */
3239 if (region->exit_blocks)
3241 unsigned int i;
3242 bitmap_iterator bi;
3243 EXECUTE_IF_SET_IN_BITMAP (region->exit_blocks, 0, i, bi)
3244 BB_VISITED_P (BASIC_BLOCK (i)) = true;
3247 qin = worklist;
3248 qend = &worklist[qlen];
3250 /* Iterate until the worklist is empty. */
3251 while (qlen)
3253 /* Take the first entry off the worklist. */
3254 bb = *qout++;
3255 qlen--;
3257 if (qout >= qend)
3258 qout = worklist;
3260 /* This block can be added to the worklist again if necessary. */
3261 AVAIL_IN_WORKLIST_P (bb) = false;
3262 tm_memopt_compute_antin (bb);
3264 /* Note: We do not add the LOCAL sets here because we already
3265 seeded the ANTIC_OUT sets with them. */
3266 if (bitmap_ior_into (STORE_ANTIC_OUT (bb), STORE_ANTIC_IN (bb))
3267 && bb != region->entry_block)
3268 /* If the out state of this block changed, then we need to add
3269 its predecessors to the worklist if they are not already in. */
3270 FOR_EACH_EDGE (e, ei, bb->preds)
3271 if (!AVAIL_IN_WORKLIST_P (e->src))
3273 *qin++ = e->src;
3274 AVAIL_IN_WORKLIST_P (e->src) = true;
3275 qlen++;
3277 if (qin >= qend)
3278 qin = worklist;
3282 free (worklist);
3284 if (dump_file)
3285 dump_tm_memopt_sets (blocks);
3288 /* Offsets of load variants from TM_LOAD. For example,
3289 BUILT_IN_TM_LOAD_RAR* is an offset of 1 from BUILT_IN_TM_LOAD*.
3290 See gtm-builtins.def. */
3291 #define TRANSFORM_RAR 1
3292 #define TRANSFORM_RAW 2
3293 #define TRANSFORM_RFW 3
3294 /* Offsets of store variants from TM_STORE. */
3295 #define TRANSFORM_WAR 1
3296 #define TRANSFORM_WAW 2
3298 /* Inform about a load/store optimization. */
3300 static void
3301 dump_tm_memopt_transform (gimple stmt)
3303 if (dump_file)
3305 fprintf (dump_file, "TM memopt: transforming: ");
3306 print_gimple_stmt (dump_file, stmt, 0, 0);
3307 fprintf (dump_file, "\n");
3311 /* Perform a read/write optimization. Replaces the TM builtin in STMT
3312 by a builtin that is OFFSET entries down in the builtins table in
3313 gtm-builtins.def. */
3315 static void
3316 tm_memopt_transform_stmt (unsigned int offset,
3317 gimple stmt,
3318 gimple_stmt_iterator *gsi)
3320 tree fn = gimple_call_fn (stmt);
3321 gcc_assert (TREE_CODE (fn) == ADDR_EXPR);
3322 TREE_OPERAND (fn, 0)
3323 = builtin_decl_explicit ((enum built_in_function)
3324 (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0))
3325 + offset));
3326 gimple_call_set_fn (stmt, fn);
3327 gsi_replace (gsi, stmt, true);
3328 dump_tm_memopt_transform (stmt);
3331 /* Perform the actual TM memory optimization transformations in the
3332 basic blocks in BLOCKS. */
3334 static void
3335 tm_memopt_transform_blocks (VEC (basic_block, heap) *blocks)
3337 size_t i;
3338 basic_block bb;
3339 gimple_stmt_iterator gsi;
3341 for (i = 0; VEC_iterate (basic_block, blocks, i, bb); ++i)
3343 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3345 gimple stmt = gsi_stmt (gsi);
3346 bitmap read_avail = READ_AVAIL_IN (bb);
3347 bitmap store_avail = STORE_AVAIL_IN (bb);
3348 bitmap store_antic = STORE_ANTIC_OUT (bb);
3349 unsigned int loc;
3351 if (is_tm_simple_load (stmt))
3353 loc = tm_memopt_value_number (stmt, NO_INSERT);
3354 if (store_avail && bitmap_bit_p (store_avail, loc))
3355 tm_memopt_transform_stmt (TRANSFORM_RAW, stmt, &gsi);
3356 else if (store_antic && bitmap_bit_p (store_antic, loc))
3358 tm_memopt_transform_stmt (TRANSFORM_RFW, stmt, &gsi);
3359 bitmap_set_bit (store_avail, loc);
3361 else if (read_avail && bitmap_bit_p (read_avail, loc))
3362 tm_memopt_transform_stmt (TRANSFORM_RAR, stmt, &gsi);
3363 else
3364 bitmap_set_bit (read_avail, loc);
3366 else if (is_tm_simple_store (stmt))
3368 loc = tm_memopt_value_number (stmt, NO_INSERT);
3369 if (store_avail && bitmap_bit_p (store_avail, loc))
3370 tm_memopt_transform_stmt (TRANSFORM_WAW, stmt, &gsi);
3371 else
3373 if (read_avail && bitmap_bit_p (read_avail, loc))
3374 tm_memopt_transform_stmt (TRANSFORM_WAR, stmt, &gsi);
3375 bitmap_set_bit (store_avail, loc);
3382 /* Return a new set of bitmaps for a BB. */
3384 static struct tm_memopt_bitmaps *
3385 tm_memopt_init_sets (void)
3387 struct tm_memopt_bitmaps *b
3388 = XOBNEW (&tm_memopt_obstack.obstack, struct tm_memopt_bitmaps);
3389 b->store_avail_in = BITMAP_ALLOC (&tm_memopt_obstack);
3390 b->store_avail_out = BITMAP_ALLOC (&tm_memopt_obstack);
3391 b->store_antic_in = BITMAP_ALLOC (&tm_memopt_obstack);
3392 b->store_antic_out = BITMAP_ALLOC (&tm_memopt_obstack);
3393 b->store_avail_out = BITMAP_ALLOC (&tm_memopt_obstack);
3394 b->read_avail_in = BITMAP_ALLOC (&tm_memopt_obstack);
3395 b->read_avail_out = BITMAP_ALLOC (&tm_memopt_obstack);
3396 b->read_local = BITMAP_ALLOC (&tm_memopt_obstack);
3397 b->store_local = BITMAP_ALLOC (&tm_memopt_obstack);
3398 return b;
3401 /* Free sets computed for each BB. */
3403 static void
3404 tm_memopt_free_sets (VEC (basic_block, heap) *blocks)
3406 size_t i;
3407 basic_block bb;
3409 for (i = 0; VEC_iterate (basic_block, blocks, i, bb); ++i)
3410 bb->aux = NULL;
3413 /* Clear the visited bit for every basic block in BLOCKS. */
3415 static void
3416 tm_memopt_clear_visited (VEC (basic_block, heap) *blocks)
3418 size_t i;
3419 basic_block bb;
3421 for (i = 0; VEC_iterate (basic_block, blocks, i, bb); ++i)
3422 BB_VISITED_P (bb) = false;
3425 /* Replace TM load/stores with hints for the runtime. We handle
3426 things like read-after-write, write-after-read, read-after-read,
3427 read-for-write, etc. */
3429 static unsigned int
3430 execute_tm_memopt (void)
3432 struct tm_region *region;
3433 VEC (basic_block, heap) *bbs;
3435 tm_memopt_value_id = 0;
3436 tm_memopt_value_numbers = htab_create (10, tm_memop_hash, tm_memop_eq, free);
3438 for (region = all_tm_regions; region; region = region->next)
3440 /* All the TM stores/loads in the current region. */
3441 size_t i;
3442 basic_block bb;
3444 bitmap_obstack_initialize (&tm_memopt_obstack);
3446 /* Save all BBs for the current region. */
3447 bbs = get_tm_region_blocks (region->entry_block,
3448 region->exit_blocks,
3449 region->irr_blocks,
3450 NULL,
3451 false);
3453 /* Collect all the memory operations. */
3454 for (i = 0; VEC_iterate (basic_block, bbs, i, bb); ++i)
3456 bb->aux = tm_memopt_init_sets ();
3457 tm_memopt_accumulate_memops (bb);
3460 /* Solve data flow equations and transform each block accordingly. */
3461 tm_memopt_clear_visited (bbs);
3462 tm_memopt_compute_available (region, bbs);
3463 tm_memopt_clear_visited (bbs);
3464 tm_memopt_compute_antic (region, bbs);
3465 tm_memopt_transform_blocks (bbs);
3467 tm_memopt_free_sets (bbs);
3468 VEC_free (basic_block, heap, bbs);
3469 bitmap_obstack_release (&tm_memopt_obstack);
3470 htab_empty (tm_memopt_value_numbers);
3473 htab_delete (tm_memopt_value_numbers);
3474 return 0;
3477 static bool
3478 gate_tm_memopt (void)
3480 return flag_tm && optimize > 0;
3483 struct gimple_opt_pass pass_tm_memopt =
3486 GIMPLE_PASS,
3487 "tmmemopt", /* name */
3488 gate_tm_memopt, /* gate */
3489 execute_tm_memopt, /* execute */
3490 NULL, /* sub */
3491 NULL, /* next */
3492 0, /* static_pass_number */
3493 TV_TRANS_MEM, /* tv_id */
3494 PROP_ssa | PROP_cfg, /* properties_required */
3495 0, /* properties_provided */
3496 0, /* properties_destroyed */
3497 0, /* todo_flags_start */
3498 0, /* todo_flags_finish */
3503 /* Interprocedual analysis for the creation of transactional clones.
3504 The aim of this pass is to find which functions are referenced in
3505 a non-irrevocable transaction context, and for those over which
3506 we have control (or user directive), create a version of the
3507 function which uses only the transactional interface to reference
3508 protected memories. This analysis proceeds in several steps:
3510 (1) Collect the set of all possible transactional clones:
3512 (a) For all local public functions marked tm_callable, push
3513 it onto the tm_callee queue.
3515 (b) For all local functions, scan for calls in transaction blocks.
3516 Push the caller and callee onto the tm_caller and tm_callee
3517 queues. Count the number of callers for each callee.
3519 (c) For each local function on the callee list, assume we will
3520 create a transactional clone. Push *all* calls onto the
3521 callee queues; count the number of clone callers separately
3522 to the number of original callers.
3524 (2) Propagate irrevocable status up the dominator tree:
3526 (a) Any external function on the callee list that is not marked
3527 tm_callable is irrevocable. Push all callers of such onto
3528 a worklist.
3530 (b) For each function on the worklist, mark each block that
3531 contains an irrevocable call. Use the AND operator to
3532 propagate that mark up the dominator tree.
3534 (c) If we reach the entry block for a possible transactional
3535 clone, then the transactional clone is irrevocable, and
3536 we should not create the clone after all. Push all
3537 callers onto the worklist.
3539 (d) Place tm_irrevocable calls at the beginning of the relevant
3540 blocks. Special case here is the entry block for the entire
3541 transaction region; there we mark it GTMA_DOES_GO_IRREVOCABLE for
3542 the library to begin the region in serial mode. Decrement
3543 the call count for all callees in the irrevocable region.
3545 (3) Create the transactional clones:
3547 Any tm_callee that still has a non-zero call count is cloned.
3550 /* This structure is stored in the AUX field of each cgraph_node. */
3551 struct tm_ipa_cg_data
3553 /* The clone of the function that got created. */
3554 struct cgraph_node *clone;
3556 /* The tm regions in the normal function. */
3557 struct tm_region *all_tm_regions;
3559 /* The blocks of the normal/clone functions that contain irrevocable
3560 calls, or blocks that are post-dominated by irrevocable calls. */
3561 bitmap irrevocable_blocks_normal;
3562 bitmap irrevocable_blocks_clone;
3564 /* The blocks of the normal function that are involved in transactions. */
3565 bitmap transaction_blocks_normal;
3567 /* The number of callers to the transactional clone of this function
3568 from normal and transactional clones respectively. */
3569 unsigned tm_callers_normal;
3570 unsigned tm_callers_clone;
3572 /* True if all calls to this function's transactional clone
3573 are irrevocable. Also automatically true if the function
3574 has no transactional clone. */
3575 bool is_irrevocable;
3577 /* Flags indicating the presence of this function in various queues. */
3578 bool in_callee_queue;
3579 bool in_worklist;
3581 /* Flags indicating the kind of scan desired while in the worklist. */
3582 bool want_irr_scan_normal;
3585 typedef VEC (cgraph_node_p, heap) *cgraph_node_queue;
3587 /* Return the ipa data associated with NODE, allocating zeroed memory
3588 if necessary. TRAVERSE_ALIASES is true if we must traverse aliases
3589 and set *NODE accordingly. */
3591 static struct tm_ipa_cg_data *
3592 get_cg_data (struct cgraph_node **node, bool traverse_aliases)
3594 struct tm_ipa_cg_data *d;
3596 if (traverse_aliases && (*node)->alias)
3597 *node = cgraph_get_node ((*node)->thunk.alias);
3599 d = (struct tm_ipa_cg_data *) (*node)->symbol.aux;
3601 if (d == NULL)
3603 d = (struct tm_ipa_cg_data *)
3604 obstack_alloc (&tm_obstack.obstack, sizeof (*d));
3605 (*node)->symbol.aux = (void *) d;
3606 memset (d, 0, sizeof (*d));
3609 return d;
3612 /* Add NODE to the end of QUEUE, unless IN_QUEUE_P indicates that
3613 it is already present. */
3615 static void
3616 maybe_push_queue (struct cgraph_node *node,
3617 cgraph_node_queue *queue_p, bool *in_queue_p)
3619 if (!*in_queue_p)
3621 *in_queue_p = true;
3622 VEC_safe_push (cgraph_node_p, heap, *queue_p, node);
3626 /* A subroutine of ipa_tm_scan_calls_transaction and ipa_tm_scan_calls_clone.
3627 Queue all callees within block BB. */
3629 static void
3630 ipa_tm_scan_calls_block (cgraph_node_queue *callees_p,
3631 basic_block bb, bool for_clone)
3633 gimple_stmt_iterator gsi;
3635 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3637 gimple stmt = gsi_stmt (gsi);
3638 if (is_gimple_call (stmt) && !is_tm_pure_call (stmt))
3640 tree fndecl = gimple_call_fndecl (stmt);
3641 if (fndecl)
3643 struct tm_ipa_cg_data *d;
3644 unsigned *pcallers;
3645 struct cgraph_node *node;
3647 if (is_tm_ending_fndecl (fndecl))
3648 continue;
3649 if (find_tm_replacement_function (fndecl))
3650 continue;
3652 node = cgraph_get_node (fndecl);
3653 gcc_assert (node != NULL);
3654 d = get_cg_data (&node, true);
3656 pcallers = (for_clone ? &d->tm_callers_clone
3657 : &d->tm_callers_normal);
3658 *pcallers += 1;
3660 maybe_push_queue (node, callees_p, &d->in_callee_queue);
3666 /* Scan all calls in NODE that are within a transaction region,
3667 and push the resulting nodes into the callee queue. */
3669 static void
3670 ipa_tm_scan_calls_transaction (struct tm_ipa_cg_data *d,
3671 cgraph_node_queue *callees_p)
3673 struct tm_region *r;
3675 d->transaction_blocks_normal = BITMAP_ALLOC (&tm_obstack);
3676 d->all_tm_regions = all_tm_regions;
3678 for (r = all_tm_regions; r; r = r->next)
3680 VEC (basic_block, heap) *bbs;
3681 basic_block bb;
3682 unsigned i;
3684 bbs = get_tm_region_blocks (r->entry_block, r->exit_blocks, NULL,
3685 d->transaction_blocks_normal, false);
3687 FOR_EACH_VEC_ELT (basic_block, bbs, i, bb)
3688 ipa_tm_scan_calls_block (callees_p, bb, false);
3690 VEC_free (basic_block, heap, bbs);
3694 /* Scan all calls in NODE as if this is the transactional clone,
3695 and push the destinations into the callee queue. */
3697 static void
3698 ipa_tm_scan_calls_clone (struct cgraph_node *node,
3699 cgraph_node_queue *callees_p)
3701 struct function *fn = DECL_STRUCT_FUNCTION (node->symbol.decl);
3702 basic_block bb;
3704 FOR_EACH_BB_FN (bb, fn)
3705 ipa_tm_scan_calls_block (callees_p, bb, true);
3708 /* The function NODE has been detected to be irrevocable. Push all
3709 of its callers onto WORKLIST for the purpose of re-scanning them. */
3711 static void
3712 ipa_tm_note_irrevocable (struct cgraph_node *node,
3713 cgraph_node_queue *worklist_p)
3715 struct tm_ipa_cg_data *d = get_cg_data (&node, true);
3716 struct cgraph_edge *e;
3718 d->is_irrevocable = true;
3720 for (e = node->callers; e ; e = e->next_caller)
3722 basic_block bb;
3723 struct cgraph_node *caller;
3725 /* Don't examine recursive calls. */
3726 if (e->caller == node)
3727 continue;
3728 /* Even if we think we can go irrevocable, believe the user
3729 above all. */
3730 if (is_tm_safe_or_pure (e->caller->symbol.decl))
3731 continue;
3733 caller = e->caller;
3734 d = get_cg_data (&caller, true);
3736 /* Check if the callee is in a transactional region. If so,
3737 schedule the function for normal re-scan as well. */
3738 bb = gimple_bb (e->call_stmt);
3739 gcc_assert (bb != NULL);
3740 if (d->transaction_blocks_normal
3741 && bitmap_bit_p (d->transaction_blocks_normal, bb->index))
3742 d->want_irr_scan_normal = true;
3744 maybe_push_queue (caller, worklist_p, &d->in_worklist);
3748 /* A subroutine of ipa_tm_scan_irr_blocks; return true iff any statement
3749 within the block is irrevocable. */
3751 static bool
3752 ipa_tm_scan_irr_block (basic_block bb)
3754 gimple_stmt_iterator gsi;
3755 tree fn;
3757 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3759 gimple stmt = gsi_stmt (gsi);
3760 switch (gimple_code (stmt))
3762 case GIMPLE_CALL:
3763 if (is_tm_pure_call (stmt))
3764 break;
3766 fn = gimple_call_fn (stmt);
3768 /* Functions with the attribute are by definition irrevocable. */
3769 if (is_tm_irrevocable (fn))
3770 return true;
3772 /* For direct function calls, go ahead and check for replacement
3773 functions, or transitive irrevocable functions. For indirect
3774 functions, we'll ask the runtime. */
3775 if (TREE_CODE (fn) == ADDR_EXPR)
3777 struct tm_ipa_cg_data *d;
3778 struct cgraph_node *node;
3780 fn = TREE_OPERAND (fn, 0);
3781 if (is_tm_ending_fndecl (fn))
3782 break;
3783 if (find_tm_replacement_function (fn))
3784 break;
3786 node = cgraph_get_node(fn);
3787 d = get_cg_data (&node, true);
3789 /* Return true if irrevocable, but above all, believe
3790 the user. */
3791 if (d->is_irrevocable
3792 && !is_tm_safe_or_pure (fn))
3793 return true;
3795 break;
3797 case GIMPLE_ASM:
3798 /* ??? The Approved Method of indicating that an inline
3799 assembly statement is not relevant to the transaction
3800 is to wrap it in a __tm_waiver block. This is not
3801 yet implemented, so we can't check for it. */
3802 if (is_tm_safe (current_function_decl))
3804 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
3805 SET_EXPR_LOCATION (t, gimple_location (stmt));
3806 TREE_BLOCK (t) = gimple_block (stmt);
3807 error ("%Kasm not allowed in %<transaction_safe%> function", t);
3809 return true;
3811 default:
3812 break;
3816 return false;
3819 /* For each of the blocks seeded witin PQUEUE, walk the CFG looking
3820 for new irrevocable blocks, marking them in NEW_IRR. Don't bother
3821 scanning past OLD_IRR or EXIT_BLOCKS. */
3823 static bool
3824 ipa_tm_scan_irr_blocks (VEC (basic_block, heap) **pqueue, bitmap new_irr,
3825 bitmap old_irr, bitmap exit_blocks)
3827 bool any_new_irr = false;
3828 edge e;
3829 edge_iterator ei;
3830 bitmap visited_blocks = BITMAP_ALLOC (NULL);
3834 basic_block bb = VEC_pop (basic_block, *pqueue);
3836 /* Don't re-scan blocks we know already are irrevocable. */
3837 if (old_irr && bitmap_bit_p (old_irr, bb->index))
3838 continue;
3840 if (ipa_tm_scan_irr_block (bb))
3842 bitmap_set_bit (new_irr, bb->index);
3843 any_new_irr = true;
3845 else if (exit_blocks == NULL || !bitmap_bit_p (exit_blocks, bb->index))
3847 FOR_EACH_EDGE (e, ei, bb->succs)
3848 if (!bitmap_bit_p (visited_blocks, e->dest->index))
3850 bitmap_set_bit (visited_blocks, e->dest->index);
3851 VEC_safe_push (basic_block, heap, *pqueue, e->dest);
3855 while (!VEC_empty (basic_block, *pqueue));
3857 BITMAP_FREE (visited_blocks);
3859 return any_new_irr;
3862 /* Propagate the irrevocable property both up and down the dominator tree.
3863 BB is the current block being scanned; EXIT_BLOCKS are the edges of the
3864 TM regions; OLD_IRR are the results of a previous scan of the dominator
3865 tree which has been fully propagated; NEW_IRR is the set of new blocks
3866 which are gaining the irrevocable property during the current scan. */
3868 static void
3869 ipa_tm_propagate_irr (basic_block entry_block, bitmap new_irr,
3870 bitmap old_irr, bitmap exit_blocks)
3872 VEC (basic_block, heap) *bbs;
3873 bitmap all_region_blocks;
3875 /* If this block is in the old set, no need to rescan. */
3876 if (old_irr && bitmap_bit_p (old_irr, entry_block->index))
3877 return;
3879 all_region_blocks = BITMAP_ALLOC (&tm_obstack);
3880 bbs = get_tm_region_blocks (entry_block, exit_blocks, NULL,
3881 all_region_blocks, false);
3884 basic_block bb = VEC_pop (basic_block, bbs);
3885 bool this_irr = bitmap_bit_p (new_irr, bb->index);
3886 bool all_son_irr = false;
3887 edge_iterator ei;
3888 edge e;
3890 /* Propagate up. If my children are, I am too, but we must have
3891 at least one child that is. */
3892 if (!this_irr)
3894 FOR_EACH_EDGE (e, ei, bb->succs)
3896 if (!bitmap_bit_p (new_irr, e->dest->index))
3898 all_son_irr = false;
3899 break;
3901 else
3902 all_son_irr = true;
3904 if (all_son_irr)
3906 /* Add block to new_irr if it hasn't already been processed. */
3907 if (!old_irr || !bitmap_bit_p (old_irr, bb->index))
3909 bitmap_set_bit (new_irr, bb->index);
3910 this_irr = true;
3915 /* Propagate down to everyone we immediately dominate. */
3916 if (this_irr)
3918 basic_block son;
3919 for (son = first_dom_son (CDI_DOMINATORS, bb);
3920 son;
3921 son = next_dom_son (CDI_DOMINATORS, son))
3923 /* Make sure block is actually in a TM region, and it
3924 isn't already in old_irr. */
3925 if ((!old_irr || !bitmap_bit_p (old_irr, son->index))
3926 && bitmap_bit_p (all_region_blocks, son->index))
3927 bitmap_set_bit (new_irr, son->index);
3931 while (!VEC_empty (basic_block, bbs));
3933 BITMAP_FREE (all_region_blocks);
3934 VEC_free (basic_block, heap, bbs);
3937 static void
3938 ipa_tm_decrement_clone_counts (basic_block bb, bool for_clone)
3940 gimple_stmt_iterator gsi;
3942 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3944 gimple stmt = gsi_stmt (gsi);
3945 if (is_gimple_call (stmt) && !is_tm_pure_call (stmt))
3947 tree fndecl = gimple_call_fndecl (stmt);
3948 if (fndecl)
3950 struct tm_ipa_cg_data *d;
3951 unsigned *pcallers;
3952 struct cgraph_node *tnode;
3954 if (is_tm_ending_fndecl (fndecl))
3955 continue;
3956 if (find_tm_replacement_function (fndecl))
3957 continue;
3959 tnode = cgraph_get_node (fndecl);
3960 d = get_cg_data (&tnode, true);
3962 pcallers = (for_clone ? &d->tm_callers_clone
3963 : &d->tm_callers_normal);
3965 gcc_assert (*pcallers > 0);
3966 *pcallers -= 1;
3972 /* (Re-)Scan the transaction blocks in NODE for calls to irrevocable functions,
3973 as well as other irrevocable actions such as inline assembly. Mark all
3974 such blocks as irrevocable and decrement the number of calls to
3975 transactional clones. Return true if, for the transactional clone, the
3976 entire function is irrevocable. */
3978 static bool
3979 ipa_tm_scan_irr_function (struct cgraph_node *node, bool for_clone)
3981 struct tm_ipa_cg_data *d;
3982 bitmap new_irr, old_irr;
3983 VEC (basic_block, heap) *queue;
3984 bool ret = false;
3986 /* Builtin operators (operator new, and such). */
3987 if (DECL_STRUCT_FUNCTION (node->symbol.decl) == NULL
3988 || DECL_STRUCT_FUNCTION (node->symbol.decl)->cfg == NULL)
3989 return false;
3991 current_function_decl = node->symbol.decl;
3992 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
3993 calculate_dominance_info (CDI_DOMINATORS);
3995 d = get_cg_data (&node, true);
3996 queue = VEC_alloc (basic_block, heap, 10);
3997 new_irr = BITMAP_ALLOC (&tm_obstack);
3999 /* Scan each tm region, propagating irrevocable status through the tree. */
4000 if (for_clone)
4002 old_irr = d->irrevocable_blocks_clone;
4003 VEC_quick_push (basic_block, queue, single_succ (ENTRY_BLOCK_PTR));
4004 if (ipa_tm_scan_irr_blocks (&queue, new_irr, old_irr, NULL))
4006 ipa_tm_propagate_irr (single_succ (ENTRY_BLOCK_PTR), new_irr,
4007 old_irr, NULL);
4008 ret = bitmap_bit_p (new_irr, single_succ (ENTRY_BLOCK_PTR)->index);
4011 else
4013 struct tm_region *region;
4015 old_irr = d->irrevocable_blocks_normal;
4016 for (region = d->all_tm_regions; region; region = region->next)
4018 VEC_quick_push (basic_block, queue, region->entry_block);
4019 if (ipa_tm_scan_irr_blocks (&queue, new_irr, old_irr,
4020 region->exit_blocks))
4021 ipa_tm_propagate_irr (region->entry_block, new_irr, old_irr,
4022 region->exit_blocks);
4026 /* If we found any new irrevocable blocks, reduce the call count for
4027 transactional clones within the irrevocable blocks. Save the new
4028 set of irrevocable blocks for next time. */
4029 if (!bitmap_empty_p (new_irr))
4031 bitmap_iterator bmi;
4032 unsigned i;
4034 EXECUTE_IF_SET_IN_BITMAP (new_irr, 0, i, bmi)
4035 ipa_tm_decrement_clone_counts (BASIC_BLOCK (i), for_clone);
4037 if (old_irr)
4039 bitmap_ior_into (old_irr, new_irr);
4040 BITMAP_FREE (new_irr);
4042 else if (for_clone)
4043 d->irrevocable_blocks_clone = new_irr;
4044 else
4045 d->irrevocable_blocks_normal = new_irr;
4047 if (dump_file && new_irr)
4049 const char *dname;
4050 bitmap_iterator bmi;
4051 unsigned i;
4053 dname = lang_hooks.decl_printable_name (current_function_decl, 2);
4054 EXECUTE_IF_SET_IN_BITMAP (new_irr, 0, i, bmi)
4055 fprintf (dump_file, "%s: bb %d goes irrevocable\n", dname, i);
4058 else
4059 BITMAP_FREE (new_irr);
4061 VEC_free (basic_block, heap, queue);
4062 pop_cfun ();
4063 current_function_decl = NULL;
4065 return ret;
4068 /* Return true if, for the transactional clone of NODE, any call
4069 may enter irrevocable mode. */
4071 static bool
4072 ipa_tm_mayenterirr_function (struct cgraph_node *node)
4074 struct tm_ipa_cg_data *d;
4075 tree decl;
4076 unsigned flags;
4078 d = get_cg_data (&node, true);
4079 decl = node->symbol.decl;
4080 flags = flags_from_decl_or_type (decl);
4082 /* Handle some TM builtins. Ordinarily these aren't actually generated
4083 at this point, but handling these functions when written in by the
4084 user makes it easier to build unit tests. */
4085 if (flags & ECF_TM_BUILTIN)
4086 return false;
4088 /* Filter out all functions that are marked. */
4089 if (flags & ECF_TM_PURE)
4090 return false;
4091 if (is_tm_safe (decl))
4092 return false;
4093 if (is_tm_irrevocable (decl))
4094 return true;
4095 if (is_tm_callable (decl))
4096 return true;
4097 if (find_tm_replacement_function (decl))
4098 return true;
4100 /* If we aren't seeing the final version of the function we don't
4101 know what it will contain at runtime. */
4102 if (cgraph_function_body_availability (node) < AVAIL_AVAILABLE)
4103 return true;
4105 /* If the function must go irrevocable, then of course true. */
4106 if (d->is_irrevocable)
4107 return true;
4109 /* If there are any blocks marked irrevocable, then the function
4110 as a whole may enter irrevocable. */
4111 if (d->irrevocable_blocks_clone)
4112 return true;
4114 /* We may have previously marked this function as tm_may_enter_irr;
4115 see pass_diagnose_tm_blocks. */
4116 if (node->local.tm_may_enter_irr)
4117 return true;
4119 /* Recurse on the main body for aliases. In general, this will
4120 result in one of the bits above being set so that we will not
4121 have to recurse next time. */
4122 if (node->alias)
4123 return ipa_tm_mayenterirr_function (cgraph_get_node (node->thunk.alias));
4125 /* What remains is unmarked local functions without items that force
4126 the function to go irrevocable. */
4127 return false;
4130 /* Diagnose calls from transaction_safe functions to unmarked
4131 functions that are determined to not be safe. */
4133 static void
4134 ipa_tm_diagnose_tm_safe (struct cgraph_node *node)
4136 struct cgraph_edge *e;
4138 for (e = node->callees; e ; e = e->next_callee)
4139 if (!is_tm_callable (e->callee->symbol.decl)
4140 && e->callee->local.tm_may_enter_irr)
4141 error_at (gimple_location (e->call_stmt),
4142 "unsafe function call %qD within "
4143 "%<transaction_safe%> function", e->callee->symbol.decl);
4146 /* Diagnose call from atomic transactions to unmarked functions
4147 that are determined to not be safe. */
4149 static void
4150 ipa_tm_diagnose_transaction (struct cgraph_node *node,
4151 struct tm_region *all_tm_regions)
4153 struct tm_region *r;
4155 for (r = all_tm_regions; r ; r = r->next)
4156 if (gimple_transaction_subcode (r->transaction_stmt) & GTMA_IS_RELAXED)
4158 /* Atomic transactions can be nested inside relaxed. */
4159 if (r->inner)
4160 ipa_tm_diagnose_transaction (node, r->inner);
4162 else
4164 VEC (basic_block, heap) *bbs;
4165 gimple_stmt_iterator gsi;
4166 basic_block bb;
4167 size_t i;
4169 bbs = get_tm_region_blocks (r->entry_block, r->exit_blocks,
4170 r->irr_blocks, NULL, false);
4172 for (i = 0; VEC_iterate (basic_block, bbs, i, bb); ++i)
4173 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4175 gimple stmt = gsi_stmt (gsi);
4176 tree fndecl;
4178 if (gimple_code (stmt) == GIMPLE_ASM)
4180 error_at (gimple_location (stmt),
4181 "asm not allowed in atomic transaction");
4182 continue;
4185 if (!is_gimple_call (stmt))
4186 continue;
4187 fndecl = gimple_call_fndecl (stmt);
4189 /* Indirect function calls have been diagnosed already. */
4190 if (!fndecl)
4191 continue;
4193 /* Stop at the end of the transaction. */
4194 if (is_tm_ending_fndecl (fndecl))
4196 if (bitmap_bit_p (r->exit_blocks, bb->index))
4197 break;
4198 continue;
4201 /* Marked functions have been diagnosed already. */
4202 if (is_tm_pure_call (stmt))
4203 continue;
4204 if (is_tm_callable (fndecl))
4205 continue;
4207 if (cgraph_local_info (fndecl)->tm_may_enter_irr)
4208 error_at (gimple_location (stmt),
4209 "unsafe function call %qD within "
4210 "atomic transaction", fndecl);
4213 VEC_free (basic_block, heap, bbs);
4217 /* Return a transactional mangled name for the DECL_ASSEMBLER_NAME in
4218 OLD_DECL. The returned value is a freshly malloced pointer that
4219 should be freed by the caller. */
4221 static tree
4222 tm_mangle (tree old_asm_id)
4224 const char *old_asm_name;
4225 char *tm_name;
4226 void *alloc = NULL;
4227 struct demangle_component *dc;
4228 tree new_asm_id;
4230 /* Determine if the symbol is already a valid C++ mangled name. Do this
4231 even for C, which might be interfacing with C++ code via appropriately
4232 ugly identifiers. */
4233 /* ??? We could probably do just as well checking for "_Z" and be done. */
4234 old_asm_name = IDENTIFIER_POINTER (old_asm_id);
4235 dc = cplus_demangle_v3_components (old_asm_name, DMGL_NO_OPTS, &alloc);
4237 if (dc == NULL)
4239 char length[8];
4241 do_unencoded:
4242 sprintf (length, "%u", IDENTIFIER_LENGTH (old_asm_id));
4243 tm_name = concat ("_ZGTt", length, old_asm_name, NULL);
4245 else
4247 old_asm_name += 2; /* Skip _Z */
4249 switch (dc->type)
4251 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4252 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4253 /* Don't play silly games, you! */
4254 goto do_unencoded;
4256 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4257 /* I'd really like to know if we can ever be passed one of
4258 these from the C++ front end. The Logical Thing would
4259 seem that hidden-alias should be outer-most, so that we
4260 get hidden-alias of a transaction-clone and not vice-versa. */
4261 old_asm_name += 2;
4262 break;
4264 default:
4265 break;
4268 tm_name = concat ("_ZGTt", old_asm_name, NULL);
4270 free (alloc);
4272 new_asm_id = get_identifier (tm_name);
4273 free (tm_name);
4275 return new_asm_id;
4278 static inline void
4279 ipa_tm_mark_force_output_node (struct cgraph_node *node)
4281 cgraph_mark_force_output_node (node);
4282 /* ??? function_and_variable_visibility will reset
4283 the needed bit, without actually checking. */
4284 node->analyzed = 1;
4287 /* Callback data for ipa_tm_create_version_alias. */
4288 struct create_version_alias_info
4290 struct cgraph_node *old_node;
4291 tree new_decl;
4294 /* A subroutine of ipa_tm_create_version, called via
4295 cgraph_for_node_and_aliases. Create new tm clones for each of
4296 the existing aliases. */
4297 static bool
4298 ipa_tm_create_version_alias (struct cgraph_node *node, void *data)
4300 struct create_version_alias_info *info
4301 = (struct create_version_alias_info *)data;
4302 tree old_decl, new_decl, tm_name;
4303 struct cgraph_node *new_node;
4305 if (!node->same_body_alias)
4306 return false;
4308 old_decl = node->symbol.decl;
4309 tm_name = tm_mangle (DECL_ASSEMBLER_NAME (old_decl));
4310 new_decl = build_decl (DECL_SOURCE_LOCATION (old_decl),
4311 TREE_CODE (old_decl), tm_name,
4312 TREE_TYPE (old_decl));
4314 SET_DECL_ASSEMBLER_NAME (new_decl, tm_name);
4315 SET_DECL_RTL (new_decl, NULL);
4317 /* Based loosely on C++'s make_alias_for(). */
4318 TREE_PUBLIC (new_decl) = TREE_PUBLIC (old_decl);
4319 DECL_CONTEXT (new_decl) = DECL_CONTEXT (old_decl);
4320 DECL_LANG_SPECIFIC (new_decl) = DECL_LANG_SPECIFIC (old_decl);
4321 TREE_READONLY (new_decl) = TREE_READONLY (old_decl);
4322 DECL_EXTERNAL (new_decl) = 0;
4323 DECL_ARTIFICIAL (new_decl) = 1;
4324 TREE_ADDRESSABLE (new_decl) = 1;
4325 TREE_USED (new_decl) = 1;
4326 TREE_SYMBOL_REFERENCED (tm_name) = 1;
4328 /* Perform the same remapping to the comdat group. */
4329 if (DECL_ONE_ONLY (new_decl))
4330 DECL_COMDAT_GROUP (new_decl) = tm_mangle (DECL_COMDAT_GROUP (old_decl));
4332 new_node = cgraph_same_body_alias (NULL, new_decl, info->new_decl);
4333 new_node->tm_clone = true;
4334 new_node->symbol.externally_visible = info->old_node->symbol.externally_visible;
4335 /* ?? Do not traverse aliases here. */
4336 get_cg_data (&node, false)->clone = new_node;
4338 record_tm_clone_pair (old_decl, new_decl);
4340 if (info->old_node->symbol.force_output
4341 || ipa_ref_list_first_referring (&info->old_node->symbol.ref_list))
4342 ipa_tm_mark_force_output_node (new_node);
4343 return false;
4346 /* Create a copy of the function (possibly declaration only) of OLD_NODE,
4347 appropriate for the transactional clone. */
4349 static void
4350 ipa_tm_create_version (struct cgraph_node *old_node)
4352 tree new_decl, old_decl, tm_name;
4353 struct cgraph_node *new_node;
4355 old_decl = old_node->symbol.decl;
4356 new_decl = copy_node (old_decl);
4358 /* DECL_ASSEMBLER_NAME needs to be set before we call
4359 cgraph_copy_node_for_versioning below, because cgraph_node will
4360 fill the assembler_name_hash. */
4361 tm_name = tm_mangle (DECL_ASSEMBLER_NAME (old_decl));
4362 SET_DECL_ASSEMBLER_NAME (new_decl, tm_name);
4363 SET_DECL_RTL (new_decl, NULL);
4364 TREE_SYMBOL_REFERENCED (tm_name) = 1;
4366 /* Perform the same remapping to the comdat group. */
4367 if (DECL_ONE_ONLY (new_decl))
4368 DECL_COMDAT_GROUP (new_decl) = tm_mangle (DECL_COMDAT_GROUP (old_decl));
4370 new_node = cgraph_copy_node_for_versioning (old_node, new_decl, NULL, NULL);
4371 new_node->symbol.externally_visible = old_node->symbol.externally_visible;
4372 new_node->lowered = true;
4373 new_node->tm_clone = 1;
4374 get_cg_data (&old_node, true)->clone = new_node;
4376 if (cgraph_function_body_availability (old_node) >= AVAIL_OVERWRITABLE)
4378 /* Remap extern inline to static inline. */
4379 /* ??? Is it worth trying to use make_decl_one_only? */
4380 if (DECL_DECLARED_INLINE_P (new_decl) && DECL_EXTERNAL (new_decl))
4382 DECL_EXTERNAL (new_decl) = 0;
4383 TREE_PUBLIC (new_decl) = 0;
4384 DECL_WEAK (new_decl) = 0;
4387 tree_function_versioning (old_decl, new_decl, NULL, false, NULL, false,
4388 NULL, NULL);
4391 record_tm_clone_pair (old_decl, new_decl);
4393 cgraph_call_function_insertion_hooks (new_node);
4394 if (old_node->symbol.force_output
4395 || ipa_ref_list_first_referring (&old_node->symbol.ref_list))
4396 ipa_tm_mark_force_output_node (new_node);
4398 /* Do the same thing, but for any aliases of the original node. */
4400 struct create_version_alias_info data;
4401 data.old_node = old_node;
4402 data.new_decl = new_decl;
4403 cgraph_for_node_and_aliases (old_node, ipa_tm_create_version_alias,
4404 &data, true);
4408 /* Construct a call to TM_IRREVOCABLE and insert it at the beginning of BB. */
4410 static void
4411 ipa_tm_insert_irr_call (struct cgraph_node *node, struct tm_region *region,
4412 basic_block bb)
4414 gimple_stmt_iterator gsi;
4415 gimple g;
4417 transaction_subcode_ior (region, GTMA_MAY_ENTER_IRREVOCABLE);
4419 g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_IRREVOCABLE),
4420 1, build_int_cst (NULL_TREE, MODE_SERIALIRREVOCABLE));
4422 split_block_after_labels (bb);
4423 gsi = gsi_after_labels (bb);
4424 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
4426 cgraph_create_edge (node,
4427 cgraph_get_create_node
4428 (builtin_decl_explicit (BUILT_IN_TM_IRREVOCABLE)),
4429 g, 0,
4430 compute_call_stmt_bb_frequency (node->symbol.decl,
4431 gimple_bb (g)));
4434 /* Construct a call to TM_GETTMCLONE and insert it before GSI. */
4436 static bool
4437 ipa_tm_insert_gettmclone_call (struct cgraph_node *node,
4438 struct tm_region *region,
4439 gimple_stmt_iterator *gsi, gimple stmt)
4441 tree gettm_fn, ret, old_fn, callfn;
4442 gimple g, g2;
4443 bool safe;
4445 old_fn = gimple_call_fn (stmt);
4447 if (TREE_CODE (old_fn) == ADDR_EXPR)
4449 tree fndecl = TREE_OPERAND (old_fn, 0);
4450 tree clone = get_tm_clone_pair (fndecl);
4452 /* By transforming the call into a TM_GETTMCLONE, we are
4453 technically taking the address of the original function and
4454 its clone. Explain this so inlining will know this function
4455 is needed. */
4456 cgraph_mark_address_taken_node (cgraph_get_node (fndecl));
4457 if (clone)
4458 cgraph_mark_address_taken_node (cgraph_get_node (clone));
4461 safe = is_tm_safe (TREE_TYPE (old_fn));
4462 gettm_fn = builtin_decl_explicit (safe ? BUILT_IN_TM_GETTMCLONE_SAFE
4463 : BUILT_IN_TM_GETTMCLONE_IRR);
4464 ret = create_tmp_var (ptr_type_node, NULL);
4466 if (!safe)
4467 transaction_subcode_ior (region, GTMA_MAY_ENTER_IRREVOCABLE);
4469 /* Discard OBJ_TYPE_REF, since we weren't able to fold it. */
4470 if (TREE_CODE (old_fn) == OBJ_TYPE_REF)
4471 old_fn = OBJ_TYPE_REF_EXPR (old_fn);
4473 g = gimple_build_call (gettm_fn, 1, old_fn);
4474 ret = make_ssa_name (ret, g);
4475 gimple_call_set_lhs (g, ret);
4477 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4479 cgraph_create_edge (node, cgraph_get_create_node (gettm_fn), g, 0,
4480 compute_call_stmt_bb_frequency (node->symbol.decl,
4481 gimple_bb(g)));
4483 /* Cast return value from tm_gettmclone* into appropriate function
4484 pointer. */
4485 callfn = create_tmp_var (TREE_TYPE (old_fn), NULL);
4486 g2 = gimple_build_assign (callfn,
4487 fold_build1 (NOP_EXPR, TREE_TYPE (callfn), ret));
4488 callfn = make_ssa_name (callfn, g2);
4489 gimple_assign_set_lhs (g2, callfn);
4490 gsi_insert_before (gsi, g2, GSI_SAME_STMT);
4492 /* ??? This is a hack to preserve the NOTHROW bit on the call,
4493 which we would have derived from the decl. Failure to save
4494 this bit means we might have to split the basic block. */
4495 if (gimple_call_nothrow_p (stmt))
4496 gimple_call_set_nothrow (stmt, true);
4498 gimple_call_set_fn (stmt, callfn);
4500 /* Discarding OBJ_TYPE_REF above may produce incompatible LHS and RHS
4501 for a call statement. Fix it. */
4503 tree lhs = gimple_call_lhs (stmt);
4504 tree rettype = TREE_TYPE (gimple_call_fntype (stmt));
4505 if (lhs
4506 && !useless_type_conversion_p (TREE_TYPE (lhs), rettype))
4508 tree temp;
4510 temp = create_tmp_reg (rettype, 0);
4511 gimple_call_set_lhs (stmt, temp);
4513 g2 = gimple_build_assign (lhs,
4514 fold_build1 (VIEW_CONVERT_EXPR,
4515 TREE_TYPE (lhs), temp));
4516 gsi_insert_after (gsi, g2, GSI_SAME_STMT);
4520 update_stmt (stmt);
4522 return true;
4525 /* Helper function for ipa_tm_transform_calls*. Given a call
4526 statement in GSI which resides inside transaction REGION, redirect
4527 the call to either its wrapper function, or its clone. */
4529 static void
4530 ipa_tm_transform_calls_redirect (struct cgraph_node *node,
4531 struct tm_region *region,
4532 gimple_stmt_iterator *gsi,
4533 bool *need_ssa_rename_p)
4535 gimple stmt = gsi_stmt (*gsi);
4536 struct cgraph_node *new_node;
4537 struct cgraph_edge *e = cgraph_edge (node, stmt);
4538 tree fndecl = gimple_call_fndecl (stmt);
4540 /* For indirect calls, pass the address through the runtime. */
4541 if (fndecl == NULL)
4543 *need_ssa_rename_p |=
4544 ipa_tm_insert_gettmclone_call (node, region, gsi, stmt);
4545 return;
4548 /* Handle some TM builtins. Ordinarily these aren't actually generated
4549 at this point, but handling these functions when written in by the
4550 user makes it easier to build unit tests. */
4551 if (flags_from_decl_or_type (fndecl) & ECF_TM_BUILTIN)
4552 return;
4554 /* Fixup recursive calls inside clones. */
4555 /* ??? Why did cgraph_copy_node_for_versioning update the call edges
4556 for recursion but not update the call statements themselves? */
4557 if (e->caller == e->callee && decl_is_tm_clone (current_function_decl))
4559 gimple_call_set_fndecl (stmt, current_function_decl);
4560 return;
4563 /* If there is a replacement, use it. */
4564 fndecl = find_tm_replacement_function (fndecl);
4565 if (fndecl)
4567 new_node = cgraph_get_create_node (fndecl);
4569 /* ??? Mark all transaction_wrap functions tm_may_enter_irr.
4571 We can't do this earlier in record_tm_replacement because
4572 cgraph_remove_unreachable_nodes is called before we inject
4573 references to the node. Further, we can't do this in some
4574 nice central place in ipa_tm_execute because we don't have
4575 the exact list of wrapper functions that would be used.
4576 Marking more wrappers than necessary results in the creation
4577 of unnecessary cgraph_nodes, which can cause some of the
4578 other IPA passes to crash.
4580 We do need to mark these nodes so that we get the proper
4581 result in expand_call_tm. */
4582 /* ??? This seems broken. How is it that we're marking the
4583 CALLEE as may_enter_irr? Surely we should be marking the
4584 CALLER. Also note that find_tm_replacement_function also
4585 contains mappings into the TM runtime, e.g. memcpy. These
4586 we know won't go irrevocable. */
4587 new_node->local.tm_may_enter_irr = 1;
4589 else
4591 struct tm_ipa_cg_data *d;
4592 struct cgraph_node *tnode = e->callee;
4594 d = get_cg_data (&tnode, true);
4595 new_node = d->clone;
4597 /* As we've already skipped pure calls and appropriate builtins,
4598 and we've already marked irrevocable blocks, if we can't come
4599 up with a static replacement, then ask the runtime. */
4600 if (new_node == NULL)
4602 *need_ssa_rename_p |=
4603 ipa_tm_insert_gettmclone_call (node, region, gsi, stmt);
4604 return;
4607 fndecl = new_node->symbol.decl;
4610 cgraph_redirect_edge_callee (e, new_node);
4611 gimple_call_set_fndecl (stmt, fndecl);
4614 /* Helper function for ipa_tm_transform_calls. For a given BB,
4615 install calls to tm_irrevocable when IRR_BLOCKS are reached,
4616 redirect other calls to the generated transactional clone. */
4618 static bool
4619 ipa_tm_transform_calls_1 (struct cgraph_node *node, struct tm_region *region,
4620 basic_block bb, bitmap irr_blocks)
4622 gimple_stmt_iterator gsi;
4623 bool need_ssa_rename = false;
4625 if (irr_blocks && bitmap_bit_p (irr_blocks, bb->index))
4627 ipa_tm_insert_irr_call (node, region, bb);
4628 return true;
4631 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4633 gimple stmt = gsi_stmt (gsi);
4635 if (!is_gimple_call (stmt))
4636 continue;
4637 if (is_tm_pure_call (stmt))
4638 continue;
4640 /* Redirect edges to the appropriate replacement or clone. */
4641 ipa_tm_transform_calls_redirect (node, region, &gsi, &need_ssa_rename);
4644 return need_ssa_rename;
4647 /* Walk the CFG for REGION, beginning at BB. Install calls to
4648 tm_irrevocable when IRR_BLOCKS are reached, redirect other calls to
4649 the generated transactional clone. */
4651 static bool
4652 ipa_tm_transform_calls (struct cgraph_node *node, struct tm_region *region,
4653 basic_block bb, bitmap irr_blocks)
4655 bool need_ssa_rename = false;
4656 edge e;
4657 edge_iterator ei;
4658 VEC(basic_block, heap) *queue = NULL;
4659 bitmap visited_blocks = BITMAP_ALLOC (NULL);
4661 VEC_safe_push (basic_block, heap, queue, bb);
4664 bb = VEC_pop (basic_block, queue);
4666 need_ssa_rename |=
4667 ipa_tm_transform_calls_1 (node, region, bb, irr_blocks);
4669 if (irr_blocks && bitmap_bit_p (irr_blocks, bb->index))
4670 continue;
4672 if (region && bitmap_bit_p (region->exit_blocks, bb->index))
4673 continue;
4675 FOR_EACH_EDGE (e, ei, bb->succs)
4676 if (!bitmap_bit_p (visited_blocks, e->dest->index))
4678 bitmap_set_bit (visited_blocks, e->dest->index);
4679 VEC_safe_push (basic_block, heap, queue, e->dest);
4682 while (!VEC_empty (basic_block, queue));
4684 VEC_free (basic_block, heap, queue);
4685 BITMAP_FREE (visited_blocks);
4687 return need_ssa_rename;
4690 /* Transform the calls within the TM regions within NODE. */
4692 static void
4693 ipa_tm_transform_transaction (struct cgraph_node *node)
4695 struct tm_ipa_cg_data *d;
4696 struct tm_region *region;
4697 bool need_ssa_rename = false;
4699 d = get_cg_data (&node, true);
4701 current_function_decl = node->symbol.decl;
4702 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
4703 calculate_dominance_info (CDI_DOMINATORS);
4705 for (region = d->all_tm_regions; region; region = region->next)
4707 /* If we're sure to go irrevocable, don't transform anything. */
4708 if (d->irrevocable_blocks_normal
4709 && bitmap_bit_p (d->irrevocable_blocks_normal,
4710 region->entry_block->index))
4712 transaction_subcode_ior (region, GTMA_DOES_GO_IRREVOCABLE);
4713 transaction_subcode_ior (region, GTMA_MAY_ENTER_IRREVOCABLE);
4714 continue;
4717 need_ssa_rename |=
4718 ipa_tm_transform_calls (node, region, region->entry_block,
4719 d->irrevocable_blocks_normal);
4722 if (need_ssa_rename)
4723 update_ssa (TODO_update_ssa_only_virtuals);
4725 pop_cfun ();
4726 current_function_decl = NULL;
4729 /* Transform the calls within the transactional clone of NODE. */
4731 static void
4732 ipa_tm_transform_clone (struct cgraph_node *node)
4734 struct tm_ipa_cg_data *d;
4735 bool need_ssa_rename;
4737 d = get_cg_data (&node, true);
4739 /* If this function makes no calls and has no irrevocable blocks,
4740 then there's nothing to do. */
4741 /* ??? Remove non-aborting top-level transactions. */
4742 if (!node->callees && !node->indirect_calls && !d->irrevocable_blocks_clone)
4743 return;
4745 current_function_decl = d->clone->symbol.decl;
4746 push_cfun (DECL_STRUCT_FUNCTION (current_function_decl));
4747 calculate_dominance_info (CDI_DOMINATORS);
4749 need_ssa_rename =
4750 ipa_tm_transform_calls (d->clone, NULL, single_succ (ENTRY_BLOCK_PTR),
4751 d->irrevocable_blocks_clone);
4753 if (need_ssa_rename)
4754 update_ssa (TODO_update_ssa_only_virtuals);
4756 pop_cfun ();
4757 current_function_decl = NULL;
4760 /* Main entry point for the transactional memory IPA pass. */
4762 static unsigned int
4763 ipa_tm_execute (void)
4765 cgraph_node_queue tm_callees = NULL;
4766 /* List of functions that will go irrevocable. */
4767 cgraph_node_queue irr_worklist = NULL;
4769 struct cgraph_node *node;
4770 struct tm_ipa_cg_data *d;
4771 enum availability a;
4772 unsigned int i;
4774 #ifdef ENABLE_CHECKING
4775 verify_cgraph ();
4776 #endif
4778 bitmap_obstack_initialize (&tm_obstack);
4780 /* For all local functions marked tm_callable, queue them. */
4781 FOR_EACH_DEFINED_FUNCTION (node)
4782 if (is_tm_callable (node->symbol.decl)
4783 && cgraph_function_body_availability (node) >= AVAIL_OVERWRITABLE)
4785 d = get_cg_data (&node, true);
4786 maybe_push_queue (node, &tm_callees, &d->in_callee_queue);
4789 /* For all local reachable functions... */
4790 FOR_EACH_DEFINED_FUNCTION (node)
4791 if (node->lowered
4792 && cgraph_function_body_availability (node) >= AVAIL_OVERWRITABLE)
4794 /* ... marked tm_pure, record that fact for the runtime by
4795 indicating that the pure function is its own tm_callable.
4796 No need to do this if the function's address can't be taken. */
4797 if (is_tm_pure (node->symbol.decl))
4799 if (!node->local.local)
4800 record_tm_clone_pair (node->symbol.decl, node->symbol.decl);
4801 continue;
4804 current_function_decl = node->symbol.decl;
4805 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
4806 calculate_dominance_info (CDI_DOMINATORS);
4808 tm_region_init (NULL);
4809 if (all_tm_regions)
4811 d = get_cg_data (&node, true);
4813 /* Scan for calls that are in each transaction. */
4814 ipa_tm_scan_calls_transaction (d, &tm_callees);
4816 /* Put it in the worklist so we can scan the function
4817 later (ipa_tm_scan_irr_function) and mark the
4818 irrevocable blocks. */
4819 maybe_push_queue (node, &irr_worklist, &d->in_worklist);
4820 d->want_irr_scan_normal = true;
4823 pop_cfun ();
4824 current_function_decl = NULL;
4827 /* For every local function on the callee list, scan as if we will be
4828 creating a transactional clone, queueing all new functions we find
4829 along the way. */
4830 for (i = 0; i < VEC_length (cgraph_node_p, tm_callees); ++i)
4832 node = VEC_index (cgraph_node_p, tm_callees, i);
4833 a = cgraph_function_body_availability (node);
4834 d = get_cg_data (&node, true);
4836 /* Put it in the worklist so we can scan the function later
4837 (ipa_tm_scan_irr_function) and mark the irrevocable
4838 blocks. */
4839 maybe_push_queue (node, &irr_worklist, &d->in_worklist);
4841 /* Some callees cannot be arbitrarily cloned. These will always be
4842 irrevocable. Mark these now, so that we need not scan them. */
4843 if (is_tm_irrevocable (node->symbol.decl))
4844 ipa_tm_note_irrevocable (node, &irr_worklist);
4845 else if (a <= AVAIL_NOT_AVAILABLE
4846 && !is_tm_safe_or_pure (node->symbol.decl))
4847 ipa_tm_note_irrevocable (node, &irr_worklist);
4848 else if (a >= AVAIL_OVERWRITABLE)
4850 if (!tree_versionable_function_p (node->symbol.decl))
4851 ipa_tm_note_irrevocable (node, &irr_worklist);
4852 else if (!d->is_irrevocable)
4854 /* If this is an alias, make sure its base is queued as well.
4855 we need not scan the callees now, as the base will do. */
4856 if (node->alias)
4858 node = cgraph_get_node (node->thunk.alias);
4859 d = get_cg_data (&node, true);
4860 maybe_push_queue (node, &tm_callees, &d->in_callee_queue);
4861 continue;
4864 /* Add all nodes called by this function into
4865 tm_callees as well. */
4866 ipa_tm_scan_calls_clone (node, &tm_callees);
4871 /* Iterate scans until no more work to be done. Prefer not to use
4872 VEC_pop because the worklist tends to follow a breadth-first
4873 search of the callgraph, which should allow convergance with a
4874 minimum number of scans. But we also don't want the worklist
4875 array to grow without bound, so we shift the array up periodically. */
4876 for (i = 0; i < VEC_length (cgraph_node_p, irr_worklist); ++i)
4878 if (i > 256 && i == VEC_length (cgraph_node_p, irr_worklist) / 8)
4880 VEC_block_remove (cgraph_node_p, irr_worklist, 0, i);
4881 i = 0;
4884 node = VEC_index (cgraph_node_p, irr_worklist, i);
4885 d = get_cg_data (&node, true);
4886 d->in_worklist = false;
4888 if (d->want_irr_scan_normal)
4890 d->want_irr_scan_normal = false;
4891 ipa_tm_scan_irr_function (node, false);
4893 if (d->in_callee_queue && ipa_tm_scan_irr_function (node, true))
4894 ipa_tm_note_irrevocable (node, &irr_worklist);
4897 /* For every function on the callee list, collect the tm_may_enter_irr
4898 bit on the node. */
4899 VEC_truncate (cgraph_node_p, irr_worklist, 0);
4900 for (i = 0; i < VEC_length (cgraph_node_p, tm_callees); ++i)
4902 node = VEC_index (cgraph_node_p, tm_callees, i);
4903 if (ipa_tm_mayenterirr_function (node))
4905 d = get_cg_data (&node, true);
4906 gcc_assert (d->in_worklist == false);
4907 maybe_push_queue (node, &irr_worklist, &d->in_worklist);
4911 /* Propagate the tm_may_enter_irr bit to callers until stable. */
4912 for (i = 0; i < VEC_length (cgraph_node_p, irr_worklist); ++i)
4914 struct cgraph_node *caller;
4915 struct cgraph_edge *e;
4916 struct ipa_ref *ref;
4917 unsigned j;
4919 if (i > 256 && i == VEC_length (cgraph_node_p, irr_worklist) / 8)
4921 VEC_block_remove (cgraph_node_p, irr_worklist, 0, i);
4922 i = 0;
4925 node = VEC_index (cgraph_node_p, irr_worklist, i);
4926 d = get_cg_data (&node, true);
4927 d->in_worklist = false;
4928 node->local.tm_may_enter_irr = true;
4930 /* Propagate back to normal callers. */
4931 for (e = node->callers; e ; e = e->next_caller)
4933 caller = e->caller;
4934 if (!is_tm_safe_or_pure (caller->symbol.decl)
4935 && !caller->local.tm_may_enter_irr)
4937 d = get_cg_data (&caller, true);
4938 maybe_push_queue (caller, &irr_worklist, &d->in_worklist);
4942 /* Propagate back to referring aliases as well. */
4943 for (j = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, j, ref); j++)
4945 caller = cgraph (ref->referring);
4946 if (ref->use == IPA_REF_ALIAS
4947 && !caller->local.tm_may_enter_irr)
4949 /* ?? Do not traverse aliases here. */
4950 d = get_cg_data (&caller, false);
4951 maybe_push_queue (caller, &irr_worklist, &d->in_worklist);
4956 /* Now validate all tm_safe functions, and all atomic regions in
4957 other functions. */
4958 FOR_EACH_DEFINED_FUNCTION (node)
4959 if (node->lowered
4960 && cgraph_function_body_availability (node) >= AVAIL_OVERWRITABLE)
4962 d = get_cg_data (&node, true);
4963 if (is_tm_safe (node->symbol.decl))
4964 ipa_tm_diagnose_tm_safe (node);
4965 else if (d->all_tm_regions)
4966 ipa_tm_diagnose_transaction (node, d->all_tm_regions);
4969 /* Create clones. Do those that are not irrevocable and have a
4970 positive call count. Do those publicly visible functions that
4971 the user directed us to clone. */
4972 for (i = 0; i < VEC_length (cgraph_node_p, tm_callees); ++i)
4974 bool doit = false;
4976 node = VEC_index (cgraph_node_p, tm_callees, i);
4977 if (node->same_body_alias)
4978 continue;
4980 a = cgraph_function_body_availability (node);
4981 d = get_cg_data (&node, true);
4983 if (a <= AVAIL_NOT_AVAILABLE)
4984 doit = is_tm_callable (node->symbol.decl);
4985 else if (a <= AVAIL_AVAILABLE && is_tm_callable (node->symbol.decl))
4986 doit = true;
4987 else if (!d->is_irrevocable
4988 && d->tm_callers_normal + d->tm_callers_clone > 0)
4989 doit = true;
4991 if (doit)
4992 ipa_tm_create_version (node);
4995 /* Redirect calls to the new clones, and insert irrevocable marks. */
4996 for (i = 0; i < VEC_length (cgraph_node_p, tm_callees); ++i)
4998 node = VEC_index (cgraph_node_p, tm_callees, i);
4999 if (node->analyzed)
5001 d = get_cg_data (&node, true);
5002 if (d->clone)
5003 ipa_tm_transform_clone (node);
5006 FOR_EACH_DEFINED_FUNCTION (node)
5007 if (node->lowered
5008 && cgraph_function_body_availability (node) >= AVAIL_OVERWRITABLE)
5010 d = get_cg_data (&node, true);
5011 if (d->all_tm_regions)
5012 ipa_tm_transform_transaction (node);
5015 /* Free and clear all data structures. */
5016 VEC_free (cgraph_node_p, heap, tm_callees);
5017 VEC_free (cgraph_node_p, heap, irr_worklist);
5018 bitmap_obstack_release (&tm_obstack);
5020 FOR_EACH_FUNCTION (node)
5021 node->symbol.aux = NULL;
5023 #ifdef ENABLE_CHECKING
5024 verify_cgraph ();
5025 #endif
5027 return 0;
5030 struct simple_ipa_opt_pass pass_ipa_tm =
5033 SIMPLE_IPA_PASS,
5034 "tmipa", /* name */
5035 gate_tm, /* gate */
5036 ipa_tm_execute, /* execute */
5037 NULL, /* sub */
5038 NULL, /* next */
5039 0, /* static_pass_number */
5040 TV_TRANS_MEM, /* tv_id */
5041 PROP_ssa | PROP_cfg, /* properties_required */
5042 0, /* properties_provided */
5043 0, /* properties_destroyed */
5044 0, /* todo_flags_start */
5045 0, /* todo_flags_finish */
5049 #include "gt-trans-mem.h"