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