More constification of things. More comments added. No functionality
[oota-llvm.git] / lib / CodeGen / SelectionDAG / LegalizeDAG.cpp
index ce5716ba906be5a12cb90e5009b184c905239459..9355e4871fe4dc38b2e25f242f3f8121c5f95e0a 100644 (file)
@@ -1133,6 +1133,26 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
     }
     break;
 
+  case ISD::MEMBARRIER: {
+    assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
+    switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
+    default: assert(0 && "This action is not supported yet!");
+    case TargetLowering::Legal: {
+      SDOperand Ops[6];
+      Ops[0] = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
+      for (int x = 1; x < 6; ++x)
+        Ops[x] = PromoteOp(Node->getOperand(x));
+      Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
+      break;
+    }
+    case TargetLowering::Expand:
+      //There is no libgcc call for this op
+      Result = Node->getOperand(0);  // Noop
+    break;
+    }
+    break;
+  }
+
   case ISD::Constant: {
     ConstantSDNode *CN = cast<ConstantSDNode>(Node);
     unsigned opAction =
@@ -1155,24 +1175,10 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
     // leave these constants as ConstantFP nodes for the target to deal with.
     ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
 
-    // Check to see if this FP immediate is already legal.
-    bool isLegal = false;
-    for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
-           E = TLI.legal_fpimm_end(); I != E; ++I)
-      if (CFP->isExactlyValue(*I)) {
-        isLegal = true;
-        break;
-      }
-
-    // If this is a legal constant, turn it into a TargetConstantFP node.
-    if (isLegal) {
-      Result = DAG.getTargetConstantFP(CFP->getValueAPF(), 
-                                       CFP->getValueType(0));
-      break;
-    }
-
     switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
     default: assert(0 && "This action is not supported yet!");
+    case TargetLowering::Legal:
+      break;
     case TargetLowering::Custom:
       Tmp3 = TLI.LowerOperation(Result, DAG);
       if (Tmp3.Val) {
@@ -1180,9 +1186,22 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
         break;
       }
       // FALLTHROUGH
-    case TargetLowering::Expand:
+    case TargetLowering::Expand: {
+      // Check to see if this FP immediate is already legal.
+      bool isLegal = false;
+      for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
+             E = TLI.legal_fpimm_end(); I != E; ++I) {
+        if (CFP->isExactlyValue(*I)) {
+          isLegal = true;
+          break;
+        }
+      }
+      // If this is a legal constant, turn it into a TargetConstantFP node.
+      if (isLegal)
+        break;
       Result = ExpandConstantFP(CFP, true, DAG, TLI);
     }
+    }
     break;
   }
   case ISD::TokenFactor:
@@ -1263,8 +1282,16 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
     break;
   case ISD::INSERT_VECTOR_ELT:
     Tmp1 = LegalizeOp(Node->getOperand(0));  // InVec
-    Tmp2 = LegalizeOp(Node->getOperand(1));  // InVal
     Tmp3 = LegalizeOp(Node->getOperand(2));  // InEltNo
+
+    // The type of the value to insert may not be legal, even though the vector
+    // type is legal.  Legalize/Promote accordingly.  We do not handle Expand
+    // here.
+    switch (getTypeAction(Node->getOperand(1).getValueType())) {
+    default: assert(0 && "Cannot expand insert element operand");
+    case Legal:   Tmp2 = LegalizeOp(Node->getOperand(1)); break;
+    case Promote: Tmp2 = PromoteOp(Node->getOperand(1));  break;
+    }
     Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
     
     switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
@@ -1283,30 +1310,35 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
       // If the insert index is a constant, codegen this as a scalar_to_vector,
       // then a shuffle that inserts it into the right position in the vector.
       if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
-        SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, 
-                                      Tmp1.getValueType(), Tmp2);
-        
-        unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
-        MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
-        MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
-        
-        // We generate a shuffle of InVec and ScVec, so the shuffle mask should
-        // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
-        // the RHS.
-        SmallVector<SDOperand, 8> ShufOps;
-        for (unsigned i = 0; i != NumElts; ++i) {
-          if (i != InsertPos->getValue())
-            ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
-          else
-            ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
+        // SCALAR_TO_VECTOR requires that the type of the value being inserted
+        // match the element type of the vector being created.
+        if (Tmp2.getValueType() == 
+            MVT::getVectorElementType(Op.getValueType())) {
+          SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, 
+                                        Tmp1.getValueType(), Tmp2);
+          
+          unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
+          MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
+          MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
+          
+          // We generate a shuffle of InVec and ScVec, so the shuffle mask
+          // should be 0,1,2,3,4,5... with the appropriate element replaced with
+          // elt 0 of the RHS.
+          SmallVector<SDOperand, 8> ShufOps;
+          for (unsigned i = 0; i != NumElts; ++i) {
+            if (i != InsertPos->getValue())
+              ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
+            else
+              ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
+          }
+          SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
+                                           &ShufOps[0], ShufOps.size());
+          
+          Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
+                               Tmp1, ScVec, ShufMask);
+          Result = LegalizeOp(Result);
+          break;
         }
-        SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
-                                         &ShufOps[0], ShufOps.size());
-        
-        Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
-                             Tmp1, ScVec, ShufMask);
-        Result = LegalizeOp(Result);
-        break;
       }
       
       // If the target doesn't support this, we have to spill the input vector
@@ -1316,13 +1348,12 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
       // permute it into place, if the idx is a constant and if the idx is
       // supported by the target.
       MVT::ValueType VT    = Tmp1.getValueType();
-      MVT::ValueType EltVT = Tmp2.getValueType();
+      MVT::ValueType EltVT = MVT::getVectorElementType(VT);
       MVT::ValueType IdxVT = Tmp3.getValueType();
       MVT::ValueType PtrVT = TLI.getPointerTy();
       SDOperand StackPtr = DAG.CreateStackTemporary(VT);
 
-      FrameIndexSDNode *StackPtrFI = dyn_cast<FrameIndexSDNode>(StackPtr.Val);
-      assert(StackPtrFI);
+      FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr.Val);
       int SPFI = StackPtrFI->getIndex();
 
       // Store the vector.
@@ -1338,8 +1369,8 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
       Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
       SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
       // Store the scalar value.
-      Ch = DAG.getStore(Ch, Tmp2, StackPtr2,
-                        PseudoSourceValue::getFixedStack(), SPFI);
+      Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
+                             PseudoSourceValue::getFixedStack(), SPFI, EltVT);
       // Load the updated vector.
       Result = DAG.getLoad(VT, Ch, StackPtr,
                            PseudoSourceValue::getFixedStack(), SPFI);
@@ -2117,7 +2148,7 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
           ExpandOp(Tmp2, Lo, Hi);
 
           // Big endian systems want the hi reg first.
-          if (!TLI.isLittleEndian())
+          if (TLI.isBigEndian())
             std::swap(Lo, Hi);
           
           if (Hi.Val)
@@ -2256,7 +2287,7 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
             uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
             SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
             SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
-            if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
+            if (TLI.isBigEndian()) std::swap(Lo, Hi);
 
             Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
                               SVOffset, isVolatile, Alignment);
@@ -2333,14 +2364,14 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
           MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
           if (TLI.isTypeLegal(TVT)) {
             // Turn this into a normal store of the vector type.
-            Tmp3 = LegalizeOp(Node->getOperand(1));
+            Tmp3 = LegalizeOp(ST->getValue());
             Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
                                   SVOffset, isVolatile, Alignment);
             Result = LegalizeOp(Result);
             break;
           } else if (NumElems == 1) {
             // Turn this into a normal store of the scalar type.
-            Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
+            Tmp3 = ScalarizeVectorOp(ST->getValue());
             Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
                                   SVOffset, isVolatile, Alignment);
             // The scalarized value type may not be legal, e.g. it might require
@@ -2348,15 +2379,15 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
             Result = LegalizeOp(Result);
             break;
           } else {
-            SplitVectorOp(Node->getOperand(1), Lo, Hi);
+            SplitVectorOp(ST->getValue(), Lo, Hi);
             IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) * 
                             MVT::getSizeInBits(EVT)/8;
           }
         } else {
-          ExpandOp(Node->getOperand(1), Lo, Hi);
+          ExpandOp(ST->getValue(), Lo, Hi);
           IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
 
-          if (!TLI.isLittleEndian())
+          if (TLI.isBigEndian())
             std::swap(Lo, Hi);
         }
 
@@ -2851,7 +2882,8 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
       }
 
       std::pair<SDOperand,SDOperand> CallResult =
-        TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
+        TLI.LowerCallTo(Tmp1, Type::VoidTy,
+                        false, false, false, CallingConv::C, false,
                         DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
       Result = CallResult.second;
       break;
@@ -3840,6 +3872,13 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
     case Expand: assert(0 && "Shouldn't need to expand other operators here!");
     case Legal:
       Tmp1 = LegalizeOp(Node->getOperand(0));
+      if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
+          TargetLowering::Custom) {
+        Tmp2 = TLI.LowerOperation(Result, DAG);
+        if (Tmp2.Val) {
+          Tmp1 = Tmp2;
+        }
+      }
       Result = DAG.UpdateNodeOperands(Result, Tmp1);
       break;
     case Promote:
@@ -3952,7 +3991,8 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
       Tmp1 = LegalizeOp(Node->getOperand(0));
       TargetLowering::ArgListTy Args;
       std::pair<SDOperand,SDOperand> CallResult =
-        TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
+        TLI.LowerCallTo(Tmp1, Type::VoidTy,
+                        false, false, false, CallingConv::C, false,
                         DAG.getExternalSymbol("abort", TLI.getPointerTy()),
                         Args, DAG);
       Result = CallResult.second;
@@ -4759,8 +4799,7 @@ SDOperand SelectionDAGLegalize::EmitStackConvert(SDOperand SrcOp,
   // Create the stack frame object.
   SDOperand FIPtr = DAG.CreateStackTemporary(SlotVT);
 
-  FrameIndexSDNode *StackPtrFI = dyn_cast<FrameIndexSDNode>(FIPtr);
-  assert(StackPtrFI);
+  FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
   int SPFI = StackPtrFI->getIndex();
 
   unsigned SrcSize = MVT::getSizeInBits(SrcOp.getValueType());
@@ -4794,8 +4833,7 @@ SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
   // then load the whole vector back out.
   SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
 
-  FrameIndexSDNode *StackPtrFI = dyn_cast<FrameIndexSDNode>(StackPtr);
-  assert(StackPtrFI);
+  FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
   int SPFI = StackPtrFI->getIndex();
 
   SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
@@ -5144,6 +5182,7 @@ SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
     const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
     Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy; 
     Entry.isSExt = isSigned;
+    Entry.isZExt = !isSigned;
     Args.push_back(Entry);
   }
   SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
@@ -5151,8 +5190,8 @@ SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
   // Splice the libcall in wherever FindInputOutputChains tells us to.
   const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
   std::pair<SDOperand,SDOperand> CallInfo =
-    TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
-                    Callee, Args, DAG);
+    TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, CallingConv::C,
+                    false, Callee, Args, DAG);
 
   // Legalize the call sequence, starting with the chain.  This will advance
   // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
@@ -5766,7 +5805,7 @@ void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
     // Remember that we legalized the chain.
     Hi = LegalizeOp(Hi);
     AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
-    if (!TLI.isLittleEndian())
+    if (TLI.isBigEndian())
       std::swap(Lo, Hi);
     break;
   }
@@ -5809,7 +5848,7 @@ void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
 
       // Remember that we legalized the chain.
       AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
-      if (!TLI.isLittleEndian())
+      if (TLI.isBigEndian())
         std::swap(Lo, Hi);
     } else {
       MVT::ValueType EVT = LD->getMemoryVT();
@@ -6773,8 +6812,7 @@ void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
       // Lower to a store/load so that it can be split.
       // FIXME: this could be improved probably.
       SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
-      FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr.Val);
-      assert(FI && "Expecting CreateStackTemporary to return a frame index.\n");
+      FrameIndexSDNode *FI = cast<FrameIndexSDNode>(Ptr.Val);
 
       SDOperand St = DAG.getStore(DAG.getEntryNode(),
                                   InOp, Ptr,