-
Notifications
You must be signed in to change notification settings - Fork 273
Simplify extractbits #2061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Simplify extractbits #2061
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,9 @@ Author: Daniel Kroening, [email protected] | |
#include "expr_util.h" | ||
#include "fixedbv.h" | ||
#include "ieee_float.h" | ||
#include "invariant.h" | ||
#include "namespace.h" | ||
#include "pointer_offset_size.h" | ||
#include "rational.h" | ||
#include "rational_tools.h" | ||
#include "std_expr.h" | ||
|
@@ -1101,48 +1103,76 @@ bool simplify_exprt::simplify_power(exprt &expr) | |
} | ||
|
||
/// Simplifies extracting of bits from a constant. | ||
bool simplify_exprt::simplify_extractbits(exprt &expr) | ||
bool simplify_exprt::simplify_extractbits(extractbits_exprt &expr) | ||
{ | ||
assert(expr.operands().size()==3); | ||
|
||
const typet &op0_type=expr.op0().type(); | ||
const typet &op0_type = expr.src().type(); | ||
|
||
if(!is_bitvector_type(op0_type) && | ||
!is_bitvector_type(expr.type())) | ||
return true; | ||
|
||
if(expr.op0().is_constant()) | ||
{ | ||
std::size_t width=to_bitvector_type(op0_type).get_width(); | ||
mp_integer start, end; | ||
mp_integer start, end; | ||
|
||
if(to_integer(expr.op1(), start)) | ||
return true; | ||
if(to_integer(expr.upper(), start)) | ||
return true; | ||
|
||
if(to_integer(expr.op2(), end)) | ||
return true; | ||
if(to_integer(expr.lower(), end)) | ||
return true; | ||
|
||
if(start<0 || start>=width || | ||
end<0 || end>=width) | ||
return true; | ||
const mp_integer width = pointer_offset_bits(op0_type, ns); | ||
|
||
if(start < 0 || start >= width || end < 0 || end >= width) | ||
return true; | ||
|
||
assert(start>=end); // is this always the case?? | ||
DATA_INVARIANT( | ||
start >= end, | ||
"extractbits must have upper() >= lower()"); | ||
|
||
const irep_idt &value=expr.op0().get(ID_value); | ||
if(expr.src().is_constant()) | ||
{ | ||
const irep_idt &value = to_constant_expr(expr.src()).get_value(); | ||
|
||
if(value.size()!=width) | ||
return true; | ||
|
||
std::string svalue=id2string(value); | ||
|
||
std::string extracted_value= | ||
svalue.substr(width-integer2size_t(start)-1, | ||
integer2size_t(start-end+1)); | ||
std::string extracted_value = | ||
svalue.substr( | ||
integer2size_t(width - start - 1), | ||
integer2size_t(start - end + 1)); | ||
|
||
expr = constant_exprt(extracted_value, expr.type()); | ||
constant_exprt result(extracted_value, expr.type()); | ||
expr.swap(result); | ||
|
||
return false; | ||
} | ||
else if(expr.src().id() == ID_concatenation) | ||
{ | ||
// the most-significant bit comes first in an concatenation_exprt, hence we | ||
// count down | ||
mp_integer offset = width; | ||
|
||
forall_operands(it, expr.src()) | ||
{ | ||
mp_integer op_width = pointer_offset_bits(it->type(), ns); | ||
|
||
if(op_width <= 0) | ||
return true; | ||
|
||
if(start + 1 == offset && end + op_width == offset) | ||
{ | ||
exprt tmp = *it; | ||
if(tmp.type() != expr.type()) | ||
return true; | ||
|
||
expr.swap(tmp); | ||
return false; | ||
} | ||
|
||
offset -= op_width; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't follow, why are we counting down from
width
instead of up from zero as you'd usually expect?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's because
concatenation_exprt
has the most-significant bits first. I've (just now) added a comment to the code.