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);
256 else if (op == op_CopyB && env->params->flags & LF_COMPOUND_RETURN) {
257 /* check for compound returns */
258 ir_node *src = get_CopyB_src(n);
259 /* older scheme using value_res_ent */
261 ir_node *proj = get_Sel_ptr(src);
262 if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_P_value_res_base) {
263 ir_node *call = get_Proj_pred(proj);
265 /* found a CopyB from compound Call result */
266 cl_entry *e = get_Call_entry(call, env);
267 set_irn_link(n, e->copyb);
272 /* new scheme: compound results are determined by the call type only */
274 ir_node *proj = get_Proj_pred(src);
275 if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_T_result) {
276 ir_node *call = get_Proj_pred(proj);
278 ctp = get_Call_type(call);
279 if (is_compound_type(get_method_res_type(ctp, get_Proj_proj(src)))) {
280 /* found a CopyB from compound Call result */
281 cl_entry *e = get_Call_entry(call, env);
282 set_irn_link(n, e->copyb);
292 * Returns non-zero if a node is a compound address
293 * of a frame-type entity.
295 * @param ft the frame type
296 * @param adr the node
298 static int is_compound_address(ir_type *ft, ir_node *adr)
304 if (get_Sel_n_indexs(adr) != 0)
306 ent = get_Sel_entity(adr);
307 return get_entity_owner(ent) == ft;
310 /** A pair for the copy-return-optimization. */
311 typedef struct cr_pair {
312 ir_entity *ent; /**< the entity than can be removed from the frame */
313 ir_node *arg; /**< the argument that replaces the entities address */
317 * Post walker: fixes all entities addresses for the copy-return
320 * Note: We expect the length of the cr_pair array (ie number of compound
321 * return values) to be 1 (C, C++) in almost all cases, so ignore the
322 * linear search complexity here.
324 static void do_copy_return_opt(ir_node *n, void *ctx) {
329 ir_entity *ent = get_Sel_entity(n);
331 for (i = ARR_LEN(arr) - 1; i >= 0; --i) {
332 if (ent == arr[i].ent) {
333 exchange(n, arr[i].arg);
341 * Return a Sel node that selects a dummy argument of type tp.
342 * Dummy arguments are only needed once and we use a map
344 * We could even assign all dummy arguments the same offset
345 * in the frame type ...
347 * @param irg the graph
348 * @param block the block where a newly create Sel should be placed
349 * @param tp the type of the dummy entity that should be create
350 * @param env the environment
352 static ir_node *get_dummy_sel(ir_graph *irg, ir_node *block, ir_type *tp, wlk_env *env)
357 /* use a map the check if we already create such an entity */
358 e = pmap_find(env->dummy_map, tp);
362 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 pmap_insert(env->dummy_map, tp, ent);
369 if (get_type_state(ft) == layout_fixed) {
370 /* Fix the layout again */
371 assert(0 && "Fixed layout not implemented");
374 return new_r_simpleSel(
383 * Add the hidden parameter from the CopyB node to the Call node.
385 * @param irg the graph
386 * @param n_com number of compound results (will be number of hidden parameters)
387 * @param ins in array to store the hidden parameters into
388 * @param entry the call list
389 * @param env the environment
391 static void add_hidden_param(ir_graph *irg, int n_com, ir_node **ins, cl_entry *entry, wlk_env *env)
393 ir_node *p, *n, *src, *mem;
399 for (p = entry->copyb; p; p = n) {
401 src = get_CopyB_src(p);
403 /* old scheme using value_res_ent */
405 ent = get_Sel_entity(src);
406 owner = get_entity_owner(ent);
408 /* find the hidden parameter index */
409 for (idx = 0; idx < get_struct_n_members(owner); ++idx)
410 if (get_struct_member(owner, idx) == ent)
412 assert(idx < get_struct_n_members(owner));
416 /* new scheme: compound returns are determined by the call type and are Proj's */
417 idx = get_Proj_proj(src);
419 ins[idx] = get_CopyB_dst(p);
420 mem = get_CopyB_mem(p);
422 /* get rid of the CopyB */
423 turn_into_tuple(p, pn_CopyB_max);
424 set_Tuple_pred(p, pn_CopyB_M_regular, mem);
425 set_Tuple_pred(p, pn_CopyB_M_except, get_irg_bad(irg));
426 set_Tuple_pred(p, pn_CopyB_X_except, get_irg_bad(irg));
430 /* now create dummy entities for function with ignored return value */
431 if (n_args < n_com) {
432 ir_type *ctp = get_Call_type(entry->call);
435 if (is_lowered_type(ctp))
436 ctp = get_associated_type(ctp);
438 for (j = i = 0; i < get_method_n_ress(ctp); ++i) {
439 ir_type *rtp = get_method_res_type(ctp, i);
440 if (is_compound_type(rtp)) {
442 ins[j] = get_dummy_sel(irg, get_nodes_block(entry->call), rtp, env);
450 * Fix all calls on a call list by adding hidden parameters.
452 * @param irg the graph
453 * @param env the environment
455 static void fix_call_list(ir_graph *irg, wlk_env *env) {
456 const lower_params_t *lp = env->params;
458 ir_node *call, **new_in;
459 ir_type *ctp, *lowered_mtp;
460 add_hidden hidden_params;
461 int i, n_params, n_com, pos;
463 new_in = NEW_ARR_F(ir_node *, 0);
464 for (p = env->cl_list; p; p = p->next) {
466 ctp = get_Call_type(call);
467 lowered_mtp = create_modified_mtd_type(lp, ctp);
468 set_Call_type(call, lowered_mtp);
470 hidden_params = lp->hidden_params;
471 if (hidden_params == ADD_HIDDEN_SMART &&
472 get_method_variadicity(ctp) == variadicity_variadic)
473 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
475 n_params = get_Call_n_params(call);
478 for (i = get_method_n_ress(ctp) - 1; i >= 0; --i) {
479 if (is_compound_type(get_method_res_type(ctp, i)))
483 ARR_RESIZE(ir_node *, new_in, n_params + n_com + pos);
484 memset(new_in, 0, sizeof(*new_in) * (n_params + n_com + pos));
485 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
486 add_hidden_param(irg, n_com, &new_in[pos], p, env);
489 /* copy all other parameters */
490 for (i = 0; i < n_params; ++i)
491 new_in[pos++] = get_Call_param(call, i);
492 if (hidden_params == ADD_HIDDEN_ALWAYS_LAST) {
493 add_hidden_param(irg, n_com, &new_in[pos], p, env);
496 new_in[0] = get_Call_mem(call);
497 new_in[1] = get_Call_ptr(call);
499 set_irn_in(call, n_params + n_com + 2, new_in);
504 * Transform a graph. If it has compound parameter returns,
505 * remove them and use the hidden parameter instead.
506 * If it calls methods with compound parameter returns, add hidden
509 * @param lp parameter struct
510 * @param irg the graph to transform
512 static void transform_irg(const lower_params_t *lp, ir_graph *irg)
514 ir_entity *ent = get_irg_entity(irg);
515 ir_type *mtp, *lowered_mtp, *tp, *ft;
516 int i, j, k, n_ress = 0, n_ret_com = 0, n_cr_opt;
517 ir_node **new_in, *ret, *endbl, *bl, *mem, *copy;
520 add_hidden hidden_params;
522 assert(ent && "Cannot tranform graph without an entity");
523 assert(get_irg_phase_state(irg) == phase_high && "call lowering must be done in phase high");
525 mtp = get_entity_type(ent);
527 if (lp->flags & LF_COMPOUND_RETURN) {
528 /* calculate the number of compound returns */
529 n_ress = get_method_n_ress(mtp);
530 for (n_ret_com = i = 0; i < n_ress; ++i) {
531 tp = get_method_res_type(mtp, i);
533 if (is_compound_type(tp))
539 /* much easier if we have only one return */
540 normalize_one_return(irg);
542 /* This graph has a compound argument. Create a new type */
543 lowered_mtp = create_modified_mtd_type(lp, mtp);
544 set_entity_type(ent, lowered_mtp);
546 hidden_params = lp->hidden_params;
547 if (hidden_params == ADD_HIDDEN_SMART &&
548 get_method_variadicity(mtp) == variadicity_variadic)
549 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
551 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
552 /* hidden arguments are added first */
553 env.arg_shift = n_ret_com;
554 env.first_hidden = 0;
557 /* hidden arguments are added last */
559 env.first_hidden = get_method_n_params(mtp);
563 /* we must only search for calls */
566 obstack_init(&env.obst);
568 env.dummy_map = pmap_create_ex(8);
572 /* scan the code, fix argument numbers and collect calls. */
573 irg_walk_graph(irg, firm_clear_link, fix_args_and_collect_calls, &env);
577 fix_call_list(irg, &env);
581 * Now fix the Return node of the current graph.
584 /* STEP 1: find the return. This is simple, we have normalized the graph. */
585 endbl = get_irg_end_block(irg);
587 for (i = get_Block_n_cfgpreds(endbl) - 1; i >= 0; --i) {
588 ir_node *pred = get_Block_cfgpred(endbl, i);
590 if (is_Return(pred)) {
595 /* there should always be a return */
599 * STEP 2: fix it. For all compound return values add a CopyB,
600 * all others are copied.
602 NEW_ARR_A(ir_node *, new_in, n_ress + 1);
604 bl = get_nodes_block(ret);
605 mem = get_Return_mem(ret);
607 ft = get_irg_frame_type(irg);
608 NEW_ARR_A(cr_pair, cr_opt, n_ret_com);
610 for (j = 1, i = k = 0; i < n_ress; ++i) {
611 ir_node *pred = get_Return_res(ret, i);
612 tp = get_method_res_type(mtp, i);
614 if (is_compound_type(tp)) {
615 ir_node *arg = get_irg_args(irg);
616 arg = new_r_Proj(irg, get_nodes_block(arg), arg, mode_P_data, env.first_hidden + k);
619 if (is_compound_address(ft, pred)) {
620 /* we can do the copy-return optimization here */
621 cr_opt[n_cr_opt].ent = get_Sel_entity(pred);
622 cr_opt[n_cr_opt].arg = arg;
625 else { /* copy-return optimization is impossible, do the copy. */
633 mem = new_r_Proj(irg, bl, copy, mode_M, pn_CopyB_M_regular);
635 if (lp->flags & LF_RETURN_HIDDEN) {
640 else { /* scalar return value */
645 /* replace the in of the Return */
647 set_irn_in(ret, j, new_in);
650 irg_walk_graph(irg, NULL, do_copy_return_opt, cr_opt);
652 for (i = ARR_LEN(cr_opt) - 1; i >= 0; --i) {
653 remove_class_member(ft, cr_opt[i].ent);
656 } /* if (n_ret_com) */
658 pmap_destroy(env.dummy_map);
659 obstack_free(&env.obst, NULL);
663 * Returns non-zero if the given type is a method
664 * type that must be lowered.
666 * @param lp lowering parameters
667 * @param tp The type.
669 static int must_be_lowered(const lower_params_t *lp, ir_type *tp) {
673 if (is_Method_type(tp)) {
674 if (lp->flags & LF_COMPOUND_RETURN) {
675 /* check for compound returns */
676 n_ress = get_method_n_ress(tp);
677 for (i = 0; i < n_ress; ++i) {
678 res_tp = get_method_res_type(tp, i);
680 if (is_compound_type(res_tp))
689 * type-walker: lower all method types of entities
690 * and points-to types.
692 static void lower_method_types(type_or_ent *tore, void *env)
694 const lower_params_t *lp = env;
697 /* fix method entities */
698 if (is_entity(tore)) {
699 ir_entity *ent = (ir_entity *)tore;
700 tp = get_entity_type(ent);
702 if (must_be_lowered(lp, tp)) {
703 tp = create_modified_mtd_type(lp, tp);
704 set_entity_type(ent, tp);
708 tp = (ir_type *)tore;
710 /* fix pointer to methods */
711 if (is_Pointer_type(tp)) {
712 ir_type *etp = get_pointer_points_to_type(tp);
713 if (must_be_lowered(lp, etp)) {
714 etp = create_modified_mtd_type(lp, etp);
715 set_pointer_points_to_type(tp, etp);
722 * Lower calls with compound parameters and return types.
723 * This function does the following transformations:
725 * - Adds a new (hidden) pointer parameter for
726 * any return compound type.
728 * - Use of the hidden parameters in the function code.
730 * - Change all calls to functions with compound return
731 * by providing space for the hidden parameter on the callers
734 * - Replace a possible block copy after the function call.
736 void lower_calls_with_compounds(const lower_params_t *params)
740 lower_params_t param = *params;
742 if (param.find_pointer_type == NULL) {
743 param.find_pointer_type = def_find_pointer_type;
744 type_map = pmap_create_ex(8);
749 /* first step: Transform all graphs */
750 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
751 irg = get_irp_irg(i);
753 if (irg == get_const_code_irg())
756 transform_irg(¶m, irg);
759 /* second step: Lower all method types of visible entities */
760 type_walk(NULL, lower_method_types, ¶m);
763 pmap_destroy(type_map);