[llvm-dev] Force casting a Value*

Dipanjan Das via llvm-dev llvm-dev at lists.llvm.org
Sun Jun 11 13:16:07 PDT 2017


On 11 June 2017 at 12:05, Tim Northover <t.p.northover at gmail.com> wrote:

> On 11 June 2017 at 11:56, Dipanjan Das via llvm-dev
> <llvm-dev at lists.llvm.org> wrote:
> > I can't pass var_value to a function accepting uint64_t. LLVM complains
> > about broken function call.
>
> Well, yes. var_value has type "ConstantInt *", not uint64_t. Assuming
> the value being stored actually is a constant known at compile-time
> you should be able to use cast<ConstantInt *>(vo)->getZExtValue() to
> retrieve a uint64_t value (careful of i128s!).
>
> If that cast fails then you're not dealing with a constant store. This
> is either a nuisance (if you were expecting the possibility and can
> deal with it) or a huge misunderstanding over compile-time vs runtime
> values.
>

I think it's a misunderstanding in may part. Let me explain my intention
first. I want to retrieve the value (if isPointerTy() returns true) to be
stored by a store instruction at run-time and pass on that value as either
an unsigned int (64) or a pointer (char*) to an external function. The call
to that function is inserted using IRBuilder.CreateCall().

Consider the code snippet below.

==================================================

if (StoreInst *store_inst = dyn_cast<StoreInst>(&I)) {
                    Value* vo = store_inst->getValueOperand();
                     uint64 var_value = /* cast vo to unsigned int 64 bit */

                   // Pass on this value to external function
                    IRBuilder<> builder(&I);
                    builder.SetInsertPoint(&B, ++builder.GetInsertPoint());
                    Constant *func =
F.getParent()->getOrInsertFunction("instrument_store",

Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), NULL);
                    Value* args[] = {var_value};
                    builder.CreateCall(func, args);
 }

==================================================

To reiterate, I want to wrap up the run-time value (var_value) as a Value*
and call the method (instrument_store) with that value as an argument.



> If it's a nuisance, you can use "dyn_cast" instead of "cast". That'll
> return nullptr if the store wasn't a constant instead of crashing; you
> can decide what to do at that point.
>
> Cheers.
>
> Tim.
>



-- 

Thanks & Regards,
Dipanjan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170611/45eba5d1/attachment.html>


More information about the llvm-dev mailing list