>From cc261cb0bf969c96b95d58dc55387a1205aba8e3 Mon Sep 17 00:00:00 2001
From: Stepan Dyatkovskiy <stpworld_dog_narod.ru>
Date: Mon, 3 Feb 2014 13:31:55 +0400
Subject: [PATCH 01/15] types comparison.

---
 lib/Transforms/IPO/MergeFunctions.cpp |   61 ++++++++++++++++++++-------------
 1 file changed, 38 insertions(+), 23 deletions(-)

diff --git a/lib/Transforms/IPO/MergeFunctions.cpp b/lib/Transforms/IPO/MergeFunctions.cpp
index 33f0707..f0c5c05 100644
--- a/lib/Transforms/IPO/MergeFunctions.cpp
+++ b/lib/Transforms/IPO/MergeFunctions.cpp
@@ -194,7 +194,12 @@ private:
   }
 
   /// Compare two Types, treating all pointer types as equal.
-  bool isEquivalentType(Type *Ty1, Type *Ty2) const;
+  int cmpType(Type *Ty1, Type *Ty2) const;
+
+  /// Compare two Types, treating all pointer types as equal.
+  bool isEquivalentType(Type *Ty1, Type *Ty2) const {
+    return cmpType(Ty1, Ty2) == 0;
+  }
 
   // The two functions undergoing comparison.
   const Function *F1, *F2;
@@ -207,9 +212,15 @@ private:
 
 }
 
+static int cmpNumbers(uint64_t L, uint64_t R) {
+  if (L < R) return -1;
+  if (L > R) return 1;
+  return 0;
+}
+
 // Any two pointers in the same address space are equivalent, intptr_t and
 // pointers are equivalent. Otherwise, standard type equivalence rules apply.
-bool FunctionComparator::isEquivalentType(Type *Ty1, Type *Ty2) const {
+int FunctionComparator::cmpType(Type *Ty1, Type *Ty2) const {
 
   PointerType *PTy1 = dyn_cast<PointerType>(Ty1);
   PointerType *PTy2 = dyn_cast<PointerType>(Ty2);
@@ -220,10 +231,10 @@ bool FunctionComparator::isEquivalentType(Type *Ty1, Type *Ty2) const {
   }
 
   if (Ty1 == Ty2)
-    return true;
+    return 0;
 
-  if (Ty1->getTypeID() != Ty2->getTypeID())
-    return false;
+  if (int Res = cmpNumbers(Ty1->getTypeID(), Ty2->getTypeID()))
+    return Res;
 
   switch (Ty1->getTypeID()) {
   default:
@@ -232,7 +243,7 @@ bool FunctionComparator::isEquivalentType(Type *Ty1, Type *Ty2) const {
   case Type::IntegerTyID:
   case Type::VectorTyID:
     // Ty1 == Ty2 would have returned true earlier.
-    return false;
+    return cmpNumbers((uint64_t)Ty1, (uint64_t)Ty2);
 
   case Type::VoidTyID:
   case Type::FloatTyID:
@@ -242,51 +253,55 @@ bool FunctionComparator::isEquivalentType(Type *Ty1, Type *Ty2) const {
   case Type::PPC_FP128TyID:
   case Type::LabelTyID:
   case Type::MetadataTyID:
-    return true;
+    return 0;
 
   case Type::PointerTyID: {
     assert(PTy1 && PTy2 && "Both types must be pointers here.");
-    return PTy1->getAddressSpace() == PTy2->getAddressSpace();
+    return cmpNumbers(PTy1->getAddressSpace(), PTy2->getAddressSpace());
   }
 
   case Type::StructTyID: {
     StructType *STy1 = cast<StructType>(Ty1);
     StructType *STy2 = cast<StructType>(Ty2);
     if (STy1->getNumElements() != STy2->getNumElements())
-      return false;
+      return cmpNumbers(STy1->getNumElements(), STy2->getNumElements());
 
     if (STy1->isPacked() != STy2->isPacked())
-      return false;
+      return cmpNumbers(STy1->isPacked(), STy2->isPacked());
 
     for (unsigned i = 0, e = STy1->getNumElements(); i != e; ++i) {
-      if (!isEquivalentType(STy1->getElementType(i), STy2->getElementType(i)))
-        return false;
+      if (int Res = cmpType(STy1->getElementType(i),
+                            STy2->getElementType(i)))
+        return Res;
     }
-    return true;
+    return 0;
   }
 
   case Type::FunctionTyID: {
     FunctionType *FTy1 = cast<FunctionType>(Ty1);
     FunctionType *FTy2 = cast<FunctionType>(Ty2);
-    if (FTy1->getNumParams() != FTy2->getNumParams() ||
-        FTy1->isVarArg() != FTy2->isVarArg())
-      return false;
+    if (FTy1->getNumParams() != FTy2->getNumParams())
+      return cmpNumbers(FTy1->getNumParams(), FTy2->getNumParams());
 
-    if (!isEquivalentType(FTy1->getReturnType(), FTy2->getReturnType()))
-      return false;
+    if (FTy1->isVarArg() != FTy2->isVarArg())
+      return cmpNumbers(FTy1->isVarArg(), FTy2->isVarArg());;
+
+    if (int Res = cmpType(FTy1->getReturnType(), FTy2->getReturnType()))
+      return Res;
 
     for (unsigned i = 0, e = FTy1->getNumParams(); i != e; ++i) {
-      if (!isEquivalentType(FTy1->getParamType(i), FTy2->getParamType(i)))
-        return false;
+      if (int Res = cmpType(FTy1->getParamType(i), FTy2->getParamType(i)))
+        return Res;
     }
-    return true;
+    return 0;
   }
 
   case Type::ArrayTyID: {
     ArrayType *ATy1 = cast<ArrayType>(Ty1);
     ArrayType *ATy2 = cast<ArrayType>(Ty2);
-    return ATy1->getNumElements() == ATy2->getNumElements() &&
-           isEquivalentType(ATy1->getElementType(), ATy2->getElementType());
+    if (ATy1->getNumElements() != ATy2->getNumElements())
+      return cmpNumbers(ATy1->getNumElements(), ATy2->getNumElements());
+    return cmpType(ATy1->getElementType(), ATy2->getElementType());
   }
   }
 }
-- 
1.7.10.4



