Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/c1/c1_ValueStack.cpp
32285 views
/*1* Copyright (c) 1999, 2012, 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(), NULL)38, _stack(scope->method()->max_stack())39, _locks()40{41verify();42}434445ValueStack::ValueStack(ValueStack* copy_from, Kind kind, int bci)46: _scope(copy_from->scope())47, _caller_state(copy_from->caller_state())48, _bci(bci)49, _kind(kind)50, _locals()51, _stack()52, _locks(copy_from->locks_size())53{54assert(kind != EmptyExceptionState || !Compilation::current()->env()->should_retain_local_variables(), "need locals");55if (kind != EmptyExceptionState) {56// only allocate space if we need to copy the locals-array57_locals = Values(copy_from->locals_size());58_locals.appendAll(©_from->_locals);59}6061if (kind != ExceptionState && kind != EmptyExceptionState) {62if (kind == Parsing) {63// stack will be modified, so reserve enough space to avoid resizing64_stack = Values(scope()->method()->max_stack());65} else {66// stack will not be modified, so do not waste space67_stack = Values(copy_from->stack_size());68}69_stack.appendAll(©_from->_stack);70}7172_locks.appendAll(©_from->_locks);7374verify();75}767778bool ValueStack::is_same(ValueStack* s) {79if (scope() != s->scope()) return false;80if (caller_state() != s->caller_state()) return false;8182if (locals_size() != s->locals_size()) return false;83if (stack_size() != s->stack_size()) return false;84if (locks_size() != s->locks_size()) return false;8586// compare each stack element with the corresponding stack element of s87int index;88Value value;89for_each_stack_value(this, index, value) {90if (value->type()->tag() != s->stack_at(index)->type()->tag()) return false;91}92for_each_lock_value(this, index, value) {93if (value != s->lock_at(index)) return false;94}95return true;96}9798void ValueStack::clear_locals() {99for (int i = _locals.length() - 1; i >= 0; i--) {100_locals.at_put(i, NULL);101}102}103104105void ValueStack::pin_stack_for_linear_scan() {106for_each_state_value(this, v,107if (v->as_Constant() == NULL && v->as_Local() == NULL) {108v->pin(Instruction::PinStackForStateSplit);109}110);111}112113114// apply function to all values of a list; factored out from values_do(f)115void ValueStack::apply(Values list, ValueVisitor* f) {116for (int i = 0; i < list.length(); i++) {117Value* va = list.adr_at(i);118Value v0 = *va;119if (v0 != NULL && !v0->type()->is_illegal()) {120f->visit(va);121#ifdef ASSERT122Value v1 = *va;123assert(v1->type()->is_illegal() || v0->type()->tag() == v1->type()->tag(), "types must match");124assert(!v1->type()->is_double_word() || list.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");125#endif126if (v0->type()->is_double_word()) i++;127}128}129}130131132void ValueStack::values_do(ValueVisitor* f) {133ValueStack* state = this;134for_each_state(state) {135apply(state->_locals, f);136apply(state->_stack, f);137apply(state->_locks, f);138}139}140141142Values* ValueStack::pop_arguments(int argument_size) {143assert(stack_size() >= argument_size, "stack too small or too many arguments");144int base = stack_size() - argument_size;145Values* args = new Values(argument_size);146for (int i = base; i < stack_size();) args->push(stack_at_inc(i));147truncate_stack(base);148return args;149}150151152int ValueStack::total_locks_size() const {153int num_locks = 0;154const ValueStack* state = this;155for_each_state(state) {156num_locks += state->locks_size();157}158return num_locks;159}160161int ValueStack::lock(Value obj) {162_locks.push(obj);163int num_locks = total_locks_size();164scope()->set_min_number_of_locks(num_locks);165return num_locks - 1;166}167168169int ValueStack::unlock() {170_locks.pop();171return total_locks_size();172}173174175void ValueStack::setup_phi_for_stack(BlockBegin* b, int index) {176assert(stack_at(index)->as_Phi() == NULL || stack_at(index)->as_Phi()->block() != b, "phi function already created");177178ValueType* t = stack_at(index)->type();179Value phi = new Phi(t, b, -index - 1);180_stack[index] = phi;181182assert(!t->is_double_word() || _stack.at(index + 1) == NULL, "hi-word of doubleword value must be NULL");183}184185void ValueStack::setup_phi_for_local(BlockBegin* b, int index) {186assert(local_at(index)->as_Phi() == NULL || local_at(index)->as_Phi()->block() != b, "phi function already created");187188ValueType* t = local_at(index)->type();189Value phi = new Phi(t, b, index);190store_local(index, phi);191}192193#ifndef PRODUCT194195void ValueStack::print() {196scope()->method()->print_name();197tty->cr();198if (stack_is_empty()) {199tty->print_cr("empty stack");200} else {201InstructionPrinter ip;202for (int i = 0; i < stack_size();) {203Value t = stack_at_inc(i);204tty->print("%2d ", i);205tty->print("%c%d ", t->type()->tchar(), t->id());206ip.print_instr(t);207tty->cr();208}209}210if (!no_active_locks()) {211InstructionPrinter ip;212for (int i = 0; i < locks_size(); i++) {213Value t = lock_at(i);214tty->print("lock %2d ", i);215if (t == NULL) {216tty->print("this");217} else {218tty->print("%c%d ", t->type()->tchar(), t->id());219ip.print_instr(t);220}221tty->cr();222}223}224if (locals_size() > 0) {225InstructionPrinter ip;226for (int i = 0; i < locals_size();) {227Value l = _locals[i];228tty->print("local %d ", i);229if (l == NULL) {230tty->print("null");231i ++;232} else {233tty->print("%c%d ", l->type()->tchar(), l->id());234ip.print_instr(l);235if (l->type()->is_illegal() || l->type()->is_single_word()) i ++; else i += 2;236}237tty->cr();238}239}240241if (caller_state() != NULL) {242caller_state()->print();243}244}245246247void ValueStack::verify() {248assert(scope() != NULL, "scope must exist");249if (caller_state() != NULL) {250assert(caller_state()->scope() == scope()->caller(), "invalid caller scope");251caller_state()->verify();252}253254if (kind() == Parsing) {255assert(bci() == -99, "bci not defined during parsing");256} else {257assert(bci() >= -1, "bci out of range");258assert(bci() < scope()->method()->code_size(), "bci out of range");259assert(bci() == SynchronizationEntryBCI || Bytecodes::is_defined(scope()->method()->java_code_at_bci(bci())), "make sure bci points at a real bytecode");260assert(scope()->method()->liveness_at_bci(bci()).is_valid(), "liveness at bci must be valid");261}262263int i;264for (i = 0; i < stack_size(); i++) {265Value v = _stack.at(i);266if (v == NULL) {267assert(_stack.at(i - 1)->type()->is_double_word(), "only hi-words are NULL on stack");268} else if (v->type()->is_double_word()) {269assert(_stack.at(i + 1) == NULL, "hi-word must be NULL");270}271}272273for (i = 0; i < locals_size(); i++) {274Value v = _locals.at(i);275if (v != NULL && v->type()->is_double_word()) {276assert(_locals.at(i + 1) == NULL, "hi-word must be NULL");277}278}279280for_each_state_value(this, v,281assert(v != NULL, "just test if state-iteration succeeds");282);283}284#endif // PRODUCT285286287