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