Skip to content

Commit 0570d1e

Browse files
committed
[GloalISel] Fold G_CTTZ if possible
This patch tries to fold `G_CTTZ` if possible.
1 parent dfe4ca9 commit 0570d1e

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

llvm/include/llvm/CodeGen/GlobalISel/Utils.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,12 @@ std::optional<APFloat> ConstantFoldIntToFloat(unsigned Opcode, LLT DstTy,
308308
Register Src,
309309
const MachineRegisterInfo &MRI);
310310

311-
/// Tries to constant fold a G_CTLZ operation on \p Src. If \p Src is a vector
312-
/// then it tries to do an element-wise constant fold.
311+
/// Tries to constant fold a counting-zero operation (G_CTLZ or G_CTTZ) on \p
312+
/// Src. If \p Src is a vector then it tries to do an element-wise constant
313+
/// fold.
313314
std::optional<SmallVector<unsigned>>
314-
ConstantFoldCTLZ(Register Src, const MachineRegisterInfo &MRI);
315+
ConstantFoldCountZeros(Register Src, const MachineRegisterInfo &MRI,
316+
std::function<unsigned(APInt)> CB);
315317

316318
/// Test if the given value is known to have exactly one bit set. This differs
317319
/// from computeKnownBits in that it doesn't necessarily determine which bit is

llvm/lib/CodeGen/GlobalISel/CSEMIRBuilder.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,16 @@ MachineInstrBuilder CSEMIRBuilder::buildInstr(unsigned Opc,
256256
return buildFConstant(DstOps[0], *Cst);
257257
break;
258258
}
259-
case TargetOpcode::G_CTLZ: {
259+
case TargetOpcode::G_CTLZ:
260+
case TargetOpcode::G_CTTZ: {
260261
assert(SrcOps.size() == 1 && "Expected one source");
261262
assert(DstOps.size() == 1 && "Expected one dest");
262-
auto MaybeCsts = ConstantFoldCTLZ(SrcOps[0].getReg(), *getMRI());
263+
std::function<unsigned(APInt)> CB;
264+
if (Opc == TargetOpcode::G_CTLZ)
265+
CB = [](APInt V) -> unsigned { return V.countl_zero(); };
266+
else
267+
CB = [](APInt V) -> unsigned { return V.countTrailingZeros(); };
268+
auto MaybeCsts = ConstantFoldCountZeros(SrcOps[0].getReg(), *getMRI(), CB);
263269
if (!MaybeCsts)
264270
break;
265271
if (MaybeCsts->size() == 1)

llvm/lib/CodeGen/GlobalISel/Utils.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -966,14 +966,15 @@ llvm::ConstantFoldIntToFloat(unsigned Opcode, LLT DstTy, Register Src,
966966
}
967967

968968
std::optional<SmallVector<unsigned>>
969-
llvm::ConstantFoldCTLZ(Register Src, const MachineRegisterInfo &MRI) {
969+
llvm::ConstantFoldCountZeros(Register Src, const MachineRegisterInfo &MRI,
970+
std::function<unsigned(APInt)> CB) {
970971
LLT Ty = MRI.getType(Src);
971972
SmallVector<unsigned> FoldedCTLZs;
972973
auto tryFoldScalar = [&](Register R) -> std::optional<unsigned> {
973974
auto MaybeCst = getIConstantVRegVal(R, MRI);
974975
if (!MaybeCst)
975976
return std::nullopt;
976-
return MaybeCst->countl_zero();
977+
return CB(*MaybeCst);
977978
};
978979
if (Ty.isVector()) {
979980
// Try to constant fold each element.

0 commit comments

Comments
 (0)