< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.nodes/src/org/graalvm/compiler/nodes/calc/CompareNode.java

Print this page
rev 52509 : [mq]: graal
   1 /*
   2  * Copyright (c) 2011, 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  */


 119             LogicNode constantCondition = tryConstantFold(condition, forX, forY, constantReflection, unorderedIsTrue);
 120             if (constantCondition != null) {
 121                 return constantCondition;
 122             }
 123             LogicNode result;
 124             if (forX.isConstant()) {
 125                 if ((result = canonicalizeSymmetricConstant(constantReflection, metaAccess, options, smallestCompareWidth, condition, forX.asConstant(), forY, true, unorderedIsTrue, view)) != null) {
 126                     return result;
 127                 }
 128             } else if (forY.isConstant()) {
 129                 if ((result = canonicalizeSymmetricConstant(constantReflection, metaAccess, options, smallestCompareWidth, condition, forY.asConstant(), forX, false, unorderedIsTrue, view)) != null) {
 130                     return result;
 131                 }
 132             } else if (forX instanceof ConvertNode && forY instanceof ConvertNode) {
 133                 ConvertNode convertX = (ConvertNode) forX;
 134                 ConvertNode convertY = (ConvertNode) forY;
 135                 if (convertX.preservesOrder(condition) && convertY.preservesOrder(condition) && convertX.getValue().stamp(view).isCompatible(convertY.getValue().stamp(view))) {
 136                     boolean supported = true;
 137                     if (convertX.getValue().stamp(view) instanceof IntegerStamp) {
 138                         IntegerStamp intStamp = (IntegerStamp) convertX.getValue().stamp(view);
 139                         supported = smallestCompareWidth != null && intStamp.getBits() >= smallestCompareWidth;

 140                     }
 141 
 142                     if (supported) {
 143                         boolean multiUsage = (convertX.asNode().hasMoreThanOneUsage() || convertY.asNode().hasMoreThanOneUsage());
 144                         if ((forX instanceof ZeroExtendNode || forX instanceof SignExtendNode) && multiUsage) {
 145                             // Do not perform for zero or sign extend if there are multiple usages
 146                             // of the value.





















 147                             return null;
 148                         }

 149                         return duplicateModified(convertX.getValue(), convertY.getValue(), unorderedIsTrue, view);
 150                     }
 151                 }
 152             }
 153             return null;
 154         }
 155 
 156         protected LogicNode canonicalizeSymmetricConstant(ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, Integer smallestCompareWidth,
 157                         CanonicalCondition condition, Constant constant, ValueNode nonConstant, boolean mirrored, boolean unorderedIsTrue, NodeView view) {
 158             if (nonConstant instanceof ConditionalNode) {
 159                 Condition realCondition = condition.asCondition();
 160                 if (mirrored) {
 161                     realCondition = realCondition.mirror();
 162                 }
 163                 return optimizeConditional(constant, (ConditionalNode) nonConstant, constantReflection, realCondition, unorderedIsTrue);
 164             } else if (nonConstant instanceof NormalizeCompareNode) {
 165                 return optimizeNormalizeCompare(constantReflection, metaAccess, options, smallestCompareWidth, constant, (NormalizeCompareNode) nonConstant, mirrored, view);
 166             } else if (nonConstant instanceof ConvertNode) {
 167                 ConvertNode convert = (ConvertNode) nonConstant;
 168                 boolean multiUsage = (convert.asNode().hasMoreThanOneUsage() && convert.getValue().hasExactlyOneUsage());
 169                 if ((convert instanceof ZeroExtendNode || convert instanceof SignExtendNode) && multiUsage) {
 170                     // Do not perform for zero or sign extend if it could introduce
 171                     // new live values.
 172                     return null;
 173                 }
 174 
 175                 boolean supported = true;
 176                 if (convert.getValue().stamp(view) instanceof IntegerStamp) {
 177                     IntegerStamp intStamp = (IntegerStamp) convert.getValue().stamp(view);
 178                     supported = smallestCompareWidth != null && intStamp.getBits() > smallestCompareWidth;
 179                 }
 180 
 181                 if (supported) {
 182                     ConstantNode newConstant = canonicalConvertConstant(constantReflection, metaAccess, options, condition, convert, constant, view);
 183                     if (newConstant != null) {
 184                         if (mirrored) {
 185                             return duplicateModified(newConstant, convert.getValue(), unorderedIsTrue, view);
 186                         } else {
 187                             return duplicateModified(convert.getValue(), newConstant, unorderedIsTrue, view);
 188                         }
 189                     }
 190                 }
 191             }
 192 
 193             return null;
 194         }
 195 
 196         private static ConstantNode canonicalConvertConstant(ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, CanonicalCondition condition,
 197                         ConvertNode convert, Constant constant, NodeView view) {
 198             if (convert.preservesOrder(condition, constant, constantReflection)) {


   1 /*
   2  * Copyright (c) 2011, 2018, 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  */


 119             LogicNode constantCondition = tryConstantFold(condition, forX, forY, constantReflection, unorderedIsTrue);
 120             if (constantCondition != null) {
 121                 return constantCondition;
 122             }
 123             LogicNode result;
 124             if (forX.isConstant()) {
 125                 if ((result = canonicalizeSymmetricConstant(constantReflection, metaAccess, options, smallestCompareWidth, condition, forX.asConstant(), forY, true, unorderedIsTrue, view)) != null) {
 126                     return result;
 127                 }
 128             } else if (forY.isConstant()) {
 129                 if ((result = canonicalizeSymmetricConstant(constantReflection, metaAccess, options, smallestCompareWidth, condition, forY.asConstant(), forX, false, unorderedIsTrue, view)) != null) {
 130                     return result;
 131                 }
 132             } else if (forX instanceof ConvertNode && forY instanceof ConvertNode) {
 133                 ConvertNode convertX = (ConvertNode) forX;
 134                 ConvertNode convertY = (ConvertNode) forY;
 135                 if (convertX.preservesOrder(condition) && convertY.preservesOrder(condition) && convertX.getValue().stamp(view).isCompatible(convertY.getValue().stamp(view))) {
 136                     boolean supported = true;
 137                     if (convertX.getValue().stamp(view) instanceof IntegerStamp) {
 138                         IntegerStamp intStamp = (IntegerStamp) convertX.getValue().stamp(view);
 139                         boolean isConversionCompatible = convertX.getClass() == convertY.getClass();
 140                         supported = smallestCompareWidth != null && intStamp.getBits() >= smallestCompareWidth && isConversionCompatible;
 141                     }
 142 
 143                     if (supported) {
 144 
 145                         ValueNode xValue = convertX.getValue();
 146                         ValueNode yValue = convertY.getValue();
 147 
 148                         if (forX instanceof ZeroExtendNode || forX instanceof SignExtendNode) {
 149 
 150                             int introducedUsages = 0;
 151                             int eliminatedNodes = 0;
 152 
 153                             if (convertX.asNode().hasExactlyOneUsage()) {
 154                                 eliminatedNodes++;
 155                             } else if (xValue.hasExactlyOneUsage()) {
 156                                 introducedUsages++;
 157                             }
 158 
 159                             if (convertY.asNode().hasExactlyOneUsage()) {
 160                                 eliminatedNodes++;
 161                             } else if (yValue.hasExactlyOneUsage()) {
 162                                 introducedUsages++;
 163                             }
 164 
 165                             if (introducedUsages > eliminatedNodes) {
 166                                 // Only perform the optimization if there is
 167                                 // a good trade-off between introduced new usages and
 168                                 // eliminated nodes.
 169                                 return null;
 170                             }
 171                         }
 172                         return duplicateModified(convertX.getValue(), convertY.getValue(), unorderedIsTrue, view);
 173                     }
 174                 }
 175             }
 176             return null;
 177         }
 178 
 179         protected LogicNode canonicalizeSymmetricConstant(ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, Integer smallestCompareWidth,
 180                         CanonicalCondition condition, Constant constant, ValueNode nonConstant, boolean mirrored, boolean unorderedIsTrue, NodeView view) {
 181             if (nonConstant instanceof ConditionalNode) {
 182                 Condition realCondition = condition.asCondition();
 183                 if (mirrored) {
 184                     realCondition = realCondition.mirror();
 185                 }
 186                 return optimizeConditional(constant, (ConditionalNode) nonConstant, constantReflection, realCondition, unorderedIsTrue);
 187             } else if (nonConstant instanceof NormalizeCompareNode) {
 188                 return optimizeNormalizeCompare(constantReflection, metaAccess, options, smallestCompareWidth, constant, (NormalizeCompareNode) nonConstant, mirrored, view);
 189             } else if (nonConstant instanceof ConvertNode) {
 190                 ConvertNode convert = (ConvertNode) nonConstant;
 191                 boolean multiUsage = (convert.asNode().hasMoreThanOneUsage() && convert.getValue().hasExactlyOneUsage());
 192                 if ((convert instanceof ZeroExtendNode || convert instanceof SignExtendNode) && multiUsage) {
 193                     // Do not perform for zero or sign extend if it could introduce
 194                     // new live values.
 195                     return null;
 196                 }
 197 
 198                 boolean supported = true;
 199                 if (convert.getValue().stamp(view) instanceof IntegerStamp) {
 200                     IntegerStamp intStamp = (IntegerStamp) convert.getValue().stamp(view);
 201                     supported = smallestCompareWidth != null && intStamp.getBits() >= smallestCompareWidth;
 202                 }
 203 
 204                 if (supported) {
 205                     ConstantNode newConstant = canonicalConvertConstant(constantReflection, metaAccess, options, condition, convert, constant, view);
 206                     if (newConstant != null) {
 207                         if (mirrored) {
 208                             return duplicateModified(newConstant, convert.getValue(), unorderedIsTrue, view);
 209                         } else {
 210                             return duplicateModified(convert.getValue(), newConstant, unorderedIsTrue, view);
 211                         }
 212                     }
 213                 }
 214             }
 215 
 216             return null;
 217         }
 218 
 219         private static ConstantNode canonicalConvertConstant(ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, CanonicalCondition condition,
 220                         ConvertNode convert, Constant constant, NodeView view) {
 221             if (convert.preservesOrder(condition, constant, constantReflection)) {


< prev index next >