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 swap Implementation
20 //===----------------------------------------------------------------------===//
22 void Use::swap(Use
&RHS
) {
47 //===----------------------------------------------------------------------===//
48 // Use getImpliedUser Implementation
49 //===----------------------------------------------------------------------===//
51 const Use
*Use::getImpliedUser() const {
52 const Use
*Current
= this;
55 unsigned Tag
= (Current
++)->Prev
.getInt();
65 unsigned Tag
= Current
->Prev
.getInt();
70 Offset
= (Offset
<< 1) + Tag
;
73 return Current
+ Offset
;
84 //===----------------------------------------------------------------------===//
85 // Use initTags Implementation
86 //===----------------------------------------------------------------------===//
88 Use
*Use::initTags(Use
* const Start
, Use
*Stop
, ptrdiff_t Done
) {
89 ptrdiff_t Count
= Done
;
90 while (Start
!= Stop
) {
94 Stop
->Prev
.setFromOpaqueValue(reinterpret_cast<Use
**>(Done
== 0
100 Stop
->Prev
.setFromOpaqueValue(reinterpret_cast<Use
**>(Count
& 1));
109 //===----------------------------------------------------------------------===//
110 // Use zap Implementation
111 //===----------------------------------------------------------------------===//
113 void Use::zap(Use
*Start
, const Use
*Stop
, bool del
) {
115 while (Start
!= Stop
) {
118 ::operator delete(Start
);
122 while (Start
!= Stop
) {
127 //===----------------------------------------------------------------------===//
128 // AugmentedUse layout struct
129 //===----------------------------------------------------------------------===//
131 struct AugmentedUse
: Use
{
132 PointerIntPair
<User
*, 1, Tag
> ref
;
133 AugmentedUse(); // not implemented
137 //===----------------------------------------------------------------------===//
138 // Use getUser Implementation
139 //===----------------------------------------------------------------------===//
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();
151 //===----------------------------------------------------------------------===//
152 // User allocHungoffUses Implementation
153 //===----------------------------------------------------------------------===//
155 Use
*User::allocHungoffUses(unsigned N
) const {
156 Use
*Begin
= static_cast<Use
*>(::operator new(sizeof(Use
) * N
157 + sizeof(AugmentedUse
)
159 Use
*End
= Begin
+ N
;
160 PointerIntPair
<User
*, 1, Tag
>& ref(static_cast<AugmentedUse
&>(End
[-1]).ref
);
161 ref
.setPointer(const_cast<User
*>(this));
163 return Use::initTags(Begin
, End
);
166 //===----------------------------------------------------------------------===//
167 // User operator new Implementations
168 //===----------------------------------------------------------------------===//
170 void *User::operator new(size_t s
, unsigned Us
) {
171 void *Storage
= ::operator new(s
+ sizeof(Use
) * Us
);
172 Use
*Start
= static_cast<Use
*>(Storage
);
173 Use
*End
= Start
+ Us
;
174 User
*Obj
= reinterpret_cast<User
*>(End
);
175 Obj
->OperandList
= Start
;
176 Obj
->NumOperands
= Us
;
177 Use::initTags(Start
, End
);
181 /// Prefixed allocation - just before the first Use, allocate a NULL pointer.
182 /// The destructor can detect its presence and readjust the OperandList
185 void *User::operator new(size_t s
, unsigned Us
, bool Prefix
) {
186 // currently prefixed allocation only admissible for
187 // unconditional branch instructions
189 return operator new(s
, Us
);
191 assert(Us
== 1 && "Other than one Use allocated?");
192 typedef PointerIntPair
<void*, 2, Use::PrevPtrTag
> TaggedPrefix
;
193 void *Raw
= ::operator new(s
+ sizeof(TaggedPrefix
) + sizeof(Use
) * Us
);
194 TaggedPrefix
*Pre
= static_cast<TaggedPrefix
*>(Raw
);
195 Pre
->setFromOpaqueValue(0);
196 void *Storage
= Pre
+ 1; // skip over prefix
197 Use
*Start
= static_cast<Use
*>(Storage
);
198 Use
*End
= Start
+ Us
;
199 User
*Obj
= reinterpret_cast<User
*>(End
);
200 Obj
->OperandList
= Start
;
201 Obj
->NumOperands
= Us
;
202 Use::initTags(Start
, End
);
206 //===----------------------------------------------------------------------===//
207 // User operator delete Implementation
208 //===----------------------------------------------------------------------===//
210 void User::operator delete(void *Usr
) {
211 User
*Start
= static_cast<User
*>(Usr
);
212 Use
*Storage
= static_cast<Use
*>(Usr
) - Start
->NumOperands
;
214 // look for a variadic User
215 if (Storage
== Start
->OperandList
) {
216 ::operator delete(Storage
);
220 // check for the flag whether the destructor has detected a prefixed
221 // allocation, in which case we remove the flag and delete starting
223 if (reinterpret_cast<intptr_t>(Start
->OperandList
) & 1) {
224 ::operator delete(reinterpret_cast<char*>(Start
->OperandList
) - 1);
228 // in all other cases just delete the nullary User (covers hung-off
230 ::operator delete(Usr
);
233 } // End llvm namespace