src/share/vm/c1/c1_ValueMap.cpp

Wed, 27 Aug 2008 00:21:55 -0700

author
never
date
Wed, 27 Aug 2008 00:21:55 -0700
changeset 739
dc7f315e41f7
parent 435
a61af66fc99e
child 1907
c18cbe5936b8
permissions
-rw-r--r--

5108146: Merge i486 and amd64 cpu directories
6459804: Want client (c1) compiler for x86_64 (amd64) for faster start-up
Reviewed-by: kvn

duke@435 1 /*
duke@435 2 * Copyright 1999-2005 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 #include "incls/_precompiled.incl"
duke@435 26 #include "incls/_c1_ValueMap.cpp.incl"
duke@435 27
duke@435 28
duke@435 29 #ifndef PRODUCT
duke@435 30
duke@435 31 int ValueMap::_number_of_finds = 0;
duke@435 32 int ValueMap::_number_of_hits = 0;
duke@435 33 int ValueMap::_number_of_kills = 0;
duke@435 34
duke@435 35 #define TRACE_VALUE_NUMBERING(code) if (PrintValueNumbering) { code; }
duke@435 36
duke@435 37 #else
duke@435 38
duke@435 39 #define TRACE_VALUE_NUMBERING(code)
duke@435 40
duke@435 41 #endif
duke@435 42
duke@435 43
duke@435 44 ValueMap::ValueMap()
duke@435 45 : _nesting(0)
duke@435 46 , _entries(ValueMapInitialSize, NULL)
duke@435 47 , _killed_values()
duke@435 48 , _entry_count(0)
duke@435 49 {
duke@435 50 NOT_PRODUCT(reset_statistics());
duke@435 51 }
duke@435 52
duke@435 53
duke@435 54 ValueMap::ValueMap(ValueMap* old)
duke@435 55 : _nesting(old->_nesting + 1)
duke@435 56 , _entries(old->_entries.length())
duke@435 57 , _killed_values()
duke@435 58 , _entry_count(old->_entry_count)
duke@435 59 {
duke@435 60 for (int i = size() - 1; i >= 0; i--) {
duke@435 61 _entries.at_put(i, old->entry_at(i));
duke@435 62 }
duke@435 63 _killed_values.set_from(&old->_killed_values);
duke@435 64 }
duke@435 65
duke@435 66
duke@435 67 void ValueMap::increase_table_size() {
duke@435 68 int old_size = size();
duke@435 69 int new_size = old_size * 2 + 1;
duke@435 70
duke@435 71 ValueMapEntryList worklist(8);
duke@435 72 ValueMapEntryArray new_entries(new_size, NULL);
duke@435 73 int new_entry_count = 0;
duke@435 74
duke@435 75 TRACE_VALUE_NUMBERING(tty->print_cr("increasing table size from %d to %d", old_size, new_size));
duke@435 76
duke@435 77 for (int i = old_size - 1; i >= 0; i--) {
duke@435 78 ValueMapEntry* entry;
duke@435 79 for (entry = entry_at(i); entry != NULL; entry = entry->next()) {
duke@435 80 if (!is_killed(entry->value())) {
duke@435 81 worklist.push(entry);
duke@435 82 }
duke@435 83 }
duke@435 84
duke@435 85 while (!worklist.is_empty()) {
duke@435 86 entry = worklist.pop();
duke@435 87 int new_index = entry_index(entry->hash(), new_size);
duke@435 88
duke@435 89 if (entry->nesting() != nesting() && new_entries.at(new_index) != entry->next()) {
duke@435 90 // changing entries with a lower nesting than the current nesting of the table
duke@435 91 // is not allowed because then the same entry is contained in multiple value maps.
duke@435 92 // clone entry when next-pointer must be changed
duke@435 93 entry = new ValueMapEntry(entry->hash(), entry->value(), entry->nesting(), NULL);
duke@435 94 }
duke@435 95 entry->set_next(new_entries.at(new_index));
duke@435 96 new_entries.at_put(new_index, entry);
duke@435 97 new_entry_count++;
duke@435 98 }
duke@435 99 }
duke@435 100
duke@435 101 _entries = new_entries;
duke@435 102 _entry_count = new_entry_count;
duke@435 103 }
duke@435 104
duke@435 105
duke@435 106 Value ValueMap::find_insert(Value x) {
duke@435 107 const intx hash = x->hash();
duke@435 108 if (hash != 0) {
duke@435 109 // 0 hash means: exclude from value numbering
duke@435 110 NOT_PRODUCT(_number_of_finds++);
duke@435 111
duke@435 112 for (ValueMapEntry* entry = entry_at(entry_index(hash, size())); entry != NULL; entry = entry->next()) {
duke@435 113 if (entry->hash() == hash) {
duke@435 114 Value f = entry->value();
duke@435 115
duke@435 116 if (!is_killed(f) && f->is_equal(x)) {
duke@435 117 NOT_PRODUCT(_number_of_hits++);
duke@435 118 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()));
duke@435 119
duke@435 120 if (entry->nesting() != nesting() && f->as_Constant() == NULL) {
duke@435 121 // non-constant values of of another block must be pinned,
duke@435 122 // otherwise it is possible that they are not evaluated
duke@435 123 f->pin(Instruction::PinGlobalValueNumbering);
duke@435 124 }
duke@435 125
duke@435 126 return f;
duke@435 127
duke@435 128 }
duke@435 129 }
duke@435 130 }
duke@435 131
duke@435 132 // x not found, so insert it
duke@435 133 if (entry_count() >= size_threshold()) {
duke@435 134 increase_table_size();
duke@435 135 }
duke@435 136 int idx = entry_index(hash, size());
duke@435 137 _entries.at_put(idx, new ValueMapEntry(hash, x, nesting(), entry_at(idx)));
duke@435 138 _entry_count++;
duke@435 139
duke@435 140 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()));
duke@435 141 }
duke@435 142
duke@435 143 return x;
duke@435 144 }
duke@435 145
duke@435 146
duke@435 147 #define GENERIC_KILL_VALUE(must_kill_implementation) \
duke@435 148 NOT_PRODUCT(_number_of_kills++); \
duke@435 149 \
duke@435 150 for (int i = size() - 1; i >= 0; i--) { \
duke@435 151 ValueMapEntry* prev_entry = NULL; \
duke@435 152 for (ValueMapEntry* entry = entry_at(i); entry != NULL; entry = entry->next()) { \
duke@435 153 Value value = entry->value(); \
duke@435 154 \
duke@435 155 must_kill_implementation(must_kill, entry, value) \
duke@435 156 \
duke@435 157 if (must_kill) { \
duke@435 158 kill_value(value); \
duke@435 159 \
duke@435 160 if (prev_entry == NULL) { \
duke@435 161 _entries.at_put(i, entry->next()); \
duke@435 162 _entry_count--; \
duke@435 163 } else if (prev_entry->nesting() == nesting()) { \
duke@435 164 prev_entry->set_next(entry->next()); \
duke@435 165 _entry_count--; \
duke@435 166 } else { \
duke@435 167 prev_entry = entry; \
duke@435 168 } \
duke@435 169 \
duke@435 170 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())); \
duke@435 171 } else { \
duke@435 172 prev_entry = entry; \
duke@435 173 } \
duke@435 174 } \
duke@435 175 } \
duke@435 176
duke@435 177 #define MUST_KILL_MEMORY(must_kill, entry, value) \
duke@435 178 bool must_kill = value->as_LoadField() != NULL || value->as_LoadIndexed() != NULL;
duke@435 179
duke@435 180 #define MUST_KILL_ARRAY(must_kill, entry, value) \
duke@435 181 bool must_kill = value->as_LoadIndexed() != NULL \
duke@435 182 && value->type()->tag() == type->tag();
duke@435 183
duke@435 184 #define MUST_KILL_FIELD(must_kill, entry, value) \
duke@435 185 /* ciField's are not unique; must compare their contents */ \
duke@435 186 LoadField* lf = value->as_LoadField(); \
duke@435 187 bool must_kill = lf != NULL \
duke@435 188 && lf->field()->holder() == field->holder() \
duke@435 189 && lf->field()->offset() == field->offset();
duke@435 190
duke@435 191 #define MUST_KILL_EXCEPTION(must_kill, entry, value) \
duke@435 192 assert(entry->nesting() < nesting(), "must not find bigger nesting than current"); \
duke@435 193 bool must_kill = (entry->nesting() == nesting() - 1);
duke@435 194
duke@435 195
duke@435 196 void ValueMap::kill_memory() {
duke@435 197 GENERIC_KILL_VALUE(MUST_KILL_MEMORY);
duke@435 198 }
duke@435 199
duke@435 200 void ValueMap::kill_array(ValueType* type) {
duke@435 201 GENERIC_KILL_VALUE(MUST_KILL_ARRAY);
duke@435 202 }
duke@435 203
duke@435 204 void ValueMap::kill_field(ciField* field) {
duke@435 205 GENERIC_KILL_VALUE(MUST_KILL_FIELD);
duke@435 206 }
duke@435 207
duke@435 208 void ValueMap::kill_exception() {
duke@435 209 GENERIC_KILL_VALUE(MUST_KILL_EXCEPTION);
duke@435 210 }
duke@435 211
duke@435 212
duke@435 213 void ValueMap::kill_map(ValueMap* map) {
duke@435 214 assert(is_global_value_numbering(), "only for global value numbering");
duke@435 215 _killed_values.set_union(&map->_killed_values);
duke@435 216 }
duke@435 217
duke@435 218 void ValueMap::kill_all() {
duke@435 219 assert(is_local_value_numbering(), "only for local value numbering");
duke@435 220 for (int i = size() - 1; i >= 0; i--) {
duke@435 221 _entries.at_put(i, NULL);
duke@435 222 }
duke@435 223 _entry_count = 0;
duke@435 224 }
duke@435 225
duke@435 226
duke@435 227 #ifndef PRODUCT
duke@435 228
duke@435 229 void ValueMap::print() {
duke@435 230 tty->print_cr("(size %d, entries %d, nesting %d)", size(), entry_count(), nesting());
duke@435 231
duke@435 232 int entries = 0;
duke@435 233 for (int i = 0; i < size(); i++) {
duke@435 234 if (entry_at(i) != NULL) {
duke@435 235 tty->print(" %2d: ", i);
duke@435 236 for (ValueMapEntry* entry = entry_at(i); entry != NULL; entry = entry->next()) {
duke@435 237 Value value = entry->value();
duke@435 238 tty->print("%s %c%d (%s%d) -> ", value->name(), value->type()->tchar(), value->id(), is_killed(value) ? "x" : "", entry->nesting());
duke@435 239 entries++;
duke@435 240 }
duke@435 241 tty->print_cr("NULL");
duke@435 242 }
duke@435 243 }
duke@435 244
duke@435 245 _killed_values.print();
duke@435 246 assert(entry_count() == entries, "entry_count incorrect");
duke@435 247 }
duke@435 248
duke@435 249 void ValueMap::reset_statistics() {
duke@435 250 _number_of_finds = 0;
duke@435 251 _number_of_hits = 0;
duke@435 252 _number_of_kills = 0;
duke@435 253 }
duke@435 254
duke@435 255 void ValueMap::print_statistics() {
duke@435 256 float hit_rate = 0;
duke@435 257 if (_number_of_finds != 0) {
duke@435 258 hit_rate = (float)_number_of_hits / _number_of_finds;
duke@435 259 }
duke@435 260
duke@435 261 tty->print_cr("finds:%3d hits:%3d kills:%3d hit rate: %1.4f", _number_of_finds, _number_of_hits, _number_of_kills, hit_rate);
duke@435 262 }
duke@435 263
duke@435 264 #endif
duke@435 265
duke@435 266
duke@435 267
duke@435 268 class ShortLoopOptimizer : public ValueNumberingVisitor {
duke@435 269 private:
duke@435 270 GlobalValueNumbering* _gvn;
duke@435 271 BlockList _loop_blocks;
duke@435 272 bool _too_complicated_loop;
duke@435 273
duke@435 274 // simplified access to methods of GlobalValueNumbering
duke@435 275 ValueMap* current_map() { return _gvn->current_map(); }
duke@435 276 ValueMap* value_map_of(BlockBegin* block) { return _gvn->value_map_of(block); }
duke@435 277
duke@435 278 // implementation for abstract methods of ValueNumberingVisitor
duke@435 279 void kill_memory() { _too_complicated_loop = true; }
duke@435 280 void kill_field(ciField* field) { current_map()->kill_field(field); };
duke@435 281 void kill_array(ValueType* type) { current_map()->kill_array(type); };
duke@435 282
duke@435 283 public:
duke@435 284 ShortLoopOptimizer(GlobalValueNumbering* gvn)
duke@435 285 : _gvn(gvn)
duke@435 286 , _loop_blocks(ValueMapMaxLoopSize)
duke@435 287 , _too_complicated_loop(false)
duke@435 288 {
duke@435 289 }
duke@435 290
duke@435 291 bool process(BlockBegin* loop_header);
duke@435 292 };
duke@435 293
duke@435 294
duke@435 295 bool ShortLoopOptimizer::process(BlockBegin* loop_header) {
duke@435 296 TRACE_VALUE_NUMBERING(tty->print_cr("** loop header block"));
duke@435 297
duke@435 298 _too_complicated_loop = false;
duke@435 299 _loop_blocks.clear();
duke@435 300 _loop_blocks.append(loop_header);
duke@435 301
duke@435 302 for (int i = 0; i < _loop_blocks.length(); i++) {
duke@435 303 BlockBegin* block = _loop_blocks.at(i);
duke@435 304 TRACE_VALUE_NUMBERING(tty->print_cr("processing loop block B%d", block->block_id()));
duke@435 305
duke@435 306 if (block->is_set(BlockBegin::exception_entry_flag)) {
duke@435 307 // this would be too complicated
duke@435 308 return false;
duke@435 309 }
duke@435 310
duke@435 311 // add predecessors to worklist
duke@435 312 for (int j = block->number_of_preds() - 1; j >= 0; j--) {
duke@435 313 BlockBegin* pred = block->pred_at(j);
duke@435 314
duke@435 315 ValueMap* pred_map = value_map_of(pred);
duke@435 316 if (pred_map != NULL) {
duke@435 317 current_map()->kill_map(pred_map);
duke@435 318 } else if (!_loop_blocks.contains(pred)) {
duke@435 319 if (_loop_blocks.length() >= ValueMapMaxLoopSize) {
duke@435 320 return false;
duke@435 321 }
duke@435 322 _loop_blocks.append(pred);
duke@435 323 }
duke@435 324 }
duke@435 325
duke@435 326 // use the instruction visitor for killing values
duke@435 327 for (Value instr = block->next(); instr != NULL; instr = instr->next()) {
duke@435 328 instr->visit(this);
duke@435 329 if (_too_complicated_loop) {
duke@435 330 return false;
duke@435 331 }
duke@435 332 }
duke@435 333 }
duke@435 334
duke@435 335 TRACE_VALUE_NUMBERING(tty->print_cr("** loop successfully optimized"));
duke@435 336 return true;
duke@435 337 }
duke@435 338
duke@435 339
duke@435 340 GlobalValueNumbering::GlobalValueNumbering(IR* ir)
duke@435 341 : _current_map(NULL)
duke@435 342 , _value_maps(ir->linear_scan_order()->length(), NULL)
duke@435 343 {
duke@435 344 TRACE_VALUE_NUMBERING(tty->print_cr("****** start of global value numbering"));
duke@435 345
duke@435 346 ShortLoopOptimizer short_loop_optimizer(this);
duke@435 347 int subst_count = 0;
duke@435 348
duke@435 349 BlockList* blocks = ir->linear_scan_order();
duke@435 350 int num_blocks = blocks->length();
duke@435 351
duke@435 352 BlockBegin* start_block = blocks->at(0);
duke@435 353 assert(start_block == ir->start() && start_block->number_of_preds() == 0 && start_block->dominator() == NULL, "must be start block");
duke@435 354 assert(start_block->next()->as_Base() != NULL && start_block->next()->next() == NULL, "start block must not have instructions");
duke@435 355
duke@435 356 // initial, empty value map with nesting 0
duke@435 357 set_value_map_of(start_block, new ValueMap());
duke@435 358
duke@435 359 for (int i = 1; i < num_blocks; i++) {
duke@435 360 BlockBegin* block = blocks->at(i);
duke@435 361 TRACE_VALUE_NUMBERING(tty->print_cr("**** processing block B%d", block->block_id()));
duke@435 362
duke@435 363 int num_preds = block->number_of_preds();
duke@435 364 assert(num_preds > 0, "block must have predecessors");
duke@435 365
duke@435 366 BlockBegin* dominator = block->dominator();
duke@435 367 assert(dominator != NULL, "dominator must exist");
duke@435 368 assert(value_map_of(dominator) != NULL, "value map of dominator must exist");
duke@435 369
duke@435 370 // create new value map with increased nesting
duke@435 371 _current_map = new ValueMap(value_map_of(dominator));
duke@435 372
duke@435 373 if (num_preds == 1) {
duke@435 374 assert(dominator == block->pred_at(0), "dominator must be equal to predecessor");
duke@435 375 // nothing to do here
duke@435 376
duke@435 377 } else if (block->is_set(BlockBegin::linear_scan_loop_header_flag)) {
duke@435 378 // block has incoming backward branches -> try to optimize short loops
duke@435 379 if (!short_loop_optimizer.process(block)) {
duke@435 380 // loop is too complicated, so kill all memory loads because there might be
duke@435 381 // stores to them in the loop
duke@435 382 current_map()->kill_memory();
duke@435 383 }
duke@435 384
duke@435 385 } else {
duke@435 386 // only incoming forward branches that are already processed
duke@435 387 for (int j = 0; j < num_preds; j++) {
duke@435 388 BlockBegin* pred = block->pred_at(j);
duke@435 389 ValueMap* pred_map = value_map_of(pred);
duke@435 390
duke@435 391 if (pred_map != NULL) {
duke@435 392 // propagate killed values of the predecessor to this block
duke@435 393 current_map()->kill_map(value_map_of(pred));
duke@435 394 } else {
duke@435 395 // kill all memory loads because predecessor not yet processed
duke@435 396 // (this can happen with non-natural loops and OSR-compiles)
duke@435 397 current_map()->kill_memory();
duke@435 398 }
duke@435 399 }
duke@435 400 }
duke@435 401
duke@435 402 if (block->is_set(BlockBegin::exception_entry_flag)) {
duke@435 403 current_map()->kill_exception();
duke@435 404 }
duke@435 405
duke@435 406 TRACE_VALUE_NUMBERING(tty->print("value map before processing block: "); current_map()->print());
duke@435 407
duke@435 408 // visit all instructions of this block
duke@435 409 for (Value instr = block->next(); instr != NULL; instr = instr->next()) {
duke@435 410 assert(!instr->has_subst(), "substitution already set");
duke@435 411
duke@435 412 // check if instruction kills any values
duke@435 413 instr->visit(this);
duke@435 414
duke@435 415 if (instr->hash() != 0) {
duke@435 416 Value f = current_map()->find_insert(instr);
duke@435 417 if (f != instr) {
duke@435 418 assert(!f->has_subst(), "can't have a substitution");
duke@435 419 instr->set_subst(f);
duke@435 420 subst_count++;
duke@435 421 }
duke@435 422 }
duke@435 423 }
duke@435 424
duke@435 425 // remember value map for successors
duke@435 426 set_value_map_of(block, current_map());
duke@435 427 }
duke@435 428
duke@435 429 if (subst_count != 0) {
duke@435 430 SubstitutionResolver resolver(ir);
duke@435 431 }
duke@435 432
duke@435 433 TRACE_VALUE_NUMBERING(tty->print("****** end of global value numbering. "); ValueMap::print_statistics());
duke@435 434 }

mercurial