cc4140e8b971f52aa003b2b514300ce3397bb957
[libfirm] / ir / ana / field_temperature.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ana/field_temperature.c
4  * Purpose:     Compute an estimate of field temperature, i.e., field access heuristic.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:     21.7.2004
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2004 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #include "field_temperature.h"
14
15 #include "irnode_t.h"
16 #include "irgraph_t.h"
17 #include "irprog_t.h"
18 #include "entity_t.h"
19 #include "irgwalk.h"
20
21 #include "array.h"
22
23 /* *************************************************************************** */
24 /* initialize, global variables.                                               */
25 /* *************************************************************************** */
26
27 /* A list of Load and Store operations that have no analyseable address. */
28 static ir_node **unrecognized_access = NULL;
29
30 static void add_unrecognized_access(ir_node *n) {
31   ARR_APP1(ir_node *, unrecognized_access, n);
32 }
33
34 /* *************************************************************************** */
35 /*   Access routines for entities                                              */
36 /* *************************************************************************** */
37
38 int get_entity_n_accesses(entity *ent) {
39   assert(ent && is_entity(ent));
40
41   if (!ent->accesses) { ent->accesses = NEW_ARR_F(ir_node *, 0); }
42
43   return ARR_LEN(ent->accesses);
44 }
45
46 ir_node *get_entity_access(entity *ent, int pos) {
47   assert(0 <= pos && pos < get_entity_n_accesses(ent));
48
49   return ent->accesses[pos];
50 }
51
52 void add_entity_access(entity *ent, ir_node *n) {
53   assert(ent && is_entity(ent));
54   assert(n && is_ir_node(n));
55
56   if (!ent->accesses) ent->accesses = NEW_ARR_F(ir_node *, 0);
57
58   ARR_APP1(ir_node *, ent->accesses, n);
59 }
60
61 void set_entity_access(entity *ent, int pos, ir_node *n) {
62   assert(0 <= pos && pos < get_entity_n_accesses(ent));
63   assert(n && is_ir_node(n));
64
65   ent->accesses[pos] = n;
66 }
67
68
69 /* *************************************************************************** */
70 /*   Access routines for nodes                                                 */
71 /* *************************************************************************** */
72
73 /* *************************************************************************** */
74 /*   Access routines for irnodes                                               */
75 /* *************************************************************************** */
76
77 /* The entities that can be accessed by this Sel node. */
78 int get_Sel_n_accessed_entities(ir_node *sel) {
79   return 1;
80 }
81
82 entity *get_Sel_accessed_entity(ir_node *sel, int pos) {
83   return get_Sel_entity(sel);
84 }
85
86 /* An addr node is a SymConst or a Sel. */
87 int get_addr_n_entities(ir_node *addr) {
88   int n_ents;
89
90   switch (get_irn_opcode(addr)) {
91   case iro_Sel:
92     /* Treat jack array sels? */
93     n_ents = get_Sel_n_accessed_entities(addr);
94     break;
95   case iro_SymConst:
96     if (get_SymConst_kind(addr) == symconst_addr_ent) {
97       n_ents = 1;
98       break;
99     }
100   default:
101     //assert(0 && "unexpected address expression");
102     n_ents = 0;
103   }
104
105   return n_ents;
106 }
107
108 /* An addr node is a SymConst or a Sel. */
109 entity *get_addr_entity(ir_node *addr, int pos) {
110   entity *ent;
111
112   switch (get_irn_opcode(addr)) {
113   case iro_Sel:
114     /* Treat jack array sels? */
115     assert (0 <= pos && pos < get_Sel_n_accessed_entities(addr));
116     ent = get_Sel_accessed_entity(addr, pos);
117     break;
118   case iro_SymConst:
119     if (get_SymConst_kind(addr) == symconst_addr_ent) {
120       assert(pos == 0);
121       ent = get_SymConst_entity(addr);
122       break;
123     }
124   default:
125     ent = NULL;
126   }
127
128   return ent;
129 }
130
131
132 int get_irn_loop_call_depth(ir_node *n) {
133   ir_graph *irg = get_irn_irg(n);
134   return get_irg_loop_depth(irg);
135 }
136
137 int get_irn_loop_depth(ir_node *n) {
138   get_loop_depth(get_irn_loop(get_nodes_block(n)));
139 }
140
141 int get_irn_recursion_depth(ir_node *n) {
142   ir_graph *irg = get_irn_irg(n);
143   return get_irg_recursion_depth(irg);
144 }
145
146
147 /* *************************************************************************** */
148 /* The heuristic                                                               */
149 /* *************************************************************************** */
150
151 int get_weighted_loop_depth(ir_node *n) {
152   int loop_call_depth = get_irn_loop_call_depth(n);
153   int loop_depth      = get_irn_loop_depth(n);
154   int recursion_depth = get_irn_recursion_depth(n);
155
156   return loop_call_depth + loop_depth + recursion_depth;
157 }
158
159 /* *************************************************************************** */
160 /* The analyses                                                                */
161 /* *************************************************************************** */
162
163 void init_field_temperature(void) {
164   assert(!unrecognized_access);
165   unrecognized_access = NEW_ARR_F(ir_node *, 0);
166 }
167
168
169 void chain_accesses(ir_node *n, void *env) {
170   int i, n_ents;
171   ir_node *addr;
172
173   if (is_memop(n)) {
174     addr = get_memop_ptr(n);
175   } else {
176     return;
177   }
178
179   n_ents = get_addr_n_entities(addr);
180   for (i = 0; i < n_ents; ++i) {
181     entity *ent = get_addr_entity(addr, i);
182     if (ent)
183       add_entity_access(ent, n);
184     else
185       add_unrecognized_access(n);
186   }
187 }
188
189
190 /* compute the field temperature. */
191 void compute_field_temperature(void) {
192
193   int i, n_irgs = get_irp_n_irgs();
194
195   init_field_temperature();
196
197   for (i=0; i < n_irgs; i++) {
198     current_ir_graph = get_irp_irg(i);
199     irg_walk_graph(current_ir_graph, NULL, chain_accesses, NULL);
200   }
201 }
202
203 /* free occupied memory, reset */
204 void free_field_temperature(void) {
205   DEL_ARR_F(unrecognized_access);
206   unrecognized_access = NULL;
207 }
208
209
210
211 /* *************************************************************************** */
212 /* Auxiliary                                                                   */
213 /* *************************************************************************** */
214
215
216 int is_jack_rts_class(type *t) {
217   ident *name = get_type_ident(t);
218
219   if (id_is_prefix(new_id_from_str("java/"), name)) return 1;
220   if (id_is_prefix(new_id_from_str("["), name)) return 1;
221   if (id_is_prefix(new_id_from_str("gnu/"), name)) return 1;
222   if (id_is_prefix(new_id_from_str("java/"), name)) return 1;
223
224   return 0;
225 }