[PATCH] D30123: [InstCombine] shrink truncated shuffle with constant operand
Sanjay Patel via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 17 15:50:50 PST 2017
spatel created this revision.
Herald added a subscriber: mcrosier.
This could be one part of solving a recent bug report:
http://lists.llvm.org/pipermail/llvm-dev/2017-February/110293.html
This keeps with our general approach to shuffles because the mask doesn't change. The transform is very similar to the existing shrinkBitwiseLogic() canonicalization.
https://reviews.llvm.org/D30123
Files:
lib/Transforms/InstCombine/InstCombineCasts.cpp
test/Transforms/InstCombine/trunc.ll
Index: test/Transforms/InstCombine/trunc.ll
===================================================================
--- test/Transforms/InstCombine/trunc.ll
+++ test/Transforms/InstCombine/trunc.ll
@@ -468,8 +468,8 @@
define <4 x i8> @shuf1(<4 x i32> %x) {
; CHECK-LABEL: @shuf1(
-; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i32> %x, <4 x i32> <i32 undef, i32 3634, i32 90, i32 undef>, <4 x i32> <i32 1, i32 5, i32 6, i32 2>
-; CHECK-NEXT: [[TRUNC:%.*]] = trunc <4 x i32> [[SHUF]] to <4 x i8>
+; CHECK-NEXT: [[TMP1:%.*]] = trunc <4 x i32> %x to <4 x i8>
+; CHECK-NEXT: [[TRUNC:%.*]] = shufflevector <4 x i8> [[TMP1]], <4 x i8> <i8 undef, i8 50, i8 90, i8 undef>, <4 x i32> <i32 1, i32 5, i32 6, i32 2>
; CHECK-NEXT: ret <4 x i8> [[TRUNC]]
;
%shuf = shufflevector <4 x i32> %x, <4 x i32> <i32 35, i32 3634, i32 90, i32 -1>, <4 x i32> <i32 1, i32 5, i32 6, i32 2>
@@ -482,8 +482,8 @@
define <4 x i8> @shuf2(<4 x i32> %x) {
; CHECK-LABEL: @shuf2(
-; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i32> <i32 -3500, i32 undef, i32 undef, i32 -1>, <4 x i32> %x, <4 x i32> <i32 3, i32 6, i32 6, i32 0>
-; CHECK-NEXT: [[TRUNC:%.*]] = trunc <4 x i32> [[SHUF]] to <4 x i8>
+; CHECK-NEXT: [[TMP1:%.*]] = trunc <4 x i32> %x to <4 x i8>
+; CHECK-NEXT: [[TRUNC:%.*]] = shufflevector <4 x i8> <i8 84, i8 undef, i8 undef, i8 -1>, <4 x i8> [[TMP1]], <4 x i32> <i32 3, i32 6, i32 6, i32 0>
; CHECK-NEXT: ret <4 x i8> [[TRUNC]]
;
%shuf = shufflevector <4 x i32> <i32 -3500, i32 3634, i32 90, i32 -1>, <4 x i32> %x, <4 x i32> <i32 3, i32 6, i32 6, i32 0>
Index: lib/Transforms/InstCombine/InstCombineCasts.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -463,6 +463,31 @@
return BinaryOperator::Create(LogicOp->getOpcode(), NarrowOp0, NarrowC);
}
+static Instruction *shrinkShuffle(TruncInst &Trunc,
+ InstCombiner::BuilderTy &Builder) {
+ auto *Shuf = dyn_cast<ShuffleVectorInst>(Trunc.getOperand(0));
+ if (!Shuf || !Shuf->hasOneUse())
+ return nullptr;
+
+ Type *DestTy = Trunc.getType();
+ if (auto *C = dyn_cast<Constant>(Shuf->getOperand(1))) {
+ // trunc (shuffle X, C, Mask) --> shuffle (trunc X), C', Mask
+ Constant *NarrowC = ConstantExpr::getTrunc(C, DestTy);
+ Value *NarrowOp = Builder.CreateTrunc(Shuf->getOperand(0), DestTy);
+ return new ShuffleVectorInst(NarrowOp, NarrowC, Shuf->getMask());
+ }
+ // TODO: Canonicalize constant to operand 1, so we don't need to match this
+ // pattern?
+ if (auto *C = dyn_cast<Constant>(Shuf->getOperand(0))) {
+ // trunc (shuffle C, X, Mask) --> shuffle C', (trunc X), Mask
+ Constant *NarrowC = ConstantExpr::getTrunc(C, DestTy);
+ Value *NarrowOp = Builder.CreateTrunc(Shuf->getOperand(1), DestTy);
+ return new ShuffleVectorInst(NarrowC, NarrowOp, Shuf->getMask());
+ }
+
+ return nullptr;
+}
+
Instruction *InstCombiner::visitTrunc(TruncInst &CI) {
if (Instruction *Result = commonCastTransforms(CI))
return Result;
@@ -554,6 +579,9 @@
if (Instruction *I = shrinkBitwiseLogic(CI))
return I;
+ if (Instruction *I = shrinkShuffle(CI, *Builder))
+ return I;
+
if (Src->hasOneUse() && isa<IntegerType>(SrcTy) &&
shouldChangeType(SrcTy, DestTy)) {
// Transform "trunc (shl X, cst)" -> "shl (trunc X), cst" so long as the
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30123.88977.patch
Type: text/x-patch
Size: 3447 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170217/6d2265d9/attachment.bin>
More information about the llvm-commits
mailing list