LLVM 20.0.0git
Value.cpp
Go to the documentation of this file.
1//===- Value.cpp - The Value class of Sandbox IR --------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
11#include "llvm/SandboxIR/User.h"
12#include <sstream>
13
14namespace llvm::sandboxir {
15
16Value::Value(ClassID SubclassID, llvm::Value *Val, Context &Ctx)
17 : SubclassID(SubclassID), Val(Val), Ctx(Ctx) {
18#ifndef NDEBUG
20#endif
21}
22
24 llvm::Use *LLVMUse = nullptr;
25 if (Val->use_begin() != Val->use_end())
26 LLVMUse = &*Val->use_begin();
27 User *User = LLVMUse != nullptr ? cast_or_null<sandboxir::User>(Ctx.getValue(
28 Val->use_begin()->getUser()))
29 : nullptr;
30 return use_iterator(Use(LLVMUse, User, Ctx));
31}
32
34 auto UseBegin = Val->use_begin();
35 auto UseEnd = Val->use_end();
36 bool AtEnd = UseBegin == UseEnd;
37 llvm::Use *LLVMUse = AtEnd ? nullptr : &*UseBegin;
38 User *User =
39 AtEnd ? nullptr
40 : cast_or_null<sandboxir::User>(Ctx.getValue(&*LLVMUse->getUser()));
41 return user_iterator(Use(LLVMUse, User, Ctx), UseToUser());
42}
43
44unsigned Value::getNumUses() const { return range_size(Val->users()); }
45
46Type *Value::getType() const { return Ctx.getType(Val->getType()); }
47
49 Value *OtherV, llvm::function_ref<bool(const Use &)> ShouldReplace) {
50 assert(getType() == OtherV->getType() && "Can't replace with different type");
51 llvm::Value *OtherVal = OtherV->Val;
52 // We are delegating RUWIf to LLVM IR's RUWIf.
54 OtherVal, [&ShouldReplace, this](llvm::Use &LLVMUse) -> bool {
55 User *DstU = cast_or_null<User>(Ctx.getValue(LLVMUse.getUser()));
56 if (DstU == nullptr)
57 return false;
58 Use UseToReplace(&LLVMUse, DstU, Ctx);
59 if (!ShouldReplace(UseToReplace))
60 return false;
61 Ctx.getTracker().emplaceIfTracking<UseSet>(UseToReplace);
62 return true;
63 });
64}
65
67 assert(getType() == Other->getType() &&
68 "Replacing with Value of different type!");
69 auto &Tracker = Ctx.getTracker();
70 if (Tracker.isTracking()) {
71 for (auto Use : uses())
72 Tracker.track(std::make_unique<UseSet>(Use));
73 }
74 // We are delegating RAUW to LLVM IR's RAUW.
77
78#ifndef NDEBUG
79std::string Value::getUid() const {
80 std::stringstream SS;
81 SS << "SB" << UID << ".";
82 return SS.str();
83}
84
86 OS << getUid() << " " << getSubclassIDStr(SubclassID) << " ";
87}
88
90 OS.indent(2) << "Val: ";
91 if (Val)
92 OS << *Val;
93 else
94 OS << "NULL";
95 OS << "\n";
96}
97
99 if (Val)
100 OS << *Val;
101 else
102 OS << "NULL ";
103}
104
106 OS << " ; " << getUid() << " (" << getSubclassIDStr(SubclassID) << ")";
107}
108
110 if (Val)
112 else
113 OS << "NULL ";
114}
115
116void Value::dump() const {
117 dumpOS(dbgs());
118 dbgs() << "\n";
119}
120#endif // NDEBUG
121
122} // namespace llvm::sandboxir
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
User * getUser() const
Returns the User that contains this Use.
Definition: Use.h:72
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
iterator_range< user_iterator > users()
Definition: Value.h:421
use_iterator use_begin()
Definition: Value.h:360
void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
Definition: AsmWriter.cpp:5144
void replaceUsesWithIf(Value *New, llvm::function_ref< bool(Use &U)> ShouldReplace)
Go through the uses list for this definition and make each use point to "V" if the callback ShouldRep...
Definition: Value.cpp:542
use_iterator use_end()
Definition: Value.h:368
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
sandboxir::Value * getValue(llvm::Value *V) const
Definition: Context.cpp:601
Type * getType(llvm::Type *LLVMTy)
Definition: Context.h:220
Tracker & getTracker()
Definition: Context.h:203
size_t getNumValues() const
\Returns the number of values registered with Context.
Definition: Context.h:241
The tracker collects all the change objects and implements the main API for saving / reverting / acce...
Definition: Tracker.h:440
bool isTracking() const
\Returns true if the tracker is recording changes.
Definition: Tracker.h:502
void track(std::unique_ptr< IRChangeBase > &&Change)
Record Change and take ownership.
Definition: Tracker.h:478
bool emplaceIfTracking(ArgsT... Args)
A convenience wrapper for track() that constructs and tracks the Change object if tracking is enabled...
Definition: Tracker.h:495
Just like llvm::Type these are immutable, unique, never get freed and can only be created via static ...
Definition: Type.h:43
Represents a Def-use/Use-def edge in SandboxIR.
Definition: Use.h:32
Iterator for the Use edges of a Value's users.
Definition: Value.h:37
A sandboxir::User has operands.
Definition: User.h:58
A SandboxIR Value has users. This is the base class.
Definition: Value.h:63
mapped_iterator< sandboxir::UserUseIterator, UseToUser > user_iterator
Definition: Value.h:210
llvm::Value * Val
The LLVM Value that corresponds to this SandboxIR Value.
Definition: Value.h:103
void dumpCommonFooter(raw_ostream &OS) const
Definition: Value.cpp:89
virtual void dumpCommonHeader(raw_ostream &OS) const
Definition: Value.cpp:85
void dumpCommonSuffix(raw_ostream &OS) const
Definition: Value.cpp:105
void replaceAllUsesWith(Value *Other)
Definition: Value.cpp:66
unsigned getNumUses() const
\Returns the number of user edges (not necessarily to unique users).
Definition: Value.cpp:44
UserUseIterator use_iterator
Definition: Value.h:186
void printAsOperandCommon(raw_ostream &OS) const
Definition: Value.cpp:109
Context & Ctx
All values point to the context.
Definition: Value.h:172
ClassID SubclassID
For isa/dyn_cast.
Definition: Value.h:94
LLVM_DUMP_METHOD void dump() const
Definition: Value.cpp:116
Type * getType() const
Definition: Value.cpp:46
Value(ClassID SubclassID, llvm::Value *Val, Context &Ctx)
Definition: Value.cpp:16
friend class Use
Definition: Value.h:107
std::string getUid() const
Returns the unique id in the form 'SB<number>.' like 'SB1.'.
Definition: Value.cpp:79
void replaceUsesWithIf(Value *OtherV, llvm::function_ref< bool(const Use &)> ShouldReplace)
Definition: Value.cpp:48
unsigned UID
A unique ID used for forming the name (used for debugging).
Definition: Value.h:97
virtual void dumpOS(raw_ostream &OS) const =0
void dumpCommonPrefix(raw_ostream &OS) const
Definition: Value.cpp:98
iterator_range< use_iterator > uses()
Definition: Value.h:198
static const char * getSubclassIDStr(ClassID ID)
Definition: Value.h:74
use_iterator use_begin()
Definition: Value.cpp:23
user_iterator user_begin()
Definition: Value.cpp:33
constexpr size_t range_size(R &&Range)
Returns the size of the Range, i.e., the number of elements.
Definition: STLExtras.h:1722
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
@ Other
Any other memory.