Skip to content

Commit 6f15919

Browse files
[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.
1 parent e67f849 commit 6f15919

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

llvm/lib/Target/RISCV/RISCVInstrInfoZb.td

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -170,44 +170,38 @@ def BCLRIANDIMaskLow : SDNodeXForm<imm, [{
170170

171171
def C3LeftShift : PatLeaf<(imm), [{
172172
uint64_t C = N->getZExtValue();
173-
return C > 3 && ((C % 3) == 0) && isPowerOf2_64(C / 3);
173+
return C > 3 && (C >> llvm::countr_zero(C)) == 3;
174174
}]>;
175175

176176
def C5LeftShift : PatLeaf<(imm), [{
177177
uint64_t C = N->getZExtValue();
178-
return C > 5 && ((C % 5) == 0) && isPowerOf2_64(C / 5);
178+
return C > 5 && (C >> llvm::countr_zero(C)) == 5;
179179
}]>;
180180

181181
def C9LeftShift : PatLeaf<(imm), [{
182182
uint64_t C = N->getZExtValue();
183-
return C > 9 && ((C % 9) == 0) && isPowerOf2_64(C / 9);
183+
return C > 5 && (C >> llvm::countr_zero(C)) == 9;
184184
}]>;
185185

186186
// Constant of the form (3 << C) where C is less than 32.
187187
def C3LeftShiftUW : PatLeaf<(imm), [{
188188
uint64_t C = N->getZExtValue();
189-
if (C <= 3 || (C % 3) != 0)
190-
return false;
191-
C /= 3;
192-
return isPowerOf2_64(C) && C < (1ULL << 32);
189+
unsigned Shift = llvm::countr_zero(C);
190+
return 1 <= Shift && Shift < 32 && (C >> Shift) == 3;
193191
}]>;
194192

195193
// Constant of the form (5 << C) where C is less than 32.
196194
def C5LeftShiftUW : PatLeaf<(imm), [{
197195
uint64_t C = N->getZExtValue();
198-
if (C <= 5 || (C % 5) != 0)
199-
return false;
200-
C /= 5;
201-
return isPowerOf2_64(C) && C < (1ULL << 32);
196+
unsigned Shift = llvm::countr_zero(C);
197+
return 1 <= Shift && Shift < 32 && (C >> Shift) == 5;
202198
}]>;
203199

204200
// Constant of the form (9 << C) where C is less than 32.
205201
def C9LeftShiftUW : PatLeaf<(imm), [{
206202
uint64_t C = N->getZExtValue();
207-
if (C <= 9 || (C % 9) != 0)
208-
return false;
209-
C /= 9;
210-
return isPowerOf2_64(C) && C < (1ULL << 32);
203+
unsigned Shift = llvm::countr_zero(C);
204+
return 1 <= Shift && Shift < 32 && (C >> Shift) == 9;
211205
}]>;
212206

213207
def CSImm12MulBy4 : PatLeaf<(imm), [{

0 commit comments

Comments
 (0)