JVM字节码中的"dup2_x2"指令有什么用?

What is the use of the "dup2_x2" instruction in JVM bytecode?

Java 有一个 dup2_x2 指令,根据 documentation,它具有以下行为:

Duplicate the top one or two values on the operand stack and insert the duplicated values, in the original order, into the operand stack.

javac是否使用此指令生成字节码?它的潜在用例是什么?

例如下面的代码

public static Long example1(Long[] array, int i, long l) {
    return array[i]=l;
}
public static long example2(long[] array, int i, long l) {
    return array[i]=l;
}

编译为

  public static java.lang.Long example1(java.lang.Long[], int, long);
    Code:
       0: aload_0
       1: iload_1
       2: lload_2
       3: invokestatic  #1        // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
       6: dup_x2
       7: aastore
       8: areturn

  public static long example2(long[], int, long);
    Code:
       0: aload_0
       1: iload_1
       2: lload_2
       3: dup2_x2
       4: lastore
       5: lreturn

看,demo on Ideone