[PATCH] D125951: [InstCombine] bitcast (extractelement (bitcast x), index) -> X

Chenbing.Zheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 19 00:51:40 PDT 2022


Chenbing.Zheng created this revision.
Chenbing.Zheng added reviewers: spatel, RKSimon, benshi001.
Chenbing.Zheng added a project: LLVM.
Herald added subscribers: arphaman, hiraditya.
Herald added a project: All.
Chenbing.Zheng requested review of this revision.
Herald added subscribers: llvm-commits, jacquesguan.

We can fold bitcast (extractelement (bitcast x), index) -> X when
X->getType() is same to DestType of bitcast.

This patch base on my previous patch D125857 <https://reviews.llvm.org/D125857>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125951

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
  llvm/test/Transforms/InstCombine/bitcast-inseltpoison.ll
  llvm/test/Transforms/InstCombine/bitcast.ll


Index: llvm/test/Transforms/InstCombine/bitcast.ll
===================================================================
--- llvm/test/Transforms/InstCombine/bitcast.ll
+++ llvm/test/Transforms/InstCombine/bitcast.ll
@@ -358,14 +358,9 @@
   ret i64 %bc2
 }
 
-; TODO: This should return %A.
-
 define <2 x i32> @bitcast_extelt3(<2 x i32> %A) {
 ; CHECK-LABEL: @bitcast_extelt3(
-; CHECK-NEXT:    [[BC1:%.*]] = bitcast <2 x i32> [[A:%.*]] to <1 x i64>
-; CHECK-NEXT:    [[EXT:%.*]] = extractelement <1 x i64> [[BC1]], i64 0
-; CHECK-NEXT:    [[BC2:%.*]] = bitcast i64 [[EXT]] to <2 x i32>
-; CHECK-NEXT:    ret <2 x i32> [[BC2]]
+; CHECK-NEXT:    ret <2 x i32> [[A:%.*]]
 ;
   %bc1 = bitcast <2 x i32> %A to <1 x i64>
   %ext = extractelement <1 x i64> %bc1, i32 0
Index: llvm/test/Transforms/InstCombine/bitcast-inseltpoison.ll
===================================================================
--- llvm/test/Transforms/InstCombine/bitcast-inseltpoison.ll
+++ llvm/test/Transforms/InstCombine/bitcast-inseltpoison.ll
@@ -358,14 +358,9 @@
   ret i64 %bc2
 }
 
-; TODO: This should return %A.
-
 define <2 x i32> @bitcast_extelt3(<2 x i32> %A) {
 ; CHECK-LABEL: @bitcast_extelt3(
-; CHECK-NEXT:    [[BC1:%.*]] = bitcast <2 x i32> [[A:%.*]] to <1 x i64>
-; CHECK-NEXT:    [[EXT:%.*]] = extractelement <1 x i64> [[BC1]], i64 0
-; CHECK-NEXT:    [[BC2:%.*]] = bitcast i64 [[EXT]] to <2 x i32>
-; CHECK-NEXT:    ret <2 x i32> [[BC2]]
+; CHECK-NEXT:    ret <2 x i32> [[A:%.*]]
 ;
   %bc1 = bitcast <2 x i32> %A to <1 x i64>
   %ext = extractelement <1 x i64> %bc1, i32 0
Index: llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -2358,13 +2358,19 @@
   // The bitcast must be to a vectorizable type, otherwise we can't make a new
   // type to extract from.
   Type *DestType = BitCast.getType();
-  if (!VectorType::isValidElementType(DestType))
-    return nullptr;
+  if (VectorType::isValidElementType(DestType)) {
+    auto *NewVecType =
+        VectorType::get(DestType, cast<VectorType>(VecOp->getType()));
+    auto *NewBC = IC.Builder.CreateBitCast(VecOp, NewVecType, "bc");
+    return ExtractElementInst::Create(NewBC, Index);
+  }
 
-  auto *NewVecType =
-      VectorType::get(DestType, cast<VectorType>(VecOp->getType()));
-  auto *NewBC = IC.Builder.CreateBitCast(VecOp, NewVecType, "bc");
-  return ExtractElementInst::Create(NewBC, Index);
+  // bitcast (extractelement (bitcast x), index) -> X
+  Value *X;
+  if (match(VecOp, m_OneUse(m_BitCast(m_Value(X)))) && X->getType() == DestType)
+    return IC.replaceInstUsesWith(BitCast, X);
+
+  return nullptr;
 }
 
 /// Change the type of a bitwise logic operation if we can eliminate a bitcast.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125951.430595.patch
Type: text/x-patch
Size: 2842 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220519/34cfee86/attachment.bin>


More information about the llvm-commits mailing list