Add DenseMap template and actually use it for for mapping virtual regs
authorAlkis Evlogimenos <alkis@evlogimenos.com>
Wed, 25 Feb 2004 21:55:45 +0000 (21:55 +0000)
committerAlkis Evlogimenos <alkis@evlogimenos.com>
Wed, 25 Feb 2004 21:55:45 +0000 (21:55 +0000)
to objects.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@11840 91177308-0d34-0410-b5e6-96231b3b80d8

include/Support/DenseMap.h [new file with mode: 0644]
include/llvm/ADT/DenseMap.h [new file with mode: 0644]
include/llvm/ADT/IndexedMap.h [new file with mode: 0644]
include/llvm/CodeGen/SSARegMap.h
include/llvm/Target/MRegisterInfo.h
lib/CodeGen/RegAllocLocal.cpp
lib/CodeGen/VirtRegMap.cpp
lib/CodeGen/VirtRegMap.h

diff --git a/include/Support/DenseMap.h b/include/Support/DenseMap.h
new file mode 100644 (file)
index 0000000..5fcdfae
--- /dev/null
@@ -0,0 +1,61 @@
+//===- DenseMap.h - A dense map implmentation -------------------*- C++ -*-===//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
+//
+// This file implements a dense map. A dense map template takes two
+// types. The first is the mapped type and the second is a functor
+// that maps its argument to a size_t. On instanciation a "null" value
+// can be provided to be used as a "does not exist" indicator in the
+// map. A member function grow() is provided that given the value of
+// the maximally indexed key (the argument of the functor) makes sure
+// the map has enough space for it.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef SUPPORT_DENSEMAP_H
+#define SUPPORT_DENSEMAP_H
+
+#include <vector>
+
+namespace llvm {
+
+template <typename T, typename ToIndexT>
+class DenseMap {
+    typedef typename ToIndexT::argument_type IndexT;
+    typedef std::vector<T> StorageT;
+    StorageT storage_;
+    T nullVal_;
+    ToIndexT toIndex_;
+
+public:
+    DenseMap() { }
+
+    explicit DenseMap(const T& val) : nullVal_(val) { }
+
+    typename StorageT::reference operator[](IndexT n) {
+        assert(toIndex_(n) < storage_.size() && "index out of bounds!");
+        return storage_[toIndex_(n)];
+    }
+
+    typename StorageT::const_reference operator[](IndexT n) const {
+        assert(toIndex_(n) < storage_.size() && "index out of bounds!");
+        return storage_[toIndex_(n)];
+    }
+
+    void clear() {
+        storage_.assign(storage_.size(), nullVal_);
+    }
+
+    void grow(IndexT n) {
+        storage_.resize(toIndex_(n) + 1, nullVal_);
+    }
+};
+
+} // End llvm namespace
+
+#endif
diff --git a/include/llvm/ADT/DenseMap.h b/include/llvm/ADT/DenseMap.h
new file mode 100644 (file)
index 0000000..5fcdfae
--- /dev/null
@@ -0,0 +1,61 @@
+//===- DenseMap.h - A dense map implmentation -------------------*- C++ -*-===//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
+//
+// This file implements a dense map. A dense map template takes two
+// types. The first is the mapped type and the second is a functor
+// that maps its argument to a size_t. On instanciation a "null" value
+// can be provided to be used as a "does not exist" indicator in the
+// map. A member function grow() is provided that given the value of
+// the maximally indexed key (the argument of the functor) makes sure
+// the map has enough space for it.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef SUPPORT_DENSEMAP_H
+#define SUPPORT_DENSEMAP_H
+
+#include <vector>
+
+namespace llvm {
+
+template <typename T, typename ToIndexT>
+class DenseMap {
+    typedef typename ToIndexT::argument_type IndexT;
+    typedef std::vector<T> StorageT;
+    StorageT storage_;
+    T nullVal_;
+    ToIndexT toIndex_;
+
+public:
+    DenseMap() { }
+
+    explicit DenseMap(const T& val) : nullVal_(val) { }
+
+    typename StorageT::reference operator[](IndexT n) {
+        assert(toIndex_(n) < storage_.size() && "index out of bounds!");
+        return storage_[toIndex_(n)];
+    }
+
+    typename StorageT::const_reference operator[](IndexT n) const {
+        assert(toIndex_(n) < storage_.size() && "index out of bounds!");
+        return storage_[toIndex_(n)];
+    }
+
+    void clear() {
+        storage_.assign(storage_.size(), nullVal_);
+    }
+
+    void grow(IndexT n) {
+        storage_.resize(toIndex_(n) + 1, nullVal_);
+    }
+};
+
+} // End llvm namespace
+
+#endif
diff --git a/include/llvm/ADT/IndexedMap.h b/include/llvm/ADT/IndexedMap.h
new file mode 100644 (file)
index 0000000..5fcdfae
--- /dev/null
@@ -0,0 +1,61 @@
+//===- DenseMap.h - A dense map implmentation -------------------*- C++ -*-===//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
+//
+// This file implements a dense map. A dense map template takes two
+// types. The first is the mapped type and the second is a functor
+// that maps its argument to a size_t. On instanciation a "null" value
+// can be provided to be used as a "does not exist" indicator in the
+// map. A member function grow() is provided that given the value of
+// the maximally indexed key (the argument of the functor) makes sure
+// the map has enough space for it.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef SUPPORT_DENSEMAP_H
+#define SUPPORT_DENSEMAP_H
+
+#include <vector>
+
+namespace llvm {
+
+template <typename T, typename ToIndexT>
+class DenseMap {
+    typedef typename ToIndexT::argument_type IndexT;
+    typedef std::vector<T> StorageT;
+    StorageT storage_;
+    T nullVal_;
+    ToIndexT toIndex_;
+
+public:
+    DenseMap() { }
+
+    explicit DenseMap(const T& val) : nullVal_(val) { }
+
+    typename StorageT::reference operator[](IndexT n) {
+        assert(toIndex_(n) < storage_.size() && "index out of bounds!");
+        return storage_[toIndex_(n)];
+    }
+
+    typename StorageT::const_reference operator[](IndexT n) const {
+        assert(toIndex_(n) < storage_.size() && "index out of bounds!");
+        return storage_[toIndex_(n)];
+    }
+
+    void clear() {
+        storage_.assign(storage_.size(), nullVal_);
+    }
+
+    void grow(IndexT n) {
+        storage_.resize(toIndex_(n) + 1, nullVal_);
+    }
+};
+
+} // End llvm namespace
+
+#endif
index 65c0cce732646ac2163c142fb120336129c62165..afed38a334e6224212a7ca423dc67fb1d6cc7f04 100644 (file)
 #define LLVM_CODEGEN_SSAREGMAP_H
 
 #include "llvm/Target/MRegisterInfo.h"
+#include "Support/DenseMap.h"
 
 namespace llvm {
 
 class TargetRegisterClass;
 
 class SSARegMap {
-  std::vector<const TargetRegisterClass*> RegClassMap;
-
-  unsigned rescale(unsigned Reg) {
-    return Reg - MRegisterInfo::FirstVirtualRegister;
-  }
+  DenseMap<const TargetRegisterClass*, VirtReg2IndexFunctor> RegClassMap;
+  unsigned NextRegNum;
 
  public:
+  SSARegMap() : NextRegNum(MRegisterInfo::FirstVirtualRegister) { }
+
   const TargetRegisterClass* getRegClass(unsigned Reg) {
-    unsigned actualReg = rescale(Reg);
-    assert(actualReg < RegClassMap.size() && "Register out of bounds");
-    return RegClassMap[actualReg];
+    return RegClassMap[Reg];
   }
 
   /// createVirtualRegister - Create and return a new virtual register in the
   /// function with the specified register class.
   ///
   unsigned createVirtualRegister(const TargetRegisterClass *RegClass) {
-    RegClassMap.push_back(RegClass);
-    return RegClassMap.size()+MRegisterInfo::FirstVirtualRegister-1;
+    RegClassMap.grow(NextRegNum);
+    RegClassMap[NextRegNum] = RegClass;
+    return NextRegNum++;
   }
 
-  unsigned getNumVirtualRegs() const {
-    return RegClassMap.size();
+  unsigned getLastVirtReg() const {
+    return NextRegNum - 1;
   }
 };
 
index ce39f09ac78e81cd50a0a7408b76c4e9c53057c0..0a83ac2ae3ac044dbae879df48b3655e31e142cb 100644 (file)
@@ -16,8 +16,9 @@
 #ifndef LLVM_TARGET_MREGISTERINFO_H
 #define LLVM_TARGET_MREGISTERINFO_H
 
-#include <cassert>
 #include "llvm/CodeGen/MachineBasicBlock.h"
+#include <cassert>
+#include <functional>
 
 namespace llvm {
 
@@ -319,6 +320,13 @@ public:
                             MachineBasicBlock &MBB) const = 0;
 };
 
+// This is useful when building DenseMap's keyed on virtual registers
+struct VirtReg2IndexFunctor : std::unary_function<unsigned, unsigned> {
+  unsigned operator()(unsigned Reg) const {
+    return Reg - MRegisterInfo::FirstVirtualRegister;
+  }
+};
+
 } // End llvm namespace
 
 #endif
index a3d66391cce4b70e106ebd9b08ae53c6019d52be..f3753326ee4a6ecb788c18283f97e54a8f3cf976 100644 (file)
@@ -23,6 +23,7 @@
 #include "llvm/Target/TargetMachine.h"
 #include "Support/CommandLine.h"
 #include "Support/Debug.h"
+#include "Support/DenseMap.h"
 #include "Support/Statistic.h"
 #include <iostream>
 using namespace llvm;
@@ -43,19 +44,11 @@ namespace {
     std::map<unsigned, int> StackSlotForVirtReg;
 
     // Virt2PhysRegMap - This map contains entries for each virtual register
-    // that is currently available in a physical register.  This is "logically"
-    // a map from virtual register numbers to physical register numbers.
-    // Instead of using a map, however, which is slow, we use a vector.  The
-    // index is the VREG number - FirstVirtualRegister.  If the entry is zero,
-    // then it is logically "not in the map".
-    //
-    std::vector<unsigned> Virt2PhysRegMap;
+    // that is currently available in a physical register.
+    DenseMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap;
 
     unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
-      assert(MRegisterInfo::isVirtualRegister(VirtReg) &&"Illegal VREG #");
-      assert(VirtReg-MRegisterInfo::FirstVirtualRegister <Virt2PhysRegMap.size()
-             && "VirtReg not in map!");
-      return Virt2PhysRegMap[VirtReg-MRegisterInfo::FirstVirtualRegister];
+      return Virt2PhysRegMap[VirtReg];
     }
 
     // PhysRegsUsed - This array is effectively a map, containing entries for
@@ -661,7 +654,8 @@ void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
 
 #ifndef NDEBUG
   bool AllOk = true;
-  for (unsigned i = 0, e = Virt2PhysRegMap.size(); i != e; ++i)
+  for (unsigned i = MRegisterInfo::FirstVirtualRegister,
+           e = MF->getSSARegMap()->getLastVirtReg(); i <= e; ++i)
     if (unsigned PR = Virt2PhysRegMap[i]) {
       std::cerr << "Register still mapped: " << i << " -> " << PR << "\n";
       AllOk = false;
@@ -689,7 +683,8 @@ bool RA::runOnMachineFunction(MachineFunction &Fn) {
 
   // initialize the virtual->physical register map to have a 'null'
   // mapping for all virtual registers
-  Virt2PhysRegMap.assign(MF->getSSARegMap()->getNumVirtualRegs(), 0);
+  Virt2PhysRegMap.clear();
+  Virt2PhysRegMap.grow(MF->getSSARegMap()->getLastVirtReg());
 
   // Loop over all of the basic blocks, eliminating virtual register references
   for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
index bb29ffd9548a26b1541ff5f687c794c638ce56da..4f06c6730fc49a367dcf7544bfeb5da4849b54e7 100644 (file)
@@ -38,12 +38,12 @@ namespace {
 int VirtRegMap::assignVirt2StackSlot(unsigned virtReg)
 {
     assert(MRegisterInfo::isVirtualRegister(virtReg));
-    assert(v2ssMap_[toIndex(virtReg)] == NO_STACK_SLOT &&
+    assert(v2ssMap_[virtReg] == NO_STACK_SLOT &&
            "attempt to assign stack slot to already spilled register");
     const TargetRegisterClass* rc =
         mf_->getSSARegMap()->getRegClass(virtReg);
     int frameIndex = mf_->getFrameInfo()->CreateStackObject(rc);
-    v2ssMap_[toIndex(virtReg)] = frameIndex;
+    v2ssMap_[virtReg] = frameIndex;
     ++numSpills;
     return frameIndex;
 }
@@ -53,14 +53,16 @@ std::ostream& llvm::operator<<(std::ostream& os, const VirtRegMap& vrm)
     const MRegisterInfo* mri = vrm.mf_->getTarget().getRegisterInfo();
 
     std::cerr << "********** REGISTER MAP **********\n";
-    for (unsigned i = 0, e = vrm.v2pMap_.size(); i != e; ++i) {
+    for (unsigned i = MRegisterInfo::FirstVirtualRegister,
+             e = vrm.mf_->getSSARegMap()->getLastVirtReg(); i <= e; ++i) {
         if (vrm.v2pMap_[i] != VirtRegMap::NO_PHYS_REG)
-            std::cerr << "[reg" << VirtRegMap::fromIndex(i) << " -> "
+            std::cerr << "[reg" << i << " -> "
                       << mri->getName(vrm.v2pMap_[i]) << "]\n";
     }
-    for (unsigned i = 0, e = vrm.v2ssMap_.size(); i != e; ++i) {
+    for (unsigned i = MRegisterInfo::FirstVirtualRegister,
+             e = vrm.mf_->getSSARegMap()->getLastVirtReg(); i <= e; ++i) {
         if (vrm.v2ssMap_[i] != VirtRegMap::NO_STACK_SLOT)
-            std::cerr << "[reg" << VirtRegMap::fromIndex(i) << " -> fi#"
+            std::cerr << "[reg" << i << " -> fi#"
                       << vrm.v2ssMap_[i] << "]\n";
     }
     return std::cerr << '\n';
index b5a819f3733cea0f4c0e8c3c1c24367e2064267d..3c991abb62e39104d6d707ec5952357cd754815c 100644 (file)
 
 #include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/SSARegMap.h"
+#include "Support/DenseMap.h"
 #include <climits>
 
 namespace llvm {
 
     class VirtRegMap {
     public:
-        typedef std::vector<unsigned> Virt2PhysMap;
-        typedef std::vector<int> Virt2StackSlotMap;
+        typedef DenseMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
+        typedef DenseMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
 
     private:
         MachineFunction* mf_;
@@ -38,13 +39,6 @@ namespace llvm {
         VirtRegMap(const VirtRegMap& rhs);
         const VirtRegMap& operator=(const VirtRegMap& rhs);
 
-        static unsigned toIndex(unsigned virtReg) {
-            return virtReg - MRegisterInfo::FirstVirtualRegister;
-        }
-        static unsigned fromIndex(unsigned index) {
-            return index + MRegisterInfo::FirstVirtualRegister;
-        }
-
         enum {
             NO_PHYS_REG   = 0,
             NO_STACK_SLOT = INT_MAX
@@ -53,8 +47,10 @@ namespace llvm {
     public:
         VirtRegMap(MachineFunction& mf)
             : mf_(&mf),
-              v2pMap_(mf.getSSARegMap()->getNumVirtualRegs(), NO_PHYS_REG),
-              v2ssMap_(mf.getSSARegMap()->getNumVirtualRegs(), NO_STACK_SLOT) {
+              v2pMap_(NO_PHYS_REG),
+              v2ssMap_(NO_STACK_SLOT) {
+            v2pMap_.grow(mf.getSSARegMap()->getLastVirtReg());
+            v2ssMap_.grow(mf.getSSARegMap()->getLastVirtReg());
         }
 
         bool hasPhys(unsigned virtReg) const {
@@ -63,23 +59,23 @@ namespace llvm {
 
         unsigned getPhys(unsigned virtReg) const {
             assert(MRegisterInfo::isVirtualRegister(virtReg));
-            return v2pMap_[toIndex(virtReg)];
+            return v2pMap_[virtReg];
         }
 
         void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
             assert(MRegisterInfo::isVirtualRegister(virtReg) &&
                    MRegisterInfo::isPhysicalRegister(physReg));
-            assert(v2pMap_[toIndex(virtReg)] == NO_PHYS_REG &&
+            assert(v2pMap_[virtReg] == NO_PHYS_REG &&
                    "attempt to assign physical register to already mapped "
                    "virtual register");
-            v2pMap_[toIndex(virtReg)] = physReg;
+            v2pMap_[virtReg] = physReg;
         }
 
         void clearVirtReg(unsigned virtReg) {
             assert(MRegisterInfo::isVirtualRegister(virtReg));
-            assert(v2pMap_[toIndex(virtReg)] != NO_PHYS_REG &&
+            assert(v2pMap_[virtReg] != NO_PHYS_REG &&
                    "attempt to clear a not assigned virtual register");
-            v2pMap_[toIndex(virtReg)] = NO_PHYS_REG;
+            v2pMap_[virtReg] = NO_PHYS_REG;
         }
 
         bool hasStackSlot(unsigned virtReg) const {
@@ -88,7 +84,7 @@ namespace llvm {
 
         int getStackSlot(unsigned virtReg) const {
             assert(MRegisterInfo::isVirtualRegister(virtReg));
-            return v2ssMap_[toIndex(virtReg)];
+            return v2ssMap_[virtReg];
         }
 
         int assignVirt2StackSlot(unsigned virtReg);