LLVM 22.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->uses().empty())
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, OtherV](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 Ctx.runSetUseCallbacks(UseToReplace, OtherV);
63 return true;
64 });
65}
66
68 assert(getType() == Other->getType() &&
69 "Replacing with Value of different type!");
70 auto &Tracker = Ctx.getTracker();
71 for (auto Use : uses()) {
73 if (Tracker.isTracking())
74 Tracker.track(std::make_unique<UseSet>(Use));
75 }
76 // We are delegating RAUW to LLVM IR's RAUW.
78}
79
80#ifndef NDEBUG
81std::string Value::getUid() const {
82 std::stringstream SS;
83 SS << "SB" << UID << ".";
84 return SS.str();
85}
86
88 OS << getUid() << " " << getSubclassIDStr(SubclassID) << " ";
89}
90
92 OS.indent(2) << "Val: ";
93 if (Val)
94 OS << *Val;
95 else
96 OS << "NULL";
97 OS << "\n";
98}
99
101 if (Val)
102 OS << *Val;
103 else
104 OS << "NULL ";
105}
106
108 OS << " ; " << getUid() << " (" << getSubclassIDStr(SubclassID) << ")";
109}
110
112 if (Val)
114 else
115 OS << "NULL ";
116}
117
118void Value::dump() const {
119 dumpOS(dbgs());
120 dbgs() << "\n";
121}
122#endif // NDEBUG
123
124} // namespace llvm::sandboxir
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
raw_pwrite_stream & OS
A Use represents the edge between a Value definition and its users.
Definition: Use.h:35
User * getUser() const
Returns the User that contains this Use.
Definition: Use.h:61
LLVM Value Representation.
Definition: Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:546
iterator_range< user_iterator > users()
Definition: Value.h:426
use_iterator use_begin()
Definition: Value.h:364
LLVM_ABI 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:5305
LLVM_ABI 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:554
iterator_range< use_iterator > uses()
Definition: Value.h:380
use_iterator use_end()
Definition: Value.h:372
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:53
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
LLVM_ABI sandboxir::Value * getValue(llvm::Value *V) const
Definition: Context.cpp:629
Type * getType(llvm::Type *LLVMTy)
Definition: Context.h:262
Tracker & getTracker()
Definition: Context.h:245
LLVM_ABI void runSetUseCallbacks(const Use &U, Value *NewSrc)
Definition: Context.cpp:718
size_t getNumValues() const
\Returns the number of values registered with Context.
Definition: Context.h:283
The tracker collects all the change objects and implements the main API for saving / reverting / acce...
Definition: Tracker.h:442
bool isTracking() const
\Returns true if the tracker is recording changes.
Definition: Tracker.h:507
void track(std::unique_ptr< IRChangeBase > &&Change)
Record Change and take ownership.
Definition: Tracker.h:483
bool emplaceIfTracking(ArgsT... Args)
A convenience wrapper for track() that constructs and tracks the Change object if tracking is enabled...
Definition: Tracker.h:500
Just like llvm::Type these are immutable, unique, never get freed and can only be created via static ...
Definition: Type.h:47
Represents a Def-use/Use-def edge in SandboxIR.
Definition: Use.h:33
Iterator for the Use edges of a Value's users.
Definition: Value.h:40
A sandboxir::User has operands.
Definition: User.h:59
A SandboxIR Value has users. This is the base class.
Definition: Value.h:66
mapped_iterator< sandboxir::UserUseIterator, UseToUser > user_iterator
Definition: Value.h:217
llvm::Value * Val
The LLVM Value that corresponds to this SandboxIR Value.
Definition: Value.h:106
void dumpCommonFooter(raw_ostream &OS) const
Definition: Value.cpp:91
virtual void dumpCommonHeader(raw_ostream &OS) const
Definition: Value.cpp:87
void dumpCommonSuffix(raw_ostream &OS) const
Definition: Value.cpp:107
LLVM_ABI void replaceAllUsesWith(Value *Other)
Definition: Value.cpp:67
LLVM_ABI unsigned getNumUses() const
\Returns the number of user edges (not necessarily to unique users).
Definition: Value.cpp:44
UserUseIterator use_iterator
Definition: Value.h:193
void printAsOperandCommon(raw_ostream &OS) const
Definition: Value.cpp:111
Context & Ctx
All values point to the context.
Definition: Value.h:179
ClassID SubclassID
For isa/dyn_cast.
Definition: Value.h:97
LLVM_DUMP_METHOD void dump() const
Definition: Value.cpp:118
LLVM_ABI Type * getType() const
Definition: Value.cpp:46
LLVM_ABI Value(ClassID SubclassID, llvm::Value *Val, Context &Ctx)
Definition: Value.cpp:16
friend class Use
Definition: Value.h:110
std::string getUid() const
Returns the unique id in the form 'SB<number>.' like 'SB1.'.
Definition: Value.cpp:81
LLVM_ABI 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:100
virtual void dumpOS(raw_ostream &OS) const =0
void dumpCommonPrefix(raw_ostream &OS) const
Definition: Value.cpp:100
iterator_range< use_iterator > uses()
Definition: Value.h:205
static const char * getSubclassIDStr(ClassID ID)
Definition: Value.h:77
LLVM_ABI use_iterator use_begin()
Definition: Value.cpp:23
LLVM_ABI 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:1727
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
@ Other
Any other memory.