make libfirm faster
[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 "irflag_t.h"
33 # include "firm_common_t.h"
34 # include "irdom_t.h" /* For size of struct dom_info. */
35 # include "dbginfo.h"
36 # include "irloop.h"
37 # include "array.h"
38
39 # include "exc.h"
40
41 /** ir node attributes **/
42
43 /** Block attributes */
44 typedef struct {
45   /* General attributes */
46   ir_graph *irg;
47   unsigned long block_visited;  /**< for the walker that walks over all blocks. */
48   /* Attributes private to construction: */
49   bool matured;               /**< if set, all in-nodes of the block are fixed */
50   struct ir_node **graph_arr; /**< array to store all parameters */
51   /* Attributes holding analyses information */
52   struct dom_info dom;        /**< Datastructure that holds information about dominators.
53                                  @@@ @todo
54                                  Eventually overlay with graph_arr as only valid
55                                  in different phases.  Eventually inline the whole
56                                  datastructure. */
57   //  exc_t exc;                      /**< role of this block for exception handling */
58   //  ir_node *handler_entry;     /**< handler entry block iff this block is part of a region */
59   ir_node ** in_cg;           /**< array with predecessors in
60                                * interprocedural_view, if they differ
61                                * from intraprocedural predecessors */
62   int *backedge;              /**< Field n set to true if pred n is backedge.
63                                  @@@ @todo Ev. replace by bitfield! */
64   int *cg_backedge;           /**< Field n set to true if pred n is interprocedural backedge.
65                                  @@@ @todo Ev. replace by bitfield! */
66 } block_attr;
67
68 /** Start attributes */
69 typedef struct {
70   char dummy;
71   //  ir_graph *irg;   @@@ now in block
72 } start_attr;
73
74 /** Cond attributes */
75 typedef struct {
76   cond_kind kind;    /**< flavor of Cond */
77   long default_proj; /**< for optimization: biggest Proj number, i.e. the one
78                           used for default. */
79 } cond_attr;
80
81 /** Const attributes */
82 typedef struct {
83   tarval *tv;        /**< the target value */
84   type   *tp;        /**< the source type, for analyses. default: type_unknown. */
85 } const_attr;
86
87 /** SymConst attributes
88     This union contains the symbolic information represented by the node */
89 typedef union type_or_id {
90   type  *typ;
91   ident *ptrinfo;
92 } type_or_id;
93
94 typedef struct {
95   type_or_id tori;
96   symconst_kind num;
97 } symconst_attr;
98
99 /** Sel attributes */
100 typedef struct {
101   entity *ent;          /**< entity to select */
102 } sel_attr;
103
104 typedef struct {
105   type *cld_tp;         /**< type of called procedure */
106 #if PRECISE_EXC_CONTEXT
107   struct ir_node **frag_arr; /**< For Phi node construction in case of exceptions */
108 #endif
109   entity ** callee_arr; /**< result of callee analysis */
110 } call_attr;
111
112 /** Alloc attributes */
113 typedef struct {
114   type *type;           /**< Type of the allocated object.  */
115   where_alloc where;    /**< stack, heap or other managed part of memory */
116 #if PRECISE_EXC_CONTEXT
117   struct ir_node **frag_arr; /**< For Phi node construction in case of exceptions */
118 #endif
119 } alloc_attr;
120
121 /** InstOf attributes */
122 typedef struct
123 {
124   type *ent;
125   int dfn;
126 } io_attr;
127
128 /** Filter attributes */
129 typedef struct {
130   long proj;                 /**< contains the result position to project (Proj) */
131   ir_node ** in_cg;          /**< array with interprocedural predecessors (Phi) */
132   int *backedge;              /**< Field n set to true if pred n is backedge.
133                                  @todo Ev. replace by bitfield! */
134 } filter_attr;
135
136 /** EndReg/EndExcept attributes */
137 typedef struct {
138   char dummy;
139   //  ir_graph * irg;            /**< ir_graph this node belongs to (for
140   //                              * navigating in interprocedural graphs)
141   //                              @@@ now in block */
142 } end_attr;
143
144 /** CallBegin attributes */
145 typedef struct {
146   //  ir_graph * irg;            / **< ir_graph this node belongs to (for
147   //                          * navigating in interprocedural graphs) */
148   //                           @@@ now in block
149   ir_node * call;            /**< associated Call-operation */
150 } callbegin_attr;
151
152 /** Cast attributes */
153 typedef struct {
154   type *totype;
155 } cast_attr;
156
157 typedef pn_Cmp confirm_attr; /** Attribute to hold compare operation */
158
159 /** Some irnodes just have one attribute, these are stored here,
160    some have more. Their name is 'irnodename_attr' */
161 typedef union {
162   start_attr     start; /**< For Start */
163   block_attr     block; /**< For Block: Fields needed to construct it */
164   cond_attr      c;     /**< For Cond. */
165   const_attr     con;   /**< For Const: contains the value of the constant and a type */
166   symconst_attr  i;     /**< For SymConst. */
167   sel_attr       s;     /**< For Sel. */
168   call_attr      call;  /**< For Call: pointer to the type of the method to call */
169   callbegin_attr callbegin; /**< For CallBegin */
170   alloc_attr     a;     /**< For Alloc. */
171   io_attr        io;    /**< For InstOf */
172   type          *f;     /**< For Free. */
173   cast_attr      cast;  /**< For Cast. */
174   int            phi0_pos;  /**< For Phi. Used to remember the value defined by
175                                this Phi node.  Needed when the Phi is completed
176                                to call get_r_internal_value to find the
177                                predecessors. If this attribute is set, the Phi
178                                node takes the role of the obsolete Phi0 node,
179                                therefore the name. */
180   int *phi_backedge;    /**< For Phi after construction.
181                            Field n set to true if pred n is backedge.
182                            @todo Ev. replace by bitfield! */
183   long           proj;  /**< For Proj: contains the result position to project */
184   confirm_attr   confirm_cmp;   /**< For Confirm: compare operation */
185   filter_attr    filter;    /**< For Filter */
186   end_attr       end;       /**< For EndReg, EndExcept */
187 #if PRECISE_EXC_CONTEXT
188   struct ir_node **frag_arr; /**< For Phi node construction in case of exceptions
189                                for nodes Store, Load, Div, Mod, Quot, DivMod. */
190 #endif
191 } attr;
192
193
194 /** common structure of an irnode
195     if the node has some attributes, they are stored in attr */
196 struct ir_node {
197   /* ------- Basics of the representation  ------- */
198   firm_kind kind;          /**< distinguishes this node from others */
199   ir_op *op;               /**< Opcode of this node. */
200   ir_mode *mode;           /**< Mode of this node. */
201   unsigned long visited;   /**< visited counter for walks of the graph */
202   struct ir_node **in;     /**< array with predecessors / operands */
203   void *link;              /**< to attach additional information to the node, e.g.
204                               used while construction to link Phi0 nodes and
205                               during optimization to link to nodes that
206                               shall replace a node. */
207   /* ------- Fields for optimizations / analysis information ------- */
208   struct ir_node **out;    /**< array of out edges */
209   struct dbg_info* dbi;    /**< A pointer to information for debug support. */
210   /* ------- For debugging ------- */
211 #ifdef DEBUG_libfirm
212   int node_nr;             /**< a unique node number for each node to make output
213                               readable. */
214 #endif
215   /* ------- For analyses -------- */
216   ir_loop *loop;           /**< the loop the node is in. Access routines in irloop.h */
217   /* ------- Opcode depending fields -------- */
218   attr attr;               /**< attribute of this node. Depends on opcode.
219                               Must be last field of struct ir_node. */
220 };
221
222 /** Copies all attributes stored in the old node  to the new node.
223     Assumes both have the same opcode and sufficient size. */
224 void
225 copy_attrs(const ir_node *old_node, ir_node *new_node);
226
227
228 /** Returns the array with the ins.  The content of the array may not be
229    changed.  */
230 ir_node     **get_irn_in            (const ir_node *node);
231
232 /*@{*/
233 /** access attributes directly */
234 INLINE const_attr    get_irn_const_attr    (ir_node *node);
235 INLINE long          get_irn_proj_attr     (ir_node *node);
236 INLINE alloc_attr    get_irn_alloc_attr    (ir_node *node);
237 INLINE type         *get_irn_free_attr     (ir_node *node);
238 INLINE symconst_attr get_irn_symconst_attr (ir_node *node);
239 type         *get_irn_call_attr     (ir_node *node);
240 type         *get_irn_funccall_attr (ir_node *node);
241 sel_attr      get_irn_sel_attr      (ir_node *node);
242 int           get_irn_phi_attr      (ir_node *node);
243 block_attr    get_irn_block_attr   (ir_node *node);
244 /*@}*/
245
246 /*********************************************************************/
247 /*  These function are most used in libfirm.  Give them as static    */
248 /*  functions so they can be inlined.                                */
249 /*********************************************************************/
250
251
252
253 /* returns the number of predecessors without the block predecessor. */
254 static INLINE int
255 intern_get_irn_intra_arity (const ir_node *node) {
256   assert(node);
257   return ARR_LEN(node->in) - 1;
258 }
259
260 /* returns the number of predecessors without the block predecessor. */
261 static INLINE int
262 intern_get_irn_inter_arity (const ir_node *node) {
263   assert(node);
264   if (get_irn_opcode(node) == iro_Filter) {
265     assert(node->attr.filter.in_cg);
266     return ARR_LEN(node->attr.filter.in_cg) - 1;
267   } else if (get_irn_opcode(node) == iro_Block && node->attr.block.in_cg) {
268     return ARR_LEN(node->attr.block.in_cg) - 1;
269   }
270   return intern_get_irn_intra_arity(node);
271 }
272
273 /* returns the number of predecessors without the block predecessor. */
274 static INLINE int
275 intern_get_irn_arity (const ir_node *node) {
276   assert(node);
277   if (interprocedural_view) return intern_get_irn_inter_arity(node);
278   return intern_get_irn_intra_arity(node);
279 }
280
281 static INLINE ir_node *
282 intern_get_irn_intra_n (ir_node *node, int n) {
283   return (node->in[n + 1] = skip_nop(node->in[n + 1]));
284 }
285
286 static INLINE ir_node*
287 intern_get_irn_inter_n (ir_node *node, int n) {
288   /* handle Filter and Block specially */
289   if (get_irn_opcode(node) == iro_Filter) {
290     assert(node->attr.filter.in_cg);
291     return (node->attr.filter.in_cg[n + 1] = skip_nop(node->attr.filter.in_cg[n + 1]));
292   } else if (get_irn_opcode(node) == iro_Block && node->attr.block.in_cg) {
293     return (node->attr.block.in_cg[n + 1] = skip_nop(node->attr.block.in_cg[n + 1]));
294   }
295
296   return get_irn_intra_n (node, n);
297 }
298
299 /* to iterate through the predecessors without touching the array */
300 /* To iterate over the operands iterate from 0 to i < get_irn_arity(),
301    to iterate including the Block predecessor iterate from i = -1 to
302    i < get_irn_arity.
303    If it is a block, the entry -1 is NULL. */
304 static INLINE ir_node *
305 intern_get_irn_n (ir_node *node, int n) {
306   assert(node); assert(-1 <= n && n < intern_get_irn_arity(node));
307   if (interprocedural_view)  return get_irn_inter_n (node, n);
308   return get_irn_intra_n (node, n);
309 }
310
311 static INLINE ir_mode *
312 intern_get_irn_mode (const ir_node *node)
313 {
314   assert (node);
315   return node->mode;
316 }
317
318 static INLINE ir_op *
319 intern_get_irn_op (const ir_node *node)
320 {
321   assert (node);
322   return node->op;
323 }
324
325 static INLINE opcode
326 intern_get_irn_opcode (const ir_node *node)
327 {
328   assert (k_ir_node == get_kind(node));
329   assert (node -> op);
330   return node->op->code;
331 }
332
333
334
335 # endif /* _IRNODE_T_H_ */