]>
Commit | Line | Data |
---|---|---|
dea61d92 | 1 | /* Loop distribution. |
a5544970 | 2 | Copyright (C) 2006-2019 Free Software Foundation, Inc. |
dea61d92 SP |
3 | Contributed by Georges-Andre Silber <Georges-Andre.Silber@ensmp.fr> |
4 | and Sebastian Pop <sebastian.pop@amd.com>. | |
5 | ||
6 | This file is part of GCC. | |
b8698a0f | 7 | |
dea61d92 SP |
8 | GCC is free software; you can redistribute it and/or modify it |
9 | under the terms of the GNU General Public License as published by the | |
10 | Free Software Foundation; either version 3, or (at your option) any | |
11 | later version. | |
b8698a0f | 12 | |
dea61d92 SP |
13 | GCC is distributed in the hope that it will be useful, but WITHOUT |
14 | ANY 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. | |
b8698a0f | 17 | |
dea61d92 SP |
18 | You should have received a copy of the GNU General Public License |
19 | along with GCC; see the file COPYING3. If not see | |
20 | <http://www.gnu.org/licenses/>. */ | |
21 | ||
22 | /* This pass performs loop distribution: for example, the loop | |
23 | ||
24 | |DO I = 2, N | |
25 | | A(I) = B(I) + C | |
26 | | D(I) = A(I-1)*E | |
27 | |ENDDO | |
28 | ||
b8698a0f | 29 | is transformed to |
dea61d92 SP |
30 | |
31 | |DOALL I = 2, N | |
32 | | A(I) = B(I) + C | |
33 | |ENDDO | |
34 | | | |
35 | |DOALL I = 2, N | |
36 | | D(I) = A(I-1)*E | |
37 | |ENDDO | |
38 | ||
a8745cc2 BC |
39 | Loop distribution is the dual of loop fusion. It separates statements |
40 | of a loop (or loop nest) into multiple loops (or loop nests) with the | |
41 | same loop header. The major goal is to separate statements which may | |
42 | be vectorized from those that can't. This pass implements distribution | |
43 | in the following steps: | |
44 | ||
45 | 1) Seed partitions with specific type statements. For now we support | |
46 | two types seed statements: statement defining variable used outside | |
47 | of loop; statement storing to memory. | |
48 | 2) Build reduced dependence graph (RDG) for loop to be distributed. | |
49 | The vertices (RDG:V) model all statements in the loop and the edges | |
50 | (RDG:E) model flow and control dependencies between statements. | |
51 | 3) Apart from RDG, compute data dependencies between memory references. | |
52 | 4) Starting from seed statement, build up partition by adding depended | |
53 | statements according to RDG's dependence information. Partition is | |
54 | classified as parallel type if it can be executed paralleled; or as | |
55 | sequential type if it can't. Parallel type partition is further | |
56 | classified as different builtin kinds if it can be implemented as | |
57 | builtin function calls. | |
58 | 5) Build partition dependence graph (PG) based on data dependencies. | |
59 | The vertices (PG:V) model all partitions and the edges (PG:E) model | |
60 | all data dependencies between every partitions pair. In general, | |
61 | data dependence is either compilation time known or unknown. In C | |
62 | family languages, there exists quite amount compilation time unknown | |
63 | dependencies because of possible alias relation of data references. | |
64 | We categorize PG's edge to two types: "true" edge that represents | |
65 | compilation time known data dependencies; "alias" edge for all other | |
66 | data dependencies. | |
67 | 6) Traverse subgraph of PG as if all "alias" edges don't exist. Merge | |
68 | partitions in each strong connected component (SCC) correspondingly. | |
69 | Build new PG for merged partitions. | |
70 | 7) Traverse PG again and this time with both "true" and "alias" edges | |
71 | included. We try to break SCCs by removing some edges. Because | |
72 | SCCs by "true" edges are all fused in step 6), we can break SCCs | |
73 | by removing some "alias" edges. It's NP-hard to choose optimal | |
74 | edge set, fortunately simple approximation is good enough for us | |
75 | given the small problem scale. | |
76 | 8) Collect all data dependencies of the removed "alias" edges. Create | |
77 | runtime alias checks for collected data dependencies. | |
78 | 9) Version loop under the condition of runtime alias checks. Given | |
79 | loop distribution generally introduces additional overhead, it is | |
80 | only useful if vectorization is achieved in distributed loop. We | |
81 | version loop with internal function call IFN_LOOP_DIST_ALIAS. If | |
82 | no distributed loop can be vectorized, we simply remove distributed | |
83 | loops and recover to the original one. | |
84 | ||
85 | TODO: | |
163aa51b BC |
86 | 1) We only distribute innermost two-level loop nest now. We should |
87 | extend it for arbitrary loop nests in the future. | |
a8745cc2 BC |
88 | 2) We only fuse partitions in SCC now. A better fusion algorithm is |
89 | desired to minimize loop overhead, maximize parallelism and maximize | |
90 | data reuse. */ | |
dea61d92 SP |
91 | |
92 | #include "config.h" | |
93 | #include "system.h" | |
94 | #include "coretypes.h" | |
c7131fb2 | 95 | #include "backend.h" |
40e23961 | 96 | #include "tree.h" |
c7131fb2 | 97 | #include "gimple.h" |
957060b5 AM |
98 | #include "cfghooks.h" |
99 | #include "tree-pass.h" | |
c7131fb2 | 100 | #include "ssa.h" |
957060b5 | 101 | #include "gimple-pretty-print.h" |
c7131fb2 | 102 | #include "fold-const.h" |
60393bbc | 103 | #include "cfganal.h" |
5be5c238 | 104 | #include "gimple-iterator.h" |
18f429e2 | 105 | #include "gimplify-me.h" |
d8a2d370 | 106 | #include "stor-layout.h" |
442b4905 | 107 | #include "tree-cfg.h" |
e28030cf | 108 | #include "tree-ssa-loop-manip.h" |
957f0d8f | 109 | #include "tree-ssa-loop-ivopts.h" |
442b4905 AM |
110 | #include "tree-ssa-loop.h" |
111 | #include "tree-into-ssa.h" | |
7a300452 | 112 | #include "tree-ssa.h" |
dea61d92 | 113 | #include "cfgloop.h" |
dea61d92 | 114 | #include "tree-scalar-evolution.h" |
9fafb14a | 115 | #include "params.h" |
826a536d | 116 | #include "tree-vectorizer.h" |
80ab0b19 RB |
117 | |
118 | ||
9fafb14a BC |
119 | #define MAX_DATAREFS_NUM \ |
120 | ((unsigned) PARAM_VALUE (PARAM_LOOP_MAX_DATAREFS_FOR_DATADEPS)) | |
121 | ||
163aa51b BC |
122 | /* Threshold controlling number of distributed partitions. Given it may |
123 | be unnecessary if a memory stream cost model is invented in the future, | |
124 | we define it as a temporary macro, rather than a parameter. */ | |
125 | #define NUM_PARTITION_THRESHOLD (4) | |
126 | ||
17c5cbdf BC |
127 | /* Hashtable helpers. */ |
128 | ||
129 | struct ddr_hasher : nofree_ptr_hash <struct data_dependence_relation> | |
130 | { | |
131 | static inline hashval_t hash (const data_dependence_relation *); | |
132 | static inline bool equal (const data_dependence_relation *, | |
133 | const data_dependence_relation *); | |
134 | }; | |
135 | ||
136 | /* Hash function for data dependence. */ | |
137 | ||
138 | inline hashval_t | |
139 | ddr_hasher::hash (const data_dependence_relation *ddr) | |
140 | { | |
141 | inchash::hash h; | |
142 | h.add_ptr (DDR_A (ddr)); | |
143 | h.add_ptr (DDR_B (ddr)); | |
144 | return h.end (); | |
145 | } | |
146 | ||
147 | /* Hash table equality function for data dependence. */ | |
148 | ||
149 | inline bool | |
150 | ddr_hasher::equal (const data_dependence_relation *ddr1, | |
151 | const data_dependence_relation *ddr2) | |
152 | { | |
153 | return (DDR_A (ddr1) == DDR_A (ddr2) && DDR_B (ddr1) == DDR_B (ddr2)); | |
154 | } | |
155 | ||
4084ea5f BC |
156 | /* The loop (nest) to be distributed. */ |
157 | static vec<loop_p> loop_nest; | |
158 | ||
9fafb14a BC |
159 | /* Vector of data references in the loop to be distributed. */ |
160 | static vec<data_reference_p> datarefs_vec; | |
161 | ||
162 | /* Store index of data reference in aux field. */ | |
163 | #define DR_INDEX(dr) ((uintptr_t) (dr)->aux) | |
164 | ||
17c5cbdf | 165 | /* Hash table for data dependence relation in the loop to be distributed. */ |
1e485f89 | 166 | static hash_table<ddr_hasher> *ddrs_table; |
17c5cbdf | 167 | |
80ab0b19 | 168 | /* A Reduced Dependence Graph (RDG) vertex representing a statement. */ |
526ceb68 | 169 | struct rdg_vertex |
80ab0b19 RB |
170 | { |
171 | /* The statement represented by this vertex. */ | |
355fe088 | 172 | gimple *stmt; |
80ab0b19 RB |
173 | |
174 | /* Vector of data-references in this statement. */ | |
175 | vec<data_reference_p> datarefs; | |
176 | ||
177 | /* True when the statement contains a write to memory. */ | |
178 | bool has_mem_write; | |
179 | ||
180 | /* True when the statement contains a read from memory. */ | |
181 | bool has_mem_reads; | |
526ceb68 | 182 | }; |
80ab0b19 RB |
183 | |
184 | #define RDGV_STMT(V) ((struct rdg_vertex *) ((V)->data))->stmt | |
185 | #define RDGV_DATAREFS(V) ((struct rdg_vertex *) ((V)->data))->datarefs | |
186 | #define RDGV_HAS_MEM_WRITE(V) ((struct rdg_vertex *) ((V)->data))->has_mem_write | |
187 | #define RDGV_HAS_MEM_READS(V) ((struct rdg_vertex *) ((V)->data))->has_mem_reads | |
188 | #define RDG_STMT(RDG, I) RDGV_STMT (&(RDG->vertices[I])) | |
189 | #define RDG_DATAREFS(RDG, I) RDGV_DATAREFS (&(RDG->vertices[I])) | |
190 | #define RDG_MEM_WRITE_STMT(RDG, I) RDGV_HAS_MEM_WRITE (&(RDG->vertices[I])) | |
191 | #define RDG_MEM_READS_STMT(RDG, I) RDGV_HAS_MEM_READS (&(RDG->vertices[I])) | |
192 | ||
193 | /* Data dependence type. */ | |
194 | ||
195 | enum rdg_dep_type | |
196 | { | |
197 | /* Read After Write (RAW). */ | |
198 | flow_dd = 'f', | |
199 | ||
36875e8f RB |
200 | /* Control dependence (execute conditional on). */ |
201 | control_dd = 'c' | |
80ab0b19 RB |
202 | }; |
203 | ||
204 | /* Dependence information attached to an edge of the RDG. */ | |
205 | ||
526ceb68 | 206 | struct rdg_edge |
80ab0b19 RB |
207 | { |
208 | /* Type of the dependence. */ | |
209 | enum rdg_dep_type type; | |
526ceb68 | 210 | }; |
80ab0b19 RB |
211 | |
212 | #define RDGE_TYPE(E) ((struct rdg_edge *) ((E)->data))->type | |
80ab0b19 | 213 | |
80ab0b19 RB |
214 | /* Dump vertex I in RDG to FILE. */ |
215 | ||
216 | static void | |
217 | dump_rdg_vertex (FILE *file, struct graph *rdg, int i) | |
218 | { | |
219 | struct vertex *v = &(rdg->vertices[i]); | |
220 | struct graph_edge *e; | |
221 | ||
222 | fprintf (file, "(vertex %d: (%s%s) (in:", i, | |
223 | RDG_MEM_WRITE_STMT (rdg, i) ? "w" : "", | |
224 | RDG_MEM_READS_STMT (rdg, i) ? "r" : ""); | |
225 | ||
226 | if (v->pred) | |
227 | for (e = v->pred; e; e = e->pred_next) | |
228 | fprintf (file, " %d", e->src); | |
229 | ||
230 | fprintf (file, ") (out:"); | |
231 | ||
232 | if (v->succ) | |
233 | for (e = v->succ; e; e = e->succ_next) | |
234 | fprintf (file, " %d", e->dest); | |
235 | ||
236 | fprintf (file, ")\n"); | |
237 | print_gimple_stmt (file, RDGV_STMT (v), 0, TDF_VOPS|TDF_MEMSYMS); | |
238 | fprintf (file, ")\n"); | |
239 | } | |
240 | ||
241 | /* Call dump_rdg_vertex on stderr. */ | |
242 | ||
243 | DEBUG_FUNCTION void | |
244 | debug_rdg_vertex (struct graph *rdg, int i) | |
245 | { | |
246 | dump_rdg_vertex (stderr, rdg, i); | |
247 | } | |
248 | ||
80ab0b19 RB |
249 | /* Dump the reduced dependence graph RDG to FILE. */ |
250 | ||
251 | static void | |
252 | dump_rdg (FILE *file, struct graph *rdg) | |
253 | { | |
80ab0b19 | 254 | fprintf (file, "(rdg\n"); |
2fd5894f RB |
255 | for (int i = 0; i < rdg->n_vertices; i++) |
256 | dump_rdg_vertex (file, rdg, i); | |
80ab0b19 | 257 | fprintf (file, ")\n"); |
80ab0b19 RB |
258 | } |
259 | ||
260 | /* Call dump_rdg on stderr. */ | |
261 | ||
262 | DEBUG_FUNCTION void | |
263 | debug_rdg (struct graph *rdg) | |
264 | { | |
265 | dump_rdg (stderr, rdg); | |
266 | } | |
267 | ||
268 | static void | |
269 | dot_rdg_1 (FILE *file, struct graph *rdg) | |
270 | { | |
271 | int i; | |
174ec470 RB |
272 | pretty_printer buffer; |
273 | pp_needs_newline (&buffer) = false; | |
274 | buffer.buffer->stream = file; | |
80ab0b19 RB |
275 | |
276 | fprintf (file, "digraph RDG {\n"); | |
277 | ||
278 | for (i = 0; i < rdg->n_vertices; i++) | |
279 | { | |
280 | struct vertex *v = &(rdg->vertices[i]); | |
281 | struct graph_edge *e; | |
282 | ||
174ec470 RB |
283 | fprintf (file, "%d [label=\"[%d] ", i, i); |
284 | pp_gimple_stmt_1 (&buffer, RDGV_STMT (v), 0, TDF_SLIM); | |
285 | pp_flush (&buffer); | |
286 | fprintf (file, "\"]\n"); | |
287 | ||
80ab0b19 RB |
288 | /* Highlight reads from memory. */ |
289 | if (RDG_MEM_READS_STMT (rdg, i)) | |
290 | fprintf (file, "%d [style=filled, fillcolor=green]\n", i); | |
291 | ||
292 | /* Highlight stores to memory. */ | |
293 | if (RDG_MEM_WRITE_STMT (rdg, i)) | |
294 | fprintf (file, "%d [style=filled, fillcolor=red]\n", i); | |
295 | ||
296 | if (v->succ) | |
297 | for (e = v->succ; e; e = e->succ_next) | |
298 | switch (RDGE_TYPE (e)) | |
299 | { | |
80ab0b19 RB |
300 | case flow_dd: |
301 | /* These are the most common dependences: don't print these. */ | |
302 | fprintf (file, "%d -> %d \n", i, e->dest); | |
303 | break; | |
304 | ||
36875e8f RB |
305 | case control_dd: |
306 | fprintf (file, "%d -> %d [label=control] \n", i, e->dest); | |
307 | break; | |
308 | ||
80ab0b19 RB |
309 | default: |
310 | gcc_unreachable (); | |
311 | } | |
312 | } | |
313 | ||
314 | fprintf (file, "}\n\n"); | |
315 | } | |
316 | ||
317 | /* Display the Reduced Dependence Graph using dotty. */ | |
318 | ||
319 | DEBUG_FUNCTION void | |
320 | dot_rdg (struct graph *rdg) | |
321 | { | |
174ec470 | 322 | /* When debugging, you may want to enable the following code. */ |
b6d94045 | 323 | #ifdef HAVE_POPEN |
c3284718 | 324 | FILE *file = popen ("dot -Tx11", "w"); |
174ec470 RB |
325 | if (!file) |
326 | return; | |
80ab0b19 | 327 | dot_rdg_1 (file, rdg); |
174ec470 RB |
328 | fflush (file); |
329 | close (fileno (file)); | |
330 | pclose (file); | |
80ab0b19 RB |
331 | #else |
332 | dot_rdg_1 (stderr, rdg); | |
333 | #endif | |
334 | } | |
335 | ||
336 | /* Returns the index of STMT in RDG. */ | |
337 | ||
338 | static int | |
355fe088 | 339 | rdg_vertex_for_stmt (struct graph *rdg ATTRIBUTE_UNUSED, gimple *stmt) |
80ab0b19 RB |
340 | { |
341 | int index = gimple_uid (stmt); | |
342 | gcc_checking_assert (index == -1 || RDG_STMT (rdg, index) == stmt); | |
343 | return index; | |
344 | } | |
345 | ||
80ab0b19 RB |
346 | /* Creates dependence edges in RDG for all the uses of DEF. IDEF is |
347 | the index of DEF in RDG. */ | |
348 | ||
349 | static void | |
350 | create_rdg_edges_for_scalar (struct graph *rdg, tree def, int idef) | |
351 | { | |
352 | use_operand_p imm_use_p; | |
353 | imm_use_iterator iterator; | |
354 | ||
355 | FOR_EACH_IMM_USE_FAST (imm_use_p, iterator, def) | |
356 | { | |
357 | struct graph_edge *e; | |
358 | int use = rdg_vertex_for_stmt (rdg, USE_STMT (imm_use_p)); | |
359 | ||
360 | if (use < 0) | |
361 | continue; | |
362 | ||
363 | e = add_edge (rdg, idef, use); | |
364 | e->data = XNEW (struct rdg_edge); | |
365 | RDGE_TYPE (e) = flow_dd; | |
80ab0b19 RB |
366 | } |
367 | } | |
368 | ||
36875e8f RB |
369 | /* Creates an edge for the control dependences of BB to the vertex V. */ |
370 | ||
371 | static void | |
372 | create_edge_for_control_dependence (struct graph *rdg, basic_block bb, | |
373 | int v, control_dependences *cd) | |
374 | { | |
375 | bitmap_iterator bi; | |
376 | unsigned edge_n; | |
377 | EXECUTE_IF_SET_IN_BITMAP (cd->get_edges_dependent_on (bb->index), | |
378 | 0, edge_n, bi) | |
379 | { | |
30fd2977 | 380 | basic_block cond_bb = cd->get_edge_src (edge_n); |
355fe088 | 381 | gimple *stmt = last_stmt (cond_bb); |
36875e8f RB |
382 | if (stmt && is_ctrl_stmt (stmt)) |
383 | { | |
384 | struct graph_edge *e; | |
385 | int c = rdg_vertex_for_stmt (rdg, stmt); | |
386 | if (c < 0) | |
387 | continue; | |
388 | ||
389 | e = add_edge (rdg, c, v); | |
390 | e->data = XNEW (struct rdg_edge); | |
391 | RDGE_TYPE (e) = control_dd; | |
36875e8f RB |
392 | } |
393 | } | |
394 | } | |
395 | ||
80ab0b19 RB |
396 | /* Creates the edges of the reduced dependence graph RDG. */ |
397 | ||
398 | static void | |
447f3223 | 399 | create_rdg_flow_edges (struct graph *rdg) |
80ab0b19 RB |
400 | { |
401 | int i; | |
80ab0b19 RB |
402 | def_operand_p def_p; |
403 | ssa_op_iter iter; | |
404 | ||
80ab0b19 RB |
405 | for (i = 0; i < rdg->n_vertices; i++) |
406 | FOR_EACH_PHI_OR_STMT_DEF (def_p, RDG_STMT (rdg, i), | |
407 | iter, SSA_OP_DEF) | |
408 | create_rdg_edges_for_scalar (rdg, DEF_FROM_PTR (def_p), i); | |
447f3223 | 409 | } |
36875e8f | 410 | |
447f3223 RB |
411 | /* Creates the edges of the reduced dependence graph RDG. */ |
412 | ||
413 | static void | |
b71b7a8e | 414 | create_rdg_cd_edges (struct graph *rdg, control_dependences *cd, loop_p loop) |
447f3223 RB |
415 | { |
416 | int i; | |
417 | ||
418 | for (i = 0; i < rdg->n_vertices; i++) | |
419 | { | |
355fe088 | 420 | gimple *stmt = RDG_STMT (rdg, i); |
447f3223 RB |
421 | if (gimple_code (stmt) == GIMPLE_PHI) |
422 | { | |
423 | edge_iterator ei; | |
424 | edge e; | |
425 | FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->preds) | |
b71b7a8e | 426 | if (flow_bb_inside_loop_p (loop, e->src)) |
36875e8f | 427 | create_edge_for_control_dependence (rdg, e->src, i, cd); |
447f3223 RB |
428 | } |
429 | else | |
430 | create_edge_for_control_dependence (rdg, gimple_bb (stmt), i, cd); | |
431 | } | |
80ab0b19 RB |
432 | } |
433 | ||
434 | /* Build the vertices of the reduced dependence graph RDG. Return false | |
435 | if that failed. */ | |
436 | ||
437 | static bool | |
9fafb14a | 438 | create_rdg_vertices (struct graph *rdg, vec<gimple *> stmts, loop_p loop) |
80ab0b19 RB |
439 | { |
440 | int i; | |
355fe088 | 441 | gimple *stmt; |
80ab0b19 RB |
442 | |
443 | FOR_EACH_VEC_ELT (stmts, i, stmt) | |
444 | { | |
445 | struct vertex *v = &(rdg->vertices[i]); | |
446 | ||
447 | /* Record statement to vertex mapping. */ | |
448 | gimple_set_uid (stmt, i); | |
449 | ||
450 | v->data = XNEW (struct rdg_vertex); | |
451 | RDGV_STMT (v) = stmt; | |
452 | RDGV_DATAREFS (v).create (0); | |
453 | RDGV_HAS_MEM_WRITE (v) = false; | |
454 | RDGV_HAS_MEM_READS (v) = false; | |
455 | if (gimple_code (stmt) == GIMPLE_PHI) | |
456 | continue; | |
457 | ||
9fafb14a BC |
458 | unsigned drp = datarefs_vec.length (); |
459 | if (!find_data_references_in_stmt (loop, stmt, &datarefs_vec)) | |
80ab0b19 | 460 | return false; |
9fafb14a | 461 | for (unsigned j = drp; j < datarefs_vec.length (); ++j) |
80ab0b19 | 462 | { |
9fafb14a | 463 | data_reference_p dr = datarefs_vec[j]; |
80ab0b19 RB |
464 | if (DR_IS_READ (dr)) |
465 | RDGV_HAS_MEM_READS (v) = true; | |
466 | else | |
467 | RDGV_HAS_MEM_WRITE (v) = true; | |
468 | RDGV_DATAREFS (v).safe_push (dr); | |
469 | } | |
470 | } | |
471 | return true; | |
472 | } | |
473 | ||
3be57c56 BC |
474 | /* Array mapping basic block's index to its topological order. */ |
475 | static int *bb_top_order_index; | |
476 | /* And size of the array. */ | |
477 | static int bb_top_order_index_size; | |
478 | ||
479 | /* If X has a smaller topological sort number than Y, returns -1; | |
480 | if greater, returns 1. */ | |
481 | ||
482 | static int | |
483 | bb_top_order_cmp (const void *x, const void *y) | |
484 | { | |
485 | basic_block bb1 = *(const basic_block *) x; | |
486 | basic_block bb2 = *(const basic_block *) y; | |
487 | ||
488 | gcc_assert (bb1->index < bb_top_order_index_size | |
489 | && bb2->index < bb_top_order_index_size); | |
490 | gcc_assert (bb1 == bb2 | |
491 | || bb_top_order_index[bb1->index] | |
492 | != bb_top_order_index[bb2->index]); | |
493 | ||
494 | return (bb_top_order_index[bb1->index] - bb_top_order_index[bb2->index]); | |
495 | } | |
496 | ||
497 | /* Initialize STMTS with all the statements of LOOP. We use topological | |
498 | order to discover all statements. The order is important because | |
499 | generate_loops_for_partition is using the same traversal for identifying | |
500 | statements in loop copies. */ | |
80ab0b19 RB |
501 | |
502 | static void | |
355fe088 | 503 | stmts_from_loop (struct loop *loop, vec<gimple *> *stmts) |
80ab0b19 RB |
504 | { |
505 | unsigned int i; | |
3be57c56 | 506 | basic_block *bbs = get_loop_body_in_custom_order (loop, bb_top_order_cmp); |
80ab0b19 RB |
507 | |
508 | for (i = 0; i < loop->num_nodes; i++) | |
509 | { | |
510 | basic_block bb = bbs[i]; | |
80ab0b19 | 511 | |
538dd0b7 DM |
512 | for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi); |
513 | gsi_next (&bsi)) | |
514 | if (!virtual_operand_p (gimple_phi_result (bsi.phi ()))) | |
515 | stmts->safe_push (bsi.phi ()); | |
80ab0b19 | 516 | |
538dd0b7 DM |
517 | for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi); |
518 | gsi_next (&bsi)) | |
80ab0b19 | 519 | { |
355fe088 | 520 | gimple *stmt = gsi_stmt (bsi); |
80ab0b19 RB |
521 | if (gimple_code (stmt) != GIMPLE_LABEL && !is_gimple_debug (stmt)) |
522 | stmts->safe_push (stmt); | |
523 | } | |
524 | } | |
525 | ||
526 | free (bbs); | |
527 | } | |
528 | ||
80ab0b19 RB |
529 | /* Free the reduced dependence graph RDG. */ |
530 | ||
531 | static void | |
532 | free_rdg (struct graph *rdg) | |
533 | { | |
534 | int i; | |
535 | ||
536 | for (i = 0; i < rdg->n_vertices; i++) | |
537 | { | |
538 | struct vertex *v = &(rdg->vertices[i]); | |
539 | struct graph_edge *e; | |
540 | ||
541 | for (e = v->succ; e; e = e->succ_next) | |
447f3223 | 542 | free (e->data); |
80ab0b19 RB |
543 | |
544 | if (v->data) | |
545 | { | |
546 | gimple_set_uid (RDGV_STMT (v), -1); | |
9fafb14a | 547 | (RDGV_DATAREFS (v)).release (); |
80ab0b19 RB |
548 | free (v->data); |
549 | } | |
550 | } | |
551 | ||
552 | free_graph (rdg); | |
553 | } | |
554 | ||
4084ea5f BC |
555 | /* Build the Reduced Dependence Graph (RDG) with one vertex per statement of |
556 | LOOP, and one edge per flow dependence or control dependence from control | |
9fafb14a BC |
557 | dependence CD. During visiting each statement, data references are also |
558 | collected and recorded in global data DATAREFS_VEC. */ | |
80ab0b19 RB |
559 | |
560 | static struct graph * | |
4084ea5f | 561 | build_rdg (struct loop *loop, control_dependences *cd) |
80ab0b19 RB |
562 | { |
563 | struct graph *rdg; | |
80ab0b19 | 564 | |
97463b2b | 565 | /* Create the RDG vertices from the stmts of the loop nest. */ |
355fe088 | 566 | auto_vec<gimple *, 10> stmts; |
4084ea5f | 567 | stmts_from_loop (loop, &stmts); |
24f161fd | 568 | rdg = new_graph (stmts.length ()); |
9fafb14a | 569 | if (!create_rdg_vertices (rdg, stmts, loop)) |
80ab0b19 | 570 | { |
80ab0b19 RB |
571 | free_rdg (rdg); |
572 | return NULL; | |
573 | } | |
574 | stmts.release (); | |
97463b2b | 575 | |
447f3223 RB |
576 | create_rdg_flow_edges (rdg); |
577 | if (cd) | |
4084ea5f | 578 | create_rdg_cd_edges (rdg, cd, loop); |
447f3223 | 579 | |
80ab0b19 RB |
580 | return rdg; |
581 | } | |
582 | ||
80ab0b19 | 583 | |
f1eb4621 | 584 | /* Kind of distributed loop. */ |
b9fc0497 | 585 | enum partition_kind { |
5955438a BC |
586 | PKIND_NORMAL, |
587 | /* Partial memset stands for a paritition can be distributed into a loop | |
588 | of memset calls, rather than a single memset call. It's handled just | |
589 | like a normal parition, i.e, distributed as separate loop, no memset | |
590 | call is generated. | |
591 | ||
592 | Note: This is a hacking fix trying to distribute ZERO-ing stmt in a | |
593 | loop nest as deep as possible. As a result, parloop achieves better | |
594 | parallelization by parallelizing deeper loop nest. This hack should | |
595 | be unnecessary and removed once distributed memset can be understood | |
596 | and analyzed in data reference analysis. See PR82604 for more. */ | |
597 | PKIND_PARTIAL_MEMSET, | |
598 | PKIND_MEMSET, PKIND_MEMCPY, PKIND_MEMMOVE | |
b9fc0497 | 599 | }; |
30d55936 | 600 | |
f1eb4621 BC |
601 | /* Type of distributed loop. */ |
602 | enum partition_type { | |
603 | /* The distributed loop can be executed parallelly. */ | |
604 | PTYPE_PARALLEL = 0, | |
605 | /* The distributed loop has to be executed sequentially. */ | |
606 | PTYPE_SEQUENTIAL | |
607 | }; | |
608 | ||
939cf90f BC |
609 | /* Builtin info for loop distribution. */ |
610 | struct builtin_info | |
611 | { | |
612 | /* data-references a kind != PKIND_NORMAL partition is about. */ | |
613 | data_reference_p dst_dr; | |
614 | data_reference_p src_dr; | |
615 | /* Base address and size of memory objects operated by the builtin. Note | |
616 | both dest and source memory objects must have the same size. */ | |
617 | tree dst_base; | |
618 | tree src_base; | |
619 | tree size; | |
957f0d8f BC |
620 | /* Base and offset part of dst_base after stripping constant offset. This |
621 | is only used in memset builtin distribution for now. */ | |
622 | tree dst_base_base; | |
623 | unsigned HOST_WIDE_INT dst_base_offset; | |
939cf90f BC |
624 | }; |
625 | ||
a7a44c07 | 626 | /* Partition for loop distribution. */ |
526ceb68 | 627 | struct partition |
c61f8985 | 628 | { |
a7a44c07 | 629 | /* Statements of the partition. */ |
c61f8985 | 630 | bitmap stmts; |
a7a44c07 | 631 | /* True if the partition defines variable which is used outside of loop. */ |
826a536d | 632 | bool reduction_p; |
30d55936 | 633 | enum partition_kind kind; |
f1eb4621 | 634 | enum partition_type type; |
a7a44c07 BC |
635 | /* Data references in the partition. */ |
636 | bitmap datarefs; | |
939cf90f BC |
637 | /* Information of builtin parition. */ |
638 | struct builtin_info *builtin; | |
526ceb68 | 639 | }; |
c61f8985 | 640 | |
c61f8985 RG |
641 | |
642 | /* Allocate and initialize a partition from BITMAP. */ | |
643 | ||
526ceb68 | 644 | static partition * |
a7a44c07 | 645 | partition_alloc (void) |
c61f8985 | 646 | { |
526ceb68 | 647 | partition *partition = XCNEW (struct partition); |
a7a44c07 | 648 | partition->stmts = BITMAP_ALLOC (NULL); |
826a536d | 649 | partition->reduction_p = false; |
30d55936 | 650 | partition->kind = PKIND_NORMAL; |
a7a44c07 | 651 | partition->datarefs = BITMAP_ALLOC (NULL); |
c61f8985 RG |
652 | return partition; |
653 | } | |
654 | ||
655 | /* Free PARTITION. */ | |
656 | ||
657 | static void | |
526ceb68 | 658 | partition_free (partition *partition) |
c61f8985 RG |
659 | { |
660 | BITMAP_FREE (partition->stmts); | |
a7a44c07 | 661 | BITMAP_FREE (partition->datarefs); |
939cf90f BC |
662 | if (partition->builtin) |
663 | free (partition->builtin); | |
664 | ||
c61f8985 RG |
665 | free (partition); |
666 | } | |
667 | ||
30d55936 RG |
668 | /* Returns true if the partition can be generated as a builtin. */ |
669 | ||
670 | static bool | |
526ceb68 | 671 | partition_builtin_p (partition *partition) |
30d55936 | 672 | { |
5955438a | 673 | return partition->kind > PKIND_PARTIAL_MEMSET; |
30d55936 | 674 | } |
c61f8985 | 675 | |
826a536d | 676 | /* Returns true if the partition contains a reduction. */ |
7ad672e4 RG |
677 | |
678 | static bool | |
526ceb68 | 679 | partition_reduction_p (partition *partition) |
7ad672e4 | 680 | { |
826a536d | 681 | return partition->reduction_p; |
7ad672e4 RG |
682 | } |
683 | ||
821dbeef BC |
684 | /* Partitions are fused because of different reasons. */ |
685 | enum fuse_type | |
686 | { | |
687 | FUSE_NON_BUILTIN = 0, | |
688 | FUSE_REDUCTION = 1, | |
689 | FUSE_SHARE_REF = 2, | |
690 | FUSE_SAME_SCC = 3, | |
691 | FUSE_FINALIZE = 4 | |
692 | }; | |
693 | ||
694 | /* Description on different fusing reason. */ | |
695 | static const char *fuse_message[] = { | |
696 | "they are non-builtins", | |
697 | "they have reductions", | |
698 | "they have shared memory refs", | |
699 | "they are in the same dependence scc", | |
700 | "there is no point to distribute loop"}; | |
701 | ||
826a536d | 702 | static void |
f1eb4621 | 703 | update_type_for_merge (struct graph *, partition *, partition *); |
821dbeef | 704 | |
f1eb4621 BC |
705 | /* Merge PARTITION into the partition DEST. RDG is the reduced dependence |
706 | graph and we update type for result partition if it is non-NULL. */ | |
a7a44c07 | 707 | |
f1eb4621 BC |
708 | static void |
709 | partition_merge_into (struct graph *rdg, partition *dest, | |
710 | partition *partition, enum fuse_type ft) | |
711 | { | |
821dbeef BC |
712 | if (dump_file && (dump_flags & TDF_DETAILS)) |
713 | { | |
714 | fprintf (dump_file, "Fuse partitions because %s:\n", fuse_message[ft]); | |
715 | fprintf (dump_file, " Part 1: "); | |
716 | dump_bitmap (dump_file, dest->stmts); | |
717 | fprintf (dump_file, " Part 2: "); | |
718 | dump_bitmap (dump_file, partition->stmts); | |
719 | } | |
f1eb4621 BC |
720 | |
721 | dest->kind = PKIND_NORMAL; | |
722 | if (dest->type == PTYPE_PARALLEL) | |
723 | dest->type = partition->type; | |
724 | ||
725 | bitmap_ior_into (dest->stmts, partition->stmts); | |
726 | if (partition_reduction_p (partition)) | |
727 | dest->reduction_p = true; | |
728 | ||
729 | /* Further check if any data dependence prevents us from executing the | |
730 | new partition parallelly. */ | |
731 | if (dest->type == PTYPE_PARALLEL && rdg != NULL) | |
732 | update_type_for_merge (rdg, dest, partition); | |
733 | ||
734 | bitmap_ior_into (dest->datarefs, partition->datarefs); | |
826a536d RB |
735 | } |
736 | ||
737 | ||
c07a8cb3 RG |
738 | /* Returns true when DEF is an SSA_NAME defined in LOOP and used after |
739 | the LOOP. */ | |
740 | ||
741 | static bool | |
742 | ssa_name_has_uses_outside_loop_p (tree def, loop_p loop) | |
743 | { | |
744 | imm_use_iterator imm_iter; | |
745 | use_operand_p use_p; | |
746 | ||
747 | FOR_EACH_IMM_USE_FAST (use_p, imm_iter, def) | |
e665269a | 748 | { |
163aa51b BC |
749 | if (is_gimple_debug (USE_STMT (use_p))) |
750 | continue; | |
751 | ||
752 | basic_block use_bb = gimple_bb (USE_STMT (use_p)); | |
753 | if (!flow_bb_inside_loop_p (loop, use_bb)) | |
e665269a RG |
754 | return true; |
755 | } | |
c07a8cb3 RG |
756 | |
757 | return false; | |
758 | } | |
759 | ||
760 | /* Returns true when STMT defines a scalar variable used after the | |
88af7c1a | 761 | loop LOOP. */ |
c07a8cb3 RG |
762 | |
763 | static bool | |
355fe088 | 764 | stmt_has_scalar_dependences_outside_loop (loop_p loop, gimple *stmt) |
c07a8cb3 | 765 | { |
88af7c1a RG |
766 | def_operand_p def_p; |
767 | ssa_op_iter op_iter; | |
c07a8cb3 | 768 | |
9ca86fc3 RG |
769 | if (gimple_code (stmt) == GIMPLE_PHI) |
770 | return ssa_name_has_uses_outside_loop_p (gimple_phi_result (stmt), loop); | |
771 | ||
88af7c1a RG |
772 | FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, op_iter, SSA_OP_DEF) |
773 | if (ssa_name_has_uses_outside_loop_p (DEF_FROM_PTR (def_p), loop)) | |
774 | return true; | |
c07a8cb3 | 775 | |
88af7c1a | 776 | return false; |
c07a8cb3 RG |
777 | } |
778 | ||
dea61d92 SP |
779 | /* Return a copy of LOOP placed before LOOP. */ |
780 | ||
781 | static struct loop * | |
782 | copy_loop_before (struct loop *loop) | |
783 | { | |
784 | struct loop *res; | |
785 | edge preheader = loop_preheader_edge (loop); | |
786 | ||
dea61d92 | 787 | initialize_original_copy_tables (); |
5ce9450f | 788 | res = slpeel_tree_duplicate_loop_to_edge_cfg (loop, NULL, preheader); |
30d55936 | 789 | gcc_assert (res != NULL); |
dea61d92 | 790 | free_original_copy_tables (); |
2cfc56b9 | 791 | delete_update_ssa (); |
dea61d92 SP |
792 | |
793 | return res; | |
794 | } | |
795 | ||
796 | /* Creates an empty basic block after LOOP. */ | |
797 | ||
798 | static void | |
799 | create_bb_after_loop (struct loop *loop) | |
800 | { | |
801 | edge exit = single_exit (loop); | |
802 | ||
803 | if (!exit) | |
804 | return; | |
805 | ||
806 | split_edge (exit); | |
807 | } | |
808 | ||
809 | /* Generate code for PARTITION from the code in LOOP. The loop is | |
810 | copied when COPY_P is true. All the statements not flagged in the | |
811 | PARTITION bitmap are removed from the loop or from its copy. The | |
812 | statements are indexed in sequence inside a basic block, and the | |
30d55936 | 813 | basic blocks of a loop are taken in dom order. */ |
dea61d92 | 814 | |
30d55936 | 815 | static void |
526ceb68 | 816 | generate_loops_for_partition (struct loop *loop, partition *partition, |
c61f8985 | 817 | bool copy_p) |
dea61d92 | 818 | { |
2fd5894f | 819 | unsigned i; |
dea61d92 SP |
820 | basic_block *bbs; |
821 | ||
822 | if (copy_p) | |
823 | { | |
a8745cc2 | 824 | int orig_loop_num = loop->orig_loop_num; |
dea61d92 | 825 | loop = copy_loop_before (loop); |
30d55936 | 826 | gcc_assert (loop != NULL); |
a8745cc2 | 827 | loop->orig_loop_num = orig_loop_num; |
dea61d92 SP |
828 | create_preheader (loop, CP_SIMPLE_PREHEADERS); |
829 | create_bb_after_loop (loop); | |
830 | } | |
a8745cc2 BC |
831 | else |
832 | { | |
833 | /* Origin number is set to the new versioned loop's num. */ | |
834 | gcc_assert (loop->orig_loop_num != loop->num); | |
835 | } | |
dea61d92 | 836 | |
2fd5894f | 837 | /* Remove stmts not in the PARTITION bitmap. */ |
dea61d92 SP |
838 | bbs = get_loop_body_in_dom_order (loop); |
839 | ||
36f52e8f | 840 | if (MAY_HAVE_DEBUG_BIND_STMTS) |
2fd5894f | 841 | for (i = 0; i < loop->num_nodes; i++) |
b03c3082 JJ |
842 | { |
843 | basic_block bb = bbs[i]; | |
844 | ||
538dd0b7 DM |
845 | for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi); |
846 | gsi_next (&bsi)) | |
2fd5894f | 847 | { |
538dd0b7 | 848 | gphi *phi = bsi.phi (); |
2fd5894f RB |
849 | if (!virtual_operand_p (gimple_phi_result (phi)) |
850 | && !bitmap_bit_p (partition->stmts, gimple_uid (phi))) | |
851 | reset_debug_uses (phi); | |
852 | } | |
b03c3082 | 853 | |
538dd0b7 | 854 | for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi)) |
b03c3082 | 855 | { |
355fe088 | 856 | gimple *stmt = gsi_stmt (bsi); |
b03c3082 JJ |
857 | if (gimple_code (stmt) != GIMPLE_LABEL |
858 | && !is_gimple_debug (stmt) | |
2fd5894f | 859 | && !bitmap_bit_p (partition->stmts, gimple_uid (stmt))) |
b03c3082 JJ |
860 | reset_debug_uses (stmt); |
861 | } | |
862 | } | |
863 | ||
2fd5894f | 864 | for (i = 0; i < loop->num_nodes; i++) |
dea61d92 SP |
865 | { |
866 | basic_block bb = bbs[i]; | |
efe040bf BC |
867 | edge inner_exit = NULL; |
868 | ||
869 | if (loop != bb->loop_father) | |
870 | inner_exit = single_exit (bb->loop_father); | |
dea61d92 | 871 | |
538dd0b7 | 872 | for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);) |
2fd5894f | 873 | { |
538dd0b7 | 874 | gphi *phi = bsi.phi (); |
2fd5894f RB |
875 | if (!virtual_operand_p (gimple_phi_result (phi)) |
876 | && !bitmap_bit_p (partition->stmts, gimple_uid (phi))) | |
2706a615 | 877 | remove_phi_node (&bsi, true); |
2fd5894f RB |
878 | else |
879 | gsi_next (&bsi); | |
880 | } | |
dea61d92 | 881 | |
538dd0b7 | 882 | for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);) |
2706a615 | 883 | { |
355fe088 | 884 | gimple *stmt = gsi_stmt (bsi); |
b03c3082 JJ |
885 | if (gimple_code (stmt) != GIMPLE_LABEL |
886 | && !is_gimple_debug (stmt) | |
2fd5894f | 887 | && !bitmap_bit_p (partition->stmts, gimple_uid (stmt))) |
2706a615 | 888 | { |
efe040bf BC |
889 | /* In distribution of loop nest, if bb is inner loop's exit_bb, |
890 | we choose its exit edge/path in order to avoid generating | |
891 | infinite loop. For all other cases, we choose an arbitrary | |
892 | path through the empty CFG part that this unnecessary | |
893 | control stmt controls. */ | |
538dd0b7 | 894 | if (gcond *cond_stmt = dyn_cast <gcond *> (stmt)) |
36875e8f | 895 | { |
efe040bf BC |
896 | if (inner_exit && inner_exit->flags & EDGE_TRUE_VALUE) |
897 | gimple_cond_make_true (cond_stmt); | |
898 | else | |
899 | gimple_cond_make_false (cond_stmt); | |
36875e8f RB |
900 | update_stmt (stmt); |
901 | } | |
902 | else if (gimple_code (stmt) == GIMPLE_SWITCH) | |
903 | { | |
538dd0b7 | 904 | gswitch *switch_stmt = as_a <gswitch *> (stmt); |
36875e8f | 905 | gimple_switch_set_index |
538dd0b7 | 906 | (switch_stmt, CASE_LOW (gimple_switch_label (switch_stmt, 1))); |
36875e8f RB |
907 | update_stmt (stmt); |
908 | } | |
909 | else | |
910 | { | |
911 | unlink_stmt_vdef (stmt); | |
912 | gsi_remove (&bsi, true); | |
913 | release_defs (stmt); | |
914 | continue; | |
915 | } | |
2706a615 | 916 | } |
36875e8f | 917 | gsi_next (&bsi); |
2706a615 | 918 | } |
dea61d92 SP |
919 | } |
920 | ||
921 | free (bbs); | |
dea61d92 SP |
922 | } |
923 | ||
401f3a81 JJ |
924 | /* If VAL memory representation contains the same value in all bytes, |
925 | return that value, otherwise return -1. | |
926 | E.g. for 0x24242424 return 0x24, for IEEE double | |
927 | 747708026454360457216.0 return 0x44, etc. */ | |
928 | ||
929 | static int | |
930 | const_with_all_bytes_same (tree val) | |
931 | { | |
932 | unsigned char buf[64]; | |
933 | int i, len; | |
934 | ||
935 | if (integer_zerop (val) | |
401f3a81 JJ |
936 | || (TREE_CODE (val) == CONSTRUCTOR |
937 | && !TREE_CLOBBER_P (val) | |
938 | && CONSTRUCTOR_NELTS (val) == 0)) | |
939 | return 0; | |
940 | ||
9e207d6f JJ |
941 | if (real_zerop (val)) |
942 | { | |
943 | /* Only return 0 for +0.0, not for -0.0, which doesn't have | |
944 | an all bytes same memory representation. Don't transform | |
945 | -0.0 stores into +0.0 even for !HONOR_SIGNED_ZEROS. */ | |
946 | switch (TREE_CODE (val)) | |
947 | { | |
948 | case REAL_CST: | |
949 | if (!real_isneg (TREE_REAL_CST_PTR (val))) | |
950 | return 0; | |
951 | break; | |
952 | case COMPLEX_CST: | |
953 | if (!const_with_all_bytes_same (TREE_REALPART (val)) | |
954 | && !const_with_all_bytes_same (TREE_IMAGPART (val))) | |
955 | return 0; | |
956 | break; | |
957 | case VECTOR_CST: | |
63570af0 RS |
958 | { |
959 | unsigned int count = vector_cst_encoded_nelts (val); | |
960 | unsigned int j; | |
961 | for (j = 0; j < count; ++j) | |
962 | if (const_with_all_bytes_same (VECTOR_CST_ENCODED_ELT (val, j))) | |
963 | break; | |
964 | if (j == count) | |
965 | return 0; | |
966 | break; | |
967 | } | |
9e207d6f JJ |
968 | default: |
969 | break; | |
970 | } | |
971 | } | |
972 | ||
401f3a81 JJ |
973 | if (CHAR_BIT != 8 || BITS_PER_UNIT != 8) |
974 | return -1; | |
975 | ||
976 | len = native_encode_expr (val, buf, sizeof (buf)); | |
977 | if (len == 0) | |
978 | return -1; | |
979 | for (i = 1; i < len; i++) | |
980 | if (buf[i] != buf[0]) | |
981 | return -1; | |
982 | return buf[0]; | |
983 | } | |
984 | ||
30d55936 | 985 | /* Generate a call to memset for PARTITION in LOOP. */ |
dea61d92 | 986 | |
cfee318d | 987 | static void |
526ceb68 | 988 | generate_memset_builtin (struct loop *loop, partition *partition) |
dea61d92 | 989 | { |
30d55936 | 990 | gimple_stmt_iterator gsi; |
818625cf | 991 | tree mem, fn, nb_bytes; |
b6dd5261 | 992 | tree val; |
939cf90f BC |
993 | struct builtin_info *builtin = partition->builtin; |
994 | gimple *fn_call; | |
30d55936 RG |
995 | |
996 | /* The new statements will be placed before LOOP. */ | |
997 | gsi = gsi_last_bb (loop_preheader_edge (loop)->src); | |
dea61d92 | 998 | |
939cf90f | 999 | nb_bytes = builtin->size; |
d0582dc1 RG |
1000 | nb_bytes = force_gimple_operand_gsi (&gsi, nb_bytes, true, NULL_TREE, |
1001 | false, GSI_CONTINUE_LINKING); | |
939cf90f | 1002 | mem = builtin->dst_base; |
d0582dc1 RG |
1003 | mem = force_gimple_operand_gsi (&gsi, mem, true, NULL_TREE, |
1004 | false, GSI_CONTINUE_LINKING); | |
dea61d92 | 1005 | |
b6dd5261 | 1006 | /* This exactly matches the pattern recognition in classify_partition. */ |
939cf90f | 1007 | val = gimple_assign_rhs1 (DR_STMT (builtin->dst_dr)); |
401f3a81 JJ |
1008 | /* Handle constants like 0x15151515 and similarly |
1009 | floating point constants etc. where all bytes are the same. */ | |
1010 | int bytev = const_with_all_bytes_same (val); | |
1011 | if (bytev != -1) | |
1012 | val = build_int_cst (integer_type_node, bytev); | |
1013 | else if (TREE_CODE (val) == INTEGER_CST) | |
1014 | val = fold_convert (integer_type_node, val); | |
1015 | else if (!useless_type_conversion_p (integer_type_node, TREE_TYPE (val))) | |
b6dd5261 | 1016 | { |
b731b390 | 1017 | tree tem = make_ssa_name (integer_type_node); |
355fe088 | 1018 | gimple *cstmt = gimple_build_assign (tem, NOP_EXPR, val); |
401f3a81 JJ |
1019 | gsi_insert_after (&gsi, cstmt, GSI_CONTINUE_LINKING); |
1020 | val = tem; | |
b6dd5261 RG |
1021 | } |
1022 | ||
e79983f4 | 1023 | fn = build_fold_addr_expr (builtin_decl_implicit (BUILT_IN_MEMSET)); |
b6dd5261 | 1024 | fn_call = gimple_build_call (fn, 3, mem, val, nb_bytes); |
d0582dc1 | 1025 | gsi_insert_after (&gsi, fn_call, GSI_CONTINUE_LINKING); |
dea61d92 SP |
1026 | |
1027 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
b6dd5261 RG |
1028 | { |
1029 | fprintf (dump_file, "generated memset"); | |
401f3a81 | 1030 | if (bytev == 0) |
b6dd5261 | 1031 | fprintf (dump_file, " zero\n"); |
b6dd5261 RG |
1032 | else |
1033 | fprintf (dump_file, "\n"); | |
1034 | } | |
dea61d92 SP |
1035 | } |
1036 | ||
d0582dc1 RG |
1037 | /* Generate a call to memcpy for PARTITION in LOOP. */ |
1038 | ||
1039 | static void | |
526ceb68 | 1040 | generate_memcpy_builtin (struct loop *loop, partition *partition) |
d0582dc1 RG |
1041 | { |
1042 | gimple_stmt_iterator gsi; | |
939cf90f | 1043 | gimple *fn_call; |
818625cf | 1044 | tree dest, src, fn, nb_bytes; |
d0582dc1 | 1045 | enum built_in_function kind; |
939cf90f | 1046 | struct builtin_info *builtin = partition->builtin; |
d0582dc1 RG |
1047 | |
1048 | /* The new statements will be placed before LOOP. */ | |
1049 | gsi = gsi_last_bb (loop_preheader_edge (loop)->src); | |
1050 | ||
939cf90f | 1051 | nb_bytes = builtin->size; |
d0582dc1 RG |
1052 | nb_bytes = force_gimple_operand_gsi (&gsi, nb_bytes, true, NULL_TREE, |
1053 | false, GSI_CONTINUE_LINKING); | |
939cf90f BC |
1054 | dest = builtin->dst_base; |
1055 | src = builtin->src_base; | |
510d73a0 RB |
1056 | if (partition->kind == PKIND_MEMCPY |
1057 | || ! ptr_derefs_may_alias_p (dest, src)) | |
d0582dc1 | 1058 | kind = BUILT_IN_MEMCPY; |
510d73a0 RB |
1059 | else |
1060 | kind = BUILT_IN_MEMMOVE; | |
d0582dc1 RG |
1061 | |
1062 | dest = force_gimple_operand_gsi (&gsi, dest, true, NULL_TREE, | |
1063 | false, GSI_CONTINUE_LINKING); | |
1064 | src = force_gimple_operand_gsi (&gsi, src, true, NULL_TREE, | |
1065 | false, GSI_CONTINUE_LINKING); | |
1066 | fn = build_fold_addr_expr (builtin_decl_implicit (kind)); | |
1067 | fn_call = gimple_build_call (fn, 3, dest, src, nb_bytes); | |
1068 | gsi_insert_after (&gsi, fn_call, GSI_CONTINUE_LINKING); | |
1069 | ||
1070 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1071 | { | |
1072 | if (kind == BUILT_IN_MEMCPY) | |
1073 | fprintf (dump_file, "generated memcpy\n"); | |
1074 | else | |
1075 | fprintf (dump_file, "generated memmove\n"); | |
1076 | } | |
1077 | } | |
1078 | ||
30d55936 | 1079 | /* Remove and destroy the loop LOOP. */ |
dea61d92 | 1080 | |
30d55936 RG |
1081 | static void |
1082 | destroy_loop (struct loop *loop) | |
dea61d92 | 1083 | { |
30d55936 RG |
1084 | unsigned nbbs = loop->num_nodes; |
1085 | edge exit = single_exit (loop); | |
1086 | basic_block src = loop_preheader_edge (loop)->src, dest = exit->dest; | |
dea61d92 | 1087 | basic_block *bbs; |
30d55936 | 1088 | unsigned i; |
dea61d92 SP |
1089 | |
1090 | bbs = get_loop_body_in_dom_order (loop); | |
1091 | ||
30d55936 RG |
1092 | redirect_edge_pred (exit, src); |
1093 | exit->flags &= ~(EDGE_TRUE_VALUE|EDGE_FALSE_VALUE); | |
1094 | exit->flags |= EDGE_FALLTHRU; | |
1095 | cancel_loop_tree (loop); | |
1096 | rescan_loop_exit (exit, false, true); | |
dea61d92 | 1097 | |
b9aba0a0 RB |
1098 | i = nbbs; |
1099 | do | |
c014f6f5 RG |
1100 | { |
1101 | /* We have made sure to not leave any dangling uses of SSA | |
1102 | names defined in the loop. With the exception of virtuals. | |
1103 | Make sure we replace all uses of virtual defs that will remain | |
1104 | outside of the loop with the bare symbol as delete_basic_block | |
1105 | will release them. */ | |
b9aba0a0 | 1106 | --i; |
538dd0b7 DM |
1107 | for (gphi_iterator gsi = gsi_start_phis (bbs[i]); !gsi_end_p (gsi); |
1108 | gsi_next (&gsi)) | |
c014f6f5 | 1109 | { |
538dd0b7 | 1110 | gphi *phi = gsi.phi (); |
ea057359 | 1111 | if (virtual_operand_p (gimple_phi_result (phi))) |
c014f6f5 RG |
1112 | mark_virtual_phi_result_for_renaming (phi); |
1113 | } | |
538dd0b7 DM |
1114 | for (gimple_stmt_iterator gsi = gsi_start_bb (bbs[i]); !gsi_end_p (gsi); |
1115 | gsi_next (&gsi)) | |
c014f6f5 | 1116 | { |
355fe088 | 1117 | gimple *stmt = gsi_stmt (gsi); |
c014f6f5 RG |
1118 | tree vdef = gimple_vdef (stmt); |
1119 | if (vdef && TREE_CODE (vdef) == SSA_NAME) | |
1120 | mark_virtual_operand_for_renaming (vdef); | |
1121 | } | |
1122 | delete_basic_block (bbs[i]); | |
1123 | } | |
b9aba0a0 RB |
1124 | while (i != 0); |
1125 | ||
dea61d92 | 1126 | free (bbs); |
30d55936 RG |
1127 | |
1128 | set_immediate_dominator (CDI_DOMINATORS, dest, | |
1129 | recompute_dominator (CDI_DOMINATORS, dest)); | |
dea61d92 SP |
1130 | } |
1131 | ||
b71b7a8e | 1132 | /* Generates code for PARTITION. Return whether LOOP needs to be destroyed. */ |
dea61d92 | 1133 | |
b71b7a8e | 1134 | static bool |
d0582dc1 | 1135 | generate_code_for_partition (struct loop *loop, |
526ceb68 | 1136 | partition *partition, bool copy_p) |
dea61d92 | 1137 | { |
30d55936 RG |
1138 | switch (partition->kind) |
1139 | { | |
826a536d | 1140 | case PKIND_NORMAL: |
5955438a | 1141 | case PKIND_PARTIAL_MEMSET: |
826a536d RB |
1142 | /* Reductions all have to be in the last partition. */ |
1143 | gcc_assert (!partition_reduction_p (partition) | |
1144 | || !copy_p); | |
1145 | generate_loops_for_partition (loop, partition, copy_p); | |
b71b7a8e | 1146 | return false; |
826a536d | 1147 | |
30d55936 | 1148 | case PKIND_MEMSET: |
d0582dc1 | 1149 | generate_memset_builtin (loop, partition); |
d0582dc1 RG |
1150 | break; |
1151 | ||
1152 | case PKIND_MEMCPY: | |
510d73a0 | 1153 | case PKIND_MEMMOVE: |
d0582dc1 | 1154 | generate_memcpy_builtin (loop, partition); |
30d55936 RG |
1155 | break; |
1156 | ||
1157 | default: | |
1158 | gcc_unreachable (); | |
1159 | } | |
dea61d92 | 1160 | |
826a536d RB |
1161 | /* Common tail for partitions we turn into a call. If this was the last |
1162 | partition for which we generate code, we have to destroy the loop. */ | |
1163 | if (!copy_p) | |
b71b7a8e RB |
1164 | return true; |
1165 | return false; | |
dea61d92 SP |
1166 | } |
1167 | ||
17c5cbdf BC |
1168 | /* Return data dependence relation for data references A and B. The two |
1169 | data references must be in lexicographic order wrto reduced dependence | |
1170 | graph RDG. We firstly try to find ddr from global ddr hash table. If | |
1171 | it doesn't exist, compute the ddr and cache it. */ | |
1172 | ||
1173 | static data_dependence_relation * | |
1174 | get_data_dependence (struct graph *rdg, data_reference_p a, data_reference_p b) | |
1175 | { | |
1176 | struct data_dependence_relation ent, **slot; | |
1177 | struct data_dependence_relation *ddr; | |
1178 | ||
1179 | gcc_assert (DR_IS_WRITE (a) || DR_IS_WRITE (b)); | |
1180 | gcc_assert (rdg_vertex_for_stmt (rdg, DR_STMT (a)) | |
1181 | <= rdg_vertex_for_stmt (rdg, DR_STMT (b))); | |
1182 | ent.a = a; | |
1183 | ent.b = b; | |
1e485f89 | 1184 | slot = ddrs_table->find_slot (&ent, INSERT); |
17c5cbdf BC |
1185 | if (*slot == NULL) |
1186 | { | |
1187 | ddr = initialize_data_dependence_relation (a, b, loop_nest); | |
1188 | compute_affine_dependence (ddr, loop_nest[0]); | |
1189 | *slot = ddr; | |
1190 | } | |
1191 | ||
1192 | return *slot; | |
1193 | } | |
dea61d92 | 1194 | |
f1eb4621 BC |
1195 | /* In reduced dependence graph RDG for loop distribution, return true if |
1196 | dependence between references DR1 and DR2 leads to a dependence cycle | |
1197 | and such dependence cycle can't be resolved by runtime alias check. */ | |
1198 | ||
1199 | static bool | |
1200 | data_dep_in_cycle_p (struct graph *rdg, | |
1201 | data_reference_p dr1, data_reference_p dr2) | |
1202 | { | |
1203 | struct data_dependence_relation *ddr; | |
1204 | ||
1205 | /* Re-shuffle data-refs to be in topological order. */ | |
1206 | if (rdg_vertex_for_stmt (rdg, DR_STMT (dr1)) | |
1207 | > rdg_vertex_for_stmt (rdg, DR_STMT (dr2))) | |
1208 | std::swap (dr1, dr2); | |
1209 | ||
1210 | ddr = get_data_dependence (rdg, dr1, dr2); | |
1211 | ||
1212 | /* In case of no data dependence. */ | |
1213 | if (DDR_ARE_DEPENDENT (ddr) == chrec_known) | |
1214 | return false; | |
1215 | /* For unknown data dependence or known data dependence which can't be | |
1216 | expressed in classic distance vector, we check if it can be resolved | |
1217 | by runtime alias check. If yes, we still consider data dependence | |
1218 | as won't introduce data dependence cycle. */ | |
1219 | else if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know | |
1220 | || DDR_NUM_DIST_VECTS (ddr) == 0) | |
1221 | return !runtime_alias_check_p (ddr, NULL, true); | |
1222 | else if (DDR_NUM_DIST_VECTS (ddr) > 1) | |
1223 | return true; | |
1224 | else if (DDR_REVERSED_P (ddr) | |
1225 | || lambda_vector_zerop (DDR_DIST_VECT (ddr, 0), 1)) | |
1226 | return false; | |
1227 | ||
1228 | return true; | |
1229 | } | |
1230 | ||
1231 | /* Given reduced dependence graph RDG, PARTITION1 and PARTITION2, update | |
1232 | PARTITION1's type after merging PARTITION2 into PARTITION1. */ | |
1233 | ||
1234 | static void | |
1235 | update_type_for_merge (struct graph *rdg, | |
1236 | partition *partition1, partition *partition2) | |
1237 | { | |
1238 | unsigned i, j; | |
1239 | bitmap_iterator bi, bj; | |
1240 | data_reference_p dr1, dr2; | |
1241 | ||
1242 | EXECUTE_IF_SET_IN_BITMAP (partition1->datarefs, 0, i, bi) | |
1243 | { | |
1244 | unsigned start = (partition1 == partition2) ? i + 1 : 0; | |
1245 | ||
1246 | dr1 = datarefs_vec[i]; | |
1247 | EXECUTE_IF_SET_IN_BITMAP (partition2->datarefs, start, j, bj) | |
1248 | { | |
1249 | dr2 = datarefs_vec[j]; | |
1250 | if (DR_IS_READ (dr1) && DR_IS_READ (dr2)) | |
1251 | continue; | |
1252 | ||
1253 | /* Partition can only be executed sequentially if there is any | |
1254 | data dependence cycle. */ | |
1255 | if (data_dep_in_cycle_p (rdg, dr1, dr2)) | |
1256 | { | |
1257 | partition1->type = PTYPE_SEQUENTIAL; | |
1258 | return; | |
1259 | } | |
1260 | } | |
1261 | } | |
1262 | } | |
1263 | ||
24f161fd RB |
1264 | /* Returns a partition with all the statements needed for computing |
1265 | the vertex V of the RDG, also including the loop exit conditions. */ | |
dea61d92 | 1266 | |
526ceb68 | 1267 | static partition * |
24f161fd | 1268 | build_rdg_partition_for_vertex (struct graph *rdg, int v) |
dea61d92 | 1269 | { |
a7a44c07 | 1270 | partition *partition = partition_alloc (); |
00f96dc9 | 1271 | auto_vec<int, 3> nodes; |
a7a44c07 | 1272 | unsigned i, j; |
dea61d92 | 1273 | int x; |
a7a44c07 | 1274 | data_reference_p dr; |
dea61d92 | 1275 | |
174ec470 | 1276 | graphds_dfs (rdg, &v, 1, &nodes, false, NULL); |
dea61d92 | 1277 | |
9771b263 | 1278 | FOR_EACH_VEC_ELT (nodes, i, x) |
24f161fd RB |
1279 | { |
1280 | bitmap_set_bit (partition->stmts, x); | |
a7a44c07 BC |
1281 | |
1282 | for (j = 0; RDG_DATAREFS (rdg, x).iterate (j, &dr); ++j) | |
1283 | { | |
1284 | unsigned idx = (unsigned) DR_INDEX (dr); | |
1285 | gcc_assert (idx < datarefs_vec.length ()); | |
1286 | ||
f1eb4621 BC |
1287 | /* Partition can only be executed sequentially if there is any |
1288 | unknown data reference. */ | |
1289 | if (!DR_BASE_ADDRESS (dr) || !DR_OFFSET (dr) | |
1290 | || !DR_INIT (dr) || !DR_STEP (dr)) | |
1291 | partition->type = PTYPE_SEQUENTIAL; | |
1292 | ||
a7a44c07 BC |
1293 | bitmap_set_bit (partition->datarefs, idx); |
1294 | } | |
24f161fd | 1295 | } |
dea61d92 | 1296 | |
f1eb4621 BC |
1297 | if (partition->type == PTYPE_SEQUENTIAL) |
1298 | return partition; | |
1299 | ||
1300 | /* Further check if any data dependence prevents us from executing the | |
1301 | partition parallelly. */ | |
1302 | update_type_for_merge (rdg, partition, partition); | |
1303 | ||
dea61d92 SP |
1304 | return partition; |
1305 | } | |
1306 | ||
85aa9ed6 BC |
1307 | /* Given PARTITION of LOOP and RDG, record single load/store data references |
1308 | for builtin partition in SRC_DR/DST_DR, return false if there is no such | |
939cf90f | 1309 | data references. */ |
cfee318d | 1310 | |
939cf90f | 1311 | static bool |
85aa9ed6 | 1312 | find_single_drs (struct loop *loop, struct graph *rdg, partition *partition, |
939cf90f | 1313 | data_reference_p *dst_dr, data_reference_p *src_dr) |
cfee318d | 1314 | { |
30d55936 | 1315 | unsigned i; |
939cf90f BC |
1316 | data_reference_p single_ld = NULL, single_st = NULL; |
1317 | bitmap_iterator bi; | |
b9fc0497 | 1318 | |
30d55936 RG |
1319 | EXECUTE_IF_SET_IN_BITMAP (partition->stmts, 0, i, bi) |
1320 | { | |
355fe088 | 1321 | gimple *stmt = RDG_STMT (rdg, i); |
d0582dc1 | 1322 | data_reference_p dr; |
30d55936 RG |
1323 | |
1324 | if (gimple_code (stmt) == GIMPLE_PHI) | |
1325 | continue; | |
1326 | ||
1327 | /* Any scalar stmts are ok. */ | |
1328 | if (!gimple_vuse (stmt)) | |
1329 | continue; | |
1330 | ||
d0582dc1 RG |
1331 | /* Otherwise just regular loads/stores. */ |
1332 | if (!gimple_assign_single_p (stmt)) | |
939cf90f | 1333 | return false; |
d0582dc1 RG |
1334 | |
1335 | /* But exactly one store and/or load. */ | |
939cf90f | 1336 | for (unsigned j = 0; RDG_DATAREFS (rdg, i).iterate (j, &dr); ++j) |
30d55936 | 1337 | { |
d002d099 JJ |
1338 | tree type = TREE_TYPE (DR_REF (dr)); |
1339 | ||
1340 | /* The memset, memcpy and memmove library calls are only | |
1341 | able to deal with generic address space. */ | |
1342 | if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))) | |
939cf90f | 1343 | return false; |
d002d099 | 1344 | |
d0582dc1 RG |
1345 | if (DR_IS_READ (dr)) |
1346 | { | |
939cf90f BC |
1347 | if (single_ld != NULL) |
1348 | return false; | |
1349 | single_ld = dr; | |
d0582dc1 RG |
1350 | } |
1351 | else | |
1352 | { | |
939cf90f BC |
1353 | if (single_st != NULL) |
1354 | return false; | |
1355 | single_st = dr; | |
d0582dc1 | 1356 | } |
30d55936 | 1357 | } |
30d55936 RG |
1358 | } |
1359 | ||
939cf90f BC |
1360 | if (!single_st) |
1361 | return false; | |
1362 | ||
1363 | /* Bail out if this is a bitfield memory reference. */ | |
1364 | if (TREE_CODE (DR_REF (single_st)) == COMPONENT_REF | |
1365 | && DECL_BIT_FIELD (TREE_OPERAND (DR_REF (single_st), 1))) | |
1366 | return false; | |
818625cf | 1367 | |
85aa9ed6 BC |
1368 | /* Data reference must be executed exactly once per iteration of each |
1369 | loop in the loop nest. We only need to check dominance information | |
1370 | against the outermost one in a perfect loop nest because a bb can't | |
1371 | dominate outermost loop's latch without dominating inner loop's. */ | |
939cf90f | 1372 | basic_block bb_st = gimple_bb (DR_STMT (single_st)); |
85aa9ed6 | 1373 | if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb_st)) |
939cf90f BC |
1374 | return false; |
1375 | ||
1376 | if (single_ld) | |
163aa51b | 1377 | { |
939cf90f BC |
1378 | gimple *store = DR_STMT (single_st), *load = DR_STMT (single_ld); |
1379 | /* Direct aggregate copy or via an SSA name temporary. */ | |
1380 | if (load != store | |
1381 | && gimple_assign_lhs (load) != gimple_assign_rhs1 (store)) | |
1382 | return false; | |
163aa51b | 1383 | |
939cf90f BC |
1384 | /* Bail out if this is a bitfield memory reference. */ |
1385 | if (TREE_CODE (DR_REF (single_ld)) == COMPONENT_REF | |
1386 | && DECL_BIT_FIELD (TREE_OPERAND (DR_REF (single_ld), 1))) | |
1387 | return false; | |
1388 | ||
1389 | /* Load and store must be in the same loop nest. */ | |
1390 | basic_block bb_ld = gimple_bb (DR_STMT (single_ld)); | |
85aa9ed6 | 1391 | if (bb_st->loop_father != bb_ld->loop_father) |
939cf90f BC |
1392 | return false; |
1393 | ||
85aa9ed6 BC |
1394 | /* Data reference must be executed exactly once per iteration. |
1395 | Same as single_st, we only need to check against the outermost | |
1396 | loop. */ | |
1397 | if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb_ld)) | |
939cf90f BC |
1398 | return false; |
1399 | ||
85aa9ed6 | 1400 | edge e = single_exit (bb_st->loop_father); |
939cf90f BC |
1401 | bool dom_ld = dominated_by_p (CDI_DOMINATORS, e->src, bb_ld); |
1402 | bool dom_st = dominated_by_p (CDI_DOMINATORS, e->src, bb_st); | |
1403 | if (dom_ld != dom_st) | |
1404 | return false; | |
1405 | } | |
1406 | ||
1407 | *src_dr = single_ld; | |
1408 | *dst_dr = single_st; | |
1409 | return true; | |
1410 | } | |
1411 | ||
1412 | /* Given data reference DR in LOOP_NEST, this function checks the enclosing | |
1413 | loops from inner to outer to see if loop's step equals to access size at | |
5955438a BC |
1414 | each level of loop. Return 2 if we can prove this at all level loops; |
1415 | record access base and size in BASE and SIZE; save loop's step at each | |
1416 | level of loop in STEPS if it is not null. For example: | |
939cf90f BC |
1417 | |
1418 | int arr[100][100][100]; | |
1419 | for (i = 0; i < 100; i++) ;steps[2] = 40000 | |
1420 | for (j = 100; j > 0; j--) ;steps[1] = -400 | |
1421 | for (k = 0; k < 100; k++) ;steps[0] = 4 | |
5955438a | 1422 | arr[i][j - 1][k] = 0; ;base = &arr, size = 4000000 |
939cf90f | 1423 | |
5955438a BC |
1424 | Return 1 if we can prove the equality at the innermost loop, but not all |
1425 | level loops. In this case, no information is recorded. | |
1426 | ||
1427 | Return 0 if no equality can be proven at any level loops. */ | |
1428 | ||
1429 | static int | |
939cf90f BC |
1430 | compute_access_range (loop_p loop_nest, data_reference_p dr, tree *base, |
1431 | tree *size, vec<tree> *steps = NULL) | |
1432 | { | |
1433 | location_t loc = gimple_location (DR_STMT (dr)); | |
1434 | basic_block bb = gimple_bb (DR_STMT (dr)); | |
1435 | struct loop *loop = bb->loop_father; | |
1436 | tree ref = DR_REF (dr); | |
1437 | tree access_base = build_fold_addr_expr (ref); | |
1438 | tree access_size = TYPE_SIZE_UNIT (TREE_TYPE (ref)); | |
5955438a | 1439 | int res = 0; |
939cf90f BC |
1440 | |
1441 | do { | |
1442 | tree scev_fn = analyze_scalar_evolution (loop, access_base); | |
1443 | if (TREE_CODE (scev_fn) != POLYNOMIAL_CHREC) | |
5955438a | 1444 | return res; |
163aa51b | 1445 | |
939cf90f BC |
1446 | access_base = CHREC_LEFT (scev_fn); |
1447 | if (tree_contains_chrecs (access_base, NULL)) | |
5955438a | 1448 | return res; |
939cf90f BC |
1449 | |
1450 | tree scev_step = CHREC_RIGHT (scev_fn); | |
1451 | /* Only support constant steps. */ | |
1452 | if (TREE_CODE (scev_step) != INTEGER_CST) | |
5955438a | 1453 | return res; |
939cf90f BC |
1454 | |
1455 | enum ev_direction access_dir = scev_direction (scev_fn); | |
1456 | if (access_dir == EV_DIR_UNKNOWN) | |
5955438a | 1457 | return res; |
939cf90f BC |
1458 | |
1459 | if (steps != NULL) | |
1460 | steps->safe_push (scev_step); | |
1461 | ||
1462 | scev_step = fold_convert_loc (loc, sizetype, scev_step); | |
1463 | /* Compute absolute value of scev step. */ | |
1464 | if (access_dir == EV_DIR_DECREASES) | |
1465 | scev_step = fold_build1_loc (loc, NEGATE_EXPR, sizetype, scev_step); | |
1466 | ||
1467 | /* At each level of loop, scev step must equal to access size. In other | |
1468 | words, DR must access consecutive memory between loop iterations. */ | |
1469 | if (!operand_equal_p (scev_step, access_size, 0)) | |
5955438a BC |
1470 | return res; |
1471 | ||
1472 | /* Access stride can be computed for data reference at least for the | |
1473 | innermost loop. */ | |
1474 | res = 1; | |
939cf90f BC |
1475 | |
1476 | /* Compute DR's execution times in loop. */ | |
1477 | tree niters = number_of_latch_executions (loop); | |
1478 | niters = fold_convert_loc (loc, sizetype, niters); | |
1479 | if (dominated_by_p (CDI_DOMINATORS, single_exit (loop)->src, bb)) | |
1480 | niters = size_binop_loc (loc, PLUS_EXPR, niters, size_one_node); | |
1481 | ||
1482 | /* Compute DR's overall access size in loop. */ | |
1483 | access_size = fold_build2_loc (loc, MULT_EXPR, sizetype, | |
1484 | niters, scev_step); | |
1485 | /* Adjust base address in case of negative step. */ | |
1486 | if (access_dir == EV_DIR_DECREASES) | |
163aa51b | 1487 | { |
939cf90f BC |
1488 | tree adj = fold_build2_loc (loc, MINUS_EXPR, sizetype, |
1489 | scev_step, access_size); | |
1490 | access_base = fold_build_pointer_plus_loc (loc, access_base, adj); | |
163aa51b | 1491 | } |
939cf90f BC |
1492 | } while (loop != loop_nest && (loop = loop_outer (loop)) != NULL); |
1493 | ||
1494 | *base = access_base; | |
1495 | *size = access_size; | |
5955438a BC |
1496 | /* Access stride can be computed for data reference at each level loop. */ |
1497 | return 2; | |
939cf90f BC |
1498 | } |
1499 | ||
1500 | /* Allocate and return builtin struct. Record information like DST_DR, | |
1501 | SRC_DR, DST_BASE, SRC_BASE and SIZE in the allocated struct. */ | |
1502 | ||
1503 | static struct builtin_info * | |
1504 | alloc_builtin (data_reference_p dst_dr, data_reference_p src_dr, | |
1505 | tree dst_base, tree src_base, tree size) | |
1506 | { | |
1507 | struct builtin_info *builtin = XNEW (struct builtin_info); | |
1508 | builtin->dst_dr = dst_dr; | |
1509 | builtin->src_dr = src_dr; | |
1510 | builtin->dst_base = dst_base; | |
1511 | builtin->src_base = src_base; | |
1512 | builtin->size = size; | |
1513 | return builtin; | |
1514 | } | |
1515 | ||
1516 | /* Given data reference DR in loop nest LOOP, classify if it forms builtin | |
1517 | memset call. */ | |
1518 | ||
1519 | static void | |
1520 | classify_builtin_st (loop_p loop, partition *partition, data_reference_p dr) | |
1521 | { | |
1522 | gimple *stmt = DR_STMT (dr); | |
1523 | tree base, size, rhs = gimple_assign_rhs1 (stmt); | |
1524 | ||
1525 | if (const_with_all_bytes_same (rhs) == -1 | |
1526 | && (!INTEGRAL_TYPE_P (TREE_TYPE (rhs)) | |
1527 | || (TYPE_MODE (TREE_TYPE (rhs)) | |
1528 | != TYPE_MODE (unsigned_char_type_node)))) | |
1529 | return; | |
1530 | ||
1531 | if (TREE_CODE (rhs) == SSA_NAME | |
1532 | && !SSA_NAME_IS_DEFAULT_DEF (rhs) | |
1533 | && flow_bb_inside_loop_p (loop, gimple_bb (SSA_NAME_DEF_STMT (rhs)))) | |
1534 | return; | |
1535 | ||
5955438a BC |
1536 | int res = compute_access_range (loop, dr, &base, &size); |
1537 | if (res == 0) | |
939cf90f | 1538 | return; |
5955438a BC |
1539 | if (res == 1) |
1540 | { | |
1541 | partition->kind = PKIND_PARTIAL_MEMSET; | |
1542 | return; | |
1543 | } | |
939cf90f | 1544 | |
d2fd6a04 RS |
1545 | poly_uint64 base_offset; |
1546 | unsigned HOST_WIDE_INT const_base_offset; | |
1547 | tree base_base = strip_offset (base, &base_offset); | |
1548 | if (!base_offset.is_constant (&const_base_offset)) | |
1549 | return; | |
1550 | ||
957f0d8f BC |
1551 | struct builtin_info *builtin; |
1552 | builtin = alloc_builtin (dr, NULL, base, NULL_TREE, size); | |
d2fd6a04 RS |
1553 | builtin->dst_base_base = base_base; |
1554 | builtin->dst_base_offset = const_base_offset; | |
957f0d8f | 1555 | partition->builtin = builtin; |
939cf90f BC |
1556 | partition->kind = PKIND_MEMSET; |
1557 | } | |
1558 | ||
1559 | /* Given data references DST_DR and SRC_DR in loop nest LOOP and RDG, classify | |
1560 | if it forms builtin memcpy or memmove call. */ | |
1561 | ||
1562 | static void | |
1563 | classify_builtin_ldst (loop_p loop, struct graph *rdg, partition *partition, | |
1564 | data_reference_p dst_dr, data_reference_p src_dr) | |
1565 | { | |
1566 | tree base, size, src_base, src_size; | |
1567 | auto_vec<tree> dst_steps, src_steps; | |
1568 | ||
5955438a BC |
1569 | /* Compute access range of both load and store. */ |
1570 | int res = compute_access_range (loop, dst_dr, &base, &size, &dst_steps); | |
1571 | if (res != 2) | |
1572 | return; | |
1573 | res = compute_access_range (loop, src_dr, &src_base, &src_size, &src_steps); | |
1574 | if (res != 2) | |
1575 | return; | |
1576 | ||
1577 | /* They much have the same access size. */ | |
1578 | if (!operand_equal_p (size, src_size, 0)) | |
939cf90f BC |
1579 | return; |
1580 | ||
1581 | /* Load and store in loop nest must access memory in the same way, i.e, | |
1582 | their must have the same steps in each loop of the nest. */ | |
1583 | if (dst_steps.length () != src_steps.length ()) | |
1584 | return; | |
1585 | for (unsigned i = 0; i < dst_steps.length (); ++i) | |
1586 | if (!operand_equal_p (dst_steps[i], src_steps[i], 0)) | |
1587 | return; | |
1588 | ||
1589 | /* Now check that if there is a dependence. */ | |
1590 | ddr_p ddr = get_data_dependence (rdg, src_dr, dst_dr); | |
1591 | ||
1592 | /* Classify as memcpy if no dependence between load and store. */ | |
1593 | if (DDR_ARE_DEPENDENT (ddr) == chrec_known) | |
1594 | { | |
1595 | partition->builtin = alloc_builtin (dst_dr, src_dr, base, src_base, size); | |
1596 | partition->kind = PKIND_MEMCPY; | |
1597 | return; | |
163aa51b BC |
1598 | } |
1599 | ||
939cf90f BC |
1600 | /* Can't do memmove in case of unknown dependence or dependence without |
1601 | classical distance vector. */ | |
1602 | if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know | |
1603 | || DDR_NUM_DIST_VECTS (ddr) == 0) | |
1604 | return; | |
818625cf | 1605 | |
939cf90f BC |
1606 | unsigned i; |
1607 | lambda_vector dist_v; | |
1608 | int num_lev = (DDR_LOOP_NEST (ddr)).length (); | |
1609 | FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v) | |
d0582dc1 | 1610 | { |
939cf90f BC |
1611 | unsigned dep_lev = dependence_level (dist_v, num_lev); |
1612 | /* Can't do memmove if load depends on store. */ | |
1613 | if (dep_lev > 0 && dist_v[dep_lev - 1] > 0 && !DDR_REVERSED_P (ddr)) | |
d0582dc1 | 1614 | return; |
d0582dc1 | 1615 | } |
939cf90f BC |
1616 | |
1617 | partition->builtin = alloc_builtin (dst_dr, src_dr, base, src_base, size); | |
1618 | partition->kind = PKIND_MEMMOVE; | |
1619 | return; | |
1620 | } | |
1621 | ||
1622 | /* Classifies the builtin kind we can generate for PARTITION of RDG and LOOP. | |
1623 | For the moment we detect memset, memcpy and memmove patterns. Bitmap | |
1624 | STMT_IN_ALL_PARTITIONS contains statements belonging to all partitions. */ | |
1625 | ||
1626 | static void | |
1627 | classify_partition (loop_p loop, struct graph *rdg, partition *partition, | |
1628 | bitmap stmt_in_all_partitions) | |
1629 | { | |
1630 | bitmap_iterator bi; | |
1631 | unsigned i; | |
1632 | data_reference_p single_ld = NULL, single_st = NULL; | |
1633 | bool volatiles_p = false, has_reduction = false; | |
1634 | ||
1635 | EXECUTE_IF_SET_IN_BITMAP (partition->stmts, 0, i, bi) | |
d0582dc1 | 1636 | { |
939cf90f | 1637 | gimple *stmt = RDG_STMT (rdg, i); |
17c5cbdf | 1638 | |
939cf90f BC |
1639 | if (gimple_has_volatile_ops (stmt)) |
1640 | volatiles_p = true; | |
17c5cbdf | 1641 | |
939cf90f BC |
1642 | /* If the stmt is not included by all partitions and there is uses |
1643 | outside of the loop, then mark the partition as reduction. */ | |
1644 | if (stmt_has_scalar_dependences_outside_loop (loop, stmt)) | |
1645 | { | |
1646 | /* Due to limitation in the transform phase we have to fuse all | |
1647 | reduction partitions. As a result, this could cancel valid | |
1648 | loop distribution especially for loop that induction variable | |
1649 | is used outside of loop. To workaround this issue, we skip | |
1650 | marking partition as reudction if the reduction stmt belongs | |
1651 | to all partitions. In such case, reduction will be computed | |
1652 | correctly no matter how partitions are fused/distributed. */ | |
1653 | if (!bitmap_bit_p (stmt_in_all_partitions, i)) | |
f20132e7 | 1654 | { |
939cf90f BC |
1655 | partition->reduction_p = true; |
1656 | return; | |
f20132e7 | 1657 | } |
939cf90f | 1658 | has_reduction = true; |
f20132e7 | 1659 | } |
d0582dc1 | 1660 | } |
939cf90f BC |
1661 | |
1662 | /* Perform general partition disqualification for builtins. */ | |
1663 | if (volatiles_p | |
1664 | /* Simple workaround to prevent classifying the partition as builtin | |
1665 | if it contains any use outside of loop. */ | |
1666 | || has_reduction | |
1667 | || !flag_tree_loop_distribute_patterns) | |
1668 | return; | |
1669 | ||
1670 | /* Find single load/store data references for builtin partition. */ | |
85aa9ed6 | 1671 | if (!find_single_drs (loop, rdg, partition, &single_st, &single_ld)) |
939cf90f BC |
1672 | return; |
1673 | ||
1674 | /* Classify the builtin kind. */ | |
1675 | if (single_ld == NULL) | |
1676 | classify_builtin_st (loop, partition, single_st); | |
1677 | else | |
1678 | classify_builtin_ldst (loop, rdg, partition, single_st, single_ld); | |
cfee318d SP |
1679 | } |
1680 | ||
95f7d11b BC |
1681 | /* Returns true when PARTITION1 and PARTITION2 access the same memory |
1682 | object in RDG. */ | |
cfee318d SP |
1683 | |
1684 | static bool | |
95f7d11b BC |
1685 | share_memory_accesses (struct graph *rdg, |
1686 | partition *partition1, partition *partition2) | |
cfee318d | 1687 | { |
95f7d11b | 1688 | unsigned i, j; |
cfee318d | 1689 | bitmap_iterator bi, bj; |
95f7d11b | 1690 | data_reference_p dr1, dr2; |
1fa0c180 RG |
1691 | |
1692 | /* First check whether in the intersection of the two partitions are | |
1693 | any loads or stores. Common loads are the situation that happens | |
1694 | most often. */ | |
1695 | EXECUTE_IF_AND_IN_BITMAP (partition1->stmts, partition2->stmts, 0, i, bi) | |
1696 | if (RDG_MEM_WRITE_STMT (rdg, i) | |
1697 | || RDG_MEM_READS_STMT (rdg, i)) | |
1698 | return true; | |
cfee318d | 1699 | |
95f7d11b BC |
1700 | /* Then check whether the two partitions access the same memory object. */ |
1701 | EXECUTE_IF_SET_IN_BITMAP (partition1->datarefs, 0, i, bi) | |
1702 | { | |
1703 | dr1 = datarefs_vec[i]; | |
1704 | ||
1705 | if (!DR_BASE_ADDRESS (dr1) | |
1706 | || !DR_OFFSET (dr1) || !DR_INIT (dr1) || !DR_STEP (dr1)) | |
1707 | continue; | |
1708 | ||
1709 | EXECUTE_IF_SET_IN_BITMAP (partition2->datarefs, 0, j, bj) | |
1710 | { | |
1711 | dr2 = datarefs_vec[j]; | |
1712 | ||
1713 | if (!DR_BASE_ADDRESS (dr2) | |
1714 | || !DR_OFFSET (dr2) || !DR_INIT (dr2) || !DR_STEP (dr2)) | |
1715 | continue; | |
1716 | ||
1717 | if (operand_equal_p (DR_BASE_ADDRESS (dr1), DR_BASE_ADDRESS (dr2), 0) | |
1718 | && operand_equal_p (DR_OFFSET (dr1), DR_OFFSET (dr2), 0) | |
1719 | && operand_equal_p (DR_INIT (dr1), DR_INIT (dr2), 0) | |
1720 | && operand_equal_p (DR_STEP (dr1), DR_STEP (dr2), 0)) | |
1721 | return true; | |
1722 | } | |
1723 | } | |
cfee318d SP |
1724 | |
1725 | return false; | |
1726 | } | |
1727 | ||
4a52eb19 BC |
1728 | /* For each seed statement in STARTING_STMTS, this function builds |
1729 | partition for it by adding depended statements according to RDG. | |
1730 | All partitions are recorded in PARTITIONS. */ | |
dea61d92 SP |
1731 | |
1732 | static void | |
83a95546 | 1733 | rdg_build_partitions (struct graph *rdg, |
355fe088 | 1734 | vec<gimple *> starting_stmts, |
526ceb68 | 1735 | vec<partition *> *partitions) |
dea61d92 | 1736 | { |
0e3de1d4 | 1737 | auto_bitmap processed; |
2fd5894f | 1738 | int i; |
355fe088 | 1739 | gimple *stmt; |
dea61d92 | 1740 | |
2fd5894f | 1741 | FOR_EACH_VEC_ELT (starting_stmts, i, stmt) |
dea61d92 | 1742 | { |
2fd5894f RB |
1743 | int v = rdg_vertex_for_stmt (rdg, stmt); |
1744 | ||
1745 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1746 | fprintf (dump_file, | |
1747 | "ldist asked to generate code for vertex %d\n", v); | |
b8698a0f | 1748 | |
24f161fd RB |
1749 | /* If the vertex is already contained in another partition so |
1750 | is the partition rooted at it. */ | |
dea61d92 SP |
1751 | if (bitmap_bit_p (processed, v)) |
1752 | continue; | |
b8698a0f | 1753 | |
526ceb68 | 1754 | partition *partition = build_rdg_partition_for_vertex (rdg, v); |
24f161fd | 1755 | bitmap_ior_into (processed, partition->stmts); |
dea61d92 | 1756 | |
826a536d | 1757 | if (dump_file && (dump_flags & TDF_DETAILS)) |
dea61d92 | 1758 | { |
f1eb4621 BC |
1759 | fprintf (dump_file, "ldist creates useful %s partition:\n", |
1760 | partition->type == PTYPE_PARALLEL ? "parallel" : "sequent"); | |
1761 | bitmap_print (dump_file, partition->stmts, " ", "\n"); | |
dea61d92 | 1762 | } |
826a536d RB |
1763 | |
1764 | partitions->safe_push (partition); | |
dea61d92 SP |
1765 | } |
1766 | ||
83a95546 RB |
1767 | /* All vertices should have been assigned to at least one partition now, |
1768 | other than vertices belonging to dead code. */ | |
dea61d92 SP |
1769 | } |
1770 | ||
1771 | /* Dump to FILE the PARTITIONS. */ | |
1772 | ||
1773 | static void | |
526ceb68 | 1774 | dump_rdg_partitions (FILE *file, vec<partition *> partitions) |
dea61d92 SP |
1775 | { |
1776 | int i; | |
526ceb68 | 1777 | partition *partition; |
dea61d92 | 1778 | |
9771b263 | 1779 | FOR_EACH_VEC_ELT (partitions, i, partition) |
c61f8985 | 1780 | debug_bitmap_file (file, partition->stmts); |
dea61d92 SP |
1781 | } |
1782 | ||
1783 | /* Debug PARTITIONS. */ | |
526ceb68 | 1784 | extern void debug_rdg_partitions (vec<partition *> ); |
dea61d92 | 1785 | |
24e47c76 | 1786 | DEBUG_FUNCTION void |
526ceb68 | 1787 | debug_rdg_partitions (vec<partition *> partitions) |
dea61d92 SP |
1788 | { |
1789 | dump_rdg_partitions (stderr, partitions); | |
1790 | } | |
1791 | ||
2b8aee8e SP |
1792 | /* Returns the number of read and write operations in the RDG. */ |
1793 | ||
1794 | static int | |
1795 | number_of_rw_in_rdg (struct graph *rdg) | |
1796 | { | |
1797 | int i, res = 0; | |
1798 | ||
1799 | for (i = 0; i < rdg->n_vertices; i++) | |
1800 | { | |
1801 | if (RDG_MEM_WRITE_STMT (rdg, i)) | |
1802 | ++res; | |
1803 | ||
1804 | if (RDG_MEM_READS_STMT (rdg, i)) | |
1805 | ++res; | |
1806 | } | |
1807 | ||
1808 | return res; | |
1809 | } | |
1810 | ||
1811 | /* Returns the number of read and write operations in a PARTITION of | |
1812 | the RDG. */ | |
1813 | ||
1814 | static int | |
526ceb68 | 1815 | number_of_rw_in_partition (struct graph *rdg, partition *partition) |
2b8aee8e SP |
1816 | { |
1817 | int res = 0; | |
1818 | unsigned i; | |
1819 | bitmap_iterator ii; | |
1820 | ||
c61f8985 | 1821 | EXECUTE_IF_SET_IN_BITMAP (partition->stmts, 0, i, ii) |
2b8aee8e SP |
1822 | { |
1823 | if (RDG_MEM_WRITE_STMT (rdg, i)) | |
1824 | ++res; | |
1825 | ||
1826 | if (RDG_MEM_READS_STMT (rdg, i)) | |
1827 | ++res; | |
1828 | } | |
1829 | ||
1830 | return res; | |
1831 | } | |
1832 | ||
1833 | /* Returns true when one of the PARTITIONS contains all the read or | |
1834 | write operations of RDG. */ | |
1835 | ||
1836 | static bool | |
9771b263 | 1837 | partition_contains_all_rw (struct graph *rdg, |
526ceb68 | 1838 | vec<partition *> partitions) |
2b8aee8e SP |
1839 | { |
1840 | int i; | |
526ceb68 | 1841 | partition *partition; |
2b8aee8e SP |
1842 | int nrw = number_of_rw_in_rdg (rdg); |
1843 | ||
9771b263 | 1844 | FOR_EACH_VEC_ELT (partitions, i, partition) |
2b8aee8e SP |
1845 | if (nrw == number_of_rw_in_partition (rdg, partition)) |
1846 | return true; | |
1847 | ||
1848 | return false; | |
1849 | } | |
1850 | ||
447f3223 | 1851 | /* Compute partition dependence created by the data references in DRS1 |
a8745cc2 BC |
1852 | and DRS2, modify and return DIR according to that. IF ALIAS_DDR is |
1853 | not NULL, we record dependence introduced by possible alias between | |
1854 | two data references in ALIAS_DDRS; otherwise, we simply ignore such | |
1855 | dependence as if it doesn't exist at all. */ | |
447f3223 RB |
1856 | |
1857 | static int | |
4084ea5f | 1858 | pg_add_dependence_edges (struct graph *rdg, int dir, |
a8745cc2 | 1859 | bitmap drs1, bitmap drs2, vec<ddr_p> *alias_ddrs) |
447f3223 | 1860 | { |
a7a44c07 BC |
1861 | unsigned i, j; |
1862 | bitmap_iterator bi, bj; | |
1863 | data_reference_p dr1, dr2, saved_dr1; | |
447f3223 RB |
1864 | |
1865 | /* dependence direction - 0 is no dependence, -1 is back, | |
1866 | 1 is forth, 2 is both (we can stop then, merging will occur). */ | |
a7a44c07 BC |
1867 | EXECUTE_IF_SET_IN_BITMAP (drs1, 0, i, bi) |
1868 | { | |
1869 | dr1 = datarefs_vec[i]; | |
1870 | ||
1871 | EXECUTE_IF_SET_IN_BITMAP (drs2, 0, j, bj) | |
1872 | { | |
a8745cc2 BC |
1873 | int res, this_dir = 1; |
1874 | ddr_p ddr; | |
1875 | ||
a7a44c07 BC |
1876 | dr2 = datarefs_vec[j]; |
1877 | ||
1878 | /* Skip all <read, read> data dependence. */ | |
1879 | if (DR_IS_READ (dr1) && DR_IS_READ (dr2)) | |
1880 | continue; | |
1881 | ||
1882 | saved_dr1 = dr1; | |
a8745cc2 | 1883 | /* Re-shuffle data-refs to be in topological order. */ |
a7a44c07 BC |
1884 | if (rdg_vertex_for_stmt (rdg, DR_STMT (dr1)) |
1885 | > rdg_vertex_for_stmt (rdg, DR_STMT (dr2))) | |
1886 | { | |
1887 | std::swap (dr1, dr2); | |
1888 | this_dir = -this_dir; | |
1889 | } | |
17c5cbdf | 1890 | ddr = get_data_dependence (rdg, dr1, dr2); |
a7a44c07 | 1891 | if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know) |
a8745cc2 BC |
1892 | { |
1893 | this_dir = 0; | |
1894 | res = data_ref_compare_tree (DR_BASE_ADDRESS (dr1), | |
1895 | DR_BASE_ADDRESS (dr2)); | |
1896 | /* Be conservative. If data references are not well analyzed, | |
1897 | or the two data references have the same base address and | |
1898 | offset, add dependence and consider it alias to each other. | |
1899 | In other words, the dependence can not be resolved by | |
1900 | runtime alias check. */ | |
1901 | if (!DR_BASE_ADDRESS (dr1) || !DR_BASE_ADDRESS (dr2) | |
1902 | || !DR_OFFSET (dr1) || !DR_OFFSET (dr2) | |
1903 | || !DR_INIT (dr1) || !DR_INIT (dr2) | |
1904 | || !DR_STEP (dr1) || !tree_fits_uhwi_p (DR_STEP (dr1)) | |
1905 | || !DR_STEP (dr2) || !tree_fits_uhwi_p (DR_STEP (dr2)) | |
1906 | || res == 0) | |
1907 | this_dir = 2; | |
1908 | /* Data dependence could be resolved by runtime alias check, | |
1909 | record it in ALIAS_DDRS. */ | |
1910 | else if (alias_ddrs != NULL) | |
1911 | alias_ddrs->safe_push (ddr); | |
1912 | /* Or simply ignore it. */ | |
1913 | } | |
a7a44c07 BC |
1914 | else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE) |
1915 | { | |
1916 | if (DDR_REVERSED_P (ddr)) | |
a8745cc2 BC |
1917 | this_dir = -this_dir; |
1918 | ||
a7a44c07 BC |
1919 | /* Known dependences can still be unordered througout the |
1920 | iteration space, see gcc.dg/tree-ssa/ldist-16.c. */ | |
1921 | if (DDR_NUM_DIST_VECTS (ddr) != 1) | |
1922 | this_dir = 2; | |
1923 | /* If the overlap is exact preserve stmt order. */ | |
0df7c778 BC |
1924 | else if (lambda_vector_zerop (DDR_DIST_VECT (ddr, 0), |
1925 | DDR_NB_LOOPS (ddr))) | |
a7a44c07 | 1926 | ; |
a8745cc2 BC |
1927 | /* Else as the distance vector is lexicographic positive swap |
1928 | the dependence direction. */ | |
a7a44c07 | 1929 | else |
a8745cc2 | 1930 | this_dir = -this_dir; |
a7a44c07 BC |
1931 | } |
1932 | else | |
1933 | this_dir = 0; | |
a7a44c07 BC |
1934 | if (this_dir == 2) |
1935 | return 2; | |
1936 | else if (dir == 0) | |
1937 | dir = this_dir; | |
1938 | else if (this_dir != 0 && dir != this_dir) | |
1939 | return 2; | |
1940 | /* Shuffle "back" dr1. */ | |
1941 | dr1 = saved_dr1; | |
1942 | } | |
1943 | } | |
447f3223 RB |
1944 | return dir; |
1945 | } | |
1946 | ||
1947 | /* Compare postorder number of the partition graph vertices V1 and V2. */ | |
1948 | ||
1949 | static int | |
1950 | pgcmp (const void *v1_, const void *v2_) | |
1951 | { | |
1952 | const vertex *v1 = (const vertex *)v1_; | |
1953 | const vertex *v2 = (const vertex *)v2_; | |
1954 | return v2->post - v1->post; | |
1955 | } | |
2fd5894f | 1956 | |
a8745cc2 BC |
1957 | /* Data attached to vertices of partition dependence graph. */ |
1958 | struct pg_vdata | |
1959 | { | |
1960 | /* ID of the corresponding partition. */ | |
1961 | int id; | |
1962 | /* The partition. */ | |
1963 | struct partition *partition; | |
1964 | }; | |
1965 | ||
1966 | /* Data attached to edges of partition dependence graph. */ | |
1967 | struct pg_edata | |
1968 | { | |
1969 | /* If the dependence edge can be resolved by runtime alias check, | |
1970 | this vector contains data dependence relations for runtime alias | |
1971 | check. On the other hand, if the dependence edge is introduced | |
1972 | because of compilation time known data dependence, this vector | |
1973 | contains nothing. */ | |
1974 | vec<ddr_p> alias_ddrs; | |
1975 | }; | |
1976 | ||
1977 | /* Callback data for traversing edges in graph. */ | |
1978 | struct pg_edge_callback_data | |
1979 | { | |
1980 | /* Bitmap contains strong connected components should be merged. */ | |
1981 | bitmap sccs_to_merge; | |
1982 | /* Array constains component information for all vertices. */ | |
1983 | int *vertices_component; | |
1984 | /* Vector to record all data dependence relations which are needed | |
1985 | to break strong connected components by runtime alias checks. */ | |
1986 | vec<ddr_p> *alias_ddrs; | |
1987 | }; | |
1988 | ||
1989 | /* Initialize vertice's data for partition dependence graph PG with | |
1990 | PARTITIONS. */ | |
1991 | ||
1992 | static void | |
1993 | init_partition_graph_vertices (struct graph *pg, | |
1994 | vec<struct partition *> *partitions) | |
1995 | { | |
1996 | int i; | |
1997 | partition *partition; | |
1998 | struct pg_vdata *data; | |
1999 | ||
2000 | for (i = 0; partitions->iterate (i, &partition); ++i) | |
2001 | { | |
2002 | data = new pg_vdata; | |
2003 | pg->vertices[i].data = data; | |
2004 | data->id = i; | |
2005 | data->partition = partition; | |
2006 | } | |
2007 | } | |
2008 | ||
2009 | /* Add edge <I, J> to partition dependence graph PG. Attach vector of data | |
2010 | dependence relations to the EDGE if DDRS isn't NULL. */ | |
2011 | ||
2012 | static void | |
2013 | add_partition_graph_edge (struct graph *pg, int i, int j, vec<ddr_p> *ddrs) | |
2014 | { | |
2015 | struct graph_edge *e = add_edge (pg, i, j); | |
2016 | ||
2017 | /* If the edge is attached with data dependence relations, it means this | |
2018 | dependence edge can be resolved by runtime alias checks. */ | |
2019 | if (ddrs != NULL) | |
2020 | { | |
2021 | struct pg_edata *data = new pg_edata; | |
2022 | ||
2023 | gcc_assert (ddrs->length () > 0); | |
2024 | e->data = data; | |
2025 | data->alias_ddrs = vNULL; | |
2026 | data->alias_ddrs.safe_splice (*ddrs); | |
2027 | } | |
2028 | } | |
2029 | ||
2030 | /* Callback function for graph travesal algorithm. It returns true | |
2031 | if edge E should skipped when traversing the graph. */ | |
2032 | ||
2033 | static bool | |
2034 | pg_skip_alias_edge (struct graph_edge *e) | |
2035 | { | |
2036 | struct pg_edata *data = (struct pg_edata *)e->data; | |
2037 | return (data != NULL && data->alias_ddrs.length () > 0); | |
2038 | } | |
2039 | ||
2040 | /* Callback function freeing data attached to edge E of graph. */ | |
2041 | ||
2042 | static void | |
2043 | free_partition_graph_edata_cb (struct graph *, struct graph_edge *e, void *) | |
2044 | { | |
2045 | if (e->data != NULL) | |
2046 | { | |
2047 | struct pg_edata *data = (struct pg_edata *)e->data; | |
2048 | data->alias_ddrs.release (); | |
2049 | delete data; | |
2050 | } | |
2051 | } | |
2052 | ||
2053 | /* Free data attached to vertice of partition dependence graph PG. */ | |
2054 | ||
2055 | static void | |
2056 | free_partition_graph_vdata (struct graph *pg) | |
2057 | { | |
2058 | int i; | |
2059 | struct pg_vdata *data; | |
2060 | ||
2061 | for (i = 0; i < pg->n_vertices; ++i) | |
2062 | { | |
2063 | data = (struct pg_vdata *)pg->vertices[i].data; | |
2064 | delete data; | |
2065 | } | |
2066 | } | |
2067 | ||
2068 | /* Build and return partition dependence graph for PARTITIONS. RDG is | |
2069 | reduced dependence graph for the loop to be distributed. If IGNORE_ALIAS_P | |
2070 | is true, data dependence caused by possible alias between references | |
2071 | is ignored, as if it doesn't exist at all; otherwise all depdendences | |
2072 | are considered. */ | |
2073 | ||
2074 | static struct graph * | |
2075 | build_partition_graph (struct graph *rdg, | |
2076 | vec<struct partition *> *partitions, | |
2077 | bool ignore_alias_p) | |
2078 | { | |
2079 | int i, j; | |
2080 | struct partition *partition1, *partition2; | |
2081 | graph *pg = new_graph (partitions->length ()); | |
2082 | auto_vec<ddr_p> alias_ddrs, *alias_ddrs_p; | |
2083 | ||
2084 | alias_ddrs_p = ignore_alias_p ? NULL : &alias_ddrs; | |
2085 | ||
2086 | init_partition_graph_vertices (pg, partitions); | |
2087 | ||
2088 | for (i = 0; partitions->iterate (i, &partition1); ++i) | |
2089 | { | |
2090 | for (j = i + 1; partitions->iterate (j, &partition2); ++j) | |
2091 | { | |
2092 | /* dependence direction - 0 is no dependence, -1 is back, | |
2093 | 1 is forth, 2 is both (we can stop then, merging will occur). */ | |
2094 | int dir = 0; | |
2095 | ||
2096 | /* If the first partition has reduction, add back edge; if the | |
2097 | second partition has reduction, add forth edge. This makes | |
2098 | sure that reduction partition will be sorted as the last one. */ | |
2099 | if (partition_reduction_p (partition1)) | |
2100 | dir = -1; | |
2101 | else if (partition_reduction_p (partition2)) | |
2102 | dir = 1; | |
2103 | ||
2104 | /* Cleanup the temporary vector. */ | |
2105 | alias_ddrs.truncate (0); | |
2106 | ||
2107 | dir = pg_add_dependence_edges (rdg, dir, partition1->datarefs, | |
2108 | partition2->datarefs, alias_ddrs_p); | |
2109 | ||
2110 | /* Add edge to partition graph if there exists dependence. There | |
2111 | are two types of edges. One type edge is caused by compilation | |
2112 | time known dependence, this type can not be resolved by runtime | |
2113 | alias check. The other type can be resolved by runtime alias | |
2114 | check. */ | |
2115 | if (dir == 1 || dir == 2 | |
2116 | || alias_ddrs.length () > 0) | |
2117 | { | |
2118 | /* Attach data dependence relations to edge that can be resolved | |
2119 | by runtime alias check. */ | |
2120 | bool alias_edge_p = (dir != 1 && dir != 2); | |
2121 | add_partition_graph_edge (pg, i, j, | |
2122 | (alias_edge_p) ? &alias_ddrs : NULL); | |
2123 | } | |
2124 | if (dir == -1 || dir == 2 | |
2125 | || alias_ddrs.length () > 0) | |
2126 | { | |
2127 | /* Attach data dependence relations to edge that can be resolved | |
2128 | by runtime alias check. */ | |
2129 | bool alias_edge_p = (dir != -1 && dir != 2); | |
2130 | add_partition_graph_edge (pg, j, i, | |
2131 | (alias_edge_p) ? &alias_ddrs : NULL); | |
2132 | } | |
2133 | } | |
2134 | } | |
2135 | return pg; | |
2136 | } | |
2137 | ||
b4ec1d31 BC |
2138 | /* Sort partitions in PG in descending post order and store them in |
2139 | PARTITIONS. */ | |
a8745cc2 BC |
2140 | |
2141 | static void | |
2142 | sort_partitions_by_post_order (struct graph *pg, | |
2143 | vec<struct partition *> *partitions) | |
2144 | { | |
2145 | int i; | |
2146 | struct pg_vdata *data; | |
2147 | ||
b4ec1d31 | 2148 | /* Now order the remaining nodes in descending postorder. */ |
a8745cc2 BC |
2149 | qsort (pg->vertices, pg->n_vertices, sizeof (vertex), pgcmp); |
2150 | partitions->truncate (0); | |
2151 | for (i = 0; i < pg->n_vertices; ++i) | |
2152 | { | |
2153 | data = (struct pg_vdata *)pg->vertices[i].data; | |
2154 | if (data->partition) | |
2155 | partitions->safe_push (data->partition); | |
2156 | } | |
2157 | } | |
2158 | ||
2159 | /* Given reduced dependence graph RDG merge strong connected components | |
163aa51b BC |
2160 | of PARTITIONS. If IGNORE_ALIAS_P is true, data dependence caused by |
2161 | possible alias between references is ignored, as if it doesn't exist | |
2162 | at all; otherwise all depdendences are considered. */ | |
a8745cc2 BC |
2163 | |
2164 | static void | |
2165 | merge_dep_scc_partitions (struct graph *rdg, | |
163aa51b BC |
2166 | vec<struct partition *> *partitions, |
2167 | bool ignore_alias_p) | |
a8745cc2 BC |
2168 | { |
2169 | struct partition *partition1, *partition2; | |
2170 | struct pg_vdata *data; | |
163aa51b | 2171 | graph *pg = build_partition_graph (rdg, partitions, ignore_alias_p); |
a8745cc2 BC |
2172 | int i, j, num_sccs = graphds_scc (pg, NULL); |
2173 | ||
2174 | /* Strong connected compoenent means dependence cycle, we cannot distribute | |
2175 | them. So fuse them together. */ | |
2176 | if ((unsigned) num_sccs < partitions->length ()) | |
2177 | { | |
2178 | for (i = 0; i < num_sccs; ++i) | |
2179 | { | |
2180 | for (j = 0; partitions->iterate (j, &partition1); ++j) | |
2181 | if (pg->vertices[j].component == i) | |
2182 | break; | |
2183 | for (j = j + 1; partitions->iterate (j, &partition2); ++j) | |
2184 | if (pg->vertices[j].component == i) | |
2185 | { | |
2186 | partition_merge_into (NULL, partition1, | |
2187 | partition2, FUSE_SAME_SCC); | |
2188 | partition1->type = PTYPE_SEQUENTIAL; | |
2189 | (*partitions)[j] = NULL; | |
2190 | partition_free (partition2); | |
2191 | data = (struct pg_vdata *)pg->vertices[j].data; | |
2192 | data->partition = NULL; | |
2193 | } | |
2194 | } | |
a8745cc2 | 2195 | } |
aa1528b5 BC |
2196 | |
2197 | sort_partitions_by_post_order (pg, partitions); | |
a8745cc2 BC |
2198 | gcc_assert (partitions->length () == (unsigned)num_sccs); |
2199 | free_partition_graph_vdata (pg); | |
2200 | free_graph (pg); | |
2201 | } | |
2202 | ||
2203 | /* Callback function for traversing edge E in graph G. DATA is private | |
2204 | callback data. */ | |
2205 | ||
2206 | static void | |
2207 | pg_collect_alias_ddrs (struct graph *g, struct graph_edge *e, void *data) | |
2208 | { | |
2209 | int i, j, component; | |
2210 | struct pg_edge_callback_data *cbdata; | |
2211 | struct pg_edata *edata = (struct pg_edata *) e->data; | |
2212 | ||
2213 | /* If the edge doesn't have attached data dependence, it represents | |
2214 | compilation time known dependences. This type dependence cannot | |
2215 | be resolved by runtime alias check. */ | |
2216 | if (edata == NULL || edata->alias_ddrs.length () == 0) | |
2217 | return; | |
2218 | ||
2219 | cbdata = (struct pg_edge_callback_data *) data; | |
2220 | i = e->src; | |
2221 | j = e->dest; | |
2222 | component = cbdata->vertices_component[i]; | |
2223 | /* Vertices are topologically sorted according to compilation time | |
2224 | known dependences, so we can break strong connected components | |
2225 | by removing edges of the opposite direction, i.e, edges pointing | |
2226 | from vertice with smaller post number to vertice with bigger post | |
2227 | number. */ | |
2228 | if (g->vertices[i].post < g->vertices[j].post | |
2229 | /* We only need to remove edges connecting vertices in the same | |
2230 | strong connected component to break it. */ | |
2231 | && component == cbdata->vertices_component[j] | |
2232 | /* Check if we want to break the strong connected component or not. */ | |
2233 | && !bitmap_bit_p (cbdata->sccs_to_merge, component)) | |
2234 | cbdata->alias_ddrs->safe_splice (edata->alias_ddrs); | |
2235 | } | |
2236 | ||
2237 | /* This is the main function breaking strong conected components in | |
2238 | PARTITIONS giving reduced depdendence graph RDG. Store data dependence | |
2239 | relations for runtime alias check in ALIAS_DDRS. */ | |
2240 | ||
2241 | static void | |
2242 | break_alias_scc_partitions (struct graph *rdg, | |
2243 | vec<struct partition *> *partitions, | |
2244 | vec<ddr_p> *alias_ddrs) | |
2245 | { | |
b4ec1d31 | 2246 | int i, j, k, num_sccs, num_sccs_no_alias; |
a8745cc2 BC |
2247 | /* Build partition dependence graph. */ |
2248 | graph *pg = build_partition_graph (rdg, partitions, false); | |
2249 | ||
2250 | alias_ddrs->truncate (0); | |
2251 | /* Find strong connected components in the graph, with all dependence edges | |
2252 | considered. */ | |
2253 | num_sccs = graphds_scc (pg, NULL); | |
2254 | /* All SCCs now can be broken by runtime alias checks because SCCs caused by | |
2255 | compilation time known dependences are merged before this function. */ | |
2256 | if ((unsigned) num_sccs < partitions->length ()) | |
2257 | { | |
2258 | struct pg_edge_callback_data cbdata; | |
2259 | auto_bitmap sccs_to_merge; | |
2260 | auto_vec<enum partition_type> scc_types; | |
2261 | struct partition *partition, *first; | |
2262 | ||
6dc29d3a | 2263 | /* If all partitions in a SCC have the same type, we can simply merge the |
a8745cc2 BC |
2264 | SCC. This loop finds out such SCCS and record them in bitmap. */ |
2265 | bitmap_set_range (sccs_to_merge, 0, (unsigned) num_sccs); | |
2266 | for (i = 0; i < num_sccs; ++i) | |
2267 | { | |
2268 | for (j = 0; partitions->iterate (j, &first); ++j) | |
2269 | if (pg->vertices[j].component == i) | |
2270 | break; | |
1623d9f3 BC |
2271 | |
2272 | bool same_type = true, all_builtins = partition_builtin_p (first); | |
a8745cc2 BC |
2273 | for (++j; partitions->iterate (j, &partition); ++j) |
2274 | { | |
2275 | if (pg->vertices[j].component != i) | |
2276 | continue; | |
2277 | ||
2278 | if (first->type != partition->type) | |
2279 | { | |
1623d9f3 | 2280 | same_type = false; |
a8745cc2 BC |
2281 | break; |
2282 | } | |
1623d9f3 | 2283 | all_builtins &= partition_builtin_p (partition); |
a8745cc2 | 2284 | } |
1623d9f3 BC |
2285 | /* Merge SCC if all partitions in SCC have the same type, though the |
2286 | result partition is sequential, because vectorizer can do better | |
2287 | runtime alias check. One expecption is all partitions in SCC are | |
2288 | builtins. */ | |
2289 | if (!same_type || all_builtins) | |
2290 | bitmap_clear_bit (sccs_to_merge, i); | |
a8745cc2 BC |
2291 | } |
2292 | ||
2293 | /* Initialize callback data for traversing. */ | |
2294 | cbdata.sccs_to_merge = sccs_to_merge; | |
2295 | cbdata.alias_ddrs = alias_ddrs; | |
2296 | cbdata.vertices_component = XNEWVEC (int, pg->n_vertices); | |
2297 | /* Record the component information which will be corrupted by next | |
2298 | graph scc finding call. */ | |
2299 | for (i = 0; i < pg->n_vertices; ++i) | |
2300 | cbdata.vertices_component[i] = pg->vertices[i].component; | |
2301 | ||
2302 | /* Collect data dependences for runtime alias checks to break SCCs. */ | |
2303 | if (bitmap_count_bits (sccs_to_merge) != (unsigned) num_sccs) | |
2304 | { | |
2305 | /* Run SCC finding algorithm again, with alias dependence edges | |
6dc29d3a | 2306 | skipped. This is to topologically sort partitions according to |
a8745cc2 BC |
2307 | compilation time known dependence. Note the topological order |
2308 | is stored in the form of pg's post order number. */ | |
2309 | num_sccs_no_alias = graphds_scc (pg, NULL, pg_skip_alias_edge); | |
2310 | gcc_assert (partitions->length () == (unsigned) num_sccs_no_alias); | |
2311 | /* With topological order, we can construct two subgraphs L and R. | |
2312 | L contains edge <x, y> where x < y in terms of post order, while | |
2313 | R contains edge <x, y> where x > y. Edges for compilation time | |
2314 | known dependence all fall in R, so we break SCCs by removing all | |
2315 | (alias) edges of in subgraph L. */ | |
2316 | for_each_edge (pg, pg_collect_alias_ddrs, &cbdata); | |
2317 | } | |
2318 | ||
2319 | /* For SCC that doesn't need to be broken, merge it. */ | |
2320 | for (i = 0; i < num_sccs; ++i) | |
2321 | { | |
2322 | if (!bitmap_bit_p (sccs_to_merge, i)) | |
2323 | continue; | |
2324 | ||
2325 | for (j = 0; partitions->iterate (j, &first); ++j) | |
2326 | if (cbdata.vertices_component[j] == i) | |
2327 | break; | |
b4ec1d31 | 2328 | for (k = j + 1; partitions->iterate (k, &partition); ++k) |
a8745cc2 BC |
2329 | { |
2330 | struct pg_vdata *data; | |
2331 | ||
b4ec1d31 | 2332 | if (cbdata.vertices_component[k] != i) |
a8745cc2 BC |
2333 | continue; |
2334 | ||
b4ec1d31 BC |
2335 | /* Update postorder number so that merged reduction partition is |
2336 | sorted after other partitions. */ | |
2337 | if (!partition_reduction_p (first) | |
2338 | && partition_reduction_p (partition)) | |
2339 | { | |
2340 | gcc_assert (pg->vertices[k].post < pg->vertices[j].post); | |
2341 | pg->vertices[j].post = pg->vertices[k].post; | |
2342 | } | |
a8745cc2 | 2343 | partition_merge_into (NULL, first, partition, FUSE_SAME_SCC); |
b4ec1d31 | 2344 | (*partitions)[k] = NULL; |
a8745cc2 | 2345 | partition_free (partition); |
b4ec1d31 BC |
2346 | data = (struct pg_vdata *)pg->vertices[k].data; |
2347 | gcc_assert (data->id == k); | |
a8745cc2 | 2348 | data->partition = NULL; |
6dc29d3a BC |
2349 | /* The result partition of merged SCC must be sequential. */ |
2350 | first->type = PTYPE_SEQUENTIAL; | |
a8745cc2 BC |
2351 | } |
2352 | } | |
2353 | } | |
2354 | ||
2355 | sort_partitions_by_post_order (pg, partitions); | |
2356 | free_partition_graph_vdata (pg); | |
2357 | for_each_edge (pg, free_partition_graph_edata_cb, NULL); | |
2358 | free_graph (pg); | |
2359 | ||
2360 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2361 | { | |
2362 | fprintf (dump_file, "Possible alias data dependence to break:\n"); | |
2363 | dump_data_dependence_relations (dump_file, *alias_ddrs); | |
2364 | } | |
2365 | } | |
2366 | ||
2367 | /* Compute and return an expression whose value is the segment length which | |
2368 | will be accessed by DR in NITERS iterations. */ | |
2369 | ||
2370 | static tree | |
2371 | data_ref_segment_size (struct data_reference *dr, tree niters) | |
2372 | { | |
a57776a1 RS |
2373 | niters = size_binop (MINUS_EXPR, |
2374 | fold_convert (sizetype, niters), | |
2375 | size_one_node); | |
2376 | return size_binop (MULT_EXPR, | |
2377 | fold_convert (sizetype, DR_STEP (dr)), | |
2378 | fold_convert (sizetype, niters)); | |
a8745cc2 BC |
2379 | } |
2380 | ||
2381 | /* Return true if LOOP's latch is dominated by statement for data reference | |
2382 | DR. */ | |
2383 | ||
2384 | static inline bool | |
2385 | latch_dominated_by_data_ref (struct loop *loop, data_reference *dr) | |
2386 | { | |
2387 | return dominated_by_p (CDI_DOMINATORS, single_exit (loop)->src, | |
2388 | gimple_bb (DR_STMT (dr))); | |
2389 | } | |
2390 | ||
2391 | /* Compute alias check pairs and store them in COMP_ALIAS_PAIRS for LOOP's | |
2392 | data dependence relations ALIAS_DDRS. */ | |
2393 | ||
2394 | static void | |
2395 | compute_alias_check_pairs (struct loop *loop, vec<ddr_p> *alias_ddrs, | |
2396 | vec<dr_with_seg_len_pair_t> *comp_alias_pairs) | |
2397 | { | |
2398 | unsigned int i; | |
2399 | unsigned HOST_WIDE_INT factor = 1; | |
2400 | tree niters_plus_one, niters = number_of_latch_executions (loop); | |
2401 | ||
2402 | gcc_assert (niters != NULL_TREE && niters != chrec_dont_know); | |
2403 | niters = fold_convert (sizetype, niters); | |
2404 | niters_plus_one = size_binop (PLUS_EXPR, niters, size_one_node); | |
2405 | ||
2406 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2407 | fprintf (dump_file, "Creating alias check pairs:\n"); | |
2408 | ||
2409 | /* Iterate all data dependence relations and compute alias check pairs. */ | |
2410 | for (i = 0; i < alias_ddrs->length (); i++) | |
2411 | { | |
2412 | ddr_p ddr = (*alias_ddrs)[i]; | |
2413 | struct data_reference *dr_a = DDR_A (ddr); | |
2414 | struct data_reference *dr_b = DDR_B (ddr); | |
2415 | tree seg_length_a, seg_length_b; | |
2416 | int comp_res = data_ref_compare_tree (DR_BASE_ADDRESS (dr_a), | |
2417 | DR_BASE_ADDRESS (dr_b)); | |
2418 | ||
2419 | if (comp_res == 0) | |
2420 | comp_res = data_ref_compare_tree (DR_OFFSET (dr_a), DR_OFFSET (dr_b)); | |
2421 | gcc_assert (comp_res != 0); | |
2422 | ||
2423 | if (latch_dominated_by_data_ref (loop, dr_a)) | |
2424 | seg_length_a = data_ref_segment_size (dr_a, niters_plus_one); | |
2425 | else | |
2426 | seg_length_a = data_ref_segment_size (dr_a, niters); | |
2427 | ||
2428 | if (latch_dominated_by_data_ref (loop, dr_b)) | |
2429 | seg_length_b = data_ref_segment_size (dr_b, niters_plus_one); | |
2430 | else | |
2431 | seg_length_b = data_ref_segment_size (dr_b, niters); | |
2432 | ||
a57776a1 RS |
2433 | unsigned HOST_WIDE_INT access_size_a |
2434 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr_a)))); | |
2435 | unsigned HOST_WIDE_INT access_size_b | |
2436 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr_b)))); | |
2437 | unsigned int align_a = TYPE_ALIGN_UNIT (TREE_TYPE (DR_REF (dr_a))); | |
2438 | unsigned int align_b = TYPE_ALIGN_UNIT (TREE_TYPE (DR_REF (dr_b))); | |
2439 | ||
a8745cc2 | 2440 | dr_with_seg_len_pair_t dr_with_seg_len_pair |
a57776a1 RS |
2441 | (dr_with_seg_len (dr_a, seg_length_a, access_size_a, align_a), |
2442 | dr_with_seg_len (dr_b, seg_length_b, access_size_b, align_b)); | |
a8745cc2 BC |
2443 | |
2444 | /* Canonicalize pairs by sorting the two DR members. */ | |
2445 | if (comp_res > 0) | |
2446 | std::swap (dr_with_seg_len_pair.first, dr_with_seg_len_pair.second); | |
2447 | ||
2448 | comp_alias_pairs->safe_push (dr_with_seg_len_pair); | |
2449 | } | |
2450 | ||
2451 | if (tree_fits_uhwi_p (niters)) | |
2452 | factor = tree_to_uhwi (niters); | |
2453 | ||
2454 | /* Prune alias check pairs. */ | |
2455 | prune_runtime_alias_test_list (comp_alias_pairs, factor); | |
2456 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2457 | fprintf (dump_file, | |
2458 | "Improved number of alias checks from %d to %d\n", | |
2459 | alias_ddrs->length (), comp_alias_pairs->length ()); | |
2460 | } | |
2461 | ||
2462 | /* Given data dependence relations in ALIAS_DDRS, generate runtime alias | |
2463 | checks and version LOOP under condition of these runtime alias checks. */ | |
2464 | ||
2465 | static void | |
1623d9f3 BC |
2466 | version_loop_by_alias_check (vec<struct partition *> *partitions, |
2467 | struct loop *loop, vec<ddr_p> *alias_ddrs) | |
a8745cc2 BC |
2468 | { |
2469 | profile_probability prob; | |
2470 | basic_block cond_bb; | |
2471 | struct loop *nloop; | |
2472 | tree lhs, arg0, cond_expr = NULL_TREE; | |
2473 | gimple_seq cond_stmts = NULL; | |
2474 | gimple *call_stmt = NULL; | |
2475 | auto_vec<dr_with_seg_len_pair_t> comp_alias_pairs; | |
2476 | ||
2477 | /* Generate code for runtime alias checks if necessary. */ | |
2478 | gcc_assert (alias_ddrs->length () > 0); | |
2479 | ||
2480 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2481 | fprintf (dump_file, | |
2482 | "Version loop <%d> with runtime alias check\n", loop->num); | |
2483 | ||
2484 | compute_alias_check_pairs (loop, alias_ddrs, &comp_alias_pairs); | |
2485 | create_runtime_alias_checks (loop, &comp_alias_pairs, &cond_expr); | |
2486 | cond_expr = force_gimple_operand_1 (cond_expr, &cond_stmts, | |
8d2d0de9 | 2487 | is_gimple_val, NULL_TREE); |
a8745cc2 BC |
2488 | |
2489 | /* Depend on vectorizer to fold IFN_LOOP_DIST_ALIAS. */ | |
1623d9f3 BC |
2490 | bool cancelable_p = flag_tree_loop_vectorize; |
2491 | if (cancelable_p) | |
2492 | { | |
2493 | unsigned i = 0; | |
2494 | struct partition *partition; | |
2495 | for (; partitions->iterate (i, &partition); ++i) | |
2496 | if (!partition_builtin_p (partition)) | |
2497 | break; | |
2498 | ||
2499 | /* If all partitions are builtins, distributing it would be profitable and | |
2500 | we don't want to cancel the runtime alias checks. */ | |
2501 | if (i == partitions->length ()) | |
2502 | cancelable_p = false; | |
2503 | } | |
2504 | ||
2505 | /* Generate internal function call for loop distribution alias check if the | |
2506 | runtime alias check should be cancelable. */ | |
2507 | if (cancelable_p) | |
a8745cc2 | 2508 | { |
a8745cc2 BC |
2509 | call_stmt = gimple_build_call_internal (IFN_LOOP_DIST_ALIAS, |
2510 | 2, NULL_TREE, cond_expr); | |
2511 | lhs = make_ssa_name (boolean_type_node); | |
2512 | gimple_call_set_lhs (call_stmt, lhs); | |
2513 | } | |
2514 | else | |
2515 | lhs = cond_expr; | |
2516 | ||
2517 | prob = profile_probability::guessed_always ().apply_scale (9, 10); | |
2518 | initialize_original_copy_tables (); | |
2519 | nloop = loop_version (loop, lhs, &cond_bb, prob, prob.invert (), | |
2520 | prob, prob.invert (), true); | |
2521 | free_original_copy_tables (); | |
2522 | /* Record the original loop number in newly generated loops. In case of | |
2523 | distribution, the original loop will be distributed and the new loop | |
2524 | is kept. */ | |
2525 | loop->orig_loop_num = nloop->num; | |
2526 | nloop->orig_loop_num = nloop->num; | |
2527 | nloop->dont_vectorize = true; | |
2528 | nloop->force_vectorize = false; | |
2529 | ||
2530 | if (call_stmt) | |
2531 | { | |
2532 | /* Record new loop's num in IFN_LOOP_DIST_ALIAS because the original | |
2533 | loop could be destroyed. */ | |
2534 | arg0 = build_int_cst (integer_type_node, loop->orig_loop_num); | |
2535 | gimple_call_set_arg (call_stmt, 0, arg0); | |
2536 | gimple_seq_add_stmt_without_update (&cond_stmts, call_stmt); | |
2537 | } | |
2538 | ||
2539 | if (cond_stmts) | |
2540 | { | |
2541 | gimple_stmt_iterator cond_gsi = gsi_last_bb (cond_bb); | |
2542 | gsi_insert_seq_before (&cond_gsi, cond_stmts, GSI_SAME_STMT); | |
2543 | } | |
2544 | update_ssa (TODO_update_ssa); | |
2545 | } | |
2546 | ||
2547 | /* Return true if loop versioning is needed to distrubute PARTITIONS. | |
2548 | ALIAS_DDRS are data dependence relations for runtime alias check. */ | |
2549 | ||
2550 | static inline bool | |
2551 | version_for_distribution_p (vec<struct partition *> *partitions, | |
2552 | vec<ddr_p> *alias_ddrs) | |
2553 | { | |
2554 | /* No need to version loop if we have only one partition. */ | |
2555 | if (partitions->length () == 1) | |
2556 | return false; | |
2557 | ||
2558 | /* Need to version loop if runtime alias check is necessary. */ | |
2559 | return (alias_ddrs->length () > 0); | |
2560 | } | |
2561 | ||
957f0d8f BC |
2562 | /* Compare base offset of builtin mem* partitions P1 and P2. */ |
2563 | ||
d2391983 AM |
2564 | static int |
2565 | offset_cmp (const void *vp1, const void *vp2) | |
957f0d8f | 2566 | { |
d2391983 AM |
2567 | struct partition *p1 = *(struct partition *const *) vp1; |
2568 | struct partition *p2 = *(struct partition *const *) vp2; | |
2569 | unsigned HOST_WIDE_INT o1 = p1->builtin->dst_base_offset; | |
2570 | unsigned HOST_WIDE_INT o2 = p2->builtin->dst_base_offset; | |
2571 | return (o2 < o1) - (o1 < o2); | |
957f0d8f BC |
2572 | } |
2573 | ||
2574 | /* Fuse adjacent memset builtin PARTITIONS if possible. This is a special | |
2575 | case optimization transforming below code: | |
2576 | ||
2577 | __builtin_memset (&obj, 0, 100); | |
2578 | _1 = &obj + 100; | |
2579 | __builtin_memset (_1, 0, 200); | |
2580 | _2 = &obj + 300; | |
2581 | __builtin_memset (_2, 0, 100); | |
2582 | ||
2583 | into: | |
2584 | ||
2585 | __builtin_memset (&obj, 0, 400); | |
2586 | ||
2587 | Note we don't have dependence information between different partitions | |
2588 | at this point, as a result, we can't handle nonadjacent memset builtin | |
2589 | partitions since dependence might be broken. */ | |
2590 | ||
2591 | static void | |
2592 | fuse_memset_builtins (vec<struct partition *> *partitions) | |
2593 | { | |
2594 | unsigned i, j; | |
2595 | struct partition *part1, *part2; | |
49e4ca31 | 2596 | tree rhs1, rhs2; |
957f0d8f BC |
2597 | |
2598 | for (i = 0; partitions->iterate (i, &part1);) | |
2599 | { | |
2600 | if (part1->kind != PKIND_MEMSET) | |
2601 | { | |
2602 | i++; | |
2603 | continue; | |
2604 | } | |
2605 | ||
2606 | /* Find sub-array of memset builtins of the same base. Index range | |
2607 | of the sub-array is [i, j) with "j > i". */ | |
2608 | for (j = i + 1; partitions->iterate (j, &part2); ++j) | |
2609 | { | |
2610 | if (part2->kind != PKIND_MEMSET | |
2611 | || !operand_equal_p (part1->builtin->dst_base_base, | |
2612 | part2->builtin->dst_base_base, 0)) | |
2613 | break; | |
49e4ca31 BC |
2614 | |
2615 | /* Memset calls setting different values can't be merged. */ | |
2616 | rhs1 = gimple_assign_rhs1 (DR_STMT (part1->builtin->dst_dr)); | |
2617 | rhs2 = gimple_assign_rhs1 (DR_STMT (part2->builtin->dst_dr)); | |
2618 | if (!operand_equal_p (rhs1, rhs2, 0)) | |
2619 | break; | |
957f0d8f BC |
2620 | } |
2621 | ||
2622 | /* Stable sort is required in order to avoid breaking dependence. */ | |
d2391983 AM |
2623 | gcc_stablesort (&(*partitions)[i], j - i, sizeof (*partitions)[i], |
2624 | offset_cmp); | |
957f0d8f BC |
2625 | /* Continue with next partition. */ |
2626 | i = j; | |
2627 | } | |
2628 | ||
2629 | /* Merge all consecutive memset builtin partitions. */ | |
2630 | for (i = 0; i < partitions->length () - 1;) | |
2631 | { | |
2632 | part1 = (*partitions)[i]; | |
2633 | if (part1->kind != PKIND_MEMSET) | |
2634 | { | |
2635 | i++; | |
2636 | continue; | |
2637 | } | |
2638 | ||
2639 | part2 = (*partitions)[i + 1]; | |
2640 | /* Only merge memset partitions of the same base and with constant | |
2641 | access sizes. */ | |
2642 | if (part2->kind != PKIND_MEMSET | |
2643 | || TREE_CODE (part1->builtin->size) != INTEGER_CST | |
2644 | || TREE_CODE (part2->builtin->size) != INTEGER_CST | |
2645 | || !operand_equal_p (part1->builtin->dst_base_base, | |
2646 | part2->builtin->dst_base_base, 0)) | |
2647 | { | |
2648 | i++; | |
2649 | continue; | |
2650 | } | |
49e4ca31 BC |
2651 | rhs1 = gimple_assign_rhs1 (DR_STMT (part1->builtin->dst_dr)); |
2652 | rhs2 = gimple_assign_rhs1 (DR_STMT (part2->builtin->dst_dr)); | |
957f0d8f BC |
2653 | int bytev1 = const_with_all_bytes_same (rhs1); |
2654 | int bytev2 = const_with_all_bytes_same (rhs2); | |
2655 | /* Only merge memset partitions of the same value. */ | |
2656 | if (bytev1 != bytev2 || bytev1 == -1) | |
2657 | { | |
2658 | i++; | |
2659 | continue; | |
2660 | } | |
2661 | wide_int end1 = wi::add (part1->builtin->dst_base_offset, | |
2662 | wi::to_wide (part1->builtin->size)); | |
2663 | /* Only merge adjacent memset partitions. */ | |
2664 | if (wi::ne_p (end1, part2->builtin->dst_base_offset)) | |
2665 | { | |
2666 | i++; | |
2667 | continue; | |
2668 | } | |
2669 | /* Merge partitions[i] and partitions[i+1]. */ | |
2670 | part1->builtin->size = fold_build2 (PLUS_EXPR, sizetype, | |
2671 | part1->builtin->size, | |
2672 | part2->builtin->size); | |
2673 | partition_free (part2); | |
2674 | partitions->ordered_remove (i + 1); | |
2675 | } | |
2676 | } | |
2677 | ||
163aa51b BC |
2678 | /* Fuse PARTITIONS of LOOP if necessary before finalizing distribution. |
2679 | ALIAS_DDRS contains ddrs which need runtime alias check. */ | |
a8745cc2 BC |
2680 | |
2681 | static void | |
163aa51b | 2682 | finalize_partitions (struct loop *loop, vec<struct partition *> *partitions, |
a8745cc2 BC |
2683 | vec<ddr_p> *alias_ddrs) |
2684 | { | |
2685 | unsigned i; | |
163aa51b | 2686 | struct partition *partition, *a; |
a8745cc2 BC |
2687 | |
2688 | if (partitions->length () == 1 | |
2689 | || alias_ddrs->length () > 0) | |
2690 | return; | |
2691 | ||
5955438a | 2692 | unsigned num_builtin = 0, num_normal = 0, num_partial_memset = 0; |
163aa51b BC |
2693 | bool same_type_p = true; |
2694 | enum partition_type type = ((*partitions)[0])->type; | |
2695 | for (i = 0; partitions->iterate (i, &partition); ++i) | |
a8745cc2 | 2696 | { |
163aa51b | 2697 | same_type_p &= (type == partition->type); |
5955438a BC |
2698 | if (partition_builtin_p (partition)) |
2699 | { | |
2700 | num_builtin++; | |
2701 | continue; | |
2702 | } | |
2703 | num_normal++; | |
2704 | if (partition->kind == PKIND_PARTIAL_MEMSET) | |
2705 | num_partial_memset++; | |
a8745cc2 BC |
2706 | } |
2707 | ||
163aa51b BC |
2708 | /* Don't distribute current loop into too many loops given we don't have |
2709 | memory stream cost model. Be even more conservative in case of loop | |
2710 | nest distribution. */ | |
5955438a BC |
2711 | if ((same_type_p && num_builtin == 0 |
2712 | && (loop->inner == NULL || num_normal != 2 || num_partial_memset != 1)) | |
163aa51b BC |
2713 | || (loop->inner != NULL |
2714 | && i >= NUM_PARTITION_THRESHOLD && num_normal > 1) | |
2715 | || (loop->inner == NULL | |
2716 | && i >= NUM_PARTITION_THRESHOLD && num_normal > num_builtin)) | |
a8745cc2 | 2717 | { |
163aa51b BC |
2718 | a = (*partitions)[0]; |
2719 | for (i = 1; partitions->iterate (i, &partition); ++i) | |
2720 | { | |
2721 | partition_merge_into (NULL, a, partition, FUSE_FINALIZE); | |
2722 | partition_free (partition); | |
2723 | } | |
2724 | partitions->truncate (1); | |
a8745cc2 | 2725 | } |
957f0d8f BC |
2726 | |
2727 | /* Fuse memset builtins if possible. */ | |
2728 | if (partitions->length () > 1) | |
2729 | fuse_memset_builtins (partitions); | |
a8745cc2 BC |
2730 | } |
2731 | ||
2732 | /* Distributes the code from LOOP in such a way that producer statements | |
2733 | are placed before consumer statements. Tries to separate only the | |
2734 | statements from STMTS into separate loops. Returns the number of | |
2735 | distributed loops. Set NB_CALLS to number of generated builtin calls. | |
2736 | Set *DESTROY_P to whether LOOP needs to be destroyed. */ | |
dea61d92 SP |
2737 | |
2738 | static int | |
355fe088 | 2739 | distribute_loop (struct loop *loop, vec<gimple *> stmts, |
b71b7a8e | 2740 | control_dependences *cd, int *nb_calls, bool *destroy_p) |
dea61d92 | 2741 | { |
1e485f89 | 2742 | ddrs_table = new hash_table<ddr_hasher> (389); |
2fd5894f | 2743 | struct graph *rdg; |
526ceb68 | 2744 | partition *partition; |
be6b029b | 2745 | bool any_builtin; |
2fd5894f | 2746 | int i, nbp; |
dea61d92 | 2747 | |
c9326aef | 2748 | *destroy_p = false; |
826a536d | 2749 | *nb_calls = 0; |
4084ea5f | 2750 | loop_nest.create (0); |
2fd5894f | 2751 | if (!find_loop_nest (loop, &loop_nest)) |
4084ea5f BC |
2752 | { |
2753 | loop_nest.release (); | |
1e485f89 | 2754 | delete ddrs_table; |
4084ea5f BC |
2755 | return 0; |
2756 | } | |
2fd5894f | 2757 | |
9fafb14a | 2758 | datarefs_vec.create (20); |
4084ea5f | 2759 | rdg = build_rdg (loop, cd); |
2fd5894f RB |
2760 | if (!rdg) |
2761 | { | |
2762 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2763 | fprintf (dump_file, | |
2764 | "Loop %d not distributed: failed to build the RDG.\n", | |
2765 | loop->num); | |
2766 | ||
4084ea5f | 2767 | loop_nest.release (); |
9fafb14a | 2768 | free_data_refs (datarefs_vec); |
1e485f89 | 2769 | delete ddrs_table; |
9fafb14a BC |
2770 | return 0; |
2771 | } | |
2772 | ||
2773 | if (datarefs_vec.length () > MAX_DATAREFS_NUM) | |
2774 | { | |
2775 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2776 | fprintf (dump_file, | |
2777 | "Loop %d not distributed: too many memory references.\n", | |
2778 | loop->num); | |
2779 | ||
2780 | free_rdg (rdg); | |
2781 | loop_nest.release (); | |
2782 | free_data_refs (datarefs_vec); | |
1e485f89 | 2783 | delete ddrs_table; |
2fd5894f RB |
2784 | return 0; |
2785 | } | |
2786 | ||
9fafb14a BC |
2787 | data_reference_p dref; |
2788 | for (i = 0; datarefs_vec.iterate (i, &dref); ++i) | |
2789 | dref->aux = (void *) (uintptr_t) i; | |
2790 | ||
2fd5894f RB |
2791 | if (dump_file && (dump_flags & TDF_DETAILS)) |
2792 | dump_rdg (dump_file, rdg); | |
2793 | ||
526ceb68 | 2794 | auto_vec<struct partition *, 3> partitions; |
2fd5894f | 2795 | rdg_build_partitions (rdg, stmts, &partitions); |
dea61d92 | 2796 | |
a8745cc2 BC |
2797 | auto_vec<ddr_p> alias_ddrs; |
2798 | ||
4a52eb19 BC |
2799 | auto_bitmap stmt_in_all_partitions; |
2800 | bitmap_copy (stmt_in_all_partitions, partitions[0]->stmts); | |
2801 | for (i = 1; partitions.iterate (i, &partition); ++i) | |
2802 | bitmap_and_into (stmt_in_all_partitions, partitions[i]->stmts); | |
2803 | ||
be6b029b | 2804 | any_builtin = false; |
9771b263 | 2805 | FOR_EACH_VEC_ELT (partitions, i, partition) |
be6b029b | 2806 | { |
4a52eb19 | 2807 | classify_partition (loop, rdg, partition, stmt_in_all_partitions); |
be6b029b RG |
2808 | any_builtin |= partition_builtin_p (partition); |
2809 | } | |
30d55936 | 2810 | |
447f3223 RB |
2811 | /* If we are only distributing patterns but did not detect any, |
2812 | simply bail out. */ | |
9fed7f3a RB |
2813 | if (!flag_tree_loop_distribution |
2814 | && !any_builtin) | |
2815 | { | |
2816 | nbp = 0; | |
2817 | goto ldist_done; | |
2818 | } | |
2819 | ||
447f3223 RB |
2820 | /* If we are only distributing patterns fuse all partitions that |
2821 | were not classified as builtins. This also avoids chopping | |
2822 | a loop into pieces, separated by builtin calls. That is, we | |
2823 | only want no or a single loop body remaining. */ | |
526ceb68 | 2824 | struct partition *into; |
447f3223 RB |
2825 | if (!flag_tree_loop_distribution) |
2826 | { | |
2827 | for (i = 0; partitions.iterate (i, &into); ++i) | |
2828 | if (!partition_builtin_p (into)) | |
2829 | break; | |
2830 | for (++i; partitions.iterate (i, &partition); ++i) | |
2831 | if (!partition_builtin_p (partition)) | |
2832 | { | |
f1eb4621 | 2833 | partition_merge_into (NULL, into, partition, FUSE_NON_BUILTIN); |
447f3223 RB |
2834 | partitions.unordered_remove (i); |
2835 | partition_free (partition); | |
2836 | i--; | |
2837 | } | |
2838 | } | |
2839 | ||
2840 | /* Due to limitations in the transform phase we have to fuse all | |
2841 | reduction partitions into the last partition so the existing | |
2842 | loop will contain all loop-closed PHI nodes. */ | |
2843 | for (i = 0; partitions.iterate (i, &into); ++i) | |
2844 | if (partition_reduction_p (into)) | |
2845 | break; | |
2846 | for (i = i + 1; partitions.iterate (i, &partition); ++i) | |
2847 | if (partition_reduction_p (partition)) | |
2848 | { | |
f1eb4621 | 2849 | partition_merge_into (rdg, into, partition, FUSE_REDUCTION); |
447f3223 RB |
2850 | partitions.unordered_remove (i); |
2851 | partition_free (partition); | |
2852 | i--; | |
2853 | } | |
2854 | ||
9fed7f3a RB |
2855 | /* Apply our simple cost model - fuse partitions with similar |
2856 | memory accesses. */ | |
9fed7f3a RB |
2857 | for (i = 0; partitions.iterate (i, &into); ++i) |
2858 | { | |
16eba420 | 2859 | bool changed = false; |
5955438a | 2860 | if (partition_builtin_p (into) || into->kind == PKIND_PARTIAL_MEMSET) |
9fed7f3a RB |
2861 | continue; |
2862 | for (int j = i + 1; | |
2863 | partitions.iterate (j, &partition); ++j) | |
2864 | { | |
95f7d11b | 2865 | if (share_memory_accesses (rdg, into, partition)) |
9fed7f3a | 2866 | { |
f1eb4621 | 2867 | partition_merge_into (rdg, into, partition, FUSE_SHARE_REF); |
447f3223 | 2868 | partitions.unordered_remove (j); |
9fed7f3a RB |
2869 | partition_free (partition); |
2870 | j--; | |
16eba420 | 2871 | changed = true; |
9fed7f3a RB |
2872 | } |
2873 | } | |
16eba420 RB |
2874 | /* If we fused 0 1 2 in step 1 to 0,2 1 as 0 and 2 have similar |
2875 | accesses when 1 and 2 have similar accesses but not 0 and 1 | |
2876 | then in the next iteration we will fail to consider merging | |
2877 | 1 into 0,2. So try again if we did any merging into 0. */ | |
2878 | if (changed) | |
2879 | i--; | |
9fed7f3a RB |
2880 | } |
2881 | ||
163aa51b BC |
2882 | /* Build the partition dependency graph and fuse partitions in strong |
2883 | connected component. */ | |
447f3223 | 2884 | if (partitions.length () > 1) |
c014f6f5 | 2885 | { |
163aa51b BC |
2886 | /* Don't support loop nest distribution under runtime alias check |
2887 | since it's not likely to enable many vectorization opportunities. */ | |
2888 | if (loop->inner) | |
2889 | merge_dep_scc_partitions (rdg, &partitions, false); | |
2890 | else | |
2891 | { | |
2892 | merge_dep_scc_partitions (rdg, &partitions, true); | |
2893 | if (partitions.length () > 1) | |
2894 | break_alias_scc_partitions (rdg, &partitions, &alias_ddrs); | |
2895 | } | |
b9fc0497 RB |
2896 | } |
2897 | ||
163aa51b | 2898 | finalize_partitions (loop, &partitions, &alias_ddrs); |
a8745cc2 | 2899 | |
9771b263 | 2900 | nbp = partitions.length (); |
a4293fa6 | 2901 | if (nbp == 0 |
9771b263 DN |
2902 | || (nbp == 1 && !partition_builtin_p (partitions[0])) |
2903 | || (nbp > 1 && partition_contains_all_rw (rdg, partitions))) | |
c014f6f5 RG |
2904 | { |
2905 | nbp = 0; | |
2906 | goto ldist_done; | |
2907 | } | |
dea61d92 | 2908 | |
a8745cc2 | 2909 | if (version_for_distribution_p (&partitions, &alias_ddrs)) |
1623d9f3 | 2910 | version_loop_by_alias_check (&partitions, loop, &alias_ddrs); |
a8745cc2 | 2911 | |
dea61d92 | 2912 | if (dump_file && (dump_flags & TDF_DETAILS)) |
a8745cc2 BC |
2913 | { |
2914 | fprintf (dump_file, | |
2915 | "distribute loop <%d> into partitions:\n", loop->num); | |
2916 | dump_rdg_partitions (dump_file, partitions); | |
2917 | } | |
dea61d92 | 2918 | |
9771b263 | 2919 | FOR_EACH_VEC_ELT (partitions, i, partition) |
826a536d RB |
2920 | { |
2921 | if (partition_builtin_p (partition)) | |
2922 | (*nb_calls)++; | |
b71b7a8e | 2923 | *destroy_p |= generate_code_for_partition (loop, partition, i < nbp - 1); |
826a536d | 2924 | } |
dea61d92 | 2925 | |
dea61d92 | 2926 | ldist_done: |
4084ea5f | 2927 | loop_nest.release (); |
9fafb14a | 2928 | free_data_refs (datarefs_vec); |
1e485f89 ML |
2929 | for (hash_table<ddr_hasher>::iterator iter = ddrs_table->begin (); |
2930 | iter != ddrs_table->end (); ++iter) | |
17c5cbdf BC |
2931 | { |
2932 | free_dependence_relation (*iter); | |
2933 | *iter = NULL; | |
2934 | } | |
1e485f89 | 2935 | delete ddrs_table; |
dea61d92 | 2936 | |
9771b263 | 2937 | FOR_EACH_VEC_ELT (partitions, i, partition) |
c61f8985 | 2938 | partition_free (partition); |
dea61d92 | 2939 | |
dea61d92 | 2940 | free_rdg (rdg); |
826a536d | 2941 | return nbp - *nb_calls; |
dea61d92 SP |
2942 | } |
2943 | ||
2944 | /* Distribute all loops in the current function. */ | |
2945 | ||
be55bfe6 TS |
2946 | namespace { |
2947 | ||
2948 | const pass_data pass_data_loop_distribution = | |
2949 | { | |
2950 | GIMPLE_PASS, /* type */ | |
2951 | "ldist", /* name */ | |
2952 | OPTGROUP_LOOP, /* optinfo_flags */ | |
be55bfe6 TS |
2953 | TV_TREE_LOOP_DISTRIBUTION, /* tv_id */ |
2954 | ( PROP_cfg | PROP_ssa ), /* properties_required */ | |
2955 | 0, /* properties_provided */ | |
2956 | 0, /* properties_destroyed */ | |
2957 | 0, /* todo_flags_start */ | |
3bea341f | 2958 | 0, /* todo_flags_finish */ |
be55bfe6 TS |
2959 | }; |
2960 | ||
2961 | class pass_loop_distribution : public gimple_opt_pass | |
2962 | { | |
2963 | public: | |
2964 | pass_loop_distribution (gcc::context *ctxt) | |
2965 | : gimple_opt_pass (pass_data_loop_distribution, ctxt) | |
2966 | {} | |
2967 | ||
2968 | /* opt_pass methods: */ | |
2969 | virtual bool gate (function *) | |
2970 | { | |
2971 | return flag_tree_loop_distribution | |
2972 | || flag_tree_loop_distribute_patterns; | |
2973 | } | |
2974 | ||
2975 | virtual unsigned int execute (function *); | |
2976 | ||
2977 | }; // class pass_loop_distribution | |
2978 | ||
163aa51b BC |
2979 | |
2980 | /* Given LOOP, this function records seed statements for distribution in | |
2981 | WORK_LIST. Return false if there is nothing for distribution. */ | |
2982 | ||
2983 | static bool | |
2984 | find_seed_stmts_for_distribution (struct loop *loop, vec<gimple *> *work_list) | |
2985 | { | |
2986 | basic_block *bbs = get_loop_body_in_dom_order (loop); | |
2987 | ||
2988 | /* Initialize the worklist with stmts we seed the partitions with. */ | |
2989 | for (unsigned i = 0; i < loop->num_nodes; ++i) | |
2990 | { | |
2991 | for (gphi_iterator gsi = gsi_start_phis (bbs[i]); | |
2992 | !gsi_end_p (gsi); gsi_next (&gsi)) | |
2993 | { | |
2994 | gphi *phi = gsi.phi (); | |
2995 | if (virtual_operand_p (gimple_phi_result (phi))) | |
2996 | continue; | |
2997 | /* Distribute stmts which have defs that are used outside of | |
2998 | the loop. */ | |
2999 | if (!stmt_has_scalar_dependences_outside_loop (loop, phi)) | |
3000 | continue; | |
3001 | work_list->safe_push (phi); | |
3002 | } | |
3003 | for (gimple_stmt_iterator gsi = gsi_start_bb (bbs[i]); | |
3004 | !gsi_end_p (gsi); gsi_next (&gsi)) | |
3005 | { | |
3006 | gimple *stmt = gsi_stmt (gsi); | |
3007 | ||
3008 | /* If there is a stmt with side-effects bail out - we | |
3009 | cannot and should not distribute this loop. */ | |
3010 | if (gimple_has_side_effects (stmt)) | |
3011 | { | |
3012 | free (bbs); | |
3013 | return false; | |
3014 | } | |
3015 | ||
3016 | /* Distribute stmts which have defs that are used outside of | |
3017 | the loop. */ | |
3018 | if (stmt_has_scalar_dependences_outside_loop (loop, stmt)) | |
3019 | ; | |
3020 | /* Otherwise only distribute stores for now. */ | |
3021 | else if (!gimple_vdef (stmt)) | |
3022 | continue; | |
3023 | ||
3024 | work_list->safe_push (stmt); | |
3025 | } | |
3026 | } | |
3027 | free (bbs); | |
3028 | return work_list->length () > 0; | |
3029 | } | |
3030 | ||
3031 | /* Given innermost LOOP, return the outermost enclosing loop that forms a | |
3032 | perfect loop nest. */ | |
3033 | ||
3034 | static struct loop * | |
3035 | prepare_perfect_loop_nest (struct loop *loop) | |
3036 | { | |
3037 | struct loop *outer = loop_outer (loop); | |
3038 | tree niters = number_of_latch_executions (loop); | |
3039 | ||
5955438a | 3040 | /* TODO: We only support the innermost 3-level loop nest distribution |
163aa51b | 3041 | because of compilation time issue for now. This should be relaxed |
5955438a BC |
3042 | in the future. Note we only allow 3-level loop nest distribution |
3043 | when parallelizing loops. */ | |
3044 | while ((loop->inner == NULL | |
3045 | || (loop->inner->inner == NULL && flag_tree_parallelize_loops > 1)) | |
163aa51b BC |
3046 | && loop_outer (outer) |
3047 | && outer->inner == loop && loop->next == NULL | |
3048 | && single_exit (outer) | |
3049 | && optimize_loop_for_speed_p (outer) | |
3050 | && !chrec_contains_symbols_defined_in_loop (niters, outer->num) | |
3051 | && (niters = number_of_latch_executions (outer)) != NULL_TREE | |
3052 | && niters != chrec_dont_know) | |
3053 | { | |
3054 | loop = outer; | |
3055 | outer = loop_outer (loop); | |
3056 | } | |
3057 | ||
3058 | return loop; | |
3059 | } | |
3060 | ||
be55bfe6 TS |
3061 | unsigned int |
3062 | pass_loop_distribution::execute (function *fun) | |
dea61d92 SP |
3063 | { |
3064 | struct loop *loop; | |
c014f6f5 | 3065 | bool changed = false; |
1fa0c180 | 3066 | basic_block bb; |
36875e8f | 3067 | control_dependences *cd = NULL; |
b71b7a8e | 3068 | auto_vec<loop_p> loops_to_be_destroyed; |
1fa0c180 | 3069 | |
773d9217 BC |
3070 | if (number_of_loops (fun) <= 1) |
3071 | return 0; | |
3072 | ||
3be57c56 BC |
3073 | /* Compute topological order for basic blocks. Topological order is |
3074 | needed because data dependence is computed for data references in | |
3075 | lexicographical order. */ | |
3076 | if (bb_top_order_index == NULL) | |
3077 | { | |
3fb82452 | 3078 | int rpo_num; |
3be57c56 BC |
3079 | int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun)); |
3080 | ||
3081 | bb_top_order_index = XNEWVEC (int, last_basic_block_for_fn (cfun)); | |
3fb82452 BC |
3082 | bb_top_order_index_size = last_basic_block_for_fn (cfun); |
3083 | rpo_num = pre_and_rev_post_order_compute_fn (cfun, NULL, rpo, true); | |
3084 | for (int i = 0; i < rpo_num; i++) | |
3be57c56 BC |
3085 | bb_top_order_index[rpo[i]] = i; |
3086 | ||
3087 | free (rpo); | |
3088 | } | |
3089 | ||
be55bfe6 | 3090 | FOR_ALL_BB_FN (bb, fun) |
1fa0c180 RG |
3091 | { |
3092 | gimple_stmt_iterator gsi; | |
3093 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
3094 | gimple_set_uid (gsi_stmt (gsi), -1); | |
3095 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
3096 | gimple_set_uid (gsi_stmt (gsi), -1); | |
3097 | } | |
dea61d92 | 3098 | |
c014f6f5 RG |
3099 | /* We can at the moment only distribute non-nested loops, thus restrict |
3100 | walking to innermost loops. */ | |
f0bd40b1 | 3101 | FOR_EACH_LOOP (loop, LI_ONLY_INNERMOST) |
dea61d92 | 3102 | { |
163aa51b BC |
3103 | /* Don't distribute multiple exit edges loop, or cold loop. */ |
3104 | if (!single_exit (loop) | |
3105 | || !optimize_loop_for_speed_p (loop)) | |
f56f2d33 JH |
3106 | continue; |
3107 | ||
6ff37519 BC |
3108 | /* Don't distribute loop if niters is unknown. */ |
3109 | tree niters = number_of_latch_executions (loop); | |
3110 | if (niters == NULL_TREE || niters == chrec_dont_know) | |
3111 | continue; | |
3112 | ||
163aa51b BC |
3113 | /* Get the perfect loop nest for distribution. */ |
3114 | loop = prepare_perfect_loop_nest (loop); | |
3115 | for (; loop; loop = loop->inner) | |
be6b029b | 3116 | { |
163aa51b BC |
3117 | auto_vec<gimple *> work_list; |
3118 | if (!find_seed_stmts_for_distribution (loop, &work_list)) | |
3119 | break; | |
c014f6f5 | 3120 | |
163aa51b | 3121 | const char *str = loop->inner ? " nest" : ""; |
4f5b9c80 | 3122 | dump_user_location_t loc = find_loop_location (loop); |
36875e8f RB |
3123 | if (!cd) |
3124 | { | |
ca406576 | 3125 | calculate_dominance_info (CDI_DOMINATORS); |
36875e8f | 3126 | calculate_dominance_info (CDI_POST_DOMINATORS); |
30fd2977 | 3127 | cd = new control_dependences (); |
36875e8f RB |
3128 | free_dominance_info (CDI_POST_DOMINATORS); |
3129 | } | |
163aa51b | 3130 | |
b71b7a8e | 3131 | bool destroy_p; |
163aa51b | 3132 | int nb_generated_loops, nb_generated_calls; |
826a536d | 3133 | nb_generated_loops = distribute_loop (loop, work_list, cd, |
b71b7a8e RB |
3134 | &nb_generated_calls, |
3135 | &destroy_p); | |
3136 | if (destroy_p) | |
3137 | loops_to_be_destroyed.safe_push (loop); | |
c014f6f5 | 3138 | |
163aa51b BC |
3139 | if (nb_generated_loops + nb_generated_calls > 0) |
3140 | { | |
3141 | changed = true; | |
bbeeac91 DM |
3142 | if (dump_enabled_p ()) |
3143 | dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, | |
3144 | loc, "Loop%s %d distributed: split to %d loops " | |
3145 | "and %d library calls.\n", str, loop->num, | |
3146 | nb_generated_loops, nb_generated_calls); | |
163aa51b BC |
3147 | |
3148 | break; | |
3149 | } | |
3150 | ||
3151 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3152 | fprintf (dump_file, "Loop%s %d not distributed.\n", str, loop->num); | |
dea61d92 | 3153 | } |
dea61d92 SP |
3154 | } |
3155 | ||
36875e8f RB |
3156 | if (cd) |
3157 | delete cd; | |
3158 | ||
3be57c56 BC |
3159 | if (bb_top_order_index != NULL) |
3160 | { | |
3161 | free (bb_top_order_index); | |
3162 | bb_top_order_index = NULL; | |
3163 | bb_top_order_index_size = 0; | |
3164 | } | |
3165 | ||
c014f6f5 RG |
3166 | if (changed) |
3167 | { | |
30fd2977 RB |
3168 | /* Destroy loop bodies that could not be reused. Do this late as we |
3169 | otherwise can end up refering to stale data in control dependences. */ | |
3170 | unsigned i; | |
3171 | FOR_EACH_VEC_ELT (loops_to_be_destroyed, i, loop) | |
3be57c56 | 3172 | destroy_loop (loop); |
30fd2977 | 3173 | |
d0ed943c RB |
3174 | /* Cached scalar evolutions now may refer to wrong or non-existing |
3175 | loops. */ | |
3176 | scev_reset_htab (); | |
be55bfe6 | 3177 | mark_virtual_operands_for_renaming (fun); |
c014f6f5 RG |
3178 | rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa); |
3179 | } | |
3180 | ||
b2b29377 | 3181 | checking_verify_loop_structure (); |
c014f6f5 | 3182 | |
1bfb3b8b | 3183 | return changed ? TODO_cleanup_cfg : 0; |
dea61d92 SP |
3184 | } |
3185 | ||
27a4cd48 DM |
3186 | } // anon namespace |
3187 | ||
3188 | gimple_opt_pass * | |
3189 | make_pass_loop_distribution (gcc::context *ctxt) | |
3190 | { | |
3191 | return new pass_loop_distribution (ctxt); | |
3192 | } |