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