2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Compute an estimate of field temperature, i.e., field access heuristic.
23 * @author Goetz Lindenmaier
33 #include "field_temperature.h"
36 #include "execution_frequency.h"
39 #include "irgraph_t.h"
46 /* *************************************************************************** */
47 /* initialize, global variables. */
48 /* *************************************************************************** */
50 /* *************************************************************************** */
51 /* Access routines for irnodes */
52 /* *************************************************************************** */
54 /* The entities that can be accessed by this Sel node. */
55 int get_Sel_n_accessed_entities(ir_node *sel) {
59 ir_entity *get_Sel_accessed_entity(ir_node *sel, int pos) {
60 return get_Sel_entity(sel);
63 /* *************************************************************************** */
65 /* *************************************************************************** */
67 int get_irn_loop_call_depth(ir_node *n) {
68 ir_graph *irg = get_irn_irg(n);
69 return get_irg_loop_depth(irg);
72 int get_irn_cfloop_depth(ir_node *n) {
73 ir_loop *l = get_irn_loop(get_nodes_block(n));
75 return get_loop_depth(l);
80 int get_irn_recursion_depth(ir_node *n) {
81 ir_graph *irg = get_irn_irg(n);
82 return get_irg_recursion_depth(irg);
86 /** @@@ the second version of the heuristic. */
87 int get_weighted_loop_depth(ir_node *n) {
88 int loop_call_depth = get_irn_loop_call_depth(n);
89 int loop_depth = get_irn_cfloop_depth(n);
90 int recursion_depth = get_irn_recursion_depth(n);
92 return loop_call_depth + loop_depth + recursion_depth;
96 /* *************************************************************************** */
97 /* The 2. heuristic */
98 /* *************************************************************************** */
100 static int default_recursion_weight = 5;
102 /* The final evaluation of a node. In this function we can
103 adapt the heuristic. Combine execution frequency with
105 @@@ the second version of the heuristic.
107 Return 0 if the node is neither in a loop nor in a recursion. */
108 double get_irn_final_cost(ir_node *n) {
109 double cost_loop = get_irn_exec_freq(n);
110 double cost_method = get_irg_method_execution_frequency(get_irn_irg(n));
111 int rec_depth = get_irn_recursion_depth(n);
115 if (get_irn_recursion_depth(n) == 0 &&
116 get_irn_loop_depth(n) == 0 &&
117 get_irg_method_loop_depth(get_irn_irg(n)) == 0)
120 if (get_weighted_loop_depth(n) == 0) return 0;
123 if (rec_depth) cost_rec = pow(default_recursion_weight, rec_depth);
124 return cost_loop*(cost_method + cost_rec);
127 double get_type_estimated_n_instances(ir_type *tp) {
128 int i, n_allocs = get_type_n_allocs(tp);
129 double n_instances = 0;
130 for (i = 0; i < n_allocs; ++i) {
131 ir_node *alloc = get_type_alloc(tp, i);
132 n_instances += get_irn_final_cost(alloc);
137 double get_type_estimated_mem_consumption_bytes(ir_type *tp) {
142 int get_type_estimated_n_fields(ir_type *tp) {
144 switch(get_type_tpop_code(tp)) {
148 case tpo_enumeration:
153 s = 1; /* dispatch pointer */
156 int i, n_mem = get_compound_n_members(tp);
157 for (i = 0; i < n_mem; ++i) {
158 ir_entity *mem = get_compound_member(tp, i);
159 if (get_entity_allocation(mem) == allocation_automatic) {
160 s += get_type_estimated_n_fields(get_entity_type(mem));
166 long n_elt = DEFAULT_N_ARRAY_ELEMENTS;
167 assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
168 if ((get_irn_op(get_array_lower_bound(tp, 0)) == op_Const) &&
169 (get_irn_op(get_array_upper_bound(tp, 0)) == op_Const) ) {
170 n_elt = get_array_upper_bound_int(tp, 0) - get_array_upper_bound_int(tp, 0);
175 default: DDMT(tp); assert(0);
181 int get_type_estimated_size_bytes(ir_type *tp) {
184 switch(get_type_tpop_code(tp)) {
188 case tpo_enumeration:
189 s = get_mode_size_bytes(get_type_mode(tp));
193 s = get_mode_size_bytes(mode_P_data); /* dispatch pointer */
196 int i, n_mem = get_compound_n_members(tp);
197 for (i = 0; i < n_mem; ++i) {
198 ir_entity *mem = get_compound_member(tp, i);
199 s += get_type_estimated_size_bytes(get_entity_type(mem));
201 if (get_entity_allocation(mem) == allocation_automatic) {
202 } /* allocation_automatic */
207 int elt_s = get_type_estimated_size_bytes(get_array_element_type(tp));
208 long n_elt = DEFAULT_N_ARRAY_ELEMENTS;
209 assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
210 if ((get_irn_op(get_array_lower_bound(tp, 0)) == op_Const) &&
211 (get_irn_op(get_array_upper_bound(tp, 0)) == op_Const) ) {
212 n_elt = get_array_upper_bound_int(tp, 0) - get_array_lower_bound_int(tp, 0);
218 default: DDMT(tp); assert(0);
224 double get_type_estimated_n_casts(ir_type *tp) {
225 int i, n_casts = get_type_n_casts(tp);
226 double n_instances = 0;
227 for (i = 0; i < n_casts; ++i) {
228 ir_node *cast = get_type_cast(tp, i);
229 n_instances += get_irn_final_cost(cast);
234 double get_class_estimated_n_upcasts(ir_type *clss) {
235 double n_instances = 0;
236 int i, j, n_casts, n_pointertypes;
238 n_casts = get_type_n_casts(clss);
239 for (i = 0; i < n_casts; ++i) {
240 ir_node *cast = get_type_cast(clss, i);
241 if (get_irn_opcode(cast) != iro_Cast) continue; /* Could be optimized away. */
243 if (is_Cast_upcast(cast))
244 n_instances += get_irn_final_cost(cast);
247 n_pointertypes = get_type_n_pointertypes_to(clss);
248 for (j = 0; j < n_pointertypes; ++j) {
249 n_instances += get_class_estimated_n_upcasts(get_type_pointertype_to(clss, j));
255 double get_class_estimated_n_downcasts(ir_type *clss) {
256 double n_instances = 0;
257 int i, j, n_casts, n_pointertypes;
259 n_casts = get_type_n_casts(clss);
260 for (i = 0; i < n_casts; ++i) {
261 ir_node *cast = get_type_cast(clss, i);
262 if (get_irn_opcode(cast) != iro_Cast) continue; /* Could be optimized away. */
264 if (is_Cast_downcast(cast))
265 n_instances += get_irn_final_cost(cast);
268 n_pointertypes = get_type_n_pointertypes_to(clss);
269 for (j = 0; j < n_pointertypes; ++j) {
270 n_instances += get_class_estimated_n_downcasts(get_type_pointertype_to(clss, j));
277 double get_class_estimated_dispatch_writes(ir_type *clss) {
278 return get_type_estimated_n_instances(clss);
281 /** Returns the number of reads of the dispatch pointer. */
282 double get_class_estimated_dispatch_reads (ir_type *clss) {
283 int i, n_mems = get_class_n_members(clss);
285 for (i = 0; i < n_mems; ++i) {
286 ir_entity *mem = get_class_member(clss, i);
287 n_calls += get_entity_estimated_n_dyncalls(mem);
292 double get_class_estimated_n_dyncalls(ir_type *clss) {
293 return get_class_estimated_dispatch_reads(clss) +
294 get_class_estimated_dispatch_writes(clss);
297 double get_entity_estimated_n_loads(ir_entity *ent) {
298 int i, n_acc = get_entity_n_accesses(ent);
300 for (i = 0; i < n_acc; ++i) {
301 ir_node *acc = get_entity_access(ent, i);
302 if (get_irn_op(acc) == op_Load) {
303 n_loads += get_irn_final_cost(acc);
309 double get_entity_estimated_n_stores(ir_entity *ent) {
310 int i, n_acc = get_entity_n_accesses(ent);
312 for (i = 0; i < n_acc; ++i) {
313 ir_node *acc = get_entity_access(ent, i);
314 if (get_irn_op(acc) == op_Store)
315 n_stores += get_irn_final_cost(acc);
320 /* @@@ Should we evaluate the callee array? */
321 double get_entity_estimated_n_calls(ir_entity *ent) {
322 int i, n_acc = get_entity_n_accesses(ent);
324 for (i = 0; i < n_acc; ++i) {
325 ir_node *acc = get_entity_access(ent, i);
326 if (get_irn_op(acc) == op_Call)
328 n_calls += get_irn_final_cost(acc);
333 double get_entity_estimated_n_dyncalls(ir_entity *ent) {
334 int i, n_acc = get_entity_n_accesses(ent);
336 for (i = 0; i < n_acc; ++i) {
337 ir_node *acc = get_entity_access(ent, i);
339 /* Call->Sel(ent) combination */
340 if (is_Call(acc) && is_Sel(get_Call_ptr(acc))) {
341 n_calls += get_irn_final_cost(acc);
343 /* MemOp->Sel combination for static, overwritten entities */
344 } else if (is_memop(acc) && is_Sel(get_memop_ptr(acc))) {
345 ir_entity *ent = get_Sel_entity(get_memop_ptr(acc));
346 if (is_Class_type(get_entity_owner(ent))) {
347 /* We might call this for inner entities in compounds. */
348 if (get_entity_n_overwrites(ent) > 0 ||
349 get_entity_n_overwrittenby(ent) > 0) {
350 n_calls += get_irn_final_cost(acc);
359 /* ------------------------------------------------------------------------- */
361 /* ------------------------------------------------------------------------- */
363 int is_jack_rts_name(ident *name) {
364 if (id_is_suffix(new_id_from_str("Exception"), name)) return 1;
365 if (id_is_suffix(new_id_from_str("Throwable"), name)) return 1;
366 if (id_is_suffix(new_id_from_str("Error"), name)) return 1;
370 if (id_is_prefix(new_id_from_str("java/"), name)) return 1;
371 if (id_is_prefix(new_id_from_str("["), name)) return 1;
372 if (id_is_prefix(new_id_from_str("gnu/"), name)) return 1;
373 if (id_is_prefix(new_id_from_str("java/"), name)) return 1;
374 if (id_is_prefix(new_id_from_str("CStringToCoreString"), name)) return 1;
380 int is_jack_rts_class(ir_type *t) {
381 ident *name = get_type_ident(t);
382 return is_jack_rts_name(name);
385 #include "entity_t.h" // for the assertion.
387 int is_jack_rts_entity(ir_entity *e) {
391 name = get_entity_ld_ident(e);
393 return is_jack_rts_name(name);