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
41 #include "iroptimize.h"
47 /** A type map for def_find_pointer_type. */
48 static pmap *type_map;
51 * Default implementation for finding a pointer type for a given element type.
52 * Simple create a new one.
54 static ir_type *def_find_pointer_type(ir_type *e_type, ir_mode *mode, int alignment)
59 /* Mode and alignment are always identical in all calls to def_find_pointer_type(), so
60 we simply can use a map from the element type to the pointer type. */
61 e = pmap_find(type_map, e_type);
65 res = new_type_pointer(mangle_u(get_type_ident(e_type), new_id_from_chars("Ptr", 3)), e_type, mode);
66 set_type_alignment_bytes(res, alignment);
67 pmap_insert(type_map, e_type, res);
73 * Creates a new lowered type for a method type with compound
74 * arguments. The new type is associated to the old one and returned.
76 * @param lp parameter struct
77 * @param mtp the method type to lower
79 * The current implementation expects that a lowered type already
80 * includes the necessary changes ...
82 static ir_type *create_modified_mtd_type(const lower_params_t *lp, ir_type *mtp)
84 ir_type *lowered, *ptr_tp;
85 ir_type **params, **results, *res_tp;
86 ir_mode *modes[MAX_REGISTER_RET_VAL];
87 int n_ress, n_params, nn_ress, nn_params, i, first_variadic;
89 add_hidden hidden_params;
93 if (is_lowered_type(mtp)) {
94 /* the type is already lowered. Not handled yet. */
95 assert(0 && "lowered types NYI");
98 lowered = get_associated_type(mtp);
102 n_ress = get_method_n_ress(mtp);
103 NEW_ARR_A(ir_type *, results, n_ress);
105 n_params = get_method_n_params(mtp);
106 NEW_ARR_A(ir_type *, params, n_params + n_ress);
108 first_variadic = get_method_first_variadic_param_index(mtp);
110 hidden_params = lp->hidden_params;
111 if (hidden_params == ADD_HIDDEN_SMART &&
112 get_method_variadicity(mtp) == variadicity_variadic)
113 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
115 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
116 /* add hidden in front */
117 for (nn_ress = nn_params = i = 0; i < n_ress; ++i) {
118 res_tp = get_method_res_type(mtp, i);
120 if (is_compound_type(res_tp)) {
123 if (lp->flags & LF_SMALL_CMP_IN_REGS)
124 n_regs = lp->ret_compound_in_regs(res_tp, modes);
127 /* this compound will be returned solely in registers */
128 panic("Returning compounds in registers not yet implemented");
131 /* this compound will be allocated on callers stack and its
132 address will be transmitted as a hidden parameter. */
133 ptr_tp = lp->find_pointer_type(res_tp, get_modeP_data(), lp->def_ptr_alignment);
134 params[nn_params++] = ptr_tp;
136 if (lp->flags & LF_RETURN_HIDDEN)
137 results[nn_ress++] = ptr_tp;
141 results[nn_ress++] = res_tp;
144 /* move the index of the first variadic parameter */
145 first_variadic += nn_params;
147 for (i = 0; i < n_params; ++i)
148 params[nn_params++] = get_method_param_type(mtp, i);
151 /* add hidden parameters last */
152 assert(get_method_variadicity(mtp) == variadicity_non_variadic &&
153 "Cannot add hidden parameters at end of variadic function");
155 for (nn_params = 0; nn_params < n_params; ++nn_params)
156 params[nn_params] = get_method_param_type(mtp, nn_params);
158 for (nn_ress = i = 0; i < n_ress; ++i) {
159 res_tp = get_method_res_type(mtp, i);
161 if (is_compound_type(res_tp))
162 params[nn_params++] = lp->find_pointer_type(res_tp, get_modeP_data(), lp->def_ptr_alignment);
164 results[nn_ress++] = res_tp;
168 /* create the new type */
169 id = mangle_u(new_id_from_chars("L", 1), get_type_ident(mtp));
170 lowered = new_d_type_method(id, nn_params, nn_ress, get_type_dbg_info(mtp));
173 for (i = 0; i < nn_params; ++i)
174 set_method_param_type(lowered, i, params[i]);
175 for (i = 0; i < nn_ress; ++i)
176 set_method_res_type(lowered, i, results[i]);
178 var = get_method_variadicity(mtp);
179 set_method_variadicity(lowered, var);
180 if (var == variadicity_variadic)
181 set_method_first_variadic_param_index(lowered, first_variadic);
183 /* associate the lowered type with the original one for easier access */
185 set_method_calling_convention(lowered, get_method_calling_convention(mtp) | cc_compound_ret);
187 set_lowered_type(mtp, lowered);
195 typedef struct cl_entry cl_entry;
197 cl_entry *next; /**< Pointer to the next entry. */
198 ir_node *call; /**< Pointer to the Call node. */
199 ir_node *copyb; /**< List of all CopyB nodes. */
203 * Walker environment for fix_args_and_collect_calls().
205 typedef struct _wlk_env_t {
206 int arg_shift; /**< The Argument index shift for parameters. */
207 int first_hidden; /**< The index of the first hidden argument. */
208 struct obstack obst; /**< An obstack to allocate the data on. */
209 cl_entry *cl_list; /**< The call list. */
210 pmap *dummy_map; /**< A map for finding the dummy arguments. */
211 unsigned dnr; /**< The dummy index number. */
212 const lower_params_t *params; /**< Lowering parameters. */
213 unsigned only_local_mem:1; /**< Set if only local memory access was found. */
214 unsigned changed:1; /**< Set if the current graph was changed. */
218 * Return the call list entry of a call node.
219 * If no entry exists yet, allocate one and enter the node into
220 * the call list of the environment.
222 * @param call A Call node.
223 * @param env The environment.
225 static cl_entry *get_Call_entry(ir_node *call, wlk_env *env) {
226 cl_entry *res = get_irn_link(call);
228 cl_entry *res = obstack_alloc(&env->obst, sizeof(*res));
229 res->next = env->cl_list;
232 set_irn_link(call, res);
239 * Finds the base address of an address by skipping Sel's and address
242 * @param adr the address
243 * @param pEnt points to the base entity if any
245 static ir_node *find_base_adr(ir_node *ptr, ir_entity **pEnt) {
246 ir_entity *ent = NULL;
247 assert(mode_is_reference(get_irn_mode(ptr)));
251 ent = get_Sel_entity(ptr);
252 ptr = get_Sel_ptr(ptr);
254 else if (is_Add(ptr)) {
255 ir_node *left = get_Add_left(ptr);
256 if (mode_is_reference(get_irn_mode(left)))
259 ptr = get_Add_right(ptr);
261 } else if (is_Sub(ptr)) {
262 ptr = get_Sub_left(ptr);
272 * Check if a given pointer represents non-local memory.
274 static void check_ptr(ir_node *ptr, wlk_env *env) {
275 ir_storage_class_class_t sc;
278 /* still alias free */
279 ptr = find_base_adr(ptr, &ent);
280 sc = classify_pointer(current_ir_graph, ptr, ent);
281 if (sc != ir_sc_localvar && sc != ir_sc_malloced) {
282 /* non-local memory access */
283 env->only_local_mem = 0;
288 * Post walker: shift all parameter indexes
289 * and collect Calls with compound returns in the call list.
290 * If a non-alias free memory access is found, reset the alias free
293 static void fix_args_and_collect_calls(ir_node *n, void *ctx) {
299 switch (get_irn_opcode(n)) {
302 if (env->only_local_mem) {
303 ptr = get_irn_n(n, 1);
308 if (env->arg_shift > 0) {
309 ir_node *pred = get_Proj_pred(n);
311 /* Fix the argument numbers */
312 if (pred == get_irg_args(current_ir_graph)) {
313 long pnr = get_Proj_proj(n);
314 set_Proj_proj(n, pnr + env->arg_shift);
320 if (! is_self_recursive_Call(n)) {
321 /* any non self recursive call might access global memory */
322 env->only_local_mem = 0;
325 ctp = get_Call_type(n);
326 if (env->params->flags & LF_COMPOUND_RETURN) {
327 /* check for compound returns */
328 for (i = get_method_n_ress(ctp) -1; i >= 0; --i) {
329 if (is_compound_type(get_method_res_type(ctp, i))) {
331 * This is a call with a compound return. As the result
332 * might be ignored, we must put it in the list.
334 (void)get_Call_entry(n, env);
341 if (env->only_local_mem) {
342 check_ptr(get_CopyB_src(n), env);
343 if (env->only_local_mem)
344 check_ptr(get_CopyB_dst(n), env);
346 if (env->params->flags & LF_COMPOUND_RETURN) {
347 /* check for compound returns */
348 ir_node *src = get_CopyB_src(n);
349 /* older scheme using value_res_ent */
351 ir_node *proj = get_Sel_ptr(src);
352 if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_P_value_res_base) {
353 ir_node *call = get_Proj_pred(proj);
355 /* found a CopyB from compound Call result */
356 cl_entry *e = get_Call_entry(call, env);
357 set_irn_link(n, e->copyb);
362 /* new scheme: compound results are determined by the call type only */
364 ir_node *proj = get_Proj_pred(src);
365 if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_T_result) {
366 ir_node *call = get_Proj_pred(proj);
368 ctp = get_Call_type(call);
369 if (is_compound_type(get_method_res_type(ctp, get_Proj_proj(src)))) {
370 /* found a CopyB from compound Call result */
371 cl_entry *e = get_Call_entry(call, env);
372 set_irn_link(n, e->copyb);
388 * Returns non-zero if a node is a compound address
389 * of a frame-type entity.
391 * @param ft the frame type
392 * @param adr the node
394 static int is_compound_address(ir_type *ft, ir_node *adr)
400 if (get_Sel_n_indexs(adr) != 0)
402 ent = get_Sel_entity(adr);
403 return get_entity_owner(ent) == ft;
406 /** A pair for the copy-return-optimization. */
407 typedef struct cr_pair {
408 ir_entity *ent; /**< the entity than can be removed from the frame */
409 ir_node *arg; /**< the argument that replaces the entities address */
413 * Post walker: fixes all entities addresses for the copy-return
416 * Note: We expect the length of the cr_pair array (ie number of compound
417 * return values) to be 1 (C, C++) in almost all cases, so ignore the
418 * linear search complexity here.
420 static void do_copy_return_opt(ir_node *n, void *ctx) {
425 ir_entity *ent = get_Sel_entity(n);
427 for (i = ARR_LEN(arr) - 1; i >= 0; --i) {
428 if (ent == arr[i].ent) {
429 exchange(n, arr[i].arg);
437 * Return a Sel node that selects a dummy argument of type tp.
438 * Dummy arguments are only needed once and we use a map
440 * We could even assign all dummy arguments the same offset
441 * in the frame type ...
443 * @param irg the graph
444 * @param block the block where a newly create Sel should be placed
445 * @param tp the type of the dummy entity that should be create
446 * @param env the environment
448 static ir_node *get_dummy_sel(ir_graph *irg, ir_node *block, ir_type *tp, wlk_env *env)
453 /* use a map the check if we already create such an entity */
454 e = pmap_find(env->dummy_map, tp);
458 ir_type *ft = get_irg_frame_type(irg);
461 snprintf(buf, sizeof(buf), "dummy.%u", env->dnr++);
462 ent = new_entity(ft, new_id_from_str(buf), tp);
463 pmap_insert(env->dummy_map, tp, ent);
465 if (get_type_state(ft) == layout_fixed) {
466 /* Fix the layout again */
467 assert(0 && "Fixed layout not implemented");
470 return new_r_simpleSel(
479 * Add the hidden parameter from the CopyB node to the Call node.
481 * @param irg the graph
482 * @param n_com number of compound results (will be number of hidden parameters)
483 * @param ins in array to store the hidden parameters into
484 * @param entry the call list
485 * @param env the environment
487 static void add_hidden_param(ir_graph *irg, int n_com, ir_node **ins, cl_entry *entry, wlk_env *env)
489 ir_node *p, *n, *src, *mem, *blk;
495 for (p = entry->copyb; p; p = n) {
497 src = get_CopyB_src(p);
499 /* old scheme using value_res_ent */
501 ent = get_Sel_entity(src);
502 owner = get_entity_owner(ent);
504 /* find the hidden parameter index */
505 for (idx = 0; idx < get_struct_n_members(owner); ++idx)
506 if (get_struct_member(owner, idx) == ent)
508 assert(idx < get_struct_n_members(owner));
510 /* new scheme: compound returns are determined by the call type and are Proj's */
511 idx = get_Proj_proj(src);
514 ins[idx] = get_CopyB_dst(p);
515 mem = get_CopyB_mem(p);
516 blk = get_nodes_block(p);
518 /* get rid of the CopyB */
519 turn_into_tuple(p, pn_CopyB_max);
520 set_Tuple_pred(p, pn_CopyB_M_regular, mem);
521 set_Tuple_pred(p, pn_CopyB_M_except, get_irg_bad(irg));
522 set_Tuple_pred(p, pn_CopyB_X_regular, new_r_Jmp(irg, blk));
523 set_Tuple_pred(p, pn_CopyB_X_except, get_irg_bad(irg));
527 /* now create dummy entities for function with ignored return value */
528 if (n_args < n_com) {
529 ir_type *ctp = get_Call_type(entry->call);
532 if (is_lowered_type(ctp))
533 ctp = get_associated_type(ctp);
535 for (j = i = 0; i < get_method_n_ress(ctp); ++i) {
536 ir_type *rtp = get_method_res_type(ctp, i);
537 if (is_compound_type(rtp)) {
539 ins[j] = get_dummy_sel(irg, get_nodes_block(entry->call), rtp, env);
547 * Fix all calls on a call list by adding hidden parameters.
549 * @param irg the graph
550 * @param env the environment
552 static void fix_call_list(ir_graph *irg, wlk_env *env) {
553 const lower_params_t *lp = env->params;
555 ir_node *call, **new_in;
556 ir_type *ctp, *lowered_mtp;
557 add_hidden hidden_params;
558 int i, n_params, n_com, pos;
560 new_in = NEW_ARR_F(ir_node *, 0);
561 for (p = env->cl_list; p; p = p->next) {
563 ctp = get_Call_type(call);
564 lowered_mtp = create_modified_mtd_type(lp, ctp);
565 set_Call_type(call, lowered_mtp);
567 hidden_params = lp->hidden_params;
568 if (hidden_params == ADD_HIDDEN_SMART &&
569 get_method_variadicity(ctp) == variadicity_variadic)
570 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
572 n_params = get_Call_n_params(call);
575 for (i = get_method_n_ress(ctp) - 1; i >= 0; --i) {
576 if (is_compound_type(get_method_res_type(ctp, i)))
580 ARR_RESIZE(ir_node *, new_in, n_params + n_com + pos);
581 memset(new_in, 0, sizeof(*new_in) * (n_params + n_com + pos));
582 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
583 add_hidden_param(irg, n_com, &new_in[pos], p, env);
586 /* copy all other parameters */
587 for (i = 0; i < n_params; ++i)
588 new_in[pos++] = get_Call_param(call, i);
589 if (hidden_params == ADD_HIDDEN_ALWAYS_LAST) {
590 add_hidden_param(irg, n_com, &new_in[pos], p, env);
593 new_in[0] = get_Call_mem(call);
594 new_in[1] = get_Call_ptr(call);
596 set_irn_in(call, n_params + n_com + 2, new_in);
601 * Transform a graph. If it has compound parameter returns,
602 * remove them and use the hidden parameter instead.
603 * If it calls methods with compound parameter returns, add hidden
606 * @param lp parameter struct
607 * @param irg the graph to transform
609 static void transform_irg(const lower_params_t *lp, ir_graph *irg)
611 ir_graph * rem = current_ir_graph;
612 ir_entity *ent = get_irg_entity(irg);
613 ir_type *mtp, *lowered_mtp, *tp, *ft;
614 int i, j, k, n_ress = 0, n_ret_com = 0, n_cr_opt;
615 ir_node **new_in, *ret, *endbl, *bl, *mem, *copy;
618 add_hidden hidden_params;
620 current_ir_graph = irg;
622 assert(ent && "Cannot transform graph without an entity");
623 assert(get_irg_phase_state(irg) == phase_high && "call lowering must be done in phase high");
625 mtp = get_entity_type(ent);
627 if (lp->flags & LF_COMPOUND_RETURN) {
628 /* calculate the number of compound returns */
629 n_ress = get_method_n_ress(mtp);
630 for (n_ret_com = i = 0; i < n_ress; ++i) {
631 tp = get_method_res_type(mtp, i);
633 if (is_compound_type(tp))
639 /* much easier if we have only one return */
640 normalize_one_return(irg);
642 /* This graph has a compound argument. Create a new type */
643 lowered_mtp = create_modified_mtd_type(lp, mtp);
644 set_entity_type(ent, lowered_mtp);
646 hidden_params = lp->hidden_params;
647 if (hidden_params == ADD_HIDDEN_SMART &&
648 get_method_variadicity(mtp) == variadicity_variadic)
649 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
651 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
652 /* hidden arguments are added first */
653 env.arg_shift = n_ret_com;
654 env.first_hidden = 0;
656 /* hidden arguments are added last */
658 env.first_hidden = get_method_n_params(mtp);
661 /* we must only search for calls */
664 obstack_init(&env.obst);
666 env.dummy_map = pmap_create_ex(8);
669 env.only_local_mem = 1;
672 /* scan the code, fix argument numbers and collect calls. */
673 irg_walk_graph(irg, firm_clear_link, fix_args_and_collect_calls, &env);
677 fix_call_list(irg, &env);
683 * Now fix the Return node of the current graph.
687 /* STEP 1: find the return. This is simple, we have normalized the graph. */
688 endbl = get_irg_end_block(irg);
690 for (i = get_Block_n_cfgpreds(endbl) - 1; i >= 0; --i) {
691 ir_node *pred = get_Block_cfgpred(endbl, i);
693 if (is_Return(pred)) {
698 /* there should always be a return */
702 * STEP 2: fix it. For all compound return values add a CopyB,
703 * all others are copied.
705 NEW_ARR_A(ir_node *, new_in, n_ress + 1);
707 bl = get_nodes_block(ret);
708 mem = get_Return_mem(ret);
710 ft = get_irg_frame_type(irg);
711 NEW_ARR_A(cr_pair, cr_opt, n_ret_com);
713 for (j = 1, i = k = 0; i < n_ress; ++i) {
714 ir_node *pred = get_Return_res(ret, i);
715 tp = get_method_res_type(mtp, i);
717 if (is_compound_type(tp)) {
718 ir_node *arg = get_irg_args(irg);
719 arg = new_r_Proj(irg, get_nodes_block(arg), arg, mode_P_data, env.first_hidden + k);
722 if (is_Unknown(pred)) {
723 /* The Return(Unknown) is the Firm construct for a missing return.
727 * Sorrily detecting that copy-return is possible isn't that simple.
728 * We must check, that the hidden address is alias free during the whole
730 * A simple heuristic: all Loads/Stores inside
731 * the function access only local frame.
733 if (env.only_local_mem && is_compound_address(ft, pred)) {
734 /* we can do the copy-return optimization here */
735 cr_opt[n_cr_opt].ent = get_Sel_entity(pred);
736 cr_opt[n_cr_opt].arg = arg;
738 } else { /* copy-return optimization is impossible, do the copy. */
746 mem = new_r_Proj(irg, bl, copy, mode_M, pn_CopyB_M_regular);
749 if (lp->flags & LF_RETURN_HIDDEN) {
753 } else { /* scalar return value */
758 /* replace the in of the Return */
760 set_irn_in(ret, j, new_in);
763 irg_walk_graph(irg, NULL, do_copy_return_opt, cr_opt);
765 for (i = ARR_LEN(cr_opt) - 1; i >= 0; --i) {
766 remove_class_member(ft, cr_opt[i].ent);
769 } /* if (n_ret_com) */
771 pmap_destroy(env.dummy_map);
772 obstack_free(&env.obst, NULL);
775 /* invalidate the analysis info */
776 set_irg_outs_inconsistent(irg);
777 set_irg_loopinfo_state(irg, loopinfo_inconsistent);
779 current_ir_graph = rem;
783 * Returns non-zero if the given type is a method
784 * type that must be lowered.
786 * @param lp lowering parameters
787 * @param tp The type.
789 static int must_be_lowered(const lower_params_t *lp, ir_type *tp) {
793 if (is_Method_type(tp)) {
794 if (lp->flags & LF_COMPOUND_RETURN) {
795 /* check for compound returns */
796 n_ress = get_method_n_ress(tp);
797 for (i = 0; i < n_ress; ++i) {
798 res_tp = get_method_res_type(tp, i);
800 if (is_compound_type(res_tp))
809 * type-walker: lower all method types of entities
810 * and points-to types.
812 static void lower_method_types(type_or_ent tore, void *env)
814 const lower_params_t *lp = env;
817 /* fix method entities */
818 if (is_entity(tore.ent)) {
819 ir_entity *ent = tore.ent;
820 tp = get_entity_type(ent);
822 if (must_be_lowered(lp, tp)) {
823 tp = create_modified_mtd_type(lp, tp);
824 set_entity_type(ent, tp);
829 /* fix pointer to methods */
830 if (is_Pointer_type(tp)) {
831 ir_type *etp = get_pointer_points_to_type(tp);
832 if (must_be_lowered(lp, etp)) {
833 etp = create_modified_mtd_type(lp, etp);
834 set_pointer_points_to_type(tp, etp);
841 * Lower calls with compound parameters and return types.
842 * This function does the following transformations:
844 * - Adds a new (hidden) pointer parameter for
845 * any return compound type.
847 * - Use of the hidden parameters in the function code.
849 * - Change all calls to functions with compound return
850 * by providing space for the hidden parameter on the callers
853 * - Replace a possible block copy after the function call.
855 void lower_calls_with_compounds(const lower_params_t *params)
859 lower_params_t param = *params;
861 if (param.find_pointer_type == NULL) {
862 param.find_pointer_type = def_find_pointer_type;
863 type_map = pmap_create_ex(8);
867 /* first step: Transform all graphs */
868 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
869 irg = get_irp_irg(i);
871 if (irg == get_const_code_irg())
874 transform_irg(¶m, irg);
877 /* second step: Lower all method types of visible entities */
878 type_walk(NULL, lower_method_types, ¶m);
881 pmap_destroy(type_map);