LLVM  10.0.0svn
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"
12 
13 namespace llvm {
14 class BasicBlock;
15 
16 //===----------------------------------------------------------------------===//
17 // User Class
18 //===----------------------------------------------------------------------===//
19 
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.
90 };
91 
93  auto MutableARef = const_cast<User *>(this)->getDescriptor();
94  return {MutableARef.begin(), MutableARef.end()};
95 }
96 
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 
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 void User::operator delete(void *Usr) {
166  // Hung off uses use a single Use* before the User, while other subclasses
167  // use a Use[] allocated prior to the user.
168  User *Obj = static_cast<User *>(Usr);
169  if (Obj->HasHungOffUses) {
170  assert(!Obj->HasDescriptor && "not supported!");
171 
172  Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
173  // drop the hung off uses.
174  Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,
175  /* Delete */ true);
176  ::operator delete(HungOffOperandList);
177  } else if (Obj->HasDescriptor) {
178  Use *UseBegin = static_cast<Use *>(Usr) - Obj->NumUserOperands;
179  Use::zap(UseBegin, UseBegin + Obj->NumUserOperands, /* Delete */ false);
180 
181  auto *DI = reinterpret_cast<DescriptorInfo *>(UseBegin) - 1;
182  uint8_t *Storage = reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes;
183  ::operator delete(Storage);
184  } else {
185  Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;
186  Use::zap(Storage, Storage + Obj->NumUserOperands,
187  /* Delete */ false);
188  ::operator delete(Storage);
189  }
190 }
191 
192 } // End llvm namespace
unsigned HasDescriptor
Definition: Value.h:122
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Various leaf nodes.
Definition: ISDOpcodes.h:59
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
void growHungoffUses(unsigned N, bool IsPhi=false)
Grow the number of hung off uses.
Definition: User.cpp:58
intptr_t SizeInBytes
Definition: User.cpp:89
unsigned HasHungOffUses
Definition: Value.h:121
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
Value * getOperand(unsigned i) const
Definition: User.h:169
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition: User.cpp:20
const Use * getOperandList() const
Definition: User.h:162
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
PointerIntPair - This class implements a pair of a pointer and small integer.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static Use * initTags(Use *Start, Use *Stop)
Initializes the waymarking tags on an array of Uses.
Definition: Use.cpp:56
ArrayRef< const uint8_t > getDescriptor() const
Returns the descriptor co-allocated with this User instance.
Definition: User.cpp:92
unsigned getNumOperands() const
Definition: User.h:191
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1146
BlockVerifier::State From
unsigned NumUserOperands
Definition: Value.h:116
void setOperand(unsigned i, Value *Val)
Definition: User.h:174
PointerIntPair< User *, 1, unsigned, UserRefPointerTraits > UserRef
Definition: Use.h:77
#define N
uint32_t Size
Definition: Profile.cpp:46
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:74
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:39
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1217
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:86