Skip to content

Commit

Permalink
Remove LLVM header in SVFType.h & Remove getLLVMModuleSet() in LLVMUt…
Browse files Browse the repository at this point in the history
…il.h
  • Loading branch information
xudon9 committed Dec 10, 2022
1 parent ea142cd commit 47219c7
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 198 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ PointerAlignment: Left
IndentPPDirectives: AfterHash
AllowShortFunctionsOnASingleLine: Empty
ColumnLimit: 80
AccessModifierOffset: -4
167 changes: 86 additions & 81 deletions include/SVF-LLVM/LLVMUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ inline bool isCallSite(const Value* val)
/// Get the definition of a function across multiple modules
inline const Function* getDefFunForMultipleModule(const Function* fun)
{
if(fun == nullptr) return nullptr;
if (fun == nullptr)
return nullptr;
LLVMModuleSet* llvmModuleset = LLVMModuleSet::getLLVMModuleSet();
if (fun->isDeclaration() && llvmModuleset->hasDefinition(fun))
fun = LLVMModuleSet::getLLVMModuleSet()->getDefinition(fun);
Expand All @@ -75,38 +76,38 @@ inline const Function* getCallee(const CallBase* cs)
{
// FIXME: do we need to strip-off the casts here to discover more library functions
const Function* callee = SVFUtil::dyn_cast<Function>(cs->getCalledOperand()->stripPointerCasts());
if(callee)
return getDefFunForMultipleModule(callee);
else
return nullptr;
return callee ? getDefFunForMultipleModule(callee) : nullptr;
}

/// Return LLVM function if this value is
inline const Function* getLLVMFunction(const Value* val)
{
const Function* fun = SVFUtil::dyn_cast<Function>(val->stripPointerCasts());
return fun;
return SVFUtil::dyn_cast<Function>(val->stripPointerCasts());
}

/// Get program entry function from module.
inline const Function* getProgFunction(const std::string& funName)
{
for (Module& M : LLVMModuleSet::getLLVMModuleSet()->getLLVMModules())
for (const Module& M : LLVMModuleSet::getLLVMModuleSet()->getLLVMModules())
{
for (Module::const_iterator F = M.begin(), E = M.end(); F != E; ++F)
{
const Function *fun = &*F;
if (fun->getName().str()==funName)
return fun;
for (const Function& fun : M) {
if (fun.getName() == funName)
return &fun;
}
}
return nullptr;
}

/// Check whether a function is an entry function (i.e., main)
inline bool isProgEntryFunction(const Function* fun)
{
return fun && fun->getName() == "main";
}

/// Check whether this value is a black hole
inline bool isBlackholeSym(const Value* val)
{
return (SVFUtil::isa<UndefValue>(val));
return SVFUtil::isa<UndefValue>(val);
}

/// Check whether this value is a black hole
Expand Down Expand Up @@ -159,16 +160,16 @@ inline const PointerType *getRefTypeOfHeapAllocOrStatic(const Instruction* inst)
//@}

/// Return true if this value refers to a object
bool isObject (const Value* ref);
bool isObject(const Value* ref);

/// Method for dead function, which does not have any possible caller
/// function address is not taken and never be used in call or invoke instruction
//@{
/// whether this is a function without any possible caller?
bool isUncalledFunction (const Function* fun);
bool isUncalledFunction(const Function* fun);

/// whether this is an argument in dead function
inline bool ArgInDeadFunction (const Value* val)
inline bool ArgInDeadFunction(const Value* val)
{
return SVFUtil::isa<Argument>(val)
&& isUncalledFunction(SVFUtil::cast<Argument>(val)->getParent());
Expand All @@ -179,12 +180,11 @@ inline bool ArgInDeadFunction (const Value* val)
inline bool ArgInProgEntryFunction(const Value* val)
{
return SVFUtil::isa<Argument>(val) &&
SVFUtil::isProgEntryFunction(
LLVMModuleSet::getLLVMModuleSet()->getSVFFunction(
SVFUtil::cast<Argument>(val)->getParent()));
LLVMUtil::isProgEntryFunction(
SVFUtil::cast<Argument>(val)->getParent());
}
/// Return true if this is value in a dead function (function without any caller)
bool isPtrInUncalledFunction (const Value* value);
bool isPtrInUncalledFunction(const Value* value);
//@}

//@}
Expand All @@ -194,9 +194,7 @@ bool isPtrInUncalledFunction (const Value* value);
/// Return true if the function does not have a caller (either it is a main function or a dead function)
inline bool isNoCallerFunction(const Function* fun)
{
return isUncalledFunction(fun) ||
SVFUtil::isProgEntryFunction(
LLVMModuleSet::getLLVMModuleSet()->getSVFFunction(fun));
return isUncalledFunction(fun) || LLVMUtil::isProgEntryFunction(fun);
}

/// Return true if the argument in a function does not have a caller
Expand All @@ -207,115 +205,121 @@ inline bool isArgOfUncalledFunction (const Value* val)
}
//@}

/// Return true if the function has a return instruction reachable from function entry
bool functionDoesNotRet (const Function* fun);
/// Return true if the function has a return instruction reachable from function
/// entry
bool functionDoesNotRet(const Function* fun);

/// Get reachable basic block from function entry
void getFunReachableBBs (const Function* svfFun, std::vector<const SVFBasicBlock*>& bbs);
void getFunReachableBBs(const Function* svfFun,
std::vector<const SVFBasicBlock*>& bbs);

/// Strip off the constant casts
const Value* stripConstantCasts(const Value* val);
const Value* stripConstantCasts(const Value* val);

/// Strip off the all casts
const Value* stripAllCasts(const Value* val) ;
const Value* stripAllCasts(const Value* val);

/// Get the type of the heap allocation
const Type* getTypeOfHeapAlloc(const Instruction* inst) ;
const Type* getTypeOfHeapAlloc(const Instruction* inst);

/// Return the bitcast instruction which is val's only use site, otherwise return nullptr
/// Return the bitcast instruction which is val's only use site, otherwise
/// return nullptr
const Value* getUniqueUseViaCastInst(const Value* val);

/// Return corresponding constant expression, otherwise return nullptr
//@{
inline const ConstantExpr *isGepConstantExpr(const Value* val)
inline const ConstantExpr* isGepConstantExpr(const Value* val)
{
if(const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
{
if(constExpr->getOpcode() == Instruction::GetElementPtr)
if (constExpr->getOpcode() == Instruction::GetElementPtr)
return constExpr;
}
return nullptr;
}

inline const ConstantExpr *isInt2PtrConstantExpr(const Value* val)
inline const ConstantExpr* isInt2PtrConstantExpr(const Value* val)
{
if(const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
{
if(constExpr->getOpcode() == Instruction::IntToPtr)
if (constExpr->getOpcode() == Instruction::IntToPtr)
return constExpr;
}
return nullptr;
}

inline const ConstantExpr *isPtr2IntConstantExpr(const Value* val)
inline const ConstantExpr* isPtr2IntConstantExpr(const Value* val)
{
if(const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
{
if(constExpr->getOpcode() == Instruction::PtrToInt)
if (constExpr->getOpcode() == Instruction::PtrToInt)
return constExpr;
}
return nullptr;
}

inline const ConstantExpr *isCastConstantExpr(const Value* val)
inline const ConstantExpr* isCastConstantExpr(const Value* val)
{
if(const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
{
if(constExpr->getOpcode() == Instruction::BitCast)
if (constExpr->getOpcode() == Instruction::BitCast)
return constExpr;
}
return nullptr;
}

inline const ConstantExpr *isSelectConstantExpr(const Value* val)
inline const ConstantExpr* isSelectConstantExpr(const Value* val)
{
if(const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
{
if(constExpr->getOpcode() == Instruction::Select)
if (constExpr->getOpcode() == Instruction::Select)
return constExpr;
}
return nullptr;
}

inline const ConstantExpr *isTruncConstantExpr(const Value* val)
inline const ConstantExpr* isTruncConstantExpr(const Value* val)
{
if(const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
{
if(constExpr->getOpcode() == Instruction::Trunc ||
constExpr->getOpcode() == Instruction::FPTrunc ||
constExpr->getOpcode() == Instruction::ZExt ||
constExpr->getOpcode() == Instruction::SExt ||
constExpr->getOpcode() == Instruction::FPExt)
if (constExpr->getOpcode() == Instruction::Trunc ||
constExpr->getOpcode() == Instruction::FPTrunc ||
constExpr->getOpcode() == Instruction::ZExt ||
constExpr->getOpcode() == Instruction::SExt ||
constExpr->getOpcode() == Instruction::FPExt)
return constExpr;
}
return nullptr;
}

inline const ConstantExpr *isCmpConstantExpr(const Value* val)
inline const ConstantExpr* isCmpConstantExpr(const Value* val)
{
if(const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
{
if(constExpr->getOpcode() == Instruction::ICmp || constExpr->getOpcode() == Instruction::FCmp)
if (constExpr->getOpcode() == Instruction::ICmp ||
constExpr->getOpcode() == Instruction::FCmp)
return constExpr;
}
return nullptr;
}

inline const ConstantExpr *isBinaryConstantExpr(const Value* val)
inline const ConstantExpr* isBinaryConstantExpr(const Value* val)
{
if(const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
{
if((constExpr->getOpcode() >= Instruction::BinaryOpsBegin) && (constExpr->getOpcode() <= Instruction::BinaryOpsEnd))
if ((constExpr->getOpcode() >= Instruction::BinaryOpsBegin) &&
(constExpr->getOpcode() <= Instruction::BinaryOpsEnd))
return constExpr;
}
return nullptr;
}

inline const ConstantExpr *isUnaryConstantExpr(const Value* val)
inline const ConstantExpr* isUnaryConstantExpr(const Value* val)
{
if(const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
{
if((constExpr->getOpcode() >= Instruction::UnaryOpsBegin) && (constExpr->getOpcode() <= Instruction::UnaryOpsEnd))
if ((constExpr->getOpcode() >= Instruction::UnaryOpsBegin) &&
(constExpr->getOpcode() <= Instruction::UnaryOpsEnd))
return constExpr;
}
return nullptr;
Expand All @@ -325,38 +329,41 @@ inline const ConstantExpr *isUnaryConstantExpr(const Value* val)
inline static DataLayout* getDataLayout(Module* mod)
{
static DataLayout *dl = nullptr;
if (dl == nullptr) dl = new DataLayout(mod);
if (dl == nullptr)
dl = new DataLayout(mod);
return dl;
}

/// Get the next instructions following control flow
void getNextInsts(const Instruction* curInst, std::vector<const SVFInstruction*>& instList);
void getNextInsts(const Instruction* curInst,
std::vector<const SVFInstruction*>& instList);

/// Get the previous instructions following control flow
void getPrevInsts(const Instruction* curInst, std::vector<const SVFInstruction*>& instList);
void getPrevInsts(const Instruction* curInst,
std::vector<const SVFInstruction*>& instList);

/// Get the next instructions following control flow
void getNextInsts(const Instruction* curInst, std::vector<const Instruction*>& instList);
void getNextInsts(const Instruction* curInst,
std::vector<const Instruction*>& instList);

/// Get the previous instructions following control flow
void getPrevInsts(const Instruction* curInst, std::vector<const Instruction*>& instList);
void getPrevInsts(const Instruction* curInst,
std::vector<const Instruction*>& instList);

/// Get num of BB's predecessors
u32_t getBBPredecessorNum(const BasicBlock* BB);



/// Check whether a file is an LLVM IR file
bool isIRFile(const std::string &filename);
bool isIRFile(const std::string& filename);

/// Parse argument for multi-module analysis
void processArguments(int argc, char **argv, int &arg_num, char **arg_value,
std::vector<std::string> &moduleNameVec);
void processArguments(int argc, char** argv, int& arg_num, char** arg_value,
std::vector<std::string>& moduleNameVec);

/// Helper method to get the size of the type from target data layout
//@{
u32_t getTypeSizeInBytes(const Type* type);
u32_t getTypeSizeInBytes(const StructType *sty, u32_t field_index);
u32_t getTypeSizeInBytes(const StructType* sty, u32_t field_index);
//@}

const std::string getSourceLoc(const Value* val);
Expand All @@ -374,17 +381,14 @@ inline const SVFFunction* getFunction(const std::string& name)
/// Return true if the value refers to constant data, e.g., i32 0
inline bool isConstDataOrAggData(const Value* val)
{
bool constDataOrConstAggregate = SVFUtil::isa<ConstantData>(val)
|| SVFUtil::isa<ConstantAggregate>(val)
|| SVFUtil::isa<MetadataAsValue>(val)
|| SVFUtil::isa<BlockAddress>(val);
return constDataOrConstAggregate;
return SVFUtil::isa<ConstantData, ConstantAggregate,
MetadataAsValue, BlockAddress>(val);
}

/// find the unique defined global across multiple modules
inline const Value* getGlobalRep(const Value* val)
{
if(const GlobalVariable* gvar = SVFUtil::dyn_cast<GlobalVariable>(val))
if (const GlobalVariable* gvar = SVFUtil::dyn_cast<GlobalVariable>(val))
{
if (LLVMModuleSet::getLLVMModuleSet()->hasGlobalRep(gvar))
val = LLVMModuleSet::getLLVMModuleSet()->getGlobalRep(gvar);
Expand All @@ -405,7 +409,7 @@ void viewCFG(const Function* fun);
void viewCFGOnly(const Function* fun);

bool isValVtbl(const Value* val);
bool isLoadVtblInst(const LoadInst *loadInst);
bool isLoadVtblInst(const LoadInst* loadInst);
bool isVirtualCallSite(const CallBase* cs);
bool isConstructor(const Function* F);
bool isDestructor(const Function* F);
Expand Down Expand Up @@ -451,7 +455,8 @@ bool VCallInCtorOrDtor(const CallBase* cs);
* }
* this and this1 are the same thisPtr in the constructor
*/
bool isSameThisPtrInConstructor(const Argument* thisPtr1, const Value* thisPtr2);
bool isSameThisPtrInConstructor(const Argument* thisPtr1,
const Value* thisPtr2);

} // End namespace LLVMUtil

Expand Down
6 changes: 3 additions & 3 deletions include/SVFIR/SVFStatements.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
* Author: Yulei Sui
*/

#ifndef PAGEDGE_H_
#define PAGEDGE_H_
#ifndef INCLUDE_SVFIR_SVFSTATEMENT_H_
#define INCLUDE_SVFIR_SVFSTATEMENT_H_

#include "Graphs/GenericGraph.h"
#include "MemoryModel/LocationSet.h"
Expand Down Expand Up @@ -1144,4 +1144,4 @@ class TDJoinPE: public RetPE

} // End namespace SVF

#endif /* PAGEDGE_H_ */
#endif /* INCLUDE_SVFIR_SVFSTATEMENT_H_ */
Loading

0 comments on commit 47219c7

Please sign in to comment.