1 /* 2 * Copyright (c) 2017, 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.core.amd64.test; 24 25 import static org.junit.Assume.assumeTrue; 26 27 import org.graalvm.compiler.asm.amd64.AMD64Address.Scale; 28 import org.graalvm.compiler.core.amd64.AMD64AddressLowering; 29 import org.graalvm.compiler.core.amd64.AMD64AddressNode; 30 import org.graalvm.compiler.core.test.GraalCompilerTest; 31 import org.graalvm.compiler.nodes.ConstantNode; 32 import org.graalvm.compiler.nodes.StructuredGraph; 33 import org.graalvm.compiler.nodes.ValueNode; 34 import org.graalvm.compiler.nodes.calc.AddNode; 35 import org.graalvm.compiler.nodes.calc.LeftShiftNode; 36 import org.graalvm.compiler.nodes.calc.NegateNode; 37 import org.graalvm.compiler.nodes.memory.address.AddressNode; 38 import org.junit.Assert; 39 import org.junit.Before; 40 import org.junit.Test; 41 42 import jdk.vm.ci.amd64.AMD64; 43 44 public class AMD64AddressLoweringTest extends GraalCompilerTest { 45 46 private StructuredGraph graph; 47 private AMD64AddressLowering lowering; 48 49 @Before 50 public void checkAMD64() { 51 assumeTrue("skipping AMD64 specific test", getTarget().arch instanceof AMD64); 52 graph = new StructuredGraph.Builder(getInitialOptions(), getDebugContext()).build(); 53 lowering = new AMD64AddressLowering(); 54 } 55 56 @Test 57 public void convertBaseAndIndexToDisplacement() { 58 ValueNode base = graph.unique(const64(1000)); 59 ValueNode index = graph.unique(const64(10)); 60 AddressNode result = lowering.lower(base, index); 61 assertAddress(result, null, null, Scale.Times1, 1010); 62 } 63 64 @Test 65 public void convertBaseToDisplacement() { 66 ValueNode constantAddress = graph.addOrUniqueWithInputs(const64(1000)); 67 AddressNode result = lowering.lower(constantAddress, null); 68 assertAddress(result, null, null, Scale.Times1, 1000); 69 } 70 71 @Test 72 public void convertBaseAndShiftedIndexToDisplacement() { 73 ValueNode base = graph.addOrUniqueWithInputs(const64(1000)); 74 ValueNode index = graph.addOrUniqueWithInputs(new LeftShiftNode(const64(10), const32(1))); 75 AddressNode result = lowering.lower(base, index); 76 assertAddress(result, null, null, Scale.Times2, 1020); 77 } 78 79 @Test 80 public void convertBaseAndNegatedShiftedIndexToDisplacement() { 81 ValueNode base = graph.addOrUniqueWithInputs(const64(1000)); 82 ValueNode index = graph.addOrUniqueWithInputs(new NegateNode(new LeftShiftNode(const64(10), const32(2)))); 83 AddressNode result = lowering.lower(base, index); 84 assertAddress(result, null, null, Scale.Times4, 960); 85 } 86 87 @Test 88 public void convertNegatedBaseAndNegatedShiftedIndexToDisplacement() { 89 ValueNode base = graph.addOrUniqueWithInputs(new NegateNode(const64(1000))); 90 ValueNode index = graph.addOrUniqueWithInputs(new NegateNode(new LeftShiftNode(const64(10), const32(2)))); 91 AddressNode result = lowering.lower(base, index); 92 assertAddress(result, null, null, Scale.Times4, -1040); 93 } 94 95 @Test 96 public void convertNegatedShiftedBaseAndNegatedIndexToDisplacement() { 97 ValueNode base = graph.addOrUniqueWithInputs(new NegateNode(new LeftShiftNode(const64(10), const32(2)))); 98 ValueNode index = graph.addOrUniqueWithInputs(new NegateNode(const64(1000))); 99 AddressNode result = lowering.lower(base, index); 100 assertAddress(result, null, null, Scale.Times4, -1040); 101 } 102 103 @Test 104 public void convertTwoLevelsOfNegatedShiftedBaseAndNegatedIndexToDisplacement() { 105 ValueNode base = graph.addOrUniqueWithInputs(new NegateNode(new LeftShiftNode(new NegateNode(new LeftShiftNode(const64(500), const32(1))), const32(1)))); 106 ValueNode index = graph.addOrUniqueWithInputs(new NegateNode(new AddNode(new NegateNode(const64(13)), const64(3)))); 107 AddressNode result = lowering.lower(base, index); 108 assertAddress(result, null, null, Scale.Times4, 2010); 109 } 110 111 private static ConstantNode const64(long value) { 112 return ConstantNode.forIntegerBits(Long.SIZE, value); 113 } 114 115 private static ConstantNode const32(long value) { 116 return ConstantNode.forIntegerBits(Integer.SIZE, value); 117 } 118 119 private static void assertAddress(AddressNode actual, ValueNode expectedBase, ValueNode expectedIndex, Scale expectedScale, int expectedDisplacement) { 120 AMD64AddressNode address = (AMD64AddressNode) actual; 121 Assert.assertEquals(expectedBase, address.getBase()); 122 Assert.assertEquals(expectedIndex, address.getIndex()); 123 Assert.assertEquals(expectedScale, address.getScale()); 124 Assert.assertEquals(expectedDisplacement, address.getDisplacement()); 125 } 126 }