1 /*
   2  * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_OPTO_INDEXSET_HPP
  26 #define SHARE_VM_OPTO_INDEXSET_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "memory/resourceArea.hpp"
  30 #include "opto/compile.hpp"
  31 #include "opto/regmask.hpp"
  32 
  33 // This file defines the IndexSet class, a set of sparse integer indices.
  34 // This data structure is used by the compiler in its liveness analysis and
  35 // during register allocation.
  36 
  37 //-------------------------------- class IndexSet ----------------------------
  38 // An IndexSet is an adapter for high-performance BitMap implementation in
  39 // VM utility classes. See JDK-8059461 for performance investigation.
  40 
  41 class IndexSet : public ResourceObj {
  42  friend class IndexSetIterator;
  43 
  44   //-------------------------- Members ------------------------------------------
  45  private:
  46 
  47   // Underlying BitMap where we store the values
  48   BitMap    _map;
  49 
  50   // The next IndexSet on the free list
  51   IndexSet *_next;
  52 
  53   //-------------------------- Free list operations ------------------------------
  54   // Individual IndexSets can be placed on a free list.  This is done in PhaseLive.
  55  public:
  56 
  57   IndexSet *next() {
  58     return _next;
  59   }
  60 
  61   void set_next(IndexSet *next) {
  62     _next = next;
  63   }
  64 
  65  public:
  66   //-------------------------- Primitive set operations --------------------------
  67 
  68   void clear() {
  69     _map.clear();
  70   }
  71 
  72   uint count() const {
  73     return _map.count_one_bits();
  74   }
  75 
  76   bool is_empty() const {
  77     return _map.is_empty();
  78   }
  79 
  80   bool member(uint element) const {
  81     return _map.at(element);
  82   }
  83 
  84   bool insert(uint element) {
  85     if (element == 0) {
  86       return 0;
  87     }
  88     return _map.set_bit_with_result(element);
  89   }
  90 
  91   bool remove(uint element) {
  92     return _map.clear_bit_with_result(element);
  93   }
  94 
  95   //------------------------- Construction, initialization -----------------------
  96 
  97   IndexSet() {}
  98 
  99   // This constructor is used for making a deep copy of a IndexSet.
 100   IndexSet(IndexSet *set) {
 101     _map = BitMap(set->_map.size());
 102     _map.set_from(set->_map);
 103   };
 104 
 105   // Perform initialization on a IndexSet
 106   void initialize(uint max_element) {
 107     _map = BitMap(max_element);
 108   };
 109 
 110   // Initialize a IndexSet in resource arena.
 111   void initialize_in_resource_arena(uint max_element) {
 112     _map = BitMap(max_element, true);
 113   };
 114 
 115   // Overwrite current IndexSet with another set
 116   void set_from(IndexSet *set) {
 117     _map.set_from(set->_map);
 118   }
 119 
 120 #ifndef PRODUCT
 121   void dump();
 122 #endif
 123 };
 124 
 125 
 126 //-------------------------------- class IndexSetIterator --------------------
 127 // An iterator for IndexSets.
 128 
 129 class IndexSetIterator {
 130  friend class IndexSet;
 131 
 132  BitMapIterator iter;
 133 
 134  public:
 135   IndexSetIterator(IndexSet *set) : iter(&(set->_map)) {}
 136 
 137   inline uint next() {
 138     return iter.next();
 139   }
 140 
 141 };
 142 
 143 #endif // SHARE_VM_OPTO_INDEXSET_HPP