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