1 /*
   2  * Copyright (c) 1997, 2010, 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/compile.hpp"
  27 #include "opto/regmask.hpp"
  28 #ifdef TARGET_ARCH_MODEL_x86_32
  29 # include "adfiles/ad_x86_32.hpp"
  30 #endif
  31 #ifdef TARGET_ARCH_MODEL_x86_64
  32 # include "adfiles/ad_x86_64.hpp"
  33 #endif
  34 #ifdef TARGET_ARCH_MODEL_sparc
  35 # include "adfiles/ad_sparc.hpp"
  36 #endif
  37 #ifdef TARGET_ARCH_MODEL_zero
  38 # include "adfiles/ad_zero.hpp"
  39 #endif
  40 
  41 #define RM_SIZE _RM_SIZE /* a constant private to the class RegMask */
  42 
  43 //-------------Non-zero bit search methods used by RegMask---------------------
  44 // Find lowest 1, or return 32 if empty
  45 int find_lowest_bit( uint32 mask ) {
  46   int n = 0;
  47   if( (mask & 0xffff) == 0 ) {
  48     mask >>= 16;
  49     n += 16;
  50   }
  51   if( (mask & 0xff) == 0 ) {
  52     mask >>= 8;
  53     n += 8;
  54   }
  55   if( (mask & 0xf) == 0 ) {
  56     mask >>= 4;
  57     n += 4;
  58   }
  59   if( (mask & 0x3) == 0 ) {
  60     mask >>= 2;
  61     n += 2;
  62   }
  63   if( (mask & 0x1) == 0 ) {
  64     mask >>= 1;
  65      n += 1;
  66   }
  67   if( mask == 0 ) {
  68     n = 32;
  69   }
  70   return n;
  71 }
  72 
  73 // Find highest 1, or return 32 if empty
  74 int find_hihghest_bit( uint32 mask ) {
  75   int n = 0;
  76   if( mask > 0xffff ) {
  77     mask >>= 16;
  78     n += 16;
  79   }
  80   if( mask > 0xff ) {
  81     mask >>= 8;
  82     n += 8;
  83   }
  84   if( mask > 0xf ) {
  85     mask >>= 4;
  86     n += 4;
  87   }
  88   if( mask > 0x3 ) {
  89     mask >>= 2;
  90     n += 2;
  91   }
  92   if( mask > 0x1 ) {
  93     mask >>= 1;
  94     n += 1;
  95   }
  96   if( mask == 0 ) {
  97     n = 32;
  98   }
  99   return n;
 100 }
 101 
 102 //------------------------------dump-------------------------------------------
 103 
 104 #ifndef PRODUCT
 105 void OptoReg::dump( int r ) {
 106   switch( r ) {
 107   case Special: tty->print("r---");   break;
 108   case Bad:     tty->print("rBAD");   break;
 109   default:
 110     if( r < _last_Mach_Reg ) tty->print(Matcher::regName[r]);
 111     else tty->print("rS%d",r);
 112     break;
 113   }
 114 }
 115 #endif
 116 
 117 
 118 //=============================================================================
 119 const RegMask RegMask::Empty(
 120 # define BODY(I) 0,
 121   FORALL_BODY
 122 # undef BODY
 123   0
 124 );
 125 
 126 //------------------------------find_first_pair--------------------------------
 127 // Find the lowest-numbered register pair in the mask.  Return the
 128 // HIGHEST register number in the pair, or BAD if no pairs.
 129 OptoReg::Name RegMask::find_first_pair() const {
 130   VerifyPairs();
 131   for( int i = 0; i < RM_SIZE; i++ ) {
 132     if( _A[i] ) {               // Found some bits
 133       int bit = _A[i] & -_A[i]; // Extract low bit
 134       // Convert to bit number, return hi bit in pair
 135       return OptoReg::Name((i<<_LogWordBits)+find_lowest_bit(bit)+1);
 136     }
 137   }
 138   return OptoReg::Bad;
 139 }
 140 
 141 //------------------------------ClearToPairs-----------------------------------
 142 // Clear out partial bits; leave only bit pairs
 143 void RegMask::ClearToPairs() {
 144   for( int i = 0; i < RM_SIZE; i++ ) {
 145     int bits = _A[i];
 146     bits &= ((bits & 0x55555555)<<1); // 1 hi-bit set for each pair
 147     bits |= (bits>>1);          // Smear 1 hi-bit into a pair
 148     _A[i] = bits;
 149   }
 150   VerifyPairs();
 151 }
 152 
 153 //------------------------------SmearToPairs-----------------------------------
 154 // Smear out partial bits; leave only bit pairs
 155 void RegMask::SmearToPairs() {
 156   for( int i = 0; i < RM_SIZE; i++ ) {
 157     int bits = _A[i];
 158     bits |= ((bits & 0x55555555)<<1); // Smear lo bit hi per pair
 159     bits |= ((bits & 0xAAAAAAAA)>>1); // Smear hi bit lo per pair
 160     _A[i] = bits;
 161   }
 162   VerifyPairs();
 163 }
 164 
 165 //------------------------------is_aligned_pairs-------------------------------
 166 bool RegMask::is_aligned_Pairs() const {
 167   // Assert that the register mask contains only bit pairs.
 168   for( int i = 0; i < RM_SIZE; i++ ) {
 169     int bits = _A[i];
 170     while( bits ) {             // Check bits for pairing
 171       int bit = bits & -bits;   // Extract low bit
 172       // Low bit is not odd means its mis-aligned.
 173       if( (bit & 0x55555555) == 0 ) return false;
 174       bits -= bit;              // Remove bit from mask
 175       // Check for aligned adjacent bit
 176       if( (bits & (bit<<1)) == 0 ) return false;
 177       bits -= (bit<<1);         // Remove other halve of pair
 178     }
 179   }
 180   return true;
 181 }
 182 
 183 //------------------------------is_bound1--------------------------------------
 184 // Return TRUE if the mask contains a single bit
 185 int RegMask::is_bound1() const {
 186   if( is_AllStack() ) return false;
 187   int bit = -1;                 // Set to hold the one bit allowed
 188   for( int i = 0; i < RM_SIZE; i++ ) {
 189     if( _A[i] ) {               // Found some bits
 190       if( bit != -1 ) return false; // Already had bits, so fail
 191       bit = _A[i] & -_A[i];     // Extract 1 bit from mask
 192       if( bit != _A[i] ) return false; // Found many bits, so fail
 193     }
 194   }
 195   // True for both the empty mask and for a single bit
 196   return true;
 197 }
 198 
 199 //------------------------------is_bound2--------------------------------------
 200 // Return TRUE if the mask contains an adjacent pair of bits and no other bits.
 201 int RegMask::is_bound2() const {
 202   if( is_AllStack() ) return false;
 203 
 204   int bit = -1;                 // Set to hold the one bit allowed
 205   for( int i = 0; i < RM_SIZE; i++ ) {
 206     if( _A[i] ) {               // Found some bits
 207       if( bit != -1 ) return false; // Already had bits, so fail
 208       bit = _A[i] & -(_A[i]);   // Extract 1 bit from mask
 209       if( (bit << 1) != 0 ) {   // Bit pair stays in same word?
 210         if( (bit | (bit<<1)) != _A[i] )
 211           return false;         // Require adjacent bit pair and no more bits
 212       } else {                  // Else its a split-pair case
 213         if( bit != _A[i] ) return false; // Found many bits, so fail
 214         i++;                    // Skip iteration forward
 215         if( _A[i] != 1 ) return false; // Require 1 lo bit in next word
 216       }
 217     }
 218   }
 219   // True for both the empty mask and for a bit pair
 220   return true;
 221 }
 222 
 223 //------------------------------is_UP------------------------------------------
 224 // UP means register only, Register plus stack, or stack only is DOWN
 225 bool RegMask::is_UP() const {
 226   // Quick common case check for DOWN (any stack slot is legal)
 227   if( is_AllStack() )
 228     return false;
 229   // Slower check for any stack bits set (also DOWN)
 230   if( overlap(Matcher::STACK_ONLY_mask) )
 231     return false;
 232   // Not DOWN, so must be UP
 233   return true;
 234 }
 235 
 236 //------------------------------Size-------------------------------------------
 237 // Compute size of register mask in bits
 238 uint RegMask::Size() const {
 239   extern uint8 bitsInByte[256];
 240   uint sum = 0;
 241   for( int i = 0; i < RM_SIZE; i++ )
 242     sum +=
 243       bitsInByte[(_A[i]>>24) & 0xff] +
 244       bitsInByte[(_A[i]>>16) & 0xff] +
 245       bitsInByte[(_A[i]>> 8) & 0xff] +
 246       bitsInByte[ _A[i]      & 0xff];
 247   return sum;
 248 }
 249 
 250 #ifndef PRODUCT
 251 //------------------------------print------------------------------------------
 252 void RegMask::dump( ) const {
 253   tty->print("[");
 254   RegMask rm = *this;           // Structure copy into local temp
 255 
 256   OptoReg::Name start = rm.find_first_elem(); // Get a register
 257   if( OptoReg::is_valid(start) ) { // Check for empty mask
 258     rm.Remove(start);           // Yank from mask
 259     OptoReg::dump(start);       // Print register
 260     OptoReg::Name last = start;
 261 
 262     // Now I have printed an initial register.
 263     // Print adjacent registers as "rX-rZ" instead of "rX,rY,rZ".
 264     // Begin looping over the remaining registers.
 265     while( 1 ) {                //
 266       OptoReg::Name reg = rm.find_first_elem(); // Get a register
 267       if( !OptoReg::is_valid(reg) )
 268         break;                  // Empty mask, end loop
 269       rm.Remove(reg);           // Yank from mask
 270 
 271       if( last+1 == reg ) {     // See if they are adjacent
 272         // Adjacent registers just collect into long runs, no printing.
 273         last = reg;
 274       } else {                  // Ending some kind of run
 275         if( start == last ) {   // 1-register run; no special printing
 276         } else if( start+1 == last ) {
 277           tty->print(",");      // 2-register run; print as "rX,rY"
 278           OptoReg::dump(last);
 279         } else {                // Multi-register run; print as "rX-rZ"
 280           tty->print("-");
 281           OptoReg::dump(last);
 282         }
 283         tty->print(",");        // Seperate start of new run
 284         start = last = reg;     // Start a new register run
 285         OptoReg::dump(start); // Print register
 286       } // End of if ending a register run or not
 287     } // End of while regmask not empty
 288 
 289     if( start == last ) {       // 1-register run; no special printing
 290     } else if( start+1 == last ) {
 291       tty->print(",");          // 2-register run; print as "rX,rY"
 292       OptoReg::dump(last);
 293     } else {                    // Multi-register run; print as "rX-rZ"
 294       tty->print("-");
 295       OptoReg::dump(last);
 296     }
 297     if( rm.is_AllStack() ) tty->print("...");
 298   }
 299   tty->print("]");
 300 }
 301 #endif