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 A nodemap. This variant is a thin wrapper around an ARR_F which
23 * uses node-indices for access. It is preferable over ir_nodehashmap
24 * if the info is dense (i.e. something is mapped for most nodes in
26 * @author Matthias Braun
28 #ifndef FIRM_IRNODEMAP_H
29 #define FIRM_IRNODEMAP_H
31 #include "firm_types.h"
32 #include "irgraph_t.h"
35 typedef struct ir_nodemap ir_nodemap;
38 * Allocate and initialize a new nodemap object
40 * @param irg The graph the nodemap will run on.
41 * @return A new nodemap object.
43 static inline void ir_nodemap_init(ir_nodemap *nodemap, const ir_graph *irg)
45 unsigned max_idx = get_irg_last_idx(irg) + 32;
46 nodemap->data = NEW_ARR_F(void*, max_idx);
47 memset(nodemap->data, 0, max_idx * sizeof(nodemap->data[0]));
51 * frees all internal memory used by the nodemap but does not free the
52 * nodemap struct itself.
54 static inline void ir_nodemap_destroy(ir_nodemap *nodemap)
56 DEL_ARR_F(nodemap->data);
61 * Insert a mapping from @p node to @p data.
63 static inline void ir_nodemap_insert(ir_nodemap *nodemap, const ir_node *node,
66 unsigned idx = get_irn_idx(node);
67 size_t len = ARR_LEN(nodemap->data);
69 ARR_RESIZE(void*, nodemap->data, idx+1);
70 memset(nodemap->data + len, 0, (idx-len) * sizeof(nodemap->data[0]));
72 nodemap->data[idx] = data;
76 * Insert a mapping from @p node to @p data (fast version).
78 * @attention You must only use this version if you can be sure that the nodemap
79 * already has enough space to store the mapping. This is the case if @p node
80 * already existed at nodemap_init() time or ir_nodemap_insert() has been used
83 static inline void ir_nodemap_insert_fast(ir_nodemap *nodemap,
84 const ir_node *node, void *data)
86 unsigned idx = get_irn_idx(node);
87 nodemap->data[idx] = data;
91 * Removed mapping for @p node.
93 * This is really a shorthand form for ir_nodemap_insert(nodemap, node, NULL);
95 static inline void ir_nodemap_remove(ir_nodemap *nodemap, const ir_node *node)
97 ir_nodemap_insert(nodemap, node, NULL);
101 * Get mapping for @p node. Returns NULL if nothing is mapped.
103 static inline void *ir_nodemap_get(const ir_nodemap *nodemap,
106 unsigned idx = get_irn_idx(node);
107 if (idx >= ARR_LEN(nodemap->data))
109 return nodemap->data[idx];
112 #define ir_nodemap_get(type, nodemap, node) ((type*)ir_nodemap_get(nodemap, node))
115 * Get mapping for @p node (fast version). Returns NULL if nothing is mapped.
117 * @attention You must only use this function if you can be sure that the
118 * nodemap has enough space to potentially contain the mapping. This is the
119 * case if @p node already existed at nodemap_init() time or ir_nodemap_insert()
120 * has been used for this node)
122 static inline void *ir_nodemap_get_fast(const ir_nodemap *nodemap,
125 unsigned idx = get_irn_idx(node);
126 return nodemap->data[idx];