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**>(stopTag));
107 Stop->Prev.setFromOpaqueValue(reinterpret_cast<Use**>(Count & 1));
116 //===----------------------------------------------------------------------===//
117 // Use zap Implementation
118 //===----------------------------------------------------------------------===//
120 void Use::zap(Use *Start, const Use *Stop, bool del) {
122 while (Start != Stop) {
125 ::operator delete(Start);
129 while (Start != Stop) {
134 //===----------------------------------------------------------------------===//
135 // AugmentedUse layout struct
136 //===----------------------------------------------------------------------===//
138 struct AugmentedUse : public Use {
139 PointerIntPair<User*, 1, Tag> ref;
140 AugmentedUse(); // not implemented
144 //===----------------------------------------------------------------------===//
145 // Use getUser Implementation
146 //===----------------------------------------------------------------------===//
148 User *Use::getUser() const {
149 const Use *End = getImpliedUser();
150 const PointerIntPair<User*, 1, Tag>& ref(
151 static_cast<const AugmentedUse*>(End - 1)->ref);
152 User *She = ref.getPointer();
158 //===----------------------------------------------------------------------===//
159 // User allocHungoffUses Implementation
160 //===----------------------------------------------------------------------===//
162 Use *User::allocHungoffUses(unsigned N) const {
163 Use *Begin = static_cast<Use*>(::operator new(sizeof(Use) * N
164 + sizeof(AugmentedUse)
166 Use *End = Begin + N;
167 PointerIntPair<User*, 1, Tag>& ref(static_cast<AugmentedUse&>(End[-1]).ref);
168 ref.setPointer(const_cast<User*>(this));
170 return Use::initTags(Begin, End);
173 //===----------------------------------------------------------------------===//
174 // User operator new Implementations
175 //===----------------------------------------------------------------------===//
177 void *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);
188 /// Prefixed allocation - just before the first Use, allocate a NULL pointer.
189 /// The destructor can detect its presence and readjust the OperandList
192 void *User::operator new(size_t s, unsigned Us, bool Prefix) {
193 // currently prefixed allocation only admissible for
194 // unconditional branch instructions
196 return operator new(s, Us);
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);
213 //===----------------------------------------------------------------------===//
214 // User operator delete Implementation
215 //===----------------------------------------------------------------------===//
217 void User::operator delete(void *Usr) {
218 User *Start = static_cast<User*>(Usr);
219 Use *Storage = static_cast<Use*>(Usr) - Start->NumOperands;
221 // look for a variadic User
222 if (Storage == Start->OperandList) {
223 ::operator delete(Storage);
227 // check for the flag whether the destructor has detected a prefixed
228 // allocation, in which case we remove the flag and delete starting
230 if (reinterpret_cast<intptr_t>(Start->OperandList) & 1) {
231 ::operator delete(reinterpret_cast<char*>(Start->OperandList) - 1);
235 // in all other cases just delete the nullary User (covers hung-off
237 ::operator delete(Usr);
240 } // End llvm namespace