# 为什么要指令重排序？

## 分析

`int a = 1;`
`int b = 1;`
`a = a + 1;`
`b = b +1 ;`

`int a = 1;`
`a = a + 1;`
`int b = 1;`
`b = b +1 ;`

`package *****;`
`/**`
``` * reorder
* @author Mageek Chiu
* @date 2018/5/25 0025:12:49
*/
public class ReOrder {```
`    public int value ;`
`    private ReOrder(int value) {`
`        this.value = value;`
`    }`
`    public static void main(String... args){`
`        ReOrder reOrder = new ReOrder(111);`
`        ReOrder reOrder1 = new ReOrder(222);`
`        ReOrder reOrder2 = new ReOrder(333);`
`        System.out.println(add1(reOrder,reOrder1,reOrder2));`
`    }`
`    static int add1(ReOrder reOrder,ReOrder reOrder1,ReOrder reOrder2){`
`        int result = 0;`
`        result += reOrder.value;`
`        result += reOrder1.value;`
`        result += reOrder2.value;//***`
`        result += reOrder.value;`
`        result += reOrder1.value;`
`        result += reOrder2.value;`
`        result += reOrder.value;`
`        result += reOrder1.value;`
`        result += reOrder2.value;`
`        return result;`
`    }`
`}`

` # {method} {0x000000001c402c80} 'add1' '(*****/ReOrder;*****/ReOrder;*****/ReOrder;)I' in '*****/ReOrder'`
`  # parm0:    rdx:rdx   = '*****/ReOrder'`
`  # parm1:    r8:r8     = '*****/ReOrder'`
`  # parm2:    r9:r9     = '*****/ReOrder'`
`  #           [sp+0x20]  (sp of caller)`
`  0x00000000032a86c0: mov     dword ptr [rsp+0ffffffffffffa000h],eax`
`  0x00000000032a86c7: push    rbp`
`  0x00000000032a86c8: sub     rsp,10h           ;*synchronization entry`
`                                                ; - *****.ReOrder::add1@-1 (line 24)`
`  0x00000000032a86cc: mov     r11d,dword ptr [rdx+0ch]`
`                                                ;*getfield value`
`                                                ; - *****.ReOrder::add1@4 (line 26)`
`                                                ; implicit exception: dispatches to 0x00000000032a86ff`
`  0x00000000032a86d0: mov     r10d,dword ptr [r8+0ch]  ;*getfield value`
`                                                ; - *****.ReOrder::add1@11 (line 27)`
`                                                ; implicit exception: dispatches to 0x00000000032a870d`
`  0x00000000032a86d4: mov     r9d,dword ptr [r9+0ch]  ;*getfield value`
`                                                ; - *****.ReOrder::add1@18 (line 28)`
`                                                ; implicit exception: dispatches to 0x00000000032a8719`
`  0x00000000032a86d8: mov     eax,r11d`
`  0x00000000032a86db: add     eax,r10d`
`  0x00000000032a86de: add     eax,r9d`
`  0x00000000032a86e1: add     eax,r11d`
`  0x00000000032a86e4: add     eax,r10d`
`  0x00000000032a86e7: add     eax,r9d`
`  0x00000000032a86ea: add     eax,r11d`
`  0x00000000032a86ed: add     eax,r10d`
`  0x00000000032a86f0: add     eax,r9d           ;*iadd`

`[Constants]`
`  # {method} {0x000000001c052c78} 'add1' '(*****/ReOrder;*****/ReOrder;*****/ReOrder;)I' in '*****/ReOrder'`
`  # parm0:    rdx:rdx   = '*****/ReOrder'`
`  # parm1:    r8:r8     = '*****/ReOrder'`
`  # parm2:    r9:r9     = '*****/ReOrder'`
`  #           [sp+0x20]  (sp of caller)`
`  0x0000000002f47d40: mov     dword ptr [rsp+0ffffffffffffa000h],eax`
`  0x0000000002f47d47: push    rbp`
`  0x0000000002f47d48: sub     rsp,10h           ;*synchronization entry`
`                                                ; - *****.ReOrder::add1@-1 (line 24)`
`  0x0000000002f47d4c: mov     r11d,dword ptr [rdx+0ch]`
`                                                ;*getfield value`
`                                                ; - *****r.ReOrder::add1@4 (line 26)`
`                                                ; implicit exception: dispatches to 0x0000000002f47d7c`
`  0x0000000002f47d50: mov     r10d,dword ptr [r8+0ch]  ;*getfield value`
`                                                ; - *****.ReOrder::add1@11 (line 27)`
`                                                ; implicit exception: dispatches to 0x0000000002f47d89`
`  0x0000000002f47d54: mov     r9d,dword ptr [r9+0ch]  ;*getfield value`
`                                                ; - *****::add1@32 (line 32)`
`                                                ; implicit exception: dispatches to 0x0000000002f47d95`
`  0x0000000002f47d58: mov     eax,r11d`
`  0x0000000002f47d5b: add     eax,r10d`
`  0x0000000002f47d5e: add     eax,r11d`
`  0x0000000002f47d61: add     eax,r10d`
`  0x0000000002f47d64: add     eax,r9d`
`  0x0000000002f47d67: add     eax,r11d`
`  0x0000000002f47d6a: add     eax,r10d`
`  0x0000000002f47d6d: add     eax,r9d           ;*iadd`

`Xcomp` 含义是使用编译模式而不是解释模式， `-XX:CompileCommand=print,*ReOrder.add1`表示只打印这个方法，`-XX:+PrintCompilation`表示打印方法名称。

undefined

1 篇文章1 人订阅

0 条评论

## 相关文章

35110

54080

25460

45770

1.5K30

### Upspin 中的错误处理

Upspin 项目使用自定义的包 —— upspin.io/errors —— 来表示系统内部出现的错误条件。这些错误满足标准的 Go error 接口，但是使...

255100

38310

### PhalGo-Echo路由

PhalGo-Echo路由 ? Echo官网地址:https://labstack.com/echo Echo是PhalGo最核心的组件,负责了整体的请求路由返...

37180

### JavaScript的介绍

javascript是什么? 　　　　javascript 是因特网上最流行的脚本语言，它存在于全世界所有 Web 浏览器中，能够增强用户与 Web 站...

10220

38650