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