Path: blob/master/src/hotspot/share/c1/c1_ValueMap.hpp
40930 views
/*1* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_C1_C1_VALUEMAP_HPP25#define SHARE_C1_C1_VALUEMAP_HPP2627#include "c1/c1_Instruction.hpp"28#include "c1/c1_ValueSet.hpp"29#include "memory/allocation.hpp"3031class ValueMapEntry: public CompilationResourceObj {32private:33intx _hash;34Value _value;35int _nesting;36ValueMapEntry* _next;3738public:39ValueMapEntry(intx hash, Value value, int nesting, ValueMapEntry* next)40: _hash(hash)41, _value(value)42, _nesting(nesting)43, _next(next)44{45}4647intx hash() { return _hash; }48Value value() { return _value; }49int nesting() { return _nesting; }50ValueMapEntry* next() { return _next; }5152void set_next(ValueMapEntry* next) { _next = next; }53};5455typedef GrowableArray<ValueMapEntry*> ValueMapEntryArray;56typedef GrowableArray<ValueMapEntry*> ValueMapEntryList;5758// ValueMap implements nested hash tables for value numbering. It59// maintains a set _killed_values which represents the instructions60// which have been killed so far and an array of linked lists of61// ValueMapEntries names _entries. Each ValueMapEntry has a nesting62// which indicates what ValueMap nesting it belongs to. Higher63// nesting values are always before lower values in the linked list.64// This allows cloning of parent ValueMaps by simply copying the heads65// of the list. _entry_count represents the number of reachable66// entries in the ValueMap. A ValueMap is only allowed to mutate67// ValueMapEntries with the same nesting level. Adding or removing68// entries at the current nesting level requires updating69// _entry_count. Elements in the parent's list that get killed can be70// skipped if they are at the head of the list by simply moving to the71// next element in the list and decrementing _entry_count.7273class ValueMap: public CompilationResourceObj {74private:75int _nesting;76ValueMapEntryArray _entries;77ValueSet _killed_values;78int _entry_count;7980int nesting() { return _nesting; }81bool is_local_value_numbering() { return _nesting == 0; }82bool is_global_value_numbering() { return _nesting > 0; }8384int entry_count() { return _entry_count; }85int size() { return _entries.length(); }86ValueMapEntry* entry_at(int i) { return _entries.at(i); }8788// calculates the index of a hash value in a hash table of size n89int entry_index(intx hash, int n) { return (unsigned int)hash % n; }9091// if entry_count > size_threshold, the size of the hash table is increased92int size_threshold() { return size(); }9394// management of the killed-bitset for global value numbering95void kill_value(Value v) { if (is_global_value_numbering()) _killed_values.put(v); }96bool is_killed(Value v) { if (is_global_value_numbering()) return _killed_values.contains(v); else return false; }9798// helper functions99void increase_table_size();100101#ifndef PRODUCT102static int _number_of_finds;103static int _number_of_hits;104static int _number_of_kills;105#endif // PRODUCT106107public:108// creation109ValueMap(); // empty value map110ValueMap(ValueMap* old); // value map with increased nesting111112// manipulation113Value find_insert(Value x);114115void kill_memory();116void kill_field(ciField* field, bool all_offsets);117void kill_array(ValueType* type);118void kill_exception();119void kill_map(ValueMap* map);120void kill_all();121122#ifndef PRODUCT123// debugging/printing124void print();125126static void reset_statistics();127static void print_statistics();128#endif129};130131typedef GrowableArray<ValueMap*> ValueMapArray;132133class ValueNumberingVisitor: public InstructionVisitor {134protected:135// called by visitor functions for instructions that kill values136virtual void kill_memory() = 0;137virtual void kill_field(ciField* field, bool all_offsets) = 0;138virtual void kill_array(ValueType* type) = 0;139140// visitor functions141void do_StoreField (StoreField* x) {142if (x->is_init_point() || // putstatic is an initialization point so treat it as a wide kill143// This is actually too strict and the JMM doesn't require144// this in all cases (e.g. load a; volatile store b; load a)145// but possible future optimizations might require this.146x->field()->is_volatile()) {147kill_memory();148} else {149kill_field(x->field(), x->needs_patching());150}151}152void do_StoreIndexed (StoreIndexed* x) { kill_array(x->type()); }153void do_MonitorEnter (MonitorEnter* x) { kill_memory(); }154void do_MonitorExit (MonitorExit* x) { kill_memory(); }155void do_Invoke (Invoke* x) { kill_memory(); }156void do_UnsafePutRaw (UnsafePutRaw* x) { kill_memory(); }157void do_UnsafePutObject(UnsafePutObject* x) { kill_memory(); }158void do_UnsafeGetAndSetObject(UnsafeGetAndSetObject* x) { kill_memory(); }159void do_UnsafeGetRaw (UnsafeGetRaw* x) { /* nothing to do */ }160void do_UnsafeGetObject(UnsafeGetObject* x) {161if (x->is_volatile()) { // the JMM requires this162kill_memory();163}164}165void do_Intrinsic (Intrinsic* x) { if (!x->preserves_state()) kill_memory(); }166167void do_Phi (Phi* x) { /* nothing to do */ }168void do_Local (Local* x) { /* nothing to do */ }169void do_Constant (Constant* x) { /* nothing to do */ }170void do_LoadField (LoadField* x) {171if (x->is_init_point() || // getstatic is an initialization point so treat it as a wide kill172x->field()->is_volatile()) { // the JMM requires this173kill_memory();174}175}176void do_ArrayLength (ArrayLength* x) { /* nothing to do */ }177void do_LoadIndexed (LoadIndexed* x) { /* nothing to do */ }178void do_NegateOp (NegateOp* x) { /* nothing to do */ }179void do_ArithmeticOp (ArithmeticOp* x) { /* nothing to do */ }180void do_ShiftOp (ShiftOp* x) { /* nothing to do */ }181void do_LogicOp (LogicOp* x) { /* nothing to do */ }182void do_CompareOp (CompareOp* x) { /* nothing to do */ }183void do_IfOp (IfOp* x) { /* nothing to do */ }184void do_Convert (Convert* x) { /* nothing to do */ }185void do_NullCheck (NullCheck* x) { /* nothing to do */ }186void do_TypeCast (TypeCast* x) { /* nothing to do */ }187void do_NewInstance (NewInstance* x) { /* nothing to do */ }188void do_NewTypeArray (NewTypeArray* x) { /* nothing to do */ }189void do_NewObjectArray (NewObjectArray* x) { /* nothing to do */ }190void do_NewMultiArray (NewMultiArray* x) { /* nothing to do */ }191void do_CheckCast (CheckCast* x) { /* nothing to do */ }192void do_InstanceOf (InstanceOf* x) { /* nothing to do */ }193void do_BlockBegin (BlockBegin* x) { /* nothing to do */ }194void do_Goto (Goto* x) { /* nothing to do */ }195void do_If (If* x) { /* nothing to do */ }196void do_TableSwitch (TableSwitch* x) { /* nothing to do */ }197void do_LookupSwitch (LookupSwitch* x) { /* nothing to do */ }198void do_Return (Return* x) { /* nothing to do */ }199void do_Throw (Throw* x) { /* nothing to do */ }200void do_Base (Base* x) { /* nothing to do */ }201void do_OsrEntry (OsrEntry* x) { /* nothing to do */ }202void do_ExceptionObject(ExceptionObject* x) { /* nothing to do */ }203void do_RoundFP (RoundFP* x) { /* nothing to do */ }204void do_ProfileCall (ProfileCall* x) { /* nothing to do */ }205void do_ProfileReturnType (ProfileReturnType* x) { /* nothing to do */ }206void do_ProfileInvoke (ProfileInvoke* x) { /* nothing to do */ };207void do_RuntimeCall (RuntimeCall* x) { /* nothing to do */ };208void do_MemBar (MemBar* x) { /* nothing to do */ };209void do_RangeCheckPredicate(RangeCheckPredicate* x) { /* nothing to do */ };210#ifdef ASSERT211void do_Assert (Assert* x) { /* nothing to do */ };212#endif213};214215216class ValueNumberingEffects: public ValueNumberingVisitor {217private:218ValueMap* _map;219220public:221// implementation for abstract methods of ValueNumberingVisitor222void kill_memory() { _map->kill_memory(); }223void kill_field(ciField* field, bool all_offsets) { _map->kill_field(field, all_offsets); }224void kill_array(ValueType* type) { _map->kill_array(type); }225226ValueNumberingEffects(ValueMap* map): _map(map) {}227};228229230class GlobalValueNumbering: public ValueNumberingVisitor {231private:232Compilation* _compilation; // compilation data233ValueMap* _current_map; // value map of current block234ValueMapArray _value_maps; // list of value maps for all blocks235ValueSet _processed_values; // marker for instructions that were already processed236bool _has_substitutions; // set to true when substitutions must be resolved237238public:239// accessors240Compilation* compilation() const { return _compilation; }241ValueMap* current_map() { return _current_map; }242ValueMap* value_map_of(BlockBegin* block) { return _value_maps.at(block->linear_scan_number()); }243void set_value_map_of(BlockBegin* block, ValueMap* map) { assert(value_map_of(block) == NULL, ""); _value_maps.at_put(block->linear_scan_number(), map); }244245bool is_processed(Value v) { return _processed_values.contains(v); }246void set_processed(Value v) { _processed_values.put(v); }247248// implementation for abstract methods of ValueNumberingVisitor249void kill_memory() { current_map()->kill_memory(); }250void kill_field(ciField* field, bool all_offsets) { current_map()->kill_field(field, all_offsets); }251void kill_array(ValueType* type) { current_map()->kill_array(type); }252253// main entry point that performs global value numbering254GlobalValueNumbering(IR* ir);255void substitute(Instruction* instr); // substitute instruction if it is contained in current value map256};257258#endif // SHARE_C1_C1_VALUEMAP_HPP259260261