1 /* 2 * Copyright (c) 1999, 2024, 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 , _force_reexecute(false) 43 { 44 verify(); 45 } 46 47 ValueStack::ValueStack(ValueStack* copy_from, Kind kind, int bci, bool reexecute) 48 : _scope(copy_from->scope()) 49 , _caller_state(copy_from->caller_state()) 50 , _bci(bci) 51 , _kind(kind) 52 , _should_reexecute(reexecute) 53 , _locals(copy_from->locals_size_for_copy(kind)) 54 , _stack(copy_from->stack_size_for_copy(kind)) 55 , _locks(copy_from->locks_size() == 0 ? nullptr : new Values(copy_from->locks_size())) 56 , _force_reexecute(false) 57 { 58 switch (kind) { 59 case EmptyExceptionState: 60 case CallerEmptyExceptionState: 61 assert(!Compilation::current()->env()->should_retain_local_variables(), "need locals"); 62 // set to all nulls, like clear_locals() 63 for (int i = 0; i < copy_from->locals_size(); ++i) { 64 _locals.append(nullptr); 65 } 66 break; 67 default: 68 _locals.appendAll(©_from->_locals); 69 } 70 71 switch (kind) { 72 case ExceptionState: 73 case EmptyExceptionState: 74 assert(stack_size() == 0, "fix stack_size_for_copy"); 75 break; 76 case CallerExceptionState: 77 case CallerEmptyExceptionState: 78 // set to all nulls 79 for (int i = 0; i < copy_from->stack_size(); ++i) { 80 _stack.append(nullptr); 81 } 82 break; 83 default: 84 _stack.appendAll(©_from->_stack); 85 } 86 87 if (copy_from->locks_size() > 0) { 88 _locks->appendAll(copy_from->_locks); 89 } 90 91 verify(); 92 } 93 94 int ValueStack::locals_size_for_copy(Kind kind) const { 95 return locals_size(); 96 } 97 98 int ValueStack::stack_size_for_copy(Kind kind) const { 99 if (kind != ExceptionState && kind != EmptyExceptionState) { 100 if (kind == Parsing) { 101 // stack will be modified, so reserve enough space to avoid resizing 102 return scope()->method()->max_stack(); 103 } else { 104 // stack will not be modified, so do not waste space 105 return stack_size(); 106 } 107 } 108 return 0; 109 } 110 111 bool ValueStack::is_same(ValueStack* s) { 112 if (scope() != s->scope()) return false; 113 if (caller_state() != s->caller_state()) return false; 114 115 if (locals_size() != s->locals_size()) return false; 116 if (stack_size() != s->stack_size()) return false; 117 if (locks_size() != s->locks_size()) return false; 118 119 // compare each stack element with the corresponding stack element of s 120 int index; 121 Value value; 122 for_each_stack_value(this, index, value) { 123 if (value->type()->tag() != s->stack_at(index)->type()->tag()) return false; 124 } 125 for (int i = 0; i < locks_size(); i++) { 126 value = lock_at(i); 127 if (value != nullptr && value != s->lock_at(i)) { 128 return false; 129 } 130 } 131 return true; 132 } 133 134 void ValueStack::clear_locals() { 135 for (int i = _locals.length() - 1; i >= 0; i--) { 136 _locals.at_put(i, nullptr); 137 } 138 } 139 140 141 void ValueStack::pin_stack_for_linear_scan() { 142 for_each_state_value(this, v, 143 if (v->as_Constant() == nullptr && v->as_Local() == nullptr) { 144 v->pin(Instruction::PinStackForStateSplit); 145 } 146 ); 147 } 148 149 150 // apply function to all values of a list; factored out from values_do(f) 151 void ValueStack::apply(const Values& list, ValueVisitor* f) { 152 for (int i = 0; i < list.length(); i++) { 153 Value* va = list.adr_at(i); 154 Value v0 = *va; 155 if (v0 != nullptr && !v0->type()->is_illegal()) { 156 f->visit(va); 157 #ifdef ASSERT 158 Value v1 = *va; 159 assert(v1->type()->is_illegal() || v0->type()->tag() == v1->type()->tag(), "types must match"); 160 assert(!v1->type()->is_double_word() || list.at(i + 1) == nullptr, "hi-word of doubleword value must be null"); 161 #endif 162 if (v0->type()->is_double_word()) i++; 163 } 164 } 165 } 166 167 168 void ValueStack::values_do(ValueVisitor* f) { 169 ValueStack* state = this; 170 for_each_state(state) { 171 apply(state->_locals, f); 172 apply(state->_stack, f); 173 if (state->_locks != nullptr) { 174 apply(*state->_locks, f); 175 } 176 } 177 } 178 179 180 Values* ValueStack::pop_arguments(int argument_size) { 181 assert(stack_size() >= argument_size, "stack too small or too many arguments"); 182 int base = stack_size() - argument_size; 183 Values* args = new Values(argument_size); 184 for (int i = base; i < stack_size();) args->push(stack_at_inc(i)); 185 truncate_stack(base); 186 return args; 187 } 188 189 190 int ValueStack::total_locks_size() const { 191 int num_locks = 0; 192 const ValueStack* state = this; 193 for_each_state(state) { 194 num_locks += state->locks_size(); 195 } 196 return num_locks; 197 } 198 199 int ValueStack::lock(Value obj) { 200 if (_locks == nullptr) { 201 _locks = new Values(); 202 } 203 _locks->push(obj); 204 int num_locks = total_locks_size(); 205 scope()->set_min_number_of_locks(num_locks); 206 return num_locks - 1; 207 } 208 209 210 int ValueStack::unlock() { 211 assert(locks_size() > 0, "sanity"); 212 _locks->pop(); 213 return total_locks_size(); 214 } 215 216 void ValueStack::setup_phi_for_stack(BlockBegin* b, int index) { 217 assert(stack_at(index)->as_Phi() == nullptr || stack_at(index)->as_Phi()->block() != b, "phi function already created"); 218 219 ValueType* t = stack_at(index)->type(); 220 Value phi = new Phi(t, b, -index - 1); 221 _stack.at_put(index, phi); 222 223 assert(!t->is_double_word() || _stack.at(index + 1) == nullptr, "hi-word of doubleword value must be null"); 224 } 225 226 void ValueStack::setup_phi_for_local(BlockBegin* b, int index) { 227 assert(local_at(index)->as_Phi() == nullptr || local_at(index)->as_Phi()->block() != b, "phi function already created"); 228 229 ValueType* t = local_at(index)->type(); 230 Value phi = new Phi(t, b, index); 231 store_local(index, phi); 232 } 233 234 #ifndef PRODUCT 235 236 void ValueStack::print() { 237 scope()->method()->print_name(); 238 tty->cr(); 239 if (stack_is_empty()) { 240 tty->print_cr("empty stack"); 241 } else { 242 InstructionPrinter ip; 243 for (int i = 0; i < stack_size();) { 244 tty->print("stack %d ", i); 245 Value t = stack_at_inc(i); 246 if (t == nullptr) { 247 tty->print("null"); 248 } else { 249 tty->print("%2d ", i); 250 tty->print("%c%d ", t->type()->tchar(), t->id()); 251 ip.print_instr(t); 252 } 253 tty->cr(); 254 } 255 } 256 if (!no_active_locks()) { 257 InstructionPrinter ip; 258 for (int i = 0; i < locks_size(); i++) { 259 Value t = lock_at(i); 260 tty->print("lock %2d ", i); 261 if (t == nullptr) { 262 tty->print("this"); 263 } else { 264 tty->print("%c%d ", t->type()->tchar(), t->id()); 265 ip.print_instr(t); 266 } 267 tty->cr(); 268 } 269 } 270 if (locals_size() > 0) { 271 InstructionPrinter ip; 272 for (int i = 0; i < locals_size();) { 273 Value l = _locals.at(i); 274 tty->print("local %d ", i); 275 if (l == nullptr) { 276 tty->print("null"); 277 i ++; 278 } else { 279 tty->print("%c%d ", l->type()->tchar(), l->id()); 280 ip.print_instr(l); 281 if (l->type()->is_illegal() || l->type()->is_single_word()) i ++; else i += 2; 282 } 283 tty->cr(); 284 } 285 } 286 287 if (caller_state() != nullptr) { 288 caller_state()->print(); 289 } 290 } 291 292 293 void ValueStack::verify() { 294 assert(scope() != nullptr, "scope must exist"); 295 if (caller_state() != nullptr) { 296 assert(caller_state()->scope() == scope()->caller(), "invalid caller scope"); 297 caller_state()->verify(); 298 } 299 300 if (kind() == Parsing) { 301 assert(bci() == -99, "bci not defined during parsing"); 302 } else { 303 assert(bci() >= -1, "bci out of range"); 304 assert(bci() < scope()->method()->code_size(), "bci out of range"); 305 assert(bci() == SynchronizationEntryBCI || Bytecodes::is_defined(scope()->method()->java_code_at_bci(bci())), "make sure bci points at a real bytecode"); 306 assert(scope()->method()->liveness_at_bci(bci()).is_valid(), "liveness at bci must be valid"); 307 } 308 309 int i; 310 for (i = 0; i < stack_size(); i++) { 311 Value v = _stack.at(i); 312 if (kind() == empty_exception_kind(true /* caller */)) { 313 assert(v == nullptr, "should be empty"); 314 } else if (v == nullptr) { 315 assert(_stack.at(i - 1)->type()->is_double_word(), "only hi-words are null on stack"); 316 } else if (v->type()->is_double_word()) { 317 assert(_stack.at(i + 1) == nullptr, "hi-word must be null"); 318 } 319 } 320 321 for (i = 0; i < locals_size(); i++) { 322 Value v = _locals.at(i); 323 if (kind() == EmptyExceptionState) { 324 assert(v == nullptr, "should be empty"); 325 } else if (v != nullptr && v->type()->is_double_word()) { 326 assert(_locals.at(i + 1) == nullptr, "hi-word must be null"); 327 } 328 } 329 330 for_each_state_value(this, v, 331 assert(v != nullptr, "just test if state-iteration succeeds"); 332 ); 333 } 334 #endif // PRODUCT