Skip to content

Commit 2f9a9b4

Browse files
SC llvm teamSC llvm team
SC llvm team
authored and
SC llvm team
committed
Merged main:be5ecc35efc902a4742669d41a88cfd88babb245 into amd-gfx:fee3baa081ca
Local branch amd-gfx fee3baa Merged main:99b85cae628c1cc5641944290712cd84ccf1f6c8 into amd-gfx:f09ee8740921 Remote branch main be5ecc3 [RISCV] Dont move source if passthru already dominates in vmv.v.v peephole (llvm#105792)
2 parents fee3baa + be5ecc3 commit 2f9a9b4

File tree

6 files changed

+134
-8
lines changed

6 files changed

+134
-8
lines changed

llvm/cmake/config-ix.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ if(C_SUPPORTS_WERROR_UNGUARDED_AVAILABILITY_NEW)
260260
endif()
261261

262262
check_cxx_symbol_exists(logf128 cmath HAS_LOGF128)
263-
check_symbol_exists(__powerpc64le__ "" __PPC64LE)
263+
check_symbol_exists(__powerpc__ "" __PPC64LE)
264264
if(HAS_LOGF128 AND NOT __PPC64LE)
265265
set(LLVM_HAS_LOGF128 On)
266266
add_compile_definitions(HAS_LOGF128)

llvm/include/llvm/Config/llvm-config.h.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
/* Indicate that this is LLVM compiled from the amd-gfx branch. */
1818
#define LLVM_HAVE_BRANCH_AMD_GFX
19-
#define LLVM_MAIN_REVISION 509529
19+
#define LLVM_MAIN_REVISION 509532
2020

2121
/* Define if LLVM_ENABLE_DUMP is enabled */
2222
#cmakedefine LLVM_ENABLE_DUMP

llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,22 @@ static bool isSafeToMove(const MachineInstr &From, const MachineInstr &To) {
437437
return From.isSafeToMove(SawStore);
438438
}
439439

440+
/// Given A and B are in the same MBB, returns true if A comes before B.
441+
static bool dominates(MachineBasicBlock::const_iterator A,
442+
MachineBasicBlock::const_iterator B) {
443+
assert(A->getParent() == B->getParent());
444+
const MachineBasicBlock *MBB = A->getParent();
445+
auto MBBEnd = MBB->end();
446+
if (B == MBBEnd)
447+
return true;
448+
449+
MachineBasicBlock::const_iterator I = MBB->begin();
450+
for (; &*I != A && &*I != B; ++I)
451+
;
452+
453+
return &*I == A;
454+
}
455+
440456
/// If a PseudoVMV_V_V is the only user of its input, fold its passthru and VL
441457
/// into it.
442458
///
@@ -481,12 +497,15 @@ bool RISCVVectorPeephole::foldVMV_V_V(MachineInstr &MI) {
481497
if (!isVLKnownLE(SrcVL, MI.getOperand(3)))
482498
return false;
483499

484-
// If Src ends up using MI's passthru/VL, move it so it can access it.
485-
// TODO: We don't need to do this if they already dominate Src.
486-
if (!SrcPassthru.isIdenticalTo(Passthru)) {
487-
if (!isSafeToMove(*Src, MI))
488-
return false;
489-
Src->moveBefore(&MI);
500+
// If the new passthru doesn't dominate Src, try to move Src so it does.
501+
if (Passthru.getReg() != RISCV::NoRegister) {
502+
MachineInstr *PassthruDef = MRI->getVRegDef(Passthru.getReg());
503+
if (PassthruDef->getParent() == Src->getParent() &&
504+
!dominates(PassthruDef, Src)) {
505+
if (!isSafeToMove(*Src, *PassthruDef->getNextNode()))
506+
return false;
507+
Src->moveBefore(PassthruDef->getNextNode());
508+
}
490509
}
491510

492511
if (SrcPassthru.getReg() != Passthru.getReg()) {

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4201,5 +4201,24 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
42014201
}
42024202
}
42034203

4204+
// select (trunc nuw X to i1), X, Y --> select (trunc nuw X to i1), 1, Y
4205+
// select (trunc nuw X to i1), Y, X --> select (trunc nuw X to i1), Y, 0
4206+
// select (trunc nsw X to i1), X, Y --> select (trunc nsw X to i1), -1, Y
4207+
// select (trunc nsw X to i1), Y, X --> select (trunc nsw X to i1), Y, 0
4208+
Value *Trunc;
4209+
if (match(CondVal, m_NUWTrunc(m_Value(Trunc)))) {
4210+
if (TrueVal == Trunc)
4211+
return replaceOperand(SI, 1, ConstantInt::get(TrueVal->getType(), 1));
4212+
if (FalseVal == Trunc)
4213+
return replaceOperand(SI, 2, ConstantInt::get(FalseVal->getType(), 0));
4214+
}
4215+
if (match(CondVal, m_NSWTrunc(m_Value(Trunc)))) {
4216+
if (TrueVal == Trunc)
4217+
return replaceOperand(SI, 1,
4218+
Constant::getAllOnesValue(TrueVal->getType()));
4219+
if (FalseVal == Trunc)
4220+
return replaceOperand(SI, 2, ConstantInt::get(FalseVal->getType(), 0));
4221+
}
4222+
42044223
return nullptr;
42054224
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 3
2+
# RUN: llc %s -o - -mtriple=riscv64 -mattr=+v -run-pass=riscv-vector-peephole \
3+
# RUN: -verify-machineinstrs | FileCheck %s
4+
5+
---
6+
name: move_src
7+
body: |
8+
bb.0:
9+
liveins: $v8
10+
; CHECK-LABEL: name: move_src
11+
; CHECK: liveins: $v8
12+
; CHECK-NEXT: {{ $}}
13+
; CHECK-NEXT: %passthru:vr = COPY $v8
14+
; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4, 5 /* e32 */, 0 /* tu, mu */
15+
; CHECK-NEXT: %y:gpr = ADDI $x0, 1
16+
%x:vr = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, 4, 5 /* e32 */, 0 /* tu, mu */
17+
%passthru:vr = COPY $v8
18+
%y:gpr = ADDI $x0, 1
19+
%z:vr = PseudoVMV_V_V_M1 %passthru, %x, 4, 5 /* e32 */, 0 /* tu, mu */
20+
...
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3+
4+
define i8 @fold_select_trunc_nuw_true(i8 %x, i8 %y) {
5+
; CHECK-LABEL: @fold_select_trunc_nuw_true(
6+
; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw i8 [[X:%.*]] to i1
7+
; CHECK-NEXT: [[RET:%.*]] = select i1 [[TRUNC]], i8 1, i8 [[Y:%.*]]
8+
; CHECK-NEXT: ret i8 [[RET]]
9+
;
10+
%trunc = trunc nuw i8 %x to i1
11+
%ret = select i1 %trunc, i8 %x, i8 %y
12+
ret i8 %ret
13+
}
14+
15+
define i8 @fold_select_trunc_nuw_false(i8 %x, i8 %y) {
16+
; CHECK-LABEL: @fold_select_trunc_nuw_false(
17+
; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw i8 [[X:%.*]] to i1
18+
; CHECK-NEXT: [[RET:%.*]] = select i1 [[TRUNC]], i8 [[Y:%.*]], i8 0
19+
; CHECK-NEXT: ret i8 [[RET]]
20+
;
21+
%trunc = trunc nuw i8 %x to i1
22+
%ret = select i1 %trunc, i8 %y, i8 %x
23+
ret i8 %ret
24+
}
25+
26+
define i128 @fold_select_trunc_nsw_true(i128 %x, i128 %y) {
27+
; CHECK-LABEL: @fold_select_trunc_nsw_true(
28+
; CHECK-NEXT: [[TRUNC:%.*]] = trunc nsw i128 [[X:%.*]] to i1
29+
; CHECK-NEXT: [[RET:%.*]] = select i1 [[TRUNC]], i128 -1, i128 [[Y:%.*]]
30+
; CHECK-NEXT: ret i128 [[RET]]
31+
;
32+
%trunc = trunc nsw i128 %x to i1
33+
%ret = select i1 %trunc, i128 %x, i128 %y
34+
ret i128 %ret
35+
}
36+
37+
define i8 @fold_select_trunc_nsw_false(i8 %x, i8 %y) {
38+
; CHECK-LABEL: @fold_select_trunc_nsw_false(
39+
; CHECK-NEXT: [[TRUNC:%.*]] = trunc nsw i8 [[X:%.*]] to i1
40+
; CHECK-NEXT: [[RET:%.*]] = select i1 [[TRUNC]], i8 [[Y:%.*]], i8 0
41+
; CHECK-NEXT: ret i8 [[RET]]
42+
;
43+
%trunc = trunc nsw i8 %x to i1
44+
%ret = select i1 %trunc, i8 %y, i8 %x
45+
ret i8 %ret
46+
}
47+
48+
define i8 @fold_select_trunc_negative(i8 %x, i8 %y) {
49+
; CHECK-LABEL: @fold_select_trunc_negative(
50+
; CHECK-NEXT: [[TRUNC:%.*]] = trunc i8 [[X:%.*]] to i1
51+
; CHECK-NEXT: [[RET:%.*]] = select i1 [[TRUNC]], i8 [[X]], i8 [[Y:%.*]]
52+
; CHECK-NEXT: ret i8 [[RET]]
53+
;
54+
%trunc = trunc i8 %x to i1
55+
%ret = select i1 %trunc, i8 %x, i8 %y
56+
ret i8 %ret
57+
}
58+
59+
define <2 x i8> @fold_select_trunc_vector(<2 x i8> %x, <2 x i8> %y) {
60+
; CHECK-LABEL: @fold_select_trunc_vector(
61+
; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw <2 x i8> [[X:%.*]] to <2 x i1>
62+
; CHECK-NEXT: [[RET:%.*]] = select <2 x i1> [[TRUNC]], <2 x i8> <i8 1, i8 1>, <2 x i8> [[Y:%.*]]
63+
; CHECK-NEXT: ret <2 x i8> [[RET]]
64+
;
65+
%trunc = trunc nuw <2 x i8> %x to <2 x i1>
66+
%ret = select <2 x i1> %trunc, <2 x i8> %x, <2 x i8> %y
67+
ret <2 x i8> %ret
68+
}

0 commit comments

Comments
 (0)