1 /*
  2  * Copyright (c) 2014, 2025, 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 "opto/narrowptrnode.hpp"
 26 #include "opto/phaseX.hpp"
 27 
 28 Node* DecodeNNode::Identity(PhaseGVN* phase) {
 29   const Type *t = phase->type( in(1) );
 30   if( t == Type::TOP ) return in(1);
 31 
 32   if (in(1)->is_EncodeP()) {
 33     // (DecodeN (EncodeP p)) -> p
 34     return in(1)->in(1);
 35   }
 36   return this;
 37 }
 38 
 39 const Type* DecodeNNode::Value(PhaseGVN* phase) const {
 40   const Type *t = phase->type( in(1) );
 41   if (t == Type::TOP) return Type::TOP;
 42   if (t == TypeNarrowOop::NULL_PTR) return TypePtr::NULL_PTR;
 43 
 44   assert(t->isa_narrowoop(), "only  narrowoop here");
 45   return t->make_ptr();
 46 }
 47 
 48 Node* EncodePNode::Identity(PhaseGVN* phase) {
 49   const Type *t = phase->type( in(1) );
 50   if( t == Type::TOP ) return in(1);
 51 
 52   if (in(1)->is_DecodeN()) {
 53     // (EncodeP (DecodeN p)) -> p
 54     return in(1)->in(1);
 55   }
 56   return this;
 57 }
 58 
 59 const Type* EncodePNode::Value(PhaseGVN* phase) const {
 60   const Type *t = phase->type( in(1) );
 61   if (t == Type::TOP) return Type::TOP;
 62   if (t == TypePtr::NULL_PTR) return TypeNarrowOop::NULL_PTR;
 63 
 64   assert(t->isa_oop_ptr(), "only oopptr here");
 65   return t->make_narrowoop();
 66 }
 67 
 68 
 69 Node* DecodeNKlassNode::Identity(PhaseGVN* phase) {
 70   const Type *t = phase->type( in(1) );
 71   if( t == Type::TOP ) return in(1);
 72 
 73   if (in(1)->is_EncodePKlass()) {
 74     // (DecodeNKlass (EncodePKlass p)) -> p
 75     return in(1)->in(1);
 76   }
 77   return this;
 78 }
 79 
 80 const Type* DecodeNKlassNode::Value(PhaseGVN* phase) const {
 81   const Type *t = phase->type( in(1) );
 82   if (t == Type::TOP) return Type::TOP;
 83   assert(t != TypeNarrowKlass::NULL_PTR, "null klass?");
 84 
 85   assert(t->isa_narrowklass(), "only narrow klass ptr here");
 86   return t->make_ptr();
 87 }
 88 
 89 Node* EncodePKlassNode::Identity(PhaseGVN* phase) {
 90   const Type *t = phase->type( in(1) );
 91   if( t == Type::TOP ) return in(1);
 92 
 93   if (in(1)->is_DecodeNKlass()) {
 94     // (EncodePKlass (DecodeNKlass p)) -> p
 95     return in(1)->in(1);
 96   }
 97   return this;
 98 }
 99 
100 const Type* EncodePKlassNode::Value(PhaseGVN* phase) const {
101   const Type *t = phase->type( in(1) );
102   if (t == Type::TOP) return Type::TOP;
103   assert (t != TypePtr::NULL_PTR, "null klass?");
104 
105   assert(UseCompressedClassPointers && t->isa_klassptr(), "only klass ptr here");
106   return t->make_narrowklass();
107 }
108