Commit 3aeea185 authored by Naman Dixit's avatar Naman Dixit

Partial port of opt to gazelle

parent 55852ebc
### NOTES
See src/pass_plugin_demo/README.txt for a simple opt based pass.
This diff is collapsed.
clang-13 -emit-llvm test.c -c -o test.bc
clang++-13 -fPIC -shared pass.cpp $(llvm-config-13 --ldflags --libs) -o pass.so
opt-13 -disable-output -enable-new-pm=0 -load ./pass.so -hello < test.bc
/*
* Creator: Naman Dixit
* Notice: © Copyright 2022 Naman Dixit
*/
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/IR/CallSite.h"
//#include "llvm/IR/Instruction.h"
using namespace llvm;
namespace {
//struct FunctionsNames : public FunctionPass {
class FunctionsNames : public FunctionPass {
public:
static char ID;
FunctionsNames() : FunctionPass(ID) {}
bool runOnFunction(Function &F) override {
// Print name of function
outs() << "*";
outs().write_escaped(F.getName()) << '\n';
for (auto& B : F) { // Iterate over each Basic Blocks in Functions
for (auto& I : B) { // Iterate over each Instructions in Basic Blocks
// Dynamically cast Instruction to CallInst.
// This step will return false for any instruction which is
// not a CallInst
if(CallBase *CI = dyn_cast<CallBase>(&I)) {
// Print out the function name
outs() << " |-" << CI->getCalledFunction()->getName() << "\n";
} else {
outs() << "Not a function\n";
}
}
}
return false;
}
}; // end of struct FunctionsNames
} // end of anonymous namespace
char FunctionsNames::ID = 0;
static RegisterPass<FunctionsNames> X("hello", "Display Function Names",
false /* Only looks at CFG */,
false /* Analysis Pass */);
static RegisterStandardPasses Y(
PassManagerBuilder::EP_EarlyAsPossible,
[](const PassManagerBuilder &Builder,
legacy::PassManagerBase &PM) { PM.add(new FunctionsNames()); });
#include <stdio.h>
void a (void)
{
return;
}
int main() {
printf("hello world\n");
a();
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment