Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 49551c1

Browse files
committed
[X86][SSE] Add selective commutation support for insertps (PR40340)
When we are inserting 1 "inline" element, and zeroing 2 of the other elements then we can safely commute the insertps source inputs to improve memory folding. Differential Revision: https://reviews.llvm.org/D56843 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@351807 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent cba5eef commit 49551c1

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

lib/Target/X86/X86InstrAVX512.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ defm : vinsert_for_mask_cast<"VINSERTI64x4Z", v32i8x_info, v64i8_info,
752752

753753
// vinsertps - insert f32 to XMM
754754
let ExeDomain = SSEPackedSingle in {
755+
let isCommutable = 1 in
755756
def VINSERTPSZrr : AVX512AIi8<0x21, MRMSrcReg, (outs VR128X:$dst),
756757
(ins VR128X:$src1, VR128X:$src2, u8imm:$src3),
757758
"vinsertps\t{$src3, $src2, $src1, $dst|$dst, $src1, $src2, $src3}",

lib/Target/X86/X86InstrInfo.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,6 +1569,28 @@ MachineInstr *X86InstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI,
15691569
return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false,
15701570
OpIdx1, OpIdx2);
15711571
}
1572+
case X86::INSERTPSrr:
1573+
case X86::VINSERTPSrr:
1574+
case X86::VINSERTPSZrr: {
1575+
unsigned Imm = MI.getOperand(MI.getNumOperands() - 1).getImm();
1576+
unsigned ZMask = Imm & 15;
1577+
unsigned DstIdx = (Imm >> 4) & 3;
1578+
unsigned SrcIdx = (Imm >> 6) & 3;
1579+
1580+
// We can commute insertps if we zero 2 of the elements, the insertion is
1581+
// "inline" and we don't override the insertion with a zero.
1582+
if (DstIdx == SrcIdx && (ZMask & (1 << DstIdx)) == 0 &&
1583+
countPopulation(ZMask) == 2) {
1584+
unsigned AltIdx = findFirstSet((ZMask | (1 << DstIdx)) ^ 15);
1585+
assert(0 <= AltIdx && AltIdx < 4 && "Illegal insertion index");
1586+
unsigned AltImm = (AltIdx << 6) | (AltIdx << 4) | ZMask;
1587+
auto &WorkingMI = cloneIfNew(MI);
1588+
WorkingMI.getOperand(MI.getNumOperands() - 1).setImm(AltImm);
1589+
return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false,
1590+
OpIdx1, OpIdx2);
1591+
}
1592+
return nullptr;
1593+
}
15721594
case X86::MOVSDrr:
15731595
case X86::MOVSSrr:
15741596
case X86::VMOVSDrr:

lib/Target/X86/X86InstrSSE.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5651,6 +5651,7 @@ let Constraints = "$src1 = $dst" in
56515651
// vector. The next one matches the intrinsic and could zero arbitrary elements
56525652
// in the target vector.
56535653
multiclass SS41I_insertf32<bits<8> opc, string asm, bit Is2Addr = 1> {
5654+
let isCommutable = 1 in
56545655
def rr : SS4AIi8<opc, MRMSrcReg, (outs VR128:$dst),
56555656
(ins VR128:$src1, VR128:$src2, u8imm:$src3),
56565657
!if(Is2Addr,

test/CodeGen/X86/insertps-combine.ll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,15 +302,12 @@ define float @extract_lane_insertps_6123(<4 x float> %a0, <4 x float> *%p1) {
302302
define <4 x float> @commute_load_insertps(<4 x float>, <4 x float>* nocapture readonly) {
303303
; SSE-LABEL: commute_load_insertps:
304304
; SSE: # %bb.0:
305-
; SSE-NEXT: movaps (%rdi), %xmm1
306-
; SSE-NEXT: insertps {{.*#+}} xmm1 = zero,xmm0[1],zero,xmm1[3]
307-
; SSE-NEXT: movaps %xmm1, %xmm0
305+
; SSE-NEXT: insertps {{.*#+}} xmm0 = zero,xmm0[1],zero,mem[0]
308306
; SSE-NEXT: retq
309307
;
310308
; AVX-LABEL: commute_load_insertps:
311309
; AVX: # %bb.0:
312-
; AVX-NEXT: vmovaps (%rdi), %xmm1
313-
; AVX-NEXT: vinsertps {{.*#+}} xmm0 = zero,xmm0[1],zero,xmm1[3]
310+
; AVX-NEXT: vinsertps {{.*#+}} xmm0 = zero,xmm0[1],zero,mem[0]
314311
; AVX-NEXT: retq
315312
%3 = load <4 x float>, <4 x float>* %1
316313
%4 = tail call <4 x float> @llvm.x86.sse41.insertps(<4 x float> %3, <4 x float> %0, i8 85)

0 commit comments

Comments
 (0)