- analysis part of an block merging optimization
[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_value_param_base(const ir_graph *irg) {
223         return get_irn_intra_n(irg->anchor, anchor_value_param_base);
224 }
225
226 static inline void
227 _set_irg_value_param_base(ir_graph *irg, ir_node *node) {
228         set_irn_n(irg->anchor, anchor_value_param_base, node);
229 }
230
231 static inline ir_node *
232 _get_irg_bad(const ir_graph *irg) {
233         return get_irn_intra_n(irg->anchor, anchor_bad);
234 }
235
236 static inline void
237 _set_irg_bad(ir_graph *irg, ir_node *node) {
238         set_irn_n(irg->anchor, anchor_bad, node);
239 }
240
241 static inline ir_node *
242 _get_irg_no_mem(const ir_graph *irg) {
243         return get_irn_intra_n(irg->anchor, anchor_no_mem);
244 }
245
246 static inline void
247 _set_irg_no_mem(ir_graph *irg, ir_node *node) {
248         set_irn_n(irg->anchor, anchor_no_mem, node);
249 }
250
251 static inline ir_node *
252 _get_irg_current_block(const ir_graph *irg) {
253         return irg->current_block;
254 }
255
256 static inline void
257 _set_irg_current_block(ir_graph *irg, ir_node *node) {
258         irg->current_block = node;
259 }
260
261 static inline ir_entity *
262 _get_irg_entity(const ir_graph *irg) {
263         assert(irg && irg->ent);
264         return irg->ent;
265 }
266
267 static inline void
268 _set_irg_entity(ir_graph *irg, ir_entity *ent) {
269         irg->ent = ent;
270 }
271
272 static inline ir_type *
273 _get_irg_frame_type(ir_graph *irg) {
274         assert(irg && irg->frame_type);
275         return irg->frame_type = skip_tid(irg->frame_type);
276 }
277
278 static inline void
279 _set_irg_frame_type(ir_graph *irg, ir_type *ftp) {
280         assert(is_frame_type(ftp));
281         irg->frame_type = ftp;
282 }
283
284 static inline struct obstack *
285 _get_irg_obstack(const ir_graph *irg) {
286         return irg->obst;
287 }
288
289
290 static inline irg_phase_state
291 _get_irg_phase_state(const ir_graph *irg) {
292         return irg->phase_state;
293 }
294
295 static inline void
296 _set_irg_phase_state(ir_graph *irg, irg_phase_state state) {
297         irg->phase_state = state;
298 }
299
300 static inline op_pin_state
301 _get_irg_pinned(const ir_graph *irg) {
302         return irg->irg_pinned_state;
303 }
304
305 static inline irg_outs_state
306 _get_irg_outs_state(const ir_graph *irg) {
307         return irg->outs_state;
308 }
309
310 static inline void
311 _set_irg_outs_inconsistent(ir_graph *irg) {
312         if (irg->outs_state == outs_consistent)
313                 irg->outs_state = outs_inconsistent;
314 }
315
316 static inline irg_extblk_state
317 _get_irg_extblk_state(const ir_graph *irg) {
318   return irg->extblk_state;
319 }
320
321 static inline void
322 _set_irg_extblk_inconsistent(ir_graph *irg) {
323         if (irg->extblk_state == extblk_valid)
324                 irg->extblk_state = extblk_invalid;
325 }
326
327 static inline irg_dom_state
328 _get_irg_dom_state(const ir_graph *irg) {
329         return irg->dom_state;
330 }
331
332 static inline irg_dom_state
333 _get_irg_postdom_state(const ir_graph *irg) {
334         return irg->pdom_state;
335 }
336
337 static inline void
338 _set_irg_doms_inconsistent(ir_graph *irg) {
339         if (irg->dom_state != dom_none)
340                 irg->dom_state = dom_inconsistent;
341         if (irg->pdom_state != dom_none)
342                 irg->pdom_state = dom_inconsistent;
343 }
344
345 static inline irg_loopinfo_state
346 _get_irg_loopinfo_state(const ir_graph *irg) {
347         return irg->loopinfo_state;
348 }
349
350 static inline void
351 _set_irg_loopinfo_state(ir_graph *irg, irg_loopinfo_state s) {
352   irg->loopinfo_state = s;
353 }
354
355 static inline void
356 _set_irg_loopinfo_inconsistent(ir_graph *irg) {
357         irg->loopinfo_state &= ~loopinfo_valid;
358 }
359
360 static inline void
361 _set_irg_pinned(ir_graph *irg, op_pin_state p) {
362         irg->irg_pinned_state = p;
363 }
364
365 static inline irg_callee_info_state
366 _get_irg_callee_info_state(const ir_graph *irg) {
367         return irg->callee_info_state;
368 }
369
370 static inline void
371 _set_irg_callee_info_state(ir_graph *irg, irg_callee_info_state s) {
372         irg_callee_info_state irp_state = get_irp_callee_info_state();
373
374         irg->callee_info_state = s;
375
376         /* I could compare ... but who knows? */
377         if ((irp_state == irg_callee_info_consistent)  ||
378             ((irp_state == irg_callee_info_inconsistent) && (s == irg_callee_info_none)))
379                 set_irp_callee_info_state(s);
380 }
381
382 static inline irg_inline_property
383 _get_irg_inline_property(const ir_graph *irg) {
384         return irg->inline_property;
385 }
386
387 static inline void
388 _set_irg_inline_property(ir_graph *irg, irg_inline_property s) {
389         irg->inline_property = s;
390 }
391
392 static inline unsigned
393 _get_irg_additional_properties(const ir_graph *irg) {
394         if (irg->additional_properties & mtp_property_inherited)
395                 return get_method_additional_properties(get_entity_type(irg->ent));
396         return irg->additional_properties;
397 }
398
399 static inline void
400 _set_irg_additional_properties(ir_graph *irg, unsigned mask) {
401         irg->additional_properties = mask & ~mtp_property_inherited;
402 }
403
404 static inline void
405 _set_irg_additional_property(ir_graph *irg, mtp_additional_property flag) {
406         unsigned prop = irg->additional_properties;
407
408         if (prop & mtp_property_inherited)
409                 prop = get_method_additional_properties(get_entity_type(irg->ent));
410         irg->additional_properties = prop | flag;
411 }
412
413 static inline void
414 _set_irg_link(ir_graph *irg, void *thing) {
415         irg->link = thing;
416 }
417
418 static inline void *
419 _get_irg_link(const ir_graph *irg) {
420         return irg->link;
421 }
422
423 static inline ir_visited_t
424 _get_irg_visited(const ir_graph *irg) {
425         return irg->visited;
426 }
427
428 static inline ir_visited_t
429 _get_irg_block_visited(const ir_graph *irg) {
430         return irg->block_visited;
431 }
432
433 static inline void
434 _set_irg_block_visited(ir_graph *irg, ir_visited_t visited) {
435         irg->block_visited = visited;
436 }
437
438 static inline void
439 _inc_irg_block_visited(ir_graph *irg) {
440         ++irg->block_visited;
441 }
442
443 static inline void
444 _dec_irg_block_visited(ir_graph *irg) {
445         --irg->block_visited;
446 }
447
448 static inline unsigned
449 _get_irg_estimated_node_cnt(const ir_graph *irg) {
450         return irg->estimated_node_count;
451 }
452
453 /* Return the floating point model of this graph. */
454 static inline unsigned
455 _get_irg_fp_model(const ir_graph *irg) {
456         return irg->fp_model;
457 }
458
459 /**
460  * Allocates a new idx in the irg for the node and adds the irn to the idx -> irn map.
461  * @param irg The graph.
462  * @param irn The node.
463  * @return    The index allocated for the node.
464  */
465 static inline unsigned irg_register_node_idx(ir_graph *irg, ir_node *irn) {
466         unsigned idx = irg->last_node_idx++;
467         if (idx >= (unsigned)ARR_LEN(irg->idx_irn_map))
468                 ARR_RESIZE(ir_node *, irg->idx_irn_map, idx + 1);
469
470         irg->idx_irn_map[idx] = irn;
471         return idx;
472 }
473
474 /**
475  * Kill a node from the irg. BEWARE: this kills
476  * all later created nodes.
477  */
478 static inline void
479 irg_kill_node(ir_graph *irg, ir_node *n) {
480         unsigned idx = get_irn_idx(n);
481         assert(idx + 1 == irg->last_node_idx);
482
483         if (idx + 1 == irg->last_node_idx)
484                 --irg->last_node_idx;
485         irg->idx_irn_map[idx] = NULL;
486         obstack_free(irg->obst, n);
487 }
488
489 /**
490  * Get the node for an index.
491  * @param irg The graph.
492  * @param idx The index you want the node for.
493  * @return    The node with that index or NULL, if there is no node with that index.
494  * @note      The node you got might be dead.
495  */
496 static inline ir_node *
497 get_idx_irn(ir_graph *irg, unsigned idx) {
498         assert(idx < (unsigned) ARR_LEN(irg->idx_irn_map));
499         return irg->idx_irn_map[idx];
500 }
501
502 /**
503  * Return the number of anchors in this graph.
504  */
505 static inline int
506 get_irg_n_anchors(const ir_graph *irg) {
507         return get_irn_arity(irg->anchor);
508 }
509
510 /**
511  * Return anchor for given index
512  */
513 static inline ir_node *
514 get_irg_anchor(const ir_graph *irg, int idx) {
515         return get_irn_intra_n(irg->anchor, idx);
516 }
517
518 /**
519  * Set anchor for given index
520  */
521 static inline void
522 set_irg_anchor(ir_graph *irg, int idx, ir_node *irn) {
523         set_irn_n(irg->anchor, idx, irn);
524 }
525
526 #ifdef INTERPROCEDURAL_VIEW
527 extern int firm_interprocedural_view;
528
529 static inline int
530 _get_interprocedural_view(void) {
531         return firm_interprocedural_view;
532 }
533
534 #define get_interprocedural_view()            _get_interprocedural_view()
535 #endif
536
537 #define is_ir_graph(thing)                    _is_ir_graph(thing)
538 #define get_irg_start_block(irg)              _get_irg_start_block(irg)
539 #define set_irg_start_block(irg, node)        _set_irg_start_block(irg, node)
540 #define get_irg_start(irg)                    _get_irg_start(irg)
541 #define set_irg_start(irg, node)              _set_irg_start(irg, node)
542 #define get_irg_end_block(irg)                _get_irg_end_block(irg)
543 #define set_irg_end_block(irg, node)          _set_irg_end_block(irg, node)
544 #define get_irg_end(irg)                      _get_irg_end(irg)
545 #define set_irg_end(irg, node)                _set_irg_end(irg, node)
546 #define get_irg_end_reg(irg)                  _get_irg_end_reg(irg)
547 #define set_irg_end_reg(irg, node)            _set_irg_end_reg(irg, node)
548 #define get_irg_end_except(irg)               _get_irg_end_except(irg)
549 #define set_irg_end_except(irg, node)         _set_irg_end_except(irg, node)
550 #define get_irg_initial_exec(irg)             _get_irg_initial_exec(irg)
551 #define set_irg_initial_exec(irg, node)       _set_irg_initial_exec(irg, node)
552 #define get_irg_frame(irg)                    _get_irg_frame(irg)
553 #define set_irg_frame(irg, node)              _set_irg_frame(irg, node)
554 #define get_irg_tls(irg)                      _get_irg_tls(irg)
555 #define set_irg_tls(irg, node)                _set_irg_tls(irg, node)
556 #define get_irg_initial_mem(irg)              _get_irg_initial_mem(irg)
557 #define set_irg_initial_mem(irg, node)        _set_irg_initial_mem(irg, node)
558 #define get_irg_args(irg)                     _get_irg_args(irg)
559 #define set_irg_args(irg, node)               _set_irg_args(irg, node)
560 #define get_irg_value_param_base(irg)         _get_irg_value_param_base(irg)
561 #define set_irg_value_param_base(irg, node)   _set_irg_value_param_base(irg, node)
562 #define get_irg_bad(irg)                      _get_irg_bad(irg)
563 #define set_irg_bad(irg, node)                _set_irg_bad(irg, node)
564 #define get_irg_no_mem(irg)                   _get_irg_no_mem(irg)
565 #define set_irn_no_mem(irg, node)             _set_irn_no_mem(irg, node)
566 #define get_irg_current_block(irg)            _get_irg_current_block(irg)
567 #define set_irg_current_block(irg, node)      _set_irg_current_block(irg, node)
568 #define get_irg_entity(irg)                   _get_irg_entity(irg)
569 #define set_irg_entity(irg, ent)              _set_irg_entity(irg, ent)
570 #define get_irg_frame_type(irg)               _get_irg_frame_type(irg)
571 #define set_irg_frame_type(irg, ftp)          _set_irg_frame_type(irg, ftp)
572 #define get_irg_obstack(irg)                  _get_irg_obstack(irg)
573 #define get_irg_phase_state(irg)              _get_irg_phase_state(irg)
574 #define set_irg_phase_state(irg, state)       _set_irg_phase_state(irg, state)
575 #define get_irg_pinned(irg)                   _get_irg_pinned(irg)
576 #define get_irg_outs_state(irg)               _get_irg_outs_state(irg)
577 #define set_irg_outs_inconsistent(irg)        _set_irg_outs_inconsistent(irg)
578 #define get_irg_extblk_state(irg)             _get_irg_extblk_state(irg)
579 #define set_irg_extblk_inconsistent(irg)      _set_irg_extblk_inconsistent(irg)
580 #define get_irg_dom_state(irg)                _get_irg_dom_state(irg)
581 #define get_irg_postdom_state(irg)            _get_irg_postdom_state(irg)
582 #define set_irg_doms_inconsistent(irg)        _set_irg_doms_inconsistent(irg)
583 #define get_irg_loopinfo_state(irg)           _get_irg_loopinfo_state(irg)
584 #define set_irg_loopinfo_state(irg, s)        _set_irg_loopinfo_state(irg, s)
585 #define set_irg_loopinfo_inconsistent(irg)    _set_irg_loopinfo_inconsistent(irg)
586 #define set_irg_pinned(irg, p)                _set_irg_pinned(irg, p)
587 #define get_irg_callee_info_state(irg)        _get_irg_callee_info_state(irg)
588 #define set_irg_callee_info_state(irg, s)     _set_irg_callee_info_state(irg, s)
589 #define get_irg_inline_property(irg)          _get_irg_inline_property(irg)
590 #define set_irg_inline_property(irg, s)       _set_irg_inline_property(irg, s)
591 #define get_irg_additional_properties(irg)    _get_irg_additional_properties(irg)
592 #define set_irg_additional_properties(irg, m) _set_irg_additional_properties(irg, m)
593 #define set_irg_additional_property(irg, f)   _set_irg_additional_property(irg, f)
594 #define set_irg_link(irg, thing)              _set_irg_link(irg, thing)
595 #define get_irg_link(irg)                     _get_irg_link(irg)
596 #define get_irg_visited(irg)                  _get_irg_visited(irg)
597 #define get_irg_block_visited(irg)            _get_irg_block_visited(irg)
598 #define set_irg_block_visited(irg, v)         _set_irg_block_visited(irg, v)
599 #define inc_irg_block_visited(irg)            _inc_irg_block_visited(irg)
600 #define dec_irg_block_visited(irg)            _dec_irg_block_visited(irg)
601 #define get_irg_estimated_node_cnt(irg)       _get_irg_estimated_node_cnt(irg)
602 #define get_irg_fp_model(irg)                 _get_irg_fp_model(irg)
603
604 #endif