Hidden parameters can be returned instead of the compound
[libfirm] / ir / lower / lower_calls.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/lower/lower_calls.c
4  * Purpose:     lowering of Calls with compound parameters
5  * Author:      Michael Beck
6  * Created:
7  * CVS-ID:      $Id$
8  * Copyright:   (c) 1998-2005 Universität Karlsruhe
9  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
10  */
11
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #ifdef HAVE_ALLOCA_H
17 #include <alloca.h>
18 #endif
19 #ifdef HAVE_MALLOC_H
20 #include <malloc.h>
21 #endif
22
23 #include "irprog_t.h"
24 #include "irnode_t.h"
25 #include "type_t.h"
26 #include "irmode_t.h"
27 #include "ircons.h"
28 #include "irgmod.h"
29 #include "irgwalk.h"
30 #include "typewalk.h"
31 #include "lower_calls.h"
32 #include "return.h"
33 #include "irtools.h"
34 #include "array.h"
35 #include "pmap.h"
36
37 /** A type map for def_find_pointer_type. */
38 static pmap *type_map;
39
40 /**
41  * Default implementation for finding a pointer type for a given element type.
42  * Simple create a new one.
43  */
44 static ir_type *def_find_pointer_type(ir_type *e_type, ir_mode *mode, int alignment)
45 {
46   ir_type *res;
47   pmap_entry *e;
48
49   /* Mode and alignment are always identical in all calls to def_find_pointer_type(), so
50      we simply can use a map from the element type to the pointer type. */
51   e = pmap_find(type_map, e_type);
52   if (e)
53     res = e->value;
54   else {
55     res = new_type_pointer(mangle_u(get_type_ident(e_type), new_id_from_chars("Ptr", 3)), e_type, mode);
56     set_type_alignment_bytes(res, alignment);
57     pmap_insert(type_map, e_type, res);
58   }
59   return res;
60 }
61
62 /**
63  * Creates a new lowered type for a method type with compound
64  * arguments. The new type is associated to the old one and returned.
65  *
66  * @param lp   parameter struct
67  * @param mtp  the method type to lower
68  *
69  * The current implementation expects that a lowered type already
70  * includes the necessary changes ...
71  */
72 static ir_type *create_modified_mtd_type(const lower_params_t *lp, ir_type *mtp)
73 {
74   ir_type *lowered, *ptr_tp;
75   ir_type **params, **results, *res_tp;
76   ir_mode *modes[MAX_REGISTER_RET_VAL];
77   int n_ress, n_params, nn_ress, nn_params, i, first_variadic;
78   ident *id;
79   add_hidden hidden_params;
80   variadicity var;
81
82   if (is_lowered_type(mtp)) {
83     /* the type is already lowered. Not handled yet. */
84     assert(0 && "lowered types NYI");
85   }
86
87   lowered = get_associated_type(mtp);
88   if (lowered)
89     return lowered;
90
91   n_ress   = get_method_n_ress(mtp);
92   NEW_ARR_A(ir_type *, results, n_ress);
93
94   n_params = get_method_n_params(mtp);
95   NEW_ARR_A(ir_type *, params, n_params + n_ress);
96
97   first_variadic = get_method_first_variadic_param_index(mtp);
98
99   hidden_params = lp->hidden_params;
100   if (hidden_params == ADD_HIDDEN_SMART &&
101     get_method_variadicity(mtp) == variadicity_variadic)
102         hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
103
104   if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
105     /* add hidden in front */
106     for (nn_ress = nn_params = i = 0; i < n_ress; ++i) {
107       res_tp = get_method_res_type(mtp, i);
108
109       if (is_compound_type(res_tp)) {
110         int n_regs = 0;
111
112         if (lp->flags & LF_SMALL_CMP_IN_REGS)
113           n_regs = lp->ret_compound_in_regs(res_tp, modes);
114
115         if (n_regs > 0) {
116           /* this compound will be returned solely in registers */
117           assert(0);
118         }
119         else {
120           /* this compound will be allocated on callers stack and its
121              address will be transmitted as a hidden parameter. */
122           ptr_tp = lp->find_pointer_type(res_tp, get_modeP_data(), lp->def_ptr_alignment);
123           params[nn_params++] = ptr_tp;
124           if (lp->flags & LF_RETURN_HIDDEN)
125             results[nn_ress++] = ptr_tp;
126         }
127       }
128       else
129         results[nn_ress++] = res_tp;
130     }
131
132     /* move the index of the first variadic parameter */
133     first_variadic += nn_params;
134
135     for (i = 0; i < n_params; ++i)
136       params[nn_params++] = get_method_param_type(mtp, i);
137   }
138   else {
139     /* add hidden parameters last */
140     assert(get_method_variadicity(mtp) == variadicity_non_variadic &&
141       "Cannot add hidden parameters at end of variadic function");
142
143     for (nn_params = 0; nn_params < n_params; ++nn_params)
144       params[nn_params] = get_method_param_type(mtp, nn_params);
145
146     for (nn_ress = i = 0; i < n_ress; ++i) {
147       res_tp = get_method_res_type(mtp, i);
148
149       if (is_compound_type(res_tp))
150         params[nn_params++] = lp->find_pointer_type(res_tp, get_modeP_data(), lp->def_ptr_alignment);
151       else
152         results[nn_ress++] = res_tp;
153     }
154   }
155
156   /* create the new type */
157   id = mangle_u(new_id_from_chars("L", 1), get_type_ident(mtp));
158   lowered = new_d_type_method(id, nn_params, nn_ress, get_type_dbg_info(mtp));
159
160   /* fill it */
161   for (i = 0; i < nn_params; ++i)
162     set_method_param_type(lowered, i, params[i]);
163   for (i = 0; i < nn_ress; ++i)
164     set_method_res_type(lowered, i, results[i]);
165
166   var = get_method_variadicity(mtp);
167   set_method_variadicity(lowered, var);
168   if (var == variadicity_variadic)
169     set_method_first_variadic_param_index(lowered, first_variadic);
170
171   /* associate the lowered type with the original one for easier access */
172   set_lowered_type(mtp, lowered);
173
174   return lowered;
175 }
176
177 /**
178  * A call list entry.
179  */
180 typedef struct cl_entry cl_entry;
181 struct cl_entry {
182   cl_entry *next;   /**< Pointer to the next entry. */
183   ir_node  *call;   /**< Pointer to the Call node. */
184   ir_node  *copyb;  /**< List of all CopyB nodes. */
185 };
186
187 /**
188  * Walker environment for fix_args_and_collect_calls().
189  */
190 typedef struct _wlk_env_t {
191   int                  arg_shift;     /**< The Argument index shift for parameters. */
192   int                  first_hidden;  /**< The index of the first hidden argument. */
193   struct obstack       obst;          /**< An obstack to allocate the data on. */
194   cl_entry             *cl_list;      /**< The call list. */
195   pmap                 *dummy_map;    /**< A map for finding the dummy arguments. */
196   unsigned             dnr;           /**< The dummy index number. */
197   const lower_params_t *params;       /**< lowering parameters */
198 } wlk_env;
199
200 /**
201  * Return the call list entry of a call node.
202  * If no entry exists yet, allocate one and enter the node into
203  * the call list of the environment.
204  *
205  * @param call   A Call node.
206  * @param env    The environment.
207  */
208 static cl_entry *get_Call_entry(ir_node *call, wlk_env *env) {
209   cl_entry *res = get_irn_link(call);
210   if (res == NULL) {
211     cl_entry *res = obstack_alloc(&env->obst, sizeof(*res));
212     res->next  = env->cl_list;
213     res->call  = call;
214     res->copyb = NULL;
215     set_irn_link(call, res);
216     env->cl_list = res;
217   }
218   return res;
219 }
220
221 /**
222  * Post walker: shift all parameter indeces
223  * and collect Calls with compound returns in the call list.
224  */
225 static void fix_args_and_collect_calls(ir_node *n, void *ctx) {
226   wlk_env *env = ctx;
227   int i;
228   ir_type *ctp;
229   ir_op *op = get_irn_op(n);
230
231   if (env->arg_shift > 0 && op == op_Proj) {
232     ir_node *pred = get_Proj_pred(n);
233
234     /* Fix the argument numbers */
235     if (pred == get_irg_args(current_ir_graph)) {
236       long pnr = get_Proj_proj(n);
237       set_Proj_proj(n, pnr + env->arg_shift);
238     }
239   }
240   else if (op == op_Call) {
241     ctp = get_Call_type(n);
242     if (env->params->flags & LF_COMPOUND_RETURN) {
243       /* check for compound returns */
244       for (i = get_method_n_ress(ctp) -1; i >= 0; --i) {
245         if (is_compound_type(get_method_res_type(ctp, i))) {
246           /*
247            * This is a call with a compound return. As the result
248            * might be ignored, we must put it in the list.
249            */
250           (void)get_Call_entry(n, env);
251         }
252       }
253     }
254   }
255   else if (op == op_CopyB && env->params->flags & LF_COMPOUND_RETURN) {
256     /* check for compound returns */
257     ir_node *src = get_CopyB_src(n);
258     /* older scheme using value_res_ent */
259     if (is_Sel(src)) {
260       ir_node *proj = get_Sel_ptr(src);
261       if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_P_value_res_base) {
262         ir_node *call = get_Proj_pred(proj);
263         if (is_Call(call)) {
264           /* found a CopyB from compound Call result */
265           cl_entry *e = get_Call_entry(call, env);
266           set_irn_link(n, e->copyb);
267           e->copyb = n;
268         }
269       }
270     } else
271     /* new scheme: compound results are determined by the call type only */
272     if (is_Proj(src)) {
273       ir_node *proj = get_Proj_pred(src);
274       if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_T_result) {
275         ir_node *call = get_Proj_pred(proj);
276         if (is_Call(call)) {
277           ctp = get_Call_type(call);
278           if (is_compound_type(get_method_res_type(ctp, get_Proj_proj(src)))) {
279             /* found a CopyB from compound Call result */
280             cl_entry *e = get_Call_entry(call, env);
281             set_irn_link(n, e->copyb);
282             e->copyb = n;
283           }
284         }
285       }
286     }
287   }
288 }
289
290 /**
291  * Returns non-zero if a node is a compound address
292  * of a frame-type entity.
293  *
294  * @param ft   the frame type
295  * @param adr  the node
296  */
297 static int is_compound_address(ir_type *ft, ir_node *adr)
298 {
299   entity *ent;
300
301   if (! is_Sel(adr))
302     return 0;
303   if (get_Sel_n_indexs(adr) != 0)
304     return 0;
305   ent = get_Sel_entity(adr);
306   return get_entity_owner(ent) == ft;
307 }
308
309 /** A pair for the copy-return-optimization. */
310 typedef struct cr_pair {
311   entity *ent;    /**< the entity than can be removed from the frame */
312   ir_node *arg;   /**< the argument that replaces the entities address */
313 } cr_pair;
314
315 /**
316  * Post walker: fixes all entities addresses for the copy-return
317  * optimization.
318  *
319  * Note: We expect the length of the cr_pair array (ie number of compound
320  * return values) to be 1 (C, C++) in almost all cases, so ignore the
321  * linear search complexity here.
322  */
323 static void do_copy_return_opt(ir_node *n, void *ctx) {
324   cr_pair *arr = ctx;
325   int i;
326
327   if (is_Sel(n)) {
328     entity *ent = get_Sel_entity(n);
329
330     for (i = ARR_LEN(arr) - 1; i >= 0; --i) {
331       if (ent == arr[i].ent) {
332         exchange(n, arr[i].arg);
333         break;
334       }
335     }
336   }
337 }
338
339 /**
340  * Return a Sel node that selects a dummy argument of type tp.
341  * Dummy arguments are only needed once and we use a map
342  * to store them.
343  * We could even assign all dummy arguments the same offset
344  * in the frame type ...
345  *
346  * @param irg    the graph
347  * @param block  the block where a newly create Sel should be placed
348  * @param tp     the type of the dummy entity that should be create
349  * @param env    the environment
350  */
351 static ir_node *get_dummy_sel(ir_graph *irg, ir_node *block, ir_type *tp, wlk_env *env)
352 {
353   ir_node *sel;
354   pmap_entry *e;
355
356   /* use a map the check if we already create such an entity */
357   e = pmap_find(env->dummy_map, tp);
358   if (e)
359     sel = e->value;
360   else {
361     ir_type *ft = get_irg_frame_type(irg);
362     entity *ent;
363     char buf[16];
364
365     snprintf(buf, sizeof(buf), "dummy.%u", env->dnr++);
366     ent = new_entity(ft, new_id_from_str(buf), tp);
367     sel = new_r_simpleSel(
368       irg,
369       block,
370       get_irg_no_mem(irg),
371       get_irg_frame(irg),
372       ent);
373     pmap_insert(env->dummy_map, tp, sel);
374
375     if (get_type_state(ft) == layout_fixed) {
376       /* Fix the layout again */
377       assert(0 && "Fixed layout not implemented");
378     }
379   }
380   return sel;
381 }
382
383 /**
384  * Add the hidden parameter from the CopyB node to the Call node.
385  *
386  * @param irg    the graph
387  * @param n_com  number of compound results (will be number of hidden parameters)
388  * @param ins    in array to store the hidden parameters into
389  * @param entry  the call list
390  * @param env    the environment
391  */
392 static void add_hidden_param(ir_graph *irg, int n_com, ir_node **ins, cl_entry *entry, wlk_env *env)
393 {
394   ir_node *p, *n, *src, *mem;
395   entity *ent;
396   ir_type *owner;
397   int idx, n_args;
398
399   n_args = 0;
400   for (p = entry->copyb; p; p = n) {
401     n   = get_irn_link(p);
402     src = get_CopyB_src(p);
403
404     /* old scheme using value_res_ent */
405     if (is_Sel(src)) {
406       ent = get_Sel_entity(src);
407       owner = get_entity_owner(ent);
408
409       /* find the hidden parameter index */
410       for (idx = 0; idx < get_struct_n_members(owner); ++idx)
411         if (get_struct_member(owner, idx) == ent)
412           break;
413       assert(idx < get_struct_n_members(owner));
414     }
415     else
416
417     /* new scheme: compound returns are determined by the call type and are Proj's */
418     idx = get_Proj_proj(src);
419
420     ins[idx] = get_CopyB_dst(p);
421     mem      = get_CopyB_mem(p);
422
423     /* get rid of the CopyB */
424     turn_into_tuple(p, pn_CopyB_max);
425     set_Tuple_pred(p, pn_CopyB_M_regular, mem);
426     set_Tuple_pred(p, pn_CopyB_M_except, get_irg_bad(irg));
427     set_Tuple_pred(p, pn_CopyB_X_except, get_irg_bad(irg));
428     ++n_args;
429   }
430
431   /* now create dummy entities for function with ignored return value */
432   if (n_args < n_com) {
433     ir_type *ctp = get_Call_type(entry->call);
434     int i, j;
435
436     if (is_lowered_type(ctp))
437       ctp = get_associated_type(ctp);
438
439     for (j = i = 0; i < get_method_n_ress(ctp); ++i) {
440       ir_type *rtp = get_method_res_type(ctp, i);
441       if (is_compound_type(rtp)) {
442         if (ins[j] == NULL)
443           ins[j] = get_dummy_sel(irg, get_nodes_block(entry->call), rtp, env);
444         ++j;
445       }
446     }
447   }
448 }
449
450 /**
451  * Fix all calls on a call list by adding hidden parameters.
452  *
453  * @param irg  the graph
454  * @param env  the environment
455  */
456 static void fix_call_list(ir_graph *irg, wlk_env *env) {
457   const lower_params_t *lp = env->params;
458   cl_entry *p;
459   ir_node *call, **new_in;
460   ir_type *ctp, *lowered_mtp;
461   add_hidden hidden_params;
462   int i, n_params, n_com, pos;
463
464   new_in = NEW_ARR_F(ir_node *, 0);
465   for (p = env->cl_list; p; p = p->next) {
466     call = p->call;
467     ctp = get_Call_type(call);
468     lowered_mtp = create_modified_mtd_type(lp, ctp);
469     set_Call_type(call, lowered_mtp);
470
471     hidden_params = lp->hidden_params;
472     if (hidden_params == ADD_HIDDEN_SMART &&
473       get_method_variadicity(ctp) == variadicity_variadic)
474       hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
475
476     n_params = get_Call_n_params(call);
477
478     n_com = 0;
479     for (i = get_method_n_ress(ctp) - 1; i >= 0; --i) {
480       if (is_compound_type(get_method_res_type(ctp, i)))
481         ++n_com;
482     }
483     pos = 2;
484     ARR_RESIZE(ir_node *, new_in, n_params + n_com + pos);
485     memset(new_in, 0, sizeof(*new_in) * (n_params + n_com + pos));
486     if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
487       add_hidden_param(irg, n_com, &new_in[pos], p, env);
488       pos += n_com;
489     }
490     /* copy all other parameters */
491     for (i = 0; i < n_params; ++i)
492       new_in[pos++] = get_Call_param(call, i);
493     if (hidden_params == ADD_HIDDEN_ALWAYS_LAST) {
494       add_hidden_param(irg, n_com, &new_in[pos], p, env);
495       pos += n_com;
496     }
497     new_in[0] = get_Call_mem(call);
498     new_in[1] = get_Call_ptr(call);
499
500     set_irn_in(call, n_params + n_com + 2, new_in);
501   }
502 }
503
504 /**
505  * Transform a graph. If it has compound parameter returns,
506  * remove them and use the hidden parameter instead.
507  * If it calls methods with compound parameter returns, add hidden
508  * parameters.
509  *
510  * @param lp   parameter struct
511  * @param irg  the graph to transform
512  */
513 static void transform_irg(const lower_params_t *lp, ir_graph *irg)
514 {
515   entity *ent = get_irg_entity(irg);
516   ir_type *mtp, *lowered_mtp, *res_tp, *ft;
517   int i, j, k, n_ress, n_ret_com, n_cr_opt;
518   ir_node **new_in, *ret, *endbl, *bl, *mem, *copy;
519   cr_pair *cr_opt;
520   wlk_env env;
521   add_hidden hidden_params;
522
523   assert(ent && "Cannot tranform graph without an entity");
524   assert(get_irg_phase_state(irg) == phase_high && "call lowering must be done in phase high");
525
526   mtp    = get_entity_type(ent);
527
528   if (lp->flags & LF_COMPOUND_RETURN) {
529     /* calculate the number of compound returns */
530     n_ress = get_method_n_ress(mtp);
531     for (n_ret_com = i = 0; i < n_ress; ++i) {
532       res_tp = get_method_res_type(mtp, i);
533
534       if (is_compound_type(res_tp))
535         ++n_ret_com;
536     }
537   }
538
539   if (n_ret_com) {
540     /* much easier if we have only one return */
541     normalize_one_return(irg);
542
543     /* This graph has a compound argument. Create a new type */
544     lowered_mtp = create_modified_mtd_type(lp, mtp);
545     set_entity_type(ent, lowered_mtp);
546
547     hidden_params = lp->hidden_params;
548     if (hidden_params == ADD_HIDDEN_SMART &&
549       get_method_variadicity(mtp) == variadicity_variadic)
550       hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
551
552     if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
553       /* hidden arguments are added first */
554       env.arg_shift    = n_ret_com;
555       env.first_hidden = 0;
556     }
557     else {
558       /* hidden arguments are added last */
559       env.arg_shift    = 0;
560       env.first_hidden = get_method_n_params(mtp);
561     }
562   }
563   else {
564     /* we must only search for calls */
565     env.arg_shift = 0;
566   }
567   obstack_init(&env.obst);
568   env.cl_list   = NULL;
569   env.dummy_map = pmap_create_ex(8);
570   env.dnr       = 0;
571   env.params    = lp;
572
573   /* scan the code, fix argument numbers and collect calls. */
574   irg_walk_graph(irg, firm_clear_link, fix_args_and_collect_calls, &env);
575
576   /* fix all calls */
577   if (env.cl_list)
578     fix_call_list(irg, &env);
579
580   if (n_ret_com) {
581     /*
582      * Now fix the Return node of the current graph.
583      */
584
585     /* STEP 1: find the return. This is simple, we have normalized the graph. */
586     endbl = get_irg_end_block(irg);
587     ret = NULL;
588     for (i = get_Block_n_cfgpreds(endbl) - 1; i >= 0; --i) {
589       ir_node *pred = get_Block_cfgpred(endbl, i);
590
591       if (is_Return(pred)) {
592         ret = pred;
593         break;
594       }
595     }
596     /* there should always be a return */
597     assert(ret);
598
599     /*
600      * STEP 2: fix it. For all compound return values add a CopyB,
601      * all others are copied.
602      */
603     NEW_ARR_A(ir_node *, new_in, n_ress + 1);
604
605     bl  = get_nodes_block(ret);
606     mem = get_Return_mem(ret);
607
608     ft = get_irg_frame_type(irg);
609     NEW_ARR_A(cr_pair, cr_opt, n_ret_com);
610     n_cr_opt = 0;
611     for (j = 1, i = k = 0; i < n_ress; ++i) {
612       ir_node *pred = get_Return_res(ret, i);
613       res_tp = get_method_res_type(mtp, i);
614
615       if (is_compound_type(res_tp)) {
616         ir_node *arg = get_irg_args(irg);
617         arg = new_r_Proj(irg, get_nodes_block(arg), arg, mode_P_data, env.first_hidden + k);
618         ++k;
619
620         if (is_compound_address(ft, pred)) {
621           /* we can do the copy-return optimization here */
622           cr_opt[n_cr_opt].ent = get_Sel_entity(pred);
623           cr_opt[n_cr_opt].arg = arg;
624           ++n_cr_opt;
625         }
626         else { /* copy-return optimization is impossible, do the copy. */
627           copy = new_r_CopyB(
628                   irg, bl,
629                   mem,
630                   arg,
631                   pred,
632                   res_tp
633                  );
634           mem = new_r_Proj(irg, bl, copy, mode_M, pn_CopyB_M_regular);
635         }
636         if (lp->flags & LF_RETURN_HIDDEN) {
637           new_in[j] = arg;
638           ++j;
639         }
640       }
641       else { /* scalar return value */
642         new_in[j] = pred;
643         ++j;
644       }
645     }
646     /* replace the in of the Return */
647     new_in[0] = mem;
648     set_irn_in(ret, j, new_in);
649
650     if (n_cr_opt > 0) {
651       irg_walk_graph(irg, NULL, do_copy_return_opt, cr_opt);
652
653       for (i = ARR_LEN(cr_opt) - 1; i >= 0; --i) {
654         remove_class_member(ft, cr_opt[i].ent);
655       }
656     }
657   } /* if (n_ret_com) */
658
659   pmap_destroy(env.dummy_map);
660   obstack_free(&env.obst, NULL);
661 }
662
663 /**
664  * Returns non-zero if the given type is a method
665  * type that must be lowered.
666  *
667  * @param lp  lowering parameters
668  * @param tp  The type.
669  */
670 static int must_be_lowered(const lower_params_t *lp, ir_type *tp) {
671   int i, n_ress;
672   ir_type *res_tp;
673
674   if (is_Method_type(tp)) {
675     if (lp->flags & LF_COMPOUND_RETURN) {
676       /* check for compound returns */
677       n_ress = get_method_n_ress(tp);
678       for (i = 0; i < n_ress; ++i) {
679         res_tp = get_method_res_type(tp, i);
680
681         if (is_compound_type(res_tp))
682           return 1;
683       }
684     }
685   }
686   return 0;
687 }
688
689 /**
690  * type-walker: lower all method types of entities
691  * and points-to types.
692  */
693 static void lower_method_types(type_or_ent *tore, void *env)
694 {
695   const lower_params_t *lp = env;
696   ir_type *tp;
697
698   /* fix method entities */
699   if (is_entity(tore)) {
700     entity *ent = (entity *)tore;
701     tp = get_entity_type(ent);
702
703     if (must_be_lowered(lp, tp)) {
704       tp = create_modified_mtd_type(lp, tp);
705       set_entity_type(ent, tp);
706     }
707   }
708   else {
709     tp = (ir_type *)tore;
710
711     /* fix pointer to methods */
712     if (is_Pointer_type(tp)) {
713       ir_type *etp = get_pointer_points_to_type(tp);
714       if (must_be_lowered(lp, etp)) {
715         etp = create_modified_mtd_type(lp, etp);
716         set_pointer_points_to_type(tp, etp);
717       }
718     }
719   }
720 }
721
722 /*
723  * Lower calls with compound parameters and return types.
724  * This function does the following transformations:
725  *
726  * - Adds a new (hidden) pointer parameter for
727  *   any return compound type.
728  *
729  * - Use of the hidden parameters in the function code.
730  *
731  * - Change all calls to functions with compound return
732  *   by providing space for the hidden parameter on the callers
733  *   stack.
734  *
735  * - Replace a possible block copy after the function call.
736  */
737 void lower_calls_with_compounds(const lower_params_t *params)
738 {
739   int i;
740   ir_graph *irg;
741   lower_params_t param = *params;
742
743   if (param.find_pointer_type == NULL) {
744     param.find_pointer_type = def_find_pointer_type;
745     type_map = pmap_create_ex(8);
746   }
747   else
748     type_map = NULL;
749
750   /* first step: Transform all graphs */
751   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
752     irg = get_irp_irg(i);
753
754     if (irg == get_const_code_irg())
755       continue;
756
757     transform_irg(&param, irg);
758   }
759
760   /* second step: Lower all method types of visible entities */
761   type_walk(NULL, lower_method_types, &param);
762
763   if (type_map)
764     pmap_destroy(type_map);
765 }