]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/IR/User.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r303571, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / IR / User.cpp
1 //===-- User.cpp - Implement the User class -------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/IR/User.h"
11 #include "llvm/IR/Constant.h"
12 #include "llvm/IR/GlobalValue.h"
13 #include "llvm/IR/Operator.h"
14
15 namespace llvm {
16 class BasicBlock;
17
18 //===----------------------------------------------------------------------===//
19 //                                 User Class
20 //===----------------------------------------------------------------------===//
21
22 void User::replaceUsesOfWith(Value *From, Value *To) {
23   if (From == To) return;   // 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); // Fix it now...
34     }
35 }
36
37 //===----------------------------------------------------------------------===//
38 //                         User allocHungoffUses Implementation
39 //===----------------------------------------------------------------------===//
40
41 void User::allocHungoffUses(unsigned N, bool IsPhi) {
42   assert(HasHungOffUses && "alloc must have hung off uses");
43
44   static_assert(alignof(Use) >= alignof(Use::UserRef),
45                 "Alignment is insufficient for 'hung-off-uses' pieces");
46   static_assert(alignof(Use::UserRef) >= alignof(BasicBlock *),
47                 "Alignment is insufficient for 'hung-off-uses' pieces");
48
49   // Allocate the array of Uses, followed by a pointer (with bottom bit set) to
50   // the User.
51   size_t size = N * sizeof(Use) + sizeof(Use::UserRef);
52   if (IsPhi)
53     size += N * sizeof(BasicBlock *);
54   Use *Begin = static_cast<Use*>(::operator new(size));
55   Use *End = Begin + N;
56   (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
57   setOperandList(Use::initTags(Begin, End));
58 }
59
60 void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
61   assert(HasHungOffUses && "realloc must have hung off uses");
62
63   unsigned OldNumUses = getNumOperands();
64
65   // We don't support shrinking the number of uses.  We wouldn't have enough
66   // space to copy the old uses in to the new space.
67   assert(NewNumUses > OldNumUses && "realloc must grow num uses");
68
69   Use *OldOps = getOperandList();
70   allocHungoffUses(NewNumUses, IsPhi);
71   Use *NewOps = getOperandList();
72
73   // Now copy from the old operands list to the new one.
74   std::copy(OldOps, OldOps + OldNumUses, NewOps);
75
76   // If this is a Phi, then we need to copy the BB pointers too.
77   if (IsPhi) {
78     auto *OldPtr =
79         reinterpret_cast<char *>(OldOps + OldNumUses) + sizeof(Use::UserRef);
80     auto *NewPtr =
81         reinterpret_cast<char *>(NewOps + NewNumUses) + sizeof(Use::UserRef);
82     std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
83   }
84   Use::zap(OldOps, OldOps + OldNumUses, true);
85 }
86
87
88 // This is a private struct used by `User` to track the co-allocated descriptor
89 // section.
90 struct DescriptorInfo {
91   intptr_t SizeInBytes;
92 };
93
94 ArrayRef<const uint8_t> User::getDescriptor() const {
95   auto MutableARef = const_cast<User *>(this)->getDescriptor();
96   return {MutableARef.begin(), MutableARef.end()};
97 }
98
99 MutableArrayRef<uint8_t> User::getDescriptor() {
100   assert(HasDescriptor && "Don't call otherwise!");
101   assert(!HasHungOffUses && "Invariant!");
102
103   auto *DI = reinterpret_cast<DescriptorInfo *>(getIntrusiveOperands()) - 1;
104   assert(DI->SizeInBytes != 0 && "Should not have had a descriptor otherwise!");
105
106   return MutableArrayRef<uint8_t>(
107       reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes, DI->SizeInBytes);
108 }
109
110 //===----------------------------------------------------------------------===//
111 //                         User operator new Implementations
112 //===----------------------------------------------------------------------===//
113
114 void *User::allocateFixedOperandUser(size_t Size, unsigned Us,
115                                      unsigned DescBytes) {
116   assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
117
118   static_assert(sizeof(DescriptorInfo) % sizeof(void *) == 0, "Required below");
119
120   unsigned DescBytesToAllocate =
121       DescBytes == 0 ? 0 : (DescBytes + sizeof(DescriptorInfo));
122   assert(DescBytesToAllocate % sizeof(void *) == 0 &&
123          "We need this to satisfy alignment constraints for Uses");
124
125   uint8_t *Storage = static_cast<uint8_t *>(
126       ::operator new(Size + sizeof(Use) * Us + DescBytesToAllocate));
127   Use *Start = reinterpret_cast<Use *>(Storage + DescBytesToAllocate);
128   Use *End = Start + Us;
129   User *Obj = reinterpret_cast<User*>(End);
130   Obj->NumUserOperands = Us;
131   Obj->HasHungOffUses = false;
132   Obj->HasDescriptor = DescBytes != 0;
133   Use::initTags(Start, End);
134
135   if (DescBytes != 0) {
136     auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes);
137     DescInfo->SizeInBytes = DescBytes;
138   }
139
140   return Obj;
141 }
142
143 void *User::operator new(size_t Size, unsigned Us) {
144   return allocateFixedOperandUser(Size, Us, 0);
145 }
146
147 void *User::operator new(size_t Size, unsigned Us, unsigned DescBytes) {
148   return allocateFixedOperandUser(Size, Us, DescBytes);
149 }
150
151 void *User::operator new(size_t Size) {
152   // Allocate space for a single Use*
153   void *Storage = ::operator new(Size + sizeof(Use *));
154   Use **HungOffOperandList = static_cast<Use **>(Storage);
155   User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1);
156   Obj->NumUserOperands = 0;
157   Obj->HasHungOffUses = true;
158   Obj->HasDescriptor = false;
159   *HungOffOperandList = nullptr;
160   return Obj;
161 }
162
163 //===----------------------------------------------------------------------===//
164 //                         User operator delete Implementation
165 //===----------------------------------------------------------------------===//
166
167 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