xref: /llvm-project-15.0.7/llvm/lib/IR/User.cpp (revision eaae52c1)
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 
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   if (auto DVI = dyn_cast_or_null<DbgVariableIntrinsic>(this)) {
35     if (is_contained(DVI->location_ops(), From))
36       DVI->replaceVariableLocationOp(From, To);
37   }
38 }
39 
40 //===----------------------------------------------------------------------===//
41 //                         User allocHungoffUses Implementation
42 //===----------------------------------------------------------------------===//
43 
44 void User::allocHungoffUses(unsigned N, bool IsPhi) {
45   assert(HasHungOffUses && "alloc must have hung off uses");
46 
47   static_assert(alignof(Use) >= alignof(BasicBlock *),
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 (IsPhi)
53     size += N * sizeof(BasicBlock *);
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 
61 void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
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, IsPhi);
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 this is a Phi, then we need to copy the BB pointers too.
78   if (IsPhi) {
79     auto *OldPtr = reinterpret_cast<char *>(OldOps + OldNumUses);
80     auto *NewPtr = reinterpret_cast<char *>(NewOps + NewNumUses);
81     std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
82   }
83   Use::zap(OldOps, OldOps + OldNumUses, true);
84 }
85 
86 
87 // This is a private struct used by `User` to track the co-allocated descriptor
88 // section.
89 struct DescriptorInfo {
90   intptr_t SizeInBytes;
91 };
92 
93 ArrayRef<const uint8_t> User::getDescriptor() const {
94   auto MutableARef = const_cast<User *>(this)->getDescriptor();
95   return {MutableARef.begin(), MutableARef.end()};
96 }
97 
98 MutableArrayRef<uint8_t> User::getDescriptor() {
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 
105   return MutableArrayRef<uint8_t>(
106       reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes, DI->SizeInBytes);
107 }
108 
109 bool User::isDroppable() const {
110   if (const auto *Intr = dyn_cast<IntrinsicInst>(this))
111     return Intr->getIntrinsicID() == Intrinsic::assume;
112   return false;
113 }
114 
115 //===----------------------------------------------------------------------===//
116 //                         User operator new Implementations
117 //===----------------------------------------------------------------------===//
118 
119 void *User::allocateFixedOperandUser(size_t Size, unsigned Us,
120                                      unsigned DescBytes) {
121   assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
122 
123   static_assert(sizeof(DescriptorInfo) % sizeof(void *) == 0, "Required below");
124 
125   unsigned DescBytesToAllocate =
126       DescBytes == 0 ? 0 : (DescBytes + sizeof(DescriptorInfo));
127   assert(DescBytesToAllocate % sizeof(void *) == 0 &&
128          "We need this to satisfy alignment constraints for Uses");
129 
130   uint8_t *Storage = static_cast<uint8_t *>(
131       ::operator new(Size + sizeof(Use) * Us + DescBytesToAllocate));
132   Use *Start = reinterpret_cast<Use *>(Storage + DescBytesToAllocate);
133   Use *End = Start + Us;
134   User *Obj = reinterpret_cast<User*>(End);
135   Obj->NumUserOperands = Us;
136   Obj->HasHungOffUses = false;
137   Obj->HasDescriptor = DescBytes != 0;
138   for (; Start != End; Start++)
139     new (Start) Use(Obj);
140 
141   if (DescBytes != 0) {
142     auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes);
143     DescInfo->SizeInBytes = DescBytes;
144   }
145 
146   return Obj;
147 }
148 
149 void *User::operator new(size_t Size, unsigned Us) {
150   return allocateFixedOperandUser(Size, Us, 0);
151 }
152 
153 void *User::operator new(size_t Size, unsigned Us, unsigned DescBytes) {
154   return allocateFixedOperandUser(Size, Us, DescBytes);
155 }
156 
157 void *User::operator new(size_t Size) {
158   // Allocate space for a single Use*
159   void *Storage = ::operator new(Size + sizeof(Use *));
160   Use **HungOffOperandList = static_cast<Use **>(Storage);
161   User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1);
162   Obj->NumUserOperands = 0;
163   Obj->HasHungOffUses = true;
164   Obj->HasDescriptor = false;
165   *HungOffOperandList = nullptr;
166   return Obj;
167 }
168 
169 //===----------------------------------------------------------------------===//
170 //                         User operator delete Implementation
171 //===----------------------------------------------------------------------===//
172 
173 // Repress memory sanitization, due to use-after-destroy by operator
174 // delete. Bug report 24578 identifies this issue.
175 LLVM_NO_SANITIZE_MEMORY_ATTRIBUTE void User::operator delete(void *Usr) {
176   // Hung off uses use a single Use* before the User, while other subclasses
177   // use a Use[] allocated prior to the user.
178   User *Obj = static_cast<User *>(Usr);
179   if (Obj->HasHungOffUses) {
180     assert(!Obj->HasDescriptor && "not supported!");
181 
182     Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
183     // drop the hung off uses.
184     Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,
185              /* Delete */ true);
186     ::operator delete(HungOffOperandList);
187   } else if (Obj->HasDescriptor) {
188     Use *UseBegin = static_cast<Use *>(Usr) - Obj->NumUserOperands;
189     Use::zap(UseBegin, UseBegin + Obj->NumUserOperands, /* Delete */ false);
190 
191     auto *DI = reinterpret_cast<DescriptorInfo *>(UseBegin) - 1;
192     uint8_t *Storage = reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes;
193     ::operator delete(Storage);
194   } else {
195     Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;
196     Use::zap(Storage, Storage + Obj->NumUserOperands,
197              /* Delete */ false);
198     ::operator delete(Storage);
199   }
200 }
201 
202 } // namespace llvm
203