blob: 041593f20b57467f014926619cf21cabe3e04a69 [file] [log] [blame]
Jay Foadbdbe3422011-01-16 08:10:57 +00001//===-- 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
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000010#include "llvm/IR/User.h"
11#include "llvm/IR/Constant.h"
12#include "llvm/IR/GlobalValue.h"
Jay Foadbdbe3422011-01-16 08:10:57 +000013
14namespace llvm {
Pete Coopere0e42bb2015-06-10 22:38:30 +000015class BasicBlock;
Jay Foadbdbe3422011-01-16 08:10:57 +000016
17//===----------------------------------------------------------------------===//
18// User Class
19//===----------------------------------------------------------------------===//
20
Jay Foadbdbe3422011-01-16 08:10:57 +000021void User::replaceUsesOfWith(Value *From, Value *To) {
22 if (From == To) return; // Duh what?
23
24 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
25 "Cannot call User::replaceUsesOfWith on a constant!");
26
27 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
28 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
29 // The side effects of this setOperand call include linking to
30 // "To", adding "this" to the uses list of To, and
31 // most importantly, removing "this" from the use list of "From".
32 setOperand(i, To); // Fix it now...
33 }
34}
35
36//===----------------------------------------------------------------------===//
37// User allocHungoffUses Implementation
38//===----------------------------------------------------------------------===//
39
Pete Cooperea423672015-06-10 22:38:46 +000040void User::allocHungoffUses(unsigned N, bool IsPhi) {
Pete Cooper8b351e42015-06-12 17:48:14 +000041 assert(HasHungOffUses && "alloc must have hung off uses");
James Y Knight4a80b4b2015-06-17 01:21:20 +000042
Benjamin Kramercb58e1e2016-10-20 15:02:18 +000043 static_assert(alignof(Use) >= alignof(Use::UserRef),
James Y Knight4dd3b872015-06-17 13:53:12 +000044 "Alignment is insufficient for 'hung-off-uses' pieces");
Benjamin Kramercb58e1e2016-10-20 15:02:18 +000045 static_assert(alignof(Use::UserRef) >= alignof(BasicBlock *),
James Y Knight4dd3b872015-06-17 13:53:12 +000046 "Alignment is insufficient for 'hung-off-uses' pieces");
James Y Knight4a80b4b2015-06-17 01:21:20 +000047
Jay Foad95c3e482011-06-23 09:09:15 +000048 // Allocate the array of Uses, followed by a pointer (with bottom bit set) to
49 // the User.
50 size_t size = N * sizeof(Use) + sizeof(Use::UserRef);
Pete Coopere0e42bb2015-06-10 22:38:30 +000051 if (IsPhi)
52 size += N * sizeof(BasicBlock *);
Jay Foad95c3e482011-06-23 09:09:15 +000053 Use *Begin = static_cast<Use*>(::operator new(size));
Jay Foadbdbe3422011-01-16 08:10:57 +000054 Use *End = Begin + N;
Jay Foad691c05b2011-06-20 14:12:33 +000055 (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
Pete Coopercff40fc2015-06-12 17:48:05 +000056 setOperandList(Use::initTags(Begin, End));
Jay Foadbdbe3422011-01-16 08:10:57 +000057}
58
Pete Cooper33102d22015-06-10 22:38:41 +000059void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
60 assert(HasHungOffUses && "realloc must have hung off uses");
61
62 unsigned OldNumUses = getNumOperands();
63
64 // We don't support shrinking the number of uses. We wouldn't have enough
65 // space to copy the old uses in to the new space.
66 assert(NewNumUses > OldNumUses && "realloc must grow num uses");
67
Pete Coopercff40fc2015-06-12 17:48:05 +000068 Use *OldOps = getOperandList();
Pete Cooper33102d22015-06-10 22:38:41 +000069 allocHungoffUses(NewNumUses, IsPhi);
Pete Coopercff40fc2015-06-12 17:48:05 +000070 Use *NewOps = getOperandList();
Pete Cooper33102d22015-06-10 22:38:41 +000071
72 // Now copy from the old operands list to the new one.
73 std::copy(OldOps, OldOps + OldNumUses, NewOps);
74
75 // If this is a Phi, then we need to copy the BB pointers too.
76 if (IsPhi) {
77 auto *OldPtr =
78 reinterpret_cast<char *>(OldOps + OldNumUses) + sizeof(Use::UserRef);
79 auto *NewPtr =
80 reinterpret_cast<char *>(NewOps + NewNumUses) + sizeof(Use::UserRef);
81 std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
82 }
83 Use::zap(OldOps, OldOps + OldNumUses, true);
84}
85
Sanjoy Das4769a742015-09-24 01:00:49 +000086
87// This is a private struct used by `User` to track the co-allocated descriptor
88// section.
89struct DescriptorInfo {
90 intptr_t SizeInBytes;
91};
92
93ArrayRef<const uint8_t> User::getDescriptor() const {
94 auto MutableARef = const_cast<User *>(this)->getDescriptor();
95 return {MutableARef.begin(), MutableARef.end()};
96}
97
98MutableArrayRef<uint8_t> User::getDescriptor() {
99 assert(HasDescriptor && "Don't call otherwise!");
100 assert(!HasHungOffUses && "Invariant!");
101
102 auto *DI = reinterpret_cast<DescriptorInfo *>(getIntrusiveOperands()) - 1;
103 assert(DI->SizeInBytes != 0 && "Should not have had a descriptor otherwise!");
104
105 return MutableArrayRef<uint8_t>(
106 reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes, DI->SizeInBytes);
107}
108
Jay Foadbdbe3422011-01-16 08:10:57 +0000109//===----------------------------------------------------------------------===//
110// User operator new Implementations
111//===----------------------------------------------------------------------===//
112
Sanjoy Das4769a742015-09-24 01:00:49 +0000113void *User::allocateFixedOperandUser(size_t Size, unsigned Us,
114 unsigned DescBytes) {
Pete Cooperaaa3fa62015-06-12 17:48:10 +0000115 assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
Sanjoy Das4769a742015-09-24 01:00:49 +0000116
117 static_assert(sizeof(DescriptorInfo) % sizeof(void *) == 0, "Required below");
118
119 unsigned DescBytesToAllocate =
120 DescBytes == 0 ? 0 : (DescBytes + sizeof(DescriptorInfo));
121 assert(DescBytesToAllocate % sizeof(void *) == 0 &&
122 "We need this to satisfy alignment constraints for Uses");
123
124 uint8_t *Storage = static_cast<uint8_t *>(
125 ::operator new(Size + sizeof(Use) * Us + DescBytesToAllocate));
126 Use *Start = reinterpret_cast<Use *>(Storage + DescBytesToAllocate);
Jay Foadbdbe3422011-01-16 08:10:57 +0000127 Use *End = Start + Us;
128 User *Obj = reinterpret_cast<User*>(End);
Pete Cooperaaa3fa62015-06-12 17:48:10 +0000129 Obj->NumUserOperands = Us;
Pete Coopera6ff2212015-06-12 17:48:18 +0000130 Obj->HasHungOffUses = false;
Sanjoy Das4769a742015-09-24 01:00:49 +0000131 Obj->HasDescriptor = DescBytes != 0;
Jay Foadbdbe3422011-01-16 08:10:57 +0000132 Use::initTags(Start, End);
Sanjoy Das4769a742015-09-24 01:00:49 +0000133
134 if (DescBytes != 0) {
135 auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes);
136 DescInfo->SizeInBytes = DescBytes;
137 }
138
Jay Foadbdbe3422011-01-16 08:10:57 +0000139 return Obj;
140}
141
Sanjoy Das4769a742015-09-24 01:00:49 +0000142void *User::operator new(size_t Size, unsigned Us) {
143 return allocateFixedOperandUser(Size, Us, 0);
144}
145
146void *User::operator new(size_t Size, unsigned Us, unsigned DescBytes) {
147 return allocateFixedOperandUser(Size, Us, DescBytes);
148}
149
Pete Cooper8b351e42015-06-12 17:48:14 +0000150void *User::operator new(size_t Size) {
Pete Coopera6ff2212015-06-12 17:48:18 +0000151 // Allocate space for a single Use*
152 void *Storage = ::operator new(Size + sizeof(Use *));
153 Use **HungOffOperandList = static_cast<Use **>(Storage);
154 User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1);
Pete Cooper8b351e42015-06-12 17:48:14 +0000155 Obj->NumUserOperands = 0;
Pete Coopera6ff2212015-06-12 17:48:18 +0000156 Obj->HasHungOffUses = true;
Sanjoy Das4769a742015-09-24 01:00:49 +0000157 Obj->HasDescriptor = false;
Pete Coopera6ff2212015-06-12 17:48:18 +0000158 *HungOffOperandList = nullptr;
Pete Cooper8b351e42015-06-12 17:48:14 +0000159 return Obj;
160}
161
Jay Foadbdbe3422011-01-16 08:10:57 +0000162//===----------------------------------------------------------------------===//
163// User operator delete Implementation
164//===----------------------------------------------------------------------===//
165
Naomi Musgrave950c0092015-08-31 21:06:08 +0000166void User::operator delete(void *Usr) {
Pete Coopera6ff2212015-06-12 17:48:18 +0000167 // Hung off uses use a single Use* before the User, while other subclasses
168 // use a Use[] allocated prior to the user.
169 User *Obj = static_cast<User *>(Usr);
170 if (Obj->HasHungOffUses) {
Sanjoy Das4769a742015-09-24 01:00:49 +0000171 assert(!Obj->HasDescriptor && "not supported!");
172
Pete Coopera6ff2212015-06-12 17:48:18 +0000173 Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
174 // drop the hung off uses.
175 Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,
176 /* Delete */ true);
177 ::operator delete(HungOffOperandList);
Sanjoy Das4769a742015-09-24 01:00:49 +0000178 } else if (Obj->HasDescriptor) {
179 Use *UseBegin = static_cast<Use *>(Usr) - Obj->NumUserOperands;
180 Use::zap(UseBegin, UseBegin + Obj->NumUserOperands, /* Delete */ false);
181
182 auto *DI = reinterpret_cast<DescriptorInfo *>(UseBegin) - 1;
183 uint8_t *Storage = reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes;
184 ::operator delete(Storage);
Pete Coopera6ff2212015-06-12 17:48:18 +0000185 } else {
186 Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;
187 Use::zap(Storage, Storage + Obj->NumUserOperands,
188 /* Delete */ false);
189 ::operator delete(Storage);
190 }
Jay Foadbdbe3422011-01-16 08:10:57 +0000191}
192
Alexander Kornienkocd52a7a2015-06-23 09:49:53 +0000193} // End llvm namespace