blob: 33a3686c94a132ced51eca7d3ee81aec73d30212 [file] [log] [blame]
Jay Foad59809c72011-01-16 08:10:57 +00001//===-- User.cpp - Implement the User class -------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Jay Foad59809c72011-01-16 08:10:57 +00006//
7//===----------------------------------------------------------------------===//
8
Chandler Carruth9fb823b2013-01-02 11:36:10 +00009#include "llvm/IR/User.h"
10#include "llvm/IR/Constant.h"
11#include "llvm/IR/GlobalValue.h"
Jay Foad59809c72011-01-16 08:10:57 +000012
13namespace llvm {
Pete Cooper87b925b2015-06-10 22:38:30 +000014class BasicBlock;
Jay Foad59809c72011-01-16 08:10:57 +000015
16//===----------------------------------------------------------------------===//
17// User Class
18//===----------------------------------------------------------------------===//
19
Jay Foad59809c72011-01-16 08:10:57 +000020void 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
Pete Cooper3fc30402015-06-10 22:38:46 +000039void User::allocHungoffUses(unsigned N, bool IsPhi) {
Pete Cooperc91fda32015-06-12 17:48:14 +000040 assert(HasHungOffUses && "alloc must have hung off uses");
James Y Knight8096d342015-06-17 01:21:20 +000041
Benjamin Kramerb2505002016-10-20 15:02:18 +000042 static_assert(alignof(Use) >= alignof(Use::UserRef),
James Y Knightf27e4412015-06-17 13:53:12 +000043 "Alignment is insufficient for 'hung-off-uses' pieces");
Benjamin Kramerb2505002016-10-20 15:02:18 +000044 static_assert(alignof(Use::UserRef) >= alignof(BasicBlock *),
James Y Knightf27e4412015-06-17 13:53:12 +000045 "Alignment is insufficient for 'hung-off-uses' pieces");
James Y Knight8096d342015-06-17 01:21:20 +000046
Jay Foad61ea0e42011-06-23 09:09:15 +000047 // 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);
Pete Cooper87b925b2015-06-10 22:38:30 +000050 if (IsPhi)
51 size += N * sizeof(BasicBlock *);
Jay Foad61ea0e42011-06-23 09:09:15 +000052 Use *Begin = static_cast<Use*>(::operator new(size));
Jay Foad59809c72011-01-16 08:10:57 +000053 Use *End = Begin + N;
Jay Foad5c54d752011-06-20 14:12:33 +000054 (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
Pete Cooper74510a42015-06-12 17:48:05 +000055 setOperandList(Use::initTags(Begin, End));
Jay Foad59809c72011-01-16 08:10:57 +000056}
57
Pete Cooper93f9ff52015-06-10 22:38:41 +000058void 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
Pete Cooper74510a42015-06-12 17:48:05 +000067 Use *OldOps = getOperandList();
Pete Cooper93f9ff52015-06-10 22:38:41 +000068 allocHungoffUses(NewNumUses, IsPhi);
Pete Cooper74510a42015-06-12 17:48:05 +000069 Use *NewOps = getOperandList();
Pete Cooper93f9ff52015-06-10 22:38:41 +000070
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
Sanjoy Dasb07fc572015-09-24 01:00:49 +000085
86// This is a private struct used by `User` to track the co-allocated descriptor
87// section.
88struct DescriptorInfo {
89 intptr_t SizeInBytes;
90};
91
92ArrayRef<const uint8_t> User::getDescriptor() const {
93 auto MutableARef = const_cast<User *>(this)->getDescriptor();
94 return {MutableARef.begin(), MutableARef.end()};
95}
96
97MutableArrayRef<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
Jay Foad59809c72011-01-16 08:10:57 +0000108//===----------------------------------------------------------------------===//
109// User operator new Implementations
110//===----------------------------------------------------------------------===//
111
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000112void *User::allocateFixedOperandUser(size_t Size, unsigned Us,
113 unsigned DescBytes) {
Pete Cooperb4eede22015-06-12 17:48:10 +0000114 assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000115
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);
Jay Foad59809c72011-01-16 08:10:57 +0000126 Use *End = Start + Us;
127 User *Obj = reinterpret_cast<User*>(End);
Pete Cooperb4eede22015-06-12 17:48:10 +0000128 Obj->NumUserOperands = Us;
Pete Cooperb676b012015-06-12 17:48:18 +0000129 Obj->HasHungOffUses = false;
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000130 Obj->HasDescriptor = DescBytes != 0;
Jay Foad59809c72011-01-16 08:10:57 +0000131 Use::initTags(Start, End);
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000132
133 if (DescBytes != 0) {
134 auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes);
135 DescInfo->SizeInBytes = DescBytes;
136 }
137
Jay Foad59809c72011-01-16 08:10:57 +0000138 return Obj;
139}
140
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000141void *User::operator new(size_t Size, unsigned Us) {
142 return allocateFixedOperandUser(Size, Us, 0);
143}
144
145void *User::operator new(size_t Size, unsigned Us, unsigned DescBytes) {
146 return allocateFixedOperandUser(Size, Us, DescBytes);
147}
148
Pete Cooperc91fda32015-06-12 17:48:14 +0000149void *User::operator new(size_t Size) {
Pete Cooperb676b012015-06-12 17:48:18 +0000150 // 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);
Pete Cooperc91fda32015-06-12 17:48:14 +0000154 Obj->NumUserOperands = 0;
Pete Cooperb676b012015-06-12 17:48:18 +0000155 Obj->HasHungOffUses = true;
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000156 Obj->HasDescriptor = false;
Pete Cooperb676b012015-06-12 17:48:18 +0000157 *HungOffOperandList = nullptr;
Pete Cooperc91fda32015-06-12 17:48:14 +0000158 return Obj;
159}
160
Jay Foad59809c72011-01-16 08:10:57 +0000161//===----------------------------------------------------------------------===//
162// User operator delete Implementation
163//===----------------------------------------------------------------------===//
164
Naomi Musgrave21c1bc42015-08-31 21:06:08 +0000165void User::operator delete(void *Usr) {
Pete Cooperb676b012015-06-12 17:48:18 +0000166 // 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) {
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000170 assert(!Obj->HasDescriptor && "not supported!");
171
Pete Cooperb676b012015-06-12 17:48:18 +0000172 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);
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000177 } 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);
Pete Cooperb676b012015-06-12 17:48:18 +0000184 } 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 }
Jay Foad59809c72011-01-16 08:10:57 +0000190}
191
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000192} // End llvm namespace