Commit Graph

159 Commits

Author SHA1 Message Date
Pietro Fezzardi 7caa5eda96 PeepholeOptimizationPass can reorder instructions 2024-06-06 10:43:17 +02:00
Lauri Vasama cccee1ed99 Add structured Clift type alias generation 2024-06-05 15:07:23 +02:00
Lauri Vasama bfe353d1f6 Improve Clift type and type attribute verification 2024-06-05 15:07:23 +02:00
Lauri Vasama 34d488a086 Add using directives for mlir::clift 2024-06-05 15:07:23 +02:00
Lauri Vasama 4ebe4fb140 Rename remaining Clift type definition attributes 2024-06-05 15:07:23 +02:00
Lauri Vasama a310080fbd Rename Clift StructType and UnionType attributes 2024-06-05 15:07:23 +02:00
Pietro Fezzardi 677155d436 TANP: fix off-by-one in wrapping ICmp reduction
This bug caused the expression x + (-2) < -3 to be rewritten as
(x > 2) || (x < -1), instead of (x > 2) && (x < -1).

We had tests for a very similar case, but this specific example was
triggered by having the two constant terms in the inequality with a
difference of only one.

This commit rewrites and unifies the logic for handling inequalities,
and adapts the test for checking this specific additional corner case.
2024-06-04 00:10:27 +02:00
Lauri Vasama 4a21b8a09b Add LIT/FileCheck tests for model type import 2024-05-29 15:43:35 +02:00
Lauri Vasama 817b2e1a65 Set revng prefix in MLIR LIT tests 2024-05-29 15:43:35 +02:00
Lauri Vasama 7abadd08ae Add model types to Clift MLIR import 2024-05-29 15:43:35 +02:00
Lauri Vasama 25c4710c9a Fix EnumAttr::verify
* Fix enumerator value strict ordering check.
* Remove enumerator name uniqueness check.
2024-05-29 15:43:35 +02:00
Pietro Fezzardi c48908c6a6 Revamp decompilation tests
Many old unit tests were too rigid, using the model and the LLVM IR.
This commit drops them, and replaces them with decompilation tests based
on revng-qa, relying on `revng model compare` to test model properties,
and on `FileCheck` to test that we emit specific constructs in C.

The new tests cover various features of the decompiler.
* DLA capability to recover complex data structure like linked-lists and
  arrays.
* DLA capability to update segment and section types, so that we emit
  nice looking accesses to segments in C.
* Capability to emit nice looking integer literals in C
* Capability to emit inline string literals in C and update the model
  types of the segments containg such string literals.
2024-03-19 09:44:00 +01:00
Pietro Fezzardi 132adeecd5 Add PeepholeOptimization pass
This pass is dedicated to applying a bunch of peephole optimizations
that are useful for decompilation and haven't been implemented in LLVM
for whatever reason.

At the moment it only runs an optimization that tries to reduce the uses
of incoming values of PHINodes, under very specific condidtions, which
enables emitting less LocalVariables.
2024-03-19 09:44:00 +01:00
Pietro Fezzardi 70c8ae48ff Retire MarkAssignment for SwitchToStatements 2024-03-19 09:44:00 +01:00
Alessandro Di Federico 0c212b66d9 Relicense to MIT 2024-02-29 17:03:36 +01:00
Ivan Krysak 6adee367ee RemoveExtraValues: simplify the pass
Simplification includes dropping dependencies onto
`FunctionMetadataCachePass` and `LoadModelWrapperPass`, as well
as removing "StructName" logic outright.
2024-02-20 14:35:56 +01:00
Ivan Krysak 5da851fcd5 lit-tests: remove unnecessary isolated tag 2024-02-20 14:35:56 +01:00
Ivan Krysak 4d97044d15 unit-tests: include helpers 2024-02-09 16:24:47 +01:00
Alessandro Di Federico e718f9aa76 Standardize unit test names 2024-02-09 09:04:28 +01:00
Alessandro Di Federico f6f542769b Convert all public strings to kebab-case 2024-02-09 09:04:25 +01:00
Alessandro Di Federico 7a2e315e29 s/IRCanonicalization/Canonicalize/g 2024-02-09 09:03:34 +01:00
Pietro Fezzardi fdf1720433 Drop ValueManipulationAnalysis (VMA)
This commit drops ValueManipulationAnalysis, which in its original
design based on MinCut and Karger, was never enabled in the
decompilation pipeline.

Until now, VMA was only used in a severely weakened form in
initModelTypes. That for was so weakened that it barely did anything.

We already have a new design for VMA so that it can work before
DataLayoutAnalysis, and on Clift.
At this point, the old VMA is basically useless anyway, and the very few
occasions where it can do something will simply be solved by the
upcoming work on making some of the remaining casts implicit.

At this point it does not make sense to keep VMA alive anymore.
2024-02-07 00:41:30 +01:00
Massimo Fioravanti 5259cc83fd Add types lit tests 2024-02-04 16:49:51 +01:00
Massimo Fioravanti ab856523dd Introduce clift types 2024-02-04 16:49:51 +01:00
Massimo Fioravanti 1434948e7e Add clift unit tests 2024-02-04 16:49:51 +01:00
Andrea Gussoni d940c03e9f clift: use shipped lit for tests infrastructure 2024-02-04 16:49:51 +01:00
Pietro Fezzardi 3e49213b43 Drop needsTopScopeDeclaration hack
This commit drops `needsTopScopeDeclaration`.
Now all the LocalVariables are declared at the top of the function.
In DecompileFunction.cpp, the emission of LocalVariables if handled at
the scope of GHAST `ASTNode`s instead of being LLVM BasicBlock-based.
This enables in the future to design and implement an analysis that for
each LocalVariable decides the C scope (represented by an `ASTNode`)
where it's declaration should be emitted to always be visible in all its
uses.
2023-11-28 15:46:19 +01:00
Ivan Krysak fa7bf17dae IRCanonicalization: introduce ternary-reduction 2023-11-21 17:05:18 +01:00
Pietro Fezzardi 48a708c07c Support printing NULL in decompiled C code 2023-10-19 16:18:41 +02:00
Pietro Fezzardi 08939f2ae8 OpaqueExtractValues: set name for struct type
This commit sets a name for types we use in OpaqueExtractValues.
These are StructTypes that can only be returned from isolated functions
with RawFunctionType prototype on the model, or from helpers that do not
represent isolated functions.

The name is required because when we will converto to MLIR LLVM Dialect,
there are checks in place that forbid unnamed types.
The code re-uses code that is used to print C code, in order to make
sure that the struct names we use for this are the same as those we emit
in C, to ease debugging.

This commit also cleans up the unittest to make it more concise, while
still testing the same things.
2023-10-02 17:13:55 +02:00
Alessandro Di Federico 30c127267c Improve ModelToHeader and HeaderToModel tests
* Fix non-PrimitiveTypes with low IDs.
* Ensure we use `revng model compare` to test the model.
* Updates the name to reflect the recent changes in naming convention
  and in the way we represent `model::Type::ID`s.
2023-08-23 16:37:39 +02:00
Alessandro Di Federico f406e978e3 Adopt progressive model::Type::IDs 2023-08-23 16:37:39 +02:00
Ivan Krysak 3c584cfabc Formatting: set AllowShortEnumsOnASingleLine
The new value is `false`.
2023-07-05 06:05:04 +00:00
Pietro Fezzardi 01a985632f OperatorPrecedence: support OpaqueExtractValue 2023-06-30 10:48:59 +02:00
Pietro Fezzardi c3d7125dd5 Drop old unused RestoreExtractValuesPass
Now we don't restore the extractvalue instructions anymore, so this
whole pass is dead code.
2023-06-30 10:48:59 +02:00
Valentina Sona 866b9965fa Add test for the boolean not TANP transformation 2023-06-12 14:17:34 +02:00
Pietro Fezzardi f4020c9fa6 TNAP: fix handling of wraparounds
Before this commit, the TwosComplementArithmeticNormalizationPass wasn't
taking into consideration properly all the cases where the arithmetic
can wrap around in case of expression such as (x + const1 <=> const2) or
(x - const1 <=> const 2).

This caused 2 different classes of problems that affected semantics.
1) In some cases we were emitting comparisons that were too broad,
   meaning that they were true in a larger set of cases than the
   original one.
2) In other cases we were emitting tautological comparisons, i.e.
   comparisons that were always demonstrably true or false at compile
   time.

This commit fixes the problem, treating all the cases in a generalized
unique way that works correctly for every case.
2023-06-09 09:24:52 +02:00
Valentina Sona 0e61bf3fd2 TANP: Move constants to right side of comparisons
Implements transforms when the results of sub or add with a constant
operand are compared against constant:
1) x + a == b --> x == b - a
2) x + a != b --> x != b - a
3) x + a <= b --> x <= b - a
4) x + a < b --> x < b - a
5) x + a >= b --> x >= b - a
6) x + a > b --> x > b - a

After the transformation, the right hand side is also enqued for
analysis to detect opporunities to use unary minus if a-b is a negative
constant.
2023-05-12 15:21:22 +02:00
Giacomo Vercesi a4ad571e61 rcc: Fix typos
Fix the typos detected by `codespell`
2023-05-11 10:04:32 +02:00
Alessandro Di Federico a823db7ea7 Adopt revng::verify 2023-04-28 13:54:12 +02:00
Pietro Fezzardi 3886b882da MarkAssignments: drop check on many uses
This commit changes MarkAssignments (and related tests) so that now
instruction with more than one use are not assigned to a new variable
by default.

In some cases it might still be good to serialize them for readability,
but for those cases we will implementa a standalone pass that only
implements heuristics based on readability.

MarkAssignments now only takes decisions based on semantics of
side-effects.
2023-04-20 17:51:16 +02:00
Pietro Fezzardi 56db9e6660 Make getExpectedModelType more strict on integers
This commit teaches getExpectedModelType about the fact that various
bitwise operations are only allowed to have integer operands.

It also updates VMA, which uses getExpectedModelType, to take this
into account.
2023-04-14 14:54:14 +02:00
Alessandro Di Federico ffee4c22f0 Adapt tests to LLVM 16 2023-04-14 14:54:14 +02:00
Pietro Fezzardi 6776f1349b MarkAssignmentsTest: drop useless allocas 2023-04-06 12:04:57 +02:00
Pietro Fezzardi d7cfb96331 Improve hasSideEffects for llvm Intrinsics
Now a call to an llvm Intrinsic is detected has having side effects only
if it is not ReadNone nor ReadOnly.
2023-04-04 09:33:22 +02:00
Alessandro Di Federico c18dca0031 Adopt new approach for MetaAddress serialization 2023-02-21 16:48:35 +01:00
Kacper Kołodziej 568bd24900 Add UnaryMinus and BinaryNot 2023-01-09 15:01:16 +01:00
Pietro Fezzardi e9d84264ff Reduce the number of emitted local variables in C
This commit does various things oriented at reducing the number of local
variables emitted in C:
- MarkAssignments now know that @Copy and @Assign involving
  @LocalVariable only have side effects that affect the local variable
  itself; this enables to reduce the number of times we're forced to
  emit a local variable due to interfering side effects
- Drop the @AssignmentMarker FunctionTag; AddAssignmentMarkerPass now
  doesn't emit @AssignmentMarker anymore; instead it emits groups of
  @LocalVariable, @Copy, and @Assign, which benefit from the previous
  point
- Drop 2 MarkAssignments::Reasons: HasManyUses and HasUsesOutsideOfBB;
  both these have now been aggregated into the AlwaysAssign reason for
  simplicity, representing all reasons non involving side effects
- Update BeautifyGHAST and how it reasons about side effects when
  beautifying; before this commit it used @AssignmentMarker, now it
  looks at @Assign
- Simplify ExitSSA; before this commit it was trying hard to be smart on
  where it emitted the store instructions representing the incoming
  values of the PHI that was being destroyed; this seemed smart when we
  originally did it but it generated C code that was not really better
  to read, so this useless complexity is finally gone
2022-12-24 02:47:10 +01:00
Kacper Kołodziej 83ab07d7fb Enable printing integers as bool, hex or char
This commit adds a new pass, PrettyIntFormatting, that injects calls to
decorator functions print_hex, print_char, and print_bool around
llvm::ConstantInt in various situations.

It also updates the rest of passes of the decompilation pipelin to
understand these new decorator functions and to properly emit decorated
integer literals in the decompiled C code.
2022-12-02 10:08:00 +01:00
Massimo Fioravanti 335d402245 Change signatures to forward metadata cache. 2022-11-22 12:27:02 +01:00