more
[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 /** Set the get_type operation of an ir_op. */
281 ir_op *firm_set_default_get_type(ir_op *op);
282
283 /*-------------------------------------------------------------------*/
284 /*  These function are most used in libfirm.  Give them as static    */
285 /*  functions so they can be inlined.                                */
286 /*-------------------------------------------------------------------*/
287
288 /**
289  * Checks whether a pointer points to a ir node.
290  * Intern version for libFirm.
291  */
292 static INLINE int
293 _is_ir_node (const void *thing) {
294   return (get_kind(thing) == k_ir_node);
295 }
296
297 /**
298  * Gets the op of a node.
299  * Intern version for libFirm.
300  */
301 static INLINE ir_op *
302 _get_irn_op (const ir_node *node) {
303   assert (node);
304   return node->op;
305 }
306
307 /** Copies all attributes stored in the old node  to the new node.
308     Assumes both have the same opcode and sufficient size. */
309 static INLINE void
310 copy_node_attr(const ir_node *old_node, ir_node *new_node) {
311   ir_op *op = _get_irn_op(old_node);
312
313   /* must always exist */
314   op->copy_attr(old_node, new_node);
315 }
316
317 /**
318  * Gets the opcode of a node.
319  * Intern version for libFirm.
320  */
321 static INLINE opcode
322 _get_irn_opcode (const ir_node *node) {
323   assert (k_ir_node == get_kind(node));
324   assert (node->op);
325   return node->op->code;
326 }
327
328 /**
329  * Returns the number of predecessors without the block predecessor.
330  * Intern version for libFirm.
331  */
332 static INLINE int
333 _get_irn_intra_arity (const ir_node *node) {
334   assert(node);
335   return ARR_LEN(node->in) - 1;
336 }
337
338 /**
339  * Returns the number of predecessors without the block predecessor.
340  * Intern version for libFirm.
341  */
342 static INLINE int
343 _get_irn_inter_arity (const ir_node *node) {
344   assert(node);
345   if (_get_irn_opcode(node) == iro_Filter) {
346     assert(node->attr.filter.in_cg);
347     return ARR_LEN(node->attr.filter.in_cg) - 1;
348   } else if (_get_irn_opcode(node) == iro_Block && node->attr.block.in_cg) {
349     return ARR_LEN(node->attr.block.in_cg) - 1;
350   }
351   return _get_irn_intra_arity(node);
352 }
353
354 /**
355  * Returns the number of predecessors without the block predecessor.
356  * Intern version for libFirm.
357  */
358 extern int (*_get_irn_arity)(const ir_node *node);
359
360 /**
361  * Intern version for libFirm.
362  */
363 static INLINE ir_node *
364 _get_irn_intra_n (const ir_node *node, int n) {
365   assert(node); assert(-1 <= n && n < _get_irn_intra_arity(node));
366
367   return (node->in[n + 1] = skip_Id(node->in[n + 1]));
368 }
369
370 /**
371  * Intern version for libFirm.
372  */
373 static INLINE ir_node*
374 _get_irn_inter_n (const ir_node *node, int n) {
375   assert(node); assert(-1 <= n && n < _get_irn_inter_arity(node));
376
377   /* handle Filter and Block specially */
378   if (_get_irn_opcode(node) == iro_Filter) {
379     assert(node->attr.filter.in_cg);
380     return (node->attr.filter.in_cg[n + 1] = skip_Id(node->attr.filter.in_cg[n + 1]));
381   } else if (_get_irn_opcode(node) == iro_Block && node->attr.block.in_cg) {
382     return (node->attr.block.in_cg[n + 1] = skip_Id(node->attr.block.in_cg[n + 1]));
383   }
384
385   return _get_irn_intra_n (node, n);
386 }
387
388 /**
389  * Access to the predecessors of a node.
390  * To iterate over the operands iterate from 0 to i < get_irn_arity(),
391  * to iterate including the Block predecessor iterate from i = -1 to
392  * i < get_irn_arity.
393  * If it is a block, the entry -1 is NULL.
394  * Intern version for libFirm.
395  */
396 extern ir_node *(*_get_irn_n)(const ir_node *node, int n);
397
398 /**
399  * Gets the mode of a node.
400  * Intern version for libFirm.
401  */
402 static INLINE ir_mode *
403 _get_irn_mode (const ir_node *node)
404 {
405   assert (node);
406   return node->mode;
407 }
408
409 /**
410  * Sets the mode of a node.
411  * Intern version of libFirm.
412  */
413 static INLINE void
414 _set_irn_mode (ir_node *node, ir_mode *mode)
415 {
416   assert (node);
417   node->mode = mode;
418 }
419
420 /**
421  * Gets the visited counter of a node.
422  * Intern version for libFirm.
423  */
424 static INLINE unsigned long
425 _get_irn_visited (const ir_node *node)
426 {
427   assert (node);
428   return node->visited;
429 }
430
431 /**
432  * Sets the visited counter of a node.
433  * Intern version for libFirm.
434  */
435 static INLINE void
436 _set_irn_visited (ir_node *node, unsigned long visited)
437 {
438   assert (node);
439   node->visited = visited;
440 }
441
442 /**
443  * Mark a node as visited in a graph.
444  * Intern version for libFirm.
445  */
446 static INLINE void
447 _mark_irn_visited (ir_node *node) {
448   assert (node);
449   node->visited = current_ir_graph->visited;
450 }
451
452 /**
453  * Returns non-zero if a node of was visited.
454  * Intern version for libFirm.
455  */
456 static INLINE int
457 _irn_visited(const ir_node *node) {
458   assert (node);
459   return (node->visited >= current_ir_graph->visited);
460 }
461
462 /**
463  * Returns non-zero if a node of was NOT visited.
464  * Intern version for libFirm.
465  */
466 static INLINE int
467 _irn_not_visited(const ir_node *node) {
468   assert (node);
469   return (node->visited < current_ir_graph->visited);
470 }
471
472 /**
473  * Sets the link of a node.
474  * Intern version of libFirm.
475  */
476 static INLINE void
477 _set_irn_link(ir_node *node, void *link) {
478   assert (node);
479   /* Link field is used for Phi construction and various optimizations
480      in iropt. */
481   assert(get_irg_phase_state(current_ir_graph) != phase_building);
482
483   node->link = link;
484 }
485
486 /**
487  * Returns the link of a node.
488  * Intern version of libFirm.
489  */
490 static INLINE void *
491 _get_irn_link(const ir_node *node) {
492   assert (node && _is_ir_node(node));
493   return node->link;
494 }
495
496 /**
497  * Returns the pinned state of a node.
498  * Intern version of libFirm.
499  */
500 static INLINE op_pin_state
501 _get_irn_pinned(const ir_node *node) {
502   op_pin_state state;
503   assert(node && _is_ir_node(node));
504   state = __get_op_pinned(_get_irn_op(node));
505   if (state >= op_pin_state_exc_pinned)
506     return get_opt_fragile_ops() ? node->attr.except.pin_state : op_pin_state_pinned;
507   return state;
508 }
509
510 static INLINE int
511 _is_unop(const ir_node *node) {
512   assert(node && _is_ir_node(node));
513   return (node->op->opar == oparity_unary);
514 }
515
516 static INLINE int
517 _is_binop(const ir_node *node) {
518   assert(node && _is_ir_node(node));
519   return (node->op->opar == oparity_binary);
520 }
521
522 static INLINE int
523 _is_Bad(const ir_node *node) {
524   assert(node);
525   return (node && _get_irn_op(node) == op_Bad);
526 }
527
528 static INLINE int
529 _is_no_Block(const ir_node *node) {
530   assert(node && _is_ir_node(node));
531   return (_get_irn_op(node) != op_Block);
532 }
533
534 static INLINE int
535 _is_Block(const ir_node *node) {
536   assert(node && _is_ir_node(node));
537   return (_get_irn_op(node) == op_Block);
538 }
539
540 static INLINE unsigned long
541 _get_Block_block_visited (ir_node *node) {
542   assert (node->op == op_Block);
543   return node->attr.block.block_visited;
544 }
545
546 static INLINE void
547 _set_Block_block_visited (ir_node *node, unsigned long visit) {
548   assert (node->op == op_Block);
549   node->attr.block.block_visited = visit;
550 }
551
552 /* For this current_ir_graph must be set. */
553 static INLINE void
554 _mark_Block_block_visited (ir_node *node) {
555   assert (node->op == op_Block);
556   node->attr.block.block_visited = get_irg_block_visited(current_ir_graph);
557 }
558
559 static INLINE int
560 _Block_not_block_visited(ir_node *node) {
561   assert (node->op == op_Block);
562   return (node->attr.block.block_visited < get_irg_block_visited(current_ir_graph));
563 }
564
565 static INLINE ir_node *
566 _set_Block_dead(ir_node *block) {
567   assert(_get_irn_op(block) == op_Block);
568   block->attr.block.dead = 1;
569   return block;
570 }
571
572 static INLINE int
573 _is_Block_dead(const ir_node *block) {
574   ir_op * op = _get_irn_op(block);
575
576   if (op == op_Bad)
577     return 1;
578   else {
579     assert(op == op_Block);
580     return block->attr.block.dead;
581   }
582 }
583
584 static INLINE tarval *_get_Const_tarval (ir_node *node) {
585   assert (_get_irn_op(node) == op_Const);
586   return node->attr.con.tv;
587 }
588
589 static INLINE cnst_classify_t _classify_Const(ir_node *node) {
590   ir_op *op;
591   assert(_is_ir_node(node));
592
593   op = _get_irn_op(node);
594
595   if(op == op_Const)
596     return classify_tarval(_get_Const_tarval(node));
597   else if(op == op_SymConst)
598     return CNST_SYMCONST;
599
600   return CNST_NO_CONST;
601 }
602
603 static INLINE type *_get_irn_type(ir_node *node) {
604   return _get_irn_op(node)->get_type(node);
605 }
606
607 /* this section MUST contain all inline functions */
608 #define is_ir_node(thing)                     _is_ir_node(thing)
609 #define get_irn_intra_arity(node)             _get_irn_intra_arity(node)
610 #define get_irn_inter_arity(node)             _get_irn_inter_arity(node)
611 #define get_irn_arity(node)                   _get_irn_arity(node)
612 #define get_irn_intra_n(node, n)              _get_irn_intra_n(node, n)
613 #define get_irn_inter_n(node, n)              _get_irn_inter_n(node, n)
614 #define get_irn_n(node, n)                    _get_irn_n(node, n)
615 #define get_irn_mode(node)                    _get_irn_mode(node)
616 #define set_irn_mode(node, mode)              _set_irn_mode(node, mode)
617 #define get_irn_op(node)                      _get_irn_op(node)
618 #define get_irn_opcode(node)                  _get_irn_opcode(node)
619 #define get_irn_visited(node)                 _get_irn_visited(node)
620 #define set_irn_visited(node, v)              _set_irn_visited(node, v)
621 #define mark_irn_visited(node)                _mark_irn_visited(node)
622 #define irn_visited(node)                     _irn_visited(node)
623 #define irn_not_visited(node)                 _irn_not_visited(node)
624 #define set_irn_link(node, link)              _set_irn_link(node, link)
625 #define get_irn_link(node)                    _get_irn_link(node)
626 #define is_unop(node)                         _is_unop(node)
627 #define is_binop(node)                        _is_binop(node)
628 #define is_Bad(node)                          _is_Bad(node)
629 #define is_no_Block(node)                     _is_no_Block(node)
630 #define is_Block(node)                        _is_Block(node)
631 #define get_Block_block_visited(node)         _get_Block_block_visited(node)
632 #define set_Block_block_visited(node, visit)  _set_Block_block_visited(node, visit)
633 #define mark_Block_block_visited(node)        _mark_Block_block_visited(node)
634 #define Block_not_block_visited(node)         _Block_not_block_visited(node)
635 #define set_Block_dead(block)                 _set_Block_dead(block)
636 #define is_Block_dead(block)                  _is_Block_dead(block)
637 #define get_Const_tarval(node)                _get_Const_tarval(node)
638 #define classify_Const(node)                  _classify_Const(node)
639 #define get_irn_type(node)                    _get_irn_type(node)
640
641 # endif /* _IRNODE_T_H_ */