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 #include "ci/ciValueKlass.hpp"
  26 #include "opto/addnode.hpp"
  27 #include "opto/graphKit.hpp"
  28 #include "opto/valuetypenode.hpp"
  29 #include "opto/phaseX.hpp"
  30 
  31 Node* ValueTypeNode::make(PhaseGVN& gvn, ciValueKlass* klass) {
  32   // Create a new ValueTypeNode with uninitialized values and NULL oop
  33   const TypeValueType* type = TypeValueType::make(klass);
  34   return new ValueTypeNode(type, gvn.zerocon(T_VALUETYPE));
  35 }
  36 
  37 Node* ValueTypeNode::make(PhaseGVN& gvn, Node* mem, Node* oop) {
  38   // Create and initialize a ValueTypeNode by loading all field
  39   // values from memory and also save the oop to the heap allocated version.
  40   const TypeValueTypePtr* vtptr = gvn.type(oop)->is_valuetypeptr();
  41   ValueTypeNode* vt = new ValueTypeNode(vtptr->value_type(), oop);
  42   for (uint index = 0; index < vt->field_count(); ++index) {
  43     int offset = vt->get_field_offset(index);
  44     const TypePtr* adr_type = vtptr->add_offset(offset);
  45     const Type* field_type = Type::get_const_basic_type(vt->get_field_type(index));
  46     Node* adr = gvn.transform(new AddPNode(oop, oop, gvn.longcon(offset)));
  47     Node* ld = LoadNode::make(gvn, NULL, mem, adr, adr_type, field_type, field_type->basic_type(), MemNode::unordered);
  48     vt->set_field_value(index, gvn.transform(ld));
  49   }
  50   return gvn.transform(vt);
  51 }
  52 
  53 Node* ValueTypeNode::store_to_memory(GraphKit* kit) {
  54   Node* in_oop = get_oop();
  55   Node* null_ctl = kit->top();
  56   // Check if value type is already allocated
  57   Node* not_null_oop = kit->null_check_oop(in_oop, &null_ctl);
  58   if (null_ctl->is_top()) {
  59     // Value type is allocated
  60     return not_null_oop;
  61   }
  62   // Not able to prove that value type is allocated.
  63   // Emit runtime check that may be folded later.
  64   const Type* oop_type = kit->gvn().type(in_oop);
  65   assert(TypePtr::NULL_PTR->higher_equal(oop_type), "should not be allocated");
  66 
  67   const TypeValueTypePtr* vtptr_type = TypeValueTypePtr::make(bottom_type()->isa_valuetype(), TypePtr::NotNull);
  68   RegionNode* region = new RegionNode(3);
  69   PhiNode* oop = new PhiNode(region, vtptr_type);
  70   PhiNode* io  = new PhiNode(region, Type::ABIO);
  71   PhiNode* mem = new PhiNode(region, Type::MEMORY, TypePtr::BOTTOM);
  72 
  73   // Oop is non-NULL, use it
  74   region->init_req(1, kit->control());
  75   // Fixme if we cast oop to not null we fail if the control path is not folded
  76   // castnode.cpp:69: #  assert(ft == Type::TOP) failed: special case #3
  77   //oop   ->init_req(1, not_null_oop);
  78   oop   ->init_req(1, in_oop);
  79   io    ->init_req(1, kit->i_o());
  80   mem   ->init_req(1, kit->merged_memory());
  81 
  82   // Oop is NULL, allocate value type
  83   kit->set_control(null_ctl);
  84   kit->kill_dead_locals();
  85   Node* klass_node = kit->makecon(TypeKlassPtr::make(get_value_klass()));
  86   Node* alloc_oop  = kit->new_instance(klass_node);
  87   // Write field values to memory
  88   for (uint index = 0; index < field_count(); ++index) {
  89     int offset = get_field_offset(index);
  90     const TypePtr* adr_type = vtptr_type->add_offset(offset);
  91     Node* adr = kit->basic_plus_adr(alloc_oop, alloc_oop, offset);
  92     kit->store_to_memory(kit->control(), adr, get_field_value(index), get_field_type(index), adr_type, MemNode::unordered);
  93   }
  94   region->init_req(2, kit->control());
  95   oop   ->init_req(2, alloc_oop);
  96   io    ->init_req(2, kit->i_o());
  97   mem   ->init_req(2, kit->merged_memory());
  98 
  99   // Update GraphKit
 100   kit->set_control(kit->gvn().transform(region));
 101   kit->set_i_o(kit->gvn().transform(io));
 102   kit->set_all_memory(kit->gvn().transform(mem));
 103   kit->record_for_igvn(region);
 104   kit->record_for_igvn(oop);
 105   kit->record_for_igvn(io);
 106   kit->record_for_igvn(mem);
 107 
 108   return kit->gvn().transform(oop);
 109 }
 110 
 111 Node* ValueTypeNode::get_field_value(uint index) const {
 112   assert(index < field_count(), "index out of bounds");
 113   return in(Values + index);
 114 }
 115 
 116 Node* ValueTypeNode::get_field_value_by_offset(int field_offset) const {
 117   int index = get_value_klass()->get_field_index_by_offset(field_offset);
 118   return get_field_value(index);
 119 }
 120 
 121 void ValueTypeNode::set_field_value(uint index, Node* value) {
 122   assert(index < field_count(), "index out of bounds");
 123   set_req(Values + index, value);
 124 }
 125 
 126 int ValueTypeNode::get_field_offset(uint index) const {
 127   assert(index < field_count(), "index out of bounds");
 128   return get_value_klass()->get_field_offset_by_index(index);
 129 }
 130 
 131 BasicType ValueTypeNode::get_field_type(uint index) const {
 132   assert(index < field_count(), "index out of bounds");
 133   return get_value_klass()->get_field_type_by_index(index);
 134 }
 135 
 136 Node* ValueTypeNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 137   // No optimizations for now
 138   return NULL;
 139 }
 140 
 141 #ifndef PRODUCT
 142 
 143 void ValueTypeNode::dump_spec(outputStream* st) const {
 144   TypeNode::dump_spec(st);
 145   if (!get_oop()->is_top()) {
 146     st->print(" #oop");
 147   }
 148 }
 149 
 150 #endif