doxygen comment updated
[libfirm] / ir / ir / irphase_t.h
index 581627a..02453a1 100644 (file)
@@ -6,7 +6,7 @@
  * Modified by:
  * Created:
  * CVS-ID:      $Id$
- * Copyright:   (c) 1998-2006 Universitaet Karlsruhe
+ * Copyright:   (c) 1998-2007 Universitaet Karlsruhe
  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
  */
 
@@ -15,6 +15,7 @@
 
 #include "obstack.h"
 #include "irgraph_t.h"
+#include "irtools.h"
 #include "irphase.h"
 #include "irtools.h"
 
@@ -31,7 +32,7 @@ typedef struct {
  */
 phase_stat_t *phase_stat(const phase_t *phase, phase_stat_t *stat);
 
-typedef void (phase_irn_data_init_t)(const phase_t *phase, const ir_node *irn, void *data);
+typedef void *(phase_irn_data_init_t)(phase_t *phase, ir_node *irn, void *old);
 
 /**
  * The default grow factor.
@@ -48,7 +49,6 @@ struct _phase_t {
        ir_graph              *irg;            /**< The irg this phase will we applied to. */
        unsigned               growth_factor;  /**< factor to leave room for add. nodes. 256 means 1.0. */
        void                  *priv;           /**< Some pointer private to the user of the phase. */
-       size_t                 data_size;      /**< The amount of bytes which shall be allocated for each irn. */
        size_t                 n_data_ptr;     /**< The length of the data_ptr array. */
        void                 **data_ptr;       /**< Map node indexes to irn data on the obstack. */
        phase_irn_data_init_t *data_init;      /**< A callback that is called to initialize newly created node data. */
@@ -58,14 +58,15 @@ struct _phase_t {
  * Initialize a phase object.
  * @param name          The name of the phase.
  * @param irg           The graph the phase will run on.
- * @param data_size     The amount of extra storage in bytes that should be allocated for each node.
  * @param growth_factor A factor denoting how many node slots will be additionally allocated,
- *                      if the node => data is full. 256 means 1.0.
+ *                      if the node => data is full. The factor is given in units of 1/256, so
+ *                      256 means 1.0.
  * @param irn_data_init A callback that is called to initialize newly created node data.
+ *                      Must be non-null.
  * @param priv          Some private pointer which is kept in the phase and can be retrieved with phase_get_private().
  * @return              A new phase object.
  */
-phase_t *phase_init(phase_t *ph, const char *name, ir_graph *irg, size_t data_size, unsigned growth_factor, phase_irn_data_init_t *data_init);
+phase_t *phase_init(phase_t *ph, const char *name, ir_graph *irg, unsigned growth_factor, phase_irn_data_init_t *data_init);
 
 /**
  * Free the phase and all node data associated with it.
@@ -76,10 +77,45 @@ void phase_free(phase_t *phase);
 /**
  * Re-initialize the irn data for all nodes in the node => data map using the given callback.
  * @param phase The phase.
- * @note This function will pass NULL to the init function passed to phase_new().
  */
 void phase_reinit_irn_data(phase_t *phase);
 
+/**
+ * Re-initialize the irn data for all nodes in the given block.
+ * @param phase The phase.
+ * @param block The block.
+ */
+void phase_reinit_block_irn_data(phase_t *phase, ir_node *block);
+
+/**
+ * Re-initialize the irn data for the given node.
+ * @param phase The phase.
+ * @param irn   The irn.
+ */
+#define phase_reinit_single_irn_data(phase, irn) _phase_reinit_single_irn_data((phase), (irn))
+
+/**
+ * Returns the first node of the phase having some data assigned.
+ * @param phase The phase.
+ * @return The first irn having some data assigned, NULL otherwise
+ */
+ir_node *phase_get_first_node(phase_t *phase);
+
+/**
+ * Returns the next node after @p start having some data assigned.
+ * @param phase The phase.
+ * @param start The node to start from
+ * @return The next node after start having some data assigned, NULL otherwise
+ */
+ir_node *phase_get_next_node(phase_t *phase, ir_node *start);
+
+/**
+ * Convenience macro to iterate over all nodes of a phase
+ * having some data assigned.
+ */
+#define foreach_phase_irn(phase, irn) \
+       for (irn = phase_get_first_node(phase); irn; irn = phase_get_next_node(phase, irn))
+
 /**
  * Get the name of the phase.
  */
@@ -121,6 +157,31 @@ void phase_reinit_irn_data(phase_t *phase);
  */
 #define phase_get_or_set_irn_data(ph, irn) _phase_get_or_set_irn_data((ph), (irn))
 
+/**
+ * Set the data for an irn.
+ * @param ph The phase.
+ * @param irn The node.
+ * @param data The data.
+ * @return The old data or NULL if there was none.
+ */
+#define phase_set_irn_data(ph, irn, data)  _phase_set_irn_data((ph), (irn), (data))
+
+/**
+ * This is private and only here for performance reasons.
+ */
+static INLINE void _phase_reinit_single_irn_data(phase_t *phase, ir_node *irn)
+{
+       int idx;
+
+       if (! phase->data_init)
+               return;
+
+       idx = get_irn_idx(irn);
+       if (phase->data_ptr[idx])
+               phase->data_init(phase, irn, phase->data_ptr[idx]);
+}
+
+
 /**
  * This is private and just here for performance reasons.
  */
@@ -134,7 +195,7 @@ static INLINE void _private_phase_enlarge(phase_t *phase, unsigned max_idx)
        max_idx = MAX(max_idx, last_irg_idx);
        new_cap = (size_t) (max_idx * phase->growth_factor / 256);
 
-       phase->data_ptr = (void **) realloc(phase->data_ptr, new_cap * sizeof(phase->data_ptr[0]));
+       phase->data_ptr = (void **)xrealloc(phase->data_ptr, new_cap * sizeof(phase->data_ptr[0]));
 
        /* initialize the newly allocated memory. */
        memset(phase->data_ptr + old_cap, 0, (new_cap - old_cap) * sizeof(phase->data_ptr[0]));
@@ -146,13 +207,28 @@ static INLINE void _private_phase_enlarge(phase_t *phase, unsigned max_idx)
  */
 #define _private_phase_assure_capacity(ph, max_idx) ((max_idx) >= (ph)->n_data_ptr ? (_private_phase_enlarge((ph), (max_idx)), 1) : 1)
 
-static INLINE void *_phase_get_irn_data(phase_t *ph, const ir_node *irn)
+static INLINE void *_phase_get_irn_data(const phase_t *ph, const ir_node *irn)
 {
        unsigned idx = get_irn_idx(irn);
        return idx < ph->n_data_ptr ? ph->data_ptr[idx] : NULL;
 }
 
-static INLINE void *_phase_get_or_set_irn_data(phase_t *ph, const ir_node *irn)
+static INLINE void *_phase_set_irn_data(phase_t *ph, const ir_node *irn, void *data)
+{
+       unsigned idx = get_irn_idx(irn);
+       void *res;
+
+       /* Assure that there's a sufficient amount of slots. */
+       _private_phase_assure_capacity(ph, idx);
+
+       res = ph->data_ptr[idx];
+       ph->data_ptr[idx] = data;
+
+       return res;
+}
+
+
+static INLINE void *_phase_get_or_set_irn_data(phase_t *ph, ir_node *irn)
 {
        unsigned idx = get_irn_idx(irn);
        void *res;
@@ -165,14 +241,12 @@ static INLINE void *_phase_get_or_set_irn_data(phase_t *ph, const ir_node *irn)
        /* If there has no irn data allocated yet, do that now. */
        if(!res) {
                phase_irn_data_init_t *data_init = ph->data_init;
-               ph->data_ptr[idx] = res = phase_alloc(ph, ph->data_size);
 
-               /* Call the irn data callback, if there is one. */
-               if(data_init)
-                       data_init(ph, irn, res);
+               /* call the node data structure allocator/constructor. */
+               res = ph->data_ptr[idx] = data_init(ph, irn, NULL);
+
        }
        return res;
 }
 
-
 #endif /* _FIRM_IR_PHASE_T_H */