aoqi@0: /* aoqi@0: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "memory/allocation.inline.hpp" aoqi@0: #include "opto/addnode.hpp" aoqi@0: #include "opto/cfgnode.hpp" aoqi@0: #include "opto/machnode.hpp" aoqi@0: #include "opto/matcher.hpp" aoqi@0: #include "opto/mathexactnode.hpp" aoqi@0: #include "opto/subnode.hpp" aoqi@0: aoqi@0: template aoqi@0: class AddHelper { aoqi@0: public: aoqi@0: typedef typename OverflowOp::TypeClass TypeClass; aoqi@0: typedef typename TypeClass::NativeType NativeType; aoqi@0: aoqi@0: static bool will_overflow(NativeType value1, NativeType value2) { aoqi@0: NativeType result = value1 + value2; aoqi@0: // Hacker's Delight 2-12 Overflow if both arguments have the opposite sign of the result aoqi@0: if (((value1 ^ result) & (value2 ^ result)) >= 0) { aoqi@0: return false; aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: static bool can_overflow(const Type* type1, const Type* type2) { aoqi@0: if (type1 == TypeClass::ZERO || type2 == TypeClass::ZERO) { aoqi@0: return false; aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: template aoqi@0: class SubHelper { aoqi@0: public: aoqi@0: typedef typename OverflowOp::TypeClass TypeClass; aoqi@0: typedef typename TypeClass::NativeType NativeType; aoqi@0: aoqi@0: static bool will_overflow(NativeType value1, NativeType value2) { aoqi@0: NativeType result = value1 - value2; aoqi@0: // hacker's delight 2-12 overflow iff the arguments have different signs and aoqi@0: // the sign of the result is different than the sign of arg1 aoqi@0: if (((value1 ^ value2) & (value1 ^ result)) >= 0) { aoqi@0: return false; aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: static bool can_overflow(const Type* type1, const Type* type2) { aoqi@0: if (type2 == TypeClass::ZERO) { aoqi@0: return false; aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: template aoqi@0: class MulHelper { aoqi@0: public: aoqi@0: typedef typename OverflowOp::TypeClass TypeClass; aoqi@0: aoqi@0: static bool can_overflow(const Type* type1, const Type* type2) { aoqi@0: if (type1 == TypeClass::ZERO || type2 == TypeClass::ZERO) { aoqi@0: return false; aoqi@0: } else if (type1 == TypeClass::ONE || type2 == TypeClass::ONE) { aoqi@0: return false; aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: bool OverflowAddINode::will_overflow(jint v1, jint v2) const { aoqi@0: return AddHelper::will_overflow(v1, v2); aoqi@0: } aoqi@0: aoqi@0: bool OverflowSubINode::will_overflow(jint v1, jint v2) const { aoqi@0: return SubHelper::will_overflow(v1, v2); aoqi@0: } aoqi@0: aoqi@0: bool OverflowMulINode::will_overflow(jint v1, jint v2) const { aoqi@0: jlong result = (jlong) v1 * (jlong) v2; aoqi@0: if ((jint) result == result) { aoqi@0: return false; aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: bool OverflowAddLNode::will_overflow(jlong v1, jlong v2) const { aoqi@0: return AddHelper::will_overflow(v1, v2); aoqi@0: } aoqi@0: aoqi@0: bool OverflowSubLNode::will_overflow(jlong v1, jlong v2) const { aoqi@0: return SubHelper::will_overflow(v1, v2); aoqi@0: } aoqi@0: aoqi@0: bool OverflowMulLNode::will_overflow(jlong val1, jlong val2) const { aoqi@0: jlong result = val1 * val2; aoqi@0: jlong ax = (val1 < 0 ? -val1 : val1); aoqi@0: jlong ay = (val2 < 0 ? -val2 : val2); aoqi@0: aoqi@0: bool overflow = false; aoqi@0: if ((ax | ay) & CONST64(0xFFFFFFFF00000000)) { aoqi@0: // potential overflow if any bit in upper 32 bits are set aoqi@0: if ((val1 == min_jlong && val2 == -1) || (val2 == min_jlong && val1 == -1)) { aoqi@0: // -1 * Long.MIN_VALUE will overflow aoqi@0: overflow = true; aoqi@0: } else if (val2 != 0 && (result / val2 != val1)) { aoqi@0: overflow = true; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return overflow; aoqi@0: } aoqi@0: aoqi@0: bool OverflowAddINode::can_overflow(const Type* t1, const Type* t2) const { aoqi@0: return AddHelper::can_overflow(t1, t2); aoqi@0: } aoqi@0: aoqi@0: bool OverflowSubINode::can_overflow(const Type* t1, const Type* t2) const { aoqi@0: if (in(1) == in(2)) { aoqi@0: return false; aoqi@0: } aoqi@0: return SubHelper::can_overflow(t1, t2); aoqi@0: } aoqi@0: aoqi@0: bool OverflowMulINode::can_overflow(const Type* t1, const Type* t2) const { aoqi@0: return MulHelper::can_overflow(t1, t2); aoqi@0: } aoqi@0: aoqi@0: bool OverflowAddLNode::can_overflow(const Type* t1, const Type* t2) const { aoqi@0: return AddHelper::can_overflow(t1, t2); aoqi@0: } aoqi@0: aoqi@0: bool OverflowSubLNode::can_overflow(const Type* t1, const Type* t2) const { aoqi@0: if (in(1) == in(2)) { aoqi@0: return false; aoqi@0: } aoqi@0: return SubHelper::can_overflow(t1, t2); aoqi@0: } aoqi@0: aoqi@0: bool OverflowMulLNode::can_overflow(const Type* t1, const Type* t2) const { aoqi@0: return MulHelper::can_overflow(t1, t2); aoqi@0: } aoqi@0: aoqi@0: const Type* OverflowNode::sub(const Type* t1, const Type* t2) const { aoqi@0: fatal(err_msg_res("sub() should not be called for '%s'", NodeClassNames[this->Opcode()])); aoqi@0: return TypeInt::CC; aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: struct IdealHelper { aoqi@0: typedef typename OverflowOp::TypeClass TypeClass; // TypeInt, TypeLong aoqi@0: typedef typename TypeClass::NativeType NativeType; aoqi@0: aoqi@0: static Node* Ideal(const OverflowOp* node, PhaseGVN* phase, bool can_reshape) { aoqi@0: Node* arg1 = node->in(1); aoqi@0: Node* arg2 = node->in(2); aoqi@0: const Type* type1 = phase->type(arg1); aoqi@0: const Type* type2 = phase->type(arg2); aoqi@0: aoqi@0: if (type1 == NULL || type2 == NULL) { aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: if (type1 != Type::TOP && type1->singleton() && aoqi@0: type2 != Type::TOP && type2->singleton()) { aoqi@0: NativeType val1 = TypeClass::as_self(type1)->get_con(); aoqi@0: NativeType val2 = TypeClass::as_self(type2)->get_con(); aoqi@0: if (node->will_overflow(val1, val2) == false) { aoqi@0: Node* con_result = ConINode::make(phase->C, 0); aoqi@0: return con_result; aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: static const Type* Value(const OverflowOp* node, PhaseTransform* phase) { aoqi@0: const Type *t1 = phase->type( node->in(1) ); aoqi@0: const Type *t2 = phase->type( node->in(2) ); aoqi@0: if( t1 == Type::TOP ) return Type::TOP; aoqi@0: if( t2 == Type::TOP ) return Type::TOP; aoqi@0: aoqi@0: const TypeClass* i1 = TypeClass::as_self(t1); aoqi@0: const TypeClass* i2 = TypeClass::as_self(t2); aoqi@0: aoqi@0: if (i1 == NULL || i2 == NULL) { aoqi@0: return TypeInt::CC; aoqi@0: } aoqi@0: aoqi@0: if (t1->singleton() && t2->singleton()) { aoqi@0: NativeType val1 = i1->get_con(); aoqi@0: NativeType val2 = i2->get_con(); aoqi@0: if (node->will_overflow(val1, val2)) { aoqi@0: return TypeInt::CC; aoqi@0: } aoqi@0: return TypeInt::ZERO; aoqi@0: } else if (i1 != TypeClass::TYPE_DOMAIN && i2 != TypeClass::TYPE_DOMAIN) { aoqi@0: if (node->will_overflow(i1->_lo, i2->_lo)) { aoqi@0: return TypeInt::CC; aoqi@0: } else if (node->will_overflow(i1->_lo, i2->_hi)) { aoqi@0: return TypeInt::CC; aoqi@0: } else if (node->will_overflow(i1->_hi, i2->_lo)) { aoqi@0: return TypeInt::CC; aoqi@0: } else if (node->will_overflow(i1->_hi, i2->_hi)) { aoqi@0: return TypeInt::CC; aoqi@0: } aoqi@0: return TypeInt::ZERO; aoqi@0: } aoqi@0: aoqi@0: if (!node->can_overflow(t1, t2)) { aoqi@0: return TypeInt::ZERO; aoqi@0: } aoqi@0: return TypeInt::CC; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: Node* OverflowINode::Ideal(PhaseGVN* phase, bool can_reshape) { aoqi@0: return IdealHelper::Ideal(this, phase, can_reshape); aoqi@0: } aoqi@0: aoqi@0: Node* OverflowLNode::Ideal(PhaseGVN* phase, bool can_reshape) { aoqi@0: return IdealHelper::Ideal(this, phase, can_reshape); aoqi@0: } aoqi@0: aoqi@0: const Type* OverflowINode::Value(PhaseTransform* phase) const { aoqi@0: return IdealHelper::Value(this, phase); aoqi@0: } aoqi@0: aoqi@0: const Type* OverflowLNode::Value(PhaseTransform* phase) const { aoqi@0: return IdealHelper::Value(this, phase); aoqi@0: } aoqi@0: