New test.
[mono.git] / mono / mini / abcremoval.h
1 /*
2  * abcremoval.h: Array bounds check removal
3  *
4  * Author:
5  *   Massimiliano Mantione (massi@ximian.com)
6  *
7  * (C) 2004 Ximian, Inc.  http://www.ximian.com
8  */
9
10 #ifndef __MONO_ABCREMOVAL_H__
11 #define __MONO_ABCREMOVAL_H__
12
13 #include <limits.h>
14
15 #include "mini.h"
16
17
18 /**
19  * All handled value types (expressions) in variable definitions and branch
20  * contitions:
21  * ANY: not handled
22  * CONSTANT: an integer constant
23  * VARIABLE: a reference to a variable, with an optional delta (can be zero)
24  * PHI: a PHI definition of the SSA representation
25  */
26 typedef enum {
27         MONO_ANY_SUMMARIZED_VALUE,
28         MONO_CONSTANT_SUMMARIZED_VALUE,
29         MONO_VARIABLE_SUMMARIZED_VALUE,
30         MONO_PHI_SUMMARIZED_VALUE
31 } MonoSummarizedValueType;
32
33 /**
34  * A MONO_CONSTANT_SUMMARIZED_VALUE value.
35  * value: the value
36  */
37 typedef struct MonoSummarizedConstantValue {
38         int value;
39 } MonoSummarizedConstantValue;
40
41 /**
42  * A MONO_VARIABLE_SUMMARIZED_VALUE value
43  * variable: the variable index in the cfg
44  * delta: the delta (can be zero)
45  */
46 typedef struct MonoSummarizedVariableValue {
47         int variable;
48         int delta;
49 } MonoSummarizedVariableValue;
50
51 /**
52  * A MONO_PHI_SUMMARIZED_VALUE value.
53  * number_of_alternatives: the number of alternatives in the PHI definition
54  * phi_alternatives: an array of integers with the indexes of the variables
55  *                   which are the alternatives in this PHI definition
56  */
57 typedef struct MonoSummarizedPhiValue {
58         int number_of_alternatives;
59         int *phi_alternatives;
60 } MonoSummarizedPhiValue;
61
62 /**
63  * A summarized value.
64  * In practice it is a "tagged union".
65  */
66 typedef struct MonoSummarizedValue {
67         MonoSummarizedValueType type;
68         union {
69                 MonoSummarizedConstantValue constant;
70                 MonoSummarizedVariableValue variable;
71                 MonoSummarizedPhiValue phi;
72         } value;
73 } MonoSummarizedValue;
74
75 /**
76  * A "relation" between two values.
77  * The enumeration is used as a bit field, with three significant bits.
78  * The degenerated cases are meaningful:
79  * MONO_ANY_RELATION: we know nothing of this relation
80  * MONO_NO_RELATION: no relation is possible (this code is unreachable)
81  */
82 typedef enum {
83         MONO_EQ_RELATION = 1,
84         MONO_LT_RELATION = 2,
85         MONO_GT_RELATION = 4,
86         MONO_NE_RELATION = (MONO_LT_RELATION|MONO_GT_RELATION),
87         MONO_LE_RELATION = (MONO_LT_RELATION|MONO_EQ_RELATION),
88         MONO_GE_RELATION = (MONO_GT_RELATION|MONO_EQ_RELATION),
89         MONO_ANY_RELATION = (MONO_EQ_RELATION|MONO_LT_RELATION|MONO_GT_RELATION),
90         MONO_NO_RELATION = 0
91 } MonoValueRelation;
92
93 /**
94  * A "kind" of integer value.
95  * The enumeration is used as a bit field, with two fields.
96  * The first, four bits wide, is the "sizeof" in bytes.
97  * The second is a flag that is true if the value is unsigned.
98  */
99 typedef enum {
100         MONO_INTEGER_VALUE_SIZE_1 = 1,
101         MONO_INTEGER_VALUE_SIZE_2 = 2,
102         MONO_INTEGER_VALUE_SIZE_4 = 4,
103         MONO_INTEGER_VALUE_SIZE_8 = 8,
104         MONO_INTEGER_VALUE_SIZE_BITMASK = 15,
105         MONO_UNSIGNED_VALUE_FLAG = 16,
106         MONO_UNSIGNED_INTEGER_VALUE_SIZE_1 = MONO_UNSIGNED_VALUE_FLAG|MONO_INTEGER_VALUE_SIZE_1,
107         MONO_UNSIGNED_INTEGER_VALUE_SIZE_2 = MONO_UNSIGNED_VALUE_FLAG|MONO_INTEGER_VALUE_SIZE_2,
108         MONO_UNSIGNED_INTEGER_VALUE_SIZE_4 = MONO_UNSIGNED_VALUE_FLAG|MONO_INTEGER_VALUE_SIZE_4,
109         MONO_UNSIGNED_INTEGER_VALUE_SIZE_8 = MONO_UNSIGNED_VALUE_FLAG|MONO_INTEGER_VALUE_SIZE_8,
110         MONO_UNKNOWN_INTEGER_VALUE = 0
111 } MonoIntegerValueKind;
112
113 /**
114  * A relation between variables (or a variable and a constant).
115  * The first variable (the one "on the left of the expression") is implicit.
116  * relation: the relation between the variable and the value
117  * related_value: the related value
118  * relation_is_static_definition: TRUE if the relation comes from a veriable
119  *                                definition, FALSE if it comes from a branch
120  *                                condition
121  * next: pointer to the next relation of this variable in the evaluation area
122  *       (relations are always kept in the evaluation area, one list for each
123  *       variable)
124  */
125 typedef struct MonoSummarizedValueRelation {
126         MonoValueRelation relation;
127         MonoSummarizedValue related_value;
128         gboolean relation_is_static_definition;
129         struct MonoSummarizedValueRelation *next;
130 } MonoSummarizedValueRelation;
131
132 /**
133  * The evaluation status for one variable.
134  * The enumeration is used as a bit field, because the status has two
135  * distinct sections.
136  * The first is the "main" one (bits 0, 1 and 2), which is actually a proper
137  * enumeration (the bits are mutually exclusive, and their meaning is obvious).
138  * The other section (the bits in the MONO_RELATIONS_EVALUATION_IS_RECURSIVE
139  * set) is used to mark an evaluation as recursive (while backtracking through
140  * the evaluation contexts), to state if the graph loop gives a value that is
141  * ascending, descending or indefinite.
142  * The bits are handled separately because the same evaluation context could
143  * belong to more than one loop, so that each loop would set its bits.
144  * After the backtracking, the bits are examined and a decision is taken.
145  * 
146  */
147 typedef enum {
148         MONO_RELATIONS_EVALUATION_NOT_STARTED = 0,
149         MONO_RELATIONS_EVALUATION_IN_PROGRESS = 1,
150         MONO_RELATIONS_EVALUATION_COMPLETED = 2,
151         MONO_RELATIONS_EVALUATION_IS_RECURSIVELY_ASCENDING = 4,
152         MONO_RELATIONS_EVALUATION_IS_RECURSIVELY_DESCENDING = 8,
153         MONO_RELATIONS_EVALUATION_IS_RECURSIVELY_INDEFINITE = 16,
154         MONO_RELATIONS_EVALUATION_IS_RECURSIVE = (MONO_RELATIONS_EVALUATION_IS_RECURSIVELY_ASCENDING|MONO_RELATIONS_EVALUATION_IS_RECURSIVELY_DESCENDING|MONO_RELATIONS_EVALUATION_IS_RECURSIVELY_INDEFINITE)
155 } MonoRelationsEvaluationStatus;
156
157 /**
158  * A range of values (ranges include their limits).
159  * A range from MIN_INT to MAX_INT is "indefinite" (any value).
160  * A range where upper < lower means unreachable code (some of the relations
161  * that generated the range is incompatible, like x = 0 and x > 0).
162  * lower: the lower limit
163  * upper: the upper limit
164  */
165 typedef struct MonoRelationsEvaluationRange {
166         int lower;
167         int upper;
168 } MonoRelationsEvaluationRange;
169
170 /**
171  * The two ranges that contain the result of a variable evaluation.
172  * zero: the range with respect to zero
173  * variable: the range with respect to the target variable in this evaluation
174  */
175 typedef struct MonoRelationsEvaluationRanges {
176         MonoRelationsEvaluationRange zero;
177         MonoRelationsEvaluationRange variable;
178 } MonoRelationsEvaluationRanges;
179
180 /**
181  * The context of a variable evaluation.
182  * status: the evaluation status
183  * current_relation: the relation that is currently evaluated.
184  * ranges: the result of the evaluation.
185  * father: the context of the evaluation that invoked this one (used to
186  *         perform the backtracking when loops are detected.
187  */
188 typedef struct MonoRelationsEvaluationContext {
189         MonoRelationsEvaluationStatus status;
190         MonoSummarizedValueRelation *current_relation;
191         MonoRelationsEvaluationRanges ranges;
192         struct MonoRelationsEvaluationContext *father;
193 } MonoRelationsEvaluationContext;
194
195 /*
196  * Basic macros to initialize and check ranges.
197  */
198 #define MONO_MAKE_RELATIONS_EVALUATION_RANGE_WEAK(r) do{\
199                 (r).lower = INT_MIN;\
200                 (r).upper = INT_MAX;\
201         } while (0)
202 #define MONO_MAKE_RELATIONS_EVALUATION_RANGES_WEAK(rs) do{\
203                 MONO_MAKE_RELATIONS_EVALUATION_RANGE_WEAK ((rs).zero); \
204                 MONO_MAKE_RELATIONS_EVALUATION_RANGE_WEAK ((rs).variable); \
205         } while (0)
206 #define MONO_MAKE_RELATIONS_EVALUATION_RANGE_IMPOSSIBLE(r) do{\
207                 (r).lower = INT_MAX;\
208                 (r).upper = INT_MIN;\
209         } while (0)
210 #define MONO_MAKE_RELATIONS_EVALUATION_RANGES_IMPOSSIBLE(rs) do{\
211                 MONO_MAKE_RELATIONS_EVALUATION_RANGE_IMPOSSIBLE ((rs).zero); \
212                 MONO_MAKE_RELATIONS_EVALUATION_RANGE_IMPOSSIBLE ((rs).variable); \
213         } while (0)
214 #define MONO_RELATIONS_EVALUATION_RANGE_IS_WEAK(r) (((r).lower==INT_MIN)&&((r).upper==INT_MAX))
215 #define MONO_RELATIONS_EVALUATION_RANGES_ARE_WEAK(rs) \
216         (MONO_RELATIONS_EVALUATION_RANGE_IS_WEAK((rs).zero) && \
217         MONO_RELATIONS_EVALUATION_RANGE_IS_WEAK((rs).variable))
218 #define MONO_RELATIONS_EVALUATION_RANGE_IS_IMPOSSIBLE(r) (((r).lower)>((r).upper))
219 #define MONO_RELATIONS_EVALUATION_RANGES_ARE_IMPOSSIBLE(rs) \
220         (MONO_RELATIONS_EVALUATION_RANGE_IS_IMPOSSIBLE((rs).zero) || \
221         MONO_RELATIONS_EVALUATION_RANGE_IS_IMPOSSIBLE((rs).variable))
222
223 /*
224  * The following macros are needed because ranges include theit limits, but
225  * some relations explicitly exclude them (GT and LT).
226  */
227 #define MONO_UPPER_EVALUATION_RANGE_NOT_EQUAL(ur) ((((ur)==INT_MIN)||((ur)==INT_MAX))?(ur):((ur)-1))
228 #define MONO_LOWER_EVALUATION_RANGE_NOT_EQUAL(lr) ((((lr)==INT_MIN)||((lr)==INT_MAX))?(lr):((lr)+1))
229 #define MONO_APPLY_INEQUALITY_TO_EVALUATION_RANGE(r) do{\
230                 (r).lower = MONO_LOWER_EVALUATION_RANGE_NOT_EQUAL ((r).lower);\
231                 (r).upper = MONO_UPPER_EVALUATION_RANGE_NOT_EQUAL ((r).upper);\
232         } while (0)
233 #define MONO_APPLY_INEQUALITY_TO_EVALUATION_RANGES(rs) do{\
234                 MONO_APPLY_INEQUALITY_TO_EVALUATION_RANGE ((rs).zero); \
235                 MONO_APPLY_INEQUALITY_TO_EVALUATION_RANGE ((rs).variable); \
236         } while (0)
237
238 /*
239  * The following macros perform union and intersection operations on ranges.
240  */
241 #define MONO_LOWER_EVALUATION_RANGE_UNION(lr,other_lr) ((lr)=MIN(lr,other_lr))
242 #define MONO_UPPER_EVALUATION_RANGE_UNION(ur,other_ur) ((ur)=MAX(ur,other_ur))
243 #define MONO_LOWER_EVALUATION_RANGE_INTERSECTION(lr,other_lr) ((lr)=MAX(lr,other_lr))
244 #define MONO_UPPER_EVALUATION_RANGE_INTERSECTION(ur,other_ur) ((ur)=MIN(ur,other_ur))
245 #define MONO_RELATIONS_EVALUATION_RANGE_UNION(r,other_r) do{\
246                 MONO_LOWER_EVALUATION_RANGE_UNION((r).lower,(other_r).lower);\
247                 MONO_UPPER_EVALUATION_RANGE_UNION((r).upper,(other_r).upper);\
248         } while (0)
249 #define MONO_RELATIONS_EVALUATION_RANGE_INTERSECTION(r,other_r) do{\
250                 MONO_LOWER_EVALUATION_RANGE_INTERSECTION((r).lower,(other_r).lower);\
251                 MONO_UPPER_EVALUATION_RANGE_INTERSECTION((r).upper,(other_r).upper);\
252         } while (0)
253 #define MONO_RELATIONS_EVALUATION_RANGES_UNION(rs,other_rs) do{\
254                 MONO_RELATIONS_EVALUATION_RANGE_UNION ((rs).zero,(other_rs).zero); \
255                 MONO_RELATIONS_EVALUATION_RANGE_UNION ((rs).variable,(other_rs).variable); \
256         } while (0)
257 #define MONO_RELATIONS_EVALUATION_RANGES_INTERSECTION(rs,other_rs) do{\
258                 MONO_RELATIONS_EVALUATION_RANGE_INTERSECTION ((rs).zero,(other_rs).zero); \
259                 MONO_RELATIONS_EVALUATION_RANGE_INTERSECTION ((rs).variable,(other_rs).variable); \
260         } while (0)
261
262 /*
263  * The following macros add or subtract "safely" (without over/under-flow) a
264  * delta (constant) value to a range.
265  */
266 #define MONO_ADD_DELTA_SAFELY(v,d) do{\
267                 if (((d) > 0) && ((v) != INT_MIN)) {\
268                         (v) = (((v)+(d))>(v))?((v)+(d)):INT_MAX;\
269                 } else if (((d) < 0) && ((v) != INT_MAX)) {\
270                         (v) = (((v)+(d))<(v))?((v)+(d)):INT_MIN;\
271                 }\
272         } while (0)
273 #define MONO_SUB_DELTA_SAFELY(v,d) do{\
274                 if (((d) < 0) && ((v) != INT_MIN)) {\
275                         (v) = (((v)-(d))>(v))?((v)-(d)):INT_MAX;\
276                 } else if (((d) > 0) && ((v) != INT_MAX)) {\
277                         (v) = (((v)-(d))<(v))?((v)-(d)):INT_MIN;\
278                 }\
279         } while (0)
280 #define MONO_ADD_DELTA_SAFELY_TO_RANGE(r,d) do{\
281                 MONO_ADD_DELTA_SAFELY((r).lower,(d));\
282                 MONO_ADD_DELTA_SAFELY((r).upper,(d));\
283         } while (0)
284 #define MONO_SUB_DELTA_SAFELY_FROM_RANGE(r,d) do{\
285                 MONO_SUB_DELTA_SAFELY((r).lower,(d));\
286                 MONO_SUB_DELTA_SAFELY((r).upper,(d));\
287         } while (0)
288 #define MONO_ADD_DELTA_SAFELY_TO_RANGES(rs,d) do{\
289                 MONO_ADD_DELTA_SAFELY_TO_RANGE((rs).zero,(d));\
290                 MONO_ADD_DELTA_SAFELY_TO_RANGE((rs).variable,(d));\
291         } while (0)
292 #define MONO_SUB_DELTA_SAFELY_FROM_RANGES(rs,d) do{\
293                 MONO_SUB_DELTA_SAFELY_FROM_RANGE((rs).zero,(d));\
294                 MONO_SUB_DELTA_SAFELY_FROM_RANGE((rs).variable,(d));\
295         } while (0)
296
297
298 /**
299  * The main evaluation area.
300  * cfg: the cfg of the method that is being examined.
301  * relations: and array of relations, one for each method variable (each
302  *            relation is the head of a list); this is the evaluation graph
303  * contexts: an array of evaluation contexts (one for each method variable)
304  * variable_value_kind: an array of MonoIntegerValueKind, one for each local
305  *                      variable (or argument)
306  */
307 typedef struct MonoVariableRelationsEvaluationArea {
308         MonoCompile *cfg;
309         MonoSummarizedValueRelation *relations;
310         MonoRelationsEvaluationContext *contexts;
311         MonoIntegerValueKind *variable_value_kind;
312 } MonoVariableRelationsEvaluationArea;
313
314 /**
315  * Convenience structure to define an "additional" relation for the main
316  * evaluation graph.
317  * variable: the variable to which the relation is applied
318  * relation: the relation
319  * insertion_point: the point in the graph where the relation is inserted
320  *                  (useful for removing it from the list when backtracking
321  *                  in the traversal of the dominator tree)
322  */
323 typedef struct MonoAdditionalVariableRelation {
324         int variable;
325         MonoSummarizedValueRelation relation;
326         MonoSummarizedValueRelation *insertion_point;
327 } MonoAdditionalVariableRelation;
328
329 /**
330  * Convenience structure that stores two additional relations.
331  * In the current code, each BB can add at most two relations to the main
332  * evaluation graph, so one of these structures is enough to hold all the
333  * modifications to the graph made examining one BB.
334  */
335 typedef struct MonoAdditionalVariableRelationsForBB {
336         MonoAdditionalVariableRelation relation1;
337         MonoAdditionalVariableRelation relation2;
338 } MonoAdditionalVariableRelationsForBB;
339
340
341 #endif /* __MONO_ABCREMOVAL_H__ */