1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Authors: Martin Trapp, Christian Schaefer
6 ** ircons.c: basic and more detailed irnode constructors
7 ** store, block and parameter administration.
8 ** Adapted to extended FIRM nodes (exceptions...) and commented
9 ** by Goetz Lindenmaier
12 # include "irgraph_t.h"
13 # include "irnode_t.h"
14 # include "irmode_t.h"
22 /* memset belongs to string.h */
25 #if USE_EXPICIT_PHI_IN_STACK
26 /* A stack needed for the automatic Phi node construction in constructor
27 Phi_in. Redefinition in irgraph.c!! */
32 typedef struct Phi_in_stack Phi_in_stack;
35 /*********************************************** */
36 /** privat interfaces, for professional use only */
38 /* Constructs a Block with a fixed number of predecessors.
39 Does not set current_block. Can not be used with automatic
40 Phi node construction. */
42 new_r_Block (ir_graph *irg, int arity, ir_node **in)
46 res = new_ir_node (irg, NULL, op_Block, mode_R, arity, in);
47 set_Block_matured(res, 1);
48 set_Block_block_visited(res, 0);
55 new_r_Start (ir_graph *irg, ir_node *block)
59 res = new_ir_node (irg, block, op_Start, mode_T, 0, NULL);
66 new_r_End (ir_graph *irg, ir_node *block)
70 res = new_ir_node (irg, block, op_End, mode_X, -1, NULL);
76 /* Creates a Phi node with all predecessors. Calling this constructor
77 is only allowed if the corresponding block is mature. */
79 new_r_Phi (ir_graph *irg, ir_node *block, int arity, ir_node **in, ir_mode *mode)
83 assert( get_Block_matured(block) );
84 assert( get_irn_arity(block) == arity );
86 res = new_ir_node (irg, block, op_Phi, mode, arity, in);
94 new_r_Const (ir_graph *irg, ir_node *block, ir_mode *mode, tarval *con)
97 res = new_ir_node (irg, block, op_Const, mode, 0, NULL);
103 res = local_optimize_newby (res);
110 new_r_Id (ir_graph *irg, ir_node *block, ir_node *val, ir_mode *mode)
112 ir_node *in[1] = {val};
114 res = new_ir_node (irg, block, op_Id, mode, 1, in);
115 res = optimize (res);
121 new_r_Proj (ir_graph *irg, ir_node *block, ir_node *arg, ir_mode *mode,
124 ir_node *in[1] = {arg};
126 res = new_ir_node (irg, block, op_Proj, mode, 1, in);
127 res->attr.proj = proj;
130 assert(get_Proj_pred(res));
131 assert(get_nodes_Block(get_Proj_pred(res)));
133 res = optimize (res);
141 new_r_Conv (ir_graph *irg, ir_node *block, ir_node *op, ir_mode *mode)
143 ir_node *in[1] = {op};
145 res = new_ir_node (irg, block, op_Conv, mode, 1, in);
146 res = optimize (res);
153 new_r_Tuple (ir_graph *irg, ir_node *block, int arity, ir_node **in)
157 res = new_ir_node (irg, block, op_Tuple, mode_T, arity, in);
158 res = optimize (res);
164 new_r_Add (ir_graph *irg, ir_node *block,
165 ir_node *op1, ir_node *op2, ir_mode *mode)
167 ir_node *in[2] = {op1, op2};
169 res = new_ir_node (irg, block, op_Add, mode, 2, in);
170 res = optimize (res);
176 new_r_Sub (ir_graph *irg, ir_node *block,
177 ir_node *op1, ir_node *op2, ir_mode *mode)
179 ir_node *in[2] = {op1, op2};
181 res = new_ir_node (irg, block, op_Sub, mode, 2, in);
182 res = optimize (res);
188 new_r_Minus (ir_graph *irg, ir_node *block,
189 ir_node *op, ir_mode *mode)
191 ir_node *in[1] = {op};
193 res = new_ir_node (irg, block, op_Minus, mode, 1, in);
194 res = optimize (res);
200 new_r_Mul (ir_graph *irg, ir_node *block,
201 ir_node *op1, ir_node *op2, ir_mode *mode)
203 ir_node *in[2] = {op1, op2};
205 res = new_ir_node (irg, block, op_Mul, mode, 2, in);
206 res = optimize (res);
212 new_r_Quot (ir_graph *irg, ir_node *block,
213 ir_node *memop, ir_node *op1, ir_node *op2)
215 ir_node *in[3] = {memop, op1, op2};
217 res = new_ir_node (irg, block, op_Quot, mode_T, 3, in);
218 res = optimize (res);
224 new_r_DivMod (ir_graph *irg, ir_node *block,
225 ir_node *memop, ir_node *op1, ir_node *op2)
227 ir_node *in[3] = {memop, op1, op2};
229 res = new_ir_node (irg, block, op_DivMod, mode_T, 3, in);
230 res = optimize (res);
236 new_r_Div (ir_graph *irg, ir_node *block,
237 ir_node *memop, ir_node *op1, ir_node *op2)
239 ir_node *in[3] = {memop, op1, op2};
241 res = new_ir_node (irg, block, op_Div, mode_T, 3, in);
242 res = optimize (res);
248 new_r_Mod (ir_graph *irg, ir_node *block,
249 ir_node *memop, ir_node *op1, ir_node *op2)
251 ir_node *in[3] = {memop, op1, op2};
253 res = new_ir_node (irg, block, op_Mod, mode_T, 3, in);
254 res = optimize (res);
260 new_r_And (ir_graph *irg, ir_node *block,
261 ir_node *op1, ir_node *op2, ir_mode *mode)
263 ir_node *in[2] = {op1, op2};
265 res = new_ir_node (irg, block, op_And, mode, 2, in);
266 res = optimize (res);
272 new_r_Or (ir_graph *irg, ir_node *block,
273 ir_node *op1, ir_node *op2, ir_mode *mode)
275 ir_node *in[2] = {op1, op2};
277 res = new_ir_node (irg, block, op_Or, mode, 2, in);
278 res = optimize (res);
284 new_r_Eor (ir_graph *irg, ir_node *block,
285 ir_node *op1, ir_node *op2, ir_mode *mode)
287 ir_node *in[2] = {op1, op2};
289 res = new_ir_node (irg, block, op_Eor, mode, 2, in);
290 res = optimize (res);
296 new_r_Not (ir_graph *irg, ir_node *block,
297 ir_node *op, ir_mode *mode)
299 ir_node *in[1] = {op};
301 res = new_ir_node (irg, block, op_Not, mode, 1, in);
302 res = optimize (res);
308 new_r_Shl (ir_graph *irg, ir_node *block,
309 ir_node *op, ir_node *k, ir_mode *mode)
311 ir_node *in[2] = {op, k};
313 res = new_ir_node (irg, block, op_Shl, mode, 2, in);
314 res = optimize (res);
320 new_r_Shr (ir_graph *irg, ir_node *block,
321 ir_node *op, ir_node *k, ir_mode *mode)
323 ir_node *in[2] = {op, k};
325 res = new_ir_node (irg, block, op_Shr, mode, 2, in);
326 res = optimize (res);
332 new_r_Shrs (ir_graph *irg, ir_node *block,
333 ir_node *op, ir_node *k, ir_mode *mode)
335 ir_node *in[2] = {op, k};
337 res = new_ir_node (irg, block, op_Shrs, mode, 2, in);
338 res = optimize (res);
344 new_r_Rot (ir_graph *irg, ir_node *block,
345 ir_node *op, ir_node *k, ir_mode *mode)
347 ir_node *in[2] = {op, k};
349 res = new_ir_node (irg, block, op_Rot, mode, 2, in);
350 res = optimize (res);
356 new_r_Abs (ir_graph *irg, ir_node *block,
357 ir_node *op, ir_mode *mode)
359 ir_node *in[1] = {op};
361 res = new_ir_node (irg, block, op_Abs, mode, 1, in);
362 res = optimize (res);
368 new_r_Cmp (ir_graph *irg, ir_node *block,
369 ir_node *op1, ir_node *op2)
371 ir_node *in[2] = {op1, op2};
373 res = new_ir_node (irg, block, op_Cmp, mode_T, 2, in);
374 res = optimize (res);
380 new_r_Jmp (ir_graph *irg, ir_node *block)
384 res = new_ir_node (irg, block, op_Jmp, mode_X, 0, in);
385 res = optimize (res);
391 new_r_Cond (ir_graph *irg, ir_node *block, ir_node *c)
393 ir_node *in[1] = {c};
395 res = new_ir_node (irg, block, op_Cond, mode_T, 1, in);
396 res = optimize (res);
402 new_r_Call (ir_graph *irg, ir_node *block, ir_node *store,
403 ir_node *callee, int arity, ir_node **in, type_method *type)
410 NEW_ARR_A (ir_node *, r_in, r_arity);
413 memcpy (&r_in[2], in, sizeof (ir_node *) * arity);
415 res = new_ir_node (irg, block, op_Call, mode_T, r_arity, r_in);
417 set_Call_type(res, type);
418 res = optimize (res);
424 new_r_Return (ir_graph *irg, ir_node *block,
425 ir_node *store, int arity, ir_node **in)
432 NEW_ARR_A (ir_node *, r_in, r_arity);
434 memcpy (&r_in[1], in, sizeof (ir_node *) * arity);
435 res = new_ir_node (irg, block, op_Return, mode_X, r_arity, r_in);
436 res = optimize (res);
442 new_r_Raise (ir_graph *irg, ir_node *block, ir_node *store, ir_node *obj)
444 ir_node *in[2] = {store, obj};
446 res = new_ir_node (irg, block, op_Raise, mode_X, 2, in);
448 res = optimize (res);
454 new_r_Load (ir_graph *irg, ir_node *block,
455 ir_node *store, ir_node *adr)
457 ir_node *in[2] = {store, adr};
459 res = new_ir_node (irg, block, op_Load, mode_T, 2, in);
461 res = optimize (res);
467 new_r_Store (ir_graph *irg, ir_node *block,
468 ir_node *store, ir_node *adr, ir_node *val)
470 ir_node *in[3] = {store, adr, val};
472 res = new_ir_node (irg, block, op_Store, mode_T, 3, in);
474 res = optimize (res);
480 new_r_Alloc (ir_graph *irg, ir_node *block, ir_node *store,
481 ir_node *size, type *alloc_type, where_alloc where)
483 ir_node *in[2] = {store, size};
485 res = new_ir_node (irg, block, op_Alloc, mode_T, 2, in);
487 res->attr.a.where = where;
488 res->attr.a.type = alloc_type;
490 res = optimize (res);
496 new_r_Free (ir_graph *irg, ir_node *block, ir_node *store,
497 ir_node *ptr, ir_node *size, type *free_type)
499 ir_node *in[3] = {store, ptr, size};
501 res = new_ir_node (irg, block, op_Free, mode_T, 3, in);
503 res->attr.f = free_type;
505 res = optimize (res);
511 new_r_Sel (ir_graph *irg, ir_node *block, ir_node *store, ir_node *objptr,
512 int arity, ir_node **in, entity *ent)
519 NEW_ARR_A (ir_node *, r_in, r_arity);
522 memcpy (&r_in[2], in, sizeof (ir_node *) * arity);
523 res = new_ir_node (irg, block, op_Sel, mode_p, r_arity, r_in);
525 res->attr.s.ltyp = static_linkage;
526 res->attr.s.ent = ent;
528 res = optimize (res);
534 new_r_SymConst (ir_graph *irg, ir_node *block, type_or_id_p value,
535 symconst_kind symkind)
540 if (symkind == linkage_ptr_info)
544 res = new_ir_node (irg, block, op_SymConst, mode, 0, in);
546 res->attr.i.num = symkind;
547 if (symkind == linkage_ptr_info) {
548 res->attr.i.tori.ptrinfo = (ident *)value;
550 assert ( ( (symkind == type_tag)
551 || (symkind == size))
552 && (is_type(value)));
553 res->attr.i.tori.typ = (type *)value;
555 res = optimize (res);
561 new_r_Sync (ir_graph *irg, ir_node *block, int arity, ir_node **in)
565 res = new_ir_node (irg, block, op_Sync, mode_M, arity, in);
567 res = optimize (res);
575 return current_ir_graph->bad;
578 /***********************/
579 /** public interfaces */
580 /** construction tools */
587 res = new_ir_node (current_ir_graph, current_ir_graph->current_block,
588 op_Start, mode_T, 0, NULL);
590 res = optimize (res);
600 res = new_ir_node (current_ir_graph, current_ir_graph->current_block,
601 op_End, mode_X, -1, NULL);
603 res = optimize (res);
609 /* Constructs a Block with a fixed number of predecessors.
610 Does set current_block. Can be used with automatic Phi
611 node construction. */
613 new_Block (int arity, ir_node **in)
617 res = new_r_Block (current_ir_graph, arity, in);
618 current_ir_graph->current_block = res;
620 /* Create and initialize array for Phi-node construction. */
621 res->attr.block.graph_arr = NEW_ARR_D (ir_node *, current_ir_graph->obst,
622 current_ir_graph->n_loc);
623 memset(res->attr.block.graph_arr, 0, sizeof(ir_node *)*current_ir_graph->n_loc);
625 res = optimize (res);
631 /*************************************************************************/
632 /* Methods necessary for automatic Phi node creation */
634 ir_node *phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
635 ir_node *get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
636 ir_node *new_r_Phi0 (ir_graph *irg, ir_node *block, ir_mode *mode)
637 ir_node *new_r_Phi_in (ir_graph *irg, ir_node *block, ir_mode *mode, ir_node **in, int ins)
639 Call Graph: ( A ---> B == A "calls" B)
641 get_value mature_block
649 get_r_value_internal |
653 new_r_Phi0 new_r_Phi_in
655 *****************************************************************************/
657 /* Creates a Phi node with 0 predecessors */
659 new_r_Phi0 (ir_graph *irg, ir_node *block, ir_mode *mode)
662 res = new_ir_node (irg, block, op_Phi, mode, 0, NULL);
667 /* There are two implementations of the Phi node construction. The first
668 is faster, but does not work for blocks with more than 2 predecessors.
669 The second works always but is slower and causes more unnecessary Phi
671 Select the implementations by the following preprocessor flag set in
673 #if USE_FAST_PHI_CONSTRUCTION
675 /* This is a stack used for allocating and deallocating nodes in
676 new_r_Phi_in. The original implementation used the obstack
677 to model this stack, now it is explicit. This reduces side effects.
679 #if USE_EXPICIT_PHI_IN_STACK
684 res = (Phi_in_stack *) malloc ( sizeof (Phi_in_stack));
686 res->stack = NEW_ARR_F (ir_node *, 1);
692 void free_to_Phi_in_stack(ir_node *phi) {
693 assert(get_irn_opcode(phi) == iro_Phi);
695 if (ARR_LEN(current_ir_graph->Phi_in_stack->stack) ==
696 current_ir_graph->Phi_in_stack->pos)
697 ARR_APP1 (ir_node *, current_ir_graph->Phi_in_stack->stack, phi);
699 current_ir_graph->Phi_in_stack->stack[current_ir_graph->Phi_in_stack->pos] = phi;
701 (current_ir_graph->Phi_in_stack->pos)++;
705 alloc_or_pop_from_Phi_in_stack(ir_graph *irg, ir_node *block, ir_mode *mode,
706 int arity, ir_node **in) {
708 ir_node **stack = current_ir_graph->Phi_in_stack->stack;
709 int pos = current_ir_graph->Phi_in_stack->pos;
713 /* We need to allocate a new node */
714 res = new_ir_node (irg, block, op_Phi, mode, arity, in);
716 /* reuse the old node and initialize it again. */
719 assert (res->kind == k_ir_node);
720 assert (res->op == op_Phi);
725 /* ???!!! How to free the old in array?? */
726 res->in = NEW_ARR_D (ir_node *, irg->obst, (arity+1));
728 memcpy (&res->in[1], in, sizeof (ir_node *) * arity);
730 (current_ir_graph->Phi_in_stack->pos)--;
734 #endif /* USE_EXPICIT_PHI_IN_STACK */
736 /* Creates a Phi node with a given, fixed array **in of predecessors.
737 If the Phi node is unnecessary, as the same value reaches the block
738 through all control flow paths, it is eliminated and the value
739 returned directly. This constructor is only intended for use in
740 the automatic Phi node generation triggered by get_value or mature.
741 The implementation is quite tricky and depends on the fact, that
742 the nodes are allocated on a stack:
743 The in array contains predecessors and NULLs. The NULLs appear,
744 if get_r_value_internal, that computed the predecessors, reached
745 the same block on two paths. In this case the same value reaches
746 this block on both paths, there is no definition in between. We need
747 not allocate a Phi where these path's merge, but we have to communicate
748 this fact to the caller. This happens by returning a pointer to the
749 node the caller _will_ allocate. (Yes, we predict the address. We can
750 do so because the nodes are allocated on the obstack.) The caller then
751 finds a pointer to itself and, when this routine is called again,
755 new_r_Phi_in (ir_graph *irg, ir_node *block, ir_mode *mode,
756 ir_node **in, int ins)
759 ir_node *res, *known;
761 /* allocate a new node on the obstack.
762 This can return a node to which some of the pointers in the in-array
764 Attention: the constructor copies the in array, i.e., the later changes
765 to the array in this routine do not affect the constructed node! If
766 the in array contains NULLs, there will be missing predecessors in the
768 Is this a possible internal state of the Phi node generation? */
769 #if USE_EXPICIT_PHI_IN_STACK
770 res = known = alloc_or_pop_from_Phi_in_stack(irg, block, mode, ins, in);
772 res = known = new_ir_node (irg, block, op_Phi, mode, ins, in);
774 /* The in-array can contain NULLs. These were returned by
775 get_r_value_internal if it reached the same block/definition on a
777 The NULLs are replaced by the node itself to simplify the test in the
779 for (i=0; i < ins; ++i)
780 if (in[i] == NULL) in[i] = res;
782 /* This loop checks whether the Phi has more than one predecessor.
783 If so, it is a real Phi node and we break the loop. Else the
784 Phi node merges the same definition on several paths and therefore
786 for (i=0; i < ins; ++i)
788 if (in[i]==res || in[i]==known) continue;
796 /* i==ins: there is at most one predecessor, we don't need a phi node. */
798 #if USE_EXPICIT_PHI_IN_STACK
799 free_to_Phi_in_stack(res);
801 obstack_free (current_ir_graph->obst, res);
805 res = optimize (res);
809 /* return the pointer to the Phi node. This node might be deallocated! */
814 get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
816 /** This function computes the predecessors for a real Phi node, and then
817 allocates and returns this node. The routine called to allocate the
818 node might optimize it away and return a real value, or even a pointer
819 to a deallocated Phi node on top of the obstack!
820 This function is called with an in-array of proper size. **/
821 static inline ir_node *
822 phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
824 ir_node *prevBlock, *res;
827 /* This loop goes to all predecessor blocks of the block the Phi node is in
828 and there finds the operands of the Phi node by calling
829 get_r_value_internal. */
830 for (i = 1; i <= ins; ++i) {
831 assert (block->in[i]);
832 prevBlock = block->in[i]->in[0]; /* go past control flow op to prev block */
834 nin[i-1] = get_r_value_internal (prevBlock, pos, mode);
837 /* After collecting all predecessors into the array nin a new Phi node
838 with these predecessors is created. This constructor contains an
839 optimization: If all predecessors of the Phi node are identical it
840 returns the only operand instead of a new Phi node. If the value
841 passes two different control flow edges without being defined, and
842 this is the second path treated, a pointer to the node that will be
843 allocated for the first path (recursion) is returned. We already
844 know the address of this node, as it is the next node to be allocated
845 and will be placed on top of the obstack. (The obstack is a _stack_!) */
846 res = new_r_Phi_in (current_ir_graph, block, mode, nin, ins);
848 /* Now we now the value for "pos" and can enter it in the array with
849 all known local variables. Attention: this might be a pointer to
850 a node, that later will be allocated!!! See new_r_Phi_in.
851 If this is called in mature, after some set_value in the same block,
852 the proper value must not be overwritten:
854 get_value (makes Phi0, put's it into graph_arr)
855 set_value (overwrites Phi0 in graph_arr)
856 mature_block (upgrades Phi0, puts it again into graph_arr, overwriting
859 if (!block->attr.block.graph_arr[pos]) {
860 block->attr.block.graph_arr[pos] = res;
862 /* printf(" value already computed by %s\n",
863 id_to_str(block->attr.block.graph_arr[pos]->op->name)); */
869 /* This function returns the last definition of a variable. In case
870 this variable was last defined in a previous block, Phi nodes are
871 inserted. If the part of the firm graph containing the definition
872 is not yet constructed, a dummy Phi node is returned. */
874 get_r_value_internal (ir_node *block, int pos, ir_mode *mode)
877 /* There are 4 cases to treat.
879 1. The block is not mature and we visit it the first time. We can not
880 create a proper Phi node, therefore a Phi0, i.e., a Phi without
881 predecessors is returned. This node is added to the linked list (field
882 "link") of the containing block to be completed when this block is
883 matured. (Comlpletion will add a new Phi and turn the Phi0 into an Id
886 2. The value is already known in this block, graph_arr[pos] is set and we
887 visit the block the first time. We can return the value without
888 creating any new nodes.
890 3. The block is mature and we visit it the first time. A Phi node needs
891 to be created (phi_merge). If the Phi is not needed, as all it's
892 operands are the same value reaching the block through different
893 paths, it's optimized away and the value itself is returned.
895 4. The block is mature, and we visit it the second time. Now two
896 subcases are possible:
897 * The value was computed completely the last time we were here. This
898 is the case if there is no loop. We can return the proper value.
899 * The recursion that visited this node and set the flag did not
900 return yet. We are computing a value in a loop and need to
901 break the recursion without knowing the result yet.
902 @@@ strange case. Straight forward we would create a Phi before
903 starting the computation of it's predecessors. In this case we will find
904 a Phi here in any case. The problem is that this implementation only
905 creates a Phi after computing the predecessors, so that it is hard to
906 compute self references of this Phi. @@@
907 There is no simple check for the second subcase. Therefore we check
908 for a second visit and treat all such cases as the second subcase.
909 Anyways, the basic situation is the same: we reached a block
910 on two paths without finding a definition of the value: No Phi
911 nodes are needed on both paths.
912 We return this information "Two paths, no Phi needed" by a very tricky
913 implementation that relies on the fact that an obstack is a stack and
914 will return a node with the same address on different allocations.
915 Look also at phi_merge and new_r_phi_in to understand this.
916 @@@ Unfortunately this does not work, see testprogram three_cfpred_example.
920 /* case 4 -- already visited. */
921 if (get_irn_visited(block) == get_irg_visited(current_ir_graph)) return NULL;
923 /* visited the first time */
924 set_irn_visited(block, get_irg_visited(current_ir_graph));
926 /* Get the local valid value */
927 res = block->attr.block.graph_arr[pos];
929 /* case 2 -- If the value is actually computed, return it. */
930 if (res) { return res;};
932 if (block->attr.block.matured) { /* case 3 */
934 /* The Phi has the same amount of ins as the corresponding block. */
935 int ins = get_irn_arity(block);
937 NEW_ARR_A (ir_node *, nin, ins);
939 /* Phi merge collects the predecessors and then creates a node. */
940 res = phi_merge (block, pos, mode, nin, ins);
942 } else { /* case 1 */
943 /* The block is not mature, we don't know how many in's are needed. A Phi
944 with zero predecessors is created. Such a Phi node is called Phi0
945 node. (There is also an obsolete Phi0 opcode.) The Phi0 is then added
946 to the list of Phi0 nodes in this block to be matured by mature_block
948 The Phi0 has to remember the pos of it's internal value. If the real
949 Phi is computed, pos is used to update the array with the local
952 res = new_r_Phi0 (current_ir_graph, block, mode);
953 res->attr.phi0_pos = pos;
954 res->link = block->link;
958 /* If we get here, the frontend missed a use-before-definition error */
961 printf("Error: no value set. Use of undefined variable. Initializing
963 assert (mode->code >= irm_f && mode->code <= irm_p);
964 res = new_r_Const (current_ir_graph, block, mode,
965 tarval_mode_null[mode->code]);
968 /* The local valid value is available now. */
969 block->attr.block.graph_arr[pos] = res;
976 /** This is the simple algorithm. If first generates a Phi0, then
977 it starts the recursion. This causes an Id at the entry of
978 every block that has no definition of the value! **/
980 #if USE_EXPICIT_PHI_IN_STACK
982 Phi_in_stack * new_Phi_in_stack() { return NULL; }
986 new_r_Phi_in (ir_graph *irg, ir_node *block, ir_mode *mode,
987 ir_node **in, int ins)
990 ir_node *res, *known;
992 /* Allocate a new node on the obstack. The allocation copies the in
994 res = new_ir_node (irg, block, op_Phi, mode, ins, in);
996 /* This loop checks whether the Phi has more than one predecessor.
997 If so, it is a real Phi node and we break the loop. Else the
998 Phi node merges the same definition on several paths and therefore
999 is not needed. Don't consider Bad nodes! */
1001 for (i=0; i < ins; ++i)
1003 if (in[i]==res || in[i]==known || is_Bad(in[i])) continue;
1011 /* i==ins: there is at most one predecessor, we don't need a phi node. */
1014 obstack_free (current_ir_graph->obst, res);
1017 /* A undefined value, e.g., in unreachable code. */
1021 res = optimize (res);
1029 get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
1031 /** This function allocates a dummy Phi node to break recursions,
1032 computes the predecessors for the real phi node, and then
1033 allocates and returns this node. The routine called to allocate the
1034 node might optimize it away and return a real value.
1035 This function is called with an in-array of proper size. **/
1036 static inline ir_node *
1037 phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
1039 ir_node *prevBlock, *res, *phi0;
1043 /* If this block has no value at pos create a Phi0 and remember it
1044 in graph_arr to break recursions. */
1046 if (!block->attr.block.graph_arr[pos]) {
1047 /* Even if all variables are defined before use, it can happen that
1048 we get to the start block, if a cond has been replaced by a tuple
1049 (bad, jmp). As the start has a self referencing control flow edge,
1050 we get a self referencing Id, which is hard to optimize away. We avoid
1051 this by defining the value as a Bad node. *
1052 if (block == get_irg_start_block(current_ir_graph)) {
1053 block->attr.block.graph_arr[pos] = new_Bad();
1056 phi0 = new_r_Phi0(current_ir_graph, block, mode);
1057 block->attr.block.graph_arr[pos] = phi0;
1061 /* This loop goes to all predecessor blocks of the block the Phi node
1062 is in and there finds the operands of the Phi node by calling
1063 get_r_value_internal. */
1064 for (i = 1; i <= ins; ++i) {
1065 assert (block->in[i]);
1066 if (is_Bad(block->in[i])) {
1067 /* In case a Cond has been optimized we would get right to the start block
1068 with an invalid definition. */
1069 nin[i-1] = new_Bad();
1072 prevBlock = block->in[i]->in[0]; /* go past control flow op to prev block */
1074 if (!is_Bad(prevBlock)) {
1075 nin[i-1] = get_r_value_internal (prevBlock, pos, mode);
1077 nin[i-1] = new_Bad();
1081 /* After collecting all predecessors into the array nin a new Phi node
1082 with these predecessors is created. This constructor contains an
1083 optimization: If all predecessors of the Phi node are identical it
1084 returns the only operand instead of a new Phi node. */
1085 res = new_r_Phi_in (current_ir_graph, block, mode, nin, ins);
1087 /* In case we allocated a Phi0 node at the beginning of this procedure,
1088 we need to exchange this Phi0 with the real Phi. */
1090 exchange(phi0, res);
1091 block->attr.block.graph_arr[pos] = res;
1097 /* This function returns the last definition of a variable. In case
1098 this variable was last defined in a previous block, Phi nodes are
1099 inserted. If the part of the firm graph containing the definition
1100 is not yet constructed, a dummy Phi node is returned. */
1102 get_r_value_internal (ir_node *block, int pos, ir_mode *mode)
1105 /* There are 4 cases to treat.
1107 1. The block is not mature and we visit it the first time. We can not
1108 create a proper Phi node, therefore a Phi0, i.e., a Phi without
1109 predecessors is returned. This node is added to the linked list (field
1110 "link") of the containing block to be completed when this block is
1111 matured. (Comlpletion will add a new Phi and turn the Phi0 into an Id
1114 2. The value is already known in this block, graph_arr[pos] is set and we
1115 visit the block the first time. We can return the value without
1116 creating any new nodes.
1118 3. The block is mature and we visit it the first time. A Phi node needs
1119 to be created (phi_merge). If the Phi is not needed, as all it's
1120 operands are the same value reaching the block through different
1121 paths, it's optimized away and the value itself is returned.
1123 4. The block is mature, and we visit it the second time. Now two
1124 subcases are possible:
1125 * The value was computed completely the last time we were here. This
1126 is the case if there is no loop. We can return the proper value.
1127 * The recursion that visited this node and set the flag did not
1128 return yet. We are computing a value in a loop and need to
1129 break the recursion. This case only happens if we visited
1130 the same block with phi_merge before, which inserted a Phi0.
1131 So we return the Phi0.
1134 /* case 4 -- already visited. */
1135 if (get_irn_visited(block) == get_irg_visited(current_ir_graph)) {
1136 /* As phi_merge allocates a Phi0 this value is always defined. Here
1137 is the critical difference of the two algorithms. */
1138 assert(block->attr.block.graph_arr[pos]);
1139 return block->attr.block.graph_arr[pos];
1142 /* visited the first time */
1143 set_irn_visited(block, get_irg_visited(current_ir_graph));
1145 /* Get the local valid value */
1146 res = block->attr.block.graph_arr[pos];
1148 /* case 2 -- If the value is actually computed, return it. */
1149 if (res) { return res; };
1151 if (block->attr.block.matured) { /* case 3 */
1153 /* The Phi has the same amount of ins as the corresponding block. */
1154 int ins = get_irn_arity(block);
1156 NEW_ARR_A (ir_node *, nin, ins);
1158 /* Phi merge collects the predecessors and then creates a node. */
1159 res = phi_merge (block, pos, mode, nin, ins);
1161 } else { /* case 1 */
1162 /* The block is not mature, we don't know how many in's are needed. A Phi
1163 with zero predecessors is created. Such a Phi node is called Phi0
1164 node. The Phi0 is then added to the list of Phi0 nodes in this block
1165 to be matured by mature_block later.
1166 The Phi0 has to remember the pos of it's internal value. If the real
1167 Phi is computed, pos is used to update the array with the local
1169 res = new_r_Phi0 (current_ir_graph, block, mode);
1170 res->attr.phi0_pos = pos;
1171 res->link = block->link;
1175 /* If we get here, the frontend missed a use-before-definition error */
1178 printf("Error: no value set. Use of undefined variable. Initializing
1180 assert (mode->code >= irm_f && mode->code <= irm_p);
1181 res = new_r_Const (current_ir_graph, block, mode,
1182 tarval_mode_null[mode->code]);
1185 /* The local valid value is available now. */
1186 block->attr.block.graph_arr[pos] = res;
1191 #endif /* USE_FAST_PHI_CONSTRUCTION */
1193 /****************************************************************************/
1195 /** Finalize a Block node, when all control flows are known. */
1196 /** Acceptable parameters are only Block nodes. */
1198 mature_block (ir_node *block)
1205 assert (get_irn_opcode(block) == iro_Block);
1207 if (!get_Block_matured(block)) {
1209 /* turn the dynamic in-array into a static one. */
1210 ins = ARR_LEN (block->in)-1;
1211 NEW_ARR_A (ir_node *, nin, ins);
1213 /* Traverse a chain of Phi nodes attached to this block and mature
1215 for (n = block->link; n; n=next) {
1216 inc_irg_visited(current_ir_graph);
1218 exchange (n, phi_merge (block, n->attr.phi0_pos, n->mode, nin, ins));
1221 block->attr.block.matured = 1;
1223 /* Now, as the block is a finished firm node, we can optimize it.
1224 Since other nodes have been allocated since the block was created
1225 we can not free the node on the obstack. Therefore we have to call
1227 Unfortunately the optimization does not change a lot, as all allocated
1228 nodes refer to the unoptimized node. */
1229 block = optimize_in_place(block);
1235 new_Phi (int arity, ir_node **in, ir_mode *mode)
1237 return new_r_Phi (current_ir_graph, current_ir_graph->current_block,
1242 new_Const (ir_mode *mode, tarval *con)
1244 return new_r_Const (current_ir_graph, current_ir_graph->start_block,
1249 new_Id (ir_node *val, ir_mode *mode)
1251 return new_r_Id (current_ir_graph, current_ir_graph->current_block,
1256 new_Proj (ir_node *arg, ir_mode *mode, long proj)
1258 return new_r_Proj (current_ir_graph, current_ir_graph->current_block,
1263 new_Conv (ir_node *op, ir_mode *mode)
1265 return new_r_Conv (current_ir_graph, current_ir_graph->current_block,
1270 new_Tuple (int arity, ir_node **in)
1272 return new_r_Tuple (current_ir_graph, current_ir_graph->current_block,
1277 new_Add (ir_node *op1, ir_node *op2, ir_mode *mode)
1279 return new_r_Add (current_ir_graph, current_ir_graph->current_block,
1284 new_Sub (ir_node *op1, ir_node *op2, ir_mode *mode)
1286 return new_r_Sub (current_ir_graph, current_ir_graph->current_block,
1292 new_Minus (ir_node *op, ir_mode *mode)
1294 return new_r_Minus (current_ir_graph, current_ir_graph->current_block,
1299 new_Mul (ir_node *op1, ir_node *op2, ir_mode *mode)
1301 return new_r_Mul (current_ir_graph, current_ir_graph->current_block,
1306 new_Quot (ir_node *memop, ir_node *op1, ir_node *op2)
1308 return new_r_Quot (current_ir_graph, current_ir_graph->current_block,
1313 new_DivMod (ir_node *memop, ir_node *op1, ir_node *op2)
1315 return new_r_DivMod (current_ir_graph, current_ir_graph->current_block,
1320 new_Div (ir_node *memop, ir_node *op1, ir_node *op2)
1322 return new_r_Div (current_ir_graph, current_ir_graph->current_block,
1327 new_Mod (ir_node *memop, ir_node *op1, ir_node *op2)
1329 return new_r_Mod (current_ir_graph, current_ir_graph->current_block,
1334 new_And (ir_node *op1, ir_node *op2, ir_mode *mode)
1336 return new_r_And (current_ir_graph, current_ir_graph->current_block,
1341 new_Or (ir_node *op1, ir_node *op2, ir_mode *mode)
1343 return new_r_Or (current_ir_graph, current_ir_graph->current_block,
1348 new_Eor (ir_node *op1, ir_node *op2, ir_mode *mode)
1350 return new_r_Eor (current_ir_graph, current_ir_graph->current_block,
1355 new_Not (ir_node *op, ir_mode *mode)
1357 return new_r_Not (current_ir_graph, current_ir_graph->current_block,
1362 new_Shl (ir_node *op, ir_node *k, ir_mode *mode)
1364 return new_r_Shl (current_ir_graph, current_ir_graph->current_block,
1369 new_Shr (ir_node *op, ir_node *k, ir_mode *mode)
1371 return new_r_Shr (current_ir_graph, current_ir_graph->current_block,
1376 new_Shrs (ir_node *op, ir_node *k, ir_mode *mode)
1378 return new_r_Shrs (current_ir_graph, current_ir_graph->current_block,
1383 new_Rotate (ir_node *op, ir_node *k, ir_mode *mode)
1385 return new_r_Rot (current_ir_graph, current_ir_graph->current_block,
1390 new_Abs (ir_node *op, ir_mode *mode)
1392 return new_r_Abs (current_ir_graph, current_ir_graph->current_block,
1397 new_Cmp (ir_node *op1, ir_node *op2)
1399 return new_r_Cmp (current_ir_graph, current_ir_graph->current_block,
1406 return new_r_Jmp (current_ir_graph, current_ir_graph->current_block);
1410 new_Cond (ir_node *c)
1412 return new_r_Cond (current_ir_graph, current_ir_graph->current_block, c);
1416 new_Call (ir_node *store, ir_node *callee, int arity, ir_node **in,
1419 return new_r_Call (current_ir_graph, current_ir_graph->current_block,
1420 store, callee, arity, in, type);
1424 new_Return (ir_node* store, int arity, ir_node **in)
1426 return new_r_Return (current_ir_graph, current_ir_graph->current_block,
1431 new_Raise (ir_node *store, ir_node *obj)
1433 return new_r_Raise (current_ir_graph, current_ir_graph->current_block,
1438 new_Load (ir_node *store, ir_node *addr)
1440 return new_r_Load (current_ir_graph, current_ir_graph->current_block,
1445 new_Store (ir_node *store, ir_node *addr, ir_node *val)
1447 return new_r_Store (current_ir_graph, current_ir_graph->current_block,
1452 new_Alloc (ir_node *store, ir_node *size, type *alloc_type,
1455 return new_r_Alloc (current_ir_graph, current_ir_graph->current_block,
1456 store, size, alloc_type, where);
1460 new_Free (ir_node *store, ir_node *ptr, ir_node *size, type *free_type)
1462 return new_r_Free (current_ir_graph, current_ir_graph->current_block,
1463 store, ptr, size, free_type);
1467 new_simpleSel (ir_node *store, ir_node *objptr, entity *ent)
1468 /* GL: objptr was called frame before. Frame was a bad choice for the name
1469 as the operand could as well be a pointer to a dynamic object. */
1471 return new_r_Sel (current_ir_graph, current_ir_graph->current_block,
1472 store, objptr, 0, NULL, ent);
1476 new_Sel (ir_node *store, ir_node *objptr, int n_index, ir_node **index, entity *sel)
1478 return new_r_Sel (current_ir_graph, current_ir_graph->current_block,
1479 store, objptr, n_index, index, sel);
1483 new_SymConst (type_or_id_p value, symconst_kind kind)
1485 return new_r_SymConst (current_ir_graph, current_ir_graph->current_block,
1490 new_Sync (int arity, ir_node** in)
1492 return new_r_Sync (current_ir_graph, current_ir_graph->current_block,
1500 return current_ir_graph->bad;
1503 /*************************************************************************/
1504 /* Comfortable interface with automatic Phi node construction. */
1505 /* (Uses also constructors of ?? interface, except new_Block. */
1506 /*************************************************************************/
1508 /** Block construction **/
1509 /* immature Block without predecessors */
1510 ir_node *new_immBlock (void) {
1513 /* creates a new dynamic in-array as length of in is -1 */
1514 res = new_ir_node (current_ir_graph, NULL, op_Block, mode_R, -1, NULL);
1515 current_ir_graph->current_block = res;
1516 res->attr.block.matured = 0;
1517 set_Block_block_visited(res, 0);
1519 /* Create and initialize array for Phi-node construction. */
1520 res->attr.block.graph_arr = NEW_ARR_D (ir_node *, current_ir_graph->obst,
1521 current_ir_graph->n_loc);
1522 memset(res->attr.block.graph_arr, 0, sizeof(ir_node *)*current_ir_graph->n_loc);
1524 /* Immature block may not be optimized! */
1530 /* add an adge to a jmp/control flow node */
1532 add_in_edge (ir_node *block, ir_node *jmp)
1534 if (block->attr.block.matured) {
1535 printf("Error: Block already matured!\n");
1538 assert (jmp != NULL);
1539 ARR_APP1 (ir_node *, block->in, jmp);
1543 /* changing the current block */
1545 switch_block (ir_node *target)
1547 current_ir_graph->current_block = target;
1550 /****************************/
1551 /* parameter administration */
1553 /* get a value from the parameter array from the current block by its index */
1555 get_value (int pos, ir_mode *mode)
1557 inc_irg_visited(current_ir_graph);
1558 return get_r_value_internal (current_ir_graph->current_block, pos + 1, mode);
1561 /* set a value at position pos in the parameter array from the current block */
1563 set_value (int pos, ir_node *value)
1565 current_ir_graph->current_block->attr.block.graph_arr[pos + 1] = value;
1568 /* get the current store */
1572 /* GL: one could call get_value instead */
1573 inc_irg_visited(current_ir_graph);
1574 return get_r_value_internal (current_ir_graph->current_block, 0, mode_M);
1577 /* set the current store */
1579 set_store (ir_node *store)
1581 /* GL: one could call set_value instead */
1582 current_ir_graph->current_block->attr.block.graph_arr[0] = store;
1585 /*************************************************************************/
1588 /* call once for each run of the library */