1 /*
   2  * Copyright (c) 2011, 2018, 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;
  26 
  27 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.CONST;
  28 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.HINT;
  29 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.OUTGOING;
  30 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
  31 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.STACK;
  32 
  33 import java.util.ArrayList;
  34 import java.util.EnumSet;
  35 
  36 import jdk.internal.vm.compiler.collections.EconomicSet;
  37 import org.graalvm.compiler.asm.Label;
  38 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
  39 import org.graalvm.compiler.debug.GraalError;
  40 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  41 import org.graalvm.compiler.lir.framemap.FrameMap;
  42 
  43 import jdk.vm.ci.code.Register;
  44 import jdk.vm.ci.code.RegisterSaveLayout;
  45 import jdk.vm.ci.code.StackSlot;
  46 import jdk.vm.ci.meta.AllocatableValue;
  47 import jdk.vm.ci.meta.Constant;
  48 import jdk.vm.ci.meta.Value;
  49 
  50 /**
  51  * A collection of machine-independent LIR operations, as well as interfaces to be implemented for
  52  * specific kinds or LIR operations.
  53  */
  54 public class StandardOp {
  55 
  56     /**
  57      * A block delimiter. Every well formed block must contain exactly one such operation and it
  58      * must be the last operation in the block.
  59      */
  60     public interface BlockEndOp {
  61     }
  62 
  63     public interface NullCheck {
  64         Value getCheckedValue();
  65 
  66         LIRFrameState getState();
  67     }
  68 
  69     public interface ImplicitNullCheck {
  70         boolean makeNullCheckFor(Value value, LIRFrameState nullCheckState, int implicitNullCheckLimit);
  71     }
  72 
  73     /**
  74      * LIR operation that defines the position of a label.
  75      */
  76     public static final class LabelOp extends LIRInstruction {
  77         public static final LIRInstructionClass<LabelOp> TYPE = LIRInstructionClass.create(LabelOp.class);
  78         public static final EnumSet<OperandFlag> incomingFlags = EnumSet.of(REG, STACK);
  79 
  80         /**
  81          * In the LIR, every register and variable must be defined before it is used. For method
  82          * parameters that are passed in fixed registers, exception objects passed to the exception
  83          * handler in a fixed register, or any other use of a fixed register not defined in this
  84          * method, an artificial definition is necessary. To avoid spill moves to be inserted
  85          * between the label at the beginning of a block an an actual definition in the second
  86          * instruction of a block, the registers are defined here in the label.
  87          */
  88         @Def({REG, STACK}) private Value[] incomingValues;
  89         private final Label label;
  90         private final boolean align;
  91         private int numbPhis;
  92 
  93         public LabelOp(Label label, boolean align) {
  94             super(TYPE);
  95             this.label = label;
  96             this.align = align;
  97             this.incomingValues = Value.NO_VALUES;
  98             this.numbPhis = 0;
  99         }
 100 
 101         public void setPhiValues(Value[] values) {
 102             setIncomingValues(values);
 103             setNumberOfPhis(values.length);
 104         }
 105 
 106         private void setNumberOfPhis(int numPhis) {
 107             assert numbPhis == 0;
 108             numbPhis = numPhis;
 109         }
 110 
 111         public int getPhiSize() {
 112             return numbPhis;
 113         }
 114 
 115         public void setIncomingValues(Value[] values) {
 116             assert this.incomingValues.length == 0;
 117             assert values != null;
 118             this.incomingValues = values;
 119         }
 120 
 121         public int getIncomingSize() {
 122             return incomingValues.length;
 123         }
 124 
 125         public Value getIncomingValue(int idx) {
 126             assert checkRange(idx);
 127             return incomingValues[idx];
 128         }
 129 
 130         public void clearIncomingValues() {
 131             incomingValues = Value.NO_VALUES;
 132         }
 133 
 134         public void addIncomingValues(Value[] values) {
 135             if (incomingValues.length == 0) {
 136                 setIncomingValues(values);
 137                 return;
 138             }
 139             int t = incomingValues.length + values.length;
 140             Value[] newArray = new Value[t];
 141             System.arraycopy(incomingValues, 0, newArray, 0, incomingValues.length);
 142             System.arraycopy(values, 0, newArray, incomingValues.length, values.length);
 143             incomingValues = newArray;
 144         }
 145 
 146         private boolean checkRange(int idx) {
 147             return idx < incomingValues.length;
 148         }
 149 
 150         @Override
 151         public void emitCode(CompilationResultBuilder crb) {
 152             if (align) {
 153                 crb.asm.align(crb.target.wordSize * 2);
 154             }
 155             crb.asm.bind(label);
 156         }
 157 
 158         public Label getLabel() {
 159             return label;
 160         }
 161 
 162         /**
 163          * @return true if this label acts as a PhiIn.
 164          */
 165         public boolean isPhiIn() {
 166             return getPhiSize() > 0;
 167         }
 168 
 169         public void forEachIncomingValue(InstructionValueProcedure proc) {
 170             for (int i = 0; i < incomingValues.length; i++) {
 171                 incomingValues[i] = proc.doValue(this, incomingValues[i], OperandMode.DEF, incomingFlags);
 172             }
 173         }
 174     }
 175 
 176     /**
 177      * LIR operation that is an unconditional jump to a {@link #destination()}.
 178      */
 179     public static class JumpOp extends LIRInstruction implements BlockEndOp {
 180         public static final LIRInstructionClass<JumpOp> TYPE = LIRInstructionClass.create(JumpOp.class);
 181         public static final EnumSet<OperandFlag> outgoingFlags = EnumSet.of(REG, STACK, CONST, OUTGOING);
 182 
 183         @Alive({REG, STACK, CONST, OUTGOING}) private Value[] outgoingValues;
 184 
 185         private final LabelRef destination;
 186 
 187         public JumpOp(LabelRef destination) {
 188             this(TYPE, destination);
 189         }
 190 
 191         protected JumpOp(LIRInstructionClass<? extends JumpOp> c, LabelRef destination) {
 192             super(c);
 193             this.destination = destination;
 194             this.outgoingValues = Value.NO_VALUES;
 195         }
 196 
 197         @Override
 198         public void emitCode(CompilationResultBuilder crb) {
 199             if (!crb.isSuccessorEdge(destination)) {
 200                 crb.asm.jmp(destination.label());
 201             }
 202         }
 203 
 204         public LabelRef destination() {
 205             return destination;
 206         }
 207 
 208         public void setPhiValues(Value[] values) {
 209             assert this.outgoingValues.length == 0;
 210             assert values != null;
 211             this.outgoingValues = values;
 212         }
 213 
 214         public int getPhiSize() {
 215             return outgoingValues.length;
 216         }
 217 
 218         public Value getOutgoingValue(int idx) {
 219             assert checkRange(idx);
 220             return outgoingValues[idx];
 221         }
 222 
 223         public void clearOutgoingValues() {
 224             outgoingValues = Value.NO_VALUES;
 225         }
 226 
 227         private boolean checkRange(int idx) {
 228             return idx < outgoingValues.length;
 229         }
 230     }
 231 
 232     /**
 233      * Marker interface for a LIR operation that is a conditional jump.
 234      */
 235     public interface BranchOp extends BlockEndOp {
 236     }
 237 
 238     /**
 239      * Marker interface for a LIR operation that moves a value to {@link #getResult()}.
 240      */
 241     public interface MoveOp {
 242 
 243         AllocatableValue getResult();
 244 
 245         // Checkstyle: stop
 246         static MoveOp asMoveOp(LIRInstruction op) {
 247             return (MoveOp) op;
 248         }
 249         // Checkstyle: resume
 250 
 251         static boolean isMoveOp(LIRInstruction op) {
 252             return op.isMoveOp();
 253         }
 254     }
 255 
 256     /**
 257      * Marker interface for a LIR operation that moves some non-constant value to another location.
 258      */
 259     public interface ValueMoveOp extends MoveOp {
 260 
 261         AllocatableValue getInput();
 262 
 263         // Checkstyle: stop
 264         static ValueMoveOp asValueMoveOp(LIRInstruction op) {
 265             return (ValueMoveOp) op;
 266         }
 267         // Checkstyle: resume
 268 
 269         static boolean isValueMoveOp(LIRInstruction op) {
 270             return op.isValueMoveOp();
 271         }
 272     }
 273 
 274     /**
 275      * Marker interface for a LIR operation that loads a {@link #getConstant()}.
 276      */
 277     public interface LoadConstantOp extends MoveOp {
 278 
 279         Constant getConstant();
 280 
 281         // Checkstyle: stop
 282         static LoadConstantOp asLoadConstantOp(LIRInstruction op) {
 283             return (LoadConstantOp) op;
 284         }
 285         // Checkstyle: resume
 286 
 287         static boolean isLoadConstantOp(LIRInstruction op) {
 288             return op.isLoadConstantOp();
 289         }
 290     }
 291 
 292     /**
 293      * An operation that saves registers to the stack. The set of saved registers can be
 294      * {@linkplain #remove(EconomicSet) pruned} and a mapping from registers to the frame slots in
 295      * which they are saved can be {@linkplain #getMap(FrameMap) retrieved}.
 296      */
 297     public interface SaveRegistersOp {
 298 
 299         /**
 300          * Determines if the {@link #remove(EconomicSet)} operation is supported for this object.
 301          */
 302         boolean supportsRemove();
 303 
 304         /**
 305          * Prunes {@code doNotSave} from the registers saved by this operation.
 306          *
 307          * @param doNotSave registers that should not be saved by this operation
 308          * @return the number of registers pruned
 309          * @throws UnsupportedOperationException if removal is not {@linkplain #supportsRemove()
 310          *             supported}
 311          */
 312         int remove(EconomicSet<Register> doNotSave);
 313 
 314         /**
 315          * Gets a map from the saved registers saved by this operation to the frame slots in which
 316          * they are saved.
 317          *
 318          * @param frameMap used to {@linkplain FrameMap#offsetForStackSlot(StackSlot) convert} a
 319          *            virtual slot to a frame slot index
 320          */
 321         RegisterSaveLayout getMap(FrameMap frameMap);
 322 
 323     }
 324 
 325     /**
 326      * A LIR operation that does nothing. If the operation records its position, it can be
 327      * subsequently {@linkplain #replace(LIR, LIRInstruction) replaced}.
 328      */
 329     public static final class NoOp extends LIRInstruction {
 330         public static final LIRInstructionClass<NoOp> TYPE = LIRInstructionClass.create(NoOp.class);
 331 
 332         /**
 333          * The block in which this instruction is located.
 334          */
 335         final AbstractBlockBase<?> block;
 336 
 337         /**
 338          * The block index of this instruction.
 339          */
 340         final int index;
 341 
 342         public NoOp(AbstractBlockBase<?> block, int index) {
 343             super(TYPE);
 344             this.block = block;
 345             this.index = index;
 346         }
 347 
 348         public void replace(LIR lir, LIRInstruction replacement) {
 349             ArrayList<LIRInstruction> instructions = lir.getLIRforBlock(block);
 350             assert instructions.get(index).equals(this) : String.format("Replacing the wrong instruction: %s instead of %s", instructions.get(index), this);
 351             instructions.set(index, replacement);
 352         }
 353 
 354         public void remove(LIR lir) {
 355             ArrayList<LIRInstruction> instructions = lir.getLIRforBlock(block);
 356             assert instructions.get(index).equals(this) : String.format("Removing the wrong instruction: %s instead of %s", instructions.get(index), this);
 357             instructions.remove(index);
 358         }
 359 
 360         @Override
 361         public void emitCode(CompilationResultBuilder crb) {
 362             if (block != null) {
 363                 throw new GraalError(this + " should have been replaced");
 364             }
 365         }
 366     }
 367 
 368     @Opcode("BLACKHOLE")
 369     public static final class BlackholeOp extends LIRInstruction {
 370         public static final LIRInstructionClass<BlackholeOp> TYPE = LIRInstructionClass.create(BlackholeOp.class);
 371 
 372         @Use({REG, STACK, CONST}) private Value value;
 373 
 374         public BlackholeOp(Value value) {
 375             super(TYPE);
 376             this.value = value;
 377         }
 378 
 379         @Override
 380         public void emitCode(CompilationResultBuilder crb) {
 381             // do nothing, just keep value alive until at least here
 382         }
 383     }
 384 
 385     public static final class BindToRegisterOp extends LIRInstruction {
 386         public static final LIRInstructionClass<BindToRegisterOp> TYPE = LIRInstructionClass.create(BindToRegisterOp.class);
 387 
 388         @Use({REG}) private Value value;
 389 
 390         public BindToRegisterOp(Value value) {
 391             super(TYPE);
 392             this.value = value;
 393         }
 394 
 395         @Override
 396         public void emitCode(CompilationResultBuilder crb) {
 397             // do nothing, just keep value alive until at least here
 398         }
 399     }
 400 
 401     @Opcode("SPILLREGISTERS")
 402     public static final class SpillRegistersOp extends LIRInstruction {
 403         public static final LIRInstructionClass<SpillRegistersOp> TYPE = LIRInstructionClass.create(SpillRegistersOp.class);
 404 
 405         public SpillRegistersOp() {
 406             super(TYPE);
 407         }
 408 
 409         @Override
 410         public boolean destroysCallerSavedRegisters() {
 411             return true;
 412         }
 413 
 414         @Override
 415         public void emitCode(CompilationResultBuilder crb) {
 416             // do nothing, just keep value alive until at least here
 417         }
 418     }
 419 
 420     public static final class StackMove extends LIRInstruction implements ValueMoveOp {
 421         public static final LIRInstructionClass<StackMove> TYPE = LIRInstructionClass.create(StackMove.class);
 422 
 423         @Def({STACK, HINT}) protected AllocatableValue result;
 424         @Use({STACK}) protected AllocatableValue input;
 425 
 426         public StackMove(AllocatableValue result, AllocatableValue input) {
 427             super(TYPE);
 428             this.result = result;
 429             this.input = input;
 430         }
 431 
 432         @Override
 433         public void emitCode(CompilationResultBuilder crb) {
 434             throw new GraalError(this + " should have been removed");
 435         }
 436 
 437         @Override
 438         public AllocatableValue getInput() {
 439             return input;
 440         }
 441 
 442         @Override
 443         public AllocatableValue getResult() {
 444             return result;
 445         }
 446     }
 447 
 448 }