extra, math: rework how fuzzy max is handled
[smatch.git] / smatch_math.c
blob4f2d2454dfa5459ae662324a2989cca1c9cd97b7
1 /*
2 * smatch/smatch_math.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include "symbol.h"
11 #include "smatch.h"
12 #include "smatch_slist.h"
13 #include "smatch_extra.h"
15 static struct range_list *_get_rl(struct expression *expr, int implied);
16 static struct range_list *handle_variable(struct expression *expr, int implied);
18 static sval_t zero = {.type = &int_ctype, {.value = 0} };
19 static sval_t one = {.type = &int_ctype, {.value = 1} };
21 static struct range_list *rl_zero(void)
23 return alloc_rl(zero, zero);
26 static struct range_list *rl_one(void)
28 return alloc_rl(one, one);
31 enum {
32 RL_EXACT,
33 RL_HARD,
34 RL_FUZZY,
35 RL_IMPLIED,
36 RL_ABSOLUTE
39 static struct range_list *last_stmt_rl(struct statement *stmt, int implied)
41 if (!stmt)
42 return NULL;
44 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
45 if (stmt->type != STMT_EXPRESSION)
46 return NULL;
47 return _get_rl(stmt->expression, implied);
50 static struct range_list *handle_expression_statement_rl(struct expression *expr, int implied)
52 return last_stmt_rl(get_expression_statement(expr), implied);
55 static struct range_list *handle_ampersand_rl(int implied)
57 if (implied == RL_EXACT || implied == RL_HARD)
58 return NULL;
59 return alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
62 static struct range_list *handle_negate_rl(struct expression *expr, int implied)
64 if (known_condition_true(expr->unop))
65 return rl_zero();
66 if (known_condition_false(expr->unop))
67 return rl_one();
69 if (implied == RL_EXACT)
70 return NULL;
72 if (implied_condition_true(expr->unop))
73 return rl_zero();
74 if (implied_condition_false(expr->unop))
75 return rl_one();
76 return alloc_rl(zero, one);
79 static struct range_list *handle_bitwise_negate(struct expression *expr, int implied)
81 struct range_list *rl;
82 sval_t sval;
84 rl = _get_rl(expr->unop, implied);
85 if (!rl_to_sval(rl, &sval))
86 return NULL;
87 sval = sval_preop(sval, '~');
88 sval_cast(get_type(expr->unop), sval);
89 return alloc_rl(sval, sval);
92 static struct range_list *handle_minus_preop(struct expression *expr, int implied)
94 struct range_list *rl;
95 sval_t sval;
97 rl = _get_rl(expr->unop, implied);
98 if (!rl_to_sval(rl, &sval))
99 return NULL;
100 sval = sval_preop(sval, '-');
101 return alloc_rl(sval, sval);
104 static struct range_list *handle_preop_rl(struct expression *expr, int implied)
106 switch (expr->op) {
107 case '&':
108 return handle_ampersand_rl(implied);
109 case '!':
110 return handle_negate_rl(expr, implied);
111 case '~':
112 return handle_bitwise_negate(expr, implied);
113 case '-':
114 return handle_minus_preop(expr, implied);
115 case '*':
116 return handle_variable(expr, implied);
117 case '(':
118 return handle_expression_statement_rl(expr, implied);
119 default:
120 return NULL;
124 static struct range_list *handle_divide_rl(struct expression *expr, int implied)
126 struct range_list *left_rl, *right_rl;
127 struct symbol *type;
128 sval_t min, max;
130 type = get_type(expr);
132 left_rl = _get_rl(expr->left, implied);
133 left_rl = cast_rl(type, left_rl);
134 right_rl = _get_rl(expr->right, implied);
135 right_rl = cast_rl(type, right_rl);
137 if (!left_rl || !right_rl)
138 return NULL;
139 if (is_whole_rl(left_rl) || is_whole_rl(right_rl))
140 return NULL;
141 if (sval_is_negative(rl_min(left_rl)) || sval_cmp_val(rl_min(right_rl), 0) <= 0)
142 return NULL;
144 max = rl_max(left_rl);
145 if (!sval_is_max(max))
146 max = sval_binop(max, '/', rl_min(right_rl));
147 min = sval_binop(rl_min(left_rl), '/', rl_max(right_rl));
148 return alloc_rl(min, max);
151 static struct range_list *handle_subtract_rl(struct expression *expr, int implied)
153 struct symbol *type;
154 struct range_list *left_rl, *right_rl;
155 sval_t max, min, tmp;
156 int comparison;
158 type = get_type(expr);
159 comparison = get_comparison(expr->left, expr->right);
161 left_rl = _get_rl(expr->left, implied);
162 left_rl = cast_rl(type, left_rl);
163 right_rl = _get_rl(expr->right, implied);
164 right_rl = cast_rl(type, right_rl);
166 if ((!left_rl || !right_rl) &&
167 (implied == RL_EXACT || implied == RL_HARD || implied == RL_FUZZY))
168 return NULL;
170 if (!left_rl)
171 left_rl = alloc_whole_rl(type);
172 if (!right_rl)
173 right_rl = alloc_whole_rl(type);
175 /* negative values complicate everything fix this later */
176 if (sval_is_negative(rl_min(right_rl)))
177 return NULL;
178 max = rl_max(left_rl);
180 switch (comparison) {
181 case '>':
182 case SPECIAL_UNSIGNED_GT:
183 min = sval_type_val(type, 1);
184 max = rl_max(left_rl);
185 break;
186 case SPECIAL_GTE:
187 case SPECIAL_UNSIGNED_GTE:
188 min = sval_type_val(type, 0);
189 max = rl_max(left_rl);
190 break;
191 default:
192 if (sval_binop_overflows(rl_min(left_rl), '-', rl_max(right_rl)))
193 return NULL;
194 min = sval_type_min(type);
197 if (!sval_binop_overflows(rl_min(left_rl), '-', rl_max(right_rl))) {
198 tmp = sval_binop(rl_min(left_rl), '-', rl_max(right_rl));
199 if (sval_cmp(tmp, min) > 0)
200 min = tmp;
203 if (!sval_is_max(rl_max(left_rl))) {
204 tmp = sval_binop(rl_max(left_rl), '-', rl_min(right_rl));
205 if (sval_cmp(tmp, max) < 0)
206 max = tmp;
209 if (sval_is_min(min) && sval_is_max(max))
210 return NULL;
212 return cast_rl(type, alloc_rl(min, max));
215 static struct range_list *handle_mod_rl(struct expression *expr, int implied)
217 struct range_list *rl;
218 sval_t left, right, sval;
220 if (implied == RL_EXACT) {
221 if (!get_value(expr->right, &right))
222 return NULL;
223 if (!get_value(expr->left, &left))
224 return NULL;
225 sval = sval_binop(left, '%', right);
226 return alloc_rl(sval, sval);
228 /* if we can't figure out the right side it's probably hopeless */
229 if (!get_implied_value(expr->right, &right))
230 return NULL;
232 right = sval_cast(get_type(expr), right);
233 right.value--;
235 rl = _get_rl(expr->left, implied);
236 if (rl && rl_max(rl).uvalue < right.uvalue)
237 right.uvalue = rl_max(rl).uvalue;
239 return alloc_rl(zero, right);
242 static sval_t sval_lowest_set_bit(sval_t sval)
244 int i;
245 int found = 0;
247 for (i = 0; i < 64; i++) {
248 if (sval.uvalue & 1ULL << i) {
249 if (!found++)
250 continue;
251 sval.uvalue &= ~(1ULL << i);
254 return sval;
257 static struct range_list *handle_bitwise_AND(struct expression *expr, int implied)
259 struct symbol *type;
260 struct range_list *left_rl, *right_rl;
261 sval_t known;
263 if (implied != RL_IMPLIED && implied != RL_ABSOLUTE)
264 return NULL;
266 type = get_type(expr);
268 if (get_implied_value(expr->left, &known)) {
269 sval_t min;
271 min = sval_lowest_set_bit(known);
272 left_rl = alloc_rl(min, known);
273 left_rl = cast_rl(type, left_rl);
274 add_range(&left_rl, sval_type_val(type, 0), sval_type_val(type, 0));
275 } else {
276 left_rl = _get_rl(expr->left, implied);
277 if (left_rl) {
278 left_rl = cast_rl(type, left_rl);
279 left_rl = alloc_rl(sval_type_val(type, 0), rl_max(left_rl));
280 } else {
281 if (implied == RL_HARD)
282 return NULL;
283 left_rl = alloc_whole_rl(type);
287 if (get_implied_value(expr->right, &known)) {
288 sval_t min;
290 min = sval_lowest_set_bit(known);
291 right_rl = alloc_rl(min, known);
292 right_rl = cast_rl(type, right_rl);
293 add_range(&right_rl, sval_type_val(type, 0), sval_type_val(type, 0));
294 } else {
295 right_rl = _get_rl(expr->right, implied);
296 if (right_rl) {
297 right_rl = cast_rl(type, right_rl);
298 right_rl = alloc_rl(sval_type_val(type, 0), rl_max(right_rl));
299 } else {
300 if (implied == RL_HARD)
301 return NULL;
302 right_rl = alloc_whole_rl(type);
306 return rl_intersection(left_rl, right_rl);
309 static struct range_list *handle_bitwise_OR(struct expression *expr, int implied)
311 struct symbol *type;
312 struct range_list *left_rl, *right_rl;
314 if (implied != RL_IMPLIED && implied != RL_ABSOLUTE)
315 return NULL;
317 type = get_type(expr);
319 get_absolute_rl(expr->left, &left_rl);
320 get_absolute_rl(expr->right, &right_rl);
321 left_rl = cast_rl(type, left_rl);
322 right_rl = cast_rl(type, right_rl);
324 return rl_union(left_rl, right_rl);
327 static struct range_list *handle_right_shift(struct expression *expr, int implied)
329 struct range_list *left_rl;
330 sval_t right;
331 sval_t min, max;
333 if (implied == RL_EXACT || implied == RL_HARD)
334 return NULL;
335 /* this is hopeless without the right side */
336 if (!get_implied_value(expr->right, &right))
337 return NULL;
338 left_rl = _get_rl(expr->left, implied);
339 if (left_rl) {
340 max = rl_max(left_rl);
341 min = rl_min(left_rl);
342 } else {
343 if (implied == RL_FUZZY)
344 return NULL;
345 max = sval_type_max(get_type(expr->left));
346 min = sval_type_val(get_type(expr->left), 0);
349 max = sval_binop(max, SPECIAL_RIGHTSHIFT, right);
350 min = sval_binop(min, SPECIAL_RIGHTSHIFT, right);
351 return alloc_rl(min, max);
354 static struct range_list *handle_left_shift(struct expression *expr, int implied)
356 struct range_list *left_rl, *res;
357 sval_t right;
358 sval_t min, max;
359 int add_zero = 0;
361 if (implied == RL_EXACT || implied == RL_HARD)
362 return NULL;
363 /* this is hopeless without the right side */
364 if (!get_implied_value(expr->right, &right))
365 return NULL;
366 left_rl = _get_rl(expr->left, implied);
367 if (left_rl) {
368 max = rl_max(left_rl);
369 min = rl_min(left_rl);
370 if (min.value == 0) {
371 min.value = 1;
372 add_zero = 1;
374 } else {
375 if (implied == RL_FUZZY)
376 return NULL;
377 max = sval_type_max(get_type(expr->left));
378 min = sval_type_val(get_type(expr->left), 1);
379 add_zero = 1;
382 max = sval_binop(max, SPECIAL_LEFTSHIFT, right);
383 min = sval_binop(min, SPECIAL_LEFTSHIFT, right);
384 res = alloc_rl(min, max);
385 if (add_zero)
386 res = rl_union(res, rl_zero());
387 return res;
390 static struct range_list *handle_known_binop(struct expression *expr)
392 sval_t left, right;
394 if (!get_value(expr->left, &left))
395 return NULL;
396 if (!get_value(expr->right, &right))
397 return NULL;
398 left = sval_binop(left, expr->op, right);
399 return alloc_rl(left, left);
402 static struct range_list *handle_binop_rl(struct expression *expr, int implied)
404 struct symbol *type;
405 struct range_list *left_rl, *right_rl, *rl;
406 sval_t min, max;
408 rl = handle_known_binop(expr);
409 if (rl)
410 return rl;
411 if (implied == RL_EXACT)
412 return NULL;
414 switch (expr->op) {
415 case '%':
416 return handle_mod_rl(expr, implied);
417 case '&':
418 return handle_bitwise_AND(expr, implied);
419 case '|':
420 return handle_bitwise_OR(expr, implied);
421 case SPECIAL_RIGHTSHIFT:
422 return handle_right_shift(expr, implied);
423 case SPECIAL_LEFTSHIFT:
424 return handle_left_shift(expr, implied);
425 case '-':
426 return handle_subtract_rl(expr, implied);
427 case '/':
428 return handle_divide_rl(expr, implied);
431 type = get_type(expr);
432 left_rl = _get_rl(expr->left, implied);
433 left_rl = cast_rl(type, left_rl);
434 right_rl = _get_rl(expr->right, implied);
435 right_rl = cast_rl(type, right_rl);
437 if (!left_rl || !right_rl)
438 return NULL;
440 if (sval_binop_overflows(rl_min(left_rl), expr->op, rl_min(right_rl)))
441 return NULL;
442 min = sval_binop(rl_min(left_rl), expr->op, rl_min(right_rl));
444 if (sval_binop_overflows(rl_max(left_rl), expr->op, rl_max(right_rl))) {
445 switch (implied) {
446 case RL_FUZZY:
447 case RL_HARD:
448 return NULL;
449 default:
450 max = sval_type_max(get_type(expr));
452 } else {
453 max = sval_binop(rl_max(left_rl), expr->op, rl_max(right_rl));
456 return alloc_rl(min, max);
459 static int do_comparison(struct expression *expr)
461 struct range_list *left_ranges = NULL;
462 struct range_list *right_ranges = NULL;
463 int poss_true, poss_false;
464 struct symbol *type;
466 type = get_type(expr);
468 get_implied_rl(expr->left, &left_ranges);
469 get_implied_rl(expr->right, &right_ranges);
470 left_ranges = cast_rl(type, left_ranges);
471 right_ranges = cast_rl(type, right_ranges);
473 poss_true = possibly_true_rl(left_ranges, expr->op, right_ranges);
474 poss_false = possibly_false_rl(left_ranges, expr->op, right_ranges);
476 free_rl(&left_ranges);
477 free_rl(&right_ranges);
479 if (!poss_true && !poss_false)
480 return 0;
481 if (poss_true && !poss_false)
482 return 1;
483 if (!poss_true && poss_false)
484 return 2;
485 return 3;
488 static struct range_list *handle_comparison_rl(struct expression *expr, int implied)
490 sval_t left, right;
491 int res;
493 if (get_value(expr->left, &left) && get_value(expr->right, &right)) {
494 struct data_range tmp_left, tmp_right;
496 tmp_left.min = left;
497 tmp_left.max = left;
498 tmp_right.min = right;
499 tmp_right.max = right;
500 if (true_comparison_range(&tmp_left, expr->op, &tmp_right))
501 return rl_one();
502 return rl_zero();
505 if (implied == RL_EXACT)
506 return NULL;
508 res = do_comparison(expr);
509 if (res == 1)
510 return rl_one();
511 if (res == 2)
512 return rl_zero();
514 return alloc_rl(zero, one);
517 static struct range_list *handle_logical_rl(struct expression *expr, int implied)
519 sval_t left, right;
520 int left_known = 0;
521 int right_known = 0;
523 if (implied == RL_EXACT) {
524 if (get_value(expr->left, &left))
525 left_known = 1;
526 if (get_value(expr->right, &right))
527 right_known = 1;
528 } else {
529 if (get_implied_value(expr->left, &left))
530 left_known = 1;
531 if (get_implied_value(expr->right, &right))
532 right_known = 1;
535 switch (expr->op) {
536 case SPECIAL_LOGICAL_OR:
537 if (left_known && left.value)
538 return rl_one();
539 if (right_known && right.value)
540 return rl_one();
541 if (left_known && right_known)
542 return rl_zero();
543 break;
544 case SPECIAL_LOGICAL_AND:
545 if (left_known && right_known) {
546 if (left.value && right.value)
547 return rl_one();
548 return rl_zero();
550 break;
551 default:
552 return NULL;
555 if (implied == RL_EXACT)
556 return NULL;
558 return alloc_rl(zero, one);
561 static struct range_list *handle_conditional_rl(struct expression *expr, int implied)
563 struct range_list *true_rl, *false_rl;
564 struct symbol *type;
565 int final_pass_orig = final_pass;
567 if (known_condition_true(expr->conditional))
568 return _get_rl(expr->cond_true, implied);
569 if (known_condition_false(expr->conditional))
570 return _get_rl(expr->cond_false, implied);
572 if (implied == RL_EXACT)
573 return NULL;
575 if (implied_condition_true(expr->conditional))
576 return _get_rl(expr->cond_true, implied);
577 if (implied_condition_false(expr->conditional))
578 return _get_rl(expr->cond_false, implied);
581 /* this becomes a problem with deeply nested conditional statements */
582 if (low_on_memory())
583 return NULL;
585 type = get_type(expr);
587 __push_fake_cur_slist();
588 final_pass = 0;
589 __split_whole_condition(expr->conditional);
590 true_rl = _get_rl(expr->cond_true, implied);
591 __push_true_states();
592 __use_false_states();
593 false_rl = _get_rl(expr->cond_false, implied);
594 __merge_true_states();
595 __free_fake_cur_slist();
596 final_pass = final_pass_orig;
598 if (!true_rl || !false_rl)
599 return NULL;
600 true_rl = cast_rl(type, true_rl);
601 false_rl = cast_rl(type, false_rl);
603 return rl_union(true_rl, false_rl);
606 static int get_fuzzy_max_helper(struct expression *expr, sval_t *max)
608 struct smatch_state *state;
609 sval_t sval;
611 if (get_hard_max(expr, &sval)) {
612 *max = sval;
613 return 1;
616 state = get_state_expr(SMATCH_EXTRA, expr);
617 if (!state || !estate_has_fuzzy_max(state))
618 return 0;
619 *max = sval_cast(get_type(expr), estate_get_fuzzy_max(state));
620 return 1;
623 static int get_fuzzy_min_helper(struct expression *expr, sval_t *min)
625 struct sm_state *sm;
626 struct sm_state *tmp;
627 sval_t sval;
629 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
630 if (!sm)
631 return 0;
633 if (!sval_is_min(estate_min(sm->state))) {
634 *min = estate_min(sm->state);
635 return 1;
638 sval = sval_type_max(estate_type(sm->state));
639 FOR_EACH_PTR(sm->possible, tmp) {
640 sval_t new_max;
642 new_max = estate_max(tmp->state);
643 if (sval_cmp(new_max, sval) < 0)
644 sval = new_max;
645 } END_FOR_EACH_PTR(tmp);
647 if (sval_is_max(sval))
648 return 0;
649 *min = sval_cast(get_type(expr), sval);
650 return 1;
653 int get_const_value(struct expression *expr, sval_t *sval)
655 struct symbol *sym;
656 sval_t right;
658 if (expr->type != EXPR_SYMBOL || !expr->symbol)
659 return 0;
660 sym = expr->symbol;
661 if (!(sym->ctype.modifiers & MOD_CONST))
662 return 0;
663 if (get_value(sym->initializer, &right)) {
664 *sval = sval_cast(get_type(expr), right);
665 return 1;
667 return 0;
670 static struct range_list *handle_variable(struct expression *expr, int implied)
672 struct smatch_state *state;
673 struct range_list *rl;
674 sval_t sval, min, max;
676 if (get_const_value(expr, &sval))
677 return alloc_rl(sval, sval);
679 switch (implied) {
680 case RL_EXACT:
681 return NULL;
682 case RL_HARD:
683 case RL_IMPLIED:
684 case RL_ABSOLUTE:
685 state = get_state_expr(SMATCH_EXTRA, expr);
686 if (!state || !state->data) {
687 if (implied == RL_HARD)
688 return NULL;
689 if (get_local_rl(expr, &rl))
690 return rl;
691 if (get_db_type_rl(expr, &rl))
692 return rl;
693 return NULL;
695 if (implied == RL_HARD && !estate_has_hard_max(state))
696 return NULL;
697 return clone_rl(estate_rl(state));
698 case RL_FUZZY:
699 if (!get_fuzzy_min_helper(expr, &min))
700 min = sval_type_min(get_type(expr));
701 if (!get_fuzzy_max_helper(expr, &max))
702 return NULL;
703 /* fuzzy ranges are often inverted */
704 if (sval_cmp(min, max) > 0) {
705 sval = min;
706 min = max;
707 max = sval;
709 return alloc_rl(min, max);
711 return NULL;
714 static sval_t handle_sizeof(struct expression *expr)
716 struct symbol *sym;
717 sval_t ret;
719 ret = sval_blank(expr);
720 sym = expr->cast_type;
721 if (!sym) {
722 sym = evaluate_expression(expr->cast_expression);
724 * Expressions of restricted types will possibly get
725 * promoted - check that here
727 if (is_restricted_type(sym)) {
728 if (sym->bit_size < bits_in_int)
729 sym = &int_ctype;
730 } else if (is_fouled_type(sym)) {
731 sym = &int_ctype;
734 examine_symbol_type(sym);
736 ret.type = size_t_ctype;
737 if (sym->bit_size <= 0) /* sizeof(void) */ {
738 if (get_real_base_type(sym) == &void_ctype)
739 ret.value = 1;
740 else
741 ret.value = 0;
742 } else
743 ret.value = bits_to_bytes(sym->bit_size);
745 return ret;
748 static struct range_list *handle_call_rl(struct expression *expr, int implied)
750 struct range_list *rl;
752 if (implied == RL_EXACT || implied == RL_HARD || implied == RL_FUZZY)
753 return NULL;
755 if (get_implied_return(expr, &rl))
756 return rl;
757 return db_return_vals(expr);
760 static struct range_list *handle_cast(struct expression *expr, int implied)
762 struct range_list *rl;
763 struct symbol *type;
765 type = get_type(expr);
766 rl = _get_rl(expr->cast_expression, implied);
767 if (rl)
768 return cast_rl(type, rl);
769 if (implied == RL_ABSOLUTE)
770 return alloc_whole_rl(type);
771 if (implied == RL_IMPLIED && type &&
772 type->bit_size > 0 && type->bit_size < 32)
773 return alloc_whole_rl(type);
774 return NULL;
777 static struct range_list *_get_rl(struct expression *expr, int implied)
779 struct range_list *rl;
780 struct symbol *type;
781 sval_t sval;
783 expr = strip_parens(expr);
784 if (!expr)
785 return NULL;
786 type = get_type(expr);
788 switch (expr->type) {
789 case EXPR_VALUE:
790 sval = sval_from_val(expr, expr->value);
791 rl = alloc_rl(sval, sval);
792 break;
793 case EXPR_PREOP:
794 rl = handle_preop_rl(expr, implied);
795 break;
796 case EXPR_POSTOP:
797 rl = _get_rl(expr->unop, implied);
798 break;
799 case EXPR_CAST:
800 case EXPR_FORCE_CAST:
801 case EXPR_IMPLIED_CAST:
802 rl = handle_cast(expr, implied);
803 break;
804 case EXPR_BINOP:
805 rl = handle_binop_rl(expr, implied);
806 break;
807 case EXPR_COMPARE:
808 rl = handle_comparison_rl(expr, implied);
809 break;
810 case EXPR_LOGICAL:
811 rl = handle_logical_rl(expr, implied);
812 break;
813 case EXPR_PTRSIZEOF:
814 case EXPR_SIZEOF:
815 sval = handle_sizeof(expr);
816 rl = alloc_rl(sval, sval);
817 break;
818 case EXPR_SELECT:
819 case EXPR_CONDITIONAL:
820 rl = handle_conditional_rl(expr, implied);
821 break;
822 case EXPR_CALL:
823 rl = handle_call_rl(expr, implied);
824 break;
825 default:
826 rl = handle_variable(expr, implied);
829 if (rl)
830 return rl;
831 if (type && implied == RL_ABSOLUTE)
832 return alloc_whole_rl(type);
833 return NULL;
836 /* returns 1 if it can get a value literal or else returns 0 */
837 int get_value(struct expression *expr, sval_t *sval)
839 struct range_list *rl;
841 rl = _get_rl(expr, RL_EXACT);
842 if (!rl_to_sval(rl, sval))
843 return 0;
844 return 1;
847 int get_implied_value(struct expression *expr, sval_t *sval)
849 struct range_list *rl;
851 rl = _get_rl(expr, RL_IMPLIED);
852 if (!rl_to_sval(rl, sval))
853 return 0;
854 return 1;
857 int get_implied_min(struct expression *expr, sval_t *sval)
859 struct range_list *rl;
861 rl = _get_rl(expr, RL_IMPLIED);
862 if (!rl)
863 return 0;
864 *sval = rl_min(rl);
865 return 1;
868 int get_implied_max(struct expression *expr, sval_t *sval)
870 struct range_list *rl;
872 rl = _get_rl(expr, RL_IMPLIED);
873 if (!rl)
874 return 0;
875 *sval = rl_max(rl);
876 return 1;
879 int get_implied_rl(struct expression *expr, struct range_list **rl)
881 *rl = _get_rl(expr, RL_IMPLIED);
882 if (*rl)
883 return 1;
884 return 0;
887 int get_absolute_rl(struct expression *expr, struct range_list **rl)
889 *rl = _get_rl(expr, RL_ABSOLUTE);
890 if (!*rl)
891 *rl = alloc_whole_rl(get_type(expr));
892 return 1;
895 int get_implied_rl_var_sym(const char *var, struct symbol *sym, struct range_list **rl)
897 struct smatch_state *state;
899 state = get_state(SMATCH_EXTRA, var, sym);
900 *rl = estate_rl(state);
901 if (*rl)
902 return 1;
903 return 0;
906 int get_hard_max(struct expression *expr, sval_t *sval)
908 struct range_list *rl;
910 rl = _get_rl(expr, RL_HARD);
911 if (!rl)
912 return 0;
913 *sval = rl_max(rl);
914 return 1;
917 int get_fuzzy_min(struct expression *expr, sval_t *sval)
919 struct range_list *rl;
920 sval_t tmp;
922 rl = _get_rl(expr, RL_FUZZY);
923 if (!rl)
924 return 0;
925 tmp = rl_min(rl);
926 if (sval_is_negative(tmp) && sval_is_min(tmp))
927 return 0;
928 *sval = tmp;
929 return 1;
932 int get_fuzzy_max(struct expression *expr, sval_t *sval)
934 struct range_list *rl;
935 sval_t max;
937 rl = _get_rl(expr, RL_FUZZY);
938 if (!rl)
939 return 0;
940 max = rl_max(rl);
941 if (max.uvalue > INT_MAX - 10000)
942 return 0;
943 *sval = max;
944 return 1;
947 int get_absolute_min(struct expression *expr, sval_t *sval)
949 struct range_list *rl;
950 struct symbol *type;
952 type = get_type(expr);
953 if (!type)
954 type = &llong_ctype; // FIXME: this is wrong but places assume get type can't fail.
955 rl = _get_rl(expr, RL_ABSOLUTE);
956 if (rl)
957 *sval = rl_min(rl);
958 else
959 *sval = sval_type_min(type);
961 if (sval_cmp(*sval, sval_type_min(type)) < 0)
962 *sval = sval_type_min(type);
963 return 1;
966 int get_absolute_max(struct expression *expr, sval_t *sval)
968 struct range_list *rl;
969 struct symbol *type;
971 type = get_type(expr);
972 if (!type)
973 type = &llong_ctype;
974 rl = _get_rl(expr, RL_ABSOLUTE);
975 if (rl)
976 *sval = rl_max(rl);
977 else
978 *sval = sval_type_max(type);
980 if (sval_cmp(sval_type_max(type), *sval) < 0)
981 *sval = sval_type_max(type);
982 return 1;
985 int known_condition_true(struct expression *expr)
987 sval_t tmp;
989 if (!expr)
990 return 0;
992 if (get_value(expr, &tmp) && tmp.value)
993 return 1;
995 return 0;
998 int known_condition_false(struct expression *expr)
1000 if (!expr)
1001 return 0;
1003 if (is_zero(expr))
1004 return 1;
1006 if (expr->type == EXPR_CALL) {
1007 if (sym_name_is("__builtin_constant_p", expr->fn))
1008 return 1;
1010 return 0;
1013 int implied_condition_true(struct expression *expr)
1015 sval_t tmp;
1017 if (!expr)
1018 return 0;
1020 if (known_condition_true(expr))
1021 return 1;
1022 if (get_implied_value(expr, &tmp) && tmp.value)
1023 return 1;
1025 if (expr->type == EXPR_POSTOP)
1026 return implied_condition_true(expr->unop);
1028 if (expr->type == EXPR_PREOP && expr->op == SPECIAL_DECREMENT)
1029 return implied_not_equal(expr->unop, 1);
1030 if (expr->type == EXPR_PREOP && expr->op == SPECIAL_INCREMENT)
1031 return implied_not_equal(expr->unop, -1);
1033 expr = strip_expr(expr);
1034 switch (expr->type) {
1035 case EXPR_COMPARE:
1036 if (do_comparison(expr) == 1)
1037 return 1;
1038 break;
1039 case EXPR_PREOP:
1040 if (expr->op == '!') {
1041 if (implied_condition_false(expr->unop))
1042 return 1;
1043 break;
1045 break;
1046 default:
1047 if (implied_not_equal(expr, 0) == 1)
1048 return 1;
1049 break;
1051 return 0;
1054 int implied_condition_false(struct expression *expr)
1056 struct expression *tmp;
1057 sval_t sval;
1059 if (!expr)
1060 return 0;
1062 if (known_condition_false(expr))
1063 return 1;
1065 switch (expr->type) {
1066 case EXPR_COMPARE:
1067 if (do_comparison(expr) == 2)
1068 return 1;
1069 case EXPR_PREOP:
1070 if (expr->op == '!') {
1071 if (implied_condition_true(expr->unop))
1072 return 1;
1073 break;
1075 tmp = strip_expr(expr);
1076 if (tmp != expr)
1077 return implied_condition_false(tmp);
1078 break;
1079 default:
1080 if (get_implied_value(expr, &sval) && sval.value == 0)
1081 return 1;
1082 break;
1084 return 0;