previous research:
- [no answers] How to get loop bounds in LLVM? - Stack Overflow
- [no answers] Using LLVM pass, I would like to extract how many times each loop in a program would run (trip count). Or Is there any better way to get this info? - Stack Overflow
I am trying to analyze every loop in a function and get the bounds of each loop via getBounds. I require an object of type ScalarEvolution to get that info.
I found that getAnalysis could be used in passes to get this info from some snippets online, but I get errors saying that getAnalysis
is not defined, or that it is not a member of a static class. I assume there is some inaccuracy in how I am going about this.
This is the expansion of the line I use to compile my pass:
/home/share/llvm_build/bin/clang -shared -o hw1.so hw1.cpp -I/home/share/llvm-project-17.0.2.src/llvm/include -I/home/share/llvm_build/include -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -fPIC -fno-rtti -L/home/share/llvm_build/lib
This is the code of the pass so far. I want to get the correct info in variable SE
on line 16:
#include "llvm/IR/PassManager.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/PassPlugin.h"
using namespace llvm;
namespace {
struct DependencyAnalysisPass : public PassInfoMixin<DependencyAnalysisPass> {
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
};
PreservedAnalyses DependencyAnalysisPass::run(Function &F, FunctionAnalysisManager &FAM) {
auto &LI = FAM.getResult<LoopAnalysis>(F);
auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
for (const auto &L : LI) {
if (auto LB = L->getBounds(*SE)) {
}
}
for (BasicBlock &BB : F) {
for (Instruction &I : BB) {
if (auto *LI = dyn_cast<LoadInst>(&I)) {
outs() << "Load: " << I << "\n" ;
} else if (auto *SI = dyn_cast<StoreInst>(&I)) {
outs() << "Store: " << I << "\n" ;
} else {
outs() << "Other: " << I << "\n" ;
}
}
}
return PreservedAnalyses::all();
}
} // end anonymous namespace
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
llvmGetPassPluginInfo() {
return {LLVM_PLUGIN_API_VERSION, "DependencyAnalysisPass", LLVM_VERSION_STRING,
[](PassBuilder &PB) {
PB.registerPipelineParsingCallback(
[](StringRef Name, FunctionPassManager &FPM,
ArrayRef<PassBuilder::PipelineElement>) {
if (Name == "depanalyze") {
FPM.addPass(DependencyAnalysisPass());
return true;
}
return false;
});
}};
}