[llvm-dev] Accessing constant contents

Johannes Doerfert via llvm-dev llvm-dev at lists.llvm.org
Sat Aug 1 15:32:41 PDT 2020


Hi James,


I haven't tried it but if my memory is not failing me you should be able to

ask for the initializer of the llvm::GlobalVariable, which is a constant.

That should actually be a llvm::ConstantDataSequential in this case 
which has

methods like isString, getString, getCString,...


Oh, and your value1 is the llvm::GlobalVariable I am talking about, so 
something along the lines of:


auto *GV = cast<GlobalVariable>(value1);

auto *CDS = cast<ConstantDataSequential>(GV->getInitializer());

CDS->getString();


Hope this helps,

   Johannes


On 8/1/20 3:25 PM, James Courtier-Dutton via llvm-dev wrote:
> Hi,
>
> I have a global variable in LLVM IR:
>
> @mem8 = private unnamed_addr constant [9 x i8] c"Hello12\0A\00", align 1
>
> I have loaded the module, so now have:
> llvm::Value *value1;
> ...
> value1->print(llvm::outs());
> That will output:
> @mem8 = private unnamed_addr constant [9 x i8] c"Hello12\0A\00", align 1
>
> What method should I use to access the contents of the constant.
> I.e. I want the "Hello12" bytes contained in @mem8 to be put in a
> std::vector<int8_t>
>
> I can access the Type hierarchy, but cannot find how to access the
> value contents.
>
> Kind Regards
>
> James
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev


More information about the llvm-dev mailing list