blob: c88ad16f60f771ed6f89dac74d5079722577f1b7 [file] [log] [blame]
Gabor Greif697e94c2008-05-15 10:04:30 +00001//===-- Use.cpp - Implement the Use class ---------------------------------===//
Gabor Greiff6caff662008-05-10 08:32:32 +00002//
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.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/User.h"
15
16namespace llvm {
17
18//===----------------------------------------------------------------------===//
Gabor Greif5ef74042008-05-13 22:51:52 +000019// Use swap Implementation
20//===----------------------------------------------------------------------===//
21
22void Use::swap(Use &RHS) {
Gabor Greif715b9d22008-09-19 15:13:20 +000023 Value *V1(Val);
24 Value *V2(RHS.Val);
Gabor Greif5ef74042008-05-13 22:51:52 +000025 if (V1 != V2) {
26 if (V1) {
27 removeFromList();
28 }
29
30 if (V2) {
31 RHS.removeFromList();
Gabor Greif715b9d22008-09-19 15:13:20 +000032 Val = V2;
Gabor Greif5ef74042008-05-13 22:51:52 +000033 V2->addUse(*this);
34 } else {
Gabor Greif715b9d22008-09-19 15:13:20 +000035 Val = 0;
Gabor Greif5ef74042008-05-13 22:51:52 +000036 }
37
38 if (V1) {
Gabor Greif715b9d22008-09-19 15:13:20 +000039 RHS.Val = V1;
Gabor Greif5ef74042008-05-13 22:51:52 +000040 V1->addUse(RHS);
41 } else {
Gabor Greif715b9d22008-09-19 15:13:20 +000042 RHS.Val = 0;
Gabor Greif5ef74042008-05-13 22:51:52 +000043 }
44 }
45}
46
47//===----------------------------------------------------------------------===//
Gabor Greiff6caff662008-05-10 08:32:32 +000048// Use getImpliedUser Implementation
49//===----------------------------------------------------------------------===//
50
51const Use *Use::getImpliedUser() const {
52 const Use *Current = this;
53
54 while (true) {
Gabor Greif2231c2c2009-01-05 16:05:32 +000055 unsigned Tag = (Current++)->Prev.getInt();
Gabor Greiff6caff662008-05-10 08:32:32 +000056 switch (Tag) {
57 case zeroDigitTag:
58 case oneDigitTag:
59 continue;
60
61 case stopTag: {
62 ++Current;
63 ptrdiff_t Offset = 1;
64 while (true) {
Gabor Greif2231c2c2009-01-05 16:05:32 +000065 unsigned Tag = Current->Prev.getInt();
Gabor Greiff6caff662008-05-10 08:32:32 +000066 switch (Tag) {
67 case zeroDigitTag:
68 case oneDigitTag:
69 ++Current;
70 Offset = (Offset << 1) + Tag;
71 continue;
72 default:
73 return Current + Offset;
74 }
75 }
76 }
77
78 case fullStopTag:
79 return Current;
80 }
81 }
82}
83
84//===----------------------------------------------------------------------===//
85// Use initTags Implementation
86//===----------------------------------------------------------------------===//
87
88Use *Use::initTags(Use * const Start, Use *Stop, ptrdiff_t Done) {
Gabor Greiffee4daf2010-07-16 20:35:19 +000089 while (Done < 6) {
90 if (Start == Stop--)
91 return Start;
92 static const PrevPtrTag tags[6] = { fullStopTag, oneDigitTag, stopTag,
93 oneDigitTag, oneDigitTag, stopTag };
94 Stop->Prev.setFromOpaqueValue(reinterpret_cast<Use**>(tags[Done++]));
95 Stop->Val = 0;
96 }
97
Gabor Greiff6caff662008-05-10 08:32:32 +000098 ptrdiff_t Count = Done;
99 while (Start != Stop) {
100 --Stop;
Gabor Greif715b9d22008-09-19 15:13:20 +0000101 Stop->Val = 0;
Gabor Greiff6caff662008-05-10 08:32:32 +0000102 if (!Count) {
Gabor Greif29609872010-07-17 20:52:46 +0000103 Stop->Prev.setFromOpaqueValue(reinterpret_cast<Use**>(stopTag));
Gabor Greiff6caff662008-05-10 08:32:32 +0000104 ++Done;
105 Count = Done;
106 } else {
Gabor Greif2231c2c2009-01-05 16:05:32 +0000107 Stop->Prev.setFromOpaqueValue(reinterpret_cast<Use**>(Count & 1));
Gabor Greiff6caff662008-05-10 08:32:32 +0000108 Count >>= 1;
109 ++Done;
110 }
111 }
112
113 return Start;
114}
115
116//===----------------------------------------------------------------------===//
117// Use zap Implementation
118//===----------------------------------------------------------------------===//
119
120void Use::zap(Use *Start, const Use *Stop, bool del) {
121 if (del) {
122 while (Start != Stop) {
123 (--Stop)->~Use();
124 }
125 ::operator delete(Start);
126 return;
127 }
128
129 while (Start != Stop) {
130 (Start++)->set(0);
131 }
132}
133
134//===----------------------------------------------------------------------===//
135// AugmentedUse layout struct
136//===----------------------------------------------------------------------===//
137
Duncan Sands0f5bbb52009-09-06 08:55:57 +0000138struct AugmentedUse : public Use {
Gabor Greif2231c2c2009-01-05 16:05:32 +0000139 PointerIntPair<User*, 1, Tag> ref;
Gabor Greiff6caff662008-05-10 08:32:32 +0000140 AugmentedUse(); // not implemented
141};
142
143
144//===----------------------------------------------------------------------===//
145// Use getUser Implementation
146//===----------------------------------------------------------------------===//
147
148User *Use::getUser() const {
149 const Use *End = getImpliedUser();
Gabor Greif4077dd02009-01-05 17:19:25 +0000150 const PointerIntPair<User*, 1, Tag>& ref(
151 static_cast<const AugmentedUse*>(End - 1)->ref);
Gabor Greif2231c2c2009-01-05 16:05:32 +0000152 User *She = ref.getPointer();
153 return ref.getInt()
Gabor Greif6265ab82009-01-05 16:28:14 +0000154 ? She
155 : (User*)End;
Gabor Greiff6caff662008-05-10 08:32:32 +0000156}
157
158//===----------------------------------------------------------------------===//
159// User allocHungoffUses Implementation
160//===----------------------------------------------------------------------===//
161
162Use *User::allocHungoffUses(unsigned N) const {
Gabor Greif697e94c2008-05-15 10:04:30 +0000163 Use *Begin = static_cast<Use*>(::operator new(sizeof(Use) * N
164 + sizeof(AugmentedUse)
165 - sizeof(Use)));
Gabor Greiff6caff662008-05-10 08:32:32 +0000166 Use *End = Begin + N;
Gabor Greif6265ab82009-01-05 16:28:14 +0000167 PointerIntPair<User*, 1, Tag>& ref(static_cast<AugmentedUse&>(End[-1]).ref);
Gabor Greif2231c2c2009-01-05 16:05:32 +0000168 ref.setPointer(const_cast<User*>(this));
169 ref.setInt(tagOne);
Gabor Greiff6caff662008-05-10 08:32:32 +0000170 return Use::initTags(Begin, End);
171}
172
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000173//===----------------------------------------------------------------------===//
174// User operator new Implementations
175//===----------------------------------------------------------------------===//
176
177void *User::operator new(size_t s, unsigned Us) {
178 void *Storage = ::operator new(s + sizeof(Use) * Us);
179 Use *Start = static_cast<Use*>(Storage);
180 Use *End = Start + Us;
181 User *Obj = reinterpret_cast<User*>(End);
182 Obj->OperandList = Start;
183 Obj->NumOperands = Us;
184 Use::initTags(Start, End);
185 return Obj;
186}
187
188/// Prefixed allocation - just before the first Use, allocate a NULL pointer.
189/// The destructor can detect its presence and readjust the OperandList
190/// for deletition.
191///
192void *User::operator new(size_t s, unsigned Us, bool Prefix) {
193 // currently prefixed allocation only admissible for
194 // unconditional branch instructions
195 if (!Prefix)
196 return operator new(s, Us);
197
198 assert(Us == 1 && "Other than one Use allocated?");
199 typedef PointerIntPair<void*, 2, Use::PrevPtrTag> TaggedPrefix;
200 void *Raw = ::operator new(s + sizeof(TaggedPrefix) + sizeof(Use) * Us);
201 TaggedPrefix *Pre = static_cast<TaggedPrefix*>(Raw);
202 Pre->setFromOpaqueValue(0);
203 void *Storage = Pre + 1; // skip over prefix
204 Use *Start = static_cast<Use*>(Storage);
205 Use *End = Start + Us;
206 User *Obj = reinterpret_cast<User*>(End);
207 Obj->OperandList = Start;
208 Obj->NumOperands = Us;
209 Use::initTags(Start, End);
210 return Obj;
211}
212
213//===----------------------------------------------------------------------===//
214// User operator delete Implementation
215//===----------------------------------------------------------------------===//
216
217void User::operator delete(void *Usr) {
218 User *Start = static_cast<User*>(Usr);
219 Use *Storage = static_cast<Use*>(Usr) - Start->NumOperands;
220 //
221 // look for a variadic User
222 if (Storage == Start->OperandList) {
223 ::operator delete(Storage);
224 return;
225 }
226 //
227 // check for the flag whether the destructor has detected a prefixed
228 // allocation, in which case we remove the flag and delete starting
229 // at OperandList
230 if (reinterpret_cast<intptr_t>(Start->OperandList) & 1) {
231 ::operator delete(reinterpret_cast<char*>(Start->OperandList) - 1);
232 return;
233 }
234 //
235 // in all other cases just delete the nullary User (covers hung-off
236 // uses also
237 ::operator delete(Usr);
238}
239
Gabor Greiff6caff662008-05-10 08:32:32 +0000240} // End llvm namespace