how to get TargetData?

Dear all

I am trying to get the size of an LLVM pointer type.
getPrimitiveSizeInBits() returns 0 for it and the documentation for isSized() suggest to use TargetData.
I figured out from Kaleidoscope example that one can get a pointer to TagetData object through the execution engine but it seems to be an overkill.
What is the right way to do it?

Best regards,
Victor

Victor Zverovich wrote:

Dear all

I am trying to get the size of an LLVM pointer type.
getPrimitiveSizeInBits() returns 0 for it and the documentation for isSized() suggest to use TargetData.
I figured out from Kaleidoscope example that one can get a pointer to TagetData object through the execution engine but it seems to be an overkill.
What is the right way to do it?

TargetData is an LLVM pass. If you're writing an LLVM pass, just make TargetData one of its prerequisite analysis passes in the getAnalysisUsage() method and use the getAnalysis<TargetData>() function to get a reference to the TargetData pass within your pass.

-- John T.

Thanks, John.
I am writing not an LLVM pass but a compiler backend, so this doesn’t work for me.

Victor

For those targets supported by LLVM, you can get their TargetData by creating TargetMachine first (take X86 as example):

==== BEGIN CODE SNIPPET ====

const std::string TripleStr = “i686-unknown-linux”; // hard coded for example
const std::string FeatureStr = “”; // hard coded for example
std::string Err;
const Target* T;
TargetMachine* TM = NULL;
const TargetData* TD;

// Or just call InitializeAllTargetInfos() and InitializeAllTargets() for all targets enabled by your LLVM build.
LLVMInitializeX86TargetInfo();
LLVMInitializeX86Target();

T = TargetRegistry::lookupTarget(TripleStr, Err);
if(!Err.empty())
// error handling

// Create TargetMachine
TM = T->createTargetMachine(TripleStr, FeatureStr);
if(TM == NULL)
// error handling

// TD is what you want.
TD = TM->getTargetData();

[…]

// Free TM
delete TM;
==== END CODE SNIPPET ====

For your case, you should be able to find TripleStr and FeatureStr in somewhere.

And also there’s a constructor in TargetData (http://llvm.org/doxygen/classllvm_1_1TargetData.html#a0d7acb06af9665b54fc74480e2c6c707) takeing a string “TargetDescription”. The string specifies the data layout of a target and its format is described here http://llvm.org/docs/LangRef.html#datalayout. Thus, if you are developing a customized target and/or you are really know the data layout specification about your target, you can get a TargetData instance by simply passing the hand-coded data layout string to the constructor of TargetData.

Hope this will be helpful to you.

Zonr

Thanks a lot, Zonr. I will give it a try.

Victor