0

How do I get the whole name of an operator in LLVM?

I'm iterating through blocks, and then, in each of their instructions, I try to get the operator name, but I do only get a part of it. Running the following code:

virtual bool runOnBasicBlock(BasicBlock &bb) {
    for (auto it(bb.begin()); it != bb.end(); ++it) {
        errs() << it->getName() << '\t' << *it << '\n';
    }
}

I get output lines like:

icmp        %cmp = icmp slt i32 %i.0, %argc
icmp        %cmp1 = icmp sgt i32 %call, %max.0
add       %inc = add nsw i32 %i.0, 1

I'd like to get icmp slt, icmp sgt, and add nsw, instead of icmp and add.

Rubens
  • 14,478
  • 11
  • 63
  • 92

1 Answers1

3

Well, slt, sgt and others for icmp are just arguments. You can access them with getPredicate (a method of CmpInst). Also see the useful function getPredicateText in lib/IR/AsmWriter.cpp.

For stuff like nsw, check out the method hasNoSignedWrap and similar methods.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • But then, how do I access these arguments? From the instruction, I mean. I've been trying `it->getOperand(i)->getName()`, but I only get the operands -- and I didn't find any `getArgument(i)`. – Rubens Apr 20 '13 at 13:16
  • +1 Thanks for the answer. Isn't there anything a bit more generic, that would allow me to iterate over the instruction, printing the information I get when I print the instruction itself? Or do I have to treat each case -- comparisons, sums, branches, and others? – Rubens Apr 20 '13 at 13:36
  • 1
    @Rubens: you can `dump` the instruction, but if you want to programmatically decompose it to parts you have to accept that instructions are different, and each has a distinct set of attributes, flags, arguments and capabilities. So there's no "generic solve all" approach – Eli Bendersky Apr 20 '13 at 13:46
  • Is there a way to directly access the definition of `getPredicateText`, from *AsmWriter.cpp*? My will is to "rewrite" the `printInstruction` function from *AsmWriter.cpp*, but I need access to most of the (static) functions there defined. – Rubens Apr 21 '13 at 14:27
  • @Rubens: I don't know if that's possible – Eli Bendersky Apr 22 '13 at 12:55