Skip to content

Commit 2163851

Browse files
committed
Typing a refactoring of simplify_extractbits
1 parent da63652 commit 2163851

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

src/util/simplify_expr.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2317,7 +2317,7 @@ bool simplify_exprt::simplify_node(exprt &expr)
23172317
else if(expr.id()==ID_concatenation)
23182318
result=simplify_concatenation(expr) && result;
23192319
else if(expr.id()==ID_extractbits)
2320-
result=simplify_extractbits(expr) && result;
2320+
result = simplify_extractbits(to_extractbits_expr(expr)) && result;
23212321
else if(expr.id()==ID_ieee_float_equal ||
23222322
expr.id()==ID_ieee_float_notequal)
23232323
result=simplify_ieee_float_relation(expr) && result;

src/util/simplify_expr_class.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class bswap_exprt;
2525
class byte_extract_exprt;
2626
class byte_update_exprt;
2727
class exprt;
28+
class extractbits_exprt;
2829
class if_exprt;
2930
class index_exprt;
3031
class member_exprt;
@@ -63,7 +64,7 @@ class simplify_exprt
6364

6465
bool simplify_typecast(exprt &expr);
6566
bool simplify_extractbit(exprt &expr);
66-
bool simplify_extractbits(exprt &expr);
67+
bool simplify_extractbits(extractbits_exprt &expr);
6768
bool simplify_concatenation(exprt &expr);
6869
bool simplify_mult(exprt &expr);
6970
bool simplify_div(exprt &expr);

src/util/simplify_expr_int.cpp

+18-18
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Author: Daniel Kroening, [email protected]
1818
#include "expr_util.h"
1919
#include "fixedbv.h"
2020
#include "ieee_float.h"
21+
#include "invariant.h"
2122
#include "namespace.h"
2223
#include "rational.h"
2324
#include "rational_tools.h"
@@ -1101,34 +1102,32 @@ bool simplify_exprt::simplify_power(exprt &expr)
11011102
}
11021103

11031104
/// Simplifies extracting of bits from a constant.
1104-
bool simplify_exprt::simplify_extractbits(exprt &expr)
1105+
bool simplify_exprt::simplify_extractbits(extractbits_exprt &expr)
11051106
{
1106-
assert(expr.operands().size()==3);
1107-
1108-
const typet &op0_type=expr.op0().type();
1107+
const typet &op0_type = expr.src().type();
11091108

11101109
if(!is_bitvector_type(op0_type) &&
11111110
!is_bitvector_type(expr.type()))
11121111
return true;
11131112

1114-
if(expr.op0().is_constant())
1115-
{
1116-
std::size_t width=to_bitvector_type(op0_type).get_width();
1117-
mp_integer start, end;
1113+
mp_integer start, end;
11181114

1119-
if(to_integer(expr.op1(), start))
1120-
return true;
1115+
if(to_integer(expr.upper(), start))
1116+
return true;
11211117

1122-
if(to_integer(expr.op2(), end))
1123-
return true;
1118+
if(to_integer(expr.lower(), end))
1119+
return true;
11241120

1125-
if(start<0 || start>=width ||
1126-
end<0 || end>=width)
1127-
return true;
1121+
const std::size_t width = to_bitvector_type(op0_type).get_width();
11281122

1129-
assert(start>=end); // is this always the case??
1123+
if(start < 0 || start >= width || end < 0 || end >= width)
1124+
return true;
11301125

1131-
const irep_idt &value=expr.op0().get(ID_value);
1126+
PRECONDITION(start >= end);
1127+
1128+
if(expr.src().is_constant())
1129+
{
1130+
const irep_idt &value = to_constant_expr(expr.src()).get_value();
11321131

11331132
if(value.size()!=width)
11341133
return true;
@@ -1139,7 +1138,8 @@ bool simplify_exprt::simplify_extractbits(exprt &expr)
11391138
svalue.substr(width-integer2size_t(start)-1,
11401139
integer2size_t(start-end+1));
11411140

1142-
expr = constant_exprt(extracted_value, expr.type());
1141+
constant_exprt result(extracted_value, expr.type());
1142+
expr.swap(result);
11431143

11441144
return false;
11451145
}

0 commit comments

Comments
 (0)