Skip to content

Commit

Permalink
refactor, move the ownership to SVFFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
bjjwwang committed Jan 26, 2025
1 parent 342f37a commit 2dd6775
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 41 deletions.
25 changes: 13 additions & 12 deletions svf-llvm/include/SVF-LLVM/LLVMModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ class LLVMModuleSet
FunToFunEntryNodeMapTy FunToFunEntryNodeMap; ///< map a function to its FunExitICFGNode
FunToFunExitNodeMapTy FunToFunExitNodeMap; ///< map a function to its FunEntryICFGNode
CallGraph* callgraph;
BasicBlockGraph* basicBlockGraph;

Map<const Function*, DominatorTree> FunToDominatorTree;

Expand Down Expand Up @@ -250,19 +249,21 @@ class LLVMModuleSet

const Value* getLLVMValue(const SVFBaseNode* value) const
{
SVFBaseNode2LLVMValueMap ::const_iterator it = SVFBaseNode2LLVMValue.find(value);
assert(it!=SVFBaseNode2LLVMValue.end() && "can't find corresponding llvm value!");
return it->second;
}

const Value* getLLVMValue(const SVFBasicBlock* value) const
{
SVFBB2LLVMBBMap::const_iterator it = SVFBB2LLVMBB.find(value);
assert(it!=SVFBB2LLVMBB.end() && "can't find corresponding llvm value!");
return it->second;
if (value->getNodeKind() == SVFBaseNode::BasicBlockKd)
{
const SVFBasicBlock* bb = SVFUtil::cast<SVFBasicBlock>(value);
SVFBB2LLVMBBMap::const_iterator it = SVFBB2LLVMBB.find(bb);
assert(it!=SVFBB2LLVMBB.end() && "can't find corresponding llvm value!");
return it->second;
}
else
{
SVFBaseNode2LLVMValueMap ::const_iterator it = SVFBaseNode2LLVMValue.find(value);
assert(it != SVFBaseNode2LLVMValue.end() && "can't find corresponding llvm value!");
return it->second;
}
}


inline CallGraphNode* getCallGraphNode(const Function* fun) const
{
LLVMFun2CallGraphNodeMap::const_iterator it = LLVMFunc2CallGraphNode.find(fun);
Expand Down
8 changes: 3 additions & 5 deletions svf-llvm/lib/LLVMModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ LLVMModuleSet::~LLVMModuleSet()
item.second = nullptr;
}
delete typeInference;
delete basicBlockGraph;
typeInference = nullptr;
}

Expand Down Expand Up @@ -171,8 +170,6 @@ void LLVMModuleSet::build()
if(preProcessed==false)
prePassSchedule();

basicBlockGraph = new BasicBlockGraph();

buildFunToFunMap();
buildGlobalDefToRepMap();

Expand Down Expand Up @@ -283,6 +280,8 @@ void LLVMModuleSet::createSVFFunction(const Function* func)
getSVFType(func->getFunctionType())),
func->isDeclaration(), LLVMUtil::isIntrinsicFun(func),
func->hasAddressTaken(), func->isVarArg(), new SVFLoopAndDomInfo);
BasicBlockGraph* bbGraph = new BasicBlockGraph(svfFunc);
svfFunc->setBasicBlockGraph(bbGraph);
svfModule->addFunctionSet(svfFunc);
addFunctionMap(func, svfFunc);

Expand All @@ -301,8 +300,7 @@ void LLVMModuleSet::createSVFFunction(const Function* func)

for (const BasicBlock& bb : *func)
{
SVFBasicBlock* svfBB = basicBlockGraph->addBasicBlockToFunction(bb.getName().str(), svfFunc);
svfFunc->addBasicBlock(svfBB);
SVFBasicBlock* svfBB = svfFunc->addBasicBlock(bb.getName().str());
addBasicBlockMap(&bb, svfBB);
for (const Instruction& inst : bb)
{
Expand Down
9 changes: 5 additions & 4 deletions svf/include/Graphs/BasicBlockG.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,22 +258,23 @@ class BasicBlockGraph: public GenericBasicBlockGraphTy
{
private:
NodeID id{0};
const SVFFunction* fun;
public:
/// Constructor
BasicBlockGraph() {
BasicBlockGraph(const SVFFunction* f): fun(f)
{

}

SVFBasicBlock* addBasicBlockToFunction(const std::string& bbname, const SVFFunction* f)
SVFBasicBlock* addBasicBlock(const std::string& bbname)
{
id++;
SVFBasicBlock* bb = new SVFBasicBlock(id, f);
SVFBasicBlock* bb = new SVFBasicBlock(id, fun);
addGNode(id, bb);
bb->setName(bbname);
return bb;
}


};
}

Expand Down
38 changes: 25 additions & 13 deletions svf/include/SVFIR/SVFValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ class SVFFunction : public SVFValue
friend class SVFIRBuilder;

public:
typedef std::vector<const SVFBasicBlock*>::const_iterator const_iterator;
typename BasicBlockGraph::IDToNodeMapTy::iterator iterator;
typedef BasicBlockGraph::IDToNodeMapTy::const_iterator const_iterator;
typedef SVFLoopAndDomInfo::BBSet BBSet;
typedef SVFLoopAndDomInfo::BBList BBList;
typedef SVFLoopAndDomInfo::LoopBBs LoopBBs;
Expand All @@ -315,21 +316,21 @@ class SVFFunction : public SVFValue
const SVFFunctionType* funcType; /// FunctionType, which is different from the type (PointerType) of this SVFFunction
SVFLoopAndDomInfo* loopAndDom; /// the loop and dominate information
const SVFFunction* realDefFun; /// the definition of a function across multiple modules
std::vector<const SVFBasicBlock*> allBBs; /// all BasicBlocks of this function
std::vector<const SVFArgument*> allArgs; /// all formal arguments of this function
SVFBasicBlock *exitBlock; /// a 'single' basic block having no successors and containing return instruction in a function
const CallGraphNode *callGraphNode; /// call graph node for this function
BasicBlockGraph* bbGraph; /// the basic block graph of this function

protected:
inline void setCallGraphNode(CallGraphNode *cgn)
{
callGraphNode = cgn;
}

///@{ attributes to be set only through Module builders e.g., LLVMModule
inline void addBasicBlock(const SVFBasicBlock* bb)
{
allBBs.push_back(bb);

inline SVFBasicBlock* addBasicBlock(const std::string& bbName) {
SVFBasicBlock* bb = bbGraph->addBasicBlock(bbName);
return bb;
}

inline void addArgument(SVFArgument* arg)
Expand Down Expand Up @@ -377,6 +378,11 @@ class SVFFunction : public SVFValue
return isDecl;
}

void setBasicBlockGraph(BasicBlockGraph* graph)
{
this->bbGraph = graph;
}

inline bool isIntrinsic() const
{
return intrinsic;
Expand Down Expand Up @@ -412,13 +418,13 @@ class SVFFunction : public SVFValue

inline bool hasBasicBlock() const
{
return !allBBs.empty();
return bbGraph && bbGraph->getTotalNodeNum()>0;
}

inline const SVFBasicBlock* getEntryBlock() const
{
assert(hasBasicBlock() && "function does not have any Basicblock, external function?");
return allBBs.front();
return bbGraph->begin()->second;
}

/// Carefully! when you call getExitBB, you need ensure the function has return instruction
Expand All @@ -438,22 +444,28 @@ class SVFFunction : public SVFValue
/// Carefully! 'back' is just the last basic block of function,
/// but not necessarily a exit basic block
/// more refer to: https://github.com/SVF-tools/SVF/pull/1262
return allBBs.back();
return std::prev(bbGraph->end())->second;
}

inline const_iterator begin() const
{
return allBBs.begin();
return bbGraph->begin();
}

inline const_iterator end() const
{
return allBBs.end();
return bbGraph->end();
}

inline const std::vector<const SVFBasicBlock*>& getBasicBlockList() const
inline std::vector<const SVFBasicBlock*> getBasicBlockList() const
{
return allBBs;
std::vector<const SVFBasicBlock*> blockList;
blockList.reserve(bbGraph->getTotalNodeNum());
std::transform(bbGraph->begin(), bbGraph->end(), std::back_inserter(blockList),
[](const BasicBlockGraph::IDToNodeMapTy::value_type& pair) {
return pair.second;
});
return blockList;
}

inline const std::vector<const SVFBasicBlock*>& getReachableBBs() const
Expand Down
2 changes: 1 addition & 1 deletion svf/lib/MSSA/MemRegion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void MRGenerator::collectModRefForLoadStore()
for (SVFFunction::const_iterator iter = fun.begin(), eiter = fun.end();
iter != eiter; ++iter)
{
const SVFBasicBlock* bb = *iter;
const SVFBasicBlock* bb = iter->second;
for (const auto& inst: bb->getICFGNodeList())
{
SVFStmtList& pagEdgeList = getPAGEdgesFromInst(inst);
Expand Down
2 changes: 1 addition & 1 deletion svf/lib/MSSA/MemSSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ void MemSSA::dumpMSSA(OutStream& Out)
for (SVFFunction::const_iterator bit = fun->begin(), ebit = fun->end();
bit != ebit; ++bit)
{
const SVFBasicBlock* bb = *bit;
const SVFBasicBlock* bb = bit->second;
Out << bb->getName() << "\n";
PHISet& phiSet = getPHISet(bb);
for(PHISet::iterator pi = phiSet.begin(), epi = phiSet.end(); pi !=epi; ++pi)
Expand Down
2 changes: 1 addition & 1 deletion svf/lib/MTA/MTAStat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void MTAStat::performMHPPairStat(MHP* mhp, LockAnalysis* lsa)
continue;
for (SVFFunction::const_iterator bit = fun->begin(), ebit = fun->end(); bit != ebit; ++bit)
{
const SVFBasicBlock* bb = *bit;
const SVFBasicBlock* bb = bit->second;
for (const auto& icfgNode : bb->getICFGNodeList())
{
for(const SVFStmt* stmt : pag->getSVFStmtList(icfgNode))
Expand Down
2 changes: 1 addition & 1 deletion svf/lib/SABER/SaberCondAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void SaberCondAllocator::allocate(const SVFModule *M)
for (SVFFunction::const_iterator bit = func->begin(), ebit = func->end();
bit != ebit; ++bit)
{
const SVFBasicBlock* bb = *bit;
const SVFBasicBlock* bb = bit->second;
collectBBCallingProgExit(*bb);
allocateForBB(*bb);
}
Expand Down
1 change: 1 addition & 0 deletions svf/lib/SVFIR/SVFValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ SVFFunction::~SVFFunction()
for(const SVFArgument* arg : allArgs)
delete arg;
delete loopAndDom;
delete bbGraph;
}

u32_t SVFFunction::arg_size() const
Expand Down
3 changes: 2 additions & 1 deletion svf/lib/Util/CDGBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ void CDGBuilder::buildControlDependence(const SVFModule *svfgModule)
void CDGBuilder::extractBBS(const SVF::SVFFunction *func,
Map<const SVF::SVFBasicBlock *, std::vector<const SVFBasicBlock *>> &res)
{
for (const auto &bb: *func)
for (const auto &it: *func)
{
const SVFBasicBlock* bb = it.second;
for (const auto &succ: bb->getSuccessors())
{
if (func->postDominate(succ, bb))
Expand Down
2 changes: 1 addition & 1 deletion svf/lib/Util/SVFStat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ void SVFStat::branchStat()
for (SVFFunction::const_iterator bbIt = func->begin(), bbEit = func->end();
bbIt != bbEit; ++bbIt)
{
const SVFBasicBlock* bb = *bbIt;
const SVFBasicBlock* bb = bbIt->second;
u32_t numOfSucc = bb->getNumSuccessors();
if (numOfSucc == 2)
numOfBB_2Succ++;
Expand Down
2 changes: 1 addition & 1 deletion svf/lib/Util/ThreadAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ void ThreadAPI::performAPIStat(SVFModule* module)
{
for (SVFFunction::const_iterator bit = (item.second)->getFunction()->begin(), ebit = (item.second)->getFunction()->end(); bit != ebit; ++bit)
{
const SVFBasicBlock* bb = *bit;
const SVFBasicBlock* bb = bit->second;
for (const auto& svfInst: bb->getICFGNodeList())
{
if (!SVFUtil::isCallSite(svfInst))
Expand Down

0 comments on commit 2dd6775

Please sign in to comment.