annotate src/hotspot/share/opto/mathexactnode.cpp @ 48520:4162f740b7bd

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