2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Lowering of Calls with compound parameters and return types.
23 * @author Michael Beck
40 #include "iroptimize.h"
45 /** A type map for def_find_pointer_type. */
46 static pmap *type_map;
49 * Default implementation for finding a pointer type for a given element type.
50 * Simple create a new one.
52 static ir_type *def_find_pointer_type(ir_type *e_type, ir_mode *mode, int alignment)
57 /* Mode and alignment are always identical in all calls to def_find_pointer_type(), so
58 we simply can use a map from the element type to the pointer type. */
59 e = pmap_find(type_map, e_type);
63 res = new_type_pointer(mangle_u(get_type_ident(e_type), new_id_from_chars("Ptr", 3)), e_type, mode);
64 set_type_alignment_bytes(res, alignment);
65 pmap_insert(type_map, e_type, res);
71 * Creates a new lowered type for a method type with compound
72 * arguments. The new type is associated to the old one and returned.
74 * @param lp parameter struct
75 * @param mtp the method type to lower
77 * The current implementation expects that a lowered type already
78 * includes the necessary changes ...
80 static ir_type *create_modified_mtd_type(const lower_params_t *lp, ir_type *mtp)
82 ir_type *lowered, *ptr_tp;
83 ir_type **params, **results, *res_tp;
84 ir_mode *modes[MAX_REGISTER_RET_VAL];
85 int n_ress, n_params, nn_ress, nn_params, i, first_variadic;
87 add_hidden hidden_params;
90 if (is_lowered_type(mtp)) {
91 /* the type is already lowered. Not handled yet. */
92 assert(0 && "lowered types NYI");
95 lowered = get_associated_type(mtp);
99 n_ress = get_method_n_ress(mtp);
100 NEW_ARR_A(ir_type *, results, n_ress);
102 n_params = get_method_n_params(mtp);
103 NEW_ARR_A(ir_type *, params, n_params + n_ress);
105 first_variadic = get_method_first_variadic_param_index(mtp);
107 hidden_params = lp->hidden_params;
108 if (hidden_params == ADD_HIDDEN_SMART &&
109 get_method_variadicity(mtp) == variadicity_variadic)
110 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
112 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
113 /* add hidden in front */
114 for (nn_ress = nn_params = i = 0; i < n_ress; ++i) {
115 res_tp = get_method_res_type(mtp, i);
117 if (is_compound_type(res_tp)) {
120 if (lp->flags & LF_SMALL_CMP_IN_REGS)
121 n_regs = lp->ret_compound_in_regs(res_tp, modes);
124 /* this compound will be returned solely in registers */
128 /* this compound will be allocated on callers stack and its
129 address will be transmitted as a hidden parameter. */
130 ptr_tp = lp->find_pointer_type(res_tp, get_modeP_data(), lp->def_ptr_alignment);
131 params[nn_params++] = ptr_tp;
132 if (lp->flags & LF_RETURN_HIDDEN)
133 results[nn_ress++] = ptr_tp;
137 results[nn_ress++] = res_tp;
140 /* move the index of the first variadic parameter */
141 first_variadic += nn_params;
143 for (i = 0; i < n_params; ++i)
144 params[nn_params++] = get_method_param_type(mtp, i);
147 /* add hidden parameters last */
148 assert(get_method_variadicity(mtp) == variadicity_non_variadic &&
149 "Cannot add hidden parameters at end of variadic function");
151 for (nn_params = 0; nn_params < n_params; ++nn_params)
152 params[nn_params] = get_method_param_type(mtp, nn_params);
154 for (nn_ress = i = 0; i < n_ress; ++i) {
155 res_tp = get_method_res_type(mtp, i);
157 if (is_compound_type(res_tp))
158 params[nn_params++] = lp->find_pointer_type(res_tp, get_modeP_data(), lp->def_ptr_alignment);
160 results[nn_ress++] = res_tp;
164 /* create the new type */
165 id = mangle_u(new_id_from_chars("L", 1), get_type_ident(mtp));
166 lowered = new_d_type_method(id, nn_params, nn_ress, get_type_dbg_info(mtp));
169 for (i = 0; i < nn_params; ++i)
170 set_method_param_type(lowered, i, params[i]);
171 for (i = 0; i < nn_ress; ++i)
172 set_method_res_type(lowered, i, results[i]);
174 var = get_method_variadicity(mtp);
175 set_method_variadicity(lowered, var);
176 if (var == variadicity_variadic)
177 set_method_first_variadic_param_index(lowered, first_variadic);
179 /* associate the lowered type with the original one for easier access */
180 set_lowered_type(mtp, lowered);
188 typedef struct cl_entry 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. */
196 * Walker environment for fix_args_and_collect_calls().
198 typedef struct _wlk_env_t {
199 int arg_shift; /**< The Argument index shift for parameters. */
200 int first_hidden; /**< The index of the first hidden argument. */
201 struct obstack obst; /**< An obstack to allocate the data on. */
202 cl_entry *cl_list; /**< The call list. */
203 pmap *dummy_map; /**< A map for finding the dummy arguments. */
204 unsigned dnr; /**< The dummy index number. */
205 const lower_params_t *params; /**< lowering parameters */
209 * Return the call list entry of a call node.
210 * If no entry exists yet, allocate one and enter the node into
211 * the call list of the environment.
213 * @param call A Call node.
214 * @param env The environment.
216 static cl_entry *get_Call_entry(ir_node *call, wlk_env *env) {
217 cl_entry *res = get_irn_link(call);
219 cl_entry *res = obstack_alloc(&env->obst, sizeof(*res));
220 res->next = env->cl_list;
223 set_irn_link(call, res);
230 * Post walker: shift all parameter indeces
231 * and collect Calls with compound returns in the call list.
233 static void fix_args_and_collect_calls(ir_node *n, void *ctx) {
237 ir_op *op = get_irn_op(n);
239 if (env->arg_shift > 0 && op == op_Proj) {
240 ir_node *pred = get_Proj_pred(n);
242 /* Fix the argument numbers */
243 if (pred == get_irg_args(current_ir_graph)) {
244 long pnr = get_Proj_proj(n);
245 set_Proj_proj(n, pnr + env->arg_shift);
248 else if (op == op_Call) {
249 ctp = get_Call_type(n);
250 if (env->params->flags & LF_COMPOUND_RETURN) {
251 /* check for compound returns */
252 for (i = get_method_n_ress(ctp) -1; i >= 0; --i) {
253 if (is_compound_type(get_method_res_type(ctp, i))) {
255 * This is a call with a compound return. As the result
256 * might be ignored, we must put it in the list.
258 (void)get_Call_entry(n, env);
264 else if (op == op_CopyB && env->params->flags & LF_COMPOUND_RETURN) {
265 /* check for compound returns */
266 ir_node *src = get_CopyB_src(n);
267 /* older scheme using value_res_ent */
269 ir_node *proj = get_Sel_ptr(src);
270 if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_P_value_res_base) {
271 ir_node *call = get_Proj_pred(proj);
273 /* found a CopyB from compound Call result */
274 cl_entry *e = get_Call_entry(call, env);
275 set_irn_link(n, e->copyb);
280 /* new scheme: compound results are determined by the call type only */
282 ir_node *proj = get_Proj_pred(src);
283 if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_T_result) {
284 ir_node *call = get_Proj_pred(proj);
286 ctp = get_Call_type(call);
287 if (is_compound_type(get_method_res_type(ctp, get_Proj_proj(src)))) {
288 /* found a CopyB from compound Call result */
289 cl_entry *e = get_Call_entry(call, env);
290 set_irn_link(n, e->copyb);
300 * Returns non-zero if a node is a compound address
301 * of a frame-type entity.
303 * @param ft the frame type
304 * @param adr the node
306 static int is_compound_address(ir_type *ft, ir_node *adr)
312 if (get_Sel_n_indexs(adr) != 0)
314 ent = get_Sel_entity(adr);
315 return get_entity_owner(ent) == ft;
318 /** A pair for the copy-return-optimization. */
319 typedef struct cr_pair {
320 ir_entity *ent; /**< the entity than can be removed from the frame */
321 ir_node *arg; /**< the argument that replaces the entities address */
325 * Post walker: fixes all entities addresses for the copy-return
328 * Note: We expect the length of the cr_pair array (ie number of compound
329 * return values) to be 1 (C, C++) in almost all cases, so ignore the
330 * linear search complexity here.
332 static void do_copy_return_opt(ir_node *n, void *ctx) {
337 ir_entity *ent = get_Sel_entity(n);
339 for (i = ARR_LEN(arr) - 1; i >= 0; --i) {
340 if (ent == arr[i].ent) {
341 exchange(n, arr[i].arg);
349 * Return a Sel node that selects a dummy argument of type tp.
350 * Dummy arguments are only needed once and we use a map
352 * We could even assign all dummy arguments the same offset
353 * in the frame type ...
355 * @param irg the graph
356 * @param block the block where a newly create Sel should be placed
357 * @param tp the type of the dummy entity that should be create
358 * @param env the environment
360 static ir_node *get_dummy_sel(ir_graph *irg, ir_node *block, ir_type *tp, wlk_env *env)
365 /* use a map the check if we already create such an entity */
366 e = pmap_find(env->dummy_map, tp);
370 ir_type *ft = get_irg_frame_type(irg);
373 snprintf(buf, sizeof(buf), "dummy.%u", env->dnr++);
374 ent = new_entity(ft, new_id_from_str(buf), tp);
375 pmap_insert(env->dummy_map, tp, ent);
377 if (get_type_state(ft) == layout_fixed) {
378 /* Fix the layout again */
379 assert(0 && "Fixed layout not implemented");
382 return new_r_simpleSel(
391 * Add the hidden parameter from the CopyB node to the Call node.
393 * @param irg the graph
394 * @param n_com number of compound results (will be number of hidden parameters)
395 * @param ins in array to store the hidden parameters into
396 * @param entry the call list
397 * @param env the environment
399 static void add_hidden_param(ir_graph *irg, int n_com, ir_node **ins, cl_entry *entry, wlk_env *env)
401 ir_node *p, *n, *src, *mem, *blk;
407 for (p = entry->copyb; p; p = n) {
409 src = get_CopyB_src(p);
411 /* old scheme using value_res_ent */
413 ent = get_Sel_entity(src);
414 owner = get_entity_owner(ent);
416 /* find the hidden parameter index */
417 for (idx = 0; idx < get_struct_n_members(owner); ++idx)
418 if (get_struct_member(owner, idx) == ent)
420 assert(idx < get_struct_n_members(owner));
424 /* new scheme: compound returns are determined by the call type and are Proj's */
425 idx = get_Proj_proj(src);
427 ins[idx] = get_CopyB_dst(p);
428 mem = get_CopyB_mem(p);
429 blk = get_nodes_block(p);
431 /* get rid of the CopyB */
432 turn_into_tuple(p, pn_CopyB_max);
433 set_Tuple_pred(p, pn_CopyB_M_regular, mem);
434 set_Tuple_pred(p, pn_CopyB_M_except, get_irg_bad(irg));
435 set_Tuple_pred(p, pn_CopyB_X_regular, new_r_Jmp(irg, blk));
436 set_Tuple_pred(p, pn_CopyB_X_except, get_irg_bad(irg));
440 /* now create dummy entities for function with ignored return value */
441 if (n_args < n_com) {
442 ir_type *ctp = get_Call_type(entry->call);
445 if (is_lowered_type(ctp))
446 ctp = get_associated_type(ctp);
448 for (j = i = 0; i < get_method_n_ress(ctp); ++i) {
449 ir_type *rtp = get_method_res_type(ctp, i);
450 if (is_compound_type(rtp)) {
452 ins[j] = get_dummy_sel(irg, get_nodes_block(entry->call), rtp, env);
460 * Fix all calls on a call list by adding hidden parameters.
462 * @param irg the graph
463 * @param env the environment
465 static void fix_call_list(ir_graph *irg, wlk_env *env) {
466 const lower_params_t *lp = env->params;
468 ir_node *call, **new_in;
469 ir_type *ctp, *lowered_mtp;
470 add_hidden hidden_params;
471 int i, n_params, n_com, pos;
473 new_in = NEW_ARR_F(ir_node *, 0);
474 for (p = env->cl_list; p; p = p->next) {
476 ctp = get_Call_type(call);
477 lowered_mtp = create_modified_mtd_type(lp, ctp);
478 set_Call_type(call, lowered_mtp);
480 hidden_params = lp->hidden_params;
481 if (hidden_params == ADD_HIDDEN_SMART &&
482 get_method_variadicity(ctp) == variadicity_variadic)
483 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
485 n_params = get_Call_n_params(call);
488 for (i = get_method_n_ress(ctp) - 1; i >= 0; --i) {
489 if (is_compound_type(get_method_res_type(ctp, i)))
493 ARR_RESIZE(ir_node *, new_in, n_params + n_com + pos);
494 memset(new_in, 0, sizeof(*new_in) * (n_params + n_com + pos));
495 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
496 add_hidden_param(irg, n_com, &new_in[pos], p, env);
499 /* copy all other parameters */
500 for (i = 0; i < n_params; ++i)
501 new_in[pos++] = get_Call_param(call, i);
502 if (hidden_params == ADD_HIDDEN_ALWAYS_LAST) {
503 add_hidden_param(irg, n_com, &new_in[pos], p, env);
506 new_in[0] = get_Call_mem(call);
507 new_in[1] = get_Call_ptr(call);
509 set_irn_in(call, n_params + n_com + 2, new_in);
514 * Transform a graph. If it has compound parameter returns,
515 * remove them and use the hidden parameter instead.
516 * If it calls methods with compound parameter returns, add hidden
519 * @param lp parameter struct
520 * @param irg the graph to transform
522 static void transform_irg(const lower_params_t *lp, ir_graph *irg)
524 ir_entity *ent = get_irg_entity(irg);
525 ir_type *mtp, *lowered_mtp, *tp, *ft;
526 int i, j, k, n_ress = 0, n_ret_com = 0, n_cr_opt;
527 ir_node **new_in, *ret, *endbl, *bl, *mem, *copy;
530 add_hidden hidden_params;
532 assert(ent && "Cannot tranform graph without an entity");
533 assert(get_irg_phase_state(irg) == phase_high && "call lowering must be done in phase high");
535 mtp = get_entity_type(ent);
537 if (lp->flags & LF_COMPOUND_RETURN) {
538 /* calculate the number of compound returns */
539 n_ress = get_method_n_ress(mtp);
540 for (n_ret_com = i = 0; i < n_ress; ++i) {
541 tp = get_method_res_type(mtp, i);
543 if (is_compound_type(tp))
549 /* much easier if we have only one return */
550 normalize_one_return(irg);
552 /* This graph has a compound argument. Create a new type */
553 lowered_mtp = create_modified_mtd_type(lp, mtp);
554 set_entity_type(ent, lowered_mtp);
556 hidden_params = lp->hidden_params;
557 if (hidden_params == ADD_HIDDEN_SMART &&
558 get_method_variadicity(mtp) == variadicity_variadic)
559 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
561 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
562 /* hidden arguments are added first */
563 env.arg_shift = n_ret_com;
564 env.first_hidden = 0;
567 /* hidden arguments are added last */
569 env.first_hidden = get_method_n_params(mtp);
573 /* we must only search for calls */
576 obstack_init(&env.obst);
578 env.dummy_map = pmap_create_ex(8);
582 /* scan the code, fix argument numbers and collect calls. */
583 irg_walk_graph(irg, firm_clear_link, fix_args_and_collect_calls, &env);
587 fix_call_list(irg, &env);
591 * Now fix the Return node of the current graph.
594 /* STEP 1: find the return. This is simple, we have normalized the graph. */
595 endbl = get_irg_end_block(irg);
597 for (i = get_Block_n_cfgpreds(endbl) - 1; i >= 0; --i) {
598 ir_node *pred = get_Block_cfgpred(endbl, i);
600 if (is_Return(pred)) {
605 /* there should always be a return */
609 * STEP 2: fix it. For all compound return values add a CopyB,
610 * all others are copied.
612 NEW_ARR_A(ir_node *, new_in, n_ress + 1);
614 bl = get_nodes_block(ret);
615 mem = get_Return_mem(ret);
617 ft = get_irg_frame_type(irg);
618 NEW_ARR_A(cr_pair, cr_opt, n_ret_com);
620 for (j = 1, i = k = 0; i < n_ress; ++i) {
621 ir_node *pred = get_Return_res(ret, i);
622 tp = get_method_res_type(mtp, i);
624 if (is_compound_type(tp)) {
625 ir_node *arg = get_irg_args(irg);
626 arg = new_r_Proj(irg, get_nodes_block(arg), arg, mode_P_data, env.first_hidden + k);
629 if (is_compound_address(ft, pred)) {
630 /* we can do the copy-return optimization here */
631 cr_opt[n_cr_opt].ent = get_Sel_entity(pred);
632 cr_opt[n_cr_opt].arg = arg;
635 else { /* copy-return optimization is impossible, do the copy. */
643 mem = new_r_Proj(irg, bl, copy, mode_M, pn_CopyB_M_regular);
645 if (lp->flags & LF_RETURN_HIDDEN) {
650 else { /* scalar return value */
655 /* replace the in of the Return */
657 set_irn_in(ret, j, new_in);
660 irg_walk_graph(irg, NULL, do_copy_return_opt, cr_opt);
662 for (i = ARR_LEN(cr_opt) - 1; i >= 0; --i) {
663 remove_class_member(ft, cr_opt[i].ent);
666 } /* if (n_ret_com) */
668 pmap_destroy(env.dummy_map);
669 obstack_free(&env.obst, NULL);
673 * Returns non-zero if the given type is a method
674 * type that must be lowered.
676 * @param lp lowering parameters
677 * @param tp The type.
679 static int must_be_lowered(const lower_params_t *lp, ir_type *tp) {
683 if (is_Method_type(tp)) {
684 if (lp->flags & LF_COMPOUND_RETURN) {
685 /* check for compound returns */
686 n_ress = get_method_n_ress(tp);
687 for (i = 0; i < n_ress; ++i) {
688 res_tp = get_method_res_type(tp, i);
690 if (is_compound_type(res_tp))
699 * type-walker: lower all method types of entities
700 * and points-to types.
702 static void lower_method_types(type_or_ent *tore, void *env)
704 const lower_params_t *lp = env;
707 /* fix method entities */
708 if (is_entity(tore)) {
709 ir_entity *ent = (ir_entity *)tore;
710 tp = get_entity_type(ent);
712 if (must_be_lowered(lp, tp)) {
713 tp = create_modified_mtd_type(lp, tp);
714 set_entity_type(ent, tp);
718 tp = (ir_type *)tore;
720 /* fix pointer to methods */
721 if (is_Pointer_type(tp)) {
722 ir_type *etp = get_pointer_points_to_type(tp);
723 if (must_be_lowered(lp, etp)) {
724 etp = create_modified_mtd_type(lp, etp);
725 set_pointer_points_to_type(tp, etp);
732 * Lower calls with compound parameters and return types.
733 * This function does the following transformations:
735 * - Adds a new (hidden) pointer parameter for
736 * any return compound type.
738 * - Use of the hidden parameters in the function code.
740 * - Change all calls to functions with compound return
741 * by providing space for the hidden parameter on the callers
744 * - Replace a possible block copy after the function call.
746 void lower_calls_with_compounds(const lower_params_t *params)
750 lower_params_t param = *params;
752 if (param.find_pointer_type == NULL) {
753 param.find_pointer_type = def_find_pointer_type;
754 type_map = pmap_create_ex(8);
759 /* first step: Transform all graphs */
760 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
761 irg = get_irp_irg(i);
763 if (irg == get_const_code_irg())
766 transform_irg(¶m, irg);
769 /* second step: Lower all method types of visible entities */
770 type_walk(NULL, lower_method_types, ¶m);
773 pmap_destroy(type_map);