Skip to content

[GlobalIsel] Use aext in ctlz_zero_undef widenScalar expansion#181506

Merged
AlexMaclean merged 2 commits intollvm:mainfrom
AlexMaclean:dev/amaclean/ctlz-undef-aext
Feb 19, 2026
Merged

[GlobalIsel] Use aext in ctlz_zero_undef widenScalar expansion#181506
AlexMaclean merged 2 commits intollvm:mainfrom
AlexMaclean:dev/amaclean/ctlz-undef-aext

Conversation

@AlexMaclean
Copy link
Member

@AlexMaclean AlexMaclean commented Feb 14, 2026

Use G_ANYEXT instead of G_ZEXT when widening the source of G_CTLZ_ZERO_UNDEF. The extended upper bits are immediately shifted out by the subsequent left-shift, so zero-extending is unnecessarily constraining.

Before:

    %wide = G_ZEXT %src
    %shifted = G_SHL %wide, sizeDiff
    %result = G_CTLZ_ZERO_UNDEF %shifted

After:

    %wide = G_ANYEXT %src
    %shifted = G_SHL %wide, sizeDiff
    %result = G_CTLZ_ZERO_UNDEF %shifted

@llvmbot
Copy link
Member

llvmbot commented Feb 14, 2026

@llvm/pr-subscribers-backend-arm
@llvm/pr-subscribers-backend-amdgpu

@llvm/pr-subscribers-llvm-globalisel

Author: Alex MacLean (AlexMaclean)

Changes

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

1 Files Affected:

  • (modified) llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp (+1)
diff --git a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
index 41b79ccb4e4d1..ffd0814f86010 100644
--- a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
@@ -2816,6 +2816,7 @@ LegalizerHelper::widenScalar(MachineInstr &MI, unsigned TypeIdx, LLT WideTy) {
     switch (Opcode) {
     case TargetOpcode::G_CTTZ:
     case TargetOpcode::G_CTTZ_ZERO_UNDEF:
+    case TargetOpcode::G_CTLZ_ZERO_UNDEF: // undef bits shifted out below
       ExtOpc = TargetOpcode::G_ANYEXT;
       break;
     case TargetOpcode::G_CTLS:

@AlexMaclean AlexMaclean changed the title [GlobalIsel] Use aext in ctlz_zero_undef expansion [GlobalIsel] Use aext in ctlz_zero_undef widenScalar expansion Feb 14, 2026
@github-actions
Copy link

github-actions bot commented Feb 14, 2026

🐧 Linux x64 Test Results

  • 190078 tests passed
  • 5073 tests skipped

✅ The build succeeded and all tests passed.

@github-actions
Copy link

github-actions bot commented Feb 14, 2026

🪟 Windows x64 Test Results

  • 130882 tests passed
  • 2938 tests skipped

✅ The build succeeded and all tests passed.

Copy link
Collaborator

@topperc topperc left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@e-kud e-kud left a comment

Choose a reason for hiding this comment

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

Interesting. LGTM!

Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

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

I'd expect this to be caught by other optimizations, but I think GlobalISel is still missing SimplifyDemandedBits

@AlexMaclean
Copy link
Member Author

I'd expect this to be caught by other optimizations, but I think GlobalISel is still missing SimplifyDemandedBits

Yea, I think this is missing. Even if it were supported, since the GIsel combiner is more opt-in I think it can't hurt to generate the best sequence of instructions for this lowering directly, instead of relying on optimization. Any concerns with this change as is?

@AlexMaclean AlexMaclean merged commit 3e3713c into llvm:main Feb 19, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants