user3314339
user3314339

Reputation: 63

LLVM Can't find getelementptr instruction

I have this byte code fragment:

define void @setGlobal(i32 %a) #0 {
entry:
  %a.addr = alloca i32, align 4
  store i32 %a, i32* %a.addr, align 4
  %0 = load i32* %a.addr, align 4
  store i32 %0, i32* @Global, align 4
  %1 = load i32* %a.addr, align 4
  store i32 %1, i32* getelementptr inbounds ([5 x i32]* @GlobalVec, i32 0, i64 0), align 4
  store i32 2, i32* getelementptr inbounds ([5 x i32]* @GlobalVec, i32 0, i64 2), align 4
  ret void
}

I am using this code to find the getelementptr from "store i32 %1, i32* getelementptr inbounds ([5 x i32]* @GlobalVec, i32 0, i64 0), align 4":

for (Module::iterator F = p_Module.begin(), endF = p_Module.end(); F != endF; ++F) {

        for (Function::iterator BB = F->begin(), endBB = F->end(); BB != endBB; ++BB) {

            for (BasicBlock::iterator I = BB->begin(), endI = BB->end(); I
                    != endI; ++I) {
                if (StoreInst* SI = dyn_cast<StoreInst>(I)) {
                    if (Instruction *gep = dyn_cast<Instruction>(SI->getOperand(1)))
                    {
                        if (gep->getOpcode() == Instruction::GetElementPtr)
                        {
                            //do something
                        }

                    }

                }

            }
        }
}

This code can't find the getelementptr. What am I doing wrong?

Upvotes: 6

Views: 1277

Answers (1)

Oak
Oak

Reputation: 26868

There are no getelementptr instructions in your bitcode snippet, which is why you can't find them.

The two cases that look like a getelementptr instructions are actually constant expressions - the telltale sign is that they appear as part of another instruction (store), which is not something you can do with regular instructions.

So if you want to search for that expression, you need to look for type GetElementPtrConstantExpr, not GetElementPtrInst.

Upvotes: 6

Related Questions