Path: blob/main/contrib/llvm-project/llvm/lib/SandboxIR/Value.cpp
213766 views
//===- Value.cpp - The Value 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/Value.h"9#include "llvm/SandboxIR/Context.h"10#include "llvm/SandboxIR/User.h"11#include <sstream>1213namespace llvm::sandboxir {1415Value::Value(ClassID SubclassID, llvm::Value *Val, Context &Ctx)16: SubclassID(SubclassID), Val(Val), Ctx(Ctx) {17#ifndef NDEBUG18UID = Ctx.getNumValues();19#endif20}2122Value::use_iterator Value::use_begin() {23llvm::Use *LLVMUse = nullptr;24if (Val->use_begin() != Val->use_end())25LLVMUse = &*Val->use_begin();26User *User = LLVMUse != nullptr ? cast_or_null<sandboxir::User>(Ctx.getValue(27Val->use_begin()->getUser()))28: nullptr;29return use_iterator(Use(LLVMUse, User, Ctx));30}3132Value::user_iterator Value::user_begin() {33auto UseBegin = Val->use_begin();34auto UseEnd = Val->use_end();35bool AtEnd = UseBegin == UseEnd;36llvm::Use *LLVMUse = AtEnd ? nullptr : &*UseBegin;37User *User =38AtEnd ? nullptr39: cast_or_null<sandboxir::User>(Ctx.getValue(&*LLVMUse->getUser()));40return user_iterator(Use(LLVMUse, User, Ctx), UseToUser());41}4243unsigned Value::getNumUses() const { return range_size(Val->users()); }4445Type *Value::getType() const { return Ctx.getType(Val->getType()); }4647void Value::replaceUsesWithIf(48Value *OtherV, llvm::function_ref<bool(const Use &)> ShouldReplace) {49assert(getType() == OtherV->getType() && "Can't replace with different type");50llvm::Value *OtherVal = OtherV->Val;51// We are delegating RUWIf to LLVM IR's RUWIf.52Val->replaceUsesWithIf(53OtherVal, [&ShouldReplace, this, OtherV](llvm::Use &LLVMUse) -> bool {54User *DstU = cast_or_null<User>(Ctx.getValue(LLVMUse.getUser()));55if (DstU == nullptr)56return false;57Use UseToReplace(&LLVMUse, DstU, Ctx);58if (!ShouldReplace(UseToReplace))59return false;60Ctx.getTracker().emplaceIfTracking<UseSet>(UseToReplace);61Ctx.runSetUseCallbacks(UseToReplace, OtherV);62return true;63});64}6566void Value::replaceAllUsesWith(Value *Other) {67assert(getType() == Other->getType() &&68"Replacing with Value of different type!");69auto &Tracker = Ctx.getTracker();70for (auto Use : uses()) {71Ctx.runSetUseCallbacks(Use, Other);72if (Tracker.isTracking())73Tracker.track(std::make_unique<UseSet>(Use));74}75// We are delegating RAUW to LLVM IR's RAUW.76Val->replaceAllUsesWith(Other->Val);77}7879#ifndef NDEBUG80std::string Value::getUid() const {81std::stringstream SS;82SS << "SB" << UID << ".";83return SS.str();84}8586void Value::dumpCommonHeader(raw_ostream &OS) const {87OS << getUid() << " " << getSubclassIDStr(SubclassID) << " ";88}8990void Value::dumpCommonFooter(raw_ostream &OS) const {91OS.indent(2) << "Val: ";92if (Val)93OS << *Val;94else95OS << "NULL";96OS << "\n";97}9899void Value::dumpCommonPrefix(raw_ostream &OS) const {100if (Val)101OS << *Val;102else103OS << "NULL ";104}105106void Value::dumpCommonSuffix(raw_ostream &OS) const {107OS << " ; " << getUid() << " (" << getSubclassIDStr(SubclassID) << ")";108}109110void Value::printAsOperandCommon(raw_ostream &OS) const {111if (Val)112Val->printAsOperand(OS);113else114OS << "NULL ";115}116117void Value::dump() const {118dumpOS(dbgs());119dbgs() << "\n";120}121#endif // NDEBUG122123} // namespace llvm::sandboxir124125126