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