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