DBG_OPT_SYNC added
[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           break;
252         }
253       }
254     }
255   }
256   else if (op == op_CopyB && env->params->flags & LF_COMPOUND_RETURN) {
257     /* check for compound returns */
258     ir_node *src = get_CopyB_src(n);
259     /* older scheme using value_res_ent */
260     if (is_Sel(src)) {
261       ir_node *proj = get_Sel_ptr(src);
262       if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_P_value_res_base) {
263         ir_node *call = get_Proj_pred(proj);
264         if (is_Call(call)) {
265           /* found a CopyB from compound Call result */
266           cl_entry *e = get_Call_entry(call, env);
267           set_irn_link(n, e->copyb);
268           e->copyb = n;
269         }
270       }
271     } else
272     /* new scheme: compound results are determined by the call type only */
273     if (is_Proj(src)) {
274       ir_node *proj = get_Proj_pred(src);
275       if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_T_result) {
276         ir_node *call = get_Proj_pred(proj);
277         if (is_Call(call)) {
278           ctp = get_Call_type(call);
279           if (is_compound_type(get_method_res_type(ctp, get_Proj_proj(src)))) {
280             /* found a CopyB from compound Call result */
281             cl_entry *e = get_Call_entry(call, env);
282             set_irn_link(n, e->copyb);
283             e->copyb = n;
284           }
285         }
286       }
287     }
288   }
289 }
290
291 /**
292  * Returns non-zero if a node is a compound address
293  * of a frame-type entity.
294  *
295  * @param ft   the frame type
296  * @param adr  the node
297  */
298 static int is_compound_address(ir_type *ft, ir_node *adr)
299 {
300   entity *ent;
301
302   if (! is_Sel(adr))
303     return 0;
304   if (get_Sel_n_indexs(adr) != 0)
305     return 0;
306   ent = get_Sel_entity(adr);
307   return get_entity_owner(ent) == ft;
308 }
309
310 /** A pair for the copy-return-optimization. */
311 typedef struct cr_pair {
312   entity *ent;    /**< the entity than can be removed from the frame */
313   ir_node *arg;   /**< the argument that replaces the entities address */
314 } cr_pair;
315
316 /**
317  * Post walker: fixes all entities addresses for the copy-return
318  * optimization.
319  *
320  * Note: We expect the length of the cr_pair array (ie number of compound
321  * return values) to be 1 (C, C++) in almost all cases, so ignore the
322  * linear search complexity here.
323  */
324 static void do_copy_return_opt(ir_node *n, void *ctx) {
325   cr_pair *arr = ctx;
326   int i;
327
328   if (is_Sel(n)) {
329     entity *ent = get_Sel_entity(n);
330
331     for (i = ARR_LEN(arr) - 1; i >= 0; --i) {
332       if (ent == arr[i].ent) {
333         exchange(n, arr[i].arg);
334         break;
335       }
336     }
337   }
338 }
339
340 /**
341  * Return a Sel node that selects a dummy argument of type tp.
342  * Dummy arguments are only needed once and we use a map
343  * to store them.
344  * We could even assign all dummy arguments the same offset
345  * in the frame type ...
346  *
347  * @param irg    the graph
348  * @param block  the block where a newly create Sel should be placed
349  * @param tp     the type of the dummy entity that should be create
350  * @param env    the environment
351  */
352 static ir_node *get_dummy_sel(ir_graph *irg, ir_node *block, ir_type *tp, wlk_env *env)
353 {
354   ir_node *sel;
355   pmap_entry *e;
356
357   /* use a map the check if we already create such an entity */
358   e = pmap_find(env->dummy_map, tp);
359   if (e)
360     sel = e->value;
361   else {
362     ir_type *ft = get_irg_frame_type(irg);
363     entity *ent;
364     char buf[16];
365
366     snprintf(buf, sizeof(buf), "dummy.%u", env->dnr++);
367     ent = new_entity(ft, new_id_from_str(buf), tp);
368     sel = new_r_simpleSel(
369       irg,
370       block,
371       get_irg_no_mem(irg),
372       get_irg_frame(irg),
373       ent);
374     pmap_insert(env->dummy_map, tp, sel);
375
376     if (get_type_state(ft) == layout_fixed) {
377       /* Fix the layout again */
378       assert(0 && "Fixed layout not implemented");
379     }
380   }
381   return sel;
382 }
383
384 /**
385  * Add the hidden parameter from the CopyB node to the Call node.
386  *
387  * @param irg    the graph
388  * @param n_com  number of compound results (will be number of hidden parameters)
389  * @param ins    in array to store the hidden parameters into
390  * @param entry  the call list
391  * @param env    the environment
392  */
393 static void add_hidden_param(ir_graph *irg, int n_com, ir_node **ins, cl_entry *entry, wlk_env *env)
394 {
395   ir_node *p, *n, *src, *mem;
396   entity *ent;
397   ir_type *owner;
398   int idx, n_args;
399
400   n_args = 0;
401   for (p = entry->copyb; p; p = n) {
402     n   = get_irn_link(p);
403     src = get_CopyB_src(p);
404
405     /* old scheme using value_res_ent */
406     if (is_Sel(src)) {
407       ent = get_Sel_entity(src);
408       owner = get_entity_owner(ent);
409
410       /* find the hidden parameter index */
411       for (idx = 0; idx < get_struct_n_members(owner); ++idx)
412         if (get_struct_member(owner, idx) == ent)
413           break;
414       assert(idx < get_struct_n_members(owner));
415     }
416     else
417
418     /* new scheme: compound returns are determined by the call type and are Proj's */
419     idx = get_Proj_proj(src);
420
421     ins[idx] = get_CopyB_dst(p);
422     mem      = get_CopyB_mem(p);
423
424     /* get rid of the CopyB */
425     turn_into_tuple(p, pn_CopyB_max);
426     set_Tuple_pred(p, pn_CopyB_M_regular, mem);
427     set_Tuple_pred(p, pn_CopyB_M_except, get_irg_bad(irg));
428     set_Tuple_pred(p, pn_CopyB_X_except, get_irg_bad(irg));
429     ++n_args;
430   }
431
432   /* now create dummy entities for function with ignored return value */
433   if (n_args < n_com) {
434     ir_type *ctp = get_Call_type(entry->call);
435     int i, j;
436
437     if (is_lowered_type(ctp))
438       ctp = get_associated_type(ctp);
439
440     for (j = i = 0; i < get_method_n_ress(ctp); ++i) {
441       ir_type *rtp = get_method_res_type(ctp, i);
442       if (is_compound_type(rtp)) {
443         if (ins[j] == NULL)
444           ins[j] = get_dummy_sel(irg, get_nodes_block(entry->call), rtp, env);
445         ++j;
446       }
447     }
448   }
449 }
450
451 /**
452  * Fix all calls on a call list by adding hidden parameters.
453  *
454  * @param irg  the graph
455  * @param env  the environment
456  */
457 static void fix_call_list(ir_graph *irg, wlk_env *env) {
458   const lower_params_t *lp = env->params;
459   cl_entry *p;
460   ir_node *call, **new_in;
461   ir_type *ctp, *lowered_mtp;
462   add_hidden hidden_params;
463   int i, n_params, n_com, pos;
464
465   new_in = NEW_ARR_F(ir_node *, 0);
466   for (p = env->cl_list; p; p = p->next) {
467     call = p->call;
468     ctp = get_Call_type(call);
469     lowered_mtp = create_modified_mtd_type(lp, ctp);
470     set_Call_type(call, lowered_mtp);
471
472     hidden_params = lp->hidden_params;
473     if (hidden_params == ADD_HIDDEN_SMART &&
474       get_method_variadicity(ctp) == variadicity_variadic)
475       hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
476
477     n_params = get_Call_n_params(call);
478
479     n_com = 0;
480     for (i = get_method_n_ress(ctp) - 1; i >= 0; --i) {
481       if (is_compound_type(get_method_res_type(ctp, i)))
482         ++n_com;
483     }
484     pos = 2;
485     ARR_RESIZE(ir_node *, new_in, n_params + n_com + pos);
486     memset(new_in, 0, sizeof(*new_in) * (n_params + n_com + pos));
487     if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
488       add_hidden_param(irg, n_com, &new_in[pos], p, env);
489       pos += n_com;
490     }
491     /* copy all other parameters */
492     for (i = 0; i < n_params; ++i)
493       new_in[pos++] = get_Call_param(call, i);
494     if (hidden_params == ADD_HIDDEN_ALWAYS_LAST) {
495       add_hidden_param(irg, n_com, &new_in[pos], p, env);
496       pos += n_com;
497     }
498     new_in[0] = get_Call_mem(call);
499     new_in[1] = get_Call_ptr(call);
500
501     set_irn_in(call, n_params + n_com + 2, new_in);
502   }
503 }
504
505 /**
506  * Transform a graph. If it has compound parameter returns,
507  * remove them and use the hidden parameter instead.
508  * If it calls methods with compound parameter returns, add hidden
509  * parameters.
510  *
511  * @param lp   parameter struct
512  * @param irg  the graph to transform
513  */
514 static void transform_irg(const lower_params_t *lp, ir_graph *irg)
515 {
516   entity *ent = get_irg_entity(irg);
517   ir_type *mtp, *lowered_mtp, *res_tp, *ft;
518   int i, j, k, n_ress, n_ret_com, n_cr_opt;
519   ir_node **new_in, *ret, *endbl, *bl, *mem, *copy;
520   cr_pair *cr_opt;
521   wlk_env env;
522   add_hidden hidden_params;
523
524   assert(ent && "Cannot tranform graph without an entity");
525   assert(get_irg_phase_state(irg) == phase_high && "call lowering must be done in phase high");
526
527   mtp    = get_entity_type(ent);
528
529   if (lp->flags & LF_COMPOUND_RETURN) {
530     /* calculate the number of compound returns */
531     n_ress = get_method_n_ress(mtp);
532     for (n_ret_com = i = 0; i < n_ress; ++i) {
533       res_tp = get_method_res_type(mtp, i);
534
535       if (is_compound_type(res_tp))
536         ++n_ret_com;
537     }
538   }
539
540   if (n_ret_com) {
541     /* much easier if we have only one return */
542     normalize_one_return(irg);
543
544     /* This graph has a compound argument. Create a new type */
545     lowered_mtp = create_modified_mtd_type(lp, mtp);
546     set_entity_type(ent, lowered_mtp);
547
548     hidden_params = lp->hidden_params;
549     if (hidden_params == ADD_HIDDEN_SMART &&
550       get_method_variadicity(mtp) == variadicity_variadic)
551       hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
552
553     if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
554       /* hidden arguments are added first */
555       env.arg_shift    = n_ret_com;
556       env.first_hidden = 0;
557     }
558     else {
559       /* hidden arguments are added last */
560       env.arg_shift    = 0;
561       env.first_hidden = get_method_n_params(mtp);
562     }
563   }
564   else {
565     /* we must only search for calls */
566     env.arg_shift = 0;
567   }
568   obstack_init(&env.obst);
569   env.cl_list   = NULL;
570   env.dummy_map = pmap_create_ex(8);
571   env.dnr       = 0;
572   env.params    = lp;
573
574   /* scan the code, fix argument numbers and collect calls. */
575   irg_walk_graph(irg, firm_clear_link, fix_args_and_collect_calls, &env);
576
577   /* fix all calls */
578   if (env.cl_list)
579     fix_call_list(irg, &env);
580
581   if (n_ret_com) {
582     /*
583      * Now fix the Return node of the current graph.
584      */
585
586     /* STEP 1: find the return. This is simple, we have normalized the graph. */
587     endbl = get_irg_end_block(irg);
588     ret = NULL;
589     for (i = get_Block_n_cfgpreds(endbl) - 1; i >= 0; --i) {
590       ir_node *pred = get_Block_cfgpred(endbl, i);
591
592       if (is_Return(pred)) {
593         ret = pred;
594         break;
595       }
596     }
597     /* there should always be a return */
598     assert(ret);
599
600     /*
601      * STEP 2: fix it. For all compound return values add a CopyB,
602      * all others are copied.
603      */
604     NEW_ARR_A(ir_node *, new_in, n_ress + 1);
605
606     bl  = get_nodes_block(ret);
607     mem = get_Return_mem(ret);
608
609     ft = get_irg_frame_type(irg);
610     NEW_ARR_A(cr_pair, cr_opt, n_ret_com);
611     n_cr_opt = 0;
612     for (j = 1, i = k = 0; i < n_ress; ++i) {
613       ir_node *pred = get_Return_res(ret, i);
614       res_tp = get_method_res_type(mtp, i);
615
616       if (is_compound_type(res_tp)) {
617         ir_node *arg = get_irg_args(irg);
618         arg = new_r_Proj(irg, get_nodes_block(arg), arg, mode_P_data, env.first_hidden + k);
619         ++k;
620
621         if (is_compound_address(ft, pred)) {
622           /* we can do the copy-return optimization here */
623           cr_opt[n_cr_opt].ent = get_Sel_entity(pred);
624           cr_opt[n_cr_opt].arg = arg;
625           ++n_cr_opt;
626         }
627         else { /* copy-return optimization is impossible, do the copy. */
628           copy = new_r_CopyB(
629                   irg, bl,
630                   mem,
631                   arg,
632                   pred,
633                   res_tp
634                  );
635           mem = new_r_Proj(irg, bl, copy, mode_M, pn_CopyB_M_regular);
636         }
637         if (lp->flags & LF_RETURN_HIDDEN) {
638           new_in[j] = arg;
639           ++j;
640         }
641       }
642       else { /* scalar return value */
643         new_in[j] = pred;
644         ++j;
645       }
646     }
647     /* replace the in of the Return */
648     new_in[0] = mem;
649     set_irn_in(ret, j, new_in);
650
651     if (n_cr_opt > 0) {
652       irg_walk_graph(irg, NULL, do_copy_return_opt, cr_opt);
653
654       for (i = ARR_LEN(cr_opt) - 1; i >= 0; --i) {
655         remove_class_member(ft, cr_opt[i].ent);
656       }
657     }
658   } /* if (n_ret_com) */
659
660   pmap_destroy(env.dummy_map);
661   obstack_free(&env.obst, NULL);
662 }
663
664 /**
665  * Returns non-zero if the given type is a method
666  * type that must be lowered.
667  *
668  * @param lp  lowering parameters
669  * @param tp  The type.
670  */
671 static int must_be_lowered(const lower_params_t *lp, ir_type *tp) {
672   int i, n_ress;
673   ir_type *res_tp;
674
675   if (is_Method_type(tp)) {
676     if (lp->flags & LF_COMPOUND_RETURN) {
677       /* check for compound returns */
678       n_ress = get_method_n_ress(tp);
679       for (i = 0; i < n_ress; ++i) {
680         res_tp = get_method_res_type(tp, i);
681
682         if (is_compound_type(res_tp))
683           return 1;
684       }
685     }
686   }
687   return 0;
688 }
689
690 /**
691  * type-walker: lower all method types of entities
692  * and points-to types.
693  */
694 static void lower_method_types(type_or_ent *tore, void *env)
695 {
696   const lower_params_t *lp = env;
697   ir_type *tp;
698
699   /* fix method entities */
700   if (is_entity(tore)) {
701     entity *ent = (entity *)tore;
702     tp = get_entity_type(ent);
703
704     if (must_be_lowered(lp, tp)) {
705       tp = create_modified_mtd_type(lp, tp);
706       set_entity_type(ent, tp);
707     }
708   }
709   else {
710     tp = (ir_type *)tore;
711
712     /* fix pointer to methods */
713     if (is_Pointer_type(tp)) {
714       ir_type *etp = get_pointer_points_to_type(tp);
715       if (must_be_lowered(lp, etp)) {
716         etp = create_modified_mtd_type(lp, etp);
717         set_pointer_points_to_type(tp, etp);
718       }
719     }
720   }
721 }
722
723 /*
724  * Lower calls with compound parameters and return types.
725  * This function does the following transformations:
726  *
727  * - Adds a new (hidden) pointer parameter for
728  *   any return compound type.
729  *
730  * - Use of the hidden parameters in the function code.
731  *
732  * - Change all calls to functions with compound return
733  *   by providing space for the hidden parameter on the callers
734  *   stack.
735  *
736  * - Replace a possible block copy after the function call.
737  */
738 void lower_calls_with_compounds(const lower_params_t *params)
739 {
740   int i;
741   ir_graph *irg;
742   lower_params_t param = *params;
743
744   if (param.find_pointer_type == NULL) {
745     param.find_pointer_type = def_find_pointer_type;
746     type_map = pmap_create_ex(8);
747   }
748   else
749     type_map = NULL;
750
751   /* first step: Transform all graphs */
752   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
753     irg = get_irp_irg(i);
754
755     if (irg == get_const_code_irg())
756       continue;
757
758     transform_irg(&param, irg);
759   }
760
761   /* second step: Lower all method types of visible entities */
762   type_walk(NULL, lower_method_types, &param);
763
764   if (type_map)
765     pmap_destroy(type_map);
766 }