Skip to content

Commit

Permalink
[RISCV] Avoid infinite loop between DAGCombiner::visitMUL and RISCVIS…
Browse files Browse the repository at this point in the history
…elLowering::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)
  • Loading branch information
asb authored and tstellar committed Feb 28, 2022
1 parent 61e78c6 commit ee0ae47
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Expand Up @@ -7203,6 +7203,11 @@ static SDValue transformAddImmMulImm(SDNode *N, SelectionDAG &DAG,
auto *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1));
if (!N0C || !N1C)
return SDValue();
// If N0C has multiple uses it's possible one of the cases in
// DAGCombiner::isMulAddWithConstProfitable will be true, which would result
// in an infinite loop.
if (!N0C->hasOneUse())
return SDValue();
int64_t C0 = N0C->getSExtValue();
int64_t C1 = N1C->getSExtValue();
int64_t CA, CB;
Expand Down
13 changes: 13 additions & 0 deletions llvm/test/CodeGen/RISCV/addimm-mulimm.ll
Expand Up @@ -872,3 +872,16 @@ define i64 @mulneg3000_sub8990_c(i64 %x) {
%tmp1 = add i64 %tmp0, -8990
ret i64 %tmp1
}

; This test case previously caused an infinite loop between transformations
; performed in RISCVISelLowering;:transformAddImmMulImm and
; DAGCombiner::visitMUL.
define i1 @pr53831(i32 %x) {
%tmp0 = add i32 %x, 1
%tmp1 = mul i32 %tmp0, 24
%tmp2 = add i32 %tmp1, 1
%tmp3 = mul i32 %x, 24
%tmp4 = add i32 %tmp3, 2048
%tmp5 = icmp eq i32 %tmp4, %tmp2
ret i1 %tmp5
}

0 comments on commit ee0ae47

Please sign in to comment.