2 * Copyright (C) 1995-2008 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
29 #ifdef INTERPROCEDURAL_VIEW
33 #include "field_temperature.h"
36 #include "execution_frequency.h"
39 #include "irgraph_t.h"
47 /* *************************************************************************** */
48 /* initialize, global variables. */
49 /* *************************************************************************** */
51 /* *************************************************************************** */
53 /* *************************************************************************** */
55 int get_irn_loop_call_depth(ir_node *n)
57 ir_graph *irg = get_irn_irg(n);
58 return get_irg_loop_depth(irg);
61 int get_irn_cfloop_depth(ir_node *n)
63 ir_loop *l = get_irn_loop(get_nodes_block(n));
65 return get_loop_depth(l);
70 int get_irn_recursion_depth(ir_node *n)
72 ir_graph *irg = get_irn_irg(n);
73 return get_irg_recursion_depth(irg);
77 /** @@@ the second version of the heuristic. */
78 int get_weighted_loop_depth(ir_node *n)
80 int loop_call_depth = get_irn_loop_call_depth(n);
81 int loop_depth = get_irn_cfloop_depth(n);
82 int recursion_depth = get_irn_recursion_depth(n);
84 return loop_call_depth + loop_depth + recursion_depth;
88 /* *************************************************************************** */
89 /* The 2. heuristic */
90 /* *************************************************************************** */
92 static int default_recursion_weight = 5;
94 /* The final evaluation of a node. In this function we can
95 adapt the heuristic. Combine execution frequency with
97 @@@ the second version of the heuristic.
99 Return 0 if the node is neither in a loop nor in a recursion. */
100 double get_irn_final_cost(ir_node *n)
102 double cost_loop = get_irn_exec_freq(n);
103 double cost_method = get_irg_method_execution_frequency(get_irn_irg(n));
104 int rec_depth = get_irn_recursion_depth(n);
108 if (get_irn_recursion_depth(n) == 0 &&
109 get_irn_loop_depth(n) == 0 &&
110 get_irg_method_loop_depth(get_irn_irg(n)) == 0)
113 if (get_weighted_loop_depth(n) == 0) return 0;
116 if (rec_depth) cost_rec = pow(default_recursion_weight, rec_depth);
117 return cost_loop*(cost_method + cost_rec);
120 double get_type_estimated_n_instances(ir_type *tp)
122 int i, n_allocs = get_type_n_allocs(tp);
123 double n_instances = 0;
124 for (i = 0; i < n_allocs; ++i) {
125 ir_node *alloc = get_type_alloc(tp, i);
126 n_instances += get_irn_final_cost(alloc);
131 double get_type_estimated_mem_consumption_bytes(ir_type *tp)
138 int get_type_estimated_n_fields(ir_type *tp)
141 switch (get_type_tpop_code(tp)) {
145 case tpo_enumeration:
150 s = 1; /* dispatch pointer */
153 int i, n_mem = get_compound_n_members(tp);
154 for (i = 0; i < n_mem; ++i) {
155 ir_entity *mem = get_compound_member(tp, i);
156 if (get_entity_allocation(mem) == allocation_automatic) {
157 s += get_type_estimated_n_fields(get_entity_type(mem));
163 long n_elt = DEFAULT_N_ARRAY_ELEMENTS;
164 assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
165 if (is_Const(get_array_lower_bound(tp, 0)) &&
166 is_Const(get_array_upper_bound(tp, 0))) {
167 n_elt = get_array_upper_bound_int(tp, 0) - get_array_upper_bound_int(tp, 0);
173 panic("Unsupported type in get_type_estimated_n_fields %+F", tp);
179 int get_type_estimated_size_bytes(ir_type *tp)
183 switch (get_type_tpop_code(tp)) {
187 case tpo_enumeration:
188 s = get_mode_size_bytes(get_type_mode(tp));
192 s = get_mode_size_bytes(mode_P_data); /* dispatch pointer */
195 int i, n_mem = get_compound_n_members(tp);
196 for (i = 0; i < n_mem; ++i) {
197 ir_entity *mem = get_compound_member(tp, i);
198 s += get_type_estimated_size_bytes(get_entity_type(mem));
200 if (get_entity_allocation(mem) == allocation_automatic) {
201 } /* allocation_automatic */
206 int elt_s = get_type_estimated_size_bytes(get_array_element_type(tp));
207 long n_elt = DEFAULT_N_ARRAY_ELEMENTS;
208 assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
209 if (is_Const(get_array_lower_bound(tp, 0)) &&
210 is_Const(get_array_upper_bound(tp, 0))) {
211 n_elt = get_array_upper_bound_int(tp, 0) - get_array_lower_bound_int(tp, 0);
223 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)
236 double n_instances = 0;
237 int i, j, n_casts, n_pointertypes;
239 n_casts = get_type_n_casts(clss);
240 for (i = 0; i < n_casts; ++i) {
241 ir_node *cast = get_type_cast(clss, i);
242 if (get_irn_opcode(cast) != iro_Cast) continue; /* Could be optimized away. */
244 if (is_Cast_upcast(cast))
245 n_instances += get_irn_final_cost(cast);
248 n_pointertypes = get_type_n_pointertypes_to(clss);
249 for (j = 0; j < n_pointertypes; ++j) {
250 n_instances += get_class_estimated_n_upcasts(get_type_pointertype_to(clss, j));
256 double get_class_estimated_n_downcasts(ir_type *clss)
258 double n_instances = 0;
259 int i, j, n_casts, n_pointertypes;
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. */
266 if (is_Cast_downcast(cast))
267 n_instances += get_irn_final_cost(cast);
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));
279 double get_class_estimated_dispatch_writes(ir_type *clss)
281 return get_type_estimated_n_instances(clss);
284 /** Returns the number of reads of the dispatch pointer. */
285 double get_class_estimated_dispatch_reads (ir_type *clss)
287 int i, n_mems = get_class_n_members(clss);
289 for (i = 0; i < n_mems; ++i) {
290 ir_entity *mem = get_class_member(clss, i);
291 n_calls += get_entity_estimated_n_dyncalls(mem);
296 double get_class_estimated_n_dyncalls(ir_type *clss)
298 return get_class_estimated_dispatch_reads(clss) +
299 get_class_estimated_dispatch_writes(clss);
302 double get_entity_estimated_n_loads(ir_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 n_loads += get_irn_final_cost(acc);
315 double get_entity_estimated_n_stores(ir_entity *ent)
317 int i, n_acc = get_entity_n_accesses(ent);
319 for (i = 0; i < n_acc; ++i) {
320 ir_node *acc = get_entity_access(ent, i);
322 n_stores += get_irn_final_cost(acc);
327 /* @@@ Should we evaluate the callee array? */
328 double get_entity_estimated_n_calls(ir_entity *ent)
330 int i, n_acc = get_entity_n_accesses(ent);
332 for (i = 0; i < n_acc; ++i) {
333 ir_node *acc = get_entity_access(ent, i);
335 n_calls += get_irn_final_cost(acc);
340 double get_entity_estimated_n_dyncalls(ir_entity *ent)
342 int i, n_acc = get_entity_n_accesses(ent);
344 for (i = 0; i < n_acc; ++i) {
345 ir_node *acc = get_entity_access(ent, i);
347 /* Call->Sel(ent) combination */
348 if (is_Call(acc) && is_Sel(get_Call_ptr(acc))) {
349 n_calls += get_irn_final_cost(acc);
351 /* MemOp->Sel combination for static, overwritten entities */
352 } else if (is_memop(acc) && is_Sel(get_memop_ptr(acc))) {
353 ir_entity *ent = get_Sel_entity(get_memop_ptr(acc));
354 if (is_Class_type(get_entity_owner(ent))) {
355 /* We might call this for inner entities in compounds. */
356 if (get_entity_n_overwrites(ent) > 0 ||
357 get_entity_n_overwrittenby(ent) > 0) {
358 n_calls += get_irn_final_cost(acc);