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