LLVM 24.0.0git
User.cpp
Go to the documentation of this file.
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/Constants.h"
12#include "llvm/IR/GlobalValue.h"
14
15using namespace llvm;
16
17//===----------------------------------------------------------------------===//
18// User Class
19//===----------------------------------------------------------------------===//
20
21bool User::replaceUsesOfWith(Value *From, Value *To) {
22 bool Changed = false;
23 if (From == To) return Changed; // Duh what?
24
25 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
26 "Cannot call User::replaceUsesOfWith on a constant!");
27
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);
34 Changed = true;
35 }
36
37 return Changed;
38}
39
40//===----------------------------------------------------------------------===//
41// User allocHungoffUses Implementation
42//===----------------------------------------------------------------------===//
43
44void User::allocHungoffUses(unsigned N, bool WithExtraValues) {
45 assert(HasHungOffUses && "alloc must have hung off uses");
46
47 static_assert(alignof(Use) >= alignof(Value *),
48 "Alignment is insufficient for 'hung-off-uses' pieces");
49
50 // Allocate the array of Uses
51 size_t size = N * sizeof(Use);
52 if (WithExtraValues)
53 size += N * sizeof(Value *);
54 Use *Begin = static_cast<Use*>(::operator new(size));
55 Use *End = Begin + N;
56 setOperandList(Begin);
57 for (; Begin != End; Begin++)
58 new (Begin) Use(this);
59}
60
61void User::growHungoffUses(unsigned NewNumUses, bool WithExtraValues) {
62 assert(HasHungOffUses && "realloc must have hung off uses");
63
64 unsigned OldNumUses = getNumOperands();
65
66 // We don't support shrinking the number of uses. We wouldn't have enough
67 // space to copy the old uses in to the new space.
68 assert(NewNumUses > OldNumUses && "realloc must grow num uses");
69
70 Use *OldOps = getOperandList();
71 allocHungoffUses(NewNumUses, WithExtraValues);
72 Use *NewOps = getOperandList();
73
74 // Now copy from the old operands list to the new one.
75 std::copy(OldOps, OldOps + OldNumUses, NewOps);
76
77 // If the User has extra values (phi basic blocks, switch case values), then
78 // we need to copy these, too.
79 if (WithExtraValues) {
80 auto *OldPtr = reinterpret_cast<char *>(OldOps + OldNumUses);
81 auto *NewPtr = reinterpret_cast<char *>(NewOps + NewNumUses);
82 std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(Value *)), NewPtr);
83 }
84 Use::zap(OldOps, OldOps + OldNumUses, true);
85}
86
87// This is a private struct used by `User` to track the co-allocated descriptor
88// section.
89struct DescriptorInfo {
90 intptr_t SizeInBytes;
91};
92
94 auto MutableARef = const_cast<User *>(this)->getDescriptor();
95 return {MutableARef.begin(), MutableARef.end()};
96}
97
99 assert(HasDescriptor && "Don't call otherwise!");
100 assert(!HasHungOffUses && "Invariant!");
101
102 auto *DI = reinterpret_cast<DescriptorInfo *>(getIntrusiveOperands()) - 1;
103 assert(DI->SizeInBytes != 0 && "Should not have had a descriptor otherwise!");
104
106 reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes, DI->SizeInBytes);
107}
108
109bool User::isDroppable() const {
110 if (auto *II = dyn_cast<IntrinsicInst>(this)) {
111 switch (II->getIntrinsicID()) {
112 default:
113 return false;
114 case Intrinsic::assume:
115 case Intrinsic::pseudoprobe:
116 case Intrinsic::experimental_noalias_scope_decl:
117 return true;
118 }
119 }
120 return false;
121}
122
123//===----------------------------------------------------------------------===//
124// User operator new Implementations
125//===----------------------------------------------------------------------===//
126
127void *User::allocateFixedOperandUser(size_t Size, unsigned Us,
128 unsigned DescBytes) {
129 assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
130
131 static_assert(sizeof(DescriptorInfo) % sizeof(void *) == 0, "Required below");
132
133 unsigned DescBytesToAllocate =
134 DescBytes == 0 ? 0 : (DescBytes + sizeof(DescriptorInfo));
135 assert(DescBytesToAllocate % sizeof(void *) == 0 &&
136 "We need this to satisfy alignment constraints for Uses");
137
138 size_t LeadingSize = DescBytesToAllocate + sizeof(Use) * Us;
139
140 // Ensure we allocate at least one pointer's worth of space before the main
141 // user allocation. We use this memory to pass information from the destructor
142 // to the deletion operator, so it can recover the true allocation start.
143 LeadingSize = std::max(LeadingSize, sizeof(void *));
144
145 uint8_t *Storage = static_cast<uint8_t *>(::operator new(LeadingSize + Size));
146 User *Obj = reinterpret_cast<User *>(Storage + LeadingSize);
147 Use *Operands = reinterpret_cast<Use *>(Obj) - Us;
148 Obj->NumUserOperands = Us;
149 Obj->HasHungOffUses = false;
150 Obj->HasDescriptor = DescBytes != 0;
151
152 if (DescBytes != 0) {
153 auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Operands) - 1;
154 DescInfo->SizeInBytes = DescBytes;
155 }
156
157 return Obj;
158}
159
160void *User::operator new(size_t Size, IntrusiveOperandsAllocMarker allocTrait) {
161 return allocateFixedOperandUser(Size, allocTrait.NumOps, 0);
162}
163
164void *User::operator new(size_t Size,
165 IntrusiveOperandsAndDescriptorAllocMarker allocTrait) {
166 return allocateFixedOperandUser(Size, allocTrait.NumOps,
167 allocTrait.DescBytes);
168}
169
170void *User::operator new(size_t Size, HungOffOperandsAllocMarker) {
171 // Allocate space for a single Use*
172 void *Storage = ::operator new(Size + sizeof(Use *));
173 Use **HungOffOperandList = static_cast<Use **>(Storage);
174 User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1);
175 Obj->NumUserOperands = 0;
176 Obj->HasHungOffUses = true;
177 Obj->HasDescriptor = false;
178 *HungOffOperandList = nullptr;
179 return Obj;
180}
181
182//===----------------------------------------------------------------------===//
183// User operator delete Implementation
184//===----------------------------------------------------------------------===//
185
186User::~User() {
187 // Hung off uses use a single Use* before the User, while other subclasses
188 // use a Use[] allocated prior to the user.
189 void *AllocStart = nullptr;
190 if (HasHungOffUses) {
191 assert(!HasDescriptor && "not supported!");
192
193 Use **HungOffOperandList = reinterpret_cast<Use **>(this) - 1;
194 // drop the hung off uses.
195 Use::zap(*HungOffOperandList, *HungOffOperandList + NumUserOperands,
196 /* Delete */ true);
197 AllocStart = HungOffOperandList;
198 } else if (HasDescriptor) {
199 Use *UseBegin = reinterpret_cast<Use *>(this) - NumUserOperands;
200 Use::zap(UseBegin, UseBegin + NumUserOperands, /* Delete */ false);
201
202 auto *DI = reinterpret_cast<DescriptorInfo *>(UseBegin) - 1;
203 AllocStart = reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes;
204 } else if (NumUserOperands > 0) {
205 Use *Storage = reinterpret_cast<Use *>(this) - NumUserOperands;
206 Use::zap(Storage, Storage + NumUserOperands,
207 /* Delete */ false);
208 AllocStart = Storage;
209 } else {
210 // Handle the edge case where there are no operands and no descriptor.
211 AllocStart = (void **)(this) - 1;
212 }
213
214 // Operator delete needs to know where the allocation started. To avoid
215 // use-after-destroy, we have to store the allocation start outside the User
216 // object memory. The `User` new operator always allocates least one pointer
217 // before the User, so we can use that to store the allocation start. As a
218 // special case, we avoid this extra prefix allocation for ConstantData
219 // instances, since those are extremely common.
220 if (!isa<ConstantData>(this))
221 ((void **)this)[-1] = AllocStart;
222}
223
224void User::operator delete(void *Usr) { ::operator delete(((void **)Usr)[-1]); }
225
226void User::operator delete(void *Usr, HungOffOperandsAllocMarker) {
227 Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
228 ::operator delete(HungOffOperandList);
229}
230
231void User::operator delete(void *Usr,
232 IntrusiveOperandsAndDescriptorAllocMarker Marker) {
233 unsigned NumOps = Marker.NumOps;
234 Use *UseBegin = static_cast<Use *>(Usr) - NumOps;
235 auto *DI = reinterpret_cast<DescriptorInfo *>(UseBegin) - 1;
236 uint8_t *Storage = reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes;
237 ::operator delete(Storage);
238}
239
240void User::operator delete(void *Usr, IntrusiveOperandsAllocMarker Marker) {
241 unsigned NumOps = Marker.NumOps;
242 size_t LeadingSize = sizeof(Use) * NumOps;
243 // Handle the edge case where there are no operands and no descriptor.
244 LeadingSize = std::max(LeadingSize, sizeof(void *));
245 uint8_t *Storage = static_cast<uint8_t *>(Usr) - LeadingSize;
246 ::operator delete(Storage);
247}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
uint64_t IntrinsicInst * II
SI Fold Operands
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
static LLVM_ABI void zap(Use *Start, const Use *Stop, bool del=false)
Destroys Use operands when the number of operands of a User changes.
Definition Use.cpp:39
LLVM_ABI void allocHungoffUses(unsigned N, bool WithExtraValues=false)
Allocate the array of Uses, followed by a pointer (with bottom bit set) to the User.
Definition User.cpp:44
LLVM_ABI ArrayRef< const uint8_t > getDescriptor() const
Returns the descriptor co-allocated with this User instance.
Definition User.cpp:93
LLVM_ABI bool isDroppable() const
A droppable user is a user for which uses can be dropped without affecting correctness and should be ...
Definition User.cpp:109
LLVM_ABI bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition User.cpp:21
LLVM_ABI void growHungoffUses(unsigned N, bool WithExtraValues=false)
Grow the number of hung off uses.
Definition User.cpp:61
LLVM_ABI ~User()
Definition User.cpp:186
LLVM_ABI const descriptor::Clause & getDescriptor(Clause C)
This is an optimization pass for GlobalISel generic memory operations.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1685
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
#define N
intptr_t SizeInBytes
Definition User.cpp:90