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