Skip to content

Commit 4c973ae

Browse files
committed
[PowerPC] Reduce symmetrical swaps for lane-insensitive vector ops
This patch simplifies pattern (xxswap (vec-op (xxswap a) (xxswap b))) into (vec-op a b) if vec-op is lane-insensitive. The motivating case is ScalarToVector-VecOp-ExtractElement sequence on LE, but the peephole itself is not related to endianness, so BE may also benefit from this. Reviewed By: nemanjai Differential Revision: https://reviews.llvm.org/D97658
1 parent cd6d179 commit 4c973ae

2 files changed

Lines changed: 157 additions & 0 deletions

File tree

‎llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6717,6 +6717,102 @@ void PPCDAGToDAGISel::PeepholePPC64ZExt() {
67176717
CurDAG->RemoveDeadNodes();
67186718
}
67196719

6720+
static bool isVSXSwap(SDValue N) {
6721+
if (!N->isMachineOpcode())
6722+
return false;
6723+
unsigned Opc = N->getMachineOpcode();
6724+
6725+
// Single-operand XXPERMDI or the regular XXPERMDI/XXSLDWI where the immediate
6726+
// operand is 2.
6727+
if (Opc == PPC::XXPERMDIs) {
6728+
return isa<ConstantSDNode>(N->getOperand(1)) &&
6729+
N->getConstantOperandVal(1) == 2;
6730+
} else if (Opc == PPC::XXPERMDI || Opc == PPC::XXSLDWI) {
6731+
return N->getOperand(0) == N->getOperand(1) &&
6732+
isa<ConstantSDNode>(N->getOperand(2)) &&
6733+
N->getConstantOperandVal(2) == 2;
6734+
}
6735+
6736+
return false;
6737+
}
6738+
6739+
// TODO: Make this complete and replace with a table-gen bit.
6740+
static bool isLaneInsensitive(SDValue N) {
6741+
if (!N->isMachineOpcode())
6742+
return false;
6743+
unsigned Opc = N->getMachineOpcode();
6744+
6745+
switch (Opc) {
6746+
default:
6747+
return false;
6748+
case PPC::VAVGSB:
6749+
case PPC::VAVGUB:
6750+
case PPC::VAVGSH:
6751+
case PPC::VAVGUH:
6752+
case PPC::VAVGSW:
6753+
case PPC::VAVGUW:
6754+
case PPC::VMAXFP:
6755+
case PPC::VMAXSB:
6756+
case PPC::VMAXUB:
6757+
case PPC::VMAXSH:
6758+
case PPC::VMAXUH:
6759+
case PPC::VMAXSW:
6760+
case PPC::VMAXUW:
6761+
case PPC::VMINFP:
6762+
case PPC::VMINSB:
6763+
case PPC::VMINUB:
6764+
case PPC::VMINSH:
6765+
case PPC::VMINUH:
6766+
case PPC::VMINSW:
6767+
case PPC::VMINUW:
6768+
case PPC::VADDFP:
6769+
case PPC::VADDUBM:
6770+
case PPC::VADDUHM:
6771+
case PPC::VADDUWM:
6772+
case PPC::VSUBFP:
6773+
case PPC::VSUBUBM:
6774+
case PPC::VSUBUHM:
6775+
case PPC::VSUBUWM:
6776+
case PPC::VAND:
6777+
case PPC::VANDC:
6778+
case PPC::VOR:
6779+
case PPC::VORC:
6780+
case PPC::VXOR:
6781+
case PPC::VNOR:
6782+
case PPC::VMULUWM:
6783+
return true;
6784+
}
6785+
}
6786+
6787+
// Try to simplify (xxswap (vec-op (xxswap) (xxswap))) where vec-op is
6788+
// lane-insensitive.
6789+
static void reduceVSXSwap(SDNode *N, SelectionDAG *DAG) {
6790+
// Our desired xxswap might be source of COPY_TO_REGCLASS.
6791+
// TODO: Can we put this a common method for DAG?
6792+
auto SkipRCCopy = [](SDValue V) {
6793+
while (V->isMachineOpcode() &&
6794+
V->getMachineOpcode() == TargetOpcode::COPY_TO_REGCLASS)
6795+
V = V->getOperand(0);
6796+
return V;
6797+
};
6798+
6799+
SDValue VecOp = SkipRCCopy(N->getOperand(0));
6800+
if (!isLaneInsensitive(VecOp))
6801+
return;
6802+
6803+
SDValue LHS = SkipRCCopy(VecOp.getOperand(0)),
6804+
RHS = SkipRCCopy(VecOp.getOperand(1));
6805+
if (!LHS.hasOneUse() || !RHS.hasOneUse() || !isVSXSwap(LHS) ||
6806+
!isVSXSwap(RHS))
6807+
return;
6808+
6809+
// These swaps may still have chain-uses here, count on dead code elimination
6810+
// in following passes to remove them.
6811+
DAG->ReplaceAllUsesOfValueWith(LHS, LHS.getOperand(0));
6812+
DAG->ReplaceAllUsesOfValueWith(RHS, RHS.getOperand(0));
6813+
DAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), N->getOperand(0));
6814+
}
6815+
67206816
void PPCDAGToDAGISel::PeepholePPC64() {
67216817
SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_end();
67226818

@@ -6726,6 +6822,9 @@ void PPCDAGToDAGISel::PeepholePPC64() {
67266822
if (N->use_empty() || !N->isMachineOpcode())
67276823
continue;
67286824

6825+
if (isVSXSwap(SDValue(N, 0)))
6826+
reduceVSXSwap(N, CurDAG);
6827+
67296828
unsigned FirstOp;
67306829
unsigned StorageOpcode = N->getMachineOpcode();
67316830
bool RequiresMod4Offset = false;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: llc -verify-machineinstrs -mcpu=pwr8 -mtriple=powerpc64le < %s | FileCheck %s
3+
4+
define i64 @test1(i64* %a, i64* %b) {
5+
; CHECK-LABEL: test1:
6+
; CHECK: # %bb.0: # %entry
7+
; CHECK-NEXT: mr 5, 3
8+
; CHECK-NEXT: ld 3, 0(3)
9+
; CHECK-NEXT: ld 4, 0(4)
10+
; CHECK-NEXT: mtvsrd 34, 3
11+
; CHECK-NEXT: add 3, 3, 4
12+
; CHECK-NEXT: mtvsrd 35, 4
13+
; CHECK-NEXT: vavgsb 2, 2, 3
14+
; CHECK-NEXT: stxsdx 34, 0, 5
15+
; CHECK-NEXT: blr
16+
entry:
17+
%lhs = load i64, i64* %a, align 8
18+
%rhs = load i64, i64* %b, align 8
19+
%sum = add i64 %lhs, %rhs
20+
%lv = insertelement <2 x i64> undef, i64 %lhs, i32 0
21+
%rv = insertelement <2 x i64> undef, i64 %rhs, i32 0
22+
%lhc = bitcast <2 x i64> %lv to <16 x i8>
23+
%rhc = bitcast <2 x i64> %rv to <16 x i8>
24+
%add = call <16 x i8> @llvm.ppc.altivec.vavgsb(<16 x i8> %lhc, <16 x i8> %rhc)
25+
%cb = bitcast <16 x i8> %add to <2 x i64>
26+
%fv = extractelement <2 x i64> %cb, i32 0
27+
store i64 %fv, i64* %a, align 8
28+
ret i64 %sum
29+
}
30+
31+
define i64 @test2(i64* %a, i64* %b) {
32+
; CHECK-LABEL: test2:
33+
; CHECK: # %bb.0: # %entry
34+
; CHECK-NEXT: mr 5, 3
35+
; CHECK-NEXT: ld 3, 0(3)
36+
; CHECK-NEXT: ld 4, 0(4)
37+
; CHECK-NEXT: mtvsrd 34, 3
38+
; CHECK-NEXT: add 3, 3, 4
39+
; CHECK-NEXT: mtvsrd 35, 4
40+
; CHECK-NEXT: vadduhm 2, 2, 3
41+
; CHECK-NEXT: stxsdx 34, 0, 5
42+
; CHECK-NEXT: blr
43+
entry:
44+
%lhs = load i64, i64* %a, align 8
45+
%rhs = load i64, i64* %b, align 8
46+
%sum = add i64 %lhs, %rhs
47+
%lv = insertelement <2 x i64> undef, i64 %lhs, i32 0
48+
%rv = insertelement <2 x i64> undef, i64 %rhs, i32 0
49+
%lhc = bitcast <2 x i64> %lv to <8 x i16>
50+
%rhc = bitcast <2 x i64> %rv to <8 x i16>
51+
%add = add <8 x i16> %lhc, %rhc
52+
%cb = bitcast <8 x i16> %add to <2 x i64>
53+
%fv = extractelement <2 x i64> %cb, i32 0
54+
store i64 %fv, i64* %a, align 8
55+
ret i64 %sum
56+
}
57+
58+
declare <16 x i8> @llvm.ppc.altivec.vavgsb(<16 x i8>, <16 x i8>)

0 commit comments

Comments
 (0)