src/share/vm/opto/mathexactnode.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/opto/mathexactnode.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,257 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "memory/allocation.inline.hpp"
    1.30 +#include "opto/addnode.hpp"
    1.31 +#include "opto/cfgnode.hpp"
    1.32 +#include "opto/machnode.hpp"
    1.33 +#include "opto/matcher.hpp"
    1.34 +#include "opto/mathexactnode.hpp"
    1.35 +#include "opto/subnode.hpp"
    1.36 +
    1.37 +template <typename OverflowOp>
    1.38 +class AddHelper {
    1.39 +public:
    1.40 +  typedef typename OverflowOp::TypeClass TypeClass;
    1.41 +  typedef typename TypeClass::NativeType NativeType;
    1.42 +
    1.43 +  static bool will_overflow(NativeType value1, NativeType value2) {
    1.44 +    NativeType result = value1 + value2;
    1.45 +    // Hacker's Delight 2-12 Overflow if both arguments have the opposite sign of the result
    1.46 +    if (((value1 ^ result) & (value2 ^ result)) >= 0) {
    1.47 +      return false;
    1.48 +    }
    1.49 +    return true;
    1.50 +  }
    1.51 +
    1.52 +  static bool can_overflow(const Type* type1, const Type* type2) {
    1.53 +    if (type1 == TypeClass::ZERO || type2 == TypeClass::ZERO) {
    1.54 +      return false;
    1.55 +    }
    1.56 +    return true;
    1.57 +  }
    1.58 +};
    1.59 +
    1.60 +template <typename OverflowOp>
    1.61 +class SubHelper {
    1.62 +public:
    1.63 +  typedef typename OverflowOp::TypeClass TypeClass;
    1.64 +  typedef typename TypeClass::NativeType NativeType;
    1.65 +
    1.66 +  static bool will_overflow(NativeType value1, NativeType value2) {
    1.67 +    NativeType result = value1 - value2;
    1.68 +    // hacker's delight 2-12 overflow iff the arguments have different signs and
    1.69 +    // the sign of the result is different than the sign of arg1
    1.70 +    if (((value1 ^ value2) & (value1 ^ result)) >= 0) {
    1.71 +      return false;
    1.72 +    }
    1.73 +    return true;
    1.74 +  }
    1.75 +
    1.76 +  static bool can_overflow(const Type* type1, const Type* type2) {
    1.77 +    if (type2 == TypeClass::ZERO) {
    1.78 +      return false;
    1.79 +    }
    1.80 +    return true;
    1.81 +  }
    1.82 +};
    1.83 +
    1.84 +template <typename OverflowOp>
    1.85 +class MulHelper {
    1.86 +public:
    1.87 +  typedef typename OverflowOp::TypeClass TypeClass;
    1.88 +
    1.89 +  static bool can_overflow(const Type* type1, const Type* type2) {
    1.90 +    if (type1 == TypeClass::ZERO || type2 == TypeClass::ZERO) {
    1.91 +      return false;
    1.92 +    } else if (type1 == TypeClass::ONE || type2 == TypeClass::ONE) {
    1.93 +      return false;
    1.94 +    }
    1.95 +    return true;
    1.96 +  }
    1.97 +};
    1.98 +
    1.99 +bool OverflowAddINode::will_overflow(jint v1, jint v2) const {
   1.100 +  return AddHelper<OverflowAddINode>::will_overflow(v1, v2);
   1.101 +}
   1.102 +
   1.103 +bool OverflowSubINode::will_overflow(jint v1, jint v2) const {
   1.104 +  return SubHelper<OverflowSubINode>::will_overflow(v1, v2);
   1.105 +}
   1.106 +
   1.107 +bool OverflowMulINode::will_overflow(jint v1, jint v2) const {
   1.108 +    jlong result = (jlong) v1 * (jlong) v2;
   1.109 +    if ((jint) result == result) {
   1.110 +      return false;
   1.111 +    }
   1.112 +    return true;
   1.113 +}
   1.114 +
   1.115 +bool OverflowAddLNode::will_overflow(jlong v1, jlong v2) const {
   1.116 +  return AddHelper<OverflowAddLNode>::will_overflow(v1, v2);
   1.117 +}
   1.118 +
   1.119 +bool OverflowSubLNode::will_overflow(jlong v1, jlong v2) const {
   1.120 +  return SubHelper<OverflowSubLNode>::will_overflow(v1, v2);
   1.121 +}
   1.122 +
   1.123 +bool OverflowMulLNode::will_overflow(jlong val1, jlong val2) const {
   1.124 +    jlong result = val1 * val2;
   1.125 +    jlong ax = (val1 < 0 ? -val1 : val1);
   1.126 +    jlong ay = (val2 < 0 ? -val2 : val2);
   1.127 +
   1.128 +    bool overflow = false;
   1.129 +    if ((ax | ay) & CONST64(0xFFFFFFFF00000000)) {
   1.130 +      // potential overflow if any bit in upper 32 bits are set
   1.131 +      if ((val1 == min_jlong && val2 == -1) || (val2 == min_jlong && val1 == -1)) {
   1.132 +        // -1 * Long.MIN_VALUE will overflow
   1.133 +        overflow = true;
   1.134 +      } else if (val2 != 0 && (result / val2 != val1)) {
   1.135 +        overflow = true;
   1.136 +      }
   1.137 +    }
   1.138 +
   1.139 +    return overflow;
   1.140 +}
   1.141 +
   1.142 +bool OverflowAddINode::can_overflow(const Type* t1, const Type* t2) const {
   1.143 +  return AddHelper<OverflowAddINode>::can_overflow(t1, t2);
   1.144 +}
   1.145 +
   1.146 +bool OverflowSubINode::can_overflow(const Type* t1, const Type* t2) const {
   1.147 +  if (in(1) == in(2)) {
   1.148 +    return false;
   1.149 +  }
   1.150 +  return SubHelper<OverflowSubINode>::can_overflow(t1, t2);
   1.151 +}
   1.152 +
   1.153 +bool OverflowMulINode::can_overflow(const Type* t1, const Type* t2) const {
   1.154 +  return MulHelper<OverflowMulINode>::can_overflow(t1, t2);
   1.155 +}
   1.156 +
   1.157 +bool OverflowAddLNode::can_overflow(const Type* t1, const Type* t2) const {
   1.158 +  return AddHelper<OverflowAddLNode>::can_overflow(t1, t2);
   1.159 +}
   1.160 +
   1.161 +bool OverflowSubLNode::can_overflow(const Type* t1, const Type* t2) const {
   1.162 +  if (in(1) == in(2)) {
   1.163 +    return false;
   1.164 +  }
   1.165 +  return SubHelper<OverflowSubLNode>::can_overflow(t1, t2);
   1.166 +}
   1.167 +
   1.168 +bool OverflowMulLNode::can_overflow(const Type* t1, const Type* t2) const {
   1.169 +  return MulHelper<OverflowMulLNode>::can_overflow(t1, t2);
   1.170 +}
   1.171 +
   1.172 +const Type* OverflowNode::sub(const Type* t1, const Type* t2) const {
   1.173 +  fatal(err_msg_res("sub() should not be called for '%s'", NodeClassNames[this->Opcode()]));
   1.174 +  return TypeInt::CC;
   1.175 +}
   1.176 +
   1.177 +template <typename OverflowOp>
   1.178 +struct IdealHelper {
   1.179 +  typedef typename OverflowOp::TypeClass TypeClass; // TypeInt, TypeLong
   1.180 +  typedef typename TypeClass::NativeType NativeType;
   1.181 +
   1.182 +  static Node* Ideal(const OverflowOp* node, PhaseGVN* phase, bool can_reshape) {
   1.183 +    Node* arg1 = node->in(1);
   1.184 +    Node* arg2 = node->in(2);
   1.185 +    const Type* type1 = phase->type(arg1);
   1.186 +    const Type* type2 = phase->type(arg2);
   1.187 +
   1.188 +    if (type1 == NULL || type2 == NULL) {
   1.189 +      return NULL;
   1.190 +    }
   1.191 +
   1.192 +    if (type1 != Type::TOP && type1->singleton() &&
   1.193 +        type2 != Type::TOP && type2->singleton()) {
   1.194 +      NativeType val1 = TypeClass::as_self(type1)->get_con();
   1.195 +      NativeType val2 = TypeClass::as_self(type2)->get_con();
   1.196 +      if (node->will_overflow(val1, val2) == false) {
   1.197 +        Node* con_result = ConINode::make(phase->C, 0);
   1.198 +        return con_result;
   1.199 +      }
   1.200 +      return NULL;
   1.201 +    }
   1.202 +    return NULL;
   1.203 +  }
   1.204 +
   1.205 +  static const Type* Value(const OverflowOp* node, PhaseTransform* phase) {
   1.206 +    const Type *t1 = phase->type( node->in(1) );
   1.207 +    const Type *t2 = phase->type( node->in(2) );
   1.208 +    if( t1 == Type::TOP ) return Type::TOP;
   1.209 +    if( t2 == Type::TOP ) return Type::TOP;
   1.210 +
   1.211 +    const TypeClass* i1 = TypeClass::as_self(t1);
   1.212 +    const TypeClass* i2 = TypeClass::as_self(t2);
   1.213 +
   1.214 +    if (i1 == NULL || i2 == NULL) {
   1.215 +      return TypeInt::CC;
   1.216 +    }
   1.217 +
   1.218 +    if (t1->singleton() && t2->singleton()) {
   1.219 +      NativeType val1 = i1->get_con();
   1.220 +      NativeType val2 = i2->get_con();
   1.221 +      if (node->will_overflow(val1, val2)) {
   1.222 +        return TypeInt::CC;
   1.223 +      }
   1.224 +      return TypeInt::ZERO;
   1.225 +    } else if (i1 != TypeClass::TYPE_DOMAIN && i2 != TypeClass::TYPE_DOMAIN) {
   1.226 +      if (node->will_overflow(i1->_lo, i2->_lo)) {
   1.227 +        return TypeInt::CC;
   1.228 +      } else if (node->will_overflow(i1->_lo, i2->_hi)) {
   1.229 +        return TypeInt::CC;
   1.230 +      } else if (node->will_overflow(i1->_hi, i2->_lo)) {
   1.231 +        return TypeInt::CC;
   1.232 +      } else if (node->will_overflow(i1->_hi, i2->_hi)) {
   1.233 +        return TypeInt::CC;
   1.234 +      }
   1.235 +      return TypeInt::ZERO;
   1.236 +    }
   1.237 +
   1.238 +    if (!node->can_overflow(t1, t2)) {
   1.239 +      return TypeInt::ZERO;
   1.240 +    }
   1.241 +    return TypeInt::CC;
   1.242 +  }
   1.243 +};
   1.244 +
   1.245 +Node* OverflowINode::Ideal(PhaseGVN* phase, bool can_reshape) {
   1.246 +  return IdealHelper<OverflowINode>::Ideal(this, phase, can_reshape);
   1.247 +}
   1.248 +
   1.249 +Node* OverflowLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
   1.250 +  return IdealHelper<OverflowLNode>::Ideal(this, phase, can_reshape);
   1.251 +}
   1.252 +
   1.253 +const Type* OverflowINode::Value(PhaseTransform* phase) const {
   1.254 +  return IdealHelper<OverflowINode>::Value(this, phase);
   1.255 +}
   1.256 +
   1.257 +const Type* OverflowLNode::Value(PhaseTransform* phase) const {
   1.258 +  return IdealHelper<OverflowLNode>::Value(this, phase);
   1.259 +}
   1.260 +

mercurial