identifiers starting with _ are reserved; remove this bad practice
[libfirm] / include / libfirm / irmemory.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    Memory disambiguator
23  * @author   Michael Beck
24  * @date     27.12.2006
25  * @version  $Id$
26  */
27 #ifndef FIRM_ANA_IRMEMORY_H
28 #define FIRM_ANA_IRMEMORY_H
29
30 #include "firm_types.h"
31 #include "begin.h"
32
33 /** The alias relation of two memory addresses. */
34 typedef enum {
35         ir_no_alias,       /**< No alias. */
36         ir_may_alias,      /**< Unknown state, may alias. */
37         ir_sure_alias      /**< Sure alias. */
38 } ir_alias_relation;
39
40 /** The state of the entity usage flags. */
41 typedef enum {
42         ir_entity_usage_not_computed,
43         ir_entity_usage_computed
44 } ir_entity_usage_computed_state;
45
46 /** Possible options for the memory disambiguator. */
47 typedef enum {
48         aa_opt_no_opt               = 0,  /**< no options: most conservative */
49         aa_opt_type_based           = 1,  /**< use type based alias analysis: strict typed source language */
50         aa_opt_byte_type_may_alias  = 2,  /**< if type based analysis is enabled: bytes types may alias other types */
51         aa_opt_no_alias_args        = 4,  /**< arguments do not alias each other but may alias global storage */
52         aa_opt_no_alias_args_global = 8,  /**< arguments do not alias global storage */
53         aa_opt_no_alias             = 16, /**< two addresses NEVER alias, use with CAUTION (gcc -fno-alias) */
54         aa_opt_inherited            = 128 /**< only for implementation: options from a graph are inherited from global */
55 } ir_disambuigator_options;
56
57 /**
58  * Classify storage locations.
59  * Except ir_sc_pointer they are all disjoint.
60  * ir_sc_pointer potentially aliases all classes which don't have a
61  * NOTTAKEN modifier.
62  */
63 typedef enum {
64         ir_sc_pointer           = 0x0,  /**< generic pointer, may be anything */
65         ir_sc_globalvar         = 0x1,  /**< an address of a global variable */
66         ir_sc_localvar          = 0x2,  /**< an address of a local variable or method argument */
67         ir_sc_tls               = 0x3,  /**< an address of a thread local storage variable */
68         ir_sc_malloced          = 0x4,  /**< an allocated heap address */
69         ir_sc_globaladdr        = 0x5,  /**< a constant address of something */
70
71         ir_sc_modifier_nottaken = 0x80, /**< if set, the address of the variable was not taken */
72         ir_sc_modifier_argument = 0x40, /**< if set pointer was a function argument */
73         ir_sc_modifiers         = ir_sc_modifier_nottaken | ir_sc_modifier_argument
74 } ir_storage_class_class_t;
75
76 /** Get the base storage class (ignore modifier) */
77 #define GET_BASE_SC(x) ((x) & ~ir_sc_modifiers)
78
79 /**
80  * A source language specific memory disambiguator function.
81  * Called by get_alias_relation().
82  */
83 typedef ir_alias_relation (*DISAMBIGUATOR_FUNC)(
84         const ir_graph *irg,
85         const ir_node *adr1, const ir_mode *mode1,
86         const ir_node *adr2, const ir_mode *mode2);
87
88 /**
89  * Classify a base pointer.
90  *
91  * @param irg  the graph of the pointer
92  * @param irn  the node representing the base address
93  * @param ent  the base entity of the base address iff any
94  */
95 FIRM_API ir_storage_class_class_t classify_pointer(const ir_graph *irg,
96                                                    const ir_node *irn,
97                                                    const ir_entity *ent);
98
99 /**
100  * Returns a human readable name for an alias relation.
101  */
102 FIRM_API const char *get_ir_alias_relation_name(ir_alias_relation rel);
103
104 /**
105  * Determine the alias relation between two addresses.
106  *
107  * @param irg     The current graph.
108  * @param adr1    The first address.
109  * @param mode1   The mode of the first memory access.
110  * @param adr2    The second address.
111  * @param mode2   The mode of the second memory access.
112  *
113  * The memory disambiguator tries to determine the alias state between
114  * two memory addresses. The following rules are used:
115  *
116  * - different variable from the same segment never alias (R1 a)
117  * - variables from different segments never alias when:
118  *   - a global variable and a local one never alias (R1 b)
119  *   - a global variable and a TLS one never alias (R1 c)
120  *   - a local variable and a TLS one never alias (R1 d)
121  *   - a local variable and a parameter never alias (R1 e)
122  *   - a global variable and the result of a malloc routine never alias (R1 f)
123  *   - a local variable and the result of a malloc routine never alias (R1 g)
124  *   - a TLS variable and the result of a malloc routine never alias (R1 h)
125  *   - a parameter and the result of a malloc routine (obtained in the
126  *     same routine as the parameter) never alias (R1 i)
127  * - two different variables never alias (R2)
128  * - if one is a variable whose address has never been taken
129  *   there is no alias (R3)
130  * - if two memory addresses have the same base and their offsets
131  *   do not describe overlapping regions there is no alias (R4)
132  * - if opt_strong_typed is set and both addresses describe entities,
133  *   different types never alias (R5)
134  *
135  * If none of these rules apply, the points-to framework must be
136  * interrogated to detect the alias relation.
137  */
138 FIRM_API ir_alias_relation get_alias_relation(
139         const ir_graph *irg,
140         const ir_node *adr1, const ir_mode *mode1,
141         const ir_node *adr2, const ir_mode *mode2);
142
143 /**
144  * Set a source language specific memory disambiguator function.
145  *
146  * @param func  The callback.
147  */
148 FIRM_API void set_language_memory_disambiguator(DISAMBIGUATOR_FUNC func);
149
150 /**
151  * Initialize the relation cache.
152  */
153 FIRM_API void mem_disambig_init(void);
154
155 /*
156  * Determine the alias relation between two addresses and
157  * cache the result.
158  *
159  * @param irg     The current graph.
160  * @param adr1    The first address.
161  * @param mode1   The mode of the first memory access.
162  * @param adr2    The second address.
163  * @param mode2   The mode of the second memory access.
164  *
165  * @see get_alias_relation()
166  */
167 FIRM_API ir_alias_relation get_alias_relation_ex(
168         const ir_graph *irg,
169         const ir_node *adr1, const ir_mode *mode1,
170         const ir_node *adr2, const ir_mode *mode2);
171
172 /**
173  * Free the relation cache.
174  */
175 FIRM_API void mem_disambig_term(void);
176
177 FIRM_API ir_entity_usage_computed_state get_irg_entity_usage_state(const ir_graph *irg);
178
179 FIRM_API void set_irg_entity_usage_state(ir_graph *irg,
180                                          ir_entity_usage_computed_state state);
181
182 /**
183  * Assure that the entity usage flags have been computed for the given graph.
184  *
185  * This analysis computes the entity usage state for all local variables.
186  *
187  * Note that this is a conservative estimation that by no Firm transformation
188  * can be invalidated, so it's only recomputed if manually triggered by calling
189  * set_irg_entity_usage_state(irg, ir_entity_usage_not_computed).
190  * Even then the information is not cleaned from the variables, call
191  * assure_irg_entity_usage_computed() again for recomputation.
192  */
193 FIRM_API void assure_irg_entity_usage_computed(ir_graph *irg);
194
195 /**
196  * Returns the current address taken state of the globals.
197  */
198 FIRM_API ir_entity_usage_computed_state get_irp_globals_entity_usage_state(void);
199
200 /**
201  * Sets the current address taken state of the globals.
202  *
203  * @param state  the new state
204  */
205 FIRM_API void set_irp_globals_entity_usage_state(ir_entity_usage_computed_state state);
206
207 /**
208  * Assure that the address taken flag is computed for the global and TLS entities (variables).
209  *
210  * This is an interprocedural analysis that computes the address_taken state
211  * for all global and TLS variables.
212  *
213  * Note that this is a conservative estimation that by no Firm transformation
214  * can be invalidated, so it's only recomputed if manually triggered by calling
215  * set_irp_globals_entity_usage_state(ir_entity_usage_not_computed).
216  * Even then the information is not cleaned from the variables, call
217  * assure_irp_globals_entity_usage_computed() again for recomputation.
218  */
219 FIRM_API void assure_irp_globals_entity_usage_computed(void);
220
221 /**
222  * Get the memory disambiguator options for a graph.
223  *
224  * @param irg  the graph
225  */
226 FIRM_API unsigned get_irg_memory_disambiguator_options(const ir_graph *irg);
227
228 /**
229  * Set the memory disambiguator options for a graph.
230  *
231  * @param irg      the graph
232  * @param options  a set of options
233  */
234 FIRM_API void set_irg_memory_disambiguator_options(ir_graph *irg,
235                                                    unsigned options);
236
237 /**
238  * Set the global disambiguator options for all graphs not having local options.
239  *
240  * @param options  a set of options
241  */
242 FIRM_API void set_irp_memory_disambiguator_options(unsigned options);
243
244 /**
245  * Mark all private methods, i.e. those of which all call sites are known.
246  * We use a very convervative estimation yet: If the address of a method is
247  * never taken AND its visibility is visibility_local, then it's private.
248  */
249 FIRM_API void mark_private_methods(void);
250
251 /**
252  * Creates an ir_prog pass for mark_private_methods().
253  *
254  * @param name     the name of this pass or NULL
255  *
256  * @return  the newly created ir_prog pass
257  */
258 FIRM_API ir_prog_pass_t *mark_private_methods_pass(const char *name);
259
260 #include "end.h"
261
262 #endif