Search code examples
compiler-constructionclangllvmllvm-ir

Get size of memory access with non-primitive type


I am trying to get the size of memory access (load) using

dyn_cast<LoadInst>(Inst)->getType()->getPrimitiveSizeinBits()

which works for most cases.

However, for load instructions like

%18 = load i8*, i8** %11, align 8, !tbaa !10

where we have double star (**) pointer access, such method would only return a value of 0. I am wondering is there any method that could allow me to get the size of loading in this case?

Thanks in advance,
Tom


Solution

  • If you want the type of the primitive regardless of how many indirection levels are there, you could do something like this:

    llvm::Type *t = [...]; // getting the type varies on instruction and operand required
    
    while(t && t->isPointerTy()) {
      t = llvm::dyn_cast<llvm::PointerType>(t)->getElementType();
    }