1 /*
  2  * Copyright (c) 1997, 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/callnode.hpp"
 26 #include "opto/cfgnode.hpp"
 27 #include "opto/matcher.hpp"
 28 #include "opto/mathexactnode.hpp"
 29 #include "opto/multnode.hpp"
 30 #include "opto/opcodes.hpp"
 31 #include "opto/phaseX.hpp"
 32 #include "opto/regmask.hpp"
 33 #include "opto/type.hpp"
 34 #include "utilities/vmError.hpp"
 35 
 36 //=============================================================================
 37 //------------------------------MultiNode--------------------------------------
 38 const RegMask &MultiNode::out_RegMask() const {
 39   return RegMask::Empty;
 40 }
 41 
 42 Node *MultiNode::match( const ProjNode *proj, const Matcher *m ) { return proj->clone(); }
 43 
 44 //------------------------------proj_out---------------------------------------
 45 // Get a named projection or null if not found
 46 ProjNode* MultiNode::proj_out_or_null(uint which_proj) const {
 47   assert((Opcode() != Op_If && Opcode() != Op_RangeCheck) || which_proj == (uint)true || which_proj == (uint)false, "must be 1 or 0");
 48   for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) {
 49     Node *p = fast_out(i);
 50     if (p->is_Proj()) {
 51       ProjNode *proj = p->as_Proj();
 52       if (proj->_con == which_proj) {
 53         assert((Opcode() != Op_If && Opcode() != Op_RangeCheck) || proj->Opcode() == (which_proj ? Op_IfTrue : Op_IfFalse), "bad if #2");
 54         return proj;
 55       }
 56     } else {
 57       assert(p == this && this->is_Start(), "else must be proj");
 58       continue;
 59     }
 60   }
 61   return nullptr;
 62 }
 63 
 64 ProjNode* MultiNode::proj_out_or_null(uint which_proj, bool is_io_use) const {
 65   for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
 66     ProjNode* proj = fast_out(i)->isa_Proj();
 67     if (proj != nullptr && (proj->_con == which_proj) && (proj->_is_io_use == is_io_use)) {
 68       return proj;
 69     }
 70   }
 71   return nullptr;
 72 }
 73 
 74 // Get a named projection
 75 ProjNode* MultiNode::proj_out(uint which_proj) const {
 76   assert((Opcode() != Op_If && Opcode() != Op_RangeCheck) || outcnt() == 2, "bad if #1");
 77   ProjNode* p = proj_out_or_null(which_proj);
 78   assert(p != nullptr, "named projection %u not found", which_proj);
 79   return p;
 80 }
 81 
 82 //=============================================================================
 83 //------------------------------ProjNode---------------------------------------
 84 uint ProjNode::hash() const {
 85   // only one input
 86   return (uintptr_t)in(TypeFunc::Control) + (_con << 1) + (_is_io_use ? 1 : 0);
 87 }
 88 bool ProjNode::cmp( const Node &n ) const { return _con == ((ProjNode&)n)._con && ((ProjNode&)n)._is_io_use == _is_io_use; }
 89 uint ProjNode::size_of() const { return sizeof(ProjNode); }
 90 
 91 // Test if we propagate interesting control along this projection
 92 bool ProjNode::is_CFG() const {
 93   Node *def = in(0);
 94   return (_con == TypeFunc::Control && def->is_CFG());
 95 }
 96 
 97 const Type* ProjNode::proj_type(const Type* t) const {
 98   if (t == Type::TOP) {
 99     return Type::TOP;
100   }
101   if (t == Type::BOTTOM) {
102     return Type::BOTTOM;
103   }
104   t = t->is_tuple()->field_at(_con);
105   Node* n = in(0);
106   if ((_con == TypeFunc::Parms) &&
107       n->is_CallStaticJava() && n->as_CallStaticJava()->is_boxing_method()) {
108     // The result of autoboxing is always non-null on normal path.
109     t = t->join_speculative(TypePtr::NOTNULL);
110   }
111   return t;
112 }
113 
114 const Type *ProjNode::bottom_type() const {
115   if (in(0) == nullptr) return Type::TOP;
116   return proj_type(in(0)->bottom_type());
117 }
118 
119 const TypePtr *ProjNode::adr_type() const {
120   if (bottom_type() == Type::MEMORY) {
121     // in(0) might be a narrow MemBar; otherwise we will report TypePtr::BOTTOM
122     Node* ctrl = in(0);
123     if (ctrl == nullptr)  return nullptr; // node is dead
124     const TypePtr* adr_type = ctrl->adr_type();
125     #ifdef ASSERT
126     if (!VMError::is_error_reported() && !Node::in_dump())
127       assert(adr_type != nullptr, "source must have adr_type");
128     #endif
129     return adr_type;
130   }
131   assert(bottom_type()->base() != Type::Memory, "no other memories?");
132   return nullptr;
133 }
134 
135 bool ProjNode::pinned() const { return in(0)->pinned(); }
136 #ifndef PRODUCT
137 void ProjNode::dump_spec(outputStream *st) const { st->print("#%d",_con); if(_is_io_use) st->print(" (i_o_use)");}
138 
139 void ProjNode::dump_compact_spec(outputStream *st) const {
140   for (DUIterator i = this->outs(); this->has_out(i); i++) {
141     Node* o = this->out(i);
142     if (not_a_node(o)) {
143       st->print("[?]");
144     } else if (o == nullptr) {
145       st->print("[_]");
146     } else {
147       st->print("[%d]", o->_idx);
148     }
149   }
150   st->print("#%d", _con);
151 }
152 #endif
153 
154 //----------------------------check_con----------------------------------------
155 void ProjNode::check_con() const {
156   Node* n = in(0);
157   if (n == nullptr)    return;  // should be assert, but NodeHash makes bogons
158   if (n->is_Mach())    return;  // mach. projs. are not type-safe
159   if (n->is_Start())   return;  // alas, starts can have mach. projs. also
160   if (_con == SCMemProjNode::SCMEMPROJCON ) return;
161   const Type* t = n->bottom_type();
162   if (t == Type::TOP)  return;  // multi is dead
163   assert(_con < t->is_tuple()->cnt(), "ProjNode::_con must be in range");
164 }
165 
166 //------------------------------Value------------------------------------------
167 const Type* ProjNode::Value(PhaseGVN* phase) const {
168   if (in(0) == nullptr) return Type::TOP;
169   return proj_type(phase->type(in(0)));
170 }
171 
172 //------------------------------out_RegMask------------------------------------
173 // Pass the buck uphill
174 const RegMask &ProjNode::out_RegMask() const {
175   return RegMask::Empty;
176 }
177 
178 //------------------------------ideal_reg--------------------------------------
179 uint ProjNode::ideal_reg() const {
180   return bottom_type()->ideal_reg();
181 }
182 
183 //-------------------------------is_uncommon_trap_proj----------------------------
184 // Return uncommon trap call node if proj is for "proj->[region->..]call_uct"
185 // null otherwise
186 CallStaticJavaNode* ProjNode::is_uncommon_trap_proj(Deoptimization::DeoptReason reason) const {
187   const int path_limit = 10;
188   const Node* out = this;
189   for (int ct = 0; ct < path_limit; ct++) {
190     out = out->unique_ctrl_out_or_null();
191     if (out == nullptr)
192       return nullptr;
193     if (out->is_CallStaticJava()) {
194       CallStaticJavaNode* call = out->as_CallStaticJava();
195       int req = call->uncommon_trap_request();
196       if (req != 0) {
197         Deoptimization::DeoptReason trap_reason = Deoptimization::trap_request_reason(req);
198         if (trap_reason == reason || reason == Deoptimization::Reason_none) {
199           return call;
200         }
201       }
202       return nullptr; // don't do further after call
203     }
204     if (out->Opcode() != Op_Region)
205       return nullptr;
206   }
207   return nullptr;
208 }
209 
210 //-------------------------------is_uncommon_trap_if_pattern-------------------------
211 // Return uncommon trap call node for    "if(test)-> proj -> ...
212 //                                                 |
213 //                                                 V
214 //                                             other_proj->[region->..]call_uct"
215 // or null otherwise.
216 CallStaticJavaNode* ProjNode::is_uncommon_trap_if_pattern(Deoptimization::DeoptReason reason) const {
217   Node* iff = in(0);
218   if (!iff->is_If() || iff->outcnt() < 2) {
219     // Not a projection of an If or variation of a dead If node.
220     return nullptr;
221   }
222   return other_if_proj()->is_uncommon_trap_proj(reason);
223 }
224 
225 ProjNode* ProjNode::other_if_proj() const {
226   assert(_con == 0 || _con == 1, "not an if?");
227   return in(0)->as_If()->proj_out(1-_con);
228 }