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 } | 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 } |