Path: blob/main/contrib/llvm-project/llvm/lib/SandboxIR/User.cpp
213766 views
//===- User.cpp - The User class of Sandbox IR ----------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/SandboxIR/User.h"9#include "llvm/SandboxIR/Context.h"1011namespace llvm::sandboxir {1213Use OperandUseIterator::operator*() const { return Use; }1415OperandUseIterator &OperandUseIterator::operator++() {16assert(Use.LLVMUse != nullptr && "Already at end!");17User *User = Use.getUser();18Use = User->getOperandUseInternal(Use.getOperandNo() + 1, /*Verify=*/false);19return *this;20}2122UserUseIterator &UserUseIterator::operator++() {23// Get the corresponding llvm::Use, get the next in the list, and update the24// sandboxir::Use.25llvm::Use *&LLVMUse = Use.LLVMUse;26assert(LLVMUse != nullptr && "Already at end!");27LLVMUse = LLVMUse->getNext();28if (LLVMUse == nullptr) {29Use.Usr = nullptr;30return *this;31}32auto *Ctx = Use.Ctx;33auto *LLVMUser = LLVMUse->getUser();34Use.Usr = cast_or_null<sandboxir::User>(Ctx->getValue(LLVMUser));35return *this;36}3738OperandUseIterator OperandUseIterator::operator+(unsigned Num) const {39sandboxir::Use U = Use.getUser()->getOperandUseInternal(40Use.getOperandNo() + Num, /*Verify=*/true);41return OperandUseIterator(U);42}4344OperandUseIterator OperandUseIterator::operator-(unsigned Num) const {45assert(Use.getOperandNo() >= Num && "Out of bounds!");46sandboxir::Use U = Use.getUser()->getOperandUseInternal(47Use.getOperandNo() - Num, /*Verify=*/true);48return OperandUseIterator(U);49}5051int OperandUseIterator::operator-(const OperandUseIterator &Other) const {52int ThisOpNo = Use.getOperandNo();53int OtherOpNo = Other.Use.getOperandNo();54return ThisOpNo - OtherOpNo;55}5657Use User::getOperandUseDefault(unsigned OpIdx, bool Verify) const {58assert((!Verify || OpIdx < getNumOperands()) && "Out of bounds!");59assert(isa<llvm::User>(Val) && "Non-users have no operands!");60llvm::Use *LLVMUse;61if (OpIdx != getNumOperands())62LLVMUse = &cast<llvm::User>(Val)->getOperandUse(OpIdx);63else64LLVMUse = cast<llvm::User>(Val)->op_end();65return Use(LLVMUse, const_cast<User *>(this), Ctx);66}6768#ifndef NDEBUG69void User::verifyUserOfLLVMUse(const llvm::Use &Use) const {70assert(Ctx.getValue(Use.getUser()) == this &&71"Use not found in this SBUser's operands!");72}73#endif7475bool User::classof(const Value *From) {76switch (From->getSubclassID()) {77#define DEF_VALUE(ID, CLASS)78#define DEF_USER(ID, CLASS) \79case ClassID::ID: \80return true;81#define DEF_INSTR(ID, OPC, CLASS) \82case ClassID::ID: \83return true;84#include "llvm/SandboxIR/Values.def"85default:86return false;87}88}8990void User::setOperand(unsigned OperandIdx, Value *Operand) {91assert(isa<llvm::User>(Val) && "No operands!");92const auto &U = getOperandUse(OperandIdx);93Ctx.getTracker().emplaceIfTracking<UseSet>(U);94Ctx.runSetUseCallbacks(U, Operand);95// We are delegating to llvm::User::setOperand().96cast<llvm::User>(Val)->setOperand(OperandIdx, Operand->Val);97}9899bool User::replaceUsesOfWith(Value *FromV, Value *ToV) {100auto &Tracker = Ctx.getTracker();101for (auto OpIdx : seq<unsigned>(0, getNumOperands())) {102auto Use = getOperandUse(OpIdx);103if (Use.get() == FromV) {104Ctx.runSetUseCallbacks(Use, ToV);105if (Tracker.isTracking())106Tracker.emplaceIfTracking<UseSet>(Use);107}108}109// We are delegating RUOW to LLVM IR's RUOW.110return cast<llvm::User>(Val)->replaceUsesOfWith(FromV->Val, ToV->Val);111}112113#ifndef NDEBUG114void User::dumpCommonHeader(raw_ostream &OS) const {115Value::dumpCommonHeader(OS);116// TODO: This is incomplete117}118#endif // NDEBUG119120} // namespace llvm::sandboxir121122123