]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/trans-mem.c
gcc/
[thirdparty/gcc.git] / gcc / trans-mem.c
1 /* Passes for transactional memory support.
2 Copyright (C) 2008-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "alias.h"
24 #include "symtab.h"
25 #include "options.h"
26 #include "tree.h"
27 #include "fold-const.h"
28 #include "predict.h"
29 #include "tm.h"
30 #include "hard-reg-set.h"
31 #include "function.h"
32 #include "dominance.h"
33 #include "cfg.h"
34 #include "basic-block.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
37 #include "tree-eh.h"
38 #include "gimple-expr.h"
39 #include "gimple.h"
40 #include "calls.h"
41 #include "rtl.h"
42 #include "emit-rtl.h"
43 #include "gimplify.h"
44 #include "gimple-iterator.h"
45 #include "gimplify-me.h"
46 #include "gimple-walk.h"
47 #include "gimple-ssa.h"
48 #include "plugin-api.h"
49 #include "ipa-ref.h"
50 #include "cgraph.h"
51 #include "tree-cfg.h"
52 #include "stringpool.h"
53 #include "tree-ssanames.h"
54 #include "tree-into-ssa.h"
55 #include "tree-pass.h"
56 #include "tree-inline.h"
57 #include "diagnostic-core.h"
58 #include "demangle.h"
59 #include "output.h"
60 #include "trans-mem.h"
61 #include "params.h"
62 #include "target.h"
63 #include "langhooks.h"
64 #include "gimple-pretty-print.h"
65 #include "cfgloop.h"
66 #include "tree-ssa-address.h"
67
68
69 #define A_RUNINSTRUMENTEDCODE 0x0001
70 #define A_RUNUNINSTRUMENTEDCODE 0x0002
71 #define A_SAVELIVEVARIABLES 0x0004
72 #define A_RESTORELIVEVARIABLES 0x0008
73 #define A_ABORTTRANSACTION 0x0010
74
75 #define AR_USERABORT 0x0001
76 #define AR_USERRETRY 0x0002
77 #define AR_TMCONFLICT 0x0004
78 #define AR_EXCEPTIONBLOCKABORT 0x0008
79 #define AR_OUTERABORT 0x0010
80
81 #define MODE_SERIALIRREVOCABLE 0x0000
82
83
84 /* The representation of a transaction changes several times during the
85 lowering process. In the beginning, in the front-end we have the
86 GENERIC tree TRANSACTION_EXPR. For example,
87
88 __transaction {
89 local++;
90 if (++global == 10)
91 __tm_abort;
92 }
93
94 During initial gimplification (gimplify.c) the TRANSACTION_EXPR node is
95 trivially replaced with a GIMPLE_TRANSACTION node.
96
97 During pass_lower_tm, we examine the body of transactions looking
98 for aborts. Transactions that do not contain an abort may be
99 merged into an outer transaction. We also add a TRY-FINALLY node
100 to arrange for the transaction to be committed on any exit.
101
102 [??? Think about how this arrangement affects throw-with-commit
103 and throw-with-abort operations. In this case we want the TRY to
104 handle gotos, but not to catch any exceptions because the transaction
105 will already be closed.]
106
107 GIMPLE_TRANSACTION [label=NULL] {
108 try {
109 local = local + 1;
110 t0 = global;
111 t1 = t0 + 1;
112 global = t1;
113 if (t1 == 10)
114 __builtin___tm_abort ();
115 } finally {
116 __builtin___tm_commit ();
117 }
118 }
119
120 During pass_lower_eh, we create EH regions for the transactions,
121 intermixed with the regular EH stuff. This gives us a nice persistent
122 mapping (all the way through rtl) from transactional memory operation
123 back to the transaction, which allows us to get the abnormal edges
124 correct to model transaction aborts and restarts:
125
126 GIMPLE_TRANSACTION [label=over]
127 local = local + 1;
128 t0 = global;
129 t1 = t0 + 1;
130 global = t1;
131 if (t1 == 10)
132 __builtin___tm_abort ();
133 __builtin___tm_commit ();
134 over:
135
136 This is the end of all_lowering_passes, and so is what is present
137 during the IPA passes, and through all of the optimization passes.
138
139 During pass_ipa_tm, we examine all GIMPLE_TRANSACTION blocks in all
140 functions and mark functions for cloning.
141
142 At the end of gimple optimization, before exiting SSA form,
143 pass_tm_edges replaces statements that perform transactional
144 memory operations with the appropriate TM builtins, and swap
145 out function calls with their transactional clones. At this
146 point we introduce the abnormal transaction restart edges and
147 complete lowering of the GIMPLE_TRANSACTION node.
148
149 x = __builtin___tm_start (MAY_ABORT);
150 eh_label:
151 if (x & abort_transaction)
152 goto over;
153 local = local + 1;
154 t0 = __builtin___tm_load (global);
155 t1 = t0 + 1;
156 __builtin___tm_store (&global, t1);
157 if (t1 == 10)
158 __builtin___tm_abort ();
159 __builtin___tm_commit ();
160 over:
161 */
162
163 static void *expand_regions (struct tm_region *,
164 void *(*callback)(struct tm_region *, void *),
165 void *, bool);
166
167 \f
168 /* Return the attributes we want to examine for X, or NULL if it's not
169 something we examine. We look at function types, but allow pointers
170 to function types and function decls and peek through. */
171
172 static tree
173 get_attrs_for (const_tree x)
174 {
175 if (x == NULL_TREE)
176 return NULL_TREE;
177
178 switch (TREE_CODE (x))
179 {
180 case FUNCTION_DECL:
181 return TYPE_ATTRIBUTES (TREE_TYPE (x));
182 break;
183
184 default:
185 if (TYPE_P (x))
186 return NULL_TREE;
187 x = TREE_TYPE (x);
188 if (TREE_CODE (x) != POINTER_TYPE)
189 return NULL_TREE;
190 /* FALLTHRU */
191
192 case POINTER_TYPE:
193 x = TREE_TYPE (x);
194 if (TREE_CODE (x) != FUNCTION_TYPE && TREE_CODE (x) != METHOD_TYPE)
195 return NULL_TREE;
196 /* FALLTHRU */
197
198 case FUNCTION_TYPE:
199 case METHOD_TYPE:
200 return TYPE_ATTRIBUTES (x);
201 }
202 }
203
204 /* Return true if X has been marked TM_PURE. */
205
206 bool
207 is_tm_pure (const_tree x)
208 {
209 unsigned flags;
210
211 switch (TREE_CODE (x))
212 {
213 case FUNCTION_DECL:
214 case FUNCTION_TYPE:
215 case METHOD_TYPE:
216 break;
217
218 default:
219 if (TYPE_P (x))
220 return false;
221 x = TREE_TYPE (x);
222 if (TREE_CODE (x) != POINTER_TYPE)
223 return false;
224 /* FALLTHRU */
225
226 case POINTER_TYPE:
227 x = TREE_TYPE (x);
228 if (TREE_CODE (x) != FUNCTION_TYPE && TREE_CODE (x) != METHOD_TYPE)
229 return false;
230 break;
231 }
232
233 flags = flags_from_decl_or_type (x);
234 return (flags & ECF_TM_PURE) != 0;
235 }
236
237 /* Return true if X has been marked TM_IRREVOCABLE. */
238
239 static bool
240 is_tm_irrevocable (tree x)
241 {
242 tree attrs = get_attrs_for (x);
243
244 if (attrs && lookup_attribute ("transaction_unsafe", attrs))
245 return true;
246
247 /* A call to the irrevocable builtin is by definition,
248 irrevocable. */
249 if (TREE_CODE (x) == ADDR_EXPR)
250 x = TREE_OPERAND (x, 0);
251 if (TREE_CODE (x) == FUNCTION_DECL
252 && DECL_BUILT_IN_CLASS (x) == BUILT_IN_NORMAL
253 && DECL_FUNCTION_CODE (x) == BUILT_IN_TM_IRREVOCABLE)
254 return true;
255
256 return false;
257 }
258
259 /* Return true if X has been marked TM_SAFE. */
260
261 bool
262 is_tm_safe (const_tree x)
263 {
264 if (flag_tm)
265 {
266 tree attrs = get_attrs_for (x);
267 if (attrs)
268 {
269 if (lookup_attribute ("transaction_safe", attrs))
270 return true;
271 if (lookup_attribute ("transaction_may_cancel_outer", attrs))
272 return true;
273 }
274 }
275 return false;
276 }
277
278 /* Return true if CALL is const, or tm_pure. */
279
280 static bool
281 is_tm_pure_call (gimple call)
282 {
283 tree fn = gimple_call_fn (call);
284
285 if (TREE_CODE (fn) == ADDR_EXPR)
286 {
287 fn = TREE_OPERAND (fn, 0);
288 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
289 }
290 else
291 fn = TREE_TYPE (fn);
292
293 return is_tm_pure (fn);
294 }
295
296 /* Return true if X has been marked TM_CALLABLE. */
297
298 static bool
299 is_tm_callable (tree x)
300 {
301 tree attrs = get_attrs_for (x);
302 if (attrs)
303 {
304 if (lookup_attribute ("transaction_callable", attrs))
305 return true;
306 if (lookup_attribute ("transaction_safe", attrs))
307 return true;
308 if (lookup_attribute ("transaction_may_cancel_outer", attrs))
309 return true;
310 }
311 return false;
312 }
313
314 /* Return true if X has been marked TRANSACTION_MAY_CANCEL_OUTER. */
315
316 bool
317 is_tm_may_cancel_outer (tree x)
318 {
319 tree attrs = get_attrs_for (x);
320 if (attrs)
321 return lookup_attribute ("transaction_may_cancel_outer", attrs) != NULL;
322 return false;
323 }
324
325 /* Return true for built in functions that "end" a transaction. */
326
327 bool
328 is_tm_ending_fndecl (tree fndecl)
329 {
330 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
331 switch (DECL_FUNCTION_CODE (fndecl))
332 {
333 case BUILT_IN_TM_COMMIT:
334 case BUILT_IN_TM_COMMIT_EH:
335 case BUILT_IN_TM_ABORT:
336 case BUILT_IN_TM_IRREVOCABLE:
337 return true;
338 default:
339 break;
340 }
341
342 return false;
343 }
344
345 /* Return true if STMT is a built in function call that "ends" a
346 transaction. */
347
348 bool
349 is_tm_ending (gimple stmt)
350 {
351 tree fndecl;
352
353 if (gimple_code (stmt) != GIMPLE_CALL)
354 return false;
355
356 fndecl = gimple_call_fndecl (stmt);
357 return (fndecl != NULL_TREE
358 && is_tm_ending_fndecl (fndecl));
359 }
360
361 /* Return true if STMT is a TM load. */
362
363 static bool
364 is_tm_load (gimple stmt)
365 {
366 tree fndecl;
367
368 if (gimple_code (stmt) != GIMPLE_CALL)
369 return false;
370
371 fndecl = gimple_call_fndecl (stmt);
372 return (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
373 && BUILTIN_TM_LOAD_P (DECL_FUNCTION_CODE (fndecl)));
374 }
375
376 /* Same as above, but for simple TM loads, that is, not the
377 after-write, after-read, etc optimized variants. */
378
379 static bool
380 is_tm_simple_load (gimple stmt)
381 {
382 tree fndecl;
383
384 if (gimple_code (stmt) != GIMPLE_CALL)
385 return false;
386
387 fndecl = gimple_call_fndecl (stmt);
388 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
389 {
390 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
391 return (fcode == BUILT_IN_TM_LOAD_1
392 || fcode == BUILT_IN_TM_LOAD_2
393 || fcode == BUILT_IN_TM_LOAD_4
394 || fcode == BUILT_IN_TM_LOAD_8
395 || fcode == BUILT_IN_TM_LOAD_FLOAT
396 || fcode == BUILT_IN_TM_LOAD_DOUBLE
397 || fcode == BUILT_IN_TM_LOAD_LDOUBLE
398 || fcode == BUILT_IN_TM_LOAD_M64
399 || fcode == BUILT_IN_TM_LOAD_M128
400 || fcode == BUILT_IN_TM_LOAD_M256);
401 }
402 return false;
403 }
404
405 /* Return true if STMT is a TM store. */
406
407 static bool
408 is_tm_store (gimple stmt)
409 {
410 tree fndecl;
411
412 if (gimple_code (stmt) != GIMPLE_CALL)
413 return false;
414
415 fndecl = gimple_call_fndecl (stmt);
416 return (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
417 && BUILTIN_TM_STORE_P (DECL_FUNCTION_CODE (fndecl)));
418 }
419
420 /* Same as above, but for simple TM stores, that is, not the
421 after-write, after-read, etc optimized variants. */
422
423 static bool
424 is_tm_simple_store (gimple stmt)
425 {
426 tree fndecl;
427
428 if (gimple_code (stmt) != GIMPLE_CALL)
429 return false;
430
431 fndecl = gimple_call_fndecl (stmt);
432 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
433 {
434 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
435 return (fcode == BUILT_IN_TM_STORE_1
436 || fcode == BUILT_IN_TM_STORE_2
437 || fcode == BUILT_IN_TM_STORE_4
438 || fcode == BUILT_IN_TM_STORE_8
439 || fcode == BUILT_IN_TM_STORE_FLOAT
440 || fcode == BUILT_IN_TM_STORE_DOUBLE
441 || fcode == BUILT_IN_TM_STORE_LDOUBLE
442 || fcode == BUILT_IN_TM_STORE_M64
443 || fcode == BUILT_IN_TM_STORE_M128
444 || fcode == BUILT_IN_TM_STORE_M256);
445 }
446 return false;
447 }
448
449 /* Return true if FNDECL is BUILT_IN_TM_ABORT. */
450
451 static bool
452 is_tm_abort (tree fndecl)
453 {
454 return (fndecl
455 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
456 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_TM_ABORT);
457 }
458
459 /* Build a GENERIC tree for a user abort. This is called by front ends
460 while transforming the __tm_abort statement. */
461
462 tree
463 build_tm_abort_call (location_t loc, bool is_outer)
464 {
465 return build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TM_ABORT), 1,
466 build_int_cst (integer_type_node,
467 AR_USERABORT
468 | (is_outer ? AR_OUTERABORT : 0)));
469 }
470 \f
471 /* Map for aribtrary function replacement under TM, as created
472 by the tm_wrap attribute. */
473
474 struct tm_wrapper_hasher : ggc_cache_hasher<tree_map *>
475 {
476 static inline hashval_t hash (tree_map *m) { return m->hash; }
477 static inline bool
478 equal (tree_map *a, tree_map *b)
479 {
480 return a->base.from == b->base.from;
481 }
482
483 static int
484 keep_cache_entry (tree_map *&m)
485 {
486 return ggc_marked_p (m->base.from);
487 }
488 };
489
490 static GTY((cache)) hash_table<tm_wrapper_hasher> *tm_wrap_map;
491
492 void
493 record_tm_replacement (tree from, tree to)
494 {
495 struct tree_map **slot, *h;
496
497 /* Do not inline wrapper functions that will get replaced in the TM
498 pass.
499
500 Suppose you have foo() that will get replaced into tmfoo(). Make
501 sure the inliner doesn't try to outsmart us and inline foo()
502 before we get a chance to do the TM replacement. */
503 DECL_UNINLINABLE (from) = 1;
504
505 if (tm_wrap_map == NULL)
506 tm_wrap_map = hash_table<tm_wrapper_hasher>::create_ggc (32);
507
508 h = ggc_alloc<tree_map> ();
509 h->hash = htab_hash_pointer (from);
510 h->base.from = from;
511 h->to = to;
512
513 slot = tm_wrap_map->find_slot_with_hash (h, h->hash, INSERT);
514 *slot = h;
515 }
516
517 /* Return a TM-aware replacement function for DECL. */
518
519 static tree
520 find_tm_replacement_function (tree fndecl)
521 {
522 if (tm_wrap_map)
523 {
524 struct tree_map *h, in;
525
526 in.base.from = fndecl;
527 in.hash = htab_hash_pointer (fndecl);
528 h = tm_wrap_map->find_with_hash (&in, in.hash);
529 if (h)
530 return h->to;
531 }
532
533 /* ??? We may well want TM versions of most of the common <string.h>
534 functions. For now, we've already these two defined. */
535 /* Adjust expand_call_tm() attributes as necessary for the cases
536 handled here: */
537 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
538 switch (DECL_FUNCTION_CODE (fndecl))
539 {
540 case BUILT_IN_MEMCPY:
541 return builtin_decl_explicit (BUILT_IN_TM_MEMCPY);
542 case BUILT_IN_MEMMOVE:
543 return builtin_decl_explicit (BUILT_IN_TM_MEMMOVE);
544 case BUILT_IN_MEMSET:
545 return builtin_decl_explicit (BUILT_IN_TM_MEMSET);
546 default:
547 return NULL;
548 }
549
550 return NULL;
551 }
552
553 /* When appropriate, record TM replacement for memory allocation functions.
554
555 FROM is the FNDECL to wrap. */
556 void
557 tm_malloc_replacement (tree from)
558 {
559 const char *str;
560 tree to;
561
562 if (TREE_CODE (from) != FUNCTION_DECL)
563 return;
564
565 /* If we have a previous replacement, the user must be explicitly
566 wrapping malloc/calloc/free. They better know what they're
567 doing... */
568 if (find_tm_replacement_function (from))
569 return;
570
571 str = IDENTIFIER_POINTER (DECL_NAME (from));
572
573 if (!strcmp (str, "malloc"))
574 to = builtin_decl_explicit (BUILT_IN_TM_MALLOC);
575 else if (!strcmp (str, "calloc"))
576 to = builtin_decl_explicit (BUILT_IN_TM_CALLOC);
577 else if (!strcmp (str, "free"))
578 to = builtin_decl_explicit (BUILT_IN_TM_FREE);
579 else
580 return;
581
582 TREE_NOTHROW (to) = 0;
583
584 record_tm_replacement (from, to);
585 }
586 \f
587 /* Diagnostics for tm_safe functions/regions. Called by the front end
588 once we've lowered the function to high-gimple. */
589
590 /* Subroutine of diagnose_tm_safe_errors, called through walk_gimple_seq.
591 Process exactly one statement. WI->INFO is set to non-null when in
592 the context of a tm_safe function, and null for a __transaction block. */
593
594 #define DIAG_TM_OUTER 1
595 #define DIAG_TM_SAFE 2
596 #define DIAG_TM_RELAXED 4
597
598 struct diagnose_tm
599 {
600 unsigned int summary_flags : 8;
601 unsigned int block_flags : 8;
602 unsigned int func_flags : 8;
603 unsigned int saw_volatile : 1;
604 gimple stmt;
605 };
606
607 /* Return true if T is a volatile variable of some kind. */
608
609 static bool
610 volatile_var_p (tree t)
611 {
612 return (SSA_VAR_P (t)
613 && TREE_THIS_VOLATILE (TREE_TYPE (t)));
614 }
615
616 /* Tree callback function for diagnose_tm pass. */
617
618 static tree
619 diagnose_tm_1_op (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
620 void *data)
621 {
622 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
623 struct diagnose_tm *d = (struct diagnose_tm *) wi->info;
624
625 if (volatile_var_p (*tp)
626 && d->block_flags & DIAG_TM_SAFE
627 && !d->saw_volatile)
628 {
629 d->saw_volatile = 1;
630 error_at (gimple_location (d->stmt),
631 "invalid volatile use of %qD inside transaction",
632 *tp);
633 }
634
635 return NULL_TREE;
636 }
637
638 static inline bool
639 is_tm_safe_or_pure (const_tree x)
640 {
641 return is_tm_safe (x) || is_tm_pure (x);
642 }
643
644 static tree
645 diagnose_tm_1 (gimple_stmt_iterator *gsi, bool *handled_ops_p,
646 struct walk_stmt_info *wi)
647 {
648 gimple stmt = gsi_stmt (*gsi);
649 struct diagnose_tm *d = (struct diagnose_tm *) wi->info;
650
651 /* Save stmt for use in leaf analysis. */
652 d->stmt = stmt;
653
654 switch (gimple_code (stmt))
655 {
656 case GIMPLE_CALL:
657 {
658 tree fn = gimple_call_fn (stmt);
659
660 if ((d->summary_flags & DIAG_TM_OUTER) == 0
661 && is_tm_may_cancel_outer (fn))
662 error_at (gimple_location (stmt),
663 "%<transaction_may_cancel_outer%> function call not within"
664 " outer transaction or %<transaction_may_cancel_outer%>");
665
666 if (d->summary_flags & DIAG_TM_SAFE)
667 {
668 bool is_safe, direct_call_p;
669 tree replacement;
670
671 if (TREE_CODE (fn) == ADDR_EXPR
672 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL)
673 {
674 direct_call_p = true;
675 replacement = TREE_OPERAND (fn, 0);
676 replacement = find_tm_replacement_function (replacement);
677 if (replacement)
678 fn = replacement;
679 }
680 else
681 {
682 direct_call_p = false;
683 replacement = NULL_TREE;
684 }
685
686 if (is_tm_safe_or_pure (fn))
687 is_safe = true;
688 else if (is_tm_callable (fn) || is_tm_irrevocable (fn))
689 {
690 /* A function explicitly marked transaction_callable as
691 opposed to transaction_safe is being defined to be
692 unsafe as part of its ABI, regardless of its contents. */
693 is_safe = false;
694 }
695 else if (direct_call_p)
696 {
697 if (IS_TYPE_OR_DECL_P (fn)
698 && flags_from_decl_or_type (fn) & ECF_TM_BUILTIN)
699 is_safe = true;
700 else if (replacement)
701 {
702 /* ??? At present we've been considering replacements
703 merely transaction_callable, and therefore might
704 enter irrevocable. The tm_wrap attribute has not
705 yet made it into the new language spec. */
706 is_safe = false;
707 }
708 else
709 {
710 /* ??? Diagnostics for unmarked direct calls moved into
711 the IPA pass. Section 3.2 of the spec details how
712 functions not marked should be considered "implicitly
713 safe" based on having examined the function body. */
714 is_safe = true;
715 }
716 }
717 else
718 {
719 /* An unmarked indirect call. Consider it unsafe even
720 though optimization may yet figure out how to inline. */
721 is_safe = false;
722 }
723
724 if (!is_safe)
725 {
726 if (TREE_CODE (fn) == ADDR_EXPR)
727 fn = TREE_OPERAND (fn, 0);
728 if (d->block_flags & DIAG_TM_SAFE)
729 {
730 if (direct_call_p)
731 error_at (gimple_location (stmt),
732 "unsafe function call %qD within "
733 "atomic transaction", fn);
734 else
735 {
736 if (!DECL_P (fn) || DECL_NAME (fn))
737 error_at (gimple_location (stmt),
738 "unsafe function call %qE within "
739 "atomic transaction", fn);
740 else
741 error_at (gimple_location (stmt),
742 "unsafe indirect function call within "
743 "atomic transaction");
744 }
745 }
746 else
747 {
748 if (direct_call_p)
749 error_at (gimple_location (stmt),
750 "unsafe function call %qD within "
751 "%<transaction_safe%> function", fn);
752 else
753 {
754 if (!DECL_P (fn) || DECL_NAME (fn))
755 error_at (gimple_location (stmt),
756 "unsafe function call %qE within "
757 "%<transaction_safe%> function", fn);
758 else
759 error_at (gimple_location (stmt),
760 "unsafe indirect function call within "
761 "%<transaction_safe%> function");
762 }
763 }
764 }
765 }
766 }
767 break;
768
769 case GIMPLE_ASM:
770 /* ??? We ought to come up with a way to add attributes to
771 asm statements, and then add "transaction_safe" to it.
772 Either that or get the language spec to resurrect __tm_waiver. */
773 if (d->block_flags & DIAG_TM_SAFE)
774 error_at (gimple_location (stmt),
775 "asm not allowed in atomic transaction");
776 else if (d->func_flags & DIAG_TM_SAFE)
777 error_at (gimple_location (stmt),
778 "asm not allowed in %<transaction_safe%> function");
779 break;
780
781 case GIMPLE_TRANSACTION:
782 {
783 gtransaction *trans_stmt = as_a <gtransaction *> (stmt);
784 unsigned char inner_flags = DIAG_TM_SAFE;
785
786 if (gimple_transaction_subcode (trans_stmt) & GTMA_IS_RELAXED)
787 {
788 if (d->block_flags & DIAG_TM_SAFE)
789 error_at (gimple_location (stmt),
790 "relaxed transaction in atomic transaction");
791 else if (d->func_flags & DIAG_TM_SAFE)
792 error_at (gimple_location (stmt),
793 "relaxed transaction in %<transaction_safe%> function");
794 inner_flags = DIAG_TM_RELAXED;
795 }
796 else if (gimple_transaction_subcode (trans_stmt) & GTMA_IS_OUTER)
797 {
798 if (d->block_flags)
799 error_at (gimple_location (stmt),
800 "outer transaction in transaction");
801 else if (d->func_flags & DIAG_TM_OUTER)
802 error_at (gimple_location (stmt),
803 "outer transaction in "
804 "%<transaction_may_cancel_outer%> function");
805 else if (d->func_flags & DIAG_TM_SAFE)
806 error_at (gimple_location (stmt),
807 "outer transaction in %<transaction_safe%> function");
808 inner_flags |= DIAG_TM_OUTER;
809 }
810
811 *handled_ops_p = true;
812 if (gimple_transaction_body (trans_stmt))
813 {
814 struct walk_stmt_info wi_inner;
815 struct diagnose_tm d_inner;
816
817 memset (&d_inner, 0, sizeof (d_inner));
818 d_inner.func_flags = d->func_flags;
819 d_inner.block_flags = d->block_flags | inner_flags;
820 d_inner.summary_flags = d_inner.func_flags | d_inner.block_flags;
821
822 memset (&wi_inner, 0, sizeof (wi_inner));
823 wi_inner.info = &d_inner;
824
825 walk_gimple_seq (gimple_transaction_body (trans_stmt),
826 diagnose_tm_1, diagnose_tm_1_op, &wi_inner);
827 }
828 }
829 break;
830
831 default:
832 break;
833 }
834
835 return NULL_TREE;
836 }
837
838 static unsigned int
839 diagnose_tm_blocks (void)
840 {
841 struct walk_stmt_info wi;
842 struct diagnose_tm d;
843
844 memset (&d, 0, sizeof (d));
845 if (is_tm_may_cancel_outer (current_function_decl))
846 d.func_flags = DIAG_TM_OUTER | DIAG_TM_SAFE;
847 else if (is_tm_safe (current_function_decl))
848 d.func_flags = DIAG_TM_SAFE;
849 d.summary_flags = d.func_flags;
850
851 memset (&wi, 0, sizeof (wi));
852 wi.info = &d;
853
854 walk_gimple_seq (gimple_body (current_function_decl),
855 diagnose_tm_1, diagnose_tm_1_op, &wi);
856
857 return 0;
858 }
859
860 namespace {
861
862 const pass_data pass_data_diagnose_tm_blocks =
863 {
864 GIMPLE_PASS, /* type */
865 "*diagnose_tm_blocks", /* name */
866 OPTGROUP_NONE, /* optinfo_flags */
867 TV_TRANS_MEM, /* tv_id */
868 PROP_gimple_any, /* properties_required */
869 0, /* properties_provided */
870 0, /* properties_destroyed */
871 0, /* todo_flags_start */
872 0, /* todo_flags_finish */
873 };
874
875 class pass_diagnose_tm_blocks : public gimple_opt_pass
876 {
877 public:
878 pass_diagnose_tm_blocks (gcc::context *ctxt)
879 : gimple_opt_pass (pass_data_diagnose_tm_blocks, ctxt)
880 {}
881
882 /* opt_pass methods: */
883 virtual bool gate (function *) { return flag_tm; }
884 virtual unsigned int execute (function *) { return diagnose_tm_blocks (); }
885
886 }; // class pass_diagnose_tm_blocks
887
888 } // anon namespace
889
890 gimple_opt_pass *
891 make_pass_diagnose_tm_blocks (gcc::context *ctxt)
892 {
893 return new pass_diagnose_tm_blocks (ctxt);
894 }
895 \f
896 /* Instead of instrumenting thread private memory, we save the
897 addresses in a log which we later use to save/restore the addresses
898 upon transaction start/restart.
899
900 The log is keyed by address, where each element contains individual
901 statements among different code paths that perform the store.
902
903 This log is later used to generate either plain save/restore of the
904 addresses upon transaction start/restart, or calls to the ITM_L*
905 logging functions.
906
907 So for something like:
908
909 struct large { int x[1000]; };
910 struct large lala = { 0 };
911 __transaction {
912 lala.x[i] = 123;
913 ...
914 }
915
916 We can either save/restore:
917
918 lala = { 0 };
919 trxn = _ITM_startTransaction ();
920 if (trxn & a_saveLiveVariables)
921 tmp_lala1 = lala.x[i];
922 else if (a & a_restoreLiveVariables)
923 lala.x[i] = tmp_lala1;
924
925 or use the logging functions:
926
927 lala = { 0 };
928 trxn = _ITM_startTransaction ();
929 _ITM_LU4 (&lala.x[i]);
930
931 Obviously, if we use _ITM_L* to log, we prefer to call _ITM_L* as
932 far up the dominator tree to shadow all of the writes to a given
933 location (thus reducing the total number of logging calls), but not
934 so high as to be called on a path that does not perform a
935 write. */
936
937 /* One individual log entry. We may have multiple statements for the
938 same location if neither dominate each other (on different
939 execution paths). */
940 typedef struct tm_log_entry
941 {
942 /* Address to save. */
943 tree addr;
944 /* Entry block for the transaction this address occurs in. */
945 basic_block entry_block;
946 /* Dominating statements the store occurs in. */
947 vec<gimple> stmts;
948 /* Initially, while we are building the log, we place a nonzero
949 value here to mean that this address *will* be saved with a
950 save/restore sequence. Later, when generating the save sequence
951 we place the SSA temp generated here. */
952 tree save_var;
953 } *tm_log_entry_t;
954
955
956 /* Log entry hashtable helpers. */
957
958 struct log_entry_hasher
959 {
960 typedef tm_log_entry *value_type;
961 typedef tm_log_entry *compare_type;
962 static inline hashval_t hash (const tm_log_entry *);
963 static inline bool equal (const tm_log_entry *, const tm_log_entry *);
964 static inline void remove (tm_log_entry *);
965 };
966
967 /* Htab support. Return hash value for a `tm_log_entry'. */
968 inline hashval_t
969 log_entry_hasher::hash (const tm_log_entry *log)
970 {
971 return iterative_hash_expr (log->addr, 0);
972 }
973
974 /* Htab support. Return true if two log entries are the same. */
975 inline bool
976 log_entry_hasher::equal (const tm_log_entry *log1, const tm_log_entry *log2)
977 {
978 /* FIXME:
979
980 rth: I suggest that we get rid of the component refs etc.
981 I.e. resolve the reference to base + offset.
982
983 We may need to actually finish a merge with mainline for this,
984 since we'd like to be presented with Richi's MEM_REF_EXPRs more
985 often than not. But in the meantime your tm_log_entry could save
986 the results of get_inner_reference.
987
988 See: g++.dg/tm/pr46653.C
989 */
990
991 /* Special case plain equality because operand_equal_p() below will
992 return FALSE if the addresses are equal but they have
993 side-effects (e.g. a volatile address). */
994 if (log1->addr == log2->addr)
995 return true;
996
997 return operand_equal_p (log1->addr, log2->addr, 0);
998 }
999
1000 /* Htab support. Free one tm_log_entry. */
1001 inline void
1002 log_entry_hasher::remove (tm_log_entry *lp)
1003 {
1004 lp->stmts.release ();
1005 free (lp);
1006 }
1007
1008
1009 /* The actual log. */
1010 static hash_table<log_entry_hasher> *tm_log;
1011
1012 /* Addresses to log with a save/restore sequence. These should be in
1013 dominator order. */
1014 static vec<tree> tm_log_save_addresses;
1015
1016 enum thread_memory_type
1017 {
1018 mem_non_local = 0,
1019 mem_thread_local,
1020 mem_transaction_local,
1021 mem_max
1022 };
1023
1024 typedef struct tm_new_mem_map
1025 {
1026 /* SSA_NAME being dereferenced. */
1027 tree val;
1028 enum thread_memory_type local_new_memory;
1029 } tm_new_mem_map_t;
1030
1031 /* Hashtable helpers. */
1032
1033 struct tm_mem_map_hasher : free_ptr_hash <tm_new_mem_map_t>
1034 {
1035 static inline hashval_t hash (const tm_new_mem_map_t *);
1036 static inline bool equal (const tm_new_mem_map_t *, const tm_new_mem_map_t *);
1037 };
1038
1039 inline hashval_t
1040 tm_mem_map_hasher::hash (const tm_new_mem_map_t *v)
1041 {
1042 return (intptr_t)v->val >> 4;
1043 }
1044
1045 inline bool
1046 tm_mem_map_hasher::equal (const tm_new_mem_map_t *v, const tm_new_mem_map_t *c)
1047 {
1048 return v->val == c->val;
1049 }
1050
1051 /* Map for an SSA_NAME originally pointing to a non aliased new piece
1052 of memory (malloc, alloc, etc). */
1053 static hash_table<tm_mem_map_hasher> *tm_new_mem_hash;
1054
1055 /* Initialize logging data structures. */
1056 static void
1057 tm_log_init (void)
1058 {
1059 tm_log = new hash_table<log_entry_hasher> (10);
1060 tm_new_mem_hash = new hash_table<tm_mem_map_hasher> (5);
1061 tm_log_save_addresses.create (5);
1062 }
1063
1064 /* Free logging data structures. */
1065 static void
1066 tm_log_delete (void)
1067 {
1068 delete tm_log;
1069 tm_log = NULL;
1070 delete tm_new_mem_hash;
1071 tm_new_mem_hash = NULL;
1072 tm_log_save_addresses.release ();
1073 }
1074
1075 /* Return true if MEM is a transaction invariant memory for the TM
1076 region starting at REGION_ENTRY_BLOCK. */
1077 static bool
1078 transaction_invariant_address_p (const_tree mem, basic_block region_entry_block)
1079 {
1080 if ((TREE_CODE (mem) == INDIRECT_REF || TREE_CODE (mem) == MEM_REF)
1081 && TREE_CODE (TREE_OPERAND (mem, 0)) == SSA_NAME)
1082 {
1083 basic_block def_bb;
1084
1085 def_bb = gimple_bb (SSA_NAME_DEF_STMT (TREE_OPERAND (mem, 0)));
1086 return def_bb != region_entry_block
1087 && dominated_by_p (CDI_DOMINATORS, region_entry_block, def_bb);
1088 }
1089
1090 mem = strip_invariant_refs (mem);
1091 return mem && (CONSTANT_CLASS_P (mem) || decl_address_invariant_p (mem));
1092 }
1093
1094 /* Given an address ADDR in STMT, find it in the memory log or add it,
1095 making sure to keep only the addresses highest in the dominator
1096 tree.
1097
1098 ENTRY_BLOCK is the entry_block for the transaction.
1099
1100 If we find the address in the log, make sure it's either the same
1101 address, or an equivalent one that dominates ADDR.
1102
1103 If we find the address, but neither ADDR dominates the found
1104 address, nor the found one dominates ADDR, we're on different
1105 execution paths. Add it.
1106
1107 If known, ENTRY_BLOCK is the entry block for the region, otherwise
1108 NULL. */
1109 static void
1110 tm_log_add (basic_block entry_block, tree addr, gimple stmt)
1111 {
1112 tm_log_entry **slot;
1113 struct tm_log_entry l, *lp;
1114
1115 l.addr = addr;
1116 slot = tm_log->find_slot (&l, INSERT);
1117 if (!*slot)
1118 {
1119 tree type = TREE_TYPE (addr);
1120
1121 lp = XNEW (struct tm_log_entry);
1122 lp->addr = addr;
1123 *slot = lp;
1124
1125 /* Small invariant addresses can be handled as save/restores. */
1126 if (entry_block
1127 && transaction_invariant_address_p (lp->addr, entry_block)
1128 && TYPE_SIZE_UNIT (type) != NULL
1129 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
1130 && ((HOST_WIDE_INT) tree_to_uhwi (TYPE_SIZE_UNIT (type))
1131 < PARAM_VALUE (PARAM_TM_MAX_AGGREGATE_SIZE))
1132 /* We must be able to copy this type normally. I.e., no
1133 special constructors and the like. */
1134 && !TREE_ADDRESSABLE (type))
1135 {
1136 lp->save_var = create_tmp_reg (TREE_TYPE (lp->addr), "tm_save");
1137 lp->stmts.create (0);
1138 lp->entry_block = entry_block;
1139 /* Save addresses separately in dominator order so we don't
1140 get confused by overlapping addresses in the save/restore
1141 sequence. */
1142 tm_log_save_addresses.safe_push (lp->addr);
1143 }
1144 else
1145 {
1146 /* Use the logging functions. */
1147 lp->stmts.create (5);
1148 lp->stmts.quick_push (stmt);
1149 lp->save_var = NULL;
1150 }
1151 }
1152 else
1153 {
1154 size_t i;
1155 gimple oldstmt;
1156
1157 lp = *slot;
1158
1159 /* If we're generating a save/restore sequence, we don't care
1160 about statements. */
1161 if (lp->save_var)
1162 return;
1163
1164 for (i = 0; lp->stmts.iterate (i, &oldstmt); ++i)
1165 {
1166 if (stmt == oldstmt)
1167 return;
1168 /* We already have a store to the same address, higher up the
1169 dominator tree. Nothing to do. */
1170 if (dominated_by_p (CDI_DOMINATORS,
1171 gimple_bb (stmt), gimple_bb (oldstmt)))
1172 return;
1173 /* We should be processing blocks in dominator tree order. */
1174 gcc_assert (!dominated_by_p (CDI_DOMINATORS,
1175 gimple_bb (oldstmt), gimple_bb (stmt)));
1176 }
1177 /* Store is on a different code path. */
1178 lp->stmts.safe_push (stmt);
1179 }
1180 }
1181
1182 /* Gimplify the address of a TARGET_MEM_REF. Return the SSA_NAME
1183 result, insert the new statements before GSI. */
1184
1185 static tree
1186 gimplify_addr (gimple_stmt_iterator *gsi, tree x)
1187 {
1188 if (TREE_CODE (x) == TARGET_MEM_REF)
1189 x = tree_mem_ref_addr (build_pointer_type (TREE_TYPE (x)), x);
1190 else
1191 x = build_fold_addr_expr (x);
1192 return force_gimple_operand_gsi (gsi, x, true, NULL, true, GSI_SAME_STMT);
1193 }
1194
1195 /* Instrument one address with the logging functions.
1196 ADDR is the address to save.
1197 STMT is the statement before which to place it. */
1198 static void
1199 tm_log_emit_stmt (tree addr, gimple stmt)
1200 {
1201 tree type = TREE_TYPE (addr);
1202 tree size = TYPE_SIZE_UNIT (type);
1203 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1204 gimple log;
1205 enum built_in_function code = BUILT_IN_TM_LOG;
1206
1207 if (type == float_type_node)
1208 code = BUILT_IN_TM_LOG_FLOAT;
1209 else if (type == double_type_node)
1210 code = BUILT_IN_TM_LOG_DOUBLE;
1211 else if (type == long_double_type_node)
1212 code = BUILT_IN_TM_LOG_LDOUBLE;
1213 else if (tree_fits_uhwi_p (size))
1214 {
1215 unsigned int n = tree_to_uhwi (size);
1216 switch (n)
1217 {
1218 case 1:
1219 code = BUILT_IN_TM_LOG_1;
1220 break;
1221 case 2:
1222 code = BUILT_IN_TM_LOG_2;
1223 break;
1224 case 4:
1225 code = BUILT_IN_TM_LOG_4;
1226 break;
1227 case 8:
1228 code = BUILT_IN_TM_LOG_8;
1229 break;
1230 default:
1231 code = BUILT_IN_TM_LOG;
1232 if (TREE_CODE (type) == VECTOR_TYPE)
1233 {
1234 if (n == 8 && builtin_decl_explicit (BUILT_IN_TM_LOG_M64))
1235 code = BUILT_IN_TM_LOG_M64;
1236 else if (n == 16 && builtin_decl_explicit (BUILT_IN_TM_LOG_M128))
1237 code = BUILT_IN_TM_LOG_M128;
1238 else if (n == 32 && builtin_decl_explicit (BUILT_IN_TM_LOG_M256))
1239 code = BUILT_IN_TM_LOG_M256;
1240 }
1241 break;
1242 }
1243 }
1244
1245 addr = gimplify_addr (&gsi, addr);
1246 if (code == BUILT_IN_TM_LOG)
1247 log = gimple_build_call (builtin_decl_explicit (code), 2, addr, size);
1248 else
1249 log = gimple_build_call (builtin_decl_explicit (code), 1, addr);
1250 gsi_insert_before (&gsi, log, GSI_SAME_STMT);
1251 }
1252
1253 /* Go through the log and instrument address that must be instrumented
1254 with the logging functions. Leave the save/restore addresses for
1255 later. */
1256 static void
1257 tm_log_emit (void)
1258 {
1259 hash_table<log_entry_hasher>::iterator hi;
1260 struct tm_log_entry *lp;
1261
1262 FOR_EACH_HASH_TABLE_ELEMENT (*tm_log, lp, tm_log_entry_t, hi)
1263 {
1264 size_t i;
1265 gimple stmt;
1266
1267 if (dump_file)
1268 {
1269 fprintf (dump_file, "TM thread private mem logging: ");
1270 print_generic_expr (dump_file, lp->addr, 0);
1271 fprintf (dump_file, "\n");
1272 }
1273
1274 if (lp->save_var)
1275 {
1276 if (dump_file)
1277 fprintf (dump_file, "DUMPING to variable\n");
1278 continue;
1279 }
1280 else
1281 {
1282 if (dump_file)
1283 fprintf (dump_file, "DUMPING with logging functions\n");
1284 for (i = 0; lp->stmts.iterate (i, &stmt); ++i)
1285 tm_log_emit_stmt (lp->addr, stmt);
1286 }
1287 }
1288 }
1289
1290 /* Emit the save sequence for the corresponding addresses in the log.
1291 ENTRY_BLOCK is the entry block for the transaction.
1292 BB is the basic block to insert the code in. */
1293 static void
1294 tm_log_emit_saves (basic_block entry_block, basic_block bb)
1295 {
1296 size_t i;
1297 gimple_stmt_iterator gsi = gsi_last_bb (bb);
1298 gimple stmt;
1299 struct tm_log_entry l, *lp;
1300
1301 for (i = 0; i < tm_log_save_addresses.length (); ++i)
1302 {
1303 l.addr = tm_log_save_addresses[i];
1304 lp = *(tm_log->find_slot (&l, NO_INSERT));
1305 gcc_assert (lp->save_var != NULL);
1306
1307 /* We only care about variables in the current transaction. */
1308 if (lp->entry_block != entry_block)
1309 continue;
1310
1311 stmt = gimple_build_assign (lp->save_var, unshare_expr (lp->addr));
1312
1313 /* Make sure we can create an SSA_NAME for this type. For
1314 instance, aggregates aren't allowed, in which case the system
1315 will create a VOP for us and everything will just work. */
1316 if (is_gimple_reg_type (TREE_TYPE (lp->save_var)))
1317 {
1318 lp->save_var = make_ssa_name (lp->save_var, stmt);
1319 gimple_assign_set_lhs (stmt, lp->save_var);
1320 }
1321
1322 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
1323 }
1324 }
1325
1326 /* Emit the restore sequence for the corresponding addresses in the log.
1327 ENTRY_BLOCK is the entry block for the transaction.
1328 BB is the basic block to insert the code in. */
1329 static void
1330 tm_log_emit_restores (basic_block entry_block, basic_block bb)
1331 {
1332 int i;
1333 struct tm_log_entry l, *lp;
1334 gimple_stmt_iterator gsi;
1335 gimple stmt;
1336
1337 for (i = tm_log_save_addresses.length () - 1; i >= 0; i--)
1338 {
1339 l.addr = tm_log_save_addresses[i];
1340 lp = *(tm_log->find_slot (&l, NO_INSERT));
1341 gcc_assert (lp->save_var != NULL);
1342
1343 /* We only care about variables in the current transaction. */
1344 if (lp->entry_block != entry_block)
1345 continue;
1346
1347 /* Restores are in LIFO order from the saves in case we have
1348 overlaps. */
1349 gsi = gsi_start_bb (bb);
1350
1351 stmt = gimple_build_assign (unshare_expr (lp->addr), lp->save_var);
1352 gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
1353 }
1354 }
1355
1356 \f
1357 static tree lower_sequence_tm (gimple_stmt_iterator *, bool *,
1358 struct walk_stmt_info *);
1359 static tree lower_sequence_no_tm (gimple_stmt_iterator *, bool *,
1360 struct walk_stmt_info *);
1361
1362 /* Evaluate an address X being dereferenced and determine if it
1363 originally points to a non aliased new chunk of memory (malloc,
1364 alloca, etc).
1365
1366 Return MEM_THREAD_LOCAL if it points to a thread-local address.
1367 Return MEM_TRANSACTION_LOCAL if it points to a transaction-local address.
1368 Return MEM_NON_LOCAL otherwise.
1369
1370 ENTRY_BLOCK is the entry block to the transaction containing the
1371 dereference of X. */
1372 static enum thread_memory_type
1373 thread_private_new_memory (basic_block entry_block, tree x)
1374 {
1375 gimple stmt = NULL;
1376 enum tree_code code;
1377 tm_new_mem_map_t **slot;
1378 tm_new_mem_map_t elt, *elt_p;
1379 tree val = x;
1380 enum thread_memory_type retval = mem_transaction_local;
1381
1382 if (!entry_block
1383 || TREE_CODE (x) != SSA_NAME
1384 /* Possible uninitialized use, or a function argument. In
1385 either case, we don't care. */
1386 || SSA_NAME_IS_DEFAULT_DEF (x))
1387 return mem_non_local;
1388
1389 /* Look in cache first. */
1390 elt.val = x;
1391 slot = tm_new_mem_hash->find_slot (&elt, INSERT);
1392 elt_p = *slot;
1393 if (elt_p)
1394 return elt_p->local_new_memory;
1395
1396 /* Optimistically assume the memory is transaction local during
1397 processing. This catches recursion into this variable. */
1398 *slot = elt_p = XNEW (tm_new_mem_map_t);
1399 elt_p->val = val;
1400 elt_p->local_new_memory = mem_transaction_local;
1401
1402 /* Search DEF chain to find the original definition of this address. */
1403 do
1404 {
1405 if (ptr_deref_may_alias_global_p (x))
1406 {
1407 /* Address escapes. This is not thread-private. */
1408 retval = mem_non_local;
1409 goto new_memory_ret;
1410 }
1411
1412 stmt = SSA_NAME_DEF_STMT (x);
1413
1414 /* If the malloc call is outside the transaction, this is
1415 thread-local. */
1416 if (retval != mem_thread_local
1417 && !dominated_by_p (CDI_DOMINATORS, gimple_bb (stmt), entry_block))
1418 retval = mem_thread_local;
1419
1420 if (is_gimple_assign (stmt))
1421 {
1422 code = gimple_assign_rhs_code (stmt);
1423 /* x = foo ==> foo */
1424 if (code == SSA_NAME)
1425 x = gimple_assign_rhs1 (stmt);
1426 /* x = foo + n ==> foo */
1427 else if (code == POINTER_PLUS_EXPR)
1428 x = gimple_assign_rhs1 (stmt);
1429 /* x = (cast*) foo ==> foo */
1430 else if (code == VIEW_CONVERT_EXPR || CONVERT_EXPR_CODE_P (code))
1431 x = gimple_assign_rhs1 (stmt);
1432 /* x = c ? op1 : op2 == > op1 or op2 just like a PHI */
1433 else if (code == COND_EXPR)
1434 {
1435 tree op1 = gimple_assign_rhs2 (stmt);
1436 tree op2 = gimple_assign_rhs3 (stmt);
1437 enum thread_memory_type mem;
1438 retval = thread_private_new_memory (entry_block, op1);
1439 if (retval == mem_non_local)
1440 goto new_memory_ret;
1441 mem = thread_private_new_memory (entry_block, op2);
1442 retval = MIN (retval, mem);
1443 goto new_memory_ret;
1444 }
1445 else
1446 {
1447 retval = mem_non_local;
1448 goto new_memory_ret;
1449 }
1450 }
1451 else
1452 {
1453 if (gimple_code (stmt) == GIMPLE_PHI)
1454 {
1455 unsigned int i;
1456 enum thread_memory_type mem;
1457 tree phi_result = gimple_phi_result (stmt);
1458
1459 /* If any of the ancestors are non-local, we are sure to
1460 be non-local. Otherwise we can avoid doing anything
1461 and inherit what has already been generated. */
1462 retval = mem_max;
1463 for (i = 0; i < gimple_phi_num_args (stmt); ++i)
1464 {
1465 tree op = PHI_ARG_DEF (stmt, i);
1466
1467 /* Exclude self-assignment. */
1468 if (phi_result == op)
1469 continue;
1470
1471 mem = thread_private_new_memory (entry_block, op);
1472 if (mem == mem_non_local)
1473 {
1474 retval = mem;
1475 goto new_memory_ret;
1476 }
1477 retval = MIN (retval, mem);
1478 }
1479 goto new_memory_ret;
1480 }
1481 break;
1482 }
1483 }
1484 while (TREE_CODE (x) == SSA_NAME);
1485
1486 if (stmt && is_gimple_call (stmt) && gimple_call_flags (stmt) & ECF_MALLOC)
1487 /* Thread-local or transaction-local. */
1488 ;
1489 else
1490 retval = mem_non_local;
1491
1492 new_memory_ret:
1493 elt_p->local_new_memory = retval;
1494 return retval;
1495 }
1496
1497 /* Determine whether X has to be instrumented using a read
1498 or write barrier.
1499
1500 ENTRY_BLOCK is the entry block for the region where stmt resides
1501 in. NULL if unknown.
1502
1503 STMT is the statement in which X occurs in. It is used for thread
1504 private memory instrumentation. If no TPM instrumentation is
1505 desired, STMT should be null. */
1506 static bool
1507 requires_barrier (basic_block entry_block, tree x, gimple stmt)
1508 {
1509 tree orig = x;
1510 while (handled_component_p (x))
1511 x = TREE_OPERAND (x, 0);
1512
1513 switch (TREE_CODE (x))
1514 {
1515 case INDIRECT_REF:
1516 case MEM_REF:
1517 {
1518 enum thread_memory_type ret;
1519
1520 ret = thread_private_new_memory (entry_block, TREE_OPERAND (x, 0));
1521 if (ret == mem_non_local)
1522 return true;
1523 if (stmt && ret == mem_thread_local)
1524 /* ?? Should we pass `orig', or the INDIRECT_REF X. ?? */
1525 tm_log_add (entry_block, orig, stmt);
1526
1527 /* Transaction-locals require nothing at all. For malloc, a
1528 transaction restart frees the memory and we reallocate.
1529 For alloca, the stack pointer gets reset by the retry and
1530 we reallocate. */
1531 return false;
1532 }
1533
1534 case TARGET_MEM_REF:
1535 if (TREE_CODE (TMR_BASE (x)) != ADDR_EXPR)
1536 return true;
1537 x = TREE_OPERAND (TMR_BASE (x), 0);
1538 if (TREE_CODE (x) == PARM_DECL)
1539 return false;
1540 gcc_assert (TREE_CODE (x) == VAR_DECL);
1541 /* FALLTHRU */
1542
1543 case PARM_DECL:
1544 case RESULT_DECL:
1545 case VAR_DECL:
1546 if (DECL_BY_REFERENCE (x))
1547 {
1548 /* ??? This value is a pointer, but aggregate_value_p has been
1549 jigged to return true which confuses needs_to_live_in_memory.
1550 This ought to be cleaned up generically.
1551
1552 FIXME: Verify this still happens after the next mainline
1553 merge. Testcase ie g++.dg/tm/pr47554.C.
1554 */
1555 return false;
1556 }
1557
1558 if (is_global_var (x))
1559 return !TREE_READONLY (x);
1560 if (/* FIXME: This condition should actually go below in the
1561 tm_log_add() call, however is_call_clobbered() depends on
1562 aliasing info which is not available during
1563 gimplification. Since requires_barrier() gets called
1564 during lower_sequence_tm/gimplification, leave the call
1565 to needs_to_live_in_memory until we eliminate
1566 lower_sequence_tm altogether. */
1567 needs_to_live_in_memory (x))
1568 return true;
1569 else
1570 {
1571 /* For local memory that doesn't escape (aka thread private
1572 memory), we can either save the value at the beginning of
1573 the transaction and restore on restart, or call a tm
1574 function to dynamically save and restore on restart
1575 (ITM_L*). */
1576 if (stmt)
1577 tm_log_add (entry_block, orig, stmt);
1578 return false;
1579 }
1580
1581 default:
1582 return false;
1583 }
1584 }
1585
1586 /* Mark the GIMPLE_ASSIGN statement as appropriate for being inside
1587 a transaction region. */
1588
1589 static void
1590 examine_assign_tm (unsigned *state, gimple_stmt_iterator *gsi)
1591 {
1592 gimple stmt = gsi_stmt (*gsi);
1593
1594 if (requires_barrier (/*entry_block=*/NULL, gimple_assign_rhs1 (stmt), NULL))
1595 *state |= GTMA_HAVE_LOAD;
1596 if (requires_barrier (/*entry_block=*/NULL, gimple_assign_lhs (stmt), NULL))
1597 *state |= GTMA_HAVE_STORE;
1598 }
1599
1600 /* Mark a GIMPLE_CALL as appropriate for being inside a transaction. */
1601
1602 static void
1603 examine_call_tm (unsigned *state, gimple_stmt_iterator *gsi)
1604 {
1605 gimple stmt = gsi_stmt (*gsi);
1606 tree fn;
1607
1608 if (is_tm_pure_call (stmt))
1609 return;
1610
1611 /* Check if this call is a transaction abort. */
1612 fn = gimple_call_fndecl (stmt);
1613 if (is_tm_abort (fn))
1614 *state |= GTMA_HAVE_ABORT;
1615
1616 /* Note that something may happen. */
1617 *state |= GTMA_HAVE_LOAD | GTMA_HAVE_STORE;
1618 }
1619
1620 /* Lower a GIMPLE_TRANSACTION statement. */
1621
1622 static void
1623 lower_transaction (gimple_stmt_iterator *gsi, struct walk_stmt_info *wi)
1624 {
1625 gimple g;
1626 gtransaction *stmt = as_a <gtransaction *> (gsi_stmt (*gsi));
1627 unsigned int *outer_state = (unsigned int *) wi->info;
1628 unsigned int this_state = 0;
1629 struct walk_stmt_info this_wi;
1630
1631 /* First, lower the body. The scanning that we do inside gives
1632 us some idea of what we're dealing with. */
1633 memset (&this_wi, 0, sizeof (this_wi));
1634 this_wi.info = (void *) &this_state;
1635 walk_gimple_seq_mod (gimple_transaction_body_ptr (stmt),
1636 lower_sequence_tm, NULL, &this_wi);
1637
1638 /* If there was absolutely nothing transaction related inside the
1639 transaction, we may elide it. Likewise if this is a nested
1640 transaction and does not contain an abort. */
1641 if (this_state == 0
1642 || (!(this_state & GTMA_HAVE_ABORT) && outer_state != NULL))
1643 {
1644 if (outer_state)
1645 *outer_state |= this_state;
1646
1647 gsi_insert_seq_before (gsi, gimple_transaction_body (stmt),
1648 GSI_SAME_STMT);
1649 gimple_transaction_set_body (stmt, NULL);
1650
1651 gsi_remove (gsi, true);
1652 wi->removed_stmt = true;
1653 return;
1654 }
1655
1656 /* Wrap the body of the transaction in a try-finally node so that
1657 the commit call is always properly called. */
1658 g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_COMMIT), 0);
1659 if (flag_exceptions)
1660 {
1661 tree ptr;
1662 gimple_seq n_seq, e_seq;
1663
1664 n_seq = gimple_seq_alloc_with_stmt (g);
1665 e_seq = NULL;
1666
1667 g = gimple_build_call (builtin_decl_explicit (BUILT_IN_EH_POINTER),
1668 1, integer_zero_node);
1669 ptr = create_tmp_var (ptr_type_node);
1670 gimple_call_set_lhs (g, ptr);
1671 gimple_seq_add_stmt (&e_seq, g);
1672
1673 g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_COMMIT_EH),
1674 1, ptr);
1675 gimple_seq_add_stmt (&e_seq, g);
1676
1677 g = gimple_build_eh_else (n_seq, e_seq);
1678 }
1679
1680 g = gimple_build_try (gimple_transaction_body (stmt),
1681 gimple_seq_alloc_with_stmt (g), GIMPLE_TRY_FINALLY);
1682 gsi_insert_after (gsi, g, GSI_CONTINUE_LINKING);
1683
1684 gimple_transaction_set_body (stmt, NULL);
1685
1686 /* If the transaction calls abort or if this is an outer transaction,
1687 add an "over" label afterwards. */
1688 if ((this_state & (GTMA_HAVE_ABORT))
1689 || (gimple_transaction_subcode (stmt) & GTMA_IS_OUTER))
1690 {
1691 tree label = create_artificial_label (UNKNOWN_LOCATION);
1692 gimple_transaction_set_label (stmt, label);
1693 gsi_insert_after (gsi, gimple_build_label (label), GSI_CONTINUE_LINKING);
1694 }
1695
1696 /* Record the set of operations found for use later. */
1697 this_state |= gimple_transaction_subcode (stmt) & GTMA_DECLARATION_MASK;
1698 gimple_transaction_set_subcode (stmt, this_state);
1699 }
1700
1701 /* Iterate through the statements in the sequence, lowering them all
1702 as appropriate for being in a transaction. */
1703
1704 static tree
1705 lower_sequence_tm (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1706 struct walk_stmt_info *wi)
1707 {
1708 unsigned int *state = (unsigned int *) wi->info;
1709 gimple stmt = gsi_stmt (*gsi);
1710
1711 *handled_ops_p = true;
1712 switch (gimple_code (stmt))
1713 {
1714 case GIMPLE_ASSIGN:
1715 /* Only memory reads/writes need to be instrumented. */
1716 if (gimple_assign_single_p (stmt))
1717 examine_assign_tm (state, gsi);
1718 break;
1719
1720 case GIMPLE_CALL:
1721 examine_call_tm (state, gsi);
1722 break;
1723
1724 case GIMPLE_ASM:
1725 *state |= GTMA_MAY_ENTER_IRREVOCABLE;
1726 break;
1727
1728 case GIMPLE_TRANSACTION:
1729 lower_transaction (gsi, wi);
1730 break;
1731
1732 default:
1733 *handled_ops_p = !gimple_has_substatements (stmt);
1734 break;
1735 }
1736
1737 return NULL_TREE;
1738 }
1739
1740 /* Iterate through the statements in the sequence, lowering them all
1741 as appropriate for being outside of a transaction. */
1742
1743 static tree
1744 lower_sequence_no_tm (gimple_stmt_iterator *gsi, bool *handled_ops_p,
1745 struct walk_stmt_info * wi)
1746 {
1747 gimple stmt = gsi_stmt (*gsi);
1748
1749 if (gimple_code (stmt) == GIMPLE_TRANSACTION)
1750 {
1751 *handled_ops_p = true;
1752 lower_transaction (gsi, wi);
1753 }
1754 else
1755 *handled_ops_p = !gimple_has_substatements (stmt);
1756
1757 return NULL_TREE;
1758 }
1759
1760 /* Main entry point for flattening GIMPLE_TRANSACTION constructs. After
1761 this, GIMPLE_TRANSACTION nodes still exist, but the nested body has
1762 been moved out, and all the data required for constructing a proper
1763 CFG has been recorded. */
1764
1765 static unsigned int
1766 execute_lower_tm (void)
1767 {
1768 struct walk_stmt_info wi;
1769 gimple_seq body;
1770
1771 /* Transactional clones aren't created until a later pass. */
1772 gcc_assert (!decl_is_tm_clone (current_function_decl));
1773
1774 body = gimple_body (current_function_decl);
1775 memset (&wi, 0, sizeof (wi));
1776 walk_gimple_seq_mod (&body, lower_sequence_no_tm, NULL, &wi);
1777 gimple_set_body (current_function_decl, body);
1778
1779 return 0;
1780 }
1781
1782 namespace {
1783
1784 const pass_data pass_data_lower_tm =
1785 {
1786 GIMPLE_PASS, /* type */
1787 "tmlower", /* name */
1788 OPTGROUP_NONE, /* optinfo_flags */
1789 TV_TRANS_MEM, /* tv_id */
1790 PROP_gimple_lcf, /* properties_required */
1791 0, /* properties_provided */
1792 0, /* properties_destroyed */
1793 0, /* todo_flags_start */
1794 0, /* todo_flags_finish */
1795 };
1796
1797 class pass_lower_tm : public gimple_opt_pass
1798 {
1799 public:
1800 pass_lower_tm (gcc::context *ctxt)
1801 : gimple_opt_pass (pass_data_lower_tm, ctxt)
1802 {}
1803
1804 /* opt_pass methods: */
1805 virtual bool gate (function *) { return flag_tm; }
1806 virtual unsigned int execute (function *) { return execute_lower_tm (); }
1807
1808 }; // class pass_lower_tm
1809
1810 } // anon namespace
1811
1812 gimple_opt_pass *
1813 make_pass_lower_tm (gcc::context *ctxt)
1814 {
1815 return new pass_lower_tm (ctxt);
1816 }
1817 \f
1818 /* Collect region information for each transaction. */
1819
1820 struct tm_region
1821 {
1822 public:
1823
1824 /* The field "transaction_stmt" is initially a gtransaction *,
1825 but eventually gets lowered to a gcall *(to BUILT_IN_TM_START).
1826
1827 Helper method to get it as a gtransaction *, with code-checking
1828 in a checked-build. */
1829
1830 gtransaction *
1831 get_transaction_stmt () const
1832 {
1833 return as_a <gtransaction *> (transaction_stmt);
1834 }
1835
1836 public:
1837
1838 /* Link to the next unnested transaction. */
1839 struct tm_region *next;
1840
1841 /* Link to the next inner transaction. */
1842 struct tm_region *inner;
1843
1844 /* Link to the next outer transaction. */
1845 struct tm_region *outer;
1846
1847 /* The GIMPLE_TRANSACTION statement beginning this transaction.
1848 After TM_MARK, this gets replaced by a call to
1849 BUILT_IN_TM_START.
1850 Hence this will be either a gtransaction *or a gcall *. */
1851 gimple transaction_stmt;
1852
1853 /* After TM_MARK expands the GIMPLE_TRANSACTION into a call to
1854 BUILT_IN_TM_START, this field is true if the transaction is an
1855 outer transaction. */
1856 bool original_transaction_was_outer;
1857
1858 /* Return value from BUILT_IN_TM_START. */
1859 tree tm_state;
1860
1861 /* The entry block to this region. This will always be the first
1862 block of the body of the transaction. */
1863 basic_block entry_block;
1864
1865 /* The first block after an expanded call to _ITM_beginTransaction. */
1866 basic_block restart_block;
1867
1868 /* The set of all blocks that end the region; NULL if only EXIT_BLOCK.
1869 These blocks are still a part of the region (i.e., the border is
1870 inclusive). Note that this set is only complete for paths in the CFG
1871 starting at ENTRY_BLOCK, and that there is no exit block recorded for
1872 the edge to the "over" label. */
1873 bitmap exit_blocks;
1874
1875 /* The set of all blocks that have an TM_IRREVOCABLE call. */
1876 bitmap irr_blocks;
1877 };
1878
1879 typedef struct tm_region *tm_region_p;
1880
1881 /* True if there are pending edge statements to be committed for the
1882 current function being scanned in the tmmark pass. */
1883 bool pending_edge_inserts_p;
1884
1885 static struct tm_region *all_tm_regions;
1886 static bitmap_obstack tm_obstack;
1887
1888
1889 /* A subroutine of tm_region_init. Record the existence of the
1890 GIMPLE_TRANSACTION statement in a tree of tm_region elements. */
1891
1892 static struct tm_region *
1893 tm_region_init_0 (struct tm_region *outer, basic_block bb,
1894 gtransaction *stmt)
1895 {
1896 struct tm_region *region;
1897
1898 region = (struct tm_region *)
1899 obstack_alloc (&tm_obstack.obstack, sizeof (struct tm_region));
1900
1901 if (outer)
1902 {
1903 region->next = outer->inner;
1904 outer->inner = region;
1905 }
1906 else
1907 {
1908 region->next = all_tm_regions;
1909 all_tm_regions = region;
1910 }
1911 region->inner = NULL;
1912 region->outer = outer;
1913
1914 region->transaction_stmt = stmt;
1915 region->original_transaction_was_outer = false;
1916 region->tm_state = NULL;
1917
1918 /* There are either one or two edges out of the block containing
1919 the GIMPLE_TRANSACTION, one to the actual region and one to the
1920 "over" label if the region contains an abort. The former will
1921 always be the one marked FALLTHRU. */
1922 region->entry_block = FALLTHRU_EDGE (bb)->dest;
1923
1924 region->exit_blocks = BITMAP_ALLOC (&tm_obstack);
1925 region->irr_blocks = BITMAP_ALLOC (&tm_obstack);
1926
1927 return region;
1928 }
1929
1930 /* A subroutine of tm_region_init. Record all the exit and
1931 irrevocable blocks in BB into the region's exit_blocks and
1932 irr_blocks bitmaps. Returns the new region being scanned. */
1933
1934 static struct tm_region *
1935 tm_region_init_1 (struct tm_region *region, basic_block bb)
1936 {
1937 gimple_stmt_iterator gsi;
1938 gimple g;
1939
1940 if (!region
1941 || (!region->irr_blocks && !region->exit_blocks))
1942 return region;
1943
1944 /* Check to see if this is the end of a region by seeing if it
1945 contains a call to __builtin_tm_commit{,_eh}. Note that the
1946 outermost region for DECL_IS_TM_CLONE need not collect this. */
1947 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
1948 {
1949 g = gsi_stmt (gsi);
1950 if (gimple_code (g) == GIMPLE_CALL)
1951 {
1952 tree fn = gimple_call_fndecl (g);
1953 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
1954 {
1955 if ((DECL_FUNCTION_CODE (fn) == BUILT_IN_TM_COMMIT
1956 || DECL_FUNCTION_CODE (fn) == BUILT_IN_TM_COMMIT_EH)
1957 && region->exit_blocks)
1958 {
1959 bitmap_set_bit (region->exit_blocks, bb->index);
1960 region = region->outer;
1961 break;
1962 }
1963 if (DECL_FUNCTION_CODE (fn) == BUILT_IN_TM_IRREVOCABLE)
1964 bitmap_set_bit (region->irr_blocks, bb->index);
1965 }
1966 }
1967 }
1968 return region;
1969 }
1970
1971 /* Collect all of the transaction regions within the current function
1972 and record them in ALL_TM_REGIONS. The REGION parameter may specify
1973 an "outermost" region for use by tm clones. */
1974
1975 static void
1976 tm_region_init (struct tm_region *region)
1977 {
1978 gimple g;
1979 edge_iterator ei;
1980 edge e;
1981 basic_block bb;
1982 auto_vec<basic_block> queue;
1983 bitmap visited_blocks = BITMAP_ALLOC (NULL);
1984 struct tm_region *old_region;
1985 auto_vec<tm_region_p> bb_regions;
1986
1987 all_tm_regions = region;
1988 bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1989
1990 /* We could store this information in bb->aux, but we may get called
1991 through get_all_tm_blocks() from another pass that may be already
1992 using bb->aux. */
1993 bb_regions.safe_grow_cleared (last_basic_block_for_fn (cfun));
1994
1995 queue.safe_push (bb);
1996 bb_regions[bb->index] = region;
1997 do
1998 {
1999 bb = queue.pop ();
2000 region = bb_regions[bb->index];
2001 bb_regions[bb->index] = NULL;
2002
2003 /* Record exit and irrevocable blocks. */
2004 region = tm_region_init_1 (region, bb);
2005
2006 /* Check for the last statement in the block beginning a new region. */
2007 g = last_stmt (bb);
2008 old_region = region;
2009 if (g)
2010 if (gtransaction *trans_stmt = dyn_cast <gtransaction *> (g))
2011 region = tm_region_init_0 (region, bb, trans_stmt);
2012
2013 /* Process subsequent blocks. */
2014 FOR_EACH_EDGE (e, ei, bb->succs)
2015 if (!bitmap_bit_p (visited_blocks, e->dest->index))
2016 {
2017 bitmap_set_bit (visited_blocks, e->dest->index);
2018 queue.safe_push (e->dest);
2019
2020 /* If the current block started a new region, make sure that only
2021 the entry block of the new region is associated with this region.
2022 Other successors are still part of the old region. */
2023 if (old_region != region && e->dest != region->entry_block)
2024 bb_regions[e->dest->index] = old_region;
2025 else
2026 bb_regions[e->dest->index] = region;
2027 }
2028 }
2029 while (!queue.is_empty ());
2030 BITMAP_FREE (visited_blocks);
2031 }
2032
2033 /* The "gate" function for all transactional memory expansion and optimization
2034 passes. We collect region information for each top-level transaction, and
2035 if we don't find any, we skip all of the TM passes. Each region will have
2036 all of the exit blocks recorded, and the originating statement. */
2037
2038 static bool
2039 gate_tm_init (void)
2040 {
2041 if (!flag_tm)
2042 return false;
2043
2044 calculate_dominance_info (CDI_DOMINATORS);
2045 bitmap_obstack_initialize (&tm_obstack);
2046
2047 /* If the function is a TM_CLONE, then the entire function is the region. */
2048 if (decl_is_tm_clone (current_function_decl))
2049 {
2050 struct tm_region *region = (struct tm_region *)
2051 obstack_alloc (&tm_obstack.obstack, sizeof (struct tm_region));
2052 memset (region, 0, sizeof (*region));
2053 region->entry_block = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
2054 /* For a clone, the entire function is the region. But even if
2055 we don't need to record any exit blocks, we may need to
2056 record irrevocable blocks. */
2057 region->irr_blocks = BITMAP_ALLOC (&tm_obstack);
2058
2059 tm_region_init (region);
2060 }
2061 else
2062 {
2063 tm_region_init (NULL);
2064
2065 /* If we didn't find any regions, cleanup and skip the whole tree
2066 of tm-related optimizations. */
2067 if (all_tm_regions == NULL)
2068 {
2069 bitmap_obstack_release (&tm_obstack);
2070 return false;
2071 }
2072 }
2073
2074 return true;
2075 }
2076
2077 namespace {
2078
2079 const pass_data pass_data_tm_init =
2080 {
2081 GIMPLE_PASS, /* type */
2082 "*tminit", /* name */
2083 OPTGROUP_NONE, /* optinfo_flags */
2084 TV_TRANS_MEM, /* tv_id */
2085 ( PROP_ssa | PROP_cfg ), /* properties_required */
2086 0, /* properties_provided */
2087 0, /* properties_destroyed */
2088 0, /* todo_flags_start */
2089 0, /* todo_flags_finish */
2090 };
2091
2092 class pass_tm_init : public gimple_opt_pass
2093 {
2094 public:
2095 pass_tm_init (gcc::context *ctxt)
2096 : gimple_opt_pass (pass_data_tm_init, ctxt)
2097 {}
2098
2099 /* opt_pass methods: */
2100 virtual bool gate (function *) { return gate_tm_init (); }
2101
2102 }; // class pass_tm_init
2103
2104 } // anon namespace
2105
2106 gimple_opt_pass *
2107 make_pass_tm_init (gcc::context *ctxt)
2108 {
2109 return new pass_tm_init (ctxt);
2110 }
2111 \f
2112 /* Add FLAGS to the GIMPLE_TRANSACTION subcode for the transaction region
2113 represented by STATE. */
2114
2115 static inline void
2116 transaction_subcode_ior (struct tm_region *region, unsigned flags)
2117 {
2118 if (region && region->transaction_stmt)
2119 {
2120 gtransaction *transaction_stmt = region->get_transaction_stmt ();
2121 flags |= gimple_transaction_subcode (transaction_stmt);
2122 gimple_transaction_set_subcode (transaction_stmt, flags);
2123 }
2124 }
2125
2126 /* Construct a memory load in a transactional context. Return the
2127 gimple statement performing the load, or NULL if there is no
2128 TM_LOAD builtin of the appropriate size to do the load.
2129
2130 LOC is the location to use for the new statement(s). */
2131
2132 static gcall *
2133 build_tm_load (location_t loc, tree lhs, tree rhs, gimple_stmt_iterator *gsi)
2134 {
2135 enum built_in_function code = END_BUILTINS;
2136 tree t, type = TREE_TYPE (rhs), decl;
2137 gcall *gcall;
2138
2139 if (type == float_type_node)
2140 code = BUILT_IN_TM_LOAD_FLOAT;
2141 else if (type == double_type_node)
2142 code = BUILT_IN_TM_LOAD_DOUBLE;
2143 else if (type == long_double_type_node)
2144 code = BUILT_IN_TM_LOAD_LDOUBLE;
2145 else if (TYPE_SIZE_UNIT (type) != NULL
2146 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
2147 {
2148 switch (tree_to_uhwi (TYPE_SIZE_UNIT (type)))
2149 {
2150 case 1:
2151 code = BUILT_IN_TM_LOAD_1;
2152 break;
2153 case 2:
2154 code = BUILT_IN_TM_LOAD_2;
2155 break;
2156 case 4:
2157 code = BUILT_IN_TM_LOAD_4;
2158 break;
2159 case 8:
2160 code = BUILT_IN_TM_LOAD_8;
2161 break;
2162 }
2163 }
2164
2165 if (code == END_BUILTINS)
2166 {
2167 decl = targetm.vectorize.builtin_tm_load (type);
2168 if (!decl)
2169 return NULL;
2170 }
2171 else
2172 decl = builtin_decl_explicit (code);
2173
2174 t = gimplify_addr (gsi, rhs);
2175 gcall = gimple_build_call (decl, 1, t);
2176 gimple_set_location (gcall, loc);
2177
2178 t = TREE_TYPE (TREE_TYPE (decl));
2179 if (useless_type_conversion_p (type, t))
2180 {
2181 gimple_call_set_lhs (gcall, lhs);
2182 gsi_insert_before (gsi, gcall, GSI_SAME_STMT);
2183 }
2184 else
2185 {
2186 gimple g;
2187 tree temp;
2188
2189 temp = create_tmp_reg (t);
2190 gimple_call_set_lhs (gcall, temp);
2191 gsi_insert_before (gsi, gcall, GSI_SAME_STMT);
2192
2193 t = fold_build1 (VIEW_CONVERT_EXPR, type, temp);
2194 g = gimple_build_assign (lhs, t);
2195 gsi_insert_before (gsi, g, GSI_SAME_STMT);
2196 }
2197
2198 return gcall;
2199 }
2200
2201
2202 /* Similarly for storing TYPE in a transactional context. */
2203
2204 static gcall *
2205 build_tm_store (location_t loc, tree lhs, tree rhs, gimple_stmt_iterator *gsi)
2206 {
2207 enum built_in_function code = END_BUILTINS;
2208 tree t, fn, type = TREE_TYPE (rhs), simple_type;
2209 gcall *gcall;
2210
2211 if (type == float_type_node)
2212 code = BUILT_IN_TM_STORE_FLOAT;
2213 else if (type == double_type_node)
2214 code = BUILT_IN_TM_STORE_DOUBLE;
2215 else if (type == long_double_type_node)
2216 code = BUILT_IN_TM_STORE_LDOUBLE;
2217 else if (TYPE_SIZE_UNIT (type) != NULL
2218 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
2219 {
2220 switch (tree_to_uhwi (TYPE_SIZE_UNIT (type)))
2221 {
2222 case 1:
2223 code = BUILT_IN_TM_STORE_1;
2224 break;
2225 case 2:
2226 code = BUILT_IN_TM_STORE_2;
2227 break;
2228 case 4:
2229 code = BUILT_IN_TM_STORE_4;
2230 break;
2231 case 8:
2232 code = BUILT_IN_TM_STORE_8;
2233 break;
2234 }
2235 }
2236
2237 if (code == END_BUILTINS)
2238 {
2239 fn = targetm.vectorize.builtin_tm_store (type);
2240 if (!fn)
2241 return NULL;
2242 }
2243 else
2244 fn = builtin_decl_explicit (code);
2245
2246 simple_type = TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn))));
2247
2248 if (TREE_CODE (rhs) == CONSTRUCTOR)
2249 {
2250 /* Handle the easy initialization to zero. */
2251 if (!CONSTRUCTOR_ELTS (rhs))
2252 rhs = build_int_cst (simple_type, 0);
2253 else
2254 {
2255 /* ...otherwise punt to the caller and probably use
2256 BUILT_IN_TM_MEMMOVE, because we can't wrap a
2257 VIEW_CONVERT_EXPR around a CONSTRUCTOR (below) and produce
2258 valid gimple. */
2259 return NULL;
2260 }
2261 }
2262 else if (!useless_type_conversion_p (simple_type, type))
2263 {
2264 gimple g;
2265 tree temp;
2266
2267 temp = create_tmp_reg (simple_type);
2268 t = fold_build1 (VIEW_CONVERT_EXPR, simple_type, rhs);
2269 g = gimple_build_assign (temp, t);
2270 gimple_set_location (g, loc);
2271 gsi_insert_before (gsi, g, GSI_SAME_STMT);
2272
2273 rhs = temp;
2274 }
2275
2276 t = gimplify_addr (gsi, lhs);
2277 gcall = gimple_build_call (fn, 2, t, rhs);
2278 gimple_set_location (gcall, loc);
2279 gsi_insert_before (gsi, gcall, GSI_SAME_STMT);
2280
2281 return gcall;
2282 }
2283
2284
2285 /* Expand an assignment statement into transactional builtins. */
2286
2287 static void
2288 expand_assign_tm (struct tm_region *region, gimple_stmt_iterator *gsi)
2289 {
2290 gimple stmt = gsi_stmt (*gsi);
2291 location_t loc = gimple_location (stmt);
2292 tree lhs = gimple_assign_lhs (stmt);
2293 tree rhs = gimple_assign_rhs1 (stmt);
2294 bool store_p = requires_barrier (region->entry_block, lhs, NULL);
2295 bool load_p = requires_barrier (region->entry_block, rhs, NULL);
2296 gimple gcall = NULL;
2297
2298 if (!load_p && !store_p)
2299 {
2300 /* Add thread private addresses to log if applicable. */
2301 requires_barrier (region->entry_block, lhs, stmt);
2302 gsi_next (gsi);
2303 return;
2304 }
2305
2306 // Remove original load/store statement.
2307 gsi_remove (gsi, true);
2308
2309 if (load_p && !store_p)
2310 {
2311 transaction_subcode_ior (region, GTMA_HAVE_LOAD);
2312 gcall = build_tm_load (loc, lhs, rhs, gsi);
2313 }
2314 else if (store_p && !load_p)
2315 {
2316 transaction_subcode_ior (region, GTMA_HAVE_STORE);
2317 gcall = build_tm_store (loc, lhs, rhs, gsi);
2318 }
2319 if (!gcall)
2320 {
2321 tree lhs_addr, rhs_addr, tmp;
2322
2323 if (load_p)
2324 transaction_subcode_ior (region, GTMA_HAVE_LOAD);
2325 if (store_p)
2326 transaction_subcode_ior (region, GTMA_HAVE_STORE);
2327
2328 /* ??? Figure out if there's any possible overlap between the LHS
2329 and the RHS and if not, use MEMCPY. */
2330
2331 if (load_p && is_gimple_reg (lhs))
2332 {
2333 tmp = create_tmp_var (TREE_TYPE (lhs));
2334 lhs_addr = build_fold_addr_expr (tmp);
2335 }
2336 else
2337 {
2338 tmp = NULL_TREE;
2339 lhs_addr = gimplify_addr (gsi, lhs);
2340 }
2341 rhs_addr = gimplify_addr (gsi, rhs);
2342 gcall = gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_MEMMOVE),
2343 3, lhs_addr, rhs_addr,
2344 TYPE_SIZE_UNIT (TREE_TYPE (lhs)));
2345 gimple_set_location (gcall, loc);
2346 gsi_insert_before (gsi, gcall, GSI_SAME_STMT);
2347
2348 if (tmp)
2349 {
2350 gcall = gimple_build_assign (lhs, tmp);
2351 gsi_insert_before (gsi, gcall, GSI_SAME_STMT);
2352 }
2353 }
2354
2355 /* Now that we have the load/store in its instrumented form, add
2356 thread private addresses to the log if applicable. */
2357 if (!store_p)
2358 requires_barrier (region->entry_block, lhs, gcall);
2359
2360 // The calls to build_tm_{store,load} above inserted the instrumented
2361 // call into the stream.
2362 // gsi_insert_before (gsi, gcall, GSI_SAME_STMT);
2363 }
2364
2365
2366 /* Expand a call statement as appropriate for a transaction. That is,
2367 either verify that the call does not affect the transaction, or
2368 redirect the call to a clone that handles transactions, or change
2369 the transaction state to IRREVOCABLE. Return true if the call is
2370 one of the builtins that end a transaction. */
2371
2372 static bool
2373 expand_call_tm (struct tm_region *region,
2374 gimple_stmt_iterator *gsi)
2375 {
2376 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
2377 tree lhs = gimple_call_lhs (stmt);
2378 tree fn_decl;
2379 struct cgraph_node *node;
2380 bool retval = false;
2381
2382 fn_decl = gimple_call_fndecl (stmt);
2383
2384 if (fn_decl == builtin_decl_explicit (BUILT_IN_TM_MEMCPY)
2385 || fn_decl == builtin_decl_explicit (BUILT_IN_TM_MEMMOVE))
2386 transaction_subcode_ior (region, GTMA_HAVE_STORE | GTMA_HAVE_LOAD);
2387 if (fn_decl == builtin_decl_explicit (BUILT_IN_TM_MEMSET))
2388 transaction_subcode_ior (region, GTMA_HAVE_STORE);
2389
2390 if (is_tm_pure_call (stmt))
2391 return false;
2392
2393 if (fn_decl)
2394 retval = is_tm_ending_fndecl (fn_decl);
2395 if (!retval)
2396 {
2397 /* Assume all non-const/pure calls write to memory, except
2398 transaction ending builtins. */
2399 transaction_subcode_ior (region, GTMA_HAVE_STORE);
2400 }
2401
2402 /* For indirect calls, we already generated a call into the runtime. */
2403 if (!fn_decl)
2404 {
2405 tree fn = gimple_call_fn (stmt);
2406
2407 /* We are guaranteed never to go irrevocable on a safe or pure
2408 call, and the pure call was handled above. */
2409 if (is_tm_safe (fn))
2410 return false;
2411 else
2412 transaction_subcode_ior (region, GTMA_MAY_ENTER_IRREVOCABLE);
2413
2414 return false;
2415 }
2416
2417 node = cgraph_node::get (fn_decl);
2418 /* All calls should have cgraph here. */
2419 if (!node)
2420 {
2421 /* We can have a nodeless call here if some pass after IPA-tm
2422 added uninstrumented calls. For example, loop distribution
2423 can transform certain loop constructs into __builtin_mem*
2424 calls. In this case, see if we have a suitable TM
2425 replacement and fill in the gaps. */
2426 gcc_assert (DECL_BUILT_IN_CLASS (fn_decl) == BUILT_IN_NORMAL);
2427 enum built_in_function code = DECL_FUNCTION_CODE (fn_decl);
2428 gcc_assert (code == BUILT_IN_MEMCPY
2429 || code == BUILT_IN_MEMMOVE
2430 || code == BUILT_IN_MEMSET);
2431
2432 tree repl = find_tm_replacement_function (fn_decl);
2433 if (repl)
2434 {
2435 gimple_call_set_fndecl (stmt, repl);
2436 update_stmt (stmt);
2437 node = cgraph_node::create (repl);
2438 node->local.tm_may_enter_irr = false;
2439 return expand_call_tm (region, gsi);
2440 }
2441 gcc_unreachable ();
2442 }
2443 if (node->local.tm_may_enter_irr)
2444 transaction_subcode_ior (region, GTMA_MAY_ENTER_IRREVOCABLE);
2445
2446 if (is_tm_abort (fn_decl))
2447 {
2448 transaction_subcode_ior (region, GTMA_HAVE_ABORT);
2449 return true;
2450 }
2451
2452 /* Instrument the store if needed.
2453
2454 If the assignment happens inside the function call (return slot
2455 optimization), there is no instrumentation to be done, since
2456 the callee should have done the right thing. */
2457 if (lhs && requires_barrier (region->entry_block, lhs, stmt)
2458 && !gimple_call_return_slot_opt_p (stmt))
2459 {
2460 tree tmp = create_tmp_reg (TREE_TYPE (lhs));
2461 location_t loc = gimple_location (stmt);
2462 edge fallthru_edge = NULL;
2463 gassign *assign_stmt;
2464
2465 /* Remember if the call was going to throw. */
2466 if (stmt_can_throw_internal (stmt))
2467 {
2468 edge_iterator ei;
2469 edge e;
2470 basic_block bb = gimple_bb (stmt);
2471
2472 FOR_EACH_EDGE (e, ei, bb->succs)
2473 if (e->flags & EDGE_FALLTHRU)
2474 {
2475 fallthru_edge = e;
2476 break;
2477 }
2478 }
2479
2480 gimple_call_set_lhs (stmt, tmp);
2481 update_stmt (stmt);
2482 assign_stmt = gimple_build_assign (lhs, tmp);
2483 gimple_set_location (assign_stmt, loc);
2484
2485 /* We cannot throw in the middle of a BB. If the call was going
2486 to throw, place the instrumentation on the fallthru edge, so
2487 the call remains the last statement in the block. */
2488 if (fallthru_edge)
2489 {
2490 gimple_seq fallthru_seq = gimple_seq_alloc_with_stmt (assign_stmt);
2491 gimple_stmt_iterator fallthru_gsi = gsi_start (fallthru_seq);
2492 expand_assign_tm (region, &fallthru_gsi);
2493 gsi_insert_seq_on_edge (fallthru_edge, fallthru_seq);
2494 pending_edge_inserts_p = true;
2495 }
2496 else
2497 {
2498 gsi_insert_after (gsi, assign_stmt, GSI_CONTINUE_LINKING);
2499 expand_assign_tm (region, gsi);
2500 }
2501
2502 transaction_subcode_ior (region, GTMA_HAVE_STORE);
2503 }
2504
2505 return retval;
2506 }
2507
2508
2509 /* Expand all statements in BB as appropriate for being inside
2510 a transaction. */
2511
2512 static void
2513 expand_block_tm (struct tm_region *region, basic_block bb)
2514 {
2515 gimple_stmt_iterator gsi;
2516
2517 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
2518 {
2519 gimple stmt = gsi_stmt (gsi);
2520 switch (gimple_code (stmt))
2521 {
2522 case GIMPLE_ASSIGN:
2523 /* Only memory reads/writes need to be instrumented. */
2524 if (gimple_assign_single_p (stmt)
2525 && !gimple_clobber_p (stmt))
2526 {
2527 expand_assign_tm (region, &gsi);
2528 continue;
2529 }
2530 break;
2531
2532 case GIMPLE_CALL:
2533 if (expand_call_tm (region, &gsi))
2534 return;
2535 break;
2536
2537 case GIMPLE_ASM:
2538 gcc_unreachable ();
2539
2540 default:
2541 break;
2542 }
2543 if (!gsi_end_p (gsi))
2544 gsi_next (&gsi);
2545 }
2546 }
2547
2548 /* Return the list of basic-blocks in REGION.
2549
2550 STOP_AT_IRREVOCABLE_P is true if caller is uninterested in blocks
2551 following a TM_IRREVOCABLE call.
2552
2553 INCLUDE_UNINSTRUMENTED_P is TRUE if we should include the
2554 uninstrumented code path blocks in the list of basic blocks
2555 returned, false otherwise. */
2556
2557 static vec<basic_block>
2558 get_tm_region_blocks (basic_block entry_block,
2559 bitmap exit_blocks,
2560 bitmap irr_blocks,
2561 bitmap all_region_blocks,
2562 bool stop_at_irrevocable_p,
2563 bool include_uninstrumented_p = true)
2564 {
2565 vec<basic_block> bbs = vNULL;
2566 unsigned i;
2567 edge e;
2568 edge_iterator ei;
2569 bitmap visited_blocks = BITMAP_ALLOC (NULL);
2570
2571 i = 0;
2572 bbs.safe_push (entry_block);
2573 bitmap_set_bit (visited_blocks, entry_block->index);
2574
2575 do
2576 {
2577 basic_block bb = bbs[i++];
2578
2579 if (exit_blocks &&
2580 bitmap_bit_p (exit_blocks, bb->index))
2581 continue;
2582
2583 if (stop_at_irrevocable_p
2584 && irr_blocks
2585 && bitmap_bit_p (irr_blocks, bb->index))
2586 continue;
2587
2588 FOR_EACH_EDGE (e, ei, bb->succs)
2589 if ((include_uninstrumented_p
2590 || !(e->flags & EDGE_TM_UNINSTRUMENTED))
2591 && !bitmap_bit_p (visited_blocks, e->dest->index))
2592 {
2593 bitmap_set_bit (visited_blocks, e->dest->index);
2594 bbs.safe_push (e->dest);
2595 }
2596 }
2597 while (i < bbs.length ());
2598
2599 if (all_region_blocks)
2600 bitmap_ior_into (all_region_blocks, visited_blocks);
2601
2602 BITMAP_FREE (visited_blocks);
2603 return bbs;
2604 }
2605
2606 // Callback data for collect_bb2reg.
2607 struct bb2reg_stuff
2608 {
2609 vec<tm_region_p> *bb2reg;
2610 bool include_uninstrumented_p;
2611 };
2612
2613 // Callback for expand_regions, collect innermost region data for each bb.
2614 static void *
2615 collect_bb2reg (struct tm_region *region, void *data)
2616 {
2617 struct bb2reg_stuff *stuff = (struct bb2reg_stuff *)data;
2618 vec<tm_region_p> *bb2reg = stuff->bb2reg;
2619 vec<basic_block> queue;
2620 unsigned int i;
2621 basic_block bb;
2622
2623 queue = get_tm_region_blocks (region->entry_block,
2624 region->exit_blocks,
2625 region->irr_blocks,
2626 NULL,
2627 /*stop_at_irr_p=*/true,
2628 stuff->include_uninstrumented_p);
2629
2630 // We expect expand_region to perform a post-order traversal of the region
2631 // tree. Therefore the last region seen for any bb is the innermost.
2632 FOR_EACH_VEC_ELT (queue, i, bb)
2633 (*bb2reg)[bb->index] = region;
2634
2635 queue.release ();
2636 return NULL;
2637 }
2638
2639 // Returns a vector, indexed by BB->INDEX, of the innermost tm_region to
2640 // which a basic block belongs. Note that we only consider the instrumented
2641 // code paths for the region; the uninstrumented code paths are ignored if
2642 // INCLUDE_UNINSTRUMENTED_P is false.
2643 //
2644 // ??? This data is very similar to the bb_regions array that is collected
2645 // during tm_region_init. Or, rather, this data is similar to what could
2646 // be used within tm_region_init. The actual computation in tm_region_init
2647 // begins and ends with bb_regions entirely full of NULL pointers, due to
2648 // the way in which pointers are swapped in and out of the array.
2649 //
2650 // ??? Our callers expect that blocks are not shared between transactions.
2651 // When the optimizers get too smart, and blocks are shared, then during
2652 // the tm_mark phase we'll add log entries to only one of the two transactions,
2653 // and in the tm_edge phase we'll add edges to the CFG that create invalid
2654 // cycles. The symptom being SSA defs that do not dominate their uses.
2655 // Note that the optimizers were locally correct with their transformation,
2656 // as we have no info within the program that suggests that the blocks cannot
2657 // be shared.
2658 //
2659 // ??? There is currently a hack inside tree-ssa-pre.c to work around the
2660 // only known instance of this block sharing.
2661
2662 static vec<tm_region_p>
2663 get_bb_regions_instrumented (bool traverse_clones,
2664 bool include_uninstrumented_p)
2665 {
2666 unsigned n = last_basic_block_for_fn (cfun);
2667 struct bb2reg_stuff stuff;
2668 vec<tm_region_p> ret;
2669
2670 ret.create (n);
2671 ret.safe_grow_cleared (n);
2672 stuff.bb2reg = &ret;
2673 stuff.include_uninstrumented_p = include_uninstrumented_p;
2674 expand_regions (all_tm_regions, collect_bb2reg, &stuff, traverse_clones);
2675
2676 return ret;
2677 }
2678
2679 /* Set the IN_TRANSACTION for all gimple statements that appear in a
2680 transaction. */
2681
2682 void
2683 compute_transaction_bits (void)
2684 {
2685 struct tm_region *region;
2686 vec<basic_block> queue;
2687 unsigned int i;
2688 basic_block bb;
2689
2690 /* ?? Perhaps we need to abstract gate_tm_init further, because we
2691 certainly don't need it to calculate CDI_DOMINATOR info. */
2692 gate_tm_init ();
2693
2694 FOR_EACH_BB_FN (bb, cfun)
2695 bb->flags &= ~BB_IN_TRANSACTION;
2696
2697 for (region = all_tm_regions; region; region = region->next)
2698 {
2699 queue = get_tm_region_blocks (region->entry_block,
2700 region->exit_blocks,
2701 region->irr_blocks,
2702 NULL,
2703 /*stop_at_irr_p=*/true);
2704 for (i = 0; queue.iterate (i, &bb); ++i)
2705 bb->flags |= BB_IN_TRANSACTION;
2706 queue.release ();
2707 }
2708
2709 if (all_tm_regions)
2710 bitmap_obstack_release (&tm_obstack);
2711 }
2712
2713 /* Replace the GIMPLE_TRANSACTION in this region with the corresponding
2714 call to BUILT_IN_TM_START. */
2715
2716 static void *
2717 expand_transaction (struct tm_region *region, void *data ATTRIBUTE_UNUSED)
2718 {
2719 tree tm_start = builtin_decl_explicit (BUILT_IN_TM_START);
2720 basic_block transaction_bb = gimple_bb (region->transaction_stmt);
2721 tree tm_state = region->tm_state;
2722 tree tm_state_type = TREE_TYPE (tm_state);
2723 edge abort_edge = NULL;
2724 edge inst_edge = NULL;
2725 edge uninst_edge = NULL;
2726 edge fallthru_edge = NULL;
2727
2728 // Identify the various successors of the transaction start.
2729 {
2730 edge_iterator i;
2731 edge e;
2732 FOR_EACH_EDGE (e, i, transaction_bb->succs)
2733 {
2734 if (e->flags & EDGE_TM_ABORT)
2735 abort_edge = e;
2736 else if (e->flags & EDGE_TM_UNINSTRUMENTED)
2737 uninst_edge = e;
2738 else
2739 inst_edge = e;
2740 if (e->flags & EDGE_FALLTHRU)
2741 fallthru_edge = e;
2742 }
2743 }
2744
2745 /* ??? There are plenty of bits here we're not computing. */
2746 {
2747 int subcode = gimple_transaction_subcode (region->get_transaction_stmt ());
2748 int flags = 0;
2749 if (subcode & GTMA_DOES_GO_IRREVOCABLE)
2750 flags |= PR_DOESGOIRREVOCABLE;
2751 if ((subcode & GTMA_MAY_ENTER_IRREVOCABLE) == 0)
2752 flags |= PR_HASNOIRREVOCABLE;
2753 /* If the transaction does not have an abort in lexical scope and is not
2754 marked as an outer transaction, then it will never abort. */
2755 if ((subcode & GTMA_HAVE_ABORT) == 0 && (subcode & GTMA_IS_OUTER) == 0)
2756 flags |= PR_HASNOABORT;
2757 if ((subcode & GTMA_HAVE_STORE) == 0)
2758 flags |= PR_READONLY;
2759 if (inst_edge && !(subcode & GTMA_HAS_NO_INSTRUMENTATION))
2760 flags |= PR_INSTRUMENTEDCODE;
2761 if (uninst_edge)
2762 flags |= PR_UNINSTRUMENTEDCODE;
2763 if (subcode & GTMA_IS_OUTER)
2764 region->original_transaction_was_outer = true;
2765 tree t = build_int_cst (tm_state_type, flags);
2766 gcall *call = gimple_build_call (tm_start, 1, t);
2767 gimple_call_set_lhs (call, tm_state);
2768 gimple_set_location (call, gimple_location (region->transaction_stmt));
2769
2770 // Replace the GIMPLE_TRANSACTION with the call to BUILT_IN_TM_START.
2771 gimple_stmt_iterator gsi = gsi_last_bb (transaction_bb);
2772 gcc_assert (gsi_stmt (gsi) == region->transaction_stmt);
2773 gsi_insert_before (&gsi, call, GSI_SAME_STMT);
2774 gsi_remove (&gsi, true);
2775 region->transaction_stmt = call;
2776 }
2777
2778 // Generate log saves.
2779 if (!tm_log_save_addresses.is_empty ())
2780 tm_log_emit_saves (region->entry_block, transaction_bb);
2781
2782 // In the beginning, we've no tests to perform on transaction restart.
2783 // Note that after this point, transaction_bb becomes the "most recent
2784 // block containing tests for the transaction".
2785 region->restart_block = region->entry_block;
2786
2787 // Generate log restores.
2788 if (!tm_log_save_addresses.is_empty ())
2789 {
2790 basic_block test_bb = create_empty_bb (transaction_bb);
2791 basic_block code_bb = create_empty_bb (test_bb);
2792 basic_block join_bb = create_empty_bb (code_bb);
2793 add_bb_to_loop (test_bb, transaction_bb->loop_father);
2794 add_bb_to_loop (code_bb, transaction_bb->loop_father);
2795 add_bb_to_loop (join_bb, transaction_bb->loop_father);
2796 if (region->restart_block == region->entry_block)
2797 region->restart_block = test_bb;
2798
2799 tree t1 = create_tmp_reg (tm_state_type);
2800 tree t2 = build_int_cst (tm_state_type, A_RESTORELIVEVARIABLES);
2801 gimple stmt = gimple_build_assign (t1, BIT_AND_EXPR, tm_state, t2);
2802 gimple_stmt_iterator gsi = gsi_last_bb (test_bb);
2803 gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
2804
2805 t2 = build_int_cst (tm_state_type, 0);
2806 stmt = gimple_build_cond (NE_EXPR, t1, t2, NULL, NULL);
2807 gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
2808
2809 tm_log_emit_restores (region->entry_block, code_bb);
2810
2811 edge ei = make_edge (transaction_bb, test_bb, EDGE_FALLTHRU);
2812 edge et = make_edge (test_bb, code_bb, EDGE_TRUE_VALUE);
2813 edge ef = make_edge (test_bb, join_bb, EDGE_FALSE_VALUE);
2814 redirect_edge_pred (fallthru_edge, join_bb);
2815
2816 join_bb->frequency = test_bb->frequency = transaction_bb->frequency;
2817 join_bb->count = test_bb->count = transaction_bb->count;
2818
2819 ei->probability = PROB_ALWAYS;
2820 et->probability = PROB_LIKELY;
2821 ef->probability = PROB_UNLIKELY;
2822 et->count = apply_probability (test_bb->count, et->probability);
2823 ef->count = apply_probability (test_bb->count, ef->probability);
2824
2825 code_bb->count = et->count;
2826 code_bb->frequency = EDGE_FREQUENCY (et);
2827
2828 transaction_bb = join_bb;
2829 }
2830
2831 // If we have an ABORT edge, create a test to perform the abort.
2832 if (abort_edge)
2833 {
2834 basic_block test_bb = create_empty_bb (transaction_bb);
2835 add_bb_to_loop (test_bb, transaction_bb->loop_father);
2836 if (region->restart_block == region->entry_block)
2837 region->restart_block = test_bb;
2838
2839 tree t1 = create_tmp_reg (tm_state_type);
2840 tree t2 = build_int_cst (tm_state_type, A_ABORTTRANSACTION);
2841 gimple stmt = gimple_build_assign (t1, BIT_AND_EXPR, tm_state, t2);
2842 gimple_stmt_iterator gsi = gsi_last_bb (test_bb);
2843 gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
2844
2845 t2 = build_int_cst (tm_state_type, 0);
2846 stmt = gimple_build_cond (NE_EXPR, t1, t2, NULL, NULL);
2847 gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
2848
2849 edge ei = make_edge (transaction_bb, test_bb, EDGE_FALLTHRU);
2850 test_bb->frequency = transaction_bb->frequency;
2851 test_bb->count = transaction_bb->count;
2852 ei->probability = PROB_ALWAYS;
2853
2854 // Not abort edge. If both are live, chose one at random as we'll
2855 // we'll be fixing that up below.
2856 redirect_edge_pred (fallthru_edge, test_bb);
2857 fallthru_edge->flags = EDGE_FALSE_VALUE;
2858 fallthru_edge->probability = PROB_VERY_LIKELY;
2859 fallthru_edge->count
2860 = apply_probability (test_bb->count, fallthru_edge->probability);
2861
2862 // Abort/over edge.
2863 redirect_edge_pred (abort_edge, test_bb);
2864 abort_edge->flags = EDGE_TRUE_VALUE;
2865 abort_edge->probability = PROB_VERY_UNLIKELY;
2866 abort_edge->count
2867 = apply_probability (test_bb->count, abort_edge->probability);
2868
2869 transaction_bb = test_bb;
2870 }
2871
2872 // If we have both instrumented and uninstrumented code paths, select one.
2873 if (inst_edge && uninst_edge)
2874 {
2875 basic_block test_bb = create_empty_bb (transaction_bb);
2876 add_bb_to_loop (test_bb, transaction_bb->loop_father);
2877 if (region->restart_block == region->entry_block)
2878 region->restart_block = test_bb;
2879
2880 tree t1 = create_tmp_reg (tm_state_type);
2881 tree t2 = build_int_cst (tm_state_type, A_RUNUNINSTRUMENTEDCODE);
2882
2883 gimple stmt = gimple_build_assign (t1, BIT_AND_EXPR, tm_state, t2);
2884 gimple_stmt_iterator gsi = gsi_last_bb (test_bb);
2885 gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
2886
2887 t2 = build_int_cst (tm_state_type, 0);
2888 stmt = gimple_build_cond (NE_EXPR, t1, t2, NULL, NULL);
2889 gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
2890
2891 // Create the edge into test_bb first, as we want to copy values
2892 // out of the fallthru edge.
2893 edge e = make_edge (transaction_bb, test_bb, fallthru_edge->flags);
2894 e->probability = fallthru_edge->probability;
2895 test_bb->count = e->count = fallthru_edge->count;
2896 test_bb->frequency = EDGE_FREQUENCY (e);
2897
2898 // Now update the edges to the inst/uninist implementations.
2899 // For now assume that the paths are equally likely. When using HTM,
2900 // we'll try the uninst path first and fallback to inst path if htm
2901 // buffers are exceeded. Without HTM we start with the inst path and
2902 // use the uninst path when falling back to serial mode.
2903 redirect_edge_pred (inst_edge, test_bb);
2904 inst_edge->flags = EDGE_FALSE_VALUE;
2905 inst_edge->probability = REG_BR_PROB_BASE / 2;
2906 inst_edge->count
2907 = apply_probability (test_bb->count, inst_edge->probability);
2908
2909 redirect_edge_pred (uninst_edge, test_bb);
2910 uninst_edge->flags = EDGE_TRUE_VALUE;
2911 uninst_edge->probability = REG_BR_PROB_BASE / 2;
2912 uninst_edge->count
2913 = apply_probability (test_bb->count, uninst_edge->probability);
2914 }
2915
2916 // If we have no previous special cases, and we have PHIs at the beginning
2917 // of the atomic region, this means we have a loop at the beginning of the
2918 // atomic region that shares the first block. This can cause problems with
2919 // the transaction restart abnormal edges to be added in the tm_edges pass.
2920 // Solve this by adding a new empty block to receive the abnormal edges.
2921 if (region->restart_block == region->entry_block
2922 && phi_nodes (region->entry_block))
2923 {
2924 basic_block empty_bb = create_empty_bb (transaction_bb);
2925 region->restart_block = empty_bb;
2926 add_bb_to_loop (empty_bb, transaction_bb->loop_father);
2927
2928 redirect_edge_pred (fallthru_edge, empty_bb);
2929 make_edge (transaction_bb, empty_bb, EDGE_FALLTHRU);
2930 }
2931
2932 return NULL;
2933 }
2934
2935 /* Generate the temporary to be used for the return value of
2936 BUILT_IN_TM_START. */
2937
2938 static void *
2939 generate_tm_state (struct tm_region *region, void *data ATTRIBUTE_UNUSED)
2940 {
2941 tree tm_start = builtin_decl_explicit (BUILT_IN_TM_START);
2942 region->tm_state =
2943 create_tmp_reg (TREE_TYPE (TREE_TYPE (tm_start)), "tm_state");
2944
2945 // Reset the subcode, post optimizations. We'll fill this in
2946 // again as we process blocks.
2947 if (region->exit_blocks)
2948 {
2949 gtransaction *transaction_stmt = region->get_transaction_stmt ();
2950 unsigned int subcode = gimple_transaction_subcode (transaction_stmt);
2951
2952 if (subcode & GTMA_DOES_GO_IRREVOCABLE)
2953 subcode &= (GTMA_DECLARATION_MASK | GTMA_DOES_GO_IRREVOCABLE
2954 | GTMA_MAY_ENTER_IRREVOCABLE
2955 | GTMA_HAS_NO_INSTRUMENTATION);
2956 else
2957 subcode &= GTMA_DECLARATION_MASK;
2958 gimple_transaction_set_subcode (transaction_stmt, subcode);
2959 }
2960
2961 return NULL;
2962 }
2963
2964 // Propagate flags from inner transactions outwards.
2965 static void
2966 propagate_tm_flags_out (struct tm_region *region)
2967 {
2968 if (region == NULL)
2969 return;
2970 propagate_tm_flags_out (region->inner);
2971
2972 if (region->outer && region->outer->transaction_stmt)
2973 {
2974 unsigned s
2975 = gimple_transaction_subcode (region->get_transaction_stmt ());
2976 s &= (GTMA_HAVE_ABORT | GTMA_HAVE_LOAD | GTMA_HAVE_STORE
2977 | GTMA_MAY_ENTER_IRREVOCABLE);
2978 s |= gimple_transaction_subcode (region->outer->get_transaction_stmt ());
2979 gimple_transaction_set_subcode (region->outer->get_transaction_stmt (),
2980 s);
2981 }
2982
2983 propagate_tm_flags_out (region->next);
2984 }
2985
2986 /* Entry point to the MARK phase of TM expansion. Here we replace
2987 transactional memory statements with calls to builtins, and function
2988 calls with their transactional clones (if available). But we don't
2989 yet lower GIMPLE_TRANSACTION or add the transaction restart back-edges. */
2990
2991 static unsigned int
2992 execute_tm_mark (void)
2993 {
2994 pending_edge_inserts_p = false;
2995
2996 expand_regions (all_tm_regions, generate_tm_state, NULL,
2997 /*traverse_clones=*/true);
2998
2999 tm_log_init ();
3000
3001 vec<tm_region_p> bb_regions
3002 = get_bb_regions_instrumented (/*traverse_clones=*/true,
3003 /*include_uninstrumented_p=*/false);
3004 struct tm_region *r;
3005 unsigned i;
3006
3007 // Expand memory operations into calls into the runtime.
3008 // This collects log entries as well.
3009 FOR_EACH_VEC_ELT (bb_regions, i, r)
3010 {
3011 if (r != NULL)
3012 {
3013 if (r->transaction_stmt)
3014 {
3015 unsigned sub
3016 = gimple_transaction_subcode (r->get_transaction_stmt ());
3017
3018 /* If we're sure to go irrevocable, there won't be
3019 anything to expand, since the run-time will go
3020 irrevocable right away. */
3021 if (sub & GTMA_DOES_GO_IRREVOCABLE
3022 && sub & GTMA_MAY_ENTER_IRREVOCABLE)
3023 continue;
3024 }
3025 expand_block_tm (r, BASIC_BLOCK_FOR_FN (cfun, i));
3026 }
3027 }
3028
3029 bb_regions.release ();
3030
3031 // Propagate flags from inner transactions outwards.
3032 propagate_tm_flags_out (all_tm_regions);
3033
3034 // Expand GIMPLE_TRANSACTIONs into calls into the runtime.
3035 expand_regions (all_tm_regions, expand_transaction, NULL,
3036 /*traverse_clones=*/false);
3037
3038 tm_log_emit ();
3039 tm_log_delete ();
3040
3041 if (pending_edge_inserts_p)
3042 gsi_commit_edge_inserts ();
3043 free_dominance_info (CDI_DOMINATORS);
3044 return 0;
3045 }
3046
3047 namespace {
3048
3049 const pass_data pass_data_tm_mark =
3050 {
3051 GIMPLE_PASS, /* type */
3052 "tmmark", /* name */
3053 OPTGROUP_NONE, /* optinfo_flags */
3054 TV_TRANS_MEM, /* tv_id */
3055 ( PROP_ssa | PROP_cfg ), /* properties_required */
3056 0, /* properties_provided */
3057 0, /* properties_destroyed */
3058 0, /* todo_flags_start */
3059 TODO_update_ssa, /* todo_flags_finish */
3060 };
3061
3062 class pass_tm_mark : public gimple_opt_pass
3063 {
3064 public:
3065 pass_tm_mark (gcc::context *ctxt)
3066 : gimple_opt_pass (pass_data_tm_mark, ctxt)
3067 {}
3068
3069 /* opt_pass methods: */
3070 virtual unsigned int execute (function *) { return execute_tm_mark (); }
3071
3072 }; // class pass_tm_mark
3073
3074 } // anon namespace
3075
3076 gimple_opt_pass *
3077 make_pass_tm_mark (gcc::context *ctxt)
3078 {
3079 return new pass_tm_mark (ctxt);
3080 }
3081 \f
3082
3083 /* Create an abnormal edge from STMT at iter, splitting the block
3084 as necessary. Adjust *PNEXT as needed for the split block. */
3085
3086 static inline void
3087 split_bb_make_tm_edge (gimple stmt, basic_block dest_bb,
3088 gimple_stmt_iterator iter, gimple_stmt_iterator *pnext)
3089 {
3090 basic_block bb = gimple_bb (stmt);
3091 if (!gsi_one_before_end_p (iter))
3092 {
3093 edge e = split_block (bb, stmt);
3094 *pnext = gsi_start_bb (e->dest);
3095 }
3096 make_edge (bb, dest_bb, EDGE_ABNORMAL);
3097
3098 // Record the need for the edge for the benefit of the rtl passes.
3099 if (cfun->gimple_df->tm_restart == NULL)
3100 cfun->gimple_df->tm_restart
3101 = hash_table<tm_restart_hasher>::create_ggc (31);
3102
3103 struct tm_restart_node dummy;
3104 dummy.stmt = stmt;
3105 dummy.label_or_list = gimple_block_label (dest_bb);
3106
3107 tm_restart_node **slot = cfun->gimple_df->tm_restart->find_slot (&dummy,
3108 INSERT);
3109 struct tm_restart_node *n = *slot;
3110 if (n == NULL)
3111 {
3112 n = ggc_alloc<tm_restart_node> ();
3113 *n = dummy;
3114 }
3115 else
3116 {
3117 tree old = n->label_or_list;
3118 if (TREE_CODE (old) == LABEL_DECL)
3119 old = tree_cons (NULL, old, NULL);
3120 n->label_or_list = tree_cons (NULL, dummy.label_or_list, old);
3121 }
3122 }
3123
3124 /* Split block BB as necessary for every builtin function we added, and
3125 wire up the abnormal back edges implied by the transaction restart. */
3126
3127 static void
3128 expand_block_edges (struct tm_region *const region, basic_block bb)
3129 {
3130 gimple_stmt_iterator gsi, next_gsi;
3131
3132 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi = next_gsi)
3133 {
3134 gimple stmt = gsi_stmt (gsi);
3135 gcall *call_stmt;
3136
3137 next_gsi = gsi;
3138 gsi_next (&next_gsi);
3139
3140 // ??? Shouldn't we split for any non-pure, non-irrevocable function?
3141 call_stmt = dyn_cast <gcall *> (stmt);
3142 if ((!call_stmt)
3143 || (gimple_call_flags (call_stmt) & ECF_TM_BUILTIN) == 0)
3144 continue;
3145
3146 if (DECL_FUNCTION_CODE (gimple_call_fndecl (call_stmt))
3147 == BUILT_IN_TM_ABORT)
3148 {
3149 // If we have a ``_transaction_cancel [[outer]]'', there is only
3150 // one abnormal edge: to the transaction marked OUTER.
3151 // All compiler-generated instances of BUILT_IN_TM_ABORT have a
3152 // constant argument, which we can examine here. Users invoking
3153 // TM_ABORT directly get what they deserve.
3154 tree arg = gimple_call_arg (call_stmt, 0);
3155 if (TREE_CODE (arg) == INTEGER_CST
3156 && (TREE_INT_CST_LOW (arg) & AR_OUTERABORT) != 0
3157 && !decl_is_tm_clone (current_function_decl))
3158 {
3159 // Find the GTMA_IS_OUTER transaction.
3160 for (struct tm_region *o = region; o; o = o->outer)
3161 if (o->original_transaction_was_outer)
3162 {
3163 split_bb_make_tm_edge (call_stmt, o->restart_block,
3164 gsi, &next_gsi);
3165 break;
3166 }
3167
3168 // Otherwise, the front-end should have semantically checked
3169 // outer aborts, but in either case the target region is not
3170 // within this function.
3171 continue;
3172 }
3173
3174 // Non-outer, TM aborts have an abnormal edge to the inner-most
3175 // transaction, the one being aborted;
3176 split_bb_make_tm_edge (call_stmt, region->restart_block, gsi,
3177 &next_gsi);
3178 }
3179
3180 // All TM builtins have an abnormal edge to the outer-most transaction.
3181 // We never restart inner transactions. For tm clones, we know a-priori
3182 // that the outer-most transaction is outside the function.
3183 if (decl_is_tm_clone (current_function_decl))
3184 continue;
3185
3186 if (cfun->gimple_df->tm_restart == NULL)
3187 cfun->gimple_df->tm_restart
3188 = hash_table<tm_restart_hasher>::create_ggc (31);
3189
3190 // All TM builtins have an abnormal edge to the outer-most transaction.
3191 // We never restart inner transactions.
3192 for (struct tm_region *o = region; o; o = o->outer)
3193 if (!o->outer)
3194 {
3195 split_bb_make_tm_edge (call_stmt, o->restart_block, gsi, &next_gsi);
3196 break;
3197 }
3198
3199 // Delete any tail-call annotation that may have been added.
3200 // The tail-call pass may have mis-identified the commit as being
3201 // a candidate because we had not yet added this restart edge.
3202 gimple_call_set_tail (call_stmt, false);
3203 }
3204 }
3205
3206 /* Entry point to the final expansion of transactional nodes. */
3207
3208 namespace {
3209
3210 const pass_data pass_data_tm_edges =
3211 {
3212 GIMPLE_PASS, /* type */
3213 "tmedge", /* name */
3214 OPTGROUP_NONE, /* optinfo_flags */
3215 TV_TRANS_MEM, /* tv_id */
3216 ( PROP_ssa | PROP_cfg ), /* properties_required */
3217 0, /* properties_provided */
3218 0, /* properties_destroyed */
3219 0, /* todo_flags_start */
3220 TODO_update_ssa, /* todo_flags_finish */
3221 };
3222
3223 class pass_tm_edges : public gimple_opt_pass
3224 {
3225 public:
3226 pass_tm_edges (gcc::context *ctxt)
3227 : gimple_opt_pass (pass_data_tm_edges, ctxt)
3228 {}
3229
3230 /* opt_pass methods: */
3231 virtual unsigned int execute (function *);
3232
3233 }; // class pass_tm_edges
3234
3235 unsigned int
3236 pass_tm_edges::execute (function *fun)
3237 {
3238 vec<tm_region_p> bb_regions
3239 = get_bb_regions_instrumented (/*traverse_clones=*/false,
3240 /*include_uninstrumented_p=*/true);
3241 struct tm_region *r;
3242 unsigned i;
3243
3244 FOR_EACH_VEC_ELT (bb_regions, i, r)
3245 if (r != NULL)
3246 expand_block_edges (r, BASIC_BLOCK_FOR_FN (fun, i));
3247
3248 bb_regions.release ();
3249
3250 /* We've got to release the dominance info now, to indicate that it
3251 must be rebuilt completely. Otherwise we'll crash trying to update
3252 the SSA web in the TODO section following this pass. */
3253 free_dominance_info (CDI_DOMINATORS);
3254 bitmap_obstack_release (&tm_obstack);
3255 all_tm_regions = NULL;
3256
3257 return 0;
3258 }
3259
3260 } // anon namespace
3261
3262 gimple_opt_pass *
3263 make_pass_tm_edges (gcc::context *ctxt)
3264 {
3265 return new pass_tm_edges (ctxt);
3266 }
3267 \f
3268 /* Helper function for expand_regions. Expand REGION and recurse to
3269 the inner region. Call CALLBACK on each region. CALLBACK returns
3270 NULL to continue the traversal, otherwise a non-null value which
3271 this function will return as well. TRAVERSE_CLONES is true if we
3272 should traverse transactional clones. */
3273
3274 static void *
3275 expand_regions_1 (struct tm_region *region,
3276 void *(*callback)(struct tm_region *, void *),
3277 void *data,
3278 bool traverse_clones)
3279 {
3280 void *retval = NULL;
3281 if (region->exit_blocks
3282 || (traverse_clones && decl_is_tm_clone (current_function_decl)))
3283 {
3284 retval = callback (region, data);
3285 if (retval)
3286 return retval;
3287 }
3288 if (region->inner)
3289 {
3290 retval = expand_regions (region->inner, callback, data, traverse_clones);
3291 if (retval)
3292 return retval;
3293 }
3294 return retval;
3295 }
3296
3297 /* Traverse the regions enclosed and including REGION. Execute
3298 CALLBACK for each region, passing DATA. CALLBACK returns NULL to
3299 continue the traversal, otherwise a non-null value which this
3300 function will return as well. TRAVERSE_CLONES is true if we should
3301 traverse transactional clones. */
3302
3303 static void *
3304 expand_regions (struct tm_region *region,
3305 void *(*callback)(struct tm_region *, void *),
3306 void *data,
3307 bool traverse_clones)
3308 {
3309 void *retval = NULL;
3310 while (region)
3311 {
3312 retval = expand_regions_1 (region, callback, data, traverse_clones);
3313 if (retval)
3314 return retval;
3315 region = region->next;
3316 }
3317 return retval;
3318 }
3319
3320 \f
3321 /* A unique TM memory operation. */
3322 typedef struct tm_memop
3323 {
3324 /* Unique ID that all memory operations to the same location have. */
3325 unsigned int value_id;
3326 /* Address of load/store. */
3327 tree addr;
3328 } *tm_memop_t;
3329
3330 /* TM memory operation hashtable helpers. */
3331
3332 struct tm_memop_hasher : free_ptr_hash <tm_memop>
3333 {
3334 static inline hashval_t hash (const tm_memop *);
3335 static inline bool equal (const tm_memop *, const tm_memop *);
3336 };
3337
3338 /* Htab support. Return a hash value for a `tm_memop'. */
3339 inline hashval_t
3340 tm_memop_hasher::hash (const tm_memop *mem)
3341 {
3342 tree addr = mem->addr;
3343 /* We drill down to the SSA_NAME/DECL for the hash, but equality is
3344 actually done with operand_equal_p (see tm_memop_eq). */
3345 if (TREE_CODE (addr) == ADDR_EXPR)
3346 addr = TREE_OPERAND (addr, 0);
3347 return iterative_hash_expr (addr, 0);
3348 }
3349
3350 /* Htab support. Return true if two tm_memop's are the same. */
3351 inline bool
3352 tm_memop_hasher::equal (const tm_memop *mem1, const tm_memop *mem2)
3353 {
3354 return operand_equal_p (mem1->addr, mem2->addr, 0);
3355 }
3356
3357 /* Sets for solving data flow equations in the memory optimization pass. */
3358 struct tm_memopt_bitmaps
3359 {
3360 /* Stores available to this BB upon entry. Basically, stores that
3361 dominate this BB. */
3362 bitmap store_avail_in;
3363 /* Stores available at the end of this BB. */
3364 bitmap store_avail_out;
3365 bitmap store_antic_in;
3366 bitmap store_antic_out;
3367 /* Reads available to this BB upon entry. Basically, reads that
3368 dominate this BB. */
3369 bitmap read_avail_in;
3370 /* Reads available at the end of this BB. */
3371 bitmap read_avail_out;
3372 /* Reads performed in this BB. */
3373 bitmap read_local;
3374 /* Writes performed in this BB. */
3375 bitmap store_local;
3376
3377 /* Temporary storage for pass. */
3378 /* Is the current BB in the worklist? */
3379 bool avail_in_worklist_p;
3380 /* Have we visited this BB? */
3381 bool visited_p;
3382 };
3383
3384 static bitmap_obstack tm_memopt_obstack;
3385
3386 /* Unique counter for TM loads and stores. Loads and stores of the
3387 same address get the same ID. */
3388 static unsigned int tm_memopt_value_id;
3389 static hash_table<tm_memop_hasher> *tm_memopt_value_numbers;
3390
3391 #define STORE_AVAIL_IN(BB) \
3392 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_avail_in
3393 #define STORE_AVAIL_OUT(BB) \
3394 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_avail_out
3395 #define STORE_ANTIC_IN(BB) \
3396 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_antic_in
3397 #define STORE_ANTIC_OUT(BB) \
3398 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_antic_out
3399 #define READ_AVAIL_IN(BB) \
3400 ((struct tm_memopt_bitmaps *) ((BB)->aux))->read_avail_in
3401 #define READ_AVAIL_OUT(BB) \
3402 ((struct tm_memopt_bitmaps *) ((BB)->aux))->read_avail_out
3403 #define READ_LOCAL(BB) \
3404 ((struct tm_memopt_bitmaps *) ((BB)->aux))->read_local
3405 #define STORE_LOCAL(BB) \
3406 ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_local
3407 #define AVAIL_IN_WORKLIST_P(BB) \
3408 ((struct tm_memopt_bitmaps *) ((BB)->aux))->avail_in_worklist_p
3409 #define BB_VISITED_P(BB) \
3410 ((struct tm_memopt_bitmaps *) ((BB)->aux))->visited_p
3411
3412 /* Given a TM load/store in STMT, return the value number for the address
3413 it accesses. */
3414
3415 static unsigned int
3416 tm_memopt_value_number (gimple stmt, enum insert_option op)
3417 {
3418 struct tm_memop tmpmem, *mem;
3419 tm_memop **slot;
3420
3421 gcc_assert (is_tm_load (stmt) || is_tm_store (stmt));
3422 tmpmem.addr = gimple_call_arg (stmt, 0);
3423 slot = tm_memopt_value_numbers->find_slot (&tmpmem, op);
3424 if (*slot)
3425 mem = *slot;
3426 else if (op == INSERT)
3427 {
3428 mem = XNEW (struct tm_memop);
3429 *slot = mem;
3430 mem->value_id = tm_memopt_value_id++;
3431 mem->addr = tmpmem.addr;
3432 }
3433 else
3434 gcc_unreachable ();
3435 return mem->value_id;
3436 }
3437
3438 /* Accumulate TM memory operations in BB into STORE_LOCAL and READ_LOCAL. */
3439
3440 static void
3441 tm_memopt_accumulate_memops (basic_block bb)
3442 {
3443 gimple_stmt_iterator gsi;
3444
3445 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3446 {
3447 gimple stmt = gsi_stmt (gsi);
3448 bitmap bits;
3449 unsigned int loc;
3450
3451 if (is_tm_store (stmt))
3452 bits = STORE_LOCAL (bb);
3453 else if (is_tm_load (stmt))
3454 bits = READ_LOCAL (bb);
3455 else
3456 continue;
3457
3458 loc = tm_memopt_value_number (stmt, INSERT);
3459 bitmap_set_bit (bits, loc);
3460 if (dump_file)
3461 {
3462 fprintf (dump_file, "TM memopt (%s): value num=%d, BB=%d, addr=",
3463 is_tm_load (stmt) ? "LOAD" : "STORE", loc,
3464 gimple_bb (stmt)->index);
3465 print_generic_expr (dump_file, gimple_call_arg (stmt, 0), 0);
3466 fprintf (dump_file, "\n");
3467 }
3468 }
3469 }
3470
3471 /* Prettily dump one of the memopt sets. BITS is the bitmap to dump. */
3472
3473 static void
3474 dump_tm_memopt_set (const char *set_name, bitmap bits)
3475 {
3476 unsigned i;
3477 bitmap_iterator bi;
3478 const char *comma = "";
3479
3480 fprintf (dump_file, "TM memopt: %s: [", set_name);
3481 EXECUTE_IF_SET_IN_BITMAP (bits, 0, i, bi)
3482 {
3483 hash_table<tm_memop_hasher>::iterator hi;
3484 struct tm_memop *mem = NULL;
3485
3486 /* Yeah, yeah, yeah. Whatever. This is just for debugging. */
3487 FOR_EACH_HASH_TABLE_ELEMENT (*tm_memopt_value_numbers, mem, tm_memop_t, hi)
3488 if (mem->value_id == i)
3489 break;
3490 gcc_assert (mem->value_id == i);
3491 fprintf (dump_file, "%s", comma);
3492 comma = ", ";
3493 print_generic_expr (dump_file, mem->addr, 0);
3494 }
3495 fprintf (dump_file, "]\n");
3496 }
3497
3498 /* Prettily dump all of the memopt sets in BLOCKS. */
3499
3500 static void
3501 dump_tm_memopt_sets (vec<basic_block> blocks)
3502 {
3503 size_t i;
3504 basic_block bb;
3505
3506 for (i = 0; blocks.iterate (i, &bb); ++i)
3507 {
3508 fprintf (dump_file, "------------BB %d---------\n", bb->index);
3509 dump_tm_memopt_set ("STORE_LOCAL", STORE_LOCAL (bb));
3510 dump_tm_memopt_set ("READ_LOCAL", READ_LOCAL (bb));
3511 dump_tm_memopt_set ("STORE_AVAIL_IN", STORE_AVAIL_IN (bb));
3512 dump_tm_memopt_set ("STORE_AVAIL_OUT", STORE_AVAIL_OUT (bb));
3513 dump_tm_memopt_set ("READ_AVAIL_IN", READ_AVAIL_IN (bb));
3514 dump_tm_memopt_set ("READ_AVAIL_OUT", READ_AVAIL_OUT (bb));
3515 }
3516 }
3517
3518 /* Compute {STORE,READ}_AVAIL_IN for the basic block BB. */
3519
3520 static void
3521 tm_memopt_compute_avin (basic_block bb)
3522 {
3523 edge e;
3524 unsigned ix;
3525
3526 /* Seed with the AVOUT of any predecessor. */
3527 for (ix = 0; ix < EDGE_COUNT (bb->preds); ix++)
3528 {
3529 e = EDGE_PRED (bb, ix);
3530 /* Make sure we have already visited this BB, and is thus
3531 initialized.
3532
3533 If e->src->aux is NULL, this predecessor is actually on an
3534 enclosing transaction. We only care about the current
3535 transaction, so ignore it. */
3536 if (e->src->aux && BB_VISITED_P (e->src))
3537 {
3538 bitmap_copy (STORE_AVAIL_IN (bb), STORE_AVAIL_OUT (e->src));
3539 bitmap_copy (READ_AVAIL_IN (bb), READ_AVAIL_OUT (e->src));
3540 break;
3541 }
3542 }
3543
3544 for (; ix < EDGE_COUNT (bb->preds); ix++)
3545 {
3546 e = EDGE_PRED (bb, ix);
3547 if (e->src->aux && BB_VISITED_P (e->src))
3548 {
3549 bitmap_and_into (STORE_AVAIL_IN (bb), STORE_AVAIL_OUT (e->src));
3550 bitmap_and_into (READ_AVAIL_IN (bb), READ_AVAIL_OUT (e->src));
3551 }
3552 }
3553
3554 BB_VISITED_P (bb) = true;
3555 }
3556
3557 /* Compute the STORE_ANTIC_IN for the basic block BB. */
3558
3559 static void
3560 tm_memopt_compute_antin (basic_block bb)
3561 {
3562 edge e;
3563 unsigned ix;
3564
3565 /* Seed with the ANTIC_OUT of any successor. */
3566 for (ix = 0; ix < EDGE_COUNT (bb->succs); ix++)
3567 {
3568 e = EDGE_SUCC (bb, ix);
3569 /* Make sure we have already visited this BB, and is thus
3570 initialized. */
3571 if (BB_VISITED_P (e->dest))
3572 {
3573 bitmap_copy (STORE_ANTIC_IN (bb), STORE_ANTIC_OUT (e->dest));
3574 break;
3575 }
3576 }
3577
3578 for (; ix < EDGE_COUNT (bb->succs); ix++)
3579 {
3580 e = EDGE_SUCC (bb, ix);
3581 if (BB_VISITED_P (e->dest))
3582 bitmap_and_into (STORE_ANTIC_IN (bb), STORE_ANTIC_OUT (e->dest));
3583 }
3584
3585 BB_VISITED_P (bb) = true;
3586 }
3587
3588 /* Compute the AVAIL sets for every basic block in BLOCKS.
3589
3590 We compute {STORE,READ}_AVAIL_{OUT,IN} as follows:
3591
3592 AVAIL_OUT[bb] = union (AVAIL_IN[bb], LOCAL[bb])
3593 AVAIL_IN[bb] = intersect (AVAIL_OUT[predecessors])
3594
3595 This is basically what we do in lcm's compute_available(), but here
3596 we calculate two sets of sets (one for STOREs and one for READs),
3597 and we work on a region instead of the entire CFG.
3598
3599 REGION is the TM region.
3600 BLOCKS are the basic blocks in the region. */
3601
3602 static void
3603 tm_memopt_compute_available (struct tm_region *region,
3604 vec<basic_block> blocks)
3605 {
3606 edge e;
3607 basic_block *worklist, *qin, *qout, *qend, bb;
3608 unsigned int qlen, i;
3609 edge_iterator ei;
3610 bool changed;
3611
3612 /* Allocate a worklist array/queue. Entries are only added to the
3613 list if they were not already on the list. So the size is
3614 bounded by the number of basic blocks in the region. */
3615 qlen = blocks.length () - 1;
3616 qin = qout = worklist =
3617 XNEWVEC (basic_block, qlen);
3618
3619 /* Put every block in the region on the worklist. */
3620 for (i = 0; blocks.iterate (i, &bb); ++i)
3621 {
3622 /* Seed AVAIL_OUT with the LOCAL set. */
3623 bitmap_ior_into (STORE_AVAIL_OUT (bb), STORE_LOCAL (bb));
3624 bitmap_ior_into (READ_AVAIL_OUT (bb), READ_LOCAL (bb));
3625
3626 AVAIL_IN_WORKLIST_P (bb) = true;
3627 /* No need to insert the entry block, since it has an AVIN of
3628 null, and an AVOUT that has already been seeded in. */
3629 if (bb != region->entry_block)
3630 *qin++ = bb;
3631 }
3632
3633 /* The entry block has been initialized with the local sets. */
3634 BB_VISITED_P (region->entry_block) = true;
3635
3636 qin = worklist;
3637 qend = &worklist[qlen];
3638
3639 /* Iterate until the worklist is empty. */
3640 while (qlen)
3641 {
3642 /* Take the first entry off the worklist. */
3643 bb = *qout++;
3644 qlen--;
3645
3646 if (qout >= qend)
3647 qout = worklist;
3648
3649 /* This block can be added to the worklist again if necessary. */
3650 AVAIL_IN_WORKLIST_P (bb) = false;
3651 tm_memopt_compute_avin (bb);
3652
3653 /* Note: We do not add the LOCAL sets here because we already
3654 seeded the AVAIL_OUT sets with them. */
3655 changed = bitmap_ior_into (STORE_AVAIL_OUT (bb), STORE_AVAIL_IN (bb));
3656 changed |= bitmap_ior_into (READ_AVAIL_OUT (bb), READ_AVAIL_IN (bb));
3657 if (changed
3658 && (region->exit_blocks == NULL
3659 || !bitmap_bit_p (region->exit_blocks, bb->index)))
3660 /* If the out state of this block changed, then we need to add
3661 its successors to the worklist if they are not already in. */
3662 FOR_EACH_EDGE (e, ei, bb->succs)
3663 if (!AVAIL_IN_WORKLIST_P (e->dest)
3664 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
3665 {
3666 *qin++ = e->dest;
3667 AVAIL_IN_WORKLIST_P (e->dest) = true;
3668 qlen++;
3669
3670 if (qin >= qend)
3671 qin = worklist;
3672 }
3673 }
3674
3675 free (worklist);
3676
3677 if (dump_file)
3678 dump_tm_memopt_sets (blocks);
3679 }
3680
3681 /* Compute ANTIC sets for every basic block in BLOCKS.
3682
3683 We compute STORE_ANTIC_OUT as follows:
3684
3685 STORE_ANTIC_OUT[bb] = union(STORE_ANTIC_IN[bb], STORE_LOCAL[bb])
3686 STORE_ANTIC_IN[bb] = intersect(STORE_ANTIC_OUT[successors])
3687
3688 REGION is the TM region.
3689 BLOCKS are the basic blocks in the region. */
3690
3691 static void
3692 tm_memopt_compute_antic (struct tm_region *region,
3693 vec<basic_block> blocks)
3694 {
3695 edge e;
3696 basic_block *worklist, *qin, *qout, *qend, bb;
3697 unsigned int qlen;
3698 int i;
3699 edge_iterator ei;
3700
3701 /* Allocate a worklist array/queue. Entries are only added to the
3702 list if they were not already on the list. So the size is
3703 bounded by the number of basic blocks in the region. */
3704 qin = qout = worklist = XNEWVEC (basic_block, blocks.length ());
3705
3706 for (qlen = 0, i = blocks.length () - 1; i >= 0; --i)
3707 {
3708 bb = blocks[i];
3709
3710 /* Seed ANTIC_OUT with the LOCAL set. */
3711 bitmap_ior_into (STORE_ANTIC_OUT (bb), STORE_LOCAL (bb));
3712
3713 /* Put every block in the region on the worklist. */
3714 AVAIL_IN_WORKLIST_P (bb) = true;
3715 /* No need to insert exit blocks, since their ANTIC_IN is NULL,
3716 and their ANTIC_OUT has already been seeded in. */
3717 if (region->exit_blocks
3718 && !bitmap_bit_p (region->exit_blocks, bb->index))
3719 {
3720 qlen++;
3721 *qin++ = bb;
3722 }
3723 }
3724
3725 /* The exit blocks have been initialized with the local sets. */
3726 if (region->exit_blocks)
3727 {
3728 unsigned int i;
3729 bitmap_iterator bi;
3730 EXECUTE_IF_SET_IN_BITMAP (region->exit_blocks, 0, i, bi)
3731 BB_VISITED_P (BASIC_BLOCK_FOR_FN (cfun, i)) = true;
3732 }
3733
3734 qin = worklist;
3735 qend = &worklist[qlen];
3736
3737 /* Iterate until the worklist is empty. */
3738 while (qlen)
3739 {
3740 /* Take the first entry off the worklist. */
3741 bb = *qout++;
3742 qlen--;
3743
3744 if (qout >= qend)
3745 qout = worklist;
3746
3747 /* This block can be added to the worklist again if necessary. */
3748 AVAIL_IN_WORKLIST_P (bb) = false;
3749 tm_memopt_compute_antin (bb);
3750
3751 /* Note: We do not add the LOCAL sets here because we already
3752 seeded the ANTIC_OUT sets with them. */
3753 if (bitmap_ior_into (STORE_ANTIC_OUT (bb), STORE_ANTIC_IN (bb))
3754 && bb != region->entry_block)
3755 /* If the out state of this block changed, then we need to add
3756 its predecessors to the worklist if they are not already in. */
3757 FOR_EACH_EDGE (e, ei, bb->preds)
3758 if (!AVAIL_IN_WORKLIST_P (e->src))
3759 {
3760 *qin++ = e->src;
3761 AVAIL_IN_WORKLIST_P (e->src) = true;
3762 qlen++;
3763
3764 if (qin >= qend)
3765 qin = worklist;
3766 }
3767 }
3768
3769 free (worklist);
3770
3771 if (dump_file)
3772 dump_tm_memopt_sets (blocks);
3773 }
3774
3775 /* Offsets of load variants from TM_LOAD. For example,
3776 BUILT_IN_TM_LOAD_RAR* is an offset of 1 from BUILT_IN_TM_LOAD*.
3777 See gtm-builtins.def. */
3778 #define TRANSFORM_RAR 1
3779 #define TRANSFORM_RAW 2
3780 #define TRANSFORM_RFW 3
3781 /* Offsets of store variants from TM_STORE. */
3782 #define TRANSFORM_WAR 1
3783 #define TRANSFORM_WAW 2
3784
3785 /* Inform about a load/store optimization. */
3786
3787 static void
3788 dump_tm_memopt_transform (gimple stmt)
3789 {
3790 if (dump_file)
3791 {
3792 fprintf (dump_file, "TM memopt: transforming: ");
3793 print_gimple_stmt (dump_file, stmt, 0, 0);
3794 fprintf (dump_file, "\n");
3795 }
3796 }
3797
3798 /* Perform a read/write optimization. Replaces the TM builtin in STMT
3799 by a builtin that is OFFSET entries down in the builtins table in
3800 gtm-builtins.def. */
3801
3802 static void
3803 tm_memopt_transform_stmt (unsigned int offset,
3804 gcall *stmt,
3805 gimple_stmt_iterator *gsi)
3806 {
3807 tree fn = gimple_call_fn (stmt);
3808 gcc_assert (TREE_CODE (fn) == ADDR_EXPR);
3809 TREE_OPERAND (fn, 0)
3810 = builtin_decl_explicit ((enum built_in_function)
3811 (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0))
3812 + offset));
3813 gimple_call_set_fn (stmt, fn);
3814 gsi_replace (gsi, stmt, true);
3815 dump_tm_memopt_transform (stmt);
3816 }
3817
3818 /* Perform the actual TM memory optimization transformations in the
3819 basic blocks in BLOCKS. */
3820
3821 static void
3822 tm_memopt_transform_blocks (vec<basic_block> blocks)
3823 {
3824 size_t i;
3825 basic_block bb;
3826 gimple_stmt_iterator gsi;
3827
3828 for (i = 0; blocks.iterate (i, &bb); ++i)
3829 {
3830 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3831 {
3832 gimple stmt = gsi_stmt (gsi);
3833 bitmap read_avail = READ_AVAIL_IN (bb);
3834 bitmap store_avail = STORE_AVAIL_IN (bb);
3835 bitmap store_antic = STORE_ANTIC_OUT (bb);
3836 unsigned int loc;
3837
3838 if (is_tm_simple_load (stmt))
3839 {
3840 gcall *call_stmt = as_a <gcall *> (stmt);
3841 loc = tm_memopt_value_number (stmt, NO_INSERT);
3842 if (store_avail && bitmap_bit_p (store_avail, loc))
3843 tm_memopt_transform_stmt (TRANSFORM_RAW, call_stmt, &gsi);
3844 else if (store_antic && bitmap_bit_p (store_antic, loc))
3845 {
3846 tm_memopt_transform_stmt (TRANSFORM_RFW, call_stmt, &gsi);
3847 bitmap_set_bit (store_avail, loc);
3848 }
3849 else if (read_avail && bitmap_bit_p (read_avail, loc))
3850 tm_memopt_transform_stmt (TRANSFORM_RAR, call_stmt, &gsi);
3851 else
3852 bitmap_set_bit (read_avail, loc);
3853 }
3854 else if (is_tm_simple_store (stmt))
3855 {
3856 gcall *call_stmt = as_a <gcall *> (stmt);
3857 loc = tm_memopt_value_number (stmt, NO_INSERT);
3858 if (store_avail && bitmap_bit_p (store_avail, loc))
3859 tm_memopt_transform_stmt (TRANSFORM_WAW, call_stmt, &gsi);
3860 else
3861 {
3862 if (read_avail && bitmap_bit_p (read_avail, loc))
3863 tm_memopt_transform_stmt (TRANSFORM_WAR, call_stmt, &gsi);
3864 bitmap_set_bit (store_avail, loc);
3865 }
3866 }
3867 }
3868 }
3869 }
3870
3871 /* Return a new set of bitmaps for a BB. */
3872
3873 static struct tm_memopt_bitmaps *
3874 tm_memopt_init_sets (void)
3875 {
3876 struct tm_memopt_bitmaps *b
3877 = XOBNEW (&tm_memopt_obstack.obstack, struct tm_memopt_bitmaps);
3878 b->store_avail_in = BITMAP_ALLOC (&tm_memopt_obstack);
3879 b->store_avail_out = BITMAP_ALLOC (&tm_memopt_obstack);
3880 b->store_antic_in = BITMAP_ALLOC (&tm_memopt_obstack);
3881 b->store_antic_out = BITMAP_ALLOC (&tm_memopt_obstack);
3882 b->store_avail_out = BITMAP_ALLOC (&tm_memopt_obstack);
3883 b->read_avail_in = BITMAP_ALLOC (&tm_memopt_obstack);
3884 b->read_avail_out = BITMAP_ALLOC (&tm_memopt_obstack);
3885 b->read_local = BITMAP_ALLOC (&tm_memopt_obstack);
3886 b->store_local = BITMAP_ALLOC (&tm_memopt_obstack);
3887 return b;
3888 }
3889
3890 /* Free sets computed for each BB. */
3891
3892 static void
3893 tm_memopt_free_sets (vec<basic_block> blocks)
3894 {
3895 size_t i;
3896 basic_block bb;
3897
3898 for (i = 0; blocks.iterate (i, &bb); ++i)
3899 bb->aux = NULL;
3900 }
3901
3902 /* Clear the visited bit for every basic block in BLOCKS. */
3903
3904 static void
3905 tm_memopt_clear_visited (vec<basic_block> blocks)
3906 {
3907 size_t i;
3908 basic_block bb;
3909
3910 for (i = 0; blocks.iterate (i, &bb); ++i)
3911 BB_VISITED_P (bb) = false;
3912 }
3913
3914 /* Replace TM load/stores with hints for the runtime. We handle
3915 things like read-after-write, write-after-read, read-after-read,
3916 read-for-write, etc. */
3917
3918 static unsigned int
3919 execute_tm_memopt (void)
3920 {
3921 struct tm_region *region;
3922 vec<basic_block> bbs;
3923
3924 tm_memopt_value_id = 0;
3925 tm_memopt_value_numbers = new hash_table<tm_memop_hasher> (10);
3926
3927 for (region = all_tm_regions; region; region = region->next)
3928 {
3929 /* All the TM stores/loads in the current region. */
3930 size_t i;
3931 basic_block bb;
3932
3933 bitmap_obstack_initialize (&tm_memopt_obstack);
3934
3935 /* Save all BBs for the current region. */
3936 bbs = get_tm_region_blocks (region->entry_block,
3937 region->exit_blocks,
3938 region->irr_blocks,
3939 NULL,
3940 false);
3941
3942 /* Collect all the memory operations. */
3943 for (i = 0; bbs.iterate (i, &bb); ++i)
3944 {
3945 bb->aux = tm_memopt_init_sets ();
3946 tm_memopt_accumulate_memops (bb);
3947 }
3948
3949 /* Solve data flow equations and transform each block accordingly. */
3950 tm_memopt_clear_visited (bbs);
3951 tm_memopt_compute_available (region, bbs);
3952 tm_memopt_clear_visited (bbs);
3953 tm_memopt_compute_antic (region, bbs);
3954 tm_memopt_transform_blocks (bbs);
3955
3956 tm_memopt_free_sets (bbs);
3957 bbs.release ();
3958 bitmap_obstack_release (&tm_memopt_obstack);
3959 tm_memopt_value_numbers->empty ();
3960 }
3961
3962 delete tm_memopt_value_numbers;
3963 tm_memopt_value_numbers = NULL;
3964 return 0;
3965 }
3966
3967 namespace {
3968
3969 const pass_data pass_data_tm_memopt =
3970 {
3971 GIMPLE_PASS, /* type */
3972 "tmmemopt", /* name */
3973 OPTGROUP_NONE, /* optinfo_flags */
3974 TV_TRANS_MEM, /* tv_id */
3975 ( PROP_ssa | PROP_cfg ), /* properties_required */
3976 0, /* properties_provided */
3977 0, /* properties_destroyed */
3978 0, /* todo_flags_start */
3979 0, /* todo_flags_finish */
3980 };
3981
3982 class pass_tm_memopt : public gimple_opt_pass
3983 {
3984 public:
3985 pass_tm_memopt (gcc::context *ctxt)
3986 : gimple_opt_pass (pass_data_tm_memopt, ctxt)
3987 {}
3988
3989 /* opt_pass methods: */
3990 virtual bool gate (function *) { return flag_tm && optimize > 0; }
3991 virtual unsigned int execute (function *) { return execute_tm_memopt (); }
3992
3993 }; // class pass_tm_memopt
3994
3995 } // anon namespace
3996
3997 gimple_opt_pass *
3998 make_pass_tm_memopt (gcc::context *ctxt)
3999 {
4000 return new pass_tm_memopt (ctxt);
4001 }
4002
4003 \f
4004 /* Interprocedual analysis for the creation of transactional clones.
4005 The aim of this pass is to find which functions are referenced in
4006 a non-irrevocable transaction context, and for those over which
4007 we have control (or user directive), create a version of the
4008 function which uses only the transactional interface to reference
4009 protected memories. This analysis proceeds in several steps:
4010
4011 (1) Collect the set of all possible transactional clones:
4012
4013 (a) For all local public functions marked tm_callable, push
4014 it onto the tm_callee queue.
4015
4016 (b) For all local functions, scan for calls in transaction blocks.
4017 Push the caller and callee onto the tm_caller and tm_callee
4018 queues. Count the number of callers for each callee.
4019
4020 (c) For each local function on the callee list, assume we will
4021 create a transactional clone. Push *all* calls onto the
4022 callee queues; count the number of clone callers separately
4023 to the number of original callers.
4024
4025 (2) Propagate irrevocable status up the dominator tree:
4026
4027 (a) Any external function on the callee list that is not marked
4028 tm_callable is irrevocable. Push all callers of such onto
4029 a worklist.
4030
4031 (b) For each function on the worklist, mark each block that
4032 contains an irrevocable call. Use the AND operator to
4033 propagate that mark up the dominator tree.
4034
4035 (c) If we reach the entry block for a possible transactional
4036 clone, then the transactional clone is irrevocable, and
4037 we should not create the clone after all. Push all
4038 callers onto the worklist.
4039
4040 (d) Place tm_irrevocable calls at the beginning of the relevant
4041 blocks. Special case here is the entry block for the entire
4042 transaction region; there we mark it GTMA_DOES_GO_IRREVOCABLE for
4043 the library to begin the region in serial mode. Decrement
4044 the call count for all callees in the irrevocable region.
4045
4046 (3) Create the transactional clones:
4047
4048 Any tm_callee that still has a non-zero call count is cloned.
4049 */
4050
4051 /* This structure is stored in the AUX field of each cgraph_node. */
4052 struct tm_ipa_cg_data
4053 {
4054 /* The clone of the function that got created. */
4055 struct cgraph_node *clone;
4056
4057 /* The tm regions in the normal function. */
4058 struct tm_region *all_tm_regions;
4059
4060 /* The blocks of the normal/clone functions that contain irrevocable
4061 calls, or blocks that are post-dominated by irrevocable calls. */
4062 bitmap irrevocable_blocks_normal;
4063 bitmap irrevocable_blocks_clone;
4064
4065 /* The blocks of the normal function that are involved in transactions. */
4066 bitmap transaction_blocks_normal;
4067
4068 /* The number of callers to the transactional clone of this function
4069 from normal and transactional clones respectively. */
4070 unsigned tm_callers_normal;
4071 unsigned tm_callers_clone;
4072
4073 /* True if all calls to this function's transactional clone
4074 are irrevocable. Also automatically true if the function
4075 has no transactional clone. */
4076 bool is_irrevocable;
4077
4078 /* Flags indicating the presence of this function in various queues. */
4079 bool in_callee_queue;
4080 bool in_worklist;
4081
4082 /* Flags indicating the kind of scan desired while in the worklist. */
4083 bool want_irr_scan_normal;
4084 };
4085
4086 typedef vec<cgraph_node *> cgraph_node_queue;
4087
4088 /* Return the ipa data associated with NODE, allocating zeroed memory
4089 if necessary. TRAVERSE_ALIASES is true if we must traverse aliases
4090 and set *NODE accordingly. */
4091
4092 static struct tm_ipa_cg_data *
4093 get_cg_data (struct cgraph_node **node, bool traverse_aliases)
4094 {
4095 struct tm_ipa_cg_data *d;
4096
4097 if (traverse_aliases && (*node)->alias)
4098 *node = (*node)->get_alias_target ();
4099
4100 d = (struct tm_ipa_cg_data *) (*node)->aux;
4101
4102 if (d == NULL)
4103 {
4104 d = (struct tm_ipa_cg_data *)
4105 obstack_alloc (&tm_obstack.obstack, sizeof (*d));
4106 (*node)->aux = (void *) d;
4107 memset (d, 0, sizeof (*d));
4108 }
4109
4110 return d;
4111 }
4112
4113 /* Add NODE to the end of QUEUE, unless IN_QUEUE_P indicates that
4114 it is already present. */
4115
4116 static void
4117 maybe_push_queue (struct cgraph_node *node,
4118 cgraph_node_queue *queue_p, bool *in_queue_p)
4119 {
4120 if (!*in_queue_p)
4121 {
4122 *in_queue_p = true;
4123 queue_p->safe_push (node);
4124 }
4125 }
4126
4127 /* Duplicate the basic blocks in QUEUE for use in the uninstrumented
4128 code path. QUEUE are the basic blocks inside the transaction
4129 represented in REGION.
4130
4131 Later in split_code_paths() we will add the conditional to choose
4132 between the two alternatives. */
4133
4134 static void
4135 ipa_uninstrument_transaction (struct tm_region *region,
4136 vec<basic_block> queue)
4137 {
4138 gimple transaction = region->transaction_stmt;
4139 basic_block transaction_bb = gimple_bb (transaction);
4140 int n = queue.length ();
4141 basic_block *new_bbs = XNEWVEC (basic_block, n);
4142
4143 copy_bbs (queue.address (), n, new_bbs, NULL, 0, NULL, NULL, transaction_bb,
4144 true);
4145 edge e = make_edge (transaction_bb, new_bbs[0], EDGE_TM_UNINSTRUMENTED);
4146 add_phi_args_after_copy (new_bbs, n, e);
4147
4148 // Now we will have a GIMPLE_ATOMIC with 3 possible edges out of it.
4149 // a) EDGE_FALLTHRU into the transaction
4150 // b) EDGE_TM_ABORT out of the transaction
4151 // c) EDGE_TM_UNINSTRUMENTED into the uninstrumented blocks.
4152
4153 free (new_bbs);
4154 }
4155
4156 /* A subroutine of ipa_tm_scan_calls_transaction and ipa_tm_scan_calls_clone.
4157 Queue all callees within block BB. */
4158
4159 static void
4160 ipa_tm_scan_calls_block (cgraph_node_queue *callees_p,
4161 basic_block bb, bool for_clone)
4162 {
4163 gimple_stmt_iterator gsi;
4164
4165 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4166 {
4167 gimple stmt = gsi_stmt (gsi);
4168 if (is_gimple_call (stmt) && !is_tm_pure_call (stmt))
4169 {
4170 tree fndecl = gimple_call_fndecl (stmt);
4171 if (fndecl)
4172 {
4173 struct tm_ipa_cg_data *d;
4174 unsigned *pcallers;
4175 struct cgraph_node *node;
4176
4177 if (is_tm_ending_fndecl (fndecl))
4178 continue;
4179 if (find_tm_replacement_function (fndecl))
4180 continue;
4181
4182 node = cgraph_node::get (fndecl);
4183 gcc_assert (node != NULL);
4184 d = get_cg_data (&node, true);
4185
4186 pcallers = (for_clone ? &d->tm_callers_clone
4187 : &d->tm_callers_normal);
4188 *pcallers += 1;
4189
4190 maybe_push_queue (node, callees_p, &d->in_callee_queue);
4191 }
4192 }
4193 }
4194 }
4195
4196 /* Scan all calls in NODE that are within a transaction region,
4197 and push the resulting nodes into the callee queue. */
4198
4199 static void
4200 ipa_tm_scan_calls_transaction (struct tm_ipa_cg_data *d,
4201 cgraph_node_queue *callees_p)
4202 {
4203 struct tm_region *r;
4204
4205 d->transaction_blocks_normal = BITMAP_ALLOC (&tm_obstack);
4206 d->all_tm_regions = all_tm_regions;
4207
4208 for (r = all_tm_regions; r; r = r->next)
4209 {
4210 vec<basic_block> bbs;
4211 basic_block bb;
4212 unsigned i;
4213
4214 bbs = get_tm_region_blocks (r->entry_block, r->exit_blocks, NULL,
4215 d->transaction_blocks_normal, false);
4216
4217 // Generate the uninstrumented code path for this transaction.
4218 ipa_uninstrument_transaction (r, bbs);
4219
4220 FOR_EACH_VEC_ELT (bbs, i, bb)
4221 ipa_tm_scan_calls_block (callees_p, bb, false);
4222
4223 bbs.release ();
4224 }
4225
4226 // ??? copy_bbs should maintain cgraph edges for the blocks as it is
4227 // copying them, rather than forcing us to do this externally.
4228 cgraph_edge::rebuild_edges ();
4229
4230 // ??? In ipa_uninstrument_transaction we don't try to update dominators
4231 // because copy_bbs doesn't return a VEC like iterate_fix_dominators expects.
4232 // Instead, just release dominators here so update_ssa recomputes them.
4233 free_dominance_info (CDI_DOMINATORS);
4234
4235 // When building the uninstrumented code path, copy_bbs will have invoked
4236 // create_new_def_for starting an "ssa update context". There is only one
4237 // instance of this context, so resolve ssa updates before moving on to
4238 // the next function.
4239 update_ssa (TODO_update_ssa);
4240 }
4241
4242 /* Scan all calls in NODE as if this is the transactional clone,
4243 and push the destinations into the callee queue. */
4244
4245 static void
4246 ipa_tm_scan_calls_clone (struct cgraph_node *node,
4247 cgraph_node_queue *callees_p)
4248 {
4249 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
4250 basic_block bb;
4251
4252 FOR_EACH_BB_FN (bb, fn)
4253 ipa_tm_scan_calls_block (callees_p, bb, true);
4254 }
4255
4256 /* The function NODE has been detected to be irrevocable. Push all
4257 of its callers onto WORKLIST for the purpose of re-scanning them. */
4258
4259 static void
4260 ipa_tm_note_irrevocable (struct cgraph_node *node,
4261 cgraph_node_queue *worklist_p)
4262 {
4263 struct tm_ipa_cg_data *d = get_cg_data (&node, true);
4264 struct cgraph_edge *e;
4265
4266 d->is_irrevocable = true;
4267
4268 for (e = node->callers; e ; e = e->next_caller)
4269 {
4270 basic_block bb;
4271 struct cgraph_node *caller;
4272
4273 /* Don't examine recursive calls. */
4274 if (e->caller == node)
4275 continue;
4276 /* Even if we think we can go irrevocable, believe the user
4277 above all. */
4278 if (is_tm_safe_or_pure (e->caller->decl))
4279 continue;
4280
4281 caller = e->caller;
4282 d = get_cg_data (&caller, true);
4283
4284 /* Check if the callee is in a transactional region. If so,
4285 schedule the function for normal re-scan as well. */
4286 bb = gimple_bb (e->call_stmt);
4287 gcc_assert (bb != NULL);
4288 if (d->transaction_blocks_normal
4289 && bitmap_bit_p (d->transaction_blocks_normal, bb->index))
4290 d->want_irr_scan_normal = true;
4291
4292 maybe_push_queue (caller, worklist_p, &d->in_worklist);
4293 }
4294 }
4295
4296 /* A subroutine of ipa_tm_scan_irr_blocks; return true iff any statement
4297 within the block is irrevocable. */
4298
4299 static bool
4300 ipa_tm_scan_irr_block (basic_block bb)
4301 {
4302 gimple_stmt_iterator gsi;
4303 tree fn;
4304
4305 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4306 {
4307 gimple stmt = gsi_stmt (gsi);
4308 switch (gimple_code (stmt))
4309 {
4310 case GIMPLE_ASSIGN:
4311 if (gimple_assign_single_p (stmt))
4312 {
4313 tree lhs = gimple_assign_lhs (stmt);
4314 tree rhs = gimple_assign_rhs1 (stmt);
4315 if (volatile_var_p (lhs) || volatile_var_p (rhs))
4316 return true;
4317 }
4318 break;
4319
4320 case GIMPLE_CALL:
4321 {
4322 tree lhs = gimple_call_lhs (stmt);
4323 if (lhs && volatile_var_p (lhs))
4324 return true;
4325
4326 if (is_tm_pure_call (stmt))
4327 break;
4328
4329 fn = gimple_call_fn (stmt);
4330
4331 /* Functions with the attribute are by definition irrevocable. */
4332 if (is_tm_irrevocable (fn))
4333 return true;
4334
4335 /* For direct function calls, go ahead and check for replacement
4336 functions, or transitive irrevocable functions. For indirect
4337 functions, we'll ask the runtime. */
4338 if (TREE_CODE (fn) == ADDR_EXPR)
4339 {
4340 struct tm_ipa_cg_data *d;
4341 struct cgraph_node *node;
4342
4343 fn = TREE_OPERAND (fn, 0);
4344 if (is_tm_ending_fndecl (fn))
4345 break;
4346 if (find_tm_replacement_function (fn))
4347 break;
4348
4349 node = cgraph_node::get (fn);
4350 d = get_cg_data (&node, true);
4351
4352 /* Return true if irrevocable, but above all, believe
4353 the user. */
4354 if (d->is_irrevocable
4355 && !is_tm_safe_or_pure (fn))
4356 return true;
4357 }
4358 break;
4359 }
4360
4361 case GIMPLE_ASM:
4362 /* ??? The Approved Method of indicating that an inline
4363 assembly statement is not relevant to the transaction
4364 is to wrap it in a __tm_waiver block. This is not
4365 yet implemented, so we can't check for it. */
4366 if (is_tm_safe (current_function_decl))
4367 {
4368 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
4369 SET_EXPR_LOCATION (t, gimple_location (stmt));
4370 error ("%Kasm not allowed in %<transaction_safe%> function", t);
4371 }
4372 return true;
4373
4374 default:
4375 break;
4376 }
4377 }
4378
4379 return false;
4380 }
4381
4382 /* For each of the blocks seeded witin PQUEUE, walk the CFG looking
4383 for new irrevocable blocks, marking them in NEW_IRR. Don't bother
4384 scanning past OLD_IRR or EXIT_BLOCKS. */
4385
4386 static bool
4387 ipa_tm_scan_irr_blocks (vec<basic_block> *pqueue, bitmap new_irr,
4388 bitmap old_irr, bitmap exit_blocks)
4389 {
4390 bool any_new_irr = false;
4391 edge e;
4392 edge_iterator ei;
4393 bitmap visited_blocks = BITMAP_ALLOC (NULL);
4394
4395 do
4396 {
4397 basic_block bb = pqueue->pop ();
4398
4399 /* Don't re-scan blocks we know already are irrevocable. */
4400 if (old_irr && bitmap_bit_p (old_irr, bb->index))
4401 continue;
4402
4403 if (ipa_tm_scan_irr_block (bb))
4404 {
4405 bitmap_set_bit (new_irr, bb->index);
4406 any_new_irr = true;
4407 }
4408 else if (exit_blocks == NULL || !bitmap_bit_p (exit_blocks, bb->index))
4409 {
4410 FOR_EACH_EDGE (e, ei, bb->succs)
4411 if (!bitmap_bit_p (visited_blocks, e->dest->index))
4412 {
4413 bitmap_set_bit (visited_blocks, e->dest->index);
4414 pqueue->safe_push (e->dest);
4415 }
4416 }
4417 }
4418 while (!pqueue->is_empty ());
4419
4420 BITMAP_FREE (visited_blocks);
4421
4422 return any_new_irr;
4423 }
4424
4425 /* Propagate the irrevocable property both up and down the dominator tree.
4426 BB is the current block being scanned; EXIT_BLOCKS are the edges of the
4427 TM regions; OLD_IRR are the results of a previous scan of the dominator
4428 tree which has been fully propagated; NEW_IRR is the set of new blocks
4429 which are gaining the irrevocable property during the current scan. */
4430
4431 static void
4432 ipa_tm_propagate_irr (basic_block entry_block, bitmap new_irr,
4433 bitmap old_irr, bitmap exit_blocks)
4434 {
4435 vec<basic_block> bbs;
4436 bitmap all_region_blocks;
4437
4438 /* If this block is in the old set, no need to rescan. */
4439 if (old_irr && bitmap_bit_p (old_irr, entry_block->index))
4440 return;
4441
4442 all_region_blocks = BITMAP_ALLOC (&tm_obstack);
4443 bbs = get_tm_region_blocks (entry_block, exit_blocks, NULL,
4444 all_region_blocks, false);
4445 do
4446 {
4447 basic_block bb = bbs.pop ();
4448 bool this_irr = bitmap_bit_p (new_irr, bb->index);
4449 bool all_son_irr = false;
4450 edge_iterator ei;
4451 edge e;
4452
4453 /* Propagate up. If my children are, I am too, but we must have
4454 at least one child that is. */
4455 if (!this_irr)
4456 {
4457 FOR_EACH_EDGE (e, ei, bb->succs)
4458 {
4459 if (!bitmap_bit_p (new_irr, e->dest->index))
4460 {
4461 all_son_irr = false;
4462 break;
4463 }
4464 else
4465 all_son_irr = true;
4466 }
4467 if (all_son_irr)
4468 {
4469 /* Add block to new_irr if it hasn't already been processed. */
4470 if (!old_irr || !bitmap_bit_p (old_irr, bb->index))
4471 {
4472 bitmap_set_bit (new_irr, bb->index);
4473 this_irr = true;
4474 }
4475 }
4476 }
4477
4478 /* Propagate down to everyone we immediately dominate. */
4479 if (this_irr)
4480 {
4481 basic_block son;
4482 for (son = first_dom_son (CDI_DOMINATORS, bb);
4483 son;
4484 son = next_dom_son (CDI_DOMINATORS, son))
4485 {
4486 /* Make sure block is actually in a TM region, and it
4487 isn't already in old_irr. */
4488 if ((!old_irr || !bitmap_bit_p (old_irr, son->index))
4489 && bitmap_bit_p (all_region_blocks, son->index))
4490 bitmap_set_bit (new_irr, son->index);
4491 }
4492 }
4493 }
4494 while (!bbs.is_empty ());
4495
4496 BITMAP_FREE (all_region_blocks);
4497 bbs.release ();
4498 }
4499
4500 static void
4501 ipa_tm_decrement_clone_counts (basic_block bb, bool for_clone)
4502 {
4503 gimple_stmt_iterator gsi;
4504
4505 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4506 {
4507 gimple stmt = gsi_stmt (gsi);
4508 if (is_gimple_call (stmt) && !is_tm_pure_call (stmt))
4509 {
4510 tree fndecl = gimple_call_fndecl (stmt);
4511 if (fndecl)
4512 {
4513 struct tm_ipa_cg_data *d;
4514 unsigned *pcallers;
4515 struct cgraph_node *tnode;
4516
4517 if (is_tm_ending_fndecl (fndecl))
4518 continue;
4519 if (find_tm_replacement_function (fndecl))
4520 continue;
4521
4522 tnode = cgraph_node::get (fndecl);
4523 d = get_cg_data (&tnode, true);
4524
4525 pcallers = (for_clone ? &d->tm_callers_clone
4526 : &d->tm_callers_normal);
4527
4528 gcc_assert (*pcallers > 0);
4529 *pcallers -= 1;
4530 }
4531 }
4532 }
4533 }
4534
4535 /* (Re-)Scan the transaction blocks in NODE for calls to irrevocable functions,
4536 as well as other irrevocable actions such as inline assembly. Mark all
4537 such blocks as irrevocable and decrement the number of calls to
4538 transactional clones. Return true if, for the transactional clone, the
4539 entire function is irrevocable. */
4540
4541 static bool
4542 ipa_tm_scan_irr_function (struct cgraph_node *node, bool for_clone)
4543 {
4544 struct tm_ipa_cg_data *d;
4545 bitmap new_irr, old_irr;
4546 bool ret = false;
4547
4548 /* Builtin operators (operator new, and such). */
4549 if (DECL_STRUCT_FUNCTION (node->decl) == NULL
4550 || DECL_STRUCT_FUNCTION (node->decl)->cfg == NULL)
4551 return false;
4552
4553 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
4554 calculate_dominance_info (CDI_DOMINATORS);
4555
4556 d = get_cg_data (&node, true);
4557 auto_vec<basic_block, 10> queue;
4558 new_irr = BITMAP_ALLOC (&tm_obstack);
4559
4560 /* Scan each tm region, propagating irrevocable status through the tree. */
4561 if (for_clone)
4562 {
4563 old_irr = d->irrevocable_blocks_clone;
4564 queue.quick_push (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
4565 if (ipa_tm_scan_irr_blocks (&queue, new_irr, old_irr, NULL))
4566 {
4567 ipa_tm_propagate_irr (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
4568 new_irr,
4569 old_irr, NULL);
4570 ret = bitmap_bit_p (new_irr,
4571 single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun))->index);
4572 }
4573 }
4574 else
4575 {
4576 struct tm_region *region;
4577
4578 old_irr = d->irrevocable_blocks_normal;
4579 for (region = d->all_tm_regions; region; region = region->next)
4580 {
4581 queue.quick_push (region->entry_block);
4582 if (ipa_tm_scan_irr_blocks (&queue, new_irr, old_irr,
4583 region->exit_blocks))
4584 ipa_tm_propagate_irr (region->entry_block, new_irr, old_irr,
4585 region->exit_blocks);
4586 }
4587 }
4588
4589 /* If we found any new irrevocable blocks, reduce the call count for
4590 transactional clones within the irrevocable blocks. Save the new
4591 set of irrevocable blocks for next time. */
4592 if (!bitmap_empty_p (new_irr))
4593 {
4594 bitmap_iterator bmi;
4595 unsigned i;
4596
4597 EXECUTE_IF_SET_IN_BITMAP (new_irr, 0, i, bmi)
4598 ipa_tm_decrement_clone_counts (BASIC_BLOCK_FOR_FN (cfun, i),
4599 for_clone);
4600
4601 if (old_irr)
4602 {
4603 bitmap_ior_into (old_irr, new_irr);
4604 BITMAP_FREE (new_irr);
4605 }
4606 else if (for_clone)
4607 d->irrevocable_blocks_clone = new_irr;
4608 else
4609 d->irrevocable_blocks_normal = new_irr;
4610
4611 if (dump_file && new_irr)
4612 {
4613 const char *dname;
4614 bitmap_iterator bmi;
4615 unsigned i;
4616
4617 dname = lang_hooks.decl_printable_name (current_function_decl, 2);
4618 EXECUTE_IF_SET_IN_BITMAP (new_irr, 0, i, bmi)
4619 fprintf (dump_file, "%s: bb %d goes irrevocable\n", dname, i);
4620 }
4621 }
4622 else
4623 BITMAP_FREE (new_irr);
4624
4625 pop_cfun ();
4626
4627 return ret;
4628 }
4629
4630 /* Return true if, for the transactional clone of NODE, any call
4631 may enter irrevocable mode. */
4632
4633 static bool
4634 ipa_tm_mayenterirr_function (struct cgraph_node *node)
4635 {
4636 struct tm_ipa_cg_data *d;
4637 tree decl;
4638 unsigned flags;
4639
4640 d = get_cg_data (&node, true);
4641 decl = node->decl;
4642 flags = flags_from_decl_or_type (decl);
4643
4644 /* Handle some TM builtins. Ordinarily these aren't actually generated
4645 at this point, but handling these functions when written in by the
4646 user makes it easier to build unit tests. */
4647 if (flags & ECF_TM_BUILTIN)
4648 return false;
4649
4650 /* Filter out all functions that are marked. */
4651 if (flags & ECF_TM_PURE)
4652 return false;
4653 if (is_tm_safe (decl))
4654 return false;
4655 if (is_tm_irrevocable (decl))
4656 return true;
4657 if (is_tm_callable (decl))
4658 return true;
4659 if (find_tm_replacement_function (decl))
4660 return true;
4661
4662 /* If we aren't seeing the final version of the function we don't
4663 know what it will contain at runtime. */
4664 if (node->get_availability () < AVAIL_AVAILABLE)
4665 return true;
4666
4667 /* If the function must go irrevocable, then of course true. */
4668 if (d->is_irrevocable)
4669 return true;
4670
4671 /* If there are any blocks marked irrevocable, then the function
4672 as a whole may enter irrevocable. */
4673 if (d->irrevocable_blocks_clone)
4674 return true;
4675
4676 /* We may have previously marked this function as tm_may_enter_irr;
4677 see pass_diagnose_tm_blocks. */
4678 if (node->local.tm_may_enter_irr)
4679 return true;
4680
4681 /* Recurse on the main body for aliases. In general, this will
4682 result in one of the bits above being set so that we will not
4683 have to recurse next time. */
4684 if (node->alias)
4685 return ipa_tm_mayenterirr_function (cgraph_node::get (node->thunk.alias));
4686
4687 /* What remains is unmarked local functions without items that force
4688 the function to go irrevocable. */
4689 return false;
4690 }
4691
4692 /* Diagnose calls from transaction_safe functions to unmarked
4693 functions that are determined to not be safe. */
4694
4695 static void
4696 ipa_tm_diagnose_tm_safe (struct cgraph_node *node)
4697 {
4698 struct cgraph_edge *e;
4699
4700 for (e = node->callees; e ; e = e->next_callee)
4701 if (!is_tm_callable (e->callee->decl)
4702 && e->callee->local.tm_may_enter_irr)
4703 error_at (gimple_location (e->call_stmt),
4704 "unsafe function call %qD within "
4705 "%<transaction_safe%> function", e->callee->decl);
4706 }
4707
4708 /* Diagnose call from atomic transactions to unmarked functions
4709 that are determined to not be safe. */
4710
4711 static void
4712 ipa_tm_diagnose_transaction (struct cgraph_node *node,
4713 struct tm_region *all_tm_regions)
4714 {
4715 struct tm_region *r;
4716
4717 for (r = all_tm_regions; r ; r = r->next)
4718 if (gimple_transaction_subcode (r->get_transaction_stmt ())
4719 & GTMA_IS_RELAXED)
4720 {
4721 /* Atomic transactions can be nested inside relaxed. */
4722 if (r->inner)
4723 ipa_tm_diagnose_transaction (node, r->inner);
4724 }
4725 else
4726 {
4727 vec<basic_block> bbs;
4728 gimple_stmt_iterator gsi;
4729 basic_block bb;
4730 size_t i;
4731
4732 bbs = get_tm_region_blocks (r->entry_block, r->exit_blocks,
4733 r->irr_blocks, NULL, false);
4734
4735 for (i = 0; bbs.iterate (i, &bb); ++i)
4736 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4737 {
4738 gimple stmt = gsi_stmt (gsi);
4739 tree fndecl;
4740
4741 if (gimple_code (stmt) == GIMPLE_ASM)
4742 {
4743 error_at (gimple_location (stmt),
4744 "asm not allowed in atomic transaction");
4745 continue;
4746 }
4747
4748 if (!is_gimple_call (stmt))
4749 continue;
4750 fndecl = gimple_call_fndecl (stmt);
4751
4752 /* Indirect function calls have been diagnosed already. */
4753 if (!fndecl)
4754 continue;
4755
4756 /* Stop at the end of the transaction. */
4757 if (is_tm_ending_fndecl (fndecl))
4758 {
4759 if (bitmap_bit_p (r->exit_blocks, bb->index))
4760 break;
4761 continue;
4762 }
4763
4764 /* Marked functions have been diagnosed already. */
4765 if (is_tm_pure_call (stmt))
4766 continue;
4767 if (is_tm_callable (fndecl))
4768 continue;
4769
4770 if (cgraph_node::local_info (fndecl)->tm_may_enter_irr)
4771 error_at (gimple_location (stmt),
4772 "unsafe function call %qD within "
4773 "atomic transaction", fndecl);
4774 }
4775
4776 bbs.release ();
4777 }
4778 }
4779
4780 /* Return a transactional mangled name for the DECL_ASSEMBLER_NAME in
4781 OLD_DECL. The returned value is a freshly malloced pointer that
4782 should be freed by the caller. */
4783
4784 static tree
4785 tm_mangle (tree old_asm_id)
4786 {
4787 const char *old_asm_name;
4788 char *tm_name;
4789 void *alloc = NULL;
4790 struct demangle_component *dc;
4791 tree new_asm_id;
4792
4793 /* Determine if the symbol is already a valid C++ mangled name. Do this
4794 even for C, which might be interfacing with C++ code via appropriately
4795 ugly identifiers. */
4796 /* ??? We could probably do just as well checking for "_Z" and be done. */
4797 old_asm_name = IDENTIFIER_POINTER (old_asm_id);
4798 dc = cplus_demangle_v3_components (old_asm_name, DMGL_NO_OPTS, &alloc);
4799
4800 if (dc == NULL)
4801 {
4802 char length[8];
4803
4804 do_unencoded:
4805 sprintf (length, "%u", IDENTIFIER_LENGTH (old_asm_id));
4806 tm_name = concat ("_ZGTt", length, old_asm_name, NULL);
4807 }
4808 else
4809 {
4810 old_asm_name += 2; /* Skip _Z */
4811
4812 switch (dc->type)
4813 {
4814 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4815 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4816 /* Don't play silly games, you! */
4817 goto do_unencoded;
4818
4819 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4820 /* I'd really like to know if we can ever be passed one of
4821 these from the C++ front end. The Logical Thing would
4822 seem that hidden-alias should be outer-most, so that we
4823 get hidden-alias of a transaction-clone and not vice-versa. */
4824 old_asm_name += 2;
4825 break;
4826
4827 default:
4828 break;
4829 }
4830
4831 tm_name = concat ("_ZGTt", old_asm_name, NULL);
4832 }
4833 free (alloc);
4834
4835 new_asm_id = get_identifier (tm_name);
4836 free (tm_name);
4837
4838 return new_asm_id;
4839 }
4840
4841 static inline void
4842 ipa_tm_mark_force_output_node (struct cgraph_node *node)
4843 {
4844 node->mark_force_output ();
4845 node->analyzed = true;
4846 }
4847
4848 static inline void
4849 ipa_tm_mark_forced_by_abi_node (struct cgraph_node *node)
4850 {
4851 node->forced_by_abi = true;
4852 node->analyzed = true;
4853 }
4854
4855 /* Callback data for ipa_tm_create_version_alias. */
4856 struct create_version_alias_info
4857 {
4858 struct cgraph_node *old_node;
4859 tree new_decl;
4860 };
4861
4862 /* A subroutine of ipa_tm_create_version, called via
4863 cgraph_for_node_and_aliases. Create new tm clones for each of
4864 the existing aliases. */
4865 static bool
4866 ipa_tm_create_version_alias (struct cgraph_node *node, void *data)
4867 {
4868 struct create_version_alias_info *info
4869 = (struct create_version_alias_info *)data;
4870 tree old_decl, new_decl, tm_name;
4871 struct cgraph_node *new_node;
4872
4873 if (!node->cpp_implicit_alias)
4874 return false;
4875
4876 old_decl = node->decl;
4877 tm_name = tm_mangle (DECL_ASSEMBLER_NAME (old_decl));
4878 new_decl = build_decl (DECL_SOURCE_LOCATION (old_decl),
4879 TREE_CODE (old_decl), tm_name,
4880 TREE_TYPE (old_decl));
4881
4882 SET_DECL_ASSEMBLER_NAME (new_decl, tm_name);
4883 SET_DECL_RTL (new_decl, NULL);
4884
4885 /* Based loosely on C++'s make_alias_for(). */
4886 TREE_PUBLIC (new_decl) = TREE_PUBLIC (old_decl);
4887 DECL_CONTEXT (new_decl) = DECL_CONTEXT (old_decl);
4888 DECL_LANG_SPECIFIC (new_decl) = DECL_LANG_SPECIFIC (old_decl);
4889 TREE_READONLY (new_decl) = TREE_READONLY (old_decl);
4890 DECL_EXTERNAL (new_decl) = 0;
4891 DECL_ARTIFICIAL (new_decl) = 1;
4892 TREE_ADDRESSABLE (new_decl) = 1;
4893 TREE_USED (new_decl) = 1;
4894 TREE_SYMBOL_REFERENCED (tm_name) = 1;
4895
4896 /* Perform the same remapping to the comdat group. */
4897 if (DECL_ONE_ONLY (new_decl))
4898 varpool_node::get (new_decl)->set_comdat_group
4899 (tm_mangle (decl_comdat_group_id (old_decl)));
4900
4901 new_node = cgraph_node::create_same_body_alias (new_decl, info->new_decl);
4902 new_node->tm_clone = true;
4903 new_node->externally_visible = info->old_node->externally_visible;
4904 new_node->no_reorder = info->old_node->no_reorder;
4905 /* ?? Do not traverse aliases here. */
4906 get_cg_data (&node, false)->clone = new_node;
4907
4908 record_tm_clone_pair (old_decl, new_decl);
4909
4910 if (info->old_node->force_output
4911 || info->old_node->ref_list.first_referring ())
4912 ipa_tm_mark_force_output_node (new_node);
4913 if (info->old_node->forced_by_abi)
4914 ipa_tm_mark_forced_by_abi_node (new_node);
4915 return false;
4916 }
4917
4918 /* Create a copy of the function (possibly declaration only) of OLD_NODE,
4919 appropriate for the transactional clone. */
4920
4921 static void
4922 ipa_tm_create_version (struct cgraph_node *old_node)
4923 {
4924 tree new_decl, old_decl, tm_name;
4925 struct cgraph_node *new_node;
4926
4927 old_decl = old_node->decl;
4928 new_decl = copy_node (old_decl);
4929
4930 /* DECL_ASSEMBLER_NAME needs to be set before we call
4931 cgraph_copy_node_for_versioning below, because cgraph_node will
4932 fill the assembler_name_hash. */
4933 tm_name = tm_mangle (DECL_ASSEMBLER_NAME (old_decl));
4934 SET_DECL_ASSEMBLER_NAME (new_decl, tm_name);
4935 SET_DECL_RTL (new_decl, NULL);
4936 TREE_SYMBOL_REFERENCED (tm_name) = 1;
4937
4938 /* Perform the same remapping to the comdat group. */
4939 if (DECL_ONE_ONLY (new_decl))
4940 varpool_node::get (new_decl)->set_comdat_group
4941 (tm_mangle (DECL_COMDAT_GROUP (old_decl)));
4942
4943 gcc_assert (!old_node->ipa_transforms_to_apply.exists ());
4944 new_node = old_node->create_version_clone (new_decl, vNULL, NULL);
4945 new_node->local.local = false;
4946 new_node->externally_visible = old_node->externally_visible;
4947 new_node->lowered = true;
4948 new_node->tm_clone = 1;
4949 if (!old_node->implicit_section)
4950 new_node->set_section (old_node->get_section ());
4951 get_cg_data (&old_node, true)->clone = new_node;
4952
4953 if (old_node->get_availability () >= AVAIL_INTERPOSABLE)
4954 {
4955 /* Remap extern inline to static inline. */
4956 /* ??? Is it worth trying to use make_decl_one_only? */
4957 if (DECL_DECLARED_INLINE_P (new_decl) && DECL_EXTERNAL (new_decl))
4958 {
4959 DECL_EXTERNAL (new_decl) = 0;
4960 TREE_PUBLIC (new_decl) = 0;
4961 DECL_WEAK (new_decl) = 0;
4962 }
4963
4964 tree_function_versioning (old_decl, new_decl,
4965 NULL, false, NULL,
4966 false, NULL, NULL);
4967 }
4968
4969 record_tm_clone_pair (old_decl, new_decl);
4970
4971 symtab->call_cgraph_insertion_hooks (new_node);
4972 if (old_node->force_output
4973 || old_node->ref_list.first_referring ())
4974 ipa_tm_mark_force_output_node (new_node);
4975 if (old_node->forced_by_abi)
4976 ipa_tm_mark_forced_by_abi_node (new_node);
4977
4978 /* Do the same thing, but for any aliases of the original node. */
4979 {
4980 struct create_version_alias_info data;
4981 data.old_node = old_node;
4982 data.new_decl = new_decl;
4983 old_node->call_for_symbol_thunks_and_aliases (ipa_tm_create_version_alias,
4984 &data, true);
4985 }
4986 }
4987
4988 /* Construct a call to TM_IRREVOCABLE and insert it at the beginning of BB. */
4989
4990 static void
4991 ipa_tm_insert_irr_call (struct cgraph_node *node, struct tm_region *region,
4992 basic_block bb)
4993 {
4994 gimple_stmt_iterator gsi;
4995 gcall *g;
4996
4997 transaction_subcode_ior (region, GTMA_MAY_ENTER_IRREVOCABLE);
4998
4999 g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_IRREVOCABLE),
5000 1, build_int_cst (NULL_TREE, MODE_SERIALIRREVOCABLE));
5001
5002 split_block_after_labels (bb);
5003 gsi = gsi_after_labels (bb);
5004 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5005
5006 node->create_edge (cgraph_node::get_create
5007 (builtin_decl_explicit (BUILT_IN_TM_IRREVOCABLE)),
5008 g, 0,
5009 compute_call_stmt_bb_frequency (node->decl,
5010 gimple_bb (g)));
5011 }
5012
5013 /* Construct a call to TM_GETTMCLONE and insert it before GSI. */
5014
5015 static bool
5016 ipa_tm_insert_gettmclone_call (struct cgraph_node *node,
5017 struct tm_region *region,
5018 gimple_stmt_iterator *gsi, gcall *stmt)
5019 {
5020 tree gettm_fn, ret, old_fn, callfn;
5021 gcall *g;
5022 gassign *g2;
5023 bool safe;
5024
5025 old_fn = gimple_call_fn (stmt);
5026
5027 if (TREE_CODE (old_fn) == ADDR_EXPR)
5028 {
5029 tree fndecl = TREE_OPERAND (old_fn, 0);
5030 tree clone = get_tm_clone_pair (fndecl);
5031
5032 /* By transforming the call into a TM_GETTMCLONE, we are
5033 technically taking the address of the original function and
5034 its clone. Explain this so inlining will know this function
5035 is needed. */
5036 cgraph_node::get (fndecl)->mark_address_taken () ;
5037 if (clone)
5038 cgraph_node::get (clone)->mark_address_taken ();
5039 }
5040
5041 safe = is_tm_safe (TREE_TYPE (old_fn));
5042 gettm_fn = builtin_decl_explicit (safe ? BUILT_IN_TM_GETTMCLONE_SAFE
5043 : BUILT_IN_TM_GETTMCLONE_IRR);
5044 ret = create_tmp_var (ptr_type_node);
5045
5046 if (!safe)
5047 transaction_subcode_ior (region, GTMA_MAY_ENTER_IRREVOCABLE);
5048
5049 /* Discard OBJ_TYPE_REF, since we weren't able to fold it. */
5050 if (TREE_CODE (old_fn) == OBJ_TYPE_REF)
5051 old_fn = OBJ_TYPE_REF_EXPR (old_fn);
5052
5053 g = gimple_build_call (gettm_fn, 1, old_fn);
5054 ret = make_ssa_name (ret, g);
5055 gimple_call_set_lhs (g, ret);
5056
5057 gsi_insert_before (gsi, g, GSI_SAME_STMT);
5058
5059 node->create_edge (cgraph_node::get_create (gettm_fn), g, 0,
5060 compute_call_stmt_bb_frequency (node->decl,
5061 gimple_bb (g)));
5062
5063 /* Cast return value from tm_gettmclone* into appropriate function
5064 pointer. */
5065 callfn = create_tmp_var (TREE_TYPE (old_fn));
5066 g2 = gimple_build_assign (callfn,
5067 fold_build1 (NOP_EXPR, TREE_TYPE (callfn), ret));
5068 callfn = make_ssa_name (callfn, g2);
5069 gimple_assign_set_lhs (g2, callfn);
5070 gsi_insert_before (gsi, g2, GSI_SAME_STMT);
5071
5072 /* ??? This is a hack to preserve the NOTHROW bit on the call,
5073 which we would have derived from the decl. Failure to save
5074 this bit means we might have to split the basic block. */
5075 if (gimple_call_nothrow_p (stmt))
5076 gimple_call_set_nothrow (stmt, true);
5077
5078 gimple_call_set_fn (stmt, callfn);
5079
5080 /* Discarding OBJ_TYPE_REF above may produce incompatible LHS and RHS
5081 for a call statement. Fix it. */
5082 {
5083 tree lhs = gimple_call_lhs (stmt);
5084 tree rettype = TREE_TYPE (gimple_call_fntype (stmt));
5085 if (lhs
5086 && !useless_type_conversion_p (TREE_TYPE (lhs), rettype))
5087 {
5088 tree temp;
5089
5090 temp = create_tmp_reg (rettype);
5091 gimple_call_set_lhs (stmt, temp);
5092
5093 g2 = gimple_build_assign (lhs,
5094 fold_build1 (VIEW_CONVERT_EXPR,
5095 TREE_TYPE (lhs), temp));
5096 gsi_insert_after (gsi, g2, GSI_SAME_STMT);
5097 }
5098 }
5099
5100 update_stmt (stmt);
5101 cgraph_edge *e = cgraph_node::get (current_function_decl)->get_edge (stmt);
5102 if (e && e->indirect_info)
5103 e->indirect_info->polymorphic = false;
5104
5105 return true;
5106 }
5107
5108 /* Helper function for ipa_tm_transform_calls*. Given a call
5109 statement in GSI which resides inside transaction REGION, redirect
5110 the call to either its wrapper function, or its clone. */
5111
5112 static void
5113 ipa_tm_transform_calls_redirect (struct cgraph_node *node,
5114 struct tm_region *region,
5115 gimple_stmt_iterator *gsi,
5116 bool *need_ssa_rename_p)
5117 {
5118 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
5119 struct cgraph_node *new_node;
5120 struct cgraph_edge *e = node->get_edge (stmt);
5121 tree fndecl = gimple_call_fndecl (stmt);
5122
5123 /* For indirect calls, pass the address through the runtime. */
5124 if (fndecl == NULL)
5125 {
5126 *need_ssa_rename_p |=
5127 ipa_tm_insert_gettmclone_call (node, region, gsi, stmt);
5128 return;
5129 }
5130
5131 /* Handle some TM builtins. Ordinarily these aren't actually generated
5132 at this point, but handling these functions when written in by the
5133 user makes it easier to build unit tests. */
5134 if (flags_from_decl_or_type (fndecl) & ECF_TM_BUILTIN)
5135 return;
5136
5137 /* Fixup recursive calls inside clones. */
5138 /* ??? Why did cgraph_copy_node_for_versioning update the call edges
5139 for recursion but not update the call statements themselves? */
5140 if (e->caller == e->callee && decl_is_tm_clone (current_function_decl))
5141 {
5142 gimple_call_set_fndecl (stmt, current_function_decl);
5143 return;
5144 }
5145
5146 /* If there is a replacement, use it. */
5147 fndecl = find_tm_replacement_function (fndecl);
5148 if (fndecl)
5149 {
5150 new_node = cgraph_node::get_create (fndecl);
5151
5152 /* ??? Mark all transaction_wrap functions tm_may_enter_irr.
5153
5154 We can't do this earlier in record_tm_replacement because
5155 cgraph_remove_unreachable_nodes is called before we inject
5156 references to the node. Further, we can't do this in some
5157 nice central place in ipa_tm_execute because we don't have
5158 the exact list of wrapper functions that would be used.
5159 Marking more wrappers than necessary results in the creation
5160 of unnecessary cgraph_nodes, which can cause some of the
5161 other IPA passes to crash.
5162
5163 We do need to mark these nodes so that we get the proper
5164 result in expand_call_tm. */
5165 /* ??? This seems broken. How is it that we're marking the
5166 CALLEE as may_enter_irr? Surely we should be marking the
5167 CALLER. Also note that find_tm_replacement_function also
5168 contains mappings into the TM runtime, e.g. memcpy. These
5169 we know won't go irrevocable. */
5170 new_node->local.tm_may_enter_irr = 1;
5171 }
5172 else
5173 {
5174 struct tm_ipa_cg_data *d;
5175 struct cgraph_node *tnode = e->callee;
5176
5177 d = get_cg_data (&tnode, true);
5178 new_node = d->clone;
5179
5180 /* As we've already skipped pure calls and appropriate builtins,
5181 and we've already marked irrevocable blocks, if we can't come
5182 up with a static replacement, then ask the runtime. */
5183 if (new_node == NULL)
5184 {
5185 *need_ssa_rename_p |=
5186 ipa_tm_insert_gettmclone_call (node, region, gsi, stmt);
5187 return;
5188 }
5189
5190 fndecl = new_node->decl;
5191 }
5192
5193 e->redirect_callee (new_node);
5194 gimple_call_set_fndecl (stmt, fndecl);
5195 }
5196
5197 /* Helper function for ipa_tm_transform_calls. For a given BB,
5198 install calls to tm_irrevocable when IRR_BLOCKS are reached,
5199 redirect other calls to the generated transactional clone. */
5200
5201 static bool
5202 ipa_tm_transform_calls_1 (struct cgraph_node *node, struct tm_region *region,
5203 basic_block bb, bitmap irr_blocks)
5204 {
5205 gimple_stmt_iterator gsi;
5206 bool need_ssa_rename = false;
5207
5208 if (irr_blocks && bitmap_bit_p (irr_blocks, bb->index))
5209 {
5210 ipa_tm_insert_irr_call (node, region, bb);
5211 return true;
5212 }
5213
5214 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5215 {
5216 gimple stmt = gsi_stmt (gsi);
5217
5218 if (!is_gimple_call (stmt))
5219 continue;
5220 if (is_tm_pure_call (stmt))
5221 continue;
5222
5223 /* Redirect edges to the appropriate replacement or clone. */
5224 ipa_tm_transform_calls_redirect (node, region, &gsi, &need_ssa_rename);
5225 }
5226
5227 return need_ssa_rename;
5228 }
5229
5230 /* Walk the CFG for REGION, beginning at BB. Install calls to
5231 tm_irrevocable when IRR_BLOCKS are reached, redirect other calls to
5232 the generated transactional clone. */
5233
5234 static bool
5235 ipa_tm_transform_calls (struct cgraph_node *node, struct tm_region *region,
5236 basic_block bb, bitmap irr_blocks)
5237 {
5238 bool need_ssa_rename = false;
5239 edge e;
5240 edge_iterator ei;
5241 auto_vec<basic_block> queue;
5242 bitmap visited_blocks = BITMAP_ALLOC (NULL);
5243
5244 queue.safe_push (bb);
5245 do
5246 {
5247 bb = queue.pop ();
5248
5249 need_ssa_rename |=
5250 ipa_tm_transform_calls_1 (node, region, bb, irr_blocks);
5251
5252 if (irr_blocks && bitmap_bit_p (irr_blocks, bb->index))
5253 continue;
5254
5255 if (region && bitmap_bit_p (region->exit_blocks, bb->index))
5256 continue;
5257
5258 FOR_EACH_EDGE (e, ei, bb->succs)
5259 if (!bitmap_bit_p (visited_blocks, e->dest->index))
5260 {
5261 bitmap_set_bit (visited_blocks, e->dest->index);
5262 queue.safe_push (e->dest);
5263 }
5264 }
5265 while (!queue.is_empty ());
5266
5267 BITMAP_FREE (visited_blocks);
5268
5269 return need_ssa_rename;
5270 }
5271
5272 /* Transform the calls within the TM regions within NODE. */
5273
5274 static void
5275 ipa_tm_transform_transaction (struct cgraph_node *node)
5276 {
5277 struct tm_ipa_cg_data *d;
5278 struct tm_region *region;
5279 bool need_ssa_rename = false;
5280
5281 d = get_cg_data (&node, true);
5282
5283 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
5284 calculate_dominance_info (CDI_DOMINATORS);
5285
5286 for (region = d->all_tm_regions; region; region = region->next)
5287 {
5288 /* If we're sure to go irrevocable, don't transform anything. */
5289 if (d->irrevocable_blocks_normal
5290 && bitmap_bit_p (d->irrevocable_blocks_normal,
5291 region->entry_block->index))
5292 {
5293 transaction_subcode_ior (region, GTMA_DOES_GO_IRREVOCABLE
5294 | GTMA_MAY_ENTER_IRREVOCABLE
5295 | GTMA_HAS_NO_INSTRUMENTATION);
5296 continue;
5297 }
5298
5299 need_ssa_rename |=
5300 ipa_tm_transform_calls (node, region, region->entry_block,
5301 d->irrevocable_blocks_normal);
5302 }
5303
5304 if (need_ssa_rename)
5305 update_ssa (TODO_update_ssa_only_virtuals);
5306
5307 pop_cfun ();
5308 }
5309
5310 /* Transform the calls within the transactional clone of NODE. */
5311
5312 static void
5313 ipa_tm_transform_clone (struct cgraph_node *node)
5314 {
5315 struct tm_ipa_cg_data *d;
5316 bool need_ssa_rename;
5317
5318 d = get_cg_data (&node, true);
5319
5320 /* If this function makes no calls and has no irrevocable blocks,
5321 then there's nothing to do. */
5322 /* ??? Remove non-aborting top-level transactions. */
5323 if (!node->callees && !node->indirect_calls && !d->irrevocable_blocks_clone)
5324 return;
5325
5326 push_cfun (DECL_STRUCT_FUNCTION (d->clone->decl));
5327 calculate_dominance_info (CDI_DOMINATORS);
5328
5329 need_ssa_rename =
5330 ipa_tm_transform_calls (d->clone, NULL,
5331 single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
5332 d->irrevocable_blocks_clone);
5333
5334 if (need_ssa_rename)
5335 update_ssa (TODO_update_ssa_only_virtuals);
5336
5337 pop_cfun ();
5338 }
5339
5340 /* Main entry point for the transactional memory IPA pass. */
5341
5342 static unsigned int
5343 ipa_tm_execute (void)
5344 {
5345 cgraph_node_queue tm_callees = cgraph_node_queue ();
5346 /* List of functions that will go irrevocable. */
5347 cgraph_node_queue irr_worklist = cgraph_node_queue ();
5348
5349 struct cgraph_node *node;
5350 struct tm_ipa_cg_data *d;
5351 enum availability a;
5352 unsigned int i;
5353
5354 #ifdef ENABLE_CHECKING
5355 cgraph_node::verify_cgraph_nodes ();
5356 #endif
5357
5358 bitmap_obstack_initialize (&tm_obstack);
5359 initialize_original_copy_tables ();
5360
5361 /* For all local functions marked tm_callable, queue them. */
5362 FOR_EACH_DEFINED_FUNCTION (node)
5363 if (is_tm_callable (node->decl)
5364 && node->get_availability () >= AVAIL_INTERPOSABLE)
5365 {
5366 d = get_cg_data (&node, true);
5367 maybe_push_queue (node, &tm_callees, &d->in_callee_queue);
5368 }
5369
5370 /* For all local reachable functions... */
5371 FOR_EACH_DEFINED_FUNCTION (node)
5372 if (node->lowered
5373 && node->get_availability () >= AVAIL_INTERPOSABLE)
5374 {
5375 /* ... marked tm_pure, record that fact for the runtime by
5376 indicating that the pure function is its own tm_callable.
5377 No need to do this if the function's address can't be taken. */
5378 if (is_tm_pure (node->decl))
5379 {
5380 if (!node->local.local)
5381 record_tm_clone_pair (node->decl, node->decl);
5382 continue;
5383 }
5384
5385 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
5386 calculate_dominance_info (CDI_DOMINATORS);
5387
5388 tm_region_init (NULL);
5389 if (all_tm_regions)
5390 {
5391 d = get_cg_data (&node, true);
5392
5393 /* Scan for calls that are in each transaction, and
5394 generate the uninstrumented code path. */
5395 ipa_tm_scan_calls_transaction (d, &tm_callees);
5396
5397 /* Put it in the worklist so we can scan the function
5398 later (ipa_tm_scan_irr_function) and mark the
5399 irrevocable blocks. */
5400 maybe_push_queue (node, &irr_worklist, &d->in_worklist);
5401 d->want_irr_scan_normal = true;
5402 }
5403
5404 pop_cfun ();
5405 }
5406
5407 /* For every local function on the callee list, scan as if we will be
5408 creating a transactional clone, queueing all new functions we find
5409 along the way. */
5410 for (i = 0; i < tm_callees.length (); ++i)
5411 {
5412 node = tm_callees[i];
5413 a = node->get_availability ();
5414 d = get_cg_data (&node, true);
5415
5416 /* Put it in the worklist so we can scan the function later
5417 (ipa_tm_scan_irr_function) and mark the irrevocable
5418 blocks. */
5419 maybe_push_queue (node, &irr_worklist, &d->in_worklist);
5420
5421 /* Some callees cannot be arbitrarily cloned. These will always be
5422 irrevocable. Mark these now, so that we need not scan them. */
5423 if (is_tm_irrevocable (node->decl))
5424 ipa_tm_note_irrevocable (node, &irr_worklist);
5425 else if (a <= AVAIL_NOT_AVAILABLE
5426 && !is_tm_safe_or_pure (node->decl))
5427 ipa_tm_note_irrevocable (node, &irr_worklist);
5428 else if (a >= AVAIL_INTERPOSABLE)
5429 {
5430 if (!tree_versionable_function_p (node->decl))
5431 ipa_tm_note_irrevocable (node, &irr_worklist);
5432 else if (!d->is_irrevocable)
5433 {
5434 /* If this is an alias, make sure its base is queued as well.
5435 we need not scan the callees now, as the base will do. */
5436 if (node->alias)
5437 {
5438 node = cgraph_node::get (node->thunk.alias);
5439 d = get_cg_data (&node, true);
5440 maybe_push_queue (node, &tm_callees, &d->in_callee_queue);
5441 continue;
5442 }
5443
5444 /* Add all nodes called by this function into
5445 tm_callees as well. */
5446 ipa_tm_scan_calls_clone (node, &tm_callees);
5447 }
5448 }
5449 }
5450
5451 /* Iterate scans until no more work to be done. Prefer not to use
5452 vec::pop because the worklist tends to follow a breadth-first
5453 search of the callgraph, which should allow convergance with a
5454 minimum number of scans. But we also don't want the worklist
5455 array to grow without bound, so we shift the array up periodically. */
5456 for (i = 0; i < irr_worklist.length (); ++i)
5457 {
5458 if (i > 256 && i == irr_worklist.length () / 8)
5459 {
5460 irr_worklist.block_remove (0, i);
5461 i = 0;
5462 }
5463
5464 node = irr_worklist[i];
5465 d = get_cg_data (&node, true);
5466 d->in_worklist = false;
5467
5468 if (d->want_irr_scan_normal)
5469 {
5470 d->want_irr_scan_normal = false;
5471 ipa_tm_scan_irr_function (node, false);
5472 }
5473 if (d->in_callee_queue && ipa_tm_scan_irr_function (node, true))
5474 ipa_tm_note_irrevocable (node, &irr_worklist);
5475 }
5476
5477 /* For every function on the callee list, collect the tm_may_enter_irr
5478 bit on the node. */
5479 irr_worklist.truncate (0);
5480 for (i = 0; i < tm_callees.length (); ++i)
5481 {
5482 node = tm_callees[i];
5483 if (ipa_tm_mayenterirr_function (node))
5484 {
5485 d = get_cg_data (&node, true);
5486 gcc_assert (d->in_worklist == false);
5487 maybe_push_queue (node, &irr_worklist, &d->in_worklist);
5488 }
5489 }
5490
5491 /* Propagate the tm_may_enter_irr bit to callers until stable. */
5492 for (i = 0; i < irr_worklist.length (); ++i)
5493 {
5494 struct cgraph_node *caller;
5495 struct cgraph_edge *e;
5496 struct ipa_ref *ref;
5497
5498 if (i > 256 && i == irr_worklist.length () / 8)
5499 {
5500 irr_worklist.block_remove (0, i);
5501 i = 0;
5502 }
5503
5504 node = irr_worklist[i];
5505 d = get_cg_data (&node, true);
5506 d->in_worklist = false;
5507 node->local.tm_may_enter_irr = true;
5508
5509 /* Propagate back to normal callers. */
5510 for (e = node->callers; e ; e = e->next_caller)
5511 {
5512 caller = e->caller;
5513 if (!is_tm_safe_or_pure (caller->decl)
5514 && !caller->local.tm_may_enter_irr)
5515 {
5516 d = get_cg_data (&caller, true);
5517 maybe_push_queue (caller, &irr_worklist, &d->in_worklist);
5518 }
5519 }
5520
5521 /* Propagate back to referring aliases as well. */
5522 FOR_EACH_ALIAS (node, ref)
5523 {
5524 caller = dyn_cast<cgraph_node *> (ref->referring);
5525 if (!caller->local.tm_may_enter_irr)
5526 {
5527 /* ?? Do not traverse aliases here. */
5528 d = get_cg_data (&caller, false);
5529 maybe_push_queue (caller, &irr_worklist, &d->in_worklist);
5530 }
5531 }
5532 }
5533
5534 /* Now validate all tm_safe functions, and all atomic regions in
5535 other functions. */
5536 FOR_EACH_DEFINED_FUNCTION (node)
5537 if (node->lowered
5538 && node->get_availability () >= AVAIL_INTERPOSABLE)
5539 {
5540 d = get_cg_data (&node, true);
5541 if (is_tm_safe (node->decl))
5542 ipa_tm_diagnose_tm_safe (node);
5543 else if (d->all_tm_regions)
5544 ipa_tm_diagnose_transaction (node, d->all_tm_regions);
5545 }
5546
5547 /* Create clones. Do those that are not irrevocable and have a
5548 positive call count. Do those publicly visible functions that
5549 the user directed us to clone. */
5550 for (i = 0; i < tm_callees.length (); ++i)
5551 {
5552 bool doit = false;
5553
5554 node = tm_callees[i];
5555 if (node->cpp_implicit_alias)
5556 continue;
5557
5558 a = node->get_availability ();
5559 d = get_cg_data (&node, true);
5560
5561 if (a <= AVAIL_NOT_AVAILABLE)
5562 doit = is_tm_callable (node->decl);
5563 else if (a <= AVAIL_AVAILABLE && is_tm_callable (node->decl))
5564 doit = true;
5565 else if (!d->is_irrevocable
5566 && d->tm_callers_normal + d->tm_callers_clone > 0)
5567 doit = true;
5568
5569 if (doit)
5570 ipa_tm_create_version (node);
5571 }
5572
5573 /* Redirect calls to the new clones, and insert irrevocable marks. */
5574 for (i = 0; i < tm_callees.length (); ++i)
5575 {
5576 node = tm_callees[i];
5577 if (node->analyzed)
5578 {
5579 d = get_cg_data (&node, true);
5580 if (d->clone)
5581 ipa_tm_transform_clone (node);
5582 }
5583 }
5584 FOR_EACH_DEFINED_FUNCTION (node)
5585 if (node->lowered
5586 && node->get_availability () >= AVAIL_INTERPOSABLE)
5587 {
5588 d = get_cg_data (&node, true);
5589 if (d->all_tm_regions)
5590 ipa_tm_transform_transaction (node);
5591 }
5592
5593 /* Free and clear all data structures. */
5594 tm_callees.release ();
5595 irr_worklist.release ();
5596 bitmap_obstack_release (&tm_obstack);
5597 free_original_copy_tables ();
5598
5599 FOR_EACH_FUNCTION (node)
5600 node->aux = NULL;
5601
5602 #ifdef ENABLE_CHECKING
5603 cgraph_node::verify_cgraph_nodes ();
5604 #endif
5605
5606 return 0;
5607 }
5608
5609 namespace {
5610
5611 const pass_data pass_data_ipa_tm =
5612 {
5613 SIMPLE_IPA_PASS, /* type */
5614 "tmipa", /* name */
5615 OPTGROUP_NONE, /* optinfo_flags */
5616 TV_TRANS_MEM, /* tv_id */
5617 ( PROP_ssa | PROP_cfg ), /* properties_required */
5618 0, /* properties_provided */
5619 0, /* properties_destroyed */
5620 0, /* todo_flags_start */
5621 0, /* todo_flags_finish */
5622 };
5623
5624 class pass_ipa_tm : public simple_ipa_opt_pass
5625 {
5626 public:
5627 pass_ipa_tm (gcc::context *ctxt)
5628 : simple_ipa_opt_pass (pass_data_ipa_tm, ctxt)
5629 {}
5630
5631 /* opt_pass methods: */
5632 virtual bool gate (function *) { return flag_tm; }
5633 virtual unsigned int execute (function *) { return ipa_tm_execute (); }
5634
5635 }; // class pass_ipa_tm
5636
5637 } // anon namespace
5638
5639 simple_ipa_opt_pass *
5640 make_pass_ipa_tm (gcc::context *ctxt)
5641 {
5642 return new pass_ipa_tm (ctxt);
5643 }
5644
5645 #include "gt-trans-mem.h"