Skip to content

Commit e7015f3

Browse files
Perform optimziation via DAGCombine + TLI
1 parent 571b61f commit e7015f3

File tree

4 files changed

+16
-15
lines changed

4 files changed

+16
-15
lines changed

llvm/include/llvm/CodeGen/TargetLowering.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2975,6 +2975,8 @@ class TargetLoweringBase {
29752975
return isTruncateFree(Val.getValueType(), VT2);
29762976
}
29772977

2978+
virtual bool shouldReduceRegisterPressure() const { return false; }
2979+
29782980
virtual bool isProfitableToHoist(Instruction *I) const { return true; }
29792981

29802982
/// Return true if the extension represented by \p I is free.

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5797,7 +5797,7 @@ SDValue DAGCombiner::hoistLogicOpWithSameOpcodeHands(SDNode *N) {
57975797
}
57985798

57995799
// logic_op (truncate x), (truncate y) --> truncate (logic_op x, y)
5800-
if (HandOpcode == ISD::TRUNCATE) {
5800+
if (HandOpcode == ISD::TRUNCATE && !TLI.shouldReduceRegisterPressure()) {
58015801
// If both operands have other uses, this transform would create extra
58025802
// instructions without eliminating anything.
58035803
if (!N0.hasOneUse() && !N1.hasOneUse())
@@ -15155,7 +15155,8 @@ SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
1515515155
case ISD::OR:
1515615156
case ISD::XOR:
1515715157
if (!LegalOperations && N0.hasOneUse() &&
15158-
(isConstantOrConstantVector(N0.getOperand(0), true) ||
15158+
(TLI.shouldReduceRegisterPressure() ||
15159+
isConstantOrConstantVector(N0.getOperand(0), true) ||
1515915160
isConstantOrConstantVector(N0.getOperand(1), true))) {
1516015161
// TODO: We already restricted this to pre-legalization, but for vectors
1516115162
// we are extra cautious to not create an unsupported operation.

llvm/lib/Target/NVPTX/NVPTXISelLowering.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,10 @@ class NVPTXTargetLowering : public TargetLowering {
502502
DstTy->getPrimitiveSizeInBits() == 32;
503503
}
504504

505+
bool shouldReduceRegisterPressure() const override {
506+
return true;
507+
}
508+
505509
EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
506510
EVT VT) const override {
507511
if (VT.isVector())

llvm/test/CodeGen/NVPTX/combine-truncate.ll

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ define i32 @trunc(i64 %a, i64 %b) {
88
; CHECK-LABEL: trunc(
99
; CHECK: {
1010
; CHECK-NEXT: .reg .b32 %r<4>;
11-
; CHECK-NEXT: .reg .b64 %rd<3>;
1211
; CHECK-EMPTY:
1312
; CHECK-NEXT: // %bb.0:
14-
; CHECK-NEXT: ld.param.u64 %rd1, [trunc_param_0];
15-
; CHECK-NEXT: ld.param.u64 %rd2, [trunc_param_1];
16-
; CHECK-NEXT: cvt.u32.u64 %r1, %rd2;
17-
; CHECK-NEXT: cvt.u32.u64 %r2, %rd1;
18-
; CHECK-NEXT: or.b32 %r3, %r2, %r1;
13+
; CHECK-NEXT: ld.param.u32 %r1, [trunc_param_0];
14+
; CHECK-NEXT: ld.param.u32 %r2, [trunc_param_1];
15+
; CHECK-NEXT: or.b32 %r3, %r1, %r2;
1916
; CHECK-NEXT: st.param.b32 [func_retval0+0], %r3;
2017
; CHECK-NEXT: ret;
2118
%or = or i64 %a, %b
@@ -48,15 +45,12 @@ define i32 @trunc_cvt(i64 %a, i64 %b) {
4845
; CHECK-LABEL: trunc_cvt(
4946
; CHECK: {
5047
; CHECK-NEXT: .reg .b32 %r<5>;
51-
; CHECK-NEXT: .reg .b64 %rd<3>;
5248
; CHECK-EMPTY:
5349
; CHECK-NEXT: // %bb.0:
54-
; CHECK-NEXT: ld.param.u64 %rd1, [trunc_cvt_param_0];
55-
; CHECK-NEXT: ld.param.u64 %rd2, [trunc_cvt_param_1];
56-
; CHECK-NEXT: cvt.u32.u64 %r1, %rd2;
57-
; CHECK-NEXT: cvt.u32.u64 %r2, %rd1;
58-
; CHECK-NEXT: add.s32 %r3, %r2, %r1;
59-
; CHECK-NEXT: or.b32 %r4, %r3, %r2;
50+
; CHECK-NEXT: ld.param.u32 %r1, [trunc_cvt_param_0];
51+
; CHECK-NEXT: ld.param.u32 %r2, [trunc_cvt_param_1];
52+
; CHECK-NEXT: add.s32 %r3, %r1, %r2;
53+
; CHECK-NEXT: or.b32 %r4, %r3, %r1;
6054
; CHECK-NEXT: st.param.b32 [func_retval0+0], %r4;
6155
; CHECK-NEXT: ret;
6256
%add = add i64 %a, %b

0 commit comments

Comments
 (0)