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