Decompiler: Simplify comparisons between INT_OR
and zero.
#6578
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.
At optimisation level
-O1
, gcc combines several values that all need to be compared against zero by combining them usingINT_OR
and only comparing the combined result against zero. With this rule, the decompiler is able to break theseINT_OR
chains apart and simplify the individual links.Example
As an example, let's compile the below source code with
gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
(or follow along on godbolt). Make sure to pass-O1
.If using godbolt, you can notice that an
or
instruction is used and only one jump instruction. If you're compiling locally, you can load the resulting binary in ghidra and let auto analysis run. I tested with Github release 11.0.3. The decompiler gives the following output:Here, you can also see the binary or operator (
|
) being used. Together with the!= 0
condition, it's not obvious at first glance that this tests if eithera
orb
is nonzero. Additionally, in this casea
andb
are variables that cannot be simplified further. The expressions being or-red together might be more complex, and this structure hinders further simplification.We can test this PR by compiling
decomp_dbg
and using this xml that I generated using the "Debug function decompilation" menu item. Then, we can use the command line interfacedecomp_dbg
to see what the decompiled code would look like with this patch: