Skip to content

Commit 4d11f04

Browse files
[InstCombine] Try to fold trunc(shuffle(zext)) to just a shuffle (#78636)
Tries to remove extra trunc/ext instruction for shufflevector instructions.
1 parent a2a0089 commit 4d11f04

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ Value *InstCombinerImpl::EvaluateInDifferentType(Value *V, Type *Ty,
103103
}
104104
}
105105
break;
106+
case Instruction::ShuffleVector: {
107+
Value *Op0 = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned);
108+
Value *Op1 = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
109+
Res = new ShuffleVectorInst(Op0, Op1,
110+
cast<ShuffleVectorInst>(I)->getShuffleMask());
111+
break;
112+
}
106113
default:
107114
// TODO: Can handle more cases here.
108115
llvm_unreachable("Unreachable!");
@@ -363,6 +370,9 @@ static bool canEvaluateTruncated(Value *V, Type *Ty, InstCombinerImpl &IC,
363370
I->getOpcode() == Instruction::FPToSI);
364371
return Ty->getScalarSizeInBits() >= MinBitWidth;
365372
}
373+
case Instruction::ShuffleVector:
374+
return canEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI) &&
375+
canEvaluateTruncated(I->getOperand(1), Ty, IC, CxtI);
366376
default:
367377
// TODO: Can handle more cases here.
368378
break;

llvm/test/Transforms/InstCombine/logical-select-inseltpoison.ll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -671,11 +671,8 @@ define <4 x i32> @computesignbits_through_shuffles(<4 x float> %x, <4 x float> %
671671

672672
define <4 x i32> @computesignbits_through_two_input_shuffle(<4 x i32> %x, <4 x i32> %y, <4 x i1> %cond1, <4 x i1> %cond2) {
673673
; CHECK-LABEL: @computesignbits_through_two_input_shuffle(
674-
; CHECK-NEXT: [[SEXT1:%.*]] = sext <4 x i1> [[COND1:%.*]] to <4 x i32>
675-
; CHECK-NEXT: [[SEXT2:%.*]] = sext <4 x i1> [[COND2:%.*]] to <4 x i32>
676-
; CHECK-NEXT: [[COND:%.*]] = shufflevector <4 x i32> [[SEXT1]], <4 x i32> [[SEXT2]], <4 x i32> <i32 0, i32 2, i32 4, i32 6>
677-
; CHECK-NEXT: [[TMP1:%.*]] = trunc <4 x i32> [[COND]] to <4 x i1>
678-
; CHECK-NEXT: [[SEL:%.*]] = select <4 x i1> [[TMP1]], <4 x i32> [[Y:%.*]], <4 x i32> [[X:%.*]]
674+
; CHECK-NEXT: [[COND:%.*]] = shufflevector <4 x i1> [[COND1:%.*]], <4 x i1> [[COND2:%.*]], <4 x i32> <i32 0, i32 2, i32 4, i32 6>
675+
; CHECK-NEXT: [[SEL:%.*]] = select <4 x i1> [[COND]], <4 x i32> [[Y:%.*]], <4 x i32> [[X:%.*]]
679676
; CHECK-NEXT: ret <4 x i32> [[SEL]]
680677
;
681678
%sext1 = sext <4 x i1> %cond1 to <4 x i32>

llvm/test/Transforms/InstCombine/logical-select.ll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -707,11 +707,8 @@ define <4 x i32> @computesignbits_through_shuffles(<4 x float> %x, <4 x float> %
707707

708708
define <4 x i32> @computesignbits_through_two_input_shuffle(<4 x i32> %x, <4 x i32> %y, <4 x i1> %cond1, <4 x i1> %cond2) {
709709
; CHECK-LABEL: @computesignbits_through_two_input_shuffle(
710-
; CHECK-NEXT: [[SEXT1:%.*]] = sext <4 x i1> [[COND1:%.*]] to <4 x i32>
711-
; CHECK-NEXT: [[SEXT2:%.*]] = sext <4 x i1> [[COND2:%.*]] to <4 x i32>
712-
; CHECK-NEXT: [[COND:%.*]] = shufflevector <4 x i32> [[SEXT1]], <4 x i32> [[SEXT2]], <4 x i32> <i32 0, i32 2, i32 4, i32 6>
713-
; CHECK-NEXT: [[TMP1:%.*]] = trunc <4 x i32> [[COND]] to <4 x i1>
714-
; CHECK-NEXT: [[SEL:%.*]] = select <4 x i1> [[TMP1]], <4 x i32> [[Y:%.*]], <4 x i32> [[X:%.*]]
710+
; CHECK-NEXT: [[COND:%.*]] = shufflevector <4 x i1> [[COND1:%.*]], <4 x i1> [[COND2:%.*]], <4 x i32> <i32 0, i32 2, i32 4, i32 6>
711+
; CHECK-NEXT: [[SEL:%.*]] = select <4 x i1> [[COND]], <4 x i32> [[Y:%.*]], <4 x i32> [[X:%.*]]
715712
; CHECK-NEXT: ret <4 x i32> [[SEL]]
716713
;
717714
%sext1 = sext <4 x i1> %cond1 to <4 x i32>

0 commit comments

Comments
 (0)