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