Commit Graph

1201 Commits

Author SHA1 Message Date
Andrea Gussoni deee899ad0 RestructureCFG: improve first iteration outline
Improve the logic of the first iteration outline:
1) The cloning of the nodes involved in the first iteration outlining,
   is now performed with a series of DFS visits from all the late entry
   nodes in the `MetaRegion`, instead of performing a blind clone of all
   the nodes and removing those not needed.
2) Restoring the edges between the cloned nodes has been subject to
   minor changes.
3) The outline nodes are now assigned to a `MetaRegion` using a new
   logic, instead of blindly assigning them to the parent `MetaRegion`.
2023-10-09 10:34:41 +02:00
Andrea Gussoni 496f0becd7 RestructureCFG: remove old Set node handling
Remove old broken code that handled `Set` nodes as source of retreatings
when inserting an `entry` dispatcher. This situation should no longer be
the case, because a `Set` node should not be the `source` of a
retreating edge. We assert this situation.
2023-10-09 10:34:41 +02:00
Andrea Gussoni 4161834bc5 RestructureCFG: improve handling of retreatings
We now explicitly store the retreatings edges, that will eventually be
connected to the `continue` nodes, so that we can explicitly handle
them, without resorting to iterating over the predecessors of the
`EntryNode`, which although correct is a fragile method with respect to
defective situations.

In addition, we check that after the restructuring, all the retreating
edges that we connect to `continue` nodes, do point to the `EntryNode`
before the transformation.
2023-10-09 10:34:41 +02:00
Andrea Gussoni 935dcdbdec RestructureCFG: improve ShortestPath
Add a dedicated Logger for the `ShortestPath` computation.
2023-10-09 10:34:41 +02:00
Andrea Gussoni 6d4f3daf30 RestructureCFG: improve code layout
Improve the code layout and the logging of the `Backedge`s insertion.
Add the `debug_function` attribute to the `LogMetaRegions` function.
2023-10-09 10:34:41 +02:00
Pietro Fezzardi 4cda5259e5 RestructureCFG: improve inclusion dominated nodes
Before this commit, the code was using an old arcane algorithm that was
adding and removing dummy frontier nodes, and relying on the
DominatorTree.

This commit drops the DominatorTree altogether, drops the use of
frontier nodes, and rewrites the algorithm only using successors and
predecessors.

Now a node outside a metaregion M is included in the metaregion only if
all its predecessors are part of the metaregion, and none of them is
already in another metaregion that is a transitive parent of M.
2023-10-09 10:34:41 +02:00
Pietro Fezzardi 1ed19a35e2 RestructureCFG: try skip first iteration outlining
Before this commit, the whole body of each loop was outlined and
duplicated, only to be removed later if not necessary.
Now we only do this if there is more than one entry to the loop, which
is the simplest condition to detect when first iteration outlining is
needed.
2023-10-09 10:34:41 +02:00
Pietro Fezzardi b8810858c8 RestructureCFG: drop RootMetaRegion placeholder 2023-10-09 10:34:41 +02:00
Pietro Fezzardi 68fca82f46 RestructureCFG: call updateNodes more efficiently
Before this commit it was called on all metaregions, now only on parent
metaregions.
2023-10-09 10:34:41 +02:00
Pietro Fezzardi 3cfad9f3f9 MetaRegion::updateNodes: simplify and add assert 2023-10-09 10:34:41 +02:00
Alessandro Di Federico 4fe4d05895 CCodeGenerator::ParentPrototype: drop Parent 2023-10-06 17:14:29 +02:00
Alessandro Di Federico 2a7e06df1f printFunctionPrototype: add newline after ABI
This commit forces getLocationAttribute's argument to be false.
2023-10-06 17:14:29 +02:00
Alessandro Di Federico 6141717440 CCodeGenerator: emit comment after noreturn calls 2023-10-06 17:14:27 +02:00
Alessandro Di Federico 9f3b832009 CCodeGenerator: add space in struct initializers 2023-10-05 19:18:38 +02:00
Alessandro Di Federico cb8aa3d929 Adopt .prototype()
Also adopt `QualifiedType::getFunctionType` where appropriate in order
to unwrap typedefs.
2023-10-05 19:18:38 +02:00
Alessandro Di Federico cae213b36b Minor changes 2023-10-05 18:45:17 +02:00
Djordje Todorovic 68400d9b30 Import from LLVM IR to MLIR
- Add PrepareLLVMIRForMLIR Pass.
  This Pass performs massaging of the LLVM IR
  produced by revng so it can be parsed/accepted
  for the LLVM MLIR Dialect.
- Introduce new pipeline branch: llvmir-to-mlir.yml.
- Add LLVMIRToMLIR step.
- Add ImportLLVMToMLIRPipe pipe.
2023-10-02 17:13:55 +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
Pietro Fezzardi 8df32f84cc BeautifyGHAST: drop broken code in hasSideEffects
This commit drops some old broken code that was a leftover from when
MarkAssignments didn't inject calls to Assign, and that was poorly
migrated to Assign, causing it to be dead code.
2023-10-02 17:10:15 +02:00
Pietro Fezzardi bd832cb140 MarkAssignments: use model types for LocalVariable
Before this commit all the local variabels emitted by MarkAssignments
were using integer types obtained with llvmIntToModelType.

This commit enables using model::QualifiedTypes more often, resulting in
overall more readable C code.
2023-10-02 17:10:14 +02:00
Pietro Fezzardi f72215485d ExitSSA: ignore undef incoming values of PHI 2023-09-27 16:48:22 +02:00
Pietro Fezzardi dd76d3bef9 BeautifyGHAST: fix hasSideEffects for NotNode
This commit fixes a bug in the hasSideEffects function, used to evaluate
if an ExprNode has side effects.
Before this commit, when calling hasSideEffects on a NotNode, the
return value was true if the operand of NotNode **did not** have side
effects, which is wrong.
This commit changes the behaviour so that a NotNode has side effects if
and only if its only operand has side effects.
2023-09-27 16:48:22 +02:00
Pietro Fezzardi 398f0888d0 BeautifyGHAST: fix hasSideEffects for AndNode
This commit fixes a bug in the hasSideEffects function, used to evaluate
if an ExprNode has side effects.
Before this commit, when calling hasSideEffects on an AndNode, the
return value was true if **both** the LHS and RHS of the AndNode had
side effects, which is wrong.
This commit changes the behaviour so that if **either** LHS **or** the
RHS of the And have any side effect, than hasSideEffects returns true
for the And.
2023-09-27 16:48:19 +02:00
Pietro Fezzardi ce6935bec7 BeautifyGHAST: simplifyShortCircuit recurs on if
Before this commit, the simplifyShortCircuit beautifier did not recur on
the then/else branch of an IfNode, basically stopping recursion very
early and potentially missing a lot of beautification opportunities.
2023-09-27 16:48:10 +02:00
Pietro Fezzardi 7df0aa0dd5 Add readonly memory effect to OpaqueExtractValue
This enables DCE to remove unused calls to OpaqueExtractValue,
also preventing them to show up in decompiled C code.
2023-09-27 16:48:03 +02:00
Pietro Fezzardi 07cf95159e DLA Frontend: fix handling of pointer-typed SCEVs
Before this commit the DLA frontend was not ready to handle all the
possible combinations of integer- and pointer-typed arithmetic that was
used to compute SCEVs.
This could cause crashes when dealing with pointer-typed SCEVs.
This commit fixes the issues by converting all pointer-typed expressions
to integers, and resizing integers to the same size before adding them.
2023-09-21 15:18:32 +02:00
Pietro Fezzardi ca2f26e40a DLA: don't dump .dot files with VerifyLog 2023-09-21 15:18:23 +02:00
Pietro Fezzardi 8d9f07d7a7 Change indentation of all decompiled C code
This commit changes the indentation of all the C code we emit, from 4
whitespaces to only 2 whitespaces.
2023-09-21 15:17:44 +02:00
Pietro Fezzardi 267e06c052 DLA: add caching to SimplifyInstanceAtOffset0
This commit adds caching for mutual reachability among children of a
node, in SimplifyInstanceAtOffset0.
This makes the DLAStep 10% to 50% faster on real-world benchmarks we
have measured, such as `updatedb.plocate` and `df` for Ubuntu 22.04
x86_64.
2023-09-20 16:36:48 +02:00
Pietro Fezzardi 990f8aab03 Drop unused set from DLATypeSystem.cpp 2023-09-20 16:36:34 +02:00
Pietro Fezzardi 9e36c02442 MarkAssignments: drop unused map 2023-09-20 16:36:28 +02:00
Pietro Fezzardi 01f338be29 Add progress bars to DecompileFunction.cpp 2023-09-20 16:36:21 +02:00
Pietro Fezzardi c04b96f72a DLA: RemoveBackedges.cpp uses EquivalenceClasses
llvm::EquivalenceClasses is an efficient data structure from LLVM, to
compute equivalence classes among objects with Tarjan's union-find.

This commit uses that to avoid an hand-crafted very inefficient
algorithm.
2023-09-07 12:19:35 +02:00
Pietro Fezzardi b8c48be350 Add progress bars to DLA 2023-09-07 12:19:35 +02:00
Giacomo Vercesi 9223ada54f ImportModelFromCAnalysis: use TemporaryFile
Remove the usage of `createUniqueDirectory` that does not run cleanup on
crash/shutdown and instead use `TemporaryFile`.
2023-08-24 10:30:27 +02:00
Alessandro Di Federico f3d6bbb047 s/ThePTMLCBuilder/B/g 2023-08-23 16:40:54 +02:00
Alessandro Di Federico cc126f63b9 Adopt model::PrimitiveType::fromName 2023-08-23 16:37:39 +02:00
Alessandro Di Federico 4a9329ebdd s/getIncommingTypesForT/getIncomingTypesFor/ 2023-08-23 16:37:39 +02:00
Alessandro Di Federico 17bf31526a Drop Revng prefixes 2023-08-23 16:37:39 +02:00
Alessandro Di Federico eaed9c7302 HeaderToModel: s/DILogger/Log/ 2023-08-23 16:37:39 +02:00
Alessandro Di Federico 4e1fed5fa0 ModelToHeader: omit _enum_max_value_* when editing 2023-08-23 16:37:39 +02:00
Alessandro Di Federico 489dddd0e7 TupleTreeReference::isValid is for assertions only 2023-08-23 16:37:39 +02:00
Alessandro Di Federico 2b97576ebb HeaderToModel: do not hardcode clang paths 2023-08-23 16:37:39 +02:00
Alessandro Di Federico 100a27bb1c HeaderToModel: emit forward declaration later
We used to emit the forward declaration for the type we were editing
*before* `#pragma once`. This was not good per se, but it also lead to
failures due to the introduction of `_PACKED`, which is not yet defined
at that stage.
2023-08-23 16:37:39 +02:00
Alessandro Di Federico c623e2d9d6 HeaderToModel: fix a couple of std::optional bugs 2023-08-23 16:37:39 +02:00
Alessandro Di Federico 5ab0659966 HeaderToModel: change CustomName only if different
This commit fixes two problems with HeaderToModel: we no longer emit
fields for padding and do not change the name if it's identical to the
old `.name()`.

This issues led to create fields for padding and, in case the entity was
originally missing the `CustomName`, to set `CustomName`s to the
generated name.
2023-08-23 16:37:39 +02:00
Alessandro Di Federico 330d2b6cc7 Model: rework how we name things
This commit:

* Introduces `_` as a prefix for all non-user entities we emit in
  decompiled code.
  Also, some names have been changed to be more concise.
  Specifically, the following entities have changed:
  `_ENUM_UNDERLYING`, `_ABI`, `_REG`, `_padding_at_`,
  `_artificial_struct_`, `_artificial_wrapper_`, `_stack`,
  `_break_from_loop_`, `_var_`, `_stack_arguments`,
  `_artificial_struct_returned_`, `_enum_max_value_`.
* Introduce _PACKED for `__attribute__((packed))`.
* `EnumEntry` name: drop the `EnumType` name prefix.
2023-08-23 16:37:39 +02:00
Alessandro Di Federico f81e9d615b dumpModelToHeader: introduce struct for options
This commit also introduces a new option for emitting code right after
the `#include` section.
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
Alessandro Di Federico a207ef7c44 Minor changes 2023-08-23 16:12:51 +02:00