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