]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/mcf.c
2015-06-04 Andrew MacLeod <amacleod@redhat.com>
[thirdparty/gcc.git] / gcc / mcf.c
1 /* Routines to implement minimum-cost maximal flow algorithm used to smooth
2 basic block and edge frequency counts.
3 Copyright (C) 2008-2015 Free Software Foundation, Inc.
4 Contributed by Paul Yuan (yingbo.com@gmail.com) and
5 Vinodha Ramasamy (vinodha@google.com).
6
7 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* References:
23 [1] "Feedback-directed Optimizations in GCC with Estimated Edge Profiles
24 from Hardware Event Sampling", Vinodha Ramasamy, Paul Yuan, Dehao Chen,
25 and Robert Hundt; GCC Summit 2008.
26 [2] "Complementing Missing and Inaccurate Profiling Using a Minimum Cost
27 Circulation Algorithm", Roy Levin, Ilan Newman and Gadi Haber;
28 HiPEAC '08.
29
30 Algorithm to smooth basic block and edge counts:
31 1. create_fixup_graph: Create fixup graph by translating function CFG into
32 a graph that satisfies MCF algorithm requirements.
33 2. find_max_flow: Find maximal flow.
34 3. compute_residual_flow: Form residual network.
35 4. Repeat:
36 cancel_negative_cycle: While G contains a negative cost cycle C, reverse
37 the flow on the found cycle by the minimum residual capacity in that
38 cycle.
39 5. Form the minimal cost flow
40 f(u,v) = rf(v, u).
41 6. adjust_cfg_counts: Update initial edge weights with corrected weights.
42 delta(u.v) = f(u,v) -f(v,u).
43 w*(u,v) = w(u,v) + delta(u,v). */
44
45 #include "config.h"
46 #include "system.h"
47 #include "coretypes.h"
48 #include "predict.h"
49 #include "vec.h"
50 #include "hashtab.h"
51 #include "hash-set.h"
52 #include "tm.h"
53 #include "hard-reg-set.h"
54 #include "input.h"
55 #include "function.h"
56 #include "dominance.h"
57 #include "cfg.h"
58 #include "basic-block.h"
59 #include "gcov-io.h"
60 #include "profile.h"
61 #include "dumpfile.h"
62
63 /* CAP_INFINITY: Constant to represent infinite capacity. */
64 #define CAP_INFINITY INTTYPE_MAXIMUM (int64_t)
65
66 /* COST FUNCTION. */
67 #define K_POS(b) ((b))
68 #define K_NEG(b) (50 * (b))
69 #define COST(k, w) ((k) / mcf_ln ((w) + 2))
70 /* Limit the number of iterations for cancel_negative_cycles() to ensure
71 reasonable compile time. */
72 #define MAX_ITER(n, e) 10 + (1000000 / ((n) * (e)))
73 typedef enum
74 {
75 INVALID_EDGE,
76 VERTEX_SPLIT_EDGE, /* Edge to represent vertex with w(e) = w(v). */
77 REDIRECT_EDGE, /* Edge after vertex transformation. */
78 REVERSE_EDGE,
79 SOURCE_CONNECT_EDGE, /* Single edge connecting to single source. */
80 SINK_CONNECT_EDGE, /* Single edge connecting to single sink. */
81 BALANCE_EDGE, /* Edge connecting with source/sink: cp(e) = 0. */
82 REDIRECT_NORMALIZED_EDGE, /* Normalized edge for a redirect edge. */
83 REVERSE_NORMALIZED_EDGE /* Normalized edge for a reverse edge. */
84 } edge_type;
85
86 /* Structure to represent an edge in the fixup graph. */
87 typedef struct fixup_edge_d
88 {
89 int src;
90 int dest;
91 /* Flag denoting type of edge and attributes for the flow field. */
92 edge_type type;
93 bool is_rflow_valid;
94 /* Index to the normalization vertex added for this edge. */
95 int norm_vertex_index;
96 /* Flow for this edge. */
97 gcov_type flow;
98 /* Residual flow for this edge - used during negative cycle canceling. */
99 gcov_type rflow;
100 gcov_type weight;
101 gcov_type cost;
102 gcov_type max_capacity;
103 } fixup_edge_type;
104
105 typedef fixup_edge_type *fixup_edge_p;
106
107
108 /* Structure to represent a vertex in the fixup graph. */
109 typedef struct fixup_vertex_d
110 {
111 vec<fixup_edge_p> succ_edges;
112 } fixup_vertex_type;
113
114 typedef fixup_vertex_type *fixup_vertex_p;
115
116 /* Fixup graph used in the MCF algorithm. */
117 typedef struct fixup_graph_d
118 {
119 /* Current number of vertices for the graph. */
120 int num_vertices;
121 /* Current number of edges for the graph. */
122 int num_edges;
123 /* Index of new entry vertex. */
124 int new_entry_index;
125 /* Index of new exit vertex. */
126 int new_exit_index;
127 /* Fixup vertex list. Adjacency list for fixup graph. */
128 fixup_vertex_p vertex_list;
129 /* Fixup edge list. */
130 fixup_edge_p edge_list;
131 } fixup_graph_type;
132
133 typedef struct queue_d
134 {
135 int *queue;
136 int head;
137 int tail;
138 int size;
139 } queue_type;
140
141 /* Structure used in the maximal flow routines to find augmenting path. */
142 typedef struct augmenting_path_d
143 {
144 /* Queue used to hold vertex indices. */
145 queue_type queue_list;
146 /* Vector to hold chain of pred vertex indices in augmenting path. */
147 int *bb_pred;
148 /* Vector that indicates if basic block i has been visited. */
149 int *is_visited;
150 } augmenting_path_type;
151
152
153 /* Function definitions. */
154
155 /* Dump routines to aid debugging. */
156
157 /* Print basic block with index N for FIXUP_GRAPH in n' and n'' format. */
158
159 static void
160 print_basic_block (FILE *file, fixup_graph_type *fixup_graph, int n)
161 {
162 if (n == ENTRY_BLOCK)
163 fputs ("ENTRY", file);
164 else if (n == ENTRY_BLOCK + 1)
165 fputs ("ENTRY''", file);
166 else if (n == 2 * EXIT_BLOCK)
167 fputs ("EXIT", file);
168 else if (n == 2 * EXIT_BLOCK + 1)
169 fputs ("EXIT''", file);
170 else if (n == fixup_graph->new_exit_index)
171 fputs ("NEW_EXIT", file);
172 else if (n == fixup_graph->new_entry_index)
173 fputs ("NEW_ENTRY", file);
174 else
175 {
176 fprintf (file, "%d", n / 2);
177 if (n % 2)
178 fputs ("''", file);
179 else
180 fputs ("'", file);
181 }
182 }
183
184
185 /* Print edge S->D for given fixup_graph with n' and n'' format.
186 PARAMETERS:
187 S is the index of the source vertex of the edge (input) and
188 D is the index of the destination vertex of the edge (input) for the given
189 fixup_graph (input). */
190
191 static void
192 print_edge (FILE *file, fixup_graph_type *fixup_graph, int s, int d)
193 {
194 print_basic_block (file, fixup_graph, s);
195 fputs ("->", file);
196 print_basic_block (file, fixup_graph, d);
197 }
198
199
200 /* Dump out the attributes of a given edge FEDGE in the fixup_graph to a
201 file. */
202 static void
203 dump_fixup_edge (FILE *file, fixup_graph_type *fixup_graph, fixup_edge_p fedge)
204 {
205 if (!fedge)
206 {
207 fputs ("NULL fixup graph edge.\n", file);
208 return;
209 }
210
211 print_edge (file, fixup_graph, fedge->src, fedge->dest);
212 fputs (": ", file);
213
214 if (fedge->type)
215 {
216 fprintf (file, "flow/capacity=%" PRId64 "/",
217 fedge->flow);
218 if (fedge->max_capacity == CAP_INFINITY)
219 fputs ("+oo,", file);
220 else
221 fprintf (file, "%" PRId64 ",", fedge->max_capacity);
222 }
223
224 if (fedge->is_rflow_valid)
225 {
226 if (fedge->rflow == CAP_INFINITY)
227 fputs (" rflow=+oo.", file);
228 else
229 fprintf (file, " rflow=%" PRId64 ",", fedge->rflow);
230 }
231
232 fprintf (file, " cost=%" PRId64 ".", fedge->cost);
233
234 fprintf (file, "\t(%d->%d)", fedge->src, fedge->dest);
235
236 if (fedge->type)
237 {
238 switch (fedge->type)
239 {
240 case VERTEX_SPLIT_EDGE:
241 fputs (" @VERTEX_SPLIT_EDGE", file);
242 break;
243
244 case REDIRECT_EDGE:
245 fputs (" @REDIRECT_EDGE", file);
246 break;
247
248 case SOURCE_CONNECT_EDGE:
249 fputs (" @SOURCE_CONNECT_EDGE", file);
250 break;
251
252 case SINK_CONNECT_EDGE:
253 fputs (" @SINK_CONNECT_EDGE", file);
254 break;
255
256 case REVERSE_EDGE:
257 fputs (" @REVERSE_EDGE", file);
258 break;
259
260 case BALANCE_EDGE:
261 fputs (" @BALANCE_EDGE", file);
262 break;
263
264 case REDIRECT_NORMALIZED_EDGE:
265 case REVERSE_NORMALIZED_EDGE:
266 fputs (" @NORMALIZED_EDGE", file);
267 break;
268
269 default:
270 fputs (" @INVALID_EDGE", file);
271 break;
272 }
273 }
274 fputs ("\n", file);
275 }
276
277
278 /* Print out the edges and vertices of the given FIXUP_GRAPH, into the dump
279 file. The input string MSG is printed out as a heading. */
280
281 static void
282 dump_fixup_graph (FILE *file, fixup_graph_type *fixup_graph, const char *msg)
283 {
284 int i, j;
285 int fnum_vertices, fnum_edges;
286
287 fixup_vertex_p fvertex_list, pfvertex;
288 fixup_edge_p pfedge;
289
290 gcc_assert (fixup_graph);
291 fvertex_list = fixup_graph->vertex_list;
292 fnum_vertices = fixup_graph->num_vertices;
293 fnum_edges = fixup_graph->num_edges;
294
295 fprintf (file, "\nDump fixup graph for %s(): %s.\n",
296 current_function_name (), msg);
297 fprintf (file,
298 "There are %d vertices and %d edges. new_exit_index is %d.\n\n",
299 fnum_vertices, fnum_edges, fixup_graph->new_exit_index);
300
301 for (i = 0; i < fnum_vertices; i++)
302 {
303 pfvertex = fvertex_list + i;
304 fprintf (file, "vertex_list[%d]: %d succ fixup edges.\n",
305 i, pfvertex->succ_edges.length ());
306
307 for (j = 0; pfvertex->succ_edges.iterate (j, &pfedge);
308 j++)
309 {
310 /* Distinguish forward edges and backward edges in the residual flow
311 network. */
312 if (pfedge->type)
313 fputs ("(f) ", file);
314 else if (pfedge->is_rflow_valid)
315 fputs ("(b) ", file);
316 dump_fixup_edge (file, fixup_graph, pfedge);
317 }
318 }
319
320 fputs ("\n", file);
321 }
322
323
324 /* Utility routines. */
325 /* ln() implementation: approximate calculation. Returns ln of X. */
326
327 static double
328 mcf_ln (double x)
329 {
330 #define E 2.71828
331 int l = 1;
332 double m = E;
333
334 gcc_assert (x >= 0);
335
336 while (m < x)
337 {
338 m *= E;
339 l++;
340 }
341
342 return l;
343 }
344
345
346 /* sqrt() implementation: based on open source QUAKE3 code (magic sqrt
347 implementation) by John Carmack. Returns sqrt of X. */
348
349 static double
350 mcf_sqrt (double x)
351 {
352 #define MAGIC_CONST1 0x1fbcf800
353 #define MAGIC_CONST2 0x5f3759df
354 union {
355 int intPart;
356 float floatPart;
357 } convertor, convertor2;
358
359 gcc_assert (x >= 0);
360
361 convertor.floatPart = x;
362 convertor2.floatPart = x;
363 convertor.intPart = MAGIC_CONST1 + (convertor.intPart >> 1);
364 convertor2.intPart = MAGIC_CONST2 - (convertor2.intPart >> 1);
365
366 return 0.5f * (convertor.floatPart + (x * convertor2.floatPart));
367 }
368
369
370 /* Common code shared between add_fixup_edge and add_rfixup_edge. Adds an edge
371 (SRC->DEST) to the edge_list maintained in FIXUP_GRAPH with cost of the edge
372 added set to COST. */
373
374 static fixup_edge_p
375 add_edge (fixup_graph_type *fixup_graph, int src, int dest, gcov_type cost)
376 {
377 fixup_vertex_p curr_vertex = fixup_graph->vertex_list + src;
378 fixup_edge_p curr_edge = fixup_graph->edge_list + fixup_graph->num_edges;
379 curr_edge->src = src;
380 curr_edge->dest = dest;
381 curr_edge->cost = cost;
382 fixup_graph->num_edges++;
383 if (dump_file)
384 dump_fixup_edge (dump_file, fixup_graph, curr_edge);
385 curr_vertex->succ_edges.safe_push (curr_edge);
386 return curr_edge;
387 }
388
389
390 /* Add a fixup edge (src->dest) with attributes TYPE, WEIGHT, COST and
391 MAX_CAPACITY to the edge_list in the fixup graph. */
392
393 static void
394 add_fixup_edge (fixup_graph_type *fixup_graph, int src, int dest,
395 edge_type type, gcov_type weight, gcov_type cost,
396 gcov_type max_capacity)
397 {
398 fixup_edge_p curr_edge = add_edge (fixup_graph, src, dest, cost);
399 curr_edge->type = type;
400 curr_edge->weight = weight;
401 curr_edge->max_capacity = max_capacity;
402 }
403
404
405 /* Add a residual edge (SRC->DEST) with attributes RFLOW and COST
406 to the fixup graph. */
407
408 static void
409 add_rfixup_edge (fixup_graph_type *fixup_graph, int src, int dest,
410 gcov_type rflow, gcov_type cost)
411 {
412 fixup_edge_p curr_edge = add_edge (fixup_graph, src, dest, cost);
413 curr_edge->rflow = rflow;
414 curr_edge->is_rflow_valid = true;
415 /* This edge is not a valid edge - merely used to hold residual flow. */
416 curr_edge->type = INVALID_EDGE;
417 }
418
419
420 /* Return the pointer to fixup edge SRC->DEST or NULL if edge does not
421 exist in the FIXUP_GRAPH. */
422
423 static fixup_edge_p
424 find_fixup_edge (fixup_graph_type *fixup_graph, int src, int dest)
425 {
426 int j;
427 fixup_edge_p pfedge;
428 fixup_vertex_p pfvertex;
429
430 gcc_assert (src < fixup_graph->num_vertices);
431
432 pfvertex = fixup_graph->vertex_list + src;
433
434 for (j = 0; pfvertex->succ_edges.iterate (j, &pfedge);
435 j++)
436 if (pfedge->dest == dest)
437 return pfedge;
438
439 return NULL;
440 }
441
442
443 /* Cleanup routine to free structures in FIXUP_GRAPH. */
444
445 static void
446 delete_fixup_graph (fixup_graph_type *fixup_graph)
447 {
448 int i;
449 int fnum_vertices = fixup_graph->num_vertices;
450 fixup_vertex_p pfvertex = fixup_graph->vertex_list;
451
452 for (i = 0; i < fnum_vertices; i++, pfvertex++)
453 pfvertex->succ_edges.release ();
454
455 free (fixup_graph->vertex_list);
456 free (fixup_graph->edge_list);
457 }
458
459
460 /* Creates a fixup graph FIXUP_GRAPH from the function CFG. */
461
462 static void
463 create_fixup_graph (fixup_graph_type *fixup_graph)
464 {
465 double sqrt_avg_vertex_weight = 0;
466 double total_vertex_weight = 0;
467 double k_pos = 0;
468 double k_neg = 0;
469 /* Vector to hold D(v) = sum_out_edges(v) - sum_in_edges(v). */
470 gcov_type *diff_out_in = NULL;
471 gcov_type supply_value = 1, demand_value = 0;
472 gcov_type fcost = 0;
473 int new_entry_index = 0, new_exit_index = 0;
474 int i = 0, j = 0;
475 int new_index = 0;
476 basic_block bb;
477 edge e;
478 edge_iterator ei;
479 fixup_edge_p pfedge, r_pfedge;
480 fixup_edge_p fedge_list;
481 int fnum_edges;
482
483 /* Each basic_block will be split into 2 during vertex transformation. */
484 int fnum_vertices_after_transform = 2 * n_basic_blocks_for_fn (cfun);
485 int fnum_edges_after_transform =
486 n_edges_for_fn (cfun) + n_basic_blocks_for_fn (cfun);
487
488 /* Count the new SOURCE and EXIT vertices to be added. */
489 int fmax_num_vertices =
490 (fnum_vertices_after_transform + n_edges_for_fn (cfun)
491 + n_basic_blocks_for_fn (cfun) + 2);
492
493 /* In create_fixup_graph: Each basic block and edge can be split into 3
494 edges. Number of balance edges = n_basic_blocks. So after
495 create_fixup_graph:
496 max_edges = 4 * n_basic_blocks + 3 * n_edges
497 Accounting for residual flow edges
498 max_edges = 2 * (4 * n_basic_blocks + 3 * n_edges)
499 = 8 * n_basic_blocks + 6 * n_edges
500 < 8 * n_basic_blocks + 8 * n_edges. */
501 int fmax_num_edges = 8 * (n_basic_blocks_for_fn (cfun) +
502 n_edges_for_fn (cfun));
503
504 /* Initial num of vertices in the fixup graph. */
505 fixup_graph->num_vertices = n_basic_blocks_for_fn (cfun);
506
507 /* Fixup graph vertex list. */
508 fixup_graph->vertex_list =
509 (fixup_vertex_p) xcalloc (fmax_num_vertices, sizeof (fixup_vertex_type));
510
511 /* Fixup graph edge list. */
512 fixup_graph->edge_list =
513 (fixup_edge_p) xcalloc (fmax_num_edges, sizeof (fixup_edge_type));
514
515 diff_out_in =
516 (gcov_type *) xcalloc (1 + fnum_vertices_after_transform,
517 sizeof (gcov_type));
518
519 /* Compute constants b, k_pos, k_neg used in the cost function calculation.
520 b = sqrt(avg_vertex_weight(cfg)); k_pos = b; k_neg = 50b. */
521 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
522 total_vertex_weight += bb->count;
523
524 sqrt_avg_vertex_weight = mcf_sqrt (total_vertex_weight /
525 n_basic_blocks_for_fn (cfun));
526
527 k_pos = K_POS (sqrt_avg_vertex_weight);
528 k_neg = K_NEG (sqrt_avg_vertex_weight);
529
530 /* 1. Vertex Transformation: Split each vertex v into two vertices v' and v'',
531 connected by an edge e from v' to v''. w(e) = w(v). */
532
533 if (dump_file)
534 fprintf (dump_file, "\nVertex transformation:\n");
535
536 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
537 {
538 /* v'->v'': index1->(index1+1). */
539 i = 2 * bb->index;
540 fcost = (gcov_type) COST (k_pos, bb->count);
541 add_fixup_edge (fixup_graph, i, i + 1, VERTEX_SPLIT_EDGE, bb->count,
542 fcost, CAP_INFINITY);
543 fixup_graph->num_vertices++;
544
545 FOR_EACH_EDGE (e, ei, bb->succs)
546 {
547 /* Edges with ignore attribute set should be treated like they don't
548 exist. */
549 if (EDGE_INFO (e) && EDGE_INFO (e)->ignore)
550 continue;
551 j = 2 * e->dest->index;
552 fcost = (gcov_type) COST (k_pos, e->count);
553 add_fixup_edge (fixup_graph, i + 1, j, REDIRECT_EDGE, e->count, fcost,
554 CAP_INFINITY);
555 }
556 }
557
558 /* After vertex transformation. */
559 gcc_assert (fixup_graph->num_vertices == fnum_vertices_after_transform);
560 /* Redirect edges are not added for edges with ignore attribute. */
561 gcc_assert (fixup_graph->num_edges <= fnum_edges_after_transform);
562
563 fnum_edges_after_transform = fixup_graph->num_edges;
564
565 /* 2. Initialize D(v). */
566 for (i = 0; i < fnum_edges_after_transform; i++)
567 {
568 pfedge = fixup_graph->edge_list + i;
569 diff_out_in[pfedge->src] += pfedge->weight;
570 diff_out_in[pfedge->dest] -= pfedge->weight;
571 }
572
573 /* Entry block - vertex indices 0, 1; EXIT block - vertex indices 2, 3. */
574 for (i = 0; i <= 3; i++)
575 diff_out_in[i] = 0;
576
577 /* 3. Add reverse edges: needed to decrease counts during smoothing. */
578 if (dump_file)
579 fprintf (dump_file, "\nReverse edges:\n");
580 for (i = 0; i < fnum_edges_after_transform; i++)
581 {
582 pfedge = fixup_graph->edge_list + i;
583 if ((pfedge->src == 0) || (pfedge->src == 2))
584 continue;
585 r_pfedge = find_fixup_edge (fixup_graph, pfedge->dest, pfedge->src);
586 if (!r_pfedge && pfedge->weight)
587 {
588 /* Skip adding reverse edges for edges with w(e) = 0, as its maximum
589 capacity is 0. */
590 fcost = (gcov_type) COST (k_neg, pfedge->weight);
591 add_fixup_edge (fixup_graph, pfedge->dest, pfedge->src,
592 REVERSE_EDGE, 0, fcost, pfedge->weight);
593 }
594 }
595
596 /* 4. Create single source and sink. Connect new source vertex s' to function
597 entry block. Connect sink vertex t' to function exit. */
598 if (dump_file)
599 fprintf (dump_file, "\ns'->S, T->t':\n");
600
601 new_entry_index = fixup_graph->new_entry_index = fixup_graph->num_vertices;
602 fixup_graph->num_vertices++;
603 /* Set supply_value to 1 to avoid zero count function ENTRY. */
604 add_fixup_edge (fixup_graph, new_entry_index, ENTRY_BLOCK, SOURCE_CONNECT_EDGE,
605 1 /* supply_value */, 0, 1 /* supply_value */);
606
607 /* Create new exit with EXIT_BLOCK as single pred. */
608 new_exit_index = fixup_graph->new_exit_index = fixup_graph->num_vertices;
609 fixup_graph->num_vertices++;
610 add_fixup_edge (fixup_graph, 2 * EXIT_BLOCK + 1, new_exit_index,
611 SINK_CONNECT_EDGE,
612 0 /* demand_value */, 0, 0 /* demand_value */);
613
614 /* Connect vertices with unbalanced D(v) to source/sink. */
615 if (dump_file)
616 fprintf (dump_file, "\nD(v) balance:\n");
617 /* Skip vertices for ENTRY (0, 1) and EXIT (2,3) blocks, so start with i = 4.
618 diff_out_in[v''] will be 0, so skip v'' vertices, hence i += 2. */
619 for (i = 4; i < new_entry_index; i += 2)
620 {
621 if (diff_out_in[i] > 0)
622 {
623 add_fixup_edge (fixup_graph, i, new_exit_index, BALANCE_EDGE, 0, 0,
624 diff_out_in[i]);
625 demand_value += diff_out_in[i];
626 }
627 else if (diff_out_in[i] < 0)
628 {
629 add_fixup_edge (fixup_graph, new_entry_index, i, BALANCE_EDGE, 0, 0,
630 -diff_out_in[i]);
631 supply_value -= diff_out_in[i];
632 }
633 }
634
635 /* Set supply = demand. */
636 if (dump_file)
637 {
638 fprintf (dump_file, "\nAdjust supply and demand:\n");
639 fprintf (dump_file, "supply_value=%" PRId64 "\n",
640 supply_value);
641 fprintf (dump_file, "demand_value=%" PRId64 "\n",
642 demand_value);
643 }
644
645 if (demand_value > supply_value)
646 {
647 pfedge = find_fixup_edge (fixup_graph, new_entry_index, ENTRY_BLOCK);
648 pfedge->max_capacity += (demand_value - supply_value);
649 }
650 else
651 {
652 pfedge = find_fixup_edge (fixup_graph, 2 * EXIT_BLOCK + 1, new_exit_index);
653 pfedge->max_capacity += (supply_value - demand_value);
654 }
655
656 /* 6. Normalize edges: remove anti-parallel edges. Anti-parallel edges are
657 created by the vertex transformation step from self-edges in the original
658 CFG and by the reverse edges added earlier. */
659 if (dump_file)
660 fprintf (dump_file, "\nNormalize edges:\n");
661
662 fnum_edges = fixup_graph->num_edges;
663 fedge_list = fixup_graph->edge_list;
664
665 for (i = 0; i < fnum_edges; i++)
666 {
667 pfedge = fedge_list + i;
668 r_pfedge = find_fixup_edge (fixup_graph, pfedge->dest, pfedge->src);
669 if (((pfedge->type == VERTEX_SPLIT_EDGE)
670 || (pfedge->type == REDIRECT_EDGE)) && r_pfedge)
671 {
672 new_index = fixup_graph->num_vertices;
673 fixup_graph->num_vertices++;
674
675 if (dump_file)
676 {
677 fprintf (dump_file, "\nAnti-parallel edge:\n");
678 dump_fixup_edge (dump_file, fixup_graph, pfedge);
679 dump_fixup_edge (dump_file, fixup_graph, r_pfedge);
680 fprintf (dump_file, "New vertex is %d.\n", new_index);
681 fprintf (dump_file, "------------------\n");
682 }
683
684 pfedge->cost /= 2;
685 pfedge->norm_vertex_index = new_index;
686 if (dump_file)
687 {
688 fprintf (dump_file, "After normalization:\n");
689 dump_fixup_edge (dump_file, fixup_graph, pfedge);
690 }
691
692 /* Add a new fixup edge: new_index->src. */
693 add_fixup_edge (fixup_graph, new_index, pfedge->src,
694 REVERSE_NORMALIZED_EDGE, 0, r_pfedge->cost,
695 r_pfedge->max_capacity);
696 gcc_assert (fixup_graph->num_vertices <= fmax_num_vertices);
697
698 /* Edge: r_pfedge->src -> r_pfedge->dest
699 ==> r_pfedge->src -> new_index. */
700 r_pfedge->dest = new_index;
701 r_pfedge->type = REVERSE_NORMALIZED_EDGE;
702 r_pfedge->cost = pfedge->cost;
703 r_pfedge->max_capacity = pfedge->max_capacity;
704 if (dump_file)
705 dump_fixup_edge (dump_file, fixup_graph, r_pfedge);
706 }
707 }
708
709 if (dump_file)
710 dump_fixup_graph (dump_file, fixup_graph, "After create_fixup_graph()");
711
712 /* Cleanup. */
713 free (diff_out_in);
714 }
715
716
717 /* Allocates space for the structures in AUGMENTING_PATH. The space needed is
718 proportional to the number of nodes in the graph, which is given by
719 GRAPH_SIZE. */
720
721 static void
722 init_augmenting_path (augmenting_path_type *augmenting_path, int graph_size)
723 {
724 augmenting_path->queue_list.queue = (int *)
725 xcalloc (graph_size + 2, sizeof (int));
726 augmenting_path->queue_list.size = graph_size + 2;
727 augmenting_path->bb_pred = (int *) xcalloc (graph_size, sizeof (int));
728 augmenting_path->is_visited = (int *) xcalloc (graph_size, sizeof (int));
729 }
730
731 /* Free the structures in AUGMENTING_PATH. */
732 static void
733 free_augmenting_path (augmenting_path_type *augmenting_path)
734 {
735 free (augmenting_path->queue_list.queue);
736 free (augmenting_path->bb_pred);
737 free (augmenting_path->is_visited);
738 }
739
740
741 /* Queue routines. Assumes queue will never overflow. */
742
743 static void
744 init_queue (queue_type *queue_list)
745 {
746 gcc_assert (queue_list);
747 queue_list->head = 0;
748 queue_list->tail = 0;
749 }
750
751 /* Return true if QUEUE_LIST is empty. */
752 static bool
753 is_empty (queue_type *queue_list)
754 {
755 return (queue_list->head == queue_list->tail);
756 }
757
758 /* Insert element X into QUEUE_LIST. */
759 static void
760 enqueue (queue_type *queue_list, int x)
761 {
762 gcc_assert (queue_list->tail < queue_list->size);
763 queue_list->queue[queue_list->tail] = x;
764 (queue_list->tail)++;
765 }
766
767 /* Return the first element in QUEUE_LIST. */
768 static int
769 dequeue (queue_type *queue_list)
770 {
771 int x;
772 gcc_assert (queue_list->head >= 0);
773 x = queue_list->queue[queue_list->head];
774 (queue_list->head)++;
775 return x;
776 }
777
778
779 /* Finds a negative cycle in the residual network using
780 the Bellman-Ford algorithm. The flow on the found cycle is reversed by the
781 minimum residual capacity of that cycle. ENTRY and EXIT vertices are not
782 considered.
783
784 Parameters:
785 FIXUP_GRAPH - Residual graph (input/output)
786 The following are allocated/freed by the caller:
787 PI - Vector to hold predecessors in path (pi = pred index)
788 D - D[I] holds minimum cost of path from i to sink
789 CYCLE - Vector to hold the minimum cost cycle
790
791 Return:
792 true if a negative cycle was found, false otherwise. */
793
794 static bool
795 cancel_negative_cycle (fixup_graph_type *fixup_graph,
796 int *pi, gcov_type *d, int *cycle)
797 {
798 int i, j, k;
799 int fnum_vertices, fnum_edges;
800 fixup_edge_p fedge_list, pfedge, r_pfedge;
801 bool found_cycle = false;
802 int cycle_start = 0, cycle_end = 0;
803 gcov_type sum_cost = 0, cycle_flow = 0;
804 int new_entry_index;
805 bool propagated = false;
806
807 gcc_assert (fixup_graph);
808 fnum_vertices = fixup_graph->num_vertices;
809 fnum_edges = fixup_graph->num_edges;
810 fedge_list = fixup_graph->edge_list;
811 new_entry_index = fixup_graph->new_entry_index;
812
813 /* Initialize. */
814 /* Skip ENTRY. */
815 for (i = 1; i < fnum_vertices; i++)
816 {
817 d[i] = CAP_INFINITY;
818 pi[i] = -1;
819 cycle[i] = -1;
820 }
821 d[ENTRY_BLOCK] = 0;
822
823 /* Relax. */
824 for (k = 1; k < fnum_vertices; k++)
825 {
826 propagated = false;
827 for (i = 0; i < fnum_edges; i++)
828 {
829 pfedge = fedge_list + i;
830 if (pfedge->src == new_entry_index)
831 continue;
832 if (pfedge->is_rflow_valid && pfedge->rflow
833 && d[pfedge->src] != CAP_INFINITY
834 && (d[pfedge->dest] > d[pfedge->src] + pfedge->cost))
835 {
836 d[pfedge->dest] = d[pfedge->src] + pfedge->cost;
837 pi[pfedge->dest] = pfedge->src;
838 propagated = true;
839 }
840 }
841 if (!propagated)
842 break;
843 }
844
845 if (!propagated)
846 /* No negative cycles exist. */
847 return 0;
848
849 /* Detect. */
850 for (i = 0; i < fnum_edges; i++)
851 {
852 pfedge = fedge_list + i;
853 if (pfedge->src == new_entry_index)
854 continue;
855 if (pfedge->is_rflow_valid && pfedge->rflow
856 && d[pfedge->src] != CAP_INFINITY
857 && (d[pfedge->dest] > d[pfedge->src] + pfedge->cost))
858 {
859 found_cycle = true;
860 break;
861 }
862 }
863
864 if (!found_cycle)
865 return 0;
866
867 /* Augment the cycle with the cycle's minimum residual capacity. */
868 found_cycle = false;
869 cycle[0] = pfedge->dest;
870 j = pfedge->dest;
871
872 for (i = 1; i < fnum_vertices; i++)
873 {
874 j = pi[j];
875 cycle[i] = j;
876 for (k = 0; k < i; k++)
877 {
878 if (cycle[k] == j)
879 {
880 /* cycle[k] -> ... -> cycle[i]. */
881 cycle_start = k;
882 cycle_end = i;
883 found_cycle = true;
884 break;
885 }
886 }
887 if (found_cycle)
888 break;
889 }
890
891 gcc_assert (cycle[cycle_start] == cycle[cycle_end]);
892 if (dump_file)
893 fprintf (dump_file, "\nNegative cycle length is %d:\n",
894 cycle_end - cycle_start);
895
896 sum_cost = 0;
897 cycle_flow = CAP_INFINITY;
898 for (k = cycle_start; k < cycle_end; k++)
899 {
900 pfedge = find_fixup_edge (fixup_graph, cycle[k + 1], cycle[k]);
901 cycle_flow = MIN (cycle_flow, pfedge->rflow);
902 sum_cost += pfedge->cost;
903 if (dump_file)
904 fprintf (dump_file, "%d ", cycle[k]);
905 }
906
907 if (dump_file)
908 {
909 fprintf (dump_file, "%d", cycle[k]);
910 fprintf (dump_file,
911 ": (%" PRId64 ", %" PRId64
912 ")\n", sum_cost, cycle_flow);
913 fprintf (dump_file,
914 "Augment cycle with %" PRId64 "\n",
915 cycle_flow);
916 }
917
918 for (k = cycle_start; k < cycle_end; k++)
919 {
920 pfedge = find_fixup_edge (fixup_graph, cycle[k + 1], cycle[k]);
921 r_pfedge = find_fixup_edge (fixup_graph, cycle[k], cycle[k + 1]);
922 pfedge->rflow -= cycle_flow;
923 if (pfedge->type)
924 pfedge->flow += cycle_flow;
925 r_pfedge->rflow += cycle_flow;
926 if (r_pfedge->type)
927 r_pfedge->flow -= cycle_flow;
928 }
929
930 return true;
931 }
932
933
934 /* Computes the residual flow for FIXUP_GRAPH by setting the rflow field of
935 the edges. ENTRY and EXIT vertices should not be considered. */
936
937 static void
938 compute_residual_flow (fixup_graph_type *fixup_graph)
939 {
940 int i;
941 int fnum_edges;
942 fixup_edge_p fedge_list, pfedge;
943
944 gcc_assert (fixup_graph);
945
946 if (dump_file)
947 fputs ("\ncompute_residual_flow():\n", dump_file);
948
949 fnum_edges = fixup_graph->num_edges;
950 fedge_list = fixup_graph->edge_list;
951
952 for (i = 0; i < fnum_edges; i++)
953 {
954 pfedge = fedge_list + i;
955 pfedge->rflow = pfedge->max_capacity - pfedge->flow;
956 pfedge->is_rflow_valid = true;
957 add_rfixup_edge (fixup_graph, pfedge->dest, pfedge->src, pfedge->flow,
958 -pfedge->cost);
959 }
960 }
961
962
963 /* Uses Edmonds-Karp algorithm - BFS to find augmenting path from SOURCE to
964 SINK. The fields in the edge vector in the FIXUP_GRAPH are not modified by
965 this routine. The vector bb_pred in the AUGMENTING_PATH structure is updated
966 to reflect the path found.
967 Returns: 0 if no augmenting path is found, 1 otherwise. */
968
969 static int
970 find_augmenting_path (fixup_graph_type *fixup_graph,
971 augmenting_path_type *augmenting_path, int source,
972 int sink)
973 {
974 int u = 0;
975 int i;
976 fixup_vertex_p fvertex_list, pfvertex;
977 fixup_edge_p pfedge;
978 int *bb_pred, *is_visited;
979 queue_type *queue_list;
980
981 gcc_assert (augmenting_path);
982 bb_pred = augmenting_path->bb_pred;
983 gcc_assert (bb_pred);
984 is_visited = augmenting_path->is_visited;
985 gcc_assert (is_visited);
986 queue_list = &(augmenting_path->queue_list);
987
988 gcc_assert (fixup_graph);
989
990 fvertex_list = fixup_graph->vertex_list;
991
992 for (u = 0; u < fixup_graph->num_vertices; u++)
993 is_visited[u] = 0;
994
995 init_queue (queue_list);
996 enqueue (queue_list, source);
997 bb_pred[source] = -1;
998
999 while (!is_empty (queue_list))
1000 {
1001 u = dequeue (queue_list);
1002 is_visited[u] = 1;
1003 pfvertex = fvertex_list + u;
1004 for (i = 0; pfvertex->succ_edges.iterate (i, &pfedge);
1005 i++)
1006 {
1007 int dest = pfedge->dest;
1008 if ((pfedge->rflow > 0) && (is_visited[dest] == 0))
1009 {
1010 enqueue (queue_list, dest);
1011 bb_pred[dest] = u;
1012 is_visited[dest] = 1;
1013 if (dest == sink)
1014 return 1;
1015 }
1016 }
1017 }
1018
1019 return 0;
1020 }
1021
1022
1023 /* Routine to find the maximal flow:
1024 Algorithm:
1025 1. Initialize flow to 0
1026 2. Find an augmenting path form source to sink.
1027 3. Send flow equal to the path's residual capacity along the edges of this path.
1028 4. Repeat steps 2 and 3 until no new augmenting path is found.
1029
1030 Parameters:
1031 SOURCE: index of source vertex (input)
1032 SINK: index of sink vertex (input)
1033 FIXUP_GRAPH: adjacency matrix representing the graph. The flow of the edges will be
1034 set to have a valid maximal flow by this routine. (input)
1035 Return: Maximum flow possible. */
1036
1037 static gcov_type
1038 find_max_flow (fixup_graph_type *fixup_graph, int source, int sink)
1039 {
1040 int fnum_edges;
1041 augmenting_path_type augmenting_path;
1042 int *bb_pred;
1043 gcov_type max_flow = 0;
1044 int i, u;
1045 fixup_edge_p fedge_list, pfedge, r_pfedge;
1046
1047 gcc_assert (fixup_graph);
1048
1049 fnum_edges = fixup_graph->num_edges;
1050 fedge_list = fixup_graph->edge_list;
1051
1052 /* Initialize flow to 0. */
1053 for (i = 0; i < fnum_edges; i++)
1054 {
1055 pfedge = fedge_list + i;
1056 pfedge->flow = 0;
1057 }
1058
1059 compute_residual_flow (fixup_graph);
1060
1061 init_augmenting_path (&augmenting_path, fixup_graph->num_vertices);
1062
1063 bb_pred = augmenting_path.bb_pred;
1064 while (find_augmenting_path (fixup_graph, &augmenting_path, source, sink))
1065 {
1066 /* Determine the amount by which we can increment the flow. */
1067 gcov_type increment = CAP_INFINITY;
1068 for (u = sink; u != source; u = bb_pred[u])
1069 {
1070 pfedge = find_fixup_edge (fixup_graph, bb_pred[u], u);
1071 increment = MIN (increment, pfedge->rflow);
1072 }
1073 max_flow += increment;
1074
1075 /* Now increment the flow. EXIT vertex index is 1. */
1076 for (u = sink; u != source; u = bb_pred[u])
1077 {
1078 pfedge = find_fixup_edge (fixup_graph, bb_pred[u], u);
1079 r_pfedge = find_fixup_edge (fixup_graph, u, bb_pred[u]);
1080 if (pfedge->type)
1081 {
1082 /* forward edge. */
1083 pfedge->flow += increment;
1084 pfedge->rflow -= increment;
1085 r_pfedge->rflow += increment;
1086 }
1087 else
1088 {
1089 /* backward edge. */
1090 gcc_assert (r_pfedge->type);
1091 r_pfedge->rflow += increment;
1092 r_pfedge->flow -= increment;
1093 pfedge->rflow -= increment;
1094 }
1095 }
1096
1097 if (dump_file)
1098 {
1099 fprintf (dump_file, "\nDump augmenting path:\n");
1100 for (u = sink; u != source; u = bb_pred[u])
1101 {
1102 print_basic_block (dump_file, fixup_graph, u);
1103 fprintf (dump_file, "<-");
1104 }
1105 fprintf (dump_file,
1106 "ENTRY (path_capacity=%" PRId64 ")\n",
1107 increment);
1108 fprintf (dump_file,
1109 "Network flow is %" PRId64 ".\n",
1110 max_flow);
1111 }
1112 }
1113
1114 free_augmenting_path (&augmenting_path);
1115 if (dump_file)
1116 dump_fixup_graph (dump_file, fixup_graph, "After find_max_flow()");
1117 return max_flow;
1118 }
1119
1120
1121 /* Computes the corrected edge and basic block weights using FIXUP_GRAPH
1122 after applying the find_minimum_cost_flow() routine. */
1123
1124 static void
1125 adjust_cfg_counts (fixup_graph_type *fixup_graph)
1126 {
1127 basic_block bb;
1128 edge e;
1129 edge_iterator ei;
1130 int i, j;
1131 fixup_edge_p pfedge, pfedge_n;
1132
1133 gcc_assert (fixup_graph);
1134
1135 if (dump_file)
1136 fprintf (dump_file, "\nadjust_cfg_counts():\n");
1137
1138 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
1139 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
1140 {
1141 i = 2 * bb->index;
1142
1143 /* Fixup BB. */
1144 if (dump_file)
1145 fprintf (dump_file,
1146 "BB%d: %" PRId64 "", bb->index, bb->count);
1147
1148 pfedge = find_fixup_edge (fixup_graph, i, i + 1);
1149 if (pfedge->flow)
1150 {
1151 bb->count += pfedge->flow;
1152 if (dump_file)
1153 {
1154 fprintf (dump_file, " + %" PRId64 "(",
1155 pfedge->flow);
1156 print_edge (dump_file, fixup_graph, i, i + 1);
1157 fprintf (dump_file, ")");
1158 }
1159 }
1160
1161 pfedge_n =
1162 find_fixup_edge (fixup_graph, i + 1, pfedge->norm_vertex_index);
1163 /* Deduct flow from normalized reverse edge. */
1164 if (pfedge->norm_vertex_index && pfedge_n->flow)
1165 {
1166 bb->count -= pfedge_n->flow;
1167 if (dump_file)
1168 {
1169 fprintf (dump_file, " - %" PRId64 "(",
1170 pfedge_n->flow);
1171 print_edge (dump_file, fixup_graph, i + 1,
1172 pfedge->norm_vertex_index);
1173 fprintf (dump_file, ")");
1174 }
1175 }
1176 if (dump_file)
1177 fprintf (dump_file, " = %" PRId64 "\n", bb->count);
1178
1179 /* Fixup edge. */
1180 FOR_EACH_EDGE (e, ei, bb->succs)
1181 {
1182 /* Treat edges with ignore attribute set as if they don't exist. */
1183 if (EDGE_INFO (e) && EDGE_INFO (e)->ignore)
1184 continue;
1185
1186 j = 2 * e->dest->index;
1187 if (dump_file)
1188 fprintf (dump_file, "%d->%d: %" PRId64 "",
1189 bb->index, e->dest->index, e->count);
1190
1191 pfedge = find_fixup_edge (fixup_graph, i + 1, j);
1192
1193 if (bb->index != e->dest->index)
1194 {
1195 /* Non-self edge. */
1196 if (pfedge->flow)
1197 {
1198 e->count += pfedge->flow;
1199 if (dump_file)
1200 {
1201 fprintf (dump_file, " + %" PRId64 "(",
1202 pfedge->flow);
1203 print_edge (dump_file, fixup_graph, i + 1, j);
1204 fprintf (dump_file, ")");
1205 }
1206 }
1207
1208 pfedge_n =
1209 find_fixup_edge (fixup_graph, j, pfedge->norm_vertex_index);
1210 /* Deduct flow from normalized reverse edge. */
1211 if (pfedge->norm_vertex_index && pfedge_n->flow)
1212 {
1213 e->count -= pfedge_n->flow;
1214 if (dump_file)
1215 {
1216 fprintf (dump_file, " - %" PRId64 "(",
1217 pfedge_n->flow);
1218 print_edge (dump_file, fixup_graph, j,
1219 pfedge->norm_vertex_index);
1220 fprintf (dump_file, ")");
1221 }
1222 }
1223 }
1224 else
1225 {
1226 /* Handle self edges. Self edge is split with a normalization
1227 vertex. Here i=j. */
1228 pfedge = find_fixup_edge (fixup_graph, j, i + 1);
1229 pfedge_n =
1230 find_fixup_edge (fixup_graph, i + 1, pfedge->norm_vertex_index);
1231 e->count += pfedge_n->flow;
1232 bb->count += pfedge_n->flow;
1233 if (dump_file)
1234 {
1235 fprintf (dump_file, "(self edge)");
1236 fprintf (dump_file, " + %" PRId64 "(",
1237 pfedge_n->flow);
1238 print_edge (dump_file, fixup_graph, i + 1,
1239 pfedge->norm_vertex_index);
1240 fprintf (dump_file, ")");
1241 }
1242 }
1243
1244 if (bb->count)
1245 e->probability = REG_BR_PROB_BASE * e->count / bb->count;
1246 if (dump_file)
1247 fprintf (dump_file, " = %" PRId64 "\t(%.1f%%)\n",
1248 e->count, e->probability * 100.0 / REG_BR_PROB_BASE);
1249 }
1250 }
1251
1252 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count =
1253 sum_edge_counts (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
1254 EXIT_BLOCK_PTR_FOR_FN (cfun)->count =
1255 sum_edge_counts (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
1256
1257 /* Compute edge probabilities. */
1258 FOR_ALL_BB_FN (bb, cfun)
1259 {
1260 if (bb->count)
1261 {
1262 FOR_EACH_EDGE (e, ei, bb->succs)
1263 e->probability = REG_BR_PROB_BASE * e->count / bb->count;
1264 }
1265 else
1266 {
1267 int total = 0;
1268 FOR_EACH_EDGE (e, ei, bb->succs)
1269 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
1270 total++;
1271 if (total)
1272 {
1273 FOR_EACH_EDGE (e, ei, bb->succs)
1274 {
1275 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
1276 e->probability = REG_BR_PROB_BASE / total;
1277 else
1278 e->probability = 0;
1279 }
1280 }
1281 else
1282 {
1283 total += EDGE_COUNT (bb->succs);
1284 FOR_EACH_EDGE (e, ei, bb->succs)
1285 e->probability = REG_BR_PROB_BASE / total;
1286 }
1287 }
1288 }
1289
1290 if (dump_file)
1291 {
1292 fprintf (dump_file, "\nCheck %s() CFG flow conservation:\n",
1293 current_function_name ());
1294 FOR_EACH_BB_FN (bb, cfun)
1295 {
1296 if ((bb->count != sum_edge_counts (bb->preds))
1297 || (bb->count != sum_edge_counts (bb->succs)))
1298 {
1299 fprintf (dump_file,
1300 "BB%d(%" PRId64 ") **INVALID**: ",
1301 bb->index, bb->count);
1302 fprintf (stderr,
1303 "******** BB%d(%" PRId64
1304 ") **INVALID**: \n", bb->index, bb->count);
1305 fprintf (dump_file, "in_edges=%" PRId64 " ",
1306 sum_edge_counts (bb->preds));
1307 fprintf (dump_file, "out_edges=%" PRId64 "\n",
1308 sum_edge_counts (bb->succs));
1309 }
1310 }
1311 }
1312 }
1313
1314
1315 /* Implements the negative cycle canceling algorithm to compute a minimum cost
1316 flow.
1317 Algorithm:
1318 1. Find maximal flow.
1319 2. Form residual network
1320 3. Repeat:
1321 While G contains a negative cost cycle C, reverse the flow on the found cycle
1322 by the minimum residual capacity in that cycle.
1323 4. Form the minimal cost flow
1324 f(u,v) = rf(v, u)
1325 Input:
1326 FIXUP_GRAPH - Initial fixup graph.
1327 The flow field is modified to represent the minimum cost flow. */
1328
1329 static void
1330 find_minimum_cost_flow (fixup_graph_type *fixup_graph)
1331 {
1332 /* Holds the index of predecessor in path. */
1333 int *pred;
1334 /* Used to hold the minimum cost cycle. */
1335 int *cycle;
1336 /* Used to record the number of iterations of cancel_negative_cycle. */
1337 int iteration;
1338 /* Vector d[i] holds the minimum cost of path from i to sink. */
1339 gcov_type *d;
1340 int fnum_vertices;
1341 int new_exit_index;
1342 int new_entry_index;
1343
1344 gcc_assert (fixup_graph);
1345 fnum_vertices = fixup_graph->num_vertices;
1346 new_exit_index = fixup_graph->new_exit_index;
1347 new_entry_index = fixup_graph->new_entry_index;
1348
1349 find_max_flow (fixup_graph, new_entry_index, new_exit_index);
1350
1351 /* Initialize the structures for find_negative_cycle(). */
1352 pred = (int *) xcalloc (fnum_vertices, sizeof (int));
1353 d = (gcov_type *) xcalloc (fnum_vertices, sizeof (gcov_type));
1354 cycle = (int *) xcalloc (fnum_vertices, sizeof (int));
1355
1356 /* Repeatedly find and cancel negative cost cycles, until
1357 no more negative cycles exist. This also updates the flow field
1358 to represent the minimum cost flow so far. */
1359 iteration = 0;
1360 while (cancel_negative_cycle (fixup_graph, pred, d, cycle))
1361 {
1362 iteration++;
1363 if (iteration > MAX_ITER (fixup_graph->num_vertices,
1364 fixup_graph->num_edges))
1365 break;
1366 }
1367
1368 if (dump_file)
1369 dump_fixup_graph (dump_file, fixup_graph,
1370 "After find_minimum_cost_flow()");
1371
1372 /* Cleanup structures. */
1373 free (pred);
1374 free (d);
1375 free (cycle);
1376 }
1377
1378
1379 /* Compute the sum of the edge counts in TO_EDGES. */
1380
1381 gcov_type
1382 sum_edge_counts (vec<edge, va_gc> *to_edges)
1383 {
1384 gcov_type sum = 0;
1385 edge e;
1386 edge_iterator ei;
1387
1388 FOR_EACH_EDGE (e, ei, to_edges)
1389 {
1390 if (EDGE_INFO (e) && EDGE_INFO (e)->ignore)
1391 continue;
1392 sum += e->count;
1393 }
1394 return sum;
1395 }
1396
1397
1398 /* Main routine. Smoothes the initial assigned basic block and edge counts using
1399 a minimum cost flow algorithm, to ensure that the flow consistency rule is
1400 obeyed: sum of outgoing edges = sum of incoming edges for each basic
1401 block. */
1402
1403 void
1404 mcf_smooth_cfg (void)
1405 {
1406 fixup_graph_type fixup_graph;
1407 memset (&fixup_graph, 0, sizeof (fixup_graph));
1408 create_fixup_graph (&fixup_graph);
1409 find_minimum_cost_flow (&fixup_graph);
1410 adjust_cfg_counts (&fixup_graph);
1411 delete_fixup_graph (&fixup_graph);
1412 }