user_data: track user data across assignments
[smatch.git] / smatch_math.c
blob820083d2c543a33133f8d12f18d250d592beec49
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 sval_t _get_value(struct expression *expr, int *undefined, int implied);
16 static sval_t _get_implied_value(struct expression *expr, int *undefined, int implied);
18 #define BOGUS 12345
20 static sval_t zero = {.type = &int_ctype, {.value = 0} };
21 static sval_t one = {.type = &int_ctype, {.value = 1} };
22 static sval_t bogus = {.type = &int_ctype, {.value = BOGUS} };
24 enum {
25 NOTIMPLIED,
26 IMPLIED,
27 IMPLIED_MIN,
28 IMPLIED_MAX,
29 FUZZY_MAX,
30 FUZZY_MIN,
31 ABSOLUTE_MIN,
32 ABSOLUTE_MAX,
33 HARD_MAX,
36 static int opposite_implied(int implied)
38 if (implied == IMPLIED_MIN)
39 return IMPLIED_MAX;
40 if (implied == IMPLIED_MAX)
41 return IMPLIED_MIN;
42 if (implied == FUZZY_MIN)
43 return FUZZY_MAX;
44 if (implied == FUZZY_MAX)
45 return FUZZY_MIN;
46 if (implied == ABSOLUTE_MIN)
47 return ABSOLUTE_MAX;
48 if (implied == ABSOLUTE_MAX)
49 return ABSOLUTE_MIN;
50 if (implied == HARD_MAX) /* we don't have a hard min. */
51 return NOTIMPLIED;
53 return implied;
56 static int last_stmt_sval(struct statement *stmt, sval_t *sval)
58 struct expression *expr;
60 if (!stmt)
61 return 0;
63 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
64 if (stmt->type != STMT_EXPRESSION)
65 return 0;
66 expr = stmt->expression;
67 if (!get_value(expr, sval))
68 return 0;
69 return 1;
72 static sval_t handle_expression_statement(struct expression *expr, int *undefined, int implied)
74 struct statement *stmt;
75 sval_t ret;
77 stmt = get_expression_statement(expr);
78 if (!last_stmt_sval(stmt, &ret)) {
79 *undefined = 1;
80 ret = bogus;
83 return ret;
86 static sval_t handle_ampersand(int *undefined, int implied)
88 sval_t ret;
90 ret.type = &ptr_ctype;
91 ret.value = BOGUS;
93 if (implied == IMPLIED_MIN || implied == FUZZY_MIN || implied == ABSOLUTE_MIN)
94 return valid_ptr_min_sval;
95 if (implied == IMPLIED_MAX || implied == FUZZY_MAX || implied == ABSOLUTE_MAX)
96 return valid_ptr_max_sval;
98 *undefined = 1;
99 return ret;
102 static sval_t handle_negate(struct expression *expr, int *undefined, int implied)
104 sval_t ret;
106 ret = sval_blank(expr->unop);
108 if (known_condition_true(expr->unop)) {
109 ret.value = 0;
110 return ret;
113 if (implied == NOTIMPLIED) {
114 *undefined = 1;
115 return bogus;
118 if (implied_condition_true(expr->unop)) {
119 ret.value = 0;
120 return ret;
122 if (implied_condition_false(expr->unop)) {
123 ret.value = 1;
124 return ret;
126 if (implied == IMPLIED_MIN || implied == FUZZY_MIN || implied == ABSOLUTE_MIN) {
127 ret.value = 0;
128 return ret;
130 if (implied == IMPLIED_MAX || implied == FUZZY_MAX || implied == ABSOLUTE_MAX) {
131 ret.value = 1;
132 return ret;
134 *undefined = 1;
135 return bogus;
138 static sval_t handle_preop(struct expression *expr, int *undefined, int implied)
140 sval_t ret;
142 switch (expr->op) {
143 case '&':
144 ret = handle_ampersand(undefined, implied);
145 break;
146 case '!':
147 ret = handle_negate(expr, undefined, implied);
148 break;
149 case '~':
150 ret = _get_value(expr->unop, undefined, implied);
151 ret = sval_preop(ret, '~');
152 ret = sval_cast(get_type(expr->unop), ret);
153 break;
154 case '-':
155 ret = _get_value(expr->unop, undefined, implied);
156 ret = sval_preop(ret, '-');
157 break;
158 case '*':
159 ret = _get_implied_value(expr, undefined, implied);
160 break;
161 case '(':
162 ret = handle_expression_statement(expr, undefined, implied);
163 break;
164 default:
165 *undefined = 1;
166 ret = sval_blank(expr);
168 ret = sval_cast(get_type(expr), ret);
169 return ret;
172 static sval_t handle_divide(struct expression *expr, int *undefined, int implied)
174 sval_t left, right;
176 left = _get_value(expr->left, undefined, implied);
177 right = _get_value(expr->right, undefined, opposite_implied(implied));
179 if (right.value == 0) {
180 *undefined = 1;
181 return bogus;
184 return sval_binop(left, '/', right);
187 static sval_t handle_subtract(struct expression *expr, int *undefined, int implied)
189 struct symbol *type;
190 sval_t left, right, ret;
191 int left_undefined = 0;
192 int right_undefined = 0;
193 int known_but_negative = 0;
194 int comparison;
196 left = _get_value(expr->left, &left_undefined, implied);
197 right = _get_value(expr->right, &right_undefined, opposite_implied(implied));
199 if (!left_undefined && !right_undefined) {
200 ret = sval_binop(left, '-', right);
201 if (sval_is_negative(ret))
202 known_but_negative = 1;
203 else
204 return ret; /* best case scenario */
207 comparison = get_comparison(expr->left, expr->right);
208 if (!comparison)
209 goto bogus;
211 type = get_type(expr);
213 switch (comparison) {
214 case '>':
215 case SPECIAL_UNSIGNED_GT:
216 switch (implied) {
217 case IMPLIED_MIN:
218 case FUZZY_MIN:
219 case ABSOLUTE_MIN:
220 return sval_type_val(type, 1);
221 case IMPLIED_MAX:
222 case FUZZY_MAX:
223 case ABSOLUTE_MAX:
224 return _get_value(expr->left, undefined, implied);
226 break;
227 case SPECIAL_GTE:
228 case SPECIAL_UNSIGNED_GTE:
229 switch (implied) {
230 case IMPLIED_MIN:
231 case FUZZY_MIN:
232 case ABSOLUTE_MIN:
233 return sval_type_val(type, 0);
234 case IMPLIED_MAX:
235 case FUZZY_MAX:
236 case ABSOLUTE_MAX:
237 return _get_value(expr->left, undefined, implied);
239 break;
242 if (known_but_negative)
243 return ret;
245 bogus:
246 *undefined = 1;
247 return bogus;
250 static sval_t handle_mod(struct expression *expr, int *undefined, int implied)
252 sval_t left, right;
254 /* if we can't figure out the right side it's probably hopeless */
255 right = _get_value(expr->right, undefined, implied);
256 if (*undefined || right.value == 0) {
257 *undefined = 1;
258 return bogus;
261 switch (implied) {
262 case NOTIMPLIED:
263 case IMPLIED:
264 left = _get_value(expr->left, undefined, implied);
265 if (!*undefined)
266 return sval_binop(left, '%', right);
267 return bogus;
268 case IMPLIED_MIN:
269 case FUZZY_MIN:
270 case ABSOLUTE_MIN:
271 *undefined = 0;
272 return sval_type_val(get_type(expr->left), 0);
273 case IMPLIED_MAX:
274 case FUZZY_MAX:
275 case ABSOLUTE_MAX:
276 *undefined = 0;
277 right = sval_cast(get_type(expr), right);
278 right.value--;
279 return right;
281 return bogus;
284 static sval_t handle_binop(struct expression *expr, int *undefined, int implied)
286 struct symbol *type;
287 sval_t left, right;
288 sval_t ret = {.type = &int_ctype, {.value = 123456} };
289 int local_undef = 0;
291 switch (expr->op) {
292 case '%':
293 return handle_mod(expr, undefined, implied);
294 case '&':
295 left = _get_value(expr->left, &local_undef, implied);
296 if (local_undef) {
297 if (implied == IMPLIED_MIN || implied == ABSOLUTE_MIN) {
298 ret = sval_blank(expr->left);
299 ret.value = 0;
300 return ret;
302 if (implied != IMPLIED_MAX && implied != ABSOLUTE_MAX)
303 *undefined = 1;
304 if (!get_absolute_max(expr->left, &left))
305 *undefined = 1;
307 right = _get_value(expr->right, undefined, implied);
308 if (*undefined)
309 return bogus;
310 return sval_binop(left, '&', right);
312 case SPECIAL_RIGHTSHIFT:
313 left = _get_value(expr->left, &local_undef, implied);
314 if (local_undef) {
315 if (implied == IMPLIED_MIN || implied == ABSOLUTE_MIN) {
316 ret = sval_blank(expr->left);
317 ret.value = 0;
318 return ret;
320 if (implied != IMPLIED_MAX && implied != ABSOLUTE_MAX)
321 *undefined = 1;
322 if (!get_absolute_max(expr->left, &left))
323 *undefined = 1;
325 right = _get_value(expr->right, undefined, implied);
326 if (*undefined)
327 return bogus;
328 return sval_binop(left, SPECIAL_RIGHTSHIFT, right);
329 case '-':
330 return handle_subtract(expr, undefined, implied);
333 left = _get_value(expr->left, undefined, implied);
334 right = _get_value(expr->right, undefined, implied);
336 if (*undefined)
337 return bogus;
339 type = get_type(expr);
340 left = sval_cast(type, left);
341 right = sval_cast(type, right);
343 switch (implied) {
344 case IMPLIED_MAX:
345 case ABSOLUTE_MAX:
346 if (sval_binop_overflows(left, expr->op, right))
347 return sval_type_max(get_type(expr));
348 break;
349 case HARD_MAX:
350 case FUZZY_MAX:
351 if (sval_binop_overflows(left, expr->op, right)) {
352 *undefined = 1;
353 return bogus;
357 switch (expr->op) {
358 case '/':
359 return handle_divide(expr, undefined, implied);
360 case '%':
361 if (right.value == 0) {
362 *undefined = 1;
363 return bogus;
364 } else {
365 return sval_binop(left, '%', right);
367 default:
368 ret = sval_binop(left, expr->op, right);
370 return ret;
373 static int do_comparison(struct expression *expr)
375 struct range_list *left_ranges = NULL;
376 struct range_list *right_ranges = NULL;
377 int poss_true, poss_false;
379 get_implied_rl(expr->left, &left_ranges);
380 get_implied_rl(expr->right, &right_ranges);
382 poss_true = possibly_true_rl(left_ranges, expr->op, right_ranges);
383 poss_false = possibly_false_rl(left_ranges, expr->op, right_ranges);
385 free_rl(&left_ranges);
386 free_rl(&right_ranges);
388 if (!poss_true && !poss_false)
389 return 0;
390 if (poss_true && !poss_false)
391 return 1;
392 if (!poss_true && poss_false)
393 return 2;
394 return 3;
397 static sval_t handle_comparison(struct expression *expr, int *undefined, int implied)
399 sval_t left, right;
400 int res;
402 if (get_value(expr->left, &left) && get_value(expr->right, &right)) {
403 struct data_range tmp_left, tmp_right;
405 tmp_left.min = left;
406 tmp_left.max = left;
407 tmp_right.min = right;
408 tmp_right.max = right;
409 if (true_comparison_range(&tmp_left, expr->op, &tmp_right))
410 return one;
411 return zero;
414 if (implied == NOTIMPLIED) {
415 *undefined = 1;
416 return bogus;
419 res = do_comparison(expr);
420 if (res == 1)
421 return one;
422 if (res == 2)
423 return zero;
425 if (implied == IMPLIED_MIN || implied == FUZZY_MIN || implied == ABSOLUTE_MIN)
426 return zero;
427 if (implied == IMPLIED_MAX || implied == FUZZY_MAX || implied == ABSOLUTE_MAX)
428 return one;
430 *undefined = 1;
431 return bogus;
434 static sval_t handle_logical(struct expression *expr, int *undefined, int implied)
436 sval_t left, right;
437 int left_known = 0;
438 int right_known = 0;
440 if (implied == NOTIMPLIED) {
441 if (get_value(expr->left, &left))
442 left_known = 1;
443 if (get_value(expr->right, &right))
444 right_known = 1;
445 } else {
446 if (get_implied_value(expr->left, &left))
447 left_known = 1;
448 if (get_implied_value(expr->right, &right))
449 right_known = 1;
452 switch (expr->op) {
453 case SPECIAL_LOGICAL_OR:
454 if (left_known && left.value)
455 return one;
456 if (right_known && right.value)
457 return one;
458 if (left_known && right_known)
459 return zero;
460 break;
461 case SPECIAL_LOGICAL_AND:
462 if (left_known && right_known) {
463 if (left.value && right.value)
464 return one;
465 return zero;
467 break;
468 default:
469 *undefined = 1;
470 return bogus;
473 if (implied == IMPLIED_MIN || implied == FUZZY_MIN || implied == ABSOLUTE_MIN)
474 return zero;
475 if (implied == IMPLIED_MAX || implied == FUZZY_MAX || implied == ABSOLUTE_MAX)
476 return one;
478 *undefined = 1;
479 return bogus;
482 static sval_t handle_conditional(struct expression *expr, int *undefined, int implied)
484 if (known_condition_true(expr->conditional))
485 return _get_value(expr->cond_true, undefined, implied);
486 if (known_condition_false(expr->conditional))
487 return _get_value(expr->cond_false, undefined, implied);
489 if (implied == NOTIMPLIED) {
490 *undefined = 1;
491 return bogus;
494 if (implied_condition_true(expr->conditional))
495 return _get_value(expr->cond_true, undefined, implied);
496 if (implied_condition_false(expr->conditional))
497 return _get_value(expr->cond_false, undefined, implied);
499 *undefined = 1;
500 return bogus;
503 static int get_local_value(struct expression *expr, sval_t *sval, int implied)
505 switch (implied) {
506 case NOTIMPLIED:
507 case IMPLIED:
508 return 0;
509 case IMPLIED_MIN:
510 case FUZZY_MIN:
511 case ABSOLUTE_MIN:
512 return get_local_min_helper(expr, sval);
513 case ABSOLUTE_MAX:
514 case HARD_MAX:
515 case IMPLIED_MAX:
516 case FUZZY_MAX:
517 return get_local_max_helper(expr, sval);
519 return 0;
522 static int get_implied_value_helper(struct expression *expr, sval_t *sval, int implied)
524 struct smatch_state *state;
525 struct symbol *sym;
526 char *name;
528 /* fixme: this should return the casted value */
530 expr = strip_expr(expr);
532 if (get_value(expr, sval))
533 return 1;
535 name = expr_to_var_sym(expr, &sym);
536 if (!name)
537 return 0;
538 *sval = sval_blank(expr);
539 state = get_state(SMATCH_EXTRA, name, sym);
540 free_string(name);
541 if (!state || !state->data)
542 return get_local_value(expr, sval, implied);
543 if (implied == IMPLIED) {
544 if (estate_get_single_value(state, sval))
545 return 1;
546 return 0;
548 if (implied == HARD_MAX) {
549 if (estate_get_hard_max(state, sval))
550 return 1;
551 return 0;
553 if (implied == IMPLIED_MAX || implied == ABSOLUTE_MAX) {
554 *sval = estate_max(state);
555 return 1;
557 *sval = estate_min(state);
558 return 1;
561 static int get_fuzzy_max_helper(struct expression *expr, sval_t *max)
563 struct sm_state *sm;
564 struct sm_state *tmp;
565 sval_t sval;
567 if (get_hard_max(expr, &sval)) {
568 *max = sval;
569 return 1;
572 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
573 if (!sm)
574 return 0;
576 sval = sval_type_min(estate_type(sm->state));
577 FOR_EACH_PTR(sm->possible, tmp) {
578 sval_t new_min;
580 new_min = estate_min(tmp->state);
581 if (sval_cmp(new_min, sval) > 0)
582 sval = new_min;
583 } END_FOR_EACH_PTR(tmp);
585 if (sval_is_min(sval))
586 return 0;
587 if (sval.value == sval_type_min(sval.type).value + 1) /* it's common to be on off */
588 return 0;
590 *max = sval_cast(get_type(expr), sval);
591 return 1;
594 static int get_fuzzy_min_helper(struct expression *expr, sval_t *min)
596 struct sm_state *sm;
597 struct sm_state *tmp;
598 sval_t sval;
600 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
601 if (!sm)
602 return 0;
604 if (!sval_is_min(estate_min(sm->state))) {
605 *min = estate_min(sm->state);
606 return 1;
609 sval = sval_type_max(estate_type(sm->state));
610 FOR_EACH_PTR(sm->possible, tmp) {
611 sval_t new_max;
613 new_max = estate_max(tmp->state);
614 if (sval_cmp(new_max, sval) < 0)
615 sval = new_max;
616 } END_FOR_EACH_PTR(tmp);
618 if (sval_is_max(sval))
619 return 0;
620 *min = sval_cast(get_type(expr), sval);
621 return 1;
624 static sval_t _get_implied_value(struct expression *expr, int *undefined, int implied)
626 sval_t ret;
628 ret = sval_blank(expr);
630 switch (implied) {
631 case IMPLIED:
632 case IMPLIED_MAX:
633 case IMPLIED_MIN:
634 case HARD_MAX:
635 case ABSOLUTE_MIN:
636 case ABSOLUTE_MAX:
637 if (!get_implied_value_helper(expr, &ret, implied))
638 *undefined = 1;
639 break;
640 case FUZZY_MAX:
641 if (!get_fuzzy_max_helper(expr, &ret))
642 *undefined = 1;
643 break;
644 case FUZZY_MIN:
645 if (!get_fuzzy_min_helper(expr, &ret))
646 *undefined = 1;
647 break;
648 default:
649 *undefined = 1;
651 return ret;
654 static int get_const_value(struct expression *expr, sval_t *sval)
656 struct symbol *sym;
657 sval_t right;
659 if (expr->type != EXPR_SYMBOL || !expr->symbol)
660 return 0;
661 sym = expr->symbol;
662 if (!(sym->ctype.modifiers & MOD_CONST))
663 return 0;
664 if (get_value(sym->initializer, &right)) {
665 *sval = sval_cast(get_type(expr), right);
666 return 1;
668 return 0;
671 static sval_t handle_sizeof(struct expression *expr)
673 struct symbol *sym;
674 sval_t ret;
676 ret = sval_blank(expr);
677 sym = expr->cast_type;
678 if (!sym) {
679 sym = evaluate_expression(expr->cast_expression);
681 * Expressions of restricted types will possibly get
682 * promoted - check that here
684 if (is_restricted_type(sym)) {
685 if (sym->bit_size < bits_in_int)
686 sym = &int_ctype;
687 } else if (is_fouled_type(sym)) {
688 sym = &int_ctype;
691 examine_symbol_type(sym);
693 ret.type = size_t_ctype;
694 if (sym->bit_size <= 0) /* sizeof(void) */
695 ret.value = 1;
696 else
697 ret.value = bits_to_bytes(sym->bit_size);
699 return ret;
702 static sval_t _get_value(struct expression *expr, int *undefined, int implied)
704 sval_t ret;
706 if (!expr) {
707 *undefined = 1;
708 return bogus;
710 if (*undefined)
711 return bogus;
713 expr = strip_parens(expr);
715 switch (expr->type) {
716 case EXPR_VALUE:
717 ret = sval_from_val(expr, expr->value);
718 break;
719 case EXPR_PREOP:
720 ret = handle_preop(expr, undefined, implied);
721 break;
722 case EXPR_POSTOP:
723 ret = _get_value(expr->unop, undefined, implied);
724 break;
725 case EXPR_CAST:
726 case EXPR_FORCE_CAST:
727 case EXPR_IMPLIED_CAST:
728 ret = _get_value(expr->cast_expression, undefined, implied);
729 ret = sval_cast(get_type(expr), ret);
730 break;
731 case EXPR_BINOP:
732 ret = handle_binop(expr, undefined, implied);
733 break;
734 case EXPR_COMPARE:
735 ret = handle_comparison(expr, undefined, implied);
736 break;
737 case EXPR_LOGICAL:
738 ret = handle_logical(expr, undefined, implied);
739 break;
740 case EXPR_PTRSIZEOF:
741 case EXPR_SIZEOF:
742 ret = handle_sizeof(expr);
743 break;
744 case EXPR_SYMBOL:
745 if (get_const_value(expr, &ret)) {
746 break;
748 ret = _get_implied_value(expr, undefined, implied);
749 break;
750 case EXPR_SELECT:
751 case EXPR_CONDITIONAL:
752 ret = handle_conditional(expr, undefined, implied);
753 break;
754 default:
755 ret = _get_implied_value(expr, undefined, implied);
757 if (*undefined)
758 return bogus;
759 return ret;
762 /* returns 1 if it can get a value literal or else returns 0 */
763 int get_value(struct expression *expr, sval_t *sval)
765 int undefined = 0;
766 sval_t ret;
768 ret = _get_value(expr, &undefined, NOTIMPLIED);
769 if (undefined)
770 return 0;
771 *sval = ret;
772 return 1;
775 int get_implied_value(struct expression *expr, sval_t *sval)
777 int undefined = 0;
778 sval_t ret;
780 ret = _get_value(expr, &undefined, IMPLIED);
781 if (undefined)
782 return 0;
783 *sval = ret;
784 return 1;
787 int get_implied_min(struct expression *expr, sval_t *sval)
789 int undefined = 0;
790 sval_t ret;
792 ret = _get_value(expr, &undefined, IMPLIED_MIN);
793 if (undefined)
794 return 0;
795 *sval = ret;
796 return 1;
799 int get_implied_max(struct expression *expr, sval_t *sval)
801 int undefined = 0;
802 sval_t ret;
804 ret = _get_value(expr, &undefined, IMPLIED_MAX);
805 if (undefined)
806 return 0;
807 *sval = ret;
808 return 1;
811 int get_implied_rl(struct expression *expr, struct range_list **rl)
813 sval_t sval;
814 struct smatch_state *state;
815 sval_t min, max;
816 int min_known = 0;
817 int max_known = 0;
819 *rl = NULL;
821 expr = strip_parens(expr);
822 if (!expr)
823 return 0;
825 state = get_state_expr(SMATCH_EXTRA, expr);
826 if (state) {
827 *rl = clone_rl(estate_rl(state));
828 return 1;
831 if (expr->type == EXPR_CALL) {
832 if (get_implied_return(expr, rl))
833 return 1;
834 *rl = db_return_vals(expr);
835 if (*rl)
836 return 1;
837 return 0;
840 if (get_implied_value(expr, &sval)) {
841 add_range(rl, sval, sval);
842 return 1;
845 if (get_local_rl(expr, rl))
846 return 1;
848 min_known = get_implied_min(expr, &min);
849 max_known = get_implied_max(expr, &max);
850 if (!min_known && !max_known)
851 return 0;
852 if (!min_known)
853 get_absolute_min(expr, &min);
854 if (!max_known)
855 get_absolute_max(expr, &max);
857 *rl = alloc_rl(min, max);
858 return 1;
861 int get_hard_max(struct expression *expr, sval_t *sval)
863 int undefined = 0;
864 sval_t ret;
866 ret = _get_value(expr, &undefined, HARD_MAX);
867 if (undefined)
868 return 0;
869 *sval = ret;
870 return 1;
873 int get_fuzzy_min(struct expression *expr, sval_t *sval)
875 int undefined = 0;
876 sval_t ret;
878 ret = _get_value(expr, &undefined, FUZZY_MIN);
879 if (undefined)
880 return 0;
881 *sval = ret;
882 return 1;
885 int get_fuzzy_max(struct expression *expr, sval_t *sval)
887 int undefined = 0;
888 sval_t ret;
890 ret = _get_value(expr, &undefined, FUZZY_MAX);
891 if (undefined)
892 return 0;
893 if (ret.uvalue > INT_MAX - 10000)
894 return 0;
895 *sval = ret;
896 return 1;
899 int get_absolute_min(struct expression *expr, sval_t *sval)
901 int undefined = 0;
902 struct symbol *type;
904 type = get_type(expr);
905 if (!type)
906 type = &llong_ctype; // FIXME: this is wrong but places assume get type can't fail.
907 *sval = _get_value(expr, &undefined, ABSOLUTE_MIN);
908 if (undefined) {
909 *sval = sval_type_min(type);
910 return 1;
913 if (sval_cmp(*sval, sval_type_min(type)) < 0)
914 *sval = sval_type_min(type);
915 return 1;
918 int get_absolute_max(struct expression *expr, sval_t *sval)
920 int undefined = 0;
921 struct symbol *type;
923 type = get_type(expr);
924 if (!type)
925 type = &llong_ctype;
926 *sval = _get_value(expr, &undefined, ABSOLUTE_MAX);
927 if (undefined) {
928 *sval = sval_type_max(type);
929 return 1;
932 if (sval_cmp(sval_type_max(type), *sval) < 0)
933 *sval = sval_type_max(type);
934 return 1;
937 int known_condition_true(struct expression *expr)
939 sval_t tmp;
941 if (!expr)
942 return 0;
944 if (get_value(expr, &tmp) && tmp.value)
945 return 1;
947 return 0;
950 int known_condition_false(struct expression *expr)
952 if (!expr)
953 return 0;
955 if (is_zero(expr))
956 return 1;
958 if (expr->type == EXPR_CALL) {
959 if (sym_name_is("__builtin_constant_p", expr->fn))
960 return 1;
962 return 0;
965 int implied_condition_true(struct expression *expr)
967 sval_t tmp;
969 if (!expr)
970 return 0;
972 if (known_condition_true(expr))
973 return 1;
974 if (get_implied_value(expr, &tmp) && tmp.value)
975 return 1;
977 if (expr->type == EXPR_POSTOP)
978 return implied_condition_true(expr->unop);
980 if (expr->type == EXPR_PREOP && expr->op == SPECIAL_DECREMENT)
981 return implied_not_equal(expr->unop, 1);
982 if (expr->type == EXPR_PREOP && expr->op == SPECIAL_INCREMENT)
983 return implied_not_equal(expr->unop, -1);
985 expr = strip_expr(expr);
986 switch (expr->type) {
987 case EXPR_COMPARE:
988 if (do_comparison(expr) == 1)
989 return 1;
990 break;
991 case EXPR_PREOP:
992 if (expr->op == '!') {
993 if (implied_condition_false(expr->unop))
994 return 1;
995 break;
997 break;
998 default:
999 if (implied_not_equal(expr, 0) == 1)
1000 return 1;
1001 break;
1003 return 0;
1006 int implied_condition_false(struct expression *expr)
1008 struct expression *tmp;
1009 sval_t sval;
1011 if (!expr)
1012 return 0;
1014 if (known_condition_false(expr))
1015 return 1;
1017 switch (expr->type) {
1018 case EXPR_COMPARE:
1019 if (do_comparison(expr) == 2)
1020 return 1;
1021 case EXPR_PREOP:
1022 if (expr->op == '!') {
1023 if (implied_condition_true(expr->unop))
1024 return 1;
1025 break;
1027 tmp = strip_expr(expr);
1028 if (tmp != expr)
1029 return implied_condition_false(tmp);
1030 break;
1031 default:
1032 if (get_implied_value(expr, &sval) && sval.value == 0)
1033 return 1;
1034 break;
1036 return 0;