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