23791
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 8025505
|
|
27 |
* @summary Constant folding deficiency
|
|
28 |
* @library /tools/javac/lib
|
|
29 |
* @build ToolBox
|
|
30 |
* @run main ConstFoldTest
|
|
31 |
*/
|
|
32 |
|
|
33 |
import java.net.URL;
|
|
34 |
import java.util.List;
|
|
35 |
|
|
36 |
public class ConstFoldTest {
|
|
37 |
public static void main(String... args) throws Exception {
|
|
38 |
new ConstFoldTest().run();
|
|
39 |
}
|
|
40 |
|
|
41 |
// This is the test case. This class should end up
|
|
42 |
// as straight-line code with no conditionals
|
|
43 |
class CFTest {
|
|
44 |
void m() {
|
|
45 |
int x;
|
|
46 |
if (1 != 2) x=1; else x=0;
|
|
47 |
if (1 == 2) x=1; else x=0;
|
|
48 |
if ("" != null) x=1; else x=0;
|
|
49 |
if ("" == null) x=1; else x=0;
|
|
50 |
if (null == null) x=1; else x=0;
|
|
51 |
if (null != null) x=1; else x=0;
|
|
52 |
|
|
53 |
x = 1 != 2 ? 1 : 0;
|
|
54 |
x = 1 == 2 ? 1 : 0;
|
|
55 |
x = "" != null ? 1 : 0;
|
|
56 |
x = "" == null ? 1 : 0;
|
|
57 |
x = null == null ? 1 : 0;
|
|
58 |
x = null != null ? 1 : 0;
|
|
59 |
|
|
60 |
boolean b;
|
|
61 |
b = 1 != 2 && true;
|
|
62 |
b = 1 == 2 || true;
|
|
63 |
b = ("" != null) && true;
|
|
64 |
b = ("" == null) || true;
|
|
65 |
b = (null == null) && true;
|
|
66 |
b = (null != null) || true;
|
|
67 |
}
|
|
68 |
}
|
|
69 |
|
|
70 |
// All of the conditionals above should be eliminated.
|
|
71 |
// these if* bytecodes should not be seen
|
|
72 |
final String regex = "\\sif(?:null|nonnull|eq|ne){1}\\s";
|
|
73 |
|
|
74 |
void run() throws Exception {
|
|
75 |
URL url = ConstFoldTest.class.getResource("ConstFoldTest$CFTest.class");
|
|
76 |
String result = ToolBox.javap(new ToolBox.JavaToolArgs().setAllArgs("-c", url.getFile()));
|
|
77 |
System.out.println(result);
|
|
78 |
|
|
79 |
List<String> bad_codes = ToolBox.grep(regex, result, "\n");
|
|
80 |
if (!bad_codes.isEmpty()) {
|
|
81 |
for (String code : bad_codes)
|
|
82 |
System.out.println("Bad OpCode Found: " + code);
|
|
83 |
throw new Exception("constant folding failed");
|
|
84 |
}
|
|
85 |
}
|
|
86 |
}
|