From efa158377fcc6b8ddd8115bcd0268dd198e52137 Mon Sep 17 00:00:00 2001 From: Xiao Date: Wed, 25 Dec 2024 16:38:27 +1100 Subject: [PATCH] add process unreachable bbs (#1619) to resolve issue #1618 * add process unreachable bbs * add process unreachable bbs * add process unreachable bbs * cache fun2domtree --- svf-llvm/include/SVF-LLVM/ICFGBuilder.h | 2 +- svf-llvm/include/SVF-LLVM/LLVMModule.h | 4 ++++ svf-llvm/lib/ICFGBuilder.cpp | 23 +++++++++++------------ svf-llvm/lib/LLVMLoopAnalysis.cpp | 3 +-- svf-llvm/lib/LLVMModule.cpp | 12 ++++++++++-- svf-llvm/lib/LLVMUtil.cpp | 3 +-- 6 files changed, 28 insertions(+), 19 deletions(-) diff --git a/svf-llvm/include/SVF-LLVM/ICFGBuilder.h b/svf-llvm/include/SVF-LLVM/ICFGBuilder.h index 1367568e2..6ce771c2a 100644 --- a/svf-llvm/include/SVF-LLVM/ICFGBuilder.h +++ b/svf-llvm/include/SVF-LLVM/ICFGBuilder.h @@ -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); diff --git a/svf-llvm/include/SVF-LLVM/LLVMModule.h b/svf-llvm/include/SVF-LLVM/LLVMModule.h index be1c14797..82d2cafec 100644 --- a/svf-llvm/include/SVF-LLVM/LLVMModule.h +++ b/svf-llvm/include/SVF-LLVM/LLVMModule.h @@ -109,6 +109,8 @@ class LLVMModuleSet FunToFunExitNodeMapTy FunToFunExitNodeMap; ///< map a function to its FunEntryICFGNode CallGraph* callgraph; + Map FunToDominatorTree; + /// Constructor LLVMModuleSet(); @@ -397,6 +399,8 @@ class LLVMModuleSet return icfg; } + DominatorTree& getDomTree(const Function* fun); + private: /// Create SVFTypes SVFType* addSVFTypeInfo(const Type* t); diff --git a/svf-llvm/lib/ICFGBuilder.cpp b/svf-llvm/lib/ICFGBuilder.cpp index 4dde63ae7..2a9f0939c 100644 --- a/svf-llvm/lib/ICFGBuilder.cpp +++ b/svf-llvm/lib/ICFGBuilder.cpp @@ -70,7 +70,7 @@ ICFG* ICFGBuilder::build() continue; WorkList worklist; processFunEntry(fun,worklist); - processNoPrecessorBasicBlocks(fun, worklist); + processUnreachableFromEntry(fun, worklist); processFunBody(worklist); processFunExit(fun); @@ -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()); } } } diff --git a/svf-llvm/lib/LLVMLoopAnalysis.cpp b/svf-llvm/lib/LLVMLoopAnalysis.cpp index 58b2fca62..c2a9d842e 100644 --- a/svf-llvm/lib/LLVMLoopAnalysis.cpp +++ b/svf-llvm/lib/LLVMLoopAnalysis.cpp @@ -48,7 +48,6 @@ using namespace SVFUtil; */ void LLVMLoopAnalysis::buildLLVMLoops(SVFModule *mod, ICFG* icfg) { - llvm::DominatorTree DT = llvm::DominatorTree(); std::vector loop_stack; for (Module& M : LLVMModuleSet::getLLVMModuleSet()->getLLVMModules()) { @@ -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(*func)); + llvm::DominatorTree& DT = LLVMModuleSet::getLLVMModuleSet()->getDomTree(func); llvm::LoopInfoBase loopInfo; std::vector llvmLoops; loopInfo.analyze(DT); diff --git a/svf-llvm/lib/LLVMModule.cpp b/svf-llvm/lib/LLVMModule.cpp index dc559368b..3cd05d0ac 100644 --- a/svf-llvm/lib/LLVMModule.cpp +++ b/svf-llvm/lib/LLVMModule.cpp @@ -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(*fun)); + return dt; +} + SVFModule* LLVMModuleSet::buildSVFModule(Module &mod) { LLVMModuleSet* mset = getLLVMModuleSet(); @@ -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(*fun)); + DominatorTree& dt = getDomTree(fun); df.analyze(dt); LoopInfo loopInfo = LoopInfo(dt); PostDominatorTree pdt = PostDominatorTree(const_cast(*fun)); diff --git a/svf-llvm/lib/LLVMUtil.cpp b/svf-llvm/lib/LLVMUtil.cpp index 10bde5152..315de6a36 100644 --- a/svf-llvm/lib/LLVMUtil.cpp +++ b/svf-llvm/lib/LLVMUtil.cpp @@ -75,8 +75,7 @@ void LLVMUtil::getFunReachableBBs (const Function* fun, std::vectorgetSVFFunction(fun)) && "The calling function cannot be an external function."); //initial DominatorTree - DominatorTree dt; - dt.recalculate(const_cast(*fun)); + DominatorTree& dt = LLVMModuleSet::getLLVMModuleSet()->getDomTree(fun); Set visited; std::vector bbVec;