LLVM Instruction to ICmpInst conversion

Hello !

I want some piece of advice regarding a LLVM pass. My particular problem is:

There is a method

bool patternDC::runOnFunction(Function &F) {

if ( CC->operEquiv(icmpInstrArray[i], icmpInstrArray[j]) ) {…}

}

having the array elements of type Instruction* : http://llvm.org/doxygen/classllvm_1_1Instruction.html

The called method is

bool ifChecker::operEquiv(Instruction *I1, Instruction *I2)
{

}

BUT I want to use the methods from class ICmpInst : clasa http://llvm.org/doxygen/classllvm_1_1ICmpInst.html inside operEquiv. I cannot do something like

ICmpInst** II1 = dyn_cast<ICmpInst*>(I1);

(a kind of instanceOf() from Java), having casting compilation problems.

The ICmpInst class is defined at line 913 from http://llvm.org/doxygen/Instructions_8h_source.html
The inheritance diagram is at http://llvm.org/doxygen/classllvm_1_1ICmpInst.html

I want to use the ICmpInst methods for objects of type Instruction. The methods are hard to copy/replicate. What solution I better to use to solve this problem? Should I use visitor pattern (about which I don’t know much) ?

Thank you for any suggestion !

You can do
ICmpInst *II1 = dyn_cast<ICmpInst>(I1);

The "dyn_cast" is a local (to LLVM) template that handles the type casting. It will return 0 if the cast was invalid, so you may need to check the return value.

-Krzysztof