LLVM 24.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
18#ifndef NDEBUG
19 UID = Ctx.getNumValues();
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()); }
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, 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()) {
72 Ctx.runSetUseCallbacks(Use, Other);
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
81 const DataLayout &DL, APInt &Offset, bool AllowNonInbounds,
82 bool AllowInvariantGroup,
83 function_ref<bool(Value &Value, APInt &Offset)> ExternalAnalysis,
84 bool LookThroughIntToPtr) const {
85 auto LLVMExternalAnalysisLambda =
86 [&ExternalAnalysis, this](llvm::Value &LLVMValue, APInt &Offset) -> bool {
87 Value &ValueRef = *Ctx.getValue(&LLVMValue);
88 return ExternalAnalysis(ValueRef, Offset);
89 };
90 function_ref<bool(llvm::Value &, APInt & Offset)> LLVMExternalAnalysis =
91 ExternalAnalysis ? LLVMExternalAnalysisLambda
92 : decltype(LLVMExternalAnalysis)(nullptr);
93 const llvm::Value *LLVMV = Val->stripAndAccumulateConstantOffsets(
94 DL, Offset, AllowNonInbounds, AllowInvariantGroup, LLVMExternalAnalysis);
95 return Ctx.getValue(LLVMV);
96}
97
99 const DataLayout &DL, APInt &Offset, bool AllowNonInbounds,
100 bool AllowInvariantGroup,
101 function_ref<bool(Value &Value, APInt &Offset)> ExternalAnalysis,
102 bool LookThroughIntToPtr) {
103 auto LLVMExternalAnalysisLambda =
104 [&ExternalAnalysis, this](llvm::Value &LLVMValue, APInt &Offset) -> bool {
105 Value &ValueRef = *Ctx.getValue(&LLVMValue);
106 return ExternalAnalysis(ValueRef, Offset);
107 };
108 function_ref<bool(llvm::Value &, APInt & Offset)> LLVMExternalAnalysis =
109 ExternalAnalysis ? LLVMExternalAnalysisLambda
110 : decltype(LLVMExternalAnalysis)(nullptr);
112 DL, Offset, AllowNonInbounds, AllowInvariantGroup, LLVMExternalAnalysis);
113 return Ctx.getValue(LLVMV);
114}
115
116#ifndef NDEBUG
117std::string Value::getUid() const {
118 std::stringstream SS;
119 SS << "SB" << UID << ".";
120 return SS.str();
121}
122
124 OS << getUid() << " " << getSubclassIDStr(SubclassID) << " ";
125}
126
128 OS.indent(2) << "Val: ";
129 if (Val)
130 OS << *Val;
131 else
132 OS << "NULL";
133 OS << "\n";
134}
135
137 if (Val)
138 OS << *Val;
139 else
140 OS << "NULL ";
141}
142
144 OS << " ; " << getUid() << " (" << getSubclassIDStr(SubclassID) << ")";
145}
146
148 if (Val)
149 Val->printAsOperand(OS);
150 else
151 OS << "NULL ";
152}
153
154void Value::dump() const {
156 dbgs() << "\n";
157}
158#endif // NDEBUG
159
160} // namespace llvm::sandboxir
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Class for arbitrary precision integers.
Definition APInt.h:78
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
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
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:553
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.
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
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.
The tracker collects all the change objects and implements the main API for saving / reverting / acce...
Definition Tracker.h:450
bool isTracking() const
\Returns true if the tracker is recording changes.
Definition Tracker.h:512
void track(std::unique_ptr< IRChangeBase > &&Change)
Record Change and take ownership.
Definition Tracker.h:488
Just like llvm::Type these are immutable, unique, never get freed and can only be created via static ...
Definition Type.h:49
mapped_iterator< sandboxir::UserUseIterator, UseToUser > user_iterator
Definition Value.h:239
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Definition Value.cpp:80
llvm::Value * Val
The LLVM Value that corresponds to this SandboxIR Value.
Definition Value.h:122
void dumpCommonFooter(raw_ostream &OS) const
Definition Value.cpp:127
virtual void dumpCommonHeader(raw_ostream &OS) const
Definition Value.cpp:123
void dumpCommonSuffix(raw_ostream &OS) const
Definition Value.cpp:143
LLVM_ABI void replaceAllUsesWith(Value *Other)
Definition Value.cpp:67
friend class User
Definition Value.h:125
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:215
void printAsOperandCommon(raw_ostream &OS) const
Definition Value.cpp:147
Context & Ctx
All values point to the context.
Definition Value.h:201
ClassID SubclassID
For isa/dyn_cast.
Definition Value.h:113
LLVM_DUMP_METHOD void dump() const
Definition Value.cpp:154
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:126
std::string getUid() const
Returns the unique id in the form 'SB<number>.' like 'SB1.'.
Definition Value.cpp:117
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:116
friend class Context
Definition Value.h:124
virtual void dumpOS(raw_ostream &OS) const =0
void dumpCommonPrefix(raw_ostream &OS) const
Definition Value.cpp:136
iterator_range< use_iterator > uses()
Definition Value.h:227
static const char * getSubclassIDStr(ClassID ID)
Definition Value.h:88
LLVM_ABI use_iterator use_begin()
Definition Value.cpp:23
LLVM_ABI user_iterator user_begin()
Definition Value.cpp:33
@ Offset
Definition DWP.cpp:577
auto cast_or_null(const Y &Val)
Definition Casting.h:714
constexpr size_t range_size(R &&Range)
Returns the size of the Range, i.e., the number of elements.
Definition STLExtras.h:1710
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
@ Other
Any other memory.
Definition ModRef.h:68