src/share/vm/c1/c1_ValueStack.cpp

Tue, 29 Dec 2009 19:08:54 +0100

author
roland
date
Tue, 29 Dec 2009 19:08:54 +0100
changeset 2174
f02a8bbe6ed4
parent 1939
b812ff5abc73
child 2314
f95d63e2154a
permissions
-rw-r--r--

6986046: C1 valuestack cleanup
Summary: fixes an historical oddity in C1 with inlining where all of the expression stacks are kept in the topmost ValueStack instead of being in their respective ValueStacks.
Reviewed-by: never
Contributed-by: Christian Wimmer <cwimmer@uci.edu>

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

mercurial