From a209a7bf98f58306aa72d187f8a3cfbb03e63c61 Mon Sep 17 00:00:00 2001 From: jumormt Date: Mon, 13 Jan 2025 11:42:17 +1100 Subject: [PATCH] make basic block the base class --- svf-llvm/include/SVF-LLVM/LLVMModule.h | 16 +++++++--- svf-llvm/lib/LLVMModule.cpp | 10 ++++-- svf-llvm/lib/LLVMUtil.cpp | 4 --- svf-llvm/lib/ObjTypeInference.cpp | 4 +-- svf/include/SVFIR/SVFFileSystem.h | 2 -- svf/include/SVFIR/SVFValue.h | 42 ++++++++++++++++++++++---- svf/lib/SVFIR/SVFFileSystem.cpp | 35 ++------------------- svf/lib/SVFIR/SVFValue.cpp | 7 +---- 8 files changed, 60 insertions(+), 60 deletions(-) diff --git a/svf-llvm/include/SVF-LLVM/LLVMModule.h b/svf-llvm/include/SVF-LLVM/LLVMModule.h index 5f5e3d7df..b10f8823e 100644 --- a/svf-llvm/include/SVF-LLVM/LLVMModule.h +++ b/svf-llvm/include/SVF-LLVM/LLVMModule.h @@ -61,6 +61,8 @@ class LLVMModuleSet typedef Map LLVMConst2SVFConstMap; typedef Map LLVMValue2SVFOtherValueMap; typedef Map SVFValue2LLVMValueMap; + + typedef Map SVFBB2LLVMBBMap; typedef Map SVFBaseNode2LLVMValueMap; typedef Map LLVMType2SVFTypeMap; typedef Map Type2TypeInfoMap; @@ -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; @@ -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; @@ -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); diff --git a/svf-llvm/lib/LLVMModule.cpp b/svf-llvm/lib/LLVMModule.cpp index 8a9b7e0ab..e371ad754 100644 --- a/svf-llvm/lib/LLVMModule.cpp +++ b/svf-llvm/lib/LLVMModule.cpp @@ -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; @@ -1364,8 +1372,6 @@ SVFValue* LLVMModuleSet::getSVFValue(const Value* value) { if (const Function* fun = SVFUtil::dyn_cast(value)) return getSVFFunction(fun); - else if (const BasicBlock* bb = SVFUtil::dyn_cast(value)) - return getSVFBasicBlock(bb); else if(const Instruction* inst = SVFUtil::dyn_cast(value)) return getSVFInstruction(inst); else if (const Argument* arg = SVFUtil::dyn_cast(value)) diff --git a/svf-llvm/lib/LLVMUtil.cpp b/svf-llvm/lib/LLVMUtil.cpp index 315de6a36..2ed78e8ad 100644 --- a/svf-llvm/lib/LLVMUtil.cpp +++ b/svf-llvm/lib/LLVMUtil.cpp @@ -726,10 +726,6 @@ std::string SVFValue::toString() const { rawstr << "Function: " << fun->getName() << " "; } - else if (const SVFBasicBlock* bb = SVFUtil::dyn_cast(this)) - { - rawstr << "BasicBlock: " << bb->getName() << " "; - } else { auto llvmVal = LLVMModuleSet::getLLVMModuleSet()->getLLVMValue(this); diff --git a/svf-llvm/lib/ObjTypeInference.cpp b/svf-llvm/lib/ObjTypeInference.cpp index b2b6a5bb8..00547ba5f 100644 --- a/svf-llvm/lib/ObjTypeInference.cpp +++ b/svf-llvm/lib/ObjTypeInference.cpp @@ -586,7 +586,7 @@ Set &ObjTypeInference::bwfindAllocOfVar(const Value *var) if (!callee->isDeclaration()) { const SVFFunction *svfFunc = LLVMModuleSet::getLLVMModuleSet()->getSVFFunction(callee); - const BasicBlock* exitBB = SVFUtil::cast(LLVMModuleSet::getLLVMModuleSet()->getLLVMValue( + const BasicBlock* exitBB = SVFUtil::cast(LLVMModuleSet::getLLVMModuleSet()->getBasicBlock( svfFunc->getExitBB())); const Value *pValue = &exitBB->back(); const auto *retInst = SVFUtil::dyn_cast(pValue); @@ -894,7 +894,7 @@ Set &ObjTypeInference::bwFindAllocOrClsNameSources(const Value *s if (!callee->isDeclaration()) { const SVFFunction *svfFunc = LLVMModuleSet::getLLVMModuleSet()->getSVFFunction(callee); - const BasicBlock* exitBB = SVFUtil::cast(LLVMModuleSet::getLLVMModuleSet()->getLLVMValue( + const BasicBlock* exitBB = SVFUtil::cast(LLVMModuleSet::getLLVMModuleSet()->getBasicBlock( svfFunc->getExitBB())); const Value *pValue = &exitBB->back(); const auto *retInst = SVFUtil::dyn_cast(pValue); diff --git a/svf/include/SVFIR/SVFFileSystem.h b/svf/include/SVFIR/SVFFileSystem.h index 102d91dd8..fe8362d58 100644 --- a/svf/include/SVFIR/SVFFileSystem.h +++ b/svf/include/SVFIR/SVFFileSystem.h @@ -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); @@ -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); diff --git a/svf/include/SVFIR/SVFValue.h b/svf/include/SVFIR/SVFValue.h index bd0f0690a..3b422e773 100644 --- a/svf/include/SVFIR/SVFValue.h +++ b/svf/include/SVFIR/SVFValue.h @@ -526,7 +526,7 @@ class SVFFunction : public SVFValue class ICFGNode; -class SVFBasicBlock : public SVFValue +class SVFBasicBlock { friend class LLVMModuleSet; friend class SVFIRWriter; @@ -539,6 +539,10 @@ class SVFBasicBlock : public SVFValue public: typedef std::vector::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 allICFGNodes; ///< all ICFGNodes in this BasicBlock std::vector succBBs; ///< all successor BasicBlocks of this BasicBlock @@ -570,12 +574,8 @@ class SVFBasicBlock : public SVFValue /// 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& getICFGNodeList() const { @@ -631,6 +631,36 @@ class SVFBasicBlock : public SVFValue 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 + { + return type; + } + + inline virtual void setSourceLoc(const std::string& sourceCodeInfo) + { + sourceLoc = sourceCodeInfo; + } + inline virtual const std::string getSourceLoc() const + { + return sourceLoc; + } + + inline const std::string toString() const { + return "BasicBlock: " + getName(); + } }; class SVFInstruction : public SVFValue diff --git a/svf/lib/SVFIR/SVFFileSystem.cpp b/svf/lib/SVFIR/SVFFileSystem.cpp index da7dd8681..4a4ef7d67 100644 --- a/svf/lib/SVFIR/SVFFileSystem.cpp +++ b/svf/lib/SVFIR/SVFFileSystem.cpp @@ -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: @@ -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); @@ -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); @@ -564,19 +560,11 @@ cJSON* SVFIRWriter::contentToJson(const SVFFunction* value) return root; } -cJSON* SVFIRWriter::contentToJson(const SVFBasicBlock* value) -{ - cJSON* root = contentToJson(static_cast(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(value)); - JSON_WRITE_FIELD(root, value, bb); JSON_WRITE_FIELD(root, value, terminator); JSON_WRITE_FIELD(root, value, ret); return root; @@ -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; @@ -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)); @@ -2019,7 +1999,6 @@ void SVFIRReader::fill(const cJSON*& fieldJson, SVFStmt* stmt) { fill(fieldJson, static_cast(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); } @@ -2171,7 +2150,6 @@ void SVFIRReader::fill(const cJSON*& fieldJson, ICFGNode* node) { fill(fieldJson, static_cast(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); } @@ -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); @@ -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(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(value)); - JSON_READ_FIELD_FWD(fieldJson, value, bb); JSON_READ_FIELD_FWD(fieldJson, value, terminator); JSON_READ_FIELD_FWD(fieldJson, value, ret); } diff --git a/svf/lib/SVFIR/SVFValue.cpp b/svf/lib/SVFIR/SVFValue.cpp index b885b9f1b..3d41c9251 100644 --- a/svf/lib/SVFIR/SVFValue.cpp +++ b/svf/lib/SVFIR/SVFValue.cpp @@ -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