blob: 359a1517ab79b719062c6294fe9b3c2aa311190e [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
Jay Foad59809c72011-01-16 08:10:57 +000014#include "llvm/Value.h"
Gabor Greiff6caff662008-05-10 08:32:32 +000015
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
Jay Foadbbb91f22011-01-16 15:30:52 +000088Use *Use::initTags(Use * const Start, Use *Stop) {
89 ptrdiff_t Done = 0;
Gabor Greif39c06b32010-07-19 14:48:15 +000090 while (Done < 20) {
Gabor Greiffee4daf2010-07-16 20:35:19 +000091 if (Start == Stop--)
92 return Start;
Gabor Greif39c06b32010-07-19 14:48:15 +000093 static const PrevPtrTag tags[20] = { fullStopTag, oneDigitTag, stopTag,
94 oneDigitTag, oneDigitTag, stopTag,
95 zeroDigitTag, oneDigitTag, oneDigitTag,
96 stopTag, zeroDigitTag, oneDigitTag,
97 zeroDigitTag, oneDigitTag, stopTag,
98 oneDigitTag, oneDigitTag, oneDigitTag,
99 oneDigitTag, stopTag
100 };
Jay Foadbbb91f22011-01-16 15:30:52 +0000101 new(Stop) Use(tags[Done++]);
Gabor Greiffee4daf2010-07-16 20:35:19 +0000102 }
103
Gabor Greiff6caff662008-05-10 08:32:32 +0000104 ptrdiff_t Count = Done;
105 while (Start != Stop) {
106 --Stop;
Gabor Greiff6caff662008-05-10 08:32:32 +0000107 if (!Count) {
Jay Foadbbb91f22011-01-16 15:30:52 +0000108 new(Stop) Use(stopTag);
Gabor Greiff6caff662008-05-10 08:32:32 +0000109 ++Done;
110 Count = Done;
111 } else {
Jay Foadbbb91f22011-01-16 15:30:52 +0000112 new(Stop) Use(PrevPtrTag(Count & 1));
Gabor Greiff6caff662008-05-10 08:32:32 +0000113 Count >>= 1;
114 ++Done;
115 }
116 }
117
118 return Start;
119}
120
121//===----------------------------------------------------------------------===//
122// Use zap Implementation
123//===----------------------------------------------------------------------===//
124
125void Use::zap(Use *Start, const Use *Stop, bool del) {
Jay Foadbbb91f22011-01-16 15:30:52 +0000126 while (Start != Stop)
127 (--Stop)->~Use();
128 if (del)
Gabor Greiff6caff662008-05-10 08:32:32 +0000129 ::operator delete(Start);
Gabor Greiff6caff662008-05-10 08:32:32 +0000130}
131
132//===----------------------------------------------------------------------===//
Gabor Greiff6caff662008-05-10 08:32:32 +0000133// Use getUser Implementation
134//===----------------------------------------------------------------------===//
135
136User *Use::getUser() const {
137 const Use *End = getImpliedUser();
Jay Foad5c54d752011-06-20 14:12:33 +0000138 const UserRef *ref = reinterpret_cast<const UserRef*>(End);
139 return ref->getInt()
140 ? ref->getPointer()
Gabor Greif6265ab82009-01-05 16:28:14 +0000141 : (User*)End;
Gabor Greiff6caff662008-05-10 08:32:32 +0000142}
143
Gabor Greiff6caff662008-05-10 08:32:32 +0000144} // End llvm namespace