3 * File name: ir/lower/lower_calls.c
4 * Purpose: lowering of Calls with compound parameters
8 * Copyright: (c) 1998-2005 Universität Karlsruhe
9 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
31 #include "lower_calls.h"
37 /** A type map for def_find_pointer_type. */
38 static pmap *type_map;
41 * Default implementation for finding a pointer type for a given element type.
42 * Simple create a new one.
44 static ir_type *def_find_pointer_type(ir_type *e_type, ir_mode *mode, int alignment)
49 /* Mode and alignment are always identical in all calls to def_find_pointer_type(), so
50 we simply can use a map from the element type to the pointer type. */
51 e = pmap_find(type_map, e_type);
55 res = new_type_pointer(mangle_u(get_type_ident(e_type), new_id_from_chars("Ptr", 3)), e_type, mode);
56 set_type_alignment_bytes(res, alignment);
57 pmap_insert(type_map, e_type, res);
63 * Creates a new lowered type for a method type with compound
64 * arguments. The new type is associated to the old one and returned.
66 * @param lp parameter struct
67 * @param mtp the method type to lower
69 * The current implementation expects that a lowered type already
70 * includes the necessary changes ...
72 static ir_type *create_modified_mtd_type(const lower_params_t *lp, ir_type *mtp)
74 ir_type *lowered, *ptr_tp;
75 ir_type **params, **results, *res_tp;
76 ir_mode *modes[MAX_REGISTER_RET_VAL];
77 int n_ress, n_params, nn_ress, nn_params, i, first_variadic;
79 add_hidden hidden_params;
82 if (is_lowered_type(mtp)) {
83 /* the type is already lowered. Not handled yet. */
84 assert(0 && "lowered types NYI");
87 lowered = get_associated_type(mtp);
91 n_ress = get_method_n_ress(mtp);
92 NEW_ARR_A(ir_type *, results, n_ress);
94 n_params = get_method_n_params(mtp);
95 NEW_ARR_A(ir_type *, params, n_params + n_ress);
97 first_variadic = get_method_first_variadic_param_index(mtp);
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;
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);
109 if (is_compound_type(res_tp)) {
112 if (lp->flags & LF_SMALL_CMP_IN_REGS)
113 n_regs = lp->ret_compound_in_regs(res_tp, modes);
116 /* this compound will be returned solely in registers */
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 if (lp->flags & LF_RETURN_HIDDEN)
125 results[nn_ress++] = ptr_tp;
129 results[nn_ress++] = res_tp;
132 /* move the index of the first variadic parameter */
133 first_variadic += nn_params;
135 for (i = 0; i < n_params; ++i)
136 params[nn_params++] = get_method_param_type(mtp, i);
139 /* add hidden parameters last */
140 assert(get_method_variadicity(mtp) == variadicity_non_variadic &&
141 "Cannot add hidden parameters at end of variadic function");
143 for (nn_params = 0; nn_params < n_params; ++nn_params)
144 params[nn_params] = get_method_param_type(mtp, nn_params);
146 for (nn_ress = i = 0; i < n_ress; ++i) {
147 res_tp = get_method_res_type(mtp, i);
149 if (is_compound_type(res_tp))
150 params[nn_params++] = lp->find_pointer_type(res_tp, get_modeP_data(), lp->def_ptr_alignment);
152 results[nn_ress++] = res_tp;
156 /* create the new type */
157 id = mangle_u(new_id_from_chars("L", 1), get_type_ident(mtp));
158 lowered = new_d_type_method(id, nn_params, nn_ress, get_type_dbg_info(mtp));
161 for (i = 0; i < nn_params; ++i)
162 set_method_param_type(lowered, i, params[i]);
163 for (i = 0; i < nn_ress; ++i)
164 set_method_res_type(lowered, i, results[i]);
166 var = get_method_variadicity(mtp);
167 set_method_variadicity(lowered, var);
168 if (var == variadicity_variadic)
169 set_method_first_variadic_param_index(lowered, first_variadic);
171 /* associate the lowered type with the original one for easier access */
172 set_lowered_type(mtp, lowered);
180 typedef struct cl_entry cl_entry;
182 cl_entry *next; /**< Pointer to the next entry. */
183 ir_node *call; /**< Pointer to the Call node. */
184 ir_node *copyb; /**< List of all CopyB nodes. */
188 * Walker environment for fix_args_and_collect_calls().
190 typedef struct _wlk_env_t {
191 int arg_shift; /**< The Argument index shift for parameters. */
192 int first_hidden; /**< The index of the first hidden argument. */
193 struct obstack obst; /**< An obstack to allocate the data on. */
194 cl_entry *cl_list; /**< The call list. */
195 pmap *dummy_map; /**< A map for finding the dummy arguments. */
196 unsigned dnr; /**< The dummy index number. */
197 const lower_params_t *params; /**< lowering parameters */
201 * Return the call list entry of a call node.
202 * If no entry exists yet, allocate one and enter the node into
203 * the call list of the environment.
205 * @param call A Call node.
206 * @param env The environment.
208 static cl_entry *get_Call_entry(ir_node *call, wlk_env *env) {
209 cl_entry *res = get_irn_link(call);
211 cl_entry *res = obstack_alloc(&env->obst, sizeof(*res));
212 res->next = env->cl_list;
215 set_irn_link(call, res);
222 * Post walker: shift all parameter indeces
223 * and collect Calls with compound returns in the call list.
225 static void fix_args_and_collect_calls(ir_node *n, void *ctx) {
229 ir_op *op = get_irn_op(n);
231 if (env->arg_shift > 0 && op == op_Proj) {
232 ir_node *pred = get_Proj_pred(n);
234 /* Fix the argument numbers */
235 if (pred == get_irg_args(current_ir_graph)) {
236 long pnr = get_Proj_proj(n);
237 set_Proj_proj(n, pnr + env->arg_shift);
240 else if (op == op_Call) {
241 ctp = get_Call_type(n);
242 if (env->params->flags & LF_COMPOUND_RETURN) {
243 /* check for compound returns */
244 for (i = get_method_n_ress(ctp) -1; i >= 0; --i) {
245 if (is_compound_type(get_method_res_type(ctp, i))) {
247 * This is a call with a compound return. As the result
248 * might be ignored, we must put it in the list.
250 (void)get_Call_entry(n, env);
255 else if (op == op_CopyB && env->params->flags & LF_COMPOUND_RETURN) {
256 /* check for compound returns */
257 ir_node *src = get_CopyB_src(n);
258 /* older scheme using value_res_ent */
260 ir_node *proj = get_Sel_ptr(src);
261 if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_P_value_res_base) {
262 ir_node *call = get_Proj_pred(proj);
264 /* found a CopyB from compound Call result */
265 cl_entry *e = get_Call_entry(call, env);
266 set_irn_link(n, e->copyb);
271 /* new scheme: compound results are determined by the call type only */
273 ir_node *proj = get_Proj_pred(src);
274 if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_T_result) {
275 ir_node *call = get_Proj_pred(proj);
277 ctp = get_Call_type(call);
278 if (is_compound_type(get_method_res_type(ctp, get_Proj_proj(src)))) {
279 /* found a CopyB from compound Call result */
280 cl_entry *e = get_Call_entry(call, env);
281 set_irn_link(n, e->copyb);
291 * Returns non-zero if a node is a compound address
292 * of a frame-type entity.
294 * @param ft the frame type
295 * @param adr the node
297 static int is_compound_address(ir_type *ft, ir_node *adr)
303 if (get_Sel_n_indexs(adr) != 0)
305 ent = get_Sel_entity(adr);
306 return get_entity_owner(ent) == ft;
309 /** A pair for the copy-return-optimization. */
310 typedef struct cr_pair {
311 entity *ent; /**< the entity than can be removed from the frame */
312 ir_node *arg; /**< the argument that replaces the entities address */
316 * Post walker: fixes all entities addresses for the copy-return
319 * Note: We expect the length of the cr_pair array (ie number of compound
320 * return values) to be 1 (C, C++) in almost all cases, so ignore the
321 * linear search complexity here.
323 static void do_copy_return_opt(ir_node *n, void *ctx) {
328 entity *ent = get_Sel_entity(n);
330 for (i = ARR_LEN(arr) - 1; i >= 0; --i) {
331 if (ent == arr[i].ent) {
332 exchange(n, arr[i].arg);
340 * Return a Sel node that selects a dummy argument of type tp.
341 * Dummy arguments are only needed once and we use a map
343 * We could even assign all dummy arguments the same offset
344 * in the frame type ...
346 * @param irg the graph
347 * @param block the block where a newly create Sel should be placed
348 * @param tp the type of the dummy entity that should be create
349 * @param env the environment
351 static ir_node *get_dummy_sel(ir_graph *irg, ir_node *block, ir_type *tp, wlk_env *env)
356 /* use a map the check if we already create such an entity */
357 e = pmap_find(env->dummy_map, tp);
361 ir_type *ft = get_irg_frame_type(irg);
365 snprintf(buf, sizeof(buf), "dummy.%u", env->dnr++);
366 ent = new_entity(ft, new_id_from_str(buf), tp);
367 sel = new_r_simpleSel(
373 pmap_insert(env->dummy_map, tp, sel);
375 if (get_type_state(ft) == layout_fixed) {
376 /* Fix the layout again */
377 assert(0 && "Fixed layout not implemented");
384 * Add the hidden parameter from the CopyB node to the Call node.
386 * @param irg the graph
387 * @param n_com number of compound results (will be number of hidden parameters)
388 * @param ins in array to store the hidden parameters into
389 * @param entry the call list
390 * @param env the environment
392 static void add_hidden_param(ir_graph *irg, int n_com, ir_node **ins, cl_entry *entry, wlk_env *env)
394 ir_node *p, *n, *src, *mem;
400 for (p = entry->copyb; p; p = n) {
402 src = get_CopyB_src(p);
404 /* old scheme using value_res_ent */
406 ent = get_Sel_entity(src);
407 owner = get_entity_owner(ent);
409 /* find the hidden parameter index */
410 for (idx = 0; idx < get_struct_n_members(owner); ++idx)
411 if (get_struct_member(owner, idx) == ent)
413 assert(idx < get_struct_n_members(owner));
417 /* new scheme: compound returns are determined by the call type and are Proj's */
418 idx = get_Proj_proj(src);
420 ins[idx] = get_CopyB_dst(p);
421 mem = get_CopyB_mem(p);
423 /* get rid of the CopyB */
424 turn_into_tuple(p, pn_CopyB_max);
425 set_Tuple_pred(p, pn_CopyB_M_regular, mem);
426 set_Tuple_pred(p, pn_CopyB_M_except, get_irg_bad(irg));
427 set_Tuple_pred(p, pn_CopyB_X_except, get_irg_bad(irg));
431 /* now create dummy entities for function with ignored return value */
432 if (n_args < n_com) {
433 ir_type *ctp = get_Call_type(entry->call);
436 if (is_lowered_type(ctp))
437 ctp = get_associated_type(ctp);
439 for (j = i = 0; i < get_method_n_ress(ctp); ++i) {
440 ir_type *rtp = get_method_res_type(ctp, i);
441 if (is_compound_type(rtp)) {
443 ins[j] = get_dummy_sel(irg, get_nodes_block(entry->call), rtp, env);
451 * Fix all calls on a call list by adding hidden parameters.
453 * @param irg the graph
454 * @param env the environment
456 static void fix_call_list(ir_graph *irg, wlk_env *env) {
457 const lower_params_t *lp = env->params;
459 ir_node *call, **new_in;
460 ir_type *ctp, *lowered_mtp;
461 add_hidden hidden_params;
462 int i, n_params, n_com, pos;
464 new_in = NEW_ARR_F(ir_node *, 0);
465 for (p = env->cl_list; p; p = p->next) {
467 ctp = get_Call_type(call);
468 lowered_mtp = create_modified_mtd_type(lp, ctp);
469 set_Call_type(call, lowered_mtp);
471 hidden_params = lp->hidden_params;
472 if (hidden_params == ADD_HIDDEN_SMART &&
473 get_method_variadicity(ctp) == variadicity_variadic)
474 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
476 n_params = get_Call_n_params(call);
479 for (i = get_method_n_ress(ctp) - 1; i >= 0; --i) {
480 if (is_compound_type(get_method_res_type(ctp, i)))
484 ARR_RESIZE(ir_node *, new_in, n_params + n_com + pos);
485 memset(new_in, 0, sizeof(*new_in) * (n_params + n_com + pos));
486 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
487 add_hidden_param(irg, n_com, &new_in[pos], p, env);
490 /* copy all other parameters */
491 for (i = 0; i < n_params; ++i)
492 new_in[pos++] = get_Call_param(call, i);
493 if (hidden_params == ADD_HIDDEN_ALWAYS_LAST) {
494 add_hidden_param(irg, n_com, &new_in[pos], p, env);
497 new_in[0] = get_Call_mem(call);
498 new_in[1] = get_Call_ptr(call);
500 set_irn_in(call, n_params + n_com + 2, new_in);
505 * Transform a graph. If it has compound parameter returns,
506 * remove them and use the hidden parameter instead.
507 * If it calls methods with compound parameter returns, add hidden
510 * @param lp parameter struct
511 * @param irg the graph to transform
513 static void transform_irg(const lower_params_t *lp, ir_graph *irg)
515 entity *ent = get_irg_entity(irg);
516 ir_type *mtp, *lowered_mtp, *res_tp, *ft;
517 int i, j, k, n_ress, n_ret_com, n_cr_opt;
518 ir_node **new_in, *ret, *endbl, *bl, *mem, *copy;
521 add_hidden hidden_params;
523 assert(ent && "Cannot tranform graph without an entity");
524 assert(get_irg_phase_state(irg) == phase_high && "call lowering must be done in phase high");
526 mtp = get_entity_type(ent);
528 if (lp->flags & LF_COMPOUND_RETURN) {
529 /* calculate the number of compound returns */
530 n_ress = get_method_n_ress(mtp);
531 for (n_ret_com = i = 0; i < n_ress; ++i) {
532 res_tp = get_method_res_type(mtp, i);
534 if (is_compound_type(res_tp))
540 /* much easier if we have only one return */
541 normalize_one_return(irg);
543 /* This graph has a compound argument. Create a new type */
544 lowered_mtp = create_modified_mtd_type(lp, mtp);
545 set_entity_type(ent, lowered_mtp);
547 hidden_params = lp->hidden_params;
548 if (hidden_params == ADD_HIDDEN_SMART &&
549 get_method_variadicity(mtp) == variadicity_variadic)
550 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
552 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
553 /* hidden arguments are added first */
554 env.arg_shift = n_ret_com;
555 env.first_hidden = 0;
558 /* hidden arguments are added last */
560 env.first_hidden = get_method_n_params(mtp);
564 /* we must only search for calls */
567 obstack_init(&env.obst);
569 env.dummy_map = pmap_create_ex(8);
573 /* scan the code, fix argument numbers and collect calls. */
574 irg_walk_graph(irg, firm_clear_link, fix_args_and_collect_calls, &env);
578 fix_call_list(irg, &env);
582 * Now fix the Return node of the current graph.
585 /* STEP 1: find the return. This is simple, we have normalized the graph. */
586 endbl = get_irg_end_block(irg);
588 for (i = get_Block_n_cfgpreds(endbl) - 1; i >= 0; --i) {
589 ir_node *pred = get_Block_cfgpred(endbl, i);
591 if (is_Return(pred)) {
596 /* there should always be a return */
600 * STEP 2: fix it. For all compound return values add a CopyB,
601 * all others are copied.
603 NEW_ARR_A(ir_node *, new_in, n_ress + 1);
605 bl = get_nodes_block(ret);
606 mem = get_Return_mem(ret);
608 ft = get_irg_frame_type(irg);
609 NEW_ARR_A(cr_pair, cr_opt, n_ret_com);
611 for (j = 1, i = k = 0; i < n_ress; ++i) {
612 ir_node *pred = get_Return_res(ret, i);
613 res_tp = get_method_res_type(mtp, i);
615 if (is_compound_type(res_tp)) {
616 ir_node *arg = get_irg_args(irg);
617 arg = new_r_Proj(irg, get_nodes_block(arg), arg, mode_P_data, env.first_hidden + k);
620 if (is_compound_address(ft, pred)) {
621 /* we can do the copy-return optimization here */
622 cr_opt[n_cr_opt].ent = get_Sel_entity(pred);
623 cr_opt[n_cr_opt].arg = arg;
626 else { /* copy-return optimization is impossible, do the copy. */
634 mem = new_r_Proj(irg, bl, copy, mode_M, pn_CopyB_M_regular);
636 if (lp->flags & LF_RETURN_HIDDEN) {
641 else { /* scalar return value */
646 /* replace the in of the Return */
648 set_irn_in(ret, j, new_in);
651 irg_walk_graph(irg, NULL, do_copy_return_opt, cr_opt);
653 for (i = ARR_LEN(cr_opt) - 1; i >= 0; --i) {
654 remove_class_member(ft, cr_opt[i].ent);
657 } /* if (n_ret_com) */
659 pmap_destroy(env.dummy_map);
660 obstack_free(&env.obst, NULL);
664 * Returns non-zero if the given type is a method
665 * type that must be lowered.
667 * @param lp lowering parameters
668 * @param tp The type.
670 static int must_be_lowered(const lower_params_t *lp, ir_type *tp) {
674 if (is_Method_type(tp)) {
675 if (lp->flags & LF_COMPOUND_RETURN) {
676 /* check for compound returns */
677 n_ress = get_method_n_ress(tp);
678 for (i = 0; i < n_ress; ++i) {
679 res_tp = get_method_res_type(tp, i);
681 if (is_compound_type(res_tp))
690 * type-walker: lower all method types of entities
691 * and points-to types.
693 static void lower_method_types(type_or_ent *tore, void *env)
695 const lower_params_t *lp = env;
698 /* fix method entities */
699 if (is_entity(tore)) {
700 entity *ent = (entity *)tore;
701 tp = get_entity_type(ent);
703 if (must_be_lowered(lp, tp)) {
704 tp = create_modified_mtd_type(lp, tp);
705 set_entity_type(ent, tp);
709 tp = (ir_type *)tore;
711 /* fix pointer to methods */
712 if (is_Pointer_type(tp)) {
713 ir_type *etp = get_pointer_points_to_type(tp);
714 if (must_be_lowered(lp, etp)) {
715 etp = create_modified_mtd_type(lp, etp);
716 set_pointer_points_to_type(tp, etp);
723 * Lower calls with compound parameters and return types.
724 * This function does the following transformations:
726 * - Adds a new (hidden) pointer parameter for
727 * any return compound type.
729 * - Use of the hidden parameters in the function code.
731 * - Change all calls to functions with compound return
732 * by providing space for the hidden parameter on the callers
735 * - Replace a possible block copy after the function call.
737 void lower_calls_with_compounds(const lower_params_t *params)
741 lower_params_t param = *params;
743 if (param.find_pointer_type == NULL) {
744 param.find_pointer_type = def_find_pointer_type;
745 type_map = pmap_create_ex(8);
750 /* first step: Transform all graphs */
751 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
752 irg = get_irp_irg(i);
754 if (irg == get_const_code_irg())
757 transform_irg(¶m, irg);
760 /* second step: Lower all method types of visible entities */
761 type_walk(NULL, lower_method_types, ¶m);
764 pmap_destroy(type_map);