Jaeger
Jaeger

Reputation: 169

Analyze store instruction consisting inttoptr in LLVM

I am trying to analyze a byte code consisting of a store instruction with inttoptr. I am having trouble to detect whether a store instruction has the inttoptr value as the value operand (3rd instruction in the following code in entry BB). My opcode looks like the following:

define dso_local i32 @test(i32* %p) #0 {
entry:
  %p.addr = alloca i32*, align 8
  store i32* %p, i32** %p.addr, align 8
  store i32* inttoptr (i64 1000 to i32*), i32** %p.addr, align 8
  %0 = load i32*, i32** %p.addr, align 8
  %1 = load i32, i32* %0, align 4
  ret i32 %1
}

I am trying to analyze the store instruction and trying to find whether inttoptr is in a store instruction by using classof method and with dyn_cast like the following code:

  StoreInst *store = dyn_cast<StoreInst>(I);
  Value *vv = store->getValueOperand();
  Value *vp = store->getPointerOperand();
  if(IntToPtrInst::classof(vv)){
    outs() << "Inttoptr found\n";
  }
  if(Instruction *inp = dyn_cast<IntToPtrInst>(vv)){
    outs() << "Inttoptr found\n";
  }

It seems I am not being able to detect inttoptr with any of the methods. I know the byte code is not creating a separate instruction for the inttoptr but it is merging with the store instruction. It would be really nice if anyone points me what I am missing and how I can detect the inttoptr in a store instruction.

Upvotes: 1

Views: 617

Answers (1)

arnt
arnt

Reputation: 9685

The cast you're interested in is not an instruction, but rather a constant cast from the constant integer 1000 to a pointer. You can detect it using a test like isa<ConstantExpr>(foo->getPointerOperand()) && cast<ConstantExpr>(foo->getPointerOperand())->getOpcode() == ConstantExpr::IntToPtrCast, but I typed that from memory and I'm sure there are typos.

When you read IR, instructions are always on their own line, while constants are inline as arguments or initialisers, even the quite complex constants produced using ConstantExpr.

Upvotes: 2

Related Questions