[LLVMdev] Cast instructions

Chris Lattner sabre at nondot.org
Wed Oct 5 13:56:40 PDT 2005

On Wed, 5 Oct 2005, Robert L. Bocchino Jr. wrote:
> The following situation comes up a lot in what I'm doing.  I want to 
> check if a value is a certain kind of instruction.  The test also needs 
> to succeed if the value is a *cast* of the instruction (or a cast of a 
> cast, etc.)  I.e., I need to "see through" any intervening casts to get 
> to the "real" value.  It's trivial to write a function that does this, 
> but does this functionality already exist somewhere in LLVM?  It's 
> something I'd like to write once and use a lot.

There isn't anything that does this.  In particular, casts can change 
values, so throwing them away indiscriminently is a bad idea.  I'd suggest 
writing a function to handle the case you care about.

However, if you're doing a lot of pattern matching, you should check out 
llvm/Support/PatternMatch.h.  It lets you write code like this:

//  Value *Exp = ...
//  Value *X, *Y;  ConstantInt *C1, *C2;      // (X & C1) | (Y & C2)
//  if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)),
//                      m_And(m_Value(Y), m_ConstantInt(C2))))) {
//    ... Pattern is matched and variables are bound ...
//  }

Check out InstructionCombining.cpp for more examples (search for 'match').



More information about the llvm-dev mailing list