2 * Copyright (C) 2012 Oracle.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
23 static unsigned long long find_possible_bits(struct expression
*expr
)
26 unsigned long long ret
;
30 expr
= strip_expr(expr
);
32 if (get_implied_value(expr
, &sval
))
35 if (expr
->type
== EXPR_BINOP
&& (expr
->op
== '&' || expr
->op
== '|')) {
36 unsigned long long left
, right
;
38 left
= find_possible_bits(expr
->left
);
41 right
= find_possible_bits(expr
->right
);
50 get_absolute_max(expr
, &sval
);
54 for (i
= 63; i
>= 0; i
--) {
64 static unsigned long long get_possible_bits(struct expression
*expr
)
68 expr
= strip_expr(expr
);
69 if (expr
->type
!= EXPR_BINOP
)
73 if (!get_implied_value(expr
->right
, &sval
))
79 static void match_condition(struct expression
*expr
)
83 unsigned long long left_mask
, right_mask
;
86 type
= get_type(expr
);
90 if (expr
->type
!= EXPR_COMPARE
)
92 if (expr
->op
!= SPECIAL_EQUAL
&& expr
->op
!= SPECIAL_NOTEQUAL
)
95 if (!get_value(expr
->right
, &sval
))
97 right_mask
= sval
.uvalue
;
99 left_mask
= get_possible_bits(expr
->left
);
103 if (type_bits(type
) < 64) {
104 left_mask
&= (1ULL << type_bits(type
)) - 1;
105 right_mask
&= (1ULL << type_bits(type
)) - 1;
108 if ((left_mask
& right_mask
) == right_mask
)
111 str
= expr_to_str(expr
);
112 sm_msg("warn: masked condition '%s' is always %s.", str
,
113 expr
->op
== SPECIAL_EQUAL
? "false" : "true");
117 void check_impossible_mask(int id
)
121 add_hook(&match_condition
, CONDITION_HOOK
);