comparison src/share/vm/c1/c1_ValueStack.cpp @ 1819:f02a8bbe6ed4

6986046: C1 valuestack cleanup Summary: fixes an historical oddity in C1 with inlining where all of the expression stacks are kept in the topmost ValueStack instead of being in their respective ValueStacks. Reviewed-by: never Contributed-by: Christian Wimmer <cwimmer@uci.edu>
author roland
date Tue, 29 Dec 2009 19:08:54 +0100
parents b812ff5abc73
children f95d63e2154a
comparison
equal deleted inserted replaced
1817:c40600e85311 1819:f02a8bbe6ed4
26 #include "incls/_c1_ValueStack.cpp.incl" 26 #include "incls/_c1_ValueStack.cpp.incl"
27 27
28 28
29 // Implementation of ValueStack 29 // Implementation of ValueStack
30 30
31 ValueStack::ValueStack(IRScope* scope, int locals_size, int max_stack_size) 31 ValueStack::ValueStack(IRScope* scope, ValueStack* caller_state)
32 : _scope(scope) 32 : _scope(scope)
33 , _locals(locals_size, NULL) 33 , _caller_state(caller_state)
34 , _stack(max_stack_size) 34 , _bci(-99)
35 , _lock_stack(false) 35 , _kind(Parsing)
36 , _locks(1) 36 , _locals(scope->method()->max_locals(), NULL)
37 , _stack(scope->method()->max_stack())
38 , _locks()
37 { 39 {
38 assert(scope != NULL, "scope must exist"); 40 verify();
39 } 41 }
40 42
41 ValueStack* ValueStack::copy() { 43
42 ValueStack* s = new ValueStack(scope(), locals_size(), max_stack_size()); 44 ValueStack::ValueStack(ValueStack* copy_from, Kind kind, int bci)
43 s->_stack.appendAll(&_stack); 45 : _scope(copy_from->scope())
44 s->_locks.appendAll(&_locks); 46 , _caller_state(copy_from->caller_state())
45 s->replace_locals(this); 47 , _bci(bci)
46 return s; 48 , _kind(kind)
47 } 49 , _locals()
48 50 , _stack()
49 51 , _locks(copy_from->locks_size())
50 ValueStack* ValueStack::copy_locks() { 52 {
51 int sz = scope()->lock_stack_size(); 53 assert(kind != EmptyExceptionState || !Compilation::current()->env()->jvmti_can_access_local_variables(), "need locals");
52 if (stack_size() == 0) { 54 if (kind != EmptyExceptionState) {
53 sz = 0; 55 // only allocate space if we need to copy the locals-array
54 } 56 _locals = Values(copy_from->locals_size());
55 ValueStack* s = new ValueStack(scope(), locals_size(), sz); 57 _locals.appendAll(&copy_from->_locals);
56 s->_lock_stack = true; 58 }
57 s->_locks.appendAll(&_locks); 59
58 s->replace_locals(this); 60 if (kind != ExceptionState && kind != EmptyExceptionState) {
59 if (sz > 0) { 61 if (kind == Parsing) {
60 assert(sz <= stack_size(), "lock stack underflow"); 62 // stack will be modified, so reserve enough space to avoid resizing
61 for (int i = 0; i < sz; i++) { 63 _stack = Values(scope()->method()->max_stack());
62 s->_stack.append(_stack[i]); 64 } else {
63 } 65 // stack will not be modified, so do not waste space
64 } 66 _stack = Values(copy_from->stack_size());
65 return s; 67 }
66 } 68 _stack.appendAll(&copy_from->_stack);
69 }
70
71 _locks.appendAll(&copy_from->_locks);
72
73 verify();
74 }
75
67 76
68 bool ValueStack::is_same(ValueStack* s) { 77 bool ValueStack::is_same(ValueStack* s) {
69 assert(s != NULL, "state must exist"); 78 if (scope() != s->scope()) return false;
70 assert(scope () == s->scope (), "scopes must correspond"); 79 if (caller_state() != s->caller_state()) return false;
71 assert(locals_size() == s->locals_size(), "locals sizes must correspond"); 80
72 return is_same_across_scopes(s); 81 if (locals_size() != s->locals_size()) return false;
73 } 82 if (stack_size() != s->stack_size()) return false;
74 83 if (locks_size() != s->locks_size()) return false;
75 84
76 bool ValueStack::is_same_across_scopes(ValueStack* s) {
77 assert(s != NULL, "state must exist");
78 assert(stack_size () == s->stack_size (), "stack sizes must correspond");
79 assert(locks_size () == s->locks_size (), "locks sizes must correspond");
80 // compare each stack element with the corresponding stack element of s 85 // compare each stack element with the corresponding stack element of s
81 int index; 86 int index;
82 Value value; 87 Value value;
83 for_each_stack_value(this, index, value) { 88 for_each_stack_value(this, index, value) {
84 if (value->type()->tag() != s->stack_at(index)->type()->tag()) return false; 89 if (value->type()->tag() != s->stack_at(index)->type()->tag()) return false;
87 if (value != s->lock_at(index)) return false; 92 if (value != s->lock_at(index)) return false;
88 } 93 }
89 return true; 94 return true;
90 } 95 }
91 96
92
93 ValueStack* ValueStack::caller_state() const {
94 return scope()->caller_state();
95 }
96
97
98 void ValueStack::clear_locals() { 97 void ValueStack::clear_locals() {
99 for (int i = _locals.length() - 1; i >= 0; i--) { 98 for (int i = _locals.length() - 1; i >= 0; i--) {
100 _locals.at_put(i, NULL); 99 _locals.at_put(i, NULL);
101 } 100 }
102 } 101 }
103 102
104
105 void ValueStack::replace_locals(ValueStack* with) {
106 assert(locals_size() == with->locals_size(), "number of locals must match");
107 for (int i = locals_size() - 1; i >= 0; i--) {
108 _locals.at_put(i, with->_locals.at(i));
109 }
110 }
111 103
112 void ValueStack::pin_stack_for_linear_scan() { 104 void ValueStack::pin_stack_for_linear_scan() {
113 for_each_state_value(this, v, 105 for_each_state_value(this, v,
114 if (v->as_Constant() == NULL && v->as_Local() == NULL) { 106 if (v->as_Constant() == NULL && v->as_Local() == NULL) {
115 v->pin(Instruction::PinStackForStateSplit); 107 v->pin(Instruction::PinStackForStateSplit);
121 // apply function to all values of a list; factored out from values_do(f) 113 // apply function to all values of a list; factored out from values_do(f)
122 void ValueStack::apply(Values list, ValueVisitor* f) { 114 void ValueStack::apply(Values list, ValueVisitor* f) {
123 for (int i = 0; i < list.length(); i++) { 115 for (int i = 0; i < list.length(); i++) {
124 Value* va = list.adr_at(i); 116 Value* va = list.adr_at(i);
125 Value v0 = *va; 117 Value v0 = *va;
126 if (v0 != NULL) { 118 if (v0 != NULL && !v0->type()->is_illegal()) {
127 if (!v0->type()->is_illegal()) { 119 f->visit(va);
128 assert(v0->as_HiWord() == NULL, "should never see HiWord during traversal");
129 f->visit(va);
130 #ifdef ASSERT 120 #ifdef ASSERT
131 Value v1 = *va; 121 Value v1 = *va;
132 if (v0 != v1) { 122 assert(v1->type()->is_illegal() || v0->type()->tag() == v1->type()->tag(), "types must match");
133 assert(v1->type()->is_illegal() || v0->type()->tag() == v1->type()->tag(), "types must match"); 123 assert(!v1->type()->is_double_word() || list.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");
134 if (v0->type()->is_double_word()) {
135 list.at_put(i + 1, v0->hi_word());
136 }
137 }
138 #endif 124 #endif
139 if (v0->type()->is_double_word()) i++; 125 if (v0->type()->is_double_word()) i++;
140 }
141 } 126 }
142 } 127 }
143 } 128 }
144 129
145 130
146 void ValueStack::values_do(ValueVisitor* f) { 131 void ValueStack::values_do(ValueVisitor* f) {
147 apply(_stack, f);
148 apply(_locks, f);
149
150 ValueStack* state = this; 132 ValueStack* state = this;
151 for_each_state(state) { 133 for_each_state(state) {
152 apply(state->_locals, f); 134 apply(state->_locals, f);
135 apply(state->_stack, f);
136 apply(state->_locks, f);
153 } 137 }
154 } 138 }
155 139
156 140
157 Values* ValueStack::pop_arguments(int argument_size) { 141 Values* ValueStack::pop_arguments(int argument_size) {
162 truncate_stack(base); 146 truncate_stack(base);
163 return args; 147 return args;
164 } 148 }
165 149
166 150
167 int ValueStack::lock(IRScope* scope, Value obj) { 151 int ValueStack::total_locks_size() const {
152 int num_locks = 0;
153 const ValueStack* state = this;
154 for_each_state(state) {
155 num_locks += state->locks_size();
156 }
157 return num_locks;
158 }
159
160 int ValueStack::lock(Value obj) {
168 _locks.push(obj); 161 _locks.push(obj);
169 scope->set_min_number_of_locks(locks_size()); 162 int num_locks = total_locks_size();
170 return locks_size() - 1; 163 scope()->set_min_number_of_locks(num_locks);
164 return num_locks - 1;
171 } 165 }
172 166
173 167
174 int ValueStack::unlock() { 168 int ValueStack::unlock() {
175 _locks.pop(); 169 _locks.pop();
176 return locks_size(); 170 return total_locks_size();
177 }
178
179
180 ValueStack* ValueStack::push_scope(IRScope* scope) {
181 assert(scope->caller() == _scope, "scopes must have caller/callee relationship");
182 ValueStack* res = new ValueStack(scope,
183 scope->method()->max_locals(),
184 max_stack_size() + scope->method()->max_stack());
185 // Preserves stack and monitors.
186 res->_stack.appendAll(&_stack);
187 res->_locks.appendAll(&_locks);
188 assert(res->_stack.size() <= res->max_stack_size(), "stack overflow");
189 return res;
190 }
191
192
193 ValueStack* ValueStack::pop_scope() {
194 assert(_scope->caller() != NULL, "scope must have caller");
195 IRScope* scope = _scope->caller();
196 int max_stack = max_stack_size() - _scope->method()->max_stack();
197 assert(max_stack >= 0, "stack underflow");
198 ValueStack* res = new ValueStack(scope,
199 scope->method()->max_locals(),
200 max_stack);
201 // Preserves stack and monitors. Restores local and store state from caller scope.
202 res->_stack.appendAll(&_stack);
203 res->_locks.appendAll(&_locks);
204 ValueStack* caller = caller_state();
205 if (caller != NULL) {
206 for (int i = 0; i < caller->_locals.length(); i++) {
207 res->_locals.at_put(i, caller->_locals.at(i));
208 }
209 assert(res->_locals.length() == res->scope()->method()->max_locals(), "just checking");
210 }
211 assert(res->_stack.size() <= res->max_stack_size(), "stack overflow");
212 return res;
213 } 171 }
214 172
215 173
216 void ValueStack::setup_phi_for_stack(BlockBegin* b, int index) { 174 void ValueStack::setup_phi_for_stack(BlockBegin* b, int index) {
217 assert(stack_at(index)->as_Phi() == NULL || stack_at(index)->as_Phi()->block() != b, "phi function already created"); 175 assert(stack_at(index)->as_Phi() == NULL || stack_at(index)->as_Phi()->block() != b, "phi function already created");
218 176
219 ValueType* t = stack_at(index)->type(); 177 ValueType* t = stack_at(index)->type();
220 Value phi = new Phi(t, b, -index - 1); 178 Value phi = new Phi(t, b, -index - 1);
221 _stack[index] = phi; 179 _stack[index] = phi;
222 180
223 #ifdef ASSERT 181 assert(!t->is_double_word() || _stack.at(index + 1) == NULL, "hi-word of doubleword value must be NULL");
224 if (t->is_double_word()) {
225 _stack[index + 1] = phi->hi_word();
226 }
227 #endif
228 } 182 }
229 183
230 void ValueStack::setup_phi_for_local(BlockBegin* b, int index) { 184 void ValueStack::setup_phi_for_local(BlockBegin* b, int index) {
231 assert(local_at(index)->as_Phi() == NULL || local_at(index)->as_Phi()->block() != b, "phi function already created"); 185 assert(local_at(index)->as_Phi() == NULL || local_at(index)->as_Phi()->block() != b, "phi function already created");
232 186
234 Value phi = new Phi(t, b, index); 188 Value phi = new Phi(t, b, index);
235 store_local(index, phi); 189 store_local(index, phi);
236 } 190 }
237 191
238 #ifndef PRODUCT 192 #ifndef PRODUCT
193
239 void ValueStack::print() { 194 void ValueStack::print() {
195 scope()->method()->print_name();
240 if (stack_is_empty()) { 196 if (stack_is_empty()) {
241 tty->print_cr("empty stack"); 197 tty->print_cr("empty stack");
242 } else { 198 } else {
243 InstructionPrinter ip; 199 InstructionPrinter ip;
244 for (int i = 0; i < stack_size();) { 200 for (int i = 0; i < stack_size();) {
245 Value t = stack_at_inc(i); 201 Value t = stack_at_inc(i);
246 tty->print("%2d ", i); 202 tty->print("%2d ", i);
203 tty->print("%c%d ", t->type()->tchar(), t->id());
247 ip.print_instr(t); 204 ip.print_instr(t);
248 tty->cr(); 205 tty->cr();
249 } 206 }
250 } 207 }
251 if (!no_active_locks()) { 208 if (!no_active_locks()) {
252 InstructionPrinter ip; 209 InstructionPrinter ip;
253 for (int i = 0; i < locks_size(); i--) { 210 for (int i = 0; i < locks_size(); i++) {
254 Value t = lock_at(i); 211 Value t = lock_at(i);
255 tty->print("lock %2d ", i); 212 tty->print("lock %2d ", i);
256 if (t == NULL) { 213 if (t == NULL) {
257 tty->print("this"); 214 tty->print("this");
258 } else { 215 } else {
216 tty->print("%c%d ", t->type()->tchar(), t->id());
259 ip.print_instr(t); 217 ip.print_instr(t);
260 } 218 }
261 tty->cr(); 219 tty->cr();
262 } 220 }
263 } 221 }
268 tty->print("local %d ", i); 226 tty->print("local %d ", i);
269 if (l == NULL) { 227 if (l == NULL) {
270 tty->print("null"); 228 tty->print("null");
271 i ++; 229 i ++;
272 } else { 230 } else {
231 tty->print("%c%d ", l->type()->tchar(), l->id());
273 ip.print_instr(l); 232 ip.print_instr(l);
274 if (l->type()->is_illegal() || l->type()->is_single_word()) i ++; else i += 2; 233 if (l->type()->is_illegal() || l->type()->is_single_word()) i ++; else i += 2;
275 } 234 }
276 tty->cr(); 235 tty->cr();
277 } 236 }
278 } 237 }
238
239 if (caller_state() != NULL) {
240 caller_state()->print();
241 }
279 } 242 }
280 243
281 244
282 void ValueStack::verify() { 245 void ValueStack::verify() {
283 Unimplemented(); 246 assert(scope() != NULL, "scope must exist");
247 if (caller_state() != NULL) {
248 assert(caller_state()->scope() == scope()->caller(), "invalid caller scope");
249 caller_state()->verify();
250 }
251
252 if (kind() == Parsing) {
253 assert(bci() == -99, "bci not defined during parsing");
254 } else {
255 assert(bci() >= -1, "bci out of range");
256 assert(bci() < scope()->method()->code_size(), "bci out of range");
257 assert(bci() == SynchronizationEntryBCI || Bytecodes::is_defined(scope()->method()->java_code_at_bci(bci())), "make sure bci points at a real bytecode");
258 assert(scope()->method()->liveness_at_bci(bci()).is_valid(), "liveness at bci must be valid");
259 }
260
261 int i;
262 for (i = 0; i < stack_size(); i++) {
263 Value v = _stack.at(i);
264 if (v == NULL) {
265 assert(_stack.at(i - 1)->type()->is_double_word(), "only hi-words are NULL on stack");
266 } else if (v->type()->is_double_word()) {
267 assert(_stack.at(i + 1) == NULL, "hi-word must be NULL");
268 }
269 }
270
271 for (i = 0; i < locals_size(); i++) {
272 Value v = _locals.at(i);
273 if (v != NULL && v->type()->is_double_word()) {
274 assert(_locals.at(i + 1) == NULL, "hi-word must be NULL");
275 }
276 }
277
278 for_each_state_value(this, v,
279 assert(v != NULL, "just test if state-iteration succeeds");
280 );
284 } 281 }
285 #endif // PRODUCT 282 #endif // PRODUCT