1 /*
   2  * Copyright (c) 2015, 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.alloc.trace;
  24 
  25 import static org.graalvm.compiler.lir.LIRValueUtil.asVariable;
  26 import static org.graalvm.compiler.lir.LIRValueUtil.isConstantValue;
  27 import static org.graalvm.compiler.lir.LIRValueUtil.isVariable;
  28 import static org.graalvm.compiler.lir.alloc.trace.TraceUtil.isTrivialTrace;
  29 
  30 import java.util.List;
  31 
  32 import org.graalvm.compiler.core.common.alloc.Trace;
  33 import org.graalvm.compiler.core.common.alloc.TraceBuilderResult;
  34 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
  35 import org.graalvm.compiler.lir.LIR;
  36 import org.graalvm.compiler.lir.LIRInstruction;
  37 import org.graalvm.compiler.lir.LIRInstruction.OperandFlag;
  38 import org.graalvm.compiler.lir.StandardOp.JumpOp;
  39 import org.graalvm.compiler.lir.StandardOp.LabelOp;
  40 import org.graalvm.compiler.lir.ValueProcedure;
  41 import org.graalvm.compiler.lir.Variable;
  42 import org.graalvm.compiler.lir.gen.LIRGenerationResult;
  43 import org.graalvm.compiler.lir.ssi.SSIUtil;
  44 import org.graalvm.compiler.lir.util.VariableVirtualStackValueMap;
  45 
  46 import jdk.vm.ci.code.TargetDescription;
  47 import jdk.vm.ci.meta.Value;
  48 
  49 /**
  50  * Allocates a trivial trace i.e. a trace consisting of a single block with no instructions other
  51  * than the {@link LabelOp} and the {@link JumpOp}.
  52  */
  53 final class TrivialTraceAllocator extends TraceAllocationPhase<TraceAllocationPhase.TraceAllocationContext> {
  54 
  55     @Override
  56     protected void run(TargetDescription target, LIRGenerationResult lirGenRes, Trace trace, TraceAllocationContext context) {
  57         LIR lir = lirGenRes.getLIR();
  58         TraceBuilderResult resultTraces = context.resultTraces;
  59         assert isTrivialTrace(lir, trace) : "Not a trivial trace! " + trace;
  60         AbstractBlockBase<?> block = trace.getBlocks()[0];
  61 
  62         AbstractBlockBase<?> pred = TraceUtil.getBestTraceInterPredecessor(resultTraces, block);
  63 
  64         VariableVirtualStackValueMap<Variable, Value> variableMap = new VariableVirtualStackValueMap<>(lir.numVariables(), 0);
  65         SSIUtil.forEachValuePair(lir, block, pred, (to, from) -> {
  66             if (isVariable(to)) {
  67                 variableMap.put(asVariable(to), from);
  68             }
  69         });
  70 
  71         ValueProcedure outputConsumer = (value, mode, flags) -> {
  72             if (isVariable(value)) {
  73                 Value incomingValue = variableMap.get(asVariable(value));
  74                 assert !flags.contains(OperandFlag.COMPOSITE);
  75                 assert !(SSIUtil.incoming(lir, block).isPhiIn() && isConstantValue(incomingValue)) : "Phi variable cannot be constant: " + incomingValue + " -> " + value;
  76                 return incomingValue;
  77             }
  78             return value;
  79         };
  80 
  81         List<LIRInstruction> instructions = lir.getLIRforBlock(block);
  82         for (LIRInstruction op : instructions) {
  83 
  84             op.forEachOutput(outputConsumer);
  85             op.forEachTemp(outputConsumer);
  86             op.forEachAlive(outputConsumer);
  87             op.forEachInput(outputConsumer);
  88             op.forEachState(outputConsumer);
  89         }
  90     }
  91 }