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