lin
2025-07-30 fcd736bf35fd93b563e9bbf594f2aa7b62028cc9
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
/*
 * 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) {
        new Main().run();
        testPreserveFloat();
        testPreserveDouble();
        System.out.println("finish");
    }
 
    public void run() {
        double a[][] = new double[200][201];
        double b[] = new double[200];
        int n = 100;
 
        foo1(a, n, b);
    }
 
    void foo1(double a[][], int n, double b[]) {
        double t;
        int i,k;
 
        for (i = 0; i < n; i++) {
            k = n - (i + 1);
            b[k] /= a[k][k];
            t = -b[k];
            foo2(k + 1000, t, b);
        }
    }
 
    void foo2(int n, double c, double b[]) {
        try {
            foo3(n, c, b);
        } catch (Exception e) {
        }
    }
 
    void foo3(int n, double c, double b[]) {
        int i = 0;
        for (i = 0; i < n; i++) {
            b[i + 1] += c * b[i + 1];
        }
    }
 
    /*
     * Test that we correctly preserve floating point registers when we deoptimize.
     *
     * Note: These tests rely on the deoptimization happening before the loop,
     * so that the loop is interpreted and fills the provided arrays. However,
     * the BCE transformation can be modified to execute the loop as many times
     * as the compiler can guarantee no AIOOBE and only deoptimize thereafter,
     * just before the throwing iteration. Then the floating point registers
     * would no longer be used after the deoptimization and another approach
     * would be needed to test this.
     */
 
    static public void testPreserveFloat() {
        float[] array = new float[2];
        try {
            $noinline$FloatFill(1.125f, 2.5f, array, 3);
            throw new Error();
        } catch (ArrayIndexOutOfBoundsException expected) {
            System.out.println("array[0]=" + array[0] + "f");
            System.out.println("array[1]=" + array[1] + "f");
        }
    }
 
    /// CHECK-START: void Main.$noinline$FloatFill(float, float, float[], int) BCE (after)
    /// CHECK-DAG:          Deoptimize
    /// CHECK-DAG:          Deoptimize
    /// CHECK-DAG:          Deoptimize
    /// CHECK-NOT:          Deoptimize
 
    /// CHECK-START: void Main.$noinline$FloatFill(float, float, float[], int) BCE (after)
    /// CHECK-NOT:          BoundsCheck
 
    public static void $noinline$FloatFill(float f1, float f2, float[] array, int n) {
        if (doThrow) { throw new Error(); }
        for (int i = 0; i < n; ++i) {
            array[i] = ((i & 1) == 1) ? f1 : f2;
            f1 += 1.5f;
            f2 += 2.25f;
        }
    }
 
    static public void testPreserveDouble() {
        double[] array = new double[2];
        try {
            $noinline$DoubleFill(2.125, 3.5, array, 3);
            throw new Error();
        } catch (ArrayIndexOutOfBoundsException expected) {
            System.out.println("array[0]=" + array[0]);
            System.out.println("array[1]=" + array[1]);
        }
    }
 
    /// CHECK-START: void Main.$noinline$DoubleFill(double, double, double[], int) BCE (after)
    /// CHECK-DAG:          Deoptimize
    /// CHECK-DAG:          Deoptimize
    /// CHECK-DAG:          Deoptimize
    /// CHECK-NOT:          Deoptimize
 
    /// CHECK-START: void Main.$noinline$DoubleFill(double, double, double[], int) BCE (after)
    /// CHECK-NOT:          BoundsCheck
 
    public static void $noinline$DoubleFill(double d1, double d2, double[] array, int n) {
        if (doThrow) { throw new Error(); }
        for (int i = 0; i < n; ++i) {
            array[i] = ((i & 1) == 1) ? d1 : d2;
            d1 += 1.5;
            d2 += 2.25;
        }
    }
 
    public static boolean doThrow = false;
}