1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Authors: Martin Trapp, Christian Schaefer,
7 ** ircons.h ir node construction
12 *** Ideas for imrovement:
14 Handle construction of exceptions more comfortable:
15 Add new constructors that pass the exception region (or better the
16 Phi for the memories, the ex. region can be found from there) as parameter,
17 constructor then adds all Proj nodes and returns the pointer
18 to the Proj node that selects the result of the arithmetic operation.
20 Maybe hide the exception region in a global variable, especially if
21 it is always unambiguous.
24 /****h* libfirm/ircons
30 * This file documents all datatypes and constructors needed to
31 * build a FIRM representation of a pocedure. The constructors are
32 * also implemented in this file.
34 * The documentation also gives a short manual how to use the library.
36 * For extensive documentation of FIRM see UKA Techreport 1999-14.
44 * This struct contains all information about a procedure.
45 * It's allocated directly to memory.
47 * The fields of ir_graph:
49 * *ent The entity describing this procedure.
51 * The beginning and end of a graph:
53 * *start_block This ir_node is the block that contains the unique
54 * start node of the procedure. With it it contains
55 * the Proj's on starts results.
56 * Further all Const nodes are placed in the start block.
57 * *start This ir_node is the unique start node of the procedure.
59 * *end_block This ir_node is the block that contains the unique
60 * end node of the procedure. This block contains no
62 * *end This ir_node is the unique end node of the procedure.
64 * The following nodes are Projs from the start node, held in ir_graph for
67 * *frame The ir_node producing the pointer to the stack frame of
68 * the procedure as output. This is the Proj node on the
69 * third output of the start node. This output of the start
70 * node is tagged as pns_frame_base. In FIRM most lokal
71 * variables are modeled as data flow edges. Static
72 * allocated arrays can not be represented as dataflow
73 * edges. Therefore FIRM has to represent them in the stack
76 * *globals This models a pointer to a space in the memory where
77 * _all_ global things are held. Select from this pointer
78 * with a Sel node the pointer to a global variable /
79 * procedure / compiler known function... .
81 * *args The ir_node that produces the arguments of the method as
82 * it's result. This is a Proj node on the fourth output of
83 * the start node. This output is tagged as pns_args.
85 * *bad The bad node is an auxiliary node. It is needed only once,
86 * so there is this globally reachable node.
88 * Datastructures that are private to a graph:
90 * *obst An obstack that contains all nodes.
92 * *current_block A pointer to the current block. Any node created with
93 * one of the node constructors (new_<opcode>) are assigned
94 * to this block. It can be set with switch_block(block).
95 * Only needed for ir construction.
97 * params/n_loc An int giving the number of local variables in this
98 * procedure. This is neede for ir construction. Name will
101 * *value_table This hash table (pset) is used for global value numbering
102 * for optimizing use in iropt.c.
104 * *Phi_in_stack; a stack needed for automatic Phi construction, needed only
105 * during ir construction.
107 * visited A int used as flag to traverse the ir_graph.
109 * block_visited A int used as a flag to traverse block nodes in the graph.
111 * Three kinds of nodes
112 * --------------------
114 * There are three kinds of nodes known to the ir: entities,
115 * types, and ir_nodes
117 * + ir_nodes are the actual nodes of the FIRM intermediate representation.
118 * They represent operations on the data of the program and control flow
121 * + entity ==> implemented in entity.h
122 * Refers to a single entity of the compiled program, e.g. a field of a
123 * class or a method. If a method or variable can not be assigned to
124 * a method or class or the like, it is a global object.
126 * + types ==> implemented in type.h
127 * With types type information is represented. There are several type
130 * Implementation of the FIRM operations: ir_node
131 * ----------------------------------------------
133 * Ir_nodes represent operations on the data of the program and control flow
134 * operations. Examples of ir_nodes: Add, Jmp, Cmp
136 * FIRM is a dataflow graph. A dataflow graph is a directed graph,
137 * so that every node has incoming and outgoing edges. A node is
138 * executable if every input at it's incoming edges is available.
139 * Execution of the dataflow graph is started at the Start node which
140 * has no incoming edges and ends when the End node executes, even if
141 * there are still executable or not executed nodes. (Is this true,
142 * or must all executable nodes be executed?) (There are exceptions
143 * to the dataflow paradigma that all inputs have to be available
144 * before a node can execute: Phi, Block. See UKA Techreport
147 * The implementation of FIRM differs from the view as a dataflow
148 * graph. To allow fast traversion of the graph edges are
149 * implemented as C-pointers. Inputs to nodes are not ambiguous, the
150 * results can be used by several other nodes. Each input can be
151 * implemented as a single pointer to a predecessor node, outputs
152 * need to be lists of pointers to successors. Therefore a node
153 * contains pointers to it's predecessor so that the implementation is a
154 * dataflow graph with reversed edges. It has to be traversed bottom
157 * All nodes of the ir have the same basic structure. They are
158 * distinguished by a field containing the opcode.
160 * The fields of an ir_node:
162 * kind A firm_kind tag containing k_ir_node. This is useful for
163 * dynamically checking the type of a node.
165 * *op This ir_op gives the opcode as a tag and a string
166 * and the number of attributes of an ir_node. There is
167 * one statically allocated struct ir_op for each opcode.
169 * *mode The ir_mode of the operation represented by this firm
170 * node. The mode of the operation is the mode of it's
171 * result. A Firm mode is a datatype as known to the target,
172 * not a type of the source language.
174 * visit A flag for traversing the ir.
176 * **in An array with pointers to the node's predecessors.
178 * *link A pointer to an ir_node. With this pointer all Phi nodes
179 * are attached to a Block, i.e., a Block points to it's
180 * first Phi node, this node points to the second Phi node
181 * in the Block and so fourth. Used in mature_block
182 * to find all Phi nodes to be matured. It's also used to
183 * annotate a node with a better, optimized version of it.
185 * attr An attr struct containing the attributes of the nodes. The
186 * attributes depend on the opcode of the node. The number
187 * of these attributes is given in op.
191 * Not yet documented. See irop.h.
195 * Not yet documented. See irmode.h.
197 * GLOBAL VARIABLES -- now also fields of ir_graph.
200 * current_ir_graph Points to the current ir_graph. All constructors for
201 * nodes add nodes to this graph.
203 * ir_visited An int used as flag to traverse the ir_graph.
205 * block_visited An int used as a flag to traverse block nodes in the
208 * Others not yet documented.
212 * CONSTRUCTOR FOR IR_GRAPH --> see irgraph.h
213 * ========================
216 * PROCEDURE TO CONSTRUCT AN IR GRAPH --> see also Firm tutorial
217 * ==================================
219 * This library supplies several interfaces to construct a FIRM graph for
221 * * A "comfortable" interface generating SSA automatically. Automatically
222 * computed predecessors of nodes need not be specified in the constructors.
223 * (new_<Node> constructurs and a set of additional routines.)
224 * * A less comfortable interface where all predecessors except the block
225 * an operation belongs to need to be specified. SSA must be constructed
226 * by hand. (new_<Node> constructors and switch_block()). This interface
227 * is called "block oriented". It automatically calles the local optimizations
229 * * An even less comfortable interface where the block needs to be specified
230 * explicitly. This is called the "raw" interface. (new_r_<Node>
231 * constructors). These nodes are not optimized.
233 * To use the functionality of the comfortable interface correctly the Front
234 * End needs to follow certain protocols. This is explained in the following.
235 * To build a correct IR with the other interfaces study the semantics of
236 * the firm node (See tech-reprot UKA 1999-14). For the construction of
237 * types and entities see the documentation in those modules.
239 * First the Frontend needs to decide which variables and values used in
240 * a procedure can be represented by dataflow edges. These are variables
241 * that need not be saved to memory as they cause no side effects visible
242 * out of the procedure. Often these are all compiler generated
243 * variables and simple local variables of the procedure as integers,
244 * reals and pointers. The frontend has to count and number these variables.
246 * First an ir_graph needs to be constructed with new_ir_graph. The
247 * constructor gets the number of local variables. The graph is hold in the
248 * global variable irg.
250 * Now the construction of the procedure can start. Several basic blocks can
251 * be constructed in parallel, but the code within each block needs to
252 * be constructed (almost) in program order.
254 * A global variable holds the current basic block. All (non block) nodes
255 * generated are added to this block. The current block can be set with
256 * switch_block(block). If several blocks are constructed in parallel block
257 * switches need to be performed constantly.
259 * To generate a Block node (with the comfortable interface) it's predecessor
260 * control flow nodes need not be known. In case of cyclic control flow these
261 * can not be known when the block is constructed. With add_in_edge(block,
262 * cfnode) predecessors can be added to the block. If all predecessors are
263 * added to the block mature_block(b) needs to be called. Calling mature_block
264 * early improves the efficiency of the Phi node construction algorithm.
265 * But if several blocks are constructed at once, mature_block must only
266 * be called after performing all set_values and set_stores in the block!
267 * (See documentation of new_immBlock constructor.)
269 * The constructors of arithmetic nodes require that their predecessors
270 * are mentioned. Sometimes these are available in the Frontend as the
271 * predecessors have just been generated by the frontend. If they are local
272 * values the predecessors can be obtained from the library with a call to
273 * get_value(local_val_nr). (local_val_nr needs to be administered by
274 * the Frontend.) A call to get_value triggers the generation of Phi nodes.
275 * If an arithmetic operation produces a local value this value needs to be
276 * passed to the library by set_value(node, local_val_nr).
277 * In straight line code these two operations just remember and return the
278 * pointer to nodes producing the value. If the value passes block boundaries
279 * Phi nodes can be inserted.
280 * Similar routines exist to manage the Memory operands: set_store and
283 * Several nodes produce more than one result. An example is the Div node.
284 * Such nodes return tuples of values. From these individual values can be
285 * extracted by proj nodes.
287 * The following example illustrates the construction of a simple basic block
288 * with two predecessors stored in variables cf_pred1 and cf_pred2, containing
291 * and finally jumping to an other block. The variable a got the local_val_nr
292 * 42 by the frontend.
294 * ir_node *this_block, *cf_pred1, *cf_pred2, *a_val, *mem, *div, *res, *cf_op;
296 * this_block = new_immBlock();
297 * add_in_edge(this_block, cf_pred1);
298 * add_in_edge(this_block, cf_pred2);
299 * mature_block(this_block);
300 * a_val = get_value(42, mode_I);
302 * div = new_Div(mem, a_val, a_val);
303 * mem = new_Proj(div, mode_M, 0); * for the numbers for Proj see docu *
304 * res = new_Proj(div, mode_I, 2);
306 * set_value(res, 42);
309 * For further information look at the documentation of the nodes and
310 * constructors and at the paragraph COPING WITH DATA OBJECTS at the
311 * end of this documentation.
313 * The comfortable interface contains the following routines further explained
316 * ir_node *new_immBlock (void);
317 * ir_node *new_Start (void);
318 * ir_node *new_End (void);
319 * ir_node *new_Jmp (void);
320 * ir_node *new_Cond (ir_node *c);
321 * ir_node *new_Return (ir_node *store, int arity, ir_node **in);
322 * ir_node *new_Raise (ir_node *store, ir_node *obj);
323 * ir_node *new_Const (ir_mode *mode, tarval *con);
324 * ir_node *new_SymConst (type_or_id *value, symconst_kind kind);
325 * ir_node *new_simpleSel (ir_node *store, ir_node *objptr, entity *ent);
326 * ir_node *new_Sel (ir_node *store, ir_node *objptr, int arity,
327 * ir_node **in, entity *ent);
328 * ir_node *new_Call (ir_node *store, ir_node *callee, int arity,
329 * ir_node **in, type_method *type);
330 * ir_node *new_Add (ir_node *op1, ir_node *op2, ir_mode *mode);
331 * ir_node *new_Sub (ir_node *op1, ir_node *op2, ir_mode *mode);
332 * ir_node *new_Minus (ir_node *op, ir_mode *mode);
333 * ir_node *new_Mul (ir_node *op1, ir_node *op2, ir_mode *mode);
334 * ir_node *new_Quot (ir_node *memop, ir_node *op1, ir_node *op2);
335 * ir_node *new_DivMod (ir_node *memop, ir_node *op1, ir_node *op2);
336 * ir_node *new_Div (ir_node *memop, ir_node *op1, ir_node *op2);
337 * ir_node *new_Mod (ir_node *memop, ir_node *op1, ir_node *op2);
338 * ir_node *new_Abs (ir_node *op, ir_mode *mode);
339 * ir_node *new_And (ir_node *op1, ir_node *op2, ir_mode *mode);
340 * ir_node *new_Or (ir_node *op1, ir_node *op2, ir_mode *mode);
341 * ir_node *new_Eor (ir_node *op1, ir_node *op2, ir_mode *mode);
342 * ir_node *new_Not (ir_node *op, ir_mode *mode);
343 * ir_node *new_Shl (ir_node *op, ir_node *k, ir_mode *mode);
344 * ir_node *new_Shr (ir_node *op, ir_node *k, ir_mode *mode);
345 * ir_node *new_Shrs (ir_node *op, ir_node *k, ir_mode *mode);
346 * ir_node *new_Rot (ir_node *op, ir_node *k, ir_mode *mode);
347 * ir_node *new_Cmp (ir_node *op1, ir_node *op2);
348 * ir_node *new_Conv (ir_node *op, ir_mode *mode);
349 * ir_node *new_Load (ir_node *store, ir_node *addr);
350 * ir_node *new_Store (ir_node *store, ir_node *addr, ir_node *val);
351 * ir_node *new_Alloc (ir_node *store, ir_node *size, type *alloc_type,
352 * where_alloc where);
353 * ir_node *new_Free (ir_node *store, ir_node *ptr, ir_node *size,
355 * ir_node *new_Proj (ir_node *arg, ir_mode *mode, long proj);
357 * void add_in_edge (ir_node *block, ir_node *jmp);
358 * void mature_block (ir_node *block);
359 * void switch_block (ir_node *target);
360 * ir_node *get_value (int pos, ir_mode *mode);
361 * void set_value (int pos, ir_node *value);
362 * ir_node *get_store (void);
363 * void set_store (ir_node *store);
366 * IR_NODES AND CONSTRUCTORS FOR IR_NODES
367 * =======================================
369 * All ir_nodes are defined by a common data structure. They are distinguished
370 * by their opcode and differ in the number of their attributes.
372 * The constructor for the block node sets current_block to itself.
373 * Const nodes are always added to the start block.
374 * All other constructors add the created node to the current_block.
375 * swich_block(block) allows to set the current block to block.
377 * Watch for my inconsistent use of input and predecessor (dataflow view)
378 * and `the node points to' (implementation view).
380 * The following description of the nodes lists four properties them if these
382 * - the parameters to the constructor
383 * - the inputs of the Firm node
384 * - the outputs of the Firm node
385 * - attributes to the node
390 * ir_node *new_immBlock (void)
391 * ----------------------------
393 * Creates a new block. Sets current_block to itself. When a new block is
394 * created it cannot be known how many predecessors this block will have in the
395 * control flow graph. Therefore the list of inputs can not be fixed at
396 * creation. Predecessors can be added with add_in_edge (block, control flow
397 * operation). With every added predecessor the number of inputs to Phi nodes
400 * The block can be completed by mature_block(block) if all predecessors are
401 * known. If several blocks are built at once, mature_block can only be called
402 * after set_value has been called for all values that are life at the end
403 * of the block. This is necessary so that Phi nodes created by mature_block
404 * get the right predecessors in case of cyclic dependencies. If all set_values
405 * of this block are called after maturing it and before calling get_value
406 * in some block that is control flow dependent on this block, the construction
409 * Example for faulty ir construction: (draw the graph on a paper and you'll
412 * block_before_loop = new_block();
414 * mature_block(block_before_loop);
415 * before2header = new_Jmp;
417 * loop_header = new_block ();
418 * header2body - new_Jmp();
420 * loop_body = new_block ();
421 * body2header = new_Jmp();
423 * add_in_edge(loop_header, before2header);
424 * add_in_edge(loop_header, body2header);
425 * add_in_edge(loop_body, header2body);
427 * mature_block(loop_header);
428 * mature_block(loop_body);
430 * get_value(loop_body, x); // gets the Phi in loop_header
431 * set_value(loop_header, x); // sets the value the above get_value should
432 * // have returned!!!
434 * Mature_block also fixes the number of inputs to the Phi nodes. Mature_block
435 * should be called as early as possible, as afterwards the generation of Phi
436 * nodes is more efficient.
439 * There is an input for each control flow predecessor of the block.
440 * The input points to an instruction producing an output of type X.
441 * Possible predecessors: Start, Jmp, Cond, Raise or Return or any node
442 * possibly causing an exception. (Often the real predecessors are Projs.)
444 * Mode BB (R), all nodes belonging to this block should consume this output.
445 * As they are strict (except Block and Phi node) it is a necessary condition
446 * that the block node executed before any other node in this block executes.
448 * block.matured Indicates whether the block is mature.
450 * This attribute contains all local values valid in this
451 * block. This is needed to build the Phi nodes and removed
452 * if the graph is complete. This field is used by the
453 * internal construction algorithm and should not be accessed
457 * ir_node *new_Block (int arity, ir_node **in)
458 * --------------------------------------------
460 * Creates a new Block with the given list of predecessors. This block
464 * CONTROL FLOW OPERATIONS
465 * -----------------------
467 * In each block there must be exactly one of the control flow
468 * operations Start, End, Jmp, Cond, Return or Raise. The output of a
469 * control flow operation points to the block to be executed next.
471 * ir_node *new_Start (void)
472 * -------------------------
474 * Creates a start node. Not actually needed public. There is only one such
475 * node in each procedure which is automatically created by new_ir_graph.
478 * No inputs except the block it belogns to.
480 * A tuple of 4 (5, 6) distinct values. These are labeled by the following
481 * projection numbers (pns_number):
483 * mode X, points to the first block to be executed.
485 * mode M, the global store
486 * * pns_frame_base mode P, a pointer to the base of the procedures
488 * * pns_globals mode P, a pointer to the part of the memory containing
489 * _all_ global things.
490 * * pns_args mode T, a tuple containing all arguments of the procedure.
493 * ir_node *new_End (void)
494 * -----------------------
496 * Creates an end node. Not actually needed public. There is only one such
497 * node in each procedure which is automatically created by new_ir_graph.
500 * No inputs except the block it belongs to.
504 * ir_node *new_Jmp (void)
505 * -----------------------
507 * Creates a Jmp node.
510 * The block the node belongs to
512 * Control flow to the next block.
514 * ir_node *new_Cond (ir_node *c)
515 * ------------------------------
517 * Creates a Cond node. There are two versions of this node.
523 * A tuple of two control flows. The first is taken if the input is
524 * false, the second if it is true.
528 * A value of mode I_u. (i)
530 * A tuple of n control flows. If the Cond's input is i, control
531 * flow will procede along output i. If the input is >= n control
532 * flow proceeds along output n.
534 * ir_node *new_Return (in_node *store, int arity, ir_node **in)
535 * -------------------------------------------------------------
537 * The return node has as inputs the results of the procedure. It
538 * passes the control flow to the end_block.
544 * Control flow to the end block.
546 * ir_node *new_Raise (ir_node *store, ir_node *obj)
547 * -------------------------------------------------
549 * Raises an exception. Unconditional change of control flow. Writes
550 * an explicit Except variable to memory to pass it to the exception
551 * handler. See TechReport 1999-14, chapter Exceptions.
555 * A pointer to the Except variable.
557 * A tuple of control flow and the changed memory state. The control flow
558 * points to the exception handler if it is definied in this procedure,
559 * else it points to the end_block.
565 * ir_node *new_Const (ir_mode *mode, tarval *con)
566 * -----------------------------------------------
568 * Creates a constant in the constant table and adds a Const node
569 * returning this value to the start block.
572 * *mode The mode of the constant.
573 * *con Points to an entry in the constant table.
574 * This pointer is added to the attributes of
575 * the node (self->attr.con)
577 * No inputs except the block it belogns to.
579 * The constant value.
581 * attr.con A tarval* pointer to the proper entry in the constant
584 * ir_node *new_SymConst (type *type, symconst_kind kind)
585 * ------------------------------------------------------------
587 * There are three kinds of symbolic constants:
588 * type_tag The symbolic constant represents a type tag.
589 * size The symbolic constant represents the size of a class.
590 * link_info Information for the linker, e.g. the name of a global
594 * kind The kind of the symbolic constant: type_tag, size or link_info.
595 * *type_or_id Points to the type the tag stands for or to the type
596 * whose size is represented by the constant or to an ident
597 * representing the linkage info.
600 * No inputs except the block it belogns to.
602 * An unsigned integer (I_u) or a pointer (P).
605 * attr.i.num The symconst_kind, i.e. one of
609 * If the attr.i.num is type_tag or size, the node contains an attribute
610 * attr.i.*type, a pointer to a type_class. The mode of the node is mode_i.
611 * if it is linkage_ptr_info it contains
612 * attr.i.*ptrinfo, an ident holding information for the linker. The mode
613 * of the node is mode_p.
618 * ir_node *new_simpleSel (ir_node *store, ir_node *frame, entity *sel)
619 * --------------------------------------------------------------------
622 * Selects an entity from a compound type. This entity can be a field or
626 * *store The memory in which the object the entity should be selected
628 * *frame The pointer to the object.
629 * *sel The entity to select.
632 * The memory containing the object.
633 * A pointer to the object.
634 * An unsigned integer.
636 * A pointer to the selected entity.
638 * attr.sel Pointer to the entity
641 * ir_node *new_Sel (ir_node *store, ir_node *frame, int arity, ir_node **in,
642 * --------------------------------------------------------------------------
646 * Selects a field from an array type. The entity has as owner the array, as
647 * type the arrays element type. The indexes to access an array element are
651 * *store The memory in which the object the entity should be selected from
653 * *frame The pointer to the object.
654 * *arity number of array indexes.
655 * *in array with index inputs to the node.
656 * *sel The entity to select.
659 * The memory containing the object.
660 * A pointer to the object.
661 * As much unsigned integer as there are array expressions.
663 * A pointer to the selected entity.
665 * attr.sel Pointer to the entity
667 * The constructors new_Sel and new_simpleSel generate the same ir nodes.
668 * simpleSel just sets the arity of the index inputs to zero.
671 * ARITHMETIC OPERATIONS
672 * ---------------------
674 * ir_node *new_Call (ir_node *store, ir_node *callee, int arity, ir_node **in,
675 * ----------------------------------------------------------------------------
679 * Creates a procedure call.
682 * *store The actual store.
683 * *callee A pointer to the called procedure.
684 * arity The number of procedure parameters.
685 * **in An array with the pointers to the parameters.
686 * The constructor copies this array.
687 * *type Type information of the procedure called.
690 * The store, the callee and the parameters.
692 * A tuple containing the eventually changed store and the procedure
695 * attr.call Contains the type information for the procedure.
697 * ir_node *new_Add (ir_node *op1, ir_node *op2, ir_mode *mode)
698 * ------------------------------------------------------------
702 * ir_node *new_Sub (ir_node *op1, ir_node *op2, ir_mode *mode)
703 * ------------------------------------------------------------
707 * ir_node *new_Minus (ir_node *op, ir_mode *mode)
708 * -----------------------------------------------
710 * Unary Minus operations on floating point values.
712 * ir_node *new_Mul (ir_node *op1, ir_node *op2, ir_mode *mode)
713 * ------------------------------------------------------------
717 * ir_node *new_Quot (ir_node *memop, ir_node *op1, ir_node *op2)
718 * --------------------------------------------------------------
720 * Quot performs exact division of floating point numbers. It's mode
721 * is Tuple, the mode of the result must be annotated to the Proj
722 * that extracts the result of the arithmetic operations.
725 * The store needed to model exceptions and the two operands.
727 * A tuple contaning a memory and a execution for modeling exceptions
728 * and the result of the arithmetic operation.
730 * ir_node *new_DivMod (ir_node *memop, ir_node *op1, ir_node *op2)
731 * ----------------------------------------------------------------
733 * Performs Div and Mod on interger values.
736 * A tuple contaning a memory and a execution for modeling exceptions
737 * and the two result of the arithmetic operations.
739 * ir_node *new_Div (ir_node *memop, ir_node *op1, ir_node *op2)
740 * -------------------------------------------------------------
744 * ir_node *new_Mod (ir_node *memop, ir_node *op1, ir_node *op2)
745 * -------------------------------------------------------------
749 * ir_node *new_Abs (ir_node *op, ir_mode *mode)
750 * ---------------------------------------------
754 * ir_node *new_And (ir_node *op1, ir_node *op2, ir_mode *mode)
755 * ------------------------------------------------------------
759 * ir_node *new_Or (ir_node *op1, ir_node *op2, ir_mode *mode)
760 * -----------------------------------------------------------
764 * ir_node *new_Eor (ir_node *op1, ir_node *op2, ir_mode *mode)
765 * ------------------------------------------------------------
769 * ir_node *new_Not (ir_node *op, ir_mode *mode)
770 * ---------------------------------------------
772 * This node constructs a constant where all bits are set to one
773 * and a Eor of this constant and the operator. This simulates a
776 * ir_node *new_Shl (ir_node *op, ir_node *k, ir_mode *mode)
777 * ---------------------------------------------------------
781 * ir_node *new_Shr (ir_node *op, ir_node *k, ir_mode *mode)
782 * ---------------------------------------------------------
784 * Logic shift right, i.e., zero extended.
787 * ir_node *new_Shrs (ir_node *op, ir_node *k, ir_mode *mode)
788 * ----------------------------------------------------------
790 * Arithmetic shift right, i.e., sign extended.
792 * ir_node *new_Rot (ir_node *op, ir_node *k, ir_mode *mode)
793 * ---------------------------------------------------------
795 * Rotates the operand to the (right??) by k bits.
797 * ir_node *new_Conv (ir_node *op, ir_mode *mode)
798 * ---------------------------------------------
800 * Mode conversion. For allowed conversions see UKA Tech Report
803 * ir_node *new_Cmp (ir_node *op1, ir_node *op2)
804 * ---------------------------------------------
807 * The two values to be compared.
809 * A 16-tuple containing the results of the 16 different comparisons.
810 * The following is a list giving the comparisons and a projection
811 * number (pnc_number) to use in Proj nodes to extract the proper result.
817 * Ge greater of equal
819 * Leg less, equal or greater = ordered
821 * Ue unordered or equal
822 * Ul unordered or less
823 * Ule unordered, less or equal
824 * Ug unordered or greater
825 * Uge unordered, greater or equal
826 * Ne unordered, less or greater = not equal
834 * In general, Phi nodes are automaitcally inserted. In some cases, if
835 * all predecessors of a block are known, an explicit Phi node constructor
836 * is needed. E.g., to construct a FIRM graph for a statement as
837 * a = (b==c) ? 2 : 5;
839 * ir_node *new_Phi (int arity, ir_node **in, ir_mode *mode)
840 * ---------------------------------------------------------
842 * Creates a Phi node. The in's order has to correspond to the order
843 * of in's of current_block. This is not checked by the library!
846 * arity number of predecessors
847 * **in array with predecessors
848 * *mode The mode of it's inputs and output.
850 * A Phi node has as many inputs as the block it belongs to.
851 * Each input points to a definition of the same value on a
852 * different path in the control flow.
854 * The definition valid in this block.
857 * OPERATIONS TO MANAGE MEMORY EXPLICITLY
858 * --------------------------------------
860 * ir_node *new_Load (ir_node *store, ir_node *addr)
861 * ----------------------------------------------------------------
863 * The Load operation reads a value from memory.
866 * *store The current memory.
867 * *addr A pointer to the variable to be read in this memory.
870 * The memory and a pointer to a variable in this memory.
872 * A tuple of the memory, a control flow to be taken in case of
873 * an exception and the loaded value.
875 * ir_node *new_Store (ir_node *store, ir_node *addr, ir_node *val)
876 * ----------------------------------------------------------------
878 * The Store operation writes a value to a variable in memory.
881 * The memory, a pointer to a variable in this memory and the value
882 * to write to this variable.
884 * A tuple of the changed memory and a control flow to be taken in
885 * case of an exception.
887 * ir_node *new_Alloc (ir_node *store, ir_node *size, type *alloc_type,
888 * --------------------------------------------------------------------
892 * The Alloc node allocates a new variable. It can be specified whether the
893 * variable should be allocated to the stack or to the heap.
896 * *store The memory which shall contain the new variable.
897 * ** *size The number of bytes to allocate. Old. **
898 * *size We decided that the size easily can be derived from the type.
899 * This field is for allocating arrays, i.e., it gives the multiple
900 * of the size of alloc_type to allocate memory for.
901 * *alloc_type The type of the allocated variable.
902 * where Where to allocate the variable, either heap_alloc or stack_alloc.
905 * A memory and an unsigned integer.
907 * A tuple of the changed memory, a control flow to be taken in
908 * case of an exception and the pointer to the new variable.
910 * a.where Indicates where the variable is allocated.
911 * a.*type A pointer to the class the allocated data object
914 * ir_node *new_Free (ir_node *store, ir_node *ptr, type *free_type)
915 * ------------------------------------------------------------------
917 * The Free node frees memory of the given variable.
920 * *store The memory which shall contain the new variable.
921 * *ptr The pointer to the object to free.
922 * *size The number of objects of type free_type to free in a sequence.
923 * *free_type The type of the freed variable.
926 * A memory, a pointer and an unsigned integer.
928 * The changed memory.
930 * f.*type A pointer to the type information of the freed data object.
934 * ir_node *new_Sync (int arity, ir_node **in)
935 * -------------------------------------------
937 * The Sync operation unifies several partial memory blocks. These blocks
938 * have to be pairwise disjunct or the values in common locations have to
939 * be identical. This operation allows to specify all operations that eventually
940 * need several partial memory blocks as input with a single entrance by
941 * unifying the memories with a preceding Sync operation.
944 * arity The number of memories to syncronize.
945 * **in An array of pointers to nodes that produce an output of
950 * The unified memory.
956 * ir_node *new_Bad (void)
957 * -----------------------
959 * Returns the unique Bad node current_ir_graph->bad.
960 * This node is used to express results of dead code elimination.
962 * ir_node *new_Proj (ir_node *arg, ir_mode *mode, long proj)
963 * ----------------------------------------------------------
965 * Selects one entry of a tuple. This is a hidden `fat edge'.
968 * *arg A node producing a tuple.
969 * *mode The mode of the value to project.
970 * proj The position of the value in the tuple.
976 * ir_node *new_Tuple (int arity, ir_node **in)
977 * --------------------------------------------
979 * Builds a Tuple from single values. This is needed to implement
980 * optimizations that remove a node that produced a tuple. The node can be
981 * replaced by the Tuple operation so that the following Proj nodes have not to
982 * be changed. (They are hard to find due to the implementation with pointers
983 * in only one direction.) The Tuple node is smaller than any other
984 * node, so that a node can be changed into a Tuple by just changing it's
985 * opcode and giving it a new in array.
988 * arity The number of tuple elements.
989 * **in An array containing pointers to the nodes producing the
992 * ir_node *new_Id (ir_node *val, ir_mode *mode)
993 * ---------------------------------------------
995 * The single output of the Id operation is it's input. Also needed
999 * COPING WITH DATA OBJECTS
1000 * ========================
1002 * Two kinds of data objects have to be distinguished for generating
1003 * FIRM. First there are local variables other than arrays that are
1004 * known to be alias free. Second there are all other data objects.
1005 * For the first a common SSA representation is built, the second
1006 * are modeled by saving them to memory. The memory is treated as
1007 * a single local variable, the alias problem is hidden in the
1008 * content of this variable.
1010 * All values known in a Block are listed in the block's attribute,
1011 * block.**graph_arr which is used to automatically insert Phi nodes.
1012 * The following two funcions can be used to add a newly computed value
1013 * to the array, or to get the producer of a value, i.e., the current
1016 * inline void set_value (int pos, ir_node *value)
1017 * -----------------------------------------------
1019 * Has to be called for every assignment to a local variable. It
1020 * adds the value to the array of used values at position pos. Pos
1021 * has to be a unique identifier for an entry in the procedure's
1022 * definition table. It can be used to access the value again.
1023 * Requires current_block to be set correctly.
1025 * ir_node *get_value (int pos, ir_mode *mode)
1026 * -------------------------------------------
1028 * Returns the node defining the value referred to by pos. If the
1029 * value is not defined in this block a Phi node is generated and
1030 * all definitions reaching this Phi node are collected. It can
1031 * happen that the algorithm allocates an unnecessary Phi node,
1032 * e.g. if there is only one definition of this value, but this
1033 * definition reaches the currend block on several different
1034 * paths. This Phi node will be eliminated if optimizations are
1035 * turned on right after it's creation.
1036 * Requires current_block to be set correctly.
1038 * There are two special routines for the global store:
1040 * inline void set_store (ir_node *store)
1041 * --------------------------------------
1043 * Adds the store to the array of known values at a reserved
1045 * Requires current_block to be set correctly.
1047 * inline ir_node *get_store (void)
1048 * --------------------------------
1050 * Returns the node defining the actual store.
1051 * Requires current_block to be set correctly.
1059 # include "common.h"
1060 # include "irgraph.h"
1061 # include "irnode.h"
1062 # include "irmode.h"
1063 # include "entity.h"
1067 /***************************************************************************/
1068 /* The raw interface */
1069 /***************************************************************************/
1071 /* Constructs a Block with a fixed number of predecessors.
1072 Does not set current_block. Can not be used with automatic
1073 Phi node construction. */
1074 ir_node *new_r_Block (ir_graph *irg, int arity, ir_node **in);
1075 ir_node *new_r_Start (ir_graph *irg, ir_node *block);
1076 ir_node *new_r_End (ir_graph *irg, ir_node *block);
1077 ir_node *new_r_Jmp (ir_graph *irg, ir_node *block);
1078 ir_node *new_r_Cond (ir_graph *irg, ir_node *block, ir_node *c);
1079 ir_node *new_r_Return (ir_graph *irg, ir_node *block,
1080 ir_node *store, int arity, ir_node **in);
1081 ir_node *new_r_Raise (ir_graph *irg, ir_node *block,
1082 ir_node *store, ir_node *obj);
1083 ir_node *new_r_Const (ir_graph *irg, ir_node *block,
1084 ir_mode *mode, tarval *con);
1085 ir_node *new_r_SymConst (ir_graph *irg, ir_node *block,
1086 type_or_id_p value, symconst_kind symkind);
1087 ir_node *new_r_Sel (ir_graph *irg, ir_node *block, ir_node *store,
1088 ir_node *objptr, int n_index, ir_node **index,
1090 ir_node *new_r_Call (ir_graph *irg, ir_node *block, ir_node *store,
1091 ir_node *callee, int arity, ir_node **in,
1093 ir_node *new_r_Add (ir_graph *irg, ir_node *block,
1094 ir_node *op1, ir_node *op2, ir_mode *mode);
1095 ir_node *new_r_Sub (ir_graph *irg, ir_node *block,
1096 ir_node *op1, ir_node *op2, ir_mode *mode);
1097 ir_node *new_r_Minus (ir_graph *irg, ir_node *block,
1098 ir_node *op, ir_mode *mode);
1099 ir_node *new_r_Mul (ir_graph *irg, ir_node *block,
1100 ir_node *op1, ir_node *op2, ir_mode *mode);
1101 ir_node *new_r_Quot (ir_graph *irg, ir_node *block,
1102 ir_node *memop, ir_node *op1, ir_node *op2);
1103 ir_node *new_r_DivMod (ir_graph *irg, ir_node *block,
1104 ir_node *memop, ir_node *op1, ir_node *op2);
1105 ir_node *new_r_Div (ir_graph *irg, ir_node *block,
1106 ir_node *memop, ir_node *op1, ir_node *op2);
1107 ir_node *new_r_Mod (ir_graph *irg, ir_node *block,
1108 ir_node *memop, ir_node *op1, ir_node *op2);
1109 ir_node *new_r_Abs (ir_graph *irg, ir_node *block,
1110 ir_node *op, ir_mode *mode);
1111 ir_node *new_r_And (ir_graph *irg, ir_node *block,
1112 ir_node *op1, ir_node *op2, ir_mode *mode);
1113 ir_node *new_r_Or (ir_graph *irg, ir_node *block,
1114 ir_node *op1, ir_node *op2, ir_mode *mode);
1115 ir_node *new_r_Eor (ir_graph *irg, ir_node *block,
1116 ir_node *op1, ir_node *op2, ir_mode *mode);
1117 ir_node *new_r_Not (ir_graph *irg, ir_node *block,
1118 ir_node *op, ir_mode *mode);
1119 ir_node *new_r_Cmp (ir_graph *irg, ir_node *block,
1120 ir_node *op1, ir_node *op2);
1121 ir_node *new_r_Shl (ir_graph *irg, ir_node *block,
1122 ir_node *op, ir_node *k, ir_mode *mode);
1123 ir_node *new_r_Shr (ir_graph *irg, ir_node *block,
1124 ir_node *op, ir_node *k, ir_mode *mode);
1125 ir_node *new_r_Shrs (ir_graph *irg, ir_node *block,
1126 ir_node *op, ir_node *k, ir_mode *mode);
1127 ir_node *new_r_Rot (ir_graph *irg, ir_node *block,
1128 ir_node *op, ir_node *k, ir_mode *mode);
1129 ir_node *new_r_Conv (ir_graph *irg, ir_node *block,
1130 ir_node *op, ir_mode *mode);
1131 ir_node *new_r_Phi (ir_graph *irg, ir_node *block, int arity,
1132 ir_node **in, ir_mode *mode);
1133 ir_node *new_r_Load (ir_graph *irg, ir_node *block,
1134 ir_node *store, ir_node *adr);
1135 ir_node *new_r_Store (ir_graph *irg, ir_node *block,
1136 ir_node *store, ir_node *adr, ir_node *val);
1137 ir_node *new_r_Alloc (ir_graph *irg, ir_node *block, ir_node *store,
1138 ir_node *size, type *alloc_type, where_alloc where);
1139 ir_node *new_r_Free (ir_graph *irg, ir_node *block, ir_node *store,
1140 ir_node *ptr, ir_node *size, type *free_type);
1141 ir_node *new_r_Sync (ir_graph *irg, ir_node *block, int arity, ir_node **in);
1142 ir_node *new_r_Proj (ir_graph *irg, ir_node *block, ir_node *arg,
1143 ir_mode *mode, long proj);
1144 ir_node *new_r_Tuple (ir_graph *irg, ir_node *block,
1145 int arity, ir_node **in);
1146 ir_node *new_r_Id (ir_graph *irg, ir_node *block,
1147 ir_node *val, ir_mode *mode);
1148 ir_node *new_r_Bad ();
1151 /*************************************************************************/
1152 /* The block oriented interface */
1153 /*************************************************************************/
1155 /* Sets the current block in which the following constructors place the
1156 nodes they construct. */
1157 void switch_block (ir_node *target);
1159 /* Constructs a Block with a fixed number of predecessors.
1160 Does set current_block. Can be used with automatic Phi
1161 node construction. */
1162 ir_node *new_Block(int arity, ir_node **in);
1163 ir_node *new_Start (void);
1164 ir_node *new_End (void);
1165 ir_node *new_Jmp (void);
1166 ir_node *new_Cond (ir_node *c);
1167 ir_node *new_Return (ir_node *store, int arity, ir_node **in);
1168 ir_node *new_Raise (ir_node *store, ir_node *obj);
1169 ir_node *new_Const (ir_mode *mode, tarval *con);
1170 ir_node *new_SymConst (type_or_id_p value, symconst_kind kind);
1171 ir_node *new_simpleSel(ir_node *store, ir_node *objptr, entity *ent);
1172 ir_node *new_Sel (ir_node *store, ir_node *objptr, int arity, ir_node **in,
1174 ir_node *new_Call (ir_node *store, ir_node *callee, int arity, ir_node **in,
1176 ir_node *new_Add (ir_node *op1, ir_node *op2, ir_mode *mode);
1177 ir_node *new_Sub (ir_node *op1, ir_node *op2, ir_mode *mode);
1178 ir_node *new_Minus (ir_node *op, ir_mode *mode);
1179 ir_node *new_Mul (ir_node *op1, ir_node *op2, ir_mode *mode);
1180 ir_node *new_Quot (ir_node *memop, ir_node *op1, ir_node *op2);
1181 ir_node *new_DivMod (ir_node *memop, ir_node *op1, ir_node *op2);
1182 ir_node *new_Div (ir_node *memop, ir_node *op1, ir_node *op2);
1183 ir_node *new_Mod (ir_node *memop, ir_node *op1, ir_node *op2);
1184 ir_node *new_Abs (ir_node *op, ir_mode *mode);
1185 ir_node *new_And (ir_node *op1, ir_node *op2, ir_mode *mode);
1186 ir_node *new_Or (ir_node *op1, ir_node *op2, ir_mode *mode);
1187 ir_node *new_Eor (ir_node *op1, ir_node *op2, ir_mode *mode);
1188 ir_node *new_Not (ir_node *op, ir_mode *mode);
1189 ir_node *new_Shl (ir_node *op, ir_node *k, ir_mode *mode);
1190 ir_node *new_Shr (ir_node *op, ir_node *k, ir_mode *mode);
1191 ir_node *new_Shrs (ir_node *op, ir_node *k, ir_mode *mode);
1192 ir_node *new_Rot (ir_node *op, ir_node *k, ir_mode *mode);
1193 ir_node *new_Cmp (ir_node *op1, ir_node *op2);
1194 ir_node *new_Conv (ir_node *op, ir_mode *mode);
1195 ir_node *new_Phi (int arity, ir_node **in, ir_mode *mode);
1196 ir_node *new_Load (ir_node *store, ir_node *addr);
1197 ir_node *new_Store (ir_node *store, ir_node *addr, ir_node *val);
1198 ir_node *new_Alloc (ir_node *store, ir_node *size, type *alloc_type,
1200 ir_node *new_Free (ir_node *store, ir_node *ptr, ir_node *size,
1202 ir_node *new_Sync (int arity, ir_node **in);
1203 ir_node *new_Proj (ir_node *arg, ir_mode *mode, long proj);
1204 ir_node *new_Tuple (int arity, ir_node **in);
1205 ir_node *new_Id (ir_node *val, ir_mode *mode);
1206 ir_node *new_Bad (void);
1208 /***********************************************************************/
1209 /* The comfortable interface. */
1210 /* Supports automatic Phi node construction. */
1211 /* All routines of the block oriented interface except new_Block are */
1213 /***********************************************************************/
1215 /** Block construction **/
1216 /* immature Block without predecessors */
1217 ir_node *new_immBlock (void);
1219 /* Add a control flow edge to an immature block. */
1220 void add_in_edge (ir_node *immblock, ir_node *jmp);
1222 /* fixes the number of predecessors of a block. */
1223 void mature_block (ir_node *block);
1225 /** Parameter administration **/
1226 /* Read a value from the array with the local variables. Use this
1227 function to obtain the last definition of the value associated with
1228 pos. Pos may not exceed the value passed as n_loc to new_ir_graph. */
1229 ir_node *get_value (int pos, ir_mode *mode);
1231 /* Write a value in the array with the local variables. Use this function
1232 to remember a new definition of the value associated with pos. Pos may
1233 not exceed the value passed as n_loc to new_ir_graph. */
1234 void set_value (int pos, ir_node *value);
1237 Use this function to get the most recent version of the store (type M).
1238 Internally it does the same as get_value. */
1239 ir_node *get_store (void);
1241 /* Write a store. */
1242 void set_store (ir_node *store);
1244 /***********************************************************************/
1245 /* initialize ir construction */
1246 /***********************************************************************/
1247 void init_cons (void);
1250 # endif /* _IRCONS_H_ */