Fix regular expression.
[llvm.git] / lib / VMCore / Use.cpp
blobb25415a3d14e0e20e55d65c0e72287c665cf3bd9
1 //===-- Use.cpp - Implement the Use 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 // This file implements the algorithm for finding the User of a Use.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/User.h"
16 namespace llvm {
18 //===----------------------------------------------------------------------===//
19 // Use swap Implementation
20 //===----------------------------------------------------------------------===//
22 void Use::swap(Use &RHS) {
23 Value *V1(Val);
24 Value *V2(RHS.Val);
25 if (V1 != V2) {
26 if (V1) {
27 removeFromList();
30 if (V2) {
31 RHS.removeFromList();
32 Val = V2;
33 V2->addUse(*this);
34 } else {
35 Val = 0;
38 if (V1) {
39 RHS.Val = V1;
40 V1->addUse(RHS);
41 } else {
42 RHS.Val = 0;
47 //===----------------------------------------------------------------------===//
48 // Use getImpliedUser Implementation
49 //===----------------------------------------------------------------------===//
51 const Use *Use::getImpliedUser() const {
52 const Use *Current = this;
54 while (true) {
55 unsigned Tag = (Current++)->Prev.getInt();
56 switch (Tag) {
57 case zeroDigitTag:
58 case oneDigitTag:
59 continue;
61 case stopTag: {
62 ++Current;
63 ptrdiff_t Offset = 1;
64 while (true) {
65 unsigned Tag = Current->Prev.getInt();
66 switch (Tag) {
67 case zeroDigitTag:
68 case oneDigitTag:
69 ++Current;
70 Offset = (Offset << 1) + Tag;
71 continue;
72 default:
73 return Current + Offset;
78 case fullStopTag:
79 return Current;
84 //===----------------------------------------------------------------------===//
85 // Use initTags Implementation
86 //===----------------------------------------------------------------------===//
88 Use *Use::initTags(Use * const Start, Use *Stop, ptrdiff_t Done) {
89 ptrdiff_t Count = Done;
90 while (Start != Stop) {
91 --Stop;
92 Stop->Val = 0;
93 if (!Count) {
94 Stop->Prev.setFromOpaqueValue(reinterpret_cast<Use**>(Done == 0
95 ? fullStopTag
96 : stopTag));
97 ++Done;
98 Count = Done;
99 } else {
100 Stop->Prev.setFromOpaqueValue(reinterpret_cast<Use**>(Count & 1));
101 Count >>= 1;
102 ++Done;
106 return Start;
109 //===----------------------------------------------------------------------===//
110 // Use zap Implementation
111 //===----------------------------------------------------------------------===//
113 void Use::zap(Use *Start, const Use *Stop, bool del) {
114 if (del) {
115 while (Start != Stop) {
116 (--Stop)->~Use();
118 ::operator delete(Start);
119 return;
122 while (Start != Stop) {
123 (Start++)->set(0);
127 //===----------------------------------------------------------------------===//
128 // AugmentedUse layout struct
129 //===----------------------------------------------------------------------===//
131 struct AugmentedUse : Use {
132 PointerIntPair<User*, 1, Tag> ref;
133 AugmentedUse(); // not implemented
137 //===----------------------------------------------------------------------===//
138 // Use getUser Implementation
139 //===----------------------------------------------------------------------===//
141 User *Use::getUser() const {
142 const Use *End = getImpliedUser();
143 const PointerIntPair<User*, 1, Tag>& ref(
144 static_cast<const AugmentedUse*>(End - 1)->ref);
145 User *She = ref.getPointer();
146 return ref.getInt()
147 ? She
148 : (User*)End;
151 //===----------------------------------------------------------------------===//
152 // User allocHungoffUses Implementation
153 //===----------------------------------------------------------------------===//
155 Use *User::allocHungoffUses(unsigned N) const {
156 Use *Begin = static_cast<Use*>(::operator new(sizeof(Use) * N
157 + sizeof(AugmentedUse)
158 - sizeof(Use)));
159 Use *End = Begin + N;
160 PointerIntPair<User*, 1, Tag>& ref(static_cast<AugmentedUse&>(End[-1]).ref);
161 ref.setPointer(const_cast<User*>(this));
162 ref.setInt(tagOne);
163 return Use::initTags(Begin, End);
166 //===----------------------------------------------------------------------===//
167 // User operator new Implementations
168 //===----------------------------------------------------------------------===//
170 void *User::operator new(size_t s, unsigned Us) {
171 void *Storage = ::operator new(s + sizeof(Use) * Us);
172 Use *Start = static_cast<Use*>(Storage);
173 Use *End = Start + Us;
174 User *Obj = reinterpret_cast<User*>(End);
175 Obj->OperandList = Start;
176 Obj->NumOperands = Us;
177 Use::initTags(Start, End);
178 return Obj;
181 /// Prefixed allocation - just before the first Use, allocate a NULL pointer.
182 /// The destructor can detect its presence and readjust the OperandList
183 /// for deletition.
185 void *User::operator new(size_t s, unsigned Us, bool Prefix) {
186 // currently prefixed allocation only admissible for
187 // unconditional branch instructions
188 if (!Prefix)
189 return operator new(s, Us);
191 assert(Us == 1 && "Other than one Use allocated?");
192 typedef PointerIntPair<void*, 2, Use::PrevPtrTag> TaggedPrefix;
193 void *Raw = ::operator new(s + sizeof(TaggedPrefix) + sizeof(Use) * Us);
194 TaggedPrefix *Pre = static_cast<TaggedPrefix*>(Raw);
195 Pre->setFromOpaqueValue(0);
196 void *Storage = Pre + 1; // skip over prefix
197 Use *Start = static_cast<Use*>(Storage);
198 Use *End = Start + Us;
199 User *Obj = reinterpret_cast<User*>(End);
200 Obj->OperandList = Start;
201 Obj->NumOperands = Us;
202 Use::initTags(Start, End);
203 return Obj;
206 //===----------------------------------------------------------------------===//
207 // User operator delete Implementation
208 //===----------------------------------------------------------------------===//
210 void User::operator delete(void *Usr) {
211 User *Start = static_cast<User*>(Usr);
212 Use *Storage = static_cast<Use*>(Usr) - Start->NumOperands;
214 // look for a variadic User
215 if (Storage == Start->OperandList) {
216 ::operator delete(Storage);
217 return;
220 // check for the flag whether the destructor has detected a prefixed
221 // allocation, in which case we remove the flag and delete starting
222 // at OperandList
223 if (reinterpret_cast<intptr_t>(Start->OperandList) & 1) {
224 ::operator delete(reinterpret_cast<char*>(Start->OperandList) - 1);
225 return;
228 // in all other cases just delete the nullary User (covers hung-off
229 // uses also
230 ::operator delete(Usr);
233 } // End llvm namespace