[PATCH] D135147: [GISel] Handle G_TRUNC in `matchExtractVecEltBuildVec`

Pierre van Houtryve via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 4 04:56:19 PDT 2022


Pierre-vh created this revision.
Pierre-vh added a reviewer: arsenm.
Herald added a subscriber: hiraditya.
Herald added a project: All.
Pierre-vh requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.

Spotted some cases in D134354 <https://reviews.llvm.org/D134354> where this was an issue.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D135147

Files:
  llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
  llvm/test/CodeGen/AArch64/GlobalISel/combine-extract-vec-elt.mir


Index: llvm/test/CodeGen/AArch64/GlobalISel/combine-extract-vec-elt.mir
===================================================================
--- llvm/test/CodeGen/AArch64/GlobalISel/combine-extract-vec-elt.mir
+++ llvm/test/CodeGen/AArch64/GlobalISel/combine-extract-vec-elt.mir
@@ -29,6 +29,40 @@
     $x0 = COPY %extract(s64)
     RET_ReallyLR implicit $x0
 
+...
+---
+name:            extract_from_trunc_build_vector
+alignment:       4
+tracksRegLiveness: true
+liveins:
+  - { reg: '$x0' }
+  - { reg: '$x1' }
+frameInfo:
+  maxAlignment:    1
+  maxCallFrameSize: 0
+machineFunctionInfo: {}
+body:             |
+  bb.0:
+    liveins: $x0, $x1
+
+    ; CHECK-LABEL: name: extract_from_trunc_build_vector
+    ; CHECK: liveins: $x0, $x1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: %arg1:_(s64) = COPY $x0
+    ; CHECK-NEXT: %extract:_(s32) = G_TRUNC %arg1(s64)
+    ; CHECK-NEXT: %zext:_(s64) = G_ZEXT %extract(s32)
+    ; CHECK-NEXT: $x0 = COPY %zext(s64)
+    ; CHECK-NEXT: RET_ReallyLR implicit $x0
+    %arg1:_(s64) = COPY $x0
+    %arg2:_(s64) = COPY $x1
+    %zero:_(s32) = G_CONSTANT i32 0
+    %bv:_(<2 x s64>) = G_BUILD_VECTOR %arg1(s64), %arg2(s64)
+    %truncbv:_(<2 x s32>) = G_TRUNC %bv
+    %extract:_(s32) = G_EXTRACT_VECTOR_ELT %truncbv(<2 x s32>), %zero(s32)
+    %zext:_(s64) = G_ZEXT %extract
+    $x0 = COPY %zext(s64)
+    RET_ReallyLR implicit $x0
+
 ...
 ---
 name:            extract_from_build_vector_idx1
Index: llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
===================================================================
--- llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
+++ llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
@@ -3937,16 +3937,27 @@
     return false;
 
   unsigned VecIdx = Cst->Value.getZExtValue();
-  MachineInstr *BuildVecMI =
-      getOpcodeDef(TargetOpcode::G_BUILD_VECTOR, SrcVec, MRI);
-  if (!BuildVecMI) {
-    BuildVecMI = getOpcodeDef(TargetOpcode::G_BUILD_VECTOR_TRUNC, SrcVec, MRI);
-    if (!BuildVecMI)
-      return false;
-    LLT ScalarTy = MRI.getType(BuildVecMI->getOperand(1).getReg());
+
+  MachineInstr *SrcVecMI = getDefIgnoringCopies(SrcVec, MRI);
+
+  // Check if we have a (trunc (build_vector)) ..
+  if (SrcVecMI->getOpcode() == TargetOpcode::G_TRUNC) {
+    SrcVecMI = getDefIgnoringCopies(SrcVecMI->getOperand(1).getReg(), MRI);
+  }
+
+  // Need a build_vector or build_vector_trunc
+  switch (SrcVecMI->getOpcode()) {
+  case TargetOpcode::G_BUILD_VECTOR:
+    break;
+  case TargetOpcode::G_BUILD_VECTOR_TRUNC: {
+    LLT ScalarTy = MRI.getType(SrcVecMI->getOperand(1).getReg());
     if (!isLegalOrBeforeLegalizer(
             {TargetOpcode::G_BUILD_VECTOR_TRUNC, {SrcTy, ScalarTy}}))
       return false;
+    break;
+  }
+  default:
+    return false;
   }
 
   EVT Ty(getMVTForLLT(SrcTy));
@@ -3954,7 +3965,7 @@
       !getTargetLowering().aggressivelyPreferBuildVectorSources(Ty))
     return false;
 
-  Reg = BuildVecMI->getOperand(VecIdx + 1).getReg();
+  Reg = SrcVecMI->getOperand(VecIdx + 1).getReg();
   return true;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135147.464964.patch
Type: text/x-patch
Size: 3025 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221004/e4e8f48c/attachment-0001.bin>


More information about the llvm-commits mailing list