Slimified the bitset implementation a little bit
[libfirm] / ir / ana / trouts.c
index 90148e0..11d2c56 100644 (file)
@@ -1,15 +1,34 @@
 /*
- * Project:     libFIRM
- * File name:   ir/ana/trouts.c
- * Purpose:     Reverse edges that reference types/entities.
- * Author:      Goetz Lindenmaier
- * Modified by:
- * Created:     29.10.2004
- * CVS-ID:      $Id$
- * Copyright:   (c) 2004 Universität Karlsruhe
- * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
+ * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
+ *
+ * This file is part of libFirm.
+ *
+ * This file may be distributed and/or modified under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation and appearing in the file LICENSE.GPL included in the
+ * packaging of this file.
+ *
+ * Licensees holding valid libFirm Professional Edition licenses may use
+ * this file in accordance with the libFirm Commercial License.
+ * Agreement provided with the Software.
+ *
+ * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+ * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE.
  */
 
+/**
+ * @file
+ * @brief    Reverse edges that reference types/entities.
+ * @author   Goetz Lindenmaier
+ * @date     29.10.2004
+ * @version  $Id$
+ */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "irnode.h"
 #include "trouts.h"
 
 #include "array.h"
@@ -17,6 +36,7 @@
 
 #include "irprog_t.h"
 #include "irgwalk.h"
+#include "irnode.h"
 
 
 /*------------------------------------------------------------------*/
@@ -28,8 +48,13 @@ static pmap *entity_reference_map = NULL;
 static pmap *type_alloc_map = NULL;
 static pmap *type_cast_map = NULL;
 static pmap *type_pointertype_map = NULL;
+static pmap *type_arraytype_map = NULL;
 
-static ir_node **get_entity_access_array(entity *ent) {
+/**
+ * Return a flexible array containing all IR-nodes
+ * that access a given entity.
+ */
+static ir_node **get_entity_access_array(ir_entity *ent) {
   ir_node **res;
   if (!entity_access_map) entity_access_map = pmap_create();
 
@@ -42,13 +67,17 @@ static ir_node **get_entity_access_array(entity *ent) {
 
   return res;
 }
-void set_entity_access_array(entity *ent, ir_node **accs) {
+void set_entity_access_array(ir_entity *ent, ir_node **accs) {
   ir_node **old = pmap_get(entity_access_map, (void *)ent);
   if (old != accs)
     pmap_insert(entity_access_map, (void *)ent, (void *)accs);
 }
 
-static ir_node **get_entity_reference_array(entity *ent) {
+/**
+ * Return a flexible array containing all IR-nodes
+ * that reference a given entity.
+ */
+static ir_node **get_entity_reference_array(ir_entity *ent) {
   ir_node **res;
   if (!entity_reference_map) entity_reference_map = pmap_create();
 
@@ -61,13 +90,17 @@ static ir_node **get_entity_reference_array(entity *ent) {
 
   return res;
 }
-void set_entity_reference_array(entity *ent, ir_node **refs) {
+void set_entity_reference_array(ir_entity *ent, ir_node **refs) {
   ir_node **old = pmap_get(entity_reference_map, (void *)ent);
   if (old != refs)
     pmap_insert(entity_reference_map, (void *)ent, (void *)refs);
 }
 
-static ir_node **get_type_alloc_array(type *tp) {
+/**
+ * Return a flexible array containing all IR-nodes
+ * that allocate a given type.
+ */
+static ir_node **get_type_alloc_array(ir_type *tp) {
   ir_node **res;
   if (!type_alloc_map) type_alloc_map = pmap_create();
 
@@ -80,13 +113,17 @@ static ir_node **get_type_alloc_array(type *tp) {
 
   return res;
 }
-void set_type_alloc_array(type *tp, ir_node **alls) {
+void set_type_alloc_array(ir_type *tp, ir_node **alls) {
   ir_node **old = pmap_get(type_alloc_map, (void *)tp);
   if (old != alls)
     pmap_insert(type_alloc_map, (void *)tp, (void *)alls);
 }
 
-static ir_node **get_type_cast_array(type *tp) {
+/**
+ * Return a flexible array containing all Cast-nodes
+ * that "create" a given type.
+ */
+static ir_node **get_type_cast_array(ir_type *tp) {
   ir_node **res;
   if (!type_cast_map) type_cast_map = pmap_create();
 
@@ -96,34 +133,62 @@ static ir_node **get_type_cast_array(type *tp) {
     res = NEW_ARR_F(ir_node *, 0);
     pmap_insert(type_cast_map, (void *)tp, (void *)res);
   }
-
   return res;
 }
-void set_type_cast_array(type *tp, ir_node **alls) {
+
+void set_type_cast_array(ir_type *tp, ir_node **alls) {
   ir_node **old = pmap_get(type_cast_map, (void *)tp);
   if (old != alls)
     pmap_insert(type_cast_map, (void *)tp, (void *)alls);
 }
 
-static type **get_type_pointertype_array(type *tp) {
-  type **res;
+/**
+ * Return a flexible array containing all pointer
+ * types that points-to a given type.
+ */
+static ir_type **get_type_pointertype_array(ir_type *tp) {
+  ir_type **res;
   if (!type_pointertype_map) type_pointertype_map = pmap_create();
 
   if (pmap_contains(type_pointertype_map, (void *)tp)) {
-    res = (type **) pmap_get(type_pointertype_map, (void *)tp);
+    res = (ir_type **) pmap_get(type_pointertype_map, (void *)tp);
   } else {
-    res = NEW_ARR_F(type *, 0);
+    res = NEW_ARR_F(ir_type *, 0);
     pmap_insert(type_pointertype_map, (void *)tp, (void *)res);
   }
 
   return res;
 }
-void set_type_pointertype_array(type *tp, type **pts) {
-  type **old = pmap_get(type_pointertype_map, (void *)tp);
+void set_type_pointertype_array(ir_type *tp, ir_type **pts) {
+  ir_type **old = pmap_get(type_pointertype_map, (void *)tp);
   if (old != pts)
     pmap_insert(type_pointertype_map, (void *)tp, (void *)pts);
 }
 
+/**
+ * Return a flexible array containing all array
+ * types that have a given type as element type.
+ */
+static ir_type **get_type_arraytype_array(ir_type *tp) {
+  ir_type **res;
+  if (!type_arraytype_map) type_arraytype_map = pmap_create();
+
+  if (pmap_contains(type_arraytype_map, (void *)tp)) {
+    res = (ir_type **) pmap_get(type_arraytype_map, (void *)tp);
+  } else {
+    res = NEW_ARR_F(ir_type *, 0);
+    pmap_insert(type_arraytype_map, (void *)tp, (void *)res);
+  }
+
+  return res;
+}
+
+void set_type_arraytype_array(ir_type *tp, ir_type **pts) {
+  ir_type **old = pmap_get(type_arraytype_map, (void *)tp);
+  if (old != pts)
+    pmap_insert(type_arraytype_map, (void *)tp, (void *)pts);
+}
+
 /*------------------------------------------------------------------*/
 /* Accessing the out data structures.                               */
 /* These routines only work properly if firm is in state            */
@@ -134,7 +199,7 @@ void set_type_pointertype_array(type *tp, type **pts) {
 /*   Access routines for entities                                    */
 /**------------------------------------------------------------------*/
 
-int get_entity_n_accesses(entity *ent) {
+int get_entity_n_accesses(ir_entity *ent) {
   ir_node ** accs;
 
   assert(ent && is_entity(ent));
@@ -143,7 +208,7 @@ int get_entity_n_accesses(entity *ent) {
   return ARR_LEN(accs);
 }
 
-ir_node *get_entity_access(entity *ent, int pos) {
+ir_node *get_entity_access(ir_entity *ent, int pos) {
   ir_node ** accs;
 
   assert(0 <= pos && pos < get_entity_n_accesses(ent));
@@ -152,7 +217,7 @@ ir_node *get_entity_access(entity *ent, int pos) {
   return accs[pos];
 }
 
-void add_entity_access(entity *ent, ir_node *n) {
+void add_entity_access(ir_entity *ent, ir_node *n) {
   ir_node ** accs;
 
   assert(ent && is_entity(ent));
@@ -163,7 +228,7 @@ void add_entity_access(entity *ent, ir_node *n) {
   set_entity_access_array(ent, accs);
 }
 
-void set_entity_access(entity *ent, int pos, ir_node *n) {
+void set_entity_access(ir_entity *ent, int pos, ir_node *n) {
   ir_node ** accs;
 
   assert(0 <= pos && pos < get_entity_n_accesses(ent));
@@ -175,7 +240,7 @@ void set_entity_access(entity *ent, int pos, ir_node *n) {
 
 /**------------------------------------------------------------------*/
 
-int get_entity_n_references(entity *ent) {
+int get_entity_n_references(ir_entity *ent) {
   ir_node ** refs;
 
   assert(ent && is_entity(ent));
@@ -184,7 +249,7 @@ int get_entity_n_references(entity *ent) {
   return ARR_LEN(refs);
 }
 
-ir_node *get_entity_reference(entity *ent, int pos) {
+ir_node *get_entity_reference(ir_entity *ent, int pos) {
   ir_node ** refs;
 
   assert(0 <= pos && pos < get_entity_n_references(ent));
@@ -193,7 +258,7 @@ ir_node *get_entity_reference(entity *ent, int pos) {
   return refs[pos];
 }
 
-void add_entity_reference(entity *ent, ir_node *n) {
+void add_entity_reference(ir_entity *ent, ir_node *n) {
   ir_node ** refs;
 
   assert(ent && is_entity(ent));
@@ -204,7 +269,7 @@ void add_entity_reference(entity *ent, ir_node *n) {
   set_entity_reference_array(ent, refs);
 }
 
-void set_entity_reference(entity *ent, int pos, ir_node *n) {
+void set_entity_reference(ir_entity *ent, int pos, ir_node *n) {
   ir_node ** refs;
 
   assert(0 <= pos && pos < get_entity_n_references(ent));
@@ -220,7 +285,7 @@ void set_entity_reference(entity *ent, int pos, ir_node *n) {
 /**------------------------------------------------------------------*/
 
 /* Number of Alloc nodes that create an instance of this type */
-int get_type_n_allocs(type *tp) {
+int get_type_n_allocs(ir_type *tp) {
   ir_node **allocs;
 
   assert(tp && is_type(tp));
@@ -230,7 +295,7 @@ int get_type_n_allocs(type *tp) {
 }
 
 /* Alloc node that creates an instance of this type */
-ir_node *get_type_alloc(type *tp, int pos) {
+ir_node *get_type_alloc(ir_type *tp, int pos) {
   ir_node **allocs;
   assert(0 <= pos && pos < get_type_n_allocs(tp));
 
@@ -238,7 +303,7 @@ ir_node *get_type_alloc(type *tp, int pos) {
   return allocs[pos];
 }
 
-void add_type_alloc(type *tp, ir_node *n) {
+void add_type_alloc(ir_type *tp, ir_node *n) {
   ir_node **allocs;
 
   assert(tp && is_type(tp));
@@ -249,7 +314,7 @@ void add_type_alloc(type *tp, ir_node *n) {
   set_type_alloc_array(tp, allocs);
 }
 
-void set_type_alloc(type *tp, int pos, ir_node *n) {
+void set_type_alloc(ir_type *tp, int pos, ir_node *n) {
   ir_node **allocs;
 
   assert(0 <= pos && pos < get_type_n_allocs(tp));
@@ -260,7 +325,7 @@ void set_type_alloc(type *tp, int pos, ir_node *n) {
 }
 
 /* Number of Cast nodes that create an instance of this type */
-int get_type_n_casts(type *tp) {
+int get_type_n_casts(ir_type *tp) {
   ir_node **casts;
 
   assert(tp && is_type(tp));
@@ -270,7 +335,7 @@ int get_type_n_casts(type *tp) {
 }
 
 
-int get_class_n_upcasts(type *clss) {
+int get_class_n_upcasts(ir_type *clss) {
   int i, n_casts = get_type_n_casts(clss);
   int n_instances = 0;
   for (i = 0; i < n_casts; ++i) {
@@ -280,7 +345,7 @@ int get_class_n_upcasts(type *clss) {
   return n_instances;
 }
 
-int get_class_n_downcasts(type *clss) {
+int get_class_n_downcasts(ir_type *clss) {
   int i, n_casts = get_type_n_casts(clss);
   int n_instances = 0;
   for (i = 0; i < n_casts; ++i) {
@@ -292,7 +357,7 @@ int get_class_n_downcasts(type *clss) {
 
 
 /* Cast node that creates an instance of this type */
-ir_node *get_type_cast(type *tp, int pos) {
+ir_node *get_type_cast(ir_type *tp, int pos) {
   ir_node **casts;
   assert(0 <= pos && pos < get_type_n_casts(tp));
 
@@ -300,7 +365,7 @@ ir_node *get_type_cast(type *tp, int pos) {
   return casts[pos];
 }
 
-void add_type_cast(type *tp, ir_node *n) {
+void add_type_cast(ir_type *tp, ir_node *n) {
   ir_node **casts;
 
   assert(tp && is_type(tp));
@@ -311,7 +376,7 @@ void add_type_cast(type *tp, ir_node *n) {
   set_type_cast_array(tp, casts);
 }
 
-void set_type_cast(type *tp, int pos, ir_node *n) {
+void set_type_cast(ir_type *tp, int pos, ir_node *n) {
   ir_node **casts;
 
   assert(0 <= pos && pos < get_type_n_casts(tp));
@@ -323,8 +388,8 @@ void set_type_cast(type *tp, int pos, ir_node *n) {
 
 /**------------------------------------------------------------------*/
 
-int get_type_n_pointertypes_to(type *tp) {
-  type ** pts;
+int get_type_n_pointertypes_to(ir_type *tp) {
+  ir_type ** pts;
 
   assert(tp && is_type(tp));
 
@@ -332,8 +397,8 @@ int get_type_n_pointertypes_to(type *tp) {
   return ARR_LEN(pts);
 }
 
-type *get_type_pointertype_to(type *tp, int pos) {
-  type ** pts;
+ir_type *get_type_pointertype_to(ir_type *tp, int pos) {
+  ir_type ** pts;
 
   assert(0 <= pos && pos < get_type_n_pointertypes_to(tp));
 
@@ -341,8 +406,8 @@ type *get_type_pointertype_to(type *tp, int pos) {
   return pts[pos];
 }
 
-void add_type_pointertype_to(type *tp, type *ptp) {
-  type ** pts;
+void add_type_pointertype_to(ir_type *tp, ir_type *ptp) {
+  ir_type ** pts;
 
   assert(tp && is_type(tp));
   assert(ptp && is_Pointer_type(ptp));
@@ -352,8 +417,8 @@ void add_type_pointertype_to(type *tp, type *ptp) {
   set_type_pointertype_array(tp, pts);
 }
 
-void set_type_pointertype_to(type *tp, int pos, type *ptp) {
-  type ** pts;
+void set_type_pointertype_to(ir_type *tp, int pos, ir_type *ptp) {
+  ir_type ** pts;
 
   assert(0 <= pos && pos < get_type_n_pointertypes_to(tp));
   assert(ptp && is_Pointer_type(ptp));
@@ -362,6 +427,48 @@ void set_type_pointertype_to(type *tp, int pos, type *ptp) {
   pts[pos] = ptp;
 }
 
+
+/**------------------------------------------------------------------*/
+
+int   get_type_n_arraytypes_of(ir_type *tp) {
+  ir_type ** pts;
+
+  assert(tp && is_type(tp));
+
+  pts = get_type_arraytype_array(tp);
+  return ARR_LEN(pts);
+}
+
+ir_type *get_type_arraytype_of(ir_type *tp, int pos) {
+  ir_type ** pts;
+
+  assert(0 <= pos && pos < get_type_n_arraytypes_of(tp));
+
+  pts = get_type_arraytype_array(tp);
+  return pts[pos];
+}
+
+void  add_type_arraytype_of(ir_type *tp, ir_type *atp) {
+  ir_type ** pts;
+
+  assert(tp && is_type(tp));
+  assert(atp && is_Array_type(atp));
+
+  pts = get_type_arraytype_array(tp);
+  ARR_APP1(ir_node *, pts, atp);
+  set_type_arraytype_array(tp, pts);
+}
+
+void  set_type_arraytype_of(ir_type *tp, int pos, ir_type *atp) {
+  ir_type ** pts;
+
+  assert(0 <= pos && pos < get_type_n_arraytypes_of(tp));
+  assert(atp && is_Array_type(atp));
+
+  pts = get_type_arraytype_array(tp);
+  pts[pos] = atp;
+}
+
 /*------------------------------------------------------------------*/
 /* Building and Removing the out datastructure                      */
 /*------------------------------------------------------------------*/
@@ -370,16 +477,17 @@ static void init_trouts(void) {
 
 }
 
-/* The entities that can be accessed by this Sel node. */
+/** The number of entities that can be accessed by this Sel node. */
 static int get_Sel_n_accessed_entities(ir_node *sel) {
   return 1;
 }
 
-static entity *get_Sel_accessed_entity(ir_node *sel) {
+/** The entity that cat be accessed by this Sel node. */
+static ir_entity *get_Sel_accessed_entity(ir_node *sel) {
   return get_Sel_entity(sel);
 }
 
-/* An addr node is a SymConst or a Sel. */
+/** An addr node is a SymConst or a Sel. */
 static int get_addr_n_entities(ir_node *addr) {
   int n_ents;
 
@@ -401,15 +509,15 @@ static int get_addr_n_entities(ir_node *addr) {
   return n_ents;
 }
 
-/* An addr node is a SymConst or a Sel.
-   If Sel follow to outermost of compound. */
-static entity *get_addr_entity(ir_node *addr, int pos) {
-  entity *ent;
+/** An addr node is a SymConst or a Sel.
+    If Sel follow to outermost of compound. */
+static ir_entity *get_addr_entity(ir_node *addr, int pos) {
+  ir_entity *ent;
 
   switch (get_irn_opcode(addr)) {
   case iro_Sel:
     /* Treat jack array sels? They are compounds!  Follow to outermost entity.  */
-    while (get_irn_op(get_Sel_ptr(addr)) == op_Sel) {
+    while (is_Sel(get_Sel_ptr(addr))) {
       addr = get_Sel_ptr(addr);
     }
     assert (0 <= pos && pos < get_Sel_n_accessed_entities(addr));
@@ -454,14 +562,14 @@ static void chain_accesses(ir_node *n, void *env) {
     addr = get_memop_ptr(n);
   } else if (get_irn_op(n) == op_Call) {
     addr = get_Call_ptr(n);
-    if (get_irn_op(addr) != op_Sel) return;  /* Sels before Calls mean a Load / polymorphic Call. */
+    if (! is_Sel(addr)) return;  /* Sels before Calls mean a Load / polymorphic Call. */
   } else {
     return;
   }
 
   n_ents = get_addr_n_entities(addr);  /* == 1 */
   for (i = 0; i < n_ents; ++i) {
-    entity *ent = get_addr_entity(addr, i);
+    ir_entity *ent = get_addr_entity(addr, i);
     if (ent)
       add_entity_access(ent, n);
     //else
@@ -469,9 +577,11 @@ static void chain_accesses(ir_node *n, void *env) {
   }
 }
 
-static void chain_types(type *tp) {
+static void chain_types(ir_type *tp) {
   if (is_Pointer_type(tp)) {
     add_type_pointertype_to(get_pointer_points_to_type(tp), tp);
+  } else if (is_Array_type(tp)) {
+    add_type_arraytype_of(get_array_element_type(tp), tp);
   }
 }
 
@@ -494,9 +604,8 @@ void compute_trouts(void) {
   init_trouts();
 
   /* Compute outs for irnodes. */
-  for (i=0; i < n_irgs; i++) {
-    current_ir_graph = get_irp_irg(i);
-    irg_walk_graph(current_ir_graph, NULL, chain_accesses, NULL);
+  for (i = 0; i < n_irgs; i++) {
+    irg_walk_graph(get_irp_irg(i), NULL, chain_accesses, NULL);
   }
   walk_const_code(NULL, chain_accesses, NULL);
 
@@ -560,5 +669,16 @@ void free_trouts(void) {
     pmap_destroy(type_pointertype_map);
     type_pointertype_map = NULL;
   }
+
+  if (type_arraytype_map) {
+    ir_node **pts;
+    for (pts = (ir_node **)pmap_first(type_arraytype_map);
+        pts;
+        pts = (ir_node **)pmap_next(type_arraytype_map))
+      ; //DEL_ARR_F(pts);
+    pmap_destroy(type_arraytype_map);
+    type_arraytype_map = NULL;
+  }
+
   irp->trouts_state = outs_none;
 }