irop: Constify get_op_ops().
[libfirm] / ir / ir / irhooks.h
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief   Generic hooks for various libFirm functions.
9  * @author  Michael Beck
10  */
11 #ifndef FIRM_IR_IRHOOKS_H
12 #define FIRM_IR_IRHOOKS_H
13
14 #include "irop.h"
15 #include "irnode.h"
16 #include "irgraph.h"
17
18 /**
19  * options for the hook_merge_nodes hook
20  */
21 typedef enum {
22         HOOK_OPT_DEAD_BLOCK,  /**< a block was removed because it's dead */
23         HOOK_OPT_STG,         /**< straightening optimization */
24         HOOK_OPT_IFSIM,       /**< if simplification */
25         HOOK_OPT_CONST_EVAL,  /**< constant evaluation */
26         HOOK_OPT_ALGSIM,      /**< algebraic simplification */
27         HOOK_OPT_PHI,         /**< Phi optimization */
28         HOOK_OPT_SYNC,        /**< Sync optimization */
29         HOOK_OPT_WAW,         /**< Write-After-Write optimization */
30         HOOK_OPT_WAR,         /**< Write-After-Read optimization */
31         HOOK_OPT_RAW,         /**< Read-After-Write optimization */
32         HOOK_OPT_RAR,         /**< Read-After-Read optimization */
33         HOOK_OPT_RC,          /**< Read-a-Const optimization */
34         HOOK_OPT_TUPLE,       /**< Tuple optimization */
35         HOOK_OPT_ID,          /**< ID optimization */
36         HOOK_OPT_CSE,         /**< common subexpression elimination */
37         HOOK_OPT_STRENGTH_RED,/**< strength reduction */
38         HOOK_OPT_ARCH_DEP,    /**< architecture dependent optimization */
39         HOOK_OPT_REASSOC,     /**< reassociation */
40         HOOK_OPT_POLY_CALL,   /**< polymorphic call optimization */
41         HOOK_OPT_IF_CONV,     /**< an if conversion was tried */
42         HOOK_OPT_FUNC_CALL,   /**< a real function call was removed */
43         HOOK_OPT_CONFIRM,     /**< a value was substituted by another due to a Confirm */
44         HOOK_OPT_CONFIRM_C,   /**< a value was substituted by a const due to a Confirm */
45         HOOK_OPT_CONFIRM_E,   /**< a value was evaluated due to a Confirm */
46         HOOK_OPT_EXC_REM,     /**< a exception edge was removed due to a Confirmation prove */
47         HOOK_OPT_NORMALIZE,   /**< a commutative node was normalized */
48         HOOK_LOWERED,         /**< lowered */
49         HOOK_BACKEND,         /**< a backend transformation */
50         HOOK_OPT_LAST
51 } hook_opt_kind;
52
53 /** Result of an if-conversion attempt */
54 typedef enum if_result_t {
55         IF_RESULT_SUCCESS         = 0,  /**< if conversion could be done */
56         IF_RESULT_SIDE_EFFECT     = 1,  /**< if conversion failed because of side effect */
57         IF_RESULT_SIDE_EFFECT_PHI = 2,  /**< if conversion failed because of Phi node found */
58         IF_RESULT_TOO_DEEP        = 3,  /**< if conversion failed because of to deep DAG's */
59         IF_RESULT_BAD_CF          = 4,  /**< if conversion failed because of bad control flow */
60         IF_RESULT_DENIED          = 5,  /**< if conversion failed because of architecture deny */
61         IF_RESULT_LAST
62 } if_result_t;
63
64 /**
65  * A generic function type.
66  */
67 typedef void (generic_func)(void);
68
69 /**
70  * a hook entry
71  */
72 typedef struct hook_entry {
73         /** A union of all possible hook types. */
74         union {
75                 /** This hook is called, after a new ir_op was created. */
76                 void (*_hook_new_ir_op)(void *context, ir_op *op);
77
78                 /** This hook is called, before am ir_op is destroyed. */
79                 void (*_hook_free_ir_op)(void *context, ir_op *op);
80
81                 /** This hook is called, after a new IR-node was created and before it is optimized. */
82                 void (*_hook_new_node)(void *context, ir_graph *graph, ir_node *node);
83
84                 /** This hook is called, before a node input was changed. */
85                 void (*_hook_set_irn_n)(void *context, ir_node *src,
86                                         int pos, ir_node *tgt, ir_node *old_tgt);
87
88                 /** This hook is called, before a node is replaced (exchange()) by another. */
89                 void (*_hook_replace)(void *context, ir_node *old_node, ir_node *new_node);
90
91                 /** This hook is called, before a node is changed into an Id node. */
92                 void (*_hook_turn_into_id)(void *context, ir_node *node);
93
94                 /** This hook is called, after a commutative node was normalized. */
95                 void (*_hook_normalize)(void *context, ir_node *node);
96
97                 /** This hook is called, after a new graph was created and before the first block
98                 *  on this graph is build. */
99                 void (*_hook_new_graph)(void *context, ir_graph *irg, ir_entity *ent);
100
101                 /** This hook is called before a graph is freed. */
102                 void (*_hook_free_graph)(void *context, ir_graph *irg);
103
104                 /** This hook is called before an irg walk is started. */
105                 void (*_hook_irg_walk)(void *context, ir_graph *irg, generic_func *pre, generic_func *post);
106
107                 /** This hook is called before an block wise irg walk is started. */
108                 void (*_hook_irg_walk_blkwise)(void *context, ir_graph *irg, generic_func *pre, generic_func *post);
109
110                 /** This hook is called before an block walk is started. */
111                 void (*_hook_irg_block_walk)(void *context, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post);
112
113                 /** This hook is called, when debug info must be merged. */
114                 void (*_hook_merge_nodes)(void *context, ir_node **new_node_array, int new_num_entries,
115                                           ir_node **old_node_array, int old_num_entries, hook_opt_kind opt);
116
117                 /** This hook is called, when reassociation is started/stopped. */
118                 void (*_hook_reassociate)(void *context, int start);
119
120                 /** This hook is called, before a node is lowered. */
121                 void (*_hook_lower)(void *context, ir_node *node);
122
123                 /** This hook is called, before a graph is inlined. */
124                 void (*_hook_inline)(void *context, ir_node *call, ir_graph *irg);
125
126                 /** This hook is called, before tail recursion is applied to a graph. */
127                 void (*_hook_tail_rec)(void *context, ir_graph *irg, int n_calls);
128
129                 /** This hook is called, before a node is replaced due to strength reduction */
130                 void (*_hook_strength_red)(void *context, ir_graph *irg, ir_node *node);
131
132                 /** This hook is called, when dead node elimination is started/stopped. */
133                 void (*_hook_dead_node_elim)(void *context, ir_graph *irg, int start);
134
135                 /** This hook is called after if conversion has run. */
136                 void (*_hook_if_conversion)(void *context, ir_graph *irg, ir_node *phi, int pos, ir_node *mux, if_result_t reason);
137
138                 /** This hook is called after a call was detected as const call */
139                 void (*_hook_func_call)(void *context, ir_graph *irg, ir_node *call);
140
141                 /** This hook is called after a Mul was replaced by a series of Shift and Add/Sub operations. */
142                 void (*_hook_arch_dep_replace_mul_with_shifts)(void *context, ir_node *irn);
143
144                 /** This hook is called after a Div/Mod by a constant value was replaced. */
145                 void (*_hook_arch_dep_replace_division_by_const)(void *context, ir_node *irn);
146
147                 /** This hook is called after a new mode was registered. */
148                 void (*_hook_new_mode)(void *context, ir_mode *mode);
149
150                 /** This hook is called after a new entity was created. */
151                 void (*_hook_new_entity)(void *context, ir_entity *ent);
152
153                 /** This hook is called after a new type was created. */
154                 void (*_hook_new_type)(void *context, ir_type *tp);
155
156                 /** This hook is called at the end of the node info dumper to dump additional node info. */
157                 void (*_hook_node_info)(void *context, FILE *f, const ir_node *n);
158         } hook; /**< hook */
159
160         /** the context for every hook */
161         void *context;
162
163         /** needed for chaining */
164         struct hook_entry *next;
165 } hook_entry_t;
166
167 /**
168  * possible hooks
169  */
170 typedef enum {
171         hook_new_ir_op,            /**< type for hook_new_ir_op() hook */
172         hook_free_ir_op,           /**< type for hook_free_ir_op() hook */
173         hook_new_node,             /**< type for hook_new_node() hook */
174         hook_set_irn_n,            /**< type for hook_set_irn_n() hook */
175         hook_replace,              /**< type for hook_replace() hook */
176         hook_turn_into_id,         /**< type for hook_turn_into_id() hook */
177         hook_normalize,            /**< type for hook_normalize() hook */
178         hook_new_graph,            /**< type for hook_new_graph() hook */
179         hook_free_graph,           /**< type for hook_free_graph() hook */
180         hook_irg_walk,             /**< type for hook_irg_walk() hook */
181         hook_irg_walk_blkwise,     /**< type for hook_irg_walk_blkwise() hook */
182         hook_irg_block_walk,       /**< type for hook_irg_block_walk() hook */
183         hook_merge_nodes,          /**< type for hook_merge_nodes() hook */
184         hook_reassociate,          /**< type for hook_reassociate() hook */
185         hook_lower,                /**< type for hook_lower() hook */
186         hook_inline,               /**< type for hook_inline() hook */
187         hook_tail_rec,             /**< type for hook_tail_rec() hook */
188         hook_strength_red,         /**< type for hook_strength_red() hook */
189         hook_dead_node_elim,       /**< type for hook_dead_node_elim() hook */
190         hook_if_conversion,        /**< type for hook_if_conversion() hook */
191         hook_func_call,            /**< type for hook_func_call() hook */
192         /** type for hook_arch_dep_replace_mul_with_shifts() hook */
193         hook_arch_dep_replace_mul_with_shifts,
194         /** type for hook_arch_dep_replace_division_by_const() hook */
195         hook_arch_dep_replace_division_by_const,
196         hook_new_mode,             /**< type for hook_new_mode() hook */
197         hook_new_entity,           /**< type for hook_new_entity() hook */
198         hook_new_type,             /**< type for hook_new_type() hook */
199         hook_node_info,            /**< type for hook_node_info() hook */
200         hook_last                  /**< last hook type */
201 } hook_type_t;
202
203 /**
204  * register a hook entry.
205  *
206  * @param hook   the hook type
207  * @param entry  the hook entry
208  */
209 void register_hook(hook_type_t hook, hook_entry_t *entry);
210
211 /**
212  * unregister a hook entry.
213  *
214  * @param hook   the hook type
215  * @param entry  the hook entry
216  */
217 void unregister_hook(hook_type_t hook, hook_entry_t *entry);
218
219 /** Global list of registerd hooks. */
220 extern hook_entry_t *hooks[hook_last];
221
222 /**
223  * Executes the hook @p what with the args @p args
224  * Do not use this macro directly.
225  */
226 #define hook_exec(what, args) do {           \
227   hook_entry_t *_p;                          \
228   for (_p = hooks[what]; _p; _p = _p->next){ \
229     void *hook_ctx_ = _p->context;           \
230     _p->hook._##what args;                   \
231   }                                          \
232 } while (0)
233
234 /** Called when a new node opcode has been created */
235 #define hook_new_ir_op(op)                hook_exec(hook_new_ir_op, (hook_ctx_, op))
236 /** Called when a node opcode has been freed */
237 #define hook_free_ir_op(op)               hook_exec(hook_free_ir_op, (hook_ctx_, op))
238 /** Called after a new node has been created */
239 #define hook_new_node(graph, node)        hook_exec(hook_new_node, (hook_ctx_, graph, node))
240 /** Called when a nodes input is changed */
241 #define hook_set_irn_n(src, pos, tgt, old_tgt) \
242   hook_exec(hook_set_irn_n, (hook_ctx_, src, pos, tgt, old_tgt))
243 /** Called when a node is replaced */
244 #define hook_replace(old, nw)             hook_exec(hook_replace, (hook_ctx_, old, nw))
245 /** Called when a node is turned into an Id node */
246 #define hook_turn_into_id(node)           hook_exec(hook_turn_into_id, (hook_ctx_, node))
247 /** Called when a node is normalized */
248 #define hook_normalize(node)              hook_exec(hook_normalize, (hook_ctx_, node))
249 /** Called after a new graph has been created */
250 #define hook_new_graph(irg, ent)          hook_exec(hook_new_graph, (hook_ctx_, irg, ent))
251 /** Called after a graph has been freed */
252 #define hook_free_graph(irg)              hook_exec(hook_free_graph, (hook_ctx_, irg))
253 /** Called before a graph walk is started */
254 #define hook_irg_walk(irg, pre, post)     hook_exec(hook_irg_walk, (hook_ctx_, irg, pre, post))
255 /** Called before a blockwise graph walk is started */
256 #define hook_irg_walk_blkwise(irg, pre, post) \
257   hook_exec(hook_irg_walk_blkwise, (hook_ctx_, irg, pre, post))
258 /** Called before a block walk is started */
259 #define hook_irg_block_walk(irg, node, pre, post) \
260   hook_exec(hook_irg_block_walk, (hook_ctx_, irg, node, pre, post))
261 /** Called before 2 nodes get merged */
262 #define hook_merge_nodes(new_node_array, new_num_entries, old_node_array, old_num_entries, opt) \
263   hook_exec(hook_merge_nodes, (hook_ctx_, new_node_array, new_num_entries, old_node_array, old_num_entries, opt))
264 /** Called before node inputs get reassociated */
265 #define hook_reassociate(start)           hook_exec(hook_reassociate, (hook_ctx_, start))
266 /** Called before a node gets lowered */
267 #define hook_lower(node)                  hook_exec(hook_lower, (hook_ctx_, node))
268 /** Called before a graph is inlined */
269 #define hook_inline(call, irg)            hook_exec(hook_inline, (hook_ctx_, call, irg))
270 /** Called before tail recursion is performed */
271 #define hook_tail_rec(irg, n_calls)       hook_exec(hook_tail_rec, (hook_ctx_, irg, n_calls))
272 /** Called before strength reduction is performed */
273 #define hook_strength_red(irg, node) \
274   hook_exec(hook_strength_red, (hook_ctx_, irg, node))
275 /** Called before dead node elimination is performed */
276 #define hook_dead_node_elim(irg, start)   hook_exec(hook_dead_node_elim, (hook_ctx_, irg, start))
277 /** Called when if-conversion creates a Mux node */
278 #define hook_if_conversion(irg, phi, pos, mux, reason) \
279   hook_exec(hook_if_conversion, (hook_ctx_, irg, phi, pos, mux, reason))
280 /** Called when a function call is optimized */
281 #define hook_func_call(irg, call) \
282   hook_exec(hook_func_call, (hook_ctx_, irg, call))
283 /** Called when a mul is replaced with shifts */
284 #define hook_arch_dep_replace_mul_with_shifts(irn) \
285   hook_exec(hook_arch_dep_replace_mul_with_shifts, (hook_ctx_, irn))
286 /** Called when a dvision by constant is replaced */
287 #define hook_arch_dep_replace_division_by_const(irn) \
288   hook_exec(hook_arch_dep_replace_division_by_const, (hook_ctx_, irn))
289 /** Called when a new mode has been created */
290 #define hook_new_mode(mode)               hook_exec(hook_new_mode, (hook_ctx_, mode))
291 /** Called when a new entity has been created */
292 #define hook_new_entity(ent)              hook_exec(hook_new_entity, (hook_ctx_, ent))
293 /** Called when a new type has been created */
294 #define hook_new_type(tp)                 hook_exec(hook_new_type, (hook_ctx_, tp))
295 /** Called at the end of the node info dumper to dump additional node info. */
296 #define hook_node_info(F, node)           hook_exec(hook_node_info, (hook_ctx_, F, node))
297
298 #endif