[llvm-dev] llvm extract struct elements and struct size in C++

Shehbaz Jaffer via llvm-dev llvm-dev at lists.llvm.org
Mon Mar 28 13:34:09 PDT 2016


LLVM Newbie here. I have the following C++ program

using namespace std;
struct A{
  int i;
  int j;
};

int main()
{
   struct A obj;
   obj.i = 10;
   obj.j = obj.i;
   return 0;
}

Using clang++, I can see that LLVM IR contains struct field as below

%struct.A = type { i32, i32 }

I would like to obtain the structure elements using LLVM Pass. I write
the following program - that iterates through both global variables,
and each of the Instruction operands, but none of them help me in
extracting struct A or A.i or A.j.

    #include "llvm/Pass.h"
    #include "llvm/IR/Function.h"
    #include "llvm/Support/raw_ostream.h"

    #include <llvm/IR/Constants.h>
    #include <llvm/IR/DerivedTypes.h>
    #include <llvm/IR/Instructions.h>
    #include <llvm/IR/IntrinsicInst.h>
    #include <llvm/IR/LLVMContext.h>
    #include <llvm/IR/Module.h>

    #include <iostream>
    #include <map>
    #include <vector>


    using namespace llvm;

    namespace {

    class StructModulePass: public ModulePass {
    public:
    static char ID;
    StructModulePass() : ModulePass(ID) {}
    virtual bool runOnModule(Module &M1) override {
    // iterate over global structures
    M = &M1;
    int i;
    for(auto G = M->global_begin(); G!= M->global_end() ; G++, i++){
    errs() << i << " == > " ;
    errs().write_escaped(G->getName()) << "\n";
  }

// iterate through each instruction. module->function->BB->Inst
  for(auto &F_ : M->functions()){
    F = &F_;
    for(auto &B_ : *F)
      B = &B_;
      for(auto &I : *B) {
        for (unsigned i = 0; i < I.getNumOperands(); i++)
          std::cerr << I.getOperand(i)->getName().data() << std::endl;
      }
  }
  return true;
  }
private:
  Module *M;
  Function *F;
  BasicBlock *B;
};
  }

char StructModulePass:: ID = 0;
static RegisterPass<StructModulePass> X("getstructnamesize", "Get All
Struct Names and Sizes",
                             false /* Only looks at CFG */ ,
                             false /* Analysis Pass */);

I want to create a database of all structures (global and local)
defined and being used in my program. Eg. < A , <int32, int32> , B ,
<int32, bool , char *>>.

I have gone through doxygen pages, LLVM tutorials and checked if we
can get the struct values, but I am unable to find a way to extract
the structures without already knowing the struct values - eg.
creating an IRBuilder, inserting predefined IntTy32 type variables.
Any help in this regard or some relevant tutorials will help

-- 
Shehbaz Jaffer
First Year Graduate Student
Sir Edward S Rogers Sr Department of Electrical and Computer Engineering
University of Toronto


More information about the llvm-dev mailing list