[ARM/AArch64][testsuite] Add vpadd, vpmax and vpmin tests.
[official-gcc.git] / gcc / ddg.c
blobe6966f3823c428d5a59016b0ff309887f911db4b
1 /* DDG - Data Dependence Graph implementation.
2 Copyright (C) 2004-2015 Free Software Foundation, Inc.
3 Contributed by Ayal Zaks and Mustafa Hagog <zaks,mustafa@il.ibm.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "diagnostic-core.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "hard-reg-set.h"
30 #include "regs.h"
31 #include "hashtab.h"
32 #include "hash-set.h"
33 #include "vec.h"
34 #include "machmode.h"
35 #include "input.h"
36 #include "function.h"
37 #include "flags.h"
38 #include "insn-config.h"
39 #include "insn-attr.h"
40 #include "except.h"
41 #include "recog.h"
42 #include "predict.h"
43 #include "basic-block.h"
44 #include "sched-int.h"
45 #include "target.h"
46 #include "cfgloop.h"
47 #include "sbitmap.h"
48 #include "symtab.h"
49 #include "statistics.h"
50 #include "double-int.h"
51 #include "real.h"
52 #include "fixed-value.h"
53 #include "alias.h"
54 #include "wide-int.h"
55 #include "inchash.h"
56 #include "tree.h"
57 #include "expmed.h"
58 #include "dojump.h"
59 #include "explow.h"
60 #include "calls.h"
61 #include "emit-rtl.h"
62 #include "varasm.h"
63 #include "stmt.h"
64 #include "expr.h"
65 #include "bitmap.h"
66 #include "df.h"
67 #include "ddg.h"
68 #include "rtl-iter.h"
70 #ifdef INSN_SCHEDULING
72 /* A flag indicating that a ddg edge belongs to an SCC or not. */
73 enum edge_flag {NOT_IN_SCC = 0, IN_SCC};
75 /* Forward declarations. */
76 static void add_backarc_to_ddg (ddg_ptr, ddg_edge_ptr);
77 static void add_backarc_to_scc (ddg_scc_ptr, ddg_edge_ptr);
78 static void add_scc_to_ddg (ddg_all_sccs_ptr, ddg_scc_ptr);
79 static void create_ddg_dep_from_intra_loop_link (ddg_ptr, ddg_node_ptr,
80 ddg_node_ptr, dep_t);
81 static void create_ddg_dep_no_link (ddg_ptr, ddg_node_ptr, ddg_node_ptr,
82 dep_type, dep_data_type, int);
83 static ddg_edge_ptr create_ddg_edge (ddg_node_ptr, ddg_node_ptr, dep_type,
84 dep_data_type, int, int);
85 static void add_edge_to_ddg (ddg_ptr g, ddg_edge_ptr);
87 /* Auxiliary variable for mem_read_insn_p/mem_write_insn_p. */
88 static bool mem_ref_p;
90 /* Auxiliary function for mem_read_insn_p. */
91 static void
92 mark_mem_use (rtx *x, void *)
94 subrtx_iterator::array_type array;
95 FOR_EACH_SUBRTX (iter, array, *x, NONCONST)
96 if (MEM_P (*iter))
98 mem_ref_p = true;
99 break;
103 /* Returns nonzero if INSN reads from memory. */
104 static bool
105 mem_read_insn_p (rtx_insn *insn)
107 mem_ref_p = false;
108 note_uses (&PATTERN (insn), mark_mem_use, NULL);
109 return mem_ref_p;
112 static void
113 mark_mem_store (rtx loc, const_rtx setter ATTRIBUTE_UNUSED, void *data ATTRIBUTE_UNUSED)
115 if (MEM_P (loc))
116 mem_ref_p = true;
119 /* Returns nonzero if INSN writes to memory. */
120 static bool
121 mem_write_insn_p (rtx_insn *insn)
123 mem_ref_p = false;
124 note_stores (PATTERN (insn), mark_mem_store, NULL);
125 return mem_ref_p;
128 /* Returns nonzero if X has access to memory. */
129 static bool
130 rtx_mem_access_p (rtx x)
132 int i, j;
133 const char *fmt;
134 enum rtx_code code;
136 if (x == 0)
137 return false;
139 if (MEM_P (x))
140 return true;
142 code = GET_CODE (x);
143 fmt = GET_RTX_FORMAT (code);
144 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
146 if (fmt[i] == 'e')
148 if (rtx_mem_access_p (XEXP (x, i)))
149 return true;
151 else if (fmt[i] == 'E')
152 for (j = 0; j < XVECLEN (x, i); j++)
154 if (rtx_mem_access_p (XVECEXP (x, i, j)))
155 return true;
158 return false;
161 /* Returns nonzero if INSN reads to or writes from memory. */
162 static bool
163 mem_access_insn_p (rtx_insn *insn)
165 return rtx_mem_access_p (PATTERN (insn));
168 /* Return true if DEF_INSN contains address being auto-inc or auto-dec
169 which is used in USE_INSN. Otherwise return false. The result is
170 being used to decide whether to remove the edge between def_insn and
171 use_insn when -fmodulo-sched-allow-regmoves is set. This function
172 doesn't need to consider the specific address register; no reg_moves
173 will be allowed for any life range defined by def_insn and used
174 by use_insn, if use_insn uses an address register auto-inc'ed by
175 def_insn. */
176 bool
177 autoinc_var_is_used_p (rtx_insn *def_insn, rtx_insn *use_insn)
179 rtx note;
181 for (note = REG_NOTES (def_insn); note; note = XEXP (note, 1))
182 if (REG_NOTE_KIND (note) == REG_INC
183 && reg_referenced_p (XEXP (note, 0), PATTERN (use_insn)))
184 return true;
186 return false;
189 /* Return true if one of the definitions in INSN has MODE_CC. Otherwise
190 return false. */
191 static bool
192 def_has_ccmode_p (rtx_insn *insn)
194 df_ref def;
196 FOR_EACH_INSN_DEF (def, insn)
198 machine_mode mode = GET_MODE (DF_REF_REG (def));
200 if (GET_MODE_CLASS (mode) == MODE_CC)
201 return true;
204 return false;
207 /* Computes the dependence parameters (latency, distance etc.), creates
208 a ddg_edge and adds it to the given DDG. */
209 static void
210 create_ddg_dep_from_intra_loop_link (ddg_ptr g, ddg_node_ptr src_node,
211 ddg_node_ptr dest_node, dep_t link)
213 ddg_edge_ptr e;
214 int latency, distance = 0;
215 dep_type t = TRUE_DEP;
216 dep_data_type dt = (mem_access_insn_p (src_node->insn)
217 && mem_access_insn_p (dest_node->insn) ? MEM_DEP
218 : REG_DEP);
219 gcc_assert (src_node->cuid < dest_node->cuid);
220 gcc_assert (link);
222 /* Note: REG_DEP_ANTI applies to MEM ANTI_DEP as well!! */
223 if (DEP_TYPE (link) == REG_DEP_ANTI)
224 t = ANTI_DEP;
225 else if (DEP_TYPE (link) == REG_DEP_OUTPUT)
226 t = OUTPUT_DEP;
228 gcc_assert (!DEBUG_INSN_P (dest_node->insn) || t == ANTI_DEP);
229 gcc_assert (!DEBUG_INSN_P (src_node->insn) || t == ANTI_DEP);
231 /* We currently choose not to create certain anti-deps edges and
232 compensate for that by generating reg-moves based on the life-range
233 analysis. The anti-deps that will be deleted are the ones which
234 have true-deps edges in the opposite direction (in other words
235 the kernel has only one def of the relevant register).
236 If the address that is being auto-inc or auto-dec in DEST_NODE
237 is used in SRC_NODE then do not remove the edge to make sure
238 reg-moves will not be created for this address.
239 TODO: support the removal of all anti-deps edges, i.e. including those
240 whose register has multiple defs in the loop. */
241 if (flag_modulo_sched_allow_regmoves
242 && (t == ANTI_DEP && dt == REG_DEP)
243 && !def_has_ccmode_p (dest_node->insn)
244 && !autoinc_var_is_used_p (dest_node->insn, src_node->insn))
246 rtx set;
248 set = single_set (dest_node->insn);
249 /* TODO: Handle registers that REG_P is not true for them, i.e.
250 subregs and special registers. */
251 if (set && REG_P (SET_DEST (set)))
253 int regno = REGNO (SET_DEST (set));
254 df_ref first_def;
255 struct df_rd_bb_info *bb_info = DF_RD_BB_INFO (g->bb);
257 first_def = df_bb_regno_first_def_find (g->bb, regno);
258 gcc_assert (first_def);
260 if (bitmap_bit_p (&bb_info->gen, DF_REF_ID (first_def)))
261 return;
265 latency = dep_cost (link);
266 e = create_ddg_edge (src_node, dest_node, t, dt, latency, distance);
267 add_edge_to_ddg (g, e);
270 /* The same as the above function, but it doesn't require a link parameter. */
271 static void
272 create_ddg_dep_no_link (ddg_ptr g, ddg_node_ptr from, ddg_node_ptr to,
273 dep_type d_t, dep_data_type d_dt, int distance)
275 ddg_edge_ptr e;
276 int l;
277 enum reg_note dep_kind;
278 struct _dep _dep, *dep = &_dep;
280 gcc_assert (!DEBUG_INSN_P (to->insn) || d_t == ANTI_DEP);
281 gcc_assert (!DEBUG_INSN_P (from->insn) || d_t == ANTI_DEP);
283 if (d_t == ANTI_DEP)
284 dep_kind = REG_DEP_ANTI;
285 else if (d_t == OUTPUT_DEP)
286 dep_kind = REG_DEP_OUTPUT;
287 else
289 gcc_assert (d_t == TRUE_DEP);
291 dep_kind = REG_DEP_TRUE;
294 init_dep (dep, from->insn, to->insn, dep_kind);
296 l = dep_cost (dep);
298 e = create_ddg_edge (from, to, d_t, d_dt, l, distance);
299 if (distance > 0)
300 add_backarc_to_ddg (g, e);
301 else
302 add_edge_to_ddg (g, e);
306 /* Given a downwards exposed register def LAST_DEF (which is the last
307 definition of that register in the bb), add inter-loop true dependences
308 to all its uses in the next iteration, an output dependence to the
309 first def of the same register (possibly itself) in the next iteration
310 and anti-dependences from its uses in the current iteration to the
311 first definition in the next iteration. */
312 static void
313 add_cross_iteration_register_deps (ddg_ptr g, df_ref last_def)
315 int regno = DF_REF_REGNO (last_def);
316 struct df_link *r_use;
317 int has_use_in_bb_p = false;
318 rtx_insn *def_insn = DF_REF_INSN (last_def);
319 ddg_node_ptr last_def_node = get_node_of_insn (g, def_insn);
320 ddg_node_ptr use_node;
321 #ifdef ENABLE_CHECKING
322 struct df_rd_bb_info *bb_info = DF_RD_BB_INFO (g->bb);
323 #endif
324 df_ref first_def = df_bb_regno_first_def_find (g->bb, regno);
326 gcc_assert (last_def_node);
327 gcc_assert (first_def);
329 #ifdef ENABLE_CHECKING
330 if (DF_REF_ID (last_def) != DF_REF_ID (first_def))
331 gcc_assert (!bitmap_bit_p (&bb_info->gen,
332 DF_REF_ID (first_def)));
333 #endif
335 /* Create inter-loop true dependences and anti dependences. */
336 for (r_use = DF_REF_CHAIN (last_def); r_use != NULL; r_use = r_use->next)
338 rtx_insn *use_insn = DF_REF_INSN (r_use->ref);
340 if (BLOCK_FOR_INSN (use_insn) != g->bb)
341 continue;
343 /* ??? Do not handle uses with DF_REF_IN_NOTE notes. */
344 use_node = get_node_of_insn (g, use_insn);
345 gcc_assert (use_node);
346 has_use_in_bb_p = true;
347 if (use_node->cuid <= last_def_node->cuid)
349 /* Add true deps from last_def to it's uses in the next
350 iteration. Any such upwards exposed use appears before
351 the last_def def. */
352 create_ddg_dep_no_link (g, last_def_node, use_node,
353 DEBUG_INSN_P (use_insn) ? ANTI_DEP : TRUE_DEP,
354 REG_DEP, 1);
356 else if (!DEBUG_INSN_P (use_insn))
358 /* Add anti deps from last_def's uses in the current iteration
359 to the first def in the next iteration. We do not add ANTI
360 dep when there is an intra-loop TRUE dep in the opposite
361 direction, but use regmoves to fix such disregarded ANTI
362 deps when broken. If the first_def reaches the USE then
363 there is such a dep. */
364 ddg_node_ptr first_def_node = get_node_of_insn (g,
365 DF_REF_INSN (first_def));
367 gcc_assert (first_def_node);
369 /* Always create the edge if the use node is a branch in
370 order to prevent the creation of reg-moves.
371 If the address that is being auto-inc or auto-dec in LAST_DEF
372 is used in USE_INSN then do not remove the edge to make sure
373 reg-moves will not be created for that address. */
374 if (DF_REF_ID (last_def) != DF_REF_ID (first_def)
375 || !flag_modulo_sched_allow_regmoves
376 || JUMP_P (use_node->insn)
377 || autoinc_var_is_used_p (DF_REF_INSN (last_def), use_insn)
378 || def_has_ccmode_p (DF_REF_INSN (last_def)))
379 create_ddg_dep_no_link (g, use_node, first_def_node, ANTI_DEP,
380 REG_DEP, 1);
384 /* Create an inter-loop output dependence between LAST_DEF (which is the
385 last def in its block, being downwards exposed) and the first def in
386 its block. Avoid creating a self output dependence. Avoid creating
387 an output dependence if there is a dependence path between the two
388 defs starting with a true dependence to a use which can be in the
389 next iteration; followed by an anti dependence of that use to the
390 first def (i.e. if there is a use between the two defs.) */
391 if (!has_use_in_bb_p)
393 ddg_node_ptr dest_node;
395 if (DF_REF_ID (last_def) == DF_REF_ID (first_def))
396 return;
398 dest_node = get_node_of_insn (g, DF_REF_INSN (first_def));
399 gcc_assert (dest_node);
400 create_ddg_dep_no_link (g, last_def_node, dest_node,
401 OUTPUT_DEP, REG_DEP, 1);
404 /* Build inter-loop dependencies, by looking at DF analysis backwards. */
405 static void
406 build_inter_loop_deps (ddg_ptr g)
408 unsigned rd_num;
409 struct df_rd_bb_info *rd_bb_info;
410 bitmap_iterator bi;
412 rd_bb_info = DF_RD_BB_INFO (g->bb);
414 /* Find inter-loop register output, true and anti deps. */
415 EXECUTE_IF_SET_IN_BITMAP (&rd_bb_info->gen, 0, rd_num, bi)
417 df_ref rd = DF_DEFS_GET (rd_num);
419 add_cross_iteration_register_deps (g, rd);
424 /* Return true if two specified instructions have mem expr with conflict
425 alias sets. */
426 static bool
427 insns_may_alias_p (rtx_insn *insn1, rtx_insn *insn2)
429 subrtx_iterator::array_type array1;
430 subrtx_iterator::array_type array2;
431 FOR_EACH_SUBRTX (iter1, array1, PATTERN (insn1), NONCONST)
433 const_rtx x1 = *iter1;
434 if (MEM_P (x1))
435 FOR_EACH_SUBRTX (iter2, array2, PATTERN (insn2), NONCONST)
437 const_rtx x2 = *iter2;
438 if (MEM_P (x2) && may_alias_p (x2, x1))
439 return true;
442 return false;
445 /* Given two nodes, analyze their RTL insns and add intra-loop mem deps
446 to ddg G. */
447 static void
448 add_intra_loop_mem_dep (ddg_ptr g, ddg_node_ptr from, ddg_node_ptr to)
451 if ((from->cuid == to->cuid)
452 || !insns_may_alias_p (from->insn, to->insn))
453 /* Do not create edge if memory references have disjoint alias sets
454 or 'to' and 'from' are the same instruction. */
455 return;
457 if (mem_write_insn_p (from->insn))
459 if (mem_read_insn_p (to->insn))
460 create_ddg_dep_no_link (g, from, to,
461 DEBUG_INSN_P (to->insn)
462 ? ANTI_DEP : TRUE_DEP, MEM_DEP, 0);
463 else
464 create_ddg_dep_no_link (g, from, to,
465 DEBUG_INSN_P (to->insn)
466 ? ANTI_DEP : OUTPUT_DEP, MEM_DEP, 0);
468 else if (!mem_read_insn_p (to->insn))
469 create_ddg_dep_no_link (g, from, to, ANTI_DEP, MEM_DEP, 0);
472 /* Given two nodes, analyze their RTL insns and add inter-loop mem deps
473 to ddg G. */
474 static void
475 add_inter_loop_mem_dep (ddg_ptr g, ddg_node_ptr from, ddg_node_ptr to)
477 if (!insns_may_alias_p (from->insn, to->insn))
478 /* Do not create edge if memory references have disjoint alias sets. */
479 return;
481 if (mem_write_insn_p (from->insn))
483 if (mem_read_insn_p (to->insn))
484 create_ddg_dep_no_link (g, from, to,
485 DEBUG_INSN_P (to->insn)
486 ? ANTI_DEP : TRUE_DEP, MEM_DEP, 1);
487 else if (from->cuid != to->cuid)
488 create_ddg_dep_no_link (g, from, to,
489 DEBUG_INSN_P (to->insn)
490 ? ANTI_DEP : OUTPUT_DEP, MEM_DEP, 1);
492 else
494 if (mem_read_insn_p (to->insn))
495 return;
496 else if (from->cuid != to->cuid)
498 create_ddg_dep_no_link (g, from, to, ANTI_DEP, MEM_DEP, 1);
499 if (DEBUG_INSN_P (from->insn) || DEBUG_INSN_P (to->insn))
500 create_ddg_dep_no_link (g, to, from, ANTI_DEP, MEM_DEP, 1);
501 else
502 create_ddg_dep_no_link (g, to, from, TRUE_DEP, MEM_DEP, 1);
508 /* Perform intra-block Data Dependency analysis and connect the nodes in
509 the DDG. We assume the loop has a single basic block. */
510 static void
511 build_intra_loop_deps (ddg_ptr g)
513 int i;
514 /* Hold the dependency analysis state during dependency calculations. */
515 struct deps_desc tmp_deps;
516 rtx_insn *head, *tail;
518 /* Build the dependence information, using the sched_analyze function. */
519 init_deps_global ();
520 init_deps (&tmp_deps, false);
522 /* Do the intra-block data dependence analysis for the given block. */
523 get_ebb_head_tail (g->bb, g->bb, &head, &tail);
524 sched_analyze (&tmp_deps, head, tail);
526 /* Build intra-loop data dependencies using the scheduler dependency
527 analysis. */
528 for (i = 0; i < g->num_nodes; i++)
530 ddg_node_ptr dest_node = &g->nodes[i];
531 sd_iterator_def sd_it;
532 dep_t dep;
534 if (! INSN_P (dest_node->insn))
535 continue;
537 FOR_EACH_DEP (dest_node->insn, SD_LIST_BACK, sd_it, dep)
539 rtx_insn *src_insn = DEP_PRO (dep);
540 ddg_node_ptr src_node;
542 /* Don't add dependencies on debug insns to non-debug insns
543 to avoid codegen differences between -g and -g0. */
544 if (DEBUG_INSN_P (src_insn) && !DEBUG_INSN_P (dest_node->insn))
545 continue;
547 src_node = get_node_of_insn (g, src_insn);
549 if (!src_node)
550 continue;
552 create_ddg_dep_from_intra_loop_link (g, src_node, dest_node, dep);
555 /* If this insn modifies memory, add an edge to all insns that access
556 memory. */
557 if (mem_access_insn_p (dest_node->insn))
559 int j;
561 for (j = 0; j <= i; j++)
563 ddg_node_ptr j_node = &g->nodes[j];
564 if (DEBUG_INSN_P (j_node->insn))
565 continue;
566 if (mem_access_insn_p (j_node->insn))
568 /* Don't bother calculating inter-loop dep if an intra-loop dep
569 already exists. */
570 if (! bitmap_bit_p (dest_node->successors, j))
571 add_inter_loop_mem_dep (g, dest_node, j_node);
572 /* If -fmodulo-sched-allow-regmoves
573 is set certain anti-dep edges are not created.
574 It might be that these anti-dep edges are on the
575 path from one memory instruction to another such that
576 removing these edges could cause a violation of the
577 memory dependencies. Thus we add intra edges between
578 every two memory instructions in this case. */
579 if (flag_modulo_sched_allow_regmoves
580 && !bitmap_bit_p (dest_node->predecessors, j))
581 add_intra_loop_mem_dep (g, j_node, dest_node);
587 /* Free the INSN_LISTs. */
588 finish_deps_global ();
589 free_deps (&tmp_deps);
591 /* Free dependencies. */
592 sched_free_deps (head, tail, false);
596 /* Given a basic block, create its DDG and return a pointer to a variable
597 of ddg type that represents it.
598 Initialize the ddg structure fields to the appropriate values. */
599 ddg_ptr
600 create_ddg (basic_block bb, int closing_branch_deps)
602 ddg_ptr g;
603 rtx_insn *insn, *first_note;
604 int i;
605 int num_nodes = 0;
607 g = (ddg_ptr) xcalloc (1, sizeof (struct ddg));
609 g->bb = bb;
610 g->closing_branch_deps = closing_branch_deps;
612 /* Count the number of insns in the BB. */
613 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
614 insn = NEXT_INSN (insn))
616 if (! INSN_P (insn) || GET_CODE (PATTERN (insn)) == USE)
617 continue;
619 if (DEBUG_INSN_P (insn))
620 g->num_debug++;
621 else
623 if (mem_read_insn_p (insn))
624 g->num_loads++;
625 if (mem_write_insn_p (insn))
626 g->num_stores++;
628 num_nodes++;
631 /* There is nothing to do for this BB. */
632 if ((num_nodes - g->num_debug) <= 1)
634 free (g);
635 return NULL;
638 /* Allocate the nodes array, and initialize the nodes. */
639 g->num_nodes = num_nodes;
640 g->nodes = (ddg_node_ptr) xcalloc (num_nodes, sizeof (struct ddg_node));
641 g->closing_branch = NULL;
642 i = 0;
643 first_note = NULL;
644 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
645 insn = NEXT_INSN (insn))
647 if (! INSN_P (insn))
649 if (! first_note && NOTE_P (insn)
650 && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK)
651 first_note = insn;
652 continue;
654 if (JUMP_P (insn))
656 gcc_assert (!g->closing_branch);
657 g->closing_branch = &g->nodes[i];
659 else if (GET_CODE (PATTERN (insn)) == USE)
661 if (! first_note)
662 first_note = insn;
663 continue;
666 g->nodes[i].cuid = i;
667 g->nodes[i].successors = sbitmap_alloc (num_nodes);
668 bitmap_clear (g->nodes[i].successors);
669 g->nodes[i].predecessors = sbitmap_alloc (num_nodes);
670 bitmap_clear (g->nodes[i].predecessors);
671 g->nodes[i].first_note = (first_note ? first_note : insn);
672 g->nodes[i++].insn = insn;
673 first_note = NULL;
676 /* We must have found a branch in DDG. */
677 gcc_assert (g->closing_branch);
680 /* Build the data dependency graph. */
681 build_intra_loop_deps (g);
682 build_inter_loop_deps (g);
683 return g;
686 /* Free all the memory allocated for the DDG. */
687 void
688 free_ddg (ddg_ptr g)
690 int i;
692 if (!g)
693 return;
695 for (i = 0; i < g->num_nodes; i++)
697 ddg_edge_ptr e = g->nodes[i].out;
699 while (e)
701 ddg_edge_ptr next = e->next_out;
703 free (e);
704 e = next;
706 sbitmap_free (g->nodes[i].successors);
707 sbitmap_free (g->nodes[i].predecessors);
709 if (g->num_backarcs > 0)
710 free (g->backarcs);
711 free (g->nodes);
712 free (g);
715 void
716 print_ddg_edge (FILE *file, ddg_edge_ptr e)
718 char dep_c;
720 switch (e->type)
722 case OUTPUT_DEP :
723 dep_c = 'O';
724 break;
725 case ANTI_DEP :
726 dep_c = 'A';
727 break;
728 default:
729 dep_c = 'T';
732 fprintf (file, " [%d -(%c,%d,%d)-> %d] ", INSN_UID (e->src->insn),
733 dep_c, e->latency, e->distance, INSN_UID (e->dest->insn));
736 /* Print the DDG nodes with there in/out edges to the dump file. */
737 void
738 print_ddg (FILE *file, ddg_ptr g)
740 int i;
742 for (i = 0; i < g->num_nodes; i++)
744 ddg_edge_ptr e;
746 fprintf (file, "Node num: %d\n", g->nodes[i].cuid);
747 print_rtl_single (file, g->nodes[i].insn);
748 fprintf (file, "OUT ARCS: ");
749 for (e = g->nodes[i].out; e; e = e->next_out)
750 print_ddg_edge (file, e);
752 fprintf (file, "\nIN ARCS: ");
753 for (e = g->nodes[i].in; e; e = e->next_in)
754 print_ddg_edge (file, e);
756 fprintf (file, "\n");
760 /* Print the given DDG in VCG format. */
761 DEBUG_FUNCTION void
762 vcg_print_ddg (FILE *file, ddg_ptr g)
764 int src_cuid;
766 fprintf (file, "graph: {\n");
767 for (src_cuid = 0; src_cuid < g->num_nodes; src_cuid++)
769 ddg_edge_ptr e;
770 int src_uid = INSN_UID (g->nodes[src_cuid].insn);
772 fprintf (file, "node: {title: \"%d_%d\" info1: \"", src_cuid, src_uid);
773 print_rtl_single (file, g->nodes[src_cuid].insn);
774 fprintf (file, "\"}\n");
775 for (e = g->nodes[src_cuid].out; e; e = e->next_out)
777 int dst_uid = INSN_UID (e->dest->insn);
778 int dst_cuid = e->dest->cuid;
780 /* Give the backarcs a different color. */
781 if (e->distance > 0)
782 fprintf (file, "backedge: {color: red ");
783 else
784 fprintf (file, "edge: { ");
786 fprintf (file, "sourcename: \"%d_%d\" ", src_cuid, src_uid);
787 fprintf (file, "targetname: \"%d_%d\" ", dst_cuid, dst_uid);
788 fprintf (file, "label: \"%d_%d\"}\n", e->latency, e->distance);
791 fprintf (file, "}\n");
794 /* Dump the sccs in SCCS. */
795 void
796 print_sccs (FILE *file, ddg_all_sccs_ptr sccs, ddg_ptr g)
798 unsigned int u = 0;
799 sbitmap_iterator sbi;
800 int i;
802 if (!file)
803 return;
805 fprintf (file, "\n;; Number of SCC nodes - %d\n", sccs->num_sccs);
806 for (i = 0; i < sccs->num_sccs; i++)
808 fprintf (file, "SCC number: %d\n", i);
809 EXECUTE_IF_SET_IN_BITMAP (sccs->sccs[i]->nodes, 0, u, sbi)
811 fprintf (file, "insn num %d\n", u);
812 print_rtl_single (file, g->nodes[u].insn);
815 fprintf (file, "\n");
818 /* Create an edge and initialize it with given values. */
819 static ddg_edge_ptr
820 create_ddg_edge (ddg_node_ptr src, ddg_node_ptr dest,
821 dep_type t, dep_data_type dt, int l, int d)
823 ddg_edge_ptr e = (ddg_edge_ptr) xmalloc (sizeof (struct ddg_edge));
825 e->src = src;
826 e->dest = dest;
827 e->type = t;
828 e->data_type = dt;
829 e->latency = l;
830 e->distance = d;
831 e->next_in = e->next_out = NULL;
832 e->aux.info = 0;
833 return e;
836 /* Add the given edge to the in/out linked lists of the DDG nodes. */
837 static void
838 add_edge_to_ddg (ddg_ptr g ATTRIBUTE_UNUSED, ddg_edge_ptr e)
840 ddg_node_ptr src = e->src;
841 ddg_node_ptr dest = e->dest;
843 /* Should have allocated the sbitmaps. */
844 gcc_assert (src->successors && dest->predecessors);
846 bitmap_set_bit (src->successors, dest->cuid);
847 bitmap_set_bit (dest->predecessors, src->cuid);
848 e->next_in = dest->in;
849 dest->in = e;
850 e->next_out = src->out;
851 src->out = e;
856 /* Algorithm for computing the recurrence_length of an scc. We assume at
857 for now that cycles in the data dependence graph contain a single backarc.
858 This simplifies the algorithm, and can be generalized later. */
859 static void
860 set_recurrence_length (ddg_scc_ptr scc, ddg_ptr g)
862 int j;
863 int result = -1;
865 for (j = 0; j < scc->num_backarcs; j++)
867 ddg_edge_ptr backarc = scc->backarcs[j];
868 int length;
869 int distance = backarc->distance;
870 ddg_node_ptr src = backarc->dest;
871 ddg_node_ptr dest = backarc->src;
873 length = longest_simple_path (g, src->cuid, dest->cuid, scc->nodes);
874 if (length < 0 )
876 /* fprintf (stderr, "Backarc not on simple cycle in SCC.\n"); */
877 continue;
879 length += backarc->latency;
880 result = MAX (result, (length / distance));
882 scc->recurrence_length = result;
885 /* Create a new SCC given the set of its nodes. Compute its recurrence_length
886 and mark edges that belong to this scc as IN_SCC. */
887 static ddg_scc_ptr
888 create_scc (ddg_ptr g, sbitmap nodes)
890 ddg_scc_ptr scc;
891 unsigned int u = 0;
892 sbitmap_iterator sbi;
894 scc = (ddg_scc_ptr) xmalloc (sizeof (struct ddg_scc));
895 scc->backarcs = NULL;
896 scc->num_backarcs = 0;
897 scc->nodes = sbitmap_alloc (g->num_nodes);
898 bitmap_copy (scc->nodes, nodes);
900 /* Mark the backarcs that belong to this SCC. */
901 EXECUTE_IF_SET_IN_BITMAP (nodes, 0, u, sbi)
903 ddg_edge_ptr e;
904 ddg_node_ptr n = &g->nodes[u];
906 for (e = n->out; e; e = e->next_out)
907 if (bitmap_bit_p (nodes, e->dest->cuid))
909 e->aux.count = IN_SCC;
910 if (e->distance > 0)
911 add_backarc_to_scc (scc, e);
915 set_recurrence_length (scc, g);
916 return scc;
919 /* Cleans the memory allocation of a given SCC. */
920 static void
921 free_scc (ddg_scc_ptr scc)
923 if (!scc)
924 return;
926 sbitmap_free (scc->nodes);
927 if (scc->num_backarcs > 0)
928 free (scc->backarcs);
929 free (scc);
933 /* Add a given edge known to be a backarc to the given DDG. */
934 static void
935 add_backarc_to_ddg (ddg_ptr g, ddg_edge_ptr e)
937 int size = (g->num_backarcs + 1) * sizeof (ddg_edge_ptr);
939 add_edge_to_ddg (g, e);
940 g->backarcs = (ddg_edge_ptr *) xrealloc (g->backarcs, size);
941 g->backarcs[g->num_backarcs++] = e;
944 /* Add backarc to an SCC. */
945 static void
946 add_backarc_to_scc (ddg_scc_ptr scc, ddg_edge_ptr e)
948 int size = (scc->num_backarcs + 1) * sizeof (ddg_edge_ptr);
950 scc->backarcs = (ddg_edge_ptr *) xrealloc (scc->backarcs, size);
951 scc->backarcs[scc->num_backarcs++] = e;
954 /* Add the given SCC to the DDG. */
955 static void
956 add_scc_to_ddg (ddg_all_sccs_ptr g, ddg_scc_ptr scc)
958 int size = (g->num_sccs + 1) * sizeof (ddg_scc_ptr);
960 g->sccs = (ddg_scc_ptr *) xrealloc (g->sccs, size);
961 g->sccs[g->num_sccs++] = scc;
964 /* Given the instruction INSN return the node that represents it. */
965 ddg_node_ptr
966 get_node_of_insn (ddg_ptr g, rtx_insn *insn)
968 int i;
970 for (i = 0; i < g->num_nodes; i++)
971 if (insn == g->nodes[i].insn)
972 return &g->nodes[i];
973 return NULL;
976 /* Given a set OPS of nodes in the DDG, find the set of their successors
977 which are not in OPS, and set their bits in SUCC. Bits corresponding to
978 OPS are cleared from SUCC. Leaves the other bits in SUCC unchanged. */
979 void
980 find_successors (sbitmap succ, ddg_ptr g, sbitmap ops)
982 unsigned int i = 0;
983 sbitmap_iterator sbi;
985 EXECUTE_IF_SET_IN_BITMAP (ops, 0, i, sbi)
987 const sbitmap node_succ = NODE_SUCCESSORS (&g->nodes[i]);
988 bitmap_ior (succ, succ, node_succ);
991 /* We want those that are not in ops. */
992 bitmap_and_compl (succ, succ, ops);
995 /* Given a set OPS of nodes in the DDG, find the set of their predecessors
996 which are not in OPS, and set their bits in PREDS. Bits corresponding to
997 OPS are cleared from PREDS. Leaves the other bits in PREDS unchanged. */
998 void
999 find_predecessors (sbitmap preds, ddg_ptr g, sbitmap ops)
1001 unsigned int i = 0;
1002 sbitmap_iterator sbi;
1004 EXECUTE_IF_SET_IN_BITMAP (ops, 0, i, sbi)
1006 const sbitmap node_preds = NODE_PREDECESSORS (&g->nodes[i]);
1007 bitmap_ior (preds, preds, node_preds);
1010 /* We want those that are not in ops. */
1011 bitmap_and_compl (preds, preds, ops);
1015 /* Compare function to be passed to qsort to order the backarcs in descending
1016 recMII order. */
1017 static int
1018 compare_sccs (const void *s1, const void *s2)
1020 const int rec_l1 = (*(const ddg_scc_ptr *)s1)->recurrence_length;
1021 const int rec_l2 = (*(const ddg_scc_ptr *)s2)->recurrence_length;
1022 return ((rec_l2 > rec_l1) - (rec_l2 < rec_l1));
1026 /* Order the backarcs in descending recMII order using compare_sccs. */
1027 static void
1028 order_sccs (ddg_all_sccs_ptr g)
1030 qsort (g->sccs, g->num_sccs, sizeof (ddg_scc_ptr),
1031 (int (*) (const void *, const void *)) compare_sccs);
1034 #ifdef ENABLE_CHECKING
1035 /* Check that every node in SCCS belongs to exactly one strongly connected
1036 component and that no element of SCCS is empty. */
1037 static void
1038 check_sccs (ddg_all_sccs_ptr sccs, int num_nodes)
1040 int i = 0;
1041 sbitmap tmp = sbitmap_alloc (num_nodes);
1043 bitmap_clear (tmp);
1044 for (i = 0; i < sccs->num_sccs; i++)
1046 gcc_assert (!bitmap_empty_p (sccs->sccs[i]->nodes));
1047 /* Verify that every node in sccs is in exactly one strongly
1048 connected component. */
1049 gcc_assert (!bitmap_intersect_p (tmp, sccs->sccs[i]->nodes));
1050 bitmap_ior (tmp, tmp, sccs->sccs[i]->nodes);
1052 sbitmap_free (tmp);
1054 #endif
1056 /* Perform the Strongly Connected Components decomposing algorithm on the
1057 DDG and return DDG_ALL_SCCS structure that contains them. */
1058 ddg_all_sccs_ptr
1059 create_ddg_all_sccs (ddg_ptr g)
1061 int i;
1062 int num_nodes = g->num_nodes;
1063 sbitmap from = sbitmap_alloc (num_nodes);
1064 sbitmap to = sbitmap_alloc (num_nodes);
1065 sbitmap scc_nodes = sbitmap_alloc (num_nodes);
1066 ddg_all_sccs_ptr sccs = (ddg_all_sccs_ptr)
1067 xmalloc (sizeof (struct ddg_all_sccs));
1069 sccs->ddg = g;
1070 sccs->sccs = NULL;
1071 sccs->num_sccs = 0;
1073 for (i = 0; i < g->num_backarcs; i++)
1075 ddg_scc_ptr scc;
1076 ddg_edge_ptr backarc = g->backarcs[i];
1077 ddg_node_ptr src = backarc->src;
1078 ddg_node_ptr dest = backarc->dest;
1080 /* If the backarc already belongs to an SCC, continue. */
1081 if (backarc->aux.count == IN_SCC)
1082 continue;
1084 bitmap_clear (scc_nodes);
1085 bitmap_clear (from);
1086 bitmap_clear (to);
1087 bitmap_set_bit (from, dest->cuid);
1088 bitmap_set_bit (to, src->cuid);
1090 if (find_nodes_on_paths (scc_nodes, g, from, to))
1092 scc = create_scc (g, scc_nodes);
1093 add_scc_to_ddg (sccs, scc);
1096 order_sccs (sccs);
1097 sbitmap_free (from);
1098 sbitmap_free (to);
1099 sbitmap_free (scc_nodes);
1100 #ifdef ENABLE_CHECKING
1101 check_sccs (sccs, num_nodes);
1102 #endif
1103 return sccs;
1106 /* Frees the memory allocated for all SCCs of the DDG, but keeps the DDG. */
1107 void
1108 free_ddg_all_sccs (ddg_all_sccs_ptr all_sccs)
1110 int i;
1112 if (!all_sccs)
1113 return;
1115 for (i = 0; i < all_sccs->num_sccs; i++)
1116 free_scc (all_sccs->sccs[i]);
1118 free (all_sccs->sccs);
1119 free (all_sccs);
1123 /* Given FROM - a bitmap of source nodes - and TO - a bitmap of destination
1124 nodes - find all nodes that lie on paths from FROM to TO (not excluding
1125 nodes from FROM and TO). Return nonzero if nodes exist. */
1127 find_nodes_on_paths (sbitmap result, ddg_ptr g, sbitmap from, sbitmap to)
1129 int answer;
1130 int change;
1131 unsigned int u = 0;
1132 int num_nodes = g->num_nodes;
1133 sbitmap_iterator sbi;
1135 sbitmap workset = sbitmap_alloc (num_nodes);
1136 sbitmap reachable_from = sbitmap_alloc (num_nodes);
1137 sbitmap reach_to = sbitmap_alloc (num_nodes);
1138 sbitmap tmp = sbitmap_alloc (num_nodes);
1140 bitmap_copy (reachable_from, from);
1141 bitmap_copy (tmp, from);
1143 change = 1;
1144 while (change)
1146 change = 0;
1147 bitmap_copy (workset, tmp);
1148 bitmap_clear (tmp);
1149 EXECUTE_IF_SET_IN_BITMAP (workset, 0, u, sbi)
1151 ddg_edge_ptr e;
1152 ddg_node_ptr u_node = &g->nodes[u];
1154 for (e = u_node->out; e != (ddg_edge_ptr) 0; e = e->next_out)
1156 ddg_node_ptr v_node = e->dest;
1157 int v = v_node->cuid;
1159 if (!bitmap_bit_p (reachable_from, v))
1161 bitmap_set_bit (reachable_from, v);
1162 bitmap_set_bit (tmp, v);
1163 change = 1;
1169 bitmap_copy (reach_to, to);
1170 bitmap_copy (tmp, to);
1172 change = 1;
1173 while (change)
1175 change = 0;
1176 bitmap_copy (workset, tmp);
1177 bitmap_clear (tmp);
1178 EXECUTE_IF_SET_IN_BITMAP (workset, 0, u, sbi)
1180 ddg_edge_ptr e;
1181 ddg_node_ptr u_node = &g->nodes[u];
1183 for (e = u_node->in; e != (ddg_edge_ptr) 0; e = e->next_in)
1185 ddg_node_ptr v_node = e->src;
1186 int v = v_node->cuid;
1188 if (!bitmap_bit_p (reach_to, v))
1190 bitmap_set_bit (reach_to, v);
1191 bitmap_set_bit (tmp, v);
1192 change = 1;
1198 answer = bitmap_and (result, reachable_from, reach_to);
1199 sbitmap_free (workset);
1200 sbitmap_free (reachable_from);
1201 sbitmap_free (reach_to);
1202 sbitmap_free (tmp);
1203 return answer;
1207 /* Updates the counts of U_NODE's successors (that belong to NODES) to be
1208 at-least as large as the count of U_NODE plus the latency between them.
1209 Sets a bit in TMP for each successor whose count was changed (increased).
1210 Returns nonzero if any count was changed. */
1211 static int
1212 update_dist_to_successors (ddg_node_ptr u_node, sbitmap nodes, sbitmap tmp)
1214 ddg_edge_ptr e;
1215 int result = 0;
1217 for (e = u_node->out; e; e = e->next_out)
1219 ddg_node_ptr v_node = e->dest;
1220 int v = v_node->cuid;
1222 if (bitmap_bit_p (nodes, v)
1223 && (e->distance == 0)
1224 && (v_node->aux.count < u_node->aux.count + e->latency))
1226 v_node->aux.count = u_node->aux.count + e->latency;
1227 bitmap_set_bit (tmp, v);
1228 result = 1;
1231 return result;
1235 /* Find the length of a longest path from SRC to DEST in G,
1236 going only through NODES, and disregarding backarcs. */
1238 longest_simple_path (struct ddg * g, int src, int dest, sbitmap nodes)
1240 int i;
1241 unsigned int u = 0;
1242 int change = 1;
1243 int result;
1244 int num_nodes = g->num_nodes;
1245 sbitmap workset = sbitmap_alloc (num_nodes);
1246 sbitmap tmp = sbitmap_alloc (num_nodes);
1249 /* Data will hold the distance of the longest path found so far from
1250 src to each node. Initialize to -1 = less than minimum. */
1251 for (i = 0; i < g->num_nodes; i++)
1252 g->nodes[i].aux.count = -1;
1253 g->nodes[src].aux.count = 0;
1255 bitmap_clear (tmp);
1256 bitmap_set_bit (tmp, src);
1258 while (change)
1260 sbitmap_iterator sbi;
1262 change = 0;
1263 bitmap_copy (workset, tmp);
1264 bitmap_clear (tmp);
1265 EXECUTE_IF_SET_IN_BITMAP (workset, 0, u, sbi)
1267 ddg_node_ptr u_node = &g->nodes[u];
1269 change |= update_dist_to_successors (u_node, nodes, tmp);
1272 result = g->nodes[dest].aux.count;
1273 sbitmap_free (workset);
1274 sbitmap_free (tmp);
1275 return result;
1278 #endif /* INSN_SCHEDULING */