[TableGen] Use std::remove_if instead of manually coded loops that call erase multipl...
authorCraig Topper <craig.topper@gmail.com>
Tue, 24 Nov 2015 08:20:47 +0000 (08:20 +0000)
committerCraig Topper <craig.topper@gmail.com>
Tue, 24 Nov 2015 08:20:47 +0000 (08:20 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@253964 91177308-0d34-0410-b5e6-96231b3b80d8

utils/TableGen/CodeGenDAGPatterns.cpp

index 39dab488f45400960a6a27d6bc1a4719bfa682f7..8bd0dc3b4b247c0212eeacb9abd9117eec83dc23 100644 (file)
@@ -385,55 +385,55 @@ bool EEVT::TypeSet::EnforceSmallerThan(EEVT::TypeSet &Other, TreePattern &TP) {
   // type size is smaller than the scalar size of the smallest type. For
   // vectors, we also need to make sure that the total size is no larger than
   // the size of the smallest type.
-  TypeSet InputSet(Other);
-  MVT Smallest = TypeVec[0];
-  for (unsigned i = 0; i != Other.TypeVec.size(); ++i) {
-    MVT OtherVT = Other.TypeVec[i];
-    // Don't compare vector and non-vector types.
-    if (OtherVT.isVector() != Smallest.isVector())
-      continue;
-    // The getSizeInBits() check here is only needed for vectors, but is
-    // a subset of the scalar check for scalars so no need to qualify.
-    if (OtherVT.getScalarSizeInBits() <= Smallest.getScalarSizeInBits() ||
-        OtherVT.getSizeInBits() < Smallest.getSizeInBits()) {
-      Other.TypeVec.erase(Other.TypeVec.begin()+i--);
-      MadeChange = true;
+  {
+    TypeSet InputSet(Other);
+    MVT Smallest = TypeVec[0];
+    auto I = std::remove_if(Other.TypeVec.begin(), Other.TypeVec.end(),
+      [Smallest](MVT OtherVT) {
+        // Don't compare vector and non-vector types.
+        if (OtherVT.isVector() != Smallest.isVector())
+          return false;
+        // The getSizeInBits() check here is only needed for vectors, but is
+        // a subset of the scalar check for scalars so no need to qualify.
+        return OtherVT.getScalarSizeInBits() <= Smallest.getScalarSizeInBits()||
+               OtherVT.getSizeInBits() < Smallest.getSizeInBits();
+      });
+    MadeChange |= I != Other.TypeVec.end(); // If we're about to remove types.
+    Other.TypeVec.erase(I, Other.TypeVec.end());
+
+    if (Other.TypeVec.empty()) {
+      TP.error("Type inference contradiction found, '" + InputSet.getName() +
+               "' has nothing larger than '" + getName() +"'!");
+      return false;
     }
   }
 
-  if (Other.TypeVec.empty()) {
-    TP.error("Type inference contradiction found, '" + InputSet.getName() +
-             "' has nothing larger than '" + getName() +"'!");
-    return false;
-  }
-
   // Okay, find the largest type from the other set and remove anything the
   // same or smaller from the current set. We need to ensure that the scalar
   // type size is larger than the scalar size of the largest type. For
   // vectors, we also need to make sure that the total size is no smaller than
   // the size of the largest type.
-  InputSet = TypeSet(*this);
-  MVT Largest = Other.TypeVec[Other.TypeVec.size()-1];
-  for (unsigned i = 0; i != TypeVec.size(); ++i) {
-    MVT OtherVT = TypeVec[i];
-    // Don't compare vector and non-vector types.
-    if (OtherVT.isVector() != Largest.isVector())
-      continue;
-    // The getSizeInBits() check here is only needed for vectors, but is
-    // a subset of the scalar check for scalars so no need to qualify.
-    if (OtherVT.getScalarSizeInBits() >= Largest.getScalarSizeInBits() ||
-         OtherVT.getSizeInBits() > Largest.getSizeInBits()) {
-      TypeVec.erase(TypeVec.begin()+i--);
-      MadeChange = true;
+  {
+    TypeSet InputSet(*this);
+    MVT Largest = Other.TypeVec[Other.TypeVec.size()-1];
+    auto I = std::remove_if(TypeVec.begin(), TypeVec.end(),
+      [Largest](MVT OtherVT) {
+        // Don't compare vector and non-vector types.
+        if (OtherVT.isVector() != Largest.isVector())
+          return false;
+        return OtherVT.getScalarSizeInBits() >= Largest.getScalarSizeInBits() ||
+               OtherVT.getSizeInBits() > Largest.getSizeInBits();
+      });
+    MadeChange |= I != TypeVec.end(); // If we're about to remove types.
+    TypeVec.erase(I, TypeVec.end());
+
+    if (TypeVec.empty()) {
+      TP.error("Type inference contradiction found, '" + InputSet.getName() +
+               "' has nothing smaller than '" + Other.getName() +"'!");
+      return false;
     }
   }
 
-  if (TypeVec.empty()) {
-    TP.error("Type inference contradiction found, '" + InputSet.getName() +
-             "' has nothing smaller than '" + Other.getName() +"'!");
-    return false;
-  }
-
   return MadeChange;
 }
 
@@ -448,17 +448,17 @@ bool EEVT::TypeSet::EnforceVectorEltTypeIs(MVT::SimpleValueType VT,
   TypeSet InputSet(*this);
 
   // Filter out all the types which don't have the right element type.
-  for (unsigned i = 0; i != TypeVec.size(); ++i) {
-    assert(isVector(TypeVec[i]) && "EnforceVector didn't work");
-    if (MVT(TypeVec[i]).getVectorElementType().SimpleTy != VT) {
-      TypeVec.erase(TypeVec.begin()+i--);
-      MadeChange = true;
-    }
-  }
+  auto I = std::remove_if(TypeVec.begin(), TypeVec.end(),
+    [VT](MVT VVT) {
+      return VVT.getVectorElementType().SimpleTy != VT;
+    });
+  MadeChange |= I != TypeVec.end();
+  TypeVec.erase(I, TypeVec.end());
 
   if (TypeVec.empty()) {  // FIXME: Really want an SMLoc here!
     TP.error("Type inference contradiction found, forcing '" +
-             InputSet.getName() + "' to have a vector element");
+             InputSet.getName() + "' to have a vector element of type " +
+             getEnumName(VT));
     return false;
   }
 
@@ -535,13 +535,13 @@ bool EEVT::TypeSet::EnforceVectorSubVectorTypeIs(EEVT::TypeSet &VTOperand,
     // Only keep types that have less elements than VTOperand.
     TypeSet InputSet(VTOperand);
 
-    for (unsigned i = 0; i != VTOperand.TypeVec.size(); ++i) {
-      assert(isVector(VTOperand.TypeVec[i]) && "EnforceVector didn't work");
-      if (MVT(VTOperand.TypeVec[i]).getVectorNumElements() >= NumElems) {
-        VTOperand.TypeVec.erase(VTOperand.TypeVec.begin()+i--);
-        MadeChange = true;
-      }
-    }
+    auto I = std::remove_if(VTOperand.TypeVec.begin(), VTOperand.TypeVec.end(),
+                            [NumElems](MVT VVT) {
+                              return VVT.getVectorNumElements() >= NumElems;
+                            });
+    MadeChange |= I != VTOperand.TypeVec.end();
+    VTOperand.TypeVec.erase(I, VTOperand.TypeVec.end());
+
     if (VTOperand.TypeVec.empty()) {  // FIXME: Really want an SMLoc here!
       TP.error("Type inference contradiction found, forcing '" +
                InputSet.getName() + "' to have less vector elements than '" +
@@ -559,13 +559,13 @@ bool EEVT::TypeSet::EnforceVectorSubVectorTypeIs(EEVT::TypeSet &VTOperand,
     // Only keep types that have more elements than 'this'.
     TypeSet InputSet(*this);
 
-    for (unsigned i = 0; i != TypeVec.size(); ++i) {
-      assert(isVector(TypeVec[i]) && "EnforceVector didn't work");
-      if (MVT(TypeVec[i]).getVectorNumElements() <= NumElems) {
-        TypeVec.erase(TypeVec.begin()+i--);
-        MadeChange = true;
-      }
-    }
+    auto I = std::remove_if(TypeVec.begin(), TypeVec.end(),
+                            [NumElems](MVT VVT) {
+                              return VVT.getVectorNumElements() <= NumElems;
+                            });
+    MadeChange |= I != TypeVec.end();
+    TypeVec.erase(I, TypeVec.end());
+
     if (TypeVec.empty()) {  // FIXME: Really want an SMLoc here!
       TP.error("Type inference contradiction found, forcing '" +
                InputSet.getName() + "' to have more vector elements than '" +
@@ -597,13 +597,13 @@ bool EEVT::TypeSet::EnforceVectorSameNumElts(EEVT::TypeSet &VTOperand,
     // Only keep types that have same elements as VTOperand.
     TypeSet InputSet(VTOperand);
 
-    for (unsigned i = 0; i != VTOperand.TypeVec.size(); ++i) {
-      assert(isVector(VTOperand.TypeVec[i]) && "EnforceVector didn't work");
-      if (MVT(VTOperand.TypeVec[i]).getVectorNumElements() != NumElems) {
-        VTOperand.TypeVec.erase(VTOperand.TypeVec.begin()+i--);
-        MadeChange = true;
-      }
-    }
+    auto I = std::remove_if(VTOperand.TypeVec.begin(), VTOperand.TypeVec.end(),
+                            [NumElems](MVT VVT) {
+                              return VVT.getVectorNumElements() != NumElems;
+                            });
+    MadeChange |= I != VTOperand.TypeVec.end();
+    VTOperand.TypeVec.erase(I, VTOperand.TypeVec.end());
+
     if (VTOperand.TypeVec.empty()) {  // FIXME: Really want an SMLoc here!
       TP.error("Type inference contradiction found, forcing '" +
                InputSet.getName() + "' to have same number elements as '" +
@@ -617,13 +617,13 @@ bool EEVT::TypeSet::EnforceVectorSameNumElts(EEVT::TypeSet &VTOperand,
     // Only keep types that have same elements as 'this'.
     TypeSet InputSet(*this);
 
-    for (unsigned i = 0; i != TypeVec.size(); ++i) {
-      assert(isVector(TypeVec[i]) && "EnforceVector didn't work");
-      if (MVT(TypeVec[i]).getVectorNumElements() != NumElems) {
-        TypeVec.erase(TypeVec.begin()+i--);
-        MadeChange = true;
-      }
-    }
+    auto I = std::remove_if(TypeVec.begin(), TypeVec.end(),
+                            [NumElems](MVT VVT) {
+                              return VVT.getVectorNumElements() != NumElems;
+                            });
+    MadeChange |= I != TypeVec.end();
+    TypeVec.erase(I, TypeVec.end());
+
     if (TypeVec.empty()) {  // FIXME: Really want an SMLoc here!
       TP.error("Type inference contradiction found, forcing '" +
                InputSet.getName() + "' to have same number elements than '" +