Skip to content

Commit

Permalink
param_return: improve filtration (#155)
Browse files Browse the repository at this point in the history
Fixes #155

Sets of possible argments found in definition is filtered
by possible argments found in its calls only when there is only
one call in such definition.
  • Loading branch information
Peter Kubov committed Aug 1, 2020
1 parent 5c377e1 commit 9a13ab3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ class DataFlowEntry : public FunctionEntry

void setCalledValue(llvm::Value* called);

std::size_t numberOfCalls() const;
bool hasBranches() const;

// Usage data.
//
public:
Expand Down
32 changes: 32 additions & 0 deletions src/bin2llvmir/optimizations/param_return/data_entries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,38 @@ void DataFlowEntry::setCalledValue(llvm::Value* called)
_calledValue = called;
}

std::size_t DataFlowEntry::numberOfCalls() const
{
auto fnc = getFunction();
if (fnc == nullptr)
return 0;

std::size_t calls = 0;
for (auto& bb: *fnc)
for (auto& i: bb)
if (auto call = dyn_cast<CallInst>(&i)) {
auto* calledFnc = call->getCalledFunction();
if (calledFnc && !calledFnc->isIntrinsic())
calls++;
}

return calls;
}

bool DataFlowEntry::hasBranches() const
{
auto fnc = getFunction();
if (fnc == nullptr)
return false;

for (auto& bb: *fnc)
for (auto& i: bb)
if (isa<BranchInst>(i))
return true;

return false;
}

CallEntry* DataFlowEntry::createCallEntry(CallInst* call)
{
_calls.push_back(CallEntry(call, this));
Expand Down
12 changes: 10 additions & 2 deletions src/bin2llvmir/optimizations/param_return/filter/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ void Filter::filterCalls(DataFlowEntry* de) const
retTempl = callRets.front();
}

/* When there is definition available we should
* use arguments from there. This is more reliable than
* constructing intersations of used arguments in file.
*/
if (de->hasDefinition())
{
FilterableLayout defArgs;
Expand All @@ -191,12 +195,16 @@ void Filter::filterCalls(DataFlowEntry* de) const
// below.
filterArgsByKnownTypes(defArgs);
}
else if (de->args().empty())
else if (de->args().empty() && de->numberOfCalls() == 1 && !de->hasBranches())
{
// In this case it might be wrapper that
// takes arguments from call and do not modify them
// in definition.
filterCallArgsByDefLayout(defArgs, argTempl);
de->setArgs(createGroupedArgValues(defArgs));
}
else if (argTempl.stacks.size() > defArgs.stacks.size())
else if (argTempl.stacks.size() > defArgs.stacks.size()
&& de->numberOfCalls() == 1 && !de->hasBranches())
{
if (argTempl.gpRegisters.size() == defArgs.gpRegisters.size()
&& argTempl.fpRegisters.size() == defArgs.fpRegisters.size()
Expand Down
1 change: 1 addition & 0 deletions src/bin2llvmir/optimizations/param_return/param_return.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ void ParamReturn::dumpInfo(const DataFlowEntry& de) const
LOG << "\t>|config f : " << (configFnc != nullptr) << std::endl;
LOG << "\t>|debug f : " << (dbgFnc != nullptr) << std::endl;
LOG << "\t>|wrapp c : " << llvmObjToString(wrappedCall) << std::endl;
LOG << "\t>|calls cnt: " << de.numberOfCalls() << std::endl;
LOG << "\t>|type set : " << !de.argTypes().empty() << std::endl;
LOG << "\t>|ret type : " << llvmObjToString(de.getRetType()) << std::endl;
LOG << "\t>|ret value: " << llvmObjToString(de.getRetValue()) << std::endl;
Expand Down

0 comments on commit 9a13ab3

Please sign in to comment.