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