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