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