[PATCH] D85865: [ConstProp] Handle insertelement constants

Arthur Eubanks via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 12 16:39:24 PDT 2020


aeubanks created this revision.
aeubanks added reviewers: huihuiz, nikic, efriedma.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
aeubanks requested review of this revision.

Previously ConstantFoldExtractElementInstruction() would only work with
insertelement instructions, not contants. This properly handles
insertelement constants as well.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D85865

Files:
  llvm/lib/IR/ConstantFold.cpp
  llvm/lib/IR/Constants.cpp
  llvm/test/Transforms/InstSimplify/vscale.ll


Index: llvm/test/Transforms/InstSimplify/vscale.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/vscale.ll
+++ llvm/test/Transforms/InstSimplify/vscale.ll
@@ -95,6 +95,14 @@
   ret i32 %r
 }
 
+define i32 @insert_extract_element_same_vec_idx_3() {
+; CHECK-LABEL: @insert_extract_element_same_vec_idx_3(
+; CHECK-NEXT:    ret i32 1
+;
+  %r = extractelement <vscale x 4 x i32> insertelement (<vscale x 4 x i32> undef, i32 1, i64 4), i64 4
+  ret i32 %r
+}
+
 ; more complicated expressions
 
 define <vscale x 2 x i1> @cmp_le_smax_always_true(<vscale x 2 x i64> %x) {
Index: llvm/lib/IR/Constants.cpp
===================================================================
--- llvm/lib/IR/Constants.cpp
+++ llvm/lib/IR/Constants.cpp
@@ -409,9 +409,19 @@
   if (const UndefValue *UV = dyn_cast<UndefValue>(this))
     return Elt < UV->getNumElements() ? UV->getElementValue(Elt) : nullptr;
 
-  if (const ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(this))
+  if (const ConstantDataSequential *CDS =
+          dyn_cast<ConstantDataSequential>(this))
     return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt)
                                        : nullptr;
+
+  if (const auto *IE = dyn_cast<InsertElementConstantExpr>(this)) {
+    if (const auto *IEIdx = dyn_cast<ConstantInt>(IE->getOperand(2))) {
+      if (IEIdx->equalsInt(Elt)) {
+        if (auto *IEValue = dyn_cast<Constant>(IE->getOperand(1)))
+          return IEValue;
+      }
+    }
+  }
   return nullptr;
 }
 
Index: llvm/lib/IR/ConstantFold.cpp
===================================================================
--- llvm/lib/IR/ConstantFold.cpp
+++ llvm/lib/IR/ConstantFold.cpp
@@ -863,7 +863,6 @@
       if (auto *CAZ = dyn_cast<ConstantAggregateZero>(Val))
         return CAZ->getElementValue(CIdx->getZExtValue());
     }
-    return nullptr;
   }
 
   return Val->getAggregateElement(CIdx);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D85865.285212.patch
Type: text/x-patch
Size: 1955 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200812/9df4d11f/attachment.bin>


More information about the llvm-commits mailing list