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