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[20] = { fullStopTag, oneDigitTag, stopTag,
93 oneDigitTag, oneDigitTag, stopTag,
94 zeroDigitTag, oneDigitTag, oneDigitTag,
95 stopTag, zeroDigitTag, oneDigitTag,
96 zeroDigitTag, oneDigitTag, stopTag,
97 oneDigitTag, oneDigitTag, oneDigitTag,
100 Stop->Prev.setFromOpaqueValue(reinterpret_cast<Use**>(tags[Done++]));
104 ptrdiff_t Count = Done;
105 while (Start != Stop) {
109 Stop->Prev.setFromOpaqueValue(reinterpret_cast<Use**>(stopTag));
113 Stop->Prev.setFromOpaqueValue(reinterpret_cast<Use**>(Count & 1));
122 //===----------------------------------------------------------------------===//
123 // Use zap Implementation
124 //===----------------------------------------------------------------------===//
126 void Use::zap(Use *Start, const Use *Stop, bool del) {
128 while (Start != Stop) {
131 ::operator delete(Start);
135 while (Start != Stop) {
140 //===----------------------------------------------------------------------===//
141 // AugmentedUse layout struct
142 //===----------------------------------------------------------------------===//
144 struct AugmentedUse : public Use {
145 PointerIntPair<User*, 1, Tag> ref;
146 AugmentedUse(); // not implemented
150 //===----------------------------------------------------------------------===//
151 // Use getUser Implementation
152 //===----------------------------------------------------------------------===//
154 User *Use::getUser() const {
155 const Use *End = getImpliedUser();
156 const PointerIntPair<User*, 1, Tag>& ref(
157 static_cast<const AugmentedUse*>(End - 1)->ref);
158 User *She = ref.getPointer();
164 //===----------------------------------------------------------------------===//
165 // User allocHungoffUses Implementation
166 //===----------------------------------------------------------------------===//
168 Use *User::allocHungoffUses(unsigned N) const {
169 Use *Begin = static_cast<Use*>(::operator new(sizeof(Use) * N
170 + sizeof(AugmentedUse)
172 Use *End = Begin + N;
173 PointerIntPair<User*, 1, Tag>& ref(static_cast<AugmentedUse&>(End[-1]).ref);
174 ref.setPointer(const_cast<User*>(this));
176 return Use::initTags(Begin, End);
179 //===----------------------------------------------------------------------===//
180 // User operator new Implementations
181 //===----------------------------------------------------------------------===//
183 void *User::operator new(size_t s, unsigned Us) {
184 void *Storage = ::operator new(s + sizeof(Use) * Us);
185 Use *Start = static_cast<Use*>(Storage);
186 Use *End = Start + Us;
187 User *Obj = reinterpret_cast<User*>(End);
188 Obj->OperandList = Start;
189 Obj->NumOperands = Us;
190 Use::initTags(Start, End);
194 //===----------------------------------------------------------------------===//
195 // User operator delete Implementation
196 //===----------------------------------------------------------------------===//
198 void User::operator delete(void *Usr) {
199 User *Start = static_cast<User*>(Usr);
200 Use *Storage = static_cast<Use*>(Usr) - Start->NumOperands;
202 // look for a variadic User
203 if (Storage == Start->OperandList) {
204 ::operator delete(Storage);
208 // in all other cases just delete the nullary User (covers hung-off
210 ::operator delete(Usr);
213 } // End llvm namespace