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  */
  25 
  26 #include "incls/_precompiled.incl"
  27 #include "incls/_sharkState.cpp.incl"
  28 
  29 using namespace llvm;
  30 
  31 void SharkState::initialize(const SharkState *state) {
  32   _locals = NEW_RESOURCE_ARRAY(SharkValue*, max_locals());
  33   _stack  = NEW_RESOURCE_ARRAY(SharkValue*, max_stack());
  34 
  35   NOT_PRODUCT(memset(_locals, 23, max_locals() * sizeof(SharkValue *)));
  36   NOT_PRODUCT(memset(_stack,  23, max_stack()  * sizeof(SharkValue *)));
  37   _sp = _stack;
  38 
  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     }
  46 
  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   }
  54 
  55   set_num_monitors(state ? state->num_monitors() : 0);
  56 }
  57 
  58 bool SharkState::equal_to(SharkState *other) {
  59   if (target() != other->target())
  60     return false;
  61 
  62   if (method() != other->method())
  63     return false;
  64 
  65   if (oop_tmp() != other->oop_tmp())
  66     return false;
  67 
  68   if (max_locals() != other->max_locals())
  69     return false;
  70 
  71   if (stack_depth() != other->stack_depth())
  72     return false;
  73 
  74   if (num_monitors() != other->num_monitors())
  75     return false;
  76 
  77   if (has_safepointed() != other->has_safepointed())
  78     return false;
  79 
  80   // Local variables
  81   for (int i = 0; i < max_locals(); i++) {
  82     SharkValue *value = local(i);
  83     SharkValue *other_value = other->local(i);
  84 
  85     if (value == NULL) {
  86       if (other_value != NULL)
  87         return false;
  88     }
  89     else {
  90       if (other_value == NULL)
  91         return false;
  92 
  93       if (!value->equal_to(other_value))
  94         return false;
  95     }
  96   }
  97 
  98   // Expression stack
  99   for (int i = 0; i < stack_depth(); i++) {
 100     SharkValue *value = stack(i);
 101     SharkValue *other_value = other->stack(i);
 102 
 103     if (value == NULL) {
 104       if (other_value != NULL)
 105         return false;
 106     }
 107     else {
 108       if (other_value == NULL)
 109         return false;
 110 
 111       if (!value->equal_to(other_value))
 112         return false;
 113     }
 114   }
 115 
 116   return true;
 117 }
 118 
 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   }
 131 
 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   }
 142 
 143   // Monitors
 144   assert(this->num_monitors() == other->num_monitors(), "should be");
 145 
 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   }
 159 
 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   }
 173 
 174   // Safepointed status
 175   set_has_safepointed(this->has_safepointed() && other->has_safepointed());
 176 }
 177 
 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   }
 184 
 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 }
 191 
 192 SharkNormalEntryState::SharkNormalEntryState(SharkTopLevelBlock* block,
 193                                              Value*              method)
 194   : SharkState(block) {
 195   assert(!block->stack_depth_at_entry(), "entry block shouldn't have stack");
 196 
 197   // Local variables
 198   for (int i = 0; i < max_locals(); i++) {
 199     ciType *type = block->local_type_at_entry(i);
 200 
 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;
 214 
 215     case ciTypeFlow::StateVector::T_NULL:
 216       value = SharkValue::null();
 217       break;
 218 
 219     case ciTypeFlow::StateVector::T_BOTTOM:
 220       break;
 221 
 222     case ciTypeFlow::StateVector::T_LONG2:
 223     case ciTypeFlow::StateVector::T_DOUBLE2:
 224       break;
 225 
 226     default:
 227       ShouldNotReachHere();
 228     }
 229     set_local(i, value);
 230   }
 231   SharkNormalEntryCacher(block->function(), method).scan(this);
 232 }
 233 
 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());
 240 
 241   // Local variables
 242   for (int i = 0; i < max_locals(); i++) {
 243     ciType *type = block->local_type_at_entry(i);
 244 
 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;
 255 
 256     case ciTypeFlow::StateVector::T_NULL:
 257       value = SharkValue::null();
 258       break;
 259 
 260     case ciTypeFlow::StateVector::T_BOTTOM:
 261       break;
 262 
 263     case ciTypeFlow::StateVector::T_LONG2:
 264     case ciTypeFlow::StateVector::T_DOUBLE2:
 265       break;
 266 
 267     default:
 268       ShouldNotReachHere();
 269     }
 270     set_local(i, value);
 271   }
 272   SharkOSREntryCacher(block->function(), method, osr_buf).scan(this);
 273 }
 274 
 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];
 280 
 281   // Method
 282   set_method(builder()->CreatePHI(SharkType::methodOop_type(), "method"));
 283 
 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     }
 291 
 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;
 304 
 305     case T_ADDRESS:
 306       value = SharkValue::address_constant(type->as_return_address()->bci());
 307       break;
 308 
 309     case ciTypeFlow::StateVector::T_BOTTOM:
 310       break;
 311 
 312     case ciTypeFlow::StateVector::T_LONG2:
 313     case ciTypeFlow::StateVector::T_DOUBLE2:
 314       break;
 315 
 316     default:
 317       ShouldNotReachHere();
 318     }
 319     set_local(i, value);
 320   }
 321 
 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     }
 329 
 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;
 342 
 343     case T_ADDRESS:
 344       value = SharkValue::address_constant(type->as_return_address()->bci());
 345       break;
 346 
 347     case ciTypeFlow::StateVector::T_LONG2:
 348     case ciTypeFlow::StateVector::T_DOUBLE2:
 349       break;
 350 
 351     default:
 352       ShouldNotReachHere();
 353     }
 354     push(value);
 355   }
 356 
 357   // Monitors
 358   set_num_monitors(block->ciblock()->monitor_count());
 359 
 360   builder()->SetInsertPoint(saved_insert_point);
 361 }
 362 
 363 void SharkPHIState::add_incoming(SharkState* incoming_state) {
 364   BasicBlock *predecessor = builder()->GetInsertBlock();
 365 
 366   // Method
 367   ((PHINode *) method())->addIncoming(incoming_state->method(), predecessor);
 368 
 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   }
 374 
 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   }
 383 
 384   // Monitors
 385   assert(num_monitors() == incoming_state->num_monitors(), "should be");
 386 
 387   // Temporary oop slot
 388   assert(oop_tmp() == incoming_state->oop_tmp(), "should be");
 389 }