[LLVMdev] Re: llvm question

Chris Lattner sabre at nondot.org
Sun Sep 29 12:17:01 PDT 2002


Sorry for the delay Lee, I was out of town yesterday.  In general it's
better to send questions to the LLVMdev list...

> hi.  all i want to do now is make a new SetCondInst that i can replace
> some old ones with, and it is turning out *impossible*.  the first
> parameter is the opcode, which is of class BinaryOps, which turns out to
> be an enum i can't understand in Instruction; i'm not sure how i'd just

Do something like this: BinaryOperator::create(Instruction::SetNE, A, B,
Name);

That wierd enum is an instruction opcode, such as Instruction::Add,
Instruction::SetLE, etc... for comparisons obviously only the SetCC should
be used.  Check out include/llvm/Instruction.def to see where they come
from.

> say i want it to be a eq or an ne.  the next two parameters have to be
> values; i want to just put constants in there, like 1s or something, and i
> cannot for the life of me make that happen.  i thought i'd figured out
> that
> (Value *) new ConstantUInt( Type::UIntTy, 1 )
> would give me a '1' for a constant, cast as a value, that i could use as
> one of the operands to my new SetCondInst().  but making a new
> constantuint is a protected operation and it won't let me do it.

Ok, yeah that won't work.  Try this instead:

ConstantUInt::get(Type::UIntTy, 1);

> i know c, not c++ really, and to me trying to work this out has just been
> flipping from class to class, traversing this chain with no real end.
> please, how can i just get an integer be an argument to a setcc
> instruction?

Yeah, this is just an instance where your C++'fu let you down (don't
worry, it gets stronger with practice :).  In general the problem with C++
ctors is:

A. They can't fail
B. They always return a newly allocated object.

In this case, A isn't a problem, but B is.  For LLVM constants, we want to
guarantee that two instances of the same constant (say an unsigned int 1)
are only ever allocated once, and after that they get recycled.  This
allows a couple of nice properties for LLVM constants (and types are the
same way):

1. It saves a marginal amount of memory
2. You don't have to worry about deleting constants or handling their
   lifetime.
3. You can compare two constants for equality by doing a pointer
   comparison (as opposed to some recursive by-value comparison)

So for these reasons, sometimes in LLVM you'll see some classes with
protected CTors (such as constants and types).  In general there will be
some kind of ::get or ::create method that you can use to "create" a new
thing.  The name ::get reflects the fact that you're not actually creating
a new object (neccesarily).

Hope this helps, good luck with the MP.

-Chris




More information about the llvm-dev mailing list