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