1 //===-- Use.cpp - Implement the Use 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 // This file implements the algorithm for finding the User of a Use.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/User.h"
18 //===----------------------------------------------------------------------===//
19 // Use swap Implementation
20 //===----------------------------------------------------------------------===//
22 void Use::swap(Use &RHS) {
47 //===----------------------------------------------------------------------===//
48 // Use getImpliedUser Implementation
49 //===----------------------------------------------------------------------===//
51 const Use *Use::getImpliedUser() const {
52 const Use *Current = this;
55 unsigned Tag = (Current++)->Prev.getInt();
65 unsigned Tag = Current->Prev.getInt();
70 Offset = (Offset << 1) + Tag;
73 return Current + Offset;
84 //===----------------------------------------------------------------------===//
85 // Use initTags Implementation
86 //===----------------------------------------------------------------------===//
88 Use *Use::initTags(Use * const Start, Use *Stop, ptrdiff_t Done) {
92 static const PrevPtrTag tags[6] = { fullStopTag, oneDigitTag, stopTag,
93 oneDigitTag, oneDigitTag, stopTag };
94 Stop->Prev.setFromOpaqueValue(reinterpret_cast<Use**>(tags[Done++]));
98 ptrdiff_t Count = Done;
99 while (Start != Stop) {
103 Stop->Prev.setFromOpaqueValue(reinterpret_cast<Use**>(Done == 0
109 Stop->Prev.setFromOpaqueValue(reinterpret_cast<Use**>(Count & 1));
118 //===----------------------------------------------------------------------===//
119 // Use zap Implementation
120 //===----------------------------------------------------------------------===//
122 void Use::zap(Use *Start, const Use *Stop, bool del) {
124 while (Start != Stop) {
127 ::operator delete(Start);
131 while (Start != Stop) {
136 //===----------------------------------------------------------------------===//
137 // AugmentedUse layout struct
138 //===----------------------------------------------------------------------===//
140 struct AugmentedUse : public Use {
141 PointerIntPair<User*, 1, Tag> ref;
142 AugmentedUse(); // not implemented
146 //===----------------------------------------------------------------------===//
147 // Use getUser Implementation
148 //===----------------------------------------------------------------------===//
150 User *Use::getUser() const {
151 const Use *End = getImpliedUser();
152 const PointerIntPair<User*, 1, Tag>& ref(
153 static_cast<const AugmentedUse*>(End - 1)->ref);
154 User *She = ref.getPointer();
160 //===----------------------------------------------------------------------===//
161 // User allocHungoffUses Implementation
162 //===----------------------------------------------------------------------===//
164 Use *User::allocHungoffUses(unsigned N) const {
165 Use *Begin = static_cast<Use*>(::operator new(sizeof(Use) * N
166 + sizeof(AugmentedUse)
168 Use *End = Begin + N;
169 PointerIntPair<User*, 1, Tag>& ref(static_cast<AugmentedUse&>(End[-1]).ref);
170 ref.setPointer(const_cast<User*>(this));
172 return Use::initTags(Begin, End);
175 //===----------------------------------------------------------------------===//
176 // User operator new Implementations
177 //===----------------------------------------------------------------------===//
179 void *User::operator new(size_t s, unsigned Us) {
180 void *Storage = ::operator new(s + sizeof(Use) * Us);
181 Use *Start = static_cast<Use*>(Storage);
182 Use *End = Start + Us;
183 User *Obj = reinterpret_cast<User*>(End);
184 Obj->OperandList = Start;
185 Obj->NumOperands = Us;
186 Use::initTags(Start, End);
190 /// Prefixed allocation - just before the first Use, allocate a NULL pointer.
191 /// The destructor can detect its presence and readjust the OperandList
194 void *User::operator new(size_t s, unsigned Us, bool Prefix) {
195 // currently prefixed allocation only admissible for
196 // unconditional branch instructions
198 return operator new(s, Us);
200 assert(Us == 1 && "Other than one Use allocated?");
201 typedef PointerIntPair<void*, 2, Use::PrevPtrTag> TaggedPrefix;
202 void *Raw = ::operator new(s + sizeof(TaggedPrefix) + sizeof(Use) * Us);
203 TaggedPrefix *Pre = static_cast<TaggedPrefix*>(Raw);
204 Pre->setFromOpaqueValue(0);
205 void *Storage = Pre + 1; // skip over prefix
206 Use *Start = static_cast<Use*>(Storage);
207 Use *End = Start + Us;
208 User *Obj = reinterpret_cast<User*>(End);
209 Obj->OperandList = Start;
210 Obj->NumOperands = Us;
211 Use::initTags(Start, End);
215 //===----------------------------------------------------------------------===//
216 // User operator delete Implementation
217 //===----------------------------------------------------------------------===//
219 void User::operator delete(void *Usr) {
220 User *Start = static_cast<User*>(Usr);
221 Use *Storage = static_cast<Use*>(Usr) - Start->NumOperands;
223 // look for a variadic User
224 if (Storage == Start->OperandList) {
225 ::operator delete(Storage);
229 // check for the flag whether the destructor has detected a prefixed
230 // allocation, in which case we remove the flag and delete starting
232 if (reinterpret_cast<intptr_t>(Start->OperandList) & 1) {
233 ::operator delete(reinterpret_cast<char*>(Start->OperandList) - 1);
237 // in all other cases just delete the nullary User (covers hung-off
239 ::operator delete(Usr);
242 } // End llvm namespace