becopyheur4: Clean up co_mst_irn_init().
[libfirm] / ir / be / becopystat.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief       Copy node statistics.
9  * @author      Daniel Grund
10  * @date        19.04.2005
11  */
12 #include "config.h"
13
14 #include <string.h>
15
16 #include "timing.h"
17 #include "irgraph.h"
18 #include "irgwalk.h"
19 #include "irprog.h"
20 #include "iredges_t.h"
21 #include "irnodeset.h"
22
23 #include "bechordal_t.h"
24 #include "benode.h"
25 #include "beutil.h"
26 #include "becopyopt_t.h"
27 #include "becopystat.h"
28 #include "bemodule.h"
29 #include "beintlive_t.h"
30
31 #define DEBUG_LVL SET_LEVEL_1
32 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
33
34 #define MAX_ARITY 20
35 #define MAX_CLS_SIZE 20
36 #define MAX_CLS_PHIS 20
37
38 /**
39  * For an explanation of these values see the code of copystat_dump_pretty
40  */
41 enum vals_t {
42         /* FROM HERE: PROBLEM CHARACTERIZATION */
43
44         I_ALL_NODES = 0,
45         I_BLOCKS,
46
47         /* phi nodes */
48         I_PHI_CNT,          /* number of phi nodes */
49         I_PHI_ARG_CNT,      /* number of arguments of phis */
50         I_PHI_ARG_SELF,     /* number of arguments of phis being the phi itself */
51         I_PHI_ARG_CONST,    /* number of arguments of phis being consts */
52         I_PHI_ARG_PRED,     /* ... being defined in a cf-pred */
53         I_PHI_ARG_GLOB,     /* ... being defined elsewhere */
54         I_PHI_ARITY_S,
55         I_PHI_ARITY_E    = I_PHI_ARITY_S+MAX_ARITY,
56
57         /* copy nodes */
58         I_CPY_CNT,          /* number of copynodes */
59
60         /* phi classes */
61         I_CLS_CNT,          /* number of phi classes */
62         I_CLS_IF_FREE,      /* number of pc having no interference */
63         I_CLS_IF_MAX,       /* number of possible interferences in all classes */
64         I_CLS_IF_CNT,       /* number of actual interferences in all classes */
65         I_CLS_SIZE_S,
66         I_CLS_SIZE_E = I_CLS_SIZE_S+MAX_CLS_SIZE,
67         I_CLS_PHIS_S,
68         I_CLS_PHIS_E = I_CLS_PHIS_S+MAX_CLS_PHIS,
69
70         /* FROM HERE: RESULT VLAUES */
71         /* all of them are external set */
72
73         /* ilp values */
74         I_HEUR_TIME,        /* solving time in milli seconds */
75         I_ILP_TIME,         /* solving time in milli seconds */
76         I_ILP_VARS,
77         I_ILP_CSTR,
78         I_ILP_ITER,         /* number of simplex iterations */
79
80         /* copy instructions */
81         I_COPIES_MAX,       /* max possible costs of copies*/
82         I_COPIES_INIT,      /* number of copies in initial allocation */
83         I_COPIES_HEUR,      /* number of copies after heuristic */
84         I_COPIES_5SEC,      /* number of copies after ilp with max n sec */
85         I_COPIES_30SEC,     /* number of copies after ilp with max n sec */
86         I_COPIES_OPT,       /* number of copies after ilp */
87         I_COPIES_IF,        /* number of copies inevitable due to root-arg-interf */
88
89         ASIZE
90 };
91
92 /**
93  * Holds current values. Values are added till next copystat_reset
94  */
95 static int curr_vals[ASIZE];
96
97 static ir_nodeset_t *all_phi_nodes;
98 static ir_nodeset_t *all_copy_nodes;
99
100 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copystat)
101 void be_init_copystat(void)
102 {
103         FIRM_DBG_REGISTER(dbg, "firm.be.copystat");
104
105         all_phi_nodes  = ir_nodeset_new(64);
106         all_copy_nodes = ir_nodeset_new(64);
107         memset(curr_vals, 0, sizeof(curr_vals));
108 }
109
110 BE_REGISTER_MODULE_DESTRUCTOR(be_quit_copystat)
111 void be_quit_copystat(void)
112 {
113         if (all_phi_nodes != NULL) {
114                 ir_nodeset_del(all_phi_nodes);
115                 all_phi_nodes = NULL;
116         }
117         if (all_copy_nodes != NULL) {
118                 ir_nodeset_del(all_copy_nodes);
119                 all_copy_nodes = NULL;
120         }
121 }
122
123 /**
124  * @return 1 if the block at pos @p pos removed a critical edge
125  *         0 else
126  */
127 static inline int was_edge_critical(const ir_node *bl, int pos)
128 {
129         const ir_edge_t *edge;
130         const ir_node *bl_at_pos, *bl_before;
131
132         /* Does bl have several predecessors ?*/
133         if (get_Block_n_cfgpreds(bl) <= 1)
134                 return 0;
135
136         /* Does the pred have exactly one predecessor */
137         bl_at_pos = get_irn_n(bl, pos);
138         if (get_irn_arity(bl_at_pos) != 1)
139                 return 0;
140
141         /* Does the pred of the pred have several successors */
142         bl_before = get_irn_n(bl_at_pos, 0);
143         edge = get_block_succ_first(bl_before);
144         return get_block_succ_next(bl_before, edge) ? 1 : 0;
145 }
146
147 void copystat_add_max_costs(int costs)
148 {
149         curr_vals[I_COPIES_MAX] += costs;
150 }
151 void copystat_add_inevit_costs(int costs)
152 {
153         curr_vals[I_COPIES_IF] += costs;
154 }
155 void copystat_add_init_costs(int costs)
156 {
157         curr_vals[I_COPIES_INIT] += costs;
158 }
159 void copystat_add_heur_costs(int costs)
160 {
161         curr_vals[I_COPIES_HEUR] += costs;
162 }
163 void copystat_add_opt_costs(int costs)
164 {
165         curr_vals[I_COPIES_OPT] += costs;
166 }
167 void copystat_add_heur_time(int time)
168 {
169         curr_vals[I_HEUR_TIME] += time;
170 }
171
172 void copystat_add_ilp_5_sec_costs(int costs)
173 {
174         curr_vals[I_COPIES_5SEC] += costs;
175 }
176 void copystat_add_ilp_30_sec_costs(int costs)
177 {
178         curr_vals[I_COPIES_30SEC] += costs;
179 }
180 void copystat_add_ilp_time(int time)
181 {
182         curr_vals[I_ILP_TIME] += time;
183 }
184 void copystat_add_ilp_vars(int vars)
185 {
186         curr_vals[I_ILP_VARS] += vars;
187 }
188 void copystat_add_ilp_csts(int csts)
189 {
190         curr_vals[I_ILP_CSTR] += csts;
191 }
192 void copystat_add_ilp_iter(int iters)
193 {
194         curr_vals[I_ILP_ITER] += iters;
195 }
196
197 /**
198  * Opens a file named base.ext with the mode mode.
199  */
200 static FILE *be_ffopen(const char *base, const char *ext, const char *mode)
201 {
202         FILE *out;
203         char buf[1024];
204
205         snprintf(buf, sizeof(buf), "%s.%s", base, ext);
206         buf[sizeof(buf) - 1] = '\0';
207         if (! (out = fopen(buf, mode))) {
208                 fprintf(stderr, "Cannot open file %s in mode %s\n", buf, mode);
209                 return NULL;
210         }
211         return out;
212 }
213
214 void copystat_dump(ir_graph *irg)
215 {
216         int i;
217         char buf[1024];
218         FILE *out;
219
220         snprintf(buf, sizeof(buf), "%s__%s", get_irp_name(), get_entity_name(get_irg_entity(irg)));
221         buf[sizeof(buf) - 1] = '\0';
222         out = be_ffopen(buf, "stat", "wt");
223
224         fprintf(out, "%d\n", (int)ASIZE);
225         for (i = 0; i < ASIZE; i++) {
226                 fprintf(out, "%i\n", curr_vals[i]);
227         }
228
229         fclose(out);
230 }
231
232 void copystat_dump_pretty(ir_graph *irg)
233 {
234         int i;
235         char buf[1024];
236         FILE *out;
237
238         snprintf(buf, sizeof(buf), "%s__%s", get_irp_name(), get_entity_name(get_irg_entity(irg)));
239         buf[sizeof(buf) - 1] = '\0';
240         out = be_ffopen(buf, "pstat", "wt");
241
242         fprintf(out, "Nodes     %4d\n", curr_vals[I_ALL_NODES]);
243         fprintf(out, "Blocks    %4d\n", curr_vals[I_BLOCKS]);
244         fprintf(out, "CopyIrn   %4d\n", curr_vals[I_CPY_CNT]);
245
246         fprintf(out, "\nPhis      %4d\n", curr_vals[I_PHI_CNT]);
247         fprintf(out, "... argument types\n");
248         fprintf(out, " Total      %4d\n", curr_vals[I_PHI_ARG_CNT]);
249         fprintf(out, " Self       %4d\n", curr_vals[I_PHI_ARG_SELF]);
250         fprintf(out, " Constants  %4d\n", curr_vals[I_PHI_ARG_CONST]);
251         fprintf(out, " CF-Pred    %4d\n", curr_vals[I_PHI_ARG_PRED]);
252         fprintf(out, " Others     %4d\n", curr_vals[I_PHI_ARG_GLOB]);
253         fprintf(out, "... arities\n");
254         for (i = I_PHI_ARITY_S; i<=I_PHI_ARITY_E; i++)
255                 fprintf(out, " %2i %4d\n", i-I_PHI_ARITY_S, curr_vals[i]);
256
257         fprintf(out, "\nPhi classes   %4d\n", curr_vals[I_CLS_CNT]);
258         fprintf(out, " compl. free  %4d\n", curr_vals[I_CLS_IF_FREE]);
259         fprintf(out, " inner intf.  %4d / %4d\n", curr_vals[I_CLS_IF_CNT], curr_vals[I_CLS_IF_MAX]);
260         fprintf(out, "... sizes\n");
261         for (i = I_CLS_SIZE_S; i<=I_CLS_SIZE_E; i++)
262                 fprintf(out, " %2i %4d\n", i-I_CLS_SIZE_S, curr_vals[i]);
263         fprintf(out, "... contained phis\n");
264         for (i = I_CLS_PHIS_S; i<=I_CLS_PHIS_E; i++)
265                 fprintf(out, " %2i %4d\n", i-I_CLS_PHIS_S, curr_vals[i]);
266
267         fprintf(out, "\nILP stat\n");
268         fprintf(out, " Time %8d\n", curr_vals[I_ILP_TIME]);
269         fprintf(out, " Iter %8d\n", curr_vals[I_ILP_ITER]);
270
271         fprintf(out, "\nCopy stat\n");
272         fprintf(out, " Max  %4d\n", curr_vals[I_COPIES_MAX]);
273         fprintf(out, " Init %4d\n", curr_vals[I_COPIES_INIT]);
274         fprintf(out, " Heur %4d\n", curr_vals[I_COPIES_HEUR]);
275         fprintf(out, " Opt  %4d\n", curr_vals[I_COPIES_OPT]);
276         fprintf(out, " Intf %4d\n", curr_vals[I_COPIES_IF]);
277
278         fclose(out);
279 }