CMake: removed lib/VMCore/DebugInfoBuilder.cpp.
[oota-llvm.git] / lib / VMCore / Use.cpp
1 //===-- Use.cpp - Implement the Use class ---------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the algorithm for finding the User of a Use.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/User.h"
15
16 namespace llvm {
17
18 //===----------------------------------------------------------------------===//
19 //                         Use swap Implementation
20 //===----------------------------------------------------------------------===//
21
22 void Use::swap(Use &RHS) {
23   Value *V1(Val);
24   Value *V2(RHS.Val);
25   if (V1 != V2) {
26     if (V1) {
27       removeFromList();
28     }
29
30     if (V2) {
31       RHS.removeFromList();
32       Val = V2;
33       V2->addUse(*this);
34     } else {
35       Val = 0;
36     }
37
38     if (V1) {
39       RHS.Val = V1;
40       V1->addUse(RHS);
41     } else {
42       RHS.Val = 0;
43     }
44   }
45 }
46
47 //===----------------------------------------------------------------------===//
48 //                         Use getImpliedUser Implementation
49 //===----------------------------------------------------------------------===//
50
51 const Use *Use::getImpliedUser() const {
52   const Use *Current = this;
53
54   while (true) {
55     unsigned Tag = (Current++)->Prev.getInt();
56     switch (Tag) {
57       case zeroDigitTag:
58       case oneDigitTag:
59         continue;
60
61       case stopTag: {
62         ++Current;
63         ptrdiff_t Offset = 1;
64         while (true) {
65           unsigned Tag = Current->Prev.getInt();
66           switch (Tag) {
67             case zeroDigitTag:
68             case oneDigitTag:
69               ++Current;
70               Offset = (Offset << 1) + Tag;
71               continue;
72             default:
73               return Current + Offset;
74           }
75         }
76       }
77
78       case fullStopTag:
79         return Current;
80     }
81   }
82 }
83
84 //===----------------------------------------------------------------------===//
85 //                         Use initTags Implementation
86 //===----------------------------------------------------------------------===//
87
88 Use *Use::initTags(Use * const Start, Use *Stop, ptrdiff_t Done) {
89   ptrdiff_t Count = Done;
90   while (Start != Stop) {
91     --Stop;
92     Stop->Val = 0;
93     if (!Count) {
94       Stop->Prev.setFromOpaqueValue(reinterpret_cast<Use**>(Done == 0
95                                                             ? fullStopTag
96                                                             : stopTag));
97       ++Done;
98       Count = Done;
99     } else {
100       Stop->Prev.setFromOpaqueValue(reinterpret_cast<Use**>(Count & 1));
101       Count >>= 1;
102       ++Done;
103     }
104   }
105
106   return Start;
107 }
108
109 //===----------------------------------------------------------------------===//
110 //                         Use zap Implementation
111 //===----------------------------------------------------------------------===//
112
113 void Use::zap(Use *Start, const Use *Stop, bool del) {
114   if (del) {
115     while (Start != Stop) {
116       (--Stop)->~Use();
117     }
118     ::operator delete(Start);
119     return;
120   }
121
122   while (Start != Stop) {
123     (Start++)->set(0);
124   }
125 }
126
127 //===----------------------------------------------------------------------===//
128 //                         AugmentedUse layout struct
129 //===----------------------------------------------------------------------===//
130
131 struct AugmentedUse : Use {
132   PointerIntPair<User*, 1, Tag> ref;
133   AugmentedUse(); // not implemented
134 };
135
136
137 //===----------------------------------------------------------------------===//
138 //                         Use getUser Implementation
139 //===----------------------------------------------------------------------===//
140
141 User *Use::getUser() const {
142   const Use *End = getImpliedUser();
143   const PointerIntPair<User*, 1, Tag>& ref(
144                                 static_cast<const AugmentedUse*>(End - 1)->ref);
145   User *She = ref.getPointer();
146   return ref.getInt()
147     ? She
148     : (User*)End;
149 }
150
151 //===----------------------------------------------------------------------===//
152 //                         User allocHungoffUses Implementation
153 //===----------------------------------------------------------------------===//
154
155 Use *User::allocHungoffUses(unsigned N) const {
156   Use *Begin = static_cast<Use*>(::operator new(sizeof(Use) * N
157                                                 + sizeof(AugmentedUse)
158                                                 - sizeof(Use)));
159   Use *End = Begin + N;
160   PointerIntPair<User*, 1, Tag>& ref(static_cast<AugmentedUse&>(End[-1]).ref);
161   ref.setPointer(const_cast<User*>(this));
162   ref.setInt(tagOne);
163   return Use::initTags(Begin, End);
164 }
165
166 } // End llvm namespace