- Introduce the concept of the graph state bitset: This is a set of flags that indica...
[libfirm] / ir / ir / irgraph_t.h
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    Entry point to the representation of procedure code -- internal header.
23  * @author   Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
24  * @version  $Id$
25  */
26 #ifndef FIRM_IR_IRGRAPH_T_H
27 #define FIRM_IR_IRGRAPH_T_H
28
29 #include "firm_types.h"
30 #include "irgraph.h"
31
32 #include "firm_common_t.h"
33 #include "irtypes.h"
34 #include "irprog.h"
35 #include "pseudo_irg.h"
36 #include "type_t.h"
37 #include "entity_t.h"
38 #include "iredgekinds.h"
39
40 #include "irloop.h"
41
42 #include "obst.h"
43 #include "pset.h"
44 #include "set.h"
45
46 /** Suffix that is added to every frame type. */
47 #define FRAME_TP_SUFFIX "frame_tp"
48
49 /**
50  * Initializes the graph construction module.
51  */
52 void firm_init_irgraph(void);
53
54 /**
55  * Set the number of locals for a given graph.
56  *
57  * @param irg    the graph
58  * @param n_loc  number of locals
59  */
60 void irg_set_nloc(ir_graph *res, int n_loc);
61
62 /**
63  * Internal constructor that does not add to irp_irgs or the like.
64  */
65 ir_graph *new_r_ir_graph(ir_entity *ent, int n_loc);
66
67 /**
68  * Make a rudimentary ir graph for the constant code.
69  * Must look like a correct irg, spare everything else.
70  */
71 ir_graph *new_const_code_irg(void);
72
73 /**
74  * Create a new graph that is a copy of a given one.
75  * Uses the link fields of the original graphs.
76  *
77  * @param irg  The graph that must be copied.
78  */
79 ir_graph *create_irg_copy(ir_graph *irg);
80
81 /**
82  * Set the op_pin_state_pinned state of a graph.
83  *
84  * @param irg     the IR graph
85  * @param p       new pin state
86  */
87 void set_irg_pinned(ir_graph *irg, op_pin_state p);
88
89 /** Returns the obstack associated with the graph. */
90 struct obstack *get_irg_obstack(const ir_graph *irg);
91
92 /**
93  * Returns true if the node n is allocated on the storage of graph irg.
94  *
95  * @param irg   the IR graph
96  * @param n the IR node
97  */
98 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n);
99
100 /*-------------------------------------------------------------------*/
101 /* inline functions for graphs                                       */
102 /*-------------------------------------------------------------------*/
103
104 static inline int
105 _is_ir_graph(const void *thing) {
106         return (get_kind(thing) == k_ir_graph);
107 }
108
109 /** Returns the start block of a graph. */
110 static inline ir_node *
111 _get_irg_start_block(const ir_graph *irg) {
112         return get_irn_intra_n(irg->anchor, anchor_start_block);
113 }
114
115 static inline void
116 _set_irg_start_block(ir_graph *irg, ir_node *node) {
117         set_irn_n(irg->anchor, anchor_start_block, node);
118 }
119
120 static inline ir_node *
121 _get_irg_start(const ir_graph *irg) {
122         return get_irn_intra_n(irg->anchor, anchor_start);
123 }
124
125 static inline void
126 _set_irg_start(ir_graph *irg, ir_node *node) {
127         set_irn_n(irg->anchor, anchor_start, node);
128 }
129
130 static inline ir_node *
131 _get_irg_end_block(const ir_graph *irg) {
132         return get_irn_intra_n(irg->anchor, anchor_end_block);
133 }
134
135 static inline void
136 _set_irg_end_block(ir_graph *irg, ir_node *node) {
137         set_irn_n(irg->anchor, -1, node);
138         set_irn_n(irg->anchor, anchor_end_block, node);
139 }
140
141 static inline ir_node *
142 _get_irg_end(const ir_graph *irg) {
143         return get_irn_intra_n(irg->anchor, anchor_end);
144 }
145
146 static inline void
147 _set_irg_end(ir_graph *irg, ir_node *node) {
148         set_irn_n(irg->anchor, anchor_end, node);
149 }
150
151 static inline ir_node *
152 _get_irg_end_reg(const ir_graph *irg) {
153         return get_irn_intra_n(irg->anchor, anchor_end_reg);
154 }
155
156 static inline void
157 _set_irg_end_reg(ir_graph *irg, ir_node *node) {
158         set_irn_n(irg->anchor, anchor_end_reg, node);
159 }
160
161 static inline ir_node *
162 _get_irg_end_except(const ir_graph *irg) {
163         return get_irn_intra_n(irg->anchor, anchor_end_except);
164 }
165
166 static inline void
167 _set_irg_end_except(ir_graph *irg, ir_node *node) {
168         set_irn_n(irg->anchor, anchor_end_except, node);
169 }
170
171 static inline ir_node *
172 _get_irg_initial_exec(const ir_graph *irg) {
173         return get_irn_intra_n(irg->anchor, anchor_initial_exec);
174 }
175
176 static inline void
177 _set_irg_initial_exec(ir_graph *irg, ir_node *node) {
178         set_irn_n(irg->anchor, anchor_initial_exec, node);
179 }
180
181 static inline ir_node *
182 _get_irg_frame(const ir_graph *irg) {
183         return get_irn_intra_n(irg->anchor, anchor_frame);
184 }
185
186 static inline void
187 _set_irg_frame(ir_graph *irg, ir_node *node) {
188         set_irn_n(irg->anchor, anchor_frame, node);
189 }
190
191 static inline ir_node *
192 _get_irg_tls(const ir_graph *irg) {
193         return get_irn_intra_n(irg->anchor, anchor_tls);
194 }
195
196 static inline void
197 _set_irg_tls(ir_graph *irg, ir_node *node) {
198         set_irn_n(irg->anchor, anchor_tls, node);
199 }
200
201 static inline ir_node *
202 _get_irg_initial_mem(const ir_graph *irg) {
203         return get_irn_intra_n(irg->anchor, anchor_initial_mem);
204 }
205
206 static inline void
207 _set_irg_initial_mem(ir_graph *irg, ir_node *node) {
208         set_irn_n(irg->anchor, anchor_initial_mem, node);
209 }
210
211 static inline ir_node *
212 _get_irg_args(const ir_graph *irg) {
213         return get_irn_intra_n(irg->anchor, anchor_args);
214 }
215
216 static inline void
217 _set_irg_args(ir_graph *irg, ir_node *node) {
218         set_irn_n(irg->anchor, anchor_args, node);
219 }
220
221 static inline ir_node *
222 _get_irg_bad(const ir_graph *irg) {
223         return get_irn_intra_n(irg->anchor, anchor_bad);
224 }
225
226 static inline void
227 _set_irg_bad(ir_graph *irg, ir_node *node) {
228         set_irn_n(irg->anchor, anchor_bad, node);
229 }
230
231 static inline ir_node *
232 _get_irg_no_mem(const ir_graph *irg) {
233         return get_irn_intra_n(irg->anchor, anchor_no_mem);
234 }
235
236 static inline void
237 _set_irg_no_mem(ir_graph *irg, ir_node *node) {
238         set_irn_n(irg->anchor, anchor_no_mem, node);
239 }
240
241 static inline ir_node *
242 _get_irg_current_block(const ir_graph *irg) {
243         return irg->current_block;
244 }
245
246 static inline void
247 _set_irg_current_block(ir_graph *irg, ir_node *node) {
248         irg->current_block = node;
249 }
250
251 static inline ir_entity *
252 _get_irg_entity(const ir_graph *irg) {
253         assert(irg && irg->ent);
254         return irg->ent;
255 }
256
257 static inline void
258 _set_irg_entity(ir_graph *irg, ir_entity *ent) {
259         irg->ent = ent;
260 }
261
262 static inline ir_type *
263 _get_irg_frame_type(ir_graph *irg) {
264         assert(irg && irg->frame_type);
265         return irg->frame_type = skip_tid(irg->frame_type);
266 }
267
268 static inline void
269 _set_irg_frame_type(ir_graph *irg, ir_type *ftp) {
270         assert(is_frame_type(ftp));
271         irg->frame_type = ftp;
272 }
273
274 static inline struct obstack *
275 _get_irg_obstack(const ir_graph *irg) {
276         return irg->obst;
277 }
278
279
280 static inline irg_phase_state
281 _get_irg_phase_state(const ir_graph *irg) {
282         return irg->phase_state;
283 }
284
285 static inline void
286 _set_irg_phase_state(ir_graph *irg, irg_phase_state state) {
287         irg->phase_state = state;
288 }
289
290 static inline op_pin_state
291 _get_irg_pinned(const ir_graph *irg) {
292         return irg->irg_pinned_state;
293 }
294
295 static inline irg_outs_state
296 _get_irg_outs_state(const ir_graph *irg) {
297         return irg->outs_state;
298 }
299
300 static inline void
301 _set_irg_outs_inconsistent(ir_graph *irg) {
302         if (irg->outs_state == outs_consistent)
303                 irg->outs_state = outs_inconsistent;
304 }
305
306 static inline irg_extblk_state
307 _get_irg_extblk_state(const ir_graph *irg) {
308   return irg->extblk_state;
309 }
310
311 static inline void
312 _set_irg_extblk_inconsistent(ir_graph *irg) {
313         if (irg->extblk_state == extblk_valid)
314                 irg->extblk_state = extblk_invalid;
315 }
316
317 static inline irg_dom_state
318 _get_irg_dom_state(const ir_graph *irg) {
319         return irg->dom_state;
320 }
321
322 static inline irg_dom_state
323 _get_irg_postdom_state(const ir_graph *irg) {
324         return irg->pdom_state;
325 }
326
327 static inline void
328 _set_irg_doms_inconsistent(ir_graph *irg) {
329         if (irg->dom_state != dom_none)
330                 irg->dom_state = dom_inconsistent;
331         if (irg->pdom_state != dom_none)
332                 irg->pdom_state = dom_inconsistent;
333 }
334
335 static inline irg_loopinfo_state
336 _get_irg_loopinfo_state(const ir_graph *irg) {
337         return irg->loopinfo_state;
338 }
339
340 static inline void
341 _set_irg_loopinfo_state(ir_graph *irg, irg_loopinfo_state s) {
342   irg->loopinfo_state = s;
343 }
344
345 static inline void
346 _set_irg_loopinfo_inconsistent(ir_graph *irg) {
347         irg->loopinfo_state &= ~loopinfo_valid;
348 }
349
350 static inline void
351 _set_irg_pinned(ir_graph *irg, op_pin_state p) {
352         irg->irg_pinned_state = p;
353 }
354
355 static inline irg_callee_info_state
356 _get_irg_callee_info_state(const ir_graph *irg) {
357         return irg->callee_info_state;
358 }
359
360 static inline void
361 _set_irg_callee_info_state(ir_graph *irg, irg_callee_info_state s) {
362         irg_callee_info_state irp_state = get_irp_callee_info_state();
363
364         irg->callee_info_state = s;
365
366         /* I could compare ... but who knows? */
367         if ((irp_state == irg_callee_info_consistent)  ||
368             ((irp_state == irg_callee_info_inconsistent) && (s == irg_callee_info_none)))
369                 set_irp_callee_info_state(s);
370 }
371
372 static inline irg_inline_property
373 _get_irg_inline_property(const ir_graph *irg) {
374         return irg->inline_property;
375 }
376
377 static inline void
378 _set_irg_inline_property(ir_graph *irg, irg_inline_property s) {
379         irg->inline_property = s;
380 }
381
382 static inline unsigned
383 _get_irg_additional_properties(const ir_graph *irg) {
384         if (irg->additional_properties & mtp_property_inherited)
385                 return get_method_additional_properties(get_entity_type(irg->ent));
386         return irg->additional_properties;
387 }
388
389 static inline void
390 _set_irg_additional_properties(ir_graph *irg, unsigned mask) {
391         irg->additional_properties = mask & ~mtp_property_inherited;
392 }
393
394 static inline void
395 _set_irg_additional_property(ir_graph *irg, mtp_additional_property flag) {
396         unsigned prop = irg->additional_properties;
397
398         if (prop & mtp_property_inherited)
399                 prop = get_method_additional_properties(get_entity_type(irg->ent));
400         irg->additional_properties = prop | flag;
401 }
402
403 static inline void
404 _set_irg_link(ir_graph *irg, void *thing) {
405         irg->link = thing;
406 }
407
408 static inline void *
409 _get_irg_link(const ir_graph *irg) {
410         return irg->link;
411 }
412
413 static inline ir_visited_t
414 _get_irg_visited(const ir_graph *irg) {
415         return irg->visited;
416 }
417
418 static inline ir_visited_t
419 _get_irg_block_visited(const ir_graph *irg) {
420         return irg->block_visited;
421 }
422
423 static inline void
424 _set_irg_block_visited(ir_graph *irg, ir_visited_t visited) {
425         irg->block_visited = visited;
426 }
427
428 static inline void
429 _inc_irg_block_visited(ir_graph *irg) {
430         ++irg->block_visited;
431 }
432
433 static inline void
434 _dec_irg_block_visited(ir_graph *irg) {
435         --irg->block_visited;
436 }
437
438 static inline unsigned
439 _get_irg_estimated_node_cnt(const ir_graph *irg) {
440         return irg->estimated_node_count;
441 }
442
443 /* Return the floating point model of this graph. */
444 static inline unsigned
445 _get_irg_fp_model(const ir_graph *irg) {
446         return irg->fp_model;
447 }
448
449 static inline void _set_irg_state(ir_graph *irg, ir_graph_state_t state)
450 {
451         irg->state |= state;
452 }
453
454 static inline void _clear_irg_state(ir_graph *irg, ir_graph_state_t state)
455 {
456         irg->state &= ~state;
457 }
458
459 static inline int _is_irg_state(const ir_graph *irg, ir_graph_state_t state)
460 {
461         return (irg->state & state) == state;
462 }
463
464 /**
465  * Allocates a new idx in the irg for the node and adds the irn to the idx -> irn map.
466  * @param irg The graph.
467  * @param irn The node.
468  * @return    The index allocated for the node.
469  */
470 static inline unsigned irg_register_node_idx(ir_graph *irg, ir_node *irn) {
471         unsigned idx = irg->last_node_idx++;
472         if (idx >= (unsigned)ARR_LEN(irg->idx_irn_map))
473                 ARR_RESIZE(ir_node *, irg->idx_irn_map, idx + 1);
474
475         irg->idx_irn_map[idx] = irn;
476         return idx;
477 }
478
479 /**
480  * Kill a node from the irg. BEWARE: this kills
481  * all later created nodes.
482  */
483 static inline void
484 irg_kill_node(ir_graph *irg, ir_node *n) {
485         unsigned idx = get_irn_idx(n);
486         assert(idx + 1 == irg->last_node_idx);
487
488         if (idx + 1 == irg->last_node_idx)
489                 --irg->last_node_idx;
490         irg->idx_irn_map[idx] = NULL;
491         obstack_free(irg->obst, n);
492 }
493
494 /**
495  * Get the node for an index.
496  * @param irg The graph.
497  * @param idx The index you want the node for.
498  * @return    The node with that index or NULL, if there is no node with that index.
499  * @note      The node you got might be dead.
500  */
501 static inline ir_node *
502 _get_idx_irn(ir_graph *irg, unsigned idx) {
503         assert(idx < (unsigned) ARR_LEN(irg->idx_irn_map));
504         return irg->idx_irn_map[idx];
505 }
506
507 /**
508  * Return the number of anchors in this graph.
509  */
510 static inline int
511 get_irg_n_anchors(const ir_graph *irg) {
512         return get_irn_arity(irg->anchor);
513 }
514
515 /**
516  * Return anchor for given index
517  */
518 static inline ir_node *
519 get_irg_anchor(const ir_graph *irg, int idx) {
520         return get_irn_intra_n(irg->anchor, idx);
521 }
522
523 /**
524  * Set anchor for given index
525  */
526 static inline void
527 set_irg_anchor(ir_graph *irg, int idx, ir_node *irn) {
528         set_irn_n(irg->anchor, idx, irn);
529 }
530
531 #ifdef INTERPROCEDURAL_VIEW
532 extern int firm_interprocedural_view;
533
534 static inline int
535 _get_interprocedural_view(void) {
536         return firm_interprocedural_view;
537 }
538
539 #define get_interprocedural_view()            _get_interprocedural_view()
540 #endif
541
542 #define is_ir_graph(thing)                    _is_ir_graph(thing)
543 #define get_irg_start_block(irg)              _get_irg_start_block(irg)
544 #define set_irg_start_block(irg, node)        _set_irg_start_block(irg, node)
545 #define get_irg_start(irg)                    _get_irg_start(irg)
546 #define set_irg_start(irg, node)              _set_irg_start(irg, node)
547 #define get_irg_end_block(irg)                _get_irg_end_block(irg)
548 #define set_irg_end_block(irg, node)          _set_irg_end_block(irg, node)
549 #define get_irg_end(irg)                      _get_irg_end(irg)
550 #define set_irg_end(irg, node)                _set_irg_end(irg, node)
551 #define get_irg_end_reg(irg)                  _get_irg_end_reg(irg)
552 #define set_irg_end_reg(irg, node)            _set_irg_end_reg(irg, node)
553 #define get_irg_end_except(irg)               _get_irg_end_except(irg)
554 #define set_irg_end_except(irg, node)         _set_irg_end_except(irg, node)
555 #define get_irg_initial_exec(irg)             _get_irg_initial_exec(irg)
556 #define set_irg_initial_exec(irg, node)       _set_irg_initial_exec(irg, node)
557 #define get_irg_frame(irg)                    _get_irg_frame(irg)
558 #define set_irg_frame(irg, node)              _set_irg_frame(irg, node)
559 #define get_irg_tls(irg)                      _get_irg_tls(irg)
560 #define set_irg_tls(irg, node)                _set_irg_tls(irg, node)
561 #define get_irg_initial_mem(irg)              _get_irg_initial_mem(irg)
562 #define set_irg_initial_mem(irg, node)        _set_irg_initial_mem(irg, node)
563 #define get_irg_args(irg)                     _get_irg_args(irg)
564 #define set_irg_args(irg, node)               _set_irg_args(irg, node)
565 #define get_irg_bad(irg)                      _get_irg_bad(irg)
566 #define set_irg_bad(irg, node)                _set_irg_bad(irg, node)
567 #define get_irg_no_mem(irg)                   _get_irg_no_mem(irg)
568 #define set_irn_no_mem(irg, node)             _set_irn_no_mem(irg, node)
569 #define get_irg_current_block(irg)            _get_irg_current_block(irg)
570 #define set_irg_current_block(irg, node)      _set_irg_current_block(irg, node)
571 #define get_irg_entity(irg)                   _get_irg_entity(irg)
572 #define set_irg_entity(irg, ent)              _set_irg_entity(irg, ent)
573 #define get_irg_frame_type(irg)               _get_irg_frame_type(irg)
574 #define set_irg_frame_type(irg, ftp)          _set_irg_frame_type(irg, ftp)
575 #define get_irg_obstack(irg)                  _get_irg_obstack(irg)
576 #define get_irg_phase_state(irg)              _get_irg_phase_state(irg)
577 #define set_irg_phase_state(irg, state)       _set_irg_phase_state(irg, state)
578 #define get_irg_pinned(irg)                   _get_irg_pinned(irg)
579 #define get_irg_outs_state(irg)               _get_irg_outs_state(irg)
580 #define set_irg_outs_inconsistent(irg)        _set_irg_outs_inconsistent(irg)
581 #define get_irg_extblk_state(irg)             _get_irg_extblk_state(irg)
582 #define set_irg_extblk_inconsistent(irg)      _set_irg_extblk_inconsistent(irg)
583 #define get_irg_dom_state(irg)                _get_irg_dom_state(irg)
584 #define get_irg_postdom_state(irg)            _get_irg_postdom_state(irg)
585 #define set_irg_doms_inconsistent(irg)        _set_irg_doms_inconsistent(irg)
586 #define get_irg_loopinfo_state(irg)           _get_irg_loopinfo_state(irg)
587 #define set_irg_loopinfo_state(irg, s)        _set_irg_loopinfo_state(irg, s)
588 #define set_irg_loopinfo_inconsistent(irg)    _set_irg_loopinfo_inconsistent(irg)
589 #define set_irg_pinned(irg, p)                _set_irg_pinned(irg, p)
590 #define get_irg_callee_info_state(irg)        _get_irg_callee_info_state(irg)
591 #define set_irg_callee_info_state(irg, s)     _set_irg_callee_info_state(irg, s)
592 #define get_irg_inline_property(irg)          _get_irg_inline_property(irg)
593 #define set_irg_inline_property(irg, s)       _set_irg_inline_property(irg, s)
594 #define get_irg_additional_properties(irg)    _get_irg_additional_properties(irg)
595 #define set_irg_additional_properties(irg, m) _set_irg_additional_properties(irg, m)
596 #define set_irg_additional_property(irg, f)   _set_irg_additional_property(irg, f)
597 #define set_irg_link(irg, thing)              _set_irg_link(irg, thing)
598 #define get_irg_link(irg)                     _get_irg_link(irg)
599 #define get_irg_visited(irg)                  _get_irg_visited(irg)
600 #define get_irg_block_visited(irg)            _get_irg_block_visited(irg)
601 #define set_irg_block_visited(irg, v)         _set_irg_block_visited(irg, v)
602 #define inc_irg_block_visited(irg)            _inc_irg_block_visited(irg)
603 #define dec_irg_block_visited(irg)            _dec_irg_block_visited(irg)
604 #define get_irg_estimated_node_cnt(irg)       _get_irg_estimated_node_cnt(irg)
605 #define get_irg_fp_model(irg)                 _get_irg_fp_model(irg)
606 #define get_idx_irn(irg, idx)                 _get_idx_irn(irg, idx)
607 #define set_irg_state(irg, state)             _set_irg_state(irg, state)
608 #define clear_irg_state(irg, state)           _clear_irg_state(irg, state)
609 #define is_irg_state(irg, state)              _is_irg_state(irg, state)
610
611 #endif