diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp --- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -1800,9 +1800,20 @@ // Iterate through token factors. The TFs grows when new token factors are // encountered. - for (unsigned i = 0; i < TFs.size(); ++i) { - SDNode *TF = TFs[i]; + bool CompletelyExpanded = true; + for (unsigned i = 0; i < TFs.size() ; ++i) { + // Limit number of nodes to inline, to avoid quadratic compile times. + // Expand outstanding tokenfactors, otherwise we might drop Ops with + // users in unexpanded tokenfactors. + if (Ops.size() > 2048) { + CompletelyExpanded = false; + for (; i < TFs.size(); i++) + for (const SDValue &Op : TFs[i]->op_values()) + Ops.push_back(Op); + break; + } + SDNode *TF = TFs[i]; // Check each of the operands. for (const SDValue &Op : TF->op_values()) { switch (Op.getOpcode()) { @@ -1923,7 +1934,7 @@ // The entry token is the only possible outcome. Result = DAG.getEntryNode(); } else { - if (DidPruneOps) { + if (DidPruneOps && CompletelyExpanded) { SmallVector PrunedOps; // for (const SDValue &Op : Ops) {