2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Base routines for register allocation.
23 * @author Sebastian Hack
43 #include "besched_t.h"
47 be_ra_timer_t *global_ra_timer = NULL;
50 sched_timestep_t get_time_step(const ir_node *irn)
55 return sched_get_time_step(irn);
58 int value_dominates_intrablock(const ir_node *a, const ir_node *b)
60 sched_timestep_t as = get_time_step(a);
61 sched_timestep_t bs = get_time_step(b);
66 int value_dominates(const ir_node *a, const ir_node *b)
68 const ir_node *block_a = get_block(a);
69 const ir_node *block_b = get_block(b);
72 * a and b are not in the same block,
73 * so dominance is determined by the dominance of the blocks.
75 if(block_a != block_b) {
76 return block_dominates(block_a, block_b);
80 * Dominance is determined by the time steps of the schedule.
82 return value_dominates_intrablock(a, b);
86 * Check, if two values interfere.
87 * @param a The first value.
88 * @param b The second value.
89 * @return 1, if a and b interfere, 0 if not.
91 int values_interfere(const be_lv_t *lv, const ir_node *a, const ir_node *b)
93 int a2b = value_dominates(a, b);
94 int b2a = value_dominates(b, a);
96 /* If there is no dominance relation, they do not interfere. */
98 const ir_edge_t *edge;
102 * Adjust a and b so, that a dominates b if
103 * a dominates b or vice versa.
106 const ir_node *t = a;
111 bb = get_nodes_block(b);
114 * If a is live end in b's block it is
115 * live at b's definition (a dominates b)
117 if(be_is_live_end(lv, bb, a))
121 * Look at all usages of a.
122 * If there's one usage of a in the block of b, then
123 * we check, if this use is dominated by b, if that's true
124 * a and b interfere. Note that b must strictly dominate the user,
125 * since if b is the last user of in the block, b and a do not
127 * Uses of a not in b's block can be disobeyed, because the
128 * check for a being live at the end of b's block is already
131 foreach_out_edge(a, edge) {
132 const ir_node *user = get_edge_src_irn(edge);
133 if(get_nodes_block(user) == bb && !is_Phi(user) && b != user && value_dominates(b, user))
141 /** The list of register allocators */
142 static be_module_list_entry_t *register_allocators = NULL;
143 static be_ra_t *selected_allocator = NULL;
145 void be_register_allocator(const char *name, be_ra_t *allocator)
147 if(selected_allocator == NULL)
148 selected_allocator = allocator;
149 be_add_module_to_list(®ister_allocators, name, allocator);
152 void be_allocate_registers(be_irg_t *birg)
154 assert(selected_allocator != NULL);
155 if(selected_allocator != NULL) {
156 selected_allocator->allocate(birg);
160 void be_init_ra(void)
162 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
164 be_add_module_list_opt(be_grp, "regalloc", "register allocator",
165 ®ister_allocators, (void**) &selected_allocator);
167 BE_REGISTER_MODULE_CONSTRUCTOR(init_be_ra);