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