[LLVMdev] GetElementPtrInst question

Krzysztof Parzyszek kparzysz at codeaurora.org
Sun Dec 2 08:37:14 PST 2012

On 12/2/2012 9:37 AM, Eduardo wrote:
> Hi all,
> How can I create an llvm::GetElementPtrInst in which the pointer and
> the index are in registers previously loaded with llvm::LoadInst ? I
> mean, the generated instruction will be like this:
>    %1 = getelementptr i8* %myreg1, i32 %myreg2
> here, %myreg1 and %myreg2 are previously created by load instructions
> (llvm::LoadInst).

Other than using the IR builder, you can create this instruction 
directly, using one of the "Create" functions, for example:

   static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
                                    const Twine &NameStr,
                                    Instruction *InsertBefore = 0) {
     GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore);
     return GEP;

The "Ptr" would be your "myreg1", and you'll need to create an ArrayRef 
that holds a single element: "myreg2".  Conveniently, ArrayRef has a 
constructor that creates a "singleton" array:

     /// Construct an ArrayRef from a single element.
     /*implicit*/ ArrayRef(const T &OneElt)
       : Data(&OneElt), Length(1) {}

Since it's not an explicit constructor, it may actually work if you do
   GEP = GetElementPtrInst::Create(myreg1, myreg2, "name.of.my.gep", 

Otherwise you'll need to create an ArrayRef that represents the 
arguments to the GetElemPtr instruction.


Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, 
hosted by The Linux Foundation

More information about the llvm-dev mailing list