remove self referencig cotrol loop by turning into Bad
[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
21 # ifndef _IRGOPT_H_
22 # define _IRGOPT_H_
23
24 # include "irgraph.h"
25
26 /** Applies local optimizations (see iropt.h) to all nodes in the graph. */
27 void local_optimize_graph (ir_graph *irg);
28
29 /** Performs dead node elimination by copying the ir graph to a new obstack.
30
31    Further removes Bad predecesors from Blocks and the corresponding
32    inputs to Phi nodes.
33    Optimization is only performed if options `optimize' and
34    `opt_dead_node_elimination' are set.
35    The graph may not be in state phase_building.  The outs datasturcture
36    is freed, the outs state set to no_outs.
37    @todo Change this? -> inconsistent.
38
39    Backedge information is conserved.
40    Removes old attributes of nodes.  Sets link field to NULL.
41    Callee information must be freed (irg_callee_info_none).
42
43    Attention: the numbers assigned to nodes if the library is compiled for
44    development/debugging are not conserved by copying. */
45 void dead_node_elimination(ir_graph *irg);
46
47 /** Removes Bad Bad predecesors from Blocks and the corresponding
48    inputs to Phi nodes as in dead_node_elimination but without
49    copying the graph.
50
51    @todo not implemented! */
52 void remove_bad_predecessors(ir_graph *irg);
53
54 /** Inlines a method at the given call site.
55
56    Removes the call node and splits the basic block the call node
57    belongs to.  Inserts a copy of the called graph between these nodes.
58    Assumes that call is a Call node in current_ir_graph and that
59    the type in the Call nodes type attribute is the same as the
60    type of the called graph.
61    Further it assumes that all Phi nodes in a block of current_ir_graph
62    are assembled in a "link" list in the link field of the corresponding
63    block nodes.  Further assumes that all Proj nodes are in a "link" list
64    in the nodes producing the tuple.  (This is only an optical feature
65    for the graph.)  Conserves this feature for the old
66    nodes of the graph.  This precondition can be established by a call to
67    collect_phisprojs(), see irgmod.h.
68    Called_graph must be unequal to current_ir_graph.   Will not inline
69    if they are equal.
70    Sets visited masterflag in current_ir_graph to the max of the flag in
71    current and called graph.
72    Assumes that both, the called and the calling graph are in state
73    "pinned".
74    It is recommended to call local_optimize_graph after inlining as this
75    function leaves a set of obscure Tuple nodes, e.g. a Proj-Tuple-Jmp
76    combination as control flow operation.
77
78    @param call          the call node that should be inlined
79    @param called_graph  the IR-graph that is called at call
80
81    @return zero if method could not be inlined (recursion for instance),
82            non-zero if all went ok
83 */
84 int inline_method(ir_node *call, ir_graph *called_graph);
85
86 /** Inlines all small methods at call sites where the called address comes
87    from a Const node that references the entity representing the called
88    method.
89    The size argument is a rough measure for the code size of the method:
90    Methods where the obstack containing the firm graph is smaller than
91    size are inlined.  Further only a limited number of calls are inlined.
92    If the method contains more than 1024 inlineable calls none will be
93    inlined.
94    Inlining is only performed if flags `optimize' and `inlineing' are set.
95    The graph may not be in state phase_building.
96    It is recommended to call local_optimize_graph after inlining as this
97    function leaves a set of obscure Tuple nodes, e.g. a Proj-Tuple-Jmp
98    combination as control flow operation.  */
99 void inline_small_irgs(ir_graph *irg, int size);
100
101
102 /** Inlineing with a different heuristic than inline_small_irgs.
103  *
104  *  Inlines leave functions.  If inlinening creates new leave
105  *  function inlines these, too. (If g calls f, and f calls leave h,
106  *  h is first inlined in f and then f in g.)
107  *
108  *  Then inlines all small functions (this is not recursive).
109  *
110  *  For a heuristic this inlineing uses firm node counts.  It does
111  *  not count auxiliary nodes as Proj, Tuple, End, Start, Id, Sync.
112  *
113  *  @param maxsize   Do not inline any calls if a method has more than
114  *                   maxsize firm nodes.  It may reach this limit by
115  *                   inlineing.
116  *  @param leavesize Inline leave functions if they have less than leavesize
117  *                   nodes.
118  *  @param size      Inline all function smaller than size.
119  */
120 void inline_leave_functions(int maxsize, int leavesize, int size);
121
122 /** Code Placement.  Pinns all floating nodes to a block where they
123    will be executed only if needed.   Depends on the flag opt_global_cse.
124    Graph may not be in phase_building.  Does not schedule control dead
125    code.  Uses dominator information which it computes if the irg is not
126    in state dom_consistent.  Destroys the out information as it moves nodes
127    to other blocks.  Optimizes Tuples in Control edges.
128    @todo This is not tested!
129
130    Call remove_critical_cf_edges() before place_code().  This normalizes
131    the control flow graph so that for all operations a basic block exists
132    where they can be optimally placed.
133
134    @todo A more powerful code placement would move operations past Phi nodes
135    out of loops.  */
136 void place_code(ir_graph *irg);
137
138 /** Control flow optimization.
139  * Removes empty blocks doing if simplifications and loop simplifications.
140  * A block is empty if it contains only a Jmp node and Phi nodes.
141  * Merges single entry single exit blocks with their predecessor
142  * and propagates dead control flow by calling equivalent_node.
143  * Independent of compiler flag it removes Tuples from cf edges,
144  * Bad predecessors form blocks and unnecessary predecessors of End.
145  *
146  * @bug So far destroys backedge information.
147  * @bug Chokes on Id nodes if called in a certain order with other
148  *      optimizations.  Call local_optimize_graph before to remove
149  *      Ids.
150  */
151 void optimize_cf(ir_graph *irg);
152
153
154 /** Places an empty basic block on critical control flow edges thereby
155    removing them.
156    A critical control flow edge is an edge from a block with several
157    control exits to a block with several control entries (See Muchnic
158    p. 407).
159    Is only executed if flag set_opt_critical_edges() is set.
160    @param irg IR Graph
161 */
162 void remove_critical_cf_edges(ir_graph *irg);
163
164 # endif /* _IRGOPT_H_ */