- Refactored finish/after_ra phases a bit, stacknode fixup and stack bias
[libfirm] / ir / be / becopyopt_t.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  * Internal header for copy optimization problem.
8  */
9
10 #ifndef _BECOPYOPT_T_H
11 #define _BECOPYOPT_T_H
12
13 #include <obstack.h>
14 #include "list.h"
15 #include "bearch.h"
16 #include "bechordal_t.h"
17 #include "becopyopt.h"
18 #include "benodesets.h"
19
20 /**
21  * Data representing the problem of copy minimization.
22  */
23 struct _copy_opt_t {
24         be_chordal_env_t *cenv;
25         const arch_register_class_t *cls;
26         const arch_env_t *aenv;
27         ir_graph *irg;
28         char *name;                                             /**< ProgName__IrgName__RegClassName */
29         cost_fct_t get_costs;                   /**< function ptr used to get costs for copies */
30
31         /** Representation as optimization units */
32         struct list_head units;                 /**< all units to optimize in specific order */
33
34         /** Representation in graph structure. Only build on demand */
35         struct obstack obst;
36         set *nodes;
37 };
38
39 /* Helpers */
40 #define ASSERT_OU_AVAIL(co)             assert((co)->units.next && "Representation as optimization-units not build")
41 #define ASSERT_GS_AVAIL(co)             assert((co)->nodes && "Representation as graph not build")
42
43 #define get_irn_col(co, irn)            arch_register_get_index(arch_get_irn_register((co)->aenv, irn))
44 #define set_irn_col(co, irn, col)       arch_set_irn_register((co)->aenv, irn, arch_register_for_index((co)->cls, col))
45 #define is_curr_reg_class(co, irn)      (arch_get_irn_reg_class((co)->aenv, irn, -1) == (co)->cls)
46
47 #define list_entry_units(lh) list_entry(lh, unit_t, units)
48
49 #define is_Reg_Phi(irn)                                         (is_Phi(irn) && mode_is_data(get_irn_mode(irn)))
50
51 #define get_Perm_src(irn)                   (get_irn_n(get_Proj_pred(irn), get_Proj_proj(irn)))
52 #define is_Perm(arch_env, irn)                          (arch_irn_classify(arch_env, irn) == arch_irn_class_perm)
53 #define is_Perm_Proj(arch_env, irn)                     (is_Proj(irn) && is_Perm(arch_env, get_Proj_pred(irn)))
54
55 #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)
56
57
58
59 /******************************************************************************
60    ____        _   _    _       _ _          _____ _
61   / __ \      | | | |  | |     (_) |        / ____| |
62  | |  | |_ __ | |_| |  | |_ __  _| |_ ___  | (___ | |_ ___  _ __ __ _  __ _  ___
63  | |  | | '_ \| __| |  | | '_ \| | __/ __|  \___ \| __/ _ \| '__/ _` |/ _` |/ _ \
64  | |__| | |_) | |_| |__| | | | | | |_\__ \  ____) | || (_) | | | (_| | (_| |  __/
65   \____/| .__/ \__|\____/|_| |_|_|\__|___/ |_____/ \__\___/|_|  \__,_|\__, |\___|
66         | |                                                            __/ |
67         |_|                                                           |___/
68  ******************************************************************************/
69
70 #define MIS_HEUR_TRIGGER 8
71
72 typedef struct _unit_t {
73         struct list_head units;         /**< chain for all units */
74         copy_opt_t *co;                         /**< the copy opt this unit belongs to */
75         int node_count;                         /**< size of the nodes array */
76         ir_node **nodes;                        /**< [0] is the root-node, others are non interfering args of it. */
77         int *costs;                                     /**< costs[i] are incurred, if nodes[i] has a different color */
78         int inevitable_costs;           /**< sum of costs of all args interfering with root */
79         int all_nodes_costs;            /**< sum of all costs[i] */
80         int min_nodes_costs;            /**< a lower bound for the costs in costs[], determined by a max independent set */
81         int sort_key;                           /**< maximum costs. controls the order of ou's in the struct list_head units. */
82
83         /* for heuristic */
84         struct list_head queue;         /**< list of qn's sorted by weight of qn-mis */
85 } unit_t;
86
87
88
89 /******************************************************************************
90    _____                 _        _____ _
91   / ____|               | |      / ____| |
92  | |  __ _ __ __ _ _ __ | |__   | (___ | |_ ___  _ __ __ _  __ _  ___
93  | | |_ | '__/ _` | '_ \| '_ \   \___ \| __/ _ \| '__/ _` |/ _` |/ _ \
94  | |__| | | | (_| | |_) | | | |  ____) | || (_) | | | (_| | (_| |  __/
95   \_____|_|  \__,_| .__/|_| |_| |_____/ \__\___/|_|  \__,_|\__, |\___|
96                   | |                                       __/ |
97                   |_|                                      |___/
98  ******************************************************************************/
99
100 typedef struct _neighb_t neighb_t;
101 typedef struct _affinity_node_t affinity_node_t;
102
103
104 struct _neighb_t {
105         neighb_t *next;                 /** the next neighbour entry*/
106         ir_node *irn;                   /** the neighbour itself */
107         int costs;                              /** the costs of the edge (affinity_node_t->irn, neighb_t->irn) */
108 };
109
110 struct _affinity_node_t {
111         ir_node *irn;                   /** a node with affinity edges */
112         int degree;                             /** number of affinity edges in the linked list below */
113         neighb_t *neighbours;   /** a linked list of all affinity neighbours */
114         void *data;             /** stuff that is attachable. */
115 };
116
117
118 static INLINE affinity_node_t *get_affinity_info(const copy_opt_t *co, ir_node *irn) {
119         affinity_node_t find;
120
121         ASSERT_GS_AVAIL(co);
122
123         find.irn = irn;
124         return set_find(co->nodes, &find, sizeof(find), nodeset_hash(irn));
125 }
126
127 #define co_gs_nodes_begin(co)                   set_first((co)->nodes)
128 #define co_gs_nodes_next(co)                    set_next((co)->nodes)
129 #define co_gs_nodes_break(co)                   set_break((co)->nodes)
130
131 #define co_gs_foreach_aff_node(co, aff_node)    for (aff_node = co_gs_nodes_begin(co); aff_node; aff_node = co_gs_nodes_next(co))
132 #define co_gs_foreach_neighb(aff_node, neighb)  for (neighb = aff_node->neighbours; neighb; neighb = neighb->next)
133
134
135 #endif