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