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 read/write analyze of graph argument, which have mode reference.
23 * @author Beyhan Veliev
37 #include "analyze_irg_args.h"
40 * Walk recursive the successors of a graph argument
41 * with mode reference and mark if it will be read,
44 * @param arg The graph argument with mode reference,
45 * that must be checked.
47 static ptr_access_kind analyze_arg(ir_node *arg, ptr_access_kind bits)
52 /* We must visit a node once to avoid endless recursion.*/
53 mark_irn_visited(arg);
55 for (i = get_irn_n_outs(arg) - 1; i >= 0; --i) {
56 succ = get_irn_out(arg, i);
59 if (irn_visited(succ))
62 /* We should not walk over the memory edge.*/
63 if (get_irn_mode(succ) == mode_M)
66 /* If we reach with the recursion a Call node and our reference
67 isn't the address of this Call we accept that the reference will
68 be read and written if the graph of the method represented by
69 "Call" isn't computed else we analyze that graph. If our
70 reference is the address of this
71 Call node that mean the reference will be read.*/
72 switch (get_irn_opcode(succ)) {
75 ir_node *ptr = get_Call_ptr(succ);
78 /* Hmm: not sure what this is, most likely a read */
79 bits |= ptr_access_read;
84 meth_ent = get_Global_entity(ptr);
86 for (p = get_Call_n_params(succ) - 1; p >= 0; --p) {
87 if (get_Call_param(succ, p) == arg) {
88 /* an arg can be used more than once ! */
89 bits |= get_method_param_access(meth_ent, p);
92 } else if (is_Sel(ptr) && get_irp_callee_info_state() == irg_callee_info_consistent) {
93 /* is be a polymorphic call but callee information is available */
94 int n_params = get_Call_n_params(succ);
97 /* simply look into ALL possible callees */
98 for (c = get_Call_n_callees(succ) - 1; c >= 0; --c) {
99 meth_ent = get_Call_callee(succ, c);
101 /* unknown_entity is used to signal that we don't know what is called */
102 if (meth_ent == unknown_entity) {
103 bits |= ptr_access_all;
107 for (p = n_params - 1; p >= 0; --p) {
108 if (get_Call_param(succ, p) == arg) {
109 /* an arg can be used more than once ! */
110 bits |= get_method_param_access(meth_ent, p);
114 } else /* can do anything */
115 bits |= ptr_access_all;
118 /* search stops here anyway */
122 /* We have reached a Store node => the reference is written or stored. */
123 if (get_Store_ptr(succ) == arg) {
125 bits |= ptr_access_write;
128 bits |= ptr_access_store;
131 /* search stops here anyway */
135 /* We have reached a Load node => the reference is read. */
136 bits |= ptr_access_read;
138 /* search stops here anyway */
142 /* our address is casted into something unknown. Break our search. */
143 bits = ptr_access_all;
150 /* If we know that, the argument will be read, write and stored, we
151 can break the recursion.*/
152 if (bits == ptr_access_all) {
153 bits = ptr_access_all;
158 * A calculation that do not lead to a reference mode ends our search.
159 * This is dangerous: It would allow to cast into integer and that cast back ...
160 * so, when we detect a Conv we go mad, see the Conv case above.
162 if (!mode_is_reference(get_irn_mode(succ)))
165 /* follow further the address calculation */
166 bits = analyze_arg(succ, bits);
168 set_irn_link(arg, NULL);
173 * Check if a argument of the ir graph with mode
174 * reference is read, write or both.
176 * @param irg The ir graph to analyze.
178 static void analyze_ent_args(ir_entity *ent)
181 ir_node *irg_args, *arg;
186 ptr_access_kind *rw_info;
188 mtp = get_entity_type(ent);
189 nparams = get_method_n_params(mtp);
191 ent->attr.mtd_attr.param_access = NEW_ARR_F(ptr_access_kind, nparams);
193 /* If the method haven't parameters we have
199 irg = get_entity_irg(ent);
201 /* we have not yet analyzed the graph, set ALL access for pointer args */
202 for (i = nparams - 1; i >= 0; --i) {
203 ir_type *type = get_method_param_type(mtp, i);
204 ent->attr.mtd_attr.param_access[i] = is_Pointer_type(type) ? ptr_access_all : ptr_access_none;
208 /* no graph, no better info */
212 assure_irg_outs(irg);
214 irg_args = get_irg_args(irg);
216 /* A array to save the information for each argument with
218 NEW_ARR_A(ptr_access_kind, rw_info, nparams);
220 /* We initialize the element with none state. */
221 for (i = nparams - 1; i >= 0; --i)
222 rw_info[i] = ptr_access_none;
224 /* search for arguments with mode reference
226 for (i = get_irn_n_outs(irg_args) - 1; i >= 0; --i) {
227 arg = get_irn_out(irg_args, i);
228 arg_mode = get_irn_mode(arg);
229 proj_nr = get_Proj_proj(arg);
231 if (mode_is_reference(arg_mode))
232 rw_info[proj_nr] |= analyze_arg(arg, rw_info[proj_nr]);
235 /* copy the temporary info */
236 memcpy(ent->attr.mtd_attr.param_access, rw_info,
237 nparams * sizeof(ent->attr.mtd_attr.param_access[0]));
240 printf("\n%s:\n", get_entity_name(ent));
241 for (i = 0; i < nparams; ++i) {
242 if (is_Pointer_type(get_method_param_type(mtp, i)))
243 if (ent->attr.mtd_attr.param_access[i] != ptr_access_none) {
244 printf(" Pointer Arg %d access: ", i);
245 if (ent->attr.mtd_attr.param_access[i] & ptr_access_read)
247 if (ent->attr.mtd_attr.param_access[i] & ptr_access_write)
249 if (ent->attr.mtd_attr.param_access[i] & ptr_access_store)
258 * Analyze how pointer arguments of a given
259 * ir graph are accessed.
261 * @param irg The ir graph to analyze.
263 void analyze_irg_args(ir_graph *irg)
267 if (irg == get_const_code_irg())
270 ent = get_irg_entity(irg);
274 if (! ent->attr.mtd_attr.param_access)
275 analyze_ent_args(ent);
279 * Compute for a method with pointer parameter(s)
280 * if they will be read or written.
282 ptr_access_kind get_method_param_access(ir_entity *ent, size_t pos)
285 ir_type *mtp = get_entity_type(ent);
286 int is_variadic = get_method_variadicity(mtp) == variadicity_variadic;
288 assert(is_variadic || pos < get_method_n_params(mtp));
291 if (ent->attr.mtd_attr.param_access) {
292 if (pos < ARR_LEN(ent->attr.mtd_attr.param_access))
293 return ent->attr.mtd_attr.param_access[pos];
295 return ptr_access_all;
298 analyze_ent_args(ent);
300 if (pos < ARR_LEN(ent->attr.mtd_attr.param_access))
301 return ent->attr.mtd_attr.param_access[pos];
303 return ptr_access_all;
306 /* Weights for parameters */
308 null_weight = 0, /**< If can't be anything optimized. */
309 binop_weight = 1, /**< If the argument have mode_weight and take part in binop. */
310 const_binop_weight = 1, /**< If the argument have mode_weight and take part in binop with a constant.*/
311 cmp_weight = 4, /**< If the argument take part in cmp. */
312 const_cmp_weight = 10, /**< If the argument take part in cmp with a constant. */
313 indirect_call_weight = 125, /**< If the argument is the address of an indirect Call. */
317 * Compute the weight of a method parameter
319 * @param arg The parameter them weight muss be computed.
321 static unsigned calc_method_param_weight(ir_node *arg)
325 unsigned weight = null_weight;
327 /* We mark the nodes to avoid endless recursion */
328 mark_irn_visited(arg);
330 for (i = get_irn_n_outs(arg) - 1; i >= 0; i--) {
331 succ = get_irn_out(arg, i);
334 if (irn_visited(succ))
337 /* We should not walk over the memory edge.*/
338 if (get_irn_mode(succ) == mode_M)
341 switch (get_irn_opcode(succ)) {
343 if (get_Call_ptr(succ) == arg) {
344 /* the arguments is used as an pointer input for a call,
345 we can probably change an indirect Call into a direct one. */
346 weight += indirect_call_weight;
350 /* We have reached a cmp and we must increase the
351 weight with the cmp_weight. */
352 if (get_Cmp_left(succ) == arg)
353 op = get_Cmp_right(succ);
355 op = get_Cmp_left(succ);
357 if (is_irn_constlike(op)) {
358 weight += const_cmp_weight;
360 weight += cmp_weight;
363 /* the argument is used for a SwitchCond, a big win */
364 weight += const_cmp_weight * get_irn_n_outs(succ);
367 /* when looking backward we might find Id nodes */
368 weight += calc_method_param_weight(succ);
371 /* unoptimized tuple */
372 for (j = get_Tuple_n_preds(succ) - 1; j >= 0; --j) {
373 ir_node *pred = get_Tuple_pred(succ, j);
375 /* look for Proj(j) */
376 for (k = get_irn_n_outs(succ) - 1; k >= 0; --k) {
377 ir_node *succ_succ = get_irn_out(succ, k);
378 if (is_Proj(succ_succ)) {
379 if (get_Proj_proj(succ_succ) == j) {
381 weight += calc_method_param_weight(succ_succ);
384 /* this should NOT happen */
391 if (is_binop(succ)) {
392 /* We have reached a BinOp and we must increase the
393 weight with the binop_weight. If the other operand of the
394 BinOp is a constant we increase the weight with const_binop_weight
395 and call the function recursive.
397 if (get_binop_left(succ) == arg)
398 op = get_binop_right(succ);
400 op = get_binop_left(succ);
402 if (is_irn_constlike(op)) {
403 weight += const_binop_weight;
404 weight += calc_method_param_weight(succ);
406 weight += binop_weight;
407 } else if (is_unop(succ)) {
408 /* We have reached a binop and we must increase the
409 weight with the const_binop_weight and call the function recursive.*/
410 weight += const_binop_weight;
411 weight += calc_method_param_weight(succ);
416 set_irn_link(arg, NULL);
421 * Calculate a weight for each argument of an entity.
423 * @param ent The entity of the ir_graph.
425 static void analyze_method_params_weight(ir_entity *ent)
429 int nparams, i, proj_nr;
430 ir_node *irg_args, *arg;
432 mtp = get_entity_type(ent);
433 nparams = get_method_n_params(mtp);
435 /* allocate a new array. currently used as 'analysed' flag */
436 ent->attr.mtd_attr.param_weight = NEW_ARR_F(unsigned, nparams);
438 /* If the method haven't parameters we have nothing to do. */
442 /* First we initialize the parameter weights with 0. */
443 for (i = nparams - 1; i >= 0; i--)
444 ent->attr.mtd_attr.param_weight[i] = null_weight;
446 irg = get_entity_irg(ent);
448 /* no graph, no better info */
452 /* Call algorithm that computes the out edges */
453 assure_irg_outs(irg);
455 irg_args = get_irg_args(irg);
456 for (i = get_irn_n_outs(irg_args) - 1; i >= 0; --i) {
457 arg = get_irn_out(irg_args, i);
458 proj_nr = get_Proj_proj(arg);
459 ent->attr.mtd_attr.param_weight[proj_nr] += calc_method_param_weight(arg);
464 * Returns for a method the 'weight' that every parameter
465 * has on optimization possibility. Higher values allows
466 * higher optimization with procedure cloning.
468 * The values are calculation on demand only.
470 * @param ent the entity to analyze
471 * @param pos the argument number
473 * @return the parameter weight or null_weight if pos is greater
474 * than the number of arguments.
476 unsigned get_method_param_weight(ir_entity *ent, size_t pos)
478 if (ent->attr.mtd_attr.param_weight) {
479 if (pos < ARR_LEN(ent->attr.mtd_attr.param_weight))
480 return ent->attr.mtd_attr.param_weight[pos];
485 analyze_method_params_weight(ent);
487 if (pos < ARR_LEN(ent->attr.mtd_attr.param_weight))
488 return ent->attr.mtd_attr.param_weight[pos];
494 * Analyze argument's weight of a given
497 * @param irg The ir graph to analyze.
499 void analyze_irg_args_weight(ir_graph *irg)
501 ir_entity *entity = get_irg_entity(irg);
505 assert(is_method_entity(entity));
506 if (entity->attr.mtd_attr.param_weight != NULL)
509 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
510 inc_irg_visited(irg);
511 analyze_method_params_weight(entity);
512 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);