my colleagues didn't like the existence of
[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 <math.h>
14
15 #include "field_temperature.h"
16
17 #include "trouts.h"
18 #include "execution_frequency.h"
19
20 #include "irnode_t.h"
21 #include "irgraph_t.h"
22 #include "irprog_t.h"
23 #include "entity_t.h"
24 #include "irgwalk.h"
25
26 #include "array.h"
27
28 /* *************************************************************************** */
29 /* initialize, global variables.                                               */
30 /* *************************************************************************** */
31
32 /* *************************************************************************** */
33 /*   Access routines for irnodes                                               */
34 /* *************************************************************************** */
35
36 /* The entities that can be accessed by this Sel node. */
37 int get_Sel_n_accessed_entities(ir_node *sel) {
38   return 1;
39 }
40
41 entity *get_Sel_accessed_entity(ir_node *sel, int pos) {
42   return get_Sel_entity(sel);
43 }
44
45 /* *************************************************************************** */
46 /* The heuristic                                                               */
47 /* *************************************************************************** */
48
49 int get_irn_loop_call_depth(ir_node *n) {
50   ir_graph *irg = get_irn_irg(n);
51   return get_irg_loop_depth(irg);
52 }
53
54 int get_irn_cfloop_depth(ir_node *n) {
55   ir_loop *l = get_irn_loop(get_nodes_block(n));
56   if (l)
57     return get_loop_depth(l);
58   else
59     return 0;
60 }
61
62 int get_irn_recursion_depth(ir_node *n) {
63   ir_graph *irg = get_irn_irg(n);
64   return get_irg_recursion_depth(irg);
65 }
66
67
68 /**   @@@ the second version of the heuristic. */
69 int get_weighted_loop_depth(ir_node *n) {
70   int loop_call_depth = get_irn_loop_call_depth(n);
71   int loop_depth      = get_irn_cfloop_depth(n);
72   int recursion_depth = get_irn_recursion_depth(n);
73
74   return loop_call_depth + loop_depth + recursion_depth;
75 }
76
77
78 /* *************************************************************************** */
79 /* The 2. heuristic                                                            */
80 /* *************************************************************************** */
81
82 static int default_recursion_weight = 5;
83
84 /* The final evaluation of a node.  In this function we can
85    adapt the heuristic.  Combine execution frequency with
86    recursion depth.
87    @@@ the second version of the heuristic. */
88 double get_irn_final_cost(ir_node *n) {
89   double cost_loop   = get_irn_exec_freq(n);
90   double cost_method = get_irg_method_execution_frequency(get_irn_irg(n));
91   int    rec_depth   = get_irn_recursion_depth(n);
92   double cost_rec    = 0;
93   if (rec_depth) cost_rec = pow(default_recursion_weight, rec_depth);
94   return cost_loop*(cost_method + cost_rec);
95 }
96
97 double get_type_estimated_n_instances(type *tp) {
98   int i, n_allocs = get_type_n_allocs(tp);
99   double n_instances = 0;
100   for (i = 0; i < n_allocs; ++i) {
101     ir_node *alloc = get_type_alloc(tp, i);
102     n_instances += get_irn_final_cost(alloc);
103   }
104   return n_instances;
105 }
106
107 double get_type_estimated_mem_consumption_bytes(type *tp) {
108   assert(0);
109   return 0.0;
110 }
111
112 int get_type_estimated_n_fields(type *tp) {
113   int s = 0;
114   switch(get_type_tpop_code(tp)) {
115
116   case tpo_primitive:
117   case tpo_pointer:
118   case tpo_enumeration:
119     s = 1;
120     break;
121
122   case tpo_class:
123     s = 1; /* dispatch pointer */
124     /* fall through */
125   case tpo_struct: {
126     int i, n_mem = get_compound_n_members(tp);
127     for (i = 0; i < n_mem; ++i) {
128       entity *mem = get_compound_member(tp, i);
129       if (get_entity_allocation(mem) == allocation_automatic) {
130         s += get_type_estimated_n_fields(get_entity_type(mem));
131       }
132     }
133   } break;
134
135   case tpo_array: {
136     long n_elt = DEFAULT_N_ARRAY_ELEMENTS;
137     assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
138     if ((get_irn_op(get_array_lower_bound(tp, 0)) == op_Const) &&
139         (get_irn_op(get_array_upper_bound(tp, 0)) == op_Const)   ) {
140       n_elt = get_array_upper_bound_int(tp, 0) - get_array_upper_bound_int(tp, 0);
141     }
142     s = n_elt;
143   } break;
144
145   default: DDMT(tp); assert(0);
146   }
147
148   return s;
149 }
150
151 int get_type_estimated_size_bytes(type *tp) {
152   int s = 0;
153
154   switch(get_type_tpop_code(tp)) {
155
156   case tpo_primitive:
157   case tpo_pointer:
158   case tpo_enumeration:
159     s = get_mode_size_bytes(get_type_mode(tp));
160     break;
161
162   case tpo_class:
163     s = get_mode_size_bytes(mode_P_mach); /* dispatch pointer */
164     /* fall through */
165   case tpo_struct: {
166     int i, n_mem = get_compound_n_members(tp);
167     for (i = 0; i < n_mem; ++i) {
168       entity *mem = get_compound_member(tp, i);
169       s += get_type_estimated_size_bytes(get_entity_type(mem));
170
171       if (get_entity_allocation(mem) == allocation_automatic) {
172       } /* allocation_automatic */
173     }
174   } break;
175
176   case tpo_array: {
177     int elt_s = get_type_estimated_size_bytes(get_array_element_type(tp));
178     long n_elt = DEFAULT_N_ARRAY_ELEMENTS;
179     assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
180     if ((get_irn_op(get_array_lower_bound(tp, 0)) == op_Const) &&
181         (get_irn_op(get_array_upper_bound(tp, 0)) == op_Const)   ) {
182       n_elt = get_array_upper_bound_int(tp, 0) - get_array_lower_bound_int(tp, 0);
183     }
184     s = n_elt * elt_s;
185     break;
186   }
187
188   default: DDMT(tp); assert(0);
189   }
190
191   return s;
192 }
193
194 double get_type_estimated_n_casts(type *tp) {
195   int i, n_casts = get_type_n_casts(tp);
196   double n_instances = 0;
197   for (i = 0; i < n_casts; ++i) {
198     ir_node *cast = get_type_cast(tp, i);
199     n_instances += get_irn_final_cost(cast);
200   }
201   return n_instances;
202 }
203
204 double get_class_estimated_n_upcasts(type *clss) {
205   double n_instances = 0;
206   int i, j, n_casts, n_pointertypes;
207
208   n_casts = get_type_n_casts(clss);
209   for (i = 0; i < n_casts; ++i) {
210     ir_node *cast = get_type_cast(clss, i);
211     if (get_irn_opcode(cast) != iro_Cast) continue;  /* Could be optimized away. */
212
213     if (is_Cast_upcast(cast))
214       n_instances += get_irn_final_cost(cast);
215   }
216
217   n_pointertypes = get_type_n_pointertypes_to(clss);
218   for (j = 0; j < n_pointertypes; ++j) {
219     n_instances += get_class_estimated_n_upcasts(get_type_pointertype_to(clss, j));
220   }
221
222   return n_instances;
223 }
224
225 double get_class_estimated_n_downcasts(type *clss) {
226   double n_instances = 0;
227   int i, j, n_casts, n_pointertypes;
228
229   n_casts = get_type_n_casts(clss);
230   for (i = 0; i < n_casts; ++i) {
231     ir_node *cast = get_type_cast(clss, i);
232     if (get_irn_opcode(cast) != iro_Cast) continue;  /* Could be optimized away. */
233
234     if (is_Cast_downcast(cast))
235       n_instances += get_irn_final_cost(cast);
236   }
237
238   n_pointertypes = get_type_n_pointertypes_to(clss);
239   for (j = 0; j < n_pointertypes; ++j) {
240     n_instances += get_class_estimated_n_downcasts(get_type_pointertype_to(clss, j));
241   }
242
243   return n_instances;
244 }
245
246
247 double get_class_estimated_dispatch_writes(type *clss) {
248   return get_type_estimated_n_instances(clss);
249 }
250
251 /** Returns the number of reads of the dispatch pointer. */
252 double get_class_estimated_dispatch_reads (type *clss) {
253   int i, n_mems = get_class_n_members(clss);
254   double n_calls = 0;
255   for (i = 0; i < n_mems; ++i) {
256     entity *mem = get_class_member(clss, i);
257     n_calls += get_entity_estimated_n_dyncalls(mem);
258   }
259   return n_calls;
260 }
261
262 double get_class_estimated_n_dyncalls(type *clss) {
263   return get_class_estimated_dispatch_reads(clss) +
264          get_class_estimated_dispatch_writes(clss);
265 }
266
267 double get_entity_estimated_n_loads(entity *ent) {
268   int i, n_acc = get_entity_n_accesses(ent);
269   double n_loads = 0;
270   for (i = 0; i < n_acc; ++i) {
271     ir_node *acc = get_entity_access(ent, i);
272     if (get_irn_op(acc) == op_Load) {
273       n_loads += get_irn_final_cost(acc);
274     }
275   }
276   return n_loads;
277 }
278
279 double get_entity_estimated_n_stores(entity *ent) {
280   int i, n_acc = get_entity_n_accesses(ent);
281   double n_stores = 0;
282   for (i = 0; i < n_acc; ++i) {
283     ir_node *acc = get_entity_access(ent, i);
284     if (get_irn_op(acc) == op_Store)
285       n_stores += get_irn_final_cost(acc);
286   }
287   return n_stores;
288 }
289
290 /* @@@ Should we evaluate the callee array?  */
291 double get_entity_estimated_n_calls(entity *ent) {
292   int i, n_acc = get_entity_n_accesses(ent);
293   double n_calls = 0;
294   for (i = 0; i < n_acc; ++i) {
295     ir_node *acc = get_entity_access(ent, i);
296     if (get_irn_op(acc) == op_Call)
297
298       n_calls += get_irn_final_cost(acc);
299   }
300   return n_calls;
301 }
302
303 double get_entity_estimated_n_dyncalls(entity *ent) {
304   int i, n_acc = get_entity_n_accesses(ent);
305   double n_calls = 0;
306   for (i = 0; i < n_acc; ++i) {
307     ir_node *acc = get_entity_access(ent, i);
308
309     /* Call->Sel(ent) combination */
310     if ((get_irn_op(acc) == op_Call)  &&
311         (get_irn_op(get_Call_ptr(acc)) == op_Sel)) {
312       n_calls += get_irn_final_cost(acc);
313
314     /* MemOp->Sel combination for static, overwritten entities */
315     } else if (is_memop(acc) && (get_irn_op(get_memop_ptr(acc)) == op_Sel)) {
316       entity *ent = get_Sel_entity(get_memop_ptr(acc));
317       if (is_Class_type(get_entity_owner(ent))) {
318         /* We might call this for inner entities in compounds. */
319         if (get_entity_n_overwrites(ent) > 0 ||
320             get_entity_n_overwrittenby(ent) > 0) {
321           n_calls += get_irn_final_cost(acc);
322         }
323       }
324     }
325
326   }
327   return n_calls;
328 }
329
330 /* ------------------------------------------------------------------------- */
331 /* Auxiliary                                                                 */
332 /* ------------------------------------------------------------------------- */
333
334 int is_jack_rts_name(ident *name) {
335   return  0;
336   if (id_is_prefix(new_id_from_str("java/"), name)) return 1;
337   if (id_is_prefix(new_id_from_str("["),     name)) return 1;
338   if (id_is_prefix(new_id_from_str("gnu/"),  name)) return 1;
339   if (id_is_prefix(new_id_from_str("java/"), name)) return 1;
340   if (id_is_prefix(new_id_from_str("CStringToCoreString"), name)) return 1;
341
342   return 0;
343 }
344
345
346 int is_jack_rts_class(type *t) {
347   ident *name = get_type_ident(t);
348   return is_jack_rts_name(name);
349 }
350
351 #include "entity_t.h"  // for the assertion.
352
353 int is_jack_rts_entity(entity *e) {
354   ident *name;
355
356   assert(e->ld_name);
357   name = get_entity_ld_ident(e);
358
359   return is_jack_rts_name(name);
360 }