Skip to content

Conversation

StephenYoung2754
Copy link

@StephenYoung2754 StephenYoung2754 commented Sep 9, 2025

Summary

Fix incorrect type checks in DAGCombiner::visitSRA.
SIGN_EXTEND should be checked against VT (the result type), and
TRUNCATE should be checked against TruncVT (the truncated type).

Details

The original code swapped the type arguments:

TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT)

This could lead to missed optimizations or invalid folding decisions for
patterns such as (sra (extend x), k).

The patch corrects the checks to:

TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, VT) &&
TLI.isOperationLegalOrCustom(ISD::TRUNCATE, TruncVT)

Testing

  • Existing SelectionDAG tests cover the affected transforms.
  • No regressions observed.
  • The fix enables valid folds on targets where these operations are legal.

Issue

Fixes #153543

Copy link

github-actions bot commented Sep 9, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the llvm:SelectionDAG SelectionDAGISel as well label Sep 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 9, 2025

@llvm/pr-subscribers-llvm-selectiondag

Author: Stephen Young (StephenYoung2754)

Changes

…TruncVT for TRUNCATE


Full diff: https://github.com/llvm/llvm-project/pull/157580.diff

1 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (+1-1)
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index fed5e7238433e..48441005b960e 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -10856,8 +10856,8 @@ SDValue DAGCombiner::visitSRA(SDNode *N) {
       // on that type, and the truncate to that type is both legal and free,
       // perform the transform.
       if ((ShiftAmt > 0) &&
-          TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
           TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
+          TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
           TLI.isTruncateFree(VT, TruncVT)) {
         SDValue Amt = DAG.getShiftAmountConstant(ShiftAmt, VT, DL);
         SDValue Shift = DAG.getNode(ISD::SRL, DL, VT,

Copy link

github-actions bot commented Sep 9, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

? This changes nothing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing this out. You’re right — I accidentally submitted a wrong edit which didn’t fix the issue.
I’ve updated the patch so that SIGN_EXTEND is checked against VT (the result type) and TRUNCATE against TruncVT (the truncated type).
Please take another look.

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing test coverage and duplicate of #153762 - please coordinate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:SelectionDAG SelectionDAGISel as well

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong types checked at DAGCombiner::visitSRA(SDNode *N)

4 participants