]>
Commit | Line | Data |
---|---|---|
28c92cbb | 1 | /* Loop autoparallelization. |
fbd26352 | 2 | Copyright (C) 2006-2019 Free Software Foundation, Inc. |
0773b627 | 3 | Contributed by Sebastian Pop <pop@cri.ensmp.fr> |
4 | Zdenek Dvorak <dvorakz@suse.cz> and Razya Ladelsky <razya@il.ibm.com>. | |
28c92cbb | 5 | |
6 | This file is part of GCC. | |
7 | ||
8 | GCC is free software; you can redistribute it and/or modify it under | |
9 | the terms of the GNU General Public License as published by the Free | |
4ef8346d | 10 | Software Foundation; either version 3, or (at your option) any later |
28c92cbb | 11 | version. |
12 | ||
13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
4ef8346d | 19 | along with GCC; see the file COPYING3. If not see |
20 | <http://www.gnu.org/licenses/>. */ | |
28c92cbb | 21 | |
22 | #include "config.h" | |
23 | #include "system.h" | |
24 | #include "coretypes.h" | |
9ef16211 | 25 | #include "backend.h" |
b20a8bb4 | 26 | #include "tree.h" |
9ef16211 | 27 | #include "gimple.h" |
7c29e30e | 28 | #include "cfghooks.h" |
29 | #include "tree-pass.h" | |
9ef16211 | 30 | #include "ssa.h" |
7c29e30e | 31 | #include "cgraph.h" |
32 | #include "gimple-pretty-print.h" | |
9ef16211 | 33 | #include "fold-const.h" |
a8783bee | 34 | #include "gimplify.h" |
dcf1a1ec | 35 | #include "gimple-iterator.h" |
e795d6e1 | 36 | #include "gimplify-me.h" |
dcf1a1ec | 37 | #include "gimple-walk.h" |
9ed99284 | 38 | #include "stor-layout.h" |
39 | #include "tree-nested.h" | |
073c1fd5 | 40 | #include "tree-cfg.h" |
05d9c18a | 41 | #include "tree-ssa-loop-ivopts.h" |
42 | #include "tree-ssa-loop-manip.h" | |
43 | #include "tree-ssa-loop-niter.h" | |
073c1fd5 | 44 | #include "tree-ssa-loop.h" |
45 | #include "tree-into-ssa.h" | |
28c92cbb | 46 | #include "cfgloop.h" |
1e5b7b1f | 47 | #include "tree-scalar-evolution.h" |
28c92cbb | 48 | #include "langhooks.h" |
cb7f680b | 49 | #include "tree-vectorizer.h" |
d9dd21a8 | 50 | #include "tree-hasher.h" |
64641360 | 51 | #include "tree-parloops.h" |
4954efd4 | 52 | #include "omp-general.h" |
7740abd8 | 53 | #include "omp-low.h" |
d38409cd | 54 | #include "tree-ssa.h" |
9a782341 | 55 | #include "params.h" |
2331aa43 | 56 | #include "params-enum.h" |
31c2b13b | 57 | #include "tree-ssa-alias.h" |
58 | #include "tree-eh.h" | |
59 | #include "gomp-constants.h" | |
60 | #include "tree-dfa.h" | |
30a86690 | 61 | #include "stringpool.h" |
62 | #include "attribs.h" | |
28c92cbb | 63 | |
64 | /* This pass tries to distribute iterations of loops into several threads. | |
65 | The implementation is straightforward -- for each loop we test whether its | |
66 | iterations are independent, and if it is the case (and some additional | |
67 | conditions regarding profitability and correctness are satisfied), we | |
75a70cf9 | 68 | add GIMPLE_OMP_PARALLEL and GIMPLE_OMP_FOR codes and let omp expansion |
69 | machinery do its job. | |
48e1416a | 70 | |
28c92cbb | 71 | The most of the complexity is in bringing the code into shape expected |
72 | by the omp expanders: | |
75a70cf9 | 73 | -- for GIMPLE_OMP_FOR, ensuring that the loop has only one induction |
74 | variable and that the exit test is at the start of the loop body | |
75 | -- for GIMPLE_OMP_PARALLEL, replacing the references to local addressable | |
28c92cbb | 76 | variables by accesses through pointers, and breaking up ssa chains |
77 | by storing the values incoming to the parallelized loop to a structure | |
78 | passed to the new function as an argument (something similar is done | |
79 | in omp gimplification, unfortunately only a small part of the code | |
80 | can be shared). | |
81 | ||
82 | TODO: | |
83 | -- if there are several parallelizable loops in a function, it may be | |
84 | possible to generate the threads just once (using synchronization to | |
85 | ensure that cross-loop dependences are obeyed). | |
0773b627 | 86 | -- handling of common reduction patterns for outer loops. |
87 | ||
88 | More info can also be found at http://gcc.gnu.org/wiki/AutoParInGCC */ | |
48e1416a | 89 | /* |
cb7f680b | 90 | Reduction handling: |
f4a50267 | 91 | currently we use vect_force_simple_reduction() to detect reduction patterns. |
cb7f680b | 92 | The code transformation will be introduced by an example. |
48e1416a | 93 | |
94 | ||
cb7f680b | 95 | parloop |
96 | { | |
97 | int sum=1; | |
98 | ||
848674d0 | 99 | for (i = 0; i < N; i++) |
cb7f680b | 100 | { |
101 | x[i] = i + 3; | |
102 | sum+=x[i]; | |
103 | } | |
104 | } | |
105 | ||
848674d0 | 106 | gimple-like code: |
cb7f680b | 107 | header_bb: |
108 | ||
848674d0 | 109 | # sum_29 = PHI <sum_11(5), 1(3)> |
110 | # i_28 = PHI <i_12(5), 0(3)> | |
111 | D.1795_8 = i_28 + 3; | |
112 | x[i_28] = D.1795_8; | |
113 | sum_11 = D.1795_8 + sum_29; | |
114 | i_12 = i_28 + 1; | |
115 | if (N_6(D) > i_12) | |
116 | goto header_bb; | |
117 | ||
cb7f680b | 118 | |
119 | exit_bb: | |
120 | ||
848674d0 | 121 | # sum_21 = PHI <sum_11(4)> |
122 | printf (&"%d"[0], sum_21); | |
cb7f680b | 123 | |
124 | ||
125 | after reduction transformation (only relevant parts): | |
126 | ||
127 | parloop | |
128 | { | |
129 | ||
130 | .... | |
131 | ||
848674d0 | 132 | |
f0b5f617 | 133 | # Storing the initial value given by the user. # |
848674d0 | 134 | |
5bb62c99 | 135 | .paral_data_store.32.sum.27 = 1; |
48e1416a | 136 | |
137 | #pragma omp parallel num_threads(4) | |
cb7f680b | 138 | |
848674d0 | 139 | #pragma omp for schedule(static) |
5bb62c99 | 140 | |
141 | # The neutral element corresponding to the particular | |
142 | reduction's operation, e.g. 0 for PLUS_EXPR, | |
143 | 1 for MULT_EXPR, etc. replaces the user's initial value. # | |
144 | ||
145 | # sum.27_29 = PHI <sum.27_11, 0> | |
146 | ||
848674d0 | 147 | sum.27_11 = D.1827_8 + sum.27_29; |
5bb62c99 | 148 | |
75a70cf9 | 149 | GIMPLE_OMP_CONTINUE |
cb7f680b | 150 | |
848674d0 | 151 | # Adding this reduction phi is done at create_phi_for_local_result() # |
152 | # sum.27_56 = PHI <sum.27_11, 0> | |
75a70cf9 | 153 | GIMPLE_OMP_RETURN |
48e1416a | 154 | |
155 | # Creating the atomic operation is done at | |
848674d0 | 156 | create_call_for_reduction_1() # |
cb7f680b | 157 | |
848674d0 | 158 | #pragma omp atomic_load |
159 | D.1839_59 = *&.paral_data_load.33_51->reduction.23; | |
160 | D.1840_60 = sum.27_56 + D.1839_59; | |
161 | #pragma omp atomic_store (D.1840_60); | |
48e1416a | 162 | |
75a70cf9 | 163 | GIMPLE_OMP_RETURN |
48e1416a | 164 | |
848674d0 | 165 | # collecting the result after the join of the threads is done at |
166 | create_loads_for_reductions(). | |
5bb62c99 | 167 | The value computed by the threads is loaded from the |
168 | shared struct. # | |
169 | ||
48e1416a | 170 | |
848674d0 | 171 | .paral_data_load.33_52 = &.paral_data_store.32; |
5bb62c99 | 172 | sum_37 = .paral_data_load.33_52->sum.27; |
848674d0 | 173 | sum_43 = D.1795_41 + sum_37; |
174 | ||
175 | exit bb: | |
176 | # sum_21 = PHI <sum_43, sum_26> | |
177 | printf (&"%d"[0], sum_21); | |
178 | ||
179 | ... | |
180 | ||
cb7f680b | 181 | } |
182 | ||
183 | */ | |
184 | ||
28c92cbb | 185 | /* Minimal number of iterations of a loop that should be executed in each |
186 | thread. */ | |
b3687123 | 187 | #define MIN_PER_THREAD PARAM_VALUE (PARAM_PARLOOPS_MIN_PER_THREAD) |
28c92cbb | 188 | |
48e1416a | 189 | /* Element of the hashtable, representing a |
cb7f680b | 190 | reduction in the current loop. */ |
191 | struct reduction_info | |
192 | { | |
42acab1c | 193 | gimple *reduc_stmt; /* reduction statement. */ |
194 | gimple *reduc_phi; /* The phi node defining the reduction. */ | |
75a70cf9 | 195 | enum tree_code reduction_code;/* code for the reduction operation. */ |
71fa519d | 196 | unsigned reduc_version; /* SSA_NAME_VERSION of original reduc_phi |
197 | result. */ | |
1a91d914 | 198 | gphi *keep_res; /* The PHI_RESULT of this phi is the resulting value |
cb7f680b | 199 | of the reduction variable when existing the loop. */ |
5bb62c99 | 200 | tree initial_value; /* The initial value of the reduction var before entering the loop. */ |
cb7f680b | 201 | tree field; /* the name of the field in the parloop data structure intended for reduction. */ |
31c2b13b | 202 | tree reduc_addr; /* The address of the reduction variable for |
203 | openacc reductions. */ | |
cb7f680b | 204 | tree init; /* reduction initialization value. */ |
1a91d914 | 205 | gphi *new_phi; /* (helper field) Newly created phi node whose result |
cb7f680b | 206 | will be passed to the atomic operation. Represents |
207 | the local result each thread computed for the reduction | |
208 | operation. */ | |
209 | }; | |
210 | ||
d9dd21a8 | 211 | /* Reduction info hashtable helpers. */ |
cb7f680b | 212 | |
298e7f9a | 213 | struct reduction_hasher : free_ptr_hash <reduction_info> |
cb7f680b | 214 | { |
9969c043 | 215 | static inline hashval_t hash (const reduction_info *); |
216 | static inline bool equal (const reduction_info *, const reduction_info *); | |
d9dd21a8 | 217 | }; |
218 | ||
219 | /* Equality and hash functions for hashtab code. */ | |
cb7f680b | 220 | |
d9dd21a8 | 221 | inline bool |
9969c043 | 222 | reduction_hasher::equal (const reduction_info *a, const reduction_info *b) |
d9dd21a8 | 223 | { |
cb7f680b | 224 | return (a->reduc_phi == b->reduc_phi); |
225 | } | |
226 | ||
d9dd21a8 | 227 | inline hashval_t |
9969c043 | 228 | reduction_hasher::hash (const reduction_info *a) |
cb7f680b | 229 | { |
71fa519d | 230 | return a->reduc_version; |
cb7f680b | 231 | } |
232 | ||
c1f445d2 | 233 | typedef hash_table<reduction_hasher> reduction_info_table_type; |
d9dd21a8 | 234 | |
235 | ||
cb7f680b | 236 | static struct reduction_info * |
42acab1c | 237 | reduction_phi (reduction_info_table_type *reduction_list, gimple *phi) |
cb7f680b | 238 | { |
239 | struct reduction_info tmpred, *red; | |
240 | ||
9a78b979 | 241 | if (reduction_list->is_empty () || phi == NULL) |
cb7f680b | 242 | return NULL; |
243 | ||
eac984fd | 244 | if (gimple_uid (phi) == (unsigned int)-1 |
245 | || gimple_uid (phi) == 0) | |
246 | return NULL; | |
247 | ||
cb7f680b | 248 | tmpred.reduc_phi = phi; |
71fa519d | 249 | tmpred.reduc_version = gimple_uid (phi); |
c1f445d2 | 250 | red = reduction_list->find (&tmpred); |
eac984fd | 251 | gcc_assert (red == NULL || red->reduc_phi == phi); |
cb7f680b | 252 | |
253 | return red; | |
254 | } | |
255 | ||
28c92cbb | 256 | /* Element of hashtable of names to copy. */ |
257 | ||
258 | struct name_to_copy_elt | |
259 | { | |
260 | unsigned version; /* The version of the name to copy. */ | |
261 | tree new_name; /* The new name used in the copy. */ | |
262 | tree field; /* The field of the structure used to pass the | |
263 | value. */ | |
264 | }; | |
265 | ||
d9dd21a8 | 266 | /* Name copies hashtable helpers. */ |
28c92cbb | 267 | |
298e7f9a | 268 | struct name_to_copy_hasher : free_ptr_hash <name_to_copy_elt> |
28c92cbb | 269 | { |
9969c043 | 270 | static inline hashval_t hash (const name_to_copy_elt *); |
271 | static inline bool equal (const name_to_copy_elt *, const name_to_copy_elt *); | |
d9dd21a8 | 272 | }; |
273 | ||
274 | /* Equality and hash functions for hashtab code. */ | |
28c92cbb | 275 | |
d9dd21a8 | 276 | inline bool |
9969c043 | 277 | name_to_copy_hasher::equal (const name_to_copy_elt *a, const name_to_copy_elt *b) |
d9dd21a8 | 278 | { |
28c92cbb | 279 | return a->version == b->version; |
280 | } | |
281 | ||
d9dd21a8 | 282 | inline hashval_t |
9969c043 | 283 | name_to_copy_hasher::hash (const name_to_copy_elt *a) |
28c92cbb | 284 | { |
28c92cbb | 285 | return (hashval_t) a->version; |
286 | } | |
287 | ||
c1f445d2 | 288 | typedef hash_table<name_to_copy_hasher> name_to_copy_table_type; |
d9dd21a8 | 289 | |
e01f9f1f | 290 | /* A transformation matrix, which is a self-contained ROWSIZE x COLSIZE |
291 | matrix. Rather than use floats, we simply keep a single DENOMINATOR that | |
292 | represents the denominator for every element in the matrix. */ | |
293 | typedef struct lambda_trans_matrix_s | |
294 | { | |
295 | lambda_matrix matrix; | |
296 | int rowsize; | |
297 | int colsize; | |
298 | int denominator; | |
299 | } *lambda_trans_matrix; | |
300 | #define LTM_MATRIX(T) ((T)->matrix) | |
301 | #define LTM_ROWSIZE(T) ((T)->rowsize) | |
302 | #define LTM_COLSIZE(T) ((T)->colsize) | |
303 | #define LTM_DENOMINATOR(T) ((T)->denominator) | |
304 | ||
305 | /* Allocate a new transformation matrix. */ | |
306 | ||
307 | static lambda_trans_matrix | |
308 | lambda_trans_matrix_new (int colsize, int rowsize, | |
309 | struct obstack * lambda_obstack) | |
310 | { | |
311 | lambda_trans_matrix ret; | |
312 | ||
313 | ret = (lambda_trans_matrix) | |
314 | obstack_alloc (lambda_obstack, sizeof (struct lambda_trans_matrix_s)); | |
315 | LTM_MATRIX (ret) = lambda_matrix_new (rowsize, colsize, lambda_obstack); | |
316 | LTM_ROWSIZE (ret) = rowsize; | |
317 | LTM_COLSIZE (ret) = colsize; | |
318 | LTM_DENOMINATOR (ret) = 1; | |
319 | return ret; | |
320 | } | |
321 | ||
322 | /* Multiply a vector VEC by a matrix MAT. | |
323 | MAT is an M*N matrix, and VEC is a vector with length N. The result | |
324 | is stored in DEST which must be a vector of length M. */ | |
325 | ||
326 | static void | |
327 | lambda_matrix_vector_mult (lambda_matrix matrix, int m, int n, | |
328 | lambda_vector vec, lambda_vector dest) | |
329 | { | |
330 | int i, j; | |
331 | ||
332 | lambda_vector_clear (dest, m); | |
333 | for (i = 0; i < m; i++) | |
334 | for (j = 0; j < n; j++) | |
335 | dest[i] += matrix[i][j] * vec[j]; | |
336 | } | |
337 | ||
338 | /* Return true if TRANS is a legal transformation matrix that respects | |
339 | the dependence vectors in DISTS and DIRS. The conservative answer | |
340 | is false. | |
341 | ||
342 | "Wolfe proves that a unimodular transformation represented by the | |
343 | matrix T is legal when applied to a loop nest with a set of | |
344 | lexicographically non-negative distance vectors RDG if and only if | |
345 | for each vector d in RDG, (T.d >= 0) is lexicographically positive. | |
346 | i.e.: if and only if it transforms the lexicographically positive | |
347 | distance vectors to lexicographically positive vectors. Note that | |
348 | a unimodular matrix must transform the zero vector (and only it) to | |
349 | the zero vector." S.Muchnick. */ | |
350 | ||
351 | static bool | |
352 | lambda_transform_legal_p (lambda_trans_matrix trans, | |
353 | int nb_loops, | |
f1f41a6c | 354 | vec<ddr_p> dependence_relations) |
e01f9f1f | 355 | { |
356 | unsigned int i, j; | |
357 | lambda_vector distres; | |
358 | struct data_dependence_relation *ddr; | |
359 | ||
360 | gcc_assert (LTM_COLSIZE (trans) == nb_loops | |
361 | && LTM_ROWSIZE (trans) == nb_loops); | |
362 | ||
363 | /* When there are no dependences, the transformation is correct. */ | |
f1f41a6c | 364 | if (dependence_relations.length () == 0) |
e01f9f1f | 365 | return true; |
366 | ||
f1f41a6c | 367 | ddr = dependence_relations[0]; |
e01f9f1f | 368 | if (ddr == NULL) |
369 | return true; | |
370 | ||
371 | /* When there is an unknown relation in the dependence_relations, we | |
372 | know that it is no worth looking at this loop nest: give up. */ | |
373 | if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know) | |
374 | return false; | |
375 | ||
376 | distres = lambda_vector_new (nb_loops); | |
377 | ||
378 | /* For each distance vector in the dependence graph. */ | |
f1f41a6c | 379 | FOR_EACH_VEC_ELT (dependence_relations, i, ddr) |
e01f9f1f | 380 | { |
381 | /* Don't care about relations for which we know that there is no | |
382 | dependence, nor about read-read (aka. output-dependences): | |
383 | these data accesses can happen in any order. */ | |
384 | if (DDR_ARE_DEPENDENT (ddr) == chrec_known | |
385 | || (DR_IS_READ (DDR_A (ddr)) && DR_IS_READ (DDR_B (ddr)))) | |
386 | continue; | |
387 | ||
388 | /* Conservatively answer: "this transformation is not valid". */ | |
389 | if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know) | |
390 | return false; | |
391 | ||
392 | /* If the dependence could not be captured by a distance vector, | |
393 | conservatively answer that the transform is not valid. */ | |
394 | if (DDR_NUM_DIST_VECTS (ddr) == 0) | |
395 | return false; | |
396 | ||
397 | /* Compute trans.dist_vect */ | |
398 | for (j = 0; j < DDR_NUM_DIST_VECTS (ddr); j++) | |
399 | { | |
400 | lambda_matrix_vector_mult (LTM_MATRIX (trans), nb_loops, nb_loops, | |
401 | DDR_DIST_VECT (ddr, j), distres); | |
402 | ||
403 | if (!lambda_vector_lexico_pos (distres, nb_loops)) | |
404 | return false; | |
405 | } | |
406 | } | |
407 | return true; | |
408 | } | |
5fa90eea | 409 | |
410 | /* Data dependency analysis. Returns true if the iterations of LOOP | |
411 | are independent on each other (that is, if we can execute them | |
412 | in parallel). */ | |
28c92cbb | 413 | |
414 | static bool | |
2e966e2a | 415 | loop_parallel_p (class loop *loop, struct obstack * parloop_obstack) |
28c92cbb | 416 | { |
f1f41a6c | 417 | vec<ddr_p> dependence_relations; |
418 | vec<data_reference_p> datarefs; | |
28c92cbb | 419 | lambda_trans_matrix trans; |
420 | bool ret = false; | |
28c92cbb | 421 | |
422 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
b0fb253a | 423 | { |
424 | fprintf (dump_file, "Considering loop %d\n", loop->num); | |
425 | if (!loop->inner) | |
426 | fprintf (dump_file, "loop is innermost\n"); | |
48e1416a | 427 | else |
b0fb253a | 428 | fprintf (dump_file, "loop NOT innermost\n"); |
429 | } | |
28c92cbb | 430 | |
28c92cbb | 431 | /* Check for problems with dependences. If the loop can be reversed, |
432 | the iterations are independent. */ | |
4997014d | 433 | auto_vec<loop_p, 3> loop_nest; |
f1f41a6c | 434 | datarefs.create (10); |
e85cf4e5 | 435 | dependence_relations.create (100); |
713f1f14 | 436 | if (! compute_data_dependences_for_loop (loop, true, &loop_nest, &datarefs, |
437 | &dependence_relations)) | |
438 | { | |
439 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
440 | fprintf (dump_file, " FAILED: cannot analyze data dependencies\n"); | |
441 | ret = false; | |
442 | goto end; | |
443 | } | |
28c92cbb | 444 | if (dump_file && (dump_flags & TDF_DETAILS)) |
445 | dump_data_dependence_relations (dump_file, dependence_relations); | |
446 | ||
1e33ad50 | 447 | trans = lambda_trans_matrix_new (1, 1, parloop_obstack); |
28c92cbb | 448 | LTM_MATRIX (trans)[0][0] = -1; |
449 | ||
450 | if (lambda_transform_legal_p (trans, 1, dependence_relations)) | |
451 | { | |
452 | ret = true; | |
453 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
454 | fprintf (dump_file, " SUCCESS: may be parallelized\n"); | |
455 | } | |
456 | else if (dump_file && (dump_flags & TDF_DETAILS)) | |
cb7f680b | 457 | fprintf (dump_file, |
458 | " FAILED: data dependencies exist across iterations\n"); | |
28c92cbb | 459 | |
713f1f14 | 460 | end: |
28c92cbb | 461 | free_dependence_relations (dependence_relations); |
462 | free_data_refs (datarefs); | |
463 | ||
464 | return ret; | |
465 | } | |
466 | ||
d4fcfd16 | 467 | /* Return true when LOOP contains basic blocks marked with the |
468 | BB_IRREDUCIBLE_LOOP flag. */ | |
469 | ||
470 | static inline bool | |
2e966e2a | 471 | loop_has_blocks_with_irreducible_flag (class loop *loop) |
d4fcfd16 | 472 | { |
473 | unsigned i; | |
474 | basic_block *bbs = get_loop_body_in_dom_order (loop); | |
475 | bool res = true; | |
476 | ||
477 | for (i = 0; i < loop->num_nodes; i++) | |
478 | if (bbs[i]->flags & BB_IRREDUCIBLE_LOOP) | |
479 | goto end; | |
480 | ||
481 | res = false; | |
482 | end: | |
483 | free (bbs); | |
484 | return res; | |
485 | } | |
486 | ||
c1fb5b25 | 487 | /* Assigns the address of OBJ in TYPE to an ssa name, and returns this name. |
e06f9c34 | 488 | The assignment statement is placed on edge ENTRY. DECL_ADDRESS maps decls |
c1fb5b25 | 489 | to their addresses that can be reused. The address of OBJ is known to |
ad57283e | 490 | be invariant in the whole function. Other needed statements are placed |
491 | right before GSI. */ | |
28c92cbb | 492 | |
493 | static tree | |
d9dd21a8 | 494 | take_address_of (tree obj, tree type, edge entry, |
c1f445d2 | 495 | int_tree_htab_type *decl_address, gimple_stmt_iterator *gsi) |
28c92cbb | 496 | { |
c1fb5b25 | 497 | int uid; |
03d37e4e | 498 | tree *var_p, name, addr; |
1a91d914 | 499 | gassign *stmt; |
75a70cf9 | 500 | gimple_seq stmts; |
28c92cbb | 501 | |
c1fb5b25 | 502 | /* Since the address of OBJ is invariant, the trees may be shared. |
503 | Avoid rewriting unrelated parts of the code. */ | |
504 | obj = unshare_expr (obj); | |
505 | for (var_p = &obj; | |
506 | handled_component_p (*var_p); | |
507 | var_p = &TREE_OPERAND (*var_p, 0)) | |
508 | continue; | |
c1fb5b25 | 509 | |
64ade643 | 510 | /* Canonicalize the access to base on a MEM_REF. */ |
511 | if (DECL_P (*var_p)) | |
512 | *var_p = build_simple_mem_ref (build_fold_addr_expr (*var_p)); | |
513 | ||
514 | /* Assign a canonical SSA name to the address of the base decl used | |
515 | in the address and share it for all accesses and addresses based | |
516 | on it. */ | |
517 | uid = DECL_UID (TREE_OPERAND (TREE_OPERAND (*var_p, 0), 0)); | |
2933f7af | 518 | int_tree_map elt; |
519 | elt.uid = uid; | |
520 | int_tree_map *slot = decl_address->find_slot (elt, INSERT); | |
521 | if (!slot->to) | |
28c92cbb | 522 | { |
ad57283e | 523 | if (gsi == NULL) |
524 | return NULL; | |
64ade643 | 525 | addr = TREE_OPERAND (*var_p, 0); |
a7c17572 | 526 | const char *obj_name |
527 | = get_name (TREE_OPERAND (TREE_OPERAND (*var_p, 0), 0)); | |
528 | if (obj_name) | |
529 | name = make_temp_ssa_name (TREE_TYPE (addr), NULL, obj_name); | |
530 | else | |
f9e245b2 | 531 | name = make_ssa_name (TREE_TYPE (addr)); |
03d37e4e | 532 | stmt = gimple_build_assign (name, addr); |
75a70cf9 | 533 | gsi_insert_on_edge_immediate (entry, stmt); |
28c92cbb | 534 | |
2933f7af | 535 | slot->uid = uid; |
536 | slot->to = name; | |
28c92cbb | 537 | } |
c1fb5b25 | 538 | else |
2933f7af | 539 | name = slot->to; |
28c92cbb | 540 | |
64ade643 | 541 | /* Express the address in terms of the canonical SSA name. */ |
542 | TREE_OPERAND (*var_p, 0) = name; | |
ad57283e | 543 | if (gsi == NULL) |
544 | return build_fold_addr_expr_with_type (obj, type); | |
545 | ||
0e49e441 | 546 | name = force_gimple_operand (build_addr (obj), |
64ade643 | 547 | &stmts, true, NULL_TREE); |
548 | if (!gimple_seq_empty_p (stmts)) | |
ad57283e | 549 | gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT); |
28c92cbb | 550 | |
64ade643 | 551 | if (!useless_type_conversion_p (type, TREE_TYPE (name))) |
c1fb5b25 | 552 | { |
75a70cf9 | 553 | name = force_gimple_operand (fold_convert (type, name), &stmts, true, |
c1fb5b25 | 554 | NULL_TREE); |
75a70cf9 | 555 | if (!gimple_seq_empty_p (stmts)) |
ad57283e | 556 | gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT); |
c1fb5b25 | 557 | } |
28c92cbb | 558 | |
559 | return name; | |
560 | } | |
561 | ||
95f4166a | 562 | static tree |
42acab1c | 563 | reduc_stmt_res (gimple *stmt) |
95f4166a | 564 | { |
565 | return (gimple_code (stmt) == GIMPLE_PHI | |
566 | ? gimple_phi_result (stmt) | |
567 | : gimple_assign_lhs (stmt)); | |
568 | } | |
569 | ||
cb7f680b | 570 | /* Callback for htab_traverse. Create the initialization statement |
48e1416a | 571 | for reduction described in SLOT, and place it at the preheader of |
cb7f680b | 572 | the loop described in DATA. */ |
573 | ||
d9dd21a8 | 574 | int |
2e966e2a | 575 | initialize_reductions (reduction_info **slot, class loop *loop) |
cb7f680b | 576 | { |
df67b98c | 577 | tree init; |
578 | tree type, arg; | |
cb7f680b | 579 | edge e; |
580 | ||
d9dd21a8 | 581 | struct reduction_info *const reduc = *slot; |
cb7f680b | 582 | |
48e1416a | 583 | /* Create initialization in preheader: |
cb7f680b | 584 | reduction_variable = initialization value of reduction. */ |
585 | ||
48e1416a | 586 | /* In the phi node at the header, replace the argument coming |
cb7f680b | 587 | from the preheader with the reduction initialization value. */ |
588 | ||
df67b98c | 589 | /* Initialize the reduction. */ |
cb7f680b | 590 | type = TREE_TYPE (PHI_RESULT (reduc->reduc_phi)); |
df67b98c | 591 | init = omp_reduction_init_op (gimple_location (reduc->reduc_stmt), |
592 | reduc->reduction_code, type); | |
cb7f680b | 593 | reduc->init = init; |
594 | ||
48e1416a | 595 | /* Replace the argument representing the initialization value |
596 | with the initialization value for the reduction (neutral | |
597 | element for the particular operation, e.g. 0 for PLUS_EXPR, | |
598 | 1 for MULT_EXPR, etc). | |
599 | Keep the old value in a new variable "reduction_initial", | |
600 | that will be taken in consideration after the parallel | |
848674d0 | 601 | computing is done. */ |
cb7f680b | 602 | |
603 | e = loop_preheader_edge (loop); | |
604 | arg = PHI_ARG_DEF_FROM_EDGE (reduc->reduc_phi, e); | |
605 | /* Create new variable to hold the initial value. */ | |
cb7f680b | 606 | |
cb7f680b | 607 | SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE |
848674d0 | 608 | (reduc->reduc_phi, loop_preheader_edge (loop)), init); |
5bb62c99 | 609 | reduc->initial_value = arg; |
cb7f680b | 610 | return 1; |
611 | } | |
28c92cbb | 612 | |
613 | struct elv_data | |
614 | { | |
75a70cf9 | 615 | struct walk_stmt_info info; |
e06f9c34 | 616 | edge entry; |
c1f445d2 | 617 | int_tree_htab_type *decl_address; |
ad57283e | 618 | gimple_stmt_iterator *gsi; |
28c92cbb | 619 | bool changed; |
ad57283e | 620 | bool reset; |
28c92cbb | 621 | }; |
622 | ||
e06f9c34 | 623 | /* Eliminates references to local variables in *TP out of the single |
624 | entry single exit region starting at DTA->ENTRY. | |
625 | DECL_ADDRESS contains addresses of the references that had their | |
626 | address taken already. If the expression is changed, CHANGED is | |
627 | set to true. Callback for walk_tree. */ | |
cb7f680b | 628 | |
28c92cbb | 629 | static tree |
c1fb5b25 | 630 | eliminate_local_variables_1 (tree *tp, int *walk_subtrees, void *data) |
28c92cbb | 631 | { |
45ba1503 | 632 | struct elv_data *const dta = (struct elv_data *) data; |
c1fb5b25 | 633 | tree t = *tp, var, addr, addr_type, type, obj; |
28c92cbb | 634 | |
635 | if (DECL_P (t)) | |
636 | { | |
637 | *walk_subtrees = 0; | |
638 | ||
639 | if (!SSA_VAR_P (t) || DECL_EXTERNAL (t)) | |
640 | return NULL_TREE; | |
641 | ||
642 | type = TREE_TYPE (t); | |
643 | addr_type = build_pointer_type (type); | |
ad57283e | 644 | addr = take_address_of (t, addr_type, dta->entry, dta->decl_address, |
645 | dta->gsi); | |
646 | if (dta->gsi == NULL && addr == NULL_TREE) | |
647 | { | |
648 | dta->reset = true; | |
649 | return NULL_TREE; | |
650 | } | |
651 | ||
182cf5a9 | 652 | *tp = build_simple_mem_ref (addr); |
28c92cbb | 653 | |
654 | dta->changed = true; | |
655 | return NULL_TREE; | |
656 | } | |
657 | ||
658 | if (TREE_CODE (t) == ADDR_EXPR) | |
659 | { | |
c1fb5b25 | 660 | /* ADDR_EXPR may appear in two contexts: |
661 | -- as a gimple operand, when the address taken is a function invariant | |
662 | -- as gimple rhs, when the resulting address in not a function | |
663 | invariant | |
664 | We do not need to do anything special in the latter case (the base of | |
665 | the memory reference whose address is taken may be replaced in the | |
666 | DECL_P case). The former case is more complicated, as we need to | |
667 | ensure that the new address is still a gimple operand. Thus, it | |
668 | is not sufficient to replace just the base of the memory reference -- | |
669 | we need to move the whole computation of the address out of the | |
670 | loop. */ | |
671 | if (!is_gimple_val (t)) | |
28c92cbb | 672 | return NULL_TREE; |
673 | ||
674 | *walk_subtrees = 0; | |
c1fb5b25 | 675 | obj = TREE_OPERAND (t, 0); |
676 | var = get_base_address (obj); | |
677 | if (!var || !SSA_VAR_P (var) || DECL_EXTERNAL (var)) | |
28c92cbb | 678 | return NULL_TREE; |
679 | ||
680 | addr_type = TREE_TYPE (t); | |
ad57283e | 681 | addr = take_address_of (obj, addr_type, dta->entry, dta->decl_address, |
682 | dta->gsi); | |
683 | if (dta->gsi == NULL && addr == NULL_TREE) | |
684 | { | |
685 | dta->reset = true; | |
686 | return NULL_TREE; | |
687 | } | |
28c92cbb | 688 | *tp = addr; |
689 | ||
690 | dta->changed = true; | |
691 | return NULL_TREE; | |
692 | } | |
693 | ||
75a70cf9 | 694 | if (!EXPR_P (t)) |
28c92cbb | 695 | *walk_subtrees = 0; |
696 | ||
697 | return NULL_TREE; | |
698 | } | |
699 | ||
ad57283e | 700 | /* Moves the references to local variables in STMT at *GSI out of the single |
e06f9c34 | 701 | entry single exit region starting at ENTRY. DECL_ADDRESS contains |
702 | addresses of the references that had their address taken | |
703 | already. */ | |
28c92cbb | 704 | |
705 | static void | |
ad57283e | 706 | eliminate_local_variables_stmt (edge entry, gimple_stmt_iterator *gsi, |
c1f445d2 | 707 | int_tree_htab_type *decl_address) |
28c92cbb | 708 | { |
709 | struct elv_data dta; | |
42acab1c | 710 | gimple *stmt = gsi_stmt (*gsi); |
28c92cbb | 711 | |
75a70cf9 | 712 | memset (&dta.info, '\0', sizeof (dta.info)); |
e06f9c34 | 713 | dta.entry = entry; |
28c92cbb | 714 | dta.decl_address = decl_address; |
715 | dta.changed = false; | |
ad57283e | 716 | dta.reset = false; |
28c92cbb | 717 | |
9845d120 | 718 | if (gimple_debug_bind_p (stmt)) |
ad57283e | 719 | { |
720 | dta.gsi = NULL; | |
721 | walk_tree (gimple_debug_bind_get_value_ptr (stmt), | |
722 | eliminate_local_variables_1, &dta.info, NULL); | |
723 | if (dta.reset) | |
724 | { | |
725 | gimple_debug_bind_reset_value (stmt); | |
726 | dta.changed = true; | |
727 | } | |
728 | } | |
a7c17572 | 729 | else if (gimple_clobber_p (stmt)) |
730 | { | |
a4838119 | 731 | unlink_stmt_vdef (stmt); |
a7c17572 | 732 | stmt = gimple_build_nop (); |
733 | gsi_replace (gsi, stmt, false); | |
734 | dta.changed = true; | |
735 | } | |
9845d120 | 736 | else |
ad57283e | 737 | { |
738 | dta.gsi = gsi; | |
739 | walk_gimple_op (stmt, eliminate_local_variables_1, &dta.info); | |
740 | } | |
28c92cbb | 741 | |
742 | if (dta.changed) | |
743 | update_stmt (stmt); | |
744 | } | |
745 | ||
e06f9c34 | 746 | /* Eliminates the references to local variables from the single entry |
747 | single exit region between the ENTRY and EXIT edges. | |
48e1416a | 748 | |
cb7f680b | 749 | This includes: |
48e1416a | 750 | 1) Taking address of a local variable -- these are moved out of the |
751 | region (and temporary variable is created to hold the address if | |
cb7f680b | 752 | necessary). |
e06f9c34 | 753 | |
28c92cbb | 754 | 2) Dereferencing a local variable -- these are replaced with indirect |
cb7f680b | 755 | references. */ |
28c92cbb | 756 | |
757 | static void | |
e06f9c34 | 758 | eliminate_local_variables (edge entry, edge exit) |
28c92cbb | 759 | { |
e06f9c34 | 760 | basic_block bb; |
4997014d | 761 | auto_vec<basic_block, 3> body; |
28c92cbb | 762 | unsigned i; |
75a70cf9 | 763 | gimple_stmt_iterator gsi; |
ad57283e | 764 | bool has_debug_stmt = false; |
c1f445d2 | 765 | int_tree_htab_type decl_address (10); |
e06f9c34 | 766 | basic_block entry_bb = entry->src; |
767 | basic_block exit_bb = exit->dest; | |
28c92cbb | 768 | |
e06f9c34 | 769 | gather_blocks_in_sese_region (entry_bb, exit_bb, &body); |
28c92cbb | 770 | |
f1f41a6c | 771 | FOR_EACH_VEC_ELT (body, i, bb) |
e06f9c34 | 772 | if (bb != entry_bb && bb != exit_bb) |
d3831f71 | 773 | { |
774 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
775 | if (is_gimple_debug (gsi_stmt (gsi))) | |
776 | { | |
777 | if (gimple_debug_bind_p (gsi_stmt (gsi))) | |
778 | has_debug_stmt = true; | |
779 | } | |
780 | else | |
781 | eliminate_local_variables_stmt (entry, &gsi, &decl_address); | |
782 | } | |
ad57283e | 783 | |
784 | if (has_debug_stmt) | |
f1f41a6c | 785 | FOR_EACH_VEC_ELT (body, i, bb) |
ad57283e | 786 | if (bb != entry_bb && bb != exit_bb) |
787 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
788 | if (gimple_debug_bind_p (gsi_stmt (gsi))) | |
c1f445d2 | 789 | eliminate_local_variables_stmt (entry, &gsi, &decl_address); |
e06f9c34 | 790 | } |
791 | ||
792 | /* Returns true if expression EXPR is not defined between ENTRY and | |
793 | EXIT, i.e. if all its operands are defined outside of the region. */ | |
794 | ||
795 | static bool | |
796 | expr_invariant_in_region_p (edge entry, edge exit, tree expr) | |
797 | { | |
798 | basic_block entry_bb = entry->src; | |
799 | basic_block exit_bb = exit->dest; | |
800 | basic_block def_bb; | |
e06f9c34 | 801 | |
802 | if (is_gimple_min_invariant (expr)) | |
803 | return true; | |
804 | ||
805 | if (TREE_CODE (expr) == SSA_NAME) | |
806 | { | |
75a70cf9 | 807 | def_bb = gimple_bb (SSA_NAME_DEF_STMT (expr)); |
e06f9c34 | 808 | if (def_bb |
809 | && dominated_by_p (CDI_DOMINATORS, def_bb, entry_bb) | |
810 | && !dominated_by_p (CDI_DOMINATORS, def_bb, exit_bb)) | |
811 | return false; | |
812 | ||
813 | return true; | |
814 | } | |
815 | ||
75a70cf9 | 816 | return false; |
28c92cbb | 817 | } |
818 | ||
819 | /* If COPY_NAME_P is true, creates and returns a duplicate of NAME. | |
820 | The copies are stored to NAME_COPIES, if NAME was already duplicated, | |
821 | its duplicate stored in NAME_COPIES is returned. | |
48e1416a | 822 | |
28c92cbb | 823 | Regardless of COPY_NAME_P, the decl used as a base of the ssa name is also |
824 | duplicated, storing the copies in DECL_COPIES. */ | |
825 | ||
826 | static tree | |
c1f445d2 | 827 | separate_decls_in_region_name (tree name, name_to_copy_table_type *name_copies, |
828 | int_tree_htab_type *decl_copies, | |
829 | bool copy_name_p) | |
28c92cbb | 830 | { |
831 | tree copy, var, var_copy; | |
832 | unsigned idx, uid, nuid; | |
2933f7af | 833 | struct int_tree_map ielt; |
28c92cbb | 834 | struct name_to_copy_elt elt, *nelt; |
d9dd21a8 | 835 | name_to_copy_elt **slot; |
2933f7af | 836 | int_tree_map *dslot; |
28c92cbb | 837 | |
838 | if (TREE_CODE (name) != SSA_NAME) | |
839 | return name; | |
840 | ||
841 | idx = SSA_NAME_VERSION (name); | |
842 | elt.version = idx; | |
c1f445d2 | 843 | slot = name_copies->find_slot_with_hash (&elt, idx, |
844 | copy_name_p ? INSERT : NO_INSERT); | |
28c92cbb | 845 | if (slot && *slot) |
d9dd21a8 | 846 | return (*slot)->new_name; |
28c92cbb | 847 | |
ec11736b | 848 | if (copy_name_p) |
849 | { | |
850 | copy = duplicate_ssa_name (name, NULL); | |
851 | nelt = XNEW (struct name_to_copy_elt); | |
852 | nelt->version = idx; | |
853 | nelt->new_name = copy; | |
854 | nelt->field = NULL_TREE; | |
855 | *slot = nelt; | |
856 | } | |
857 | else | |
858 | { | |
859 | gcc_assert (!slot); | |
860 | copy = name; | |
861 | } | |
862 | ||
28c92cbb | 863 | var = SSA_NAME_VAR (name); |
ec11736b | 864 | if (!var) |
865 | return copy; | |
866 | ||
28c92cbb | 867 | uid = DECL_UID (var); |
868 | ielt.uid = uid; | |
2933f7af | 869 | dslot = decl_copies->find_slot_with_hash (ielt, uid, INSERT); |
870 | if (!dslot->to) | |
28c92cbb | 871 | { |
872 | var_copy = create_tmp_var (TREE_TYPE (var), get_name (var)); | |
55ed4df6 | 873 | DECL_GIMPLE_REG_P (var_copy) = DECL_GIMPLE_REG_P (var); |
2933f7af | 874 | dslot->uid = uid; |
875 | dslot->to = var_copy; | |
28c92cbb | 876 | |
877 | /* Ensure that when we meet this decl next time, we won't duplicate | |
cb7f680b | 878 | it again. */ |
28c92cbb | 879 | nuid = DECL_UID (var_copy); |
880 | ielt.uid = nuid; | |
2933f7af | 881 | dslot = decl_copies->find_slot_with_hash (ielt, nuid, INSERT); |
882 | gcc_assert (!dslot->to); | |
883 | dslot->uid = nuid; | |
884 | dslot->to = var_copy; | |
28c92cbb | 885 | } |
886 | else | |
2933f7af | 887 | var_copy = dslot->to; |
28c92cbb | 888 | |
3b652cc1 | 889 | replace_ssa_name_symbol (copy, var_copy); |
28c92cbb | 890 | return copy; |
891 | } | |
892 | ||
e06f9c34 | 893 | /* Finds the ssa names used in STMT that are defined outside the |
894 | region between ENTRY and EXIT and replaces such ssa names with | |
895 | their duplicates. The duplicates are stored to NAME_COPIES. Base | |
896 | decls of all ssa names used in STMT (including those defined in | |
897 | LOOP) are replaced with the new temporary variables; the | |
898 | replacement decls are stored in DECL_COPIES. */ | |
28c92cbb | 899 | |
900 | static void | |
42acab1c | 901 | separate_decls_in_region_stmt (edge entry, edge exit, gimple *stmt, |
c1f445d2 | 902 | name_to_copy_table_type *name_copies, |
903 | int_tree_htab_type *decl_copies) | |
28c92cbb | 904 | { |
905 | use_operand_p use; | |
906 | def_operand_p def; | |
907 | ssa_op_iter oi; | |
908 | tree name, copy; | |
909 | bool copy_name_p; | |
910 | ||
28c92cbb | 911 | FOR_EACH_PHI_OR_STMT_DEF (def, stmt, oi, SSA_OP_DEF) |
cb7f680b | 912 | { |
913 | name = DEF_FROM_PTR (def); | |
914 | gcc_assert (TREE_CODE (name) == SSA_NAME); | |
e06f9c34 | 915 | copy = separate_decls_in_region_name (name, name_copies, decl_copies, |
916 | false); | |
cb7f680b | 917 | gcc_assert (copy == name); |
918 | } | |
28c92cbb | 919 | |
920 | FOR_EACH_PHI_OR_STMT_USE (use, stmt, oi, SSA_OP_USE) | |
cb7f680b | 921 | { |
922 | name = USE_FROM_PTR (use); | |
923 | if (TREE_CODE (name) != SSA_NAME) | |
924 | continue; | |
925 | ||
e06f9c34 | 926 | copy_name_p = expr_invariant_in_region_p (entry, exit, name); |
927 | copy = separate_decls_in_region_name (name, name_copies, decl_copies, | |
928 | copy_name_p); | |
cb7f680b | 929 | SET_USE (use, copy); |
930 | } | |
28c92cbb | 931 | } |
932 | ||
9845d120 | 933 | /* Finds the ssa names used in STMT that are defined outside the |
934 | region between ENTRY and EXIT and replaces such ssa names with | |
935 | their duplicates. The duplicates are stored to NAME_COPIES. Base | |
936 | decls of all ssa names used in STMT (including those defined in | |
937 | LOOP) are replaced with the new temporary variables; the | |
938 | replacement decls are stored in DECL_COPIES. */ | |
939 | ||
940 | static bool | |
42acab1c | 941 | separate_decls_in_region_debug (gimple *stmt, |
c1f445d2 | 942 | name_to_copy_table_type *name_copies, |
943 | int_tree_htab_type *decl_copies) | |
9845d120 | 944 | { |
945 | use_operand_p use; | |
946 | ssa_op_iter oi; | |
947 | tree var, name; | |
948 | struct int_tree_map ielt; | |
949 | struct name_to_copy_elt elt; | |
d9dd21a8 | 950 | name_to_copy_elt **slot; |
2933f7af | 951 | int_tree_map *dslot; |
9845d120 | 952 | |
841424cc | 953 | if (gimple_debug_bind_p (stmt)) |
954 | var = gimple_debug_bind_get_var (stmt); | |
955 | else if (gimple_debug_source_bind_p (stmt)) | |
956 | var = gimple_debug_source_bind_get_var (stmt); | |
957 | else | |
958 | return true; | |
eee873f6 | 959 | if (TREE_CODE (var) == DEBUG_EXPR_DECL || TREE_CODE (var) == LABEL_DECL) |
9e3c8673 | 960 | return true; |
9845d120 | 961 | gcc_assert (DECL_P (var) && SSA_VAR_P (var)); |
962 | ielt.uid = DECL_UID (var); | |
2933f7af | 963 | dslot = decl_copies->find_slot_with_hash (ielt, ielt.uid, NO_INSERT); |
9845d120 | 964 | if (!dslot) |
965 | return true; | |
841424cc | 966 | if (gimple_debug_bind_p (stmt)) |
2933f7af | 967 | gimple_debug_bind_set_var (stmt, dslot->to); |
841424cc | 968 | else if (gimple_debug_source_bind_p (stmt)) |
2933f7af | 969 | gimple_debug_source_bind_set_var (stmt, dslot->to); |
9845d120 | 970 | |
971 | FOR_EACH_PHI_OR_STMT_USE (use, stmt, oi, SSA_OP_USE) | |
972 | { | |
973 | name = USE_FROM_PTR (use); | |
974 | if (TREE_CODE (name) != SSA_NAME) | |
975 | continue; | |
976 | ||
977 | elt.version = SSA_NAME_VERSION (name); | |
c1f445d2 | 978 | slot = name_copies->find_slot_with_hash (&elt, elt.version, NO_INSERT); |
9845d120 | 979 | if (!slot) |
980 | { | |
981 | gimple_debug_bind_reset_value (stmt); | |
982 | update_stmt (stmt); | |
983 | break; | |
984 | } | |
985 | ||
d9dd21a8 | 986 | SET_USE (use, (*slot)->new_name); |
9845d120 | 987 | } |
988 | ||
989 | return false; | |
990 | } | |
991 | ||
848674d0 | 992 | /* Callback for htab_traverse. Adds a field corresponding to the reduction |
993 | specified in SLOT. The type is passed in DATA. */ | |
994 | ||
d9dd21a8 | 995 | int |
996 | add_field_for_reduction (reduction_info **slot, tree type) | |
cb7f680b | 997 | { |
48e1416a | 998 | |
d9dd21a8 | 999 | struct reduction_info *const red = *slot; |
95f4166a | 1000 | tree var = reduc_stmt_res (red->reduc_stmt); |
59c0ed80 | 1001 | tree field = build_decl (gimple_location (red->reduc_stmt), FIELD_DECL, |
1002 | SSA_NAME_IDENTIFIER (var), TREE_TYPE (var)); | |
848674d0 | 1003 | |
1004 | insert_field_into_struct (type, field); | |
1005 | ||
1006 | red->field = field; | |
1007 | ||
1008 | return 1; | |
1009 | } | |
cb7f680b | 1010 | |
28c92cbb | 1011 | /* Callback for htab_traverse. Adds a field corresponding to a ssa name |
48e1416a | 1012 | described in SLOT. The type is passed in DATA. */ |
28c92cbb | 1013 | |
d9dd21a8 | 1014 | int |
1015 | add_field_for_name (name_to_copy_elt **slot, tree type) | |
28c92cbb | 1016 | { |
d9dd21a8 | 1017 | struct name_to_copy_elt *const elt = *slot; |
28c92cbb | 1018 | tree name = ssa_name (elt->version); |
ec11736b | 1019 | tree field = build_decl (UNKNOWN_LOCATION, |
1020 | FIELD_DECL, SSA_NAME_IDENTIFIER (name), | |
1021 | TREE_TYPE (name)); | |
28c92cbb | 1022 | |
1023 | insert_field_into_struct (type, field); | |
1024 | elt->field = field; | |
cb7f680b | 1025 | |
28c92cbb | 1026 | return 1; |
1027 | } | |
1028 | ||
48e1416a | 1029 | /* Callback for htab_traverse. A local result is the intermediate result |
1030 | computed by a single | |
f0b5f617 | 1031 | thread, or the initial value in case no iteration was executed. |
48e1416a | 1032 | This function creates a phi node reflecting these values. |
1033 | The phi's result will be stored in NEW_PHI field of the | |
1034 | reduction's data structure. */ | |
cb7f680b | 1035 | |
d9dd21a8 | 1036 | int |
2e966e2a | 1037 | create_phi_for_local_result (reduction_info **slot, class loop *loop) |
cb7f680b | 1038 | { |
d9dd21a8 | 1039 | struct reduction_info *const reduc = *slot; |
cb7f680b | 1040 | edge e; |
1a91d914 | 1041 | gphi *new_phi; |
86a932e0 | 1042 | basic_block store_bb, continue_bb; |
cb7f680b | 1043 | tree local_res; |
be1e7283 | 1044 | location_t locus; |
cb7f680b | 1045 | |
48e1416a | 1046 | /* STORE_BB is the block where the phi |
1047 | should be stored. It is the destination of the loop exit. | |
75a70cf9 | 1048 | (Find the fallthru edge from GIMPLE_OMP_CONTINUE). */ |
86a932e0 | 1049 | continue_bb = single_pred (loop->latch); |
1050 | store_bb = FALLTHRU_EDGE (continue_bb)->dest; | |
cb7f680b | 1051 | |
1052 | /* STORE_BB has two predecessors. One coming from the loop | |
1053 | (the reduction's result is computed at the loop), | |
48e1416a | 1054 | and another coming from a block preceding the loop, |
1055 | when no iterations | |
1056 | are executed (the initial value should be taken). */ | |
86a932e0 | 1057 | if (EDGE_PRED (store_bb, 0) == FALLTHRU_EDGE (continue_bb)) |
cb7f680b | 1058 | e = EDGE_PRED (store_bb, 1); |
1059 | else | |
1060 | e = EDGE_PRED (store_bb, 0); | |
95f4166a | 1061 | tree lhs = reduc_stmt_res (reduc->reduc_stmt); |
1062 | local_res = copy_ssa_name (lhs); | |
efbcb6de | 1063 | locus = gimple_location (reduc->reduc_stmt); |
cb7f680b | 1064 | new_phi = create_phi_node (local_res, store_bb); |
60d535d2 | 1065 | add_phi_arg (new_phi, reduc->init, e, locus); |
86a932e0 | 1066 | add_phi_arg (new_phi, lhs, FALLTHRU_EDGE (continue_bb), locus); |
cb7f680b | 1067 | reduc->new_phi = new_phi; |
1068 | ||
1069 | return 1; | |
1070 | } | |
28c92cbb | 1071 | |
1072 | struct clsn_data | |
1073 | { | |
1074 | tree store; | |
1075 | tree load; | |
1076 | ||
1077 | basic_block store_bb; | |
1078 | basic_block load_bb; | |
1079 | }; | |
1080 | ||
cb7f680b | 1081 | /* Callback for htab_traverse. Create an atomic instruction for the |
48e1416a | 1082 | reduction described in SLOT. |
cb7f680b | 1083 | DATA annotates the place in memory the atomic operation relates to, |
1084 | and the basic block it needs to be generated in. */ | |
1085 | ||
d9dd21a8 | 1086 | int |
1087 | create_call_for_reduction_1 (reduction_info **slot, struct clsn_data *clsn_data) | |
cb7f680b | 1088 | { |
d9dd21a8 | 1089 | struct reduction_info *const reduc = *slot; |
75a70cf9 | 1090 | gimple_stmt_iterator gsi; |
cb7f680b | 1091 | tree type = TREE_TYPE (PHI_RESULT (reduc->reduc_phi)); |
cb7f680b | 1092 | tree load_struct; |
1093 | basic_block bb; | |
1094 | basic_block new_bb; | |
1095 | edge e; | |
f018d957 | 1096 | tree t, addr, ref, x; |
75a70cf9 | 1097 | tree tmp_load, name; |
42acab1c | 1098 | gimple *load; |
cb7f680b | 1099 | |
31c2b13b | 1100 | if (reduc->reduc_addr == NULL_TREE) |
1101 | { | |
1102 | load_struct = build_simple_mem_ref (clsn_data->load); | |
1103 | t = build3 (COMPONENT_REF, type, load_struct, reduc->field, NULL_TREE); | |
cb7f680b | 1104 | |
31c2b13b | 1105 | addr = build_addr (t); |
1106 | } | |
1107 | else | |
1108 | { | |
1109 | /* Set the address for the atomic store. */ | |
1110 | addr = reduc->reduc_addr; | |
1111 | ||
1112 | /* Remove the non-atomic store '*addr = sum'. */ | |
1113 | tree res = PHI_RESULT (reduc->keep_res); | |
1114 | use_operand_p use_p; | |
1115 | gimple *stmt; | |
1116 | bool single_use_p = single_imm_use (res, &use_p, &stmt); | |
1117 | gcc_assert (single_use_p); | |
1118 | replace_uses_by (gimple_vdef (stmt), | |
1119 | gimple_vuse (stmt)); | |
1120 | gimple_stmt_iterator gsi = gsi_for_stmt (stmt); | |
1121 | gsi_remove (&gsi, true); | |
1122 | } | |
cb7f680b | 1123 | |
1124 | /* Create phi node. */ | |
1125 | bb = clsn_data->load_bb; | |
1126 | ||
923635e7 | 1127 | gsi = gsi_last_bb (bb); |
1128 | e = split_block (bb, gsi_stmt (gsi)); | |
cb7f680b | 1129 | new_bb = e->dest; |
1130 | ||
f9e245b2 | 1131 | tmp_load = create_tmp_var (TREE_TYPE (TREE_TYPE (addr))); |
1132 | tmp_load = make_ssa_name (tmp_load); | |
7e5a76c8 | 1133 | load = gimple_build_omp_atomic_load (tmp_load, addr, |
1134 | OMP_MEMORY_ORDER_RELAXED); | |
cb7f680b | 1135 | SSA_NAME_DEF_STMT (tmp_load) = load; |
75a70cf9 | 1136 | gsi = gsi_start_bb (new_bb); |
1137 | gsi_insert_after (&gsi, load, GSI_NEW_STMT); | |
cb7f680b | 1138 | |
1139 | e = split_block (new_bb, load); | |
1140 | new_bb = e->dest; | |
75a70cf9 | 1141 | gsi = gsi_start_bb (new_bb); |
cb7f680b | 1142 | ref = tmp_load; |
75a70cf9 | 1143 | x = fold_build2 (reduc->reduction_code, |
1144 | TREE_TYPE (PHI_RESULT (reduc->new_phi)), ref, | |
1145 | PHI_RESULT (reduc->new_phi)); | |
cb7f680b | 1146 | |
75a70cf9 | 1147 | name = force_gimple_operand_gsi (&gsi, x, true, NULL_TREE, true, |
1148 | GSI_CONTINUE_LINKING); | |
cb7f680b | 1149 | |
7e5a76c8 | 1150 | gimple *store = gimple_build_omp_atomic_store (name, |
1151 | OMP_MEMORY_ORDER_RELAXED); | |
1152 | gsi_insert_after (&gsi, store, GSI_NEW_STMT); | |
cb7f680b | 1153 | return 1; |
1154 | } | |
1155 | ||
48e1416a | 1156 | /* Create the atomic operation at the join point of the threads. |
1157 | REDUCTION_LIST describes the reductions in the LOOP. | |
1158 | LD_ST_DATA describes the shared data structure where | |
cb7f680b | 1159 | shared data is stored in and loaded from. */ |
1160 | static void | |
2e966e2a | 1161 | create_call_for_reduction (class loop *loop, |
c1f445d2 | 1162 | reduction_info_table_type *reduction_list, |
cb7f680b | 1163 | struct clsn_data *ld_st_data) |
1164 | { | |
2e966e2a | 1165 | reduction_list->traverse <class loop *, create_phi_for_local_result> (loop); |
75a70cf9 | 1166 | /* Find the fallthru edge from GIMPLE_OMP_CONTINUE. */ |
86a932e0 | 1167 | basic_block continue_bb = single_pred (loop->latch); |
1168 | ld_st_data->load_bb = FALLTHRU_EDGE (continue_bb)->dest; | |
d9dd21a8 | 1169 | reduction_list |
c1f445d2 | 1170 | ->traverse <struct clsn_data *, create_call_for_reduction_1> (ld_st_data); |
cb7f680b | 1171 | } |
1172 | ||
5bb62c99 | 1173 | /* Callback for htab_traverse. Loads the final reduction value at the |
1174 | join point of all threads, and inserts it in the right place. */ | |
cb7f680b | 1175 | |
d9dd21a8 | 1176 | int |
1177 | create_loads_for_reductions (reduction_info **slot, struct clsn_data *clsn_data) | |
cb7f680b | 1178 | { |
d9dd21a8 | 1179 | struct reduction_info *const red = *slot; |
42acab1c | 1180 | gimple *stmt; |
75a70cf9 | 1181 | gimple_stmt_iterator gsi; |
95f4166a | 1182 | tree type = TREE_TYPE (reduc_stmt_res (red->reduc_stmt)); |
cb7f680b | 1183 | tree load_struct; |
5bb62c99 | 1184 | tree name; |
cb7f680b | 1185 | tree x; |
1186 | ||
de46ad2e | 1187 | /* If there's no exit phi, the result of the reduction is unused. */ |
1188 | if (red->keep_res == NULL) | |
1189 | return 1; | |
1190 | ||
75a70cf9 | 1191 | gsi = gsi_after_labels (clsn_data->load_bb); |
182cf5a9 | 1192 | load_struct = build_simple_mem_ref (clsn_data->load); |
cb7f680b | 1193 | load_struct = build3 (COMPONENT_REF, type, load_struct, red->field, |
1194 | NULL_TREE); | |
cb7f680b | 1195 | |
5bb62c99 | 1196 | x = load_struct; |
cb7f680b | 1197 | name = PHI_RESULT (red->keep_res); |
75a70cf9 | 1198 | stmt = gimple_build_assign (name, x); |
cb7f680b | 1199 | |
75a70cf9 | 1200 | gsi_insert_after (&gsi, stmt, GSI_NEW_STMT); |
cb7f680b | 1201 | |
75a70cf9 | 1202 | for (gsi = gsi_start_phis (gimple_bb (red->keep_res)); |
1203 | !gsi_end_p (gsi); gsi_next (&gsi)) | |
1204 | if (gsi_stmt (gsi) == red->keep_res) | |
1205 | { | |
1206 | remove_phi_node (&gsi, false); | |
1207 | return 1; | |
1208 | } | |
1209 | gcc_unreachable (); | |
cb7f680b | 1210 | } |
1211 | ||
48e1416a | 1212 | /* Load the reduction result that was stored in LD_ST_DATA. |
cb7f680b | 1213 | REDUCTION_LIST describes the list of reductions that the |
f0b5f617 | 1214 | loads should be generated for. */ |
cb7f680b | 1215 | static void |
c1f445d2 | 1216 | create_final_loads_for_reduction (reduction_info_table_type *reduction_list, |
cb7f680b | 1217 | struct clsn_data *ld_st_data) |
1218 | { | |
75a70cf9 | 1219 | gimple_stmt_iterator gsi; |
cb7f680b | 1220 | tree t; |
42acab1c | 1221 | gimple *stmt; |
cb7f680b | 1222 | |
75a70cf9 | 1223 | gsi = gsi_after_labels (ld_st_data->load_bb); |
cb7f680b | 1224 | t = build_fold_addr_expr (ld_st_data->store); |
75a70cf9 | 1225 | stmt = gimple_build_assign (ld_st_data->load, t); |
cb7f680b | 1226 | |
75a70cf9 | 1227 | gsi_insert_before (&gsi, stmt, GSI_NEW_STMT); |
cb7f680b | 1228 | |
d9dd21a8 | 1229 | reduction_list |
c1f445d2 | 1230 | ->traverse <struct clsn_data *, create_loads_for_reductions> (ld_st_data); |
cb7f680b | 1231 | |
1232 | } | |
1233 | ||
848674d0 | 1234 | /* Callback for htab_traverse. Store the neutral value for the |
1235 | particular reduction's operation, e.g. 0 for PLUS_EXPR, | |
1236 | 1 for MULT_EXPR, etc. into the reduction field. | |
48e1416a | 1237 | The reduction is specified in SLOT. The store information is |
1238 | passed in DATA. */ | |
848674d0 | 1239 | |
d9dd21a8 | 1240 | int |
1241 | create_stores_for_reduction (reduction_info **slot, struct clsn_data *clsn_data) | |
848674d0 | 1242 | { |
d9dd21a8 | 1243 | struct reduction_info *const red = *slot; |
75a70cf9 | 1244 | tree t; |
42acab1c | 1245 | gimple *stmt; |
75a70cf9 | 1246 | gimple_stmt_iterator gsi; |
95f4166a | 1247 | tree type = TREE_TYPE (reduc_stmt_res (red->reduc_stmt)); |
75a70cf9 | 1248 | |
1249 | gsi = gsi_last_bb (clsn_data->store_bb); | |
1250 | t = build3 (COMPONENT_REF, type, clsn_data->store, red->field, NULL_TREE); | |
1251 | stmt = gimple_build_assign (t, red->initial_value); | |
75a70cf9 | 1252 | gsi_insert_after (&gsi, stmt, GSI_NEW_STMT); |
848674d0 | 1253 | |
1254 | return 1; | |
1255 | } | |
1256 | ||
cb7f680b | 1257 | /* Callback for htab_traverse. Creates loads to a field of LOAD in LOAD_BB and |
1258 | store to a field of STORE in STORE_BB for the ssa name and its duplicate | |
1259 | specified in SLOT. */ | |
1260 | ||
d9dd21a8 | 1261 | int |
1262 | create_loads_and_stores_for_name (name_to_copy_elt **slot, | |
1263 | struct clsn_data *clsn_data) | |
28c92cbb | 1264 | { |
d9dd21a8 | 1265 | struct name_to_copy_elt *const elt = *slot; |
75a70cf9 | 1266 | tree t; |
42acab1c | 1267 | gimple *stmt; |
75a70cf9 | 1268 | gimple_stmt_iterator gsi; |
28c92cbb | 1269 | tree type = TREE_TYPE (elt->new_name); |
28c92cbb | 1270 | tree load_struct; |
1271 | ||
75a70cf9 | 1272 | gsi = gsi_last_bb (clsn_data->store_bb); |
1273 | t = build3 (COMPONENT_REF, type, clsn_data->store, elt->field, NULL_TREE); | |
1274 | stmt = gimple_build_assign (t, ssa_name (elt->version)); | |
75a70cf9 | 1275 | gsi_insert_after (&gsi, stmt, GSI_NEW_STMT); |
28c92cbb | 1276 | |
75a70cf9 | 1277 | gsi = gsi_last_bb (clsn_data->load_bb); |
182cf5a9 | 1278 | load_struct = build_simple_mem_ref (clsn_data->load); |
75a70cf9 | 1279 | t = build3 (COMPONENT_REF, type, load_struct, elt->field, NULL_TREE); |
1280 | stmt = gimple_build_assign (elt->new_name, t); | |
75a70cf9 | 1281 | gsi_insert_after (&gsi, stmt, GSI_NEW_STMT); |
28c92cbb | 1282 | |
1283 | return 1; | |
1284 | } | |
1285 | ||
1286 | /* Moves all the variables used in LOOP and defined outside of it (including | |
1287 | the initial values of loop phi nodes, and *PER_THREAD if it is a ssa | |
1288 | name) to a structure created for this purpose. The code | |
48e1416a | 1289 | |
28c92cbb | 1290 | while (1) |
1291 | { | |
1292 | use (a); | |
1293 | use (b); | |
1294 | } | |
1295 | ||
1296 | is transformed this way: | |
1297 | ||
1298 | bb0: | |
1299 | old.a = a; | |
1300 | old.b = b; | |
1301 | ||
1302 | bb1: | |
1303 | a' = new->a; | |
1304 | b' = new->b; | |
1305 | while (1) | |
1306 | { | |
1307 | use (a'); | |
1308 | use (b'); | |
1309 | } | |
1310 | ||
1311 | `old' is stored to *ARG_STRUCT and `new' is stored to NEW_ARG_STRUCT. The | |
1312 | pointer `new' is intentionally not initialized (the loop will be split to a | |
1313 | separate function later, and `new' will be initialized from its arguments). | |
cb7f680b | 1314 | LD_ST_DATA holds information about the shared data structure used to pass |
48e1416a | 1315 | information among the threads. It is initialized here, and |
1316 | gen_parallel_loop will pass it to create_call_for_reduction that | |
1317 | needs this information. REDUCTION_LIST describes the reductions | |
cb7f680b | 1318 | in LOOP. */ |
28c92cbb | 1319 | |
1320 | static void | |
d9dd21a8 | 1321 | separate_decls_in_region (edge entry, edge exit, |
c1f445d2 | 1322 | reduction_info_table_type *reduction_list, |
48e1416a | 1323 | tree *arg_struct, tree *new_arg_struct, |
e06f9c34 | 1324 | struct clsn_data *ld_st_data) |
cb7f680b | 1325 | |
28c92cbb | 1326 | { |
e06f9c34 | 1327 | basic_block bb1 = split_edge (entry); |
28c92cbb | 1328 | basic_block bb0 = single_pred (bb1); |
c1f445d2 | 1329 | name_to_copy_table_type name_copies (10); |
1330 | int_tree_htab_type decl_copies (10); | |
28c92cbb | 1331 | unsigned i; |
75a70cf9 | 1332 | tree type, type_name, nvar; |
1333 | gimple_stmt_iterator gsi; | |
28c92cbb | 1334 | struct clsn_data clsn_data; |
4997014d | 1335 | auto_vec<basic_block, 3> body; |
e06f9c34 | 1336 | basic_block bb; |
1337 | basic_block entry_bb = bb1; | |
1338 | basic_block exit_bb = exit->dest; | |
9845d120 | 1339 | bool has_debug_stmt = false; |
28c92cbb | 1340 | |
75a70cf9 | 1341 | entry = single_succ_edge (entry_bb); |
e06f9c34 | 1342 | gather_blocks_in_sese_region (entry_bb, exit_bb, &body); |
28c92cbb | 1343 | |
f1f41a6c | 1344 | FOR_EACH_VEC_ELT (body, i, bb) |
e06f9c34 | 1345 | { |
48e1416a | 1346 | if (bb != entry_bb && bb != exit_bb) |
e06f9c34 | 1347 | { |
75a70cf9 | 1348 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
1349 | separate_decls_in_region_stmt (entry, exit, gsi_stmt (gsi), | |
c1f445d2 | 1350 | &name_copies, &decl_copies); |
75a70cf9 | 1351 | |
1352 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
9845d120 | 1353 | { |
42acab1c | 1354 | gimple *stmt = gsi_stmt (gsi); |
9845d120 | 1355 | |
1356 | if (is_gimple_debug (stmt)) | |
1357 | has_debug_stmt = true; | |
1358 | else | |
1359 | separate_decls_in_region_stmt (entry, exit, stmt, | |
c1f445d2 | 1360 | &name_copies, &decl_copies); |
9845d120 | 1361 | } |
e06f9c34 | 1362 | } |
28c92cbb | 1363 | } |
e06f9c34 | 1364 | |
9845d120 | 1365 | /* Now process debug bind stmts. We must not create decls while |
1366 | processing debug stmts, so we defer their processing so as to | |
1367 | make sure we will have debug info for as many variables as | |
1368 | possible (all of those that were dealt with in the loop above), | |
1369 | and discard those for which we know there's nothing we can | |
1370 | do. */ | |
1371 | if (has_debug_stmt) | |
f1f41a6c | 1372 | FOR_EACH_VEC_ELT (body, i, bb) |
9845d120 | 1373 | if (bb != entry_bb && bb != exit_bb) |
1374 | { | |
1375 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);) | |
1376 | { | |
42acab1c | 1377 | gimple *stmt = gsi_stmt (gsi); |
9845d120 | 1378 | |
841424cc | 1379 | if (is_gimple_debug (stmt)) |
9845d120 | 1380 | { |
c1f445d2 | 1381 | if (separate_decls_in_region_debug (stmt, &name_copies, |
1382 | &decl_copies)) | |
9845d120 | 1383 | { |
1384 | gsi_remove (&gsi, true); | |
1385 | continue; | |
1386 | } | |
1387 | } | |
1388 | ||
1389 | gsi_next (&gsi); | |
1390 | } | |
1391 | } | |
1392 | ||
9a78b979 | 1393 | if (name_copies.is_empty () && reduction_list->is_empty ()) |
28c92cbb | 1394 | { |
1395 | /* It may happen that there is nothing to copy (if there are only | |
cb7f680b | 1396 | loop carried and external variables in the loop). */ |
28c92cbb | 1397 | *arg_struct = NULL; |
1398 | *new_arg_struct = NULL; | |
1399 | } | |
1400 | else | |
1401 | { | |
1402 | /* Create the type for the structure to store the ssa names to. */ | |
1403 | type = lang_hooks.types.make_type (RECORD_TYPE); | |
0aecb55e | 1404 | type_name = build_decl (UNKNOWN_LOCATION, |
e60a6f7b | 1405 | TYPE_DECL, create_tmp_var_name (".paral_data"), |
28c92cbb | 1406 | type); |
1407 | TYPE_NAME (type) = type_name; | |
1408 | ||
d9dd21a8 | 1409 | name_copies.traverse <tree, add_field_for_name> (type); |
9a78b979 | 1410 | if (reduction_list && !reduction_list->is_empty ()) |
848674d0 | 1411 | { |
1412 | /* Create the fields for reductions. */ | |
c1f445d2 | 1413 | reduction_list->traverse <tree, add_field_for_reduction> (type); |
848674d0 | 1414 | } |
28c92cbb | 1415 | layout_type (type); |
48e1416a | 1416 | |
28c92cbb | 1417 | /* Create the loads and stores. */ |
1418 | *arg_struct = create_tmp_var (type, ".paral_data_store"); | |
28c92cbb | 1419 | nvar = create_tmp_var (build_pointer_type (type), ".paral_data_load"); |
f9e245b2 | 1420 | *new_arg_struct = make_ssa_name (nvar); |
28c92cbb | 1421 | |
cb7f680b | 1422 | ld_st_data->store = *arg_struct; |
1423 | ld_st_data->load = *new_arg_struct; | |
1424 | ld_st_data->store_bb = bb0; | |
1425 | ld_st_data->load_bb = bb1; | |
848674d0 | 1426 | |
d9dd21a8 | 1427 | name_copies |
1428 | .traverse <struct clsn_data *, create_loads_and_stores_for_name> | |
1429 | (ld_st_data); | |
cb7f680b | 1430 | |
5bb62c99 | 1431 | /* Load the calculation from memory (after the join of the threads). */ |
1432 | ||
9a78b979 | 1433 | if (reduction_list && !reduction_list->is_empty ()) |
cb7f680b | 1434 | { |
d9dd21a8 | 1435 | reduction_list |
c1f445d2 | 1436 | ->traverse <struct clsn_data *, create_stores_for_reduction> |
1437 | (ld_st_data); | |
f9e245b2 | 1438 | clsn_data.load = make_ssa_name (nvar); |
e06f9c34 | 1439 | clsn_data.load_bb = exit->dest; |
cb7f680b | 1440 | clsn_data.store = ld_st_data->store; |
1441 | create_final_loads_for_reduction (reduction_list, &clsn_data); | |
1442 | } | |
28c92cbb | 1443 | } |
28c92cbb | 1444 | } |
1445 | ||
66460ec1 | 1446 | /* Returns true if FN was created to run in parallel. */ |
28c92cbb | 1447 | |
479a6d79 | 1448 | bool |
66460ec1 | 1449 | parallelized_function_p (tree fndecl) |
28c92cbb | 1450 | { |
66460ec1 | 1451 | cgraph_node *node = cgraph_node::get (fndecl); |
1452 | gcc_assert (node != NULL); | |
1453 | return node->parallelized_function; | |
28c92cbb | 1454 | } |
1455 | ||
1456 | /* Creates and returns an empty function that will receive the body of | |
1457 | a parallelized loop. */ | |
1458 | ||
1459 | static tree | |
0aecb55e | 1460 | create_loop_fn (location_t loc) |
28c92cbb | 1461 | { |
1462 | char buf[100]; | |
1463 | char *tname; | |
1464 | tree decl, type, name, t; | |
1465 | struct function *act_cfun = cfun; | |
1466 | static unsigned loopfn_num; | |
1467 | ||
5169661d | 1468 | loc = LOCATION_LOCUS (loc); |
28c92cbb | 1469 | snprintf (buf, 100, "%s.$loopfn", current_function_name ()); |
1470 | ASM_FORMAT_PRIVATE_NAME (tname, buf, loopfn_num++); | |
1471 | clean_symbol_name (tname); | |
1472 | name = get_identifier (tname); | |
1473 | type = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE); | |
1474 | ||
0aecb55e | 1475 | decl = build_decl (loc, FUNCTION_DECL, name, type); |
28c92cbb | 1476 | TREE_STATIC (decl) = 1; |
1477 | TREE_USED (decl) = 1; | |
1478 | DECL_ARTIFICIAL (decl) = 1; | |
1479 | DECL_IGNORED_P (decl) = 0; | |
1480 | TREE_PUBLIC (decl) = 0; | |
1481 | DECL_UNINLINABLE (decl) = 1; | |
1482 | DECL_EXTERNAL (decl) = 0; | |
1483 | DECL_CONTEXT (decl) = NULL_TREE; | |
1484 | DECL_INITIAL (decl) = make_node (BLOCK); | |
2a066179 | 1485 | BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl; |
28c92cbb | 1486 | |
0aecb55e | 1487 | t = build_decl (loc, RESULT_DECL, NULL_TREE, void_type_node); |
28c92cbb | 1488 | DECL_ARTIFICIAL (t) = 1; |
1489 | DECL_IGNORED_P (t) = 1; | |
1490 | DECL_RESULT (decl) = t; | |
1491 | ||
0aecb55e | 1492 | t = build_decl (loc, PARM_DECL, get_identifier (".paral_data_param"), |
28c92cbb | 1493 | ptr_type_node); |
1494 | DECL_ARTIFICIAL (t) = 1; | |
1495 | DECL_ARG_TYPE (t) = ptr_type_node; | |
1496 | DECL_CONTEXT (t) = decl; | |
1497 | TREE_USED (t) = 1; | |
1498 | DECL_ARGUMENTS (decl) = t; | |
1499 | ||
80f2ef47 | 1500 | allocate_struct_function (decl, false); |
612006d4 | 1501 | DECL_STRUCT_FUNCTION (decl)->last_clique = act_cfun->last_clique; |
28c92cbb | 1502 | |
1503 | /* The call to allocate_struct_function clobbers CFUN, so we need to restore | |
1504 | it. */ | |
c8a152f6 | 1505 | set_cfun (act_cfun); |
28c92cbb | 1506 | |
1507 | return decl; | |
1508 | } | |
1509 | ||
d38409cd | 1510 | /* Replace uses of NAME by VAL in block BB. */ |
1511 | ||
1512 | static void | |
1513 | replace_uses_in_bb_by (tree name, tree val, basic_block bb) | |
1514 | { | |
42acab1c | 1515 | gimple *use_stmt; |
d38409cd | 1516 | imm_use_iterator imm_iter; |
1517 | ||
1518 | FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, name) | |
1519 | { | |
1520 | if (gimple_bb (use_stmt) != bb) | |
1521 | continue; | |
1522 | ||
1523 | use_operand_p use_p; | |
1524 | FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter) | |
1525 | SET_USE (use_p, val); | |
1526 | } | |
1527 | } | |
1528 | ||
d38409cd | 1529 | /* Do transformation from: |
1530 | ||
1531 | <bb preheader>: | |
1532 | ... | |
1533 | goto <bb header> | |
1534 | ||
1535 | <bb header>: | |
1536 | ivtmp_a = PHI <ivtmp_init (preheader), ivtmp_b (latch)> | |
1537 | sum_a = PHI <sum_init (preheader), sum_b (latch)> | |
1538 | ... | |
1539 | use (ivtmp_a) | |
1540 | ... | |
1541 | sum_b = sum_a + sum_update | |
1542 | ... | |
1543 | if (ivtmp_a < n) | |
1544 | goto <bb latch>; | |
1545 | else | |
1546 | goto <bb exit>; | |
1547 | ||
1548 | <bb latch>: | |
1549 | ivtmp_b = ivtmp_a + 1; | |
1550 | goto <bb header> | |
1551 | ||
1552 | <bb exit>: | |
5a3d2e18 | 1553 | sum_z = PHI <sum_b (cond[1]), ...> |
d38409cd | 1554 | |
1555 | [1] Where <bb cond> is single_pred (bb latch); In the simplest case, | |
1556 | that's <bb header>. | |
1557 | ||
1558 | to: | |
1559 | ||
1560 | <bb preheader>: | |
1561 | ... | |
1562 | goto <bb newheader> | |
1563 | ||
1564 | <bb header>: | |
1565 | ivtmp_a = PHI <ivtmp_c (latch)> | |
1566 | sum_a = PHI <sum_c (latch)> | |
1567 | ... | |
1568 | use (ivtmp_a) | |
1569 | ... | |
1570 | sum_b = sum_a + sum_update | |
1571 | ... | |
1572 | goto <bb latch>; | |
1573 | ||
1574 | <bb newheader>: | |
1575 | ivtmp_c = PHI <ivtmp_init (preheader), ivtmp_b (latch)> | |
1576 | sum_c = PHI <sum_init (preheader), sum_b (latch)> | |
1577 | if (ivtmp_c < n + 1) | |
1578 | goto <bb header>; | |
1579 | else | |
5a3d2e18 | 1580 | goto <bb newexit>; |
d38409cd | 1581 | |
1582 | <bb latch>: | |
1583 | ivtmp_b = ivtmp_a + 1; | |
1584 | goto <bb newheader> | |
1585 | ||
5a3d2e18 | 1586 | <bb newexit>: |
1587 | sum_y = PHI <sum_c (newheader)> | |
1588 | ||
d38409cd | 1589 | <bb exit>: |
5a3d2e18 | 1590 | sum_z = PHI <sum_y (newexit), ...> |
d38409cd | 1591 | |
1592 | ||
1593 | In unified diff format: | |
1594 | ||
1595 | <bb preheader>: | |
1596 | ... | |
1597 | - goto <bb header> | |
1598 | + goto <bb newheader> | |
1599 | ||
1600 | <bb header>: | |
1601 | - ivtmp_a = PHI <ivtmp_init (preheader), ivtmp_b (latch)> | |
1602 | - sum_a = PHI <sum_init (preheader), sum_b (latch)> | |
1603 | + ivtmp_a = PHI <ivtmp_c (latch)> | |
1604 | + sum_a = PHI <sum_c (latch)> | |
1605 | ... | |
1606 | use (ivtmp_a) | |
1607 | ... | |
1608 | sum_b = sum_a + sum_update | |
1609 | ... | |
1610 | - if (ivtmp_a < n) | |
1611 | - goto <bb latch>; | |
1612 | + goto <bb latch>; | |
1613 | + | |
1614 | + <bb newheader>: | |
1615 | + ivtmp_c = PHI <ivtmp_init (preheader), ivtmp_b (latch)> | |
1616 | + sum_c = PHI <sum_init (preheader), sum_b (latch)> | |
1617 | + if (ivtmp_c < n + 1) | |
1618 | + goto <bb header>; | |
1619 | else | |
1620 | goto <bb exit>; | |
1621 | ||
1622 | <bb latch>: | |
1623 | ivtmp_b = ivtmp_a + 1; | |
1624 | - goto <bb header> | |
1625 | + goto <bb newheader> | |
1626 | ||
5a3d2e18 | 1627 | + <bb newexit>: |
1628 | + sum_y = PHI <sum_c (newheader)> | |
1629 | ||
d38409cd | 1630 | <bb exit>: |
5a3d2e18 | 1631 | - sum_z = PHI <sum_b (cond[1]), ...> |
1632 | + sum_z = PHI <sum_y (newexit), ...> | |
d38409cd | 1633 | |
1634 | Note: the example does not show any virtual phis, but these are handled more | |
1635 | or less as reductions. | |
48e1416a | 1636 | |
d38409cd | 1637 | |
1638 | Moves the exit condition of LOOP to the beginning of its header. | |
1639 | REDUCTION_LIST describes the reductions in LOOP. BOUND is the new loop | |
1640 | bound. */ | |
1641 | ||
1642 | static void | |
2e966e2a | 1643 | transform_to_exit_first_loop_alt (class loop *loop, |
d38409cd | 1644 | reduction_info_table_type *reduction_list, |
1645 | tree bound) | |
1646 | { | |
1647 | basic_block header = loop->header; | |
1648 | basic_block latch = loop->latch; | |
1649 | edge exit = single_dom_exit (loop); | |
1650 | basic_block exit_block = exit->dest; | |
1651 | gcond *cond_stmt = as_a <gcond *> (last_stmt (exit->src)); | |
1652 | tree control = gimple_cond_lhs (cond_stmt); | |
1653 | edge e; | |
1654 | ||
4d7c6f77 | 1655 | /* Rewriting virtuals into loop-closed ssa normal form makes this |
1656 | transformation simpler. It also ensures that the virtuals are in | |
1657 | loop-closed ssa normal from after the transformation, which is required by | |
1658 | create_parallel_loop. */ | |
1659 | rewrite_virtuals_into_loop_closed_ssa (loop); | |
d38409cd | 1660 | |
1661 | /* Create the new_header block. */ | |
1662 | basic_block new_header = split_block_before_cond_jump (exit->src); | |
5a3d2e18 | 1663 | edge edge_at_split = single_pred_edge (new_header); |
d38409cd | 1664 | |
1665 | /* Redirect entry edge to new_header. */ | |
1666 | edge entry = loop_preheader_edge (loop); | |
1667 | e = redirect_edge_and_branch (entry, new_header); | |
1668 | gcc_assert (e == entry); | |
1669 | ||
1670 | /* Redirect post_inc_edge to new_header. */ | |
1671 | edge post_inc_edge = single_succ_edge (latch); | |
1672 | e = redirect_edge_and_branch (post_inc_edge, new_header); | |
1673 | gcc_assert (e == post_inc_edge); | |
1674 | ||
1675 | /* Redirect post_cond_edge to header. */ | |
1676 | edge post_cond_edge = single_pred_edge (latch); | |
1677 | e = redirect_edge_and_branch (post_cond_edge, header); | |
1678 | gcc_assert (e == post_cond_edge); | |
1679 | ||
5a3d2e18 | 1680 | /* Redirect edge_at_split to latch. */ |
1681 | e = redirect_edge_and_branch (edge_at_split, latch); | |
1682 | gcc_assert (e == edge_at_split); | |
d38409cd | 1683 | |
1684 | /* Set the new loop bound. */ | |
1685 | gimple_cond_set_rhs (cond_stmt, bound); | |
7f2289e8 | 1686 | update_stmt (cond_stmt); |
d38409cd | 1687 | |
1688 | /* Repair the ssa. */ | |
1689 | vec<edge_var_map> *v = redirect_edge_var_map_vector (post_inc_edge); | |
1690 | edge_var_map *vm; | |
1691 | gphi_iterator gsi; | |
4d7c6f77 | 1692 | int i; |
d38409cd | 1693 | for (gsi = gsi_start_phis (header), i = 0; |
1694 | !gsi_end_p (gsi) && v->iterate (i, &vm); | |
1695 | gsi_next (&gsi), i++) | |
1696 | { | |
1697 | gphi *phi = gsi.phi (); | |
1698 | tree res_a = PHI_RESULT (phi); | |
1699 | ||
1700 | /* Create new phi. */ | |
1701 | tree res_c = copy_ssa_name (res_a, phi); | |
1702 | gphi *nphi = create_phi_node (res_c, new_header); | |
1703 | ||
1704 | /* Replace ivtmp_a with ivtmp_c in condition 'if (ivtmp_a < n)'. */ | |
1705 | replace_uses_in_bb_by (res_a, res_c, new_header); | |
1706 | ||
1707 | /* Replace ivtmp/sum_b with ivtmp/sum_c in header phi. */ | |
1708 | add_phi_arg (phi, res_c, post_cond_edge, UNKNOWN_LOCATION); | |
1709 | ||
4d7c6f77 | 1710 | /* Replace sum_b with sum_c in exit phi. */ |
d38409cd | 1711 | tree res_b = redirect_edge_var_map_def (vm); |
4d7c6f77 | 1712 | replace_uses_in_bb_by (res_b, res_c, exit_block); |
d38409cd | 1713 | |
1714 | struct reduction_info *red = reduction_phi (reduction_list, phi); | |
1715 | gcc_assert (virtual_operand_p (res_a) | |
1716 | || res_a == control | |
1717 | || red != NULL); | |
1718 | ||
1719 | if (red) | |
1720 | { | |
1721 | /* Register the new reduction phi. */ | |
1722 | red->reduc_phi = nphi; | |
1723 | gimple_set_uid (red->reduc_phi, red->reduc_version); | |
1724 | } | |
1725 | } | |
1726 | gcc_assert (gsi_end_p (gsi) && !v->iterate (i, &vm)); | |
d38409cd | 1727 | |
1728 | /* Set the preheader argument of the new phis to ivtmp/sum_init. */ | |
1729 | flush_pending_stmts (entry); | |
1730 | ||
1731 | /* Set the latch arguments of the new phis to ivtmp/sum_b. */ | |
1732 | flush_pending_stmts (post_inc_edge); | |
1733 | ||
0381a8e7 | 1734 | |
1735 | basic_block new_exit_block = NULL; | |
1736 | if (!single_pred_p (exit->dest)) | |
1737 | { | |
1738 | /* Create a new empty exit block, inbetween the new loop header and the | |
1739 | old exit block. The function separate_decls_in_region needs this block | |
1740 | to insert code that is active on loop exit, but not any other path. */ | |
1741 | new_exit_block = split_edge (exit); | |
1742 | } | |
5a3d2e18 | 1743 | |
1744 | /* Insert and register the reduction exit phis. */ | |
d38409cd | 1745 | for (gphi_iterator gsi = gsi_start_phis (exit_block); |
1746 | !gsi_end_p (gsi); | |
1747 | gsi_next (&gsi)) | |
1748 | { | |
1749 | gphi *phi = gsi.phi (); | |
0381a8e7 | 1750 | gphi *nphi = NULL; |
d38409cd | 1751 | tree res_z = PHI_RESULT (phi); |
0381a8e7 | 1752 | tree res_c; |
5a3d2e18 | 1753 | |
0381a8e7 | 1754 | if (new_exit_block != NULL) |
1755 | { | |
1756 | /* Now that we have a new exit block, duplicate the phi of the old | |
1757 | exit block in the new exit block to preserve loop-closed ssa. */ | |
1758 | edge succ_new_exit_block = single_succ_edge (new_exit_block); | |
1759 | edge pred_new_exit_block = single_pred_edge (new_exit_block); | |
1760 | tree res_y = copy_ssa_name (res_z, phi); | |
1761 | nphi = create_phi_node (res_y, new_exit_block); | |
1762 | res_c = PHI_ARG_DEF_FROM_EDGE (phi, succ_new_exit_block); | |
1763 | add_phi_arg (nphi, res_c, pred_new_exit_block, UNKNOWN_LOCATION); | |
1764 | add_phi_arg (phi, res_y, succ_new_exit_block, UNKNOWN_LOCATION); | |
1765 | } | |
1766 | else | |
1767 | res_c = PHI_ARG_DEF_FROM_EDGE (phi, exit); | |
5a3d2e18 | 1768 | |
d38409cd | 1769 | if (virtual_operand_p (res_z)) |
1770 | continue; | |
1771 | ||
42acab1c | 1772 | gimple *reduc_phi = SSA_NAME_DEF_STMT (res_c); |
d38409cd | 1773 | struct reduction_info *red = reduction_phi (reduction_list, reduc_phi); |
1774 | if (red != NULL) | |
0381a8e7 | 1775 | red->keep_res = (nphi != NULL |
1776 | ? nphi | |
1777 | : phi); | |
d38409cd | 1778 | } |
1779 | ||
1780 | /* We're going to cancel the loop at the end of gen_parallel_loop, but until | |
1781 | then we're still using some fields, so only bother about fields that are | |
1782 | still used: header and latch. | |
1783 | The loop has a new header bb, so we update it. The latch bb stays the | |
1784 | same. */ | |
1785 | loop->header = new_header; | |
1786 | ||
1787 | /* Recalculate dominance info. */ | |
1788 | free_dominance_info (CDI_DOMINATORS); | |
1789 | calculate_dominance_info (CDI_DOMINATORS); | |
a7abe6a0 | 1790 | |
1791 | checking_verify_ssa (true, true); | |
d38409cd | 1792 | } |
1793 | ||
1794 | /* Tries to moves the exit condition of LOOP to the beginning of its header | |
1795 | without duplication of the loop body. NIT is the number of iterations of the | |
1796 | loop. REDUCTION_LIST describes the reductions in LOOP. Return true if | |
1797 | transformation is successful. */ | |
1798 | ||
1799 | static bool | |
2e966e2a | 1800 | try_transform_to_exit_first_loop_alt (class loop *loop, |
d38409cd | 1801 | reduction_info_table_type *reduction_list, |
1802 | tree nit) | |
1803 | { | |
1804 | /* Check whether the latch contains a single statement. */ | |
2ce3b2a5 | 1805 | if (!gimple_seq_nondebug_singleton_p (bb_seq (loop->latch))) |
1806 | return false; | |
d38409cd | 1807 | |
9eb890f1 | 1808 | /* Check whether the latch contains no phis. */ |
1809 | if (phi_nodes (loop->latch) != NULL) | |
1810 | return false; | |
1811 | ||
d38409cd | 1812 | /* Check whether the latch contains the loop iv increment. */ |
1813 | edge back = single_succ_edge (loop->latch); | |
1814 | edge exit = single_dom_exit (loop); | |
1815 | gcond *cond_stmt = as_a <gcond *> (last_stmt (exit->src)); | |
1816 | tree control = gimple_cond_lhs (cond_stmt); | |
1817 | gphi *phi = as_a <gphi *> (SSA_NAME_DEF_STMT (control)); | |
1818 | tree inc_res = gimple_phi_arg_def (phi, back->dest_idx); | |
1819 | if (gimple_bb (SSA_NAME_DEF_STMT (inc_res)) != loop->latch) | |
1820 | return false; | |
1821 | ||
1822 | /* Check whether there's no code between the loop condition and the latch. */ | |
1823 | if (!single_pred_p (loop->latch) | |
1824 | || single_pred (loop->latch) != exit->src) | |
1825 | return false; | |
1826 | ||
1827 | tree alt_bound = NULL_TREE; | |
1828 | tree nit_type = TREE_TYPE (nit); | |
1829 | ||
1830 | /* Figure out whether nit + 1 overflows. */ | |
1831 | if (TREE_CODE (nit) == INTEGER_CST) | |
1832 | { | |
a8de6c92 | 1833 | if (!tree_int_cst_equal (nit, TYPE_MAX_VALUE (nit_type))) |
d38409cd | 1834 | { |
1835 | alt_bound = fold_build2_loc (UNKNOWN_LOCATION, PLUS_EXPR, nit_type, | |
1836 | nit, build_one_cst (nit_type)); | |
1837 | ||
1838 | gcc_assert (TREE_CODE (alt_bound) == INTEGER_CST); | |
bd797881 | 1839 | transform_to_exit_first_loop_alt (loop, reduction_list, alt_bound); |
1840 | return true; | |
d38409cd | 1841 | } |
1842 | else | |
1843 | { | |
1844 | /* Todo: Figure out if we can trigger this, if it's worth to handle | |
1845 | optimally, and if we can handle it optimally. */ | |
bd797881 | 1846 | return false; |
d38409cd | 1847 | } |
1848 | } | |
d38409cd | 1849 | |
bd797881 | 1850 | gcc_assert (TREE_CODE (nit) == SSA_NAME); |
d38409cd | 1851 | |
5d4f3ed8 | 1852 | /* Variable nit is the loop bound as returned by canonicalize_loop_ivs, for an |
1853 | iv with base 0 and step 1 that is incremented in the latch, like this: | |
1854 | ||
1855 | <bb header>: | |
1856 | # iv_1 = PHI <0 (preheader), iv_2 (latch)> | |
1857 | ... | |
1858 | if (iv_1 < nit) | |
1859 | goto <bb latch>; | |
1860 | else | |
1861 | goto <bb exit>; | |
1862 | ||
1863 | <bb latch>: | |
1864 | iv_2 = iv_1 + 1; | |
1865 | goto <bb header>; | |
1866 | ||
1867 | The range of iv_1 is [0, nit]. The latch edge is taken for | |
1868 | iv_1 == [0, nit - 1] and the exit edge is taken for iv_1 == nit. So the | |
1869 | number of latch executions is equal to nit. | |
1870 | ||
1871 | The function max_loop_iterations gives us the maximum number of latch | |
1872 | executions, so it gives us the maximum value of nit. */ | |
1873 | widest_int nit_max; | |
1874 | if (!max_loop_iterations (loop, &nit_max)) | |
1875 | return false; | |
1876 | ||
1877 | /* Check if nit + 1 overflows. */ | |
a8de6c92 | 1878 | widest_int type_max = wi::to_widest (TYPE_MAX_VALUE (nit_type)); |
32115eac | 1879 | if (nit_max >= type_max) |
5d4f3ed8 | 1880 | return false; |
1881 | ||
42acab1c | 1882 | gimple *def = SSA_NAME_DEF_STMT (nit); |
d38409cd | 1883 | |
5d4f3ed8 | 1884 | /* Try to find nit + 1, in the form of n in an assignment nit = n - 1. */ |
bd797881 | 1885 | if (def |
1886 | && is_gimple_assign (def) | |
1887 | && gimple_assign_rhs_code (def) == PLUS_EXPR) | |
1888 | { | |
1889 | tree op1 = gimple_assign_rhs1 (def); | |
1890 | tree op2 = gimple_assign_rhs2 (def); | |
1891 | if (integer_minus_onep (op1)) | |
1892 | alt_bound = op2; | |
1893 | else if (integer_minus_onep (op2)) | |
1894 | alt_bound = op1; | |
d38409cd | 1895 | } |
1896 | ||
3c31a6c6 | 1897 | /* If not found, insert nit + 1. */ |
d38409cd | 1898 | if (alt_bound == NULL_TREE) |
3c31a6c6 | 1899 | { |
1900 | alt_bound = fold_build2 (PLUS_EXPR, nit_type, nit, | |
1901 | build_int_cst_type (nit_type, 1)); | |
1902 | ||
1903 | gimple_stmt_iterator gsi = gsi_last_bb (loop_preheader_edge (loop)->src); | |
1904 | ||
1905 | alt_bound | |
1906 | = force_gimple_operand_gsi (&gsi, alt_bound, true, NULL_TREE, false, | |
1907 | GSI_CONTINUE_LINKING); | |
1908 | } | |
d38409cd | 1909 | |
1910 | transform_to_exit_first_loop_alt (loop, reduction_list, alt_bound); | |
1911 | return true; | |
1912 | } | |
1913 | ||
1914 | /* Moves the exit condition of LOOP to the beginning of its header. NIT is the | |
1915 | number of iterations of the loop. REDUCTION_LIST describes the reductions in | |
1916 | LOOP. */ | |
28c92cbb | 1917 | |
1918 | static void | |
2e966e2a | 1919 | transform_to_exit_first_loop (class loop *loop, |
c1f445d2 | 1920 | reduction_info_table_type *reduction_list, |
d9dd21a8 | 1921 | tree nit) |
28c92cbb | 1922 | { |
1923 | basic_block *bbs, *nbbs, ex_bb, orig_header; | |
1924 | unsigned n; | |
1925 | bool ok; | |
1926 | edge exit = single_dom_exit (loop), hpred; | |
75a70cf9 | 1927 | tree control, control_name, res, t; |
1a91d914 | 1928 | gphi *phi, *nphi; |
1929 | gassign *stmt; | |
1930 | gcond *cond_stmt, *cond_nit; | |
b0fb253a | 1931 | tree nit_1; |
28c92cbb | 1932 | |
1933 | split_block_after_labels (loop->header); | |
1934 | orig_header = single_succ (loop->header); | |
1935 | hpred = single_succ_edge (loop->header); | |
1936 | ||
1a91d914 | 1937 | cond_stmt = as_a <gcond *> (last_stmt (exit->src)); |
75a70cf9 | 1938 | control = gimple_cond_lhs (cond_stmt); |
1939 | gcc_assert (gimple_cond_rhs (cond_stmt) == nit); | |
28c92cbb | 1940 | |
1941 | /* Make sure that we have phi nodes on exit for all loop header phis | |
1942 | (create_parallel_loop requires that). */ | |
1a91d914 | 1943 | for (gphi_iterator gsi = gsi_start_phis (loop->header); |
1944 | !gsi_end_p (gsi); | |
1945 | gsi_next (&gsi)) | |
28c92cbb | 1946 | { |
1a91d914 | 1947 | phi = gsi.phi (); |
28c92cbb | 1948 | res = PHI_RESULT (phi); |
874117c8 | 1949 | t = copy_ssa_name (res, phi); |
28c92cbb | 1950 | SET_PHI_RESULT (phi, t); |
28c92cbb | 1951 | nphi = create_phi_node (res, orig_header); |
60d535d2 | 1952 | add_phi_arg (nphi, t, hpred, UNKNOWN_LOCATION); |
28c92cbb | 1953 | |
1954 | if (res == control) | |
1955 | { | |
75a70cf9 | 1956 | gimple_cond_set_lhs (cond_stmt, t); |
28c92cbb | 1957 | update_stmt (cond_stmt); |
1958 | control = t; | |
1959 | } | |
1960 | } | |
2a556654 | 1961 | |
28c92cbb | 1962 | bbs = get_loop_body_in_dom_order (loop); |
b0fb253a | 1963 | |
89675e8c | 1964 | for (n = 0; bbs[n] != exit->src; n++) |
1965 | continue; | |
28c92cbb | 1966 | nbbs = XNEWVEC (basic_block, n); |
75a70cf9 | 1967 | ok = gimple_duplicate_sese_tail (single_succ_edge (loop->header), exit, |
1968 | bbs + 1, n, nbbs); | |
28c92cbb | 1969 | gcc_assert (ok); |
1970 | free (bbs); | |
1971 | ex_bb = nbbs[0]; | |
1972 | free (nbbs); | |
1973 | ||
48e1416a | 1974 | /* Other than reductions, the only gimple reg that should be copied |
75a70cf9 | 1975 | out of the loop is the control variable. */ |
89675e8c | 1976 | exit = single_dom_exit (loop); |
28c92cbb | 1977 | control_name = NULL_TREE; |
1a91d914 | 1978 | for (gphi_iterator gsi = gsi_start_phis (ex_bb); |
1979 | !gsi_end_p (gsi); ) | |
28c92cbb | 1980 | { |
1a91d914 | 1981 | phi = gsi.phi (); |
28c92cbb | 1982 | res = PHI_RESULT (phi); |
7c782c9b | 1983 | if (virtual_operand_p (res)) |
75a70cf9 | 1984 | { |
1985 | gsi_next (&gsi); | |
1986 | continue; | |
1987 | } | |
28c92cbb | 1988 | |
cb7f680b | 1989 | /* Check if it is a part of reduction. If it is, |
48e1416a | 1990 | keep the phi at the reduction's keep_res field. The |
1991 | PHI_RESULT of this phi is the resulting value of the reduction | |
cb7f680b | 1992 | variable when exiting the loop. */ |
1993 | ||
9a78b979 | 1994 | if (!reduction_list->is_empty ()) |
cb7f680b | 1995 | { |
1996 | struct reduction_info *red; | |
1997 | ||
1998 | tree val = PHI_ARG_DEF_FROM_EDGE (phi, exit); | |
cb7f680b | 1999 | red = reduction_phi (reduction_list, SSA_NAME_DEF_STMT (val)); |
2000 | if (red) | |
75a70cf9 | 2001 | { |
2002 | red->keep_res = phi; | |
2003 | gsi_next (&gsi); | |
2004 | continue; | |
2005 | } | |
cb7f680b | 2006 | } |
75a70cf9 | 2007 | gcc_assert (control_name == NULL_TREE |
2008 | && SSA_NAME_VAR (res) == SSA_NAME_VAR (control)); | |
28c92cbb | 2009 | control_name = res; |
75a70cf9 | 2010 | remove_phi_node (&gsi, false); |
28c92cbb | 2011 | } |
2012 | gcc_assert (control_name != NULL_TREE); | |
28c92cbb | 2013 | |
48e1416a | 2014 | /* Initialize the control variable to number of iterations |
b0fb253a | 2015 | according to the rhs of the exit condition. */ |
1a91d914 | 2016 | gimple_stmt_iterator gsi = gsi_after_labels (ex_bb); |
2017 | cond_nit = as_a <gcond *> (last_stmt (exit->src)); | |
b0fb253a | 2018 | nit_1 = gimple_cond_rhs (cond_nit); |
2019 | nit_1 = force_gimple_operand_gsi (&gsi, | |
2020 | fold_convert (TREE_TYPE (control_name), nit_1), | |
75a70cf9 | 2021 | false, NULL_TREE, false, GSI_SAME_STMT); |
b0fb253a | 2022 | stmt = gimple_build_assign (control_name, nit_1); |
75a70cf9 | 2023 | gsi_insert_before (&gsi, stmt, GSI_NEW_STMT); |
28c92cbb | 2024 | } |
2025 | ||
2026 | /* Create the parallel constructs for LOOP as described in gen_parallel_loop. | |
75a70cf9 | 2027 | LOOP_FN and DATA are the arguments of GIMPLE_OMP_PARALLEL. |
28c92cbb | 2028 | NEW_DATA is the variable that should be initialized from the argument |
3a37a410 | 2029 | of LOOP_FN. N_THREADS is the requested number of threads, which can be 0 if |
2030 | that number is to be determined later. */ | |
28c92cbb | 2031 | |
6943c9df | 2032 | static void |
2e966e2a | 2033 | create_parallel_loop (class loop *loop, tree loop_fn, tree data, |
31c2b13b | 2034 | tree new_data, unsigned n_threads, location_t loc, |
2035 | bool oacc_kernels_p) | |
28c92cbb | 2036 | { |
75a70cf9 | 2037 | gimple_stmt_iterator gsi; |
31c2b13b | 2038 | basic_block for_bb, ex_bb, continue_bb; |
f018d957 | 2039 | tree t, param; |
1a91d914 | 2040 | gomp_parallel *omp_par_stmt; |
42acab1c | 2041 | gimple *omp_return_stmt1, *omp_return_stmt2; |
2042 | gimple *phi; | |
1a91d914 | 2043 | gcond *cond_stmt; |
2044 | gomp_for *for_stmt; | |
2045 | gomp_continue *omp_cont_stmt; | |
75a70cf9 | 2046 | tree cvar, cvar_init, initvar, cvar_next, cvar_base, type; |
28c92cbb | 2047 | edge exit, nexit, guard, end, e; |
2048 | ||
31c2b13b | 2049 | if (oacc_kernels_p) |
2050 | { | |
1d3ea8fc | 2051 | gcc_checking_assert (lookup_attribute ("oacc kernels", |
2052 | DECL_ATTRIBUTES (cfun->decl))); | |
5dd0c4e8 | 2053 | /* Indicate to later processing that this is a parallelized OpenACC |
2054 | kernels construct. */ | |
2055 | DECL_ATTRIBUTES (cfun->decl) | |
2056 | = tree_cons (get_identifier ("oacc kernels parallelized"), | |
2057 | NULL_TREE, DECL_ATTRIBUTES (cfun->decl)); | |
31c2b13b | 2058 | } |
2059 | else | |
2060 | { | |
5dd0c4e8 | 2061 | /* Prepare the GIMPLE_OMP_PARALLEL statement. */ |
2062 | ||
31c2b13b | 2063 | basic_block bb = loop_preheader_edge (loop)->src; |
2064 | basic_block paral_bb = single_pred (bb); | |
2065 | gsi = gsi_last_bb (paral_bb); | |
28c92cbb | 2066 | |
3a37a410 | 2067 | gcc_checking_assert (n_threads != 0); |
31c2b13b | 2068 | t = build_omp_clause (loc, OMP_CLAUSE_NUM_THREADS); |
2069 | OMP_CLAUSE_NUM_THREADS_EXPR (t) | |
2070 | = build_int_cst (integer_type_node, n_threads); | |
2071 | omp_par_stmt = gimple_build_omp_parallel (NULL, t, loop_fn, data); | |
2072 | gimple_set_location (omp_par_stmt, loc); | |
28c92cbb | 2073 | |
31c2b13b | 2074 | gsi_insert_after (&gsi, omp_par_stmt, GSI_NEW_STMT); |
28c92cbb | 2075 | |
31c2b13b | 2076 | /* Initialize NEW_DATA. */ |
2077 | if (data) | |
2078 | { | |
2079 | gassign *assign_stmt; | |
1a91d914 | 2080 | |
31c2b13b | 2081 | gsi = gsi_after_labels (bb); |
75a70cf9 | 2082 | |
31c2b13b | 2083 | param = make_ssa_name (DECL_ARGUMENTS (loop_fn)); |
2084 | assign_stmt = gimple_build_assign (param, build_fold_addr_expr (data)); | |
2085 | gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT); | |
75a70cf9 | 2086 | |
31c2b13b | 2087 | assign_stmt = gimple_build_assign (new_data, |
2088 | fold_convert (TREE_TYPE (new_data), param)); | |
2089 | gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT); | |
2090 | } | |
28c92cbb | 2091 | |
31c2b13b | 2092 | /* Emit GIMPLE_OMP_RETURN for GIMPLE_OMP_PARALLEL. */ |
2093 | bb = split_loop_exit_edge (single_dom_exit (loop)); | |
2094 | gsi = gsi_last_bb (bb); | |
2095 | omp_return_stmt1 = gimple_build_omp_return (false); | |
2096 | gimple_set_location (omp_return_stmt1, loc); | |
2097 | gsi_insert_after (&gsi, omp_return_stmt1, GSI_NEW_STMT); | |
2098 | } | |
28c92cbb | 2099 | |
75a70cf9 | 2100 | /* Extract data for GIMPLE_OMP_FOR. */ |
28c92cbb | 2101 | gcc_assert (loop->header == single_dom_exit (loop)->src); |
1a91d914 | 2102 | cond_stmt = as_a <gcond *> (last_stmt (loop->header)); |
28c92cbb | 2103 | |
75a70cf9 | 2104 | cvar = gimple_cond_lhs (cond_stmt); |
28c92cbb | 2105 | cvar_base = SSA_NAME_VAR (cvar); |
2106 | phi = SSA_NAME_DEF_STMT (cvar); | |
2107 | cvar_init = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop)); | |
f9e245b2 | 2108 | initvar = copy_ssa_name (cvar); |
28c92cbb | 2109 | SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, loop_preheader_edge (loop)), |
2110 | initvar); | |
2111 | cvar_next = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop)); | |
2112 | ||
4bcf12c5 | 2113 | gsi = gsi_last_nondebug_bb (loop->latch); |
75a70cf9 | 2114 | gcc_assert (gsi_stmt (gsi) == SSA_NAME_DEF_STMT (cvar_next)); |
2115 | gsi_remove (&gsi, true); | |
28c92cbb | 2116 | |
2117 | /* Prepare cfg. */ | |
2118 | for_bb = split_edge (loop_preheader_edge (loop)); | |
2119 | ex_bb = split_loop_exit_edge (single_dom_exit (loop)); | |
2120 | extract_true_false_edges_from_block (loop->header, &nexit, &exit); | |
2121 | gcc_assert (exit == single_dom_exit (loop)); | |
2122 | ||
2123 | guard = make_edge (for_bb, ex_bb, 0); | |
720cfc43 | 2124 | /* FIXME: What is the probability? */ |
2125 | guard->probability = profile_probability::guessed_never (); | |
86a932e0 | 2126 | /* Split the latch edge, so LOOPS_HAVE_SIMPLE_LATCHES is still valid. */ |
2127 | loop->latch = split_edge (single_succ_edge (loop->latch)); | |
2128 | single_pred_edge (loop->latch)->flags = 0; | |
720cfc43 | 2129 | end = make_single_succ_edge (single_pred (loop->latch), ex_bb, EDGE_FALLTHRU); |
86a932e0 | 2130 | rescan_loop_exit (end, true, false); |
2131 | ||
1a91d914 | 2132 | for (gphi_iterator gpi = gsi_start_phis (ex_bb); |
2133 | !gsi_end_p (gpi); gsi_next (&gpi)) | |
28c92cbb | 2134 | { |
be1e7283 | 2135 | location_t locus; |
1a91d914 | 2136 | gphi *phi = gpi.phi (); |
d1134db8 | 2137 | tree def = PHI_ARG_DEF_FROM_EDGE (phi, exit); |
42acab1c | 2138 | gimple *def_stmt = SSA_NAME_DEF_STMT (def); |
1a91d914 | 2139 | |
d1134db8 | 2140 | /* If the exit phi is not connected to a header phi in the same loop, this |
2141 | value is not modified in the loop, and we're done with this phi. */ | |
2142 | if (!(gimple_code (def_stmt) == GIMPLE_PHI | |
2143 | && gimple_bb (def_stmt) == loop->header)) | |
07d36511 | 2144 | { |
2145 | locus = gimple_phi_arg_location_from_edge (phi, exit); | |
2146 | add_phi_arg (phi, def, guard, locus); | |
2147 | add_phi_arg (phi, def, end, locus); | |
2148 | continue; | |
2149 | } | |
efbcb6de | 2150 | |
d1134db8 | 2151 | gphi *stmt = as_a <gphi *> (def_stmt); |
efbcb6de | 2152 | def = PHI_ARG_DEF_FROM_EDGE (stmt, loop_preheader_edge (loop)); |
48e1416a | 2153 | locus = gimple_phi_arg_location_from_edge (stmt, |
efbcb6de | 2154 | loop_preheader_edge (loop)); |
60d535d2 | 2155 | add_phi_arg (phi, def, guard, locus); |
efbcb6de | 2156 | |
2157 | def = PHI_ARG_DEF_FROM_EDGE (stmt, loop_latch_edge (loop)); | |
2158 | locus = gimple_phi_arg_location_from_edge (stmt, loop_latch_edge (loop)); | |
60d535d2 | 2159 | add_phi_arg (phi, def, end, locus); |
28c92cbb | 2160 | } |
2161 | e = redirect_edge_and_branch (exit, nexit->dest); | |
2162 | PENDING_STMT (e) = NULL; | |
2163 | ||
75a70cf9 | 2164 | /* Emit GIMPLE_OMP_FOR. */ |
31c2b13b | 2165 | if (oacc_kernels_p) |
5dd0c4e8 | 2166 | /* Parallelized OpenACC kernels constructs use gang parallelism. See also |
2167 | omp-offload.c:execute_oacc_device_lower. */ | |
31c2b13b | 2168 | t = build_omp_clause (loc, OMP_CLAUSE_GANG); |
2169 | else | |
2331aa43 | 2170 | { |
31c2b13b | 2171 | t = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE); |
2172 | int chunk_size = PARAM_VALUE (PARAM_PARLOOPS_CHUNK_SIZE); | |
2173 | enum PARAM_PARLOOPS_SCHEDULE_KIND schedule_type \ | |
2174 | = (enum PARAM_PARLOOPS_SCHEDULE_KIND) PARAM_VALUE (PARAM_PARLOOPS_SCHEDULE); | |
2175 | switch (schedule_type) | |
2176 | { | |
2177 | case PARAM_PARLOOPS_SCHEDULE_KIND_static: | |
2178 | OMP_CLAUSE_SCHEDULE_KIND (t) = OMP_CLAUSE_SCHEDULE_STATIC; | |
2179 | break; | |
2180 | case PARAM_PARLOOPS_SCHEDULE_KIND_dynamic: | |
2181 | OMP_CLAUSE_SCHEDULE_KIND (t) = OMP_CLAUSE_SCHEDULE_DYNAMIC; | |
2182 | break; | |
2183 | case PARAM_PARLOOPS_SCHEDULE_KIND_guided: | |
2184 | OMP_CLAUSE_SCHEDULE_KIND (t) = OMP_CLAUSE_SCHEDULE_GUIDED; | |
2185 | break; | |
2186 | case PARAM_PARLOOPS_SCHEDULE_KIND_auto: | |
2187 | OMP_CLAUSE_SCHEDULE_KIND (t) = OMP_CLAUSE_SCHEDULE_AUTO; | |
2188 | chunk_size = 0; | |
2189 | break; | |
2190 | case PARAM_PARLOOPS_SCHEDULE_KIND_runtime: | |
2191 | OMP_CLAUSE_SCHEDULE_KIND (t) = OMP_CLAUSE_SCHEDULE_RUNTIME; | |
2192 | chunk_size = 0; | |
2193 | break; | |
2194 | default: | |
2195 | gcc_unreachable (); | |
2196 | } | |
2197 | if (chunk_size != 0) | |
2198 | OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (t) | |
2199 | = build_int_cst (integer_type_node, chunk_size); | |
2331aa43 | 2200 | } |
28c92cbb | 2201 | |
31c2b13b | 2202 | for_stmt = gimple_build_omp_for (NULL, |
2203 | (oacc_kernels_p | |
2204 | ? GF_OMP_FOR_KIND_OACC_LOOP | |
2205 | : GF_OMP_FOR_KIND_FOR), | |
2206 | t, 1, NULL); | |
2207 | ||
2208 | gimple_cond_set_lhs (cond_stmt, cvar_base); | |
2209 | type = TREE_TYPE (cvar); | |
0aecb55e | 2210 | gimple_set_location (for_stmt, loc); |
75a70cf9 | 2211 | gimple_omp_for_set_index (for_stmt, 0, initvar); |
2212 | gimple_omp_for_set_initial (for_stmt, 0, cvar_init); | |
2213 | gimple_omp_for_set_final (for_stmt, 0, gimple_cond_rhs (cond_stmt)); | |
2214 | gimple_omp_for_set_cond (for_stmt, 0, gimple_cond_code (cond_stmt)); | |
2215 | gimple_omp_for_set_incr (for_stmt, 0, build2 (PLUS_EXPR, type, | |
2216 | cvar_base, | |
2217 | build_int_cst (type, 1))); | |
2218 | ||
2219 | gsi = gsi_last_bb (for_bb); | |
2220 | gsi_insert_after (&gsi, for_stmt, GSI_NEW_STMT); | |
28c92cbb | 2221 | SSA_NAME_DEF_STMT (initvar) = for_stmt; |
2222 | ||
75a70cf9 | 2223 | /* Emit GIMPLE_OMP_CONTINUE. */ |
86a932e0 | 2224 | continue_bb = single_pred (loop->latch); |
2225 | gsi = gsi_last_bb (continue_bb); | |
1a91d914 | 2226 | omp_cont_stmt = gimple_build_omp_continue (cvar_next, cvar); |
2227 | gimple_set_location (omp_cont_stmt, loc); | |
2228 | gsi_insert_after (&gsi, omp_cont_stmt, GSI_NEW_STMT); | |
2229 | SSA_NAME_DEF_STMT (cvar_next) = omp_cont_stmt; | |
28c92cbb | 2230 | |
75a70cf9 | 2231 | /* Emit GIMPLE_OMP_RETURN for GIMPLE_OMP_FOR. */ |
2232 | gsi = gsi_last_bb (ex_bb); | |
1a91d914 | 2233 | omp_return_stmt2 = gimple_build_omp_return (true); |
2234 | gimple_set_location (omp_return_stmt2, loc); | |
2235 | gsi_insert_after (&gsi, omp_return_stmt2, GSI_NEW_STMT); | |
28c92cbb | 2236 | |
821ac701 | 2237 | /* After the above dom info is hosed. Re-compute it. */ |
2238 | free_dominance_info (CDI_DOMINATORS); | |
2239 | calculate_dominance_info (CDI_DOMINATORS); | |
28c92cbb | 2240 | } |
2241 | ||
bfb63268 | 2242 | /* Return number of phis in bb. If COUNT_VIRTUAL_P is false, don't count the |
2243 | virtual phi. */ | |
2244 | ||
2245 | static unsigned int | |
2246 | num_phis (basic_block bb, bool count_virtual_p) | |
2247 | { | |
2248 | unsigned int nr_phis = 0; | |
2249 | gphi_iterator gsi; | |
2250 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
2251 | { | |
2252 | if (!count_virtual_p && virtual_operand_p (PHI_RESULT (gsi.phi ()))) | |
2253 | continue; | |
2254 | ||
2255 | nr_phis++; | |
2256 | } | |
2257 | ||
2258 | return nr_phis; | |
2259 | } | |
2260 | ||
5fa90eea | 2261 | /* Generates code to execute the iterations of LOOP in N_THREADS |
3a37a410 | 2262 | threads in parallel, which can be 0 if that number is to be determined |
2263 | later. | |
5fa90eea | 2264 | |
2265 | NITER describes number of iterations of LOOP. | |
f0b5f617 | 2266 | REDUCTION_LIST describes the reductions existent in the LOOP. */ |
28c92cbb | 2267 | |
2268 | static void | |
2e966e2a | 2269 | gen_parallel_loop (class loop *loop, |
c1f445d2 | 2270 | reduction_info_table_type *reduction_list, |
2e966e2a | 2271 | unsigned n_threads, class tree_niter_desc *niter, |
31c2b13b | 2272 | bool oacc_kernels_p) |
28c92cbb | 2273 | { |
28c92cbb | 2274 | tree many_iterations_cond, type, nit; |
75a70cf9 | 2275 | tree arg_struct, new_arg_struct; |
2276 | gimple_seq stmts; | |
e06f9c34 | 2277 | edge entry, exit; |
cb7f680b | 2278 | struct clsn_data clsn_data; |
0aecb55e | 2279 | location_t loc; |
42acab1c | 2280 | gimple *cond_stmt; |
362dc73c | 2281 | unsigned int m_p_thread=2; |
28c92cbb | 2282 | |
2283 | /* From | |
2284 | ||
2285 | --------------------------------------------------------------------- | |
2286 | loop | |
2287 | { | |
2288 | IV = phi (INIT, IV + STEP) | |
2289 | BODY1; | |
2290 | if (COND) | |
2291 | break; | |
2292 | BODY2; | |
2293 | } | |
2294 | --------------------------------------------------------------------- | |
2295 | ||
2296 | with # of iterations NITER (possibly with MAY_BE_ZERO assumption), | |
2297 | we generate the following code: | |
2298 | ||
2299 | --------------------------------------------------------------------- | |
2300 | ||
2301 | if (MAY_BE_ZERO | |
cb7f680b | 2302 | || NITER < MIN_PER_THREAD * N_THREADS) |
2303 | goto original; | |
28c92cbb | 2304 | |
2305 | BODY1; | |
2306 | store all local loop-invariant variables used in body of the loop to DATA. | |
75a70cf9 | 2307 | GIMPLE_OMP_PARALLEL (OMP_CLAUSE_NUM_THREADS (N_THREADS), LOOPFN, DATA); |
28c92cbb | 2308 | load the variables from DATA. |
75a70cf9 | 2309 | GIMPLE_OMP_FOR (IV = INIT; COND; IV += STEP) (OMP_CLAUSE_SCHEDULE (static)) |
28c92cbb | 2310 | BODY2; |
2311 | BODY1; | |
75a70cf9 | 2312 | GIMPLE_OMP_CONTINUE; |
2313 | GIMPLE_OMP_RETURN -- GIMPLE_OMP_FOR | |
2314 | GIMPLE_OMP_RETURN -- GIMPLE_OMP_PARALLEL | |
28c92cbb | 2315 | goto end; |
2316 | ||
2317 | original: | |
2318 | loop | |
2319 | { | |
2320 | IV = phi (INIT, IV + STEP) | |
2321 | BODY1; | |
2322 | if (COND) | |
2323 | break; | |
2324 | BODY2; | |
2325 | } | |
2326 | ||
2327 | end: | |
2328 | ||
2329 | */ | |
2330 | ||
2331 | /* Create two versions of the loop -- in the old one, we know that the | |
2332 | number of iterations is large enough, and we will transform it into the | |
2333 | loop that will be split to loop_fn, the new one will be used for the | |
2334 | remaining iterations. */ | |
cb7f680b | 2335 | |
362dc73c | 2336 | /* We should compute a better number-of-iterations value for outer loops. |
2337 | That is, if we have | |
2338 | ||
2339 | for (i = 0; i < n; ++i) | |
2340 | for (j = 0; j < m; ++j) | |
2341 | ... | |
2342 | ||
2343 | we should compute nit = n * m, not nit = n. | |
2344 | Also may_be_zero handling would need to be adjusted. */ | |
2345 | ||
28c92cbb | 2346 | type = TREE_TYPE (niter->niter); |
2347 | nit = force_gimple_operand (unshare_expr (niter->niter), &stmts, true, | |
2348 | NULL_TREE); | |
2349 | if (stmts) | |
75a70cf9 | 2350 | gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts); |
28c92cbb | 2351 | |
31c2b13b | 2352 | if (!oacc_kernels_p) |
28c92cbb | 2353 | { |
31c2b13b | 2354 | if (loop->inner) |
2355 | m_p_thread=2; | |
2356 | else | |
2357 | m_p_thread=MIN_PER_THREAD; | |
2358 | ||
3a37a410 | 2359 | gcc_checking_assert (n_threads != 0); |
31c2b13b | 2360 | many_iterations_cond = |
2361 | fold_build2 (GE_EXPR, boolean_type_node, | |
b3687123 | 2362 | nit, build_int_cst (type, m_p_thread * n_threads - 1)); |
31c2b13b | 2363 | |
28c92cbb | 2364 | many_iterations_cond |
31c2b13b | 2365 | = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, |
2366 | invert_truthvalue (unshare_expr (niter->may_be_zero)), | |
2367 | many_iterations_cond); | |
2368 | many_iterations_cond | |
2369 | = force_gimple_operand (many_iterations_cond, &stmts, false, NULL_TREE); | |
28c92cbb | 2370 | if (stmts) |
75a70cf9 | 2371 | gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts); |
31c2b13b | 2372 | if (!is_gimple_condexpr (many_iterations_cond)) |
2373 | { | |
2374 | many_iterations_cond | |
2375 | = force_gimple_operand (many_iterations_cond, &stmts, | |
2376 | true, NULL_TREE); | |
2377 | if (stmts) | |
2378 | gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), | |
2379 | stmts); | |
2380 | } | |
28c92cbb | 2381 | |
31c2b13b | 2382 | initialize_original_copy_tables (); |
28c92cbb | 2383 | |
31c2b13b | 2384 | /* We assume that the loop usually iterates a lot. */ |
31c2b13b | 2385 | loop_version (loop, many_iterations_cond, NULL, |
ca69b069 | 2386 | profile_probability::likely (), |
2387 | profile_probability::unlikely (), | |
2388 | profile_probability::likely (), | |
2389 | profile_probability::unlikely (), true); | |
31c2b13b | 2390 | update_ssa (TODO_update_ssa); |
2391 | free_original_copy_tables (); | |
2392 | } | |
28c92cbb | 2393 | |
2394 | /* Base all the induction variables in LOOP on a single control one. */ | |
0207206d | 2395 | canonicalize_loop_ivs (loop, &nit, true); |
bfb63268 | 2396 | if (num_phis (loop->header, false) != reduction_list->elements () + 1) |
2397 | { | |
2398 | /* The call to canonicalize_loop_ivs above failed to "base all the | |
2399 | induction variables in LOOP on a single control one". Do damage | |
2400 | control. */ | |
2401 | basic_block preheader = loop_preheader_edge (loop)->src; | |
2402 | basic_block cond_bb = single_pred (preheader); | |
2403 | gcond *cond = as_a <gcond *> (gsi_stmt (gsi_last_bb (cond_bb))); | |
2404 | gimple_cond_make_true (cond); | |
2405 | update_stmt (cond); | |
2406 | /* We've gotten rid of the duplicate loop created by loop_version, but | |
2407 | we can't undo whatever canonicalize_loop_ivs has done. | |
2408 | TODO: Fix this properly by ensuring that the call to | |
2409 | canonicalize_loop_ivs succeeds. */ | |
2410 | if (dump_file | |
2411 | && (dump_flags & TDF_DETAILS)) | |
2412 | fprintf (dump_file, "canonicalize_loop_ivs failed for loop %d," | |
2413 | " aborting transformation\n", loop->num); | |
2414 | return; | |
2415 | } | |
28c92cbb | 2416 | |
d38409cd | 2417 | /* Ensure that the exit condition is the first statement in the loop. |
2418 | The common case is that latch of the loop is empty (apart from the | |
2419 | increment) and immediately follows the loop exit test. Attempt to move the | |
2420 | entry of the loop directly before the exit check and increase the number of | |
2421 | iterations of the loop by one. */ | |
91d14856 | 2422 | if (try_transform_to_exit_first_loop_alt (loop, reduction_list, nit)) |
2423 | { | |
2424 | if (dump_file | |
2425 | && (dump_flags & TDF_DETAILS)) | |
2426 | fprintf (dump_file, | |
2427 | "alternative exit-first loop transform succeeded" | |
2428 | " for loop %d\n", loop->num); | |
2429 | } | |
2430 | else | |
d38409cd | 2431 | { |
31c2b13b | 2432 | if (oacc_kernels_p) |
2433 | n_threads = 1; | |
2434 | ||
d38409cd | 2435 | /* Fall back on the method that handles more cases, but duplicates the |
2436 | loop body: move the exit condition of LOOP to the beginning of its | |
2437 | header, and duplicate the part of the last iteration that gets disabled | |
2438 | to the exit of the loop. */ | |
2439 | transform_to_exit_first_loop (loop, reduction_list, nit); | |
2440 | } | |
cb7f680b | 2441 | |
f0b5f617 | 2442 | /* Generate initializations for reductions. */ |
9a78b979 | 2443 | if (!reduction_list->is_empty ()) |
2e966e2a | 2444 | reduction_list->traverse <class loop *, initialize_reductions> (loop); |
28c92cbb | 2445 | |
2446 | /* Eliminate the references to local variables from the loop. */ | |
e06f9c34 | 2447 | gcc_assert (single_exit (loop)); |
2448 | entry = loop_preheader_edge (loop); | |
2449 | exit = single_dom_exit (loop); | |
28c92cbb | 2450 | |
31c2b13b | 2451 | /* This rewrites the body in terms of new variables. This has already |
2452 | been done for oacc_kernels_p in pass_lower_omp/lower_omp (). */ | |
2453 | if (!oacc_kernels_p) | |
2454 | { | |
2455 | eliminate_local_variables (entry, exit); | |
2456 | /* In the old loop, move all variables non-local to the loop to a | |
2457 | structure and back, and create separate decls for the variables used in | |
2458 | loop. */ | |
2459 | separate_decls_in_region (entry, exit, reduction_list, &arg_struct, | |
2460 | &new_arg_struct, &clsn_data); | |
2461 | } | |
2462 | else | |
2463 | { | |
2464 | arg_struct = NULL_TREE; | |
2465 | new_arg_struct = NULL_TREE; | |
2466 | clsn_data.load = NULL_TREE; | |
2467 | clsn_data.load_bb = exit->dest; | |
2468 | clsn_data.store = NULL_TREE; | |
2469 | clsn_data.store_bb = NULL; | |
2470 | } | |
28c92cbb | 2471 | |
2472 | /* Create the parallel constructs. */ | |
0aecb55e | 2473 | loc = UNKNOWN_LOCATION; |
2474 | cond_stmt = last_stmt (loop->header); | |
2475 | if (cond_stmt) | |
2476 | loc = gimple_location (cond_stmt); | |
31c2b13b | 2477 | create_parallel_loop (loop, create_loop_fn (loc), arg_struct, new_arg_struct, |
2478 | n_threads, loc, oacc_kernels_p); | |
9a78b979 | 2479 | if (!reduction_list->is_empty ()) |
cb7f680b | 2480 | create_call_for_reduction (loop, reduction_list, &clsn_data); |
28c92cbb | 2481 | |
2482 | scev_reset (); | |
2483 | ||
d46d3c1c | 2484 | /* Free loop bound estimations that could contain references to |
2485 | removed statements. */ | |
46480a95 | 2486 | free_numbers_of_iterations_estimates (cfun); |
28c92cbb | 2487 | } |
2488 | ||
c968a07c | 2489 | /* Returns true when LOOP contains vector phi nodes. */ |
2490 | ||
2491 | static bool | |
2e966e2a | 2492 | loop_has_vector_phi_nodes (class loop *loop ATTRIBUTE_UNUSED) |
c968a07c | 2493 | { |
2494 | unsigned i; | |
2495 | basic_block *bbs = get_loop_body_in_dom_order (loop); | |
1a91d914 | 2496 | gphi_iterator gsi; |
c968a07c | 2497 | bool res = true; |
c968a07c | 2498 | |
2499 | for (i = 0; i < loop->num_nodes; i++) | |
75a70cf9 | 2500 | for (gsi = gsi_start_phis (bbs[i]); !gsi_end_p (gsi); gsi_next (&gsi)) |
1a91d914 | 2501 | if (TREE_CODE (TREE_TYPE (PHI_RESULT (gsi.phi ()))) == VECTOR_TYPE) |
c968a07c | 2502 | goto end; |
2503 | ||
2504 | res = false; | |
2505 | end: | |
2506 | free (bbs); | |
2507 | return res; | |
2508 | } | |
2509 | ||
5fa90eea | 2510 | /* Create a reduction_info struct, initialize it with REDUC_STMT |
2511 | and PHI, insert it to the REDUCTION_LIST. */ | |
2512 | ||
2513 | static void | |
c1f445d2 | 2514 | build_new_reduction (reduction_info_table_type *reduction_list, |
42acab1c | 2515 | gimple *reduc_stmt, gphi *phi) |
5fa90eea | 2516 | { |
d9dd21a8 | 2517 | reduction_info **slot; |
5fa90eea | 2518 | struct reduction_info *new_reduction; |
95f4166a | 2519 | enum tree_code reduction_code; |
5fa90eea | 2520 | |
2521 | gcc_assert (reduc_stmt); | |
48e1416a | 2522 | |
95f4166a | 2523 | if (gimple_code (reduc_stmt) == GIMPLE_PHI) |
2524 | { | |
2525 | tree op1 = PHI_ARG_DEF (reduc_stmt, 0); | |
42acab1c | 2526 | gimple *def1 = SSA_NAME_DEF_STMT (op1); |
95f4166a | 2527 | reduction_code = gimple_assign_rhs_code (def1); |
2528 | } | |
95f4166a | 2529 | else |
2530 | reduction_code = gimple_assign_rhs_code (reduc_stmt); | |
3f109986 | 2531 | /* Check for OpenMP supported reduction. */ |
2532 | switch (reduction_code) | |
2533 | { | |
2534 | case PLUS_EXPR: | |
2535 | case MULT_EXPR: | |
2536 | case MAX_EXPR: | |
2537 | case MIN_EXPR: | |
2538 | case BIT_IOR_EXPR: | |
2539 | case BIT_XOR_EXPR: | |
2540 | case BIT_AND_EXPR: | |
2541 | case TRUTH_OR_EXPR: | |
2542 | case TRUTH_XOR_EXPR: | |
2543 | case TRUTH_AND_EXPR: | |
2544 | break; | |
2545 | default: | |
2546 | return; | |
2547 | } | |
2548 | ||
2549 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2550 | { | |
2551 | fprintf (dump_file, | |
2552 | "Detected reduction. reduction stmt is:\n"); | |
2553 | print_gimple_stmt (dump_file, reduc_stmt, 0); | |
2554 | fprintf (dump_file, "\n"); | |
2555 | } | |
95f4166a | 2556 | |
5fa90eea | 2557 | new_reduction = XCNEW (struct reduction_info); |
48e1416a | 2558 | |
5fa90eea | 2559 | new_reduction->reduc_stmt = reduc_stmt; |
2560 | new_reduction->reduc_phi = phi; | |
71fa519d | 2561 | new_reduction->reduc_version = SSA_NAME_VERSION (gimple_phi_result (phi)); |
95f4166a | 2562 | new_reduction->reduction_code = reduction_code; |
c1f445d2 | 2563 | slot = reduction_list->find_slot (new_reduction, INSERT); |
5fa90eea | 2564 | *slot = new_reduction; |
2565 | } | |
2566 | ||
71fa519d | 2567 | /* Callback for htab_traverse. Sets gimple_uid of reduc_phi stmts. */ |
2568 | ||
d9dd21a8 | 2569 | int |
2570 | set_reduc_phi_uids (reduction_info **slot, void *data ATTRIBUTE_UNUSED) | |
71fa519d | 2571 | { |
d9dd21a8 | 2572 | struct reduction_info *const red = *slot; |
71fa519d | 2573 | gimple_set_uid (red->reduc_phi, red->reduc_version); |
2574 | return 1; | |
2575 | } | |
2576 | ||
f4649a92 | 2577 | /* Return true if the type of reduction performed by STMT_INFO is suitable |
d77809a4 | 2578 | for this pass. */ |
2579 | ||
2580 | static bool | |
f4649a92 | 2581 | valid_reduction_p (stmt_vec_info stmt_info) |
d77809a4 | 2582 | { |
2583 | /* Parallelization would reassociate the operation, which isn't | |
2584 | allowed for in-order reductions. */ | |
d77809a4 | 2585 | vect_reduction_type reduc_type = STMT_VINFO_REDUC_TYPE (stmt_info); |
2586 | return reduc_type != FOLD_LEFT_REDUCTION; | |
2587 | } | |
2588 | ||
5fa90eea | 2589 | /* Detect all reductions in the LOOP, insert them into REDUCTION_LIST. */ |
2590 | ||
2591 | static void | |
c1f445d2 | 2592 | gather_scalar_reductions (loop_p loop, reduction_info_table_type *reduction_list) |
5fa90eea | 2593 | { |
1a91d914 | 2594 | gphi_iterator gsi; |
5fa90eea | 2595 | loop_vec_info simple_loop_info; |
11022ec4 | 2596 | auto_vec<gphi *, 4> double_reduc_phis; |
2597 | auto_vec<gimple *, 4> double_reduc_stmts; | |
5fa90eea | 2598 | |
a99aba41 | 2599 | vec_info_shared shared; |
2600 | simple_loop_info = vect_analyze_loop_form (loop, &shared); | |
81fbee06 | 2601 | if (simple_loop_info == NULL) |
593bddd3 | 2602 | goto gather_done; |
5fa90eea | 2603 | |
2604 | for (gsi = gsi_start_phis (loop->header); !gsi_end_p (gsi); gsi_next (&gsi)) | |
2605 | { | |
1a91d914 | 2606 | gphi *phi = gsi.phi (); |
5fa90eea | 2607 | affine_iv iv; |
2608 | tree res = PHI_RESULT (phi); | |
2609 | bool double_reduc; | |
2610 | ||
7c782c9b | 2611 | if (virtual_operand_p (res)) |
5fa90eea | 2612 | continue; |
2613 | ||
81fbee06 | 2614 | if (simple_iv (loop, loop, res, &iv, true)) |
2615 | continue; | |
2616 | ||
f4649a92 | 2617 | stmt_vec_info reduc_stmt_info |
2618 | = vect_force_simple_reduction (simple_loop_info, | |
2619 | simple_loop_info->lookup_stmt (phi), | |
81fbee06 | 2620 | &double_reduc, true); |
f4649a92 | 2621 | if (!reduc_stmt_info || !valid_reduction_p (reduc_stmt_info)) |
81fbee06 | 2622 | continue; |
2623 | ||
95f4166a | 2624 | if (double_reduc) |
2625 | { | |
11022ec4 | 2626 | if (loop->inner->inner != NULL) |
95f4166a | 2627 | continue; |
2628 | ||
11022ec4 | 2629 | double_reduc_phis.safe_push (phi); |
f4649a92 | 2630 | double_reduc_stmts.safe_push (reduc_stmt_info->stmt); |
11022ec4 | 2631 | continue; |
95f4166a | 2632 | } |
2633 | ||
f4649a92 | 2634 | build_new_reduction (reduction_list, reduc_stmt_info->stmt, phi); |
5fa90eea | 2635 | } |
e15e8a2a | 2636 | delete simple_loop_info; |
11022ec4 | 2637 | |
2638 | if (!double_reduc_phis.is_empty ()) | |
2639 | { | |
a99aba41 | 2640 | vec_info_shared shared; |
2641 | simple_loop_info = vect_analyze_loop_form (loop->inner, &shared); | |
11022ec4 | 2642 | if (simple_loop_info) |
2643 | { | |
2644 | gphi *phi; | |
2645 | unsigned int i; | |
2646 | ||
2647 | FOR_EACH_VEC_ELT (double_reduc_phis, i, phi) | |
2648 | { | |
2649 | affine_iv iv; | |
2650 | tree res = PHI_RESULT (phi); | |
2651 | bool double_reduc; | |
2652 | ||
2653 | use_operand_p use_p; | |
2654 | gimple *inner_stmt; | |
2655 | bool single_use_p = single_imm_use (res, &use_p, &inner_stmt); | |
2656 | gcc_assert (single_use_p); | |
2657 | if (gimple_code (inner_stmt) != GIMPLE_PHI) | |
2658 | continue; | |
2659 | gphi *inner_phi = as_a <gphi *> (inner_stmt); | |
2660 | if (simple_iv (loop->inner, loop->inner, PHI_RESULT (inner_phi), | |
2661 | &iv, true)) | |
2662 | continue; | |
2663 | ||
f4649a92 | 2664 | stmt_vec_info inner_phi_info |
2665 | = simple_loop_info->lookup_stmt (inner_phi); | |
2666 | stmt_vec_info inner_reduc_stmt_info | |
2667 | = vect_force_simple_reduction (simple_loop_info, | |
2668 | inner_phi_info, | |
119a8852 | 2669 | &double_reduc, true); |
11022ec4 | 2670 | gcc_assert (!double_reduc); |
f4649a92 | 2671 | if (!inner_reduc_stmt_info |
2672 | || !valid_reduction_p (inner_reduc_stmt_info)) | |
11022ec4 | 2673 | continue; |
2674 | ||
2675 | build_new_reduction (reduction_list, double_reduc_stmts[i], phi); | |
2676 | } | |
e15e8a2a | 2677 | delete simple_loop_info; |
11022ec4 | 2678 | } |
2679 | } | |
71fa519d | 2680 | |
593bddd3 | 2681 | gather_done: |
9a78b979 | 2682 | if (reduction_list->is_empty ()) |
593bddd3 | 2683 | return; |
2684 | ||
71fa519d | 2685 | /* As gimple_uid is used by the vectorizer in between vect_analyze_loop_form |
c626a338 | 2686 | and delete simple_loop_info, we can set gimple_uid of reduc_phi stmts only |
eac984fd | 2687 | now. */ |
2688 | basic_block bb; | |
2689 | FOR_EACH_BB_FN (bb, cfun) | |
2690 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
2691 | gimple_set_uid (gsi_stmt (gsi), (unsigned int)-1); | |
c1f445d2 | 2692 | reduction_list->traverse <void *, set_reduc_phi_uids> (NULL); |
5fa90eea | 2693 | } |
2694 | ||
2695 | /* Try to initialize NITER for code generation part. */ | |
2696 | ||
2697 | static bool | |
2e966e2a | 2698 | try_get_loop_niter (loop_p loop, class tree_niter_desc *niter) |
5fa90eea | 2699 | { |
2700 | edge exit = single_dom_exit (loop); | |
2701 | ||
2702 | gcc_assert (exit); | |
2703 | ||
2704 | /* We need to know # of iterations, and there should be no uses of values | |
2705 | defined inside loop outside of it, unless the values are invariants of | |
2706 | the loop. */ | |
2707 | if (!number_of_iterations_exit (loop, exit, niter, false)) | |
2708 | { | |
2709 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2710 | fprintf (dump_file, " FAILED: number of iterations not known\n"); | |
2711 | return false; | |
2712 | } | |
2713 | ||
2714 | return true; | |
2715 | } | |
2716 | ||
31c2b13b | 2717 | /* Return the default def of the first function argument. */ |
2718 | ||
2719 | static tree | |
2720 | get_omp_data_i_param (void) | |
2721 | { | |
2722 | tree decl = DECL_ARGUMENTS (cfun->decl); | |
2723 | gcc_assert (DECL_CHAIN (decl) == NULL_TREE); | |
2724 | return ssa_default_def (cfun, decl); | |
2725 | } | |
2726 | ||
2727 | /* For PHI in loop header of LOOP, look for pattern: | |
2728 | ||
2729 | <bb preheader> | |
2730 | .omp_data_i = &.omp_data_arr; | |
2731 | addr = .omp_data_i->sum; | |
2732 | sum_a = *addr; | |
2733 | ||
2734 | <bb header>: | |
2735 | sum_b = PHI <sum_a (preheader), sum_c (latch)> | |
2736 | ||
2737 | and return addr. Otherwise, return NULL_TREE. */ | |
2738 | ||
2739 | static tree | |
2e966e2a | 2740 | find_reduc_addr (class loop *loop, gphi *phi) |
31c2b13b | 2741 | { |
2742 | edge e = loop_preheader_edge (loop); | |
2743 | tree arg = PHI_ARG_DEF_FROM_EDGE (phi, e); | |
2744 | gimple *stmt = SSA_NAME_DEF_STMT (arg); | |
2745 | if (!gimple_assign_single_p (stmt)) | |
2746 | return NULL_TREE; | |
2747 | tree memref = gimple_assign_rhs1 (stmt); | |
2748 | if (TREE_CODE (memref) != MEM_REF) | |
2749 | return NULL_TREE; | |
2750 | tree addr = TREE_OPERAND (memref, 0); | |
2751 | ||
2752 | gimple *stmt2 = SSA_NAME_DEF_STMT (addr); | |
2753 | if (!gimple_assign_single_p (stmt2)) | |
2754 | return NULL_TREE; | |
2755 | tree compref = gimple_assign_rhs1 (stmt2); | |
2756 | if (TREE_CODE (compref) != COMPONENT_REF) | |
2757 | return NULL_TREE; | |
2758 | tree addr2 = TREE_OPERAND (compref, 0); | |
2759 | if (TREE_CODE (addr2) != MEM_REF) | |
2760 | return NULL_TREE; | |
2761 | addr2 = TREE_OPERAND (addr2, 0); | |
2762 | if (TREE_CODE (addr2) != SSA_NAME | |
2763 | || addr2 != get_omp_data_i_param ()) | |
2764 | return NULL_TREE; | |
2765 | ||
2766 | return addr; | |
2767 | } | |
2768 | ||
5fa90eea | 2769 | /* Try to initialize REDUCTION_LIST for code generation part. |
2770 | REDUCTION_LIST describes the reductions. */ | |
2771 | ||
2772 | static bool | |
d9dd21a8 | 2773 | try_create_reduction_list (loop_p loop, |
31c2b13b | 2774 | reduction_info_table_type *reduction_list, |
2775 | bool oacc_kernels_p) | |
5fa90eea | 2776 | { |
2777 | edge exit = single_dom_exit (loop); | |
1a91d914 | 2778 | gphi_iterator gsi; |
5fa90eea | 2779 | |
2780 | gcc_assert (exit); | |
2781 | ||
9e75a9b9 | 2782 | /* Try to get rid of exit phis. */ |
2783 | final_value_replacement_loop (loop); | |
2784 | ||
5fa90eea | 2785 | gather_scalar_reductions (loop, reduction_list); |
2786 | ||
48e1416a | 2787 | |
5fa90eea | 2788 | for (gsi = gsi_start_phis (exit->dest); !gsi_end_p (gsi); gsi_next (&gsi)) |
2789 | { | |
1a91d914 | 2790 | gphi *phi = gsi.phi (); |
5fa90eea | 2791 | struct reduction_info *red; |
2792 | imm_use_iterator imm_iter; | |
2793 | use_operand_p use_p; | |
42acab1c | 2794 | gimple *reduc_phi; |
5fa90eea | 2795 | tree val = PHI_ARG_DEF_FROM_EDGE (phi, exit); |
2796 | ||
3f7d0abc | 2797 | if (!virtual_operand_p (val)) |
5fa90eea | 2798 | { |
3f7d0abc | 2799 | if (TREE_CODE (val) != SSA_NAME) |
2800 | { | |
2801 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2802 | fprintf (dump_file, | |
2803 | " FAILED: exit PHI argument invariant.\n"); | |
2804 | return false; | |
2805 | } | |
2806 | ||
5fa90eea | 2807 | if (dump_file && (dump_flags & TDF_DETAILS)) |
2808 | { | |
2809 | fprintf (dump_file, "phi is "); | |
1ffa4346 | 2810 | print_gimple_stmt (dump_file, phi, 0); |
5fa90eea | 2811 | fprintf (dump_file, "arg of phi to exit: value "); |
1ffa4346 | 2812 | print_generic_expr (dump_file, val); |
5fa90eea | 2813 | fprintf (dump_file, " used outside loop\n"); |
2814 | fprintf (dump_file, | |
7a7837a1 | 2815 | " checking if it is part of reduction pattern:\n"); |
5fa90eea | 2816 | } |
9a78b979 | 2817 | if (reduction_list->is_empty ()) |
5fa90eea | 2818 | { |
2819 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2820 | fprintf (dump_file, | |
2821 | " FAILED: it is not a part of reduction.\n"); | |
2822 | return false; | |
2823 | } | |
2824 | reduc_phi = NULL; | |
2825 | FOR_EACH_IMM_USE_FAST (use_p, imm_iter, val) | |
2826 | { | |
43989e16 | 2827 | if (!gimple_debug_bind_p (USE_STMT (use_p)) |
2828 | && flow_bb_inside_loop_p (loop, gimple_bb (USE_STMT (use_p)))) | |
5fa90eea | 2829 | { |
2830 | reduc_phi = USE_STMT (use_p); | |
2831 | break; | |
2832 | } | |
2833 | } | |
2834 | red = reduction_phi (reduction_list, reduc_phi); | |
2835 | if (red == NULL) | |
2836 | { | |
2837 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2838 | fprintf (dump_file, | |
2839 | " FAILED: it is not a part of reduction.\n"); | |
2840 | return false; | |
2841 | } | |
d95e1856 | 2842 | if (red->keep_res != NULL) |
2843 | { | |
2844 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2845 | fprintf (dump_file, | |
2846 | " FAILED: reduction has multiple exit phis.\n"); | |
2847 | return false; | |
2848 | } | |
2849 | red->keep_res = phi; | |
5fa90eea | 2850 | if (dump_file && (dump_flags & TDF_DETAILS)) |
2851 | { | |
2852 | fprintf (dump_file, "reduction phi is "); | |
1ffa4346 | 2853 | print_gimple_stmt (dump_file, red->reduc_phi, 0); |
5fa90eea | 2854 | fprintf (dump_file, "reduction stmt is "); |
1ffa4346 | 2855 | print_gimple_stmt (dump_file, red->reduc_stmt, 0); |
5fa90eea | 2856 | } |
2857 | } | |
2858 | } | |
2859 | ||
2860 | /* The iterations of the loop may communicate only through bivs whose | |
2861 | iteration space can be distributed efficiently. */ | |
2862 | for (gsi = gsi_start_phis (loop->header); !gsi_end_p (gsi); gsi_next (&gsi)) | |
2863 | { | |
1a91d914 | 2864 | gphi *phi = gsi.phi (); |
5fa90eea | 2865 | tree def = PHI_RESULT (phi); |
2866 | affine_iv iv; | |
2867 | ||
7c782c9b | 2868 | if (!virtual_operand_p (def) && !simple_iv (loop, loop, def, &iv, true)) |
5fa90eea | 2869 | { |
2870 | struct reduction_info *red; | |
2871 | ||
2872 | red = reduction_phi (reduction_list, phi); | |
2873 | if (red == NULL) | |
2874 | { | |
2875 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2876 | fprintf (dump_file, | |
2877 | " FAILED: scalar dependency between iterations\n"); | |
2878 | return false; | |
2879 | } | |
2880 | } | |
2881 | } | |
2882 | ||
31c2b13b | 2883 | if (oacc_kernels_p) |
2884 | { | |
2885 | for (gsi = gsi_start_phis (loop->header); !gsi_end_p (gsi); | |
2886 | gsi_next (&gsi)) | |
2887 | { | |
2888 | gphi *phi = gsi.phi (); | |
2889 | tree def = PHI_RESULT (phi); | |
2890 | affine_iv iv; | |
2891 | ||
2892 | if (!virtual_operand_p (def) | |
2893 | && !simple_iv (loop, loop, def, &iv, true)) | |
2894 | { | |
2895 | tree addr = find_reduc_addr (loop, phi); | |
2896 | if (addr == NULL_TREE) | |
2897 | return false; | |
2898 | struct reduction_info *red = reduction_phi (reduction_list, phi); | |
2899 | red->reduc_addr = addr; | |
2900 | } | |
2901 | } | |
2902 | } | |
5fa90eea | 2903 | |
2904 | return true; | |
2905 | } | |
2906 | ||
a006c0bb | 2907 | /* Return true if LOOP contains phis with ADDR_EXPR in args. */ |
2908 | ||
2909 | static bool | |
2e966e2a | 2910 | loop_has_phi_with_address_arg (class loop *loop) |
a006c0bb | 2911 | { |
2912 | basic_block *bbs = get_loop_body (loop); | |
2913 | bool res = false; | |
2914 | ||
2915 | unsigned i, j; | |
2916 | gphi_iterator gsi; | |
2917 | for (i = 0; i < loop->num_nodes; i++) | |
2918 | for (gsi = gsi_start_phis (bbs[i]); !gsi_end_p (gsi); gsi_next (&gsi)) | |
2919 | { | |
2920 | gphi *phi = gsi.phi (); | |
2921 | for (j = 0; j < gimple_phi_num_args (phi); j++) | |
2922 | { | |
2923 | tree arg = gimple_phi_arg_def (phi, j); | |
2924 | if (TREE_CODE (arg) == ADDR_EXPR) | |
2925 | { | |
2926 | /* This should be handled by eliminate_local_variables, but that | |
2927 | function currently ignores phis. */ | |
2928 | res = true; | |
2929 | goto end; | |
2930 | } | |
2931 | } | |
2932 | } | |
2933 | end: | |
2934 | free (bbs); | |
31c2b13b | 2935 | |
2936 | return res; | |
2937 | } | |
2938 | ||
2939 | /* Return true if memory ref REF (corresponding to the stmt at GSI in | |
2940 | REGIONS_BB[I]) conflicts with the statements in REGIONS_BB[I] after gsi, | |
2941 | or the statements in REGIONS_BB[I + n]. REF_IS_STORE indicates if REF is a | |
2942 | store. Ignore conflicts with SKIP_STMT. */ | |
2943 | ||
2944 | static bool | |
2945 | ref_conflicts_with_region (gimple_stmt_iterator gsi, ao_ref *ref, | |
2946 | bool ref_is_store, vec<basic_block> region_bbs, | |
2947 | unsigned int i, gimple *skip_stmt) | |
2948 | { | |
2949 | basic_block bb = region_bbs[i]; | |
2950 | gsi_next (&gsi); | |
2951 | ||
2952 | while (true) | |
2953 | { | |
2954 | for (; !gsi_end_p (gsi); | |
2955 | gsi_next (&gsi)) | |
2956 | { | |
2957 | gimple *stmt = gsi_stmt (gsi); | |
2958 | if (stmt == skip_stmt) | |
2959 | { | |
2960 | if (dump_file) | |
2961 | { | |
2962 | fprintf (dump_file, "skipping reduction store: "); | |
1ffa4346 | 2963 | print_gimple_stmt (dump_file, stmt, 0); |
31c2b13b | 2964 | } |
2965 | continue; | |
2966 | } | |
2967 | ||
2968 | if (!gimple_vdef (stmt) | |
2969 | && !gimple_vuse (stmt)) | |
2970 | continue; | |
2971 | ||
2972 | if (gimple_code (stmt) == GIMPLE_RETURN) | |
2973 | continue; | |
2974 | ||
2975 | if (ref_is_store) | |
2976 | { | |
2977 | if (ref_maybe_used_by_stmt_p (stmt, ref)) | |
2978 | { | |
2979 | if (dump_file) | |
2980 | { | |
2981 | fprintf (dump_file, "Stmt "); | |
1ffa4346 | 2982 | print_gimple_stmt (dump_file, stmt, 0); |
31c2b13b | 2983 | } |
2984 | return true; | |
2985 | } | |
2986 | } | |
2987 | else | |
2988 | { | |
2989 | if (stmt_may_clobber_ref_p_1 (stmt, ref)) | |
2990 | { | |
2991 | if (dump_file) | |
2992 | { | |
2993 | fprintf (dump_file, "Stmt "); | |
1ffa4346 | 2994 | print_gimple_stmt (dump_file, stmt, 0); |
31c2b13b | 2995 | } |
2996 | return true; | |
2997 | } | |
2998 | } | |
2999 | } | |
3000 | i++; | |
3001 | if (i == region_bbs.length ()) | |
3002 | break; | |
3003 | bb = region_bbs[i]; | |
3004 | gsi = gsi_start_bb (bb); | |
3005 | } | |
3006 | ||
3007 | return false; | |
3008 | } | |
3009 | ||
3010 | /* Return true if the bbs in REGION_BBS but not in in_loop_bbs can be executed | |
3011 | in parallel with REGION_BBS containing the loop. Return the stores of | |
3012 | reduction results in REDUCTION_STORES. */ | |
3013 | ||
3014 | static bool | |
3015 | oacc_entry_exit_ok_1 (bitmap in_loop_bbs, vec<basic_block> region_bbs, | |
3016 | reduction_info_table_type *reduction_list, | |
3017 | bitmap reduction_stores) | |
3018 | { | |
3019 | tree omp_data_i = get_omp_data_i_param (); | |
3020 | ||
3021 | unsigned i; | |
3022 | basic_block bb; | |
3023 | FOR_EACH_VEC_ELT (region_bbs, i, bb) | |
3024 | { | |
3025 | if (bitmap_bit_p (in_loop_bbs, bb->index)) | |
3026 | continue; | |
3027 | ||
3028 | gimple_stmt_iterator gsi; | |
3029 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); | |
3030 | gsi_next (&gsi)) | |
3031 | { | |
3032 | gimple *stmt = gsi_stmt (gsi); | |
3033 | gimple *skip_stmt = NULL; | |
3034 | ||
3035 | if (is_gimple_debug (stmt) | |
3036 | || gimple_code (stmt) == GIMPLE_COND) | |
3037 | continue; | |
3038 | ||
3039 | ao_ref ref; | |
3040 | bool ref_is_store = false; | |
3041 | if (gimple_assign_load_p (stmt)) | |
3042 | { | |
3043 | tree rhs = gimple_assign_rhs1 (stmt); | |
3044 | tree base = get_base_address (rhs); | |
3045 | if (TREE_CODE (base) == MEM_REF | |
3046 | && operand_equal_p (TREE_OPERAND (base, 0), omp_data_i, 0)) | |
3047 | continue; | |
3048 | ||
3049 | tree lhs = gimple_assign_lhs (stmt); | |
3050 | if (TREE_CODE (lhs) == SSA_NAME | |
3051 | && has_single_use (lhs)) | |
3052 | { | |
3053 | use_operand_p use_p; | |
3054 | gimple *use_stmt; | |
41d384c5 | 3055 | struct reduction_info *red; |
31c2b13b | 3056 | single_imm_use (lhs, &use_p, &use_stmt); |
41d384c5 | 3057 | if (gimple_code (use_stmt) == GIMPLE_PHI |
3058 | && (red = reduction_phi (reduction_list, use_stmt))) | |
31c2b13b | 3059 | { |
31c2b13b | 3060 | tree val = PHI_RESULT (red->keep_res); |
3061 | if (has_single_use (val)) | |
3062 | { | |
3063 | single_imm_use (val, &use_p, &use_stmt); | |
3064 | if (gimple_store_p (use_stmt)) | |
3065 | { | |
3066 | unsigned int id | |
3067 | = SSA_NAME_VERSION (gimple_vdef (use_stmt)); | |
3068 | bitmap_set_bit (reduction_stores, id); | |
3069 | skip_stmt = use_stmt; | |
3070 | if (dump_file) | |
3071 | { | |
3072 | fprintf (dump_file, "found reduction load: "); | |
1ffa4346 | 3073 | print_gimple_stmt (dump_file, stmt, 0); |
31c2b13b | 3074 | } |
3075 | } | |
3076 | } | |
3077 | } | |
3078 | } | |
3079 | ||
3080 | ao_ref_init (&ref, rhs); | |
3081 | } | |
3082 | else if (gimple_store_p (stmt)) | |
3083 | { | |
3084 | ao_ref_init (&ref, gimple_assign_lhs (stmt)); | |
3085 | ref_is_store = true; | |
3086 | } | |
3087 | else if (gimple_code (stmt) == GIMPLE_OMP_RETURN) | |
3088 | continue; | |
3089 | else if (!gimple_has_side_effects (stmt) | |
3090 | && !gimple_could_trap_p (stmt) | |
aac19106 | 3091 | && !stmt_could_throw_p (cfun, stmt) |
31c2b13b | 3092 | && !gimple_vdef (stmt) |
3093 | && !gimple_vuse (stmt)) | |
3094 | continue; | |
7408cd7d | 3095 | else if (gimple_call_internal_p (stmt, IFN_GOACC_DIM_POS)) |
31c2b13b | 3096 | continue; |
3097 | else if (gimple_code (stmt) == GIMPLE_RETURN) | |
3098 | continue; | |
3099 | else | |
3100 | { | |
3101 | if (dump_file) | |
3102 | { | |
3103 | fprintf (dump_file, "Unhandled stmt in entry/exit: "); | |
1ffa4346 | 3104 | print_gimple_stmt (dump_file, stmt, 0); |
31c2b13b | 3105 | } |
3106 | return false; | |
3107 | } | |
3108 | ||
3109 | if (ref_conflicts_with_region (gsi, &ref, ref_is_store, region_bbs, | |
3110 | i, skip_stmt)) | |
3111 | { | |
3112 | if (dump_file) | |
3113 | { | |
3114 | fprintf (dump_file, "conflicts with entry/exit stmt: "); | |
1ffa4346 | 3115 | print_gimple_stmt (dump_file, stmt, 0); |
31c2b13b | 3116 | } |
3117 | return false; | |
3118 | } | |
3119 | } | |
3120 | } | |
3121 | ||
3122 | return true; | |
3123 | } | |
3124 | ||
3125 | /* Find stores inside REGION_BBS and outside IN_LOOP_BBS, and guard them with | |
3126 | gang_pos == 0, except when the stores are REDUCTION_STORES. Return true | |
3127 | if any changes were made. */ | |
3128 | ||
3129 | static bool | |
3130 | oacc_entry_exit_single_gang (bitmap in_loop_bbs, vec<basic_block> region_bbs, | |
3131 | bitmap reduction_stores) | |
3132 | { | |
3133 | tree gang_pos = NULL_TREE; | |
3134 | bool changed = false; | |
3135 | ||
3136 | unsigned i; | |
3137 | basic_block bb; | |
3138 | FOR_EACH_VEC_ELT (region_bbs, i, bb) | |
3139 | { | |
3140 | if (bitmap_bit_p (in_loop_bbs, bb->index)) | |
3141 | continue; | |
3142 | ||
3143 | gimple_stmt_iterator gsi; | |
3144 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);) | |
3145 | { | |
3146 | gimple *stmt = gsi_stmt (gsi); | |
3147 | ||
3148 | if (!gimple_store_p (stmt)) | |
3149 | { | |
3150 | /* Update gsi to point to next stmt. */ | |
3151 | gsi_next (&gsi); | |
3152 | continue; | |
3153 | } | |
3154 | ||
3155 | if (bitmap_bit_p (reduction_stores, | |
3156 | SSA_NAME_VERSION (gimple_vdef (stmt)))) | |
3157 | { | |
3158 | if (dump_file) | |
3159 | { | |
3160 | fprintf (dump_file, | |
3161 | "skipped reduction store for single-gang" | |
3162 | " neutering: "); | |
1ffa4346 | 3163 | print_gimple_stmt (dump_file, stmt, 0); |
31c2b13b | 3164 | } |
3165 | ||
3166 | /* Update gsi to point to next stmt. */ | |
3167 | gsi_next (&gsi); | |
3168 | continue; | |
3169 | } | |
3170 | ||
3171 | changed = true; | |
3172 | ||
3173 | if (gang_pos == NULL_TREE) | |
3174 | { | |
3175 | tree arg = build_int_cst (integer_type_node, GOMP_DIM_GANG); | |
3176 | gcall *gang_single | |
3177 | = gimple_build_call_internal (IFN_GOACC_DIM_POS, 1, arg); | |
3178 | gang_pos = make_ssa_name (integer_type_node); | |
3179 | gimple_call_set_lhs (gang_single, gang_pos); | |
3180 | gimple_stmt_iterator start | |
3181 | = gsi_start_bb (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun))); | |
3182 | tree vuse = ssa_default_def (cfun, gimple_vop (cfun)); | |
3183 | gimple_set_vuse (gang_single, vuse); | |
3184 | gsi_insert_before (&start, gang_single, GSI_SAME_STMT); | |
3185 | } | |
3186 | ||
3187 | if (dump_file) | |
3188 | { | |
3189 | fprintf (dump_file, | |
3190 | "found store that needs single-gang neutering: "); | |
1ffa4346 | 3191 | print_gimple_stmt (dump_file, stmt, 0); |
31c2b13b | 3192 | } |
3193 | ||
3194 | { | |
3195 | /* Split block before store. */ | |
3196 | gimple_stmt_iterator gsi2 = gsi; | |
3197 | gsi_prev (&gsi2); | |
3198 | edge e; | |
3199 | if (gsi_end_p (gsi2)) | |
3200 | { | |
3201 | e = split_block_after_labels (bb); | |
3202 | gsi2 = gsi_last_bb (bb); | |
3203 | } | |
3204 | else | |
3205 | e = split_block (bb, gsi_stmt (gsi2)); | |
3206 | basic_block bb2 = e->dest; | |
3207 | ||
3208 | /* Split block after store. */ | |
3209 | gimple_stmt_iterator gsi3 = gsi_start_bb (bb2); | |
3210 | edge e2 = split_block (bb2, gsi_stmt (gsi3)); | |
3211 | basic_block bb3 = e2->dest; | |
3212 | ||
3213 | gimple *cond | |
3214 | = gimple_build_cond (EQ_EXPR, gang_pos, integer_zero_node, | |
3215 | NULL_TREE, NULL_TREE); | |
3216 | gsi_insert_after (&gsi2, cond, GSI_NEW_STMT); | |
3217 | ||
3218 | edge e3 = make_edge (bb, bb3, EDGE_FALSE_VALUE); | |
720cfc43 | 3219 | /* FIXME: What is the probability? */ |
3220 | e3->probability = profile_probability::guessed_never (); | |
31c2b13b | 3221 | e->flags = EDGE_TRUE_VALUE; |
3222 | ||
3223 | tree vdef = gimple_vdef (stmt); | |
3224 | tree vuse = gimple_vuse (stmt); | |
3225 | ||
3226 | tree phi_res = copy_ssa_name (vdef); | |
3227 | gphi *new_phi = create_phi_node (phi_res, bb3); | |
3228 | replace_uses_by (vdef, phi_res); | |
3229 | add_phi_arg (new_phi, vuse, e3, UNKNOWN_LOCATION); | |
3230 | add_phi_arg (new_phi, vdef, e2, UNKNOWN_LOCATION); | |
3231 | ||
3232 | /* Update gsi to point to next stmt. */ | |
3233 | bb = bb3; | |
3234 | gsi = gsi_start_bb (bb); | |
3235 | } | |
3236 | } | |
3237 | } | |
3238 | ||
3239 | return changed; | |
3240 | } | |
3241 | ||
3242 | /* Return true if the statements before and after the LOOP can be executed in | |
3243 | parallel with the function containing the loop. Resolve conflicting stores | |
3244 | outside LOOP by guarding them such that only a single gang executes them. */ | |
3245 | ||
3246 | static bool | |
2e966e2a | 3247 | oacc_entry_exit_ok (class loop *loop, |
31c2b13b | 3248 | reduction_info_table_type *reduction_list) |
3249 | { | |
3250 | basic_block *loop_bbs = get_loop_body_in_dom_order (loop); | |
3251 | vec<basic_block> region_bbs | |
3252 | = get_all_dominated_blocks (CDI_DOMINATORS, ENTRY_BLOCK_PTR_FOR_FN (cfun)); | |
3253 | ||
3254 | bitmap in_loop_bbs = BITMAP_ALLOC (NULL); | |
3255 | bitmap_clear (in_loop_bbs); | |
3256 | for (unsigned int i = 0; i < loop->num_nodes; i++) | |
3257 | bitmap_set_bit (in_loop_bbs, loop_bbs[i]->index); | |
3258 | ||
3259 | bitmap reduction_stores = BITMAP_ALLOC (NULL); | |
3260 | bool res = oacc_entry_exit_ok_1 (in_loop_bbs, region_bbs, reduction_list, | |
3261 | reduction_stores); | |
3262 | ||
3263 | if (res) | |
3264 | { | |
3265 | bool changed = oacc_entry_exit_single_gang (in_loop_bbs, region_bbs, | |
3266 | reduction_stores); | |
3267 | if (changed) | |
3268 | { | |
3269 | free_dominance_info (CDI_DOMINATORS); | |
3270 | calculate_dominance_info (CDI_DOMINATORS); | |
3271 | } | |
3272 | } | |
3273 | ||
9bde174b | 3274 | region_bbs.release (); |
31c2b13b | 3275 | free (loop_bbs); |
3276 | ||
3277 | BITMAP_FREE (in_loop_bbs); | |
3278 | BITMAP_FREE (reduction_stores); | |
3279 | ||
a006c0bb | 3280 | return res; |
3281 | } | |
3282 | ||
28c92cbb | 3283 | /* Detect parallel loops and generate parallel code using libgomp |
3284 | primitives. Returns true if some loop was parallelized, false | |
3285 | otherwise. */ | |
3286 | ||
4abc36e8 | 3287 | static bool |
31c2b13b | 3288 | parallelize_loops (bool oacc_kernels_p) |
28c92cbb | 3289 | { |
3a37a410 | 3290 | unsigned n_threads; |
28c92cbb | 3291 | bool changed = false; |
2e966e2a | 3292 | class loop *loop; |
3293 | class loop *skip_loop = NULL; | |
3294 | class tree_niter_desc niter_desc; | |
1e33ad50 | 3295 | struct obstack parloop_obstack; |
fbbe5b51 | 3296 | HOST_WIDE_INT estimated; |
1e33ad50 | 3297 | |
28c92cbb | 3298 | /* Do not parallelize loops in the functions created by parallelization. */ |
31c2b13b | 3299 | if (!oacc_kernels_p |
3300 | && parallelized_function_p (cfun->decl)) | |
28c92cbb | 3301 | return false; |
31c2b13b | 3302 | |
3303 | /* Do not parallelize loops in offloaded functions. */ | |
3304 | if (!oacc_kernels_p | |
4954efd4 | 3305 | && oacc_get_fn_attrib (cfun->decl) != NULL) |
31c2b13b | 3306 | return false; |
3307 | ||
fbbe5b51 | 3308 | if (cfun->has_nonlocal_label) |
3309 | return false; | |
28c92cbb | 3310 | |
3a37a410 | 3311 | /* For OpenACC kernels, n_threads will be determined later; otherwise, it's |
3312 | the argument to -ftree-parallelize-loops. */ | |
3313 | if (oacc_kernels_p) | |
3314 | n_threads = 0; | |
3315 | else | |
3316 | n_threads = flag_tree_parallelize_loops; | |
3317 | ||
1e33ad50 | 3318 | gcc_obstack_init (&parloop_obstack); |
c1f445d2 | 3319 | reduction_info_table_type reduction_list (10); |
cb7f680b | 3320 | |
31c2b13b | 3321 | calculate_dominance_info (CDI_DOMINATORS); |
3322 | ||
f21d4d00 | 3323 | FOR_EACH_LOOP (loop, 0) |
28c92cbb | 3324 | { |
86a932e0 | 3325 | if (loop == skip_loop) |
3326 | { | |
31c2b13b | 3327 | if (!loop->in_oacc_kernels_region |
3328 | && dump_file && (dump_flags & TDF_DETAILS)) | |
86a932e0 | 3329 | fprintf (dump_file, |
3330 | "Skipping loop %d as inner loop of parallelized loop\n", | |
3331 | loop->num); | |
3332 | ||
3333 | skip_loop = loop->inner; | |
3334 | continue; | |
3335 | } | |
3336 | else | |
3337 | skip_loop = NULL; | |
3338 | ||
d9dd21a8 | 3339 | reduction_list.empty (); |
31c2b13b | 3340 | |
3341 | if (oacc_kernels_p) | |
3342 | { | |
3343 | if (!loop->in_oacc_kernels_region) | |
3344 | continue; | |
3345 | ||
3346 | /* Don't try to parallelize inner loops in an oacc kernels region. */ | |
3347 | if (loop->inner) | |
3348 | skip_loop = loop->inner; | |
3349 | ||
3350 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3351 | fprintf (dump_file, | |
3352 | "Trying loop %d with header bb %d in oacc kernels" | |
3353 | " region\n", loop->num, loop->header->index); | |
3354 | } | |
3355 | ||
b0fb253a | 3356 | if (dump_file && (dump_flags & TDF_DETAILS)) |
3357 | { | |
3358 | fprintf (dump_file, "Trying loop %d as candidate\n",loop->num); | |
3359 | if (loop->inner) | |
3360 | fprintf (dump_file, "loop %d is not innermost\n",loop->num); | |
3361 | else | |
3362 | fprintf (dump_file, "loop %d is innermost\n",loop->num); | |
3363 | } | |
48e1416a | 3364 | |
b0fb253a | 3365 | if (!single_dom_exit (loop)) |
3366 | { | |
48e1416a | 3367 | |
b0fb253a | 3368 | if (dump_file && (dump_flags & TDF_DETAILS)) |
3369 | fprintf (dump_file, "loop is !single_dom_exit\n"); | |
48e1416a | 3370 | |
5fa90eea | 3371 | continue; |
b0fb253a | 3372 | } |
5fa90eea | 3373 | |
3374 | if (/* And of course, the loop must be parallelizable. */ | |
3375 | !can_duplicate_loop_p (loop) | |
d4fcfd16 | 3376 | || loop_has_blocks_with_irreducible_flag (loop) |
fbbe5b51 | 3377 | || (loop_preheader_edge (loop)->src->flags & BB_IRREDUCIBLE_LOOP) |
c968a07c | 3378 | /* FIXME: the check for vector phi nodes could be removed. */ |
89675e8c | 3379 | || loop_has_vector_phi_nodes (loop)) |
5fa90eea | 3380 | continue; |
b0b097b4 | 3381 | |
b3687123 | 3382 | estimated = estimated_loop_iterations_int (loop); |
b0b097b4 | 3383 | if (estimated == -1) |
b3687123 | 3384 | estimated = get_likely_max_loop_iterations_int (loop); |
525c22c4 | 3385 | /* FIXME: Bypass this check as graphite doesn't update the |
b0b097b4 | 3386 | count and frequency correctly now. */ |
525c22c4 | 3387 | if (!flag_loop_parallelize_all |
31c2b13b | 3388 | && !oacc_kernels_p |
b0b097b4 | 3389 | && ((estimated != -1 |
b3687123 | 3390 | && (estimated |
3391 | < ((HOST_WIDE_INT) n_threads | |
3392 | * (loop->inner ? 2 : MIN_PER_THREAD) - 1))) | |
525c22c4 | 3393 | /* Do not bother with loops in cold areas. */ |
3394 | || optimize_loop_nest_for_size_p (loop))) | |
5fa90eea | 3395 | continue; |
48e1416a | 3396 | |
5fa90eea | 3397 | if (!try_get_loop_niter (loop, &niter_desc)) |
3398 | continue; | |
3399 | ||
31c2b13b | 3400 | if (!try_create_reduction_list (loop, &reduction_list, oacc_kernels_p)) |
5fa90eea | 3401 | continue; |
3402 | ||
a006c0bb | 3403 | if (loop_has_phi_with_address_arg (loop)) |
3404 | continue; | |
3405 | ||
b3687123 | 3406 | if (!loop->can_be_parallel |
1e33ad50 | 3407 | && !loop_parallel_p (loop, &parloop_obstack)) |
28c92cbb | 3408 | continue; |
3409 | ||
31c2b13b | 3410 | if (oacc_kernels_p |
3411 | && !oacc_entry_exit_ok (loop, &reduction_list)) | |
3412 | { | |
3413 | if (dump_file) | |
3414 | fprintf (dump_file, "entry/exit not ok: FAILED\n"); | |
3415 | continue; | |
3416 | } | |
3417 | ||
28c92cbb | 3418 | changed = true; |
86a932e0 | 3419 | skip_loop = loop->inner; |
7de57141 | 3420 | |
91f42adc | 3421 | if (dump_enabled_p ()) |
3422 | { | |
3423 | dump_user_location_t loop_loc = find_loop_location (loop); | |
3424 | if (loop->inner) | |
3425 | dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loop_loc, | |
3426 | "parallelizing outer loop %d\n", loop->num); | |
3427 | else | |
3428 | dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loop_loc, | |
3429 | "parallelizing inner loop %d\n", loop->num); | |
3430 | } | |
31c2b13b | 3431 | |
c1f445d2 | 3432 | gen_parallel_loop (loop, &reduction_list, |
31c2b13b | 3433 | n_threads, &niter_desc, oacc_kernels_p); |
28c92cbb | 3434 | } |
3435 | ||
1e33ad50 | 3436 | obstack_free (&parloop_obstack, NULL); |
7f81b5ee | 3437 | |
3438 | /* Parallelization will cause new function calls to be inserted through | |
cb245216 | 3439 | which local variables will escape. Reset the points-to solution |
3440 | for ESCAPED. */ | |
7f81b5ee | 3441 | if (changed) |
cb245216 | 3442 | pt_solution_reset (&cfun->gimple_df->escaped); |
7f81b5ee | 3443 | |
28c92cbb | 3444 | return changed; |
3445 | } | |
3446 | ||
64641360 | 3447 | /* Parallelization. */ |
3448 | ||
64641360 | 3449 | namespace { |
3450 | ||
3451 | const pass_data pass_data_parallelize_loops = | |
3452 | { | |
3453 | GIMPLE_PASS, /* type */ | |
3454 | "parloops", /* name */ | |
3455 | OPTGROUP_LOOP, /* optinfo_flags */ | |
64641360 | 3456 | TV_TREE_PARALLELIZE_LOOPS, /* tv_id */ |
3457 | ( PROP_cfg | PROP_ssa ), /* properties_required */ | |
3458 | 0, /* properties_provided */ | |
3459 | 0, /* properties_destroyed */ | |
3460 | 0, /* todo_flags_start */ | |
8b88439e | 3461 | 0, /* todo_flags_finish */ |
64641360 | 3462 | }; |
3463 | ||
3464 | class pass_parallelize_loops : public gimple_opt_pass | |
3465 | { | |
3466 | public: | |
3467 | pass_parallelize_loops (gcc::context *ctxt) | |
31c2b13b | 3468 | : gimple_opt_pass (pass_data_parallelize_loops, ctxt), |
3469 | oacc_kernels_p (false) | |
64641360 | 3470 | {} |
3471 | ||
3472 | /* opt_pass methods: */ | |
3a37a410 | 3473 | virtual bool gate (function *) |
3474 | { | |
3475 | if (oacc_kernels_p) | |
3476 | return flag_openacc; | |
3477 | else | |
3478 | return flag_tree_parallelize_loops > 1; | |
3479 | } | |
65b0537f | 3480 | virtual unsigned int execute (function *); |
31c2b13b | 3481 | opt_pass * clone () { return new pass_parallelize_loops (m_ctxt); } |
3482 | void set_pass_param (unsigned int n, bool param) | |
3483 | { | |
3484 | gcc_assert (n == 0); | |
3485 | oacc_kernels_p = param; | |
3486 | } | |
64641360 | 3487 | |
31c2b13b | 3488 | private: |
3489 | bool oacc_kernels_p; | |
64641360 | 3490 | }; // class pass_parallelize_loops |
3491 | ||
65b0537f | 3492 | unsigned |
3493 | pass_parallelize_loops::execute (function *fun) | |
3494 | { | |
71e083dc | 3495 | tree nthreads = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS); |
3496 | if (nthreads == NULL_TREE) | |
3497 | return 0; | |
3498 | ||
7c5e2ac7 | 3499 | bool in_loop_pipeline = scev_initialized_p (); |
3500 | if (!in_loop_pipeline) | |
3501 | loop_optimizer_init (LOOPS_NORMAL | |
3502 | | LOOPS_HAVE_RECORDED_EXITS); | |
3503 | ||
3504 | if (number_of_loops (fun) <= 1) | |
3505 | return 0; | |
3506 | ||
3507 | if (!in_loop_pipeline) | |
3508 | { | |
3509 | rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa); | |
3510 | scev_initialize (); | |
3511 | } | |
3512 | ||
3513 | unsigned int todo = 0; | |
31c2b13b | 3514 | if (parallelize_loops (oacc_kernels_p)) |
8917c50b | 3515 | { |
3516 | fun->curr_properties &= ~(PROP_gimple_eomp); | |
86a932e0 | 3517 | |
382ecba7 | 3518 | checking_verify_loop_structure (); |
86a932e0 | 3519 | |
7c5e2ac7 | 3520 | todo |= TODO_update_ssa; |
3521 | } | |
3522 | ||
3523 | if (!in_loop_pipeline) | |
3524 | { | |
3525 | scev_finalize (); | |
3526 | loop_optimizer_finalize (); | |
8917c50b | 3527 | } |
3528 | ||
7c5e2ac7 | 3529 | return todo; |
65b0537f | 3530 | } |
3531 | ||
64641360 | 3532 | } // anon namespace |
3533 | ||
3534 | gimple_opt_pass * | |
3535 | make_pass_parallelize_loops (gcc::context *ctxt) | |
3536 | { | |
3537 | return new pass_parallelize_loops (ctxt); | |
3538 | } |