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