参考:http://www.alonemonkey.com/2016/12/21/learning-llvm/
一、目的:
写入opt和clang的命令参数,可以直接参数指定调用
二、使用pass
把上一小节的pass稍加修改
- 创建头文件:include/llvm/Transforms/CountOpcode/CountOpcode.h
#include "llvm/IR/Function.h"
#include "llvm/Pass.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/Instructions.h"
#include "llvm/InitializePasses.h"
#define DEBUG_TYPE "opcodecounter"
namespace llvm {
FunctionPass *createCountOpcodePass();
// void initializeCountOpcodePass(PassRegistry &Registry);
}
- cpp文件:lib/Transforms/CountOpcode/CountOpcode.cpp
#include "llvm/Transforms/CountOpcode/CountOpcode.h"
using namespace llvm;
static cl::opt<bool> EnableCountOpcode("opcodeCounter2", cl::init(false),
cl::desc("Count opcode number"));
namespace llvm {
struct CountOpcode : public FunctionPass {
std::map<std::string,int> opcodeCounter;
static char ID;
CountOpcode() : FunctionPass(ID){}
virtual bool runOnFunction(Function &F) {
if(!EnableCountOpcode){
return false;
}
errs()<<"FunctionName:"<<F.getName()<<"\n";
for(Function::iterator bb = F.begin(),e = F.end();bb!=e;++bb){
for(BasicBlock::iterator i = bb->begin(),ie = bb->end();i!=ie;++i){
if(opcodeCounter.find(i->getOpcodeName()) == opcodeCounter.end()){
opcodeCounter[i->getOpcodeName()] =1;
}else{
opcodeCounter[i->getOpcodeName()] +=1;
}
}
}
std::map<std::string,int>::iterator ib = opcodeCounter.begin();
std::map<std::string,int>::iterator ie = opcodeCounter.end();
while (ib != ie) {
errs() << ib->first << " : " << ib->second << "\n";
ib++;
}
errs()<<"\n";
opcodeCounter.clear();
return false;
}
};
FunctionPass * createCountOpcodePass(){
return new CountOpcode();
}
}
char CountOpcode::ID = 0;
//static RegisterPass<CountOpcode> X("opcodeCounter", "Count opcode number", false, false);
INITIALIZE_PASS(CountOpcode, "opcodeCounter", "Count opcode number", false, false)
- 创建LLVMBuild.txt : lib/Transforms/CountOpcode/LLVMBuild.txt
[component_0]
type = Library
name = CountOpcode
parent = Transforms
library_name = CountOpcode
- 创建CMakeLists.txt : lib/Transforms/CountOpcode/CMakeLists.txt
add_llvm_library( LLVMCountOpcode
CountOpcode.cpp
DEPENDS
intrinsics_gen
)
- lib/Transforms/CMakeLists.txt
add_subdirectory(CountOpcode)
- lib/Transforms/LLVMBuild.txt 加上我们的pass
[common]
subdirectories = AggressiveInstCombine Coroutines IPO InstCombine Instrumentation Scalar Utils Vectorize ObjCARC CountOpcode
- PassManager 注册
- lib/Transforms/IPO/PassManagerBuilder.cpp 添加头文件:
#include "llvm/Transforms/CountOpcode/CountOpcode.h"
- lib/Transforms/IPO/PassManagerBuilder.cpp 函数 populateModulePassManager 添加:
MPM.add(createCountOpcode());
- lib/Transforms/IPO/LLVMBuild.txt 添加:
required_libraries = AggressiveInstCombine Analysis BitReader BitWriter Core InstCombine IRReader Linker Object ProfileData Scalar Support TransformUtils Vectorize Instrumentation CountOpcode
- 相关头文件修改:
llvm/LinkAllPasses.h
头文件添加:
#include "llvm/Transforms/CountOpcode/CountOpcode.h"
(void) llvm::createCountOpcodePass();
llvm/InitializePasses.h
头文件添加:
void initializeCountOpcodePass(PassRegistry&);
- opt命令参数添加:
initializeCountOpcodePass(Registry);
最后分别编译opt和clang。
- 编译可能的问题:
- 最终链接找不到新pass的符号,需要在链接flag配置新创建pass库的路径:
ld: symbol(s) not found for architecture x86_64
如下配置链接库:
image.png
四、opt 和 clang执行:
-
opt 参数与运行结果:
image.png
image.png
-
clang 参数与运行结果:
image.png