]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree-parloops.c
Use max_loop_iterations in transform_to_exit_first_loop_alt
[thirdparty/gcc.git] / gcc / tree-parloops.c
1 /* Loop autoparallelization.
2 Copyright (C) 2006-2015 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <pop@cri.ensmp.fr>
4 Zdenek Dvorak <dvorakz@suse.cz> and Razya Ladelsky <razya@il.ibm.com>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "alias.h"
26 #include "symtab.h"
27 #include "options.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "predict.h"
31 #include "tm.h"
32 #include "hard-reg-set.h"
33 #include "function.h"
34 #include "dominance.h"
35 #include "cfg.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-expr.h"
40 #include "gimple.h"
41 #include "gimplify.h"
42 #include "gimple-iterator.h"
43 #include "gimplify-me.h"
44 #include "gimple-walk.h"
45 #include "stor-layout.h"
46 #include "tree-nested.h"
47 #include "gimple-ssa.h"
48 #include "tree-cfg.h"
49 #include "tree-phinodes.h"
50 #include "ssa-iterators.h"
51 #include "stringpool.h"
52 #include "tree-ssanames.h"
53 #include "tree-ssa-loop-ivopts.h"
54 #include "tree-ssa-loop-manip.h"
55 #include "tree-ssa-loop-niter.h"
56 #include "tree-ssa-loop.h"
57 #include "tree-into-ssa.h"
58 #include "cfgloop.h"
59 #include "tree-data-ref.h"
60 #include "tree-scalar-evolution.h"
61 #include "gimple-pretty-print.h"
62 #include "tree-pass.h"
63 #include "langhooks.h"
64 #include "tree-vectorizer.h"
65 #include "tree-hasher.h"
66 #include "tree-parloops.h"
67 #include "omp-low.h"
68 #include "tree-nested.h"
69 #include "cgraph.h"
70 #include "tree-ssa.h"
71
72 /* This pass tries to distribute iterations of loops into several threads.
73 The implementation is straightforward -- for each loop we test whether its
74 iterations are independent, and if it is the case (and some additional
75 conditions regarding profitability and correctness are satisfied), we
76 add GIMPLE_OMP_PARALLEL and GIMPLE_OMP_FOR codes and let omp expansion
77 machinery do its job.
78
79 The most of the complexity is in bringing the code into shape expected
80 by the omp expanders:
81 -- for GIMPLE_OMP_FOR, ensuring that the loop has only one induction
82 variable and that the exit test is at the start of the loop body
83 -- for GIMPLE_OMP_PARALLEL, replacing the references to local addressable
84 variables by accesses through pointers, and breaking up ssa chains
85 by storing the values incoming to the parallelized loop to a structure
86 passed to the new function as an argument (something similar is done
87 in omp gimplification, unfortunately only a small part of the code
88 can be shared).
89
90 TODO:
91 -- if there are several parallelizable loops in a function, it may be
92 possible to generate the threads just once (using synchronization to
93 ensure that cross-loop dependences are obeyed).
94 -- handling of common reduction patterns for outer loops.
95
96 More info can also be found at http://gcc.gnu.org/wiki/AutoParInGCC */
97 /*
98 Reduction handling:
99 currently we use vect_force_simple_reduction() to detect reduction patterns.
100 The code transformation will be introduced by an example.
101
102
103 parloop
104 {
105 int sum=1;
106
107 for (i = 0; i < N; i++)
108 {
109 x[i] = i + 3;
110 sum+=x[i];
111 }
112 }
113
114 gimple-like code:
115 header_bb:
116
117 # sum_29 = PHI <sum_11(5), 1(3)>
118 # i_28 = PHI <i_12(5), 0(3)>
119 D.1795_8 = i_28 + 3;
120 x[i_28] = D.1795_8;
121 sum_11 = D.1795_8 + sum_29;
122 i_12 = i_28 + 1;
123 if (N_6(D) > i_12)
124 goto header_bb;
125
126
127 exit_bb:
128
129 # sum_21 = PHI <sum_11(4)>
130 printf (&"%d"[0], sum_21);
131
132
133 after reduction transformation (only relevant parts):
134
135 parloop
136 {
137
138 ....
139
140
141 # Storing the initial value given by the user. #
142
143 .paral_data_store.32.sum.27 = 1;
144
145 #pragma omp parallel num_threads(4)
146
147 #pragma omp for schedule(static)
148
149 # The neutral element corresponding to the particular
150 reduction's operation, e.g. 0 for PLUS_EXPR,
151 1 for MULT_EXPR, etc. replaces the user's initial value. #
152
153 # sum.27_29 = PHI <sum.27_11, 0>
154
155 sum.27_11 = D.1827_8 + sum.27_29;
156
157 GIMPLE_OMP_CONTINUE
158
159 # Adding this reduction phi is done at create_phi_for_local_result() #
160 # sum.27_56 = PHI <sum.27_11, 0>
161 GIMPLE_OMP_RETURN
162
163 # Creating the atomic operation is done at
164 create_call_for_reduction_1() #
165
166 #pragma omp atomic_load
167 D.1839_59 = *&.paral_data_load.33_51->reduction.23;
168 D.1840_60 = sum.27_56 + D.1839_59;
169 #pragma omp atomic_store (D.1840_60);
170
171 GIMPLE_OMP_RETURN
172
173 # collecting the result after the join of the threads is done at
174 create_loads_for_reductions().
175 The value computed by the threads is loaded from the
176 shared struct. #
177
178
179 .paral_data_load.33_52 = &.paral_data_store.32;
180 sum_37 = .paral_data_load.33_52->sum.27;
181 sum_43 = D.1795_41 + sum_37;
182
183 exit bb:
184 # sum_21 = PHI <sum_43, sum_26>
185 printf (&"%d"[0], sum_21);
186
187 ...
188
189 }
190
191 */
192
193 /* Minimal number of iterations of a loop that should be executed in each
194 thread. */
195 #define MIN_PER_THREAD 100
196
197 /* Element of the hashtable, representing a
198 reduction in the current loop. */
199 struct reduction_info
200 {
201 gimple reduc_stmt; /* reduction statement. */
202 gimple reduc_phi; /* The phi node defining the reduction. */
203 enum tree_code reduction_code;/* code for the reduction operation. */
204 unsigned reduc_version; /* SSA_NAME_VERSION of original reduc_phi
205 result. */
206 gphi *keep_res; /* The PHI_RESULT of this phi is the resulting value
207 of the reduction variable when existing the loop. */
208 tree initial_value; /* The initial value of the reduction var before entering the loop. */
209 tree field; /* the name of the field in the parloop data structure intended for reduction. */
210 tree init; /* reduction initialization value. */
211 gphi *new_phi; /* (helper field) Newly created phi node whose result
212 will be passed to the atomic operation. Represents
213 the local result each thread computed for the reduction
214 operation. */
215 };
216
217 /* Reduction info hashtable helpers. */
218
219 struct reduction_hasher : free_ptr_hash <reduction_info>
220 {
221 static inline hashval_t hash (const reduction_info *);
222 static inline bool equal (const reduction_info *, const reduction_info *);
223 };
224
225 /* Equality and hash functions for hashtab code. */
226
227 inline bool
228 reduction_hasher::equal (const reduction_info *a, const reduction_info *b)
229 {
230 return (a->reduc_phi == b->reduc_phi);
231 }
232
233 inline hashval_t
234 reduction_hasher::hash (const reduction_info *a)
235 {
236 return a->reduc_version;
237 }
238
239 typedef hash_table<reduction_hasher> reduction_info_table_type;
240
241
242 static struct reduction_info *
243 reduction_phi (reduction_info_table_type *reduction_list, gimple phi)
244 {
245 struct reduction_info tmpred, *red;
246
247 if (reduction_list->elements () == 0 || phi == NULL)
248 return NULL;
249
250 tmpred.reduc_phi = phi;
251 tmpred.reduc_version = gimple_uid (phi);
252 red = reduction_list->find (&tmpred);
253
254 return red;
255 }
256
257 /* Element of hashtable of names to copy. */
258
259 struct name_to_copy_elt
260 {
261 unsigned version; /* The version of the name to copy. */
262 tree new_name; /* The new name used in the copy. */
263 tree field; /* The field of the structure used to pass the
264 value. */
265 };
266
267 /* Name copies hashtable helpers. */
268
269 struct name_to_copy_hasher : free_ptr_hash <name_to_copy_elt>
270 {
271 static inline hashval_t hash (const name_to_copy_elt *);
272 static inline bool equal (const name_to_copy_elt *, const name_to_copy_elt *);
273 };
274
275 /* Equality and hash functions for hashtab code. */
276
277 inline bool
278 name_to_copy_hasher::equal (const name_to_copy_elt *a, const name_to_copy_elt *b)
279 {
280 return a->version == b->version;
281 }
282
283 inline hashval_t
284 name_to_copy_hasher::hash (const name_to_copy_elt *a)
285 {
286 return (hashval_t) a->version;
287 }
288
289 typedef hash_table<name_to_copy_hasher> name_to_copy_table_type;
290
291 /* A transformation matrix, which is a self-contained ROWSIZE x COLSIZE
292 matrix. Rather than use floats, we simply keep a single DENOMINATOR that
293 represents the denominator for every element in the matrix. */
294 typedef struct lambda_trans_matrix_s
295 {
296 lambda_matrix matrix;
297 int rowsize;
298 int colsize;
299 int denominator;
300 } *lambda_trans_matrix;
301 #define LTM_MATRIX(T) ((T)->matrix)
302 #define LTM_ROWSIZE(T) ((T)->rowsize)
303 #define LTM_COLSIZE(T) ((T)->colsize)
304 #define LTM_DENOMINATOR(T) ((T)->denominator)
305
306 /* Allocate a new transformation matrix. */
307
308 static lambda_trans_matrix
309 lambda_trans_matrix_new (int colsize, int rowsize,
310 struct obstack * lambda_obstack)
311 {
312 lambda_trans_matrix ret;
313
314 ret = (lambda_trans_matrix)
315 obstack_alloc (lambda_obstack, sizeof (struct lambda_trans_matrix_s));
316 LTM_MATRIX (ret) = lambda_matrix_new (rowsize, colsize, lambda_obstack);
317 LTM_ROWSIZE (ret) = rowsize;
318 LTM_COLSIZE (ret) = colsize;
319 LTM_DENOMINATOR (ret) = 1;
320 return ret;
321 }
322
323 /* Multiply a vector VEC by a matrix MAT.
324 MAT is an M*N matrix, and VEC is a vector with length N. The result
325 is stored in DEST which must be a vector of length M. */
326
327 static void
328 lambda_matrix_vector_mult (lambda_matrix matrix, int m, int n,
329 lambda_vector vec, lambda_vector dest)
330 {
331 int i, j;
332
333 lambda_vector_clear (dest, m);
334 for (i = 0; i < m; i++)
335 for (j = 0; j < n; j++)
336 dest[i] += matrix[i][j] * vec[j];
337 }
338
339 /* Return true if TRANS is a legal transformation matrix that respects
340 the dependence vectors in DISTS and DIRS. The conservative answer
341 is false.
342
343 "Wolfe proves that a unimodular transformation represented by the
344 matrix T is legal when applied to a loop nest with a set of
345 lexicographically non-negative distance vectors RDG if and only if
346 for each vector d in RDG, (T.d >= 0) is lexicographically positive.
347 i.e.: if and only if it transforms the lexicographically positive
348 distance vectors to lexicographically positive vectors. Note that
349 a unimodular matrix must transform the zero vector (and only it) to
350 the zero vector." S.Muchnick. */
351
352 static bool
353 lambda_transform_legal_p (lambda_trans_matrix trans,
354 int nb_loops,
355 vec<ddr_p> dependence_relations)
356 {
357 unsigned int i, j;
358 lambda_vector distres;
359 struct data_dependence_relation *ddr;
360
361 gcc_assert (LTM_COLSIZE (trans) == nb_loops
362 && LTM_ROWSIZE (trans) == nb_loops);
363
364 /* When there are no dependences, the transformation is correct. */
365 if (dependence_relations.length () == 0)
366 return true;
367
368 ddr = dependence_relations[0];
369 if (ddr == NULL)
370 return true;
371
372 /* When there is an unknown relation in the dependence_relations, we
373 know that it is no worth looking at this loop nest: give up. */
374 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
375 return false;
376
377 distres = lambda_vector_new (nb_loops);
378
379 /* For each distance vector in the dependence graph. */
380 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
381 {
382 /* Don't care about relations for which we know that there is no
383 dependence, nor about read-read (aka. output-dependences):
384 these data accesses can happen in any order. */
385 if (DDR_ARE_DEPENDENT (ddr) == chrec_known
386 || (DR_IS_READ (DDR_A (ddr)) && DR_IS_READ (DDR_B (ddr))))
387 continue;
388
389 /* Conservatively answer: "this transformation is not valid". */
390 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
391 return false;
392
393 /* If the dependence could not be captured by a distance vector,
394 conservatively answer that the transform is not valid. */
395 if (DDR_NUM_DIST_VECTS (ddr) == 0)
396 return false;
397
398 /* Compute trans.dist_vect */
399 for (j = 0; j < DDR_NUM_DIST_VECTS (ddr); j++)
400 {
401 lambda_matrix_vector_mult (LTM_MATRIX (trans), nb_loops, nb_loops,
402 DDR_DIST_VECT (ddr, j), distres);
403
404 if (!lambda_vector_lexico_pos (distres, nb_loops))
405 return false;
406 }
407 }
408 return true;
409 }
410
411 /* Data dependency analysis. Returns true if the iterations of LOOP
412 are independent on each other (that is, if we can execute them
413 in parallel). */
414
415 static bool
416 loop_parallel_p (struct loop *loop, struct obstack * parloop_obstack)
417 {
418 vec<ddr_p> dependence_relations;
419 vec<data_reference_p> datarefs;
420 lambda_trans_matrix trans;
421 bool ret = false;
422
423 if (dump_file && (dump_flags & TDF_DETAILS))
424 {
425 fprintf (dump_file, "Considering loop %d\n", loop->num);
426 if (!loop->inner)
427 fprintf (dump_file, "loop is innermost\n");
428 else
429 fprintf (dump_file, "loop NOT innermost\n");
430 }
431
432 /* Check for problems with dependences. If the loop can be reversed,
433 the iterations are independent. */
434 auto_vec<loop_p, 3> loop_nest;
435 datarefs.create (10);
436 dependence_relations.create (100);
437 if (! compute_data_dependences_for_loop (loop, true, &loop_nest, &datarefs,
438 &dependence_relations))
439 {
440 if (dump_file && (dump_flags & TDF_DETAILS))
441 fprintf (dump_file, " FAILED: cannot analyze data dependencies\n");
442 ret = false;
443 goto end;
444 }
445 if (dump_file && (dump_flags & TDF_DETAILS))
446 dump_data_dependence_relations (dump_file, dependence_relations);
447
448 trans = lambda_trans_matrix_new (1, 1, parloop_obstack);
449 LTM_MATRIX (trans)[0][0] = -1;
450
451 if (lambda_transform_legal_p (trans, 1, dependence_relations))
452 {
453 ret = true;
454 if (dump_file && (dump_flags & TDF_DETAILS))
455 fprintf (dump_file, " SUCCESS: may be parallelized\n");
456 }
457 else if (dump_file && (dump_flags & TDF_DETAILS))
458 fprintf (dump_file,
459 " FAILED: data dependencies exist across iterations\n");
460
461 end:
462 free_dependence_relations (dependence_relations);
463 free_data_refs (datarefs);
464
465 return ret;
466 }
467
468 /* Return true when LOOP contains basic blocks marked with the
469 BB_IRREDUCIBLE_LOOP flag. */
470
471 static inline bool
472 loop_has_blocks_with_irreducible_flag (struct loop *loop)
473 {
474 unsigned i;
475 basic_block *bbs = get_loop_body_in_dom_order (loop);
476 bool res = true;
477
478 for (i = 0; i < loop->num_nodes; i++)
479 if (bbs[i]->flags & BB_IRREDUCIBLE_LOOP)
480 goto end;
481
482 res = false;
483 end:
484 free (bbs);
485 return res;
486 }
487
488 /* Assigns the address of OBJ in TYPE to an ssa name, and returns this name.
489 The assignment statement is placed on edge ENTRY. DECL_ADDRESS maps decls
490 to their addresses that can be reused. The address of OBJ is known to
491 be invariant in the whole function. Other needed statements are placed
492 right before GSI. */
493
494 static tree
495 take_address_of (tree obj, tree type, edge entry,
496 int_tree_htab_type *decl_address, gimple_stmt_iterator *gsi)
497 {
498 int uid;
499 tree *var_p, name, addr;
500 gassign *stmt;
501 gimple_seq stmts;
502
503 /* Since the address of OBJ is invariant, the trees may be shared.
504 Avoid rewriting unrelated parts of the code. */
505 obj = unshare_expr (obj);
506 for (var_p = &obj;
507 handled_component_p (*var_p);
508 var_p = &TREE_OPERAND (*var_p, 0))
509 continue;
510
511 /* Canonicalize the access to base on a MEM_REF. */
512 if (DECL_P (*var_p))
513 *var_p = build_simple_mem_ref (build_fold_addr_expr (*var_p));
514
515 /* Assign a canonical SSA name to the address of the base decl used
516 in the address and share it for all accesses and addresses based
517 on it. */
518 uid = DECL_UID (TREE_OPERAND (TREE_OPERAND (*var_p, 0), 0));
519 int_tree_map elt;
520 elt.uid = uid;
521 int_tree_map *slot = decl_address->find_slot (elt, INSERT);
522 if (!slot->to)
523 {
524 if (gsi == NULL)
525 return NULL;
526 addr = TREE_OPERAND (*var_p, 0);
527 const char *obj_name
528 = get_name (TREE_OPERAND (TREE_OPERAND (*var_p, 0), 0));
529 if (obj_name)
530 name = make_temp_ssa_name (TREE_TYPE (addr), NULL, obj_name);
531 else
532 name = make_ssa_name (TREE_TYPE (addr));
533 stmt = gimple_build_assign (name, addr);
534 gsi_insert_on_edge_immediate (entry, stmt);
535
536 slot->uid = uid;
537 slot->to = name;
538 }
539 else
540 name = slot->to;
541
542 /* Express the address in terms of the canonical SSA name. */
543 TREE_OPERAND (*var_p, 0) = name;
544 if (gsi == NULL)
545 return build_fold_addr_expr_with_type (obj, type);
546
547 name = force_gimple_operand (build_addr (obj, current_function_decl),
548 &stmts, true, NULL_TREE);
549 if (!gimple_seq_empty_p (stmts))
550 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
551
552 if (!useless_type_conversion_p (type, TREE_TYPE (name)))
553 {
554 name = force_gimple_operand (fold_convert (type, name), &stmts, true,
555 NULL_TREE);
556 if (!gimple_seq_empty_p (stmts))
557 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
558 }
559
560 return name;
561 }
562
563 /* Callback for htab_traverse. Create the initialization statement
564 for reduction described in SLOT, and place it at the preheader of
565 the loop described in DATA. */
566
567 int
568 initialize_reductions (reduction_info **slot, struct loop *loop)
569 {
570 tree init, c;
571 tree bvar, type, arg;
572 edge e;
573
574 struct reduction_info *const reduc = *slot;
575
576 /* Create initialization in preheader:
577 reduction_variable = initialization value of reduction. */
578
579 /* In the phi node at the header, replace the argument coming
580 from the preheader with the reduction initialization value. */
581
582 /* Create a new variable to initialize the reduction. */
583 type = TREE_TYPE (PHI_RESULT (reduc->reduc_phi));
584 bvar = create_tmp_var (type, "reduction");
585
586 c = build_omp_clause (gimple_location (reduc->reduc_stmt),
587 OMP_CLAUSE_REDUCTION);
588 OMP_CLAUSE_REDUCTION_CODE (c) = reduc->reduction_code;
589 OMP_CLAUSE_DECL (c) = SSA_NAME_VAR (gimple_assign_lhs (reduc->reduc_stmt));
590
591 init = omp_reduction_init (c, TREE_TYPE (bvar));
592 reduc->init = init;
593
594 /* Replace the argument representing the initialization value
595 with the initialization value for the reduction (neutral
596 element for the particular operation, e.g. 0 for PLUS_EXPR,
597 1 for MULT_EXPR, etc).
598 Keep the old value in a new variable "reduction_initial",
599 that will be taken in consideration after the parallel
600 computing is done. */
601
602 e = loop_preheader_edge (loop);
603 arg = PHI_ARG_DEF_FROM_EDGE (reduc->reduc_phi, e);
604 /* Create new variable to hold the initial value. */
605
606 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE
607 (reduc->reduc_phi, loop_preheader_edge (loop)), init);
608 reduc->initial_value = arg;
609 return 1;
610 }
611
612 struct elv_data
613 {
614 struct walk_stmt_info info;
615 edge entry;
616 int_tree_htab_type *decl_address;
617 gimple_stmt_iterator *gsi;
618 bool changed;
619 bool reset;
620 };
621
622 /* Eliminates references to local variables in *TP out of the single
623 entry single exit region starting at DTA->ENTRY.
624 DECL_ADDRESS contains addresses of the references that had their
625 address taken already. If the expression is changed, CHANGED is
626 set to true. Callback for walk_tree. */
627
628 static tree
629 eliminate_local_variables_1 (tree *tp, int *walk_subtrees, void *data)
630 {
631 struct elv_data *const dta = (struct elv_data *) data;
632 tree t = *tp, var, addr, addr_type, type, obj;
633
634 if (DECL_P (t))
635 {
636 *walk_subtrees = 0;
637
638 if (!SSA_VAR_P (t) || DECL_EXTERNAL (t))
639 return NULL_TREE;
640
641 type = TREE_TYPE (t);
642 addr_type = build_pointer_type (type);
643 addr = take_address_of (t, addr_type, dta->entry, dta->decl_address,
644 dta->gsi);
645 if (dta->gsi == NULL && addr == NULL_TREE)
646 {
647 dta->reset = true;
648 return NULL_TREE;
649 }
650
651 *tp = build_simple_mem_ref (addr);
652
653 dta->changed = true;
654 return NULL_TREE;
655 }
656
657 if (TREE_CODE (t) == ADDR_EXPR)
658 {
659 /* ADDR_EXPR may appear in two contexts:
660 -- as a gimple operand, when the address taken is a function invariant
661 -- as gimple rhs, when the resulting address in not a function
662 invariant
663 We do not need to do anything special in the latter case (the base of
664 the memory reference whose address is taken may be replaced in the
665 DECL_P case). The former case is more complicated, as we need to
666 ensure that the new address is still a gimple operand. Thus, it
667 is not sufficient to replace just the base of the memory reference --
668 we need to move the whole computation of the address out of the
669 loop. */
670 if (!is_gimple_val (t))
671 return NULL_TREE;
672
673 *walk_subtrees = 0;
674 obj = TREE_OPERAND (t, 0);
675 var = get_base_address (obj);
676 if (!var || !SSA_VAR_P (var) || DECL_EXTERNAL (var))
677 return NULL_TREE;
678
679 addr_type = TREE_TYPE (t);
680 addr = take_address_of (obj, addr_type, dta->entry, dta->decl_address,
681 dta->gsi);
682 if (dta->gsi == NULL && addr == NULL_TREE)
683 {
684 dta->reset = true;
685 return NULL_TREE;
686 }
687 *tp = addr;
688
689 dta->changed = true;
690 return NULL_TREE;
691 }
692
693 if (!EXPR_P (t))
694 *walk_subtrees = 0;
695
696 return NULL_TREE;
697 }
698
699 /* Moves the references to local variables in STMT at *GSI out of the single
700 entry single exit region starting at ENTRY. DECL_ADDRESS contains
701 addresses of the references that had their address taken
702 already. */
703
704 static void
705 eliminate_local_variables_stmt (edge entry, gimple_stmt_iterator *gsi,
706 int_tree_htab_type *decl_address)
707 {
708 struct elv_data dta;
709 gimple stmt = gsi_stmt (*gsi);
710
711 memset (&dta.info, '\0', sizeof (dta.info));
712 dta.entry = entry;
713 dta.decl_address = decl_address;
714 dta.changed = false;
715 dta.reset = false;
716
717 if (gimple_debug_bind_p (stmt))
718 {
719 dta.gsi = NULL;
720 walk_tree (gimple_debug_bind_get_value_ptr (stmt),
721 eliminate_local_variables_1, &dta.info, NULL);
722 if (dta.reset)
723 {
724 gimple_debug_bind_reset_value (stmt);
725 dta.changed = true;
726 }
727 }
728 else if (gimple_clobber_p (stmt))
729 {
730 stmt = gimple_build_nop ();
731 gsi_replace (gsi, stmt, false);
732 dta.changed = true;
733 }
734 else
735 {
736 dta.gsi = gsi;
737 walk_gimple_op (stmt, eliminate_local_variables_1, &dta.info);
738 }
739
740 if (dta.changed)
741 update_stmt (stmt);
742 }
743
744 /* Eliminates the references to local variables from the single entry
745 single exit region between the ENTRY and EXIT edges.
746
747 This includes:
748 1) Taking address of a local variable -- these are moved out of the
749 region (and temporary variable is created to hold the address if
750 necessary).
751
752 2) Dereferencing a local variable -- these are replaced with indirect
753 references. */
754
755 static void
756 eliminate_local_variables (edge entry, edge exit)
757 {
758 basic_block bb;
759 auto_vec<basic_block, 3> body;
760 unsigned i;
761 gimple_stmt_iterator gsi;
762 bool has_debug_stmt = false;
763 int_tree_htab_type decl_address (10);
764 basic_block entry_bb = entry->src;
765 basic_block exit_bb = exit->dest;
766
767 gather_blocks_in_sese_region (entry_bb, exit_bb, &body);
768
769 FOR_EACH_VEC_ELT (body, i, bb)
770 if (bb != entry_bb && bb != exit_bb)
771 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
772 if (is_gimple_debug (gsi_stmt (gsi)))
773 {
774 if (gimple_debug_bind_p (gsi_stmt (gsi)))
775 has_debug_stmt = true;
776 }
777 else
778 eliminate_local_variables_stmt (entry, &gsi, &decl_address);
779
780 if (has_debug_stmt)
781 FOR_EACH_VEC_ELT (body, i, bb)
782 if (bb != entry_bb && bb != exit_bb)
783 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
784 if (gimple_debug_bind_p (gsi_stmt (gsi)))
785 eliminate_local_variables_stmt (entry, &gsi, &decl_address);
786 }
787
788 /* Returns true if expression EXPR is not defined between ENTRY and
789 EXIT, i.e. if all its operands are defined outside of the region. */
790
791 static bool
792 expr_invariant_in_region_p (edge entry, edge exit, tree expr)
793 {
794 basic_block entry_bb = entry->src;
795 basic_block exit_bb = exit->dest;
796 basic_block def_bb;
797
798 if (is_gimple_min_invariant (expr))
799 return true;
800
801 if (TREE_CODE (expr) == SSA_NAME)
802 {
803 def_bb = gimple_bb (SSA_NAME_DEF_STMT (expr));
804 if (def_bb
805 && dominated_by_p (CDI_DOMINATORS, def_bb, entry_bb)
806 && !dominated_by_p (CDI_DOMINATORS, def_bb, exit_bb))
807 return false;
808
809 return true;
810 }
811
812 return false;
813 }
814
815 /* If COPY_NAME_P is true, creates and returns a duplicate of NAME.
816 The copies are stored to NAME_COPIES, if NAME was already duplicated,
817 its duplicate stored in NAME_COPIES is returned.
818
819 Regardless of COPY_NAME_P, the decl used as a base of the ssa name is also
820 duplicated, storing the copies in DECL_COPIES. */
821
822 static tree
823 separate_decls_in_region_name (tree name, name_to_copy_table_type *name_copies,
824 int_tree_htab_type *decl_copies,
825 bool copy_name_p)
826 {
827 tree copy, var, var_copy;
828 unsigned idx, uid, nuid;
829 struct int_tree_map ielt;
830 struct name_to_copy_elt elt, *nelt;
831 name_to_copy_elt **slot;
832 int_tree_map *dslot;
833
834 if (TREE_CODE (name) != SSA_NAME)
835 return name;
836
837 idx = SSA_NAME_VERSION (name);
838 elt.version = idx;
839 slot = name_copies->find_slot_with_hash (&elt, idx,
840 copy_name_p ? INSERT : NO_INSERT);
841 if (slot && *slot)
842 return (*slot)->new_name;
843
844 if (copy_name_p)
845 {
846 copy = duplicate_ssa_name (name, NULL);
847 nelt = XNEW (struct name_to_copy_elt);
848 nelt->version = idx;
849 nelt->new_name = copy;
850 nelt->field = NULL_TREE;
851 *slot = nelt;
852 }
853 else
854 {
855 gcc_assert (!slot);
856 copy = name;
857 }
858
859 var = SSA_NAME_VAR (name);
860 if (!var)
861 return copy;
862
863 uid = DECL_UID (var);
864 ielt.uid = uid;
865 dslot = decl_copies->find_slot_with_hash (ielt, uid, INSERT);
866 if (!dslot->to)
867 {
868 var_copy = create_tmp_var (TREE_TYPE (var), get_name (var));
869 DECL_GIMPLE_REG_P (var_copy) = DECL_GIMPLE_REG_P (var);
870 dslot->uid = uid;
871 dslot->to = var_copy;
872
873 /* Ensure that when we meet this decl next time, we won't duplicate
874 it again. */
875 nuid = DECL_UID (var_copy);
876 ielt.uid = nuid;
877 dslot = decl_copies->find_slot_with_hash (ielt, nuid, INSERT);
878 gcc_assert (!dslot->to);
879 dslot->uid = nuid;
880 dslot->to = var_copy;
881 }
882 else
883 var_copy = dslot->to;
884
885 replace_ssa_name_symbol (copy, var_copy);
886 return copy;
887 }
888
889 /* Finds the ssa names used in STMT that are defined outside the
890 region between ENTRY and EXIT and replaces such ssa names with
891 their duplicates. The duplicates are stored to NAME_COPIES. Base
892 decls of all ssa names used in STMT (including those defined in
893 LOOP) are replaced with the new temporary variables; the
894 replacement decls are stored in DECL_COPIES. */
895
896 static void
897 separate_decls_in_region_stmt (edge entry, edge exit, gimple stmt,
898 name_to_copy_table_type *name_copies,
899 int_tree_htab_type *decl_copies)
900 {
901 use_operand_p use;
902 def_operand_p def;
903 ssa_op_iter oi;
904 tree name, copy;
905 bool copy_name_p;
906
907 FOR_EACH_PHI_OR_STMT_DEF (def, stmt, oi, SSA_OP_DEF)
908 {
909 name = DEF_FROM_PTR (def);
910 gcc_assert (TREE_CODE (name) == SSA_NAME);
911 copy = separate_decls_in_region_name (name, name_copies, decl_copies,
912 false);
913 gcc_assert (copy == name);
914 }
915
916 FOR_EACH_PHI_OR_STMT_USE (use, stmt, oi, SSA_OP_USE)
917 {
918 name = USE_FROM_PTR (use);
919 if (TREE_CODE (name) != SSA_NAME)
920 continue;
921
922 copy_name_p = expr_invariant_in_region_p (entry, exit, name);
923 copy = separate_decls_in_region_name (name, name_copies, decl_copies,
924 copy_name_p);
925 SET_USE (use, copy);
926 }
927 }
928
929 /* Finds the ssa names used in STMT that are defined outside the
930 region between ENTRY and EXIT and replaces such ssa names with
931 their duplicates. The duplicates are stored to NAME_COPIES. Base
932 decls of all ssa names used in STMT (including those defined in
933 LOOP) are replaced with the new temporary variables; the
934 replacement decls are stored in DECL_COPIES. */
935
936 static bool
937 separate_decls_in_region_debug (gimple stmt,
938 name_to_copy_table_type *name_copies,
939 int_tree_htab_type *decl_copies)
940 {
941 use_operand_p use;
942 ssa_op_iter oi;
943 tree var, name;
944 struct int_tree_map ielt;
945 struct name_to_copy_elt elt;
946 name_to_copy_elt **slot;
947 int_tree_map *dslot;
948
949 if (gimple_debug_bind_p (stmt))
950 var = gimple_debug_bind_get_var (stmt);
951 else if (gimple_debug_source_bind_p (stmt))
952 var = gimple_debug_source_bind_get_var (stmt);
953 else
954 return true;
955 if (TREE_CODE (var) == DEBUG_EXPR_DECL || TREE_CODE (var) == LABEL_DECL)
956 return true;
957 gcc_assert (DECL_P (var) && SSA_VAR_P (var));
958 ielt.uid = DECL_UID (var);
959 dslot = decl_copies->find_slot_with_hash (ielt, ielt.uid, NO_INSERT);
960 if (!dslot)
961 return true;
962 if (gimple_debug_bind_p (stmt))
963 gimple_debug_bind_set_var (stmt, dslot->to);
964 else if (gimple_debug_source_bind_p (stmt))
965 gimple_debug_source_bind_set_var (stmt, dslot->to);
966
967 FOR_EACH_PHI_OR_STMT_USE (use, stmt, oi, SSA_OP_USE)
968 {
969 name = USE_FROM_PTR (use);
970 if (TREE_CODE (name) != SSA_NAME)
971 continue;
972
973 elt.version = SSA_NAME_VERSION (name);
974 slot = name_copies->find_slot_with_hash (&elt, elt.version, NO_INSERT);
975 if (!slot)
976 {
977 gimple_debug_bind_reset_value (stmt);
978 update_stmt (stmt);
979 break;
980 }
981
982 SET_USE (use, (*slot)->new_name);
983 }
984
985 return false;
986 }
987
988 /* Callback for htab_traverse. Adds a field corresponding to the reduction
989 specified in SLOT. The type is passed in DATA. */
990
991 int
992 add_field_for_reduction (reduction_info **slot, tree type)
993 {
994
995 struct reduction_info *const red = *slot;
996 tree var = gimple_assign_lhs (red->reduc_stmt);
997 tree field = build_decl (gimple_location (red->reduc_stmt), FIELD_DECL,
998 SSA_NAME_IDENTIFIER (var), TREE_TYPE (var));
999
1000 insert_field_into_struct (type, field);
1001
1002 red->field = field;
1003
1004 return 1;
1005 }
1006
1007 /* Callback for htab_traverse. Adds a field corresponding to a ssa name
1008 described in SLOT. The type is passed in DATA. */
1009
1010 int
1011 add_field_for_name (name_to_copy_elt **slot, tree type)
1012 {
1013 struct name_to_copy_elt *const elt = *slot;
1014 tree name = ssa_name (elt->version);
1015 tree field = build_decl (UNKNOWN_LOCATION,
1016 FIELD_DECL, SSA_NAME_IDENTIFIER (name),
1017 TREE_TYPE (name));
1018
1019 insert_field_into_struct (type, field);
1020 elt->field = field;
1021
1022 return 1;
1023 }
1024
1025 /* Callback for htab_traverse. A local result is the intermediate result
1026 computed by a single
1027 thread, or the initial value in case no iteration was executed.
1028 This function creates a phi node reflecting these values.
1029 The phi's result will be stored in NEW_PHI field of the
1030 reduction's data structure. */
1031
1032 int
1033 create_phi_for_local_result (reduction_info **slot, struct loop *loop)
1034 {
1035 struct reduction_info *const reduc = *slot;
1036 edge e;
1037 gphi *new_phi;
1038 basic_block store_bb;
1039 tree local_res;
1040 source_location locus;
1041
1042 /* STORE_BB is the block where the phi
1043 should be stored. It is the destination of the loop exit.
1044 (Find the fallthru edge from GIMPLE_OMP_CONTINUE). */
1045 store_bb = FALLTHRU_EDGE (loop->latch)->dest;
1046
1047 /* STORE_BB has two predecessors. One coming from the loop
1048 (the reduction's result is computed at the loop),
1049 and another coming from a block preceding the loop,
1050 when no iterations
1051 are executed (the initial value should be taken). */
1052 if (EDGE_PRED (store_bb, 0) == FALLTHRU_EDGE (loop->latch))
1053 e = EDGE_PRED (store_bb, 1);
1054 else
1055 e = EDGE_PRED (store_bb, 0);
1056 local_res = copy_ssa_name (gimple_assign_lhs (reduc->reduc_stmt));
1057 locus = gimple_location (reduc->reduc_stmt);
1058 new_phi = create_phi_node (local_res, store_bb);
1059 add_phi_arg (new_phi, reduc->init, e, locus);
1060 add_phi_arg (new_phi, gimple_assign_lhs (reduc->reduc_stmt),
1061 FALLTHRU_EDGE (loop->latch), locus);
1062 reduc->new_phi = new_phi;
1063
1064 return 1;
1065 }
1066
1067 struct clsn_data
1068 {
1069 tree store;
1070 tree load;
1071
1072 basic_block store_bb;
1073 basic_block load_bb;
1074 };
1075
1076 /* Callback for htab_traverse. Create an atomic instruction for the
1077 reduction described in SLOT.
1078 DATA annotates the place in memory the atomic operation relates to,
1079 and the basic block it needs to be generated in. */
1080
1081 int
1082 create_call_for_reduction_1 (reduction_info **slot, struct clsn_data *clsn_data)
1083 {
1084 struct reduction_info *const reduc = *slot;
1085 gimple_stmt_iterator gsi;
1086 tree type = TREE_TYPE (PHI_RESULT (reduc->reduc_phi));
1087 tree load_struct;
1088 basic_block bb;
1089 basic_block new_bb;
1090 edge e;
1091 tree t, addr, ref, x;
1092 tree tmp_load, name;
1093 gimple load;
1094
1095 load_struct = build_simple_mem_ref (clsn_data->load);
1096 t = build3 (COMPONENT_REF, type, load_struct, reduc->field, NULL_TREE);
1097
1098 addr = build_addr (t, current_function_decl);
1099
1100 /* Create phi node. */
1101 bb = clsn_data->load_bb;
1102
1103 gsi = gsi_last_bb (bb);
1104 e = split_block (bb, gsi_stmt (gsi));
1105 new_bb = e->dest;
1106
1107 tmp_load = create_tmp_var (TREE_TYPE (TREE_TYPE (addr)));
1108 tmp_load = make_ssa_name (tmp_load);
1109 load = gimple_build_omp_atomic_load (tmp_load, addr);
1110 SSA_NAME_DEF_STMT (tmp_load) = load;
1111 gsi = gsi_start_bb (new_bb);
1112 gsi_insert_after (&gsi, load, GSI_NEW_STMT);
1113
1114 e = split_block (new_bb, load);
1115 new_bb = e->dest;
1116 gsi = gsi_start_bb (new_bb);
1117 ref = tmp_load;
1118 x = fold_build2 (reduc->reduction_code,
1119 TREE_TYPE (PHI_RESULT (reduc->new_phi)), ref,
1120 PHI_RESULT (reduc->new_phi));
1121
1122 name = force_gimple_operand_gsi (&gsi, x, true, NULL_TREE, true,
1123 GSI_CONTINUE_LINKING);
1124
1125 gsi_insert_after (&gsi, gimple_build_omp_atomic_store (name), GSI_NEW_STMT);
1126 return 1;
1127 }
1128
1129 /* Create the atomic operation at the join point of the threads.
1130 REDUCTION_LIST describes the reductions in the LOOP.
1131 LD_ST_DATA describes the shared data structure where
1132 shared data is stored in and loaded from. */
1133 static void
1134 create_call_for_reduction (struct loop *loop,
1135 reduction_info_table_type *reduction_list,
1136 struct clsn_data *ld_st_data)
1137 {
1138 reduction_list->traverse <struct loop *, create_phi_for_local_result> (loop);
1139 /* Find the fallthru edge from GIMPLE_OMP_CONTINUE. */
1140 ld_st_data->load_bb = FALLTHRU_EDGE (loop->latch)->dest;
1141 reduction_list
1142 ->traverse <struct clsn_data *, create_call_for_reduction_1> (ld_st_data);
1143 }
1144
1145 /* Callback for htab_traverse. Loads the final reduction value at the
1146 join point of all threads, and inserts it in the right place. */
1147
1148 int
1149 create_loads_for_reductions (reduction_info **slot, struct clsn_data *clsn_data)
1150 {
1151 struct reduction_info *const red = *slot;
1152 gimple stmt;
1153 gimple_stmt_iterator gsi;
1154 tree type = TREE_TYPE (gimple_assign_lhs (red->reduc_stmt));
1155 tree load_struct;
1156 tree name;
1157 tree x;
1158
1159 gsi = gsi_after_labels (clsn_data->load_bb);
1160 load_struct = build_simple_mem_ref (clsn_data->load);
1161 load_struct = build3 (COMPONENT_REF, type, load_struct, red->field,
1162 NULL_TREE);
1163
1164 x = load_struct;
1165 name = PHI_RESULT (red->keep_res);
1166 stmt = gimple_build_assign (name, x);
1167
1168 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
1169
1170 for (gsi = gsi_start_phis (gimple_bb (red->keep_res));
1171 !gsi_end_p (gsi); gsi_next (&gsi))
1172 if (gsi_stmt (gsi) == red->keep_res)
1173 {
1174 remove_phi_node (&gsi, false);
1175 return 1;
1176 }
1177 gcc_unreachable ();
1178 }
1179
1180 /* Load the reduction result that was stored in LD_ST_DATA.
1181 REDUCTION_LIST describes the list of reductions that the
1182 loads should be generated for. */
1183 static void
1184 create_final_loads_for_reduction (reduction_info_table_type *reduction_list,
1185 struct clsn_data *ld_st_data)
1186 {
1187 gimple_stmt_iterator gsi;
1188 tree t;
1189 gimple stmt;
1190
1191 gsi = gsi_after_labels (ld_st_data->load_bb);
1192 t = build_fold_addr_expr (ld_st_data->store);
1193 stmt = gimple_build_assign (ld_st_data->load, t);
1194
1195 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
1196
1197 reduction_list
1198 ->traverse <struct clsn_data *, create_loads_for_reductions> (ld_st_data);
1199
1200 }
1201
1202 /* Callback for htab_traverse. Store the neutral value for the
1203 particular reduction's operation, e.g. 0 for PLUS_EXPR,
1204 1 for MULT_EXPR, etc. into the reduction field.
1205 The reduction is specified in SLOT. The store information is
1206 passed in DATA. */
1207
1208 int
1209 create_stores_for_reduction (reduction_info **slot, struct clsn_data *clsn_data)
1210 {
1211 struct reduction_info *const red = *slot;
1212 tree t;
1213 gimple stmt;
1214 gimple_stmt_iterator gsi;
1215 tree type = TREE_TYPE (gimple_assign_lhs (red->reduc_stmt));
1216
1217 gsi = gsi_last_bb (clsn_data->store_bb);
1218 t = build3 (COMPONENT_REF, type, clsn_data->store, red->field, NULL_TREE);
1219 stmt = gimple_build_assign (t, red->initial_value);
1220 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
1221
1222 return 1;
1223 }
1224
1225 /* Callback for htab_traverse. Creates loads to a field of LOAD in LOAD_BB and
1226 store to a field of STORE in STORE_BB for the ssa name and its duplicate
1227 specified in SLOT. */
1228
1229 int
1230 create_loads_and_stores_for_name (name_to_copy_elt **slot,
1231 struct clsn_data *clsn_data)
1232 {
1233 struct name_to_copy_elt *const elt = *slot;
1234 tree t;
1235 gimple stmt;
1236 gimple_stmt_iterator gsi;
1237 tree type = TREE_TYPE (elt->new_name);
1238 tree load_struct;
1239
1240 gsi = gsi_last_bb (clsn_data->store_bb);
1241 t = build3 (COMPONENT_REF, type, clsn_data->store, elt->field, NULL_TREE);
1242 stmt = gimple_build_assign (t, ssa_name (elt->version));
1243 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
1244
1245 gsi = gsi_last_bb (clsn_data->load_bb);
1246 load_struct = build_simple_mem_ref (clsn_data->load);
1247 t = build3 (COMPONENT_REF, type, load_struct, elt->field, NULL_TREE);
1248 stmt = gimple_build_assign (elt->new_name, t);
1249 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
1250
1251 return 1;
1252 }
1253
1254 /* Moves all the variables used in LOOP and defined outside of it (including
1255 the initial values of loop phi nodes, and *PER_THREAD if it is a ssa
1256 name) to a structure created for this purpose. The code
1257
1258 while (1)
1259 {
1260 use (a);
1261 use (b);
1262 }
1263
1264 is transformed this way:
1265
1266 bb0:
1267 old.a = a;
1268 old.b = b;
1269
1270 bb1:
1271 a' = new->a;
1272 b' = new->b;
1273 while (1)
1274 {
1275 use (a');
1276 use (b');
1277 }
1278
1279 `old' is stored to *ARG_STRUCT and `new' is stored to NEW_ARG_STRUCT. The
1280 pointer `new' is intentionally not initialized (the loop will be split to a
1281 separate function later, and `new' will be initialized from its arguments).
1282 LD_ST_DATA holds information about the shared data structure used to pass
1283 information among the threads. It is initialized here, and
1284 gen_parallel_loop will pass it to create_call_for_reduction that
1285 needs this information. REDUCTION_LIST describes the reductions
1286 in LOOP. */
1287
1288 static void
1289 separate_decls_in_region (edge entry, edge exit,
1290 reduction_info_table_type *reduction_list,
1291 tree *arg_struct, tree *new_arg_struct,
1292 struct clsn_data *ld_st_data)
1293
1294 {
1295 basic_block bb1 = split_edge (entry);
1296 basic_block bb0 = single_pred (bb1);
1297 name_to_copy_table_type name_copies (10);
1298 int_tree_htab_type decl_copies (10);
1299 unsigned i;
1300 tree type, type_name, nvar;
1301 gimple_stmt_iterator gsi;
1302 struct clsn_data clsn_data;
1303 auto_vec<basic_block, 3> body;
1304 basic_block bb;
1305 basic_block entry_bb = bb1;
1306 basic_block exit_bb = exit->dest;
1307 bool has_debug_stmt = false;
1308
1309 entry = single_succ_edge (entry_bb);
1310 gather_blocks_in_sese_region (entry_bb, exit_bb, &body);
1311
1312 FOR_EACH_VEC_ELT (body, i, bb)
1313 {
1314 if (bb != entry_bb && bb != exit_bb)
1315 {
1316 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1317 separate_decls_in_region_stmt (entry, exit, gsi_stmt (gsi),
1318 &name_copies, &decl_copies);
1319
1320 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1321 {
1322 gimple stmt = gsi_stmt (gsi);
1323
1324 if (is_gimple_debug (stmt))
1325 has_debug_stmt = true;
1326 else
1327 separate_decls_in_region_stmt (entry, exit, stmt,
1328 &name_copies, &decl_copies);
1329 }
1330 }
1331 }
1332
1333 /* Now process debug bind stmts. We must not create decls while
1334 processing debug stmts, so we defer their processing so as to
1335 make sure we will have debug info for as many variables as
1336 possible (all of those that were dealt with in the loop above),
1337 and discard those for which we know there's nothing we can
1338 do. */
1339 if (has_debug_stmt)
1340 FOR_EACH_VEC_ELT (body, i, bb)
1341 if (bb != entry_bb && bb != exit_bb)
1342 {
1343 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
1344 {
1345 gimple stmt = gsi_stmt (gsi);
1346
1347 if (is_gimple_debug (stmt))
1348 {
1349 if (separate_decls_in_region_debug (stmt, &name_copies,
1350 &decl_copies))
1351 {
1352 gsi_remove (&gsi, true);
1353 continue;
1354 }
1355 }
1356
1357 gsi_next (&gsi);
1358 }
1359 }
1360
1361 if (name_copies.elements () == 0 && reduction_list->elements () == 0)
1362 {
1363 /* It may happen that there is nothing to copy (if there are only
1364 loop carried and external variables in the loop). */
1365 *arg_struct = NULL;
1366 *new_arg_struct = NULL;
1367 }
1368 else
1369 {
1370 /* Create the type for the structure to store the ssa names to. */
1371 type = lang_hooks.types.make_type (RECORD_TYPE);
1372 type_name = build_decl (UNKNOWN_LOCATION,
1373 TYPE_DECL, create_tmp_var_name (".paral_data"),
1374 type);
1375 TYPE_NAME (type) = type_name;
1376
1377 name_copies.traverse <tree, add_field_for_name> (type);
1378 if (reduction_list && reduction_list->elements () > 0)
1379 {
1380 /* Create the fields for reductions. */
1381 reduction_list->traverse <tree, add_field_for_reduction> (type);
1382 }
1383 layout_type (type);
1384
1385 /* Create the loads and stores. */
1386 *arg_struct = create_tmp_var (type, ".paral_data_store");
1387 nvar = create_tmp_var (build_pointer_type (type), ".paral_data_load");
1388 *new_arg_struct = make_ssa_name (nvar);
1389
1390 ld_st_data->store = *arg_struct;
1391 ld_st_data->load = *new_arg_struct;
1392 ld_st_data->store_bb = bb0;
1393 ld_st_data->load_bb = bb1;
1394
1395 name_copies
1396 .traverse <struct clsn_data *, create_loads_and_stores_for_name>
1397 (ld_st_data);
1398
1399 /* Load the calculation from memory (after the join of the threads). */
1400
1401 if (reduction_list && reduction_list->elements () > 0)
1402 {
1403 reduction_list
1404 ->traverse <struct clsn_data *, create_stores_for_reduction>
1405 (ld_st_data);
1406 clsn_data.load = make_ssa_name (nvar);
1407 clsn_data.load_bb = exit->dest;
1408 clsn_data.store = ld_st_data->store;
1409 create_final_loads_for_reduction (reduction_list, &clsn_data);
1410 }
1411 }
1412 }
1413
1414 /* Returns true if FN was created to run in parallel. */
1415
1416 bool
1417 parallelized_function_p (tree fndecl)
1418 {
1419 cgraph_node *node = cgraph_node::get (fndecl);
1420 gcc_assert (node != NULL);
1421 return node->parallelized_function;
1422 }
1423
1424 /* Creates and returns an empty function that will receive the body of
1425 a parallelized loop. */
1426
1427 static tree
1428 create_loop_fn (location_t loc)
1429 {
1430 char buf[100];
1431 char *tname;
1432 tree decl, type, name, t;
1433 struct function *act_cfun = cfun;
1434 static unsigned loopfn_num;
1435
1436 loc = LOCATION_LOCUS (loc);
1437 snprintf (buf, 100, "%s.$loopfn", current_function_name ());
1438 ASM_FORMAT_PRIVATE_NAME (tname, buf, loopfn_num++);
1439 clean_symbol_name (tname);
1440 name = get_identifier (tname);
1441 type = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
1442
1443 decl = build_decl (loc, FUNCTION_DECL, name, type);
1444 TREE_STATIC (decl) = 1;
1445 TREE_USED (decl) = 1;
1446 DECL_ARTIFICIAL (decl) = 1;
1447 DECL_IGNORED_P (decl) = 0;
1448 TREE_PUBLIC (decl) = 0;
1449 DECL_UNINLINABLE (decl) = 1;
1450 DECL_EXTERNAL (decl) = 0;
1451 DECL_CONTEXT (decl) = NULL_TREE;
1452 DECL_INITIAL (decl) = make_node (BLOCK);
1453
1454 t = build_decl (loc, RESULT_DECL, NULL_TREE, void_type_node);
1455 DECL_ARTIFICIAL (t) = 1;
1456 DECL_IGNORED_P (t) = 1;
1457 DECL_RESULT (decl) = t;
1458
1459 t = build_decl (loc, PARM_DECL, get_identifier (".paral_data_param"),
1460 ptr_type_node);
1461 DECL_ARTIFICIAL (t) = 1;
1462 DECL_ARG_TYPE (t) = ptr_type_node;
1463 DECL_CONTEXT (t) = decl;
1464 TREE_USED (t) = 1;
1465 DECL_ARGUMENTS (decl) = t;
1466
1467 allocate_struct_function (decl, false);
1468
1469 /* The call to allocate_struct_function clobbers CFUN, so we need to restore
1470 it. */
1471 set_cfun (act_cfun);
1472
1473 return decl;
1474 }
1475
1476 /* Replace uses of NAME by VAL in block BB. */
1477
1478 static void
1479 replace_uses_in_bb_by (tree name, tree val, basic_block bb)
1480 {
1481 gimple use_stmt;
1482 imm_use_iterator imm_iter;
1483
1484 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, name)
1485 {
1486 if (gimple_bb (use_stmt) != bb)
1487 continue;
1488
1489 use_operand_p use_p;
1490 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
1491 SET_USE (use_p, val);
1492 }
1493 }
1494
1495 /* Replace uses of NAME by VAL in blocks BBS. */
1496
1497 static void
1498 replace_uses_in_bbs_by (tree name, tree val, bitmap bbs)
1499 {
1500 gimple use_stmt;
1501 imm_use_iterator imm_iter;
1502
1503 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, name)
1504 {
1505 if (!bitmap_bit_p (bbs, gimple_bb (use_stmt)->index))
1506 continue;
1507
1508 use_operand_p use_p;
1509 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
1510 SET_USE (use_p, val);
1511 }
1512 }
1513
1514 /* Do transformation from:
1515
1516 <bb preheader>:
1517 ...
1518 goto <bb header>
1519
1520 <bb header>:
1521 ivtmp_a = PHI <ivtmp_init (preheader), ivtmp_b (latch)>
1522 sum_a = PHI <sum_init (preheader), sum_b (latch)>
1523 ...
1524 use (ivtmp_a)
1525 ...
1526 sum_b = sum_a + sum_update
1527 ...
1528 if (ivtmp_a < n)
1529 goto <bb latch>;
1530 else
1531 goto <bb exit>;
1532
1533 <bb latch>:
1534 ivtmp_b = ivtmp_a + 1;
1535 goto <bb header>
1536
1537 <bb exit>:
1538 sum_z = PHI <sum_b (cond[1])>
1539
1540 [1] Where <bb cond> is single_pred (bb latch); In the simplest case,
1541 that's <bb header>.
1542
1543 to:
1544
1545 <bb preheader>:
1546 ...
1547 goto <bb newheader>
1548
1549 <bb header>:
1550 ivtmp_a = PHI <ivtmp_c (latch)>
1551 sum_a = PHI <sum_c (latch)>
1552 ...
1553 use (ivtmp_a)
1554 ...
1555 sum_b = sum_a + sum_update
1556 ...
1557 goto <bb latch>;
1558
1559 <bb newheader>:
1560 ivtmp_c = PHI <ivtmp_init (preheader), ivtmp_b (latch)>
1561 sum_c = PHI <sum_init (preheader), sum_b (latch)>
1562 if (ivtmp_c < n + 1)
1563 goto <bb header>;
1564 else
1565 goto <bb exit>;
1566
1567 <bb latch>:
1568 ivtmp_b = ivtmp_a + 1;
1569 goto <bb newheader>
1570
1571 <bb exit>:
1572 sum_z = PHI <sum_c (newheader)>
1573
1574
1575 In unified diff format:
1576
1577 <bb preheader>:
1578 ...
1579 - goto <bb header>
1580 + goto <bb newheader>
1581
1582 <bb header>:
1583 - ivtmp_a = PHI <ivtmp_init (preheader), ivtmp_b (latch)>
1584 - sum_a = PHI <sum_init (preheader), sum_b (latch)>
1585 + ivtmp_a = PHI <ivtmp_c (latch)>
1586 + sum_a = PHI <sum_c (latch)>
1587 ...
1588 use (ivtmp_a)
1589 ...
1590 sum_b = sum_a + sum_update
1591 ...
1592 - if (ivtmp_a < n)
1593 - goto <bb latch>;
1594 + goto <bb latch>;
1595 +
1596 + <bb newheader>:
1597 + ivtmp_c = PHI <ivtmp_init (preheader), ivtmp_b (latch)>
1598 + sum_c = PHI <sum_init (preheader), sum_b (latch)>
1599 + if (ivtmp_c < n + 1)
1600 + goto <bb header>;
1601 else
1602 goto <bb exit>;
1603
1604 <bb latch>:
1605 ivtmp_b = ivtmp_a + 1;
1606 - goto <bb header>
1607 + goto <bb newheader>
1608
1609 <bb exit>:
1610 - sum_z = PHI <sum_b (cond[1])>
1611 + sum_z = PHI <sum_c (newheader)>
1612
1613 Note: the example does not show any virtual phis, but these are handled more
1614 or less as reductions.
1615
1616
1617 Moves the exit condition of LOOP to the beginning of its header.
1618 REDUCTION_LIST describes the reductions in LOOP. BOUND is the new loop
1619 bound. */
1620
1621 static void
1622 transform_to_exit_first_loop_alt (struct loop *loop,
1623 reduction_info_table_type *reduction_list,
1624 tree bound)
1625 {
1626 basic_block header = loop->header;
1627 basic_block latch = loop->latch;
1628 edge exit = single_dom_exit (loop);
1629 basic_block exit_block = exit->dest;
1630 gcond *cond_stmt = as_a <gcond *> (last_stmt (exit->src));
1631 tree control = gimple_cond_lhs (cond_stmt);
1632 edge e;
1633
1634 /* Gather the bbs dominated by the exit block. */
1635 bitmap exit_dominated = BITMAP_ALLOC (NULL);
1636 bitmap_set_bit (exit_dominated, exit_block->index);
1637 vec<basic_block> exit_dominated_vec
1638 = get_dominated_by (CDI_DOMINATORS, exit_block);
1639
1640 int i;
1641 basic_block dom_bb;
1642 FOR_EACH_VEC_ELT (exit_dominated_vec, i, dom_bb)
1643 bitmap_set_bit (exit_dominated, dom_bb->index);
1644
1645 exit_dominated_vec.release ();
1646
1647 /* Create the new_header block. */
1648 basic_block new_header = split_block_before_cond_jump (exit->src);
1649 edge split_edge = single_pred_edge (new_header);
1650
1651 /* Redirect entry edge to new_header. */
1652 edge entry = loop_preheader_edge (loop);
1653 e = redirect_edge_and_branch (entry, new_header);
1654 gcc_assert (e == entry);
1655
1656 /* Redirect post_inc_edge to new_header. */
1657 edge post_inc_edge = single_succ_edge (latch);
1658 e = redirect_edge_and_branch (post_inc_edge, new_header);
1659 gcc_assert (e == post_inc_edge);
1660
1661 /* Redirect post_cond_edge to header. */
1662 edge post_cond_edge = single_pred_edge (latch);
1663 e = redirect_edge_and_branch (post_cond_edge, header);
1664 gcc_assert (e == post_cond_edge);
1665
1666 /* Redirect split_edge to latch. */
1667 e = redirect_edge_and_branch (split_edge, latch);
1668 gcc_assert (e == split_edge);
1669
1670 /* Set the new loop bound. */
1671 gimple_cond_set_rhs (cond_stmt, bound);
1672 update_stmt (cond_stmt);
1673
1674 /* Repair the ssa. */
1675 vec<edge_var_map> *v = redirect_edge_var_map_vector (post_inc_edge);
1676 edge_var_map *vm;
1677 gphi_iterator gsi;
1678 for (gsi = gsi_start_phis (header), i = 0;
1679 !gsi_end_p (gsi) && v->iterate (i, &vm);
1680 gsi_next (&gsi), i++)
1681 {
1682 gphi *phi = gsi.phi ();
1683 tree res_a = PHI_RESULT (phi);
1684
1685 /* Create new phi. */
1686 tree res_c = copy_ssa_name (res_a, phi);
1687 gphi *nphi = create_phi_node (res_c, new_header);
1688
1689 /* Replace ivtmp_a with ivtmp_c in condition 'if (ivtmp_a < n)'. */
1690 replace_uses_in_bb_by (res_a, res_c, new_header);
1691
1692 /* Replace ivtmp/sum_b with ivtmp/sum_c in header phi. */
1693 add_phi_arg (phi, res_c, post_cond_edge, UNKNOWN_LOCATION);
1694
1695 /* Replace sum_b with sum_c in exit phi. Loop-closed ssa does not hold
1696 for virtuals, so we cannot get away with exit_block only. */
1697 tree res_b = redirect_edge_var_map_def (vm);
1698 replace_uses_in_bbs_by (res_b, res_c, exit_dominated);
1699
1700 struct reduction_info *red = reduction_phi (reduction_list, phi);
1701 gcc_assert (virtual_operand_p (res_a)
1702 || res_a == control
1703 || red != NULL);
1704
1705 if (red)
1706 {
1707 /* Register the new reduction phi. */
1708 red->reduc_phi = nphi;
1709 gimple_set_uid (red->reduc_phi, red->reduc_version);
1710 }
1711 }
1712 gcc_assert (gsi_end_p (gsi) && !v->iterate (i, &vm));
1713 BITMAP_FREE (exit_dominated);
1714
1715 /* Set the preheader argument of the new phis to ivtmp/sum_init. */
1716 flush_pending_stmts (entry);
1717
1718 /* Set the latch arguments of the new phis to ivtmp/sum_b. */
1719 flush_pending_stmts (post_inc_edge);
1720
1721 /* Register the reduction exit phis. */
1722 for (gphi_iterator gsi = gsi_start_phis (exit_block);
1723 !gsi_end_p (gsi);
1724 gsi_next (&gsi))
1725 {
1726 gphi *phi = gsi.phi ();
1727 tree res_z = PHI_RESULT (phi);
1728 if (virtual_operand_p (res_z))
1729 continue;
1730
1731 tree res_c = PHI_ARG_DEF_FROM_EDGE (phi, exit);
1732 gimple reduc_phi = SSA_NAME_DEF_STMT (res_c);
1733 struct reduction_info *red = reduction_phi (reduction_list, reduc_phi);
1734 if (red != NULL)
1735 red->keep_res = phi;
1736 }
1737
1738 /* We're going to cancel the loop at the end of gen_parallel_loop, but until
1739 then we're still using some fields, so only bother about fields that are
1740 still used: header and latch.
1741 The loop has a new header bb, so we update it. The latch bb stays the
1742 same. */
1743 loop->header = new_header;
1744
1745 /* Recalculate dominance info. */
1746 free_dominance_info (CDI_DOMINATORS);
1747 calculate_dominance_info (CDI_DOMINATORS);
1748 }
1749
1750 /* Tries to moves the exit condition of LOOP to the beginning of its header
1751 without duplication of the loop body. NIT is the number of iterations of the
1752 loop. REDUCTION_LIST describes the reductions in LOOP. Return true if
1753 transformation is successful. */
1754
1755 static bool
1756 try_transform_to_exit_first_loop_alt (struct loop *loop,
1757 reduction_info_table_type *reduction_list,
1758 tree nit)
1759 {
1760 /* Check whether the latch contains a single statement. */
1761 if (!gimple_seq_nondebug_singleton_p (bb_seq (loop->latch)))
1762 return false;
1763
1764 /* Check whether the latch contains the loop iv increment. */
1765 edge back = single_succ_edge (loop->latch);
1766 edge exit = single_dom_exit (loop);
1767 gcond *cond_stmt = as_a <gcond *> (last_stmt (exit->src));
1768 tree control = gimple_cond_lhs (cond_stmt);
1769 gphi *phi = as_a <gphi *> (SSA_NAME_DEF_STMT (control));
1770 tree inc_res = gimple_phi_arg_def (phi, back->dest_idx);
1771 if (gimple_bb (SSA_NAME_DEF_STMT (inc_res)) != loop->latch)
1772 return false;
1773
1774 /* Check whether there's no code between the loop condition and the latch. */
1775 if (!single_pred_p (loop->latch)
1776 || single_pred (loop->latch) != exit->src)
1777 return false;
1778
1779 tree alt_bound = NULL_TREE;
1780 tree nit_type = TREE_TYPE (nit);
1781
1782 /* Figure out whether nit + 1 overflows. */
1783 if (TREE_CODE (nit) == INTEGER_CST)
1784 {
1785 if (!tree_int_cst_equal (nit, TYPE_MAXVAL (nit_type)))
1786 {
1787 alt_bound = fold_build2_loc (UNKNOWN_LOCATION, PLUS_EXPR, nit_type,
1788 nit, build_one_cst (nit_type));
1789
1790 gcc_assert (TREE_CODE (alt_bound) == INTEGER_CST);
1791 transform_to_exit_first_loop_alt (loop, reduction_list, alt_bound);
1792 return true;
1793 }
1794 else
1795 {
1796 /* Todo: Figure out if we can trigger this, if it's worth to handle
1797 optimally, and if we can handle it optimally. */
1798 return false;
1799 }
1800 }
1801
1802 gcc_assert (TREE_CODE (nit) == SSA_NAME);
1803
1804 /* Variable nit is the loop bound as returned by canonicalize_loop_ivs, for an
1805 iv with base 0 and step 1 that is incremented in the latch, like this:
1806
1807 <bb header>:
1808 # iv_1 = PHI <0 (preheader), iv_2 (latch)>
1809 ...
1810 if (iv_1 < nit)
1811 goto <bb latch>;
1812 else
1813 goto <bb exit>;
1814
1815 <bb latch>:
1816 iv_2 = iv_1 + 1;
1817 goto <bb header>;
1818
1819 The range of iv_1 is [0, nit]. The latch edge is taken for
1820 iv_1 == [0, nit - 1] and the exit edge is taken for iv_1 == nit. So the
1821 number of latch executions is equal to nit.
1822
1823 The function max_loop_iterations gives us the maximum number of latch
1824 executions, so it gives us the maximum value of nit. */
1825 widest_int nit_max;
1826 if (!max_loop_iterations (loop, &nit_max))
1827 return false;
1828
1829 /* Check if nit + 1 overflows. */
1830 widest_int type_max = wi::to_widest (TYPE_MAXVAL (nit_type));
1831 if (!wi::lts_p (nit_max, type_max))
1832 return false;
1833
1834 gimple def = SSA_NAME_DEF_STMT (nit);
1835
1836 /* Try to find nit + 1, in the form of n in an assignment nit = n - 1. */
1837 if (def
1838 && is_gimple_assign (def)
1839 && gimple_assign_rhs_code (def) == PLUS_EXPR)
1840 {
1841 tree op1 = gimple_assign_rhs1 (def);
1842 tree op2 = gimple_assign_rhs2 (def);
1843 if (integer_minus_onep (op1))
1844 alt_bound = op2;
1845 else if (integer_minus_onep (op2))
1846 alt_bound = op1;
1847 }
1848
1849 if (alt_bound == NULL_TREE)
1850 return false;
1851
1852 transform_to_exit_first_loop_alt (loop, reduction_list, alt_bound);
1853 return true;
1854 }
1855
1856 /* Moves the exit condition of LOOP to the beginning of its header. NIT is the
1857 number of iterations of the loop. REDUCTION_LIST describes the reductions in
1858 LOOP. */
1859
1860 static void
1861 transform_to_exit_first_loop (struct loop *loop,
1862 reduction_info_table_type *reduction_list,
1863 tree nit)
1864 {
1865 basic_block *bbs, *nbbs, ex_bb, orig_header;
1866 unsigned n;
1867 bool ok;
1868 edge exit = single_dom_exit (loop), hpred;
1869 tree control, control_name, res, t;
1870 gphi *phi, *nphi;
1871 gassign *stmt;
1872 gcond *cond_stmt, *cond_nit;
1873 tree nit_1;
1874
1875 split_block_after_labels (loop->header);
1876 orig_header = single_succ (loop->header);
1877 hpred = single_succ_edge (loop->header);
1878
1879 cond_stmt = as_a <gcond *> (last_stmt (exit->src));
1880 control = gimple_cond_lhs (cond_stmt);
1881 gcc_assert (gimple_cond_rhs (cond_stmt) == nit);
1882
1883 /* Make sure that we have phi nodes on exit for all loop header phis
1884 (create_parallel_loop requires that). */
1885 for (gphi_iterator gsi = gsi_start_phis (loop->header);
1886 !gsi_end_p (gsi);
1887 gsi_next (&gsi))
1888 {
1889 phi = gsi.phi ();
1890 res = PHI_RESULT (phi);
1891 t = copy_ssa_name (res, phi);
1892 SET_PHI_RESULT (phi, t);
1893 nphi = create_phi_node (res, orig_header);
1894 add_phi_arg (nphi, t, hpred, UNKNOWN_LOCATION);
1895
1896 if (res == control)
1897 {
1898 gimple_cond_set_lhs (cond_stmt, t);
1899 update_stmt (cond_stmt);
1900 control = t;
1901 }
1902 }
1903
1904 bbs = get_loop_body_in_dom_order (loop);
1905
1906 for (n = 0; bbs[n] != exit->src; n++)
1907 continue;
1908 nbbs = XNEWVEC (basic_block, n);
1909 ok = gimple_duplicate_sese_tail (single_succ_edge (loop->header), exit,
1910 bbs + 1, n, nbbs);
1911 gcc_assert (ok);
1912 free (bbs);
1913 ex_bb = nbbs[0];
1914 free (nbbs);
1915
1916 /* Other than reductions, the only gimple reg that should be copied
1917 out of the loop is the control variable. */
1918 exit = single_dom_exit (loop);
1919 control_name = NULL_TREE;
1920 for (gphi_iterator gsi = gsi_start_phis (ex_bb);
1921 !gsi_end_p (gsi); )
1922 {
1923 phi = gsi.phi ();
1924 res = PHI_RESULT (phi);
1925 if (virtual_operand_p (res))
1926 {
1927 gsi_next (&gsi);
1928 continue;
1929 }
1930
1931 /* Check if it is a part of reduction. If it is,
1932 keep the phi at the reduction's keep_res field. The
1933 PHI_RESULT of this phi is the resulting value of the reduction
1934 variable when exiting the loop. */
1935
1936 if (reduction_list->elements () > 0)
1937 {
1938 struct reduction_info *red;
1939
1940 tree val = PHI_ARG_DEF_FROM_EDGE (phi, exit);
1941 red = reduction_phi (reduction_list, SSA_NAME_DEF_STMT (val));
1942 if (red)
1943 {
1944 red->keep_res = phi;
1945 gsi_next (&gsi);
1946 continue;
1947 }
1948 }
1949 gcc_assert (control_name == NULL_TREE
1950 && SSA_NAME_VAR (res) == SSA_NAME_VAR (control));
1951 control_name = res;
1952 remove_phi_node (&gsi, false);
1953 }
1954 gcc_assert (control_name != NULL_TREE);
1955
1956 /* Initialize the control variable to number of iterations
1957 according to the rhs of the exit condition. */
1958 gimple_stmt_iterator gsi = gsi_after_labels (ex_bb);
1959 cond_nit = as_a <gcond *> (last_stmt (exit->src));
1960 nit_1 = gimple_cond_rhs (cond_nit);
1961 nit_1 = force_gimple_operand_gsi (&gsi,
1962 fold_convert (TREE_TYPE (control_name), nit_1),
1963 false, NULL_TREE, false, GSI_SAME_STMT);
1964 stmt = gimple_build_assign (control_name, nit_1);
1965 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
1966 }
1967
1968 /* Create the parallel constructs for LOOP as described in gen_parallel_loop.
1969 LOOP_FN and DATA are the arguments of GIMPLE_OMP_PARALLEL.
1970 NEW_DATA is the variable that should be initialized from the argument
1971 of LOOP_FN. N_THREADS is the requested number of threads. Returns the
1972 basic block containing GIMPLE_OMP_PARALLEL tree. */
1973
1974 static basic_block
1975 create_parallel_loop (struct loop *loop, tree loop_fn, tree data,
1976 tree new_data, unsigned n_threads, location_t loc)
1977 {
1978 gimple_stmt_iterator gsi;
1979 basic_block bb, paral_bb, for_bb, ex_bb;
1980 tree t, param;
1981 gomp_parallel *omp_par_stmt;
1982 gimple omp_return_stmt1, omp_return_stmt2;
1983 gimple phi;
1984 gcond *cond_stmt;
1985 gomp_for *for_stmt;
1986 gomp_continue *omp_cont_stmt;
1987 tree cvar, cvar_init, initvar, cvar_next, cvar_base, type;
1988 edge exit, nexit, guard, end, e;
1989
1990 /* Prepare the GIMPLE_OMP_PARALLEL statement. */
1991 bb = loop_preheader_edge (loop)->src;
1992 paral_bb = single_pred (bb);
1993 gsi = gsi_last_bb (paral_bb);
1994
1995 t = build_omp_clause (loc, OMP_CLAUSE_NUM_THREADS);
1996 OMP_CLAUSE_NUM_THREADS_EXPR (t)
1997 = build_int_cst (integer_type_node, n_threads);
1998 omp_par_stmt = gimple_build_omp_parallel (NULL, t, loop_fn, data);
1999 gimple_set_location (omp_par_stmt, loc);
2000
2001 gsi_insert_after (&gsi, omp_par_stmt, GSI_NEW_STMT);
2002
2003 /* Initialize NEW_DATA. */
2004 if (data)
2005 {
2006 gassign *assign_stmt;
2007
2008 gsi = gsi_after_labels (bb);
2009
2010 param = make_ssa_name (DECL_ARGUMENTS (loop_fn));
2011 assign_stmt = gimple_build_assign (param, build_fold_addr_expr (data));
2012 gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
2013
2014 assign_stmt = gimple_build_assign (new_data,
2015 fold_convert (TREE_TYPE (new_data), param));
2016 gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
2017 }
2018
2019 /* Emit GIMPLE_OMP_RETURN for GIMPLE_OMP_PARALLEL. */
2020 bb = split_loop_exit_edge (single_dom_exit (loop));
2021 gsi = gsi_last_bb (bb);
2022 omp_return_stmt1 = gimple_build_omp_return (false);
2023 gimple_set_location (omp_return_stmt1, loc);
2024 gsi_insert_after (&gsi, omp_return_stmt1, GSI_NEW_STMT);
2025
2026 /* Extract data for GIMPLE_OMP_FOR. */
2027 gcc_assert (loop->header == single_dom_exit (loop)->src);
2028 cond_stmt = as_a <gcond *> (last_stmt (loop->header));
2029
2030 cvar = gimple_cond_lhs (cond_stmt);
2031 cvar_base = SSA_NAME_VAR (cvar);
2032 phi = SSA_NAME_DEF_STMT (cvar);
2033 cvar_init = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
2034 initvar = copy_ssa_name (cvar);
2035 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, loop_preheader_edge (loop)),
2036 initvar);
2037 cvar_next = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
2038
2039 gsi = gsi_last_nondebug_bb (loop->latch);
2040 gcc_assert (gsi_stmt (gsi) == SSA_NAME_DEF_STMT (cvar_next));
2041 gsi_remove (&gsi, true);
2042
2043 /* Prepare cfg. */
2044 for_bb = split_edge (loop_preheader_edge (loop));
2045 ex_bb = split_loop_exit_edge (single_dom_exit (loop));
2046 extract_true_false_edges_from_block (loop->header, &nexit, &exit);
2047 gcc_assert (exit == single_dom_exit (loop));
2048
2049 guard = make_edge (for_bb, ex_bb, 0);
2050 single_succ_edge (loop->latch)->flags = 0;
2051 end = make_edge (loop->latch, ex_bb, EDGE_FALLTHRU);
2052 for (gphi_iterator gpi = gsi_start_phis (ex_bb);
2053 !gsi_end_p (gpi); gsi_next (&gpi))
2054 {
2055 source_location locus;
2056 tree def;
2057 gphi *phi = gpi.phi ();
2058 gphi *stmt;
2059
2060 stmt = as_a <gphi *> (
2061 SSA_NAME_DEF_STMT (PHI_ARG_DEF_FROM_EDGE (phi, exit)));
2062
2063 def = PHI_ARG_DEF_FROM_EDGE (stmt, loop_preheader_edge (loop));
2064 locus = gimple_phi_arg_location_from_edge (stmt,
2065 loop_preheader_edge (loop));
2066 add_phi_arg (phi, def, guard, locus);
2067
2068 def = PHI_ARG_DEF_FROM_EDGE (stmt, loop_latch_edge (loop));
2069 locus = gimple_phi_arg_location_from_edge (stmt, loop_latch_edge (loop));
2070 add_phi_arg (phi, def, end, locus);
2071 }
2072 e = redirect_edge_and_branch (exit, nexit->dest);
2073 PENDING_STMT (e) = NULL;
2074
2075 /* Emit GIMPLE_OMP_FOR. */
2076 gimple_cond_set_lhs (cond_stmt, cvar_base);
2077 type = TREE_TYPE (cvar);
2078 t = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
2079 OMP_CLAUSE_SCHEDULE_KIND (t) = OMP_CLAUSE_SCHEDULE_STATIC;
2080
2081 for_stmt = gimple_build_omp_for (NULL, GF_OMP_FOR_KIND_FOR, t, 1, NULL);
2082 gimple_set_location (for_stmt, loc);
2083 gimple_omp_for_set_index (for_stmt, 0, initvar);
2084 gimple_omp_for_set_initial (for_stmt, 0, cvar_init);
2085 gimple_omp_for_set_final (for_stmt, 0, gimple_cond_rhs (cond_stmt));
2086 gimple_omp_for_set_cond (for_stmt, 0, gimple_cond_code (cond_stmt));
2087 gimple_omp_for_set_incr (for_stmt, 0, build2 (PLUS_EXPR, type,
2088 cvar_base,
2089 build_int_cst (type, 1)));
2090
2091 gsi = gsi_last_bb (for_bb);
2092 gsi_insert_after (&gsi, for_stmt, GSI_NEW_STMT);
2093 SSA_NAME_DEF_STMT (initvar) = for_stmt;
2094
2095 /* Emit GIMPLE_OMP_CONTINUE. */
2096 gsi = gsi_last_bb (loop->latch);
2097 omp_cont_stmt = gimple_build_omp_continue (cvar_next, cvar);
2098 gimple_set_location (omp_cont_stmt, loc);
2099 gsi_insert_after (&gsi, omp_cont_stmt, GSI_NEW_STMT);
2100 SSA_NAME_DEF_STMT (cvar_next) = omp_cont_stmt;
2101
2102 /* Emit GIMPLE_OMP_RETURN for GIMPLE_OMP_FOR. */
2103 gsi = gsi_last_bb (ex_bb);
2104 omp_return_stmt2 = gimple_build_omp_return (true);
2105 gimple_set_location (omp_return_stmt2, loc);
2106 gsi_insert_after (&gsi, omp_return_stmt2, GSI_NEW_STMT);
2107
2108 /* After the above dom info is hosed. Re-compute it. */
2109 free_dominance_info (CDI_DOMINATORS);
2110 calculate_dominance_info (CDI_DOMINATORS);
2111
2112 return paral_bb;
2113 }
2114
2115 /* Generates code to execute the iterations of LOOP in N_THREADS
2116 threads in parallel.
2117
2118 NITER describes number of iterations of LOOP.
2119 REDUCTION_LIST describes the reductions existent in the LOOP. */
2120
2121 static void
2122 gen_parallel_loop (struct loop *loop,
2123 reduction_info_table_type *reduction_list,
2124 unsigned n_threads, struct tree_niter_desc *niter)
2125 {
2126 tree many_iterations_cond, type, nit;
2127 tree arg_struct, new_arg_struct;
2128 gimple_seq stmts;
2129 edge entry, exit;
2130 struct clsn_data clsn_data;
2131 unsigned prob;
2132 location_t loc;
2133 gimple cond_stmt;
2134 unsigned int m_p_thread=2;
2135
2136 /* From
2137
2138 ---------------------------------------------------------------------
2139 loop
2140 {
2141 IV = phi (INIT, IV + STEP)
2142 BODY1;
2143 if (COND)
2144 break;
2145 BODY2;
2146 }
2147 ---------------------------------------------------------------------
2148
2149 with # of iterations NITER (possibly with MAY_BE_ZERO assumption),
2150 we generate the following code:
2151
2152 ---------------------------------------------------------------------
2153
2154 if (MAY_BE_ZERO
2155 || NITER < MIN_PER_THREAD * N_THREADS)
2156 goto original;
2157
2158 BODY1;
2159 store all local loop-invariant variables used in body of the loop to DATA.
2160 GIMPLE_OMP_PARALLEL (OMP_CLAUSE_NUM_THREADS (N_THREADS), LOOPFN, DATA);
2161 load the variables from DATA.
2162 GIMPLE_OMP_FOR (IV = INIT; COND; IV += STEP) (OMP_CLAUSE_SCHEDULE (static))
2163 BODY2;
2164 BODY1;
2165 GIMPLE_OMP_CONTINUE;
2166 GIMPLE_OMP_RETURN -- GIMPLE_OMP_FOR
2167 GIMPLE_OMP_RETURN -- GIMPLE_OMP_PARALLEL
2168 goto end;
2169
2170 original:
2171 loop
2172 {
2173 IV = phi (INIT, IV + STEP)
2174 BODY1;
2175 if (COND)
2176 break;
2177 BODY2;
2178 }
2179
2180 end:
2181
2182 */
2183
2184 /* Create two versions of the loop -- in the old one, we know that the
2185 number of iterations is large enough, and we will transform it into the
2186 loop that will be split to loop_fn, the new one will be used for the
2187 remaining iterations. */
2188
2189 /* We should compute a better number-of-iterations value for outer loops.
2190 That is, if we have
2191
2192 for (i = 0; i < n; ++i)
2193 for (j = 0; j < m; ++j)
2194 ...
2195
2196 we should compute nit = n * m, not nit = n.
2197 Also may_be_zero handling would need to be adjusted. */
2198
2199 type = TREE_TYPE (niter->niter);
2200 nit = force_gimple_operand (unshare_expr (niter->niter), &stmts, true,
2201 NULL_TREE);
2202 if (stmts)
2203 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
2204
2205 if (loop->inner)
2206 m_p_thread=2;
2207 else
2208 m_p_thread=MIN_PER_THREAD;
2209
2210 many_iterations_cond =
2211 fold_build2 (GE_EXPR, boolean_type_node,
2212 nit, build_int_cst (type, m_p_thread * n_threads));
2213
2214 many_iterations_cond
2215 = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
2216 invert_truthvalue (unshare_expr (niter->may_be_zero)),
2217 many_iterations_cond);
2218 many_iterations_cond
2219 = force_gimple_operand (many_iterations_cond, &stmts, false, NULL_TREE);
2220 if (stmts)
2221 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
2222 if (!is_gimple_condexpr (many_iterations_cond))
2223 {
2224 many_iterations_cond
2225 = force_gimple_operand (many_iterations_cond, &stmts,
2226 true, NULL_TREE);
2227 if (stmts)
2228 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
2229 }
2230
2231 initialize_original_copy_tables ();
2232
2233 /* We assume that the loop usually iterates a lot. */
2234 prob = 4 * REG_BR_PROB_BASE / 5;
2235 loop_version (loop, many_iterations_cond, NULL,
2236 prob, prob, REG_BR_PROB_BASE - prob, true);
2237 update_ssa (TODO_update_ssa);
2238 free_original_copy_tables ();
2239
2240 /* Base all the induction variables in LOOP on a single control one. */
2241 canonicalize_loop_ivs (loop, &nit, true);
2242
2243 /* Ensure that the exit condition is the first statement in the loop.
2244 The common case is that latch of the loop is empty (apart from the
2245 increment) and immediately follows the loop exit test. Attempt to move the
2246 entry of the loop directly before the exit check and increase the number of
2247 iterations of the loop by one. */
2248 if (!try_transform_to_exit_first_loop_alt (loop, reduction_list, nit))
2249 {
2250 /* Fall back on the method that handles more cases, but duplicates the
2251 loop body: move the exit condition of LOOP to the beginning of its
2252 header, and duplicate the part of the last iteration that gets disabled
2253 to the exit of the loop. */
2254 transform_to_exit_first_loop (loop, reduction_list, nit);
2255 }
2256
2257 /* Generate initializations for reductions. */
2258 if (reduction_list->elements () > 0)
2259 reduction_list->traverse <struct loop *, initialize_reductions> (loop);
2260
2261 /* Eliminate the references to local variables from the loop. */
2262 gcc_assert (single_exit (loop));
2263 entry = loop_preheader_edge (loop);
2264 exit = single_dom_exit (loop);
2265
2266 eliminate_local_variables (entry, exit);
2267 /* In the old loop, move all variables non-local to the loop to a structure
2268 and back, and create separate decls for the variables used in loop. */
2269 separate_decls_in_region (entry, exit, reduction_list, &arg_struct,
2270 &new_arg_struct, &clsn_data);
2271
2272 /* Create the parallel constructs. */
2273 loc = UNKNOWN_LOCATION;
2274 cond_stmt = last_stmt (loop->header);
2275 if (cond_stmt)
2276 loc = gimple_location (cond_stmt);
2277 create_parallel_loop (loop, create_loop_fn (loc), arg_struct,
2278 new_arg_struct, n_threads, loc);
2279 if (reduction_list->elements () > 0)
2280 create_call_for_reduction (loop, reduction_list, &clsn_data);
2281
2282 scev_reset ();
2283
2284 /* Cancel the loop (it is simpler to do it here rather than to teach the
2285 expander to do it). */
2286 cancel_loop_tree (loop);
2287
2288 /* Free loop bound estimations that could contain references to
2289 removed statements. */
2290 FOR_EACH_LOOP (loop, 0)
2291 free_numbers_of_iterations_estimates_loop (loop);
2292 }
2293
2294 /* Returns true when LOOP contains vector phi nodes. */
2295
2296 static bool
2297 loop_has_vector_phi_nodes (struct loop *loop ATTRIBUTE_UNUSED)
2298 {
2299 unsigned i;
2300 basic_block *bbs = get_loop_body_in_dom_order (loop);
2301 gphi_iterator gsi;
2302 bool res = true;
2303
2304 for (i = 0; i < loop->num_nodes; i++)
2305 for (gsi = gsi_start_phis (bbs[i]); !gsi_end_p (gsi); gsi_next (&gsi))
2306 if (TREE_CODE (TREE_TYPE (PHI_RESULT (gsi.phi ()))) == VECTOR_TYPE)
2307 goto end;
2308
2309 res = false;
2310 end:
2311 free (bbs);
2312 return res;
2313 }
2314
2315 /* Create a reduction_info struct, initialize it with REDUC_STMT
2316 and PHI, insert it to the REDUCTION_LIST. */
2317
2318 static void
2319 build_new_reduction (reduction_info_table_type *reduction_list,
2320 gimple reduc_stmt, gphi *phi)
2321 {
2322 reduction_info **slot;
2323 struct reduction_info *new_reduction;
2324
2325 gcc_assert (reduc_stmt);
2326
2327 if (dump_file && (dump_flags & TDF_DETAILS))
2328 {
2329 fprintf (dump_file,
2330 "Detected reduction. reduction stmt is: \n");
2331 print_gimple_stmt (dump_file, reduc_stmt, 0, 0);
2332 fprintf (dump_file, "\n");
2333 }
2334
2335 new_reduction = XCNEW (struct reduction_info);
2336
2337 new_reduction->reduc_stmt = reduc_stmt;
2338 new_reduction->reduc_phi = phi;
2339 new_reduction->reduc_version = SSA_NAME_VERSION (gimple_phi_result (phi));
2340 new_reduction->reduction_code = gimple_assign_rhs_code (reduc_stmt);
2341 slot = reduction_list->find_slot (new_reduction, INSERT);
2342 *slot = new_reduction;
2343 }
2344
2345 /* Callback for htab_traverse. Sets gimple_uid of reduc_phi stmts. */
2346
2347 int
2348 set_reduc_phi_uids (reduction_info **slot, void *data ATTRIBUTE_UNUSED)
2349 {
2350 struct reduction_info *const red = *slot;
2351 gimple_set_uid (red->reduc_phi, red->reduc_version);
2352 return 1;
2353 }
2354
2355 /* Detect all reductions in the LOOP, insert them into REDUCTION_LIST. */
2356
2357 static void
2358 gather_scalar_reductions (loop_p loop, reduction_info_table_type *reduction_list)
2359 {
2360 gphi_iterator gsi;
2361 loop_vec_info simple_loop_info;
2362
2363 simple_loop_info = vect_analyze_loop_form (loop);
2364
2365 for (gsi = gsi_start_phis (loop->header); !gsi_end_p (gsi); gsi_next (&gsi))
2366 {
2367 gphi *phi = gsi.phi ();
2368 affine_iv iv;
2369 tree res = PHI_RESULT (phi);
2370 bool double_reduc;
2371
2372 if (virtual_operand_p (res))
2373 continue;
2374
2375 if (!simple_iv (loop, loop, res, &iv, true)
2376 && simple_loop_info)
2377 {
2378 gimple reduc_stmt = vect_force_simple_reduction (simple_loop_info,
2379 phi, true,
2380 &double_reduc);
2381 if (reduc_stmt && !double_reduc)
2382 build_new_reduction (reduction_list, reduc_stmt, phi);
2383 }
2384 }
2385 destroy_loop_vec_info (simple_loop_info, true);
2386
2387 /* As gimple_uid is used by the vectorizer in between vect_analyze_loop_form
2388 and destroy_loop_vec_info, we can set gimple_uid of reduc_phi stmts
2389 only now. */
2390 reduction_list->traverse <void *, set_reduc_phi_uids> (NULL);
2391 }
2392
2393 /* Try to initialize NITER for code generation part. */
2394
2395 static bool
2396 try_get_loop_niter (loop_p loop, struct tree_niter_desc *niter)
2397 {
2398 edge exit = single_dom_exit (loop);
2399
2400 gcc_assert (exit);
2401
2402 /* We need to know # of iterations, and there should be no uses of values
2403 defined inside loop outside of it, unless the values are invariants of
2404 the loop. */
2405 if (!number_of_iterations_exit (loop, exit, niter, false))
2406 {
2407 if (dump_file && (dump_flags & TDF_DETAILS))
2408 fprintf (dump_file, " FAILED: number of iterations not known\n");
2409 return false;
2410 }
2411
2412 return true;
2413 }
2414
2415 /* Try to initialize REDUCTION_LIST for code generation part.
2416 REDUCTION_LIST describes the reductions. */
2417
2418 static bool
2419 try_create_reduction_list (loop_p loop,
2420 reduction_info_table_type *reduction_list)
2421 {
2422 edge exit = single_dom_exit (loop);
2423 gphi_iterator gsi;
2424
2425 gcc_assert (exit);
2426
2427 gather_scalar_reductions (loop, reduction_list);
2428
2429
2430 for (gsi = gsi_start_phis (exit->dest); !gsi_end_p (gsi); gsi_next (&gsi))
2431 {
2432 gphi *phi = gsi.phi ();
2433 struct reduction_info *red;
2434 imm_use_iterator imm_iter;
2435 use_operand_p use_p;
2436 gimple reduc_phi;
2437 tree val = PHI_ARG_DEF_FROM_EDGE (phi, exit);
2438
2439 if (!virtual_operand_p (val))
2440 {
2441 if (dump_file && (dump_flags & TDF_DETAILS))
2442 {
2443 fprintf (dump_file, "phi is ");
2444 print_gimple_stmt (dump_file, phi, 0, 0);
2445 fprintf (dump_file, "arg of phi to exit: value ");
2446 print_generic_expr (dump_file, val, 0);
2447 fprintf (dump_file, " used outside loop\n");
2448 fprintf (dump_file,
2449 " checking if it a part of reduction pattern: \n");
2450 }
2451 if (reduction_list->elements () == 0)
2452 {
2453 if (dump_file && (dump_flags & TDF_DETAILS))
2454 fprintf (dump_file,
2455 " FAILED: it is not a part of reduction.\n");
2456 return false;
2457 }
2458 reduc_phi = NULL;
2459 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, val)
2460 {
2461 if (!gimple_debug_bind_p (USE_STMT (use_p))
2462 && flow_bb_inside_loop_p (loop, gimple_bb (USE_STMT (use_p))))
2463 {
2464 reduc_phi = USE_STMT (use_p);
2465 break;
2466 }
2467 }
2468 red = reduction_phi (reduction_list, reduc_phi);
2469 if (red == NULL)
2470 {
2471 if (dump_file && (dump_flags & TDF_DETAILS))
2472 fprintf (dump_file,
2473 " FAILED: it is not a part of reduction.\n");
2474 return false;
2475 }
2476 if (dump_file && (dump_flags & TDF_DETAILS))
2477 {
2478 fprintf (dump_file, "reduction phi is ");
2479 print_gimple_stmt (dump_file, red->reduc_phi, 0, 0);
2480 fprintf (dump_file, "reduction stmt is ");
2481 print_gimple_stmt (dump_file, red->reduc_stmt, 0, 0);
2482 }
2483 }
2484 }
2485
2486 /* The iterations of the loop may communicate only through bivs whose
2487 iteration space can be distributed efficiently. */
2488 for (gsi = gsi_start_phis (loop->header); !gsi_end_p (gsi); gsi_next (&gsi))
2489 {
2490 gphi *phi = gsi.phi ();
2491 tree def = PHI_RESULT (phi);
2492 affine_iv iv;
2493
2494 if (!virtual_operand_p (def) && !simple_iv (loop, loop, def, &iv, true))
2495 {
2496 struct reduction_info *red;
2497
2498 red = reduction_phi (reduction_list, phi);
2499 if (red == NULL)
2500 {
2501 if (dump_file && (dump_flags & TDF_DETAILS))
2502 fprintf (dump_file,
2503 " FAILED: scalar dependency between iterations\n");
2504 return false;
2505 }
2506 }
2507 }
2508
2509
2510 return true;
2511 }
2512
2513 /* Detect parallel loops and generate parallel code using libgomp
2514 primitives. Returns true if some loop was parallelized, false
2515 otherwise. */
2516
2517 static bool
2518 parallelize_loops (void)
2519 {
2520 unsigned n_threads = flag_tree_parallelize_loops;
2521 bool changed = false;
2522 struct loop *loop;
2523 struct tree_niter_desc niter_desc;
2524 struct obstack parloop_obstack;
2525 HOST_WIDE_INT estimated;
2526 source_location loop_loc;
2527
2528 /* Do not parallelize loops in the functions created by parallelization. */
2529 if (parallelized_function_p (cfun->decl))
2530 return false;
2531 if (cfun->has_nonlocal_label)
2532 return false;
2533
2534 gcc_obstack_init (&parloop_obstack);
2535 reduction_info_table_type reduction_list (10);
2536 init_stmt_vec_info_vec ();
2537
2538 FOR_EACH_LOOP (loop, 0)
2539 {
2540 reduction_list.empty ();
2541 if (dump_file && (dump_flags & TDF_DETAILS))
2542 {
2543 fprintf (dump_file, "Trying loop %d as candidate\n",loop->num);
2544 if (loop->inner)
2545 fprintf (dump_file, "loop %d is not innermost\n",loop->num);
2546 else
2547 fprintf (dump_file, "loop %d is innermost\n",loop->num);
2548 }
2549
2550 /* If we use autopar in graphite pass, we use its marked dependency
2551 checking results. */
2552 if (flag_loop_parallelize_all && !loop->can_be_parallel)
2553 {
2554 if (dump_file && (dump_flags & TDF_DETAILS))
2555 fprintf (dump_file, "loop is not parallel according to graphite\n");
2556 continue;
2557 }
2558
2559 if (!single_dom_exit (loop))
2560 {
2561
2562 if (dump_file && (dump_flags & TDF_DETAILS))
2563 fprintf (dump_file, "loop is !single_dom_exit\n");
2564
2565 continue;
2566 }
2567
2568 if (/* And of course, the loop must be parallelizable. */
2569 !can_duplicate_loop_p (loop)
2570 || loop_has_blocks_with_irreducible_flag (loop)
2571 || (loop_preheader_edge (loop)->src->flags & BB_IRREDUCIBLE_LOOP)
2572 /* FIXME: the check for vector phi nodes could be removed. */
2573 || loop_has_vector_phi_nodes (loop))
2574 continue;
2575
2576 estimated = estimated_stmt_executions_int (loop);
2577 if (estimated == -1)
2578 estimated = max_stmt_executions_int (loop);
2579 /* FIXME: Bypass this check as graphite doesn't update the
2580 count and frequency correctly now. */
2581 if (!flag_loop_parallelize_all
2582 && ((estimated != -1
2583 && estimated <= (HOST_WIDE_INT) n_threads * MIN_PER_THREAD)
2584 /* Do not bother with loops in cold areas. */
2585 || optimize_loop_nest_for_size_p (loop)))
2586 continue;
2587
2588 if (!try_get_loop_niter (loop, &niter_desc))
2589 continue;
2590
2591 if (!try_create_reduction_list (loop, &reduction_list))
2592 continue;
2593
2594 if (!flag_loop_parallelize_all
2595 && !loop_parallel_p (loop, &parloop_obstack))
2596 continue;
2597
2598 changed = true;
2599 if (dump_file && (dump_flags & TDF_DETAILS))
2600 {
2601 if (loop->inner)
2602 fprintf (dump_file, "parallelizing outer loop %d\n",loop->header->index);
2603 else
2604 fprintf (dump_file, "parallelizing inner loop %d\n",loop->header->index);
2605 loop_loc = find_loop_location (loop);
2606 if (loop_loc != UNKNOWN_LOCATION)
2607 fprintf (dump_file, "\nloop at %s:%d: ",
2608 LOCATION_FILE (loop_loc), LOCATION_LINE (loop_loc));
2609 }
2610 gen_parallel_loop (loop, &reduction_list,
2611 n_threads, &niter_desc);
2612 }
2613
2614 free_stmt_vec_info_vec ();
2615 obstack_free (&parloop_obstack, NULL);
2616
2617 /* Parallelization will cause new function calls to be inserted through
2618 which local variables will escape. Reset the points-to solution
2619 for ESCAPED. */
2620 if (changed)
2621 pt_solution_reset (&cfun->gimple_df->escaped);
2622
2623 return changed;
2624 }
2625
2626 /* Parallelization. */
2627
2628 namespace {
2629
2630 const pass_data pass_data_parallelize_loops =
2631 {
2632 GIMPLE_PASS, /* type */
2633 "parloops", /* name */
2634 OPTGROUP_LOOP, /* optinfo_flags */
2635 TV_TREE_PARALLELIZE_LOOPS, /* tv_id */
2636 ( PROP_cfg | PROP_ssa ), /* properties_required */
2637 0, /* properties_provided */
2638 0, /* properties_destroyed */
2639 0, /* todo_flags_start */
2640 0, /* todo_flags_finish */
2641 };
2642
2643 class pass_parallelize_loops : public gimple_opt_pass
2644 {
2645 public:
2646 pass_parallelize_loops (gcc::context *ctxt)
2647 : gimple_opt_pass (pass_data_parallelize_loops, ctxt)
2648 {}
2649
2650 /* opt_pass methods: */
2651 virtual bool gate (function *) { return flag_tree_parallelize_loops > 1; }
2652 virtual unsigned int execute (function *);
2653
2654 }; // class pass_parallelize_loops
2655
2656 unsigned
2657 pass_parallelize_loops::execute (function *fun)
2658 {
2659 if (number_of_loops (fun) <= 1)
2660 return 0;
2661
2662 if (parallelize_loops ())
2663 {
2664 fun->curr_properties &= ~(PROP_gimple_eomp);
2665 return TODO_update_ssa;
2666 }
2667
2668 return 0;
2669 }
2670
2671 } // anon namespace
2672
2673 gimple_opt_pass *
2674 make_pass_parallelize_loops (gcc::context *ctxt)
2675 {
2676 return new pass_parallelize_loops (ctxt);
2677 }