001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.nabla.algorithmic.forward.trimming;
018    
019    import org.objectweb.asm.Opcodes;
020    import org.objectweb.asm.tree.AbstractInsnNode;
021    import org.objectweb.asm.tree.InsnList;
022    
023    /** Trimmer removing (DLOAD i, POP2).
024     */
025    public class DLoadPop2Trimmer extends BytecodeTrimmer {
026    
027        // CHECKSTYLE: stop HideUtilityClassConstructor
028        /** Holder for the singleton instance.*/
029        private static class LazyHolder  {
030            /** The singleton instance. */
031            private static final BytecodeTrimmer INSTANCE = new DLoadPop2Trimmer();
032        }
033        // CHECKSTYLE: resume HideUtilityClassConstructor
034    
035        /** Hidden constructor.
036         */
037        private DLoadPop2Trimmer() {
038            super(2);
039        }
040    
041        /** Get the singleton instance.
042         * <p>We use here the Initialization on Demand Holder idiom.</p>
043         * @return the singleton instance
044         */
045        public static BytecodeTrimmer getInstance() {
046            return LazyHolder.INSTANCE;
047        }
048    
049        /** {@inheritDoc} */
050        @Override
051        protected boolean trimWindow(final InsnList instructions,
052                                     final AbstractInsnNode[] window) {
053    
054            if ((window[0].getOpcode() == Opcodes.DUP2) &&
055                (window[1].getOpcode() == Opcodes.POP2)) {
056    
057                // remove the no-op instructions pair
058                instructions.remove(window[0]);
059                instructions.remove(window[1]);
060    
061                return true;
062    
063            }
064    
065            // nothing have been done
066            return false;
067    
068        }
069    
070    }