[llvm-dev] Cast between struct

Jie Zhou via llvm-dev llvm-dev at lists.llvm.org
Thu May 7 14:38:11 PDT 2020



> On May 6, 2020, at 22:14, Krzysztof Parzyszek <kparzysz at quicinc.com> wrote:
> 
>> -----Original Message-----
>> 
>> Thanks for the help. I’m thinking the same way. Now I’m stuck with how to
>> create a struct; I know how to create a llvm::StructType but I’m not clear of
>> how to create a Value whose type is struct). It seems that llvm disallowed
>> directly create a Value object; all Values are created from one of its
>> numerous descendants. Can you give me some guidance here?
> 
> The "trick" is to start with "undef" (which always exists for any type).

Thanks very much for the direction. This UndefValue method works for me.

For anyone who’s interested in more details, here is the code snippet that
“cast” a struct of type {i8*, i64} to a struct of type {some_struct*, i64}:

// V is a struct of type {i8*, i64}, and RetIRTy is type {some_struct*, i64}.
llvm::UndefValue *newRet = llvm::UndefValue::get(RetIRTy);        
llvm::Value *genericPtr = Builder.CreateExtractValue(V, 0);       
llvm::Value *ID = Builder.CreateExtractValue(V, 1);               
llvm::Value *concretePtr = Builder.CreateBitCast                  
  (genericPtr, cast<llvm::StructType>(RetIRTy)->getElementType(0));
llvm::Value *insertPtr = Builder.CreateInsertValue(newRet, concretePtr, 0);
V = Builder.CreateInsertValue(insertPtr, ID, 1);

- Jie
> 
> -Krzysztof
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200507/670b9155/attachment.html>


More information about the llvm-dev mailing list