LegalizeTypes can sometimes have deleted nodes
[oota-llvm.git] / lib / CodeGen / SelectionDAG / LegalizeTypes.cpp
index cfaf0a9c9342b99daff7c49b4b7093b2157397a0..34f99da49fa7f074542d77d93da6af391b45ad57 100644 (file)
@@ -68,25 +68,29 @@ void DAGTypeLegalizer::run() {
     unsigned NumResults = N->getNumValues();
     do {
       MVT::ValueType ResultVT = N->getValueType(i);
-      LegalizeAction Action = getTypeAction(ResultVT);
-      if (Action == Promote) {
+      switch (getTypeAction(ResultVT)) {
+      default:
+        assert(false && "Unknown action!");
+      case Legal:
+        break;
+      case Promote:
         PromoteResult(N, i);
         goto NodeDone;
-      } else if (Action == Expand) {
-        // Expand can mean 1) split integer in half 2) scalarize single-element
-        // vector 3) split vector in half.
-        if (!MVT::isVector(ResultVT))
-          ExpandResult(N, i);
-        else if (MVT::getVectorNumElements(ResultVT) == 1)
-          ScalarizeResult(N, i);     // Scalarize the single-element vector.
-        else
-          SplitResult(N, i);         // Split the vector in half.
+      case Expand:
+        ExpandResult(N, i);
+        goto NodeDone;
+      case FloatToInt:
+        FloatToIntResult(N, i);
+        goto NodeDone;
+      case Scalarize:
+        ScalarizeResult(N, i);
+        goto NodeDone;
+      case Split:
+        SplitResult(N, i);
         goto NodeDone;
-      } else {
-        assert(Action == Legal && "Unknown action!");
       }
     } while (++i < NumResults);
-    
+
     // Scan the operand list for the node, handling any nodes with operands that
     // are illegal.
     {
@@ -94,25 +98,28 @@ void DAGTypeLegalizer::run() {
     bool NeedsRevisit = false;
     for (i = 0; i != NumOperands; ++i) {
       MVT::ValueType OpVT = N->getOperand(i).getValueType();
-      LegalizeAction Action = getTypeAction(OpVT);
-      if (Action == Promote) {
+      switch (getTypeAction(OpVT)) {
+      default:
+        assert(false && "Unknown action!");
+      case Legal:
+        continue;
+      case Promote:
         NeedsRevisit = PromoteOperand(N, i);
         break;
-      } else if (Action == Expand) {
-        // Expand can mean 1) split integer in half 2) scalarize single-element
-        // vector 3) split vector in half.
-        if (!MVT::isVector(OpVT)) {
-          NeedsRevisit = ExpandOperand(N, i);
-        } else if (MVT::getVectorNumElements(OpVT) == 1) {
-          // Scalarize the single-element vector.
-          NeedsRevisit = ScalarizeOperand(N, i);
-        } else {
-          NeedsRevisit = SplitOperand(N, i); // Split the vector in half.
-        }
+      case Expand:
+        NeedsRevisit = ExpandOperand(N, i);
+        break;
+      case FloatToInt:
+        NeedsRevisit = FloatToIntOperand(N, i);
+        break;
+      case Scalarize:
+        NeedsRevisit = ScalarizeOperand(N, i);
+        break;
+      case Split:
+        NeedsRevisit = SplitOperand(N, i);
         break;
-      } else {
-        assert(Action == Legal && "Unknown action!");
       }
+      break;
     }
 
     // If the node needs revisiting, don't add all users to the worklist etc.
@@ -130,7 +137,7 @@ NodeDone:
     
     for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
          UI != E; ++UI) {
-      SDNode *User = *UI;
+      SDNode *User = UI->getUser();
       int NodeID = User->getNodeId();
       assert(NodeID != ReadyToProcess && NodeID != Processed &&
              "Invalid node id for user of unprocessed node!");
@@ -260,6 +267,51 @@ void DAGTypeLegalizer::AnalyzeNewNode(SDNode *&N) {
     Worklist.push_back(N);
 }
 
+void DAGTypeLegalizer::SanityCheck(SDNode *N) {
+  for (SmallVector<SDNode*, 128>::iterator I = Worklist.begin(),
+       E = Worklist.end(); I != E; ++I)
+    assert(*I != N);
+
+  for (DenseMap<SDOperandImpl, SDOperand>::iterator I = ReplacedNodes.begin(),
+       E = ReplacedNodes.end(); I != E; ++I) {
+    assert(I->first.Val != N);
+    assert(I->second.Val != N);
+  }
+
+  for (DenseMap<SDOperandImpl, SDOperand>::iterator I = PromotedNodes.begin(),
+       E = PromotedNodes.end(); I != E; ++I) {
+    assert(I->first.Val != N);
+    assert(I->second.Val != N);
+  }
+
+  for (DenseMap<SDOperandImpl, SDOperand>::iterator
+       I = FloatToIntedNodes.begin(),
+       E = FloatToIntedNodes.end(); I != E; ++I) {
+    assert(I->first.Val != N);
+    assert(I->second.Val != N);
+  }
+
+  for (DenseMap<SDOperandImpl, SDOperand>::iterator I = ScalarizedNodes.begin(),
+       E = ScalarizedNodes.end(); I != E; ++I) {
+    assert(I->first.Val != N);
+    assert(I->second.Val != N);
+  }
+
+  for (DenseMap<SDOperandImpl, std::pair<SDOperand, SDOperand> >::iterator
+       I = ExpandedNodes.begin(), E = ExpandedNodes.end(); I != E; ++I) {
+    assert(I->first.Val != N);
+    assert(I->second.first.Val != N);
+    assert(I->second.second.Val != N);
+  }
+
+  for (DenseMap<SDOperandImpl, std::pair<SDOperand, SDOperand> >::iterator
+       I = SplitNodes.begin(), E = SplitNodes.end(); I != E; ++I) {
+    assert(I->first.Val != N);
+    assert(I->second.first.Val != N);
+    assert(I->second.second.Val != N);
+  }
+}
+
 namespace {
   /// NodeUpdateListener - This class is a DAGUpdateListener that listens for
   /// updates to nodes and recomputes their ready state.
@@ -274,6 +326,9 @@ namespace {
       assert(N->getNodeId() != DAGTypeLegalizer::Processed &&
              N->getNodeId() != DAGTypeLegalizer::ReadyToProcess &&
              "RAUW deleted processed node!");
+#ifndef NDEBUG
+      DTL.SanityCheck(N);
+#endif
     }
 
     virtual void NodeUpdated(SDNode *N) {
@@ -337,7 +392,7 @@ void DAGTypeLegalizer::ReplaceNodeWith(SDNode *From, SDNode *To) {
 /// RemapNode - If the specified value was already legalized to another value,
 /// replace it by that value.
 void DAGTypeLegalizer::RemapNode(SDOperand &N) {
-  DenseMap<SDOperand, SDOperand>::iterator I = ReplacedNodes.find(N);
+  DenseMap<SDOperandImpl, SDOperand>::iterator I = ReplacedNodes.find(N);
   if (I != ReplacedNodes.end()) {
     // Use path compression to speed up future lookups if values get multiply
     // replaced with other values.
@@ -354,6 +409,14 @@ void DAGTypeLegalizer::SetPromotedOp(SDOperand Op, SDOperand Result) {
   OpEntry = Result;
 }
 
+void DAGTypeLegalizer::SetIntegerOp(SDOperand Op, SDOperand Result) {
+  AnalyzeNewNode(Result.Val);
+
+  SDOperand &OpEntry = FloatToIntedNodes[Op];
+  assert(OpEntry.Val == 0 && "Node is already converted to integer!");
+  OpEntry = Result;
+}
+
 void DAGTypeLegalizer::SetScalarizedOp(SDOperand Op, SDOperand Result) {
   AnalyzeNewNode(Result.Val);
 
@@ -362,7 +425,6 @@ void DAGTypeLegalizer::SetScalarizedOp(SDOperand Op, SDOperand Result) {
   OpEntry = Result;
 }
 
-
 void DAGTypeLegalizer::GetExpandedOp(SDOperand Op, SDOperand &Lo, 
                                      SDOperand &Hi) {
   std::pair<SDOperand, SDOperand> &Entry = ExpandedNodes[Op];
@@ -407,6 +469,13 @@ void DAGTypeLegalizer::SetSplitOp(SDOperand Op, SDOperand Lo, SDOperand Hi) {
 }
 
 
+/// BitConvertToInteger - Convert to an integer of the same size.
+SDOperand DAGTypeLegalizer::BitConvertToInteger(SDOperand Op) {
+  return DAG.getNode(ISD::BIT_CONVERT,
+                     MVT::getIntegerType(MVT::getSizeInBits(Op.getValueType())),
+                     Op);
+}
+
 SDOperand DAGTypeLegalizer::CreateStackStoreLoad(SDOperand Op, 
                                                  MVT::ValueType DestVT) {
   // Create the stack frame object.
@@ -418,64 +487,42 @@ SDOperand DAGTypeLegalizer::CreateStackStoreLoad(SDOperand Op,
   return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
 }
 
-/// HandleMemIntrinsic - This handles memcpy/memset/memmove with invalid
-/// operands.  This promotes or expands the operands as required.
-SDOperand DAGTypeLegalizer::HandleMemIntrinsic(SDNode *N) {
-  // The chain and pointer [operands #0 and #1] are always valid types.
-  SDOperand Chain = N->getOperand(0);
-  SDOperand Ptr   = N->getOperand(1);
-  SDOperand Op2   = N->getOperand(2);
-  
-  // Op #2 is either a value (memset) or a pointer.  Promote it if required.
-  switch (getTypeAction(Op2.getValueType())) {
-  default: assert(0 && "Unknown action for pointer/value operand");
-  case Legal: break;
-  case Promote: Op2 = GetPromotedOp(Op2); break;
-  }
-  
-  // The length could have any action required.
-  SDOperand Length = N->getOperand(3);
-  switch (getTypeAction(Length.getValueType())) {
-  default: assert(0 && "Unknown action for memop operand");
-  case Legal: break;
-  case Promote: Length = GetPromotedZExtOp(Length); break;
-  case Expand:
-    SDOperand Dummy;  // discard the high part.
-    GetExpandedOp(Length, Length, Dummy);
-    break;
-  }
-  
-  SDOperand Align = N->getOperand(4);
-  switch (getTypeAction(Align.getValueType())) {
-  default: assert(0 && "Unknown action for memop operand");
-  case Legal: break;
-  case Promote: Align = GetPromotedZExtOp(Align); break;
-  }
-  
-  SDOperand AlwaysInline = N->getOperand(5);
-  switch (getTypeAction(AlwaysInline.getValueType())) {
-  default: assert(0 && "Unknown action for memop operand");
-  case Legal: break;
-  case Promote: AlwaysInline = GetPromotedZExtOp(AlwaysInline); break;
-  }
-  
-  SDOperand Ops[] = { Chain, Ptr, Op2, Length, Align, AlwaysInline };
-  return DAG.UpdateNodeOperands(SDOperand(N, 0), Ops, 6);
+/// JoinIntegers - Build an integer with low bits Lo and high bits Hi.
+SDOperand DAGTypeLegalizer::JoinIntegers(SDOperand Lo, SDOperand Hi) {
+  MVT::ValueType LVT = Lo.getValueType();
+  MVT::ValueType HVT = Hi.getValueType();
+  MVT::ValueType NVT = MVT::getIntegerType(MVT::getSizeInBits(LVT) +
+                                           MVT::getSizeInBits(HVT));
+
+  Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Lo);
+  Hi = DAG.getNode(ISD::ANY_EXTEND, NVT, Hi);
+  Hi = DAG.getNode(ISD::SHL, NVT, Hi, DAG.getConstant(MVT::getSizeInBits(LVT),
+                                                      TLI.getShiftAmountTy()));
+  return DAG.getNode(ISD::OR, NVT, Lo, Hi);
 }
 
-/// SplitOp - Return the lower and upper halves of Op's bits in a value type
-/// half the size of Op's.
-void DAGTypeLegalizer::SplitOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi) {
-  unsigned NVTBits = MVT::getSizeInBits(Op.getValueType())/2;
-  assert(MVT::getSizeInBits(Op.getValueType()) == 2*NVTBits &&
-         "Cannot split odd sized integer type");
-  MVT::ValueType NVT = MVT::getIntegerType(NVTBits);
-  Lo = DAG.getNode(ISD::TRUNCATE, NVT, Op);
+/// SplitInteger - Return the lower LoVT bits of Op in Lo and the upper HiVT
+/// bits in Hi.
+void DAGTypeLegalizer::SplitInteger(SDOperand Op,
+                                    MVT::ValueType LoVT, MVT::ValueType HiVT,
+                                    SDOperand &Lo, SDOperand &Hi) {
+  assert(MVT::getSizeInBits(LoVT) + MVT::getSizeInBits(HiVT) ==
+         MVT::getSizeInBits(Op.getValueType()) && "Invalid integer splitting!");
+  Lo = DAG.getNode(ISD::TRUNCATE, LoVT, Op);
   Hi = DAG.getNode(ISD::SRL, Op.getValueType(), Op,
-                   DAG.getConstant(NVTBits, TLI.getShiftAmountTy()));
-  Hi = DAG.getNode(ISD::TRUNCATE, NVT, Hi);
+                   DAG.getConstant(MVT::getSizeInBits(LoVT),
+                                   TLI.getShiftAmountTy()));
+  Hi = DAG.getNode(ISD::TRUNCATE, HiVT, Hi);
 }
 
+/// SplitInteger - Return the lower and upper halves of Op's bits in a value type
+/// half the size of Op's.
+void DAGTypeLegalizer::SplitInteger(SDOperand Op,
+                                    SDOperand &Lo, SDOperand &Hi) {
+  MVT::ValueType HalfVT =
+    MVT::getIntegerType(MVT::getSizeInBits(Op.getValueType())/2);
+  SplitInteger(Op, HalfVT, HalfVT, Lo, Hi);
+}
 
 //===----------------------------------------------------------------------===//
 //  Entry Point