0e5527d62d068bac9c0fc2ecb2823d54e334cf72
[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
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 /* when we need verifying */
48 #ifdef NDEBUG
49 # define IRN_VERIFY_IRG(res, irg)
50 #else
51 # define IRN_VERIFY_IRG(res, irg)  irn_verify_irg(res, irg)
52 #endif /* NDEBUG */
53
54 /**
55  * Language dependent variable initialization callback.
56  */
57 static uninitialized_local_variable_func_t *default_initialize_local_variable = NULL;
58
59 #include "gen_ir_cons.c.inl"
60
61 static ir_node *new_bd_Start(dbg_info *db, ir_node *block)
62 {
63         ir_node  *res;
64         ir_graph *irg = get_irn_irg(block);
65
66         res = new_ir_node(db, irg, block, op_Start, mode_T, 0, NULL);
67
68         IRN_VERIFY_IRG(res, irg);
69         return res;
70 }  /* new_bd_Start */
71
72 static ir_node *new_bd_End(dbg_info *db, ir_node *block)
73 {
74         ir_node  *res;
75         ir_graph *irg = get_irn_irg(block);
76
77         res = new_ir_node(db, irg, block, op_End, mode_X, -1, NULL);
78
79         IRN_VERIFY_IRG(res, irg);
80         return res;
81 }  /* new_bd_End */
82
83 /**
84  * Creates a Phi node with all predecessors.  Calling this constructor
85  * is only allowed if the corresponding block is mature.
86  */
87 static ir_node *new_bd_Phi(dbg_info *db, ir_node *block, int arity, ir_node **in, ir_mode *mode)
88 {
89         ir_node  *res;
90         ir_graph *irg = get_irn_irg(block);
91         int i;
92         int has_unknown = 0;
93
94         /* Don't assert that block matured: the use of this constructor is strongly
95            restricted ... */
96         if (get_Block_matured(block))
97                 assert(get_irn_arity(block) == arity);
98
99         res = new_ir_node(db, irg, block, op_Phi, mode, arity, in);
100
101         res->attr.phi.u.backedge = new_backedge_arr(irg->obst, arity);
102
103         for (i = arity - 1; i >= 0; --i)
104                 if (is_Unknown(in[i])) {
105                         has_unknown = 1;
106                         break;
107                 }
108
109         if (!has_unknown) res = optimize_node(res);
110         IRN_VERIFY_IRG(res, irg);
111
112         /* Memory Phis in endless loops must be kept alive.
113            As we can't distinguish these easily we keep all of them alive. */
114         if (is_Phi(res) && mode == mode_M)
115                 add_End_keepalive(get_irg_end(irg), res);
116         return res;
117 }  /* new_bd_Phi */
118
119 static ir_node *new_bd_Const_type(dbg_info *db, tarval *con, ir_type *tp)
120 {
121         ir_node  *res;
122         ir_graph *irg = current_ir_graph;
123
124         res = new_ir_node(db, irg, get_irg_start_block(irg), op_Const, get_tarval_mode(con), 0, NULL);
125         res->attr.con.tarval = con;
126         set_Const_type(res, tp);  /* Call method because of complex assertion. */
127         res = optimize_node (res);
128         assert(get_Const_type(res) == tp);
129         IRN_VERIFY_IRG(res, irg);
130
131         return res;
132 }  /* new_bd_Const_type */
133
134 static ir_node *new_bd_Const(dbg_info *db, tarval *con)
135 {
136         ir_graph *irg = current_ir_graph;
137
138         return new_rd_Const_type(db, irg, con, firm_unknown_type);
139 }  /* new_bd_Const */
140
141 static ir_node *new_bd_Const_long(dbg_info *db, ir_mode *mode, long value)
142 {
143         ir_graph *irg = current_ir_graph;
144
145         return new_rd_Const(db, irg, new_tarval_from_long(value, mode));
146 }  /* new_bd_Const_long */
147
148 static ir_node *new_bd_defaultProj(dbg_info *db, ir_node *arg, long max_proj)
149 {
150         ir_node  *res;
151
152         assert(is_Cond(arg));
153         arg->attr.cond.default_proj = max_proj;
154         res = new_rd_Proj(db, arg, mode_X, max_proj);
155         return res;
156 }  /* new_bd_defaultProj */
157
158 static ir_node *new_bd_Sel(dbg_info *db, ir_node *block, ir_node *store,
159                            ir_node *objptr, int arity, ir_node **in,
160                            ir_entity *ent)
161 {
162         ir_node  **r_in;
163         ir_node  *res;
164         int      r_arity;
165         ir_graph *irg  = get_irn_irg(block);
166         ir_mode  *mode = is_Method_type(get_entity_type(ent)) ? mode_P_code : mode_P_data;
167
168         assert(ent != NULL && is_entity(ent) && "entity expected in Sel construction");
169
170         r_arity = arity + 2;
171         NEW_ARR_A(ir_node *, r_in, r_arity);  /* uses alloca */
172         r_in[0] = store;
173         r_in[1] = objptr;
174         memcpy(&r_in[2], in, sizeof(ir_node *) * arity);
175         /*
176          * Sel's can select functions which should be of mode mode_P_code.
177          */
178         res = new_ir_node(db, irg, block, op_Sel, mode, r_arity, r_in);
179         res->attr.sel.entity = ent;
180         res = optimize_node(res);
181         IRN_VERIFY_IRG(res, irg);
182         return res;
183 }  /* new_bd_Sel */
184
185 static ir_node *new_bd_SymConst_type(dbg_info *db, ir_node *block,
186                                      ir_mode *mode, symconst_symbol value,
187                                      symconst_kind symkind, ir_type *tp)
188 {
189         ir_graph *irg = get_irn_irg(block);
190         ir_node  *res = new_ir_node(db, irg, block, op_SymConst, mode, 0, NULL);
191
192         res->attr.symc.kind = symkind;
193         res->attr.symc.sym  = value;
194         res->attr.symc.tp   = tp;
195
196         res = optimize_node(res);
197         IRN_VERIFY_IRG(res, irg);
198         return res;
199 }  /* new_bd_SymConst_type */
200
201 static ir_node *new_bd_Sync(dbg_info *db, ir_node *block)
202 {
203         ir_node  *res;
204         ir_graph *irg = get_irn_irg(block);
205
206         res = new_ir_node(db, irg, block, op_Sync, mode_M, -1, NULL);
207         /* no need to call optimize node here, Sync are always created with no predecessors */
208         IRN_VERIFY_IRG(res, irg);
209         return res;
210 }  /* new_bd_Sync */
211
212 static ir_node *new_bd_ASM(dbg_info *db, ir_node *block, int arity,
213                            ir_node *in[], ir_asm_constraint *inputs, int n_outs,
214                                  ir_asm_constraint *outputs, int n_clobber,
215                                  ident *clobber[], ident *text)
216 {
217         ir_node  *res;
218         ir_graph *irg = get_irn_irg(block);
219
220         res = new_ir_node(db, irg, block, op_ASM, mode_T, arity, in);
221         res->attr.assem.pin_state = op_pin_state_pinned;
222         res->attr.assem.input_constraints
223                 = NEW_ARR_D(ir_asm_constraint, irg->obst, arity);
224         res->attr.assem.output_constraints
225                 = NEW_ARR_D(ir_asm_constraint, irg->obst, n_outs);
226         res->attr.assem.clobbers = NEW_ARR_D(ident *, irg->obst, n_clobber);
227         res->attr.assem.text     = text;
228
229         memcpy(res->attr.assem.input_constraints,  inputs,  sizeof(inputs[0]) * arity);
230         memcpy(res->attr.assem.output_constraints, outputs, sizeof(outputs[0]) * n_outs);
231         memcpy(res->attr.assem.clobbers, clobber, sizeof(clobber[0]) * n_clobber);
232
233         res = optimize_node(res);
234         IRN_VERIFY_IRG(res, irg);
235         return res;
236 }  /* new_bd_ASM */
237
238 /* --------------------------------------------- */
239 /* private interfaces, for professional use only */
240 /* --------------------------------------------- */
241
242 ir_node *new_rd_Start(dbg_info *db, ir_graph *irg, ir_node *block)
243 {
244         ir_graph *rem = current_ir_graph;
245         ir_node  *res;
246
247         current_ir_graph = irg;
248         res = new_bd_Start(db, block);
249         current_ir_graph = rem;
250
251         return res;
252 }  /* new_rd_Start */
253
254 ir_node *new_rd_End(dbg_info *db, ir_graph *irg, ir_node *block)
255 {
256         ir_node  *res;
257         ir_graph *rem = current_ir_graph;
258
259         current_ir_graph = irg;
260         res = new_bd_End(db, block);
261         current_ir_graph = rem;
262
263         return res;
264 }  /* new_rd_End */
265
266 /* Creates a Phi node with all predecessors.  Calling this constructor
267    is only allowed if the corresponding block is mature.  */
268 ir_node *new_rd_Phi(dbg_info *db, ir_node *block, int arity, ir_node **in, ir_mode *mode)
269 {
270         ir_node  *res;
271         ir_graph *rem = current_ir_graph;
272
273         current_ir_graph = get_Block_irg(block);
274         res = new_bd_Phi(db, block,arity, in, mode);
275         current_ir_graph = rem;
276
277         return res;
278 }  /* new_rd_Phi */
279
280 ir_node *new_rd_Const_type(dbg_info *db, ir_graph *irg, tarval *con, ir_type *tp)
281 {
282         ir_node  *res;
283         ir_graph *rem = current_ir_graph;
284
285         current_ir_graph = irg;
286         res = new_bd_Const_type(db, con, tp);
287         current_ir_graph = rem;
288
289         return res;
290 }  /* new_rd_Const_type */
291
292 ir_node *new_rd_Const(dbg_info *db, ir_graph *irg, tarval *con)
293 {
294         ir_node  *res;
295         ir_graph *rem = current_ir_graph;
296
297         current_ir_graph = irg;
298         res = new_bd_Const_type(db, con, firm_unknown_type);
299         current_ir_graph = rem;
300
301         return res;
302 }  /* new_rd_Const */
303
304 ir_node *new_rd_Const_long(dbg_info *db, ir_graph *irg, ir_mode *mode, long value)
305 {
306         return new_rd_Const(db, irg, new_tarval_from_long(value, mode));
307 }  /* new_rd_Const_long */
308
309 ir_node *new_rd_defaultProj(dbg_info *db, ir_node *arg, long max_proj)
310 {
311         return new_bd_defaultProj(db, arg, max_proj);
312 }  /* new_rd_defaultProj */
313
314 ir_node *new_rd_simpleSel(dbg_info *db, ir_node *block, ir_node *store,
315                           ir_node *objptr, ir_entity *ent)
316 {
317         ir_node  *res;
318         ir_graph *rem = current_ir_graph;
319
320         current_ir_graph = get_Block_irg(block);
321         res = new_bd_Sel(db, block, store, objptr, 0, NULL, ent);
322         current_ir_graph = rem;
323
324         return res;
325 }  /* new_rd_simpleSel */
326
327 ir_node *new_rd_SymConst_type(dbg_info *db, ir_graph *irg, ir_mode *mode,
328                               symconst_symbol value, symconst_kind symkind,
329                               ir_type *tp)
330 {
331         ir_node  *res;
332         ir_graph *rem = current_ir_graph;
333         ir_node  *block = get_irg_start_block(irg);
334
335         current_ir_graph = irg;
336         res = new_bd_SymConst_type(db, block, mode, value, symkind, tp);
337         current_ir_graph = rem;
338
339         return res;
340 }  /* new_rd_SymConst_type */
341
342 ir_node *new_rd_SymConst(dbg_info *db, ir_graph *irg, ir_mode *mode,
343                          symconst_symbol value, symconst_kind symkind)
344 {
345         return new_rd_SymConst_type(db, irg, mode, value, symkind, firm_unknown_type);
346 }  /* new_rd_SymConst */
347
348 ir_node *new_rd_SymConst_addr_ent(dbg_info *db, ir_graph *irg, ir_mode *mode, ir_entity *symbol, ir_type *tp)
349 {
350         symconst_symbol sym;
351         sym.entity_p = symbol;
352         return new_rd_SymConst_type(db, irg, mode, sym, symconst_addr_ent, tp);
353 }  /* new_rd_SymConst_addr_ent */
354
355 ir_node *new_rd_SymConst_ofs_ent(dbg_info *db, ir_graph *irg, ir_mode *mode, ir_entity *symbol, ir_type *tp)
356 {
357         symconst_symbol sym;
358         sym.entity_p = symbol;
359         return new_rd_SymConst_type(db, irg, mode, sym, symconst_ofs_ent, tp);
360 }  /* new_rd_SymConst_ofs_ent */
361
362 ir_node *new_rd_SymConst_type_tag(dbg_info *db, ir_graph *irg, ir_mode *mode, ir_type *symbol, ir_type *tp)
363 {
364         symconst_symbol sym;
365         sym.type_p = symbol;
366         return new_rd_SymConst_type(db, irg, mode, sym, symconst_type_tag, tp);
367 }  /* new_rd_SymConst_type_tag */
368
369 ir_node *new_rd_SymConst_size(dbg_info *db, ir_graph *irg, ir_mode *mode, ir_type *symbol, ir_type *tp)
370 {
371         symconst_symbol sym;
372         sym.type_p = symbol;
373         return new_rd_SymConst_type(db, irg, mode, sym, symconst_type_size, tp);
374 }  /* new_rd_SymConst_size */
375
376 ir_node *new_rd_SymConst_align(dbg_info *db, ir_graph *irg, ir_mode *mode, ir_type *symbol, ir_type *tp)
377 {
378         symconst_symbol sym;
379         sym.type_p = symbol;
380         return new_rd_SymConst_type(db, irg, mode, sym, symconst_type_align, tp);
381 }  /* new_rd_SymConst_align */
382
383 ir_node *new_rd_Sync(dbg_info *db, ir_node *block, int arity, ir_node *in[])
384 {
385         ir_node  *res;
386         ir_graph *rem = current_ir_graph;
387         int      i;
388
389         current_ir_graph = get_Block_irg(block);
390         res = new_bd_Sync(db, block);
391         current_ir_graph = rem;
392
393         for (i = 0; i < arity; ++i)
394                 add_Sync_pred(res, in[i]);
395
396         return res;
397 }  /* new_rd_Sync */
398
399 ir_node *new_rd_ASM(dbg_info *db, ir_node *block,
400                     int arity, ir_node *in[], ir_asm_constraint *inputs,
401                     int n_outs, ir_asm_constraint *outputs,
402                     int n_clobber, ident *clobber[], ident *text)
403 {
404         ir_node  *res;
405         ir_graph *rem = current_ir_graph;
406
407         current_ir_graph = get_Block_irg(block);
408         res = new_bd_ASM(db, block, arity, in, inputs, n_outs, outputs, n_clobber, clobber, text);
409         current_ir_graph = rem;
410
411         return res;
412 }  /* new_rd_ASM */
413
414 ir_node *new_r_Start(ir_graph *irg, ir_node *block)
415 {
416         return new_rd_Start(NULL, irg, block);
417 }
418 ir_node *new_r_End(ir_graph *irg, ir_node *block)
419 {
420         return new_rd_End(NULL, irg, block);
421 }
422 ir_node *new_r_Const(ir_graph *irg, tarval *con)
423 {
424         return new_rd_Const(NULL, irg, con);
425 }
426 ir_node *new_r_Const_long(ir_graph *irg, ir_mode *mode, long value)
427 {
428         return new_rd_Const_long(NULL, irg, mode, value);
429 }
430 ir_node *new_r_Const_type(ir_graph *irg, tarval *con, ir_type *tp)
431 {
432         return new_rd_Const_type(NULL, irg, con, tp);
433 }
434 ir_node *new_r_SymConst(ir_graph *irg, ir_mode *mode, symconst_symbol value,
435                         symconst_kind symkind)
436 {
437         return new_rd_SymConst(NULL, irg, mode, value, symkind);
438 }
439 ir_node *new_r_simpleSel(ir_node *block, ir_node *store, ir_node *objptr,
440                          ir_entity *ent)
441 {
442         return new_rd_Sel(NULL, block, store, objptr, 0, NULL, ent);
443 }
444 ir_node *new_r_Phi(ir_node *block, int arity, ir_node **in, ir_mode *mode)
445 {
446         return new_rd_Phi(NULL, block, arity, in, mode);
447 }
448 ir_node *new_r_Sync(ir_node *block, int arity, ir_node *in[])
449 {
450         return new_rd_Sync(NULL, block, arity, in);
451 }
452 ir_node *new_r_defaultProj(ir_node *arg, long max_proj)
453 {
454         return new_rd_defaultProj(NULL, arg, max_proj);
455 }
456 ir_node *new_r_Bad(ir_graph *irg)
457 {
458         return get_irg_bad(irg);
459 }
460 ir_node *new_r_NoMem(ir_graph *irg)
461 {
462         return get_irg_no_mem(irg);
463 }
464 ir_node *new_r_ASM(ir_node *block,
465                    int arity, ir_node *in[], ir_asm_constraint *inputs,
466                    int n_outs, ir_asm_constraint *outputs,
467                    int n_clobber, ident *clobber[], ident *text)
468 {
469         return new_rd_ASM(NULL, block, arity, in, inputs, n_outs, outputs, n_clobber, clobber, text);
470 }
471
472 /** ********************/
473 /** public interfaces  */
474 /** construction tools */
475
476 ir_node *new_d_Start(dbg_info *db)
477 {
478         ir_node *res;
479
480         res = new_ir_node(db, current_ir_graph, current_ir_graph->current_block,
481                           op_Start, mode_T, 0, NULL);
482
483         res = optimize_node(res);
484         IRN_VERIFY_IRG(res, current_ir_graph);
485         return res;
486 }  /* new_d_Start */
487
488 ir_node *new_d_End(dbg_info *db)
489 {
490         ir_node *res;
491         res = new_ir_node(db, current_ir_graph,  current_ir_graph->current_block,
492                           op_End, mode_X, -1, NULL);
493         res = optimize_node(res);
494         IRN_VERIFY_IRG(res, current_ir_graph);
495
496         return res;
497 }  /* new_d_End */
498
499 /* ***********************************************************************/
500 /* Methods necessary for automatic Phi node creation                     */
501 /*
502   ir_node *phi_merge            (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
503   ir_node *get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
504   ir_node *new_rd_Phi0          (ir_graph *irg, ir_node *block, ir_mode *mode)
505   ir_node *new_rd_Phi_in        (ir_graph *irg, ir_node *block, ir_mode *mode, ir_node **in, int ins)
506
507   Call Graph:   ( A ---> B == A "calls" B)
508
509        get_value         mature_immBlock
510           |                   |
511           |                   |
512           |                   |
513           |          ---> phi_merge
514           |         /       /   \
515           |        /       /     \
516          \|/      /      |/_      \
517        get_r_value_internal        |
518                 |                  |
519                 |                  |
520                \|/                \|/
521            new_rd_Phi0          new_rd_Phi_in
522
523 * *************************************************************************** */
524
525 /** Creates a Phi node with 0 predecessors. */
526 static inline ir_node *new_rd_Phi0(ir_graph *irg, ir_node *block, ir_mode *mode)
527 {
528         ir_node *res;
529
530         res = new_ir_node(NULL, irg, block, op_Phi, mode, 0, NULL);
531         IRN_VERIFY_IRG(res, irg);
532         return res;
533 }  /* new_rd_Phi0 */
534
535
536 /**
537  * Internal constructor of a Phi node by a phi_merge operation.
538  *
539  * @param irg    the graph on which the Phi will be constructed
540  * @param block  the block in which the Phi will be constructed
541  * @param mode   the mod eof the Phi node
542  * @param in     the input array of the phi node
543  * @param ins    number of elements in the input array
544  * @param phi0   in non-NULL: the Phi0 node in the same block that represents
545  *               the value for which the new Phi is constructed
546  */
547 static inline ir_node *new_rd_Phi_in(ir_graph *irg, ir_node *block,
548                                      ir_mode *mode, ir_node **in, int ins,
549                                      ir_node *phi0)
550 {
551         int i;
552         ir_node *res, *known;
553
554         /* Allocate a new node on the obstack.  The allocation copies the in
555            array. */
556         res = new_ir_node(NULL, irg, block, op_Phi, mode, ins, in);
557         res->attr.phi.u.backedge = new_backedge_arr(irg->obst, ins);
558
559         /* This loop checks whether the Phi has more than one predecessor.
560            If so, it is a real Phi node and we break the loop.  Else the
561            Phi node merges the same definition on several paths and therefore
562            is not needed.
563            Note: We MUST consider Bad nodes, else we might get data flow cycles in dead loops! */
564         known = res;
565         for (i = ins - 1; i >= 0; --i) {
566                 assert(in[i]);
567
568                 in[i] = skip_Id(in[i]);  /* increases the number of freed Phis. */
569
570                 /* Optimize self referencing Phis:  We can't detect them yet properly, as
571                 they still refer to the Phi0 they will replace.  So replace right now. */
572                 if (phi0 && in[i] == phi0)
573                         in[i] = res;
574
575                 if (in[i] == res || in[i] == known)
576                         continue;
577
578                 if (known == res)
579                         known = in[i];
580                 else
581                         break;
582         }
583
584         /* i < 0: there is at most one predecessor, we don't need a phi node. */
585         if (i < 0) {
586                 if (res != known) {
587                         edges_node_deleted(res, current_ir_graph);
588                         obstack_free(current_ir_graph->obst, res);
589                         if (is_Phi(known)) {
590                                 /* If pred is a phi node we want to optimize it: If loops are matured in a bad
591                                    order, an enclosing Phi know may get superfluous. */
592                                 res = optimize_in_place_2(known);
593                                 if (res != known)
594                                         exchange(known, res);
595                         }
596                         else
597                                 res = known;
598                 } else {
599                         /* A undefined value, e.g., in unreachable code. */
600                         res = new_Bad();
601                 }
602         } else {
603                 res = optimize_node(res);  /* This is necessary to add the node to the hash table for cse. */
604                 IRN_VERIFY_IRG(res, irg);
605                 /* Memory Phis in endless loops must be kept alive.
606                    As we can't distinguish these easily we keep all of them alive. */
607                 if (is_Phi(res) && mode == mode_M)
608                         add_End_keepalive(get_irg_end(irg), res);
609         }
610
611         return res;
612 }  /* new_rd_Phi_in */
613
614 static ir_node *get_r_value_internal(ir_node *block, int pos, ir_mode *mode);
615
616 static ir_node *phi_merge(ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins);
617
618 /**
619  * Computes the predecessors for the real phi node, and then
620  * allocates and returns this node.  The routine called to allocate the
621  * node might optimize it away and return a real value.
622  * This function must be called with an in-array of proper size.
623  */
624 static ir_node *phi_merge(ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
625 {
626         ir_node *prevBlock, *res, *phi0, *phi0_all;
627         int i;
628
629         /* If this block has no value at pos create a Phi0 and remember it
630            in graph_arr to break recursions.
631            Else we may not set graph_arr as there a later value is remembered. */
632         phi0 = NULL;
633         if (block->attr.block.graph_arr[pos] == NULL) {
634                 ir_graph *irg = current_ir_graph;
635
636                 if (block == get_irg_start_block(irg)) {
637                         /* Collapsing to Bad tarvals is no good idea.
638                            So we call a user-supplied routine here that deals with this
639                            case as appropriate for the given language. Sorrily the only
640                            help we can give here is the position.
641
642                            Even if all variables are defined before use, it can happen that
643                            we get to the start block, if a Cond has been replaced by a tuple
644                            (bad, jmp).  In this case we call the function needlessly,
645                            eventually generating an non existent error.
646                            However, this SHOULD NOT HAPPEN, as bad control flow nodes are
647                            intercepted before recurring.
648                          */
649                         if (default_initialize_local_variable != NULL) {
650                                 ir_node *rem = get_cur_block();
651
652                                 set_cur_block(block);
653                                 block->attr.block.graph_arr[pos] = default_initialize_local_variable(irg, mode, pos - 1);
654                                 set_cur_block(rem);
655                         } else {
656                                 block->attr.block.graph_arr[pos] = new_Unknown(mode);
657                         }
658                         return block->attr.block.graph_arr[pos];
659                 } else {
660                         phi0 = new_rd_Phi0(irg, block, mode);
661                         block->attr.block.graph_arr[pos] = phi0;
662                 }
663         }
664
665         /* This loop goes to all predecessor blocks of the block the Phi node
666            is in and there finds the operands of the Phi node by calling
667            get_r_value_internal.  */
668         for (i = 1; i <= ins; ++i) {
669                 ir_node *cf_pred = block->in[i];
670                 ir_node *prevCfOp = skip_Proj(cf_pred);
671                 assert(prevCfOp);
672                 if (is_Bad(prevCfOp)) {
673                         /* In case a Cond has been optimized we would get right to the start block
674                         with an invalid definition. */
675                         nin[i-1] = new_Bad();
676                         continue;
677                 }
678                 prevBlock = prevCfOp->in[0]; /* go past control flow op to prev block */
679                 assert(prevBlock);
680                 if (!is_Bad(prevBlock)) {
681                         nin[i-1] = get_r_value_internal(prevBlock, pos, mode);
682                 } else {
683                         nin[i-1] = new_Bad();
684                 }
685         }
686
687         /* We want to pass the Phi0 node to the constructor: this finds additional
688            optimization possibilities.
689            The Phi0 node either is allocated in this function, or it comes from
690            a former call to get_r_value_internal(). In this case we may not yet
691            exchange phi0, as this is done in mature_immBlock(). */
692         if (phi0 == NULL) {
693                 phi0_all = block->attr.block.graph_arr[pos];
694                 if (! is_Phi0(phi0_all)            ||
695                     get_irn_arity(phi0_all) != 0   ||
696                     get_nodes_block(phi0_all) != block)
697                         phi0_all = NULL;
698         } else {
699                 phi0_all = phi0;
700         }
701
702         /* After collecting all predecessors into the array nin a new Phi node
703            with these predecessors is created.  This constructor contains an
704            optimization: If all predecessors of the Phi node are identical it
705            returns the only operand instead of a new Phi node.  */
706         res = new_rd_Phi_in(current_ir_graph, block, mode, nin, ins, phi0_all);
707
708         /* In case we allocated a Phi0 node at the beginning of this procedure,
709            we need to exchange this Phi0 with the real Phi. */
710         if (phi0 != NULL) {
711                 exchange(phi0, res);
712                 block->attr.block.graph_arr[pos] = res;
713         }
714
715         return res;
716 }  /* phi_merge */
717
718 /**
719  * This function returns the last definition of a value.  In case
720  * this value was last defined in a previous block, Phi nodes are
721  * inserted.  If the part of the firm graph containing the definition
722  * is not yet constructed, a dummy Phi node is returned.
723  *
724  * @param block   the current block
725  * @param pos     the value number of the value searched
726  * @param mode    the mode of this value (needed for Phi construction)
727  */
728 static ir_node *get_r_value_internal(ir_node *block, int pos, ir_mode *mode)
729 {
730         ir_node *res;
731         /* There are 4 cases to treat.
732
733            1. The block is not mature and we visit it the first time.  We can not
734               create a proper Phi node, therefore a Phi0, i.e., a Phi without
735               predecessors is returned.  This node is added to the linked list (block
736               attribute "phis") of the containing block to be completed when this block is
737               matured. (Completion will add a new Phi and turn the Phi0 into an Id
738               node.)
739
740            2. The value is already known in this block, graph_arr[pos] is set and we
741               visit the block the first time.  We can return the value without
742               creating any new nodes.
743
744            3. The block is mature and we visit it the first time.  A Phi node needs
745               to be created (phi_merge).  If the Phi is not needed, as all it's
746               operands are the same value reaching the block through different
747               paths, it's optimized away and the value itself is returned.
748
749            4. The block is mature, and we visit it the second time.  Now two
750               subcases are possible:
751               * The value was computed completely the last time we were here. This
752                 is the case if there is no loop.  We can return the proper value.
753               * The recursion that visited this node and set the flag did not
754                 return yet.  We are computing a value in a loop and need to
755                 break the recursion.  This case only happens if we visited
756             the same block with phi_merge before, which inserted a Phi0.
757             So we return the Phi0.
758         */
759
760         /* case 4 -- already visited. */
761         if (get_irn_visited(block) == get_irg_visited(current_ir_graph)) {
762                 /* As phi_merge allocates a Phi0 this value is always defined. Here
763                 is the critical difference of the two algorithms. */
764                 assert(block->attr.block.graph_arr[pos]);
765                 return block->attr.block.graph_arr[pos];
766         }
767
768         /* visited the first time */
769         set_irn_visited(block, get_irg_visited(current_ir_graph));
770
771         /* Get the local valid value */
772         res = block->attr.block.graph_arr[pos];
773
774         /* case 2 -- If the value is actually computed, return it. */
775         if (res != NULL)
776                 return res;
777
778         if (block->attr.block.is_matured) { /* case 3 */
779
780                 /* The Phi has the same amount of ins as the corresponding block. */
781                 int ins = get_irn_arity(block);
782                 ir_node **nin;
783                 NEW_ARR_A(ir_node *, nin, ins);
784
785                 /* Phi merge collects the predecessors and then creates a node. */
786                 res = phi_merge(block, pos, mode, nin, ins);
787
788         } else {  /* case 1 */
789                 /* The block is not mature, we don't know how many in's are needed.  A Phi
790                    with zero predecessors is created.  Such a Phi node is called Phi0
791                    node.  The Phi0 is then added to the list of Phi0 nodes in this block
792                    to be matured by mature_immBlock later.
793                    The Phi0 has to remember the pos of it's internal value.  If the real
794                    Phi is computed, pos is used to update the array with the local
795                    values. */
796                 res = new_rd_Phi0(current_ir_graph, block, mode);
797                 res->attr.phi.u.pos    = pos;
798                 res->attr.phi.next     = block->attr.block.phis;
799                 block->attr.block.phis = res;
800         }
801
802         assert(is_ir_node(res) && "phi_merge() failed to construct a definition");
803
804         /* The local valid value is available now. */
805         block->attr.block.graph_arr[pos] = res;
806
807         return res;
808 }  /* get_r_value_internal */
809
810 /* ************************************************************************** */
811
812 /*
813  * Finalize a Block node, when all control flows are known.
814  * Acceptable parameters are only Block nodes.
815  */
816 void mature_immBlock(ir_node *block)
817 {
818         int ins;
819         ir_node *n, **nin;
820         ir_node *next;
821
822         assert(is_Block(block));
823         if (!get_Block_matured(block)) {
824                 ir_graph *irg = current_ir_graph;
825
826                 ins = ARR_LEN(block->in) - 1;
827                 /* Fix block parameters */
828                 block->attr.block.backedge = new_backedge_arr(irg->obst, ins);
829
830                 /* An array for building the Phi nodes. */
831                 NEW_ARR_A(ir_node *, nin, ins);
832
833                 /* Traverse a chain of Phi nodes attached to this block and mature
834                 these, too. **/
835                 for (n = block->attr.block.phis; n; n = next) {
836                         inc_irg_visited(irg);
837                         next = n->attr.phi.next;
838                         exchange(n, phi_merge(block, n->attr.phi.u.pos, n->mode, nin, ins));
839                 }
840
841                 block->attr.block.is_matured = 1;
842
843                 /* Now, as the block is a finished Firm node, we can optimize it.
844                    Since other nodes have been allocated since the block was created
845                    we can not free the node on the obstack.  Therefore we have to call
846                    optimize_in_place().
847                    Unfortunately the optimization does not change a lot, as all allocated
848                    nodes refer to the unoptimized node.
849                    We can call optimize_in_place_2(), as global cse has no effect on blocks. */
850                 block = optimize_in_place_2(block);
851                 IRN_VERIFY_IRG(block, irg);
852         }
853 }  /* mature_immBlock */
854
855 ir_node *new_d_Phi(dbg_info *db, int arity, ir_node **in, ir_mode *mode)
856 {
857         return new_bd_Phi(db, current_ir_graph->current_block, arity, in, mode);
858 }  /* new_d_Phi */
859
860 ir_node *new_d_Const(dbg_info *db, tarval *con)
861 {
862         return new_bd_Const(db, con);
863 }  /* new_d_Const */
864
865 ir_node *new_d_Const_long(dbg_info *db, ir_mode *mode, long value)
866 {
867         return new_bd_Const_long(db, mode, value);
868 }  /* new_d_Const_long */
869
870 ir_node *new_d_Const_type(dbg_info *db, tarval *con, ir_type *tp)
871 {
872         return new_bd_Const_type(db, con, tp);
873 }  /* new_d_Const_type */
874
875
876 ir_node *new_d_defaultProj(dbg_info *db, ir_node *arg, long max_proj)
877 {
878         ir_node *res;
879         assert(is_Cond(arg));
880         arg->attr.cond.default_proj = max_proj;
881         res = new_d_Proj(db, arg, mode_X, max_proj);
882         return res;
883 }  /* new_d_defaultProj */
884
885 ir_node *new_d_simpleSel(dbg_info *db, ir_node *store, ir_node *objptr, ir_entity *ent)
886 /* GL: objptr was called frame before.  Frame was a bad choice for the name
887    as the operand could as well be a pointer to a dynamic object. */
888 {
889         return new_bd_Sel(db, current_ir_graph->current_block,
890                           store, objptr, 0, NULL, ent);
891 }  /* new_d_simpleSel */
892
893 ir_node *new_d_SymConst_type(dbg_info *db, ir_mode *mode, symconst_symbol value, symconst_kind kind, ir_type *tp)
894 {
895         return new_bd_SymConst_type(db, get_irg_start_block(current_ir_graph), mode,
896                                     value, kind, tp);
897 }  /* new_d_SymConst_type */
898
899 ir_node *new_d_SymConst(dbg_info *db, ir_mode *mode, symconst_symbol value, symconst_kind kind)
900 {
901         return new_bd_SymConst_type(db, get_irg_start_block(current_ir_graph), mode,
902                                     value, kind, firm_unknown_type);
903 }  /* new_d_SymConst */
904
905 ir_node *new_d_Sync(dbg_info *db, int arity, ir_node *in[])
906 {
907         return new_rd_Sync(db, current_ir_graph->current_block, arity, in);
908 }  /* new_d_Sync */
909
910 ir_node *new_d_ASM(dbg_info *db, int arity, ir_node *in[], ir_asm_constraint *inputs,
911                    int n_outs, ir_asm_constraint *outputs, int n_clobber,
912                    ident *clobber[], ident *text)
913 {
914         return new_bd_ASM(db, current_ir_graph->current_block, arity, in, inputs, n_outs, outputs, n_clobber, clobber, text);
915 }  /* new_d_ASM */
916
917 /* ********************************************************************* */
918 /* Comfortable interface with automatic Phi node construction.           */
919 /* (Uses also constructors of ?? interface, except new_Block.            */
920 /* ********************************************************************* */
921
922 /*  Block construction */
923 /* immature Block without predecessors */
924 ir_node *new_d_immBlock(dbg_info *db)
925 {
926         ir_node *res;
927
928         assert(get_irg_phase_state(current_ir_graph) == phase_building);
929         /* creates a new dynamic in-array as length of in is -1 */
930         res = new_ir_node(db, current_ir_graph, NULL, op_Block, mode_BB, -1, NULL);
931
932         res->attr.block.is_matured  = 0;
933         res->attr.block.is_dead     = 0;
934         res->attr.block.irg.irg     = current_ir_graph;
935         res->attr.block.backedge    = NULL;
936         res->attr.block.in_cg       = NULL;
937         res->attr.block.cg_backedge = NULL;
938         res->attr.block.extblk      = NULL;
939         res->attr.block.region      = NULL;
940         res->attr.block.entity      = NULL;
941
942         set_Block_block_visited(res, 0);
943
944         /* Create and initialize array for Phi-node construction. */
945         res->attr.block.graph_arr = NEW_ARR_D(ir_node *, current_ir_graph->obst,
946                                               current_ir_graph->n_loc);
947         memset(res->attr.block.graph_arr, 0, sizeof(ir_node *)*current_ir_graph->n_loc);
948
949         /* Immature block may not be optimized! */
950         IRN_VERIFY_IRG(res, current_ir_graph);
951
952         return res;
953 }  /* new_d_immBlock */
954
955 ir_node *new_immBlock(void)
956 {
957         return new_d_immBlock(NULL);
958 }  /* new_immBlock */
959
960 /* add an edge to a jmp/control flow node */
961 void add_immBlock_pred(ir_node *block, ir_node *jmp)
962 {
963         int n = ARR_LEN(block->in) - 1;
964
965         assert(is_Block(block) && "Error: Must be a Block");
966         assert(!block->attr.block.is_matured && "Error: Block already matured!\n");
967         assert(is_ir_node(jmp));
968
969         ARR_APP1(ir_node *, block->in, jmp);
970         /* Call the hook */
971         hook_set_irn_n(block, n, jmp, NULL);
972 }  /* add_immBlock_pred */
973
974 /* changing the current block */
975 void set_cur_block(ir_node *target)
976 {
977         current_ir_graph->current_block = target;
978 }  /* set_cur_block */
979
980 /* ************************ */
981 /* parameter administration */
982
983 /* get a value from the parameter array from the current block by its index */
984 ir_node *get_d_value(dbg_info *db, int pos, ir_mode *mode)
985 {
986         ir_graph *irg = current_ir_graph;
987         assert(get_irg_phase_state(irg) == phase_building);
988         inc_irg_visited(irg);
989         (void) db;
990
991         assert(pos >= 0);
992
993         return get_r_value_internal(irg->current_block, pos + 1, mode);
994 }  /* get_d_value */
995
996 /* get a value from the parameter array from the current block by its index */
997 ir_node *get_value(int pos, ir_mode *mode)
998 {
999         return get_d_value(NULL, pos, mode);
1000 }  /* get_value */
1001
1002 /**
1003  * helper function for guess_mode: recursively look for a definition for
1004  * local variable @p pos, returns its mode if found.
1005  */
1006 static ir_mode *guess_recursively(ir_node *block, int pos)
1007 {
1008         ir_node *value;
1009         int      n_preds;
1010         int      i;
1011
1012         if (irn_visited(block))
1013                 return NULL;
1014         mark_irn_visited(block);
1015
1016         /* already have a defintion -> we can simply look at its mode */
1017         value = block->attr.block.graph_arr[pos];
1018         if (value != NULL)
1019                 return get_irn_mode(value);
1020
1021         /* now we try to guess, by looking at the predecessor blocks */
1022         n_preds = get_irn_arity(block);
1023         for (i = 0; i < n_preds; ++i) {
1024                 ir_node *pred_block = get_Block_cfgpred_block(block, i);
1025                 ir_mode *mode       = guess_recursively(pred_block, pos);
1026                 if (mode != NULL)
1027                         return mode;
1028         }
1029
1030         /* no way to guess */
1031         return NULL;
1032 }
1033
1034 ir_mode *ir_guess_mode(int pos)
1035 {
1036         ir_graph *irg   = current_ir_graph;
1037         ir_node  *block = irg->current_block;
1038         ir_node  *value = block->attr.block.graph_arr[pos+1];
1039         ir_mode  *mode;
1040
1041         /* already have a defintion -> we can simply look at its mode */
1042         if (value != NULL)
1043                 return get_irn_mode(value);
1044
1045         ir_reserve_resources(current_ir_graph, IR_RESOURCE_IRN_VISITED);
1046         inc_irg_visited(current_ir_graph);
1047         mode = guess_recursively(block, pos+1);
1048         ir_free_resources(current_ir_graph, IR_RESOURCE_IRN_VISITED);
1049
1050         return mode;
1051 }
1052
1053 /* set a value at position pos in the parameter array from the current block */
1054 void set_value(int pos, ir_node *value)
1055 {
1056         ir_graph *irg = current_ir_graph;
1057         assert(get_irg_phase_state(irg) == phase_building);
1058         assert(pos >= 0);
1059         assert(pos+1 < irg->n_loc);
1060         assert(is_ir_node(value));
1061         irg->current_block->attr.block.graph_arr[pos + 1] = value;
1062 }  /* set_value */
1063
1064 /* Find the value number for a node in the current block.*/
1065 int find_value(ir_node *value)
1066 {
1067         int i;
1068         ir_node *bl = current_ir_graph->current_block;
1069
1070         for (i = ARR_LEN(bl->attr.block.graph_arr) - 1; i >= 1; --i)
1071                 if (bl->attr.block.graph_arr[i] == value)
1072                         return i - 1;
1073         return -1;
1074 }  /* find_value */
1075
1076 /* get the current store */
1077 ir_node *get_store(void)
1078 {
1079         ir_graph *irg = current_ir_graph;
1080
1081         assert(get_irg_phase_state(irg) == phase_building);
1082         /* GL: one could call get_value instead */
1083         inc_irg_visited(irg);
1084         return get_r_value_internal(irg->current_block, 0, mode_M);
1085 }  /* get_store */
1086
1087 /* set the current store: handles automatic Sync construction for Load nodes */
1088 void set_store(ir_node *store)
1089 {
1090         ir_node *load, *pload, *pred, *in[2];
1091
1092         assert(get_irg_phase_state(current_ir_graph) == phase_building);
1093         /* Beware: due to dead code elimination, a store might become a Bad node even in
1094            the construction phase. */
1095         assert((get_irn_mode(store) == mode_M || is_Bad(store)) && "storing non-memory node");
1096
1097         if (get_opt_auto_create_sync()) {
1098                 /* handle non-volatile Load nodes by automatically creating Sync's */
1099                 load = skip_Proj(store);
1100                 if (is_Load(load) && get_Load_volatility(load) == volatility_non_volatile) {
1101                         pred = get_Load_mem(load);
1102
1103                         if (is_Sync(pred)) {
1104                                 /* a Load after a Sync: move it up */
1105                                 ir_node *mem = skip_Proj(get_Sync_pred(pred, 0));
1106
1107                                 set_Load_mem(load, get_memop_mem(mem));
1108                                 add_Sync_pred(pred, store);
1109                                 store = pred;
1110                         } else {
1111                                 pload = skip_Proj(pred);
1112                                 if (is_Load(pload) && get_Load_volatility(pload) == volatility_non_volatile) {
1113                                         /* a Load after a Load: create a new Sync */
1114                                         set_Load_mem(load, get_Load_mem(pload));
1115
1116                                         in[0] = pred;
1117                                         in[1] = store;
1118                                         store = new_Sync(2, in);
1119                                 }
1120                         }
1121                 }
1122         }
1123         current_ir_graph->current_block->attr.block.graph_arr[0] = store;
1124 }  /* set_store */
1125
1126 void keep_alive(ir_node *ka)
1127 {
1128         add_End_keepalive(get_irg_end(current_ir_graph), ka);
1129 }  /* keep_alive */
1130
1131 /* --- Useful access routines --- */
1132 /* Returns the current block of the current graph.  To set the current
1133    block use set_cur_block. */
1134 ir_node *get_cur_block(void)
1135 {
1136         return get_irg_current_block(current_ir_graph);
1137 }  /* get_cur_block */
1138
1139 /* Returns the frame type of the current graph */
1140 ir_type *get_cur_frame_type(void)
1141 {
1142         return get_irg_frame_type(current_ir_graph);
1143 }  /* get_cur_frame_type */
1144
1145
1146 /* ********************************************************************* */
1147 /* initialize */
1148
1149 /* call once for each run of the library */
1150 void ir_set_uninitialized_local_variable_func(
1151                 uninitialized_local_variable_func_t *func)
1152 {
1153         default_initialize_local_variable = func;
1154 }
1155
1156 void irg_finalize_cons(ir_graph *irg)
1157 {
1158         set_irg_phase_state(irg, phase_high);
1159 }
1160
1161 void irp_finalize_cons(void)
1162 {
1163         int i;
1164         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1165                 irg_finalize_cons(get_irp_irg(i));
1166         }
1167         irp->phase_state = phase_high;
1168 }
1169
1170 ir_node *new_Start(void)
1171 {
1172         return new_d_Start(NULL);
1173 }
1174 ir_node *new_End(void)
1175 {
1176         return new_d_End(NULL);
1177 }
1178 ir_node *new_Const(tarval *con)
1179 {
1180         return new_d_Const(NULL, con);
1181 }
1182
1183 ir_node *new_Const_long(ir_mode *mode, long value)
1184 {
1185         return new_d_Const_long(NULL, mode, value);
1186 }
1187
1188 ir_node *new_Const_type(tarval *con, ir_type *tp)
1189 {
1190         return new_d_Const_type(NULL, con, tp);
1191 }
1192
1193 ir_node *new_SymConst_type(ir_mode *mode, symconst_symbol value, symconst_kind kind, ir_type *type)
1194 {
1195         return new_d_SymConst_type(NULL, mode, value, kind, type);
1196 }
1197 ir_node *new_SymConst(ir_mode *mode, symconst_symbol value, symconst_kind kind)
1198 {
1199         return new_d_SymConst(NULL, mode, value, kind);
1200 }
1201 ir_node *new_simpleSel(ir_node *store, ir_node *objptr, ir_entity *ent)
1202 {
1203         return new_d_simpleSel(NULL, store, objptr, ent);
1204 }
1205 ir_node *new_Phi(int arity, ir_node **in, ir_mode *mode)
1206 {
1207         return new_d_Phi(NULL, arity, in, mode);
1208 }
1209 ir_node *new_Sync(int arity, ir_node *in[])
1210 {
1211         return new_d_Sync(NULL, arity, in);
1212 }
1213 ir_node *new_defaultProj(ir_node *arg, long max_proj)
1214 {
1215         return new_d_defaultProj(NULL, arg, max_proj);
1216 }
1217 ir_node *new_Bad(void)
1218 {
1219         return get_irg_bad(current_ir_graph);
1220 }
1221 ir_node *new_NoMem(void)
1222 {
1223         return get_irg_no_mem(current_ir_graph);
1224 }
1225 ir_node *new_ASM(int arity, ir_node *in[], ir_asm_constraint *inputs,
1226                  int n_outs, ir_asm_constraint *outputs,
1227                  int n_clobber, ident *clobber[], ident *text)
1228 {
1229         return new_d_ASM(NULL, arity, in, inputs, n_outs, outputs, n_clobber, clobber, text);
1230 }
1231
1232 /* create a new anchor node */
1233 ir_node *new_Anchor(ir_graph *irg)
1234 {
1235         ir_node *in[anchor_last];
1236         ir_node *res;
1237         memset(in, 0, sizeof(in));
1238         res = new_ir_node(NULL, irg, NULL, op_Anchor, mode_ANY, anchor_last, in);
1239
1240         /* hack to get get_irn_irg working: set block to ourself and allow
1241          * get_Block_irg for anchor */
1242         res->attr.irg.irg = irg;
1243         res->in[0] = res;
1244
1245         return res;
1246 }