Added a version of User::new for hung off uses.
authorPete Cooper <peter_cooper@apple.com>
Fri, 12 Jun 2015 17:48:14 +0000 (17:48 +0000)
committerPete Cooper <peter_cooper@apple.com>
Fri, 12 Jun 2015 17:48:14 +0000 (17:48 +0000)
There are now 2 versions of User::new.  The first takes a size_t and is the current
implementation for subclasses which need 0 or more Use's allocated for their operands.

The new version takes no extra arguments to say that this subclass needs 'hung off uses'.
The HungOffUses bool is now set in this version of User::new and we can assert in
allocHungOffUses that we are allowed to have hung off uses.
This ensures we call the correct version of User::new for subclasses which need hung off uses.

A future commit will then allocate space for a single Use* which will be used
in place of User::OperandList once that field has been removed.

Reviewed by Duncan Exon Smith.

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

include/llvm/IR/Instructions.h
include/llvm/IR/User.h
lib/IR/User.cpp

index 00283766c783045bb8bd2a1dadc05e8869867a86..41c3bba19c8e9b317125979e9cd29d22da5072b0 100644 (file)
@@ -2226,7 +2226,7 @@ class PHINode : public Instruction {
   PHINode(const PHINode &PN);
   // allocate space for exactly zero operands
   void *operator new(size_t s) {
-    return User::operator new(s, 0);
+    return User::operator new(s);
   }
   explicit PHINode(Type *Ty, unsigned NumReservedValues,
                    const Twine &NameStr = "",
@@ -2434,7 +2434,7 @@ private:
   void *operator new(size_t, unsigned) = delete;
   // Allocate space for exactly zero operands.
   void *operator new(size_t s) {
-    return User::operator new(s, 0);
+    return User::operator new(s);
   }
   void growOperands(unsigned Size);
   void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
@@ -2708,7 +2708,7 @@ class SwitchInst : public TerminatorInst {
   void growOperands();
   // allocate space for exactly zero operands
   void *operator new(size_t s) {
-    return User::operator new(s, 0);
+    return User::operator new(s);
   }
   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
   /// switch on and a default destination.  The number of additional cases can
@@ -3015,7 +3015,7 @@ class IndirectBrInst : public TerminatorInst {
   void growOperands();
   // allocate space for exactly zero operands
   void *operator new(size_t s) {
-    return User::operator new(s, 0);
+    return User::operator new(s);
   }
   /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
   /// Address to jump to.  The number of expected destinations can be specified
index 3b72cfe1587b45630f269fe0cc31f791a47a893c..743eefb707790914e7f0bea80903be6b6e810636 100644 (file)
@@ -34,7 +34,6 @@ struct OperandTraits;
 
 class User : public Value {
   User(const User &) = delete;
-  void *operator new(size_t) = delete;
   template <unsigned>
   friend struct HungoffOperandTraits;
   virtual void anchor();
@@ -48,7 +47,16 @@ protected:
   Use *LegacyOperandList;
 
 protected:
-  void *operator new(size_t s, unsigned Us);
+  /// Allocate a User with an operand pointer co-allocated.
+  ///
+  /// This is used for subclasses which need to allocate a variable number
+  /// of operands, ie, 'hung off uses'.
+  void *operator new(size_t Size);
+
+  /// Allocate a User with the operands co-allocated.
+  ///
+  /// This is used for subclasses which have a fixed number of operands.
+  void *operator new(size_t Size, unsigned Us);
 
   User(Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
       : Value(ty, vty) {
index 1084a505cfd0352cb389bd2dc283bc1404471b28..5ccf00e1552793d04e52ed5b52e80b885b0f9757 100644 (file)
@@ -41,6 +41,7 @@ void User::replaceUsesOfWith(Value *From, Value *To) {
 //===----------------------------------------------------------------------===//
 
 void User::allocHungoffUses(unsigned N, bool IsPhi) {
+  assert(HasHungOffUses && "alloc must have hung off uses");
   // Allocate the array of Uses, followed by a pointer (with bottom bit set) to
   // the User.
   size_t size = N * sizeof(Use) + sizeof(Use::UserRef);
@@ -50,8 +51,6 @@ void User::allocHungoffUses(unsigned N, bool IsPhi) {
   Use *End = Begin + N;
   (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
   setOperandList(Use::initTags(Begin, End));
-  // Tag this operand list as being a hung off.
-  HasHungOffUses = true;
 }
 
 void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
@@ -85,9 +84,9 @@ void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
 //                         User operator new Implementations
 //===----------------------------------------------------------------------===//
 
-void *User::operator new(size_t s, unsigned Us) {
+void *User::operator new(size_t Size, unsigned Us) {
   assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
-  void *Storage = ::operator new(s + sizeof(Use) * Us);
+  void *Storage = ::operator new(Size + sizeof(Use) * Us);
   Use *Start = static_cast<Use*>(Storage);
   Use *End = Start + Us;
   User *Obj = reinterpret_cast<User*>(End);
@@ -98,6 +97,15 @@ void *User::operator new(size_t s, unsigned Us) {
   return Obj;
 }
 
+void *User::operator new(size_t Size) {
+  void *Storage = ::operator new(Size);
+  User *Obj = reinterpret_cast<User*>(Storage);
+  Obj->setOperandList(nullptr);
+  Obj->HasHungOffUses = true;
+  Obj->NumUserOperands = 0;
+  return Obj;
+}
+
 //===----------------------------------------------------------------------===//
 //                         User operator delete Implementation
 //===----------------------------------------------------------------------===//