xref: /openbsd/gnu/llvm/llvm/lib/IR/User.cpp (revision d415bd75)
109467b48Spatrick //===-- User.cpp - Implement the User class -------------------------------===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick 
909467b48Spatrick #include "llvm/IR/User.h"
1009467b48Spatrick #include "llvm/IR/Constant.h"
1109467b48Spatrick #include "llvm/IR/GlobalValue.h"
12097a140dSpatrick #include "llvm/IR/IntrinsicInst.h"
1309467b48Spatrick 
1409467b48Spatrick namespace llvm {
1509467b48Spatrick class BasicBlock;
1609467b48Spatrick 
1709467b48Spatrick //===----------------------------------------------------------------------===//
1809467b48Spatrick //                                 User Class
1909467b48Spatrick //===----------------------------------------------------------------------===//
2009467b48Spatrick 
replaceUsesOfWith(Value * From,Value * To)21*d415bd75Srobert bool User::replaceUsesOfWith(Value *From, Value *To) {
22*d415bd75Srobert   bool Changed = false;
23*d415bd75Srobert   if (From == To) return Changed;   // Duh what?
2409467b48Spatrick 
2509467b48Spatrick   assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
2609467b48Spatrick          "Cannot call User::replaceUsesOfWith on a constant!");
2709467b48Spatrick 
2809467b48Spatrick   for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
2909467b48Spatrick     if (getOperand(i) == From) {  // Is This operand is pointing to oldval?
3009467b48Spatrick       // The side effects of this setOperand call include linking to
3109467b48Spatrick       // "To", adding "this" to the uses list of To, and
3209467b48Spatrick       // most importantly, removing "this" from the use list of "From".
3373471bf0Spatrick       setOperand(i, To);
34*d415bd75Srobert       Changed = true;
3573471bf0Spatrick     }
3673471bf0Spatrick   if (auto DVI = dyn_cast_or_null<DbgVariableIntrinsic>(this)) {
37*d415bd75Srobert     if (is_contained(DVI->location_ops(), From)) {
3873471bf0Spatrick       DVI->replaceVariableLocationOp(From, To);
39*d415bd75Srobert       Changed = true;
4009467b48Spatrick     }
4109467b48Spatrick   }
4209467b48Spatrick 
43*d415bd75Srobert   return Changed;
44*d415bd75Srobert }
45*d415bd75Srobert 
4609467b48Spatrick //===----------------------------------------------------------------------===//
4709467b48Spatrick //                         User allocHungoffUses Implementation
4809467b48Spatrick //===----------------------------------------------------------------------===//
4909467b48Spatrick 
allocHungoffUses(unsigned N,bool IsPhi)5009467b48Spatrick void User::allocHungoffUses(unsigned N, bool IsPhi) {
5109467b48Spatrick   assert(HasHungOffUses && "alloc must have hung off uses");
5209467b48Spatrick 
53097a140dSpatrick   static_assert(alignof(Use) >= alignof(BasicBlock *),
5409467b48Spatrick                 "Alignment is insufficient for 'hung-off-uses' pieces");
5509467b48Spatrick 
56097a140dSpatrick   // Allocate the array of Uses
57097a140dSpatrick   size_t size = N * sizeof(Use);
5809467b48Spatrick   if (IsPhi)
5909467b48Spatrick     size += N * sizeof(BasicBlock *);
6009467b48Spatrick   Use *Begin = static_cast<Use*>(::operator new(size));
6109467b48Spatrick   Use *End = Begin + N;
62097a140dSpatrick   setOperandList(Begin);
63097a140dSpatrick   for (; Begin != End; Begin++)
64097a140dSpatrick     new (Begin) Use(this);
6509467b48Spatrick }
6609467b48Spatrick 
growHungoffUses(unsigned NewNumUses,bool IsPhi)6709467b48Spatrick void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
6809467b48Spatrick   assert(HasHungOffUses && "realloc must have hung off uses");
6909467b48Spatrick 
7009467b48Spatrick   unsigned OldNumUses = getNumOperands();
7109467b48Spatrick 
7209467b48Spatrick   // We don't support shrinking the number of uses.  We wouldn't have enough
7309467b48Spatrick   // space to copy the old uses in to the new space.
7409467b48Spatrick   assert(NewNumUses > OldNumUses && "realloc must grow num uses");
7509467b48Spatrick 
7609467b48Spatrick   Use *OldOps = getOperandList();
7709467b48Spatrick   allocHungoffUses(NewNumUses, IsPhi);
7809467b48Spatrick   Use *NewOps = getOperandList();
7909467b48Spatrick 
8009467b48Spatrick   // Now copy from the old operands list to the new one.
8109467b48Spatrick   std::copy(OldOps, OldOps + OldNumUses, NewOps);
8209467b48Spatrick 
8309467b48Spatrick   // If this is a Phi, then we need to copy the BB pointers too.
8409467b48Spatrick   if (IsPhi) {
85097a140dSpatrick     auto *OldPtr = reinterpret_cast<char *>(OldOps + OldNumUses);
86097a140dSpatrick     auto *NewPtr = reinterpret_cast<char *>(NewOps + NewNumUses);
8709467b48Spatrick     std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
8809467b48Spatrick   }
8909467b48Spatrick   Use::zap(OldOps, OldOps + OldNumUses, true);
9009467b48Spatrick }
9109467b48Spatrick 
9209467b48Spatrick 
9309467b48Spatrick // This is a private struct used by `User` to track the co-allocated descriptor
9409467b48Spatrick // section.
9509467b48Spatrick struct DescriptorInfo {
9609467b48Spatrick   intptr_t SizeInBytes;
9709467b48Spatrick };
9809467b48Spatrick 
getDescriptor() const9909467b48Spatrick ArrayRef<const uint8_t> User::getDescriptor() const {
10009467b48Spatrick   auto MutableARef = const_cast<User *>(this)->getDescriptor();
10109467b48Spatrick   return {MutableARef.begin(), MutableARef.end()};
10209467b48Spatrick }
10309467b48Spatrick 
getDescriptor()10409467b48Spatrick MutableArrayRef<uint8_t> User::getDescriptor() {
10509467b48Spatrick   assert(HasDescriptor && "Don't call otherwise!");
10609467b48Spatrick   assert(!HasHungOffUses && "Invariant!");
10709467b48Spatrick 
10809467b48Spatrick   auto *DI = reinterpret_cast<DescriptorInfo *>(getIntrusiveOperands()) - 1;
10909467b48Spatrick   assert(DI->SizeInBytes != 0 && "Should not have had a descriptor otherwise!");
11009467b48Spatrick 
11109467b48Spatrick   return MutableArrayRef<uint8_t>(
11209467b48Spatrick       reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes, DI->SizeInBytes);
11309467b48Spatrick }
11409467b48Spatrick 
isDroppable() const115097a140dSpatrick bool User::isDroppable() const {
116*d415bd75Srobert   return isa<AssumeInst>(this) || isa<PseudoProbeInst>(this);
117097a140dSpatrick }
118097a140dSpatrick 
11909467b48Spatrick //===----------------------------------------------------------------------===//
12009467b48Spatrick //                         User operator new Implementations
12109467b48Spatrick //===----------------------------------------------------------------------===//
12209467b48Spatrick 
allocateFixedOperandUser(size_t Size,unsigned Us,unsigned DescBytes)12309467b48Spatrick void *User::allocateFixedOperandUser(size_t Size, unsigned Us,
12409467b48Spatrick                                      unsigned DescBytes) {
12509467b48Spatrick   assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
12609467b48Spatrick 
12709467b48Spatrick   static_assert(sizeof(DescriptorInfo) % sizeof(void *) == 0, "Required below");
12809467b48Spatrick 
12909467b48Spatrick   unsigned DescBytesToAllocate =
13009467b48Spatrick       DescBytes == 0 ? 0 : (DescBytes + sizeof(DescriptorInfo));
13109467b48Spatrick   assert(DescBytesToAllocate % sizeof(void *) == 0 &&
13209467b48Spatrick          "We need this to satisfy alignment constraints for Uses");
13309467b48Spatrick 
13409467b48Spatrick   uint8_t *Storage = static_cast<uint8_t *>(
13509467b48Spatrick       ::operator new(Size + sizeof(Use) * Us + DescBytesToAllocate));
13609467b48Spatrick   Use *Start = reinterpret_cast<Use *>(Storage + DescBytesToAllocate);
13709467b48Spatrick   Use *End = Start + Us;
13809467b48Spatrick   User *Obj = reinterpret_cast<User*>(End);
13909467b48Spatrick   Obj->NumUserOperands = Us;
14009467b48Spatrick   Obj->HasHungOffUses = false;
14109467b48Spatrick   Obj->HasDescriptor = DescBytes != 0;
142097a140dSpatrick   for (; Start != End; Start++)
143097a140dSpatrick     new (Start) Use(Obj);
14409467b48Spatrick 
14509467b48Spatrick   if (DescBytes != 0) {
14609467b48Spatrick     auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes);
14709467b48Spatrick     DescInfo->SizeInBytes = DescBytes;
14809467b48Spatrick   }
14909467b48Spatrick 
15009467b48Spatrick   return Obj;
15109467b48Spatrick }
15209467b48Spatrick 
operator new(size_t Size,unsigned Us)15309467b48Spatrick void *User::operator new(size_t Size, unsigned Us) {
15409467b48Spatrick   return allocateFixedOperandUser(Size, Us, 0);
15509467b48Spatrick }
15609467b48Spatrick 
operator new(size_t Size,unsigned Us,unsigned DescBytes)15709467b48Spatrick void *User::operator new(size_t Size, unsigned Us, unsigned DescBytes) {
15809467b48Spatrick   return allocateFixedOperandUser(Size, Us, DescBytes);
15909467b48Spatrick }
16009467b48Spatrick 
operator new(size_t Size)16109467b48Spatrick void *User::operator new(size_t Size) {
16209467b48Spatrick   // Allocate space for a single Use*
16309467b48Spatrick   void *Storage = ::operator new(Size + sizeof(Use *));
16409467b48Spatrick   Use **HungOffOperandList = static_cast<Use **>(Storage);
16509467b48Spatrick   User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1);
16609467b48Spatrick   Obj->NumUserOperands = 0;
16709467b48Spatrick   Obj->HasHungOffUses = true;
16809467b48Spatrick   Obj->HasDescriptor = false;
16909467b48Spatrick   *HungOffOperandList = nullptr;
17009467b48Spatrick   return Obj;
17109467b48Spatrick }
17209467b48Spatrick 
17309467b48Spatrick //===----------------------------------------------------------------------===//
17409467b48Spatrick //                         User operator delete Implementation
17509467b48Spatrick //===----------------------------------------------------------------------===//
17609467b48Spatrick 
17709467b48Spatrick // Repress memory sanitization, due to use-after-destroy by operator
17809467b48Spatrick // delete. Bug report 24578 identifies this issue.
operator delete(void * Usr)17909467b48Spatrick LLVM_NO_SANITIZE_MEMORY_ATTRIBUTE void User::operator delete(void *Usr) {
18009467b48Spatrick   // Hung off uses use a single Use* before the User, while other subclasses
18109467b48Spatrick   // use a Use[] allocated prior to the user.
18209467b48Spatrick   User *Obj = static_cast<User *>(Usr);
18309467b48Spatrick   if (Obj->HasHungOffUses) {
18409467b48Spatrick     assert(!Obj->HasDescriptor && "not supported!");
18509467b48Spatrick 
18609467b48Spatrick     Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
18709467b48Spatrick     // drop the hung off uses.
18809467b48Spatrick     Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,
18909467b48Spatrick              /* Delete */ true);
19009467b48Spatrick     ::operator delete(HungOffOperandList);
19109467b48Spatrick   } else if (Obj->HasDescriptor) {
19209467b48Spatrick     Use *UseBegin = static_cast<Use *>(Usr) - Obj->NumUserOperands;
19309467b48Spatrick     Use::zap(UseBegin, UseBegin + Obj->NumUserOperands, /* Delete */ false);
19409467b48Spatrick 
19509467b48Spatrick     auto *DI = reinterpret_cast<DescriptorInfo *>(UseBegin) - 1;
19609467b48Spatrick     uint8_t *Storage = reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes;
19709467b48Spatrick     ::operator delete(Storage);
19809467b48Spatrick   } else {
19909467b48Spatrick     Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;
20009467b48Spatrick     Use::zap(Storage, Storage + Obj->NumUserOperands,
20109467b48Spatrick              /* Delete */ false);
20209467b48Spatrick     ::operator delete(Storage);
20309467b48Spatrick   }
20409467b48Spatrick }
20509467b48Spatrick 
206097a140dSpatrick } // namespace llvm
207