9e57bef30941144e428454d243af061b5c05f922
[libfirm] / ir / lower / lower_calls.c
1 /*
2  * Copyright (C) 1995-2011 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 #include "config.h"
27
28 #include <stdbool.h>
29
30 #include "lower_calls.h"
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 "irmemory.h"
40 #include "irtools.h"
41 #include "iroptimize.h"
42 #include "array_t.h"
43 #include "pmap.h"
44 #include "error.h"
45
46 static pmap *pointer_types;
47 static pmap *lowered_mtps;
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 *get_pointer_type(ir_type *dest_type)
54 {
55         ir_type *res = (ir_type*)pmap_get(pointer_types, dest_type);
56         if (res == NULL) {
57                 res = new_type_pointer(dest_type);
58                 pmap_insert(pointer_types, dest_type, res);
59         }
60         return res;
61 }
62
63 static void fix_parameter_entities(ir_graph *irg, size_t n_compound_ret)
64 {
65         ir_type *frame_type = get_irg_frame_type(irg);
66         size_t   n_compound = get_compound_n_members(frame_type);
67         size_t   i;
68
69         if (n_compound_ret == 0)
70                 return;
71
72         for (i = 0; i < n_compound; ++i) {
73                 ir_entity *member = get_compound_member(frame_type, i);
74                 size_t     num;
75                 if (!is_parameter_entity(member))
76                         continue;
77
78                 /* increase parameter number since we added a new parameter in front */
79                 num = get_entity_parameter_number(member);
80                 if (num == IR_VA_START_PARAMETER_NUMBER)
81                         continue;
82                 set_entity_parameter_number(member, num + n_compound_ret);
83         }
84 }
85
86 /**
87  * Creates a new lowered type for a method type with compound
88  * arguments. The new type is associated to the old one and returned.
89  */
90 static ir_type *lower_mtp(compound_call_lowering_flags flags, ir_type *mtp)
91 {
92         bool      must_be_lowered = false;
93         ir_type  *lowered;
94         ir_type **params;
95         ir_type **results;
96         size_t    n_ress;
97         size_t    n_params;
98         size_t    nn_ress;
99         size_t    nn_params;
100         size_t    i;
101         mtp_additional_properties mtp_properties;
102
103         if (!is_Method_type(mtp))
104                 return mtp;
105
106         lowered = (ir_type*)pmap_get(lowered_mtps, mtp);
107         if (lowered != NULL)
108                 return lowered;
109
110         /* check if the type has to be lowered at all */
111         n_ress = get_method_n_ress(mtp);
112         for (i = 0; i < n_ress; ++i) {
113                 ir_type *res_tp = get_method_res_type(mtp, i);
114                 if (is_compound_type(res_tp)) {
115                         must_be_lowered = true;
116                         break;
117                 }
118         }
119         if (!must_be_lowered)
120                 return mtp;
121
122         n_params  = get_method_n_params(mtp);
123         results   = ALLOCANZ(ir_type*, n_ress);
124         params    = ALLOCANZ(ir_type*, n_params + n_ress);
125         nn_ress   = 0;
126         nn_params = 0;
127
128         /* add a hidden parameter in front for every compound result */
129         for (i = 0; i < n_ress; ++i) {
130                 ir_type *res_tp = get_method_res_type(mtp, i);
131
132                 if (is_compound_type(res_tp)) {
133                         /* this compound will be allocated on callers stack and its
134                            address will be transmitted as a hidden parameter. */
135                         ir_type *ptr_tp = get_pointer_type(res_tp);
136                         params[nn_params++] = ptr_tp;
137                         if (flags & LF_RETURN_HIDDEN)
138                                 results[nn_ress++] = ptr_tp;
139                 } else {
140                         /* scalar result */
141                         results[nn_ress++] = res_tp;
142                 }
143         }
144         /* copy over parameter types */
145         for (i = 0; i < n_params; ++i) {
146                 params[nn_params++] = get_method_param_type(mtp, i);
147         }
148         assert(nn_ress <= n_ress);
149         assert(nn_params <= n_params + n_ress);
150
151         /* create the new type */
152         lowered = new_d_type_method(nn_params, nn_ress, get_type_dbg_info(mtp));
153         lowered->attr.ma.has_compound_ret_parameter = true;
154
155         /* fill it */
156         for (i = 0; i < nn_params; ++i)
157                 set_method_param_type(lowered, i, params[i]);
158         for (i = 0; i < nn_ress; ++i)
159                 set_method_res_type(lowered, i, results[i]);
160
161         set_method_variadicity(lowered, get_method_variadicity(mtp));
162
163         set_method_calling_convention(lowered, get_method_calling_convention(mtp) | cc_compound_ret);
164         mtp_properties = get_method_additional_properties(mtp);
165         /* after lowering the call is not const anymore, since it writes to the
166          * memory for the return value passed to it */
167         mtp_properties &= ~mtp_property_const;
168         set_method_additional_properties(lowered, mtp_properties);
169
170         /* associate the lowered type with the original one for easier access */
171         set_lowered_type(mtp, lowered);
172         pmap_insert(lowered_mtps, mtp, lowered);
173
174         return lowered;
175 }
176
177 /**
178  * A call list entry.
179  */
180 typedef struct cl_entry cl_entry;
181 struct cl_entry {
182         cl_entry *next;   /**< Pointer to the next entry. */
183         ir_node  *call;   /**< Pointer to the Call node. */
184         ir_node  *copyb;  /**< List of all CopyB nodes. */
185 };
186
187 /**
188  * Walker environment for fix_args_and_collect_calls().
189  */
190 typedef struct wlk_env_t {
191         size_t               arg_shift;        /**< The Argument index shift for parameters. */
192         struct obstack       obst;             /**< An obstack to allocate the data on. */
193         cl_entry             *cl_list;         /**< The call list. */
194         pmap                 *dummy_map;       /**< A map for finding the dummy arguments. */
195         compound_call_lowering_flags flags;
196         ir_type              *lowered_mtp;     /**< The lowered method type of the current irg if any. */
197         unsigned             only_local_mem:1; /**< Set if only local memory access was found. */
198         unsigned             changed:1;        /**< Set if the current graph was changed. */
199 } wlk_env;
200
201 /**
202  * Return the call list entry of a call node.
203  * If no entry exists yet, allocate one and enter the node into
204  * the call list of the environment.
205  *
206  * @param call   A Call node.
207  * @param env    The environment.
208  */
209 static cl_entry *get_Call_entry(ir_node *call, wlk_env *env)
210 {
211         cl_entry *res = (cl_entry*)get_irn_link(call);
212         if (res == NULL) {
213                 res = OALLOC(&env->obst, cl_entry);
214                 res->next  = env->cl_list;
215                 res->call  = call;
216                 res->copyb = NULL;
217                 set_irn_link(call, res);
218                 env->cl_list = res;
219         }
220         return res;
221 }
222
223 /**
224  * Finds the base address of an address by skipping Sel's and address
225  * calculation.
226  *
227  * @param adr   the address
228  * @param pEnt  points to the base entity if any
229  */
230 static ir_node *find_base_adr(ir_node *ptr, ir_entity **pEnt)
231 {
232         ir_entity *ent = NULL;
233         assert(mode_is_reference(get_irn_mode(ptr)));
234
235         for (;;) {
236                 if (is_Sel(ptr)) {
237                         ent = get_Sel_entity(ptr);
238                         ptr = get_Sel_ptr(ptr);
239                 }
240                 else if (is_Add(ptr)) {
241                         ir_node *left = get_Add_left(ptr);
242                         if (mode_is_reference(get_irn_mode(left)))
243                                 ptr = left;
244                         else
245                                 ptr = get_Add_right(ptr);
246                         ent = NULL;
247                 } else if (is_Sub(ptr)) {
248                         ptr = get_Sub_left(ptr);
249                         ent = NULL;
250                 } else {
251                         *pEnt = ent;
252                         return ptr;
253                 }
254         }
255 }
256
257 /**
258  * Check if a given pointer represents non-local memory.
259  */
260 static void check_ptr(ir_node *ptr, wlk_env *env)
261 {
262         ir_storage_class_class_t sc;
263         ir_entity                *ent;
264
265         /* still alias free */
266         ptr = find_base_adr(ptr, &ent);
267         sc  = get_base_sc(classify_pointer(ptr, ent));
268         if (sc != ir_sc_localvar && sc != ir_sc_malloced) {
269                 /* non-local memory access */
270                 env->only_local_mem = 0;
271         }
272 }
273
274 /*
275  * Returns non-zero if a Call is surely a self-recursive Call.
276  * Beware: if this functions returns 0, the call might be self-recursive!
277  */
278 static bool is_self_recursive_Call(const ir_node *call)
279 {
280         const ir_node *callee = get_Call_ptr(call);
281
282         if (is_SymConst_addr_ent(callee)) {
283                 const ir_entity *ent = get_SymConst_entity(callee);
284                 const ir_graph  *irg = get_entity_irg(ent);
285                 if (irg == get_irn_irg(call))
286                         return 1;
287         }
288         return 0;
289 }
290
291 /**
292  * Post walker: shift all parameter indexes
293  * and collect Calls with compound returns in the call list.
294  * If a non-alias free memory access is found, reset the alias free
295  * flag.
296  */
297 static void fix_args_and_collect_calls(ir_node *n, void *ctx)
298 {
299         wlk_env *env = (wlk_env*)ctx;
300         ir_type *ctp;
301         ir_node *ptr;
302
303         switch (get_irn_opcode(n)) {
304         case iro_Load:
305         case iro_Store:
306                 if (env->only_local_mem) {
307                         ptr = get_irn_n(n, 1);
308                         check_ptr(ptr, env);
309                 }
310                 break;
311         case iro_Proj:
312                 if (env->arg_shift > 0) {
313                         ir_node *pred = get_Proj_pred(n);
314                         ir_graph *irg = get_irn_irg(n);
315
316                         /* Fix the argument numbers */
317                         if (pred == get_irg_args(irg)) {
318                                 long pnr = get_Proj_proj(n);
319                                 set_Proj_proj(n, pnr + env->arg_shift);
320                                 env->changed = 1;
321                         }
322                 }
323                 break;
324         case iro_Call: {
325                 size_t i;
326                 size_t n_res;
327                 if (! is_self_recursive_Call(n)) {
328                         /* any non self recursive call might access global memory */
329                         env->only_local_mem = 0;
330                 }
331
332                 ctp = get_Call_type(n);
333                 /* check for compound returns */
334                 for (i = 0, n_res = get_method_n_ress(ctp); i < n_res; ++i) {
335                         if (is_compound_type(get_method_res_type(ctp, i))) {
336                                 /*
337                                  * This is a call with a compound return. As the result
338                                  * might be ignored, we must put it in the list.
339                                  */
340                                 (void)get_Call_entry(n, env);
341                                 break;
342                         }
343                 }
344                 break;
345         }
346         case iro_CopyB: {
347                 ir_node *src = get_CopyB_src(n);
348                 if (env->only_local_mem) {
349                         check_ptr(get_CopyB_src(n), env);
350                         if (env->only_local_mem)
351                                 check_ptr(get_CopyB_dst(n), env);
352                 }
353                 /* check for compound returns */
354                 if (is_Proj(src)) {
355                         ir_node *proj = get_Proj_pred(src);
356                         if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_T_result) {
357                                 ir_node *call = get_Proj_pred(proj);
358                                 if (is_Call(call)) {
359                                         ctp = get_Call_type(call);
360                                         if (is_compound_type(get_method_res_type(ctp, get_Proj_proj(src)))) {
361                                                 /* found a CopyB from compound Call result */
362                                                 cl_entry *e = get_Call_entry(call, env);
363                                                 set_irn_link(n, e->copyb);
364                                                 e->copyb = n;
365                                         }
366                                 }
367                         }
368                 }
369                 break;
370         }
371         case iro_Sel: {
372                 ir_entity *ent  = get_Sel_entity(n);
373                 ir_type   *type = get_entity_type(ent);
374
375                 /* we need to copy compound parameters */
376                 if (is_parameter_entity(ent) && is_compound_type(type)) {
377                         env->only_local_mem = 0;
378                 }
379                 break;
380         }
381         default:
382                 /* do nothing */
383                 break;
384         }
385 }
386
387 /**
388  * Returns non-zero if a node is a compound address
389  * of a frame-type entity.
390  *
391  * @param ft   the frame type
392  * @param adr  the node
393  */
394 static bool is_compound_address(ir_type *ft, ir_node *adr)
395 {
396         ir_entity *ent;
397
398         if (! is_Sel(adr))
399                 return false;
400         if (get_Sel_n_indexs(adr) != 0)
401                 return false;
402         ent = get_Sel_entity(adr);
403         return get_entity_owner(ent) == ft;
404 }
405
406 /** A pair for the copy-return-optimization. */
407 typedef struct cr_pair {
408         ir_entity *ent; /**< the entity than can be removed from the frame */
409         ir_node *arg;   /**< the argument that replaces the entities address */
410 } cr_pair;
411
412 /**
413  * Post walker: fixes all entities addresses for the copy-return
414  * optimization.
415  *
416  * Note: We expect the length of the cr_pair array (i.e. number of compound
417  * return values) to be 1 (C, C++) in almost all cases, so ignore the
418  * linear search complexity here.
419  */
420 static void do_copy_return_opt(ir_node *n, void *ctx)
421 {
422         if (is_Sel(n)) {
423                 ir_entity *ent = get_Sel_entity(n);
424                 cr_pair   *arr = (cr_pair*)ctx;
425                 size_t    i, l;
426
427                 for (i = 0, l = ARR_LEN(arr); i < l; ++i) {
428                         if (ent == arr[i].ent) {
429                                 exchange(n, arr[i].arg);
430                                 break;
431                         }
432                 }
433         }
434 }
435
436 /**
437  * Return a Sel node that selects a dummy argument of type tp.
438  * Dummy arguments are only needed once and we use a map
439  * to store them.
440  * We could even assign all dummy arguments the same offset
441  * in the frame type ...
442  *
443  * @param irg    the graph
444  * @param block  the block where a newly create Sel should be placed
445  * @param tp     the type of the dummy entity that should be create
446  * @param env    the environment
447  */
448 static ir_node *get_dummy_sel(ir_graph *irg, ir_node *block, ir_type *tp,
449                               wlk_env *env)
450 {
451         ir_entity *ent;
452         pmap_entry *e;
453
454         /* use a map the check if we already create such an entity */
455         e = pmap_find(env->dummy_map, tp);
456         if (e) {
457                 ent = (ir_entity*)e->value;
458         } else {
459                 ir_type *ft = get_irg_frame_type(irg);
460                 ident   *dummy_id = id_unique("dummy.%u");
461                 ent = new_entity(ft, dummy_id, tp);
462                 pmap_insert(env->dummy_map, tp, ent);
463
464                 if (get_type_state(ft) == layout_fixed) {
465                         /* Fix the layout again */
466                         panic("Fixed layout not implemented");
467                 }
468         }
469         return new_r_simpleSel(block, get_irg_no_mem(irg), get_irg_frame(irg), ent);
470 }
471
472 /**
473  * Add the hidden parameter from the CopyB node to the Call node.
474  *
475  * @param irg    the graph
476  * @param n_com  number of compound results (will be number of hidden parameters)
477  * @param ins    in array to store the hidden parameters into
478  * @param entry  the call list
479  * @param env    the environment
480  */
481 static void add_hidden_param(ir_graph *irg, size_t n_com, ir_node **ins,
482                              cl_entry *entry, wlk_env *env)
483 {
484         ir_node *p, *n, *mem, *blk;
485         size_t n_args;
486
487         n_args = 0;
488         for (p = entry->copyb; p; p = n) {
489                 ir_node *src = get_CopyB_src(p);
490                 size_t   idx = get_Proj_proj(src);
491                 n = (ir_node*)get_irn_link(p);
492
493                 ins[idx] = get_CopyB_dst(p);
494                 blk      = get_nodes_block(p);
495
496                 /* use the memory output of the call and not the input of the CopyB
497                  * otherwise stuff breaks if the call was mtp_property_const, because
498                  * then the copyb skips the call. But after lowering the call is not
499                  * const anymore, and its memory has to be used */
500                 mem = new_r_Proj(entry->call, mode_M, pn_Call_M);
501
502                 /* get rid of the CopyB */
503                 turn_into_tuple(p, pn_CopyB_max+1);
504                 set_Tuple_pred(p, pn_CopyB_M,         mem);
505                 set_Tuple_pred(p, pn_CopyB_X_regular, new_r_Jmp(blk));
506                 set_Tuple_pred(p, pn_CopyB_X_except,  new_r_Bad(irg, mode_X));
507                 ++n_args;
508         }
509
510         /* now create dummy entities for function with ignored return value */
511         if (n_args < n_com) {
512                 ir_type *ctp = get_Call_type(entry->call);
513                 size_t   i;
514                 size_t   j;
515
516                 if (is_lowered_type(ctp))
517                         ctp = get_associated_type(ctp);
518
519                 for (j = i = 0; i < get_method_n_ress(ctp); ++i) {
520                         ir_type *rtp = get_method_res_type(ctp, i);
521                         if (is_compound_type(rtp)) {
522                                 if (ins[j] == NULL)
523                                         ins[j] = get_dummy_sel(irg, get_nodes_block(entry->call), rtp, env);
524                                 ++j;
525                         }
526                 }
527         }
528 }
529
530 /**
531  * Fix all calls on a call list by adding hidden parameters.
532  *
533  * @param irg  the graph
534  * @param env  the environment
535  */
536 static void fix_call_list(ir_graph *irg, wlk_env *env)
537 {
538         cl_entry *p;
539         ir_node *call, **new_in;
540         ir_type *ctp, *lowered_mtp;
541         size_t i, n_res, n_params, n_com, pos;
542
543         new_in = NEW_ARR_F(ir_node *, 0);
544         for (p = env->cl_list; p; p = p->next) {
545                 call = p->call;
546                 ctp = get_Call_type(call);
547                 lowered_mtp = lower_mtp(env->flags, ctp);
548                 set_Call_type(call, lowered_mtp);
549
550                 n_params = get_Call_n_params(call);
551
552                 n_com = 0;
553                 for (i = 0, n_res = get_method_n_ress(ctp); i < n_res; ++i) {
554                         if (is_compound_type(get_method_res_type(ctp, i)))
555                                 ++n_com;
556                 }
557                 pos = 2;
558                 ARR_RESIZE(ir_node *, new_in, n_params + n_com + pos);
559                 memset(new_in, 0, sizeof(*new_in) * (n_params + n_com + pos));
560                 add_hidden_param(irg, n_com, &new_in[pos], p, env);
561                 pos += n_com;
562                 /* copy all other parameters */
563                 for (i = 0; i < n_params; ++i)
564                         new_in[pos++] = get_Call_param(call, i);
565                 new_in[0] = get_Call_mem(call);
566                 new_in[1] = get_Call_ptr(call);
567
568                 set_irn_in(call, n_params + n_com + 2, new_in);
569         }
570 }
571
572 /**
573  * Transform a graph. If it has compound parameter returns,
574  * remove them and use the hidden parameter instead.
575  * If it calls methods with compound parameter returns, add hidden
576  * parameters.
577  *
578  * @param irg  the graph to transform
579  */
580 static void transform_irg(compound_call_lowering_flags flags, ir_graph *irg)
581 {
582         ir_entity  *ent = get_irg_entity(irg);
583         ir_type    *mtp, *lowered_mtp, *tp, *ft;
584         size_t     i, j, k, n_ress = 0, n_ret_com = 0;
585         size_t     n_cr_opt;
586         ir_node    **new_in, *ret, *endbl, *bl, *mem, *copy;
587         cr_pair    *cr_opt;
588         wlk_env    env;
589
590         mtp = get_entity_type(ent);
591
592         /* calculate the number of compound returns */
593         n_ress = get_method_n_ress(mtp);
594         for (n_ret_com = i = 0; i < n_ress; ++i) {
595                 tp = get_method_res_type(mtp, i);
596
597                 if (is_compound_type(tp))
598                         ++n_ret_com;
599         }
600
601         fix_parameter_entities(irg, n_ret_com);
602
603         if (n_ret_com) {
604                 /* much easier if we have only one return */
605                 normalize_one_return(irg);
606
607                 /* This graph has a compound argument. Create a new type */
608                 lowered_mtp = lower_mtp(flags, mtp);
609                 set_entity_type(ent, lowered_mtp);
610
611                 /* hidden arguments are added first */
612                 env.arg_shift    = n_ret_com;
613         } else {
614                 /* we must only search for calls */
615                 env.arg_shift = 0;
616                 lowered_mtp   = NULL;
617         }
618         obstack_init(&env.obst);
619         env.cl_list        = NULL;
620         env.dummy_map      = pmap_create_ex(8);
621         env.flags          = flags;
622         env.lowered_mtp    = lowered_mtp;
623         env.only_local_mem = 1;
624         env.changed        = 0;
625
626         /* scan the code, fix argument numbers and collect calls. */
627         irg_walk_graph(irg, firm_clear_link, fix_args_and_collect_calls, &env);
628
629         /* fix all calls */
630         if (env.cl_list) {
631                 fix_call_list(irg, &env);
632                 env.changed = 1;
633         }
634
635         if (n_ret_com) {
636                 int idx;
637
638                 /* STEP 1: find the return. This is simple, we have normalized the graph. */
639                 endbl = get_irg_end_block(irg);
640                 ret = NULL;
641                 for (idx = get_Block_n_cfgpreds(endbl) - 1; idx >= 0; --idx) {
642                         ir_node *pred = get_Block_cfgpred(endbl, idx);
643
644                         if (is_Return(pred)) {
645                                 ret = pred;
646                                 break;
647                         }
648                 }
649
650                 /* in case of infinite loops, there might be no return */
651                 if (ret != NULL) {
652                         /*
653                          * Now fix the Return node of the current graph.
654                          */
655                         env.changed = 1;
656
657                         /*
658                          * STEP 2: fix it. For all compound return values add a CopyB,
659                          * all others are copied.
660                          */
661                         NEW_ARR_A(ir_node *, new_in, n_ress + 1);
662
663                         bl  = get_nodes_block(ret);
664                         mem = get_Return_mem(ret);
665
666                         ft = get_irg_frame_type(irg);
667                         NEW_ARR_A(cr_pair, cr_opt, n_ret_com);
668                         n_cr_opt = 0;
669                         for (j = 1, i = k = 0; i < n_ress; ++i) {
670                                 ir_node *pred = get_Return_res(ret, i);
671                                 tp = get_method_res_type(mtp, i);
672
673                                 if (is_compound_type(tp)) {
674                                         ir_node *arg = get_irg_args(irg);
675                                         arg = new_r_Proj(arg, mode_P_data, k);
676                                         ++k;
677
678                                         if (is_Unknown(pred)) {
679                                                 /* The Return(Unknown) is the Firm construct for a
680                                                  * missing return. Do nothing. */
681                                         } else {
682                                                 /**
683                                                  * Sorrily detecting that copy-return is possible isn't
684                                                  * that simple. We must check, that the hidden address
685                                                  * is alias free during the whole function.
686                                                  * A simple heuristic: all Loads/Stores inside
687                                                  * the function access only local frame.
688                                                  */
689                                                 if (env.only_local_mem && is_compound_address(ft, pred)) {
690                                                         /* we can do the copy-return optimization here */
691                                                         cr_opt[n_cr_opt].ent = get_Sel_entity(pred);
692                                                         cr_opt[n_cr_opt].arg = arg;
693                                                         ++n_cr_opt;
694                                                 } else { /* copy-return optimization is impossible, do the copy. */
695                                                         copy = new_r_CopyB(
696                                                                         bl,
697                                                                         mem,
698                                                                         arg,
699                                                                         pred,
700                                                                         tp
701                                                                         );
702                                                         mem = new_r_Proj(copy, mode_M, pn_CopyB_M);
703                                                 }
704                                         }
705                                         if (flags & LF_RETURN_HIDDEN) {
706                                                 new_in[j] = arg;
707                                                 ++j;
708                                         }
709                                 } else { /* scalar return value */
710                                         new_in[j] = pred;
711                                         ++j;
712                                 }
713                         }
714                         /* replace the in of the Return */
715                         new_in[0] = mem;
716                         set_irn_in(ret, j, new_in);
717
718                         if (n_cr_opt > 0) {
719                                 size_t c;
720                                 size_t n;
721
722                                 irg_walk_graph(irg, NULL, do_copy_return_opt, cr_opt);
723
724                                 for (c = 0, n = ARR_LEN(cr_opt); c < n; ++c) {
725                                         free_entity(cr_opt[c].ent);
726                                 }
727                         }
728                 }
729         }
730
731         pmap_destroy(env.dummy_map);
732         obstack_free(&env.obst, NULL);
733 }
734
735 static void lower_method_types(type_or_ent tore, void *env)
736 {
737         const compound_call_lowering_flags *flags
738                 = (const compound_call_lowering_flags*)env;
739
740         /* fix method entities */
741         if (is_entity(tore.ent)) {
742                 ir_entity *ent     = tore.ent;
743                 ir_type   *tp      = get_entity_type(ent);
744                 ir_type   *lowered = lower_mtp(*flags, tp);
745                 set_entity_type(ent, lowered);
746         } else {
747                 ir_type *tp = tore.typ;
748
749                 /* fix pointer to methods */
750                 if (is_Pointer_type(tp)) {
751                         ir_type *points_to         = get_pointer_points_to_type(tp);
752                         ir_type *lowered_points_to = lower_mtp(*flags, points_to);
753                         set_pointer_points_to_type(tp, lowered_points_to);
754                 }
755         }
756 }
757
758 void lower_calls_with_compounds(compound_call_lowering_flags flags)
759 {
760         size_t i, n;
761
762         pointer_types = pmap_create();
763         lowered_mtps = pmap_create();
764
765         /* first step: Transform all graphs */
766         for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
767                 ir_graph *irg = get_irp_irg(i);
768                 transform_irg(flags, irg);
769         }
770
771         /* second step: Lower all method types of visible entities */
772         type_walk(NULL, lower_method_types, &flags);
773
774         pmap_destroy(lowered_mtps);
775         pmap_destroy(pointer_types);
776 }