- more irp_resource_reserved()
[libfirm] / ir / be / bepressurestat.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       Register Pressure Statistics.
23  * @author      Adam M. Szalkowski
24  * @date        06.04.2006
25  * @version     $Id$
26  */
27 #include "config.h"
28
29 #include <math.h>
30
31 #include "hashptr.h"
32 #include "debug.h"
33 #include "obst.h"
34 #include "set.h"
35 #include "list.h"
36 #include "pmap.h"
37
38 #include "irprintf.h"
39 #include "irgwalk.h"
40 #include "irdump_t.h"
41 #include "irnode_t.h"
42 #include "ircons_t.h"
43 #include "irloop_t.h"
44 #include "phiclass.h"
45 #include "iredges.h"
46 #include "execfreq.h"
47 #include "irtools.h"
48
49 #include "be_t.h"
50 #include "belive_t.h"
51 #include "besched_t.h"
52 #include "beirgmod.h"
53 #include "bearch_t.h"
54 #include "benode_t.h"
55 #include "beutil.h"
56 #include "bespill.h"
57 #include "beirg_t.h"
58
59 #define MAXPRESSURE 128
60
61 typedef struct _regpressure_ana_t {
62         const arch_register_class_t  *cls;
63         const be_lv_t                *lv;
64         unsigned int                 *stat;
65         DEBUG_ONLY(firm_dbg_module_t *dbg);
66 } regpressure_ana_t;
67
68 static inline int has_reg_class(const regpressure_ana_t *ra, const ir_node *irn)
69 {
70         return arch_irn_consider_in_reg_alloc(ra->cls, irn);
71 }
72
73 static inline int regpressure(pset *live) {
74         int pressure = pset_count(live);
75         return MIN(pressure, MAXPRESSURE);
76 }
77
78 static void
79 regpressureanawalker(ir_node *bb, void *data)
80 {
81         regpressure_ana_t  *ra   = data;
82         pset               *live = pset_new_ptr_default();
83         const ir_node      *irn;
84         unsigned int       *stat = ra->stat;
85         int                i;
86         const be_lv_t      *lv   = ra->lv;
87
88         be_lv_foreach(lv, bb, be_lv_state_end, i) {
89                 ir_node *value = be_lv_get_irn(lv, bb, i);
90                 if (has_reg_class(ra, value)) {
91                         pset_insert_ptr(live, value);
92                 }
93         }
94         stat[regpressure(live)]++;
95
96         sched_foreach_reverse(bb, irn) {
97
98                 if (is_Phi(irn)) break;
99
100                 if (has_reg_class(ra, irn)) {
101                         pset_remove_ptr(live, irn);
102                 }
103
104                 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
105                         ir_node  *arg = get_irn_n(irn, i);
106
107                         if (has_reg_class(ra, arg)) {
108                                 pset_insert_ptr(live, arg);
109                         }
110                 }
111
112                 if (! is_Proj(irn))
113                         stat[regpressure(live)]++;
114         }
115 }
116
117 void be_analyze_regpressure(be_irg_t *birg, const arch_register_class_t *cls, const char *suffix)
118 {
119         regpressure_ana_t ra;
120         unsigned int      stat[MAXPRESSURE+1];
121         unsigned int      i;
122         char              fname[256];
123         FILE              *f;
124         ir_graph          *irg = be_get_birg_irg(birg);
125
126         ir_snprintf(fname, sizeof(fname), "%F_%s%s_pressure.stat", irg, cls->name, suffix);
127         f = fopen(fname, "w");
128         assert(f);
129
130         be_liveness_assure_sets(be_assure_liveness(birg));
131
132         FIRM_DBG_REGISTER(ra.dbg, "firm.be.regpressureana");
133
134         ra.lv   = be_get_birg_liveness(birg);
135         ra.cls  = cls;
136         ra.stat = stat;
137
138         memset(stat, 0, sizeof(stat));
139
140         irg_block_walk_graph(irg, regpressureanawalker, NULL, &ra);
141
142         for (i = 0; i <= MAXPRESSURE; ++i) {
143                 fprintf(f, "%d\n", stat[i]);
144         }
145
146         fclose(f);
147 }