diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp --- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp +++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp @@ -7477,6 +7477,11 @@ auto *N1C = dyn_cast(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; diff --git a/llvm/test/CodeGen/RISCV/addimm-mulimm.ll b/llvm/test/CodeGen/RISCV/addimm-mulimm.ll --- a/llvm/test/CodeGen/RISCV/addimm-mulimm.ll +++ b/llvm/test/CodeGen/RISCV/addimm-mulimm.ll @@ -872,3 +872,16 @@ %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 +}