2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Reverse edges that reference types/entities.
23 * @author Goetz Lindenmaier
40 /*------------------------------------------------------------------*/
41 /* We represent the fields in entities/types by hashmaps. */
42 /*------------------------------------------------------------------*/
44 static pmap *entity_access_map = NULL;
45 static pmap *entity_reference_map = NULL;
46 static pmap *type_alloc_map = NULL;
47 static pmap *type_cast_map = NULL;
48 static pmap *type_pointertype_map = NULL;
49 static pmap *type_arraytype_map = NULL;
52 * Return a flexible array containing all IR-nodes
53 * that access a given entity.
55 static ir_node **get_entity_access_array(const ir_entity *ent)
58 if (!entity_access_map) entity_access_map = pmap_create();
60 if (pmap_contains(entity_access_map, ent)) {
61 res = (ir_node **) pmap_get(entity_access_map, ent);
63 res = NEW_ARR_F(ir_node *, 0);
64 pmap_insert(entity_access_map, ent, (void *)res);
70 static void set_entity_access_array(const ir_entity *ent, ir_node **accs)
72 ir_node **old = pmap_get(entity_access_map, ent);
74 pmap_insert(entity_access_map, ent, (void *)accs);
78 * Return a flexible array containing all IR-nodes
79 * that reference a given entity.
81 static ir_node **get_entity_reference_array(const ir_entity *ent)
84 if (!entity_reference_map) entity_reference_map = pmap_create();
86 if (pmap_contains(entity_reference_map, ent)) {
87 res = (ir_node **) pmap_get(entity_reference_map, ent);
89 res = NEW_ARR_F(ir_node *, 0);
90 pmap_insert(entity_reference_map, ent, (void *)res);
96 static void set_entity_reference_array(const ir_entity *ent, ir_node **refs)
98 ir_node **old = pmap_get(entity_reference_map, ent);
100 pmap_insert(entity_reference_map, ent, (void *)refs);
104 * Return a flexible array containing all IR-nodes
105 * that allocate a given type.
107 static ir_node **get_type_alloc_array(const ir_type *tp)
110 if (!type_alloc_map) type_alloc_map = pmap_create();
112 if (pmap_contains(type_alloc_map, tp)) {
113 res = (ir_node **) pmap_get(type_alloc_map, tp);
115 res = NEW_ARR_F(ir_node *, 0);
116 pmap_insert(type_alloc_map, tp, (void *)res);
122 static void set_type_alloc_array(const ir_type *tp, ir_node **alls)
124 ir_node **old = pmap_get(type_alloc_map, tp);
126 pmap_insert(type_alloc_map, tp, (void *)alls);
130 * Return a flexible array containing all Cast-nodes
131 * that "create" a given type.
133 static ir_node **get_type_cast_array(const ir_type *tp)
136 if (!type_cast_map) type_cast_map = pmap_create();
138 if (pmap_contains(type_cast_map, tp)) {
139 res = (ir_node **) pmap_get(type_cast_map, tp);
141 res = NEW_ARR_F(ir_node *, 0);
142 pmap_insert(type_cast_map, tp, (void *)res);
147 static void set_type_cast_array(const ir_type *tp, ir_node **alls)
149 ir_node **old = pmap_get(type_cast_map, tp);
151 pmap_insert(type_cast_map, tp, (void *)alls);
155 * Return a flexible array containing all pointer
156 * types that points-to a given type.
158 static ir_type **get_type_pointertype_array(const ir_type *tp)
161 if (!type_pointertype_map) type_pointertype_map = pmap_create();
163 if (pmap_contains(type_pointertype_map, tp)) {
164 res = (ir_type **) pmap_get(type_pointertype_map, tp);
166 res = NEW_ARR_F(ir_type *, 0);
167 pmap_insert(type_pointertype_map, tp, (void *)res);
173 static void set_type_pointertype_array(const ir_type *tp, ir_type **pts)
175 ir_type **old = pmap_get(type_pointertype_map, tp);
177 pmap_insert(type_pointertype_map, tp, (void *)pts);
181 * Return a flexible array containing all array
182 * types that have a given type as element type.
184 static ir_type **get_type_arraytype_array(const ir_type *tp)
187 if (!type_arraytype_map) type_arraytype_map = pmap_create();
189 if (pmap_contains(type_arraytype_map, tp)) {
190 res = (ir_type **) pmap_get(type_arraytype_map, tp);
192 res = NEW_ARR_F(ir_type *, 0);
193 pmap_insert(type_arraytype_map, tp, (void *)res);
199 void set_type_arraytype_array(const ir_type *tp, ir_type **pts)
201 ir_type **old = pmap_get(type_arraytype_map, tp);
203 pmap_insert(type_arraytype_map, tp, (void *)pts);
206 /*------------------------------------------------------------------*/
207 /* Accessing the out data structures. */
208 /* These routines only work properly if firm is in state */
209 /* trouts_consistent or trouts_inconsistent. */
210 /*------------------------------------------------------------------*/
212 /**------------------------------------------------------------------*/
213 /* Access routines for entities */
214 /**------------------------------------------------------------------*/
216 int get_entity_n_accesses(const ir_entity *ent)
220 assert(ent && is_entity(ent));
222 accs = get_entity_access_array(ent);
223 return ARR_LEN(accs);
226 ir_node *get_entity_access(const ir_entity *ent, int pos)
230 assert(0 <= pos && pos < get_entity_n_accesses(ent));
232 accs = get_entity_access_array(ent);
236 static void add_entity_access(const ir_entity *ent, ir_node *n)
240 assert(ent && is_entity(ent));
241 assert(n && is_ir_node(n));
243 accs = get_entity_access_array(ent);
244 ARR_APP1(ir_node *, accs, n);
245 set_entity_access_array(ent, accs);
248 void set_entity_access(const ir_entity *ent, int pos, ir_node *n)
252 assert(0 <= pos && pos < get_entity_n_accesses(ent));
253 assert(n && is_ir_node(n));
255 accs = get_entity_access_array(ent);
259 /*------------------------------------------------------------------*/
261 int get_entity_n_references(const ir_entity *ent)
265 assert(ent && is_entity(ent));
267 refs = get_entity_reference_array(ent);
268 return ARR_LEN(refs);
271 ir_node *get_entity_reference(const ir_entity *ent, int pos)
275 assert(0 <= pos && pos < get_entity_n_references(ent));
277 refs = get_entity_reference_array(ent);
281 static void add_entity_reference(const ir_entity *ent, ir_node *n)
285 assert(ent && is_entity(ent));
286 assert(n && is_ir_node(n));
288 refs = get_entity_reference_array(ent);
289 ARR_APP1(ir_node *, refs, n);
290 set_entity_reference_array(ent, refs);
293 void set_entity_reference(const ir_entity *ent, int pos, ir_node *n)
297 assert(0 <= pos && pos < get_entity_n_references(ent));
298 assert(n && is_ir_node(n));
300 refs = get_entity_reference_array(ent);
305 /**------------------------------------------------------------------*/
306 /* Access routines for types */
307 /**------------------------------------------------------------------*/
309 /* Number of Alloc nodes that create an instance of this type */
310 int get_type_n_allocs(const ir_type *tp)
314 assert(tp && is_type(tp));
316 allocs = get_type_alloc_array(tp);
317 return ARR_LEN(allocs);
320 /* Alloc node that creates an instance of this type */
321 ir_node *get_type_alloc(const ir_type *tp, int pos)
324 assert(0 <= pos && pos < get_type_n_allocs(tp));
326 allocs = get_type_alloc_array(tp);
330 static void add_type_alloc(const ir_type *tp, ir_node *n)
334 assert(tp && is_type(tp));
335 assert(n && is_ir_node(n));
337 allocs = get_type_alloc_array(tp);
338 ARR_APP1(ir_node *, allocs, n);
339 set_type_alloc_array(tp, allocs);
342 void set_type_alloc(const ir_type *tp, int pos, ir_node *n)
346 assert(0 <= pos && pos < get_type_n_allocs(tp));
347 assert(n && is_ir_node(n));
349 allocs = get_type_alloc_array(tp);
353 /* Number of Cast nodes that create an instance of this type */
354 int get_type_n_casts(const ir_type *tp)
358 assert(tp && is_type(tp));
360 casts = get_type_cast_array(tp);
361 return ARR_LEN(casts);
365 int get_class_n_upcasts(const ir_type *clss)
367 int i, n_casts = get_type_n_casts(clss);
369 for (i = 0; i < n_casts; ++i) {
370 ir_node *cast = get_type_cast(clss, i);
371 if (is_Cast_upcast(cast))
377 int get_class_n_downcasts(const ir_type *clss)
379 int i, n_casts = get_type_n_casts(clss);
381 for (i = 0; i < n_casts; ++i) {
382 ir_node *cast = get_type_cast(clss, i);
383 if (is_Cast_downcast(cast))
389 /* Cast node that creates an instance of this type */
390 ir_node *get_type_cast(const ir_type *tp, int pos)
393 assert(0 <= pos && pos < get_type_n_casts(tp));
395 casts = get_type_cast_array(tp);
399 void add_type_cast(const ir_type *tp, ir_node *n)
403 assert(tp && is_type(tp));
404 assert(n && is_ir_node(n));
406 casts = get_type_cast_array(tp);
407 ARR_APP1(ir_node *, casts, n);
408 set_type_cast_array(tp, casts);
411 void set_type_cast(const ir_type *tp, int pos, ir_node *n)
415 assert(0 <= pos && pos < get_type_n_casts(tp));
416 assert(n && is_ir_node(n));
418 casts = get_type_cast_array(tp);
422 /*------------------------------------------------------------------*/
424 int get_type_n_pointertypes_to(const ir_type *tp)
428 assert(tp && is_type(tp));
430 pts = get_type_pointertype_array(tp);
434 ir_type *get_type_pointertype_to(const ir_type *tp, int pos)
438 assert(0 <= pos && pos < get_type_n_pointertypes_to(tp));
440 pts = get_type_pointertype_array(tp);
444 void add_type_pointertype_to(const ir_type *tp, ir_type *ptp)
448 assert(tp && is_type(tp));
449 assert(ptp && is_Pointer_type(ptp));
451 pts = get_type_pointertype_array(tp);
452 ARR_APP1(ir_node *, pts, ptp);
453 set_type_pointertype_array(tp, pts);
456 void set_type_pointertype_to(const ir_type *tp, int pos, ir_type *ptp)
460 assert(0 <= pos && pos < get_type_n_pointertypes_to(tp));
461 assert(ptp && is_Pointer_type(ptp));
463 pts = get_type_pointertype_array(tp);
468 /*------------------------------------------------------------------*/
470 int get_type_n_arraytypes_of(const ir_type *tp)
474 assert(tp && is_type(tp));
476 pts = get_type_arraytype_array(tp);
480 ir_type *get_type_arraytype_of(const ir_type *tp, int pos)
484 assert(0 <= pos && pos < get_type_n_arraytypes_of(tp));
486 pts = get_type_arraytype_array(tp);
490 void add_type_arraytype_of(const ir_type *tp, ir_type *atp)
494 assert(tp && is_type(tp));
495 assert(atp && is_Array_type(atp));
497 pts = get_type_arraytype_array(tp);
498 ARR_APP1(ir_node *, pts, atp);
499 set_type_arraytype_array(tp, pts);
502 void set_type_arraytype_of(const ir_type *tp, int pos, ir_type *atp)
506 assert(0 <= pos && pos < get_type_n_arraytypes_of(tp));
507 assert(atp && is_Array_type(atp));
509 pts = get_type_arraytype_array(tp);
513 /*------------------------------------------------------------------*/
514 /* Building and Removing the out datastructure */
515 /*------------------------------------------------------------------*/
517 /** Initialize the trouts handling. */
518 static void init_trouts(void)
522 /** The number of entities that can be accessed by this Sel node. */
523 static int get_Sel_n_accessed_entities(const ir_node *sel)
529 /** The entity that cat be accessed by this Sel node. */
530 static ir_entity *get_Sel_accessed_entity(const ir_node *sel)
532 return get_Sel_entity(sel);
535 /** An addr node is a SymConst or a Sel. */
536 static int get_addr_n_entities(const ir_node *addr)
538 switch (get_irn_opcode(addr)) {
540 /* Treat jack array sels? */
541 return get_Sel_n_accessed_entities(addr);
543 if (get_SymConst_kind(addr) == symconst_addr_ent)
551 /** An addr node is a SymConst or a Sel.
552 If Sel follow to outermost of compound. */
553 static ir_entity *get_addr_entity(const ir_node *addr, int pos)
558 switch (get_irn_opcode(addr)) {
560 /* Treat jack array sels? They are compounds! Follow to outermost entity. */
561 ptr = get_Sel_ptr(addr);
562 while (is_Sel(ptr)) {
564 ptr = get_Sel_ptr(addr);
566 assert(0 <= pos && pos < get_Sel_n_accessed_entities(addr));
567 return get_Sel_accessed_entity(addr);
569 if (get_SymConst_kind(addr) == symconst_addr_ent) {
571 return get_SymConst_entity(addr);
579 static void chain_accesses(ir_node *n, void *env)
586 add_type_alloc(get_Alloc_type(n), n);
588 } else if (is_Cast(n)) {
589 add_type_cast(get_Cast_type(n), n);
591 } else if (is_Sel(n)) {
592 add_entity_reference(get_Sel_entity(n), n);
594 } else if (is_SymConst_addr_ent(n)) {
595 add_entity_reference(get_SymConst_entity(n), n);
597 } else if (is_memop(n)) {
598 addr = get_memop_ptr(n);
599 } else if (is_Call(n)) {
600 addr = get_Call_ptr(n);
601 if (! is_Sel(addr)) return; /* Sels before Calls mean a Load / polymorphic Call. */
606 n_ents = get_addr_n_entities(addr); /* == 1 */
607 for (i = 0; i < n_ents; ++i) {
608 ir_entity *ent = get_addr_entity(addr, i);
610 add_entity_access(ent, n);
612 //add_unrecognized_access(n);
617 * Handle chain types (pointer, array) by adding them to
620 static void chain_types(ir_type *tp)
622 if (is_Pointer_type(tp)) {
623 add_type_pointertype_to(get_pointer_points_to_type(tp), tp);
624 } else if (is_Array_type(tp)) {
625 add_type_arraytype_of(get_array_element_type(tp), tp);
629 irg_outs_state get_trouts_state(void)
631 return irp->trouts_state;
634 void set_trouts_inconsistent(void)
636 if (irp->trouts_state == outs_consistent)
637 irp->trouts_state = outs_inconsistent;
640 /* compute the trouts data structures. */
641 void compute_trouts(void)
648 /* Compute outs for IR nodes. */
649 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
650 irg_walk_graph(get_irp_irg(i), NULL, chain_accesses, NULL);
652 walk_const_code(NULL, chain_accesses, NULL);
654 /* Compute outs for types */
655 for (i = get_irp_n_types() - 1; i >= 0; --i) {
656 chain_types(get_irp_type(i));
659 irp->trouts_state = outs_consistent;
662 void free_trouts(void)
664 if (entity_access_map) {
666 for (accs = (ir_node **)pmap_first(entity_access_map);
668 accs = (ir_node **)pmap_next(entity_access_map))
670 pmap_destroy(entity_access_map);
671 entity_access_map = NULL;
674 if (entity_reference_map) {
676 for (refs = (ir_node **)pmap_first(entity_reference_map);
678 refs = (ir_node **)pmap_next(entity_reference_map))
680 pmap_destroy(entity_reference_map);
681 entity_reference_map = NULL;
684 if (type_alloc_map) {
686 for (alls = (ir_node **)pmap_first(type_alloc_map);
688 alls = (ir_node **)pmap_next(type_alloc_map))
690 pmap_destroy(type_alloc_map);
691 type_alloc_map = NULL;
696 for (casts = (ir_node **)pmap_first(type_cast_map);
698 casts = (ir_node **)pmap_next(type_cast_map))
700 pmap_destroy(type_cast_map);
701 type_cast_map = NULL;
704 if (type_pointertype_map) {
706 for (pts = (ir_node **)pmap_first(type_pointertype_map);
708 pts = (ir_node **)pmap_next(type_pointertype_map))
710 pmap_destroy(type_pointertype_map);
711 type_pointertype_map = NULL;
714 if (type_arraytype_map) {
716 for (pts = (ir_node **)pmap_first(type_arraytype_map);
718 pts = (ir_node **)pmap_next(type_arraytype_map))
720 pmap_destroy(type_arraytype_map);
721 type_arraytype_map = NULL;
724 irp->trouts_state = outs_none;