1/*
2 * Copyright (c) 2014, 2019, 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 "opto/addnode.hpp"
27#include "opto/castnode.hpp"
28#include "opto/convertnode.hpp"
29#include "opto/matcher.hpp"
30#include "opto/phaseX.hpp"
31#include "opto/subnode.hpp"
32#include "runtime/sharedRuntime.hpp"
33
34//=============================================================================
35//------------------------------Identity---------------------------------------
36Node* Conv2BNode::Identity(PhaseGVN* phase) {
37 const Type *t = phase->type( in(1) );
38 if( t == Type::TOP ) return in(1);
39 if( t == TypeInt::ZERO ) return in(1);
40 if( t == TypeInt::ONE ) return in(1);
41 if( t == TypeInt::BOOL ) return in(1);
42 return this;
43}
44
45//------------------------------Value------------------------------------------
46const Type* Conv2BNode::Value(PhaseGVN* phase) const {
47 const Type *t = phase->type( in(1) );
48 if( t == Type::TOP ) return Type::TOP;
49 if( t == TypeInt::ZERO ) return TypeInt::ZERO;
50 if( t == TypePtr::NULL_PTR ) return TypeInt::ZERO;
51 const TypePtr *tp = t->isa_ptr();
52 if( tp != NULL ) {
53 if( tp->ptr() == TypePtr::AnyNull ) return Type::TOP;
54 if( tp->ptr() == TypePtr::Constant) return TypeInt::ONE;
55 if (tp->ptr() == TypePtr::NotNull) return TypeInt::ONE;
56 return TypeInt::BOOL;
57 }
58 if (t->base() != Type::Int) return TypeInt::BOOL;
59 const TypeInt *ti = t->is_int();
60 if( ti->_hi < 0 || ti->_lo > 0 ) return TypeInt::ONE;
61 return TypeInt::BOOL;
62}
63
64
65// The conversions operations are all Alpha sorted. Please keep it that way!
66//=============================================================================
67//------------------------------Value------------------------------------------
68const Type* ConvD2FNode::Value(PhaseGVN* phase) const {
69 const Type *t = phase->type( in(1) );
70 if( t == Type::TOP ) return Type::TOP;
71 if( t == Type::DOUBLE ) return Type::FLOAT;
72 const TypeD *td = t->is_double_constant();
73 return TypeF::make( (float)td->getd() );
74}
75
76//------------------------------Ideal------------------------------------------
77// If we see pattern ConvF2D SomeDoubleOp ConvD2F, do operation as float.
78Node *ConvD2FNode::Ideal(PhaseGVN *phase, bool can_reshape) {
79 if ( in(1)->Opcode() == Op_SqrtD ) {
80 Node* sqrtd = in(1);
81 if ( sqrtd->in(1)->Opcode() == Op_ConvF2D ) {
82 if ( Matcher::match_rule_supported(Op_SqrtF) ) {
83 Node* convf2d = sqrtd->in(1);
84 return new SqrtFNode(phase->C, sqrtd->in(0), convf2d->in(1));
85 }
86 }
87 }
88 return NULL;
89}
90
91//------------------------------Identity---------------------------------------
92// Float's can be converted to doubles with no loss of bits. Hence
93// converting a float to a double and back to a float is a NOP.
94Node* ConvD2FNode::Identity(PhaseGVN* phase) {
95 return (in(1)->Opcode() == Op_ConvF2D) ? in(1)->in(1) : this;
96}
97
98//=============================================================================
99//------------------------------Value------------------------------------------
100const Type* ConvD2INode::Value(PhaseGVN* phase) const {
101 const Type *t = phase->type( in(1) );
102 if( t == Type::TOP ) return Type::TOP;
103 if( t == Type::DOUBLE ) return TypeInt::INT;
104 const TypeD *td = t->is_double_constant();
105 return TypeInt::make( SharedRuntime::d2i( td->getd() ) );
106}
107
108//------------------------------Ideal------------------------------------------
109// If converting to an int type, skip any rounding nodes
110Node *ConvD2INode::Ideal(PhaseGVN *phase, bool can_reshape) {
111 if( in(1)->Opcode() == Op_RoundDouble )
112 set_req(1,in(1)->in(1));
113 return NULL;
114}
115
116//------------------------------Identity---------------------------------------
117// Int's can be converted to doubles with no loss of bits. Hence
118// converting an integer to a double and back to an integer is a NOP.
119Node* ConvD2INode::Identity(PhaseGVN* phase) {
120 return (in(1)->Opcode() == Op_ConvI2D) ? in(1)->in(1) : this;
121}
122
123//=============================================================================
124//------------------------------Value------------------------------------------
125const Type* ConvD2LNode::Value(PhaseGVN* phase) const {
126 const Type *t = phase->type( in(1) );
127 if( t == Type::TOP ) return Type::TOP;
128 if( t == Type::DOUBLE ) return TypeLong::LONG;
129 const TypeD *td = t->is_double_constant();
130 return TypeLong::make( SharedRuntime::d2l( td->getd() ) );
131}
132
133//------------------------------Identity---------------------------------------
134Node* ConvD2LNode::Identity(PhaseGVN* phase) {
135 // Remove ConvD2L->ConvL2D->ConvD2L sequences.
136 if( in(1) ->Opcode() == Op_ConvL2D &&
137 in(1)->in(1)->Opcode() == Op_ConvD2L )
138 return in(1)->in(1);
139 return this;
140}
141
142//------------------------------Ideal------------------------------------------
143// If converting to an int type, skip any rounding nodes
144Node *ConvD2LNode::Ideal(PhaseGVN *phase, bool can_reshape) {
145 if( in(1)->Opcode() == Op_RoundDouble )
146 set_req(1,in(1)->in(1));
147 return NULL;
148}
149
150//=============================================================================
151//------------------------------Value------------------------------------------
152const Type* ConvF2DNode::Value(PhaseGVN* phase) const {
153 const Type *t = phase->type( in(1) );
154 if( t == Type::TOP ) return Type::TOP;
155 if( t == Type::FLOAT ) return Type::DOUBLE;
156 const TypeF *tf = t->is_float_constant();
157 return TypeD::make( (double)tf->getf() );
158}
159
160//=============================================================================
161//------------------------------Value------------------------------------------
162const Type* ConvF2INode::Value(PhaseGVN* phase) const {
163 const Type *t = phase->type( in(1) );
164 if( t == Type::TOP ) return Type::TOP;
165 if( t == Type::FLOAT ) return TypeInt::INT;
166 const TypeF *tf = t->is_float_constant();
167 return TypeInt::make( SharedRuntime::f2i( tf->getf() ) );
168}
169
170//------------------------------Identity---------------------------------------
171Node* ConvF2INode::Identity(PhaseGVN* phase) {
172 // Remove ConvF2I->ConvI2F->ConvF2I sequences.
173 if( in(1) ->Opcode() == Op_ConvI2F &&
174 in(1)->in(1)->Opcode() == Op_ConvF2I )
175 return in(1)->in(1);
176 return this;
177}
178
179//------------------------------Ideal------------------------------------------
180// If converting to an int type, skip any rounding nodes
181Node *ConvF2INode::Ideal(PhaseGVN *phase, bool can_reshape) {
182 if( in(1)->Opcode() == Op_RoundFloat )
183 set_req(1,in(1)->in(1));
184 return NULL;
185}
186
187//=============================================================================
188//------------------------------Value------------------------------------------
189const Type* ConvF2LNode::Value(PhaseGVN* phase) const {
190 const Type *t = phase->type( in(1) );
191 if( t == Type::TOP ) return Type::TOP;
192 if( t == Type::FLOAT ) return TypeLong::LONG;
193 const TypeF *tf = t->is_float_constant();
194 return TypeLong::make( SharedRuntime::f2l( tf->getf() ) );
195}
196
197//------------------------------Identity---------------------------------------
198Node* ConvF2LNode::Identity(PhaseGVN* phase) {
199 // Remove ConvF2L->ConvL2F->ConvF2L sequences.
200 if( in(1) ->Opcode() == Op_ConvL2F &&
201 in(1)->in(1)->Opcode() == Op_ConvF2L )
202 return in(1)->in(1);
203 return this;
204}
205
206//------------------------------Ideal------------------------------------------
207// If converting to an int type, skip any rounding nodes
208Node *ConvF2LNode::Ideal(PhaseGVN *phase, bool can_reshape) {
209 if( in(1)->Opcode() == Op_RoundFloat )
210 set_req(1,in(1)->in(1));
211 return NULL;
212}
213
214//=============================================================================
215//------------------------------Value------------------------------------------
216const Type* ConvI2DNode::Value(PhaseGVN* phase) const {
217 const Type *t = phase->type( in(1) );
218 if( t == Type::TOP ) return Type::TOP;
219 const TypeInt *ti = t->is_int();
220 if( ti->is_con() ) return TypeD::make( (double)ti->get_con() );
221 return bottom_type();
222}
223
224//=============================================================================
225//------------------------------Value------------------------------------------
226const Type* ConvI2FNode::Value(PhaseGVN* phase) const {
227 const Type *t = phase->type( in(1) );
228 if( t == Type::TOP ) return Type::TOP;
229 const TypeInt *ti = t->is_int();
230 if( ti->is_con() ) return TypeF::make( (float)ti->get_con() );
231 return bottom_type();
232}
233
234//------------------------------Identity---------------------------------------
235Node* ConvI2FNode::Identity(PhaseGVN* phase) {
236 // Remove ConvI2F->ConvF2I->ConvI2F sequences.
237 if( in(1) ->Opcode() == Op_ConvF2I &&
238 in(1)->in(1)->Opcode() == Op_ConvI2F )
239 return in(1)->in(1);
240 return this;
241}
242
243//=============================================================================
244//------------------------------Value------------------------------------------
245const Type* ConvI2LNode::Value(PhaseGVN* phase) const {
246 const Type *t = phase->type( in(1) );
247 if( t == Type::TOP ) return Type::TOP;
248 const TypeInt *ti = t->is_int();
249 const Type* tl = TypeLong::make(ti->_lo, ti->_hi, ti->_widen);
250 // Join my declared type against my incoming type.
251 tl = tl->filter(_type);
252 return tl;
253}
254
255#ifdef _LP64
256static inline bool long_ranges_overlap(jlong lo1, jlong hi1,
257 jlong lo2, jlong hi2) {
258 // Two ranges overlap iff one range's low point falls in the other range.
259 return (lo2 <= lo1 && lo1 <= hi2) || (lo1 <= lo2 && lo2 <= hi1);
260}
261#endif
262
263//------------------------------Ideal------------------------------------------
264Node *ConvI2LNode::Ideal(PhaseGVN *phase, bool can_reshape) {
265 const TypeLong* this_type = this->type()->is_long();
266 Node* this_changed = NULL;
267
268 // If _major_progress, then more loop optimizations follow. Do NOT
269 // remove this node's type assertion until no more loop ops can happen.
270 // The progress bit is set in the major loop optimizations THEN comes the
271 // call to IterGVN and any chance of hitting this code. Cf. Opaque1Node.
272 if (can_reshape && !phase->C->major_progress()) {
273 const TypeInt* in_type = phase->type(in(1))->isa_int();
274 if (in_type != NULL && this_type != NULL &&
275 (in_type->_lo != this_type->_lo ||
276 in_type->_hi != this_type->_hi)) {
277 // Although this WORSENS the type, it increases GVN opportunities,
278 // because I2L nodes with the same input will common up, regardless
279 // of slightly differing type assertions. Such slight differences
280 // arise routinely as a result of loop unrolling, so this is a
281 // post-unrolling graph cleanup. Choose a type which depends only
282 // on my input. (Exception: Keep a range assertion of >=0 or <0.)
283 jlong lo1 = this_type->_lo;
284 jlong hi1 = this_type->_hi;
285 int w1 = this_type->_widen;
286 if (lo1 != (jint)lo1 ||
287 hi1 != (jint)hi1 ||
288 lo1 > hi1) {
289 // Overflow leads to wraparound, wraparound leads to range saturation.
290 lo1 = min_jint; hi1 = max_jint;
291 } else if (lo1 >= 0) {
292 // Keep a range assertion of >=0.
293 lo1 = 0; hi1 = max_jint;
294 } else if (hi1 < 0) {
295 // Keep a range assertion of <0.
296 lo1 = min_jint; hi1 = -1;
297 } else {
298 lo1 = min_jint; hi1 = max_jint;
299 }
300 const TypeLong* wtype = TypeLong::make(MAX2((jlong)in_type->_lo, lo1),
301 MIN2((jlong)in_type->_hi, hi1),
302 MAX2((int)in_type->_widen, w1));
303 if (wtype != type()) {
304 set_type(wtype);
305 // Note: this_type still has old type value, for the logic below.
306 this_changed = this;
307 }
308 }
309 }
310
311#ifdef _LP64
312 // Convert ConvI2L(AddI(x, y)) to AddL(ConvI2L(x), ConvI2L(y))
313 // but only if x and y have subranges that cannot cause 32-bit overflow,
314 // under the assumption that x+y is in my own subrange this->type().
315
316 // This assumption is based on a constraint (i.e., type assertion)
317 // established in Parse::array_addressing or perhaps elsewhere.
318 // This constraint has been adjoined to the "natural" type of
319 // the incoming argument in(0). We know (because of runtime
320 // checks) - that the result value I2L(x+y) is in the joined range.
321 // Hence we can restrict the incoming terms (x, y) to values such
322 // that their sum also lands in that range.
323
324 // This optimization is useful only on 64-bit systems, where we hope
325 // the addition will end up subsumed in an addressing mode.
326 // It is necessary to do this when optimizing an unrolled array
327 // copy loop such as x[i++] = y[i++].
328
329 // On 32-bit systems, it's better to perform as much 32-bit math as
330 // possible before the I2L conversion, because 32-bit math is cheaper.
331 // There's no common reason to "leak" a constant offset through the I2L.
332 // Addressing arithmetic will not absorb it as part of a 64-bit AddL.
333
334 Node* z = in(1);
335 int op = z->Opcode();
336 if (op == Op_AddI || op == Op_SubI) {
337 Node* x = z->in(1);
338 Node* y = z->in(2);
339 assert (x != z && y != z, "dead loop in ConvI2LNode::Ideal");
340 if (phase->type(x) == Type::TOP) return this_changed;
341 if (phase->type(y) == Type::TOP) return this_changed;
342 const TypeInt* tx = phase->type(x)->is_int();
343 const TypeInt* ty = phase->type(y)->is_int();
344 const TypeLong* tz = this_type;
345 jlong xlo = tx->_lo;
346 jlong xhi = tx->_hi;
347 jlong ylo = ty->_lo;
348 jlong yhi = ty->_hi;
349 jlong zlo = tz->_lo;
350 jlong zhi = tz->_hi;
351 jlong vbit = CONST64(1) << BitsPerInt;
352 int widen = MAX2(tx->_widen, ty->_widen);
353 if (op == Op_SubI) {
354 jlong ylo0 = ylo;
355 ylo = -yhi;
356 yhi = -ylo0;
357 }
358 // See if x+y can cause positive overflow into z+2**32
359 if (long_ranges_overlap(xlo+ylo, xhi+yhi, zlo+vbit, zhi+vbit)) {
360 return this_changed;
361 }
362 // See if x+y can cause negative overflow into z-2**32
363 if (long_ranges_overlap(xlo+ylo, xhi+yhi, zlo-vbit, zhi-vbit)) {
364 return this_changed;
365 }
366 // Now it's always safe to assume x+y does not overflow.
367 // This is true even if some pairs x,y might cause overflow, as long
368 // as that overflow value cannot fall into [zlo,zhi].
369
370 // Confident that the arithmetic is "as if infinite precision",
371 // we can now use z's range to put constraints on those of x and y.
372 // The "natural" range of x [xlo,xhi] can perhaps be narrowed to a
373 // more "restricted" range by intersecting [xlo,xhi] with the
374 // range obtained by subtracting y's range from the asserted range
375 // of the I2L conversion. Here's the interval arithmetic algebra:
376 // x == z-y == [zlo,zhi]-[ylo,yhi] == [zlo,zhi]+[-yhi,-ylo]
377 // => x in [zlo-yhi, zhi-ylo]
378 // => x in [zlo-yhi, zhi-ylo] INTERSECT [xlo,xhi]
379 // => x in [xlo MAX zlo-yhi, xhi MIN zhi-ylo]
380 jlong rxlo = MAX2(xlo, zlo - yhi);
381 jlong rxhi = MIN2(xhi, zhi - ylo);
382 // And similarly, x changing place with y:
383 jlong rylo = MAX2(ylo, zlo - xhi);
384 jlong ryhi = MIN2(yhi, zhi - xlo);
385 if (rxlo > rxhi || rylo > ryhi) {
386 return this_changed; // x or y is dying; don't mess w/ it
387 }
388 if (op == Op_SubI) {
389 jlong rylo0 = rylo;
390 rylo = -ryhi;
391 ryhi = -rylo0;
392 }
393 assert(rxlo == (int)rxlo && rxhi == (int)rxhi, "x should not overflow");
394 assert(rylo == (int)rylo && ryhi == (int)ryhi, "y should not overflow");
395 Node* cx = phase->C->constrained_convI2L(phase, x, TypeInt::make(rxlo, rxhi, widen), NULL);
396 Node *hook = new Node(1);
397 hook->init_req(0, cx); // Add a use to cx to prevent him from dying
398 Node* cy = phase->C->constrained_convI2L(phase, y, TypeInt::make(rylo, ryhi, widen), NULL);
399 hook->del_req(0); // Just yank bogus edge
400 hook->destruct();
401 switch (op) {
402 case Op_AddI: return new AddLNode(cx, cy);
403 case Op_SubI: return new SubLNode(cx, cy);
404 default: ShouldNotReachHere();
405 }
406 }
407#endif //_LP64
408
409 return this_changed;
410}
411
412//=============================================================================
413//------------------------------Value------------------------------------------
414const Type* ConvL2DNode::Value(PhaseGVN* phase) const {
415 const Type *t = phase->type( in(1) );
416 if( t == Type::TOP ) return Type::TOP;
417 const TypeLong *tl = t->is_long();
418 if( tl->is_con() ) return TypeD::make( (double)tl->get_con() );
419 return bottom_type();
420}
421
422//=============================================================================
423//------------------------------Value------------------------------------------
424const Type* ConvL2FNode::Value(PhaseGVN* phase) const {
425 const Type *t = phase->type( in(1) );
426 if( t == Type::TOP ) return Type::TOP;
427 const TypeLong *tl = t->is_long();
428 if( tl->is_con() ) return TypeF::make( (float)tl->get_con() );
429 return bottom_type();
430}
431
432//=============================================================================
433//----------------------------Identity-----------------------------------------
434Node* ConvL2INode::Identity(PhaseGVN* phase) {
435 // Convert L2I(I2L(x)) => x
436 if (in(1)->Opcode() == Op_ConvI2L) return in(1)->in(1);
437 return this;
438}
439
440//------------------------------Value------------------------------------------
441const Type* ConvL2INode::Value(PhaseGVN* phase) const {
442 const Type *t = phase->type( in(1) );
443 if( t == Type::TOP ) return Type::TOP;
444 const TypeLong *tl = t->is_long();
445 if (tl->is_con())
446 // Easy case.
447 return TypeInt::make((jint)tl->get_con());
448 return bottom_type();
449}
450
451//------------------------------Ideal------------------------------------------
452// Return a node which is more "ideal" than the current node.
453// Blow off prior masking to int
454Node *ConvL2INode::Ideal(PhaseGVN *phase, bool can_reshape) {
455 Node *andl = in(1);
456 uint andl_op = andl->Opcode();
457 if( andl_op == Op_AndL ) {
458 // Blow off prior masking to int
459 if( phase->type(andl->in(2)) == TypeLong::make( 0xFFFFFFFF ) ) {
460 set_req(1,andl->in(1));
461 return this;
462 }
463 }
464
465 // Swap with a prior add: convL2I(addL(x,y)) ==> addI(convL2I(x),convL2I(y))
466 // This replaces an 'AddL' with an 'AddI'.
467 if( andl_op == Op_AddL ) {
468 // Don't do this for nodes which have more than one user since
469 // we'll end up computing the long add anyway.
470 if (andl->outcnt() > 1) return NULL;
471
472 Node* x = andl->in(1);
473 Node* y = andl->in(2);
474 assert( x != andl && y != andl, "dead loop in ConvL2INode::Ideal" );
475 if (phase->type(x) == Type::TOP) return NULL;
476 if (phase->type(y) == Type::TOP) return NULL;
477 Node *add1 = phase->transform(new ConvL2INode(x));
478 Node *add2 = phase->transform(new ConvL2INode(y));
479 return new AddINode(add1,add2);
480 }
481
482 // Disable optimization: LoadL->ConvL2I ==> LoadI.
483 // It causes problems (sizes of Load and Store nodes do not match)
484 // in objects initialization code and Escape Analysis.
485 return NULL;
486}
487
488
489
490//=============================================================================
491//------------------------------Identity---------------------------------------
492// Remove redundant roundings
493Node* RoundFloatNode::Identity(PhaseGVN* phase) {
494 assert(Matcher::strict_fp_requires_explicit_rounding, "should only generate for Intel");
495 // Do not round constants
496 if (phase->type(in(1))->base() == Type::FloatCon) return in(1);
497 int op = in(1)->Opcode();
498 // Redundant rounding
499 if( op == Op_RoundFloat ) return in(1);
500 // Already rounded
501 if( op == Op_Parm ) return in(1);
502 if( op == Op_LoadF ) return in(1);
503 return this;
504}
505
506//------------------------------Value------------------------------------------
507const Type* RoundFloatNode::Value(PhaseGVN* phase) const {
508 return phase->type( in(1) );
509}
510
511//=============================================================================
512//------------------------------Identity---------------------------------------
513// Remove redundant roundings. Incoming arguments are already rounded.
514Node* RoundDoubleNode::Identity(PhaseGVN* phase) {
515 assert(Matcher::strict_fp_requires_explicit_rounding, "should only generate for Intel");
516 // Do not round constants
517 if (phase->type(in(1))->base() == Type::DoubleCon) return in(1);
518 int op = in(1)->Opcode();
519 // Redundant rounding
520 if( op == Op_RoundDouble ) return in(1);
521 // Already rounded
522 if( op == Op_Parm ) return in(1);
523 if( op == Op_LoadD ) return in(1);
524 if( op == Op_ConvF2D ) return in(1);
525 if( op == Op_ConvI2D ) return in(1);
526 return this;
527}
528
529//------------------------------Value------------------------------------------
530const Type* RoundDoubleNode::Value(PhaseGVN* phase) const {
531 return phase->type( in(1) );
532}
533
534
535