ADT Headers can be installed in libfirm/adt now
[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 "set.h"
41 #include "entity_t.h"
42 #include "type_t.h"
43
44
45 /** ir node attributes **/
46
47 /** Block attributes */
48 typedef struct {
49   /* General attributes */
50   ir_graph *irg;
51   unsigned long block_visited;  /**< for the walker that walks over all blocks. */
52   /* Attributes private to construction: */
53   int matured:1;                /**< if set, all in-nodes of the block are fixed */
54   int dead:1;                   /**< if set, the block is dead (and could be replace by a Bad */
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   type *tp;          /**< the source type, for analyses. default: type_unknown. */
96 } symconst_attr;
97
98 /** Sel attributes */
99 typedef struct {
100   entity *ent;          /**< entity to select */
101 } sel_attr;
102
103 /** Exception attributes */
104 typedef struct {
105   op_pin_state   pin_state;     /**< the pin state for operations that might generate a exception:
106                                      If it's know that no exception will be generated, could be set to
107                                      op_pin_state_floats. */
108 #if PRECISE_EXC_CONTEXT
109   struct ir_node **frag_arr;    /**< For Phi node construction in case of exception */
110 #endif
111 } except_attr;
112
113 typedef struct {
114   except_attr    exc;           /**< the exception attribute. MUST be the first one. */
115   type *cld_tp;                 /**< type of called procedure */
116   entity ** callee_arr;         /**< result of callee analysis */
117 } call_attr;
118
119 /** Alloc attributes */
120 typedef struct {
121   except_attr    exc;           /**< the exception attribute. MUST be the first one. */
122   type *type;                   /**< Type of the allocated object.  */
123   where_alloc where;            /**< stack, heap or other managed part of memory */
124 } alloc_attr;
125
126 /** InstOf attributes */
127 typedef struct
128 {
129   type *ent;
130   int dfn;
131 } io_attr;
132
133 /** Filter attributes */
134 typedef struct {
135   long proj;                 /**< contains the result position to project (Proj) */
136   ir_node ** in_cg;          /**< array with interprocedural predecessors (Phi) */
137   int *backedge;              /**< Field n set to true if pred n is backedge.
138                      @todo Ev. replace by bitfield! */
139 } filter_attr;
140
141 /** EndReg/EndExcept attributes */
142 typedef struct {
143   char dummy;
144   /*   ir_graph * irg; */           /**< ir_graph this node belongs to (for */
145   /*                               * navigating in interprocedural graphs)  */
146   /*                       @@@ now in block */
147 } end_attr;
148
149 /** CallBegin attributes */
150 typedef struct {
151   /*   ir_graph * irg; */           /**< ir_graph this node belongs to (for */
152   /*                   * navigating in interprocedural graphs) */
153   /*                            @@@ now in block */
154   ir_node * call;            /**< associated Call-operation */
155 } callbegin_attr;
156
157 /** Cast attributes */
158 typedef struct {
159   type *totype;
160 } cast_attr;
161
162 /** Load attributes */
163 typedef struct {
164   except_attr    exc;           /**< the exception attribute. MUST be the first one. */
165   ir_mode        *load_mode;    /**< the mode of this Load operation */
166   ent_volatility volatility;    /**< the volatility of a Load/Store operation */
167 } load_attr;
168
169 /** Store attributes */
170 typedef struct {
171   except_attr    exc;           /**< the exception attribute. MUST be the first one. */
172   ent_volatility volatility;    /**< the volatility of a Store operation */
173 } store_attr;
174
175 typedef pn_Cmp confirm_attr; /** Attribute to hold compare operation */
176
177 /** Some irnodes just have one attribute, these are stored here,
178    some have more. Their name is 'irnodename_attr' */
179 typedef union {
180   start_attr     start; /**< For Start */
181   block_attr     block; /**< For Block: Fields needed to construct it */
182   cond_attr      c;     /**< For Cond. */
183   const_attr     con;   /**< For Const: contains the value of the constant and a type */
184   symconst_attr  i;     /**< For SymConst. */
185   sel_attr       s;     /**< For Sel. */
186   call_attr      call;  /**< For Call: pointer to the type of the method to call */
187   callbegin_attr callbegin; /**< For CallBegin */
188   alloc_attr     a;     /**< For Alloc. */
189   io_attr        io;    /**< For InstOf */
190   type          *f;     /**< For Free. */
191   cast_attr      cast;  /**< For Cast. */
192   load_attr      load;  /**< For Load. */
193   store_attr     store;  /**< For Store. */
194   int            phi0_pos;  /**< For Phi. Used to remember the value defined by
195                    this Phi node.  Needed when the Phi is completed
196                    to call get_r_internal_value to find the
197                    predecessors. If this attribute is set, the Phi
198                    node takes the role of the obsolete Phi0 node,
199                    therefore the name. */
200   int *phi_backedge;    /**< For Phi after construction.
201                Field n set to true if pred n is backedge.
202                @todo Ev. replace by bitfield! */
203   long           proj;  /**< For Proj: contains the result position to project */
204   confirm_attr   confirm_cmp;   /**< For Confirm: compare operation */
205   filter_attr    filter;    /**< For Filter */
206   end_attr       end;       /**< For EndReg, EndExcept */
207   except_attr    except; /**< For Phi node construction in case of exceptions */
208 } attr;
209
210
211 /** common structure of an irnode
212     if the node has some attributes, they are stored in attr */
213 struct ir_node {
214   /* ------- Basics of the representation  ------- */
215   firm_kind kind;          /**< distinguishes this node from others */
216   ir_op *op;               /**< Opcode of this node. */
217   ir_mode *mode;           /**< Mode of this node. */
218   unsigned long visited;   /**< visited counter for walks of the graph */
219   struct ir_node **in;     /**< array with predecessors / operands */
220   void *link;              /**< to attach additional information to the node, e.g.
221                               used while construction to link Phi0 nodes and
222                               during optimization to link to nodes that
223                               shall replace a node. */
224   /* ------- Fields for optimizations / analysis information ------- */
225   struct ir_node **out;    /**< array of out edges */
226   struct dbg_info* dbi;    /**< A pointer to information for debug support. */
227   /* ------- For debugging ------- */
228 #ifdef DEBUG_libfirm
229   int out_valid;           /** < indicate that out edges are valid */
230   int node_nr;             /**< a unique node number for each node to make output
231                               readable. */
232 #endif
233   /* ------- For analyses -------- */
234   ir_loop *loop;           /**< the loop the node is in. Access routines in irloop.h */
235 #ifdef  DO_HEAPANALYSIS
236   struct abstval *av;
237   struct section *sec;
238 #endif
239   /* ------- Opcode depending fields -------- */
240   attr attr;               /**< attribute of this node. Depends on opcode.
241                               Must be last field of struct ir_node. */
242 };
243
244
245 /** Copies all attributes stored in the old node  to the new node.
246     Assumes both have the same opcode and sufficient size. */
247 void
248 copy_attrs(const ir_node *old_node, ir_node *new_node);
249
250
251 /** Returns the array with the ins.  The content of the array may not be
252    changed.  */
253 ir_node     **get_irn_in            (const ir_node *node);
254
255 /** @{ */
256 /** access attributes directly */
257 INLINE const_attr    get_irn_const_attr    (ir_node *node);
258 INLINE long          get_irn_proj_attr     (ir_node *node);
259 INLINE alloc_attr    get_irn_alloc_attr    (ir_node *node);
260 INLINE type         *get_irn_free_attr     (ir_node *node);
261 INLINE symconst_attr get_irn_symconst_attr (ir_node *node);
262 type                *get_irn_call_attr     (ir_node *node);
263 type                *get_irn_funccall_attr (ir_node *node);
264 sel_attr             get_irn_sel_attr      (ir_node *node);
265 int                  get_irn_phi_attr      (ir_node *node);
266 block_attr           get_irn_block_attr    (ir_node *node);
267 load_attr            get_irn_load_attr     (ir_node *node);
268 store_attr           get_irn_store_attr    (ir_node *node);
269 except_attr          get_irn_except_attr   (ir_node *node);
270 /** @} */
271
272 /*-------------------------------------------------------------------*/
273 /*  These function are most used in libfirm.  Give them as static    */
274 /*  functions so they can be inlined.                                */
275 /*-------------------------------------------------------------------*/
276
277 /**
278  * Checks whether a pointer points to a ir node.
279  * Intern version for libFirm.
280  */
281 static INLINE int
282 __is_ir_node (const void *thing) {
283   return (get_kind(thing) == k_ir_node);
284 }
285
286 /**
287  * Gets the op of a node.
288  * Intern version for libFirm.
289  */
290 static INLINE ir_op *
291 __get_irn_op (const ir_node *node) {
292   assert (node);
293   return node->op;
294 }
295
296 /**
297  * Gets the opcode of a node.
298  * Intern version for libFirm.
299  */
300 static INLINE opcode
301 __get_irn_opcode (const ir_node *node) {
302   assert (k_ir_node == get_kind(node));
303   assert (node -> op);
304   return node->op->code;
305 }
306
307 /**
308  * Returns the number of predecessors without the block predecessor.
309  * Intern version for libFirm.
310  */
311 static INLINE int
312 __get_irn_intra_arity (const ir_node *node) {
313   assert(node);
314   return ARR_LEN(node->in) - 1;
315 }
316
317 /**
318  * Returns the number of predecessors without the block predecessor.
319  * Intern version for libFirm.
320  */
321 static INLINE int
322 __get_irn_inter_arity (const ir_node *node) {
323   assert(node);
324   if (__get_irn_opcode(node) == iro_Filter) {
325     assert(node->attr.filter.in_cg);
326     return ARR_LEN(node->attr.filter.in_cg) - 1;
327   } else if (__get_irn_opcode(node) == iro_Block && node->attr.block.in_cg) {
328     return ARR_LEN(node->attr.block.in_cg) - 1;
329   }
330   return __get_irn_intra_arity(node);
331 }
332
333 /**
334  * Returns the number of predecessors without the block predecessor.
335  * Intern version for libFirm.
336  */
337 extern int (*__get_irn_arity)(const ir_node *node);
338
339 /**
340  * Intern version for libFirm.
341  */
342 static INLINE ir_node *
343 __get_irn_intra_n (const ir_node *node, int n) {
344   assert(node); assert(-1 <= n && n < __get_irn_intra_arity(node));
345
346   return (node->in[n + 1] = skip_Id(node->in[n + 1]));
347 }
348
349 /**
350  * Intern version for libFirm.
351  */
352 static INLINE ir_node*
353 __get_irn_inter_n (const ir_node *node, int n) {
354   assert(node); assert(-1 <= n && n < __get_irn_inter_arity(node));
355
356   /* handle Filter and Block specially */
357   if (__get_irn_opcode(node) == iro_Filter) {
358     assert(node->attr.filter.in_cg);
359     return (node->attr.filter.in_cg[n + 1] = skip_Id(node->attr.filter.in_cg[n + 1]));
360   } else if (__get_irn_opcode(node) == iro_Block && node->attr.block.in_cg) {
361     return (node->attr.block.in_cg[n + 1] = skip_Id(node->attr.block.in_cg[n + 1]));
362   }
363
364   return __get_irn_intra_n (node, n);
365 }
366
367 /**
368  * Access to the predecessors of a node.
369  * To iterate over the operands iterate from 0 to i < get_irn_arity(),
370  * to iterate including the Block predecessor iterate from i = -1 to
371  * i < get_irn_arity.
372  * If it is a block, the entry -1 is NULL.
373  * Intern version for libFirm.
374  */
375 extern ir_node *(*__get_irn_n)(const ir_node *node, int n);
376
377 /**
378  * Gets the mode of a node.
379  * Intern version for libFirm.
380  */
381 static INLINE ir_mode *
382 __get_irn_mode (const ir_node *node)
383 {
384   assert (node);
385   return node->mode;
386 }
387
388 /**
389  * Sets the mode of a node.
390  * Intern version of libFirm.
391  */
392 static INLINE void
393 __set_irn_mode (ir_node *node, ir_mode *mode)
394 {
395   assert (node);
396   node->mode = mode;
397 }
398
399 /**
400  * Gets the visited counter of a node.
401  * Intern version for libFirm.
402  */
403 static INLINE unsigned long
404 __get_irn_visited (const ir_node *node)
405 {
406   assert (node);
407   return node->visited;
408 }
409
410 /**
411  * Sets the visited counter of a node.
412  * Intern version for libFirm.
413  */
414 static INLINE void
415 __set_irn_visited (ir_node *node, unsigned long visited)
416 {
417   assert (node);
418   node->visited = visited;
419 }
420
421 /**
422  * Mark a node as visited in a graph.
423  * Intern version for libFirm.
424  */
425 static INLINE void
426 __mark_irn_visited (ir_node *node) {
427   assert (node);
428   node->visited = current_ir_graph->visited;
429 }
430
431 /**
432  * Returns non-zero if a node of was visited.
433  * Intern version for libFirm.
434  */
435 static INLINE int
436 __irn_visited(const ir_node *node) {
437   assert (node);
438   return (node->visited >= current_ir_graph->visited);
439 }
440
441 /**
442  * Returns non-zero if a node of was NOT visited.
443  * Intern version for libFirm.
444  */
445 static INLINE int
446 __irn_not_visited(const ir_node *node) {
447   assert (node);
448   return (node->visited < current_ir_graph->visited);
449 }
450
451 /**
452  * Sets the link of a node.
453  * Intern version of libFirm.
454  */
455 static INLINE void
456 __set_irn_link(ir_node *node, void *link) {
457   assert (node);
458   /* Link field is used for Phi construction and various optimizations
459      in iropt. */
460   assert(get_irg_phase_state(current_ir_graph) != phase_building);
461
462   node->link = link;
463 }
464
465 /**
466  * Returns the link of a node.
467  * Intern version of libFirm.
468  */
469 static INLINE void *
470 __get_irn_link(const ir_node *node) {
471   assert (node);
472   return node->link;
473 }
474
475 /**
476  * Returns the pinned state of a node.
477  * Intern version of libFirm.
478  */
479 static INLINE op_pin_state
480 __get_irn_pinned(const ir_node *node) {
481   op_pin_state state = __get_op_pinned(__get_irn_op(node));
482   if (state >= op_pin_state_exc_pinned)
483     return get_opt_fragile_ops() ? node->attr.except.pin_state : op_pin_state_pinned;
484   return state;
485 }
486
487 static INLINE int
488 __is_unop(const ir_node *node) {
489   return (node->op->opar == oparity_unary);
490 }
491
492 static INLINE int
493 __is_binop(const ir_node *node) {
494   return (node->op->opar == oparity_binary);
495 }
496
497 static INLINE int
498 __is_Bad(const ir_node *node) {
499   assert(node);
500   return (node && __get_irn_op(node) == op_Bad);
501 }
502
503 static INLINE int
504 __is_no_Block(const ir_node *node) {
505   assert(node);
506   return (__get_irn_op(node) != op_Block);
507 }
508
509 static INLINE int
510 __is_Block(const ir_node *node) {
511   assert(node);
512   return (__get_irn_op(node) == op_Block);
513 }
514
515 static INLINE ir_node *
516 __set_Block_dead(ir_node *block) {
517   assert(__get_irn_op(block) == op_Block);
518   block->attr.block.dead = 1;
519   return block;
520 }
521
522 static INLINE int
523 __is_Block_dead(const ir_node *block) {
524   ir_op * op = __get_irn_op(block);
525
526   if (op == op_Bad)
527     return 1;
528   else {
529     assert(op == op_Block);
530     return block->attr.block.dead;
531   }
532 }
533
534 /* this section MUST contain all inline functions */
535 #define is_ir_node(thing)          __is_ir_node(thing)
536 #define get_irn_intra_arity(node)  __get_irn_intra_arity(node)
537 #define get_irn_inter_arity(node)  __get_irn_inter_arity(node)
538 #define get_irn_arity(node)        __get_irn_arity(node)
539 #define get_irn_intra_n(node, n)   __get_irn_intra_n(node, n)
540 #define get_irn_inter_n(node, n)   __get_irn_inter_n(node, n)
541 #define get_irn_n(node, n)         __get_irn_n(node, n)
542 #define get_irn_mode(node)         __get_irn_mode(node)
543 #define set_irn_mode(node, mode)   __set_irn_mode(node, mode)
544 #define get_irn_op(node)           __get_irn_op(node)
545 #define get_irn_opcode(node)       __get_irn_opcode(node)
546 #define get_irn_visited(node)      __get_irn_visited(node)
547 #define set_irn_visited(node, v)   __set_irn_visited(node, v)
548 #define mark_irn_visited(node)     __mark_irn_visited(node)
549 #define irn_visited(node)          __irn_visited(node)
550 #define irn_not_visited(node)      __irn_not_visited(node)
551 #define set_irn_link(node, link)   __set_irn_link(node, link)
552 #define get_irn_link(node)         __get_irn_link(node)
553 #define is_unop(node)              __is_unop(node)
554 #define is_binop(node)             __is_binop(node)
555 #define is_Bad(node)               __is_Bad(node)
556 #define is_no_Block(node)          __is_no_Block(node)
557 #define is_Block(node)             __is_Block(node)
558 #define set_Block_dead(block)      __set_Block_dead(block)
559 #define is_Block_dead(block)       __is_Block_dead(block)
560
561 # endif /* _IRNODE_T_H_ */