f5050e934a5bfe4dc0122cfb86c42b4122b188a3
[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 #include "irextbb_t.h"
41
42
43 /** ir node attributes **/
44
45 /** Block attributes */
46 typedef struct {
47   /* General attributes */
48   ir_graph *irg;
49   unsigned long block_visited;  /**< for the walker that walks over all blocks. */
50   /* Attributes private to construction: */
51   int matured:1;                /**< if set, all in-nodes of the block are fixed */
52   int dead:1;                   /**< if set, the block is dead (and could be replace by a Bad */
53   struct ir_node **graph_arr; /**< array to store all parameters */
54   /* Attributes holding analyses information */
55   struct dom_info dom;        /**< Datastructure that holds information about dominators.
56                  @@@ @todo
57                  Eventually overlay with graph_arr as only valid
58                  in different phases.  Eventually inline the whole
59                  datastructure. */
60   /*   exc_t exc;  */            /**< role of this block for exception handling */
61   /*   ir_node *handler_entry; */    /**< handler entry block iff this block is part of a region */
62   ir_node ** in_cg;           /**< array with predecessors in
63                    * interprocedural_view, if they differ
64                    * from intraprocedural predecessors */
65   int *backedge;              /**< Field n set to true if pred n is backedge.
66                      @@@ @todo Ev. replace by bitfield! */
67   int *cg_backedge;           /**< Field n set to true if pred n is interprocedural backedge.
68                      @@@ @todo Ev. replace by bitfield! */
69   ir_extblk *extblk;          /**< the extended basic block this block belongs to */
70
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 /** Free attributes */
127 typedef struct {
128   type *type;                   /**< Type of the allocated object.  */
129   where_alloc where;            /**< stack, heap or other managed part of memory */
130 } free_attr;
131
132 /** InstOf attributes */
133 typedef struct
134 {
135   type *ent;
136   int dfn;
137 } io_attr;
138
139 /** Filter attributes */
140 typedef struct {
141   long proj;                 /**< contains the result position to project (Proj) */
142   ir_node ** in_cg;          /**< array with interprocedural predecessors (Phi) */
143   int *backedge;              /**< Field n set to true if pred n is backedge.
144                      @todo Ev. replace by bitfield! */
145 } filter_attr;
146
147 /** EndReg/EndExcept attributes */
148 typedef struct {
149   char dummy;
150 } end_attr;
151
152 /** CallBegin attributes */
153 typedef struct {
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 /**
178  * Edge info to put into an irn.
179  */
180 typedef struct _irn_edge_info_t {
181   struct list_head outs_head;  /**< The list of all outs */
182   int out_count;               /**< number of outs in the list */
183 } irn_edge_info_t;
184
185
186 /** Some irnodes just have one attribute, these are stored here,
187    some have more. Their name is 'irnodename_attr' */
188 typedef union {
189   start_attr     start; /**< For Start */
190   block_attr     block; /**< For Block: Fields needed to construct it */
191   cond_attr      c;     /**< For Cond. */
192   const_attr     con;   /**< For Const: contains the value of the constant and a type */
193   symconst_attr  i;     /**< For SymConst. */
194   sel_attr       s;     /**< For Sel. */
195   call_attr      call;  /**< For Call: pointer to the type of the method to call */
196   callbegin_attr callbegin; /**< For CallBegin */
197   alloc_attr     a;    /**< For Alloc. */
198   free_attr      f;    /**< For Free. */
199   io_attr        io;    /**< For InstOf */
200   cast_attr      cast;  /**< For Cast. */
201   load_attr      load;  /**< For Load. */
202   store_attr     store;  /**< For Store. */
203   int            phi0_pos;  /**< For Phi. Used to remember the value defined by
204                    this Phi node.  Needed when the Phi is completed
205                    to call get_r_internal_value to find the
206                    predecessors. If this attribute is set, the Phi
207                    node takes the role of the obsolete Phi0 node,
208                    therefore the name. */
209   int *phi_backedge;    /**< For Phi after construction.
210                            Field n set to true if pred n is backedge.
211                            @todo Ev. replace by bitfield! */
212   long           proj;  /**< For Proj: contains the result position to project */
213   confirm_attr   confirm_cmp;   /**< For Confirm: compare operation */
214   filter_attr    filter;    /**< For Filter */
215   end_attr       end;       /**< For EndReg, EndExcept */
216   except_attr    except; /**< For Phi node construction in case of exceptions */
217 } attr;
218
219
220 /** common structure of an irnode
221     if the node has some attributes, they are stored in attr */
222 struct ir_node {
223   /* ------- Basics of the representation  ------- */
224   firm_kind kind;          /**< distinguishes this node from others */
225   ir_op *op;               /**< Opcode of this node. */
226   ir_mode *mode;           /**< Mode of this node. */
227   unsigned long visited;   /**< visited counter for walks of the graph */
228   struct ir_node **in;     /**< array with predecessors / operands */
229   void *link;              /**< to attach additional information to the node, e.g.
230                               used while construction to link Phi0 nodes and
231                               during optimization to link to nodes that
232                               shall replace a node. */
233   /* ------- Fields for optimizations / analysis information ------- */
234   struct ir_node **out;    /**< @deprecated array of out edges. */
235   struct dbg_info* dbi;    /**< A pointer to information for debug support. */
236   /* ------- For debugging ------- */
237 #ifdef DEBUG_libfirm
238         int out_valid;
239   int node_nr;             /**< a unique node number for each node to make output
240                               readable. */
241 #endif
242   /* ------- For analyses -------- */
243   ir_loop *loop;           /**< the loop the node is in. Access routines in irloop.h */
244 #ifdef  DO_HEAPANALYSIS
245   struct abstval *av;
246   struct section *sec;
247 #endif
248 #if FIRM_EDGES_INPLACE
249   irn_edge_info_t edge_info;  /**< everlasting out edges */
250 #endif
251   /* ------- Opcode depending fields -------- */
252   attr attr;               /**< attribute of this node. Depends on opcode.
253                               Must be last field of struct ir_node. */
254 };
255
256
257 /** Returns the array with the ins.  The content of the array may not be
258    changed.  */
259 ir_node     **get_irn_in            (const ir_node *node);
260
261 /** @{ */
262 /** access attributes directly */
263 const_attr    get_irn_const_attr    (ir_node *node);
264 long          get_irn_proj_attr     (ir_node *node);
265 alloc_attr    get_irn_alloc_attr    (ir_node *node);
266 free_attr     get_irn_free_attr     (ir_node *node);
267 symconst_attr get_irn_symconst_attr (ir_node *node);
268 type         *get_irn_call_attr     (ir_node *node);
269 type         *get_irn_funccall_attr (ir_node *node);
270 sel_attr      get_irn_sel_attr      (ir_node *node);
271 int           get_irn_phi_attr      (ir_node *node);
272 block_attr    get_irn_block_attr    (ir_node *node);
273 load_attr     get_irn_load_attr     (ir_node *node);
274 store_attr    get_irn_store_attr    (ir_node *node);
275 except_attr   get_irn_except_attr   (ir_node *node);
276 /** @} */
277
278 /**
279  * The amount of additional space for custom data to be allocated upon creating a new node.
280  */
281 extern unsigned firm_add_node_size;
282
283 /** Set the get_type operation of an ir_op. */
284 ir_op *firm_set_default_get_type(ir_op *op);
285
286 /*-------------------------------------------------------------------*/
287 /*  These function are most used in libfirm.  Give them as static    */
288 /*  functions so they can be inlined.                                */
289 /*-------------------------------------------------------------------*/
290
291 /**
292  * Checks whether a pointer points to a ir node.
293  * Intern version for libFirm.
294  */
295 static INLINE int
296 _is_ir_node (const void *thing) {
297   return (get_kind(thing) == k_ir_node);
298 }
299
300 /**
301  * Gets the op of a node.
302  * Intern version for libFirm.
303  */
304 static INLINE ir_op *
305 _get_irn_op (const ir_node *node) {
306   assert (node);
307   return node->op;
308 }
309
310 /** Copies all attributes stored in the old node  to the new node.
311     Assumes both have the same opcode and sufficient size. */
312 static INLINE void
313 copy_node_attr(const ir_node *old_node, ir_node *new_node) {
314   ir_op *op = _get_irn_op(old_node);
315
316   /* must always exist */
317   op->copy_attr(old_node, new_node);
318 }
319
320 /**
321  * Gets the opcode of a node.
322  * Intern version for libFirm.
323  */
324 static INLINE opcode
325 _get_irn_opcode (const ir_node *node) {
326   assert (k_ir_node == get_kind(node));
327   assert (node->op);
328   return node->op->code;
329 }
330
331 /**
332  * Returns the number of predecessors without the block predecessor.
333  * Intern version for libFirm.
334  */
335 static INLINE int
336 _get_irn_intra_arity (const ir_node *node) {
337   assert(node);
338   return ARR_LEN(node->in) - 1;
339 }
340
341 /**
342  * Returns the number of predecessors without the block predecessor.
343  * Intern version for libFirm.
344  */
345 static INLINE int
346 _get_irn_inter_arity (const ir_node *node) {
347   assert(node);
348   if (_get_irn_opcode(node) == iro_Filter) {
349     assert(node->attr.filter.in_cg);
350     return ARR_LEN(node->attr.filter.in_cg) - 1;
351   } else if (_get_irn_opcode(node) == iro_Block && node->attr.block.in_cg) {
352     return ARR_LEN(node->attr.block.in_cg) - 1;
353   }
354   return _get_irn_intra_arity(node);
355 }
356
357 /**
358  * Returns the number of predecessors without the block predecessor.
359  * Intern version for libFirm.
360  */
361 extern int (*_get_irn_arity)(const ir_node *node);
362
363 /**
364  * Intern version for libFirm.
365  */
366 static INLINE ir_node *
367 _get_irn_intra_n (const ir_node *node, int n) {
368   assert(node); assert(-1 <= n && n < _get_irn_intra_arity(node));
369
370   return (node->in[n + 1] = skip_Id(node->in[n + 1]));
371 }
372
373 /**
374  * Intern version for libFirm.
375  */
376 static INLINE ir_node*
377 _get_irn_inter_n (const ir_node *node, int n) {
378   assert(node); assert(-1 <= n && n < _get_irn_inter_arity(node));
379
380   /* handle Filter and Block specially */
381   if (_get_irn_op(node) == op_Filter) {
382     assert(node->attr.filter.in_cg);
383     return (node->attr.filter.in_cg[n + 1] = skip_Id(node->attr.filter.in_cg[n + 1]));
384   } else if (_get_irn_op(node) == op_Block && node->attr.block.in_cg) {
385     return (node->attr.block.in_cg[n + 1] = skip_Id(node->attr.block.in_cg[n + 1]));
386   }
387
388   return _get_irn_intra_n (node, n);
389 }
390
391 /**
392  * Access to the predecessors of a node.
393  * To iterate over the operands iterate from 0 to i < get_irn_arity(),
394  * to iterate including the Block predecessor iterate from i = -1 to
395  * i < get_irn_arity.
396  * If it is a block, the entry -1 is NULL.
397  * Intern version for libFirm.
398  */
399 extern ir_node *(*_get_irn_n)(const ir_node *node, int n);
400
401 /**
402  * Gets the mode of a node.
403  * Intern version for libFirm.
404  */
405 static INLINE ir_mode *
406 _get_irn_mode (const ir_node *node)
407 {
408   assert (node);
409   return node->mode;
410 }
411
412 /**
413  * Sets the mode of a node.
414  * Intern version of libFirm.
415  */
416 static INLINE void
417 _set_irn_mode (ir_node *node, ir_mode *mode)
418 {
419   assert (node);
420   node->mode = mode;
421 }
422
423 /**
424  * Gets the visited counter of a node.
425  * Intern version for libFirm.
426  */
427 static INLINE unsigned long
428 _get_irn_visited (const ir_node *node)
429 {
430   assert (node);
431   return node->visited;
432 }
433
434 /**
435  * Sets the visited counter of a node.
436  * Intern version for libFirm.
437  */
438 static INLINE void
439 _set_irn_visited (ir_node *node, unsigned long visited)
440 {
441   assert (node);
442   node->visited = visited;
443 }
444
445 /**
446  * Mark a node as visited in a graph.
447  * Intern version for libFirm.
448  */
449 static INLINE void
450 _mark_irn_visited (ir_node *node) {
451   assert (node);
452   node->visited = current_ir_graph->visited;
453 }
454
455 /**
456  * Returns non-zero if a node of was visited.
457  * Intern version for libFirm.
458  */
459 static INLINE int
460 _irn_visited(const ir_node *node) {
461   assert (node);
462   return (node->visited >= current_ir_graph->visited);
463 }
464
465 /**
466  * Returns non-zero if a node of was NOT visited.
467  * Intern version for libFirm.
468  */
469 static INLINE int
470 _irn_not_visited(const ir_node *node) {
471   assert (node);
472   return (node->visited < current_ir_graph->visited);
473 }
474
475 /**
476  * Sets the link of a node.
477  * Intern version of libFirm.
478  */
479 static INLINE void
480 _set_irn_link(ir_node *node, void *link) {
481   assert (node);
482   /* Link field is used for Phi construction and various optimizations
483      in iropt. */
484   assert(get_irg_phase_state(current_ir_graph) != phase_building);
485
486   node->link = link;
487 }
488
489 /**
490  * Returns the link of a node.
491  * Intern version of libFirm.
492  */
493 static INLINE void *
494 _get_irn_link(const ir_node *node) {
495   assert (node && _is_ir_node(node));
496   return node->link;
497 }
498
499 /**
500  * Returns the pinned state of a node.
501  * Intern version of libFirm.
502  */
503 static INLINE op_pin_state
504 _get_irn_pinned(const ir_node *node) {
505   op_pin_state state;
506   assert(node && _is_ir_node(node));
507   state = _get_op_pinned(_get_irn_op(node));
508   if (state >= op_pin_state_exc_pinned)
509     return get_opt_fragile_ops() ? node->attr.except.pin_state : op_pin_state_pinned;
510   return state;
511 }
512
513 static INLINE int
514 _is_unop(const ir_node *node) {
515   assert(node && _is_ir_node(node));
516   return (node->op->opar == oparity_unary);
517 }
518
519 static INLINE int
520 _is_binop(const ir_node *node) {
521   assert(node && _is_ir_node(node));
522   return (node->op->opar == oparity_binary);
523 }
524
525 static INLINE int
526 _is_Bad(const ir_node *node) {
527   assert(node);
528   return (node && _get_irn_op(node) == op_Bad);
529 }
530
531 static INLINE int
532 _is_Const(const ir_node *node) {
533   assert(node);
534   return (node && _get_irn_op(node) == op_Const);
535 }
536
537 static INLINE int
538 _is_Unknown (const ir_node *node) {
539   assert(node);
540   return (node && _get_irn_op(node) == op_Unknown);
541 }
542
543 static INLINE int
544 _is_no_Block(const ir_node *node) {
545   assert(node && _is_ir_node(node));
546   return (_get_irn_op(node) != op_Block);
547 }
548
549 static INLINE int
550 _is_Block(const ir_node *node) {
551   assert(node && _is_ir_node(node));
552   return (_get_irn_op(node) == op_Block);
553 }
554
555 static INLINE unsigned long
556 _get_Block_block_visited (ir_node *node) {
557   assert (node->op == op_Block);
558   return node->attr.block.block_visited;
559 }
560
561 static INLINE void
562 _set_Block_block_visited (ir_node *node, unsigned long visit) {
563   assert (node->op == op_Block);
564   node->attr.block.block_visited = visit;
565 }
566
567 /* For this current_ir_graph must be set. */
568 static INLINE void
569 _mark_Block_block_visited (ir_node *node) {
570   assert (node->op == op_Block);
571   node->attr.block.block_visited = get_irg_block_visited(current_ir_graph);
572 }
573
574 static INLINE int
575 _Block_not_block_visited(ir_node *node) {
576   assert (node->op == op_Block);
577   return (node->attr.block.block_visited < get_irg_block_visited(current_ir_graph));
578 }
579
580 static INLINE ir_node *
581 _set_Block_dead(ir_node *block) {
582   assert(_get_irn_op(block) == op_Block);
583   block->attr.block.dead = 1;
584   return block;
585 }
586
587 static INLINE int
588 _is_Block_dead(const ir_node *block) {
589   ir_op * op = _get_irn_op(block);
590
591   if (op == op_Bad)
592     return 1;
593   else {
594     assert(op == op_Block);
595     return block->attr.block.dead;
596   }
597 }
598
599 static INLINE tarval *_get_Const_tarval (ir_node *node) {
600   assert (_get_irn_op(node) == op_Const);
601   return node->attr.con.tv;
602 }
603
604 static INLINE cnst_classify_t _classify_Const(ir_node *node) {
605   ir_op *op;
606   assert(_is_ir_node(node));
607
608   op = _get_irn_op(node);
609
610   if(op == op_Const)
611     return classify_tarval(_get_Const_tarval(node));
612   else if(op == op_SymConst)
613     return CNST_SYMCONST;
614
615   return CNST_NO_CONST;
616 }
617
618 static INLINE type *_get_irn_type(ir_node *node) {
619   return _get_irn_op(node)->get_type(node);
620 }
621
622 /* this section MUST contain all inline functions */
623 #define is_ir_node(thing)                     _is_ir_node(thing)
624 #define get_irn_intra_arity(node)             _get_irn_intra_arity(node)
625 #define get_irn_inter_arity(node)             _get_irn_inter_arity(node)
626 #define get_irn_arity(node)                   _get_irn_arity(node)
627 #define get_irn_intra_n(node, n)              _get_irn_intra_n(node, n)
628 #define get_irn_inter_n(node, n)              _get_irn_inter_n(node, n)
629 #define get_irn_n(node, n)                    _get_irn_n(node, n)
630 #define get_irn_mode(node)                    _get_irn_mode(node)
631 #define set_irn_mode(node, mode)              _set_irn_mode(node, mode)
632 #define get_irn_op(node)                      _get_irn_op(node)
633 #define get_irn_opcode(node)                  _get_irn_opcode(node)
634 #define get_irn_visited(node)                 _get_irn_visited(node)
635 #define set_irn_visited(node, v)              _set_irn_visited(node, v)
636 #define mark_irn_visited(node)                _mark_irn_visited(node)
637 #define irn_visited(node)                     _irn_visited(node)
638 #define irn_not_visited(node)                 _irn_not_visited(node)
639 #define set_irn_link(node, link)              _set_irn_link(node, link)
640 #define get_irn_link(node)                    _get_irn_link(node)
641 #define is_unop(node)                         _is_unop(node)
642 #define is_binop(node)                        _is_binop(node)
643 #define is_Const(node)                        _is_Const(node)
644 #define is_Unknown(node)                      _is_Unknown(node)
645 #define is_Bad(node)                          _is_Bad(node)
646 #define is_no_Block(node)                     _is_no_Block(node)
647 #define is_Block(node)                        _is_Block(node)
648 #define get_Block_block_visited(node)         _get_Block_block_visited(node)
649 #define set_Block_block_visited(node, visit)  _set_Block_block_visited(node, visit)
650 #define mark_Block_block_visited(node)        _mark_Block_block_visited(node)
651 #define Block_not_block_visited(node)         _Block_not_block_visited(node)
652 #define set_Block_dead(block)                 _set_Block_dead(block)
653 #define is_Block_dead(block)                  _is_Block_dead(block)
654 #define get_Const_tarval(node)                _get_Const_tarval(node)
655 #define classify_Const(node)                  _classify_Const(node)
656 #define get_irn_type(node)                    _get_irn_type(node)
657
658 # endif /* _IRNODE_T_H_ */