invalid cast semantics


I have some code that tries to flatten pointers to pointer-sized
integer type. Trying to approximate what resulting IR the compiler
produced when doing an pointer to integer cast in c/c++, I reached to
this snippet of code:

inputLLVMValue is a llvm::Value* that points to some pointer-to-8-word
integer type:
addrIntTypellvm is an 8-word integer type:

llvm::Type* ptrType = llvm::Type::getInt8PtrTy(getLLVMContext());
                llvm::Value* vCast =
buildParameters.builder.CreateBitCast( inputLLVMValue, ptrType ,
                llvm::AllocaInst* alv =
Hive::Compiler::Detail::CreateLLVMTypedEntryBlockAlloca( llvmFunction,
Hive::AST::SymbolName_t("vCast"), ptrType);
                llvm::StoreInst* stv =
buildParameters.builder.CreateStore(vCast, alv);
                llvm::LoadInst* lv = builder.CreateLoad(alv, "vCastLoad");
                llvm::Value* iCast =
buildParameters.builder.CreatePtrToInt( lv, addrIntTypellvm,
                llvm::AllocaInst* ali =
                llvm::StoreInst* sti =
buildParameters.builder.CreateStore(iCast, ali);
                llvm::LoadInst* li = builder.CreateLoad(ali, "iCastLoad");

I then use the result of li as any other exprssion

Apparently my code is raising an invalid cast assertion for this test

(Found at IR/Instructions.cpp: CastInst::castIsValid:

return SrcTy->getScalarType()->isPointerTy() &&

when trying to create CreatePtrToInt instruction.

Any idea what might be causing this? What other information could help
me debug this problem?

Rather than squinting at this, I suggest that you set a breakpoint
here, and inspect lv->getScalarType(),
lv->getScalarType()->isPointerTy(), addrIntTypellvm->getScalarType(),
and addrIntTypellvm->getScalarType()->isIntegerTy() yourself.

Hope that helps.