src/share/vm/shark/sharkState.cpp

Wed, 11 Aug 2010 05:51:21 -0700

author
twisti
date
Wed, 11 Aug 2010 05:51:21 -0700
changeset 2047
d2ede61b7a12
child 2314
f95d63e2154a
permissions
-rw-r--r--

6976186: integrate Shark HotSpot changes
Summary: Shark is a JIT compiler for Zero that uses the LLVM compiler infrastructure.
Reviewed-by: kvn, twisti
Contributed-by: Gary Benson <gbenson@redhat.com>

     1 /*
     2  * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
     3  * Copyright 2008, 2009 Red Hat, Inc.
     4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     5  *
     6  * This code is free software; you can redistribute it and/or modify it
     7  * under the terms of the GNU General Public License version 2 only, as
     8  * published by the Free Software Foundation.
     9  *
    10  * This code is distributed in the hope that it will be useful, but WITHOUT
    11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    13  * version 2 for more details (a copy is included in the LICENSE file that
    14  * accompanied this code).
    15  *
    16  * You should have received a copy of the GNU General Public License version
    17  * 2 along with this work; if not, write to the Free Software Foundation,
    18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    19  *
    20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    21  * or visit www.oracle.com if you need additional information or have any
    22  * questions.
    23  *
    24  */
    26 #include "incls/_precompiled.incl"
    27 #include "incls/_sharkState.cpp.incl"
    29 using namespace llvm;
    31 void SharkState::initialize(const SharkState *state) {
    32   _locals = NEW_RESOURCE_ARRAY(SharkValue*, max_locals());
    33   _stack  = NEW_RESOURCE_ARRAY(SharkValue*, max_stack());
    35   NOT_PRODUCT(memset(_locals, 23, max_locals() * sizeof(SharkValue *)));
    36   NOT_PRODUCT(memset(_stack,  23, max_stack()  * sizeof(SharkValue *)));
    37   _sp = _stack;
    39   if (state) {
    40     for (int i = 0; i < max_locals(); i++) {
    41       SharkValue *value = state->local(i);
    42       if (value)
    43         value = value->clone();
    44       set_local(i, value);
    45     }
    47     for (int i = state->stack_depth() - 1; i >= 0; i--) {
    48       SharkValue *value = state->stack(i);
    49       if (value)
    50         value = value->clone();
    51       push(value);
    52     }
    53   }
    55   set_num_monitors(state ? state->num_monitors() : 0);
    56 }
    58 bool SharkState::equal_to(SharkState *other) {
    59   if (target() != other->target())
    60     return false;
    62   if (method() != other->method())
    63     return false;
    65   if (oop_tmp() != other->oop_tmp())
    66     return false;
    68   if (max_locals() != other->max_locals())
    69     return false;
    71   if (stack_depth() != other->stack_depth())
    72     return false;
    74   if (num_monitors() != other->num_monitors())
    75     return false;
    77   if (has_safepointed() != other->has_safepointed())
    78     return false;
    80   // Local variables
    81   for (int i = 0; i < max_locals(); i++) {
    82     SharkValue *value = local(i);
    83     SharkValue *other_value = other->local(i);
    85     if (value == NULL) {
    86       if (other_value != NULL)
    87         return false;
    88     }
    89     else {
    90       if (other_value == NULL)
    91         return false;
    93       if (!value->equal_to(other_value))
    94         return false;
    95     }
    96   }
    98   // Expression stack
    99   for (int i = 0; i < stack_depth(); i++) {
   100     SharkValue *value = stack(i);
   101     SharkValue *other_value = other->stack(i);
   103     if (value == NULL) {
   104       if (other_value != NULL)
   105         return false;
   106     }
   107     else {
   108       if (other_value == NULL)
   109         return false;
   111       if (!value->equal_to(other_value))
   112         return false;
   113     }
   114   }
   116   return true;
   117 }
   119 void SharkState::merge(SharkState* other,
   120                        BasicBlock* other_block,
   121                        BasicBlock* this_block) {
   122   // Method
   123   Value *this_method = this->method();
   124   Value *other_method = other->method();
   125   if (this_method != other_method) {
   126     PHINode *phi = builder()->CreatePHI(SharkType::methodOop_type(), "method");
   127     phi->addIncoming(this_method, this_block);
   128     phi->addIncoming(other_method, other_block);
   129     set_method(phi);
   130   }
   132   // Temporary oop slot
   133   Value *this_oop_tmp = this->oop_tmp();
   134   Value *other_oop_tmp = other->oop_tmp();
   135   if (this_oop_tmp != other_oop_tmp) {
   136     assert(this_oop_tmp && other_oop_tmp, "can't merge NULL with non-NULL");
   137     PHINode *phi = builder()->CreatePHI(SharkType::oop_type(), "oop_tmp");
   138     phi->addIncoming(this_oop_tmp, this_block);
   139     phi->addIncoming(other_oop_tmp, other_block);
   140     set_oop_tmp(phi);
   141   }
   143   // Monitors
   144   assert(this->num_monitors() == other->num_monitors(), "should be");
   146   // Local variables
   147   assert(this->max_locals() == other->max_locals(), "should be");
   148   for (int i = 0; i < max_locals(); i++) {
   149     SharkValue *this_value = this->local(i);
   150     SharkValue *other_value = other->local(i);
   151     assert((this_value == NULL) == (other_value == NULL), "should be");
   152     if (this_value != NULL) {
   153       char name[18];
   154       snprintf(name, sizeof(name), "local_%d_", i);
   155       set_local(i, this_value->merge(
   156         builder(), other_value, other_block, this_block, name));
   157     }
   158   }
   160   // Expression stack
   161   assert(this->stack_depth() == other->stack_depth(), "should be");
   162   for (int i = 0; i < stack_depth(); i++) {
   163     SharkValue *this_value = this->stack(i);
   164     SharkValue *other_value = other->stack(i);
   165     assert((this_value == NULL) == (other_value == NULL), "should be");
   166     if (this_value != NULL) {
   167       char name[18];
   168       snprintf(name, sizeof(name), "stack_%d_", i);
   169       set_stack(i, this_value->merge(
   170         builder(), other_value, other_block, this_block, name));
   171     }
   172   }
   174   // Safepointed status
   175   set_has_safepointed(this->has_safepointed() && other->has_safepointed());
   176 }
   178 void SharkState::replace_all(SharkValue* old_value, SharkValue* new_value) {
   179   // Local variables
   180   for (int i = 0; i < max_locals(); i++) {
   181     if (local(i) == old_value)
   182       set_local(i, new_value);
   183   }
   185   // Expression stack
   186   for (int i = 0; i < stack_depth(); i++) {
   187     if (stack(i) == old_value)
   188       set_stack(i, new_value);
   189   }
   190 }
   192 SharkNormalEntryState::SharkNormalEntryState(SharkTopLevelBlock* block,
   193                                              Value*              method)
   194   : SharkState(block) {
   195   assert(!block->stack_depth_at_entry(), "entry block shouldn't have stack");
   197   // Local variables
   198   for (int i = 0; i < max_locals(); i++) {
   199     ciType *type = block->local_type_at_entry(i);
   201     SharkValue *value = NULL;
   202     switch (type->basic_type()) {
   203     case T_INT:
   204     case T_LONG:
   205     case T_FLOAT:
   206     case T_DOUBLE:
   207     case T_OBJECT:
   208     case T_ARRAY:
   209       if (i >= arg_size()) {
   210         ShouldNotReachHere();
   211       }
   212       value = SharkValue::create_generic(type, NULL, i == 0 && !is_static());
   213       break;
   215     case ciTypeFlow::StateVector::T_NULL:
   216       value = SharkValue::null();
   217       break;
   219     case ciTypeFlow::StateVector::T_BOTTOM:
   220       break;
   222     case ciTypeFlow::StateVector::T_LONG2:
   223     case ciTypeFlow::StateVector::T_DOUBLE2:
   224       break;
   226     default:
   227       ShouldNotReachHere();
   228     }
   229     set_local(i, value);
   230   }
   231   SharkNormalEntryCacher(block->function(), method).scan(this);
   232 }
   234 SharkOSREntryState::SharkOSREntryState(SharkTopLevelBlock* block,
   235                                        Value*              method,
   236                                        Value*              osr_buf)
   237   : SharkState(block) {
   238   assert(!block->stack_depth_at_entry(), "entry block shouldn't have stack");
   239   set_num_monitors(block->ciblock()->monitor_count());
   241   // Local variables
   242   for (int i = 0; i < max_locals(); i++) {
   243     ciType *type = block->local_type_at_entry(i);
   245     SharkValue *value = NULL;
   246     switch (type->basic_type()) {
   247     case T_INT:
   248     case T_LONG:
   249     case T_FLOAT:
   250     case T_DOUBLE:
   251     case T_OBJECT:
   252     case T_ARRAY:
   253       value = SharkValue::create_generic(type, NULL, false);
   254       break;
   256     case ciTypeFlow::StateVector::T_NULL:
   257       value = SharkValue::null();
   258       break;
   260     case ciTypeFlow::StateVector::T_BOTTOM:
   261       break;
   263     case ciTypeFlow::StateVector::T_LONG2:
   264     case ciTypeFlow::StateVector::T_DOUBLE2:
   265       break;
   267     default:
   268       ShouldNotReachHere();
   269     }
   270     set_local(i, value);
   271   }
   272   SharkOSREntryCacher(block->function(), method, osr_buf).scan(this);
   273 }
   275 SharkPHIState::SharkPHIState(SharkTopLevelBlock* block)
   276   : SharkState(block), _block(block) {
   277   BasicBlock *saved_insert_point = builder()->GetInsertBlock();
   278   builder()->SetInsertPoint(block->entry_block());
   279   char name[18];
   281   // Method
   282   set_method(builder()->CreatePHI(SharkType::methodOop_type(), "method"));
   284   // Local variables
   285   for (int i = 0; i < max_locals(); i++) {
   286     ciType *type = block->local_type_at_entry(i);
   287     if (type->basic_type() == (BasicType) ciTypeFlow::StateVector::T_NULL) {
   288       // XXX we could do all kinds of clever stuff here
   289       type = ciType::make(T_OBJECT); // XXX what about T_ARRAY?
   290     }
   292     SharkValue *value = NULL;
   293     switch (type->basic_type()) {
   294     case T_INT:
   295     case T_LONG:
   296     case T_FLOAT:
   297     case T_DOUBLE:
   298     case T_OBJECT:
   299     case T_ARRAY:
   300       snprintf(name, sizeof(name), "local_%d_", i);
   301       value = SharkValue::create_phi(
   302         type, builder()->CreatePHI(SharkType::to_stackType(type), name));
   303       break;
   305     case T_ADDRESS:
   306       value = SharkValue::address_constant(type->as_return_address()->bci());
   307       break;
   309     case ciTypeFlow::StateVector::T_BOTTOM:
   310       break;
   312     case ciTypeFlow::StateVector::T_LONG2:
   313     case ciTypeFlow::StateVector::T_DOUBLE2:
   314       break;
   316     default:
   317       ShouldNotReachHere();
   318     }
   319     set_local(i, value);
   320   }
   322   // Expression stack
   323   for (int i = 0; i < block->stack_depth_at_entry(); i++) {
   324     ciType *type = block->stack_type_at_entry(i);
   325     if (type->basic_type() == (BasicType) ciTypeFlow::StateVector::T_NULL) {
   326       // XXX we could do all kinds of clever stuff here
   327       type = ciType::make(T_OBJECT); // XXX what about T_ARRAY?
   328     }
   330     SharkValue *value = NULL;
   331     switch (type->basic_type()) {
   332     case T_INT:
   333     case T_LONG:
   334     case T_FLOAT:
   335     case T_DOUBLE:
   336     case T_OBJECT:
   337     case T_ARRAY:
   338       snprintf(name, sizeof(name), "stack_%d_", i);
   339       value = SharkValue::create_phi(
   340         type, builder()->CreatePHI(SharkType::to_stackType(type), name));
   341       break;
   343     case T_ADDRESS:
   344       value = SharkValue::address_constant(type->as_return_address()->bci());
   345       break;
   347     case ciTypeFlow::StateVector::T_LONG2:
   348     case ciTypeFlow::StateVector::T_DOUBLE2:
   349       break;
   351     default:
   352       ShouldNotReachHere();
   353     }
   354     push(value);
   355   }
   357   // Monitors
   358   set_num_monitors(block->ciblock()->monitor_count());
   360   builder()->SetInsertPoint(saved_insert_point);
   361 }
   363 void SharkPHIState::add_incoming(SharkState* incoming_state) {
   364   BasicBlock *predecessor = builder()->GetInsertBlock();
   366   // Method
   367   ((PHINode *) method())->addIncoming(incoming_state->method(), predecessor);
   369   // Local variables
   370   for (int i = 0; i < max_locals(); i++) {
   371     if (local(i) != NULL)
   372       local(i)->addIncoming(incoming_state->local(i), predecessor);
   373   }
   375   // Expression stack
   376   int stack_depth = block()->stack_depth_at_entry();
   377   assert(stack_depth == incoming_state->stack_depth(), "should be");
   378   for (int i = 0; i < stack_depth; i++) {
   379     assert((stack(i) == NULL) == (incoming_state->stack(i) == NULL), "oops");
   380     if (stack(i))
   381       stack(i)->addIncoming(incoming_state->stack(i), predecessor);
   382   }
   384   // Monitors
   385   assert(num_monitors() == incoming_state->num_monitors(), "should be");
   387   // Temporary oop slot
   388   assert(oop_tmp() == incoming_state->oop_tmp(), "should be");
   389 }

mercurial