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