Tue, 08 Aug 2017 15:57:29 +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 | #ifndef SHARE_VM_C1_C1_VALUEMAP_HPP |
aoqi@0 | 26 | #define SHARE_VM_C1_C1_VALUEMAP_HPP |
aoqi@0 | 27 | |
aoqi@0 | 28 | #include "c1/c1_Instruction.hpp" |
aoqi@0 | 29 | #include "c1/c1_ValueSet.hpp" |
aoqi@0 | 30 | #include "memory/allocation.hpp" |
aoqi@0 | 31 | |
aoqi@0 | 32 | class ValueMapEntry: public CompilationResourceObj { |
aoqi@0 | 33 | private: |
aoqi@0 | 34 | intx _hash; |
aoqi@0 | 35 | Value _value; |
aoqi@0 | 36 | int _nesting; |
aoqi@0 | 37 | ValueMapEntry* _next; |
aoqi@0 | 38 | |
aoqi@0 | 39 | public: |
aoqi@0 | 40 | ValueMapEntry(intx hash, Value value, int nesting, ValueMapEntry* next) |
aoqi@0 | 41 | : _hash(hash) |
aoqi@0 | 42 | , _value(value) |
aoqi@0 | 43 | , _nesting(nesting) |
aoqi@0 | 44 | , _next(next) |
aoqi@0 | 45 | { |
aoqi@0 | 46 | } |
aoqi@0 | 47 | |
aoqi@0 | 48 | intx hash() { return _hash; } |
aoqi@0 | 49 | Value value() { return _value; } |
aoqi@0 | 50 | int nesting() { return _nesting; } |
aoqi@0 | 51 | ValueMapEntry* next() { return _next; } |
aoqi@0 | 52 | |
aoqi@0 | 53 | void set_next(ValueMapEntry* next) { _next = next; } |
aoqi@0 | 54 | }; |
aoqi@0 | 55 | |
aoqi@0 | 56 | define_array(ValueMapEntryArray, ValueMapEntry*) |
aoqi@0 | 57 | define_stack(ValueMapEntryList, ValueMapEntryArray) |
aoqi@0 | 58 | |
aoqi@0 | 59 | // ValueMap implements nested hash tables for value numbering. It |
aoqi@0 | 60 | // maintains a set _killed_values which represents the instructions |
aoqi@0 | 61 | // which have been killed so far and an array of linked lists of |
aoqi@0 | 62 | // ValueMapEntries names _entries. Each ValueMapEntry has a nesting |
aoqi@0 | 63 | // which indicates what ValueMap nesting it belongs to. Higher |
aoqi@0 | 64 | // nesting values are always before lower values in the linked list. |
aoqi@0 | 65 | // This allows cloning of parent ValueMaps by simply copying the heads |
aoqi@0 | 66 | // of the list. _entry_count represents the number of reachable |
aoqi@0 | 67 | // entries in the ValueMap. A ValueMap is only allowed to mutate |
aoqi@0 | 68 | // ValueMapEntries with the same nesting level. Adding or removing |
aoqi@0 | 69 | // entries at the current nesting level requires updating |
aoqi@0 | 70 | // _entry_count. Elements in the parent's list that get killed can be |
aoqi@0 | 71 | // skipped if they are at the head of the list by simply moving to the |
aoqi@0 | 72 | // next element in the list and decrementing _entry_count. |
aoqi@0 | 73 | |
aoqi@0 | 74 | class ValueMap: public CompilationResourceObj { |
aoqi@0 | 75 | private: |
aoqi@0 | 76 | int _nesting; |
aoqi@0 | 77 | ValueMapEntryArray _entries; |
aoqi@0 | 78 | ValueSet _killed_values; |
aoqi@0 | 79 | int _entry_count; |
aoqi@0 | 80 | |
aoqi@0 | 81 | int nesting() { return _nesting; } |
aoqi@0 | 82 | bool is_local_value_numbering() { return _nesting == 0; } |
aoqi@0 | 83 | bool is_global_value_numbering() { return _nesting > 0; } |
aoqi@0 | 84 | |
aoqi@0 | 85 | int entry_count() { return _entry_count; } |
aoqi@0 | 86 | int size() { return _entries.length(); } |
aoqi@0 | 87 | ValueMapEntry* entry_at(int i) { return _entries.at(i); } |
aoqi@0 | 88 | |
aoqi@0 | 89 | // calculates the index of a hash value in a hash table of size n |
aoqi@0 | 90 | int entry_index(intx hash, int n) { return (unsigned int)hash % n; } |
aoqi@0 | 91 | |
aoqi@0 | 92 | // if entry_count > size_threshold, the size of the hash table is increased |
aoqi@0 | 93 | int size_threshold() { return size(); } |
aoqi@0 | 94 | |
aoqi@0 | 95 | // management of the killed-bitset for global value numbering |
aoqi@0 | 96 | void kill_value(Value v) { if (is_global_value_numbering()) _killed_values.put(v); } |
aoqi@0 | 97 | bool is_killed(Value v) { if (is_global_value_numbering()) return _killed_values.contains(v); else return false; } |
aoqi@0 | 98 | |
aoqi@0 | 99 | // helper functions |
aoqi@0 | 100 | void increase_table_size(); |
aoqi@0 | 101 | |
aoqi@0 | 102 | #ifndef PRODUCT |
aoqi@0 | 103 | static int _number_of_finds; |
aoqi@0 | 104 | static int _number_of_hits; |
aoqi@0 | 105 | static int _number_of_kills; |
aoqi@0 | 106 | #endif // PRODUCT |
aoqi@0 | 107 | |
aoqi@0 | 108 | public: |
aoqi@0 | 109 | // creation |
aoqi@0 | 110 | ValueMap(); // empty value map |
aoqi@0 | 111 | ValueMap(ValueMap* old); // value map with increased nesting |
aoqi@0 | 112 | |
aoqi@0 | 113 | // manipulation |
aoqi@0 | 114 | Value find_insert(Value x); |
aoqi@0 | 115 | |
aoqi@0 | 116 | void kill_memory(); |
aoqi@0 | 117 | void kill_field(ciField* field, bool all_offsets); |
aoqi@0 | 118 | void kill_array(ValueType* type); |
aoqi@0 | 119 | void kill_exception(); |
aoqi@0 | 120 | void kill_map(ValueMap* map); |
aoqi@0 | 121 | void kill_all(); |
aoqi@0 | 122 | |
aoqi@0 | 123 | #ifndef PRODUCT |
aoqi@0 | 124 | // debugging/printing |
aoqi@0 | 125 | void print(); |
aoqi@0 | 126 | |
aoqi@0 | 127 | static void reset_statistics(); |
aoqi@0 | 128 | static void print_statistics(); |
aoqi@0 | 129 | #endif |
aoqi@0 | 130 | }; |
aoqi@0 | 131 | |
aoqi@0 | 132 | define_array(ValueMapArray, ValueMap*) |
aoqi@0 | 133 | |
aoqi@0 | 134 | |
aoqi@0 | 135 | class ValueNumberingVisitor: public InstructionVisitor { |
aoqi@0 | 136 | protected: |
aoqi@0 | 137 | // called by visitor functions for instructions that kill values |
aoqi@0 | 138 | virtual void kill_memory() = 0; |
aoqi@0 | 139 | virtual void kill_field(ciField* field, bool all_offsets) = 0; |
aoqi@0 | 140 | virtual void kill_array(ValueType* type) = 0; |
aoqi@0 | 141 | |
aoqi@0 | 142 | // visitor functions |
aoqi@0 | 143 | void do_StoreField (StoreField* x) { |
aoqi@0 | 144 | if (x->is_init_point() || // putstatic is an initialization point so treat it as a wide kill |
aoqi@0 | 145 | // This is actually too strict and the JMM doesn't require |
aoqi@0 | 146 | // this in all cases (e.g. load a; volatile store b; load a) |
aoqi@0 | 147 | // but possible future optimizations might require this. |
aoqi@0 | 148 | x->field()->is_volatile()) { |
aoqi@0 | 149 | kill_memory(); |
aoqi@0 | 150 | } else { |
aoqi@0 | 151 | kill_field(x->field(), x->needs_patching()); |
aoqi@0 | 152 | } |
aoqi@0 | 153 | } |
aoqi@0 | 154 | void do_StoreIndexed (StoreIndexed* x) { kill_array(x->type()); } |
aoqi@0 | 155 | void do_MonitorEnter (MonitorEnter* x) { kill_memory(); } |
aoqi@0 | 156 | void do_MonitorExit (MonitorExit* x) { kill_memory(); } |
aoqi@0 | 157 | void do_Invoke (Invoke* x) { kill_memory(); } |
aoqi@0 | 158 | void do_UnsafePutRaw (UnsafePutRaw* x) { kill_memory(); } |
aoqi@0 | 159 | void do_UnsafePutObject(UnsafePutObject* x) { kill_memory(); } |
aoqi@0 | 160 | void do_UnsafeGetAndSetObject(UnsafeGetAndSetObject* x) { kill_memory(); } |
aoqi@0 | 161 | void do_Intrinsic (Intrinsic* x) { if (!x->preserves_state()) kill_memory(); } |
aoqi@0 | 162 | |
aoqi@0 | 163 | void do_Phi (Phi* x) { /* nothing to do */ } |
aoqi@0 | 164 | void do_Local (Local* x) { /* nothing to do */ } |
aoqi@0 | 165 | void do_Constant (Constant* x) { /* nothing to do */ } |
aoqi@0 | 166 | void do_LoadField (LoadField* x) { |
aoqi@0 | 167 | if (x->is_init_point() || // getstatic is an initialization point so treat it as a wide kill |
aoqi@0 | 168 | x->field()->is_volatile()) { // the JMM requires this |
aoqi@0 | 169 | kill_memory(); |
aoqi@0 | 170 | } |
aoqi@0 | 171 | } |
aoqi@0 | 172 | void do_ArrayLength (ArrayLength* x) { /* nothing to do */ } |
aoqi@0 | 173 | void do_LoadIndexed (LoadIndexed* x) { /* nothing to do */ } |
aoqi@0 | 174 | void do_NegateOp (NegateOp* x) { /* nothing to do */ } |
aoqi@0 | 175 | void do_ArithmeticOp (ArithmeticOp* x) { /* nothing to do */ } |
aoqi@0 | 176 | void do_ShiftOp (ShiftOp* x) { /* nothing to do */ } |
aoqi@0 | 177 | void do_LogicOp (LogicOp* x) { /* nothing to do */ } |
aoqi@0 | 178 | void do_CompareOp (CompareOp* x) { /* nothing to do */ } |
aoqi@0 | 179 | void do_IfOp (IfOp* x) { /* nothing to do */ } |
aoqi@0 | 180 | void do_Convert (Convert* x) { /* nothing to do */ } |
aoqi@0 | 181 | void do_NullCheck (NullCheck* x) { /* nothing to do */ } |
aoqi@0 | 182 | void do_TypeCast (TypeCast* x) { /* nothing to do */ } |
aoqi@0 | 183 | void do_NewInstance (NewInstance* x) { /* nothing to do */ } |
aoqi@0 | 184 | void do_NewTypeArray (NewTypeArray* x) { /* nothing to do */ } |
aoqi@0 | 185 | void do_NewObjectArray (NewObjectArray* x) { /* nothing to do */ } |
aoqi@0 | 186 | void do_NewMultiArray (NewMultiArray* x) { /* nothing to do */ } |
aoqi@0 | 187 | void do_CheckCast (CheckCast* x) { /* nothing to do */ } |
aoqi@0 | 188 | void do_InstanceOf (InstanceOf* x) { /* nothing to do */ } |
aoqi@0 | 189 | void do_BlockBegin (BlockBegin* x) { /* nothing to do */ } |
aoqi@0 | 190 | void do_Goto (Goto* x) { /* nothing to do */ } |
aoqi@0 | 191 | void do_If (If* x) { /* nothing to do */ } |
aoqi@0 | 192 | void do_IfInstanceOf (IfInstanceOf* x) { /* nothing to do */ } |
aoqi@0 | 193 | void do_TableSwitch (TableSwitch* x) { /* nothing to do */ } |
aoqi@0 | 194 | void do_LookupSwitch (LookupSwitch* x) { /* nothing to do */ } |
aoqi@0 | 195 | void do_Return (Return* x) { /* nothing to do */ } |
aoqi@0 | 196 | void do_Throw (Throw* x) { /* nothing to do */ } |
aoqi@0 | 197 | void do_Base (Base* x) { /* nothing to do */ } |
aoqi@0 | 198 | void do_OsrEntry (OsrEntry* x) { /* nothing to do */ } |
aoqi@0 | 199 | void do_ExceptionObject(ExceptionObject* x) { /* nothing to do */ } |
aoqi@0 | 200 | void do_RoundFP (RoundFP* x) { /* nothing to do */ } |
aoqi@0 | 201 | void do_UnsafeGetRaw (UnsafeGetRaw* x) { /* nothing to do */ } |
aoqi@0 | 202 | void do_UnsafeGetObject(UnsafeGetObject* x) { /* nothing to do */ } |
aoqi@0 | 203 | void do_UnsafePrefetchRead (UnsafePrefetchRead* x) { /* nothing to do */ } |
aoqi@0 | 204 | void do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) { /* nothing to do */ } |
aoqi@0 | 205 | void do_ProfileCall (ProfileCall* x) { /* nothing to do */ } |
aoqi@0 | 206 | void do_ProfileReturnType (ProfileReturnType* x) { /* nothing to do */ } |
aoqi@0 | 207 | void do_ProfileInvoke (ProfileInvoke* x) { /* nothing to do */ }; |
aoqi@0 | 208 | void do_RuntimeCall (RuntimeCall* x) { /* nothing to do */ }; |
aoqi@0 | 209 | void do_MemBar (MemBar* x) { /* nothing to do */ }; |
aoqi@0 | 210 | void do_RangeCheckPredicate(RangeCheckPredicate* x) { /* nothing to do */ }; |
aoqi@0 | 211 | #ifdef ASSERT |
aoqi@0 | 212 | void do_Assert (Assert* x) { /* nothing to do */ }; |
aoqi@0 | 213 | #endif |
aoqi@0 | 214 | }; |
aoqi@0 | 215 | |
aoqi@0 | 216 | |
aoqi@0 | 217 | class ValueNumberingEffects: public ValueNumberingVisitor { |
aoqi@0 | 218 | private: |
aoqi@0 | 219 | ValueMap* _map; |
aoqi@0 | 220 | |
aoqi@0 | 221 | public: |
aoqi@0 | 222 | // implementation for abstract methods of ValueNumberingVisitor |
aoqi@0 | 223 | void kill_memory() { _map->kill_memory(); } |
aoqi@0 | 224 | void kill_field(ciField* field, bool all_offsets) { _map->kill_field(field, all_offsets); } |
aoqi@0 | 225 | void kill_array(ValueType* type) { _map->kill_array(type); } |
aoqi@0 | 226 | |
aoqi@0 | 227 | ValueNumberingEffects(ValueMap* map): _map(map) {} |
aoqi@0 | 228 | }; |
aoqi@0 | 229 | |
aoqi@0 | 230 | |
aoqi@0 | 231 | class GlobalValueNumbering: public ValueNumberingVisitor { |
aoqi@0 | 232 | private: |
aoqi@0 | 233 | Compilation* _compilation; // compilation data |
aoqi@0 | 234 | ValueMap* _current_map; // value map of current block |
aoqi@0 | 235 | ValueMapArray _value_maps; // list of value maps for all blocks |
aoqi@0 | 236 | ValueSet _processed_values; // marker for instructions that were already processed |
aoqi@0 | 237 | bool _has_substitutions; // set to true when substitutions must be resolved |
aoqi@0 | 238 | |
aoqi@0 | 239 | public: |
aoqi@0 | 240 | // accessors |
aoqi@0 | 241 | Compilation* compilation() const { return _compilation; } |
aoqi@0 | 242 | ValueMap* current_map() { return _current_map; } |
aoqi@0 | 243 | ValueMap* value_map_of(BlockBegin* block) { return _value_maps.at(block->linear_scan_number()); } |
aoqi@0 | 244 | void set_value_map_of(BlockBegin* block, ValueMap* map) { assert(value_map_of(block) == NULL, ""); _value_maps.at_put(block->linear_scan_number(), map); } |
aoqi@0 | 245 | |
aoqi@0 | 246 | bool is_processed(Value v) { return _processed_values.contains(v); } |
aoqi@0 | 247 | void set_processed(Value v) { _processed_values.put(v); } |
aoqi@0 | 248 | |
aoqi@0 | 249 | // implementation for abstract methods of ValueNumberingVisitor |
aoqi@0 | 250 | void kill_memory() { current_map()->kill_memory(); } |
aoqi@0 | 251 | void kill_field(ciField* field, bool all_offsets) { current_map()->kill_field(field, all_offsets); } |
aoqi@0 | 252 | void kill_array(ValueType* type) { current_map()->kill_array(type); } |
aoqi@0 | 253 | |
aoqi@0 | 254 | // main entry point that performs global value numbering |
aoqi@0 | 255 | GlobalValueNumbering(IR* ir); |
aoqi@0 | 256 | void substitute(Instruction* instr); // substitute instruction if it is contained in current value map |
aoqi@0 | 257 | }; |
aoqi@0 | 258 | |
aoqi@0 | 259 | #endif // SHARE_VM_C1_C1_VALUEMAP_HPP |