[PATCH] D103180: [InstSimplify] Add constant fold for extractelement + splat for scalable vectors

Caroline via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 1 06:03:45 PDT 2021


CarolineConcatto updated this revision to Diff 348954.
CarolineConcatto added a comment.

- Remove vector of zeros check  by a splat vector check


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D103180/new/

https://reviews.llvm.org/D103180

Files:
  llvm/lib/IR/ConstantFold.cpp
  llvm/test/Transforms/InstSimplify/ConstProp/extractelement-vscale.ll


Index: llvm/test/Transforms/InstSimplify/ConstProp/extractelement-vscale.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/ConstProp/extractelement-vscale.ll
+++ llvm/test/Transforms/InstSimplify/ConstProp/extractelement-vscale.ll
@@ -1,4 +1,4 @@
-; RUN: opt -instcombine -S < %s | FileCheck %s
+; RUN: opt -S -instsimplify < %s | FileCheck %s
 
 ; CHECK-LABEL: definitely_in_bounds
 ; CHECK: ret i8 0
@@ -11,3 +11,35 @@
 define i8 @maybe_in_bounds() {
   ret i8 extractelement (<vscale x 16 x i8> zeroinitializer, i64 16)
 }
+
+; Examples of extract a lane from a splat constant
+
+define i32 @extractconstant_shuffle_in_range(i32 %v) {
+; CHECK-LABEL: @extractconstant_shuffle_in_range(
+; CHECK-NEXT:    ret i32 1024
+;
+  %in = insertelement <vscale x 4 x i32> undef, i32 1024, i32 0
+  %splat = shufflevector <vscale x 4 x i32> %in, <vscale x 4 x i32> undef, <vscale x 4 x i32> zeroinitializer
+  %r = extractelement <vscale x 4 x i32> %splat, i32 1
+  ret i32 %r
+}
+
+define i32 @extractconstant_shuffle_maybe_out_of_range(i32 %v) {
+; CHECK-LABEL: @extractconstant_shuffle_maybe_out_of_range(
+; CHECK-NEXT:    ret i32 extractelement (<vscale x 4 x i32> shufflevector (<vscale x 4 x i32> insertelement (<vscale x 4 x i32> undef, i32 1024, i32 0), <vscale x 4 x i32> undef, <vscale x 4 x i32> zeroinitializer), i32 4)
+;
+  %in = insertelement <vscale x 4 x i32> undef, i32 1024, i32 0
+  %splat = shufflevector <vscale x 4 x i32> %in, <vscale x 4 x i32> undef, <vscale x 4 x i32> zeroinitializer
+  %r = extractelement <vscale x 4 x i32> %splat, i32 4
+  ret i32 %r
+}
+
+define i32 @extractconstant_shuffle_invalid_index(i32 %v) {
+; CHECK-LABEL: @extractconstant_shuffle_invalid_index(
+; CHECK-NEXT:    ret i32 extractelement (<vscale x 4 x i32> shufflevector (<vscale x 4 x i32> insertelement (<vscale x 4 x i32> undef, i32 1024, i32 0), <vscale x 4 x i32> undef, <vscale x 4 x i32> zeroinitializer), i32 -1)
+;
+  %in = insertelement <vscale x 4 x i32> undef, i32 1024, i32 0
+  %splat = shufflevector <vscale x 4 x i32> %in, <vscale x 4 x i32> undef, <vscale x 4 x i32> zeroinitializer
+  %r = extractelement <vscale x 4 x i32> %splat, i32 -1
+  ret i32 %r
+}
Index: llvm/lib/IR/ConstantFold.cpp
===================================================================
--- llvm/lib/IR/ConstantFold.cpp
+++ llvm/lib/IR/ConstantFold.cpp
@@ -907,12 +907,12 @@
     }
   }
 
-  // CAZ of type ScalableVectorType and n < CAZ->getMinNumElements() =>
-  //   extractelt CAZ, n -> 0
+  // ScalableVectorType and Index(n) < Splat minimum vector width =>
+  //  extractelt Splat(x), n -> x
   if (auto *ValSVTy = dyn_cast<ScalableVectorType>(Val->getType())) {
     if (!CIdx->uge(ValSVTy->getMinNumElements())) {
-      if (auto *CAZ = dyn_cast<ConstantAggregateZero>(Val))
-        return CAZ->getElementValue(CIdx->getZExtValue());
+      if (Constant *SplatVal = Val->getSplatValue())
+        return SplatVal;
     }
     return nullptr;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D103180.348954.patch
Type: text/x-patch
Size: 3006 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210601/0363e165/attachment.bin>


More information about the llvm-commits mailing list