Check if getElementPtr Operand


consider the following IR code :
%count4 = getelementptr inbounds %struct.r32, %struct.r32* %cur.087, i64 0, i32 4

How to check in the instruction, whether the operand is a structure or not

GetElementPtrInst *getElementPtrInst=dyn_cast<GetElementPtrInst>(&instruction);

//check if getElemetPtrInst operands structure or array.


Thank You


Bernard Nongpoh

Take a look at visitGetElementPtrInst in InstructionCombining.cpp for some examples about how to iterate over GEP and check for type.

Hope I understood your question correctly.

Thanks Ryan, I’m able to retrieved the type using the following code:

Type *type=getElementPtrInst->getSourceElementType();

Hi, just a remark in your code below

dyn_cast does the job of isa already (and the job of cast if isa is true), here you better do this directly:

if( GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&instruction) ) {
	// processing on GEP

And beware of your isa: it should operate on a pointer to an instruction, not the instruction instance itself (just like for dyn_cast). Hope that helps, Pierre

Hello Pierre,


Thanks for your recommendation,