#include"llvm/Pass.h" #include"llvm/IR/Function.h" #include"llvm/IR/InstrTypes.h" #include"llvm/Support/raw_ostream.h" #include"llvm/IR/LegacyPassManager.h" #include"llvm/IR/Constants.h" #include"llvm/Transforms/IPO/PassManagerBuilder.h" #include<iostream> using namespace llvm; using std::cout; using std::endl; using std::string; /* anonymous namespace. visible only to the current file. */ namespace { structSqu : public FunctionPass { staticchar ID; Squ() : FunctionPass(ID) {}
/* which overrides an abstract virtual method inherited from FunctionPass. */ boolrunOnFunction(Function &F) override { /* print the function name */ errs().write_escaped(F.getName()) << '\n'; /* BasicBlock iterator */ for(auto &BB_i : F){ /* Istruction iterator */ for(auto &I_i : BB_i){ Value *CalledFunction; if(auto *CB = dyn_cast<CallBase>(&I_i)){ CalledFunction = CB->getCalledFunction(); StringRef Name = CalledFunction->getName(); unsignedint num = CB->getNumOperands();
errs() << "\tCall : " << Name << " with " \ << num << " argus" << "\n";
for(auto i = 0; i < num - 1; i++){ ConstantInt *ci = dyn_cast<ConstantInt>(CB->getArgOperand(i)); // assert(ci != NULL); if(ci == NULL) continue; // not a digital errs() << "\t\t" << ci->getZExtValue(); } errs() << "\n"; } } } returnfalse; } }; // end of struct Squ } // end of anonymous namespace
/* LLVM uses ID’s address to identify a pass */ char Squ::ID = 0; /* important here for cmd-line use */ static RegisterPass<Squ> X("squ", "squ Pass", false/* Only looks at CFG */, false/* Analysis Pass */);