src/share/vm/opto/mathexactnode.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "memory/allocation.inline.hpp"
aoqi@0 27 #include "opto/addnode.hpp"
aoqi@0 28 #include "opto/cfgnode.hpp"
aoqi@0 29 #include "opto/machnode.hpp"
aoqi@0 30 #include "opto/matcher.hpp"
aoqi@0 31 #include "opto/mathexactnode.hpp"
aoqi@0 32 #include "opto/subnode.hpp"
aoqi@0 33
aoqi@0 34 template <typename OverflowOp>
aoqi@0 35 class AddHelper {
aoqi@0 36 public:
aoqi@0 37 typedef typename OverflowOp::TypeClass TypeClass;
aoqi@0 38 typedef typename TypeClass::NativeType NativeType;
aoqi@0 39
aoqi@0 40 static bool will_overflow(NativeType value1, NativeType value2) {
aoqi@0 41 NativeType result = value1 + value2;
aoqi@0 42 // Hacker's Delight 2-12 Overflow if both arguments have the opposite sign of the result
aoqi@0 43 if (((value1 ^ result) & (value2 ^ result)) >= 0) {
aoqi@0 44 return false;
aoqi@0 45 }
aoqi@0 46 return true;
aoqi@0 47 }
aoqi@0 48
aoqi@0 49 static bool can_overflow(const Type* type1, const Type* type2) {
aoqi@0 50 if (type1 == TypeClass::ZERO || type2 == TypeClass::ZERO) {
aoqi@0 51 return false;
aoqi@0 52 }
aoqi@0 53 return true;
aoqi@0 54 }
aoqi@0 55 };
aoqi@0 56
aoqi@0 57 template <typename OverflowOp>
aoqi@0 58 class SubHelper {
aoqi@0 59 public:
aoqi@0 60 typedef typename OverflowOp::TypeClass TypeClass;
aoqi@0 61 typedef typename TypeClass::NativeType NativeType;
aoqi@0 62
aoqi@0 63 static bool will_overflow(NativeType value1, NativeType value2) {
aoqi@0 64 NativeType result = value1 - value2;
aoqi@0 65 // hacker's delight 2-12 overflow iff the arguments have different signs and
aoqi@0 66 // the sign of the result is different than the sign of arg1
aoqi@0 67 if (((value1 ^ value2) & (value1 ^ result)) >= 0) {
aoqi@0 68 return false;
aoqi@0 69 }
aoqi@0 70 return true;
aoqi@0 71 }
aoqi@0 72
aoqi@0 73 static bool can_overflow(const Type* type1, const Type* type2) {
aoqi@0 74 if (type2 == TypeClass::ZERO) {
aoqi@0 75 return false;
aoqi@0 76 }
aoqi@0 77 return true;
aoqi@0 78 }
aoqi@0 79 };
aoqi@0 80
aoqi@0 81 template <typename OverflowOp>
aoqi@0 82 class MulHelper {
aoqi@0 83 public:
aoqi@0 84 typedef typename OverflowOp::TypeClass TypeClass;
aoqi@0 85
aoqi@0 86 static bool can_overflow(const Type* type1, const Type* type2) {
aoqi@0 87 if (type1 == TypeClass::ZERO || type2 == TypeClass::ZERO) {
aoqi@0 88 return false;
aoqi@0 89 } else if (type1 == TypeClass::ONE || type2 == TypeClass::ONE) {
aoqi@0 90 return false;
aoqi@0 91 }
aoqi@0 92 return true;
aoqi@0 93 }
aoqi@0 94 };
aoqi@0 95
aoqi@0 96 bool OverflowAddINode::will_overflow(jint v1, jint v2) const {
aoqi@0 97 return AddHelper<OverflowAddINode>::will_overflow(v1, v2);
aoqi@0 98 }
aoqi@0 99
aoqi@0 100 bool OverflowSubINode::will_overflow(jint v1, jint v2) const {
aoqi@0 101 return SubHelper<OverflowSubINode>::will_overflow(v1, v2);
aoqi@0 102 }
aoqi@0 103
aoqi@0 104 bool OverflowMulINode::will_overflow(jint v1, jint v2) const {
aoqi@0 105 jlong result = (jlong) v1 * (jlong) v2;
aoqi@0 106 if ((jint) result == result) {
aoqi@0 107 return false;
aoqi@0 108 }
aoqi@0 109 return true;
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 bool OverflowAddLNode::will_overflow(jlong v1, jlong v2) const {
aoqi@0 113 return AddHelper<OverflowAddLNode>::will_overflow(v1, v2);
aoqi@0 114 }
aoqi@0 115
aoqi@0 116 bool OverflowSubLNode::will_overflow(jlong v1, jlong v2) const {
aoqi@0 117 return SubHelper<OverflowSubLNode>::will_overflow(v1, v2);
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 bool OverflowMulLNode::will_overflow(jlong val1, jlong val2) const {
aoqi@0 121 jlong result = val1 * val2;
aoqi@0 122 jlong ax = (val1 < 0 ? -val1 : val1);
aoqi@0 123 jlong ay = (val2 < 0 ? -val2 : val2);
aoqi@0 124
aoqi@0 125 bool overflow = false;
aoqi@0 126 if ((ax | ay) & CONST64(0xFFFFFFFF00000000)) {
aoqi@0 127 // potential overflow if any bit in upper 32 bits are set
aoqi@0 128 if ((val1 == min_jlong && val2 == -1) || (val2 == min_jlong && val1 == -1)) {
aoqi@0 129 // -1 * Long.MIN_VALUE will overflow
aoqi@0 130 overflow = true;
aoqi@0 131 } else if (val2 != 0 && (result / val2 != val1)) {
aoqi@0 132 overflow = true;
aoqi@0 133 }
aoqi@0 134 }
aoqi@0 135
aoqi@0 136 return overflow;
aoqi@0 137 }
aoqi@0 138
aoqi@0 139 bool OverflowAddINode::can_overflow(const Type* t1, const Type* t2) const {
aoqi@0 140 return AddHelper<OverflowAddINode>::can_overflow(t1, t2);
aoqi@0 141 }
aoqi@0 142
aoqi@0 143 bool OverflowSubINode::can_overflow(const Type* t1, const Type* t2) const {
aoqi@0 144 if (in(1) == in(2)) {
aoqi@0 145 return false;
aoqi@0 146 }
aoqi@0 147 return SubHelper<OverflowSubINode>::can_overflow(t1, t2);
aoqi@0 148 }
aoqi@0 149
aoqi@0 150 bool OverflowMulINode::can_overflow(const Type* t1, const Type* t2) const {
aoqi@0 151 return MulHelper<OverflowMulINode>::can_overflow(t1, t2);
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 bool OverflowAddLNode::can_overflow(const Type* t1, const Type* t2) const {
aoqi@0 155 return AddHelper<OverflowAddLNode>::can_overflow(t1, t2);
aoqi@0 156 }
aoqi@0 157
aoqi@0 158 bool OverflowSubLNode::can_overflow(const Type* t1, const Type* t2) const {
aoqi@0 159 if (in(1) == in(2)) {
aoqi@0 160 return false;
aoqi@0 161 }
aoqi@0 162 return SubHelper<OverflowSubLNode>::can_overflow(t1, t2);
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 bool OverflowMulLNode::can_overflow(const Type* t1, const Type* t2) const {
aoqi@0 166 return MulHelper<OverflowMulLNode>::can_overflow(t1, t2);
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 const Type* OverflowNode::sub(const Type* t1, const Type* t2) const {
aoqi@0 170 fatal(err_msg_res("sub() should not be called for '%s'", NodeClassNames[this->Opcode()]));
aoqi@0 171 return TypeInt::CC;
aoqi@0 172 }
aoqi@0 173
aoqi@0 174 template <typename OverflowOp>
aoqi@0 175 struct IdealHelper {
aoqi@0 176 typedef typename OverflowOp::TypeClass TypeClass; // TypeInt, TypeLong
aoqi@0 177 typedef typename TypeClass::NativeType NativeType;
aoqi@0 178
aoqi@0 179 static Node* Ideal(const OverflowOp* node, PhaseGVN* phase, bool can_reshape) {
aoqi@0 180 Node* arg1 = node->in(1);
aoqi@0 181 Node* arg2 = node->in(2);
aoqi@0 182 const Type* type1 = phase->type(arg1);
aoqi@0 183 const Type* type2 = phase->type(arg2);
aoqi@0 184
aoqi@0 185 if (type1 == NULL || type2 == NULL) {
aoqi@0 186 return NULL;
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 if (type1 != Type::TOP && type1->singleton() &&
aoqi@0 190 type2 != Type::TOP && type2->singleton()) {
aoqi@0 191 NativeType val1 = TypeClass::as_self(type1)->get_con();
aoqi@0 192 NativeType val2 = TypeClass::as_self(type2)->get_con();
aoqi@0 193 if (node->will_overflow(val1, val2) == false) {
aoqi@0 194 Node* con_result = ConINode::make(phase->C, 0);
aoqi@0 195 return con_result;
aoqi@0 196 }
aoqi@0 197 return NULL;
aoqi@0 198 }
aoqi@0 199 return NULL;
aoqi@0 200 }
aoqi@0 201
aoqi@0 202 static const Type* Value(const OverflowOp* node, PhaseTransform* phase) {
aoqi@0 203 const Type *t1 = phase->type( node->in(1) );
aoqi@0 204 const Type *t2 = phase->type( node->in(2) );
aoqi@0 205 if( t1 == Type::TOP ) return Type::TOP;
aoqi@0 206 if( t2 == Type::TOP ) return Type::TOP;
aoqi@0 207
aoqi@0 208 const TypeClass* i1 = TypeClass::as_self(t1);
aoqi@0 209 const TypeClass* i2 = TypeClass::as_self(t2);
aoqi@0 210
aoqi@0 211 if (i1 == NULL || i2 == NULL) {
aoqi@0 212 return TypeInt::CC;
aoqi@0 213 }
aoqi@0 214
aoqi@0 215 if (t1->singleton() && t2->singleton()) {
aoqi@0 216 NativeType val1 = i1->get_con();
aoqi@0 217 NativeType val2 = i2->get_con();
aoqi@0 218 if (node->will_overflow(val1, val2)) {
aoqi@0 219 return TypeInt::CC;
aoqi@0 220 }
aoqi@0 221 return TypeInt::ZERO;
aoqi@0 222 } else if (i1 != TypeClass::TYPE_DOMAIN && i2 != TypeClass::TYPE_DOMAIN) {
aoqi@0 223 if (node->will_overflow(i1->_lo, i2->_lo)) {
aoqi@0 224 return TypeInt::CC;
aoqi@0 225 } else if (node->will_overflow(i1->_lo, i2->_hi)) {
aoqi@0 226 return TypeInt::CC;
aoqi@0 227 } else if (node->will_overflow(i1->_hi, i2->_lo)) {
aoqi@0 228 return TypeInt::CC;
aoqi@0 229 } else if (node->will_overflow(i1->_hi, i2->_hi)) {
aoqi@0 230 return TypeInt::CC;
aoqi@0 231 }
aoqi@0 232 return TypeInt::ZERO;
aoqi@0 233 }
aoqi@0 234
aoqi@0 235 if (!node->can_overflow(t1, t2)) {
aoqi@0 236 return TypeInt::ZERO;
aoqi@0 237 }
aoqi@0 238 return TypeInt::CC;
aoqi@0 239 }
aoqi@0 240 };
aoqi@0 241
aoqi@0 242 Node* OverflowINode::Ideal(PhaseGVN* phase, bool can_reshape) {
aoqi@0 243 return IdealHelper<OverflowINode>::Ideal(this, phase, can_reshape);
aoqi@0 244 }
aoqi@0 245
aoqi@0 246 Node* OverflowLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
aoqi@0 247 return IdealHelper<OverflowLNode>::Ideal(this, phase, can_reshape);
aoqi@0 248 }
aoqi@0 249
aoqi@0 250 const Type* OverflowINode::Value(PhaseTransform* phase) const {
aoqi@0 251 return IdealHelper<OverflowINode>::Value(this, phase);
aoqi@0 252 }
aoqi@0 253
aoqi@0 254 const Type* OverflowLNode::Value(PhaseTransform* phase) const {
aoqi@0 255 return IdealHelper<OverflowLNode>::Value(this, phase);
aoqi@0 256 }
aoqi@0 257

mercurial