2 * Copyright (C) 1995-2011 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
39 /*------------------------------------------------------------------*/
40 /* We represent the fields in entities/types by hashmaps. */
41 /*------------------------------------------------------------------*/
43 static pmap *entity_access_map = NULL;
44 static pmap *entity_reference_map = NULL;
45 static pmap *type_alloc_map = NULL;
46 static pmap *type_cast_map = NULL;
47 static pmap *type_pointertype_map = NULL;
48 static pmap *type_arraytype_map = NULL;
51 * Return a flexible array containing all IR-nodes
52 * that access a given entity.
54 static ir_node **get_entity_access_array(const ir_entity *ent)
56 if (!entity_access_map) entity_access_map = pmap_create();
58 ir_node **res = pmap_get(ir_node*, entity_access_map, ent);
60 res = NEW_ARR_F(ir_node *, 0);
61 pmap_insert(entity_access_map, ent, (void *)res);
67 static void set_entity_access_array(const ir_entity *ent, ir_node **accs)
69 pmap_insert(entity_access_map, ent, (void *)accs);
73 * Return a flexible array containing all IR-nodes
74 * that reference a given entity.
76 static ir_node **get_entity_reference_array(const ir_entity *ent)
78 if (!entity_reference_map) entity_reference_map = pmap_create();
80 ir_node **res = pmap_get(ir_node*, entity_reference_map, ent);
82 res = NEW_ARR_F(ir_node *, 0);
83 pmap_insert(entity_reference_map, ent, (void *)res);
89 static void set_entity_reference_array(const ir_entity *ent, ir_node **refs)
91 pmap_insert(entity_reference_map, ent, (void *)refs);
95 * Return a flexible array containing all IR-nodes
96 * that allocate a given type.
98 static ir_node **get_type_alloc_array(const ir_type *tp)
100 if (!type_alloc_map) type_alloc_map = pmap_create();
102 ir_node **res = pmap_get(ir_node*, type_alloc_map, tp);
104 res = NEW_ARR_F(ir_node *, 0);
105 pmap_insert(type_alloc_map, tp, (void *)res);
111 static void set_type_alloc_array(const ir_type *tp, ir_node **alls)
113 pmap_insert(type_alloc_map, tp, (void *)alls);
117 * Return a flexible array containing all Cast-nodes
118 * that "create" a given type.
120 static ir_node **get_type_cast_array(const ir_type *tp)
122 if (!type_cast_map) type_cast_map = pmap_create();
124 ir_node **res = pmap_get(ir_node*, type_cast_map, tp);
126 res = NEW_ARR_F(ir_node *, 0);
127 pmap_insert(type_cast_map, tp, (void *)res);
132 static void set_type_cast_array(const ir_type *tp, ir_node **alls)
134 pmap_insert(type_cast_map, tp, (void *)alls);
138 * Return a flexible array containing all pointer
139 * types that points-to a given type.
141 static ir_type **get_type_pointertype_array(const ir_type *tp)
143 if (!type_pointertype_map) type_pointertype_map = pmap_create();
145 ir_type **res = pmap_get(ir_type*, type_pointertype_map, tp);
147 res = NEW_ARR_F(ir_type *, 0);
148 pmap_insert(type_pointertype_map, tp, (void *)res);
154 static void set_type_pointertype_array(const ir_type *tp, ir_type **pts)
156 pmap_insert(type_pointertype_map, tp, (void *)pts);
160 * Return a flexible array containing all array
161 * types that have a given type as element type.
163 static ir_type **get_type_arraytype_array(const ir_type *tp)
165 if (!type_arraytype_map) type_arraytype_map = pmap_create();
167 ir_type **res = pmap_get(ir_type*, type_arraytype_map, tp);
169 res = NEW_ARR_F(ir_type *, 0);
170 pmap_insert(type_arraytype_map, tp, (void *)res);
176 static void set_type_arraytype_array(const ir_type *tp, ir_type **pts)
178 pmap_insert(type_arraytype_map, tp, (void *)pts);
181 /*------------------------------------------------------------------*/
182 /* Accessing the out data structures. */
183 /* These routines only work properly if firm is in state */
184 /* trouts_consistent or trouts_inconsistent. */
185 /*------------------------------------------------------------------*/
187 /**------------------------------------------------------------------*/
188 /* Access routines for entities */
189 /**------------------------------------------------------------------*/
191 size_t get_entity_n_accesses(const ir_entity *ent)
195 assert(ent && is_entity(ent));
197 accs = get_entity_access_array(ent);
198 return ARR_LEN(accs);
201 ir_node *get_entity_access(const ir_entity *ent, size_t pos)
205 assert(pos < get_entity_n_accesses(ent));
207 accs = get_entity_access_array(ent);
211 static void add_entity_access(const ir_entity *ent, ir_node *n)
215 assert(ent && is_entity(ent));
216 assert(n && is_ir_node(n));
218 accs = get_entity_access_array(ent);
219 ARR_APP1(ir_node *, accs, n);
220 set_entity_access_array(ent, accs);
224 void set_entity_access(const ir_entity *ent, int pos, ir_node *n)
228 assert(0 <= pos && pos < get_entity_n_accesses(ent));
229 assert(n && is_ir_node(n));
231 accs = get_entity_access_array(ent);
236 /*------------------------------------------------------------------*/
238 size_t get_entity_n_references(const ir_entity *ent)
242 assert(ent && is_entity(ent));
244 refs = get_entity_reference_array(ent);
245 return ARR_LEN(refs);
248 ir_node *get_entity_reference(const ir_entity *ent, size_t pos)
252 assert( pos < get_entity_n_references(ent));
254 refs = get_entity_reference_array(ent);
258 static void add_entity_reference(const ir_entity *ent, ir_node *n)
262 assert(ent && is_entity(ent));
263 assert(n && is_ir_node(n));
265 refs = get_entity_reference_array(ent);
266 ARR_APP1(ir_node *, refs, n);
267 set_entity_reference_array(ent, refs);
271 void set_entity_reference(const ir_entity *ent, int pos, ir_node *n)
275 assert(0 <= pos && pos < get_entity_n_references(ent));
276 assert(n && is_ir_node(n));
278 refs = get_entity_reference_array(ent);
283 /**------------------------------------------------------------------*/
284 /* Access routines for types */
285 /**------------------------------------------------------------------*/
287 /* Number of Alloc nodes that create an instance of this type */
288 size_t get_type_n_allocs(const ir_type *tp)
292 assert(tp && is_type(tp));
294 allocs = get_type_alloc_array(tp);
295 return ARR_LEN(allocs);
298 /* Alloc node that creates an instance of this type */
299 ir_node *get_type_alloc(const ir_type *tp, size_t pos)
302 assert( pos < get_type_n_allocs(tp));
304 allocs = get_type_alloc_array(tp);
308 static void add_type_alloc(const ir_type *tp, ir_node *n)
312 assert(tp && is_type(tp));
313 assert(n && is_ir_node(n));
315 allocs = get_type_alloc_array(tp);
316 ARR_APP1(ir_node *, allocs, n);
317 set_type_alloc_array(tp, allocs);
321 void set_type_alloc(const ir_type *tp, int pos, ir_node *n)
325 assert(0 <= pos && pos < get_type_n_allocs(tp));
326 assert(n && is_ir_node(n));
328 allocs = get_type_alloc_array(tp);
333 /* Number of Cast nodes that create an instance of this type */
334 size_t get_type_n_casts(const ir_type *tp)
338 assert(tp && is_type(tp));
340 casts = get_type_cast_array(tp);
341 return ARR_LEN(casts);
345 size_t get_class_n_upcasts(const ir_type *clss)
347 size_t i, n_casts = get_type_n_casts(clss);
348 size_t n_instances = 0;
349 for (i = 0; i < n_casts; ++i) {
350 ir_node *cast = get_type_cast(clss, i);
351 if (is_Cast_upcast(cast))
357 size_t get_class_n_downcasts(const ir_type *clss)
359 size_t i, n_casts = get_type_n_casts(clss);
360 size_t n_instances = 0;
361 for (i = 0; i < n_casts; ++i) {
362 ir_node *cast = get_type_cast(clss, i);
363 if (is_Cast_downcast(cast))
369 ir_node *get_type_cast(const ir_type *tp, size_t pos)
372 assert(pos < get_type_n_casts(tp));
374 casts = get_type_cast_array(tp);
378 void add_type_cast(const ir_type *tp, ir_node *n)
382 assert(tp && is_type(tp));
383 assert(n && is_ir_node(n));
385 casts = get_type_cast_array(tp);
386 ARR_APP1(ir_node *, casts, n);
387 set_type_cast_array(tp, casts);
391 void set_type_cast(const ir_type *tp, size_t pos, ir_node *n)
395 assert(pos < get_type_n_casts(tp));
396 assert(n && is_ir_node(n));
398 casts = get_type_cast_array(tp);
403 /*------------------------------------------------------------------*/
405 size_t get_type_n_pointertypes_to(const ir_type *tp)
409 assert(tp && is_type(tp));
411 pts = get_type_pointertype_array(tp);
415 ir_type *get_type_pointertype_to(const ir_type *tp, size_t pos)
419 assert(pos < get_type_n_pointertypes_to(tp));
421 pts = get_type_pointertype_array(tp);
425 void add_type_pointertype_to(const ir_type *tp, ir_type *ptp)
429 assert(tp && is_type(tp));
430 assert(ptp && is_Pointer_type(ptp));
432 pts = get_type_pointertype_array(tp);
433 ARR_APP1(ir_type*, pts, ptp);
434 set_type_pointertype_array(tp, pts);
438 void set_type_pointertype_to(const ir_type *tp, int pos, ir_type *ptp)
442 assert(0 <= pos && pos < get_type_n_pointertypes_to(tp));
443 assert(ptp && is_Pointer_type(ptp));
445 pts = get_type_pointertype_array(tp);
450 /*------------------------------------------------------------------*/
452 size_t get_type_n_arraytypes_of(const ir_type *tp)
456 assert(tp && is_type(tp));
458 pts = get_type_arraytype_array(tp);
462 ir_type *get_type_arraytype_of(const ir_type *tp, size_t pos)
466 assert(pos < get_type_n_arraytypes_of(tp));
468 pts = get_type_arraytype_array(tp);
472 void add_type_arraytype_of(const ir_type *tp, ir_type *atp)
476 assert(tp && is_type(tp));
477 assert(atp && is_Array_type(atp));
479 pts = get_type_arraytype_array(tp);
480 ARR_APP1(ir_type*, pts, atp);
481 set_type_arraytype_array(tp, pts);
485 void set_type_arraytype_of(const ir_type *tp, int pos, ir_type *atp)
489 assert(0 <= pos && pos < get_type_n_arraytypes_of(tp));
490 assert(atp && is_Array_type(atp));
492 pts = get_type_arraytype_array(tp);
497 /*------------------------------------------------------------------*/
498 /* Building and Removing the out datastructure */
499 /*------------------------------------------------------------------*/
501 /** Initialize the trouts handling. */
502 static void init_trouts(void)
506 /** The number of entities that can be accessed by this Sel node. */
507 static int get_Sel_n_accessed_entities(const ir_node *sel)
513 /** The entity that cat be accessed by this Sel node. */
514 static ir_entity *get_Sel_accessed_entity(const ir_node *sel)
516 return get_Sel_entity(sel);
519 /** An addr node is a SymConst or a Sel. */
520 static int get_addr_n_entities(const ir_node *addr)
522 switch (get_irn_opcode(addr)) {
524 /* Treat jack array sels? */
525 return get_Sel_n_accessed_entities(addr);
527 if (get_SymConst_kind(addr) == symconst_addr_ent)
535 /** An addr node is a SymConst or a Sel.
536 If Sel follow to outermost of compound. */
537 static ir_entity *get_addr_entity(const ir_node *addr, int pos)
542 switch (get_irn_opcode(addr)) {
544 /* Treat jack array sels? They are compounds! Follow to outermost entity. */
545 ptr = get_Sel_ptr(addr);
546 while (is_Sel(ptr)) {
548 ptr = get_Sel_ptr(addr);
550 assert(0 <= pos && pos < get_Sel_n_accessed_entities(addr));
551 return get_Sel_accessed_entity(addr);
553 if (get_SymConst_kind(addr) == symconst_addr_ent) {
555 return get_SymConst_entity(addr);
563 static void chain_accesses(ir_node *n, void *env)
570 add_type_alloc(get_Alloc_type(n), n);
572 } else if (is_Cast(n)) {
573 add_type_cast(get_Cast_type(n), n);
575 } else if (is_Sel(n)) {
576 add_entity_reference(get_Sel_entity(n), n);
578 } else if (is_SymConst_addr_ent(n)) {
579 add_entity_reference(get_SymConst_entity(n), n);
581 } else if (is_Store(n)) {
582 addr = get_Store_ptr(n);
583 } else if (is_Load(n)) {
584 addr = get_Load_ptr(n);
585 } else if (is_Call(n)) {
586 addr = get_Call_ptr(n);
587 if (! is_Sel(addr)) return; /* Sels before Calls mean a Load / polymorphic Call. */
592 n_ents = get_addr_n_entities(addr); /* == 1 */
593 for (i = 0; i < n_ents; ++i) {
594 ir_entity *ent = get_addr_entity(addr, i);
596 add_entity_access(ent, n);
598 //add_unrecognized_access(n);
603 * Handle chain types (pointer, array) by adding them to
606 static void chain_types(ir_type *tp)
608 if (is_Pointer_type(tp)) {
609 add_type_pointertype_to(get_pointer_points_to_type(tp), tp);
610 } else if (is_Array_type(tp)) {
611 add_type_arraytype_of(get_array_element_type(tp), tp);
615 void compute_trouts(void)
622 /* Compute outs for IR nodes. */
623 for (i = get_irp_n_irgs(); i > 0;) {
624 ir_graph *irg = get_irp_irg(--i);
625 irg_walk_graph(irg, NULL, chain_accesses, NULL);
627 walk_const_code(NULL, chain_accesses, NULL);
629 /* Compute outs for types */
630 for (i = get_irp_n_types(); i > 0;) {
631 ir_type *type = get_irp_type(--i);
636 void free_trouts(void)
638 if (entity_access_map) {
640 for (accs = (ir_node **)pmap_first(entity_access_map);
642 accs = (ir_node **)pmap_next(entity_access_map)) {
643 /* DEL_ARR_F(accs); */
645 pmap_destroy(entity_access_map);
646 entity_access_map = NULL;
649 if (entity_reference_map) {
651 for (refs = (ir_node **)pmap_first(entity_reference_map);
653 refs = (ir_node **)pmap_next(entity_reference_map)) {
654 /* DEL_ARR_F(refs); */
656 pmap_destroy(entity_reference_map);
657 entity_reference_map = NULL;
660 if (type_alloc_map) {
662 for (alls = (ir_node **)pmap_first(type_alloc_map);
664 alls = (ir_node **)pmap_next(type_alloc_map)) {
665 /* DEL_ARR_F(alls); */
667 pmap_destroy(type_alloc_map);
668 type_alloc_map = NULL;
673 for (casts = (ir_node **)pmap_first(type_cast_map);
675 casts = (ir_node **)pmap_next(type_cast_map)) {
676 /* DEL_ARR_F(alls); */
678 pmap_destroy(type_cast_map);
679 type_cast_map = NULL;
682 if (type_pointertype_map) {
684 for (pts = (ir_node **)pmap_first(type_pointertype_map);
686 pts = (ir_node **)pmap_next(type_pointertype_map)) {
687 /* DEL_ARR_F(pts); */
689 pmap_destroy(type_pointertype_map);
690 type_pointertype_map = NULL;
693 if (type_arraytype_map) {
695 for (pts = (ir_node **)pmap_first(type_arraytype_map);
697 pts = (ir_node **)pmap_next(type_arraytype_map)) {
698 /* DEL_ARR_F(pts); */
700 pmap_destroy(type_arraytype_map);
701 type_arraytype_map = NULL;