Detecting GEPs embedded inside other instructions

Hello All

I am writing a simple pass to detect all GEPs inside a function. While I am able to detected normal GEP instructions, I am having some trouble with GEPs which are embedded inside other instructions.

For Example:
%3 = load float, float* getelementptr inbounds (<{ float, [199 x float] }>, <{ float, [199 x float] }>* @gl_b, i64 0, i32 1, i64 22), align 4

Here the LoadInst has a GEP inside it. Is it possible to prevent LLVM from generating such complex IR?

Alternatively, is there a way that I can extract the GEP embedded inside the LoadInst, so that I have a GetElementPtrInst at the end? This is because I want to run some analysis on the GEP itself and the GetElementPtrInst API has some of the functions that I want to use.

Thanks in advance!

This is a constant expression GEP. You can abstract over both by casting to GEPOperator, which should also provide all the methods you need.

There are various ways to convert constant expressions to instructions, but the simplest one is to use the bitcode reader with the -expand-constant-exprs option.

1 Like