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 #include "llvm/IR/Use.h"
11 #include "llvm/IR/User.h"
12 #include "llvm/IR/Value.h"
17 void Use::swap(Use &RHS) {
41 User *Use::getUser() const {
42 const Use *End = getImpliedUser();
43 const UserRef *ref = reinterpret_cast<const UserRef *>(End);
44 return ref->getInt() ? ref->getPointer()
45 : reinterpret_cast<User *>(const_cast<Use *>(End));
48 unsigned Use::getOperandNo() const {
49 return this - getUser()->op_begin();
52 // Sets up the waymarking algorithm's tags for a series of Uses. See the
53 // algorithm details here:
55 // http://www.llvm.org/docs/ProgrammersManual.html#the-waymarking-algorithm
57 Use *Use::initTags(Use *const Start, Use *Stop) {
62 static const PrevPtrTag tags[20] = {
63 fullStopTag, oneDigitTag, stopTag, oneDigitTag, oneDigitTag,
64 stopTag, zeroDigitTag, oneDigitTag, oneDigitTag, stopTag,
65 zeroDigitTag, oneDigitTag, zeroDigitTag, oneDigitTag, stopTag,
66 oneDigitTag, oneDigitTag, oneDigitTag, oneDigitTag, stopTag};
67 new (Stop) Use(tags[Done++]);
70 ptrdiff_t Count = Done;
71 while (Start != Stop) {
74 new (Stop) Use(stopTag);
78 new (Stop) Use(PrevPtrTag(Count & 1));
87 void Use::zap(Use *Start, const Use *Stop, bool del) {
91 ::operator delete(Start);
94 const Use *Use::getImpliedUser() const {
95 const Use *Current = this;
98 unsigned Tag = (Current++)->Prev.getInt();
106 ptrdiff_t Offset = 1;
108 unsigned Tag = Current->Prev.getInt();
113 Offset = (Offset << 1) + Tag;
116 return Current + Offset;
127 } // End llvm namespace