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 getImpliedUser Implementation
20 //===----------------------------------------------------------------------===//
22 const Use *Use::getImpliedUser() const {
23 const Use *Current = this;
26 unsigned Tag = extractTag<PrevPtrTag, fullStopTag>((Current++)->Prev);
36 unsigned Tag = extractTag<PrevPtrTag, fullStopTag>(Current->Prev);
41 Offset = (Offset << 1) + Tag;
44 return Current + Offset;
55 //===----------------------------------------------------------------------===//
56 // Use initTags Implementation
57 //===----------------------------------------------------------------------===//
59 Use *Use::initTags(Use * const Start, Use *Stop, ptrdiff_t Done) {
60 ptrdiff_t Count = Done;
61 while (Start != Stop) {
65 Stop->Prev = reinterpret_cast<Use**>(Done == 0 ? fullStopTag : stopTag);
69 Stop->Prev = reinterpret_cast<Use**>(Count & 1);
78 //===----------------------------------------------------------------------===//
79 // Use zap Implementation
80 //===----------------------------------------------------------------------===//
82 void Use::zap(Use *Start, const Use *Stop, bool del) {
84 while (Start != Stop) {
87 ::operator delete(Start);
91 while (Start != Stop) {
96 //===----------------------------------------------------------------------===//
97 // AugmentedUse layout struct
98 //===----------------------------------------------------------------------===//
100 struct AugmentedUse : Use {
102 AugmentedUse(); // not implemented
106 //===----------------------------------------------------------------------===//
107 // Use getUser Implementation
108 //===----------------------------------------------------------------------===//
110 User *Use::getUser() const {
111 const Use *End = getImpliedUser();
112 User *She = static_cast<const AugmentedUse*>(End - 1)->ref;
113 She = extractTag<Tag, tagOne>(She)
114 ? llvm::stripTag<tagOne>(She)
115 : reinterpret_cast<User*>(const_cast<Use*>(End));
120 //===----------------------------------------------------------------------===//
121 // User allocHungoffUses Implementation
122 //===----------------------------------------------------------------------===//
124 Use *User::allocHungoffUses(unsigned N) const {
125 Use *Begin = static_cast<Use*>(::operator new(sizeof(Use) * N + sizeof(AugmentedUse) - sizeof(Use)));
126 Use *End = Begin + N;
127 static_cast<AugmentedUse&>(End[-1]).ref = addTag(this, tagOne);
128 return Use::initTags(Begin, End);
131 } // End llvm namespace