1 /*
   2  * Copyright (c) 2016, 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 #ifndef SHARE_VM_OPTO_VALUETYPENODE_HPP
  26 #define SHARE_VM_OPTO_VALUETYPENODE_HPP
  27 
  28 #include "opto/node.hpp"
  29 #include "opto/connode.hpp"
  30 
  31 class GraphKit;
  32 
  33 //------------------------------ValueTypeNode-------------------------------------
  34 // Node representing a value type in C2 IR
  35 class ValueTypeNode : public TypeNode {
  36 private:
  37   ValueTypeNode(const TypeValueType* t, Node* oop)
  38     : TypeNode(t, Values + t->value_klass()->nof_declared_nonstatic_fields()) {
  39     init_class_id(Class_ValueType);
  40     init_req(Oop, oop);
  41   }
  42 
  43   // Get the klass defining the field layout of the value type
  44   ciValueKlass* value_klass() const { return type()->is_valuetype()->value_klass(); }
  45   // Initialize the value type by loading its field values from memory
  46   void load_values(PhaseGVN& gvn, Node* mem, ciInstanceKlass* holder, Node* base, int base_offset = 0);
  47   // Store the field values to memory
  48   void store_values(GraphKit* kit, ciInstanceKlass* holder, Node* base, int base_offset = 0) const;
  49 
  50   enum { Control,   // Control input
  51          Oop,       // Oop of TypeValueTypePtr
  52          Values     // Nodes corresponding to values of the value type's fields.
  53                     // Nodes are connected in increasing order of the index of the field
  54                     // they correspond to. Field indeces are defined in ciValueKlass::_field_index_map.
  55   };
  56 
  57 public:
  58   // Create a new ValueTypeNode with uninitialized values
  59   static ValueTypeNode* make(PhaseGVN& gvn, ciValueKlass* klass);
  60   // Create a new ValueTypeNode and load its values from an oop
  61   static Node* make(PhaseGVN& gvn, Node* mem, Node* oop);
  62   // Create a new ValueTypeNode and load its values from a flattened value type field
  63   static Node* make(PhaseGVN& gvn, ciValueKlass* klass, Node* mem, ciInstanceKlass* holder, Node* obj, int field_offset);
  64 
  65   // Support for control flow merges
  66   ValueTypeNode* clone_with_phis(PhaseGVN& gvn, Node* region);
  67   bool  has_phi_inputs(Node* region);
  68   Node* merge_with(GraphKit* kit, const ValueTypeNode* other, int pnum);
  69 
  70   // Store the value type to memory if not yet allocated and returns the oop
  71   Node* store_to_memory(GraphKit* kit);
  72   // Store the value type in a field of an object
  73   void store_to_field(GraphKit* kit, ciInstanceKlass* holder, Node* obj, int field_offset) const;
  74 
  75   // Get oop for heap allocated value type (may be TypePtr::NULL_PTR)
  76   Node* get_oop() const    { return in(Oop); }
  77   void  set_oop(Node* oop) { set_req(Oop, oop); }
  78 
  79   // Value type fields
  80   uint          field_count() const { return req() - Values; }
  81   Node*     get_field_value(uint index) const;
  82   Node*     get_field_value_by_offset(int offset, bool recursive = false) const;
  83   void      set_field_value(uint index, Node* value);
  84   int       get_field_offset(uint index) const;
  85   ciType*   get_field_type(uint index) const;
  86 
  87   // Replace ValueTypeNodes in debug info at safepoints with SafePointScalarObjectNodes
  88   void make_scalar_in_safepoints(Compile* C);
  89   uint set_arguments_for_java_call(CallJavaNode* call, int base_input, const GraphKit& kit, ciValueKlass* base_vk = NULL, int base_offset = 0);
  90   virtual Node* Ideal(PhaseGVN* phase, bool can_reshape);
  91   virtual int Opcode() const;
  92 
  93 #ifndef PRODUCT
  94   virtual void dump_spec(outputStream* st) const;
  95 #endif
  96 };
  97 
  98 #endif // SHARE_VM_OPTO_VALUETYPENODE_HPP