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_loop_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_loop_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 freqency with
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);
93 if (rec_depth) cost_rec = pow(default_recursion_weight, rec_depth);
94 return cost_loop*(cost_method + cost_rec);
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);
107 double get_type_estimated_mem_consumption_bytes(type *tp) {
112 int get_type_estimated_n_fields(type *tp) {
114 switch(get_type_tpop_code(tp)) {
118 case tpo_enumeration:
123 s = 1; /* dispatch pointer */
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));
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);
145 default: DDMT(tp); assert(0);
151 int get_type_estimated_size_bytes(type *tp) {
154 switch(get_type_tpop_code(tp)) {
158 case tpo_enumeration:
159 s = get_mode_size_bytes(get_type_mode(tp));
163 s = get_mode_size_bytes(mode_P_mach); /* dispatch pointer */
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));
171 if (get_entity_allocation(mem) == allocation_automatic) {
172 } /* allocation_automatic */
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);
188 default: DDMT(tp); assert(0);
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);
204 double get_class_estimated_n_upcasts(type *clss) {
205 double n_instances = 0;
206 int i, j, n_casts, n_pointertypes;
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. */
213 if (is_Cast_upcast(cast))
214 n_instances += get_irn_final_cost(cast);
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));
225 double get_class_estimated_n_downcasts(type *clss) {
226 double n_instances = 0;
227 int i, j, n_casts, n_pointertypes;
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. */
234 if (is_Cast_downcast(cast))
235 n_instances += get_irn_final_cost(cast);
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));
247 double get_class_estimated_dispatch_writes(type *clss) {
248 return get_type_estimated_n_instances(clss);
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);
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);
262 double get_class_estimated_n_dyncalls(type *clss) {
263 return get_class_estimated_dispatch_reads(clss) +
264 get_class_estimated_dispatch_writes(clss);
267 double get_entity_estimated_n_loads(entity *ent) {
268 int i, n_acc = get_entity_n_accesses(ent);
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);
279 double get_entity_estimated_n_stores(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_Store)
285 n_stores += get_irn_final_cost(acc);
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);
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)
298 n_calls += get_irn_final_cost(acc);
303 double get_entity_estimated_n_dyncalls(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);
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);
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);
330 /* ------------------------------------------------------------------------- */
332 /* ------------------------------------------------------------------------- */
334 int is_jack_rts_name(ident *name) {
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;
346 int is_jack_rts_class(type *t) {
347 ident *name = get_type_ident(t);
348 return is_jack_rts_name(name);
351 #include "entity_t.h" // for the assertion.
353 int is_jack_rts_entity(entity *e) {
357 name = get_entity_ld_ident(e);
359 return is_jack_rts_name(name);