diff --git a/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp b/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp --- a/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp +++ b/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp @@ -2667,6 +2667,20 @@ bool HasChainOp = True.getOperand(True.getNumOperands() - 1).getValueType() == MVT::Other; + if (HasChainOp) { + // Avoid creating cycles in the DAG. We must ensure that none of the other + // operands depend on True through it's Chain. + SmallVector LoopWorklist; + SmallPtrSet Visited; + LoopWorklist.push_back(False.getNode()); + LoopWorklist.push_back(Mask.getNode()); + LoopWorklist.push_back(VL.getNode()); + if (SDNode *Glued = N->getGluedNode()) + LoopWorklist.push_back(Glued); + if (SDNode::hasPredecessorHelper(True.getNode(), Visited, LoopWorklist)) + return false; + } + // Need True has same VL with N. unsigned TrueVLIndex = True.getNumOperands() - HasChainOp - 2; SDValue TrueVL = True.getOperand(TrueVLIndex);