1 /* 2 * Copyright (c) 2013, 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 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "memory/allocation.inline.hpp" 27 #include "opto/addnode.hpp" 28 #include "opto/cfgnode.hpp" 29 #include "opto/machnode.hpp" 30 #include "opto/matcher.hpp" 31 #include "opto/mathexactnode.hpp" 32 #include "opto/subnode.hpp" 33 34 template <typename OverflowOp> 35 class AddHelper { 36 public: 37 typedef typename OverflowOp::TypeClass TypeClass; 38 typedef typename TypeClass::NativeType NativeType; 39 40 static bool will_overflow(NativeType value1, NativeType value2) { 41 NativeType result = value1 + value2; 42 // Hacker's Delight 2-12 Overflow if both arguments have the opposite sign of the result 43 if (((value1 ^ result) & (value2 ^ result)) >= 0) { 44 return false; 45 } 46 return true; 47 } 48 49 static bool can_overflow(const Type* type1, const Type* type2) { 50 if (type1 == TypeClass::ZERO || type2 == TypeClass::ZERO) { 51 return false; 52 } 53 return true; 54 } 55 }; 56 57 template <typename OverflowOp> 58 class SubHelper { 59 public: 60 typedef typename OverflowOp::TypeClass TypeClass; 61 typedef typename TypeClass::NativeType NativeType; 62 63 static bool will_overflow(NativeType value1, NativeType value2) { 64 NativeType result = value1 - value2; 65 // hacker's delight 2-12 overflow iff the arguments have different signs and 66 // the sign of the result is different than the sign of arg1 67 if (((value1 ^ value2) & (value1 ^ result)) >= 0) { 68 return false; 69 } 70 return true; 71 } 72 73 static bool can_overflow(const Type* type1, const Type* type2) { 74 if (type2 == TypeClass::ZERO) { 75 return false; 76 } 77 return true; 78 } 79 }; 80 81 template <typename OverflowOp> 82 class MulHelper { 83 public: 84 typedef typename OverflowOp::TypeClass TypeClass; 85 86 static bool can_overflow(const Type* type1, const Type* type2) { 87 if (type1 == TypeClass::ZERO || type2 == TypeClass::ZERO) { 88 return false; 89 } else if (type1 == TypeClass::ONE || type2 == TypeClass::ONE) { 90 return false; 91 } 92 return true; 93 } 94 }; 95 96 bool OverflowAddINode::will_overflow(jint v1, jint v2) const { 97 return AddHelper<OverflowAddINode>::will_overflow(v1, v2); 98 } 99 100 bool OverflowSubINode::will_overflow(jint v1, jint v2) const { 101 return SubHelper<OverflowSubINode>::will_overflow(v1, v2); 102 } 103 104 bool OverflowMulINode::will_overflow(jint v1, jint v2) const { 105 jlong result = (jlong) v1 * (jlong) v2; 106 if ((jint) result == result) { 107 return false; 108 } 109 return true; 110 } 111 112 bool OverflowAddLNode::will_overflow(jlong v1, jlong v2) const { 113 return AddHelper<OverflowAddLNode>::will_overflow(v1, v2); 114 } 115 116 bool OverflowSubLNode::will_overflow(jlong v1, jlong v2) const { 117 return SubHelper<OverflowSubLNode>::will_overflow(v1, v2); 118 } 119 120 // this method is here to avoid undefined behaviour when doing -val and val is min_jlong 121 // -min_jlong overflows and goes back to being min_jlong 122 static jlong negate_jlong(jlong val) { 123 if (val == min_jlong) { 124 return min_jlong; 125 } 126 return -val; 127 } 128 129 bool OverflowMulLNode::will_overflow(jlong val1, jlong val2) const { 130 julong v1 = (julong) val1; 131 julong v2 = (julong) val2; 132 // the multiplication here is done as unsigned to avoid undefined behaviour which 133 // can be used by the compiler to assume that the check further down (result / val2 != val1) 134 // is always false and break the overflow check 135 julong tmp = v1 * v2; 136 jlong result = (jlong) tmp; 137 jlong ax = (val1 < 0 ? negate_jlong(val1) : val1); 138 jlong ay = (val2 < 0 ? negate_jlong(val2) : val2); 139 140 bool overflow = false; 141 if ((ax | ay) & CONST64(0xFFFFFFFF00000000)) { 142 // potential overflow if any bit in upper 32 bits are set 143 if ((val1 == min_jlong && val2 == -1) || (val2 == min_jlong && val1 == -1)) { 144 // -1 * Long.MIN_VALUE will overflow 145 overflow = true; 146 } else if (val2 != 0 && (result / val2 != val1)) { 147 overflow = true; 148 } 149 } 150 151 return overflow; 152 } 153 154 bool OverflowAddINode::can_overflow(const Type* t1, const Type* t2) const { 155 return AddHelper<OverflowAddINode>::can_overflow(t1, t2); 156 } 157 158 bool OverflowSubINode::can_overflow(const Type* t1, const Type* t2) const { 159 if (in(1) == in(2)) { 160 return false; 161 } 162 return SubHelper<OverflowSubINode>::can_overflow(t1, t2); 163 } 164 165 bool OverflowMulINode::can_overflow(const Type* t1, const Type* t2) const { 166 return MulHelper<OverflowMulINode>::can_overflow(t1, t2); 167 } 168 169 bool OverflowAddLNode::can_overflow(const Type* t1, const Type* t2) const { 170 return AddHelper<OverflowAddLNode>::can_overflow(t1, t2); 171 } 172 173 bool OverflowSubLNode::can_overflow(const Type* t1, const Type* t2) const { 174 if (in(1) == in(2)) { 175 return false; 176 } 177 return SubHelper<OverflowSubLNode>::can_overflow(t1, t2); 178 } 179 180 bool OverflowMulLNode::can_overflow(const Type* t1, const Type* t2) const { 181 return MulHelper<OverflowMulLNode>::can_overflow(t1, t2); 182 } 183 184 const Type* OverflowNode::sub(const Type* t1, const Type* t2) const { 185 fatal("sub() should not be called for '%s'", NodeClassNames[this->Opcode()]); 186 return TypeInt::CC; 187 } 188 189 template <typename OverflowOp> 190 struct IdealHelper { 191 typedef typename OverflowOp::TypeClass TypeClass; // TypeInt, TypeLong 192 typedef typename TypeClass::NativeType NativeType; 193 194 static Node* Ideal(const OverflowOp* node, PhaseGVN* phase, bool can_reshape) { 195 Node* arg1 = node->in(1); 196 Node* arg2 = node->in(2); 197 const Type* type1 = phase->type(arg1); 198 const Type* type2 = phase->type(arg2); 199 200 if (type1 == NULL || type2 == NULL) { 201 return NULL; 202 } 203 204 if (type1 != Type::TOP && type1->singleton() && 205 type2 != Type::TOP && type2->singleton()) { 206 NativeType val1 = TypeClass::as_self(type1)->get_con(); 207 NativeType val2 = TypeClass::as_self(type2)->get_con(); 208 if (node->will_overflow(val1, val2) == false) { 209 Node* con_result = ConINode::make(0); 210 return con_result; 211 } 212 return NULL; 213 } 214 return NULL; 215 } 216 217 static const Type* Value(const OverflowOp* node, PhaseTransform* phase) { 218 const Type *t1 = phase->type( node->in(1) ); 219 const Type *t2 = phase->type( node->in(2) ); 220 if( t1 == Type::TOP ) return Type::TOP; 221 if( t2 == Type::TOP ) return Type::TOP; 222 223 const TypeClass* i1 = TypeClass::as_self(t1); 224 const TypeClass* i2 = TypeClass::as_self(t2); 225 226 if (i1 == NULL || i2 == NULL) { 227 return TypeInt::CC; 228 } 229 230 if (t1->singleton() && t2->singleton()) { 231 NativeType val1 = i1->get_con(); 232 NativeType val2 = i2->get_con(); 233 if (node->will_overflow(val1, val2)) { 234 return TypeInt::CC; 235 } 236 return TypeInt::ZERO; 237 } else if (i1 != TypeClass::TYPE_DOMAIN && i2 != TypeClass::TYPE_DOMAIN) { 238 if (node->will_overflow(i1->_lo, i2->_lo)) { 239 return TypeInt::CC; 240 } else if (node->will_overflow(i1->_lo, i2->_hi)) { 241 return TypeInt::CC; 242 } else if (node->will_overflow(i1->_hi, i2->_lo)) { 243 return TypeInt::CC; 244 } else if (node->will_overflow(i1->_hi, i2->_hi)) { 245 return TypeInt::CC; 246 } 247 return TypeInt::ZERO; 248 } 249 250 if (!node->can_overflow(t1, t2)) { 251 return TypeInt::ZERO; 252 } 253 return TypeInt::CC; 254 } 255 }; 256 257 Node* OverflowINode::Ideal(PhaseGVN* phase, bool can_reshape) { 258 return IdealHelper<OverflowINode>::Ideal(this, phase, can_reshape); 259 } 260 261 Node* OverflowLNode::Ideal(PhaseGVN* phase, bool can_reshape) { 262 return IdealHelper<OverflowLNode>::Ideal(this, phase, can_reshape); 263 } 264 265 const Type* OverflowINode::Value(PhaseGVN* phase) const { 266 return IdealHelper<OverflowINode>::Value(this, phase); 267 } 268 269 const Type* OverflowLNode::Value(PhaseGVN* phase) const { 270 return IdealHelper<OverflowLNode>::Value(this, phase); 271 } 272