How can I create an Instruction `%3 = call i8* inttoptr (i64 1 to i8* (i8*, i8*)*)(i8* bitcast (%struct.anon* @pin_counter to i8*), i8* nonnull %2)` using `IRBuilder`

Hi, I am new to LLVM, and I want to create an instruction %3 = call i8* inttoptr (i64 1 to i8* (i8*, i8*)*)(i8* bitcast (%struct.anon* @pin_counter to i8*), i8* nonnull %2) using IRBuilder. How can I do this?

You can create an i64 constant with getInt64, a bitcast with CreateBitCast, an inttoptr with CreateIntToPtr and a call with CreateCall. That just leaves looking up the global with getNamedGlobal on the Module (not the IRBuilder).

Hi. You might want to look into opaque pointers. CLANG-15/LLVM-15 is moving to use opaque pointers instead of typed pointers.
As a side affect, it would remove the need for the bitcast.

Hi, thank you for your reply. But I encounter another error.

     /*3  */insts.push_back(builder1.getInt64(1));
    /*4  */insts.push_back(builder1.CreateBitCast(pinCounter,  builder1.getInt8PtrTy()));
    Type* resType = builder1.getInt8PtrTy();
    ArrayRef<Type*> Params = {builder1.getInt8PtrTy(),builder1.getInt8PtrTy()};

    /*5  */insts.push_back(builder1.CreateIntToPtr(insts[3],PointerType::get(FunctionType::get(resType, Params,false),0)));
    
    ArrayRef<Value *> Args = {insts[4], insts[1]};
    /*6  */insts.push_back(builder1.CreateCall(FunctionType::get(resType, Params,false),insts[5],Args));

the /*index*/ means the index in the array insts. But I got the following error:

Called function is not the same type as the call!
  %3 = call i8* inttoptr (i64 1 to i8* (i8*, i8*)*)(i8* bitcast (%struct.anon* @pin_counter to i8*), i8* %2)

I see. Thank you. But I have not upgraded my llvm version. I will check it later.

Do I miss something? To be more precise, I cannot understand how to get the struct (i64 1 to i8* (i8*, i8*)*). In fact, we can generate it but the llvm reject it. Could you please tell me how to solve my error?