1 /*
2  * Copyright (c) 1997, 2020, 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_OPTO_REGMASK_HPP
26 #define SHARE_OPTO_REGMASK_HPP
27 
28 #include "code/vmreg.hpp"
29 #include "opto/optoreg.hpp"
30 #include "utilities/count_leading_zeros.hpp"
31 #include "utilities/count_trailing_zeros.hpp"
32 
33 class LRG;
34 
35 //-------------Non-zero bit search methods used by RegMask---------------------
36 // Find lowest 1, undefined if empty/0
find_lowest_bit(uintptr_t mask)37 static unsigned int find_lowest_bit(uintptr_t mask) {
38   return count_trailing_zeros(mask);
39 }
40 // Find highest 1, undefined if empty/0
find_highest_bit(uintptr_t mask)41 static unsigned int find_highest_bit(uintptr_t mask) {
42   return count_leading_zeros(mask) ^ (BitsPerWord - 1U);
43 }
44 
45 //------------------------------RegMask----------------------------------------
46 // The ADL file describes how to print the machine-specific registers, as well
47 // as any notion of register classes.  We provide a register mask, which is
48 // just a collection of Register numbers.
49 
50 // The ADLC defines 2 macros, RM_SIZE and FORALL_BODY.
51 // RM_SIZE is the size of a register mask in 32-bit words.
52 // FORALL_BODY replicates a BODY macro once per word in the register mask.
53 // The usage is somewhat clumsy and limited to the regmask.[h,c]pp files.
54 // However, it means the ADLC can redefine the unroll macro and all loops
55 // over register masks will be unrolled by the correct amount.
56 
57 class RegMask {
58 
59   friend class RegMaskIterator;
60 
61   // The RM_SIZE is aligned to 64-bit - assert that this holds
62   LP64_ONLY(STATIC_ASSERT(is_aligned(RM_SIZE, 2)));
63 
64   static const unsigned int _WordBitMask = BitsPerWord - 1U;
65   static const unsigned int _LogWordBits = LogBitsPerWord;
66   static const unsigned int _RM_SIZE     = LP64_ONLY(RM_SIZE >> 1) NOT_LP64(RM_SIZE);
67   static const unsigned int _RM_MAX      = _RM_SIZE - 1U;
68 
69   union {
70     // Array of Register Mask bits.  This array is large enough to cover
71     // all the machine registers and all parameters that need to be passed
72     // on the stack (stack registers) up to some interesting limit.  Methods
73     // that need more parameters will NOT be compiled.  On Intel, the limit
74     // is something like 90+ parameters.
75     int       _RM_I[RM_SIZE];
76     uintptr_t _RM_UP[_RM_SIZE];
77   };
78 
79   // The low and high water marks represents the lowest and highest word
80   // that might contain set register mask bits, respectively. We guarantee
81   // that there are no bits in words outside this range, but any word at
82   // and between the two marks can still be 0.
83   unsigned int _lwm;
84   unsigned int _hwm;
85 
86  public:
87   enum { CHUNK_SIZE = _RM_SIZE * BitsPerWord };
88 
89   // SlotsPerLong is 2, since slots are 32 bits and longs are 64 bits.
90   // Also, consider the maximum alignment size for a normally allocated
91   // value.  Since we allocate register pairs but not register quads (at
92   // present), this alignment is SlotsPerLong (== 2).  A normally
93   // aligned allocated register is either a single register, or a pair
94   // of adjacent registers, the lower-numbered being even.
95   // See also is_aligned_Pairs() below, and the padding added before
96   // Matcher::_new_SP to keep allocated pairs aligned properly.
97   // If we ever go to quad-word allocations, SlotsPerQuad will become
98   // the controlling alignment constraint.  Note that this alignment
99   // requirement is internal to the allocator, and independent of any
100   // particular platform.
101   enum { SlotsPerLong = 2,
102          SlotsPerVecA = 8,
103          SlotsPerVecS = 1,
104          SlotsPerVecD = 2,
105          SlotsPerVecX = 4,
106          SlotsPerVecY = 8,
107          SlotsPerVecZ = 16,
108          };
109 
110   // A constructor only used by the ADLC output.  All mask fields are filled
111   // in directly.  Calls to this look something like RM(1,2,3,4);
RegMask(FORALL_BODY int dummy=0)112   RegMask(
113 #   define BODY(I) int a##I,
114     FORALL_BODY
115 #   undef BODY
116     int dummy = 0) {
117 #if defined(VM_LITTLE_ENDIAN) || !defined(_LP64)
118 #   define BODY(I) _RM_I[I] = a##I;
119 #else
120     // We need to swap ints.
121 #   define BODY(I) _RM_I[I ^ 1] = a##I;
122 #endif
123     FORALL_BODY
124 #   undef BODY
125     _lwm = 0;
126     _hwm = _RM_MAX;
127     while (_hwm > 0      && _RM_UP[_hwm] == 0) _hwm--;
128     while ((_lwm < _hwm) && _RM_UP[_lwm] == 0) _lwm++;
129     assert(valid_watermarks(), "post-condition");
130   }
131 
132   // Handy copying constructor
RegMask(RegMask * rm)133   RegMask(RegMask *rm) {
134     _hwm = rm->_hwm;
135     _lwm = rm->_lwm;
136     for (unsigned i = 0; i < _RM_SIZE; i++) {
137       _RM_UP[i] = rm->_RM_UP[i];
138     }
139     assert(valid_watermarks(), "post-condition");
140   }
141 
142   // Construct an empty mask
RegMask()143   RegMask() : _RM_UP(), _lwm(_RM_MAX), _hwm(0) {
144     assert(valid_watermarks(), "post-condition");
145   }
146 
147   // Construct a mask with a single bit
RegMask(OptoReg::Name reg)148   RegMask(OptoReg::Name reg) : RegMask() {
149     Insert(reg);
150   }
151 
152   // Check for register being in mask
Member(OptoReg::Name reg) const153   bool Member(OptoReg::Name reg) const {
154     assert(reg < CHUNK_SIZE, "");
155 
156     unsigned r = (unsigned)reg;
157     return _RM_UP[r >> _LogWordBits] & (uintptr_t(1) << (r & _WordBitMask));
158   }
159 
160   // The last bit in the register mask indicates that the mask should repeat
161   // indefinitely with ONE bits.  Returns TRUE if mask is infinite or
162   // unbounded in size.  Returns FALSE if mask is finite size.
is_AllStack() const163   bool is_AllStack() const {
164     return (_RM_UP[_RM_MAX] & (uintptr_t(1) << _WordBitMask)) != 0;
165   }
166 
set_AllStack()167   void set_AllStack() {
168     _RM_UP[_RM_MAX] |= (uintptr_t(1) << _WordBitMask);
169   }
170 
171   // Test for being a not-empty mask.
is_NotEmpty() const172   bool is_NotEmpty() const {
173     assert(valid_watermarks(), "sanity");
174     uintptr_t tmp = 0;
175     for (unsigned i = _lwm; i <= _hwm; i++) {
176       tmp |= _RM_UP[i];
177     }
178     return tmp;
179   }
180 
181   // Find lowest-numbered register from mask, or BAD if mask is empty.
find_first_elem() const182   OptoReg::Name find_first_elem() const {
183     assert(valid_watermarks(), "sanity");
184     for (unsigned i = _lwm; i <= _hwm; i++) {
185       uintptr_t bits = _RM_UP[i];
186       if (bits) {
187         return OptoReg::Name((i << _LogWordBits) + find_lowest_bit(bits));
188       }
189     }
190     return OptoReg::Name(OptoReg::Bad);
191   }
192 
193   // Get highest-numbered register from mask, or BAD if mask is empty.
find_last_elem() const194   OptoReg::Name find_last_elem() const {
195     assert(valid_watermarks(), "sanity");
196     // Careful not to overflow if _lwm == 0
197     unsigned i = _hwm + 1;
198     while (i > _lwm) {
199       uintptr_t bits = _RM_UP[--i];
200       if (bits) {
201         return OptoReg::Name((i << _LogWordBits) + find_highest_bit(bits));
202       }
203     }
204     return OptoReg::Name(OptoReg::Bad);
205   }
206 
207   // Clear out partial bits; leave only aligned adjacent bit pairs.
208   void clear_to_pairs();
209 
210 #ifdef ASSERT
211   // Verify watermarks are sane, i.e., within bounds and that no
212   // register words below or above the watermarks have bits set.
valid_watermarks() const213   bool valid_watermarks() const {
214     assert(_hwm < _RM_SIZE, "_hwm out of range: %d", _hwm);
215     assert(_lwm < _RM_SIZE, "_lwm out of range: %d", _lwm);
216     for (unsigned i = 0; i < _lwm; i++) {
217       assert(_RM_UP[i] == 0, "_lwm too high: %d regs at: %d", _lwm, i);
218     }
219     for (unsigned i = _hwm + 1; i < _RM_SIZE; i++) {
220       assert(_RM_UP[i] == 0, "_hwm too low: %d regs at: %d", _hwm, i);
221     }
222     return true;
223   }
224 #endif // !ASSERT
225 
226   // Test that the mask contains only aligned adjacent bit pairs
227   bool is_aligned_pairs() const;
228 
229   // mask is a pair of misaligned registers
230   bool is_misaligned_pair() const;
231   // Test for single register
232   bool is_bound1() const;
233   // Test for a single adjacent pair
234   bool is_bound_pair() const;
235   // Test for a single adjacent set of ideal register's size.
236   bool is_bound(uint ireg) const;
237 
238   // Check that whether given reg number with size is valid
239   // for current regmask, where reg is the highest number.
240   bool is_valid_reg(OptoReg::Name reg, const int size) const;
241 
242   // Find the lowest-numbered register set in the mask.  Return the
243   // HIGHEST register number in the set, or BAD if no sets.
244   // Assert that the mask contains only bit sets.
245   OptoReg::Name find_first_set(LRG &lrg, const int size) const;
246 
247   // Clear out partial bits; leave only aligned adjacent bit sets of size.
248   void clear_to_sets(const unsigned int size);
249   // Smear out partial bits to aligned adjacent bit sets.
250   void smear_to_sets(const unsigned int size);
251   // Test that the mask contains only aligned adjacent bit sets
252   bool is_aligned_sets(const unsigned int size) const;
253 
254   // Test for a single adjacent set
255   bool is_bound_set(const unsigned int size) const;
256 
257   static bool is_vector(uint ireg);
258   static int num_registers(uint ireg);
259   static int num_registers(uint ireg, LRG &lrg);
260 
261   // Fast overlap test.  Non-zero if any registers in common.
overlap(const RegMask & rm) const262   bool overlap(const RegMask &rm) const {
263     assert(valid_watermarks() && rm.valid_watermarks(), "sanity");
264     unsigned hwm = MIN2(_hwm, rm._hwm);
265     unsigned lwm = MAX2(_lwm, rm._lwm);
266     uintptr_t result = 0;
267     for (unsigned i = lwm; i <= hwm; i++) {
268       result |= _RM_UP[i] & rm._RM_UP[i];
269     }
270     return result;
271   }
272 
273   // Special test for register pressure based splitting
274   // UP means register only, Register plus stack, or stack only is DOWN
275   bool is_UP() const;
276 
277   // Clear a register mask
Clear()278   void Clear() {
279     _lwm = _RM_MAX;
280     _hwm = 0;
281     memset(_RM_UP, 0, sizeof(uintptr_t) * _RM_SIZE);
282     assert(valid_watermarks(), "sanity");
283   }
284 
285   // Fill a register mask with 1's
Set_All()286   void Set_All() {
287     _lwm = 0;
288     _hwm = _RM_MAX;
289     memset(_RM_UP, 0xFF, sizeof(uintptr_t) * _RM_SIZE);
290     assert(valid_watermarks(), "sanity");
291   }
292 
293   // Insert register into mask
Insert(OptoReg::Name reg)294   void Insert(OptoReg::Name reg) {
295     assert(reg != OptoReg::Bad, "sanity");
296     assert(reg != OptoReg::Special, "sanity");
297     assert(reg < CHUNK_SIZE, "sanity");
298     assert(valid_watermarks(), "pre-condition");
299     unsigned r = (unsigned)reg;
300     unsigned index = r >> _LogWordBits;
301     if (index > _hwm) _hwm = index;
302     if (index < _lwm) _lwm = index;
303     _RM_UP[index] |= (uintptr_t(1) << (r & _WordBitMask));
304     assert(valid_watermarks(), "post-condition");
305   }
306 
307   // Remove register from mask
Remove(OptoReg::Name reg)308   void Remove(OptoReg::Name reg) {
309     assert(reg < CHUNK_SIZE, "");
310     unsigned r = (unsigned)reg;
311     _RM_UP[r >> _LogWordBits] &= ~(uintptr_t(1) << (r & _WordBitMask));
312   }
313 
314   // OR 'rm' into 'this'
OR(const RegMask & rm)315   void OR(const RegMask &rm) {
316     assert(valid_watermarks() && rm.valid_watermarks(), "sanity");
317     // OR widens the live range
318     if (_lwm > rm._lwm) _lwm = rm._lwm;
319     if (_hwm < rm._hwm) _hwm = rm._hwm;
320     for (unsigned i = _lwm; i <= _hwm; i++) {
321       _RM_UP[i] |= rm._RM_UP[i];
322     }
323     assert(valid_watermarks(), "sanity");
324   }
325 
326   // AND 'rm' into 'this'
AND(const RegMask & rm)327   void AND(const RegMask &rm) {
328     assert(valid_watermarks() && rm.valid_watermarks(), "sanity");
329     // Do not evaluate words outside the current watermark range, as they are
330     // already zero and an &= would not change that
331     for (unsigned i = _lwm; i <= _hwm; i++) {
332       _RM_UP[i] &= rm._RM_UP[i];
333     }
334     // Narrow the watermarks if &rm spans a narrower range.
335     // Update after to ensure non-overlapping words are zeroed out.
336     if (_lwm < rm._lwm) _lwm = rm._lwm;
337     if (_hwm > rm._hwm) _hwm = rm._hwm;
338   }
339 
340   // Subtract 'rm' from 'this'
SUBTRACT(const RegMask & rm)341   void SUBTRACT(const RegMask &rm) {
342     assert(valid_watermarks() && rm.valid_watermarks(), "sanity");
343     unsigned hwm = MIN2(_hwm, rm._hwm);
344     unsigned lwm = MAX2(_lwm, rm._lwm);
345     for (unsigned i = lwm; i <= hwm; i++) {
346       _RM_UP[i] &= ~rm._RM_UP[i];
347     }
348   }
349 
350   // Compute size of register mask: number of bits
351   uint Size() const;
352 
353 #ifndef PRODUCT
print() const354   void print() const { dump(); }
355   void dump(outputStream *st = tty) const; // Print a mask
356 #endif
357 
358   static const RegMask Empty;   // Common empty mask
359 
can_represent(OptoReg::Name reg)360   static bool can_represent(OptoReg::Name reg) {
361     // NOTE: -1 in computation reflects the usage of the last
362     //       bit of the regmask as an infinite stack flag and
363     //       -7 is to keep mask aligned for largest value (VecZ).
364     return (int)reg < (int)(CHUNK_SIZE - 1);
365   }
can_represent_arg(OptoReg::Name reg)366   static bool can_represent_arg(OptoReg::Name reg) {
367     // NOTE: -SlotsPerVecZ in computation reflects the need
368     //       to keep mask aligned for largest value (VecZ).
369     return (int)reg < (int)(CHUNK_SIZE - SlotsPerVecZ);
370   }
371 };
372 
373 class RegMaskIterator {
374  private:
375   uintptr_t _current_bits;
376   unsigned int _next_index;
377   OptoReg::Name _reg;
378   const RegMask& _rm;
379  public:
RegMaskIterator(const RegMask & rm)380   RegMaskIterator(const RegMask& rm) : _current_bits(0), _next_index(rm._lwm), _reg(OptoReg::Bad), _rm(rm) {
381     // Calculate the first element
382     next();
383   }
384 
has_next()385   bool has_next() {
386     return _reg != OptoReg::Bad;
387   }
388 
389   // Get the current element and calculate the next
next()390   OptoReg::Name next() {
391     OptoReg::Name r = _reg;
392 
393     // This bit shift scheme, borrowed from IndexSetIterator,
394     // shifts the _current_bits down by the number of trailing
395     // zeros - which leaves the "current" bit on position zero,
396     // then subtracts by 1 to clear it. This quirk avoids the
397     // undefined behavior that could arise if trying to shift
398     // away the bit with a single >> (next_bit + 1) shift when
399     // next_bit is 31/63. It also keeps number of shifts and
400     // arithmetic ops to a minimum.
401 
402     // We have previously found bits at _next_index - 1, and
403     // still have some left at the same index.
404     if (_current_bits != 0) {
405       unsigned int next_bit = find_lowest_bit(_current_bits);
406       assert(_reg != OptoReg::Bad, "can't be in a bad state");
407       assert(next_bit > 0, "must be");
408       assert(((_current_bits >> next_bit) & 0x1) == 1, "lowest bit must be set after shift");
409       _current_bits = (_current_bits >> next_bit) - 1;
410       _reg = OptoReg::add(_reg, next_bit);
411       return r;
412     }
413 
414     // Find the next word with bits
415     while (_next_index <= _rm._hwm) {
416       _current_bits = _rm._RM_UP[_next_index++];
417       if (_current_bits != 0) {
418         // Found a word. Calculate the first register element and
419         // prepare _current_bits by shifting it down and clearing
420         // the lowest bit
421         unsigned int next_bit = find_lowest_bit(_current_bits);
422         assert(((_current_bits >> next_bit) & 0x1) == 1, "lowest bit must be set after shift");
423         _current_bits = (_current_bits >> next_bit) - 1;
424         _reg = OptoReg::Name(((_next_index - 1) << RegMask::_LogWordBits) + next_bit);
425         return r;
426       }
427     }
428 
429     // No more bits
430     _reg = OptoReg::Name(OptoReg::Bad);
431     return r;
432   }
433 };
434 
435 // Do not use this constant directly in client code!
436 #undef RM_SIZE
437 
438 #endif // SHARE_OPTO_REGMASK_HPP
439