5

I am trying to get the name of the register in which the result of the load insutrction is stored from the LoadInst pointer.
For example, if my loadInst pointer points to this the instruction %0 = load i32* %i, align 4 then how should I get %0 from the instruction?

3 Answers 3

8

That %0 is the instruction's name, not a register name - there are no registers in the LLVM intermediate representation.

In any case, all instructions inherit from the Value class which defines a getName() method, and that's what you should call. However, keep in mind that typically many instruction will be unnamed and thus getName() won't return anything useful - names such as %0 are only assigned when emitting the module as text, and do not exist before that.

5

The first thing is that %0 is just a label. If we want to explicitly give it a name, there is an LLVM pass called instnamer. The following cmd I used to explicitly give the name for each label using instnamer pass

$ clang++  -std=c++11 -g -emit-llvm -c hello.c -o hello.bc
$ opt -instnamer -load ../your/path/to/library.so -passname   <hello.bc> hello.bc

Then in your LLVM pass i.e LLVM API :

if (LoadInst *loadInst = dyn_cast<LoadInst>(&I)) {
   loadInst->dump();
   errs()<<loadInst->getName(); // This is your %temp0  not %0 anymore the pass explicitly rewritten the LLVM IR to %temp0.
}

Hope this helps..

1
  • This should be the correct answer. I tried many functions and only by adding '-instnamer' it could output the explicit name. May 13, 2020 at 13:11
1

A callInst inherets from Value so you can get the Name by getName(). However is the value is unamed (has a name like %0) then that won't work as thet is no meaningfull value to return. SO if you want to get a name you need to give it a name.

2
  • 1
    Yes I am talking about the llvm api. For example LoadInst *loadInst = dyn_cast<LoadInst>(&*inst); Now for ICmpInst and AllocaInst, getName function returns the name of the regiser as const char * but I could not find any api of LoadInst which does the same.
    – ari
    Jan 4, 2014 at 21:10
  • 1
    Like the example below, I want to do the same for LoadInst. AllocaInst *alloca = dyn_cast<AllocaInst>(&*inst); errs()<<"Alloca instruction :"<<*alloca<<" Name is : "<<alloca->getName()<<"\n";
    – ari
    Jan 4, 2014 at 21:13

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.