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.
24 #include "lower_calls.h"
31 /** A type map for def_find_pointer_type. */
32 static pmap *type_map;
35 * Default implementation for finding a pointer type for a given element type.
36 * Simple create a new one.
38 static ir_type *def_find_pointer_type(ir_type *e_type, ir_mode *mode, int alignment)
43 /* Mode and alignment are always identical in all calls to def_find_pointer_type(), so
44 we simply can use a map from the element type to the pointer type. */
45 e = pmap_find(type_map, e_type);
49 res = new_type_pointer(mangle_u(get_type_ident(e_type), new_id_from_chars("Ptr", 3)), e_type, mode);
50 set_type_alignment_bytes(res, alignment);
51 pmap_insert(type_map, e_type, res);
57 * Creates a new lowered type for a method type with compound
58 * arguments. The new type is associated to the old one and returned.
60 * @param lp parameter struct
61 * @param mtp the method type to lower
63 * The current implementation expects that a lowered type already
64 * includes the necessary changes ...
66 static ir_type *create_modified_mtd_type(const lower_params_t *lp, ir_type *mtp)
68 ir_type *lowered, *ptr_tp;
69 ir_type **params, **results, *res_tp;
70 ir_mode *modes[MAX_REGISTER_RET_VAL];
71 int n_ress, n_params, nn_ress, nn_params, i, first_variadic;
73 add_hidden hidden_params;
76 if (is_lowered_type(mtp)) {
77 /* the type is already lowered. Not handled yet. */
78 assert(0 && "lowered types NYI");
81 lowered = get_associated_type(mtp);
85 n_ress = get_method_n_ress(mtp);
86 NEW_ARR_A(ir_type *, results, n_ress);
88 n_params = get_method_n_params(mtp);
89 NEW_ARR_A(ir_type *, params, n_params + n_ress);
91 first_variadic = get_method_first_variadic_param_index(mtp);
93 hidden_params = lp->hidden_params;
94 if (hidden_params == ADD_HIDDEN_SMART &&
95 get_method_variadicity(mtp) == variadicity_variadic)
96 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
98 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
99 /* add hidden in front */
100 for (nn_ress = nn_params = i = 0; i < n_ress; ++i) {
101 res_tp = get_method_res_type(mtp, i);
103 if (is_compound_type(res_tp)) {
106 if (lp->flags & LF_SMALL_CMP_IN_REGS)
107 n_regs = lp->ret_compound_in_regs(res_tp, modes);
110 /* this compound will be returned solely in registers */
114 /* this compound will be allocated on callers stack and its
115 address will be transmitted as a hidden parameter. */
116 ptr_tp = lp->find_pointer_type(res_tp, get_modeP_data(), lp->def_ptr_alignment);
117 params[nn_params++] = ptr_tp;
118 if (lp->flags & LF_RETURN_HIDDEN)
119 results[nn_ress++] = ptr_tp;
123 results[nn_ress++] = res_tp;
126 /* move the index of the first variadic parameter */
127 first_variadic += nn_params;
129 for (i = 0; i < n_params; ++i)
130 params[nn_params++] = get_method_param_type(mtp, i);
133 /* add hidden parameters last */
134 assert(get_method_variadicity(mtp) == variadicity_non_variadic &&
135 "Cannot add hidden parameters at end of variadic function");
137 for (nn_params = 0; nn_params < n_params; ++nn_params)
138 params[nn_params] = get_method_param_type(mtp, nn_params);
140 for (nn_ress = i = 0; i < n_ress; ++i) {
141 res_tp = get_method_res_type(mtp, i);
143 if (is_compound_type(res_tp))
144 params[nn_params++] = lp->find_pointer_type(res_tp, get_modeP_data(), lp->def_ptr_alignment);
146 results[nn_ress++] = res_tp;
150 /* create the new type */
151 id = mangle_u(new_id_from_chars("L", 1), get_type_ident(mtp));
152 lowered = new_d_type_method(id, nn_params, nn_ress, get_type_dbg_info(mtp));
155 for (i = 0; i < nn_params; ++i)
156 set_method_param_type(lowered, i, params[i]);
157 for (i = 0; i < nn_ress; ++i)
158 set_method_res_type(lowered, i, results[i]);
160 var = get_method_variadicity(mtp);
161 set_method_variadicity(lowered, var);
162 if (var == variadicity_variadic)
163 set_method_first_variadic_param_index(lowered, first_variadic);
165 /* associate the lowered type with the original one for easier access */
166 set_lowered_type(mtp, lowered);
174 typedef struct cl_entry cl_entry;
176 cl_entry *next; /**< Pointer to the next entry. */
177 ir_node *call; /**< Pointer to the Call node. */
178 ir_node *copyb; /**< List of all CopyB nodes. */
182 * Walker environment for fix_args_and_collect_calls().
184 typedef struct _wlk_env_t {
185 int arg_shift; /**< The Argument index shift for parameters. */
186 int first_hidden; /**< The index of the first hidden argument. */
187 struct obstack obst; /**< An obstack to allocate the data on. */
188 cl_entry *cl_list; /**< The call list. */
189 pmap *dummy_map; /**< A map for finding the dummy arguments. */
190 unsigned dnr; /**< The dummy index number. */
191 const lower_params_t *params; /**< lowering parameters */
195 * Return the call list entry of a call node.
196 * If no entry exists yet, allocate one and enter the node into
197 * the call list of the environment.
199 * @param call A Call node.
200 * @param env The environment.
202 static cl_entry *get_Call_entry(ir_node *call, wlk_env *env) {
203 cl_entry *res = get_irn_link(call);
205 cl_entry *res = obstack_alloc(&env->obst, sizeof(*res));
206 res->next = env->cl_list;
209 set_irn_link(call, res);
216 * Post walker: shift all parameter indeces
217 * and collect Calls with compound returns in the call list.
219 static void fix_args_and_collect_calls(ir_node *n, void *ctx) {
223 ir_op *op = get_irn_op(n);
225 if (env->arg_shift > 0 && op == op_Proj) {
226 ir_node *pred = get_Proj_pred(n);
228 /* Fix the argument numbers */
229 if (pred == get_irg_args(current_ir_graph)) {
230 long pnr = get_Proj_proj(n);
231 set_Proj_proj(n, pnr + env->arg_shift);
234 else if (op == op_Call) {
235 ctp = get_Call_type(n);
236 if (env->params->flags & LF_COMPOUND_RETURN) {
237 /* check for compound returns */
238 for (i = get_method_n_ress(ctp) -1; i >= 0; --i) {
239 if (is_compound_type(get_method_res_type(ctp, i))) {
241 * This is a call with a compound return. As the result
242 * might be ignored, we must put it in the list.
244 (void)get_Call_entry(n, env);
250 else if (op == op_CopyB && env->params->flags & LF_COMPOUND_RETURN) {
251 /* check for compound returns */
252 ir_node *src = get_CopyB_src(n);
253 /* older scheme using value_res_ent */
255 ir_node *proj = get_Sel_ptr(src);
256 if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_P_value_res_base) {
257 ir_node *call = get_Proj_pred(proj);
259 /* found a CopyB from compound Call result */
260 cl_entry *e = get_Call_entry(call, env);
261 set_irn_link(n, e->copyb);
266 /* new scheme: compound results are determined by the call type only */
268 ir_node *proj = get_Proj_pred(src);
269 if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_T_result) {
270 ir_node *call = get_Proj_pred(proj);
272 ctp = get_Call_type(call);
273 if (is_compound_type(get_method_res_type(ctp, get_Proj_proj(src)))) {
274 /* found a CopyB from compound Call result */
275 cl_entry *e = get_Call_entry(call, env);
276 set_irn_link(n, e->copyb);
286 * Returns non-zero if a node is a compound address
287 * of a frame-type entity.
289 * @param ft the frame type
290 * @param adr the node
292 static int is_compound_address(ir_type *ft, ir_node *adr)
298 if (get_Sel_n_indexs(adr) != 0)
300 ent = get_Sel_entity(adr);
301 return get_entity_owner(ent) == ft;
304 /** A pair for the copy-return-optimization. */
305 typedef struct cr_pair {
306 ir_entity *ent; /**< the entity than can be removed from the frame */
307 ir_node *arg; /**< the argument that replaces the entities address */
311 * Post walker: fixes all entities addresses for the copy-return
314 * Note: We expect the length of the cr_pair array (ie number of compound
315 * return values) to be 1 (C, C++) in almost all cases, so ignore the
316 * linear search complexity here.
318 static void do_copy_return_opt(ir_node *n, void *ctx) {
323 ir_entity *ent = get_Sel_entity(n);
325 for (i = ARR_LEN(arr) - 1; i >= 0; --i) {
326 if (ent == arr[i].ent) {
327 exchange(n, arr[i].arg);
335 * Return a Sel node that selects a dummy argument of type tp.
336 * Dummy arguments are only needed once and we use a map
338 * We could even assign all dummy arguments the same offset
339 * in the frame type ...
341 * @param irg the graph
342 * @param block the block where a newly create Sel should be placed
343 * @param tp the type of the dummy entity that should be create
344 * @param env the environment
346 static ir_node *get_dummy_sel(ir_graph *irg, ir_node *block, ir_type *tp, wlk_env *env)
351 /* use a map the check if we already create such an entity */
352 e = pmap_find(env->dummy_map, tp);
356 ir_type *ft = get_irg_frame_type(irg);
359 snprintf(buf, sizeof(buf), "dummy.%u", env->dnr++);
360 ent = new_entity(ft, new_id_from_str(buf), tp);
361 pmap_insert(env->dummy_map, tp, ent);
363 if (get_type_state(ft) == layout_fixed) {
364 /* Fix the layout again */
365 assert(0 && "Fixed layout not implemented");
368 return new_r_simpleSel(
377 * Add the hidden parameter from the CopyB node to the Call node.
379 * @param irg the graph
380 * @param n_com number of compound results (will be number of hidden parameters)
381 * @param ins in array to store the hidden parameters into
382 * @param entry the call list
383 * @param env the environment
385 static void add_hidden_param(ir_graph *irg, int n_com, ir_node **ins, cl_entry *entry, wlk_env *env)
387 ir_node *p, *n, *src, *mem;
393 for (p = entry->copyb; p; p = n) {
395 src = get_CopyB_src(p);
397 /* old scheme using value_res_ent */
399 ent = get_Sel_entity(src);
400 owner = get_entity_owner(ent);
402 /* find the hidden parameter index */
403 for (idx = 0; idx < get_struct_n_members(owner); ++idx)
404 if (get_struct_member(owner, idx) == ent)
406 assert(idx < get_struct_n_members(owner));
410 /* new scheme: compound returns are determined by the call type and are Proj's */
411 idx = get_Proj_proj(src);
413 ins[idx] = get_CopyB_dst(p);
414 mem = get_CopyB_mem(p);
416 /* get rid of the CopyB */
417 turn_into_tuple(p, pn_CopyB_max);
418 set_Tuple_pred(p, pn_CopyB_M_regular, mem);
419 set_Tuple_pred(p, pn_CopyB_M_except, get_irg_bad(irg));
420 set_Tuple_pred(p, pn_CopyB_X_except, get_irg_bad(irg));
424 /* now create dummy entities for function with ignored return value */
425 if (n_args < n_com) {
426 ir_type *ctp = get_Call_type(entry->call);
429 if (is_lowered_type(ctp))
430 ctp = get_associated_type(ctp);
432 for (j = i = 0; i < get_method_n_ress(ctp); ++i) {
433 ir_type *rtp = get_method_res_type(ctp, i);
434 if (is_compound_type(rtp)) {
436 ins[j] = get_dummy_sel(irg, get_nodes_block(entry->call), rtp, env);
444 * Fix all calls on a call list by adding hidden parameters.
446 * @param irg the graph
447 * @param env the environment
449 static void fix_call_list(ir_graph *irg, wlk_env *env) {
450 const lower_params_t *lp = env->params;
452 ir_node *call, **new_in;
453 ir_type *ctp, *lowered_mtp;
454 add_hidden hidden_params;
455 int i, n_params, n_com, pos;
457 new_in = NEW_ARR_F(ir_node *, 0);
458 for (p = env->cl_list; p; p = p->next) {
460 ctp = get_Call_type(call);
461 lowered_mtp = create_modified_mtd_type(lp, ctp);
462 set_Call_type(call, lowered_mtp);
464 hidden_params = lp->hidden_params;
465 if (hidden_params == ADD_HIDDEN_SMART &&
466 get_method_variadicity(ctp) == variadicity_variadic)
467 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
469 n_params = get_Call_n_params(call);
472 for (i = get_method_n_ress(ctp) - 1; i >= 0; --i) {
473 if (is_compound_type(get_method_res_type(ctp, i)))
477 ARR_RESIZE(ir_node *, new_in, n_params + n_com + pos);
478 memset(new_in, 0, sizeof(*new_in) * (n_params + n_com + pos));
479 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
480 add_hidden_param(irg, n_com, &new_in[pos], p, env);
483 /* copy all other parameters */
484 for (i = 0; i < n_params; ++i)
485 new_in[pos++] = get_Call_param(call, i);
486 if (hidden_params == ADD_HIDDEN_ALWAYS_LAST) {
487 add_hidden_param(irg, n_com, &new_in[pos], p, env);
490 new_in[0] = get_Call_mem(call);
491 new_in[1] = get_Call_ptr(call);
493 set_irn_in(call, n_params + n_com + 2, new_in);
498 * Transform a graph. If it has compound parameter returns,
499 * remove them and use the hidden parameter instead.
500 * If it calls methods with compound parameter returns, add hidden
503 * @param lp parameter struct
504 * @param irg the graph to transform
506 static void transform_irg(const lower_params_t *lp, ir_graph *irg)
508 ir_entity *ent = get_irg_entity(irg);
509 ir_type *mtp, *lowered_mtp, *tp, *ft;
510 int i, j, k, n_ress = 0, n_ret_com = 0, n_cr_opt;
511 ir_node **new_in, *ret, *endbl, *bl, *mem, *copy;
514 add_hidden hidden_params;
516 assert(ent && "Cannot tranform graph without an entity");
517 assert(get_irg_phase_state(irg) == phase_high && "call lowering must be done in phase high");
519 mtp = get_entity_type(ent);
521 if (lp->flags & LF_COMPOUND_RETURN) {
522 /* calculate the number of compound returns */
523 n_ress = get_method_n_ress(mtp);
524 for (n_ret_com = i = 0; i < n_ress; ++i) {
525 tp = get_method_res_type(mtp, i);
527 if (is_compound_type(tp))
533 /* much easier if we have only one return */
534 normalize_one_return(irg);
536 /* This graph has a compound argument. Create a new type */
537 lowered_mtp = create_modified_mtd_type(lp, mtp);
538 set_entity_type(ent, lowered_mtp);
540 hidden_params = lp->hidden_params;
541 if (hidden_params == ADD_HIDDEN_SMART &&
542 get_method_variadicity(mtp) == variadicity_variadic)
543 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
545 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
546 /* hidden arguments are added first */
547 env.arg_shift = n_ret_com;
548 env.first_hidden = 0;
551 /* hidden arguments are added last */
553 env.first_hidden = get_method_n_params(mtp);
557 /* we must only search for calls */
560 obstack_init(&env.obst);
562 env.dummy_map = pmap_create_ex(8);
566 /* scan the code, fix argument numbers and collect calls. */
567 irg_walk_graph(irg, firm_clear_link, fix_args_and_collect_calls, &env);
571 fix_call_list(irg, &env);
575 * Now fix the Return node of the current graph.
578 /* STEP 1: find the return. This is simple, we have normalized the graph. */
579 endbl = get_irg_end_block(irg);
581 for (i = get_Block_n_cfgpreds(endbl) - 1; i >= 0; --i) {
582 ir_node *pred = get_Block_cfgpred(endbl, i);
584 if (is_Return(pred)) {
589 /* there should always be a return */
593 * STEP 2: fix it. For all compound return values add a CopyB,
594 * all others are copied.
596 NEW_ARR_A(ir_node *, new_in, n_ress + 1);
598 bl = get_nodes_block(ret);
599 mem = get_Return_mem(ret);
601 ft = get_irg_frame_type(irg);
602 NEW_ARR_A(cr_pair, cr_opt, n_ret_com);
604 for (j = 1, i = k = 0; i < n_ress; ++i) {
605 ir_node *pred = get_Return_res(ret, i);
606 tp = get_method_res_type(mtp, i);
608 if (is_compound_type(tp)) {
609 ir_node *arg = get_irg_args(irg);
610 arg = new_r_Proj(irg, get_nodes_block(arg), arg, mode_P_data, env.first_hidden + k);
613 if (is_compound_address(ft, pred)) {
614 /* we can do the copy-return optimization here */
615 cr_opt[n_cr_opt].ent = get_Sel_entity(pred);
616 cr_opt[n_cr_opt].arg = arg;
619 else { /* copy-return optimization is impossible, do the copy. */
627 mem = new_r_Proj(irg, bl, copy, mode_M, pn_CopyB_M_regular);
629 if (lp->flags & LF_RETURN_HIDDEN) {
634 else { /* scalar return value */
639 /* replace the in of the Return */
641 set_irn_in(ret, j, new_in);
644 irg_walk_graph(irg, NULL, do_copy_return_opt, cr_opt);
646 for (i = ARR_LEN(cr_opt) - 1; i >= 0; --i) {
647 remove_class_member(ft, cr_opt[i].ent);
650 } /* if (n_ret_com) */
652 pmap_destroy(env.dummy_map);
653 obstack_free(&env.obst, NULL);
657 * Returns non-zero if the given type is a method
658 * type that must be lowered.
660 * @param lp lowering parameters
661 * @param tp The type.
663 static int must_be_lowered(const lower_params_t *lp, ir_type *tp) {
667 if (is_Method_type(tp)) {
668 if (lp->flags & LF_COMPOUND_RETURN) {
669 /* check for compound returns */
670 n_ress = get_method_n_ress(tp);
671 for (i = 0; i < n_ress; ++i) {
672 res_tp = get_method_res_type(tp, i);
674 if (is_compound_type(res_tp))
683 * type-walker: lower all method types of entities
684 * and points-to types.
686 static void lower_method_types(type_or_ent *tore, void *env)
688 const lower_params_t *lp = env;
691 /* fix method entities */
692 if (is_entity(tore)) {
693 ir_entity *ent = (ir_entity *)tore;
694 tp = get_entity_type(ent);
696 if (must_be_lowered(lp, tp)) {
697 tp = create_modified_mtd_type(lp, tp);
698 set_entity_type(ent, tp);
702 tp = (ir_type *)tore;
704 /* fix pointer to methods */
705 if (is_Pointer_type(tp)) {
706 ir_type *etp = get_pointer_points_to_type(tp);
707 if (must_be_lowered(lp, etp)) {
708 etp = create_modified_mtd_type(lp, etp);
709 set_pointer_points_to_type(tp, etp);
716 * Lower calls with compound parameters and return types.
717 * This function does the following transformations:
719 * - Adds a new (hidden) pointer parameter for
720 * any return compound type.
722 * - Use of the hidden parameters in the function code.
724 * - Change all calls to functions with compound return
725 * by providing space for the hidden parameter on the callers
728 * - Replace a possible block copy after the function call.
730 void lower_calls_with_compounds(const lower_params_t *params)
734 lower_params_t param = *params;
736 if (param.find_pointer_type == NULL) {
737 param.find_pointer_type = def_find_pointer_type;
738 type_map = pmap_create_ex(8);
743 /* first step: Transform all graphs */
744 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
745 irg = get_irp_irg(i);
747 if (irg == get_const_code_irg())
750 transform_irg(¶m, irg);
753 /* second step: Lower all method types of visible entities */
754 type_walk(NULL, lower_method_types, ¶m);
757 pmap_destroy(type_map);