1 /*
   2  * Copyright (c) 2014, 2016, 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 package compiler.testlibrary.sha.predicate;
  25 
  26 import jdk.test.lib.Platform;
  27 import jdk.test.lib.cli.predicate.AndPredicate;
  28 import jdk.test.lib.cli.predicate.CPUSpecificPredicate;
  29 import jdk.test.lib.cli.predicate.OrPredicate;
  30 import sun.hotspot.WhiteBox;
  31 
  32 import java.util.function.BooleanSupplier;
  33 
  34 /**
  35  * Helper class aimed to provide predicates on availability of SHA-related
  36  * CPU instructions and intrinsics.
  37  */
  38 public class IntrinsicPredicates {
  39     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  40     private static final long TIERED_MAX_LEVEL = 4L;
  41     /**
  42      * Boolean supplier that check if any method could be compiled by C2.
  43      * Method potentially could be compiled by C2 if Server VM is used and
  44      * either tiered compilation is disabled or TIERED_MAX_LEVEL tier is
  45      * reachable.
  46      *
  47      * Please don't place this definition after SHA*_INTRINSICS_AVAILABLE
  48      * definitions. Otherwise its value will be {@code null} at the time when
  49      * all dependent fields will be initialized.
  50      */
  51     private static final BooleanSupplier COMPILABLE_BY_C2 = () -> {
  52         boolean isTiered = IntrinsicPredicates.WHITE_BOX.getBooleanVMFlag(
  53                 "TieredCompilation");
  54         long tieredMaxLevel = IntrinsicPredicates.WHITE_BOX.getIntxVMFlag(
  55                 "TieredStopAtLevel");
  56         boolean maxLevelIsReachable = (tieredMaxLevel
  57                 == IntrinsicPredicates.TIERED_MAX_LEVEL);
  58         return Platform.isServer() && !Platform.isEmulatedClient() && (!isTiered || maxLevelIsReachable);
  59     };
  60 
  61     public static final BooleanSupplier SHA1_INSTRUCTION_AVAILABLE
  62             = new OrPredicate(new CPUSpecificPredicate("aarch64.*", new String[] { "sha1" }, null),
  63               new OrPredicate(new CPUSpecificPredicate("s390.*",    new String[] { "sha1" }, null),
  64               new OrPredicate(new CPUSpecificPredicate("sparc.*",   new String[] { "sha1" }, null),
  65               // x86 variants
  66               new OrPredicate(new CPUSpecificPredicate("amd64.*",   new String[] { "sha" },  null),
  67               new OrPredicate(new CPUSpecificPredicate("i386.*",    new String[] { "sha" },  null),
  68                               new CPUSpecificPredicate("x86.*",     new String[] { "sha" },  null))))));
  69 
  70     public static final BooleanSupplier SHA256_INSTRUCTION_AVAILABLE
  71             = new OrPredicate(new CPUSpecificPredicate("aarch64.*", new String[] { "sha256"       }, null),
  72               new OrPredicate(new CPUSpecificPredicate("s390.*",    new String[] { "sha256"       }, null),
  73               new OrPredicate(new CPUSpecificPredicate("sparc.*",   new String[] { "sha256"       }, null),
  74               // x86 variants
  75               new OrPredicate(new CPUSpecificPredicate("amd64.*",   new String[] { "sha"          }, null),
  76               new OrPredicate(new CPUSpecificPredicate("i386.*",    new String[] { "sha"          }, null),
  77               new OrPredicate(new CPUSpecificPredicate("x86.*",     new String[] { "sha"          }, null),
  78               new OrPredicate(new CPUSpecificPredicate("amd64.*",   new String[] { "avx2", "bmi2" }, null),
  79                               new CPUSpecificPredicate("x86_64",    new String[] { "avx2", "bmi2" }, null))))))));
  80 
  81     public static final BooleanSupplier SHA512_INSTRUCTION_AVAILABLE
  82             = new OrPredicate(new CPUSpecificPredicate("aarch64.*", new String[] { "sha512"       }, null),
  83               new OrPredicate(new CPUSpecificPredicate("s390.*",    new String[] { "sha512"       }, null),
  84               new OrPredicate(new CPUSpecificPredicate("sparc.*",   new String[] { "sha512"       }, null),
  85               // x86 variants
  86               new OrPredicate(new CPUSpecificPredicate("amd64.*",   new String[] { "sha"          }, null),
  87               new OrPredicate(new CPUSpecificPredicate("i386.*",    new String[] { "sha"          }, null),
  88               new OrPredicate(new CPUSpecificPredicate("x86.*",     new String[] { "sha"          }, null),
  89               new OrPredicate(new CPUSpecificPredicate("amd64.*",   new String[] { "avx2", "bmi2" }, null),
  90                               new CPUSpecificPredicate("x86_64",    new String[] { "avx2", "bmi2" }, null))))))));
  91 
  92     public static final BooleanSupplier ANY_SHA_INSTRUCTION_AVAILABLE
  93             = new OrPredicate(IntrinsicPredicates.SHA1_INSTRUCTION_AVAILABLE,
  94                     new OrPredicate(
  95                             IntrinsicPredicates.SHA256_INSTRUCTION_AVAILABLE,
  96                             IntrinsicPredicates.SHA512_INSTRUCTION_AVAILABLE));
  97 
  98     public static final BooleanSupplier SHA1_INTRINSICS_AVAILABLE
  99             = new AndPredicate(new AndPredicate(
 100                     IntrinsicPredicates.SHA1_INSTRUCTION_AVAILABLE,
 101                     IntrinsicPredicates.COMPILABLE_BY_C2),
 102                 IntrinsicPredicates.booleanOptionValue("UseSHA1Intrinsics"));
 103 
 104     public static final BooleanSupplier SHA256_INTRINSICS_AVAILABLE
 105             = new AndPredicate(new AndPredicate(
 106                     IntrinsicPredicates.SHA256_INSTRUCTION_AVAILABLE,
 107                     IntrinsicPredicates.COMPILABLE_BY_C2),
 108                 IntrinsicPredicates.booleanOptionValue("UseSHA256Intrinsics"));
 109 
 110     public static final BooleanSupplier SHA512_INTRINSICS_AVAILABLE
 111             = new AndPredicate(new AndPredicate(
 112                     IntrinsicPredicates.SHA512_INSTRUCTION_AVAILABLE,
 113                     IntrinsicPredicates.COMPILABLE_BY_C2),
 114                 IntrinsicPredicates.booleanOptionValue("UseSHA512Intrinsics"));
 115 
 116     private static BooleanSupplier booleanOptionValue(String option) {
 117         return () -> IntrinsicPredicates.WHITE_BOX.getBooleanVMFlag(option);
 118     }
 119 
 120     private IntrinsicPredicates() {
 121     }
 122 }