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 
13 namespace llvm {
14 class BasicBlock;
15 
16 //===----------------------------------------------------------------------===//
17 //                                 User Class
18 //===----------------------------------------------------------------------===//
19 
20 void User::replaceUsesOfWith(Value *From, Value *To) {
21   if (From == To) return;   // Duh what?
22 
23   assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
24          "Cannot call User::replaceUsesOfWith on a constant!");
25 
26   for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
27     if (getOperand(i) == From) {  // Is This operand is pointing to oldval?
28       // The side effects of this setOperand call include linking to
29       // "To", adding "this" to the uses list of To, and
30       // most importantly, removing "this" from the use list of "From".
31       setOperand(i, To); // Fix it now...
32     }
33 }
34 
35 //===----------------------------------------------------------------------===//
36 //                         User allocHungoffUses Implementation
37 //===----------------------------------------------------------------------===//
38 
39 void User::allocHungoffUses(unsigned N, bool IsPhi) {
40   assert(HasHungOffUses && "alloc must have hung off uses");
41 
42   static_assert(alignof(Use) >= alignof(Use::UserRef),
43                 "Alignment is insufficient for 'hung-off-uses' pieces");
44   static_assert(alignof(Use::UserRef) >= alignof(BasicBlock *),
45                 "Alignment is insufficient for 'hung-off-uses' pieces");
46 
47   // Allocate the array of Uses, followed by a pointer (with bottom bit set) to
48   // the User.
49   size_t size = N * sizeof(Use) + sizeof(Use::UserRef);
50   if (IsPhi)
51     size += N * sizeof(BasicBlock *);
52   Use *Begin = static_cast<Use*>(::operator new(size));
53   Use *End = Begin + N;
54   (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
55   setOperandList(Use::initTags(Begin, End));
56 }
57 
58 void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
59   assert(HasHungOffUses && "realloc must have hung off uses");
60 
61   unsigned OldNumUses = getNumOperands();
62 
63   // We don't support shrinking the number of uses.  We wouldn't have enough
64   // space to copy the old uses in to the new space.
65   assert(NewNumUses > OldNumUses && "realloc must grow num uses");
66 
67   Use *OldOps = getOperandList();
68   allocHungoffUses(NewNumUses, IsPhi);
69   Use *NewOps = getOperandList();
70 
71   // Now copy from the old operands list to the new one.
72   std::copy(OldOps, OldOps + OldNumUses, NewOps);
73 
74   // If this is a Phi, then we need to copy the BB pointers too.
75   if (IsPhi) {
76     auto *OldPtr =
77         reinterpret_cast<char *>(OldOps + OldNumUses) + sizeof(Use::UserRef);
78     auto *NewPtr =
79         reinterpret_cast<char *>(NewOps + NewNumUses) + sizeof(Use::UserRef);
80     std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
81   }
82   Use::zap(OldOps, OldOps + OldNumUses, true);
83 }
84 
85 
86 // This is a private struct used by `User` to track the co-allocated descriptor
87 // section.
88 struct DescriptorInfo {
89   intptr_t SizeInBytes;
90 };
91 
92 ArrayRef<const uint8_t> User::getDescriptor() const {
93   auto MutableARef = const_cast<User *>(this)->getDescriptor();
94   return {MutableARef.begin(), MutableARef.end()};
95 }
96 
97 MutableArrayRef<uint8_t> User::getDescriptor() {
98   assert(HasDescriptor && "Don't call otherwise!");
99   assert(!HasHungOffUses && "Invariant!");
100 
101   auto *DI = reinterpret_cast<DescriptorInfo *>(getIntrusiveOperands()) - 1;
102   assert(DI->SizeInBytes != 0 && "Should not have had a descriptor otherwise!");
103 
104   return MutableArrayRef<uint8_t>(
105       reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes, DI->SizeInBytes);
106 }
107 
108 //===----------------------------------------------------------------------===//
109 //                         User operator new Implementations
110 //===----------------------------------------------------------------------===//
111 
112 void *User::allocateFixedOperandUser(size_t Size, unsigned Us,
113                                      unsigned DescBytes) {
114   assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
115 
116   static_assert(sizeof(DescriptorInfo) % sizeof(void *) == 0, "Required below");
117 
118   unsigned DescBytesToAllocate =
119       DescBytes == 0 ? 0 : (DescBytes + sizeof(DescriptorInfo));
120   assert(DescBytesToAllocate % sizeof(void *) == 0 &&
121          "We need this to satisfy alignment constraints for Uses");
122 
123   uint8_t *Storage = static_cast<uint8_t *>(
124       ::operator new(Size + sizeof(Use) * Us + DescBytesToAllocate));
125   Use *Start = reinterpret_cast<Use *>(Storage + DescBytesToAllocate);
126   Use *End = Start + Us;
127   User *Obj = reinterpret_cast<User*>(End);
128   Obj->NumUserOperands = Us;
129   Obj->HasHungOffUses = false;
130   Obj->HasDescriptor = DescBytes != 0;
131   Use::initTags(Start, End);
132 
133   if (DescBytes != 0) {
134     auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes);
135     DescInfo->SizeInBytes = DescBytes;
136   }
137 
138   return Obj;
139 }
140 
141 void *User::operator new(size_t Size, unsigned Us) {
142   return allocateFixedOperandUser(Size, Us, 0);
143 }
144 
145 void *User::operator new(size_t Size, unsigned Us, unsigned DescBytes) {
146   return allocateFixedOperandUser(Size, Us, DescBytes);
147 }
148 
149 void *User::operator new(size_t Size) {
150   // Allocate space for a single Use*
151   void *Storage = ::operator new(Size + sizeof(Use *));
152   Use **HungOffOperandList = static_cast<Use **>(Storage);
153   User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1);
154   Obj->NumUserOperands = 0;
155   Obj->HasHungOffUses = true;
156   Obj->HasDescriptor = false;
157   *HungOffOperandList = nullptr;
158   return Obj;
159 }
160 
161 //===----------------------------------------------------------------------===//
162 //                         User operator delete Implementation
163 //===----------------------------------------------------------------------===//
164 
165 // Repress memory sanitization, due to use-after-destroy by operator
166 // delete. Bug report 24578 identifies this issue.
167 LLVM_NO_SANITIZE_MEMORY_ATTRIBUTE void User::operator delete(void *Usr) {
168   // Hung off uses use a single Use* before the User, while other subclasses
169   // use a Use[] allocated prior to the user.
170   User *Obj = static_cast<User *>(Usr);
171   if (Obj->HasHungOffUses) {
172     assert(!Obj->HasDescriptor && "not supported!");
173 
174     Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
175     // drop the hung off uses.
176     Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,
177              /* Delete */ true);
178     ::operator delete(HungOffOperandList);
179   } else if (Obj->HasDescriptor) {
180     Use *UseBegin = static_cast<Use *>(Usr) - Obj->NumUserOperands;
181     Use::zap(UseBegin, UseBegin + Obj->NumUserOperands, /* Delete */ false);
182 
183     auto *DI = reinterpret_cast<DescriptorInfo *>(UseBegin) - 1;
184     uint8_t *Storage = reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes;
185     ::operator delete(Storage);
186   } else {
187     Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;
188     Use::zap(Storage, Storage + Obj->NumUserOperands,
189              /* Delete */ false);
190     ::operator delete(Storage);
191   }
192 }
193 
194 } // End llvm namespace
195