src/share/vm/c1/c1_ValueStack.cpp

Thu, 24 May 2018 18:41:44 +0800

author
aoqi
date
Thu, 24 May 2018 18:41:44 +0800
changeset 8856
ac27a9c85bea
parent 6876
710a3c8b516e
child 10015
eb7ce841ccec
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "c1/c1_IR.hpp"
aoqi@0 27 #include "c1/c1_InstructionPrinter.hpp"
aoqi@0 28 #include "c1/c1_ValueStack.hpp"
aoqi@0 29
aoqi@0 30
aoqi@0 31 // Implementation of ValueStack
aoqi@0 32
aoqi@0 33 ValueStack::ValueStack(IRScope* scope, ValueStack* caller_state)
aoqi@0 34 : _scope(scope)
aoqi@0 35 , _caller_state(caller_state)
aoqi@0 36 , _bci(-99)
aoqi@0 37 , _kind(Parsing)
aoqi@0 38 , _locals(scope->method()->max_locals(), NULL)
aoqi@0 39 , _stack(scope->method()->max_stack())
aoqi@0 40 , _locks()
aoqi@0 41 {
aoqi@0 42 verify();
aoqi@0 43 }
aoqi@0 44
aoqi@0 45
aoqi@0 46 ValueStack::ValueStack(ValueStack* copy_from, Kind kind, int bci)
aoqi@0 47 : _scope(copy_from->scope())
aoqi@0 48 , _caller_state(copy_from->caller_state())
aoqi@0 49 , _bci(bci)
aoqi@0 50 , _kind(kind)
aoqi@0 51 , _locals()
aoqi@0 52 , _stack()
aoqi@0 53 , _locks(copy_from->locks_size())
aoqi@0 54 {
aoqi@0 55 assert(kind != EmptyExceptionState || !Compilation::current()->env()->jvmti_can_access_local_variables(), "need locals");
aoqi@0 56 if (kind != EmptyExceptionState) {
aoqi@0 57 // only allocate space if we need to copy the locals-array
aoqi@0 58 _locals = Values(copy_from->locals_size());
aoqi@0 59 _locals.appendAll(&copy_from->_locals);
aoqi@0 60 }
aoqi@0 61
aoqi@0 62 if (kind != ExceptionState && kind != EmptyExceptionState) {
aoqi@0 63 if (kind == Parsing) {
aoqi@0 64 // stack will be modified, so reserve enough space to avoid resizing
aoqi@0 65 _stack = Values(scope()->method()->max_stack());
aoqi@0 66 } else {
aoqi@0 67 // stack will not be modified, so do not waste space
aoqi@0 68 _stack = Values(copy_from->stack_size());
aoqi@0 69 }
aoqi@0 70 _stack.appendAll(&copy_from->_stack);
aoqi@0 71 }
aoqi@0 72
aoqi@0 73 _locks.appendAll(&copy_from->_locks);
aoqi@0 74
aoqi@0 75 verify();
aoqi@0 76 }
aoqi@0 77
aoqi@0 78
aoqi@0 79 bool ValueStack::is_same(ValueStack* s) {
aoqi@0 80 if (scope() != s->scope()) return false;
aoqi@0 81 if (caller_state() != s->caller_state()) return false;
aoqi@0 82
aoqi@0 83 if (locals_size() != s->locals_size()) return false;
aoqi@0 84 if (stack_size() != s->stack_size()) return false;
aoqi@0 85 if (locks_size() != s->locks_size()) return false;
aoqi@0 86
aoqi@0 87 // compare each stack element with the corresponding stack element of s
aoqi@0 88 int index;
aoqi@0 89 Value value;
aoqi@0 90 for_each_stack_value(this, index, value) {
aoqi@0 91 if (value->type()->tag() != s->stack_at(index)->type()->tag()) return false;
aoqi@0 92 }
aoqi@0 93 for_each_lock_value(this, index, value) {
aoqi@0 94 if (value != s->lock_at(index)) return false;
aoqi@0 95 }
aoqi@0 96 return true;
aoqi@0 97 }
aoqi@0 98
aoqi@0 99 void ValueStack::clear_locals() {
aoqi@0 100 for (int i = _locals.length() - 1; i >= 0; i--) {
aoqi@0 101 _locals.at_put(i, NULL);
aoqi@0 102 }
aoqi@0 103 }
aoqi@0 104
aoqi@0 105
aoqi@0 106 void ValueStack::pin_stack_for_linear_scan() {
aoqi@0 107 for_each_state_value(this, v,
aoqi@0 108 if (v->as_Constant() == NULL && v->as_Local() == NULL) {
aoqi@0 109 v->pin(Instruction::PinStackForStateSplit);
aoqi@0 110 }
aoqi@0 111 );
aoqi@0 112 }
aoqi@0 113
aoqi@0 114
aoqi@0 115 // apply function to all values of a list; factored out from values_do(f)
aoqi@0 116 void ValueStack::apply(Values list, ValueVisitor* f) {
aoqi@0 117 for (int i = 0; i < list.length(); i++) {
aoqi@0 118 Value* va = list.adr_at(i);
aoqi@0 119 Value v0 = *va;
aoqi@0 120 if (v0 != NULL && !v0->type()->is_illegal()) {
aoqi@0 121 f->visit(va);
aoqi@0 122 #ifdef ASSERT
aoqi@0 123 Value v1 = *va;
aoqi@0 124 assert(v1->type()->is_illegal() || v0->type()->tag() == v1->type()->tag(), "types must match");
aoqi@0 125 assert(!v1->type()->is_double_word() || list.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");
aoqi@0 126 #endif
aoqi@0 127 if (v0->type()->is_double_word()) i++;
aoqi@0 128 }
aoqi@0 129 }
aoqi@0 130 }
aoqi@0 131
aoqi@0 132
aoqi@0 133 void ValueStack::values_do(ValueVisitor* f) {
aoqi@0 134 ValueStack* state = this;
aoqi@0 135 for_each_state(state) {
aoqi@0 136 apply(state->_locals, f);
aoqi@0 137 apply(state->_stack, f);
aoqi@0 138 apply(state->_locks, f);
aoqi@0 139 }
aoqi@0 140 }
aoqi@0 141
aoqi@0 142
aoqi@0 143 Values* ValueStack::pop_arguments(int argument_size) {
aoqi@0 144 assert(stack_size() >= argument_size, "stack too small or too many arguments");
aoqi@0 145 int base = stack_size() - argument_size;
aoqi@0 146 Values* args = new Values(argument_size);
aoqi@0 147 for (int i = base; i < stack_size();) args->push(stack_at_inc(i));
aoqi@0 148 truncate_stack(base);
aoqi@0 149 return args;
aoqi@0 150 }
aoqi@0 151
aoqi@0 152
aoqi@0 153 int ValueStack::total_locks_size() const {
aoqi@0 154 int num_locks = 0;
aoqi@0 155 const ValueStack* state = this;
aoqi@0 156 for_each_state(state) {
aoqi@0 157 num_locks += state->locks_size();
aoqi@0 158 }
aoqi@0 159 return num_locks;
aoqi@0 160 }
aoqi@0 161
aoqi@0 162 int ValueStack::lock(Value obj) {
aoqi@0 163 _locks.push(obj);
aoqi@0 164 int num_locks = total_locks_size();
aoqi@0 165 scope()->set_min_number_of_locks(num_locks);
aoqi@0 166 return num_locks - 1;
aoqi@0 167 }
aoqi@0 168
aoqi@0 169
aoqi@0 170 int ValueStack::unlock() {
aoqi@0 171 _locks.pop();
aoqi@0 172 return total_locks_size();
aoqi@0 173 }
aoqi@0 174
aoqi@0 175
aoqi@0 176 void ValueStack::setup_phi_for_stack(BlockBegin* b, int index) {
aoqi@0 177 assert(stack_at(index)->as_Phi() == NULL || stack_at(index)->as_Phi()->block() != b, "phi function already created");
aoqi@0 178
aoqi@0 179 ValueType* t = stack_at(index)->type();
aoqi@0 180 Value phi = new Phi(t, b, -index - 1);
aoqi@0 181 _stack[index] = phi;
aoqi@0 182
aoqi@0 183 assert(!t->is_double_word() || _stack.at(index + 1) == NULL, "hi-word of doubleword value must be NULL");
aoqi@0 184 }
aoqi@0 185
aoqi@0 186 void ValueStack::setup_phi_for_local(BlockBegin* b, int index) {
aoqi@0 187 assert(local_at(index)->as_Phi() == NULL || local_at(index)->as_Phi()->block() != b, "phi function already created");
aoqi@0 188
aoqi@0 189 ValueType* t = local_at(index)->type();
aoqi@0 190 Value phi = new Phi(t, b, index);
aoqi@0 191 store_local(index, phi);
aoqi@0 192 }
aoqi@0 193
aoqi@0 194 #ifndef PRODUCT
aoqi@0 195
aoqi@0 196 void ValueStack::print() {
aoqi@0 197 scope()->method()->print_name();
aoqi@0 198 tty->cr();
aoqi@0 199 if (stack_is_empty()) {
aoqi@0 200 tty->print_cr("empty stack");
aoqi@0 201 } else {
aoqi@0 202 InstructionPrinter ip;
aoqi@0 203 for (int i = 0; i < stack_size();) {
aoqi@0 204 Value t = stack_at_inc(i);
aoqi@0 205 tty->print("%2d ", i);
aoqi@0 206 tty->print("%c%d ", t->type()->tchar(), t->id());
aoqi@0 207 ip.print_instr(t);
aoqi@0 208 tty->cr();
aoqi@0 209 }
aoqi@0 210 }
aoqi@0 211 if (!no_active_locks()) {
aoqi@0 212 InstructionPrinter ip;
aoqi@0 213 for (int i = 0; i < locks_size(); i++) {
aoqi@0 214 Value t = lock_at(i);
aoqi@0 215 tty->print("lock %2d ", i);
aoqi@0 216 if (t == NULL) {
aoqi@0 217 tty->print("this");
aoqi@0 218 } else {
aoqi@0 219 tty->print("%c%d ", t->type()->tchar(), t->id());
aoqi@0 220 ip.print_instr(t);
aoqi@0 221 }
aoqi@0 222 tty->cr();
aoqi@0 223 }
aoqi@0 224 }
aoqi@0 225 if (locals_size() > 0) {
aoqi@0 226 InstructionPrinter ip;
aoqi@0 227 for (int i = 0; i < locals_size();) {
aoqi@0 228 Value l = _locals[i];
aoqi@0 229 tty->print("local %d ", i);
aoqi@0 230 if (l == NULL) {
aoqi@0 231 tty->print("null");
aoqi@0 232 i ++;
aoqi@0 233 } else {
aoqi@0 234 tty->print("%c%d ", l->type()->tchar(), l->id());
aoqi@0 235 ip.print_instr(l);
aoqi@0 236 if (l->type()->is_illegal() || l->type()->is_single_word()) i ++; else i += 2;
aoqi@0 237 }
aoqi@0 238 tty->cr();
aoqi@0 239 }
aoqi@0 240 }
aoqi@0 241
aoqi@0 242 if (caller_state() != NULL) {
aoqi@0 243 caller_state()->print();
aoqi@0 244 }
aoqi@0 245 }
aoqi@0 246
aoqi@0 247
aoqi@0 248 void ValueStack::verify() {
aoqi@0 249 assert(scope() != NULL, "scope must exist");
aoqi@0 250 if (caller_state() != NULL) {
aoqi@0 251 assert(caller_state()->scope() == scope()->caller(), "invalid caller scope");
aoqi@0 252 caller_state()->verify();
aoqi@0 253 }
aoqi@0 254
aoqi@0 255 if (kind() == Parsing) {
aoqi@0 256 assert(bci() == -99, "bci not defined during parsing");
aoqi@0 257 } else {
aoqi@0 258 assert(bci() >= -1, "bci out of range");
aoqi@0 259 assert(bci() < scope()->method()->code_size(), "bci out of range");
aoqi@0 260 assert(bci() == SynchronizationEntryBCI || Bytecodes::is_defined(scope()->method()->java_code_at_bci(bci())), "make sure bci points at a real bytecode");
aoqi@0 261 assert(scope()->method()->liveness_at_bci(bci()).is_valid(), "liveness at bci must be valid");
aoqi@0 262 }
aoqi@0 263
aoqi@0 264 int i;
aoqi@0 265 for (i = 0; i < stack_size(); i++) {
aoqi@0 266 Value v = _stack.at(i);
aoqi@0 267 if (v == NULL) {
aoqi@0 268 assert(_stack.at(i - 1)->type()->is_double_word(), "only hi-words are NULL on stack");
aoqi@0 269 } else if (v->type()->is_double_word()) {
aoqi@0 270 assert(_stack.at(i + 1) == NULL, "hi-word must be NULL");
aoqi@0 271 }
aoqi@0 272 }
aoqi@0 273
aoqi@0 274 for (i = 0; i < locals_size(); i++) {
aoqi@0 275 Value v = _locals.at(i);
aoqi@0 276 if (v != NULL && v->type()->is_double_word()) {
aoqi@0 277 assert(_locals.at(i + 1) == NULL, "hi-word must be NULL");
aoqi@0 278 }
aoqi@0 279 }
aoqi@0 280
aoqi@0 281 for_each_state_value(this, v,
aoqi@0 282 assert(v != NULL, "just test if state-iteration succeeds");
aoqi@0 283 );
aoqi@0 284 }
aoqi@0 285 #endif // PRODUCT

mercurial