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, Matthias Braun
29 #include "lower_calls.h"
40 #include "iroptimize.h"
46 static pmap *pointer_types;
47 static pmap *lowered_mtps;
49 static bool needs_lowering(const ir_type *type)
51 return is_compound_type(type) || is_Array_type(type);
55 * Default implementation for finding a pointer type for a given element type.
56 * Simply create a new one.
58 static ir_type *get_pointer_type(ir_type *dest_type)
60 ir_type *res = pmap_get(ir_type, pointer_types, dest_type);
62 res = new_type_pointer(dest_type);
63 pmap_insert(pointer_types, dest_type, res);
68 static void fix_parameter_entities(ir_graph *irg, size_t n_compound_ret)
70 ir_type *frame_type = get_irg_frame_type(irg);
71 size_t n_members = get_compound_n_members(frame_type);
74 for (i = 0; i < n_members; ++i) {
75 ir_entity *member = get_compound_member(frame_type, i);
77 if (!is_parameter_entity(member))
80 /* increase parameter number since we added a new parameter in front */
81 num = get_entity_parameter_number(member);
82 if (num == IR_VA_START_PARAMETER_NUMBER)
84 set_entity_parameter_number(member, num + n_compound_ret);
88 static void remove_compound_param_entities(ir_graph *irg)
90 ir_type *frame_type = get_irg_frame_type(irg);
91 size_t n_members = get_compound_n_members(frame_type);
94 for (i = n_members; i > 0; ) {
95 ir_entity *member = get_compound_member(frame_type, --i);
97 if (!is_parameter_entity(member))
100 type = get_entity_type(member);
101 if (needs_lowering(type)) {
108 * Creates a new lowered type for a method type with compound
109 * arguments. The new type is associated to the old one and returned.
111 static ir_type *lower_mtp(compound_call_lowering_flags flags, ir_type *mtp)
113 bool must_be_lowered = false;
123 mtp_additional_properties mtp_properties;
125 if (!is_Method_type(mtp))
128 lowered = pmap_get(ir_type, lowered_mtps, mtp);
132 /* check if the type has to be lowered at all */
133 n_params = get_method_n_params(mtp);
134 n_ress = get_method_n_ress(mtp);
135 for (i = 0; i < n_ress; ++i) {
136 ir_type *res_tp = get_method_res_type(mtp, i);
137 if (needs_lowering(res_tp)) {
138 must_be_lowered = true;
142 if (!must_be_lowered && !(flags & LF_DONT_LOWER_ARGUMENTS)) {
143 for (i = 0; i < n_params; ++i) {
144 ir_type *param_type = get_method_param_type(mtp, i);
145 if (needs_lowering(param_type)) {
146 must_be_lowered = true;
151 if (!must_be_lowered)
154 results = ALLOCANZ(ir_type*, n_ress);
155 params = ALLOCANZ(ir_type*, n_params + n_ress);
159 /* add a hidden parameter in front for every compound result */
160 for (i = 0; i < n_ress; ++i) {
161 ir_type *res_tp = get_method_res_type(mtp, i);
163 if (needs_lowering(res_tp)) {
164 /* this compound will be allocated on callers stack and its
165 address will be transmitted as a hidden parameter. */
166 ir_type *ptr_tp = get_pointer_type(res_tp);
167 params[nn_params++] = ptr_tp;
168 if (flags & LF_RETURN_HIDDEN)
169 results[nn_ress++] = ptr_tp;
172 results[nn_ress++] = res_tp;
175 /* copy over parameter types */
176 for (i = 0; i < n_params; ++i) {
177 ir_type *param_type = get_method_param_type(mtp, i);
178 if (! (flags & LF_DONT_LOWER_ARGUMENTS) && needs_lowering(param_type)) {
179 /* turn parameter into a pointer type */
180 param_type = new_type_pointer(param_type);
182 params[nn_params++] = param_type;
184 assert(nn_ress <= n_ress);
185 assert(nn_params <= n_params + n_ress);
187 /* create the new type */
188 lowered = new_d_type_method(nn_params, nn_ress, get_type_dbg_info(mtp));
191 for (i = 0; i < nn_params; ++i)
192 set_method_param_type(lowered, i, params[i]);
193 for (i = 0; i < nn_ress; ++i)
194 set_method_res_type(lowered, i, results[i]);
196 set_method_variadicity(lowered, get_method_variadicity(mtp));
198 cconv = get_method_calling_convention(mtp);
199 if (nn_params > n_params) {
200 cconv |= cc_compound_ret;
202 set_method_calling_convention(lowered, cconv);
204 mtp_properties = get_method_additional_properties(mtp);
205 /* after lowering the call is not const anymore, since it writes to the
206 * memory for the return value passed to it */
207 mtp_properties &= ~mtp_property_const;
208 set_method_additional_properties(lowered, mtp_properties);
210 /* associate the lowered type with the original one for easier access */
211 set_higher_type(lowered, mtp);
212 pmap_insert(lowered_mtps, mtp, lowered);
220 typedef struct cl_entry cl_entry;
222 cl_entry *next; /**< Pointer to the next entry. */
223 ir_node *call; /**< Pointer to the Call node. */
224 ir_node *copyb; /**< List of all CopyB nodes. */
225 bool has_compound_ret : 1;
226 bool has_compound_param : 1;
230 * Walker environment for fix_args_and_collect_calls().
232 typedef struct wlk_env_t {
233 size_t arg_shift; /**< The Argument index shift for parameters. */
234 struct obstack obst; /**< An obstack to allocate the data on. */
235 cl_entry *cl_list; /**< The call list. */
236 compound_call_lowering_flags flags;
237 ir_type *lowered_mtp; /**< The lowered method type of the current irg if any. */
238 bool only_local_mem:1;/**< Set if only local memory access was found. */
239 bool changed:1; /**< Set if the current graph was changed. */
243 * Return the call list entry of a call node.
244 * If no entry exists yet, allocate one and enter the node into
245 * the call list of the environment.
247 * @param call A Call node.
248 * @param env The environment.
250 static cl_entry *get_call_entry(ir_node *call, wlk_env *env)
252 cl_entry *res = (cl_entry*)get_irn_link(call);
254 res = OALLOC(&env->obst, cl_entry);
255 res->next = env->cl_list;
258 set_irn_link(call, res);
265 * Finds the base address of an address by skipping Sel's and address
268 * @param adr the address
269 * @param pEnt points to the base entity if any
271 static ir_node *find_base_adr(ir_node *ptr, ir_entity **pEnt)
273 ir_entity *ent = NULL;
274 assert(mode_is_reference(get_irn_mode(ptr)));
278 ent = get_Sel_entity(ptr);
279 ptr = get_Sel_ptr(ptr);
281 else if (is_Add(ptr)) {
282 ir_node *left = get_Add_left(ptr);
283 if (mode_is_reference(get_irn_mode(left)))
286 ptr = get_Add_right(ptr);
288 } else if (is_Sub(ptr)) {
289 ptr = get_Sub_left(ptr);
299 * Check if a given pointer represents non-local memory.
301 static void check_ptr(ir_node *ptr, wlk_env *env)
303 ir_storage_class_class_t sc;
306 /* still alias free */
307 ptr = find_base_adr(ptr, &ent);
308 sc = get_base_sc(classify_pointer(ptr, ent));
309 if (sc != ir_sc_localvar && sc != ir_sc_malloced) {
310 /* non-local memory access */
311 env->only_local_mem = false;
316 * Returns non-zero if a Call is surely a self-recursive Call.
317 * Beware: if this functions returns 0, the call might be self-recursive!
319 static bool is_self_recursive_Call(const ir_node *call)
321 const ir_node *callee = get_Call_ptr(call);
323 if (is_SymConst_addr_ent(callee)) {
324 const ir_entity *ent = get_SymConst_entity(callee);
325 const ir_graph *irg = get_entity_irg(ent);
326 if (irg == get_irn_irg(call))
333 * Post walker: shift all parameter indexes
334 * and collect Calls with compound returns in the call list.
335 * If a non-alias free memory access is found, reset the alias free
338 static void fix_args_and_collect_calls(ir_node *n, void *ctx)
340 wlk_env *env = (wlk_env*)ctx;
343 switch (get_irn_opcode(n)) {
346 if (env->only_local_mem) {
347 ptr = get_irn_n(n, 1);
352 if (env->arg_shift > 0) {
353 ir_node *pred = get_Proj_pred(n);
354 ir_graph *irg = get_irn_irg(n);
356 /* Fix the argument numbers */
357 if (pred == get_irg_args(irg)) {
358 long pnr = get_Proj_proj(n);
359 set_Proj_proj(n, pnr + env->arg_shift);
365 ir_type *ctp = get_Call_type(n);
366 size_t n_ress = get_method_n_ress(ctp);
367 size_t n_params = get_method_n_params(ctp);
369 if (! is_self_recursive_Call(n)) {
370 /* any non self recursive call might access global memory */
371 env->only_local_mem = false;
374 /* check for compound returns */
375 for (i = 0; i < n_ress; ++i) {
376 ir_type *type = get_method_res_type(ctp, i);
377 if (needs_lowering(type)) {
379 * This is a call with a compound return. As the result
380 * might be ignored, we must put it in the list.
382 cl_entry *entry = get_call_entry(n, env);
383 entry->has_compound_ret = true;
387 for (i = 0; i < n_params; ++i) {
388 ir_type *type = get_method_param_type(ctp, i);
389 if (needs_lowering(type)) {
390 cl_entry *entry = get_call_entry(n, env);
391 entry->has_compound_param = true;
398 ir_node *src = get_CopyB_src(n);
399 if (env->only_local_mem) {
400 check_ptr(get_CopyB_src(n), env);
401 if (env->only_local_mem)
402 check_ptr(get_CopyB_dst(n), env);
404 /* check for compound returns */
406 ir_node *proj = get_Proj_pred(src);
407 if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_T_result) {
408 ir_node *call = get_Proj_pred(proj);
410 ir_type *ctp = get_Call_type(call);
411 if (needs_lowering(get_method_res_type(ctp, get_Proj_proj(src)))) {
412 /* found a CopyB from compound Call result */
413 cl_entry *e = get_call_entry(call, env);
414 set_irn_link(n, e->copyb);
423 ir_entity *entity = get_Sel_entity(n);
424 ir_type *type = get_entity_type(entity);
426 if (is_parameter_entity(entity) && needs_lowering(type)) {
427 if (! (env->flags & LF_DONT_LOWER_ARGUMENTS)) {
428 /* note that num was already modified by fix_parameter_entities
429 * so no need to add env->arg_shift again */
430 size_t num = get_entity_parameter_number(entity);
431 ir_graph *irg = get_irn_irg(n);
432 ir_node *args = get_irg_args(irg);
433 ir_node *ptr = new_r_Proj(args, mode_P, num);
435 /* hack to avoid us visiting the proj again */
436 mark_irn_visited(ptr);
439 /* we need to copy compound parameters */
440 env->only_local_mem = false;
451 * Returns non-zero if a node is a compound address
452 * of a frame-type entity.
454 * @param ft the frame type
455 * @param adr the node
457 static bool is_compound_address(ir_type *ft, ir_node *adr)
463 if (get_Sel_n_indexs(adr) != 0)
465 ent = get_Sel_entity(adr);
466 return get_entity_owner(ent) == ft;
469 /** A pair for the copy-return-optimization. */
470 typedef struct cr_pair {
471 ir_entity *ent; /**< the entity than can be removed from the frame */
472 ir_node *arg; /**< the argument that replaces the entities address */
476 * Post walker: fixes all entities addresses for the copy-return
479 * Note: We expect the length of the cr_pair array (i.e. number of compound
480 * return values) to be 1 (C, C++) in almost all cases, so ignore the
481 * linear search complexity here.
483 static void do_copy_return_opt(ir_node *n, void *ctx)
486 ir_entity *ent = get_Sel_entity(n);
487 cr_pair *arr = (cr_pair*)ctx;
490 for (i = 0, l = ARR_LEN(arr); i < l; ++i) {
491 if (ent == arr[i].ent) {
492 exchange(n, arr[i].arg);
500 * Return a Sel node that selects a dummy argument of type tp.
502 * @param irg the graph
503 * @param block the block where a newly create Sel should be placed
504 * @param tp the type of the dummy entity that should be create
506 static ir_node *get_dummy_sel(ir_graph *irg, ir_node *block, ir_type *tp)
508 ir_type *ft = get_irg_frame_type(irg);
509 ident *dummy_id = id_unique("dummy.%u");
510 ir_entity *ent = new_entity(ft, dummy_id, tp);
512 if (get_type_state(ft) == layout_fixed) {
513 /* Fix the layout again */
514 panic("Fixed layout not implemented");
517 return new_r_simpleSel(block, get_irg_no_mem(irg), get_irg_frame(irg), ent);
521 * Add the hidden parameter from the CopyB node to the Call node.
523 static void add_hidden_param(ir_graph *irg, size_t n_com, ir_node **ins,
524 cl_entry *entry, ir_type *ctp)
530 for (p = entry->copyb; p; p = n) {
531 ir_node *src = get_CopyB_src(p);
532 size_t idx = get_Proj_proj(src);
533 n = (ir_node*)get_irn_link(p);
535 /* consider only the first CopyB */
536 if (ins[idx] == NULL) {
537 ir_node *block = get_nodes_block(p);
539 /* use the memory output of the call and not the input of the CopyB
540 * otherwise stuff breaks if the call was mtp_property_const, because
541 * then the copyb skips the call. But after lowering the call is not
542 * const anymore, and its memory has to be used */
543 ir_node *mem = new_r_Proj(entry->call, mode_M, pn_Call_M);
545 ins[idx] = get_CopyB_dst(p);
547 /* get rid of the CopyB */
548 if (ir_throws_exception(p)) {
549 ir_node *const in[] = {
551 [pn_CopyB_X_regular] = new_r_Jmp(block),
552 [pn_CopyB_X_except] = new_r_Bad(irg, mode_X),
554 turn_into_tuple(p, ARRAY_SIZE(in), in);
556 ir_node *const in[] = { mem };
557 turn_into_tuple(p, ARRAY_SIZE(in), in);
563 /* now create dummy entities for function with ignored return value */
564 if (n_args < n_com) {
568 for (j = i = 0; i < get_method_n_ress(ctp); ++i) {
569 ir_type *rtp = get_method_res_type(ctp, i);
570 if (needs_lowering(rtp)) {
572 ins[j] = get_dummy_sel(irg, get_nodes_block(entry->call), rtp);
579 static void fix_compound_ret(cl_entry *entry, ir_type *ctp)
581 ir_node *call = entry->call;
582 ir_graph *irg = get_irn_irg(call);
583 size_t n_params = get_Call_n_params(call);
585 size_t n_res = get_method_n_ress(ctp);
590 for (i = 0; i < n_res; ++i) {
591 ir_type *type = get_method_res_type(ctp, i);
592 if (needs_lowering(type))
596 new_in = ALLOCANZ(ir_node*, n_params + n_com + (n_Call_max+1));
597 new_in[pos++] = get_Call_mem(call);
598 new_in[pos++] = get_Call_ptr(call);
599 assert(pos == n_Call_max+1);
600 add_hidden_param(irg, n_com, &new_in[pos], entry, ctp);
603 /* copy all other parameters */
604 for (i = 0; i < n_params; ++i) {
605 ir_node *param = get_Call_param(call, i);
606 new_in[pos++] = param;
608 assert(pos == n_params+n_com+(n_Call_max+1));
609 set_irn_in(call, pos, new_in);
612 static ir_entity *create_compound_arg_entity(ir_graph *irg, ir_type *type)
614 ir_type *frame = get_irg_frame_type(irg);
615 ident *id = id_unique("$compound_param.%u");
616 ir_entity *entity = new_entity(frame, id, type);
618 * we could do some optimisations here and create a big union type for all
619 * different call types in a function */
623 static void fix_compound_params(cl_entry *entry, ir_type *ctp)
625 ir_node *call = entry->call;
626 dbg_info *dbgi = get_irn_dbg_info(call);
627 ir_node *mem = get_Call_mem(call);
628 ir_graph *irg = get_irn_irg(call);
629 ir_node *nomem = new_r_NoMem(irg);
630 ir_node *frame = get_irg_frame(irg);
631 size_t n_params = get_method_n_params(ctp);
634 for (i = 0; i < n_params; ++i) {
635 ir_type *type = get_method_param_type(ctp, i);
640 ir_entity *arg_entity;
641 if (!needs_lowering(type))
644 arg = get_Call_param(call, i);
645 arg_entity = create_compound_arg_entity(irg, type);
646 block = get_nodes_block(call);
647 sel = new_rd_simpleSel(dbgi, block, nomem, frame, arg_entity);
648 copyb = new_rd_CopyB(dbgi, block, mem, sel, arg, type);
649 mem = new_r_Proj(copyb, mode_M, pn_CopyB_M);
650 set_Call_param(call, i, sel);
652 set_Call_mem(call, mem);
655 static void fix_calls(wlk_env *env)
658 for (entry = env->cl_list; entry; entry = entry->next) {
659 ir_node *call = entry->call;
660 ir_type *ctp = get_Call_type(call);
661 ir_type *lowered_mtp = lower_mtp(env->flags, ctp);
662 set_Call_type(call, lowered_mtp);
664 if (entry->has_compound_param) {
665 fix_compound_params(entry, ctp);
667 if (entry->has_compound_ret) {
668 fix_compound_ret(entry, ctp);
674 * Transform a graph. If it has compound parameter returns,
675 * remove them and use the hidden parameter instead.
676 * If it calls methods with compound parameter returns, add hidden
679 * @param irg the graph to transform
681 static void transform_irg(compound_call_lowering_flags flags, ir_graph *irg)
683 ir_entity *ent = get_irg_entity(irg);
684 ir_type *mtp = get_entity_type(ent);
685 size_t n_ress = get_method_n_ress(mtp);
686 size_t n_params = get_method_n_params(mtp);
687 size_t n_param_com = 0;
689 ir_type *lowered_mtp, *tp, *ft;
692 ir_node **new_in, *ret, *endbl, *bl, *mem, *copy;
696 /* calculate the number of compound returns */
697 size_t n_ret_com = 0;
698 for (i = 0; i < n_ress; ++i) {
699 ir_type *type = get_method_res_type(mtp, i);
700 if (needs_lowering(type))
703 for (i = 0; i < n_params; ++i) {
704 ir_type *type = get_method_param_type(mtp, i);
705 if (needs_lowering(type))
710 fix_parameter_entities(irg, n_ret_com);
713 /* much easier if we have only one return */
714 normalize_one_return(irg);
716 /* This graph has a compound argument. Create a new type */
717 lowered_mtp = lower_mtp(flags, mtp);
718 set_entity_type(ent, lowered_mtp);
720 /* hidden arguments are added first */
721 env.arg_shift = n_ret_com;
723 /* we must only search for calls */
727 obstack_init(&env.obst);
730 env.lowered_mtp = lowered_mtp;
731 env.only_local_mem = true;
734 /* scan the code, fix argument numbers and collect calls. */
735 irg_walk_graph(irg, firm_clear_link, NULL, &env);
736 irg_walk_graph(irg, fix_args_and_collect_calls, NULL, &env);
738 if (n_param_com > 0 && !(flags & LF_DONT_LOWER_ARGUMENTS))
739 remove_compound_param_entities(irg);
742 if (env.cl_list != NULL) {
750 /* STEP 1: find the return. This is simple, we have normalized the graph. */
751 endbl = get_irg_end_block(irg);
753 for (idx = get_Block_n_cfgpreds(endbl) - 1; idx >= 0; --idx) {
754 ir_node *pred = get_Block_cfgpred(endbl, idx);
756 if (is_Return(pred)) {
762 /* in case of infinite loops, there might be no return */
765 * Now fix the Return node of the current graph.
770 * STEP 2: fix it. For all compound return values add a CopyB,
771 * all others are copied.
773 NEW_ARR_A(ir_node *, new_in, n_ress + 1);
775 bl = get_nodes_block(ret);
776 mem = get_Return_mem(ret);
778 ft = get_irg_frame_type(irg);
779 NEW_ARR_A(cr_pair, cr_opt, n_ret_com);
781 for (j = 1, i = k = 0; i < n_ress; ++i) {
782 ir_node *pred = get_Return_res(ret, i);
783 tp = get_method_res_type(mtp, i);
785 if (needs_lowering(tp)) {
786 ir_node *arg = get_irg_args(irg);
787 arg = new_r_Proj(arg, mode_P_data, k);
790 if (is_Unknown(pred)) {
791 /* The Return(Unknown) is the Firm construct for a
792 * missing return. Do nothing. */
795 * Sorrily detecting that copy-return is possible isn't
796 * that simple. We must check, that the hidden address
797 * is alias free during the whole function.
798 * A simple heuristic: all Loads/Stores inside
799 * the function access only local frame.
801 if (env.only_local_mem && is_compound_address(ft, pred)) {
802 /* we can do the copy-return optimization here */
803 cr_opt[n_cr_opt].ent = get_Sel_entity(pred);
804 cr_opt[n_cr_opt].arg = arg;
806 } else { /* copy-return optimization is impossible, do the copy. */
814 mem = new_r_Proj(copy, mode_M, pn_CopyB_M);
817 if (flags & LF_RETURN_HIDDEN) {
821 } else { /* scalar return value */
826 /* replace the in of the Return */
828 set_irn_in(ret, j, new_in);
834 irg_walk_graph(irg, NULL, do_copy_return_opt, cr_opt);
836 for (c = 0, n = ARR_LEN(cr_opt); c < n; ++c) {
837 free_entity(cr_opt[c].ent);
843 obstack_free(&env.obst, NULL);
846 static void lower_method_types(type_or_ent tore, void *env)
848 const compound_call_lowering_flags *flags
849 = (const compound_call_lowering_flags*)env;
851 /* fix method entities */
852 if (is_entity(tore.ent)) {
853 ir_entity *ent = tore.ent;
854 ir_type *tp = get_entity_type(ent);
855 ir_type *lowered = lower_mtp(*flags, tp);
856 set_entity_type(ent, lowered);
858 ir_type *tp = tore.typ;
860 /* fix pointer to methods */
861 if (is_Pointer_type(tp)) {
862 ir_type *points_to = get_pointer_points_to_type(tp);
863 ir_type *lowered_points_to = lower_mtp(*flags, points_to);
864 set_pointer_points_to_type(tp, lowered_points_to);
869 void lower_calls_with_compounds(compound_call_lowering_flags flags)
873 pointer_types = pmap_create();
874 lowered_mtps = pmap_create();
876 /* first step: Transform all graphs */
877 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
878 ir_graph *irg = get_irp_irg(i);
879 transform_irg(flags, irg);
882 /* second step: Lower all method types of visible entities */
883 type_walk(NULL, lower_method_types, &flags);
885 pmap_destroy(lowered_mtps);
886 pmap_destroy(pointer_types);