duke@435: /* never@2486: * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_C1_C1_VALUEMAP_HPP stefank@2314: #define SHARE_VM_C1_C1_VALUEMAP_HPP stefank@2314: stefank@2314: #include "c1/c1_Instruction.hpp" stefank@2314: #include "c1/c1_ValueSet.hpp" stefank@2314: #include "memory/allocation.hpp" stefank@2314: duke@435: class ValueMapEntry: public CompilationResourceObj { duke@435: private: duke@435: intx _hash; duke@435: Value _value; duke@435: int _nesting; duke@435: ValueMapEntry* _next; duke@435: duke@435: public: duke@435: ValueMapEntry(intx hash, Value value, int nesting, ValueMapEntry* next) duke@435: : _hash(hash) duke@435: , _value(value) duke@435: , _nesting(nesting) duke@435: , _next(next) duke@435: { duke@435: } duke@435: duke@435: intx hash() { return _hash; } duke@435: Value value() { return _value; } duke@435: int nesting() { return _nesting; } duke@435: ValueMapEntry* next() { return _next; } duke@435: duke@435: void set_next(ValueMapEntry* next) { _next = next; } duke@435: }; duke@435: duke@435: define_array(ValueMapEntryArray, ValueMapEntry*) duke@435: define_stack(ValueMapEntryList, ValueMapEntryArray) duke@435: duke@435: // ValueMap implements nested hash tables for value numbering. It duke@435: // maintains a set _killed_values which represents the instructions duke@435: // which have been killed so far and an array of linked lists of duke@435: // ValueMapEntries names _entries. Each ValueMapEntry has a nesting duke@435: // which indicates what ValueMap nesting it belongs to. Higher duke@435: // nesting values are always before lower values in the linked list. duke@435: // This allows cloning of parent ValueMaps by simply copying the heads duke@435: // of the list. _entry_count represents the number of reachable duke@435: // entries in the ValueMap. A ValueMap is only allowed to mutate duke@435: // ValueMapEntries with the same nesting level. Adding or removing duke@435: // entries at the current nesting level requires updating duke@435: // _entry_count. Elements in the parent's list that get killed can be duke@435: // skipped if they are at the head of the list by simply moving to the duke@435: // next element in the list and decrementing _entry_count. duke@435: duke@435: class ValueMap: public CompilationResourceObj { duke@435: private: duke@435: int _nesting; duke@435: ValueMapEntryArray _entries; duke@435: ValueSet _killed_values; duke@435: int _entry_count; duke@435: duke@435: int nesting() { return _nesting; } duke@435: bool is_local_value_numbering() { return _nesting == 0; } duke@435: bool is_global_value_numbering() { return _nesting > 0; } duke@435: duke@435: int entry_count() { return _entry_count; } duke@435: int size() { return _entries.length(); } duke@435: ValueMapEntry* entry_at(int i) { return _entries.at(i); } duke@435: duke@435: // calculates the index of a hash value in a hash table of size n duke@435: int entry_index(intx hash, int n) { return (unsigned int)hash % n; } duke@435: duke@435: // if entry_count > size_threshold, the size of the hash table is increased duke@435: int size_threshold() { return size(); } duke@435: duke@435: // management of the killed-bitset for global value numbering duke@435: void kill_value(Value v) { if (is_global_value_numbering()) _killed_values.put(v); } duke@435: bool is_killed(Value v) { if (is_global_value_numbering()) return _killed_values.contains(v); else return false; } duke@435: duke@435: // helper functions duke@435: void increase_table_size(); duke@435: duke@435: #ifndef PRODUCT duke@435: static int _number_of_finds; duke@435: static int _number_of_hits; duke@435: static int _number_of_kills; duke@435: #endif // PRODUCT duke@435: duke@435: public: duke@435: // creation duke@435: ValueMap(); // empty value map duke@435: ValueMap(ValueMap* old); // value map with increased nesting duke@435: duke@435: // manipulation duke@435: Value find_insert(Value x); duke@435: duke@435: void kill_memory(); duke@435: void kill_field(ciField* field); duke@435: void kill_array(ValueType* type); duke@435: void kill_exception(); duke@435: void kill_map(ValueMap* map); duke@435: void kill_all(); duke@435: duke@435: #ifndef PRODUCT duke@435: // debugging/printing duke@435: void print(); duke@435: duke@435: static void reset_statistics(); duke@435: static void print_statistics(); duke@435: #endif duke@435: }; duke@435: duke@435: define_array(ValueMapArray, ValueMap*) duke@435: duke@435: duke@435: class ValueNumberingVisitor: public InstructionVisitor { duke@435: protected: duke@435: // called by visitor functions for instructions that kill values duke@435: virtual void kill_memory() = 0; duke@435: virtual void kill_field(ciField* field) = 0; duke@435: virtual void kill_array(ValueType* type) = 0; duke@435: duke@435: // visitor functions never@894: void do_StoreField (StoreField* x) { never@894: if (!x->is_initialized()) { never@894: kill_memory(); never@894: } else { never@894: kill_field(x->field()); never@894: } never@894: } never@894: void do_StoreIndexed (StoreIndexed* x) { kill_array(x->type()); } never@894: void do_MonitorEnter (MonitorEnter* x) { kill_memory(); } never@894: void do_MonitorExit (MonitorExit* x) { kill_memory(); } never@894: void do_Invoke (Invoke* x) { kill_memory(); } never@894: void do_UnsafePutRaw (UnsafePutRaw* x) { kill_memory(); } never@894: void do_UnsafePutObject(UnsafePutObject* x) { kill_memory(); } never@894: void do_Intrinsic (Intrinsic* x) { if (!x->preserves_state()) kill_memory(); } duke@435: never@894: void do_Phi (Phi* x) { /* nothing to do */ } never@894: void do_Local (Local* x) { /* nothing to do */ } never@894: void do_Constant (Constant* x) { /* nothing to do */ } never@894: void do_LoadField (LoadField* x) { never@894: if (!x->is_initialized()) { never@894: kill_memory(); never@894: } never@894: } never@894: void do_ArrayLength (ArrayLength* x) { /* nothing to do */ } never@894: void do_LoadIndexed (LoadIndexed* x) { /* nothing to do */ } never@894: void do_NegateOp (NegateOp* x) { /* nothing to do */ } never@894: void do_ArithmeticOp (ArithmeticOp* x) { /* nothing to do */ } never@894: void do_ShiftOp (ShiftOp* x) { /* nothing to do */ } never@894: void do_LogicOp (LogicOp* x) { /* nothing to do */ } never@894: void do_CompareOp (CompareOp* x) { /* nothing to do */ } never@894: void do_IfOp (IfOp* x) { /* nothing to do */ } never@894: void do_Convert (Convert* x) { /* nothing to do */ } never@894: void do_NullCheck (NullCheck* x) { /* nothing to do */ } never@894: void do_NewInstance (NewInstance* x) { /* nothing to do */ } never@894: void do_NewTypeArray (NewTypeArray* x) { /* nothing to do */ } never@894: void do_NewObjectArray (NewObjectArray* x) { /* nothing to do */ } never@894: void do_NewMultiArray (NewMultiArray* x) { /* nothing to do */ } never@894: void do_CheckCast (CheckCast* x) { /* nothing to do */ } never@894: void do_InstanceOf (InstanceOf* x) { /* nothing to do */ } never@894: void do_BlockBegin (BlockBegin* x) { /* nothing to do */ } never@894: void do_Goto (Goto* x) { /* nothing to do */ } never@894: void do_If (If* x) { /* nothing to do */ } never@894: void do_IfInstanceOf (IfInstanceOf* x) { /* nothing to do */ } never@894: void do_TableSwitch (TableSwitch* x) { /* nothing to do */ } never@894: void do_LookupSwitch (LookupSwitch* x) { /* nothing to do */ } never@894: void do_Return (Return* x) { /* nothing to do */ } never@894: void do_Throw (Throw* x) { /* nothing to do */ } never@894: void do_Base (Base* x) { /* nothing to do */ } never@894: void do_OsrEntry (OsrEntry* x) { /* nothing to do */ } never@894: void do_ExceptionObject(ExceptionObject* x) { /* nothing to do */ } never@894: void do_RoundFP (RoundFP* x) { /* nothing to do */ } never@894: void do_UnsafeGetRaw (UnsafeGetRaw* x) { /* nothing to do */ } never@894: void do_UnsafeGetObject(UnsafeGetObject* x) { /* nothing to do */ } never@894: void do_UnsafePrefetchRead (UnsafePrefetchRead* x) { /* nothing to do */ } never@894: void do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) { /* nothing to do */ } never@894: void do_ProfileCall (ProfileCall* x) { /* nothing to do */ } never@2486: void do_ProfileInvoke (ProfileInvoke* x) { /* nothing to do */ }; never@2486: void do_RuntimeCall (RuntimeCall* x) { /* nothing to do */ }; never@894: }; never@894: never@894: never@894: class ValueNumberingEffects: public ValueNumberingVisitor { never@894: private: never@894: ValueMap* _map; never@894: never@894: public: never@894: // implementation for abstract methods of ValueNumberingVisitor never@894: void kill_memory() { _map->kill_memory(); } never@894: void kill_field(ciField* field) { _map->kill_field(field); } never@894: void kill_array(ValueType* type) { _map->kill_array(type); } never@894: never@894: ValueNumberingEffects(ValueMap* map): _map(map) {} duke@435: }; duke@435: duke@435: duke@435: class GlobalValueNumbering: public ValueNumberingVisitor { duke@435: private: duke@435: ValueMap* _current_map; // value map of current block duke@435: ValueMapArray _value_maps; // list of value maps for all blocks duke@435: duke@435: public: duke@435: // accessors duke@435: ValueMap* current_map() { return _current_map; } duke@435: ValueMap* value_map_of(BlockBegin* block) { return _value_maps.at(block->linear_scan_number()); } duke@435: void set_value_map_of(BlockBegin* block, ValueMap* map) { assert(value_map_of(block) == NULL, ""); _value_maps.at_put(block->linear_scan_number(), map); } duke@435: duke@435: // implementation for abstract methods of ValueNumberingVisitor duke@435: void kill_memory() { current_map()->kill_memory(); } duke@435: void kill_field(ciField* field) { current_map()->kill_field(field); } duke@435: void kill_array(ValueType* type) { current_map()->kill_array(type); } duke@435: duke@435: // main entry point that performs global value numbering duke@435: GlobalValueNumbering(IR* ir); duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_C1_C1_VALUEMAP_HPP