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

add process unreachable bbs #1619

Merged
merged 4 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion svf-llvm/include/SVF-LLVM/ICFGBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class ICFGBuilder
///@{
void processFunEntry(const Function* fun, WorkList& worklist);

void processNoPrecessorBasicBlocks(const Function* fun, WorkList& worklist);
void processUnreachableFromEntry(const Function* fun, WorkList& worklist);

void processFunBody(WorkList& worklist);

Expand Down
4 changes: 4 additions & 0 deletions svf-llvm/include/SVF-LLVM/LLVMModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class LLVMModuleSet
FunToFunExitNodeMapTy FunToFunExitNodeMap; ///< map a function to its FunEntryICFGNode
CallGraph* callgraph;

Map<const Function*, DominatorTree> FunToDominatorTree;

/// Constructor
LLVMModuleSet();

Expand Down Expand Up @@ -397,6 +399,8 @@ class LLVMModuleSet
return icfg;
}

DominatorTree& getDomTree(const Function* fun);

private:
/// Create SVFTypes
SVFType* addSVFTypeInfo(const Type* t);
Expand Down
23 changes: 11 additions & 12 deletions svf-llvm/lib/ICFGBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ ICFG* ICFGBuilder::build()
continue;
WorkList worklist;
processFunEntry(fun,worklist);
processNoPrecessorBasicBlocks(fun, worklist);
processUnreachableFromEntry(fun, worklist);
processFunBody(worklist);
processFunExit(fun);

Expand Down Expand Up @@ -122,21 +122,20 @@ void ICFGBuilder::processFunEntry(const Function* fun, WorkList& worklist)
}

/*!
* bbs with no predecessors
* bbs unreachable from function entry
*/
void ICFGBuilder::processNoPrecessorBasicBlocks(const Function* fun, WorkList& worklist)
void ICFGBuilder::processUnreachableFromEntry(const Function* fun, WorkList& worklist)
{
for (const auto& bb: *fun)
SVFLoopAndDomInfo* pInfo =
llvmModuleSet()->getSVFFunction(fun)->getLoopAndDomInfo();
for (const auto& bb : *fun)
{
for (const auto& inst: bb)
if (pInfo->isUnreachable(llvmModuleSet()->getSVFBasicBlock(&bb)) &&
!visited.count(&bb.front()))
{
if (LLVMUtil::isNoPrecessorBasicBlock(inst.getParent()) &&
!visited.count(&inst))
{
visited.insert(&inst);
(void)addBlockICFGNode(&inst);
worklist.push(&inst);
}
visited.insert(&bb.front());
(void)addBlockICFGNode(&bb.front());
worklist.push(&bb.front());
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions svf-llvm/lib/LLVMLoopAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ using namespace SVFUtil;
*/
void LLVMLoopAnalysis::buildLLVMLoops(SVFModule *mod, ICFG* icfg)
{
llvm::DominatorTree DT = llvm::DominatorTree();
std::vector<const Loop *> loop_stack;
for (Module& M : LLVMModuleSet::getLLVMModuleSet()->getLLVMModules())
{
Expand All @@ -59,7 +58,7 @@ void LLVMLoopAnalysis::buildLLVMLoops(SVFModule *mod, ICFG* icfg)
if (func->isDeclaration()) continue;
// do not analyze external call
if (SVFUtil::isExtCall(svffun)) continue;
DT.recalculate(const_cast<Function&>(*func));
llvm::DominatorTree& DT = LLVMModuleSet::getLLVMModuleSet()->getDomTree(func);
llvm::LoopInfoBase<llvm::BasicBlock, llvm::Loop> loopInfo;
std::vector<const Loop*> llvmLoops;
loopInfo.analyze(DT);
Expand Down
12 changes: 10 additions & 2 deletions svf-llvm/lib/LLVMModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ ObjTypeInference* LLVMModuleSet::getTypeInference()
return typeInference;
}

DominatorTree& LLVMModuleSet::getDomTree(const SVF::Function* fun)
{
auto it = FunToDominatorTree.find(fun);
if(it != FunToDominatorTree.end()) return it->second;
DominatorTree& dt = FunToDominatorTree[fun];
dt.recalculate(const_cast<Function&>(*fun));
return dt;
}

SVFModule* LLVMModuleSet::buildSVFModule(Module &mod)
{
LLVMModuleSet* mset = getLLVMModuleSet();
Expand Down Expand Up @@ -407,9 +416,8 @@ void LLVMModuleSet::initDomTree(SVFFunction* svffun, const Function* fun)
if (fun->isDeclaration())
return;
//process and stored dt & df
DominatorTree dt;
DominanceFrontier df;
dt.recalculate(const_cast<Function&>(*fun));
DominatorTree& dt = getDomTree(fun);
df.analyze(dt);
LoopInfo loopInfo = LoopInfo(dt);
PostDominatorTree pdt = PostDominatorTree(const_cast<Function&>(*fun));
Expand Down
3 changes: 1 addition & 2 deletions svf-llvm/lib/LLVMUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ void LLVMUtil::getFunReachableBBs (const Function* fun, std::vector<const SVFBas
{
assert(!SVFUtil::isExtCall(LLVMModuleSet::getLLVMModuleSet()->getSVFFunction(fun)) && "The calling function cannot be an external function.");
//initial DominatorTree
DominatorTree dt;
dt.recalculate(const_cast<Function&>(*fun));
DominatorTree& dt = LLVMModuleSet::getLLVMModuleSet()->getDomTree(fun);

Set<const BasicBlock*> visited;
std::vector<const BasicBlock*> bbVec;
Expand Down
Loading