There are (at least) two kinds of casts in LLVM IR: BitCastInst and bitcast values. You have the later. Fortunately, there is a method for retrieving the original value within the bitcast: stripPointerCasts(). It took me sometime to figure out this distinction.

Here is my usage of the routine, where I was trying to identify the function called (BasicBlock::iterator I):

if (CallInst *ci = dyn_cast<CallInst>(&*I)) {
    Function *f = ci->getCalledFunction();

    if (f == NULL) 
    { 
        Value* v = ci->getCalledValue();
        f = dyn_cast<Function>(v->stripPointerCasts());
        if (f == NULL)
        {
            continue; 
        }
    }

    const char* fname = f->getName().data();
Answer from Brian on Stack Overflow
🌐
LLVM
llvm.org › doxygen › classllvm_1_1Function.html
LLVM: llvm::Function Class Reference
You can just say 'call F->viewCFG()' and a ghostview window should pop up from the program, displaying the CFG of the current function. This depends on there being a 'dot' and 'gv' program in your path. Definition at line 156 of file CFGPrinter.cpp. References viewCFG(). Referenced by viewCFG(), viewCFG(), viewCFGOnly(), and viewCFGOnly(). Extended form to print edge weights. Definition at line 162 of file CFGPrinter.cpp. References CFGFuncName, contains(), llvm::getMaxFreq(), getName(), llvm::Value::getName(), and llvm::ViewGraph().
🌐
Hdoc
docs.hdoc.io › hdoc › llvm-project › f3A01E6120389A14C.html
function getName: LLVM/Clang 15.x documentation - hdoc
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx". Note, this version is for intrinsics with no overloads. Use the other version of getName if overloads are required.
🌐
Carnegie Mellon University
cs.cmu.edu › afs › cs › academic › class › 15745-s15 › public › lectures › L6-LLVM2-1up.pdf pdf
Carnegie Mellon Lecture 6 More on the LLVM Compiler Jonathan Burket
Oddly, there’s not equivalent of std::endl in LLVM · Printing the Name of a Function: std::cout << F->getName().str() << std::endl; outs() << F->getName() << “\n”; Printing an Instruction: Instruction *I; I->dump(); or · outs() << *I << “\n”; Printing an Entire Basic Block: BB->dump() or ·
Top answer
1 of 2
3

There are (at least) two kinds of casts in LLVM IR: BitCastInst and bitcast values. You have the later. Fortunately, there is a method for retrieving the original value within the bitcast: stripPointerCasts(). It took me sometime to figure out this distinction.

Here is my usage of the routine, where I was trying to identify the function called (BasicBlock::iterator I):

if (CallInst *ci = dyn_cast<CallInst>(&*I)) {
    Function *f = ci->getCalledFunction();

    if (f == NULL) 
    { 
        Value* v = ci->getCalledValue();
        f = dyn_cast<Function>(v->stripPointerCasts());
        if (f == NULL)
        {
            continue; 
        }
    }

    const char* fname = f->getName().data();
2 of 2
1

As explained in the previous question asking the same thing [marginally different], you are better off using the AST form that the Clang compiler produces, rather than the LLVM IR form. It is a much more direct representation of the C or C++ code than the LLVM IR, and easier to work with in general.

But from the StoreInst you can use getValueOperand to get the value that is being stored, and then getName of the value. Of course, like I also said in comments the previous answer, it's not very hard to make the code hard to derive what the original value stored was.

In otherwords, if we have an llvm::Instruction *inst, we could do this:

if (llvm::StoreInst* si = llvm::dyn_cast<llvm::StoreInst>(inst))
{
    std::string name = si->getValueOperand()->getName();
}

[Code is not tested, not compiled, no guarantee provided, I just wrote it as part of this answer with the intention that it may work]

🌐
HotExamples
cpp.hotexamples.com › examples › llvm › Function › getName › cpp-function-getname-method-examples.html
C++ (Cpp) Function::getName Examples, llvm::Function::getName C++ (Cpp) Examples - HotExamples
bool DINOGlobal::shouldIgnoreFunction (const llvm::Function &F) { if (F.getName().find(DINOPrefix) == 0) { #ifdef DINO_VERBOSE llvm::outs() << "Skipping DINO function " << F.getName() << "\n"; #endif //DINO_VERBOSE return true; } return false; }
🌐
Google Groups
groups.google.com › g › llvm-dev › c › Qyh2Drq_H80
[LLVMdev] How to get the original function name in C++?
The other two names are called "name" and "display name". DWARF does define what a name should be, "a string representing the name as it appears in the source program" and goes on to clarify that it should be the unmangled form. Get it by calling DISubprogram::getName().
🌐
LLVM
lists.llvm.org › pipermail › llvm-dev › 2011-January › 037534.html
[LLVMdev] How to get the name and argument of a function
January 19, 2011 - >>>> >>>> With the CallInst/InvokeInst ... pointer) then you don't >>>> know >>>> what it's calling. Ignoring indirect calls, you can call getName() on a >>>> function to get its name....
Find elsewhere
🌐
GitHub
github.com › llvm-mirror › llvm › blob › master › lib › IR › Function.cpp
llvm/lib/IR/Function.cpp at master · llvm-mirror/llvm
void Function::recalculateIntrinsicID() { StringRef Name = getName(); if (!Name.startswith("llvm.")) { HasLLVMReservedName = false; IntID = Intrinsic::not_intrinsic; return; } HasLLVMReservedName = true; IntID = lookupIntrinsicID(Name); } ·
Author   llvm-mirror
🌐
LLVM Discussion Forums
discourse.llvm.org › project infrastructure › llvm dev list archives
Get the class name of the function - LLVM Dev List Archives - LLVM Discussion Forums
February 27, 2020 - Hey guys, I’m wondering if there is a way to write an llvm pass to get the class name of the functions. I am not talking about the simple self-written functions like foo::bar(…), but those STL container functions like ZNKSt4lessINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEclERKS5_S8, demangles as std::less const&, std::__cxx11::basic_stri...
🌐
Google Groups
groups.google.com › g › llvm-dev › c › uYtl-SsDGpI
[LLVMdev] How to get the name and argument of a function
January 17, 2011 - With the CallInst/InvokeInst you ... (ie., function pointer) then you don't know what it's calling. Ignoring indirect calls, you can call getName() on a function to get its name....
🌐
LLVM
lists.llvm.org › pipermail › llvm-dev › 2014-December › 079618.html
[LLVMdev] How to get the original function name in C++?
December 11, 2014 - > When LLVM translates C++ source code to IR, it will add a prefix to > the function name. For example: > source code: > int foo(){ > return 1; > } > IR form: > define i32 @_Z3foov() #0 { > entry: > ret i32 1, !dbg !20 > } > The getName() method returns _Z3foov, then how can I get foo?
Top answer
1 of 4
14

This is part of the debug information that's attached to LLVM IR in the form of metadata. Documentation is here. An old blog post with some background is also available.


$ cat  > z.c
long fact(long arg, long farg, long bart)
{
    long foo = farg + bart;
    return foo * arg;
}

$ clang -emit-llvm -O3 -g -c z.c
$ llvm-dis z.bc -o -

Produces this:

define i64 @fact(i64 %arg, i64 %farg, i64 %bart) #0 {
entry:
  tail call void @llvm.dbg.value(metadata !{i64 %arg}, i64 0, metadata !10), !dbg !17
  tail call void @llvm.dbg.value(metadata !{i64 %farg}, i64 0, metadata !11), !dbg !17
  tail call void @llvm.dbg.value(metadata !{i64 %bart}, i64 0, metadata !12), !dbg !17
  %add = add nsw i64 %bart, %farg, !dbg !18
  tail call void @llvm.dbg.value(metadata !{i64 %add}, i64 0, metadata !13), !dbg !18
  %mul = mul nsw i64 %add, %arg, !dbg !19
  ret i64 %mul, !dbg !19
}

With -O0 instead of -O3, you won't see llvm.dbg.value, but you will see llvm.dbg.declare.

2 of 4
12

Given a Value, getting variable name from it can be done by traversing all the llvm.dbg.declare and llvm.dbg.value calls in the enclosing function, checking if any refers to that value, and if so, return the DIVariable associated with the value by that intrinsic call.

So, the code should look something like (roughly, not tested or even compiled):

const Function* findEnclosingFunc(const Value* V) {
  if (const Argument* Arg = dyn_cast<Argument>(V)) {
    return Arg->getParent();
  }
  if (const Instruction* I = dyn_cast<Instruction>(V)) {
    return I->getParent()->getParent();
  }
  return NULL;
}

const MDNode* findVar(const Value* V, const Function* F) {
  for (const_inst_iterator Iter = inst_begin(F), End = inst_end(F); Iter != End; ++Iter) {
    const Instruction* I = &*Iter;
    if (const DbgDeclareInst* DbgDeclare = dyn_cast<DbgDeclareInst>(I)) {
      if (DbgDeclare->getAddress() == V) return DbgDeclare->getVariable();
    } else if (const DbgValueInst* DbgValue = dyn_cast<DbgValueInst>(I)) {
      if (DbgValue->getValue() == V) return DbgValue->getVariable();
    }
  }
  return NULL;
}

StringRef getOriginalName(const Value* V) {
  // TODO handle globals as well

  const Function* F = findEnclosingFunc(V);
  if (!F) return V->getName();

  const MDNode* Var = findVar(V, F);
  if (!Var) return "tmp";

  return DIVariable(Var).getName();
}

You can see above I was too lazy to add handling of globals, but it's not that big a deal actually - this requires iterating over all the globals listed under the current compile unit debug info (use M.getNamedMetadata("llvm.dbg.cu") to get a list of all the compile units in the current module), then checking which matches your variable (via the getGlobal method) and returning its name.

However, keep in mind the above will only work for values directly associated with original variables. Any value that is a result of any computation will not be properly named this way; and in particular, values that represent field accesses will not be named with the field name. This is doable but requires more involved processing - you'll have to identify the field number from the GEP, then dig into the type debug information for the struct to get back the field name. Debuggers do that, yes, but no debugger operates in LLVM IR land - as far as I know even LLVM's own LLDB works differently, by parsing the DWARF in the object file into Clang types.

🌐
LLVM
llvm.org › doxygen › classllvm_1_1Value.html
LLVM: llvm::Value Class Reference
Referenced by addReplicateRegions(), llvm::SPIRVGlobalRegistry::buildGlobalVariable(), llvm::CloneBasicBlock(), llvm::dumpBasicBlockLabel(), llvm::ExecutionEngine::emitGlobals(), llvm::AMDGPU::HSAMD::MetadataStreamerMsgPackV4::emitKernelArg(), llvm::emitLinkerFlagsForGlobalCOFF(), llvm::emitLinkerFlagsForUsedCOFF(), externalize(), extractSubModule(), llvm::JITSymbolFlags::fromGlobalValue(), getArm64ECMangledFunctionName(), llvm::ModuleSummaryIndex::getGlobalValueSummary(), llvm::ExecutionEngine::getMangledName(), llvm::Mangler::getNameWithPrefix(), llvm::TargetLoweringObjectFileXCOFF::getSecti