1 //===-- User.cpp - Implement the User class -------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/IR/User.h"
10 #include "llvm/IR/Constant.h"
11 #include "llvm/IR/GlobalValue.h"
12 #include "llvm/IR/IntrinsicInst.h"
13
14 namespace llvm {
15 class BasicBlock;
16
17 //===----------------------------------------------------------------------===//
18 // User Class
19 //===----------------------------------------------------------------------===//
20
replaceUsesOfWith(Value * From,Value * To)21 void User::replaceUsesOfWith(Value *From, Value *To) {
22 if (From == To) return; // Duh what?
23
24 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
25 "Cannot call User::replaceUsesOfWith on a constant!");
26
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);
33 }
34 }
35
36 //===----------------------------------------------------------------------===//
37 // User allocHungoffUses Implementation
38 //===----------------------------------------------------------------------===//
39
allocHungoffUses(unsigned N,bool IsPhi)40 void User::allocHungoffUses(unsigned N, bool IsPhi) {
41 assert(HasHungOffUses && "alloc must have hung off uses");
42
43 static_assert(alignof(Use) >= alignof(BasicBlock *),
44 "Alignment is insufficient for 'hung-off-uses' pieces");
45
46 // Allocate the array of Uses
47 size_t size = N * sizeof(Use);
48 if (IsPhi)
49 size += N * sizeof(BasicBlock *);
50 Use *Begin = static_cast<Use*>(::operator new(size));
51 Use *End = Begin + N;
52 setOperandList(Begin);
53 for (; Begin != End; Begin++)
54 new (Begin) Use(this);
55 }
56
growHungoffUses(unsigned NewNumUses,bool IsPhi)57 void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
58 assert(HasHungOffUses && "realloc must have hung off uses");
59
60 unsigned OldNumUses = getNumOperands();
61
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");
65
66 Use *OldOps = getOperandList();
67 allocHungoffUses(NewNumUses, IsPhi);
68 Use *NewOps = getOperandList();
69
70 // Now copy from the old operands list to the new one.
71 std::copy(OldOps, OldOps + OldNumUses, NewOps);
72
73 // If this is a Phi, then we need to copy the BB pointers too.
74 if (IsPhi) {
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);
78 }
79 Use::zap(OldOps, OldOps + OldNumUses, true);
80 }
81
82
83 // This is a private struct used by `User` to track the co-allocated descriptor
84 // section.
85 struct DescriptorInfo {
86 intptr_t SizeInBytes;
87 };
88
getDescriptor() const89 ArrayRef<const uint8_t> User::getDescriptor() const {
90 auto MutableARef = const_cast<User *>(this)->getDescriptor();
91 return {MutableARef.begin(), MutableARef.end()};
92 }
93
getDescriptor()94 MutableArrayRef<uint8_t> User::getDescriptor() {
95 assert(HasDescriptor && "Don't call otherwise!");
96 assert(!HasHungOffUses && "Invariant!");
97
98 auto *DI = reinterpret_cast<DescriptorInfo *>(getIntrusiveOperands()) - 1;
99 assert(DI->SizeInBytes != 0 && "Should not have had a descriptor otherwise!");
100
101 return MutableArrayRef<uint8_t>(
102 reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes, DI->SizeInBytes);
103 }
104
isDroppable() const105 bool User::isDroppable() const {
106 if (const auto *Intr = dyn_cast<IntrinsicInst>(this))
107 return Intr->getIntrinsicID() == Intrinsic::assume;
108 return false;
109 }
110
111 //===----------------------------------------------------------------------===//
112 // User operator new Implementations
113 //===----------------------------------------------------------------------===//
114
allocateFixedOperandUser(size_t Size,unsigned Us,unsigned DescBytes)115 void *User::allocateFixedOperandUser(size_t Size, unsigned Us,
116 unsigned DescBytes) {
117 assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
118
119 static_assert(sizeof(DescriptorInfo) % sizeof(void *) == 0, "Required below");
120
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");
125
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);
136
137 if (DescBytes != 0) {
138 auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes);
139 DescInfo->SizeInBytes = DescBytes;
140 }
141
142 return Obj;
143 }
144
operator new(size_t Size,unsigned Us)145 void *User::operator new(size_t Size, unsigned Us) {
146 return allocateFixedOperandUser(Size, Us, 0);
147 }
148
operator new(size_t Size,unsigned Us,unsigned DescBytes)149 void *User::operator new(size_t Size, unsigned Us, unsigned DescBytes) {
150 return allocateFixedOperandUser(Size, Us, DescBytes);
151 }
152
operator new(size_t Size)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;
162 return Obj;
163 }
164
165 //===----------------------------------------------------------------------===//
166 // User operator delete Implementation
167 //===----------------------------------------------------------------------===//
168
169 // Repress memory sanitization, due to use-after-destroy by operator
170 // delete. Bug report 24578 identifies this issue.
operator delete(void * Usr)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!");
177
178 Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
179 // drop the hung off uses.
180 Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,
181 /* Delete */ true);
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);
186
187 auto *DI = reinterpret_cast<DescriptorInfo *>(UseBegin) - 1;
188 uint8_t *Storage = reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes;
189 ::operator delete(Storage);
190 } else {
191 Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;
192 Use::zap(Storage, Storage + Obj->NumUserOperands,
193 /* Delete */ false);
194 ::operator delete(Storage);
195 }
196 }
197
198 } // namespace llvm
199