1 /*
   2  * Copyright (c) 1999, 2012, 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 "precompiled.hpp"
  27 #include "ci/ciType.hpp"
  28 #include "ci/ciTypeFlow.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "shark/sharkBuilder.hpp"
  31 #include "shark/sharkCacheDecache.hpp"
  32 #include "shark/sharkState.hpp"
  33 #include "shark/sharkTopLevelBlock.hpp"
  34 #include "shark/sharkType.hpp"
  35 #include "shark/sharkValue.hpp"
  36 
  37 using namespace llvm;
  38 
  39 void SharkState::initialize(const SharkState *state) {
  40   _locals = NEW_RESOURCE_ARRAY(SharkValue*, max_locals());
  41   _stack  = NEW_RESOURCE_ARRAY(SharkValue*, max_stack());
  42 
  43   NOT_PRODUCT(memset(_locals, 23, max_locals() * sizeof(SharkValue *)));
  44   NOT_PRODUCT(memset(_stack,  23, max_stack()  * sizeof(SharkValue *)));
  45   _sp = _stack;
  46 
  47   if (state) {
  48     for (int i = 0; i < max_locals(); i++) {
  49       SharkValue *value = state->local(i);
  50       if (value)
  51         value = value->clone();
  52       set_local(i, value);
  53     }
  54 
  55     for (int i = state->stack_depth() - 1; i >= 0; i--) {
  56       SharkValue *value = state->stack(i);
  57       if (value)
  58         value = value->clone();
  59       push(value);
  60     }
  61   }
  62 
  63   set_num_monitors(state ? state->num_monitors() : 0);
  64 }
  65 
  66 bool SharkState::equal_to(SharkState *other) {
  67   if (target() != other->target())
  68     return false;
  69 
  70   if (method() != other->method())
  71     return false;
  72 
  73   if (oop_tmp() != other->oop_tmp())
  74     return false;
  75 
  76   if (max_locals() != other->max_locals())
  77     return false;
  78 
  79   if (stack_depth() != other->stack_depth())
  80     return false;
  81 
  82   if (num_monitors() != other->num_monitors())
  83     return false;
  84 
  85   if (has_safepointed() != other->has_safepointed())
  86     return false;
  87 
  88   // Local variables
  89   for (int i = 0; i < max_locals(); i++) {
  90     SharkValue *value = local(i);
  91     SharkValue *other_value = other->local(i);
  92 
  93     if (value == NULL) {
  94       if (other_value != NULL)
  95         return false;
  96     }
  97     else {
  98       if (other_value == NULL)
  99         return false;
 100 
 101       if (!value->equal_to(other_value))
 102         return false;
 103     }
 104   }
 105 
 106   // Expression stack
 107   for (int i = 0; i < stack_depth(); i++) {
 108     SharkValue *value = stack(i);
 109     SharkValue *other_value = other->stack(i);
 110 
 111     if (value == NULL) {
 112       if (other_value != NULL)
 113         return false;
 114     }
 115     else {
 116       if (other_value == NULL)
 117         return false;
 118 
 119       if (!value->equal_to(other_value))
 120         return false;
 121     }
 122   }
 123 
 124   return true;
 125 }
 126 
 127 void SharkState::merge(SharkState* other,
 128                        BasicBlock* other_block,
 129                        BasicBlock* this_block) {
 130   // Method
 131   Value *this_method = this->method();
 132   Value *other_method = other->method();
 133   if (this_method != other_method) {
 134     PHINode *phi = builder()->CreatePHI(SharkType::Method_type(), 0, "method");
 135     phi->addIncoming(this_method, this_block);
 136     phi->addIncoming(other_method, other_block);
 137     set_method(phi);
 138   }
 139 
 140   // Temporary oop slot
 141   Value *this_oop_tmp = this->oop_tmp();
 142   Value *other_oop_tmp = other->oop_tmp();
 143   if (this_oop_tmp != other_oop_tmp) {
 144     assert(this_oop_tmp && other_oop_tmp, "can't merge NULL with non-NULL");
 145     PHINode *phi = builder()->CreatePHI(SharkType::oop_type(), 0, "oop_tmp");
 146     phi->addIncoming(this_oop_tmp, this_block);
 147     phi->addIncoming(other_oop_tmp, other_block);
 148     set_oop_tmp(phi);
 149   }
 150 
 151   // Monitors
 152   assert(this->num_monitors() == other->num_monitors(), "should be");
 153 
 154   // Local variables
 155   assert(this->max_locals() == other->max_locals(), "should be");
 156   for (int i = 0; i < max_locals(); i++) {
 157     SharkValue *this_value = this->local(i);
 158     SharkValue *other_value = other->local(i);
 159     assert((this_value == NULL) == (other_value == NULL), "should be");
 160     if (this_value != NULL) {
 161       char name[18];
 162       snprintf(name, sizeof(name), "local_%d_", i);
 163       set_local(i, this_value->merge(
 164         builder(), other_value, other_block, this_block, name));
 165     }
 166   }
 167 
 168   // Expression stack
 169   assert(this->stack_depth() == other->stack_depth(), "should be");
 170   for (int i = 0; i < stack_depth(); i++) {
 171     SharkValue *this_value = this->stack(i);
 172     SharkValue *other_value = other->stack(i);
 173     assert((this_value == NULL) == (other_value == NULL), "should be");
 174     if (this_value != NULL) {
 175       char name[18];
 176       snprintf(name, sizeof(name), "stack_%d_", i);
 177       set_stack(i, this_value->merge(
 178         builder(), other_value, other_block, this_block, name));
 179     }
 180   }
 181 
 182   // Safepointed status
 183   set_has_safepointed(this->has_safepointed() && other->has_safepointed());
 184 }
 185 
 186 void SharkState::replace_all(SharkValue* old_value, SharkValue* new_value) {
 187   // Local variables
 188   for (int i = 0; i < max_locals(); i++) {
 189     if (local(i) == old_value)
 190       set_local(i, new_value);
 191   }
 192 
 193   // Expression stack
 194   for (int i = 0; i < stack_depth(); i++) {
 195     if (stack(i) == old_value)
 196       set_stack(i, new_value);
 197   }
 198 }
 199 
 200 SharkNormalEntryState::SharkNormalEntryState(SharkTopLevelBlock* block,
 201                                              Value*              method)
 202   : SharkState(block) {
 203   assert(!block->stack_depth_at_entry(), "entry block shouldn't have stack");
 204 
 205   // Local variables
 206   for (int i = 0; i < max_locals(); i++) {
 207     ciType *type = block->local_type_at_entry(i);
 208 
 209     SharkValue *value = NULL;
 210     switch (type->basic_type()) {
 211     case T_INT:
 212     case T_LONG:
 213     case T_FLOAT:
 214     case T_DOUBLE:
 215     case T_OBJECT:
 216     case T_ARRAY:
 217       if (i >= arg_size()) {
 218         ShouldNotReachHere();
 219       }
 220       value = SharkValue::create_generic(type, NULL, i == 0 && !is_static());
 221       break;
 222 
 223     case ciTypeFlow::StateVector::T_NULL:
 224       value = SharkValue::null();
 225       break;
 226 
 227     case ciTypeFlow::StateVector::T_BOTTOM:
 228       break;
 229 
 230     case ciTypeFlow::StateVector::T_LONG2:
 231     case ciTypeFlow::StateVector::T_DOUBLE2:
 232       break;
 233 
 234     default:
 235       ShouldNotReachHere();
 236     }
 237     set_local(i, value);
 238   }
 239   SharkNormalEntryCacher(block->function(), method).scan(this);
 240 }
 241 
 242 SharkOSREntryState::SharkOSREntryState(SharkTopLevelBlock* block,
 243                                        Value*              method,
 244                                        Value*              osr_buf)
 245   : SharkState(block) {
 246   assert(!block->stack_depth_at_entry(), "entry block shouldn't have stack");
 247   set_num_monitors(block->ciblock()->monitor_count());
 248 
 249   // Local variables
 250   for (int i = 0; i < max_locals(); i++) {
 251     ciType *type = block->local_type_at_entry(i);
 252 
 253     SharkValue *value = NULL;
 254     switch (type->basic_type()) {
 255     case T_INT:
 256     case T_LONG:
 257     case T_FLOAT:
 258     case T_DOUBLE:
 259     case T_OBJECT:
 260     case T_ARRAY:
 261       value = SharkValue::create_generic(type, NULL, false);
 262       break;
 263 
 264     case ciTypeFlow::StateVector::T_NULL:
 265       value = SharkValue::null();
 266       break;
 267 
 268     case ciTypeFlow::StateVector::T_BOTTOM:
 269       break;
 270 
 271     case ciTypeFlow::StateVector::T_LONG2:
 272     case ciTypeFlow::StateVector::T_DOUBLE2:
 273       break;
 274 
 275     default:
 276       ShouldNotReachHere();
 277     }
 278     set_local(i, value);
 279   }
 280   SharkOSREntryCacher(block->function(), method, osr_buf).scan(this);
 281 }
 282 
 283 SharkPHIState::SharkPHIState(SharkTopLevelBlock* block)
 284   : SharkState(block), _block(block) {
 285   BasicBlock *saved_insert_point = builder()->GetInsertBlock();
 286   builder()->SetInsertPoint(block->entry_block());
 287   char name[18];
 288 
 289   // Method
 290   set_method(builder()->CreatePHI(SharkType::Method_type(), 0, "method"));
 291 
 292   // Local variables
 293   for (int i = 0; i < max_locals(); i++) {
 294     ciType *type = block->local_type_at_entry(i);
 295     if (type->basic_type() == (BasicType) ciTypeFlow::StateVector::T_NULL) {
 296       // XXX we could do all kinds of clever stuff here
 297       type = ciType::make(T_OBJECT); // XXX what about T_ARRAY?
 298     }
 299 
 300     SharkValue *value = NULL;
 301     switch (type->basic_type()) {
 302     case T_INT:
 303     case T_LONG:
 304     case T_FLOAT:
 305     case T_DOUBLE:
 306     case T_OBJECT:
 307     case T_ARRAY:
 308       snprintf(name, sizeof(name), "local_%d_", i);
 309       value = SharkValue::create_phi(
 310         type, builder()->CreatePHI(SharkType::to_stackType(type), 0, name));
 311       break;
 312 
 313     case T_ADDRESS:
 314       value = SharkValue::address_constant(type->as_return_address()->bci());
 315       break;
 316 
 317     case ciTypeFlow::StateVector::T_BOTTOM:
 318       break;
 319 
 320     case ciTypeFlow::StateVector::T_LONG2:
 321     case ciTypeFlow::StateVector::T_DOUBLE2:
 322       break;
 323 
 324     default:
 325       ShouldNotReachHere();
 326     }
 327     set_local(i, value);
 328   }
 329 
 330   // Expression stack
 331   for (int i = 0; i < block->stack_depth_at_entry(); i++) {
 332     ciType *type = block->stack_type_at_entry(i);
 333     if (type->basic_type() == (BasicType) ciTypeFlow::StateVector::T_NULL) {
 334       // XXX we could do all kinds of clever stuff here
 335       type = ciType::make(T_OBJECT); // XXX what about T_ARRAY?
 336     }
 337 
 338     SharkValue *value = NULL;
 339     switch (type->basic_type()) {
 340     case T_INT:
 341     case T_LONG:
 342     case T_FLOAT:
 343     case T_DOUBLE:
 344     case T_OBJECT:
 345     case T_ARRAY:
 346       snprintf(name, sizeof(name), "stack_%d_", i);
 347       value = SharkValue::create_phi(
 348         type, builder()->CreatePHI(SharkType::to_stackType(type), 0, name));
 349       break;
 350 
 351     case T_ADDRESS:
 352       value = SharkValue::address_constant(type->as_return_address()->bci());
 353       break;
 354 
 355     case ciTypeFlow::StateVector::T_LONG2:
 356     case ciTypeFlow::StateVector::T_DOUBLE2:
 357       break;
 358 
 359     default:
 360       ShouldNotReachHere();
 361     }
 362     push(value);
 363   }
 364 
 365   // Monitors
 366   set_num_monitors(block->ciblock()->monitor_count());
 367 
 368   builder()->SetInsertPoint(saved_insert_point);
 369 }
 370 
 371 void SharkPHIState::add_incoming(SharkState* incoming_state) {
 372   BasicBlock *predecessor = builder()->GetInsertBlock();
 373 
 374   // Method
 375   ((PHINode *) method())->addIncoming(incoming_state->method(), predecessor);
 376 
 377   // Local variables
 378   for (int i = 0; i < max_locals(); i++) {
 379     if (local(i) != NULL)
 380       local(i)->addIncoming(incoming_state->local(i), predecessor);
 381   }
 382 
 383   // Expression stack
 384   int stack_depth = block()->stack_depth_at_entry();
 385   assert(stack_depth == incoming_state->stack_depth(), "should be");
 386   for (int i = 0; i < stack_depth; i++) {
 387     assert((stack(i) == NULL) == (incoming_state->stack(i) == NULL), "oops");
 388     if (stack(i))
 389       stack(i)->addIncoming(incoming_state->stack(i), predecessor);
 390   }
 391 
 392   // Monitors
 393   assert(num_monitors() == incoming_state->num_monitors(), "should be");
 394 
 395   // Temporary oop slot
 396   assert(oop_tmp() == incoming_state->oop_tmp(), "should be");
 397 }