220 lines · cpp
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-exception6//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 14using namespace llvm;15 16namespace llvm {17class BasicBlock;18}19 20//===----------------------------------------------------------------------===//21// User Class22//===----------------------------------------------------------------------===//23 24bool User::replaceUsesOfWith(Value *From, Value *To) {25 bool Changed = false;26 if (From == To) return Changed; // Duh what?27 28 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&29 "Cannot call User::replaceUsesOfWith on a constant!");30 31 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)32 if (getOperand(i) == From) { // Is This operand is pointing to oldval?33 // The side effects of this setOperand call include linking to34 // "To", adding "this" to the uses list of To, and35 // most importantly, removing "this" from the use list of "From".36 setOperand(i, To);37 Changed = true;38 }39 if (auto DVI = dyn_cast_or_null<DbgVariableIntrinsic>(this)) {40 if (is_contained(DVI->location_ops(), From)) {41 DVI->replaceVariableLocationOp(From, To);42 Changed = true;43 }44 }45 46 return Changed;47}48 49//===----------------------------------------------------------------------===//50// User allocHungoffUses Implementation51//===----------------------------------------------------------------------===//52 53void User::allocHungoffUses(unsigned N, bool IsPhi) {54 assert(HasHungOffUses && "alloc must have hung off uses");55 56 static_assert(alignof(Use) >= alignof(BasicBlock *),57 "Alignment is insufficient for 'hung-off-uses' pieces");58 59 // Allocate the array of Uses60 size_t size = N * sizeof(Use);61 if (IsPhi)62 size += N * sizeof(BasicBlock *);63 Use *Begin = static_cast<Use*>(::operator new(size));64 Use *End = Begin + N;65 setOperandList(Begin);66 for (; Begin != End; Begin++)67 new (Begin) Use(this);68}69 70void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {71 assert(HasHungOffUses && "realloc must have hung off uses");72 73 unsigned OldNumUses = getNumOperands();74 75 // We don't support shrinking the number of uses. We wouldn't have enough76 // space to copy the old uses in to the new space.77 assert(NewNumUses > OldNumUses && "realloc must grow num uses");78 79 Use *OldOps = getOperandList();80 allocHungoffUses(NewNumUses, IsPhi);81 Use *NewOps = getOperandList();82 83 // Now copy from the old operands list to the new one.84 std::copy(OldOps, OldOps + OldNumUses, NewOps);85 86 // If this is a Phi, then we need to copy the BB pointers too.87 if (IsPhi) {88 auto *OldPtr = reinterpret_cast<char *>(OldOps + OldNumUses);89 auto *NewPtr = reinterpret_cast<char *>(NewOps + NewNumUses);90 std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);91 }92 Use::zap(OldOps, OldOps + OldNumUses, true);93}94 95 96// This is a private struct used by `User` to track the co-allocated descriptor97// section.98struct DescriptorInfo {99 intptr_t SizeInBytes;100};101 102ArrayRef<const uint8_t> User::getDescriptor() const {103 auto MutableARef = const_cast<User *>(this)->getDescriptor();104 return {MutableARef.begin(), MutableARef.end()};105}106 107MutableArrayRef<uint8_t> User::getDescriptor() {108 assert(HasDescriptor && "Don't call otherwise!");109 assert(!HasHungOffUses && "Invariant!");110 111 auto *DI = reinterpret_cast<DescriptorInfo *>(getIntrusiveOperands()) - 1;112 assert(DI->SizeInBytes != 0 && "Should not have had a descriptor otherwise!");113 114 return MutableArrayRef<uint8_t>(115 reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes, DI->SizeInBytes);116}117 118bool User::isDroppable() const {119 if (auto *II = dyn_cast<IntrinsicInst>(this)) {120 switch (II->getIntrinsicID()) {121 default:122 return false;123 case Intrinsic::assume:124 case Intrinsic::pseudoprobe:125 case Intrinsic::experimental_noalias_scope_decl:126 return true;127 }128 }129 return false;130}131 132//===----------------------------------------------------------------------===//133// User operator new Implementations134//===----------------------------------------------------------------------===//135 136void *User::allocateFixedOperandUser(size_t Size, unsigned Us,137 unsigned DescBytes) {138 assert(Us < (1u << NumUserOperandsBits) && "Too many operands");139 140 static_assert(sizeof(DescriptorInfo) % sizeof(void *) == 0, "Required below");141 142 unsigned DescBytesToAllocate =143 DescBytes == 0 ? 0 : (DescBytes + sizeof(DescriptorInfo));144 assert(DescBytesToAllocate % sizeof(void *) == 0 &&145 "We need this to satisfy alignment constraints for Uses");146 147 uint8_t *Storage = static_cast<uint8_t *>(148 ::operator new(Size + sizeof(Use) * Us + DescBytesToAllocate));149 Use *Start = reinterpret_cast<Use *>(Storage + DescBytesToAllocate);150 Use *End = Start + Us;151 User *Obj = reinterpret_cast<User *>(End);152 Obj->NumUserOperands = Us;153 Obj->HasHungOffUses = false;154 Obj->HasDescriptor = DescBytes != 0;155 for (; Start != End; Start++)156 new (Start) Use(Obj);157 158 if (DescBytes != 0) {159 auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes);160 DescInfo->SizeInBytes = DescBytes;161 }162 163 return Obj;164}165 166void *User::operator new(size_t Size, IntrusiveOperandsAllocMarker allocTrait) {167 return allocateFixedOperandUser(Size, allocTrait.NumOps, 0);168}169 170void *User::operator new(size_t Size,171 IntrusiveOperandsAndDescriptorAllocMarker allocTrait) {172 return allocateFixedOperandUser(Size, allocTrait.NumOps,173 allocTrait.DescBytes);174}175 176void *User::operator new(size_t Size, HungOffOperandsAllocMarker) {177 // Allocate space for a single Use*178 void *Storage = ::operator new(Size + sizeof(Use *));179 Use **HungOffOperandList = static_cast<Use **>(Storage);180 User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1);181 Obj->NumUserOperands = 0;182 Obj->HasHungOffUses = true;183 Obj->HasDescriptor = false;184 *HungOffOperandList = nullptr;185 return Obj;186}187 188//===----------------------------------------------------------------------===//189// User operator delete Implementation190//===----------------------------------------------------------------------===//191 192// Repress memory sanitization, due to use-after-destroy by operator193// delete. Bug report 24578 identifies this issue.194LLVM_NO_SANITIZE_MEMORY_ATTRIBUTE void User::operator delete(void *Usr) {195 // Hung off uses use a single Use* before the User, while other subclasses196 // use a Use[] allocated prior to the user.197 User *Obj = static_cast<User *>(Usr);198 if (Obj->HasHungOffUses) {199 assert(!Obj->HasDescriptor && "not supported!");200 201 Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;202 // drop the hung off uses.203 Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,204 /* Delete */ true);205 ::operator delete(HungOffOperandList);206 } else if (Obj->HasDescriptor) {207 Use *UseBegin = static_cast<Use *>(Usr) - Obj->NumUserOperands;208 Use::zap(UseBegin, UseBegin + Obj->NumUserOperands, /* Delete */ false);209 210 auto *DI = reinterpret_cast<DescriptorInfo *>(UseBegin) - 1;211 uint8_t *Storage = reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes;212 ::operator delete(Storage);213 } else {214 Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;215 Use::zap(Storage, Storage + Obj->NumUserOperands,216 /* Delete */ false);217 ::operator delete(Storage);218 }219}220