From 6f15919d986d671be084eb4edce21ef76301808d Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Sat, 28 Jan 2023 12:14:43 -0800 Subject: [PATCH] [RISCV] Simplify predicates with llvm::countr_zero (NFC) 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 | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td b/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td index aac23f0..0bac610 100644 --- a/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td +++ b/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td @@ -170,44 +170,38 @@ def BCLRIANDIMaskLow : SDNodeXFormgetZExtValue(); - 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), [{ -- 2.7.4