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