blob: ba2b2081db57ebf460aec5fe270514e957b9fd37 [file] [log] [blame]
Jay Foad59809c72011-01-16 08:10:57 +00001//===-- 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
14namespace llvm {
15
16//===----------------------------------------------------------------------===//
17// User Class
18//===----------------------------------------------------------------------===//
19
20// replaceUsesOfWith - Replaces all references to the "From" definition with
21// references to the "To" definition.
22//
23void User::replaceUsesOfWith(Value *From, Value *To) {
24 if (From == To) return; // Duh what?
25
26 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
27 "Cannot call User::replaceUsesOfWith on a constant!");
28
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...
35 }
36}
37
38//===----------------------------------------------------------------------===//
39// User allocHungoffUses Implementation
40//===----------------------------------------------------------------------===//
41
42Use *User::allocHungoffUses(unsigned N) const {
43 Use *Begin = static_cast<Use*>(::operator new(sizeof(Use) * N
44 + sizeof(AugmentedUse)
45 - sizeof(Use)));
46 Use *End = Begin + N;
Jay Foadfe873642011-01-17 15:18:06 +000047 PointerIntPair<User*, 1, unsigned>&
48 ref(static_cast<AugmentedUse&>(End[-1]).ref);
Jay Foad59809c72011-01-16 08:10:57 +000049 ref.setPointer(const_cast<User*>(this));
Jay Foadfe873642011-01-17 15:18:06 +000050 ref.setInt(1);
Jay Foad59809c72011-01-16 08:10:57 +000051 return Use::initTags(Begin, End);
52}
53
54//===----------------------------------------------------------------------===//
55// User operator new Implementations
56//===----------------------------------------------------------------------===//
57
58void *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
73void User::operator delete(void *Usr) {
74 User *Start = static_cast<User*>(Usr);
75 Use *Storage = static_cast<Use*>(Usr) - Start->NumOperands;
76 //
77 // look for a variadic User
78 if (Storage == Start->OperandList) {
79 ::operator delete(Storage);
80 return;
81 }
82 //
83 // in all other cases just delete the nullary User (covers hung-off
84 // uses also
85 ::operator delete(Usr);
86}
87
88} // End llvm namespace