1 /* 2 * Copyright (c) 2016, 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 package org.graalvm.compiler.lir.ssi; 24 25 import java.util.BitSet; 26 27 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase; 28 import org.graalvm.compiler.debug.Debug; 29 import org.graalvm.compiler.debug.GraalError; 30 import org.graalvm.compiler.debug.Indent; 31 import org.graalvm.compiler.lir.LIR; 32 import org.graalvm.compiler.lir.StandardOp.BlockEndOp; 33 import org.graalvm.compiler.lir.StandardOp.LabelOp; 34 35 import jdk.vm.ci.meta.Value; 36 37 public abstract class SSIBuilderBase { 38 39 protected static final int LOG_LEVEL = Debug.INFO_LOG_LEVEL; 40 protected final Value[] operands; 41 protected final LIR lir; 42 43 public SSIBuilderBase(LIR lir) { 44 this.lir = lir; 45 this.operands = new Value[lir.numVariables()]; 46 } 47 48 protected LIR getLIR() { 49 return lir; 50 } 51 52 abstract BitSet getLiveIn(AbstractBlockBase<?> block); 53 54 abstract BitSet getLiveOut(AbstractBlockBase<?> block); 55 56 public final void build() { 57 buildIntern(); 58 // check that the liveIn set of the first block is empty 59 AbstractBlockBase<?> startBlock = getLIR().getControlFlowGraph().getStartBlock(); 60 if (getLiveIn(startBlock).cardinality() != 0) { 61 // bailout if this occurs in product mode. 62 throw new GraalError("liveIn set of first block must be empty: " + getLiveIn(startBlock)); 63 } 64 } 65 66 protected abstract void buildIntern(); 67 68 @SuppressWarnings("try") 69 public final void finish() { 70 // iterate all blocks in reverse order 71 for (AbstractBlockBase<?> block : (AbstractBlockBase<?>[]) lir.getControlFlowGraph().getBlocks()) { 72 try (Indent indent = Debug.logAndIndent(LOG_LEVEL, "Finish Block %s", block)) { 73 // set label 74 buildOutgoing(block, getLiveOut(block)); 75 buildIncoming(block, getLiveIn(block)); 76 } 77 } 78 } 79 80 private void buildIncoming(AbstractBlockBase<?> block, BitSet liveIn) { 81 /* 82 * Collect live out of predecessors since there might be values not used in this block which 83 * might cause out/in mismatch. 84 */ 85 BitSet predLiveOut = new BitSet(liveIn.length()); 86 for (AbstractBlockBase<?> pred : block.getPredecessors()) { 87 predLiveOut.or(getLiveOut(pred)); 88 } 89 if (predLiveOut.isEmpty()) { 90 return; 91 } 92 93 Value[] values = new Value[predLiveOut.cardinality()]; 94 assert values.length > 0; 95 int cnt = 0; 96 for (int i = predLiveOut.nextSetBit(0); i >= 0; i = predLiveOut.nextSetBit(i + 1)) { 97 values[cnt++] = liveIn.get(i) ? operands[i] : Value.ILLEGAL; 98 } 99 LabelOp label = SSIUtil.incoming(getLIR(), block); 100 label.addIncomingValues(values); 101 } 102 103 private void buildOutgoing(AbstractBlockBase<?> block, BitSet liveOut) { 104 if (liveOut.isEmpty()) { 105 return; 106 } 107 Value[] values = new Value[liveOut.cardinality()]; 108 assert values.length > 0; 109 int cnt = 0; 110 for (int i = liveOut.nextSetBit(0); i >= 0; i = liveOut.nextSetBit(i + 1)) { 111 values[cnt++] = operands[i]; 112 } 113 BlockEndOp blockEndOp = SSIUtil.outgoing(getLIR(), block); 114 blockEndOp.addOutgoingValues(values); 115 } 116 }