2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Definition of the Firm IR base types, concentrated here
11 #ifndef FIRM_IR_IRDEFS_H
12 #define FIRM_IR_IRDEFS_H
16 #include "firm_types.h"
21 #include "iredgekinds.h"
22 #include "irtypeinfo.h"
24 #include "callgraph.h"
35 void **data; /**< maps node indices to void* */
38 /** The type of an ir_op. */
40 unsigned code; /**< The unique opcode of the op. */
41 ident *name; /**< The name of the op. */
42 size_t attr_size; /**< Space needed in memory for private attributes
44 op_pin_state pin_state; /**< How to deal with the node in CSE, PRE. */
45 op_arity opar; /**< The arity of operator. */
46 int op_index; /**< The index of the first data operand, 0 for
47 most cases, 1 for Div etc. */
48 int memory_index; /**< index of memory input for memory nodes */
49 int pn_x_regular; /**< for fragile ops the position of the
51 int pn_x_except; /**< for fragile ops the position of the
53 unsigned flags; /**< Flags describing the behavior of the ir_op,
54 a bitmasks of irop_flags. */
55 unsigned tag; /**< Some custom TAG value the op's creator set */
56 void *attr; /**< custom pointer where op's creator can attach
57 attribute stuff to. */
58 ir_op_ops ops; /**< The operations of the this op. */
61 /** Helper values for ir_mode_sort. */
62 enum ir_mode_sort_helper {
63 irmsh_is_num = 0x10, /**< mode represents a number */
64 irmsh_is_data = 0x20, /**< mode represents data (can be carried in registers) */
65 irmsh_is_datab = 0x40, /**< mode represents data or is internal boolean */
66 irmsh_is_dataM = 0x80, /**< mode represents data or is memory */
70 * These values represent the different mode classes of value representations.
71 * Beware: do not change the order of these values without checking
74 typedef enum ir_mode_sort {
75 irms_control_flow = 0, /**< Marks all control flow modes. */
80 irms_memory = 5 | irmsh_is_dataM, /**< Marks the memory mode. Not extensible. (irm_M) */
82 /** Internal boolean representation.
83 Storing to memory impossible, convert first. (irm_b) */
84 irms_internal_boolean = 6 | irmsh_is_datab,
86 /** A mode to represent entities.
87 Restricted int computations can be performed */
88 irms_reference = 7 | irmsh_is_data | irmsh_is_datab | irmsh_is_dataM,
89 /** A mode to represent int numbers.
90 Integer computations can be performed. */
91 irms_int_number = 8 | irmsh_is_data | irmsh_is_datab | irmsh_is_dataM | irmsh_is_num,
92 /** A mode to represent float numbers.
93 Floating point computations can be performed. */
94 irms_float_number = 9 | irmsh_is_data | irmsh_is_datab | irmsh_is_dataM | irmsh_is_num,
98 * A descriptor for an IEEE754 float value.
100 typedef struct float_descriptor_t {
101 unsigned char exponent_size; /**< size of exponent in bits */
102 unsigned char mantissa_size; /**< size of mantissa in bits */
103 bool explicit_one; /**< set if the leading one is explicit */
104 } float_descriptor_t;
107 * Contains relevant information about a mode.
109 * Necessary information about a mode is stored in this struct
110 * which is used by the tarval module to perform calculations
111 * and comparisons of values of a such described mode.
114 * - ident *name: Name of this mode. Two modes are different if the name is different.
115 * - ir_mode_sort sort: sort of mode specifying possible usage categories
116 * - int size: size of the mode in Bits.
117 * - unsigned sign:1: signedness of this mode
119 * - modulo_shift specifies for modes of kind irms_int_number
120 * whether shift applies modulo to value of bits to shift
123 * The tech report 1999-44 describing FIRM and predefined modes
127 firm_kind kind; /**< distinguishes this node from others */
128 ident *name; /**< Name ident of this mode */
129 ir_type *type; /**< corresponding primitive type */
131 /* ---------------------------------------------------------------------- */
132 /* On changing this struct you have to evaluate the mode_are_equal function!*/
133 ir_mode_sort sort; /**< coarse classification of this mode:
134 int, float, reference ...
136 ir_mode_arithmetic arithmetic; /**< different arithmetic operations possible with a mode */
137 unsigned size; /**< size of the mode in Bits. */
138 unsigned sign:1; /**< signedness of this mode */
139 unsigned int modulo_shift; /**< number of bits a values of this mode will be shifted */
140 float_descriptor_t float_desc;
142 /* ---------------------------------------------------------------------- */
143 ir_tarval *min; /**< the minimum value that can be expressed */
144 ir_tarval *max; /**< the maximum value that can be expressed */
145 ir_tarval *null; /**< the value 0 */
146 ir_tarval *one; /**< the value 1 */
147 ir_tarval *minus_one; /**< the value -1 */
148 ir_tarval *all_one; /**< the value ~0 */
149 ir_mode *eq_signed; /**< For pointer modes, the equivalent signed integer one. */
150 ir_mode *eq_unsigned; /**< For pointer modes, the equivalent unsigned integer one. */
151 void *link; /**< To store some intermediate information */
152 const void *tv_priv; /**< tarval module will save private data here */
155 /* note: we use "long" here because that is the type used for Proj-Numbers */
156 typedef struct ir_switch_table_entry {
160 } ir_switch_table_entry;
162 struct ir_switch_table {
164 ir_switch_table_entry entries[];
167 /* ir node attributes */
169 /** first attribute of Bad, Block, Anchor nodes */
170 typedef struct irg_attr {
171 ir_graph *irg; /**< The graph this block like node belongs to. */
174 typedef struct bad_attr {
178 typedef struct anchor_attr {
182 /** Block attributes */
183 typedef struct block_attr {
184 /* General attributes */
185 irg_attr irg; /**< The graph this block belongs to. */
186 ir_visited_t block_visited; /**< For the walker that walks over all blocks. */
187 /* Attributes private to construction: */
188 unsigned is_matured:1; /**< If set, all in-nodes of the block are fixed. */
189 unsigned dynamic_ins:1; /**< if set in-array is an ARR_F on the heap. */
190 unsigned marked:1; /**< Can be set/unset to temporary mark a block. */
191 ir_node **graph_arr; /**< An array to store all parameters. */
192 /* Attributes holding analyses information */
193 ir_dom_info dom; /**< Datastructure that holds information about dominators. */
194 ir_dom_info pdom; /**< Datastructure that holds information about post-dominators. */
195 bitset_t *backedge; /**< Bitfield n set to true if pred n is backedge.*/
196 ir_entity *entity; /**< entitiy representing this block */
197 ir_node *phis; /**< The list of Phi nodes in this block. */
198 double execfreq; /**< block execution frequency */
201 /** Cond attributes. */
202 typedef struct cond_attr {
203 cond_jmp_predicate jmp_pred; /**< only for binary Conds: The jump predication. */
206 /** Const attributes. */
207 typedef struct const_attr {
208 ir_tarval *tarval; /**< the target value */
211 /** SymConst attributes. */
212 typedef struct symconst_attr {
213 symconst_symbol sym; // old tori
217 /** Sel attributes. */
218 typedef struct sel_attr {
219 ir_entity *entity; /**< entity to select */
222 /** Exception attributes. */
223 typedef struct except_attr {
224 unsigned pin_state : 2; /**< the pin state for operations with
225 variable pinned state. Contains a
227 unsigned throws_exception : 1; /**< if true a fragile op throws and
228 must produce X_except and X_regular
232 /** Call attributes. */
233 typedef struct call_attr {
234 except_attr exc; /**< the exception attribute. MUST be the first one. */
235 ir_type *type; /**< type of called procedure */
236 ir_entity **callee_arr; /**< result of callee analysis */
239 /** Builtin attributes. */
240 typedef struct builtin_attr {
241 except_attr exc; /**< the exception attribute. MUST be the first one. */
242 ir_builtin_kind kind; /**< kind of the called builtin procedure */
243 ir_type *type; /**< type of called builtin procedure */
246 /** Alloc attributes. */
247 typedef struct alloc_attr {
248 except_attr exc; /**< the exception attribute. MUST be the first one. */
249 ir_where_alloc where; /**< stack, heap or other managed part of memory */
250 ir_type *type; /**< Type of the allocated object. */
253 /** Free attributes. */
254 typedef struct free_attr {
255 ir_type *type; /**< Type of the allocated object. */
256 ir_where_alloc where; /**< stack, heap or other managed part of memory */
259 /** InstOf attributes. */
260 typedef struct io_attr {
261 except_attr exc; /**< the exception attribute. MUST be the first one. */
262 ir_type *type; /**< the type of which the object pointer must be */
265 /** Load attributes. */
266 typedef struct load_attr {
267 except_attr exc; /**< The exception attribute. MUST be the first one. */
268 __extension__ ir_volatility volatility:1; /**< The volatility of this Load operation. */
269 __extension__ ir_align unaligned:1; /**< The align attribute of this Load operation. */
270 ir_mode *mode; /**< The mode of this Load operation. */
273 /** Store attributes. */
274 typedef struct store_attr {
275 except_attr exc; /**< the exception attribute. MUST be the first one. */
276 __extension__ ir_volatility volatility:1; /**< The volatility of this Store operation. */
277 __extension__ ir_align unaligned:1; /**< The align attribute of this Store operation. */
280 typedef struct phi_attr {
281 ir_node *next; /**< Points to the next Phi in the Phi list of a block. */
283 bitset_t *backedge; /**< Raw Bitfield: bit n is set to true if pred n is backedge. */
284 int pos; /**< For Phi0. Used to remember the value defined by
285 this Phi node. Needed when the Phi is completed
286 to call get_r_internal_value() to find the
287 predecessors. If this attribute is set, the Phi
288 node takes the role of the obsolete Phi0 node,
289 therefore the name. */
293 /**< Cmp attribute. */
294 typedef struct cmp_attr {
295 ir_relation relation; /**< comparison condition. */
298 /**< Confirm attribute. */
299 typedef struct confirm_attr {
300 ir_relation relation; /**< relation between value and bound */
303 /** CopyB attribute. */
304 typedef struct copyb_attr {
305 except_attr exc; /**< The exception attribute. MUST be the first one. */
306 ir_type *type; /**< Type of the copied entity. */
309 /** Div attribute. */
310 typedef struct div_attr {
311 except_attr exc; /**< The exception attribute. MUST be the first one. */
312 ir_mode *resmode; /**< Result mode for the division. */
313 char no_remainder; /**< Set, if known that a division can be done without a remainder. */
316 /** Mod attribute. */
317 typedef struct mod_attr {
318 except_attr exc; /**< The exception attribute. MUST be the first one. */
319 ir_mode *resmode; /**< Result mode for the division. */
322 /** Inline Assembler support attribute. */
323 typedef struct asm_attr {
324 /* BEWARE: pin state MUST be the first attribute */
325 op_pin_state pin_state; /**< the pin state for operations that might generate a exception */
326 ident *text; /**< The inline assembler text. */
327 ir_asm_constraint *input_constraints; /**< Input constraints. */
328 ir_asm_constraint *output_constraints; /**< Output constraints. */
329 ident **clobbers; /**< List of clobbered registers. */
332 typedef struct proj_attr {
333 long proj; /**< position of tuple sub-value which is projected */
336 typedef struct switch_attr {
338 ir_switch_table *table;
341 /** Some IR-nodes just have one attribute, these are stored here,
342 some have more. Their name is 'irnodename_attr' */
343 typedef union ir_attr {
344 irg_attr irg; /**< For Blocks and Bad: its belonging irg */
345 bad_attr bad; /**< for Bads: irg reference */
346 anchor_attr anchor; /**< for Anchor: irg reference */
347 block_attr block; /**< For Block: Fields needed to construct it */
348 cmp_attr cmp; /**< For Cmp. */
349 cond_attr cond; /**< For Cond. */
350 const_attr con; /**< For Const: contains the value of the constant and a type */
351 symconst_attr symc; /**< For SymConst. */
352 sel_attr sel; /**< For Sel. */
353 call_attr call; /**< For Call. */
354 builtin_attr builtin; /**< For Builtin. */
355 alloc_attr alloc; /**< For Alloc. */
356 free_attr free; /**< For Free. */
357 io_attr instof; /**< For InstOf */
358 load_attr load; /**< For Load. */
359 store_attr store; /**< For Store. */
360 phi_attr phi; /**< For Phi. */
361 proj_attr proj; /**< For Proj. */
362 confirm_attr confirm; /**< For Confirm: compare operation and region. */
363 except_attr except; /**< For Phi node construction in case of exceptions */
364 copyb_attr copyb; /**< For CopyB operation */
365 div_attr div; /**< For Div operation */
366 mod_attr mod; /**< For Mod operation */
367 asm_attr assem; /**< For ASM operation. */
368 switch_attr switcha; /**< For Switch operation. */
372 * Edge info to put into an irn.
374 typedef struct irn_edge_kind_info_t {
375 struct list_head outs_head; /**< The list of all outs. */
376 unsigned edges_built : 1; /**< Set edges where built for this node. */
377 unsigned out_count : 31; /**< Number of outs in the list. */
380 typedef irn_edge_info_t irn_edges_info_t[EDGE_KIND_LAST];
385 typedef struct ir_def_use_edge {
386 ir_node *use; /** The use node of that edge. */
387 int pos; /** The position of this edge in use's input array. */
390 typedef struct ir_def_use_edges {
392 ir_def_use_edge edges[];
396 * The common structure of an irnode.
397 * If the node has some attributes, they are stored in the attr field.
400 /* ------- Basics of the representation ------- */
401 firm_kind kind; /**< Distinguishes this node from others. */
402 unsigned node_idx; /**< The node index of this node in its graph. */
403 ir_op *op; /**< The Opcode of this node. */
404 ir_mode *mode; /**< The Mode of this node. */
405 struct ir_node **in; /**< The array of predecessors / operands. */
406 ir_visited_t visited; /**< The visited counter for walks of the graph. */
407 void *link; /**< To attach additional information to the node, e.g.
408 used during optimization to link to nodes that
409 shall replace a node. */
410 long node_nr; /**< A globally unique node number for each node. */
411 /* ------- Fields for optimizations / analysis information ------- */
413 ir_def_use_edges *out; /**< array of def-use edges. */
414 unsigned n_outs; /**< number of def-use edges (temporarily used
415 during construction of datastructure ) */
417 struct dbg_info *dbi; /**< A pointer to information for debug support. */
418 /* ------- For analyses -------- */
419 ir_loop *loop; /**< the loop the node is in. Access routines in irloop.h */
420 struct ir_node **deps; /**< Additional dependencies induced by state. */
422 irn_edges_info_t edge_info; /**< Everlasting out edges. */
424 /* ------- Opcode depending fields -------- */
425 ir_attr attr; /**< The set of attributes of this node. Depends on opcode.
426 Must be last field of struct ir_node. */
429 #include "iredgeset.h"
432 * Edge info to put into an irg.
434 typedef struct irg_edge_info_t {
435 ir_edgeset_t edges; /**< A set containing all edges of the current graph. */
436 struct list_head free_edges; /**< list of all free edges. */
437 struct obstack edges_obst; /**< Obstack, where edges are allocated on. */
438 unsigned allocated : 1; /**< Set if edges are allocated on the obstack. */
439 unsigned activated : 1; /**< Set if edges are activated for the graph. */
442 typedef irg_edge_info_t irg_edges_info_t[EDGE_KIND_LAST];
445 * Index constants for nodes that can be accessed through the graph anchor node.
447 typedef enum irg_anchors {
449 anchor_end_block = anchor_first, /**< block the end node will belong to,
450 same as Anchors block */
451 anchor_start_block, /**< block the start node will belong to */
452 anchor_end, /**< end node of this ir_graph */
453 anchor_start, /**< start node of this ir_graph */
454 anchor_initial_exec, /**< methods initial control flow */
455 anchor_frame, /**< methods frame */
456 anchor_initial_mem, /**< initial memory of this graph */
457 anchor_args, /**< methods arguments */
458 anchor_no_mem, /**< NoMem node of this ir_graph, the one and only in this graph */
459 anchor_last = anchor_no_mem
461 ENUM_COUNTABLE(irg_anchors)
463 /** A callgraph entry for callees. */
464 typedef struct cg_callee_entry {
465 ir_graph *irg; /**< The called irg. */
466 ir_node **call_list; /**< The list of all calls to the irg. */
467 size_t max_depth; /**< Maximum depth of all Call nodes to irg. */
470 typedef struct ir_vrp_info {
471 struct ir_nodemap infos;
476 * An ir_graph holds all information for a procedure.
479 firm_kind kind; /**< Always set to k_ir_graph. */
480 /* -- Basics of the representation -- */
481 unsigned last_node_idx; /**< The last IR node index for this graph. */
482 ir_entity *ent; /**< The entity of this procedure, i.e.,
483 the type of the procedure and the
484 class it belongs to. */
485 ir_type *frame_type; /**< A class type representing the stack frame.
486 Can include "inner" methods. */
487 ir_node *anchor; /**< Pointer to the anchor node of this graph. */
488 struct obstack obst; /**< The obstack where all of the ir_nodes live. */
489 ir_node *current_block; /**< Current block for new_*()ly created ir_nodes. */
491 /* -- Fields indicating different states of irgraph -- */
492 ir_graph_properties_t properties;
493 ir_graph_constraints_t constraints;
494 op_pin_state irg_pinned_state; /**< Flag for status of nodes. */
495 ir_typeinfo_state typeinfo_state; /**< Validity of type information. */
496 irg_callee_info_state callee_info_state; /**< Validity of callee information. */
497 unsigned mem_disambig_opt; /**< Options for the memory disambiguator. */
498 unsigned fp_model; /**< floating point model of the graph. */
500 /* -- Fields for construction -- */
501 int n_loc; /**< Number of local variables in this
502 procedure including procedure parameters. */
503 void **loc_descriptions; /**< Storage for local variable descriptions. */
505 /* -- Fields for optimizations / analysis information -- */
506 pset *value_table; /**< Hash table for global value numbering (cse)
507 for optimizing use in iropt.c */
508 struct obstack out_obst; /**< Space for the Def-Use arrays. */
509 bool out_obst_allocated;
510 ir_vrp_info vrp; /**< vrp info */
512 ir_loop *loop; /**< The outermost loop for this graph. */
513 ir_dom_front_info_t domfront; /**< dominance frontier analysis data */
514 void *link; /**< A void* field to link any information to
517 ir_graph **callers; /**< For callgraph analysis: list of caller graphs. */
518 unsigned *caller_isbe; /**< For callgraph analysis: raw bitset if backedge info calculated. */
519 cg_callee_entry **callees; /**< For callgraph analysis: list of callee calls */
520 unsigned *callee_isbe; /**< For callgraph analysis: raw bitset if backedge info calculated. */
521 ir_loop *l; /**< For callgraph analysis. */
522 size_t callgraph_loop_depth; /**< For callgraph analysis */
523 size_t callgraph_recursion_depth; /**< For callgraph analysis */
524 double method_execution_frequency; /**< For callgraph analysis */
527 /* -- Fields for Walking the graph -- */
528 ir_visited_t visited; /**< this flag is an identifier for
529 ir walk. it will be incremented
530 every time someone walks through
532 ir_visited_t block_visited; /**< same as visited, for a complete block */
534 ir_visited_t self_visited; /**< visited flag of the irg */
536 irg_edges_info_t edge_info; /**< edge info for automatic outs */
537 ir_node **idx_irn_map; /**< Array mapping node indexes to nodes. */
539 size_t index; /**< a unique number for each graph */
540 /** extra info which should survive accross multiple passes */
541 void *be_data; /**< backend can put in private data here */
543 unsigned dump_nr; /**< number of graph dumps */
545 long graph_nr; /**< a unique graph number for each
546 graph to make output readable. */
550 ir_resources_t reserved_resources; /**< Bitset for tracking used local resources. */
555 * Data structure that holds central information about a program
557 * One irp is created by libFirm on construction, so irp should never be NULL.
559 * - main_irg: The ir graph that is the entry point to the program.
560 * (Anything not reachable from here may be optimized away
561 * if this irp represents a whole program.
562 * - irg: List of all ir graphs in the program or module.
563 * - type: A list containing all types known to the translated program.
564 * Some types can have several entries in this list (as a result of
565 * using exchange_types()).
566 * - glob_type: The unique global type that is owner of all global entities
570 firm_kind kind; /**< must be k_ir_prog */
571 ident *name; /**< A file name or the like. */
572 ir_graph *main_irg; /**< The entry point to the compiled program
573 or NULL if no point exists. */
574 ir_graph **graphs; /**< A list of all graphs in the ir. */
575 ir_graph *const_code_irg; /**< This ir graph gives the proper environment
576 to allocate nodes the represent values
577 of constant entities. It is not meant as
579 ir_entity *unknown_entity; /**< unique 'unknown'-entity */
580 ir_type *segment_types[IR_SEGMENT_LAST+1];
581 ir_type **types; /**< A list of all types in the ir. */
582 ir_type *none_type; /**< unique 'none'-type */
583 ir_type *code_type; /**< unique 'code'-type */
584 ir_type *unknown_type; /**< unique 'unknown'-type */
585 ir_type *byte_type; /**< type for a 'byte' */
586 ident **global_asms; /**< An array of global ASM insertions. */
588 /* -- states of and access to generated information -- */
589 ir_node **ip_outedges; /**< A huge Array that contains all out edges
590 in interprocedural view. */
592 irg_callee_info_state callee_info_state; /**< Validity of callee information.
593 Contains the lowest value or all irgs. */
594 ir_typeinfo_state typeinfo_state; /**< Validity of type information. */
595 inh_transitive_closure_state inh_trans_closure_state; /**< State of transitive closure
596 of inheritance relations. */
598 irp_callgraph_state callgraph_state; /**< The state of the callgraph. */
599 ir_loop *outermost_cg_loop; /**< For callgraph analysis: entry point
600 to looptree over callgraph. */
601 size_t max_callgraph_loop_depth; /**< needed in callgraph. */
602 size_t max_callgraph_recursion_depth; /**< needed in callgraph. */
603 double max_method_execution_frequency; /**< needed in callgraph. */
604 loop_nesting_depth_state lnd_state; /**< The state of loop nesting depth information. */
605 ir_entity_usage_computed_state globals_entity_usage_state;
607 ir_label_t last_label_nr; /**< The highest label number for generating unique labels. */
608 size_t max_irg_idx; /**< highest unused irg index */
609 long max_node_nr; /**< to generate unique numbers for nodes. */
610 unsigned dump_nr; /**< number of program info dumps */
612 irp_resources_t reserved_resources; /**< Bitset for tracking used global resources. */