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_method_calling_convention(lowered,
181 get_method_calling_convention(mtp) | cc_compound_ret);
182 set_lowered_type(mtp, lowered);
190 typedef struct cl_entry cl_entry;
192 cl_entry *next; /**< Pointer to the next entry. */
193 ir_node *call; /**< Pointer to the Call node. */
194 ir_node *copyb; /**< List of all CopyB nodes. */
198 * Walker environment for fix_args_and_collect_calls().
200 typedef struct _wlk_env_t {
201 int arg_shift; /**< The Argument index shift for parameters. */
202 int first_hidden; /**< The index of the first hidden argument. */
203 struct obstack obst; /**< An obstack to allocate the data on. */
204 cl_entry *cl_list; /**< The call list. */
205 pmap *dummy_map; /**< A map for finding the dummy arguments. */
206 unsigned dnr; /**< The dummy index number. */
207 const lower_params_t *params; /**< lowering parameters */
211 * Return the call list entry of a call node.
212 * If no entry exists yet, allocate one and enter the node into
213 * the call list of the environment.
215 * @param call A Call node.
216 * @param env The environment.
218 static cl_entry *get_Call_entry(ir_node *call, wlk_env *env) {
219 cl_entry *res = get_irn_link(call);
221 cl_entry *res = obstack_alloc(&env->obst, sizeof(*res));
222 res->next = env->cl_list;
225 set_irn_link(call, res);
232 * Post walker: shift all parameter indeces
233 * and collect Calls with compound returns in the call list.
235 static void fix_args_and_collect_calls(ir_node *n, void *ctx) {
239 ir_op *op = get_irn_op(n);
241 if (env->arg_shift > 0 && op == op_Proj) {
242 ir_node *pred = get_Proj_pred(n);
244 /* Fix the argument numbers */
245 if (pred == get_irg_args(current_ir_graph)) {
246 long pnr = get_Proj_proj(n);
247 set_Proj_proj(n, pnr + env->arg_shift);
250 else if (op == op_Call) {
251 ctp = get_Call_type(n);
252 if (env->params->flags & LF_COMPOUND_RETURN) {
253 /* check for compound returns */
254 for (i = get_method_n_ress(ctp) -1; i >= 0; --i) {
255 if (is_compound_type(get_method_res_type(ctp, i))) {
257 * This is a call with a compound return. As the result
258 * might be ignored, we must put it in the list.
260 (void)get_Call_entry(n, env);
266 else if (op == op_CopyB && env->params->flags & LF_COMPOUND_RETURN) {
267 /* check for compound returns */
268 ir_node *src = get_CopyB_src(n);
269 /* older scheme using value_res_ent */
271 ir_node *proj = get_Sel_ptr(src);
272 if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_P_value_res_base) {
273 ir_node *call = get_Proj_pred(proj);
275 /* found a CopyB from compound Call result */
276 cl_entry *e = get_Call_entry(call, env);
277 set_irn_link(n, e->copyb);
282 /* new scheme: compound results are determined by the call type only */
284 ir_node *proj = get_Proj_pred(src);
285 if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_T_result) {
286 ir_node *call = get_Proj_pred(proj);
288 ctp = get_Call_type(call);
289 if (is_compound_type(get_method_res_type(ctp, get_Proj_proj(src)))) {
290 /* found a CopyB from compound Call result */
291 cl_entry *e = get_Call_entry(call, env);
292 set_irn_link(n, e->copyb);
302 * Returns non-zero if a node is a compound address
303 * of a frame-type entity.
305 * @param ft the frame type
306 * @param adr the node
308 static int is_compound_address(ir_type *ft, ir_node *adr)
314 if (get_Sel_n_indexs(adr) != 0)
316 ent = get_Sel_entity(adr);
317 return get_entity_owner(ent) == ft;
320 /** A pair for the copy-return-optimization. */
321 typedef struct cr_pair {
322 ir_entity *ent; /**< the entity than can be removed from the frame */
323 ir_node *arg; /**< the argument that replaces the entities address */
327 * Post walker: fixes all entities addresses for the copy-return
330 * Note: We expect the length of the cr_pair array (ie number of compound
331 * return values) to be 1 (C, C++) in almost all cases, so ignore the
332 * linear search complexity here.
334 static void do_copy_return_opt(ir_node *n, void *ctx) {
339 ir_entity *ent = get_Sel_entity(n);
341 for (i = ARR_LEN(arr) - 1; i >= 0; --i) {
342 if (ent == arr[i].ent) {
343 exchange(n, arr[i].arg);
351 * Return a Sel node that selects a dummy argument of type tp.
352 * Dummy arguments are only needed once and we use a map
354 * We could even assign all dummy arguments the same offset
355 * in the frame type ...
357 * @param irg the graph
358 * @param block the block where a newly create Sel should be placed
359 * @param tp the type of the dummy entity that should be create
360 * @param env the environment
362 static ir_node *get_dummy_sel(ir_graph *irg, ir_node *block, ir_type *tp, wlk_env *env)
367 /* use a map the check if we already create such an entity */
368 e = pmap_find(env->dummy_map, tp);
372 ir_type *ft = get_irg_frame_type(irg);
375 snprintf(buf, sizeof(buf), "dummy.%u", env->dnr++);
376 ent = new_entity(ft, new_id_from_str(buf), tp);
377 pmap_insert(env->dummy_map, tp, ent);
379 if (get_type_state(ft) == layout_fixed) {
380 /* Fix the layout again */
381 assert(0 && "Fixed layout not implemented");
384 return new_r_simpleSel(
393 * Add the hidden parameter from the CopyB node to the Call node.
395 * @param irg the graph
396 * @param n_com number of compound results (will be number of hidden parameters)
397 * @param ins in array to store the hidden parameters into
398 * @param entry the call list
399 * @param env the environment
401 static void add_hidden_param(ir_graph *irg, int n_com, ir_node **ins, cl_entry *entry, wlk_env *env)
403 ir_node *p, *n, *src, *mem, *blk;
409 for (p = entry->copyb; p; p = n) {
411 src = get_CopyB_src(p);
413 /* old scheme using value_res_ent */
415 ent = get_Sel_entity(src);
416 owner = get_entity_owner(ent);
418 /* find the hidden parameter index */
419 for (idx = 0; idx < get_struct_n_members(owner); ++idx)
420 if (get_struct_member(owner, idx) == ent)
422 assert(idx < get_struct_n_members(owner));
426 /* new scheme: compound returns are determined by the call type and are Proj's */
427 idx = get_Proj_proj(src);
429 ins[idx] = get_CopyB_dst(p);
430 mem = get_CopyB_mem(p);
431 blk = get_nodes_block(p);
433 /* get rid of the CopyB */
434 turn_into_tuple(p, pn_CopyB_max);
435 set_Tuple_pred(p, pn_CopyB_M_regular, mem);
436 set_Tuple_pred(p, pn_CopyB_M_except, get_irg_bad(irg));
437 set_Tuple_pred(p, pn_CopyB_X_regular, new_r_Jmp(irg, blk));
438 set_Tuple_pred(p, pn_CopyB_X_except, get_irg_bad(irg));
442 /* now create dummy entities for function with ignored return value */
443 if (n_args < n_com) {
444 ir_type *ctp = get_Call_type(entry->call);
447 if (is_lowered_type(ctp))
448 ctp = get_associated_type(ctp);
450 for (j = i = 0; i < get_method_n_ress(ctp); ++i) {
451 ir_type *rtp = get_method_res_type(ctp, i);
452 if (is_compound_type(rtp)) {
454 ins[j] = get_dummy_sel(irg, get_nodes_block(entry->call), rtp, env);
462 * Fix all calls on a call list by adding hidden parameters.
464 * @param irg the graph
465 * @param env the environment
467 static void fix_call_list(ir_graph *irg, wlk_env *env) {
468 const lower_params_t *lp = env->params;
470 ir_node *call, **new_in;
471 ir_type *ctp, *lowered_mtp;
472 add_hidden hidden_params;
473 int i, n_params, n_com, pos;
475 new_in = NEW_ARR_F(ir_node *, 0);
476 for (p = env->cl_list; p; p = p->next) {
478 ctp = get_Call_type(call);
479 lowered_mtp = create_modified_mtd_type(lp, ctp);
480 set_Call_type(call, lowered_mtp);
482 hidden_params = lp->hidden_params;
483 if (hidden_params == ADD_HIDDEN_SMART &&
484 get_method_variadicity(ctp) == variadicity_variadic)
485 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
487 n_params = get_Call_n_params(call);
490 for (i = get_method_n_ress(ctp) - 1; i >= 0; --i) {
491 if (is_compound_type(get_method_res_type(ctp, i)))
495 ARR_RESIZE(ir_node *, new_in, n_params + n_com + pos);
496 memset(new_in, 0, sizeof(*new_in) * (n_params + n_com + pos));
497 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
498 add_hidden_param(irg, n_com, &new_in[pos], p, env);
501 /* copy all other parameters */
502 for (i = 0; i < n_params; ++i)
503 new_in[pos++] = get_Call_param(call, i);
504 if (hidden_params == ADD_HIDDEN_ALWAYS_LAST) {
505 add_hidden_param(irg, n_com, &new_in[pos], p, env);
508 new_in[0] = get_Call_mem(call);
509 new_in[1] = get_Call_ptr(call);
511 set_irn_in(call, n_params + n_com + 2, new_in);
516 * Transform a graph. If it has compound parameter returns,
517 * remove them and use the hidden parameter instead.
518 * If it calls methods with compound parameter returns, add hidden
521 * @param lp parameter struct
522 * @param irg the graph to transform
524 static void transform_irg(const lower_params_t *lp, ir_graph *irg)
526 ir_entity *ent = get_irg_entity(irg);
527 ir_type *mtp, *lowered_mtp, *tp, *ft;
528 int i, j, k, n_ress = 0, n_ret_com = 0, n_cr_opt;
529 ir_node **new_in, *ret, *endbl, *bl, *mem, *copy;
532 add_hidden hidden_params;
534 assert(ent && "Cannot tranform graph without an entity");
535 assert(get_irg_phase_state(irg) == phase_high && "call lowering must be done in phase high");
537 mtp = get_entity_type(ent);
539 if (lp->flags & LF_COMPOUND_RETURN) {
540 /* calculate the number of compound returns */
541 n_ress = get_method_n_ress(mtp);
542 for (n_ret_com = i = 0; i < n_ress; ++i) {
543 tp = get_method_res_type(mtp, i);
545 if (is_compound_type(tp))
551 /* much easier if we have only one return */
552 normalize_one_return(irg);
554 /* This graph has a compound argument. Create a new type */
555 lowered_mtp = create_modified_mtd_type(lp, mtp);
556 set_entity_type(ent, lowered_mtp);
558 hidden_params = lp->hidden_params;
559 if (hidden_params == ADD_HIDDEN_SMART &&
560 get_method_variadicity(mtp) == variadicity_variadic)
561 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
563 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
564 /* hidden arguments are added first */
565 env.arg_shift = n_ret_com;
566 env.first_hidden = 0;
569 /* hidden arguments are added last */
571 env.first_hidden = get_method_n_params(mtp);
575 /* we must only search for calls */
578 obstack_init(&env.obst);
580 env.dummy_map = pmap_create_ex(8);
584 /* scan the code, fix argument numbers and collect calls. */
585 irg_walk_graph(irg, firm_clear_link, fix_args_and_collect_calls, &env);
589 fix_call_list(irg, &env);
593 * Now fix the Return node of the current graph.
596 /* STEP 1: find the return. This is simple, we have normalized the graph. */
597 endbl = get_irg_end_block(irg);
599 for (i = get_Block_n_cfgpreds(endbl) - 1; i >= 0; --i) {
600 ir_node *pred = get_Block_cfgpred(endbl, i);
602 if (is_Return(pred)) {
607 /* there should always be a return */
611 * STEP 2: fix it. For all compound return values add a CopyB,
612 * all others are copied.
614 NEW_ARR_A(ir_node *, new_in, n_ress + 1);
616 bl = get_nodes_block(ret);
617 mem = get_Return_mem(ret);
619 ft = get_irg_frame_type(irg);
620 NEW_ARR_A(cr_pair, cr_opt, n_ret_com);
622 for (j = 1, i = k = 0; i < n_ress; ++i) {
623 ir_node *pred = get_Return_res(ret, i);
624 tp = get_method_res_type(mtp, i);
626 if (is_compound_type(tp)) {
627 ir_node *arg = get_irg_args(irg);
628 arg = new_r_Proj(irg, get_nodes_block(arg), arg, mode_P_data, env.first_hidden + k);
631 if (is_compound_address(ft, pred)) {
632 /* we can do the copy-return optimization here */
633 cr_opt[n_cr_opt].ent = get_Sel_entity(pred);
634 cr_opt[n_cr_opt].arg = arg;
637 else { /* copy-return optimization is impossible, do the copy. */
645 mem = new_r_Proj(irg, bl, copy, mode_M, pn_CopyB_M_regular);
647 if (lp->flags & LF_RETURN_HIDDEN) {
652 else { /* scalar return value */
657 /* replace the in of the Return */
659 set_irn_in(ret, j, new_in);
662 irg_walk_graph(irg, NULL, do_copy_return_opt, cr_opt);
664 for (i = ARR_LEN(cr_opt) - 1; i >= 0; --i) {
665 remove_class_member(ft, cr_opt[i].ent);
668 } /* if (n_ret_com) */
670 pmap_destroy(env.dummy_map);
671 obstack_free(&env.obst, NULL);
675 * Returns non-zero if the given type is a method
676 * type that must be lowered.
678 * @param lp lowering parameters
679 * @param tp The type.
681 static int must_be_lowered(const lower_params_t *lp, ir_type *tp) {
685 if (is_Method_type(tp)) {
686 if (lp->flags & LF_COMPOUND_RETURN) {
687 /* check for compound returns */
688 n_ress = get_method_n_ress(tp);
689 for (i = 0; i < n_ress; ++i) {
690 res_tp = get_method_res_type(tp, i);
692 if (is_compound_type(res_tp))
701 * type-walker: lower all method types of entities
702 * and points-to types.
704 static void lower_method_types(type_or_ent *tore, void *env)
706 const lower_params_t *lp = env;
709 /* fix method entities */
710 if (is_entity(tore)) {
711 ir_entity *ent = (ir_entity *)tore;
712 tp = get_entity_type(ent);
714 if (must_be_lowered(lp, tp)) {
715 tp = create_modified_mtd_type(lp, tp);
716 set_entity_type(ent, tp);
720 tp = (ir_type *)tore;
722 /* fix pointer to methods */
723 if (is_Pointer_type(tp)) {
724 ir_type *etp = get_pointer_points_to_type(tp);
725 if (must_be_lowered(lp, etp)) {
726 etp = create_modified_mtd_type(lp, etp);
727 set_pointer_points_to_type(tp, etp);
734 * Lower calls with compound parameters and return types.
735 * This function does the following transformations:
737 * - Adds a new (hidden) pointer parameter for
738 * any return compound type.
740 * - Use of the hidden parameters in the function code.
742 * - Change all calls to functions with compound return
743 * by providing space for the hidden parameter on the callers
746 * - Replace a possible block copy after the function call.
748 void lower_calls_with_compounds(const lower_params_t *params)
752 lower_params_t param = *params;
754 if (param.find_pointer_type == NULL) {
755 param.find_pointer_type = def_find_pointer_type;
756 type_map = pmap_create_ex(8);
761 /* first step: Transform all graphs */
762 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
763 irg = get_irp_irg(i);
765 if (irg == get_const_code_irg())
768 transform_irg(¶m, irg);
771 /* second step: Lower all method types of visible entities */
772 type_walk(NULL, lower_method_types, ¶m);
775 pmap_destroy(type_map);