[LLVMdev] [LLVMDev] How to use memcpy with getOrInsertFunction()

David Wiberg dwiberg at gmail.com
Mon Aug 3 23:59:53 PDT 2015


Hi,

I have a partial answer for you, see below. Hopefully someone else
will answer the other part of your question.

2015-08-04 4:34 GMT+02:00 Prem Anand <prem.iamme at gmail.com>:
>
> A)
> In LLVM, why are memcpy (and other memory related functions) treated
> specially? i.e why do they have this intrinsic notion.

The intrinsics capture information about alignment which can be used
to produce better code. See [1] and [2] for some more information.

> The IR generated is like the following, while the normal functions differ.
>
> call void @llvm.memcpy.p0i8.p0i8.i64(....)
> call void @my_function(...)
>
> Can anyone explain me what the p0* mean in the above IR of memcpy?

The 'p' means that the argument is a pointer type and '0' is the
address space of the pointer type. If you are interested, the name is
created by "Intrinsic::getName" in "lib/IR/Function.cpp".

/ David

>
> B)
> How to insert a call to "memcpy(A, B, 252);" before a particular basic
> block. Assume A and B are integer64 array of 252 size each.
> Can anyone help me figure out the parameter that needs to be placed in
> '??' below
>
>     my_memcpy = M.getOrInsertFunction("memcpy",
>                                     FunctionType::getInt8PtrTy(Context),
>                                     Type::getInt8PtrTy(Context),
>                                     Type::getInt8PtrTy(Context),
>                                     Type::getInt64Ty(Context),
>                                     NULL);
>
>
>         Value *my_params[] = {
>             builder.CreateIntToPtr(builder.getInt64( ?? ),
> Type::getInt8PtrTy(Context)),
>             builder.CreateIntToPtr(builder.getInt64( ??),
> Type::getInt8PtrTy(Context)),
>             ConstantInt::get(Context, APInt(64, 252)),
>         };
>         Function *func_ptr= cast<Function>(my_memcpy);
>         builder.CreateCall(func_ptr, my_params);
>
>
> Hoping for a reply,
> Prem Anand
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

[1] http://article.gmane.org/gmane.comp.compilers.llvm.devel/12102/match=memcpy+intrinsic
[2] http://stackoverflow.com/questions/25302796/why-are-there-some-intrinsics-in-llvm-language



More information about the llvm-dev mailing list