2 * Copyright (C) 1995-2008 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;
91 if (is_lowered_type(mtp)) {
92 /* the type is already lowered. Not handled yet. */
93 assert(0 && "lowered types NYI");
96 lowered = get_associated_type(mtp);
100 n_ress = get_method_n_ress(mtp);
101 NEW_ARR_A(ir_type *, results, n_ress);
103 n_params = get_method_n_params(mtp);
104 NEW_ARR_A(ir_type *, params, n_params + n_ress);
106 first_variadic = get_method_first_variadic_param_index(mtp);
108 hidden_params = lp->hidden_params;
109 if (hidden_params == ADD_HIDDEN_SMART &&
110 get_method_variadicity(mtp) == variadicity_variadic)
111 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
113 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
114 /* add hidden in front */
115 for (nn_ress = nn_params = i = 0; i < n_ress; ++i) {
116 res_tp = get_method_res_type(mtp, i);
118 if (is_compound_type(res_tp)) {
121 if (lp->flags & LF_SMALL_CMP_IN_REGS)
122 n_regs = lp->ret_compound_in_regs(res_tp, modes);
125 /* this compound will be returned solely in registers */
129 /* this compound will be allocated on callers stack and its
130 address will be transmitted as a hidden parameter. */
131 ptr_tp = lp->find_pointer_type(res_tp, get_modeP_data(), lp->def_ptr_alignment);
132 params[nn_params++] = ptr_tp;
134 if (lp->flags & LF_RETURN_HIDDEN)
135 results[nn_ress++] = ptr_tp;
139 results[nn_ress++] = res_tp;
142 /* move the index of the first variadic parameter */
143 first_variadic += nn_params;
145 for (i = 0; i < n_params; ++i)
146 params[nn_params++] = get_method_param_type(mtp, i);
149 /* add hidden parameters last */
150 assert(get_method_variadicity(mtp) == variadicity_non_variadic &&
151 "Cannot add hidden parameters at end of variadic function");
153 for (nn_params = 0; nn_params < n_params; ++nn_params)
154 params[nn_params] = get_method_param_type(mtp, nn_params);
156 for (nn_ress = i = 0; i < n_ress; ++i) {
157 res_tp = get_method_res_type(mtp, i);
159 if (is_compound_type(res_tp))
160 params[nn_params++] = lp->find_pointer_type(res_tp, get_modeP_data(), lp->def_ptr_alignment);
162 results[nn_ress++] = res_tp;
166 /* create the new type */
167 id = mangle_u(new_id_from_chars("L", 1), get_type_ident(mtp));
168 lowered = new_d_type_method(id, nn_params, nn_ress, get_type_dbg_info(mtp));
171 for (i = 0; i < nn_params; ++i)
172 set_method_param_type(lowered, i, params[i]);
173 for (i = 0; i < nn_ress; ++i)
174 set_method_res_type(lowered, i, results[i]);
176 var = get_method_variadicity(mtp);
177 set_method_variadicity(lowered, var);
178 if (var == variadicity_variadic)
179 set_method_first_variadic_param_index(lowered, first_variadic);
181 /* associate the lowered type with the original one for easier access */
183 set_method_calling_convention(lowered, get_method_calling_convention(mtp) | cc_compound_ret);
185 set_lowered_type(mtp, lowered);
193 typedef struct cl_entry cl_entry;
195 cl_entry *next; /**< Pointer to the next entry. */
196 ir_node *call; /**< Pointer to the Call node. */
197 ir_node *copyb; /**< List of all CopyB nodes. */
201 * Walker environment for fix_args_and_collect_calls().
203 typedef struct _wlk_env_t {
204 int arg_shift; /**< The Argument index shift for parameters. */
205 int first_hidden; /**< The index of the first hidden argument. */
206 struct obstack obst; /**< An obstack to allocate the data on. */
207 cl_entry *cl_list; /**< The call list. */
208 pmap *dummy_map; /**< A map for finding the dummy arguments. */
209 unsigned dnr; /**< The dummy index number. */
210 const lower_params_t *params; /**< Lowering parameters. */
211 unsigned non_local_mem:1; /**< Set if non-local memory access was found. */
212 unsigned changed:1; /**< Set if the current graph was changed. */
216 * Return the call list entry of a call node.
217 * If no entry exists yet, allocate one and enter the node into
218 * the call list of the environment.
220 * @param call A Call node.
221 * @param env The environment.
223 static cl_entry *get_Call_entry(ir_node *call, wlk_env *env) {
224 cl_entry *res = get_irn_link(call);
226 cl_entry *res = obstack_alloc(&env->obst, sizeof(*res));
227 res->next = env->cl_list;
230 set_irn_link(call, res);
237 * Post walker: shift all parameter indeces
238 * and collect Calls with compound returns in the call list.
240 static void fix_args_and_collect_calls(ir_node *n, void *ctx) {
244 ir_opcode code = get_irn_opcode(n);
246 if (code == iro_Load || code == iro_Store) {
247 ir_node *ptr = get_irn_n(n, 1);
250 env->non_local_mem = 1;
251 else if (get_Sel_ptr(ptr) != get_irg_frame(current_ir_graph))
252 env->non_local_mem = 1;
253 } else if (env->arg_shift > 0 && code == iro_Proj) {
254 ir_node *pred = get_Proj_pred(n);
256 /* Fix the argument numbers */
257 if (pred == get_irg_args(current_ir_graph)) {
258 long pnr = get_Proj_proj(n);
259 set_Proj_proj(n, pnr + env->arg_shift);
262 } else if (code == iro_Call) {
263 ctp = get_Call_type(n);
264 if (env->params->flags & LF_COMPOUND_RETURN) {
265 /* check for compound returns */
266 for (i = get_method_n_ress(ctp) -1; i >= 0; --i) {
267 if (is_compound_type(get_method_res_type(ctp, i))) {
269 * This is a call with a compound return. As the result
270 * might be ignored, we must put it in the list.
272 (void)get_Call_entry(n, env);
277 } else if (code == iro_CopyB && env->params->flags & LF_COMPOUND_RETURN) {
278 /* check for compound returns */
279 ir_node *src = get_CopyB_src(n);
280 /* older scheme using value_res_ent */
282 ir_node *proj = get_Sel_ptr(src);
283 if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_P_value_res_base) {
284 ir_node *call = get_Proj_pred(proj);
286 /* found a CopyB from compound Call result */
287 cl_entry *e = get_Call_entry(call, env);
288 set_irn_link(n, e->copyb);
293 /* new scheme: compound results are determined by the call type only */
295 ir_node *proj = get_Proj_pred(src);
296 if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_T_result) {
297 ir_node *call = get_Proj_pred(proj);
299 ctp = get_Call_type(call);
300 if (is_compound_type(get_method_res_type(ctp, get_Proj_proj(src)))) {
301 /* found a CopyB from compound Call result */
302 cl_entry *e = get_Call_entry(call, env);
303 set_irn_link(n, e->copyb);
314 * Returns non-zero if a node is a compound address
315 * of a frame-type entity.
317 * @param ft the frame type
318 * @param adr the node
320 static int is_compound_address(ir_type *ft, ir_node *adr)
326 if (get_Sel_n_indexs(adr) != 0)
328 ent = get_Sel_entity(adr);
329 return get_entity_owner(ent) == ft;
332 /** A pair for the copy-return-optimization. */
333 typedef struct cr_pair {
334 ir_entity *ent; /**< the entity than can be removed from the frame */
335 ir_node *arg; /**< the argument that replaces the entities address */
339 * Post walker: fixes all entities addresses for the copy-return
342 * Note: We expect the length of the cr_pair array (ie number of compound
343 * return values) to be 1 (C, C++) in almost all cases, so ignore the
344 * linear search complexity here.
346 static void do_copy_return_opt(ir_node *n, void *ctx) {
351 ir_entity *ent = get_Sel_entity(n);
353 for (i = ARR_LEN(arr) - 1; i >= 0; --i) {
354 if (ent == arr[i].ent) {
355 exchange(n, arr[i].arg);
363 * Return a Sel node that selects a dummy argument of type tp.
364 * Dummy arguments are only needed once and we use a map
366 * We could even assign all dummy arguments the same offset
367 * in the frame type ...
369 * @param irg the graph
370 * @param block the block where a newly create Sel should be placed
371 * @param tp the type of the dummy entity that should be create
372 * @param env the environment
374 static ir_node *get_dummy_sel(ir_graph *irg, ir_node *block, ir_type *tp, wlk_env *env)
379 /* use a map the check if we already create such an entity */
380 e = pmap_find(env->dummy_map, tp);
384 ir_type *ft = get_irg_frame_type(irg);
387 snprintf(buf, sizeof(buf), "dummy.%u", env->dnr++);
388 ent = new_entity(ft, new_id_from_str(buf), tp);
389 pmap_insert(env->dummy_map, tp, ent);
391 if (get_type_state(ft) == layout_fixed) {
392 /* Fix the layout again */
393 assert(0 && "Fixed layout not implemented");
396 return new_r_simpleSel(
405 * Add the hidden parameter from the CopyB node to the Call node.
407 * @param irg the graph
408 * @param n_com number of compound results (will be number of hidden parameters)
409 * @param ins in array to store the hidden parameters into
410 * @param entry the call list
411 * @param env the environment
413 static void add_hidden_param(ir_graph *irg, int n_com, ir_node **ins, cl_entry *entry, wlk_env *env)
415 ir_node *p, *n, *src, *mem, *blk;
421 for (p = entry->copyb; p; p = n) {
423 src = get_CopyB_src(p);
425 /* old scheme using value_res_ent */
427 ent = get_Sel_entity(src);
428 owner = get_entity_owner(ent);
430 /* find the hidden parameter index */
431 for (idx = 0; idx < get_struct_n_members(owner); ++idx)
432 if (get_struct_member(owner, idx) == ent)
434 assert(idx < get_struct_n_members(owner));
436 /* new scheme: compound returns are determined by the call type and are Proj's */
437 idx = get_Proj_proj(src);
440 ins[idx] = get_CopyB_dst(p);
441 mem = get_CopyB_mem(p);
442 blk = get_nodes_block(p);
444 /* get rid of the CopyB */
445 turn_into_tuple(p, pn_CopyB_max);
446 set_Tuple_pred(p, pn_CopyB_M_regular, mem);
447 set_Tuple_pred(p, pn_CopyB_M_except, get_irg_bad(irg));
448 set_Tuple_pred(p, pn_CopyB_X_regular, new_r_Jmp(irg, blk));
449 set_Tuple_pred(p, pn_CopyB_X_except, get_irg_bad(irg));
453 /* now create dummy entities for function with ignored return value */
454 if (n_args < n_com) {
455 ir_type *ctp = get_Call_type(entry->call);
458 if (is_lowered_type(ctp))
459 ctp = get_associated_type(ctp);
461 for (j = i = 0; i < get_method_n_ress(ctp); ++i) {
462 ir_type *rtp = get_method_res_type(ctp, i);
463 if (is_compound_type(rtp)) {
465 ins[j] = get_dummy_sel(irg, get_nodes_block(entry->call), rtp, env);
473 * Fix all calls on a call list by adding hidden parameters.
475 * @param irg the graph
476 * @param env the environment
478 static void fix_call_list(ir_graph *irg, wlk_env *env) {
479 const lower_params_t *lp = env->params;
481 ir_node *call, **new_in;
482 ir_type *ctp, *lowered_mtp;
483 add_hidden hidden_params;
484 int i, n_params, n_com, pos;
486 new_in = NEW_ARR_F(ir_node *, 0);
487 for (p = env->cl_list; p; p = p->next) {
489 ctp = get_Call_type(call);
490 lowered_mtp = create_modified_mtd_type(lp, ctp);
491 set_Call_type(call, lowered_mtp);
493 hidden_params = lp->hidden_params;
494 if (hidden_params == ADD_HIDDEN_SMART &&
495 get_method_variadicity(ctp) == variadicity_variadic)
496 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
498 n_params = get_Call_n_params(call);
501 for (i = get_method_n_ress(ctp) - 1; i >= 0; --i) {
502 if (is_compound_type(get_method_res_type(ctp, i)))
506 ARR_RESIZE(ir_node *, new_in, n_params + n_com + pos);
507 memset(new_in, 0, sizeof(*new_in) * (n_params + n_com + pos));
508 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
509 add_hidden_param(irg, n_com, &new_in[pos], p, env);
512 /* copy all other parameters */
513 for (i = 0; i < n_params; ++i)
514 new_in[pos++] = get_Call_param(call, i);
515 if (hidden_params == ADD_HIDDEN_ALWAYS_LAST) {
516 add_hidden_param(irg, n_com, &new_in[pos], p, env);
519 new_in[0] = get_Call_mem(call);
520 new_in[1] = get_Call_ptr(call);
522 set_irn_in(call, n_params + n_com + 2, new_in);
527 * Transform a graph. If it has compound parameter returns,
528 * remove them and use the hidden parameter instead.
529 * If it calls methods with compound parameter returns, add hidden
532 * @param lp parameter struct
533 * @param irg the graph to transform
535 static void transform_irg(const lower_params_t *lp, ir_graph *irg)
537 ir_graph * rem = current_ir_graph;
538 ir_entity *ent = get_irg_entity(irg);
539 ir_type *mtp, *lowered_mtp, *tp, *ft;
540 int i, j, k, n_ress = 0, n_ret_com = 0, n_cr_opt;
541 ir_node **new_in, *ret, *endbl, *bl, *mem, *copy;
544 add_hidden hidden_params;
546 current_ir_graph = irg;
548 assert(ent && "Cannot transform graph without an entity");
549 assert(get_irg_phase_state(irg) == phase_high && "call lowering must be done in phase high");
551 mtp = get_entity_type(ent);
553 if (lp->flags & LF_COMPOUND_RETURN) {
554 /* calculate the number of compound returns */
555 n_ress = get_method_n_ress(mtp);
556 for (n_ret_com = i = 0; i < n_ress; ++i) {
557 tp = get_method_res_type(mtp, i);
559 if (is_compound_type(tp))
565 /* much easier if we have only one return */
566 normalize_one_return(irg);
568 /* This graph has a compound argument. Create a new type */
569 lowered_mtp = create_modified_mtd_type(lp, mtp);
570 set_entity_type(ent, lowered_mtp);
572 hidden_params = lp->hidden_params;
573 if (hidden_params == ADD_HIDDEN_SMART &&
574 get_method_variadicity(mtp) == variadicity_variadic)
575 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
577 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
578 /* hidden arguments are added first */
579 env.arg_shift = n_ret_com;
580 env.first_hidden = 0;
582 /* hidden arguments are added last */
584 env.first_hidden = get_method_n_params(mtp);
587 /* we must only search for calls */
590 obstack_init(&env.obst);
592 env.dummy_map = pmap_create_ex(8);
595 env.non_local_mem = 0;
598 /* scan the code, fix argument numbers and collect calls. */
599 irg_walk_graph(irg, firm_clear_link, fix_args_and_collect_calls, &env);
603 fix_call_list(irg, &env);
609 * Now fix the Return node of the current graph.
613 /* STEP 1: find the return. This is simple, we have normalized the graph. */
614 endbl = get_irg_end_block(irg);
616 for (i = get_Block_n_cfgpreds(endbl) - 1; i >= 0; --i) {
617 ir_node *pred = get_Block_cfgpred(endbl, i);
619 if (is_Return(pred)) {
624 /* there should always be a return */
628 * STEP 2: fix it. For all compound return values add a CopyB,
629 * all others are copied.
631 NEW_ARR_A(ir_node *, new_in, n_ress + 1);
633 bl = get_nodes_block(ret);
634 mem = get_Return_mem(ret);
636 ft = get_irg_frame_type(irg);
637 NEW_ARR_A(cr_pair, cr_opt, n_ret_com);
639 for (j = 1, i = k = 0; i < n_ress; ++i) {
640 ir_node *pred = get_Return_res(ret, i);
641 tp = get_method_res_type(mtp, i);
643 if (is_compound_type(tp)) {
644 ir_node *arg = get_irg_args(irg);
645 arg = new_r_Proj(irg, get_nodes_block(arg), arg, mode_P_data, env.first_hidden + k);
648 if (is_Unknown(pred)) {
649 /* The Return(Unknown) is the Firm construct for a missing return.
653 * Sorrily detecting that copy-return is possible isn't that simple.
654 * We must check, that the hidden address is alias free during the whole
656 * A simple heuristic: all Loads/Stores inside
657 * the function access only local frame.
659 if (!env.non_local_mem && is_compound_address(ft, pred)) {
660 /* we can do the copy-return optimization here */
661 cr_opt[n_cr_opt].ent = get_Sel_entity(pred);
662 cr_opt[n_cr_opt].arg = arg;
664 } else { /* copy-return optimization is impossible, do the copy. */
672 mem = new_r_Proj(irg, bl, copy, mode_M, pn_CopyB_M_regular);
675 if (lp->flags & LF_RETURN_HIDDEN) {
679 } else { /* scalar return value */
684 /* replace the in of the Return */
686 set_irn_in(ret, j, new_in);
689 irg_walk_graph(irg, NULL, do_copy_return_opt, cr_opt);
691 for (i = ARR_LEN(cr_opt) - 1; i >= 0; --i) {
692 remove_class_member(ft, cr_opt[i].ent);
695 } /* if (n_ret_com) */
697 pmap_destroy(env.dummy_map);
698 obstack_free(&env.obst, NULL);
701 /* invalidate the analysis info */
702 set_irg_outs_inconsistent(irg);
703 set_irg_loopinfo_state(irg, loopinfo_inconsistent);
705 current_ir_graph = rem;
709 * Returns non-zero if the given type is a method
710 * type that must be lowered.
712 * @param lp lowering parameters
713 * @param tp The type.
715 static int must_be_lowered(const lower_params_t *lp, ir_type *tp) {
719 if (is_Method_type(tp)) {
720 if (lp->flags & LF_COMPOUND_RETURN) {
721 /* check for compound returns */
722 n_ress = get_method_n_ress(tp);
723 for (i = 0; i < n_ress; ++i) {
724 res_tp = get_method_res_type(tp, i);
726 if (is_compound_type(res_tp))
735 * type-walker: lower all method types of entities
736 * and points-to types.
738 static void lower_method_types(type_or_ent *tore, void *env)
740 const lower_params_t *lp = env;
743 /* fix method entities */
744 if (is_entity(tore)) {
745 ir_entity *ent = (ir_entity *)tore;
746 tp = get_entity_type(ent);
748 if (must_be_lowered(lp, tp)) {
749 tp = create_modified_mtd_type(lp, tp);
750 set_entity_type(ent, tp);
753 tp = (ir_type *)tore;
755 /* fix pointer to methods */
756 if (is_Pointer_type(tp)) {
757 ir_type *etp = get_pointer_points_to_type(tp);
758 if (must_be_lowered(lp, etp)) {
759 etp = create_modified_mtd_type(lp, etp);
760 set_pointer_points_to_type(tp, etp);
767 * Lower calls with compound parameters and return types.
768 * This function does the following transformations:
770 * - Adds a new (hidden) pointer parameter for
771 * any return compound type.
773 * - Use of the hidden parameters in the function code.
775 * - Change all calls to functions with compound return
776 * by providing space for the hidden parameter on the callers
779 * - Replace a possible block copy after the function call.
781 void lower_calls_with_compounds(const lower_params_t *params)
785 lower_params_t param = *params;
787 if (param.find_pointer_type == NULL) {
788 param.find_pointer_type = def_find_pointer_type;
789 type_map = pmap_create_ex(8);
793 /* first step: Transform all graphs */
794 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
795 irg = get_irp_irg(i);
797 if (irg == get_const_code_irg())
800 transform_irg(¶m, irg);
803 /* second step: Lower all method types of visible entities */
804 type_walk(NULL, lower_method_types, ¶m);
807 pmap_destroy(type_map);