7512807071ae666b25c8d83f7f5e947e58b509ab
[libfirm] / ir / be / bepressurestat.c
1 /** vim: set sw=4 ts=4:
2  * @file   bepressurestat.c
3  * @date   2006-04-06
4  * @author Adam M. Szalkowski
5  *
6  * Register Pressure Statistics
7  *
8  * Copyright (C) 2006 Universitaet Karlsruhe
9  * Released under the GPL
10  */
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #include <math.h>
16
17 #include "hashptr.h"
18 #include "debug.h"
19 #include "obst.h"
20 #include "set.h"
21 #include "list.h"
22 #include "pmap.h"
23
24 #include "irprintf.h"
25 #include "irgwalk.h"
26 #include "irdump_t.h"
27 #include "irnode_t.h"
28 #include "ircons_t.h"
29 #include "irloop_t.h"
30 #include "phiclass.h"
31 #include "iredges.h"
32 #include "execfreq.h"
33
34 #include <libcore/lc_bitset.h>
35
36 #include "be_t.h"
37 #include "belive_t.h"
38 #include "besched_t.h"
39 #include "beirgmod.h"
40 #include "bearch.h"
41 #include "benode_t.h"
42 #include "beutil.h"
43 #include "bespillremat.h"
44 #include "bespill.h"
45
46 #include "bechordal_t.h"
47
48 #define MAXPRESSURE 128
49
50 typedef struct _regpressure_ana_t {
51         const arch_register_class_t  *cls;
52         const be_chordal_env_t       *chordal_env;
53         unsigned int                 *stat;
54         DEBUG_ONLY(firm_dbg_module_t * dbg);
55 } regpressure_ana_t;
56
57 static INLINE int
58 has_reg_class(const regpressure_ana_t * ra, const ir_node * irn)
59 {
60         return chordal_has_class(ra->chordal_env, irn);
61 }
62
63 static INLINE int
64 regpressure(pset * live)
65 {
66         int pressure = pset_count(live);
67
68         return (pressure>MAXPRESSURE)?MAXPRESSURE:pressure;
69 }
70
71 static void
72 regpressureanawalker(ir_node * bb, void * data)
73 {
74   regpressure_ana_t  *ra = data;
75   pset               *live = pset_new_ptr_default();
76   const ir_node      *irn;
77   unsigned int       *stat = ra->stat;
78   int                 i;
79   be_lv_t *lv = ra->chordal_env->birg->lv;
80
81   be_lv_foreach(lv, bb, be_lv_state_end, i) {
82     ir_node *value = be_lv_get_irn(lv, bb, i);
83     if (has_reg_class(ra, value)) {
84       pset_insert_ptr(live, value);
85     }
86   }
87   stat[regpressure(live)]++;
88
89   sched_foreach_reverse(bb, irn) {
90
91     if(is_Phi(irn)) break;
92
93     if(has_reg_class(ra, irn)) {
94       pset_remove_ptr(live, irn);
95     }
96
97     for(i=get_irn_arity(irn)-1; i>=0; --i) {
98       ir_node  *arg = get_irn_n(irn, i);
99
100       if(has_reg_class(ra, arg)) {
101                   pset_insert_ptr(live, arg);
102       }
103     }
104
105     if(!is_Proj(irn)) stat[regpressure(live)]++;
106   }
107 }
108
109 void
110 be_analyze_regpressure(const be_chordal_env_t * chordal_env, const char * suffix)
111 {
112   regpressure_ana_t   ra;
113   unsigned int        stat[MAXPRESSURE+1];
114   unsigned int        i;
115   char                fname[256];
116   FILE               *f;
117
118   ir_snprintf(fname, sizeof(fname), "%F_%s%s_pressure.stat", chordal_env->irg, chordal_env->cls->name, suffix);
119   f = fopen(fname, "w");
120   assert(f);
121
122   FIRM_DBG_REGISTER(ra.dbg, "firm.be.regpressureana");
123
124   ra.chordal_env = chordal_env;
125   ra.cls = chordal_env->cls;
126   ra.stat = stat;
127
128   memset(stat, 0, sizeof(stat));
129
130   irg_block_walk_graph(chordal_env->irg, regpressureanawalker, NULL, &ra);
131
132   for(i=0; i<=MAXPRESSURE; ++i) {
133     fprintf(f,"%d\n",stat[i]);
134   }
135
136   fclose(f);
137 }