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.  Oracle designates this
 8  * particular file as subject to the "Classpath" exception as provided
 9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package java.lang.reflect.code.bytecode;
26 
27 import java.lang.classfile.CodeBuilder;
28 import java.lang.classfile.CodeElement;
29 import java.lang.classfile.CodeTransform;
30 import java.lang.classfile.PseudoInstruction;
31 import java.lang.classfile.instruction.BranchInstruction;
32 import java.lang.classfile.instruction.LabelTarget;
33 import java.util.ArrayList;
34 import java.util.List;
35 
36 /**
37  * BranchCompactor is a CodeTransform skipping redundant branches to immediate targets.
38  */
39 public final class BranchCompactor implements CodeTransform {
40 
41     private BranchInstruction branch;
42     private final List<PseudoInstruction> buffer = new ArrayList<>();
43 
44     public BranchCompactor() {
45     }
46 
47     @Override
48     public void accept(CodeBuilder cob, CodeElement coe) {
49         if (branch == null) {
50             if (coe instanceof BranchInstruction bi && bi.opcode().isUnconditionalBranch()) {
51                 //unconditional branch is stored
52                 branch = bi;
53             } else {
54                 //all other elements are passed
55                 cob.with(coe);
56             }
57         } else {
58             switch (coe) {
59                 case LabelTarget lt when branch.target() == lt.label() -> {
60                     //skip branch to immediate target
61                     branch = null;
62                     //flush the buffer
63                     atEnd(cob);
64                     //pass the target
65                     cob.with(lt);
66                 }
67                 case PseudoInstruction pi -> {
68                     //buffer pseudo instructions
69                     buffer.add(pi);
70                 }
71                 default -> {
72                     //any other instruction flushes the branch and buffer
73                     atEnd(cob);
74                     //replay the code element
75                     accept(cob, coe);
76                 }
77             }
78         }
79     }
80 
81     @Override
82     public void atEnd(CodeBuilder cob) {
83         if (branch != null) {
84             //flush the branch
85             cob.with(branch);
86             branch = null;
87         }
88         //flush the buffer
89         buffer.forEach(cob::with);
90         buffer.clear();
91     }
92 }