[LLVMdev] How to get the indices in an getelementptr Value?

508 views
Skip to first unread message

Qiuping Yi

unread,
Nov 22, 2014, 3:36:23 AM11/22/14
to llv...@cs.uiuc.edu, Qiuping Yi
Hi, all

I am a LLVM user. I want to get every element in the next instruction:

%0 = load i32* getelementptr inbounds (%struct.Args* @globalArg, i64 0, i32 2), align 4, !dbg !85, !clap !86

Now I can only get value "i32* getelementptr inbounds (%struct.Args* @globalArg, i64 0, i32 2)" through "getOperand(0)",
but I can not get "%struct.Args* @globalArg", "i64 0", and "i32 2" in this instruction.

I know I can get the indices of getelementptr instructions through "getOperand()", but now in the previous example, 
I want to get the indices from a getlementptr value. How should I do? Thank you in advance. 

Best Regards!

--------------------------------------------
Qiuping Yi
Institute Of Software
Chinese Academy of Sciences

Michael Zolotukhin

unread,
Nov 22, 2014, 3:54:36 AM11/22/14
to Qiuping Yi, llv...@cs.uiuc.edu
Hi Qiuping,

I guess you need to use getPointerOperand to access the pointer operand (@globalArg in your example) and idx_begin/idx_end iterators to access indexes. You can find a short description of these functions in documentation [1], or look at examples of their uses in the code, e.g. in lib/Transforms/InstCombine/*.cpp.


Best regards,
Michael

_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

Qiuping Yi

unread,
Nov 22, 2014, 4:14:33 AM11/22/14
to Michael Zolotukhin, llv...@cs.uiuc.edu
Hi Michael,
 
Thank you very much.

But idx_begin/idx_end iterators can only be used through a getelementptr instruction, right? However, I think value "i32* getelementptr inbounds (%struct.Args* @globalArg, i64 0, i32 2)" itself is not a getelementptr instruction, so? Or could you tell me how can I get a getelementptr instruction first from this value?


--------------------------------------------
Qiuping Yi
Institute Of Software
Chinese Academy of Sciences

Sanjoy Das

unread,
Nov 22, 2014, 2:13:21 PM11/22/14
to Qiuping Yi, LLVM Developers Mailing List
Hi Qiuping,

If I'm reading the IR correctly, what you have is a
GetElementPtrConstantExpr [1]. It subclasses from llvm::Constant.

Thanks,
-- Sanjoy

[1]: http://llvm.org/docs/doxygen/html/classllvm_1_1GetElementPtrConstantExpr.html

David Majnemer

unread,
Nov 22, 2014, 3:10:51 PM11/22/14
to Sanjoy Das, Qiuping Yi, LLVM Developers Mailing List
On Sat, Nov 22, 2014 at 11:09 AM, Sanjoy Das <san...@playingwithpointers.com> wrote:
Hi Qiuping,

If I'm reading the IR correctly, what you have is a
GetElementPtrConstantExpr [1].  It subclasses from llvm::Constant.

If you want the same code to handle GetElementPtrConstantExpr *and* GetElementPtrInst, you can use GEPOperator.

Qiuping Yi

unread,
Nov 22, 2014, 11:41:31 PM11/22/14
to David Majnemer, Sanjoy Das, LLVM Developers Mailing List
Thank you very much for your useful information. As you say, it is a subclass llvm::ConstantExpr.

Now I cast value "i32* getelementptr inbounds (%struct.Args* @globalArg, i64 0, i32 2)"  to its subclass llvm::Constant and then subclass llvm::ConstantExpr, and then I can access its indices through 

for (unsigned i=0; i<constExpr->getNumOperands(); ++i) 
    v = constExpr->getOperand(i);

However, I have another question. The invoking of getNumOperands() on value "i32* getelementptr inbounds (%struct.Args* @globalArg, i64 0, i32 2)" returns 3, but it returns 1 on value "i32* getelementptr inbounds (%struct.Args* @globalArg, i64 0, i32 0)", and furthermore, through value "i32* getelementptr inbounds (%struct.Args* @globalArg, i64 0, i32 2)" is subclass llvm::Constant, but it is not subclass llvm::ConstantExpr, why?   



--------------------------------------------
Qiuping Yi
Institute Of Software
Chinese Academy of Sciences

Qiuping Yi

unread,
Nov 23, 2014, 2:20:54 AM11/23/14
to David Majnemer, LLVM Developers Mailing List
Hi David Majnemer,

I found I can cast value "i32* getelementptr inbounds (%struct.Args* @globalArg, i64 0, i32 2)" to class GEPOperator, but failed on value "i32* getelementptr inbounds (%struct.Args* @globalArg, i64 0, i32 0)". As my previous email described, I found that the later value is a class llvm::Constant but not a subclass llvm::ConstantExpr. I find it is very strange. 


--------------------------------------------
Qiuping Yi
Institute Of Software
Chinese Academy of Sciences

On Sun, Nov 23, 2014 at 4:08 AM, David Majnemer <david.m...@gmail.com> wrote:

Qiuping Yi

unread,
Nov 23, 2014, 2:46:50 AM11/23/14
to David Majnemer, LLVM Developers Mailing List
I find the root of my problem. I mistakenly invoking stripPointerCasts() before casting. Now I can access these elements by casting the related value to subclass llvm::constantExpr or GEPOperator. Thank you all. 


--------------------------------------------
Qiuping Yi
Institute Of Software
Chinese Academy of Sciences

Reply all
Reply to author
Forward
0 new messages