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