2008-05-15 18:04:30 +08:00
|
|
|
//===-- Use.cpp - Implement the Use class ---------------------------------===//
|
2008-05-10 16:32:32 +08: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 16:51:00 +08:00
|
|
|
#include "llvm/IR/Use.h"
|
2014-03-04 17:19:43 +08:00
|
|
|
#include "llvm/IR/User.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Value.h"
|
2012-03-26 22:04:17 +08:00
|
|
|
#include <new>
|
2008-05-10 16:32:32 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2008-05-14 06:51:52 +08:00
|
|
|
void Use::swap(Use &RHS) {
|
2014-03-04 17:00:15 +08:00
|
|
|
if (Val == RHS.Val)
|
|
|
|
return;
|
2008-05-14 06:51:52 +08:00
|
|
|
|
2014-03-04 17:00:15 +08:00
|
|
|
if (Val)
|
|
|
|
removeFromList();
|
2008-05-14 06:51:52 +08:00
|
|
|
|
2014-03-04 17:00:15 +08:00
|
|
|
Value *OldVal = Val;
|
|
|
|
if (RHS.Val) {
|
|
|
|
RHS.removeFromList();
|
|
|
|
Val = RHS.Val;
|
|
|
|
Val->addUse(*this);
|
|
|
|
} else {
|
2014-04-09 14:08:46 +08:00
|
|
|
Val = nullptr;
|
2014-03-04 17:00:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (OldVal) {
|
|
|
|
RHS.Val = OldVal;
|
|
|
|
RHS.Val->addUse(RHS);
|
|
|
|
} else {
|
2014-04-09 14:08:46 +08:00
|
|
|
RHS.Val = nullptr;
|
2008-05-14 06:51:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-04 16:51:00 +08:00
|
|
|
User *Use::getUser() const {
|
|
|
|
const Use *End = getImpliedUser();
|
2014-03-04 16:53:41 +08:00
|
|
|
const UserRef *ref = reinterpret_cast<const UserRef *>(End);
|
|
|
|
return ref->getInt() ? ref->getPointer()
|
|
|
|
: reinterpret_cast<User *>(const_cast<Use *>(End));
|
2008-05-10 16:32:32 +08:00
|
|
|
}
|
|
|
|
|
2014-03-04 17:19:43 +08:00
|
|
|
unsigned Use::getOperandNo() const {
|
|
|
|
return this - getUser()->op_begin();
|
|
|
|
}
|
|
|
|
|
2014-04-09 22:47:27 +08:00
|
|
|
// Sets up the waymarking algorithm's tags for a series of Uses. See the
|
2014-03-04 16:51:00 +08:00
|
|
|
// algorithm details here:
|
|
|
|
//
|
2014-08-22 05:09:24 +08:00
|
|
|
// http://www.llvm.org/docs/ProgrammersManual.html#the-waymarking-algorithm
|
2014-03-04 16:51:00 +08:00
|
|
|
//
|
2014-03-04 16:53:41 +08:00
|
|
|
Use *Use::initTags(Use *const Start, Use *Stop) {
|
2011-01-16 23:30:52 +08:00
|
|
|
ptrdiff_t Done = 0;
|
2010-07-19 22:48:15 +08:00
|
|
|
while (Done < 20) {
|
2010-07-17 04:35:19 +08:00
|
|
|
if (Start == Stop--)
|
|
|
|
return Start;
|
2014-03-04 16:53:41 +08: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-17 04:35:19 +08:00
|
|
|
}
|
|
|
|
|
2008-05-10 16:32:32 +08:00
|
|
|
ptrdiff_t Count = Done;
|
|
|
|
while (Start != Stop) {
|
|
|
|
--Stop;
|
|
|
|
if (!Count) {
|
2014-03-04 16:53:41 +08:00
|
|
|
new (Stop) Use(stopTag);
|
2008-05-10 16:32:32 +08:00
|
|
|
++Done;
|
|
|
|
Count = Done;
|
|
|
|
} else {
|
2014-03-04 16:53:41 +08:00
|
|
|
new (Stop) Use(PrevPtrTag(Count & 1));
|
2008-05-10 16:32:32 +08:00
|
|
|
Count >>= 1;
|
|
|
|
++Done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Start;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Use::zap(Use *Start, const Use *Stop, bool del) {
|
2011-01-16 23:30:52 +08:00
|
|
|
while (Start != Stop)
|
|
|
|
(--Stop)->~Use();
|
|
|
|
if (del)
|
2008-05-10 16:32:32 +08:00
|
|
|
::operator delete(Start);
|
|
|
|
}
|
|
|
|
|
2014-03-04 16:51:00 +08:00
|
|
|
const Use *Use::getImpliedUser() const {
|
|
|
|
const Use *Current = this;
|
2008-05-10 16:32:32 +08:00
|
|
|
|
2014-03-04 16:51:00 +08:00
|
|
|
while (true) {
|
|
|
|
unsigned Tag = (Current++)->Prev.getInt();
|
|
|
|
switch (Tag) {
|
2014-03-04 16:53:41 +08:00
|
|
|
case zeroDigitTag:
|
|
|
|
case oneDigitTag:
|
|
|
|
continue;
|
2014-03-04 16:51:00 +08:00
|
|
|
|
2014-03-04 16:53:41 +08: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 16:51:00 +08:00
|
|
|
}
|
|
|
|
}
|
2014-03-04 16:53:41 +08:00
|
|
|
}
|
2014-03-04 16:51:00 +08:00
|
|
|
|
2014-03-04 16:53:41 +08:00
|
|
|
case fullStopTag:
|
|
|
|
return Current;
|
2014-03-04 16:51:00 +08:00
|
|
|
}
|
|
|
|
}
|
2008-05-10 16:32:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // End llvm namespace
|