2008-05-15 10:04:30 +00:00
|
|
|
//===-- Use.cpp - Implement the Use class ---------------------------------===//
|
2008-05-10 08:32:32 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-03-04 08:51:00 +00:00
|
|
|
#include "llvm/IR/Use.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Value.h"
|
2012-03-26 14:04:17 +00:00
|
|
|
#include <new>
|
2008-05-10 08:32:32 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2008-05-13 22:51:52 +00:00
|
|
|
void Use::swap(Use &RHS) {
|
2008-09-19 15:13:20 +00:00
|
|
|
Value *V1(Val);
|
|
|
|
Value *V2(RHS.Val);
|
2008-05-13 22:51:52 +00:00
|
|
|
if (V1 != V2) {
|
|
|
|
if (V1) {
|
|
|
|
removeFromList();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (V2) {
|
|
|
|
RHS.removeFromList();
|
2008-09-19 15:13:20 +00:00
|
|
|
Val = V2;
|
2008-05-13 22:51:52 +00:00
|
|
|
V2->addUse(*this);
|
|
|
|
} else {
|
2008-09-19 15:13:20 +00:00
|
|
|
Val = 0;
|
2008-05-13 22:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (V1) {
|
2008-09-19 15:13:20 +00:00
|
|
|
RHS.Val = V1;
|
2008-05-13 22:51:52 +00:00
|
|
|
V1->addUse(RHS);
|
|
|
|
} else {
|
2008-09-19 15:13:20 +00:00
|
|
|
RHS.Val = 0;
|
2008-05-13 22:51:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-04 08:51:00 +00:00
|
|
|
User *Use::getUser() const {
|
|
|
|
const Use *End = getImpliedUser();
|
2014-03-04 08:53:41 +00:00
|
|
|
const UserRef *ref = reinterpret_cast<const UserRef *>(End);
|
|
|
|
return ref->getInt() ? ref->getPointer()
|
|
|
|
: reinterpret_cast<User *>(const_cast<Use *>(End));
|
2008-05-10 08:32:32 +00:00
|
|
|
}
|
|
|
|
|
2014-03-04 08:51:00 +00:00
|
|
|
// Sets up the waymarking algoritm's tags for a series of Uses. See the
|
|
|
|
// algorithm details here:
|
|
|
|
//
|
|
|
|
// http://www.llvm.org/docs/ProgrammersManual.html#UserLayout
|
|
|
|
//
|
2014-03-04 08:53:41 +00:00
|
|
|
Use *Use::initTags(Use *const Start, Use *Stop) {
|
2011-01-16 15:30:52 +00:00
|
|
|
ptrdiff_t Done = 0;
|
2010-07-19 14:48:15 +00:00
|
|
|
while (Done < 20) {
|
2010-07-16 20:35:19 +00:00
|
|
|
if (Start == Stop--)
|
|
|
|
return Start;
|
2014-03-04 08:53:41 +00:00
|
|
|
static const PrevPtrTag tags[20] = {
|
|
|
|
fullStopTag, oneDigitTag, stopTag, oneDigitTag, oneDigitTag,
|
|
|
|
stopTag, zeroDigitTag, oneDigitTag, oneDigitTag, stopTag,
|
|
|
|
zeroDigitTag, oneDigitTag, zeroDigitTag, oneDigitTag, stopTag,
|
|
|
|
oneDigitTag, oneDigitTag, oneDigitTag, oneDigitTag, stopTag};
|
|
|
|
new (Stop) Use(tags[Done++]);
|
2010-07-16 20:35:19 +00:00
|
|
|
}
|
|
|
|
|
2008-05-10 08:32:32 +00:00
|
|
|
ptrdiff_t Count = Done;
|
|
|
|
while (Start != Stop) {
|
|
|
|
--Stop;
|
|
|
|
if (!Count) {
|
2014-03-04 08:53:41 +00:00
|
|
|
new (Stop) Use(stopTag);
|
2008-05-10 08:32:32 +00:00
|
|
|
++Done;
|
|
|
|
Count = Done;
|
|
|
|
} else {
|
2014-03-04 08:53:41 +00:00
|
|
|
new (Stop) Use(PrevPtrTag(Count & 1));
|
2008-05-10 08:32:32 +00:00
|
|
|
Count >>= 1;
|
|
|
|
++Done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Start;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Use::zap(Use *Start, const Use *Stop, bool del) {
|
2011-01-16 15:30:52 +00:00
|
|
|
while (Start != Stop)
|
|
|
|
(--Stop)->~Use();
|
|
|
|
if (del)
|
2008-05-10 08:32:32 +00:00
|
|
|
::operator delete(Start);
|
|
|
|
}
|
|
|
|
|
2014-03-04 08:51:00 +00:00
|
|
|
const Use *Use::getImpliedUser() const {
|
|
|
|
const Use *Current = this;
|
2008-05-10 08:32:32 +00:00
|
|
|
|
2014-03-04 08:51:00 +00:00
|
|
|
while (true) {
|
|
|
|
unsigned Tag = (Current++)->Prev.getInt();
|
|
|
|
switch (Tag) {
|
2014-03-04 08:53:41 +00:00
|
|
|
case zeroDigitTag:
|
|
|
|
case oneDigitTag:
|
|
|
|
continue;
|
2014-03-04 08:51:00 +00:00
|
|
|
|
2014-03-04 08:53:41 +00:00
|
|
|
case stopTag: {
|
|
|
|
++Current;
|
|
|
|
ptrdiff_t Offset = 1;
|
|
|
|
while (true) {
|
|
|
|
unsigned Tag = Current->Prev.getInt();
|
|
|
|
switch (Tag) {
|
|
|
|
case zeroDigitTag:
|
|
|
|
case oneDigitTag:
|
|
|
|
++Current;
|
|
|
|
Offset = (Offset << 1) + Tag;
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
return Current + Offset;
|
2014-03-04 08:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-04 08:53:41 +00:00
|
|
|
}
|
2014-03-04 08:51:00 +00:00
|
|
|
|
2014-03-04 08:53:41 +00:00
|
|
|
case fullStopTag:
|
|
|
|
return Current;
|
2014-03-04 08:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-10 08:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // End llvm namespace
|