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