LLVM

2012-02-14 8 views
0

의 상수 IR 피연산자를 비교 나는이처럼 보이는 IR 있습니다LLVM

%5=icmp eq i32 %4,0 

내가 ICMP 명령의 두 번째 피연산자가 0 또는 뭔가 다른 경우 확인하고자합니다. 나는 getOperand(1)을 사용하여 결과를 Value * 형식으로 반환합니다. 상수 0과 어떻게 비교할 수 있습니까?

+0

0 'val-> isZero()', 다른 상수에 대한 좀 더 까다로운. 'match'를 사용하는 것이 좋습니다. 많은 예제를 보려면 InstructionSimplify.cpp를보십시오. –

+0

@ SK-logic :'Value' 자체는'isZero'를 가지고 있지 않습니다. 그것은'ConstantInt'의 메소드입니다. 자세한 내용은 내 대답을 참조하십시오. –

+0

@EliBendersky, 물론, ConstantInt에 대한 동적 캐스팅을 성공적으로 제공했습니다. –

답변

3

여기 당신이 찾아 실행할 수있는 샘플 패스 : 그것은이다

특히
class DetectZeroValuePass : public FunctionPass { 
public: 
    static char ID; 

    DetectZeroValuePass() 
     : FunctionPass(ID) 
    {} 

    virtual bool runOnFunction(Function &F) { 
     for (Function::iterator bb = F.begin(), bb_e = F.end(); bb != bb_e; ++bb) { 
      for (BasicBlock::iterator ii = bb->begin(), ii_e = bb->end(); ii != ii_e; ++ii) { 
       if (CmpInst *cmpInst = dyn_cast<CmpInst>(&*ii)) { 
        handle_cmp(cmpInst); 
       } 
      } 
     } 
    } 

    void handle_cmp(CmpInst *cmpInst) { 
     // Detect cmp instructions with the second operand being 0 
     if (cmpInst->getNumOperands() >= 2) { 
      Value *secondOperand = cmpInst->getOperand(1); 
      if (ConstantInt *CI = dyn_cast<ConstantInt>(secondOperand)) 
       if (CI->isZero()) { 
        errs() << "In the following instruction, second operand is 0\n"; 
        cmpInst->dump(); 
       } 
     } 
    } 
};