added doxygen comments
[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  * @file irnode_t.h
15  *
16  * Declarations of an ir node.
17  *
18  * @author Martin Trapp, Christian Schaefer
19  */
20
21 # ifndef _IRNODE_T_H_
22 # define _IRNODE_T_H_
23
24 #include "firm_config.h"
25 #include "irnode.h"
26 #include "irop_t.h"
27 #include "irgraph_t.h"
28 #include "irflag_t.h"
29 #include "firm_common_t.h"
30 #include "irdom_t.h" /* For size of struct dom_info. */
31 #include "dbginfo.h"
32 #include "irloop.h"
33 #include "array.h"
34
35 #include "set.h"
36 #include "list.h"
37 #include "entity_t.h"
38 #include "type_t.h"
39 #include "tv_t.h"
40
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   int matured:1;                /**< if set, all in-nodes of the block are fixed */
51   int dead:1;                   /**< if set, the block is dead (and could be replace by a Bad */
52   struct ir_node **graph_arr; /**< array to store all parameters */
53   /* Attributes holding analyses information */
54   struct dom_info dom;        /**< Datastructure that holds information about dominators.
55                  @@@ @todo
56                  Eventually overlay with graph_arr as only valid
57                  in different phases.  Eventually inline the whole
58                  datastructure. */
59   /*   exc_t exc;  */            /**< role of this block for exception handling */
60   /*   ir_node *handler_entry; */    /**< handler entry block iff this block is part of a region */
61   ir_node ** in_cg;           /**< array with predecessors in
62                    * interprocedural_view, if they differ
63                    * from intraprocedural predecessors */
64   int *backedge;              /**< Field n set to true if pred n is backedge.
65                      @@@ @todo Ev. replace by bitfield! */
66   int *cg_backedge;           /**< Field n set to true if pred n is interprocedural backedge.
67                      @@@ @todo Ev. replace by bitfield! */
68 } block_attr;
69
70 /** Start attributes */
71 typedef struct {
72   char dummy;
73   /*   ir_graph *irg;   @@@ now in block */
74 } start_attr;
75
76 /** Cond attributes */
77 typedef struct {
78   cond_kind kind;    /**< flavor of Cond */
79   long default_proj; /**< for optimization: biggest Proj number, i.e. the one
80                           used for default. */
81 } cond_attr;
82
83 /** Const attributes */
84 typedef struct {
85   tarval *tv;        /**< the target value */
86   type   *tp;        /**< the source type, for analyses. default: type_unknown. */
87 } const_attr;
88
89 typedef struct {
90   symconst_symbol sym;  // old tori
91   symconst_kind num;
92   type *tp;          /**< the source type, for analyses. default: type_unknown. */
93 } symconst_attr;
94
95 /** Sel attributes */
96 typedef struct {
97   entity *ent;          /**< entity to select */
98 } sel_attr;
99
100 /** Exception attributes */
101 typedef struct {
102   op_pin_state   pin_state;     /**< the pin state for operations that might generate a exception:
103                                      If it's know that no exception will be generated, could be set to
104                                      op_pin_state_floats. */
105 #if PRECISE_EXC_CONTEXT
106   struct ir_node **frag_arr;    /**< For Phi node construction in case of exception */
107 #endif
108 } except_attr;
109
110 typedef struct {
111   except_attr    exc;           /**< the exception attribute. MUST be the first one. */
112   type *cld_tp;                 /**< type of called procedure */
113   entity ** callee_arr;         /**< result of callee analysis */
114 } call_attr;
115
116 /** Alloc attributes */
117 typedef struct {
118   except_attr    exc;           /**< the exception attribute. MUST be the first one. */
119   type *type;                   /**< Type of the allocated object.  */
120   where_alloc where;            /**< stack, heap or other managed part of memory */
121 } alloc_attr;
122
123 /** Free attributes */
124 typedef struct {
125   type *type;                   /**< Type of the allocated object.  */
126   where_alloc where;            /**< stack, heap or other managed part of memory */
127 } free_attr;
128
129 /** InstOf attributes */
130 typedef struct
131 {
132   type *ent;
133   int dfn;
134 } io_attr;
135
136 /** Filter attributes */
137 typedef struct {
138   long proj;                 /**< contains the result position to project (Proj) */
139   ir_node ** in_cg;          /**< array with interprocedural predecessors (Phi) */
140   int *backedge;              /**< Field n set to true if pred n is backedge.
141                      @todo Ev. replace by bitfield! */
142 } filter_attr;
143
144 /** EndReg/EndExcept attributes */
145 typedef struct {
146   char dummy;
147 } end_attr;
148
149 /** CallBegin attributes */
150 typedef struct {
151   ir_node * call;            /**< associated Call-operation */
152 } callbegin_attr;
153
154 /** Cast attributes */
155 typedef struct {
156   type *totype;
157 } cast_attr;
158
159 /** Load attributes */
160 typedef struct {
161   except_attr    exc;           /**< the exception attribute. MUST be the first one. */
162   ir_mode        *load_mode;    /**< the mode of this Load operation */
163   ent_volatility volatility;    /**< the volatility of a Load/Store operation */
164 } load_attr;
165
166 /** Store attributes */
167 typedef struct {
168   except_attr    exc;           /**< the exception attribute. MUST be the first one. */
169   ent_volatility volatility;    /**< the volatility of a Store operation */
170 } store_attr;
171
172 typedef pn_Cmp confirm_attr;    /**< Attribute to hold compare operation */
173
174 /**
175  * Edge info to put into an irn.
176  */
177 typedef struct _irn_edge_info_t {
178   struct list_head outs_head;  /**< The list of all outs */
179   int out_count;               /**< number of outs in the list */
180 } irn_edge_info_t;
181
182
183 /** Some irnodes just have one attribute, these are stored here,
184    some have more. Their name is 'irnodename_attr' */
185 typedef union {
186   start_attr     start; /**< For Start */
187   block_attr     block; /**< For Block: Fields needed to construct it */
188   cond_attr      c;     /**< For Cond. */
189   const_attr     con;   /**< For Const: contains the value of the constant and a type */
190   symconst_attr  i;     /**< For SymConst. */
191   sel_attr       s;     /**< For Sel. */
192   call_attr      call;  /**< For Call: pointer to the type of the method to call */
193   callbegin_attr callbegin; /**< For CallBegin */
194   alloc_attr     a;    /**< For Alloc. */
195   free_attr      f;    /**< For Free. */
196   io_attr        io;    /**< For InstOf */
197   cast_attr      cast;  /**< For Cast. */
198   load_attr      load;  /**< For Load. */
199   store_attr     store;  /**< For Store. */
200   int            phi0_pos;  /**< For Phi. Used to remember the value defined by
201                    this Phi node.  Needed when the Phi is completed
202                    to call get_r_internal_value to find the
203                    predecessors. If this attribute is set, the Phi
204                    node takes the role of the obsolete Phi0 node,
205                    therefore the name. */
206   int *phi_backedge;    /**< For Phi after construction.
207                            Field n set to true if pred n is backedge.
208                            @todo Ev. replace by bitfield! */
209   long           proj;  /**< For Proj: contains the result position to project */
210   confirm_attr   confirm_cmp;   /**< For Confirm: compare operation */
211   filter_attr    filter;    /**< For Filter */
212   end_attr       end;       /**< For EndReg, EndExcept */
213   except_attr    except; /**< For Phi node construction in case of exceptions */
214 } attr;
215
216
217 /** common structure of an irnode
218     if the node has some attributes, they are stored in attr */
219 struct ir_node {
220   /* ------- Basics of the representation  ------- */
221   firm_kind kind;          /**< distinguishes this node from others */
222   ir_op *op;               /**< Opcode of this node. */
223   ir_mode *mode;           /**< Mode of this node. */
224   unsigned long visited;   /**< visited counter for walks of the graph */
225   struct ir_node **in;     /**< array with predecessors / operands */
226   void *link;              /**< to attach additional information to the node, e.g.
227                               used while construction to link Phi0 nodes and
228                               during optimization to link to nodes that
229                               shall replace a node. */
230   /* ------- Fields for optimizations / analysis information ------- */
231   struct ir_node **out;    /**< @deprecated array of out edges. */
232   struct dbg_info* dbi;    /**< A pointer to information for debug support. */
233   /* ------- For debugging ------- */
234 #ifdef DEBUG_libfirm
235         int out_valid;
236   int node_nr;             /**< a unique node number for each node to make output
237                               readable. */
238 #endif
239   /* ------- For analyses -------- */
240   ir_loop *loop;           /**< the loop the node is in. Access routines in irloop.h */
241 #ifdef  DO_HEAPANALYSIS
242   struct abstval *av;
243   struct section *sec;
244 #endif
245 #if FIRM_EDGES_INPLACE
246   irn_edge_info_t edge_info;  /**< everlasting out edges */
247 #endif
248   /* ------- Opcode depending fields -------- */
249   attr attr;               /**< attribute of this node. Depends on opcode.
250                               Must be last field of struct ir_node. */
251 };
252
253
254 /** Returns the array with the ins.  The content of the array may not be
255    changed.  */
256 ir_node     **get_irn_in            (const ir_node *node);
257
258 /** @{ */
259 /** access attributes directly */
260 const_attr    get_irn_const_attr    (ir_node *node);
261 long          get_irn_proj_attr     (ir_node *node);
262 alloc_attr    get_irn_alloc_attr    (ir_node *node);
263 free_attr     get_irn_free_attr     (ir_node *node);
264 symconst_attr get_irn_symconst_attr (ir_node *node);
265 type         *get_irn_call_attr     (ir_node *node);
266 type         *get_irn_funccall_attr (ir_node *node);
267 sel_attr      get_irn_sel_attr      (ir_node *node);
268 int           get_irn_phi_attr      (ir_node *node);
269 block_attr    get_irn_block_attr    (ir_node *node);
270 load_attr     get_irn_load_attr     (ir_node *node);
271 store_attr    get_irn_store_attr    (ir_node *node);
272 except_attr   get_irn_except_attr   (ir_node *node);
273 /** @} */
274
275 /*
276  * The amount of additional space for custom data to be allocated upon creating a new node.
277  */
278 extern unsigned firm_add_node_size;
279
280 /*-------------------------------------------------------------------*/
281 /*  These function are most used in libfirm.  Give them as static    */
282 /*  functions so they can be inlined.                                */
283 /*-------------------------------------------------------------------*/
284
285 /**
286  * Checks whether a pointer points to a ir node.
287  * Intern version for libFirm.
288  */
289 static INLINE int
290 _is_ir_node (const void *thing) {
291   return (get_kind(thing) == k_ir_node);
292 }
293
294 /**
295  * Gets the op of a node.
296  * Intern version for libFirm.
297  */
298 static INLINE ir_op *
299 _get_irn_op (const ir_node *node) {
300   assert (node);
301   return node->op;
302 }
303
304 /** Copies all attributes stored in the old node  to the new node.
305     Assumes both have the same opcode and sufficient size. */
306 static INLINE void
307 copy_node_attr(const ir_node *old_node, ir_node *new_node) {
308   ir_op *op = _get_irn_op(old_node);
309
310   /* must always exist */
311   op->copy_attr(old_node, new_node);
312 }
313
314 /**
315  * Gets the opcode of a node.
316  * Intern version for libFirm.
317  */
318 static INLINE opcode
319 _get_irn_opcode (const ir_node *node) {
320   assert (k_ir_node == get_kind(node));
321   assert (node->op);
322   return node->op->code;
323 }
324
325 /**
326  * Returns the number of predecessors without the block predecessor.
327  * Intern version for libFirm.
328  */
329 static INLINE int
330 _get_irn_intra_arity (const ir_node *node) {
331   assert(node);
332   return ARR_LEN(node->in) - 1;
333 }
334
335 /**
336  * Returns the number of predecessors without the block predecessor.
337  * Intern version for libFirm.
338  */
339 static INLINE int
340 _get_irn_inter_arity (const ir_node *node) {
341   assert(node);
342   if (_get_irn_opcode(node) == iro_Filter) {
343     assert(node->attr.filter.in_cg);
344     return ARR_LEN(node->attr.filter.in_cg) - 1;
345   } else if (_get_irn_opcode(node) == iro_Block && node->attr.block.in_cg) {
346     return ARR_LEN(node->attr.block.in_cg) - 1;
347   }
348   return _get_irn_intra_arity(node);
349 }
350
351 /**
352  * Returns the number of predecessors without the block predecessor.
353  * Intern version for libFirm.
354  */
355 extern int (*_get_irn_arity)(const ir_node *node);
356
357 /**
358  * Intern version for libFirm.
359  */
360 static INLINE ir_node *
361 _get_irn_intra_n (const ir_node *node, int n) {
362   assert(node); assert(-1 <= n && n < _get_irn_intra_arity(node));
363
364   return (node->in[n + 1] = skip_Id(node->in[n + 1]));
365 }
366
367 /**
368  * Intern version for libFirm.
369  */
370 static INLINE ir_node*
371 _get_irn_inter_n (const ir_node *node, int n) {
372   assert(node); assert(-1 <= n && n < _get_irn_inter_arity(node));
373
374   /* handle Filter and Block specially */
375   if (_get_irn_opcode(node) == iro_Filter) {
376     assert(node->attr.filter.in_cg);
377     return (node->attr.filter.in_cg[n + 1] = skip_Id(node->attr.filter.in_cg[n + 1]));
378   } else if (_get_irn_opcode(node) == iro_Block && node->attr.block.in_cg) {
379     return (node->attr.block.in_cg[n + 1] = skip_Id(node->attr.block.in_cg[n + 1]));
380   }
381
382   return _get_irn_intra_n (node, n);
383 }
384
385 /**
386  * Access to the predecessors of a node.
387  * To iterate over the operands iterate from 0 to i < get_irn_arity(),
388  * to iterate including the Block predecessor iterate from i = -1 to
389  * i < get_irn_arity.
390  * If it is a block, the entry -1 is NULL.
391  * Intern version for libFirm.
392  */
393 extern ir_node *(*_get_irn_n)(const ir_node *node, int n);
394
395 /**
396  * Gets the mode of a node.
397  * Intern version for libFirm.
398  */
399 static INLINE ir_mode *
400 _get_irn_mode (const ir_node *node)
401 {
402   assert (node);
403   return node->mode;
404 }
405
406 /**
407  * Sets the mode of a node.
408  * Intern version of libFirm.
409  */
410 static INLINE void
411 _set_irn_mode (ir_node *node, ir_mode *mode)
412 {
413   assert (node);
414   node->mode = mode;
415 }
416
417 /**
418  * Gets the visited counter of a node.
419  * Intern version for libFirm.
420  */
421 static INLINE unsigned long
422 _get_irn_visited (const ir_node *node)
423 {
424   assert (node);
425   return node->visited;
426 }
427
428 /**
429  * Sets the visited counter of a node.
430  * Intern version for libFirm.
431  */
432 static INLINE void
433 _set_irn_visited (ir_node *node, unsigned long visited)
434 {
435   assert (node);
436   node->visited = visited;
437 }
438
439 /**
440  * Mark a node as visited in a graph.
441  * Intern version for libFirm.
442  */
443 static INLINE void
444 _mark_irn_visited (ir_node *node) {
445   assert (node);
446   node->visited = current_ir_graph->visited;
447 }
448
449 /**
450  * Returns non-zero if a node of was visited.
451  * Intern version for libFirm.
452  */
453 static INLINE int
454 _irn_visited(const ir_node *node) {
455   assert (node);
456   return (node->visited >= current_ir_graph->visited);
457 }
458
459 /**
460  * Returns non-zero if a node of was NOT visited.
461  * Intern version for libFirm.
462  */
463 static INLINE int
464 _irn_not_visited(const ir_node *node) {
465   assert (node);
466   return (node->visited < current_ir_graph->visited);
467 }
468
469 /**
470  * Sets the link of a node.
471  * Intern version of libFirm.
472  */
473 static INLINE void
474 _set_irn_link(ir_node *node, void *link) {
475   assert (node);
476   /* Link field is used for Phi construction and various optimizations
477      in iropt. */
478   assert(get_irg_phase_state(current_ir_graph) != phase_building);
479
480   node->link = link;
481 }
482
483 /**
484  * Returns the link of a node.
485  * Intern version of libFirm.
486  */
487 static INLINE void *
488 _get_irn_link(const ir_node *node) {
489   assert (node && _is_ir_node(node));
490   return node->link;
491 }
492
493 /**
494  * Returns the pinned state of a node.
495  * Intern version of libFirm.
496  */
497 static INLINE op_pin_state
498 _get_irn_pinned(const ir_node *node) {
499   op_pin_state state;
500   assert(node && _is_ir_node(node));
501   state = __get_op_pinned(_get_irn_op(node));
502   if (state >= op_pin_state_exc_pinned)
503     return get_opt_fragile_ops() ? node->attr.except.pin_state : op_pin_state_pinned;
504   return state;
505 }
506
507 static INLINE int
508 _is_unop(const ir_node *node) {
509   assert(node && _is_ir_node(node));
510   return (node->op->opar == oparity_unary);
511 }
512
513 static INLINE int
514 _is_binop(const ir_node *node) {
515   assert(node && _is_ir_node(node));
516   return (node->op->opar == oparity_binary);
517 }
518
519 static INLINE int
520 _is_Bad(const ir_node *node) {
521   assert(node);
522   return (node && _get_irn_op(node) == op_Bad);
523 }
524
525 static INLINE int
526 _is_no_Block(const ir_node *node) {
527   assert(node && _is_ir_node(node));
528   return (_get_irn_op(node) != op_Block);
529 }
530
531 static INLINE int
532 _is_Block(const ir_node *node) {
533   assert(node && _is_ir_node(node));
534   return (_get_irn_op(node) == op_Block);
535 }
536
537 static INLINE ir_node *
538 _set_Block_dead(ir_node *block) {
539   assert(_get_irn_op(block) == op_Block);
540   block->attr.block.dead = 1;
541   return block;
542 }
543
544 static INLINE int
545 _is_Block_dead(const ir_node *block) {
546   ir_op * op = _get_irn_op(block);
547
548   if (op == op_Bad)
549     return 1;
550   else {
551     assert(op == op_Block);
552     return block->attr.block.dead;
553   }
554 }
555
556 static INLINE tarval *_get_Const_tarval (ir_node *node) {
557   assert (node->op == op_Const);
558   return node->attr.con.tv;
559 }
560
561
562 static INLINE cnst_classify_t _classify_Const(ir_node *node)
563 {
564         ir_op *op;
565   assert(_is_ir_node(node));
566
567         op = _get_irn_op(node);
568
569         if(op == op_Const)
570                 return classify_tarval(_get_Const_tarval(node));
571         else if(op == op_SymConst)
572                 return CNST_SYMCONST;
573
574         return CNST_NO_CONST;
575 }
576
577
578 /* this section MUST contain all inline functions */
579 #define is_ir_node(thing)          _is_ir_node(thing)
580 #define get_irn_intra_arity(node)  _get_irn_intra_arity(node)
581 #define get_irn_inter_arity(node)  _get_irn_inter_arity(node)
582 #define get_irn_arity(node)        _get_irn_arity(node)
583 #define get_irn_intra_n(node, n)   _get_irn_intra_n(node, n)
584 #define get_irn_inter_n(node, n)   _get_irn_inter_n(node, n)
585 #define get_irn_n(node, n)         _get_irn_n(node, n)
586 #define get_irn_mode(node)         _get_irn_mode(node)
587 #define set_irn_mode(node, mode)   _set_irn_mode(node, mode)
588 #define get_irn_op(node)           _get_irn_op(node)
589 #define get_irn_opcode(node)       _get_irn_opcode(node)
590 #define get_irn_visited(node)      _get_irn_visited(node)
591 #define set_irn_visited(node, v)   _set_irn_visited(node, v)
592 #define mark_irn_visited(node)     _mark_irn_visited(node)
593 #define irn_visited(node)          _irn_visited(node)
594 #define irn_not_visited(node)      _irn_not_visited(node)
595 #define set_irn_link(node, link)   _set_irn_link(node, link)
596 #define get_irn_link(node)         _get_irn_link(node)
597 #define is_unop(node)              _is_unop(node)
598 #define is_binop(node)             _is_binop(node)
599 #define is_Bad(node)               _is_Bad(node)
600 #define is_no_Block(node)          _is_no_Block(node)
601 #define is_Block(node)             _is_Block(node)
602 #define set_Block_dead(block)      _set_Block_dead(block)
603 #define is_Block_dead(block)       _is_Block_dead(block)
604 #define get_Const_tarval(node)           _get_Const_tarval(node)
605 #define classify_Const(node)                     _classify_Const(node)
606
607 # endif /* _IRNODE_T_H_ */