001/*
002 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.lir.test;
024
025import static com.oracle.graal.lir.LIRInstruction.OperandFlag.*;
026import static org.junit.Assert.*;
027
028import java.util.*;
029
030import jdk.internal.jvmci.meta.*;
031
032import org.junit.*;
033
034import com.oracle.graal.lir.*;
035import com.oracle.graal.lir.LIRInstruction.OperandFlag;
036import com.oracle.graal.lir.LIRInstruction.OperandMode;
037import com.oracle.graal.lir.asm.*;
038
039/**
040 * This test verifies that {@link CompositeValue}s are immutable, i.e. that a write to a component
041 * of a {@link CompositeValue} results in a new {@link CompositeValue}.
042 */
043public class CompositeValueReplacementTest1 {
044
045    private static class TestCompositeValue extends CompositeValue {
046
047        @Component({REG, OperandFlag.ILLEGAL}) protected Value value;
048
049        public TestCompositeValue(Value value) {
050            super(LIRKind.Illegal);
051            this.value = value;
052        }
053
054        private static final EnumSet<OperandFlag> flags = EnumSet.of(OperandFlag.REG, OperandFlag.ILLEGAL);
055
056        @Override
057        public CompositeValue forEachComponent(LIRInstruction inst, OperandMode mode, InstructionValueProcedure proc) {
058            Value newValue = proc.doValue(inst, value, mode, flags);
059            if (!value.identityEquals(newValue)) {
060                return new TestCompositeValue(newValue);
061            }
062            return this;
063        }
064
065        @Override
066        protected void forEachComponent(LIRInstruction inst, OperandMode mode, InstructionValueConsumer proc) {
067            proc.visitValue(inst, value, mode, flags);
068        }
069    }
070
071    private static class DummyValue extends AbstractValue {
072
073        private final int id;
074        private static int counter = 1;
075
076        protected DummyValue() {
077            super(LIRKind.Illegal);
078            this.id = counter++;
079        }
080
081        @Override
082        public int hashCode() {
083            final int prime = 31;
084            int result = super.hashCode();
085            result = prime * result + id;
086            return result;
087        }
088
089        @Override
090        public boolean equals(Object obj) {
091            if (this == obj) {
092                return true;
093            }
094            if (!super.equals(obj)) {
095                return false;
096            }
097            if (getClass() != obj.getClass()) {
098                return false;
099            }
100            DummyValue other = (DummyValue) obj;
101            if (id != other.id) {
102                return false;
103            }
104            return true;
105        }
106
107        @Override
108        public String toString() {
109            return "DummyValue [id=" + id + "]";
110        }
111
112    }
113
114    private static final class TestOp extends LIRInstruction {
115        public static final LIRInstructionClass<TestOp> TYPE = LIRInstructionClass.create(TestOp.class);
116
117        @Use({COMPOSITE}) protected TestCompositeValue compValue;
118
119        public TestOp(TestCompositeValue compValue) {
120            super(TYPE);
121            this.compValue = compValue;
122        }
123
124        @Override
125        public void emitCode(CompilationResultBuilder crb) {
126            fail("should not reach!");
127        }
128
129    }
130
131    @Test
132    public void replaceCompValueTest0() {
133        DummyValue dummyValue1 = new DummyValue();
134        DummyValue dummyValue2 = new DummyValue();
135        DummyValue dummyValue3 = new DummyValue();
136        TestCompositeValue compValue1 = new TestCompositeValue(dummyValue1);
137        LIRInstruction op1 = new TestOp(compValue1);
138        LIRInstruction op2 = new TestOp(compValue1);
139
140        op1.forEachInput((instruction, value, mode, flags) -> {
141            assertEquals(dummyValue1, value);
142            return dummyValue2;
143        });
144
145        op2.forEachInput((instruction, value, mode, flags) -> {
146            assertEquals(dummyValue1, value);
147            return dummyValue3;
148        });
149
150        op1.visitEachInput((instruction, value, mode, flags) -> assertEquals(dummyValue2, value));
151        op2.visitEachInput((instruction, value, mode, flags) -> assertEquals(dummyValue3, value));
152    }
153}