Remove some patterns for matching vector_shuffle instructions since vector_shuffles...
[oota-llvm.git] / lib / Target / X86 / X86ISelDAGToDAG.cpp
index 3df6039eaf6c87d6eeb013f1c76971135edb70a3..830cb65878e2d00b5ed9f6ce48d42016e9550747 100644 (file)
@@ -725,6 +725,19 @@ bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) {
   return false;
 }
 
+// Insert a node into the DAG at least before the Pos node's position. This
+// will reposition the node as needed, and will assign it a node ID that is <=
+// the Pos node's ID. Note that this does *not* preserve the uniqueness of node
+// IDs! The selection DAG must no longer depend on their uniqueness when this
+// is used.
+static void InsertDAGNode(SelectionDAG &DAG, SDValue Pos, SDValue N) {
+  if (N.getNode()->getNodeId() == -1 ||
+      N.getNode()->getNodeId() > Pos.getNode()->getNodeId()) {
+    DAG.RepositionNode(Pos.getNode(), N.getNode());
+    N.getNode()->setNodeId(Pos.getNode()->getNodeId());
+  }
+}
+
 // Transform "(X >> (8-C1)) & C2" to "(X >> 8) & 0xff)" if safe. This
 // allows us to convert the shift and and into an h-register extract and
 // a scaled index. Returns false if the simplification is performed.
@@ -751,43 +764,66 @@ static bool FoldMaskAndShiftToExtract(SelectionDAG &DAG, SDValue N,
   SDValue ShlCount = DAG.getConstant(ScaleLog, MVT::i8);
   SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, And, ShlCount);
 
-  // Insert the new nodes into the topological ordering.
-  if (Eight.getNode()->getNodeId() == -1 ||
-      Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
-    DAG.RepositionNode(X.getNode(), Eight.getNode());
-    Eight.getNode()->setNodeId(X.getNode()->getNodeId());
-  }
-  if (NewMask.getNode()->getNodeId() == -1 ||
-      NewMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
-    DAG.RepositionNode(X.getNode(), NewMask.getNode());
-    NewMask.getNode()->setNodeId(X.getNode()->getNodeId());
-  }
-  if (Srl.getNode()->getNodeId() == -1 ||
-      Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
-    DAG.RepositionNode(Shift.getNode(), Srl.getNode());
-    Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
-  }
-  if (And.getNode()->getNodeId() == -1 ||
-      And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
-    DAG.RepositionNode(N.getNode(), And.getNode());
-    And.getNode()->setNodeId(N.getNode()->getNodeId());
-  }
-  if (ShlCount.getNode()->getNodeId() == -1 ||
-      ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
-    DAG.RepositionNode(X.getNode(), ShlCount.getNode());
-    ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
-  }
-  if (Shl.getNode()->getNodeId() == -1 ||
-      Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
-    DAG.RepositionNode(N.getNode(), Shl.getNode());
-    Shl.getNode()->setNodeId(N.getNode()->getNodeId());
-  }
+  // Insert the new nodes into the topological ordering. We must do this in
+  // a valid topological ordering as nothing is going to go back and re-sort
+  // these nodes. We continually insert before 'N' in sequence as this is
+  // essentially a pre-flattened and pre-sorted sequence of nodes. There is no
+  // hierarchy left to express.
+  InsertDAGNode(DAG, N, Eight);
+  InsertDAGNode(DAG, N, Srl);
+  InsertDAGNode(DAG, N, NewMask);
+  InsertDAGNode(DAG, N, And);
+  InsertDAGNode(DAG, N, ShlCount);
+  InsertDAGNode(DAG, N, Shl);
   DAG.ReplaceAllUsesWith(N, Shl);
   AM.IndexReg = And;
   AM.Scale = (1 << ScaleLog);
   return false;
 }
 
+// Transforms "(X << C1) & C2" to "(X & (C2>>C1)) << C1" if safe and if this
+// allows us to fold the shift into this addressing mode. Returns false if the
+// transform succeeded.
+static bool FoldMaskedShiftToScaledMask(SelectionDAG &DAG, SDValue N,
+                                        uint64_t Mask,
+                                        SDValue Shift, SDValue X,
+                                        X86ISelAddressMode &AM) {
+  if (Shift.getOpcode() != ISD::SHL ||
+      !isa<ConstantSDNode>(Shift.getOperand(1)))
+    return true;
+
+  // Not likely to be profitable if either the AND or SHIFT node has more
+  // than one use (unless all uses are for address computation). Besides,
+  // isel mechanism requires their node ids to be reused.
+  if (!N.hasOneUse() || !Shift.hasOneUse())
+    return true;
+
+  // Verify that the shift amount is something we can fold.
+  unsigned ShiftAmt = Shift.getConstantOperandVal(1);
+  if (ShiftAmt != 1 && ShiftAmt != 2 && ShiftAmt != 3)
+    return true;
+
+  EVT VT = N.getValueType();
+  DebugLoc DL = N.getDebugLoc();
+  SDValue NewMask = DAG.getConstant(Mask >> ShiftAmt, VT);
+  SDValue NewAnd = DAG.getNode(ISD::AND, DL, VT, X, NewMask);
+  SDValue NewShift = DAG.getNode(ISD::SHL, DL, VT, NewAnd, Shift.getOperand(1));
+
+  // Insert the new nodes into the topological ordering. We must do this in
+  // a valid topological ordering as nothing is going to go back and re-sort
+  // these nodes. We continually insert before 'N' in sequence as this is
+  // essentially a pre-flattened and pre-sorted sequence of nodes. There is no
+  // hierarchy left to express.
+  InsertDAGNode(DAG, N, NewMask);
+  InsertDAGNode(DAG, N, NewAnd);
+  InsertDAGNode(DAG, N, NewShift);
+  DAG.ReplaceAllUsesWith(N, NewShift);
+
+  AM.Scale = 1 << ShiftAmt;
+  AM.IndexReg = NewAnd;
+  return false;
+}
+
 // Implement some heroics to detect shifts of masked values where the mask can
 // be replaced by extending the shift and undoing that in the addressing mode
 // scale. Patterns such as (shl (srl x, c1), c2) are canonicalized into (and
@@ -812,35 +848,24 @@ static bool FoldMaskAndShiftToExtract(SelectionDAG &DAG, SDValue N,
 //   andl $124, %rcx
 //   addl (%rsi,%rcx), %eax
 //
+// Note that this function assumes the mask is provided as a mask *after* the
+// value is shifted. The input chain may or may not match that, but computing
+// such a mask is trivial.
 static bool FoldMaskAndShiftToScale(SelectionDAG &DAG, SDValue N,
+                                    uint64_t Mask,
+                                    SDValue Shift, SDValue X,
                                     X86ISelAddressMode &AM) {
-  // Scale must not be used already.
-  if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) return true;
-
-  SDValue Shift = N;
-  SDValue And = N.getOperand(0);
-  if (N.getOpcode() != ISD::SRL)
-    std::swap(Shift, And);
-  if (Shift.getOpcode() != ISD::SRL || And.getOpcode() != ISD::AND ||
-      !Shift.hasOneUse() ||
-      !isa<ConstantSDNode>(Shift.getOperand(1)) ||
-      !isa<ConstantSDNode>(And.getOperand(1)))
+  if (Shift.getOpcode() != ISD::SRL || !Shift.hasOneUse() ||
+      !isa<ConstantSDNode>(Shift.getOperand(1)))
     return true;
-  SDValue X = (N == Shift ? And.getOperand(0) : Shift.getOperand(0));
 
-  // We only handle up to 64-bit values here as those are what matter for
-  // addressing mode optimizations.
-  if (X.getValueSizeInBits() > 64) return true;
-
-  uint64_t Mask = And.getConstantOperandVal(1);
   unsigned ShiftAmt = Shift.getConstantOperandVal(1);
   unsigned MaskLZ = CountLeadingZeros_64(Mask);
   unsigned MaskTZ = CountTrailingZeros_64(Mask);
 
   // The amount of shift we're trying to fit into the addressing mode is taken
-  // from the trailing zeros of the mask. If the mask is pre-shift, we subtract
-  // the shift amount.
-  int AMShiftAmt = MaskTZ - (N == Shift ? ShiftAmt : 0);
+  // from the trailing zeros of the mask.
+  unsigned AMShiftAmt = MaskTZ;
 
   // There is nothing we can do here unless the mask is removing some bits.
   // Also, the addressing mode can only represent shifts of 1, 2, or 3 bits.
@@ -850,9 +875,8 @@ static bool FoldMaskAndShiftToScale(SelectionDAG &DAG, SDValue N,
   if (CountTrailingOnes_64(Mask >> MaskTZ) + MaskTZ + MaskLZ != 64) return true;
 
   // Scale the leading zero count down based on the actual size of the value.
-  // Also scale it down based on the size of the shift if it was applied
-  // before the mask.
-  MaskLZ -= (64 - X.getValueSizeInBits()) + (N == Shift ? 0 : ShiftAmt);
+  // Also scale it down based on the size of the shift.
+  MaskLZ -= (64 - X.getValueSizeInBits()) + ShiftAmt;
 
   // The final check is to ensure that any masked out high bits of X are
   // already known to be zero. Otherwise, the mask has a semantic impact
@@ -883,11 +907,7 @@ static bool FoldMaskAndShiftToScale(SelectionDAG &DAG, SDValue N,
     assert(X.getValueType() != VT);
     // We looked through an ANY_EXTEND node, insert a ZERO_EXTEND.
     SDValue NewX = DAG.getNode(ISD::ZERO_EXTEND, X.getDebugLoc(), VT, X);
-    if (NewX.getNode()->getNodeId() == -1 ||
-        NewX.getNode()->getNodeId() > N.getNode()->getNodeId()) {
-      DAG.RepositionNode(N.getNode(), NewX.getNode());
-      NewX.getNode()->setNodeId(N.getNode()->getNodeId());
-    }
+    InsertDAGNode(DAG, N, NewX);
     X = NewX;
   }
   DebugLoc DL = N.getDebugLoc();
@@ -895,26 +915,16 @@ static bool FoldMaskAndShiftToScale(SelectionDAG &DAG, SDValue N,
   SDValue NewSRL = DAG.getNode(ISD::SRL, DL, VT, X, NewSRLAmt);
   SDValue NewSHLAmt = DAG.getConstant(AMShiftAmt, MVT::i8);
   SDValue NewSHL = DAG.getNode(ISD::SHL, DL, VT, NewSRL, NewSHLAmt);
-  if (NewSRLAmt.getNode()->getNodeId() == -1 ||
-      NewSRLAmt.getNode()->getNodeId() > N.getNode()->getNodeId()) {
-    DAG.RepositionNode(N.getNode(), NewSRLAmt.getNode());
-    NewSRLAmt.getNode()->setNodeId(N.getNode()->getNodeId());
-  }
-  if (NewSRL.getNode()->getNodeId() == -1 ||
-      NewSRL.getNode()->getNodeId() > N.getNode()->getNodeId()) {
-    DAG.RepositionNode(N.getNode(), NewSRL.getNode());
-    NewSRL.getNode()->setNodeId(N.getNode()->getNodeId());
-  }
-  if (NewSHLAmt.getNode()->getNodeId() == -1 ||
-      NewSHLAmt.getNode()->getNodeId() > N.getNode()->getNodeId()) {
-    DAG.RepositionNode(N.getNode(), NewSHLAmt.getNode());
-    NewSHLAmt.getNode()->setNodeId(N.getNode()->getNodeId());
-  }
-  if (NewSHL.getNode()->getNodeId() == -1 ||
-      NewSHL.getNode()->getNodeId() > N.getNode()->getNodeId()) {
-    DAG.RepositionNode(N.getNode(), NewSHL.getNode());
-    NewSHL.getNode()->setNodeId(N.getNode()->getNodeId());
-  }
+
+  // Insert the new nodes into the topological ordering. We must do this in
+  // a valid topological ordering as nothing is going to go back and re-sort
+  // these nodes. We continually insert before 'N' in sequence as this is
+  // essentially a pre-flattened and pre-sorted sequence of nodes. There is no
+  // hierarchy left to express.
+  InsertDAGNode(DAG, N, NewSRLAmt);
+  InsertDAGNode(DAG, N, NewSRL);
+  InsertDAGNode(DAG, N, NewSHLAmt);
+  InsertDAGNode(DAG, N, NewSHL);
   DAG.ReplaceAllUsesWith(N, NewSHL);
 
   AM.Scale = 1 << AMShiftAmt;
@@ -1011,12 +1021,32 @@ bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
     break;
     }
 
-  case ISD::SRL:
+  case ISD::SRL: {
+    // Scale must not be used already.
+    if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
+
+    SDValue And = N.getOperand(0);
+    if (And.getOpcode() != ISD::AND) break;
+    SDValue X = And.getOperand(0);
+
+    // We only handle up to 64-bit values here as those are what matter for
+    // addressing mode optimizations.
+    if (X.getValueSizeInBits() > 64) break;
+
+    // The mask used for the transform is expected to be post-shift, but we
+    // found the shift first so just apply the shift to the mask before passing
+    // it down.
+    if (!isa<ConstantSDNode>(N.getOperand(1)) ||
+        !isa<ConstantSDNode>(And.getOperand(1)))
+      break;
+    uint64_t Mask = And.getConstantOperandVal(1) >> N.getConstantOperandVal(1);
+
     // Try to fold the mask and shift into the scale, and return false if we
     // succeed.
-    if (!FoldMaskAndShiftToScale(*CurDAG, N, AM))
+    if (!FoldMaskAndShiftToScale(*CurDAG, N, Mask, N, X, AM))
       return false;
     break;
+  }
 
   case ISD::SMUL_LOHI:
   case ISD::UMUL_LOHI:
@@ -1121,16 +1151,8 @@ bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
     AM.Scale = 1;
 
     // Insert the new nodes into the topological ordering.
-    if (Zero.getNode()->getNodeId() == -1 ||
-        Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
-      CurDAG->RepositionNode(N.getNode(), Zero.getNode());
-      Zero.getNode()->setNodeId(N.getNode()->getNodeId());
-    }
-    if (Neg.getNode()->getNodeId() == -1 ||
-        Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
-      CurDAG->RepositionNode(N.getNode(), Neg.getNode());
-      Neg.getNode()->setNodeId(N.getNode()->getNodeId());
-    }
+    InsertDAGNode(*CurDAG, N, Zero);
+    InsertDAGNode(*CurDAG, N, Neg);
     return false;
   }
 
@@ -1185,75 +1207,34 @@ bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
     // Perform some heroic transforms on an and of a constant-count shift
     // with a constant to enable use of the scaled offset field.
 
-    SDValue Shift = N.getOperand(0);
-    if (Shift.getNumOperands() != 2) break;
-
     // Scale must not be used already.
     if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
 
+    SDValue Shift = N.getOperand(0);
+    if (Shift.getOpcode() != ISD::SRL && Shift.getOpcode() != ISD::SHL) break;
     SDValue X = Shift.getOperand(0);
-    ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
-    ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
-    if (!C1 || !C2) break;
+
+    // We only handle up to 64-bit values here as those are what matter for
+    // addressing mode optimizations.
+    if (X.getValueSizeInBits() > 64) break;
+
+    if (!isa<ConstantSDNode>(N.getOperand(1)))
+      break;
+    uint64_t Mask = N.getConstantOperandVal(1);
 
     // Try to fold the mask and shift into an extract and scale.
-    if (!FoldMaskAndShiftToExtract(*CurDAG, N, C2->getZExtValue(),
-                                   Shift, X, AM))
+    if (!FoldMaskAndShiftToExtract(*CurDAG, N, Mask, Shift, X, AM))
       return false;
 
     // Try to fold the mask and shift directly into the scale.
-    if (!FoldMaskAndShiftToScale(*CurDAG, N, AM))
+    if (!FoldMaskAndShiftToScale(*CurDAG, N, Mask, Shift, X, AM))
       return false;
 
-    // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
-    // allows us to fold the shift into this addressing mode.
-    if (Shift.getOpcode() != ISD::SHL) break;
-
-    // Not likely to be profitable if either the AND or SHIFT node has more
-    // than one use (unless all uses are for address computation). Besides,
-    // isel mechanism requires their node ids to be reused.
-    if (!N.hasOneUse() || !Shift.hasOneUse())
-      break;
-    
-    // Verify that the shift amount is something we can fold.
-    unsigned ShiftCst = C1->getZExtValue();
-    if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
-      break;
-    
-    // Get the new AND mask, this folds to a constant.
-    SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
-                                         SDValue(C2, 0), SDValue(C1, 0));
-    SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X, 
-                                     NewANDMask);
-    SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
-                                       NewAND, SDValue(C1, 0));
-
-    // Insert the new nodes into the topological ordering.
-    if (C1->getNodeId() > X.getNode()->getNodeId()) {
-      CurDAG->RepositionNode(X.getNode(), C1);
-      C1->setNodeId(X.getNode()->getNodeId());
-    }
-    if (NewANDMask.getNode()->getNodeId() == -1 ||
-        NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
-      CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
-      NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
-    }
-    if (NewAND.getNode()->getNodeId() == -1 ||
-        NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
-      CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
-      NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
-    }
-    if (NewSHIFT.getNode()->getNodeId() == -1 ||
-        NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
-      CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
-      NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
-    }
-
-    CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
-    
-    AM.Scale = 1 << ShiftCst;
-    AM.IndexReg = NewAND;
-    return false;
+    // Try to swap the mask and shift to place shifts which can be done as
+    // a scale on the outside of the mask.
+    if (!FoldMaskedShiftToScaledMask(*CurDAG, N, Mask, Shift, X, AM))
+      return false;
+    break;
   }
   }
 
@@ -1987,7 +1968,6 @@ SDNode *X86DAGToDAGISel::Select(SDNode *Node) {
     SDNode *New = CurDAG->getMachineNode(Op, dl, NVT, N0->getOperand(0),NewCst);
     return CurDAG->SelectNodeTo(Node, ShlOp, NVT, SDValue(New, 0),
                                 getI8Imm(ShlVal));
-    break;
   }
   case X86ISD::UMUL: {
     SDValue N0 = Node->getOperand(0);