LLVM 19.0.0git
SandboxIR.h
Go to the documentation of this file.
1//===- SandboxIR.h ----------------------------------------------*- C++ -*-===//
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// Sandbox IR is a lightweight overlay transactional IR on top of LLVM IR.
10// Features:
11// - You can save/rollback the state of the IR at any time.
12// - Any changes made to Sandbox IR will automatically update the underlying
13// LLVM IR so both IRs are always in sync.
14// - Feels like LLVM IR, similar API.
15//
16// SandboxIR forms a class hierarchy that resembles that of LLVM IR
17// but is in the `sandboxir` namespace:
18//
19// namespace sandboxir {
20//
21// +- Argument +- BinaryOperator
22// | |
23// Value -+- BasicBlock +- BranchInst
24// | |
25// +- Function +- Constant +- CastInst
26// | | |
27// +- User ------+- Instruction -+- CallInst
28// |
29// +- CmpInst
30// |
31// +- ExtractElementInst
32// |
33// +- GetElementPtrInst
34// |
35// +- InsertElementInst
36// |
37// +- LoadInst
38// |
39// +- OpaqueInst
40// |
41// +- PHINode
42// |
43// +- RetInst
44// |
45// +- SelectInst
46// |
47// +- ShuffleVectorInst
48// |
49// +- StoreInst
50// |
51// +- UnaryOperator
52//
53// Use
54//
55// } // namespace sandboxir
56//
57
58#ifndef LLVM_TRANSFORMS_SANDBOXIR_SANDBOXIR_H
59#define LLVM_TRANSFORMS_SANDBOXIR_SANDBOXIR_H
60
61#include "llvm/IR/Function.h"
62#include "llvm/IR/User.h"
63#include "llvm/IR/Value.h"
65#include <iterator>
66
67namespace llvm {
68
69namespace sandboxir {
70
71class Function;
72class Context;
73class Instruction;
74class User;
75class Value;
76
77/// Represents a Def-use/Use-def edge in SandboxIR.
78/// NOTE: Unlike llvm::Use, this is not an integral part of the use-def chains.
79/// It is also not uniqued and is currently passed by value, so you can have
80/// more than one sandboxir::Use objects for the same use-def edge.
81class Use {
82 llvm::Use *LLVMUse;
83 User *Usr;
84 Context *Ctx;
85
86 /// Don't allow the user to create a sandboxir::Use directly.
87 Use(llvm::Use *LLVMUse, User *Usr, Context &Ctx)
88 : LLVMUse(LLVMUse), Usr(Usr), Ctx(&Ctx) {}
89 Use() : LLVMUse(nullptr), Ctx(nullptr) {}
90
91 friend class Value; // For constructor
92 friend class User; // For constructor
93 friend class OperandUseIterator; // For constructor
94 friend class UserUseIterator; // For accessing members
95
96public:
97 operator Value *() const { return get(); }
98 Value *get() const;
99 class User *getUser() const { return Usr; }
100 unsigned getOperandNo() const;
101 Context *getContext() const { return Ctx; }
102 bool operator==(const Use &Other) const {
103 assert(Ctx == Other.Ctx && "Contexts differ!");
104 return LLVMUse == Other.LLVMUse && Usr == Other.Usr;
105 }
106 bool operator!=(const Use &Other) const { return !(*this == Other); }
107#ifndef NDEBUG
108 void dump(raw_ostream &OS) const;
109 void dump() const;
110#endif // NDEBUG
111};
112
113/// Returns the operand edge when dereferenced.
116 /// Don't let the user create a non-empty OperandUseIterator.
117 OperandUseIterator(const class Use &Use) : Use(Use) {}
118 friend class User; // For constructor
119#define DEF_INSTR(ID, OPC, CLASS) friend class CLASS; // For constructor
120#include "llvm/SandboxIR/SandboxIRValues.def"
121
122public:
123 using difference_type = std::ptrdiff_t;
127 using iterator_category = std::input_iterator_tag;
128
130 value_type operator*() const;
133 return Use == Other.Use;
134 }
136 return !(*this == Other);
137 }
138};
139
140/// Returns user edge when dereferenced.
143 /// Don't let the user create a non-empty UserUseIterator.
144 UserUseIterator(const class Use &Use) : Use(Use) {}
145 friend class Value; // For constructor
146
147public:
148 using difference_type = std::ptrdiff_t;
152 using iterator_category = std::input_iterator_tag;
153
154 UserUseIterator() = default;
155 value_type operator*() const { return Use; }
157 bool operator==(const UserUseIterator &Other) const {
158 return Use == Other.Use;
159 }
160 bool operator!=(const UserUseIterator &Other) const {
161 return !(*this == Other);
162 }
163};
164
165/// A SandboxIR Value has users. This is the base class.
166class Value {
167public:
168 enum class ClassID : unsigned {
169#define DEF_VALUE(ID, CLASS) ID,
170#define DEF_USER(ID, CLASS) ID,
171#define DEF_INSTR(ID, OPC, CLASS) ID,
172#include "llvm/SandboxIR/SandboxIRValues.def"
173 };
174
175protected:
176 static const char *getSubclassIDStr(ClassID ID) {
177 switch (ID) {
178#define DEF_VALUE(ID, CLASS) \
179 case ClassID::ID: \
180 return #ID;
181#define DEF_USER(ID, CLASS) \
182 case ClassID::ID: \
183 return #ID;
184#define DEF_INSTR(ID, OPC, CLASS) \
185 case ClassID::ID: \
186 return #ID;
187#include "llvm/SandboxIR/SandboxIRValues.def"
188 }
189 llvm_unreachable("Unimplemented ID");
190 }
191
192 /// For isa/dyn_cast.
194#ifndef NDEBUG
195 /// A unique ID used for forming the name (used for debugging).
196 unsigned UID;
197#endif
198 /// The LLVM Value that corresponds to this SandboxIR Value.
199 /// NOTE: Some SBInstructions, like Packs, may include more than one value.
200 llvm::Value *Val = nullptr;
201
202 friend class Context; // For getting `Val`.
203 friend class User; // For getting `Val`.
204
205 /// All values point to the context.
207 // This is used by eraseFromParent().
208 void clearValue() { Val = nullptr; }
209 template <typename ItTy, typename SBTy> friend class LLVMOpUserItToSBTy;
210
212
213public:
214 virtual ~Value() = default;
215 ClassID getSubclassID() const { return SubclassID; }
216
219
222 return const_cast<Value *>(this)->use_begin();
223 }
224 use_iterator use_end() { return use_iterator(Use(nullptr, nullptr, Ctx)); }
226 return const_cast<Value *>(this)->use_end();
227 }
228
230 return make_range<use_iterator>(use_begin(), use_end());
231 }
233 return make_range<const_use_iterator>(use_begin(), use_end());
234 }
235
236 /// Helper for mapped_iterator.
237 struct UseToUser {
238 User *operator()(const Use &Use) const { return &*Use.getUser(); }
239 };
240
243
246 return user_iterator(Use(nullptr, nullptr, Ctx), UseToUser());
247 }
249 return const_cast<Value *>(this)->user_begin();
250 }
252 return const_cast<Value *>(this)->user_end();
253 }
254
256 return make_range<user_iterator>(user_begin(), user_end());
257 }
259 return make_range<const_user_iterator>(user_begin(), user_end());
260 }
261 /// \Returns the number of user edges (not necessarily to unique users).
262 /// WARNING: This is a linear-time operation.
263 unsigned getNumUses() const;
264 /// Return true if this value has N uses or more.
265 /// This is logically equivalent to getNumUses() >= N.
266 /// WARNING: This can be expensive, as it is linear to the number of users.
267 bool hasNUsesOrMore(unsigned Num) const {
268 unsigned Cnt = 0;
269 for (auto It = use_begin(), ItE = use_end(); It != ItE; ++It) {
270 if (++Cnt >= Num)
271 return true;
272 }
273 return false;
274 }
275 /// Return true if this Value has exactly N uses.
276 bool hasNUses(unsigned Num) const {
277 unsigned Cnt = 0;
278 for (auto It = use_begin(), ItE = use_end(); It != ItE; ++It) {
279 if (++Cnt > Num)
280 return false;
281 }
282 return Cnt == Num;
283 }
284
285 Type *getType() const { return Val->getType(); }
286
287 Context &getContext() const { return Ctx; }
288
289 void replaceUsesWithIf(Value *OtherV,
290 llvm::function_ref<bool(const Use &)> ShouldReplace);
292
293#ifndef NDEBUG
294 /// Should crash if there is something wrong with the instruction.
295 virtual void verify() const = 0;
296 /// Returns the name in the form 'SB<number>.' like 'SB1.'
297 std::string getName() const;
298 virtual void dumpCommonHeader(raw_ostream &OS) const;
299 void dumpCommonFooter(raw_ostream &OS) const;
300 void dumpCommonPrefix(raw_ostream &OS) const;
301 void dumpCommonSuffix(raw_ostream &OS) const;
304 V.dump(OS);
305 return OS;
306 }
307 virtual void dump(raw_ostream &OS) const = 0;
308 LLVM_DUMP_METHOD virtual void dump() const = 0;
309#endif
310};
311
312/// Argument of a sandboxir::Function.
315 : sandboxir::Value(ClassID::Argument, Arg, Ctx) {}
316 friend class Context; // For constructor.
317
318public:
319 static bool classof(const sandboxir::Value *From) {
320 return From->getSubclassID() == ClassID::Argument;
321 }
322#ifndef NDEBUG
323 void verify() const final {
324 assert(isa<llvm::Argument>(Val) && "Expected Argument!");
325 }
327 const sandboxir::Argument &TArg) {
328 TArg.dump(OS);
329 return OS;
330 }
331 void printAsOperand(raw_ostream &OS) const;
332 void dump(raw_ostream &OS) const final;
333 LLVM_DUMP_METHOD void dump() const final;
334#endif
335};
336
337class User : public Value {
338protected:
340
341 /// \Returns the Use edge that corresponds to \p OpIdx.
342 /// Note: This is the default implementation that works for instructions that
343 /// match the underlying LLVM instruction. All others should use a different
344 /// implementation.
345 Use getOperandUseDefault(unsigned OpIdx, bool Verify) const;
346 virtual Use getOperandUseInternal(unsigned OpIdx, bool Verify) const = 0;
347 friend class OperandUseIterator; // for getOperandUseInternal()
348
349 /// The default implementation works only for single-LLVMIR-instruction
350 /// Users and only if they match exactly the LLVM instruction.
351 unsigned getUseOperandNoDefault(const Use &Use) const {
352 return Use.LLVMUse->getOperandNo();
353 }
354 /// \Returns the operand index of \p Use.
355 virtual unsigned getUseOperandNo(const Use &Use) const = 0;
356 friend unsigned Use::getOperandNo() const; // For getUseOperandNo()
357
358#ifndef NDEBUG
359 void verifyUserOfLLVMUse(const llvm::Use &Use) const;
360#endif // NDEBUG
361
362public:
363 /// For isa/dyn_cast.
364 static bool classof(const Value *From);
369
371 assert(isa<llvm::User>(Val) && "Expect User value!");
372 return op_iterator(getOperandUseInternal(0, /*Verify=*/false));
373 }
374 virtual op_iterator op_end() {
375 assert(isa<llvm::User>(Val) && "Expect User value!");
376 return op_iterator(
377 getOperandUseInternal(getNumOperands(), /*Verify=*/false));
378 }
379 virtual const_op_iterator op_begin() const {
380 return const_cast<User *>(this)->op_begin();
381 }
382 virtual const_op_iterator op_end() const {
383 return const_cast<User *>(this)->op_end();
384 }
385
386 op_range operands() { return make_range<op_iterator>(op_begin(), op_end()); }
388 return make_range<const_op_iterator>(op_begin(), op_end());
389 }
390 Value *getOperand(unsigned OpIdx) const { return getOperandUse(OpIdx).get(); }
391 /// \Returns the operand edge for \p OpIdx. NOTE: This should also work for
392 /// OpIdx == getNumOperands(), which is used for op_end().
393 Use getOperandUse(unsigned OpIdx) const {
394 return getOperandUseInternal(OpIdx, /*Verify=*/true);
395 }
396 virtual unsigned getNumOperands() const {
397 return isa<llvm::User>(Val) ? cast<llvm::User>(Val)->getNumOperands() : 0;
398 }
399
400 virtual void setOperand(unsigned OperandIdx, Value *Operand);
401 /// Replaces any operands that match \p FromV with \p ToV. Returns whether any
402 /// operands were replaced.
403 bool replaceUsesOfWith(Value *FromV, Value *ToV);
404
405#ifndef NDEBUG
406 void verify() const override {
407 assert(isa<llvm::User>(Val) && "Expected User!");
408 }
409 void dumpCommonHeader(raw_ostream &OS) const final;
410 void dump(raw_ostream &OS) const override {
411 // TODO: Remove this tmp implementation once we get the Instruction classes.
412 }
413 LLVM_DUMP_METHOD void dump() const override {
414 // TODO: Remove this tmp implementation once we get the Instruction classes.
415 }
416#endif
417};
418
419class Constant : public sandboxir::User {
421 : sandboxir::User(ClassID::Constant, C, SBCtx) {}
422 friend class Context; // For constructor.
423 Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
424 return getOperandUseDefault(OpIdx, Verify);
425 }
426
427public:
428 /// For isa/dyn_cast.
429 static bool classof(const sandboxir::Value *From) {
430 return From->getSubclassID() == ClassID::Constant ||
431 From->getSubclassID() == ClassID::Function;
432 }
434 unsigned getUseOperandNo(const Use &Use) const final {
436 }
437#ifndef NDEBUG
438 void verify() const final {
439 assert(isa<llvm::Constant>(Val) && "Expected Constant!");
440 }
442 const sandboxir::Constant &SBC) {
443 SBC.dump(OS);
444 return OS;
445 }
446 void dump(raw_ostream &OS) const override;
447 LLVM_DUMP_METHOD void dump() const override;
448#endif
449};
450
451/// The BasicBlock::iterator.
453public:
454 using difference_type = std::ptrdiff_t;
458 using iterator_category = std::bidirectional_iterator_tag;
459
460private:
463 Context *Ctx;
464 pointer getInstr(llvm::BasicBlock::iterator It) const;
465
466public:
467 BBIterator() : BB(nullptr), Ctx(nullptr) {}
469 : BB(BB), It(It), Ctx(Ctx) {}
470 reference operator*() const { return *getInstr(It); }
473 auto Copy = *this;
474 ++*this;
475 return Copy;
476 }
479 auto Copy = *this;
480 --*this;
481 return Copy;
482 }
483 bool operator==(const BBIterator &Other) const {
484 assert(Ctx == Other.Ctx && "BBIterators in different context!");
485 return It == Other.It;
486 }
487 bool operator!=(const BBIterator &Other) const { return !(*this == Other); }
488 /// \Returns the SBInstruction that corresponds to this iterator, or null if
489 /// the instruction is not found in the IR-to-SandboxIR tables.
490 pointer get() const { return getInstr(It); }
491};
492
493/// A sandboxir::User with operands and opcode.
495public:
496 enum class Opcode {
497#define DEF_VALUE(ID, CLASS)
498#define DEF_USER(ID, CLASS)
499#define OP(OPC) OPC,
500#define DEF_INSTR(ID, OPC, CLASS) OPC
501#include "llvm/SandboxIR/SandboxIRValues.def"
502 };
503
504protected:
506 sandboxir::Context &SBCtx)
507 : sandboxir::User(ID, I, SBCtx), Opc(Opc) {}
508
510
511public:
512 static const char *getOpcodeName(Opcode Opc);
513#ifndef NDEBUG
515 OS << getOpcodeName(Opc);
516 return OS;
517 }
518#endif
519 /// This is used by BasicBlock::iterator.
520 virtual unsigned getNumOfIRInstrs() const = 0;
521 /// For isa/dyn_cast.
522 static bool classof(const sandboxir::Value *From);
523
524#ifndef NDEBUG
526 const sandboxir::Instruction &SBI) {
527 SBI.dump(OS);
528 return OS;
529 }
530 void dump(raw_ostream &OS) const override;
531 LLVM_DUMP_METHOD void dump() const override;
532#endif
533};
534
535/// An LLLVM Instruction that has no SandboxIR equivalent class gets mapped to
536/// an OpaqueInstr.
539 : sandboxir::Instruction(ClassID::Opaque, Opcode::Opaque, I, Ctx) {}
541 : sandboxir::Instruction(SubclassID, Opcode::Opaque, I, Ctx) {}
542 friend class Context; // For constructor.
543 Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
544 return getOperandUseDefault(OpIdx, Verify);
545 }
546
547public:
548 static bool classof(const sandboxir::Value *From) {
549 return From->getSubclassID() == ClassID::Opaque;
550 }
551 unsigned getUseOperandNo(const Use &Use) const final {
553 }
554 unsigned getNumOfIRInstrs() const final { return 1u; }
555#ifndef NDEBUG
556 void verify() const final {
557 // Nothing to do
558 }
560 const sandboxir::OpaqueInst &OI) {
561 OI.dump(OS);
562 return OS;
563 }
564 void dump(raw_ostream &OS) const override;
565 LLVM_DUMP_METHOD void dump() const override;
566#endif
567};
568
569class BasicBlock : public Value {
570 /// Builds a graph that contains all values in \p BB in their original form
571 /// i.e., no vectorization is taking place here.
572 void buildBasicBlockFromLLVMIR(llvm::BasicBlock *LLVMBB);
573 friend class Context; // For `buildBasicBlockFromIR`
574
576 : Value(ClassID::Block, BB, SBCtx) {
577 buildBasicBlockFromLLVMIR(BB);
578 }
579
580public:
581 ~BasicBlock() = default;
582 /// For isa/dyn_cast.
583 static bool classof(const Value *From) {
584 return From->getSubclassID() == Value::ClassID::Block;
585 }
586 Function *getParent() const;
588 iterator begin() const;
589 iterator end() const {
590 auto *BB = cast<llvm::BasicBlock>(Val);
591 return iterator(BB, BB->end(), &Ctx);
592 }
593 std::reverse_iterator<iterator> rbegin() const {
594 return std::make_reverse_iterator(end());
595 }
596 std::reverse_iterator<iterator> rend() const {
597 return std::make_reverse_iterator(begin());
598 }
599 Context &getContext() const { return Ctx; }
600 Instruction *getTerminator() const;
601 bool empty() const { return begin() == end(); }
602 Instruction &front() const;
603 Instruction &back() const;
604
605#ifndef NDEBUG
606 void verify() const final {
607 assert(isa<llvm::BasicBlock>(Val) && "Expected BasicBlock!");
608 }
610 SBBB.dump(OS);
611 return OS;
612 }
613 void dump(raw_ostream &OS) const final;
614 LLVM_DUMP_METHOD void dump() const final;
615#endif
616};
617
618class Context {
619protected:
621 /// Maps LLVM Value to the corresponding sandboxir::Value. Owns all
622 /// SandboxIR objects.
625
626 /// Take ownership of VPtr and store it in `LLVMValueToValueMap`.
627 Value *registerValue(std::unique_ptr<Value> &&VPtr);
628
630
632 auto Pair = LLVMValueToValueMap.insert({LLVMArg, nullptr});
633 auto It = Pair.first;
634 if (Pair.second) {
635 It->second = std::unique_ptr<Argument>(new Argument(LLVMArg, *this));
636 return cast<Argument>(It->second.get());
637 }
638 return cast<Argument>(It->second.get());
639 }
640
642 return getOrCreateValueInternal(LLVMV, 0);
643 }
644
646
647 friend class BasicBlock; // For getOrCreateValue().
648
649public:
651
653 const sandboxir::Value *getValue(const llvm::Value *V) const {
654 return getValue(const_cast<llvm::Value *>(V));
655 }
656
658
659 /// \Returns the number of values registered with Context.
660 size_t getNumValues() const { return LLVMValueToValueMap.size(); }
661};
662
664 /// Helper for mapped_iterator.
665 struct LLVMBBToBB {
666 Context &Ctx;
667 LLVMBBToBB(Context &Ctx) : Ctx(Ctx) {}
668 BasicBlock &operator()(llvm::BasicBlock &LLVMBB) const {
669 return *cast<BasicBlock>(Ctx.getValue(&LLVMBB));
670 }
671 };
672 /// Use Context::createFunction() instead.
674 : sandboxir::Value(ClassID::Function, F, Ctx) {}
675 friend class Context; // For constructor.
676
677public:
678 /// For isa/dyn_cast.
679 static bool classof(const sandboxir::Value *From) {
680 return From->getSubclassID() == ClassID::Function;
681 }
682
683 Argument *getArg(unsigned Idx) const {
684 llvm::Argument *Arg = cast<llvm::Function>(Val)->getArg(Idx);
685 return cast<Argument>(Ctx.getValue(Arg));
686 }
687
688 size_t arg_size() const { return cast<llvm::Function>(Val)->arg_size(); }
689 bool arg_empty() const { return cast<llvm::Function>(Val)->arg_empty(); }
690
692 iterator begin() const {
693 LLVMBBToBB BBGetter(Ctx);
694 return iterator(cast<llvm::Function>(Val)->begin(), BBGetter);
695 }
696 iterator end() const {
697 LLVMBBToBB BBGetter(Ctx);
698 return iterator(cast<llvm::Function>(Val)->end(), BBGetter);
699 }
700
701#ifndef NDEBUG
702 void verify() const final {
703 assert(isa<llvm::Function>(Val) && "Expected Function!");
704 }
705 void dumpNameAndArgs(raw_ostream &OS) const;
706 void dump(raw_ostream &OS) const final;
707 LLVM_DUMP_METHOD void dump() const final;
708#endif
709};
710
711} // namespace sandboxir
712} // namespace llvm
713
714#endif // LLVM_TRANSFORMS_SANDBOXIR_SANDBOXIR_H
aarch64 promote const
BlockVerifier::State From
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:537
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
ppc ctr loops PowerPC CTR Loops Verify
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:167
This is an important base class in LLVM.
Definition: Constant.h:42
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
An efficient, type-erasing, non-owning reference to a callable.
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
Argument of a sandboxir::Function.
Definition: SandboxIR.h:313
friend raw_ostream & operator<<(raw_ostream &OS, const sandboxir::Argument &TArg)
Definition: SandboxIR.h:326
void dump(raw_ostream &OS) const final
Definition: SandboxIR.cpp:166
void printAsOperand(raw_ostream &OS) const
Definition: SandboxIR.cpp:163
LLVM_DUMP_METHOD void dump() const final
Definition: SandboxIR.cpp:170
void verify() const final
Should crash if there is something wrong with the instruction.
Definition: SandboxIR.h:323
static bool classof(const sandboxir::Value *From)
Definition: SandboxIR.h:319
The BasicBlock::iterator.
Definition: SandboxIR.h:452
BBIterator operator++(int)
Definition: SandboxIR.h:472
bool operator!=(const BBIterator &Other) const
Definition: SandboxIR.h:487
BBIterator operator--(int)
Definition: SandboxIR.h:478
pointer get() const
\Returns the SBInstruction that corresponds to this iterator, or null if the instruction is not found...
Definition: SandboxIR.h:490
reference operator*() const
Definition: SandboxIR.h:470
std::bidirectional_iterator_tag iterator_category
Definition: SandboxIR.h:458
std::ptrdiff_t difference_type
Definition: SandboxIR.h:454
BBIterator(llvm::BasicBlock *BB, llvm::BasicBlock::iterator It, Context *Ctx)
Definition: SandboxIR.h:468
bool operator==(const BBIterator &Other) const
Definition: SandboxIR.h:483
std::reverse_iterator< iterator > rbegin() const
Definition: SandboxIR.h:593
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.h:583
void verify() const final
Should crash if there is something wrong with the instruction.
Definition: SandboxIR.h:606
std::reverse_iterator< iterator > rend() const
Definition: SandboxIR.h:596
Function * getParent() const
Definition: SandboxIR.cpp:415
void dump(raw_ostream &OS) const final
Definition: SandboxIR.cpp:483
Instruction & front() const
Definition: SandboxIR.cpp:466
Instruction * getTerminator() const
Definition: SandboxIR.cpp:460
Context & getContext() const
Definition: SandboxIR.h:599
Instruction & back() const
Definition: SandboxIR.cpp:474
iterator begin() const
Definition: SandboxIR.cpp:446
friend raw_ostream & operator<<(raw_ostream &OS, const BasicBlock &SBBB)
Definition: SandboxIR.h:609
iterator end() const
Definition: SandboxIR.h:589
LLVM_DUMP_METHOD void dump() const final
Definition: SandboxIR.cpp:517
sandboxir::Context & getParent() const
Definition: SandboxIR.h:433
friend raw_ostream & operator<<(raw_ostream &OS, const sandboxir::Constant &SBC)
Definition: SandboxIR.h:441
static bool classof(const sandboxir::Value *From)
For isa/dyn_cast.
Definition: SandboxIR.h:429
void verify() const final
Should crash if there is something wrong with the instruction.
Definition: SandboxIR.h:438
unsigned getUseOperandNo(const Use &Use) const final
\Returns the operand index of Use.
Definition: SandboxIR.h:434
LLVM_DUMP_METHOD void dump() const override
Definition: SandboxIR.cpp:300
DenseMap< llvm::Value *, std::unique_ptr< sandboxir::Value > > LLVMValueToValueMap
Maps LLVM Value to the corresponding sandboxir::Value.
Definition: SandboxIR.h:624
Value * registerValue(std::unique_ptr< Value > &&VPtr)
Take ownership of VPtr and store it in LLVMValueToValueMap.
Definition: SandboxIR.cpp:347
sandboxir::Value * getValue(llvm::Value *V) const
Definition: SandboxIR.cpp:395
Argument * getOrCreateArgument(llvm::Argument *LLVMArg)
Definition: SandboxIR.h:631
Function * createFunction(llvm::Function *F)
Definition: SandboxIR.cpp:402
Value * getOrCreateValueInternal(llvm::Value *V, llvm::User *U=nullptr)
Definition: SandboxIR.cpp:356
const sandboxir::Value * getValue(const llvm::Value *V) const
Definition: SandboxIR.h:653
BasicBlock * createBasicBlock(llvm::BasicBlock *BB)
Definition: SandboxIR.cpp:386
Context(LLVMContext &LLVMCtx)
Definition: SandboxIR.h:650
Value * getOrCreateValue(llvm::Value *LLVMV)
Definition: SandboxIR.h:641
LLVMContext & LLVMCtx
Definition: SandboxIR.h:620
size_t getNumValues() const
\Returns the number of values registered with Context.
Definition: SandboxIR.h:660
Argument * getArg(unsigned Idx) const
Definition: SandboxIR.h:683
iterator begin() const
Definition: SandboxIR.h:692
void dumpNameAndArgs(raw_ostream &OS) const
Definition: SandboxIR.cpp:305
LLVM_DUMP_METHOD void dump() const final
Definition: SandboxIR.cpp:336
size_t arg_size() const
Definition: SandboxIR.h:688
bool arg_empty() const
Definition: SandboxIR.h:689
void verify() const final
Should crash if there is something wrong with the instruction.
Definition: SandboxIR.h:702
static bool classof(const sandboxir::Value *From)
For isa/dyn_cast.
Definition: SandboxIR.h:679
mapped_iterator< llvm::Function::iterator, LLVMBBToBB > iterator
Definition: SandboxIR.h:691
iterator end() const
Definition: SandboxIR.h:696
A sandboxir::User with operands and opcode.
Definition: SandboxIR.h:494
virtual unsigned getNumOfIRInstrs() const =0
This is used by BasicBlock::iterator.
static bool classof(const sandboxir::Value *From)
For isa/dyn_cast.
Definition: SandboxIR.cpp:265
static const char * getOpcodeName(Opcode Opc)
Definition: SandboxIR.cpp:252
void dump(raw_ostream &OS) const override
Definition: SandboxIR.cpp:277
friend raw_ostream & operator<<(raw_ostream &OS, Opcode Opc)
Definition: SandboxIR.h:514
LLVM_DUMP_METHOD void dump() const override
Definition: SandboxIR.cpp:280
Instruction(ClassID ID, Opcode Opc, llvm::Instruction *I, sandboxir::Context &SBCtx)
Definition: SandboxIR.h:505
friend raw_ostream & operator<<(raw_ostream &OS, const sandboxir::Instruction &SBI)
Definition: SandboxIR.h:525
An LLLVM Instruction that has no SandboxIR equivalent class gets mapped to an OpaqueInstr.
Definition: SandboxIR.h:537
void dump(raw_ostream &OS) const override
Definition: SandboxIR.cpp:285
unsigned getUseOperandNo(const Use &Use) const final
\Returns the operand index of Use.
Definition: SandboxIR.h:551
unsigned getNumOfIRInstrs() const final
This is used by BasicBlock::iterator.
Definition: SandboxIR.h:554
void verify() const final
Should crash if there is something wrong with the instruction.
Definition: SandboxIR.h:556
friend raw_ostream & operator<<(raw_ostream &OS, const sandboxir::OpaqueInst &OI)
Definition: SandboxIR.h:559
LLVM_DUMP_METHOD void dump() const override
Definition: SandboxIR.cpp:290
static bool classof(const sandboxir::Value *From)
Definition: SandboxIR.h:548
Returns the operand edge when dereferenced.
Definition: SandboxIR.h:114
std::input_iterator_tag iterator_category
Definition: SandboxIR.h:127
bool operator!=(const OperandUseIterator &Other) const
Definition: SandboxIR.h:135
bool operator==(const OperandUseIterator &Other) const
Definition: SandboxIR.h:132
OperandUseIterator & operator++()
Definition: SandboxIR.cpp:55
Represents a Def-use/Use-def edge in SandboxIR.
Definition: SandboxIR.h:81
unsigned getOperandNo() const
Definition: SandboxIR.cpp:19
Value * get() const
Definition: SandboxIR.cpp:17
bool operator==(const Use &Other) const
Definition: SandboxIR.h:102
bool operator!=(const Use &Other) const
Definition: SandboxIR.h:106
Context * getContext() const
Definition: SandboxIR.h:101
class User * getUser() const
Definition: SandboxIR.h:99
void dump() const
Definition: SandboxIR.cpp:50
Returns user edge when dereferenced.
Definition: SandboxIR.h:141
std::input_iterator_tag iterator_category
Definition: SandboxIR.h:152
bool operator!=(const UserUseIterator &Other) const
Definition: SandboxIR.h:160
UserUseIterator & operator++()
Definition: SandboxIR.cpp:62
value_type operator*() const
Definition: SandboxIR.h:155
bool operator==(const UserUseIterator &Other) const
Definition: SandboxIR.h:157
virtual op_iterator op_begin()
Definition: SandboxIR.h:370
friend class OperandUseIterator
Definition: SandboxIR.h:347
Value * getOperand(unsigned OpIdx) const
Definition: SandboxIR.h:390
virtual unsigned getUseOperandNo(const Use &Use) const =0
\Returns the operand index of Use.
unsigned getUseOperandNoDefault(const Use &Use) const
The default implementation works only for single-LLVMIR-instruction Users and only if they match exac...
Definition: SandboxIR.h:351
virtual const_op_iterator op_end() const
Definition: SandboxIR.h:382
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.cpp:194
void dump(raw_ostream &OS) const override
Definition: SandboxIR.h:410
bool replaceUsesOfWith(Value *FromV, Value *ToV)
Replaces any operands that match FromV with ToV.
Definition: SandboxIR.cpp:214
void verifyUserOfLLVMUse(const llvm::Use &Use) const
Definition: SandboxIR.cpp:188
op_range operands()
Definition: SandboxIR.h:386
void verify() const override
Should crash if there is something wrong with the instruction.
Definition: SandboxIR.h:406
Use getOperandUseDefault(unsigned OpIdx, bool Verify) const
\Returns the Use edge that corresponds to OpIdx.
Definition: SandboxIR.cpp:176
LLVM_DUMP_METHOD void dump() const override
Definition: SandboxIR.h:413
OperandUseIterator op_iterator
Definition: SandboxIR.h:365
const_op_range operands() const
Definition: SandboxIR.h:387
virtual void setOperand(unsigned OperandIdx, Value *Operand)
Definition: SandboxIR.cpp:209
virtual Use getOperandUseInternal(unsigned OpIdx, bool Verify) const =0
virtual unsigned getNumOperands() const
Definition: SandboxIR.h:396
virtual const_op_iterator op_begin() const
Definition: SandboxIR.h:379
Use getOperandUse(unsigned OpIdx) const
\Returns the operand edge for OpIdx.
Definition: SandboxIR.h:393
virtual op_iterator op_end()
Definition: SandboxIR.h:374
User(ClassID ID, llvm::Value *V, Context &Ctx)
Definition: SandboxIR.h:339
void dumpCommonHeader(raw_ostream &OS) const final
Definition: SandboxIR.cpp:219
A SandboxIR Value has users. This is the base class.
Definition: SandboxIR.h:166
mapped_iterator< sandboxir::UserUseIterator, UseToUser > user_iterator
Definition: SandboxIR.h:241
use_iterator use_begin()
Definition: SandboxIR.cpp:83
llvm::Value * Val
The LLVM Value that corresponds to this SandboxIR Value.
Definition: SandboxIR.h:200
virtual void dump(raw_ostream &OS) const =0
const_use_iterator use_end() const
Definition: SandboxIR.h:225
user_iterator user_begin()
Definition: SandboxIR.cpp:93
void replaceAllUsesWith(Value *Other)
Definition: SandboxIR.cpp:119
ClassID getSubclassID() const
Definition: SandboxIR.h:215
virtual void verify() const =0
Should crash if there is something wrong with the instruction.
void dumpCommonFooter(raw_ostream &OS) const
Definition: SandboxIR.cpp:136
virtual void dumpCommonHeader(raw_ostream &OS) const
Definition: SandboxIR.cpp:132
UserUseIterator use_iterator
Definition: SandboxIR.h:217
Context & Ctx
All values point to the context.
Definition: SandboxIR.h:206
ClassID SubclassID
For isa/dyn_cast.
Definition: SandboxIR.h:193
user_iterator user_end()
Definition: SandboxIR.h:245
void dumpCommonSuffix(raw_ostream &OS) const
Definition: SandboxIR.cpp:152
Type * getType() const
Definition: SandboxIR.h:285
friend class LLVMOpUserItToSBTy
Definition: SandboxIR.h:209
virtual ~Value()=default
const_use_iterator use_begin() const
Definition: SandboxIR.h:221
Context & getContext() const
Definition: SandboxIR.h:287
const_user_iterator user_begin() const
Definition: SandboxIR.h:248
bool hasNUsesOrMore(unsigned Num) const
Return true if this value has N uses or more.
Definition: SandboxIR.h:267
std::string getName() const
Returns the name in the form 'SB<number>.' like 'SB1.'.
Definition: SandboxIR.cpp:126
iterator_range< const_user_iterator > users() const
Definition: SandboxIR.h:258
iterator_range< user_iterator > users()
Definition: SandboxIR.h:255
virtual LLVM_DUMP_METHOD void dump() const =0
void replaceUsesWithIf(Value *OtherV, llvm::function_ref< bool(const Use &)> ShouldReplace)
Definition: SandboxIR.cpp:106
unsigned getNumUses() const
\Returns the number of user edges (not necessarily to unique users).
Definition: SandboxIR.cpp:104
bool hasNUses(unsigned Num) const
Return true if this Value has exactly N uses.
Definition: SandboxIR.h:276
unsigned UID
A unique ID used for forming the name (used for debugging).
Definition: SandboxIR.h:196
use_iterator use_end()
Definition: SandboxIR.h:224
iterator_range< use_iterator > uses()
Definition: SandboxIR.h:229
const_user_iterator user_end() const
Definition: SandboxIR.h:251
void dumpCommonPrefix(raw_ostream &OS) const
Definition: SandboxIR.cpp:145
void printAsOperandCommon(raw_ostream &OS) const
Definition: SandboxIR.cpp:156
static const char * getSubclassIDStr(ClassID ID)
Definition: SandboxIR.h:176
friend raw_ostream & operator<<(raw_ostream &OS, const sandboxir::Value &V)
Definition: SandboxIR.h:303
iterator_range< const_use_iterator > uses() const
Definition: SandboxIR.h:232
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Other
Any other memory.
Helper for mapped_iterator.
Definition: SandboxIR.h:237
User * operator()(const Use &Use) const
Definition: SandboxIR.h:238