Modifying ConstantExpr

Hi All,
I have the below statement stored as const_expr of type ConstantExpr.
Statement : i8
getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0)

I am looking to change [3 x i8] to say [5 x i8].

If I do the below, I am able to get [3 x i8], but I am not able to modify. Should I try differently? Any inputs please?
GEPOperator *gep = cast(const_expr)
*(gep->getSourceElementType()) //This prints it properly

Thanks!

Hi!

First of all, please be aware that LLVM is now transitioning to opaque pointer types for real. Very soon, you won’t have [3 x i8]* types anymore, it’ll all just be ptr types.

That said: the common approach to doing this sort of thing is to build new instructions (or constantexprs – but more likely instructions that are automatically constant-folded, depending on your context). That is, you’d first create a bitcast of the GEP operand @str to the desired type and then create a new GEP. Then you’d typically do replaceAllUsesWith (RAUW).

In your case, the GEP itself is a constant, so the details of the RAUW may differ. It depends on the larger context of what you’re trying to do.

Thanks,
Nicolai

Thank you. Your suggestion seems to work. Just to share, the below is what I did.

Constant *GEPIndices[2] = {
ConstantInt::get(Type::getInt64Ty(F.getContext()), 0),
ConstantInt::get(Type::getInt64Ty(F.getContext()), 0)};
Constant *GEP = ConstantExpr::getGetElementPtr(s1Ty, g1, GEPIndices);
i->setOperand(0,GEP);