]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-loop-distribution.c
* doc/install.texi (Specific, alpha): Remove note to use
[thirdparty/gcc.git] / gcc / tree-loop-distribution.c
CommitLineData
801c5610 1/* Loop distribution.
fbd26352 2 Copyright (C) 2006-2019 Free Software Foundation, Inc.
801c5610 3 Contributed by Georges-Andre Silber <Georges-Andre.Silber@ensmp.fr>
4 and Sebastian Pop <sebastian.pop@amd.com>.
5
6This file is part of GCC.
48e1416a 7
801c5610 8GCC is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 3, or (at your option) any
11later version.
48e1416a 12
801c5610 13GCC is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
48e1416a 17
801c5610 18You should have received a copy of the GNU General Public License
19along 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
48e1416a 29 is transformed to
801c5610 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
f562e2ea 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:
883b4905 86 1) We only distribute innermost two-level loop nest now. We should
87 extend it for arbitrary loop nests in the future.
f562e2ea 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. */
801c5610 91
92#include "config.h"
93#include "system.h"
94#include "coretypes.h"
9ef16211 95#include "backend.h"
b20a8bb4 96#include "tree.h"
9ef16211 97#include "gimple.h"
7c29e30e 98#include "cfghooks.h"
99#include "tree-pass.h"
9ef16211 100#include "ssa.h"
7c29e30e 101#include "gimple-pretty-print.h"
9ef16211 102#include "fold-const.h"
94ea8568 103#include "cfganal.h"
dcf1a1ec 104#include "gimple-iterator.h"
e795d6e1 105#include "gimplify-me.h"
9ed99284 106#include "stor-layout.h"
073c1fd5 107#include "tree-cfg.h"
05d9c18a 108#include "tree-ssa-loop-manip.h"
05ebeee6 109#include "tree-ssa-loop-ivopts.h"
073c1fd5 110#include "tree-ssa-loop.h"
111#include "tree-into-ssa.h"
69ee5dbb 112#include "tree-ssa.h"
801c5610 113#include "cfgloop.h"
801c5610 114#include "tree-scalar-evolution.h"
f3754041 115#include "params.h"
df9892ff 116#include "tree-vectorizer.h"
bc6e884f 117#include "tree-eh.h"
98125d0d 118#include "gimple-fold.h"
a2740310 119
120
f3754041 121#define MAX_DATAREFS_NUM \
122 ((unsigned) PARAM_VALUE (PARAM_LOOP_MAX_DATAREFS_FOR_DATADEPS))
123
883b4905 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
50f5937e 129/* Hashtable helpers. */
130
131struct 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
140inline hashval_t
141ddr_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
151inline bool
152ddr_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
209a62a6 158/* The loop (nest) to be distributed. */
159static vec<loop_p> loop_nest;
160
f3754041 161/* Vector of data references in the loop to be distributed. */
162static vec<data_reference_p> datarefs_vec;
163
6079e9be 164/* If there is nonaddressable data reference in above vector. */
165static bool has_nonaddressable_dataref_p;
166
f3754041 167/* Store index of data reference in aux field. */
168#define DR_INDEX(dr) ((uintptr_t) (dr)->aux)
169
50f5937e 170/* Hash table for data dependence relation in the loop to be distributed. */
bbb229ef 171static hash_table<ddr_hasher> *ddrs_table;
50f5937e 172
a2740310 173/* A Reduced Dependence Graph (RDG) vertex representing a statement. */
04009ada 174struct rdg_vertex
a2740310 175{
176 /* The statement represented by this vertex. */
42acab1c 177 gimple *stmt;
a2740310 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;
04009ada 187};
a2740310 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
200enum rdg_dep_type
201{
202 /* Read After Write (RAW). */
203 flow_dd = 'f',
204
f1ce84d9 205 /* Control dependence (execute conditional on). */
206 control_dd = 'c'
a2740310 207};
208
209/* Dependence information attached to an edge of the RDG. */
210
04009ada 211struct rdg_edge
a2740310 212{
213 /* Type of the dependence. */
214 enum rdg_dep_type type;
04009ada 215};
a2740310 216
217#define RDGE_TYPE(E) ((struct rdg_edge *) ((E)->data))->type
a2740310 218
a2740310 219/* Dump vertex I in RDG to FILE. */
220
221static void
222dump_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
248DEBUG_FUNCTION void
249debug_rdg_vertex (struct graph *rdg, int i)
250{
251 dump_rdg_vertex (stderr, rdg, i);
252}
253
a2740310 254/* Dump the reduced dependence graph RDG to FILE. */
255
256static void
257dump_rdg (FILE *file, struct graph *rdg)
258{
a2740310 259 fprintf (file, "(rdg\n");
15c8650d 260 for (int i = 0; i < rdg->n_vertices; i++)
261 dump_rdg_vertex (file, rdg, i);
a2740310 262 fprintf (file, ")\n");
a2740310 263}
264
265/* Call dump_rdg on stderr. */
266
267DEBUG_FUNCTION void
268debug_rdg (struct graph *rdg)
269{
270 dump_rdg (stderr, rdg);
271}
272
273static void
274dot_rdg_1 (FILE *file, struct graph *rdg)
275{
276 int i;
5529df60 277 pretty_printer buffer;
278 pp_needs_newline (&buffer) = false;
279 buffer.buffer->stream = file;
a2740310 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
5529df60 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
a2740310 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 {
a2740310 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
f1ce84d9 310 case control_dd:
311 fprintf (file, "%d -> %d [label=control] \n", i, e->dest);
312 break;
313
a2740310 314 default:
315 gcc_unreachable ();
316 }
317 }
318
319 fprintf (file, "}\n\n");
320}
321
322/* Display the Reduced Dependence Graph using dotty. */
323
324DEBUG_FUNCTION void
325dot_rdg (struct graph *rdg)
326{
5529df60 327 /* When debugging, you may want to enable the following code. */
6cc8a944 328#ifdef HAVE_POPEN
9af5ce0c 329 FILE *file = popen ("dot -Tx11", "w");
5529df60 330 if (!file)
331 return;
a2740310 332 dot_rdg_1 (file, rdg);
5529df60 333 fflush (file);
334 close (fileno (file));
335 pclose (file);
a2740310 336#else
337 dot_rdg_1 (stderr, rdg);
338#endif
339}
340
341/* Returns the index of STMT in RDG. */
342
343static int
42acab1c 344rdg_vertex_for_stmt (struct graph *rdg ATTRIBUTE_UNUSED, gimple *stmt)
a2740310 345{
346 int index = gimple_uid (stmt);
347 gcc_checking_assert (index == -1 || RDG_STMT (rdg, index) == stmt);
348 return index;
349}
350
a2740310 351/* Creates dependence edges in RDG for all the uses of DEF. IDEF is
352 the index of DEF in RDG. */
353
354static void
355create_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;
a2740310 371 }
372}
373
f1ce84d9 374/* Creates an edge for the control dependences of BB to the vertex V. */
375
376static void
377create_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 {
ce143ff0 385 basic_block cond_bb = cd->get_edge_src (edge_n);
42acab1c 386 gimple *stmt = last_stmt (cond_bb);
f1ce84d9 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;
f1ce84d9 397 }
398 }
399}
400
a2740310 401/* Creates the edges of the reduced dependence graph RDG. */
402
403static void
7103facc 404create_rdg_flow_edges (struct graph *rdg)
a2740310 405{
406 int i;
a2740310 407 def_operand_p def_p;
408 ssa_op_iter iter;
409
a2740310 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);
7103facc 414}
f1ce84d9 415
7103facc 416/* Creates the edges of the reduced dependence graph RDG. */
417
418static void
77d095c0 419create_rdg_cd_edges (struct graph *rdg, control_dependences *cd, loop_p loop)
7103facc 420{
421 int i;
422
423 for (i = 0; i < rdg->n_vertices; i++)
424 {
42acab1c 425 gimple *stmt = RDG_STMT (rdg, i);
7103facc 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)
77d095c0 431 if (flow_bb_inside_loop_p (loop, e->src))
f1ce84d9 432 create_edge_for_control_dependence (rdg, e->src, i, cd);
7103facc 433 }
434 else
435 create_edge_for_control_dependence (rdg, gimple_bb (stmt), i, cd);
436 }
a2740310 437}
438
439/* Build the vertices of the reduced dependence graph RDG. Return false
440 if that failed. */
441
442static bool
f3754041 443create_rdg_vertices (struct graph *rdg, vec<gimple *> stmts, loop_p loop)
a2740310 444{
445 int i;
42acab1c 446 gimple *stmt;
a2740310 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
f3754041 463 unsigned drp = datarefs_vec.length ();
464 if (!find_data_references_in_stmt (loop, stmt, &datarefs_vec))
a2740310 465 return false;
f3754041 466 for (unsigned j = drp; j < datarefs_vec.length (); ++j)
a2740310 467 {
f3754041 468 data_reference_p dr = datarefs_vec[j];
a2740310 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);
6079e9be 474 has_nonaddressable_dataref_p |= may_be_nonaddressable_p (dr->ref);
a2740310 475 }
476 }
477 return true;
478}
479
50eda3a8 480/* Array mapping basic block's index to its topological order. */
481static int *bb_top_order_index;
482/* And size of the array. */
483static 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
488static int
489bb_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. */
a2740310 507
508static void
2e966e2a 509stmts_from_loop (class loop *loop, vec<gimple *> *stmts)
a2740310 510{
511 unsigned int i;
50eda3a8 512 basic_block *bbs = get_loop_body_in_custom_order (loop, bb_top_order_cmp);
a2740310 513
514 for (i = 0; i < loop->num_nodes; i++)
515 {
516 basic_block bb = bbs[i];
a2740310 517
1a91d914 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 ());
a2740310 522
1a91d914 523 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
524 gsi_next (&bsi))
a2740310 525 {
42acab1c 526 gimple *stmt = gsi_stmt (bsi);
a2740310 527 if (gimple_code (stmt) != GIMPLE_LABEL && !is_gimple_debug (stmt))
528 stmts->safe_push (stmt);
529 }
530 }
531
532 free (bbs);
533}
534
a2740310 535/* Free the reduced dependence graph RDG. */
536
537static void
538free_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)
7103facc 548 free (e->data);
a2740310 549
550 if (v->data)
551 {
552 gimple_set_uid (RDGV_STMT (v), -1);
f3754041 553 (RDGV_DATAREFS (v)).release ();
a2740310 554 free (v->data);
555 }
556 }
557
558 free_graph (rdg);
559}
560
209a62a6 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
f3754041 563 dependence CD. During visiting each statement, data references are also
564 collected and recorded in global data DATAREFS_VEC. */
a2740310 565
566static struct graph *
2e966e2a 567build_rdg (class loop *loop, control_dependences *cd)
a2740310 568{
569 struct graph *rdg;
a2740310 570
eeaec025 571 /* Create the RDG vertices from the stmts of the loop nest. */
42acab1c 572 auto_vec<gimple *, 10> stmts;
209a62a6 573 stmts_from_loop (loop, &stmts);
b223e75a 574 rdg = new_graph (stmts.length ());
f3754041 575 if (!create_rdg_vertices (rdg, stmts, loop))
a2740310 576 {
a2740310 577 free_rdg (rdg);
578 return NULL;
579 }
580 stmts.release ();
eeaec025 581
7103facc 582 create_rdg_flow_edges (rdg);
583 if (cd)
209a62a6 584 create_rdg_cd_edges (rdg, cd, loop);
7103facc 585
a2740310 586 return rdg;
587}
588
a2740310 589
f024aa04 590/* Kind of distributed loop. */
ac7a1007 591enum partition_kind {
b1c062d8 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
ac7a1007 605};
d32bc1d7 606
f024aa04 607/* Type of distributed loop. */
608enum 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
506fcb4f 615/* Builtin info for loop distribution. */
616struct 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;
05ebeee6 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;
506fcb4f 630};
631
889a3926 632/* Partition for loop distribution. */
04009ada 633struct partition
543506e0 634{
889a3926 635 /* Statements of the partition. */
543506e0 636 bitmap stmts;
889a3926 637 /* True if the partition defines variable which is used outside of loop. */
df9892ff 638 bool reduction_p;
e68b4984 639 location_t loc;
d32bc1d7 640 enum partition_kind kind;
f024aa04 641 enum partition_type type;
889a3926 642 /* Data references in the partition. */
643 bitmap datarefs;
506fcb4f 644 /* Information of builtin parition. */
645 struct builtin_info *builtin;
04009ada 646};
543506e0 647
543506e0 648
649/* Allocate and initialize a partition from BITMAP. */
650
04009ada 651static partition *
889a3926 652partition_alloc (void)
543506e0 653{
04009ada 654 partition *partition = XCNEW (struct partition);
889a3926 655 partition->stmts = BITMAP_ALLOC (NULL);
df9892ff 656 partition->reduction_p = false;
e68b4984 657 partition->loc = UNKNOWN_LOCATION;
d32bc1d7 658 partition->kind = PKIND_NORMAL;
e68b4984 659 partition->type = PTYPE_PARALLEL;
889a3926 660 partition->datarefs = BITMAP_ALLOC (NULL);
543506e0 661 return partition;
662}
663
664/* Free PARTITION. */
665
666static void
04009ada 667partition_free (partition *partition)
543506e0 668{
669 BITMAP_FREE (partition->stmts);
889a3926 670 BITMAP_FREE (partition->datarefs);
506fcb4f 671 if (partition->builtin)
672 free (partition->builtin);
673
543506e0 674 free (partition);
675}
676
d32bc1d7 677/* Returns true if the partition can be generated as a builtin. */
678
679static bool
04009ada 680partition_builtin_p (partition *partition)
d32bc1d7 681{
b1c062d8 682 return partition->kind > PKIND_PARTIAL_MEMSET;
d32bc1d7 683}
543506e0 684
df9892ff 685/* Returns true if the partition contains a reduction. */
e5edce84 686
687static bool
04009ada 688partition_reduction_p (partition *partition)
e5edce84 689{
df9892ff 690 return partition->reduction_p;
e5edce84 691}
692
f1edc00d 693/* Partitions are fused because of different reasons. */
694enum 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. */
704static 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
df9892ff 711static void
f024aa04 712update_type_for_merge (struct graph *, partition *, partition *);
f1edc00d 713
f024aa04 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. */
889a3926 716
f024aa04 717static void
718partition_merge_into (struct graph *rdg, partition *dest,
719 partition *partition, enum fuse_type ft)
720{
f1edc00d 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 }
f024aa04 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);
df9892ff 744}
745
746
b5698e04 747/* Returns true when DEF is an SSA_NAME defined in LOOP and used after
748 the LOOP. */
749
750static bool
751ssa_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)
64a31469 757 {
883b4905 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))
64a31469 763 return true;
764 }
b5698e04 765
766 return false;
767}
768
769/* Returns true when STMT defines a scalar variable used after the
bfcf35ff 770 loop LOOP. */
b5698e04 771
772static bool
42acab1c 773stmt_has_scalar_dependences_outside_loop (loop_p loop, gimple *stmt)
b5698e04 774{
bfcf35ff 775 def_operand_p def_p;
776 ssa_op_iter op_iter;
b5698e04 777
35ec0372 778 if (gimple_code (stmt) == GIMPLE_PHI)
779 return ssa_name_has_uses_outside_loop_p (gimple_phi_result (stmt), loop);
780
bfcf35ff 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;
b5698e04 784
bfcf35ff 785 return false;
b5698e04 786}
787
801c5610 788/* Return a copy of LOOP placed before LOOP. */
789
2e966e2a 790static class loop *
791copy_loop_before (class loop *loop)
801c5610 792{
2e966e2a 793 class loop *res;
801c5610 794 edge preheader = loop_preheader_edge (loop);
795
801c5610 796 initialize_original_copy_tables ();
c71d3c24 797 res = slpeel_tree_duplicate_loop_to_edge_cfg (loop, NULL, preheader);
d32bc1d7 798 gcc_assert (res != NULL);
801c5610 799 free_original_copy_tables ();
c9b2c569 800 delete_update_ssa ();
801c5610 801
802 return res;
803}
804
805/* Creates an empty basic block after LOOP. */
806
807static void
2e966e2a 808create_bb_after_loop (class loop *loop)
801c5610 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
d32bc1d7 822 basic blocks of a loop are taken in dom order. */
801c5610 823
d32bc1d7 824static void
2e966e2a 825generate_loops_for_partition (class loop *loop, partition *partition,
543506e0 826 bool copy_p)
801c5610 827{
15c8650d 828 unsigned i;
801c5610 829 basic_block *bbs;
830
831 if (copy_p)
832 {
f562e2ea 833 int orig_loop_num = loop->orig_loop_num;
801c5610 834 loop = copy_loop_before (loop);
d32bc1d7 835 gcc_assert (loop != NULL);
f562e2ea 836 loop->orig_loop_num = orig_loop_num;
801c5610 837 create_preheader (loop, CP_SIMPLE_PREHEADERS);
838 create_bb_after_loop (loop);
839 }
f562e2ea 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 }
801c5610 845
15c8650d 846 /* Remove stmts not in the PARTITION bitmap. */
801c5610 847 bbs = get_loop_body_in_dom_order (loop);
848
c64f38bf 849 if (MAY_HAVE_DEBUG_BIND_STMTS)
15c8650d 850 for (i = 0; i < loop->num_nodes; i++)
8ebd8f29 851 {
852 basic_block bb = bbs[i];
853
1a91d914 854 for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
855 gsi_next (&bsi))
15c8650d 856 {
1a91d914 857 gphi *phi = bsi.phi ();
15c8650d 858 if (!virtual_operand_p (gimple_phi_result (phi))
859 && !bitmap_bit_p (partition->stmts, gimple_uid (phi)))
860 reset_debug_uses (phi);
861 }
8ebd8f29 862
1a91d914 863 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
8ebd8f29 864 {
42acab1c 865 gimple *stmt = gsi_stmt (bsi);
8ebd8f29 866 if (gimple_code (stmt) != GIMPLE_LABEL
867 && !is_gimple_debug (stmt)
15c8650d 868 && !bitmap_bit_p (partition->stmts, gimple_uid (stmt)))
8ebd8f29 869 reset_debug_uses (stmt);
870 }
871 }
872
15c8650d 873 for (i = 0; i < loop->num_nodes; i++)
801c5610 874 {
875 basic_block bb = bbs[i];
142b4ffb 876 edge inner_exit = NULL;
877
878 if (loop != bb->loop_father)
879 inner_exit = single_exit (bb->loop_father);
801c5610 880
1a91d914 881 for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);)
15c8650d 882 {
1a91d914 883 gphi *phi = bsi.phi ();
15c8650d 884 if (!virtual_operand_p (gimple_phi_result (phi))
885 && !bitmap_bit_p (partition->stmts, gimple_uid (phi)))
ff24e1e2 886 remove_phi_node (&bsi, true);
15c8650d 887 else
888 gsi_next (&bsi);
889 }
801c5610 890
1a91d914 891 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);)
ff24e1e2 892 {
42acab1c 893 gimple *stmt = gsi_stmt (bsi);
8ebd8f29 894 if (gimple_code (stmt) != GIMPLE_LABEL
895 && !is_gimple_debug (stmt)
15c8650d 896 && !bitmap_bit_p (partition->stmts, gimple_uid (stmt)))
ff24e1e2 897 {
142b4ffb 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. */
1a91d914 903 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
f1ce84d9 904 {
142b4ffb 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);
f1ce84d9 909 update_stmt (stmt);
910 }
911 else if (gimple_code (stmt) == GIMPLE_SWITCH)
912 {
1a91d914 913 gswitch *switch_stmt = as_a <gswitch *> (stmt);
f1ce84d9 914 gimple_switch_set_index
1a91d914 915 (switch_stmt, CASE_LOW (gimple_switch_label (switch_stmt, 1)));
f1ce84d9 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 }
ff24e1e2 925 }
f1ce84d9 926 gsi_next (&bsi);
ff24e1e2 927 }
801c5610 928 }
929
930 free (bbs);
801c5610 931}
932
0dc30b0c 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
938static int
939const_with_all_bytes_same (tree val)
940{
941 unsigned char buf[64];
942 int i, len;
943
944 if (integer_zerop (val)
0dc30b0c 945 || (TREE_CODE (val) == CONSTRUCTOR
946 && !TREE_CLOBBER_P (val)
947 && CONSTRUCTOR_NELTS (val) == 0))
948 return 0;
949
53e7aca1 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:
0a2b1323 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 }
53e7aca1 977 default:
978 break;
979 }
980 }
981
0dc30b0c 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
d32bc1d7 994/* Generate a call to memset for PARTITION in LOOP. */
801c5610 995
a136cad6 996static void
2e966e2a 997generate_memset_builtin (class loop *loop, partition *partition)
801c5610 998{
d32bc1d7 999 gimple_stmt_iterator gsi;
f59ae127 1000 tree mem, fn, nb_bytes;
0644fcba 1001 tree val;
506fcb4f 1002 struct builtin_info *builtin = partition->builtin;
1003 gimple *fn_call;
d32bc1d7 1004
1005 /* The new statements will be placed before LOOP. */
1006 gsi = gsi_last_bb (loop_preheader_edge (loop)->src);
801c5610 1007
bc6e884f 1008 nb_bytes = rewrite_to_non_trapping_overflow (builtin->size);
f689d33d 1009 nb_bytes = force_gimple_operand_gsi (&gsi, nb_bytes, true, NULL_TREE,
1010 false, GSI_CONTINUE_LINKING);
506fcb4f 1011 mem = builtin->dst_base;
f689d33d 1012 mem = force_gimple_operand_gsi (&gsi, mem, true, NULL_TREE,
1013 false, GSI_CONTINUE_LINKING);
801c5610 1014
0644fcba 1015 /* This exactly matches the pattern recognition in classify_partition. */
506fcb4f 1016 val = gimple_assign_rhs1 (DR_STMT (builtin->dst_dr));
0dc30b0c 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)))
0644fcba 1025 {
f9e245b2 1026 tree tem = make_ssa_name (integer_type_node);
42acab1c 1027 gimple *cstmt = gimple_build_assign (tem, NOP_EXPR, val);
0dc30b0c 1028 gsi_insert_after (&gsi, cstmt, GSI_CONTINUE_LINKING);
1029 val = tem;
0644fcba 1030 }
1031
b9a16870 1032 fn = build_fold_addr_expr (builtin_decl_implicit (BUILT_IN_MEMSET));
0644fcba 1033 fn_call = gimple_build_call (fn, 3, mem, val, nb_bytes);
e68b4984 1034 gimple_set_location (fn_call, partition->loc);
f689d33d 1035 gsi_insert_after (&gsi, fn_call, GSI_CONTINUE_LINKING);
98125d0d 1036 fold_stmt (&gsi);
801c5610 1037
1038 if (dump_file && (dump_flags & TDF_DETAILS))
0644fcba 1039 {
1040 fprintf (dump_file, "generated memset");
0dc30b0c 1041 if (bytev == 0)
0644fcba 1042 fprintf (dump_file, " zero\n");
0644fcba 1043 else
1044 fprintf (dump_file, "\n");
1045 }
801c5610 1046}
1047
f689d33d 1048/* Generate a call to memcpy for PARTITION in LOOP. */
1049
1050static void
2e966e2a 1051generate_memcpy_builtin (class loop *loop, partition *partition)
f689d33d 1052{
1053 gimple_stmt_iterator gsi;
506fcb4f 1054 gimple *fn_call;
f59ae127 1055 tree dest, src, fn, nb_bytes;
f689d33d 1056 enum built_in_function kind;
506fcb4f 1057 struct builtin_info *builtin = partition->builtin;
f689d33d 1058
1059 /* The new statements will be placed before LOOP. */
1060 gsi = gsi_last_bb (loop_preheader_edge (loop)->src);
1061
bc6e884f 1062 nb_bytes = rewrite_to_non_trapping_overflow (builtin->size);
f689d33d 1063 nb_bytes = force_gimple_operand_gsi (&gsi, nb_bytes, true, NULL_TREE,
1064 false, GSI_CONTINUE_LINKING);
506fcb4f 1065 dest = builtin->dst_base;
1066 src = builtin->src_base;
5d4e2409 1067 if (partition->kind == PKIND_MEMCPY
1068 || ! ptr_derefs_may_alias_p (dest, src))
f689d33d 1069 kind = BUILT_IN_MEMCPY;
5d4e2409 1070 else
1071 kind = BUILT_IN_MEMMOVE;
f689d33d 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);
e68b4984 1079 gimple_set_location (fn_call, partition->loc);
f689d33d 1080 gsi_insert_after (&gsi, fn_call, GSI_CONTINUE_LINKING);
98125d0d 1081 fold_stmt (&gsi);
f689d33d 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
d32bc1d7 1092/* Remove and destroy the loop LOOP. */
801c5610 1093
d32bc1d7 1094static void
2e966e2a 1095destroy_loop (class loop *loop)
801c5610 1096{
d32bc1d7 1097 unsigned nbbs = loop->num_nodes;
1098 edge exit = single_exit (loop);
1099 basic_block src = loop_preheader_edge (loop)->src, dest = exit->dest;
801c5610 1100 basic_block *bbs;
d32bc1d7 1101 unsigned i;
801c5610 1102
1103 bbs = get_loop_body_in_dom_order (loop);
1104
969b7ab6 1105 gimple_stmt_iterator dst_gsi = gsi_after_labels (exit->dest);
1106 bool safe_p = single_pred_p (exit->dest);
aac7d99e 1107 for (unsigned i = 0; i < nbbs; ++i)
54459dd6 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. */
1a91d914 1114 for (gphi_iterator gsi = gsi_start_phis (bbs[i]); !gsi_end_p (gsi);
1115 gsi_next (&gsi))
54459dd6 1116 {
1a91d914 1117 gphi *phi = gsi.phi ();
7c782c9b 1118 if (virtual_operand_p (gimple_phi_result (phi)))
54459dd6 1119 mark_virtual_phi_result_for_renaming (phi);
1120 }
969b7ab6 1121 for (gimple_stmt_iterator gsi = gsi_start_bb (bbs[i]); !gsi_end_p (gsi);)
54459dd6 1122 {
42acab1c 1123 gimple *stmt = gsi_stmt (gsi);
54459dd6 1124 tree vdef = gimple_vdef (stmt);
1125 if (vdef && TREE_CODE (vdef) == SSA_NAME)
1126 mark_virtual_operand_for_renaming (vdef);
969b7ab6 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);
54459dd6 1146 }
969b7ab6 1147 }
969b7ab6 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;
54459dd6 1159 delete_basic_block (bbs[i]);
1160 }
2084e279 1161 while (i != 0);
1162
801c5610 1163 free (bbs);
d32bc1d7 1164
1165 set_immediate_dominator (CDI_DOMINATORS, dest,
1166 recompute_dominator (CDI_DOMINATORS, dest));
801c5610 1167}
1168
77d095c0 1169/* Generates code for PARTITION. Return whether LOOP needs to be destroyed. */
801c5610 1170
77d095c0 1171static bool
2e966e2a 1172generate_code_for_partition (class loop *loop,
04009ada 1173 partition *partition, bool copy_p)
801c5610 1174{
d32bc1d7 1175 switch (partition->kind)
1176 {
df9892ff 1177 case PKIND_NORMAL:
b1c062d8 1178 case PKIND_PARTIAL_MEMSET:
df9892ff 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);
77d095c0 1183 return false;
df9892ff 1184
d32bc1d7 1185 case PKIND_MEMSET:
f689d33d 1186 generate_memset_builtin (loop, partition);
f689d33d 1187 break;
1188
1189 case PKIND_MEMCPY:
5d4e2409 1190 case PKIND_MEMMOVE:
f689d33d 1191 generate_memcpy_builtin (loop, partition);
d32bc1d7 1192 break;
1193
1194 default:
1195 gcc_unreachable ();
1196 }
801c5610 1197
df9892ff 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)
77d095c0 1201 return true;
1202 return false;
801c5610 1203}
1204
50f5937e 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
1210static data_dependence_relation *
1211get_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;
bbb229ef 1221 slot = ddrs_table->find_slot (&ent, INSERT);
50f5937e 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}
801c5610 1231
f024aa04 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
1236static bool
1237data_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
1271static void
1272update_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
b223e75a 1301/* Returns a partition with all the statements needed for computing
1302 the vertex V of the RDG, also including the loop exit conditions. */
801c5610 1303
04009ada 1304static partition *
b223e75a 1305build_rdg_partition_for_vertex (struct graph *rdg, int v)
801c5610 1306{
889a3926 1307 partition *partition = partition_alloc ();
4997014d 1308 auto_vec<int, 3> nodes;
889a3926 1309 unsigned i, j;
801c5610 1310 int x;
889a3926 1311 data_reference_p dr;
801c5610 1312
5529df60 1313 graphds_dfs (rdg, &v, 1, &nodes, false, NULL);
801c5610 1314
f1f41a6c 1315 FOR_EACH_VEC_ELT (nodes, i, x)
b223e75a 1316 {
1317 bitmap_set_bit (partition->stmts, x);
889a3926 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
f024aa04 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
889a3926 1330 bitmap_set_bit (partition->datarefs, idx);
1331 }
b223e75a 1332 }
801c5610 1333
f024aa04 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
801c5610 1341 return partition;
1342}
1343
b363c31b 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
506fcb4f 1346 data references. */
a136cad6 1347
506fcb4f 1348static bool
2e966e2a 1349find_single_drs (class loop *loop, struct graph *rdg, partition *partition,
506fcb4f 1350 data_reference_p *dst_dr, data_reference_p *src_dr)
a136cad6 1351{
d32bc1d7 1352 unsigned i;
506fcb4f 1353 data_reference_p single_ld = NULL, single_st = NULL;
1354 bitmap_iterator bi;
ac7a1007 1355
d32bc1d7 1356 EXECUTE_IF_SET_IN_BITMAP (partition->stmts, 0, i, bi)
1357 {
42acab1c 1358 gimple *stmt = RDG_STMT (rdg, i);
f689d33d 1359 data_reference_p dr;
d32bc1d7 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
f689d33d 1368 /* Otherwise just regular loads/stores. */
1369 if (!gimple_assign_single_p (stmt))
506fcb4f 1370 return false;
f689d33d 1371
1372 /* But exactly one store and/or load. */
506fcb4f 1373 for (unsigned j = 0; RDG_DATAREFS (rdg, i).iterate (j, &dr); ++j)
d32bc1d7 1374 {
c8a8b2cf 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)))
506fcb4f 1380 return false;
c8a8b2cf 1381
f689d33d 1382 if (DR_IS_READ (dr))
1383 {
506fcb4f 1384 if (single_ld != NULL)
1385 return false;
1386 single_ld = dr;
f689d33d 1387 }
1388 else
1389 {
506fcb4f 1390 if (single_st != NULL)
1391 return false;
1392 single_st = dr;
f689d33d 1393 }
d32bc1d7 1394 }
d32bc1d7 1395 }
1396
506fcb4f 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;
f59ae127 1404
b363c31b 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. */
506fcb4f 1409 basic_block bb_st = gimple_bb (DR_STMT (single_st));
b363c31b 1410 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb_st))
506fcb4f 1411 return false;
1412
1413 if (single_ld)
883b4905 1414 {
506fcb4f 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;
883b4905 1420
506fcb4f 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));
b363c31b 1428 if (bb_st->loop_father != bb_ld->loop_father)
506fcb4f 1429 return false;
1430
b363c31b 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))
506fcb4f 1435 return false;
1436
b363c31b 1437 edge e = single_exit (bb_st->loop_father);
506fcb4f 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
b1c062d8 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:
506fcb4f 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
b1c062d8 1459 arr[i][j - 1][k] = 0; ;base = &arr, size = 4000000
506fcb4f 1460
b1c062d8 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
1466static int
506fcb4f 1467compute_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));
2e966e2a 1472 class loop *loop = bb->loop_father;
506fcb4f 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));
b1c062d8 1476 int res = 0;
506fcb4f 1477
1478 do {
1479 tree scev_fn = analyze_scalar_evolution (loop, access_base);
1480 if (TREE_CODE (scev_fn) != POLYNOMIAL_CHREC)
b1c062d8 1481 return res;
883b4905 1482
506fcb4f 1483 access_base = CHREC_LEFT (scev_fn);
1484 if (tree_contains_chrecs (access_base, NULL))
b1c062d8 1485 return res;
506fcb4f 1486
1487 tree scev_step = CHREC_RIGHT (scev_fn);
1488 /* Only support constant steps. */
1489 if (TREE_CODE (scev_step) != INTEGER_CST)
b1c062d8 1490 return res;
506fcb4f 1491
1492 enum ev_direction access_dir = scev_direction (scev_fn);
1493 if (access_dir == EV_DIR_UNKNOWN)
b1c062d8 1494 return res;
506fcb4f 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))
b1c062d8 1507 return res;
1508
1509 /* Access stride can be computed for data reference at least for the
1510 innermost loop. */
1511 res = 1;
506fcb4f 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)
883b4905 1524 {
506fcb4f 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);
883b4905 1528 }
506fcb4f 1529 } while (loop != loop_nest && (loop = loop_outer (loop)) != NULL);
1530
1531 *base = access_base;
1532 *size = access_size;
b1c062d8 1533 /* Access stride can be computed for data reference at each level loop. */
1534 return 2;
506fcb4f 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
1540static struct builtin_info *
1541alloc_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
1556static void
1557classify_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
b1c062d8 1573 int res = compute_access_range (loop, dr, &base, &size);
1574 if (res == 0)
506fcb4f 1575 return;
b1c062d8 1576 if (res == 1)
1577 {
1578 partition->kind = PKIND_PARTIAL_MEMSET;
1579 return;
1580 }
506fcb4f 1581
caf8edd6 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
05ebeee6 1588 struct builtin_info *builtin;
1589 builtin = alloc_builtin (dr, NULL, base, NULL_TREE, size);
caf8edd6 1590 builtin->dst_base_base = base_base;
1591 builtin->dst_base_offset = const_base_offset;
05ebeee6 1592 partition->builtin = builtin;
506fcb4f 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
1599static void
1600classify_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
b1c062d8 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))
506fcb4f 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;
883b4905 1635 }
1636
506fcb4f 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;
f59ae127 1642
506fcb4f 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)
f689d33d 1647 {
506fcb4f 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))
f689d33d 1651 return;
f689d33d 1652 }
506fcb4f 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
2ab89918 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. */
506fcb4f 1664
2ab89918 1665static bool
506fcb4f 1666classify_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)
f689d33d 1675 {
506fcb4f 1676 gimple *stmt = RDG_STMT (rdg, i);
50f5937e 1677
506fcb4f 1678 if (gimple_has_volatile_ops (stmt))
1679 volatiles_p = true;
50f5937e 1680
506fcb4f 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))
2ab89918 1693 partition->reduction_p = true;
1694 else
1695 has_reduction = true;
7b6f8db4 1696 }
f689d33d 1697 }
506fcb4f 1698
2ab89918 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
506fcb4f 1706 /* Perform general partition disqualification for builtins. */
1707 if (volatiles_p
506fcb4f 1708 || !flag_tree_loop_distribute_patterns)
2ab89918 1709 return has_reduction;
506fcb4f 1710
1711 /* Find single load/store data references for builtin partition. */
b363c31b 1712 if (!find_single_drs (loop, rdg, partition, &single_st, &single_ld))
2ab89918 1713 return has_reduction;
506fcb4f 1714
e68b4984 1715 partition->loc = gimple_location (DR_STMT (single_st));
1716
506fcb4f 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);
2ab89918 1722 return has_reduction;
a136cad6 1723}
1724
fd34627b 1725/* Returns true when PARTITION1 and PARTITION2 access the same memory
1726 object in RDG. */
a136cad6 1727
1728static bool
fd34627b 1729share_memory_accesses (struct graph *rdg,
1730 partition *partition1, partition *partition2)
a136cad6 1731{
fd34627b 1732 unsigned i, j;
a136cad6 1733 bitmap_iterator bi, bj;
fd34627b 1734 data_reference_p dr1, dr2;
f83623cc 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;
a136cad6 1743
fd34627b 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 }
a136cad6 1768
1769 return false;
1770}
1771
8d95fe31 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. */
801c5610 1775
1776static void
88e0cdd9 1777rdg_build_partitions (struct graph *rdg,
42acab1c 1778 vec<gimple *> starting_stmts,
04009ada 1779 vec<partition *> *partitions)
801c5610 1780{
035def86 1781 auto_bitmap processed;
15c8650d 1782 int i;
42acab1c 1783 gimple *stmt;
801c5610 1784
15c8650d 1785 FOR_EACH_VEC_ELT (starting_stmts, i, stmt)
801c5610 1786 {
15c8650d 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);
48e1416a 1792
b223e75a 1793 /* If the vertex is already contained in another partition so
1794 is the partition rooted at it. */
801c5610 1795 if (bitmap_bit_p (processed, v))
1796 continue;
48e1416a 1797
04009ada 1798 partition *partition = build_rdg_partition_for_vertex (rdg, v);
b223e75a 1799 bitmap_ior_into (processed, partition->stmts);
801c5610 1800
df9892ff 1801 if (dump_file && (dump_flags & TDF_DETAILS))
801c5610 1802 {
f024aa04 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");
801c5610 1806 }
df9892ff 1807
1808 partitions->safe_push (partition);
801c5610 1809 }
1810
88e0cdd9 1811 /* All vertices should have been assigned to at least one partition now,
1812 other than vertices belonging to dead code. */
801c5610 1813}
1814
1815/* Dump to FILE the PARTITIONS. */
1816
1817static void
04009ada 1818dump_rdg_partitions (FILE *file, vec<partition *> partitions)
801c5610 1819{
1820 int i;
04009ada 1821 partition *partition;
801c5610 1822
f1f41a6c 1823 FOR_EACH_VEC_ELT (partitions, i, partition)
543506e0 1824 debug_bitmap_file (file, partition->stmts);
801c5610 1825}
1826
1827/* Debug PARTITIONS. */
04009ada 1828extern void debug_rdg_partitions (vec<partition *> );
801c5610 1829
4b987fac 1830DEBUG_FUNCTION void
04009ada 1831debug_rdg_partitions (vec<partition *> partitions)
801c5610 1832{
1833 dump_rdg_partitions (stderr, partitions);
1834}
1835
577982d8 1836/* Returns the number of read and write operations in the RDG. */
1837
1838static int
1839number_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
1858static int
04009ada 1859number_of_rw_in_partition (struct graph *rdg, partition *partition)
577982d8 1860{
1861 int res = 0;
1862 unsigned i;
1863 bitmap_iterator ii;
1864
543506e0 1865 EXECUTE_IF_SET_IN_BITMAP (partition->stmts, 0, i, ii)
577982d8 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
1880static bool
f1f41a6c 1881partition_contains_all_rw (struct graph *rdg,
04009ada 1882 vec<partition *> partitions)
577982d8 1883{
1884 int i;
04009ada 1885 partition *partition;
577982d8 1886 int nrw = number_of_rw_in_rdg (rdg);
1887
f1f41a6c 1888 FOR_EACH_VEC_ELT (partitions, i, partition)
577982d8 1889 if (nrw == number_of_rw_in_partition (rdg, partition))
1890 return true;
1891
1892 return false;
1893}
1894
7103facc 1895/* Compute partition dependence created by the data references in DRS1
f562e2ea 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. */
7103facc 1900
1901static int
209a62a6 1902pg_add_dependence_edges (struct graph *rdg, int dir,
f562e2ea 1903 bitmap drs1, bitmap drs2, vec<ddr_p> *alias_ddrs)
7103facc 1904{
889a3926 1905 unsigned i, j;
1906 bitmap_iterator bi, bj;
1907 data_reference_p dr1, dr2, saved_dr1;
7103facc 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). */
889a3926 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 {
f562e2ea 1917 int res, this_dir = 1;
1918 ddr_p ddr;
1919
889a3926 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;
f562e2ea 1927 /* Re-shuffle data-refs to be in topological order. */
889a3926 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 }
50f5937e 1934 ddr = get_data_dependence (rdg, dr1, dr2);
889a3926 1935 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
f562e2ea 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.
f4d3c071 1943 In other words, the dependence cannot be resolved by
f562e2ea 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 }
889a3926 1958 else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
1959 {
1960 if (DDR_REVERSED_P (ddr))
f562e2ea 1961 this_dir = -this_dir;
1962
889a3926 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. */
21e5a1b6 1968 else if (lambda_vector_zerop (DDR_DIST_VECT (ddr, 0),
1969 DDR_NB_LOOPS (ddr)))
889a3926 1970 ;
f562e2ea 1971 /* Else as the distance vector is lexicographic positive swap
1972 the dependence direction. */
889a3926 1973 else
f562e2ea 1974 this_dir = -this_dir;
889a3926 1975 }
1976 else
1977 this_dir = 0;
889a3926 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 }
7103facc 1988 return dir;
1989}
1990
1991/* Compare postorder number of the partition graph vertices V1 and V2. */
1992
1993static int
1994pgcmp (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}
15c8650d 2000
f562e2ea 2001/* Data attached to vertices of partition dependence graph. */
2002struct 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. */
2011struct 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. */
2022struct 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
2036static void
2037init_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
2056static void
2057add_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
2077static bool
2078pg_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
2086static void
2087free_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
2099static void
2100free_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
2118static struct graph *
2119build_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
f4d3c071 2156 time known dependence, this type cannot be resolved by runtime
f562e2ea 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
cb072485 2182/* Sort partitions in PG in descending post order and store them in
2183 PARTITIONS. */
f562e2ea 2184
2185static void
2186sort_partitions_by_post_order (struct graph *pg,
2187 vec<struct partition *> *partitions)
2188{
2189 int i;
2190 struct pg_vdata *data;
2191
cb072485 2192 /* Now order the remaining nodes in descending postorder. */
f562e2ea 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
883b4905 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. */
f562e2ea 2207
2208static void
2209merge_dep_scc_partitions (struct graph *rdg,
883b4905 2210 vec<struct partition *> *partitions,
2211 bool ignore_alias_p)
f562e2ea 2212{
2213 struct partition *partition1, *partition2;
2214 struct pg_vdata *data;
883b4905 2215 graph *pg = build_partition_graph (rdg, partitions, ignore_alias_p);
f562e2ea 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 }
f562e2ea 2239 }
1df7b42b 2240
2241 sort_partitions_by_post_order (pg, partitions);
f562e2ea 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
2250static void
2251pg_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
2285static void
2286break_alias_scc_partitions (struct graph *rdg,
2287 vec<struct partition *> *partitions,
2288 vec<ddr_p> *alias_ddrs)
2289{
cb072485 2290 int i, j, k, num_sccs, num_sccs_no_alias;
f562e2ea 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
85676b62 2307 /* If all partitions in a SCC have the same type, we can simply merge the
f562e2ea 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;
a1e9c80f 2315
2316 bool same_type = true, all_builtins = partition_builtin_p (first);
f562e2ea 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 {
a1e9c80f 2324 same_type = false;
f562e2ea 2325 break;
2326 }
a1e9c80f 2327 all_builtins &= partition_builtin_p (partition);
f562e2ea 2328 }
a1e9c80f 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);
f562e2ea 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
85676b62 2350 skipped. This is to topologically sort partitions according to
f562e2ea 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;
cb072485 2372 for (k = j + 1; partitions->iterate (k, &partition); ++k)
f562e2ea 2373 {
2374 struct pg_vdata *data;
2375
cb072485 2376 if (cbdata.vertices_component[k] != i)
f562e2ea 2377 continue;
2378
cb072485 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 }
f562e2ea 2387 partition_merge_into (NULL, first, partition, FUSE_SAME_SCC);
cb072485 2388 (*partitions)[k] = NULL;
f562e2ea 2389 partition_free (partition);
cb072485 2390 data = (struct pg_vdata *)pg->vertices[k].data;
2391 gcc_assert (data->id == k);
f562e2ea 2392 data->partition = NULL;
85676b62 2393 /* The result partition of merged SCC must be sequential. */
2394 first->type = PTYPE_SEQUENTIAL;
f562e2ea 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
2414static tree
2415data_ref_segment_size (struct data_reference *dr, tree niters)
2416{
e85b4a5e 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));
f562e2ea 2423}
2424
2425/* Return true if LOOP's latch is dominated by statement for data reference
2426 DR. */
2427
2428static inline bool
2e966e2a 2429latch_dominated_by_data_ref (class loop *loop, data_reference *dr)
f562e2ea 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
2438static void
2e966e2a 2439compute_alias_check_pairs (class loop *loop, vec<ddr_p> *alias_ddrs,
f562e2ea 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
e85b4a5e 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
f562e2ea 2484 dr_with_seg_len_pair_t dr_with_seg_len_pair
e85b4a5e 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));
f562e2ea 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
2509static void
a1e9c80f 2510version_loop_by_alias_check (vec<struct partition *> *partitions,
2e966e2a 2511 class loop *loop, vec<ddr_p> *alias_ddrs)
f562e2ea 2512{
2513 profile_probability prob;
2514 basic_block cond_bb;
2e966e2a 2515 class loop *nloop;
f562e2ea 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,
b48c230a 2531 is_gimple_val, NULL_TREE);
f562e2ea 2532
2533 /* Depend on vectorizer to fold IFN_LOOP_DIST_ALIAS. */
a1e9c80f 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)
f562e2ea 2552 {
f562e2ea 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
2594static inline bool
2595version_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
05ebeee6 2606/* Compare base offset of builtin mem* partitions P1 and P2. */
2607
207c43a2 2608static int
2609offset_cmp (const void *vp1, const void *vp2)
05ebeee6 2610{
207c43a2 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);
05ebeee6 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
2635static void
2636fuse_memset_builtins (vec<struct partition *> *partitions)
2637{
2638 unsigned i, j;
2639 struct partition *part1, *part2;
79f9738a 2640 tree rhs1, rhs2;
05ebeee6 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;
79f9738a 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;
05ebeee6 2664 }
2665
2666 /* Stable sort is required in order to avoid breaking dependence. */
207c43a2 2667 gcc_stablesort (&(*partitions)[i], j - i, sizeof (*partitions)[i],
2668 offset_cmp);
05ebeee6 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 }
79f9738a 2695 rhs1 = gimple_assign_rhs1 (DR_STMT (part1->builtin->dst_dr));
2696 rhs2 = gimple_assign_rhs1 (DR_STMT (part2->builtin->dst_dr));
05ebeee6 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
883b4905 2722/* Fuse PARTITIONS of LOOP if necessary before finalizing distribution.
2723 ALIAS_DDRS contains ddrs which need runtime alias check. */
f562e2ea 2724
2725static void
2e966e2a 2726finalize_partitions (class loop *loop, vec<struct partition *> *partitions,
f562e2ea 2727 vec<ddr_p> *alias_ddrs)
2728{
2729 unsigned i;
883b4905 2730 struct partition *partition, *a;
f562e2ea 2731
2732 if (partitions->length () == 1
2733 || alias_ddrs->length () > 0)
2734 return;
2735
b1c062d8 2736 unsigned num_builtin = 0, num_normal = 0, num_partial_memset = 0;
883b4905 2737 bool same_type_p = true;
2738 enum partition_type type = ((*partitions)[0])->type;
2739 for (i = 0; partitions->iterate (i, &partition); ++i)
f562e2ea 2740 {
883b4905 2741 same_type_p &= (type == partition->type);
b1c062d8 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++;
f562e2ea 2750 }
2751
883b4905 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. */
b1c062d8 2755 if ((same_type_p && num_builtin == 0
2756 && (loop->inner == NULL || num_normal != 2 || num_partial_memset != 1))
883b4905 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))
f562e2ea 2761 {
883b4905 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);
f562e2ea 2769 }
05ebeee6 2770
2771 /* Fuse memset builtins if possible. */
2772 if (partitions->length () > 1)
2773 fuse_memset_builtins (partitions);
f562e2ea 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. */
801c5610 2781
2782static int
2e966e2a 2783distribute_loop (class loop *loop, vec<gimple *> stmts,
98125d0d 2784 control_dependences *cd, int *nb_calls, bool *destroy_p,
2785 bool only_patterns_p)
801c5610 2786{
bbb229ef 2787 ddrs_table = new hash_table<ddr_hasher> (389);
15c8650d 2788 struct graph *rdg;
04009ada 2789 partition *partition;
15c8650d 2790 int i, nbp;
801c5610 2791
303e9267 2792 *destroy_p = false;
df9892ff 2793 *nb_calls = 0;
209a62a6 2794 loop_nest.create (0);
15c8650d 2795 if (!find_loop_nest (loop, &loop_nest))
209a62a6 2796 {
2797 loop_nest.release ();
bbb229ef 2798 delete ddrs_table;
209a62a6 2799 return 0;
2800 }
15c8650d 2801
f3754041 2802 datarefs_vec.create (20);
6079e9be 2803 has_nonaddressable_dataref_p = false;
209a62a6 2804 rdg = build_rdg (loop, cd);
15c8650d 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
209a62a6 2812 loop_nest.release ();
f3754041 2813 free_data_refs (datarefs_vec);
bbb229ef 2814 delete ddrs_table;
f3754041 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);
bbb229ef 2828 delete ddrs_table;
15c8650d 2829 return 0;
2830 }
2831
f3754041 2832 data_reference_p dref;
2833 for (i = 0; datarefs_vec.iterate (i, &dref); ++i)
2834 dref->aux = (void *) (uintptr_t) i;
2835
15c8650d 2836 if (dump_file && (dump_flags & TDF_DETAILS))
2837 dump_rdg (dump_file, rdg);
2838
04009ada 2839 auto_vec<struct partition *, 3> partitions;
15c8650d 2840 rdg_build_partitions (rdg, stmts, &partitions);
801c5610 2841
f562e2ea 2842 auto_vec<ddr_p> alias_ddrs;
2843
8d95fe31 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
2ab89918 2849 bool any_builtin = false;
2850 bool reduction_in_all = false;
f1f41a6c 2851 FOR_EACH_VEC_ELT (partitions, i, partition)
6198d968 2852 {
2ab89918 2853 reduction_in_all
2854 |= classify_partition (loop, rdg, partition, stmt_in_all_partitions);
6198d968 2855 any_builtin |= partition_builtin_p (partition);
2856 }
d32bc1d7 2857
7103facc 2858 /* If we are only distributing patterns but did not detect any,
2859 simply bail out. */
98125d0d 2860 if (only_patterns_p
0c58611e 2861 && !any_builtin)
2862 {
2863 nbp = 0;
2864 goto ldist_done;
2865 }
2866
7103facc 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. */
04009ada 2871 struct partition *into;
98125d0d 2872 if (only_patterns_p)
7103facc 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 {
f024aa04 2880 partition_merge_into (NULL, into, partition, FUSE_NON_BUILTIN);
7103facc 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 {
f024aa04 2896 partition_merge_into (rdg, into, partition, FUSE_REDUCTION);
7103facc 2897 partitions.unordered_remove (i);
2898 partition_free (partition);
2899 i--;
2900 }
2901
0c58611e 2902 /* Apply our simple cost model - fuse partitions with similar
2903 memory accesses. */
0c58611e 2904 for (i = 0; partitions.iterate (i, &into); ++i)
2905 {
facf7bb7 2906 bool changed = false;
b1c062d8 2907 if (partition_builtin_p (into) || into->kind == PKIND_PARTIAL_MEMSET)
0c58611e 2908 continue;
2909 for (int j = i + 1;
2910 partitions.iterate (j, &partition); ++j)
2911 {
fd34627b 2912 if (share_memory_accesses (rdg, into, partition))
0c58611e 2913 {
f024aa04 2914 partition_merge_into (rdg, into, partition, FUSE_SHARE_REF);
7103facc 2915 partitions.unordered_remove (j);
0c58611e 2916 partition_free (partition);
2917 j--;
facf7bb7 2918 changed = true;
0c58611e 2919 }
2920 }
facf7bb7 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--;
0c58611e 2927 }
2928
2ab89918 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
883b4905 2944 /* Build the partition dependency graph and fuse partitions in strong
2945 connected component. */
7103facc 2946 if (partitions.length () > 1)
54459dd6 2947 {
883b4905 2948 /* Don't support loop nest distribution under runtime alias check
6079e9be 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)
883b4905 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 }
ac7a1007 2960 }
2961
883b4905 2962 finalize_partitions (loop, &partitions, &alias_ddrs);
f562e2ea 2963
2ab89918 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
f1f41a6c 2979 nbp = partitions.length ();
58ccfbea 2980 if (nbp == 0
f1f41a6c 2981 || (nbp == 1 && !partition_builtin_p (partitions[0]))
2982 || (nbp > 1 && partition_contains_all_rw (rdg, partitions)))
54459dd6 2983 {
2984 nbp = 0;
2985 goto ldist_done;
2986 }
801c5610 2987
f562e2ea 2988 if (version_for_distribution_p (&partitions, &alias_ddrs))
a1e9c80f 2989 version_loop_by_alias_check (&partitions, loop, &alias_ddrs);
f562e2ea 2990
801c5610 2991 if (dump_file && (dump_flags & TDF_DETAILS))
f562e2ea 2992 {
2993 fprintf (dump_file,
2994 "distribute loop <%d> into partitions:\n", loop->num);
2995 dump_rdg_partitions (dump_file, partitions);
2996 }
801c5610 2997
f1f41a6c 2998 FOR_EACH_VEC_ELT (partitions, i, partition)
df9892ff 2999 {
3000 if (partition_builtin_p (partition))
3001 (*nb_calls)++;
77d095c0 3002 *destroy_p |= generate_code_for_partition (loop, partition, i < nbp - 1);
df9892ff 3003 }
801c5610 3004
801c5610 3005 ldist_done:
209a62a6 3006 loop_nest.release ();
f3754041 3007 free_data_refs (datarefs_vec);
bbb229ef 3008 for (hash_table<ddr_hasher>::iterator iter = ddrs_table->begin ();
3009 iter != ddrs_table->end (); ++iter)
50f5937e 3010 {
3011 free_dependence_relation (*iter);
3012 *iter = NULL;
3013 }
bbb229ef 3014 delete ddrs_table;
801c5610 3015
f1f41a6c 3016 FOR_EACH_VEC_ELT (partitions, i, partition)
543506e0 3017 partition_free (partition);
801c5610 3018
801c5610 3019 free_rdg (rdg);
df9892ff 3020 return nbp - *nb_calls;
801c5610 3021}
3022
3023/* Distribute all loops in the current function. */
3024
65b0537f 3025namespace {
3026
3027const pass_data pass_data_loop_distribution =
3028{
3029 GIMPLE_PASS, /* type */
3030 "ldist", /* name */
3031 OPTGROUP_LOOP, /* optinfo_flags */
65b0537f 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 */
8b88439e 3037 0, /* todo_flags_finish */
65b0537f 3038};
3039
3040class pass_loop_distribution : public gimple_opt_pass
3041{
3042public:
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
883b4905 3058
3059/* Given LOOP, this function records seed statements for distribution in
3060 WORK_LIST. Return false if there is nothing for distribution. */
3061
3062static bool
2e966e2a 3063find_seed_stmts_for_distribution (class loop *loop, vec<gimple *> *work_list)
883b4905 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
39730b97 3087 /* Ignore clobbers, they do not have true side effects. */
3088 if (gimple_clobber_p (stmt))
3089 continue;
3090
883b4905 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
2e966e2a 3117static class loop *
3118prepare_perfect_loop_nest (class loop *loop)
883b4905 3119{
2e966e2a 3120 class loop *outer = loop_outer (loop);
883b4905 3121 tree niters = number_of_latch_executions (loop);
3122
b1c062d8 3123 /* TODO: We only support the innermost 3-level loop nest distribution
883b4905 3124 because of compilation time issue for now. This should be relaxed
b1c062d8 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))
883b4905 3129 && loop_outer (outer)
3130 && outer->inner == loop && loop->next == NULL
3131 && single_exit (outer)
883b4905 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
65b0537f 3143unsigned int
3144pass_loop_distribution::execute (function *fun)
801c5610 3145{
2e966e2a 3146 class loop *loop;
54459dd6 3147 bool changed = false;
f83623cc 3148 basic_block bb;
f1ce84d9 3149 control_dependences *cd = NULL;
77d095c0 3150 auto_vec<loop_p> loops_to_be_destroyed;
f83623cc 3151
c4b1b865 3152 if (number_of_loops (fun) <= 1)
3153 return 0;
3154
50eda3a8 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 {
5c49e6ea 3160 int rpo_num;
50eda3a8 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));
5c49e6ea 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++)
50eda3a8 3167 bb_top_order_index[rpo[i]] = i;
3168
3169 free (rpo);
3170 }
3171
65b0537f 3172 FOR_ALL_BB_FN (bb, fun)
f83623cc 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 }
801c5610 3180
54459dd6 3181 /* We can at the moment only distribute non-nested loops, thus restrict
3182 walking to innermost loops. */
f21d4d00 3183 FOR_EACH_LOOP (loop, LI_ONLY_INNERMOST)
801c5610 3184 {
98125d0d 3185 /* Don't distribute multiple exit edges loop, or cold loop when
3186 not doing pattern detection. */
883b4905 3187 if (!single_exit (loop)
98125d0d 3188 || (!flag_tree_loop_distribute_patterns
3189 && !optimize_loop_for_speed_p (loop)))
5f38d9ef 3190 continue;
3191
1c4ee769 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
883b4905 3197 /* Get the perfect loop nest for distribution. */
3198 loop = prepare_perfect_loop_nest (loop);
3199 for (; loop; loop = loop->inner)
6198d968 3200 {
883b4905 3201 auto_vec<gimple *> work_list;
3202 if (!find_seed_stmts_for_distribution (loop, &work_list))
3203 break;
54459dd6 3204
883b4905 3205 const char *str = loop->inner ? " nest" : "";
c309657f 3206 dump_user_location_t loc = find_loop_location (loop);
f1ce84d9 3207 if (!cd)
3208 {
73423315 3209 calculate_dominance_info (CDI_DOMINATORS);
f1ce84d9 3210 calculate_dominance_info (CDI_POST_DOMINATORS);
ce143ff0 3211 cd = new control_dependences ();
f1ce84d9 3212 free_dominance_info (CDI_POST_DOMINATORS);
3213 }
883b4905 3214
77d095c0 3215 bool destroy_p;
883b4905 3216 int nb_generated_loops, nb_generated_calls;
98125d0d 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));
77d095c0 3221 if (destroy_p)
3222 loops_to_be_destroyed.safe_push (loop);
54459dd6 3223
883b4905 3224 if (nb_generated_loops + nb_generated_calls > 0)
3225 {
3226 changed = true;
91f42adc 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);
883b4905 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);
801c5610 3238 }
801c5610 3239 }
3240
f1ce84d9 3241 if (cd)
3242 delete cd;
3243
50eda3a8 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
54459dd6 3251 if (changed)
3252 {
ce143ff0 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)
50eda3a8 3257 destroy_loop (loop);
ce143ff0 3258
505d3633 3259 /* Cached scalar evolutions now may refer to wrong or non-existing
3260 loops. */
3261 scev_reset_htab ();
65b0537f 3262 mark_virtual_operands_for_renaming (fun);
54459dd6 3263 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
3264 }
3265
382ecba7 3266 checking_verify_loop_structure ();
54459dd6 3267
ade75d0f 3268 return changed ? TODO_cleanup_cfg : 0;
801c5610 3269}
3270
cbe8bda8 3271} // anon namespace
3272
3273gimple_opt_pass *
3274make_pass_loop_distribution (gcc::context *ctxt)
3275{
3276 return new pass_loop_distribution (ctxt);
3277}