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