title: Is there a Java bytecode optimizer that removes useless gotos?
body: <p>Problem: I have a method that compiles to over 8000 bytes of Java bytecode. HotSpot has a magic limit that makes the JIT not kick in for methods that exceed 8000 bytes. (Yes, it is reasonable to have a huge method. This is a tokenizer loop.) The method is in a library and I don't want to require users of the library to have to configure HotSpot to deactivate the magic limit.</p>
  
  <p>Observation: Decompiling the bytecode shows that Eclipse Java Compiler generates a lot of pointless gotos. (javac is even worse.) That is, there are gotos that are only reachable from jumps. Obviously, the jump that jumps to the goto should instead jump directly where the goto jumps and the goto should be eliminated.</p>
  
  <p>Question: Is there a bytecode optimizer for Java 5 class files that flattens pointless jump chains and then removes unnecessary gotos?</p>
  
  <p>Edit: I mean patterns like:</p>
  
  <pre><code>8698:   goto    8548
  8701:   goto    0
  </code></pre>
  
  <p>Obviously, the second goto can only be reached by a jump to 8701 which might as well be a direct jump to 0.</p>
  
  <p>On a second investigation, this questionable pattern is more common:</p>
  
  <pre><code>4257:   if_icmpne   4263
  4260:   goto    8704
  4263:   aload_0
  </code></pre>
  
  <p>Where obviously, one would like the compiler to reverse the "not equal" comparison to "equal" comparison, jump to 8704 and eliminate the goto.</p>