[llvm-commits] [llvm] r122293 - in /llvm/trunk: include/llvm/ADT/IntEqClasses.h lib/Support/CMakeLists.txt lib/Support/IntEqClasses.cpp unittests/ADT/IntEqClassesTest.cpp unittests/CMakeLists.txt
Jakob Stoklund Olesen
stoklund at 2pi.dk
Mon Dec 20 16:04:46 PST 2010
Author: stoklund
Date: Mon Dec 20 18:04:46 2010
New Revision: 122293
URL: http://llvm.org/viewvc/llvm-project?rev=122293&view=rev
Log:
Add ADT/IntEqClasses.h as a light-weight implementation of EquivalenceClasses.h.
This implementation already exists as ConnectedVNInfoEqClasses in
LiveInterval.cpp, and it seems to be generally useful to have a light-weight way
of forming equivalence classes of small integers.
IntEqClasses doesn't allow enumeration of the elements in a class.
Added:
llvm/trunk/include/llvm/ADT/IntEqClasses.h
llvm/trunk/lib/Support/IntEqClasses.cpp
llvm/trunk/unittests/ADT/IntEqClassesTest.cpp
Modified:
llvm/trunk/lib/Support/CMakeLists.txt
llvm/trunk/unittests/CMakeLists.txt
Added: llvm/trunk/include/llvm/ADT/IntEqClasses.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/IntEqClasses.h?rev=122293&view=auto
==============================================================================
--- llvm/trunk/include/llvm/ADT/IntEqClasses.h (added)
+++ llvm/trunk/include/llvm/ADT/IntEqClasses.h Mon Dec 20 18:04:46 2010
@@ -0,0 +1,81 @@
+//===-- llvm/ADT/IntEqClasses.h - Equiv. Classes of Integers ----*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Equivalence classes for small integers. This is a mapping of the integers
+// 0 .. N-1 into M equivalence classes numbered 0 .. M-1.
+//
+// Initially each integer has its own equivalence class. Classes are joined by
+// passing a representative member of each class to join().
+//
+// Once the classes are built, compress() will number them 0 .. M-1 and prevent
+// further changes.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ADT_INTEQCLASSES_H
+#define LLVM_ADT_INTEQCLASSES_H
+
+#include "llvm/ADT/SmallVector.h"
+
+namespace llvm {
+
+class IntEqClasses {
+ /// EC - When uncompressed, map each integer to a smaller member of its
+ /// equivalence class. The class leader is the smallest member and maps to
+ /// itself.
+ ///
+ /// When compressed, EC[i] is the equivalence class of i.
+ SmallVector<unsigned, 8> EC;
+
+ /// NumClasses - The number of equivalence classes when compressed, or 0 when
+ /// uncompressed.
+ unsigned NumClasses;
+
+public:
+ /// IntEqClasses - Create an equivalence class mapping for 0 .. N-1.
+ IntEqClasses(unsigned N) : NumClasses(0) { grow(N); }
+
+ /// grow - Increase capacity to hold 0 .. N-1, putting new integers in unique
+ /// equivalence classes.
+ /// This requires an uncompressed map.
+ void grow(unsigned N);
+
+ /// join - Join the equivalence classes of a and b. After joining classes,
+ /// findLeader(a) == findLeader(b).
+ /// This requires an uncompressed map.
+ void join(unsigned a, unsigned b);
+
+ /// findLeader - Compute the leader of a's equivalence class. This is the
+ /// smallest member of the class.
+ /// This requires an uncompressed map.
+ unsigned findLeader(unsigned a) const;
+
+ /// compress - Compress equivalence classes by numbering them 0 .. M.
+ /// This makes the equivalence class map immutable.
+ void compress();
+
+ /// getNumClasses - Return the number of equivalence classes after compress()
+ /// was called.
+ unsigned getNumClasses() const { return NumClasses; }
+
+ /// operator[] - Return a's equivalence class number, 0 .. getNumClasses()-1.
+ /// This requires a compressed map.
+ unsigned operator[](unsigned a) const {
+ assert(NumClasses && "operator[] called before compress()");
+ return EC[a];
+ }
+
+ /// uncompress - Change back to the uncompressed representation that allows
+ /// editing.
+ void uncompress();
+};
+
+} // End llvm namespace
+
+#endif
Modified: llvm/trunk/lib/Support/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/CMakeLists.txt?rev=122293&r1=122292&r2=122293&view=diff
==============================================================================
--- llvm/trunk/lib/Support/CMakeLists.txt (original)
+++ llvm/trunk/lib/Support/CMakeLists.txt Mon Dec 20 18:04:46 2010
@@ -22,6 +22,7 @@
FoldingSet.cpp
FormattedStream.cpp
GraphWriter.cpp
+ IntEqClasses.cpp
IntervalMap.cpp
IsInf.cpp
IsNAN.cpp
Added: llvm/trunk/lib/Support/IntEqClasses.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/IntEqClasses.cpp?rev=122293&view=auto
==============================================================================
--- llvm/trunk/lib/Support/IntEqClasses.cpp (added)
+++ llvm/trunk/lib/Support/IntEqClasses.cpp Mon Dec 20 18:04:46 2010
@@ -0,0 +1,69 @@
+//===-- llvm/ADT/IntEqClasses.cpp - Equivalence Classes of Integers -------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Equivalence classes for small integers. This is a mapping of the integers
+// 0 .. N-1 into M equivalence classes numbered 0 .. M-1.
+//
+// Initially each integer has its own equivalence class. Classes are joined by
+// passing a representative member of each class to join().
+//
+// Once the classes are built, compress() will number them 0 .. M-1 and prevent
+// further changes.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/IntEqClasses.h"
+
+using namespace llvm;
+
+void IntEqClasses::grow(unsigned N) {
+ assert(NumClasses == 0 && "grow() called after compress().");
+ while (EC.size() < N)
+ EC.push_back(EC.size());
+}
+
+void IntEqClasses::join(unsigned a, unsigned b) {
+ assert(NumClasses == 0 && "join() called after compress().");
+ unsigned eca = EC[a];
+ unsigned ecb = EC[b];
+ // Update pointers while searching for the leaders, compressing the paths
+ // incrementally. The larger leader will eventually be updated, joining the
+ // classes.
+ while (eca != ecb)
+ if (eca < ecb)
+ EC[b] = eca, b = ecb, ecb = EC[b];
+ else
+ EC[a] = ecb, a = eca, eca = EC[a];
+}
+
+unsigned IntEqClasses::findLeader(unsigned a) const {
+ assert(NumClasses == 0 && "findLeader() called after compress().");
+ while (a != EC[a])
+ a = EC[a];
+ return a;
+}
+
+void IntEqClasses::compress() {
+ if (NumClasses)
+ return;
+ for (unsigned i = 0, e = EC.size(); i != e; ++i)
+ EC[i] = (EC[i] == i) ? NumClasses++ : EC[EC[i]];
+}
+
+void IntEqClasses::uncompress() {
+ if (!NumClasses)
+ return;
+ SmallVector<unsigned, 8> Leader;
+ for (unsigned i = 0, e = EC.size(); i != e; ++i)
+ if (EC[i] < Leader.size())
+ EC[i] = Leader[EC[i]];
+ else
+ Leader.push_back(EC[i] = i);
+ NumClasses = 0;
+}
Added: llvm/trunk/unittests/ADT/IntEqClassesTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/IntEqClassesTest.cpp?rev=122293&view=auto
==============================================================================
--- llvm/trunk/unittests/ADT/IntEqClassesTest.cpp (added)
+++ llvm/trunk/unittests/ADT/IntEqClassesTest.cpp Mon Dec 20 18:04:46 2010
@@ -0,0 +1,107 @@
+//===---- ADT/IntEqClassesTest.cpp - IntEqClasses unit tests ----*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/IntEqClasses.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+
+TEST(IntEqClasses, Simple) {
+ IntEqClasses ec(10);
+
+ ec.join(0, 1);
+ ec.join(3, 2);
+ ec.join(4, 5);
+ ec.join(7, 6);
+
+ EXPECT_EQ(0u, ec.findLeader(0));
+ EXPECT_EQ(0u, ec.findLeader(1));
+ EXPECT_EQ(2u, ec.findLeader(2));
+ EXPECT_EQ(2u, ec.findLeader(3));
+ EXPECT_EQ(4u, ec.findLeader(4));
+ EXPECT_EQ(4u, ec.findLeader(5));
+ EXPECT_EQ(6u, ec.findLeader(6));
+ EXPECT_EQ(6u, ec.findLeader(7));
+ EXPECT_EQ(8u, ec.findLeader(8));
+ EXPECT_EQ(9u, ec.findLeader(9));
+
+ // join two non-leaders.
+ ec.join(1, 3);
+
+ EXPECT_EQ(0u, ec.findLeader(0));
+ EXPECT_EQ(0u, ec.findLeader(1));
+ EXPECT_EQ(0u, ec.findLeader(2));
+ EXPECT_EQ(0u, ec.findLeader(3));
+ EXPECT_EQ(4u, ec.findLeader(4));
+ EXPECT_EQ(4u, ec.findLeader(5));
+ EXPECT_EQ(6u, ec.findLeader(6));
+ EXPECT_EQ(6u, ec.findLeader(7));
+ EXPECT_EQ(8u, ec.findLeader(8));
+ EXPECT_EQ(9u, ec.findLeader(9));
+
+ // join two leaders.
+ ec.join(4, 8);
+
+ EXPECT_EQ(0u, ec.findLeader(0));
+ EXPECT_EQ(0u, ec.findLeader(1));
+ EXPECT_EQ(0u, ec.findLeader(2));
+ EXPECT_EQ(0u, ec.findLeader(3));
+ EXPECT_EQ(4u, ec.findLeader(4));
+ EXPECT_EQ(4u, ec.findLeader(5));
+ EXPECT_EQ(6u, ec.findLeader(6));
+ EXPECT_EQ(6u, ec.findLeader(7));
+ EXPECT_EQ(4u, ec.findLeader(8));
+ EXPECT_EQ(9u, ec.findLeader(9));
+
+ // join mixed.
+ ec.join(9, 1);
+
+ EXPECT_EQ(0u, ec.findLeader(0));
+ EXPECT_EQ(0u, ec.findLeader(1));
+ EXPECT_EQ(0u, ec.findLeader(2));
+ EXPECT_EQ(0u, ec.findLeader(3));
+ EXPECT_EQ(4u, ec.findLeader(4));
+ EXPECT_EQ(4u, ec.findLeader(5));
+ EXPECT_EQ(6u, ec.findLeader(6));
+ EXPECT_EQ(6u, ec.findLeader(7));
+ EXPECT_EQ(4u, ec.findLeader(8));
+ EXPECT_EQ(0u, ec.findLeader(9));
+
+ // compressed map.
+ ec.compress();
+ EXPECT_EQ(3u, ec.getNumClasses());
+
+ EXPECT_EQ(0u, ec[0]);
+ EXPECT_EQ(0u, ec[1]);
+ EXPECT_EQ(0u, ec[2]);
+ EXPECT_EQ(0u, ec[3]);
+ EXPECT_EQ(1u, ec[4]);
+ EXPECT_EQ(1u, ec[5]);
+ EXPECT_EQ(2u, ec[6]);
+ EXPECT_EQ(2u, ec[7]);
+ EXPECT_EQ(1u, ec[8]);
+ EXPECT_EQ(0u, ec[9]);
+
+ // uncompressed map.
+ ec.uncompress();
+ EXPECT_EQ(0u, ec.findLeader(0));
+ EXPECT_EQ(0u, ec.findLeader(1));
+ EXPECT_EQ(0u, ec.findLeader(2));
+ EXPECT_EQ(0u, ec.findLeader(3));
+ EXPECT_EQ(4u, ec.findLeader(4));
+ EXPECT_EQ(4u, ec.findLeader(5));
+ EXPECT_EQ(6u, ec.findLeader(6));
+ EXPECT_EQ(6u, ec.findLeader(7));
+ EXPECT_EQ(4u, ec.findLeader(8));
+ EXPECT_EQ(0u, ec.findLeader(9));
+}
+
+} // end anonymous namespace
Modified: llvm/trunk/unittests/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/CMakeLists.txt?rev=122293&r1=122292&r2=122293&view=diff
==============================================================================
--- llvm/trunk/unittests/CMakeLists.txt (original)
+++ llvm/trunk/unittests/CMakeLists.txt Mon Dec 20 18:04:46 2010
@@ -54,6 +54,7 @@
ADT/FoldingSet.cpp
ADT/ilistTest.cpp
ADT/ImmutableSetTest.cpp
+ ADT/IntEqClassesTest.cpp
ADT/IntervalMapTest.cpp
ADT/SmallBitVectorTest.cpp
ADT/SmallStringTest.cpp
More information about the llvm-commits
mailing list