Bug Summary

File:jdk/src/hotspot/share/c1/c1_Optimizer.cpp
Warning:line 220, column 3
Value stored to 'cur_end' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name c1_Optimizer.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -mthread-model posix -fno-delete-null-pointer-checks -mframe-pointer=all -relaxed-aliasing -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/hotspot/variant-server/libjvm/objs/precompiled -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D _GNU_SOURCE -D _REENTRANT -D LIBC=gnu -D LINUX -D VM_LITTLE_ENDIAN -D _LP64=1 -D ASSERT -D CHECK_UNHANDLED_OOPS -D TARGET_ARCH_x86 -D INCLUDE_SUFFIX_OS=_linux -D INCLUDE_SUFFIX_CPU=_x86 -D INCLUDE_SUFFIX_COMPILER=_gcc -D TARGET_COMPILER_gcc -D AMD64 -D HOTSPOT_LIB_ARCH="amd64" -D COMPILER1 -D COMPILER2 -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/hotspot/variant-server/gensrc/adfiles -I /home/daniel/Projects/java/jdk/src/hotspot/share -I /home/daniel/Projects/java/jdk/src/hotspot/os/linux -I /home/daniel/Projects/java/jdk/src/hotspot/os/posix -I /home/daniel/Projects/java/jdk/src/hotspot/cpu/x86 -I /home/daniel/Projects/java/jdk/src/hotspot/os_cpu/linux_x86 -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/hotspot/variant-server/gensrc -I /home/daniel/Projects/java/jdk/src/hotspot/share/precompiled -I /home/daniel/Projects/java/jdk/src/hotspot/share/include -I /home/daniel/Projects/java/jdk/src/hotspot/os/posix/include -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/support/modules_include/java.base -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/support/modules_include/java.base/linux -I /home/daniel/Projects/java/jdk/src/java.base/share/native/libjimage -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/hotspot/variant-server/gensrc/adfiles -I /home/daniel/Projects/java/jdk/src/hotspot/share -I /home/daniel/Projects/java/jdk/src/hotspot/os/linux -I /home/daniel/Projects/java/jdk/src/hotspot/os/posix -I /home/daniel/Projects/java/jdk/src/hotspot/cpu/x86 -I /home/daniel/Projects/java/jdk/src/hotspot/os_cpu/linux_x86 -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/hotspot/variant-server/gensrc -D _FORTIFY_SOURCE=2 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/x86_64-linux-gnu/c++/7.5.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/x86_64-linux-gnu/c++/7.5.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Wno-format-zero-length -Wno-unused-parameter -Wno-unused -Wno-parentheses -Wno-comment -Wno-unknown-pragmas -Wno-address -Wno-delete-non-virtual-dtor -Wno-char-subscripts -Wno-array-bounds -Wno-int-in-bool-context -Wno-ignored-qualifiers -Wno-missing-field-initializers -Wno-implicit-fallthrough -Wno-empty-body -Wno-strict-overflow -Wno-sequence-point -Wno-maybe-uninitialized -Wno-misleading-indentation -Wno-cast-function-type -Wno-shift-negative-value -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /home/daniel/Projects/java/jdk/make/hotspot -ferror-limit 19 -fmessage-length 0 -fvisibility hidden -stack-protector 1 -fno-rtti -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -o /home/daniel/Projects/java/scan/2021-12-21-193737-8510-1 -x c++ /home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp
1/*
2 * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25#include "precompiled.hpp"
26#include "c1/c1_Canonicalizer.hpp"
27#include "c1/c1_Optimizer.hpp"
28#include "c1/c1_ValueMap.hpp"
29#include "c1/c1_ValueSet.hpp"
30#include "c1/c1_ValueStack.hpp"
31#include "memory/resourceArea.hpp"
32#include "utilities/bitMap.inline.hpp"
33#include "compiler/compileLog.hpp"
34
35typedef GrowableArray<ValueSet*> ValueSetList;
36
37Optimizer::Optimizer(IR* ir) {
38 assert(ir->is_valid(), "IR must be valid")do { if (!(ir->is_valid())) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 38, "assert(" "ir->is_valid()" ") failed", "IR must be valid"
); ::breakpoint(); } } while (0)
;
39 _ir = ir;
40}
41
42class CE_Eliminator: public BlockClosure {
43 private:
44 IR* _hir;
45 int _cee_count; // the number of CEs successfully eliminated
46 int _ifop_count; // the number of IfOps successfully simplified
47 int _has_substitution;
48
49 public:
50 CE_Eliminator(IR* hir) : _hir(hir), _cee_count(0), _ifop_count(0) {
51 _has_substitution = false;
52 _hir->iterate_preorder(this);
53 if (_has_substitution) {
54 // substituted some ifops/phis, so resolve the substitution
55 SubstitutionResolver sr(_hir);
56 }
57
58 CompileLog* log = _hir->compilation()->log();
59 if (log != NULL__null)
60 log->set_context("optimize name='cee'");
61 }
62
63 ~CE_Eliminator() {
64 CompileLog* log = _hir->compilation()->log();
65 if (log != NULL__null)
66 log->clear_context(); // skip marker if nothing was printed
67 }
68
69 int cee_count() const { return _cee_count; }
70 int ifop_count() const { return _ifop_count; }
71
72 void adjust_exception_edges(BlockBegin* block, BlockBegin* sux) {
73 int e = sux->number_of_exception_handlers();
74 for (int i = 0; i < e; i++) {
75 BlockBegin* xhandler = sux->exception_handler_at(i);
76 block->add_exception_handler(xhandler);
77
78 assert(xhandler->is_predecessor(sux), "missing predecessor")do { if (!(xhandler->is_predecessor(sux))) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 78, "assert(" "xhandler->is_predecessor(sux)" ") failed"
, "missing predecessor"); ::breakpoint(); } } while (0)
;
79 if (sux->number_of_preds() == 0) {
80 // sux is disconnected from graph so disconnect from exception handlers
81 xhandler->remove_predecessor(sux);
82 }
83 if (!xhandler->is_predecessor(block)) {
84 xhandler->add_predecessor(block);
85 }
86 }
87 }
88
89 virtual void block_do(BlockBegin* block);
90
91 private:
92 Value make_ifop(Value x, Instruction::Condition cond, Value y, Value tval, Value fval);
93};
94
95void CE_Eliminator::block_do(BlockBegin* block) {
96 // 1) find conditional expression
97 // check if block ends with an If
98 If* if_ = block->end()->as_If();
99 if (if_ == NULL__null) return;
100
101 // check if If works on int or object types
102 // (we cannot handle If's working on long, float or doubles yet,
103 // since IfOp doesn't support them - these If's show up if cmp
104 // operations followed by If's are eliminated)
105 ValueType* if_type = if_->x()->type();
106 if (!if_type->is_int() && !if_type->is_object()) return;
107
108 BlockBegin* t_block = if_->tsux();
109 BlockBegin* f_block = if_->fsux();
110 Instruction* t_cur = t_block->next();
111 Instruction* f_cur = f_block->next();
112
113 // one Constant may be present between BlockBegin and BlockEnd
114 Value t_const = NULL__null;
115 Value f_const = NULL__null;
116 if (t_cur->as_Constant() != NULL__null && !t_cur->can_trap()) {
117 t_const = t_cur;
118 t_cur = t_cur->next();
119 }
120 if (f_cur->as_Constant() != NULL__null && !f_cur->can_trap()) {
121 f_const = f_cur;
122 f_cur = f_cur->next();
123 }
124
125 // check if both branches end with a goto
126 Goto* t_goto = t_cur->as_Goto();
127 if (t_goto == NULL__null) return;
128 Goto* f_goto = f_cur->as_Goto();
129 if (f_goto == NULL__null) return;
130
131 // check if both gotos merge into the same block
132 BlockBegin* sux = t_goto->default_sux();
133 if (sux != f_goto->default_sux()) return;
134
135 // check if at least one word was pushed on sux_state
136 // inlining depths must match
137 ValueStack* if_state = if_->state();
138 ValueStack* sux_state = sux->state();
139 if (if_state->scope()->level() > sux_state->scope()->level()) {
140 while (sux_state->scope() != if_state->scope()) {
141 if_state = if_state->caller_state();
142 assert(if_state != NULL, "states do not match up")do { if (!(if_state != __null)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 142, "assert(" "if_state != __null" ") failed", "states do not match up"
); ::breakpoint(); } } while (0)
;
143 }
144 } else if (if_state->scope()->level() < sux_state->scope()->level()) {
145 while (sux_state->scope() != if_state->scope()) {
146 sux_state = sux_state->caller_state();
147 assert(sux_state != NULL, "states do not match up")do { if (!(sux_state != __null)) { (*g_assert_poison) = 'X';;
report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 147, "assert(" "sux_state != __null" ") failed", "states do not match up"
); ::breakpoint(); } } while (0)
;
148 }
149 }
150
151 if (sux_state->stack_size() <= if_state->stack_size()) return;
152
153 // check if phi function is present at end of successor stack and that
154 // only this phi was pushed on the stack
155 Value sux_phi = sux_state->stack_at(if_state->stack_size());
156 if (sux_phi == NULL__null || sux_phi->as_Phi() == NULL__null || sux_phi->as_Phi()->block() != sux) return;
157 if (sux_phi->type()->size() != sux_state->stack_size() - if_state->stack_size()) return;
158
159 // get the values that were pushed in the true- and false-branch
160 Value t_value = t_goto->state()->stack_at(if_state->stack_size());
161 Value f_value = f_goto->state()->stack_at(if_state->stack_size());
162
163 // backend does not support floats
164 assert(t_value->type()->base() == f_value->type()->base(), "incompatible types")do { if (!(t_value->type()->base() == f_value->type(
)->base())) { (*g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 164, "assert(" "t_value->type()->base() == f_value->type()->base()"
") failed", "incompatible types"); ::breakpoint(); } } while
(0)
;
165 if (t_value->type()->is_float_kind()) return;
166
167 // check that successor has no other phi functions but sux_phi
168 // this can happen when t_block or f_block contained additonal stores to local variables
169 // that are no longer represented by explicit instructions
170 for_each_phi_fun(sux, phi,{ int cur_index; ValueStack* cur_state = sux->state(); Value
value; { int temp__172 = cur_state->stack_size(); for (cur_index
= 0; cur_index < temp__172 && (value = cur_state->
stack_at(cur_index), true); cur_index += value->type()->
size()) { Phi* phi = value->as_Phi(); if (phi != __null &&
phi->block() == sux) { if (phi != sux_phi) return;; } } }
{ int temp__172 = cur_state->locals_size(); for (cur_index
= 0; cur_index < temp__172 && (value = cur_state->
local_at(cur_index), true); cur_index += (value == __null || value
->type()->is_illegal() ? 1 : value->type()->size(
))) if (value != __null) { Phi* phi = value->as_Phi(); if (
phi != __null && phi->block() == sux) { if (phi !=
sux_phi) return;; } } } }
171 if (phi != sux_phi) return;{ int cur_index; ValueStack* cur_state = sux->state(); Value
value; { int temp__172 = cur_state->stack_size(); for (cur_index
= 0; cur_index < temp__172 && (value = cur_state->
stack_at(cur_index), true); cur_index += value->type()->
size()) { Phi* phi = value->as_Phi(); if (phi != __null &&
phi->block() == sux) { if (phi != sux_phi) return;; } } }
{ int temp__172 = cur_state->locals_size(); for (cur_index
= 0; cur_index < temp__172 && (value = cur_state->
local_at(cur_index), true); cur_index += (value == __null || value
->type()->is_illegal() ? 1 : value->type()->size(
))) if (value != __null) { Phi* phi = value->as_Phi(); if (
phi != __null && phi->block() == sux) { if (phi !=
sux_phi) return;; } } } }
172 ){ int cur_index; ValueStack* cur_state = sux->state(); Value
value; { int temp__172 = cur_state->stack_size(); for (cur_index
= 0; cur_index < temp__172 && (value = cur_state->
stack_at(cur_index), true); cur_index += value->type()->
size()) { Phi* phi = value->as_Phi(); if (phi != __null &&
phi->block() == sux) { if (phi != sux_phi) return;; } } }
{ int temp__172 = cur_state->locals_size(); for (cur_index
= 0; cur_index < temp__172 && (value = cur_state->
local_at(cur_index), true); cur_index += (value == __null || value
->type()->is_illegal() ? 1 : value->type()->size(
))) if (value != __null) { Phi* phi = value->as_Phi(); if (
phi != __null && phi->block() == sux) { if (phi !=
sux_phi) return;; } } } }
;
173 // true and false blocks can't have phis
174 for_each_phi_fun(t_block, phi, return; ){ int cur_index; ValueStack* cur_state = t_block->state();
Value value; { int temp__174 = cur_state->stack_size(); for
(cur_index = 0; cur_index < temp__174 && (value =
cur_state->stack_at(cur_index), true); cur_index += value
->type()->size()) { Phi* phi = value->as_Phi(); if (
phi != __null && phi->block() == t_block) { return
;; } } } { int temp__174 = cur_state->locals_size(); for (
cur_index = 0; cur_index < temp__174 && (value = cur_state
->local_at(cur_index), true); cur_index += (value == __null
|| value->type()->is_illegal() ? 1 : value->type()->
size())) if (value != __null) { Phi* phi = value->as_Phi()
; if (phi != __null && phi->block() == t_block) { return
;; } } } }
;
175 for_each_phi_fun(f_block, phi, return; ){ int cur_index; ValueStack* cur_state = f_block->state();
Value value; { int temp__175 = cur_state->stack_size(); for
(cur_index = 0; cur_index < temp__175 && (value =
cur_state->stack_at(cur_index), true); cur_index += value
->type()->size()) { Phi* phi = value->as_Phi(); if (
phi != __null && phi->block() == f_block) { return
;; } } } { int temp__175 = cur_state->locals_size(); for (
cur_index = 0; cur_index < temp__175 && (value = cur_state
->local_at(cur_index), true); cur_index += (value == __null
|| value->type()->is_illegal() ? 1 : value->type()->
size())) if (value != __null) { Phi* phi = value->as_Phi()
; if (phi != __null && phi->block() == f_block) { return
;; } } } }
;
176
177 // Only replace safepoint gotos if state_before information is available (if is a safepoint)
178 bool is_safepoint = if_->is_safepoint();
179 if (!is_safepoint && (t_goto->is_safepoint() || f_goto->is_safepoint())) {
180 return;
181 }
182
183 // 2) substitute conditional expression
184 // with an IfOp followed by a Goto
185 // cut if_ away and get node before
186 Instruction* cur_end = if_->prev();
187
188 // append constants of true- and false-block if necessary
189 // clone constants because original block must not be destroyed
190 assert((t_value != f_const && f_value != t_const) || t_const == f_const, "mismatch")do { if (!((t_value != f_const && f_value != t_const)
|| t_const == f_const)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 190, "assert(" "(t_value != f_const && f_value != t_const) || t_const == f_const"
") failed", "mismatch"); ::breakpoint(); } } while (0)
;
191 if (t_value == t_const) {
192 t_value = new Constant(t_const->type());
193 NOT_PRODUCT(t_value->set_printable_bci(if_->printable_bci()))t_value->set_printable_bci(if_->printable_bci());
194 cur_end = cur_end->set_next(t_value);
195 }
196 if (f_value == f_const) {
197 f_value = new Constant(f_const->type());
198 NOT_PRODUCT(f_value->set_printable_bci(if_->printable_bci()))f_value->set_printable_bci(if_->printable_bci());
199 cur_end = cur_end->set_next(f_value);
200 }
201
202 Value result = make_ifop(if_->x(), if_->cond(), if_->y(), t_value, f_value);
203 assert(result != NULL, "make_ifop must return a non-null instruction")do { if (!(result != __null)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 203, "assert(" "result != __null" ") failed", "make_ifop must return a non-null instruction"
); ::breakpoint(); } } while (0)
;
204 if (!result->is_linked() && result->can_be_linked()) {
205 NOT_PRODUCT(result->set_printable_bci(if_->printable_bci()))result->set_printable_bci(if_->printable_bci());
206 cur_end = cur_end->set_next(result);
207 }
208
209 // append Goto to successor
210 ValueStack* state_before = if_->state_before();
211 Goto* goto_ = new Goto(sux, state_before, is_safepoint);
212
213 // prepare state for Goto
214 ValueStack* goto_state = if_state;
215 goto_state = goto_state->copy(ValueStack::StateAfter, goto_state->bci());
216 goto_state->push(result->type(), result);
217 assert(goto_state->is_same(sux_state), "states must match now")do { if (!(goto_state->is_same(sux_state))) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 217, "assert(" "goto_state->is_same(sux_state)" ") failed"
, "states must match now"); ::breakpoint(); } } while (0)
;
218 goto_->set_state(goto_state);
219
220 cur_end = cur_end->set_next(goto_, goto_state->bci());
Value stored to 'cur_end' is never read
221
222 // Adjust control flow graph
223 BlockBegin::disconnect_edge(block, t_block);
224 BlockBegin::disconnect_edge(block, f_block);
225 if (t_block->number_of_preds() == 0) {
226 BlockBegin::disconnect_edge(t_block, sux);
227 }
228 adjust_exception_edges(block, t_block);
229 if (f_block->number_of_preds() == 0) {
230 BlockBegin::disconnect_edge(f_block, sux);
231 }
232 adjust_exception_edges(block, f_block);
233
234 // update block end
235 block->set_end(goto_);
236
237 // substitute the phi if possible
238 if (sux_phi->as_Phi()->operand_count() == 1) {
239 assert(sux_phi->as_Phi()->operand_at(0) == result, "screwed up phi")do { if (!(sux_phi->as_Phi()->operand_at(0) == result))
{ (*g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 239, "assert(" "sux_phi->as_Phi()->operand_at(0) == result"
") failed", "screwed up phi"); ::breakpoint(); } } while (0)
;
240 sux_phi->set_subst(result);
241 _has_substitution = true;
242 }
243
244 // 3) successfully eliminated a conditional expression
245 _cee_count++;
246 if (PrintCEE) {
247 tty->print_cr("%d. CEE in B%d (B%d B%d)", cee_count(), block->block_id(), t_block->block_id(), f_block->block_id());
248 tty->print_cr("%d. IfOp in B%d", ifop_count(), block->block_id());
249 }
250
251 _hir->verify();
252}
253
254Value CE_Eliminator::make_ifop(Value x, Instruction::Condition cond, Value y, Value tval, Value fval) {
255 if (!OptimizeIfOps) {
256 return new IfOp(x, cond, y, tval, fval);
257 }
258
259 tval = tval->subst();
260 fval = fval->subst();
261 if (tval == fval) {
262 _ifop_count++;
263 return tval;
264 }
265
266 x = x->subst();
267 y = y->subst();
268
269 Constant* y_const = y->as_Constant();
270 if (y_const != NULL__null) {
271 IfOp* x_ifop = x->as_IfOp();
272 if (x_ifop != NULL__null) { // x is an ifop, y is a constant
273 Constant* x_tval_const = x_ifop->tval()->subst()->as_Constant();
274 Constant* x_fval_const = x_ifop->fval()->subst()->as_Constant();
275
276 if (x_tval_const != NULL__null && x_fval_const != NULL__null) {
277 Instruction::Condition x_ifop_cond = x_ifop->cond();
278
279 Constant::CompareResult t_compare_res = x_tval_const->compare(cond, y_const);
280 Constant::CompareResult f_compare_res = x_fval_const->compare(cond, y_const);
281
282 // not_comparable here is a valid return in case we're comparing unloaded oop constants
283 if (t_compare_res != Constant::not_comparable && f_compare_res != Constant::not_comparable) {
284 Value new_tval = t_compare_res == Constant::cond_true ? tval : fval;
285 Value new_fval = f_compare_res == Constant::cond_true ? tval : fval;
286
287 _ifop_count++;
288 if (new_tval == new_fval) {
289 return new_tval;
290 } else {
291 return new IfOp(x_ifop->x(), x_ifop_cond, x_ifop->y(), new_tval, new_fval);
292 }
293 }
294 }
295 } else {
296 Constant* x_const = x->as_Constant();
297 if (x_const != NULL__null) { // x and y are constants
298 Constant::CompareResult x_compare_res = x_const->compare(cond, y_const);
299 // not_comparable here is a valid return in case we're comparing unloaded oop constants
300 if (x_compare_res != Constant::not_comparable) {
301 _ifop_count++;
302 return x_compare_res == Constant::cond_true ? tval : fval;
303 }
304 }
305 }
306 }
307 return new IfOp(x, cond, y, tval, fval);
308}
309
310void Optimizer::eliminate_conditional_expressions() {
311 // find conditional expressions & replace them with IfOps
312 CE_Eliminator ce(ir());
313}
314
315void disconnect_from_graph(BlockBegin* block) {
316 for (int p = 0; p < block->number_of_preds(); p++) {
317 BlockBegin* pred = block->pred_at(p);
318 int idx;
319 while ((idx = pred->end()->find_sux(block)) >= 0) {
320 pred->end()->remove_sux_at(idx);
321 }
322 }
323 for (int s = 0; s < block->number_of_sux(); s++) {
324 block->sux_at(s)->remove_predecessor(block);
325 }
326}
327
328class BlockMerger: public BlockClosure {
329 private:
330 IR* _hir;
331 int _merge_count; // the number of block pairs successfully merged
332
333 public:
334 BlockMerger(IR* hir)
335 : _hir(hir)
336 , _merge_count(0)
337 {
338 _hir->iterate_preorder(this);
339 CompileLog* log = _hir->compilation()->log();
340 if (log != NULL__null)
341 log->set_context("optimize name='eliminate_blocks'");
342 }
343
344 ~BlockMerger() {
345 CompileLog* log = _hir->compilation()->log();
346 if (log != NULL__null)
347 log->clear_context(); // skip marker if nothing was printed
348 }
349
350 bool try_merge(BlockBegin* block) {
351 BlockEnd* end = block->end();
352 if (end->as_Goto() == NULL__null) return false;
353
354 assert(end->number_of_sux() == 1, "end must have exactly one successor")do { if (!(end->number_of_sux() == 1)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 354, "assert(" "end->number_of_sux() == 1" ") failed", "end must have exactly one successor"
); ::breakpoint(); } } while (0)
;
355 // Note: It would be sufficient to check for the number of successors (= 1)
356 // in order to decide if this block can be merged potentially. That
357 // would then also include switch statements w/ only a default case.
358 // However, in that case we would need to make sure the switch tag
359 // expression is executed if it can produce observable side effects.
360 // We should probably have the canonicalizer simplifying such switch
361 // statements and then we are sure we don't miss these merge opportunities
362 // here (was bug - gri 7/7/99).
363 BlockBegin* sux = end->default_sux();
364 if (sux->number_of_preds() != 1 || sux->is_entry_block() || end->is_safepoint()) return false;
365 // merge the two blocks
366
367#ifdef ASSERT1
368 // verify that state at the end of block and at the beginning of sux are equal
369 // no phi functions must be present at beginning of sux
370 ValueStack* sux_state = sux->state();
371 ValueStack* end_state = end->state();
372
373 assert(end_state->scope() == sux_state->scope(), "scopes must match")do { if (!(end_state->scope() == sux_state->scope())) {
(*g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 373, "assert(" "end_state->scope() == sux_state->scope()"
") failed", "scopes must match"); ::breakpoint(); } } while (
0)
;
374 assert(end_state->stack_size() == sux_state->stack_size(), "stack not equal")do { if (!(end_state->stack_size() == sux_state->stack_size
())) { (*g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 374, "assert(" "end_state->stack_size() == sux_state->stack_size()"
") failed", "stack not equal"); ::breakpoint(); } } while (0
)
;
375 assert(end_state->locals_size() == sux_state->locals_size(), "locals not equal")do { if (!(end_state->locals_size() == sux_state->locals_size
())) { (*g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 375, "assert(" "end_state->locals_size() == sux_state->locals_size()"
") failed", "locals not equal"); ::breakpoint(); } } while (
0)
;
376
377 int index;
378 Value sux_value;
379 for_each_stack_value(sux_state, index, sux_value)int temp__379 = sux_state->stack_size(); for (index = 0; index
< temp__379 && (sux_value = sux_state->stack_at
(index), true); index += sux_value->type()->size())
{
380 assert(sux_value == end_state->stack_at(index), "stack not equal")do { if (!(sux_value == end_state->stack_at(index))) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 380, "assert(" "sux_value == end_state->stack_at(index)"
") failed", "stack not equal"); ::breakpoint(); } } while (0
)
;
381 }
382 for_each_local_value(sux_state, index, sux_value)int temp__382 = sux_state->locals_size(); for (index = 0; index
< temp__382 && (sux_value = sux_state->local_at
(index), true); index += (sux_value == __null || sux_value->
type()->is_illegal() ? 1 : sux_value->type()->size()
)) if (sux_value != __null)
{
383 Phi* sux_phi = sux_value->as_Phi();
384 if (sux_phi != NULL__null && sux_phi->is_illegal()) continue;
385 assert(sux_value == end_state->local_at(index), "locals not equal")do { if (!(sux_value == end_state->local_at(index))) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 385, "assert(" "sux_value == end_state->local_at(index)"
") failed", "locals not equal"); ::breakpoint(); } } while (
0)
;
386 }
387 assert(sux_state->caller_state() == end_state->caller_state(), "caller not equal")do { if (!(sux_state->caller_state() == end_state->caller_state
())) { (*g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 387, "assert(" "sux_state->caller_state() == end_state->caller_state()"
") failed", "caller not equal"); ::breakpoint(); } } while (
0)
;
388#endif
389
390 // find instruction before end & append first instruction of sux block
391 Instruction* prev = end->prev();
392 Instruction* next = sux->next();
393 assert(prev->as_BlockEnd() == NULL, "must not be a BlockEnd")do { if (!(prev->as_BlockEnd() == __null)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 393, "assert(" "prev->as_BlockEnd() == __null" ") failed"
, "must not be a BlockEnd"); ::breakpoint(); } } while (0)
;
394 prev->set_next(next);
395 prev->fixup_block_pointers();
396
397 // disconnect this block from all other blocks
398 disconnect_from_graph(sux);
399 block->set_end(sux->end());
400
401 // TODO Should this be done in set_end universally?
402 // add exception handlers of deleted block, if any
403 for (int k = 0; k < sux->number_of_exception_handlers(); k++) {
404 BlockBegin* xhandler = sux->exception_handler_at(k);
405 block->add_exception_handler(xhandler);
406
407 // also substitute predecessor of exception handler
408 assert(xhandler->is_predecessor(sux), "missing predecessor")do { if (!(xhandler->is_predecessor(sux))) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 408, "assert(" "xhandler->is_predecessor(sux)" ") failed"
, "missing predecessor"); ::breakpoint(); } } while (0)
;
409 xhandler->remove_predecessor(sux);
410 if (!xhandler->is_predecessor(block)) {
411 xhandler->add_predecessor(block);
412 }
413 }
414
415 // debugging output
416 _merge_count++;
417 if (PrintBlockElimination) {
418 tty->print_cr("%d. merged B%d & B%d (stack size = %d)",
419 _merge_count, block->block_id(), sux->block_id(), sux->state()->stack_size());
420 }
421
422 _hir->verify();
423
424 If* if_ = block->end()->as_If();
425 if (if_) {
426 IfOp* ifop = if_->x()->as_IfOp();
427 Constant* con = if_->y()->as_Constant();
428 bool swapped = false;
429 if (!con || !ifop) {
430 ifop = if_->y()->as_IfOp();
431 con = if_->x()->as_Constant();
432 swapped = true;
433 }
434 if (con && ifop) {
435 Constant* tval = ifop->tval()->as_Constant();
436 Constant* fval = ifop->fval()->as_Constant();
437 if (tval && fval) {
438 // Find the instruction before if_, starting with ifop.
439 // When if_ and ifop are not in the same block, prev
440 // becomes NULL In such (rare) cases it is not
441 // profitable to perform the optimization.
442 Value prev = ifop;
443 while (prev != NULL__null && prev->next() != if_) {
444 prev = prev->next();
445 }
446
447 if (prev != NULL__null) {
448 Instruction::Condition cond = if_->cond();
449 BlockBegin* tsux = if_->tsux();
450 BlockBegin* fsux = if_->fsux();
451 if (swapped) {
452 cond = Instruction::mirror(cond);
453 }
454
455 BlockBegin* tblock = tval->compare(cond, con, tsux, fsux);
456 BlockBegin* fblock = fval->compare(cond, con, tsux, fsux);
457 if (tblock != fblock && !if_->is_safepoint()) {
458 If* newif = new If(ifop->x(), ifop->cond(), false, ifop->y(),
459 tblock, fblock, if_->state_before(), if_->is_safepoint());
460 newif->set_state(if_->state()->copy());
461
462 assert(prev->next() == if_, "must be guaranteed by above search")do { if (!(prev->next() == if_)) { (*g_assert_poison) = 'X'
;; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 462, "assert(" "prev->next() == if_" ") failed", "must be guaranteed by above search"
); ::breakpoint(); } } while (0)
;
463 NOT_PRODUCT(newif->set_printable_bci(if_->printable_bci()))newif->set_printable_bci(if_->printable_bci());
464 prev->set_next(newif);
465 block->set_end(newif);
466
467 _merge_count++;
468 if (PrintBlockElimination) {
469 tty->print_cr("%d. replaced If and IfOp at end of B%d with single If", _merge_count, block->block_id());
470 }
471
472 _hir->verify();
473 }
474 }
475 }
476 }
477 }
478
479 return true;
480 }
481
482 virtual void block_do(BlockBegin* block) {
483 // repeat since the same block may merge again
484 while (try_merge(block)) ;
485 }
486};
487
488
489void Optimizer::eliminate_blocks() {
490 // merge blocks if possible
491 BlockMerger bm(ir());
492}
493
494
495class NullCheckEliminator;
496class NullCheckVisitor: public InstructionVisitor {
497private:
498 NullCheckEliminator* _nce;
499 NullCheckEliminator* nce() { return _nce; }
500
501public:
502 NullCheckVisitor() {}
503
504 void set_eliminator(NullCheckEliminator* nce) { _nce = nce; }
505
506 void do_Phi (Phi* x);
507 void do_Local (Local* x);
508 void do_Constant (Constant* x);
509 void do_LoadField (LoadField* x);
510 void do_StoreField (StoreField* x);
511 void do_ArrayLength (ArrayLength* x);
512 void do_LoadIndexed (LoadIndexed* x);
513 void do_StoreIndexed (StoreIndexed* x);
514 void do_NegateOp (NegateOp* x);
515 void do_ArithmeticOp (ArithmeticOp* x);
516 void do_ShiftOp (ShiftOp* x);
517 void do_LogicOp (LogicOp* x);
518 void do_CompareOp (CompareOp* x);
519 void do_IfOp (IfOp* x);
520 void do_Convert (Convert* x);
521 void do_NullCheck (NullCheck* x);
522 void do_TypeCast (TypeCast* x);
523 void do_Invoke (Invoke* x);
524 void do_NewInstance (NewInstance* x);
525 void do_NewTypeArray (NewTypeArray* x);
526 void do_NewObjectArray (NewObjectArray* x);
527 void do_NewMultiArray (NewMultiArray* x);
528 void do_CheckCast (CheckCast* x);
529 void do_InstanceOf (InstanceOf* x);
530 void do_MonitorEnter (MonitorEnter* x);
531 void do_MonitorExit (MonitorExit* x);
532 void do_Intrinsic (Intrinsic* x);
533 void do_BlockBegin (BlockBegin* x);
534 void do_Goto (Goto* x);
535 void do_If (If* x);
536 void do_TableSwitch (TableSwitch* x);
537 void do_LookupSwitch (LookupSwitch* x);
538 void do_Return (Return* x);
539 void do_Throw (Throw* x);
540 void do_Base (Base* x);
541 void do_OsrEntry (OsrEntry* x);
542 void do_ExceptionObject(ExceptionObject* x);
543 void do_RoundFP (RoundFP* x);
544 void do_UnsafeGet (UnsafeGet* x);
545 void do_UnsafePut (UnsafePut* x);
546 void do_UnsafeGetAndSet(UnsafeGetAndSet* x);
547 void do_ProfileCall (ProfileCall* x);
548 void do_ProfileReturnType (ProfileReturnType* x);
549 void do_ProfileInvoke (ProfileInvoke* x);
550 void do_RuntimeCall (RuntimeCall* x);
551 void do_MemBar (MemBar* x);
552 void do_RangeCheckPredicate(RangeCheckPredicate* x);
553#ifdef ASSERT1
554 void do_Assert (Assert* x);
555#endif
556};
557
558
559// Because of a static contained within (for the purpose of iteration
560// over instructions), it is only valid to have one of these active at
561// a time
562class NullCheckEliminator: public ValueVisitor {
563 private:
564 Optimizer* _opt;
565
566 ValueSet* _visitable_instructions; // Visit each instruction only once per basic block
567 BlockList* _work_list; // Basic blocks to visit
568
569 bool visitable(Value x) {
570 assert(_visitable_instructions != NULL, "check")do { if (!(_visitable_instructions != __null)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 570, "assert(" "_visitable_instructions != __null" ") failed"
, "check"); ::breakpoint(); } } while (0)
;
571 return _visitable_instructions->contains(x);
572 }
573 void mark_visited(Value x) {
574 assert(_visitable_instructions != NULL, "check")do { if (!(_visitable_instructions != __null)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 574, "assert(" "_visitable_instructions != __null" ") failed"
, "check"); ::breakpoint(); } } while (0)
;
575 _visitable_instructions->remove(x);
576 }
577 void mark_visitable(Value x) {
578 assert(_visitable_instructions != NULL, "check")do { if (!(_visitable_instructions != __null)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 578, "assert(" "_visitable_instructions != __null" ") failed"
, "check"); ::breakpoint(); } } while (0)
;
579 _visitable_instructions->put(x);
580 }
581 void clear_visitable_state() {
582 assert(_visitable_instructions != NULL, "check")do { if (!(_visitable_instructions != __null)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 582, "assert(" "_visitable_instructions != __null" ") failed"
, "check"); ::breakpoint(); } } while (0)
;
583 _visitable_instructions->clear();
584 }
585
586 ValueSet* _set; // current state, propagated to subsequent BlockBegins
587 ValueSetList _block_states; // BlockBegin null-check states for all processed blocks
588 NullCheckVisitor _visitor;
589 NullCheck* _last_explicit_null_check;
590
591 bool set_contains(Value x) { assert(_set != NULL, "check")do { if (!(_set != __null)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 591, "assert(" "_set != __null" ") failed", "check"); ::breakpoint
(); } } while (0)
; return _set->contains(x); }
592 void set_put (Value x) { assert(_set != NULL, "check")do { if (!(_set != __null)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 592, "assert(" "_set != __null" ") failed", "check"); ::breakpoint
(); } } while (0)
; _set->put(x); }
593 void set_remove (Value x) { assert(_set != NULL, "check")do { if (!(_set != __null)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 593, "assert(" "_set != __null" ") failed", "check"); ::breakpoint
(); } } while (0)
; _set->remove(x); }
594
595 BlockList* work_list() { return _work_list; }
596
597 void iterate_all();
598 void iterate_one(BlockBegin* block);
599
600 ValueSet* state() { return _set; }
601 void set_state_from (ValueSet* state) { _set->set_from(state); }
602 ValueSet* state_for (BlockBegin* block) { return _block_states.at(block->block_id()); }
603 void set_state_for (BlockBegin* block, ValueSet* stack) { _block_states.at_put(block->block_id(), stack); }
604 // Returns true if caused a change in the block's state.
605 bool merge_state_for(BlockBegin* block,
606 ValueSet* incoming_state);
607
608 public:
609 // constructor
610 NullCheckEliminator(Optimizer* opt)
611 : _opt(opt)
612 , _work_list(new BlockList())
613 , _set(new ValueSet())
614 , _block_states(BlockBegin::number_of_blocks(), BlockBegin::number_of_blocks(), NULL__null)
615 , _last_explicit_null_check(NULL__null) {
616 _visitable_instructions = new ValueSet();
617 _visitor.set_eliminator(this);
618 CompileLog* log = _opt->ir()->compilation()->log();
619 if (log != NULL__null)
620 log->set_context("optimize name='null_check_elimination'");
621 }
622
623 ~NullCheckEliminator() {
624 CompileLog* log = _opt->ir()->compilation()->log();
625 if (log != NULL__null)
626 log->clear_context(); // skip marker if nothing was printed
627 }
628
629 Optimizer* opt() { return _opt; }
630 IR* ir () { return opt()->ir(); }
631
632 // Process a graph
633 void iterate(BlockBegin* root);
634
635 void visit(Value* f);
636
637 // In some situations (like NullCheck(x); getfield(x)) the debug
638 // information from the explicit NullCheck can be used to populate
639 // the getfield, even if the two instructions are in different
640 // scopes; this allows implicit null checks to be used but the
641 // correct exception information to be generated. We must clear the
642 // last-traversed NullCheck when we reach a potentially-exception-
643 // throwing instruction, as well as in some other cases.
644 void set_last_explicit_null_check(NullCheck* check) { _last_explicit_null_check = check; }
645 NullCheck* last_explicit_null_check() { return _last_explicit_null_check; }
646 Value last_explicit_null_check_obj() { return (_last_explicit_null_check
647 ? _last_explicit_null_check->obj()
648 : NULL__null); }
649 NullCheck* consume_last_explicit_null_check() {
650 _last_explicit_null_check->unpin(Instruction::PinExplicitNullCheck);
651 _last_explicit_null_check->set_can_trap(false);
652 return _last_explicit_null_check;
653 }
654 void clear_last_explicit_null_check() { _last_explicit_null_check = NULL__null; }
655
656 // Handlers for relevant instructions
657 // (separated out from NullCheckVisitor for clarity)
658
659 // The basic contract is that these must leave the instruction in
660 // the desired state; must not assume anything about the state of
661 // the instruction. We make multiple passes over some basic blocks
662 // and the last pass is the only one whose result is valid.
663 void handle_AccessField (AccessField* x);
664 void handle_ArrayLength (ArrayLength* x);
665 void handle_LoadIndexed (LoadIndexed* x);
666 void handle_StoreIndexed (StoreIndexed* x);
667 void handle_NullCheck (NullCheck* x);
668 void handle_Invoke (Invoke* x);
669 void handle_NewInstance (NewInstance* x);
670 void handle_NewArray (NewArray* x);
671 void handle_AccessMonitor (AccessMonitor* x);
672 void handle_Intrinsic (Intrinsic* x);
673 void handle_ExceptionObject (ExceptionObject* x);
674 void handle_Phi (Phi* x);
675 void handle_ProfileCall (ProfileCall* x);
676 void handle_ProfileReturnType (ProfileReturnType* x);
677};
678
679
680// NEEDS_CLEANUP
681// There may be other instructions which need to clear the last
682// explicit null check. Anything across which we can not hoist the
683// debug information for a NullCheck instruction must clear it. It
684// might be safer to pattern match "NullCheck ; {AccessField,
685// ArrayLength, LoadIndexed}" but it is more easily structured this way.
686// Should test to see performance hit of clearing it for all handlers
687// with empty bodies below. If it is negligible then we should leave
688// that in for safety, otherwise should think more about it.
689void NullCheckVisitor::do_Phi (Phi* x) { nce()->handle_Phi(x); }
690void NullCheckVisitor::do_Local (Local* x) {}
691void NullCheckVisitor::do_Constant (Constant* x) { /* FIXME: handle object constants */ }
692void NullCheckVisitor::do_LoadField (LoadField* x) { nce()->handle_AccessField(x); }
693void NullCheckVisitor::do_StoreField (StoreField* x) { nce()->handle_AccessField(x); }
694void NullCheckVisitor::do_ArrayLength (ArrayLength* x) { nce()->handle_ArrayLength(x); }
695void NullCheckVisitor::do_LoadIndexed (LoadIndexed* x) { nce()->handle_LoadIndexed(x); }
696void NullCheckVisitor::do_StoreIndexed (StoreIndexed* x) { nce()->handle_StoreIndexed(x); }
697void NullCheckVisitor::do_NegateOp (NegateOp* x) {}
698void NullCheckVisitor::do_ArithmeticOp (ArithmeticOp* x) { if (x->can_trap()) nce()->clear_last_explicit_null_check(); }
699void NullCheckVisitor::do_ShiftOp (ShiftOp* x) {}
700void NullCheckVisitor::do_LogicOp (LogicOp* x) {}
701void NullCheckVisitor::do_CompareOp (CompareOp* x) {}
702void NullCheckVisitor::do_IfOp (IfOp* x) {}
703void NullCheckVisitor::do_Convert (Convert* x) {}
704void NullCheckVisitor::do_NullCheck (NullCheck* x) { nce()->handle_NullCheck(x); }
705void NullCheckVisitor::do_TypeCast (TypeCast* x) {}
706void NullCheckVisitor::do_Invoke (Invoke* x) { nce()->handle_Invoke(x); }
707void NullCheckVisitor::do_NewInstance (NewInstance* x) { nce()->handle_NewInstance(x); }
708void NullCheckVisitor::do_NewTypeArray (NewTypeArray* x) { nce()->handle_NewArray(x); }
709void NullCheckVisitor::do_NewObjectArray (NewObjectArray* x) { nce()->handle_NewArray(x); }
710void NullCheckVisitor::do_NewMultiArray (NewMultiArray* x) { nce()->handle_NewArray(x); }
711void NullCheckVisitor::do_CheckCast (CheckCast* x) { nce()->clear_last_explicit_null_check(); }
712void NullCheckVisitor::do_InstanceOf (InstanceOf* x) {}
713void NullCheckVisitor::do_MonitorEnter (MonitorEnter* x) { nce()->handle_AccessMonitor(x); }
714void NullCheckVisitor::do_MonitorExit (MonitorExit* x) { nce()->handle_AccessMonitor(x); }
715void NullCheckVisitor::do_Intrinsic (Intrinsic* x) { nce()->handle_Intrinsic(x); }
716void NullCheckVisitor::do_BlockBegin (BlockBegin* x) {}
717void NullCheckVisitor::do_Goto (Goto* x) {}
718void NullCheckVisitor::do_If (If* x) {}
719void NullCheckVisitor::do_TableSwitch (TableSwitch* x) {}
720void NullCheckVisitor::do_LookupSwitch (LookupSwitch* x) {}
721void NullCheckVisitor::do_Return (Return* x) {}
722void NullCheckVisitor::do_Throw (Throw* x) { nce()->clear_last_explicit_null_check(); }
723void NullCheckVisitor::do_Base (Base* x) {}
724void NullCheckVisitor::do_OsrEntry (OsrEntry* x) {}
725void NullCheckVisitor::do_ExceptionObject(ExceptionObject* x) { nce()->handle_ExceptionObject(x); }
726void NullCheckVisitor::do_RoundFP (RoundFP* x) {}
727void NullCheckVisitor::do_UnsafeGet (UnsafeGet* x) {}
728void NullCheckVisitor::do_UnsafePut (UnsafePut* x) {}
729void NullCheckVisitor::do_UnsafeGetAndSet(UnsafeGetAndSet* x) {}
730void NullCheckVisitor::do_ProfileCall (ProfileCall* x) { nce()->clear_last_explicit_null_check();
731 nce()->handle_ProfileCall(x); }
732void NullCheckVisitor::do_ProfileReturnType (ProfileReturnType* x) { nce()->handle_ProfileReturnType(x); }
733void NullCheckVisitor::do_ProfileInvoke (ProfileInvoke* x) {}
734void NullCheckVisitor::do_RuntimeCall (RuntimeCall* x) {}
735void NullCheckVisitor::do_MemBar (MemBar* x) {}
736void NullCheckVisitor::do_RangeCheckPredicate(RangeCheckPredicate* x) {}
737#ifdef ASSERT1
738void NullCheckVisitor::do_Assert (Assert* x) {}
739#endif
740
741void NullCheckEliminator::visit(Value* p) {
742 assert(*p != NULL, "should not find NULL instructions")do { if (!(*p != __null)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 742, "assert(" "*p != __null" ") failed", "should not find NULL instructions"
); ::breakpoint(); } } while (0)
;
743 if (visitable(*p)) {
744 mark_visited(*p);
745 (*p)->visit(&_visitor);
746 }
747}
748
749bool NullCheckEliminator::merge_state_for(BlockBegin* block, ValueSet* incoming_state) {
750 ValueSet* state = state_for(block);
751 if (state == NULL__null) {
752 state = incoming_state->copy();
753 set_state_for(block, state);
754 return true;
755 } else {
756 bool changed = state->set_intersect(incoming_state);
757 if (PrintNullCheckElimination && changed) {
758 tty->print_cr("Block %d's null check state changed", block->block_id());
759 }
760 return changed;
761 }
762}
763
764
765void NullCheckEliminator::iterate_all() {
766 while (work_list()->length() > 0) {
767 iterate_one(work_list()->pop());
768 }
769}
770
771
772void NullCheckEliminator::iterate_one(BlockBegin* block) {
773 clear_visitable_state();
774 // clear out an old explicit null checks
775 set_last_explicit_null_check(NULL__null);
776
777 if (PrintNullCheckElimination) {
778 tty->print_cr(" ...iterating block %d in null check elimination for %s::%s%s",
779 block->block_id(),
780 ir()->method()->holder()->name()->as_utf8(),
781 ir()->method()->name()->as_utf8(),
782 ir()->method()->signature()->as_symbol()->as_utf8());
783 }
784
785 // Create new state if none present (only happens at root)
786 if (state_for(block) == NULL__null) {
787 ValueSet* tmp_state = new ValueSet();
788 set_state_for(block, tmp_state);
789 // Initial state is that local 0 (receiver) is non-null for
790 // non-static methods
791 ValueStack* stack = block->state();
792 IRScope* scope = stack->scope();
793 ciMethod* method = scope->method();
794 if (!method->is_static()) {
795 Local* local0 = stack->local_at(0)->as_Local();
796 assert(local0 != NULL, "must be")do { if (!(local0 != __null)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 796, "assert(" "local0 != __null" ") failed", "must be"); ::
breakpoint(); } } while (0)
;
797 assert(local0->type() == objectType, "invalid type of receiver")do { if (!(local0->type() == objectType)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 797, "assert(" "local0->type() == objectType" ") failed"
, "invalid type of receiver"); ::breakpoint(); } } while (0)
;
798
799 if (local0 != NULL__null) {
800 // Local 0 is used in this scope
801 tmp_state->put(local0);
802 if (PrintNullCheckElimination) {
803 tty->print_cr("Local 0 (value %d) proven non-null upon entry", local0->id());
804 }
805 }
806 }
807 }
808
809 // Must copy block's state to avoid mutating it during iteration
810 // through the block -- otherwise "not-null" states can accidentally
811 // propagate "up" through the block during processing of backward
812 // branches and algorithm is incorrect (and does not converge)
813 set_state_from(state_for(block));
814
815 // allow visiting of Phis belonging to this block
816 for_each_phi_fun(block, phi,{ int cur_index; ValueStack* cur_state = block->state(); Value
value; { int temp__818 = cur_state->stack_size(); for (cur_index
= 0; cur_index < temp__818 && (value = cur_state->
stack_at(cur_index), true); cur_index += value->type()->
size()) { Phi* phi = value->as_Phi(); if (phi != __null &&
phi->block() == block) { mark_visitable(phi);; } } } { int
temp__818 = cur_state->locals_size(); for (cur_index = 0;
cur_index < temp__818 && (value = cur_state->local_at
(cur_index), true); cur_index += (value == __null || value->
type()->is_illegal() ? 1 : value->type()->size())) if
(value != __null) { Phi* phi = value->as_Phi(); if (phi !=
__null && phi->block() == block) { mark_visitable
(phi);; } } } }
817 mark_visitable(phi);{ int cur_index; ValueStack* cur_state = block->state(); Value
value; { int temp__818 = cur_state->stack_size(); for (cur_index
= 0; cur_index < temp__818 && (value = cur_state->
stack_at(cur_index), true); cur_index += value->type()->
size()) { Phi* phi = value->as_Phi(); if (phi != __null &&
phi->block() == block) { mark_visitable(phi);; } } } { int
temp__818 = cur_state->locals_size(); for (cur_index = 0;
cur_index < temp__818 && (value = cur_state->local_at
(cur_index), true); cur_index += (value == __null || value->
type()->is_illegal() ? 1 : value->type()->size())) if
(value != __null) { Phi* phi = value->as_Phi(); if (phi !=
__null && phi->block() == block) { mark_visitable
(phi);; } } } }
818 ){ int cur_index; ValueStack* cur_state = block->state(); Value
value; { int temp__818 = cur_state->stack_size(); for (cur_index
= 0; cur_index < temp__818 && (value = cur_state->
stack_at(cur_index), true); cur_index += value->type()->
size()) { Phi* phi = value->as_Phi(); if (phi != __null &&
phi->block() == block) { mark_visitable(phi);; } } } { int
temp__818 = cur_state->locals_size(); for (cur_index = 0;
cur_index < temp__818 && (value = cur_state->local_at
(cur_index), true); cur_index += (value == __null || value->
type()->is_illegal() ? 1 : value->type()->size())) if
(value != __null) { Phi* phi = value->as_Phi(); if (phi !=
__null && phi->block() == block) { mark_visitable
(phi);; } } } }
;
819
820 BlockEnd* e = block->end();
821 assert(e != NULL, "incomplete graph")do { if (!(e != __null)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/share/c1/c1_Optimizer.cpp"
, 821, "assert(" "e != __null" ") failed", "incomplete graph"
); ::breakpoint(); } } while (0)
;
822 int i;
823
824 // Propagate the state before this block into the exception
825 // handlers. They aren't true successors since we aren't guaranteed
826 // to execute the whole block before executing them. Also putting
827 // them on first seems to help reduce the amount of iteration to
828 // reach a fixed point.
829 for (i = 0; i < block->number_of_exception_handlers(); i++) {
830 BlockBegin* next = block->exception_handler_at(i);
831 if (merge_state_for(next, state())) {
832 if (!work_list()->contains(next)) {
833 work_list()->push(next);
834 }
835 }
836 }
837
838 // Iterate through block, updating state.
839 for (Instruction* instr = block; instr != NULL__null; instr = instr->next()) {
840 // Mark instructions in this block as visitable as they are seen
841 // in the instruction list. This keeps the iteration from
842 // visiting instructions which are references in other blocks or
843 // visiting instructions more than once.
844 mark_visitable(instr);
845 if (instr->is_pinned() || instr->can_trap() || (instr->as_NullCheck() != NULL__null)) {
846 mark_visited(instr);
847 instr->input_values_do(this);
848 instr->visit(&_visitor);
849 }
850 }
851
852 // Propagate state to successors if necessary
853 for (i = 0; i < e->number_of_sux(); i++) {
854 BlockBegin* next = e->sux_at(i);
855 if (merge_state_for(next, state())) {
856 if (!work_list()->contains(next)) {
857 work_list()->push(next);
858 }
859 }
860 }
861}
862
863
864void NullCheckEliminator::iterate(BlockBegin* block) {
865 work_list()->push(block);
866 iterate_all();
867}
868
869void NullCheckEliminator::handle_AccessField(AccessField* x) {
870 if (x->is_static()) {
871 if (x->as_LoadField() != NULL__null) {
872 // If the field is a non-null static final object field (as is
873 // often the case for sun.misc.Unsafe), put this LoadField into
874 // the non-null map
875 ciField* field = x->field();
876 if (field->is_constant()) {
877 ciConstant field_val = field->constant_value();
878 BasicType field_type = field_val.basic_type();
879 if (is_reference_type(field_type)) {
880 ciObject* obj_val = field_val.as_object();
881 if (!obj_val->is_null_object()) {
882 if (PrintNullCheckElimination) {
883 tty->print_cr("AccessField %d proven non-null by static final non-null oop check",
884 x->id());
885 }
886 set_put(x);
887 }
888 }
889 }
890 }
891 // Be conservative
892 clear_last_explicit_null_check();
893 return;
894 }
895
896 Value obj = x->obj();
897 if (set_contains(obj)) {
898 // Value is non-null => update AccessField
899 if (last_explicit_null_check_obj() == obj && !x->needs_patching()) {
900 x->set_explicit_null_check(consume_last_explicit_null_check());
901 x->set_needs_null_check(true);
902 if (PrintNullCheckElimination) {
903 tty->print_cr("Folded NullCheck %d into AccessField %d's null check for value %d",
904 x->explicit_null_check()->id(), x->id(), obj->id());
905 }
906 } else {
907 x->set_explicit_null_check(NULL__null);
908 x->set_needs_null_check(false);
909 if (PrintNullCheckElimination) {
910 tty->print_cr("Eliminated AccessField %d's null check for value %d", x->id(), obj->id());
911 }
912 }
913 } else {
914 set_put(obj);
915 if (PrintNullCheckElimination) {
916 tty->print_cr("AccessField %d of value %d proves value to be non-null", x->id(), obj->id());
917 }
918 // Ensure previous passes do not cause wrong state
919 x->set_needs_null_check(true);
920 x->set_explicit_null_check(NULL__null);
921 }
922 clear_last_explicit_null_check();
923}
924
925
926void NullCheckEliminator::handle_ArrayLength(ArrayLength* x) {
927 Value array = x->array();
928 if (set_contains(array)) {
929 // Value is non-null => update AccessArray
930 if (last_explicit_null_check_obj() == array) {
931 x->set_explicit_null_check(consume_last_explicit_null_check());
932 x->set_needs_null_check(true);
933 if (PrintNullCheckElimination) {
934 tty->print_cr("Folded NullCheck %d into ArrayLength %d's null check for value %d",
935 x->explicit_null_check()->id(), x->id(), array->id());
936 }
937 } else {
938 x->set_explicit_null_check(NULL__null);
939 x->set_needs_null_check(false);
940 if (PrintNullCheckElimination) {
941 tty->print_cr("Eliminated ArrayLength %d's null check for value %d", x->id(), array->id());
942 }
943 }
944 } else {
945 set_put(array);
946 if (PrintNullCheckElimination) {
947 tty->print_cr("ArrayLength %d of value %d proves value to be non-null", x->id(), array->id());
948 }
949 // Ensure previous passes do not cause wrong state
950 x->set_needs_null_check(true);
951 x->set_explicit_null_check(NULL__null);
952 }
953 clear_last_explicit_null_check();
954}
955
956
957void NullCheckEliminator::handle_LoadIndexed(LoadIndexed* x) {
958 Value array = x->array();
959 if (set_contains(array)) {
960 // Value is non-null => update AccessArray
961 if (last_explicit_null_check_obj() == array) {
962 x->set_explicit_null_check(consume_last_explicit_null_check());
963 x->set_needs_null_check(true);
964 if (PrintNullCheckElimination) {
965 tty->print_cr("Folded NullCheck %d into LoadIndexed %d's null check for value %d",
966 x->explicit_null_check()->id(), x->id(), array->id());
967 }
968 } else {
969 x->set_explicit_null_check(NULL__null);
970 x->set_needs_null_check(false);
971 if (PrintNullCheckElimination) {
972 tty->print_cr("Eliminated LoadIndexed %d's null check for value %d", x->id(), array->id());
973 }
974 }
975 } else {
976 set_put(array);
977 if (PrintNullCheckElimination) {
978 tty->print_cr("LoadIndexed %d of value %d proves value to be non-null", x->id(), array->id());
979 }
980 // Ensure previous passes do not cause wrong state
981 x->set_needs_null_check(true);
982 x->set_explicit_null_check(NULL__null);
983 }
984 clear_last_explicit_null_check();
985}
986
987
988void NullCheckEliminator::handle_StoreIndexed(StoreIndexed* x) {
989 Value array = x->array();
990 if (set_contains(array)) {
991 // Value is non-null => update AccessArray
992 if (PrintNullCheckElimination) {
993 tty->print_cr("Eliminated StoreIndexed %d's null check for value %d", x->id(), array->id());
994 }
995 x->set_needs_null_check(false);
996 } else {
997 set_put(array);
998 if (PrintNullCheckElimination) {
999 tty->print_cr("StoreIndexed %d of value %d proves value to be non-null", x->id(), array->id());
1000 }
1001 // Ensure previous passes do not cause wrong state
1002 x->set_needs_null_check(true);
1003 }
1004 clear_last_explicit_null_check();
1005}
1006
1007
1008void NullCheckEliminator::handle_NullCheck(NullCheck* x) {
1009 Value obj = x->obj();
1010 if (set_contains(obj)) {
1011 // Already proven to be non-null => this NullCheck is useless
1012 if (PrintNullCheckElimination) {
1013 tty->print_cr("Eliminated NullCheck %d for value %d", x->id(), obj->id());
1014 }
1015 // Don't unpin since that may shrink obj's live range and make it unavailable for debug info.
1016 // The code generator won't emit LIR for a NullCheck that cannot trap.
1017 x->set_can_trap(false);
1018 } else {
1019 // May be null => add to map and set last explicit NullCheck
1020 x->set_can_trap(true);
1021 // make sure it's pinned if it can trap
1022 x->pin(Instruction::PinExplicitNullCheck);
1023 set_put(obj);
1024 set_last_explicit_null_check(x);
1025 if (PrintNullCheckElimination) {
1026 tty->print_cr("NullCheck %d of value %d proves value to be non-null", x->id(), obj->id());
1027 }
1028 }
1029}
1030
1031
1032void NullCheckEliminator::handle_Invoke(Invoke* x) {
1033 if (!x->has_receiver()) {
1034 // Be conservative
1035 clear_last_explicit_null_check();
1036 return;
1037 }
1038
1039 Value recv = x->receiver();
1040 if (!set_contains(recv)) {
1041 set_put(recv);
1042 if (PrintNullCheckElimination) {
1043 tty->print_cr("Invoke %d of value %d proves value to be non-null", x->id(), recv->id());
1044 }
1045 }
1046 clear_last_explicit_null_check();
1047}
1048
1049
1050void NullCheckEliminator::handle_NewInstance(NewInstance* x) {
1051 set_put(x);
1052 if (PrintNullCheckElimination) {
1053 tty->print_cr("NewInstance %d is non-null", x->id());
1054 }
1055}
1056
1057
1058void NullCheckEliminator::handle_NewArray(NewArray* x) {
1059 set_put(x);
1060 if (PrintNullCheckElimination) {
1061 tty->print_cr("NewArray %d is non-null", x->id());
1062 }
1063}
1064
1065
1066void NullCheckEliminator::handle_ExceptionObject(ExceptionObject* x) {
1067 set_put(x);
1068 if (PrintNullCheckElimination) {
1069 tty->print_cr("ExceptionObject %d is non-null", x->id());
1070 }
1071}
1072
1073
1074void NullCheckEliminator::handle_AccessMonitor(AccessMonitor* x) {
1075 Value obj = x->obj();
1076 if (set_contains(obj)) {
1077 // Value is non-null => update AccessMonitor
1078 if (PrintNullCheckElimination) {
1079 tty->print_cr("Eliminated AccessMonitor %d's null check for value %d", x->id(), obj->id());
1080 }
1081 x->set_needs_null_check(false);
1082 } else {
1083 set_put(obj);
1084 if (PrintNullCheckElimination) {
1085 tty->print_cr("AccessMonitor %d of value %d proves value to be non-null", x->id(), obj->id());
1086 }
1087 // Ensure previous passes do not cause wrong state
1088 x->set_needs_null_check(true);
1089 }
1090 clear_last_explicit_null_check();
1091}
1092
1093
1094void NullCheckEliminator::handle_Intrinsic(Intrinsic* x) {
1095 if (!x->has_receiver()) {
1096 if (x->id() == vmIntrinsics::_arraycopy) {
1097 for (int i = 0; i < x->number_of_arguments(); i++) {
1098 x->set_arg_needs_null_check(i, !set_contains(x->argument_at(i)));
1099 }
1100 }
1101
1102 // Be conservative
1103 clear_last_explicit_null_check();
1104 return;
1105 }
1106
1107 Value recv = x->receiver();
1108 if (set_contains(recv)) {
1109 // Value is non-null => update Intrinsic
1110 if (PrintNullCheckElimination) {
1111 tty->print_cr("Eliminated Intrinsic %d's null check for value %d", vmIntrinsics::as_int(x->id()), recv->id());
1112 }
1113 x->set_needs_null_check(false);
1114 } else {
1115 set_put(recv);
1116 if (PrintNullCheckElimination) {
1117 tty->print_cr("Intrinsic %d of value %d proves value to be non-null", vmIntrinsics::as_int(x->id()), recv->id());
1118 }
1119 // Ensure previous passes do not cause wrong state
1120 x->set_needs_null_check(true);
1121 }
1122 clear_last_explicit_null_check();
1123}
1124
1125
1126void NullCheckEliminator::handle_Phi(Phi* x) {
1127 int i;
1128 bool all_non_null = true;
1129 if (x->is_illegal()) {
1130 all_non_null = false;
1131 } else {
1132 for (i = 0; i < x->operand_count(); i++) {
1133 Value input = x->operand_at(i);
1134 if (!set_contains(input)) {
1135 all_non_null = false;
1136 }
1137 }
1138 }
1139
1140 if (all_non_null) {
1141 // Value is non-null => update Phi
1142 if (PrintNullCheckElimination) {
1143 tty->print_cr("Eliminated Phi %d's null check for phifun because all inputs are non-null", x->id());
1144 }
1145 x->set_needs_null_check(false);
1146 } else if (set_contains(x)) {
1147 set_remove(x);
1148 }
1149}
1150
1151void NullCheckEliminator::handle_ProfileCall(ProfileCall* x) {
1152 for (int i = 0; i < x->nb_profiled_args(); i++) {
1153 x->set_arg_needs_null_check(i, !set_contains(x->profiled_arg_at(i)));
1154 }
1155}
1156
1157void NullCheckEliminator::handle_ProfileReturnType(ProfileReturnType* x) {
1158 x->set_needs_null_check(!set_contains(x->ret()));
1159}
1160
1161void Optimizer::eliminate_null_checks() {
1162 ResourceMark rm;
1163
1164 NullCheckEliminator nce(this);
1165
1166 if (PrintNullCheckElimination) {
1167 tty->print_cr("Starting null check elimination for method %s::%s%s",
1168 ir()->method()->holder()->name()->as_utf8(),
1169 ir()->method()->name()->as_utf8(),
1170 ir()->method()->signature()->as_symbol()->as_utf8());
1171 }
1172
1173 // Apply to graph
1174 nce.iterate(ir()->start());
1175
1176 // walk over the graph looking for exception
1177 // handlers and iterate over them as well
1178 int nblocks = BlockBegin::number_of_blocks();
1179 BlockList blocks(nblocks);
1180 boolArray visited_block(nblocks, nblocks, false);
1181
1182 blocks.push(ir()->start());
1183 visited_block.at_put(ir()->start()->block_id(), true);
1184 for (int i = 0; i < blocks.length(); i++) {
1185 BlockBegin* b = blocks.at(i);
1186 // exception handlers need to be treated as additional roots
1187 for (int e = b->number_of_exception_handlers(); e-- > 0; ) {
1188 BlockBegin* excp = b->exception_handler_at(e);
1189 int id = excp->block_id();
1190 if (!visited_block.at(id)) {
1191 blocks.push(excp);
1192 visited_block.at_put(id, true);
1193 nce.iterate(excp);
1194 }
1195 }
1196 // traverse successors
1197 BlockEnd *end = b->end();
1198 for (int s = end->number_of_sux(); s-- > 0; ) {
1199 BlockBegin* next = end->sux_at(s);
1200 int id = next->block_id();
1201 if (!visited_block.at(id)) {
1202 blocks.push(next);
1203 visited_block.at_put(id, true);
1204 }
1205 }
1206 }
1207
1208
1209 if (PrintNullCheckElimination) {
1210 tty->print_cr("Done with null check elimination for method %s::%s%s",
1211 ir()->method()->holder()->name()->as_utf8(),
1212 ir()->method()->name()->as_utf8(),
1213 ir()->method()->signature()->as_symbol()->as_utf8());
1214 }
1215}