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.test;
  24 
  25 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.COMPOSITE;
  26 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
  27 import static org.junit.Assert.assertEquals;
  28 import static org.junit.Assert.fail;
  29 
  30 import java.util.EnumSet;
  31 
  32 import org.junit.Test;
  33 
  34 import org.graalvm.compiler.core.common.LIRKind;
  35 import org.graalvm.compiler.lir.CompositeValue;
  36 import org.graalvm.compiler.lir.InstructionValueConsumer;
  37 import org.graalvm.compiler.lir.InstructionValueProcedure;
  38 import org.graalvm.compiler.lir.LIRInstruction;
  39 import org.graalvm.compiler.lir.LIRInstruction.OperandFlag;
  40 import org.graalvm.compiler.lir.LIRInstruction.OperandMode;
  41 import org.graalvm.compiler.lir.LIRInstructionClass;
  42 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  43 
  44 import jdk.vm.ci.meta.Value;
  45 
  46 /**
  47  * This test verifies that {@link CompositeValue}s are immutable, i.e. that a write to a component
  48  * of a {@link CompositeValue} results in a new {@link CompositeValue}.
  49  */
  50 public class CompositeValueReplacementTest1 {
  51 
  52     private static class TestCompositeValue extends CompositeValue {
  53 
  54         @Component({REG, OperandFlag.ILLEGAL}) protected Value value;
  55 
  56         TestCompositeValue(Value value) {
  57             super(LIRKind.Illegal);
  58             this.value = value;
  59         }
  60 
  61         private static final EnumSet<OperandFlag> flags = EnumSet.of(OperandFlag.REG, OperandFlag.ILLEGAL);
  62 
  63         @Override
  64         public CompositeValue forEachComponent(LIRInstruction inst, OperandMode mode, InstructionValueProcedure proc) {
  65             Value newValue = proc.doValue(inst, value, mode, flags);
  66             if (!value.identityEquals(newValue)) {
  67                 return new TestCompositeValue(newValue);
  68             }
  69             return this;
  70         }
  71 
  72         @Override
  73         protected void visitEachComponent(LIRInstruction inst, OperandMode mode, InstructionValueConsumer proc) {
  74             proc.visitValue(inst, value, mode, flags);
  75         }
  76     }
  77 
  78     private static class DummyValue extends Value {
  79 
  80         private final int id;
  81         private static int counter = 1;
  82 
  83         protected DummyValue() {
  84             super(LIRKind.Illegal);
  85             this.id = counter++;
  86         }
  87 
  88         @Override
  89         public int hashCode() {
  90             final int prime = 31;
  91             int result = super.hashCode();
  92             result = prime * result + id;
  93             return result;
  94         }
  95 
  96         @Override
  97         public boolean equals(Object obj) {
  98             if (this == obj) {
  99                 return true;
 100             }
 101             if (!super.equals(obj)) {
 102                 return false;
 103             }
 104             if (getClass() != obj.getClass()) {
 105                 return false;
 106             }
 107             DummyValue other = (DummyValue) obj;
 108             if (id != other.id) {
 109                 return false;
 110             }
 111             return true;
 112         }
 113 
 114         @Override
 115         public String toString() {
 116             return "DummyValue [id=" + id + "]";
 117         }
 118 
 119     }
 120 
 121     private static final class TestOp extends LIRInstruction {
 122         public static final LIRInstructionClass<TestOp> TYPE = LIRInstructionClass.create(TestOp.class);
 123 
 124         @Use({COMPOSITE}) protected TestCompositeValue compValue;
 125 
 126         TestOp(TestCompositeValue compValue) {
 127             super(TYPE);
 128             this.compValue = compValue;
 129         }
 130 
 131         @Override
 132         public void emitCode(CompilationResultBuilder crb) {
 133             fail("should not reach!");
 134         }
 135 
 136     }
 137 
 138     @Test
 139     public void replaceCompValueTest0() {
 140         DummyValue dummyValue1 = new DummyValue();
 141         DummyValue dummyValue2 = new DummyValue();
 142         DummyValue dummyValue3 = new DummyValue();
 143         TestCompositeValue compValue1 = new TestCompositeValue(dummyValue1);
 144         LIRInstruction op1 = new TestOp(compValue1);
 145         LIRInstruction op2 = new TestOp(compValue1);
 146 
 147         op1.forEachInput((instruction, value, mode, flags) -> {
 148             assertEquals(dummyValue1, value);
 149             return dummyValue2;
 150         });
 151 
 152         op2.forEachInput((instruction, value, mode, flags) -> {
 153             assertEquals(dummyValue1, value);
 154             return dummyValue3;
 155         });
 156 
 157         op1.visitEachInput((instruction, value, mode, flags) -> assertEquals(dummyValue2, value));
 158         op2.visitEachInput((instruction, value, mode, flags) -> assertEquals(dummyValue3, value));
 159     }
 160 }