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
12 changes: 8 additions & 4 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 Down Expand Up @@ -177,11 +178,14 @@ class LLVMModuleSet

void addFunctionMap(const Function* func, CallGraphNode* svfFunc);

inline void addBasicBlockMap(const BasicBlock* bb, SVFBasicBlock* svfBB)
// create a SVFBasicBlock according to LLVM BasicBlock, then add it to SVFFunction's BasicBlockGraph
inline void addBasicBlock(SVFFunction* fun, const BasicBlock* bb)
{
SVFBasicBlock* svfBB = fun->getBasicBlockGraph()->addBasicBlock(bb->getName().str());
LLVMBB2SVFBB[bb] = svfBB;
setValueAttr(bb,svfBB);
SVFBaseNode2LLVMValue[svfBB] = bb;
}

inline void addInstructionMap(const Instruction* inst, SVFInstruction* svfInst)
{
LLVMInst2SVFInst[inst] = svfInst;
Expand Down Expand Up @@ -246,7 +250,7 @@ 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!");
assert(it != SVFBaseNode2LLVMValue.end() && "can't find corresponding llvm value!");
return it->second;
}

Expand All @@ -264,7 +268,7 @@ 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!");
Expand Down
13 changes: 5 additions & 8 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,25 +300,22 @@ void LLVMModuleSet::createSVFFunction(const Function* func)

for (const BasicBlock& bb : *func)
{
SVFBasicBlock* svfBB =
new SVFBasicBlock(getSVFType(bb.getType()), svfFunc);
svfFunc->addBasicBlock(svfBB);
addBasicBlockMap(&bb, svfBB);
addBasicBlock(svfFunc, &bb);
for (const Instruction& inst : bb)
{
SVFInstruction* svfInst = nullptr;
if (const CallBase* call = SVFUtil::dyn_cast<CallBase>(&inst))
{
svfInst = new SVFCallInst(
getSVFType(call->getType()), svfBB,
getSVFType(call->getType()), getSVFBasicBlock(&bb),
call->getFunctionType()->isVarArg(),
inst.isTerminator());
}
else
{
svfInst =
new SVFInstruction(getSVFType(inst.getType()),
svfBB, inst.isTerminator(),
getSVFBasicBlock(&bb), inst.isTerminator(),
SVFUtil::isa<ReturnInst>(inst));
}

Expand Down Expand Up @@ -1365,8 +1364,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
8 changes: 4 additions & 4 deletions svf-llvm/lib/ObjTypeInference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,8 @@ 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 = SVFUtil::dyn_cast<BasicBlock>(LLVMModuleSet::getLLVMModuleSet()->getLLVMValue(svfFunc->getExitBB()));
Copy link
Collaborator

Choose a reason for hiding this comment

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

use cast here or you need to add an assertion

assert (exitBB && "exit bb is not a basic block?");
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 +894,8 @@ 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 = SVFUtil::dyn_cast<BasicBlock>(LLVMModuleSet::getLLVMModuleSet()->getLLVMValue(svfFunc->getExitBB()));
Copy link
Collaborator

Choose a reason for hiding this comment

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

see above

Copy link
Collaborator

Choose a reason for hiding this comment

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

const BasicBlock* exitBB = SVFUtil::dyn_cast(LLVMModuleSet::getLLVMModuleSet()->getLLVMValue(svfFunc->getExitBB()));
assert (exitBB && "exit bb is not a basic block?");

assert (exitBB && "exit bb is not a basic block?");
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