1 /*
   2  * Copyright (c) 1997, 2010, 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/multnode.hpp"
  30 #include "opto/opcodes.hpp"
  31 #include "opto/phaseX.hpp"
  32 #include "opto/regmask.hpp"
  33 #include "opto/type.hpp"
  34 
  35 //=============================================================================
  36 //------------------------------MultiNode--------------------------------------
  37 const RegMask &MultiNode::out_RegMask() const {
  38   return RegMask::Empty;
  39 }
  40 
  41 Node *MultiNode::match( const ProjNode *proj, const Matcher *m ) { return proj->clone(); }
  42 
  43 //------------------------------proj_out---------------------------------------
  44 // Get a named projection
  45 ProjNode* MultiNode::proj_out(uint which_proj) const {
  46   assert(Opcode() != Op_If || which_proj == (uint)true || which_proj == (uint)false, "must be 1 or 0");
  47   assert(Opcode() != Op_If || outcnt() == 2, "bad if #1");
  48   for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) {
  49     Node *p = fast_out(i);
  50     if( !p->is_Proj() ) {
  51       assert(p == this && this->is_Start(), "else must be proj");
  52       continue;
  53     }
  54     ProjNode *proj = p->as_Proj();
  55     if( proj->_con == which_proj ) {
  56       assert(Opcode() != Op_If || proj->Opcode() == (which_proj?Op_IfTrue:Op_IfFalse), "bad if #2");
  57       return proj;
  58     }
  59   }
  60   return NULL;
  61 }
  62 
  63 //=============================================================================
  64 //------------------------------ProjNode---------------------------------------
  65 uint ProjNode::hash() const {
  66   // only one input
  67   return (uintptr_t)in(TypeFunc::Control) + (_con << 1) + (_is_io_use ? 1 : 0);
  68 }
  69 uint ProjNode::cmp( const Node &n ) const { return _con == ((ProjNode&)n)._con && ((ProjNode&)n)._is_io_use == _is_io_use; }
  70 uint ProjNode::size_of() const { return sizeof(ProjNode); }
  71 
  72 // Test if we propagate interesting control along this projection
  73 bool ProjNode::is_CFG() const {
  74   Node *def = in(0);
  75   return (_con == TypeFunc::Control && def->is_CFG());
  76 }
  77 
  78 const Type *ProjNode::bottom_type() const {
  79   if (in(0) == NULL)  return Type::TOP;
  80   const Type *tb = in(0)->bottom_type();
  81   if( tb == Type::TOP ) return Type::TOP;
  82   if( tb == Type::BOTTOM ) return Type::BOTTOM;
  83   const TypeTuple *t = tb->is_tuple();
  84   return t->field_at(_con);
  85 }
  86 
  87 const TypePtr *ProjNode::adr_type() const {
  88   if (bottom_type() == Type::MEMORY) {
  89     // in(0) might be a narrow MemBar; otherwise we will report TypePtr::BOTTOM
  90     const TypePtr* adr_type = in(0)->adr_type();
  91     #ifdef ASSERT
  92     if (!is_error_reported() && !Node::in_dump())
  93       assert(adr_type != NULL, "source must have adr_type");
  94     #endif
  95     return adr_type;
  96   }
  97   assert(bottom_type()->base() != Type::Memory, "no other memories?");
  98   return NULL;
  99 }
 100 
 101 bool ProjNode::pinned() const { return in(0)->pinned(); }
 102 #ifndef PRODUCT
 103 void ProjNode::dump_spec(outputStream *st) const { st->print("#%d",_con); if(_is_io_use) st->print(" (i_o_use)");}
 104 #endif
 105 
 106 //----------------------------check_con----------------------------------------
 107 void ProjNode::check_con() const {
 108   Node* n = in(0);
 109   if (n == NULL)       return;  // should be assert, but NodeHash makes bogons
 110   if (n->is_Mach())    return;  // mach. projs. are not type-safe
 111   if (n->is_Start())   return;  // alas, starts can have mach. projs. also
 112   if (_con == SCMemProjNode::SCMEMPROJCON ) return;
 113   const Type* t = n->bottom_type();
 114   if (t == Type::TOP)  return;  // multi is dead
 115   assert(_con < t->is_tuple()->cnt(), "ProjNode::_con must be in range");
 116 }
 117 
 118 //------------------------------Value------------------------------------------
 119 const Type *ProjNode::Value( PhaseTransform *phase ) const {
 120   if( !in(0) ) return Type::TOP;
 121   const Type *t = phase->type(in(0));
 122   if( t == Type::TOP ) return t;
 123   if( t == Type::BOTTOM ) return t;
 124   return t->is_tuple()->field_at(_con);
 125 }
 126 
 127 //------------------------------out_RegMask------------------------------------
 128 // Pass the buck uphill
 129 const RegMask &ProjNode::out_RegMask() const {
 130   return RegMask::Empty;
 131 }
 132 
 133 //------------------------------ideal_reg--------------------------------------
 134 uint ProjNode::ideal_reg() const {
 135   return Matcher::base2reg[bottom_type()->base()];
 136 }
 137 
 138 //-------------------------------is_uncommon_trap_proj----------------------------
 139 // Return true if proj is the form of "proj->[region->..]call_uct"
 140 bool ProjNode::is_uncommon_trap_proj(Deoptimization::DeoptReason reason) {
 141   int path_limit = 10;
 142   Node* out = this;
 143   for (int ct = 0; ct < path_limit; ct++) {
 144     out = out->unique_ctrl_out();
 145     if (out == NULL)
 146       return false;
 147     if (out->is_CallStaticJava()) {
 148       int req = out->as_CallStaticJava()->uncommon_trap_request();
 149       if (req != 0) {
 150         Deoptimization::DeoptReason trap_reason = Deoptimization::trap_request_reason(req);
 151         if (trap_reason == reason || reason == Deoptimization::Reason_none) {
 152            return true;
 153         }
 154       }
 155       return false; // don't do further after call
 156     }
 157     if (out->Opcode() != Op_Region)
 158       return false;
 159   }
 160   return false;
 161 }
 162 
 163 //-------------------------------is_uncommon_trap_if_pattern-------------------------
 164 // Return true  for "if(test)-> proj -> ...
 165 //                          |
 166 //                          V
 167 //                      other_proj->[region->..]call_uct"
 168 //
 169 // "must_reason_predicate" means the uct reason must be Reason_predicate
 170 bool ProjNode::is_uncommon_trap_if_pattern(Deoptimization::DeoptReason reason) {
 171   Node *in0 = in(0);
 172   if (!in0->is_If()) return false;
 173   // Variation of a dead If node.
 174   if (in0->outcnt() < 2)  return false;
 175   IfNode* iff = in0->as_If();
 176 
 177   // we need "If(Conv2B(Opaque1(...)))" pattern for reason_predicate
 178   if (reason != Deoptimization::Reason_none) {
 179     if (iff->in(1)->Opcode() != Op_Conv2B ||
 180        iff->in(1)->in(1)->Opcode() != Op_Opaque1) {
 181       return false;
 182     }
 183   }
 184 
 185   ProjNode* other_proj = iff->proj_out(1-_con)->as_Proj();
 186   if (other_proj->is_uncommon_trap_proj(reason)) {
 187     assert(reason == Deoptimization::Reason_none ||
 188            Compile::current()->is_predicate_opaq(iff->in(1)->in(1)), "should be on the list");
 189     return true;
 190   }
 191   return false;
 192 }