LLVM 22.0.0git
BasicBlock.cpp
Go to the documentation of this file.
1//===- BasicBlock.cpp - The BasicBlock 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
13
14namespace llvm::sandboxir {
15
17 auto ItE = BB->end();
18 assert(It != ItE && "Already at end!");
19 ++It;
20 if (It == ItE)
21 return *this;
22 Instruction &NextI = *cast<sandboxir::Instruction>(Ctx->getValue(&*It));
23 unsigned Num = NextI.getNumOfIRInstrs();
24 assert(Num > 0 && "Bad getNumOfIRInstrs()");
25 It = std::next(It, Num - 1);
26 return *this;
27}
28
30 assert(It != BB->begin() && "Already at begin!");
31 if (It == BB->end()) {
32 --It;
33 return *this;
34 }
35 Instruction &CurrI = **this;
36 unsigned Num = CurrI.getNumOfIRInstrs();
37 assert(Num > 0 && "Bad getNumOfIRInstrs()");
38 assert(std::prev(It, Num - 1) != BB->begin() && "Already at begin!");
39 It = std::prev(It, Num);
40 return *this;
41}
44 llvm::BasicBlock *Parent = const_cast<BBIterator *>(this)->It.getNodeParent();
45 return cast<BasicBlock>(Ctx->getValue(Parent));
46}
47
49BasicBlock::iterator::getInstr(llvm::BasicBlock::iterator It) const {
50 return cast_or_null<Instruction>(Ctx->getValue(&*It));
51}
52
54 auto *BB = cast<llvm::BasicBlock>(Val);
55 auto *F = BB->getParent();
56 if (F == nullptr)
57 // Detached
58 return nullptr;
59 return cast_or_null<Function>(Ctx.getValue(F));
60}
61
62void BasicBlock::buildBasicBlockFromLLVMIR(llvm::BasicBlock *LLVMBB) {
63 for (llvm::Instruction &IRef : reverse(*LLVMBB)) {
64 llvm::Instruction *I = &IRef;
66 for (auto [OpIdx, Op] : enumerate(I->operands())) {
67 // Skip instruction's label operands
68 if (isa<llvm::BasicBlock>(Op))
69 continue;
71 }
72 }
73#if !defined(NDEBUG)
74 verify();
75#endif
76}
77
79 llvm::BasicBlock *BB = cast<llvm::BasicBlock>(Val);
81 if (!BB->empty()) {
82 auto *V = Ctx.getValue(&*BB->begin());
83 assert(V != nullptr && "No SandboxIR for BB->begin()!");
84 auto *I = cast<Instruction>(V);
85 unsigned Num = I->getNumOfIRInstrs();
86 assert(Num >= 1u && "Bad getNumOfIRInstrs()");
87 It = std::next(It, Num - 1);
88 }
89 return iterator(BB, It, &Ctx);
90}
91
93 auto *TerminatorV =
94 Ctx.getValue(cast<llvm::BasicBlock>(Val)->getTerminator());
95 return cast_or_null<Instruction>(TerminatorV);
96}
97
99 auto *BB = cast<llvm::BasicBlock>(Val);
100 assert(!BB->empty() && "Empty block!");
101 auto *SBI = cast<Instruction>(getContext().getValue(&*BB->begin()));
102 assert(SBI != nullptr && "Expected Instr!");
103 return *SBI;
104}
105
107 auto *BB = cast<llvm::BasicBlock>(Val);
108 assert(!BB->empty() && "Empty block!");
109 auto *SBI = cast<Instruction>(getContext().getValue(&*BB->rbegin()));
110 assert(SBI != nullptr && "Expected Instr!");
111 return *SBI;
112}
113
114#ifndef NDEBUG
116 llvm::BasicBlock *BB = cast<llvm::BasicBlock>(Val);
117 const auto &Name = BB->getName();
118 OS << Name;
119 if (!Name.empty())
120 OS << ":\n";
121 // If there are Instructions in the BB that are not mapped to SandboxIR, then
122 // use a crash-proof dump.
123 if (any_of(*BB, [this](llvm::Instruction &I) {
124 return Ctx.getValue(&I) == nullptr;
125 })) {
126 OS << "<Crash-proof mode!>\n";
128 for (llvm::Instruction &IRef : *BB) {
129 Value *SBV = Ctx.getValue(&IRef);
130 if (SBV == nullptr)
131 OS << IRef << " *** No SandboxIR ***\n";
132 else {
133 auto *SBI = dyn_cast<Instruction>(SBV);
134 if (SBI == nullptr) {
135 OS << IRef << " *** Not a SBInstruction!!! ***\n";
136 } else {
137 if (Visited.insert(SBI).second)
138 OS << *SBI << "\n";
139 }
140 }
141 }
142 } else {
143 for (auto &SBI : *this) {
144 SBI.dumpOS(OS);
145 OS << "\n";
146 }
147 }
148}
149
150void BasicBlock::verify() const {
151 assert(isa<llvm::BasicBlock>(Val) && "Expected BasicBlock!");
152 for (const auto &I : *this) {
153 I.verify();
154 }
155}
156#endif // NDEBUG
157
158} // namespace llvm::sandboxir
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
std::string Name
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
MachineInstr unsigned OpIdx
raw_pwrite_stream & OS
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
iterator end()
Definition: BasicBlock.h:472
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:459
bool empty() const
Definition: BasicBlock.h:481
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:170
This class represents an Operation in the Expression.
Implements a dense probed hash-table based set.
Definition: DenseSet.h:263
LLVM Value Representation.
Definition: Value.h:75
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:322
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:194
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
Iterator for Instructions in a `BasicBlock.
Definition: BasicBlock.h:24
LLVM_ABI BasicBlock * getNodeParent() const
\Returns the parent BB.
Definition: BasicBlock.cpp:43
LLVM_ABI BBIterator & operator++()
Definition: BasicBlock.cpp:16
LLVM_ABI BBIterator & operator--()
Definition: BasicBlock.cpp:29
Contains a list of sandboxir::Instruction's.
Definition: BasicBlock.h:68
LLVM_ABI Function * getParent() const
Definition: BasicBlock.cpp:53
void verify() const final
Should crash if there is something wrong with the instruction.
Definition: BasicBlock.cpp:150
LLVM_ABI Instruction & back() const
Definition: BasicBlock.cpp:106
LLVM_ABI Instruction * getTerminator() const
Definition: BasicBlock.cpp:92
LLVM_ABI Instruction & front() const
Definition: BasicBlock.cpp:98
LLVM_ABI iterator begin() const
Definition: BasicBlock.cpp:78
Context & getContext() const
Definition: BasicBlock.h:99
void dumpOS(raw_ostream &OS) const final
Definition: BasicBlock.cpp:115
LLVM_ABI sandboxir::Value * getValue(llvm::Value *V) const
Definition: Context.cpp:629
Value * getOrCreateValue(llvm::Value *LLVMV)
Get or create a sandboxir::Value for an existing LLVM IR LLVMV.
Definition: Context.h:131
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
Definition: Instruction.h:43
virtual unsigned getNumOfIRInstrs() const =0
This is used by BasicBlock::iterator.
llvm::Value * Val
The LLVM Value that corresponds to this SandboxIR Value.
Definition: Value.h:106
Context & Ctx
All values point to the context.
Definition: Value.h:179
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition: STLExtras.h:2491
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1751
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:428