annotate src/hotspot/share/c1/c1_ValueStack.cpp @ 54074:9c6508806663

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