1 /*
 2  * Copyright (c) 2024, 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 import java.lang.classfile.Attributes;
25 import java.lang.classfile.ClassFile;
26 import java.lang.classfile.Instruction;
27 import jdk.internal.classfile.components.ClassPrinter;
28 import static java.lang.classfile.Opcode.*;
29 import java.lang.constant.ClassDesc;
30 import java.lang.constant.ConstantDescs;
31 import jdk.incubator.code.bytecode.BranchCompactor;
32 import java.util.List;
33 import org.testng.Assert;
34 import org.testng.annotations.Test;
35 
36 /*
37  * @test
38  * @modules jdk.incubator.code
39  * @modules java.base/jdk.internal.classfile.components
40  * @enablePreview
41  * @run testng TestBranchCompactor
42  */
43 public class TestBranchCompactor {
44 
45     @Test
46     public void testBranchCompactor() {
47         var cc = ClassFile.of(ClassFile.StackMapsOption.DROP_STACK_MAPS);
48         var clm = cc.parse(cc.build(ClassDesc.of("c"), clb -> clb.withMethodBody("m", ConstantDescs.MTD_void, 0,
49                 cb -> cb.transforming(new BranchCompactor(), cob -> {
50                     var l = cob.newLabel();
51                     cob.goto_(l) //compact
52                        .lineNumber(1)
53                        .labelBinding(l)
54                        .nop();
55 
56                     l = cob.newLabel();
57                     cob.goto_w(l) //compact
58                        .lineNumber(2)
59                        .labelBinding(l);
60 
61                     l = cob.newLabel();
62                     cob.goto_(l) //compact
63                        .labelBinding(l);
64 
65                     cob.iconst_0();
66                     l = cob.newLabel();
67                     cob.ifeq(l) //do not compact
68                        .labelBinding(l);
69 
70                     l = cob.newLabel();
71                     cob.goto_(l) //do not compact
72                        .nop()
73                        .labelBinding(l)
74                        .return_();
75                 }))));
76         var code = clm.methods().get(0).code().get();
77         ClassPrinter.toYaml(code, ClassPrinter.Verbosity.TRACE_ALL, System.out::print);
78         Assert.assertEquals(
79                 code.elementList().stream().mapMulti((e, ec) -> {if (e instanceof Instruction i) ec.accept(i.opcode());}).toList(),
80                 List.of(NOP, ICONST_0, IFEQ, GOTO, NOP, RETURN));
81         Assert.assertEquals(code.findAttribute(Attributes.lineNumberTable()).get().lineNumbers().size(), 2);
82     }
83 }