blob: 497b4aa176434a7447bd87e14fef2a50ae5eaa94 [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
David Blaikie3a15e142011-12-01 08:00:17 +000022void User::anchor() {}
23
Jay Foad59809c72011-01-16 08:10:57 +000024void User::replaceUsesOfWith(Value *From, Value *To) {
25 if (From == To) return; // Duh what?
26
27 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
28 "Cannot call User::replaceUsesOfWith on a constant!");
29
30 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
31 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
32 // The side effects of this setOperand call include linking to
33 // "To", adding "this" to the uses list of To, and
34 // most importantly, removing "this" from the use list of "From".
35 setOperand(i, To); // Fix it now...
36 }
37}
38
39//===----------------------------------------------------------------------===//
40// User allocHungoffUses Implementation
41//===----------------------------------------------------------------------===//
42
Pete Cooper3fc30402015-06-10 22:38:46 +000043void User::allocHungoffUses(unsigned N, bool IsPhi) {
Pete Cooperc91fda32015-06-12 17:48:14 +000044 assert(HasHungOffUses && "alloc must have hung off uses");
James Y Knight8096d342015-06-17 01:21:20 +000045
Benjamin Kramerb2505002016-10-20 15:02:18 +000046 static_assert(alignof(Use) >= alignof(Use::UserRef),
James Y Knightf27e4412015-06-17 13:53:12 +000047 "Alignment is insufficient for 'hung-off-uses' pieces");
Benjamin Kramerb2505002016-10-20 15:02:18 +000048 static_assert(alignof(Use::UserRef) >= alignof(BasicBlock *),
James Y Knightf27e4412015-06-17 13:53:12 +000049 "Alignment is insufficient for 'hung-off-uses' pieces");
James Y Knight8096d342015-06-17 01:21:20 +000050
Jay Foad61ea0e42011-06-23 09:09:15 +000051 // Allocate the array of Uses, followed by a pointer (with bottom bit set) to
52 // the User.
53 size_t size = N * sizeof(Use) + sizeof(Use::UserRef);
Pete Cooper87b925b2015-06-10 22:38:30 +000054 if (IsPhi)
55 size += N * sizeof(BasicBlock *);
Jay Foad61ea0e42011-06-23 09:09:15 +000056 Use *Begin = static_cast<Use*>(::operator new(size));
Jay Foad59809c72011-01-16 08:10:57 +000057 Use *End = Begin + N;
Jay Foad5c54d752011-06-20 14:12:33 +000058 (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
Pete Cooper74510a42015-06-12 17:48:05 +000059 setOperandList(Use::initTags(Begin, End));
Jay Foad59809c72011-01-16 08:10:57 +000060}
61
Pete Cooper93f9ff52015-06-10 22:38:41 +000062void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
63 assert(HasHungOffUses && "realloc must have hung off uses");
64
65 unsigned OldNumUses = getNumOperands();
66
67 // We don't support shrinking the number of uses. We wouldn't have enough
68 // space to copy the old uses in to the new space.
69 assert(NewNumUses > OldNumUses && "realloc must grow num uses");
70
Pete Cooper74510a42015-06-12 17:48:05 +000071 Use *OldOps = getOperandList();
Pete Cooper93f9ff52015-06-10 22:38:41 +000072 allocHungoffUses(NewNumUses, IsPhi);
Pete Cooper74510a42015-06-12 17:48:05 +000073 Use *NewOps = getOperandList();
Pete Cooper93f9ff52015-06-10 22:38:41 +000074
75 // Now copy from the old operands list to the new one.
76 std::copy(OldOps, OldOps + OldNumUses, NewOps);
77
78 // If this is a Phi, then we need to copy the BB pointers too.
79 if (IsPhi) {
80 auto *OldPtr =
81 reinterpret_cast<char *>(OldOps + OldNumUses) + sizeof(Use::UserRef);
82 auto *NewPtr =
83 reinterpret_cast<char *>(NewOps + NewNumUses) + sizeof(Use::UserRef);
84 std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
85 }
86 Use::zap(OldOps, OldOps + OldNumUses, true);
87}
88
Sanjoy Dasb07fc572015-09-24 01:00:49 +000089
90// This is a private struct used by `User` to track the co-allocated descriptor
91// section.
92struct DescriptorInfo {
93 intptr_t SizeInBytes;
94};
95
96ArrayRef<const uint8_t> User::getDescriptor() const {
97 auto MutableARef = const_cast<User *>(this)->getDescriptor();
98 return {MutableARef.begin(), MutableARef.end()};
99}
100
101MutableArrayRef<uint8_t> User::getDescriptor() {
102 assert(HasDescriptor && "Don't call otherwise!");
103 assert(!HasHungOffUses && "Invariant!");
104
105 auto *DI = reinterpret_cast<DescriptorInfo *>(getIntrusiveOperands()) - 1;
106 assert(DI->SizeInBytes != 0 && "Should not have had a descriptor otherwise!");
107
108 return MutableArrayRef<uint8_t>(
109 reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes, DI->SizeInBytes);
110}
111
Jay Foad59809c72011-01-16 08:10:57 +0000112//===----------------------------------------------------------------------===//
113// User operator new Implementations
114//===----------------------------------------------------------------------===//
115
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000116void *User::allocateFixedOperandUser(size_t Size, unsigned Us,
117 unsigned DescBytes) {
Pete Cooperb4eede22015-06-12 17:48:10 +0000118 assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000119
120 static_assert(sizeof(DescriptorInfo) % sizeof(void *) == 0, "Required below");
121
122 unsigned DescBytesToAllocate =
123 DescBytes == 0 ? 0 : (DescBytes + sizeof(DescriptorInfo));
124 assert(DescBytesToAllocate % sizeof(void *) == 0 &&
125 "We need this to satisfy alignment constraints for Uses");
126
127 uint8_t *Storage = static_cast<uint8_t *>(
128 ::operator new(Size + sizeof(Use) * Us + DescBytesToAllocate));
129 Use *Start = reinterpret_cast<Use *>(Storage + DescBytesToAllocate);
Jay Foad59809c72011-01-16 08:10:57 +0000130 Use *End = Start + Us;
131 User *Obj = reinterpret_cast<User*>(End);
Pete Cooperb4eede22015-06-12 17:48:10 +0000132 Obj->NumUserOperands = Us;
Pete Cooperb676b012015-06-12 17:48:18 +0000133 Obj->HasHungOffUses = false;
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000134 Obj->HasDescriptor = DescBytes != 0;
Jay Foad59809c72011-01-16 08:10:57 +0000135 Use::initTags(Start, End);
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000136
137 if (DescBytes != 0) {
138 auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes);
139 DescInfo->SizeInBytes = DescBytes;
140 }
141
Jay Foad59809c72011-01-16 08:10:57 +0000142 return Obj;
143}
144
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000145void *User::operator new(size_t Size, unsigned Us) {
146 return allocateFixedOperandUser(Size, Us, 0);
147}
148
149void *User::operator new(size_t Size, unsigned Us, unsigned DescBytes) {
150 return allocateFixedOperandUser(Size, Us, DescBytes);
151}
152
Pete Cooperc91fda32015-06-12 17:48:14 +0000153void *User::operator new(size_t Size) {
Pete Cooperb676b012015-06-12 17:48:18 +0000154 // Allocate space for a single Use*
155 void *Storage = ::operator new(Size + sizeof(Use *));
156 Use **HungOffOperandList = static_cast<Use **>(Storage);
157 User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1);
Pete Cooperc91fda32015-06-12 17:48:14 +0000158 Obj->NumUserOperands = 0;
Pete Cooperb676b012015-06-12 17:48:18 +0000159 Obj->HasHungOffUses = true;
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000160 Obj->HasDescriptor = false;
Pete Cooperb676b012015-06-12 17:48:18 +0000161 *HungOffOperandList = nullptr;
Pete Cooperc91fda32015-06-12 17:48:14 +0000162 return Obj;
163}
164
Jay Foad59809c72011-01-16 08:10:57 +0000165//===----------------------------------------------------------------------===//
166// User operator delete Implementation
167//===----------------------------------------------------------------------===//
168
Naomi Musgrave21c1bc42015-08-31 21:06:08 +0000169void User::operator delete(void *Usr) {
Pete Cooperb676b012015-06-12 17:48:18 +0000170 // Hung off uses use a single Use* before the User, while other subclasses
171 // use a Use[] allocated prior to the user.
172 User *Obj = static_cast<User *>(Usr);
173 if (Obj->HasHungOffUses) {
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000174 assert(!Obj->HasDescriptor && "not supported!");
175
Pete Cooperb676b012015-06-12 17:48:18 +0000176 Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
177 // drop the hung off uses.
178 Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,
179 /* Delete */ true);
180 ::operator delete(HungOffOperandList);
Sanjoy Dasb07fc572015-09-24 01:00:49 +0000181 } else if (Obj->HasDescriptor) {
182 Use *UseBegin = static_cast<Use *>(Usr) - Obj->NumUserOperands;
183 Use::zap(UseBegin, UseBegin + Obj->NumUserOperands, /* Delete */ false);
184
185 auto *DI = reinterpret_cast<DescriptorInfo *>(UseBegin) - 1;
186 uint8_t *Storage = reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes;
187 ::operator delete(Storage);
Pete Cooperb676b012015-06-12 17:48:18 +0000188 } else {
189 Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;
190 Use::zap(Storage, Storage + Obj->NumUserOperands,
191 /* Delete */ false);
192 ::operator delete(Storage);
193 }
Jay Foad59809c72011-01-16 08:10:57 +0000194}
195
Richard Smith1f6f4552012-10-24 00:30:41 +0000196//===----------------------------------------------------------------------===//
197// Operator Class
198//===----------------------------------------------------------------------===//
199
200Operator::~Operator() {
201 llvm_unreachable("should never destroy an Operator");
202}
203
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000204} // End llvm namespace