[RISCV] Simplify predicates with llvm::countr_zero (NFC)
authorKazu Hirata <kazu@google.com>
Sat, 28 Jan 2023 20:14:43 +0000 (12:14 -0800)
committerKazu Hirata <kazu@google.com>
Sat, 28 Jan 2023 20:14:43 +0000 (12:14 -0800)
It takes less code (in C++ and the host assembly) to get rid of the
trailing zeros and compare against {3,5,9} than divide the immediate
by {3,5,9} and check to see if we have a power of 2.

llvm/lib/Target/RISCV/RISCVInstrInfoZb.td

index aac23f0..0bac610 100644 (file)
@@ -170,44 +170,38 @@ def BCLRIANDIMaskLow : SDNodeXForm<imm, [{
 
 def C3LeftShift : PatLeaf<(imm), [{
   uint64_t C = N->getZExtValue();
-  return C > 3 && ((C % 3) == 0) && isPowerOf2_64(C / 3);
+  return C > 3 && (C >> llvm::countr_zero(C)) == 3;
 }]>;
 
 def C5LeftShift : PatLeaf<(imm), [{
   uint64_t C = N->getZExtValue();
-  return C > 5 && ((C % 5) == 0) && isPowerOf2_64(C / 5);
+  return C > 5 && (C >> llvm::countr_zero(C)) == 5;
 }]>;
 
 def C9LeftShift : PatLeaf<(imm), [{
   uint64_t C = N->getZExtValue();
-  return C > 9 && ((C % 9) == 0) && isPowerOf2_64(C / 9);
+  return C > 5 && (C >> llvm::countr_zero(C)) == 9;
 }]>;
 
 // Constant of the form (3 << C) where C is less than 32.
 def C3LeftShiftUW : PatLeaf<(imm), [{
   uint64_t C = N->getZExtValue();
-  if (C <= 3 || (C % 3) != 0)
-    return false;
-  C /= 3;
-  return isPowerOf2_64(C) && C < (1ULL << 32);
+  unsigned Shift = llvm::countr_zero(C);
+  return 1 <= Shift && Shift < 32 && (C >> Shift) == 3;
 }]>;
 
 // Constant of the form (5 << C) where C is less than 32.
 def C5LeftShiftUW : PatLeaf<(imm), [{
   uint64_t C = N->getZExtValue();
-  if (C <= 5 || (C % 5) != 0)
-    return false;
-  C /= 5;
-  return isPowerOf2_64(C) && C < (1ULL << 32);
+  unsigned Shift = llvm::countr_zero(C);
+  return 1 <= Shift && Shift < 32 && (C >> Shift) == 5;
 }]>;
 
 // Constant of the form (9 << C) where C is less than 32.
 def C9LeftShiftUW : PatLeaf<(imm), [{
   uint64_t C = N->getZExtValue();
-  if (C <= 9 || (C % 9) != 0)
-    return false;
-  C /= 9;
-  return isPowerOf2_64(C) && C < (1ULL << 32);
+  unsigned Shift = llvm::countr_zero(C);
+  return 1 <= Shift && Shift < 32 && (C >> Shift) == 9;
 }]>;
 
 def CSImm12MulBy4 : PatLeaf<(imm), [{