1139f7f9bSDimitry Andric //===-- User.cpp - Implement the User class -------------------------------===//
2139f7f9bSDimitry Andric //
3139f7f9bSDimitry Andric // The LLVM Compiler Infrastructure
4139f7f9bSDimitry Andric //
5139f7f9bSDimitry Andric // This file is distributed under the University of Illinois Open Source
6139f7f9bSDimitry Andric // License. See LICENSE.TXT for details.
7139f7f9bSDimitry Andric //
8139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
9139f7f9bSDimitry Andric
10139f7f9bSDimitry Andric #include "llvm/IR/User.h"
11139f7f9bSDimitry Andric #include "llvm/IR/Constant.h"
12139f7f9bSDimitry Andric #include "llvm/IR/GlobalValue.h"
13139f7f9bSDimitry Andric
14139f7f9bSDimitry Andric namespace llvm {
158f0fd8f6SDimitry Andric class BasicBlock;
16139f7f9bSDimitry Andric
17139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
18139f7f9bSDimitry Andric // User Class
19139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
20139f7f9bSDimitry Andric
replaceUsesOfWith(Value * From,Value * To)21139f7f9bSDimitry Andric void User::replaceUsesOfWith(Value *From, Value *To) {
22139f7f9bSDimitry Andric if (From == To) return; // Duh what?
23139f7f9bSDimitry Andric
24139f7f9bSDimitry Andric assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
25139f7f9bSDimitry Andric "Cannot call User::replaceUsesOfWith on a constant!");
26139f7f9bSDimitry Andric
27139f7f9bSDimitry Andric for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
28139f7f9bSDimitry Andric if (getOperand(i) == From) { // Is This operand is pointing to oldval?
29139f7f9bSDimitry Andric // The side effects of this setOperand call include linking to
30139f7f9bSDimitry Andric // "To", adding "this" to the uses list of To, and
31139f7f9bSDimitry Andric // most importantly, removing "this" from the use list of "From".
32139f7f9bSDimitry Andric setOperand(i, To); // Fix it now...
33139f7f9bSDimitry Andric }
34139f7f9bSDimitry Andric }
35139f7f9bSDimitry Andric
36139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
37139f7f9bSDimitry Andric // User allocHungoffUses Implementation
38139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
39139f7f9bSDimitry Andric
allocHungoffUses(unsigned N,bool IsPhi)408f0fd8f6SDimitry Andric void User::allocHungoffUses(unsigned N, bool IsPhi) {
418f0fd8f6SDimitry Andric assert(HasHungOffUses && "alloc must have hung off uses");
428f0fd8f6SDimitry Andric
43*d88c1a5aSDimitry Andric static_assert(alignof(Use) >= alignof(Use::UserRef),
448f0fd8f6SDimitry Andric "Alignment is insufficient for 'hung-off-uses' pieces");
45*d88c1a5aSDimitry Andric static_assert(alignof(Use::UserRef) >= alignof(BasicBlock *),
468f0fd8f6SDimitry Andric "Alignment is insufficient for 'hung-off-uses' pieces");
478f0fd8f6SDimitry Andric
48139f7f9bSDimitry Andric // Allocate the array of Uses, followed by a pointer (with bottom bit set) to
49139f7f9bSDimitry Andric // the User.
50139f7f9bSDimitry Andric size_t size = N * sizeof(Use) + sizeof(Use::UserRef);
518f0fd8f6SDimitry Andric if (IsPhi)
528f0fd8f6SDimitry Andric size += N * sizeof(BasicBlock *);
53139f7f9bSDimitry Andric Use *Begin = static_cast<Use*>(::operator new(size));
54139f7f9bSDimitry Andric Use *End = Begin + N;
55139f7f9bSDimitry Andric (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
568f0fd8f6SDimitry Andric setOperandList(Use::initTags(Begin, End));
578f0fd8f6SDimitry Andric }
588f0fd8f6SDimitry Andric
growHungoffUses(unsigned NewNumUses,bool IsPhi)598f0fd8f6SDimitry Andric void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
608f0fd8f6SDimitry Andric assert(HasHungOffUses && "realloc must have hung off uses");
618f0fd8f6SDimitry Andric
628f0fd8f6SDimitry Andric unsigned OldNumUses = getNumOperands();
638f0fd8f6SDimitry Andric
648f0fd8f6SDimitry Andric // We don't support shrinking the number of uses. We wouldn't have enough
658f0fd8f6SDimitry Andric // space to copy the old uses in to the new space.
668f0fd8f6SDimitry Andric assert(NewNumUses > OldNumUses && "realloc must grow num uses");
678f0fd8f6SDimitry Andric
688f0fd8f6SDimitry Andric Use *OldOps = getOperandList();
698f0fd8f6SDimitry Andric allocHungoffUses(NewNumUses, IsPhi);
708f0fd8f6SDimitry Andric Use *NewOps = getOperandList();
718f0fd8f6SDimitry Andric
728f0fd8f6SDimitry Andric // Now copy from the old operands list to the new one.
738f0fd8f6SDimitry Andric std::copy(OldOps, OldOps + OldNumUses, NewOps);
748f0fd8f6SDimitry Andric
758f0fd8f6SDimitry Andric // If this is a Phi, then we need to copy the BB pointers too.
768f0fd8f6SDimitry Andric if (IsPhi) {
778f0fd8f6SDimitry Andric auto *OldPtr =
788f0fd8f6SDimitry Andric reinterpret_cast<char *>(OldOps + OldNumUses) + sizeof(Use::UserRef);
798f0fd8f6SDimitry Andric auto *NewPtr =
808f0fd8f6SDimitry Andric reinterpret_cast<char *>(NewOps + NewNumUses) + sizeof(Use::UserRef);
818f0fd8f6SDimitry Andric std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
828f0fd8f6SDimitry Andric }
838f0fd8f6SDimitry Andric Use::zap(OldOps, OldOps + OldNumUses, true);
84139f7f9bSDimitry Andric }
85139f7f9bSDimitry Andric
867d523365SDimitry Andric
877d523365SDimitry Andric // This is a private struct used by `User` to track the co-allocated descriptor
887d523365SDimitry Andric // section.
897d523365SDimitry Andric struct DescriptorInfo {
907d523365SDimitry Andric intptr_t SizeInBytes;
917d523365SDimitry Andric };
927d523365SDimitry Andric
getDescriptor() const937d523365SDimitry Andric ArrayRef<const uint8_t> User::getDescriptor() const {
947d523365SDimitry Andric auto MutableARef = const_cast<User *>(this)->getDescriptor();
957d523365SDimitry Andric return {MutableARef.begin(), MutableARef.end()};
967d523365SDimitry Andric }
977d523365SDimitry Andric
getDescriptor()987d523365SDimitry Andric MutableArrayRef<uint8_t> User::getDescriptor() {
997d523365SDimitry Andric assert(HasDescriptor && "Don't call otherwise!");
1007d523365SDimitry Andric assert(!HasHungOffUses && "Invariant!");
1017d523365SDimitry Andric
1027d523365SDimitry Andric auto *DI = reinterpret_cast<DescriptorInfo *>(getIntrusiveOperands()) - 1;
1037d523365SDimitry Andric assert(DI->SizeInBytes != 0 && "Should not have had a descriptor otherwise!");
1047d523365SDimitry Andric
1057d523365SDimitry Andric return MutableArrayRef<uint8_t>(
1067d523365SDimitry Andric reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes, DI->SizeInBytes);
1077d523365SDimitry Andric }
1087d523365SDimitry Andric
109139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
110139f7f9bSDimitry Andric // User operator new Implementations
111139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
112139f7f9bSDimitry Andric
allocateFixedOperandUser(size_t Size,unsigned Us,unsigned DescBytes)1137d523365SDimitry Andric void *User::allocateFixedOperandUser(size_t Size, unsigned Us,
1147d523365SDimitry Andric unsigned DescBytes) {
1158f0fd8f6SDimitry Andric assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
1167d523365SDimitry Andric
1177d523365SDimitry Andric static_assert(sizeof(DescriptorInfo) % sizeof(void *) == 0, "Required below");
1187d523365SDimitry Andric
1197d523365SDimitry Andric unsigned DescBytesToAllocate =
1207d523365SDimitry Andric DescBytes == 0 ? 0 : (DescBytes + sizeof(DescriptorInfo));
1217d523365SDimitry Andric assert(DescBytesToAllocate % sizeof(void *) == 0 &&
1227d523365SDimitry Andric "We need this to satisfy alignment constraints for Uses");
1237d523365SDimitry Andric
1247d523365SDimitry Andric uint8_t *Storage = static_cast<uint8_t *>(
1257d523365SDimitry Andric ::operator new(Size + sizeof(Use) * Us + DescBytesToAllocate));
1267d523365SDimitry Andric Use *Start = reinterpret_cast<Use *>(Storage + DescBytesToAllocate);
127139f7f9bSDimitry Andric Use *End = Start + Us;
128139f7f9bSDimitry Andric User *Obj = reinterpret_cast<User*>(End);
1298f0fd8f6SDimitry Andric Obj->NumUserOperands = Us;
1308f0fd8f6SDimitry Andric Obj->HasHungOffUses = false;
1317d523365SDimitry Andric Obj->HasDescriptor = DescBytes != 0;
132139f7f9bSDimitry Andric Use::initTags(Start, End);
1337d523365SDimitry Andric
1347d523365SDimitry Andric if (DescBytes != 0) {
1357d523365SDimitry Andric auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes);
1367d523365SDimitry Andric DescInfo->SizeInBytes = DescBytes;
1377d523365SDimitry Andric }
1387d523365SDimitry Andric
139139f7f9bSDimitry Andric return Obj;
140139f7f9bSDimitry Andric }
141139f7f9bSDimitry Andric
operator new(size_t Size,unsigned Us)1427d523365SDimitry Andric void *User::operator new(size_t Size, unsigned Us) {
1437d523365SDimitry Andric return allocateFixedOperandUser(Size, Us, 0);
1447d523365SDimitry Andric }
1457d523365SDimitry Andric
operator new(size_t Size,unsigned Us,unsigned DescBytes)1467d523365SDimitry Andric void *User::operator new(size_t Size, unsigned Us, unsigned DescBytes) {
1477d523365SDimitry Andric return allocateFixedOperandUser(Size, Us, DescBytes);
1487d523365SDimitry Andric }
1497d523365SDimitry Andric
operator new(size_t Size)1508f0fd8f6SDimitry Andric void *User::operator new(size_t Size) {
1518f0fd8f6SDimitry Andric // Allocate space for a single Use*
1528f0fd8f6SDimitry Andric void *Storage = ::operator new(Size + sizeof(Use *));
1538f0fd8f6SDimitry Andric Use **HungOffOperandList = static_cast<Use **>(Storage);
1548f0fd8f6SDimitry Andric User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1);
1558f0fd8f6SDimitry Andric Obj->NumUserOperands = 0;
1568f0fd8f6SDimitry Andric Obj->HasHungOffUses = true;
1577d523365SDimitry Andric Obj->HasDescriptor = false;
1588f0fd8f6SDimitry Andric *HungOffOperandList = nullptr;
1598f0fd8f6SDimitry Andric return Obj;
1608f0fd8f6SDimitry Andric }
1618f0fd8f6SDimitry Andric
162139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
163139f7f9bSDimitry Andric // User operator delete Implementation
164139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
165139f7f9bSDimitry Andric
operator delete(void * Usr)166139f7f9bSDimitry Andric void User::operator delete(void *Usr) {
1678f0fd8f6SDimitry Andric // Hung off uses use a single Use* before the User, while other subclasses
1688f0fd8f6SDimitry Andric // use a Use[] allocated prior to the user.
1698f0fd8f6SDimitry Andric User *Obj = static_cast<User *>(Usr);
1708f0fd8f6SDimitry Andric if (Obj->HasHungOffUses) {
1717d523365SDimitry Andric assert(!Obj->HasDescriptor && "not supported!");
1727d523365SDimitry Andric
1738f0fd8f6SDimitry Andric Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
1748f0fd8f6SDimitry Andric // drop the hung off uses.
1758f0fd8f6SDimitry Andric Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,
1768f0fd8f6SDimitry Andric /* Delete */ true);
1778f0fd8f6SDimitry Andric ::operator delete(HungOffOperandList);
1787d523365SDimitry Andric } else if (Obj->HasDescriptor) {
1797d523365SDimitry Andric Use *UseBegin = static_cast<Use *>(Usr) - Obj->NumUserOperands;
1807d523365SDimitry Andric Use::zap(UseBegin, UseBegin + Obj->NumUserOperands, /* Delete */ false);
1817d523365SDimitry Andric
1827d523365SDimitry Andric auto *DI = reinterpret_cast<DescriptorInfo *>(UseBegin) - 1;
1837d523365SDimitry Andric uint8_t *Storage = reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes;
1847d523365SDimitry Andric ::operator delete(Storage);
1858f0fd8f6SDimitry Andric } else {
1868f0fd8f6SDimitry Andric Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;
1878f0fd8f6SDimitry Andric Use::zap(Storage, Storage + Obj->NumUserOperands,
1888f0fd8f6SDimitry Andric /* Delete */ false);
189139f7f9bSDimitry Andric ::operator delete(Storage);
190139f7f9bSDimitry Andric }
1918f0fd8f6SDimitry Andric }
192139f7f9bSDimitry Andric
1933dac3a9bSDimitry Andric } // End llvm namespace
194