1 //===-- User.cpp - Implement the User class -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/IR/User.h"
11 #include "llvm/IR/Constant.h"
12 #include "llvm/IR/GlobalValue.h"
13 #include "llvm/IR/Operator.h"
18 //===----------------------------------------------------------------------===//
20 //===----------------------------------------------------------------------===//
22 void User::anchor() {}
24 void User::replaceUsesOfWith(Value *From, Value *To) {
25 if (From == To) return; // Duh what?
27 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
28 "Cannot call User::replaceUsesOfWith on a constant!");
30 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
31 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
32 // The side effects of this setOperand call include linking to
33 // "To", adding "this" to the uses list of To, and
34 // most importantly, removing "this" from the use list of "From".
35 setOperand(i, To); // Fix it now...
39 //===----------------------------------------------------------------------===//
40 // User allocHungoffUses Implementation
41 //===----------------------------------------------------------------------===//
43 void User::allocHungoffUses(unsigned N, bool IsPhi) {
44 assert(HasHungOffUses && "alloc must have hung off uses");
46 static_assert(AlignOf<Use>::Alignment >= AlignOf<Use::UserRef>::Alignment,
47 "Alignment is insufficient for 'hung-off-uses' pieces");
48 static_assert(AlignOf<Use::UserRef>::Alignment >=
49 AlignOf<BasicBlock *>::Alignment,
50 "Alignment is insufficient for 'hung-off-uses' pieces");
52 // Allocate the array of Uses, followed by a pointer (with bottom bit set) to
54 size_t size = N * sizeof(Use) + sizeof(Use::UserRef);
56 size += N * sizeof(BasicBlock *);
57 Use *Begin = static_cast<Use*>(::operator new(size));
59 (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
60 setOperandList(Use::initTags(Begin, End));
63 void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
64 assert(HasHungOffUses && "realloc must have hung off uses");
66 unsigned OldNumUses = getNumOperands();
68 // We don't support shrinking the number of uses. We wouldn't have enough
69 // space to copy the old uses in to the new space.
70 assert(NewNumUses > OldNumUses && "realloc must grow num uses");
72 Use *OldOps = getOperandList();
73 allocHungoffUses(NewNumUses, IsPhi);
74 Use *NewOps = getOperandList();
76 // Now copy from the old operands list to the new one.
77 std::copy(OldOps, OldOps + OldNumUses, NewOps);
79 // If this is a Phi, then we need to copy the BB pointers too.
82 reinterpret_cast<char *>(OldOps + OldNumUses) + sizeof(Use::UserRef);
84 reinterpret_cast<char *>(NewOps + NewNumUses) + sizeof(Use::UserRef);
85 std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
87 Use::zap(OldOps, OldOps + OldNumUses, true);
90 //===----------------------------------------------------------------------===//
91 // User operator new Implementations
92 //===----------------------------------------------------------------------===//
94 void *User::operator new(size_t Size, unsigned Us) {
95 assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
96 void *Storage = ::operator new(Size + sizeof(Use) * Us);
97 Use *Start = static_cast<Use*>(Storage);
98 Use *End = Start + Us;
99 User *Obj = reinterpret_cast<User*>(End);
100 Obj->NumUserOperands = Us;
101 Obj->HasHungOffUses = false;
102 Use::initTags(Start, End);
106 void *User::operator new(size_t Size) {
107 // Allocate space for a single Use*
108 void *Storage = ::operator new(Size + sizeof(Use *));
109 Use **HungOffOperandList = static_cast<Use **>(Storage);
110 User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1);
111 Obj->NumUserOperands = 0;
112 Obj->HasHungOffUses = true;
113 *HungOffOperandList = nullptr;
117 //===----------------------------------------------------------------------===//
118 // User operator delete Implementation
119 //===----------------------------------------------------------------------===//
121 void User::operator delete(void *Usr) {
122 // Hung off uses use a single Use* before the User, while other subclasses
123 // use a Use[] allocated prior to the user.
124 User *Obj = static_cast<User *>(Usr);
125 if (Obj->HasHungOffUses) {
126 Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
127 // drop the hung off uses.
128 Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,
130 ::operator delete(HungOffOperandList);
132 Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;
133 Use::zap(Storage, Storage + Obj->NumUserOperands,
135 ::operator delete(Storage);
139 //===----------------------------------------------------------------------===//
141 //===----------------------------------------------------------------------===//
143 Operator::~Operator() {
144 llvm_unreachable("should never destroy an Operator");
147 } // End llvm namespace