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
9 * Copyright: (c) 2004 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
15 #include "field_temperature.h"
18 #include "execution_frequency.h"
21 #include "irgraph_t.h"
28 /* *************************************************************************** */
29 /* initialize, global variables. */
30 /* *************************************************************************** */
32 /* *************************************************************************** */
33 /* Access routines for irnodes */
34 /* *************************************************************************** */
36 /* The entities that can be accessed by this Sel node. */
37 int get_Sel_n_accessed_entities(ir_node *sel) {
41 entity *get_Sel_accessed_entity(ir_node *sel, int pos) {
42 return get_Sel_entity(sel);
45 /* *************************************************************************** */
47 /* *************************************************************************** */
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);
54 int get_irn_cfloop_depth(ir_node *n) {
55 ir_loop *l = get_irn_loop(get_nodes_block(n));
57 return get_loop_depth(l);
62 int get_irn_recursion_depth(ir_node *n) {
63 ir_graph *irg = get_irn_irg(n);
64 return get_irg_recursion_depth(irg);
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);
74 return loop_call_depth + loop_depth + recursion_depth;
78 /* *************************************************************************** */
79 /* The 2. heuristic */
80 /* *************************************************************************** */
82 static int default_recursion_weight = 5;
84 /* The final evaluation of a node. In this function we can
85 adapt the heuristic. Combine execution frequency with
87 @@@ the second version of the heuristic.
89 Return 0 if the node is neither in a loop nor in a recursion. */
90 double get_irn_final_cost(ir_node *n) {
91 double cost_loop = get_irn_exec_freq(n);
92 double cost_method = get_irg_method_execution_frequency(get_irn_irg(n));
93 int rec_depth = get_irn_recursion_depth(n);
97 if (get_irn_recursion_depth(n) == 0 &&
98 get_irn_loop_depth(n) == 0 &&
99 get_irg_method_loop_depth(get_irn_irg(n)) == 0)
102 if (get_weighted_loop_depth(n) == 0) return 0;
105 if (rec_depth) cost_rec = pow(default_recursion_weight, rec_depth);
106 return cost_loop*(cost_method + cost_rec);
109 double get_type_estimated_n_instances(ir_type *tp) {
110 int i, n_allocs = get_type_n_allocs(tp);
111 double n_instances = 0;
112 for (i = 0; i < n_allocs; ++i) {
113 ir_node *alloc = get_type_alloc(tp, i);
114 n_instances += get_irn_final_cost(alloc);
119 double get_type_estimated_mem_consumption_bytes(ir_type *tp) {
124 int get_type_estimated_n_fields(ir_type *tp) {
126 switch(get_type_tpop_code(tp)) {
130 case tpo_enumeration:
135 s = 1; /* dispatch pointer */
138 int i, n_mem = get_compound_n_members(tp);
139 for (i = 0; i < n_mem; ++i) {
140 entity *mem = get_compound_member(tp, i);
141 if (get_entity_allocation(mem) == allocation_automatic) {
142 s += get_type_estimated_n_fields(get_entity_type(mem));
148 long n_elt = DEFAULT_N_ARRAY_ELEMENTS;
149 assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
150 if ((get_irn_op(get_array_lower_bound(tp, 0)) == op_Const) &&
151 (get_irn_op(get_array_upper_bound(tp, 0)) == op_Const) ) {
152 n_elt = get_array_upper_bound_int(tp, 0) - get_array_upper_bound_int(tp, 0);
157 default: DDMT(tp); assert(0);
163 int get_type_estimated_size_bytes(ir_type *tp) {
166 switch(get_type_tpop_code(tp)) {
170 case tpo_enumeration:
171 s = get_mode_size_bytes(get_type_mode(tp));
175 s = get_mode_size_bytes(mode_P_data); /* dispatch pointer */
178 int i, n_mem = get_compound_n_members(tp);
179 for (i = 0; i < n_mem; ++i) {
180 entity *mem = get_compound_member(tp, i);
181 s += get_type_estimated_size_bytes(get_entity_type(mem));
183 if (get_entity_allocation(mem) == allocation_automatic) {
184 } /* allocation_automatic */
189 int elt_s = get_type_estimated_size_bytes(get_array_element_type(tp));
190 long n_elt = DEFAULT_N_ARRAY_ELEMENTS;
191 assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
192 if ((get_irn_op(get_array_lower_bound(tp, 0)) == op_Const) &&
193 (get_irn_op(get_array_upper_bound(tp, 0)) == op_Const) ) {
194 n_elt = get_array_upper_bound_int(tp, 0) - get_array_lower_bound_int(tp, 0);
200 default: DDMT(tp); assert(0);
206 double get_type_estimated_n_casts(ir_type *tp) {
207 int i, n_casts = get_type_n_casts(tp);
208 double n_instances = 0;
209 for (i = 0; i < n_casts; ++i) {
210 ir_node *cast = get_type_cast(tp, i);
211 n_instances += get_irn_final_cost(cast);
216 double get_class_estimated_n_upcasts(ir_type *clss) {
217 double n_instances = 0;
218 int i, j, n_casts, n_pointertypes;
220 n_casts = get_type_n_casts(clss);
221 for (i = 0; i < n_casts; ++i) {
222 ir_node *cast = get_type_cast(clss, i);
223 if (get_irn_opcode(cast) != iro_Cast) continue; /* Could be optimized away. */
225 if (is_Cast_upcast(cast))
226 n_instances += get_irn_final_cost(cast);
229 n_pointertypes = get_type_n_pointertypes_to(clss);
230 for (j = 0; j < n_pointertypes; ++j) {
231 n_instances += get_class_estimated_n_upcasts(get_type_pointertype_to(clss, j));
237 double get_class_estimated_n_downcasts(ir_type *clss) {
238 double n_instances = 0;
239 int i, j, n_casts, n_pointertypes;
241 n_casts = get_type_n_casts(clss);
242 for (i = 0; i < n_casts; ++i) {
243 ir_node *cast = get_type_cast(clss, i);
244 if (get_irn_opcode(cast) != iro_Cast) continue; /* Could be optimized away. */
246 if (is_Cast_downcast(cast))
247 n_instances += get_irn_final_cost(cast);
250 n_pointertypes = get_type_n_pointertypes_to(clss);
251 for (j = 0; j < n_pointertypes; ++j) {
252 n_instances += get_class_estimated_n_downcasts(get_type_pointertype_to(clss, j));
259 double get_class_estimated_dispatch_writes(ir_type *clss) {
260 return get_type_estimated_n_instances(clss);
263 /** Returns the number of reads of the dispatch pointer. */
264 double get_class_estimated_dispatch_reads (ir_type *clss) {
265 int i, n_mems = get_class_n_members(clss);
267 for (i = 0; i < n_mems; ++i) {
268 entity *mem = get_class_member(clss, i);
269 n_calls += get_entity_estimated_n_dyncalls(mem);
274 double get_class_estimated_n_dyncalls(ir_type *clss) {
275 return get_class_estimated_dispatch_reads(clss) +
276 get_class_estimated_dispatch_writes(clss);
279 double get_entity_estimated_n_loads(entity *ent) {
280 int i, n_acc = get_entity_n_accesses(ent);
282 for (i = 0; i < n_acc; ++i) {
283 ir_node *acc = get_entity_access(ent, i);
284 if (get_irn_op(acc) == op_Load) {
285 n_loads += get_irn_final_cost(acc);
291 double get_entity_estimated_n_stores(entity *ent) {
292 int i, n_acc = get_entity_n_accesses(ent);
294 for (i = 0; i < n_acc; ++i) {
295 ir_node *acc = get_entity_access(ent, i);
296 if (get_irn_op(acc) == op_Store)
297 n_stores += get_irn_final_cost(acc);
302 /* @@@ Should we evaluate the callee array? */
303 double get_entity_estimated_n_calls(entity *ent) {
304 int i, n_acc = get_entity_n_accesses(ent);
306 for (i = 0; i < n_acc; ++i) {
307 ir_node *acc = get_entity_access(ent, i);
308 if (get_irn_op(acc) == op_Call)
310 n_calls += get_irn_final_cost(acc);
315 double get_entity_estimated_n_dyncalls(entity *ent) {
316 int i, n_acc = get_entity_n_accesses(ent);
318 for (i = 0; i < n_acc; ++i) {
319 ir_node *acc = get_entity_access(ent, i);
321 /* Call->Sel(ent) combination */
322 if (is_Call(acc) && is_Sel(get_Call_ptr(acc))) {
323 n_calls += get_irn_final_cost(acc);
325 /* MemOp->Sel combination for static, overwritten entities */
326 } else if (is_memop(acc) && is_Sel(get_memop_ptr(acc))) {
327 entity *ent = get_Sel_entity(get_memop_ptr(acc));
328 if (is_Class_type(get_entity_owner(ent))) {
329 /* We might call this for inner entities in compounds. */
330 if (get_entity_n_overwrites(ent) > 0 ||
331 get_entity_n_overwrittenby(ent) > 0) {
332 n_calls += get_irn_final_cost(acc);
341 /* ------------------------------------------------------------------------- */
343 /* ------------------------------------------------------------------------- */
345 int is_jack_rts_name(ident *name) {
346 if (id_is_suffix(new_id_from_str("Exception"), name)) return 1;
347 if (id_is_suffix(new_id_from_str("Throwable"), name)) return 1;
348 if (id_is_suffix(new_id_from_str("Error"), name)) return 1;
352 if (id_is_prefix(new_id_from_str("java/"), name)) return 1;
353 if (id_is_prefix(new_id_from_str("["), name)) return 1;
354 if (id_is_prefix(new_id_from_str("gnu/"), name)) return 1;
355 if (id_is_prefix(new_id_from_str("java/"), name)) return 1;
356 if (id_is_prefix(new_id_from_str("CStringToCoreString"), name)) return 1;
362 int is_jack_rts_class(ir_type *t) {
363 ident *name = get_type_ident(t);
364 return is_jack_rts_name(name);
367 #include "entity_t.h" // for the assertion.
369 int is_jack_rts_entity(entity *e) {
373 name = get_entity_ld_ident(e);
375 return is_jack_rts_name(name);