src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.replacements.amd64/src/org/graalvm/compiler/replacements/amd64/AMD64LongSubstitutions.java
changeset 58877 aec7bf35d6f5
equal deleted inserted replaced
58876:1a8d65e71a66 58877:aec7bf35d6f5
       
     1 /*
       
     2  * Copyright (c) 2019, 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.replacements.amd64;
       
    26 
       
    27 // JaCoCo Exclude
       
    28 
       
    29 import static org.graalvm.compiler.replacements.NodeIntrinsificationProvider.INJECTED_TARGET;
       
    30 
       
    31 import org.graalvm.compiler.api.replacements.ClassSubstitution;
       
    32 import org.graalvm.compiler.api.replacements.Fold;
       
    33 import org.graalvm.compiler.api.replacements.MethodSubstitution;
       
    34 import org.graalvm.compiler.core.common.SuppressFBWarnings;
       
    35 import org.graalvm.compiler.replacements.nodes.BitScanForwardNode;
       
    36 import org.graalvm.compiler.replacements.nodes.BitScanReverseNode;
       
    37 
       
    38 import jdk.vm.ci.amd64.AMD64;
       
    39 import jdk.vm.ci.code.TargetDescription;
       
    40 
       
    41 @ClassSubstitution(Long.class)
       
    42 public class AMD64LongSubstitutions {
       
    43 
       
    44     @Fold
       
    45     static boolean lzcnt(@Fold.InjectedParameter TargetDescription target) {
       
    46         AMD64 arch = (AMD64) target.arch;
       
    47         return arch.getFeatures().contains(AMD64.CPUFeature.LZCNT) && arch.getFlags().contains(AMD64.Flag.UseCountLeadingZerosInstruction);
       
    48     }
       
    49 
       
    50     @Fold
       
    51     static boolean tzcnt(@Fold.InjectedParameter TargetDescription target) {
       
    52         AMD64 arch = (AMD64) target.arch;
       
    53         return arch.getFeatures().contains(AMD64.CPUFeature.BMI1) && arch.getFlags().contains(AMD64.Flag.UseCountTrailingZerosInstruction);
       
    54     }
       
    55 
       
    56     @MethodSubstitution
       
    57     @SuppressFBWarnings(value = "NP_NULL_PARAM_DEREF_NONVIRTUAL", justification = "foldable method parameters are injected")
       
    58     public static int numberOfLeadingZeros(long i) {
       
    59         if (lzcnt(INJECTED_TARGET)) {
       
    60             return AMD64CountLeadingZerosNode.countLeadingZeros(i);
       
    61         }
       
    62         if (i == 0) {
       
    63             return 64;
       
    64         }
       
    65         return 63 - BitScanReverseNode.unsafeScan(i);
       
    66     }
       
    67 
       
    68     @MethodSubstitution
       
    69     @SuppressFBWarnings(value = "NP_NULL_PARAM_DEREF_NONVIRTUAL", justification = "foldable method parameters are injected")
       
    70     public static int numberOfTrailingZeros(long i) {
       
    71         if (tzcnt(INJECTED_TARGET)) {
       
    72             return AMD64CountTrailingZerosNode.countTrailingZeros(i);
       
    73         }
       
    74 
       
    75         if (i == 0) {
       
    76             return 64;
       
    77         }
       
    78         return BitScanForwardNode.unsafeScan(i);
       
    79     }
       
    80 }