Unify CALLSEQ_{START,END}. They take 4 parameters: the chain, two stack
adjustment fields, and an optional flag. If there is a "dynamic_stackalloc" in
the code, make sure that it's bracketed by CALLSEQ_START and CALLSEQ_END. If
not, then there is the potential for the stack to be changed while the stack's
being used by another instruction (like a call).

This can only result in tears...


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44037 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp b/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
index 4e096d8..5264141 100644
--- a/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
+++ b/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
@@ -1142,12 +1142,20 @@
     // The only option for this is to custom lower it.
     Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
     assert(Tmp3.Val && "Target didn't custom lower this node!");
-    assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
+
+    // The number of incoming and outgoing values should match; unless the final
+    // outgoing value is a flag.
+    assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
+            (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
+             Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
+               MVT::Flag)) &&
            "Lowering call/formal_arguments produced unexpected # results!");
     
     // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
     // remember that we legalized all of them, so it doesn't get relegalized.
     for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
+      if (Tmp3.Val->getValueType(i) == MVT::Flag)
+        continue;
       Tmp1 = LegalizeOp(Tmp3.getValue(i));
       if (Op.ResNo == i)
         Tmp2 = Tmp1;
@@ -1472,6 +1480,12 @@
       assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
              " not tell us which reg is the stack pointer!");
       SDOperand Chain = Tmp1.getOperand(0);
+
+      // Chain the dynamic stack allocation so that it doesn't modify the stack
+      // pointer when other instructions are using the stack.
+      Chain = DAG.getCALLSEQ_START(Chain,
+                                   DAG.getConstant(0, TLI.getPointerTy()));
+
       SDOperand Size  = Tmp2.getOperand(1);
       SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
       Chain = SP.getValue(1);
@@ -1482,7 +1496,14 @@
         SP = DAG.getNode(ISD::AND, VT, SP,
                          DAG.getConstant(-(uint64_t)Align, VT));
       Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size);       // Value
-      Tmp2 = DAG.getCopyToReg(Chain, SPReg, Tmp1);      // Output chain
+      Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1);     // Output chain
+
+      Tmp2 =
+        DAG.getCALLSEQ_END(Chain,
+                           DAG.getConstant(0, TLI.getPointerTy()),
+                           DAG.getConstant(0, TLI.getPointerTy()),
+                           SDOperand());
+
       Tmp1 = LegalizeOp(Tmp1);
       Tmp2 = LegalizeOp(Tmp2);
       break;