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