3 * File name: ir/ana/analyze_irg_agrs.c
4 * Purpose: read/write analyze of graph argument, which have mode reference.
5 * Author: Beyhan Veliev
8 * Copyright: (c) 1998-2005 Universität Karlsruhe
9 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
13 * @file analyze_irg_agrs.c
32 #include "analyze_irg_args.h"
35 static void *VISITED = &v;
38 * Walk recursive the successors of a graph argument
39 * with mode reference and mark if it will be read,
42 * @param arg The graph argument with mode reference,
43 * that must be checked.
45 static unsigned analyze_arg(ir_node *arg, unsigned bits)
50 /* We must visit a node once to avoid endless recursion.*/
51 set_irn_link(arg, VISITED);
53 for (i = get_irn_n_outs(arg) - 1; i >= 0; --i) {
54 succ = get_irn_out(arg, i);
57 if (get_irn_link(succ) == VISITED)
60 /* We should not walk over the memory edge.*/
61 if (get_irn_mode(succ) == mode_M)
64 /* If we reach with the recursion a Call node and our reference
65 isn't the address of this Call we accept that the reference will
66 be read and written if the graph of the method represented by
67 "Call" isn't computed else we analyze that graph. If our
68 reference is the address of this
69 Call node that mean the reference will be read.*/
70 switch (get_irn_opcode(succ)) {
73 ir_node *ptr = get_Call_ptr(succ);
76 /* Hmm: not sure what this is, most likely a read */
77 bits |= ptr_access_read;
80 ir_op *op = get_irn_op(ptr);
83 if (op == op_SymConst && get_SymConst_kind(ptr) == symconst_addr_ent) {
84 meth_ent = get_SymConst_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);
93 else if (op == op_Sel && get_irp_callee_info_state() == irg_callee_info_consistent) {
94 /* is be a polymorphic call but callee information is available */
95 int i, n_params = get_Call_n_params(succ);
97 /* simply look into ALL possible callees */
98 for (i = get_Call_n_callees(succ) - 1; i >= 0; --i) {
99 meth_ent = get_Call_callee(succ, i);
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);
115 else /* can do anything */
116 bits |= ptr_access_all;
119 /* search stops here anyway */
123 /* We have reached a Store node => the reference is written or stored. */
124 if (get_Store_ptr(succ) == arg) {
126 bits |= ptr_access_write;
130 bits |= ptr_access_store;
133 /* search stops here anyway */
137 /* We have reached a Load node => the reference is read. */
138 bits |= ptr_access_read;
140 /* search stops here anyway */
144 /* our address is casted into something unknown. Break our search. */
145 bits = ptr_access_all;
152 /* If we know that, the argument will be read, write and stored, we
153 can break the recursion.*/
154 if (bits == ptr_access_all) {
155 bits = ptr_access_all;
160 * A calculation that do not lead to a reference mode ends our search.
161 * This is dangerous: It would allow to cast into integer and that cast back ...
162 * so, when we detect a Conv we go mad, see the Conv case above.
164 if (!mode_is_reference(get_irn_mode(succ)))
167 /* follow further the address calculation */
168 bits = analyze_arg(succ, bits);
170 set_irn_link(arg, NULL);
175 * Check if a argument of the ir graph with mode
176 * reference is read, write or both.
178 * @param irg The ir graph to analyze.
180 static void analyze_ent_args(ir_entity *ent)
183 ir_node *irg_args, *arg;
188 ptr_access_kind *rw_info;
190 mtp = get_entity_type(ent);
191 nparams = get_method_n_params(mtp);
193 ent->attr.mtd_attr.param_access = NEW_ARR_F(ptr_access_kind, nparams);
195 /* If the method haven't parameters we have
201 irg = get_entity_irg(ent);
203 /* we have not yet analyzed the graph, set ALL access for pointer args */
204 for (i = nparams - 1; i >= 0; --i)
205 ent->attr.mtd_attr.param_access[i] =
206 is_Pointer_type(get_method_param_type(mtp, i)) ? ptr_access_all : ptr_access_none;
209 /* no graph, no better info */
213 assure_irg_outs(irg);
215 irg_args = get_irg_args(irg);
217 /* A array to save the information for each argument with
219 NEW_ARR_A(ptr_access_kind, rw_info, nparams);
221 /* We initialize the element with none state. */
222 for (i = nparams - 1; i >= 0; --i)
223 rw_info[i] = ptr_access_none;
225 /* search for arguments with mode reference
227 for (i = get_irn_n_outs(irg_args) - 1; i >= 0; --i) {
228 arg = get_irn_out(irg_args, i);
229 arg_mode = get_irn_mode(arg);
230 proj_nr = get_Proj_proj(arg);
232 if (mode_is_reference(arg_mode))
233 rw_info[proj_nr] |= analyze_arg(arg, rw_info[proj_nr]);
236 /* copy the temporary info */
237 memcpy(ent->attr.mtd_attr.param_access, rw_info,
238 nparams * sizeof(ent->attr.mtd_attr.param_access[0]));
241 printf("\n%s:\n", get_entity_name(ent));
242 for (i = 0; i < nparams; ++i) {
243 if (is_Pointer_type(get_method_param_type(mtp, i)))
244 if (ent->attr.mtd_attr.param_access[i] != ptr_access_none) {
245 printf(" Pointer Arg %d access: ", i);
246 if (ent->attr.mtd_attr.param_access[i] & ptr_access_read)
248 if (ent->attr.mtd_attr.param_access[i] & ptr_access_write)
250 if (ent->attr.mtd_attr.param_access[i] & ptr_access_store)
259 * Analyze how pointer arguments of a given
260 * ir graph are accessed.
262 * @param irg The ir graph to analyze.
264 void analyze_irg_args(ir_graph *irg)
268 if (irg == get_const_code_irg())
271 ent = get_irg_entity(irg);
275 if (! ent->attr.mtd_attr.param_access)
276 analyze_ent_args(ent);
280 * Compute for a method with pointer parameter(s)
281 * if they will be read or written.
283 ptr_access_kind get_method_param_access(ir_entity *ent, int pos)
285 ir_type *mtp = get_entity_type(ent);
287 int is_variadic = get_method_variadicity(mtp) == variadicity_variadic;
289 assert(0 <= pos && (is_variadic || pos < get_method_n_params(mtp)));
292 if (ent->attr.mtd_attr.param_access) {
293 if (pos < ARR_LEN(ent->attr.mtd_attr.param_access))
294 return ent->attr.mtd_attr.param_access[pos];
296 return ptr_access_all;
299 analyze_ent_args(ent);
301 if (pos < ARR_LEN(ent->attr.mtd_attr.param_access))
302 return ent->attr.mtd_attr.param_access[pos];
304 return ptr_access_all;
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. */
316 * Compute the weight of a method parameter
318 * @param arg The parameter them weight muss be computed.
320 static float calc_method_param_weight(ir_node *arg)
324 float weight = null_weight;
326 /* We mark the nodes to avoid endless recursion */
327 set_irn_link(arg, VISITED);
329 for (i = get_irn_n_outs(arg) - 1; i >= 0; i--) {
330 succ = get_irn_out(arg, i);
333 if (get_irn_link(succ) == VISITED)
336 /* We should not walk over the memory edge.*/
337 if (get_irn_mode(succ) == mode_M)
340 /* We have reached a cmp and we must increase the
341 weight with the cmp_weight.*/
342 if (get_irn_op(succ) == op_Cmp) {
344 if (get_Cmp_left(succ) == arg)
345 op = get_Cmp_right(succ);
347 op = get_Cmp_left(succ);
349 if (is_irn_constlike(op)) {
350 weight += const_cmp_weight;
353 weight += cmp_weight;
355 else if (is_binop(succ)) {
356 /* We have reached a binop and we must increase the
357 weight with the binop_weight. If the other operand of the
358 binop is a constant we increase the weight with const_binop_weight
359 and call the function recursive.
361 if (get_binop_left(succ) == arg)
362 op = get_binop_right(succ);
364 op = get_binop_left(succ);
366 if (is_irn_constlike(op)) {
367 weight += const_binop_weight;
368 weight += calc_method_param_weight(succ);
371 weight += binop_weight;
372 } else if (is_unop(succ)) {
373 /* We have reached a binop and we must increase the
374 weight with the const_binop_weight and call the function recursive.*/
375 weight += const_binop_weight;
376 weight += calc_method_param_weight(succ);
379 set_irn_link(arg, NULL);
384 * Set a weight for each argument of a ir_graph.
385 * The args with a greater weight are good for optimize.
387 * @param ent The entity of the ir_graph.
389 static void analyze_method_params_weight(ir_entity *ent)
393 int nparams, i, proj_nr;
394 ir_node *irg_args, *arg;
396 mtp = get_entity_type(ent);
397 nparams = get_method_n_params(mtp);
399 /* allocate a new array. currently used as 'analysed' flag */
400 ent->attr.mtd_attr.param_weight = NEW_ARR_F(float, nparams);
402 /* If the method haven't parameters we have
408 irg = get_entity_irg(ent);
410 /* First we initialize the parameter weight with 0. */
411 for (i = nparams - 1; i >= 0; i--)
412 ent->attr.mtd_attr.param_weight[i] = null_weight;
415 /* no graph, no better info */
419 /* Call algorithm that computes the out edges */
420 assure_irg_outs(irg);
422 irg_args = get_irg_args(irg);
424 for (i = get_irn_n_outs(irg_args) - 1; i >= 0; --i) {
425 arg = get_irn_out(irg_args, i);
426 proj_nr = get_Proj_proj(arg);
427 ent->attr.mtd_attr.param_weight[proj_nr] += calc_method_param_weight(arg);
431 printf("\n%s:\n", get_entity_name(ent));
432 for (i = nparams - 1; i >= 0; --i)
433 printf("The weight of argument %i is %f \n", i, ent->param_weight[i]);
438 * Compute for a method with pointer parameter(s)
439 * if they will be read or written.
441 float get_method_param_weight(ir_entity *ent, int pos)
443 ir_type *mtp = get_entity_type(ent);
446 int is_variadic = get_method_variadicity(mtp) == variadicity_variadic;
447 assert(0 <= pos && (is_variadic || pos < get_method_n_params(mtp)));
450 if (ent->attr.mtd_attr.param_weight) {
451 if (pos < ARR_LEN(ent->attr.mtd_attr.param_weight))
452 return ent->attr.mtd_attr.param_weight[pos];
457 analyze_method_params_weight(ent);
459 if (pos < ARR_LEN(ent->attr.mtd_attr.param_weight))
460 return ent->attr.mtd_attr.param_weight[pos];
467 * Analyze argument's weight of a given
470 * @param irg The ir graph to analyze.
472 void analyze_irg_args_weight(ir_graph *irg)
476 ent = get_irg_entity(irg);
480 if (! ent->attr.mtd_attr.param_weight)
481 analyze_method_params_weight(ent);