2ddfebd0d37d4cae0b48f68da3cfb72ec15a4a28
[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 "irvrfy.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
46 /* when we need verifying */
47 #ifdef NDEBUG
48 # define IRN_VRFY_IRG(res, irg)
49 #else
50 # define IRN_VRFY_IRG(res, irg)  irn_vrfy_irg(res, irg)
51 #endif /* NDEBUG */
52
53 /**
54  * Language dependent variable initialization callback.
55  */
56 static uninitialized_local_variable_func_t *default_initialize_local_variable = NULL;
57
58 /* creates a bd constructor for a binop */
59 #define NEW_BD_BINOP(instr)                                     \
60 static ir_node *                                                \
61 new_bd_##instr(dbg_info *db, ir_node *block,                    \
62        ir_node *op1, ir_node *op2, ir_mode *mode)               \
63 {                                                               \
64   ir_node  *in[2];                                              \
65   ir_node  *res;                                                \
66   ir_graph *irg = current_ir_graph;                             \
67   in[0] = op1;                                                  \
68   in[1] = op2;                                                  \
69   res = new_ir_node(db, irg, block, op_##instr, mode, 2, in);   \
70   res = optimize_node(res);                                     \
71   IRN_VRFY_IRG(res, irg);                                       \
72   return res;                                                   \
73 }
74
75 /* creates a bd constructor for an unop */
76 #define NEW_BD_UNOP(instr)                                      \
77 static ir_node *                                                \
78 new_bd_##instr(dbg_info *db, ir_node *block,                    \
79               ir_node *op, ir_mode *mode)                       \
80 {                                                               \
81   ir_node  *res;                                                \
82   ir_graph *irg = current_ir_graph;                             \
83   res = new_ir_node(db, irg, block, op_##instr, mode, 1, &op);  \
84   res = optimize_node(res);                                     \
85   IRN_VRFY_IRG(res, irg);                                       \
86   return res;                                                   \
87 }
88
89 /* creates a bd constructor for an divop */
90 #define NEW_BD_DIVOP(instr)                                     \
91 static ir_node *                                                \
92 new_bd_##instr(dbg_info *db, ir_node *block,                    \
93             ir_node *memop, ir_node *op1, ir_node *op2, ir_mode *mode, op_pin_state state) \
94 {                                                               \
95   ir_node  *in[3];                                              \
96   ir_node  *res;                                                \
97   ir_graph *irg = current_ir_graph;                             \
98   in[0] = memop;                                                \
99   in[1] = op1;                                                  \
100   in[2] = op2;                                                  \
101   res = new_ir_node(db, irg, block, op_##instr, mode_T, 3, in); \
102   res->attr.divmod.exc.pin_state = state;                       \
103   res->attr.divmod.resmode = mode;                              \
104   res->attr.divmod.no_remainder = 0;                            \
105   res = optimize_node(res);                                     \
106   IRN_VRFY_IRG(res, irg);                                       \
107   return res;                                                   \
108 }
109
110 /* creates a rd constructor for a binop */
111 #define NEW_RD_BINOP(instr)                                     \
112 ir_node *                                                       \
113 new_rd_##instr(dbg_info *db, ir_graph *irg, ir_node *block,     \
114        ir_node *op1, ir_node *op2, ir_mode *mode)               \
115 {                                                               \
116   ir_node  *res;                                                \
117   ir_graph *rem = current_ir_graph;                             \
118   current_ir_graph = irg;                                       \
119   res = new_bd_##instr(db, block, op1, op2, mode);              \
120   current_ir_graph = rem;                                       \
121   return res;                                                   \
122 }
123
124 /* creates a rd constructor for an unop */
125 #define NEW_RD_UNOP(instr)                                      \
126 ir_node *                                                       \
127 new_rd_##instr(dbg_info *db, ir_graph *irg, ir_node *block,     \
128               ir_node *op, ir_mode *mode)                       \
129 {                                                               \
130   ir_node  *res;                                                \
131   ir_graph *rem = current_ir_graph;                             \
132   current_ir_graph = irg;                                       \
133   res = new_bd_##instr(db, block, op, mode);                    \
134   current_ir_graph = rem;                                       \
135   return res;                                                   \
136 }
137
138 /* creates a rd constructor for an divop */
139 #define NEW_RD_DIVOP(instr)                                     \
140 ir_node *                                                       \
141 new_rd_##instr(dbg_info *db, ir_graph *irg, ir_node *block,     \
142             ir_node *memop, ir_node *op1, ir_node *op2, ir_mode *mode, op_pin_state state) \
143 {                                                               \
144   ir_node  *res;                                                \
145   ir_graph *rem = current_ir_graph;                             \
146   current_ir_graph = irg;                                       \
147   res = new_bd_##instr(db, block, memop, op1, op2, mode, state);\
148   current_ir_graph = rem;                                       \
149   return res;                                                   \
150 }
151
152 /* creates a d constructor for an binop */
153 #define NEW_D_BINOP(instr)                                                    \
154 ir_node *                                                                     \
155 new_d_##instr(dbg_info *db, ir_node *op1, ir_node *op2, ir_mode *mode) {      \
156   return new_bd_##instr(db, current_ir_graph->current_block, op1, op2, mode); \
157 }
158
159 /* creates a d constructor for an unop */
160 #define NEW_D_UNOP(instr)                                                     \
161 ir_node *                                                                     \
162 new_d_##instr(dbg_info *db, ir_node *op, ir_mode *mode) {                     \
163   return new_bd_##instr(db, current_ir_graph->current_block, op, mode);       \
164 }
165
166 #include "gen_ir_cons.c.inl"
167
168 static ir_node *
169 new_bd_Start(dbg_info *db, ir_node *block) {
170         ir_node  *res;
171         ir_graph *irg = current_ir_graph;
172
173         res = new_ir_node(db, irg, block, op_Start, mode_T, 0, NULL);
174
175         IRN_VRFY_IRG(res, irg);
176         return res;
177 }  /* new_bd_Start */
178
179 static ir_node *
180 new_bd_End(dbg_info *db, ir_node *block) {
181         ir_node  *res;
182         ir_graph *irg = current_ir_graph;
183
184         res = new_ir_node(db, irg, block, op_End, mode_X, -1, NULL);
185
186         IRN_VRFY_IRG(res, irg);
187         return res;
188 }  /* new_bd_End */
189
190 /**
191  * Creates a Phi node with all predecessors.  Calling this constructor
192  * is only allowed if the corresponding block is mature.
193  */
194 static ir_node *
195 new_bd_Phi(dbg_info *db, ir_node *block, int arity, ir_node **in, ir_mode *mode) {
196         ir_node  *res;
197         ir_graph *irg = current_ir_graph;
198         int i;
199         int has_unknown = 0;
200
201         /* Don't assert that block matured: the use of this constructor is strongly
202            restricted ... */
203         if (get_Block_matured(block))
204                 assert(get_irn_arity(block) == arity);
205
206         res = new_ir_node(db, irg, block, op_Phi, mode, arity, in);
207
208         res->attr.phi.u.backedge = new_backedge_arr(irg->obst, arity);
209
210         for (i = arity - 1; i >= 0; --i)
211                 if (is_Unknown(in[i])) {
212                         has_unknown = 1;
213                         break;
214                 }
215
216         if (!has_unknown) res = optimize_node(res);
217         IRN_VRFY_IRG(res, irg);
218
219         /* Memory Phis in endless loops must be kept alive.
220            As we can't distinguish these easily we keep all of them alive. */
221         if (is_Phi(res) && mode == mode_M)
222                 add_End_keepalive(get_irg_end(irg), res);
223         return res;
224 }  /* new_bd_Phi */
225
226 static ir_node *
227 new_bd_Const_type(dbg_info *db, tarval *con, ir_type *tp) {
228         ir_node  *res;
229         ir_graph *irg = current_ir_graph;
230
231         res = new_ir_node(db, irg, get_irg_start_block(irg), op_Const, get_tarval_mode(con), 0, NULL);
232         res->attr.con.tv = con;
233         set_Const_type(res, tp);  /* Call method because of complex assertion. */
234         res = optimize_node (res);
235         assert(get_Const_type(res) == tp);
236         IRN_VRFY_IRG(res, irg);
237
238         return res;
239 }  /* new_bd_Const_type */
240
241 static ir_node *
242 new_bd_Const(dbg_info *db, tarval *con) {
243         ir_graph *irg = current_ir_graph;
244
245         return new_rd_Const_type (db, irg, con, firm_unknown_type);
246 }  /* new_bd_Const */
247
248 static ir_node *
249 new_bd_Const_long(dbg_info *db, ir_mode *mode, long value) {
250         ir_graph *irg = current_ir_graph;
251
252         return new_rd_Const(db, irg, new_tarval_from_long(value, mode));
253 }  /* new_bd_Const_long */
254
255 static ir_node *
256 new_bd_defaultProj(dbg_info *db, ir_node *block, ir_node *arg,
257            long max_proj) {
258         ir_node  *res;
259         ir_graph *irg = current_ir_graph;
260
261         assert(arg->op == op_Cond);
262         arg->attr.cond.kind = fragmentary;
263         arg->attr.cond.default_proj = max_proj;
264         res = new_rd_Proj (db, irg, block, arg, mode_X, max_proj);
265         return res;
266 }  /* new_bd_defaultProj */
267
268 static ir_node *
269 new_bd_Sel(dbg_info *db, ir_node *block, ir_node *store, ir_node *objptr,
270            int arity, ir_node **in, ir_entity *ent) {
271         ir_node  **r_in;
272         ir_node  *res;
273         int      r_arity;
274         ir_graph *irg = current_ir_graph;
275         ir_mode  *mode = is_Method_type(get_entity_type(ent)) ? mode_P_code : mode_P_data;
276
277         assert(ent != NULL && is_entity(ent) && "entity expected in Sel construction");
278
279         r_arity = arity + 2;
280         NEW_ARR_A(ir_node *, r_in, r_arity);  /* uses alloca */
281         r_in[0] = store;
282         r_in[1] = objptr;
283         memcpy(&r_in[2], in, sizeof(ir_node *) * arity);
284         /*
285          * Sel's can select functions which should be of mode mode_P_code.
286          */
287         res = new_ir_node(db, irg, block, op_Sel, mode, r_arity, r_in);
288         res->attr.sel.entity = ent;
289         res = optimize_node(res);
290         IRN_VRFY_IRG(res, irg);
291         return res;
292 }  /* new_bd_Sel */
293
294 static ir_node *
295 new_bd_SymConst_type(dbg_info *db, ir_node *block, ir_mode *mode,
296                      symconst_symbol value,symconst_kind symkind, ir_type *tp) {
297         ir_graph *irg = current_ir_graph;
298         ir_node  *res = new_ir_node(db, irg, block, op_SymConst, mode, 0, NULL);
299
300         res->attr.symc.kind = symkind;
301         res->attr.symc.sym  = value;
302         res->attr.symc.tp   = tp;
303
304         res = optimize_node(res);
305         IRN_VRFY_IRG(res, irg);
306         return res;
307 }  /* new_bd_SymConst_type */
308
309 static ir_node *
310 new_bd_Sync(dbg_info *db, ir_node *block) {
311         ir_node  *res;
312         ir_graph *irg = current_ir_graph;
313
314         res = new_ir_node(db, irg, block, op_Sync, mode_M, -1, NULL);
315         /* no need to call optimize node here, Sync are always created with no predecessors */
316         IRN_VRFY_IRG(res, irg);
317         return res;
318 }  /* new_bd_Sync */
319
320
321 static ir_node *
322 new_bd_EndReg(dbg_info *db, ir_node *block) {
323         ir_node  *res;
324         ir_graph *irg = current_ir_graph;
325
326         res = new_ir_node(db, irg, block, op_EndReg, mode_T, -1, NULL);
327         set_irg_end_reg(irg, res);
328         IRN_VRFY_IRG(res, irg);
329         return res;
330 }  /* new_bd_EndReg */
331
332 static ir_node *
333 new_bd_EndExcept(dbg_info *db, ir_node *block) {
334         ir_node  *res;
335         ir_graph *irg = current_ir_graph;
336
337         res = new_ir_node(db, irg, block, op_EndExcept, mode_T, -1, NULL);
338         set_irg_end_except(irg, res);
339         IRN_VRFY_IRG (res, irg);
340         return res;
341 }  /* new_bd_EndExcept */
342
343 static ir_node *
344 new_bd_ASM(dbg_info *db, ir_node *block, int arity, ir_node *in[], ir_asm_constraint *inputs,
345            int n_outs, ir_asm_constraint *outputs, int n_clobber, ident *clobber[], ident *asm_text) {
346         ir_node  *res;
347         ir_graph *irg = current_ir_graph;
348
349         res = new_ir_node(db, irg, block, op_ASM, mode_T, arity, in);
350         res->attr.assem.pin_state = op_pin_state_pinned;
351         res->attr.assem.inputs    = NEW_ARR_D(ir_asm_constraint, irg->obst, arity);
352         res->attr.assem.outputs   = NEW_ARR_D(ir_asm_constraint, irg->obst, n_outs);
353         res->attr.assem.clobber   = NEW_ARR_D(ident *, irg->obst, n_clobber);
354         res->attr.assem.asm_text  = asm_text;
355
356         memcpy(res->attr.assem.inputs,  inputs,  sizeof(inputs[0]) * arity);
357         memcpy(res->attr.assem.outputs, outputs, sizeof(outputs[0]) * n_outs);
358         memcpy(res->attr.assem.clobber, clobber, sizeof(clobber[0]) * n_clobber);
359
360         res = optimize_node(res);
361         IRN_VRFY_IRG(res, irg);
362         return res;
363 }  /* new_bd_ASM */
364
365 /* --------------------------------------------- */
366 /* private interfaces, for professional use only */
367 /* --------------------------------------------- */
368
369 ir_node *
370 new_rd_Start(dbg_info *db, ir_graph *irg, ir_node *block) {
371         ir_graph *rem = current_ir_graph;
372         ir_node  *res;
373
374         current_ir_graph = irg;
375         res = new_bd_Start(db, block);
376         current_ir_graph = rem;
377
378         return res;
379 }  /* new_rd_Start */
380
381 ir_node *
382 new_rd_End(dbg_info *db, ir_graph *irg, ir_node *block) {
383         ir_node  *res;
384         ir_graph *rem = current_ir_graph;
385
386         current_ir_graph = irg;
387         res = new_bd_End(db, block);
388         current_ir_graph = rem;
389
390         return res;
391 }  /* new_rd_End */
392
393 /* Creates a Phi node with all predecessors.  Calling this constructor
394    is only allowed if the corresponding block is mature.  */
395 ir_node *
396 new_rd_Phi(dbg_info *db, ir_graph *irg, ir_node *block, int arity, ir_node **in, ir_mode *mode) {
397         ir_node  *res;
398         ir_graph *rem = current_ir_graph;
399
400         current_ir_graph = irg;
401         res = new_bd_Phi(db, block,arity, in, mode);
402         current_ir_graph = rem;
403
404         return res;
405 }  /* new_rd_Phi */
406
407 ir_node *
408 new_rd_Const_type(dbg_info *db, ir_graph *irg, tarval *con, ir_type *tp) {
409         ir_node  *res;
410         ir_graph *rem = current_ir_graph;
411
412         current_ir_graph = irg;
413         res = new_bd_Const_type(db, con, tp);
414         current_ir_graph = rem;
415
416         return res;
417 }  /* new_rd_Const_type */
418
419 ir_node *
420 new_rd_Const(dbg_info *db, ir_graph *irg, tarval *con) {
421         ir_node  *res;
422 //#ifdef USE_ORIGINAL
423         ir_graph *rem = current_ir_graph;
424
425         current_ir_graph = irg;
426         res = new_bd_Const_type(db, con, firm_unknown_type);
427         current_ir_graph = rem;
428 //#else
429 //      res = new_rd_Const_type(db, irg, con, firm_unknown_type);
430 //#endif
431
432         return res;
433 }  /* new_rd_Const */
434
435 ir_node *
436 new_rd_Const_long(dbg_info *db, ir_graph *irg, ir_mode *mode, long value) {
437         return new_rd_Const(db, irg, new_tarval_from_long(value, mode));
438 }  /* new_rd_Const_long */
439
440 ir_node *
441 new_rd_defaultProj(dbg_info *db, ir_graph *irg, ir_node *block, ir_node *arg,
442                    long max_proj) {
443         ir_node  *res;
444         ir_graph *rem = current_ir_graph;
445
446         current_ir_graph = irg;
447         res = new_bd_defaultProj(db, block, arg, max_proj);
448         current_ir_graph = rem;
449
450         return res;
451 }  /* new_rd_defaultProj */
452
453 ir_node *
454 new_rd_simpleSel(dbg_info *db, ir_graph *irg, ir_node *block,
455                  ir_node *store, ir_node *objptr, ir_entity *ent) {
456         ir_node  *res;
457         ir_graph *rem = current_ir_graph;
458
459         current_ir_graph = irg;
460         res = new_bd_Sel(db, block, store, objptr, 0, NULL, ent);
461         current_ir_graph = rem;
462
463         return res;
464 }  /* new_rd_simpleSel */
465
466 ir_node *
467 new_rd_SymConst_type(dbg_info *db, ir_graph *irg, ir_node *block, ir_mode *mode,
468                      symconst_symbol value, symconst_kind symkind, ir_type *tp) {
469         ir_node  *res;
470         ir_graph *rem = current_ir_graph;
471
472         current_ir_graph = irg;
473         res = new_bd_SymConst_type(db, block, mode, value, symkind, tp);
474         current_ir_graph = rem;
475
476         return res;
477 }  /* new_rd_SymConst_type */
478
479 ir_node *
480 new_rd_SymConst(dbg_info *db, ir_graph *irg, ir_node *block, ir_mode *mode,
481                 symconst_symbol value, symconst_kind symkind) {
482         return new_rd_SymConst_type(db, irg, block, mode, value, symkind, firm_unknown_type);
483 }  /* new_rd_SymConst */
484
485  ir_node *new_rd_SymConst_addr_ent(dbg_info *db, ir_graph *irg, ir_mode *mode, ir_entity *symbol, ir_type *tp) {
486         symconst_symbol sym;
487         sym.entity_p = symbol;
488         return new_rd_SymConst_type(db, irg, get_irg_start_block(irg), mode, sym, symconst_addr_ent, tp);
489 }  /* new_rd_SymConst_addr_ent */
490
491 ir_node *new_rd_SymConst_ofs_ent(dbg_info *db, ir_graph *irg, ir_mode *mode, ir_entity *symbol, ir_type *tp) {
492         symconst_symbol sym;
493         sym.entity_p = symbol;
494         return new_rd_SymConst_type(db, irg, get_irg_start_block(irg), mode, sym, symconst_ofs_ent, tp);
495 }  /* new_rd_SymConst_ofs_ent */
496
497 ir_node *new_rd_SymConst_addr_name(dbg_info *db, ir_graph *irg, ir_mode *mode, ident *symbol, ir_type *tp) {
498         symconst_symbol sym;
499         sym.ident_p = symbol;
500         return new_rd_SymConst_type(db, irg, get_irg_start_block(irg), mode, sym, symconst_addr_name, tp);
501 }  /* new_rd_SymConst_addr_name */
502
503 ir_node *new_rd_SymConst_type_tag(dbg_info *db, ir_graph *irg, ir_mode *mode, ir_type *symbol, ir_type *tp) {
504         symconst_symbol sym;
505         sym.type_p = symbol;
506         return new_rd_SymConst_type(db, irg, get_irg_start_block(irg), mode, sym, symconst_type_tag, tp);
507 }  /* new_rd_SymConst_type_tag */
508
509 ir_node *new_rd_SymConst_size(dbg_info *db, ir_graph *irg, ir_mode *mode, ir_type *symbol, ir_type *tp) {
510         symconst_symbol sym;
511         sym.type_p = symbol;
512         return new_rd_SymConst_type(db, irg, get_irg_start_block(irg), mode, sym, symconst_type_size, tp);
513 }  /* new_rd_SymConst_size */
514
515 ir_node *new_rd_SymConst_align(dbg_info *db, ir_graph *irg, ir_mode *mode, ir_type *symbol, ir_type *tp) {
516         symconst_symbol sym;
517         sym.type_p = symbol;
518         return new_rd_SymConst_type(db, irg, get_irg_start_block(irg), mode, sym, symconst_type_align, tp);
519 }  /* new_rd_SymConst_align */
520
521 ir_node *
522 new_rd_Sync(dbg_info *db, ir_graph *irg, ir_node *block, int arity, ir_node *in[]) {
523         ir_node  *res;
524         ir_graph *rem = current_ir_graph;
525         int      i;
526
527         current_ir_graph = irg;
528         res = new_bd_Sync(db, block);
529         current_ir_graph = rem;
530
531         for (i = 0; i < arity; ++i)
532                 add_Sync_pred(res, in[i]);
533
534         return res;
535 }  /* new_rd_Sync */
536
537 ir_node *
538 new_rd_EndReg(dbg_info *db, ir_graph *irg, ir_node *block) {
539         ir_node *res;
540
541         res = new_ir_node(db, irg, block, op_EndReg, mode_T, -1, NULL);
542         set_irg_end_reg(irg, res);
543         IRN_VRFY_IRG(res, irg);
544         return res;
545 }  /* new_rd_EndReg */
546
547 ir_node *
548 new_rd_EndExcept(dbg_info *db, ir_graph *irg, ir_node *block) {
549         ir_node *res;
550
551         res = new_ir_node(db, irg, block, op_EndExcept, mode_T, -1, NULL);
552         set_irg_end_except(irg, res);
553         IRN_VRFY_IRG (res, irg);
554         return res;
555 }  /* new_rd_EndExcept */
556
557 ir_node *new_rd_ASM(dbg_info *db, ir_graph *irg, ir_node *block,
558                     int arity, ir_node *in[], ir_asm_constraint *inputs,
559                     int n_outs, ir_asm_constraint *outputs,
560                     int n_clobber, ident *clobber[], ident *asm_text) {
561         ir_node  *res;
562         ir_graph *rem = current_ir_graph;
563
564         current_ir_graph = irg;
565         res = new_bd_ASM(db, block, arity, in, inputs, n_outs, outputs, n_clobber, clobber, asm_text);
566         current_ir_graph = rem;
567
568         return res;
569 }  /* new_rd_ASM */
570
571 ir_node *new_r_Start(ir_graph *irg, ir_node *block) {
572         return new_rd_Start(NULL, irg, block);
573 }
574 ir_node *new_r_End(ir_graph *irg, ir_node *block) {
575         return new_rd_End(NULL, irg, block);
576 }
577 ir_node *new_r_Const(ir_graph *irg, tarval *con) {
578         return new_rd_Const(NULL, irg, con);
579 }
580 ir_node *new_r_Const_long(ir_graph *irg, ir_mode *mode, long value) {
581         return new_rd_Const_long(NULL, irg, mode, value);
582 }
583 ir_node *new_r_Const_type(ir_graph *irg, tarval *con, ir_type *tp) {
584         return new_rd_Const_type(NULL, irg, con, tp);
585 }
586 ir_node *new_r_SymConst(ir_graph *irg, ir_node *block, ir_mode *mode,
587                         symconst_symbol value, symconst_kind symkind) {
588         return new_rd_SymConst(NULL, irg, block, mode, value, symkind);
589 }
590 ir_node *new_r_simpleSel(ir_graph *irg, ir_node *block, ir_node *store,
591                          ir_node *objptr, ir_entity *ent) {
592         return new_rd_Sel(NULL, irg, block, store, objptr, 0, NULL, ent);
593 }
594 ir_node *new_r_Phi(ir_graph *irg, ir_node *block, int arity,
595                    ir_node **in, ir_mode *mode) {
596         return new_rd_Phi(NULL, irg, block, arity, in, mode);
597 }
598 ir_node *new_r_Sync(ir_graph *irg, ir_node *block, int arity, ir_node *in[]) {
599         return new_rd_Sync(NULL, irg, block, arity, in);
600 }
601 ir_node *new_r_defaultProj(ir_graph *irg, ir_node *block, ir_node *arg,
602                            long max_proj) {
603         return new_rd_defaultProj(NULL, irg, block, arg, max_proj);
604 }
605 ir_node *new_r_Bad(ir_graph *irg) {
606         return get_irg_bad(irg);
607 }
608 ir_node *new_r_EndReg(ir_graph *irg, ir_node *block) {
609         return new_rd_EndReg(NULL, irg, block);
610 }
611 ir_node *new_r_EndExcept(ir_graph *irg, ir_node *block) {
612         return new_rd_EndExcept(NULL, irg, block);
613 }
614 ir_node *new_r_NoMem(ir_graph *irg) {
615         return get_irg_no_mem(irg);
616 }
617 ir_node *new_r_ASM(ir_graph *irg, ir_node *block,
618                    int arity, ir_node *in[], ir_asm_constraint *inputs,
619                    int n_outs, ir_asm_constraint *outputs,
620                    int n_clobber, ident *clobber[], ident *asm_text) {
621         return new_rd_ASM(NULL, irg, block, arity, in, inputs, n_outs, outputs, n_clobber, clobber, asm_text);
622 }
623
624 /** ********************/
625 /** public interfaces  */
626 /** construction tools */
627
628 ir_node *
629 new_d_Start(dbg_info *db) {
630         ir_node *res;
631
632         res = new_ir_node(db, current_ir_graph, current_ir_graph->current_block,
633                           op_Start, mode_T, 0, NULL);
634
635         res = optimize_node(res);
636         IRN_VRFY_IRG(res, current_ir_graph);
637         return res;
638 }  /* new_d_Start */
639
640 ir_node *
641 new_d_End(dbg_info *db) {
642         ir_node *res;
643         res = new_ir_node(db, current_ir_graph,  current_ir_graph->current_block,
644                           op_End, mode_X, -1, NULL);
645         res = optimize_node(res);
646         IRN_VRFY_IRG(res, current_ir_graph);
647
648         return res;
649 }  /* new_d_End */
650
651 /* ***********************************************************************/
652 /* Methods necessary for automatic Phi node creation                     */
653 /*
654   ir_node *phi_merge            (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
655   ir_node *get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
656   ir_node *new_rd_Phi0          (ir_graph *irg, ir_node *block, ir_mode *mode)
657   ir_node *new_rd_Phi_in        (ir_graph *irg, ir_node *block, ir_mode *mode, ir_node **in, int ins)
658
659   Call Graph:   ( A ---> B == A "calls" B)
660
661        get_value         mature_immBlock
662           |                   |
663           |                   |
664           |                   |
665           |          ---> phi_merge
666           |         /       /   \
667           |        /       /     \
668          \|/      /      |/_      \
669        get_r_value_internal        |
670                 |                  |
671                 |                  |
672                \|/                \|/
673            new_rd_Phi0          new_rd_Phi_in
674
675 * *************************************************************************** */
676
677 /** Creates a Phi node with 0 predecessors. */
678 static inline ir_node *
679 new_rd_Phi0(ir_graph *irg, ir_node *block, ir_mode *mode) {
680         ir_node *res;
681
682         res = new_ir_node(NULL, irg, block, op_Phi, mode, 0, NULL);
683         IRN_VRFY_IRG(res, irg);
684         return res;
685 }  /* new_rd_Phi0 */
686
687
688 /**
689  * Internal constructor of a Phi node by a phi_merge operation.
690  *
691  * @param irg    the graph on which the Phi will be constructed
692  * @param block  the block in which the Phi will be constructed
693  * @param mode   the mod eof the Phi node
694  * @param in     the input array of the phi node
695  * @param ins    number of elements in the input array
696  * @param phi0   in non-NULL: the Phi0 node in the same block that represents
697  *               the value for which the new Phi is constructed
698  */
699 static inline ir_node *
700 new_rd_Phi_in(ir_graph *irg, ir_node *block, ir_mode *mode,
701               ir_node **in, int ins, ir_node *phi0) {
702         int i;
703         ir_node *res, *known;
704
705         /* Allocate a new node on the obstack.  The allocation copies the in
706            array. */
707         res = new_ir_node(NULL, irg, block, op_Phi, mode, ins, in);
708         res->attr.phi.u.backedge = new_backedge_arr(irg->obst, ins);
709
710         /* This loop checks whether the Phi has more than one predecessor.
711            If so, it is a real Phi node and we break the loop.  Else the
712            Phi node merges the same definition on several paths and therefore
713            is not needed.
714            Note: We MUST consider Bad nodes, else we might get data flow cycles in dead loops! */
715         known = res;
716         for (i = ins - 1; i >= 0; --i)  {
717                 assert(in[i]);
718
719                 in[i] = skip_Id(in[i]);  /* increases the number of freed Phis. */
720
721                 /* Optimize self referencing Phis:  We can't detect them yet properly, as
722                 they still refer to the Phi0 they will replace.  So replace right now. */
723                 if (phi0 && in[i] == phi0)
724                         in[i] = res;
725
726                 if (in[i] == res || in[i] == known)
727                         continue;
728
729                 if (known == res)
730                         known = in[i];
731                 else
732                         break;
733         }
734
735         /* i < 0: there is at most one predecessor, we don't need a phi node. */
736         if (i < 0) {
737                 if (res != known) {
738                         edges_node_deleted(res, current_ir_graph);
739                         obstack_free(current_ir_graph->obst, res);
740                         if (is_Phi(known)) {
741                                 /* If pred is a phi node we want to optimize it: If loops are matured in a bad
742                                    order, an enclosing Phi know may get superfluous. */
743                                 res = optimize_in_place_2(known);
744                                 if (res != known)
745                                         exchange(known, res);
746                         }
747                         else
748                                 res = known;
749                 } else {
750                         /* A undefined value, e.g., in unreachable code. */
751                         res = new_Bad();
752                 }
753         } else {
754                 res = optimize_node(res);  /* This is necessary to add the node to the hash table for cse. */
755                 IRN_VRFY_IRG(res, irg);
756                 /* Memory Phis in endless loops must be kept alive.
757                    As we can't distinguish these easily we keep all of them alive. */
758                 if (is_Phi(res) && mode == mode_M)
759                         add_End_keepalive(get_irg_end(irg), res);
760         }
761
762         return res;
763 }  /* new_rd_Phi_in */
764
765 static ir_node *
766 get_r_value_internal(ir_node *block, int pos, ir_mode *mode);
767
768 static ir_node *
769 phi_merge(ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins);
770
771 /**
772  * Construct a new frag_array for node n.
773  * Copy the content from the current graph_arr of the corresponding block:
774  * this is the current state.
775  * Set ProjM(n) as current memory state.
776  * Further the last entry in frag_arr of current block points to n.  This
777  * constructs a chain block->last_frag_op-> ... first_frag_op of all frag ops in the block.
778  */
779 static inline ir_node **new_frag_arr(ir_node *n) {
780         ir_node **arr;
781         int opt;
782
783         arr = NEW_ARR_D (ir_node *, current_ir_graph->obst, current_ir_graph->n_loc);
784         memcpy(arr, current_ir_graph->current_block->attr.block.graph_arr,
785                sizeof(ir_node *)*current_ir_graph->n_loc);
786
787         /* turn off optimization before allocating Proj nodes, as res isn't
788            finished yet. */
789         opt = get_opt_optimize(); set_optimize(0);
790         /* Here we rely on the fact that all frag ops have Memory as first result! */
791         if (is_Call(n)) {
792                 arr[0] = new_Proj(n, mode_M, pn_Call_M_except);
793         } else if (is_CopyB(n)) {
794                 arr[0] = new_Proj(n, mode_M, pn_CopyB_M_except);
795         } else {
796                 assert((pn_Quot_M == pn_DivMod_M) &&
797                        (pn_Quot_M == pn_Div_M)    &&
798                        (pn_Quot_M == pn_Mod_M)    &&
799                        (pn_Quot_M == pn_Load_M)   &&
800                        (pn_Quot_M == pn_Store_M)  &&
801                        (pn_Quot_M == pn_Alloc_M)  &&
802                        (pn_Quot_M == pn_Bound_M));
803                 arr[0] = new_Proj(n, mode_M, pn_Alloc_M);
804         }
805         set_optimize(opt);
806
807         current_ir_graph->current_block->attr.block.graph_arr[current_ir_graph->n_loc-1] = n;
808         return arr;
809 }  /* new_frag_arr */
810
811 /**
812  * Returns the frag_arr from a node.
813  */
814 static inline ir_node **get_frag_arr(ir_node *n) {
815         switch (get_irn_opcode(n)) {
816         case iro_Call:
817                 return n->attr.call.exc.frag_arr;
818         case iro_Alloc:
819                 return n->attr.alloc.exc.frag_arr;
820         case iro_Load:
821                 return n->attr.load.exc.frag_arr;
822         case iro_Store:
823                 return n->attr.store.exc.frag_arr;
824         default:
825                 return n->attr.except.frag_arr;
826         }
827 }  /* get_frag_arr */
828
829 static void
830 set_frag_value(ir_node **frag_arr, int pos, ir_node *val) {
831 #ifdef DEBUG_libfirm
832         int i;
833
834         for (i = 1024; i >= 0; --i)
835 #else
836         for (;;)
837 #endif
838         {
839                 if (frag_arr[pos] == NULL)
840                         frag_arr[pos] = val;
841                 if (frag_arr[current_ir_graph->n_loc - 1] != NULL) {
842                         ir_node **arr = get_frag_arr(frag_arr[current_ir_graph->n_loc - 1]);
843                         assert(arr != frag_arr && "Endless recursion detected");
844                         frag_arr = arr;
845                 } else
846                         return;
847         }
848         assert(!"potential endless recursion in set_frag_value");
849 }  /* set_frag_value */
850
851 static ir_node *
852 get_r_frag_value_internal(ir_node *block, ir_node *cfOp, int pos, ir_mode *mode) {
853         ir_node *res;
854         ir_node **frag_arr;
855
856         assert(is_fragile_op(cfOp) && !is_Bad(cfOp));
857
858         frag_arr = get_frag_arr(cfOp);
859         res = frag_arr[pos];
860         if (res == NULL) {
861                 if (block->attr.block.graph_arr[pos] != NULL) {
862                         /* There was a set_value() after the cfOp and no get_value() before that
863                            set_value().  We must build a Phi node now. */
864                         if (block->attr.block.is_matured) {
865                                 int ins = get_irn_arity(block);
866                                 ir_node **nin;
867                                 NEW_ARR_A(ir_node *, nin, ins);
868                                 res = phi_merge(block, pos, mode, nin, ins);
869                         } else {
870                                 res = new_rd_Phi0(current_ir_graph, block, mode);
871                                 res->attr.phi.u.pos    = pos;
872                                 res->attr.phi.next     = block->attr.block.phis;
873                                 block->attr.block.phis = res;
874                         }
875                         assert(res != NULL);
876                         /* It's a Phi, we can write this into all graph_arrs with NULL */
877                         set_frag_value(block->attr.block.graph_arr, pos, res);
878                 } else {
879                         res = get_r_value_internal(block, pos, mode);
880                         set_frag_value(block->attr.block.graph_arr, pos, res);
881                 }
882         }
883         return res;
884 }  /* get_r_frag_value_internal */
885
886 /**
887  * Check whether a control flownode  cf_pred represents an exception flow.
888  *
889  * @param cf_pred     the control flow node
890  * @param prev_cf_op  if cf_pred is a Proj, the predecessor node, else equal to cf_pred
891  */
892 static int is_exception_flow(ir_node *cf_pred, ir_node *prev_cf_op) {
893         /*
894          * Note: all projections from a raise are "exceptional control flow" we we handle it
895          * like a normal Jmp, because there is no "regular" one.
896          * That's why Raise is no "fragile_op"!
897          */
898         if (is_fragile_op(prev_cf_op)) {
899                 if (is_Proj(cf_pred)) {
900                         if (get_Proj_proj(cf_pred) == pn_Generic_X_regular) {
901                                 /* the regular control flow, NO exception */
902                                 return 0;
903                         }
904                         assert(get_Proj_proj(cf_pred) == pn_Generic_X_except);
905                         return 1;
906                 }
907                 /* Hmm, exception but not a Proj? */
908                 assert(!"unexpected condition: fragile op without a proj");
909                 return 1;
910         }
911         return 0;
912 }  /* is_exception_flow */
913
914 /**
915  * Computes the predecessors for the real phi node, and then
916  * allocates and returns this node.  The routine called to allocate the
917  * node might optimize it away and return a real value.
918  * This function must be called with an in-array of proper size.
919  */
920 static ir_node *
921 phi_merge(ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins) {
922         ir_node *prevBlock, *res, *phi0, *phi0_all;
923         int i;
924
925         /* If this block has no value at pos create a Phi0 and remember it
926            in graph_arr to break recursions.
927            Else we may not set graph_arr as there a later value is remembered. */
928         phi0 = NULL;
929         if (block->attr.block.graph_arr[pos] == NULL) {
930                 ir_graph *irg = current_ir_graph;
931
932                 if (block == get_irg_start_block(irg)) {
933                         /* Collapsing to Bad tarvals is no good idea.
934                            So we call a user-supplied routine here that deals with this case as
935                            appropriate for the given language. Sorrily the only help we can give
936                            here is the position.
937
938                            Even if all variables are defined before use, it can happen that
939                            we get to the start block, if a Cond has been replaced by a tuple
940                            (bad, jmp).  In this case we call the function needlessly, eventually
941                            generating an non existent error.
942                            However, this SHOULD NOT HAPPEN, as bad control flow nodes are intercepted
943                            before recurring.
944                          */
945                         if (default_initialize_local_variable != NULL) {
946                                 ir_node *rem = get_cur_block();
947
948                                 set_cur_block(block);
949                                 block->attr.block.graph_arr[pos] = default_initialize_local_variable(irg, mode, pos - 1);
950                                 set_cur_block(rem);
951                         }
952                         else
953                                 block->attr.block.graph_arr[pos] = new_Unknown(mode);
954                         /* We don't need to care about exception ops in the start block.
955                            There are none by definition. */
956                         return block->attr.block.graph_arr[pos];
957                 } else {
958                         phi0 = new_rd_Phi0(irg, block, mode);
959                         block->attr.block.graph_arr[pos] = phi0;
960                         if (get_opt_precise_exc_context()) {
961                                 /* Set graph_arr for fragile ops.  Also here we should break recursion.
962                                    We could choose a cyclic path through an cfop.  But the recursion would
963                                    break at some point. */
964                                 set_frag_value(block->attr.block.graph_arr, pos, phi0);
965                         }
966                 }
967         }
968
969         /* This loop goes to all predecessor blocks of the block the Phi node
970            is in and there finds the operands of the Phi node by calling
971            get_r_value_internal.  */
972         for (i = 1; i <= ins; ++i) {
973                 ir_node *cf_pred = block->in[i];
974                 ir_node *prevCfOp = skip_Proj(cf_pred);
975                 assert(prevCfOp);
976                 if (is_Bad(prevCfOp)) {
977                         /* In case a Cond has been optimized we would get right to the start block
978                         with an invalid definition. */
979                         nin[i-1] = new_Bad();
980                         continue;
981                 }
982                 prevBlock = prevCfOp->in[0]; /* go past control flow op to prev block */
983                 assert(prevBlock);
984                 if (!is_Bad(prevBlock)) {
985                         if (get_opt_precise_exc_context() && is_exception_flow(cf_pred, prevCfOp)) {
986                                 assert(get_r_frag_value_internal(prevBlock, prevCfOp, pos, mode));
987                                 nin[i-1] = get_r_frag_value_internal(prevBlock, prevCfOp, pos, mode);
988                         } else
989                                 nin[i-1] = get_r_value_internal(prevBlock, pos, mode);
990                 } else {
991                         nin[i-1] = new_Bad();
992                 }
993         }
994
995         /* We want to pass the Phi0 node to the constructor: this finds additional
996            optimization possibilities.
997            The Phi0 node either is allocated in this function, or it comes from
998            a former call to get_r_value_internal(). In this case we may not yet
999            exchange phi0, as this is done in mature_immBlock(). */
1000         if (phi0 == NULL) {
1001                 phi0_all = block->attr.block.graph_arr[pos];
1002                 if (! is_Phi0(phi0_all)            ||
1003                     get_irn_arity(phi0_all) != 0   ||
1004                     get_nodes_block(phi0_all) != block)
1005                         phi0_all = NULL;
1006         } else {
1007                 phi0_all = phi0;
1008         }
1009
1010         /* After collecting all predecessors into the array nin a new Phi node
1011            with these predecessors is created.  This constructor contains an
1012            optimization: If all predecessors of the Phi node are identical it
1013            returns the only operand instead of a new Phi node.  */
1014         res = new_rd_Phi_in(current_ir_graph, block, mode, nin, ins, phi0_all);
1015
1016         /* In case we allocated a Phi0 node at the beginning of this procedure,
1017            we need to exchange this Phi0 with the real Phi. */
1018         if (phi0 != NULL) {
1019                 exchange(phi0, res);
1020                 block->attr.block.graph_arr[pos] = res;
1021                 /* Don't set_frag_value as it does not overwrite.  Doesn't matter, is
1022                    only an optimization. */
1023         }
1024
1025         return res;
1026 }  /* phi_merge */
1027
1028 /**
1029  * This function returns the last definition of a value.  In case
1030  * this value was last defined in a previous block, Phi nodes are
1031  * inserted.  If the part of the firm graph containing the definition
1032  * is not yet constructed, a dummy Phi node is returned.
1033  *
1034  * @param block   the current block
1035  * @param pos     the value number of the value searched
1036  * @param mode    the mode of this value (needed for Phi construction)
1037  */
1038 static ir_node *
1039 get_r_value_internal(ir_node *block, int pos, ir_mode *mode) {
1040         ir_node *res;
1041         /* There are 4 cases to treat.
1042
1043            1. The block is not mature and we visit it the first time.  We can not
1044               create a proper Phi node, therefore a Phi0, i.e., a Phi without
1045               predecessors is returned.  This node is added to the linked list (block
1046               attribute "phis") of the containing block to be completed when this block is
1047               matured. (Completion will add a new Phi and turn the Phi0 into an Id
1048               node.)
1049
1050            2. The value is already known in this block, graph_arr[pos] is set and we
1051               visit the block the first time.  We can return the value without
1052               creating any new nodes.
1053
1054            3. The block is mature and we visit it the first time.  A Phi node needs
1055               to be created (phi_merge).  If the Phi is not needed, as all it's
1056               operands are the same value reaching the block through different
1057               paths, it's optimized away and the value itself is returned.
1058
1059            4. The block is mature, and we visit it the second time.  Now two
1060               subcases are possible:
1061               * The value was computed completely the last time we were here. This
1062                 is the case if there is no loop.  We can return the proper value.
1063               * The recursion that visited this node and set the flag did not
1064                 return yet.  We are computing a value in a loop and need to
1065                 break the recursion.  This case only happens if we visited
1066             the same block with phi_merge before, which inserted a Phi0.
1067             So we return the Phi0.
1068         */
1069
1070         /* case 4 -- already visited. */
1071         if (get_irn_visited(block) == get_irg_visited(current_ir_graph)) {
1072                 /* As phi_merge allocates a Phi0 this value is always defined. Here
1073                 is the critical difference of the two algorithms. */
1074                 assert(block->attr.block.graph_arr[pos]);
1075                 return block->attr.block.graph_arr[pos];
1076         }
1077
1078         /* visited the first time */
1079         set_irn_visited(block, get_irg_visited(current_ir_graph));
1080
1081         /* Get the local valid value */
1082         res = block->attr.block.graph_arr[pos];
1083
1084         /* case 2 -- If the value is actually computed, return it. */
1085         if (res != NULL)
1086                 return res;
1087
1088         if (block->attr.block.is_matured) { /* case 3 */
1089
1090                 /* The Phi has the same amount of ins as the corresponding block. */
1091                 int ins = get_irn_arity(block);
1092                 ir_node **nin;
1093                 NEW_ARR_A(ir_node *, nin, ins);
1094
1095                 /* Phi merge collects the predecessors and then creates a node. */
1096                 res = phi_merge(block, pos, mode, nin, ins);
1097
1098         } else {  /* case 1 */
1099                 /* The block is not mature, we don't know how many in's are needed.  A Phi
1100                    with zero predecessors is created.  Such a Phi node is called Phi0
1101                    node.  The Phi0 is then added to the list of Phi0 nodes in this block
1102                    to be matured by mature_immBlock later.
1103                    The Phi0 has to remember the pos of it's internal value.  If the real
1104                    Phi is computed, pos is used to update the array with the local
1105                    values. */
1106                 res = new_rd_Phi0(current_ir_graph, block, mode);
1107                 res->attr.phi.u.pos    = pos;
1108                 res->attr.phi.next     = block->attr.block.phis;
1109                 block->attr.block.phis = res;
1110         }
1111
1112         assert(is_ir_node(res) && "phi_merge() failed to construct a definition");
1113
1114         /* The local valid value is available now. */
1115         block->attr.block.graph_arr[pos] = res;
1116
1117         return res;
1118 }  /* get_r_value_internal */
1119
1120 /* ************************************************************************** */
1121
1122 /*
1123  * Finalize a Block node, when all control flows are known.
1124  * Acceptable parameters are only Block nodes.
1125  */
1126 void
1127 mature_immBlock(ir_node *block) {
1128         int ins;
1129         ir_node *n, **nin;
1130         ir_node *next;
1131
1132         assert(is_Block(block));
1133         if (!get_Block_matured(block)) {
1134                 ir_graph *irg = current_ir_graph;
1135
1136                 ins = ARR_LEN(block->in) - 1;
1137                 /* Fix block parameters */
1138                 block->attr.block.backedge = new_backedge_arr(irg->obst, ins);
1139
1140                 /* An array for building the Phi nodes. */
1141                 NEW_ARR_A(ir_node *, nin, ins);
1142
1143                 /* Traverse a chain of Phi nodes attached to this block and mature
1144                 these, too. **/
1145                 for (n = block->attr.block.phis; n; n = next) {
1146                         inc_irg_visited(irg);
1147                         next = n->attr.phi.next;
1148                         exchange(n, phi_merge(block, n->attr.phi.u.pos, n->mode, nin, ins));
1149                 }
1150
1151                 block->attr.block.is_matured = 1;
1152
1153                 /* Now, as the block is a finished Firm node, we can optimize it.
1154                    Since other nodes have been allocated since the block was created
1155                    we can not free the node on the obstack.  Therefore we have to call
1156                    optimize_in_place().
1157                    Unfortunately the optimization does not change a lot, as all allocated
1158                    nodes refer to the unoptimized node.
1159                    We can call optimize_in_place_2(), as global cse has no effect on blocks. */
1160                 block = optimize_in_place_2(block);
1161                 IRN_VRFY_IRG(block, irg);
1162         }
1163 }  /* mature_immBlock */
1164
1165 ir_node *
1166 new_d_Phi(dbg_info *db, int arity, ir_node **in, ir_mode *mode) {
1167         return new_bd_Phi(db, current_ir_graph->current_block, arity, in, mode);
1168 }  /* new_d_Phi */
1169
1170 ir_node *
1171 new_d_Const(dbg_info *db, tarval *con) {
1172         return new_bd_Const(db, con);
1173 }  /* new_d_Const */
1174
1175 ir_node *
1176 new_d_Const_long(dbg_info *db, ir_mode *mode, long value) {
1177         return new_bd_Const_long(db, mode, value);
1178 }  /* new_d_Const_long */
1179
1180 ir_node *
1181 new_d_Const_type(dbg_info *db, tarval *con, ir_type *tp) {
1182         return new_bd_Const_type(db, con, tp);
1183 }  /* new_d_Const_type */
1184
1185
1186 ir_node *
1187 new_d_defaultProj(dbg_info *db, ir_node *arg, long max_proj) {
1188         ir_node *res;
1189         assert(arg->op == op_Cond);
1190         arg->attr.cond.kind = fragmentary;
1191         arg->attr.cond.default_proj = max_proj;
1192         res = new_d_Proj(db, arg, mode_X, max_proj);
1193         return res;
1194 }  /* new_d_defaultProj */
1195
1196 /**
1197  * Allocate a frag array for a node if the current graph state is phase_building.
1198  *
1199  * @param irn         the node for which the frag array should be allocated
1200  * @param op          the opcode of the (original) node, if does not match opcode of irn,
1201  *                    nothing is done
1202  * @param frag_store  the address of the frag store in irn attributes, if this
1203  *                    address contains a value != NULL, does nothing
1204  */
1205 void firm_alloc_frag_arr(ir_node *irn, ir_op *op, ir_node ***frag_store) {
1206         if (get_opt_precise_exc_context()) {
1207                 if ((current_ir_graph->phase_state == phase_building) &&
1208                     (get_irn_op(irn) == op) && /* Could be optimized away. */
1209                     !*frag_store)    /* Could be a cse where the arr is already set. */ {
1210                         *frag_store = new_frag_arr(irn);
1211                 }
1212         }
1213 }  /* firm_alloc_frag_arr */
1214
1215 ir_node *
1216 new_d_simpleSel(dbg_info *db, ir_node *store, ir_node *objptr, ir_entity *ent)
1217 /* GL: objptr was called frame before.  Frame was a bad choice for the name
1218    as the operand could as well be a pointer to a dynamic object. */
1219 {
1220         return new_bd_Sel(db, current_ir_graph->current_block,
1221                           store, objptr, 0, NULL, ent);
1222 }  /* new_d_simpleSel */
1223
1224 ir_node *
1225 new_d_SymConst_type(dbg_info *db, ir_mode *mode, symconst_symbol value, symconst_kind kind, ir_type *tp) {
1226         return new_bd_SymConst_type(db, get_irg_start_block(current_ir_graph), mode,
1227                                     value, kind, tp);
1228 }  /* new_d_SymConst_type */
1229
1230 ir_node *
1231 new_d_SymConst(dbg_info *db, ir_mode *mode, symconst_symbol value, symconst_kind kind) {
1232         return new_bd_SymConst_type(db, get_irg_start_block(current_ir_graph), mode,
1233                                     value, kind, firm_unknown_type);
1234 }  /* new_d_SymConst */
1235
1236 ir_node *
1237 new_d_Sync(dbg_info *db, int arity, ir_node *in[]) {
1238         return new_rd_Sync(db, current_ir_graph, current_ir_graph->current_block, arity, in);
1239 }  /* new_d_Sync */
1240
1241 ir_node *
1242 new_d_EndReg(dbg_info *db) {
1243         return new_bd_EndReg(db, current_ir_graph->current_block);
1244 }  /* new_d_EndReg */
1245
1246 ir_node *
1247 new_d_EndExcept(dbg_info *db) {
1248         return new_bd_EndExcept(db, current_ir_graph->current_block);
1249 }  /* new_d_EndExcept */
1250
1251
1252 ir_node *
1253 new_d_ASM(dbg_info *db, int arity, ir_node *in[], ir_asm_constraint *inputs,
1254           int n_outs, ir_asm_constraint *outputs,
1255           int n_clobber, ident *clobber[], ident *asm_text) {
1256         return new_bd_ASM(db, current_ir_graph->current_block, arity, in, inputs, n_outs, outputs, n_clobber, clobber, asm_text);
1257 }  /* new_d_ASM */
1258
1259 /* ********************************************************************* */
1260 /* Comfortable interface with automatic Phi node construction.           */
1261 /* (Uses also constructors of ?? interface, except new_Block.            */
1262 /* ********************************************************************* */
1263
1264 /*  Block construction */
1265 /* immature Block without predecessors */
1266 ir_node *
1267 new_d_immBlock(dbg_info *db) {
1268         ir_node *res;
1269
1270         assert(get_irg_phase_state(current_ir_graph) == phase_building);
1271         /* creates a new dynamic in-array as length of in is -1 */
1272         res = new_ir_node(db, current_ir_graph, NULL, op_Block, mode_BB, -1, NULL);
1273
1274         /* macroblock head */
1275         res->in[0] = res;
1276
1277         res->attr.block.is_matured  = 0;
1278         res->attr.block.is_dead     = 0;
1279         res->attr.block.is_mb_head  = 1;
1280         res->attr.block.irg.irg     = current_ir_graph;
1281         res->attr.block.backedge    = NULL;
1282         res->attr.block.in_cg       = NULL;
1283         res->attr.block.cg_backedge = NULL;
1284         res->attr.block.extblk      = NULL;
1285         res->attr.block.region      = NULL;
1286         res->attr.block.mb_depth    = 0;
1287         res->attr.block.entity      = NULL;
1288
1289         set_Block_block_visited(res, 0);
1290
1291         /* Create and initialize array for Phi-node construction. */
1292         res->attr.block.graph_arr = NEW_ARR_D(ir_node *, current_ir_graph->obst,
1293                                               current_ir_graph->n_loc);
1294         memset(res->attr.block.graph_arr, 0, sizeof(ir_node *)*current_ir_graph->n_loc);
1295
1296         /* Immature block may not be optimized! */
1297         IRN_VRFY_IRG(res, current_ir_graph);
1298
1299         return res;
1300 }  /* new_d_immBlock */
1301
1302 ir_node *
1303 new_immBlock(void) {
1304         return new_d_immBlock(NULL);
1305 }  /* new_immBlock */
1306
1307 /* immature PartBlock with its predecessors */
1308 ir_node *
1309 new_d_immPartBlock(dbg_info *db, ir_node *pred_jmp) {
1310         ir_node *res = new_d_immBlock(db);
1311         ir_node *blk = get_nodes_block(pred_jmp);
1312
1313         res->in[0] = blk->in[0];
1314         assert(res->in[0] != NULL);
1315         add_immBlock_pred(res, pred_jmp);
1316
1317         res->attr.block.is_mb_head = 0;
1318         res->attr.block.mb_depth = blk->attr.block.mb_depth + 1;
1319
1320         return res;
1321 }  /* new_d_immPartBlock */
1322
1323 ir_node *
1324 new_immPartBlock(ir_node *pred_jmp) {
1325         return new_d_immPartBlock(NULL, pred_jmp);
1326 }  /* new_immPartBlock */
1327
1328 /* add an edge to a jmp/control flow node */
1329 void
1330 add_immBlock_pred(ir_node *block, ir_node *jmp) {
1331         int n = ARR_LEN(block->in) - 1;
1332
1333         assert(!block->attr.block.is_matured && "Error: Block already matured!\n");
1334         assert(block->attr.block.is_mb_head && "Error: Cannot add a predecessor to a PartBlock");
1335         assert(is_ir_node(jmp));
1336
1337         ARR_APP1(ir_node *, block->in, jmp);
1338         /* Call the hook */
1339         hook_set_irn_n(block, n, jmp, NULL);
1340 }  /* add_immBlock_pred */
1341
1342 /* changing the current block */
1343 void
1344 set_cur_block(ir_node *target) {
1345         current_ir_graph->current_block = target;
1346 }  /* set_cur_block */
1347
1348 /* ************************ */
1349 /* parameter administration */
1350
1351 /* get a value from the parameter array from the current block by its index */
1352 ir_node *
1353 get_d_value(dbg_info *db, int pos, ir_mode *mode) {
1354         ir_graph *irg = current_ir_graph;
1355         assert(get_irg_phase_state(irg) == phase_building);
1356         inc_irg_visited(irg);
1357         (void) db;
1358
1359         assert(pos >= 0);
1360
1361         return get_r_value_internal(irg->current_block, pos + 1, mode);
1362 }  /* get_d_value */
1363
1364 /* get a value from the parameter array from the current block by its index */
1365 ir_node *
1366 get_value(int pos, ir_mode *mode) {
1367         return get_d_value(NULL, pos, mode);
1368 }  /* get_value */
1369
1370 /* set a value at position pos in the parameter array from the current block */
1371 void
1372 set_value(int pos, ir_node *value) {
1373         ir_graph *irg = current_ir_graph;
1374         assert(get_irg_phase_state(irg) == phase_building);
1375         assert(pos >= 0);
1376         assert(pos+1 < irg->n_loc);
1377         assert(is_ir_node(value));
1378         irg->current_block->attr.block.graph_arr[pos + 1] = value;
1379 }  /* set_value */
1380
1381 /* Find the value number for a node in the current block.*/
1382 int
1383 find_value(ir_node *value) {
1384         int i;
1385         ir_node *bl = current_ir_graph->current_block;
1386
1387         for (i = ARR_LEN(bl->attr.block.graph_arr) - 1; i >= 1; --i)
1388                 if (bl->attr.block.graph_arr[i] == value)
1389                         return i - 1;
1390         return -1;
1391 }  /* find_value */
1392
1393 /* get the current store */
1394 ir_node *
1395 get_store(void) {
1396         ir_graph *irg = current_ir_graph;
1397
1398         assert(get_irg_phase_state(irg) == phase_building);
1399         /* GL: one could call get_value instead */
1400         inc_irg_visited(irg);
1401         return get_r_value_internal(irg->current_block, 0, mode_M);
1402 }  /* get_store */
1403
1404 /* set the current store: handles automatic Sync construction for Load nodes */
1405 void
1406 set_store(ir_node *store) {
1407         ir_node *load, *pload, *pred, *in[2];
1408
1409         assert(get_irg_phase_state(current_ir_graph) == phase_building);
1410         /* Beware: due to dead code elimination, a store might become a Bad node even in
1411            the construction phase. */
1412         assert((get_irn_mode(store) == mode_M || is_Bad(store)) && "storing non-memory node");
1413
1414         if (get_opt_auto_create_sync()) {
1415                 /* handle non-volatile Load nodes by automatically creating Sync's */
1416                 load = skip_Proj(store);
1417                 if (is_Load(load) && get_Load_volatility(load) == volatility_non_volatile) {
1418                         pred = get_Load_mem(load);
1419
1420                         if (is_Sync(pred)) {
1421                                 /* a Load after a Sync: move it up */
1422                                 ir_node *mem = skip_Proj(get_Sync_pred(pred, 0));
1423
1424                                 set_Load_mem(load, get_memop_mem(mem));
1425                                 add_Sync_pred(pred, store);
1426                                 store = pred;
1427                         } else {
1428                                 pload = skip_Proj(pred);
1429                                 if (is_Load(pload) && get_Load_volatility(pload) == volatility_non_volatile) {
1430                                         /* a Load after a Load: create a new Sync */
1431                                         set_Load_mem(load, get_Load_mem(pload));
1432
1433                                         in[0] = pred;
1434                                         in[1] = store;
1435                                         store = new_Sync(2, in);
1436                                 }
1437                         }
1438                 }
1439         }
1440         current_ir_graph->current_block->attr.block.graph_arr[0] = store;
1441 }  /* set_store */
1442
1443 void
1444 keep_alive(ir_node *ka) {
1445         add_End_keepalive(get_irg_end(current_ir_graph), ka);
1446 }  /* keep_alive */
1447
1448 /* --- Useful access routines --- */
1449 /* Returns the current block of the current graph.  To set the current
1450    block use set_cur_block. */
1451 ir_node *get_cur_block(void) {
1452         return get_irg_current_block(current_ir_graph);
1453 }  /* get_cur_block */
1454
1455 /* Returns the frame type of the current graph */
1456 ir_type *get_cur_frame_type(void) {
1457         return get_irg_frame_type(current_ir_graph);
1458 }  /* get_cur_frame_type */
1459
1460
1461 /* ********************************************************************* */
1462 /* initialize */
1463
1464 /* call once for each run of the library */
1465 void
1466 firm_init_cons(uninitialized_local_variable_func_t *func) {
1467         default_initialize_local_variable = func;
1468 }  /* firm_init_cons */
1469
1470 void
1471 irp_finalize_cons(void) {
1472         int i;
1473         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1474                 irg_finalize_cons(get_irp_irg(i));
1475         }
1476         irp->phase_state = phase_high;
1477 }  /* irp_finalize_cons */
1478
1479 ir_node *new_Start(void) {
1480         return new_d_Start(NULL);
1481 }
1482 ir_node *new_End(void) {
1483         return new_d_End(NULL);
1484 }
1485 ir_node *new_Const(tarval *con) {
1486         return new_d_Const(NULL, con);
1487 }
1488
1489 ir_node *new_Const_long(ir_mode *mode, long value) {
1490         return new_d_Const_long(NULL, mode, value);
1491 }
1492
1493 ir_node *new_Const_type(tarval *con, ir_type *tp) {
1494         return new_d_Const_type(NULL, con, tp);
1495 }
1496
1497 ir_node *new_SymConst_type(ir_mode *mode, symconst_symbol value, symconst_kind kind, ir_type *type) {
1498         return new_d_SymConst_type(NULL, mode, value, kind, type);
1499 }
1500 ir_node *new_SymConst(ir_mode *mode, symconst_symbol value, symconst_kind kind) {
1501         return new_d_SymConst(NULL, mode, value, kind);
1502 }
1503 ir_node *new_simpleSel(ir_node *store, ir_node *objptr, ir_entity *ent) {
1504         return new_d_simpleSel(NULL, store, objptr, ent);
1505 }
1506 ir_node *new_Phi(int arity, ir_node **in, ir_mode *mode) {
1507         return new_d_Phi(NULL, arity, in, mode);
1508 }
1509 ir_node *new_Sync(int arity, ir_node *in[]) {
1510         return new_d_Sync(NULL, arity, in);
1511 }
1512 ir_node *new_defaultProj(ir_node *arg, long max_proj) {
1513         return new_d_defaultProj(NULL, arg, max_proj);
1514 }
1515 ir_node *new_Bad(void) {
1516         return get_irg_bad(current_ir_graph);
1517 }
1518 ir_node *new_EndReg(void) {
1519         return new_d_EndReg(NULL);
1520 }
1521 ir_node *new_EndExcept(void) {
1522         return new_d_EndExcept(NULL);
1523 }
1524 ir_node *new_NoMem(void) {
1525         return get_irg_no_mem(current_ir_graph);
1526 }
1527 ir_node *new_ASM(int arity, ir_node *in[], ir_asm_constraint *inputs,
1528                  int n_outs, ir_asm_constraint *outputs,
1529                  int n_clobber, ident *clobber[], ident *asm_text) {
1530         return new_d_ASM(NULL, arity, in, inputs, n_outs, outputs, n_clobber, clobber, asm_text);
1531 }
1532
1533 /* create a new anchor node */
1534 ir_node *new_Anchor(ir_graph *irg) {
1535         ir_node *in[anchor_last];
1536         memset(in, 0, sizeof(in));
1537         return new_ir_node(NULL, irg, NULL, op_Anchor, mode_ANY, anchor_last, in);
1538 }