기능

2017-11-09 7 views
1

에 LLVM 지침의 수를 계산 내가 기능

opt -stats -analyze -instcount file.bc 

을 사용하고있는 것은 코드의 통계 정보를 얻을 수 있습니다. 이제 LLVM 명령어의 수를 특정 이름, 예를 들어 "bar"의 함수로 얻고 싶습니다.

이상적으로,이 방법

opt -stats -analyze -instcount funcname="bar" 

어떤 것이 올바른 옵션을 사용하기에 일하는 것이 opt의 옵션을 기대? 나는 많이 봤 거든 아직 대답이 없네.

답변

3

function analysis pass을 작성하십시오. (llvm::FunctionPass documentation)

같은 것을 보일 것이다 귀하의 코드 :

// This is a contrived example. 

#include <iterator> 
#include <string> 

#include "llvm/Pass.h" 
#include "llvm/IR/BasicBlock.h" 
#include "llvm/IR/Function.h" 
#include "llvm/IR/Instruction.h" 
#include "llvm/Support/raw_ostream.h" 

namespace 
{ 

using namespace llvm; 

cl::opt<std::string> functionName("funcname", cl::ValueRequired, 
    cl::desc("Function name"), cl::NotHidden); 

class FunctionInstCounter : public FunctionPass 
{ 
public: 
    static char ID; 

    FunctionInstCounter() 
    : FunctionPass(ID) 
    { 
    initializeFunctionInstCounterPass(*PassRegistry::getPassRegistry()); 
    } 

    bool runOnFunction(Function& func) override 
    { 
    if (func.getName() != functionName) 
    { 
     return false; 
    } 

    unsigned int instCount = 0; 
    for (BasicBlock& bb : func) 
    { 
     instCount += std::distance(bb.begin(), bb.end()); 
    } 

    llvm::outs() << "Number of instructions in " << func.getName() << ": " 
     << instCount << "\n"; 
    return false; 
    } 
}; 

} // namespace 

char FunctionInstCounter::ID = 0; 

INITIALIZE_PASS(FunctionInstCounter, "funcinstcount", 
    "Function instruction counter", false, true) 

llvm::Pass* llvm::createFunctionInstCounterPass() 
{ 
    return new FunctionInstCounter(); 
} 

당신은 이런 식으로 부를 것이다 :

opt -funcinstcount -funcname=NameOfFunctionHere