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