annotate src/share/vm/c1/c1_Optimizer.cpp @ 20543:e7d0505c8a30

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