1 /* 2 * Copyright (c) 2015, 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.amd64.phases; 24 25 import static org.graalvm.compiler.lir.phases.LIRPhase.Options.LIROptimization; 26 27 import java.util.ArrayList; 28 import java.util.Collections; 29 import java.util.List; 30 31 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase; 32 import org.graalvm.compiler.debug.CounterKey; 33 import org.graalvm.compiler.debug.DebugContext; 34 import org.graalvm.compiler.lir.LIR; 35 import org.graalvm.compiler.lir.LIRInstruction; 36 import org.graalvm.compiler.lir.RedundantMoveElimination; 37 import org.graalvm.compiler.lir.amd64.AMD64Move.AMD64MultiStackMove; 38 import org.graalvm.compiler.lir.amd64.AMD64Move.AMD64StackMove; 39 import org.graalvm.compiler.lir.gen.LIRGenerationResult; 40 import org.graalvm.compiler.lir.phases.PostAllocationOptimizationPhase; 41 import org.graalvm.compiler.options.NestedBooleanOptionKey; 42 import org.graalvm.compiler.options.Option; 43 import org.graalvm.compiler.options.OptionType; 44 45 import jdk.vm.ci.code.Register; 46 import jdk.vm.ci.code.TargetDescription; 47 import jdk.vm.ci.meta.AllocatableValue; 48 import jdk.vm.ci.meta.Value; 49 50 /** 51 * Replaces sequential {@link AMD64StackMove}s of the same type with a single 52 * {@link AMD64MultiStackMove} to avoid storing/restoring the scratch register multiple times. 53 * 54 * Note: this phase must be inserted <b>after</b> {@link RedundantMoveElimination} phase because 55 * {@link AMD64MultiStackMove} are not probably detected. 56 */ 57 public class StackMoveOptimizationPhase extends PostAllocationOptimizationPhase { 58 public static class Options { 59 // @formatter:off 60 @Option(help = "", type = OptionType.Debug) 61 public static final NestedBooleanOptionKey LIROptStackMoveOptimizer = new NestedBooleanOptionKey(LIROptimization, true); 62 // @formatter:on 63 } 64 65 private static final CounterKey eliminatedBackup = DebugContext.counter("StackMoveOptimizer[EliminatedScratchBackupRestore]"); 66 67 @Override 68 protected void run(TargetDescription target, LIRGenerationResult lirGenRes, PostAllocationOptimizationContext context) { 69 LIR lir = lirGenRes.getLIR(); 70 DebugContext debug = lir.getDebug(); 71 for (AbstractBlockBase<?> block : lir.getControlFlowGraph().getBlocks()) { 72 ArrayList<LIRInstruction> instructions = lir.getLIRforBlock(block); 73 new Closure().process(debug, instructions); 74 } 75 } 76 77 private static class Closure { 78 private static final int NONE = -1; 79 80 private int begin = NONE; 81 private Register reg = null; 82 private List<AllocatableValue> dst; 83 private List<Value> src; 84 private AllocatableValue slot; 85 private boolean removed = false; 86 87 public void process(DebugContext debug, List<LIRInstruction> instructions) { 88 for (int i = 0; i < instructions.size(); i++) { 89 LIRInstruction inst = instructions.get(i); 90 91 if (isStackMove(inst)) { 92 AMD64StackMove move = asStackMove(inst); 93 94 if (reg != null && !reg.equals(move.getScratchRegister())) { 95 // end of trace & start of new 96 replaceStackMoves(debug, instructions); 97 } 98 99 // lazy initialize 100 if (dst == null) { 101 assert src == null; 102 dst = new ArrayList<>(); 103 src = new ArrayList<>(); 104 } 105 106 dst.add(move.getResult()); 107 src.add(move.getInput()); 108 109 if (begin == NONE) { 110 // trace begin 111 begin = i; 112 reg = move.getScratchRegister(); 113 slot = move.getBackupSlot(); 114 } 115 116 } else if (begin != NONE) { 117 // end of trace 118 replaceStackMoves(debug, instructions); 119 } 120 } 121 // remove instructions 122 if (removed) { 123 instructions.removeAll(Collections.singleton(null)); 124 } 125 126 } 127 128 private void replaceStackMoves(DebugContext debug, List<LIRInstruction> instructions) { 129 int size = dst.size(); 130 if (size > 1) { 131 AMD64MultiStackMove multiMove = new AMD64MultiStackMove(dst.toArray(new AllocatableValue[size]), src.toArray(new AllocatableValue[size]), reg, slot); 132 // replace first instruction 133 instructions.set(begin, multiMove); 134 // and null out others 135 Collections.fill(instructions.subList(begin + 1, begin + size), null); 136 // removed 137 removed = true; 138 eliminatedBackup.add(debug, size - 1); 139 } 140 // reset 141 dst.clear(); 142 src.clear(); 143 begin = NONE; 144 reg = null; 145 slot = null; 146 } 147 } 148 149 private static AMD64StackMove asStackMove(LIRInstruction inst) { 150 assert isStackMove(inst); 151 return (AMD64StackMove) inst; 152 } 153 154 private static boolean isStackMove(LIRInstruction inst) { 155 return inst instanceof AMD64StackMove; 156 } 157 158 }