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