becopyheur4: Clean up co_mst_irn_init().
[libfirm] / ir / opt / proc_cloning.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief   Procedure cloning.
9  * @author  Beyhan Veliev, Michael Beck
10  * @brief
11  *
12  * The purpose is first to find and analyze functions, that are called
13  * with constant parameter(s).
14  * The second step is to optimize the function that are found from our
15  * analyze. Optimize mean to make a new function with parameters, that
16  * aren't be constant. The constant parameters of the function are placed
17  * in the function graph. They aren't be passed as parameters.
18  */
19 #include "config.h"
20
21 #include <string.h>
22
23 #include "iroptimize.h"
24 #include "tv.h"
25 #include "set.h"
26 #include "irprog_t.h"
27 #include "hashptr.h"
28 #include "irgwalk.h"
29 #include "analyze_irg_args.h"
30 #include "irprintf.h"
31 #include "ircons.h"
32 #include "irouts.h"
33 #include "irnode_t.h"
34 #include "irtools.h"
35 #include "irgmod.h"
36 #include "array_t.h"
37 #include "irpass_t.h"
38
39 /**
40  * This struct contains the information quadruple for a Call, which we need to
41  * decide if this function must be cloned.
42  */
43 typedef struct quadruple {
44         ir_entity *ent;     /**< The entity of our Call. */
45         size_t    pos;      /**< Position of a constant argument of our Call. */
46         ir_tarval *tv;      /**< The tarval of this argument if Const node. */
47         ir_node   **calls;  /**< The list of all calls with the same characteristics */
48 } quadruple_t;
49
50 /**
51  * The quadruplets are hold in a sorted list
52  */
53 typedef struct entry {
54         quadruple_t  q;      /**< the quadruple */
55         float        weight; /**< its weight */
56         struct entry *next;  /**< link to the next one */
57 } entry_t;
58
59 typedef struct q_set {
60         struct obstack obst;        /**< an obstack containing all entries */
61         pset           *map;        /**< a hash map containing the quadruples */
62         entry_t        *heavy_uses; /**< the ordered list of heavy uses */
63 } q_set;
64
65 /**
66  * Compare two quadruplets.
67  *
68  * @return zero if they are identically, non-zero else
69  */
70 static int entry_cmp(const void *elt, const void *key)
71 {
72         const entry_t *e1 = (const entry_t*)elt;
73         const entry_t *e2 = (const entry_t*)key;
74
75         return (e1->q.ent != e2->q.ent) || (e1->q.pos != e2->q.pos) || (e1->q.tv != e2->q.tv);
76 }
77
78 /**
79  * Hash an element of type entry_t.
80  *
81  * @param entry  The element to be hashed.
82  */
83 static unsigned hash_entry(const entry_t *entry)
84 {
85         return hash_ptr(entry->q.ent) ^ hash_ptr(entry->q.tv) ^ (unsigned)(entry->q.pos * 9);
86 }
87
88 /**
89  * Free memory associated with a quadruplet.
90  */
91 static void kill_entry(entry_t *entry)
92 {
93         if (entry->q.calls) {
94                 DEL_ARR_F(entry->q.calls);
95                 entry->q.calls = NULL;
96         }
97 }
98
99 /**
100  * Process a call node.
101  *
102  * @param call    A ir_node to be checked.
103  * @param callee  The entity of the callee
104  * @param hmap    The quadruple-set containing the calls with constant parameters
105  */
106 static void process_call(ir_node *call, ir_entity *callee, q_set *hmap)
107 {
108         entry_t *key, *entry;
109         ir_node *call_param;
110         size_t i, n_params;
111
112         n_params = get_Call_n_params(call);
113
114         /* TODO
115          * Beware: we cannot clone variadic parameters as well as the
116          * last non-variadic one, which might be needed for the va_start()
117          * magic
118          */
119
120         /* In this for loop we collect the calls, that have
121            an constant parameter. */
122         for (i = n_params; i > 0;) {
123                 call_param = get_Call_param(call, --i);
124                 if (is_Const(call_param)) {
125                         /* we have found a Call to collect and we save the informations,
126                            which we need.*/
127                         if (! hmap->map)
128                                 hmap->map = new_pset(entry_cmp, 8);
129
130                         key = OALLOC(&hmap->obst, entry_t);
131
132                         key->q.ent   = callee;
133                         key->q.pos   = i;
134                         key->q.tv    = get_Const_tarval(call_param);
135                         key->q.calls = NULL;
136                         key->weight  = 0.0F;
137                         key->next    = NULL;
138
139                         /* We insert our information in the set, where we collect the calls.*/
140                         entry = (entry_t*)pset_insert(hmap->map, key, hash_entry(key));
141
142                         if (entry != key)
143                                 obstack_free(&hmap->obst, key);
144
145                         /* add the call to the list */
146                         if (! entry->q.calls) {
147                                 entry->q.calls = NEW_ARR_F(ir_node *, 1);
148                                 entry->q.calls[0] = call;
149                         } else
150                                 ARR_APP1(ir_node *, entry->q.calls, call);
151                 }
152         }
153 }
154
155 /**
156  * Collect all calls in a ir_graph to a set.
157  *
158  * @param call   A ir_node to be checked.
159  * @param env   The quadruple-set containing the calls with constant parameters
160  */
161 static void collect_irg_calls(ir_node *call, void *env)
162 {
163         q_set *hmap = (q_set*)env;
164         ir_node *call_ptr;
165         ir_entity *callee;
166
167         /* We collect just "Call" nodes */
168         if (is_Call(call)) {
169                 call_ptr = get_Call_ptr(call);
170
171                 if (! is_SymConst_addr_ent(call_ptr))
172                         return;
173
174                 callee = get_SymConst_entity(call_ptr);
175
176                 /* we don't know which function gets finally bound to a weak symbol */
177                 if (get_entity_linkage(callee) & IR_LINKAGE_WEAK)
178                         return;
179
180                 /* we can only clone calls to existing entities */
181                 if (get_entity_irg(callee) == NULL)
182                         return;
183
184                 process_call(call, callee, hmap);
185         }
186 }
187
188 /**
189  * Make a name for a clone. The clone name is
190  * the name of the original method suffixed with "_cl_pos_nr".
191  * pos is the pos from our quadruplet and nr is a counter.
192  *
193  * @param id  The ident of the cloned function.
194  * @param pos The "pos" from our quadruplet.
195  * @param nr  A counter for the clones.
196  */
197 static ident *get_clone_ident(ident *id, size_t pos, size_t nr)
198 {
199         char clone_postfix[32];
200
201         ir_snprintf(clone_postfix, sizeof(clone_postfix), "_cl_%zu_%zu", pos, nr);
202
203         return id_mangle(id, new_id_from_str(clone_postfix));
204 }
205
206 /**
207  * Pre-Walker: Copies blocks and nodes from the original method graph
208  * to the cloned graph. Fixes the argument projection numbers for
209  * all arguments behind the removed one.
210  *
211  * @param irn  A node from the original method graph.
212  * @param env  The clone graph.
213  */
214 static void copy_nodes(ir_node *irn, void *env)
215 {
216         ir_graph *clone_irg = (ir_graph*)env;
217         ir_node  *arg       = (ir_node*)get_irg_link(clone_irg);
218         ir_node  *irg_args  = get_Proj_pred(arg);
219         ir_node  *irn_copy;
220         long      proj_nr;
221
222         /* Copy all nodes except the arg. */
223         if (irn != arg)
224                 copy_irn_to_irg(irn, clone_irg);
225
226         irn_copy = (ir_node*)get_irn_link(irn);
227
228         /* Fix argument numbers */
229         if (is_Proj(irn) && get_Proj_pred(irn) == irg_args) {
230                 proj_nr = get_Proj_proj(irn);
231                 if (get_Proj_proj(arg) < proj_nr)
232                         set_Proj_proj(irn_copy, proj_nr - 1);
233         }
234 }
235
236 /**
237  * Post-walker: Set the predecessors of the copied nodes.
238  * The copied nodes are set as link of their original nodes. The links of
239  * "irn" predecessors are the predecessors of copied node.
240  */
241 static void set_preds(ir_node *irn, void *env)
242 {
243         ir_graph *clone_irg = (ir_graph*)env;
244         ir_node  *arg       = (ir_node*)get_irg_link(clone_irg);
245         int       i;
246         ir_node  *irn_copy;
247         ir_node  *pred;
248
249         /* Arg is the method argument, that we have replaced by a constant.*/
250         if (arg == irn)
251                 return;
252
253         irn_copy = (ir_node*)get_irn_link(irn);
254
255         if (is_Block(irn)) {
256                 ir_graph *const irg = get_Block_irg(irn);
257                 for (i = get_Block_n_cfgpreds(irn) - 1; i >= 0; --i) {
258                         pred = get_Block_cfgpred(irn, i);
259                         /* "End" block must be handled extra, because it is not matured.*/
260                         if (get_irg_end_block(irg) == irn)
261                                 add_immBlock_pred(get_irg_end_block(clone_irg), (ir_node*)get_irn_link(pred));
262                         else
263                                 set_Block_cfgpred(irn_copy, i, (ir_node*)get_irn_link(pred));
264                 }
265         } else {
266                 /* First we set the block our copy if it is not a block.*/
267                 set_nodes_block(irn_copy, (ir_node*)get_irn_link(get_nodes_block(irn)));
268                 if (is_End(irn)) {
269                         /* Handle the keep-alives. This must be done separately, because
270                            the End node was NOT copied */
271                         for (i = 0; i < get_End_n_keepalives(irn); ++i)
272                                 add_End_keepalive(irn_copy, (ir_node*)get_irn_link(get_End_keepalive(irn, i)));
273                 } else {
274                         for (i = get_irn_arity(irn) - 1; i >= 0; i--) {
275                                 pred = get_irn_n(irn, i);
276                                 set_irn_n(irn_copy, i, (ir_node*)get_irn_link(pred));
277                         }
278                 }
279         }
280 }
281
282 /**
283  * Get the method argument at the position "pos".
284  *
285  * @param irg  irg that must be cloned.
286  * @param pos  The position of the argument.
287  */
288 static ir_node *get_irg_arg(ir_graph *irg, size_t pos)
289 {
290         ir_node *irg_args = get_irg_args(irg), *arg = NULL;
291
292         /* Call algorithm that computes the out edges */
293         assure_irg_outs(irg);
294
295         /* Search the argument with the number pos.*/
296         for (unsigned i = get_irn_n_outs(irg_args); i-- > 0; ) {
297                 ir_node *proj = get_irn_out(irg_args, i);
298                 if ((int)pos == get_Proj_proj(proj)) {
299                         if (arg) {
300                                 /*
301                                  * More than one arg node found:
302                                  * We rely on the fact that only one arg exists, so do
303                                  * a cheap CSE in this case.
304                                  */
305                                 set_irn_out(irg_args, i, arg, 0);
306                                 exchange(proj, arg);
307                         } else
308                                 arg = proj;
309                 }
310         }
311         assert(arg && "Argument not found");
312         return arg;
313 }
314
315 /**
316  * Create a new graph for the clone of the method,
317  * that we want to clone.
318  *
319  * @param ent The entity of the method that must be cloned.
320  * @param q   Our quadruplet.
321  */
322 static void create_clone_proc_irg(ir_entity *ent, const quadruple_t *q)
323 {
324         ir_graph *method_irg, *clone_irg;
325         ir_node *arg, *const_arg;
326
327         method_irg = get_entity_irg(ent);
328
329         /* We create the skeleton of the clone irg.*/
330         clone_irg  = new_ir_graph(ent, 0);
331
332         arg        = get_irg_arg(get_entity_irg(q->ent), q->pos);
333         /* we will replace the argument in position "q->pos" by this constant. */
334         const_arg  = new_r_Const(clone_irg, q->tv);
335
336         /* args copy in the cloned graph will be the const. */
337         set_irn_link(arg, const_arg);
338
339         /* Store the arg that will be replaced here, so we can easily detect it. */
340         set_irg_link(clone_irg, arg);
341
342         /* We copy the blocks and nodes, that must be in
343         the clone graph and set their predecessors. */
344         irg_walk_graph(method_irg, copy_nodes, set_preds, clone_irg);
345
346         /* The "cloned" graph must be matured. */
347         mature_immBlock(get_irg_end_block(clone_irg));
348         irg_finalize_cons(clone_irg);
349 }
350
351 /**
352  * The function create a new entity type
353  * for our clone and set it to clone entity.
354  *
355  * @param q   Contains information for the method to clone.
356  * @param ent The entity of the clone.
357  * @param nr  A pointer to the counter of clones.
358  **/
359 static void change_entity_type(const quadruple_t *q, ir_entity *ent)
360 {
361         ir_type *mtp, *new_mtp, *tp;
362         size_t  i, j, n_params, n_ress;
363
364         mtp      = get_entity_type(q->ent);
365         n_params = get_method_n_params(mtp);
366         n_ress   = get_method_n_ress(mtp);
367
368         /* Create the new type for our clone. It must have one parameter
369            less then the original.*/
370         new_mtp  = new_type_method(n_params - 1, n_ress);
371
372         /* We must set the type of the methods parameters.*/
373         for (i = j = 0; i < n_params; ++i) {
374                 if (i == q->pos) {
375                         /* This is the position of the argument, that we have
376                            replaced. */
377                         continue;
378                 }
379                 tp = get_method_param_type(mtp, i);
380                 set_method_param_type(new_mtp, j++, tp);
381         }
382         /* Copy the methods result types. */
383         for (i = 0; i < n_ress; ++i) {
384                 tp = get_method_res_type(mtp, i);
385                 set_method_res_type(new_mtp, i, tp);
386         }
387         set_entity_type(ent, new_mtp);
388 }
389
390 /**
391  * Make a clone of a method.
392  *
393  * @param q   Contains information for the method to clone.
394  */
395 static ir_entity *clone_method(const quadruple_t *q)
396 {
397         ir_entity *new_entity;
398         ident *clone_ident;
399         /* A counter for the clones.*/
400         static size_t nr = 0;
401
402         /* We get a new ident for our clone method.*/
403         clone_ident = get_clone_ident(get_entity_ident(q->ent), q->pos, nr);
404         /* We get our entity for the clone method. */
405         new_entity  = copy_entity_name(q->ent, clone_ident);
406
407         /* a cloned entity is always local */
408         set_entity_visibility(new_entity, ir_visibility_local);
409
410         /* set a ld name here: Should we mangle this ? */
411         set_entity_ld_ident(new_entity, get_entity_ident(new_entity));
412
413         /* set a new type here. */
414         change_entity_type(q, new_entity);
415
416         /* We need now a new ir_graph for our clone method. */
417         create_clone_proc_irg(new_entity, q);
418
419         /* The "new_entity" don't have this information. */
420         new_entity->attr.mtd_attr.param_access = NULL;
421         new_entity->attr.mtd_attr.param_weight = NULL;
422
423         return new_entity;
424 }
425
426 /**
427  * Creates a new "cloned" Call node and return it.
428  *
429  * @param call        The call that must be cloned.
430  * @param new_entity  The entity of the cloned function.
431  * @param pos         The position of the replaced parameter of this call.
432  **/
433 static ir_node *new_cl_Call(ir_node *call, ir_entity *new_entity, size_t pos)
434 {
435         ir_node **in;
436         size_t i, n_params, new_params = 0;
437         ir_node *callee;
438         symconst_symbol sym;
439         ir_graph *irg = get_irn_irg(call);
440         ir_node *bl = get_nodes_block(call);
441
442         sym.entity_p = new_entity;
443         callee = new_r_SymConst(irg, mode_P_code, sym, symconst_addr_ent);
444
445         n_params = get_Call_n_params(call);
446         NEW_ARR_A(ir_node *, in, n_params - 1);
447
448         /* we save the parameters of the new call in the array "in" without the
449          * parameter in position "pos", that is replaced with a constant.*/
450         for (i = 0; i < n_params; ++i) {
451                 if (pos != i)
452                         in[new_params++] = get_Call_param(call, i);
453         }
454         /* Create and return the new Call. */
455         return new_r_Call(bl, get_Call_mem(call),
456                 callee, n_params - 1, in, get_entity_type(new_entity));
457 }
458
459 /**
460  * Exchange all Calls stored in the quadruplet to Calls of the cloned entity.
461  *
462  * @param q             The quadruple
463  * @param cloned_ent    The entity of the new function that must be called
464  *                      from the new Call.
465  */
466 static void exchange_calls(quadruple_t *q, ir_entity *cloned_ent)
467 {
468         size_t pos = q->pos;
469         ir_node *new_call, *call;
470         size_t i;
471
472         /* We iterate the list of the "call".*/
473         for (i = 0; i < ARR_LEN(q->calls); ++i) {
474                 call = q->calls[i];
475
476                 /* A clone exist and the copy of "call" in this
477                  * clone graph must be exchanged with new one.*/
478                 new_call = new_cl_Call(call, cloned_ent, pos);
479                 exchange(call, new_call);
480         }
481 }
482
483 /**
484  * The weight formula:
485  * We save one instruction in every caller and param_weight instructions
486  * in the callee.
487  */
488 static float calculate_weight(const entry_t *entry)
489 {
490         return ARR_LEN(entry->q.calls) *
491                 (float)(get_method_param_weight(entry->q.ent, entry->q.pos) + 1);
492 }
493
494 /**
495  * After we exchanged all calls, some entries on the list for
496  * the next cloned entity may get invalid, so we have to check
497  * them and may even update the list of heavy uses.
498  */
499 static void reorder_weights(q_set *hmap, float threshold)
500 {
501         entry_t **adr, *p, *entry;
502         size_t i, len;
503
504 restart:
505         entry = hmap->heavy_uses;
506         if (! entry)
507                 return;
508
509         len = ARR_LEN(entry->q.calls);
510         for (i = 0; i < len; ++i) {
511                 ir_node *ptr, *call = entry->q.calls[i];
512
513                 /* might be exchanged, so skip Id nodes here. */
514                 call = skip_Id(call);
515
516                 /* we know, that a SymConst is here */
517                 ptr = get_Call_ptr(call);
518
519                 ir_entity *const callee = get_SymConst_entity(ptr);
520                 if (callee != entry->q.ent) {
521                         /*
522                          * This call is already changed because of a previous
523                          * optimization. Remove it from the list.
524                          */
525                         --len;
526                         entry->q.calls[i] = entry->q.calls[len];
527                         entry->q.calls[len] = NULL;
528
529                         /* the new call should be processed */
530                         process_call(call, callee, hmap);
531                         --i;
532                 }
533         }
534
535         /* the length might be changed */
536         ARR_SHRINKLEN(entry->q.calls, len);
537
538         /* recalculate the weight and resort the heavy uses map */
539         entry->weight = calculate_weight(entry);
540
541         if (len <= 0 || entry->weight < threshold) {
542                 hmap->heavy_uses = entry->next;
543                 kill_entry(entry);
544
545                 /* we have changed the list, check the next one */
546                 goto restart;
547         }
548
549         adr = NULL;
550         for (p = entry->next; p && entry->weight < p->weight; p = p->next) {
551                 adr = &p->next;
552         }
553
554         if (adr) {
555                 hmap->heavy_uses = entry->next;
556                 entry->next      = *adr;
557                 *adr             = entry;
558
559                 /* we have changed the list, check the next one */
560                 goto restart;
561         }
562 }
563
564 /*
565  * Do the procedure cloning. Evaluate a heuristic weight for every
566  * call(..., Const, ...). If the weight is bigger than threshold,
567  * clone the entity and fix the calls.
568  */
569 void proc_cloning(float threshold)
570 {
571         entry_t *p;
572         size_t i, n;
573         q_set hmap;
574
575         DEBUG_ONLY(firm_dbg_module_t *dbg;)
576
577         /* register a debug mask */
578         FIRM_DBG_REGISTER(dbg, "firm.opt.proc_cloning");
579
580         obstack_init(&hmap.obst);
581         hmap.map        = NULL;
582         hmap.heavy_uses = NULL;
583
584         /* initially fill our map by visiting all irgs */
585         for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
586                 ir_graph *irg = get_irp_irg(i);
587                 irg_walk_graph(irg, collect_irg_calls, NULL, &hmap);
588         }
589
590         /* We have the "Call" nodes to optimize in set "set_entries". Our algorithm
591            replace one constant parameter and make a new "Call" node for all found "Calls". It exchange the
592            old one with the new one and the algorithm is called with the new "Call".
593          */
594         while (hmap.map || hmap.heavy_uses) {
595                 /* We iterate the set and arrange the element of the set in a list.
596                    The elements are arranged dependent of their value descending.*/
597                 if (hmap.map) {
598                         foreach_pset(hmap.map, entry_t, entry) {
599                                 entry->weight = calculate_weight(entry);
600
601                                 /*
602                                  * Do not put entry with a weight < threshold in the list
603                                  */
604                                 if (entry->weight < threshold) {
605                                         kill_entry(entry);
606                                         continue;
607                                 }
608
609                                 /* put entry in the heavy uses list */
610                                 entry->next = NULL;
611                                 if (! hmap.heavy_uses)
612                                         hmap.heavy_uses = entry;
613                                 else {
614                                         if (entry->weight >= hmap.heavy_uses->weight) {
615                                                 entry->next     = hmap.heavy_uses;
616                                                 hmap.heavy_uses = entry;
617                                         } else {
618                                                 for (p = hmap.heavy_uses; p->next; p = p->next) {
619                                                         if (entry->weight >= p->next->weight) {
620                                                                 entry->next = p->next;
621                                                                 p->next     = entry;
622                                                                 break;
623                                                         }
624                                                 }
625                                                 if (! p->next)
626                                                         p->next = entry;
627                                         }
628                                 }
629                         }
630                         del_pset(hmap.map);
631                         hmap.map = NULL;
632                 }
633
634 #ifdef DEBUG_libfirm
635                 /* Print some information about the list. */
636                 DB((dbg, LEVEL_2, "-----------------\n"));
637                 for (entry_t *entry = hmap.heavy_uses; entry; entry = entry->next) {
638                         DB((dbg, LEVEL_2, "\nweight: is %f\n", entry->weight));
639                         DB((dbg, LEVEL_2, "Call for Method %E\n", entry->q.ent));
640                         DB((dbg, LEVEL_2, "Position %zu\n", entry->q.pos));
641                         DB((dbg, LEVEL_2, "Value %T\n", entry->q.tv));
642                 }
643 #endif
644                 entry_t *const entry = hmap.heavy_uses;
645                 if (entry) {
646                         quadruple_t *qp = &entry->q;
647
648                         ir_entity *ent = clone_method(qp);
649                         DB((dbg, LEVEL_1, "Cloned <%+F, %zu, %T> info %+F\n", qp->ent, qp->pos, qp->tv, ent));
650
651                         hmap.heavy_uses = entry->next;
652
653                         /* We must exchange the copies of this call in all clones too.*/
654                         exchange_calls(&entry->q, ent);
655                         kill_entry(entry);
656
657                         /*
658                          * after we exchanged all calls, some entries on the list for
659                          * the next cloned entity may get invalid, so we have to check
660                          * them and may even update the list of heavy uses.
661                          */
662                         reorder_weights(&hmap, threshold);
663                 }
664         }
665         obstack_free(&hmap.obst, NULL);
666 }
667
668 typedef struct pass_t {
669         ir_prog_pass_t pass;
670         float          threshold;
671 } pass_t;
672
673 /**
674  * Wrapper to run proc_cloning() as an ir_prog pass.
675  */
676 static int proc_cloning_wrapper(ir_prog *irp, void *context)
677 {
678         pass_t *pass = (pass_t*)context;
679
680         (void)irp;
681         proc_cloning(pass->threshold);
682         return 0;
683 }
684
685 /* create a ir_prog pass */
686 ir_prog_pass_t *proc_cloning_pass(const char *name, float threshold)
687 {
688         pass_t *pass = XMALLOCZ(pass_t);
689
690         pass->threshold = threshold;
691         return def_prog_pass_constructor(
692                 &pass->pass, name ? name : "cloning", proc_cloning_wrapper);
693 }