RFR: 8261137: Optimization of Box nodes in uncommon_trap

Eric Liu github.com+10482586+therealeliu at openjdk.java.net
Sun Feb 7 10:08:42 UTC 2021


On Fri, 5 Feb 2021 07:15:55 GMT, Wang Huang <whuang at openjdk.org> wrote:

> > I was wandering if we can remove the useless `if` as it's always true in this case. Do you know why this kind of `if` haven't been eliminated by GVN phase?

> It is a good idea . However, C2 might not optimize some `if` for many reasons:
> 
> * This situation can also be triggered in calling other method. For example :
> 
> ```
> public class MyBenchmark {
> 
>   static int[] data = new int[10000];
> 
>   static {
>       for(int i = 0; i < data.length; ++i) {
>           data[i] = 299;
>       }
>   }
> 
>   @Benchmark
>   public void testMethod(Blackhole bh) {
>     int sum = 0;
>     for (int i = 0; i < data.length; i++) {
>       Integer ii = Integer.valueOf(data[i]);
>       sum += abs(ii);
>     }
>     bh.consume(sum);
>   }
> 
>   public int abs(Integer ii) {
>     if (ii.intValue() > 0) {
>       return ii.intValue();
>     } else {
>       return 0 - ii.intValue();
>     }
>   }
> }
> ```
> 
> The method `abs` will be inlined when it is hot enough. However, this `If` can not be eliminated.
> 
> * The case in the JDK-8261137 is just like this case:
> 
> ```
> public class MyBenchmark {
> 
>     static int[] data = new int[10000];
> 
>     static {
>         for(int i = 0; i < data.length; ++i) {
>             data[i] = i * 1337 % 7331;
>         }
>     }
> 
>     @Benchmark
>     public void testMethod(Blackhole bh) {
>       int sum = 0;
>       for (int i = 0; i < data.length; i++) {
>           Integer ii = Integer.valueOf(data[i]);
>           if (i < 100000) {
>               sum += ii.intValue();
>           }
>       }
>       bh.consume(sum);
>     }
> }
> ```

Thanks for your explanation. This makes more sense to me now.

-------------

PR: https://git.openjdk.java.net/jdk/pull/2401


More information about the hotspot-compiler-dev mailing list