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