aboutsummaryrefslogtreecommitdiff
path: root/src/share/vm/opto/mathexactnode.cpp
blob: 00466ad3d50427a58b940b99c15082ed038f2d15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * 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"

template <typename OverflowOp>
class AddHelper {
public:
  typedef typename OverflowOp::TypeClass TypeClass;
  typedef typename TypeClass::NativeType NativeType;

  static bool will_overflow(NativeType value1, NativeType value2) {
    NativeType result = value1 + value2;
    // Hacker's Delight 2-12 Overflow if both arguments have the opposite sign of the result
    if (((value1 ^ result) & (value2 ^ result)) >= 0) {
      return false;
    }
    return true;
  }

  static bool can_overflow(const Type* type1, const Type* type2) {
    if (type1 == TypeClass::ZERO || type2 == TypeClass::ZERO) {
      return false;
    }
    return true;
  }
};

template <typename OverflowOp>
class SubHelper {
public:
  typedef typename OverflowOp::TypeClass TypeClass;
  typedef typename TypeClass::NativeType NativeType;

  static bool will_overflow(NativeType value1, NativeType value2) {
    NativeType result = value1 - value2;
    // 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 (((value1 ^ value2) & (value1 ^ result)) >= 0) {
      return false;
    }
    return true;
  }

  static bool can_overflow(const Type* type1, const Type* type2) {
    if (type2 == TypeClass::ZERO) {
      return false;
    }
    return true;
  }
};

template <typename OverflowOp>
class MulHelper {
public:
  typedef typename OverflowOp::TypeClass TypeClass;

  static bool can_overflow(const Type* type1, const Type* type2) {
    if (type1 == TypeClass::ZERO || type2 == TypeClass::ZERO) {
      return false;
    } else if (type1 == TypeClass::ONE || type2 == TypeClass::ONE) {
      return false;
    }
    return true;
  }
};

bool OverflowAddINode::will_overflow(jint v1, jint v2) const {
  return AddHelper<OverflowAddINode>::will_overflow(v1, v2);
}

bool OverflowSubINode::will_overflow(jint v1, jint v2) const {
  return SubHelper<OverflowSubINode>::will_overflow(v1, v2);
}

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 {
  return AddHelper<OverflowAddLNode>::will_overflow(v1, v2);
}

bool OverflowSubLNode::will_overflow(jlong v1, jlong v2) const {
  return SubHelper<OverflowSubLNode>::will_overflow(v1, v2);
}

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;
}

bool OverflowAddINode::can_overflow(const Type* t1, const Type* t2) const {
  return AddHelper<OverflowAddINode>::can_overflow(t1, t2);
}

bool OverflowSubINode::can_overflow(const Type* t1, const Type* t2) const {
  if (in(1) == in(2)) {
    return false;
  }
  return SubHelper<OverflowSubINode>::can_overflow(t1, t2);
}

bool OverflowMulINode::can_overflow(const Type* t1, const Type* t2) const {
  return MulHelper<OverflowMulINode>::can_overflow(t1, t2);
}

bool OverflowAddLNode::can_overflow(const Type* t1, const Type* t2) const {
  return AddHelper<OverflowAddLNode>::can_overflow(t1, t2);
}

bool OverflowSubLNode::can_overflow(const Type* t1, const Type* t2) const {
  if (in(1) == in(2)) {
    return false;
  }
  return SubHelper<OverflowSubLNode>::can_overflow(t1, t2);
}

bool OverflowMulLNode::can_overflow(const Type* t1, const Type* t2) const {
  return MulHelper<OverflowMulLNode>::can_overflow(t1, t2);
}

const Type* OverflowNode::sub(const Type* t1, const Type* t2) const {
  fatal(err_msg_res("sub() should not be called for '%s'", NodeClassNames[this->Opcode()]));
  return TypeInt::CC;
}

template <typename OverflowOp>
struct IdealHelper {
  typedef typename OverflowOp::TypeClass TypeClass; // TypeInt, TypeLong
  typedef typename TypeClass::NativeType NativeType;

  static Node* Ideal(const OverflowOp* node, PhaseGVN* phase, bool can_reshape) {
    Node* arg1 = node->in(1);
    Node* arg2 = node->in(2);
    const Type* type1 = phase->type(arg1);
    const Type* type2 = phase->type(arg2);

    if (type1 == NULL || type2 == NULL) {
      return NULL;
    }

    if (type1 != Type::TOP && type1->singleton() &&
        type2 != Type::TOP && type2->singleton()) {
      NativeType val1 = TypeClass::as_self(type1)->get_con();
      NativeType val2 = TypeClass::as_self(type2)->get_con();
      if (node->will_overflow(val1, val2) == false) {
        Node* con_result = ConINode::make(phase->C, 0);
        return con_result;
      }
      return NULL;
    }
    return NULL;
  }

  static const Type* Value(const OverflowOp* node, PhaseTransform* phase) {
    const Type *t1 = phase->type( node->in(1) );
    const Type *t2 = phase->type( node->in(2) );
    if( t1 == Type::TOP ) return Type::TOP;
    if( t2 == Type::TOP ) return Type::TOP;

    const TypeClass* i1 = TypeClass::as_self(t1);
    const TypeClass* i2 = TypeClass::as_self(t2);

    if (i1 == NULL || i2 == NULL) {
      return TypeInt::CC;
    }

    if (t1->singleton() && t2->singleton()) {
      NativeType val1 = i1->get_con();
      NativeType val2 = i2->get_con();
      if (node->will_overflow(val1, val2)) {
        return TypeInt::CC;
      }
      return TypeInt::ZERO;
    } else if (i1 != TypeClass::TYPE_DOMAIN && i2 != TypeClass::TYPE_DOMAIN) {
      if (node->will_overflow(i1->_lo, i2->_lo)) {
        return TypeInt::CC;
      } else if (node->will_overflow(i1->_lo, i2->_hi)) {
        return TypeInt::CC;
      } else if (node->will_overflow(i1->_hi, i2->_lo)) {
        return TypeInt::CC;
      } else if (node->will_overflow(i1->_hi, i2->_hi)) {
        return TypeInt::CC;
      }
      return TypeInt::ZERO;
    }

    if (!node->can_overflow(t1, t2)) {
      return TypeInt::ZERO;
    }
    return TypeInt::CC;
  }
};

Node* OverflowINode::Ideal(PhaseGVN* phase, bool can_reshape) {
  return IdealHelper<OverflowINode>::Ideal(this, phase, can_reshape);
}

Node* OverflowLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
  return IdealHelper<OverflowLNode>::Ideal(this, phase, can_reshape);
}

const Type* OverflowINode::Value(PhaseTransform* phase) const {
  return IdealHelper<OverflowINode>::Value(this, phase);
}

const Type* OverflowLNode::Value(PhaseTransform* phase) const {
  return IdealHelper<OverflowLNode>::Value(this, phase);
}