1 /* 2 * Copyright (c) 2009, 2018, 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 25 package org.graalvm.compiler.lir; 26 27 import static org.graalvm.compiler.lir.LIR.verifyBlocks; 28 29 import java.util.ArrayList; 30 31 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase; 32 import org.graalvm.compiler.debug.CounterKey; 33 import org.graalvm.compiler.debug.DebugContext; 34 import org.graalvm.compiler.lir.gen.LIRGenerationResult; 35 import org.graalvm.compiler.lir.phases.PostAllocationOptimizationPhase; 36 37 import jdk.vm.ci.code.TargetDescription; 38 39 /** 40 * This class performs basic optimizations on the control flow graph after LIR generation. 41 */ 42 public final class ControlFlowOptimizer extends PostAllocationOptimizationPhase { 43 44 /** 45 * Performs control flow optimizations on the given LIR graph. 46 */ 47 @Override run(TargetDescription target, LIRGenerationResult lirGenRes, PostAllocationOptimizationContext context)48 protected void run(TargetDescription target, LIRGenerationResult lirGenRes, PostAllocationOptimizationContext context) { 49 LIR lir = lirGenRes.getLIR(); 50 new Optimizer(lir).deleteEmptyBlocks(lir.codeEmittingOrder()); 51 } 52 53 private static final class Optimizer { 54 55 private final LIR lir; 56 Optimizer(LIR lir)57 private Optimizer(LIR lir) { 58 this.lir = lir; 59 } 60 61 private static final CounterKey BLOCKS_DELETED = DebugContext.counter("BlocksDeleted"); 62 63 /** 64 * Checks whether a block can be deleted. Only blocks with exactly one successor and an 65 * unconditional branch to this successor are eligable. 66 * 67 * @param block the block checked for deletion 68 * @return whether the block can be deleted 69 */ canDeleteBlock(AbstractBlockBase<?> block)70 private boolean canDeleteBlock(AbstractBlockBase<?> block) { 71 if (block == null || block.getSuccessorCount() != 1 || block.getPredecessorCount() == 0 || block.getSuccessors()[0] == block) { 72 return false; 73 } 74 75 ArrayList<LIRInstruction> instructions = lir.getLIRforBlock(block); 76 77 assert instructions.size() >= 2 : "block must have label and branch"; 78 assert instructions.get(0) instanceof StandardOp.LabelOp : "first instruction must always be a label"; 79 assert instructions.get(instructions.size() - 1) instanceof StandardOp.JumpOp : "last instruction must always be a branch"; 80 assert ((StandardOp.JumpOp) instructions.get(instructions.size() - 1)).destination().label() == ((StandardOp.LabelOp) lir.getLIRforBlock(block.getSuccessors()[0]).get( 81 0)).getLabel() : "branch target must be the successor"; 82 83 // Block must have exactly one successor. 84 return instructions.size() == 2 && !instructions.get(instructions.size() - 1).hasState() && !block.isExceptionEntry(); 85 } 86 alignBlock(AbstractBlockBase<?> block)87 private void alignBlock(AbstractBlockBase<?> block) { 88 if (!block.isAligned()) { 89 block.setAlign(true); 90 ArrayList<LIRInstruction> instructions = lir.getLIRforBlock(block); 91 assert instructions.get(0) instanceof StandardOp.LabelOp : "first instruction must always be a label"; 92 StandardOp.LabelOp label = (StandardOp.LabelOp) instructions.get(0); 93 instructions.set(0, new StandardOp.LabelOp(label.getLabel(), true)); 94 } 95 } 96 deleteEmptyBlocks(AbstractBlockBase<?>[] blocks)97 private void deleteEmptyBlocks(AbstractBlockBase<?>[] blocks) { 98 assert verifyBlocks(lir, blocks); 99 for (int i = 0; i < blocks.length; i++) { 100 AbstractBlockBase<?> block = blocks[i]; 101 if (canDeleteBlock(block)) { 102 103 block.delete(); 104 // adjust successor and predecessor lists 105 AbstractBlockBase<?> other = block.getSuccessors()[0]; 106 if (block.isAligned()) { 107 alignBlock(other); 108 } 109 110 BLOCKS_DELETED.increment(lir.getDebug()); 111 blocks[i] = null; 112 } 113 } 114 assert verifyBlocks(lir, blocks); 115 } 116 } 117 } 118