New inlining schema implemented:
[libfirm] / ir / ir / irnode_t.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irnode_t.h
4  * Purpose:     Representation of an intermediate operation -- private header.
5  * Author:      Martin Trapp, Christian Schaefer
6  * Modified by: Goetz Lindenmaier
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 /**
15  * @file irnode_t.h
16  *
17  * Declarations of an ir node.
18  *
19  * @author Martin Trapp, Christian Schaefer
20  */
21
22
23 # ifndef _IRNODE_T_H_
24 # define _IRNODE_T_H_
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 # include "irnode.h"
31 # include "irop_t.h"
32 # include "irgraph_t.h"
33 # include "irflag_t.h"
34 # include "firm_common_t.h"
35 # include "irdom_t.h" /* For size of struct dom_info. */
36 # include "dbginfo.h"
37 # include "irloop.h"
38 # include "array.h"
39
40 # include "exc.h"
41
42 /** ir node attributes **/
43
44 /** Block attributes */
45 typedef struct {
46   /* General attributes */
47   ir_graph *irg;
48   unsigned long block_visited;  /**< for the walker that walks over all blocks. */
49   /* Attributes private to construction: */
50   bool matured;               /**< if set, all in-nodes of the block are fixed */
51   struct ir_node **graph_arr; /**< array to store all parameters */
52   /* Attributes holding analyses information */
53   struct dom_info dom;        /**< Datastructure that holds information about dominators.
54                  @@@ @todo
55                  Eventually overlay with graph_arr as only valid
56                  in different phases.  Eventually inline the whole
57                  datastructure. */
58   /*   exc_t exc;  */            /**< role of this block for exception handling */
59   /*   ir_node *handler_entry; */    /**< handler entry block iff this block is part of a region */
60   ir_node ** in_cg;           /**< array with predecessors in
61                    * interprocedural_view, if they differ
62                    * from intraprocedural predecessors */
63   int *backedge;              /**< Field n set to true if pred n is backedge.
64                      @@@ @todo Ev. replace by bitfield! */
65   int *cg_backedge;           /**< Field n set to true if pred n is interprocedural backedge.
66                      @@@ @todo Ev. replace by bitfield! */
67 } block_attr;
68
69 /** Start attributes */
70 typedef struct {
71   char dummy;
72   /*   ir_graph *irg;   @@@ now in block */
73 } start_attr;
74
75 /** Cond attributes */
76 typedef struct {
77   cond_kind kind;    /**< flavor of Cond */
78   long default_proj; /**< for optimization: biggest Proj number, i.e. the one
79               used for default. */
80 } cond_attr;
81
82 /** Const attributes */
83 typedef struct {
84   tarval *tv;        /**< the target value */
85   type   *tp;        /**< the source type, for analyses. default: type_unknown. */
86 } const_attr;
87
88 /** SymConst attributes
89     This union contains the symbolic information represented by the node */
90 typedef union type_or_id {
91   type  *typ;
92   ident *ptrinfo;
93 } type_or_id;
94
95 typedef struct {
96   type_or_id tori;
97   symconst_kind num;
98 } symconst_attr;
99
100 /** Sel attributes */
101 typedef struct {
102   entity *ent;          /**< entity to select */
103 } sel_attr;
104
105 typedef struct {
106   type *cld_tp;         /**< type of called procedure */
107 #if PRECISE_EXC_CONTEXT
108   struct ir_node **frag_arr; /**< For Phi node construction in case of exceptions */
109 #endif
110   entity ** callee_arr; /**< result of callee analysis */
111 } call_attr;
112
113 /** Alloc attributes */
114 typedef struct {
115   type *type;           /**< Type of the allocated object.  */
116   where_alloc where;    /**< stack, heap or other managed part of memory */
117 #if PRECISE_EXC_CONTEXT
118   struct ir_node **frag_arr; /**< For Phi node construction in case of exceptions */
119 #endif
120 } alloc_attr;
121
122 /** InstOf attributes */
123 typedef struct
124 {
125   type *ent;
126   int dfn;
127 } io_attr;
128
129 /** Filter attributes */
130 typedef struct {
131   long proj;                 /**< contains the result position to project (Proj) */
132   ir_node ** in_cg;          /**< array with interprocedural predecessors (Phi) */
133   int *backedge;              /**< Field n set to true if pred n is backedge.
134                      @todo Ev. replace by bitfield! */
135 } filter_attr;
136
137 /** EndReg/EndExcept attributes */
138 typedef struct {
139   char dummy;
140   /*   ir_graph * irg; */           /**< ir_graph this node belongs to (for */
141   /*                               * navigating in interprocedural graphs)  */
142   /*                       @@@ now in block */
143 } end_attr;
144
145 /** CallBegin attributes */
146 typedef struct {
147   /*   ir_graph * irg; */           /**< ir_graph this node belongs to (for */
148   /*                   * navigating in interprocedural graphs) */
149   /*                            @@@ now in block */
150   ir_node * call;            /**< associated Call-operation */
151 } callbegin_attr;
152
153 /** Cast attributes */
154 typedef struct {
155   type *totype;
156 } cast_attr;
157
158 typedef pn_Cmp confirm_attr; /** Attribute to hold compare operation */
159
160 /** Some irnodes just have one attribute, these are stored here,
161    some have more. Their name is 'irnodename_attr' */
162 typedef union {
163   start_attr     start; /**< For Start */
164   block_attr     block; /**< For Block: Fields needed to construct it */
165   cond_attr      c;     /**< For Cond. */
166   const_attr     con;   /**< For Const: contains the value of the constant and a type */
167   symconst_attr  i;     /**< For SymConst. */
168   sel_attr       s;     /**< For Sel. */
169   call_attr      call;  /**< For Call: pointer to the type of the method to call */
170   callbegin_attr callbegin; /**< For CallBegin */
171   alloc_attr     a;     /**< For Alloc. */
172   io_attr        io;    /**< For InstOf */
173   type           *f;    /**< For Free. */
174   cast_attr      cast;  /**< For Cast. */
175   int            phi0_pos;  /**< For Phi. Used to remember the value defined by
176                    this Phi node.  Needed when the Phi is completed
177                    to call get_r_internal_value to find the
178                    predecessors. If this attribute is set, the Phi
179                    node takes the role of the obsolete Phi0 node,
180                    therefore the name. */
181   int *phi_backedge;    /**< For Phi after construction.
182                Field n set to true if pred n is backedge.
183                @todo Ev. replace by bitfield! */
184   long           proj;  /**< For Proj: contains the result position to project */
185   confirm_attr   confirm_cmp;   /**< For Confirm: compare operation */
186   filter_attr    filter;    /**< For Filter */
187   end_attr       end;       /**< For EndReg, EndExcept */
188 #if PRECISE_EXC_CONTEXT
189   struct ir_node **frag_arr; /**< For Phi node construction in case of exceptions
190                 for nodes Store, Load, Div, Mod, Quot, DivMod. */
191 #endif
192 } attr;
193
194
195 /** common structure of an irnode
196     if the node has some attributes, they are stored in attr */
197 struct ir_node {
198   /* ------- Basics of the representation  ------- */
199   firm_kind kind;          /**< distinguishes this node from others */
200   ir_op *op;               /**< Opcode of this node. */
201   ir_mode *mode;           /**< Mode of this node. */
202   unsigned long visited;   /**< visited counter for walks of the graph */
203   struct ir_node **in;     /**< array with predecessors / operands */
204   void *link;              /**< to attach additional information to the node, e.g.
205                               used while construction to link Phi0 nodes and
206                   during optimization to link to nodes that
207                   shall replace a node. */
208   /* ------- Fields for optimizations / analysis information ------- */
209   struct ir_node **out;    /**< array of out edges */
210   struct dbg_info* dbi;    /**< A pointer to information for debug support. */
211   /* ------- For debugging ------- */
212 #ifdef DEBUG_libfirm
213   int out_valid;                /** < indicate that out edges are valid */
214   int node_nr;             /**< a unique node number for each node to make output
215                   readable. */
216 #endif
217   /* ------- For analyses -------- */
218   ir_loop *loop;           /**< the loop the node is in. Access routines in irloop.h */
219   /* ------- Opcode depending fields -------- */
220   attr attr;               /**< attribute of this node. Depends on opcode.
221                               Must be last field of struct ir_node. */
222 };
223
224 /** Copies all attributes stored in the old node  to the new node.
225     Assumes both have the same opcode and sufficient size. */
226 void
227 copy_attrs(const ir_node *old_node, ir_node *new_node);
228
229
230 /** Returns the array with the ins.  The content of the array may not be
231    changed.  */
232 ir_node     **get_irn_in            (const ir_node *node);
233
234 /** @{ */
235 /** access attributes directly */
236 INLINE const_attr    get_irn_const_attr    (ir_node *node);
237 INLINE long          get_irn_proj_attr     (ir_node *node);
238 INLINE alloc_attr    get_irn_alloc_attr    (ir_node *node);
239 INLINE type         *get_irn_free_attr     (ir_node *node);
240 INLINE symconst_attr get_irn_symconst_attr (ir_node *node);
241 type         *get_irn_call_attr     (ir_node *node);
242 type         *get_irn_funccall_attr (ir_node *node);
243 sel_attr      get_irn_sel_attr      (ir_node *node);
244 int           get_irn_phi_attr      (ir_node *node);
245 block_attr    get_irn_block_attr   (ir_node *node);
246 /** @} */
247
248 /*-------------------------------------------------------------------*/
249 /*  These function are most used in libfirm.  Give them as static    */
250 /*  functions so they can be inlined.                                */
251 /*-------------------------------------------------------------------*/
252
253 /**
254  * Checks whether a pointer points to a ir node.
255  * Intern version for libFirm.
256  */
257 static INLINE int
258 __is_ir_node (const void *thing) {
259   return (get_kind(thing) == k_ir_node);
260 }
261
262 /**
263  * Gets the op of a node.
264  * Intern version for libFirm.
265  */
266 static INLINE ir_op *
267 __get_irn_op (const ir_node *node)
268 {
269   assert (node);
270   return node->op;
271 }
272
273 /**
274  * Gets the opcode of a node.
275  * Intern version for libFirm.
276  */
277 static INLINE opcode
278 __get_irn_opcode (const ir_node *node)
279 {
280   assert (k_ir_node == get_kind(node));
281   assert (node -> op);
282   return node->op->code;
283 }
284
285 /**
286  * Returns the number of predecessors without the block predecessor.
287  * Intern version for libFirm.
288  */
289 static INLINE int
290 __get_irn_intra_arity (const ir_node *node) {
291   assert(node);
292   return ARR_LEN(node->in) - 1;
293 }
294
295 /**
296  * Returns the number of predecessors without the block predecessor.
297  * Intern version for libFirm.
298  */
299 static INLINE int
300 __get_irn_inter_arity (const ir_node *node) {
301   assert(node);
302   if (__get_irn_opcode(node) == iro_Filter) {
303     assert(node->attr.filter.in_cg);
304     return ARR_LEN(node->attr.filter.in_cg) - 1;
305   } else if (__get_irn_opcode(node) == iro_Block && node->attr.block.in_cg) {
306     return ARR_LEN(node->attr.block.in_cg) - 1;
307   }
308   return __get_irn_intra_arity(node);
309 }
310
311 /**
312  * Returns the number of predecessors without the block predecessor.
313  * Intern version for libFirm.
314  */
315 static INLINE int
316 __get_irn_arity (const ir_node *node) {
317   assert(node);
318   if (interprocedural_view) return __get_irn_inter_arity(node);
319   return __get_irn_intra_arity(node);
320 }
321
322 /**
323  * Intern version for libFirm.
324  */
325 static INLINE ir_node *
326 __get_irn_intra_n (ir_node *node, int n) {
327   return (node->in[n + 1] = skip_nop(node->in[n + 1]));
328 }
329
330 /**
331  * Intern version for libFirm.
332  */
333 static INLINE ir_node*
334 __get_irn_inter_n (ir_node *node, int n) {
335   /* handle Filter and Block specially */
336   if (__get_irn_opcode(node) == iro_Filter) {
337     assert(node->attr.filter.in_cg);
338     return (node->attr.filter.in_cg[n + 1] = skip_nop(node->attr.filter.in_cg[n + 1]));
339   } else if (__get_irn_opcode(node) == iro_Block && node->attr.block.in_cg) {
340     return (node->attr.block.in_cg[n + 1] = skip_nop(node->attr.block.in_cg[n + 1]));
341   }
342
343   return __get_irn_intra_n (node, n);
344 }
345
346 /**
347  * Access to the predecessors of a node.
348  * To iterate over the operands iterate from 0 to i < get_irn_arity(),
349  * to iterate including the Block predecessor iterate from i = -1 to
350  * i < get_irn_arity.
351  * If it is a block, the entry -1 is NULL.
352  * Intern version for libFirm.
353  */
354 static INLINE ir_node *
355 __get_irn_n (ir_node *node, int n) {
356   assert(node); assert(-1 <= n && n < __get_irn_arity(node));
357   if (interprocedural_view)  return __get_irn_inter_n (node, n);
358   return __get_irn_intra_n (node, n);
359 }
360
361 /**
362  * Gets the mode of a node.
363  * Intern version for libFirm.
364  */
365 static INLINE ir_mode *
366 __get_irn_mode (const ir_node *node)
367 {
368   assert (node);
369   return node->mode;
370 }
371
372 /**
373  * Sets the mode of a node.
374  * Intern version of libFirm.
375  */
376 static INLINE void
377 __set_irn_mode (ir_node *node, ir_mode *mode)
378 {
379   assert (node);
380   node->mode = mode;
381 }
382
383 /**
384  * Gets the visited counter of a node.
385  * Intern version for libFirm.
386  */
387 static INLINE unsigned long
388 __get_irn_visited (const ir_node *node)
389 {
390   assert (node);
391   return node->visited;
392 }
393
394 /**
395  * Sets the visited counter of a node.
396  * Intern version for libFirm.
397  */
398 static INLINE void
399 __set_irn_visited (ir_node *node, unsigned long visited)
400 {
401   assert (node);
402   node->visited = visited;
403 }
404
405 /**
406  * Mark a node as visited in a graph.
407  * Intern version for libFirm.
408  */
409 static INLINE void
410 __mark_irn_visited (ir_node *node) {
411   assert (node);
412   node->visited = current_ir_graph->visited;
413 }
414
415 /**
416  * Returns non-zero if a node of was visited.
417  * Intern version for libFirm.
418  */
419 static INLINE int
420 __irn_visited(const ir_node *node) {
421   assert (node);
422   return (node->visited >= current_ir_graph->visited);
423 }
424
425 /**
426  * Returns non-zero if a node of was NOT visited.
427  * Intern version for libFirm.
428  */
429 static INLINE int
430 __irn_not_visited(const ir_node *node) {
431   assert (node);
432   return (node->visited < current_ir_graph->visited);
433 }
434
435 /**
436  * Sets the link of a node.
437  * Intern version of libFirm.
438  */
439 static INLINE void
440 __set_irn_link(ir_node *node, void *link) {
441   assert (node);
442   /* Link field is used for Phi construction and various optimizations
443      in iropt. */
444   assert(get_irg_phase_state(current_ir_graph) != phase_building);
445
446   node->link = link;
447 }
448
449 /**
450  * Returns the link of a node.
451  * Intern version of libFirm.
452  */
453 static INLINE void *
454 __get_irn_link(const ir_node *node) {
455   assert (node);
456   return node->link;
457 }
458
459 /* this section MUST contain all inline functions */
460 #define is_ir_node(thing)          __is_ir_node(thing)
461 #define get_irn_intra_arity(node)  __get_irn_intra_arity(node)
462 #define get_irn_inter_arity(node)  __get_irn_inter_arity(node)
463 #define get_irn_arity(node)        __get_irn_arity(node)
464 #define get_irn_intra_n(node, n)   __get_irn_intra_n(node, n)
465 #define get_irn_inter_n(node, n)   __get_irn_inter_n(node, n)
466 #define get_irn_n(node, n)         __get_irn_n(node, n)
467 #define get_irn_mode(node)         __get_irn_mode(node)
468 #define set_irn_mode(node, mode)   __set_irn_mode(node, mode)
469 #define get_irn_op(node)           __get_irn_op(node)
470 #define get_irn_opcode(node)       __get_irn_opcode(node)
471 #define get_irn_visited(node)      __get_irn_visited(node)
472 #define set_irn_visited(node, v)   __set_irn_visited(node, v)
473 #define mark_irn_visited(node)     __mark_irn_visited(node)
474 #define irn_visited(node)          __irn_visited(node)
475 #define irn_not_visited(node)      __irn_not_visited(node)
476 #define set_irn_link(node, link)   __set_irn_link(node, link)
477 #define get_irn_link(node)         __get_irn_link(node)
478
479 # endif /* _IRNODE_T_H_ */