Change the interface to the type legalization method
ReplaceNodeResults: rather than returning a node which
must have the same number of results as the original
node (which means mucking around with MERGE_VALUES,
and which is also easy to get wrong since SelectionDAG
folding may mean you don't get the node you expect),
return the results in a vector.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@60348 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp b/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
index 3ff6645..5b230fb 100644
--- a/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
+++ b/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
@@ -6083,8 +6083,10 @@
   // the target lowering hooks to expand it.  Just keep the low part of the
   // expanded operation, we know that we're truncating anyway.
   if (getTypeAction(NewOutTy) == Expand) {
-    Operation = SDValue(TLI.ReplaceNodeResults(Operation.getNode(), DAG), 0);
-    assert(Operation.getNode() && "Didn't return anything");
+    SmallVector<SDValue, 2> Results;
+    TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
+    assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
+    Operation = Results[0];
   }
 
   // Truncate the result of the extended FP_TO_*INT operation to the desired
diff --git a/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp b/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp
index e803bba..9510987 100644
--- a/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp
+++ b/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp
@@ -727,16 +727,8 @@
   Lo = Hi = SDValue();
 
   // See if the target wants to custom expand this node.
-  if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
-      TargetLowering::Custom) {
-    // If the target wants to, allow it to lower this itself.
-    if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
-      // Everything that once used N now uses P.  We are guaranteed that the
-      // result value types of N and the result value types of P match.
-      ReplaceNodeWith(N, P);
-      return;
-    }
-  }
+  if (CustomLowerResults(N, ResNo))
+    return;
 
   switch (N->getOpcode()) {
   default:
diff --git a/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp b/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp
index 6e78107..0c17531 100644
--- a/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp
+++ b/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp
@@ -34,16 +34,8 @@
   SDValue Result = SDValue();
 
   // See if the target wants to custom expand this node.
-  if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
-      TargetLowering::Custom) {
-    // If the target wants to, allow it to lower this itself.
-    if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
-      // Everything that once used N now uses P.  We are guaranteed that the
-      // result value types of N and the result value types of P match.
-      ReplaceNodeWith(N, P);
-      return;
-    }
-  }
+  if (CustomLowerResults(N, ResNo))
+    return;
 
   switch (N->getOpcode()) {
   default:
@@ -949,16 +941,8 @@
   Lo = Hi = SDValue();
 
   // See if the target wants to custom expand this node.
-  if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
-      TargetLowering::Custom) {
-    // If the target wants to, allow it to lower this itself.
-    if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
-      // Everything that once used N now uses P.  We are guaranteed that the
-      // result value types of N and the result value types of P match.
-      ReplaceNodeWith(N, P);
-      return;
-    }
-  }
+  if (CustomLowerResults(N, ResNo))
+    return;
 
   switch (N->getOpcode()) {
   default:
diff --git a/lib/CodeGen/SelectionDAG/LegalizeTypes.cpp b/lib/CodeGen/SelectionDAG/LegalizeTypes.cpp
index 9502b27..9a22f09 100644
--- a/lib/CodeGen/SelectionDAG/LegalizeTypes.cpp
+++ b/lib/CodeGen/SelectionDAG/LegalizeTypes.cpp
@@ -376,45 +376,6 @@
   ReplacedValues[From] = To;
 }
 
-/// ReplaceNodeWith - Replace uses of the 'from' node's results with the 'to'
-/// node's results.  The from and to node must define identical result types.
-void DAGTypeLegalizer::ReplaceNodeWith(SDNode *From, SDNode *To) {
-  if (From == To) return;
-
-  // If expansion produced new nodes, make sure they are properly marked.
-  ExpungeNode(From);
-
-  To = AnalyzeNewNode(To); // Expunges To.
-  // If To morphed into an already processed node, its values may need
-  // remapping.  This is done below.
-
-  assert(From->getNumValues() == To->getNumValues() &&
-         "Node results don't match");
-
-  // Anything that used the old node should now use the new one.  Note that this
-  // can potentially cause recursive merging.
-  NodeUpdateListener NUL(*this);
-  for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
-    SDValue FromVal(From, i);
-    SDValue ToVal(To, i);
-
-    // AnalyzeNewNode may have morphed a new node into a processed node.  Remap
-    // values now.
-    if (To->getNodeId() == Processed)
-      RemapValue(ToVal);
-
-    assert(FromVal.getValueType() == ToVal.getValueType() &&
-           "Node results don't match!");
-
-    // Make anything that used the old value use the new value.
-    DAG.ReplaceAllUsesOfValueWith(FromVal, ToVal, &NUL);
-
-    // The old node may still be present in a map like ExpandedIntegers or
-    // PromotedIntegers.  Inform maps about the replacement.
-    ReplacedValues[FromVal] = ToVal;
-  }
-}
-
 /// RemapValue - If the specified value was already legalized to another value,
 /// replace it by that value.
 void DAGTypeLegalizer::RemapValue(SDValue &N) {
@@ -621,6 +582,28 @@
   return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
 }
 
+/// CustomLowerResults - Replace the node's results with custom code provided
+/// by the target and return "true", or do nothing and return "false".
+bool DAGTypeLegalizer::CustomLowerResults(SDNode *N, unsigned ResNo) {
+  // See if the target wants to custom lower this node.
+  if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) !=
+      TargetLowering::Custom)
+    return false;
+
+  SmallVector<SDValue, 8> Results;
+  TLI.ReplaceNodeResults(N, Results, DAG);
+  if (Results.empty())
+    // The target didn't want to custom lower it after all.
+    return false;
+
+  // Make everything that once used N's values now use those in Results instead.
+  assert(Results.size() == N->getNumValues() &&
+         "Custom lowering returned the wrong number of results!");
+  for (unsigned i = 0, e = Results.size(); i != e; ++i)
+    ReplaceValueWith(SDValue(N, i), Results[i]);
+  return true;
+}
+
 /// JoinIntegers - Build an integer with low bits Lo and high bits Hi.
 SDValue DAGTypeLegalizer::JoinIntegers(SDValue Lo, SDValue Hi) {
   MVT LVT = Lo.getValueType();
diff --git a/lib/CodeGen/SelectionDAG/LegalizeTypes.h b/lib/CodeGen/SelectionDAG/LegalizeTypes.h
index 6d41cc5..6a003a0 100644
--- a/lib/CodeGen/SelectionDAG/LegalizeTypes.h
+++ b/lib/CodeGen/SelectionDAG/LegalizeTypes.h
@@ -185,12 +185,13 @@
   void AnalyzeNewValue(SDValue &Val);
 
   void ReplaceValueWith(SDValue From, SDValue To);
-  void ReplaceNodeWith(SDNode *From, SDNode *To);
 
   void RemapValue(SDValue &N);
   void ExpungeNode(SDNode *N);
 
   // Common routines.
+  bool CustomLowerResults(SDNode *N, unsigned ResNo);
+
   SDValue CreateStackStoreLoad(SDValue Op, MVT DestVT);
   SDValue MakeLibCall(RTLIB::Libcall LC, MVT RetVT,
                       const SDValue *Ops, unsigned NumOps, bool isSigned);