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