1 /*
2  * Copyright (c) 1997, 2020, 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/connode.hpp"
29 #include "opto/convertnode.hpp"
30 #include "opto/memnode.hpp"
31 #include "opto/mulnode.hpp"
32 #include "opto/phaseX.hpp"
33 #include "opto/subnode.hpp"
34 #include "utilities/powerOfTwo.hpp"
35 
36 // Portions of code courtesy of Clifford Click
37 
38 
39 //=============================================================================
40 //------------------------------hash-------------------------------------------
41 // Hash function over MulNodes.  Needs to be commutative; i.e., I swap
42 // (commute) inputs to MulNodes willy-nilly so the hash function must return
43 // the same value in the presence of edge swapping.
hash() const44 uint MulNode::hash() const {
45   return (uintptr_t)in(1) + (uintptr_t)in(2) + Opcode();
46 }
47 
48 //------------------------------Identity---------------------------------------
49 // Multiplying a one preserves the other argument
Identity(PhaseGVN * phase)50 Node* MulNode::Identity(PhaseGVN* phase) {
51   const Type *one = mul_id();  // The multiplicative identity
52   if( phase->type( in(1) )->higher_equal( one ) ) return in(2);
53   if( phase->type( in(2) )->higher_equal( one ) ) return in(1);
54 
55   return this;
56 }
57 
58 //------------------------------Ideal------------------------------------------
59 // We also canonicalize the Node, moving constants to the right input,
60 // and flatten expressions (so that 1+x+2 becomes x+3).
Ideal(PhaseGVN * phase,bool can_reshape)61 Node *MulNode::Ideal(PhaseGVN *phase, bool can_reshape) {
62   const Type *t1 = phase->type( in(1) );
63   const Type *t2 = phase->type( in(2) );
64   Node *progress = NULL;        // Progress flag
65   // We are OK if right is a constant, or right is a load and
66   // left is a non-constant.
67   if( !(t2->singleton() ||
68         (in(2)->is_Load() && !(t1->singleton() || in(1)->is_Load())) ) ) {
69     if( t1->singleton() ||       // Left input is a constant?
70         // Otherwise, sort inputs (commutativity) to help value numbering.
71         (in(1)->_idx > in(2)->_idx) ) {
72       swap_edges(1, 2);
73       const Type *t = t1;
74       t1 = t2;
75       t2 = t;
76       progress = this;            // Made progress
77     }
78   }
79 
80   // If the right input is a constant, and the left input is a product of a
81   // constant, flatten the expression tree.
82   uint op = Opcode();
83   if( t2->singleton() &&        // Right input is a constant?
84       op != Op_MulF &&          // Float & double cannot reassociate
85       op != Op_MulD ) {
86     if( t2 == Type::TOP ) return NULL;
87     Node *mul1 = in(1);
88 #ifdef ASSERT
89     // Check for dead loop
90     int op1 = mul1->Opcode();
91     if ((mul1 == this) || (in(2) == this) ||
92         ((op1 == mul_opcode() || op1 == add_opcode()) &&
93          ((mul1->in(1) == this) || (mul1->in(2) == this) ||
94           (mul1->in(1) == mul1) || (mul1->in(2) == mul1)))) {
95       assert(false, "dead loop in MulNode::Ideal");
96     }
97 #endif
98 
99     if( mul1->Opcode() == mul_opcode() ) {  // Left input is a multiply?
100       // Mul of a constant?
101       const Type *t12 = phase->type( mul1->in(2) );
102       if( t12->singleton() && t12 != Type::TOP) { // Left input is an add of a constant?
103         // Compute new constant; check for overflow
104         const Type *tcon01 = ((MulNode*)mul1)->mul_ring(t2,t12);
105         if( tcon01->singleton() ) {
106           // The Mul of the flattened expression
107           set_req(1, mul1->in(1));
108           set_req(2, phase->makecon( tcon01 ));
109           t2 = tcon01;
110           progress = this;      // Made progress
111         }
112       }
113     }
114     // If the right input is a constant, and the left input is an add of a
115     // constant, flatten the tree: (X+con1)*con0 ==> X*con0 + con1*con0
116     const Node *add1 = in(1);
117     if( add1->Opcode() == add_opcode() ) {      // Left input is an add?
118       // Add of a constant?
119       const Type *t12 = phase->type( add1->in(2) );
120       if( t12->singleton() && t12 != Type::TOP ) { // Left input is an add of a constant?
121         assert( add1->in(1) != add1, "dead loop in MulNode::Ideal" );
122         // Compute new constant; check for overflow
123         const Type *tcon01 = mul_ring(t2,t12);
124         if( tcon01->singleton() ) {
125 
126         // Convert (X+con1)*con0 into X*con0
127           Node *mul = clone();    // mul = ()*con0
128           mul->set_req(1,add1->in(1));  // mul = X*con0
129           mul = phase->transform(mul);
130 
131           Node *add2 = add1->clone();
132           add2->set_req(1, mul);        // X*con0 + con0*con1
133           add2->set_req(2, phase->makecon(tcon01) );
134           progress = add2;
135         }
136       }
137     } // End of is left input an add
138   } // End of is right input a Mul
139 
140   return progress;
141 }
142 
143 //------------------------------Value-----------------------------------------
Value(PhaseGVN * phase) const144 const Type* MulNode::Value(PhaseGVN* phase) const {
145   const Type *t1 = phase->type( in(1) );
146   const Type *t2 = phase->type( in(2) );
147   // Either input is TOP ==> the result is TOP
148   if( t1 == Type::TOP ) return Type::TOP;
149   if( t2 == Type::TOP ) return Type::TOP;
150 
151   // Either input is ZERO ==> the result is ZERO.
152   // Not valid for floats or doubles since +0.0 * -0.0 --> +0.0
153   int op = Opcode();
154   if( op == Op_MulI || op == Op_AndI || op == Op_MulL || op == Op_AndL ) {
155     const Type *zero = add_id();        // The multiplicative zero
156     if( t1->higher_equal( zero ) ) return zero;
157     if( t2->higher_equal( zero ) ) return zero;
158   }
159 
160   // Either input is BOTTOM ==> the result is the local BOTTOM
161   if( t1 == Type::BOTTOM || t2 == Type::BOTTOM )
162     return bottom_type();
163 
164 #if defined(IA32)
165   // Can't trust native compilers to properly fold strict double
166   // multiplication with round-to-zero on this platform.
167   if (op == Op_MulD && phase->C->method()->is_strict()) {
168     return TypeD::DOUBLE;
169   }
170 #endif
171 
172   return mul_ring(t1,t2);            // Local flavor of type multiplication
173 }
174 
175 //=============================================================================
176 //------------------------------Ideal------------------------------------------
177 // Check for power-of-2 multiply, then try the regular MulNode::Ideal
Ideal(PhaseGVN * phase,bool can_reshape)178 Node *MulINode::Ideal(PhaseGVN *phase, bool can_reshape) {
179   // Swap constant to right
180   jint con;
181   if ((con = in(1)->find_int_con(0)) != 0) {
182     swap_edges(1, 2);
183     // Finish rest of method to use info in 'con'
184   } else if ((con = in(2)->find_int_con(0)) == 0) {
185     return MulNode::Ideal(phase, can_reshape);
186   }
187 
188   // Now we have a constant Node on the right and the constant in con
189   if (con == 0) return NULL;   // By zero is handled by Value call
190   if (con == 1) return NULL;   // By one  is handled by Identity call
191 
192   // Check for negative constant; if so negate the final result
193   bool sign_flip = false;
194 
195   unsigned int abs_con = uabs(con);
196   if (abs_con != (unsigned int)con) {
197     sign_flip = true;
198   }
199 
200   // Get low bit; check for being the only bit
201   Node *res = NULL;
202   unsigned int bit1 = abs_con & (0-abs_con);       // Extract low bit
203   if (bit1 == abs_con) {           // Found a power of 2?
204     res = new LShiftINode(in(1), phase->intcon(log2_uint(bit1)));
205   } else {
206 
207     // Check for constant with 2 bits set
208     unsigned int bit2 = abs_con-bit1;
209     bit2 = bit2 & (0-bit2);          // Extract 2nd bit
210     if (bit2 + bit1 == abs_con) {    // Found all bits in con?
211       Node *n1 = phase->transform( new LShiftINode(in(1), phase->intcon(log2_uint(bit1))));
212       Node *n2 = phase->transform( new LShiftINode(in(1), phase->intcon(log2_uint(bit2))));
213       res = new AddINode(n2, n1);
214 
215     } else if (is_power_of_2(abs_con+1)) {
216       // Sleezy: power-of-2 -1.  Next time be generic.
217       unsigned int temp = abs_con + 1;
218       Node *n1 = phase->transform(new LShiftINode(in(1), phase->intcon(log2_uint(temp))));
219       res = new SubINode(n1, in(1));
220     } else {
221       return MulNode::Ideal(phase, can_reshape);
222     }
223   }
224 
225   if (sign_flip) {             // Need to negate result?
226     res = phase->transform(res);// Transform, before making the zero con
227     res = new SubINode(phase->intcon(0),res);
228   }
229 
230   return res;                   // Return final result
231 }
232 
233 //------------------------------mul_ring---------------------------------------
234 // Compute the product type of two integer ranges into this node.
mul_ring(const Type * t0,const Type * t1) const235 const Type *MulINode::mul_ring(const Type *t0, const Type *t1) const {
236   const TypeInt *r0 = t0->is_int(); // Handy access
237   const TypeInt *r1 = t1->is_int();
238 
239   // Fetch endpoints of all ranges
240   jint lo0 = r0->_lo;
241   double a = (double)lo0;
242   jint hi0 = r0->_hi;
243   double b = (double)hi0;
244   jint lo1 = r1->_lo;
245   double c = (double)lo1;
246   jint hi1 = r1->_hi;
247   double d = (double)hi1;
248 
249   // Compute all endpoints & check for overflow
250   int32_t A = java_multiply(lo0, lo1);
251   if( (double)A != a*c ) return TypeInt::INT; // Overflow?
252   int32_t B = java_multiply(lo0, hi1);
253   if( (double)B != a*d ) return TypeInt::INT; // Overflow?
254   int32_t C = java_multiply(hi0, lo1);
255   if( (double)C != b*c ) return TypeInt::INT; // Overflow?
256   int32_t D = java_multiply(hi0, hi1);
257   if( (double)D != b*d ) return TypeInt::INT; // Overflow?
258 
259   if( A < B ) { lo0 = A; hi0 = B; } // Sort range endpoints
260   else { lo0 = B; hi0 = A; }
261   if( C < D ) {
262     if( C < lo0 ) lo0 = C;
263     if( D > hi0 ) hi0 = D;
264   } else {
265     if( D < lo0 ) lo0 = D;
266     if( C > hi0 ) hi0 = C;
267   }
268   return TypeInt::make(lo0, hi0, MAX2(r0->_widen,r1->_widen));
269 }
270 
271 
272 //=============================================================================
273 //------------------------------Ideal------------------------------------------
274 // Check for power-of-2 multiply, then try the regular MulNode::Ideal
Ideal(PhaseGVN * phase,bool can_reshape)275 Node *MulLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
276   // Swap constant to right
277   jlong con;
278   if ((con = in(1)->find_long_con(0)) != 0) {
279     swap_edges(1, 2);
280     // Finish rest of method to use info in 'con'
281   } else if ((con = in(2)->find_long_con(0)) == 0) {
282     return MulNode::Ideal(phase, can_reshape);
283   }
284 
285   // Now we have a constant Node on the right and the constant in con
286   if (con == CONST64(0)) return NULL;  // By zero is handled by Value call
287   if (con == CONST64(1)) return NULL;  // By one  is handled by Identity call
288 
289   // Check for negative constant; if so negate the final result
290   bool sign_flip = false;
291   julong abs_con = uabs(con);
292   if (abs_con != (julong)con) {
293     sign_flip = true;
294   }
295 
296   // Get low bit; check for being the only bit
297   Node *res = NULL;
298   julong bit1 = abs_con & (0-abs_con);      // Extract low bit
299   if (bit1 == abs_con) {           // Found a power of 2?
300     res = new LShiftLNode(in(1), phase->intcon(log2_long(bit1)));
301   } else {
302 
303     // Check for constant with 2 bits set
304     julong bit2 = abs_con-bit1;
305     bit2 = bit2 & (0-bit2);          // Extract 2nd bit
306     if (bit2 + bit1 == abs_con) {    // Found all bits in con?
307       Node *n1 = phase->transform(new LShiftLNode(in(1), phase->intcon(log2_long(bit1))));
308       Node *n2 = phase->transform(new LShiftLNode(in(1), phase->intcon(log2_long(bit2))));
309       res = new AddLNode(n2, n1);
310 
311     } else if (is_power_of_2(abs_con+1)) {
312       // Sleezy: power-of-2 -1.  Next time be generic.
313       julong temp = abs_con + 1;
314       Node *n1 = phase->transform( new LShiftLNode(in(1), phase->intcon(log2_long(temp))));
315       res = new SubLNode(n1, in(1));
316     } else {
317       return MulNode::Ideal(phase, can_reshape);
318     }
319   }
320 
321   if (sign_flip) {             // Need to negate result?
322     res = phase->transform(res);// Transform, before making the zero con
323     res = new SubLNode(phase->longcon(0),res);
324   }
325 
326   return res;                   // Return final result
327 }
328 
329 //------------------------------mul_ring---------------------------------------
330 // Compute the product type of two integer ranges into this node.
mul_ring(const Type * t0,const Type * t1) const331 const Type *MulLNode::mul_ring(const Type *t0, const Type *t1) const {
332   const TypeLong *r0 = t0->is_long(); // Handy access
333   const TypeLong *r1 = t1->is_long();
334 
335   // Fetch endpoints of all ranges
336   jlong lo0 = r0->_lo;
337   double a = (double)lo0;
338   jlong hi0 = r0->_hi;
339   double b = (double)hi0;
340   jlong lo1 = r1->_lo;
341   double c = (double)lo1;
342   jlong hi1 = r1->_hi;
343   double d = (double)hi1;
344 
345   // Compute all endpoints & check for overflow
346   jlong A = java_multiply(lo0, lo1);
347   if( (double)A != a*c ) return TypeLong::LONG; // Overflow?
348   jlong B = java_multiply(lo0, hi1);
349   if( (double)B != a*d ) return TypeLong::LONG; // Overflow?
350   jlong C = java_multiply(hi0, lo1);
351   if( (double)C != b*c ) return TypeLong::LONG; // Overflow?
352   jlong D = java_multiply(hi0, hi1);
353   if( (double)D != b*d ) return TypeLong::LONG; // Overflow?
354 
355   if( A < B ) { lo0 = A; hi0 = B; } // Sort range endpoints
356   else { lo0 = B; hi0 = A; }
357   if( C < D ) {
358     if( C < lo0 ) lo0 = C;
359     if( D > hi0 ) hi0 = D;
360   } else {
361     if( D < lo0 ) lo0 = D;
362     if( C > hi0 ) hi0 = C;
363   }
364   return TypeLong::make(lo0, hi0, MAX2(r0->_widen,r1->_widen));
365 }
366 
367 //=============================================================================
368 //------------------------------mul_ring---------------------------------------
369 // Compute the product type of two double ranges into this node.
mul_ring(const Type * t0,const Type * t1) const370 const Type *MulFNode::mul_ring(const Type *t0, const Type *t1) const {
371   if( t0 == Type::FLOAT || t1 == Type::FLOAT ) return Type::FLOAT;
372   return TypeF::make( t0->getf() * t1->getf() );
373 }
374 
375 //=============================================================================
376 //------------------------------mul_ring---------------------------------------
377 // Compute the product type of two double ranges into this node.
mul_ring(const Type * t0,const Type * t1) const378 const Type *MulDNode::mul_ring(const Type *t0, const Type *t1) const {
379   if( t0 == Type::DOUBLE || t1 == Type::DOUBLE ) return Type::DOUBLE;
380   // We must be multiplying 2 double constants.
381   return TypeD::make( t0->getd() * t1->getd() );
382 }
383 
384 //=============================================================================
385 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const386 const Type* MulHiLNode::Value(PhaseGVN* phase) const {
387   // Either input is TOP ==> the result is TOP
388   const Type *t1 = phase->type( in(1) );
389   const Type *t2 = phase->type( in(2) );
390   if( t1 == Type::TOP ) return Type::TOP;
391   if( t2 == Type::TOP ) return Type::TOP;
392 
393   // Either input is BOTTOM ==> the result is the local BOTTOM
394   const Type *bot = bottom_type();
395   if( (t1 == bot) || (t2 == bot) ||
396       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
397     return bot;
398 
399   // It is not worth trying to constant fold this stuff!
400   return TypeLong::LONG;
401 }
402 
403 //=============================================================================
404 //------------------------------mul_ring---------------------------------------
405 // Supplied function returns the product of the inputs IN THE CURRENT RING.
406 // For the logical operations the ring's MUL is really a logical AND function.
407 // This also type-checks the inputs for sanity.  Guaranteed never to
408 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
mul_ring(const Type * t0,const Type * t1) const409 const Type *AndINode::mul_ring( const Type *t0, const Type *t1 ) const {
410   const TypeInt *r0 = t0->is_int(); // Handy access
411   const TypeInt *r1 = t1->is_int();
412   int widen = MAX2(r0->_widen,r1->_widen);
413 
414   // If either input is a constant, might be able to trim cases
415   if( !r0->is_con() && !r1->is_con() )
416     return TypeInt::INT;        // No constants to be had
417 
418   // Both constants?  Return bits
419   if( r0->is_con() && r1->is_con() )
420     return TypeInt::make( r0->get_con() & r1->get_con() );
421 
422   if( r0->is_con() && r0->get_con() > 0 )
423     return TypeInt::make(0, r0->get_con(), widen);
424 
425   if( r1->is_con() && r1->get_con() > 0 )
426     return TypeInt::make(0, r1->get_con(), widen);
427 
428   if( r0 == TypeInt::BOOL || r1 == TypeInt::BOOL ) {
429     return TypeInt::BOOL;
430   }
431 
432   return TypeInt::INT;          // No constants to be had
433 }
434 
435 //------------------------------Identity---------------------------------------
436 // Masking off the high bits of an unsigned load is not required
Identity(PhaseGVN * phase)437 Node* AndINode::Identity(PhaseGVN* phase) {
438 
439   // x & x => x
440   if (in(1) == in(2)) {
441     return in(1);
442   }
443 
444   Node* in1 = in(1);
445   uint op = in1->Opcode();
446   const TypeInt* t2 = phase->type(in(2))->isa_int();
447   if (t2 && t2->is_con()) {
448     int con = t2->get_con();
449     // Masking off high bits which are always zero is useless.
450     const TypeInt* t1 = phase->type( in(1) )->isa_int();
451     if (t1 != NULL && t1->_lo >= 0) {
452       jint t1_support = right_n_bits(1 + log2_jint(t1->_hi));
453       if ((t1_support & con) == t1_support)
454         return in1;
455     }
456     // Masking off the high bits of a unsigned-shift-right is not
457     // needed either.
458     if (op == Op_URShiftI) {
459       const TypeInt* t12 = phase->type(in1->in(2))->isa_int();
460       if (t12 && t12->is_con()) {  // Shift is by a constant
461         int shift = t12->get_con();
462         shift &= BitsPerJavaInteger - 1;  // semantics of Java shifts
463         int mask = max_juint >> shift;
464         if ((mask & con) == mask)  // If AND is useless, skip it
465           return in1;
466       }
467     }
468   }
469   return MulNode::Identity(phase);
470 }
471 
472 //------------------------------Ideal------------------------------------------
Ideal(PhaseGVN * phase,bool can_reshape)473 Node *AndINode::Ideal(PhaseGVN *phase, bool can_reshape) {
474   // Special case constant AND mask
475   const TypeInt *t2 = phase->type( in(2) )->isa_int();
476   if( !t2 || !t2->is_con() ) return MulNode::Ideal(phase, can_reshape);
477   const int mask = t2->get_con();
478   Node *load = in(1);
479   uint lop = load->Opcode();
480 
481   // Masking bits off of a Character?  Hi bits are already zero.
482   if( lop == Op_LoadUS &&
483       (mask & 0xFFFF0000) )     // Can we make a smaller mask?
484     return new AndINode(load,phase->intcon(mask&0xFFFF));
485 
486   // Masking bits off of a Short?  Loading a Character does some masking
487   if (can_reshape &&
488       load->outcnt() == 1 && load->unique_out() == this) {
489     if (lop == Op_LoadS && (mask & 0xFFFF0000) == 0 ) {
490       Node* ldus = load->as_Load()->convert_to_unsigned_load(*phase);
491       ldus = phase->transform(ldus);
492       return new AndINode(ldus, phase->intcon(mask & 0xFFFF));
493     }
494 
495     // Masking sign bits off of a Byte?  Do an unsigned byte load plus
496     // an and.
497     if (lop == Op_LoadB && (mask & 0xFFFFFF00) == 0) {
498       Node* ldub = load->as_Load()->convert_to_unsigned_load(*phase);
499       ldub = phase->transform(ldub);
500       return new AndINode(ldub, phase->intcon(mask));
501     }
502   }
503 
504   // Masking off sign bits?  Dont make them!
505   if( lop == Op_RShiftI ) {
506     const TypeInt *t12 = phase->type(load->in(2))->isa_int();
507     if( t12 && t12->is_con() ) { // Shift is by a constant
508       int shift = t12->get_con();
509       shift &= BitsPerJavaInteger-1;  // semantics of Java shifts
510       const int sign_bits_mask = ~right_n_bits(BitsPerJavaInteger - shift);
511       // If the AND'ing of the 2 masks has no bits, then only original shifted
512       // bits survive.  NO sign-extension bits survive the maskings.
513       if( (sign_bits_mask & mask) == 0 ) {
514         // Use zero-fill shift instead
515         Node *zshift = phase->transform(new URShiftINode(load->in(1),load->in(2)));
516         return new AndINode( zshift, in(2) );
517       }
518     }
519   }
520 
521   // Check for 'negate/and-1', a pattern emitted when someone asks for
522   // 'mod 2'.  Negate leaves the low order bit unchanged (think: complement
523   // plus 1) and the mask is of the low order bit.  Skip the negate.
524   if( lop == Op_SubI && mask == 1 && load->in(1) &&
525       phase->type(load->in(1)) == TypeInt::ZERO )
526     return new AndINode( load->in(2), in(2) );
527 
528   return MulNode::Ideal(phase, can_reshape);
529 }
530 
531 //=============================================================================
532 //------------------------------mul_ring---------------------------------------
533 // Supplied function returns the product of the inputs IN THE CURRENT RING.
534 // For the logical operations the ring's MUL is really a logical AND function.
535 // This also type-checks the inputs for sanity.  Guaranteed never to
536 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
mul_ring(const Type * t0,const Type * t1) const537 const Type *AndLNode::mul_ring( const Type *t0, const Type *t1 ) const {
538   const TypeLong *r0 = t0->is_long(); // Handy access
539   const TypeLong *r1 = t1->is_long();
540   int widen = MAX2(r0->_widen,r1->_widen);
541 
542   // If either input is a constant, might be able to trim cases
543   if( !r0->is_con() && !r1->is_con() )
544     return TypeLong::LONG;      // No constants to be had
545 
546   // Both constants?  Return bits
547   if( r0->is_con() && r1->is_con() )
548     return TypeLong::make( r0->get_con() & r1->get_con() );
549 
550   if( r0->is_con() && r0->get_con() > 0 )
551     return TypeLong::make(CONST64(0), r0->get_con(), widen);
552 
553   if( r1->is_con() && r1->get_con() > 0 )
554     return TypeLong::make(CONST64(0), r1->get_con(), widen);
555 
556   return TypeLong::LONG;        // No constants to be had
557 }
558 
559 //------------------------------Identity---------------------------------------
560 // Masking off the high bits of an unsigned load is not required
Identity(PhaseGVN * phase)561 Node* AndLNode::Identity(PhaseGVN* phase) {
562 
563   // x & x => x
564   if (in(1) == in(2)) {
565     return in(1);
566   }
567 
568   Node *usr = in(1);
569   const TypeLong *t2 = phase->type( in(2) )->isa_long();
570   if( t2 && t2->is_con() ) {
571     jlong con = t2->get_con();
572     // Masking off high bits which are always zero is useless.
573     const TypeLong* t1 = phase->type( in(1) )->isa_long();
574     if (t1 != NULL && t1->_lo >= 0) {
575       int bit_count = log2_long(t1->_hi) + 1;
576       jlong t1_support = jlong(max_julong >> (BitsPerJavaLong - bit_count));
577       if ((t1_support & con) == t1_support)
578         return usr;
579     }
580     uint lop = usr->Opcode();
581     // Masking off the high bits of a unsigned-shift-right is not
582     // needed either.
583     if( lop == Op_URShiftL ) {
584       const TypeInt *t12 = phase->type( usr->in(2) )->isa_int();
585       if( t12 && t12->is_con() ) {  // Shift is by a constant
586         int shift = t12->get_con();
587         shift &= BitsPerJavaLong - 1;  // semantics of Java shifts
588         jlong mask = max_julong >> shift;
589         if( (mask&con) == mask )  // If AND is useless, skip it
590           return usr;
591       }
592     }
593   }
594   return MulNode::Identity(phase);
595 }
596 
597 //------------------------------Ideal------------------------------------------
Ideal(PhaseGVN * phase,bool can_reshape)598 Node *AndLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
599   // Special case constant AND mask
600   const TypeLong *t2 = phase->type( in(2) )->isa_long();
601   if( !t2 || !t2->is_con() ) return MulNode::Ideal(phase, can_reshape);
602   const jlong mask = t2->get_con();
603 
604   Node* in1 = in(1);
605   uint op = in1->Opcode();
606 
607   // Are we masking a long that was converted from an int with a mask
608   // that fits in 32-bits?  Commute them and use an AndINode.  Don't
609   // convert masks which would cause a sign extension of the integer
610   // value.  This check includes UI2L masks (0x00000000FFFFFFFF) which
611   // would be optimized away later in Identity.
612   if (op == Op_ConvI2L && (mask & UCONST64(0xFFFFFFFF80000000)) == 0) {
613     Node* andi = new AndINode(in1->in(1), phase->intcon(mask));
614     andi = phase->transform(andi);
615     return new ConvI2LNode(andi);
616   }
617 
618   // Masking off sign bits?  Dont make them!
619   if (op == Op_RShiftL) {
620     const TypeInt* t12 = phase->type(in1->in(2))->isa_int();
621     if( t12 && t12->is_con() ) { // Shift is by a constant
622       int shift = t12->get_con();
623       shift &= BitsPerJavaLong - 1;  // semantics of Java shifts
624       const jlong sign_bits_mask = ~(((jlong)CONST64(1) << (jlong)(BitsPerJavaLong - shift)) -1);
625       // If the AND'ing of the 2 masks has no bits, then only original shifted
626       // bits survive.  NO sign-extension bits survive the maskings.
627       if( (sign_bits_mask & mask) == 0 ) {
628         // Use zero-fill shift instead
629         Node *zshift = phase->transform(new URShiftLNode(in1->in(1), in1->in(2)));
630         return new AndLNode(zshift, in(2));
631       }
632     }
633   }
634 
635   return MulNode::Ideal(phase, can_reshape);
636 }
637 
638 //=============================================================================
639 
const_shift_count(PhaseGVN * phase,Node * shiftNode,int * count)640 static bool const_shift_count(PhaseGVN* phase, Node* shiftNode, int* count) {
641   const TypeInt* tcount = phase->type(shiftNode->in(2))->isa_int();
642   if (tcount != NULL && tcount->is_con()) {
643     *count = tcount->get_con();
644     return true;
645   }
646   return false;
647 }
648 
maskShiftAmount(PhaseGVN * phase,Node * shiftNode,int nBits)649 static int maskShiftAmount(PhaseGVN* phase, Node* shiftNode, int nBits) {
650   int count = 0;
651   if (const_shift_count(phase, shiftNode, &count)) {
652     int maskedShift = count & (nBits - 1);
653     if (maskedShift == 0) {
654       // Let Identity() handle 0 shift count.
655       return 0;
656     }
657 
658     if (count != maskedShift) {
659       shiftNode->set_req(2, phase->intcon(maskedShift)); // Replace shift count with masked value.
660       PhaseIterGVN* igvn = phase->is_IterGVN();
661       if (igvn) {
662         igvn->rehash_node_delayed(shiftNode);
663       }
664     }
665     return maskedShift;
666   }
667   return 0;
668 }
669 
670 //------------------------------Identity---------------------------------------
Identity(PhaseGVN * phase)671 Node* LShiftINode::Identity(PhaseGVN* phase) {
672   int count = 0;
673   if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaInteger - 1)) == 0) {
674     // Shift by a multiple of 32 does nothing
675     return in(1);
676   }
677   return this;
678 }
679 
680 //------------------------------Ideal------------------------------------------
681 // If the right input is a constant, and the left input is an add of a
682 // constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0
Ideal(PhaseGVN * phase,bool can_reshape)683 Node *LShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) {
684   int con = maskShiftAmount(phase, this, BitsPerJavaInteger);
685   if (con == 0) {
686     return NULL;
687   }
688 
689   // Left input is an add of a constant?
690   Node *add1 = in(1);
691   int add1_op = add1->Opcode();
692   if( add1_op == Op_AddI ) {    // Left input is an add?
693     assert( add1 != add1->in(1), "dead loop in LShiftINode::Ideal" );
694     const TypeInt *t12 = phase->type(add1->in(2))->isa_int();
695     if( t12 && t12->is_con() ){ // Left input is an add of a con?
696       // Transform is legal, but check for profit.  Avoid breaking 'i2s'
697       // and 'i2b' patterns which typically fold into 'StoreC/StoreB'.
698       if( con < 16 ) {
699         // Compute X << con0
700         Node *lsh = phase->transform( new LShiftINode( add1->in(1), in(2) ) );
701         // Compute X<<con0 + (con1<<con0)
702         return new AddINode( lsh, phase->intcon(t12->get_con() << con));
703       }
704     }
705   }
706 
707   // Check for "(x>>c0)<<c0" which just masks off low bits
708   if( (add1_op == Op_RShiftI || add1_op == Op_URShiftI ) &&
709       add1->in(2) == in(2) )
710     // Convert to "(x & -(1<<c0))"
711     return new AndINode(add1->in(1),phase->intcon( -(1<<con)));
712 
713   // Check for "((x>>c0) & Y)<<c0" which just masks off more low bits
714   if( add1_op == Op_AndI ) {
715     Node *add2 = add1->in(1);
716     int add2_op = add2->Opcode();
717     if( (add2_op == Op_RShiftI || add2_op == Op_URShiftI ) &&
718         add2->in(2) == in(2) ) {
719       // Convert to "(x & (Y<<c0))"
720       Node *y_sh = phase->transform( new LShiftINode( add1->in(2), in(2) ) );
721       return new AndINode( add2->in(1), y_sh );
722     }
723   }
724 
725   // Check for ((x & ((1<<(32-c0))-1)) << c0) which ANDs off high bits
726   // before shifting them away.
727   const jint bits_mask = right_n_bits(BitsPerJavaInteger-con);
728   if( add1_op == Op_AndI &&
729       phase->type(add1->in(2)) == TypeInt::make( bits_mask ) )
730     return new LShiftINode( add1->in(1), in(2) );
731 
732   return NULL;
733 }
734 
735 //------------------------------Value------------------------------------------
736 // A LShiftINode shifts its input2 left by input1 amount.
Value(PhaseGVN * phase) const737 const Type* LShiftINode::Value(PhaseGVN* phase) const {
738   const Type *t1 = phase->type( in(1) );
739   const Type *t2 = phase->type( in(2) );
740   // Either input is TOP ==> the result is TOP
741   if( t1 == Type::TOP ) return Type::TOP;
742   if( t2 == Type::TOP ) return Type::TOP;
743 
744   // Left input is ZERO ==> the result is ZERO.
745   if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
746   // Shift by zero does nothing
747   if( t2 == TypeInt::ZERO ) return t1;
748 
749   // Either input is BOTTOM ==> the result is BOTTOM
750   if( (t1 == TypeInt::INT) || (t2 == TypeInt::INT) ||
751       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
752     return TypeInt::INT;
753 
754   const TypeInt *r1 = t1->is_int(); // Handy access
755   const TypeInt *r2 = t2->is_int(); // Handy access
756 
757   if (!r2->is_con())
758     return TypeInt::INT;
759 
760   uint shift = r2->get_con();
761   shift &= BitsPerJavaInteger-1;  // semantics of Java shifts
762   // Shift by a multiple of 32 does nothing:
763   if (shift == 0)  return t1;
764 
765   // If the shift is a constant, shift the bounds of the type,
766   // unless this could lead to an overflow.
767   if (!r1->is_con()) {
768     jint lo = r1->_lo, hi = r1->_hi;
769     if (((lo << shift) >> shift) == lo &&
770         ((hi << shift) >> shift) == hi) {
771       // No overflow.  The range shifts up cleanly.
772       return TypeInt::make((jint)lo << (jint)shift,
773                            (jint)hi << (jint)shift,
774                            MAX2(r1->_widen,r2->_widen));
775     }
776     return TypeInt::INT;
777   }
778 
779   return TypeInt::make( (jint)r1->get_con() << (jint)shift );
780 }
781 
782 //=============================================================================
783 //------------------------------Identity---------------------------------------
Identity(PhaseGVN * phase)784 Node* LShiftLNode::Identity(PhaseGVN* phase) {
785   int count = 0;
786   if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaLong - 1)) == 0) {
787     // Shift by a multiple of 64 does nothing
788     return in(1);
789   }
790   return this;
791 }
792 
793 //------------------------------Ideal------------------------------------------
794 // If the right input is a constant, and the left input is an add of a
795 // constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0
Ideal(PhaseGVN * phase,bool can_reshape)796 Node *LShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
797   int con = maskShiftAmount(phase, this, BitsPerJavaLong);
798   if (con == 0) {
799     return NULL;
800   }
801 
802   // Left input is an add of a constant?
803   Node *add1 = in(1);
804   int add1_op = add1->Opcode();
805   if( add1_op == Op_AddL ) {    // Left input is an add?
806     // Avoid dead data cycles from dead loops
807     assert( add1 != add1->in(1), "dead loop in LShiftLNode::Ideal" );
808     const TypeLong *t12 = phase->type(add1->in(2))->isa_long();
809     if( t12 && t12->is_con() ){ // Left input is an add of a con?
810       // Compute X << con0
811       Node *lsh = phase->transform( new LShiftLNode( add1->in(1), in(2) ) );
812       // Compute X<<con0 + (con1<<con0)
813       return new AddLNode( lsh, phase->longcon(t12->get_con() << con));
814     }
815   }
816 
817   // Check for "(x>>c0)<<c0" which just masks off low bits
818   if( (add1_op == Op_RShiftL || add1_op == Op_URShiftL ) &&
819       add1->in(2) == in(2) )
820     // Convert to "(x & -(1<<c0))"
821     return new AndLNode(add1->in(1),phase->longcon( -(CONST64(1)<<con)));
822 
823   // Check for "((x>>c0) & Y)<<c0" which just masks off more low bits
824   if( add1_op == Op_AndL ) {
825     Node *add2 = add1->in(1);
826     int add2_op = add2->Opcode();
827     if( (add2_op == Op_RShiftL || add2_op == Op_URShiftL ) &&
828         add2->in(2) == in(2) ) {
829       // Convert to "(x & (Y<<c0))"
830       Node *y_sh = phase->transform( new LShiftLNode( add1->in(2), in(2) ) );
831       return new AndLNode( add2->in(1), y_sh );
832     }
833   }
834 
835   // Check for ((x & ((CONST64(1)<<(64-c0))-1)) << c0) which ANDs off high bits
836   // before shifting them away.
837   const jlong bits_mask = jlong(max_julong >> con);
838   if( add1_op == Op_AndL &&
839       phase->type(add1->in(2)) == TypeLong::make( bits_mask ) )
840     return new LShiftLNode( add1->in(1), in(2) );
841 
842   return NULL;
843 }
844 
845 //------------------------------Value------------------------------------------
846 // A LShiftLNode shifts its input2 left by input1 amount.
Value(PhaseGVN * phase) const847 const Type* LShiftLNode::Value(PhaseGVN* phase) const {
848   const Type *t1 = phase->type( in(1) );
849   const Type *t2 = phase->type( in(2) );
850   // Either input is TOP ==> the result is TOP
851   if( t1 == Type::TOP ) return Type::TOP;
852   if( t2 == Type::TOP ) return Type::TOP;
853 
854   // Left input is ZERO ==> the result is ZERO.
855   if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
856   // Shift by zero does nothing
857   if( t2 == TypeInt::ZERO ) return t1;
858 
859   // Either input is BOTTOM ==> the result is BOTTOM
860   if( (t1 == TypeLong::LONG) || (t2 == TypeInt::INT) ||
861       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
862     return TypeLong::LONG;
863 
864   const TypeLong *r1 = t1->is_long(); // Handy access
865   const TypeInt  *r2 = t2->is_int();  // Handy access
866 
867   if (!r2->is_con())
868     return TypeLong::LONG;
869 
870   uint shift = r2->get_con();
871   shift &= BitsPerJavaLong - 1;  // semantics of Java shifts
872   // Shift by a multiple of 64 does nothing:
873   if (shift == 0)  return t1;
874 
875   // If the shift is a constant, shift the bounds of the type,
876   // unless this could lead to an overflow.
877   if (!r1->is_con()) {
878     jlong lo = r1->_lo, hi = r1->_hi;
879     if (((lo << shift) >> shift) == lo &&
880         ((hi << shift) >> shift) == hi) {
881       // No overflow.  The range shifts up cleanly.
882       return TypeLong::make((jlong)lo << (jint)shift,
883                             (jlong)hi << (jint)shift,
884                             MAX2(r1->_widen,r2->_widen));
885     }
886     return TypeLong::LONG;
887   }
888 
889   return TypeLong::make( (jlong)r1->get_con() << (jint)shift );
890 }
891 
892 //=============================================================================
893 //------------------------------Identity---------------------------------------
Identity(PhaseGVN * phase)894 Node* RShiftINode::Identity(PhaseGVN* phase) {
895   int count = 0;
896   if (const_shift_count(phase, this, &count)) {
897     if ((count & (BitsPerJavaInteger - 1)) == 0) {
898       // Shift by a multiple of 32 does nothing
899       return in(1);
900     }
901     // Check for useless sign-masking
902     if (in(1)->Opcode() == Op_LShiftI &&
903         in(1)->req() == 3 &&
904         in(1)->in(2) == in(2)) {
905       count &= BitsPerJavaInteger-1; // semantics of Java shifts
906       // Compute masks for which this shifting doesn't change
907       int lo = (-1 << (BitsPerJavaInteger - ((uint)count)-1)); // FFFF8000
908       int hi = ~lo;               // 00007FFF
909       const TypeInt* t11 = phase->type(in(1)->in(1))->isa_int();
910       if (t11 == NULL) {
911         return this;
912       }
913       // Does actual value fit inside of mask?
914       if (lo <= t11->_lo && t11->_hi <= hi) {
915         return in(1)->in(1);      // Then shifting is a nop
916       }
917     }
918   }
919   return this;
920 }
921 
922 //------------------------------Ideal------------------------------------------
Ideal(PhaseGVN * phase,bool can_reshape)923 Node *RShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) {
924   // Inputs may be TOP if they are dead.
925   const TypeInt *t1 = phase->type(in(1))->isa_int();
926   if (!t1) return NULL;        // Left input is an integer
927   const TypeInt *t3;  // type of in(1).in(2)
928   int shift = maskShiftAmount(phase, this, BitsPerJavaInteger);
929   if (shift == 0) {
930     return NULL;
931   }
932 
933   // Check for (x & 0xFF000000) >> 24, whose mask can be made smaller.
934   // Such expressions arise normally from shift chains like (byte)(x >> 24).
935   const Node *mask = in(1);
936   if( mask->Opcode() == Op_AndI &&
937       (t3 = phase->type(mask->in(2))->isa_int()) &&
938       t3->is_con() ) {
939     Node *x = mask->in(1);
940     jint maskbits = t3->get_con();
941     // Convert to "(x >> shift) & (mask >> shift)"
942     Node *shr_nomask = phase->transform( new RShiftINode(mask->in(1), in(2)) );
943     return new AndINode(shr_nomask, phase->intcon( maskbits >> shift));
944   }
945 
946   // Check for "(short[i] <<16)>>16" which simply sign-extends
947   const Node *shl = in(1);
948   if( shl->Opcode() != Op_LShiftI ) return NULL;
949 
950   if( shift == 16 &&
951       (t3 = phase->type(shl->in(2))->isa_int()) &&
952       t3->is_con(16) ) {
953     Node *ld = shl->in(1);
954     if( ld->Opcode() == Op_LoadS ) {
955       // Sign extension is just useless here.  Return a RShiftI of zero instead
956       // returning 'ld' directly.  We cannot return an old Node directly as
957       // that is the job of 'Identity' calls and Identity calls only work on
958       // direct inputs ('ld' is an extra Node removed from 'this').  The
959       // combined optimization requires Identity only return direct inputs.
960       set_req(1, ld);
961       set_req(2, phase->intcon(0));
962       return this;
963     }
964     else if( can_reshape &&
965              ld->Opcode() == Op_LoadUS &&
966              ld->outcnt() == 1 && ld->unique_out() == shl)
967       // Replace zero-extension-load with sign-extension-load
968       return ld->as_Load()->convert_to_signed_load(*phase);
969   }
970 
971   // Check for "(byte[i] <<24)>>24" which simply sign-extends
972   if( shift == 24 &&
973       (t3 = phase->type(shl->in(2))->isa_int()) &&
974       t3->is_con(24) ) {
975     Node *ld = shl->in(1);
976     if( ld->Opcode() == Op_LoadB ) {
977       // Sign extension is just useless here
978       set_req(1, ld);
979       set_req(2, phase->intcon(0));
980       return this;
981     }
982   }
983 
984   return NULL;
985 }
986 
987 //------------------------------Value------------------------------------------
988 // A RShiftINode shifts its input2 right by input1 amount.
Value(PhaseGVN * phase) const989 const Type* RShiftINode::Value(PhaseGVN* phase) const {
990   const Type *t1 = phase->type( in(1) );
991   const Type *t2 = phase->type( in(2) );
992   // Either input is TOP ==> the result is TOP
993   if( t1 == Type::TOP ) return Type::TOP;
994   if( t2 == Type::TOP ) return Type::TOP;
995 
996   // Left input is ZERO ==> the result is ZERO.
997   if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
998   // Shift by zero does nothing
999   if( t2 == TypeInt::ZERO ) return t1;
1000 
1001   // Either input is BOTTOM ==> the result is BOTTOM
1002   if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
1003     return TypeInt::INT;
1004 
1005   if (t2 == TypeInt::INT)
1006     return TypeInt::INT;
1007 
1008   const TypeInt *r1 = t1->is_int(); // Handy access
1009   const TypeInt *r2 = t2->is_int(); // Handy access
1010 
1011   // If the shift is a constant, just shift the bounds of the type.
1012   // For example, if the shift is 31, we just propagate sign bits.
1013   if (r2->is_con()) {
1014     uint shift = r2->get_con();
1015     shift &= BitsPerJavaInteger-1;  // semantics of Java shifts
1016     // Shift by a multiple of 32 does nothing:
1017     if (shift == 0)  return t1;
1018     // Calculate reasonably aggressive bounds for the result.
1019     // This is necessary if we are to correctly type things
1020     // like (x<<24>>24) == ((byte)x).
1021     jint lo = (jint)r1->_lo >> (jint)shift;
1022     jint hi = (jint)r1->_hi >> (jint)shift;
1023     assert(lo <= hi, "must have valid bounds");
1024     const TypeInt* ti = TypeInt::make(lo, hi, MAX2(r1->_widen,r2->_widen));
1025 #ifdef ASSERT
1026     // Make sure we get the sign-capture idiom correct.
1027     if (shift == BitsPerJavaInteger-1) {
1028       if (r1->_lo >= 0) assert(ti == TypeInt::ZERO,    ">>31 of + is  0");
1029       if (r1->_hi <  0) assert(ti == TypeInt::MINUS_1, ">>31 of - is -1");
1030     }
1031 #endif
1032     return ti;
1033   }
1034 
1035   if( !r1->is_con() || !r2->is_con() )
1036     return TypeInt::INT;
1037 
1038   // Signed shift right
1039   return TypeInt::make( r1->get_con() >> (r2->get_con()&31) );
1040 }
1041 
1042 //=============================================================================
1043 //------------------------------Identity---------------------------------------
Identity(PhaseGVN * phase)1044 Node* RShiftLNode::Identity(PhaseGVN* phase) {
1045   const TypeInt *ti = phase->type(in(2))->isa_int(); // Shift count is an int.
1046   return (ti && ti->is_con() && (ti->get_con() & (BitsPerJavaLong - 1)) == 0) ? in(1) : this;
1047 }
1048 
1049 //------------------------------Value------------------------------------------
1050 // A RShiftLNode shifts its input2 right by input1 amount.
Value(PhaseGVN * phase) const1051 const Type* RShiftLNode::Value(PhaseGVN* phase) const {
1052   const Type *t1 = phase->type( in(1) );
1053   const Type *t2 = phase->type( in(2) );
1054   // Either input is TOP ==> the result is TOP
1055   if( t1 == Type::TOP ) return Type::TOP;
1056   if( t2 == Type::TOP ) return Type::TOP;
1057 
1058   // Left input is ZERO ==> the result is ZERO.
1059   if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
1060   // Shift by zero does nothing
1061   if( t2 == TypeInt::ZERO ) return t1;
1062 
1063   // Either input is BOTTOM ==> the result is BOTTOM
1064   if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
1065     return TypeLong::LONG;
1066 
1067   if (t2 == TypeInt::INT)
1068     return TypeLong::LONG;
1069 
1070   const TypeLong *r1 = t1->is_long(); // Handy access
1071   const TypeInt  *r2 = t2->is_int (); // Handy access
1072 
1073   // If the shift is a constant, just shift the bounds of the type.
1074   // For example, if the shift is 63, we just propagate sign bits.
1075   if (r2->is_con()) {
1076     uint shift = r2->get_con();
1077     shift &= (2*BitsPerJavaInteger)-1;  // semantics of Java shifts
1078     // Shift by a multiple of 64 does nothing:
1079     if (shift == 0)  return t1;
1080     // Calculate reasonably aggressive bounds for the result.
1081     // This is necessary if we are to correctly type things
1082     // like (x<<24>>24) == ((byte)x).
1083     jlong lo = (jlong)r1->_lo >> (jlong)shift;
1084     jlong hi = (jlong)r1->_hi >> (jlong)shift;
1085     assert(lo <= hi, "must have valid bounds");
1086     const TypeLong* tl = TypeLong::make(lo, hi, MAX2(r1->_widen,r2->_widen));
1087     #ifdef ASSERT
1088     // Make sure we get the sign-capture idiom correct.
1089     if (shift == (2*BitsPerJavaInteger)-1) {
1090       if (r1->_lo >= 0) assert(tl == TypeLong::ZERO,    ">>63 of + is 0");
1091       if (r1->_hi < 0)  assert(tl == TypeLong::MINUS_1, ">>63 of - is -1");
1092     }
1093     #endif
1094     return tl;
1095   }
1096 
1097   return TypeLong::LONG;                // Give up
1098 }
1099 
1100 //=============================================================================
1101 //------------------------------Identity---------------------------------------
Identity(PhaseGVN * phase)1102 Node* URShiftINode::Identity(PhaseGVN* phase) {
1103   int count = 0;
1104   if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaInteger - 1)) == 0) {
1105     // Shift by a multiple of 32 does nothing
1106     return in(1);
1107   }
1108 
1109   // Check for "((x << LogBytesPerWord) + (wordSize-1)) >> LogBytesPerWord" which is just "x".
1110   // Happens during new-array length computation.
1111   // Safe if 'x' is in the range [0..(max_int>>LogBytesPerWord)]
1112   Node *add = in(1);
1113   if (add->Opcode() == Op_AddI) {
1114     const TypeInt *t2 = phase->type(add->in(2))->isa_int();
1115     if (t2 && t2->is_con(wordSize - 1) &&
1116         add->in(1)->Opcode() == Op_LShiftI) {
1117       // Check that shift_counts are LogBytesPerWord.
1118       Node          *lshift_count   = add->in(1)->in(2);
1119       const TypeInt *t_lshift_count = phase->type(lshift_count)->isa_int();
1120       if (t_lshift_count && t_lshift_count->is_con(LogBytesPerWord) &&
1121           t_lshift_count == phase->type(in(2))) {
1122         Node          *x   = add->in(1)->in(1);
1123         const TypeInt *t_x = phase->type(x)->isa_int();
1124         if (t_x != NULL && 0 <= t_x->_lo && t_x->_hi <= (max_jint>>LogBytesPerWord)) {
1125           return x;
1126         }
1127       }
1128     }
1129   }
1130 
1131   return (phase->type(in(2))->higher_equal(TypeInt::ZERO)) ? in(1) : this;
1132 }
1133 
1134 //------------------------------Ideal------------------------------------------
Ideal(PhaseGVN * phase,bool can_reshape)1135 Node *URShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) {
1136   int con = maskShiftAmount(phase, this, BitsPerJavaInteger);
1137   if (con == 0) {
1138     return NULL;
1139   }
1140 
1141   // We'll be wanting the right-shift amount as a mask of that many bits
1142   const int mask = right_n_bits(BitsPerJavaInteger - con);
1143 
1144   int in1_op = in(1)->Opcode();
1145 
1146   // Check for ((x>>>a)>>>b) and replace with (x>>>(a+b)) when a+b < 32
1147   if( in1_op == Op_URShiftI ) {
1148     const TypeInt *t12 = phase->type( in(1)->in(2) )->isa_int();
1149     if( t12 && t12->is_con() ) { // Right input is a constant
1150       assert( in(1) != in(1)->in(1), "dead loop in URShiftINode::Ideal" );
1151       const int con2 = t12->get_con() & 31; // Shift count is always masked
1152       const int con3 = con+con2;
1153       if( con3 < 32 )           // Only merge shifts if total is < 32
1154         return new URShiftINode( in(1)->in(1), phase->intcon(con3) );
1155     }
1156   }
1157 
1158   // Check for ((x << z) + Y) >>> z.  Replace with x + con>>>z
1159   // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z".
1160   // If Q is "X << z" the rounding is useless.  Look for patterns like
1161   // ((X<<Z) + Y) >>> Z  and replace with (X + Y>>>Z) & Z-mask.
1162   Node *add = in(1);
1163   const TypeInt *t2 = phase->type(in(2))->isa_int();
1164   if (in1_op == Op_AddI) {
1165     Node *lshl = add->in(1);
1166     if( lshl->Opcode() == Op_LShiftI &&
1167         phase->type(lshl->in(2)) == t2 ) {
1168       Node *y_z = phase->transform( new URShiftINode(add->in(2),in(2)) );
1169       Node *sum = phase->transform( new AddINode( lshl->in(1), y_z ) );
1170       return new AndINode( sum, phase->intcon(mask) );
1171     }
1172   }
1173 
1174   // Check for (x & mask) >>> z.  Replace with (x >>> z) & (mask >>> z)
1175   // This shortens the mask.  Also, if we are extracting a high byte and
1176   // storing it to a buffer, the mask will be removed completely.
1177   Node *andi = in(1);
1178   if( in1_op == Op_AndI ) {
1179     const TypeInt *t3 = phase->type( andi->in(2) )->isa_int();
1180     if( t3 && t3->is_con() ) { // Right input is a constant
1181       jint mask2 = t3->get_con();
1182       mask2 >>= con;  // *signed* shift downward (high-order zeroes do not help)
1183       Node *newshr = phase->transform( new URShiftINode(andi->in(1), in(2)) );
1184       return new AndINode(newshr, phase->intcon(mask2));
1185       // The negative values are easier to materialize than positive ones.
1186       // A typical case from address arithmetic is ((x & ~15) >> 4).
1187       // It's better to change that to ((x >> 4) & ~0) versus
1188       // ((x >> 4) & 0x0FFFFFFF).  The difference is greatest in LP64.
1189     }
1190   }
1191 
1192   // Check for "(X << z ) >>> z" which simply zero-extends
1193   Node *shl = in(1);
1194   if( in1_op == Op_LShiftI &&
1195       phase->type(shl->in(2)) == t2 )
1196     return new AndINode( shl->in(1), phase->intcon(mask) );
1197 
1198   // Check for (x >> n) >>> 31. Replace with (x >>> 31)
1199   Node *shr = in(1);
1200   if ( in1_op == Op_RShiftI ) {
1201     Node *in11 = shr->in(1);
1202     Node *in12 = shr->in(2);
1203     const TypeInt *t11 = phase->type(in11)->isa_int();
1204     const TypeInt *t12 = phase->type(in12)->isa_int();
1205     if ( t11 && t2 && t2->is_con(31) && t12 && t12->is_con() ) {
1206       return new URShiftINode(in11, phase->intcon(31));
1207     }
1208   }
1209 
1210   return NULL;
1211 }
1212 
1213 //------------------------------Value------------------------------------------
1214 // A URShiftINode shifts its input2 right by input1 amount.
Value(PhaseGVN * phase) const1215 const Type* URShiftINode::Value(PhaseGVN* phase) const {
1216   // (This is a near clone of RShiftINode::Value.)
1217   const Type *t1 = phase->type( in(1) );
1218   const Type *t2 = phase->type( in(2) );
1219   // Either input is TOP ==> the result is TOP
1220   if( t1 == Type::TOP ) return Type::TOP;
1221   if( t2 == Type::TOP ) return Type::TOP;
1222 
1223   // Left input is ZERO ==> the result is ZERO.
1224   if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
1225   // Shift by zero does nothing
1226   if( t2 == TypeInt::ZERO ) return t1;
1227 
1228   // Either input is BOTTOM ==> the result is BOTTOM
1229   if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
1230     return TypeInt::INT;
1231 
1232   if (t2 == TypeInt::INT)
1233     return TypeInt::INT;
1234 
1235   const TypeInt *r1 = t1->is_int();     // Handy access
1236   const TypeInt *r2 = t2->is_int();     // Handy access
1237 
1238   if (r2->is_con()) {
1239     uint shift = r2->get_con();
1240     shift &= BitsPerJavaInteger-1;  // semantics of Java shifts
1241     // Shift by a multiple of 32 does nothing:
1242     if (shift == 0)  return t1;
1243     // Calculate reasonably aggressive bounds for the result.
1244     jint lo = (juint)r1->_lo >> (juint)shift;
1245     jint hi = (juint)r1->_hi >> (juint)shift;
1246     if (r1->_hi >= 0 && r1->_lo < 0) {
1247       // If the type has both negative and positive values,
1248       // there are two separate sub-domains to worry about:
1249       // The positive half and the negative half.
1250       jint neg_lo = lo;
1251       jint neg_hi = (juint)-1 >> (juint)shift;
1252       jint pos_lo = (juint) 0 >> (juint)shift;
1253       jint pos_hi = hi;
1254       lo = MIN2(neg_lo, pos_lo);  // == 0
1255       hi = MAX2(neg_hi, pos_hi);  // == -1 >>> shift;
1256     }
1257     assert(lo <= hi, "must have valid bounds");
1258     const TypeInt* ti = TypeInt::make(lo, hi, MAX2(r1->_widen,r2->_widen));
1259     #ifdef ASSERT
1260     // Make sure we get the sign-capture idiom correct.
1261     if (shift == BitsPerJavaInteger-1) {
1262       if (r1->_lo >= 0) assert(ti == TypeInt::ZERO, ">>>31 of + is 0");
1263       if (r1->_hi < 0)  assert(ti == TypeInt::ONE,  ">>>31 of - is +1");
1264     }
1265     #endif
1266     return ti;
1267   }
1268 
1269   //
1270   // Do not support shifted oops in info for GC
1271   //
1272   // else if( t1->base() == Type::InstPtr ) {
1273   //
1274   //   const TypeInstPtr *o = t1->is_instptr();
1275   //   if( t1->singleton() )
1276   //     return TypeInt::make( ((uint32_t)o->const_oop() + o->_offset) >> shift );
1277   // }
1278   // else if( t1->base() == Type::KlassPtr ) {
1279   //   const TypeKlassPtr *o = t1->is_klassptr();
1280   //   if( t1->singleton() )
1281   //     return TypeInt::make( ((uint32_t)o->const_oop() + o->_offset) >> shift );
1282   // }
1283 
1284   return TypeInt::INT;
1285 }
1286 
1287 //=============================================================================
1288 //------------------------------Identity---------------------------------------
Identity(PhaseGVN * phase)1289 Node* URShiftLNode::Identity(PhaseGVN* phase) {
1290   int count = 0;
1291   if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaLong - 1)) == 0) {
1292     // Shift by a multiple of 64 does nothing
1293     return in(1);
1294   }
1295   return this;
1296 }
1297 
1298 //------------------------------Ideal------------------------------------------
Ideal(PhaseGVN * phase,bool can_reshape)1299 Node *URShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1300   int con = maskShiftAmount(phase, this, BitsPerJavaLong);
1301   if (con == 0) {
1302     return NULL;
1303   }
1304 
1305   // We'll be wanting the right-shift amount as a mask of that many bits
1306   const jlong mask = jlong(max_julong >> con);
1307 
1308   // Check for ((x << z) + Y) >>> z.  Replace with x + con>>>z
1309   // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z".
1310   // If Q is "X << z" the rounding is useless.  Look for patterns like
1311   // ((X<<Z) + Y) >>> Z  and replace with (X + Y>>>Z) & Z-mask.
1312   Node *add = in(1);
1313   const TypeInt *t2 = phase->type(in(2))->isa_int();
1314   if (add->Opcode() == Op_AddL) {
1315     Node *lshl = add->in(1);
1316     if( lshl->Opcode() == Op_LShiftL &&
1317         phase->type(lshl->in(2)) == t2 ) {
1318       Node *y_z = phase->transform( new URShiftLNode(add->in(2),in(2)) );
1319       Node *sum = phase->transform( new AddLNode( lshl->in(1), y_z ) );
1320       return new AndLNode( sum, phase->longcon(mask) );
1321     }
1322   }
1323 
1324   // Check for (x & mask) >>> z.  Replace with (x >>> z) & (mask >>> z)
1325   // This shortens the mask.  Also, if we are extracting a high byte and
1326   // storing it to a buffer, the mask will be removed completely.
1327   Node *andi = in(1);
1328   if( andi->Opcode() == Op_AndL ) {
1329     const TypeLong *t3 = phase->type( andi->in(2) )->isa_long();
1330     if( t3 && t3->is_con() ) { // Right input is a constant
1331       jlong mask2 = t3->get_con();
1332       mask2 >>= con;  // *signed* shift downward (high-order zeroes do not help)
1333       Node *newshr = phase->transform( new URShiftLNode(andi->in(1), in(2)) );
1334       return new AndLNode(newshr, phase->longcon(mask2));
1335     }
1336   }
1337 
1338   // Check for "(X << z ) >>> z" which simply zero-extends
1339   Node *shl = in(1);
1340   if( shl->Opcode() == Op_LShiftL &&
1341       phase->type(shl->in(2)) == t2 )
1342     return new AndLNode( shl->in(1), phase->longcon(mask) );
1343 
1344   // Check for (x >> n) >>> 63. Replace with (x >>> 63)
1345   Node *shr = in(1);
1346   if ( shr->Opcode() == Op_RShiftL ) {
1347     Node *in11 = shr->in(1);
1348     Node *in12 = shr->in(2);
1349     const TypeLong *t11 = phase->type(in11)->isa_long();
1350     const TypeInt *t12 = phase->type(in12)->isa_int();
1351     if ( t11 && t2 && t2->is_con(63) && t12 && t12->is_con() ) {
1352       return new URShiftLNode(in11, phase->intcon(63));
1353     }
1354   }
1355   return NULL;
1356 }
1357 
1358 //------------------------------Value------------------------------------------
1359 // A URShiftINode shifts its input2 right by input1 amount.
Value(PhaseGVN * phase) const1360 const Type* URShiftLNode::Value(PhaseGVN* phase) const {
1361   // (This is a near clone of RShiftLNode::Value.)
1362   const Type *t1 = phase->type( in(1) );
1363   const Type *t2 = phase->type( in(2) );
1364   // Either input is TOP ==> the result is TOP
1365   if( t1 == Type::TOP ) return Type::TOP;
1366   if( t2 == Type::TOP ) return Type::TOP;
1367 
1368   // Left input is ZERO ==> the result is ZERO.
1369   if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
1370   // Shift by zero does nothing
1371   if( t2 == TypeInt::ZERO ) return t1;
1372 
1373   // Either input is BOTTOM ==> the result is BOTTOM
1374   if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
1375     return TypeLong::LONG;
1376 
1377   if (t2 == TypeInt::INT)
1378     return TypeLong::LONG;
1379 
1380   const TypeLong *r1 = t1->is_long(); // Handy access
1381   const TypeInt  *r2 = t2->is_int (); // Handy access
1382 
1383   if (r2->is_con()) {
1384     uint shift = r2->get_con();
1385     shift &= BitsPerJavaLong - 1;  // semantics of Java shifts
1386     // Shift by a multiple of 64 does nothing:
1387     if (shift == 0)  return t1;
1388     // Calculate reasonably aggressive bounds for the result.
1389     jlong lo = (julong)r1->_lo >> (juint)shift;
1390     jlong hi = (julong)r1->_hi >> (juint)shift;
1391     if (r1->_hi >= 0 && r1->_lo < 0) {
1392       // If the type has both negative and positive values,
1393       // there are two separate sub-domains to worry about:
1394       // The positive half and the negative half.
1395       jlong neg_lo = lo;
1396       jlong neg_hi = (julong)-1 >> (juint)shift;
1397       jlong pos_lo = (julong) 0 >> (juint)shift;
1398       jlong pos_hi = hi;
1399       //lo = MIN2(neg_lo, pos_lo);  // == 0
1400       lo = neg_lo < pos_lo ? neg_lo : pos_lo;
1401       //hi = MAX2(neg_hi, pos_hi);  // == -1 >>> shift;
1402       hi = neg_hi > pos_hi ? neg_hi : pos_hi;
1403     }
1404     assert(lo <= hi, "must have valid bounds");
1405     const TypeLong* tl = TypeLong::make(lo, hi, MAX2(r1->_widen,r2->_widen));
1406     #ifdef ASSERT
1407     // Make sure we get the sign-capture idiom correct.
1408     if (shift == BitsPerJavaLong - 1) {
1409       if (r1->_lo >= 0) assert(tl == TypeLong::ZERO, ">>>63 of + is 0");
1410       if (r1->_hi < 0)  assert(tl == TypeLong::ONE,  ">>>63 of - is +1");
1411     }
1412     #endif
1413     return tl;
1414   }
1415 
1416   return TypeLong::LONG;                // Give up
1417 }
1418 
1419 //=============================================================================
1420 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const1421 const Type* FmaDNode::Value(PhaseGVN* phase) const {
1422   const Type *t1 = phase->type(in(1));
1423   if (t1 == Type::TOP) return Type::TOP;
1424   if (t1->base() != Type::DoubleCon) return Type::DOUBLE;
1425   const Type *t2 = phase->type(in(2));
1426   if (t2 == Type::TOP) return Type::TOP;
1427   if (t2->base() != Type::DoubleCon) return Type::DOUBLE;
1428   const Type *t3 = phase->type(in(3));
1429   if (t3 == Type::TOP) return Type::TOP;
1430   if (t3->base() != Type::DoubleCon) return Type::DOUBLE;
1431 #ifndef __STDC_IEC_559__
1432   return Type::DOUBLE;
1433 #else
1434   double d1 = t1->getd();
1435   double d2 = t2->getd();
1436   double d3 = t3->getd();
1437   return TypeD::make(fma(d1, d2, d3));
1438 #endif
1439 }
1440 
1441 //=============================================================================
1442 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const1443 const Type* FmaFNode::Value(PhaseGVN* phase) const {
1444   const Type *t1 = phase->type(in(1));
1445   if (t1 == Type::TOP) return Type::TOP;
1446   if (t1->base() != Type::FloatCon) return Type::FLOAT;
1447   const Type *t2 = phase->type(in(2));
1448   if (t2 == Type::TOP) return Type::TOP;
1449   if (t2->base() != Type::FloatCon) return Type::FLOAT;
1450   const Type *t3 = phase->type(in(3));
1451   if (t3 == Type::TOP) return Type::TOP;
1452   if (t3->base() != Type::FloatCon) return Type::FLOAT;
1453 #ifndef __STDC_IEC_559__
1454   return Type::FLOAT;
1455 #else
1456   float f1 = t1->getf();
1457   float f2 = t2->getf();
1458   float f3 = t3->getf();
1459   return TypeF::make(fma(f1, f2, f3));
1460 #endif
1461 }
1462 
1463 //=============================================================================
1464 //------------------------------hash-------------------------------------------
1465 // Hash function for MulAddS2INode.  Operation is commutative with commutative pairs.
1466 // The hash function must return the same value when edge swapping is performed.
hash() const1467 uint MulAddS2INode::hash() const {
1468   return (uintptr_t)in(1) + (uintptr_t)in(2) + (uintptr_t)in(3) + (uintptr_t)in(4) + Opcode();
1469 }
1470 
1471 //------------------------------Rotate Operations ------------------------------
1472 
Identity(PhaseGVN * phase)1473 Node* RotateLeftNode::Identity(PhaseGVN* phase) {
1474   const Type* t1 = phase->type(in(1));
1475   if (t1 == Type::TOP) {
1476     return this;
1477   }
1478   int count = 0;
1479   assert(t1->isa_int() || t1->isa_long(), "Unexpected type");
1480   int mask = (t1->isa_int() ? BitsPerJavaInteger : BitsPerJavaLong) - 1;
1481   if (const_shift_count(phase, this, &count) && (count & mask) == 0) {
1482     // Rotate by a multiple of 32/64 does nothing
1483     return in(1);
1484   }
1485   return this;
1486 }
1487 
Value(PhaseGVN * phase) const1488 const Type* RotateLeftNode::Value(PhaseGVN* phase) const {
1489   const Type* t1 = phase->type(in(1));
1490   const Type* t2 = phase->type(in(2));
1491   // Either input is TOP ==> the result is TOP
1492   if (t1 == Type::TOP || t2 == Type::TOP) {
1493     return Type::TOP;
1494   }
1495 
1496   if (t1->isa_int()) {
1497     const TypeInt* r1 = t1->is_int();
1498     const TypeInt* r2 = t2->is_int();
1499 
1500     // Left input is ZERO ==> the result is ZERO.
1501     if (r1 == TypeInt::ZERO) {
1502       return TypeInt::ZERO;
1503     }
1504     // Rotate by zero does nothing
1505     if (r2 == TypeInt::ZERO) {
1506       return r1;
1507     }
1508     if (r1->is_con() && r2->is_con()) {
1509       juint r1_con = (juint)r1->get_con();
1510       juint shift = (juint)(r2->get_con()) & (juint)(BitsPerJavaInteger - 1); // semantics of Java shifts
1511       return TypeInt::make((r1_con << shift) | (r1_con >> (32 - shift)));
1512     }
1513     return TypeInt::INT;
1514   } else {
1515     assert(t1->isa_long(), "Type must be a long");
1516     const TypeLong* r1 = t1->is_long();
1517     const TypeInt*  r2 = t2->is_int();
1518 
1519     // Left input is ZERO ==> the result is ZERO.
1520     if (r1 == TypeLong::ZERO) {
1521       return TypeLong::ZERO;
1522     }
1523     // Rotate by zero does nothing
1524     if (r2 == TypeInt::ZERO) {
1525       return r1;
1526     }
1527     if (r1->is_con() && r2->is_con()) {
1528       julong r1_con = (julong)r1->get_con();
1529       julong shift = (julong)(r2->get_con()) & (julong)(BitsPerJavaLong - 1); // semantics of Java shifts
1530       return TypeLong::make((r1_con << shift) | (r1_con >> (64 - shift)));
1531     }
1532     return TypeLong::LONG;
1533   }
1534 }
1535 
Ideal(PhaseGVN * phase,bool can_reshape)1536 Node* RotateLeftNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1537   const Type* t1 = phase->type(in(1));
1538   const Type* t2 = phase->type(in(2));
1539   if (t2->isa_int() && t2->is_int()->is_con()) {
1540     if (t1->isa_int()) {
1541       int lshift = t2->is_int()->get_con() & 31;
1542       return new RotateRightNode(in(1), phase->intcon(32 - (lshift & 31)), TypeInt::INT);
1543     } else if (t1 != Type::TOP) {
1544       assert(t1->isa_long(), "Type must be a long");
1545       int lshift = t2->is_int()->get_con() & 63;
1546       return new RotateRightNode(in(1), phase->intcon(64 - (lshift & 63)), TypeLong::LONG);
1547     }
1548   }
1549   return NULL;
1550 }
1551 
Identity(PhaseGVN * phase)1552 Node* RotateRightNode::Identity(PhaseGVN* phase) {
1553   const Type* t1 = phase->type(in(1));
1554   if (t1 == Type::TOP) {
1555     return this;
1556   }
1557   int count = 0;
1558   assert(t1->isa_int() || t1->isa_long(), "Unexpected type");
1559   int mask = (t1->isa_int() ? BitsPerJavaInteger : BitsPerJavaLong) - 1;
1560   if (const_shift_count(phase, this, &count) && (count & mask) == 0) {
1561     // Rotate by a multiple of 32/64 does nothing
1562     return in(1);
1563   }
1564   return this;
1565 }
1566 
Value(PhaseGVN * phase) const1567 const Type* RotateRightNode::Value(PhaseGVN* phase) const {
1568   const Type* t1 = phase->type(in(1));
1569   const Type* t2 = phase->type(in(2));
1570   // Either input is TOP ==> the result is TOP
1571   if (t1 == Type::TOP || t2 == Type::TOP) {
1572     return Type::TOP;
1573   }
1574 
1575   if (t1->isa_int()) {
1576     const TypeInt* r1 = t1->is_int();
1577     const TypeInt* r2 = t2->is_int();
1578 
1579     // Left input is ZERO ==> the result is ZERO.
1580     if (r1 == TypeInt::ZERO) {
1581       return TypeInt::ZERO;
1582     }
1583     // Rotate by zero does nothing
1584     if (r2 == TypeInt::ZERO) {
1585       return r1;
1586     }
1587     if (r1->is_con() && r2->is_con()) {
1588       juint r1_con = (juint)r1->get_con();
1589       juint shift = (juint)(r2->get_con()) & (juint)(BitsPerJavaInteger - 1); // semantics of Java shifts
1590       return TypeInt::make((r1_con >> shift) | (r1_con << (32 - shift)));
1591     }
1592     return TypeInt::INT;
1593   } else {
1594     assert(t1->isa_long(), "Type must be a long");
1595     const TypeLong* r1 = t1->is_long();
1596     const TypeInt*  r2 = t2->is_int();
1597     // Left input is ZERO ==> the result is ZERO.
1598     if (r1 == TypeLong::ZERO) {
1599       return TypeLong::ZERO;
1600     }
1601     // Rotate by zero does nothing
1602     if (r2 == TypeInt::ZERO) {
1603       return r1;
1604     }
1605     if (r1->is_con() && r2->is_con()) {
1606       julong r1_con = (julong)r1->get_con();
1607       julong shift = (julong)(r2->get_con()) & (julong)(BitsPerJavaLong - 1); // semantics of Java shifts
1608       return TypeLong::make((r1_con >> shift) | (r1_con << (64 - shift)));
1609     }
1610     return TypeLong::LONG;
1611   }
1612 }
1613