title: Mistakes in llvm instruction::getOpcode() and getOpcodeName
body: <p>I find there may be some errors in my llvm pass project. I wrote a little program to try to print out all the instructions and their corresponding opcode. I use Instruction::getOpcode() and Instruction::getOpcodeName().</p>
  
  <h1>But the result looks a little bit odd. Following is my result:</h1>
  
  <pre><code>  %1 = alloca i32, align 4
  load
    %a = alloca i32, align 4
  load
    %b = alloca i32, align 4
  load
    store i32 0, i32* %1
  getelementptr
    %2 = call i64 @time(i64* null) #3
  select
    %3 = trunc i64 %2 to i32
  zext
    call void @srand(i32 %3) #3
  select
    %4 = call i32 @rand() #3
  select
    %5 = srem i32 %4, 10
  frem
    store i32 %5, i32* %a, align 4
  getelementptr
    %6 = call i32 @rand() #3
  select
    %7 = srem i32 %6, 10
  frem
    store i32 %7, i32* %b, align 4
  getelementptr
    %8 = load i32* %a, align 4
  store
    %9 = load i32* %b, align 4
  store
    %10 = icmp sgt i32 %8, %9
  fcmp
    br i1 %10, label %11, label %14
  switch
    %12 = load i32* %a, align 4
  store
    %13 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([4 x i8]* @.str, i32 0, i32 0), i32 %12)
  select
    br label %17
  switch
    %15 = load i32* %b, align 4
  store
    %16 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([4 x i8]* @.str, i32 0, i32 0), i32 %15)
  select
    br label %17
  switch
    %18 = load i32* %1
  store
    ret i32 %18
  br
  </code></pre>
  
  <p>I try to print out every instruction opcodeName after instruction, but every instruction get a wrong opcodeName. I have also checked the opcode for each opcodeName, and they are also wrong. I do not know exactly where to find the enum of instruction list, but I think I have ever found it before, and wrong opcodeName is correlating to opcode. Looks like they both shift from right case by 2 unit.</p>
  
  <p>Have you ever faced the problem like this?</p>
  
  <p>I also attach the code of my llvm pass as follows:</p>
  
  <pre><code>#include "llvm/ADT/Statistic.h"
  #include "llvm/IR/Function.h"
  #include "llvm/Pass.h"
  #include "llvm/Support/raw_ostream.h"
  #include "llvmFaultInjector.h"
  #include "llvm/IR/Instructions.h"
  #include "llvm/IR/Module.h"
  #include "llvm/IR/BasicBlock.h"
  #include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  
  using namespace llvm;
  
  namespace {
    // Hello - The first implementation, without getAnalysisUsage.
    struct Hello : public ModulePass {
      static char ID; // Pass identification, replacement for typeid
      Hello() : ModulePass(ID) {}
  
      bool runOnModule(Module &amp;M) override {
          Module::iterator fi, fie;
          Function::iterator bi, bie;
          BasicBlock::iterator ii, iie;
  
          /* test a example : delete first instruction of test file */
          Instruction * inst;
  
          for(fi = M.begin(), fie = M.end(); fi != fie; fi++)
          {
              for(bi = fi-&gt;begin(), bie = fi-&gt;end(); bi != bie; bi++)
              {
                  for(ii = bi-&gt;begin(), iie = bi-&gt;end(); ii != iie; ii++)
                  {
                      inst = (Instruction *)(ii);
                      errs() &lt;&lt; *inst &lt;&lt; '\n';
                      errs() &lt;&lt; inst-&gt;getOpcodeName() &lt;&lt; '\n';
                  }
              }
          }
  
          /* delete instruction*/
          /*llvmFaultInjector::tranBranchDelete(inst);
          */      
          return true;
      }
    };
  }
  
  char Hello::ID = 0;
  static RegisterPass&lt;Hello&gt; X("CFCFaultInject", "llvm CFCFaultInjector");
  </code></pre>