c6436cfda8104f1746b56d6b19230808e2f40f0f
[libfirm] / ir / ir / ircons.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Various irnode constructors. Automatic construction of SSA
23  *          representation.
24  * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Boris Boesler
25  *          Michael Beck, Matthias Braun
26  * @version $Id$
27  */
28 #include "config.h"
29
30 #include "irprog_t.h"
31 #include "irgraph_t.h"
32 #include "irnode_t.h"
33 #include "irmode_t.h"
34 #include "ircons_t.h"
35 #include "irverify.h"
36 #include "irop_t.h"
37 #include "iropt_t.h"
38 #include "irgmod.h"
39 #include "irhooks.h"
40 #include "array_t.h"
41 #include "irbackedge_t.h"
42 #include "irflag_t.h"
43 #include "iredges_t.h"
44 #include "irflag_t.h"
45 #include "error.h"
46
47 #include "gen_ir_cons.c.inl"
48
49 /**
50  * Language dependent variable initialization callback.
51  */
52 static uninitialized_local_variable_func_t *default_initialize_local_variable = NULL;
53
54 /**
55  * Creates a Phi node with all predecessors.  Calling this constructor
56  * is only allowed if the corresponding block is mature.
57  */
58 ir_node *new_rd_Phi(dbg_info *db, ir_node *block, int arity, ir_node **in,
59                     ir_mode *mode)
60 {
61         ir_graph *irg = get_irn_irg(block);
62         ir_node  *res = new_ir_node(db, irg, block, op_Phi, mode, arity, in);
63         res->attr.phi.u.backedge = new_backedge_arr(irg->obst, arity);
64
65         res = optimize_node(res);
66         irn_verify_irg(res, irg);
67
68         /* Memory Phis in endless loops must be kept alive.
69            As we can't distinguish these easily we keep all of them alive. */
70         if (is_Phi(res) && mode == mode_M)
71                 add_End_keepalive(get_irg_end(irg), res);
72         return res;
73 }
74
75 ir_node *new_rd_Const(dbg_info *db, ir_graph *irg, ir_tarval *con)
76 {
77         ir_node  *block = get_irg_start_block(irg);
78         ir_mode  *mode  = get_tarval_mode(con);
79         ir_node  *res   = new_ir_node(db, irg, block, op_Const, mode, 0, NULL);
80         res->attr.con.tarval = con;
81
82         res = optimize_node (res);
83         irn_verify_irg(res, irg);
84
85         return res;
86 }
87
88 ir_node *new_rd_Const_long(dbg_info *db, ir_graph *irg, ir_mode *mode,
89                            long value)
90 {
91         return new_rd_Const(db, irg, new_tarval_from_long(value, mode));
92 }
93
94 ir_node *new_rd_defaultProj(dbg_info *db, ir_node *arg, long max_proj)
95 {
96         ir_node *res;
97
98         assert(is_Cond(arg));
99         arg->attr.cond.default_proj = max_proj;
100         res = new_rd_Proj(db, arg, mode_X, max_proj);
101         return res;
102 }
103
104 ir_node *new_rd_ASM(dbg_info *db, ir_node *block, int arity, ir_node *in[],
105                     ir_asm_constraint *inputs, int n_outs,
106                         ir_asm_constraint *outputs, int n_clobber,
107                         ident *clobber[], ident *text)
108 {
109         ir_graph *irg = get_irn_irg(block);
110         ir_node  *res = new_ir_node(db, irg, block, op_ASM, mode_T, arity, in);
111
112         res->attr.assem.pin_state = op_pin_state_pinned;
113         res->attr.assem.input_constraints
114                 = NEW_ARR_D(ir_asm_constraint, irg->obst, arity);
115         res->attr.assem.output_constraints
116                 = NEW_ARR_D(ir_asm_constraint, irg->obst, n_outs);
117         res->attr.assem.clobbers = NEW_ARR_D(ident *, irg->obst, n_clobber);
118         res->attr.assem.text     = text;
119
120         memcpy(res->attr.assem.input_constraints,  inputs,  sizeof(inputs[0]) * arity);
121         memcpy(res->attr.assem.output_constraints, outputs, sizeof(outputs[0]) * n_outs);
122         memcpy(res->attr.assem.clobbers, clobber, sizeof(clobber[0]) * n_clobber);
123
124         res = optimize_node(res);
125         irn_verify_irg(res, irg);
126         return res;
127 }
128
129 ir_node *new_rd_simpleSel(dbg_info *db, ir_node *block, ir_node *store,
130                           ir_node *objptr, ir_entity *ent)
131 {
132         return new_rd_Sel(db, block, store, objptr, 0, NULL, ent);
133 }
134
135 ir_node *new_rd_SymConst(dbg_info *db, ir_graph *irg, ir_mode *mode,
136                          symconst_symbol value, symconst_kind symkind)
137 {
138         ir_node *block = get_irg_start_block(irg);
139         ir_node *res   = new_ir_node(db, irg, block, op_SymConst, mode, 0, NULL);
140         res->attr.symc.kind = symkind;
141         res->attr.symc.sym  = value;
142
143         res = optimize_node(res);
144         irn_verify_irg(res, irg);
145         return res;
146 }
147
148 ir_node *new_rd_SymConst_addr_ent(dbg_info *db, ir_graph *irg, ir_mode *mode, ir_entity *symbol)
149 {
150         symconst_symbol sym;
151         sym.entity_p = symbol;
152         return new_rd_SymConst(db, irg, mode, sym, symconst_addr_ent);
153 }
154
155 ir_node *new_rd_SymConst_ofs_ent(dbg_info *db, ir_graph *irg, ir_mode *mode, ir_entity *symbol)
156 {
157         symconst_symbol sym;
158         sym.entity_p = symbol;
159         return new_rd_SymConst(db, irg, mode, sym, symconst_ofs_ent);
160 }
161
162 ir_node *new_rd_SymConst_type_tag(dbg_info *db, ir_graph *irg, ir_mode *mode, ir_type *symbol)
163 {
164         symconst_symbol sym;
165         sym.type_p = symbol;
166         return new_rd_SymConst(db, irg, mode, sym, symconst_type_tag);
167 }
168
169 ir_node *new_rd_SymConst_size(dbg_info *db, ir_graph *irg, ir_mode *mode, ir_type *symbol)
170 {
171         symconst_symbol sym;
172         sym.type_p = symbol;
173         return new_rd_SymConst(db, irg, mode, sym, symconst_type_size);
174 }
175
176 ir_node *new_rd_SymConst_align(dbg_info *db, ir_graph *irg, ir_mode *mode, ir_type *symbol)
177 {
178         symconst_symbol sym;
179         sym.type_p = symbol;
180         return new_rd_SymConst(db, irg, mode, sym, symconst_type_align);
181 }
182
183 ir_node *new_r_Const(ir_graph *irg, ir_tarval *con)
184 {
185         return new_rd_Const(NULL, irg, con);
186 }
187 ir_node *new_r_Const_long(ir_graph *irg, ir_mode *mode, long value)
188 {
189         return new_rd_Const_long(NULL, irg, mode, value);
190 }
191 ir_node *new_r_SymConst(ir_graph *irg, ir_mode *mode, symconst_symbol value,
192                         symconst_kind symkind)
193 {
194         return new_rd_SymConst(NULL, irg, mode, value, symkind);
195 }
196 ir_node *new_r_simpleSel(ir_node *block, ir_node *store, ir_node *objptr,
197                          ir_entity *ent)
198 {
199         return new_rd_Sel(NULL, block, store, objptr, 0, NULL, ent);
200 }
201 ir_node *new_r_Phi(ir_node *block, int arity, ir_node **in, ir_mode *mode)
202 {
203         return new_rd_Phi(NULL, block, arity, in, mode);
204 }
205 ir_node *new_r_defaultProj(ir_node *arg, long max_proj)
206 {
207         return new_rd_defaultProj(NULL, arg, max_proj);
208 }
209 ir_node *new_r_Bad(ir_graph *irg)
210 {
211         return get_irg_bad(irg);
212 }
213 ir_node *new_r_NoMem(ir_graph *irg)
214 {
215         return get_irg_no_mem(irg);
216 }
217 ir_node *new_r_ASM(ir_node *block,
218                    int arity, ir_node *in[], ir_asm_constraint *inputs,
219                    int n_outs, ir_asm_constraint *outputs,
220                    int n_clobber, ident *clobber[], ident *text)
221 {
222         return new_rd_ASM(NULL, block, arity, in, inputs, n_outs, outputs, n_clobber, clobber, text);
223 }
224
225 /* ***********************************************************************/
226 /* Methods necessary for automatic Phi node creation                     */
227 /*
228   ir_node *phi_merge            (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
229   ir_node *get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
230   ir_node *new_rd_Phi0          (ir_graph *irg, ir_node *block, ir_mode *mode)
231   ir_node *new_rd_Phi_in        (ir_graph *irg, ir_node *block, ir_mode *mode, ir_node **in, int ins)
232
233   Call Graph:   ( A ---> B == A "calls" B)
234
235        get_value         mature_immBlock
236           |                   |
237           |                   |
238           |                   |
239           |          ---> phi_merge
240           |         /       /   \
241           |        /       /     \
242          \|/      /      |/_      \
243        get_r_value_internal        |
244                 |                  |
245                 |                  |
246                \|/                \|/
247            new_rd_Phi0          new_rd_Phi_in
248
249 * *************************************************************************** */
250
251 /** Creates a Phi node with 0 predecessors. */
252 static inline ir_node *new_rd_Phi0(ir_node *block, ir_mode *mode)
253 {
254         ir_graph *irg = get_irn_irg(block);
255         ir_node  *res = new_ir_node(NULL, irg, block, op_Phi, mode, 0, NULL);
256         irn_verify_irg(res, irg);
257         return res;
258 }
259
260 /**
261  * Internal constructor of a Phi node by a phi_merge operation.
262  *
263  * @param block  the block in which the Phi will be constructed
264  * @param mode   the mod eof the Phi node
265  * @param in     the input array of the phi node
266  * @param n_in   number of elements in the input array
267  * @param phi0   in non-NULL: the Phi0 node in the same block that represents
268  *               the value for which the new Phi is constructed
269  */
270 static ir_node *new_rd_Phi_in(ir_node *block, ir_mode *mode,
271                               int n_in, ir_node **in, ir_node *phi0)
272 {
273         int i;
274         ir_node *res, *known;
275         ir_graph *irg = get_irn_irg(block);
276
277         /* Allocate a new node on the obstack.  The allocation copies the in
278            array. */
279         res = new_ir_node(NULL, irg, block, op_Phi, mode, n_in, in);
280         res->attr.phi.u.backedge = new_backedge_arr(irg->obst, n_in);
281
282         /* This loop checks whether the Phi has more than one predecessor.
283            If so, it is a real Phi node and we break the loop.  Else the
284            Phi node merges the same definition on several paths and therefore
285            is not needed.
286            Note: We MUST consider Bad nodes, else we might get data flow cycles in dead loops! */
287         known = res;
288         for (i = n_in - 1; i >= 0; --i) {
289                 assert(in[i]);
290
291                 in[i] = skip_Id(in[i]);  /* increases the number of freed Phis. */
292
293                 /* Optimize self referencing Phis:  We can't detect them yet properly, as
294                 they still refer to the Phi0 they will replace.  So replace right now. */
295                 if (phi0 && in[i] == phi0)
296                         in[i] = res;
297
298                 if (in[i] == res || in[i] == known)
299                         continue;
300
301                 if (known == res)
302                         known = in[i];
303                 else
304                         break;
305         }
306
307         /* i < 0: there is at most one predecessor, we don't need a phi node. */
308         if (i < 0) {
309                 if (res != known) {
310                         edges_node_deleted(res, current_ir_graph);
311                         obstack_free(current_ir_graph->obst, res);
312                         if (is_Phi(known)) {
313                                 /* If pred is a phi node we want to optimize it: If loops are matured in a bad
314                                    order, an enclosing Phi know may get superfluous. */
315                                 res = optimize_in_place_2(known);
316                                 if (res != known)
317                                         exchange(known, res);
318                         }
319                         else
320                                 res = known;
321                 } else {
322                         /* A undefined value, e.g., in unreachable code. */
323                         res = new_r_Bad(irg);
324                 }
325         } else {
326                 res = optimize_node(res);  /* This is necessary to add the node to the hash table for cse. */
327                 irn_verify_irg(res, irg);
328                 /* Memory Phis in endless loops must be kept alive.
329                    As we can't distinguish these easily we keep all of them alive. */
330                 if (is_Phi(res) && mode == mode_M)
331                         add_End_keepalive(get_irg_end(irg), res);
332         }
333
334         return res;
335 }
336
337 static ir_node *get_r_value_internal(ir_node *block, int pos, ir_mode *mode);
338
339 /**
340  * Computes the predecessors for the real phi node, and then
341  * allocates and returns this node.  The routine called to allocate the
342  * node might optimize it away and return a real value.
343  * This function must be called with an in-array of proper size.
344  */
345 static ir_node *phi_merge(ir_node *block, int pos, ir_mode *mode,
346                           int n_ins, ir_node **ins)
347 {
348         ir_graph *irg = get_irn_irg(block);
349         ir_node *prevBlock, *res, *phi0, *phi0_all;
350         int i;
351
352         /* If this block has no value at pos create a Phi0 and remember it
353            in graph_arr to break recursions.
354            Else we may not set graph_arr as there a later value is remembered. */
355         phi0 = NULL;
356         if (block->attr.block.graph_arr[pos] == NULL) {
357
358                 if (block == get_irg_start_block(irg)) {
359                         /* Collapsing to Bad tarvals is no good idea.
360                            So we call a user-supplied routine here that deals with this
361                            case as appropriate for the given language. Sorrily the only
362                            help we can give here is the position.
363
364                            Even if all variables are defined before use, it can happen that
365                            we get to the start block, if a Cond has been replaced by a tuple
366                            (bad, jmp).  In this case we call the function needlessly,
367                            eventually generating an non existent error.
368                            However, this SHOULD NOT HAPPEN, as bad control flow nodes are
369                            intercepted before recurring.
370                          */
371                         if (default_initialize_local_variable != NULL) {
372                                 ir_node *rem = get_r_cur_block(irg);
373
374                                 set_r_cur_block(irg, block);
375                                 block->attr.block.graph_arr[pos] = default_initialize_local_variable(irg, mode, pos - 1);
376                                 set_r_cur_block(irg, rem);
377                         } else {
378                                 block->attr.block.graph_arr[pos] = new_r_Unknown(irg, mode);
379                         }
380                         return block->attr.block.graph_arr[pos];
381                 } else {
382                         phi0 = new_rd_Phi0(block, mode);
383                         block->attr.block.graph_arr[pos] = phi0;
384                 }
385         }
386
387         /* This loop goes to all predecessor blocks of the block the Phi node
388            is in and there finds the operands of the Phi node by calling
389            get_r_value_internal.  */
390         for (i = 1; i <= n_ins; ++i) {
391                 ir_node *cf_pred = block->in[i];
392                 ir_node *prevCfOp = skip_Proj(cf_pred);
393                 assert(prevCfOp);
394                 if (is_Bad(prevCfOp)) {
395                         /* In case a Cond has been optimized we would get right to the start block
396                         with an invalid definition. */
397                         ins[i-1] = new_r_Bad(irg);
398                         continue;
399                 }
400                 prevBlock = prevCfOp->in[0]; /* go past control flow op to prev block */
401                 assert(prevBlock);
402                 if (!is_Bad(prevBlock)) {
403                         ins[i-1] = get_r_value_internal(prevBlock, pos, mode);
404                 } else {
405                         ins[i-1] = new_r_Bad(irg);
406                 }
407         }
408
409         /* We want to pass the Phi0 node to the constructor: this finds additional
410            optimization possibilities.
411            The Phi0 node either is allocated in this function, or it comes from
412            a former call to get_r_value_internal(). In this case we may not yet
413            exchange phi0, as this is done in mature_immBlock(). */
414         if (phi0 == NULL) {
415                 phi0_all = block->attr.block.graph_arr[pos];
416                 if (! is_Phi0(phi0_all)            ||
417                     get_irn_arity(phi0_all) != 0   ||
418                     get_nodes_block(phi0_all) != block)
419                         phi0_all = NULL;
420         } else {
421                 phi0_all = phi0;
422         }
423
424         /* After collecting all predecessors into the array ins a new Phi node
425            with these predecessors is created.  This constructor contains an
426            optimization: If all predecessors of the Phi node are identical it
427            returns the only operand instead of a new Phi node.  */
428         res = new_rd_Phi_in(block, mode, n_ins, ins, phi0_all);
429
430         /* In case we allocated a Phi0 node at the beginning of this procedure,
431            we need to exchange this Phi0 with the real Phi. */
432         if (phi0 != NULL) {
433                 exchange(phi0, res);
434                 block->attr.block.graph_arr[pos] = res;
435         }
436
437         return res;
438 }
439
440 /**
441  * This function returns the last definition of a value.  In case
442  * this value was last defined in a previous block, Phi nodes are
443  * inserted.  If the part of the firm graph containing the definition
444  * is not yet constructed, a dummy Phi node is returned.
445  *
446  * @param block   the current block
447  * @param pos     the value number of the value searched
448  * @param mode    the mode of this value (needed for Phi construction)
449  */
450 static ir_node *get_r_value_internal(ir_node *block, int pos, ir_mode *mode)
451 {
452         ir_node *res;
453         /* There are 4 cases to treat.
454
455            1. The block is not mature and we visit it the first time.  We can not
456               create a proper Phi node, therefore a Phi0, i.e., a Phi without
457               predecessors is returned.  This node is added to the linked list (block
458               attribute "phis") of the containing block to be completed when this block is
459               matured. (Completion will add a new Phi and turn the Phi0 into an Id
460               node.)
461
462            2. The value is already known in this block, graph_arr[pos] is set and we
463               visit the block the first time.  We can return the value without
464               creating any new nodes.
465
466            3. The block is mature and we visit it the first time.  A Phi node needs
467               to be created (phi_merge).  If the Phi is not needed, as all it's
468               operands are the same value reaching the block through different
469               paths, it's optimized away and the value itself is returned.
470
471            4. The block is mature, and we visit it the second time.  Now two
472               subcases are possible:
473               * The value was computed completely the last time we were here. This
474                 is the case if there is no loop.  We can return the proper value.
475               * The recursion that visited this node and set the flag did not
476                 return yet.  We are computing a value in a loop and need to
477                 break the recursion.  This case only happens if we visited
478             the same block with phi_merge before, which inserted a Phi0.
479             So we return the Phi0.
480         */
481
482         /* case 4 -- already visited. */
483         if (irn_visited(block)) {
484                 /* As phi_merge allocates a Phi0 this value is always defined. Here
485                 is the critical difference of the two algorithms. */
486                 assert(block->attr.block.graph_arr[pos]);
487                 return block->attr.block.graph_arr[pos];
488         }
489
490         /* visited the first time */
491         mark_irn_visited(block);
492
493         /* Get the local valid value */
494         res = block->attr.block.graph_arr[pos];
495
496         /* case 2 -- If the value is actually computed, return it. */
497         if (res != NULL)
498                 return res;
499
500         if (block->attr.block.is_matured) { /* case 3 */
501
502                 /* The Phi has the same amount of ins as the corresponding block. */
503                 int n_in = get_irn_arity(block);
504                 ir_node **in;
505                 NEW_ARR_A(ir_node *, in, n_in);
506
507                 /* Phi merge collects the predecessors and then creates a node. */
508                 res = phi_merge(block, pos, mode, n_in, in);
509         } else {  /* case 1 */
510                 /* The block is not mature, we don't know how many in's are needed.  A Phi
511                    with zero predecessors is created.  Such a Phi node is called Phi0
512                    node.  The Phi0 is then added to the list of Phi0 nodes in this block
513                    to be matured by mature_immBlock later.
514                    The Phi0 has to remember the pos of it's internal value.  If the real
515                    Phi is computed, pos is used to update the array with the local
516                    values. */
517                 res = new_rd_Phi0(block, mode);
518                 res->attr.phi.u.pos    = pos;
519                 res->attr.phi.next     = block->attr.block.phis;
520                 block->attr.block.phis = res;
521         }
522
523         assert(is_ir_node(res) && "phi_merge() failed to construct a definition");
524
525         /* The local valid value is available now. */
526         block->attr.block.graph_arr[pos] = res;
527
528         return res;
529 }
530
531 /* ************************************************************************** */
532
533 /*
534  * Finalize a Block node, when all control flows are known.
535  * Acceptable parameters are only Block nodes.
536  */
537 void mature_immBlock(ir_node *block)
538 {
539         int ins;
540         ir_node *n, **nin;
541         ir_node *next;
542
543         assert(is_Block(block));
544         if (!get_Block_matured(block)) {
545                 ir_graph *irg = current_ir_graph;
546
547                 ins = ARR_LEN(block->in) - 1;
548                 /* Fix block parameters */
549                 block->attr.block.backedge = new_backedge_arr(irg->obst, ins);
550
551                 /* An array for building the Phi nodes. */
552                 NEW_ARR_A(ir_node *, nin, ins);
553
554                 /* Traverse a chain of Phi nodes attached to this block and mature
555                 these, too. **/
556                 for (n = block->attr.block.phis; n; n = next) {
557                         inc_irg_visited(irg);
558                         next = n->attr.phi.next;
559                         exchange(n, phi_merge(block, n->attr.phi.u.pos, n->mode, ins, nin));
560                 }
561
562                 block->attr.block.is_matured = 1;
563
564                 /* Now, as the block is a finished Firm node, we can optimize it.
565                    Since other nodes have been allocated since the block was created
566                    we can not free the node on the obstack.  Therefore we have to call
567                    optimize_in_place().
568                    Unfortunately the optimization does not change a lot, as all allocated
569                    nodes refer to the unoptimized node.
570                    We can call optimize_in_place_2(), as global cse has no effect on blocks. */
571                 block = optimize_in_place_2(block);
572                 irn_verify_irg(block, irg);
573         }
574 }
575
576 ir_node *new_d_Phi(dbg_info *db, int arity, ir_node **in, ir_mode *mode)
577 {
578         assert(get_irg_phase_state(current_ir_graph) == phase_building);
579         return new_rd_Phi(db, current_ir_graph->current_block, arity, in, mode);
580 }
581
582 ir_node *new_d_Const(dbg_info *db, ir_tarval *con)
583 {
584         assert(get_irg_phase_state(current_ir_graph) == phase_building);
585         return new_rd_Const(db, current_ir_graph, con);
586 }
587
588 ir_node *new_d_Const_long(dbg_info *db, ir_mode *mode, long value)
589 {
590         assert(get_irg_phase_state(current_ir_graph) == phase_building);
591         return new_rd_Const_long(db, current_ir_graph, mode, value);
592 }
593
594 ir_node *new_d_defaultProj(dbg_info *db, ir_node *arg, long max_proj)
595 {
596         ir_node *res;
597         assert(is_Cond(arg));
598         assert(get_irg_phase_state(current_ir_graph) == phase_building);
599         arg->attr.cond.default_proj = max_proj;
600         res = new_d_Proj(db, arg, mode_X, max_proj);
601         return res;
602 }
603
604 ir_node *new_d_simpleSel(dbg_info *db, ir_node *store, ir_node *objptr,
605                          ir_entity *ent)
606 {
607         assert(get_irg_phase_state(current_ir_graph) == phase_building);
608         return new_rd_Sel(db, current_ir_graph->current_block,
609                           store, objptr, 0, NULL, ent);
610 }
611
612 ir_node *new_d_SymConst(dbg_info *db, ir_mode *mode, symconst_symbol value,
613                         symconst_kind kind)
614 {
615         assert(get_irg_phase_state(current_ir_graph) == phase_building);
616         return new_rd_SymConst(db, current_ir_graph, mode, value, kind);
617 }
618
619 ir_node *new_d_ASM(dbg_info *db, int arity, ir_node *in[],
620                    ir_asm_constraint *inputs,
621                    int n_outs, ir_asm_constraint *outputs, int n_clobber,
622                    ident *clobber[], ident *text)
623 {
624         assert(get_irg_phase_state(current_ir_graph) == phase_building);
625         return new_rd_ASM(db, current_ir_graph->current_block, arity, in, inputs,
626                           n_outs, outputs, n_clobber, clobber, text);
627 }
628
629 ir_node *new_rd_immBlock(dbg_info *dbgi, ir_graph *irg)
630 {
631         ir_node *res;
632
633         assert(get_irg_phase_state(irg) == phase_building);
634         /* creates a new dynamic in-array as length of in is -1 */
635         res = new_ir_node(dbgi, irg, NULL, op_Block, mode_BB, -1, NULL);
636
637         res->attr.block.is_matured  = 0;
638         res->attr.block.is_dead     = 0;
639         res->attr.block.irg.irg     = irg;
640         res->attr.block.backedge    = NULL;
641         res->attr.block.in_cg       = NULL;
642         res->attr.block.cg_backedge = NULL;
643         res->attr.block.extblk      = NULL;
644         res->attr.block.region      = NULL;
645         res->attr.block.entity      = NULL;
646
647         set_Block_block_visited(res, 0);
648
649         /* Create and initialize array for Phi-node construction. */
650         res->attr.block.graph_arr = NEW_ARR_D(ir_node *, irg->obst, irg->n_loc);
651         memset(res->attr.block.graph_arr, 0, sizeof(ir_node*) * irg->n_loc);
652
653         /* Immature block may not be optimized! */
654         irn_verify_irg(res, irg);
655
656         return res;
657 }
658
659 ir_node *new_r_immBlock(ir_graph *irg)
660 {
661         return new_rd_immBlock(NULL, irg);
662 }
663
664 ir_node *new_d_immBlock(dbg_info *dbgi)
665 {
666         return new_rd_immBlock(dbgi, current_ir_graph);
667 }
668
669 ir_node *new_immBlock(void)
670 {
671         return new_rd_immBlock(NULL, current_ir_graph);
672 }
673
674 void add_immBlock_pred(ir_node *block, ir_node *jmp)
675 {
676         int n = ARR_LEN(block->in) - 1;
677
678         assert(is_Block(block) && "Error: Must be a Block");
679         assert(!block->attr.block.is_matured && "Error: Block already matured!\n");
680         assert(is_ir_node(jmp));
681
682         ARR_APP1(ir_node *, block->in, jmp);
683         /* Call the hook */
684         hook_set_irn_n(block, n, jmp, NULL);
685 }
686
687 void set_cur_block(ir_node *target)
688 {
689         current_ir_graph->current_block = target;
690 }
691
692 void set_r_cur_block(ir_graph *irg, ir_node *target)
693 {
694         irg->current_block = target;
695 }
696
697 ir_node *get_r_cur_block(ir_graph *irg)
698 {
699         return irg->current_block;
700 }
701
702 ir_node *get_cur_block(void)
703 {
704         return get_r_cur_block(current_ir_graph);
705 }
706
707 ir_node *get_r_value(ir_graph *irg, int pos, ir_mode *mode)
708 {
709         assert(get_irg_phase_state(irg) == phase_building);
710         inc_irg_visited(irg);
711
712         assert(pos >= 0);
713
714         return get_r_value_internal(irg->current_block, pos + 1, mode);
715 }
716
717 ir_node *get_value(int pos, ir_mode *mode)
718 {
719         return get_r_value(current_ir_graph, pos, mode);
720 }
721
722 /**
723  * helper function for guess_mode: recursively look for a definition for
724  * local variable @p pos, returns its mode if found.
725  */
726 static ir_mode *guess_recursively(ir_node *block, int pos)
727 {
728         ir_node *value;
729         int      n_preds;
730         int      i;
731
732         if (irn_visited(block))
733                 return NULL;
734         mark_irn_visited(block);
735
736         /* already have a defintion -> we can simply look at its mode */
737         value = block->attr.block.graph_arr[pos];
738         if (value != NULL)
739                 return get_irn_mode(value);
740
741         /* now we try to guess, by looking at the predecessor blocks */
742         n_preds = get_irn_arity(block);
743         for (i = 0; i < n_preds; ++i) {
744                 ir_node *pred_block = get_Block_cfgpred_block(block, i);
745                 ir_mode *mode       = guess_recursively(pred_block, pos);
746                 if (mode != NULL)
747                         return mode;
748         }
749
750         /* no way to guess */
751         return NULL;
752 }
753
754 ir_mode *ir_guess_mode(int pos)
755 {
756         ir_graph *irg   = current_ir_graph;
757         ir_node  *block = irg->current_block;
758         ir_node  *value = block->attr.block.graph_arr[pos+1];
759         ir_mode  *mode;
760
761         /* already have a defintion -> we can simply look at its mode */
762         if (value != NULL)
763                 return get_irn_mode(value);
764
765         ir_reserve_resources(current_ir_graph, IR_RESOURCE_IRN_VISITED);
766         inc_irg_visited(current_ir_graph);
767         mode = guess_recursively(block, pos+1);
768         ir_free_resources(current_ir_graph, IR_RESOURCE_IRN_VISITED);
769
770         return mode;
771 }
772
773 void set_r_value(ir_graph *irg, int pos, ir_node *value)
774 {
775         assert(get_irg_phase_state(irg) == phase_building);
776         assert(pos >= 0);
777         assert(pos+1 < irg->n_loc);
778         assert(is_ir_node(value));
779         irg->current_block->attr.block.graph_arr[pos + 1] = value;
780 }
781
782 void set_value(int pos, ir_node *value)
783 {
784         set_r_value(current_ir_graph, pos, value);
785 }
786
787 int find_value(ir_node *value)
788 {
789         int i;
790         ir_node *bl = current_ir_graph->current_block;
791
792         for (i = ARR_LEN(bl->attr.block.graph_arr) - 1; i >= 1; --i)
793                 if (bl->attr.block.graph_arr[i] == value)
794                         return i - 1;
795         return -1;
796 }
797
798 ir_node *get_r_store(ir_graph *irg)
799 {
800         assert(get_irg_phase_state(irg) == phase_building);
801         inc_irg_visited(irg);
802         return get_r_value_internal(irg->current_block, 0, mode_M);
803 }
804
805 ir_node *get_store(void)
806 {
807         return get_r_store(current_ir_graph);
808 }
809
810 void set_r_store(ir_graph *irg, ir_node *store)
811 {
812         ir_node *load, *pload, *pred, *in[2];
813
814         assert(get_irg_phase_state(irg) == phase_building);
815         /* Beware: due to dead code elimination, a store might become a Bad node even in
816            the construction phase. */
817         assert((get_irn_mode(store) == mode_M || is_Bad(store)) && "storing non-memory node");
818
819         if (get_opt_auto_create_sync()) {
820                 /* handle non-volatile Load nodes by automatically creating Sync's */
821                 load = skip_Proj(store);
822                 if (is_Load(load) && get_Load_volatility(load) == volatility_non_volatile) {
823                         pred = get_Load_mem(load);
824
825                         if (is_Sync(pred)) {
826                                 /* a Load after a Sync: move it up */
827                                 ir_node *mem = skip_Proj(get_Sync_pred(pred, 0));
828
829                                 set_Load_mem(load, get_memop_mem(mem));
830                                 add_Sync_pred(pred, store);
831                                 store = pred;
832                         } else {
833                                 pload = skip_Proj(pred);
834                                 if (is_Load(pload) && get_Load_volatility(pload) == volatility_non_volatile) {
835                                         /* a Load after a Load: create a new Sync */
836                                         set_Load_mem(load, get_Load_mem(pload));
837
838                                         in[0] = pred;
839                                         in[1] = store;
840                                         store = new_r_Sync(irg->current_block, 2, in);
841                                 }
842                         }
843                 }
844         }
845         irg->current_block->attr.block.graph_arr[0] = store;
846 }
847
848 void set_store(ir_node *store)
849 {
850         set_r_store(current_ir_graph, store);
851 }
852
853 void keep_alive(ir_node *ka)
854 {
855         add_End_keepalive(get_irg_end(current_ir_graph), ka);
856 }
857
858 void ir_set_uninitialized_local_variable_func(
859                 uninitialized_local_variable_func_t *func)
860 {
861         default_initialize_local_variable = func;
862 }
863
864 void irg_finalize_cons(ir_graph *irg)
865 {
866         set_irg_phase_state(irg, phase_high);
867 }
868
869 void irp_finalize_cons(void)
870 {
871         int i;
872         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
873                 irg_finalize_cons(get_irp_irg(i));
874         }
875         irp->phase_state = phase_high;
876 }
877
878 ir_node *new_Const(ir_tarval *con)
879 {
880         return new_d_Const(NULL, con);
881 }
882
883 ir_node *new_Const_long(ir_mode *mode, long value)
884 {
885         return new_d_Const_long(NULL, mode, value);
886 }
887
888 ir_node *new_SymConst(ir_mode *mode, symconst_symbol value, symconst_kind kind)
889 {
890         return new_d_SymConst(NULL, mode, value, kind);
891 }
892 ir_node *new_simpleSel(ir_node *store, ir_node *objptr, ir_entity *ent)
893 {
894         return new_d_simpleSel(NULL, store, objptr, ent);
895 }
896 ir_node *new_Phi(int arity, ir_node **in, ir_mode *mode)
897 {
898         return new_d_Phi(NULL, arity, in, mode);
899 }
900 ir_node *new_defaultProj(ir_node *arg, long max_proj)
901 {
902         return new_d_defaultProj(NULL, arg, max_proj);
903 }
904 ir_node *new_Bad(void)
905 {
906         assert(get_irg_phase_state(current_ir_graph) == phase_building);
907         return get_irg_bad(current_ir_graph);
908 }
909 ir_node *new_NoMem(void)
910 {
911         assert(get_irg_phase_state(current_ir_graph) == phase_building);
912         return get_irg_no_mem(current_ir_graph);
913 }
914 ir_node *new_ASM(int arity, ir_node *in[], ir_asm_constraint *inputs,
915                  int n_outs, ir_asm_constraint *outputs,
916                  int n_clobber, ident *clobber[], ident *text)
917 {
918         return new_d_ASM(NULL, arity, in, inputs, n_outs, outputs, n_clobber, clobber, text);
919 }
920
921 ir_node *new_r_Anchor(ir_graph *irg)
922 {
923         ir_node *in[anchor_last];
924         ir_node *res;
925         memset(in, 0, sizeof(in));
926         res = new_ir_node(NULL, irg, NULL, op_Anchor, mode_ANY, anchor_last, in);
927
928         /* hack to get get_irn_irg working: set block to ourself and allow
929          * get_Block_irg for anchor */
930         res->attr.irg.irg = irg;
931         res->in[0] = res;
932
933         return res;
934 }
935
936 ir_node *new_r_Block_noopt(ir_graph *irg, int arity, ir_node *in[])
937 {
938         ir_node *res = new_ir_node(NULL, irg, NULL, op_Block, mode_BB, arity, in);
939         res->attr.block.irg.irg = irg;
940         res->attr.block.backedge = new_backedge_arr(irg->obst, arity);
941         set_Block_matured(res, 1);
942         /* Create and initialize array for Phi-node construction. */
943         if (get_irg_phase_state(irg) == phase_building) {
944                 res->attr.block.graph_arr = NEW_ARR_D(ir_node *, irg->obst, irg->n_loc);
945                 memset(res->attr.block.graph_arr, 0, irg->n_loc * sizeof(ir_node*));
946         }
947         irn_verify_irg(res, irg);
948         return res;
949 }