How to Extract `inttoptr` from a store instruction in LLVM IR?

Problem Statement

Hi folks. I tried to extract the target address of a store instruction:

store i32 0, ptr inttoptr (i32 268697856 to ptr), align 256, !dbg !4216, !tbaa !4166

To this end, I wrote a FunctionPass which analyzed the code at the LTO stage. Particularly, I casted the pointer operand of the store instruction into IntToPtrInst and dumped the instruction:

if (IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(store_inst->getPointerOperand())) {
    int_to_ptr_inst->dump();
}

However, the pointer operand of this store instruction cannot be casted into IntToPtrInst and the dumped nothing.
I guess the pointer operand is not IntToPtrInst. Can anyone tell me how to extract correctly? Thank you in advance!

Settings

  • LLVM version: 15.0.1

The operand is a ConstantExpr with IntToPtr opcode.

Thank you @nikic .
Is there any definition of StoreInst that describe the possible operands of it? It’s difficult to “guess” the correct type of an operand correctly.

This is not a special StoreInst feature, it’s something that can occur as an operand to any instruction. See LLVM Language Reference Manual — LLVM 17.0.0git documentation for the documentation of this “feature”.

Thank you @nikic .