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 /* *************************************************************************** */
33 /* *************************************************************************** */
34 /* Access routines for irnodes */
35 /* *************************************************************************** */
37 /* The entities that can be accessed by this Sel node. */
38 int get_Sel_n_accessed_entities(ir_node *sel) {
42 entity *get_Sel_accessed_entity(ir_node *sel, int pos) {
43 return get_Sel_entity(sel);
48 /* *************************************************************************** */
50 /* *************************************************************************** */
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);
57 int get_irn_loop_depth(ir_node *n) {
58 ir_loop *l = get_irn_loop(get_nodes_block(n));
60 return get_loop_depth(l);
65 int get_irn_recursion_depth(ir_node *n) {
66 ir_graph *irg = get_irn_irg(n);
67 return get_irg_recursion_depth(irg);
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);
76 return loop_call_depth + loop_depth + recursion_depth;
80 /* *************************************************************************** */
81 /* The 2. heuristic */
82 /* *************************************************************************** */
84 static int default_recursion_weight = 5;
87 /* The final evaluation of a node. In this function we can
88 adapt the heuristic. Combine execution freqency with
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;
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) {
111 int get_type_estimated_n_fields(type *tp) {
113 switch(get_type_tpop_code(tp)) {
117 case tpo_enumeration:
122 s = 1; /* dispatch pointer */
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));
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);
144 default: DDMT(tp); assert(0);
150 int get_type_estimated_size_bytes(type *tp) {
153 switch(get_type_tpop_code(tp)) {
157 case tpo_enumeration:
158 s = get_mode_size_bytes(get_type_mode(tp));
162 s = get_mode_size_bytes(mode_P_mach); /* dispatch pointer */
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));
170 if (get_entity_allocation(mem) == allocation_automatic) {
171 } /* allocation_automatic */
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);
187 default: DDMT(tp); assert(0);
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);
203 double get_class_estimated_n_upcasts(type *clss) {
204 double n_instances = 0;
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. */
211 if (is_Cast_upcast(cast))
212 n_instances += get_irn_final_cost(cast);
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));
223 double get_class_estimated_n_downcasts(type *clss) {
224 double n_instances = 0;
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. */
231 if (is_Cast_downcast(cast))
232 n_instances += get_irn_final_cost(cast);
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));
244 double get_class_estimated_dispatch_writes(type *clss) {
245 return get_type_estimated_n_instances(clss);
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);
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);
259 double get_class_estimated_n_dyncalls(type *clss) {
260 return get_class_estimated_dispatch_reads(clss) +
261 get_class_estimated_dispatch_writes(clss);
264 double get_entity_estimated_n_loads(entity *ent) {
265 int i, n_acc = get_entity_n_accesses(ent);
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);
276 double get_entity_estimated_n_stores(entity *ent) {
277 int i, n_acc = get_entity_n_accesses(ent);
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);
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);
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)
295 n_calls += get_irn_final_cost(acc);
300 double get_entity_estimated_n_dyncalls(entity *ent) {
301 int i, n_acc = get_entity_n_accesses(ent);
303 for (i = 0; i < n_acc; ++i) {
304 ir_node *acc = get_entity_access(ent, i);
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);
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);
328 /* *************************************************************************** */
330 /* *************************************************************************** */
332 int is_jack_rts_name(const ident *name) {
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;
344 int is_jack_rts_class(type *t) {
345 ident *name = get_type_ident(t);
346 return is_jack_rts_name(name);
349 #include "entity_t.h" // for the assertion.
351 int is_jack_rts_entity(entity *e) {
353 ident *name = get_entity_ld_ident(e);
355 return is_jack_rts_name(name);