DisableGVCompilation should not abort on internal GlobalValue's.
[oota-llvm.git] / include / llvm / Use.h
1 //===-- llvm/Use.h - Definition of the Use class ----------------*- C++ -*-===//
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 defines the Use class.  The Use class represents the operand of an
11 // instruction or some other User instance which refers to a Value.  The Use
12 // class keeps the "use list" of the referenced value up to date.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_USE_H
17 #define LLVM_USE_H
18
19 #include "llvm/Support/Casting.h"
20 #include "llvm/ADT/iterator.h"
21
22 namespace llvm {
23
24 class Value;
25 class User;
26
27
28 //===----------------------------------------------------------------------===//
29 //                          Generic Tagging Functions
30 //===----------------------------------------------------------------------===//
31
32 // We adhere to the following convention: The type of a tagged pointer
33 // to T is T volatile*. This means that functions that superpose a tag
34 // on a pointer will be supplied a T* (or T const*) and will return a
35 // tagged one: T volatile*. Untagging functions do it the other way
36 // 'round. While this scheme does not prevent dereferencing of tagged
37 // pointers, proper type annotations do catch most inappropriate uses.
38
39 /// Tag - generic tag type for (at least 32 bit) pointers
40 enum Tag { noTag, tagOne, tagTwo, tagThree };
41
42 /// addTag - insert tag bits into an (untagged) pointer
43 template <typename T, typename TAG>
44 inline volatile T *addTag(const T *P, TAG Tag) {
45   return reinterpret_cast<T*>(ptrdiff_t(P) | Tag);
46 }
47
48 /// stripTag - remove tag bits from a pointer,
49 /// making it dereferencable
50 template <ptrdiff_t MASK, typename T>
51 inline T *stripTag(const volatile T *P) {
52   return reinterpret_cast<T*>(ptrdiff_t(P) & ~MASK);
53 }
54
55 /// extractTag - extract tag bits from a pointer
56 template <typename TAG, TAG MASK, typename T>
57 inline TAG extractTag(const volatile T *P) {
58   return TAG(ptrdiff_t(P) & MASK);
59 }
60
61 /// transferTag - transfer tag bits from a pointer,
62 /// to an untagged pointer
63 template <ptrdiff_t MASK, typename T>
64 inline volatile T *transferTag(const volatile T *From, const T *To) {
65   return reinterpret_cast<T*>((ptrdiff_t(From) & MASK) | ptrdiff_t(To));
66 }
67
68
69 //===----------------------------------------------------------------------===//
70 //                                  Use Class
71 //===----------------------------------------------------------------------===//
72
73 /// Use is here to make keeping the "use" list of a Value up-to-date really
74 /// easy.
75 class Use {
76 public:
77   /// swap - provide a fast substitute to std::swap<Use>
78   /// that also works with less standard-compliant compilers
79   void swap(Use &RHS);
80
81 private:
82   /// Copy ctor - do not implement
83   Use(const Use &U);
84
85   /// Destructor - Only for zap()
86   inline ~Use() {
87     if (Val) removeFromList();
88   }
89
90   /// Default ctor - This leaves the Use completely uninitialized.  The only
91   /// thing that is valid to do with this use is to call the "init" method.
92   inline Use() {}
93   enum PrevPtrTag { zeroDigitTag = noTag
94                   , oneDigitTag = tagOne
95                   , stopTag = tagTwo
96                   , fullStopTag = tagThree };
97
98 public:
99   /// Normally Use will just implicitly convert to a Value* that it holds.
100   operator Value*() const { return Val; }
101   
102   /// If implicit conversion to Value* doesn't work, the get() method returns
103   /// the Value*.
104   Value *get() const { return Val; }
105   
106   /// getUser - This returns the User that contains this Use.  For an
107   /// instruction operand, for example, this will return the instruction.
108   User *getUser() const;
109
110   inline void set(Value *Val);
111
112   Value *operator=(Value *RHS) {
113     set(RHS);
114     return RHS;
115   }
116   const Use &operator=(const Use &RHS) {
117     set(RHS.Val);
118     return *this;
119   }
120
121         Value *operator->()       { return Val; }
122   const Value *operator->() const { return Val; }
123
124   Use *getNext() const { return Next; }
125
126   
127   /// zap - This is used to destroy Use operands when the number of operands of
128   /// a User changes.
129   static void zap(Use *Start, const Use *Stop, bool del = false);
130
131 private:
132   const Use* getImpliedUser() const;
133   static Use *initTags(Use *Start, Use *Stop, ptrdiff_t Done = 0);
134   
135   Value *Val;
136   Use *Next, *volatile*Prev;
137
138   void setPrev(Use **NewPrev) {
139     Prev = transferTag<fullStopTag>(Prev, NewPrev);
140   }
141   void addToList(Use **List) {
142     Next = *List;
143     if (Next) Next->setPrev(&Next);
144     setPrev(List);
145     *List = this;
146   }
147   void removeFromList() {
148     Use **StrippedPrev = stripTag<fullStopTag>(Prev);
149     *StrippedPrev = Next;
150     if (Next) Next->setPrev(StrippedPrev);
151   }
152
153   friend class Value;
154   friend class User;
155 };
156
157 // simplify_type - Allow clients to treat uses just like values when using
158 // casting operators.
159 template<> struct simplify_type<Use> {
160   typedef Value* SimpleType;
161   static SimpleType getSimplifiedValue(const Use &Val) {
162     return static_cast<SimpleType>(Val.get());
163   }
164 };
165 template<> struct simplify_type<const Use> {
166   typedef Value* SimpleType;
167   static SimpleType getSimplifiedValue(const Use &Val) {
168     return static_cast<SimpleType>(Val.get());
169   }
170 };
171
172
173
174 template<typename UserTy>  // UserTy == 'User' or 'const User'
175 class value_use_iterator : public forward_iterator<UserTy*, ptrdiff_t> {
176   typedef forward_iterator<UserTy*, ptrdiff_t> super;
177   typedef value_use_iterator<UserTy> _Self;
178
179   Use *U;
180   explicit value_use_iterator(Use *u) : U(u) {}
181   friend class Value;
182 public:
183   typedef typename super::reference reference;
184   typedef typename super::pointer pointer;
185
186   value_use_iterator(const _Self &I) : U(I.U) {}
187   value_use_iterator() {}
188
189   bool operator==(const _Self &x) const {
190     return U == x.U;
191   }
192   bool operator!=(const _Self &x) const {
193     return !operator==(x);
194   }
195
196   /// atEnd - return true if this iterator is equal to use_end() on the value.
197   bool atEnd() const { return U == 0; }
198
199   // Iterator traversal: forward iteration only
200   _Self &operator++() {          // Preincrement
201     assert(U && "Cannot increment end iterator!");
202     U = U->getNext();
203     return *this;
204   }
205   _Self operator++(int) {        // Postincrement
206     _Self tmp = *this; ++*this; return tmp;
207   }
208
209   // Retrieve a pointer to the current User.
210   UserTy *operator*() const {
211     assert(U && "Cannot dereference end iterator!");
212     return U->getUser();
213   }
214
215   UserTy *operator->() const { return operator*(); }
216
217   Use &getUse() const { return *U; }
218   
219   /// getOperandNo - Return the operand # of this use in its User.  Defined in
220   /// User.h
221   ///
222   unsigned getOperandNo() const;
223 };
224
225
226 template<> struct simplify_type<value_use_iterator<User> > {
227   typedef User* SimpleType;
228   
229   static SimpleType getSimplifiedValue(const value_use_iterator<User> &Val) {
230     return *Val;
231   }
232 };
233
234 template<> struct simplify_type<const value_use_iterator<User> >
235  : public simplify_type<value_use_iterator<User> > {};
236
237 template<> struct simplify_type<value_use_iterator<const User> > {
238   typedef const User* SimpleType;
239   
240   static SimpleType getSimplifiedValue(const 
241                                        value_use_iterator<const User> &Val) {
242     return *Val;
243   }
244 };
245
246 template<> struct simplify_type<const value_use_iterator<const User> >
247   : public simplify_type<value_use_iterator<const User> > {};
248
249 } // End llvm namespace
250
251 #endif