huangcm
2025-07-01 676035278781360996553c427a12bf358249ebf7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
public class Main  {
  public static void main(String[] args) throws Exception {
    if (testOddLow1(5L)) {
      throw new Error();
    }
 
    if (testNonFollowingHigh(5)) {
      throw new Error();
    }
 
    if (testOddLow2()) {
      throw new Error();
    }
  }
 
  public static boolean testOddLow1(long a /* ECX-EDX */) {
    // class instance is in EBP
    long b = myLongField1; // ESI-EDI
    int f = myField1; // EBX
    int e = myField2; // EAX
    int g = myField3; // ESI (by spilling ESI-EDI, see below)
    int h = myField4; // EDI
    myLongField2 = a; // Make sure ESI-EDI gets spilled and not ECX-EDX
    myField2 = f; // use of EBX
    myField1 = e; // use of EAX
    myField3 = h; // use of ESI
    myField4 = g; // use if EDI
 
    // At this point `b` has been spilled and needs to have a pair. The ordering
    // in the register allocator triggers the allocation of `res` before `b`.
    // `res` being used after the `doCall`, we want a callee saved register.
    //
    // EBP is taken by the class instance and EDI is taken by `g` (both used in the `myField4`
    // assignment below). So we end up allocating ESI for `res`.
    //
    // When we try to allocate a pair for `b` we're in the following situation:
    // EAX is free
    // ECX is taken
    // EDX is taken
    // EBX is free
    // ESP is blocked
    // EBP could be spilled
    // ESI is taken
    // EDI could be spilled
    //
    // So there is no consecutive registers available to please the register allocator.
    // The compiler used to trip then because of a bogus implementation of trying to split
    // an unaligned register pair (here ECX and EDX). The implementation would not find
    // a register and the register allocator would then complain about not having
    // enough registers for the operation.
    boolean res = a == b;
    $noinline$doCall();
    myField4 = g;
    return res;
  }
 
  public static boolean testNonFollowingHigh(int i) {
    // class instance is in EBP
    long b = myLongField1; // ESI-EDI
    long a = (long)i; // EAX-EDX
    int f = myField1; // EBX
    int e = myField2; // ECX
    int g = myField3; // ESI (by spilling ESI-EDI, see below)
    int h = myField4; // EDI
    myLongField2 = a; // Make sure ESI-EDI gets spilled and not ECX-EDX
    myField2 = f; // use of EBX
    myField1 = e; // use of ECX
    myField3 = h; // use of EDI
    myField4 = g; // use of ESI
 
    // At this point `b` has been spilled and needs to have a pair. The ordering
    // in the register allocator triggers the allocation of `res` before `b`.
    // `res` being used after the `doCall`, we want a callee saved register.
    //
    // EBP is taken by the class instance and ESI is taken by `g` (both used in the `myField4`
    // assignment below). So we end up allocating EDI for `res`.
    //
    // When we try to allocate a pair for `b` we're in the following situation:
    // EAX is taken
    // ECX is free
    // EDX is taken
    // EBX is free
    // ESP is blocked
    // EBP could be spilled
    // ESI is taken
    // EDI could be spilled
    //
    // So there is no consecutive registers available to please the register allocator.
    // The compiler used to be in a bad state because of a bogus implementation of trying
    // to split an unaligned register pair (here EAX and EDX).
    boolean res = a == b;
    $noinline$doCall();
    myField4 = g;
    return res;
  }
 
  public static boolean testOddLow2() {
    // class instance is in EBP
    long b = myLongField1; // ECX-EDX (hint due to call below).
    long a = myLongField2; // ESI-EDI
    int f = myField1; // EBX
    int e = myField2; // EAX
    int g = myField3; // ECX
    int h = myField4; // EDX
    int i = myField5; // ESI - callee saved due to assignment after call to $noinline$doCall.
    myField2 = f; // use of EBX
    myField1 = e; // use of EAX
    myField3 = h; // use of EDX
    myField4 = i; // use of ESI
    myField5 = g; // use of ECX
 
    // At this point `a` and `b` have been spilled and need to have a pairs. The ordering
    // in the register allocator triggers the allocation of `res` before `a` and `b`.
    // `res` being used after the `doCall`, we want a callee saved register.
    //
    // EBP is taken by the class instance and ESI is taken by `i` (both used in the `myField4`
    // assignment below). So we end up allocating EDI for `res`.
    //
    // We first try to allocator a pair for `b`. We're in the following situation:
    // EAX is free
    // ECX is free
    // EDX is free
    // EBX is free
    // ESP is blocked
    // EBP could be spilled
    // ESI could be spilled
    // EDI is taken
    //
    // Because `b` is used as a first argument to a call, we take its hint and allocate
    // ECX-EDX to it.
    //
    // We then try to allocate a pair for `a`. We're in the following situation:
    // EAX is free
    // ECX could be spilled
    // EDX could be spilled
    // EBX is free
    // ESP is blocked
    // EBP could be spilled
    // ESI could be spilled
    // EDI is taken
    //
    // So no consecutive two free registers are available. When trying to find a slot, we pick
    // the first unaligned or non-pair interval. In this case, this is the unaligned ECX-EDX.
    // The compiler used to then trip because it forgot to remove the high interval containing
    // the pair from the active list.
 
    boolean res = a == b;
    $noinline$doCall(b);
    myField4 = i; // use of ESI
    return res;
  }
 
  public static void $noinline$doCall() {
    if (doThrow) throw new Error();
  }
 
  public static void $noinline$doCall(long e) {
    if (doThrow) throw new Error();
  }
 
  public static boolean doThrow;
  public static int myField1;
  public static int myField2;
  public static int myField3;
  public static int myField4;
  public static int myField5;
  public static long myLongField1;
  public static long myLongField2;
}