annotate test/compiler/loopopts/TestLogSum.java @ 17985:4077c61b03a0

8046516: Segmentation fault in JVM (easily reproducible) Summary: Place new nodes on the previous loop exit in reorg_offsets(). Reviewed-by: roland
author kvn
date Thu, 12 Jun 2014 10:15:43 -0700
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17985
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
1 /*
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
4 *
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
7 * published by the Free Software Foundation.
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
8 *
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
13 * accompanied this code).
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
14 *
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
18 *
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
21 * questions.
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
22 */
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
23
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
24 /*
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
25 * @test
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
26 * @bug 8046516
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
27 * @summary Segmentation fault in JVM (easily reproducible)
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
28 * @run main/othervm -XX:-TieredCompilation -Xbatch TestLogSum
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
29 * @author jackkamm@gmail.com
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
30 */
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
31
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
32 import java.util.Arrays;
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
33 import java.util.HashMap;
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
34 import java.util.List;
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
35 import java.util.Map;
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
36 public class TestLogSum {
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
37 public static void main(String[] args) {
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
38 double sum;
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
39
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
40 for (int i = 0; i < 6; i++) {
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
41 for (int n = 2; n < 30; n++) {
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
42 for (int j = 1; j <= n; j++) {
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
43 for (int k = 1; k <= j; k++) {
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
44 // System.out.println(computeSum(k, j));
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
45 sum = computeSum(k, j);
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
46 }
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
47 }
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
48 }
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
49 }
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
50 }
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
51
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
52 private static Map<List<Integer>, Double> cache = new HashMap<List<Integer>, Double>();
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
53 public static double computeSum(int x, int y) {
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
54 List<Integer> key = Arrays.asList(new Integer[] {x, y});
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
55
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
56 if (!cache.containsKey(key)) {
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
57
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
58 // explicitly creating/updating a double[] array, instead of using the LogSumArray wrapper object, will prevent the error
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
59 LogSumArray toReturn = new LogSumArray(x);
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
60
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
61 // changing loop indices will prevent the error
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
62 // in particular, for(z=0; z<x-1; z++), and then using z+1 in place of z, will not produce error
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
63 for (int z = 1; z < x+1; z++) {
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
64 double logSummand = Math.log(z + x + y);
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
65 toReturn.addLogSummand(logSummand);
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
66 }
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
67
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
68 // returning the value here without cacheing it will prevent the segfault
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
69 cache.put(key, toReturn.retrieveLogSum());
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
70 }
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
71 return cache.get(key);
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
72 }
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
73
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
74 /*
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
75 * Given a bunch of logarithms log(X),log(Y),log(Z),...
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
76 * This class is used to compute the log of the sum, log(X+Y+Z+...)
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
77 */
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
78 private static class LogSumArray {
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
79 private double[] logSummandArray;
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
80 private int currSize;
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
81
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
82 private double maxLogSummand;
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
83
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
84 public LogSumArray(int maxEntries) {
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
85 this.logSummandArray = new double[maxEntries];
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
86
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
87 this.currSize = 0;
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
88 this.maxLogSummand = Double.NEGATIVE_INFINITY;
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
89 }
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
90
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
91 public void addLogSummand(double logSummand) {
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
92 logSummandArray[currSize] = logSummand;
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
93 currSize++;
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
94 // removing this line will prevent the error
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
95 maxLogSummand = Math.max(maxLogSummand, logSummand);
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
96 }
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
97
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
98 public double retrieveLogSum() {
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
99 if (maxLogSummand == Double.NEGATIVE_INFINITY) return Double.NEGATIVE_INFINITY;
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
100
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
101 assert currSize <= logSummandArray.length;
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
102
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
103 double factorSum = 0;
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
104 for (int i = 0; i < currSize; i++) {
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
105 factorSum += Math.exp(logSummandArray[i] - maxLogSummand);
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
106 }
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
107
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
108 return Math.log(factorSum) + maxLogSummand;
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
109 }
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
110 }
4077c61b03a0 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
111 }