1*0b57cec5SDimitry Andric //===-- Use.cpp - Implement the Use class ---------------------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric 9*0b57cec5SDimitry Andric #include "llvm/IR/Use.h" 10*0b57cec5SDimitry Andric #include "llvm/IR/User.h" 11*0b57cec5SDimitry Andric #include "llvm/IR/Value.h" 12*0b57cec5SDimitry Andric #include <new> 13*0b57cec5SDimitry Andric 14*0b57cec5SDimitry Andric namespace llvm { 15*0b57cec5SDimitry Andric 16*0b57cec5SDimitry Andric void Use::swap(Use &RHS) { 17*0b57cec5SDimitry Andric if (Val == RHS.Val) 18*0b57cec5SDimitry Andric return; 19*0b57cec5SDimitry Andric 20*0b57cec5SDimitry Andric if (Val) 21*0b57cec5SDimitry Andric removeFromList(); 22*0b57cec5SDimitry Andric 23*0b57cec5SDimitry Andric Value *OldVal = Val; 24*0b57cec5SDimitry Andric if (RHS.Val) { 25*0b57cec5SDimitry Andric RHS.removeFromList(); 26*0b57cec5SDimitry Andric Val = RHS.Val; 27*0b57cec5SDimitry Andric Val->addUse(*this); 28*0b57cec5SDimitry Andric } else { 29*0b57cec5SDimitry Andric Val = nullptr; 30*0b57cec5SDimitry Andric } 31*0b57cec5SDimitry Andric 32*0b57cec5SDimitry Andric if (OldVal) { 33*0b57cec5SDimitry Andric RHS.Val = OldVal; 34*0b57cec5SDimitry Andric RHS.Val->addUse(RHS); 35*0b57cec5SDimitry Andric } else { 36*0b57cec5SDimitry Andric RHS.Val = nullptr; 37*0b57cec5SDimitry Andric } 38*0b57cec5SDimitry Andric } 39*0b57cec5SDimitry Andric 40*0b57cec5SDimitry Andric User *Use::getUser() const { 41*0b57cec5SDimitry Andric const Use *End = getImpliedUser(); 42*0b57cec5SDimitry Andric const UserRef *ref = reinterpret_cast<const UserRef *>(End); 43*0b57cec5SDimitry Andric return ref->getInt() ? ref->getPointer() 44*0b57cec5SDimitry Andric : reinterpret_cast<User *>(const_cast<Use *>(End)); 45*0b57cec5SDimitry Andric } 46*0b57cec5SDimitry Andric 47*0b57cec5SDimitry Andric unsigned Use::getOperandNo() const { 48*0b57cec5SDimitry Andric return this - getUser()->op_begin(); 49*0b57cec5SDimitry Andric } 50*0b57cec5SDimitry Andric 51*0b57cec5SDimitry Andric // Sets up the waymarking algorithm's tags for a series of Uses. See the 52*0b57cec5SDimitry Andric // algorithm details here: 53*0b57cec5SDimitry Andric // 54*0b57cec5SDimitry Andric // http://www.llvm.org/docs/ProgrammersManual.html#the-waymarking-algorithm 55*0b57cec5SDimitry Andric // 56*0b57cec5SDimitry Andric Use *Use::initTags(Use *const Start, Use *Stop) { 57*0b57cec5SDimitry Andric ptrdiff_t Done = 0; 58*0b57cec5SDimitry Andric while (Done < 20) { 59*0b57cec5SDimitry Andric if (Start == Stop--) 60*0b57cec5SDimitry Andric return Start; 61*0b57cec5SDimitry Andric static const PrevPtrTag tags[20] = { 62*0b57cec5SDimitry Andric fullStopTag, oneDigitTag, stopTag, oneDigitTag, oneDigitTag, 63*0b57cec5SDimitry Andric stopTag, zeroDigitTag, oneDigitTag, oneDigitTag, stopTag, 64*0b57cec5SDimitry Andric zeroDigitTag, oneDigitTag, zeroDigitTag, oneDigitTag, stopTag, 65*0b57cec5SDimitry Andric oneDigitTag, oneDigitTag, oneDigitTag, oneDigitTag, stopTag}; 66*0b57cec5SDimitry Andric new (Stop) Use(tags[Done++]); 67*0b57cec5SDimitry Andric } 68*0b57cec5SDimitry Andric 69*0b57cec5SDimitry Andric ptrdiff_t Count = Done; 70*0b57cec5SDimitry Andric while (Start != Stop) { 71*0b57cec5SDimitry Andric --Stop; 72*0b57cec5SDimitry Andric if (!Count) { 73*0b57cec5SDimitry Andric new (Stop) Use(stopTag); 74*0b57cec5SDimitry Andric ++Done; 75*0b57cec5SDimitry Andric Count = Done; 76*0b57cec5SDimitry Andric } else { 77*0b57cec5SDimitry Andric new (Stop) Use(PrevPtrTag(Count & 1)); 78*0b57cec5SDimitry Andric Count >>= 1; 79*0b57cec5SDimitry Andric ++Done; 80*0b57cec5SDimitry Andric } 81*0b57cec5SDimitry Andric } 82*0b57cec5SDimitry Andric 83*0b57cec5SDimitry Andric return Start; 84*0b57cec5SDimitry Andric } 85*0b57cec5SDimitry Andric 86*0b57cec5SDimitry Andric void Use::zap(Use *Start, const Use *Stop, bool del) { 87*0b57cec5SDimitry Andric while (Start != Stop) 88*0b57cec5SDimitry Andric (--Stop)->~Use(); 89*0b57cec5SDimitry Andric if (del) 90*0b57cec5SDimitry Andric ::operator delete(Start); 91*0b57cec5SDimitry Andric } 92*0b57cec5SDimitry Andric 93*0b57cec5SDimitry Andric const Use *Use::getImpliedUser() const { 94*0b57cec5SDimitry Andric const Use *Current = this; 95*0b57cec5SDimitry Andric 96*0b57cec5SDimitry Andric while (true) { 97*0b57cec5SDimitry Andric unsigned Tag = (Current++)->Prev.getInt(); 98*0b57cec5SDimitry Andric switch (Tag) { 99*0b57cec5SDimitry Andric case zeroDigitTag: 100*0b57cec5SDimitry Andric case oneDigitTag: 101*0b57cec5SDimitry Andric continue; 102*0b57cec5SDimitry Andric 103*0b57cec5SDimitry Andric case stopTag: { 104*0b57cec5SDimitry Andric ++Current; 105*0b57cec5SDimitry Andric ptrdiff_t Offset = 1; 106*0b57cec5SDimitry Andric while (true) { 107*0b57cec5SDimitry Andric unsigned Tag = Current->Prev.getInt(); 108*0b57cec5SDimitry Andric switch (Tag) { 109*0b57cec5SDimitry Andric case zeroDigitTag: 110*0b57cec5SDimitry Andric case oneDigitTag: 111*0b57cec5SDimitry Andric ++Current; 112*0b57cec5SDimitry Andric Offset = (Offset << 1) + Tag; 113*0b57cec5SDimitry Andric continue; 114*0b57cec5SDimitry Andric default: 115*0b57cec5SDimitry Andric return Current + Offset; 116*0b57cec5SDimitry Andric } 117*0b57cec5SDimitry Andric } 118*0b57cec5SDimitry Andric } 119*0b57cec5SDimitry Andric 120*0b57cec5SDimitry Andric case fullStopTag: 121*0b57cec5SDimitry Andric return Current; 122*0b57cec5SDimitry Andric } 123*0b57cec5SDimitry Andric } 124*0b57cec5SDimitry Andric } 125*0b57cec5SDimitry Andric 126*0b57cec5SDimitry Andric } // End llvm namespace 127