3 * File name: ir/opt/proc_cloning.c
4 * Purpose: procedure cloning
5 * Author: Beyhan Veliev
8 * Copyright: (c) 1998-2005 Universität Karlsruhe
9 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
13 * @file proc_cloning.c
15 * The purpose is first to find and analyze functions, that are called
16 * with constant parameter(s).
17 * The second step is to optimize the function that are found from our
18 * analyze. Optimize mean to make a new function with parameters, that
19 * aren't be constant. The constant parameters of the function are placed
20 * in the function graph. They aren't be passed as parameters.
37 #include "proc_cloning.h"
38 #include "analyze_irg_args.h"
49 /* A macro to iterate sets.*/
50 #define ITERATE_SET(set_entries, entry) for(entry = set_first(set_entries); entry; entry = set_next(set_entries))
53 * This struct contains the information quadruple for a Call, which we need to
54 * decide if this function must be cloned.
56 typedef struct quadruple {
57 ir_entity *ent; /**< The entity of our Call. */
58 int pos; /**< Position of a constant argument of our Call. */
59 tarval *tv; /**< The tarval of this argument if Const node. */
60 ir_node **calls; /**< The list of all calls with the same characteristics */
64 * The quadruplets are hold in a sorted list
66 typedef struct entry {
67 quadruple_t q; /**< the quadruple */
68 float weight; /**< its weight */
69 struct entry *next; /**< link to the next one */
72 typedef struct q_set {
73 struct obstack obst; /**< an obstack containing all entries */
74 pset *map; /**< a hash map containing the quadruples */
75 entry_t *heavy_uses; /**< the ordered list of heavy uses */
79 * Compare two quadruplets.
81 * @return zero if they are identically, non-zero else
83 static int entry_cmp(const void *elt, const void *key)
85 const entry_t *e1 = elt;
86 const entry_t *e2 = key;
88 return (e1->q.ent != e2->q.ent) || (e1->q.pos != e2->q.pos) || (e1->q.tv != e2->q.tv);
92 * Hash an element of type entry_t.
94 * @param entry The element to be hashed.
96 static int hash_entry(const entry_t *entry)
98 return HASH_PTR(entry->q.ent) ^ HASH_PTR(entry->q.tv) ^ (entry->q.pos * 9);
102 * Free memory associated with a quadruplet.
104 static void kill_entry(entry_t *entry) {
105 if (entry->q.calls) {
106 DEL_ARR_F(entry->q.calls);
107 entry->q.calls = NULL;
112 * Process a call node.
114 * @param call A ir_node to be checked.
115 * @param callee The entity of the callee
116 * @param hmap The quadruple-set containing the calls with constant parameters
118 static void process_call(ir_node *call, ir_entity *callee, q_set *hmap)
121 entry_t *key, *entry;
125 n_params = get_Call_n_params(call);
127 /* Beware: we cannot clone variadic parameters as well as the
128 * last non-variadic one, which might be needed for the va_start()
131 mtp = get_Call_type(call);
132 if (get_method_variadicity(mtp) != variadicity_non_variadic) {
133 n_params = get_method_first_variadic_param_index(mtp) - 1;
136 /* In this for loop we collect the calls, that have
137 an constant parameter. */
138 for (i = n_params - 1; i >= 0; --i) {
139 call_param = get_Call_param(call, i);
140 if (is_Const(call_param)) {
141 /* we have found a Call to collect and we save the informations,
144 hmap->map = new_pset(entry_cmp, 8);
146 key = obstack_alloc(&hmap->obst, sizeof(*key));
150 key->q.tv = get_Const_tarval(call_param);
155 /* We insert our information in the set, where we collect the calls.*/
156 entry = pset_insert(hmap->map, key, hash_entry(key));
159 obstack_free(&hmap->obst, key);
161 /* add the call to the list */
162 if (! entry->q.calls) {
163 entry->q.calls = NEW_ARR_F(ir_node *, 1);
164 entry->q.calls[0] = call;
167 ARR_APP1(ir_node *, entry->q.calls, call);
173 * Collect all calls in a ir_graph to a set.
175 * @param call A ir_node to be checked.
176 * @param env The quadruple-set containing the calls with constant parameters
178 static void collect_irg_calls(ir_node *call, void *env)
184 /* We collect just "Call" nodes */
186 call_ptr = get_Call_ptr(call);
188 /* Call pointer must be a SymConst*/
189 if (op_SymConst != get_irn_op(call_ptr))
191 /* Call pointer must be the address of an entity.*/
192 if (get_SymConst_kind(call_ptr) != symconst_addr_ent)
195 callee = get_SymConst_entity(call_ptr);
197 /* we can only clone calls to existing entities */
198 if (get_entity_visibility(callee) == visibility_external_allocated)
201 process_call(call, callee, hmap);
206 * Make a name for a clone. The clone name is
207 * the name of the original method suffixed with "_cl_pos_nr".
208 * pos is the pos from our quadruplet and nr is a counter.
210 * @param id The ident of the cloned function.
211 * @param pos The "pos" from our quadruplet.
212 * @param nr A counter for the clones.
214 static ident *get_clone_ident(ident *id, int pos, unsigned nr)
216 char clone_postfix[32];
218 snprintf(clone_postfix, sizeof(clone_postfix), "_cl_%d_%u", pos, nr);
220 return mangle(id, new_id_from_str(clone_postfix));
224 * Pre-Walker: Copies blocks and nodes from the original method graph
225 * to the cloned graph. Fixes the argument projection numbers for
226 * all arguments behind the removed one.
228 * @param irn A node from the original method graph.
229 * @param env The clone graph.
231 static void copy_nodes(ir_node *irn, void *env)
233 ir_node *arg, *irg_args, *irn_copy;
235 ir_graph *clone_irg = env;
237 arg = get_irg_link(clone_irg);
238 irg_args = get_Proj_pred(arg);
240 /* Copy all nodes except the arg. */
242 copy_irn_to_irg(irn, clone_irg);
244 irn_copy = get_irn_link(irn);
246 /* Fix argument numbers */
247 if (is_Proj(irn) && get_Proj_pred(irn) == irg_args) {
248 proj_nr = get_Proj_proj(irn);
249 if (get_Proj_proj(arg) < proj_nr)
250 set_Proj_proj(irn_copy, proj_nr - 1);
255 * Post-walker: Set the predecessors of the copied nodes.
256 * The copied nodes are set as link of their original nodes. The links of
257 * "irn" predecessors are the predecessors of copied node.
259 static void set_preds(ir_node *irn, void *env)
262 ir_node *irn_copy, *pred, *arg;
263 ir_graph *clone_irg = env;
265 arg = get_irg_link(clone_irg);
266 /* Arg is the method argument, that we have replaced by a constant.*/
270 irn_copy = get_irn_link(irn);
273 for (i = get_Block_n_cfgpreds(irn) - 1; i >= 0; i--) {
274 pred = get_Block_cfgpred(irn, i);
275 /* "End" block must be handled extra, because it is not matured.*/
276 if (get_irg_end_block(current_ir_graph) == irn)
277 add_immBlock_pred(get_irg_end_block(clone_irg), get_irn_link(pred));
279 set_Block_cfgpred(irn_copy, i, get_irn_link(pred));
283 /* First we set the block our copy if it is not a block.*/
284 set_nodes_block(irn_copy, get_irn_link(get_nodes_block(irn)));
285 if (get_irn_op(irn) == op_End) {
286 /* Handle the keep-alives. This must be done separately, because
287 the End node was NOT copied */
288 for (i = 0; i < get_End_n_keepalives(irn); ++i)
289 add_End_keepalive(irn_copy, get_irn_link(get_End_keepalive(irn, i)));
292 for (i = get_irn_arity(irn) - 1; i >= 0; i--) {
293 pred = get_irn_n(irn, i);
294 set_irn_n(irn_copy, i, get_irn_link(pred));
301 * Get the method argument at the position "pos".
303 * @param irg irg that must be cloned.
304 * @param pos The position of the argument.
306 static ir_node *get_irg_arg(ir_graph *irg, int pos)
308 ir_node *irg_args = get_irg_args(irg), *arg = NULL;
311 /* Call algorithm that computes the out edges */
312 if (get_irg_outs_state(irg) != outs_consistent)
313 compute_irg_outs(irg);
315 /* Search the argument with the number pos.*/
316 for (i = get_irn_n_outs(irg_args) - 1; i >= 0; --i) {
317 ir_node *proj = get_irn_out(irg_args, i);
318 if (pos == get_Proj_proj(proj)) {
321 * More than one arg node found:
322 * We rely on the fact the only one arg exists, so do
323 * a cheap CSE in this case.
325 set_irn_out(irg_args, i, arg);
332 assert(arg && "Argument not found");
337 * Create a new graph for the clone of the method,
338 * that we want to clone.
340 * @param ent The entity of the method that must be cloned.
341 * @param q Our quadruplet.
343 static void create_clone_proc_irg(ir_entity *ent, quadruple_t *q)
345 ir_graph *method_irg, *clone_irg;
346 ir_node *arg, *const_arg;
348 method_irg = get_entity_irg(ent);
350 /* We create the skeleton of the clone irg.*/
351 clone_irg = new_ir_graph(ent, 0);
353 arg = get_irg_arg(get_entity_irg(q->ent), q->pos);
354 /* we will replace the argument in position "q->pos" by this constant. */
355 const_arg = new_r_Const_type(
356 clone_irg, get_nodes_block(arg), get_irn_mode(arg), q->tv,
357 get_method_param_type(get_entity_type(q->ent), q->pos));
359 /* args copy in the cloned graph will be the const. */
360 set_irn_link(arg, const_arg);
362 /* Store the arg that will be replaced here, so we can easily detect it. */
363 set_irg_link(clone_irg, arg);
365 /* We copy the blocks and nodes, that must be in
366 the clone graph and set their predecessors. */
367 irg_walk_graph(method_irg, copy_nodes, set_preds, clone_irg);
369 /* The "cloned" graph must be matured. */
370 mature_immBlock(get_irg_end_block(clone_irg));
371 irg_finalize_cons(clone_irg);
375 * The function create a new entity type
376 * for our clone and set it to clone entity.
378 * @param q Contains information for the method to clone.
379 * @param ent The entity of the clone.
380 * @param nr A pointer to the counter of clones.
382 static void change_entity_type(quadruple_t *q, ir_entity *ent, unsigned *nr)
384 ir_type *mtp, *new_mtp, *tp;
386 int i, j, n_params, n_ress;
388 mtp = get_entity_type(q->ent);
389 tp_name = get_clone_ident(get_type_ident(mtp), q->pos, (*nr)++);
390 n_params = get_method_n_params(mtp);
391 n_ress = get_method_n_ress(mtp);
393 /* Create the new type for our clone. It must have one parameter
394 less then the original.*/
395 new_mtp = new_type_method(tp_name, n_params - 1, n_ress);
397 /* We must set the type of the methods parameters.*/
398 for (i = j = 0; i < n_params; ++i) {
400 /* This is the position of the argument, that we have
404 tp = get_method_param_type(mtp, i);
405 set_method_param_type(new_mtp, j++, tp);
407 /* Copy the methods result types. */
408 for (i = 0; i < n_ress; ++i) {
409 tp = get_method_res_type(mtp, i);
410 set_method_res_type(new_mtp, i, tp);
412 set_entity_type(ent, new_mtp);
416 * Make a clone of a method.
418 * @param q Contains information for the method to clone.
420 static ir_entity *clone_method(quadruple_t *q)
422 ir_entity *new_entity;
426 /* A counter for the clones.*/
427 static unsigned nr = 0;
429 /* We get a new ident for our clone method.*/
430 clone_ident = get_clone_ident(get_entity_ident(q->ent), q->pos, nr);
431 /* We get our entity for the clone method. */
432 new_entity = copy_entity_name(q->ent, clone_ident);
434 /* a cloned entity is always local */
435 set_entity_visibility(new_entity, visibility_local);
437 /* set a ld name here: Should we mangle this ? */
438 set_entity_ld_ident(new_entity, get_entity_ident(new_entity));
440 /* set a new type here. */
441 change_entity_type(q, new_entity, &nr);
443 /* We need now a new ir_graph for our clone method. */
444 create_clone_proc_irg(new_entity, q);
446 /* We must set the atomic value of our "new_entity". */
447 sym.entity_p = new_entity;
448 rem = current_ir_graph;
449 current_ir_graph = get_const_code_irg();
450 new_entity->value = new_SymConst(sym, symconst_addr_ent);
451 current_ir_graph = rem;
453 /* The "new_entity" don't have this information. */
454 new_entity->attr.mtd_attr.param_access = NULL;
455 new_entity->attr.mtd_attr.param_weight = NULL;
461 * Creates a new "cloned" Call node and return it.
463 * @param call The call that must be cloned.
464 * @param new_entity The entity of the cloned function.
465 * @param pos The position of the replaced parameter of this call.
467 static ir_node *new_cl_Call(ir_node *call, ir_entity *new_entity, int pos)
471 int i, n_params, new_params = 0;
474 ir_graph *irg = get_irn_irg(call);
475 ir_node *bl = get_nodes_block(call);
477 sym.entity_p = new_entity;
478 callee = new_r_SymConst(irg, bl, sym, symconst_addr_ent);
480 mtp = get_entity_type(new_entity);
481 n_params = get_Call_n_params(call);
482 NEW_ARR_A(ir_node *, in, n_params - 1);
484 /* we save the parameters of the new call in the array "in" without the
485 * parameter in position "pos", that is replaced with a constant.*/
486 for (i = 0; i < n_params; i++){
488 in[new_params++] = get_Call_param(call, i);
490 /* Create and return the new Call. */
491 return new_r_Call(irg, bl, get_Call_mem(call),
492 callee, n_params - 1, in, get_entity_type(new_entity));
496 * Exchange all Calls stored in the quadruplet to Calls of the cloned entity.
498 * @param q The quadruple
499 * @param cloned_ent The entity of the new function that must be called
502 static void exchange_calls(quadruple_t *q, ir_entity *cloned_ent)
505 ir_node *new_call, *call;
508 /* We iterate the list of the "call".*/
509 for (i = 0; i < ARR_LEN(q->calls); ++i) {
512 /* A clone exist and the copy of "call" in this
513 * clone graph must be exchanged with new one.*/
514 new_call = new_cl_Call(call, cloned_ent, pos);
515 exchange(call, new_call);
520 * The weight formula:
521 * We save one instruction in every caller and param_weight instructions
524 static float calculate_weight(const entry_t *entry) {
525 return ARR_LEN(entry->q.calls) *
526 (get_method_param_weight(entry->q.ent, entry->q.pos) + 1);
530 * After we exchanged all calls, some entries on the list for
531 * the next cloned entity may get invalid, so we have to check
532 * them and may even update the list of heavy uses.
534 static void reorder_weights(q_set *hmap, float threshold)
536 entry_t **adr, *p, *entry;
541 entry = hmap->heavy_uses;
545 len = ARR_LEN(entry->q.calls);
546 for (i = 0; i < len; ++i) {
547 ir_node *ptr, *call = entry->q.calls[i];
549 /* might be exchanged, so skip Id nodes here. */
550 call = skip_Id(call);
552 /* we know, that a SymConst is here */
553 ptr = get_Call_ptr(call);
554 assert(get_irn_op(ptr) == op_SymConst);
556 callee = get_SymConst_entity(ptr);
557 if (callee != entry->q.ent) {
559 * This call is already changed because of a previous
560 * optimization. Remove it from the list.
563 entry->q.calls[i] = entry->q.calls[len];
564 entry->q.calls[len] = NULL;
566 /* the new call should be processed */
567 process_call(call, callee, hmap);
572 /* the length might be changed */
573 ARR_SHRINKLEN(entry->q.calls, len);
575 /* recalculate the weight and resort the heavy uses map */
576 entry->weight = calculate_weight(entry);
578 if (len <= 0 || entry->weight < threshold) {
579 hmap->heavy_uses = entry->next;
582 /* we have changed the list, check the next one */
587 for (p = entry->next; p && entry->weight < p->weight; p = p->next) {
592 hmap->heavy_uses = entry->next;
595 entry = hmap->heavy_uses;
597 /* we have changed the list, check the next one */
603 * Do the procedure cloning. Evaluate a heuristic weight for every
604 * call(..., Const, ...). If the weight is bigger than threshold,
605 * clone the entity and fix the calls.
607 void proc_cloning(float threshold)
609 entry_t *entry = NULL, *p;
614 obstack_init(&hmap.obst);
616 hmap.heavy_uses = NULL;
618 /* initially fill our map by visiting all irgs */
619 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
620 irg = get_irp_irg(i);
621 irg_walk_graph(irg, collect_irg_calls, NULL, &hmap);
624 /* We have the "Call" nodes to optimize in set "set_entries". Our algorithm
625 replace one constant parameter and make a new "Call" node for all found "Calls". It exchange the
626 old one with the new one and the algorithm is called with the new "Call".
628 while (hmap.map || hmap.heavy_uses) {
629 /* We iterate the set and arrange the element of the set in a list.
630 The elements are arranged dependent of their value descending.*/
632 for (entry = pset_first(hmap.map); entry; entry = pset_next(hmap.map)) {
633 entry->weight = calculate_weight(entry);
636 * Do not put entry with a weight < threshold in the list
638 if (entry->weight < threshold) {
643 /* put entry in the heavy uses list */
645 if (! hmap.heavy_uses)
646 hmap.heavy_uses = entry;
648 if (entry->weight >= hmap.heavy_uses->weight) {
649 entry->next = hmap.heavy_uses;
650 hmap.heavy_uses = entry;
653 for (p = hmap.heavy_uses; p->next; p = p->next) {
654 if (entry->weight >= p->next->weight) {
655 entry->next = p->next;
669 /* Print some information about the list. */
670 printf("-----------------\n");
671 for (entry = hmap.heavy_uses; entry; entry = entry->next) {
672 printf("\nweight: is %f\n", entry->weight);
673 ir_printf("Call for Method %E\n", entry->q.ent);
674 printf("Position %i\n", entry->q.pos);
675 ir_printf("Value %T\n", entry->q.tv);
678 entry = hmap.heavy_uses;
680 ir_entity *ent = clone_method(&entry->q);
682 hmap.heavy_uses = entry->next;
684 /* We must exchange the copies of this call in all clones too.*/
685 exchange_calls(&entry->q, ent);
689 * after we exchanged all calls, some entries on the list for
690 * the next cloned entity may get invalid, so we have to check
691 * them and may even update the list of heavy uses.
693 reorder_weights(&hmap, threshold);
696 obstack_free(&hmap.obst, NULL);