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