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 SVFBasicBlock. BasicBlockEdge, Graph #1639

Merged
merged 12 commits into from
Jan 27, 2025
31 changes: 26 additions & 5 deletions svf-llvm/include/SVF-LLVM/LLVMModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "SVFIR/SVFValue.h"
#include "SVFIR/SVFModule.h"
#include "Util/Options.h"
#include "Graphs/BasicBlockG.h"

namespace SVF
{
Expand All @@ -56,6 +57,7 @@ class LLVMModuleSet
typedef Map<const Function*, SVFFunction*> LLVMFun2SVFFunMap;
typedef Map<const Function*, CallGraphNode*> LLVMFun2CallGraphNodeMap;
typedef Map<const BasicBlock*, SVFBasicBlock*> LLVMBB2SVFBBMap;
typedef Map<const SVFBasicBlock*, const BasicBlock*> SVFBB2LLVMBBMap;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this map.

typedef Map<const Instruction*, SVFInstruction*> LLVMInst2SVFInstMap;
typedef Map<const Argument*, SVFArgument*> LLVMArgument2SVFArgumentMap;
typedef Map<const Constant*, SVFConstant*> LLVMConst2SVFConstMap;
Expand Down Expand Up @@ -92,6 +94,7 @@ class LLVMModuleSet
LLVMFun2SVFFunMap LLVMFunc2SVFFunc; ///< Map an LLVM Function to an SVF Function
LLVMFun2CallGraphNodeMap LLVMFunc2CallGraphNode; ///< Map an LLVM Function to an CallGraph Node
LLVMBB2SVFBBMap LLVMBB2SVFBB;
SVFBB2LLVMBBMap SVFBB2LLVMBB;
LLVMInst2SVFInstMap LLVMInst2SVFInst;
LLVMArgument2SVFArgumentMap LLVMArgument2SVFArgument;
LLVMConst2SVFConstMap LLVMConst2SVFConst;
Expand Down Expand Up @@ -180,8 +183,9 @@ class LLVMModuleSet
inline void addBasicBlockMap(const BasicBlock* bb, SVFBasicBlock* svfBB)
{
LLVMBB2SVFBB[bb] = svfBB;
setValueAttr(bb,svfBB);
SVFBB2LLVMBB[svfBB] = bb;
}

inline void addInstructionMap(const Instruction* inst, SVFInstruction* svfInst)
{
LLVMInst2SVFInst[inst] = svfInst;
Expand Down Expand Up @@ -245,9 +249,19 @@ 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;
if (value->getNodeKind() == SVFBaseNode::BasicBlockKd)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use isa<>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for reminding. I write the classof()

{
const SVFBasicBlock* bb = SVFUtil::cast<SVFBasicBlock>(value);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why we need two maps, SVFBB2LLVMBBMap and SVFBaseNode2LLVMValueMap?

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
Expand All @@ -264,13 +278,20 @@ class LLVMModuleSet
return it->second;
}

inline SVFBasicBlock* getSVFBasicBlock(const BasicBlock* bb) const
SVFBasicBlock* getSVFBasicBlock(const BasicBlock* bb)
{
LLVMBB2SVFBBMap::const_iterator it = LLVMBB2SVFBB.find(bb);
assert(it!=LLVMBB2SVFBB.end() && "SVF BasicBlock not found!");
return it->second;
}

const BasicBlock* getLLVMBasicBlock(const SVFBasicBlock* bb) const
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this method.

{
SVFBB2LLVMBBMap::const_iterator it = SVFBB2LLVMBB.find(bb);
assert(it!=SVFBB2LLVMBB.end() && "LLVM BasicBlock not found!");
return it->second;
}

inline SVFInstruction* getSVFInstruction(const Instruction* inst) const
{
LLVMInst2SVFInstMap::const_iterator it = LLVMInst2SVFInst.find(inst);
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 @@ -280,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 @@ -298,9 +300,7 @@ void LLVMModuleSet::createSVFFunction(const Function* func)

for (const BasicBlock& bb : *func)
{
SVFBasicBlock* svfBB =
new SVFBasicBlock(getSVFType(bb.getType()), svfFunc);
svfFunc->addBasicBlock(svfBB);
SVFBasicBlock* svfBB = svfFunc->addBasicBlock(bb.getName().str());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

directly use bbGraph->addBasicBlock(..)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use bbGraph->addBasicBlock?

addBasicBlockMap(&bb, svfBB);
for (const Instruction& inst : bb)
{
Expand Down Expand Up @@ -1365,8 +1365,6 @@ SVFValue* LLVMModuleSet::getSVFValue(const Value* value)
{
if (const Function* fun = SVFUtil::dyn_cast<Function>(value))
return getSVFFunction(fun);
else if (const BasicBlock* bb = SVFUtil::dyn_cast<BasicBlock>(value))
return getSVFBasicBlock(bb);
else if(const Instruction* inst = SVFUtil::dyn_cast<Instruction>(value))
return getSVFInstruction(inst);
else if (const Argument* arg = SVFUtil::dyn_cast<Argument>(value))
Expand Down
16 changes: 12 additions & 4 deletions svf-llvm/lib/LLVMUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -726,10 +726,6 @@ std::string SVFValue::toString() const
{
rawstr << "Function: " << fun->getName() << " ";
}
else if (const SVFBasicBlock* bb = SVFUtil::dyn_cast<SVFBasicBlock>(this))
{
rawstr << "BasicBlock: " << bb->getName() << " ";
}
else
{
auto llvmVal = LLVMModuleSet::getLLVMModuleSet()->getLLVMValue(this);
Expand All @@ -742,6 +738,18 @@ std::string SVFValue::toString() const
return rawstr.str();
}

const std::string SVFBasicBlock::toString() const
{
std::string str;
llvm::raw_string_ostream rawstr(str);
auto llvmVal = LLVMModuleSet::getLLVMModuleSet()->getLLVMValue(this);
if (llvmVal)
rawstr << " " << *llvmVal << " ";
else
rawstr << " No llvmVal found";
rawstr << this->getSourceLoc();
return rawstr.str();
}

const std::string SVFBaseNode::valueOnlyToString() const
{
Expand Down
6 changes: 2 additions & 4 deletions svf-llvm/lib/ObjTypeInference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,7 @@ Set<const Value *> &ObjTypeInference::bwfindAllocOfVar(const Value *var)
if (!callee->isDeclaration())
{
const SVFFunction *svfFunc = LLVMModuleSet::getLLVMModuleSet()->getSVFFunction(callee);
const BasicBlock* exitBB = SVFUtil::cast<BasicBlock>(LLVMModuleSet::getLLVMModuleSet()->getLLVMValue(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

roll back

svfFunc->getExitBB()));
const BasicBlock* exitBB = LLVMModuleSet::getLLVMModuleSet()->getLLVMBasicBlock(svfFunc->getExitBB());
const Value *pValue = &exitBB->back();
const auto *retInst = SVFUtil::dyn_cast<ReturnInst>(pValue);
ABORT_IFNOT(retInst && retInst->getReturnValue(), "not return inst?");
Expand Down Expand Up @@ -894,8 +893,7 @@ Set<const Value *> &ObjTypeInference::bwFindAllocOrClsNameSources(const Value *s
if (!callee->isDeclaration())
{
const SVFFunction *svfFunc = LLVMModuleSet::getLLVMModuleSet()->getSVFFunction(callee);
const BasicBlock* exitBB = SVFUtil::cast<BasicBlock>(LLVMModuleSet::getLLVMModuleSet()->getLLVMValue(
svfFunc->getExitBB()));
const BasicBlock* exitBB = LLVMModuleSet::getLLVMModuleSet()->getLLVMBasicBlock(svfFunc->getExitBB());
const Value *pValue = &exitBB->back();
const auto *retInst = SVFUtil::dyn_cast<ReturnInst>(pValue);
ABORT_IFNOT(retInst && retInst->getReturnValue(), "not return inst?");
Expand Down
Loading
Loading