Commit Graph

6113 Commits

Author SHA1 Message Date
Andrea Gussoni 3cd475e96e Refactor IsASwitch in a static function
Refactor the computation of `IsASwitch` (which checks if the original
blocks contained a `switch` instruction) in a dedicated static function.
2020-06-15 15:06:23 +02:00
Andrea Gussoni aa11bcc307 Refactor switch AST generation phase
Isolated the part creating `switch` AST nodes in a dedicated section of
the `generateAST` method.

In addition, the creation of the standard nodes has been refactored with
a `switch` case covering all the possible situations.
2020-06-15 15:06:23 +02:00
Andrea Gussoni cd6edec660 Factor dispatcher creation code 2020-06-15 15:06:23 +02:00
Andrea Gussoni e4a24b2ebc ReversePostOrderTraversalExt in dedicated header 2020-06-15 15:06:23 +02:00
Andrea Gussoni 0bf43f75c1 Incremental update for DT and PDT 2020-06-15 15:06:23 +02:00
Andrea Gussoni da3a278489 Remove distinction between entry/exit dispatcher
Removed the distinction between entry and exit dispatcher nodes since
they are semantically equivalent.
Only the name differentiates them (useful for debugging purpose).
2020-06-15 15:06:23 +02:00
Andrea Gussoni 4f7661e7fe Restructure predecessors and successors methods
Drop duplicated code in `addPredecessor/addSuccessor` reusing the code
of respectively `hasPredecessor/hasSuccessor`.

Restructure the code of `hasPredecessor/hasSuccessor` with idiomatic use
of `std::find`.
2020-06-15 15:06:23 +02:00
Andrea Gussoni 407f37c4f6 Merge two debug procedures 2020-06-15 15:06:23 +02:00
Andrea Gussoni 787b437723 Restructure the weaving
Restructure the core algorithm of the weaving, according to the
following idea:
- Compute the PDT.
- Iterate on the graph in post order.
- For every switch node S, take its immediate post dominator P.
- Let C be the set of cases for S and iterate in reverse post order on
  the nodes N between S and P.
- If N post-dominates at least one case in C, but not all of them, it's
  a candidate for weaving.
- Waving inserts a common predecessor K to the cases to be weaved. The
  weaved cases are removed from C, and K is added to C (C is the set of
  case labels for the switch S).
- The PDT is then updated to reflect the addition of K.
2020-06-15 15:06:23 +02:00
Andrea Gussoni c5671b2a17 Fix typos 2020-06-15 15:06:23 +02:00
Andrea Gussoni b0b45e7663 Remove leftovers and debugging stuff
Remove some leftovers and debugging instructions left from the
development.
2020-06-15 15:06:23 +02:00
Andrea Gussoni 42b95d7054 Promote iteration to ranged for loop
As suggested in the MR, promoted `for` iteration to ranged `for`.
2020-06-15 15:06:23 +02:00
Andrea Gussoni 42c81fb9d6 Remove assert on max number of successor
Remove an assert used to check that no switch nodes were present in the
graph. These assertions are not useful anymore since we now handle
generic conditional nodes.
2020-06-15 15:06:23 +02:00
Andrea Gussoni 9d4023f5db Correct the wave -> weave typo 2020-06-15 15:06:23 +02:00
Andrea Gussoni 1c842fbd28 Rework Dispatcher kind attribute and constructor
Reorganize attribute `Kind` incorporating it into the `Type` attribute,
by lowering its memory footprint.
Modified the constructors, some accessors and helpers accordingly.
2020-06-15 15:06:23 +02:00
Andrea Gussoni be39c9a5e3 Actualize assert to new node types 2020-06-15 15:06:23 +02:00
Andrea Gussoni 1732de392f Remove leftover useless comments 2020-06-15 15:06:23 +02:00
Andrea Gussoni dd10bbf99b Improve RegionCFG::initialize method
Removed some development leftover in the aforementioned method. In the
future we should consider adding some logic to not set as a successor
for each `switch` node the `unexpectedpc`, which causes sub-optimal
performances in the restructuring (and combing, and untangling, and
waeving).
2020-06-15 15:06:23 +02:00
Andrea Gussoni fcf87bf283 Reorder case switch in flattening
Reorder the cases in a switch to improve quality, as suggested in the
MR.
2020-06-15 15:06:23 +02:00
Andrea Gussoni d69364a7d1 Improve some ASTNode constructors
Improve constructors of `SwitchNode`, `RegularSwitchNode` and
`SwitchCheckNode` by adding a new default parameter for the successor
and therefore removing a useless parameter.
2020-06-15 15:06:23 +02:00
Andrea Gussoni a8b25fdd52 Differentiate entry and exit dispatcher
Differentiate the entry and exit dispatchers introduced during the
restructuring with a different `Kind` enum inside the node.
2020-06-15 15:06:23 +02:00
Andrea Gussoni 0a2ae1a19e Update dom and postdom at each waveing iteration
We now update the dominator and postdominator trees after each iteration
of the waveing algorithm.
2020-06-15 15:06:22 +02:00
Andrea Gussoni 17809e0ad1 Rename throttle in wave
Change the name of the throttle function into waveing, accordingly to
our conventions.
2020-06-15 15:06:22 +02:00
Andrea Gussoni c2bd0f2654 Set the postdominator for cases nodes
We now set the postdominator (meaning the point where the combing stop
the exploration for dominated nodes) for the case nodes using the
postdominator of the switch node corresponding to the case.
2020-06-15 15:06:22 +02:00
Andrea Gussoni 3be06929b5 Workaround to always catch nodes
Workaround that resort to looking for the original switch node in the IR
to force the creation of a switch node even if we have < 4 nodes under
our domination.
2020-06-15 15:06:22 +02:00
Andrea Gussoni 6ac4334ad2 First implementation of the waveing pass
First implementation sketch of the waveing pass.

The implementation operates by going through the postdominator tree, and
by adding preposterous switch nodes in case we have a topology that we
cannot emit using standard switches.
In this way, we build a tree of nested switch costructs.
2020-06-15 15:06:22 +02:00
Andrea Gussoni fb2e01dda1 Add skeleton for waveing pass
Add the skeleton for the weaving pass and the graph dumping before and
after the pass to improve debuggability.
2020-06-15 15:06:22 +02:00
Andrea Gussoni 1f94d0fdd7 Remove assertions on SwitchNode
Disable assertions which are no more valid since `CheckNodes` are no
more present in the `RegionCFG`.
2020-06-15 15:06:22 +02:00
Andrea Gussoni d4111aaa20 Document the untangle 2020-06-15 15:06:22 +02:00
Andrea Gussoni 93d4095584 Add new dispatcher node
Added a new constructor for the dispatcher nodes which will be emitted
as `switch` nodes by the decompiler.
These nodes are introduced during the restructuring phase as loop
dispatchers.
2020-06-15 15:06:22 +02:00
Andrea Gussoni 0fb4fc6503 Enable the untangle procedure again 2020-06-15 15:06:22 +02:00
Andrea Gussoni c7975effc9 Do not consider postdominator as case in switch
We avoid to add the postdominator node of a switch node as one of the
cases of the switch AST node.
2020-06-15 15:06:22 +02:00
Andrea Gussoni eaac9ec0f9 Improve switch node creation in AST creation phase
Improved the creation of `Regular` and `Check` switch nodes types during
the preliminary AST creation phase performed on the `RegionCFG`.

In particular, we now handle the dispatchers creating the right AST node
type.
2020-06-15 15:06:22 +02:00
Andrea Gussoni 0616cdbfdd New constructor for SwitchCheck AST node
Added a new constructor for the `SwitchCheck` node type in order to
build AST nodes with a successor field.
2020-06-15 15:06:22 +02:00
Andrea Gussoni 63b6409a17 Handle more node types during flattening 2020-06-15 15:06:22 +02:00
Andrea Gussoni ca747929e8 Build new dispatchers during restructuring
Build the new dispatcher nodes type (switches instead of nested trees of
`if`) during the control flow restructuring phase.
2020-06-15 15:06:22 +02:00
Andrea Gussoni 2bc60d9c27 Dispatcher node type in RegionCFG
Created a new node type in the RegionCFG to represent the dispatcher
nodes.
2020-06-15 15:06:22 +02:00
Andrea Gussoni 9e869e7e23 Fix indentation 2020-06-15 15:06:22 +02:00
Andrea Gussoni dce994acdb Fix SwitchNode bug in Clone method
Explicitly handle the `RegularSwitchNode` type in the `ASTNode` cloning
method.
2020-06-15 15:06:22 +02:00
Andrea Gussoni 0a630cc9ea Handle Switch in AST construction analysis 2020-06-15 15:06:22 +02:00
Andrea Gussoni 36af400ab8 Fix bug using wrong LLVM context 2020-06-15 15:06:22 +02:00
Andrea Gussoni f2f659d57a Search and set post dominator node for switches
When building the switch AST node search for its post dominator, and add
this information into the AST node just created.
2020-06-15 15:06:22 +02:00
Andrea Gussoni cbcabfef39 Handle SwitchNode during preliminary AST build 2020-06-15 15:06:22 +02:00
Andrea Gussoni 4777bf3083 Load also unexpectedpc blocks 2020-06-15 15:06:22 +02:00
Andrea Gussoni 59fc142dc9 Build RegularSwitchNode during AST creation
Create directly regular switch nodes in the AST build starting from the
`RegionCFG`.

As of now, a switch node is built only if we find a node that
immediately dominates more than 3 nodes.

In the future, we will need to refine this criterion.
2020-06-15 15:06:22 +02:00
Andrea Gussoni a867854e1d Fix RegularSwitchNode dump bug 2020-06-15 15:06:22 +02:00
Andrea Gussoni 026baf03d0 Handle switch nodes in inflate
The switch nodes are now considered as conditional nodes, and therefore
processed by the combing.
2020-06-15 15:06:22 +02:00
Andrea Gussoni 4775492d97 Handle switch node during RegionCFG initialization
During the RegionCFG creation from the LLVM IR handle the creation of
nodes with possibly more than two successors.

In this way we do not have to disassemble the switch nodes in nested if
trees.
2020-06-15 15:06:22 +02:00
Andrea Gussoni aacb65238a HACK: temporarily disable untangle
Disable the untangle processing until we understand what we need to do
with the new switch nodes.

In this way we can proceed with the prototyping.
2020-06-15 15:06:22 +02:00
Pietro Fezzardi b0835a6aaf Remove unused FlattenLog Logger
This Logger was also defined as `static` into a public header, making it
bug-prone.
2020-06-15 15:04:04 +02:00