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