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

make basic block the base class #1628

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 11 additions & 5 deletions svf-llvm/include/SVF-LLVM/LLVMModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class LLVMModuleSet
typedef Map<const Constant*, SVFConstant*> LLVMConst2SVFConstMap;
typedef Map<const Value*, SVFOtherValue*> LLVMValue2SVFOtherValueMap;
typedef Map<const SVFValue*, const Value*> SVFValue2LLVMValueMap;

typedef Map<const SVFBasicBlock*, const BasicBlock*> SVFBB2LLVMBBMap;
typedef Map<const SVFBaseNode*, const Value*> SVFBaseNode2LLVMValueMap;
typedef Map<const Type*, SVFType*> LLVMType2SVFTypeMap;
typedef Map<const Type*, StInfo*> Type2TypeInfoMap;
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 @@ -177,11 +180,8 @@ class LLVMModuleSet

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

inline void addBasicBlockMap(const BasicBlock* bb, SVFBasicBlock* svfBB)
{
LLVMBB2SVFBB[bb] = svfBB;
setValueAttr(bb,svfBB);
}
void addBasicBlockMap(const BasicBlock* bb, SVFBasicBlock* svfBB);

inline void addInstructionMap(const Instruction* inst, SVFInstruction* svfInst)
{
LLVMInst2SVFInst[inst] = svfInst;
Expand Down Expand Up @@ -250,6 +250,12 @@ class LLVMModuleSet
return it->second;
}

inline const BasicBlock* getBasicBlock(const SVFBasicBlock* bb) const {
SVFBB2LLVMBBMap::const_iterator it = SVFBB2LLVMBB.find(bb);
assert(it != SVFBB2LLVMBB.end() && "can't find corresponding llvm basic block!");
return it->second;
}

inline CallGraphNode* getCallGraphNode(const Function* fun) const
{
LLVMFun2CallGraphNodeMap::const_iterator it = LLVMFunc2CallGraphNode.find(fun);
Expand Down
10 changes: 8 additions & 2 deletions svf-llvm/lib/LLVMModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,15 @@ void LLVMModuleSet::addFunctionMap(const Function* func, CallGraphNode* svfFunc)
LLVMFunc2CallGraphNode[func] = svfFunc;
addToLLVMVal2SVFVarMap(func, svfFunc);
}
void LLVMModuleSet::addBasicBlockMap(const BasicBlock* bb, SVFBasicBlock* svfBB)
{
LLVMBB2SVFBB[bb] = svfBB;
SVFBB2LLVMBB[svfBB] = bb;
svfBB->setSourceLoc(LLVMUtil::getSourceLoc(bb));

if (bb->hasName())
svfBB->setName(bb->getName().str());
}
void LLVMModuleSet::setValueAttr(const Value* val, SVFValue* svfvalue)
{
SVFValue2LLVMValue[svfvalue] = val;
Expand Down Expand Up @@ -1364,8 +1372,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
4 changes: 0 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 Down
4 changes: 2 additions & 2 deletions svf-llvm/lib/ObjTypeInference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +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(
const BasicBlock* exitBB = SVFUtil::cast<BasicBlock>(LLVMModuleSet::getLLVMModuleSet()->getBasicBlock(
svfFunc->getExitBB()));
const Value *pValue = &exitBB->back();
const auto *retInst = SVFUtil::dyn_cast<ReturnInst>(pValue);
Expand Down Expand Up @@ -894,7 +894,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(
const BasicBlock* exitBB = SVFUtil::cast<BasicBlock>(LLVMModuleSet::getLLVMModuleSet()->getBasicBlock(
svfFunc->getExitBB()));
const Value *pValue = &exitBB->back();
const auto *retInst = SVFUtil::dyn_cast<ReturnInst>(pValue);
Expand Down
2 changes: 0 additions & 2 deletions svf/include/SVFIR/SVFFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,6 @@ class SVFIRWriter

cJSON* contentToJson(const SVFValue* value);
cJSON* contentToJson(const SVFFunction* value);
cJSON* contentToJson(const SVFBasicBlock* value);
cJSON* contentToJson(const SVFInstruction* value);
cJSON* contentToJson(const SVFCallInst* value);
cJSON* contentToJson(const SVFConstant* value);
Expand Down Expand Up @@ -1285,7 +1284,6 @@ class SVFIRReader
void virtFill(const cJSON*& fieldJson, SVFValue* value);
void fill(const cJSON*& fieldJson, SVFValue* value);
void fill(const cJSON*& fieldJson, SVFFunction* value);
void fill(const cJSON*& fieldJson, SVFBasicBlock* value);
void fill(const cJSON*& fieldJson, SVFInstruction* value);
void fill(const cJSON*& fieldJson, SVFCallInst* value);
void fill(const cJSON*& fieldJson, SVFConstant* value);
Expand Down
42 changes: 36 additions & 6 deletions svf/include/SVFIR/SVFValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@

class ICFGNode;

class SVFBasicBlock : public SVFValue
class SVFBasicBlock
{
friend class LLVMModuleSet;
friend class SVFIRWriter;
Expand All @@ -539,6 +539,10 @@
public:
typedef std::vector<const ICFGNode*>::const_iterator const_iterator;

protected:
const SVFType* type; ///< Type of this SVFValue
std::string name; ///< Short name of value for printing & debugging
std::string sourceLoc; ///< Source code information of this value
private:
std::vector<const ICFGNode*> allICFGNodes; ///< all ICFGNodes in this BasicBlock
std::vector<const SVFBasicBlock*> succBBs; ///< all successor BasicBlocks of this BasicBlock
Expand Down Expand Up @@ -570,12 +574,8 @@
/// Constructor without name
SVFBasicBlock(const SVFType* ty, const SVFFunction* f);
SVFBasicBlock() = delete;
~SVFBasicBlock() override;
virtual ~SVFBasicBlock() = default;

static inline bool classof(const SVFValue *node)
{
return node->getKind() == SVFBB;
}

inline const std::vector<const ICFGNode*>& getICFGNodeList() const
{
Expand Down Expand Up @@ -631,6 +631,36 @@
u32_t getBBSuccessorPos(const SVFBasicBlock* succbb) const;
u32_t getBBPredecessorPos(const SVFBasicBlock* succbb);
u32_t getBBPredecessorPos(const SVFBasicBlock* succbb) const;
inline const std::string &getName() const
{
return name;
}
inline void setName(const std::string& n)
{
name = n;
}
inline void setName(std::string&& n)
{
name = std::move(n);
}

inline virtual const SVFType* getType() const

Check warning on line 647 in svf/include/SVFIR/SVFValue.h

View check run for this annotation

Codecov / codecov/patch

svf/include/SVFIR/SVFValue.h#L647

Added line #L647 was not covered by tests
{
return type;

Check warning on line 649 in svf/include/SVFIR/SVFValue.h

View check run for this annotation

Codecov / codecov/patch

svf/include/SVFIR/SVFValue.h#L649

Added line #L649 was not covered by tests
}

inline virtual void setSourceLoc(const std::string& sourceCodeInfo)
{
sourceLoc = sourceCodeInfo;
}
inline virtual const std::string getSourceLoc() const

Check warning on line 656 in svf/include/SVFIR/SVFValue.h

View check run for this annotation

Codecov / codecov/patch

svf/include/SVFIR/SVFValue.h#L656

Added line #L656 was not covered by tests
{
return sourceLoc;

Check warning on line 658 in svf/include/SVFIR/SVFValue.h

View check run for this annotation

Codecov / codecov/patch

svf/include/SVFIR/SVFValue.h#L658

Added line #L658 was not covered by tests
}

inline const std::string toString() const {
return "BasicBlock: " + getName();
}
};

class SVFInstruction : public SVFValue
Expand Down
35 changes: 2 additions & 33 deletions svf/lib/SVFIR/SVFFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ static SVFValue* createSVFValue(SVFValue::GNodeK kind, const SVFType* type,
ABORT_MSG("Creation of RAW SVFValue isn't allowed");
case SVFValue::SVFFunc:
return new SVFFunction(type, {}, {}, {}, {}, {}, {});
case SVFValue::SVFBB:
return new SVFBasicBlock(type, {});
case SVFValue::SVFInst:
return new SVFInstruction(type, {}, {}, {});
case SVFValue::SVFCall:
Expand Down Expand Up @@ -184,7 +182,6 @@ cJSON* SVFIRWriter::virtToJson(const SVFValue* value)

CASE(SVFVal, SVFValue);
CASE(SVFFunc, SVFFunction);
CASE(SVFBB, SVFBasicBlock);
CASE(SVFInst, SVFInstruction);
CASE(SVFCall, SVFCallInst);
CASE(SVFGlob, SVFGlobalValue);
Expand Down Expand Up @@ -384,7 +381,6 @@ cJSON* SVFIRWriter::contentToJson(const ICFGNode* node)
{
cJSON* root = genericNodeToJson(node);
JSON_WRITE_FIELD(root, node, fun);
JSON_WRITE_FIELD(root, node, bb);
// TODO: Ensure this?
assert(node->VFGNodes.empty() && "VFGNodes list not empty?");
JSON_WRITE_FIELD(root, node, pagEdges);
Expand Down Expand Up @@ -564,19 +560,11 @@ cJSON* SVFIRWriter::contentToJson(const SVFFunction* value)
return root;
}

cJSON* SVFIRWriter::contentToJson(const SVFBasicBlock* value)
{
cJSON* root = contentToJson(static_cast<const SVFValue*>(value));
JSON_WRITE_FIELD(root, value, succBBs);
JSON_WRITE_FIELD(root, value, predBBs);
JSON_WRITE_FIELD(root, value, fun);
return root;
}


cJSON* SVFIRWriter::contentToJson(const SVFInstruction* value)
{
cJSON* root = contentToJson(static_cast<const SVFValue*>(value));
JSON_WRITE_FIELD(root, value, bb);
JSON_WRITE_FIELD(root, value, terminator);
JSON_WRITE_FIELD(root, value, ret);
return root;
Expand Down Expand Up @@ -656,7 +644,6 @@ cJSON* SVFIRWriter::contentToJson(const SVFStmt* edge)
{
cJSON* root = genericEdgeToJson(edge);
JSON_WRITE_FIELD(root, edge, value);
JSON_WRITE_FIELD(root, edge, basicBlock);
JSON_WRITE_FIELD(root, edge, icfgNode);
JSON_WRITE_FIELD(root, edge, edgeId);
return root;
Expand Down Expand Up @@ -1888,13 +1875,6 @@ void SVFIRReader::readJson(const cJSON* obj, SVFLoopAndDomInfo*& ldInfo)

ldInfo = new SVFLoopAndDomInfo();

JSON_READ_FIELD_FWD(field, ldInfo, reachableBBs);
JSON_READ_FIELD_FWD(field, ldInfo, dtBBsMap);
JSON_READ_FIELD_FWD(field, ldInfo, pdtBBsMap);
JSON_READ_FIELD_FWD(field, ldInfo, dfBBsMap);
JSON_READ_FIELD_FWD(field, ldInfo, bb2LoopMap);
JSON_READ_FIELD_FWD(field, ldInfo, bb2PdomLevel);
JSON_READ_FIELD_FWD(field, ldInfo, bb2PIdom);

ABORT_IFNOT(!field,
"Extra field in SVFLoopAndDomInfo: " << JSON_KEY(field));
Expand Down Expand Up @@ -2019,7 +1999,6 @@ void SVFIRReader::fill(const cJSON*& fieldJson, SVFStmt* stmt)
{
fill(fieldJson, static_cast<GenericPAGEdgeTy*>(stmt));
JSON_READ_FIELD_FWD(fieldJson, stmt, value);
JSON_READ_FIELD_FWD(fieldJson, stmt, basicBlock);
JSON_READ_FIELD_FWD(fieldJson, stmt, icfgNode);
JSON_READ_FIELD_FWD(fieldJson, stmt, edgeId);
}
Expand Down Expand Up @@ -2171,7 +2150,6 @@ void SVFIRReader::fill(const cJSON*& fieldJson, ICFGNode* node)
{
fill(fieldJson, static_cast<GenericICFGNodeTy*>(node));
JSON_READ_FIELD_FWD(fieldJson, node, fun);
JSON_READ_FIELD_FWD(fieldJson, node, bb);
// Skip VFGNodes as it is empty
JSON_READ_FIELD_FWD(fieldJson, node, pagEdges);
}
Expand Down Expand Up @@ -2309,7 +2287,6 @@ void SVFIRReader::virtFill(const cJSON*& fieldJson, SVFValue* value)

CASE(SVFVal, SVFValue);
CASE(SVFFunc, SVFFunction);
CASE(SVFBB, SVFBasicBlock);
CASE(SVFInst, SVFInstruction);
CASE(SVFCall, SVFCallInst);
CASE(SVFGlob, SVFGlobalValue);
Expand Down Expand Up @@ -2347,23 +2324,15 @@ void SVFIRReader::fill(const cJSON*& fieldJson, SVFFunction* value)
F(funcType);
F(loopAndDom);
F(realDefFun);
F(allBBs);
F(allArgs);
#undef F
}

void SVFIRReader::fill(const cJSON*& fieldJson, SVFBasicBlock* value)
{
fill(fieldJson, static_cast<SVFValue*>(value));
JSON_READ_FIELD_FWD(fieldJson, value, succBBs);
JSON_READ_FIELD_FWD(fieldJson, value, predBBs);
JSON_READ_FIELD_FWD(fieldJson, value, fun);
}


void SVFIRReader::fill(const cJSON*& fieldJson, SVFInstruction* value)
{
fill(fieldJson, static_cast<SVFValue*>(value));
JSON_READ_FIELD_FWD(fieldJson, value, bb);
JSON_READ_FIELD_FWD(fieldJson, value, terminator);
JSON_READ_FIELD_FWD(fieldJson, value, ret);
}
Expand Down
7 changes: 1 addition & 6 deletions svf/lib/SVFIR/SVFValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,10 @@ void SVFFunction::setExitBlock(SVFBasicBlock *bb)
exitBlock = bb;
}

SVFBasicBlock::SVFBasicBlock(const SVFType* ty, const SVFFunction* f)
: SVFValue(ty, SVFValue::SVFBB), fun(f)
SVFBasicBlock::SVFBasicBlock(const SVFType* ty, const SVFFunction* f): type(ty), fun(f)
{
}

SVFBasicBlock::~SVFBasicBlock()
{

}

/*!
* Get position of a successor basic block
Expand Down
Loading