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
16 # include "irgraph_t.h"
17 # include "irnode_t.h"
18 # include "irmode_t.h"
26 /* memset belongs to string.h */
29 #if USE_EXPICIT_PHI_IN_STACK
30 /* A stack needed for the automatic Phi node construction in constructor
31 Phi_in. Redefinition in irgraph.c!! */
36 typedef struct Phi_in_stack Phi_in_stack;
39 /*** ******************************************** */
40 /** privat interfaces, for professional use only */
42 /* Constructs a Block with a fixed number of predecessors.
43 Does not set current_block. Can not be used with automatic
44 Phi node construction. */
46 new_r_Block (ir_graph *irg, int arity, ir_node **in)
50 res = new_ir_node (irg, NULL, op_Block, mode_R, arity, in);
51 set_Block_matured(res, 1);
52 set_Block_block_visited(res, 0);
59 new_r_Start (ir_graph *irg, ir_node *block)
63 res = new_ir_node (irg, block, op_Start, mode_T, 0, NULL);
70 new_r_End (ir_graph *irg, ir_node *block)
74 res = new_ir_node (irg, block, op_End, mode_X, -1, NULL);
80 /* Creates a Phi node with all predecessors. Calling this constructor
81 is only allowed if the corresponding block is mature. */
83 new_r_Phi (ir_graph *irg, ir_node *block, int arity, ir_node **in, ir_mode *mode)
87 assert( get_Block_matured(block) );
88 assert( get_irn_arity(block) == arity );
90 res = new_ir_node (irg, block, op_Phi, mode, arity, in);
98 new_r_Const (ir_graph *irg, ir_node *block, ir_mode *mode, tarval *con)
101 res = new_ir_node (irg, block, op_Const, mode, 0, NULL);
103 res = optimize (res);
107 res = local_optimize_newby (res);
114 new_r_Id (ir_graph *irg, ir_node *block, ir_node *val, ir_mode *mode)
116 ir_node *in[1] = {val};
118 res = new_ir_node (irg, block, op_Id, mode, 1, in);
119 res = optimize (res);
125 new_r_Proj (ir_graph *irg, ir_node *block, ir_node *arg, ir_mode *mode,
128 ir_node *in[1] = {arg};
130 res = new_ir_node (irg, block, op_Proj, mode, 1, in);
131 res->attr.proj = proj;
134 assert(get_Proj_pred(res));
135 assert(get_nodes_Block(get_Proj_pred(res)));
137 res = optimize (res);
145 new_r_defaultProj (ir_graph *irg, ir_node *block, ir_node *arg,
149 assert((arg->op==op_Cond) && (get_irn_mode(arg->in[1]) == mode_I));
150 arg->attr.c = fragmentary;
151 res = new_r_Proj (irg, block, arg, mode_X, max_proj);
156 new_r_Conv (ir_graph *irg, ir_node *block, ir_node *op, ir_mode *mode)
158 ir_node *in[1] = {op};
160 res = new_ir_node (irg, block, op_Conv, mode, 1, in);
161 res = optimize (res);
168 new_r_Tuple (ir_graph *irg, ir_node *block, int arity, ir_node **in)
172 res = new_ir_node (irg, block, op_Tuple, mode_T, arity, in);
173 res = optimize (res);
179 new_r_Add (ir_graph *irg, ir_node *block,
180 ir_node *op1, ir_node *op2, ir_mode *mode)
182 ir_node *in[2] = {op1, op2};
184 res = new_ir_node (irg, block, op_Add, mode, 2, in);
185 res = optimize (res);
191 new_r_Sub (ir_graph *irg, ir_node *block,
192 ir_node *op1, ir_node *op2, ir_mode *mode)
194 ir_node *in[2] = {op1, op2};
196 res = new_ir_node (irg, block, op_Sub, mode, 2, in);
197 res = optimize (res);
203 new_r_Minus (ir_graph *irg, ir_node *block,
204 ir_node *op, ir_mode *mode)
206 ir_node *in[1] = {op};
208 res = new_ir_node (irg, block, op_Minus, mode, 1, in);
209 res = optimize (res);
215 new_r_Mul (ir_graph *irg, ir_node *block,
216 ir_node *op1, ir_node *op2, ir_mode *mode)
218 ir_node *in[2] = {op1, op2};
220 res = new_ir_node (irg, block, op_Mul, mode, 2, in);
221 res = optimize (res);
227 new_r_Quot (ir_graph *irg, ir_node *block,
228 ir_node *memop, ir_node *op1, ir_node *op2)
230 ir_node *in[3] = {memop, op1, op2};
232 res = new_ir_node (irg, block, op_Quot, mode_T, 3, in);
233 res = optimize (res);
239 new_r_DivMod (ir_graph *irg, ir_node *block,
240 ir_node *memop, ir_node *op1, ir_node *op2)
242 ir_node *in[3] = {memop, op1, op2};
244 res = new_ir_node (irg, block, op_DivMod, mode_T, 3, in);
245 res = optimize (res);
251 new_r_Div (ir_graph *irg, ir_node *block,
252 ir_node *memop, ir_node *op1, ir_node *op2)
254 ir_node *in[3] = {memop, op1, op2};
256 res = new_ir_node (irg, block, op_Div, mode_T, 3, in);
257 res = optimize (res);
263 new_r_Mod (ir_graph *irg, ir_node *block,
264 ir_node *memop, ir_node *op1, ir_node *op2)
266 ir_node *in[3] = {memop, op1, op2};
268 res = new_ir_node (irg, block, op_Mod, mode_T, 3, in);
269 res = optimize (res);
275 new_r_And (ir_graph *irg, ir_node *block,
276 ir_node *op1, ir_node *op2, ir_mode *mode)
278 ir_node *in[2] = {op1, op2};
280 res = new_ir_node (irg, block, op_And, mode, 2, in);
281 res = optimize (res);
287 new_r_Or (ir_graph *irg, ir_node *block,
288 ir_node *op1, ir_node *op2, ir_mode *mode)
290 ir_node *in[2] = {op1, op2};
292 res = new_ir_node (irg, block, op_Or, mode, 2, in);
293 res = optimize (res);
299 new_r_Eor (ir_graph *irg, ir_node *block,
300 ir_node *op1, ir_node *op2, ir_mode *mode)
302 ir_node *in[2] = {op1, op2};
304 res = new_ir_node (irg, block, op_Eor, mode, 2, in);
305 res = optimize (res);
311 new_r_Not (ir_graph *irg, ir_node *block,
312 ir_node *op, ir_mode *mode)
314 ir_node *in[1] = {op};
316 res = new_ir_node (irg, block, op_Not, mode, 1, in);
317 res = optimize (res);
323 new_r_Shl (ir_graph *irg, ir_node *block,
324 ir_node *op, ir_node *k, ir_mode *mode)
326 ir_node *in[2] = {op, k};
328 res = new_ir_node (irg, block, op_Shl, mode, 2, in);
329 res = optimize (res);
335 new_r_Shr (ir_graph *irg, ir_node *block,
336 ir_node *op, ir_node *k, ir_mode *mode)
338 ir_node *in[2] = {op, k};
340 res = new_ir_node (irg, block, op_Shr, mode, 2, in);
341 res = optimize (res);
347 new_r_Shrs (ir_graph *irg, ir_node *block,
348 ir_node *op, ir_node *k, ir_mode *mode)
350 ir_node *in[2] = {op, k};
352 res = new_ir_node (irg, block, op_Shrs, mode, 2, in);
353 res = optimize (res);
359 new_r_Rot (ir_graph *irg, ir_node *block,
360 ir_node *op, ir_node *k, ir_mode *mode)
362 ir_node *in[2] = {op, k};
364 res = new_ir_node (irg, block, op_Rot, mode, 2, in);
365 res = optimize (res);
371 new_r_Abs (ir_graph *irg, ir_node *block,
372 ir_node *op, ir_mode *mode)
374 ir_node *in[1] = {op};
376 res = new_ir_node (irg, block, op_Abs, mode, 1, in);
377 res = optimize (res);
383 new_r_Cmp (ir_graph *irg, ir_node *block,
384 ir_node *op1, ir_node *op2)
386 ir_node *in[2] = {op1, op2};
388 res = new_ir_node (irg, block, op_Cmp, mode_T, 2, in);
389 res = optimize (res);
395 new_r_Jmp (ir_graph *irg, ir_node *block)
399 res = new_ir_node (irg, block, op_Jmp, mode_X, 0, in);
400 res = optimize (res);
406 new_r_Cond (ir_graph *irg, ir_node *block, ir_node *c)
408 ir_node *in[1] = {c};
410 res = new_ir_node (irg, block, op_Cond, mode_T, 1, in);
412 res = optimize (res);
418 new_r_Call (ir_graph *irg, ir_node *block, ir_node *store,
419 ir_node *callee, int arity, ir_node **in, type *type)
426 NEW_ARR_A (ir_node *, r_in, r_arity);
429 memcpy (&r_in[2], in, sizeof (ir_node *) * arity);
431 res = new_ir_node (irg, block, op_Call, mode_T, r_arity, r_in);
433 assert(is_method_type(type));
434 set_Call_type(res, type);
435 res = optimize (res);
441 new_r_Return (ir_graph *irg, ir_node *block,
442 ir_node *store, int arity, ir_node **in)
449 NEW_ARR_A (ir_node *, r_in, r_arity);
451 memcpy (&r_in[1], in, sizeof (ir_node *) * arity);
452 res = new_ir_node (irg, block, op_Return, mode_X, r_arity, r_in);
453 res = optimize (res);
459 new_r_Raise (ir_graph *irg, ir_node *block, ir_node *store, ir_node *obj)
461 ir_node *in[2] = {store, obj};
463 res = new_ir_node (irg, block, op_Raise, mode_X, 2, in);
465 res = optimize (res);
471 new_r_Load (ir_graph *irg, ir_node *block,
472 ir_node *store, ir_node *adr)
474 ir_node *in[2] = {store, adr};
476 res = new_ir_node (irg, block, op_Load, mode_T, 2, in);
478 res = optimize (res);
484 new_r_Store (ir_graph *irg, ir_node *block,
485 ir_node *store, ir_node *adr, ir_node *val)
487 ir_node *in[3] = {store, adr, val};
489 res = new_ir_node (irg, block, op_Store, mode_T, 3, in);
491 res = optimize (res);
497 new_r_Alloc (ir_graph *irg, ir_node *block, ir_node *store,
498 ir_node *size, type *alloc_type, where_alloc where)
500 ir_node *in[2] = {store, size};
502 res = new_ir_node (irg, block, op_Alloc, mode_T, 2, in);
504 res->attr.a.where = where;
505 res->attr.a.type = alloc_type;
507 res = optimize (res);
513 new_r_Free (ir_graph *irg, ir_node *block, ir_node *store,
514 ir_node *ptr, ir_node *size, type *free_type)
516 ir_node *in[3] = {store, ptr, size};
518 res = new_ir_node (irg, block, op_Free, mode_T, 3, in);
520 res->attr.f = free_type;
522 res = optimize (res);
528 new_r_Sel (ir_graph *irg, ir_node *block, ir_node *store, ir_node *objptr,
529 int arity, ir_node **in, entity *ent)
536 NEW_ARR_A (ir_node *, r_in, r_arity);
539 memcpy (&r_in[2], in, sizeof (ir_node *) * arity);
540 res = new_ir_node (irg, block, op_Sel, mode_p, r_arity, r_in);
542 res->attr.s.ltyp = static_linkage;
543 res->attr.s.ent = ent;
545 res = optimize (res);
551 new_r_SymConst (ir_graph *irg, ir_node *block, type_or_id_p value,
552 symconst_kind symkind)
557 if (symkind == linkage_ptr_info)
561 res = new_ir_node (irg, block, op_SymConst, mode, 0, in);
563 res->attr.i.num = symkind;
564 if (symkind == linkage_ptr_info) {
565 res->attr.i.tori.ptrinfo = (ident *)value;
567 assert ( ( (symkind == type_tag)
568 || (symkind == size))
569 && (is_type(value)));
570 res->attr.i.tori.typ = (type *)value;
572 res = optimize (res);
578 new_r_Sync (ir_graph *irg, ir_node *block, int arity, ir_node **in)
582 res = new_ir_node (irg, block, op_Sync, mode_M, arity, in);
584 res = optimize (res);
592 return current_ir_graph->bad;
595 /** ********************/
596 /** public interfaces */
597 /** construction tools */
599 /****f* ircons/new_Start
602 * new_Start -- create a new Start node in the current block
605 * s = new_Start(void);
606 * ir_node* new_Start(void);
609 * s - pointer to the created Start node
618 res = new_ir_node (current_ir_graph, current_ir_graph->current_block,
619 op_Start, mode_T, 0, NULL);
621 res = optimize (res);
631 res = new_ir_node (current_ir_graph, current_ir_graph->current_block,
632 op_End, mode_X, -1, NULL);
634 res = optimize (res);
640 /* Constructs a Block with a fixed number of predecessors.
641 Does set current_block. Can be used with automatic Phi
642 node construction. */
644 new_Block (int arity, ir_node **in)
648 res = new_r_Block (current_ir_graph, arity, in);
649 current_ir_graph->current_block = res;
651 /* Create and initialize array for Phi-node construction. */
652 res->attr.block.graph_arr = NEW_ARR_D (ir_node *, current_ir_graph->obst,
653 current_ir_graph->n_loc);
654 memset(res->attr.block.graph_arr, 0, sizeof(ir_node *)*current_ir_graph->n_loc);
656 res = optimize (res);
662 /* ***********************************************************************/
663 /* Methods necessary for automatic Phi node creation */
665 ir_node *phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
666 ir_node *get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
667 ir_node *new_r_Phi0 (ir_graph *irg, ir_node *block, ir_mode *mode)
668 ir_node *new_r_Phi_in (ir_graph *irg, ir_node *block, ir_mode *mode, ir_node **in, int ins)
670 Call Graph: ( A ---> B == A "calls" B)
672 get_value mature_block
680 get_r_value_internal |
684 new_r_Phi0 new_r_Phi_in
686 * *************************************************************************** */
688 /* Creates a Phi node with 0 predecessors */
690 new_r_Phi0 (ir_graph *irg, ir_node *block, ir_mode *mode)
693 res = new_ir_node (irg, block, op_Phi, mode, 0, NULL);
698 /* There are two implementations of the Phi node construction. The first
699 is faster, but does not work for blocks with more than 2 predecessors.
700 The second works always but is slower and causes more unnecessary Phi
702 Select the implementations by the following preprocessor flag set in
704 #if USE_FAST_PHI_CONSTRUCTION
706 /* This is a stack used for allocating and deallocating nodes in
707 new_r_Phi_in. The original implementation used the obstack
708 to model this stack, now it is explicit. This reduces side effects.
710 #if USE_EXPICIT_PHI_IN_STACK
715 res = (Phi_in_stack *) malloc ( sizeof (Phi_in_stack));
717 res->stack = NEW_ARR_F (ir_node *, 1);
723 void free_to_Phi_in_stack(ir_node *phi) {
724 assert(get_irn_opcode(phi) == iro_Phi);
726 if (ARR_LEN(current_ir_graph->Phi_in_stack->stack) ==
727 current_ir_graph->Phi_in_stack->pos)
728 ARR_APP1 (ir_node *, current_ir_graph->Phi_in_stack->stack, phi);
730 current_ir_graph->Phi_in_stack->stack[current_ir_graph->Phi_in_stack->pos] = phi;
732 (current_ir_graph->Phi_in_stack->pos)++;
736 alloc_or_pop_from_Phi_in_stack(ir_graph *irg, ir_node *block, ir_mode *mode,
737 int arity, ir_node **in) {
739 ir_node **stack = current_ir_graph->Phi_in_stack->stack;
740 int pos = current_ir_graph->Phi_in_stack->pos;
744 /* We need to allocate a new node */
745 res = new_ir_node (irg, block, op_Phi, mode, arity, in);
747 /* reuse the old node and initialize it again. */
750 assert (res->kind == k_ir_node);
751 assert (res->op == op_Phi);
756 /* ???!!! How to free the old in array?? */
757 res->in = NEW_ARR_D (ir_node *, irg->obst, (arity+1));
759 memcpy (&res->in[1], in, sizeof (ir_node *) * arity);
761 (current_ir_graph->Phi_in_stack->pos)--;
765 #endif /* USE_EXPICIT_PHI_IN_STACK */
767 /* Creates a Phi node with a given, fixed array **in of predecessors.
768 If the Phi node is unnecessary, as the same value reaches the block
769 through all control flow paths, it is eliminated and the value
770 returned directly. This constructor is only intended for use in
771 the automatic Phi node generation triggered by get_value or mature.
772 The implementation is quite tricky and depends on the fact, that
773 the nodes are allocated on a stack:
774 The in array contains predecessors and NULLs. The NULLs appear,
775 if get_r_value_internal, that computed the predecessors, reached
776 the same block on two paths. In this case the same value reaches
777 this block on both paths, there is no definition in between. We need
778 not allocate a Phi where these path's merge, but we have to communicate
779 this fact to the caller. This happens by returning a pointer to the
780 node the caller _will_ allocate. (Yes, we predict the address. We can
781 do so because the nodes are allocated on the obstack.) The caller then
782 finds a pointer to itself and, when this routine is called again,
786 new_r_Phi_in (ir_graph *irg, ir_node *block, ir_mode *mode,
787 ir_node **in, int ins)
790 ir_node *res, *known;
792 /* allocate a new node on the obstack.
793 This can return a node to which some of the pointers in the in-array
795 Attention: the constructor copies the in array, i.e., the later changes
796 to the array in this routine do not affect the constructed node! If
797 the in array contains NULLs, there will be missing predecessors in the
799 Is this a possible internal state of the Phi node generation? */
800 #if USE_EXPICIT_PHI_IN_STACK
801 res = known = alloc_or_pop_from_Phi_in_stack(irg, block, mode, ins, in);
803 res = known = new_ir_node (irg, block, op_Phi, mode, ins, in);
805 /* The in-array can contain NULLs. These were returned by
806 get_r_value_internal if it reached the same block/definition on a
808 The NULLs are replaced by the node itself to simplify the test in the
810 for (i=0; i < ins; ++i)
811 if (in[i] == NULL) in[i] = res;
813 /* This loop checks whether the Phi has more than one predecessor.
814 If so, it is a real Phi node and we break the loop. Else the
815 Phi node merges the same definition on several paths and therefore
817 for (i=0; i < ins; ++i)
819 if (in[i]==res || in[i]==known) continue;
827 /* i==ins: there is at most one predecessor, we don't need a phi node. */
829 #if USE_EXPICIT_PHI_IN_STACK
830 free_to_Phi_in_stack(res);
832 obstack_free (current_ir_graph->obst, res);
836 res = optimize (res);
840 /* return the pointer to the Phi node. This node might be deallocated! */
845 get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
847 /** This function computes the predecessors for a real Phi node, and then
848 allocates and returns this node. The routine called to allocate the
849 node might optimize it away and return a real value, or even a pointer
850 to a deallocated Phi node on top of the obstack!
851 This function is called with an in-array of proper size. **/
852 static inline ir_node *
853 phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
855 ir_node *prevBlock, *res;
858 /* This loop goes to all predecessor blocks of the block the Phi node is in
859 and there finds the operands of the Phi node by calling
860 get_r_value_internal. */
861 for (i = 1; i <= ins; ++i) {
862 assert (block->in[i]);
863 prevBlock = block->in[i]->in[0]; /* go past control flow op to prev block */
865 nin[i-1] = get_r_value_internal (prevBlock, pos, mode);
868 /* After collecting all predecessors into the array nin a new Phi node
869 with these predecessors is created. This constructor contains an
870 optimization: If all predecessors of the Phi node are identical it
871 returns the only operand instead of a new Phi node. If the value
872 passes two different control flow edges without being defined, and
873 this is the second path treated, a pointer to the node that will be
874 allocated for the first path (recursion) is returned. We already
875 know the address of this node, as it is the next node to be allocated
876 and will be placed on top of the obstack. (The obstack is a _stack_!) */
877 res = new_r_Phi_in (current_ir_graph, block, mode, nin, ins);
879 /* Now we now the value for "pos" and can enter it in the array with
880 all known local variables. Attention: this might be a pointer to
881 a node, that later will be allocated!!! See new_r_Phi_in.
882 If this is called in mature, after some set_value in the same block,
883 the proper value must not be overwritten:
885 get_value (makes Phi0, put's it into graph_arr)
886 set_value (overwrites Phi0 in graph_arr)
887 mature_block (upgrades Phi0, puts it again into graph_arr, overwriting
890 if (!block->attr.block.graph_arr[pos]) {
891 block->attr.block.graph_arr[pos] = res;
893 /* printf(" value already computed by %s\n",
894 id_to_str(block->attr.block.graph_arr[pos]->op->name)); */
900 /* This function returns the last definition of a variable. In case
901 this variable was last defined in a previous block, Phi nodes are
902 inserted. If the part of the firm graph containing the definition
903 is not yet constructed, a dummy Phi node is returned. */
905 get_r_value_internal (ir_node *block, int pos, ir_mode *mode)
908 /* There are 4 cases to treat.
910 1. The block is not mature and we visit it the first time. We can not
911 create a proper Phi node, therefore a Phi0, i.e., a Phi without
912 predecessors is returned. This node is added to the linked list (field
913 "link") of the containing block to be completed when this block is
914 matured. (Comlpletion will add a new Phi and turn the Phi0 into an Id
917 2. The value is already known in this block, graph_arr[pos] is set and we
918 visit the block the first time. We can return the value without
919 creating any new nodes.
921 3. The block is mature and we visit it the first time. A Phi node needs
922 to be created (phi_merge). If the Phi is not needed, as all it's
923 operands are the same value reaching the block through different
924 paths, it's optimized away and the value itself is returned.
926 4. The block is mature, and we visit it the second time. Now two
927 subcases are possible:
928 * The value was computed completely the last time we were here. This
929 is the case if there is no loop. We can return the proper value.
930 * The recursion that visited this node and set the flag did not
931 return yet. We are computing a value in a loop and need to
932 break the recursion without knowing the result yet.
933 @@@ strange case. Straight forward we would create a Phi before
934 starting the computation of it's predecessors. In this case we will
935 find a Phi here in any case. The problem is that this implementation
936 only creates a Phi after computing the predecessors, so that it is
937 hard to compute self references of this Phi. @@@
938 There is no simple check for the second subcase. Therefore we check
939 for a second visit and treat all such cases as the second subcase.
940 Anyways, the basic situation is the same: we reached a block
941 on two paths without finding a definition of the value: No Phi
942 nodes are needed on both paths.
943 We return this information "Two paths, no Phi needed" by a very tricky
944 implementation that relies on the fact that an obstack is a stack and
945 will return a node with the same address on different allocations.
946 Look also at phi_merge and new_r_phi_in to understand this.
947 @@@ Unfortunately this does not work, see testprogram
948 three_cfpred_example.
952 /* case 4 -- already visited. */
953 if (get_irn_visited(block) == get_irg_visited(current_ir_graph)) return NULL;
955 /* visited the first time */
956 set_irn_visited(block, get_irg_visited(current_ir_graph));
958 /* Get the local valid value */
959 res = block->attr.block.graph_arr[pos];
961 /* case 2 -- If the value is actually computed, return it. */
962 if (res) { return res;};
964 if (block->attr.block.matured) { /* case 3 */
966 /* The Phi has the same amount of ins as the corresponding block. */
967 int ins = get_irn_arity(block);
969 NEW_ARR_A (ir_node *, nin, ins);
971 /* Phi merge collects the predecessors and then creates a node. */
972 res = phi_merge (block, pos, mode, nin, ins);
974 } else { /* case 1 */
975 /* The block is not mature, we don't know how many in's are needed. A Phi
976 with zero predecessors is created. Such a Phi node is called Phi0
977 node. (There is also an obsolete Phi0 opcode.) The Phi0 is then added
978 to the list of Phi0 nodes in this block to be matured by mature_block
980 The Phi0 has to remember the pos of it's internal value. If the real
981 Phi is computed, pos is used to update the array with the local
984 res = new_r_Phi0 (current_ir_graph, block, mode);
985 res->attr.phi0_pos = pos;
986 res->link = block->link;
990 /* If we get here, the frontend missed a use-before-definition error */
993 printf("Error: no value set. Use of undefined variable. Initializing
995 assert (mode->code >= irm_f && mode->code <= irm_p);
996 res = new_r_Const (current_ir_graph, block, mode,
997 tarval_mode_null[mode->code]);
1000 /* The local valid value is available now. */
1001 block->attr.block.graph_arr[pos] = res;
1008 /** This is the simple algorithm. If first generates a Phi0, then
1009 it starts the recursion. This causes an Id at the entry of
1010 every block that has no definition of the value! **/
1012 #if USE_EXPICIT_PHI_IN_STACK
1014 Phi_in_stack * new_Phi_in_stack() { return NULL; }
1018 new_r_Phi_in (ir_graph *irg, ir_node *block, ir_mode *mode,
1019 ir_node **in, int ins)
1022 ir_node *res, *known;
1024 /* Allocate a new node on the obstack. The allocation copies the in
1026 res = new_ir_node (irg, block, op_Phi, mode, ins, in);
1028 /* This loop checks whether the Phi has more than one predecessor.
1029 If so, it is a real Phi node and we break the loop. Else the
1030 Phi node merges the same definition on several paths and therefore
1031 is not needed. Don't consider Bad nodes! */
1033 for (i=0; i < ins; ++i)
1035 if (in[i]==res || in[i]==known || is_Bad(in[i])) continue;
1043 /* i==ins: there is at most one predecessor, we don't need a phi node. */
1046 obstack_free (current_ir_graph->obst, res);
1049 /* A undefined value, e.g., in unreachable code. */
1053 res = optimize (res);
1061 get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
1063 /** This function allocates a dummy Phi node to break recursions,
1064 computes the predecessors for the real phi node, and then
1065 allocates and returns this node. The routine called to allocate the
1066 node might optimize it away and return a real value.
1067 This function is called with an in-array of proper size. **/
1068 static inline ir_node *
1069 phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
1071 ir_node *prevBlock, *res, *phi0;
1075 /* If this block has no value at pos create a Phi0 and remember it
1076 in graph_arr to break recursions. */
1078 if (!block->attr.block.graph_arr[pos]) {
1079 /* This is commented out as collapsing to Bads is no good idea.
1080 Either we need an assert here, or we need to call a routine
1081 that deals with this case as appropriate for the given language.
1082 Right now a self referencing Id is created which will crash irg_vryfy().
1084 Even if all variables are defined before use, it can happen that
1085 we get to the start block, if a cond has been replaced by a tuple
1086 (bad, jmp). As the start has a self referencing control flow edge,
1087 we get a self referencing Id, which is hard to optimize away. We avoid
1088 this by defining the value as a Bad node.
1089 Returning a const with tarval_bad is a preliminary solution. In some
1090 situations we might want a Warning or an Error. */
1092 if (block == get_irg_start_block(current_ir_graph)) {
1093 block->attr.block.graph_arr[pos] = new_Const(mode, tarval_bad);
1094 return block->attr.block.graph_arr[pos];
1096 phi0 = new_r_Phi0(current_ir_graph, block, mode);
1097 block->attr.block.graph_arr[pos] = phi0;
1101 /* This loop goes to all predecessor blocks of the block the Phi node
1102 is in and there finds the operands of the Phi node by calling
1103 get_r_value_internal. */
1104 for (i = 1; i <= ins; ++i) {
1105 assert (block->in[i]);
1106 if (is_Bad(block->in[i])) {
1107 /* In case a Cond has been optimized we would get right to the start block
1108 with an invalid definition. */
1109 nin[i-1] = new_Bad();
1112 prevBlock = block->in[i]->in[0]; /* go past control flow op to prev block */
1114 if (!is_Bad(prevBlock)) {
1115 nin[i-1] = get_r_value_internal (prevBlock, pos, mode);
1117 nin[i-1] = new_Bad();
1121 /* After collecting all predecessors into the array nin a new Phi node
1122 with these predecessors is created. This constructor contains an
1123 optimization: If all predecessors of the Phi node are identical it
1124 returns the only operand instead of a new Phi node. */
1125 res = new_r_Phi_in (current_ir_graph, block, mode, nin, ins);
1127 /* In case we allocated a Phi0 node at the beginning of this procedure,
1128 we need to exchange this Phi0 with the real Phi. */
1130 exchange(phi0, res);
1131 block->attr.block.graph_arr[pos] = res;
1137 /* This function returns the last definition of a variable. In case
1138 this variable was last defined in a previous block, Phi nodes are
1139 inserted. If the part of the firm graph containing the definition
1140 is not yet constructed, a dummy Phi node is returned. */
1142 get_r_value_internal (ir_node *block, int pos, ir_mode *mode)
1145 /* There are 4 cases to treat.
1147 1. The block is not mature and we visit it the first time. We can not
1148 create a proper Phi node, therefore a Phi0, i.e., a Phi without
1149 predecessors is returned. This node is added to the linked list (field
1150 "link") of the containing block to be completed when this block is
1151 matured. (Comlpletion will add a new Phi and turn the Phi0 into an Id
1154 2. The value is already known in this block, graph_arr[pos] is set and we
1155 visit the block the first time. We can return the value without
1156 creating any new nodes.
1158 3. The block is mature and we visit it the first time. A Phi node needs
1159 to be created (phi_merge). If the Phi is not needed, as all it's
1160 operands are the same value reaching the block through different
1161 paths, it's optimized away and the value itself is returned.
1163 4. The block is mature, and we visit it the second time. Now two
1164 subcases are possible:
1165 * The value was computed completely the last time we were here. This
1166 is the case if there is no loop. We can return the proper value.
1167 * The recursion that visited this node and set the flag did not
1168 return yet. We are computing a value in a loop and need to
1169 break the recursion. This case only happens if we visited
1170 the same block with phi_merge before, which inserted a Phi0.
1171 So we return the Phi0.
1174 /* case 4 -- already visited. */
1175 if (get_irn_visited(block) == get_irg_visited(current_ir_graph)) {
1176 /* As phi_merge allocates a Phi0 this value is always defined. Here
1177 is the critical difference of the two algorithms. */
1178 assert(block->attr.block.graph_arr[pos]);
1179 return block->attr.block.graph_arr[pos];
1182 /* visited the first time */
1183 set_irn_visited(block, get_irg_visited(current_ir_graph));
1185 /* Get the local valid value */
1186 res = block->attr.block.graph_arr[pos];
1188 /* case 2 -- If the value is actually computed, return it. */
1189 if (res) { return res; };
1191 if (block->attr.block.matured) { /* case 3 */
1193 /* The Phi has the same amount of ins as the corresponding block. */
1194 int ins = get_irn_arity(block);
1196 NEW_ARR_A (ir_node *, nin, ins);
1198 /* Phi merge collects the predecessors and then creates a node. */
1199 res = phi_merge (block, pos, mode, nin, ins);
1201 } else { /* case 1 */
1202 /* The block is not mature, we don't know how many in's are needed. A Phi
1203 with zero predecessors is created. Such a Phi node is called Phi0
1204 node. The Phi0 is then added to the list of Phi0 nodes in this block
1205 to be matured by mature_block later.
1206 The Phi0 has to remember the pos of it's internal value. If the real
1207 Phi is computed, pos is used to update the array with the local
1209 res = new_r_Phi0 (current_ir_graph, block, mode);
1210 res->attr.phi0_pos = pos;
1211 res->link = block->link;
1215 /* If we get here, the frontend missed a use-before-definition error */
1218 printf("Error: no value set. Use of undefined variable. Initializing
1220 assert (mode->code >= irm_f && mode->code <= irm_p);
1221 res = new_r_Const (current_ir_graph, block, mode,
1222 tarval_mode_null[mode->code]);
1225 /* The local valid value is available now. */
1226 block->attr.block.graph_arr[pos] = res;
1231 #endif /* USE_FAST_PHI_CONSTRUCTION */
1233 /* ************************************************************************** */
1235 /** Finalize a Block node, when all control flows are known. */
1236 /** Acceptable parameters are only Block nodes. */
1238 mature_block (ir_node *block)
1245 assert (get_irn_opcode(block) == iro_Block);
1247 if (!get_Block_matured(block)) {
1249 /* turn the dynamic in-array into a static one. */
1250 ins = ARR_LEN (block->in)-1;
1251 NEW_ARR_A (ir_node *, nin, ins);
1252 /* @@@ something is strange here... why isn't the array copied? */
1254 /* Traverse a chain of Phi nodes attached to this block and mature
1256 for (n = block->link; n; n=next) {
1257 inc_irg_visited(current_ir_graph);
1259 exchange (n, phi_merge (block, n->attr.phi0_pos, n->mode, nin, ins));
1262 block->attr.block.matured = 1;
1264 /* Now, as the block is a finished firm node, we can optimize it.
1265 Since other nodes have been allocated since the block was created
1266 we can not free the node on the obstack. Therefore we have to call
1268 Unfortunately the optimization does not change a lot, as all allocated
1269 nodes refer to the unoptimized node. */
1270 block = optimize_in_place(block);
1276 new_Phi (int arity, ir_node **in, ir_mode *mode)
1278 return new_r_Phi (current_ir_graph, current_ir_graph->current_block,
1283 new_Const (ir_mode *mode, tarval *con)
1285 return new_r_Const (current_ir_graph, current_ir_graph->start_block,
1290 new_Id (ir_node *val, ir_mode *mode)
1292 return new_r_Id (current_ir_graph, current_ir_graph->current_block,
1297 new_Proj (ir_node *arg, ir_mode *mode, long proj)
1299 return new_r_Proj (current_ir_graph, current_ir_graph->current_block,
1304 new_defaultProj (ir_node *arg, long max_proj)
1307 assert((arg->op==op_Cond) && (get_irn_mode(arg->in[1]) == mode_I));
1308 arg->attr.c = fragmentary;
1309 res = new_Proj (arg, mode_X, max_proj);
1314 new_Conv (ir_node *op, ir_mode *mode)
1316 return new_r_Conv (current_ir_graph, current_ir_graph->current_block,
1321 new_Tuple (int arity, ir_node **in)
1323 return new_r_Tuple (current_ir_graph, current_ir_graph->current_block,
1328 new_Add (ir_node *op1, ir_node *op2, ir_mode *mode)
1330 return new_r_Add (current_ir_graph, current_ir_graph->current_block,
1335 new_Sub (ir_node *op1, ir_node *op2, ir_mode *mode)
1337 return new_r_Sub (current_ir_graph, current_ir_graph->current_block,
1343 new_Minus (ir_node *op, ir_mode *mode)
1345 return new_r_Minus (current_ir_graph, current_ir_graph->current_block,
1350 new_Mul (ir_node *op1, ir_node *op2, ir_mode *mode)
1352 return new_r_Mul (current_ir_graph, current_ir_graph->current_block,
1357 new_Quot (ir_node *memop, ir_node *op1, ir_node *op2)
1359 return new_r_Quot (current_ir_graph, current_ir_graph->current_block,
1364 new_DivMod (ir_node *memop, ir_node *op1, ir_node *op2)
1366 return new_r_DivMod (current_ir_graph, current_ir_graph->current_block,
1371 new_Div (ir_node *memop, ir_node *op1, ir_node *op2)
1373 return new_r_Div (current_ir_graph, current_ir_graph->current_block,
1378 new_Mod (ir_node *memop, ir_node *op1, ir_node *op2)
1380 return new_r_Mod (current_ir_graph, current_ir_graph->current_block,
1385 new_And (ir_node *op1, ir_node *op2, ir_mode *mode)
1387 return new_r_And (current_ir_graph, current_ir_graph->current_block,
1392 new_Or (ir_node *op1, ir_node *op2, ir_mode *mode)
1394 return new_r_Or (current_ir_graph, current_ir_graph->current_block,
1399 new_Eor (ir_node *op1, ir_node *op2, ir_mode *mode)
1401 return new_r_Eor (current_ir_graph, current_ir_graph->current_block,
1406 new_Not (ir_node *op, ir_mode *mode)
1408 return new_r_Not (current_ir_graph, current_ir_graph->current_block,
1413 new_Shl (ir_node *op, ir_node *k, ir_mode *mode)
1415 return new_r_Shl (current_ir_graph, current_ir_graph->current_block,
1420 new_Shr (ir_node *op, ir_node *k, ir_mode *mode)
1422 return new_r_Shr (current_ir_graph, current_ir_graph->current_block,
1427 new_Shrs (ir_node *op, ir_node *k, ir_mode *mode)
1429 return new_r_Shrs (current_ir_graph, current_ir_graph->current_block,
1434 new_Rotate (ir_node *op, ir_node *k, ir_mode *mode)
1436 return new_r_Rot (current_ir_graph, current_ir_graph->current_block,
1441 new_Abs (ir_node *op, ir_mode *mode)
1443 return new_r_Abs (current_ir_graph, current_ir_graph->current_block,
1448 new_Cmp (ir_node *op1, ir_node *op2)
1450 return new_r_Cmp (current_ir_graph, current_ir_graph->current_block,
1457 return new_r_Jmp (current_ir_graph, current_ir_graph->current_block);
1461 new_Cond (ir_node *c)
1463 return new_r_Cond (current_ir_graph, current_ir_graph->current_block, c);
1467 new_Call (ir_node *store, ir_node *callee, int arity, ir_node **in,
1470 return new_r_Call (current_ir_graph, current_ir_graph->current_block,
1471 store, callee, arity, in, type);
1475 new_Return (ir_node* store, int arity, ir_node **in)
1477 return new_r_Return (current_ir_graph, current_ir_graph->current_block,
1482 new_Raise (ir_node *store, ir_node *obj)
1484 return new_r_Raise (current_ir_graph, current_ir_graph->current_block,
1489 new_Load (ir_node *store, ir_node *addr)
1491 return new_r_Load (current_ir_graph, current_ir_graph->current_block,
1496 new_Store (ir_node *store, ir_node *addr, ir_node *val)
1498 return new_r_Store (current_ir_graph, current_ir_graph->current_block,
1503 new_Alloc (ir_node *store, ir_node *size, type *alloc_type,
1506 return new_r_Alloc (current_ir_graph, current_ir_graph->current_block,
1507 store, size, alloc_type, where);
1511 new_Free (ir_node *store, ir_node *ptr, ir_node *size, type *free_type)
1513 return new_r_Free (current_ir_graph, current_ir_graph->current_block,
1514 store, ptr, size, free_type);
1518 new_simpleSel (ir_node *store, ir_node *objptr, entity *ent)
1519 /* GL: objptr was called frame before. Frame was a bad choice for the name
1520 as the operand could as well be a pointer to a dynamic object. */
1522 return new_r_Sel (current_ir_graph, current_ir_graph->current_block,
1523 store, objptr, 0, NULL, ent);
1527 new_Sel (ir_node *store, ir_node *objptr, int n_index, ir_node **index, entity *sel)
1529 return new_r_Sel (current_ir_graph, current_ir_graph->current_block,
1530 store, objptr, n_index, index, sel);
1534 new_SymConst (type_or_id_p value, symconst_kind kind)
1536 return new_r_SymConst (current_ir_graph, current_ir_graph->current_block,
1541 new_Sync (int arity, ir_node** in)
1543 return new_r_Sync (current_ir_graph, current_ir_graph->current_block,
1551 return current_ir_graph->bad;
1554 /* ********************************************************************* */
1555 /* Comfortable interface with automatic Phi node construction. */
1556 /* (Uses also constructors of ?? interface, except new_Block. */
1557 /* ********************************************************************* */
1559 /** Block construction **/
1560 /* immature Block without predecessors */
1561 ir_node *new_immBlock (void) {
1564 /* creates a new dynamic in-array as length of in is -1 */
1565 res = new_ir_node (current_ir_graph, NULL, op_Block, mode_R, -1, NULL);
1566 current_ir_graph->current_block = res;
1567 res->attr.block.matured = 0;
1568 set_Block_block_visited(res, 0);
1570 /* Create and initialize array for Phi-node construction. */
1571 res->attr.block.graph_arr = NEW_ARR_D (ir_node *, current_ir_graph->obst,
1572 current_ir_graph->n_loc);
1573 memset(res->attr.block.graph_arr, 0, sizeof(ir_node *)*current_ir_graph->n_loc);
1575 /* Immature block may not be optimized! */
1581 /* add an adge to a jmp/control flow node */
1583 add_in_edge (ir_node *block, ir_node *jmp)
1585 if (block->attr.block.matured) {
1586 printf("Error: Block already matured!\n");
1589 assert (jmp != NULL);
1590 ARR_APP1 (ir_node *, block->in, jmp);
1594 /* changing the current block */
1596 switch_block (ir_node *target)
1598 current_ir_graph->current_block = target;
1601 /* ************************ */
1602 /* parameter administration */
1604 /* get a value from the parameter array from the current block by its index */
1606 get_value (int pos, ir_mode *mode)
1608 inc_irg_visited(current_ir_graph);
1609 return get_r_value_internal (current_ir_graph->current_block, pos + 1, mode);
1613 /* set a value at position pos in the parameter array from the current block */
1615 set_value (int pos, ir_node *value)
1617 current_ir_graph->current_block->attr.block.graph_arr[pos + 1] = value;
1620 /* get the current store */
1624 /* GL: one could call get_value instead */
1625 inc_irg_visited(current_ir_graph);
1626 return get_r_value_internal (current_ir_graph->current_block, 0, mode_M);
1629 /* set the current store */
1631 set_store (ir_node *store)
1633 /* GL: one could call set_value instead */
1634 current_ir_graph->current_block->attr.block.graph_arr[0] = store;
1637 /* ********************************************************************* */
1640 /* call once for each run of the library */