1/* 2 * Copyright (c) 2014, 2016, 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/lib 29 * @modules jdk.compiler/com.sun.tools.javac.api 30 * jdk.compiler/com.sun.tools.javac.main 31 * jdk.jdeps/com.sun.tools.javap 32 * @build toolbox.ToolBox toolbox.JavapTask 33 * @run main ConstFoldTest 34 */ 35 36import java.net.URL; 37import java.nio.file.Path; 38import java.nio.file.Paths; 39import java.util.List; 40 41import toolbox.JavapTask; 42import toolbox.Task; 43import toolbox.ToolBox; 44 45public class ConstFoldTest { 46 public static void main(String... args) throws Exception { 47 new ConstFoldTest().run(); 48 } 49 50 // This is the test case. This class should end up 51 // as straight-line code with no conditionals 52 class CFTest { 53 void m() { 54 int x; 55 if (1 != 2) x=1; else x=0; 56 if (1 == 2) x=1; else x=0; 57 if ("" != null) x=1; else x=0; 58 if ("" == null) x=1; else x=0; 59 if (null == null) x=1; else x=0; 60 if (null != null) x=1; else x=0; 61 62 x = 1 != 2 ? 1 : 0; 63 x = 1 == 2 ? 1 : 0; 64 x = "" != null ? 1 : 0; 65 x = "" == null ? 1 : 0; 66 x = null == null ? 1 : 0; 67 x = null != null ? 1 : 0; 68 69 boolean b; 70 b = 1 != 2 && true; 71 b = 1 == 2 || true; 72 b = ("" != null) && true; 73 b = ("" == null) || true; 74 b = (null == null) && true; 75 b = (null != null) || true; 76 } 77 } 78 79 // All of the conditionals above should be eliminated. 80 // these if* bytecodes should not be seen 81 final String regex = "\\sif(?:null|nonnull|eq|ne){1}\\s"; 82 83 void run() throws Exception { 84 ToolBox tb = new ToolBox(); 85 86 URL url = ConstFoldTest.class.getResource("ConstFoldTest$CFTest.class"); 87 Path file = Paths.get(url.toURI()); 88 List<String> result = new JavapTask(tb) 89 .options("-c") 90 .classes(file.toString()) 91 .run() 92 .write(Task.OutputKind.DIRECT) 93 .getOutputLines(Task.OutputKind.DIRECT); 94 95 List<String> bad_codes = tb.grep(regex, result); 96 if (!bad_codes.isEmpty()) { 97 for (String code : bad_codes) 98 System.out.println("Bad OpCode Found: " + code); 99 throw new Exception("constant folding failed"); 100 } 101 } 102} 103