-
Notifications
You must be signed in to change notification settings - Fork 273
Do not generate redundant if statements in assert expansions #974
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4fb0603
Make is_skip publicly available and use constant argument
tautschnig c5cde18
Do not generate redundant if statements in assert expansions
tautschnig d44bfd3
Also simplify assert structure found on alpine linux
tautschnig 4122a28
Test to demonstrate assert bug on alpine
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include <assert.h> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
int x = 5; | ||
assert(x == 5); | ||
|
||
return 0; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
CORE | ||
main.c | ||
--cover location | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
^\[main\.coverage\.1\] .* function main block 1: SATISFIED$ | ||
(1 of 1|3 of 3) covered \(100\.0%\)$ | ||
-- | ||
^warning: ignoring | ||
^CONVERSION ERROR$ | ||
^\[main\.coverage\..\] .* function main block .: FAILED$ | ||
-- | ||
On Windows/Visual Studio, "assert" does not introduce any branching. |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#include <assert.h> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
assert(0); | ||
return 0; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CORE | ||
main.c | ||
--dump-c | ||
^EXIT=10$ | ||
^SIGNAL=0$ | ||
-- | ||
^warning: ignoring | ||
^[[:space:]]*IF |
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 |
---|---|---|
|
@@ -26,13 +26,12 @@ Author: Daniel Kroening, [email protected] | |
|
||
#include "goto_convert_class.h" | ||
#include "destructor.h" | ||
#include "remove_skip.h" | ||
|
||
static bool is_empty(const goto_programt &goto_program) | ||
{ | ||
forall_goto_program_instructions(it, goto_program) | ||
if(!it->is_skip() || | ||
!it->labels.empty() || | ||
!it->code.is_nil()) | ||
if(!is_skip(goto_program, it)) | ||
return false; | ||
|
||
return true; | ||
|
@@ -1723,6 +1722,11 @@ void goto_convertt::generate_ifthenelse( | |
true_case.instructions.back().guard=boolean_negate(guard); | ||
dest.destructive_append(true_case); | ||
true_case.instructions.clear(); | ||
if( | ||
is_empty(false_case) || | ||
(is_size_one(false_case) && | ||
is_skip(false_case, false_case.instructions.begin()))) | ||
return; | ||
} | ||
|
||
// similarly, do guarded assertions directly | ||
|
@@ -1736,6 +1740,28 @@ void goto_convertt::generate_ifthenelse( | |
false_case.instructions.back().guard=guard; | ||
dest.destructive_append(false_case); | ||
false_case.instructions.clear(); | ||
if( | ||
is_empty(true_case) || | ||
(is_size_one(true_case) && | ||
is_skip(true_case, true_case.instructions.begin()))) | ||
return; | ||
} | ||
|
||
// a special case for C libraries that use | ||
// (void)((cond) || (assert(0),0)) | ||
if( | ||
is_empty(false_case) && true_case.instructions.size() == 2 && | ||
true_case.instructions.front().is_assert() && | ||
true_case.instructions.front().guard.is_false() && | ||
true_case.instructions.front().labels.empty() && | ||
true_case.instructions.back().labels.empty()) | ||
{ | ||
true_case.instructions.front().guard = boolean_negate(guard); | ||
true_case.instructions.erase(--true_case.instructions.end()); | ||
dest.destructive_append(true_case); | ||
true_case.instructions.clear(); | ||
|
||
return; | ||
} | ||
|
||
// Flip around if no 'true' case code. | ||
|
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 |
---|---|---|
|
@@ -12,7 +12,7 @@ Author: Daniel Kroening, [email protected] | |
#include "remove_skip.h" | ||
#include "goto_model.h" | ||
|
||
static bool is_skip(goto_programt::instructionst::iterator it) | ||
bool is_skip(const goto_programt &body, goto_programt::const_targett it) | ||
{ | ||
// we won't remove labelled statements | ||
// (think about error labels or the like) | ||
|
@@ -28,9 +28,12 @@ static bool is_skip(goto_programt::instructionst::iterator it) | |
if(it->guard.is_false()) | ||
return true; | ||
|
||
goto_programt::instructionst::iterator next_it=it; | ||
goto_programt::const_targett next_it = it; | ||
next_it++; | ||
|
||
if(next_it == body.instructions.end()) | ||
return false; | ||
|
||
// A branch to the next instruction is a skip | ||
// We also require the guard to be 'true' | ||
return it->guard.is_true() && | ||
|
@@ -92,7 +95,7 @@ void remove_skip(goto_programt &goto_program) | |
// for collecting labels | ||
std::list<irep_idt> labels; | ||
|
||
while(is_skip(it)) | ||
while(is_skip(goto_program, it)) | ||
{ | ||
// don't remove the last skip statement, | ||
// it could be a target | ||
|
@@ -144,9 +147,10 @@ void remove_skip(goto_programt &goto_program) | |
// remove the last skip statement unless it's a target | ||
goto_program.compute_incoming_edges(); | ||
|
||
if(!goto_program.instructions.empty() && | ||
is_skip(--goto_program.instructions.end()) && | ||
!goto_program.instructions.back().is_target()) | ||
if( | ||
!goto_program.instructions.empty() && | ||
is_skip(goto_program, --goto_program.instructions.end()) && | ||
!goto_program.instructions.back().is_target()) | ||
goto_program.instructions.pop_back(); | ||
} | ||
while(goto_program.instructions.size()<old_size); | ||
|
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 |
---|---|---|
|
@@ -16,6 +16,7 @@ Author: Daniel Kroening, [email protected] | |
|
||
class goto_modelt; | ||
|
||
bool is_skip(const goto_programt &, goto_programt::const_targett); | ||
void remove_skip(goto_programt &); | ||
void remove_skip(goto_functionst &); | ||
void remove_skip(goto_modelt &); | ||
|
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.
Is there any reason to restrict ourselves to two instructions here? Could we simply say "if(x) { assert(false); ... } --> assert(!x)", since the rest of the true-case is dead code? Also, why only consider the true case?
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.
It's only dead code if none of the instruction has a label that could be jumped to.