sharkTopLevelBlock.hpp 11.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
/*
 * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2008, 2009, 2010 Red Hat, Inc.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

class SharkTopLevelBlock : public SharkBlock {
 public:
  SharkTopLevelBlock(SharkFunction* function, ciTypeFlow::Block* ciblock)
    : SharkBlock(function),
      _function(function),
      _ciblock(ciblock),
      _entered(false),
      _has_trap(false),
      _needs_phis(false),
      _entry_state(NULL),
      _entry_block(NULL) {}

 private:
  SharkFunction*     _function;
  ciTypeFlow::Block* _ciblock;

 public:
  SharkFunction* function() const {
    return _function;
  }
  ciTypeFlow::Block* ciblock() const {
    return _ciblock;
  }

  // Function properties
 public:
  SharkStack* stack() const {
    return function()->stack();
  }

  // Typeflow properties
 public:
  int index() const {
    return ciblock()->pre_order();
  }
  bool is_backedge_copy() const {
    return ciblock()->is_backedge_copy();
  }
  int stack_depth_at_entry() const {
    return ciblock()->stack_size();
  }
  ciType* local_type_at_entry(int index) const {
    return ciblock()->local_type_at(index);
  }
  ciType* stack_type_at_entry(int slot) const {
    return ciblock()->stack_type_at(slot);
  }
  int start() const {
    return ciblock()->start();
  }
  int limit() const {
    return ciblock()->limit();
  }
  bool falls_through() const {
    return ciblock()->control() == ciBlock::fall_through_bci;
  }
  int num_successors() const {
    return ciblock()->successors()->length();
  }
  SharkTopLevelBlock* successor(int index) const {
    return function()->block(ciblock()->successors()->at(index)->pre_order());
  }
  SharkTopLevelBlock* bci_successor(int bci) const;

  // Exceptions
 private:
  GrowableArray<ciExceptionHandler*>* _exc_handlers;
  GrowableArray<SharkTopLevelBlock*>* _exceptions;

 private:
  void compute_exceptions();

 private:
  int num_exceptions() const {
    return _exc_handlers->length();
  }
  ciExceptionHandler* exc_handler(int index) const {
    return _exc_handlers->at(index);
  }
  SharkTopLevelBlock* exception(int index) const {
    return _exceptions->at(index);
  }

  // Traps
 private:
  bool _has_trap;
  int  _trap_request;
  int  _trap_bci;

  void set_trap(int trap_request, int trap_bci) {
    assert(!has_trap(), "shouldn't have");
    _has_trap     = true;
    _trap_request = trap_request;
    _trap_bci     = trap_bci;
  }

 private:
  bool has_trap() {
    return _has_trap;
  }
  int trap_request() {
    assert(has_trap(), "should have");
    return _trap_request;
  }
  int trap_bci() {
    assert(has_trap(), "should have");
    return _trap_bci;
  }

 private:
  void scan_for_traps();

 private:
  bool static_field_ok_in_clinit(ciField* field);

  // Entry state
 private:
  bool _entered;
  bool _needs_phis;

 public:
  bool entered() const {
    return _entered;
  }
  bool needs_phis() const {
    return _needs_phis;
  }

 private:
  void enter(SharkTopLevelBlock* predecessor, bool is_exception);

 public:
  void enter() {
    enter(NULL, false);
  }

 private:
  SharkState* _entry_state;

 private:
  SharkState* entry_state();

 private:
  llvm::BasicBlock* _entry_block;

 public:
  llvm::BasicBlock* entry_block() const {
    return _entry_block;
  }

 public:
  void initialize();

 public:
  void add_incoming(SharkState* incoming_state);

  // Method
 public:
  llvm::Value* method() {
    return current_state()->method();
  }

  // Temporary oop storage
 public:
  void set_oop_tmp(llvm::Value* value) {
    assert(value, "value must be non-NULL (will be reset by get_oop_tmp)");
    assert(!current_state()->oop_tmp(), "oop_tmp gets and sets must match");
    current_state()->set_oop_tmp(value);
  }
  llvm::Value* get_oop_tmp() {
    llvm::Value* value = current_state()->oop_tmp();
    assert(value, "oop_tmp gets and sets must match");
    current_state()->set_oop_tmp(NULL);
    return value;
  }

  // Cache and decache
 private:
  void decache_for_Java_call(ciMethod* callee);
  void cache_after_Java_call(ciMethod* callee);
  void decache_for_VM_call();
  void cache_after_VM_call();
  void decache_for_trap();

  // Monitors
 private:
  int num_monitors() {
    return current_state()->num_monitors();
  }
  int set_num_monitors(int num_monitors) {
    current_state()->set_num_monitors(num_monitors);
  }

  // Code generation
 public:
  void emit_IR();

  // Branch helpers
 private:
  void do_branch(int successor_index);

  // Zero checks
 private:
  void do_zero_check(SharkValue* value);
  void zero_check_value(SharkValue* value, llvm::BasicBlock* continue_block);

 public:
  void do_deferred_zero_check(SharkValue*       value,
                              int               bci,
                              SharkState*       saved_state,
                              llvm::BasicBlock* continue_block);
  // Exceptions
 private:
  llvm::Value* pending_exception_address() const {
    return builder()->CreateAddressOfStructEntry(
      thread(), Thread::pending_exception_offset(),
      llvm::PointerType::getUnqual(SharkType::oop_type()),
      "pending_exception_addr");
  }
  llvm::LoadInst* get_pending_exception() const {
    return builder()->CreateLoad(
      pending_exception_address(), "pending_exception");
  }
  void clear_pending_exception() const {
    builder()->CreateStore(LLVMValue::null(), pending_exception_address());
  }
 public:
  enum ExceptionActionMask {
    // The actual bitmasks that things test against
    EAM_CHECK         = 1, // whether to check for pending exceptions
    EAM_HANDLE        = 2, // whether to attempt to handle pending exceptions
    EAM_MONITOR_FUDGE = 4, // whether the monitor count needs adjusting

    // More convenient values for passing
    EX_CHECK_NONE     = 0,
    EX_CHECK_NO_CATCH = EAM_CHECK,
    EX_CHECK_FULL     = EAM_CHECK | EAM_HANDLE
  };
  void check_pending_exception(int action);
  void handle_exception(llvm::Value* exception, int action);
  void marshal_exception_fast(int num_options);
  void marshal_exception_slow(int num_options);
  llvm::BasicBlock* handler_for_exception(int index);

  // VM calls
 private:
  llvm::CallInst* call_vm(llvm::Value*  callee,
                          llvm::Value** args_start,
                          llvm::Value** args_end,
                          int           exception_action) {
    decache_for_VM_call();
    stack()->CreateSetLastJavaFrame();
    llvm::CallInst *res = builder()->CreateCall(callee, args_start, args_end);
    stack()->CreateResetLastJavaFrame();
    cache_after_VM_call();
    if (exception_action & EAM_CHECK) {
      check_pending_exception(exception_action);
      current_state()->set_has_safepointed(true);
    }
    return res;
  }

 public:
  llvm::CallInst* call_vm(llvm::Value* callee,
                          int          exception_action) {
    llvm::Value *args[] = {thread()};
    return call_vm(callee, args, args + 1, exception_action);
  }
  llvm::CallInst* call_vm(llvm::Value* callee,
                          llvm::Value* arg1,
                          int          exception_action) {
    llvm::Value *args[] = {thread(), arg1};
    return call_vm(callee, args, args + 2, exception_action);
  }
  llvm::CallInst* call_vm(llvm::Value* callee,
                          llvm::Value* arg1,
                          llvm::Value* arg2,
                          int          exception_action) {
    llvm::Value *args[] = {thread(), arg1, arg2};
    return call_vm(callee, args, args + 3, exception_action);
  }
  llvm::CallInst* call_vm(llvm::Value* callee,
                          llvm::Value* arg1,
                          llvm::Value* arg2,
                          llvm::Value* arg3,
                          int          exception_action) {
    llvm::Value *args[] = {thread(), arg1, arg2, arg3};
    return call_vm(callee, args, args + 4, exception_action);
  }

  // VM call oop return handling
 private:
  llvm::LoadInst* get_vm_result() const {
    llvm::Value *addr = builder()->CreateAddressOfStructEntry(
      thread(), JavaThread::vm_result_offset(),
      llvm::PointerType::getUnqual(SharkType::oop_type()),
      "vm_result_addr");
    llvm::LoadInst *result = builder()->CreateLoad(addr, "vm_result");
    builder()->CreateStore(LLVMValue::null(), addr);
    return result;
  }

  // Synchronization
 private:
  void acquire_lock(llvm::Value* lockee, int exception_action);
  void release_lock(int exception_action);

 public:
  void acquire_method_lock();

  // Bounds checks
 private:
  void check_bounds(SharkValue* array, SharkValue* index);

  // Safepoints
 private:
  void maybe_add_safepoint();
  void maybe_add_backedge_safepoint();

  // Loop safepoint removal
 private:
  bool _can_reach_visited;

  bool can_reach(SharkTopLevelBlock* other);
  bool can_reach_helper(SharkTopLevelBlock* other);

  // Traps
 private:
  llvm::BasicBlock* make_trap(int trap_bci, int trap_request);
  void do_trap(int trap_request);

  // Returns
 private:
  void call_register_finalizer(llvm::Value* receiver);
  void handle_return(BasicType type, llvm::Value* exception);

  // arraylength
 private:
  void do_arraylength();

  // *aload and *astore
 private:
  void do_aload(BasicType basic_type);
  void do_astore(BasicType basic_type);

  // *return and athrow
 private:
  void do_return(BasicType type);
  void do_athrow();

  // goto*
 private:
  void do_goto();

  // jsr* and ret
 private:
  void do_jsr();
  void do_ret();

  // if*
 private:
  void do_if_helper(llvm::ICmpInst::Predicate p,
                    llvm::Value*              b,
                    llvm::Value*              a,
                    SharkState*               if_taken_state,
                    SharkState*               not_taken_state);
  void do_if(llvm::ICmpInst::Predicate p, SharkValue* b, SharkValue* a);

  // tableswitch and lookupswitch
 private:
  void do_switch();

  // invoke*
 private:
  ciMethod* improve_virtual_call(ciMethod*        caller,
                                 ciInstanceKlass* klass,
                                 ciMethod*        dest_method,
                                 ciType*          receiver_type);
  llvm::Value* get_direct_callee(ciMethod* method);
  llvm::Value* get_virtual_callee(SharkValue* receiver, int vtable_index);
  llvm::Value* get_interface_callee(SharkValue* receiver, ciMethod* method);

  void do_call();

  // checkcast and instanceof
 private:
  bool static_subtype_check(ciKlass* check_klass, ciKlass* object_klass);
  void do_full_instance_check(ciKlass* klass);
  void do_trapping_instance_check(ciKlass* klass);

  void do_instance_check();
  bool maybe_do_instanceof_if();

  // new and *newarray
 private:
  void do_new();
  void do_newarray();
  void do_anewarray();
  void do_multianewarray();

  // monitorenter and monitorexit
 private:
  void do_monitorenter();
  void do_monitorexit();
};