More C++20 build fixes.

This is almost entirely taken from the upstream
https://github.com/llvm/llvm-project/commit/92310454bf0f1f9686f38afd11756c7d046495c9,
except for the fix to MachineOutliner.cpp, which no longer existed in
the tree at that point.

Bug: chromium:1284275
Change-Id: Icb72e3cb9336f346d8bba2cf1449d3042522f219
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/70808
Commit-Queue: Peter Kasting <pkasting@google.com>
Tested-by: Peter Kasting <pkasting@google.com>
Reviewed-by: Shahbaz Youssefi <syoussefi@google.com>
diff --git a/third_party/llvm-10.0/llvm/include/llvm/ADT/DenseMap.h b/third_party/llvm-10.0/llvm/include/llvm/ADT/DenseMap.h
index 148d319..c3cc810 100644
--- a/third_party/llvm-10.0/llvm/include/llvm/ADT/DenseMap.h
+++ b/third_party/llvm-10.0/llvm/include/llvm/ADT/DenseMap.h
@@ -1212,19 +1212,18 @@
     return Ptr;
   }
 
-  bool operator==(const ConstIterator &RHS) const {
-    assert((!Ptr || isHandleInSync()) && "handle not in sync!");
+  friend bool operator==(const DenseMapIterator &LHS,
+                         const DenseMapIterator &RHS) {
+    assert((!LHS.Ptr || LHS.isHandleInSync()) && "handle not in sync!");
     assert((!RHS.Ptr || RHS.isHandleInSync()) && "handle not in sync!");
-    assert(getEpochAddress() == RHS.getEpochAddress() &&
+    assert(LHS.getEpochAddress() == RHS.getEpochAddress() &&
            "comparing incomparable iterators!");
-    return Ptr == RHS.Ptr;
+    return LHS.Ptr == RHS.Ptr;
   }
-  bool operator!=(const ConstIterator &RHS) const {
-    assert((!Ptr || isHandleInSync()) && "handle not in sync!");
-    assert((!RHS.Ptr || RHS.isHandleInSync()) && "handle not in sync!");
-    assert(getEpochAddress() == RHS.getEpochAddress() &&
-           "comparing incomparable iterators!");
-    return Ptr != RHS.Ptr;
+
+  friend bool operator!=(const DenseMapIterator &LHS,
+                         const DenseMapIterator &RHS) {
+    return !(LHS == RHS);
   }
 
   inline DenseMapIterator& operator++() {  // Preincrement
diff --git a/third_party/llvm-10.0/llvm/include/llvm/ADT/DenseSet.h b/third_party/llvm-10.0/llvm/include/llvm/ADT/DenseSet.h
index 9afb715..4544e9d 100644
--- a/third_party/llvm-10.0/llvm/include/llvm/ADT/DenseSet.h
+++ b/third_party/llvm-10.0/llvm/include/llvm/ADT/DenseSet.h
@@ -124,8 +124,12 @@
 
     Iterator& operator++() { ++I; return *this; }
     Iterator operator++(int) { auto T = *this; ++I; return T; }
-    bool operator==(const ConstIterator& X) const { return I == X.I; }
-    bool operator!=(const ConstIterator& X) const { return I != X.I; }
+    friend bool operator==(const Iterator &X, const Iterator &Y) {
+      return X.I == Y.I;
+    }
+    friend bool operator!=(const Iterator &X, const Iterator &Y) {
+      return X.I != Y.I;
+    }
   };
 
   class ConstIterator {
@@ -149,8 +153,12 @@
 
     ConstIterator& operator++() { ++I; return *this; }
     ConstIterator operator++(int) { auto T = *this; ++I; return T; }
-    bool operator==(const ConstIterator& X) const { return I == X.I; }
-    bool operator!=(const ConstIterator& X) const { return I != X.I; }
+    friend bool operator==(const ConstIterator &X, const ConstIterator &Y) {
+      return X.I == Y.I;
+    }
+    friend bool operator!=(const ConstIterator &X, const ConstIterator &Y) {
+      return X.I != Y.I;
+    }
   };
 
   using iterator = Iterator;
diff --git a/third_party/llvm-10.0/llvm/include/llvm/ADT/STLExtras.h b/third_party/llvm-10.0/llvm/include/llvm/ADT/STLExtras.h
index b61dab2..620209b 100644
--- a/third_party/llvm-10.0/llvm/include/llvm/ADT/STLExtras.h
+++ b/third_party/llvm-10.0/llvm/include/llvm/ADT/STLExtras.h
@@ -486,12 +486,12 @@
     return *this;
   }
 
-  using BaseT::operator==;
-  bool operator==(const early_inc_iterator_impl &RHS) const {
+  friend bool operator==(const early_inc_iterator_impl &LHS,
+                         const early_inc_iterator_impl &RHS) {
 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
-    assert(!IsEarlyIncremented && "Cannot compare after dereferencing!");
+    assert(!LHS.IsEarlyIncremented && "Cannot compare after dereferencing!");
 #endif
-    return BaseT::operator==(RHS);
+    return (const BaseT &)LHS == (const BaseT &)RHS;
   }
 };
 
diff --git a/third_party/llvm-10.0/llvm/include/llvm/ADT/StringMap.h b/third_party/llvm-10.0/llvm/include/llvm/ADT/StringMap.h
index 108185b..2b00191 100644
--- a/third_party/llvm-10.0/llvm/include/llvm/ADT/StringMap.h
+++ b/third_party/llvm-10.0/llvm/include/llvm/ADT/StringMap.h
@@ -505,7 +505,9 @@
     return static_cast<DerivedTy &>(*this);
   }
 
-  bool operator==(const DerivedTy &RHS) const { return Ptr == RHS.Ptr; }
+  friend bool operator==(const DerivedTy &LHS, const DerivedTy &RHS) {
+    return LHS.Ptr == RHS.Ptr;
+  }
 
   DerivedTy &operator++() { // Preincrement
     ++Ptr;
diff --git a/third_party/llvm-10.0/llvm/include/llvm/ADT/iterator.h b/third_party/llvm-10.0/llvm/include/llvm/ADT/iterator.h
index 21948f5..a60b20d 100644
--- a/third_party/llvm-10.0/llvm/include/llvm/ADT/iterator.h
+++ b/third_party/llvm-10.0/llvm/include/llvm/ADT/iterator.h
@@ -146,28 +146,30 @@
     return tmp;
   }
 
+#ifndef __cpp_impl_three_way_comparison
   bool operator!=(const DerivedT &RHS) const {
-    return !static_cast<const DerivedT *>(this)->operator==(RHS);
+    return !(static_cast<const DerivedT &>(*this) == RHS);
   }
+#endif
 
   bool operator>(const DerivedT &RHS) const {
     static_assert(
         IsRandomAccess,
         "Relational operators are only defined for random access iterators.");
-    return !static_cast<const DerivedT *>(this)->operator<(RHS) &&
-           !static_cast<const DerivedT *>(this)->operator==(RHS);
+    return !(static_cast<const DerivedT &>(*this) < RHS) &&
+           !(static_cast<const DerivedT &>(*this) == RHS);
   }
   bool operator<=(const DerivedT &RHS) const {
     static_assert(
         IsRandomAccess,
         "Relational operators are only defined for random access iterators.");
-    return !static_cast<const DerivedT *>(this)->operator>(RHS);
+    return !(static_cast<const DerivedT &>(*this) > RHS);
   }
   bool operator>=(const DerivedT &RHS) const {
     static_assert(
         IsRandomAccess,
         "Relational operators are only defined for random access iterators.");
-    return !static_cast<const DerivedT *>(this)->operator<(RHS);
+   return !(static_cast<const DerivedT &>(*this) < RHS);
   }
 
   PointerT operator->() { return &static_cast<DerivedT *>(this)->operator*(); }
@@ -264,12 +266,16 @@
     return *static_cast<DerivedT *>(this);
   }
 
-  bool operator==(const DerivedT &RHS) const { return I == RHS.I; }
-  bool operator<(const DerivedT &RHS) const {
+  friend bool operator==(const iterator_adaptor_base &LHS,
+                         const iterator_adaptor_base &RHS) {
+    return LHS.I == RHS.I;
+  }
+  friend bool operator<(const iterator_adaptor_base &LHS,
+                        const iterator_adaptor_base &RHS) {
     static_assert(
         BaseT::IsRandomAccess,
         "Relational operators are only defined for random access iterators.");
-    return I < RHS.I;
+    return LHS.I < RHS.I;
   }
 
   ReferenceT operator*() const { return *I; }
diff --git a/third_party/llvm-10.0/llvm/include/llvm/CodeGen/LiveInterval.h b/third_party/llvm-10.0/llvm/include/llvm/CodeGen/LiveInterval.h
index 85bd1a2..2f0b4f7 100644
--- a/third_party/llvm-10.0/llvm/include/llvm/CodeGen/LiveInterval.h
+++ b/third_party/llvm-10.0/llvm/include/llvm/CodeGen/LiveInterval.h
@@ -729,10 +729,10 @@
         ++*this;
         return res;
       }
-      bool operator!=(const SingleLinkedListIterator<T> &Other) {
+      bool operator!=(const SingleLinkedListIterator<T> &Other) const {
         return P != Other.operator->();
       }
-      bool operator==(const SingleLinkedListIterator<T> &Other) {
+      bool operator==(const SingleLinkedListIterator<T> &Other) const {
         return P == Other.operator->();
       }
       T &operator*() const {
diff --git a/third_party/llvm-10.0/llvm/include/llvm/IR/Attributes.h b/third_party/llvm-10.0/llvm/include/llvm/IR/Attributes.h
index e6b2804..9b8672f 100644
--- a/third_party/llvm-10.0/llvm/include/llvm/IR/Attributes.h
+++ b/third_party/llvm-10.0/llvm/include/llvm/IR/Attributes.h
@@ -862,10 +862,8 @@
 
   bool td_empty() const { return TargetDepAttrs.empty(); }
 
-  bool operator==(const AttrBuilder &B);
-  bool operator!=(const AttrBuilder &B) {
-    return !(*this == B);
-  }
+  bool operator==(const AttrBuilder &B) const;
+  bool operator!=(const AttrBuilder &B) const { return !(*this == B); }
 };
 
 namespace AttributeFuncs {
diff --git a/third_party/llvm-10.0/llvm/lib/CodeGen/MachineOutliner.cpp b/third_party/llvm-10.0/llvm/lib/CodeGen/MachineOutliner.cpp
index 3a9104b..3344989 100644
--- a/third_party/llvm-10.0/llvm/lib/CodeGen/MachineOutliner.cpp
+++ b/third_party/llvm-10.0/llvm/lib/CodeGen/MachineOutliner.cpp
@@ -582,10 +582,10 @@
       return It;
     }
 
-    bool operator==(const RepeatedSubstringIterator &Other) {
+    bool operator==(const RepeatedSubstringIterator &Other) const {
       return N == Other.N;
     }
-    bool operator!=(const RepeatedSubstringIterator &Other) {
+    bool operator!=(const RepeatedSubstringIterator &Other) const {
       return !(*this == Other);
     }
 
diff --git a/third_party/llvm-10.0/llvm/lib/IR/Attributes.cpp b/third_party/llvm-10.0/llvm/lib/IR/Attributes.cpp
index 5ca99c9..16274df 100644
--- a/third_party/llvm-10.0/llvm/lib/IR/Attributes.cpp
+++ b/third_party/llvm-10.0/llvm/lib/IR/Attributes.cpp
@@ -1676,7 +1676,7 @@
   return Alignment != 0;
 }
 
-bool AttrBuilder::operator==(const AttrBuilder &B) {
+bool AttrBuilder::operator==(const AttrBuilder &B) const {
   if (Attrs != B.Attrs)
     return false;