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/Constant.h"
11 #include "llvm/GlobalValue.h"
12 #include "llvm/User.h"
16 //===----------------------------------------------------------------------===//
18 //===----------------------------------------------------------------------===//
20 // replaceUsesOfWith - Replaces all references to the "From" definition with
21 // references to the "To" definition.
23 void User::replaceUsesOfWith(Value *From, Value *To) {
24 if (From == To) return; // Duh what?
26 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
27 "Cannot call User::replaceUsesOfWith on a constant!");
29 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
30 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
31 // The side effects of this setOperand call include linking to
32 // "To", adding "this" to the uses list of To, and
33 // most importantly, removing "this" from the use list of "From".
34 setOperand(i, To); // Fix it now...
38 //===----------------------------------------------------------------------===//
39 // User allocHungoffUses Implementation
40 //===----------------------------------------------------------------------===//
42 Use *User::allocHungoffUses(unsigned N) const {
43 Use *Begin = static_cast<Use*>(::operator new(sizeof(Use) * N
44 + sizeof(AugmentedUse)
47 PointerIntPair<User*, 1, Tag>& ref(static_cast<AugmentedUse&>(End[-1]).ref);
48 ref.setPointer(const_cast<User*>(this));
50 return Use::initTags(Begin, End);
53 //===----------------------------------------------------------------------===//
54 // User operator new Implementations
55 //===----------------------------------------------------------------------===//
57 void *User::operator new(size_t s, unsigned Us) {
58 void *Storage = ::operator new(s + sizeof(Use) * Us);
59 Use *Start = static_cast<Use*>(Storage);
60 Use *End = Start + Us;
61 User *Obj = reinterpret_cast<User*>(End);
62 Obj->OperandList = Start;
63 Obj->NumOperands = Us;
64 Use::initTags(Start, End);
68 //===----------------------------------------------------------------------===//
69 // User operator delete Implementation
70 //===----------------------------------------------------------------------===//
72 void User::operator delete(void *Usr) {
73 User *Start = static_cast<User*>(Usr);
74 Use *Storage = static_cast<Use*>(Usr) - Start->NumOperands;
76 // look for a variadic User
77 if (Storage == Start->OperandList) {
78 ::operator delete(Storage);
82 // in all other cases just delete the nullary User (covers hung-off
84 ::operator delete(Usr);
87 } // End llvm namespace