[llvm-dev] How to find variable names from LLVM IR?

Dhriti Khanna via llvm-dev llvm-dev at lists.llvm.org
Sun May 29 03:41:46 PDT 2016


If I have an operand as a Value from an instruction like: Value* V =
opnd->get(); and I am sure this is a variable, I want to know the variable
name (in source code) for this Value object. I am doing it like this:

                                   const Function* Func;
                if (const Argument* Arg = dyn_cast<Argument>(V))
                {
                  Func = Arg->getParent();
                }
                else if (const Instruction* I = dyn_cast<Instruction>(V))
                {
                  Func = I->getParent()->getParent();
                }

                StringRef name;
                if (!Func)
                   name = V->getName();

                else
                {
                  const DILocalVariable* Var = NULL;

                  for (const_inst_iterator Iter = inst_begin(Func), End =
inst_end(Func); Iter != End; ++Iter)
                  {
                    if (const DbgDeclareInst* DbgDeclare =
dyn_cast<DbgDeclareInst>(&*Iter))
                    {
                      if (DbgDeclare->getAddress() == V) Var =
DbgDeclare->getVariable();
                    }
                    else if (const DbgValueInst* DbgValue =
dyn_cast<DbgValueInst>(&*Iter))
                    {
                      if (DbgValue->getValue() == V) Var =
DbgValue->getVariable();
                    }
                  }

                  name = Var->getName();
                }
                errs() << "\nVariableName: " << name.str() << "\n";

But  this condition: if(DbgDeclare->getAddress() == V) produces false
result as the address for the Value object differs from every Value object
found in dbg.declare intrinsic. Is this not how one finds the variable
name??
What am I doing wrong?

--
Regards
Dhriti Khanna
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160529/0b33995a/attachment.html>


More information about the llvm-dev mailing list