[PATCH] D124882: Fix zero-width bitfield extracts to emit 0

Jon Roelofs via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 3 14:36:08 PDT 2022


jroelofs created this revision.
jroelofs added reviewers: aemerson, paquette, arsenm, regehr, DavidSpickett.
Herald added a subscriber: hiraditya.
Herald added a project: All.
jroelofs requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.

Fixes #55129


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D124882

Files:
  llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
  llvm/test/CodeGen/AArch64/GlobalISel/form-bitfield-extract-from-shr-and.mir


Index: llvm/test/CodeGen/AArch64/GlobalISel/form-bitfield-extract-from-shr-and.mir
===================================================================
--- llvm/test/CodeGen/AArch64/GlobalISel/form-bitfield-extract-from-shr-and.mir
+++ llvm/test/CodeGen/AArch64/GlobalISel/form-bitfield-extract-from-shr-and.mir
@@ -188,3 +188,20 @@
     %4:_(s32) = G_LSHR %3, %2
     $w0 = COPY %4(s32)
 ...
+---
+name:          zero_from_large_shift
+legalized: true
+body:              |
+  bb.1.entry:
+    ; CHECK-LABEL: name: zero_from_large_shift
+    ; CHECK: [[C:%[0-9]+]]:_(s64) = G_CONSTANT i64 0
+    ; CHECK-NEXT: $x0 = COPY [[C]](s64)
+    %2:_(s32) = COPY $w0
+    %1:_(s8) = G_TRUNC %2:_(s32)
+    %3:_(s8) = G_ASSERT_ZEXT %1:_, 1
+    %5:_(s64) = G_CONSTANT i64 1
+    %7:_(s64) = G_ANYEXT %3:_(s8)
+    %4:_(s64) = G_AND %7:_, %5:_
+    %6:_(s64) = G_LSHR %4:_, %5:_(s64)
+    $x0 = COPY %6:_(s64)
+...
Index: llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
===================================================================
--- llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
+++ llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
@@ -4384,6 +4384,14 @@
   if (ShrAmt < 0 || ShrAmt >= Size)
     return false;
 
+  // If the shift subsumes the mask, emit the 0 directly.
+  if (0 == (SMask >> ShrAmt)) {
+    MatchInfo = [=](MachineIRBuilder &B) {
+      B.buildConstant(Dst, 0);
+    };
+    return true;
+  }
+
   // Check that ubfx can do the extraction, with no holes in the mask.
   uint64_t UMask = SMask;
   UMask |= maskTrailingOnes<uint64_t>(ShrAmt);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D124882.426843.patch
Type: text/x-patch
Size: 1557 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220503/9a133e81/attachment.bin>


More information about the llvm-commits mailing list