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