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 read/write analyze of graph argument, which have mode reference.
23 * @author Beyhan Veliev
42 #include "analyze_irg_args.h"
45 static void *VISITED = &v;
48 * Walk recursive the successors of a graph argument
49 * with mode reference and mark if it will be read,
52 * @param arg The graph argument with mode reference,
53 * that must be checked.
55 static unsigned analyze_arg(ir_node *arg, unsigned bits)
60 /* We must visit a node once to avoid endless recursion.*/
61 set_irn_link(arg, VISITED);
63 for (i = get_irn_n_outs(arg) - 1; i >= 0; --i) {
64 succ = get_irn_out(arg, i);
67 if (get_irn_link(succ) == VISITED)
70 /* We should not walk over the memory edge.*/
71 if (get_irn_mode(succ) == mode_M)
74 /* If we reach with the recursion a Call node and our reference
75 isn't the address of this Call we accept that the reference will
76 be read and written if the graph of the method represented by
77 "Call" isn't computed else we analyze that graph. If our
78 reference is the address of this
79 Call node that mean the reference will be read.*/
80 switch (get_irn_opcode(succ)) {
83 ir_node *ptr = get_Call_ptr(succ);
86 /* Hmm: not sure what this is, most likely a read */
87 bits |= ptr_access_read;
90 ir_op *op = get_irn_op(ptr);
93 if (op == op_SymConst && get_SymConst_kind(ptr) == symconst_addr_ent) {
94 meth_ent = get_SymConst_entity(ptr);
96 for (p = get_Call_n_params(succ) - 1; p >= 0; --p) {
97 if (get_Call_param(succ, p) == arg) {
98 /* an arg can be used more than once ! */
99 bits |= get_method_param_access(meth_ent, p);
103 else if (op == op_Sel && get_irp_callee_info_state() == irg_callee_info_consistent) {
104 /* is be a polymorphic call but callee information is available */
105 int i, n_params = get_Call_n_params(succ);
107 /* simply look into ALL possible callees */
108 for (i = get_Call_n_callees(succ) - 1; i >= 0; --i) {
109 meth_ent = get_Call_callee(succ, i);
111 /* unknown_entity is used to signal that we don't know what is called */
112 if (meth_ent == unknown_entity) {
113 bits |= ptr_access_all;
117 for (p = n_params - 1; p >= 0; --p) {
118 if (get_Call_param(succ, p) == arg) {
119 /* an arg can be used more than once ! */
120 bits |= get_method_param_access(meth_ent, p);
125 else /* can do anything */
126 bits |= ptr_access_all;
129 /* search stops here anyway */
133 /* We have reached a Store node => the reference is written or stored. */
134 if (get_Store_ptr(succ) == arg) {
136 bits |= ptr_access_write;
140 bits |= ptr_access_store;
143 /* search stops here anyway */
147 /* We have reached a Load node => the reference is read. */
148 bits |= ptr_access_read;
150 /* search stops here anyway */
154 /* our address is casted into something unknown. Break our search. */
155 bits = ptr_access_all;
162 /* If we know that, the argument will be read, write and stored, we
163 can break the recursion.*/
164 if (bits == ptr_access_all) {
165 bits = ptr_access_all;
170 * A calculation that do not lead to a reference mode ends our search.
171 * This is dangerous: It would allow to cast into integer and that cast back ...
172 * so, when we detect a Conv we go mad, see the Conv case above.
174 if (!mode_is_reference(get_irn_mode(succ)))
177 /* follow further the address calculation */
178 bits = analyze_arg(succ, bits);
180 set_irn_link(arg, NULL);
185 * Check if a argument of the ir graph with mode
186 * reference is read, write or both.
188 * @param irg The ir graph to analyze.
190 static void analyze_ent_args(ir_entity *ent)
193 ir_node *irg_args, *arg;
198 ptr_access_kind *rw_info;
200 mtp = get_entity_type(ent);
201 nparams = get_method_n_params(mtp);
203 ent->attr.mtd_attr.param_access = NEW_ARR_F(ptr_access_kind, nparams);
205 /* If the method haven't parameters we have
211 irg = get_entity_irg(ent);
213 /* we have not yet analyzed the graph, set ALL access for pointer args */
214 for (i = nparams - 1; i >= 0; --i)
215 ent->attr.mtd_attr.param_access[i] =
216 is_Pointer_type(get_method_param_type(mtp, i)) ? ptr_access_all : ptr_access_none;
219 /* no graph, no better info */
223 assure_irg_outs(irg);
225 irg_args = get_irg_args(irg);
227 /* A array to save the information for each argument with
229 NEW_ARR_A(ptr_access_kind, rw_info, nparams);
231 /* We initialize the element with none state. */
232 for (i = nparams - 1; i >= 0; --i)
233 rw_info[i] = ptr_access_none;
235 /* search for arguments with mode reference
237 for (i = get_irn_n_outs(irg_args) - 1; i >= 0; --i) {
238 arg = get_irn_out(irg_args, i);
239 arg_mode = get_irn_mode(arg);
240 proj_nr = get_Proj_proj(arg);
242 if (mode_is_reference(arg_mode))
243 rw_info[proj_nr] |= analyze_arg(arg, rw_info[proj_nr]);
246 /* copy the temporary info */
247 memcpy(ent->attr.mtd_attr.param_access, rw_info,
248 nparams * sizeof(ent->attr.mtd_attr.param_access[0]));
251 printf("\n%s:\n", get_entity_name(ent));
252 for (i = 0; i < nparams; ++i) {
253 if (is_Pointer_type(get_method_param_type(mtp, i)))
254 if (ent->attr.mtd_attr.param_access[i] != ptr_access_none) {
255 printf(" Pointer Arg %d access: ", i);
256 if (ent->attr.mtd_attr.param_access[i] & ptr_access_read)
258 if (ent->attr.mtd_attr.param_access[i] & ptr_access_write)
260 if (ent->attr.mtd_attr.param_access[i] & ptr_access_store)
269 * Analyze how pointer arguments of a given
270 * ir graph are accessed.
272 * @param irg The ir graph to analyze.
274 void analyze_irg_args(ir_graph *irg)
278 if (irg == get_const_code_irg())
281 ent = get_irg_entity(irg);
285 if (! ent->attr.mtd_attr.param_access)
286 analyze_ent_args(ent);
290 * Compute for a method with pointer parameter(s)
291 * if they will be read or written.
293 ptr_access_kind get_method_param_access(ir_entity *ent, int pos)
295 ir_type *mtp = get_entity_type(ent);
297 int is_variadic = get_method_variadicity(mtp) == variadicity_variadic;
299 assert(0 <= pos && (is_variadic || pos < get_method_n_params(mtp)));
302 if (ent->attr.mtd_attr.param_access) {
303 if (pos < ARR_LEN(ent->attr.mtd_attr.param_access))
304 return ent->attr.mtd_attr.param_access[pos];
306 return ptr_access_all;
309 analyze_ent_args(ent);
311 if (pos < ARR_LEN(ent->attr.mtd_attr.param_access))
312 return ent->attr.mtd_attr.param_access[pos];
314 return ptr_access_all;
318 null_weight = 0, /**< If can't be anything optimized. */
319 binop_weight = 1, /**< If the argument have mode_weight and take part in binop. */
320 const_binop_weight = 1, /**< If the argument have mode_weight and take part in binop with a constant.*/
321 cmp_weight = 4, /**< If the argument take part in cmp. */
322 const_cmp_weight = 10 /**< If the argument take part in cmp with a constant. */
326 * Compute the weight of a method parameter
328 * @param arg The parameter them weight muss be computed.
330 static float calc_method_param_weight(ir_node *arg)
334 float weight = null_weight;
336 /* We mark the nodes to avoid endless recursion */
337 set_irn_link(arg, VISITED);
339 for (i = get_irn_n_outs(arg) - 1; i >= 0; i--) {
340 succ = get_irn_out(arg, i);
343 if (get_irn_link(succ) == VISITED)
346 /* We should not walk over the memory edge.*/
347 if (get_irn_mode(succ) == mode_M)
350 /* We have reached a cmp and we must increase the
351 weight with the cmp_weight.*/
352 if (get_irn_op(succ) == op_Cmp) {
354 if (get_Cmp_left(succ) == arg)
355 op = get_Cmp_right(succ);
357 op = get_Cmp_left(succ);
359 if (is_irn_constlike(op)) {
360 weight += const_cmp_weight;
363 weight += cmp_weight;
365 else if (is_binop(succ)) {
366 /* We have reached a binop and we must increase the
367 weight with the binop_weight. If the other operand of the
368 binop is a constant we increase the weight with const_binop_weight
369 and call the function recursive.
371 if (get_binop_left(succ) == arg)
372 op = get_binop_right(succ);
374 op = get_binop_left(succ);
376 if (is_irn_constlike(op)) {
377 weight += const_binop_weight;
378 weight += calc_method_param_weight(succ);
381 weight += binop_weight;
382 } else if (is_unop(succ)) {
383 /* We have reached a binop and we must increase the
384 weight with the const_binop_weight and call the function recursive.*/
385 weight += const_binop_weight;
386 weight += calc_method_param_weight(succ);
389 set_irn_link(arg, NULL);
394 * Set a weight for each argument of a ir_graph.
395 * The args with a greater weight are good for optimize.
397 * @param ent The entity of the ir_graph.
399 static void analyze_method_params_weight(ir_entity *ent)
403 int nparams, i, proj_nr;
404 ir_node *irg_args, *arg;
406 mtp = get_entity_type(ent);
407 nparams = get_method_n_params(mtp);
409 /* allocate a new array. currently used as 'analysed' flag */
410 ent->attr.mtd_attr.param_weight = NEW_ARR_F(float, nparams);
412 /* If the method haven't parameters we have
418 irg = get_entity_irg(ent);
420 /* First we initialize the parameter weight with 0. */
421 for (i = nparams - 1; i >= 0; i--)
422 ent->attr.mtd_attr.param_weight[i] = null_weight;
425 /* no graph, no better info */
429 /* Call algorithm that computes the out edges */
430 assure_irg_outs(irg);
432 irg_args = get_irg_args(irg);
434 for (i = get_irn_n_outs(irg_args) - 1; i >= 0; --i) {
435 arg = get_irn_out(irg_args, i);
436 proj_nr = get_Proj_proj(arg);
437 ent->attr.mtd_attr.param_weight[proj_nr] += calc_method_param_weight(arg);
441 printf("\n%s:\n", get_entity_name(ent));
442 for (i = nparams - 1; i >= 0; --i)
443 printf("The weight of argument %i is %f \n", i, ent->param_weight[i]);
448 * Compute for a method with pointer parameter(s)
449 * if they will be read or written.
451 float get_method_param_weight(ir_entity *ent, int pos)
453 ir_type *mtp = get_entity_type(ent);
456 int is_variadic = get_method_variadicity(mtp) == variadicity_variadic;
457 assert(0 <= pos && (is_variadic || pos < get_method_n_params(mtp)));
460 if (ent->attr.mtd_attr.param_weight) {
461 if (pos < ARR_LEN(ent->attr.mtd_attr.param_weight))
462 return ent->attr.mtd_attr.param_weight[pos];
467 analyze_method_params_weight(ent);
469 if (pos < ARR_LEN(ent->attr.mtd_attr.param_weight))
470 return ent->attr.mtd_attr.param_weight[pos];
477 * Analyze argument's weight of a given
480 * @param irg The ir graph to analyze.
482 void analyze_irg_args_weight(ir_graph *irg)
486 ent = get_irg_entity(irg);
490 if (! ent->attr.mtd_attr.param_weight)
491 analyze_method_params_weight(ent);