]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-parloops.c
Revert 'require nonpic target' for fuse-caller-save*.c
[thirdparty/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
AM
25#include "hash-set.h"
26#include "machmode.h"
40e23961
MC
27#include "vec.h"
28#include "double-int.h"
29#include "input.h"
30#include "alias.h"
31#include "symtab.h"
32#include "options.h"
33#include "wide-int.h"
34#include "inchash.h"
35#include "tree.h"
36#include "fold-const.h"
37#include "predict.h"
60393bbc
AM
38#include "tm.h"
39#include "hard-reg-set.h"
40#include "input.h"
41#include "function.h"
42#include "dominance.h"
43#include "cfg.h"
2fb9a547
AM
44#include "basic-block.h"
45#include "tree-ssa-alias.h"
46#include "internal-fn.h"
47#include "gimple-expr.h"
48#include "is-a.h"
18f429e2 49#include "gimple.h"
45b0be94 50#include "gimplify.h"
5be5c238 51#include "gimple-iterator.h"
18f429e2 52#include "gimplify-me.h"
5be5c238 53#include "gimple-walk.h"
d8a2d370
DN
54#include "stor-layout.h"
55#include "tree-nested.h"
442b4905
AM
56#include "gimple-ssa.h"
57#include "tree-cfg.h"
58#include "tree-phinodes.h"
59#include "ssa-iterators.h"
d8a2d370 60#include "stringpool.h"
442b4905 61#include "tree-ssanames.h"
e28030cf
AM
62#include "tree-ssa-loop-ivopts.h"
63#include "tree-ssa-loop-manip.h"
64#include "tree-ssa-loop-niter.h"
442b4905
AM
65#include "tree-ssa-loop.h"
66#include "tree-into-ssa.h"
5f40b3cb 67#include "cfgloop.h"
5f40b3cb 68#include "tree-data-ref.h"
1bd6497c 69#include "tree-scalar-evolution.h"
cf835838 70#include "gimple-pretty-print.h"
5f40b3cb 71#include "tree-pass.h"
5f40b3cb 72#include "langhooks.h"
a509ebb5 73#include "tree-vectorizer.h"
4a8fb1a1 74#include "tree-hasher.h"
c1bf2a39 75#include "tree-parloops.h"
0645c1a2 76#include "omp-low.h"
1fe37220 77#include "tree-nested.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{
4a8fb1a1
LC
228 typedef reduction_info value_type;
229 typedef reduction_info compare_type;
230 static inline hashval_t hash (const value_type *);
231 static inline bool equal (const value_type *, const compare_type *);
232};
233
234/* Equality and hash functions for hashtab code. */
a509ebb5 235
4a8fb1a1
LC
236inline bool
237reduction_hasher::equal (const value_type *a, const compare_type *b)
238{
a509ebb5
RL
239 return (a->reduc_phi == b->reduc_phi);
240}
241
4a8fb1a1
LC
242inline hashval_t
243reduction_hasher::hash (const value_type *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{
4a8fb1a1
LC
280 typedef name_to_copy_elt value_type;
281 typedef name_to_copy_elt compare_type;
282 static inline hashval_t hash (const value_type *);
283 static inline bool equal (const value_type *, const compare_type *);
284};
285
286/* Equality and hash functions for hashtab code. */
5f40b3cb 287
4a8fb1a1
LC
288inline bool
289name_to_copy_hasher::equal (const value_type *a, const compare_type *b)
290{
5f40b3cb
ZD
291 return a->version == b->version;
292}
293
4a8fb1a1
LC
294inline hashval_t
295name_to_copy_hasher::hash (const value_type *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
1114 e = split_block (bb, t);
1115 new_bb = e->dest;
1116
b731b390
JJ
1117 tmp_load = create_tmp_var (TREE_TYPE (TREE_TYPE (addr)));
1118 tmp_load = make_ssa_name (tmp_load);
726a989a 1119 load = gimple_build_omp_atomic_load (tmp_load, addr);
a509ebb5 1120 SSA_NAME_DEF_STMT (tmp_load) = load;
726a989a
RB
1121 gsi = gsi_start_bb (new_bb);
1122 gsi_insert_after (&gsi, load, GSI_NEW_STMT);
a509ebb5
RL
1123
1124 e = split_block (new_bb, load);
1125 new_bb = e->dest;
726a989a 1126 gsi = gsi_start_bb (new_bb);
a509ebb5 1127 ref = tmp_load;
726a989a
RB
1128 x = fold_build2 (reduc->reduction_code,
1129 TREE_TYPE (PHI_RESULT (reduc->new_phi)), ref,
1130 PHI_RESULT (reduc->new_phi));
a509ebb5 1131
726a989a
RB
1132 name = force_gimple_operand_gsi (&gsi, x, true, NULL_TREE, true,
1133 GSI_CONTINUE_LINKING);
a509ebb5 1134
726a989a 1135 gsi_insert_after (&gsi, gimple_build_omp_atomic_store (name), GSI_NEW_STMT);
a509ebb5
RL
1136 return 1;
1137}
1138
b8698a0f
L
1139/* Create the atomic operation at the join point of the threads.
1140 REDUCTION_LIST describes the reductions in the LOOP.
1141 LD_ST_DATA describes the shared data structure where
a509ebb5
RL
1142 shared data is stored in and loaded from. */
1143static void
4a8fb1a1 1144create_call_for_reduction (struct loop *loop,
c203e8a7 1145 reduction_info_table_type *reduction_list,
a509ebb5
RL
1146 struct clsn_data *ld_st_data)
1147{
c203e8a7 1148 reduction_list->traverse <struct loop *, create_phi_for_local_result> (loop);
726a989a 1149 /* Find the fallthru edge from GIMPLE_OMP_CONTINUE. */
a509ebb5 1150 ld_st_data->load_bb = FALLTHRU_EDGE (loop->latch)->dest;
4a8fb1a1 1151 reduction_list
c203e8a7 1152 ->traverse <struct clsn_data *, create_call_for_reduction_1> (ld_st_data);
a509ebb5
RL
1153}
1154
ae0bce62
RL
1155/* Callback for htab_traverse. Loads the final reduction value at the
1156 join point of all threads, and inserts it in the right place. */
a509ebb5 1157
4a8fb1a1
LC
1158int
1159create_loads_for_reductions (reduction_info **slot, struct clsn_data *clsn_data)
a509ebb5 1160{
4a8fb1a1 1161 struct reduction_info *const red = *slot;
726a989a
RB
1162 gimple stmt;
1163 gimple_stmt_iterator gsi;
1164 tree type = TREE_TYPE (gimple_assign_lhs (red->reduc_stmt));
a509ebb5 1165 tree load_struct;
ae0bce62 1166 tree name;
a509ebb5
RL
1167 tree x;
1168
726a989a 1169 gsi = gsi_after_labels (clsn_data->load_bb);
70f34814 1170 load_struct = build_simple_mem_ref (clsn_data->load);
a509ebb5
RL
1171 load_struct = build3 (COMPONENT_REF, type, load_struct, red->field,
1172 NULL_TREE);
a509ebb5 1173
ae0bce62 1174 x = load_struct;
a509ebb5 1175 name = PHI_RESULT (red->keep_res);
726a989a 1176 stmt = gimple_build_assign (name, x);
a509ebb5 1177
726a989a 1178 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
a509ebb5 1179
726a989a
RB
1180 for (gsi = gsi_start_phis (gimple_bb (red->keep_res));
1181 !gsi_end_p (gsi); gsi_next (&gsi))
1182 if (gsi_stmt (gsi) == red->keep_res)
1183 {
1184 remove_phi_node (&gsi, false);
1185 return 1;
1186 }
1187 gcc_unreachable ();
a509ebb5
RL
1188}
1189
b8698a0f 1190/* Load the reduction result that was stored in LD_ST_DATA.
a509ebb5 1191 REDUCTION_LIST describes the list of reductions that the
fa10beec 1192 loads should be generated for. */
a509ebb5 1193static void
c203e8a7 1194create_final_loads_for_reduction (reduction_info_table_type *reduction_list,
a509ebb5
RL
1195 struct clsn_data *ld_st_data)
1196{
726a989a 1197 gimple_stmt_iterator gsi;
a509ebb5 1198 tree t;
726a989a 1199 gimple stmt;
a509ebb5 1200
726a989a 1201 gsi = gsi_after_labels (ld_st_data->load_bb);
a509ebb5 1202 t = build_fold_addr_expr (ld_st_data->store);
726a989a 1203 stmt = gimple_build_assign (ld_st_data->load, t);
a509ebb5 1204
726a989a 1205 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
a509ebb5 1206
4a8fb1a1 1207 reduction_list
c203e8a7 1208 ->traverse <struct clsn_data *, create_loads_for_reductions> (ld_st_data);
a509ebb5
RL
1209
1210}
1211
0eb7e7aa
RL
1212/* Callback for htab_traverse. Store the neutral value for the
1213 particular reduction's operation, e.g. 0 for PLUS_EXPR,
1214 1 for MULT_EXPR, etc. into the reduction field.
b8698a0f
L
1215 The reduction is specified in SLOT. The store information is
1216 passed in DATA. */
0eb7e7aa 1217
4a8fb1a1
LC
1218int
1219create_stores_for_reduction (reduction_info **slot, struct clsn_data *clsn_data)
0eb7e7aa 1220{
4a8fb1a1 1221 struct reduction_info *const red = *slot;
726a989a
RB
1222 tree t;
1223 gimple stmt;
1224 gimple_stmt_iterator gsi;
1225 tree type = TREE_TYPE (gimple_assign_lhs (red->reduc_stmt));
1226
1227 gsi = gsi_last_bb (clsn_data->store_bb);
1228 t = build3 (COMPONENT_REF, type, clsn_data->store, red->field, NULL_TREE);
1229 stmt = gimple_build_assign (t, red->initial_value);
726a989a 1230 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
0eb7e7aa
RL
1231
1232 return 1;
1233}
1234
a509ebb5
RL
1235/* Callback for htab_traverse. Creates loads to a field of LOAD in LOAD_BB and
1236 store to a field of STORE in STORE_BB for the ssa name and its duplicate
1237 specified in SLOT. */
1238
4a8fb1a1
LC
1239int
1240create_loads_and_stores_for_name (name_to_copy_elt **slot,
1241 struct clsn_data *clsn_data)
5f40b3cb 1242{
4a8fb1a1 1243 struct name_to_copy_elt *const elt = *slot;
726a989a
RB
1244 tree t;
1245 gimple stmt;
1246 gimple_stmt_iterator gsi;
5f40b3cb 1247 tree type = TREE_TYPE (elt->new_name);
5f40b3cb
ZD
1248 tree load_struct;
1249
726a989a
RB
1250 gsi = gsi_last_bb (clsn_data->store_bb);
1251 t = build3 (COMPONENT_REF, type, clsn_data->store, elt->field, NULL_TREE);
1252 stmt = gimple_build_assign (t, ssa_name (elt->version));
726a989a 1253 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
5f40b3cb 1254
726a989a 1255 gsi = gsi_last_bb (clsn_data->load_bb);
70f34814 1256 load_struct = build_simple_mem_ref (clsn_data->load);
726a989a
RB
1257 t = build3 (COMPONENT_REF, type, load_struct, elt->field, NULL_TREE);
1258 stmt = gimple_build_assign (elt->new_name, t);
726a989a 1259 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
5f40b3cb
ZD
1260
1261 return 1;
1262}
1263
1264/* Moves all the variables used in LOOP and defined outside of it (including
1265 the initial values of loop phi nodes, and *PER_THREAD if it is a ssa
1266 name) to a structure created for this purpose. The code
b8698a0f 1267
5f40b3cb
ZD
1268 while (1)
1269 {
1270 use (a);
1271 use (b);
1272 }
1273
1274 is transformed this way:
1275
1276 bb0:
1277 old.a = a;
1278 old.b = b;
1279
1280 bb1:
1281 a' = new->a;
1282 b' = new->b;
1283 while (1)
1284 {
1285 use (a');
1286 use (b');
1287 }
1288
1289 `old' is stored to *ARG_STRUCT and `new' is stored to NEW_ARG_STRUCT. The
1290 pointer `new' is intentionally not initialized (the loop will be split to a
1291 separate function later, and `new' will be initialized from its arguments).
a509ebb5 1292 LD_ST_DATA holds information about the shared data structure used to pass
b8698a0f
L
1293 information among the threads. It is initialized here, and
1294 gen_parallel_loop will pass it to create_call_for_reduction that
1295 needs this information. REDUCTION_LIST describes the reductions
a509ebb5 1296 in LOOP. */
5f40b3cb
ZD
1297
1298static void
4a8fb1a1 1299separate_decls_in_region (edge entry, edge exit,
c203e8a7 1300 reduction_info_table_type *reduction_list,
b8698a0f 1301 tree *arg_struct, tree *new_arg_struct,
9f9f72aa 1302 struct clsn_data *ld_st_data)
a509ebb5 1303
5f40b3cb 1304{
9f9f72aa 1305 basic_block bb1 = split_edge (entry);
5f40b3cb 1306 basic_block bb0 = single_pred (bb1);
c203e8a7
TS
1307 name_to_copy_table_type name_copies (10);
1308 int_tree_htab_type decl_copies (10);
5f40b3cb 1309 unsigned i;
726a989a
RB
1310 tree type, type_name, nvar;
1311 gimple_stmt_iterator gsi;
5f40b3cb 1312 struct clsn_data clsn_data;
00f96dc9 1313 auto_vec<basic_block, 3> body;
9f9f72aa
AP
1314 basic_block bb;
1315 basic_block entry_bb = bb1;
1316 basic_block exit_bb = exit->dest;
b5b8b0ac 1317 bool has_debug_stmt = false;
5f40b3cb 1318
726a989a 1319 entry = single_succ_edge (entry_bb);
9f9f72aa 1320 gather_blocks_in_sese_region (entry_bb, exit_bb, &body);
5f40b3cb 1321
9771b263 1322 FOR_EACH_VEC_ELT (body, i, bb)
9f9f72aa 1323 {
b8698a0f 1324 if (bb != entry_bb && bb != exit_bb)
9f9f72aa 1325 {
726a989a
RB
1326 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1327 separate_decls_in_region_stmt (entry, exit, gsi_stmt (gsi),
c203e8a7 1328 &name_copies, &decl_copies);
726a989a
RB
1329
1330 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
b5b8b0ac
AO
1331 {
1332 gimple stmt = gsi_stmt (gsi);
1333
1334 if (is_gimple_debug (stmt))
1335 has_debug_stmt = true;
1336 else
1337 separate_decls_in_region_stmt (entry, exit, stmt,
c203e8a7 1338 &name_copies, &decl_copies);
b5b8b0ac 1339 }
9f9f72aa 1340 }
5f40b3cb 1341 }
9f9f72aa 1342
b5b8b0ac
AO
1343 /* Now process debug bind stmts. We must not create decls while
1344 processing debug stmts, so we defer their processing so as to
1345 make sure we will have debug info for as many variables as
1346 possible (all of those that were dealt with in the loop above),
1347 and discard those for which we know there's nothing we can
1348 do. */
1349 if (has_debug_stmt)
9771b263 1350 FOR_EACH_VEC_ELT (body, i, bb)
b5b8b0ac
AO
1351 if (bb != entry_bb && bb != exit_bb)
1352 {
1353 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
1354 {
1355 gimple stmt = gsi_stmt (gsi);
1356
ddb555ed 1357 if (is_gimple_debug (stmt))
b5b8b0ac 1358 {
c203e8a7
TS
1359 if (separate_decls_in_region_debug (stmt, &name_copies,
1360 &decl_copies))
b5b8b0ac
AO
1361 {
1362 gsi_remove (&gsi, true);
1363 continue;
1364 }
1365 }
1366
1367 gsi_next (&gsi);
1368 }
1369 }
1370
c203e8a7 1371 if (name_copies.elements () == 0 && reduction_list->elements () == 0)
5f40b3cb
ZD
1372 {
1373 /* It may happen that there is nothing to copy (if there are only
a509ebb5 1374 loop carried and external variables in the loop). */
5f40b3cb
ZD
1375 *arg_struct = NULL;
1376 *new_arg_struct = NULL;
1377 }
1378 else
1379 {
1380 /* Create the type for the structure to store the ssa names to. */
1381 type = lang_hooks.types.make_type (RECORD_TYPE);
9ff70652 1382 type_name = build_decl (UNKNOWN_LOCATION,
c2255bc4 1383 TYPE_DECL, create_tmp_var_name (".paral_data"),
5f40b3cb
ZD
1384 type);
1385 TYPE_NAME (type) = type_name;
1386
4a8fb1a1 1387 name_copies.traverse <tree, add_field_for_name> (type);
c203e8a7 1388 if (reduction_list && reduction_list->elements () > 0)
0eb7e7aa
RL
1389 {
1390 /* Create the fields for reductions. */
c203e8a7 1391 reduction_list->traverse <tree, add_field_for_reduction> (type);
0eb7e7aa 1392 }
5f40b3cb 1393 layout_type (type);
b8698a0f 1394
5f40b3cb
ZD
1395 /* Create the loads and stores. */
1396 *arg_struct = create_tmp_var (type, ".paral_data_store");
5f40b3cb 1397 nvar = create_tmp_var (build_pointer_type (type), ".paral_data_load");
b731b390 1398 *new_arg_struct = make_ssa_name (nvar);
5f40b3cb 1399
a509ebb5
RL
1400 ld_st_data->store = *arg_struct;
1401 ld_st_data->load = *new_arg_struct;
1402 ld_st_data->store_bb = bb0;
1403 ld_st_data->load_bb = bb1;
0eb7e7aa 1404
4a8fb1a1
LC
1405 name_copies
1406 .traverse <struct clsn_data *, create_loads_and_stores_for_name>
1407 (ld_st_data);
a509ebb5 1408
ae0bce62
RL
1409 /* Load the calculation from memory (after the join of the threads). */
1410
c203e8a7 1411 if (reduction_list && reduction_list->elements () > 0)
a509ebb5 1412 {
4a8fb1a1 1413 reduction_list
c203e8a7
TS
1414 ->traverse <struct clsn_data *, create_stores_for_reduction>
1415 (ld_st_data);
b731b390 1416 clsn_data.load = make_ssa_name (nvar);
9f9f72aa 1417 clsn_data.load_bb = exit->dest;
a509ebb5
RL
1418 clsn_data.store = ld_st_data->store;
1419 create_final_loads_for_reduction (reduction_list, &clsn_data);
1420 }
5f40b3cb 1421 }
5f40b3cb
ZD
1422}
1423
1424/* Bitmap containing uids of functions created by parallelization. We cannot
1425 allocate it from the default obstack, as it must live across compilation
1426 of several functions; we make it gc allocated instead. */
1427
1428static GTY(()) bitmap parallelized_functions;
1429
1430/* Returns true if FN was created by create_loop_fn. */
1431
62e0a1ed 1432bool
5f40b3cb
ZD
1433parallelized_function_p (tree fn)
1434{
1435 if (!parallelized_functions || !DECL_ARTIFICIAL (fn))
1436 return false;
1437
1438 return bitmap_bit_p (parallelized_functions, DECL_UID (fn));
1439}
1440
1441/* Creates and returns an empty function that will receive the body of
1442 a parallelized loop. */
1443
1444static tree
9ff70652 1445create_loop_fn (location_t loc)
5f40b3cb
ZD
1446{
1447 char buf[100];
1448 char *tname;
1449 tree decl, type, name, t;
1450 struct function *act_cfun = cfun;
1451 static unsigned loopfn_num;
1452
5368224f 1453 loc = LOCATION_LOCUS (loc);
5f40b3cb
ZD
1454 snprintf (buf, 100, "%s.$loopfn", current_function_name ());
1455 ASM_FORMAT_PRIVATE_NAME (tname, buf, loopfn_num++);
1456 clean_symbol_name (tname);
1457 name = get_identifier (tname);
1458 type = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
1459
9ff70652 1460 decl = build_decl (loc, FUNCTION_DECL, name, type);
5f40b3cb
ZD
1461 if (!parallelized_functions)
1462 parallelized_functions = BITMAP_GGC_ALLOC ();
1463 bitmap_set_bit (parallelized_functions, DECL_UID (decl));
1464
1465 TREE_STATIC (decl) = 1;
1466 TREE_USED (decl) = 1;
1467 DECL_ARTIFICIAL (decl) = 1;
1468 DECL_IGNORED_P (decl) = 0;
1469 TREE_PUBLIC (decl) = 0;
1470 DECL_UNINLINABLE (decl) = 1;
1471 DECL_EXTERNAL (decl) = 0;
1472 DECL_CONTEXT (decl) = NULL_TREE;
1473 DECL_INITIAL (decl) = make_node (BLOCK);
1474
9ff70652 1475 t = build_decl (loc, RESULT_DECL, NULL_TREE, void_type_node);
5f40b3cb
ZD
1476 DECL_ARTIFICIAL (t) = 1;
1477 DECL_IGNORED_P (t) = 1;
1478 DECL_RESULT (decl) = t;
1479
9ff70652 1480 t = build_decl (loc, PARM_DECL, get_identifier (".paral_data_param"),
5f40b3cb
ZD
1481 ptr_type_node);
1482 DECL_ARTIFICIAL (t) = 1;
1483 DECL_ARG_TYPE (t) = ptr_type_node;
1484 DECL_CONTEXT (t) = decl;
1485 TREE_USED (t) = 1;
1486 DECL_ARGUMENTS (decl) = t;
1487
182e0d71 1488 allocate_struct_function (decl, false);
5f40b3cb
ZD
1489
1490 /* The call to allocate_struct_function clobbers CFUN, so we need to restore
1491 it. */
5576d6f2 1492 set_cfun (act_cfun);
5f40b3cb
ZD
1493
1494 return decl;
1495}
1496
5f40b3cb
ZD
1497/* Moves the exit condition of LOOP to the beginning of its header, and
1498 duplicates the part of the last iteration that gets disabled to the
1499 exit of the loop. NIT is the number of iterations of the loop
1500 (used to initialize the variables in the duplicated part).
b8698a0f 1501
fa10beec 1502 TODO: the common case is that latch of the loop is empty and immediately
5f40b3cb
ZD
1503 follows the loop exit. In this case, it would be better not to copy the
1504 body of the loop, but only move the entry of the loop directly before the
1505 exit check and increase the number of iterations of the loop by one.
b8698a0f 1506 This may need some additional preconditioning in case NIT = ~0.
a509ebb5 1507 REDUCTION_LIST describes the reductions in LOOP. */
5f40b3cb
ZD
1508
1509static void
4a8fb1a1 1510transform_to_exit_first_loop (struct loop *loop,
c203e8a7 1511 reduction_info_table_type *reduction_list,
4a8fb1a1 1512 tree nit)
5f40b3cb
ZD
1513{
1514 basic_block *bbs, *nbbs, ex_bb, orig_header;
1515 unsigned n;
1516 bool ok;
1517 edge exit = single_dom_exit (loop), hpred;
726a989a 1518 tree control, control_name, res, t;
538dd0b7
DM
1519 gphi *phi, *nphi;
1520 gassign *stmt;
1521 gcond *cond_stmt, *cond_nit;
48710229 1522 tree nit_1;
5f40b3cb
ZD
1523
1524 split_block_after_labels (loop->header);
1525 orig_header = single_succ (loop->header);
1526 hpred = single_succ_edge (loop->header);
1527
538dd0b7 1528 cond_stmt = as_a <gcond *> (last_stmt (exit->src));
726a989a
RB
1529 control = gimple_cond_lhs (cond_stmt);
1530 gcc_assert (gimple_cond_rhs (cond_stmt) == nit);
5f40b3cb
ZD
1531
1532 /* Make sure that we have phi nodes on exit for all loop header phis
1533 (create_parallel_loop requires that). */
538dd0b7
DM
1534 for (gphi_iterator gsi = gsi_start_phis (loop->header);
1535 !gsi_end_p (gsi);
1536 gsi_next (&gsi))
5f40b3cb 1537 {
538dd0b7 1538 phi = gsi.phi ();
5f40b3cb 1539 res = PHI_RESULT (phi);
070ecdfd 1540 t = copy_ssa_name (res, phi);
5f40b3cb 1541 SET_PHI_RESULT (phi, t);
5f40b3cb 1542 nphi = create_phi_node (res, orig_header);
9e227d60 1543 add_phi_arg (nphi, t, hpred, UNKNOWN_LOCATION);
5f40b3cb
ZD
1544
1545 if (res == control)
1546 {
726a989a 1547 gimple_cond_set_lhs (cond_stmt, t);
5f40b3cb
ZD
1548 update_stmt (cond_stmt);
1549 control = t;
1550 }
1551 }
12037899 1552
5f40b3cb 1553 bbs = get_loop_body_in_dom_order (loop);
48710229 1554
69958396
RL
1555 for (n = 0; bbs[n] != exit->src; n++)
1556 continue;
5f40b3cb 1557 nbbs = XNEWVEC (basic_block, n);
726a989a
RB
1558 ok = gimple_duplicate_sese_tail (single_succ_edge (loop->header), exit,
1559 bbs + 1, n, nbbs);
5f40b3cb
ZD
1560 gcc_assert (ok);
1561 free (bbs);
1562 ex_bb = nbbs[0];
1563 free (nbbs);
1564
b8698a0f 1565 /* Other than reductions, the only gimple reg that should be copied
726a989a 1566 out of the loop is the control variable. */
69958396 1567 exit = single_dom_exit (loop);
5f40b3cb 1568 control_name = NULL_TREE;
538dd0b7
DM
1569 for (gphi_iterator gsi = gsi_start_phis (ex_bb);
1570 !gsi_end_p (gsi); )
5f40b3cb 1571 {
538dd0b7 1572 phi = gsi.phi ();
5f40b3cb 1573 res = PHI_RESULT (phi);
ea057359 1574 if (virtual_operand_p (res))
726a989a
RB
1575 {
1576 gsi_next (&gsi);
1577 continue;
1578 }
5f40b3cb 1579
a509ebb5 1580 /* Check if it is a part of reduction. If it is,
b8698a0f
L
1581 keep the phi at the reduction's keep_res field. The
1582 PHI_RESULT of this phi is the resulting value of the reduction
a509ebb5
RL
1583 variable when exiting the loop. */
1584
c203e8a7 1585 if (reduction_list->elements () > 0)
a509ebb5
RL
1586 {
1587 struct reduction_info *red;
1588
1589 tree val = PHI_ARG_DEF_FROM_EDGE (phi, exit);
a509ebb5
RL
1590 red = reduction_phi (reduction_list, SSA_NAME_DEF_STMT (val));
1591 if (red)
726a989a
RB
1592 {
1593 red->keep_res = phi;
1594 gsi_next (&gsi);
1595 continue;
1596 }
a509ebb5 1597 }
726a989a
RB
1598 gcc_assert (control_name == NULL_TREE
1599 && SSA_NAME_VAR (res) == SSA_NAME_VAR (control));
5f40b3cb 1600 control_name = res;
726a989a 1601 remove_phi_node (&gsi, false);
5f40b3cb
ZD
1602 }
1603 gcc_assert (control_name != NULL_TREE);
5f40b3cb 1604
b8698a0f 1605 /* Initialize the control variable to number of iterations
48710229 1606 according to the rhs of the exit condition. */
538dd0b7
DM
1607 gimple_stmt_iterator gsi = gsi_after_labels (ex_bb);
1608 cond_nit = as_a <gcond *> (last_stmt (exit->src));
48710229
RL
1609 nit_1 = gimple_cond_rhs (cond_nit);
1610 nit_1 = force_gimple_operand_gsi (&gsi,
1611 fold_convert (TREE_TYPE (control_name), nit_1),
726a989a 1612 false, NULL_TREE, false, GSI_SAME_STMT);
48710229 1613 stmt = gimple_build_assign (control_name, nit_1);
726a989a 1614 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
5f40b3cb
ZD
1615}
1616
1617/* Create the parallel constructs for LOOP as described in gen_parallel_loop.
726a989a 1618 LOOP_FN and DATA are the arguments of GIMPLE_OMP_PARALLEL.
5f40b3cb
ZD
1619 NEW_DATA is the variable that should be initialized from the argument
1620 of LOOP_FN. N_THREADS is the requested number of threads. Returns the
726a989a 1621 basic block containing GIMPLE_OMP_PARALLEL tree. */
5f40b3cb
ZD
1622
1623static basic_block
1624create_parallel_loop (struct loop *loop, tree loop_fn, tree data,
9ff70652 1625 tree new_data, unsigned n_threads, location_t loc)
5f40b3cb 1626{
726a989a 1627 gimple_stmt_iterator gsi;
5f40b3cb 1628 basic_block bb, paral_bb, for_bb, ex_bb;
0f900dfa 1629 tree t, param;
538dd0b7
DM
1630 gomp_parallel *omp_par_stmt;
1631 gimple omp_return_stmt1, omp_return_stmt2;
1632 gimple phi;
1633 gcond *cond_stmt;
1634 gomp_for *for_stmt;
1635 gomp_continue *omp_cont_stmt;
726a989a 1636 tree cvar, cvar_init, initvar, cvar_next, cvar_base, type;
5f40b3cb
ZD
1637 edge exit, nexit, guard, end, e;
1638
726a989a 1639 /* Prepare the GIMPLE_OMP_PARALLEL statement. */
5f40b3cb
ZD
1640 bb = loop_preheader_edge (loop)->src;
1641 paral_bb = single_pred (bb);
726a989a 1642 gsi = gsi_last_bb (paral_bb);
5f40b3cb 1643
9ff70652 1644 t = build_omp_clause (loc, OMP_CLAUSE_NUM_THREADS);
5f40b3cb 1645 OMP_CLAUSE_NUM_THREADS_EXPR (t)
a509ebb5 1646 = build_int_cst (integer_type_node, n_threads);
538dd0b7
DM
1647 omp_par_stmt = gimple_build_omp_parallel (NULL, t, loop_fn, data);
1648 gimple_set_location (omp_par_stmt, loc);
5f40b3cb 1649
538dd0b7 1650 gsi_insert_after (&gsi, omp_par_stmt, GSI_NEW_STMT);
5f40b3cb
ZD
1651
1652 /* Initialize NEW_DATA. */
1653 if (data)
1654 {
538dd0b7
DM
1655 gassign *assign_stmt;
1656
726a989a
RB
1657 gsi = gsi_after_labels (bb);
1658
b731b390 1659 param = make_ssa_name (DECL_ARGUMENTS (loop_fn));
538dd0b7
DM
1660 assign_stmt = gimple_build_assign (param, build_fold_addr_expr (data));
1661 gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
726a989a 1662
538dd0b7 1663 assign_stmt = gimple_build_assign (new_data,
726a989a 1664 fold_convert (TREE_TYPE (new_data), param));
538dd0b7 1665 gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
5f40b3cb
ZD
1666 }
1667
726a989a 1668 /* Emit GIMPLE_OMP_RETURN for GIMPLE_OMP_PARALLEL. */
5f40b3cb 1669 bb = split_loop_exit_edge (single_dom_exit (loop));
726a989a 1670 gsi = gsi_last_bb (bb);
538dd0b7
DM
1671 omp_return_stmt1 = gimple_build_omp_return (false);
1672 gimple_set_location (omp_return_stmt1, loc);
1673 gsi_insert_after (&gsi, omp_return_stmt1, GSI_NEW_STMT);
5f40b3cb 1674
726a989a 1675 /* Extract data for GIMPLE_OMP_FOR. */
5f40b3cb 1676 gcc_assert (loop->header == single_dom_exit (loop)->src);
538dd0b7 1677 cond_stmt = as_a <gcond *> (last_stmt (loop->header));
5f40b3cb 1678
726a989a 1679 cvar = gimple_cond_lhs (cond_stmt);
5f40b3cb
ZD
1680 cvar_base = SSA_NAME_VAR (cvar);
1681 phi = SSA_NAME_DEF_STMT (cvar);
1682 cvar_init = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
b731b390 1683 initvar = copy_ssa_name (cvar);
5f40b3cb
ZD
1684 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, loop_preheader_edge (loop)),
1685 initvar);
1686 cvar_next = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
1687
1dff453d 1688 gsi = gsi_last_nondebug_bb (loop->latch);
726a989a
RB
1689 gcc_assert (gsi_stmt (gsi) == SSA_NAME_DEF_STMT (cvar_next));
1690 gsi_remove (&gsi, true);
5f40b3cb
ZD
1691
1692 /* Prepare cfg. */
1693 for_bb = split_edge (loop_preheader_edge (loop));
1694 ex_bb = split_loop_exit_edge (single_dom_exit (loop));
1695 extract_true_false_edges_from_block (loop->header, &nexit, &exit);
1696 gcc_assert (exit == single_dom_exit (loop));
1697
1698 guard = make_edge (for_bb, ex_bb, 0);
1699 single_succ_edge (loop->latch)->flags = 0;
1700 end = make_edge (loop->latch, ex_bb, EDGE_FALLTHRU);
538dd0b7
DM
1701 for (gphi_iterator gpi = gsi_start_phis (ex_bb);
1702 !gsi_end_p (gpi); gsi_next (&gpi))
5f40b3cb 1703 {
f5045c96
AM
1704 source_location locus;
1705 tree def;
538dd0b7
DM
1706 gphi *phi = gpi.phi ();
1707 gphi *stmt;
1708
1709 stmt = as_a <gphi *> (
1710 SSA_NAME_DEF_STMT (PHI_ARG_DEF_FROM_EDGE (phi, exit)));
f5045c96
AM
1711
1712 def = PHI_ARG_DEF_FROM_EDGE (stmt, loop_preheader_edge (loop));
b8698a0f 1713 locus = gimple_phi_arg_location_from_edge (stmt,
f5045c96 1714 loop_preheader_edge (loop));
9e227d60 1715 add_phi_arg (phi, def, guard, locus);
f5045c96
AM
1716
1717 def = PHI_ARG_DEF_FROM_EDGE (stmt, loop_latch_edge (loop));
1718 locus = gimple_phi_arg_location_from_edge (stmt, loop_latch_edge (loop));
9e227d60 1719 add_phi_arg (phi, def, end, locus);
5f40b3cb
ZD
1720 }
1721 e = redirect_edge_and_branch (exit, nexit->dest);
1722 PENDING_STMT (e) = NULL;
1723
726a989a
RB
1724 /* Emit GIMPLE_OMP_FOR. */
1725 gimple_cond_set_lhs (cond_stmt, cvar_base);
5f40b3cb 1726 type = TREE_TYPE (cvar);
9ff70652 1727 t = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
5f40b3cb
ZD
1728 OMP_CLAUSE_SCHEDULE_KIND (t) = OMP_CLAUSE_SCHEDULE_STATIC;
1729
74bf76ed 1730 for_stmt = gimple_build_omp_for (NULL, GF_OMP_FOR_KIND_FOR, t, 1, NULL);
9ff70652 1731 gimple_set_location (for_stmt, loc);
726a989a
RB
1732 gimple_omp_for_set_index (for_stmt, 0, initvar);
1733 gimple_omp_for_set_initial (for_stmt, 0, cvar_init);
1734 gimple_omp_for_set_final (for_stmt, 0, gimple_cond_rhs (cond_stmt));
1735 gimple_omp_for_set_cond (for_stmt, 0, gimple_cond_code (cond_stmt));
1736 gimple_omp_for_set_incr (for_stmt, 0, build2 (PLUS_EXPR, type,
1737 cvar_base,
1738 build_int_cst (type, 1)));
1739
1740 gsi = gsi_last_bb (for_bb);
1741 gsi_insert_after (&gsi, for_stmt, GSI_NEW_STMT);
5f40b3cb
ZD
1742 SSA_NAME_DEF_STMT (initvar) = for_stmt;
1743
726a989a
RB
1744 /* Emit GIMPLE_OMP_CONTINUE. */
1745 gsi = gsi_last_bb (loop->latch);
538dd0b7
DM
1746 omp_cont_stmt = gimple_build_omp_continue (cvar_next, cvar);
1747 gimple_set_location (omp_cont_stmt, loc);
1748 gsi_insert_after (&gsi, omp_cont_stmt, GSI_NEW_STMT);
1749 SSA_NAME_DEF_STMT (cvar_next) = omp_cont_stmt;
5f40b3cb 1750
726a989a
RB
1751 /* Emit GIMPLE_OMP_RETURN for GIMPLE_OMP_FOR. */
1752 gsi = gsi_last_bb (ex_bb);
538dd0b7
DM
1753 omp_return_stmt2 = gimple_build_omp_return (true);
1754 gimple_set_location (omp_return_stmt2, loc);
1755 gsi_insert_after (&gsi, omp_return_stmt2, GSI_NEW_STMT);
5f40b3cb 1756
cd7d9fd7
RG
1757 /* After the above dom info is hosed. Re-compute it. */
1758 free_dominance_info (CDI_DOMINATORS);
1759 calculate_dominance_info (CDI_DOMINATORS);
1760
5f40b3cb
ZD
1761 return paral_bb;
1762}
1763
08dab97a
RL
1764/* Generates code to execute the iterations of LOOP in N_THREADS
1765 threads in parallel.
1766
1767 NITER describes number of iterations of LOOP.
fa10beec 1768 REDUCTION_LIST describes the reductions existent in the LOOP. */
5f40b3cb
ZD
1769
1770static void
c203e8a7
TS
1771gen_parallel_loop (struct loop *loop,
1772 reduction_info_table_type *reduction_list,
a509ebb5 1773 unsigned n_threads, struct tree_niter_desc *niter)
5f40b3cb 1774{
5f40b3cb 1775 tree many_iterations_cond, type, nit;
726a989a
RB
1776 tree arg_struct, new_arg_struct;
1777 gimple_seq stmts;
9f9f72aa 1778 edge entry, exit;
a509ebb5 1779 struct clsn_data clsn_data;
5f40b3cb 1780 unsigned prob;
9ff70652
JJ
1781 location_t loc;
1782 gimple cond_stmt;
768da0da 1783 unsigned int m_p_thread=2;
5f40b3cb
ZD
1784
1785 /* From
1786
1787 ---------------------------------------------------------------------
1788 loop
1789 {
1790 IV = phi (INIT, IV + STEP)
1791 BODY1;
1792 if (COND)
1793 break;
1794 BODY2;
1795 }
1796 ---------------------------------------------------------------------
1797
1798 with # of iterations NITER (possibly with MAY_BE_ZERO assumption),
1799 we generate the following code:
1800
1801 ---------------------------------------------------------------------
1802
1803 if (MAY_BE_ZERO
a509ebb5
RL
1804 || NITER < MIN_PER_THREAD * N_THREADS)
1805 goto original;
5f40b3cb
ZD
1806
1807 BODY1;
1808 store all local loop-invariant variables used in body of the loop to DATA.
726a989a 1809 GIMPLE_OMP_PARALLEL (OMP_CLAUSE_NUM_THREADS (N_THREADS), LOOPFN, DATA);
5f40b3cb 1810 load the variables from DATA.
726a989a 1811 GIMPLE_OMP_FOR (IV = INIT; COND; IV += STEP) (OMP_CLAUSE_SCHEDULE (static))
5f40b3cb
ZD
1812 BODY2;
1813 BODY1;
726a989a
RB
1814 GIMPLE_OMP_CONTINUE;
1815 GIMPLE_OMP_RETURN -- GIMPLE_OMP_FOR
1816 GIMPLE_OMP_RETURN -- GIMPLE_OMP_PARALLEL
5f40b3cb
ZD
1817 goto end;
1818
1819 original:
1820 loop
1821 {
1822 IV = phi (INIT, IV + STEP)
1823 BODY1;
1824 if (COND)
1825 break;
1826 BODY2;
1827 }
1828
1829 end:
1830
1831 */
1832
1833 /* Create two versions of the loop -- in the old one, we know that the
1834 number of iterations is large enough, and we will transform it into the
1835 loop that will be split to loop_fn, the new one will be used for the
1836 remaining iterations. */
a509ebb5 1837
768da0da
RL
1838 /* We should compute a better number-of-iterations value for outer loops.
1839 That is, if we have
1840
1841 for (i = 0; i < n; ++i)
1842 for (j = 0; j < m; ++j)
1843 ...
1844
1845 we should compute nit = n * m, not nit = n.
1846 Also may_be_zero handling would need to be adjusted. */
1847
5f40b3cb
ZD
1848 type = TREE_TYPE (niter->niter);
1849 nit = force_gimple_operand (unshare_expr (niter->niter), &stmts, true,
1850 NULL_TREE);
1851 if (stmts)
726a989a 1852 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
5f40b3cb 1853
768da0da
RL
1854 if (loop->inner)
1855 m_p_thread=2;
1856 else
1857 m_p_thread=MIN_PER_THREAD;
1858
1859 many_iterations_cond =
1860 fold_build2 (GE_EXPR, boolean_type_node,
1861 nit, build_int_cst (type, m_p_thread * n_threads));
1862
5f40b3cb 1863 many_iterations_cond
a509ebb5
RL
1864 = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
1865 invert_truthvalue (unshare_expr (niter->may_be_zero)),
1866 many_iterations_cond);
5f40b3cb 1867 many_iterations_cond
a509ebb5 1868 = force_gimple_operand (many_iterations_cond, &stmts, false, NULL_TREE);
5f40b3cb 1869 if (stmts)
726a989a 1870 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
5f40b3cb
ZD
1871 if (!is_gimple_condexpr (many_iterations_cond))
1872 {
1873 many_iterations_cond
a509ebb5
RL
1874 = force_gimple_operand (many_iterations_cond, &stmts,
1875 true, NULL_TREE);
5f40b3cb 1876 if (stmts)
726a989a 1877 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
5f40b3cb
ZD
1878 }
1879
1880 initialize_original_copy_tables ();
1881
1882 /* We assume that the loop usually iterates a lot. */
1883 prob = 4 * REG_BR_PROB_BASE / 5;
0f900dfa
JJ
1884 loop_version (loop, many_iterations_cond, NULL,
1885 prob, prob, REG_BR_PROB_BASE - prob, true);
5f40b3cb
ZD
1886 update_ssa (TODO_update_ssa);
1887 free_original_copy_tables ();
1888
1889 /* Base all the induction variables in LOOP on a single control one. */
c80a5403 1890 canonicalize_loop_ivs (loop, &nit, true);
5f40b3cb
ZD
1891
1892 /* Ensure that the exit condition is the first statement in the loop. */
a509ebb5
RL
1893 transform_to_exit_first_loop (loop, reduction_list, nit);
1894
fa10beec 1895 /* Generate initializations for reductions. */
c203e8a7
TS
1896 if (reduction_list->elements () > 0)
1897 reduction_list->traverse <struct loop *, initialize_reductions> (loop);
5f40b3cb
ZD
1898
1899 /* Eliminate the references to local variables from the loop. */
9f9f72aa
AP
1900 gcc_assert (single_exit (loop));
1901 entry = loop_preheader_edge (loop);
1902 exit = single_dom_exit (loop);
5f40b3cb 1903
9f9f72aa 1904 eliminate_local_variables (entry, exit);
5f40b3cb
ZD
1905 /* In the old loop, move all variables non-local to the loop to a structure
1906 and back, and create separate decls for the variables used in loop. */
b8698a0f 1907 separate_decls_in_region (entry, exit, reduction_list, &arg_struct,
9f9f72aa 1908 &new_arg_struct, &clsn_data);
5f40b3cb
ZD
1909
1910 /* Create the parallel constructs. */
9ff70652
JJ
1911 loc = UNKNOWN_LOCATION;
1912 cond_stmt = last_stmt (loop->header);
1913 if (cond_stmt)
1914 loc = gimple_location (cond_stmt);
18751894
TV
1915 create_parallel_loop (loop, create_loop_fn (loc), arg_struct,
1916 new_arg_struct, n_threads, loc);
c203e8a7 1917 if (reduction_list->elements () > 0)
a509ebb5 1918 create_call_for_reduction (loop, reduction_list, &clsn_data);
5f40b3cb
ZD
1919
1920 scev_reset ();
1921
1922 /* Cancel the loop (it is simpler to do it here rather than to teach the
1923 expander to do it). */
1924 cancel_loop_tree (loop);
1925
92a6bdbd
SP
1926 /* Free loop bound estimations that could contain references to
1927 removed statements. */
f0bd40b1 1928 FOR_EACH_LOOP (loop, 0)
92a6bdbd 1929 free_numbers_of_iterations_estimates_loop (loop);
5f40b3cb
ZD
1930}
1931
9857228c
SP
1932/* Returns true when LOOP contains vector phi nodes. */
1933
1934static bool
726a989a 1935loop_has_vector_phi_nodes (struct loop *loop ATTRIBUTE_UNUSED)
9857228c
SP
1936{
1937 unsigned i;
1938 basic_block *bbs = get_loop_body_in_dom_order (loop);
538dd0b7 1939 gphi_iterator gsi;
9857228c 1940 bool res = true;
9857228c
SP
1941
1942 for (i = 0; i < loop->num_nodes; i++)
726a989a 1943 for (gsi = gsi_start_phis (bbs[i]); !gsi_end_p (gsi); gsi_next (&gsi))
538dd0b7 1944 if (TREE_CODE (TREE_TYPE (PHI_RESULT (gsi.phi ()))) == VECTOR_TYPE)
9857228c
SP
1945 goto end;
1946
1947 res = false;
1948 end:
1949 free (bbs);
1950 return res;
1951}
1952
08dab97a
RL
1953/* Create a reduction_info struct, initialize it with REDUC_STMT
1954 and PHI, insert it to the REDUCTION_LIST. */
1955
1956static void
c203e8a7 1957build_new_reduction (reduction_info_table_type *reduction_list,
538dd0b7 1958 gimple reduc_stmt, gphi *phi)
08dab97a 1959{
4a8fb1a1 1960 reduction_info **slot;
08dab97a
RL
1961 struct reduction_info *new_reduction;
1962
1963 gcc_assert (reduc_stmt);
b8698a0f 1964
08dab97a
RL
1965 if (dump_file && (dump_flags & TDF_DETAILS))
1966 {
1967 fprintf (dump_file,
1968 "Detected reduction. reduction stmt is: \n");
1969 print_gimple_stmt (dump_file, reduc_stmt, 0, 0);
1970 fprintf (dump_file, "\n");
1971 }
b8698a0f 1972
08dab97a 1973 new_reduction = XCNEW (struct reduction_info);
b8698a0f 1974
08dab97a
RL
1975 new_reduction->reduc_stmt = reduc_stmt;
1976 new_reduction->reduc_phi = phi;
5d1fd1de 1977 new_reduction->reduc_version = SSA_NAME_VERSION (gimple_phi_result (phi));
08dab97a 1978 new_reduction->reduction_code = gimple_assign_rhs_code (reduc_stmt);
c203e8a7 1979 slot = reduction_list->find_slot (new_reduction, INSERT);
08dab97a
RL
1980 *slot = new_reduction;
1981}
1982
5d1fd1de
JJ
1983/* Callback for htab_traverse. Sets gimple_uid of reduc_phi stmts. */
1984
4a8fb1a1
LC
1985int
1986set_reduc_phi_uids (reduction_info **slot, void *data ATTRIBUTE_UNUSED)
5d1fd1de 1987{
4a8fb1a1 1988 struct reduction_info *const red = *slot;
5d1fd1de
JJ
1989 gimple_set_uid (red->reduc_phi, red->reduc_version);
1990 return 1;
1991}
1992
08dab97a
RL
1993/* Detect all reductions in the LOOP, insert them into REDUCTION_LIST. */
1994
1995static void
c203e8a7 1996gather_scalar_reductions (loop_p loop, reduction_info_table_type *reduction_list)
08dab97a 1997{
538dd0b7 1998 gphi_iterator gsi;
08dab97a
RL
1999 loop_vec_info simple_loop_info;
2000
08dab97a
RL
2001 simple_loop_info = vect_analyze_loop_form (loop);
2002
2003 for (gsi = gsi_start_phis (loop->header); !gsi_end_p (gsi); gsi_next (&gsi))
2004 {
538dd0b7 2005 gphi *phi = gsi.phi ();
08dab97a
RL
2006 affine_iv iv;
2007 tree res = PHI_RESULT (phi);
2008 bool double_reduc;
2009
ea057359 2010 if (virtual_operand_p (res))
08dab97a
RL
2011 continue;
2012
2013 if (!simple_iv (loop, loop, res, &iv, true)
2014 && simple_loop_info)
2015 {
8a9ecffd
MM
2016 gimple reduc_stmt = vect_force_simple_reduction (simple_loop_info,
2017 phi, true,
2018 &double_reduc);
48710229 2019 if (reduc_stmt && !double_reduc)
08dab97a
RL
2020 build_new_reduction (reduction_list, reduc_stmt, phi);
2021 }
2022 }
5d1fd1de
JJ
2023 destroy_loop_vec_info (simple_loop_info, true);
2024
2025 /* As gimple_uid is used by the vectorizer in between vect_analyze_loop_form
2026 and destroy_loop_vec_info, we can set gimple_uid of reduc_phi stmts
2027 only now. */
c203e8a7 2028 reduction_list->traverse <void *, set_reduc_phi_uids> (NULL);
08dab97a
RL
2029}
2030
2031/* Try to initialize NITER for code generation part. */
2032
2033static bool
2034try_get_loop_niter (loop_p loop, struct tree_niter_desc *niter)
2035{
2036 edge exit = single_dom_exit (loop);
2037
2038 gcc_assert (exit);
2039
2040 /* We need to know # of iterations, and there should be no uses of values
2041 defined inside loop outside of it, unless the values are invariants of
2042 the loop. */
2043 if (!number_of_iterations_exit (loop, exit, niter, false))
2044 {
2045 if (dump_file && (dump_flags & TDF_DETAILS))
2046 fprintf (dump_file, " FAILED: number of iterations not known\n");
2047 return false;
2048 }
2049
2050 return true;
2051}
2052
2053/* Try to initialize REDUCTION_LIST for code generation part.
2054 REDUCTION_LIST describes the reductions. */
2055
2056static bool
4a8fb1a1 2057try_create_reduction_list (loop_p loop,
c203e8a7 2058 reduction_info_table_type *reduction_list)
08dab97a
RL
2059{
2060 edge exit = single_dom_exit (loop);
538dd0b7 2061 gphi_iterator gsi;
08dab97a
RL
2062
2063 gcc_assert (exit);
2064
2065 gather_scalar_reductions (loop, reduction_list);
2066
b8698a0f 2067
08dab97a
RL
2068 for (gsi = gsi_start_phis (exit->dest); !gsi_end_p (gsi); gsi_next (&gsi))
2069 {
538dd0b7 2070 gphi *phi = gsi.phi ();
08dab97a
RL
2071 struct reduction_info *red;
2072 imm_use_iterator imm_iter;
2073 use_operand_p use_p;
2074 gimple reduc_phi;
2075 tree val = PHI_ARG_DEF_FROM_EDGE (phi, exit);
2076
ea057359 2077 if (!virtual_operand_p (val))
08dab97a
RL
2078 {
2079 if (dump_file && (dump_flags & TDF_DETAILS))
2080 {
2081 fprintf (dump_file, "phi is ");
2082 print_gimple_stmt (dump_file, phi, 0, 0);
2083 fprintf (dump_file, "arg of phi to exit: value ");
2084 print_generic_expr (dump_file, val, 0);
2085 fprintf (dump_file, " used outside loop\n");
2086 fprintf (dump_file,
2087 " checking if it a part of reduction pattern: \n");
2088 }
c203e8a7 2089 if (reduction_list->elements () == 0)
08dab97a
RL
2090 {
2091 if (dump_file && (dump_flags & TDF_DETAILS))
2092 fprintf (dump_file,
2093 " FAILED: it is not a part of reduction.\n");
2094 return false;
2095 }
2096 reduc_phi = NULL;
2097 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, val)
2098 {
4942af9b
JJ
2099 if (!gimple_debug_bind_p (USE_STMT (use_p))
2100 && flow_bb_inside_loop_p (loop, gimple_bb (USE_STMT (use_p))))
08dab97a
RL
2101 {
2102 reduc_phi = USE_STMT (use_p);
2103 break;
2104 }
2105 }
2106 red = reduction_phi (reduction_list, reduc_phi);
2107 if (red == NULL)
2108 {
2109 if (dump_file && (dump_flags & TDF_DETAILS))
2110 fprintf (dump_file,
2111 " FAILED: it is not a part of reduction.\n");
2112 return false;
2113 }
2114 if (dump_file && (dump_flags & TDF_DETAILS))
2115 {
2116 fprintf (dump_file, "reduction phi is ");
2117 print_gimple_stmt (dump_file, red->reduc_phi, 0, 0);
2118 fprintf (dump_file, "reduction stmt is ");
2119 print_gimple_stmt (dump_file, red->reduc_stmt, 0, 0);
2120 }
2121 }
2122 }
2123
2124 /* The iterations of the loop may communicate only through bivs whose
2125 iteration space can be distributed efficiently. */
2126 for (gsi = gsi_start_phis (loop->header); !gsi_end_p (gsi); gsi_next (&gsi))
2127 {
538dd0b7 2128 gphi *phi = gsi.phi ();
08dab97a
RL
2129 tree def = PHI_RESULT (phi);
2130 affine_iv iv;
2131
ea057359 2132 if (!virtual_operand_p (def) && !simple_iv (loop, loop, def, &iv, true))
08dab97a
RL
2133 {
2134 struct reduction_info *red;
2135
2136 red = reduction_phi (reduction_list, phi);
2137 if (red == NULL)
2138 {
2139 if (dump_file && (dump_flags & TDF_DETAILS))
2140 fprintf (dump_file,
2141 " FAILED: scalar dependency between iterations\n");
2142 return false;
2143 }
2144 }
2145 }
2146
2147
2148 return true;
2149}
2150
5f40b3cb
ZD
2151/* Detect parallel loops and generate parallel code using libgomp
2152 primitives. Returns true if some loop was parallelized, false
2153 otherwise. */
2154
2155bool
2156parallelize_loops (void)
2157{
2158 unsigned n_threads = flag_tree_parallelize_loops;
2159 bool changed = false;
2160 struct loop *loop;
2161 struct tree_niter_desc niter_desc;
f873b205 2162 struct obstack parloop_obstack;
8adfe01d 2163 HOST_WIDE_INT estimated;
b05e0233 2164 source_location loop_loc;
f873b205 2165
5f40b3cb
ZD
2166 /* Do not parallelize loops in the functions created by parallelization. */
2167 if (parallelized_function_p (cfun->decl))
2168 return false;
8adfe01d
RL
2169 if (cfun->has_nonlocal_label)
2170 return false;
5f40b3cb 2171
f873b205 2172 gcc_obstack_init (&parloop_obstack);
c203e8a7 2173 reduction_info_table_type reduction_list (10);
726a989a 2174 init_stmt_vec_info_vec ();
a509ebb5 2175
f0bd40b1 2176 FOR_EACH_LOOP (loop, 0)
5f40b3cb 2177 {
4a8fb1a1 2178 reduction_list.empty ();
48710229
RL
2179 if (dump_file && (dump_flags & TDF_DETAILS))
2180 {
2181 fprintf (dump_file, "Trying loop %d as candidate\n",loop->num);
2182 if (loop->inner)
2183 fprintf (dump_file, "loop %d is not innermost\n",loop->num);
2184 else
2185 fprintf (dump_file, "loop %d is innermost\n",loop->num);
2186 }
b8698a0f 2187
48710229 2188 /* If we use autopar in graphite pass, we use its marked dependency
87d4d0ee
SP
2189 checking results. */
2190 if (flag_loop_parallelize_all && !loop->can_be_parallel)
48710229
RL
2191 {
2192 if (dump_file && (dump_flags & TDF_DETAILS))
2193 fprintf (dump_file, "loop is not parallel according to graphite\n");
87d4d0ee 2194 continue;
48710229 2195 }
87d4d0ee 2196
48710229
RL
2197 if (!single_dom_exit (loop))
2198 {
b8698a0f 2199
48710229
RL
2200 if (dump_file && (dump_flags & TDF_DETAILS))
2201 fprintf (dump_file, "loop is !single_dom_exit\n");
b8698a0f 2202
08dab97a 2203 continue;
48710229 2204 }
08dab97a
RL
2205
2206 if (/* And of course, the loop must be parallelizable. */
2207 !can_duplicate_loop_p (loop)
1d4af1e8 2208 || loop_has_blocks_with_irreducible_flag (loop)
8adfe01d 2209 || (loop_preheader_edge (loop)->src->flags & BB_IRREDUCIBLE_LOOP)
9857228c 2210 /* FIXME: the check for vector phi nodes could be removed. */
69958396 2211 || loop_has_vector_phi_nodes (loop))
08dab97a 2212 continue;
e5b332cd 2213
652c4c71 2214 estimated = estimated_stmt_executions_int (loop);
e5b332cd
RG
2215 if (estimated == -1)
2216 estimated = max_stmt_executions_int (loop);
87d4d0ee 2217 /* FIXME: Bypass this check as graphite doesn't update the
e5b332cd 2218 count and frequency correctly now. */
87d4d0ee 2219 if (!flag_loop_parallelize_all
e5b332cd
RG
2220 && ((estimated != -1
2221 && estimated <= (HOST_WIDE_INT) n_threads * MIN_PER_THREAD)
87d4d0ee
SP
2222 /* Do not bother with loops in cold areas. */
2223 || optimize_loop_nest_for_size_p (loop)))
08dab97a 2224 continue;
b8698a0f 2225
08dab97a
RL
2226 if (!try_get_loop_niter (loop, &niter_desc))
2227 continue;
2228
c203e8a7 2229 if (!try_create_reduction_list (loop, &reduction_list))
08dab97a
RL
2230 continue;
2231
f873b205
LB
2232 if (!flag_loop_parallelize_all
2233 && !loop_parallel_p (loop, &parloop_obstack))
5f40b3cb
ZD
2234 continue;
2235
2236 changed = true;
48710229
RL
2237 if (dump_file && (dump_flags & TDF_DETAILS))
2238 {
48710229 2239 if (loop->inner)
8adfe01d 2240 fprintf (dump_file, "parallelizing outer loop %d\n",loop->header->index);
48710229 2241 else
8adfe01d
RL
2242 fprintf (dump_file, "parallelizing inner loop %d\n",loop->header->index);
2243 loop_loc = find_loop_location (loop);
b05e0233 2244 if (loop_loc != UNKNOWN_LOCATION)
8adfe01d 2245 fprintf (dump_file, "\nloop at %s:%d: ",
b05e0233 2246 LOCATION_FILE (loop_loc), LOCATION_LINE (loop_loc));
b8698a0f 2247 }
c203e8a7 2248 gen_parallel_loop (loop, &reduction_list,
08dab97a 2249 n_threads, &niter_desc);
5f40b3cb
ZD
2250 }
2251
726a989a 2252 free_stmt_vec_info_vec ();
f873b205 2253 obstack_free (&parloop_obstack, NULL);
6b8ed145
RG
2254
2255 /* Parallelization will cause new function calls to be inserted through
d086d311
RG
2256 which local variables will escape. Reset the points-to solution
2257 for ESCAPED. */
6b8ed145 2258 if (changed)
d086d311 2259 pt_solution_reset (&cfun->gimple_df->escaped);
6b8ed145 2260
5f40b3cb
ZD
2261 return changed;
2262}
2263
c1bf2a39
AM
2264/* Parallelization. */
2265
c1bf2a39
AM
2266namespace {
2267
2268const pass_data pass_data_parallelize_loops =
2269{
2270 GIMPLE_PASS, /* type */
2271 "parloops", /* name */
2272 OPTGROUP_LOOP, /* optinfo_flags */
c1bf2a39
AM
2273 TV_TREE_PARALLELIZE_LOOPS, /* tv_id */
2274 ( PROP_cfg | PROP_ssa ), /* properties_required */
2275 0, /* properties_provided */
2276 0, /* properties_destroyed */
2277 0, /* todo_flags_start */
3bea341f 2278 0, /* todo_flags_finish */
c1bf2a39
AM
2279};
2280
2281class pass_parallelize_loops : public gimple_opt_pass
2282{
2283public:
2284 pass_parallelize_loops (gcc::context *ctxt)
2285 : gimple_opt_pass (pass_data_parallelize_loops, ctxt)
2286 {}
2287
2288 /* opt_pass methods: */
1a3d085c 2289 virtual bool gate (function *) { return flag_tree_parallelize_loops > 1; }
be55bfe6 2290 virtual unsigned int execute (function *);
c1bf2a39
AM
2291
2292}; // class pass_parallelize_loops
2293
be55bfe6
TS
2294unsigned
2295pass_parallelize_loops::execute (function *fun)
2296{
2297 if (number_of_loops (fun) <= 1)
2298 return 0;
2299
2300 if (parallelize_loops ())
18751894
TV
2301 {
2302 fun->curr_properties &= ~(PROP_gimple_eomp);
2303 return TODO_update_ssa;
2304 }
2305
be55bfe6
TS
2306 return 0;
2307}
2308
c1bf2a39
AM
2309} // anon namespace
2310
2311gimple_opt_pass *
2312make_pass_parallelize_loops (gcc::context *ctxt)
2313{
2314 return new pass_parallelize_loops (ctxt);
2315}
2316
2317
5f40b3cb 2318#include "gt-tree-parloops.h"