Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.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
9#include "llvm/SandboxIR/BasicBlock.h"
10#include "llvm/SandboxIR/Context.h"
11#include "llvm/SandboxIR/Function.h"
12#include "llvm/SandboxIR/Instruction.h"
13
14namespacellvm::sandboxir {
15
16BBIterator &BBIterator::operator++() {
17auto ItE = BB->end();
18assert(It != ItE &&"Already at end!");
19 ++It;
20if (It == ItE)
21return *this;
22Instruction &NextI = *cast<sandboxir::Instruction>(Ctx->getValue(&*It));
23unsigned Num = NextI.getNumOfIRInstrs();
24assert(Num > 0 &&"Bad getNumOfIRInstrs()");
25 It = std::next(It, Num - 1);
26return *this;
27}
28
29BBIterator &BBIterator::operator--() {
30assert(It != BB->begin() &&"Already at begin!");
31if (It == BB->end()) {
32 --It;
33return *this;
34 }
35Instruction &CurrI = **this;
36unsigned Num = CurrI.getNumOfIRInstrs();
37assert(Num > 0 &&"Bad getNumOfIRInstrs()");
38assert(std::prev(It, Num - 1) != BB->begin() &&"Already at begin!");
39 It = std::prev(It, Num);
40return *this;
41}
42
43BasicBlock *BBIterator::getNodeParent() const{
44llvm::BasicBlock *Parent =const_cast<BBIterator *>(this)->It.getNodeParent();
45return cast<BasicBlock>(Ctx->getValue(Parent));
46}
47
48BasicBlock::iterator::pointer
49BasicBlock::iterator::getInstr(llvm::BasicBlock::iterator It) const{
50return cast_or_null<Instruction>(Ctx->getValue(&*It));
51}
52
53Function *BasicBlock::getParent() const{
54auto *BB = cast<llvm::BasicBlock>(Val);
55auto *F = BB->getParent();
56if (F ==nullptr)
57// Detached
58returnnullptr;
59return cast_or_null<Function>(Ctx.getValue(F));
60}
61
62void BasicBlock::buildBasicBlockFromLLVMIR(llvm::BasicBlock *LLVMBB) {
63for (llvm::Instruction &IRef :reverse(*LLVMBB)) {
64llvm::Instruction *I = &IRef;
65Ctx.getOrCreateValue(I);
66for (auto [OpIdx,Op] :enumerate(I->operands())) {
67// Skip instruction's label operands
68if (isa<llvm::BasicBlock>(Op))
69continue;
70// Skip metadata
71if (isa<llvm::MetadataAsValue>(Op))
72continue;
73// Skip asm
74if (isa<llvm::InlineAsm>(Op))
75continue;
76Ctx.getOrCreateValue(Op);
77 }
78 }
79#if !defined(NDEBUG)
80verify();
81#endif
82}
83
84BasicBlock::iteratorBasicBlock::begin() const{
85llvm::BasicBlock *BB = cast<llvm::BasicBlock>(Val);
86llvm::BasicBlock::iterator It = BB->begin();
87if (!BB->empty()) {
88auto *V =Ctx.getValue(&*BB->begin());
89assert(V !=nullptr &&"No SandboxIR for BB->begin()!");
90auto *I = cast<Instruction>(V);
91unsigned Num =I->getNumOfIRInstrs();
92assert(Num >= 1u &&"Bad getNumOfIRInstrs()");
93 It = std::next(It, Num - 1);
94 }
95returniterator(BB, It, &Ctx);
96}
97
98Instruction *BasicBlock::getTerminator() const{
99auto *TerminatorV =
100Ctx.getValue(cast<llvm::BasicBlock>(Val)->getTerminator());
101return cast_or_null<Instruction>(TerminatorV);
102}
103
104Instruction &BasicBlock::front() const{
105auto *BB = cast<llvm::BasicBlock>(Val);
106assert(!BB->empty() &&"Empty block!");
107auto *SBI = cast<Instruction>(getContext().getValue(&*BB->begin()));
108assert(SBI !=nullptr &&"Expected Instr!");
109return *SBI;
110}
111
112Instruction &BasicBlock::back() const{
113auto *BB = cast<llvm::BasicBlock>(Val);
114assert(!BB->empty() &&"Empty block!");
115auto *SBI = cast<Instruction>(getContext().getValue(&*BB->rbegin()));
116assert(SBI !=nullptr &&"Expected Instr!");
117return *SBI;
118}
119
120#ifndef NDEBUG
121voidBasicBlock::dumpOS(raw_ostream &OS) const{
122llvm::BasicBlock *BB = cast<llvm::BasicBlock>(Val);
123constauto &Name = BB->getName();
124OS <<Name;
125if (!Name.empty())
126OS <<":\n";
127// If there are Instructions in the BB that are not mapped to SandboxIR, then
128// use a crash-proof dump.
129if (any_of(*BB, [this](llvm::Instruction &I) {
130returnCtx.getValue(&I) ==nullptr;
131 })) {
132OS <<"<Crash-proof mode!>\n";
133DenseSet<Instruction *> Visited;
134for (llvm::Instruction &IRef : *BB) {
135Value *SBV =Ctx.getValue(&IRef);
136if (SBV ==nullptr)
137OS << IRef <<" *** No SandboxIR ***\n";
138else {
139auto *SBI = dyn_cast<Instruction>(SBV);
140if (SBI ==nullptr) {
141OS << IRef <<" *** Not a SBInstruction!!! ***\n";
142 }else {
143if (Visited.insert(SBI).second)
144OS << *SBI <<"\n";
145 }
146 }
147 }
148 }else {
149for (auto &SBI : *this) {
150 SBI.dumpOS(OS);
151OS <<"\n";
152 }
153 }
154}
155
156voidBasicBlock::verify() const{
157assert(isa<llvm::BasicBlock>(Val) &&"Expected BasicBlock!");
158for (constauto &I : *this) {
159I.verify();
160 }
161}
162#endif// NDEBUG
163
164}// namespace llvm::sandboxir
Name
std::string Name
Definition:ELFObjHandler.cpp:77
F
#define F(x, y, z)
Definition:MD5.cpp:55
I
#define I(x, y, z)
Definition:MD5.cpp:58
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
OS
raw_pwrite_stream & OS
Definition:SampleProfWriter.cpp:51
BasicBlock.h
Context.h
Function.h
Instruction.h
llvm::BasicBlock
LLVM Basic Block Representation.
Definition:BasicBlock.h:61
llvm::BasicBlock::end
iterator end()
Definition:BasicBlock.h:464
llvm::BasicBlock::begin
iterator begin()
Instruction iterator methods.
Definition:BasicBlock.h:451
llvm::BasicBlock::empty
bool empty() const
Definition:BasicBlock.h:473
llvm::BasicBlock::iterator
InstListType::iterator iterator
Instruction iterators...
Definition:BasicBlock.h:177
llvm::DWARFExpression::Operation
This class represents an Operation in the Expression.
Definition:DWARFExpression.h:32
llvm::DenseSet
Implements a dense probed hash-table based set.
Definition:DenseSet.h:278
llvm::Instruction
Definition:Instruction.h:68
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition:Value.cpp:309
llvm::detail::DenseSetImpl::insert
std::pair< iterator, bool > insert(const ValueT &V)
Definition:DenseSet.h:213
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition:raw_ostream.h:52
llvm::sandboxir::BBIterator
Iterator for Instructions in a `BasicBlock.
Definition:BasicBlock.h:23
llvm::sandboxir::BBIterator::pointer
value_type * pointer
Definition:BasicBlock.h:27
llvm::sandboxir::BBIterator::getNodeParent
BasicBlock * getNodeParent() const
\Returns the parent BB.
Definition:BasicBlock.cpp:43
llvm::sandboxir::BBIterator::operator++
BBIterator & operator++()
Definition:BasicBlock.cpp:16
llvm::sandboxir::BBIterator::operator--
BBIterator & operator--()
Definition:BasicBlock.cpp:29
llvm::sandboxir::BasicBlock
Contains a list of sandboxir::Instruction's.
Definition:BasicBlock.h:67
llvm::sandboxir::BasicBlock::iterator
BBIterator iterator
Definition:BasicBlock.h:86
llvm::sandboxir::BasicBlock::getParent
Function * getParent() const
Definition:BasicBlock.cpp:53
llvm::sandboxir::BasicBlock::verify
void verify() const final
Should crash if there is something wrong with the instruction.
Definition:BasicBlock.cpp:156
llvm::sandboxir::BasicBlock::back
Instruction & back() const
Definition:BasicBlock.cpp:112
llvm::sandboxir::BasicBlock::getTerminator
Instruction * getTerminator() const
Definition:BasicBlock.cpp:98
llvm::sandboxir::BasicBlock::front
Instruction & front() const
Definition:BasicBlock.cpp:104
llvm::sandboxir::BasicBlock::begin
iterator begin() const
Definition:BasicBlock.cpp:84
llvm::sandboxir::BasicBlock::getContext
Context & getContext() const
Definition:BasicBlock.h:98
llvm::sandboxir::BasicBlock::dumpOS
void dumpOS(raw_ostream &OS) const final
Definition:BasicBlock.cpp:121
llvm::sandboxir::Context::getValue
sandboxir::Value * getValue(llvm::Value *V) const
Definition:Context.cpp:601
llvm::sandboxir::Context::getOrCreateValue
Value * getOrCreateValue(llvm::Value *LLVMV)
Get or create a sandboxir::Value for an existing LLVM IR LLVMV.
Definition:Context.h:122
llvm::sandboxir::Function
Definition:Function.h:18
llvm::sandboxir::Instruction
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
Definition:Instruction.h:42
llvm::sandboxir::Instruction::getNumOfIRInstrs
virtual unsigned getNumOfIRInstrs() const =0
This is used by BasicBlock::iterator.
llvm::sandboxir::Value
A SandboxIR Value has users. This is the base class.
Definition:Value.h:63
llvm::sandboxir::Value::Val
llvm::Value * Val
The LLVM Value that corresponds to this SandboxIR Value.
Definition:Value.h:103
llvm::sandboxir::Value::Ctx
Context & Ctx
All values point to the context.
Definition:Value.h:173
llvm::sandboxir
Definition:Argument.h:15
llvm::enumerate
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:2448
llvm::any_of
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:1746
llvm::reverse
auto reverse(ContainerTy &&C)
Definition:STLExtras.h:420

Generated on Thu Jul 17 2025 12:26:52 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp