decompiler  1.0.0
Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
Merge Class Reference

Class for merging low-level Varnodes into high-level HighVariables. More...

#include <merge.hh>

Public Member Functions

 Merge (Funcdata &fd)
 Construct given a specific function.
 
bool intersection (HighVariable *a, HighVariable *b)
 Test the intersection of two HighVariables and cache the result. More...
 
bool inflateTest (Varnode *a, HighVariable *high)
 Test if we can inflate the Cover of the given Varnode without incurring intersections. More...
 
void inflate (Varnode *a, HighVariable *high)
 Inflate the Cover of a given Varnode with a HighVariable. More...
 
bool mergeTest (HighVariable *high, vector< HighVariable * > &tmplist)
 Test for intersections between a given HighVariable and a list of other HighVariables. More...
 
void mergeOpcode (OpCode opc)
 Try to force merges of input to output for all p-code ops of a given type. More...
 
void mergeByDatatype (VarnodeLocSet::const_iterator startiter, VarnodeLocSet::const_iterator enditer)
 Try to merge all HighVariables in the given range that have the same data-type. More...
 
void mergeAddrTied (void)
 Force the merge of address tried Varnodes. More...
 
void mergeMarker (void)
 Force the merge of input and output Varnodes to MULTIEQUAL and INDIRECT ops. More...
 
void mergeAdjacent (void)
 Speculatively merge Varnodes that are input/output to the same p-code op. More...
 
void mergeMultiEntry (void)
 Merge together Varnodes mapped to SymbolEntrys from the same Symbol. More...
 
bool hideShadows (HighVariable *high)
 Hide shadow Varnodes related to the given HighVariable by consolidating COPY chains. More...
 
void processCopyTrims (void)
 Try to reduce/eliminate COPYs produced by the merge trimming process. More...
 
void markInternalCopies (void)
 Mark redundant/internal COPY PcodeOps. More...
 

Private Member Functions

bool updateHigh (HighVariable *a)
 Make sure given HighVariable's Cover is up-to-date. More...
 
void purgeHigh (HighVariable *high)
 Remove cached intersection tests for a given HighVariable. More...
 
bool blockIntersection (HighVariable *a, HighVariable *b, int4 blk)
 Test if two HighVariables intersect on a given BlockBasic. More...
 
void collectCovering (vector< Varnode * > &vlist, HighVariable *high, PcodeOp *op)
 Collect all instances of the given HighVariable whose Cover intersects a p-code op. More...
 
bool collectCorrectable (const vector< Varnode * > &vlist, list< PcodeOp * > &oplist, vector< int4 > &slotlist, PcodeOp *op)
 Check for for p-code op intersections that are correctable. More...
 
PcodeOpallocateCopyTrim (Varnode *inVn, Datatype *ct, const Address &addr)
 Allocate COPY PcodeOp designed to trim an overextended Cover. More...
 
void snipReads (Varnode *vn, list< PcodeOp * > &markedop)
 Snip off set of read p-code ops for a given Varnode. More...
 
void snipIndirect (PcodeOp *indop)
 Snip instances of the input of an INDIRECT op that interfere with its output. More...
 
void eliminateIntersect (Varnode *vn, const vector< BlockVarnode > &blocksort)
 Eliminate intersections of given Varnode with other Varnodes in a list. More...
 
void unifyAddress (VarnodeLocSet::const_iterator startiter, VarnodeLocSet::const_iterator enditer)
 Make sure all Varnodes with the same storage address and size can be merged. More...
 
void trimOpOutput (PcodeOp *op)
 Trim the output HighVariable of the given PcodeOp so that its Cover is tiny. More...
 
void trimOpInput (PcodeOp *op, int4 slot)
 Trim the input HighVariable of the given PcodeOp so that its Cover is tiny. More...
 
void mergeRangeMust (VarnodeLocSet::const_iterator startiter, VarnodeLocSet::const_iterator enditer)
 Force the merge of a ranges of Varnodes with the same size and storage address. More...
 
void mergeOp (PcodeOp *op)
 Force the merge of all input and output Varnodes for the given PcodeOp. More...
 
void mergeIndirect (PcodeOp *indop)
 Force the merge of all input and output Varnodes to a given INDIRECT op. More...
 
void mergeLinear (vector< HighVariable * > &highvec)
 Speculatively merge all HighVariables in the given list as well as possible. More...
 
bool merge (HighVariable *high1, HighVariable *high2, bool isspeculative)
 Perform low-level details of merging two HighVariables if possible. More...
 
bool checkCopyPair (HighVariable *high, PcodeOp *domOp, PcodeOp *subOp)
 Check if the given PcodeOp COPYs are redundant. More...
 
void buildDominantCopy (HighVariable *high, vector< PcodeOp * > &copy, int4 pos, int4 size)
 Try to replace a set of COPYs from the same Varnode with a single dominant COPY. More...
 
void markRedundantCopies (HighVariable *high, vector< PcodeOp * > &copy, int4 pos, int4 size)
 Search for and mark redundant COPY ops into the given high as non-printing. More...
 
void processHighDominantCopy (HighVariable *high)
 Try to replace COPYs into the given HighVariable with a single dominant COPY. More...
 
void processHighRedundantCopy (HighVariable *high)
 Mark COPY ops into the given HighVariable that are redundant. More...
 

Static Private Member Functions

static bool mergeTestRequired (HighVariable *high_out, HighVariable *high_in)
 Required tests to merge HighVariables that are not Cover related. More...
 
static bool mergeTestAdjacent (HighVariable *high_out, HighVariable *high_in)
 Adjacency tests for merging Varnodes that are input or output to the same p-code op. More...
 
static bool mergeTestSpeculative (HighVariable *high_out, HighVariable *high_in)
 Speculative tests for merging HighVariables that are not Cover related. More...
 
static bool mergeTestBasic (Varnode *vn)
 A test if the given Varnode can ever be merged. More...
 
static void findSingleCopy (HighVariable *high, vector< Varnode * > &singlelist)
 Find instance Varnodes that copied to from outside the given HighVariable. More...
 
static bool compareHighByBlock (const HighVariable *a, const HighVariable *b)
 Compare HighVariables by the blocks they cover. More...
 
static bool compareCopyByInVarnode (PcodeOp *op1, PcodeOp *op2)
 Compare COPY ops first by Varnode input, then by block containing the op. More...
 
static bool shadowedVarnode (const Varnode *vn)
 Determine if given Varnode is shadowed by another Varnode in the same HighVariable. More...
 
static void findAllIntoCopies (HighVariable *high, vector< PcodeOp * > &copyIns, bool filterTemps)
 Find all the COPY ops into the given HighVariable. More...
 

Private Attributes

Funcdatadata
 The function containing the Varnodes to be merged.
 
map< HighEdge, bool > highedgemap
 A cache of intersection tests, sorted by HighVariable pair.
 
vector< PcodeOp * > copyTrims
 COPY ops inserted to facilitate merges.
 

Detailed Description

Class for merging low-level Varnodes into high-level HighVariables.

As a node in Single Static Assignment (SSA) form, a Varnode has at most one defining operation. To get a suitable notion of a single high-level variable (HighVariable) that may be reassigned at multiple places in a single function, individual Varnode objects can be merged into a HighVariable object. Varnode objects may be merged in this way if there is no pairwise intersection between each Varnode's Cover, the ranges of code where the Varnode holds its value.

For a given function, this class attempts to merge Varnodes using various strategies and keeps track of Cover intersections to facilitate the process. Merge strategies break up into two general categories: forced merges, and speculative merges. Forced merges must happen, and extra Varnodes may be added to split up problematic covers to enforce it. Forced merges include:

Speculative merges are attempted to reduce the overall number of variables defined by a function, but any given merge attempt is abandoned if there are Cover intersections. No modification is made to the data-flow to force the merge. Speculative merges include:

Member Function Documentation

PcodeOp * Merge::allocateCopyTrim ( Varnode inVn,
Datatype ct,
const Address addr 
)
private

Allocate COPY PcodeOp designed to trim an overextended Cover.

A COPY is allocated with the given input and data-type. A unique space output is created.

Parameters
inVnis the given input Varnode for the new COPY
ctis the data-type to assign to the new unique output
addris the address associated with the new COPY
Returns
the newly allocated COPY

References CPUI_COPY, and Varnode::getSize().

bool Merge::blockIntersection ( HighVariable a,
HighVariable b,
int4  blk 
)
private

Test if two HighVariables intersect on a given BlockBasic.

Intersections are checked only on the specified block.

Parameters
ais the first HighVariable
bis the second HighVariable
blkis the index of the BlockBasic on which to test intersection
Returns
true if an intersection occurs in the specified block

References Varnode::copyShadow(), Varnode::getCover(), HighVariable::getInstance(), Cover::intersectByBlock(), HighVariable::numInstances(), BlockVarnode::vn, and HighVariable::wholecover.

void Merge::buildDominantCopy ( HighVariable high,
vector< PcodeOp * > &  copy,
int4  pos,
int4  size 
)
private

Try to replace a set of COPYs from the same Varnode with a single dominant COPY.

All the COPY outputs must be instances of the same HighVariable (not the same Varnode). Either an existing COPY dominates all the others, or a new dominating COPY is constructed. The read locations of all other COPY outputs are replaced with the output of the dominating COPY, if it does not cause intersections in the HighVariable's Cover. Because of intersections, replacement may fail or partially succeed. Replacement only happens with COPY outputs that are temporary registers. The cover of the HighVariable may be extended because of a new COPY output instance.

Parameters
highis the HighVariable being copied to
copyis the list of COPY ops into the HighVariable
posis the index of the first COPY from the specific input Varnode
sizeis the number of COPYs (in sequence) from the same specific Varnode

References Cover::addDefPoint(), Cover::addRefPoint(), Varnode::beginDescend(), PcodeOp::clearMark(), PcodeOp::code(), Varnode::copyShadow(), CPUI_COPY, Varnode::endDescend(), FlowBlock::findCommonBlock(), Varnode::getCover(), Varnode::getDef(), Varnode::getHigh(), PcodeOp::getIn(), HighVariable::getInstance(), PcodeOp::getOut(), Varnode::getSize(), BlockBasic::getStop(), Varnode::getType(), Cover::intersect(), PcodeOp::isMark(), Varnode::isWritten(), HighVariable::merge(), Cover::merge(), HighVariable::numInstances(), HighVariable::remove(), PcodeOp::setMark(), and BlockVarnode::vn.

bool Merge::checkCopyPair ( HighVariable high,
PcodeOp domOp,
PcodeOp subOp 
)
private

Check if the given PcodeOp COPYs are redundant.

Both the given COPYs assign to the same HighVariable. One is redundant if there is no other assignment to the HighVariable between the first COPY and the second COPY. The first COPY must come from a block with a smaller or equal index to the second COPY. If the indices are equal, the first COPY must come before the second within the block.

Parameters
highis the HighVariable being assigned to
domOpis the first COPY
subOpis the second COPY
Returns
true if the second COPY is redundant

References Cover::addDefPoint(), Cover::addRefPoint(), PcodeOp::code(), Cover::contain(), CPUI_COPY, FlowBlock::dominates(), Varnode::getDef(), PcodeOp::getIn(), HighVariable::getInstance(), PcodeOp::getOut(), PcodeOp::getParent(), Varnode::isWritten(), HighVariable::numInstances(), and BlockVarnode::vn.

bool Merge::collectCorrectable ( const vector< Varnode * > &  vlist,
list< PcodeOp * > &  oplist,
vector< int4 > &  slotlist,
PcodeOp op 
)
private

Check for for p-code op intersections that are correctable.

Given a list of Varnodes that intersect a specific PcodeOp, check that each intersection is on the boundary, and if so, pass back the read op(s) that cause the intersection.

Parameters
vlistis the given list of intersecting Varnodes
oplistwill hold the boundary intersecting read ops
slotlistwill hold the corresponding input slots of the instance
opis the specific intersecting PcodeOp
Returns
false if any instance in the list intersects the PcodeOp on the interior

References Varnode::beginDescend(), CoverBlock::boundary(), Varnode::endDescend(), Varnode::getCover(), Cover::getCoverBlock(), FlowBlock::getIndex(), PcodeOp::getParent(), PcodeOp::getSlot(), CoverBlock::getUIndex(), and BlockVarnode::vn.

void Merge::collectCovering ( vector< Varnode * > &  vlist,
HighVariable high,
PcodeOp op 
)
private

Collect all instances of the given HighVariable whose Cover intersects a p-code op.

Efficiently test if each instance Varnodes contains the specific p-code op in its Cover and return a list of the instances that do.

Parameters
vlistwill hold the resulting list of intersecting instances
highis the given HighVariable
opis the specific PcodeOp to test intersection with

References CoverBlock::contain(), Varnode::getCover(), Cover::getCoverBlock(), FlowBlock::getIndex(), HighVariable::getInstance(), PcodeOp::getParent(), HighVariable::numInstances(), and BlockVarnode::vn.

bool Merge::compareCopyByInVarnode ( PcodeOp op1,
PcodeOp op2 
)
staticprivate

Compare COPY ops first by Varnode input, then by block containing the op.

A sort designed to group COPY ops from the same Varnode together. Then within a group, COPYs are sorted by their containing basic block (so that dominating ops come first).

Parameters
op1is the first PcodeOp being compared
op2is the second PcodeOp being compared
Returns
true if the first PcodeOp should be ordered before the second

References Varnode::getCreateIndex(), PcodeOp::getIn(), FlowBlock::getIndex(), SeqNum::getOrder(), PcodeOp::getParent(), and PcodeOp::getSeqNum().

bool Merge::compareHighByBlock ( const HighVariable a,
const HighVariable b 
)
inlinestaticprivate

Compare HighVariables by the blocks they cover.

This comparator sorts, based on:

  • Index of the first block containing cover for the HighVariable
  • Address of the first instance
  • Address of the defining p-code op
  • Storage address
Parameters
ais the first HighVariable to compare
bis the second HighVariable
Returns
true if the first HighVariable should be ordered before the second

References Cover::compareTo(), PcodeOp::getAddr(), Varnode::getAddr(), Varnode::getDef(), HighVariable::getInstance(), and HighVariable::wholecover.

void Merge::eliminateIntersect ( Varnode vn,
const vector< BlockVarnode > &  blocksort 
)
private

Eliminate intersections of given Varnode with other Varnodes in a list.

Both the given Varnode and those in the list are assumed to be at the same storage address. For any intersection, identify the PcodeOp reading the given Varnode which causes the intersection and snip the read by inserting additional COPY ops.

Parameters
vnis the given Varnode
blocksortis the list of other Varnodes sorted by their defining basic block

References Cover::addDefPoint(), Cover::addRefPoint(), Cover::begin(), Varnode::beginDescend(), PcodeOp::code(), Cover::containVarnodeDef(), Varnode::copyShadow(), CPUI_INDIRECT, Cover::end(), Varnode::endDescend(), BlockVarnode::findFront(), Varnode::getAddr(), Varnode::getDef(), PcodeOp::getIn(), BlockVarnode::getIndex(), PcodeOp::getOpFromConst(), SeqNum::getOrder(), PcodeOp::getSeqNum(), Varnode::isAddrForce(), and Varnode::isWritten().

void Merge::findAllIntoCopies ( HighVariable high,
vector< PcodeOp * > &  copyIns,
bool  filterTemps 
)
staticprivate

Find all the COPY ops into the given HighVariable.

Collect all the COPYs whose output is the given HighVariable but the input is from a different HighVariable. Returned COPYs are sorted first by the input Varnode then by block order.

Parameters
highis the given HighVariable
copyInswill hold the list of COPYs
filterTempsis true if COPYs must have a temporary output

References PcodeOp::code(), CPUI_COPY, Varnode::getDef(), Varnode::getHigh(), PcodeOp::getIn(), HighVariable::getInstance(), PcodeOp::getOut(), Varnode::getSpace(), AddrSpace::getType(), IPTR_INTERNAL, Varnode::isWritten(), HighVariable::numInstances(), and BlockVarnode::vn.

void Merge::findSingleCopy ( HighVariable high,
vector< Varnode * > &  singlelist 
)
staticprivate

Find instance Varnodes that copied to from outside the given HighVariable.

Find all Varnodes in the HighVariable which are defined by a COPY from another Varnode which is not part of the same HighVariable.

Parameters
highis the given HighVariable
singlelistwill hold the resulting list of copied instances

References PcodeOp::code(), CPUI_COPY, Varnode::getDef(), Varnode::getHigh(), PcodeOp::getIn(), HighVariable::getInstance(), Varnode::isWritten(), HighVariable::numInstances(), and BlockVarnode::vn.

bool Merge::hideShadows ( HighVariable high)

Hide shadow Varnodes related to the given HighVariable by consolidating COPY chains.

If two Varnodes are copied from the same common ancestor then they will always contain the same value and can be considered shadows of the same variable. If the paths from the ancestor to the two Varnodes aren't properly nested, the two Varnodes will still look like distinct variables. This routine searches for this situation, relative to a single HighVariable, and alters data-flow so that copying from ancestor to first Varnode to second Varnode becomes a single path. Both Varnodes then ultimately become instances of the same HighVariable.

Parameters
highis the given HighVariable to search near
Returns
true if a change was made to data-flow

References Cover::containVarnodeDef(), Varnode::copyShadow(), Varnode::getCover(), and Varnode::getDef().

Referenced by ActionHideShadow::apply().

void Merge::inflate ( Varnode a,
HighVariable high 
)

Inflate the Cover of a given Varnode with a HighVariable.

An expression involving a HighVariable can be propagated to all the read sites of the output Varnode of the expression if the Varnode Cover can be inflated to include the Cover of the HighVariable, even though the Varnode is not part of the HighVariable. This routine performs the inflation, assuming an intersection test is already performed.

Parameters
ais the given Varnode to inflate
highis the HighVariable to inflate with

References Varnode::cover, HighVariable::coverDirty(), Varnode::getHigh(), HighVariable::getInstance(), Cover::merge(), and HighVariable::numInstances().

Referenced by ActionMarkImplied::apply().

bool Merge::inflateTest ( Varnode a,
HighVariable high 
)

Test if we can inflate the Cover of the given Varnode without incurring intersections.

This routine tests whether an expression involving a HighVariable can be propagated to all the read sites of the output Varnode of the expression. This is possible only if the Varnode Cover can be inflated to include the Cover of the HighVariable, even though the Varnode is not part of the HighVariable.

Parameters
ais the given Varnode to inflate
highis the HighVariable being propagated
Returns
true if the Varnode can be inflated without intersection

References Varnode::copyShadow(), Varnode::getCover(), Varnode::getHigh(), HighVariable::getInstance(), Cover::intersect(), HighVariable::numInstances(), and HighVariable::wholecover.

Referenced by ActionMarkImplied::checkImpliedCover().

bool Merge::intersection ( HighVariable a,
HighVariable b 
)

Test the intersection of two HighVariables and cache the result.

If the Covers of the two variables intersect, this routine returns true. To avoid expensive computation on the Cover objects themselves, the test result associated with the pair of HighVariables is cached.

Parameters
ais the first HighVariable
bis the second HighVariable
Returns
true if the variables intersect

References Cover::intersectList(), and HighVariable::wholecover.

void Merge::markInternalCopies ( void  )

Mark redundant/internal COPY PcodeOps.

Run through all COPY, SUBPIECE, and PIECE operations (PcodeOps that copy data) and characterize those that are internal (copy data between storage locations representing the same variable) or redundant (perform the same copy as an earlier operation). These, as a result, are not printed in the final source code representation.

References HighVariable::clearCopyIns(), PcodeOp::code(), CPUI_COPY, CPUI_PIECE, CPUI_SUBPIECE, Varnode::getHigh(), PcodeOp::getIn(), Varnode::getOffset(), PcodeOp::getOut(), Varnode::getSize(), HighVariable::getTiedVarnode(), HighVariable::hasCopyIn1(), HighVariable::hasCopyIn2(), Varnode::hasNoDescend(), HighVariable::isAddrTied(), Varnode::overlap(), HighVariable::setCopyIn1(), and HighVariable::setCopyIn2().

Referenced by ActionCopyMarker::apply().

void Merge::markRedundantCopies ( HighVariable high,
vector< PcodeOp * > &  copy,
int4  pos,
int4  size 
)
private

Search for and mark redundant COPY ops into the given high as non-printing.

Trimming during the merge process can insert multiple COPYs from the same source. In some cases, one or more COPYs may be redundant and shouldn't be printed. This method searches for redundancy among COPY ops that assign to the given HighVariable.

Parameters
highis the given HighVariable
copyis the list of COPYs coming from the same source HighVariable
posis the starting index of a set of COPYs coming from the same Varnode
sizeis the number of Varnodes in the set coming from the same Varnode

References PcodeOp::isDead().

bool Merge::merge ( HighVariable high1,
HighVariable high2,
bool  isspeculative 
)
private

Perform low-level details of merging two HighVariables if possible.

This routine only fails (returning false) if there is a Cover intersection between the two variables. Otherwise, all the Varnode instances from the second HighVariable are merged into the first and its Cover is updated. The cached intersection tests are also updated to reflect the merge.

Parameters
high1is the first HighVariable being merged
high2is the second
isspeculativeis true if the desired merge is speculative
Returns
true if the merge was successful

References HighVariable::merge(), HighVariable::setMark(), and HighVariable::updateCover().

void Merge::mergeAddrTied ( void  )

Force the merge of address tried Varnodes.

For each set of address tied Varnodes with the same size and storage address, merge them into a single HighVariable. The merges are forced, so any Cover intersections must be resolved by altering data-flow, which involves inserting additional COPY ops and unique Varnodes.

References Varnode::written.

Referenced by ActionMergeRequired::apply().

void Merge::mergeAdjacent ( void  )

Speculatively merge Varnodes that are input/output to the same p-code op.

If a single p-code op has an input and output HighVariable that share the same data-type, attempt to merge them. Each merge is speculative and is skipped if it would introduce Cover intersections.

References Varnode::getDef(), Varnode::getHigh(), PcodeOp::getIn(), PcodeOp::getOut(), Varnode::getSize(), PcodeOp::inputTypeLocal(), PcodeOp::isCall(), Varnode::isInput(), PcodeOp::numInput(), and PcodeOp::outputTypeLocal().

Referenced by ActionMergeAdjacent::apply().

void Merge::mergeByDatatype ( VarnodeLocSet::const_iterator  startiter,
VarnodeLocSet::const_iterator  enditer 
)

Try to merge all HighVariables in the given range that have the same data-type.

HighVariables that have an instance within the given Varnode range are sorted into groups based on their data-type. Then an attempt is made to merge all the HighVariables within a group. If a particular merge causes Cover intersection, it is skipped.

Parameters
startiteris the start of the given range of Varnodes
enditeris the end of the given range

References HighVariable::getType(), Varnode::isFree(), HighVariable::isMark(), HighVariable::setMark(), and BlockVarnode::vn.

Referenced by ActionMergeType::apply().

void Merge::mergeIndirect ( PcodeOp indop)
private

Force the merge of all input and output Varnodes to a given INDIRECT op.

Merging INDIRECTs take a little care if their output is address forced because by convention the value must be present at the address BEFORE the indirect effect operation takes place.

Parameters
indopis the given INDIRECT

References PcodeOp::getAddr(), Varnode::getHigh(), PcodeOp::getIn(), PcodeOp::getOut(), Varnode::getType(), and Varnode::isAddrForce().

void Merge::mergeLinear ( vector< HighVariable * > &  highvec)
private

Speculatively merge all HighVariables in the given list as well as possible.

The variables are first sorted by the index of the earliest block in their range. Then proceeding in order, an attempt is made to merge each variable with the first. The attempt fails if the speculative test doesn't pass or if there are Cover intersections, in which case that particular merge is skipped.

void Merge::mergeMarker ( void  )

Force the merge of input and output Varnodes to MULTIEQUAL and INDIRECT ops.

Run through all MULTIEQUAL and INDIRECT ops in the function. Force the merge of each input Varnode with the output Varnode, doing data-flow modification if necessary to resolve Cover intersections.

References PcodeOp::code(), CPUI_INDIRECT, PcodeOp::isIndirectCreation(), and PcodeOp::isMarker().

Referenced by ActionMergeRequired::apply().

void Merge::mergeMultiEntry ( void  )

Merge together Varnodes mapped to SymbolEntrys from the same Symbol.

Symbols that have more than one SymbolEntry may attach to more than one Varnode. These Varnodes need to be merged to properly represent a single variable.

References Symbol::getMapEntry(), Symbol::getName(), Datatype::getSize(), SymbolEntry::getSize(), Symbol::getType(), Symbol::numEntries(), Symbol::setMergeProblems(), and HighVariable::setUnmerged().

Referenced by ActionMergeMultiEntry::apply().

void Merge::mergeOp ( PcodeOp op)
private

Force the merge of all input and output Varnodes for the given PcodeOp.

Data-flow for specific input and output Varnodes are snipped until everything can be merged.

Parameters
opis the given PcodeOp

References PcodeOp::code(), CPUI_INDIRECT, Varnode::getHigh(), PcodeOp::getIn(), PcodeOp::getOut(), PcodeOp::getSeqNum(), and PcodeOp::numInput().

void Merge::mergeOpcode ( OpCode  opc)

Try to force merges of input to output for all p-code ops of a given type.

For a given opcode, run through all ops in the function in block/address order and try to merge each input HighVariable with the output HighVariable. If this would introduce Cover intersections, the merge is skipped. This is generally used to try to merge the input and output of COPY ops if possible.

Parameters
opcis the op-code type to merge

References BlockBasic::beginOp(), PcodeOp::code(), BlockBasic::endOp(), Varnode::getHigh(), PcodeOp::getIn(), PcodeOp::getOut(), and PcodeOp::numInput().

Referenced by ActionMergeCopy::apply().

void Merge::mergeRangeMust ( VarnodeLocSet::const_iterator  startiter,
VarnodeLocSet::const_iterator  enditer 
)
private

Force the merge of a ranges of Varnodes with the same size and storage address.

The list of Varnodes to be merged is provided as a range in the main location sorted container. Any Cover intersection is assumed to already be snipped, so any problems with merging cause an exception to be thrown.

Parameters
startiteris the beginning of the range of Varnodes with the same storage address
enditeris the end of the range

References Varnode::getHigh(), Varnode::isSpacebase(), and BlockVarnode::vn.

bool Merge::mergeTest ( HighVariable high,
vector< HighVariable * > &  tmplist 
)

Test for intersections between a given HighVariable and a list of other HighVariables.

If there is any Cover intersection between the given HighVariable and one in the list, this routine returns false. Otherwise, the given HighVariable is added to the end of the list and true is returned.

Parameters
highis the given HighVariable
tmplistis the list of HighVariables to test against
Returns
true if there are no pairwise intersections.

References Varnode::getHigh(), HighVariable::hasCopyIn1(), HighVariable::hasCover(), Varnode::hasCover(), HighVariable::setCopyIn1(), and BlockVarnode::vn.

bool Merge::mergeTestAdjacent ( HighVariable high_out,
HighVariable high_in 
)
staticprivate

Adjacency tests for merging Varnodes that are input or output to the same p-code op.

All the required tests (mergeTestRequired()) are performed, and then some additional tests are performed. This does not perform any Cover tests.

Parameters
high_outis the output HighVariable to test
high_inis the input HighVariable to test
Returns
true if tests pass and the HighVariables are not forbidden to merge

References HighVariable::getInputVarnode(), HighVariable::getSymbol(), HighVariable::getType(), Varnode::isIllegalInput(), Varnode::isIndirectOnly(), HighVariable::isInput(), Symbol::isIsolated(), HighVariable::isNameLock(), and BlockVarnode::vn.

bool Merge::mergeTestBasic ( Varnode vn)
staticprivate

A test if the given Varnode can ever be merged.

Some Varnodes (constants, annotations, implied, spacebase) are never merged with another Varnode.

Parameters
vnis the Varnode to test
Returns
true if the Varnode is not forbidden from ever merging

References Varnode::hasCover(), Varnode::isImplied(), and Varnode::isSpacebase().

bool Merge::mergeTestRequired ( HighVariable high_out,
HighVariable high_in 
)
staticprivate

Required tests to merge HighVariables that are not Cover related.

This is designed to short circuit merge tests, when we know properties of the two HighVariables preclude merging. For example, you can't merge HighVariables if:

  • They are locked to different data-types
  • They are both mapped to different address ranges
  • One is a parameter one is a global
Parameters
high_outis the first HighVariable to test
high_inis the second HighVariable to test
Returns
true if tests pass and the HighVariables are not forbidden to merge

References Varnode::getAddr(), HighVariable::getSymbol(), HighVariable::getSymbolOffset(), HighVariable::getTiedVarnode(), HighVariable::getType(), HighVariable::isAddrTied(), HighVariable::isExtraOut(), HighVariable::isInput(), HighVariable::isPersist(), and HighVariable::isTypeLock().

bool Merge::mergeTestSpeculative ( HighVariable high_out,
HighVariable high_in 
)
staticprivate

Speculative tests for merging HighVariables that are not Cover related.

This does all the required and adjacency merge tests and then performs additional tests required for speculative merges.

Parameters
high_outis the first HighVariable to test
high_inis the second HighVariable to test
Returns
true if tests pass and the HighVariables are not forbidden to merge

References HighVariable::isAddrTied(), HighVariable::isInput(), and HighVariable::isPersist().

void Merge::processCopyTrims ( void  )

Try to reduce/eliminate COPYs produced by the merge trimming process.

In order to force merging of certain Varnodes, extra COPY operations may be inserted to reduce their Cover ranges, and multiple COPYs from the same Varnode can be created this way. This method collects sets of COPYs generated in this way that have the same input Varnode and then tries to replace the COPYs with fewer or a single COPY.

References HighVariable::clearCopyIns(), HighVariable::hasCopyIn1(), HighVariable::hasCopyIn2(), HighVariable::setCopyIn1(), and HighVariable::setCopyIn2().

Referenced by ActionDominantCopy::apply().

void Merge::processHighDominantCopy ( HighVariable high)
private

Try to replace COPYs into the given HighVariable with a single dominant COPY.

Find groups of COPYs into the given HighVariable that come from a single source Varnode, then try to replace them with a COPY.

Parameters
highis the given HighVariable
void Merge::processHighRedundantCopy ( HighVariable high)
private

Mark COPY ops into the given HighVariable that are redundant.

A COPY is redundant if another COPY performs the same action and has dominant control flow. The redundant COPY is not removed but is marked so that it doesn't print in the final source output.

Parameters
highis the given HighVariable
void Merge::purgeHigh ( HighVariable high)
private

Remove cached intersection tests for a given HighVariable.

All tests for pairs where either the first or second HighVariable matches the given one are removed.

Parameters
highis the given HighVariable to purge
bool Merge::shadowedVarnode ( const Varnode vn)
staticprivate

Determine if given Varnode is shadowed by another Varnode in the same HighVariable.

Parameters
vnis the Varnode to check for shadowing
Returns
true if vn is shadowed by another Varnode in its high-level variable

References Varnode::getCover(), Varnode::getHigh(), HighVariable::getInstance(), Cover::intersect(), and HighVariable::numInstances().

void Merge::snipIndirect ( PcodeOp indop)
private

Snip instances of the input of an INDIRECT op that interfere with its output.

Examine the input and output HighVariable for the given INDIRECT op. Varnode instances of the input that intersect the output Cover are snipped by creating a new COPY op from the input to a new temporary and then replacing the Varnode reads with the temporary.

Parameters
indopis the given INDIRECT op

References PcodeOp::getAddr(), Varnode::getAddr(), Varnode::getHigh(), PcodeOp::getIn(), PcodeOp::getOpFromConst(), PcodeOp::getOut(), and Varnode::getType().

void Merge::snipReads ( Varnode vn,
list< PcodeOp * > &  markedop 
)
private

Snip off set of read p-code ops for a given Varnode.

The data-flow for the given Varnode is truncated by creating a COPY p-code from the Varnode into a new temporary Varnode, then replacing the Varnode reads for a specific set of p-code ops with the temporary.

Parameters
vnis the given Varnode
markedopis the specific set of PcodeOps reading the Varnode

References PcodeOp::code(), CPUI_INDIRECT, PcodeOp::getAddr(), Varnode::getAddr(), Varnode::getDef(), PcodeOp::getIn(), PcodeOp::getOpFromConst(), PcodeOp::getOut(), PcodeOp::getParent(), BlockBasic::getStart(), Varnode::getType(), Varnode::isInput(), PcodeOp::numInput(), and BlockVarnode::vn.

void Merge::trimOpInput ( PcodeOp op,
int4  slot 
)
private

Trim the input HighVariable of the given PcodeOp so that its Cover is tiny.

The given PcodeOp is assumed to force merging so that input and output Covers shouldn't intersect. A new COPY is inserted right before the given PcodeOp with a new unique output that replaces the specified input, disassociating it from the other original inputs and output.

Parameters
opis the given PcodeOp
slotis the specified slot of the input Varnode to be trimmed

References PcodeOp::code(), CPUI_MULTIEQUAL, PcodeOp::getAddr(), PcodeOp::getIn(), FlowBlock::getIn(), PcodeOp::getOut(), PcodeOp::getParent(), BlockBasic::getStop(), Varnode::getType(), and BlockVarnode::vn.

void Merge::trimOpOutput ( PcodeOp op)
private

Trim the output HighVariable of the given PcodeOp so that its Cover is tiny.

The given PcodeOp is assumed to force merging so that input and output Covers shouldn't intersect. The original PcodeOp output is moved so that it becomes the output of a new COPY, disassociating the original output Varnode from the inputs.

Parameters
opis the given PcodeOp

References PcodeOp::code(), CPUI_COPY, CPUI_INDIRECT, PcodeOp::getAddr(), Varnode::getAddr(), PcodeOp::getIn(), PcodeOp::getOpFromConst(), PcodeOp::getOut(), Varnode::getSize(), Varnode::getType(), and BlockVarnode::vn.

void Merge::unifyAddress ( VarnodeLocSet::const_iterator  startiter,
VarnodeLocSet::const_iterator  enditer 
)
private

Make sure all Varnodes with the same storage address and size can be merged.

The list of Varnodes to be merged is provided as a range in the main location sorted container. Any discovered intersection is snipped by splitting data-flow for one of the Varnodes into two or more flows, which involves insert new COPY ops and temporaries.

Parameters
startiteris the beginning of the range of Varnodes with the same storage address
enditeris the end of the range

References BlockVarnode::vn.

bool Merge::updateHigh ( HighVariable a)
private

Make sure given HighVariable's Cover is up-to-date.

As manipulations are made, Cover information gets out of date. A dirty flag is used to indicate a particular HighVariable Cover is out-of-date. This routine checks the dirty flag and updates the Cover information if it is set.

Parameters
ais the HighVariable to update
Returns
true if the HighVariable was not originally dirty

References HighVariable::coverdirty, HighVariable::getInstance(), HighVariable::highflags, HighVariable::numInstances(), HighVariable::updateCover(), and Varnode::updateCover().


The documentation for this class was generated from the following files: