LLVM 20.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/GlobalValue.h"
13
14namespace llvm {
15class BasicBlock;
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 if (auto DVI = dyn_cast_or_null<DbgVariableIntrinsic>(this)) {
37 if (is_contained(DVI->location_ops(), From)) {
38 DVI->replaceVariableLocationOp(From, To);
39 Changed = true;
40 }
41 }
42
43 return Changed;
44}
45
46//===----------------------------------------------------------------------===//
47// User allocHungoffUses Implementation
48//===----------------------------------------------------------------------===//
49
50void User::allocHungoffUses(unsigned N, bool IsPhi) {
51 assert(HasHungOffUses && "alloc must have hung off uses");
52
53 static_assert(alignof(Use) >= alignof(BasicBlock *),
54 "Alignment is insufficient for 'hung-off-uses' pieces");
55
56 // Allocate the array of Uses
57 size_t size = N * sizeof(Use);
58 if (IsPhi)
59 size += N * sizeof(BasicBlock *);
60 Use *Begin = static_cast<Use*>(::operator new(size));
61 Use *End = Begin + N;
62 setOperandList(Begin);
63 for (; Begin != End; Begin++)
64 new (Begin) Use(this);
65}
66
67void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
68 assert(HasHungOffUses && "realloc must have hung off uses");
69
70 unsigned OldNumUses = getNumOperands();
71
72 // We don't support shrinking the number of uses. We wouldn't have enough
73 // space to copy the old uses in to the new space.
74 assert(NewNumUses > OldNumUses && "realloc must grow num uses");
75
76 Use *OldOps = getOperandList();
77 allocHungoffUses(NewNumUses, IsPhi);
78 Use *NewOps = getOperandList();
79
80 // Now copy from the old operands list to the new one.
81 std::copy(OldOps, OldOps + OldNumUses, NewOps);
82
83 // If this is a Phi, then we need to copy the BB pointers too.
84 if (IsPhi) {
85 auto *OldPtr = reinterpret_cast<char *>(OldOps + OldNumUses);
86 auto *NewPtr = reinterpret_cast<char *>(NewOps + NewNumUses);
87 std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
88 }
89 Use::zap(OldOps, OldOps + OldNumUses, true);
90}
91
92
93// This is a private struct used by `User` to track the co-allocated descriptor
94// section.
95struct DescriptorInfo {
96 intptr_t SizeInBytes;
97};
98
99ArrayRef<const uint8_t> User::getDescriptor() const {
100 auto MutableARef = const_cast<User *>(this)->getDescriptor();
101 return {MutableARef.begin(), MutableARef.end()};
102}
103
104MutableArrayRef<uint8_t> User::getDescriptor() {
105 assert(HasDescriptor && "Don't call otherwise!");
106 assert(!HasHungOffUses && "Invariant!");
107
108 auto *DI = reinterpret_cast<DescriptorInfo *>(getIntrusiveOperands()) - 1;
109 assert(DI->SizeInBytes != 0 && "Should not have had a descriptor otherwise!");
110
111 return MutableArrayRef<uint8_t>(
112 reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes, DI->SizeInBytes);
113}
114
115bool User::isDroppable() const {
116 if (auto *II = dyn_cast<IntrinsicInst>(this)) {
117 switch (II->getIntrinsicID()) {
118 default:
119 return false;
120 case Intrinsic::assume:
121 case Intrinsic::pseudoprobe:
122 case Intrinsic::experimental_noalias_scope_decl:
123 return true;
124 }
125 }
126 return false;
127}
128
129//===----------------------------------------------------------------------===//
130// User operator new Implementations
131//===----------------------------------------------------------------------===//
132
133void *User::allocateFixedOperandUser(size_t Size, unsigned Us,
134 unsigned DescBytes) {
135 assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
136
137 static_assert(sizeof(DescriptorInfo) % sizeof(void *) == 0, "Required below");
138
139 unsigned DescBytesToAllocate =
140 DescBytes == 0 ? 0 : (DescBytes + sizeof(DescriptorInfo));
141 assert(DescBytesToAllocate % sizeof(void *) == 0 &&
142 "We need this to satisfy alignment constraints for Uses");
143
144 uint8_t *Storage = static_cast<uint8_t *>(
145 ::operator new(Size + sizeof(Use) * Us + DescBytesToAllocate));
146 Use *Start = reinterpret_cast<Use *>(Storage + DescBytesToAllocate);
147 Use *End = Start + Us;
148 User *Obj = reinterpret_cast<User *>(End);
149 for (; Start != End; Start++)
150 new (Start) Use(Obj);
151
152 if (DescBytes != 0) {
153 auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes);
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 *HungOffOperandList = nullptr;
176 return Obj;
177}
178
179//===----------------------------------------------------------------------===//
180// User operator delete Implementation
181//===----------------------------------------------------------------------===//
182
183// Repress memory sanitization, due to use-after-destroy by operator
184// delete. Bug report 24578 identifies this issue.
185LLVM_NO_SANITIZE_MEMORY_ATTRIBUTE void User::operator delete(void *Usr) {
186 // Hung off uses use a single Use* before the User, while other subclasses
187 // use a Use[] allocated prior to the user.
188 User *Obj = static_cast<User *>(Usr);
189 if (Obj->HasHungOffUses) {
190 assert(!Obj->HasDescriptor && "not supported!");
191
192 Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
193 // drop the hung off uses.
194 Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,
195 /* Delete */ true);
196 ::operator delete(HungOffOperandList);
197 } else if (Obj->HasDescriptor) {
198 Use *UseBegin = static_cast<Use *>(Usr) - Obj->NumUserOperands;
199 Use::zap(UseBegin, UseBegin + Obj->NumUserOperands, /* Delete */ false);
200
201 auto *DI = reinterpret_cast<DescriptorInfo *>(UseBegin) - 1;
202 uint8_t *Storage = reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes;
203 ::operator delete(Storage);
204 } else {
205 Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;
206 Use::zap(Storage, Storage + Obj->NumUserOperands,
207 /* Delete */ false);
208 ::operator delete(Storage);
209 }
210}
211
212} // namespace llvm
BlockVerifier::State From
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_NO_SANITIZE_MEMORY_ATTRIBUTE
Definition: Compiler.h:529
uint64_t Size
bool End
Definition: ELF_riscv.cpp:480
uint64_t IntrinsicInst * II
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static 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:35
const Use * getOperandList() const
Definition: User.h:221
ArrayRef< const uint8_t > getDescriptor() const
Returns the descriptor co-allocated with this User instance.
Definition: User.cpp:99
bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition: User.cpp:21
void allocHungoffUses(unsigned N, bool IsPhi=false)
Allocate the array of Uses, followed by a pointer (with bottom bit set) to the User.
Definition: User.cpp:50
User(Type *ty, unsigned vty, AllocInfo AllocInfo)
Definition: User.h:115
void setOperand(unsigned i, Value *Val)
Definition: User.h:233
Value * getOperand(unsigned i) const
Definition: User.h:228
unsigned getNumOperands() const
Definition: User.h:250
bool isDroppable() const
A droppable user is a user for which uses can be dropped without affecting correctness and should be ...
Definition: User.cpp:115
void growHungoffUses(unsigned N, bool IsPhi=false)
Grow the number of hung off uses.
Definition: User.cpp:67
@ NumUserOperandsBits
Definition: Value.h:107
unsigned HasHungOffUses
Definition: Value.h:114
unsigned HasDescriptor
Definition: Value.h:115
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
@ User
could "use" a pointer
NodeAddr< UseNode * > Use
Definition: RDFGraph.h:385
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:1697
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1903
#define N
intptr_t SizeInBytes
Definition: User.cpp:96