From 565a386fbff81dd7847730174d4e609f1bfc8da9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zaoral?= Date: Tue, 3 May 2022 12:43:58 +0200 Subject: [PATCH] llvm14: CallBase::getNumArgOperands was removed --- include/dg/llvm/CallGraph/CallGraph.h | 13 ++++-- lib/llvm/PointerAnalysis/Interprocedural.cpp | 12 ++--- lib/llvm/PointerAnalysis/PointerAnalysis.cpp | 6 +-- lib/llvm/ReadWriteGraph/Calls.cpp | 10 ++--- .../SystemDependenceGraph.cpp | 7 +-- lib/llvm/ValueRelations/StructureAnalyzer.cpp | 4 +- lib/llvm/llvm-utils.h | 44 ++++++++++++++++--- tools/llvm-pta-ben.cpp | 6 ++- 8 files changed, 74 insertions(+), 28 deletions(-) diff --git a/include/dg/llvm/CallGraph/CallGraph.h b/include/dg/llvm/CallGraph/CallGraph.h index 7e5bb005e..7e20a7a38 100644 --- a/include/dg/llvm/CallGraph/CallGraph.h +++ b/include/dg/llvm/CallGraph/CallGraph.h @@ -135,14 +135,20 @@ callIsCompatible(const llvm::Function *F, const llvm::CallInst *CI, CallCompatibility policy = CallCompatibility::LOOSE) { using namespace llvm; +#if LLVM_VERSION_MAJOR >= 8 + auto max_idx = CI->arg_size(); +#else + auto max_idx = CI->getNumArgOperands(); +#endif + if (policy != CallCompatibility::MATCHING_ARGS) { if (F->isVarArg()) { - if (F->arg_size() > CI->getNumArgOperands()) { + if (F->arg_size() > max_idx) { return false; } - } else if (F->arg_size() != CI->getNumArgOperands()) { + } else if (F->arg_size() != max_idx) { if (policy == CallCompatibility::STRICT || - F->arg_size() > CI->getNumArgOperands()) { + F->arg_size() > max_idx) { // too few arguments return false; } @@ -159,7 +165,6 @@ callIsCompatible(const llvm::Function *F, const llvm::CallInst *CI, } size_t idx = 0; - auto max_idx = CI->getNumArgOperands(); for (auto A = F->arg_begin(), E = F->arg_end(); idx < max_idx && A != E; ++A, ++idx) { Type *CTy = CI->getArgOperand(idx)->getType(); diff --git a/lib/llvm/PointerAnalysis/Interprocedural.cpp b/lib/llvm/PointerAnalysis/Interprocedural.cpp index d44476c59..946234f3a 100644 --- a/lib/llvm/PointerAnalysis/Interprocedural.cpp +++ b/lib/llvm/PointerAnalysis/Interprocedural.cpp @@ -12,12 +12,14 @@ #include "dg/llvm/PointerAnalysis/PointerGraph.h" +#include "llvm/llvm-utils.h" + namespace dg { namespace pta { void LLVMPointerGraphBuilder::addArgumentOperands(const llvm::CallInst *CI, PSNode *arg, unsigned idx) { - assert(idx < CI->getNumArgOperands()); + assert(idx < llvmutils::getNumArgOperands(CI)); PSNode *op = tryGetOperand(CI->getArgOperand(idx)); if (op && !arg->hasOperand(op)) { // NOTE: do not add an operand multiple-times @@ -29,9 +31,8 @@ void LLVMPointerGraphBuilder::addArgumentOperands(const llvm::CallInst *CI, void LLVMPointerGraphBuilder::addArgumentOperands(const llvm::CallInst &CI, PSNode &node) { - auto sentinel = CI.getNumArgOperands(); - for (unsigned i = 0; i < sentinel; ++i) { - PSNode *operand = tryGetOperand(CI.getArgOperand(i)); + for (const auto &arg : llvmutils::args(CI)) { + PSNode *operand = tryGetOperand(arg); if (operand && !node.hasOperand(operand)) { node.addOperand(operand); } @@ -75,7 +76,8 @@ void LLVMPointerGraphBuilder::addArgumentsOperands(const llvm::Function *F, void LLVMPointerGraphBuilder::addVariadicArgumentOperands( const llvm::Function *F, const llvm::CallInst *CI, PSNode *arg) { - for (unsigned idx = F->arg_size() - 1; idx < CI->getNumArgOperands(); ++idx) + for (unsigned idx = F->arg_size() - 1; + idx < llvmutils::getNumArgOperands(CI); ++idx) addArgumentOperands(CI, arg, idx); } diff --git a/lib/llvm/PointerAnalysis/PointerAnalysis.cpp b/lib/llvm/PointerAnalysis/PointerAnalysis.cpp index af5bc6d4d..62d01c3bf 100644 --- a/lib/llvm/PointerAnalysis/PointerAnalysis.cpp +++ b/lib/llvm/PointerAnalysis/PointerAnalysis.cpp @@ -73,9 +73,9 @@ LLVMPointerAnalysis::getAccessedMemory(const llvm::Instruction *I) { // check which operands are pointers and get the information for them bool hasUnknown = false; - for (unsigned i = 0; i < CI->getNumArgOperands(); ++i) { - if (hasPointsTo(CI->getArgOperand(i))) { - auto tmp = getLLVMPointsToChecked(CI->getArgOperand(i)); + for (const auto &arg : llvmutils::args(CI)) { + if (hasPointsTo(arg)) { + auto tmp = getLLVMPointsToChecked(arg); hasUnknown |= tmp.first; // translate to regions for (const auto &ptr : tmp.second) { diff --git a/lib/llvm/ReadWriteGraph/Calls.cpp b/lib/llvm/ReadWriteGraph/Calls.cpp index d191dca18..2a35f537c 100644 --- a/lib/llvm/ReadWriteGraph/Calls.cpp +++ b/lib/llvm/ReadWriteGraph/Calls.cpp @@ -111,12 +111,10 @@ LLVMReadWriteGraphBuilder::createUnknownCall(const llvm::CallInst *CInst) { // every pointer we pass into the undefined call may be defined // in the function - for (unsigned int i = 0; i < CInst->getNumArgOperands(); ++i) { - const Value *llvmOp = CInst->getArgOperand(i); - + for (const auto &arg : llvmutils::args(CInst)) { // constants cannot be redefined except for global variables // (that are constant, but may point to non constant memory - const Value *strippedValue = llvmOp->stripPointerCasts(); + const Value *strippedValue = arg->stripPointerCasts(); if (isa(strippedValue)) { const GlobalVariable *GV = dyn_cast(strippedValue); // if the constant is not global variable, @@ -125,7 +123,7 @@ LLVMReadWriteGraphBuilder::createUnknownCall(const llvm::CallInst *CInst) { continue; } - auto pts = PTA->getLLVMPointsToChecked(llvmOp); + auto pts = PTA->getLLVMPointsToChecked(arg); // if we do not have a pts, this is not pointer // relevant instruction. We must do it this way // instead of type checking, due to the inttoptr. @@ -275,7 +273,7 @@ RWNode *LLVMReadWriteGraphBuilder::funcFromModel(const FunctionModel *model, const llvm::CallInst *CInst) { RWNode *node = &create(RWNodeType::GENERIC); - for (unsigned int i = 0; i < CInst->getNumArgOperands(); ++i) { + for (unsigned int i = 0; i < llvmutils::getNumArgOperands(CInst); ++i) { if (!model->handles(i)) continue; diff --git a/lib/llvm/SystemDependenceGraph/SystemDependenceGraph.cpp b/lib/llvm/SystemDependenceGraph/SystemDependenceGraph.cpp index cb867a3b1..ab54cb376 100644 --- a/lib/llvm/SystemDependenceGraph/SystemDependenceGraph.cpp +++ b/lib/llvm/SystemDependenceGraph/SystemDependenceGraph.cpp @@ -1,6 +1,8 @@ #include "dg/llvm/SystemDependenceGraph/SystemDependenceGraph.h" #include "dg/util/debug.h" +#include "llvm/llvm-utils.h" + namespace dg { namespace llvmdg { @@ -49,9 +51,8 @@ struct SDGBuilder { // create actual parameters auto ¶ms = node.getParameters(); - for (unsigned i = 0; i < CI->getNumArgOperands(); ++i) { - auto *A = CI->getArgOperand(i); - llvm::errs() << "Act: " << *A << "\n"; + for (const auto &arg : llvmutils::args(CI)) { + llvm::errs() << "Act: " << *arg << "\n"; params.createParameter(); } return node; diff --git a/lib/llvm/ValueRelations/StructureAnalyzer.cpp b/lib/llvm/ValueRelations/StructureAnalyzer.cpp index 988adf359..98c388213 100644 --- a/lib/llvm/ValueRelations/StructureAnalyzer.cpp +++ b/lib/llvm/ValueRelations/StructureAnalyzer.cpp @@ -1,5 +1,7 @@ #include "dg/llvm/ValueRelations/StructureAnalyzer.h" +#include "llvm/llvm-utils.h" + #include #include #include @@ -689,7 +691,7 @@ void StructureAnalyzer::initializeCallRelations() { // set formal parameters equal to real unsigned argCount = 0; for (const llvm::Argument &formalArg : function.args()) { - if (argCount >= call->getNumArgOperands()) + if (argCount >= llvmutils::getNumArgOperands(call)) break; const llvm::Value *realArg = call->getArgOperand(argCount); diff --git a/lib/llvm/llvm-utils.h b/lib/llvm/llvm-utils.h index d253dd4f8..346f20633 100644 --- a/lib/llvm/llvm-utils.h +++ b/lib/llvm/llvm-utils.h @@ -1,6 +1,7 @@ #ifndef DG_LLVM_UTILS_H_ #define DG_LLVM_UTILS_H_ +#include #include #include #include @@ -15,6 +16,39 @@ namespace llvmutils { using namespace llvm; +/* ---------------------------------------------- + * -- COMPAT + * ---------------------------------------------- */ + +// FIXME: Remove this when LLVM 8 is the minimal version for DG! +inline iterator_range args(CallInst *CI) { + return make_range(CI->arg_begin(), CI->arg_end()); +} + +inline iterator_range args(const CallInst *CI) { + return make_range(CI->arg_begin(), CI->arg_end()); +} + +inline iterator_range args(CallInst &CI) { + return args(&CI); +} + +inline iterator_range args(const CallInst &CI) { + return args(&CI); +} + +inline unsigned getNumArgOperands(const CallInst *CI) { +#if LLVM_VERSION_MAJOR >= 8 + return CI->arg_size(); +#else + return CI->getNumArgOperands(); +#endif +} + +inline unsigned getNumArgOperands(const CallInst &CI) { + return getNumArgOperands(&CI); +} + /* ---------------------------------------------- * -- PRINTING * ---------------------------------------------- */ @@ -58,14 +92,15 @@ callIsCompatible(const Function *F, const CallInst *CI, CallCompatibility policy = CallCompatibility::LOOSE) { using namespace llvm; + auto ci_arg_size = getNumArgOperands(CI); if (policy != CallCompatibility::MATCHING_ARGS) { if (F->isVarArg()) { - if (F->arg_size() > CI->getNumArgOperands()) { + if (F->arg_size() > ci_arg_size) { return false; } - } else if (F->arg_size() != CI->getNumArgOperands()) { + } else if (F->arg_size() != ci_arg_size) { if (policy == CallCompatibility::STRICT || - F->arg_size() > CI->getNumArgOperands()) { + F->arg_size() > ci_arg_size) { // too few arguments return false; } @@ -82,8 +117,7 @@ callIsCompatible(const Function *F, const CallInst *CI, } size_t idx = 0; - auto max_idx = CI->getNumArgOperands(); - for (auto A = F->arg_begin(), E = F->arg_end(); idx < max_idx && A != E; + for (auto A = F->arg_begin(), E = F->arg_end(); idx < ci_arg_size && A != E; ++A, ++idx) { Type *CTy = CI->getArgOperand(idx)->getType(); Type *ATy = A->getType(); diff --git a/tools/llvm-pta-ben.cpp b/tools/llvm-pta-ben.cpp index cfc07a3b7..ff804edee 100644 --- a/tools/llvm-pta-ben.cpp +++ b/tools/llvm-pta-ben.cpp @@ -30,6 +30,8 @@ #include "dg/util/TimeMeasure.h" +#include "llvm/llvm-utils.h" + using namespace dg; using namespace dg::pta; using dg::debug::TimeMeasure; @@ -247,8 +249,10 @@ static void evalPSNode(DGLLVMPointerAnalysis *pta, PSNode *node) { const llvm::Function *called = llvm::cast(v); const llvm::StringRef &fun = called->getName(); - if (call->getNumArgOperands() != 2) + + if (llvmutils::getNumArgOperands(call) != 2) return; + if (!test_checkfunc(fun)) return;