[LLVMdev] Cast specific pointer type to generic one

Richard Pennington rich at pennware.com
Sat Mar 29 17:41:44 PDT 2014


On 03/29/2014 07:26 PM, David Blaikie wrote:
> On Sat, Mar 29, 2014 at 3:55 PM, Tehila Mayzels
> <tehila at cs.technion.ac.il> wrote:
>> Hi,
>>
>>
>>
>> Suppose I have a pointer to "something" (a structure I defined) and I want
>> to pass the pointer to a generic function, that gets a 64-bit address
>> pointer.
>>
>> How do I do that?
>>
>>
>>
>> For instance:
>>
>> The function is:
>>
>> void Foo (void *);
>>
>> I get the specific pointer using getPointerOperand() on a store instruction
>> that store to it:
>>
>> inst->getPointerOperand()->getType()
>>
>>
>>
>> Now I want to call Foo with the pointer, but the types of the arguments are
>> mismatches - one is specific (%"class.XXX::YYY"*) and the other generic.
>>
>> How should I cast it so there won't be a mismatch?
> Perhaps compiling similar code with Clang and looking at Clang's
> generated IR would give you some insight into how that might be done.
>
Plugging

void foo(void *p)
{
}

int main()
{
     struct foo { int a; } fum;
     foo(&fum);
}

into http://ellcc.org/blog/?page_id=340 (the demo) with optimization set 
to none and selecting "LLVM C++ API code" gives (among a lot of other 
stuff):

  // Function: main (func_main)
  {

   BasicBlock* label_entry_10 = BasicBlock::Create(mod->getContext(), 
"entry",func_main,0);

   // Block entry (label_entry_10)
   AllocaInst* ptr_fum = new AllocaInst(StructTy_struct_foo, "fum", 
label_entry_10);
   ptr_fum->setAlignment(4);
   CastInst* ptr_11 = new BitCastInst(ptr_fum, PointerTy_1, "", 
label_entry_10);
   CallInst* void_12 = CallInst::Create(func_foo, ptr_11, "", 
label_entry_10);
   void_12->setCallingConv(CallingConv::C);
   void_12->setTailCall(false);
   AttributeSet void_12_PAL;
   void_12->setAttributes(void_12_PAL);

   ReturnInst::Create(mod->getContext(), const_int32_7, label_entry_10);

  }

  return mod;
}

The BitCastInst is the key.

-Rich
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140329/78094d56/attachment.html>


More information about the llvm-dev mailing list