[llvm-dev] Cast a function parameter to GEP

Tim Northover via llvm-dev llvm-dev at lists.llvm.org
Fri Mar 8 23:41:53 PST 2019


On Sat, 9 Mar 2019 at 07:22, Alberto Barbaro <barbaro.alberto at gmail.com> wrote:
> I now need to find the value of the constant expression and I found online a solution like: string value = cast<ConstantDataArray>(cast<GlobalVariable>(CallInstruction->getOperand(0))->getInitializer())->getAsCString();

> This one works but I feel the GEPOperator should be used based on your comments, so I tried to cast the operator and get the value but I'm not able to do it.

If that one works it sounds like you don't have any kind of GEP at
that point. Otherwise the cast to GlobalVariable would fail. Have you
tried calling Module::dump while that code is executing to see what
you're really dealing with?

> I cast using GEPOperator *op = dyn_cast<GEPOperator>(CI->getOperand(0));

That looks right to me if you have a GEP.  Once you've got that you'd
have to carefully check the operands of the GEP to make sure no-one is
doing anything tricksy like

    printf(LocalVariable); //
cast<GlobalVariable>(MyGEP->getPointerOperand) will fail so use
dyn_cast and check the result.
    printf(&"MwahahahaHello world\n"[9]); // The GEP will have
non-zero index operands. They may not even be constant.

Cheers.

Tim.


More information about the llvm-dev mailing list