Merge pull request #301 from directhex/master
[mono.git] / mono / mini / abcremoval.c
1 /*
2  * abcremoval.c: 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 #include <string.h>
10 #include <stdio.h>
11
12 #include <mono/metadata/debug-helpers.h>
13 #include <mono/metadata/mempool.h>
14 #include <mono/metadata/opcodes.h>
15 #include <mono/metadata/mempool-internals.h>
16
17 #include <config.h>
18
19 #ifndef DISABLE_JIT
20
21 #include "abcremoval.h"
22
23 #if SIZEOF_VOID_P == 8
24 #define OP_PCONST OP_I8CONST
25 #else
26 #define OP_PCONST OP_ICONST
27 #endif
28
29
30 #define TRACE_ABC_REMOVAL (verbose_level > 2)
31 #define REPORT_ABC_REMOVAL (verbose_level > 1)
32
33 /*
34  * A little hack for the verbosity level.
35  * The verbosity level is stored in the cfg, but not all functions that must
36  * print something see the cfg, so we store the verbosity level here at the
37  * beginning of the algorithm.
38  * This is not thread safe (does not handle correctly different verbosity
39  * levels in different threads), and is not exact in case of dynamic changes
40  * of the verbosity level...
41  * Anyway, this is not needed, all that can happen is that something more
42  * (or less) is logged, the result is in any case correct.
43  */
44 static int verbose_level;
45
46
47 #define RELATION_BETWEEN_VALUES(value,related_value) (\
48         ((value) > (related_value))? MONO_GT_RELATION :\
49         (((value) < (related_value))? MONO_LT_RELATION : MONO_EQ_RELATION))
50
51 #define MAKE_VALUE_ANY(v) do{\
52                 (v).type = MONO_ANY_SUMMARIZED_VALUE;\
53         } while (0)
54
55 #define MAKE_VALUE_RELATION_ANY(r) do{\
56                 (r)->relation = MONO_ANY_RELATION;\
57                 MAKE_VALUE_ANY((r)->related_value);\
58         } while (0)
59
60 #define INITIALIZE_VALUE_RELATION(r) do{\
61                 MAKE_VALUE_RELATION_ANY((r));\
62                 (r)->next = NULL;\
63         } while (0)
64
65 #define MONO_NEGATED_RELATION(r) ((~(r))&MONO_ANY_RELATION)
66 #define MONO_SYMMETRIC_RELATION(r) (((r)&MONO_EQ_RELATION)|(((r)&MONO_LT_RELATION)<<1)|((r&MONO_GT_RELATION)>>1))
67
68
69
70 static void
71 print_relation (int relation) {
72         int print_or = 0;
73         printf ("(");
74         if (relation & MONO_LT_RELATION) {
75                 printf ("LT");
76                 print_or = 1;
77         }
78         if (relation & MONO_EQ_RELATION) {
79                 if (print_or) {
80                         printf ("|");
81                 }
82                 printf ("EQ");
83                 print_or = 1;
84         }
85         if (relation & MONO_GT_RELATION) {
86                 if (print_or) {
87                         printf ("|");
88                 }
89                 printf ("GT");
90                 print_or = 1;
91         }
92         printf (")");
93 }
94
95 static void
96 print_summarized_value (MonoSummarizedValue *value) {
97         switch (value->type) {
98         case MONO_ANY_SUMMARIZED_VALUE:
99                 printf ("ANY");
100                 break;
101         case MONO_CONSTANT_SUMMARIZED_VALUE:
102                 printf ("CONSTANT %d", value->value.constant.value);
103                 break;
104         case MONO_VARIABLE_SUMMARIZED_VALUE:
105                 printf ("VARIABLE %d, delta %d", value->value.variable.variable, value->value.variable.delta);
106                 break;
107         case MONO_PHI_SUMMARIZED_VALUE: {
108                 int phi;
109                 printf ("PHI (");
110                 for (phi = 0; phi < value->value.phi.number_of_alternatives; phi++) {
111                         if (phi) printf (",");
112                         printf ("%d", value->value.phi.phi_alternatives [phi]);
113                 }
114                 printf (")");
115                 break;
116         }
117         default:
118                 g_assert_not_reached ();
119         }
120 }
121
122 static void
123 print_summarized_value_relation (MonoSummarizedValueRelation *relation) {
124         printf ("Relation ");
125         print_relation (relation->relation);
126         printf (" with value ");
127         print_summarized_value (&(relation->related_value));
128 }
129
130 #if 0
131 static void
132 print_summarized_value_relation_chain (MonoSummarizedValueRelation *relation) {
133         printf ("Relations:\n");
134         while (relation) {
135                 print_summarized_value_relation (relation);
136                 printf ("\n");
137                 relation = relation->next;
138         }
139 }
140 #endif
141
142 static void
143 print_evaluation_context_status (MonoRelationsEvaluationStatus status) {
144         if (status == MONO_RELATIONS_EVALUATION_NOT_STARTED) {
145                 printf ("EVALUATION_NOT_STARTED");
146         } else {
147                 gboolean print_or = FALSE;
148                 
149                 printf ("(");
150                 if (status & MONO_RELATIONS_EVALUATION_IN_PROGRESS) {
151                         if (print_or) printf ("|");
152                         printf ("EVALUATION_IN_PROGRESS");
153                         print_or = TRUE;
154                 }
155                 if (status & MONO_RELATIONS_EVALUATION_COMPLETED) {
156                         if (print_or) printf ("|");
157                         printf ("EVALUATION_COMPLETED");
158                         print_or = TRUE;
159                 }
160                 if (status & MONO_RELATIONS_EVALUATION_IS_RECURSIVELY_ASCENDING) {
161                         if (print_or) printf ("|");
162                         printf ("RECURSIVELY_ASCENDING");
163                         print_or = TRUE;
164                 }
165                 if (status & MONO_RELATIONS_EVALUATION_IS_RECURSIVELY_DESCENDING) {
166                         if (print_or) printf ("|");
167                         printf ("RECURSIVELY_DESCENDING");
168                         print_or = TRUE;
169                 }
170                 if (status & MONO_RELATIONS_EVALUATION_IS_RECURSIVELY_INDEFINITE) {
171                         if (print_or) printf ("|");
172                         printf ("RECURSIVELY_INDEFINITE");
173                         print_or = TRUE;
174                 }
175                 printf (")");
176         }
177 }
178
179
180 static void
181 print_evaluation_context_ranges (MonoRelationsEvaluationRanges *ranges) {
182         printf ("(ranges: zero [%d,%d], variable [%d,%d])", ranges->zero.lower, ranges->zero.upper, ranges->variable.lower, ranges->variable.upper);
183 }
184
185 static void
186 print_evaluation_context (MonoRelationsEvaluationContext *context) {
187         printf ("Context status: ");
188         print_evaluation_context_status (context->status);
189         if (context->status & (MONO_RELATIONS_EVALUATION_IN_PROGRESS|MONO_RELATIONS_EVALUATION_COMPLETED)) {
190                 print_evaluation_context_ranges (&(context->ranges));
191         }
192         printf ("\n");
193 }
194
195 #if 0
196 static void
197 print_evaluation_area (MonoVariableRelationsEvaluationArea *area) {
198         int i;
199         printf ("Dump of evaluation area (%d variables):\n", area->cfg->num_varinfo);
200         for (i = 0; i < area->cfg->num_varinfo; i++) {
201                 printf ("Variable %d: ", i);
202                 print_evaluation_context (&(area->contexts [i]));
203                 print_summarized_value_relation_chain (&(area->relations [i]));
204         }
205 }
206
207 static void
208 print_evaluation_area_contexts (MonoVariableRelationsEvaluationArea *area) {
209         int i;
210         printf ("Dump of evaluation area contexts (%d variables):\n", area->cfg->num_varinfo);
211         for (i = 0; i < area->cfg->num_varinfo; i++) {
212                 printf ("Variable %d: ", i);
213                 print_evaluation_context (&(area->contexts [i]));
214         }
215 }
216 #endif
217
218 /*
219  * Check if the delta of an integer variable value is safe with respect
220  * to the variable size in bytes and its kind (signed or unsigned).
221  * If the delta is not safe, make the value an "any".
222  */
223 static G_GNUC_UNUSED void
224 check_delta_safety (MonoVariableRelationsEvaluationArea *area, MonoSummarizedValue *value) {
225         if (value->type == MONO_VARIABLE_SUMMARIZED_VALUE) {
226                 int variable = value->value.variable.variable;
227                 int delta = value->value.variable.delta;
228                 if ((area->variable_value_kind [variable]) & MONO_UNSIGNED_VALUE_FLAG) {
229                         if (delta < 0) {
230                                 MAKE_VALUE_ANY (*value);
231                         }
232                 } else {
233                         if (((area->variable_value_kind [variable]) & MONO_INTEGER_VALUE_SIZE_BITMASK) < 4) {
234                                 MAKE_VALUE_ANY (*value);
235                         } else if (delta > 16) {
236                                 MAKE_VALUE_ANY (*value);
237                         }
238                 }
239         }
240 }
241
242 /*
243  * get_relation_from_ins:
244  *
245  *   Obtain relations from a MonoInst.
246  *
247  * result_value_kind: the "expected" kind of result;
248  * result: the "summarized" value
249  * returns the "actual" kind of result, if guessable (otherwise MONO_UNKNOWN_INTEGER_VALUE)
250  */
251 static MonoIntegerValueKind
252 get_relation_from_ins (MonoVariableRelationsEvaluationArea *area, MonoInst *ins, MonoSummarizedValueRelation *result, MonoIntegerValueKind result_value_kind)
253 {
254         MonoIntegerValueKind value_kind;
255         MonoSummarizedValue *value = &result->related_value;
256         
257         if (ins->type == STACK_I8) {
258                 value_kind = MONO_INTEGER_VALUE_SIZE_8;
259         } else if (ins->type == STACK_I4) {
260                 value_kind = MONO_INTEGER_VALUE_SIZE_4;
261         } else {
262                 value_kind = MONO_UNKNOWN_INTEGER_VALUE;
263         }
264
265         result->relation = MONO_EQ_RELATION;
266         MAKE_VALUE_ANY (*value);
267
268         switch (ins->opcode) {
269         case OP_ICONST:
270                 value->type = MONO_CONSTANT_SUMMARIZED_VALUE;
271                 value->value.constant.value = ins->inst_c0;
272                 break;
273         case OP_MOVE:
274                 value->type = MONO_VARIABLE_SUMMARIZED_VALUE;
275                 value->value.variable.variable = ins->sreg1;
276                 value->value.variable.delta = 0;
277                 break;
278         case OP_PHI:
279                 value->type = MONO_PHI_SUMMARIZED_VALUE;
280                 value->value.phi.number_of_alternatives = *(ins->inst_phi_args);
281                 value->value.phi.phi_alternatives = ins->inst_phi_args + 1;
282                 break;
283         case OP_IADD_IMM:
284                 value->type = MONO_VARIABLE_SUMMARIZED_VALUE;
285                 value->value.variable.variable = ins->sreg1;
286                 value->value.variable.delta = ins->inst_imm;
287                 /* FIXME: */
288                 //check_delta_safety (area, result);
289                 break;
290         case OP_ISUB_IMM:
291                 value->type = MONO_VARIABLE_SUMMARIZED_VALUE;
292                 value->value.variable.variable = ins->sreg1;
293                 value->value.variable.delta = ins->inst_imm;
294                 /* FIXME: */
295                 //check_delta_safety (area, result);
296                 break;
297         case OP_IREM_UN:
298                 /* The result of an unsigned remainder is 0 < x < the divisor */
299                 result->relation = MONO_LT_RELATION;
300                 value->type = MONO_VARIABLE_SUMMARIZED_VALUE;
301                 value->value.variable.variable = ins->sreg2;
302                 value->value.variable.delta = 0;
303                 value_kind = MONO_UNSIGNED_INTEGER_VALUE_SIZE_4;
304                 break;
305         case OP_LDLEN:
306                 /*
307                  * We represent arrays by their length, so r1<-ldlen r2 is stored
308                  * as r1 == r2 in the evaluation graph.
309                  */
310                 value->type = MONO_VARIABLE_SUMMARIZED_VALUE;
311                 value->value.variable.variable = ins->sreg1;
312                 value->value.variable.delta = 0;
313                 value_kind = MONO_UNSIGNED_INTEGER_VALUE_SIZE_4;
314                 break;
315         case OP_NEWARR:
316                 value->type = MONO_VARIABLE_SUMMARIZED_VALUE;
317                 value->value.variable.variable = ins->sreg1;
318                 value->value.variable.delta = 0;
319                 area->defs [ins->dreg] = ins;
320                 break;
321         case OP_LDADDR:
322                 /* The result is non-null */
323                 result->relation = MONO_GT_RELATION;
324                 value->type = MONO_CONSTANT_SUMMARIZED_VALUE;
325                 value->value.constant.value = 0;
326                 break;
327
328                 /* FIXME: Add more opcodes */
329         default:
330                 /* These opcodes are not currently handled while running SciMark, first
331                  * column is the number of times the warning was shown:
332                  *
333                  *       1 add_imm
334                  *       1 float_conv_to_i8
335                  *       1 int_mul_ovf_un
336                  *       1 int_neg
337                  *       1 int_or
338                  *       1 int_shr_un
339                  *       1 localloc
340                  *       1 long_ceq
341                  *       1 long_rem
342                  *       1 long_sub
343                  *       2 int_ceq
344                  *       2 int_conv_to_i2
345                  *       2 int_min
346                  *       2 lcall
347                  *       2 long_div
348                  *       3 int_conv_to_u2
349                  *       3 long_shr_imm
350                  *       4 int_rem
351                  *       4 int_rem_imm
352                  *       4 loadi1_membase
353                  *       4 loadu4_membase
354                  *       5 int_div
355                  *       5 shl_imm
356                  *       6 int_div_imm
357                  *       6 int_mul
358                  *       9 int_mul_imm
359                  *       9 zext_i4
360                  *      10 int_shr_imm
361                  *      12 int_shr_un_imm
362                  *      12 long_add_imm
363                  *      12 outarg_vtretaddr
364                  *      12 strlen
365                  *      13 int_or_imm
366                  *      23 call_membase
367                  *      23 int_conv_to_u1
368                  *      23 long_add
369                  *      24 int_and_imm
370                  *      24 int_shl_imm
371                  *      24 loadu2_membase
372                  *      29 loadi8_membase
373                  *      31 llvm_outarg_vt
374                  *      34 int_sub
375                  *      34 loadu1_membase
376                  *      42 int_add
377                  *      85 ldaddr
378                  *     116 loadi4_membase
379                  *     159 x86_lea
380                  *     179 sext_i4
381                  *     291 load_membase
382                  *     462 i8const
383                  *     472 call
384                  */
385                 
386                 break;
387         }
388         return value_kind;
389 }
390
391 static MonoValueRelation
392 get_relation_from_branch_instruction (MonoInst *ins)
393 {
394         if (MONO_IS_COND_BRANCH_OP (ins)) {
395                 CompRelation rel = mono_opcode_to_cond (ins->opcode);
396
397                 switch (rel) {
398                 case CMP_EQ:
399                         return MONO_EQ_RELATION;
400                 case CMP_NE:
401                         return MONO_NE_RELATION;
402                 case CMP_LE:
403                 case CMP_LE_UN:
404                         return MONO_LE_RELATION;
405                 case CMP_GE:
406                 case CMP_GE_UN:
407                         return MONO_GE_RELATION;
408                 case CMP_LT:
409                 case CMP_LT_UN:
410                         return MONO_LT_RELATION;
411                 case CMP_GT:
412                 case CMP_GT_UN:
413                         return MONO_GT_RELATION;
414                 default:
415                         g_assert_not_reached ();
416                         return MONO_ANY_RELATION;
417                 }
418         } else {
419                 return MONO_ANY_RELATION;
420         }
421 }
422
423 /*
424  * Given a BB, find its entry condition and put its relations in a
425  * "MonoAdditionalVariableRelationsForBB" structure.
426  * bb: the BB
427  * relations: the resulting relations (entry condition of the given BB)
428  */
429 static void
430 get_relations_from_previous_bb (MonoVariableRelationsEvaluationArea *area, MonoBasicBlock *bb, MonoAdditionalVariableRelationsForBB *relations)
431 {
432         MonoBasicBlock *in_bb;
433         MonoInst *ins, *compare, *branch;
434         MonoValueRelation branch_relation;
435         MonoValueRelation symmetric_relation;
436         gboolean code_path;
437         
438         INITIALIZE_VALUE_RELATION (&(relations->relation1.relation));
439         relations->relation1.relation.relation_is_static_definition = FALSE;
440         relations->relation1.relation.next = NULL;
441         relations->relation1.insertion_point = NULL;
442         relations->relation1.variable = -1;
443         INITIALIZE_VALUE_RELATION (&(relations->relation2.relation));
444         relations->relation2.relation.relation_is_static_definition = FALSE;
445         relations->relation2.relation.next = NULL;      
446         relations->relation2.insertion_point = NULL;
447         relations->relation2.variable = -1;
448         
449         if (bb->in_count == 1) { /* Should write the code to "sum" conditions... */
450                 in_bb = bb->in_bb [0];
451
452                 if ((in_bb->last_ins == NULL) || (in_bb->code == in_bb->last_ins))
453                         return;
454
455                 for (ins = in_bb->code; ins->next != in_bb->last_ins; ins = ins->next)
456                         ;
457
458                 compare = ins;
459                 branch = ins->next;
460                 branch_relation = get_relation_from_branch_instruction (branch);
461
462                 if (branch_relation != MONO_ANY_RELATION) {
463                         if (branch->inst_true_bb == bb) {
464                                 code_path = TRUE;
465                         } else if (branch->inst_false_bb == bb) {
466                                 code_path = FALSE;
467                         } else {
468                                 code_path = TRUE;
469                                 g_assert_not_reached ();
470                         }
471                         if (!code_path)
472                                 branch_relation = MONO_NEGATED_RELATION (branch_relation);
473                         symmetric_relation = MONO_SYMMETRIC_RELATION (branch_relation);
474
475                         /* FIXME: Other compare opcodes */
476                         if (compare->opcode == OP_ICOMPARE) {
477                                 relations->relation1.variable = compare->sreg1;
478                                 relations->relation1.relation.relation = branch_relation;
479                                 relations->relation1.relation.related_value.type = MONO_VARIABLE_SUMMARIZED_VALUE;
480                                 relations->relation1.relation.related_value.value.variable.variable = compare->sreg2;
481                                 relations->relation1.relation.related_value.value.variable.delta = 0;
482
483                                 relations->relation2.variable = compare->sreg2;
484                                 relations->relation2.relation.relation = symmetric_relation;
485                                 relations->relation2.relation.related_value.type = MONO_VARIABLE_SUMMARIZED_VALUE;
486                                 relations->relation2.relation.related_value.value.variable.variable = compare->sreg1;
487                                 relations->relation2.relation.related_value.value.variable.delta = 0;
488                         } else if (compare->opcode == OP_ICOMPARE_IMM) {
489                                 relations->relation1.variable = compare->sreg1;
490                                 relations->relation1.relation.relation = branch_relation;
491                                 relations->relation1.relation.related_value.type = MONO_CONSTANT_SUMMARIZED_VALUE;
492                                 relations->relation1.relation.related_value.value.constant.value = compare->inst_imm;
493                         }
494                 }
495         }
496 }
497
498 /*
499  * Add the given relations to the evaluation area.
500  * area: the evaluation area
501  * change: the relations that must be added
502  */
503 static void
504 apply_change_to_evaluation_area (MonoVariableRelationsEvaluationArea *area, MonoAdditionalVariableRelation *change)
505 {
506         MonoSummarizedValueRelation *base_relation;
507         
508         if (change->relation.relation != MONO_ANY_RELATION) {
509                 base_relation = &(area->relations [change->variable]);
510                 while ((base_relation->next != NULL) && (base_relation->next->relation_is_static_definition)) {
511                         base_relation = base_relation->next;
512                 }
513                 change->insertion_point = base_relation;
514                 change->relation.next = base_relation->next;
515                 base_relation->next = &(change->relation);
516         }
517 }
518
519 /*
520  * Remove the given relation from the evaluation area.
521  * change: the relation that must be removed
522  */
523 static void
524 remove_change_from_evaluation_area (MonoAdditionalVariableRelation *change)
525 {
526         if (change->insertion_point != NULL) {
527                 change->insertion_point->next = change->relation.next;
528                 change->relation.next = NULL;
529         }
530 }
531
532
533 static void
534 clean_contexts (MonoRelationsEvaluationContext *contexts, int number)
535 {
536         int i;
537         for (i = 0; i < number; i++) {
538                 contexts [i].status = MONO_RELATIONS_EVALUATION_NOT_STARTED;
539         }
540 }
541
542
543 /*
544  * Perform the intersection of a range and a constant value (taking into
545  * account the relation that the value has with the range).
546  * range: the range that will be intersected with the value
547  * value: the value that will be intersected with the range
548  * relation: the relation between the range and the value
549  */
550 static void
551 intersect_value( MonoRelationsEvaluationRange *range, int value, MonoValueRelation relation )
552 {
553         switch (relation) {
554         case MONO_NO_RELATION:
555                 MONO_MAKE_RELATIONS_EVALUATION_RANGE_IMPOSSIBLE (*range);
556                 break;
557         case MONO_ANY_RELATION:
558                 break;
559         case MONO_EQ_RELATION:
560                 MONO_UPPER_EVALUATION_RANGE_INTERSECTION (range->upper, value);
561                 MONO_LOWER_EVALUATION_RANGE_INTERSECTION (range->lower, value);
562                 break;
563         case MONO_NE_RELATION: {
564                 /* IMPROVEMENT Figure this out! (ignoring it is safe anyway) */
565                 break;
566         }
567         case MONO_LT_RELATION:
568                 MONO_UPPER_EVALUATION_RANGE_INTERSECTION (range->upper, MONO_UPPER_EVALUATION_RANGE_NOT_EQUAL (value));
569                 break;
570         case MONO_LE_RELATION:
571                 MONO_UPPER_EVALUATION_RANGE_INTERSECTION (range->upper, value);
572                 break;
573         case MONO_GT_RELATION:
574                 MONO_LOWER_EVALUATION_RANGE_INTERSECTION (range->lower, MONO_LOWER_EVALUATION_RANGE_NOT_EQUAL (value));
575                 break;
576         case MONO_GE_RELATION:
577                 MONO_LOWER_EVALUATION_RANGE_INTERSECTION (range->lower, value);
578                 break;
579         default:
580                 g_assert_not_reached();
581         }
582 }
583
584
585 /*
586  * Perform the intersection of two pairs of ranges (taking into account the
587  * relation between the ranges and a given delta).
588  * ranges: the ranges that will be intersected
589  * other_ranges the other ranges that will be intersected
590  * delta: the delta between the pairs of ranges
591  * relation: the relation between the pairs of ranges
592  */
593 static void
594 intersect_ranges( MonoRelationsEvaluationRanges *ranges, MonoRelationsEvaluationRanges *other_ranges, int delta, MonoValueRelation relation )
595 {
596         if (delta == 0) {
597                 switch (relation) {
598                 case MONO_NO_RELATION:
599                         MONO_MAKE_RELATIONS_EVALUATION_RANGES_IMPOSSIBLE (*ranges);
600                         break;
601                 case MONO_ANY_RELATION:
602                         break;
603                 case MONO_EQ_RELATION:
604                         MONO_RELATIONS_EVALUATION_RANGES_INTERSECTION (*ranges, *other_ranges);
605                         break;
606                 case MONO_NE_RELATION: {
607                         /* FIXME Figure this out! (ignoring it is safe anyway) */
608                         break;
609                 }
610                 case MONO_LT_RELATION:
611                         MONO_UPPER_EVALUATION_RANGE_INTERSECTION (ranges->zero.upper, MONO_UPPER_EVALUATION_RANGE_NOT_EQUAL (other_ranges->zero.upper));
612                         MONO_UPPER_EVALUATION_RANGE_INTERSECTION (ranges->variable.upper, MONO_UPPER_EVALUATION_RANGE_NOT_EQUAL (other_ranges->variable.upper));
613                         break;
614                 case MONO_LE_RELATION:
615                         MONO_UPPER_EVALUATION_RANGE_INTERSECTION (ranges->zero.upper, other_ranges->zero.upper);
616                         MONO_UPPER_EVALUATION_RANGE_INTERSECTION (ranges->variable.upper, other_ranges->variable.upper);
617                         break;
618                 case MONO_GT_RELATION:
619                         MONO_LOWER_EVALUATION_RANGE_INTERSECTION (ranges->zero.lower, MONO_LOWER_EVALUATION_RANGE_NOT_EQUAL (other_ranges->zero.lower));
620                         MONO_LOWER_EVALUATION_RANGE_INTERSECTION (ranges->variable.lower, MONO_LOWER_EVALUATION_RANGE_NOT_EQUAL (other_ranges->variable.lower));
621                         break;
622                 case MONO_GE_RELATION:
623                         MONO_LOWER_EVALUATION_RANGE_INTERSECTION (ranges->zero.lower, other_ranges->zero.lower);
624                         MONO_LOWER_EVALUATION_RANGE_INTERSECTION (ranges->variable.lower, other_ranges->variable.lower);
625                         break;
626                 default:
627                         g_assert_not_reached();
628                 }
629         } else {
630                 MonoRelationsEvaluationRanges translated_ranges = *other_ranges;
631                 MONO_ADD_DELTA_SAFELY_TO_RANGES (translated_ranges, delta);
632                 intersect_ranges( ranges, &translated_ranges, FALSE, relation );
633         }
634 }
635
636 /*
637  * Recursive method that traverses the relation graph to evaluate the
638  * relation between two variables.
639  * At the end of the execution, the resulting ranges are in the context of
640  * the "starting" variable.
641  * area: the current evaluation area (it contains the relation graph and
642  *       memory for all the evaluation contexts is already allocated)
643  * variable: starting variable (the value ranges in its context are the result
644  *           of the execution of this procedure)
645  * target_variable: the variable with respect to which the starting variable
646  *                  is evaluated (tipically the starting variable is the index
647  *                  and the target one is the array (which means its length))
648  * father_context: the previous evaluation context in recursive invocations
649  *                 (or NULL for the first invocation)
650  */
651 static void
652 evaluate_relation_with_target_variable (MonoVariableRelationsEvaluationArea *area, int variable, int target_variable, MonoRelationsEvaluationContext *father_context)
653 {
654         MonoRelationsEvaluationContext *context = &(area->contexts [variable]);
655         
656         // First of all, we check the evaluation status
657         // (what must be done is *very* different in each case)
658         switch (context->status) {
659         case MONO_RELATIONS_EVALUATION_NOT_STARTED: {
660                 MonoSummarizedValueRelation *relation = &(area->relations [variable]);
661                 
662                 if (TRACE_ABC_REMOVAL) {
663                         printf ("Evaluating variable %d (target variable %d)\n", variable, target_variable);
664                         print_summarized_value_relation (relation);
665                         printf ("\n");
666                 }
667                 
668                 // We properly inizialize the context
669                 context->status = MONO_RELATIONS_EVALUATION_IN_PROGRESS;
670                 context->father = father_context;
671                 MONO_MAKE_RELATIONS_EVALUATION_RANGES_WEAK (context->ranges);
672                 
673                 // If we have found the target variable, we can set the range
674                 // related to it in the context to "equal" (which is [0,0])
675                 if (variable == target_variable) {
676                         if (TRACE_ABC_REMOVAL) {
677                                 printf ("Target variable reached (%d), continuing to evaluate relations with constants\n", variable);
678                         }
679                         context->ranges.variable.lower = 0;
680                         context->ranges.variable.upper = 0;
681                 }
682                 
683                 // Examine all relations for this variable (scan the list)
684                 // The contribute of each relation will be intersected (logical and)
685                 while (relation != NULL) {
686                         context->current_relation = relation;
687                         
688                         if (TRACE_ABC_REMOVAL) {
689                                 printf ("Processing (%d): ", variable);
690                                 print_summarized_value_relation (relation);
691                                 printf ("\n");
692                         }
693                         
694                         // We decie what to do according the the type of the related value
695                         switch (relation->related_value.type) {
696                         case MONO_ANY_SUMMARIZED_VALUE:
697                                 // No added information, skip it
698                                 break;
699                         case MONO_CONSTANT_SUMMARIZED_VALUE:
700                                 // Intersect range with constant (taking into account the relation)
701                                 intersect_value (&(context->ranges.zero), relation->related_value.value.constant.value, relation->relation);
702                                 break;
703                         case MONO_VARIABLE_SUMMARIZED_VALUE:
704                                 // Generally, evaluate related variable and intersect ranges.
705                                 // However, some check is necessary...
706                                 
707                                 // If the relation is "ANY", nothing to do (no added information)
708                                 if (relation->relation != MONO_ANY_RELATION) {
709                                         int related_variable = relation->related_value.value.variable.variable;
710                                         MonoRelationsEvaluationContext *related_context = &(area->contexts [related_variable]);
711                                         
712                                         // The second condition in the "or" avoids messing with "back edges" in the graph traversal
713                                         // (they are simply ignored instead of triggering the handling of recursion)
714                                         if ( (related_context->status == MONO_RELATIONS_EVALUATION_NOT_STARTED) || !
715                                                         ((related_context->current_relation->related_value.type == MONO_VARIABLE_SUMMARIZED_VALUE) &&
716                                                         (related_context->current_relation->related_value.value.variable.variable == variable))) {
717                                                 // Evaluate the related variable
718                                                 evaluate_relation_with_target_variable (area, related_variable, target_variable, context);
719                                                 
720                                                 // Check if we are part of a recursive loop
721                                                 if (context->status & MONO_RELATIONS_EVALUATION_IS_RECURSIVE) {
722                                                         if (TRACE_ABC_REMOVAL) {
723                                                                 printf ("Recursivity detected for variable %d (target variable %d), status ", variable, target_variable);
724                                                                 print_evaluation_context_status (context->status);
725                                                         }
726                                                         
727                                                         // If we are, check if the evaluation of the related variable is complete
728                                                         if (related_context->status == MONO_RELATIONS_EVALUATION_COMPLETED) {
729                                                                 // If it is complete, we are part of a recursive definition.
730                                                                 // Since it is a *definition* (and definitions are evaluated *before*
731                                                                 // conditions because they are first in the list), intersection is not
732                                                                 // strictly necessary, we simply copy the ranges and apply the delta
733                                                                 context->ranges = related_context->ranges;
734                                                                 /* Delta has already been checked for over/under-flow when evaluating values */
735                                                                 MONO_ADD_DELTA_SAFELY_TO_RANGES (context->ranges, relation->related_value.value.variable.delta);
736                                                                 context->status = MONO_RELATIONS_EVALUATION_COMPLETED;
737                                                                 if (TRACE_ABC_REMOVAL) {
738                                                                         printf (", ranges already computed, result: \n");
739                                                                         print_evaluation_context_ranges (&(context->ranges));
740                                                                         printf (" (delta is %d)\n", relation->related_value.value.variable.delta);
741                                                                 }
742                                                         } else {
743                                                                 // If it is not complete, do nothing (we do not have enough information)
744                                                                 if (TRACE_ABC_REMOVAL) {
745                                                                         printf (", ranges not computed\n");
746                                                                 }
747                                                         }
748                                                 } else {
749                                                         // If we are not (the common case) intersect the result
750                                                         intersect_ranges( &(context->ranges), &(related_context->ranges), relation->related_value.value.variable.delta, relation->relation );
751                                                 }
752                                         } else {
753                                                 if (TRACE_ABC_REMOVAL) {
754                                                         printf ("Relation is a back-edge in this traversal, skipping\n");
755                                                 }
756                                         }
757                                 }
758                                 break;
759                         case MONO_PHI_SUMMARIZED_VALUE: {
760                                 // We must compute all PHI alternatives, combining the results (with a union, which is a logical "or"),
761                                 // and intersect this result with the ranges in the context; we must also take into account recursions
762                                 // (with loops that can be ascending, descending, or indefinite)
763                                 MonoRelationsEvaluationRanges phi_ranges;
764                                 int phi;
765                                 gboolean is_ascending = FALSE;
766                                 gboolean is_descending = FALSE;
767                                 
768                                 MONO_MAKE_RELATIONS_EVALUATION_RANGES_IMPOSSIBLE (phi_ranges);
769                                 for (phi = 0; phi < relation->related_value.value.phi.number_of_alternatives; phi++) {
770                                         int phi_alternative = relation->related_value.value.phi.phi_alternatives [phi];
771                                         evaluate_relation_with_target_variable (area, phi_alternative, target_variable, context);
772                                         
773                                         // This means we are part of a recursive loop
774                                         if (context->status & MONO_RELATIONS_EVALUATION_IS_RECURSIVE) {
775                                                 if (TRACE_ABC_REMOVAL) {
776                                                         printf ("Recursivity detected for variable %d (target variable %d), status ", variable, target_variable);
777                                                         print_evaluation_context_status (context->status);
778                                                         printf ("\n");
779                                                 }
780                                                 if (context->status & MONO_RELATIONS_EVALUATION_IS_RECURSIVELY_ASCENDING) {
781                                                         is_ascending = TRUE;
782                                                 }
783                                                 if (context->status & MONO_RELATIONS_EVALUATION_IS_RECURSIVELY_DESCENDING) {
784                                                         is_descending = TRUE;
785                                                 }
786                                                 if (context->status & MONO_RELATIONS_EVALUATION_IS_RECURSIVELY_INDEFINITE) {
787                                                         is_ascending = TRUE;
788                                                         is_descending = TRUE;
789                                                 }
790                                                 
791                                                 // Clear "recursivity" bits in the status (recursion has been handled)
792                                                 context->status = MONO_RELATIONS_EVALUATION_IN_PROGRESS;
793                                         } else {
794                                                 MONO_RELATIONS_EVALUATION_RANGES_UNION (phi_ranges, area->contexts [phi_alternative].ranges);
795                                         }
796                                 }
797                                 
798                                 // Apply the effects of all recursive loops
799                                 if (is_ascending) {
800                                         phi_ranges.zero.upper = INT_MAX;
801                                         phi_ranges.variable.upper = INT_MAX;
802                                 }
803                                 if (is_descending) {
804                                         phi_ranges.zero.lower = INT_MIN;
805                                         phi_ranges.variable.lower = INT_MIN;
806                                 }
807                                 
808                                 // Intersect final result
809                                 MONO_RELATIONS_EVALUATION_RANGES_INTERSECTION (context->ranges, phi_ranges);
810                                 break;
811                         }
812                         default:
813                                 g_assert_not_reached();
814                         }
815                         
816                         // Pass to next relation
817                         relation = relation->next;
818                 }
819                 
820                 // Check if any recursivity bits are still in the status, and in any case clear them
821                 if (context->status & MONO_RELATIONS_EVALUATION_IS_RECURSIVE) {
822                         if (TRACE_ABC_REMOVAL) {
823                                 printf ("Recursivity for variable %d (target variable %d) discards computation, status ", variable, target_variable);
824                                 print_evaluation_context_status (context->status);
825                                 printf ("\n");
826                         }
827                         // If yes, we did not have enough information (most likely we were evaluated inside a PHI, but we also
828                         // depended on the same PHI, which was still in evaluation...), so clear the status to "NOT_STARTED"
829                         // (if we will be evaluated again, the PHI will be already done, so our evaluation will succeed)
830                         context->status = MONO_RELATIONS_EVALUATION_NOT_STARTED;
831                 } else {
832                         if (TRACE_ABC_REMOVAL) {
833                                 printf ("Ranges for variable %d (target variable %d) computed: ", variable, target_variable);
834                                 print_evaluation_context_ranges (&(context->ranges));
835                                 printf ("\n");
836                         }
837                         // If not (the common case) the evaluation is complete, and the result is in the context
838                         context->status = MONO_RELATIONS_EVALUATION_COMPLETED;
839                 }
840                 break;
841         }
842         case MONO_RELATIONS_EVALUATION_IN_PROGRESS: {
843                 // This means we are in a recursive loop
844                 MonoRelationsEvaluationContext *current_context = father_context;
845                 MonoRelationsEvaluationContext *last_context = context->father;
846                 gboolean evaluation_can_be_recursive = TRUE;
847                 gboolean evaluation_is_definition = TRUE;
848                 int path_value = 0;
849                 
850                 if (TRACE_ABC_REMOVAL) {
851                         printf ("Evaluation of variable %d (target variable %d) already in progress\n", variable, target_variable);
852                         print_evaluation_context (context);
853                         print_summarized_value_relation (context->current_relation);
854                         printf ("\n");
855                 }
856                 
857                 // We must check if the loop can be a recursive definition (we scan the whole loop)
858                 while (current_context != last_context) {
859                         if (current_context == NULL) {
860                                 printf ("Broken recursive ring in ABC removal\n");
861                                 g_assert_not_reached ();
862                         }
863                         
864                         if (current_context->current_relation->relation_is_static_definition) {
865                                 if (current_context->current_relation->related_value.type == MONO_VARIABLE_SUMMARIZED_VALUE) {
866                                         /* No need to check path_value for over/under-flow, since delta should be safe */
867                                         path_value += current_context->current_relation->related_value.value.variable.delta;
868                                 } else if (current_context->current_relation->related_value.type != MONO_PHI_SUMMARIZED_VALUE) {
869                                         evaluation_can_be_recursive = FALSE;
870                                 }
871                         } else {
872                                 evaluation_is_definition = FALSE;
873                                 evaluation_can_be_recursive = FALSE;
874                         }
875                         
876                         current_context = current_context->father;
877                 }
878                 
879                 // If this is a recursive definition, we properly flag the status in all the involved contexts
880                 if (evaluation_is_definition) {
881                         MonoRelationsEvaluationStatus recursive_status;
882                         if (evaluation_can_be_recursive) {
883                                 if (path_value > 0) {
884                                         recursive_status = MONO_RELATIONS_EVALUATION_IS_RECURSIVELY_ASCENDING;
885                                 } else if (path_value < 0) {
886                                         recursive_status = MONO_RELATIONS_EVALUATION_IS_RECURSIVELY_DESCENDING;
887                                 } else {
888                                         recursive_status = MONO_RELATIONS_EVALUATION_IS_RECURSIVELY_INDEFINITE;
889                                 }
890                         } else {
891                                 recursive_status = MONO_RELATIONS_EVALUATION_IS_RECURSIVELY_INDEFINITE;
892                         }
893                         
894                         if (TRACE_ABC_REMOVAL) {
895                                 printf ("Recursivity accepted (");
896                                 print_evaluation_context_status (recursive_status);
897                                 printf (")\n");
898                         }
899                         
900                         current_context = father_context;
901                         while (current_context != last_context) {
902                                 current_context->status |= recursive_status;
903                                 current_context = current_context->father;
904                         }
905                 } else {
906                         if (TRACE_ABC_REMOVAL) {
907                                 printf ("Recursivity rejected (some relation in the cycle is not a defintion)\n");
908                         }
909                 }
910                 break;
911         }
912         case MONO_RELATIONS_EVALUATION_COMPLETED: {
913                 return;
914         }
915         default:
916                 if (TRACE_ABC_REMOVAL) {
917                         printf ("Variable %d (target variable %d) already in a recursive ring, skipping\n", variable, target_variable);
918                         print_evaluation_context (context);
919                         print_summarized_value_relation (context->current_relation);
920                         printf ("\n");
921                 }
922                 break;
923         }
924         
925 }
926
927 /*
928  * Apply the given value kind to the given range
929  */
930 static void
931 apply_value_kind_to_range (MonoRelationsEvaluationRange *range, MonoIntegerValueKind value_kind)
932 {
933         if (value_kind != MONO_UNKNOWN_INTEGER_VALUE) {
934                 if (value_kind & MONO_UNSIGNED_VALUE_FLAG) {
935                         if (range->lower < 0) {
936                                 range->lower = 0;
937                         }
938                         if ((value_kind & MONO_INTEGER_VALUE_SIZE_BITMASK) == 1) {
939                                 if (range->upper > 0xff) {
940                                         range->upper = 0xff;
941                                 }
942                         } else if ((value_kind & MONO_INTEGER_VALUE_SIZE_BITMASK) == 2) {
943                                 if (range->upper > 0xffff) {
944                                         range->upper = 0xffff;
945                                 }
946                         }
947                 } else {
948                         if ((value_kind & MONO_INTEGER_VALUE_SIZE_BITMASK) == 1) {
949                                 if (range->lower < -0x80) {
950                                         range->lower = -0x80;
951                                 }
952                                 if (range->upper > 0x7f) {
953                                         range->upper = 0x7f;
954                                 }
955                         } else if ((value_kind & MONO_INTEGER_VALUE_SIZE_BITMASK) == 2) {
956                                 if (range->lower < -0x8000) {
957                                         range->lower = -0x8000;
958                                 }
959                                 if (range->upper > 0x7fff) {
960                                         range->upper = 0x7fff;
961                                 }
962                         }
963                 }
964         }
965 }
966
967 /*
968  * Attempt the removal of bounds checks from a MonoInst.
969  * inst: the MonoInst
970  * area: the current evaluation area (it contains the relation graph and
971  *       memory for all the evaluation contexts is already allocated)
972  */
973 static void
974 remove_abc_from_inst (MonoInst *ins, MonoVariableRelationsEvaluationArea *area)
975 {
976         /* FIXME: Add support for 'constant' arrays and constant indexes */
977
978         int array_variable = ins->sreg1;
979         int index_variable = ins->sreg2;
980         MonoRelationsEvaluationContext *array_context = &(area->contexts [array_variable]);
981         MonoRelationsEvaluationContext *index_context = &(area->contexts [index_variable]);
982                                 
983         clean_contexts (area->contexts, area->cfg->next_vreg);
984                                 
985         evaluate_relation_with_target_variable (area, index_variable, array_variable, NULL);
986         evaluate_relation_with_target_variable (area, array_variable, array_variable, NULL);
987
988         if ((index_context->ranges.zero.lower >=0) && ((index_context->ranges.variable.upper < 0)||(index_context->ranges.zero.upper < array_context->ranges.zero.lower))) {
989                 if (REPORT_ABC_REMOVAL) {
990                         printf ("ARRAY-ACCESS: removed bounds check on array %d with index %d\n",
991                                         array_variable, index_variable);
992                         NULLIFY_INS (ins);
993                 }
994         } else {
995                 if (TRACE_ABC_REMOVAL) {
996                         if (index_context->ranges.zero.lower >= 0) {
997                                 printf ("ARRAY-ACCESS: Removed lower bound check on array %d with index %d\n", array_variable, index_variable);
998                         }
999                         if (index_context->ranges.variable.upper < 0) {
1000                                 printf ("ARRAY-ACCESS: Removed upper bound check (through variable) on array %d with index %d\n", array_variable, index_variable);
1001                         }
1002                         if (index_context->ranges.zero.upper < array_context->ranges.zero.lower) {
1003                                 printf ("ARRAY-ACCESS: Removed upper bound check (through constant) on array %d with index %d\n", array_variable, index_variable);
1004                         }
1005                 }
1006         }
1007 }
1008
1009 static gboolean
1010 eval_non_null (MonoVariableRelationsEvaluationArea *area, int reg)
1011 {
1012         MonoRelationsEvaluationContext *context = &(area->contexts [reg]);
1013
1014         clean_contexts (area->contexts, area->cfg->next_vreg);
1015         evaluate_relation_with_target_variable (area, reg, reg, NULL);
1016                                 
1017         return context->ranges.zero.lower > 0;
1018 }
1019
1020 static void
1021 add_non_null (MonoVariableRelationsEvaluationArea *area, MonoCompile *cfg, int reg,
1022                           GSList **check_relations)
1023 {
1024         MonoAdditionalVariableRelation *rel;
1025
1026         rel = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoAdditionalVariableRelation));
1027         rel->variable = reg;
1028         rel->relation.relation = MONO_GT_RELATION;
1029         rel->relation.related_value.type = MONO_CONSTANT_SUMMARIZED_VALUE;
1030         rel->relation.related_value.value.constant.value = 0;
1031
1032         apply_change_to_evaluation_area (area, rel);
1033
1034         *check_relations = g_slist_append_mempool (cfg->mempool, *check_relations, rel);
1035 }
1036
1037 /*
1038  * Process a BB removing bounds checks from array accesses.
1039  * It does the following (in sequence):
1040  * - Get the BB entry condition
1041  * - Add its relations to the relation graph in the evaluation area
1042  * - Process all the MonoInst trees in the BB
1043  * - Recursively process all the children BBs in the dominator tree
1044  * - Remove the relations previously added to the relation graph
1045  *
1046  * bb: the BB that must be processed
1047  * area: the current evaluation area (it contains the relation graph and
1048  *       memory for all the evaluation contexts is already allocated)
1049  */
1050 static void
1051 process_block (MonoCompile *cfg, MonoBasicBlock *bb, MonoVariableRelationsEvaluationArea *area) {
1052         int inst_index;
1053         MonoInst *ins;
1054         MonoAdditionalVariableRelationsForBB additional_relations;
1055         GSList *dominated_bb, *l;
1056         GSList *check_relations = NULL;
1057         
1058         if (TRACE_ABC_REMOVAL) {
1059                 printf ("\nProcessing block %d [dfn %d]...\n", bb->block_num, bb->dfn);
1060         }
1061
1062         if (bb->region != -1)
1063                 return;
1064
1065         get_relations_from_previous_bb (area, bb, &additional_relations);
1066         if (TRACE_ABC_REMOVAL) {
1067                 if (additional_relations.relation1.relation.relation != MONO_ANY_RELATION) {
1068                         printf ("Adding relation 1 on variable %d: ", additional_relations.relation1.variable);
1069                         print_summarized_value_relation (&(additional_relations.relation1.relation));
1070                         printf ("\n");
1071                 }
1072                 if (additional_relations.relation2.relation.relation != MONO_ANY_RELATION) {
1073                         printf ("Adding relation 2 on variable %d: ", additional_relations.relation2.variable);
1074                         print_summarized_value_relation (&(additional_relations.relation2.relation));
1075                         printf ("\n");
1076                 }
1077         }
1078         apply_change_to_evaluation_area (area, &(additional_relations.relation1));
1079         apply_change_to_evaluation_area (area, &(additional_relations.relation2));
1080
1081         inst_index = 0;
1082         for (ins = bb->code; ins; ins = ins->next) {
1083                 MonoAdditionalVariableRelation *rel;
1084                 int array_var, index_var;
1085
1086                 if (TRACE_ABC_REMOVAL) {
1087                         printf ("Processing instruction %d\n", inst_index);
1088                         inst_index++;
1089                 }
1090
1091                 if (ins->opcode == OP_BOUNDS_CHECK) { /* Handle OP_LDELEMA2D, too */
1092                         if (TRACE_ABC_REMOVAL) {
1093                                 printf ("Attempting check removal...\n");
1094                         }
1095
1096                         array_var = ins->sreg1;
1097                         index_var = ins->sreg2;
1098                 
1099                         remove_abc_from_inst (ins, area);
1100
1101                         /* We can derive additional relations from the bounds check */
1102                         if (ins->opcode != OP_NOP) {
1103                                 rel = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoAdditionalVariableRelation));
1104                                 rel->variable = index_var;
1105                                 rel->relation.relation = MONO_LT_RELATION;
1106                                 rel->relation.related_value.type = MONO_VARIABLE_SUMMARIZED_VALUE;
1107                                 rel->relation.related_value.value.variable.variable = array_var;
1108                                 rel->relation.related_value.value.variable.delta = 0;
1109
1110                                 apply_change_to_evaluation_area (area, rel);
1111
1112                                 check_relations = g_slist_append_mempool (cfg->mempool, check_relations, rel);
1113
1114                                 rel = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoAdditionalVariableRelation));
1115                                 rel->variable = index_var;
1116                                 rel->relation.relation = MONO_GE_RELATION;
1117                                 rel->relation.related_value.type = MONO_CONSTANT_SUMMARIZED_VALUE;
1118                                 rel->relation.related_value.value.constant.value = 0;
1119
1120                                 apply_change_to_evaluation_area (area, rel);
1121
1122                                 check_relations = g_slist_append_mempool (cfg->mempool, check_relations, rel);
1123                         }
1124                 }
1125
1126                 if (ins->opcode == OP_CHECK_THIS) {
1127                         if (eval_non_null (area, ins->sreg1)) {
1128                                 if (REPORT_ABC_REMOVAL)
1129                                         printf ("ARRAY-ACCESS: removed check_this instruction.\n");
1130                                 NULLIFY_INS (ins);
1131                         }
1132                 }
1133
1134                 if (ins->opcode == OP_NOT_NULL)
1135                         add_non_null (area, cfg, ins->sreg1, &check_relations);
1136
1137                 /* 
1138                  * FIXME: abcrem equates an array with its length,
1139                  * so a = new int [100] implies a != null, but a = new int [0] doesn't.
1140                  */
1141                 /*
1142                  * Eliminate MONO_INST_FAULT flags if possible.
1143                  */
1144                 if (COMPILE_LLVM (cfg) && (ins->opcode == OP_LDLEN ||
1145                                                                    ins->opcode == OP_BOUNDS_CHECK ||
1146                                                                    ins->opcode == OP_STRLEN ||
1147                                                                    (MONO_IS_LOAD_MEMBASE (ins) && (ins->flags & MONO_INST_FAULT)) ||
1148                                                                    (MONO_IS_STORE_MEMBASE (ins) && (ins->flags & MONO_INST_FAULT)))) {
1149                         int reg;
1150
1151                         if (MONO_IS_STORE_MEMBASE (ins))
1152                                 reg = ins->inst_destbasereg;
1153                         else if (MONO_IS_LOAD_MEMBASE (ins))
1154                                 reg = ins->inst_basereg;
1155                         else
1156                                 reg = ins->sreg1;
1157
1158                         /*
1159                          * This doesn't work because LLVM can move the non-faulting loads before the faulting
1160                          * ones (test_0_llvm_moving_faulting_loads ()).
1161                          * So only do it if we know the load cannot be moved before the instruction which ensures it is not
1162                          * null (i.e. the def of its sreg).
1163                          */
1164                         if (area->defs [reg] && area->defs [reg]->opcode == OP_NEWARR) {
1165                                 if (REPORT_ABC_REMOVAL)
1166                                         printf ("ARRAY-ACCESS: removed MONO_INST_FAULT flag.\n");
1167                                 ins->flags &= ~MONO_INST_FAULT;
1168                         }
1169                         /*
1170                         if (eval_non_null (area, reg)) {
1171                                 if (REPORT_ABC_REMOVAL)
1172                                         printf ("ARRAY-ACCESS: removed MONO_INST_FAULT flag.\n");
1173                                 ins->flags &= ~MONO_INST_FAULT;
1174                         } else {
1175                                 add_non_null (area, cfg, reg, &check_relations);
1176                         }
1177                         */
1178                 }
1179         }       
1180         
1181         if (TRACE_ABC_REMOVAL) {
1182                 printf ("Processing block %d [dfn %d] done.\n", bb->block_num, bb->dfn);
1183         }
1184         
1185         for (dominated_bb = bb->dominated; dominated_bb != NULL; dominated_bb = dominated_bb->next) {
1186                 process_block (cfg, (MonoBasicBlock*) (dominated_bb->data), area);
1187         }
1188
1189         for (l = check_relations; l; l = l->next)
1190                 remove_change_from_evaluation_area (l->data);
1191         
1192         remove_change_from_evaluation_area (&(additional_relations.relation1));
1193         remove_change_from_evaluation_area (&(additional_relations.relation2));
1194 }
1195
1196 static MonoIntegerValueKind
1197 type_to_value_kind (MonoType *type)
1198 {
1199         if (type->byref)
1200                 return MONO_UNKNOWN_INTEGER_VALUE;
1201         switch (type->type) {
1202         case MONO_TYPE_I1:
1203                 return MONO_INTEGER_VALUE_SIZE_1;
1204                 break;
1205         case MONO_TYPE_U1:
1206                 return MONO_UNSIGNED_INTEGER_VALUE_SIZE_1;
1207                 break;
1208         case MONO_TYPE_I2:
1209                 return MONO_INTEGER_VALUE_SIZE_2;
1210                 break;
1211         case MONO_TYPE_U2:
1212                 return MONO_UNSIGNED_INTEGER_VALUE_SIZE_2;
1213                 break;
1214         case MONO_TYPE_I4:
1215                 return MONO_INTEGER_VALUE_SIZE_4;
1216                 break;
1217         case MONO_TYPE_U4:
1218                 return MONO_UNSIGNED_INTEGER_VALUE_SIZE_4;
1219                 break;
1220         case MONO_TYPE_I:
1221                 return SIZEOF_VOID_P;
1222                 break;
1223         case MONO_TYPE_U:
1224                 return (MONO_UNSIGNED_VALUE_FLAG|SIZEOF_VOID_P);
1225                 break;
1226         case MONO_TYPE_I8:
1227                 return MONO_INTEGER_VALUE_SIZE_8;
1228                 break;
1229         case MONO_TYPE_U8:
1230                 return MONO_UNSIGNED_INTEGER_VALUE_SIZE_8;
1231         default:
1232                 return MONO_UNKNOWN_INTEGER_VALUE;
1233         }
1234 }
1235
1236 /**
1237  * mono_perform_abc_removal:
1238  * @cfg: Control Flow Graph
1239  *
1240  * Performs the ABC removal from a cfg in SSA form.
1241  * It does the following:
1242  * - Prepare the evaluation area
1243  * - Allocate memory for the relation graph in the evaluation area
1244  *   (of course, only for variable definitions) and summarize there all
1245  *   variable definitions
1246  * - Allocate memory for the evaluation contexts in the evaluation area
1247  * - Recursively process all the BBs in the dominator tree (it is enough
1248  *   to invoke the processing on the entry BB)
1249  * 
1250  * cfg: the method code
1251  */
1252 void
1253 mono_perform_abc_removal (MonoCompile *cfg)
1254 {
1255         MonoVariableRelationsEvaluationArea area;
1256         MonoBasicBlock *bb;
1257         int i;
1258         
1259         verbose_level = cfg->verbose_level;
1260         
1261         if (TRACE_ABC_REMOVAL) {
1262                 printf ("\nRemoving array bound checks in %s\n", mono_method_full_name (cfg->method, TRUE));
1263         }
1264
1265         area.cfg = cfg;
1266         area.relations = (MonoSummarizedValueRelation *)
1267                 mono_mempool_alloc (cfg->mempool, sizeof (MonoSummarizedValueRelation) * (cfg->next_vreg) * 2);
1268         area.contexts = (MonoRelationsEvaluationContext *)
1269                 mono_mempool_alloc (cfg->mempool, sizeof (MonoRelationsEvaluationContext) * (cfg->next_vreg));
1270         area.variable_value_kind = (MonoIntegerValueKind *)
1271                 mono_mempool_alloc (cfg->mempool, sizeof (MonoIntegerValueKind) * (cfg->next_vreg));
1272         area.defs = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * cfg->next_vreg);
1273         for (i = 0; i < cfg->next_vreg; i++) {
1274                 area.variable_value_kind [i] = MONO_UNKNOWN_INTEGER_VALUE;
1275                 area.relations [i].relation = MONO_EQ_RELATION;
1276                 area.relations [i].relation_is_static_definition = TRUE;
1277                 MAKE_VALUE_ANY (area.relations [i].related_value);
1278                 area.relations [i].next = NULL;
1279                 area.defs [i] = NULL;
1280         }
1281
1282         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1283                 MonoInst *ins;
1284
1285                 if (TRACE_ABC_REMOVAL)
1286                         printf ("\nABCREM BLOCK %d:\n", bb->block_num);
1287
1288                 for (ins = bb->code; ins; ins = ins->next) {
1289                         const char *spec = INS_INFO (ins->opcode);
1290                         
1291                         if (spec [MONO_INST_DEST] == ' ' || MONO_IS_STORE_MEMBASE (ins))
1292                                 continue;
1293
1294                         if (spec [MONO_INST_DEST] == 'i') {
1295                                 MonoIntegerValueKind effective_value_kind;
1296                                 MonoRelationsEvaluationRange range;
1297                                 MonoSummarizedValueRelation *type_relation;
1298                                 MonoInst *var;
1299
1300                                 if (TRACE_ABC_REMOVAL)
1301                                         mono_print_ins (ins);
1302
1303                                 var = get_vreg_to_inst (cfg, ins->dreg);
1304                                 if (var)
1305                                         area.variable_value_kind [ins->dreg] = type_to_value_kind (var->inst_vtype);
1306
1307                                 effective_value_kind = get_relation_from_ins (&area, ins, &area.relations [ins->dreg], area.variable_value_kind [ins->dreg]);
1308
1309                                 MONO_MAKE_RELATIONS_EVALUATION_RANGE_WEAK (range);
1310                                 apply_value_kind_to_range (&range, area.variable_value_kind [ins->dreg]);
1311                                 apply_value_kind_to_range (&range, effective_value_kind);
1312                                         
1313                                 if (range.upper < INT_MAX) {
1314                                         type_relation = (MonoSummarizedValueRelation *) mono_mempool_alloc (cfg->mempool, sizeof (MonoSummarizedValueRelation));
1315                                         type_relation->relation = MONO_LE_RELATION;
1316                                         type_relation->related_value.type = MONO_CONSTANT_SUMMARIZED_VALUE;
1317                                         type_relation->related_value.value.constant.value = range.upper;
1318                                         type_relation->relation_is_static_definition = TRUE;
1319                                         type_relation->next = area.relations [ins->dreg].next;
1320                                         area.relations [ins->dreg].next = type_relation;
1321                                         if (TRACE_ABC_REMOVAL) {
1322                                                 printf ("[var%d <= %d]", ins->dreg, range.upper);
1323                                         }
1324                                 }
1325                                 if (range.lower > INT_MIN) {
1326                                         type_relation = (MonoSummarizedValueRelation *) mono_mempool_alloc (cfg->mempool, sizeof (MonoSummarizedValueRelation));
1327                                         type_relation->relation = MONO_GE_RELATION;
1328                                         type_relation->related_value.type = MONO_CONSTANT_SUMMARIZED_VALUE;
1329                                         type_relation->related_value.value.constant.value = range.lower;
1330                                         type_relation->relation_is_static_definition = TRUE;
1331                                         type_relation->next = area.relations [ins->dreg].next;
1332                                         area.relations [ins->dreg].next = type_relation;
1333                                         if (TRACE_ABC_REMOVAL) {
1334                                                 printf ("[var%d >= %d]", ins->dreg, range.lower);
1335                                         }
1336                                 }
1337                                 if (TRACE_ABC_REMOVAL) {
1338                                         printf ("Summarized variable %d: ", ins->dreg);
1339                                         print_summarized_value (&(area.relations [ins->dreg].related_value));
1340                                         printf ("\n");
1341                                 }
1342                         }
1343                 }
1344         }
1345
1346         /* Add symmetric relations */
1347         for (i = 0; i < cfg->next_vreg; i++) {
1348                 if (area.relations [i].related_value.type == MONO_VARIABLE_SUMMARIZED_VALUE) {
1349                         int related_index = cfg->next_vreg + i;
1350                         int related_variable = area.relations [i].related_value.value.variable.variable;
1351                         
1352                         area.relations [related_index].relation = MONO_EQ_RELATION;
1353                         area.relations [related_index].relation_is_static_definition = TRUE;
1354                         area.relations [related_index].related_value.type = MONO_VARIABLE_SUMMARIZED_VALUE;
1355                         area.relations [related_index].related_value.value.variable.variable = i;
1356                         area.relations [related_index].related_value.value.variable.delta = - area.relations [i].related_value.value.variable.delta;
1357                         
1358                         area.relations [related_index].next = area.relations [related_variable].next;
1359                         area.relations [related_variable].next = &(area.relations [related_index]);
1360                         
1361                         if (TRACE_ABC_REMOVAL) {
1362                                 printf ("Added symmetric summarized value for variable variable %d (to %d): ", i, related_variable);
1363                                 print_summarized_value (&(area.relations [related_index].related_value));
1364                                 printf ("\n");
1365                         }
1366                 }
1367         }
1368
1369         process_block (cfg, cfg->bblocks [0], &area);
1370 }
1371
1372 #endif /* DISABLE_JIT */