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"
46 /** A type map for def_find_pointer_type. */
47 static pmap *type_map;
50 * Default implementation for finding a pointer type for a given element type.
51 * Simple create a new one.
53 static ir_type *def_find_pointer_type(ir_type *e_type, ir_mode *mode, int alignment)
58 /* Mode and alignment are always identical in all calls to def_find_pointer_type(), so
59 we simply can use a map from the element type to the pointer type. */
60 e = pmap_find(type_map, e_type);
64 res = new_type_pointer(mangle_u(get_type_ident(e_type), new_id_from_chars("Ptr", 3)), e_type, mode);
65 set_type_alignment_bytes(res, alignment);
66 pmap_insert(type_map, e_type, res);
72 * Creates a new lowered type for a method type with compound
73 * arguments. The new type is associated to the old one and returned.
75 * @param lp parameter struct
76 * @param mtp the method type to lower
78 * The current implementation expects that a lowered type already
79 * includes the necessary changes ...
81 static ir_type *create_modified_mtd_type(const lower_params_t *lp, ir_type *mtp)
83 ir_type *lowered, *ptr_tp;
84 ir_type **params, **results, *res_tp;
85 ir_mode *modes[MAX_REGISTER_RET_VAL];
86 int n_ress, n_params, nn_ress, nn_params, i, first_variadic;
88 add_hidden hidden_params;
92 if (is_lowered_type(mtp)) {
93 /* the type is already lowered. Not handled yet. */
94 assert(0 && "lowered types NYI");
97 lowered = get_associated_type(mtp);
101 n_ress = get_method_n_ress(mtp);
102 NEW_ARR_A(ir_type *, results, n_ress);
104 n_params = get_method_n_params(mtp);
105 NEW_ARR_A(ir_type *, params, n_params + n_ress);
107 first_variadic = get_method_first_variadic_param_index(mtp);
109 hidden_params = lp->hidden_params;
110 if (hidden_params == ADD_HIDDEN_SMART &&
111 get_method_variadicity(mtp) == variadicity_variadic)
112 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
114 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
115 /* add hidden in front */
116 for (nn_ress = nn_params = i = 0; i < n_ress; ++i) {
117 res_tp = get_method_res_type(mtp, i);
119 if (is_compound_type(res_tp)) {
122 if (lp->flags & LF_SMALL_CMP_IN_REGS)
123 n_regs = lp->ret_compound_in_regs(res_tp, modes);
126 /* this compound will be returned solely in registers */
130 /* this compound will be allocated on callers stack and its
131 address will be transmitted as a hidden parameter. */
132 ptr_tp = lp->find_pointer_type(res_tp, get_modeP_data(), lp->def_ptr_alignment);
133 params[nn_params++] = ptr_tp;
135 if (lp->flags & LF_RETURN_HIDDEN)
136 results[nn_ress++] = ptr_tp;
140 results[nn_ress++] = res_tp;
143 /* move the index of the first variadic parameter */
144 first_variadic += nn_params;
146 for (i = 0; i < n_params; ++i)
147 params[nn_params++] = get_method_param_type(mtp, i);
150 /* add hidden parameters last */
151 assert(get_method_variadicity(mtp) == variadicity_non_variadic &&
152 "Cannot add hidden parameters at end of variadic function");
154 for (nn_params = 0; nn_params < n_params; ++nn_params)
155 params[nn_params] = get_method_param_type(mtp, nn_params);
157 for (nn_ress = i = 0; i < n_ress; ++i) {
158 res_tp = get_method_res_type(mtp, i);
160 if (is_compound_type(res_tp))
161 params[nn_params++] = lp->find_pointer_type(res_tp, get_modeP_data(), lp->def_ptr_alignment);
163 results[nn_ress++] = res_tp;
167 /* create the new type */
168 id = mangle_u(new_id_from_chars("L", 1), get_type_ident(mtp));
169 lowered = new_d_type_method(id, nn_params, nn_ress, get_type_dbg_info(mtp));
172 for (i = 0; i < nn_params; ++i)
173 set_method_param_type(lowered, i, params[i]);
174 for (i = 0; i < nn_ress; ++i)
175 set_method_res_type(lowered, i, results[i]);
177 var = get_method_variadicity(mtp);
178 set_method_variadicity(lowered, var);
179 if (var == variadicity_variadic)
180 set_method_first_variadic_param_index(lowered, first_variadic);
182 /* associate the lowered type with the original one for easier access */
184 set_method_calling_convention(lowered, get_method_calling_convention(mtp) | cc_compound_ret);
186 set_lowered_type(mtp, lowered);
194 typedef struct cl_entry cl_entry;
196 cl_entry *next; /**< Pointer to the next entry. */
197 ir_node *call; /**< Pointer to the Call node. */
198 ir_node *copyb; /**< List of all CopyB nodes. */
202 * Walker environment for fix_args_and_collect_calls().
204 typedef struct _wlk_env_t {
205 int arg_shift; /**< The Argument index shift for parameters. */
206 int first_hidden; /**< The index of the first hidden argument. */
207 struct obstack obst; /**< An obstack to allocate the data on. */
208 cl_entry *cl_list; /**< The call list. */
209 pmap *dummy_map; /**< A map for finding the dummy arguments. */
210 unsigned dnr; /**< The dummy index number. */
211 const lower_params_t *params; /**< Lowering parameters. */
212 unsigned only_local_mem:1; /**< Set if only local memory access was found. */
213 unsigned changed:1; /**< Set if the current graph was changed. */
217 * Return the call list entry of a call node.
218 * If no entry exists yet, allocate one and enter the node into
219 * the call list of the environment.
221 * @param call A Call node.
222 * @param env The environment.
224 static cl_entry *get_Call_entry(ir_node *call, wlk_env *env) {
225 cl_entry *res = get_irn_link(call);
227 cl_entry *res = obstack_alloc(&env->obst, sizeof(*res));
228 res->next = env->cl_list;
231 set_irn_link(call, res);
238 * Finds the base address of an address by skipping Sel's and address
241 * @param adr the address
242 * @param pEnt points to the base entity if any
244 static ir_node *find_base_adr(ir_node *ptr, ir_entity **pEnt) {
245 ir_entity *ent = NULL;
246 assert(mode_is_reference(get_irn_mode(ptr)));
250 ent = get_Sel_entity(ptr);
251 ptr = get_Sel_ptr(ptr);
253 else if (is_Add(ptr)) {
254 ir_node *left = get_Add_left(ptr);
255 if (mode_is_reference(get_irn_mode(left)))
258 ptr = get_Add_right(ptr);
260 } else if (is_Sub(ptr)) {
261 ptr = get_Sub_left(ptr);
271 * Check if a given pointer represents non-local memory.
273 static void check_ptr(ir_node *ptr, wlk_env *env) {
274 ir_storage_class_class_t sc;
277 /* still alias free */
278 ptr = find_base_adr(ptr, &ent);
279 sc = classify_pointer(current_ir_graph, ptr, ent);
280 if (sc != ir_sc_localvar && sc != ir_sc_malloced) {
281 /* non-local memory access */
282 env->only_local_mem = 0;
287 * Post walker: shift all parameter indexes
288 * and collect Calls with compound returns in the call list.
289 * If a non-alias free memory access is found, reset the alias free
292 static void fix_args_and_collect_calls(ir_node *n, void *ctx) {
298 switch (get_irn_opcode(n)) {
301 if (env->only_local_mem) {
302 ptr = get_irn_n(n, 1);
307 if (env->arg_shift > 0) {
308 ir_node *pred = get_Proj_pred(n);
310 /* Fix the argument numbers */
311 if (pred == get_irg_args(current_ir_graph)) {
312 long pnr = get_Proj_proj(n);
313 set_Proj_proj(n, pnr + env->arg_shift);
319 if (! is_self_recursive_Call(n)) {
320 /* any non self recursive call might access global memory */
321 env->only_local_mem = 0;
324 ctp = get_Call_type(n);
325 if (env->params->flags & LF_COMPOUND_RETURN) {
326 /* check for compound returns */
327 for (i = get_method_n_ress(ctp) -1; i >= 0; --i) {
328 if (is_compound_type(get_method_res_type(ctp, i))) {
330 * This is a call with a compound return. As the result
331 * might be ignored, we must put it in the list.
333 (void)get_Call_entry(n, env);
340 if (env->only_local_mem) {
341 check_ptr(get_CopyB_src(n), env);
342 if (env->only_local_mem)
343 check_ptr(get_CopyB_dst(n), env);
345 if (env->params->flags & LF_COMPOUND_RETURN) {
346 /* check for compound returns */
347 ir_node *src = get_CopyB_src(n);
348 /* older scheme using value_res_ent */
350 ir_node *proj = get_Sel_ptr(src);
351 if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_P_value_res_base) {
352 ir_node *call = get_Proj_pred(proj);
354 /* found a CopyB from compound Call result */
355 cl_entry *e = get_Call_entry(call, env);
356 set_irn_link(n, e->copyb);
361 /* new scheme: compound results are determined by the call type only */
363 ir_node *proj = get_Proj_pred(src);
364 if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_T_result) {
365 ir_node *call = get_Proj_pred(proj);
367 ctp = get_Call_type(call);
368 if (is_compound_type(get_method_res_type(ctp, get_Proj_proj(src)))) {
369 /* found a CopyB from compound Call result */
370 cl_entry *e = get_Call_entry(call, env);
371 set_irn_link(n, e->copyb);
387 * Returns non-zero if a node is a compound address
388 * of a frame-type entity.
390 * @param ft the frame type
391 * @param adr the node
393 static int is_compound_address(ir_type *ft, ir_node *adr)
399 if (get_Sel_n_indexs(adr) != 0)
401 ent = get_Sel_entity(adr);
402 return get_entity_owner(ent) == ft;
405 /** A pair for the copy-return-optimization. */
406 typedef struct cr_pair {
407 ir_entity *ent; /**< the entity than can be removed from the frame */
408 ir_node *arg; /**< the argument that replaces the entities address */
412 * Post walker: fixes all entities addresses for the copy-return
415 * Note: We expect the length of the cr_pair array (ie number of compound
416 * return values) to be 1 (C, C++) in almost all cases, so ignore the
417 * linear search complexity here.
419 static void do_copy_return_opt(ir_node *n, void *ctx) {
424 ir_entity *ent = get_Sel_entity(n);
426 for (i = ARR_LEN(arr) - 1; i >= 0; --i) {
427 if (ent == arr[i].ent) {
428 exchange(n, arr[i].arg);
436 * Return a Sel node that selects a dummy argument of type tp.
437 * Dummy arguments are only needed once and we use a map
439 * We could even assign all dummy arguments the same offset
440 * in the frame type ...
442 * @param irg the graph
443 * @param block the block where a newly create Sel should be placed
444 * @param tp the type of the dummy entity that should be create
445 * @param env the environment
447 static ir_node *get_dummy_sel(ir_graph *irg, ir_node *block, ir_type *tp, wlk_env *env)
452 /* use a map the check if we already create such an entity */
453 e = pmap_find(env->dummy_map, tp);
457 ir_type *ft = get_irg_frame_type(irg);
460 snprintf(buf, sizeof(buf), "dummy.%u", env->dnr++);
461 ent = new_entity(ft, new_id_from_str(buf), tp);
462 pmap_insert(env->dummy_map, tp, ent);
464 if (get_type_state(ft) == layout_fixed) {
465 /* Fix the layout again */
466 assert(0 && "Fixed layout not implemented");
469 return new_r_simpleSel(
478 * Add the hidden parameter from the CopyB node to the Call node.
480 * @param irg the graph
481 * @param n_com number of compound results (will be number of hidden parameters)
482 * @param ins in array to store the hidden parameters into
483 * @param entry the call list
484 * @param env the environment
486 static void add_hidden_param(ir_graph *irg, int n_com, ir_node **ins, cl_entry *entry, wlk_env *env)
488 ir_node *p, *n, *src, *mem, *blk;
494 for (p = entry->copyb; p; p = n) {
496 src = get_CopyB_src(p);
498 /* old scheme using value_res_ent */
500 ent = get_Sel_entity(src);
501 owner = get_entity_owner(ent);
503 /* find the hidden parameter index */
504 for (idx = 0; idx < get_struct_n_members(owner); ++idx)
505 if (get_struct_member(owner, idx) == ent)
507 assert(idx < get_struct_n_members(owner));
509 /* new scheme: compound returns are determined by the call type and are Proj's */
510 idx = get_Proj_proj(src);
513 ins[idx] = get_CopyB_dst(p);
514 mem = get_CopyB_mem(p);
515 blk = get_nodes_block(p);
517 /* get rid of the CopyB */
518 turn_into_tuple(p, pn_CopyB_max);
519 set_Tuple_pred(p, pn_CopyB_M_regular, mem);
520 set_Tuple_pred(p, pn_CopyB_M_except, get_irg_bad(irg));
521 set_Tuple_pred(p, pn_CopyB_X_regular, new_r_Jmp(irg, blk));
522 set_Tuple_pred(p, pn_CopyB_X_except, get_irg_bad(irg));
526 /* now create dummy entities for function with ignored return value */
527 if (n_args < n_com) {
528 ir_type *ctp = get_Call_type(entry->call);
531 if (is_lowered_type(ctp))
532 ctp = get_associated_type(ctp);
534 for (j = i = 0; i < get_method_n_ress(ctp); ++i) {
535 ir_type *rtp = get_method_res_type(ctp, i);
536 if (is_compound_type(rtp)) {
538 ins[j] = get_dummy_sel(irg, get_nodes_block(entry->call), rtp, env);
546 * Fix all calls on a call list by adding hidden parameters.
548 * @param irg the graph
549 * @param env the environment
551 static void fix_call_list(ir_graph *irg, wlk_env *env) {
552 const lower_params_t *lp = env->params;
554 ir_node *call, **new_in;
555 ir_type *ctp, *lowered_mtp;
556 add_hidden hidden_params;
557 int i, n_params, n_com, pos;
559 new_in = NEW_ARR_F(ir_node *, 0);
560 for (p = env->cl_list; p; p = p->next) {
562 ctp = get_Call_type(call);
563 lowered_mtp = create_modified_mtd_type(lp, ctp);
564 set_Call_type(call, lowered_mtp);
566 hidden_params = lp->hidden_params;
567 if (hidden_params == ADD_HIDDEN_SMART &&
568 get_method_variadicity(ctp) == variadicity_variadic)
569 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
571 n_params = get_Call_n_params(call);
574 for (i = get_method_n_ress(ctp) - 1; i >= 0; --i) {
575 if (is_compound_type(get_method_res_type(ctp, i)))
579 ARR_RESIZE(ir_node *, new_in, n_params + n_com + pos);
580 memset(new_in, 0, sizeof(*new_in) * (n_params + n_com + pos));
581 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
582 add_hidden_param(irg, n_com, &new_in[pos], p, env);
585 /* copy all other parameters */
586 for (i = 0; i < n_params; ++i)
587 new_in[pos++] = get_Call_param(call, i);
588 if (hidden_params == ADD_HIDDEN_ALWAYS_LAST) {
589 add_hidden_param(irg, n_com, &new_in[pos], p, env);
592 new_in[0] = get_Call_mem(call);
593 new_in[1] = get_Call_ptr(call);
595 set_irn_in(call, n_params + n_com + 2, new_in);
600 * Transform a graph. If it has compound parameter returns,
601 * remove them and use the hidden parameter instead.
602 * If it calls methods with compound parameter returns, add hidden
605 * @param lp parameter struct
606 * @param irg the graph to transform
608 static void transform_irg(const lower_params_t *lp, ir_graph *irg)
610 ir_graph * rem = current_ir_graph;
611 ir_entity *ent = get_irg_entity(irg);
612 ir_type *mtp, *lowered_mtp, *tp, *ft;
613 int i, j, k, n_ress = 0, n_ret_com = 0, n_cr_opt;
614 ir_node **new_in, *ret, *endbl, *bl, *mem, *copy;
617 add_hidden hidden_params;
619 current_ir_graph = irg;
621 assert(ent && "Cannot transform graph without an entity");
622 assert(get_irg_phase_state(irg) == phase_high && "call lowering must be done in phase high");
624 mtp = get_entity_type(ent);
626 if (lp->flags & LF_COMPOUND_RETURN) {
627 /* calculate the number of compound returns */
628 n_ress = get_method_n_ress(mtp);
629 for (n_ret_com = i = 0; i < n_ress; ++i) {
630 tp = get_method_res_type(mtp, i);
632 if (is_compound_type(tp))
638 /* much easier if we have only one return */
639 normalize_one_return(irg);
641 /* This graph has a compound argument. Create a new type */
642 lowered_mtp = create_modified_mtd_type(lp, mtp);
643 set_entity_type(ent, lowered_mtp);
645 hidden_params = lp->hidden_params;
646 if (hidden_params == ADD_HIDDEN_SMART &&
647 get_method_variadicity(mtp) == variadicity_variadic)
648 hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
650 if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
651 /* hidden arguments are added first */
652 env.arg_shift = n_ret_com;
653 env.first_hidden = 0;
655 /* hidden arguments are added last */
657 env.first_hidden = get_method_n_params(mtp);
660 /* we must only search for calls */
663 obstack_init(&env.obst);
665 env.dummy_map = pmap_create_ex(8);
668 env.only_local_mem = 1;
671 /* scan the code, fix argument numbers and collect calls. */
672 irg_walk_graph(irg, firm_clear_link, fix_args_and_collect_calls, &env);
676 fix_call_list(irg, &env);
682 * Now fix the Return node of the current graph.
686 /* STEP 1: find the return. This is simple, we have normalized the graph. */
687 endbl = get_irg_end_block(irg);
689 for (i = get_Block_n_cfgpreds(endbl) - 1; i >= 0; --i) {
690 ir_node *pred = get_Block_cfgpred(endbl, i);
692 if (is_Return(pred)) {
697 /* there should always be a return */
701 * STEP 2: fix it. For all compound return values add a CopyB,
702 * all others are copied.
704 NEW_ARR_A(ir_node *, new_in, n_ress + 1);
706 bl = get_nodes_block(ret);
707 mem = get_Return_mem(ret);
709 ft = get_irg_frame_type(irg);
710 NEW_ARR_A(cr_pair, cr_opt, n_ret_com);
712 for (j = 1, i = k = 0; i < n_ress; ++i) {
713 ir_node *pred = get_Return_res(ret, i);
714 tp = get_method_res_type(mtp, i);
716 if (is_compound_type(tp)) {
717 ir_node *arg = get_irg_args(irg);
718 arg = new_r_Proj(irg, get_nodes_block(arg), arg, mode_P_data, env.first_hidden + k);
721 if (is_Unknown(pred)) {
722 /* The Return(Unknown) is the Firm construct for a missing return.
726 * Sorrily detecting that copy-return is possible isn't that simple.
727 * We must check, that the hidden address is alias free during the whole
729 * A simple heuristic: all Loads/Stores inside
730 * the function access only local frame.
732 if (env.only_local_mem && is_compound_address(ft, pred)) {
733 /* we can do the copy-return optimization here */
734 cr_opt[n_cr_opt].ent = get_Sel_entity(pred);
735 cr_opt[n_cr_opt].arg = arg;
737 } else { /* copy-return optimization is impossible, do the copy. */
745 mem = new_r_Proj(irg, bl, copy, mode_M, pn_CopyB_M_regular);
748 if (lp->flags & LF_RETURN_HIDDEN) {
752 } else { /* scalar return value */
757 /* replace the in of the Return */
759 set_irn_in(ret, j, new_in);
762 irg_walk_graph(irg, NULL, do_copy_return_opt, cr_opt);
764 for (i = ARR_LEN(cr_opt) - 1; i >= 0; --i) {
765 remove_class_member(ft, cr_opt[i].ent);
768 } /* if (n_ret_com) */
770 pmap_destroy(env.dummy_map);
771 obstack_free(&env.obst, NULL);
774 /* invalidate the analysis info */
775 set_irg_outs_inconsistent(irg);
776 set_irg_loopinfo_state(irg, loopinfo_inconsistent);
778 current_ir_graph = rem;
782 * Returns non-zero if the given type is a method
783 * type that must be lowered.
785 * @param lp lowering parameters
786 * @param tp The type.
788 static int must_be_lowered(const lower_params_t *lp, ir_type *tp) {
792 if (is_Method_type(tp)) {
793 if (lp->flags & LF_COMPOUND_RETURN) {
794 /* check for compound returns */
795 n_ress = get_method_n_ress(tp);
796 for (i = 0; i < n_ress; ++i) {
797 res_tp = get_method_res_type(tp, i);
799 if (is_compound_type(res_tp))
808 * type-walker: lower all method types of entities
809 * and points-to types.
811 static void lower_method_types(type_or_ent *tore, void *env)
813 const lower_params_t *lp = env;
816 /* fix method entities */
817 if (is_entity(tore)) {
818 ir_entity *ent = (ir_entity *)tore;
819 tp = get_entity_type(ent);
821 if (must_be_lowered(lp, tp)) {
822 tp = create_modified_mtd_type(lp, tp);
823 set_entity_type(ent, tp);
826 tp = (ir_type *)tore;
828 /* fix pointer to methods */
829 if (is_Pointer_type(tp)) {
830 ir_type *etp = get_pointer_points_to_type(tp);
831 if (must_be_lowered(lp, etp)) {
832 etp = create_modified_mtd_type(lp, etp);
833 set_pointer_points_to_type(tp, etp);
840 * Lower calls with compound parameters and return types.
841 * This function does the following transformations:
843 * - Adds a new (hidden) pointer parameter for
844 * any return compound type.
846 * - Use of the hidden parameters in the function code.
848 * - Change all calls to functions with compound return
849 * by providing space for the hidden parameter on the callers
852 * - Replace a possible block copy after the function call.
854 void lower_calls_with_compounds(const lower_params_t *params)
858 lower_params_t param = *params;
860 if (param.find_pointer_type == NULL) {
861 param.find_pointer_type = def_find_pointer_type;
862 type_map = pmap_create_ex(8);
866 /* first step: Transform all graphs */
867 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
868 irg = get_irp_irg(i);
870 if (irg == get_const_code_irg())
873 transform_irg(¶m, irg);
876 /* second step: Lower all method types of visible entities */
877 type_walk(NULL, lower_method_types, ¶m);
880 pmap_destroy(type_map);