[InstCombine] Signed saturation tests. NFC
[llvm-complete.git] / lib / IR / User.cpp
blob33a3686c94a132ced51eca7d3ee81aec73d30212
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 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/User.h"
10 #include "llvm/IR/Constant.h"
11 #include "llvm/IR/GlobalValue.h"
13 namespace llvm {
14 class BasicBlock;
16 //===----------------------------------------------------------------------===//
17 // User Class
18 //===----------------------------------------------------------------------===//
20 void User::replaceUsesOfWith(Value *From, Value *To) {
21 if (From == To) return; // Duh what?
23 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
24 "Cannot call User::replaceUsesOfWith on a constant!");
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...
35 //===----------------------------------------------------------------------===//
36 // User allocHungoffUses Implementation
37 //===----------------------------------------------------------------------===//
39 void User::allocHungoffUses(unsigned N, bool IsPhi) {
40 assert(HasHungOffUses && "alloc must have hung off uses");
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");
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));
58 void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
59 assert(HasHungOffUses && "realloc must have hung off uses");
61 unsigned OldNumUses = getNumOperands();
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");
67 Use *OldOps = getOperandList();
68 allocHungoffUses(NewNumUses, IsPhi);
69 Use *NewOps = getOperandList();
71 // Now copy from the old operands list to the new one.
72 std::copy(OldOps, OldOps + OldNumUses, NewOps);
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);
82 Use::zap(OldOps, OldOps + OldNumUses, true);
86 // This is a private struct used by `User` to track the co-allocated descriptor
87 // section.
88 struct DescriptorInfo {
89 intptr_t SizeInBytes;
92 ArrayRef<const uint8_t> User::getDescriptor() const {
93 auto MutableARef = const_cast<User *>(this)->getDescriptor();
94 return {MutableARef.begin(), MutableARef.end()};
97 MutableArrayRef<uint8_t> User::getDescriptor() {
98 assert(HasDescriptor && "Don't call otherwise!");
99 assert(!HasHungOffUses && "Invariant!");
101 auto *DI = reinterpret_cast<DescriptorInfo *>(getIntrusiveOperands()) - 1;
102 assert(DI->SizeInBytes != 0 && "Should not have had a descriptor otherwise!");
104 return MutableArrayRef<uint8_t>(
105 reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes, DI->SizeInBytes);
108 //===----------------------------------------------------------------------===//
109 // User operator new Implementations
110 //===----------------------------------------------------------------------===//
112 void *User::allocateFixedOperandUser(size_t Size, unsigned Us,
113 unsigned DescBytes) {
114 assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
116 static_assert(sizeof(DescriptorInfo) % sizeof(void *) == 0, "Required below");
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");
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);
133 if (DescBytes != 0) {
134 auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes);
135 DescInfo->SizeInBytes = DescBytes;
138 return Obj;
141 void *User::operator new(size_t Size, unsigned Us) {
142 return allocateFixedOperandUser(Size, Us, 0);
145 void *User::operator new(size_t Size, unsigned Us, unsigned DescBytes) {
146 return allocateFixedOperandUser(Size, Us, DescBytes);
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;
161 //===----------------------------------------------------------------------===//
162 // User operator delete Implementation
163 //===----------------------------------------------------------------------===//
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!");
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);
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);
192 } // End llvm namespace