addnode.cpp revision 0:a61af66fc99e
1/*
2 * Copyright 1997-2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25// Portions of code courtesy of Clifford Click
26
27#include "incls/_precompiled.incl"
28#include "incls/_addnode.cpp.incl"
29
30#define MAXFLOAT        ((float)3.40282346638528860e+38)
31
32// Classic Add functionality.  This covers all the usual 'add' behaviors for
33// an algebraic ring.  Add-integer, add-float, add-double, and binary-or are
34// all inherited from this class.  The various identity values are supplied
35// by virtual functions.
36
37
38//=============================================================================
39//------------------------------hash-------------------------------------------
40// Hash function over AddNodes.  Needs to be commutative; i.e., I swap
41// (commute) inputs to AddNodes willy-nilly so the hash function must return
42// the same value in the presence of edge swapping.
43uint AddNode::hash() const {
44  return (uintptr_t)in(1) + (uintptr_t)in(2) + Opcode();
45}
46
47//------------------------------Identity---------------------------------------
48// If either input is a constant 0, return the other input.
49Node *AddNode::Identity( PhaseTransform *phase ) {
50  const Type *zero = add_id();  // The additive identity
51  if( phase->type( in(1) )->higher_equal( zero ) ) return in(2);
52  if( phase->type( in(2) )->higher_equal( zero ) ) return in(1);
53  return this;
54}
55
56//------------------------------commute----------------------------------------
57// Commute operands to move loads and constants to the right.
58static bool commute( Node *add, int con_left, int con_right ) {
59  Node *in1 = add->in(1);
60  Node *in2 = add->in(2);
61
62  // Convert "1+x" into "x+1".
63  // Right is a constant; leave it
64  if( con_right ) return false;
65  // Left is a constant; move it right.
66  if( con_left ) {
67    add->swap_edges(1, 2);
68    return true;
69  }
70
71  // Convert "Load+x" into "x+Load".
72  // Now check for loads
73  if( in2->is_Load() ) return false;
74  // Left is a Load and Right is not; move it right.
75  if( in1->is_Load() ) {
76    add->swap_edges(1, 2);
77    return true;
78  }
79
80  PhiNode *phi;
81  // Check for tight loop increments: Loop-phi of Add of loop-phi
82  if( in1->is_Phi() && (phi = in1->as_Phi()) && !phi->is_copy() && phi->region()->is_Loop() && phi->in(2)==add)
83    return false;
84  if( in2->is_Phi() && (phi = in2->as_Phi()) && !phi->is_copy() && phi->region()->is_Loop() && phi->in(2)==add){
85    add->swap_edges(1, 2);
86    return true;
87  }
88
89  // Otherwise, sort inputs (commutativity) to help value numbering.
90  if( in1->_idx > in2->_idx ) {
91    add->swap_edges(1, 2);
92    return true;
93  }
94  return false;
95}
96
97//------------------------------Idealize---------------------------------------
98// If we get here, we assume we are associative!
99Node *AddNode::Ideal(PhaseGVN *phase, bool can_reshape) {
100  const Type *t1 = phase->type( in(1) );
101  const Type *t2 = phase->type( in(2) );
102  int con_left  = t1->singleton();
103  int con_right = t2->singleton();
104
105  // Check for commutative operation desired
106  if( commute(this,con_left,con_right) ) return this;
107
108  AddNode *progress = NULL;             // Progress flag
109
110  // Convert "(x+1)+2" into "x+(1+2)".  If the right input is a
111  // constant, and the left input is an add of a constant, flatten the
112  // expression tree.
113  Node *add1 = in(1);
114  Node *add2 = in(2);
115  int add1_op = add1->Opcode();
116  int this_op = Opcode();
117  if( con_right && t2 != Type::TOP && // Right input is a constant?
118      add1_op == this_op ) { // Left input is an Add?
119
120    // Type of left _in right input
121    const Type *t12 = phase->type( add1->in(2) );
122    if( t12->singleton() && t12 != Type::TOP ) { // Left input is an add of a constant?
123      // Check for rare case of closed data cycle which can happen inside
124      // unreachable loops. In these cases the computation is undefined.
125#ifdef ASSERT
126      Node *add11    = add1->in(1);
127      int   add11_op = add11->Opcode();
128      if( (add1 == add1->in(1))
129         || (add11_op == this_op && add11->in(1) == add1) ) {
130        assert(false, "dead loop in AddNode::Ideal");
131      }
132#endif
133      // The Add of the flattened expression
134      Node *x1 = add1->in(1);
135      Node *x2 = phase->makecon( add1->as_Add()->add_ring( t2, t12 ));
136      PhaseIterGVN *igvn = phase->is_IterGVN();
137      if( igvn ) {
138        set_req_X(2,x2,igvn);
139        set_req_X(1,x1,igvn);
140      } else {
141        set_req(2,x2);
142        set_req(1,x1);
143      }
144      progress = this;            // Made progress
145      add1 = in(1);
146      add1_op = add1->Opcode();
147    }
148  }
149
150  // Convert "(x+1)+y" into "(x+y)+1".  Push constants down the expression tree.
151  if( add1_op == this_op && !con_right ) {
152    Node *a12 = add1->in(2);
153    const Type *t12 = phase->type( a12 );
154    if( t12->singleton() && t12 != Type::TOP && (add1 != add1->in(1)) ) {
155      add2 = add1->clone();
156      add2->set_req(2, in(2));
157      add2 = phase->transform(add2);
158      set_req(1, add2);
159      set_req(2, a12);
160      progress = this;
161      add2 = a12;
162    }
163  }
164
165  // Convert "x+(y+1)" into "(x+y)+1".  Push constants down the expression tree.
166  int add2_op = add2->Opcode();
167  if( add2_op == this_op && !con_left ) {
168    Node *a22 = add2->in(2);
169    const Type *t22 = phase->type( a22 );
170    if( t22->singleton() && t22 != Type::TOP && (add2 != add2->in(1)) ) {
171      Node *addx = add2->clone();
172      addx->set_req(1, in(1));
173      addx->set_req(2, add2->in(1));
174      addx = phase->transform(addx);
175      set_req(1, addx);
176      set_req(2, a22);
177      progress = this;
178    }
179  }
180
181  return progress;
182}
183
184//------------------------------Value-----------------------------------------
185// An add node sums it's two _in.  If one input is an RSD, we must mixin
186// the other input's symbols.
187const Type *AddNode::Value( PhaseTransform *phase ) const {
188  // Either input is TOP ==> the result is TOP
189  const Type *t1 = phase->type( in(1) );
190  const Type *t2 = phase->type( in(2) );
191  if( t1 == Type::TOP ) return Type::TOP;
192  if( t2 == Type::TOP ) return Type::TOP;
193
194  // Either input is BOTTOM ==> the result is the local BOTTOM
195  const Type *bot = bottom_type();
196  if( (t1 == bot) || (t2 == bot) ||
197      (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
198    return bot;
199
200  // Check for an addition involving the additive identity
201  const Type *tadd = add_of_identity( t1, t2 );
202  if( tadd ) return tadd;
203
204  return add_ring(t1,t2);               // Local flavor of type addition
205}
206
207//------------------------------add_identity-----------------------------------
208// Check for addition of the identity
209const Type *AddNode::add_of_identity( const Type *t1, const Type *t2 ) const {
210  const Type *zero = add_id();  // The additive identity
211  if( t1->higher_equal( zero ) ) return t2;
212  if( t2->higher_equal( zero ) ) return t1;
213
214  return NULL;
215}
216
217
218//=============================================================================
219//------------------------------Idealize---------------------------------------
220Node *AddINode::Ideal(PhaseGVN *phase, bool can_reshape) {
221  int op1 = in(1)->Opcode();
222  int op2 = in(2)->Opcode();
223  // Fold (con1-x)+con2 into (con1+con2)-x
224  if( op1 == Op_SubI ) {
225    const Type *t_sub1 = phase->type( in(1)->in(1) );
226    const Type *t_2    = phase->type( in(2)        );
227    if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP )
228      return new (phase->C, 3) SubINode(phase->makecon( add_ring( t_sub1, t_2 ) ),
229                              in(1)->in(2) );
230    // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"
231    if( op2 == Op_SubI ) {
232      // Check for dead cycle: d = (a-b)+(c-d)
233      assert( in(1)->in(2) != this && in(2)->in(2) != this,
234              "dead loop in AddINode::Ideal" );
235      Node *sub  = new (phase->C, 3) SubINode(NULL, NULL);
236      sub->init_req(1, phase->transform(new (phase->C, 3) AddINode(in(1)->in(1), in(2)->in(1) ) ));
237      sub->init_req(2, phase->transform(new (phase->C, 3) AddINode(in(1)->in(2), in(2)->in(2) ) ));
238      return sub;
239    }
240  }
241
242  // Convert "x+(0-y)" into "(x-y)"
243  if( op2 == Op_SubI && phase->type(in(2)->in(1)) == TypeInt::ZERO )
244    return new (phase->C, 3) SubINode(in(1), in(2)->in(2) );
245
246  // Convert "(0-y)+x" into "(x-y)"
247  if( op1 == Op_SubI && phase->type(in(1)->in(1)) == TypeInt::ZERO )
248    return new (phase->C, 3) SubINode( in(2), in(1)->in(2) );
249
250  // Convert (x>>>z)+y into (x+(y<<z))>>>z for small constant z and y.
251  // Helps with array allocation math constant folding
252  // See 4790063:
253  // Unrestricted transformation is unsafe for some runtime values of 'x'
254  // ( x ==  0, z == 1, y == -1 ) fails
255  // ( x == -5, z == 1, y ==  1 ) fails
256  // Transform works for small z and small negative y when the addition
257  // (x + (y << z)) does not cross zero.
258  // Implement support for negative y and (x >= -(y << z))
259  // Have not observed cases where type information exists to support
260  // positive y and (x <= -(y << z))
261  if( op1 == Op_URShiftI && op2 == Op_ConI &&
262      in(1)->in(2)->Opcode() == Op_ConI ) {
263    jint z = phase->type( in(1)->in(2) )->is_int()->get_con() & 0x1f; // only least significant 5 bits matter
264    jint y = phase->type( in(2) )->is_int()->get_con();
265
266    if( z < 5 && -5 < y && y < 0 ) {
267      const Type *t_in11 = phase->type(in(1)->in(1));
268      if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z)) ) {
269        Node *a = phase->transform( new (phase->C, 3) AddINode( in(1)->in(1), phase->intcon(y<<z) ) );
270        return new (phase->C, 3) URShiftINode( a, in(1)->in(2) );
271      }
272    }
273  }
274
275  return AddNode::Ideal(phase, can_reshape);
276}
277
278
279//------------------------------Identity---------------------------------------
280// Fold (x-y)+y  OR  y+(x-y)  into  x
281Node *AddINode::Identity( PhaseTransform *phase ) {
282  if( in(1)->Opcode() == Op_SubI && phase->eqv(in(1)->in(2),in(2)) ) {
283    return in(1)->in(1);
284  }
285  else if( in(2)->Opcode() == Op_SubI && phase->eqv(in(2)->in(2),in(1)) ) {
286    return in(2)->in(1);
287  }
288  return AddNode::Identity(phase);
289}
290
291
292//------------------------------add_ring---------------------------------------
293// Supplied function returns the sum of the inputs.  Guaranteed never
294// to be passed a TOP or BOTTOM type, these are filtered out by
295// pre-check.
296const Type *AddINode::add_ring( const Type *t0, const Type *t1 ) const {
297  const TypeInt *r0 = t0->is_int(); // Handy access
298  const TypeInt *r1 = t1->is_int();
299  int lo = r0->_lo + r1->_lo;
300  int hi = r0->_hi + r1->_hi;
301  if( !(r0->is_con() && r1->is_con()) ) {
302    // Not both constants, compute approximate result
303    if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
304      lo = min_jint; hi = max_jint; // Underflow on the low side
305    }
306    if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
307      lo = min_jint; hi = max_jint; // Overflow on the high side
308    }
309    if( lo > hi ) {               // Handle overflow
310      lo = min_jint; hi = max_jint;
311    }
312  } else {
313    // both constants, compute precise result using 'lo' and 'hi'
314    // Semantics define overflow and underflow for integer addition
315    // as expected.  In particular: 0x80000000 + 0x80000000 --> 0x0
316  }
317  return TypeInt::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
318}
319
320
321//=============================================================================
322//------------------------------Idealize---------------------------------------
323Node *AddLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
324  int op1 = in(1)->Opcode();
325  int op2 = in(2)->Opcode();
326  // Fold (con1-x)+con2 into (con1+con2)-x
327  if( op1 == Op_SubL ) {
328    const Type *t_sub1 = phase->type( in(1)->in(1) );
329    const Type *t_2    = phase->type( in(2)        );
330    if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP )
331      return new (phase->C, 3) SubLNode(phase->makecon( add_ring( t_sub1, t_2 ) ),
332                              in(1)->in(2) );
333    // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"
334    if( op2 == Op_SubL ) {
335      // Check for dead cycle: d = (a-b)+(c-d)
336      assert( in(1)->in(2) != this && in(2)->in(2) != this,
337              "dead loop in AddLNode::Ideal" );
338      Node *sub  = new (phase->C, 3) SubLNode(NULL, NULL);
339      sub->init_req(1, phase->transform(new (phase->C, 3) AddLNode(in(1)->in(1), in(2)->in(1) ) ));
340      sub->init_req(2, phase->transform(new (phase->C, 3) AddLNode(in(1)->in(2), in(2)->in(2) ) ));
341      return sub;
342    }
343  }
344
345  // Convert "x+(0-y)" into "(x-y)"
346  if( op2 == Op_SubL && phase->type(in(2)->in(1)) == TypeLong::ZERO )
347    return new (phase->C, 3) SubLNode(in(1), in(2)->in(2) );
348
349  // Convert "X+X+X+X+X...+X+Y" into "k*X+Y" or really convert "X+(X+Y)"
350  // into "(X<<1)+Y" and let shift-folding happen.
351  if( op2 == Op_AddL &&
352      in(2)->in(1) == in(1) &&
353      op1 != Op_ConL &&
354      0 ) {
355    Node *shift = phase->transform(new (phase->C, 3) LShiftLNode(in(1),phase->intcon(1)));
356    return new (phase->C, 3) AddLNode(shift,in(2)->in(2));
357  }
358
359  return AddNode::Ideal(phase, can_reshape);
360}
361
362
363//------------------------------Identity---------------------------------------
364// Fold (x-y)+y  OR  y+(x-y)  into  x
365Node *AddLNode::Identity( PhaseTransform *phase ) {
366  if( in(1)->Opcode() == Op_SubL && phase->eqv(in(1)->in(2),in(2)) ) {
367    return in(1)->in(1);
368  }
369  else if( in(2)->Opcode() == Op_SubL && phase->eqv(in(2)->in(2),in(1)) ) {
370    return in(2)->in(1);
371  }
372  return AddNode::Identity(phase);
373}
374
375
376//------------------------------add_ring---------------------------------------
377// Supplied function returns the sum of the inputs.  Guaranteed never
378// to be passed a TOP or BOTTOM type, these are filtered out by
379// pre-check.
380const Type *AddLNode::add_ring( const Type *t0, const Type *t1 ) const {
381  const TypeLong *r0 = t0->is_long(); // Handy access
382  const TypeLong *r1 = t1->is_long();
383  jlong lo = r0->_lo + r1->_lo;
384  jlong hi = r0->_hi + r1->_hi;
385  if( !(r0->is_con() && r1->is_con()) ) {
386    // Not both constants, compute approximate result
387    if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
388      lo =min_jlong; hi = max_jlong; // Underflow on the low side
389    }
390    if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
391      lo = min_jlong; hi = max_jlong; // Overflow on the high side
392    }
393    if( lo > hi ) {               // Handle overflow
394      lo = min_jlong; hi = max_jlong;
395    }
396  } else {
397    // both constants, compute precise result using 'lo' and 'hi'
398    // Semantics define overflow and underflow for integer addition
399    // as expected.  In particular: 0x80000000 + 0x80000000 --> 0x0
400  }
401  return TypeLong::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
402}
403
404
405//=============================================================================
406//------------------------------add_of_identity--------------------------------
407// Check for addition of the identity
408const Type *AddFNode::add_of_identity( const Type *t1, const Type *t2 ) const {
409  // x ADD 0  should return x unless 'x' is a -zero
410  //
411  // const Type *zero = add_id();     // The additive identity
412  // jfloat f1 = t1->getf();
413  // jfloat f2 = t2->getf();
414  //
415  // if( t1->higher_equal( zero ) ) return t2;
416  // if( t2->higher_equal( zero ) ) return t1;
417
418  return NULL;
419}
420
421//------------------------------add_ring---------------------------------------
422// Supplied function returns the sum of the inputs.
423// This also type-checks the inputs for sanity.  Guaranteed never to
424// be passed a TOP or BOTTOM type, these are filtered out by pre-check.
425const Type *AddFNode::add_ring( const Type *t0, const Type *t1 ) const {
426  // We must be adding 2 float constants.
427  return TypeF::make( t0->getf() + t1->getf() );
428}
429
430//------------------------------Ideal------------------------------------------
431Node *AddFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
432  if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
433    return AddNode::Ideal(phase, can_reshape); // commutative and associative transforms
434  }
435
436  // Floating point additions are not associative because of boundary conditions (infinity)
437  return commute(this,
438                 phase->type( in(1) )->singleton(),
439                 phase->type( in(2) )->singleton() ) ? this : NULL;
440}
441
442
443//=============================================================================
444//------------------------------add_of_identity--------------------------------
445// Check for addition of the identity
446const Type *AddDNode::add_of_identity( const Type *t1, const Type *t2 ) const {
447  // x ADD 0  should return x unless 'x' is a -zero
448  //
449  // const Type *zero = add_id();     // The additive identity
450  // jfloat f1 = t1->getf();
451  // jfloat f2 = t2->getf();
452  //
453  // if( t1->higher_equal( zero ) ) return t2;
454  // if( t2->higher_equal( zero ) ) return t1;
455
456  return NULL;
457}
458//------------------------------add_ring---------------------------------------
459// Supplied function returns the sum of the inputs.
460// This also type-checks the inputs for sanity.  Guaranteed never to
461// be passed a TOP or BOTTOM type, these are filtered out by pre-check.
462const Type *AddDNode::add_ring( const Type *t0, const Type *t1 ) const {
463  // We must be adding 2 double constants.
464  return TypeD::make( t0->getd() + t1->getd() );
465}
466
467//------------------------------Ideal------------------------------------------
468Node *AddDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
469  if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
470    return AddNode::Ideal(phase, can_reshape); // commutative and associative transforms
471  }
472
473  // Floating point additions are not associative because of boundary conditions (infinity)
474  return commute(this,
475                 phase->type( in(1) )->singleton(),
476                 phase->type( in(2) )->singleton() ) ? this : NULL;
477}
478
479
480//=============================================================================
481//------------------------------Identity---------------------------------------
482// If one input is a constant 0, return the other input.
483Node *AddPNode::Identity( PhaseTransform *phase ) {
484  return ( phase->type( in(Offset) )->higher_equal( TypeX_ZERO ) ) ? in(Address) : this;
485}
486
487//------------------------------Idealize---------------------------------------
488Node *AddPNode::Ideal(PhaseGVN *phase, bool can_reshape) {
489  // Bail out if dead inputs
490  if( phase->type( in(Address) ) == Type::TOP ) return NULL;
491
492  // If the left input is an add of a constant, flatten the expression tree.
493  const Node *n = in(Address);
494  if (n->is_AddP() && n->in(Base) == in(Base)) {
495    const AddPNode *addp = n->as_AddP(); // Left input is an AddP
496    assert( !addp->in(Address)->is_AddP() ||
497             addp->in(Address)->as_AddP() != addp,
498            "dead loop in AddPNode::Ideal" );
499    // Type of left input's right input
500    const Type *t = phase->type( addp->in(Offset) );
501    if( t == Type::TOP ) return NULL;
502    const TypeX *t12 = t->is_intptr_t();
503    if( t12->is_con() ) {       // Left input is an add of a constant?
504      // If the right input is a constant, combine constants
505      const Type *temp_t2 = phase->type( in(Offset) );
506      if( temp_t2 == Type::TOP ) return NULL;
507      const TypeX *t2 = temp_t2->is_intptr_t();
508      if( t2->is_con() ) {
509        // The Add of the flattened expression
510        set_req(Address, addp->in(Address));
511        set_req(Offset , phase->MakeConX(t2->get_con() + t12->get_con()));
512        return this;                    // Made progress
513      }
514      // Else move the constant to the right.  ((A+con)+B) into ((A+B)+con)
515      set_req(Address, phase->transform(new (phase->C, 4) AddPNode(in(Base),addp->in(Address),in(Offset))));
516      set_req(Offset , addp->in(Offset));
517      return this;
518    }
519  }
520
521  // Raw pointers?
522  if( in(Base)->bottom_type() == Type::TOP ) {
523    // If this is a NULL+long form (from unsafe accesses), switch to a rawptr.
524    if (phase->type(in(Address)) == TypePtr::NULL_PTR) {
525      Node* offset = in(Offset);
526      return new (phase->C, 2) CastX2PNode(offset);
527    }
528  }
529
530  // If the right is an add of a constant, push the offset down.
531  // Convert: (ptr + (offset+con)) into (ptr+offset)+con.
532  // The idea is to merge array_base+scaled_index groups together,
533  // and only have different constant offsets from the same base.
534  const Node *add = in(Offset);
535  if( add->Opcode() == Op_AddX && add->in(1) != add ) {
536    const Type *t22 = phase->type( add->in(2) );
537    if( t22->singleton() && (t22 != Type::TOP) ) {  // Right input is an add of a constant?
538      set_req(Address, phase->transform(new (phase->C, 4) AddPNode(in(Base),in(Address),add->in(1))));
539      set_req(Offset, add->in(2));
540      return this;              // Made progress
541    }
542  }
543
544  return NULL;                  // No progress
545}
546
547//------------------------------bottom_type------------------------------------
548// Bottom-type is the pointer-type with unknown offset.
549const Type *AddPNode::bottom_type() const {
550  if (in(Address) == NULL)  return TypePtr::BOTTOM;
551  const TypePtr *tp = in(Address)->bottom_type()->isa_ptr();
552  if( !tp ) return Type::TOP;   // TOP input means TOP output
553  assert( in(Offset)->Opcode() != Op_ConP, "" );
554  const Type *t = in(Offset)->bottom_type();
555  if( t == Type::TOP )
556    return tp->add_offset(Type::OffsetTop);
557  const TypeX *tx = t->is_intptr_t();
558  intptr_t txoffset = Type::OffsetBot;
559  if (tx->is_con()) {   // Left input is an add of a constant?
560    txoffset = tx->get_con();
561    if (txoffset != (int)txoffset)
562      txoffset = Type::OffsetBot;   // oops:  add_offset will choke on it
563  }
564  return tp->add_offset(txoffset);
565}
566
567//------------------------------Value------------------------------------------
568const Type *AddPNode::Value( PhaseTransform *phase ) const {
569  // Either input is TOP ==> the result is TOP
570  const Type *t1 = phase->type( in(Address) );
571  const Type *t2 = phase->type( in(Offset) );
572  if( t1 == Type::TOP ) return Type::TOP;
573  if( t2 == Type::TOP ) return Type::TOP;
574
575  // Left input is a pointer
576  const TypePtr *p1 = t1->isa_ptr();
577  // Right input is an int
578  const TypeX *p2 = t2->is_intptr_t();
579  // Add 'em
580  intptr_t p2offset = Type::OffsetBot;
581  if (p2->is_con()) {   // Left input is an add of a constant?
582    p2offset = p2->get_con();
583    if (p2offset != (int)p2offset)
584      p2offset = Type::OffsetBot;   // oops:  add_offset will choke on it
585  }
586  return p1->add_offset(p2offset);
587}
588
589//------------------------Ideal_base_and_offset--------------------------------
590// Split an oop pointer into a base and offset.
591// (The offset might be Type::OffsetBot in the case of an array.)
592// Return the base, or NULL if failure.
593Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseTransform* phase,
594                                      // second return value:
595                                      intptr_t& offset) {
596  if (ptr->is_AddP()) {
597    Node* base = ptr->in(AddPNode::Base);
598    Node* addr = ptr->in(AddPNode::Address);
599    Node* offs = ptr->in(AddPNode::Offset);
600    if (base == addr || base->is_top()) {
601      offset = phase->find_intptr_t_con(offs, Type::OffsetBot);
602      if (offset != Type::OffsetBot) {
603        return addr;
604      }
605    }
606  }
607  offset = Type::OffsetBot;
608  return NULL;
609}
610
611//------------------------------match_edge-------------------------------------
612// Do we Match on this edge index or not?  Do not match base pointer edge
613uint AddPNode::match_edge(uint idx) const {
614  return idx > Base;
615}
616
617//---------------------------mach_bottom_type----------------------------------
618// Utility function for use by ADLC.  Implements bottom_type for matched AddP.
619const Type *AddPNode::mach_bottom_type( const MachNode* n) {
620  Node* base = n->in(Base);
621  const Type *t = base->bottom_type();
622  if ( t == Type::TOP ) {
623    // an untyped pointer
624    return TypeRawPtr::BOTTOM;
625  }
626  const TypePtr* tp = t->isa_oopptr();
627  if ( tp == NULL )  return t;
628  if ( tp->_offset == TypePtr::OffsetBot )  return tp;
629
630  // We must carefully add up the various offsets...
631  intptr_t offset = 0;
632  const TypePtr* tptr = NULL;
633
634  uint numopnds = n->num_opnds();
635  uint index = n->oper_input_base();
636  for ( uint i = 1; i < numopnds; i++ ) {
637    MachOper *opnd = n->_opnds[i];
638    // Check for any interesting operand info.
639    // In particular, check for both memory and non-memory operands.
640    // %%%%% Clean this up: use xadd_offset
641    int con = opnd->constant();
642    if ( con == TypePtr::OffsetBot )  goto bottom_out;
643    offset += con;
644    con = opnd->constant_disp();
645    if ( con == TypePtr::OffsetBot )  goto bottom_out;
646    offset += con;
647    if( opnd->scale() != 0 ) goto bottom_out;
648
649    // Check each operand input edge.  Find the 1 allowed pointer
650    // edge.  Other edges must be index edges; track exact constant
651    // inputs and otherwise assume the worst.
652    for ( uint j = opnd->num_edges(); j > 0; j-- ) {
653      Node* edge = n->in(index++);
654      const Type*    et  = edge->bottom_type();
655      const TypeX*   eti = et->isa_intptr_t();
656      if ( eti == NULL ) {
657        // there must be one pointer among the operands
658        guarantee(tptr == NULL, "must be only one pointer operand");
659        tptr = et->isa_oopptr();
660        guarantee(tptr != NULL, "non-int operand must be pointer");
661        continue;
662      }
663      if ( eti->_hi != eti->_lo )  goto bottom_out;
664      offset += eti->_lo;
665    }
666  }
667  guarantee(tptr != NULL, "must be exactly one pointer operand");
668  return tptr->add_offset(offset);
669
670 bottom_out:
671  return tp->add_offset(TypePtr::OffsetBot);
672}
673
674//=============================================================================
675//------------------------------Identity---------------------------------------
676Node *OrINode::Identity( PhaseTransform *phase ) {
677  // x | x => x
678  if (phase->eqv(in(1), in(2))) {
679    return in(1);
680  }
681
682  return AddNode::Identity(phase);
683}
684
685//------------------------------add_ring---------------------------------------
686// Supplied function returns the sum of the inputs IN THE CURRENT RING.  For
687// the logical operations the ring's ADD is really a logical OR function.
688// This also type-checks the inputs for sanity.  Guaranteed never to
689// be passed a TOP or BOTTOM type, these are filtered out by pre-check.
690const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const {
691  const TypeInt *r0 = t0->is_int(); // Handy access
692  const TypeInt *r1 = t1->is_int();
693
694  // If both args are bool, can figure out better types
695  if ( r0 == TypeInt::BOOL ) {
696    if ( r1 == TypeInt::ONE) {
697      return TypeInt::ONE;
698    } else if ( r1 == TypeInt::BOOL ) {
699      return TypeInt::BOOL;
700    }
701  } else if ( r0 == TypeInt::ONE ) {
702    if ( r1 == TypeInt::BOOL ) {
703      return TypeInt::ONE;
704    }
705  }
706
707  // If either input is not a constant, just return all integers.
708  if( !r0->is_con() || !r1->is_con() )
709    return TypeInt::INT;        // Any integer, but still no symbols.
710
711  // Otherwise just OR them bits.
712  return TypeInt::make( r0->get_con() | r1->get_con() );
713}
714
715//=============================================================================
716//------------------------------Identity---------------------------------------
717Node *OrLNode::Identity( PhaseTransform *phase ) {
718  // x | x => x
719  if (phase->eqv(in(1), in(2))) {
720    return in(1);
721  }
722
723  return AddNode::Identity(phase);
724}
725
726//------------------------------add_ring---------------------------------------
727const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const {
728  const TypeLong *r0 = t0->is_long(); // Handy access
729  const TypeLong *r1 = t1->is_long();
730
731  // If either input is not a constant, just return all integers.
732  if( !r0->is_con() || !r1->is_con() )
733    return TypeLong::LONG;      // Any integer, but still no symbols.
734
735  // Otherwise just OR them bits.
736  return TypeLong::make( r0->get_con() | r1->get_con() );
737}
738
739//=============================================================================
740//------------------------------add_ring---------------------------------------
741// Supplied function returns the sum of the inputs IN THE CURRENT RING.  For
742// the logical operations the ring's ADD is really a logical OR function.
743// This also type-checks the inputs for sanity.  Guaranteed never to
744// be passed a TOP or BOTTOM type, these are filtered out by pre-check.
745const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const {
746  const TypeInt *r0 = t0->is_int(); // Handy access
747  const TypeInt *r1 = t1->is_int();
748
749  // Complementing a boolean?
750  if( r0 == TypeInt::BOOL && ( r1 == TypeInt::ONE
751                               || r1 == TypeInt::BOOL))
752    return TypeInt::BOOL;
753
754  if( !r0->is_con() || !r1->is_con() ) // Not constants
755    return TypeInt::INT;        // Any integer, but still no symbols.
756
757  // Otherwise just XOR them bits.
758  return TypeInt::make( r0->get_con() ^ r1->get_con() );
759}
760
761//=============================================================================
762//------------------------------add_ring---------------------------------------
763const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const {
764  const TypeLong *r0 = t0->is_long(); // Handy access
765  const TypeLong *r1 = t1->is_long();
766
767  // If either input is not a constant, just return all integers.
768  if( !r0->is_con() || !r1->is_con() )
769    return TypeLong::LONG;      // Any integer, but still no symbols.
770
771  // Otherwise just OR them bits.
772  return TypeLong::make( r0->get_con() ^ r1->get_con() );
773}
774
775//=============================================================================
776//------------------------------add_ring---------------------------------------
777// Supplied function returns the sum of the inputs.
778const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const {
779  const TypeInt *r0 = t0->is_int(); // Handy access
780  const TypeInt *r1 = t1->is_int();
781
782  // Otherwise just MAX them bits.
783  return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
784}
785
786//=============================================================================
787//------------------------------Idealize---------------------------------------
788// MINs show up in range-check loop limit calculations.  Look for
789// "MIN2(x+c0,MIN2(y,x+c1))".  Pick the smaller constant: "MIN2(x+c0,y)"
790Node *MinINode::Ideal(PhaseGVN *phase, bool can_reshape) {
791  Node *progress = NULL;
792  // Force a right-spline graph
793  Node *l = in(1);
794  Node *r = in(2);
795  // Transform  MinI1( MinI2(a,b), c)  into  MinI1( a, MinI2(b,c) )
796  // to force a right-spline graph for the rest of MinINode::Ideal().
797  if( l->Opcode() == Op_MinI ) {
798    assert( l != l->in(1), "dead loop in MinINode::Ideal" );
799    r = phase->transform(new (phase->C, 3) MinINode(l->in(2),r));
800    l = l->in(1);
801    set_req(1, l);
802    set_req(2, r);
803    return this;
804  }
805
806  // Get left input & constant
807  Node *x = l;
808  int x_off = 0;
809  if( x->Opcode() == Op_AddI && // Check for "x+c0" and collect constant
810      x->in(2)->is_Con() ) {
811    const Type *t = x->in(2)->bottom_type();
812    if( t == Type::TOP ) return NULL;  // No progress
813    x_off = t->is_int()->get_con();
814    x = x->in(1);
815  }
816
817  // Scan a right-spline-tree for MINs
818  Node *y = r;
819  int y_off = 0;
820  // Check final part of MIN tree
821  if( y->Opcode() == Op_AddI && // Check for "y+c1" and collect constant
822      y->in(2)->is_Con() ) {
823    const Type *t = y->in(2)->bottom_type();
824    if( t == Type::TOP ) return NULL;  // No progress
825    y_off = t->is_int()->get_con();
826    y = y->in(1);
827  }
828  if( x->_idx > y->_idx && r->Opcode() != Op_MinI ) {
829    swap_edges(1, 2);
830    return this;
831  }
832
833
834  if( r->Opcode() == Op_MinI ) {
835    assert( r != r->in(2), "dead loop in MinINode::Ideal" );
836    y = r->in(1);
837    // Check final part of MIN tree
838    if( y->Opcode() == Op_AddI &&// Check for "y+c1" and collect constant
839        y->in(2)->is_Con() ) {
840      const Type *t = y->in(2)->bottom_type();
841      if( t == Type::TOP ) return NULL;  // No progress
842      y_off = t->is_int()->get_con();
843      y = y->in(1);
844    }
845
846    if( x->_idx > y->_idx )
847      return new (phase->C, 3) MinINode(r->in(1),phase->transform(new (phase->C, 3) MinINode(l,r->in(2))));
848
849    // See if covers: MIN2(x+c0,MIN2(y+c1,z))
850    if( !phase->eqv(x,y) ) return NULL;
851    // If (y == x) transform MIN2(x+c0, MIN2(x+c1,z)) into
852    // MIN2(x+c0 or x+c1 which less, z).
853    return new (phase->C, 3) MinINode(phase->transform(new (phase->C, 3) AddINode(x,phase->intcon(MIN2(x_off,y_off)))),r->in(2));
854  } else {
855    // See if covers: MIN2(x+c0,y+c1)
856    if( !phase->eqv(x,y) ) return NULL;
857    // If (y == x) transform MIN2(x+c0,x+c1) into x+c0 or x+c1 which less.
858    return new (phase->C, 3) AddINode(x,phase->intcon(MIN2(x_off,y_off)));
859  }
860
861}
862
863//------------------------------add_ring---------------------------------------
864// Supplied function returns the sum of the inputs.
865const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {
866  const TypeInt *r0 = t0->is_int(); // Handy access
867  const TypeInt *r1 = t1->is_int();
868
869  // Otherwise just MIN them bits.
870  return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
871}
872