blob: c44c17f55e5f15de84134934ff6edb2aaf1b3cce [file] [log] [blame]
Gabor Greif697e94c2008-05-15 10:04:30 +00001//===-- Use.cpp - Implement the Use class ---------------------------------===//
Gabor Greiff6caff662008-05-10 08:32:32 +00002//
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
16namespace llvm {
17
18//===----------------------------------------------------------------------===//
Gabor Greif5ef74042008-05-13 22:51:52 +000019// Use swap Implementation
20//===----------------------------------------------------------------------===//
21
22void Use::swap(Use &RHS) {
Gabor Greif715b9d22008-09-19 15:13:20 +000023 Value *V1(Val);
24 Value *V2(RHS.Val);
Gabor Greif5ef74042008-05-13 22:51:52 +000025 if (V1 != V2) {
26 if (V1) {
27 removeFromList();
28 }
29
30 if (V2) {
31 RHS.removeFromList();
Gabor Greif715b9d22008-09-19 15:13:20 +000032 Val = V2;
Gabor Greif5ef74042008-05-13 22:51:52 +000033 V2->addUse(*this);
34 } else {
Gabor Greif715b9d22008-09-19 15:13:20 +000035 Val = 0;
Gabor Greif5ef74042008-05-13 22:51:52 +000036 }
37
38 if (V1) {
Gabor Greif715b9d22008-09-19 15:13:20 +000039 RHS.Val = V1;
Gabor Greif5ef74042008-05-13 22:51:52 +000040 V1->addUse(RHS);
41 } else {
Gabor Greif715b9d22008-09-19 15:13:20 +000042 RHS.Val = 0;
Gabor Greif5ef74042008-05-13 22:51:52 +000043 }
44 }
45}
46
47//===----------------------------------------------------------------------===//
Gabor Greiff6caff662008-05-10 08:32:32 +000048// Use getImpliedUser Implementation
49//===----------------------------------------------------------------------===//
50
51const Use *Use::getImpliedUser() const {
52 const Use *Current = this;
53
54 while (true) {
Gabor Greif2231c2c2009-01-05 16:05:32 +000055 unsigned Tag = (Current++)->Prev.getInt();
Gabor Greiff6caff662008-05-10 08:32:32 +000056 switch (Tag) {
57 case zeroDigitTag:
58 case oneDigitTag:
59 continue;
60
61 case stopTag: {
62 ++Current;
63 ptrdiff_t Offset = 1;
64 while (true) {
Gabor Greif2231c2c2009-01-05 16:05:32 +000065 unsigned Tag = Current->Prev.getInt();
Gabor Greiff6caff662008-05-10 08:32:32 +000066 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
88Use *Use::initTags(Use * const Start, Use *Stop, ptrdiff_t Done) {
89 ptrdiff_t Count = Done;
90 while (Start != Stop) {
91 --Stop;
Gabor Greif715b9d22008-09-19 15:13:20 +000092 Stop->Val = 0;
Gabor Greiff6caff662008-05-10 08:32:32 +000093 if (!Count) {
Gabor Greif2231c2c2009-01-05 16:05:32 +000094 Stop->Prev.setFromOpaqueValue(reinterpret_cast<Use**>(Done == 0 ? fullStopTag : stopTag));
Gabor Greiff6caff662008-05-10 08:32:32 +000095 ++Done;
96 Count = Done;
97 } else {
Gabor Greif2231c2c2009-01-05 16:05:32 +000098 Stop->Prev.setFromOpaqueValue(reinterpret_cast<Use**>(Count & 1));
Gabor Greiff6caff662008-05-10 08:32:32 +000099 Count >>= 1;
100 ++Done;
101 }
102 }
103
104 return Start;
105}
106
107//===----------------------------------------------------------------------===//
108// Use zap Implementation
109//===----------------------------------------------------------------------===//
110
111void Use::zap(Use *Start, const Use *Stop, bool del) {
112 if (del) {
113 while (Start != Stop) {
114 (--Stop)->~Use();
115 }
116 ::operator delete(Start);
117 return;
118 }
119
120 while (Start != Stop) {
121 (Start++)->set(0);
122 }
123}
124
125//===----------------------------------------------------------------------===//
126// AugmentedUse layout struct
127//===----------------------------------------------------------------------===//
128
129struct AugmentedUse : Use {
Gabor Greif2231c2c2009-01-05 16:05:32 +0000130 PointerIntPair<User*, 1, Tag> ref;
Gabor Greiff6caff662008-05-10 08:32:32 +0000131 AugmentedUse(); // not implemented
132};
133
134
135//===----------------------------------------------------------------------===//
136// Use getUser Implementation
137//===----------------------------------------------------------------------===//
138
139User *Use::getUser() const {
140 const Use *End = getImpliedUser();
Gabor Greif6265ab82009-01-05 16:28:14 +0000141 PointerIntPair<User*, 1, Tag>& ref(static_cast<const AugmentedUse*>(End - 1)->ref);
Gabor Greif2231c2c2009-01-05 16:05:32 +0000142 User *She = ref.getPointer();
143 return ref.getInt()
Gabor Greif6265ab82009-01-05 16:28:14 +0000144 ? She
145 : (User*)End;
Gabor Greiff6caff662008-05-10 08:32:32 +0000146}
147
148//===----------------------------------------------------------------------===//
149// User allocHungoffUses Implementation
150//===----------------------------------------------------------------------===//
151
152Use *User::allocHungoffUses(unsigned N) const {
Gabor Greif697e94c2008-05-15 10:04:30 +0000153 Use *Begin = static_cast<Use*>(::operator new(sizeof(Use) * N
154 + sizeof(AugmentedUse)
155 - sizeof(Use)));
Gabor Greiff6caff662008-05-10 08:32:32 +0000156 Use *End = Begin + N;
Gabor Greif6265ab82009-01-05 16:28:14 +0000157 PointerIntPair<User*, 1, Tag>& ref(static_cast<AugmentedUse&>(End[-1]).ref);
Gabor Greif2231c2c2009-01-05 16:05:32 +0000158 ref.setPointer(const_cast<User*>(this));
159 ref.setInt(tagOne);
Gabor Greiff6caff662008-05-10 08:32:32 +0000160 return Use::initTags(Begin, End);
161}
162
163} // End llvm namespace