[PATCH] D135625: [instcombine] (extelt (inselt Vec, Value, Index), Index) -> Value

Daniel Sanders via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 10 15:42:56 PDT 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG4a95a64e4ad8: [instcombine] (extelt (inselt Vec, Value, Index), Index) -> Value (authored by dsanders).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D135625

Files:
  llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
  llvm/test/Transforms/InstCombine/vector-inseltextelt.ll


Index: llvm/test/Transforms/InstCombine/vector-inseltextelt.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/InstCombine/vector-inseltextelt.ll
@@ -0,0 +1,23 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+; extracting a just-inserted element should yield the original value
+define float @constant_index(<4 x float> %x, float %val) {
+; CHECK-LABEL: @constant_index(
+; CHECK-NEXT:    ret float [[VAL:%.*]]
+;
+  %ins = insertelement <4 x float> %x, float %val, i32 1
+  %ext = extractelement <4 x float> %ins, i32 1
+  ret float %ext
+}
+
+; extracting a just-inserted element should yield the original value even if
+; the index is dynamic
+define float @dynamic_index(<4 x float> %x, float %val, i32 %idx) {
+; CHECK-LABEL: @dynamic_index(
+; CHECK-NEXT:    ret float [[VAL:%.*]]
+;
+  %ins = insertelement <4 x float> %x, float %val, i32 %idx
+  %ext = extractelement <4 x float> %ins, i32 %idx
+  ret float %ext
+}
Index: llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
@@ -474,6 +474,11 @@
 
   if (auto *I = dyn_cast<Instruction>(SrcVec)) {
     if (auto *IE = dyn_cast<InsertElementInst>(I)) {
+      // If the possibly-variable indices are trivially known to be equal
+      // (because they are the same operand) then use the value that was
+      // inserted directly.
+      if (IE->getOperand(2) == Index)
+        return replaceInstUsesWith(EI, IE->getOperand(1));
       // instsimplify already handled the case where the indices are constants
       // and equal by value, if both are constants, they must not be the same
       // value, extract from the pre-inserted value instead.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135625.466651.patch
Type: text/x-patch
Size: 1949 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221010/bc0d19b6/attachment.bin>


More information about the llvm-commits mailing list