[PATCH] D100227: [AArch64] Teach InstCombine to optimize coercion through an undef vector

Andrew Savonichev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 9 13:16:03 PDT 2021


asavonic created this revision.
Herald added subscribers: danielkiss, hiraditya, kristof.beyls.
asavonic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Clang (after D100225 <https://reviews.llvm.org/D100225>) generates the following sequence as a way to
keep information about undefined bits in a coerced value:

  %call = tail call i64 @_Z3fooi(i32 %x) #2
  %coerce.val.ii = trunc i64 %call to i32
  %coerce.val.vec = insertelement <2 x i32> undef, i32 %coerce.val.ii, i8 0
  %coerce.val.vec.ii = bitcast <2 x i32> %coerce.val.vec to i64
  ret i64 %coerce.val.vec.ii

This can be folded to just call + ret, because higher bits are known
to be undef.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D100227

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
  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
@@ -571,3 +571,28 @@
   %ptr = bitcast <1 x i8*> %ptrvec to i8*
   ret i8* %ptr
 }
+
+define i64 @bitcast_from_undef_insert_elem(i64 %val) {
+; CHECK-LABEL: @bitcast_from_undef_insert_elem
+; CHECK-SAME:    (i64 %[[VAL:.*]])
+; CHECK-NEXT:    ret i64 %[[VAL]]
+;
+  %coerce.val.ii = trunc i64 %val to i32
+  %coerce.val.vec = insertelement <2 x i32> undef, i32 %coerce.val.ii, i8 0
+  %coerce.val.vec.ii = bitcast <2 x i32> %coerce.val.vec to i64
+  ret i64 %coerce.val.vec.ii
+}
+
+define i64 @bitcast_from_undef_insert_elem_incompatible_size(i32 %val) {
+; CHECK-LABEL: @bitcast_from_undef_insert_elem
+; CHECK-SAME:    (i32 %[[VAL:.*]])
+; CHECK-NEXT:    %[[COERCE:.*]] = trunc i32 %[[VAL]] to i16
+; CHECK-NEXT:    %[[VEC:.*]] = insertelement <4 x i16> undef, i16 %[[COERCE]], i8 0
+; CHECK-NEXT:    %[[BITCAST:.*]] = bitcast <4 x i16> %[[VEC]] to i64
+; CHECK-NEXT:    ret i64 %[[BITCAST]]
+;
+  %coerce.val.ii = trunc i32 %val to i16
+  %coerce.val.vec = insertelement <4 x i16> undef, i16 %coerce.val.ii, i8 0
+  %coerce.val.vec.ii = bitcast <4 x i16> %coerce.val.vec to i64
+  ret i64 %coerce.val.vec.ii
+}
Index: llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -2359,6 +2359,28 @@
   return nullptr;
 }
 
+// Optimize bitcast(insertelt(undef,trunc(X))) --> bitcast(X)
+static Value *foldBitCastUndefInsElem(BitCastInst &BitCast,
+                                      InstCombiner::BuilderTy &Builder) {
+  Value *OrigVal;
+  uint64_t Idx = ~0U;
+  if (!match(BitCast.getOperand(0),
+             m_OneUse(m_InsertElt(m_Undef(), m_Trunc(m_Value(OrigVal)),
+                                  m_ConstantInt(Idx))))) {
+    return nullptr;
+  }
+
+  if (Idx != 0)
+    return nullptr;
+
+  if (OrigVal->getType()->getScalarSizeInBits() !=
+      BitCast.getType()->getScalarSizeInBits()) {
+    return nullptr;
+  }
+
+  return Builder.CreateBitCast(OrigVal, BitCast.getType());
+}
+
 /// Check if all users of CI are StoreInsts.
 static bool hasStoreUsersOnly(CastInst &CI) {
   for (User *U : CI.users()) {
@@ -2720,6 +2742,9 @@
   if (Instruction *I = foldBitCastSelect(CI, Builder))
     return I;
 
+  if (Value *V = foldBitCastUndefInsElem(CI, Builder))
+    return replaceInstUsesWith(CI, V);
+
   if (SrcTy->isPointerTy())
     return commonPointerCastTransforms(CI);
   return commonCastTransforms(CI);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D100227.336551.patch
Type: text/x-patch
Size: 2723 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210409/ba06ff53/attachment.bin>


More information about the llvm-commits mailing list