Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhanced Switch] Bug 575652 - [17][codegen][switch pattern] Unnecessary casts in switch #3465

Open
srikanth-sankaran opened this issue Dec 17, 2024 · 0 comments · May be fixed by #3471
Open
Assignees

Comments

@srikanth-sankaran
Copy link
Contributor

srikanth-sankaran commented Dec 17, 2024

Carried over from bugzilla.

For this code:

import static java.util.stream.IntStream.rangeClosed;

public interface X {
  static String fizzbuzz(int i) {
    return switch((Integer) i) {
      case Integer v when v % 15 == 0 -> "FizzBuzz";
      case Integer v when v % 5 == 0 -> "Buzz";
      case Integer v when v % 3 == 0 -> "Fizz";
      case Integer v -> "" + v;
    };
  }

  static void main(String[] args) {
    rangeClosed(1, 100).mapToObj(i -> fizzbuzz(i)).forEach(System.out::println);
  }
} 

We generate the following bytecode:

  public static java.lang.String fizzbuzz(int);
    descriptor: (I)Ljava/lang/String;
    flags: (0x0009) ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=6, args_size=1
         0: iload_0
         1: invokestatic  #8                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
         4: dup
         5: invokestatic  #14                 // Method java/util/Objects.requireNonNull:(Ljava/lang/Object;)Ljava/lang/Object;
         8: pop
         9: astore_1
        10: aload_1
        11: iconst_0
        12: invokedynamic #20,  0             // InvokeDynamic #0:typeSwitch:(Ljava/lang/Object;I)I
        17: tableswitch   { // 0 to 3

                       0: 48

                       1: 76

                       2: 103

                       3: 132
                 default: 132
            }
        48: aload_1
        49: checkcast     #9                  // class java/lang/Integer
        52: astore_2
        53: aload_2
        54: invokevirtual #24                 // Method java/lang/Integer.intValue:()I
        57: bipush        15
        59: irem
        60: ifne          66
        63: goto          71
        66: aload_1
        67: iconst_1
        68: goto          12
        71: ldc           #28                 // String FizzBuzz
        73: goto          145
        76: aload_1
        77: checkcast     #9                  // class java/lang/Integer
        80: astore_3
        81: aload_3
        82: invokevirtual #24                 // Method java/lang/Integer.intValue:()I
        85: iconst_5
        86: irem
        87: ifne          93
        90: goto          98
        93: aload_1
        94: iconst_2
        95: goto          12
        98: ldc           #30                 // String Buzz
       100: goto          145
       103: aload_1
       104: checkcast     #9                  // class java/lang/Integer
       107: astore        4
       109: aload         4
       111: invokevirtual #24                 // Method java/lang/Integer.intValue:()I
       114: iconst_3
       115: irem
       116: ifne          122
       119: goto          127
       122: aload_1
       123: iconst_3
       124: goto          12
       127: ldc           #32                 // String Fizz
       129: goto          145
       132: aload_1
       133: astore        5
       135: aload         5
       137: invokestatic  #34                 // Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
       140: invokedynamic #39,  0             // InvokeDynamic #1:makeConcatWithConstants:(Ljava/lang/String;)Ljava/lang/String;
       145: areturn

The three instances of checkcast #9 are redundant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant