LLVM 22.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
14using namespace llvm;
15
16namespace llvm {
17class BasicBlock;
18}
19
20//===----------------------------------------------------------------------===//
21// User Class
22//===----------------------------------------------------------------------===//
23
24bool User::replaceUsesOfWith(Value *From, Value *To) {
25 bool Changed = false;
26 if (From == To) return Changed; // Duh what?
27
28 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
29 "Cannot call User::replaceUsesOfWith on a constant!");
30
31 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
32 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
33 // The side effects of this setOperand call include linking to
34 // "To", adding "this" to the uses list of To, and
35 // most importantly, removing "this" from the use list of "From".
36 setOperand(i, To);
37 Changed = true;
38 }
39 if (auto DVI = dyn_cast_or_null<DbgVariableIntrinsic>(this)) {
40 if (is_contained(DVI->location_ops(), From)) {
41 DVI->replaceVariableLocationOp(From, To);
42 Changed = true;
43 }
44 }
45
46 return Changed;
47}
48
49//===----------------------------------------------------------------------===//
50// User allocHungoffUses Implementation
51//===----------------------------------------------------------------------===//
52
53void User::allocHungoffUses(unsigned N, bool IsPhi) {
54 assert(HasHungOffUses && "alloc must have hung off uses");
55
56 static_assert(alignof(Use) >= alignof(BasicBlock *),
57 "Alignment is insufficient for 'hung-off-uses' pieces");
58
59 // Allocate the array of Uses
60 size_t size = N * sizeof(Use);
61 if (IsPhi)
62 size += N * sizeof(BasicBlock *);
63 Use *Begin = static_cast<Use*>(::operator new(size));
64 Use *End = Begin + N;
65 setOperandList(Begin);
66 for (; Begin != End; Begin++)
67 new (Begin) Use(this);
68}
69
70void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
71 assert(HasHungOffUses && "realloc must have hung off uses");
72
73 unsigned OldNumUses = getNumOperands();
74
75 // We don't support shrinking the number of uses. We wouldn't have enough
76 // space to copy the old uses in to the new space.
77 assert(NewNumUses > OldNumUses && "realloc must grow num uses");
78
79 Use *OldOps = getOperandList();
80 allocHungoffUses(NewNumUses, IsPhi);
81 Use *NewOps = getOperandList();
82
83 // Now copy from the old operands list to the new one.
84 std::copy(OldOps, OldOps + OldNumUses, NewOps);
85
86 // If this is a Phi, then we need to copy the BB pointers too.
87 if (IsPhi) {
88 auto *OldPtr = reinterpret_cast<char *>(OldOps + OldNumUses);
89 auto *NewPtr = reinterpret_cast<char *>(NewOps + NewNumUses);
90 std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
91 }
92 Use::zap(OldOps, OldOps + OldNumUses, true);
93}
94
95
96// This is a private struct used by `User` to track the co-allocated descriptor
97// section.
98struct DescriptorInfo {
99 intptr_t SizeInBytes;
100};
101
103 auto MutableARef = const_cast<User *>(this)->getDescriptor();
104 return {MutableARef.begin(), MutableARef.end()};
105}
106
108 assert(HasDescriptor && "Don't call otherwise!");
109 assert(!HasHungOffUses && "Invariant!");
110
111 auto *DI = reinterpret_cast<DescriptorInfo *>(getIntrusiveOperands()) - 1;
112 assert(DI->SizeInBytes != 0 && "Should not have had a descriptor otherwise!");
113
115 reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes, DI->SizeInBytes);
116}
117
118bool User::isDroppable() const {
119 if (auto *II = dyn_cast<IntrinsicInst>(this)) {
120 switch (II->getIntrinsicID()) {
121 default:
122 return false;
123 case Intrinsic::assume:
124 case Intrinsic::pseudoprobe:
125 case Intrinsic::experimental_noalias_scope_decl:
126 return true;
127 }
128 }
129 return false;
130}
131
132//===----------------------------------------------------------------------===//
133// User operator new Implementations
134//===----------------------------------------------------------------------===//
135
136void *User::allocateFixedOperandUser(size_t Size, unsigned Us,
137 unsigned DescBytes) {
138 assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
139
140 static_assert(sizeof(DescriptorInfo) % sizeof(void *) == 0, "Required below");
141
142 unsigned DescBytesToAllocate =
143 DescBytes == 0 ? 0 : (DescBytes + sizeof(DescriptorInfo));
144 assert(DescBytesToAllocate % sizeof(void *) == 0 &&
145 "We need this to satisfy alignment constraints for Uses");
146
147 uint8_t *Storage = static_cast<uint8_t *>(
148 ::operator new(Size + sizeof(Use) * Us + DescBytesToAllocate));
149 Use *Start = reinterpret_cast<Use *>(Storage + DescBytesToAllocate);
150 Use *End = Start + Us;
151 User *Obj = reinterpret_cast<User *>(End);
152 Obj->NumUserOperands = Us;
153 Obj->HasHungOffUses = false;
154 Obj->HasDescriptor = DescBytes != 0;
155 for (; Start != End; Start++)
156 new (Start) Use(Obj);
157
158 if (DescBytes != 0) {
159 auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes);
160 DescInfo->SizeInBytes = DescBytes;
161 }
162
163 return Obj;
164}
165
166void *User::operator new(size_t Size, IntrusiveOperandsAllocMarker allocTrait) {
167 return allocateFixedOperandUser(Size, allocTrait.NumOps, 0);
168}
169
170void *User::operator new(size_t Size,
171 IntrusiveOperandsAndDescriptorAllocMarker allocTrait) {
172 return allocateFixedOperandUser(Size, allocTrait.NumOps,
173 allocTrait.DescBytes);
174}
175
176void *User::operator new(size_t Size, HungOffOperandsAllocMarker) {
177 // Allocate space for a single Use*
178 void *Storage = ::operator new(Size + sizeof(Use *));
179 Use **HungOffOperandList = static_cast<Use **>(Storage);
180 User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1);
181 Obj->NumUserOperands = 0;
182 Obj->HasHungOffUses = true;
183 Obj->HasDescriptor = false;
184 *HungOffOperandList = nullptr;
185 return Obj;
186}
187
188//===----------------------------------------------------------------------===//
189// User operator delete Implementation
190//===----------------------------------------------------------------------===//
191
192// Repress memory sanitization, due to use-after-destroy by operator
193// delete. Bug report 24578 identifies this issue.
194LLVM_NO_SANITIZE_MEMORY_ATTRIBUTE void User::operator delete(void *Usr) {
195 // Hung off uses use a single Use* before the User, while other subclasses
196 // use a Use[] allocated prior to the user.
197 User *Obj = static_cast<User *>(Usr);
198 if (Obj->HasHungOffUses) {
199 assert(!Obj->HasDescriptor && "not supported!");
200
201 Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
202 // drop the hung off uses.
203 Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,
204 /* Delete */ true);
205 ::operator delete(HungOffOperandList);
206 } else if (Obj->HasDescriptor) {
207 Use *UseBegin = static_cast<Use *>(Usr) - Obj->NumUserOperands;
208 Use::zap(UseBegin, UseBegin + Obj->NumUserOperands, /* Delete */ false);
209
210 auto *DI = reinterpret_cast<DescriptorInfo *>(UseBegin) - 1;
211 uint8_t *Storage = reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes;
212 ::operator delete(Storage);
213 } else {
214 Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;
215 Use::zap(Storage, Storage + Obj->NumUserOperands,
216 /* Delete */ false);
217 ::operator delete(Storage);
218 }
219}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_NO_SANITIZE_MEMORY_ATTRIBUTE
Definition Compiler.h:545
uint64_t IntrinsicInst * II
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
LLVM Basic Block Representation.
Definition BasicBlock.h:62
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:299
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 ArrayRef< const uint8_t > getDescriptor() const
Returns the descriptor co-allocated with this User instance.
Definition User.cpp:102
LLVM_ABI void growHungoffUses(unsigned N, bool IsPhi=false)
Grow the number of hung off uses.
Definition User.cpp:70
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:118
LLVM_ABI 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:53
LLVM_ABI bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition User.cpp:24
unsigned NumUserOperands
Definition Value.h:109
unsigned HasHungOffUses
Definition Value.h:115
unsigned HasDescriptor
Definition Value.h:116
Changed
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:1655
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
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
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1897
#define N
intptr_t SizeInBytes
Definition User.cpp:99