1 //===-- User.cpp - Implement the User class -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/IR/User.h"
11 #include "llvm/IR/Constant.h"
12 #include "llvm/IR/GlobalValue.h"
13 #include "llvm/IR/Operator.h"
18 //===----------------------------------------------------------------------===//
20 //===----------------------------------------------------------------------===//
22 void User::anchor() {}
24 void User::replaceUsesOfWith(Value *From, Value *To) {
25 if (From == To) return; // Duh what?
27 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
28 "Cannot call User::replaceUsesOfWith on a constant!");
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...
39 //===----------------------------------------------------------------------===//
40 // User allocHungoffUses Implementation
41 //===----------------------------------------------------------------------===//
43 void User::allocHungoffUses(unsigned N, bool IsPhi) {
44 // Allocate the array of Uses, followed by a pointer (with bottom bit set) to
46 size_t size = N * sizeof(Use) + sizeof(Use::UserRef);
48 size += N * sizeof(BasicBlock *);
49 Use *Begin = static_cast<Use*>(::operator new(size));
51 (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
52 setOperandList(Use::initTags(Begin, End));
53 // Tag this operand list as being a hung off.
54 HasHungOffUses = true;
57 void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
58 assert(HasHungOffUses && "realloc must have hung off uses");
60 unsigned OldNumUses = getNumOperands();
62 // We don't support shrinking the number of uses. We wouldn't have enough
63 // space to copy the old uses in to the new space.
64 assert(NewNumUses > OldNumUses && "realloc must grow num uses");
66 Use *OldOps = getOperandList();
67 allocHungoffUses(NewNumUses, IsPhi);
68 Use *NewOps = getOperandList();
70 // Now copy from the old operands list to the new one.
71 std::copy(OldOps, OldOps + OldNumUses, NewOps);
73 // If this is a Phi, then we need to copy the BB pointers too.
76 reinterpret_cast<char *>(OldOps + OldNumUses) + sizeof(Use::UserRef);
78 reinterpret_cast<char *>(NewOps + NewNumUses) + sizeof(Use::UserRef);
79 std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
81 Use::zap(OldOps, OldOps + OldNumUses, true);
84 //===----------------------------------------------------------------------===//
85 // User operator new Implementations
86 //===----------------------------------------------------------------------===//
88 void *User::operator new(size_t s, unsigned Us) {
89 assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
90 void *Storage = ::operator new(s + sizeof(Use) * Us);
91 Use *Start = static_cast<Use*>(Storage);
92 Use *End = Start + Us;
93 User *Obj = reinterpret_cast<User*>(End);
94 Obj->setOperandList(Start);
95 Obj->HasHungOffUses = false;
96 Obj->NumUserOperands = Us;
97 Use::initTags(Start, End);
101 //===----------------------------------------------------------------------===//
102 // User operator delete Implementation
103 //===----------------------------------------------------------------------===//
105 void User::operator delete(void *Usr) {
106 User *Start = static_cast<User*>(Usr);
107 Use *Storage = static_cast<Use*>(Usr) - Start->NumUserOperands;
108 // If there were hung-off uses, they will have been freed already and
109 // NumOperands reset to 0, so here we just free the User itself.
110 ::operator delete(Storage);
113 //===----------------------------------------------------------------------===//
115 //===----------------------------------------------------------------------===//
117 Operator::~Operator() {
118 llvm_unreachable("should never destroy an Operator");
121 } // End llvm namespace