Skip to content

Commit ee0ae47

Browse files
asbtstellar
authored andcommitted
[RISCV] Avoid infinite loop between DAGCombiner::visitMUL and RISCVISelLowering::transformAddImmMulImm
See #53831 for a full discussion. The basic issue is that DAGCombiner::visitMUL and RISCVISelLowering;:transformAddImmMullImm get stuck in a loop, as the current checks in transformAddImmMulImm aren't sufficient to avoid all cases where DAGCombiner::isMulAddWithConstProfitable might trigger a transformation. This patch makes transformAddImmMulImm bail out if C0 (the constant used for multiplication) has more than one use. Differential Revision: https://reviews.llvm.org/D120332 (cherry picked from commit c5bcfb9)
1 parent 61e78c6 commit ee0ae47

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7203,6 +7203,11 @@ static SDValue transformAddImmMulImm(SDNode *N, SelectionDAG &DAG,
72037203
auto *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1));
72047204
if (!N0C || !N1C)
72057205
return SDValue();
7206+
// If N0C has multiple uses it's possible one of the cases in
7207+
// DAGCombiner::isMulAddWithConstProfitable will be true, which would result
7208+
// in an infinite loop.
7209+
if (!N0C->hasOneUse())
7210+
return SDValue();
72067211
int64_t C0 = N0C->getSExtValue();
72077212
int64_t C1 = N1C->getSExtValue();
72087213
int64_t CA, CB;

llvm/test/CodeGen/RISCV/addimm-mulimm.ll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,3 +872,16 @@ define i64 @mulneg3000_sub8990_c(i64 %x) {
872872
%tmp1 = add i64 %tmp0, -8990
873873
ret i64 %tmp1
874874
}
875+
876+
; This test case previously caused an infinite loop between transformations
877+
; performed in RISCVISelLowering;:transformAddImmMulImm and
878+
; DAGCombiner::visitMUL.
879+
define i1 @pr53831(i32 %x) {
880+
%tmp0 = add i32 %x, 1
881+
%tmp1 = mul i32 %tmp0, 24
882+
%tmp2 = add i32 %tmp1, 1
883+
%tmp3 = mul i32 %x, 24
884+
%tmp4 = add i32 %tmp3, 2048
885+
%tmp5 = icmp eq i32 %tmp4, %tmp2
886+
ret i1 %tmp5
887+
}

0 commit comments

Comments
 (0)