2 * Copyright (C) 1995-2011 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
30 #include "lower_calls.h"
41 #include "iroptimize.h"
46 static pmap *pointer_types;
47 static pmap *lowered_mtps;
50 * Default implementation for finding a pointer type for a given element type.
51 * Simple create a new one.
53 static ir_type *get_pointer_type(ir_type *dest_type)
55 ir_type *res = (ir_type*)pmap_get(pointer_types, dest_type);
57 res = new_type_pointer(dest_type);
58 pmap_insert(pointer_types, dest_type, res);
63 static void fix_parameter_entities(ir_graph *irg, size_t n_compound_ret)
65 ir_type *frame_type = get_irg_frame_type(irg);
66 size_t n_compound = get_compound_n_members(frame_type);
69 if (n_compound_ret == 0)
72 for (i = 0; i < n_compound; ++i) {
73 ir_entity *member = get_compound_member(frame_type, i);
75 if (!is_parameter_entity(member))
78 /* increase parameter number since we added a new parameter in front */
79 num = get_entity_parameter_number(member);
80 if (num == IR_VA_START_PARAMETER_NUMBER)
82 set_entity_parameter_number(member, num + n_compound_ret);
87 * Creates a new lowered type for a method type with compound
88 * arguments. The new type is associated to the old one and returned.
90 static ir_type *lower_mtp(compound_call_lowering_flags flags, ir_type *mtp)
92 bool must_be_lowered = false;
101 mtp_additional_properties mtp_properties;
103 if (!is_Method_type(mtp))
106 lowered = (ir_type*)pmap_get(lowered_mtps, mtp);
110 /* check if the type has to be lowered at all */
111 n_ress = get_method_n_ress(mtp);
112 for (i = 0; i < n_ress; ++i) {
113 ir_type *res_tp = get_method_res_type(mtp, i);
114 if (is_compound_type(res_tp)) {
115 must_be_lowered = true;
119 if (!must_be_lowered)
122 n_params = get_method_n_params(mtp);
123 results = ALLOCANZ(ir_type*, n_ress);
124 params = ALLOCANZ(ir_type*, n_params + n_ress);
128 /* add a hidden parameter in front for every compound result */
129 for (i = 0; i < n_ress; ++i) {
130 ir_type *res_tp = get_method_res_type(mtp, i);
132 if (is_compound_type(res_tp)) {
133 /* this compound will be allocated on callers stack and its
134 address will be transmitted as a hidden parameter. */
135 ir_type *ptr_tp = get_pointer_type(res_tp);
136 params[nn_params++] = ptr_tp;
137 if (flags & LF_RETURN_HIDDEN)
138 results[nn_ress++] = ptr_tp;
141 results[nn_ress++] = res_tp;
144 /* copy over parameter types */
145 for (i = 0; i < n_params; ++i) {
146 params[nn_params++] = get_method_param_type(mtp, i);
148 assert(nn_ress <= n_ress);
149 assert(nn_params <= n_params + n_ress);
151 /* create the new type */
152 lowered = new_d_type_method(nn_params, nn_ress, get_type_dbg_info(mtp));
153 lowered->attr.ma.has_compound_ret_parameter = true;
156 for (i = 0; i < nn_params; ++i)
157 set_method_param_type(lowered, i, params[i]);
158 for (i = 0; i < nn_ress; ++i)
159 set_method_res_type(lowered, i, results[i]);
161 set_method_variadicity(lowered, get_method_variadicity(mtp));
163 set_method_calling_convention(lowered, get_method_calling_convention(mtp) | cc_compound_ret);
164 mtp_properties = get_method_additional_properties(mtp);
165 /* after lowering the call is not const anymore, since it writes to the
166 * memory for the return value passed to it */
167 mtp_properties &= ~mtp_property_const;
168 set_method_additional_properties(lowered, mtp_properties);
170 /* associate the lowered type with the original one for easier access */
171 set_lowered_type(mtp, lowered);
172 pmap_insert(lowered_mtps, mtp, lowered);
180 typedef struct cl_entry cl_entry;
182 cl_entry *next; /**< Pointer to the next entry. */
183 ir_node *call; /**< Pointer to the Call node. */
184 ir_node *copyb; /**< List of all CopyB nodes. */
188 * Walker environment for fix_args_and_collect_calls().
190 typedef struct wlk_env_t {
191 size_t arg_shift; /**< The Argument index shift for parameters. */
192 struct obstack obst; /**< An obstack to allocate the data on. */
193 cl_entry *cl_list; /**< The call list. */
194 pmap *dummy_map; /**< A map for finding the dummy arguments. */
195 compound_call_lowering_flags flags;
196 ir_type *lowered_mtp; /**< The lowered method type of the current irg if any. */
197 unsigned only_local_mem:1; /**< Set if only local memory access was found. */
198 unsigned changed:1; /**< Set if the current graph was changed. */
202 * Return the call list entry of a call node.
203 * If no entry exists yet, allocate one and enter the node into
204 * the call list of the environment.
206 * @param call A Call node.
207 * @param env The environment.
209 static cl_entry *get_Call_entry(ir_node *call, wlk_env *env)
211 cl_entry *res = (cl_entry*)get_irn_link(call);
213 res = OALLOC(&env->obst, cl_entry);
214 res->next = env->cl_list;
217 set_irn_link(call, res);
224 * Finds the base address of an address by skipping Sel's and address
227 * @param adr the address
228 * @param pEnt points to the base entity if any
230 static ir_node *find_base_adr(ir_node *ptr, ir_entity **pEnt)
232 ir_entity *ent = NULL;
233 assert(mode_is_reference(get_irn_mode(ptr)));
237 ent = get_Sel_entity(ptr);
238 ptr = get_Sel_ptr(ptr);
240 else if (is_Add(ptr)) {
241 ir_node *left = get_Add_left(ptr);
242 if (mode_is_reference(get_irn_mode(left)))
245 ptr = get_Add_right(ptr);
247 } else if (is_Sub(ptr)) {
248 ptr = get_Sub_left(ptr);
258 * Check if a given pointer represents non-local memory.
260 static void check_ptr(ir_node *ptr, wlk_env *env)
262 ir_storage_class_class_t sc;
265 /* still alias free */
266 ptr = find_base_adr(ptr, &ent);
267 sc = get_base_sc(classify_pointer(ptr, ent));
268 if (sc != ir_sc_localvar && sc != ir_sc_malloced) {
269 /* non-local memory access */
270 env->only_local_mem = 0;
275 * Returns non-zero if a Call is surely a self-recursive Call.
276 * Beware: if this functions returns 0, the call might be self-recursive!
278 static bool is_self_recursive_Call(const ir_node *call)
280 const ir_node *callee = get_Call_ptr(call);
282 if (is_SymConst_addr_ent(callee)) {
283 const ir_entity *ent = get_SymConst_entity(callee);
284 const ir_graph *irg = get_entity_irg(ent);
285 if (irg == get_irn_irg(call))
292 * Post walker: shift all parameter indexes
293 * and collect Calls with compound returns in the call list.
294 * If a non-alias free memory access is found, reset the alias free
297 static void fix_args_and_collect_calls(ir_node *n, void *ctx)
299 wlk_env *env = (wlk_env*)ctx;
303 switch (get_irn_opcode(n)) {
306 if (env->only_local_mem) {
307 ptr = get_irn_n(n, 1);
312 if (env->arg_shift > 0) {
313 ir_node *pred = get_Proj_pred(n);
314 ir_graph *irg = get_irn_irg(n);
316 /* Fix the argument numbers */
317 if (pred == get_irg_args(irg)) {
318 long pnr = get_Proj_proj(n);
319 set_Proj_proj(n, pnr + env->arg_shift);
327 if (! is_self_recursive_Call(n)) {
328 /* any non self recursive call might access global memory */
329 env->only_local_mem = 0;
332 ctp = get_Call_type(n);
333 /* check for compound returns */
334 for (i = 0, n_res = get_method_n_ress(ctp); i < n_res; ++i) {
335 if (is_compound_type(get_method_res_type(ctp, i))) {
337 * This is a call with a compound return. As the result
338 * might be ignored, we must put it in the list.
340 (void)get_Call_entry(n, env);
347 ir_node *src = get_CopyB_src(n);
348 if (env->only_local_mem) {
349 check_ptr(get_CopyB_src(n), env);
350 if (env->only_local_mem)
351 check_ptr(get_CopyB_dst(n), env);
353 /* check for compound returns */
355 ir_node *proj = get_Proj_pred(src);
356 if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_T_result) {
357 ir_node *call = get_Proj_pred(proj);
359 ctp = get_Call_type(call);
360 if (is_compound_type(get_method_res_type(ctp, get_Proj_proj(src)))) {
361 /* found a CopyB from compound Call result */
362 cl_entry *e = get_Call_entry(call, env);
363 set_irn_link(n, e->copyb);
372 ir_entity *ent = get_Sel_entity(n);
373 ir_type *type = get_entity_type(ent);
375 /* we need to copy compound parameters */
376 if (is_parameter_entity(ent) && is_compound_type(type)) {
377 env->only_local_mem = 0;
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 bool 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 (i.e. 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)
423 ir_entity *ent = get_Sel_entity(n);
424 cr_pair *arr = (cr_pair*)ctx;
427 for (i = 0, l = ARR_LEN(arr); i < l; ++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,
454 /* use a map the check if we already create such an entity */
455 e = pmap_find(env->dummy_map, tp);
457 ent = (ir_entity*)e->value;
459 ir_type *ft = get_irg_frame_type(irg);
460 ident *dummy_id = id_unique("dummy.%u");
461 ent = new_entity(ft, dummy_id, tp);
462 pmap_insert(env->dummy_map, tp, ent);
464 if (get_type_state(ft) == layout_fixed) {
465 /* Fix the layout again */
466 panic("Fixed layout not implemented");
469 return new_r_simpleSel(block, get_irg_no_mem(irg), get_irg_frame(irg), ent);
473 * Add the hidden parameter from the CopyB node to the Call node.
475 * @param irg the graph
476 * @param n_com number of compound results (will be number of hidden parameters)
477 * @param ins in array to store the hidden parameters into
478 * @param entry the call list
479 * @param env the environment
481 static void add_hidden_param(ir_graph *irg, size_t n_com, ir_node **ins,
482 cl_entry *entry, wlk_env *env)
484 ir_node *p, *n, *mem, *blk;
488 for (p = entry->copyb; p; p = n) {
489 ir_node *src = get_CopyB_src(p);
490 size_t idx = get_Proj_proj(src);
491 n = (ir_node*)get_irn_link(p);
493 ins[idx] = get_CopyB_dst(p);
494 blk = get_nodes_block(p);
496 /* use the memory output of the call and not the input of the CopyB
497 * otherwise stuff breaks if the call was mtp_property_const, because
498 * then the copyb skips the call. But after lowering the call is not
499 * const anymore, and its memory has to be used */
500 mem = new_r_Proj(entry->call, mode_M, pn_Call_M);
502 /* get rid of the CopyB */
503 turn_into_tuple(p, pn_CopyB_max+1);
504 set_Tuple_pred(p, pn_CopyB_M, mem);
505 set_Tuple_pred(p, pn_CopyB_X_regular, new_r_Jmp(blk));
506 set_Tuple_pred(p, pn_CopyB_X_except, new_r_Bad(irg, mode_X));
510 /* now create dummy entities for function with ignored return value */
511 if (n_args < n_com) {
512 ir_type *ctp = get_Call_type(entry->call);
516 if (is_lowered_type(ctp))
517 ctp = get_associated_type(ctp);
519 for (j = i = 0; i < get_method_n_ress(ctp); ++i) {
520 ir_type *rtp = get_method_res_type(ctp, i);
521 if (is_compound_type(rtp)) {
523 ins[j] = get_dummy_sel(irg, get_nodes_block(entry->call), rtp, env);
531 * Fix all calls on a call list by adding hidden parameters.
533 * @param irg the graph
534 * @param env the environment
536 static void fix_call_list(ir_graph *irg, wlk_env *env)
539 ir_node *call, **new_in;
540 ir_type *ctp, *lowered_mtp;
541 size_t i, n_res, n_params, n_com, pos;
543 new_in = NEW_ARR_F(ir_node *, 0);
544 for (p = env->cl_list; p; p = p->next) {
546 ctp = get_Call_type(call);
547 lowered_mtp = lower_mtp(env->flags, ctp);
548 set_Call_type(call, lowered_mtp);
550 n_params = get_Call_n_params(call);
553 for (i = 0, n_res = get_method_n_ress(ctp); i < n_res; ++i) {
554 if (is_compound_type(get_method_res_type(ctp, i)))
558 ARR_RESIZE(ir_node *, new_in, n_params + n_com + pos);
559 memset(new_in, 0, sizeof(*new_in) * (n_params + n_com + pos));
560 add_hidden_param(irg, n_com, &new_in[pos], p, env);
562 /* copy all other parameters */
563 for (i = 0; i < n_params; ++i)
564 new_in[pos++] = get_Call_param(call, i);
565 new_in[0] = get_Call_mem(call);
566 new_in[1] = get_Call_ptr(call);
568 set_irn_in(call, n_params + n_com + 2, new_in);
573 * Transform a graph. If it has compound parameter returns,
574 * remove them and use the hidden parameter instead.
575 * If it calls methods with compound parameter returns, add hidden
578 * @param irg the graph to transform
580 static void transform_irg(compound_call_lowering_flags flags, ir_graph *irg)
582 ir_entity *ent = get_irg_entity(irg);
583 ir_type *mtp, *lowered_mtp, *tp, *ft;
584 size_t i, j, k, n_ress = 0, n_ret_com = 0;
586 ir_node **new_in, *ret, *endbl, *bl, *mem, *copy;
590 mtp = get_entity_type(ent);
592 /* calculate the number of compound returns */
593 n_ress = get_method_n_ress(mtp);
594 for (n_ret_com = i = 0; i < n_ress; ++i) {
595 tp = get_method_res_type(mtp, i);
597 if (is_compound_type(tp))
601 fix_parameter_entities(irg, n_ret_com);
604 /* much easier if we have only one return */
605 normalize_one_return(irg);
607 /* This graph has a compound argument. Create a new type */
608 lowered_mtp = lower_mtp(flags, mtp);
609 set_entity_type(ent, lowered_mtp);
611 /* hidden arguments are added first */
612 env.arg_shift = n_ret_com;
614 /* we must only search for calls */
618 obstack_init(&env.obst);
620 env.dummy_map = pmap_create_ex(8);
622 env.lowered_mtp = lowered_mtp;
623 env.only_local_mem = 1;
626 /* scan the code, fix argument numbers and collect calls. */
627 irg_walk_graph(irg, firm_clear_link, fix_args_and_collect_calls, &env);
631 fix_call_list(irg, &env);
638 /* STEP 1: find the return. This is simple, we have normalized the graph. */
639 endbl = get_irg_end_block(irg);
641 for (idx = get_Block_n_cfgpreds(endbl) - 1; idx >= 0; --idx) {
642 ir_node *pred = get_Block_cfgpred(endbl, idx);
644 if (is_Return(pred)) {
650 /* in case of infinite loops, there might be no return */
653 * Now fix the Return node of the current graph.
658 * STEP 2: fix it. For all compound return values add a CopyB,
659 * all others are copied.
661 NEW_ARR_A(ir_node *, new_in, n_ress + 1);
663 bl = get_nodes_block(ret);
664 mem = get_Return_mem(ret);
666 ft = get_irg_frame_type(irg);
667 NEW_ARR_A(cr_pair, cr_opt, n_ret_com);
669 for (j = 1, i = k = 0; i < n_ress; ++i) {
670 ir_node *pred = get_Return_res(ret, i);
671 tp = get_method_res_type(mtp, i);
673 if (is_compound_type(tp)) {
674 ir_node *arg = get_irg_args(irg);
675 arg = new_r_Proj(arg, mode_P_data, k);
678 if (is_Unknown(pred)) {
679 /* The Return(Unknown) is the Firm construct for a
680 * missing return. Do nothing. */
683 * Sorrily detecting that copy-return is possible isn't
684 * that simple. We must check, that the hidden address
685 * is alias free during the whole function.
686 * A simple heuristic: all Loads/Stores inside
687 * the function access only local frame.
689 if (env.only_local_mem && is_compound_address(ft, pred)) {
690 /* we can do the copy-return optimization here */
691 cr_opt[n_cr_opt].ent = get_Sel_entity(pred);
692 cr_opt[n_cr_opt].arg = arg;
694 } else { /* copy-return optimization is impossible, do the copy. */
702 mem = new_r_Proj(copy, mode_M, pn_CopyB_M);
705 if (flags & LF_RETURN_HIDDEN) {
709 } else { /* scalar return value */
714 /* replace the in of the Return */
716 set_irn_in(ret, j, new_in);
722 irg_walk_graph(irg, NULL, do_copy_return_opt, cr_opt);
724 for (c = 0, n = ARR_LEN(cr_opt); c < n; ++c) {
725 free_entity(cr_opt[c].ent);
731 pmap_destroy(env.dummy_map);
732 obstack_free(&env.obst, NULL);
735 static void lower_method_types(type_or_ent tore, void *env)
737 const compound_call_lowering_flags *flags
738 = (const compound_call_lowering_flags*)env;
740 /* fix method entities */
741 if (is_entity(tore.ent)) {
742 ir_entity *ent = tore.ent;
743 ir_type *tp = get_entity_type(ent);
744 ir_type *lowered = lower_mtp(*flags, tp);
745 set_entity_type(ent, lowered);
747 ir_type *tp = tore.typ;
749 /* fix pointer to methods */
750 if (is_Pointer_type(tp)) {
751 ir_type *points_to = get_pointer_points_to_type(tp);
752 ir_type *lowered_points_to = lower_mtp(*flags, points_to);
753 set_pointer_points_to_type(tp, lowered_points_to);
758 void lower_calls_with_compounds(compound_call_lowering_flags flags)
762 pointer_types = pmap_create();
763 lowered_mtps = pmap_create();
765 /* first step: Transform all graphs */
766 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
767 ir_graph *irg = get_irp_irg(i);
768 transform_irg(flags, irg);
771 /* second step: Lower all method types of visible entities */
772 type_walk(NULL, lower_method_types, &flags);
774 pmap_destroy(lowered_mtps);
775 pmap_destroy(pointer_types);