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