Subzero: Use CFG-local arena allocation for relevant containers.

In particular, node lists for in and out edges of a CfgNode, and the live range segment list in a Variable.  This is done by making the Cfg allocator globally available through TLS, and providing the STL containers with an allocator struct that uses this.

This also cleans up some other allocation-related issues:

* The allocator is now hung off the Cfg via a pointer, rather than being embedded into the Cfg.  This allows a const Cfg pointer to be stored in TLS while still allowing its allocator to be mutated.

* Cfg is now created via a static create() method.

* The redundant Cfg::allocateInst<> methods are removed.

* The Variable::asType() method allocates a whole new Variable from the Cfg arena, rather than allocating it on the stack, removing the need for the move constructor in Variable and Operand.  This is OK since asType() is only used for textual asm emission.

* The same 1MB arena allocator is now used by the assembler as well.  The fact that it wasn't changed over to be the same as Cfg and GlobalContext was an oversight.  (It turns out this adds ~3MB to the translator memory footprint, so that could be tuned later.)

BUG= none
R=jfb@chromium.org, jvoung@chromium.org

Review URL: https://codereview.chromium.org/802183004
diff --git a/src/IceCfg.h b/src/IceCfg.h
index 1768761..f037709 100644
--- a/src/IceCfg.h
+++ b/src/IceCfg.h
@@ -17,8 +17,6 @@
 
 #include <memory>
 
-#include "llvm/Support/Allocator.h"
-
 #include "assembler.h"
 #include "IceClFlags.h"
 #include "IceDefs.h"
@@ -32,9 +30,24 @@
   Cfg &operator=(const Cfg &) = delete;
 
 public:
-  Cfg(GlobalContext *Ctx);
   ~Cfg();
 
+  // TODO(stichnot): Change this to return unique_ptr<Cfg>, and plumb
+  // it through the callers, to make ownership and lifetime and
+  // destruction requirements more explicit.
+  static Cfg *create(GlobalContext *Ctx) {
+    Cfg *Func = new Cfg(Ctx);
+    CurrentCfg = Func;
+    return Func;
+  }
+  // Gets a pointer to the current thread's Cfg.
+  static const Cfg *getCurrentCfg() { return CurrentCfg; }
+  // Gets a pointer to the current thread's Cfg's allocator.
+  static ArenaAllocator *getCurrentCfgAllocator() {
+    assert(CurrentCfg);
+    return CurrentCfg->Allocator.get();
+  }
+
   GlobalContext *getContext() const { return Ctx; }
 
   // Manage the name and return type of the function being translated.
@@ -150,38 +163,25 @@
   void dump(const IceString &Message = "");
 
   // Allocate data of type T using the per-Cfg allocator.
-  template <typename T> T *allocate() { return Allocator.Allocate<T>(); }
-
-  // Allocate an instruction of type T using the per-Cfg instruction allocator.
-  template <typename T> T *allocateInst() { return Allocator.Allocate<T>(); }
+  template <typename T> T *allocate() { return Allocator->Allocate<T>(); }
 
   // Allocate an array of data of type T using the per-Cfg allocator.
   template <typename T> T *allocateArrayOf(size_t NumElems) {
-    return Allocator.Allocate<T>(NumElems);
+    return Allocator->Allocate<T>(NumElems);
   }
 
   // Deallocate data that was allocated via allocate<T>().
   template <typename T> void deallocate(T *Object) {
-    Allocator.Deallocate(Object);
-  }
-
-  // Deallocate data that was allocated via allocateInst<T>().
-  template <typename T> void deallocateInst(T *Instr) {
-    Allocator.Deallocate(Instr);
+    Allocator->Deallocate(Object);
   }
 
   // Deallocate data that was allocated via allocateArrayOf<T>().
   template <typename T> void deallocateArrayOf(T *Array) {
-    Allocator.Deallocate(Array);
+    Allocator->Deallocate(Array);
   }
 
 private:
-  // TODO: for now, everything is allocated from the same allocator. In the
-  // future we may want to split this to several allocators, for example in
-  // order to use a "Recycler" to preserve memory. If we keep all allocation
-  // requests from the Cfg exposed via methods, we can always switch the
-  // implementation over at a later point.
-  llvm::BumpPtrAllocatorImpl<llvm::MallocAllocator, 1024 * 1024> Allocator;
+  Cfg(GlobalContext *Ctx);
 
   GlobalContext *Ctx;
   IceString FunctionName;
@@ -197,6 +197,7 @@
   VarList Variables;
   VarList Args; // subset of Variables, in argument order
   VarList ImplicitArgs; // subset of Variables
+  std::unique_ptr<ArenaAllocator> Allocator;
   std::unique_ptr<Liveness> Live;
   std::unique_ptr<TargetLowering> Target;
   std::unique_ptr<VariablesMetadata> VMetadata;
@@ -208,6 +209,11 @@
   // register allocation, resetCurrentNode() should be called to avoid
   // spurious validation failures.
   const CfgNode *CurrentNode;
+
+  // Maintain a pointer in TLS to the current Cfg being translated.
+  // This is primarily for accessing its allocator statelessly, but
+  // other uses are possible.
+  thread_local static const Cfg *CurrentCfg;
 };
 
 } // end of namespace Ice