35551690ab6eac6192c994fe619c9212cf4a01d0
[libfirm] / ir / be / becopyopt.h
1 /**
2  * Author:      Daniel Grund
3  * Date:                12.04.2005
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  *
7  * Header for copy optimization problem. Analysis and set up of the problem.
8  */
9
10 #ifndef _BECOPYOPT_H
11 #define _BECOPYOPT_H
12
13 #include "debug.h"
14 #include "obst.h"
15 #include "list.h"
16 #include "set.h"
17 #include "pset.h"
18 #include "bitset.h"
19
20 #include "irgraph.h"
21 #include "irgwalk.h"
22 #include "irnode.h"
23 #include "irdom.h"
24 #include "irouts.h"
25
26 #include "beutil.h"
27 #include "benumb_t.h"
28 #include "belive_t.h"
29 #include "bechordal_t.h"
30 #include "bearch.h"
31
32 #define DEBUG_IRG "MergeSort.c__merge__datab"
33 #define DEBUG_IRG_LVL_CO   SET_LEVEL_1
34 #define DEBUG_IRG_LVL_HEUR SET_LEVEL_1
35 #define DEBUG_IRG_LVL_ILP  SET_LEVEL_1
36 #define DEBUG_LVL_CO   SET_LEVEL_0
37 #define DEBUG_LVL_HEUR SET_LEVEL_0
38 #define DEBUG_LVL_ILP  SET_LEVEL_0
39
40 typedef int(*cost_fct_t)(ir_node*, ir_node*, int);
41
42 /**
43  * Data representing the problem of copy minimization.
44  */
45 typedef struct _copy_opt_t {
46         be_chordal_env_t *chordal_env;
47         char *name;                                             /**< ProgName__IrgName__RegClass */
48         struct list_head units;                 /**< all units to optimize in right order */
49         pset *roots;                                    /**< used only temporary for detecting multiple appends */
50         cost_fct_t get_costs;                   /**< function ptr used to get costs for copies */
51         struct obstack ob;
52 } copy_opt_t;
53
54 /**
55  * A single unit of optimization. Lots of these form a copy-opt problem
56  */
57 typedef struct _unit_t {
58         struct list_head units;         /**< chain for all units */
59         copy_opt_t *co;                         /**< the copy_opt this unit belongs to */
60         int node_count;                         /**< size of the nodes array */
61         ir_node **nodes;                        /**< [0] is the root-node, others are non interfering args of it. */
62         int *costs;                                     /**< costs[i] are arising, if nodes[i] has a different color */
63         int complete_costs;                     /**< sum of all costs[i] */
64         int minimal_costs;                      /**< a lower bound for this ou, considering only ifg (not coloring conflicts) */
65
66         int sort_key;                           /**< maximum costs. controls the order of ou's. */
67
68         /* for heuristic */
69         struct list_head queue;         /**< list of (mis/color) sorted by size of mis */
70 } unit_t;
71
72 /* Helpers */
73 #define set_irn_col(co, irn, col) \
74         arch_set_irn_register(co->chordal_env->arch_env, irn, 0, arch_register_for_index(co->chordal_env->cls, col))
75
76 #define get_irn_col(co, irn) \
77         arch_register_get_index(arch_get_irn_register(co->chordal_env->arch_env, irn, 0))
78
79 #define list_entry_units(lh) list_entry(lh, unit_t, units)
80
81
82 /**
83  * Generate the problem. Collect all infos and optimizable nodes.
84  */
85 copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, int (*get_costs)(ir_node*, ir_node*, int));
86
87 /**
88  * Free the space...
89  */
90 void free_copy_opt(copy_opt_t *co);
91
92
93 #define is_Perm(arch_env, irn) (arch_irn_classify(arch_env, irn) == arch_irn_class_perm)
94
95 /**
96  * A copy is a proj haning out of perm node
97  */
98 #define is_Copy(arch_env, irn) (is_Proj(irn) && is_Perm(arch_env, get_Proj_pred(irn)))
99
100 /**
101  * returns the corresponding argument of the perm node for a copy
102  */
103 #define get_Copy_src(irn) (get_irn_n(get_Proj_pred(irn), get_Proj_proj(irn)))
104
105 /**
106  * Checks if a node is optimizable, viz. is a target of a 'copy-op'
107  */
108 #define is_optimizable(arch_env, irn) (is_Phi(irn) || is_Copy(arch_env, irn))
109
110 /**
111  * Checks if the irn is a non-interfering argument of a node which 'is_optimizable'
112  */
113 int is_optimizable_arg(const copy_opt_t *co, ir_node *irn);
114
115 /**
116  * Computes the costs of a copy according to loop depth
117  * @param root, arg: clear.
118  * @param pos:  -1 for perm-copies.
119  *                              Else the argument position of arg in the phi node root.
120  * @return Must be >= 0 in all cases.
121  */
122 int get_costs_loop_depth(ir_node *root, ir_node* arg, int pos);
123
124 /**
125  * All costs equal 1. Using this will reduce the number of copies.
126  * @return Must be >= 0 in all cases.
127  */
128 int get_costs_all_one(ir_node *root, ir_node* arg, int pos);
129
130 /**
131  * Returns the current costs the copies are causing
132  */
133 int co_get_copy_costs(const copy_opt_t *co);
134
135 /**
136  * Returns a lower bound for the costs of copies based on interfering
137  * arguments and the size of a max indep. set (only ifg-edges) of the other args.
138  */
139 int co_get_lower_bound(const copy_opt_t *co);
140
141 /**
142  * Solves the problem using a heuristic approach
143  */
144 void co_heur_opt(copy_opt_t *co);
145
146 /**
147  * Solves the problem using mixed integer programming
148  */
149 void co_ilp_opt(copy_opt_t *co);
150
151 #endif