701f00bdf9ebba10822c381e4d25d416f88a7787
[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 "!!deflate.c__longest_match__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 #define MIS_HEUR_TRIGGER 8
41
42
43 typedef int(*cost_fct_t)(ir_node*, ir_node*, int);
44
45 /**
46  * Data representing the problem of copy minimization.
47  */
48 typedef struct _copy_opt_t {
49         be_chordal_env_t *chordal_env;
50         char *name;                                             /**< ProgName__IrgName__RegClass */
51         struct list_head units;                 /**< all units to optimize in specific order */
52         cost_fct_t get_costs;                   /**< function ptr used to get costs for copies */
53         struct obstack ob;
54 } copy_opt_t;
55
56 /**
57  * A single unit of optimization. Lots of these form a copy-opt problem
58  */
59 typedef struct _unit_t {
60         struct list_head units;         /**< chain for all units */
61         copy_opt_t *co;                         /**< the copy_opt this unit belongs to */
62         int node_count;                         /**< size of the nodes array */
63         ir_node **nodes;                        /**< [0] is the root-node, others are non interfering args of it. */
64         int *costs;                                     /**< costs[i] are arising, if nodes[i] has a different color */
65         int inevitable_costs;           /**< sum of costs of all args interfering with root */
66         int all_nodes_costs;            /**< sum of all costs[i] */
67         int min_nodes_costs;            /**< a lower bound for the costs in costs[], determined by a max indep. set */
68         int sort_key;                           /**< maximum costs. controls the order of ou's in the struct list_head units. */
69
70         /* for heuristic */
71         struct list_head queue;         /**< list of qn's sorted by weight of qn-mis */
72 } unit_t;
73
74 /* Helpers */
75 #define get_arch_env(co) ((co)->chordal_env->main_env->arch_env)
76 #define get_irg(co)      ((co)->chordal_env->irg)
77 #define get_irn_col(co, irn) \
78         arch_register_get_index(arch_get_irn_register(get_arch_env(co), irn))
79 #define set_irn_col(co, irn, col) \
80         arch_set_irn_register(get_arch_env(co), irn, arch_register_for_index(co->chordal_env->cls, col))
81
82
83 #define list_entry_units(lh) list_entry(lh, unit_t, units)
84
85
86 /**
87  * Generate the problem. Collect all infos and optimizable nodes.
88  */
89 copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, int (*get_costs)(ir_node*, ir_node*, int));
90
91 /**
92  * Free the space...
93  */
94 void free_copy_opt(copy_opt_t *co);
95
96 #define get_Copy_src(irn) (get_irn_n(get_Proj_pred(irn), get_Proj_proj(irn)))
97 #define is_Perm(arch_env, irn)                          (arch_irn_classify(arch_env, irn) == arch_irn_class_perm)
98
99 #define is_Reg_Phi(irn)                                         (is_Phi(irn) && mode_is_data(get_irn_mode(irn)))
100 #define is_Copy(arch_env, irn)                          (is_Proj(irn) && is_Perm(arch_env, get_Proj_pred(irn)))
101 #define is_2addr_code(arch_env, irn, req)       (arch_get_register_req(arch_env, req, irn, -1)->type == arch_register_req_type_should_be_same)
102
103 /**
104  * Checks if a node is optimizable, viz. has somthing to do with coalescing
105  * @param arch The architecture environment
106  * @param irn  The irn to check
107  * @param req  A register_requirement structure (used to check for 2-addr-code)
108  */
109 #define is_optimizable(arch, irn, req) (is_Reg_Phi(irn) || is_Copy(arch, irn) || is_2addr_code(arch, irn, req))
110
111 /**
112  * Checks if the irn is a non-interfering argument of a node which 'is_optimizable'
113  */
114 int is_optimizable_arg(const copy_opt_t *co, ir_node *irn);
115
116 /**
117  * Computes the costs of a copy according to loop depth
118  * @param pos:  the argument position of arg in the root arguments
119  * @return Must be >= 0 in all cases.
120  */
121 int get_costs_loop_depth(ir_node *root, ir_node* arg, int pos);
122
123 /**
124  * All costs equal 1. Using this will reduce the number of copies.
125  * @return Must be >= 0 in all cases.
126  */
127 int get_costs_all_one(ir_node *root, ir_node* arg, int pos);
128
129 /**
130  * Returns the maximal costs possible, i.e. the costs if all
131  * pairs would be assigned different registers.
132  */
133 int co_get_max_copy_costs(const copy_opt_t *co);
134
135 /**
136  * Returns the inevitable costs, i.e. the costs of
137  * all copy pairs which interfere.
138  */
139 int co_get_inevit_copy_costs(const copy_opt_t *co);
140
141 /**
142  * Returns the current costs the copies are causing.
143  * The result includes inevitable costs and the costs
144  * of the copies regarding the current register allocation
145  */
146 int co_get_copy_costs(const copy_opt_t *co);
147
148 /**
149  * Returns a lower bound for the costs of copies in this ou.
150  * The result includes inevitable costs and the costs of a
151  * minimal costs caused by the nodes of the ou.
152  */
153 int co_get_lower_bound(const copy_opt_t *co);
154
155 /**
156  * Solves the problem using a heuristic approach
157  */
158 void co_heur_opt(copy_opt_t *co);
159
160 /**
161  * Solves the problem using mixed integer programming
162  * @returns 1 iff solution state was optimal
163  */
164 int co_ilp_opt(copy_opt_t *co, double time_limit);
165
166 #endif