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 necessery changes ...
72 static ir_type *create_modified_mtd_type(const lower_params_t *lp, ir_type *mtp)
75 ir_type **params, **results, *res_tp;
76 int n_ress, n_params, nn_ress, nn_params, i, first_variadic;
78 add_hidden hidden_params;
81 if (is_lowered_type(mtp)) {
82 /* the type is already lowered. Not handled yet. */
83 assert(0 && "lowered types NYI");
86 lowered = get_associated_type(mtp);
90 n_ress = get_method_n_ress(mtp);
91 NEW_ARR_A(ir_type *, results, n_ress);
93 n_params = get_method_n_params(mtp);
94 NEW_ARR_A(ir_type *, params, n_params + n_ress);
96 first_variadic = get_method_first_variadic_param_index(mtp);
98 hidden_params = lp->hidden_params;
99 if (hidden_params == ADD_HIDDEN_SMART &&
100 get_method_variadicity(mtp) == variadicity_variadic)
101 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
103 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
104 /* add hidden in front */
105 for (nn_ress = nn_params = i = 0; i < n_ress; ++i) {
106 res_tp = get_method_res_type(mtp, i);
108 if (is_compound_type(res_tp))
109 params[nn_params++] = lp->find_pointer_type(res_tp, get_modeP_data(), lp->def_ptr_alignment);
111 results[nn_ress++] = res_tp;
114 /* move the index of the first variadic parameter */
115 first_variadic += nn_params;
117 for (i = 0; i < n_params; ++i)
118 params[nn_params++] = get_method_param_type(mtp, i);
121 /* add hidden parameters last */
122 assert(get_method_variadicity(mtp) == variadicity_non_variadic &&
123 "Cannot add hidden parameters at end of variadic function");
125 for (nn_params = 0; nn_params < n_params; ++nn_params)
126 params[nn_params] = get_method_param_type(mtp, nn_params);
128 for (nn_ress = i = 0; i < n_ress; ++i) {
129 res_tp = get_method_res_type(mtp, i);
131 if (is_compound_type(res_tp))
132 params[nn_params++] = lp->find_pointer_type(res_tp, get_modeP_data(), lp->def_ptr_alignment);
134 results[nn_ress++] = res_tp;
138 /* create the new type */
139 id = mangle_u(new_id_from_chars("L", 1), get_type_ident(mtp));
140 lowered = new_d_type_method(id, nn_params, nn_ress, get_type_dbg_info(mtp));
143 for (i = 0; i < nn_params; ++i)
144 set_method_param_type(lowered, i, params[i]);
145 for (i = 0; i < nn_ress; ++i)
146 set_method_res_type(lowered, i, results[i]);
148 var = get_method_variadicity(mtp);
149 set_method_variadicity(lowered, var);
150 if (var == variadicity_variadic)
151 set_method_first_variadic_param_index(lowered, first_variadic);
153 /* associate the lowered type with the original one for easier access */
154 set_lowered_type(mtp, lowered);
162 typedef struct cl_entry cl_entry;
164 cl_entry *next; /**< Pointer to the next entry. */
165 ir_node *call; /**< Pointer to the Call node. */
166 ir_node *copyb; /**< List of all CopyB nodes. */
170 * Walker environment for fix_args_and_collect_calls().
172 typedef struct _wlk_env_t {
173 int arg_shift; /**< The Argument index shift for parameters. */
174 int first_hidden; /**< The index of the first hidden argument. */
175 struct obstack obst; /**< An obstack to allocate the data on. */
176 cl_entry *cl_list; /**< The call list. */
177 pmap *dummy_map; /**< A map for finding the dummy arguments. */
178 unsigned dnr; /**< The dummy index number. */
182 * Return the call list entry of a call node.
183 * If no entry exists yet, allocate one and enter the node into
184 * the call list of the environment.
186 * @param call A Call node.
187 * @param env The environment.
189 static cl_entry *get_Call_entry(ir_node *call, wlk_env *env) {
190 cl_entry *res = get_irn_link(call);
192 cl_entry *res = obstack_alloc(&env->obst, sizeof(*res));
193 res->next = env->cl_list;
196 set_irn_link(call, res);
203 * Post walker: shift all parameter indeces
204 * and collect Calls with compound returns in the call list.
206 static void fix_args_and_collect_calls(ir_node *n, void *ctx) {
210 ir_op *op = get_irn_op(n);
212 if (env->arg_shift > 0 && op == op_Proj) {
213 ir_node *pred = get_Proj_pred(n);
215 /* Fix the argument numbers */
216 if (pred == get_irg_args(current_ir_graph)) {
217 long pnr = get_Proj_proj(n);
218 set_Proj_proj(n, pnr + env->arg_shift);
221 else if (op == op_Call) {
222 ctp = get_Call_type(n);
223 for (i = get_method_n_ress(ctp) -1; i >= 0; --i) {
224 if (is_compound_type(get_method_res_type(ctp, i))) {
226 * This is a call with a compound return. As the result
227 * might be ignored, we must put it in the list.
229 (void)get_Call_entry(n, env);
233 else if (op == op_CopyB) {
234 ir_node *src = get_CopyB_src(n);
235 /* older scheme using value_res_ent */
237 ir_node *proj = get_Sel_ptr(src);
238 if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_P_value_res_base) {
239 ir_node *call = get_Proj_pred(proj);
241 /* found a CopyB from compound Call result */
242 cl_entry *e = get_Call_entry(call, env);
243 set_irn_link(n, e->copyb);
248 /* new scheme: compound results are determined by the call type only */
250 ir_node *proj = get_Proj_pred(src);
251 if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_T_result) {
252 ir_node *call = get_Proj_pred(proj);
254 ctp = get_Call_type(call);
255 if (is_compound_type(get_method_res_type(ctp, get_Proj_proj(src)))) {
256 /* found a CopyB from compound Call result */
257 cl_entry *e = get_Call_entry(call, env);
258 set_irn_link(n, e->copyb);
268 * Returns non-zero if a node is a compound address
269 * of a frame-type entity.
271 * @param ft the frame type
272 * @param adr the node
274 static int is_compound_address(ir_type *ft, ir_node *adr)
280 if (get_Sel_n_indexs(adr) != 0)
282 ent = get_Sel_entity(adr);
283 return get_entity_owner(ent) == ft;
286 /** A pair for the copy-return-optimization. */
287 typedef struct cr_pair {
288 entity *ent; /**< the entity than can be removed from the frame */
289 ir_node *arg; /**< the argument that replaces the entities address */
293 * Post walker: fixes all entities addresses for the copy-return
296 * Note: We expect the length of the cr_pair array (ie number of compound
297 * return values) to be 1 (C, C++) in almost all cases, so ignore the
298 * linear search complexity here.
300 static void do_copy_return_opt(ir_node *n, void *ctx) {
305 entity *ent = get_Sel_entity(n);
307 for (i = ARR_LEN(arr) - 1; i >= 0; --i) {
308 if (ent == arr[i].ent) {
309 exchange(n, arr[i].arg);
317 * Return a Sel node that selects a dummy argument of type tp.
318 * Dummy arguments are only needed once and we use a map
320 * We could even assign all dummy arguments the same offset
321 * in the frame type ...
323 * @param irg the graph
324 * @param block the block where a newly create Sel should be placed
325 * @param tp the type of the dummy entity that should be create
326 * @param env the environment
328 static ir_node *get_dummy_sel(ir_graph *irg, ir_node *block, ir_type *tp, wlk_env *env)
333 /* use a map the check if we already create such an entity */
334 e = pmap_find(env->dummy_map, tp);
338 ir_type *ft = get_irg_frame_type(irg);
342 snprintf(buf, sizeof(buf), "dummy.%u", env->dnr++);
343 ent = new_entity(ft, new_id_from_str(buf), tp);
344 sel = new_r_simpleSel(
350 pmap_insert(env->dummy_map, tp, sel);
352 if (get_type_state(ft) == layout_fixed) {
353 /* Fix the layout again */
354 assert(0 && "Fixed layout not implemented");
361 * Add the hidden parameter from the CopyB node to the Call node.
363 * @param irg the graph
364 * @param n_com number of compound results (will be number of hidden parameters)
365 * @param ins in array to store the hidden parameters into
366 * @param entry the call list
367 * @param env the environment
369 static void add_hidden_param(ir_graph *irg, int n_com, ir_node **ins, cl_entry *entry, wlk_env *env)
371 ir_node *p, *n, *src, *mem;
377 for (p = entry->copyb; p; p = n) {
379 src = get_CopyB_src(p);
381 /* old scheme using value_res_ent */
383 ent = get_Sel_entity(src);
384 owner = get_entity_owner(ent);
386 /* find the hidden parameter index */
387 for (idx = 0; idx < get_struct_n_members(owner); ++idx)
388 if (get_struct_member(owner, idx) == ent)
390 assert(idx < get_struct_n_members(owner));
394 /* new scheme: compound returns are determined by the call type and are Proj's */
395 idx = get_Proj_proj(src);
397 ins[idx] = get_CopyB_dst(p);
398 mem = get_CopyB_mem(p);
400 /* get rid of the CopyB */
401 turn_into_tuple(p, pn_CopyB_max);
402 set_Tuple_pred(p, pn_CopyB_M_regular, mem);
403 set_Tuple_pred(p, pn_CopyB_M_except, get_irg_bad(irg));
404 set_Tuple_pred(p, pn_CopyB_X_except, get_irg_bad(irg));
408 /* now create dummy entities for function with ignored return value */
409 if (n_args < n_com) {
410 ir_type *ctp = get_Call_type(entry->call);
413 if (is_lowered_type(ctp))
414 ctp = get_associated_type(ctp);
416 for (j = i = 0; i < get_method_n_ress(ctp); ++i) {
417 ir_type *rtp = get_method_res_type(ctp, i);
418 if (is_compound_type(rtp)) {
420 ins[j] = get_dummy_sel(irg, get_nodes_block(entry->call), rtp, env);
428 * Fix all calls on a call list by adding hidden parameters.
430 * @param lp the parameter struct
431 * @param irg the graph
432 * @param env the environment
434 static void fix_call_list(const lower_params_t *lp, ir_graph *irg, wlk_env *env) {
436 ir_node *call, **new_in;
437 ir_type *ctp, *lowered_mtp;
438 add_hidden hidden_params;
439 int i, n_params, n_com, pos;
442 new_in = NEW_ARR_F(ir_node *, 0);
443 for (p = env->cl_list; p; p = p->next) {
445 ctp = get_Call_type(call);
446 lowered_mtp = create_modified_mtd_type(lp, ctp);
447 set_Call_type(call, lowered_mtp);
449 hidden_params = lp->hidden_params;
450 if (hidden_params == ADD_HIDDEN_SMART &&
451 get_method_variadicity(ctp) == variadicity_variadic)
452 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
454 n_params = get_Call_n_params(call);
457 for (i = get_method_n_ress(ctp) - 1; i >= 0; --i) {
458 if (is_compound_type(get_method_res_type(ctp, i)))
462 ARR_RESIZE(ir_node *, new_in, n_params + n_com + pos);
463 memset(new_in, 0, sizeof(*new_in) * (n_params + n_com + pos));
464 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
465 add_hidden_param(irg, n_com, &new_in[pos], p, env);
468 /* copy all other parameters */
469 for (i = 0; i < n_params; ++i)
470 new_in[pos++] = get_Call_param(call, i);
471 if (hidden_params == ADD_HIDDEN_ALWAYS_LAST) {
472 add_hidden_param(irg, n_com, &new_in[pos], p, env);
475 new_in[0] = get_Call_mem(call);
476 new_in[1] = get_Call_ptr(call);
478 set_irn_in(call, n_params + n_com + 2, new_in);
483 * Transform a graph. If it has compound parameter returns,
484 * remove them and use the hidden parameter instead.
485 * If it calls methods with compound parameter returns, add hidden
488 * @param lp parameter struct
489 * @param irg the graph to transform
491 static void transform_irg(const lower_params_t *lp, ir_graph *irg)
493 entity *ent = get_irg_entity(irg);
494 ir_type *mtp, *lowered_mtp, *res_tp, *ft;
495 int i, j, k, n_ress, n_com, n_cr_opt;
496 ir_node **new_in, *ret, *endbl, *bl, *mem, *copy;
499 add_hidden hidden_params;
501 assert(ent && "Cannot tranform graph without an entity");
502 assert(get_irg_phase_state(irg) == phase_high && "call lowering must be done in phase high");
504 mtp = get_entity_type(ent);
505 n_ress = get_method_n_ress(mtp);
506 for (n_com = i = 0; i < n_ress; ++i) {
507 res_tp = get_method_res_type(mtp, i);
509 if (is_compound_type(res_tp))
514 /* much easier if we have only one return */
515 normalize_one_return(irg);
517 /* This graph has a compound argument. Create a new type */
518 lowered_mtp = create_modified_mtd_type(lp, mtp);
519 set_entity_type(ent, lowered_mtp);
521 hidden_params = lp->hidden_params;
522 if (hidden_params == ADD_HIDDEN_SMART &&
523 get_method_variadicity(mtp) == variadicity_variadic)
524 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
526 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
527 /* hidden arguments are added first */
528 env.arg_shift = n_com;
529 env.first_hidden = 0;
532 /* hidden arguments are added last */
534 env.first_hidden = get_method_n_params(mtp);
538 /* we must only search for calls */
541 obstack_init(&env.obst);
543 env.dummy_map = pmap_create_ex(8);
546 /* scan the code, fix argument numbers and collect calls. */
547 irg_walk_graph(irg, firm_clear_link, fix_args_and_collect_calls, &env);
551 fix_call_list(lp, irg, &env);
555 * Now fix the Return node of the current graph.
558 /* STEP 1: find the return. This is simple, we have normalized the graph. */
559 endbl = get_irg_end_block(irg);
561 for (i = get_Block_n_cfgpreds(endbl) - 1; i >= 0; --i) {
562 ir_node *pred = get_Block_cfgpred(endbl, i);
564 if (is_Return(pred)) {
569 /* there should always be a return */
573 * STEP 2: fix it. For all compound return values add a CopyB,
574 * all others are copied.
576 NEW_ARR_A(ir_node *, new_in, n_ress - n_com + 1);
578 bl = get_nodes_block(ret);
579 mem = get_Return_mem(ret);
581 ft = get_irg_frame_type(irg);
582 NEW_ARR_A(cr_pair, cr_opt, n_com);
584 for (j = 1, i = k = 0; i < n_ress; ++i) {
585 ir_node *pred = get_Return_res(ret, i);
586 res_tp = get_method_res_type(mtp, i);
588 if (is_compound_type(res_tp)) {
589 ir_node *arg = get_irg_args(irg);
590 arg = new_r_Proj(irg, get_nodes_block(arg), arg, mode_P_data, env.first_hidden + k);
593 if (is_compound_address(ft, pred)) {
594 /* we can do the copy-return optimization here */
595 cr_opt[n_cr_opt].ent = get_Sel_entity(pred);
596 cr_opt[n_cr_opt].arg = arg;
599 else { /* copy-return optimization is impossible, do the copy. */
607 mem = new_r_Proj(irg, bl, copy, mode_M, pn_CopyB_M_regular);
610 else { /* skalar return value */
616 set_irn_in(ret, j, new_in);
619 irg_walk_graph(irg, NULL, do_copy_return_opt, cr_opt);
621 for (i = ARR_LEN(cr_opt) - 1; i >= 0; --i) {
622 remove_class_member(ft, cr_opt[i].ent);
627 pmap_destroy(env.dummy_map);
628 obstack_free(&env.obst, NULL);
632 * Returns non-zero if the given type is a method
633 * type that must be lowered.
635 * @param tp The type.
637 static int must_be_lowered(ir_type *tp) {
641 if (is_Method_type(tp)) {
642 n_ress = get_method_n_ress(tp);
643 for (i = 0; i < n_ress; ++i) {
644 res_tp = get_method_res_type(tp, i);
646 if (is_compound_type(res_tp))
654 * type-walker: lower all method types of entities
655 * and points-to types.
657 static void lower_method_types(type_or_ent *tore, void *env)
659 const lower_params_t *lp = env;
662 /* fix method entities */
663 if (is_entity(tore)) {
664 entity *ent = (entity *)tore;
665 tp = get_entity_type(ent);
667 if (must_be_lowered(tp)) {
668 tp = create_modified_mtd_type(lp, tp);
669 set_entity_type(ent, tp);
673 tp = (ir_type *)tore;
675 /* fix pointer to methods */
676 if (is_Pointer_type(tp)) {
677 ir_type *etp = get_pointer_points_to_type(tp);
678 if (must_be_lowered(etp)) {
679 etp = create_modified_mtd_type(lp, etp);
680 set_pointer_points_to_type(tp, etp);
687 * Lower calls with compound return types.
688 * This function does the following transformations:
690 * - Adds a new (hidden) pointer parameter for
691 * any return compound type.
693 * - Use of the hidden parameters in the function code.
695 * - Change all calls to functions with compound return
696 * by providing space for the hidden parameter on the callers
699 * - Replace a possible block copy after the function call.
701 void lower_compound_ret_calls(const lower_params_t *params)
705 lower_params_t param = *params;
707 if (param.find_pointer_type == NULL) {
708 param.find_pointer_type = def_find_pointer_type;
709 type_map = pmap_create_ex(8);
714 /* first step: Transform all graphs */
715 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
716 irg = get_irp_irg(i);
718 if (irg == get_const_code_irg())
721 transform_irg(¶m, irg);
724 /* second step: Lower all method types of visible entities */
725 type_walk(NULL, lower_method_types, ¶m);
728 pmap_destroy(type_map);