[LegalizeTypes] Further limit expansion of CTTZ during type promotion.
authorCraig Topper <craig.topper@sifive.com>
Wed, 17 Nov 2021 23:21:00 +0000 (15:21 -0800)
committerCraig Topper <craig.topper@sifive.com>
Wed, 17 Nov 2021 23:27:29 +0000 (15:27 -0800)
Don't expand CTTZ if CTPOP or CTLZ is supported on the promoted type.
We have special handling for CTTZ expansion to use those ops with a
small conversion. The setup for that doesn't generate extra code or
large constants so we don't gain anything from expanding early and we
make CTTZ_ZERO_UNDEF codegen worse.

Follow up from post commit feedback on D112268. We don't seem to have
any in tree tests that care about this.

llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp

index 6ce7822..1fa4d88 100644 (file)
@@ -622,10 +622,13 @@ SDValue DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode *N) {
 
   // If the larger CTTZ isn't supported by the target, try to expand now.
   // If we expand later we'll end up with more operations since we lost the
-  // original type.
+  // original type. Don't expand if we can use CTPOP or CTLZ expansion on the
+  // larger type.
   if (!OVT.isVector() && TLI.isTypeLegal(NVT) &&
       !TLI.isOperationLegalOrCustomOrPromote(ISD::CTTZ, NVT) &&
-      !TLI.isOperationLegalOrCustomOrPromote(ISD::CTTZ_ZERO_UNDEF, NVT)) {
+      !TLI.isOperationLegalOrCustomOrPromote(ISD::CTTZ_ZERO_UNDEF, NVT) &&
+      !TLI.isOperationLegal(ISD::CTPOP, NVT) &&
+      !TLI.isOperationLegal(ISD::CTLZ, NVT)) {
     if (SDValue Result = TLI.expandCTTZ(N, DAG)) {
       Result = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Result);
       return Result;