1 /*
2 * Copyright (c) 1997, 2019, 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 #include "precompiled.hpp"
26 #include "opto/ad.hpp"
27 #include "opto/compile.hpp"
28 #include "opto/matcher.hpp"
29 #include "opto/node.hpp"
30 #include "opto/regmask.hpp"
31 #include "utilities/population_count.hpp"
32 #include "utilities/powerOfTwo.hpp"
33
34 #define RM_SIZE _RM_SIZE /* a constant private to the class RegMask */
35
36 //------------------------------dump-------------------------------------------
37
38 #ifndef PRODUCT
dump(int r,outputStream * st)39 void OptoReg::dump(int r, outputStream *st) {
40 switch (r) {
41 case Special: st->print("r---"); break;
42 case Bad: st->print("rBAD"); break;
43 default:
44 if (r < _last_Mach_Reg) st->print("%s", Matcher::regName[r]);
45 else st->print("rS%d",r);
46 break;
47 }
48 }
49 #endif
50
51
52 //=============================================================================
53 const RegMask RegMask::Empty(
54 # define BODY(I) 0,
55 FORALL_BODY
56 # undef BODY
57 0
58 );
59
60 //=============================================================================
is_vector(uint ireg)61 bool RegMask::is_vector(uint ireg) {
62 return (ireg == Op_VecS || ireg == Op_VecD ||
63 ireg == Op_VecX || ireg == Op_VecY || ireg == Op_VecZ );
64 }
65
num_registers(uint ireg)66 int RegMask::num_registers(uint ireg) {
67 switch(ireg) {
68 case Op_VecZ:
69 return 16;
70 case Op_VecY:
71 return 8;
72 case Op_VecX:
73 return 4;
74 case Op_VecD:
75 case Op_RegD:
76 case Op_RegL:
77 #ifdef _LP64
78 case Op_RegP:
79 #endif
80 return 2;
81 }
82 // Op_VecS and the rest ideal registers.
83 return 1;
84 }
85
86 // Clear out partial bits; leave only bit pairs
clear_to_pairs()87 void RegMask::clear_to_pairs() {
88 assert(valid_watermarks(), "sanity");
89 for (int i = _lwm; i <= _hwm; i++) {
90 int bits = _A[i];
91 bits &= ((bits & 0x55555555)<<1); // 1 hi-bit set for each pair
92 bits |= (bits>>1); // Smear 1 hi-bit into a pair
93 _A[i] = bits;
94 }
95 assert(is_aligned_pairs(), "mask is not aligned, adjacent pairs");
96 }
97
is_misaligned_pair() const98 bool RegMask::is_misaligned_pair() const {
99 return Size() == 2 && !is_aligned_pairs();
100 }
101
is_aligned_pairs() const102 bool RegMask::is_aligned_pairs() const {
103 // Assert that the register mask contains only bit pairs.
104 assert(valid_watermarks(), "sanity");
105 for (int i = _lwm; i <= _hwm; i++) {
106 int bits = _A[i];
107 while (bits) { // Check bits for pairing
108 int bit = bits & -bits; // Extract low bit
109 // Low bit is not odd means its mis-aligned.
110 if ((bit & 0x55555555) == 0) return false;
111 bits -= bit; // Remove bit from mask
112 // Check for aligned adjacent bit
113 if ((bits & (bit<<1)) == 0) return false;
114 bits -= (bit<<1); // Remove other halve of pair
115 }
116 }
117 return true;
118 }
119
120 // Return TRUE if the mask contains a single bit
is_bound1() const121 bool RegMask::is_bound1() const {
122 if (is_AllStack()) return false;
123 return Size() == 1;
124 }
125
126 // Return TRUE if the mask contains an adjacent pair of bits and no other bits.
is_bound_pair() const127 bool RegMask::is_bound_pair() const {
128 if (is_AllStack()) return false;
129 int bit = -1; // Set to hold the one bit allowed
130 assert(valid_watermarks(), "sanity");
131 for (int i = _lwm; i <= _hwm; i++) {
132 if (_A[i]) { // Found some bits
133 if (bit != -1) return false; // Already had bits, so fail
134 bit = _A[i] & -(_A[i]); // Extract 1 bit from mask
135 if ((bit << 1) != 0) { // Bit pair stays in same word?
136 if ((bit | (bit<<1)) != _A[i])
137 return false; // Require adjacent bit pair and no more bits
138 } else { // Else its a split-pair case
139 if(bit != _A[i]) return false; // Found many bits, so fail
140 i++; // Skip iteration forward
141 if (i > _hwm || _A[i] != 1)
142 return false; // Require 1 lo bit in next word
143 }
144 }
145 }
146 // True for both the empty mask and for a bit pair
147 return true;
148 }
149
150 // Test for a single adjacent set of ideal register's size.
is_bound(uint ireg) const151 bool RegMask::is_bound(uint ireg) const {
152 if (is_vector(ireg)) {
153 if (is_bound_set(num_registers(ireg)))
154 return true;
155 } else if (is_bound1() || is_bound_pair()) {
156 return true;
157 }
158 return false;
159 }
160
161 // only indicies of power 2 are accessed, so index 3 is only filled in for storage.
162 static int low_bits[5] = { 0x55555555, 0x11111111, 0x01010101, 0x00000000, 0x00010001 };
163
164 // Find the lowest-numbered register set in the mask. Return the
165 // HIGHEST register number in the set, or BAD if no sets.
166 // Works also for size 1.
find_first_set(const int size) const167 OptoReg::Name RegMask::find_first_set(const int size) const {
168 assert(is_aligned_sets(size), "mask is not aligned, adjacent sets");
169 assert(valid_watermarks(), "sanity");
170 for (int i = _lwm; i <= _hwm; i++) {
171 if (_A[i]) { // Found some bits
172 // Convert to bit number, return hi bit in pair
173 return OptoReg::Name((i<<_LogWordBits) + find_lowest_bit(_A[i]) + (size - 1));
174 }
175 }
176 return OptoReg::Bad;
177 }
178
179 // Clear out partial bits; leave only aligned adjacent bit pairs
clear_to_sets(const int size)180 void RegMask::clear_to_sets(const int size) {
181 if (size == 1) return;
182 assert(2 <= size && size <= 16, "update low bits table");
183 assert(is_power_of_2(size), "sanity");
184 assert(valid_watermarks(), "sanity");
185 int low_bits_mask = low_bits[size>>2];
186 for (int i = _lwm; i <= _hwm; i++) {
187 int bits = _A[i];
188 int sets = (bits & low_bits_mask);
189 for (int j = 1; j < size; j++) {
190 sets = (bits & (sets<<1)); // filter bits which produce whole sets
191 }
192 sets |= (sets>>1); // Smear 1 hi-bit into a set
193 if (size > 2) {
194 sets |= (sets>>2); // Smear 2 hi-bits into a set
195 if (size > 4) {
196 sets |= (sets>>4); // Smear 4 hi-bits into a set
197 if (size > 8) {
198 sets |= (sets>>8); // Smear 8 hi-bits into a set
199 }
200 }
201 }
202 _A[i] = sets;
203 }
204 assert(is_aligned_sets(size), "mask is not aligned, adjacent sets");
205 }
206
207 // Smear out partial bits to aligned adjacent bit sets
smear_to_sets(const int size)208 void RegMask::smear_to_sets(const int size) {
209 if (size == 1) return;
210 assert(2 <= size && size <= 16, "update low bits table");
211 assert(is_power_of_2(size), "sanity");
212 assert(valid_watermarks(), "sanity");
213 int low_bits_mask = low_bits[size>>2];
214 for (int i = _lwm; i <= _hwm; i++) {
215 int bits = _A[i];
216 int sets = 0;
217 for (int j = 0; j < size; j++) {
218 sets |= (bits & low_bits_mask); // collect partial bits
219 bits = bits>>1;
220 }
221 sets |= (sets<<1); // Smear 1 lo-bit into a set
222 if (size > 2) {
223 sets |= (sets<<2); // Smear 2 lo-bits into a set
224 if (size > 4) {
225 sets |= (sets<<4); // Smear 4 lo-bits into a set
226 if (size > 8) {
227 sets |= (sets<<8); // Smear 8 lo-bits into a set
228 }
229 }
230 }
231 _A[i] = sets;
232 }
233 assert(is_aligned_sets(size), "mask is not aligned, adjacent sets");
234 }
235
236 // Assert that the register mask contains only bit sets.
is_aligned_sets(const int size) const237 bool RegMask::is_aligned_sets(const int size) const {
238 if (size == 1) return true;
239 assert(2 <= size && size <= 16, "update low bits table");
240 assert(is_power_of_2(size), "sanity");
241 int low_bits_mask = low_bits[size>>2];
242 assert(valid_watermarks(), "sanity");
243 for (int i = _lwm; i <= _hwm; i++) {
244 int bits = _A[i];
245 while (bits) { // Check bits for pairing
246 int bit = bits & -bits; // Extract low bit
247 // Low bit is not odd means its mis-aligned.
248 if ((bit & low_bits_mask) == 0) return false;
249 // Do extra work since (bit << size) may overflow.
250 int hi_bit = bit << (size-1); // high bit
251 int set = hi_bit + ((hi_bit-1) & ~(bit-1));
252 // Check for aligned adjacent bits in this set
253 if ((bits & set) != set) return false;
254 bits -= set; // Remove this set
255 }
256 }
257 return true;
258 }
259
260 // Return TRUE if the mask contains one adjacent set of bits and no other bits.
261 // Works also for size 1.
is_bound_set(const int size) const262 int RegMask::is_bound_set(const int size) const {
263 if (is_AllStack()) return false;
264 assert(1 <= size && size <= 16, "update low bits table");
265 assert(valid_watermarks(), "sanity");
266 int bit = -1; // Set to hold the one bit allowed
267 for (int i = _lwm; i <= _hwm; i++) {
268 if (_A[i] ) { // Found some bits
269 if (bit != -1)
270 return false; // Already had bits, so fail
271 bit = _A[i] & -_A[i]; // Extract low bit from mask
272 int hi_bit = bit << (size-1); // high bit
273 if (hi_bit != 0) { // Bit set stays in same word?
274 int set = hi_bit + ((hi_bit-1) & ~(bit-1));
275 if (set != _A[i])
276 return false; // Require adjacent bit set and no more bits
277 } else { // Else its a split-set case
278 if (((-1) & ~(bit-1)) != _A[i])
279 return false; // Found many bits, so fail
280 i++; // Skip iteration forward and check high part
281 // The lower (32-size) bits should be 0 since it is split case.
282 int clear_bit_size = 32-size;
283 int shift_back_size = 32-clear_bit_size;
284 int set = bit>>clear_bit_size;
285 set = set & -set; // Remove sign extension.
286 set = (((set << size) - 1) >> shift_back_size);
287 if (i > _hwm || _A[i] != set)
288 return false; // Require expected low bits in next word
289 }
290 }
291 }
292 // True for both the empty mask and for a bit set
293 return true;
294 }
295
296 // UP means register only, Register plus stack, or stack only is DOWN
is_UP() const297 bool RegMask::is_UP() const {
298 // Quick common case check for DOWN (any stack slot is legal)
299 if (is_AllStack())
300 return false;
301 // Slower check for any stack bits set (also DOWN)
302 if (overlap(Matcher::STACK_ONLY_mask))
303 return false;
304 // Not DOWN, so must be UP
305 return true;
306 }
307
308 // Compute size of register mask in bits
Size() const309 uint RegMask::Size() const {
310 uint sum = 0;
311 assert(valid_watermarks(), "sanity");
312 for (int i = _lwm; i <= _hwm; i++) {
313 sum += population_count((unsigned)_A[i]);
314 }
315 return sum;
316 }
317
318 #ifndef PRODUCT
dump(outputStream * st) const319 void RegMask::dump(outputStream *st) const {
320 st->print("[");
321 RegMask rm = *this; // Structure copy into local temp
322
323 OptoReg::Name start = rm.find_first_elem(); // Get a register
324 if (OptoReg::is_valid(start)) { // Check for empty mask
325 rm.Remove(start); // Yank from mask
326 OptoReg::dump(start, st); // Print register
327 OptoReg::Name last = start;
328
329 // Now I have printed an initial register.
330 // Print adjacent registers as "rX-rZ" instead of "rX,rY,rZ".
331 // Begin looping over the remaining registers.
332 while (1) { //
333 OptoReg::Name reg = rm.find_first_elem(); // Get a register
334 if (!OptoReg::is_valid(reg))
335 break; // Empty mask, end loop
336 rm.Remove(reg); // Yank from mask
337
338 if (last+1 == reg) { // See if they are adjacent
339 // Adjacent registers just collect into long runs, no printing.
340 last = reg;
341 } else { // Ending some kind of run
342 if (start == last) { // 1-register run; no special printing
343 } else if (start+1 == last) {
344 st->print(","); // 2-register run; print as "rX,rY"
345 OptoReg::dump(last, st);
346 } else { // Multi-register run; print as "rX-rZ"
347 st->print("-");
348 OptoReg::dump(last, st);
349 }
350 st->print(","); // Seperate start of new run
351 start = last = reg; // Start a new register run
352 OptoReg::dump(start, st); // Print register
353 } // End of if ending a register run or not
354 } // End of while regmask not empty
355
356 if (start == last) { // 1-register run; no special printing
357 } else if (start+1 == last) {
358 st->print(","); // 2-register run; print as "rX,rY"
359 OptoReg::dump(last, st);
360 } else { // Multi-register run; print as "rX-rZ"
361 st->print("-");
362 OptoReg::dump(last, st);
363 }
364 if (rm.is_AllStack()) st->print("...");
365 }
366 st->print("]");
367 }
368 #endif
369