Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-7166 More efficient versions of RulePropagateCopy and RuleIndirectCollapse

caheckman6732a6ad642e
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/architecture.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/architecture.cc
index 6ddf1d140a..385cd2523d 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/architecture.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/architecture.cc
@@ -33,7 +33,7 @@ using std::sqrt;
vector<ArchitectureCapability *> ArchitectureCapability::thelist;
const uint4 ArchitectureCapability::majorversion = 6;
-const uint4 ArchitectureCapability::minorversion = 1;
+const uint4 ArchitectureCapability::minorversion = 2;
AttributeId ATTRIB_ADDRESS = AttributeId("address",148);
AttributeId ATTRIB_ADJUSTVMA = AttributeId("adjustvma",103);
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc
index 46f9e08994..3dd322cb17 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc
@@ -5920,7 +5920,7 @@ void ActionDatabase::universalAction(Architecture *conf)
actprop->addRule( new RuleShift2Mult("analysis") );
actprop->addRule( new RuleShiftPiece("analysis") );
actprop->addRule( new RuleMultiCollapse("analysis") );
- actprop->addRule( new RuleIndirectCollapse("analysis") );
+ actprop->addRule( new RuleAliasUpdate("analysis") );
actprop->addRule( new Rule2Comp2Mult("analysis") );
actprop->addRule( new RuleSub2Add("analysis") );
actprop->addRule( new RuleCarryElim("analysis") );
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/double.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/double.cc
index 858b64b499..0f2d4c76c1 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/double.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/double.cc
@@ -957,21 +957,6 @@ bool SplitVarnode::otherwiseEmpty(PcodeOp *branchop)
return true;
}
-/// \brief Verify that the given PcodeOp is a CPUI_INT_MULT by -1
-///
-/// The PcodeOp must be a CPUI_INT_MULT and the second operand must be a constant -1.
-/// \param op is the given PcodeOp
-/// \return \b true if the PcodeOp is a multiple by -1
-bool SplitVarnode::verifyMultNegOne(PcodeOp *op)
-
-{
- if (op->code() != CPUI_INT_MULT) return false;
- Varnode *in1 = op->getIn(1);
- if (!in1->isConstant()) return false;
- if (in1->getOffset() != calc_mask(in1->getSize())) return false;
- return true;
-}
-
/// \brief Check that the logical version of a (generic) binary double-precision operation can be created
///
/// This checks only the most generic aspects of the calculation. The input and output whole Varnodes
@@ -1640,8 +1625,8 @@ bool SubForm::verify(Varnode *h,Varnode *l,PcodeOp *op)
}
if (!hineg1->isWritten()) continue;
if (!hineg2->isWritten()) continue;
- if (!SplitVarnode::verifyMultNegOne(hineg1->getDef())) continue;
- if (!SplitVarnode::verifyMultNegOne(hineg2->getDef())) continue;
+ if (!hineg1->getDef()->verifyMultNegOne()) continue;
+ if (!hineg2->getDef()->verifyMultNegOne()) continue;
hizext1 = hineg1->getDef()->getIn(0);
hizext2 = hineg2->getDef()->getIn(0);
for(int4 j=0;j<2;++j) {
@@ -1670,7 +1655,7 @@ bool SubForm::verify(Varnode *h,Varnode *l,PcodeOp *op)
Varnode *tmpvn = loadd->getIn(1-loadd->getSlot(lo1));
if (!tmpvn->isWritten()) continue;
negop = tmpvn->getDef();
- if (!SplitVarnode::verifyMultNegOne(negop)) continue;
+ if (!negop->verifyMultNegOne()) continue;
if (negop->getIn(0) != lo2) continue;
reslo = loadd->getOut();
return true;
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/double.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/double.hh
index fb53fa330b..346a2d9996 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/double.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/double.hh
@@ -78,7 +78,6 @@ public:
static void findCopies(const SplitVarnode &in,vector<SplitVarnode> &splitvec);
static void getTrueFalse(PcodeOp *boolop,bool flip,BlockBasic *&trueout,BlockBasic *&falseout);
static bool otherwiseEmpty(PcodeOp *branchop);
- static bool verifyMultNegOne(PcodeOp *op);
static PcodeOp *prepareBinaryOp(SplitVarnode &out,SplitVarnode &in1,SplitVarnode &in2);
static void createBinaryOp(Funcdata &data,SplitVarnode &out,SplitVarnode &in1,SplitVarnode &in2,
PcodeOp *existop,OpCode opc);
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh
index f394e91ff1..2c6cecf885 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh
@@ -103,6 +103,7 @@ class Funcdata {
// Low level Varnode functions
void setVarnodeProperties(Varnode *vn) const; ///< Look-up boolean properties and data-type information
HighVariable *assignHigh(Varnode *vn); ///< Assign a new HighVariable to a Varnode
+ static void markIndirectAliasUpdate(Varnode *vn); ///< Inform defining INDIRECT of update in alias status for given Varnode
bool syncVarnodesWithSymbol(VarnodeLocSet::const_iterator &iter,uint4 fl,Datatype *ct);
bool descend2Undef(Varnode *vn); ///< Transform all reads of the given Varnode to a special \b undefined constant
@@ -503,6 +504,8 @@ public:
static int4 opFlipInPlaceTest(PcodeOp *op,vector<PcodeOp *> &fliplist,bool allowOpRemoval);
void opFlipInPlaceExecute(vector<PcodeOp *> &fliplist);
bool opNormalizeFlip(PcodeOp *cbranch);
+ void opCollapseIndirectsForCopy(PcodeOp *copyOp); ///< Convert INDIRECTs associated with the given COPY op
+ void opCollapseIndirectsForAlias(PcodeOp *effectOp); ///< Eliminate INDIRECTs for addresses with no alias
/// \brief Start of PcodeOp objects with the given op-code
list<PcodeOp *>::const_iterator beginOp(OpCode opc) const { return obank.begin(opc); }
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_op.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_op.cc
index bf79e64352..e791f4be75 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_op.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_op.cc
@@ -1278,6 +1278,171 @@ bool Funcdata::opNormalizeFlip(PcodeOp *cbranch)
return true;
}
+/// The op is assumed to be a recent STORE converted to a COPY. The INDIRECTs associated with
+/// the old STORE are converted to a COPY or SUBPIECE, depending on the overlap of the
+/// INDIRECT output with the COPY output.
+/// \param copyOp is the new COPY converted from a STORE
+void Funcdata::opCollapseIndirectsForCopy(PcodeOp *copyOp)
+
+{
+ BlockBasic *bb = copyOp->getParent();
+ list<PcodeOp *>::iterator iter = copyOp->getBasicIter();
+ while(iter != bb->beginOp()) {
+ list<PcodeOp *>::iterator previter = iter;
+ --previter;
+ PcodeOp *op = *previter;
+ if (op->code() != CPUI_INDIRECT) break;
+ Varnode *vn1 = copyOp->getOut();
+ Varnode *vn2 = op->getOut();
+ int4 res = vn1->characterizeOverlap(*vn2);
+ if (res > 0) { // Copy has an effect of some sort
+ if (res != 2 && vn1->contains(*vn2) == 0) { // INDIRECT output is properly contained in COPY output
+ // Convert INDIRECT to a SUBPIECE
+ uintb trunc;
+ if (vn1->getSpace()->isBigEndian())
+ trunc = vn1->getOffset() + vn1->getSize() - (vn2->getOffset() + vn2->getSize());
+ else
+ trunc = vn2->getOffset() - vn1->getOffset();
+ opUninsert(op);
+ opSetInput(op,vn1,0);
+ opSetInput(op,newConstant(4,trunc),1);
+ opSetOpcode(op, CPUI_SUBPIECE);
+ opInsertAfter(op, copyOp);
+ continue;
+ }
+ if (res != 2) {
+ Varnode *invn = op->getIn(0);
+ PcodeOp *insertPoint = copyOp;
+ int4 bytesBefore = 0;
+ if (vn2->getOffset() < vn1->getOffset())
+ bytesBefore = vn1->getOffset() - vn2->getOffset();
+ uintb vn2end = vn2->getOffset() + vn2->getSize()-1;
+ uintb vn1end = vn1->getOffset() + vn1->getSize()-1;
+ int4 bytesAfter = 0;
+ if (vn2end > vn1end)
+ bytesAfter = vn2end - vn1end;
+ int4 overlap = vn2->getSize() - bytesBefore - bytesAfter;
+ Varnode *frontVn = (Varnode *)0;
+ Varnode *backVn = (Varnode *)0;
+ Varnode *otherPiece;
+ int4 frontSlot = (vn2->getSpace()->isBigEndian()) ? 0 : 1;
+ if (bytesBefore != 0) {
+ int4 byteOff = (vn2->getSpace()->isBigEndian()) ? vn2->getSize() - bytesBefore : 0;
+ PcodeOp *subBefore = newOp(2,op->getAddr());
+ opSetOpcode(subBefore,CPUI_SUBPIECE);
+ frontVn = newVarnodeOut(bytesAfter,vn2->getAddr(),subBefore);
+ opSetInput(subBefore,invn,0);
+ opSetInput(subBefore,newConstant(4,byteOff),1);
+ opInsertAfter(subBefore, insertPoint);
+ insertPoint = subBefore;
+ }
+ if (bytesAfter != 0) {
+ int4 byteOff = (vn2->getSpace()->isBigEndian()) ? 0 : vn2->getSize() - bytesAfter;
+ PcodeOp *subAfter = newOp(2,op->getAddr());
+ opSetOpcode(subAfter,CPUI_SUBPIECE);
+ Address addr = vn2->getAddr() + (vn2->getSize() - bytesAfter);
+ backVn = newVarnodeOut(bytesAfter,addr,subAfter);
+ opSetInput(subAfter,invn,0);
+ opSetInput(subAfter,newConstant(4,byteOff),1);
+ opInsertAfter(subAfter,insertPoint);
+ insertPoint = subAfter;
+ }
+ if (overlap != vn1->getSize()) {
+ int4 byteOff;
+ if (bytesAfter == 0)
+ byteOff = (vn1->getSpace()->isBigEndian()) ? vn1->getSize() - overlap : 0;
+ else
+ byteOff = (vn1->getSpace()->isBigEndian()) ? 0 : vn1->getSize() - overlap;
+ PcodeOp *subMiddle = newOp(2,op->getAddr());
+ opSetOpcode(subMiddle,CPUI_SUBPIECE);
+ Address addr = vn2->getAddr() + bytesBefore;
+ otherPiece = newVarnodeOut(overlap,addr,subMiddle);
+ opSetInput(subMiddle,vn1,0);
+ opSetInput(subMiddle,newConstant(4,byteOff),1);
+ opInsertAfter(subMiddle,insertPoint);
+ insertPoint = subMiddle;
+ }
+ else {
+ otherPiece = vn1;
+ }
+ if (bytesBefore == 0) {
+ frontVn = otherPiece;
+ }
+ else if (bytesAfter == 0) {
+ backVn = otherPiece;
+ }
+ else {
+ PcodeOp *concat = newOp(2,op->getAddr());
+ opSetOpcode(concat,CPUI_PIECE);
+ Varnode *newVn = newVarnodeOut(frontVn->getSize() + otherPiece->getSize(),vn2->getAddr(),concat);
+ opSetInput(concat,frontVn,frontSlot);
+ opSetInput(concat,otherPiece,1-frontSlot);
+ opInsertAfter(concat,insertPoint);
+ insertPoint = concat;
+ frontVn = newVn;
+ }
+ opUninsert(op);
+ opSetOpcode(op, CPUI_PIECE);
+ opSetInput(op,frontVn,frontSlot);
+ opSetInput(op,backVn,1-frontSlot);
+ opInsertAfter(op, insertPoint);
+ continue;
+ }
+ // Convert INDIRECT to COPY
+ opUninsert(op);
+ opSetInput(op,vn1,0);
+ opRemoveInput(op,1);
+ opSetOpcode(op,CPUI_COPY);
+ opInsertAfter(op, copyOp);
+ }
+ else {
+ totalReplace(op->getOut(),op->getIn(0));
+ opDestroy(op); // Get rid of the INDIRECT
+ }
+ }
+}
+
+/// For the given op with indirect effects, run through its INDIRECTs, and
+/// if there is no longer a possible alias for an INDIRECT address, remove the INDIRECT.
+/// \param effectOp is the given op with indirect effects
+void Funcdata::opCollapseIndirectsForAlias(PcodeOp *effectOp)
+
+{
+ effectOp->clearAdditionalFlag(PcodeOp::store_aliasupdate);
+ BlockBasic *bb = effectOp->getParent();
+ list<PcodeOp *>::iterator iter = effectOp->getBasicIter();
+ if (iter == bb->beginOp()) return;
+ --iter;
+ const LoadGuard *guard = (const LoadGuard *)0;
+ if (effectOp->usesSpacebasePtr() && effectOp->code() == CPUI_STORE)
+ guard = getStoreGuard(effectOp);
+ for(;;) {
+ PcodeOp *op = *iter;
+ if (op->code() != CPUI_INDIRECT) break;
+ bool shouldDestroy = false;
+ if (op->getOut()->hasNoLocalAlias() && !op->isIndirectCreation() && !op->noIndirectCollapse())
+ shouldDestroy = true;
+ else if (guard != (const LoadGuard *)0 && !guard->isGuarded(op->getOut()->getAddr()))
+ shouldDestroy = true;
+
+ if (shouldDestroy) {
+ totalReplace(op->getOut(),op->getIn(0));
+ if (iter == bb->beginOp()) {
+ opDestroy(op); // Get rid of the INDIRECT
+ break;
+ }
+ else {
+ --iter;
+ opDestroy(op);
+ }
+ }
+ else if (iter == bb->beginOp())
+ break;
+ else
+ --iter;
+ }
+}
+
/// \brief Find a duplicate calculation of a given PcodeOp reading a specific Varnode
///
/// We only match 1 level of calculation. Additionally the duplicate must occur in the
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_varnode.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_varnode.cc
index 077d687050..9b650aa50c 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_varnode.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_varnode.cc
@@ -1021,6 +1021,19 @@ bool Funcdata::syncVarnodesWithSymbols(const ScopeLocal *lm,bool updateDatatypes
return updateoccurred;
}
+/// If the Varnode is defined by an INDIRECT, mark PcodeOp causing the effect as having updated alias information
+/// \param vn is the given Varnode
+void Funcdata::markIndirectAliasUpdate(Varnode *vn)
+
+{
+ PcodeOp *indop = vn->def;
+ if (indop == (PcodeOp *)0) return;
+ if (indop->code() != CPUI_INDIRECT) return;
+ if (indop->getIn(1)->getSpace()->getType()!=IPTR_IOP) return ;
+ PcodeOp *effectOp = PcodeOp::getOpFromConst(indop->getIn(1)->getAddr());
+ effectOp->setAliasUpdate();
+}
+
/// \brief Update properties (and the data-type) for a set of Varnodes associated with one Symbol
///
/// The set of Varnodes with the same size and address all have their boolean properties
@@ -1072,12 +1085,14 @@ bool Funcdata::syncVarnodesWithSymbol(VarnodeLocSet::const_iterator &iter,uint4
updateoccurred = true;
vn->setFlags(localFlags);
vn->clearFlags((~localFlags)&localMask);
+ markIndirectAliasUpdate(vn);
}
}
else if ((vnflags & mask) != fl) { // We have a change
updateoccurred = true;
vn->setFlags(fl);
vn->clearFlags((~fl)&mask);
+ markIndirectAliasUpdate(vn);
}
if (ct != (Datatype *)0) {
if (vn->updateType(ct))
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc
index 0f73153c06..8b0d8b3326 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc
@@ -882,6 +882,7 @@ void Heritage::analyzeNewLoadGuards(void)
for(iter=storeIter;iter!=storeGuard.end(); ++iter) {
LoadGuard &guard( *iter );
guard.establishRange(vsSolver.getValueSetRead(guard.op->getSeqNum()));
+ guard.op->setAliasUpdate(); // Mark that alias information for the STORE has been changed
if (guard.analysisState == 0)
runFullAnalysis = true;
}
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/op.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/op.cc
index f1dd827b19..547d87fa36 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/op.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/op.cc
@@ -819,6 +819,18 @@ int4 PcodeOp::compareOrder(const PcodeOp *bop) const
return 0;
}
+/// This must be a CPUI_INT_MULT and the second operand must be a constant -1.
+/// \return \b true if \b this multipies by -1
+bool PcodeOp::verifyMultNegOne(void) const
+
+{
+ if (opcode->getOpcode() != CPUI_INT_MULT) return false;
+ const Varnode *in1 = getIn(1);
+ if (!in1->isConstant()) return false;
+ if (in1->getOffset() != calc_mask(in1->getSize())) return false;
+ return true;
+}
+
/// \brief Determine if a Varnode is a leaf within the CONCAT tree rooted at the given Varnode
///
/// The CONCAT tree is the maximal set of Varnodes that are all inputs to CPUI_PIECE operations,
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/op.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/op.hh
index a6a793d868..a485fdd4ae 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/op.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/op.hh
@@ -117,7 +117,8 @@ public:
concat_root = 0x100, ///< Output of \b this is root of a CONCAT tree
no_indirect_collapse = 0x200, ///< Do not collapse \b this INDIRECT (via RuleIndirectCollapse)
store_unmapped = 0x400, ///< If STORE collapses to a stack Varnode, force it to be unmapped
- immed_copy = 0x800 ///< Copy has propagated into input of \b this op
+ immed_copy = 0x800, ///< Copy has propagated into input of \b this op
+ store_aliasupdate = 0x1000 ///< If alias information for \b this STORE has been changed
};
private:
TypeOp *opcode; ///< Pointer to class providing behavioral details of the operation
@@ -227,6 +228,8 @@ public:
void setStoreUnmapped(void) const { addlflags |= store_unmapped; } ///< Mark that STORE location should be unmapped
void setCopyImmed(int4 slot); ///< Mark that a COPY propagation from the immediate input block has happened
bool hasCopyImmed(int4 slot) const; ///< Return \b true if a COPY propagation from an immediate input block has happened
+ bool hasAliasUpdate(void) const { return ((addlflags & store_aliasupdate)!=0); } ///< Has alias information for \b this been updated
+ void setAliasUpdate(void) { addlflags |= store_aliasupdate; } ///< Mark that there is new alias information for \b this op
/// \brief Return \b true if this LOADs or STOREs from a dynamic \e spacebase pointer
bool usesSpacebasePtr(void) const { return ((flags&PcodeOp::spacebase_ptr)!=0); }
uintm getCseHash(void) const; ///< Return hash indicating possibility of common subexpression elimination
@@ -243,6 +246,7 @@ public:
PcodeOp *target(void) const; ///< Return starting op for instruction associated with this op
uintb getNZMaskLocal(bool cliploop) const; ///< Calculate known zero bits for output to this op
int4 compareOrder(const PcodeOp *bop) const; ///< Compare the control-flow order of this and \e bop
+ bool verifyMultNegOne(void) const; ///< Check if \b this is CPUI_INT_MULT by -1
void printRaw(ostream &s) const { opcode->printRaw(s,this); } ///< Print raw info about this op to stream
const string &getOpName(void) const { return opcode->getName(); } ///< Return the name of this op
void printDebug(ostream &s) const; ///< Print debug description of this op to stream
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc
index dd24476a0b..1a638ebcc7 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc
@@ -3164,80 +3164,22 @@ int4 RuleLogic2Bool::applyOp(PcodeOp *op,Funcdata &data)
return 1;
}
-/// \class RuleIndirectCollapse
-/// \brief Remove a CPUI_INDIRECT if its blocking PcodeOp is dead
-void RuleIndirectCollapse::getOpList(vector<uint4> &oplist) const
+/// \class RuleAliasUpdate
+/// \brief Collapse INDIRECTs when there is no longer a possible alias of its output
+void RuleAliasUpdate::getOpList(vector<uint4> &oplist) const
{
- oplist.push_back(CPUI_INDIRECT);
+ oplist.push_back(CPUI_STORE);
+ oplist.push_back(CPUI_CALL);
+ oplist.push_back(CPUI_CALLIND);
+ oplist.push_back(CPUI_CALLOTHER);
}
-int4 RuleIndirectCollapse::applyOp(PcodeOp *op,Funcdata &data)
+int4 RuleAliasUpdate::applyOp(PcodeOp *op,Funcdata &data)
{
- PcodeOp *indop;
-
- if (op->getIn(1)->getSpace()->getType()!=IPTR_IOP) return 0;
- indop = PcodeOp::getOpFromConst(op->getIn(1)->getAddr());
-
- // Is the indirect effect gone?
- if (!indop->isDead()) {
- if (indop->code() == CPUI_COPY) { // STORE resolved to a COPY
- Varnode *vn1 = indop->getOut();
- Varnode *vn2 = op->getOut();
- int4 res = vn1->characterizeOverlap(*vn2);
- if (res > 0) { // Copy has an effect of some sort
- if (res == 2) { // vn1 and vn2 are the same storage
- // Convert INDIRECT to COPY
- data.opUninsert(op);
- data.opSetInput(op,vn1,0);
- data.opRemoveInput(op,1);
- data.opSetOpcode(op,CPUI_COPY);
- data.opInsertAfter(op, indop);
- return 1;
- }
- if (vn1->contains(*vn2) == 0) { // INDIRECT output is properly contained in COPY output
- // Convert INDIRECT to a SUBPIECE
- uintb trunc;
- if (vn1->getSpace()->isBigEndian())
- trunc = vn1->getOffset() + vn1->getSize() - (vn2->getOffset() + vn2->getSize());
- else
- trunc = vn2->getOffset() - vn1->getOffset();
- data.opUninsert(op);
- data.opSetInput(op,vn1,0);
- data.opSetInput(op,data.newConstant(4,trunc),1);
- data.opSetOpcode(op, CPUI_SUBPIECE);
- data.opInsertAfter(op, indop);
- return 1;
- }
- data.warning("Ignoring partial resolution of indirect",indop->getAddr());
- return 0; // Partial overlap, not sure what to do
- }
- }
- else if (op->getOut()->hasNoLocalAlias()) {
- if (op->isIndirectCreation() || op->noIndirectCollapse())
- return 0;
- }
- else if (indop->usesSpacebasePtr()) {
- if (indop->code() == CPUI_STORE) {
- const LoadGuard *guard = data.getStoreGuard(indop);
- if (guard != (const LoadGuard *)0) {
- if (guard->isGuarded(op->getOut()->getAddr()))
- return 0;
- }
- else {
- // A marked STORE that is not guarded should eventually get converted to a COPY
- // so we keep the INDIRECT until that happens
- return 0;
- }
- }
- }
- else
- return 0;
- }
-
- data.totalReplace(op->getOut(),op->getIn(0));
- data.opDestroy(op); // Get rid of the INDIRECT
+ if (!op->hasAliasUpdate()) return 0;
+ data.opCollapseIndirectsForAlias(op);
return 1;
}
@@ -3786,6 +3728,24 @@ void RuleShiftPiece::getOpList(vector<uint4> &oplist) const
oplist.push_back(CPUI_INT_ADD);
}
+/// Check for both INT_LEFT and INT_MULT forms of multiplying by a constant power of 2.
+/// \param op is the op to check
+/// \return the power (shift amount) or -1 if the op does not match
+int4 RuleShiftPiece::multPowerOf2(PcodeOp *op)
+
+{
+ OpCode opc = op->code();
+ if (opc != CPUI_INT_LEFT && opc != CPUI_INT_MULT) return -1;
+ Varnode *cvn = op->getIn(1);
+ if (!cvn->isConstant()) return -1;
+ uintb val = cvn->getOffset();
+ if (opc == CPUI_INT_LEFT) return val;
+ int4 sa = leastsigbit_set(val);
+ if (sa >=0 && (val >> sa) == 1)
+ return sa;
+ return -1;
+}
+
int4 RuleShiftPiece::applyOp(PcodeOp *op,Funcdata &data)
{
@@ -3798,13 +3758,14 @@ int4 RuleShiftPiece::applyOp(PcodeOp *op,Funcdata &data)
if (!vn2->isWritten()) return 0;
shiftop = vn1->getDef();
zextloop = vn2->getDef();
- if (shiftop->code() != CPUI_INT_LEFT) {
- if (zextloop->code() != CPUI_INT_LEFT) return 0;
+ int4 sa = multPowerOf2(shiftop);
+ if (sa < 0) {
+ sa = multPowerOf2(zextloop);
+ if (sa < 0) return 0;
PcodeOp *tmpop = zextloop;
zextloop = shiftop;
shiftop = tmpop;
}
- if (!shiftop->getIn(1)->isConstant()) return 0;
vn1 = shiftop->getIn(0);
if (!vn1->isWritten()) return 0;
zexthiop = vn1->getDef();
@@ -3819,7 +3780,6 @@ int4 RuleShiftPiece::applyOp(PcodeOp *op,Funcdata &data)
}
else if (vn1->isFree())
return 0;
- int4 sa = shiftop->getIn(1)->getOffset();
int4 concatsize = sa + 8*vn1->getSize();
if (op->getOut()->getSize() * 8 < concatsize) return 0;
if (zextloop->code() != CPUI_INT_ZEXT) {
@@ -3941,40 +3901,44 @@ int4 RuleTransformCpool::applyOp(PcodeOp *op,Funcdata &data)
/// \class RulePropagateCopy
/// \brief Propagate the input of a COPY to all the places that read the output
-int4 RulePropagateCopy::applyOp(PcodeOp *op,Funcdata &data)
+void RulePropagateCopy::getOpList(vector<uint4> &oplist) const
{
- int4 i;
- PcodeOp *copyop;
- Varnode *vn,*invn;
+ oplist.push_back(CPUI_COPY);
+}
- if (op->isReturnCopy()) return 0;
- for(i=0;i<op->numInput();++i) {
- vn = op->getIn(i);
- if (!vn->isWritten()) continue; // Varnode must be written to
+int4 RulePropagateCopy::applyOp(PcodeOp *op,Funcdata &data)
- copyop = vn->getDef();
- if (copyop->code()!=CPUI_COPY)
- continue; // not a propagating instruction
-
- invn = copyop->getIn(0);
- if (!invn->isHeritageKnown()) continue; // Don't propagate free's away from their first use
- if (invn == vn)
- throw LowlevelError("Self-defined varnode");
- if (op->isMarker()) {
- if (invn->isConstant()) continue; // Don't propagate constants into markers
- if (vn->isAddrForce()) continue; // Don't propagate if we are keeping the COPY anyway
- if (invn->isAddrTied() && op->getOut()->isAddrTied() &&
- (op->getOut()->getAddr() != invn->getAddr()))
- continue; // We must not allow merging of different addrtieds
- if (op->code() == CPUI_MULTIEQUAL && copyop->getParent() == op->getParent()->getIn(i)) {
- op->setCopyImmed(i);
+{
+ Varnode *vn = op->getOut();
+ Varnode *invn = op->getIn(0);
+ if (invn == vn)
+ throw LowlevelError("Self-defined varnode");
+ if (!invn->isHeritageKnown()) return 0; // Don't propagate free's away from their first use
+ list<PcodeOp *>::const_iterator iter = vn->beginDescend();
+ int4 count = 0;
+ while(iter != vn->endDescend()) {
+ PcodeOp *otherOp = *iter;
+ ++iter;
+ if (otherOp->isReturnCopy()) continue;
+ for(int4 slot=0;slot<otherOp->numInput();++slot) {
+ if (vn != otherOp->getIn(slot)) continue;
+ if (otherOp->isMarker()) {
+ if (invn->isConstant()) continue; // Don't propagate constants into markers
+ if (vn->isAddrForce()) continue; // Don't propagate if we are keeping the COPY anyway
+ if (invn->isAddrTied() && otherOp->getOut()->isAddrTied() &&
+ (otherOp->getOut()->getAddr() != invn->getAddr()))
+ continue; // We must not allow merging of different addrtieds
+ if (otherOp->code() == CPUI_MULTIEQUAL && op->getParent() == otherOp->getParent()->getIn(slot)) {
+ otherOp->setCopyImmed(slot);
+ }
}
+ data.opSetInput(otherOp,invn,slot); // Propagate input of COPY into otherVn
+ count += 1;
+ break; // Allow at most one propagation per descendant
}
- data.opSetInput(op,invn,i); // otherwise propagate just a single copy
- return 1;
}
- return 0;
+ return count;
}
/// \class Rule2Comp2Mult
@@ -4358,6 +4322,7 @@ int4 RuleStoreVarnode::applyOp(PcodeOp *op,Funcdata &data)
if (op->isStoreUnmapped()) {
data.getScopeLocal()->markNotMapped(baseoff, offoff, size, false);
}
+ data.opCollapseIndirectsForCopy(op);
return 1;
}
@@ -4891,7 +4856,7 @@ int4 RuleZextShiftZext::applyOp(PcodeOp *op,Funcdata &data)
PcodeOp *shiftop = invn->getDef();
if (shiftop->code() == CPUI_INT_ZEXT) { // Check for ZEXT( ZEXT( a ) )
Varnode *vn = shiftop->getIn(0);
- if (vn->isFree()) return 0;
+ if (vn->isFree() && !vn->isConstant()) return 0;
if (invn->loneDescend() != op) // Only propagate if -op- is only use of -invn-
return 0;
data.opSetInput(op,vn,0);
@@ -4991,6 +4956,7 @@ int4 RuleConcatZero::applyOp(PcodeOp *op,Funcdata &data)
int4 sa = 8*op->getIn(1)->getSize();
Varnode *highvn = op->getIn(0);
+ if (highvn->isConstant() && highvn->getOffset() == 0) return 0;
PcodeOp *newop = data.newOp(1,op->getAddr());
Varnode *outvn = data.newUniqueOut(op->getOut()->getSize(),newop);
data.opSetOpcode(newop,CPUI_INT_ZEXT);
@@ -5872,7 +5838,7 @@ int4 RuleEqual2Zero::applyOp(PcodeOp *op,Funcdata &data)
{
Varnode *vn,*vn2,*addvn;
- Varnode *posvn,*negvn,*unnegvn;
+ Varnode *posvn,*unnegvn;
PcodeOp *addop;
vn = op->getIn(0);
@@ -5902,21 +5868,16 @@ int4 RuleEqual2Zero::applyOp(PcodeOp *op,Funcdata &data)
posvn = vn;
}
else {
- if ((vn->isWritten())&&(vn->getDef()->code()==CPUI_INT_MULT)) {
- negvn = vn;
+ if (vn->isWritten() && vn->getDef()->verifyMultNegOne()) {
posvn = vn2;
+ unnegvn = vn->getDef()->getIn(0);
}
- else if ((vn2->isWritten())&&(vn2->getDef()->code()==CPUI_INT_MULT)) {
- negvn = vn2;
+ else if (vn2->isWritten() && vn2->getDef()->verifyMultNegOne()) {
posvn = vn;
+ unnegvn = vn2->getDef()->getIn(0);
}
else
return 0;
- uintb multiplier;
- if (!negvn->getDef()->getIn(1)->isConstant()) return 0;
- unnegvn = negvn->getDef()->getIn(0);
- multiplier = negvn->getDef()->getIn(1)->getOffset();
- if (multiplier != calc_mask(unnegvn->getSize())) return 0;
}
if (!posvn->isHeritageKnown()) return 0;
if (!unnegvn->isHeritageKnown()) return 0;
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hh
index 6796597a3d..05ca0b4399 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hh
@@ -598,12 +598,12 @@ public:
virtual void getOpList(vector<uint4> &oplist) const;
virtual int4 applyOp(PcodeOp *op,Funcdata &data);
};
-class RuleIndirectCollapse : public Rule {
+class RuleAliasUpdate : public Rule {
public:
- RuleIndirectCollapse(const string &g) : Rule(g, 0, "indirectcollapse") {} ///< Constructor
+ RuleAliasUpdate(const string &g) : Rule(g, 0, "aliasupdate") {} ///< Constructor
virtual Rule *clone(const ActionGroupList &grouplist) const {
if (!grouplist.contains(getGroup())) return (Rule *)0;
- return new RuleIndirectCollapse(getGroup());
+ return new RuleAliasUpdate(getGroup());
}
virtual void getOpList(vector<uint4> &oplist) const;
virtual int4 applyOp(PcodeOp *op,Funcdata &data);
@@ -690,6 +690,7 @@ public:
virtual int4 applyOp(PcodeOp *op,Funcdata &data);
};
class RuleShiftPiece : public Rule {
+ static int4 multPowerOf2(PcodeOp *op); ///< Verify that op multiplies by a power of 2
public:
RuleShiftPiece(const string &g) : Rule(g, 0, "shiftpiece") {} ///< Constructor
virtual Rule *clone(const ActionGroupList &grouplist) const {
@@ -726,7 +727,7 @@ public:
if (!grouplist.contains(getGroup())) return (Rule *)0;
return new RulePropagateCopy(getGroup());
}
- // applies to all opcodes
+ virtual void getOpList(vector<uint4> &oplist) const;
virtual int4 applyOp(PcodeOp *op,Funcdata &data);
};
class Rule2Comp2Mult : public Rule {
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/subflow.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/subflow.cc
index 5410f863c8..4e9f9a2b5d 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/subflow.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/subflow.cc
@@ -3909,24 +3909,29 @@ bool LaneDivide::buildZext(PcodeOp *op,TransformVar *outVars,int4 numLanes,int4
{
int4 inLanes,inSkip;
Varnode *invn = op->getIn(0);
- if (!description.restriction(numLanes, skipLanes, 0, invn->getSize(), inLanes, inSkip)) {
- return false;
- }
+ if (!invn->isConstant() || invn->getOffset() != 0) {
+ if (!description.restriction(numLanes, skipLanes, 0, invn->getSize(), inLanes, inSkip)) {
+ return false;
+ }
// inSkip should always come back as equal to skipLanes
- if (inLanes == 1) {
- TransformOp *rop = newOpReplace(1, CPUI_COPY, op);
- TransformVar *inVar = getPreexistingVarnode(invn);
- opSetInput(rop,inVar,0);
- opSetOutput(rop,outVars);
- }
- else {
- TransformVar *inRvn = setReplacement(invn,inLanes,inSkip);
- if (inRvn == (TransformVar *)0) return false;
- for(int4 i=0;i<inLanes;++i) {
+ if (inLanes == 1) {
TransformOp *rop = newOpReplace(1, CPUI_COPY, op);
- opSetInput(rop,inRvn+i,0);
- opSetOutput(rop,outVars + i);
+ TransformVar *inVar = getPreexistingVarnode(invn);
+ opSetInput(rop,inVar,0);
+ opSetOutput(rop,outVars);
}
+ else {
+ TransformVar *inRvn = setReplacement(invn,inLanes,inSkip);
+ if (inRvn == (TransformVar *)0) return false;
+ for(int4 i=0;i<inLanes;++i) {
+ TransformOp *rop = newOpReplace(1, CPUI_COPY, op);
+ opSetInput(rop,inRvn+i,0);
+ opSetOutput(rop,outVars + i);
+ }
+ }
+ }
+ else {
+ inLanes = 0;
}
for(int4 i=0;i<numLanes-inLanes;++i) { // Write 0 constants to remaining lanes
TransformOp *rop = newOpReplace(1, CPUI_COPY, op);