Mercurial > hg > truffle
annotate src/share/vm/c1/c1_ValueMap.cpp @ 8868:30f42e691e70
8004640: C2 assert failure in memnode.cpp: NULL+offs not RAW address
Summary: always transform AddP nodes in IdealKit by calling _gvn.transform().
Reviewed-by: roland, twisti
author | kvn |
---|---|
date | Tue, 26 Mar 2013 12:55:26 -0700 |
parents | 46f6f063b272 |
children | 84ab5667f290 |
rev | line source |
---|---|
0 | 1 /* |
6842
b9a9ed0f8eeb
7197424: update copyright year to match last edit in jdk8 hotspot repository
mikael
parents:
6618
diff
changeset
|
2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. |
0 | 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
0
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
0
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:
0
diff
changeset
|
21 * questions. |
0 | 22 * |
23 */ | |
24 | |
1972 | 25 #include "precompiled.hpp" |
26 #include "c1/c1_Canonicalizer.hpp" | |
27 #include "c1/c1_IR.hpp" | |
28 #include "c1/c1_ValueMap.hpp" | |
8860 | 29 #include "c1/c1_ValueStack.hpp" |
1972 | 30 #include "utilities/bitMap.inline.hpp" |
0 | 31 |
32 #ifndef PRODUCT | |
33 | |
34 int ValueMap::_number_of_finds = 0; | |
35 int ValueMap::_number_of_hits = 0; | |
36 int ValueMap::_number_of_kills = 0; | |
37 | |
38 #define TRACE_VALUE_NUMBERING(code) if (PrintValueNumbering) { code; } | |
39 | |
40 #else | |
41 | |
42 #define TRACE_VALUE_NUMBERING(code) | |
43 | |
44 #endif | |
45 | |
46 | |
47 ValueMap::ValueMap() | |
48 : _nesting(0) | |
49 , _entries(ValueMapInitialSize, NULL) | |
50 , _killed_values() | |
51 , _entry_count(0) | |
52 { | |
53 NOT_PRODUCT(reset_statistics()); | |
54 } | |
55 | |
56 | |
57 ValueMap::ValueMap(ValueMap* old) | |
58 : _nesting(old->_nesting + 1) | |
59 , _entries(old->_entries.length()) | |
60 , _killed_values() | |
61 , _entry_count(old->_entry_count) | |
62 { | |
63 for (int i = size() - 1; i >= 0; i--) { | |
64 _entries.at_put(i, old->entry_at(i)); | |
65 } | |
66 _killed_values.set_from(&old->_killed_values); | |
67 } | |
68 | |
69 | |
70 void ValueMap::increase_table_size() { | |
71 int old_size = size(); | |
72 int new_size = old_size * 2 + 1; | |
73 | |
74 ValueMapEntryList worklist(8); | |
75 ValueMapEntryArray new_entries(new_size, NULL); | |
76 int new_entry_count = 0; | |
77 | |
78 TRACE_VALUE_NUMBERING(tty->print_cr("increasing table size from %d to %d", old_size, new_size)); | |
79 | |
80 for (int i = old_size - 1; i >= 0; i--) { | |
81 ValueMapEntry* entry; | |
82 for (entry = entry_at(i); entry != NULL; entry = entry->next()) { | |
83 if (!is_killed(entry->value())) { | |
84 worklist.push(entry); | |
85 } | |
86 } | |
87 | |
88 while (!worklist.is_empty()) { | |
89 entry = worklist.pop(); | |
90 int new_index = entry_index(entry->hash(), new_size); | |
91 | |
92 if (entry->nesting() != nesting() && new_entries.at(new_index) != entry->next()) { | |
93 // changing entries with a lower nesting than the current nesting of the table | |
94 // is not allowed because then the same entry is contained in multiple value maps. | |
95 // clone entry when next-pointer must be changed | |
96 entry = new ValueMapEntry(entry->hash(), entry->value(), entry->nesting(), NULL); | |
97 } | |
98 entry->set_next(new_entries.at(new_index)); | |
99 new_entries.at_put(new_index, entry); | |
100 new_entry_count++; | |
101 } | |
102 } | |
103 | |
104 _entries = new_entries; | |
105 _entry_count = new_entry_count; | |
106 } | |
107 | |
108 | |
109 Value ValueMap::find_insert(Value x) { | |
110 const intx hash = x->hash(); | |
111 if (hash != 0) { | |
112 // 0 hash means: exclude from value numbering | |
113 NOT_PRODUCT(_number_of_finds++); | |
114 | |
115 for (ValueMapEntry* entry = entry_at(entry_index(hash, size())); entry != NULL; entry = entry->next()) { | |
116 if (entry->hash() == hash) { | |
117 Value f = entry->value(); | |
118 | |
119 if (!is_killed(f) && f->is_equal(x)) { | |
120 NOT_PRODUCT(_number_of_hits++); | |
121 TRACE_VALUE_NUMBERING(tty->print_cr("Value Numbering: %s %c%d equal to %c%d (size %d, entries %d, nesting-diff %d)", x->name(), x->type()->tchar(), x->id(), f->type()->tchar(), f->id(), size(), entry_count(), nesting() - entry->nesting())); | |
122 | |
123 if (entry->nesting() != nesting() && f->as_Constant() == NULL) { | |
124 // non-constant values of of another block must be pinned, | |
125 // otherwise it is possible that they are not evaluated | |
126 f->pin(Instruction::PinGlobalValueNumbering); | |
127 } | |
4871
f067b4e0e04b
7090976: Eclipse/CDT causes a JVM crash while indexing C++ code
roland
parents:
1972
diff
changeset
|
128 assert(x->type()->tag() == f->type()->tag(), "should have same type"); |
0 | 129 |
130 return f; | |
131 | |
132 } | |
133 } | |
134 } | |
135 | |
136 // x not found, so insert it | |
137 if (entry_count() >= size_threshold()) { | |
138 increase_table_size(); | |
139 } | |
140 int idx = entry_index(hash, size()); | |
141 _entries.at_put(idx, new ValueMapEntry(hash, x, nesting(), entry_at(idx))); | |
142 _entry_count++; | |
143 | |
144 TRACE_VALUE_NUMBERING(tty->print_cr("Value Numbering: insert %s %c%d (size %d, entries %d, nesting %d)", x->name(), x->type()->tchar(), x->id(), size(), entry_count(), nesting())); | |
145 } | |
146 | |
147 return x; | |
148 } | |
149 | |
150 | |
151 #define GENERIC_KILL_VALUE(must_kill_implementation) \ | |
152 NOT_PRODUCT(_number_of_kills++); \ | |
153 \ | |
154 for (int i = size() - 1; i >= 0; i--) { \ | |
155 ValueMapEntry* prev_entry = NULL; \ | |
156 for (ValueMapEntry* entry = entry_at(i); entry != NULL; entry = entry->next()) { \ | |
157 Value value = entry->value(); \ | |
158 \ | |
159 must_kill_implementation(must_kill, entry, value) \ | |
160 \ | |
161 if (must_kill) { \ | |
162 kill_value(value); \ | |
163 \ | |
164 if (prev_entry == NULL) { \ | |
165 _entries.at_put(i, entry->next()); \ | |
166 _entry_count--; \ | |
167 } else if (prev_entry->nesting() == nesting()) { \ | |
168 prev_entry->set_next(entry->next()); \ | |
169 _entry_count--; \ | |
170 } else { \ | |
171 prev_entry = entry; \ | |
172 } \ | |
173 \ | |
174 TRACE_VALUE_NUMBERING(tty->print_cr("Value Numbering: killed %s %c%d (size %d, entries %d, nesting-diff %d)", value->name(), value->type()->tchar(), value->id(), size(), entry_count(), nesting() - entry->nesting())); \ | |
175 } else { \ | |
176 prev_entry = entry; \ | |
177 } \ | |
178 } \ | |
179 } \ | |
180 | |
181 #define MUST_KILL_MEMORY(must_kill, entry, value) \ | |
182 bool must_kill = value->as_LoadField() != NULL || value->as_LoadIndexed() != NULL; | |
183 | |
184 #define MUST_KILL_ARRAY(must_kill, entry, value) \ | |
185 bool must_kill = value->as_LoadIndexed() != NULL \ | |
186 && value->type()->tag() == type->tag(); | |
187 | |
188 #define MUST_KILL_FIELD(must_kill, entry, value) \ | |
189 /* ciField's are not unique; must compare their contents */ \ | |
190 LoadField* lf = value->as_LoadField(); \ | |
191 bool must_kill = lf != NULL \ | |
192 && lf->field()->holder() == field->holder() \ | |
6618
0bfcb7a3e12d
7171824: assert(_offset >= 1) failed: illegal call to offset()
roland
parents:
4871
diff
changeset
|
193 && (all_offsets || lf->field()->offset() == field->offset()); |
0 | 194 |
195 | |
196 void ValueMap::kill_memory() { | |
197 GENERIC_KILL_VALUE(MUST_KILL_MEMORY); | |
198 } | |
199 | |
200 void ValueMap::kill_array(ValueType* type) { | |
201 GENERIC_KILL_VALUE(MUST_KILL_ARRAY); | |
202 } | |
203 | |
6618
0bfcb7a3e12d
7171824: assert(_offset >= 1) failed: illegal call to offset()
roland
parents:
4871
diff
changeset
|
204 void ValueMap::kill_field(ciField* field, bool all_offsets) { |
0 | 205 GENERIC_KILL_VALUE(MUST_KILL_FIELD); |
206 } | |
207 | |
208 void ValueMap::kill_map(ValueMap* map) { | |
209 assert(is_global_value_numbering(), "only for global value numbering"); | |
210 _killed_values.set_union(&map->_killed_values); | |
211 } | |
212 | |
213 void ValueMap::kill_all() { | |
214 assert(is_local_value_numbering(), "only for local value numbering"); | |
215 for (int i = size() - 1; i >= 0; i--) { | |
216 _entries.at_put(i, NULL); | |
217 } | |
218 _entry_count = 0; | |
219 } | |
220 | |
221 | |
222 #ifndef PRODUCT | |
223 | |
224 void ValueMap::print() { | |
225 tty->print_cr("(size %d, entries %d, nesting %d)", size(), entry_count(), nesting()); | |
226 | |
227 int entries = 0; | |
228 for (int i = 0; i < size(); i++) { | |
229 if (entry_at(i) != NULL) { | |
230 tty->print(" %2d: ", i); | |
231 for (ValueMapEntry* entry = entry_at(i); entry != NULL; entry = entry->next()) { | |
232 Value value = entry->value(); | |
233 tty->print("%s %c%d (%s%d) -> ", value->name(), value->type()->tchar(), value->id(), is_killed(value) ? "x" : "", entry->nesting()); | |
234 entries++; | |
235 } | |
236 tty->print_cr("NULL"); | |
237 } | |
238 } | |
239 | |
240 _killed_values.print(); | |
241 assert(entry_count() == entries, "entry_count incorrect"); | |
242 } | |
243 | |
244 void ValueMap::reset_statistics() { | |
245 _number_of_finds = 0; | |
246 _number_of_hits = 0; | |
247 _number_of_kills = 0; | |
248 } | |
249 | |
250 void ValueMap::print_statistics() { | |
251 float hit_rate = 0; | |
252 if (_number_of_finds != 0) { | |
253 hit_rate = (float)_number_of_hits / _number_of_finds; | |
254 } | |
255 | |
256 tty->print_cr("finds:%3d hits:%3d kills:%3d hit rate: %1.4f", _number_of_finds, _number_of_hits, _number_of_kills, hit_rate); | |
257 } | |
258 | |
259 #endif | |
260 | |
261 | |
262 | |
263 class ShortLoopOptimizer : public ValueNumberingVisitor { | |
264 private: | |
265 GlobalValueNumbering* _gvn; | |
266 BlockList _loop_blocks; | |
267 bool _too_complicated_loop; | |
8860 | 268 bool _has_field_store[T_ARRAY + 1]; |
269 bool _has_indexed_store[T_ARRAY + 1]; | |
0 | 270 |
271 // simplified access to methods of GlobalValueNumbering | |
272 ValueMap* current_map() { return _gvn->current_map(); } | |
273 ValueMap* value_map_of(BlockBegin* block) { return _gvn->value_map_of(block); } | |
274 | |
275 // implementation for abstract methods of ValueNumberingVisitor | |
6618
0bfcb7a3e12d
7171824: assert(_offset >= 1) failed: illegal call to offset()
roland
parents:
4871
diff
changeset
|
276 void kill_memory() { _too_complicated_loop = true; } |
8860 | 277 void kill_field(ciField* field, bool all_offsets) { |
278 current_map()->kill_field(field, all_offsets); | |
279 assert(field->type()->basic_type() >= 0 && field->type()->basic_type() <= T_ARRAY, "Invalid type"); | |
280 _has_field_store[field->type()->basic_type()] = true; | |
281 } | |
282 void kill_array(ValueType* type) { | |
283 current_map()->kill_array(type); | |
284 BasicType basic_type = as_BasicType(type); assert(basic_type >= 0 && basic_type <= T_ARRAY, "Invalid type"); | |
285 _has_indexed_store[basic_type] = true; | |
286 } | |
0 | 287 |
288 public: | |
289 ShortLoopOptimizer(GlobalValueNumbering* gvn) | |
290 : _gvn(gvn) | |
291 , _loop_blocks(ValueMapMaxLoopSize) | |
292 , _too_complicated_loop(false) | |
293 { | |
8860 | 294 for (int i=0; i<= T_ARRAY; i++){ |
295 _has_field_store[i] = false; | |
296 _has_indexed_store[i] = false; | |
297 } | |
298 } | |
299 | |
300 bool has_field_store(BasicType type) { | |
301 assert(type >= 0 && type <= T_ARRAY, "Invalid type"); | |
302 return _has_field_store[type]; | |
303 } | |
304 | |
305 bool has_indexed_store(BasicType type) { | |
306 assert(type >= 0 && type <= T_ARRAY, "Invalid type"); | |
307 return _has_indexed_store[type]; | |
0 | 308 } |
309 | |
310 bool process(BlockBegin* loop_header); | |
311 }; | |
312 | |
8860 | 313 class LoopInvariantCodeMotion : public StackObj { |
314 private: | |
315 GlobalValueNumbering* _gvn; | |
316 ShortLoopOptimizer* _short_loop_optimizer; | |
317 Instruction* _insertion_point; | |
318 ValueStack * _state; | |
319 | |
320 void set_invariant(Value v) const { _gvn->set_processed(v); } | |
321 bool is_invariant(Value v) const { return _gvn->is_processed(v); } | |
322 | |
323 void process_block(BlockBegin* block); | |
324 | |
325 public: | |
326 LoopInvariantCodeMotion(ShortLoopOptimizer *slo, GlobalValueNumbering* gvn, BlockBegin* loop_header, BlockList* loop_blocks); | |
327 }; | |
328 | |
329 LoopInvariantCodeMotion::LoopInvariantCodeMotion(ShortLoopOptimizer *slo, GlobalValueNumbering* gvn, BlockBegin* loop_header, BlockList* loop_blocks) | |
330 : _gvn(gvn), _short_loop_optimizer(slo) { | |
331 | |
332 TRACE_VALUE_NUMBERING(tty->print_cr("using loop invariant code motion loop_header = %d", loop_header->block_id())); | |
333 TRACE_VALUE_NUMBERING(tty->print_cr("** loop invariant code motion for short loop B%d", loop_header->block_id())); | |
334 | |
335 BlockBegin* insertion_block = loop_header->dominator(); | |
336 if (insertion_block->number_of_preds() == 0) { | |
337 return; // only the entry block does not have a predecessor | |
338 } | |
339 | |
340 assert(insertion_block->end()->as_Base() == NULL, "cannot insert into entry block"); | |
341 _insertion_point = insertion_block->end()->prev(); | |
342 | |
343 BlockEnd *block_end = insertion_block->end(); | |
344 _state = block_end->state_before(); | |
345 | |
346 if (!_state) { | |
347 // If, TableSwitch and LookupSwitch always have state_before when | |
348 // loop invariant code motion happens.. | |
349 assert(block_end->as_Goto(), "Block has to be goto"); | |
350 _state = block_end->state(); | |
351 } | |
352 | |
353 // the loop_blocks are filled by going backward from the loop header, so this processing order is best | |
354 assert(loop_blocks->at(0) == loop_header, "loop header must be first loop block"); | |
355 process_block(loop_header); | |
356 for (int i = loop_blocks->length() - 1; i >= 1; i--) { | |
357 process_block(loop_blocks->at(i)); | |
358 } | |
359 } | |
360 | |
361 void LoopInvariantCodeMotion::process_block(BlockBegin* block) { | |
362 TRACE_VALUE_NUMBERING(tty->print_cr("processing block B%d", block->block_id())); | |
363 | |
364 Instruction* prev = block; | |
365 Instruction* cur = block->next(); | |
366 | |
367 while (cur != NULL) { | |
368 | |
369 // determine if cur instruction is loop invariant | |
370 // only selected instruction types are processed here | |
371 bool cur_invariant = false; | |
372 | |
373 if (cur->as_Constant() != NULL) { | |
374 cur_invariant = !cur->can_trap(); | |
375 } else if (cur->as_ArithmeticOp() != NULL || cur->as_LogicOp() != NULL || cur->as_ShiftOp() != NULL) { | |
376 assert(cur->as_Op2() != NULL, "must be Op2"); | |
377 Op2* op2 = (Op2*)cur; | |
378 cur_invariant = !op2->can_trap() && is_invariant(op2->x()) && is_invariant(op2->y()); | |
379 } else if (cur->as_LoadField() != NULL) { | |
380 LoadField* lf = (LoadField*)cur; | |
381 // deoptimizes on NullPointerException | |
382 cur_invariant = !lf->needs_patching() && !lf->field()->is_volatile() && !_short_loop_optimizer->has_field_store(lf->field()->type()->basic_type()) && is_invariant(lf->obj()); | |
383 } else if (cur->as_ArrayLength() != NULL) { | |
384 ArrayLength *length = cur->as_ArrayLength(); | |
385 cur_invariant = is_invariant(length->array()); | |
386 } else if (cur->as_LoadIndexed() != NULL) { | |
387 LoadIndexed *li = (LoadIndexed *)cur->as_LoadIndexed(); | |
388 cur_invariant = !_short_loop_optimizer->has_indexed_store(as_BasicType(cur->type())) && is_invariant(li->array()) && is_invariant(li->index()); | |
389 } | |
390 | |
391 if (cur_invariant) { | |
392 // perform value numbering and mark instruction as loop-invariant | |
393 _gvn->substitute(cur); | |
394 | |
395 if (cur->as_Constant() == NULL) { | |
396 // ensure that code for non-constant instructions is always generated | |
397 cur->pin(); | |
398 } | |
399 | |
400 // remove cur instruction from loop block and append it to block before loop | |
401 Instruction* next = cur->next(); | |
402 Instruction* in = _insertion_point->next(); | |
403 _insertion_point = _insertion_point->set_next(cur); | |
404 cur->set_next(in); | |
405 | |
406 // Deoptimize on exception | |
407 cur->set_flag(Instruction::DeoptimizeOnException, true); | |
408 | |
409 // Clear exception handlers | |
410 cur->set_exception_handlers(NULL); | |
411 | |
412 TRACE_VALUE_NUMBERING(tty->print_cr("Instruction %c%d is loop invariant", cur->type()->tchar(), cur->id())); | |
413 | |
414 if (cur->state_before() != NULL) { | |
415 cur->set_state_before(_state->copy()); | |
416 } | |
417 if (cur->exception_state() != NULL) { | |
418 cur->set_exception_state(_state->copy()); | |
419 } | |
420 | |
421 cur = prev->set_next(next); | |
422 | |
423 } else { | |
424 prev = cur; | |
425 cur = cur->next(); | |
426 } | |
427 } | |
428 } | |
0 | 429 |
430 bool ShortLoopOptimizer::process(BlockBegin* loop_header) { | |
431 TRACE_VALUE_NUMBERING(tty->print_cr("** loop header block")); | |
432 | |
433 _too_complicated_loop = false; | |
434 _loop_blocks.clear(); | |
435 _loop_blocks.append(loop_header); | |
436 | |
437 for (int i = 0; i < _loop_blocks.length(); i++) { | |
438 BlockBegin* block = _loop_blocks.at(i); | |
439 TRACE_VALUE_NUMBERING(tty->print_cr("processing loop block B%d", block->block_id())); | |
440 | |
441 if (block->is_set(BlockBegin::exception_entry_flag)) { | |
442 // this would be too complicated | |
443 return false; | |
444 } | |
445 | |
446 // add predecessors to worklist | |
447 for (int j = block->number_of_preds() - 1; j >= 0; j--) { | |
448 BlockBegin* pred = block->pred_at(j); | |
449 | |
8860 | 450 if (pred->is_set(BlockBegin::osr_entry_flag)) { |
451 return false; | |
452 } | |
453 | |
0 | 454 ValueMap* pred_map = value_map_of(pred); |
455 if (pred_map != NULL) { | |
456 current_map()->kill_map(pred_map); | |
457 } else if (!_loop_blocks.contains(pred)) { | |
458 if (_loop_blocks.length() >= ValueMapMaxLoopSize) { | |
459 return false; | |
460 } | |
461 _loop_blocks.append(pred); | |
462 } | |
463 } | |
464 | |
465 // use the instruction visitor for killing values | |
466 for (Value instr = block->next(); instr != NULL; instr = instr->next()) { | |
467 instr->visit(this); | |
468 if (_too_complicated_loop) { | |
469 return false; | |
470 } | |
471 } | |
472 } | |
473 | |
8860 | 474 bool optimistic = this->_gvn->compilation()->is_optimistic(); |
475 | |
476 if (UseLoopInvariantCodeMotion && optimistic) { | |
477 LoopInvariantCodeMotion code_motion(this, _gvn, loop_header, &_loop_blocks); | |
478 } | |
479 | |
0 | 480 TRACE_VALUE_NUMBERING(tty->print_cr("** loop successfully optimized")); |
481 return true; | |
482 } | |
483 | |
484 | |
485 GlobalValueNumbering::GlobalValueNumbering(IR* ir) | |
486 : _current_map(NULL) | |
487 , _value_maps(ir->linear_scan_order()->length(), NULL) | |
8860 | 488 , _compilation(ir->compilation()) |
0 | 489 { |
490 TRACE_VALUE_NUMBERING(tty->print_cr("****** start of global value numbering")); | |
491 | |
492 ShortLoopOptimizer short_loop_optimizer(this); | |
493 | |
494 BlockList* blocks = ir->linear_scan_order(); | |
495 int num_blocks = blocks->length(); | |
496 | |
497 BlockBegin* start_block = blocks->at(0); | |
498 assert(start_block == ir->start() && start_block->number_of_preds() == 0 && start_block->dominator() == NULL, "must be start block"); | |
499 assert(start_block->next()->as_Base() != NULL && start_block->next()->next() == NULL, "start block must not have instructions"); | |
500 | |
8860 | 501 // method parameters are not linked in instructions list, so process them separateley |
502 for_each_state_value(start_block->state(), value, | |
503 assert(value->as_Local() != NULL, "only method parameters allowed"); | |
504 set_processed(value); | |
505 ); | |
506 | |
0 | 507 // initial, empty value map with nesting 0 |
508 set_value_map_of(start_block, new ValueMap()); | |
509 | |
510 for (int i = 1; i < num_blocks; i++) { | |
511 BlockBegin* block = blocks->at(i); | |
512 TRACE_VALUE_NUMBERING(tty->print_cr("**** processing block B%d", block->block_id())); | |
513 | |
514 int num_preds = block->number_of_preds(); | |
515 assert(num_preds > 0, "block must have predecessors"); | |
516 | |
517 BlockBegin* dominator = block->dominator(); | |
518 assert(dominator != NULL, "dominator must exist"); | |
519 assert(value_map_of(dominator) != NULL, "value map of dominator must exist"); | |
520 | |
521 // create new value map with increased nesting | |
522 _current_map = new ValueMap(value_map_of(dominator)); | |
523 | |
8860 | 524 if (num_preds == 1 && !block->is_set(BlockBegin::exception_entry_flag)) { |
0 | 525 assert(dominator == block->pred_at(0), "dominator must be equal to predecessor"); |
526 // nothing to do here | |
527 | |
528 } else if (block->is_set(BlockBegin::linear_scan_loop_header_flag)) { | |
529 // block has incoming backward branches -> try to optimize short loops | |
530 if (!short_loop_optimizer.process(block)) { | |
531 // loop is too complicated, so kill all memory loads because there might be | |
532 // stores to them in the loop | |
533 current_map()->kill_memory(); | |
534 } | |
535 | |
536 } else { | |
537 // only incoming forward branches that are already processed | |
538 for (int j = 0; j < num_preds; j++) { | |
539 BlockBegin* pred = block->pred_at(j); | |
540 ValueMap* pred_map = value_map_of(pred); | |
541 | |
542 if (pred_map != NULL) { | |
543 // propagate killed values of the predecessor to this block | |
544 current_map()->kill_map(value_map_of(pred)); | |
545 } else { | |
546 // kill all memory loads because predecessor not yet processed | |
547 // (this can happen with non-natural loops and OSR-compiles) | |
548 current_map()->kill_memory(); | |
549 } | |
550 } | |
551 } | |
552 | |
8860 | 553 // phi functions are not linked in instructions list, so process them separateley |
554 for_each_phi_fun(block, phi, | |
555 set_processed(phi); | |
556 ); | |
0 | 557 |
558 TRACE_VALUE_NUMBERING(tty->print("value map before processing block: "); current_map()->print()); | |
559 | |
560 // visit all instructions of this block | |
561 for (Value instr = block->next(); instr != NULL; instr = instr->next()) { | |
562 // check if instruction kills any values | |
563 instr->visit(this); | |
8860 | 564 // perform actual value numbering |
565 substitute(instr); | |
0 | 566 } |
567 | |
568 // remember value map for successors | |
569 set_value_map_of(block, current_map()); | |
570 } | |
571 | |
8860 | 572 if (_has_substitutions) { |
0 | 573 SubstitutionResolver resolver(ir); |
574 } | |
575 | |
576 TRACE_VALUE_NUMBERING(tty->print("****** end of global value numbering. "); ValueMap::print_statistics()); | |
577 } | |
8860 | 578 |
579 void GlobalValueNumbering::substitute(Instruction* instr) { | |
580 assert(!instr->has_subst(), "substitution already set"); | |
581 Value subst = current_map()->find_insert(instr); | |
582 if (subst != instr) { | |
583 assert(!subst->has_subst(), "can't have a substitution"); | |
584 | |
585 TRACE_VALUE_NUMBERING(tty->print_cr("substitution for %d set to %d", instr->id(), subst->id())); | |
586 instr->set_subst(subst); | |
587 _has_substitutions = true; | |
588 } | |
589 set_processed(instr); | |
590 } |