1 //===- Value.cpp - The Value class of Sandbox IR --------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/SandboxIR/Value.h"
10 #include "llvm/SandboxIR/Context.h"
11 #include "llvm/SandboxIR/User.h"
14 namespace llvm::sandboxir
{
16 Value::Value(ClassID SubclassID
, llvm::Value
*Val
, Context
&Ctx
)
17 : SubclassID(SubclassID
), Val(Val
), Ctx(Ctx
) {
19 UID
= Ctx
.getNumValues();
23 Value::use_iterator
Value::use_begin() {
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()))
30 return use_iterator(Use(LLVMUse
, User
, Ctx
));
33 Value::user_iterator
Value::user_begin() {
34 auto UseBegin
= Val
->use_begin();
35 auto UseEnd
= Val
->use_end();
36 bool AtEnd
= UseBegin
== UseEnd
;
37 llvm::Use
*LLVMUse
= AtEnd
? nullptr : &*UseBegin
;
40 : cast_or_null
<sandboxir::User
>(Ctx
.getValue(&*LLVMUse
->getUser()));
41 return user_iterator(Use(LLVMUse
, User
, Ctx
), UseToUser());
44 unsigned Value::getNumUses() const { return range_size(Val
->users()); }
46 Type
*Value::getType() const { return Ctx
.getType(Val
->getType()); }
48 void Value::replaceUsesWithIf(
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.
53 Val
->replaceUsesWithIf(
54 OtherVal
, [&ShouldReplace
, this](llvm::Use
&LLVMUse
) -> bool {
55 User
*DstU
= cast_or_null
<User
>(Ctx
.getValue(LLVMUse
.getUser()));
58 Use
UseToReplace(&LLVMUse
, DstU
, Ctx
);
59 if (!ShouldReplace(UseToReplace
))
61 Ctx
.getTracker().emplaceIfTracking
<UseSet
>(UseToReplace
);
66 void Value::replaceAllUsesWith(Value
*Other
) {
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
));
74 // We are delegating RAUW to LLVM IR's RAUW.
75 Val
->replaceAllUsesWith(Other
->Val
);
79 std::string
Value::getUid() const {
81 SS
<< "SB" << UID
<< ".";
85 void Value::dumpCommonHeader(raw_ostream
&OS
) const {
86 OS
<< getUid() << " " << getSubclassIDStr(SubclassID
) << " ";
89 void Value::dumpCommonFooter(raw_ostream
&OS
) const {
90 OS
.indent(2) << "Val: ";
98 void Value::dumpCommonPrefix(raw_ostream
&OS
) const {
105 void Value::dumpCommonSuffix(raw_ostream
&OS
) const {
106 OS
<< " ; " << getUid() << " (" << getSubclassIDStr(SubclassID
) << ")";
109 void Value::printAsOperandCommon(raw_ostream
&OS
) const {
111 Val
->printAsOperand(OS
);
116 void Value::dump() const {
122 } // namespace llvm::sandboxir