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