1 //===-- User.cpp - Implement the User class -------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/User.h"
10 #include "llvm/IR/Constant.h"
11 #include "llvm/IR/GlobalValue.h"
12 #include "llvm/IR/IntrinsicInst.h"
17 //===----------------------------------------------------------------------===//
19 //===----------------------------------------------------------------------===//
21 void User::replaceUsesOfWith(Value *From, Value *To) {
22 if (From == To) return; // Duh what?
24 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
25 "Cannot call User::replaceUsesOfWith on a constant!");
27 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
28 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
29 // The side effects of this setOperand call include linking to
30 // "To", adding "this" to the uses list of To, and
31 // most importantly, removing "this" from the use list of "From".
32 setOperand(i, To); // Fix it now...
36 //===----------------------------------------------------------------------===//
37 // User allocHungoffUses Implementation
38 //===----------------------------------------------------------------------===//
40 void User::allocHungoffUses(unsigned N, bool IsPhi) {
41 assert(HasHungOffUses && "alloc must have hung off uses");
43 static_assert(alignof(Use) >= alignof(BasicBlock *),
44 "Alignment is insufficient for 'hung-off-uses' pieces");
46 // Allocate the array of Uses
47 size_t size = N * sizeof(Use);
49 size += N * sizeof(BasicBlock *);
50 Use *Begin = static_cast<Use*>(::operator new(size));
52 setOperandList(Begin);
53 for (; Begin != End; Begin++)
54 new (Begin) Use(this);
57 void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
58 assert(HasHungOffUses && "realloc must have hung off uses");
60 unsigned OldNumUses = getNumOperands();
62 // We don't support shrinking the number of uses. We wouldn't have enough
63 // space to copy the old uses in to the new space.
64 assert(NewNumUses > OldNumUses && "realloc must grow num uses");
66 Use *OldOps = getOperandList();
67 allocHungoffUses(NewNumUses, IsPhi);
68 Use *NewOps = getOperandList();
70 // Now copy from the old operands list to the new one.
71 std::copy(OldOps, OldOps + OldNumUses, NewOps);
73 // If this is a Phi, then we need to copy the BB pointers too.
75 auto *OldPtr = reinterpret_cast<char *>(OldOps + OldNumUses);
76 auto *NewPtr = reinterpret_cast<char *>(NewOps + NewNumUses);
77 std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
79 Use::zap(OldOps, OldOps + OldNumUses, true);
83 // This is a private struct used by `User` to track the co-allocated descriptor
85 struct DescriptorInfo {
89 ArrayRef<const uint8_t> User::getDescriptor() const {
90 auto MutableARef = const_cast<User *>(this)->getDescriptor();
91 return {MutableARef.begin(), MutableARef.end()};
94 MutableArrayRef<uint8_t> User::getDescriptor() {
95 assert(HasDescriptor && "Don't call otherwise!");
96 assert(!HasHungOffUses && "Invariant!");
98 auto *DI = reinterpret_cast<DescriptorInfo *>(getIntrusiveOperands()) - 1;
99 assert(DI->SizeInBytes != 0 && "Should not have had a descriptor otherwise!");
101 return MutableArrayRef<uint8_t>(
102 reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes, DI->SizeInBytes);
105 bool User::isDroppable() const {
106 if (const auto *Intr = dyn_cast<IntrinsicInst>(this))
107 return Intr->getIntrinsicID() == Intrinsic::assume;
111 //===----------------------------------------------------------------------===//
112 // User operator new Implementations
113 //===----------------------------------------------------------------------===//
115 void *User::allocateFixedOperandUser(size_t Size, unsigned Us,
116 unsigned DescBytes) {
117 assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
119 static_assert(sizeof(DescriptorInfo) % sizeof(void *) == 0, "Required below");
121 unsigned DescBytesToAllocate =
122 DescBytes == 0 ? 0 : (DescBytes + sizeof(DescriptorInfo));
123 assert(DescBytesToAllocate % sizeof(void *) == 0 &&
124 "We need this to satisfy alignment constraints for Uses");
126 uint8_t *Storage = static_cast<uint8_t *>(
127 ::operator new(Size + sizeof(Use) * Us + DescBytesToAllocate));
128 Use *Start = reinterpret_cast<Use *>(Storage + DescBytesToAllocate);
129 Use *End = Start + Us;
130 User *Obj = reinterpret_cast<User*>(End);
131 Obj->NumUserOperands = Us;
132 Obj->HasHungOffUses = false;
133 Obj->HasDescriptor = DescBytes != 0;
134 for (; Start != End; Start++)
135 new (Start) Use(Obj);
137 if (DescBytes != 0) {
138 auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes);
139 DescInfo->SizeInBytes = DescBytes;
145 void *User::operator new(size_t Size, unsigned Us) {
146 return allocateFixedOperandUser(Size, Us, 0);
149 void *User::operator new(size_t Size, unsigned Us, unsigned DescBytes) {
150 return allocateFixedOperandUser(Size, Us, DescBytes);
153 void *User::operator new(size_t Size) {
154 // Allocate space for a single Use*
155 void *Storage = ::operator new(Size + sizeof(Use *));
156 Use **HungOffOperandList = static_cast<Use **>(Storage);
157 User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1);
158 Obj->NumUserOperands = 0;
159 Obj->HasHungOffUses = true;
160 Obj->HasDescriptor = false;
161 *HungOffOperandList = nullptr;
165 //===----------------------------------------------------------------------===//
166 // User operator delete Implementation
167 //===----------------------------------------------------------------------===//
169 // Repress memory sanitization, due to use-after-destroy by operator
170 // delete. Bug report 24578 identifies this issue.
171 LLVM_NO_SANITIZE_MEMORY_ATTRIBUTE void User::operator delete(void *Usr) {
172 // Hung off uses use a single Use* before the User, while other subclasses
173 // use a Use[] allocated prior to the user.
174 User *Obj = static_cast<User *>(Usr);
175 if (Obj->HasHungOffUses) {
176 assert(!Obj->HasDescriptor && "not supported!");
178 Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
179 // drop the hung off uses.
180 Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,
182 ::operator delete(HungOffOperandList);
183 } else if (Obj->HasDescriptor) {
184 Use *UseBegin = static_cast<Use *>(Usr) - Obj->NumUserOperands;
185 Use::zap(UseBegin, UseBegin + Obj->NumUserOperands, /* Delete */ false);
187 auto *DI = reinterpret_cast<DescriptorInfo *>(UseBegin) - 1;
188 uint8_t *Storage = reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes;
189 ::operator delete(Storage);
191 Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;
192 Use::zap(Storage, Storage + Obj->NumUserOperands,
194 ::operator delete(Storage);