1/*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25#include "precompiled.hpp"
26#include "opto/addnode.hpp"
27#include "opto/callnode.hpp"
28#include "opto/castnode.hpp"
29#include "opto/connode.hpp"
30#include "opto/matcher.hpp"
31#include "opto/phaseX.hpp"
32#include "opto/subnode.hpp"
33#include "opto/type.hpp"
34
35//=============================================================================
36// If input is already higher or equal to cast type, then this is an identity.
37Node* ConstraintCastNode::Identity(PhaseGVN* phase) {
38  Node* dom = dominating_cast(phase);
39  if (dom != NULL) {
40    return dom;
41  }
42  if (_carry_dependency) {
43    return this;
44  }
45  return phase->type(in(1))->higher_equal_speculative(_type) ? in(1) : this;
46}
47
48//------------------------------Value------------------------------------------
49// Take 'join' of input and cast-up type
50const Type* ConstraintCastNode::Value(PhaseGVN* phase) const {
51  if (in(0) && phase->type(in(0)) == Type::TOP) return Type::TOP;
52  const Type* ft = phase->type(in(1))->filter_speculative(_type);
53
54#ifdef ASSERT
55  // Previous versions of this function had some special case logic,
56  // which is no longer necessary.  Make sure of the required effects.
57  switch (Opcode()) {
58    case Op_CastII:
59    {
60      const Type* t1 = phase->type(in(1));
61      if( t1 == Type::TOP )  assert(ft == Type::TOP, "special case #1");
62      const Type* rt = t1->join_speculative(_type);
63      if (rt->empty())       assert(ft == Type::TOP, "special case #2");
64      break;
65    }
66    case Op_CastPP:
67    if (phase->type(in(1)) == TypePtr::NULL_PTR &&
68        _type->isa_ptr() && _type->is_ptr()->_ptr == TypePtr::NotNull)
69    assert(ft == Type::TOP, "special case #3");
70    break;
71  }
72#endif //ASSERT
73
74  return ft;
75}
76
77//------------------------------Ideal------------------------------------------
78// Return a node which is more "ideal" than the current node.  Strip out
79// control copies
80Node *ConstraintCastNode::Ideal(PhaseGVN *phase, bool can_reshape) {
81  return (in(0) && remove_dead_region(phase, can_reshape)) ? this : NULL;
82}
83
84uint ConstraintCastNode::cmp(const Node &n) const {
85  return TypeNode::cmp(n) && ((ConstraintCastNode&)n)._carry_dependency == _carry_dependency;
86}
87
88uint ConstraintCastNode::size_of() const {
89  return sizeof(*this);
90}
91
92Node* ConstraintCastNode::make_cast(int opcode, Node* c, Node *n, const Type *t, bool carry_dependency) {
93  switch(opcode) {
94  case Op_CastII: {
95    Node* cast = new CastIINode(n, t, carry_dependency);
96    cast->set_req(0, c);
97    return cast;
98  }
99  case Op_CastPP: {
100    Node* cast = new CastPPNode(n, t, carry_dependency);
101    cast->set_req(0, c);
102    return cast;
103  }
104  case Op_CheckCastPP: return new CheckCastPPNode(c, n, t, carry_dependency);
105  default:
106    fatal("Bad opcode %d", opcode);
107  }
108  return NULL;
109}
110
111TypeNode* ConstraintCastNode::dominating_cast(PhaseTransform *phase) const {
112  Node* val = in(1);
113  Node* ctl = in(0);
114  int opc = Opcode();
115  if (ctl == NULL) {
116    return NULL;
117  }
118  // Range check CastIIs may all end up under a single range check and
119  // in that case only the narrower CastII would be kept by the code
120  // below which would be incorrect.
121  if (is_CastII() && as_CastII()->has_range_check()) {
122    return NULL;
123  }
124  for (DUIterator_Fast imax, i = val->fast_outs(imax); i < imax; i++) {
125    Node* u = val->fast_out(i);
126    if (u != this &&
127        u->outcnt() > 0 &&
128        u->Opcode() == opc &&
129        u->in(0) != NULL &&
130        u->bottom_type()->higher_equal(type())) {
131      if (phase->is_dominator(u->in(0), ctl)) {
132        return u->as_Type();
133      }
134      if (is_CheckCastPP() && u->in(1)->is_Proj() && u->in(1)->in(0)->is_Allocate() &&
135          u->in(0)->is_Proj() && u->in(0)->in(0)->is_Initialize() &&
136          u->in(1)->in(0)->as_Allocate()->initialization() == u->in(0)->in(0)) {
137        // CheckCastPP following an allocation always dominates all
138        // use of the allocation result
139        return u->as_Type();
140      }
141    }
142  }
143  return NULL;
144}
145
146#ifndef PRODUCT
147void ConstraintCastNode::dump_spec(outputStream *st) const {
148  TypeNode::dump_spec(st);
149  if (_carry_dependency) {
150    st->print(" carry dependency");
151  }
152}
153#endif
154
155const Type* CastIINode::Value(PhaseGVN* phase) const {
156  const Type *res = ConstraintCastNode::Value(phase);
157
158  // Try to improve the type of the CastII if we recognize a CmpI/If
159  // pattern.
160  if (_carry_dependency) {
161    if (in(0) != NULL && in(0)->in(0) != NULL && in(0)->in(0)->is_If()) {
162      assert(in(0)->is_IfFalse() || in(0)->is_IfTrue(), "should be If proj");
163      Node* proj = in(0);
164      if (proj->in(0)->in(1)->is_Bool()) {
165        Node* b = proj->in(0)->in(1);
166        if (b->in(1)->Opcode() == Op_CmpI) {
167          Node* cmp = b->in(1);
168          if (cmp->in(1) == in(1) && phase->type(cmp->in(2))->isa_int()) {
169            const TypeInt* in2_t = phase->type(cmp->in(2))->is_int();
170            const Type* t = TypeInt::INT;
171            BoolTest test = b->as_Bool()->_test;
172            if (proj->is_IfFalse()) {
173              test = test.negate();
174            }
175            BoolTest::mask m = test._test;
176            jlong lo_long = min_jint;
177            jlong hi_long = max_jint;
178            if (m == BoolTest::le || m == BoolTest::lt) {
179              hi_long = in2_t->_hi;
180              if (m == BoolTest::lt) {
181                hi_long -= 1;
182              }
183            } else if (m == BoolTest::ge || m == BoolTest::gt) {
184              lo_long = in2_t->_lo;
185              if (m == BoolTest::gt) {
186                lo_long += 1;
187              }
188            } else if (m == BoolTest::eq) {
189              lo_long = in2_t->_lo;
190              hi_long = in2_t->_hi;
191            } else if (m == BoolTest::ne) {
192              // can't do any better
193            } else {
194              stringStream ss;
195              test.dump_on(&ss);
196              fatal("unexpected comparison %s", ss.as_string());
197            }
198            int lo_int = (int)lo_long;
199            int hi_int = (int)hi_long;
200
201            if (lo_long != (jlong)lo_int) {
202              lo_int = min_jint;
203            }
204            if (hi_long != (jlong)hi_int) {
205              hi_int = max_jint;
206            }
207
208            t = TypeInt::make(lo_int, hi_int, Type::WidenMax);
209
210            res = res->filter_speculative(t);
211
212            return res;
213          }
214        }
215      }
216    }
217  }
218  return res;
219}
220
221Node *CastIINode::Ideal(PhaseGVN *phase, bool can_reshape) {
222  Node* progress = ConstraintCastNode::Ideal(phase, can_reshape);
223  if (progress != NULL) {
224    return progress;
225  }
226
227  // Similar to ConvI2LNode::Ideal() for the same reasons
228  // Do not narrow the type of range check dependent CastIINodes to
229  // avoid corruption of the graph if a CastII is replaced by TOP but
230  // the corresponding range check is not removed.
231  if (can_reshape && !_range_check_dependency && !phase->C->major_progress()) {
232    const TypeInt* this_type = this->type()->is_int();
233    const TypeInt* in_type = phase->type(in(1))->isa_int();
234    if (in_type != NULL && this_type != NULL &&
235        (in_type->_lo != this_type->_lo ||
236         in_type->_hi != this_type->_hi)) {
237      int lo1 = this_type->_lo;
238      int hi1 = this_type->_hi;
239      int w1  = this_type->_widen;
240
241      if (lo1 >= 0) {
242        // Keep a range assertion of >=0.
243        lo1 = 0;        hi1 = max_jint;
244      } else if (hi1 < 0) {
245        // Keep a range assertion of <0.
246        lo1 = min_jint; hi1 = -1;
247      } else {
248        lo1 = min_jint; hi1 = max_jint;
249      }
250      const TypeInt* wtype = TypeInt::make(MAX2(in_type->_lo, lo1),
251                                           MIN2(in_type->_hi, hi1),
252                                           MAX2((int)in_type->_widen, w1));
253      if (wtype != type()) {
254        set_type(wtype);
255        return this;
256      }
257    }
258  }
259  return NULL;
260}
261
262uint CastIINode::cmp(const Node &n) const {
263  return ConstraintCastNode::cmp(n) && ((CastIINode&)n)._range_check_dependency == _range_check_dependency;
264}
265
266uint CastIINode::size_of() const {
267  return sizeof(*this);
268}
269
270#ifndef PRODUCT
271void CastIINode::dump_spec(outputStream* st) const {
272  ConstraintCastNode::dump_spec(st);
273  if (_range_check_dependency) {
274    st->print(" range check dependency");
275  }
276}
277#endif
278
279//=============================================================================
280//------------------------------Identity---------------------------------------
281// If input is already higher or equal to cast type, then this is an identity.
282Node* CheckCastPPNode::Identity(PhaseGVN* phase) {
283  Node* dom = dominating_cast(phase);
284  if (dom != NULL) {
285    return dom;
286  }
287  if (_carry_dependency) {
288    return this;
289  }
290  // Toned down to rescue meeting at a Phi 3 different oops all implementing
291  // the same interface.  CompileTheWorld starting at 502, kd12rc1.zip.
292  return (phase->type(in(1)) == phase->type(this)) ? in(1) : this;
293}
294
295//------------------------------Value------------------------------------------
296// Take 'join' of input and cast-up type, unless working with an Interface
297const Type* CheckCastPPNode::Value(PhaseGVN* phase) const {
298  if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP;
299
300  const Type *inn = phase->type(in(1));
301  if( inn == Type::TOP ) return Type::TOP;  // No information yet
302
303  const TypePtr *in_type   = inn->isa_ptr();
304  const TypePtr *my_type   = _type->isa_ptr();
305  const Type *result = _type;
306  if( in_type != NULL && my_type != NULL ) {
307    TypePtr::PTR   in_ptr    = in_type->ptr();
308    if (in_ptr == TypePtr::Null) {
309      result = in_type;
310    } else if (in_ptr == TypePtr::Constant) {
311      const TypeOopPtr *jptr = my_type->isa_oopptr();
312      assert(jptr, "");
313      result = !in_type->higher_equal(_type)
314      ? my_type->cast_to_ptr_type(TypePtr::NotNull)
315      : in_type;
316    } else {
317      result =  my_type->cast_to_ptr_type( my_type->join_ptr(in_ptr) );
318    }
319  }
320
321  // This is the code from TypePtr::xmeet() that prevents us from
322  // having 2 ways to represent the same type. We have to replicate it
323  // here because we don't go through meet/join.
324  if (result->remove_speculative() == result->speculative()) {
325    result = result->remove_speculative();
326  }
327
328  // Same as above: because we don't go through meet/join, remove the
329  // speculative type if we know we won't use it.
330  return result->cleanup_speculative();
331
332  // JOIN NOT DONE HERE BECAUSE OF INTERFACE ISSUES.
333  // FIX THIS (DO THE JOIN) WHEN UNION TYPES APPEAR!
334
335  //
336  // Remove this code after overnight run indicates no performance
337  // loss from not performing JOIN at CheckCastPPNode
338  //
339  // const TypeInstPtr *in_oop = in->isa_instptr();
340  // const TypeInstPtr *my_oop = _type->isa_instptr();
341  // // If either input is an 'interface', return destination type
342  // assert (in_oop == NULL || in_oop->klass() != NULL, "");
343  // assert (my_oop == NULL || my_oop->klass() != NULL, "");
344  // if( (in_oop && in_oop->klass()->is_interface())
345  //   ||(my_oop && my_oop->klass()->is_interface()) ) {
346  //   TypePtr::PTR  in_ptr = in->isa_ptr() ? in->is_ptr()->_ptr : TypePtr::BotPTR;
347  //   // Preserve cast away nullness for interfaces
348  //   if( in_ptr == TypePtr::NotNull && my_oop && my_oop->_ptr == TypePtr::BotPTR ) {
349  //     return my_oop->cast_to_ptr_type(TypePtr::NotNull);
350  //   }
351  //   return _type;
352  // }
353  //
354  // // Neither the input nor the destination type is an interface,
355  //
356  // // history: JOIN used to cause weird corner case bugs
357  // //          return (in == TypeOopPtr::NULL_PTR) ? in : _type;
358  // // JOIN picks up NotNull in common instance-of/check-cast idioms, both oops.
359  // // JOIN does not preserve NotNull in other cases, e.g. RawPtr vs InstPtr
360  // const Type *join = in->join(_type);
361  // // Check if join preserved NotNull'ness for pointers
362  // if( join->isa_ptr() && _type->isa_ptr() ) {
363  //   TypePtr::PTR join_ptr = join->is_ptr()->_ptr;
364  //   TypePtr::PTR type_ptr = _type->is_ptr()->_ptr;
365  //   // If there isn't any NotNull'ness to preserve
366  //   // OR if join preserved NotNull'ness then return it
367  //   if( type_ptr == TypePtr::BotPTR  || type_ptr == TypePtr::Null ||
368  //       join_ptr == TypePtr::NotNull || join_ptr == TypePtr::Constant ) {
369  //     return join;
370  //   }
371  //   // ELSE return same old type as before
372  //   return _type;
373  // }
374  // // Not joining two pointers
375  // return join;
376}
377
378//=============================================================================
379//------------------------------Value------------------------------------------
380const Type* CastX2PNode::Value(PhaseGVN* phase) const {
381  const Type* t = phase->type(in(1));
382  if (t == Type::TOP) return Type::TOP;
383  if (t->base() == Type_X && t->singleton()) {
384    uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con();
385    if (bits == 0)   return TypePtr::NULL_PTR;
386    return TypeRawPtr::make((address) bits);
387  }
388  return CastX2PNode::bottom_type();
389}
390
391//------------------------------Idealize---------------------------------------
392static inline bool fits_in_int(const Type* t, bool but_not_min_int = false) {
393  if (t == Type::TOP)  return false;
394  const TypeX* tl = t->is_intptr_t();
395  jint lo = min_jint;
396  jint hi = max_jint;
397  if (but_not_min_int)  ++lo;  // caller wants to negate the value w/o overflow
398  return (tl->_lo >= lo) && (tl->_hi <= hi);
399}
400
401static inline Node* addP_of_X2P(PhaseGVN *phase,
402                                Node* base,
403                                Node* dispX,
404                                bool negate = false) {
405  if (negate) {
406    dispX = new SubXNode(phase->MakeConX(0), phase->transform(dispX));
407  }
408  return new AddPNode(phase->C->top(),
409                      phase->transform(new CastX2PNode(base)),
410                      phase->transform(dispX));
411}
412
413Node *CastX2PNode::Ideal(PhaseGVN *phase, bool can_reshape) {
414  // convert CastX2P(AddX(x, y)) to AddP(CastX2P(x), y) if y fits in an int
415  int op = in(1)->Opcode();
416  Node* x;
417  Node* y;
418  switch (op) {
419    case Op_SubX:
420    x = in(1)->in(1);
421    // Avoid ideal transformations ping-pong between this and AddP for raw pointers.
422    if (phase->find_intptr_t_con(x, -1) == 0)
423    break;
424    y = in(1)->in(2);
425    if (fits_in_int(phase->type(y), true)) {
426      return addP_of_X2P(phase, x, y, true);
427    }
428    break;
429    case Op_AddX:
430    x = in(1)->in(1);
431    y = in(1)->in(2);
432    if (fits_in_int(phase->type(y))) {
433      return addP_of_X2P(phase, x, y);
434    }
435    if (fits_in_int(phase->type(x))) {
436      return addP_of_X2P(phase, y, x);
437    }
438    break;
439  }
440  return NULL;
441}
442
443//------------------------------Identity---------------------------------------
444Node* CastX2PNode::Identity(PhaseGVN* phase) {
445  if (in(1)->Opcode() == Op_CastP2X)  return in(1)->in(1);
446  return this;
447}
448
449//=============================================================================
450//------------------------------Value------------------------------------------
451const Type* CastP2XNode::Value(PhaseGVN* phase) const {
452  const Type* t = phase->type(in(1));
453  if (t == Type::TOP) return Type::TOP;
454  if (t->base() == Type::RawPtr && t->singleton()) {
455    uintptr_t bits = (uintptr_t) t->is_rawptr()->get_con();
456    return TypeX::make(bits);
457  }
458  return CastP2XNode::bottom_type();
459}
460
461Node *CastP2XNode::Ideal(PhaseGVN *phase, bool can_reshape) {
462  return (in(0) && remove_dead_region(phase, can_reshape)) ? this : NULL;
463}
464
465//------------------------------Identity---------------------------------------
466Node* CastP2XNode::Identity(PhaseGVN* phase) {
467  if (in(1)->Opcode() == Op_CastX2P)  return in(1)->in(1);
468  return this;
469}
470