2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Copy node statistics.
23 * @author Daniel Grund
34 #include "iredges_t.h"
35 #include "irnodeset.h"
37 #include "bechordal_t.h"
40 #include "becopyopt_t.h"
41 #include "becopystat.h"
44 #include "beintlive_t.h"
46 #define DEBUG_LVL SET_LEVEL_1
47 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
50 #define MAX_CLS_SIZE 20
51 #define MAX_CLS_PHIS 20
54 * For an explanation of these values see the code of copystat_dump_pretty
57 /* FROM HERE: PROBLEM CHARACTERIZATION */
63 I_PHI_CNT, /* number of phi nodes */
64 I_PHI_ARG_CNT, /* number of arguments of phis */
65 I_PHI_ARG_SELF, /* number of arguments of phis being the phi itself */
66 I_PHI_ARG_CONST, /* number of arguments of phis being consts */
67 I_PHI_ARG_PRED, /* ... being defined in a cf-pred */
68 I_PHI_ARG_GLOB, /* ... being defined elsewhere */
70 I_PHI_ARITY_E = I_PHI_ARITY_S+MAX_ARITY,
73 I_CPY_CNT, /* number of copynodes */
76 I_CLS_CNT, /* number of phi classes */
77 I_CLS_IF_FREE, /* number of pc having no interference */
78 I_CLS_IF_MAX, /* number of possible interferences in all classes */
79 I_CLS_IF_CNT, /* number of actual interferences in all classes */
81 I_CLS_SIZE_E = I_CLS_SIZE_S+MAX_CLS_SIZE,
83 I_CLS_PHIS_E = I_CLS_PHIS_S+MAX_CLS_PHIS,
85 /* FROM HERE: RESULT VLAUES */
86 /* all of them are external set */
89 I_HEUR_TIME, /* solving time in milli seconds */
90 I_ILP_TIME, /* solving time in milli seconds */
93 I_ILP_ITER, /* number of simplex iterations */
95 /* copy instructions */
96 I_COPIES_MAX, /* max possible costs of copies*/
97 I_COPIES_INIT, /* number of copies in initial allocation */
98 I_COPIES_HEUR, /* number of copies after heuristic */
99 I_COPIES_5SEC, /* number of copies after ilp with max n sec */
100 I_COPIES_30SEC, /* number of copies after ilp with max n sec */
101 I_COPIES_OPT, /* number of copies after ilp */
102 I_COPIES_IF, /* number of copies inevitable due to root-arg-interf */
108 * Holds current values. Values are added till next copystat_reset
110 static int curr_vals[ASIZE];
112 static ir_nodeset_t *all_phi_nodes;
113 static ir_nodeset_t *all_copy_nodes;
115 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copystat)
116 void be_init_copystat(void)
118 FIRM_DBG_REGISTER(dbg, "firm.be.copystat");
120 all_phi_nodes = ir_nodeset_new(64);
121 all_copy_nodes = ir_nodeset_new(64);
122 memset(curr_vals, 0, sizeof(curr_vals));
125 BE_REGISTER_MODULE_DESTRUCTOR(be_quit_copystat)
126 void be_quit_copystat(void)
128 if (all_phi_nodes != NULL) {
129 ir_nodeset_del(all_phi_nodes);
130 all_phi_nodes = NULL;
132 if (all_copy_nodes != NULL) {
133 ir_nodeset_del(all_copy_nodes);
134 all_copy_nodes = NULL;
139 * @return 1 if the block at pos @p pos removed a critical edge
142 static inline int was_edge_critical(const ir_node *bl, int pos)
144 const ir_edge_t *edge;
145 const ir_node *bl_at_pos, *bl_before;
146 assert(is_Block(bl));
148 /* Does bl have several predecessors ?*/
149 if (get_irn_arity(bl) <= 1)
152 /* Does the pred have exactly one predecessor */
153 bl_at_pos = get_irn_n(bl, pos);
154 if (get_irn_arity(bl_at_pos) != 1)
157 /* Does the pred of the pred have several successors */
158 bl_before = get_irn_n(bl_at_pos, 0);
159 edge = get_block_succ_first(bl_before);
160 return get_block_succ_next(bl_before, edge) ? 1 : 0;
163 void copystat_add_max_costs(int costs)
165 curr_vals[I_COPIES_MAX] += costs;
167 void copystat_add_inevit_costs(int costs)
169 curr_vals[I_COPIES_IF] += costs;
171 void copystat_add_init_costs(int costs)
173 curr_vals[I_COPIES_INIT] += costs;
175 void copystat_add_heur_costs(int costs)
177 curr_vals[I_COPIES_HEUR] += costs;
179 void copystat_add_opt_costs(int costs)
181 curr_vals[I_COPIES_OPT] += costs;
183 void copystat_add_heur_time(int time)
185 curr_vals[I_HEUR_TIME] += time;
188 void copystat_add_ilp_5_sec_costs(int costs)
190 curr_vals[I_COPIES_5SEC] += costs;
192 void copystat_add_ilp_30_sec_costs(int costs)
194 curr_vals[I_COPIES_30SEC] += costs;
196 void copystat_add_ilp_time(int time)
198 curr_vals[I_ILP_TIME] += time;
200 void copystat_add_ilp_vars(int vars)
202 curr_vals[I_ILP_VARS] += vars;
204 void copystat_add_ilp_csts(int csts)
206 curr_vals[I_ILP_CSTR] += csts;
208 void copystat_add_ilp_iter(int iters)
210 curr_vals[I_ILP_ITER] += iters;
214 * Opens a file named base.ext with the mode mode.
216 static FILE *be_ffopen(const char *base, const char *ext, const char *mode)
221 snprintf(buf, sizeof(buf), "%s.%s", base, ext);
222 buf[sizeof(buf) - 1] = '\0';
223 if (! (out = fopen(buf, mode))) {
224 fprintf(stderr, "Cannot open file %s in mode %s\n", buf, mode);
230 void copystat_dump(ir_graph *irg)
236 snprintf(buf, sizeof(buf), "%s__%s", get_irp_name(), get_entity_name(get_irg_entity(irg)));
237 buf[sizeof(buf) - 1] = '\0';
238 out = be_ffopen(buf, "stat", "wt");
240 fprintf(out, "%d\n", (int)ASIZE);
241 for (i = 0; i < ASIZE; i++) {
243 if (i >= I_PHI_ARITY_S && i <= I_PHI_ARITY_E)
244 fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_PHI_CNT]);
245 else if (i >= I_CLS_SIZE_S && i <= I_CLS_SIZE_E)
246 fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_CLS_CNT]);
249 fprintf(out, "%i\n", curr_vals[i]);
255 void copystat_dump_pretty(ir_graph *irg)
261 snprintf(buf, sizeof(buf), "%s__%s", get_irp_name(), get_entity_name(get_irg_entity(irg)));
262 buf[sizeof(buf) - 1] = '\0';
263 out = be_ffopen(buf, "pstat", "wt");
265 fprintf(out, "Nodes %4d\n", curr_vals[I_ALL_NODES]);
266 fprintf(out, "Blocks %4d\n", curr_vals[I_BLOCKS]);
267 fprintf(out, "CopyIrn %4d\n", curr_vals[I_CPY_CNT]);
269 fprintf(out, "\nPhis %4d\n", curr_vals[I_PHI_CNT]);
270 fprintf(out, "... argument types\n");
271 fprintf(out, " Total %4d\n", curr_vals[I_PHI_ARG_CNT]);
272 fprintf(out, " Self %4d\n", curr_vals[I_PHI_ARG_SELF]);
273 fprintf(out, " Constants %4d\n", curr_vals[I_PHI_ARG_CONST]);
274 fprintf(out, " CF-Pred %4d\n", curr_vals[I_PHI_ARG_PRED]);
275 fprintf(out, " Others %4d\n", curr_vals[I_PHI_ARG_GLOB]);
276 fprintf(out, "... arities\n");
277 for (i = I_PHI_ARITY_S; i<=I_PHI_ARITY_E; i++)
278 fprintf(out, " %2i %4d\n", i-I_PHI_ARITY_S, curr_vals[i]);
280 fprintf(out, "\nPhi classes %4d\n", curr_vals[I_CLS_CNT]);
281 fprintf(out, " compl. free %4d\n", curr_vals[I_CLS_IF_FREE]);
282 fprintf(out, " inner intf. %4d / %4d\n", curr_vals[I_CLS_IF_CNT], curr_vals[I_CLS_IF_MAX]);
283 fprintf(out, "... sizes\n");
284 for (i = I_CLS_SIZE_S; i<=I_CLS_SIZE_E; i++)
285 fprintf(out, " %2i %4d\n", i-I_CLS_SIZE_S, curr_vals[i]);
286 fprintf(out, "... contained phis\n");
287 for (i = I_CLS_PHIS_S; i<=I_CLS_PHIS_E; i++)
288 fprintf(out, " %2i %4d\n", i-I_CLS_PHIS_S, curr_vals[i]);
290 fprintf(out, "\nILP stat\n");
291 fprintf(out, " Time %8d\n", curr_vals[I_ILP_TIME]);
292 fprintf(out, " Iter %8d\n", curr_vals[I_ILP_ITER]);
294 fprintf(out, "\nCopy stat\n");
295 fprintf(out, " Max %4d\n", curr_vals[I_COPIES_MAX]);
296 fprintf(out, " Init %4d\n", curr_vals[I_COPIES_INIT]);
297 fprintf(out, " Heur %4d\n", curr_vals[I_COPIES_HEUR]);
298 fprintf(out, " Opt %4d\n", curr_vals[I_COPIES_OPT]);
299 fprintf(out, " Intf %4d\n", curr_vals[I_COPIES_IF]);