1 //===-- User.cpp - Implement the User class -------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Constant.h"
11 #include "llvm/GlobalValue.h"
12 #include "llvm/User.h"
13
14 namespace llvm {
15
16 //===----------------------------------------------------------------------===//
17 // User Class
18 //===----------------------------------------------------------------------===//
19
anchor()20 void User::anchor() {}
21
22 // replaceUsesOfWith - Replaces all references to the "From" definition with
23 // references to the "To" definition.
24 //
replaceUsesOfWith(Value * From,Value * To)25 void User::replaceUsesOfWith(Value *From, Value *To) {
26 if (From == To) return; // 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 to
34 // "To", adding "this" to the uses list of To, and
35 // most importantly, removing "this" from the use list of "From".
36 setOperand(i, To); // Fix it now...
37 }
38 }
39
40 //===----------------------------------------------------------------------===//
41 // User allocHungoffUses Implementation
42 //===----------------------------------------------------------------------===//
43
allocHungoffUses(unsigned N) const44 Use *User::allocHungoffUses(unsigned N) const {
45 // Allocate the array of Uses, followed by a pointer (with bottom bit set) to
46 // the User.
47 size_t size = N * sizeof(Use) + sizeof(Use::UserRef);
48 Use *Begin = static_cast<Use*>(::operator new(size));
49 Use *End = Begin + N;
50 (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
51 return Use::initTags(Begin, End);
52 }
53
54 //===----------------------------------------------------------------------===//
55 // User operator new Implementations
56 //===----------------------------------------------------------------------===//
57
operator new(size_t s,unsigned Us)58 void *User::operator new(size_t s, unsigned Us) {
59 void *Storage = ::operator new(s + sizeof(Use) * Us);
60 Use *Start = static_cast<Use*>(Storage);
61 Use *End = Start + Us;
62 User *Obj = reinterpret_cast<User*>(End);
63 Obj->OperandList = Start;
64 Obj->NumOperands = Us;
65 Use::initTags(Start, End);
66 return Obj;
67 }
68
69 //===----------------------------------------------------------------------===//
70 // User operator delete Implementation
71 //===----------------------------------------------------------------------===//
72
operator delete(void * Usr)73 void User::operator delete(void *Usr) {
74 User *Start = static_cast<User*>(Usr);
75 Use *Storage = static_cast<Use*>(Usr) - Start->NumOperands;
76 // If there were hung-off uses, they will have been freed already and
77 // NumOperands reset to 0, so here we just free the User itself.
78 ::operator delete(Storage);
79 }
80
81 } // End llvm namespace
82