ConstantExpr::getIntToPtr()
creates a constant expression. So in effect, what you're trying to generate is equivalent to this IR:
%2 = load i32* inttoptr (i32 %1 to i32*)
But this is illegal since a constant expression, as hinted by its name, only supports constants, and %1 isn't a constant. ConstantExpr::getIntToPtr()
requires a Constant
as a first argument to verify it, but you passed it a non-constant value which was forcefully cast to a constant.
The correct way to convert a non-constant integer to a pointer is with IRBuilder::createIntToPtr
. However, since you say the previous value (loc[n]
) was created via an alloca
then it's already a pointer, and you don't need to perform any conversion: just do builder->CreateLoad(loc[n])
.
By the way, the proper way to cast a Value to a Constant in LLVM is not via a c-style cast but via cast<>
, like so: cast<Constant>(loc[n])
.