1 /*
   2  * Copyright (c) 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.core.amd64.test;
  24 
  25 import static org.junit.Assume.assumeTrue;
  26 
  27 import org.junit.Before;
  28 import org.junit.Test;
  29 
  30 import org.graalvm.compiler.lir.LIR;
  31 import org.graalvm.compiler.lir.LIRInstruction;
  32 import org.graalvm.compiler.lir.amd64.AMD64BinaryConsumer.MemoryConstOp;
  33 import org.graalvm.compiler.lir.gen.LIRGenerationResult;
  34 import org.graalvm.compiler.lir.jtt.LIRTest;
  35 import org.graalvm.compiler.lir.phases.LIRPhase;
  36 import org.graalvm.compiler.lir.phases.PreAllocationOptimizationPhase.PreAllocationOptimizationContext;
  37 
  38 import jdk.vm.ci.amd64.AMD64;
  39 import jdk.vm.ci.code.TargetDescription;
  40 
  41 public class MatchRuleTest extends LIRTest {
  42     private static LIR lir;
  43 
  44     @Before
  45     public void checkAMD64() {
  46         assumeTrue("skipping AMD64 specific test", getTarget().arch instanceof AMD64);
  47     }
  48 
  49     public static int test1Snippet(TestClass o, TestClass b, TestClass c) {
  50         if (o.x == 42) {
  51             return b.z;
  52         } else {
  53             return c.y;
  54         }
  55     }
  56 
  57     /**
  58      * Verifies, if the match rules in AMD64NodeMatchRules do work on the graphs by compiling and
  59      * checking if the expected LIR instruction show up.
  60      */
  61     @Test
  62     public void test1() {
  63         getLIRSuites().getPreAllocationOptimizationStage().appendPhase(new CheckPhase());
  64         compile(getResolvedJavaMethod("test1Snippet"), null);
  65         boolean found = false;
  66         for (LIRInstruction ins : lir.getLIRforBlock(lir.codeEmittingOrder()[0])) {
  67             if (ins instanceof MemoryConstOp && ((MemoryConstOp) ins).getOpcode().toString().equals("CMP")) {
  68                 assertFalse("MemoryConstOp expected only once in first block", found);
  69                 found = true;
  70             }
  71         }
  72         assertTrue("Memory compare must be in the LIR", found);
  73     }
  74 
  75     public static class TestClass {
  76         public int x;
  77         public int y;
  78         public int z;
  79 
  80         public TestClass(int x) {
  81             super();
  82             this.x = x;
  83         }
  84     }
  85 
  86     public static class CheckPhase extends LIRPhase<PreAllocationOptimizationContext> {
  87         @Override
  88         protected void run(TargetDescription target, LIRGenerationResult lirGenRes, PreAllocationOptimizationContext context) {
  89             lir = lirGenRes.getLIR();
  90         }
  91     }
  92 }