added be_peephole_before_exchange_and_kill() to fix the IncSP,IncSP jumping above...
[libfirm] / ir / be / bepeephole.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       peephole optimisation framework
23  * @author      Matthias Braun
24  * @version     $Id$
25  */
26
27 #ifndef BEPEEPHOLE_H
28 #define BEPEEPHOLE_H
29
30 #include "beirg.h"
31 #include "bearch_t.h"
32
33 extern ir_node ***register_values;
34
35 static INLINE ir_node *be_peephole_get_value(unsigned regclass_idx,
36                                              unsigned register_idx)
37 {
38         return register_values[regclass_idx][register_idx];
39 }
40
41 static INLINE ir_node *be_peephole_get_reg_value(const arch_register_t *reg)
42 {
43         unsigned regclass_idx = arch_register_class_index(arch_register_get_class(reg));
44         unsigned register_idx = arch_register_get_index(reg);
45
46         return be_peephole_get_value(regclass_idx, register_idx);
47 }
48
49 /**
50  * Datatype of the generic op handler for optimisation.
51  */
52 typedef void (*peephole_opt_func) (ir_node *node);
53
54 /**
55  * must be called from peephole optimisations before a node is exchanged,
56  * so bepeephole can update it's internal state.
57  */
58 void be_peephole_before_exchange(const ir_node *old_node, ir_node *new_node);
59
60 /**
61  * must be called from peephole optimisations after a node is exchanged,
62  * so bepeephole can update it's internal state.
63  */
64 void be_peephole_after_exchange(ir_node *new_node);
65
66 /**
67  * must be called from peephole optimisations before a node will be killed
68  * and its users will be redirected to new_node.
69  * so bepeephole can update it's internal state.
70  *
71  * Note: killing a node and rewiring os only allowed if new_node produces
72  * the same registers as old_node.
73  */
74 void be_peephole_before_exchange_and_kill(const ir_node *old_node, ir_node *new_node);
75
76 /**
77  * Tries to optimize a beIncSp node with it's previous IncSP node.
78  * Must be run from a be_peephole_opt() context.
79  *
80  * @param node  a be_IncSP node
81  *
82  * @return the new IncSP node or node itself
83  */
84 ir_node *be_peephole_IncSP_IncSP(ir_node *node);
85
86 /**
87  * Do peephole optimisations. It traverses the schedule of all blocks in
88  * backward direction. The register_values variable indicates which (live)
89  * values are stored in which register.
90  * The generic op handler is called for each node if it exists. That's where
91  * backend specific optimisations should be performed based on the
92  * register-liveness information.
93  */
94 void be_peephole_opt(be_irg_t *birg);
95
96 #endif