1 /*
2 * Copyright (c) 1998, 2025, 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 "ci/ciInlineKlass.hpp"
26 #include "ci/ciMethodData.hpp"
27 #include "ci/ciSymbols.hpp"
28 #include "classfile/vmSymbols.hpp"
29 #include "compiler/compileLog.hpp"
30 #include "interpreter/linkResolver.hpp"
31 #include "jvm_io.h"
32 #include "memory/resourceArea.hpp"
33 #include "memory/universe.hpp"
34 #include "oops/oop.inline.hpp"
35 #include "opto/addnode.hpp"
36 #include "opto/castnode.hpp"
37 #include "opto/convertnode.hpp"
38 #include "opto/divnode.hpp"
39 #include "opto/idealGraphPrinter.hpp"
40 #include "opto/idealKit.hpp"
41 #include "opto/inlinetypenode.hpp"
42 #include "opto/matcher.hpp"
43 #include "opto/memnode.hpp"
44 #include "opto/mulnode.hpp"
45 #include "opto/opaquenode.hpp"
46 #include "opto/parse.hpp"
47 #include "opto/runtime.hpp"
48 #include "runtime/deoptimization.hpp"
49 #include "runtime/sharedRuntime.hpp"
50
51 #ifndef PRODUCT
52 extern uint explicit_null_checks_inserted,
53 explicit_null_checks_elided;
54 #endif
55
56 Node* Parse::record_profile_for_speculation_at_array_load(Node* ld) {
57 // Feed unused profile data to type speculation
58 if (UseTypeSpeculation && UseArrayLoadStoreProfile) {
59 ciKlass* array_type = nullptr;
60 ciKlass* element_type = nullptr;
61 ProfilePtrKind element_ptr = ProfileMaybeNull;
62 bool flat_array = true;
63 bool null_free_array = true;
64 method()->array_access_profiled_type(bci(), array_type, element_type, element_ptr, flat_array, null_free_array);
65 if (element_type != nullptr || element_ptr != ProfileMaybeNull) {
66 ld = record_profile_for_speculation(ld, element_type, element_ptr);
67 }
68 }
69 return ld;
70 }
71
72
73 //---------------------------------array_load----------------------------------
74 void Parse::array_load(BasicType bt) {
75 const Type* elemtype = Type::TOP;
76 Node* adr = array_addressing(bt, 0, elemtype);
77 if (stopped()) return; // guaranteed null or range check
78
79 Node* array_index = pop();
80 Node* array = pop();
81
82 // Handle inline type arrays
83 const TypeOopPtr* element_ptr = elemtype->make_oopptr();
84 const TypeAryPtr* array_type = _gvn.type(array)->is_aryptr();
85
86 if (!array_type->is_not_flat()) {
87 // Cannot statically determine if array is a flat array, emit runtime check
88 assert(UseArrayFlattening && is_reference_type(bt) && element_ptr->can_be_inline_type() &&
89 (!element_ptr->is_inlinetypeptr() || element_ptr->inline_klass()->maybe_flat_in_array()), "array can't be flat");
90 IdealKit ideal(this);
91 IdealVariable res(ideal);
92 ideal.declarations_done();
93 ideal.if_then(flat_array_test(array, /* flat = */ false)); {
94 // Non-flat array
95 sync_kit(ideal);
96 if (!array_type->is_flat()) {
97 assert(array_type->is_flat() || control()->in(0)->as_If()->is_flat_array_check(&_gvn), "Should be found");
98 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
99 DecoratorSet decorator_set = IN_HEAP | IS_ARRAY | C2_CONTROL_DEPENDENT_LOAD;
100 if (needs_range_check(array_type->size(), array_index)) {
101 // We've emitted a RangeCheck but now insert an additional check between the range check and the actual load.
102 // We cannot pin the load to two separate nodes. Instead, we pin it conservatively here such that it cannot
103 // possibly float above the range check at any point.
104 decorator_set |= C2_UNKNOWN_CONTROL_LOAD;
105 }
106 Node* ld = access_load_at(array, adr, adr_type, element_ptr, bt, decorator_set);
107 if (element_ptr->is_inlinetypeptr()) {
108 ld = InlineTypeNode::make_from_oop(this, ld, element_ptr->inline_klass());
109 }
110 ideal.set(res, ld);
111 }
112 ideal.sync_kit(this);
113 } ideal.else_(); {
114 // Flat array
115 sync_kit(ideal);
116 if (!array_type->is_not_flat()) {
117 if (element_ptr->is_inlinetypeptr()) {
118 ciInlineKlass* vk = element_ptr->inline_klass();
119 Node* flat_array = cast_to_flat_array(array, vk, false, false, false);
120 Node* vt = InlineTypeNode::make_from_flat_array(this, vk, flat_array, array_index);
121 ideal.set(res, vt);
122 } else {
123 // Element type is unknown, and thus we cannot statically determine the exact flat array layout. Emit a
124 // runtime call to correctly load the inline type element from the flat array.
125 Node* inline_type = load_from_unknown_flat_array(array, array_index, element_ptr);
126 bool is_null_free = array_type->is_null_free() || !UseNullableValueFlattening;
127 if (is_null_free) {
128 inline_type = cast_not_null(inline_type);
129 }
130 ideal.set(res, inline_type);
131 }
132 }
133 ideal.sync_kit(this);
134 } ideal.end_if();
135 sync_kit(ideal);
136 Node* ld = _gvn.transform(ideal.value(res));
137 ld = record_profile_for_speculation_at_array_load(ld);
138 push_node(bt, ld);
139 return;
140 }
141
142 if (elemtype == TypeInt::BOOL) {
143 bt = T_BOOLEAN;
144 }
145 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
146 Node* ld = access_load_at(array, adr, adr_type, elemtype, bt,
147 IN_HEAP | IS_ARRAY | C2_CONTROL_DEPENDENT_LOAD);
148 ld = record_profile_for_speculation_at_array_load(ld);
149 // Loading an inline type from a non-flat array
150 if (element_ptr != nullptr && element_ptr->is_inlinetypeptr()) {
151 assert(!array_type->is_null_free() || !element_ptr->maybe_null(), "inline type array elements should never be null");
152 ld = InlineTypeNode::make_from_oop(this, ld, element_ptr->inline_klass());
153 }
154 push_node(bt, ld);
155 }
156
157 Node* Parse::load_from_unknown_flat_array(Node* array, Node* array_index, const TypeOopPtr* element_ptr) {
158 // Below membars keep this access to an unknown flat array correctly
159 // ordered with other unknown and known flat array accesses.
160 insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
161
162 Node* call = nullptr;
163 {
164 // Re-execute flat array load if runtime call triggers deoptimization
165 PreserveReexecuteState preexecs(this);
166 jvms()->set_bci(_bci);
167 jvms()->set_should_reexecute(true);
168 inc_sp(2);
169 kill_dead_locals();
170 call = make_runtime_call(RC_NO_LEAF | RC_NO_IO,
171 OptoRuntime::load_unknown_inline_Type(),
172 OptoRuntime::load_unknown_inline_Java(),
173 nullptr, TypeRawPtr::BOTTOM,
174 array, array_index);
175 }
176 make_slow_call_ex(call, env()->Throwable_klass(), false);
177 Node* buffer = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
178
179 insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
180
181 // Keep track of the information that the inline type is in flat arrays
182 const Type* unknown_value = element_ptr->is_instptr()->cast_to_flat_in_array();
183 return _gvn.transform(new CheckCastPPNode(control(), buffer, unknown_value));
184 }
185
186 //--------------------------------array_store----------------------------------
187 void Parse::array_store(BasicType bt) {
188 const Type* elemtype = Type::TOP;
189 Node* adr = array_addressing(bt, type2size[bt], elemtype);
190 if (stopped()) return; // guaranteed null or range check
191 Node* stored_value_casted = nullptr;
192 if (bt == T_OBJECT) {
193 stored_value_casted = array_store_check(adr, elemtype);
194 if (stopped()) {
195 return;
196 }
197 }
198 Node* const stored_value = pop_node(bt); // Value to store
199 Node* const array_index = pop(); // Index in the array
200 Node* array = pop(); // The array itself
201
202 const TypeAryPtr* array_type = _gvn.type(array)->is_aryptr();
203 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
204
205 if (elemtype == TypeInt::BOOL) {
206 bt = T_BOOLEAN;
207 } else if (bt == T_OBJECT) {
208 elemtype = elemtype->make_oopptr();
209 const Type* stored_value_casted_type = _gvn.type(stored_value_casted);
210 // Based on the value to be stored, try to determine if the array is not null-free and/or not flat.
211 // This is only legal for non-null stores because the array_store_check always passes for null, even
212 // if the array is null-free. Null stores are handled in GraphKit::inline_array_null_guard().
213 bool not_inline = !stored_value_casted_type->maybe_null() && !stored_value_casted_type->is_oopptr()->can_be_inline_type();
214 bool not_null_free = not_inline;
215 bool not_flat = not_inline || ( stored_value_casted_type->is_inlinetypeptr() &&
216 !stored_value_casted_type->inline_klass()->maybe_flat_in_array());
217 if (!array_type->is_not_null_free() && not_null_free) {
218 // Storing a non-inline type, mark array as not null-free.
219 array_type = array_type->cast_to_not_null_free();
220 Node* cast = _gvn.transform(new CheckCastPPNode(control(), array, array_type));
221 replace_in_map(array, cast);
222 array = cast;
223 }
224 if (!array_type->is_not_flat() && not_flat) {
225 // Storing to a non-flat array, mark array as not flat.
226 array_type = array_type->cast_to_not_flat();
227 Node* cast = _gvn.transform(new CheckCastPPNode(control(), array, array_type));
228 replace_in_map(array, cast);
229 array = cast;
230 }
231
232 if (!array_type->is_flat() && array_type->is_null_free()) {
233 // Store to non-flat null-free inline type array (elements can never be null)
234 assert(!stored_value_casted_type->maybe_null(), "should be guaranteed by array store check");
235 if (elemtype->is_inlinetypeptr() && elemtype->inline_klass()->is_empty()) {
236 // Ignore empty inline stores, array is already initialized.
237 return;
238 }
239 } else if (!array_type->is_not_flat()) {
240 // Array might be a flat array, emit runtime checks (for nullptr, a simple inline_array_null_guard is sufficient).
241 assert(UseArrayFlattening && !not_flat && elemtype->is_oopptr()->can_be_inline_type() &&
242 (!array_type->klass_is_exact() || array_type->is_flat()), "array can't be a flat array");
243 // TODO 8350865 Depending on the available layouts, we can avoid this check in below flat/not-flat branches. Also the safe_for_replace arg is now always true.
244 array = inline_array_null_guard(array, stored_value_casted, 3, true);
245 IdealKit ideal(this);
246 ideal.if_then(flat_array_test(array, /* flat = */ false)); {
247 // Non-flat array
248 if (!array_type->is_flat()) {
249 sync_kit(ideal);
250 assert(array_type->is_flat() || ideal.ctrl()->in(0)->as_If()->is_flat_array_check(&_gvn), "Should be found");
251 inc_sp(3);
252 access_store_at(array, adr, adr_type, stored_value_casted, elemtype, bt, MO_UNORDERED | IN_HEAP | IS_ARRAY, false);
253 dec_sp(3);
254 ideal.sync_kit(this);
255 }
256 } ideal.else_(); {
257 // Flat array
258 sync_kit(ideal);
259 if (!array_type->is_not_flat()) {
260 // Try to determine the inline klass type of the stored value
261 ciInlineKlass* vk = nullptr;
262 if (stored_value_casted_type->is_inlinetypeptr()) {
263 vk = stored_value_casted_type->inline_klass();
264 } else if (elemtype->is_inlinetypeptr()) {
265 vk = elemtype->inline_klass();
266 }
267
268 if (vk != nullptr) {
269 // Element type is known, cast and store to flat array layout.
270 Node* flat_array = cast_to_flat_array(array, vk, false, false, false);
271
272 // Re-execute flat array store if buffering triggers deoptimization
273 PreserveReexecuteState preexecs(this);
274 jvms()->set_should_reexecute(true);
275 inc_sp(3);
276
277 if (!stored_value_casted->is_InlineType()) {
278 assert(_gvn.type(stored_value_casted) == TypePtr::NULL_PTR, "Unexpected value");
279 stored_value_casted = InlineTypeNode::make_null(_gvn, vk);
280 }
281
282 stored_value_casted->as_InlineType()->store_flat_array(this, flat_array, array_index);
283 } else {
284 // Element type is unknown, emit a runtime call since the flat array layout is not statically known.
285 store_to_unknown_flat_array(array, array_index, stored_value_casted);
286 }
287 }
288 ideal.sync_kit(this);
289 }
290 ideal.end_if();
291 sync_kit(ideal);
292 return;
293 } else if (!array_type->is_not_null_free()) {
294 // Array is not flat but may be null free
295 assert(elemtype->is_oopptr()->can_be_inline_type(), "array can't be null-free");
296 array = inline_array_null_guard(array, stored_value_casted, 3, true);
297 }
298 }
299 inc_sp(3);
300 access_store_at(array, adr, adr_type, stored_value, elemtype, bt, MO_UNORDERED | IN_HEAP | IS_ARRAY);
301 dec_sp(3);
302 }
303
304 // Emit a runtime call to store to a flat array whose element type is either unknown (i.e. we do not know the flat
305 // array layout) or not exact (could have different flat array layouts at runtime).
306 void Parse::store_to_unknown_flat_array(Node* array, Node* const idx, Node* non_null_stored_value) {
307 // Below membars keep this access to an unknown flat array correctly
308 // ordered with other unknown and known flat array accesses.
309 insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
310
311 Node* call = nullptr;
312 {
313 // Re-execute flat array store if runtime call triggers deoptimization
314 PreserveReexecuteState preexecs(this);
315 jvms()->set_bci(_bci);
316 jvms()->set_should_reexecute(true);
317 inc_sp(3);
318 kill_dead_locals();
319 call = make_runtime_call(RC_NO_LEAF | RC_NO_IO,
320 OptoRuntime::store_unknown_inline_Type(),
321 OptoRuntime::store_unknown_inline_Java(),
322 nullptr, TypeRawPtr::BOTTOM,
323 non_null_stored_value, array, idx);
324 }
325 make_slow_call_ex(call, env()->Throwable_klass(), false);
326
327 insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
328 }
329
330 //------------------------------array_addressing-------------------------------
331 // Pull array and index from the stack. Compute pointer-to-element.
332 Node* Parse::array_addressing(BasicType type, int vals, const Type*& elemtype) {
333 Node *idx = peek(0+vals); // Get from stack without popping
334 Node *ary = peek(1+vals); // in case of exception
335
336 // Null check the array base, with correct stack contents
337 ary = null_check(ary, T_ARRAY);
338 // Compile-time detect of null-exception?
339 if (stopped()) return top();
340
341 const TypeAryPtr* arytype = _gvn.type(ary)->is_aryptr();
342 const TypeInt* sizetype = arytype->size();
343 elemtype = arytype->elem();
344
345 if (UseUniqueSubclasses) {
346 const Type* el = elemtype->make_ptr();
347 if (el && el->isa_instptr()) {
348 const TypeInstPtr* toop = el->is_instptr();
349 if (toop->instance_klass()->unique_concrete_subklass()) {
350 // If we load from "AbstractClass[]" we must see "ConcreteSubClass".
351 const Type* subklass = Type::get_const_type(toop->instance_klass());
352 elemtype = subklass->join_speculative(el);
353 }
354 }
355 }
356
357 if (!arytype->is_loaded()) {
358 // Only fails for some -Xcomp runs
359 // The class is unloaded. We have to run this bytecode in the interpreter.
360 ciKlass* klass = arytype->unloaded_klass();
361
362 uncommon_trap(Deoptimization::Reason_unloaded,
363 Deoptimization::Action_reinterpret,
364 klass, "!loaded array");
365 return top();
366 }
367
368 ary = create_speculative_inline_type_array_checks(ary, arytype, elemtype);
369
370 if (needs_range_check(sizetype, idx)) {
371 create_range_check(idx, ary, sizetype);
372 } else if (C->log() != nullptr) {
373 C->log()->elem("observe that='!need_range_check'");
374 }
375
376 // Check for always knowing you are throwing a range-check exception
377 if (stopped()) return top();
378
379 // Make array address computation control dependent to prevent it
380 // from floating above the range check during loop optimizations.
381 Node* ptr = array_element_address(ary, idx, type, sizetype, control());
382 assert(ptr != top(), "top should go hand-in-hand with stopped");
383
384 return ptr;
385 }
386
387 // Check if we need a range check for an array access. This is the case if the index is either negative or if it could
388 // be greater or equal the smallest possible array size (i.e. out-of-bounds).
389 bool Parse::needs_range_check(const TypeInt* size_type, const Node* index) const {
390 const TypeInt* index_type = _gvn.type(index)->is_int();
391 return index_type->_hi >= size_type->_lo || index_type->_lo < 0;
392 }
393
394 void Parse::create_range_check(Node* idx, Node* ary, const TypeInt* sizetype) {
395 Node* tst;
396 if (sizetype->_hi <= 0) {
397 // The greatest array bound is negative, so we can conclude that we're
398 // compiling unreachable code, but the unsigned compare trick used below
399 // only works with non-negative lengths. Instead, hack "tst" to be zero so
400 // the uncommon_trap path will always be taken.
401 tst = _gvn.intcon(0);
402 } else {
403 // Range is constant in array-oop, so we can use the original state of mem
404 Node* len = load_array_length(ary);
405
406 // Test length vs index (standard trick using unsigned compare)
407 Node* chk = _gvn.transform(new CmpUNode(idx, len) );
408 BoolTest::mask btest = BoolTest::lt;
409 tst = _gvn.transform(new BoolNode(chk, btest) );
410 }
411 RangeCheckNode* rc = new RangeCheckNode(control(), tst, PROB_MAX, COUNT_UNKNOWN);
412 _gvn.set_type(rc, rc->Value(&_gvn));
413 if (!tst->is_Con()) {
414 record_for_igvn(rc);
415 }
416 set_control(_gvn.transform(new IfTrueNode(rc)));
417 // Branch to failure if out of bounds
418 {
419 PreserveJVMState pjvms(this);
420 set_control(_gvn.transform(new IfFalseNode(rc)));
421 if (C->allow_range_check_smearing()) {
422 // Do not use builtin_throw, since range checks are sometimes
423 // made more stringent by an optimistic transformation.
424 // This creates "tentative" range checks at this point,
425 // which are not guaranteed to throw exceptions.
426 // See IfNode::Ideal, is_range_check, adjust_check.
427 uncommon_trap(Deoptimization::Reason_range_check,
428 Deoptimization::Action_make_not_entrant,
429 nullptr, "range_check");
430 } else {
431 // If we have already recompiled with the range-check-widening
432 // heroic optimization turned off, then we must really be throwing
433 // range check exceptions.
434 builtin_throw(Deoptimization::Reason_range_check);
435 }
436 }
437 }
438
439 // For inline type arrays, we can use the profiling information for array accesses to speculate on the type, flatness,
440 // and null-freeness. We can either prepare the speculative type for later uses or emit explicit speculative checks with
441 // traps now. In the latter case, the speculative type guarantees can avoid additional runtime checks later (e.g.
442 // non-null-free implies non-flat which allows us to remove flatness checks). This makes the graph simpler.
443 Node* Parse::create_speculative_inline_type_array_checks(Node* array, const TypeAryPtr* array_type,
444 const Type*& element_type) {
445 if (!array_type->is_flat() && !array_type->is_not_flat()) {
446 // For arrays that might be flat, speculate that the array has the exact type reported in the profile data such that
447 // we can rely on a fixed memory layout (i.e. either a flat layout or not).
448 array = cast_to_speculative_array_type(array, array_type, element_type);
449 } else if (UseTypeSpeculation && UseArrayLoadStoreProfile) {
450 // Array is known to be either flat or not flat. If possible, update the speculative type by using the profile data
451 // at this bci.
452 array = cast_to_profiled_array_type(array);
453 }
454
455 // Even though the type does not tell us whether we have an inline type array or not, we can still check the profile data
456 // whether we have a non-null-free or non-flat array. Speculating on a non-null-free array doesn't help aaload but could
457 // be profitable for a subsequent aastore.
458 if (!array_type->is_null_free() && !array_type->is_not_null_free()) {
459 array = speculate_non_null_free_array(array, array_type);
460 }
461 if (!array_type->is_flat() && !array_type->is_not_flat()) {
462 array = speculate_non_flat_array(array, array_type);
463 }
464 return array;
465 }
466
467 // Speculate that the array has the exact type reported in the profile data. We emit a trap when this turns out to be
468 // wrong. On the fast path, we add a CheckCastPP to use the exact type.
469 Node* Parse::cast_to_speculative_array_type(Node* const array, const TypeAryPtr*& array_type, const Type*& element_type) {
470 Deoptimization::DeoptReason reason = Deoptimization::Reason_speculate_class_check;
471 ciKlass* speculative_array_type = array_type->speculative_type();
472 if (too_many_traps_or_recompiles(reason) || speculative_array_type == nullptr) {
473 // No speculative type, check profile data at this bci
474 speculative_array_type = nullptr;
475 reason = Deoptimization::Reason_class_check;
476 if (UseArrayLoadStoreProfile && !too_many_traps_or_recompiles(reason)) {
477 ciKlass* profiled_element_type = nullptr;
478 ProfilePtrKind element_ptr = ProfileMaybeNull;
479 bool flat_array = true;
480 bool null_free_array = true;
481 method()->array_access_profiled_type(bci(), speculative_array_type, profiled_element_type, element_ptr, flat_array,
482 null_free_array);
483 }
484 }
485 if (speculative_array_type != nullptr) {
486 // Speculate that this array has the exact type reported by profile data
487 Node* casted_array = nullptr;
488 DEBUG_ONLY(Node* old_control = control();)
489 Node* slow_ctl = type_check_receiver(array, speculative_array_type, 1.0, &casted_array);
490 if (stopped()) {
491 // The check always fails and therefore profile information is incorrect. Don't use it.
492 assert(old_control == slow_ctl, "type check should have been removed");
493 set_control(slow_ctl);
494 } else if (!slow_ctl->is_top()) {
495 { PreserveJVMState pjvms(this);
496 set_control(slow_ctl);
497 uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
498 }
499 replace_in_map(array, casted_array);
500 array_type = _gvn.type(casted_array)->is_aryptr();
501 element_type = array_type->elem();
502 return casted_array;
503 }
504 }
505 return array;
506 }
507
508 // Create a CheckCastPP when the speculative type can improve the current type.
509 Node* Parse::cast_to_profiled_array_type(Node* const array) {
510 ciKlass* array_type = nullptr;
511 ciKlass* element_type = nullptr;
512 ProfilePtrKind element_ptr = ProfileMaybeNull;
513 bool flat_array = true;
514 bool null_free_array = true;
515 method()->array_access_profiled_type(bci(), array_type, element_type, element_ptr, flat_array, null_free_array);
516 if (array_type != nullptr) {
517 return record_profile_for_speculation(array, array_type, ProfileMaybeNull);
518 }
519 return array;
520 }
521
522 // Speculate that the array is non-null-free. We emit a trap when this turns out to be
523 // wrong. On the fast path, we add a CheckCastPP to use the non-null-free type.
524 Node* Parse::speculate_non_null_free_array(Node* const array, const TypeAryPtr*& array_type) {
525 bool null_free_array = true;
526 Deoptimization::DeoptReason reason = Deoptimization::Reason_none;
527 if (array_type->speculative() != nullptr &&
528 array_type->speculative()->is_aryptr()->is_not_null_free() &&
529 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
530 null_free_array = false;
531 reason = Deoptimization::Reason_speculate_class_check;
532 } else if (UseArrayLoadStoreProfile && !too_many_traps_or_recompiles(Deoptimization::Reason_class_check)) {
533 ciKlass* profiled_array_type = nullptr;
534 ciKlass* profiled_element_type = nullptr;
535 ProfilePtrKind element_ptr = ProfileMaybeNull;
536 bool flat_array = true;
537 method()->array_access_profiled_type(bci(), profiled_array_type, profiled_element_type, element_ptr, flat_array,
538 null_free_array);
539 reason = Deoptimization::Reason_class_check;
540 }
541 if (!null_free_array) {
542 { // Deoptimize if null-free array
543 BuildCutout unless(this, null_free_array_test(array, /* null_free = */ false), PROB_MAX);
544 uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
545 }
546 assert(!stopped(), "null-free array should have been caught earlier");
547 Node* casted_array = _gvn.transform(new CheckCastPPNode(control(), array, array_type->cast_to_not_null_free()));
548 replace_in_map(array, casted_array);
549 array_type = _gvn.type(casted_array)->is_aryptr();
550 return casted_array;
551 }
552 return array;
553 }
554
555 // Speculate that the array is non-flat. We emit a trap when this turns out to be wrong.
556 // On the fast path, we add a CheckCastPP to use the non-flat type.
557 Node* Parse::speculate_non_flat_array(Node* const array, const TypeAryPtr* const array_type) {
558 bool flat_array = true;
559 Deoptimization::DeoptReason reason = Deoptimization::Reason_none;
560 if (array_type->speculative() != nullptr &&
561 array_type->speculative()->is_aryptr()->is_not_flat() &&
562 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
563 flat_array = false;
564 reason = Deoptimization::Reason_speculate_class_check;
565 } else if (UseArrayLoadStoreProfile && !too_many_traps_or_recompiles(reason)) {
566 ciKlass* profiled_array_type = nullptr;
567 ciKlass* profiled_element_type = nullptr;
568 ProfilePtrKind element_ptr = ProfileMaybeNull;
569 bool null_free_array = true;
570 method()->array_access_profiled_type(bci(), profiled_array_type, profiled_element_type, element_ptr, flat_array,
571 null_free_array);
572 reason = Deoptimization::Reason_class_check;
573 }
574 if (!flat_array) {
575 { // Deoptimize if flat array
576 BuildCutout unless(this, flat_array_test(array, /* flat = */ false), PROB_MAX);
577 uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
578 }
579 assert(!stopped(), "flat array should have been caught earlier");
580 Node* casted_array = _gvn.transform(new CheckCastPPNode(control(), array, array_type->cast_to_not_flat()));
581 replace_in_map(array, casted_array);
582 return casted_array;
583 }
584 return array;
585 }
586
587 // returns IfNode
588 IfNode* Parse::jump_if_fork_int(Node* a, Node* b, BoolTest::mask mask, float prob, float cnt) {
589 Node *cmp = _gvn.transform(new CmpINode(a, b)); // two cases: shiftcount > 32 and shiftcount <= 32
590 Node *tst = _gvn.transform(new BoolNode(cmp, mask));
591 IfNode *iff = create_and_map_if(control(), tst, prob, cnt);
592 return iff;
593 }
594
595
596 // sentinel value for the target bci to mark never taken branches
597 // (according to profiling)
598 static const int never_reached = INT_MAX;
599
600 //------------------------------helper for tableswitch-------------------------
601 void Parse::jump_if_true_fork(IfNode *iff, int dest_bci_if_true, bool unc) {
602 // True branch, use existing map info
603 { PreserveJVMState pjvms(this);
604 Node *iftrue = _gvn.transform( new IfTrueNode (iff) );
605 set_control( iftrue );
606 if (unc) {
607 repush_if_args();
608 uncommon_trap(Deoptimization::Reason_unstable_if,
609 Deoptimization::Action_reinterpret,
610 nullptr,
611 "taken always");
612 } else {
613 assert(dest_bci_if_true != never_reached, "inconsistent dest");
614 merge_new_path(dest_bci_if_true);
615 }
616 }
617
618 // False branch
619 Node *iffalse = _gvn.transform( new IfFalseNode(iff) );
620 set_control( iffalse );
621 }
622
623 void Parse::jump_if_false_fork(IfNode *iff, int dest_bci_if_true, bool unc) {
624 // True branch, use existing map info
625 { PreserveJVMState pjvms(this);
626 Node *iffalse = _gvn.transform( new IfFalseNode (iff) );
627 set_control( iffalse );
628 if (unc) {
629 repush_if_args();
630 uncommon_trap(Deoptimization::Reason_unstable_if,
631 Deoptimization::Action_reinterpret,
632 nullptr,
633 "taken never");
634 } else {
635 assert(dest_bci_if_true != never_reached, "inconsistent dest");
636 merge_new_path(dest_bci_if_true);
637 }
638 }
639
640 // False branch
641 Node *iftrue = _gvn.transform( new IfTrueNode(iff) );
642 set_control( iftrue );
643 }
644
645 void Parse::jump_if_always_fork(int dest_bci, bool unc) {
646 // False branch, use existing map and control()
647 if (unc) {
648 repush_if_args();
649 uncommon_trap(Deoptimization::Reason_unstable_if,
650 Deoptimization::Action_reinterpret,
651 nullptr,
652 "taken never");
653 } else {
654 assert(dest_bci != never_reached, "inconsistent dest");
655 merge_new_path(dest_bci);
656 }
657 }
658
659
660 extern "C" {
661 static int jint_cmp(const void *i, const void *j) {
662 int a = *(jint *)i;
663 int b = *(jint *)j;
664 return a > b ? 1 : a < b ? -1 : 0;
665 }
666 }
667
668
669 class SwitchRange : public StackObj {
670 // a range of integers coupled with a bci destination
671 jint _lo; // inclusive lower limit
672 jint _hi; // inclusive upper limit
673 int _dest;
674 float _cnt; // how many times this range was hit according to profiling
675
676 public:
677 jint lo() const { return _lo; }
678 jint hi() const { return _hi; }
679 int dest() const { return _dest; }
680 bool is_singleton() const { return _lo == _hi; }
681 float cnt() const { return _cnt; }
682
683 void setRange(jint lo, jint hi, int dest, float cnt) {
684 assert(lo <= hi, "must be a non-empty range");
685 _lo = lo, _hi = hi; _dest = dest; _cnt = cnt;
686 assert(_cnt >= 0, "");
687 }
688 bool adjoinRange(jint lo, jint hi, int dest, float cnt, bool trim_ranges) {
689 assert(lo <= hi, "must be a non-empty range");
690 if (lo == _hi+1) {
691 // see merge_ranges() comment below
692 if (trim_ranges) {
693 if (cnt == 0) {
694 if (_cnt != 0) {
695 return false;
696 }
697 if (dest != _dest) {
698 _dest = never_reached;
699 }
700 } else {
701 if (_cnt == 0) {
702 return false;
703 }
704 if (dest != _dest) {
705 return false;
706 }
707 }
708 } else {
709 if (dest != _dest) {
710 return false;
711 }
712 }
713 _hi = hi;
714 _cnt += cnt;
715 return true;
716 }
717 return false;
718 }
719
720 void set (jint value, int dest, float cnt) {
721 setRange(value, value, dest, cnt);
722 }
723 bool adjoin(jint value, int dest, float cnt, bool trim_ranges) {
724 return adjoinRange(value, value, dest, cnt, trim_ranges);
725 }
726 bool adjoin(SwitchRange& other) {
727 return adjoinRange(other._lo, other._hi, other._dest, other._cnt, false);
728 }
729
730 void print() {
731 if (is_singleton())
732 tty->print(" {%d}=>%d (cnt=%f)", lo(), dest(), cnt());
733 else if (lo() == min_jint)
734 tty->print(" {..%d}=>%d (cnt=%f)", hi(), dest(), cnt());
735 else if (hi() == max_jint)
736 tty->print(" {%d..}=>%d (cnt=%f)", lo(), dest(), cnt());
737 else
738 tty->print(" {%d..%d}=>%d (cnt=%f)", lo(), hi(), dest(), cnt());
739 }
740 };
741
742 // We try to minimize the number of ranges and the size of the taken
743 // ones using profiling data. When ranges are created,
744 // SwitchRange::adjoinRange() only allows 2 adjoining ranges to merge
745 // if both were never hit or both were hit to build longer unreached
746 // ranges. Here, we now merge adjoining ranges with the same
747 // destination and finally set destination of unreached ranges to the
748 // special value never_reached because it can help minimize the number
749 // of tests that are necessary.
750 //
751 // For instance:
752 // [0, 1] to target1 sometimes taken
753 // [1, 2] to target1 never taken
754 // [2, 3] to target2 never taken
755 // would lead to:
756 // [0, 1] to target1 sometimes taken
757 // [1, 3] never taken
758 //
759 // (first 2 ranges to target1 are not merged)
760 static void merge_ranges(SwitchRange* ranges, int& rp) {
761 if (rp == 0) {
762 return;
763 }
764 int shift = 0;
765 for (int j = 0; j < rp; j++) {
766 SwitchRange& r1 = ranges[j-shift];
767 SwitchRange& r2 = ranges[j+1];
768 if (r1.adjoin(r2)) {
769 shift++;
770 } else if (shift > 0) {
771 ranges[j+1-shift] = r2;
772 }
773 }
774 rp -= shift;
775 for (int j = 0; j <= rp; j++) {
776 SwitchRange& r = ranges[j];
777 if (r.cnt() == 0 && r.dest() != never_reached) {
778 r.setRange(r.lo(), r.hi(), never_reached, r.cnt());
779 }
780 }
781 }
782
783 //-------------------------------do_tableswitch--------------------------------
784 void Parse::do_tableswitch() {
785 // Get information about tableswitch
786 int default_dest = iter().get_dest_table(0);
787 jint lo_index = iter().get_int_table(1);
788 jint hi_index = iter().get_int_table(2);
789 int len = hi_index - lo_index + 1;
790
791 if (len < 1) {
792 // If this is a backward branch, add safepoint
793 maybe_add_safepoint(default_dest);
794 pop(); // the effect of the instruction execution on the operand stack
795 merge(default_dest);
796 return;
797 }
798
799 ciMethodData* methodData = method()->method_data();
800 ciMultiBranchData* profile = nullptr;
801 if (methodData->is_mature() && UseSwitchProfiling) {
802 ciProfileData* data = methodData->bci_to_data(bci());
803 if (data != nullptr && data->is_MultiBranchData()) {
804 profile = (ciMultiBranchData*)data;
805 }
806 }
807 bool trim_ranges = !C->too_many_traps(method(), bci(), Deoptimization::Reason_unstable_if);
808
809 // generate decision tree, using trichotomy when possible
810 int rnum = len+2;
811 bool makes_backward_branch = (default_dest <= bci());
812 SwitchRange* ranges = NEW_RESOURCE_ARRAY(SwitchRange, rnum);
813 int rp = -1;
814 if (lo_index != min_jint) {
815 float cnt = 1.0F;
816 if (profile != nullptr) {
817 cnt = (float)profile->default_count() / (hi_index != max_jint ? 2.0F : 1.0F);
818 }
819 ranges[++rp].setRange(min_jint, lo_index-1, default_dest, cnt);
820 }
821 for (int j = 0; j < len; j++) {
822 jint match_int = lo_index+j;
823 int dest = iter().get_dest_table(j+3);
824 makes_backward_branch |= (dest <= bci());
825 float cnt = 1.0F;
826 if (profile != nullptr) {
827 cnt = (float)profile->count_at(j);
828 }
829 if (rp < 0 || !ranges[rp].adjoin(match_int, dest, cnt, trim_ranges)) {
830 ranges[++rp].set(match_int, dest, cnt);
831 }
832 }
833 jint highest = lo_index+(len-1);
834 assert(ranges[rp].hi() == highest, "");
835 if (highest != max_jint) {
836 float cnt = 1.0F;
837 if (profile != nullptr) {
838 cnt = (float)profile->default_count() / (lo_index != min_jint ? 2.0F : 1.0F);
839 }
840 if (!ranges[rp].adjoinRange(highest+1, max_jint, default_dest, cnt, trim_ranges)) {
841 ranges[++rp].setRange(highest+1, max_jint, default_dest, cnt);
842 }
843 }
844 assert(rp < len+2, "not too many ranges");
845
846 if (trim_ranges) {
847 merge_ranges(ranges, rp);
848 }
849
850 // Safepoint in case if backward branch observed
851 if (makes_backward_branch) {
852 add_safepoint();
853 }
854
855 Node* lookup = pop(); // lookup value
856 jump_switch_ranges(lookup, &ranges[0], &ranges[rp]);
857 }
858
859
860 //------------------------------do_lookupswitch--------------------------------
861 void Parse::do_lookupswitch() {
862 // Get information about lookupswitch
863 int default_dest = iter().get_dest_table(0);
864 jint len = iter().get_int_table(1);
865
866 if (len < 1) { // If this is a backward branch, add safepoint
867 maybe_add_safepoint(default_dest);
868 pop(); // the effect of the instruction execution on the operand stack
869 merge(default_dest);
870 return;
871 }
872
873 ciMethodData* methodData = method()->method_data();
874 ciMultiBranchData* profile = nullptr;
875 if (methodData->is_mature() && UseSwitchProfiling) {
876 ciProfileData* data = methodData->bci_to_data(bci());
877 if (data != nullptr && data->is_MultiBranchData()) {
878 profile = (ciMultiBranchData*)data;
879 }
880 }
881 bool trim_ranges = !C->too_many_traps(method(), bci(), Deoptimization::Reason_unstable_if);
882
883 // generate decision tree, using trichotomy when possible
884 jint* table = NEW_RESOURCE_ARRAY(jint, len*3);
885 {
886 for (int j = 0; j < len; j++) {
887 table[3*j+0] = iter().get_int_table(2+2*j);
888 table[3*j+1] = iter().get_dest_table(2+2*j+1);
889 // Handle overflow when converting from uint to jint
890 table[3*j+2] = (profile == nullptr) ? 1 : (jint)MIN2<uint>((uint)max_jint, profile->count_at(j));
891 }
892 qsort(table, len, 3*sizeof(table[0]), jint_cmp);
893 }
894
895 float default_cnt = 1.0F;
896 if (profile != nullptr) {
897 juint defaults = max_juint - len;
898 default_cnt = (float)profile->default_count()/(float)defaults;
899 }
900
901 int rnum = len*2+1;
902 bool makes_backward_branch = (default_dest <= bci());
903 SwitchRange* ranges = NEW_RESOURCE_ARRAY(SwitchRange, rnum);
904 int rp = -1;
905 for (int j = 0; j < len; j++) {
906 jint match_int = table[3*j+0];
907 jint dest = table[3*j+1];
908 jint cnt = table[3*j+2];
909 jint next_lo = rp < 0 ? min_jint : ranges[rp].hi()+1;
910 makes_backward_branch |= (dest <= bci());
911 float c = default_cnt * ((float)match_int - (float)next_lo);
912 if (match_int != next_lo && (rp < 0 || !ranges[rp].adjoinRange(next_lo, match_int-1, default_dest, c, trim_ranges))) {
913 assert(default_dest != never_reached, "sentinel value for dead destinations");
914 ranges[++rp].setRange(next_lo, match_int-1, default_dest, c);
915 }
916 if (rp < 0 || !ranges[rp].adjoin(match_int, dest, (float)cnt, trim_ranges)) {
917 assert(dest != never_reached, "sentinel value for dead destinations");
918 ranges[++rp].set(match_int, dest, (float)cnt);
919 }
920 }
921 jint highest = table[3*(len-1)];
922 assert(ranges[rp].hi() == highest, "");
923 if (highest != max_jint &&
924 !ranges[rp].adjoinRange(highest+1, max_jint, default_dest, default_cnt * ((float)max_jint - (float)highest), trim_ranges)) {
925 ranges[++rp].setRange(highest+1, max_jint, default_dest, default_cnt * ((float)max_jint - (float)highest));
926 }
927 assert(rp < rnum, "not too many ranges");
928
929 if (trim_ranges) {
930 merge_ranges(ranges, rp);
931 }
932
933 // Safepoint in case backward branch observed
934 if (makes_backward_branch) {
935 add_safepoint();
936 }
937
938 Node *lookup = pop(); // lookup value
939 jump_switch_ranges(lookup, &ranges[0], &ranges[rp]);
940 }
941
942 static float if_prob(float taken_cnt, float total_cnt) {
943 assert(taken_cnt <= total_cnt, "");
944 if (total_cnt == 0) {
945 return PROB_FAIR;
946 }
947 float p = taken_cnt / total_cnt;
948 return clamp(p, PROB_MIN, PROB_MAX);
949 }
950
951 static float if_cnt(float cnt) {
952 if (cnt == 0) {
953 return COUNT_UNKNOWN;
954 }
955 return cnt;
956 }
957
958 static float sum_of_cnts(SwitchRange *lo, SwitchRange *hi) {
959 float total_cnt = 0;
960 for (SwitchRange* sr = lo; sr <= hi; sr++) {
961 total_cnt += sr->cnt();
962 }
963 return total_cnt;
964 }
965
966 class SwitchRanges : public ResourceObj {
967 public:
968 SwitchRange* _lo;
969 SwitchRange* _hi;
970 SwitchRange* _mid;
971 float _cost;
972
973 enum {
974 Start,
975 LeftDone,
976 RightDone,
977 Done
978 } _state;
979
980 SwitchRanges(SwitchRange *lo, SwitchRange *hi)
981 : _lo(lo), _hi(hi), _mid(nullptr),
982 _cost(0), _state(Start) {
983 }
984
985 SwitchRanges()
986 : _lo(nullptr), _hi(nullptr), _mid(nullptr),
987 _cost(0), _state(Start) {}
988 };
989
990 // Estimate cost of performing a binary search on lo..hi
991 static float compute_tree_cost(SwitchRange *lo, SwitchRange *hi, float total_cnt) {
992 GrowableArray<SwitchRanges> tree;
993 SwitchRanges root(lo, hi);
994 tree.push(root);
995
996 float cost = 0;
997 do {
998 SwitchRanges& r = *tree.adr_at(tree.length()-1);
999 if (r._hi != r._lo) {
1000 if (r._mid == nullptr) {
1001 float r_cnt = sum_of_cnts(r._lo, r._hi);
1002
1003 if (r_cnt == 0) {
1004 tree.pop();
1005 cost = 0;
1006 continue;
1007 }
1008
1009 SwitchRange* mid = nullptr;
1010 mid = r._lo;
1011 for (float cnt = 0; ; ) {
1012 assert(mid <= r._hi, "out of bounds");
1013 cnt += mid->cnt();
1014 if (cnt > r_cnt / 2) {
1015 break;
1016 }
1017 mid++;
1018 }
1019 assert(mid <= r._hi, "out of bounds");
1020 r._mid = mid;
1021 r._cost = r_cnt / total_cnt;
1022 }
1023 r._cost += cost;
1024 if (r._state < SwitchRanges::LeftDone && r._mid > r._lo) {
1025 cost = 0;
1026 r._state = SwitchRanges::LeftDone;
1027 tree.push(SwitchRanges(r._lo, r._mid-1));
1028 } else if (r._state < SwitchRanges::RightDone) {
1029 cost = 0;
1030 r._state = SwitchRanges::RightDone;
1031 tree.push(SwitchRanges(r._mid == r._lo ? r._mid+1 : r._mid, r._hi));
1032 } else {
1033 tree.pop();
1034 cost = r._cost;
1035 }
1036 } else {
1037 tree.pop();
1038 cost = r._cost;
1039 }
1040 } while (tree.length() > 0);
1041
1042
1043 return cost;
1044 }
1045
1046 // It sometimes pays off to test most common ranges before the binary search
1047 void Parse::linear_search_switch_ranges(Node* key_val, SwitchRange*& lo, SwitchRange*& hi) {
1048 uint nr = hi - lo + 1;
1049 float total_cnt = sum_of_cnts(lo, hi);
1050
1051 float min = compute_tree_cost(lo, hi, total_cnt);
1052 float extra = 1;
1053 float sub = 0;
1054
1055 SwitchRange* array1 = lo;
1056 SwitchRange* array2 = NEW_RESOURCE_ARRAY(SwitchRange, nr);
1057
1058 SwitchRange* ranges = nullptr;
1059
1060 while (nr >= 2) {
1061 assert(lo == array1 || lo == array2, "one the 2 already allocated arrays");
1062 ranges = (lo == array1) ? array2 : array1;
1063
1064 // Find highest frequency range
1065 SwitchRange* candidate = lo;
1066 for (SwitchRange* sr = lo+1; sr <= hi; sr++) {
1067 if (sr->cnt() > candidate->cnt()) {
1068 candidate = sr;
1069 }
1070 }
1071 SwitchRange most_freq = *candidate;
1072 if (most_freq.cnt() == 0) {
1073 break;
1074 }
1075
1076 // Copy remaining ranges into another array
1077 int shift = 0;
1078 for (uint i = 0; i < nr; i++) {
1079 SwitchRange* sr = &lo[i];
1080 if (sr != candidate) {
1081 ranges[i-shift] = *sr;
1082 } else {
1083 shift++;
1084 if (i > 0 && i < nr-1) {
1085 SwitchRange prev = lo[i-1];
1086 prev.setRange(prev.lo(), sr->hi(), prev.dest(), prev.cnt());
1087 if (prev.adjoin(lo[i+1])) {
1088 shift++;
1089 i++;
1090 }
1091 ranges[i-shift] = prev;
1092 }
1093 }
1094 }
1095 nr -= shift;
1096
1097 // Evaluate cost of testing the most common range and performing a
1098 // binary search on the other ranges
1099 float cost = extra + compute_tree_cost(&ranges[0], &ranges[nr-1], total_cnt);
1100 if (cost >= min) {
1101 break;
1102 }
1103 // swap arrays
1104 lo = &ranges[0];
1105 hi = &ranges[nr-1];
1106
1107 // It pays off: emit the test for the most common range
1108 assert(most_freq.cnt() > 0, "must be taken");
1109 Node* val = _gvn.transform(new SubINode(key_val, _gvn.intcon(most_freq.lo())));
1110 Node* cmp = _gvn.transform(new CmpUNode(val, _gvn.intcon(java_subtract(most_freq.hi(), most_freq.lo()))));
1111 Node* tst = _gvn.transform(new BoolNode(cmp, BoolTest::le));
1112 IfNode* iff = create_and_map_if(control(), tst, if_prob(most_freq.cnt(), total_cnt), if_cnt(most_freq.cnt()));
1113 jump_if_true_fork(iff, most_freq.dest(), false);
1114
1115 sub += most_freq.cnt() / total_cnt;
1116 extra += 1 - sub;
1117 min = cost;
1118 }
1119 }
1120
1121 //----------------------------create_jump_tables-------------------------------
1122 bool Parse::create_jump_tables(Node* key_val, SwitchRange* lo, SwitchRange* hi) {
1123 // Are jumptables enabled
1124 if (!UseJumpTables) return false;
1125
1126 // Are jumptables supported
1127 if (!Matcher::has_match_rule(Op_Jump)) return false;
1128
1129 bool trim_ranges = !C->too_many_traps(method(), bci(), Deoptimization::Reason_unstable_if);
1130
1131 // Decide if a guard is needed to lop off big ranges at either (or
1132 // both) end(s) of the input set. We'll call this the default target
1133 // even though we can't be sure that it is the true "default".
1134
1135 bool needs_guard = false;
1136 int default_dest;
1137 int64_t total_outlier_size = 0;
1138 int64_t hi_size = ((int64_t)hi->hi()) - ((int64_t)hi->lo()) + 1;
1139 int64_t lo_size = ((int64_t)lo->hi()) - ((int64_t)lo->lo()) + 1;
1140
1141 if (lo->dest() == hi->dest()) {
1142 total_outlier_size = hi_size + lo_size;
1143 default_dest = lo->dest();
1144 } else if (lo_size > hi_size) {
1145 total_outlier_size = lo_size;
1146 default_dest = lo->dest();
1147 } else {
1148 total_outlier_size = hi_size;
1149 default_dest = hi->dest();
1150 }
1151
1152 float total = sum_of_cnts(lo, hi);
1153 float cost = compute_tree_cost(lo, hi, total);
1154
1155 // If a guard test will eliminate very sparse end ranges, then
1156 // it is worth the cost of an extra jump.
1157 float trimmed_cnt = 0;
1158 if (total_outlier_size > (MaxJumpTableSparseness * 4)) {
1159 needs_guard = true;
1160 if (default_dest == lo->dest()) {
1161 trimmed_cnt += lo->cnt();
1162 lo++;
1163 }
1164 if (default_dest == hi->dest()) {
1165 trimmed_cnt += hi->cnt();
1166 hi--;
1167 }
1168 }
1169
1170 // Find the total number of cases and ranges
1171 int64_t num_cases = ((int64_t)hi->hi()) - ((int64_t)lo->lo()) + 1;
1172 int num_range = hi - lo + 1;
1173
1174 // Don't create table if: too large, too small, or too sparse.
1175 if (num_cases > MaxJumpTableSize)
1176 return false;
1177 if (UseSwitchProfiling) {
1178 // MinJumpTableSize is set so with a well balanced binary tree,
1179 // when the number of ranges is MinJumpTableSize, it's cheaper to
1180 // go through a JumpNode that a tree of IfNodes. Average cost of a
1181 // tree of IfNodes with MinJumpTableSize is
1182 // log2f(MinJumpTableSize) comparisons. So if the cost computed
1183 // from profile data is less than log2f(MinJumpTableSize) then
1184 // going with the binary search is cheaper.
1185 if (cost < log2f(MinJumpTableSize)) {
1186 return false;
1187 }
1188 } else {
1189 if (num_cases < MinJumpTableSize)
1190 return false;
1191 }
1192 if (num_cases > (MaxJumpTableSparseness * num_range))
1193 return false;
1194
1195 // Normalize table lookups to zero
1196 int lowval = lo->lo();
1197 key_val = _gvn.transform( new SubINode(key_val, _gvn.intcon(lowval)) );
1198
1199 // Generate a guard to protect against input keyvals that aren't
1200 // in the switch domain.
1201 if (needs_guard) {
1202 Node* size = _gvn.intcon(num_cases);
1203 Node* cmp = _gvn.transform(new CmpUNode(key_val, size));
1204 Node* tst = _gvn.transform(new BoolNode(cmp, BoolTest::ge));
1205 IfNode* iff = create_and_map_if(control(), tst, if_prob(trimmed_cnt, total), if_cnt(trimmed_cnt));
1206 jump_if_true_fork(iff, default_dest, trim_ranges && trimmed_cnt == 0);
1207
1208 total -= trimmed_cnt;
1209 }
1210
1211 // Create an ideal node JumpTable that has projections
1212 // of all possible ranges for a switch statement
1213 // The key_val input must be converted to a pointer offset and scaled.
1214 // Compare Parse::array_addressing above.
1215
1216 // Clean the 32-bit int into a real 64-bit offset.
1217 // Otherwise, the jint value 0 might turn into an offset of 0x0800000000.
1218 // Make I2L conversion control dependent to prevent it from
1219 // floating above the range check during loop optimizations.
1220 // Do not use a narrow int type here to prevent the data path from dying
1221 // while the control path is not removed. This can happen if the type of key_val
1222 // is later known to be out of bounds of [0, num_cases] and therefore a narrow cast
1223 // would be replaced by TOP while C2 is not able to fold the corresponding range checks.
1224 // Set _carry_dependency for the cast to avoid being removed by IGVN.
1225 #ifdef _LP64
1226 key_val = C->constrained_convI2L(&_gvn, key_val, TypeInt::INT, control(), true /* carry_dependency */);
1227 #endif
1228
1229 // Shift the value by wordsize so we have an index into the table, rather
1230 // than a switch value
1231 Node *shiftWord = _gvn.MakeConX(wordSize);
1232 key_val = _gvn.transform( new MulXNode( key_val, shiftWord));
1233
1234 // Create the JumpNode
1235 Arena* arena = C->comp_arena();
1236 float* probs = (float*)arena->Amalloc(sizeof(float)*num_cases);
1237 int i = 0;
1238 if (total == 0) {
1239 for (SwitchRange* r = lo; r <= hi; r++) {
1240 for (int64_t j = r->lo(); j <= r->hi(); j++, i++) {
1241 probs[i] = 1.0F / num_cases;
1242 }
1243 }
1244 } else {
1245 for (SwitchRange* r = lo; r <= hi; r++) {
1246 float prob = r->cnt()/total;
1247 for (int64_t j = r->lo(); j <= r->hi(); j++, i++) {
1248 probs[i] = prob / (r->hi() - r->lo() + 1);
1249 }
1250 }
1251 }
1252
1253 ciMethodData* methodData = method()->method_data();
1254 ciMultiBranchData* profile = nullptr;
1255 if (methodData->is_mature()) {
1256 ciProfileData* data = methodData->bci_to_data(bci());
1257 if (data != nullptr && data->is_MultiBranchData()) {
1258 profile = (ciMultiBranchData*)data;
1259 }
1260 }
1261
1262 Node* jtn = _gvn.transform(new JumpNode(control(), key_val, num_cases, probs, profile == nullptr ? COUNT_UNKNOWN : total));
1263
1264 // These are the switch destinations hanging off the jumpnode
1265 i = 0;
1266 for (SwitchRange* r = lo; r <= hi; r++) {
1267 for (int64_t j = r->lo(); j <= r->hi(); j++, i++) {
1268 Node* input = _gvn.transform(new JumpProjNode(jtn, i, r->dest(), (int)(j - lowval)));
1269 {
1270 PreserveJVMState pjvms(this);
1271 set_control(input);
1272 jump_if_always_fork(r->dest(), trim_ranges && r->cnt() == 0);
1273 }
1274 }
1275 }
1276 assert(i == num_cases, "miscount of cases");
1277 stop_and_kill_map(); // no more uses for this JVMS
1278 return true;
1279 }
1280
1281 //----------------------------jump_switch_ranges-------------------------------
1282 void Parse::jump_switch_ranges(Node* key_val, SwitchRange *lo, SwitchRange *hi, int switch_depth) {
1283 Block* switch_block = block();
1284 bool trim_ranges = !C->too_many_traps(method(), bci(), Deoptimization::Reason_unstable_if);
1285
1286 if (switch_depth == 0) {
1287 // Do special processing for the top-level call.
1288 assert(lo->lo() == min_jint, "initial range must exhaust Type::INT");
1289 assert(hi->hi() == max_jint, "initial range must exhaust Type::INT");
1290
1291 // Decrement pred-numbers for the unique set of nodes.
1292 #ifdef ASSERT
1293 if (!trim_ranges) {
1294 // Ensure that the block's successors are a (duplicate-free) set.
1295 int successors_counted = 0; // block occurrences in [hi..lo]
1296 int unique_successors = switch_block->num_successors();
1297 for (int i = 0; i < unique_successors; i++) {
1298 Block* target = switch_block->successor_at(i);
1299
1300 // Check that the set of successors is the same in both places.
1301 int successors_found = 0;
1302 for (SwitchRange* p = lo; p <= hi; p++) {
1303 if (p->dest() == target->start()) successors_found++;
1304 }
1305 assert(successors_found > 0, "successor must be known");
1306 successors_counted += successors_found;
1307 }
1308 assert(successors_counted == (hi-lo)+1, "no unexpected successors");
1309 }
1310 #endif
1311
1312 // Maybe prune the inputs, based on the type of key_val.
1313 jint min_val = min_jint;
1314 jint max_val = max_jint;
1315 const TypeInt* ti = key_val->bottom_type()->isa_int();
1316 if (ti != nullptr) {
1317 min_val = ti->_lo;
1318 max_val = ti->_hi;
1319 assert(min_val <= max_val, "invalid int type");
1320 }
1321 while (lo->hi() < min_val) {
1322 lo++;
1323 }
1324 if (lo->lo() < min_val) {
1325 lo->setRange(min_val, lo->hi(), lo->dest(), lo->cnt());
1326 }
1327 while (hi->lo() > max_val) {
1328 hi--;
1329 }
1330 if (hi->hi() > max_val) {
1331 hi->setRange(hi->lo(), max_val, hi->dest(), hi->cnt());
1332 }
1333
1334 linear_search_switch_ranges(key_val, lo, hi);
1335 }
1336
1337 #ifndef PRODUCT
1338 if (switch_depth == 0) {
1339 _max_switch_depth = 0;
1340 _est_switch_depth = log2i_graceful((hi - lo + 1) - 1) + 1;
1341 }
1342 #endif
1343
1344 assert(lo <= hi, "must be a non-empty set of ranges");
1345 if (lo == hi) {
1346 jump_if_always_fork(lo->dest(), trim_ranges && lo->cnt() == 0);
1347 } else {
1348 assert(lo->hi() == (lo+1)->lo()-1, "contiguous ranges");
1349 assert(hi->lo() == (hi-1)->hi()+1, "contiguous ranges");
1350
1351 if (create_jump_tables(key_val, lo, hi)) return;
1352
1353 SwitchRange* mid = nullptr;
1354 float total_cnt = sum_of_cnts(lo, hi);
1355
1356 int nr = hi - lo + 1;
1357 if (UseSwitchProfiling) {
1358 // Don't keep the binary search tree balanced: pick up mid point
1359 // that split frequencies in half.
1360 float cnt = 0;
1361 for (SwitchRange* sr = lo; sr <= hi; sr++) {
1362 cnt += sr->cnt();
1363 if (cnt >= total_cnt / 2) {
1364 mid = sr;
1365 break;
1366 }
1367 }
1368 } else {
1369 mid = lo + nr/2;
1370
1371 // if there is an easy choice, pivot at a singleton:
1372 if (nr > 3 && !mid->is_singleton() && (mid-1)->is_singleton()) mid--;
1373
1374 assert(lo < mid && mid <= hi, "good pivot choice");
1375 assert(nr != 2 || mid == hi, "should pick higher of 2");
1376 assert(nr != 3 || mid == hi-1, "should pick middle of 3");
1377 }
1378
1379
1380 Node *test_val = _gvn.intcon(mid == lo ? mid->hi() : mid->lo());
1381
1382 if (mid->is_singleton()) {
1383 IfNode *iff_ne = jump_if_fork_int(key_val, test_val, BoolTest::ne, 1-if_prob(mid->cnt(), total_cnt), if_cnt(mid->cnt()));
1384 jump_if_false_fork(iff_ne, mid->dest(), trim_ranges && mid->cnt() == 0);
1385
1386 // Special Case: If there are exactly three ranges, and the high
1387 // and low range each go to the same place, omit the "gt" test,
1388 // since it will not discriminate anything.
1389 bool eq_test_only = (hi == lo+2 && hi->dest() == lo->dest() && mid == hi-1) || mid == lo;
1390
1391 // if there is a higher range, test for it and process it:
1392 if (mid < hi && !eq_test_only) {
1393 // two comparisons of same values--should enable 1 test for 2 branches
1394 // Use BoolTest::lt instead of BoolTest::gt
1395 float cnt = sum_of_cnts(lo, mid-1);
1396 IfNode *iff_lt = jump_if_fork_int(key_val, test_val, BoolTest::lt, if_prob(cnt, total_cnt), if_cnt(cnt));
1397 Node *iftrue = _gvn.transform( new IfTrueNode(iff_lt) );
1398 Node *iffalse = _gvn.transform( new IfFalseNode(iff_lt) );
1399 { PreserveJVMState pjvms(this);
1400 set_control(iffalse);
1401 jump_switch_ranges(key_val, mid+1, hi, switch_depth+1);
1402 }
1403 set_control(iftrue);
1404 }
1405
1406 } else {
1407 // mid is a range, not a singleton, so treat mid..hi as a unit
1408 float cnt = sum_of_cnts(mid == lo ? mid+1 : mid, hi);
1409 IfNode *iff_ge = jump_if_fork_int(key_val, test_val, mid == lo ? BoolTest::gt : BoolTest::ge, if_prob(cnt, total_cnt), if_cnt(cnt));
1410
1411 // if there is a higher range, test for it and process it:
1412 if (mid == hi) {
1413 jump_if_true_fork(iff_ge, mid->dest(), trim_ranges && cnt == 0);
1414 } else {
1415 Node *iftrue = _gvn.transform( new IfTrueNode(iff_ge) );
1416 Node *iffalse = _gvn.transform( new IfFalseNode(iff_ge) );
1417 { PreserveJVMState pjvms(this);
1418 set_control(iftrue);
1419 jump_switch_ranges(key_val, mid == lo ? mid+1 : mid, hi, switch_depth+1);
1420 }
1421 set_control(iffalse);
1422 }
1423 }
1424
1425 // in any case, process the lower range
1426 if (mid == lo) {
1427 if (mid->is_singleton()) {
1428 jump_switch_ranges(key_val, lo+1, hi, switch_depth+1);
1429 } else {
1430 jump_if_always_fork(lo->dest(), trim_ranges && lo->cnt() == 0);
1431 }
1432 } else {
1433 jump_switch_ranges(key_val, lo, mid-1, switch_depth+1);
1434 }
1435 }
1436
1437 // Decrease pred_count for each successor after all is done.
1438 if (switch_depth == 0) {
1439 int unique_successors = switch_block->num_successors();
1440 for (int i = 0; i < unique_successors; i++) {
1441 Block* target = switch_block->successor_at(i);
1442 // Throw away the pre-allocated path for each unique successor.
1443 target->next_path_num();
1444 }
1445 }
1446
1447 #ifndef PRODUCT
1448 _max_switch_depth = MAX2(switch_depth, _max_switch_depth);
1449 if (TraceOptoParse && Verbose && WizardMode && switch_depth == 0) {
1450 SwitchRange* r;
1451 int nsing = 0;
1452 for( r = lo; r <= hi; r++ ) {
1453 if( r->is_singleton() ) nsing++;
1454 }
1455 tty->print(">>> ");
1456 _method->print_short_name();
1457 tty->print_cr(" switch decision tree");
1458 tty->print_cr(" %d ranges (%d singletons), max_depth=%d, est_depth=%d",
1459 (int) (hi-lo+1), nsing, _max_switch_depth, _est_switch_depth);
1460 if (_max_switch_depth > _est_switch_depth) {
1461 tty->print_cr("******** BAD SWITCH DEPTH ********");
1462 }
1463 tty->print(" ");
1464 for( r = lo; r <= hi; r++ ) {
1465 r->print();
1466 }
1467 tty->cr();
1468 }
1469 #endif
1470 }
1471
1472 Node* Parse::floating_point_mod(Node* a, Node* b, BasicType type) {
1473 assert(type == BasicType::T_FLOAT || type == BasicType::T_DOUBLE, "only float and double are floating points");
1474 CallLeafPureNode* mod = type == BasicType::T_DOUBLE ? static_cast<CallLeafPureNode*>(new ModDNode(C, a, b)) : new ModFNode(C, a, b);
1475
1476 set_predefined_input_for_runtime_call(mod);
1477 mod = _gvn.transform(mod)->as_CallLeafPure();
1478 set_predefined_output_for_runtime_call(mod);
1479 Node* result = _gvn.transform(new ProjNode(mod, TypeFunc::Parms + 0));
1480 record_for_igvn(mod);
1481 return result;
1482 }
1483
1484 void Parse::l2f() {
1485 Node* f2 = pop();
1486 Node* f1 = pop();
1487 Node* c = make_runtime_call(RC_LEAF, OptoRuntime::l2f_Type(),
1488 CAST_FROM_FN_PTR(address, SharedRuntime::l2f),
1489 "l2f", nullptr, //no memory effects
1490 f1, f2);
1491 Node* res = _gvn.transform(new ProjNode(c, TypeFunc::Parms + 0));
1492
1493 push(res);
1494 }
1495
1496 // Handle jsr and jsr_w bytecode
1497 void Parse::do_jsr() {
1498 assert(bc() == Bytecodes::_jsr || bc() == Bytecodes::_jsr_w, "wrong bytecode");
1499
1500 // Store information about current state, tagged with new _jsr_bci
1501 int return_bci = iter().next_bci();
1502 int jsr_bci = (bc() == Bytecodes::_jsr) ? iter().get_dest() : iter().get_far_dest();
1503
1504 // The way we do things now, there is only one successor block
1505 // for the jsr, because the target code is cloned by ciTypeFlow.
1506 Block* target = successor_for_bci(jsr_bci);
1507
1508 // What got pushed?
1509 const Type* ret_addr = target->peek();
1510 assert(ret_addr->singleton(), "must be a constant (cloned jsr body)");
1511
1512 // Effect on jsr on stack
1513 push(_gvn.makecon(ret_addr));
1514
1515 // Flow to the jsr.
1516 merge(jsr_bci);
1517 }
1518
1519 // Handle ret bytecode
1520 void Parse::do_ret() {
1521 // Find to whom we return.
1522 assert(block()->num_successors() == 1, "a ret can only go one place now");
1523 Block* target = block()->successor_at(0);
1524 assert(!target->is_ready(), "our arrival must be expected");
1525 int pnum = target->next_path_num();
1526 merge_common(target, pnum);
1527 }
1528
1529 static bool has_injected_profile(BoolTest::mask btest, Node* test, int& taken, int& not_taken) {
1530 if (btest != BoolTest::eq && btest != BoolTest::ne) {
1531 // Only ::eq and ::ne are supported for profile injection.
1532 return false;
1533 }
1534 if (test->is_Cmp() &&
1535 test->in(1)->Opcode() == Op_ProfileBoolean) {
1536 ProfileBooleanNode* profile = (ProfileBooleanNode*)test->in(1);
1537 int false_cnt = profile->false_count();
1538 int true_cnt = profile->true_count();
1539
1540 // Counts matching depends on the actual test operation (::eq or ::ne).
1541 // No need to scale the counts because profile injection was designed
1542 // to feed exact counts into VM.
1543 taken = (btest == BoolTest::eq) ? false_cnt : true_cnt;
1544 not_taken = (btest == BoolTest::eq) ? true_cnt : false_cnt;
1545
1546 profile->consume();
1547 return true;
1548 }
1549 return false;
1550 }
1551
1552 // Give up if too few (or too many, in which case the sum will overflow) counts to be meaningful.
1553 // We also check that individual counters are positive first, otherwise the sum can become positive.
1554 // (check for saturation, integer overflow, and immature counts)
1555 static bool counters_are_meaningful(int counter1, int counter2, int min) {
1556 // check for saturation, including "uint" values too big to fit in "int"
1557 if (counter1 < 0 || counter2 < 0) {
1558 return false;
1559 }
1560 // check for integer overflow of the sum
1561 int64_t sum = (int64_t)counter1 + (int64_t)counter2;
1562 STATIC_ASSERT(sizeof(counter1) < sizeof(sum));
1563 if (sum > INT_MAX) {
1564 return false;
1565 }
1566 // check if mature
1567 return (counter1 + counter2) >= min;
1568 }
1569
1570 //--------------------------dynamic_branch_prediction--------------------------
1571 // Try to gather dynamic branch prediction behavior. Return a probability
1572 // of the branch being taken and set the "cnt" field. Returns a -1.0
1573 // if we need to use static prediction for some reason.
1574 float Parse::dynamic_branch_prediction(float &cnt, BoolTest::mask btest, Node* test) {
1575 ResourceMark rm;
1576
1577 cnt = COUNT_UNKNOWN;
1578
1579 int taken = 0;
1580 int not_taken = 0;
1581
1582 bool use_mdo = !has_injected_profile(btest, test, taken, not_taken);
1583
1584 if (use_mdo) {
1585 // Use MethodData information if it is available
1586 // FIXME: free the ProfileData structure
1587 ciMethodData* methodData = method()->method_data();
1588 if (!methodData->is_mature()) return PROB_UNKNOWN;
1589 ciProfileData* data = methodData->bci_to_data(bci());
1590 if (data == nullptr) {
1591 return PROB_UNKNOWN;
1592 }
1593 if (!data->is_JumpData()) return PROB_UNKNOWN;
1594
1595 // get taken and not taken values
1596 // NOTE: saturated UINT_MAX values become negative,
1597 // as do counts above INT_MAX.
1598 taken = data->as_JumpData()->taken();
1599 not_taken = 0;
1600 if (data->is_BranchData()) {
1601 not_taken = data->as_BranchData()->not_taken();
1602 }
1603
1604 // scale the counts to be commensurate with invocation counts:
1605 // NOTE: overflow for positive values is clamped at INT_MAX
1606 taken = method()->scale_count(taken);
1607 not_taken = method()->scale_count(not_taken);
1608 }
1609 // At this point, saturation or overflow is indicated by INT_MAX
1610 // or a negative value.
1611
1612 // Give up if too few (or too many, in which case the sum will overflow) counts to be meaningful.
1613 // We also check that individual counters are positive first, otherwise the sum can become positive.
1614 if (!counters_are_meaningful(taken, not_taken, 40)) {
1615 if (C->log() != nullptr) {
1616 C->log()->elem("branch target_bci='%d' taken='%d' not_taken='%d'", iter().get_dest(), taken, not_taken);
1617 }
1618 return PROB_UNKNOWN;
1619 }
1620
1621 // Compute frequency that we arrive here
1622 float sum = taken + not_taken;
1623 // Adjust, if this block is a cloned private block but the
1624 // Jump counts are shared. Taken the private counts for
1625 // just this path instead of the shared counts.
1626 if( block()->count() > 0 )
1627 sum = block()->count();
1628 cnt = sum / FreqCountInvocations;
1629
1630 // Pin probability to sane limits
1631 float prob;
1632 if( !taken )
1633 prob = (0+PROB_MIN) / 2;
1634 else if( !not_taken )
1635 prob = (1+PROB_MAX) / 2;
1636 else { // Compute probability of true path
1637 prob = (float)taken / (float)(taken + not_taken);
1638 if (prob > PROB_MAX) prob = PROB_MAX;
1639 if (prob < PROB_MIN) prob = PROB_MIN;
1640 }
1641
1642 assert((cnt > 0.0f) && (prob > 0.0f),
1643 "Bad frequency assignment in if cnt=%g prob=%g taken=%d not_taken=%d", cnt, prob, taken, not_taken);
1644
1645 if (C->log() != nullptr) {
1646 const char* prob_str = nullptr;
1647 if (prob >= PROB_MAX) prob_str = (prob == PROB_MAX) ? "max" : "always";
1648 if (prob <= PROB_MIN) prob_str = (prob == PROB_MIN) ? "min" : "never";
1649 char prob_str_buf[30];
1650 if (prob_str == nullptr) {
1651 jio_snprintf(prob_str_buf, sizeof(prob_str_buf), "%20.2f", prob);
1652 prob_str = prob_str_buf;
1653 }
1654 C->log()->elem("branch target_bci='%d' taken='%d' not_taken='%d' cnt='%f' prob='%s'",
1655 iter().get_dest(), taken, not_taken, cnt, prob_str);
1656 }
1657 return prob;
1658 }
1659
1660 //-----------------------------branch_prediction-------------------------------
1661 float Parse::branch_prediction(float& cnt,
1662 BoolTest::mask btest,
1663 int target_bci,
1664 Node* test) {
1665 float prob = dynamic_branch_prediction(cnt, btest, test);
1666 // If prob is unknown, switch to static prediction
1667 if (prob != PROB_UNKNOWN) return prob;
1668
1669 prob = PROB_FAIR; // Set default value
1670 if (btest == BoolTest::eq) // Exactly equal test?
1671 prob = PROB_STATIC_INFREQUENT; // Assume its relatively infrequent
1672 else if (btest == BoolTest::ne)
1673 prob = PROB_STATIC_FREQUENT; // Assume its relatively frequent
1674
1675 // If this is a conditional test guarding a backwards branch,
1676 // assume its a loop-back edge. Make it a likely taken branch.
1677 if (target_bci < bci()) {
1678 if (is_osr_parse()) { // Could be a hot OSR'd loop; force deopt
1679 // Since it's an OSR, we probably have profile data, but since
1680 // branch_prediction returned PROB_UNKNOWN, the counts are too small.
1681 // Let's make a special check here for completely zero counts.
1682 ciMethodData* methodData = method()->method_data();
1683 if (!methodData->is_empty()) {
1684 ciProfileData* data = methodData->bci_to_data(bci());
1685 // Only stop for truly zero counts, which mean an unknown part
1686 // of the OSR-ed method, and we want to deopt to gather more stats.
1687 // If you have ANY counts, then this loop is simply 'cold' relative
1688 // to the OSR loop.
1689 if (data == nullptr ||
1690 (data->as_BranchData()->taken() + data->as_BranchData()->not_taken() == 0)) {
1691 // This is the only way to return PROB_UNKNOWN:
1692 return PROB_UNKNOWN;
1693 }
1694 }
1695 }
1696 prob = PROB_STATIC_FREQUENT; // Likely to take backwards branch
1697 }
1698
1699 assert(prob != PROB_UNKNOWN, "must have some guess at this point");
1700 return prob;
1701 }
1702
1703 // The magic constants are chosen so as to match the output of
1704 // branch_prediction() when the profile reports a zero taken count.
1705 // It is important to distinguish zero counts unambiguously, because
1706 // some branches (e.g., _213_javac.Assembler.eliminate) validly produce
1707 // very small but nonzero probabilities, which if confused with zero
1708 // counts would keep the program recompiling indefinitely.
1709 bool Parse::seems_never_taken(float prob) const {
1710 return prob < PROB_MIN;
1711 }
1712
1713 //-------------------------------repush_if_args--------------------------------
1714 // Push arguments of an "if" bytecode back onto the stack by adjusting _sp.
1715 inline int Parse::repush_if_args() {
1716 if (PrintOpto && WizardMode) {
1717 tty->print("defending against excessive implicit null exceptions on %s @%d in ",
1718 Bytecodes::name(iter().cur_bc()), iter().cur_bci());
1719 method()->print_name(); tty->cr();
1720 }
1721 int bc_depth = - Bytecodes::depth(iter().cur_bc());
1722 assert(bc_depth == 1 || bc_depth == 2, "only two kinds of branches");
1723 DEBUG_ONLY(sync_jvms()); // argument(n) requires a synced jvms
1724 assert(argument(0) != nullptr, "must exist");
1725 assert(bc_depth == 1 || argument(1) != nullptr, "two must exist");
1726 inc_sp(bc_depth);
1727 return bc_depth;
1728 }
1729
1730 // Used by StressUnstableIfTraps
1731 static volatile int _trap_stress_counter = 0;
1732
1733 void Parse::increment_trap_stress_counter(Node*& counter, Node*& incr_store) {
1734 Node* counter_addr = makecon(TypeRawPtr::make((address)&_trap_stress_counter));
1735 counter = make_load(control(), counter_addr, TypeInt::INT, T_INT, MemNode::unordered);
1736 counter = _gvn.transform(new AddINode(counter, intcon(1)));
1737 incr_store = store_to_memory(control(), counter_addr, counter, T_INT, MemNode::unordered);
1738 }
1739
1740 //----------------------------------do_ifnull----------------------------------
1741 void Parse::do_ifnull(BoolTest::mask btest, Node *c) {
1742 int target_bci = iter().get_dest();
1743
1744 Node* counter = nullptr;
1745 Node* incr_store = nullptr;
1746 bool do_stress_trap = StressUnstableIfTraps && ((C->random() % 2) == 0);
1747 if (do_stress_trap) {
1748 increment_trap_stress_counter(counter, incr_store);
1749 }
1750
1751 Block* branch_block = successor_for_bci(target_bci);
1752 Block* next_block = successor_for_bci(iter().next_bci());
1753
1754 float cnt;
1755 float prob = branch_prediction(cnt, btest, target_bci, c);
1756 if (prob == PROB_UNKNOWN) {
1757 // (An earlier version of do_ifnull omitted this trap for OSR methods.)
1758 if (PrintOpto && Verbose) {
1759 tty->print_cr("Never-taken edge stops compilation at bci %d", bci());
1760 }
1761 repush_if_args(); // to gather stats on loop
1762 uncommon_trap(Deoptimization::Reason_unreached,
1763 Deoptimization::Action_reinterpret,
1764 nullptr, "cold");
1765 if (C->eliminate_boxing()) {
1766 // Mark the successor blocks as parsed
1767 branch_block->next_path_num();
1768 next_block->next_path_num();
1769 }
1770 return;
1771 }
1772
1773 NOT_PRODUCT(explicit_null_checks_inserted++);
1774
1775 // Generate real control flow
1776 Node *tst = _gvn.transform( new BoolNode( c, btest ) );
1777
1778 // Sanity check the probability value
1779 assert(prob > 0.0f,"Bad probability in Parser");
1780 // Need xform to put node in hash table
1781 IfNode *iff = create_and_xform_if( control(), tst, prob, cnt );
1782 assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser");
1783 // True branch
1784 { PreserveJVMState pjvms(this);
1785 Node* iftrue = _gvn.transform( new IfTrueNode (iff) );
1786 set_control(iftrue);
1787
1788 if (stopped()) { // Path is dead?
1789 NOT_PRODUCT(explicit_null_checks_elided++);
1790 if (C->eliminate_boxing()) {
1791 // Mark the successor block as parsed
1792 branch_block->next_path_num();
1793 }
1794 } else { // Path is live.
1795 adjust_map_after_if(btest, c, prob, branch_block);
1796 if (!stopped()) {
1797 merge(target_bci);
1798 }
1799 }
1800 }
1801
1802 // False branch
1803 Node* iffalse = _gvn.transform( new IfFalseNode(iff) );
1804 set_control(iffalse);
1805
1806 if (stopped()) { // Path is dead?
1807 NOT_PRODUCT(explicit_null_checks_elided++);
1808 if (C->eliminate_boxing()) {
1809 // Mark the successor block as parsed
1810 next_block->next_path_num();
1811 }
1812 } else { // Path is live.
1813 adjust_map_after_if(BoolTest(btest).negate(), c, 1.0-prob, next_block);
1814 }
1815
1816 if (do_stress_trap) {
1817 stress_trap(iff, counter, incr_store);
1818 }
1819 }
1820
1821 //------------------------------------do_if------------------------------------
1822 void Parse::do_if(BoolTest::mask btest, Node* c, bool can_trap, bool new_path, Node** ctrl_taken, Node** stress_count_mem) {
1823 int target_bci = iter().get_dest();
1824
1825 Block* branch_block = successor_for_bci(target_bci);
1826 Block* next_block = successor_for_bci(iter().next_bci());
1827
1828 float cnt;
1829 float prob = branch_prediction(cnt, btest, target_bci, c);
1830 float untaken_prob = 1.0 - prob;
1831
1832 if (prob == PROB_UNKNOWN) {
1833 if (PrintOpto && Verbose) {
1834 tty->print_cr("Never-taken edge stops compilation at bci %d", bci());
1835 }
1836 repush_if_args(); // to gather stats on loop
1837 uncommon_trap(Deoptimization::Reason_unreached,
1838 Deoptimization::Action_reinterpret,
1839 nullptr, "cold");
1840 if (C->eliminate_boxing()) {
1841 // Mark the successor blocks as parsed
1842 branch_block->next_path_num();
1843 next_block->next_path_num();
1844 }
1845 return;
1846 }
1847
1848 Node* counter = nullptr;
1849 Node* incr_store = nullptr;
1850 bool do_stress_trap = StressUnstableIfTraps && ((C->random() % 2) == 0);
1851 if (do_stress_trap) {
1852 increment_trap_stress_counter(counter, incr_store);
1853 if (stress_count_mem != nullptr) {
1854 *stress_count_mem = incr_store;
1855 }
1856 }
1857
1858 // Sanity check the probability value
1859 assert(0.0f < prob && prob < 1.0f,"Bad probability in Parser");
1860
1861 bool taken_if_true = true;
1862 // Convert BoolTest to canonical form:
1863 if (!BoolTest(btest).is_canonical()) {
1864 btest = BoolTest(btest).negate();
1865 taken_if_true = false;
1866 // prob is NOT updated here; it remains the probability of the taken
1867 // path (as opposed to the prob of the path guarded by an 'IfTrueNode').
1868 }
1869 assert(btest != BoolTest::eq, "!= is the only canonical exact test");
1870
1871 Node* tst0 = new BoolNode(c, btest);
1872 Node* tst = _gvn.transform(tst0);
1873 BoolTest::mask taken_btest = BoolTest::illegal;
1874 BoolTest::mask untaken_btest = BoolTest::illegal;
1875
1876 if (tst->is_Bool()) {
1877 // Refresh c from the transformed bool node, since it may be
1878 // simpler than the original c. Also re-canonicalize btest.
1879 // This wins when (Bool ne (Conv2B p) 0) => (Bool ne (CmpP p null)).
1880 // That can arise from statements like: if (x instanceof C) ...
1881 if (tst != tst0) {
1882 // Canonicalize one more time since transform can change it.
1883 btest = tst->as_Bool()->_test._test;
1884 if (!BoolTest(btest).is_canonical()) {
1885 // Reverse edges one more time...
1886 tst = _gvn.transform( tst->as_Bool()->negate(&_gvn) );
1887 btest = tst->as_Bool()->_test._test;
1888 assert(BoolTest(btest).is_canonical(), "sanity");
1889 taken_if_true = !taken_if_true;
1890 }
1891 c = tst->in(1);
1892 }
1893 BoolTest::mask neg_btest = BoolTest(btest).negate();
1894 taken_btest = taken_if_true ? btest : neg_btest;
1895 untaken_btest = taken_if_true ? neg_btest : btest;
1896 }
1897
1898 // Generate real control flow
1899 float true_prob = (taken_if_true ? prob : untaken_prob);
1900 IfNode* iff = create_and_map_if(control(), tst, true_prob, cnt);
1901 assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser");
1902 Node* taken_branch = new IfTrueNode(iff);
1903 Node* untaken_branch = new IfFalseNode(iff);
1904 if (!taken_if_true) { // Finish conversion to canonical form
1905 Node* tmp = taken_branch;
1906 taken_branch = untaken_branch;
1907 untaken_branch = tmp;
1908 }
1909
1910 // Branch is taken:
1911 { PreserveJVMState pjvms(this);
1912 taken_branch = _gvn.transform(taken_branch);
1913 set_control(taken_branch);
1914
1915 if (stopped()) {
1916 if (C->eliminate_boxing() && !new_path) {
1917 // Mark the successor block as parsed (if we haven't created a new path)
1918 branch_block->next_path_num();
1919 }
1920 } else {
1921 adjust_map_after_if(taken_btest, c, prob, branch_block, can_trap);
1922 if (!stopped()) {
1923 if (new_path) {
1924 // Merge by using a new path
1925 merge_new_path(target_bci);
1926 } else if (ctrl_taken != nullptr) {
1927 // Don't merge but save taken branch to be wired by caller
1928 *ctrl_taken = control();
1929 } else {
1930 merge(target_bci);
1931 }
1932 }
1933 }
1934 }
1935
1936 untaken_branch = _gvn.transform(untaken_branch);
1937 set_control(untaken_branch);
1938
1939 // Branch not taken.
1940 if (stopped() && ctrl_taken == nullptr) {
1941 if (C->eliminate_boxing()) {
1942 // Mark the successor block as parsed (if caller does not re-wire control flow)
1943 next_block->next_path_num();
1944 }
1945 } else {
1946 adjust_map_after_if(untaken_btest, c, untaken_prob, next_block, can_trap);
1947 }
1948
1949 if (do_stress_trap) {
1950 stress_trap(iff, counter, incr_store);
1951 }
1952 }
1953
1954
1955 static ProfilePtrKind speculative_ptr_kind(const TypeOopPtr* t) {
1956 if (t->speculative() == nullptr) {
1957 return ProfileUnknownNull;
1958 }
1959 if (t->speculative_always_null()) {
1960 return ProfileAlwaysNull;
1961 }
1962 if (t->speculative_maybe_null()) {
1963 return ProfileMaybeNull;
1964 }
1965 return ProfileNeverNull;
1966 }
1967
1968 void Parse::acmp_always_null_input(Node* input, const TypeOopPtr* tinput, BoolTest::mask btest, Node* eq_region) {
1969 inc_sp(2);
1970 Node* cast = null_check_common(input, T_OBJECT, true, nullptr,
1971 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check) &&
1972 speculative_ptr_kind(tinput) == ProfileAlwaysNull);
1973 dec_sp(2);
1974 if (btest == BoolTest::ne) {
1975 {
1976 PreserveJVMState pjvms(this);
1977 replace_in_map(input, cast);
1978 int target_bci = iter().get_dest();
1979 merge(target_bci);
1980 }
1981 record_for_igvn(eq_region);
1982 set_control(_gvn.transform(eq_region));
1983 } else {
1984 replace_in_map(input, cast);
1985 }
1986 }
1987
1988 Node* Parse::acmp_null_check(Node* input, const TypeOopPtr* tinput, ProfilePtrKind input_ptr, Node*& null_ctl) {
1989 inc_sp(2);
1990 null_ctl = top();
1991 Node* cast = null_check_oop(input, &null_ctl,
1992 input_ptr == ProfileNeverNull || (input_ptr == ProfileUnknownNull && !too_many_traps_or_recompiles(Deoptimization::Reason_null_check)),
1993 false,
1994 speculative_ptr_kind(tinput) == ProfileNeverNull &&
1995 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check));
1996 dec_sp(2);
1997 assert(!stopped(), "null input should have been caught earlier");
1998 return cast;
1999 }
2000
2001 void Parse::acmp_known_non_inline_type_input(Node* input, const TypeOopPtr* tinput, ProfilePtrKind input_ptr, ciKlass* input_type, BoolTest::mask btest, Node* eq_region) {
2002 Node* ne_region = new RegionNode(1);
2003 Node* null_ctl;
2004 Node* cast = acmp_null_check(input, tinput, input_ptr, null_ctl);
2005 ne_region->add_req(null_ctl);
2006
2007 Node* slow_ctl = type_check_receiver(cast, input_type, 1.0, &cast);
2008 {
2009 PreserveJVMState pjvms(this);
2010 inc_sp(2);
2011 set_control(slow_ctl);
2012 Deoptimization::DeoptReason reason;
2013 if (tinput->speculative_type() != nullptr && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
2014 reason = Deoptimization::Reason_speculate_class_check;
2015 } else {
2016 reason = Deoptimization::Reason_class_check;
2017 }
2018 uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
2019 }
2020 ne_region->add_req(control());
2021
2022 record_for_igvn(ne_region);
2023 set_control(_gvn.transform(ne_region));
2024 if (btest == BoolTest::ne) {
2025 {
2026 PreserveJVMState pjvms(this);
2027 if (null_ctl == top()) {
2028 replace_in_map(input, cast);
2029 }
2030 int target_bci = iter().get_dest();
2031 merge(target_bci);
2032 }
2033 record_for_igvn(eq_region);
2034 set_control(_gvn.transform(eq_region));
2035 } else {
2036 if (null_ctl == top()) {
2037 replace_in_map(input, cast);
2038 }
2039 set_control(_gvn.transform(ne_region));
2040 }
2041 }
2042
2043 void Parse::acmp_unknown_non_inline_type_input(Node* input, const TypeOopPtr* tinput, ProfilePtrKind input_ptr, BoolTest::mask btest, Node* eq_region) {
2044 Node* ne_region = new RegionNode(1);
2045 Node* null_ctl;
2046 Node* cast = acmp_null_check(input, tinput, input_ptr, null_ctl);
2047 ne_region->add_req(null_ctl);
2048
2049 {
2050 BuildCutout unless(this, inline_type_test(cast, /* is_inline = */ false), PROB_MAX);
2051 inc_sp(2);
2052 uncommon_trap_exact(Deoptimization::Reason_class_check, Deoptimization::Action_maybe_recompile);
2053 }
2054
2055 ne_region->add_req(control());
2056
2057 record_for_igvn(ne_region);
2058 set_control(_gvn.transform(ne_region));
2059 if (btest == BoolTest::ne) {
2060 {
2061 PreserveJVMState pjvms(this);
2062 if (null_ctl == top()) {
2063 replace_in_map(input, cast);
2064 }
2065 int target_bci = iter().get_dest();
2066 merge(target_bci);
2067 }
2068 record_for_igvn(eq_region);
2069 set_control(_gvn.transform(eq_region));
2070 } else {
2071 if (null_ctl == top()) {
2072 replace_in_map(input, cast);
2073 }
2074 set_control(_gvn.transform(ne_region));
2075 }
2076 }
2077
2078 void Parse::do_acmp(BoolTest::mask btest, Node* left, Node* right) {
2079 ciKlass* left_type = nullptr;
2080 ciKlass* right_type = nullptr;
2081 ProfilePtrKind left_ptr = ProfileUnknownNull;
2082 ProfilePtrKind right_ptr = ProfileUnknownNull;
2083 bool left_inline_type = true;
2084 bool right_inline_type = true;
2085
2086 // Leverage profiling at acmp
2087 if (UseACmpProfile) {
2088 method()->acmp_profiled_type(bci(), left_type, right_type, left_ptr, right_ptr, left_inline_type, right_inline_type);
2089 if (too_many_traps_or_recompiles(Deoptimization::Reason_class_check)) {
2090 left_type = nullptr;
2091 right_type = nullptr;
2092 left_inline_type = true;
2093 right_inline_type = true;
2094 }
2095 if (too_many_traps_or_recompiles(Deoptimization::Reason_null_check)) {
2096 left_ptr = ProfileUnknownNull;
2097 right_ptr = ProfileUnknownNull;
2098 }
2099 }
2100
2101 if (UseTypeSpeculation) {
2102 record_profile_for_speculation(left, left_type, left_ptr);
2103 record_profile_for_speculation(right, right_type, right_ptr);
2104 }
2105
2106 if (!EnableValhalla) {
2107 Node* cmp = CmpP(left, right);
2108 cmp = optimize_cmp_with_klass(cmp);
2109 do_if(btest, cmp);
2110 return;
2111 }
2112
2113 // Check for equality before potentially allocating
2114 if (left == right) {
2115 do_if(btest, makecon(TypeInt::CC_EQ));
2116 return;
2117 }
2118
2119 // Allocate inline type operands and re-execute on deoptimization
2120 if (left->is_InlineType()) {
2121 if (_gvn.type(right)->is_zero_type() ||
2122 (right->is_InlineType() && _gvn.type(right->as_InlineType()->get_null_marker())->is_zero_type())) {
2123 // Null checking a scalarized but nullable inline type. Check the null marker
2124 // input instead of the oop input to avoid keeping buffer allocations alive.
2125 Node* cmp = CmpI(left->as_InlineType()->get_null_marker(), intcon(0));
2126 do_if(btest, cmp);
2127 return;
2128 } else {
2129 PreserveReexecuteState preexecs(this);
2130 inc_sp(2);
2131 jvms()->set_should_reexecute(true);
2132 left = left->as_InlineType()->buffer(this)->get_oop();
2133 }
2134 }
2135 if (right->is_InlineType()) {
2136 PreserveReexecuteState preexecs(this);
2137 inc_sp(2);
2138 jvms()->set_should_reexecute(true);
2139 right = right->as_InlineType()->buffer(this)->get_oop();
2140 }
2141
2142 // First, do a normal pointer comparison
2143 const TypeOopPtr* tleft = _gvn.type(left)->isa_oopptr();
2144 const TypeOopPtr* tright = _gvn.type(right)->isa_oopptr();
2145 Node* cmp = CmpP(left, right);
2146 cmp = optimize_cmp_with_klass(cmp);
2147 if (tleft == nullptr || !tleft->can_be_inline_type() ||
2148 tright == nullptr || !tright->can_be_inline_type()) {
2149 // This is sufficient, if one of the operands can't be an inline type
2150 do_if(btest, cmp);
2151 return;
2152 }
2153
2154 // Don't add traps to unstable if branches because additional checks are required to
2155 // decide if the operands are equal/substitutable and we therefore shouldn't prune
2156 // branches for one if based on the profiling of the acmp branches.
2157 // Also, OptimizeUnstableIf would set an incorrect re-rexecution state because it
2158 // assumes that there is a 1-1 mapping between the if and the acmp branches and that
2159 // hitting a trap means that we will take the corresponding acmp branch on re-execution.
2160 const bool can_trap = true;
2161
2162 Node* eq_region = nullptr;
2163 if (btest == BoolTest::eq) {
2164 do_if(btest, cmp, !can_trap, true);
2165 if (stopped()) {
2166 // Pointers are equal, operands must be equal
2167 return;
2168 }
2169 } else {
2170 assert(btest == BoolTest::ne, "only eq or ne");
2171 Node* is_not_equal = nullptr;
2172 eq_region = new RegionNode(3);
2173 {
2174 PreserveJVMState pjvms(this);
2175 // Pointers are not equal, but more checks are needed to determine if the operands are (not) substitutable
2176 do_if(btest, cmp, !can_trap, false, &is_not_equal);
2177 if (!stopped()) {
2178 eq_region->init_req(1, control());
2179 }
2180 }
2181 if (is_not_equal == nullptr || is_not_equal->is_top()) {
2182 record_for_igvn(eq_region);
2183 set_control(_gvn.transform(eq_region));
2184 return;
2185 }
2186 set_control(is_not_equal);
2187 }
2188
2189 // Prefer speculative types if available
2190 if (!too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
2191 if (tleft->speculative_type() != nullptr) {
2192 left_type = tleft->speculative_type();
2193 }
2194 if (tright->speculative_type() != nullptr) {
2195 right_type = tright->speculative_type();
2196 }
2197 }
2198
2199 if (speculative_ptr_kind(tleft) != ProfileMaybeNull && speculative_ptr_kind(tleft) != ProfileUnknownNull) {
2200 ProfilePtrKind speculative_left_ptr = speculative_ptr_kind(tleft);
2201 if (speculative_left_ptr == ProfileAlwaysNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_assert)) {
2202 left_ptr = speculative_left_ptr;
2203 } else if (speculative_left_ptr == ProfileNeverNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check)) {
2204 left_ptr = speculative_left_ptr;
2205 }
2206 }
2207 if (speculative_ptr_kind(tright) != ProfileMaybeNull && speculative_ptr_kind(tright) != ProfileUnknownNull) {
2208 ProfilePtrKind speculative_right_ptr = speculative_ptr_kind(tright);
2209 if (speculative_right_ptr == ProfileAlwaysNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_assert)) {
2210 right_ptr = speculative_right_ptr;
2211 } else if (speculative_right_ptr == ProfileNeverNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check)) {
2212 right_ptr = speculative_right_ptr;
2213 }
2214 }
2215
2216 if (left_ptr == ProfileAlwaysNull) {
2217 // Comparison with null. Assert the input is indeed null and we're done.
2218 acmp_always_null_input(left, tleft, btest, eq_region);
2219 return;
2220 }
2221 if (right_ptr == ProfileAlwaysNull) {
2222 // Comparison with null. Assert the input is indeed null and we're done.
2223 acmp_always_null_input(right, tright, btest, eq_region);
2224 return;
2225 }
2226 if (left_type != nullptr && !left_type->is_inlinetype()) {
2227 // Comparison with an object of known type
2228 acmp_known_non_inline_type_input(left, tleft, left_ptr, left_type, btest, eq_region);
2229 return;
2230 }
2231 if (right_type != nullptr && !right_type->is_inlinetype()) {
2232 // Comparison with an object of known type
2233 acmp_known_non_inline_type_input(right, tright, right_ptr, right_type, btest, eq_region);
2234 return;
2235 }
2236 if (!left_inline_type) {
2237 // Comparison with an object known not to be an inline type
2238 acmp_unknown_non_inline_type_input(left, tleft, left_ptr, btest, eq_region);
2239 return;
2240 }
2241 if (!right_inline_type) {
2242 // Comparison with an object known not to be an inline type
2243 acmp_unknown_non_inline_type_input(right, tright, right_ptr, btest, eq_region);
2244 return;
2245 }
2246
2247 // Pointers are not equal, check if first operand is non-null
2248 Node* ne_region = new RegionNode(6);
2249 Node* null_ctl;
2250 Node* not_null_right = acmp_null_check(right, tright, right_ptr, null_ctl);
2251 ne_region->init_req(1, null_ctl);
2252
2253 // First operand is non-null, check if it is an inline type
2254 Node* is_value = inline_type_test(not_null_right);
2255 IfNode* is_value_iff = create_and_map_if(control(), is_value, PROB_FAIR, COUNT_UNKNOWN);
2256 Node* not_value = _gvn.transform(new IfFalseNode(is_value_iff));
2257 ne_region->init_req(2, not_value);
2258 set_control(_gvn.transform(new IfTrueNode(is_value_iff)));
2259
2260 // The first operand is an inline type, check if the second operand is non-null
2261 Node* not_null_left = acmp_null_check(left, tleft, left_ptr, null_ctl);
2262 ne_region->init_req(3, null_ctl);
2263
2264 // Check if both operands are of the same class.
2265 Node* kls_left = load_object_klass(not_null_left);
2266 Node* kls_right = load_object_klass(not_null_right);
2267 Node* kls_cmp = CmpP(kls_left, kls_right);
2268 Node* kls_bol = _gvn.transform(new BoolNode(kls_cmp, BoolTest::ne));
2269 IfNode* kls_iff = create_and_map_if(control(), kls_bol, PROB_FAIR, COUNT_UNKNOWN);
2270 Node* kls_ne = _gvn.transform(new IfTrueNode(kls_iff));
2271 set_control(_gvn.transform(new IfFalseNode(kls_iff)));
2272 ne_region->init_req(4, kls_ne);
2273
2274 if (stopped()) {
2275 record_for_igvn(ne_region);
2276 set_control(_gvn.transform(ne_region));
2277 if (btest == BoolTest::ne) {
2278 {
2279 PreserveJVMState pjvms(this);
2280 int target_bci = iter().get_dest();
2281 merge(target_bci);
2282 }
2283 record_for_igvn(eq_region);
2284 set_control(_gvn.transform(eq_region));
2285 }
2286 return;
2287 }
2288
2289 // Both operands are values types of the same class, we need to perform a
2290 // substitutability test. Delegate to ValueObjectMethods::isSubstitutable().
2291 Node* ne_io_phi = PhiNode::make(ne_region, i_o());
2292 Node* mem = reset_memory();
2293 Node* ne_mem_phi = PhiNode::make(ne_region, mem);
2294
2295 Node* eq_io_phi = nullptr;
2296 Node* eq_mem_phi = nullptr;
2297 if (eq_region != nullptr) {
2298 eq_io_phi = PhiNode::make(eq_region, i_o());
2299 eq_mem_phi = PhiNode::make(eq_region, mem);
2300 }
2301
2302 set_all_memory(mem);
2303
2304 kill_dead_locals();
2305 ciSymbol* subst_method_name = UseAltSubstitutabilityMethod ? ciSymbols::isSubstitutableAlt_name() : ciSymbols::isSubstitutable_name();
2306 ciMethod* subst_method = ciEnv::current()->ValueObjectMethods_klass()->find_method(subst_method_name, ciSymbols::object_object_boolean_signature());
2307 CallStaticJavaNode *call = new CallStaticJavaNode(C, TypeFunc::make(subst_method), SharedRuntime::get_resolve_static_call_stub(), subst_method);
2308 call->set_override_symbolic_info(true);
2309 call->init_req(TypeFunc::Parms, not_null_left);
2310 call->init_req(TypeFunc::Parms+1, not_null_right);
2311 inc_sp(2);
2312 set_edges_for_java_call(call, false, false);
2313 Node* ret = set_results_for_java_call(call, false, true);
2314 dec_sp(2);
2315
2316 // Test the return value of ValueObjectMethods::isSubstitutable()
2317 // This is the last check, do_if can emit traps now.
2318 Node* subst_cmp = _gvn.transform(new CmpINode(ret, intcon(1)));
2319 Node* ctl = C->top();
2320 Node* stress_count_mem = nullptr;
2321 if (btest == BoolTest::eq) {
2322 PreserveJVMState pjvms(this);
2323 do_if(btest, subst_cmp, can_trap, false, nullptr, &stress_count_mem);
2324 if (!stopped()) {
2325 ctl = control();
2326 }
2327 } else {
2328 assert(btest == BoolTest::ne, "only eq or ne");
2329 PreserveJVMState pjvms(this);
2330 do_if(btest, subst_cmp, can_trap, false, &ctl, &stress_count_mem);
2331 if (!stopped()) {
2332 eq_region->init_req(2, control());
2333 eq_io_phi->init_req(2, i_o());
2334 eq_mem_phi->init_req(2, reset_memory());
2335 }
2336 }
2337 if (stress_count_mem != nullptr) {
2338 set_memory(stress_count_mem, stress_count_mem->adr_type());
2339 }
2340 ne_region->init_req(5, ctl);
2341 ne_io_phi->init_req(5, i_o());
2342 ne_mem_phi->init_req(5, reset_memory());
2343
2344 record_for_igvn(ne_region);
2345 set_control(_gvn.transform(ne_region));
2346 set_i_o(_gvn.transform(ne_io_phi));
2347 set_all_memory(_gvn.transform(ne_mem_phi));
2348
2349 if (btest == BoolTest::ne) {
2350 {
2351 PreserveJVMState pjvms(this);
2352 int target_bci = iter().get_dest();
2353 merge(target_bci);
2354 }
2355
2356 record_for_igvn(eq_region);
2357 set_control(_gvn.transform(eq_region));
2358 set_i_o(_gvn.transform(eq_io_phi));
2359 set_all_memory(_gvn.transform(eq_mem_phi));
2360 }
2361 }
2362
2363 // Force unstable if traps to be taken randomly to trigger intermittent bugs such as incorrect debug information.
2364 // Add another if before the unstable if that checks a "random" condition at runtime (a simple shared counter) and
2365 // then either takes the trap or executes the original, unstable if.
2366 void Parse::stress_trap(IfNode* orig_iff, Node* counter, Node* incr_store) {
2367 // Search for an unstable if trap
2368 CallStaticJavaNode* trap = nullptr;
2369 assert(orig_iff->Opcode() == Op_If && orig_iff->outcnt() == 2, "malformed if");
2370 ProjNode* trap_proj = orig_iff->uncommon_trap_proj(trap, Deoptimization::Reason_unstable_if);
2371 if (trap == nullptr || !trap->jvms()->should_reexecute()) {
2372 // No suitable trap found. Remove unused counter load and increment.
2373 C->gvn_replace_by(incr_store, incr_store->in(MemNode::Memory));
2374 return;
2375 }
2376
2377 // Remove trap from optimization list since we add another path to the trap.
2378 bool success = C->remove_unstable_if_trap(trap, true);
2379 assert(success, "Trap already modified");
2380
2381 // Add a check before the original if that will trap with a certain frequency and execute the original if otherwise
2382 int freq_log = (C->random() % 31) + 1; // Random logarithmic frequency in [1, 31]
2383 Node* mask = intcon(right_n_bits(freq_log));
2384 counter = _gvn.transform(new AndINode(counter, mask));
2385 Node* cmp = _gvn.transform(new CmpINode(counter, intcon(0)));
2386 Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::mask::eq));
2387 IfNode* iff = _gvn.transform(new IfNode(orig_iff->in(0), bol, orig_iff->_prob, orig_iff->_fcnt))->as_If();
2388 Node* if_true = _gvn.transform(new IfTrueNode(iff));
2389 Node* if_false = _gvn.transform(new IfFalseNode(iff));
2390 assert(!if_true->is_top() && !if_false->is_top(), "trap always / never taken");
2391
2392 // Trap
2393 assert(trap_proj->outcnt() == 1, "some other nodes are dependent on the trap projection");
2394
2395 Node* trap_region = new RegionNode(3);
2396 trap_region->set_req(1, trap_proj);
2397 trap_region->set_req(2, if_true);
2398 trap->set_req(0, _gvn.transform(trap_region));
2399
2400 // Don't trap, execute original if
2401 orig_iff->set_req(0, if_false);
2402 }
2403
2404 bool Parse::path_is_suitable_for_uncommon_trap(float prob) const {
2405 // Randomly skip emitting an uncommon trap
2406 if (StressUnstableIfTraps && ((C->random() % 2) == 0)) {
2407 return false;
2408 }
2409 // Don't want to speculate on uncommon traps when running with -Xcomp
2410 if (!UseInterpreter) {
2411 return false;
2412 }
2413 return seems_never_taken(prob) &&
2414 !C->too_many_traps(method(), bci(), Deoptimization::Reason_unstable_if);
2415 }
2416
2417 void Parse::maybe_add_predicate_after_if(Block* path) {
2418 if (path->is_SEL_head() && path->preds_parsed() == 0) {
2419 // Add predicates at bci of if dominating the loop so traps can be
2420 // recorded on the if's profile data
2421 int bc_depth = repush_if_args();
2422 add_parse_predicates();
2423 dec_sp(bc_depth);
2424 path->set_has_predicates();
2425 }
2426 }
2427
2428
2429 //----------------------------adjust_map_after_if------------------------------
2430 // Adjust the JVM state to reflect the result of taking this path.
2431 // Basically, it means inspecting the CmpNode controlling this
2432 // branch, seeing how it constrains a tested value, and then
2433 // deciding if it's worth our while to encode this constraint
2434 // as graph nodes in the current abstract interpretation map.
2435 void Parse::adjust_map_after_if(BoolTest::mask btest, Node* c, float prob, Block* path, bool can_trap) {
2436 if (!c->is_Cmp()) {
2437 maybe_add_predicate_after_if(path);
2438 return;
2439 }
2440
2441 if (stopped() || btest == BoolTest::illegal) {
2442 return; // nothing to do
2443 }
2444
2445 bool is_fallthrough = (path == successor_for_bci(iter().next_bci()));
2446
2447 if (can_trap && path_is_suitable_for_uncommon_trap(prob)) {
2448 repush_if_args();
2449 Node* call = uncommon_trap(Deoptimization::Reason_unstable_if,
2450 Deoptimization::Action_reinterpret,
2451 nullptr,
2452 (is_fallthrough ? "taken always" : "taken never"));
2453
2454 if (call != nullptr) {
2455 C->record_unstable_if_trap(new UnstableIfTrap(call->as_CallStaticJava(), path));
2456 }
2457 return;
2458 }
2459
2460 Node* val = c->in(1);
2461 Node* con = c->in(2);
2462 const Type* tcon = _gvn.type(con);
2463 const Type* tval = _gvn.type(val);
2464 bool have_con = tcon->singleton();
2465 if (tval->singleton()) {
2466 if (!have_con) {
2467 // Swap, so constant is in con.
2468 con = val;
2469 tcon = tval;
2470 val = c->in(2);
2471 tval = _gvn.type(val);
2472 btest = BoolTest(btest).commute();
2473 have_con = true;
2474 } else {
2475 // Do we have two constants? Then leave well enough alone.
2476 have_con = false;
2477 }
2478 }
2479 if (!have_con) { // remaining adjustments need a con
2480 maybe_add_predicate_after_if(path);
2481 return;
2482 }
2483
2484 sharpen_type_after_if(btest, con, tcon, val, tval);
2485 maybe_add_predicate_after_if(path);
2486 }
2487
2488
2489 static Node* extract_obj_from_klass_load(PhaseGVN* gvn, Node* n) {
2490 Node* ldk;
2491 if (n->is_DecodeNKlass()) {
2492 if (n->in(1)->Opcode() != Op_LoadNKlass) {
2493 return nullptr;
2494 } else {
2495 ldk = n->in(1);
2496 }
2497 } else if (n->Opcode() != Op_LoadKlass) {
2498 return nullptr;
2499 } else {
2500 ldk = n;
2501 }
2502 assert(ldk != nullptr && ldk->is_Load(), "should have found a LoadKlass or LoadNKlass node");
2503
2504 Node* adr = ldk->in(MemNode::Address);
2505 intptr_t off = 0;
2506 Node* obj = AddPNode::Ideal_base_and_offset(adr, gvn, off);
2507 if (obj == nullptr || off != oopDesc::klass_offset_in_bytes()) // loading oopDesc::_klass?
2508 return nullptr;
2509 const TypePtr* tp = gvn->type(obj)->is_ptr();
2510 if (tp == nullptr || !(tp->isa_instptr() || tp->isa_aryptr())) // is obj a Java object ptr?
2511 return nullptr;
2512
2513 return obj;
2514 }
2515
2516 void Parse::sharpen_type_after_if(BoolTest::mask btest,
2517 Node* con, const Type* tcon,
2518 Node* val, const Type* tval) {
2519 // Look for opportunities to sharpen the type of a node
2520 // whose klass is compared with a constant klass.
2521 if (btest == BoolTest::eq && tcon->isa_klassptr()) {
2522 Node* obj = extract_obj_from_klass_load(&_gvn, val);
2523 const TypeOopPtr* con_type = tcon->isa_klassptr()->as_instance_type();
2524 if (obj != nullptr && (con_type->isa_instptr() || con_type->isa_aryptr())) {
2525 // Found:
2526 // Bool(CmpP(LoadKlass(obj._klass), ConP(Foo.klass)), [eq])
2527 // or the narrowOop equivalent.
2528 const Type* obj_type = _gvn.type(obj);
2529 const TypeOopPtr* tboth = obj_type->join_speculative(con_type)->isa_oopptr();
2530 if (tboth != nullptr && tboth->klass_is_exact() && tboth != obj_type &&
2531 tboth->higher_equal(obj_type)) {
2532 // obj has to be of the exact type Foo if the CmpP succeeds.
2533 int obj_in_map = map()->find_edge(obj);
2534 JVMState* jvms = this->jvms();
2535 if (obj_in_map >= 0 &&
2536 (jvms->is_loc(obj_in_map) || jvms->is_stk(obj_in_map))) {
2537 TypeNode* ccast = new CheckCastPPNode(control(), obj, tboth);
2538 const Type* tcc = ccast->as_Type()->type();
2539 assert(tcc != obj_type && tcc->higher_equal(obj_type), "must improve");
2540 // Delay transform() call to allow recovery of pre-cast value
2541 // at the control merge.
2542 _gvn.set_type_bottom(ccast);
2543 record_for_igvn(ccast);
2544 if (tboth->is_inlinetypeptr()) {
2545 ccast = InlineTypeNode::make_from_oop(this, ccast, tboth->exact_klass(true)->as_inline_klass());
2546 }
2547 // Here's the payoff.
2548 replace_in_map(obj, ccast);
2549 }
2550 }
2551 }
2552 }
2553
2554 int val_in_map = map()->find_edge(val);
2555 if (val_in_map < 0) return; // replace_in_map would be useless
2556 {
2557 JVMState* jvms = this->jvms();
2558 if (!(jvms->is_loc(val_in_map) ||
2559 jvms->is_stk(val_in_map)))
2560 return; // again, it would be useless
2561 }
2562
2563 // Check for a comparison to a constant, and "know" that the compared
2564 // value is constrained on this path.
2565 assert(tcon->singleton(), "");
2566 ConstraintCastNode* ccast = nullptr;
2567 Node* cast = nullptr;
2568
2569 switch (btest) {
2570 case BoolTest::eq: // Constant test?
2571 {
2572 const Type* tboth = tcon->join_speculative(tval);
2573 if (tboth == tval) break; // Nothing to gain.
2574 if (tcon->isa_int()) {
2575 ccast = new CastIINode(control(), val, tboth);
2576 } else if (tcon == TypePtr::NULL_PTR) {
2577 // Cast to null, but keep the pointer identity temporarily live.
2578 ccast = new CastPPNode(control(), val, tboth);
2579 } else {
2580 const TypeF* tf = tcon->isa_float_constant();
2581 const TypeD* td = tcon->isa_double_constant();
2582 // Exclude tests vs float/double 0 as these could be
2583 // either +0 or -0. Just because you are equal to +0
2584 // doesn't mean you ARE +0!
2585 // Note, following code also replaces Long and Oop values.
2586 if ((!tf || tf->_f != 0.0) &&
2587 (!td || td->_d != 0.0))
2588 cast = con; // Replace non-constant val by con.
2589 }
2590 }
2591 break;
2592
2593 case BoolTest::ne:
2594 if (tcon == TypePtr::NULL_PTR) {
2595 cast = cast_not_null(val, false);
2596 }
2597 break;
2598
2599 default:
2600 // (At this point we could record int range types with CastII.)
2601 break;
2602 }
2603
2604 if (ccast != nullptr) {
2605 const Type* tcc = ccast->as_Type()->type();
2606 assert(tcc != tval && tcc->higher_equal(tval), "must improve");
2607 // Delay transform() call to allow recovery of pre-cast value
2608 // at the control merge.
2609 _gvn.set_type_bottom(ccast);
2610 record_for_igvn(ccast);
2611 cast = ccast;
2612 }
2613
2614 if (cast != nullptr) { // Here's the payoff.
2615 replace_in_map(val, cast);
2616 }
2617 }
2618
2619 /**
2620 * Use speculative type to optimize CmpP node: if comparison is
2621 * against the low level class, cast the object to the speculative
2622 * type if any. CmpP should then go away.
2623 *
2624 * @param c expected CmpP node
2625 * @return result of CmpP on object casted to speculative type
2626 *
2627 */
2628 Node* Parse::optimize_cmp_with_klass(Node* c) {
2629 // If this is transformed by the _gvn to a comparison with the low
2630 // level klass then we may be able to use speculation
2631 if (c->Opcode() == Op_CmpP &&
2632 (c->in(1)->Opcode() == Op_LoadKlass || c->in(1)->Opcode() == Op_DecodeNKlass) &&
2633 c->in(2)->is_Con()) {
2634 Node* load_klass = nullptr;
2635 Node* decode = nullptr;
2636 if (c->in(1)->Opcode() == Op_DecodeNKlass) {
2637 decode = c->in(1);
2638 load_klass = c->in(1)->in(1);
2639 } else {
2640 load_klass = c->in(1);
2641 }
2642 if (load_klass->in(2)->is_AddP()) {
2643 Node* addp = load_klass->in(2);
2644 Node* obj = addp->in(AddPNode::Address);
2645 const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
2646 if (obj_type->speculative_type_not_null() != nullptr) {
2647 ciKlass* k = obj_type->speculative_type();
2648 inc_sp(2);
2649 obj = maybe_cast_profiled_obj(obj, k);
2650 dec_sp(2);
2651 if (obj->is_InlineType()) {
2652 assert(obj->as_InlineType()->is_allocated(&_gvn), "must be allocated");
2653 obj = obj->as_InlineType()->get_oop();
2654 }
2655 // Make the CmpP use the casted obj
2656 addp = basic_plus_adr(obj, addp->in(AddPNode::Offset));
2657 load_klass = load_klass->clone();
2658 load_klass->set_req(2, addp);
2659 load_klass = _gvn.transform(load_klass);
2660 if (decode != nullptr) {
2661 decode = decode->clone();
2662 decode->set_req(1, load_klass);
2663 load_klass = _gvn.transform(decode);
2664 }
2665 c = c->clone();
2666 c->set_req(1, load_klass);
2667 c = _gvn.transform(c);
2668 }
2669 }
2670 }
2671 return c;
2672 }
2673
2674 //------------------------------do_one_bytecode--------------------------------
2675 // Parse this bytecode, and alter the Parsers JVM->Node mapping
2676 void Parse::do_one_bytecode() {
2677 Node *a, *b, *c, *d; // Handy temps
2678 BoolTest::mask btest;
2679 int i;
2680
2681 assert(!has_exceptions(), "bytecode entry state must be clear of throws");
2682
2683 if (C->check_node_count(NodeLimitFudgeFactor * 5,
2684 "out of nodes parsing method")) {
2685 return;
2686 }
2687
2688 #ifdef ASSERT
2689 // for setting breakpoints
2690 if (TraceOptoParse) {
2691 tty->print(" @");
2692 dump_bci(bci());
2693 tty->print(" %s", Bytecodes::name(bc()));
2694 tty->cr();
2695 }
2696 #endif
2697
2698 switch (bc()) {
2699 case Bytecodes::_nop:
2700 // do nothing
2701 break;
2702 case Bytecodes::_lconst_0:
2703 push_pair(longcon(0));
2704 break;
2705
2706 case Bytecodes::_lconst_1:
2707 push_pair(longcon(1));
2708 break;
2709
2710 case Bytecodes::_fconst_0:
2711 push(zerocon(T_FLOAT));
2712 break;
2713
2714 case Bytecodes::_fconst_1:
2715 push(makecon(TypeF::ONE));
2716 break;
2717
2718 case Bytecodes::_fconst_2:
2719 push(makecon(TypeF::make(2.0f)));
2720 break;
2721
2722 case Bytecodes::_dconst_0:
2723 push_pair(zerocon(T_DOUBLE));
2724 break;
2725
2726 case Bytecodes::_dconst_1:
2727 push_pair(makecon(TypeD::ONE));
2728 break;
2729
2730 case Bytecodes::_iconst_m1:push(intcon(-1)); break;
2731 case Bytecodes::_iconst_0: push(intcon( 0)); break;
2732 case Bytecodes::_iconst_1: push(intcon( 1)); break;
2733 case Bytecodes::_iconst_2: push(intcon( 2)); break;
2734 case Bytecodes::_iconst_3: push(intcon( 3)); break;
2735 case Bytecodes::_iconst_4: push(intcon( 4)); break;
2736 case Bytecodes::_iconst_5: push(intcon( 5)); break;
2737 case Bytecodes::_bipush: push(intcon(iter().get_constant_u1())); break;
2738 case Bytecodes::_sipush: push(intcon(iter().get_constant_u2())); break;
2739 case Bytecodes::_aconst_null: push(null()); break;
2740
2741 case Bytecodes::_ldc:
2742 case Bytecodes::_ldc_w:
2743 case Bytecodes::_ldc2_w: {
2744 // ciTypeFlow should trap if the ldc is in error state or if the constant is not loaded
2745 assert(!iter().is_in_error(), "ldc is in error state");
2746 ciConstant constant = iter().get_constant();
2747 assert(constant.is_loaded(), "constant is not loaded");
2748 const Type* con_type = Type::make_from_constant(constant);
2749 if (con_type != nullptr) {
2750 push_node(con_type->basic_type(), makecon(con_type));
2751 }
2752 break;
2753 }
2754
2755 case Bytecodes::_aload_0:
2756 push( local(0) );
2757 break;
2758 case Bytecodes::_aload_1:
2759 push( local(1) );
2760 break;
2761 case Bytecodes::_aload_2:
2762 push( local(2) );
2763 break;
2764 case Bytecodes::_aload_3:
2765 push( local(3) );
2766 break;
2767 case Bytecodes::_aload:
2768 push( local(iter().get_index()) );
2769 break;
2770
2771 case Bytecodes::_fload_0:
2772 case Bytecodes::_iload_0:
2773 push( local(0) );
2774 break;
2775 case Bytecodes::_fload_1:
2776 case Bytecodes::_iload_1:
2777 push( local(1) );
2778 break;
2779 case Bytecodes::_fload_2:
2780 case Bytecodes::_iload_2:
2781 push( local(2) );
2782 break;
2783 case Bytecodes::_fload_3:
2784 case Bytecodes::_iload_3:
2785 push( local(3) );
2786 break;
2787 case Bytecodes::_fload:
2788 case Bytecodes::_iload:
2789 push( local(iter().get_index()) );
2790 break;
2791 case Bytecodes::_lload_0:
2792 push_pair_local( 0 );
2793 break;
2794 case Bytecodes::_lload_1:
2795 push_pair_local( 1 );
2796 break;
2797 case Bytecodes::_lload_2:
2798 push_pair_local( 2 );
2799 break;
2800 case Bytecodes::_lload_3:
2801 push_pair_local( 3 );
2802 break;
2803 case Bytecodes::_lload:
2804 push_pair_local( iter().get_index() );
2805 break;
2806
2807 case Bytecodes::_dload_0:
2808 push_pair_local(0);
2809 break;
2810 case Bytecodes::_dload_1:
2811 push_pair_local(1);
2812 break;
2813 case Bytecodes::_dload_2:
2814 push_pair_local(2);
2815 break;
2816 case Bytecodes::_dload_3:
2817 push_pair_local(3);
2818 break;
2819 case Bytecodes::_dload:
2820 push_pair_local(iter().get_index());
2821 break;
2822 case Bytecodes::_fstore_0:
2823 case Bytecodes::_istore_0:
2824 case Bytecodes::_astore_0:
2825 set_local( 0, pop() );
2826 break;
2827 case Bytecodes::_fstore_1:
2828 case Bytecodes::_istore_1:
2829 case Bytecodes::_astore_1:
2830 set_local( 1, pop() );
2831 break;
2832 case Bytecodes::_fstore_2:
2833 case Bytecodes::_istore_2:
2834 case Bytecodes::_astore_2:
2835 set_local( 2, pop() );
2836 break;
2837 case Bytecodes::_fstore_3:
2838 case Bytecodes::_istore_3:
2839 case Bytecodes::_astore_3:
2840 set_local( 3, pop() );
2841 break;
2842 case Bytecodes::_fstore:
2843 case Bytecodes::_istore:
2844 case Bytecodes::_astore:
2845 set_local( iter().get_index(), pop() );
2846 break;
2847 // long stores
2848 case Bytecodes::_lstore_0:
2849 set_pair_local( 0, pop_pair() );
2850 break;
2851 case Bytecodes::_lstore_1:
2852 set_pair_local( 1, pop_pair() );
2853 break;
2854 case Bytecodes::_lstore_2:
2855 set_pair_local( 2, pop_pair() );
2856 break;
2857 case Bytecodes::_lstore_3:
2858 set_pair_local( 3, pop_pair() );
2859 break;
2860 case Bytecodes::_lstore:
2861 set_pair_local( iter().get_index(), pop_pair() );
2862 break;
2863
2864 // double stores
2865 case Bytecodes::_dstore_0:
2866 set_pair_local( 0, pop_pair() );
2867 break;
2868 case Bytecodes::_dstore_1:
2869 set_pair_local( 1, pop_pair() );
2870 break;
2871 case Bytecodes::_dstore_2:
2872 set_pair_local( 2, pop_pair() );
2873 break;
2874 case Bytecodes::_dstore_3:
2875 set_pair_local( 3, pop_pair() );
2876 break;
2877 case Bytecodes::_dstore:
2878 set_pair_local( iter().get_index(), pop_pair() );
2879 break;
2880
2881 case Bytecodes::_pop: dec_sp(1); break;
2882 case Bytecodes::_pop2: dec_sp(2); break;
2883 case Bytecodes::_swap:
2884 a = pop();
2885 b = pop();
2886 push(a);
2887 push(b);
2888 break;
2889 case Bytecodes::_dup:
2890 a = pop();
2891 push(a);
2892 push(a);
2893 break;
2894 case Bytecodes::_dup_x1:
2895 a = pop();
2896 b = pop();
2897 push( a );
2898 push( b );
2899 push( a );
2900 break;
2901 case Bytecodes::_dup_x2:
2902 a = pop();
2903 b = pop();
2904 c = pop();
2905 push( a );
2906 push( c );
2907 push( b );
2908 push( a );
2909 break;
2910 case Bytecodes::_dup2:
2911 a = pop();
2912 b = pop();
2913 push( b );
2914 push( a );
2915 push( b );
2916 push( a );
2917 break;
2918
2919 case Bytecodes::_dup2_x1:
2920 // before: .. c, b, a
2921 // after: .. b, a, c, b, a
2922 // not tested
2923 a = pop();
2924 b = pop();
2925 c = pop();
2926 push( b );
2927 push( a );
2928 push( c );
2929 push( b );
2930 push( a );
2931 break;
2932 case Bytecodes::_dup2_x2:
2933 // before: .. d, c, b, a
2934 // after: .. b, a, d, c, b, a
2935 // not tested
2936 a = pop();
2937 b = pop();
2938 c = pop();
2939 d = pop();
2940 push( b );
2941 push( a );
2942 push( d );
2943 push( c );
2944 push( b );
2945 push( a );
2946 break;
2947
2948 case Bytecodes::_arraylength: {
2949 // Must do null-check with value on expression stack
2950 Node *ary = null_check(peek(), T_ARRAY);
2951 // Compile-time detect of null-exception?
2952 if (stopped()) return;
2953 a = pop();
2954 push(load_array_length(a));
2955 break;
2956 }
2957
2958 case Bytecodes::_baload: array_load(T_BYTE); break;
2959 case Bytecodes::_caload: array_load(T_CHAR); break;
2960 case Bytecodes::_iaload: array_load(T_INT); break;
2961 case Bytecodes::_saload: array_load(T_SHORT); break;
2962 case Bytecodes::_faload: array_load(T_FLOAT); break;
2963 case Bytecodes::_aaload: array_load(T_OBJECT); break;
2964 case Bytecodes::_laload: array_load(T_LONG); break;
2965 case Bytecodes::_daload: array_load(T_DOUBLE); break;
2966 case Bytecodes::_bastore: array_store(T_BYTE); break;
2967 case Bytecodes::_castore: array_store(T_CHAR); break;
2968 case Bytecodes::_iastore: array_store(T_INT); break;
2969 case Bytecodes::_sastore: array_store(T_SHORT); break;
2970 case Bytecodes::_fastore: array_store(T_FLOAT); break;
2971 case Bytecodes::_aastore: array_store(T_OBJECT); break;
2972 case Bytecodes::_lastore: array_store(T_LONG); break;
2973 case Bytecodes::_dastore: array_store(T_DOUBLE); break;
2974
2975 case Bytecodes::_getfield:
2976 do_getfield();
2977 break;
2978
2979 case Bytecodes::_getstatic:
2980 do_getstatic();
2981 break;
2982
2983 case Bytecodes::_putfield:
2984 do_putfield();
2985 break;
2986
2987 case Bytecodes::_putstatic:
2988 do_putstatic();
2989 break;
2990
2991 case Bytecodes::_irem:
2992 // Must keep both values on the expression-stack during null-check
2993 zero_check_int(peek());
2994 // Compile-time detect of null-exception?
2995 if (stopped()) return;
2996 b = pop();
2997 a = pop();
2998 push(_gvn.transform(new ModINode(control(), a, b)));
2999 break;
3000 case Bytecodes::_idiv:
3001 // Must keep both values on the expression-stack during null-check
3002 zero_check_int(peek());
3003 // Compile-time detect of null-exception?
3004 if (stopped()) return;
3005 b = pop();
3006 a = pop();
3007 push( _gvn.transform( new DivINode(control(),a,b) ) );
3008 break;
3009 case Bytecodes::_imul:
3010 b = pop(); a = pop();
3011 push( _gvn.transform( new MulINode(a,b) ) );
3012 break;
3013 case Bytecodes::_iadd:
3014 b = pop(); a = pop();
3015 push( _gvn.transform( new AddINode(a,b) ) );
3016 break;
3017 case Bytecodes::_ineg:
3018 a = pop();
3019 push( _gvn.transform( new SubINode(_gvn.intcon(0),a)) );
3020 break;
3021 case Bytecodes::_isub:
3022 b = pop(); a = pop();
3023 push( _gvn.transform( new SubINode(a,b) ) );
3024 break;
3025 case Bytecodes::_iand:
3026 b = pop(); a = pop();
3027 push( _gvn.transform( new AndINode(a,b) ) );
3028 break;
3029 case Bytecodes::_ior:
3030 b = pop(); a = pop();
3031 push( _gvn.transform( new OrINode(a,b) ) );
3032 break;
3033 case Bytecodes::_ixor:
3034 b = pop(); a = pop();
3035 push( _gvn.transform( new XorINode(a,b) ) );
3036 break;
3037 case Bytecodes::_ishl:
3038 b = pop(); a = pop();
3039 push( _gvn.transform( new LShiftINode(a,b) ) );
3040 break;
3041 case Bytecodes::_ishr:
3042 b = pop(); a = pop();
3043 push( _gvn.transform( new RShiftINode(a,b) ) );
3044 break;
3045 case Bytecodes::_iushr:
3046 b = pop(); a = pop();
3047 push( _gvn.transform( new URShiftINode(a,b) ) );
3048 break;
3049
3050 case Bytecodes::_fneg:
3051 a = pop();
3052 b = _gvn.transform(new NegFNode (a));
3053 push(b);
3054 break;
3055
3056 case Bytecodes::_fsub:
3057 b = pop();
3058 a = pop();
3059 c = _gvn.transform( new SubFNode(a,b) );
3060 push(c);
3061 break;
3062
3063 case Bytecodes::_fadd:
3064 b = pop();
3065 a = pop();
3066 c = _gvn.transform( new AddFNode(a,b) );
3067 push(c);
3068 break;
3069
3070 case Bytecodes::_fmul:
3071 b = pop();
3072 a = pop();
3073 c = _gvn.transform( new MulFNode(a,b) );
3074 push(c);
3075 break;
3076
3077 case Bytecodes::_fdiv:
3078 b = pop();
3079 a = pop();
3080 c = _gvn.transform( new DivFNode(nullptr,a,b) );
3081 push(c);
3082 break;
3083
3084 case Bytecodes::_frem:
3085 // Generate a ModF node.
3086 b = pop();
3087 a = pop();
3088 push(floating_point_mod(a, b, BasicType::T_FLOAT));
3089 break;
3090
3091 case Bytecodes::_fcmpl:
3092 b = pop();
3093 a = pop();
3094 c = _gvn.transform( new CmpF3Node( a, b));
3095 push(c);
3096 break;
3097 case Bytecodes::_fcmpg:
3098 b = pop();
3099 a = pop();
3100
3101 // Same as fcmpl but need to flip the unordered case. Swap the inputs,
3102 // which negates the result sign except for unordered. Flip the unordered
3103 // as well by using CmpF3 which implements unordered-lesser instead of
3104 // unordered-greater semantics. Finally, commute the result bits. Result
3105 // is same as using a CmpF3Greater except we did it with CmpF3 alone.
3106 c = _gvn.transform( new CmpF3Node( b, a));
3107 c = _gvn.transform( new SubINode(_gvn.intcon(0),c) );
3108 push(c);
3109 break;
3110
3111 case Bytecodes::_f2i:
3112 a = pop();
3113 push(_gvn.transform(new ConvF2INode(a)));
3114 break;
3115
3116 case Bytecodes::_d2i:
3117 a = pop_pair();
3118 b = _gvn.transform(new ConvD2INode(a));
3119 push( b );
3120 break;
3121
3122 case Bytecodes::_f2d:
3123 a = pop();
3124 b = _gvn.transform( new ConvF2DNode(a));
3125 push_pair( b );
3126 break;
3127
3128 case Bytecodes::_d2f:
3129 a = pop_pair();
3130 b = _gvn.transform( new ConvD2FNode(a));
3131 push( b );
3132 break;
3133
3134 case Bytecodes::_l2f:
3135 if (Matcher::convL2FSupported()) {
3136 a = pop_pair();
3137 b = _gvn.transform( new ConvL2FNode(a));
3138 push(b);
3139 } else {
3140 l2f();
3141 }
3142 break;
3143
3144 case Bytecodes::_l2d:
3145 a = pop_pair();
3146 b = _gvn.transform( new ConvL2DNode(a));
3147 push_pair(b);
3148 break;
3149
3150 case Bytecodes::_f2l:
3151 a = pop();
3152 b = _gvn.transform( new ConvF2LNode(a));
3153 push_pair(b);
3154 break;
3155
3156 case Bytecodes::_d2l:
3157 a = pop_pair();
3158 b = _gvn.transform( new ConvD2LNode(a));
3159 push_pair(b);
3160 break;
3161
3162 case Bytecodes::_dsub:
3163 b = pop_pair();
3164 a = pop_pair();
3165 c = _gvn.transform( new SubDNode(a,b) );
3166 push_pair(c);
3167 break;
3168
3169 case Bytecodes::_dadd:
3170 b = pop_pair();
3171 a = pop_pair();
3172 c = _gvn.transform( new AddDNode(a,b) );
3173 push_pair(c);
3174 break;
3175
3176 case Bytecodes::_dmul:
3177 b = pop_pair();
3178 a = pop_pair();
3179 c = _gvn.transform( new MulDNode(a,b) );
3180 push_pair(c);
3181 break;
3182
3183 case Bytecodes::_ddiv:
3184 b = pop_pair();
3185 a = pop_pair();
3186 c = _gvn.transform( new DivDNode(nullptr,a,b) );
3187 push_pair(c);
3188 break;
3189
3190 case Bytecodes::_dneg:
3191 a = pop_pair();
3192 b = _gvn.transform(new NegDNode (a));
3193 push_pair(b);
3194 break;
3195
3196 case Bytecodes::_drem:
3197 // Generate a ModD node.
3198 b = pop_pair();
3199 a = pop_pair();
3200 push_pair(floating_point_mod(a, b, BasicType::T_DOUBLE));
3201 break;
3202
3203 case Bytecodes::_dcmpl:
3204 b = pop_pair();
3205 a = pop_pair();
3206 c = _gvn.transform( new CmpD3Node( a, b));
3207 push(c);
3208 break;
3209
3210 case Bytecodes::_dcmpg:
3211 b = pop_pair();
3212 a = pop_pair();
3213 // Same as dcmpl but need to flip the unordered case.
3214 // Commute the inputs, which negates the result sign except for unordered.
3215 // Flip the unordered as well by using CmpD3 which implements
3216 // unordered-lesser instead of unordered-greater semantics.
3217 // Finally, negate the result bits. Result is same as using a
3218 // CmpD3Greater except we did it with CmpD3 alone.
3219 c = _gvn.transform( new CmpD3Node( b, a));
3220 c = _gvn.transform( new SubINode(_gvn.intcon(0),c) );
3221 push(c);
3222 break;
3223
3224
3225 // Note for longs -> lo word is on TOS, hi word is on TOS - 1
3226 case Bytecodes::_land:
3227 b = pop_pair();
3228 a = pop_pair();
3229 c = _gvn.transform( new AndLNode(a,b) );
3230 push_pair(c);
3231 break;
3232 case Bytecodes::_lor:
3233 b = pop_pair();
3234 a = pop_pair();
3235 c = _gvn.transform( new OrLNode(a,b) );
3236 push_pair(c);
3237 break;
3238 case Bytecodes::_lxor:
3239 b = pop_pair();
3240 a = pop_pair();
3241 c = _gvn.transform( new XorLNode(a,b) );
3242 push_pair(c);
3243 break;
3244
3245 case Bytecodes::_lshl:
3246 b = pop(); // the shift count
3247 a = pop_pair(); // value to be shifted
3248 c = _gvn.transform( new LShiftLNode(a,b) );
3249 push_pair(c);
3250 break;
3251 case Bytecodes::_lshr:
3252 b = pop(); // the shift count
3253 a = pop_pair(); // value to be shifted
3254 c = _gvn.transform( new RShiftLNode(a,b) );
3255 push_pair(c);
3256 break;
3257 case Bytecodes::_lushr:
3258 b = pop(); // the shift count
3259 a = pop_pair(); // value to be shifted
3260 c = _gvn.transform( new URShiftLNode(a,b) );
3261 push_pair(c);
3262 break;
3263 case Bytecodes::_lmul:
3264 b = pop_pair();
3265 a = pop_pair();
3266 c = _gvn.transform( new MulLNode(a,b) );
3267 push_pair(c);
3268 break;
3269
3270 case Bytecodes::_lrem:
3271 // Must keep both values on the expression-stack during null-check
3272 assert(peek(0) == top(), "long word order");
3273 zero_check_long(peek(1));
3274 // Compile-time detect of null-exception?
3275 if (stopped()) return;
3276 b = pop_pair();
3277 a = pop_pair();
3278 c = _gvn.transform( new ModLNode(control(),a,b) );
3279 push_pair(c);
3280 break;
3281
3282 case Bytecodes::_ldiv:
3283 // Must keep both values on the expression-stack during null-check
3284 assert(peek(0) == top(), "long word order");
3285 zero_check_long(peek(1));
3286 // Compile-time detect of null-exception?
3287 if (stopped()) return;
3288 b = pop_pair();
3289 a = pop_pair();
3290 c = _gvn.transform( new DivLNode(control(),a,b) );
3291 push_pair(c);
3292 break;
3293
3294 case Bytecodes::_ladd:
3295 b = pop_pair();
3296 a = pop_pair();
3297 c = _gvn.transform( new AddLNode(a,b) );
3298 push_pair(c);
3299 break;
3300 case Bytecodes::_lsub:
3301 b = pop_pair();
3302 a = pop_pair();
3303 c = _gvn.transform( new SubLNode(a,b) );
3304 push_pair(c);
3305 break;
3306 case Bytecodes::_lcmp:
3307 // Safepoints are now inserted _before_ branches. The long-compare
3308 // bytecode painfully produces a 3-way value (-1,0,+1) which requires a
3309 // slew of control flow. These are usually followed by a CmpI vs zero and
3310 // a branch; this pattern then optimizes to the obvious long-compare and
3311 // branch. However, if the branch is backwards there's a Safepoint
3312 // inserted. The inserted Safepoint captures the JVM state at the
3313 // pre-branch point, i.e. it captures the 3-way value. Thus if a
3314 // long-compare is used to control a loop the debug info will force
3315 // computation of the 3-way value, even though the generated code uses a
3316 // long-compare and branch. We try to rectify the situation by inserting
3317 // a SafePoint here and have it dominate and kill the safepoint added at a
3318 // following backwards branch. At this point the JVM state merely holds 2
3319 // longs but not the 3-way value.
3320 switch (iter().next_bc()) {
3321 case Bytecodes::_ifgt:
3322 case Bytecodes::_iflt:
3323 case Bytecodes::_ifge:
3324 case Bytecodes::_ifle:
3325 case Bytecodes::_ifne:
3326 case Bytecodes::_ifeq:
3327 // If this is a backwards branch in the bytecodes, add Safepoint
3328 maybe_add_safepoint(iter().next_get_dest());
3329 default:
3330 break;
3331 }
3332 b = pop_pair();
3333 a = pop_pair();
3334 c = _gvn.transform( new CmpL3Node( a, b ));
3335 push(c);
3336 break;
3337
3338 case Bytecodes::_lneg:
3339 a = pop_pair();
3340 b = _gvn.transform( new SubLNode(longcon(0),a));
3341 push_pair(b);
3342 break;
3343 case Bytecodes::_l2i:
3344 a = pop_pair();
3345 push( _gvn.transform( new ConvL2INode(a)));
3346 break;
3347 case Bytecodes::_i2l:
3348 a = pop();
3349 b = _gvn.transform( new ConvI2LNode(a));
3350 push_pair(b);
3351 break;
3352 case Bytecodes::_i2b:
3353 // Sign extend
3354 a = pop();
3355 a = Compile::narrow_value(T_BYTE, a, nullptr, &_gvn, true);
3356 push(a);
3357 break;
3358 case Bytecodes::_i2s:
3359 a = pop();
3360 a = Compile::narrow_value(T_SHORT, a, nullptr, &_gvn, true);
3361 push(a);
3362 break;
3363 case Bytecodes::_i2c:
3364 a = pop();
3365 a = Compile::narrow_value(T_CHAR, a, nullptr, &_gvn, true);
3366 push(a);
3367 break;
3368
3369 case Bytecodes::_i2f:
3370 a = pop();
3371 b = _gvn.transform( new ConvI2FNode(a) ) ;
3372 push(b);
3373 break;
3374
3375 case Bytecodes::_i2d:
3376 a = pop();
3377 b = _gvn.transform( new ConvI2DNode(a));
3378 push_pair(b);
3379 break;
3380
3381 case Bytecodes::_iinc: // Increment local
3382 i = iter().get_index(); // Get local index
3383 set_local( i, _gvn.transform( new AddINode( _gvn.intcon(iter().get_iinc_con()), local(i) ) ) );
3384 break;
3385
3386 // Exit points of synchronized methods must have an unlock node
3387 case Bytecodes::_return:
3388 return_current(nullptr);
3389 break;
3390
3391 case Bytecodes::_ireturn:
3392 case Bytecodes::_areturn:
3393 case Bytecodes::_freturn:
3394 return_current(cast_to_non_larval(pop()));
3395 break;
3396 case Bytecodes::_lreturn:
3397 case Bytecodes::_dreturn:
3398 return_current(pop_pair());
3399 break;
3400
3401 case Bytecodes::_athrow:
3402 // null exception oop throws null pointer exception
3403 null_check(peek());
3404 if (stopped()) return;
3405 // Hook the thrown exception directly to subsequent handlers.
3406 if (BailoutToInterpreterForThrows) {
3407 // Keep method interpreted from now on.
3408 uncommon_trap(Deoptimization::Reason_unhandled,
3409 Deoptimization::Action_make_not_compilable);
3410 return;
3411 }
3412 if (env()->jvmti_can_post_on_exceptions()) {
3413 // check if we must post exception events, take uncommon trap if so (with must_throw = false)
3414 uncommon_trap_if_should_post_on_exceptions(Deoptimization::Reason_unhandled, false);
3415 }
3416 // Here if either can_post_on_exceptions or should_post_on_exceptions is false
3417 add_exception_state(make_exception_state(peek()));
3418 break;
3419
3420 case Bytecodes::_goto: // fall through
3421 case Bytecodes::_goto_w: {
3422 int target_bci = (bc() == Bytecodes::_goto) ? iter().get_dest() : iter().get_far_dest();
3423
3424 // If this is a backwards branch in the bytecodes, add Safepoint
3425 maybe_add_safepoint(target_bci);
3426
3427 // Merge the current control into the target basic block
3428 merge(target_bci);
3429
3430 // See if we can get some profile data and hand it off to the next block
3431 Block *target_block = block()->successor_for_bci(target_bci);
3432 if (target_block->pred_count() != 1) break;
3433 ciMethodData* methodData = method()->method_data();
3434 if (!methodData->is_mature()) break;
3435 ciProfileData* data = methodData->bci_to_data(bci());
3436 assert(data != nullptr && data->is_JumpData(), "need JumpData for taken branch");
3437 int taken = ((ciJumpData*)data)->taken();
3438 taken = method()->scale_count(taken);
3439 target_block->set_count(taken);
3440 break;
3441 }
3442
3443 case Bytecodes::_ifnull: btest = BoolTest::eq; goto handle_if_null;
3444 case Bytecodes::_ifnonnull: btest = BoolTest::ne; goto handle_if_null;
3445 handle_if_null:
3446 // If this is a backwards branch in the bytecodes, add Safepoint
3447 maybe_add_safepoint(iter().get_dest());
3448 a = null();
3449 b = cast_to_non_larval(pop());
3450 if (b->is_InlineType()) {
3451 // Null checking a scalarized but nullable inline type. Check the null marker
3452 // input instead of the oop input to avoid keeping buffer allocations alive
3453 c = _gvn.transform(new CmpINode(b->as_InlineType()->get_null_marker(), zerocon(T_INT)));
3454 } else {
3455 if (!_gvn.type(b)->speculative_maybe_null() &&
3456 !too_many_traps(Deoptimization::Reason_speculate_null_check)) {
3457 inc_sp(1);
3458 Node* null_ctl = top();
3459 b = null_check_oop(b, &null_ctl, true, true, true);
3460 assert(null_ctl->is_top(), "no null control here");
3461 dec_sp(1);
3462 } else if (_gvn.type(b)->speculative_always_null() &&
3463 !too_many_traps(Deoptimization::Reason_speculate_null_assert)) {
3464 inc_sp(1);
3465 b = null_assert(b);
3466 dec_sp(1);
3467 }
3468 c = _gvn.transform( new CmpPNode(b, a) );
3469 }
3470 do_ifnull(btest, c);
3471 break;
3472
3473 case Bytecodes::_if_acmpeq: btest = BoolTest::eq; goto handle_if_acmp;
3474 case Bytecodes::_if_acmpne: btest = BoolTest::ne; goto handle_if_acmp;
3475 handle_if_acmp:
3476 // If this is a backwards branch in the bytecodes, add Safepoint
3477 maybe_add_safepoint(iter().get_dest());
3478 a = cast_to_non_larval(pop());
3479 b = cast_to_non_larval(pop());
3480 do_acmp(btest, b, a);
3481 break;
3482
3483 case Bytecodes::_ifeq: btest = BoolTest::eq; goto handle_ifxx;
3484 case Bytecodes::_ifne: btest = BoolTest::ne; goto handle_ifxx;
3485 case Bytecodes::_iflt: btest = BoolTest::lt; goto handle_ifxx;
3486 case Bytecodes::_ifle: btest = BoolTest::le; goto handle_ifxx;
3487 case Bytecodes::_ifgt: btest = BoolTest::gt; goto handle_ifxx;
3488 case Bytecodes::_ifge: btest = BoolTest::ge; goto handle_ifxx;
3489 handle_ifxx:
3490 // If this is a backwards branch in the bytecodes, add Safepoint
3491 maybe_add_safepoint(iter().get_dest());
3492 a = _gvn.intcon(0);
3493 b = pop();
3494 c = _gvn.transform( new CmpINode(b, a) );
3495 do_if(btest, c);
3496 break;
3497
3498 case Bytecodes::_if_icmpeq: btest = BoolTest::eq; goto handle_if_icmp;
3499 case Bytecodes::_if_icmpne: btest = BoolTest::ne; goto handle_if_icmp;
3500 case Bytecodes::_if_icmplt: btest = BoolTest::lt; goto handle_if_icmp;
3501 case Bytecodes::_if_icmple: btest = BoolTest::le; goto handle_if_icmp;
3502 case Bytecodes::_if_icmpgt: btest = BoolTest::gt; goto handle_if_icmp;
3503 case Bytecodes::_if_icmpge: btest = BoolTest::ge; goto handle_if_icmp;
3504 handle_if_icmp:
3505 // If this is a backwards branch in the bytecodes, add Safepoint
3506 maybe_add_safepoint(iter().get_dest());
3507 a = pop();
3508 b = pop();
3509 c = _gvn.transform( new CmpINode( b, a ) );
3510 do_if(btest, c);
3511 break;
3512
3513 case Bytecodes::_tableswitch:
3514 do_tableswitch();
3515 break;
3516
3517 case Bytecodes::_lookupswitch:
3518 do_lookupswitch();
3519 break;
3520
3521 case Bytecodes::_invokestatic:
3522 case Bytecodes::_invokedynamic:
3523 case Bytecodes::_invokespecial:
3524 case Bytecodes::_invokevirtual:
3525 case Bytecodes::_invokeinterface:
3526 do_call();
3527 break;
3528 case Bytecodes::_checkcast:
3529 do_checkcast();
3530 break;
3531 case Bytecodes::_instanceof:
3532 do_instanceof();
3533 break;
3534 case Bytecodes::_anewarray:
3535 do_newarray();
3536 break;
3537 case Bytecodes::_newarray:
3538 do_newarray((BasicType)iter().get_index());
3539 break;
3540 case Bytecodes::_multianewarray:
3541 do_multianewarray();
3542 break;
3543 case Bytecodes::_new:
3544 do_new();
3545 break;
3546
3547 case Bytecodes::_jsr:
3548 case Bytecodes::_jsr_w:
3549 do_jsr();
3550 break;
3551
3552 case Bytecodes::_ret:
3553 do_ret();
3554 break;
3555
3556
3557 case Bytecodes::_monitorenter:
3558 do_monitor_enter();
3559 break;
3560
3561 case Bytecodes::_monitorexit:
3562 do_monitor_exit();
3563 break;
3564
3565 case Bytecodes::_breakpoint:
3566 // Breakpoint set concurrently to compile
3567 // %%% use an uncommon trap?
3568 C->record_failure("breakpoint in method");
3569 return;
3570
3571 default:
3572 #ifndef PRODUCT
3573 map()->dump(99);
3574 #endif
3575 tty->print("\nUnhandled bytecode %s\n", Bytecodes::name(bc()) );
3576 ShouldNotReachHere();
3577 }
3578
3579 #ifndef PRODUCT
3580 if (failing()) { return; }
3581 constexpr int perBytecode = 6;
3582 if (C->should_print_igv(perBytecode)) {
3583 IdealGraphPrinter* printer = C->igv_printer();
3584 char buffer[256];
3585 jio_snprintf(buffer, sizeof(buffer), "Bytecode %d: %s", bci(), Bytecodes::name(bc()));
3586 bool old = printer->traverse_outs();
3587 printer->set_traverse_outs(true);
3588 printer->set_parse(this);
3589 printer->print_graph(buffer);
3590 printer->set_traverse_outs(old);
3591 printer->set_parse(nullptr);
3592 }
3593 #endif
3594 }