template <unsigned>
friend struct HungoffOperandTraits;
virtual void anchor();
-protected:
- /// \brief This is a pointer to the array of Uses for this User.
- ///
- /// For nodes of fixed arity (e.g. a binary operator) this array will live
- /// prefixed to some derived class instance. For nodes of resizable variable
- /// arity (e.g. PHINodes, SwitchInst etc.), this memory will be dynamically
- /// allocated and should be destroyed by the classes' virtual dtor.
- Use *LegacyOperandList;
protected:
/// Allocate a User with an operand pointer co-allocated.
User(Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
: Value(ty, vty) {
- setOperandList(OpList);
assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
NumUserOperands = NumOps;
+ // If we have hung off uses, then the operand list should initially be
+ // null.
+ assert((!HasHungOffUses || !getOperandList()) &&
+ "Error in initializing hung off uses for User");
}
/// \brief Allocate the array of Uses, followed by a pointer
public:
~User() override {
- // drop the hung off uses.
- Use::zap(getOperandList(), getOperandList() + NumUserOperands,
- HasHungOffUses);
- if (HasHungOffUses) {
- setOperandList(nullptr);
- // Reset NumOperands so User::operator delete() does the right thing.
- NumUserOperands = 0;
- }
}
/// \brief Free memory allocated for User and Use objects.
void operator delete(void *Usr);
return OpFrom<Idx>(this);
}
private:
+ Use *&getHungOffOperands() { return *(reinterpret_cast<Use **>(this) - 1); }
+
+ Use *getIntrusiveOperands() {
+ return reinterpret_cast<Use *>(this) - NumUserOperands;
+ }
+
void setOperandList(Use *NewList) {
- LegacyOperandList = NewList;
+ assert(HasHungOffUses &&
+ "Setting operand list only required for hung off uses");
+ getHungOffOperands() = NewList;
}
public:
- Use *getOperandList() const {
- return LegacyOperandList;
+ Use *getOperandList() {
+ return HasHungOffUses ? getHungOffOperands() : getIntrusiveOperands();
+ }
+ const Use *getOperandList() const {
+ return const_cast<User *>(this)->getOperandList();
}
Value *getOperand(unsigned i) const {
assert(i < NumUserOperands && "getOperand() out of range!");
Use *Start = static_cast<Use*>(Storage);
Use *End = Start + Us;
User *Obj = reinterpret_cast<User*>(End);
- Obj->setOperandList(Start);
- Obj->HasHungOffUses = false;
Obj->NumUserOperands = Us;
+ Obj->HasHungOffUses = false;
Use::initTags(Start, End);
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;
+ // Allocate space for a single Use*
+ void *Storage = ::operator new(Size + sizeof(Use *));
+ Use **HungOffOperandList = static_cast<Use **>(Storage);
+ User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1);
Obj->NumUserOperands = 0;
+ Obj->HasHungOffUses = true;
+ *HungOffOperandList = nullptr;
return Obj;
}
//===----------------------------------------------------------------------===//
void User::operator delete(void *Usr) {
- User *Start = static_cast<User*>(Usr);
- Use *Storage = static_cast<Use*>(Usr) - Start->NumUserOperands;
- // If there were hung-off uses, they will have been freed already and
- // NumOperands reset to 0, so here we just free the User itself.
- ::operator delete(Storage);
+ // Hung off uses use a single Use* before the User, while other subclasses
+ // use a Use[] allocated prior to the user.
+ User *Obj = static_cast<User *>(Usr);
+ if (Obj->HasHungOffUses) {
+ Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
+ // drop the hung off uses.
+ Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,
+ /* Delete */ true);
+ ::operator delete(HungOffOperandList);
+ } else {
+ Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;
+ Use::zap(Storage, Storage + Obj->NumUserOperands,
+ /* Delete */ false);
+ ::operator delete(Storage);
+ }
}
//===----------------------------------------------------------------------===//