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