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::replaceUsesOfWith(Value *From, Value *To) {
23 if (From == To) return; // Duh what?
25 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
26 "Cannot call User::replaceUsesOfWith on a constant!");
28 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
29 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
30 // The side effects of this setOperand call include linking to
31 // "To", adding "this" to the uses list of To, and
32 // most importantly, removing "this" from the use list of "From".
33 setOperand(i, To); // Fix it now...
37 //===----------------------------------------------------------------------===//
38 // User allocHungoffUses Implementation
39 //===----------------------------------------------------------------------===//
41 void User::allocHungoffUses(unsigned N, bool IsPhi) {
42 assert(HasHungOffUses && "alloc must have hung off uses");
44 static_assert(alignof(Use) >= alignof(Use::UserRef),
45 "Alignment is insufficient for 'hung-off-uses' pieces");
46 static_assert(alignof(Use::UserRef) >= alignof(BasicBlock *),
47 "Alignment is insufficient for 'hung-off-uses' pieces");
49 // Allocate the array of Uses, followed by a pointer (with bottom bit set) to
51 size_t size = N * sizeof(Use) + sizeof(Use::UserRef);
53 size += N * sizeof(BasicBlock *);
54 Use *Begin = static_cast<Use*>(::operator new(size));
56 (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
57 setOperandList(Use::initTags(Begin, End));
60 void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
61 assert(HasHungOffUses && "realloc must have hung off uses");
63 unsigned OldNumUses = getNumOperands();
65 // We don't support shrinking the number of uses. We wouldn't have enough
66 // space to copy the old uses in to the new space.
67 assert(NewNumUses > OldNumUses && "realloc must grow num uses");
69 Use *OldOps = getOperandList();
70 allocHungoffUses(NewNumUses, IsPhi);
71 Use *NewOps = getOperandList();
73 // Now copy from the old operands list to the new one.
74 std::copy(OldOps, OldOps + OldNumUses, NewOps);
76 // If this is a Phi, then we need to copy the BB pointers too.
79 reinterpret_cast<char *>(OldOps + OldNumUses) + sizeof(Use::UserRef);
81 reinterpret_cast<char *>(NewOps + NewNumUses) + sizeof(Use::UserRef);
82 std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
84 Use::zap(OldOps, OldOps + OldNumUses, true);
88 // This is a private struct used by `User` to track the co-allocated descriptor
90 struct DescriptorInfo {
94 ArrayRef<const uint8_t> User::getDescriptor() const {
95 auto MutableARef = const_cast<User *>(this)->getDescriptor();
96 return {MutableARef.begin(), MutableARef.end()};
99 MutableArrayRef<uint8_t> User::getDescriptor() {
100 assert(HasDescriptor && "Don't call otherwise!");
101 assert(!HasHungOffUses && "Invariant!");
103 auto *DI = reinterpret_cast<DescriptorInfo *>(getIntrusiveOperands()) - 1;
104 assert(DI->SizeInBytes != 0 && "Should not have had a descriptor otherwise!");
106 return MutableArrayRef<uint8_t>(
107 reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes, DI->SizeInBytes);
110 //===----------------------------------------------------------------------===//
111 // User operator new Implementations
112 //===----------------------------------------------------------------------===//
114 void *User::allocateFixedOperandUser(size_t Size, unsigned Us,
115 unsigned DescBytes) {
116 assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
118 static_assert(sizeof(DescriptorInfo) % sizeof(void *) == 0, "Required below");
120 unsigned DescBytesToAllocate =
121 DescBytes == 0 ? 0 : (DescBytes + sizeof(DescriptorInfo));
122 assert(DescBytesToAllocate % sizeof(void *) == 0 &&
123 "We need this to satisfy alignment constraints for Uses");
125 uint8_t *Storage = static_cast<uint8_t *>(
126 ::operator new(Size + sizeof(Use) * Us + DescBytesToAllocate));
127 Use *Start = reinterpret_cast<Use *>(Storage + DescBytesToAllocate);
128 Use *End = Start + Us;
129 User *Obj = reinterpret_cast<User*>(End);
130 Obj->NumUserOperands = Us;
131 Obj->HasHungOffUses = false;
132 Obj->HasDescriptor = DescBytes != 0;
133 Use::initTags(Start, End);
135 if (DescBytes != 0) {
136 auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes);
137 DescInfo->SizeInBytes = DescBytes;
143 void *User::operator new(size_t Size, unsigned Us) {
144 return allocateFixedOperandUser(Size, Us, 0);
147 void *User::operator new(size_t Size, unsigned Us, unsigned DescBytes) {
148 return allocateFixedOperandUser(Size, Us, DescBytes);
151 void *User::operator new(size_t Size) {
152 // Allocate space for a single Use*
153 void *Storage = ::operator new(Size + sizeof(Use *));
154 Use **HungOffOperandList = static_cast<Use **>(Storage);
155 User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1);
156 Obj->NumUserOperands = 0;
157 Obj->HasHungOffUses = true;
158 Obj->HasDescriptor = false;
159 *HungOffOperandList = nullptr;
163 //===----------------------------------------------------------------------===//
164 // User operator delete Implementation
165 //===----------------------------------------------------------------------===//
167 void User::operator delete(void *Usr) {
168 // Hung off uses use a single Use* before the User, while other subclasses
169 // use a Use[] allocated prior to the user.
170 User *Obj = static_cast<User *>(Usr);
171 if (Obj->HasHungOffUses) {
172 assert(!Obj->HasDescriptor && "not supported!");
174 Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
175 // drop the hung off uses.
176 Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,
178 ::operator delete(HungOffOperandList);
179 } else if (Obj->HasDescriptor) {
180 Use *UseBegin = static_cast<Use *>(Usr) - Obj->NumUserOperands;
181 Use::zap(UseBegin, UseBegin + Obj->NumUserOperands, /* Delete */ false);
183 auto *DI = reinterpret_cast<DescriptorInfo *>(UseBegin) - 1;
184 uint8_t *Storage = reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes;
185 ::operator delete(Storage);
187 Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;
188 Use::zap(Storage, Storage + Obj->NumUserOperands,
190 ::operator delete(Storage);
194 } // End llvm namespace