1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/shark/sharkFunction.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,202 @@ 1.4 +/* 1.5 + * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * Copyright 2008, 2009 Red Hat, Inc. 1.7 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.8 + * 1.9 + * This code is free software; you can redistribute it and/or modify it 1.10 + * under the terms of the GNU General Public License version 2 only, as 1.11 + * published by the Free Software Foundation. 1.12 + * 1.13 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.15 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.16 + * version 2 for more details (a copy is included in the LICENSE file that 1.17 + * accompanied this code). 1.18 + * 1.19 + * You should have received a copy of the GNU General Public License version 1.20 + * 2 along with this work; if not, write to the Free Software Foundation, 1.21 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.22 + * 1.23 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.24 + * or visit www.oracle.com if you need additional information or have any 1.25 + * questions. 1.26 + * 1.27 + */ 1.28 + 1.29 +#include "precompiled.hpp" 1.30 +#include "ci/ciTypeFlow.hpp" 1.31 +#include "memory/allocation.hpp" 1.32 +#include "shark/llvmHeaders.hpp" 1.33 +#include "shark/llvmValue.hpp" 1.34 +#include "shark/sharkBuilder.hpp" 1.35 +#include "shark/sharkEntry.hpp" 1.36 +#include "shark/sharkFunction.hpp" 1.37 +#include "shark/sharkState.hpp" 1.38 +#include "shark/sharkTopLevelBlock.hpp" 1.39 +#include "shark/shark_globals.hpp" 1.40 +#include "utilities/debug.hpp" 1.41 + 1.42 +using namespace llvm; 1.43 + 1.44 +void SharkFunction::initialize(const char *name) { 1.45 + // Create the function 1.46 + _function = Function::Create( 1.47 + entry_point_type(), 1.48 + GlobalVariable::InternalLinkage, 1.49 + name); 1.50 + 1.51 + // Get our arguments 1.52 + Function::arg_iterator ai = function()->arg_begin(); 1.53 + Argument *method = ai++; 1.54 + method->setName("method"); 1.55 + Argument *osr_buf = NULL; 1.56 + if (is_osr()) { 1.57 + osr_buf = ai++; 1.58 + osr_buf->setName("osr_buf"); 1.59 + } 1.60 + Argument *base_pc = ai++; 1.61 + base_pc->setName("base_pc"); 1.62 + code_buffer()->set_base_pc(base_pc); 1.63 + Argument *thread = ai++; 1.64 + thread->setName("thread"); 1.65 + set_thread(thread); 1.66 + 1.67 + // Create the list of blocks 1.68 + set_block_insertion_point(NULL); 1.69 + _blocks = NEW_RESOURCE_ARRAY(SharkTopLevelBlock*, block_count()); 1.70 + for (int i = 0; i < block_count(); i++) { 1.71 + ciTypeFlow::Block *b = flow()->pre_order_at(i); 1.72 + 1.73 + // Work around a bug in pre_order_at() that does not return 1.74 + // the correct pre-ordering. If pre_order_at() were correct 1.75 + // this line could simply be: 1.76 + // _blocks[i] = new SharkTopLevelBlock(this, b); 1.77 + _blocks[b->pre_order()] = new SharkTopLevelBlock(this, b); 1.78 + } 1.79 + 1.80 + // Walk the tree from the start block to determine which 1.81 + // blocks are entered and which blocks require phis 1.82 + SharkTopLevelBlock *start_block = block(flow()->start_block_num()); 1.83 + if (is_osr() && start_block->stack_depth_at_entry() != 0) { 1.84 + env()->record_method_not_compilable("can't compile OSR block with incoming stack-depth > 0"); 1.85 + return; 1.86 + } 1.87 + assert(start_block->start() == flow()->start_bci(), "blocks out of order"); 1.88 + start_block->enter(); 1.89 + 1.90 + // Initialize all entered blocks 1.91 + for (int i = 0; i < block_count(); i++) { 1.92 + if (block(i)->entered()) 1.93 + block(i)->initialize(); 1.94 + } 1.95 + 1.96 + // Create and push our stack frame 1.97 + set_block_insertion_point(&function()->front()); 1.98 + builder()->SetInsertPoint(CreateBlock()); 1.99 + _stack = SharkStack::CreateBuildAndPushFrame(this, method); 1.100 + 1.101 + // Create the entry state 1.102 + SharkState *entry_state; 1.103 + if (is_osr()) { 1.104 + entry_state = new SharkOSREntryState(start_block, method, osr_buf); 1.105 + 1.106 + // Free the OSR buffer 1.107 + builder()->CreateCall(builder()->osr_migration_end(), osr_buf); 1.108 + } 1.109 + else { 1.110 + entry_state = new SharkNormalEntryState(start_block, method); 1.111 + 1.112 + // Lock if necessary 1.113 + if (is_synchronized()) { 1.114 + SharkTopLevelBlock *locker = 1.115 + new SharkTopLevelBlock(this, start_block->ciblock()); 1.116 + locker->add_incoming(entry_state); 1.117 + 1.118 + set_block_insertion_point(start_block->entry_block()); 1.119 + locker->acquire_method_lock(); 1.120 + 1.121 + entry_state = locker->current_state(); 1.122 + } 1.123 + } 1.124 + 1.125 + // Transition into the method proper 1.126 + start_block->add_incoming(entry_state); 1.127 + builder()->CreateBr(start_block->entry_block()); 1.128 + 1.129 + // Parse the blocks 1.130 + for (int i = 0; i < block_count(); i++) { 1.131 + if (!block(i)->entered()) 1.132 + continue; 1.133 + 1.134 + if (i + 1 < block_count()) 1.135 + set_block_insertion_point(block(i + 1)->entry_block()); 1.136 + else 1.137 + set_block_insertion_point(NULL); 1.138 + 1.139 + block(i)->emit_IR(); 1.140 + } 1.141 + do_deferred_zero_checks(); 1.142 +} 1.143 + 1.144 +class DeferredZeroCheck : public SharkTargetInvariants { 1.145 + public: 1.146 + DeferredZeroCheck(SharkTopLevelBlock* block, SharkValue* value) 1.147 + : SharkTargetInvariants(block), 1.148 + _block(block), 1.149 + _value(value), 1.150 + _bci(block->bci()), 1.151 + _state(block->current_state()->copy()), 1.152 + _check_block(builder()->GetInsertBlock()), 1.153 + _continue_block(function()->CreateBlock("not_zero")) { 1.154 + builder()->SetInsertPoint(continue_block()); 1.155 + } 1.156 + 1.157 + private: 1.158 + SharkTopLevelBlock* _block; 1.159 + SharkValue* _value; 1.160 + int _bci; 1.161 + SharkState* _state; 1.162 + BasicBlock* _check_block; 1.163 + BasicBlock* _continue_block; 1.164 + 1.165 + public: 1.166 + SharkTopLevelBlock* block() const { 1.167 + return _block; 1.168 + } 1.169 + SharkValue* value() const { 1.170 + return _value; 1.171 + } 1.172 + int bci() const { 1.173 + return _bci; 1.174 + } 1.175 + SharkState* state() const { 1.176 + return _state; 1.177 + } 1.178 + BasicBlock* check_block() const { 1.179 + return _check_block; 1.180 + } 1.181 + BasicBlock* continue_block() const { 1.182 + return _continue_block; 1.183 + } 1.184 + 1.185 + public: 1.186 + SharkFunction* function() const { 1.187 + return block()->function(); 1.188 + } 1.189 + 1.190 + public: 1.191 + void process() const { 1.192 + builder()->SetInsertPoint(check_block()); 1.193 + block()->do_deferred_zero_check(value(), bci(), state(), continue_block()); 1.194 + } 1.195 +}; 1.196 + 1.197 +void SharkFunction::add_deferred_zero_check(SharkTopLevelBlock* block, 1.198 + SharkValue* value) { 1.199 + deferred_zero_checks()->append(new DeferredZeroCheck(block, value)); 1.200 +} 1.201 + 1.202 +void SharkFunction::do_deferred_zero_checks() { 1.203 + for (int i = 0; i < deferred_zero_checks()->length(); i++) 1.204 + deferred_zero_checks()->at(i)->process(); 1.205 +}