added new licence header
[libfirm] / ir / ir / irgopt.h
1 /*
2  * Copyright (C) 1995-2007 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  * Project:     libFIRM
22  * File name:   ir/ir/irgopt.h
23  * Purpose:     Optimizations for a whole ir graph, i.e., a procedure.
24  * Author:      Christian Schaefer, Goetz Lindenmaier
25  * Modified by: Sebastian Felis
26  * Created:
27  * CVS-ID:      $Id$
28  * Copyright:   (c) 1998-2003 Universität Karlsruhe
29  */
30
31 /**
32  * @file irgopt.h
33  *
34  * Optimizations for a whole ir graph, i.e., a procedure.
35  *
36  * @author Christian Schaefer, Goetz Lindenmaier
37  */
38 #ifndef _FIRM_IR_IRGOPT_H_
39 #define _FIRM_IR_IRGOPT_H_
40
41 #include "firm_types.h"
42
43 /** Applies local optimizations (see iropt.h) to all nodes reachable from node n.
44  *
45  * @param n The node to be optimized.
46  */
47 void local_optimize_node(ir_node *n);
48
49 /** Applies local optimizations (see iropt.h) to all nodes in the graph.
50  *
51  * @param irg  The graph to be optimized.
52  *
53  * After applying local_optimize_graph() to a IR-graph, Bad nodes
54  * only occure as predecessor of Block and Phi nodes.
55  */
56 void local_optimize_graph (ir_graph *irg);
57
58 /** Applies local optimizations (see iropt.h) to all nodes in the graph.
59  *
60  * @param irg  The graph to be optimized.
61  *
62  * After applying local_optimize_graph() to a IR-graph, Bad nodes
63  * only occure as predecessor of Block and Phi nodes.
64  *
65  * This version used a fixpoint iteration.
66  */
67 void optimize_graph_df(ir_graph *irg);
68
69 /** Performs dead node elimination by copying the ir graph to a new obstack.
70  *
71  *  The major intention of this pass is to free memory occupied by
72  *  dead nodes and outdated analyzes information.  Further this
73  *  function removes Bad predecessors from Blocks and the corresponding
74  *  inputs to Phi nodes.  This opens optimization potential for other
75  *  optimizations.  Further this phase reduces dead Block<->Jmp
76  *  self-cycles to Bad nodes.
77  *
78  *  Dead_node_elimination is only performed if options `optimize' and
79  *  `opt_dead_node_elimination' are set.  The graph may
80  *  not be in state phase_building.  The outs datasturcture is freed,
81  *  the outs state set to outs_none.  Backedge information is conserved.
82  *  Removes old attributes of nodes.  Sets link field to NULL.
83  *  Callee information must be freed (irg_callee_info_none).
84  *
85  * @param irg  The graph to be optimized.
86  */
87 void dead_node_elimination(ir_graph *irg);
88
89 typedef struct _survive_dce_t survive_dce_t;
90
91 /**
92  * Make a new Survive DCE environment.
93  */
94 survive_dce_t *new_survive_dce(void);
95
96 /**
97  * Free a Survive DCE environment.
98  */
99 void free_survive_dce(survive_dce_t *sd);
100
101 /**
102  * Register a node pointer to be patched upon DCE.
103  * When DCE occurs, the node pointer specified by @p place will be
104  * patched to the new address of the node it is pointing to.
105  *
106  * @param sd    The Survive DCE environment.
107  * @param place The address of the node pointer.
108  */
109 void survive_dce_register_irn(survive_dce_t *sd, ir_node **place);
110
111 /**  Cleans the control flow from Bad predecessors.
112  *
113  * Removes Bad predecessors from Blocks and the corresponding
114  * inputs to Phi nodes as in dead_node_elimination but without
115  * copying the graph.
116  *
117  * Conserves loop information.
118  *
119  * @param irg  The graph to be optimized.
120  */
121 void remove_bad_predecessors(ir_graph *irg);
122
123 /** Inlines a method at the given call site.
124  *
125  *  Removes the call node and splits the basic block the call node
126  *  belongs to.  Inserts a copy of the called graph between these nodes.
127  *  Assumes that call is a Call node in current_ir_graph and that
128  *  the type in the Call nodes type attribute is the same as the
129  *  type of the called graph.
130  *  Further it assumes that all Phi nodes in a block of current_ir_graph
131  *  are assembled in a "link" list in the link field of the corresponding
132  *  block nodes.  Further assumes that all Proj nodes are in a "link" list
133  *  in the nodes producing the tuple.  (This is only an optical feature
134  *  for the graph.)  Conserves this feature for the old
135  *  nodes of the graph.  This precondition can be established by a call to
136  *  collect_phisprojs(), see irgmod.h.
137  *  As dead_node_elimination this function reduces dead Block<->Jmp
138  *  self-cycles to Bad nodes.
139  *
140  *  Called_graph must be unequal to current_ir_graph.   Will not inline
141  *  if they are equal.
142  *  Sets visited masterflag in current_ir_graph to the max of the flag in
143  *  current and called graph.
144  *  Assumes that both, the called and the calling graph are in state
145  *  "op_pin_state_pinned".
146  *  It is recommended to call local_optimize_graph() after inlining as this
147  *  function leaves a set of obscure Tuple nodes, e.g. a Proj-Tuple-Jmp
148  *  combination as control flow operation.
149  *
150  *  @param call          the call node that should be inlined
151  *  @param called_graph  the IR-graph that is called at call
152  *
153  *  @return zero if method could not be inlined (recursion for instance),
154  *          non-zero if all went ok
155  */
156 int inline_method(ir_node *call, ir_graph *called_graph);
157
158 /** Inlines all small methods at call sites where the called address comes
159  *  from a SymConst node that references the entity representing the called
160  *  method.
161  *
162  *  The size argument is a rough measure for the code size of the method:
163  *  Methods where the obstack containing the firm graph is smaller than
164  *  size are inlined.  Further only a limited number of calls are inlined.
165  *  If the method contains more than 1024 inlineable calls none will be
166  *  inlined.
167  *  Inlining is only performed if flags `optimize' and `inlineing' are set.
168  *  The graph may not be in state phase_building.
169  *  It is recommended to call local_optimize_graph() after inlining as this
170  *  function leaves a set of obscure Tuple nodes, e.g. a Proj-Tuple-Jmp
171  *  combination as control flow operation.
172  */
173 void inline_small_irgs(ir_graph *irg, int size);
174
175
176 /** Inlineing with a different heuristic than inline_small_irgs().
177  *
178  *  Inlines leave functions.  If inlinening creates new leave
179  *  function inlines these, too. (If g calls f, and f calls leave h,
180  *  h is first inlined in f and then f in g.)
181  *
182  *  Then inlines all small functions (this is not recursive).
183  *
184  *  For a heuristic this inlineing uses firm node counts.  It does
185  *  not count auxiliary nodes as Proj, Tuple, End, Start, Id, Sync.
186  *  If the ignore_runtime flag is set, calls to functions marked with the
187  *  mtp_property_runtime property are ignored.
188  *
189  *  @param maxsize         Do not inline any calls if a method has more than
190  *                         maxsize firm nodes.  It may reach this limit by
191  *                         inlineing.
192  *  @param leavesize       Inline leave functions if they have less than leavesize
193  *                         nodes.
194  *  @param size            Inline all function smaller than size.
195  *  @param ignore_runtime  count a function only calling runtime functions as
196  *                         leave
197  */
198 void inline_leave_functions(int maxsize, int leavesize, int size, int ignore_runtime);
199
200 /** Code Placement.
201  *
202  * Pins all floating nodes to a block where they
203  * will be executed only if needed.   Depends on the flag opt_global_cse.
204  * Graph may not be in phase_building.  Does not schedule control dead
205  * code.  Uses dominator information which it computes if the irg is not
206  * in state dom_consistent.  Destroys the out information as it moves nodes
207  * to other blocks.  Optimizes Tuples in Control edges.
208  * @todo This is not tested!
209  *
210  * Call remove_critical_cf_edges() before place_code().  This normalizes
211  * the control flow graph so that for all operations a basic block exists
212  * where they can be optimally placed.
213  *
214  * @todo A more powerful code placement would move operations past Phi nodes
215  * out of loops.
216  */
217 void place_code(ir_graph *irg);
218
219 /** Places an empty basic block on critical control flow edges thereby
220  * removing them.
221  *
222  * A critical control flow edge is an edge from a block with several
223  * control exits to a block with several control entries (See Muchnic
224  * p. 407).
225  *
226  * @param irg IR Graph
227  */
228 void remove_critical_cf_edges(ir_graph *irg);
229
230 #endif /* _FIRM_IR_IRGOPT_H_ */