1 /*
   2  * Copyright (c) 2014, 2019, 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.stackslotalloc;
  26 
  27 import static org.graalvm.compiler.lir.LIRValueUtil.asVirtualStackSlot;
  28 import static org.graalvm.compiler.lir.LIRValueUtil.isVirtualStackSlot;
  29 import static org.graalvm.compiler.lir.stackslotalloc.StackSlotAllocatorUtil.allocatedFramesize;
  30 import static org.graalvm.compiler.lir.stackslotalloc.StackSlotAllocatorUtil.allocatedSlots;
  31 import static org.graalvm.compiler.lir.stackslotalloc.StackSlotAllocatorUtil.virtualFramesize;
  32 
  33 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
  34 import org.graalvm.compiler.debug.DebugContext;
  35 import org.graalvm.compiler.debug.GraalError;
  36 import org.graalvm.compiler.debug.Indent;
  37 import org.graalvm.compiler.lir.LIRInstruction;
  38 import org.graalvm.compiler.lir.ValueProcedure;
  39 import org.graalvm.compiler.lir.VirtualStackSlot;
  40 import org.graalvm.compiler.lir.framemap.FrameMapBuilderTool;
  41 import org.graalvm.compiler.lir.framemap.SimpleVirtualStackSlot;
  42 import org.graalvm.compiler.lir.framemap.VirtualStackSlotRange;
  43 import org.graalvm.compiler.lir.gen.LIRGenerationResult;
  44 import org.graalvm.compiler.lir.phases.AllocationPhase;
  45 
  46 import jdk.vm.ci.code.StackSlot;
  47 import jdk.vm.ci.code.TargetDescription;
  48 
  49 public class SimpleStackSlotAllocator extends AllocationPhase {
  50 
  51     @Override
  52     protected void run(TargetDescription target, LIRGenerationResult lirGenRes, AllocationContext context) {
  53         allocateStackSlots((FrameMapBuilderTool) lirGenRes.getFrameMapBuilder(), lirGenRes);
  54         lirGenRes.buildFrameMap();
  55     }
  56 
  57     public void allocateStackSlots(FrameMapBuilderTool builder, LIRGenerationResult res) {
  58         DebugContext debug = res.getLIR().getDebug();
  59         StackSlot[] mapping = new StackSlot[builder.getNumberOfStackSlots()];
  60         boolean allocatedFramesizeEnabled = allocatedFramesize.isEnabled(debug);
  61         long currentFrameSize = allocatedFramesizeEnabled ? builder.getFrameMap().currentFrameSize() : 0;
  62         for (VirtualStackSlot virtualSlot : builder.getStackSlots()) {
  63             final StackSlot slot;
  64             if (virtualSlot instanceof SimpleVirtualStackSlot) {
  65                 slot = mapSimpleVirtualStackSlot(builder, (SimpleVirtualStackSlot) virtualSlot);
  66                 virtualFramesize.add(debug, builder.getFrameMap().spillSlotSize(virtualSlot.getValueKind()));
  67             } else if (virtualSlot instanceof VirtualStackSlotRange) {
  68                 VirtualStackSlotRange slotRange = (VirtualStackSlotRange) virtualSlot;
  69                 slot = mapVirtualStackSlotRange(builder, slotRange);
  70                 virtualFramesize.add(debug, builder.getFrameMap().spillSlotRangeSize(slotRange.getSlots()));
  71             } else {
  72                 throw GraalError.shouldNotReachHere("Unknown VirtualStackSlot: " + virtualSlot);
  73             }
  74             allocatedSlots.increment(debug);
  75             mapping[virtualSlot.getId()] = slot;
  76         }
  77         updateLIR(res, mapping);
  78         if (allocatedFramesizeEnabled) {
  79             allocatedFramesize.add(debug, builder.getFrameMap().currentFrameSize() - currentFrameSize);
  80         }
  81     }
  82 
  83     @SuppressWarnings("try")
  84     protected void updateLIR(LIRGenerationResult res, StackSlot[] mapping) {
  85         DebugContext debug = res.getLIR().getDebug();
  86         try (DebugContext.Scope scope = debug.scope("StackSlotMappingLIR")) {
  87             ValueProcedure updateProc = (value, mode, flags) -> {
  88                 if (isVirtualStackSlot(value)) {
  89                     StackSlot stackSlot = mapping[asVirtualStackSlot(value).getId()];
  90                     debug.log("map %s -> %s", value, stackSlot);
  91                     return stackSlot;
  92                 }
  93                 return value;
  94             };
  95             for (AbstractBlockBase<?> block : res.getLIR().getControlFlowGraph().getBlocks()) {
  96                 try (Indent indent0 = debug.logAndIndent("block: %s", block)) {
  97                     for (LIRInstruction inst : res.getLIR().getLIRforBlock(block)) {
  98                         try (Indent indent1 = debug.logAndIndent("Inst: %d: %s", inst.id(), inst)) {
  99                             inst.forEachAlive(updateProc);
 100                             inst.forEachInput(updateProc);
 101                             inst.forEachOutput(updateProc);
 102                             inst.forEachTemp(updateProc);
 103                             inst.forEachState(updateProc);
 104                         }
 105                     }
 106                 }
 107             }
 108         }
 109     }
 110 
 111     protected StackSlot mapSimpleVirtualStackSlot(FrameMapBuilderTool builder, SimpleVirtualStackSlot virtualStackSlot) {
 112         return builder.getFrameMap().allocateSpillSlot(virtualStackSlot.getValueKind());
 113     }
 114 
 115     protected StackSlot mapVirtualStackSlotRange(FrameMapBuilderTool builder, VirtualStackSlotRange virtualStackSlot) {
 116         return builder.getFrameMap().allocateStackSlots(virtualStackSlot.getSlots());
 117     }
 118 }