blob: d46039107f33176dde94da3e748f4d9acd94a6da [file] [log] [blame]
Jay Foad59809c72011-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 Carruth9fb823b2013-01-02 11:36:10 +000010#include "llvm/IR/User.h"
11#include "llvm/IR/Constant.h"
12#include "llvm/IR/GlobalValue.h"
13#include "llvm/IR/Operator.h"
Jay Foad59809c72011-01-16 08:10:57 +000014
15namespace llvm {
Pete Cooper87b925b2015-06-10 22:38:30 +000016class BasicBlock;
Jay Foad59809c72011-01-16 08:10:57 +000017
18//===----------------------------------------------------------------------===//
19// User Class
20//===----------------------------------------------------------------------===//
21
Jay Foad59809c72011-01-16 08:10:57 +000022void 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
Pete Cooper3fc30402015-06-10 22:38:46 +000041void User::allocHungoffUses(unsigned N, bool IsPhi) {
Pete Cooperc91fda32015-06-12 17:48:14 +000042 assert(HasHungOffUses && "alloc must have hung off uses");
James Y Knight8096d342015-06-17 01:21:20 +000043
Benjamin Kramerb2505002016-10-20 15:02:18 +000044 static_assert(alignof(Use) >= alignof(Use::UserRef),
James Y Knightf27e4412015-06-17 13:53:12 +000045 "Alignment is insufficient for 'hung-off-uses' pieces");
Benjamin Kramerb2505002016-10-20 15:02:18 +000046 static_assert(alignof(Use::UserRef) >= alignof(BasicBlock *),
James Y Knightf27e4412015-06-17 13:53:12 +000047 "Alignment is insufficient for 'hung-off-uses' pieces");
James Y Knight8096d342015-06-17 01:21:20 +000048
Jay Foad61ea0e42011-06-23 09:09:15 +000049 // 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);
Pete Cooper87b925b2015-06-10 22:38:30 +000052 if (IsPhi)
53 size += N * sizeof(BasicBlock *);
Jay Foad61ea0e42011-06-23 09:09:15 +000054 Use *Begin = static_cast<Use*>(::operator new(size));
Jay Foad59809c72011-01-16 08:10:57 +000055 Use *End = Begin + N;
Jay Foad5c54d752011-06-20 14:12:33 +000056 (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
Pete Cooper74510a42015-06-12 17:48:05 +000057 setOperandList(Use::initTags(Begin, End));
Jay Foad59809c72011-01-16 08:10:57 +000058}
59
Pete Cooper93f9ff52015-06-10 22:38:41 +000060void 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
Pete Cooper74510a42015-06-12 17:48:05 +000069 Use *OldOps = getOperandList();
Pete Cooper93f9ff52015-06-10 22:38:41 +000070 allocHungoffUses(NewNumUses, IsPhi);
Pete Cooper74510a42015-06-12 17:48:05 +000071 Use *NewOps = getOperandList();
Pete Cooper93f9ff52015-06-10 22:38:41 +000072
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
Sanjoy Dasb07fc572015-09-24 01:00:49 +000087
88// This is a private struct used by `User` to track the co-allocated descriptor
89// section.
90struct DescriptorInfo {
91 intptr_t SizeInBytes;
92};
93
94ArrayRef<const uint8_t> User::getDescriptor() const {
95 auto MutableARef = const_cast<User *>(this)->getDescriptor();
96 return {MutableARef.begin(), MutableARef.end()};
97}
98
99MutableArrayRef<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
Jay Foad59809c72011-01-16 08:10:57 +0000110//===----------------------------------------------------------------------===//
111// User operator new Implementations
112//===----------------------------------------------------------------------===//
113
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000114void *User::allocateFixedOperandUser(size_t Size, unsigned Us,
115 unsigned DescBytes) {
Pete Cooperb4eede22015-06-12 17:48:10 +0000116 assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000117
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);
Jay Foad59809c72011-01-16 08:10:57 +0000128 Use *End = Start + Us;
129 User *Obj = reinterpret_cast<User*>(End);
Pete Cooperb4eede22015-06-12 17:48:10 +0000130 Obj->NumUserOperands = Us;
Pete Cooperb676b012015-06-12 17:48:18 +0000131 Obj->HasHungOffUses = false;
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000132 Obj->HasDescriptor = DescBytes != 0;
Jay Foad59809c72011-01-16 08:10:57 +0000133 Use::initTags(Start, End);
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000134
135 if (DescBytes != 0) {
136 auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes);
137 DescInfo->SizeInBytes = DescBytes;
138 }
139
Jay Foad59809c72011-01-16 08:10:57 +0000140 return Obj;
141}
142
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000143void *User::operator new(size_t Size, unsigned Us) {
144 return allocateFixedOperandUser(Size, Us, 0);
145}
146
147void *User::operator new(size_t Size, unsigned Us, unsigned DescBytes) {
148 return allocateFixedOperandUser(Size, Us, DescBytes);
149}
150
Pete Cooperc91fda32015-06-12 17:48:14 +0000151void *User::operator new(size_t Size) {
Pete Cooperb676b012015-06-12 17:48:18 +0000152 // 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);
Pete Cooperc91fda32015-06-12 17:48:14 +0000156 Obj->NumUserOperands = 0;
Pete Cooperb676b012015-06-12 17:48:18 +0000157 Obj->HasHungOffUses = true;
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000158 Obj->HasDescriptor = false;
Pete Cooperb676b012015-06-12 17:48:18 +0000159 *HungOffOperandList = nullptr;
Pete Cooperc91fda32015-06-12 17:48:14 +0000160 return Obj;
161}
162
Jay Foad59809c72011-01-16 08:10:57 +0000163//===----------------------------------------------------------------------===//
164// User operator delete Implementation
165//===----------------------------------------------------------------------===//
166
Naomi Musgrave21c1bc42015-08-31 21:06:08 +0000167void User::operator delete(void *Usr) {
Pete Cooperb676b012015-06-12 17:48:18 +0000168 // 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) {
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000172 assert(!Obj->HasDescriptor && "not supported!");
173
Pete Cooperb676b012015-06-12 17:48:18 +0000174 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);
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000179 } 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);
Pete Cooperb676b012015-06-12 17:48:18 +0000186 } 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 }
Jay Foad59809c72011-01-16 08:10:57 +0000192}
193
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000194} // End llvm namespace