Path: blob/master/src/hotspot/share/c1/c1_ValueStack.cpp
40931 views
/*1* Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "c1/c1_IR.hpp"26#include "c1/c1_InstructionPrinter.hpp"27#include "c1/c1_ValueStack.hpp"282930// Implementation of ValueStack3132ValueStack::ValueStack(IRScope* scope, ValueStack* caller_state)33: _scope(scope)34, _caller_state(caller_state)35, _bci(-99)36, _kind(Parsing)37, _locals(scope->method()->max_locals(), scope->method()->max_locals(), NULL)38, _stack(scope->method()->max_stack())39, _locks(NULL)40{41verify();42}4344ValueStack::ValueStack(ValueStack* copy_from, Kind kind, int bci)45: _scope(copy_from->scope())46, _caller_state(copy_from->caller_state())47, _bci(bci)48, _kind(kind)49, _locals(copy_from->locals_size_for_copy(kind))50, _stack(copy_from->stack_size_for_copy(kind))51, _locks(copy_from->locks_size() == 0 ? NULL : new Values(copy_from->locks_size()))52{53assert(kind != EmptyExceptionState || !Compilation::current()->env()->should_retain_local_variables(), "need locals");54if (kind != EmptyExceptionState) {55_locals.appendAll(©_from->_locals);56}5758if (kind != ExceptionState && kind != EmptyExceptionState) {59_stack.appendAll(©_from->_stack);60}6162if (copy_from->locks_size() > 0) {63_locks->appendAll(copy_from->_locks);64}6566verify();67}6869int ValueStack::locals_size_for_copy(Kind kind) const {70if (kind != EmptyExceptionState) {71return locals_size();72}73return 0;74}7576int ValueStack::stack_size_for_copy(Kind kind) const {77if (kind != ExceptionState && kind != EmptyExceptionState) {78if (kind == Parsing) {79// stack will be modified, so reserve enough space to avoid resizing80return scope()->method()->max_stack();81} else {82// stack will not be modified, so do not waste space83return stack_size();84}85}86return 0;87}8889bool ValueStack::is_same(ValueStack* s) {90if (scope() != s->scope()) return false;91if (caller_state() != s->caller_state()) return false;9293if (locals_size() != s->locals_size()) return false;94if (stack_size() != s->stack_size()) return false;95if (locks_size() != s->locks_size()) return false;9697// compare each stack element with the corresponding stack element of s98int index;99Value value;100for_each_stack_value(this, index, value) {101if (value->type()->tag() != s->stack_at(index)->type()->tag()) return false;102}103for (int i = 0; i < locks_size(); i++) {104value = lock_at(i);105if (value != NULL && value != s->lock_at(i)) {106return false;107}108}109return true;110}111112void ValueStack::clear_locals() {113for (int i = _locals.length() - 1; i >= 0; i--) {114_locals.at_put(i, NULL);115}116}117118119void ValueStack::pin_stack_for_linear_scan() {120for_each_state_value(this, v,121if (v->as_Constant() == NULL && v->as_Local() == NULL) {122v->pin(Instruction::PinStackForStateSplit);123}124);125}126127128// apply function to all values of a list; factored out from values_do(f)129void ValueStack::apply(const Values& list, ValueVisitor* f) {130for (int i = 0; i < list.length(); i++) {131Value* va = list.adr_at(i);132Value v0 = *va;133if (v0 != NULL && !v0->type()->is_illegal()) {134f->visit(va);135#ifdef ASSERT136Value v1 = *va;137assert(v1->type()->is_illegal() || v0->type()->tag() == v1->type()->tag(), "types must match");138assert(!v1->type()->is_double_word() || list.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");139#endif140if (v0->type()->is_double_word()) i++;141}142}143}144145146void ValueStack::values_do(ValueVisitor* f) {147ValueStack* state = this;148for_each_state(state) {149apply(state->_locals, f);150apply(state->_stack, f);151if (state->_locks != NULL) {152apply(*state->_locks, f);153}154}155}156157158Values* ValueStack::pop_arguments(int argument_size) {159assert(stack_size() >= argument_size, "stack too small or too many arguments");160int base = stack_size() - argument_size;161Values* args = new Values(argument_size);162for (int i = base; i < stack_size();) args->push(stack_at_inc(i));163truncate_stack(base);164return args;165}166167168int ValueStack::total_locks_size() const {169int num_locks = 0;170const ValueStack* state = this;171for_each_state(state) {172num_locks += state->locks_size();173}174return num_locks;175}176177int ValueStack::lock(Value obj) {178if (_locks == NULL) {179_locks = new Values();180}181_locks->push(obj);182int num_locks = total_locks_size();183scope()->set_min_number_of_locks(num_locks);184return num_locks - 1;185}186187188int ValueStack::unlock() {189assert(locks_size() > 0, "sanity");190_locks->pop();191return total_locks_size();192}193194195void ValueStack::setup_phi_for_stack(BlockBegin* b, int index) {196assert(stack_at(index)->as_Phi() == NULL || stack_at(index)->as_Phi()->block() != b, "phi function already created");197198ValueType* t = stack_at(index)->type();199Value phi = new Phi(t, b, -index - 1);200_stack.at_put(index, phi);201202assert(!t->is_double_word() || _stack.at(index + 1) == NULL, "hi-word of doubleword value must be NULL");203}204205void ValueStack::setup_phi_for_local(BlockBegin* b, int index) {206assert(local_at(index)->as_Phi() == NULL || local_at(index)->as_Phi()->block() != b, "phi function already created");207208ValueType* t = local_at(index)->type();209Value phi = new Phi(t, b, index);210store_local(index, phi);211}212213#ifndef PRODUCT214215void ValueStack::print() {216scope()->method()->print_name();217tty->cr();218if (stack_is_empty()) {219tty->print_cr("empty stack");220} else {221InstructionPrinter ip;222for (int i = 0; i < stack_size();) {223Value t = stack_at_inc(i);224tty->print("%2d ", i);225tty->print("%c%d ", t->type()->tchar(), t->id());226ip.print_instr(t);227tty->cr();228}229}230if (!no_active_locks()) {231InstructionPrinter ip;232for (int i = 0; i < locks_size(); i++) {233Value t = lock_at(i);234tty->print("lock %2d ", i);235if (t == NULL) {236tty->print("this");237} else {238tty->print("%c%d ", t->type()->tchar(), t->id());239ip.print_instr(t);240}241tty->cr();242}243}244if (locals_size() > 0) {245InstructionPrinter ip;246for (int i = 0; i < locals_size();) {247Value l = _locals.at(i);248tty->print("local %d ", i);249if (l == NULL) {250tty->print("null");251i ++;252} else {253tty->print("%c%d ", l->type()->tchar(), l->id());254ip.print_instr(l);255if (l->type()->is_illegal() || l->type()->is_single_word()) i ++; else i += 2;256}257tty->cr();258}259}260261if (caller_state() != NULL) {262caller_state()->print();263}264}265266267void ValueStack::verify() {268assert(scope() != NULL, "scope must exist");269if (caller_state() != NULL) {270assert(caller_state()->scope() == scope()->caller(), "invalid caller scope");271caller_state()->verify();272}273274if (kind() == Parsing) {275assert(bci() == -99, "bci not defined during parsing");276} else {277assert(bci() >= -1, "bci out of range");278assert(bci() < scope()->method()->code_size(), "bci out of range");279assert(bci() == SynchronizationEntryBCI || Bytecodes::is_defined(scope()->method()->java_code_at_bci(bci())), "make sure bci points at a real bytecode");280assert(scope()->method()->liveness_at_bci(bci()).is_valid(), "liveness at bci must be valid");281}282283int i;284for (i = 0; i < stack_size(); i++) {285Value v = _stack.at(i);286if (v == NULL) {287assert(_stack.at(i - 1)->type()->is_double_word(), "only hi-words are NULL on stack");288} else if (v->type()->is_double_word()) {289assert(_stack.at(i + 1) == NULL, "hi-word must be NULL");290}291}292293for (i = 0; i < locals_size(); i++) {294Value v = _locals.at(i);295if (v != NULL && v->type()->is_double_word()) {296assert(_locals.at(i + 1) == NULL, "hi-word must be NULL");297}298}299300for_each_state_value(this, v,301assert(v != NULL, "just test if state-iteration succeeds");302);303}304#endif // PRODUCT305306307