Hello, I'm new to llvm development and I have a question which I think should be straight forward, but I am having trouble figuring it out. I want to be able to access the return variable name for an instruction. For some instructions I can get this value through the "getName" method. For example, with the instruction: > %arg11 = bitcast i32* %arg1 to i8*, !dbg !42, !id !43 calling the "getName" method returns: > arg11 However, for the instruction: > %4 = load i32* %arg1, align 4, !dbg !57, !id !59 the "getName" method returns the empty string. How would I find the value "4" for this load instruction? Thanks in advance. - John
Hi John, Welcome, and this is a frequent newbie question: in LLVM names are more for human readability of the code, they are not really used as references internally. Instead engine operates instructions, which could be "used by" or "user of" other instructions (see use_iterator of Value.h). Also the Function class has an iterator of BasicBlocks, and each BasicBlock has an iterator of Instructions. For example, you can iterate through all instructions in the scope of your interest and find those, whose type is ReturnInst. Hope it helps, - Dima. 2012/7/5 John Backes <back0145 at umn.edu>:> Hello, > > I'm new to llvm development and I have a question which I think should > be straight forward, but I am having trouble figuring it out. I want to > be able to access the return variable name for an instruction. For some > instructions I can get this value through the "getName" method. For > example, with the instruction: > > > %arg11 = bitcast i32* %arg1 to i8*, !dbg !42, !id !43 > > calling the "getName" method returns: > > > arg11 > > However, for the instruction: > > > %4 = load i32* %arg1, align 4, !dbg !57, !id !59 > > the "getName" method returns the empty string. How would I find the > value "4" for this load instruction? Thanks in advance. > > - John > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Hi John, %4 is not returned, because it's a temp register value and not a named variable. But you can implement something like the code below to get this value. (Not the most elegant, but more or less working solution). static std::string getName(Instruction* i) { if(i->getOpcode() == Instruction::Store) { return getName((dyn_cast<StoreInst>(&*i))->getPointerOperand()); } else if(i->getOpcode() == Instruction::Ret) { return "Return"; } std::string name; raw_string_ostream s(name); s << *i; // string representation of an instruction size_t var_start = name.find('%'); // var starts with a % size_t var_end = name.find(" ", var_start+1); // till space size_t var_comma = name.find(",", var_start+1); // or comma return var_comma<var_end? name.substr(var_start,var_comma-var_start) : name.substr(var_start, var_end-var_start); } Have fun Igor On Thu, Jul 5, 2012 at 11:15 PM, John Backes <back0145 at umn.edu> wrote:> Hello, > > I'm new to llvm development and I have a question which I think should > be straight forward, but I am having trouble figuring it out. I want to > be able to access the return variable name for an instruction. For some > instructions I can get this value through the "getName" method. For > example, with the instruction: > > > %arg11 = bitcast i32* %arg1 to i8*, !dbg !42, !id !43 > > calling the "getName" method returns: > > > arg11 > > However, for the instruction: > > > %4 = load i32* %arg1, align 4, !dbg !57, !id !59 > > the "getName" method returns the empty string. How would I find the > value "4" for this load instruction? Thanks in advance. > > - John > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120705/4f7a145b/attachment.html>
> Welcome, and this is a frequent newbie question: [...]If not already there, could you find an appropriate place to put this in the documentation? --Sean Silva On Thu, Jul 5, 2012 at 2:29 PM, Dmitry N. Mikushin <maemarcus at gmail.com> wrote:> Hi John, > > Welcome, and this is a frequent newbie question: in LLVM names are > more for human readability of the code, they are not really used as > references internally. Instead engine operates instructions, which > could be "used by" or "user of" other instructions (see use_iterator > of Value.h). Also the Function class has an iterator of BasicBlocks, > and each BasicBlock has an iterator of Instructions. For example, you > can iterate through all instructions in the scope of your interest and > find those, whose type is ReturnInst. > > Hope it helps, > - Dima. > > 2012/7/5 John Backes <back0145 at umn.edu>: >> Hello, >> >> I'm new to llvm development and I have a question which I think should >> be straight forward, but I am having trouble figuring it out. I want to >> be able to access the return variable name for an instruction. For some >> instructions I can get this value through the "getName" method. For >> example, with the instruction: >> >> > %arg11 = bitcast i32* %arg1 to i8*, !dbg !42, !id !43 >> >> calling the "getName" method returns: >> >> > arg11 >> >> However, for the instruction: >> >> > %4 = load i32* %arg1, align 4, !dbg !57, !id !59 >> >> the "getName" method returns the empty string. How would I find the >> value "4" for this load instruction? Thanks in advance. >> >> - John >> >> >> _______________________________________________ >> LLVM Developers mailing list >> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu >> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
There is also the Instruction Namer pass that gives all anon registers names. On Jul 5, 2012 10:35 PM, "Igor Merkulow" <igor.merkulow at gmail.com> wrote:> Hi John, > > %4 is not returned, because it's a temp register value and not a named > variable. > But you can implement something like the code below to get this value. > (Not the most elegant, but more or less working solution). > > static std::string getName(Instruction* i) > { > if(i->getOpcode() == Instruction::Store) > { > return > getName((dyn_cast<StoreInst>(&*i))->getPointerOperand()); > } > else if(i->getOpcode() == Instruction::Ret) > { > return "Return"; > } > > std::string name; > raw_string_ostream s(name); > > s << *i; // string representation of an instruction > > size_t var_start = name.find('%'); // var starts with a % > size_t var_end = name.find(" ", var_start+1); // till space > size_t var_comma = name.find(",", var_start+1); // or comma > > return var_comma<var_end? > name.substr(var_start,var_comma-var_start) : > name.substr(var_start, var_end-var_start); > } > > Have fun > Igor > > On Thu, Jul 5, 2012 at 11:15 PM, John Backes <back0145 at umn.edu> wrote: > >> Hello, >> >> I'm new to llvm development and I have a question which I think should >> be straight forward, but I am having trouble figuring it out. I want to >> be able to access the return variable name for an instruction. For some >> instructions I can get this value through the "getName" method. For >> example, with the instruction: >> >> > %arg11 = bitcast i32* %arg1 to i8*, !dbg !42, !id !43 >> >> calling the "getName" method returns: >> >> > arg11 >> >> However, for the instruction: >> >> > %4 = load i32* %arg1, align 4, !dbg !57, !id !59 >> >> the "getName" method returns the empty string. How would I find the >> value "4" for this load instruction? Thanks in advance. >> >> - John >> >> >> _______________________________________________ >> LLVM Developers mailing list >> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu >> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >> > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120705/21d67c1e/attachment.html>