参考书:《LLVM Cookbook中文版》
分析pass可以在不更改IR的情况下提供关于IR的更高级的信息,下面我们实现一个分析pass,计算和输出一个函数中使用的操作码数量。
步骤和前几节 编写pass一样。
一、pass源码 lib/Transforms/CountOpcode/CountOpcode.cpp:
#include "llvm/IR/Function.h"
#include "llvm/Pass.h"
#include "llvm/Support/raw_ostream.h"
#include <map>
#define DEBUG_TYPE "opcodecounter"
using namespace llvm;
namespace {
struct CountOpcode : public FunctionPass {
std::map<std::string,int> opcodeCounter;
static char ID;
CountOpcode() : FunctionPass(ID){}
virtual bool runOnFunction(Function &F) {
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;
}
};
}
char CountOpcode::ID = 0;
static RegisterPass<CountOpcode> X("opcodeCounter", "Count opcode number", false, false);
二、创建测试代码:
countopcode_test.cpp
#include <stdio.h>
#include <stdlib.h>
int func(int x, int y) {
int sum=0,iter=0;
for(;iter<x;iter++){
int iter1 = 0;
for(;iter1<y;iter1++){
sum+=iter>iter1?1:0;
}
}
return sum;
}
int main(int argc,char **argv) {
printf("hello, world!\n");
int a = func(atoi(argv[1]),3);
printf("%d\n",a);
return 0;
}
网友评论