1 //===-- User.cpp - Implement the User class -------------------------------===// 2 // 3 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/IR/User.h" 10 #include "llvm/IR/Constant.h" 11 #include "llvm/IR/GlobalValue.h" 12 #include "llvm/IR/IntrinsicInst.h" 13 14 namespace llvm { 15 class BasicBlock; 16 17 //===----------------------------------------------------------------------===// 18 // User Class 19 //===----------------------------------------------------------------------===// 20 21 void 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 40 void User::allocHungoffUses(unsigned N, bool IsPhi) { 41 assert(HasHungOffUses && "alloc must have hung off uses"); 42 43 static_assert(alignof(Use) >= alignof(Use::UserRef), 44 "Alignment is insufficient for 'hung-off-uses' pieces"); 45 static_assert(alignof(Use::UserRef) >= alignof(BasicBlock *), 46 "Alignment is insufficient for 'hung-off-uses' pieces"); 47 48 // 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); 51 if (IsPhi) 52 size += N * sizeof(BasicBlock *); 53 Use *Begin = static_cast<Use*>(::operator new(size)); 54 Use *End = Begin + N; 55 (void) new(End) Use::UserRef(const_cast<User*>(this), 1); 56 setOperandList(Use::initTags(Begin, End)); 57 } 58 59 void 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 68 Use *OldOps = getOperandList(); 69 allocHungoffUses(NewNumUses, IsPhi); 70 Use *NewOps = getOperandList(); 71 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 86 87 // This is a private struct used by `User` to track the co-allocated descriptor 88 // section. 89 struct DescriptorInfo { 90 intptr_t SizeInBytes; 91 }; 92 93 ArrayRef<const uint8_t> User::getDescriptor() const { 94 auto MutableARef = const_cast<User *>(this)->getDescriptor(); 95 return {MutableARef.begin(), MutableARef.end()}; 96 } 97 98 MutableArrayRef<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 109 bool User::isDroppable() const { 110 if (const auto *Intr = dyn_cast<IntrinsicInst>(this)) 111 return Intr->getIntrinsicID() == Intrinsic::assume; 112 return false; 113 } 114 115 //===----------------------------------------------------------------------===// 116 // User operator new Implementations 117 //===----------------------------------------------------------------------===// 118 119 void *User::allocateFixedOperandUser(size_t Size, unsigned Us, 120 unsigned DescBytes) { 121 assert(Us < (1u << NumUserOperandsBits) && "Too many operands"); 122 123 static_assert(sizeof(DescriptorInfo) % sizeof(void *) == 0, "Required below"); 124 125 unsigned DescBytesToAllocate = 126 DescBytes == 0 ? 0 : (DescBytes + sizeof(DescriptorInfo)); 127 assert(DescBytesToAllocate % sizeof(void *) == 0 && 128 "We need this to satisfy alignment constraints for Uses"); 129 130 uint8_t *Storage = static_cast<uint8_t *>( 131 ::operator new(Size + sizeof(Use) * Us + DescBytesToAllocate)); 132 Use *Start = reinterpret_cast<Use *>(Storage + DescBytesToAllocate); 133 Use *End = Start + Us; 134 User *Obj = reinterpret_cast<User*>(End); 135 Obj->NumUserOperands = Us; 136 Obj->HasHungOffUses = false; 137 Obj->HasDescriptor = DescBytes != 0; 138 Use::initTags(Start, End); 139 140 if (DescBytes != 0) { 141 auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes); 142 DescInfo->SizeInBytes = DescBytes; 143 } 144 145 return Obj; 146 } 147 148 void *User::operator new(size_t Size, unsigned Us) { 149 return allocateFixedOperandUser(Size, Us, 0); 150 } 151 152 void *User::operator new(size_t Size, unsigned Us, unsigned DescBytes) { 153 return allocateFixedOperandUser(Size, Us, DescBytes); 154 } 155 156 void *User::operator new(size_t Size) { 157 // Allocate space for a single Use* 158 void *Storage = ::operator new(Size + sizeof(Use *)); 159 Use **HungOffOperandList = static_cast<Use **>(Storage); 160 User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1); 161 Obj->NumUserOperands = 0; 162 Obj->HasHungOffUses = true; 163 Obj->HasDescriptor = false; 164 *HungOffOperandList = nullptr; 165 return Obj; 166 } 167 168 //===----------------------------------------------------------------------===// 169 // User operator delete Implementation 170 //===----------------------------------------------------------------------===// 171 172 // Repress memory sanitization, due to use-after-destroy by operator 173 // delete. Bug report 24578 identifies this issue. 174 LLVM_NO_SANITIZE_MEMORY_ATTRIBUTE void User::operator delete(void *Usr) { 175 // Hung off uses use a single Use* before the User, while other subclasses 176 // use a Use[] allocated prior to the user. 177 User *Obj = static_cast<User *>(Usr); 178 if (Obj->HasHungOffUses) { 179 assert(!Obj->HasDescriptor && "not supported!"); 180 181 Use **HungOffOperandList = static_cast<Use **>(Usr) - 1; 182 // drop the hung off uses. 183 Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands, 184 /* Delete */ true); 185 ::operator delete(HungOffOperandList); 186 } else if (Obj->HasDescriptor) { 187 Use *UseBegin = static_cast<Use *>(Usr) - Obj->NumUserOperands; 188 Use::zap(UseBegin, UseBegin + Obj->NumUserOperands, /* Delete */ false); 189 190 auto *DI = reinterpret_cast<DescriptorInfo *>(UseBegin) - 1; 191 uint8_t *Storage = reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes; 192 ::operator delete(Storage); 193 } else { 194 Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands; 195 Use::zap(Storage, Storage + Obj->NumUserOperands, 196 /* Delete */ false); 197 ::operator delete(Storage); 198 } 199 } 200 201 } // End llvm namespace 202