C2 compiler could be better ...

Ulf Zibis Ulf.Zibis at gmx.de
Tue Mar 2 16:46:36 PST 2010


Having following code:

     public String(int[] codePoints, int offset, int count) {
         ...
         int n = 0;
         for (int i = offset; i < offset + count; i++) {
             int c = codePoints[i];
             if (c >> 16 == 0)
                 n += 1;
             else if ((c >> 16) < (Character.MAX_CODE_POINT + 1 >> 16))
                 n += 2;
             else
                 throw new IllegalArgumentException(Integer.toString(c));
         }
         ...
     }

It compiles to:

->          int c = codePoints[i];
   0x00b87aa4: mov    0x1c(%esp),%ebp
   0x00b87aa8: mov    0xc(%ebp,%edi,4),%ebx  ;*iaload
                                         ; - 
java.lang.String::&lt;init&gt;@72 (line 284)

->          if (c >> 16 == 0)
   0x00b87aac: mov    %ebx,%ecx
   0x00b87aae: sar    $0x10,%ecx         ;*ishr
                                         ; - 
java.lang.String::&lt;init&gt;@79 (line 290)
   0x00b87ab1: test   %ecx,%ecx
   0x00b87ab3: je     0x00b87acb         ;*ifne
                                         ; - 
java.lang.String::&lt;init&gt;@80 (line 290)

->          else if ((c >> 16) < (Character.MAX_CODE_POINT + 1 >> 16))
   0x00b87ab5: cmp    $0x11,%ecx

->          else
   0x00b87ab8: jge    0x00b87ce6         ;*if_icmpge
                                         ; - 
java.lang.String::&lt;init&gt;@96 (line 293)

->              n += 2;
   0x00b87abe: mov    0x8(%esp),%ebx
   0x00b87ac2: add    $0x2,%ebx          ;*iinc
                                         ; - 
java.lang.String::&lt;init&gt;@99 (line 295)
   0x00b87ac5: mov    %ebx,0x8(%esp)
   0x00b87ac9: jmp    0x00b87ad4

->              n += 1;
   0x00b87acb: mov    0x8(%esp),%ecx
   0x00b87acf: inc    %ecx               ;*iinc
                                         ; - 
java.lang.String::&lt;init&gt;@83 (line 292)
   0x00b87ad0: mov    %ecx,0x8(%esp)     ;*goto
                                         ; - 
java.lang.String::&lt;init&gt;@102 (line 295)

->          i++
   0x00b87ad4: inc    %edi               ;*iinc
                                         ; - 
java.lang.String::&lt;init&gt;@118 (line 283)

->          i < offset + count;
   0x00b87ad5: cmp    0x18(%esp),%edi
   0x00b87ad9: jl     0x00b87aa4         ;*if_icmpge
                                         ; - 
java.lang.String::&lt;init&gt;@66 (line 283)
   0x00b87adb:


My questions/suggestions:

1.) Why is variable n first loaded to register %ebx / %ecx and than 
copied back?
     Wouldn't it be better to have:
   0x00b87ac2: add    $0x2,0x8(%esp)
     and:
   0x00b87acf: inc 0x8(%esp)

2.) Or better, why is,'t variable n constantly held in register e.g. %eax ?

3.) Why is oop of int[] codePoints always newly loaded in each loop?
     I think it could be held in register %ebp ?


-Ulf






More information about the hotspot-compiler-dev mailing list