I want to find the size in bytes of a memory location pointed to by a pointer.
I have found a function that does it from MemoryBuiltins.h
bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL,
const TargetLibraryInfo *TLI, ObjectSizeOpts Opts = {});
and I used the DeadStoreElimination(DSE) transformation pass (in function getPointerSize) as an example of how to use it. However, when I pass the same IR in my pass, it always returns zero, while DSE returns the actual size. Am I doing something wrong?
How I create my TLI obj inside the function pass
const TargetLibraryInfo &TLI = AM.getResult<TargetLibraryAnalysis>(F);
The parameters of my transformation pass
static bool MyTransformationPass( Function &F, const TargetLibraryInfo &TLI)
Calling the getPointerSize function
if (Value *V = dyn_cast<Value>(inst)){
const DataLayout &DL = inst->getModule()->getDataLayout();
uint64_t ptrSize = getPointerSize(V, DL, TLI, &F);
errs() << "SIZE: " << ptrSize << "\n";
}
The getPointerSize that I copied from the DeadStoreElimination code
uint64_t getPointerSize(const Value *V, const DataLayout &DL,
const TargetLibraryInfo &TLI,
const Function *F) {
uint64_t Size;
ObjectSizeOpts Opts;
Opts.NullIsUnknownSize = NullPointerIsDefined(F);
if (getObjectSize(V, Size, DL, &TLI, Opts))
return Size;
return 0;
}
Lastly, is there an easier way of doing this, or its this the best way?