Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExtAPI refactor #1174

Merged
merged 3 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor SVF ext api
1) optimize buildFuntoFunMap
2) change the condition of isCalledExtFunction()
  • Loading branch information
jiawei.wang committed Aug 22, 2023
commit dce53ec925fec759c21888600891cea27d31b7ad
20 changes: 14 additions & 6 deletions svf-llvm/include/SVF-LLVM/LLVMModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,22 @@ class LLVMModuleSet
SVFOtherValue* getSVFOtherValue(const Value* ov);

/// Remove unused function in extapi.bc module
bool isUsedExtFunction(Function* func)
{
bool isCalledExtFunction(Function* func)
{
/// check if a llvm Function is called.
auto isFunctionCalled = [](llvm::Function* F) {
for (auto& use : F->uses()) {
llvm::User* user = use.getUser();
if (llvm::isa<llvm::CallBase>(user)) {
return true;
}
}
return false;
};
/// if this function func defined in extapi.bc but never used in application code (without any corresponding declared functions).
if (func->getParent()->getName().str() == Options::ExtAPIInput()
if (func->getParent()->getName().str() == Options::ExtAPIInput()
&& !isFunctionCalled(func)
&& func->getName().str() != "svf__main"
&& func->getName().str() != "malloc"
&& func->getName().str() != "main"
&& func->getName().substr(0,4) != "sse_"
&& FunDefToDeclsMap.find(func) == FunDefToDeclsMap.end()
&& std::find(ExtFuncsVec.begin(), ExtFuncsVec.end(), func) == ExtFuncsVec.end())
{
Expand Down
43 changes: 25 additions & 18 deletions svf-llvm/lib/LLVMModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,16 @@ void LLVMModuleSet::createSVFDataStructure()
{
getSVFType(IntegerType::getInt8Ty(getContext()));
// Functions need to be retrieved in the order of insertion
// candidateDefs is the vector for all used defined functions
// candidateDecls is the vector for all used declared functions
std::vector<const Function*> candidateDefs, candidateDecls;
for (Module& mod : modules)
{
std::vector<Function*> removedFuncList;
/// Function
for (Function& func : mod.functions())
{
if (isUsedExtFunction(&func))
if (isCalledExtFunction(&func))
{
removedFuncList.push_back(&func);
}
Expand Down Expand Up @@ -797,26 +799,36 @@ void LLVMModuleSet::buildFunToFunMap()
OrderedSet<string> declNames, defNames, intersectNames;
typedef Map<string, const Function*> NameToFunDefMapTy;
typedef Map<string, Set<const Function*>> NameToFunDeclsMapTy;
const Function* main_ext = nullptr;
const Function* main_app = nullptr;
for (Module& mod : modules)
{
// extapi.bc functions
if (mod.getName().str() == Options::ExtAPIInput())
{
for (const Function& fun : mod.functions())
{
extFuncs.insert(&fun);
// Find overwrite functions in extapi.bc
std::vector<std::string> annotations = LLVMUtil::getFunAnnotations(&fun);
auto it = std::find_if(annotations.begin(), annotations.end(), [&](const std::string& annotation) {
return annotation.find("OVERWRITE") != std::string::npos;
});
if (it != annotations.end()) {
overwriteExtFuncs.insert(&fun);
// there is main declaration in ext bc, it should be mapped to
// main definition in app bc.
if (fun.getName().str() == "main")
{
funDecls.insert(&fun);
declNames.insert(fun.getName().str());
}
if (fun.getName().str() == "main") {
main_ext = &fun;
else
{
extFuncs.insert(&fun);
// Find overwrite functions in extapi.bc
std::vector<std::string> annotations =
LLVMUtil::getFunAnnotations(&fun);
auto it =
std::find_if(annotations.begin(), annotations.end(),
[&](const std::string& annotation) {
return annotation.find("OVERWRITE") !=
std::string::npos;
});
if (it != annotations.end())
{
overwriteExtFuncs.insert(&fun);
}
}
}
}
Expand All @@ -835,9 +847,6 @@ void LLVMModuleSet::buildFunToFunMap()
funDefs.insert(&fun);
defNames.insert(fun.getName().str());
}
if (fun.getName().str() == "main") {
main_app = &fun;
}
}
}
}
Expand Down Expand Up @@ -881,7 +890,6 @@ void LLVMModuleSet::buildFunToFunMap()
FunDeclToDefMap[fdecl] = mit->second;
}
}
FunDeclToDefMap[main_ext] = main_app;

/// Fun def --> decls
for (const Function* fdef : funDefs)
Expand All @@ -903,7 +911,6 @@ void LLVMModuleSet::buildFunToFunMap()
decls.push_back(decl);
}
}
FunDefToDeclsMap[main_app] = {main_ext};

/// App Func decl -> SVF extern Func def
for (const Function* fdecl : funDecls)
Expand Down