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) {
23 ptrdiff_t dist((char*)&RHS - (char*)this);
26 Use *valid1(stripTag<tagMaskN>(Next));
27 Use *valid2(stripTag<tagMaskN>(RHS.Next));
28 if (valid1 && valid2) {
29 bool real1(fullStopTagN != extractTag<NextPtrTag, tagMaskN>(Next));
30 bool real2(fullStopTagN != extractTag<NextPtrTag, tagMaskN>(RHS.Next));
31 (char*&)*stripTag<tagMask>(Prev) += dist;
32 (char*&)*stripTag<tagMask>(RHS.Prev) -= dist;
34 (char*&)valid1->Next += dist;
36 (char*&)valid2->Next -= dist;
41 std::swap(Next, RHS.Next);
42 Use** Prev1 = transferTag<tagMask>(Prev, stripTag<tagMask>(RHS.Prev));
43 RHS.Prev = transferTag<tagMask>(RHS.Prev, stripTag<tagMask>(Prev));
71 //===----------------------------------------------------------------------===//
72 // Use getImpliedUser Implementation
73 //===----------------------------------------------------------------------===//
75 const Use *Use::getImpliedUser() const {
76 const Use *Current = this;
79 unsigned Tag = extractTag<PrevPtrTag, tagMask>((Current++)->Prev);
89 unsigned Tag = extractTag<PrevPtrTag, tagMask>(Current->Prev);
94 Offset = (Offset << 1) + Tag;
97 return Current + Offset;
108 //===----------------------------------------------------------------------===//
109 // Use initTags Implementation
110 //===----------------------------------------------------------------------===//
112 Use *Use::initTags(Use * const Start, Use *Stop, ptrdiff_t Done) {
113 ptrdiff_t Count = Done;
114 while (Start != Stop) {
117 Stop->Next = nilUse(0);
119 Stop->Prev = reinterpret_cast<Use**>(Done == 0 ? fullStopTag : stopTag);
123 Stop->Prev = reinterpret_cast<Use**>(Count & 1);
132 //===----------------------------------------------------------------------===//
133 // Use zap Implementation
134 //===----------------------------------------------------------------------===//
136 void Use::zap(Use *Start, const Use *Stop, bool del) {
138 while (Start != Stop) {
141 ::operator delete(Start);
145 while (Start != Stop) {
150 //===----------------------------------------------------------------------===//
151 // AugmentedUse layout struct
152 //===----------------------------------------------------------------------===//
154 struct AugmentedUse : Use {
156 AugmentedUse(); // not implemented
160 //===----------------------------------------------------------------------===//
161 // Use getUser Implementation
162 //===----------------------------------------------------------------------===//
164 User *Use::getUser() const {
165 const Use *End = getImpliedUser();
166 User *She = static_cast<const AugmentedUse*>(End - 1)->ref;
167 She = extractTag<Tag, tagOne>(She)
168 ? llvm::stripTag<tagOne>(She)
169 : reinterpret_cast<User*>(const_cast<Use*>(End));
174 //===----------------------------------------------------------------------===//
175 // User allocHungoffUses Implementation
176 //===----------------------------------------------------------------------===//
178 Use *User::allocHungoffUses(unsigned N) const {
179 Use *Begin = static_cast<Use*>(::operator new(sizeof(Use) * N
180 + sizeof(AugmentedUse)
182 Use *End = Begin + N;
183 static_cast<AugmentedUse&>(End[-1]).ref = addTag(this, tagOne);
184 return Use::initTags(Begin, End);
187 } // End llvm namespace