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_SANDBOXIR_SANDBOXIR_H
59#define LLVM_SANDBOXIR_SANDBOXIR_H
60
61#include "llvm/IR/Function.h"
62#include "llvm/IR/IRBuilder.h"
63#include "llvm/IR/User.h"
64#include "llvm/IR/Value.h"
66#include "llvm/SandboxIR/Use.h"
68#include <iterator>
69
70namespace llvm {
71
72namespace sandboxir {
73
74class BasicBlock;
75class Context;
76class Function;
77class Instruction;
78class LoadInst;
79class User;
80class Value;
81
82/// Iterator for the `Use` edges of a User's operands.
83/// \Returns the operand `Use` when dereferenced.
86 /// Don't let the user create a non-empty OperandUseIterator.
87 OperandUseIterator(const class Use &Use) : Use(Use) {}
88 friend class User; // For constructor
89#define DEF_INSTR(ID, OPC, CLASS) friend class CLASS; // For constructor
90#include "llvm/SandboxIR/SandboxIRValues.def"
91
92public:
93 using difference_type = std::ptrdiff_t;
97 using iterator_category = std::input_iterator_tag;
98
99 OperandUseIterator() = default;
100 value_type operator*() const;
103 return Use == Other.Use;
104 }
106 return !(*this == Other);
107 }
108};
109
110/// Iterator for the `Use` edges of a Value's users.
111/// \Returns a `Use` when dereferenced.
114 /// Don't let the user create a non-empty UserUseIterator.
115 UserUseIterator(const class Use &Use) : Use(Use) {}
116 friend class Value; // For constructor
117
118public:
119 using difference_type = std::ptrdiff_t;
123 using iterator_category = std::input_iterator_tag;
124
125 UserUseIterator() = default;
126 value_type operator*() const { return Use; }
128 bool operator==(const UserUseIterator &Other) const {
129 return Use == Other.Use;
130 }
131 bool operator!=(const UserUseIterator &Other) const {
132 return !(*this == Other);
133 }
134};
135
136/// A SandboxIR Value has users. This is the base class.
137class Value {
138public:
139 enum class ClassID : unsigned {
140#define DEF_VALUE(ID, CLASS) ID,
141#define DEF_USER(ID, CLASS) ID,
142#define DEF_INSTR(ID, OPC, CLASS) ID,
143#include "llvm/SandboxIR/SandboxIRValues.def"
144 };
145
146protected:
147 static const char *getSubclassIDStr(ClassID ID) {
148 switch (ID) {
149#define DEF_VALUE(ID, CLASS) \
150 case ClassID::ID: \
151 return #ID;
152#define DEF_USER(ID, CLASS) \
153 case ClassID::ID: \
154 return #ID;
155#define DEF_INSTR(ID, OPC, CLASS) \
156 case ClassID::ID: \
157 return #ID;
158#include "llvm/SandboxIR/SandboxIRValues.def"
159 }
160 llvm_unreachable("Unimplemented ID");
161 }
162
163 /// For isa/dyn_cast.
165#ifndef NDEBUG
166 /// A unique ID used for forming the name (used for debugging).
167 unsigned UID;
168#endif
169 /// The LLVM Value that corresponds to this SandboxIR Value.
170 /// NOTE: Some sandboxir Instructions, like Packs, may include more than one
171 /// value and in these cases `Val` points to the last instruction in program
172 /// order.
173 llvm::Value *Val = nullptr;
174
175 friend class Context; // For getting `Val`.
176 friend class User; // For getting `Val`.
177 friend class Use; // For getting `Val`.
178 friend class LoadInst; // For getting `Val`.
179
180 /// All values point to the context.
182 // This is used by eraseFromParent().
183 void clearValue() { Val = nullptr; }
184 template <typename ItTy, typename SBTy> friend class LLVMOpUserItToSBTy;
185
187
188public:
189 virtual ~Value() = default;
190 ClassID getSubclassID() const { return SubclassID; }
191
194
197 return const_cast<Value *>(this)->use_begin();
198 }
199 use_iterator use_end() { return use_iterator(Use(nullptr, nullptr, Ctx)); }
201 return const_cast<Value *>(this)->use_end();
202 }
203
205 return make_range<use_iterator>(use_begin(), use_end());
206 }
208 return make_range<const_use_iterator>(use_begin(), use_end());
209 }
210
211 /// Helper for mapped_iterator.
212 struct UseToUser {
213 User *operator()(const Use &Use) const { return &*Use.getUser(); }
214 };
215
218
221 return user_iterator(Use(nullptr, nullptr, Ctx), UseToUser());
222 }
224 return const_cast<Value *>(this)->user_begin();
225 }
227 return const_cast<Value *>(this)->user_end();
228 }
229
231 return make_range<user_iterator>(user_begin(), user_end());
232 }
234 return make_range<const_user_iterator>(user_begin(), user_end());
235 }
236 /// \Returns the number of user edges (not necessarily to unique users).
237 /// WARNING: This is a linear-time operation.
238 unsigned getNumUses() const;
239 /// Return true if this value has N uses or more.
240 /// This is logically equivalent to getNumUses() >= N.
241 /// WARNING: This can be expensive, as it is linear to the number of users.
242 bool hasNUsesOrMore(unsigned Num) const {
243 unsigned Cnt = 0;
244 for (auto It = use_begin(), ItE = use_end(); It != ItE; ++It) {
245 if (++Cnt >= Num)
246 return true;
247 }
248 return false;
249 }
250 /// Return true if this Value has exactly N uses.
251 bool hasNUses(unsigned Num) const {
252 unsigned Cnt = 0;
253 for (auto It = use_begin(), ItE = use_end(); It != ItE; ++It) {
254 if (++Cnt > Num)
255 return false;
256 }
257 return Cnt == Num;
258 }
259
260 Type *getType() const { return Val->getType(); }
261
262 Context &getContext() const { return Ctx; }
263
264 void replaceUsesWithIf(Value *OtherV,
265 llvm::function_ref<bool(const Use &)> ShouldReplace);
267
268 /// \Returns the LLVM IR name of the bottom-most LLVM value.
269 StringRef getName() const { return Val->getName(); }
270
271#ifndef NDEBUG
272 /// Should crash if there is something wrong with the instruction.
273 virtual void verify() const = 0;
274 /// Returns the unique id in the form 'SB<number>.' like 'SB1.'
275 std::string getUid() const;
276 virtual void dumpCommonHeader(raw_ostream &OS) const;
277 void dumpCommonFooter(raw_ostream &OS) const;
278 void dumpCommonPrefix(raw_ostream &OS) const;
279 void dumpCommonSuffix(raw_ostream &OS) const;
282 V.dump(OS);
283 return OS;
284 }
285 virtual void dump(raw_ostream &OS) const = 0;
286 LLVM_DUMP_METHOD virtual void dump() const = 0;
287#endif
288};
289
290/// Argument of a sandboxir::Function.
293 : sandboxir::Value(ClassID::Argument, Arg, Ctx) {}
294 friend class Context; // For constructor.
295
296public:
297 static bool classof(const sandboxir::Value *From) {
298 return From->getSubclassID() == ClassID::Argument;
299 }
300#ifndef NDEBUG
301 void verify() const final {
302 assert(isa<llvm::Argument>(Val) && "Expected Argument!");
303 }
305 const sandboxir::Argument &TArg) {
306 TArg.dump(OS);
307 return OS;
308 }
309 void printAsOperand(raw_ostream &OS) const;
310 void dump(raw_ostream &OS) const final;
311 LLVM_DUMP_METHOD void dump() const final;
312#endif
313};
314
315/// A sandboxir::User has operands.
316class User : public Value {
317protected:
319
320 /// \Returns the Use edge that corresponds to \p OpIdx.
321 /// Note: This is the default implementation that works for instructions that
322 /// match the underlying LLVM instruction. All others should use a different
323 /// implementation.
324 Use getOperandUseDefault(unsigned OpIdx, bool Verify) const;
325 /// \Returns the Use for the \p OpIdx'th operand. This is virtual to allow
326 /// instructions to deviate from the LLVM IR operands, which is a requirement
327 /// for sandboxir Instructions that consist of more than one LLVM Instruction.
328 virtual Use getOperandUseInternal(unsigned OpIdx, bool Verify) const = 0;
329 friend class OperandUseIterator; // for getOperandUseInternal()
330
331 /// The default implementation works only for single-LLVMIR-instruction
332 /// Users and only if they match exactly the LLVM instruction.
333 unsigned getUseOperandNoDefault(const Use &Use) const {
334 return Use.LLVMUse->getOperandNo();
335 }
336 /// \Returns the operand index of \p Use.
337 virtual unsigned getUseOperandNo(const Use &Use) const = 0;
338 friend unsigned Use::getOperandNo() const; // For getUseOperandNo()
339
340#ifndef NDEBUG
341 void verifyUserOfLLVMUse(const llvm::Use &Use) const;
342#endif // NDEBUG
343
344public:
345 /// For isa/dyn_cast.
346 static bool classof(const Value *From);
351
353 assert(isa<llvm::User>(Val) && "Expect User value!");
354 return op_iterator(getOperandUseInternal(0, /*Verify=*/false));
355 }
356 virtual op_iterator op_end() {
357 assert(isa<llvm::User>(Val) && "Expect User value!");
358 return op_iterator(
359 getOperandUseInternal(getNumOperands(), /*Verify=*/false));
360 }
361 virtual const_op_iterator op_begin() const {
362 return const_cast<User *>(this)->op_begin();
363 }
364 virtual const_op_iterator op_end() const {
365 return const_cast<User *>(this)->op_end();
366 }
367
368 op_range operands() { return make_range<op_iterator>(op_begin(), op_end()); }
370 return make_range<const_op_iterator>(op_begin(), op_end());
371 }
372 Value *getOperand(unsigned OpIdx) const { return getOperandUse(OpIdx).get(); }
373 /// \Returns the operand edge for \p OpIdx. NOTE: This should also work for
374 /// OpIdx == getNumOperands(), which is used for op_end().
375 Use getOperandUse(unsigned OpIdx) const {
376 return getOperandUseInternal(OpIdx, /*Verify=*/true);
377 }
378 virtual unsigned getNumOperands() const {
379 return isa<llvm::User>(Val) ? cast<llvm::User>(Val)->getNumOperands() : 0;
380 }
381
382 virtual void setOperand(unsigned OperandIdx, Value *Operand);
383 /// Replaces any operands that match \p FromV with \p ToV. Returns whether any
384 /// operands were replaced.
385 bool replaceUsesOfWith(Value *FromV, Value *ToV);
386
387#ifndef NDEBUG
388 void verify() const override {
389 assert(isa<llvm::User>(Val) && "Expected User!");
390 }
391 void dumpCommonHeader(raw_ostream &OS) const final;
392 void dump(raw_ostream &OS) const override {
393 // TODO: Remove this tmp implementation once we get the Instruction classes.
394 }
395 LLVM_DUMP_METHOD void dump() const override {
396 // TODO: Remove this tmp implementation once we get the Instruction classes.
397 }
398#endif
399};
400
401class Constant : public sandboxir::User {
403 : sandboxir::User(ClassID::Constant, C, SBCtx) {}
404 friend class Context; // For constructor.
405 Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
406 return getOperandUseDefault(OpIdx, Verify);
407 }
408
409public:
410 /// For isa/dyn_cast.
411 static bool classof(const sandboxir::Value *From) {
412 return From->getSubclassID() == ClassID::Constant ||
413 From->getSubclassID() == ClassID::Function;
414 }
416 unsigned getUseOperandNo(const Use &Use) const final {
418 }
419#ifndef NDEBUG
420 void verify() const final {
421 assert(isa<llvm::Constant>(Val) && "Expected Constant!");
422 }
424 const sandboxir::Constant &SBC) {
425 SBC.dump(OS);
426 return OS;
427 }
428 void dump(raw_ostream &OS) const override;
429 LLVM_DUMP_METHOD void dump() const override;
430#endif
431};
432
433/// Iterator for `Instruction`s in a `BasicBlock.
434/// \Returns an sandboxir::Instruction & when derereferenced.
436public:
437 using difference_type = std::ptrdiff_t;
441 using iterator_category = std::bidirectional_iterator_tag;
442
443private:
446 Context *Ctx;
447 pointer getInstr(llvm::BasicBlock::iterator It) const;
448
449public:
450 BBIterator() : BB(nullptr), Ctx(nullptr) {}
452 : BB(BB), It(It), Ctx(Ctx) {}
453 reference operator*() const { return *getInstr(It); }
456 auto Copy = *this;
457 ++*this;
458 return Copy;
459 }
462 auto Copy = *this;
463 --*this;
464 return Copy;
465 }
466 bool operator==(const BBIterator &Other) const {
467 assert(Ctx == Other.Ctx && "BBIterators in different context!");
468 return It == Other.It;
469 }
470 bool operator!=(const BBIterator &Other) const { return !(*this == Other); }
471 /// \Returns the SBInstruction that corresponds to this iterator, or null if
472 /// the instruction is not found in the IR-to-SandboxIR tables.
473 pointer get() const { return getInstr(It); }
474};
475
476/// A sandboxir::User with operands, opcode and linked with previous/next
477/// instructions in an instruction list.
479public:
480 enum class Opcode {
481#define DEF_VALUE(ID, CLASS)
482#define DEF_USER(ID, CLASS)
483#define OP(OPC) OPC,
484#define DEF_INSTR(ID, OPC, CLASS) OPC
485#include "llvm/SandboxIR/SandboxIRValues.def"
486 };
487
488protected:
490 sandboxir::Context &SBCtx)
491 : sandboxir::User(ID, I, SBCtx), Opc(Opc) {}
492
494
495 /// A SandboxIR Instruction may map to multiple LLVM IR Instruction. This
496 /// returns its topmost LLVM IR instruction.
498 friend class LoadInst; // For getTopmostLLVMInstruction().
499
500 /// \Returns the LLVM IR Instructions that this SandboxIR maps to in program
501 /// order.
503 friend class EraseFromParent; // For getLLVMInstrs().
504
505public:
506 static const char *getOpcodeName(Opcode Opc);
507#ifndef NDEBUG
509 OS << getOpcodeName(Opc);
510 return OS;
511 }
512#endif
513 /// This is used by BasicBlock::iterator.
514 virtual unsigned getNumOfIRInstrs() const = 0;
515 /// \Returns a BasicBlock::iterator for this Instruction.
516 BBIterator getIterator() const;
517 /// \Returns the next sandboxir::Instruction in the block, or nullptr if at
518 /// the end of the block.
519 Instruction *getNextNode() const;
520 /// \Returns the previous sandboxir::Instruction in the block, or nullptr if
521 /// at the beginning of the block.
522 Instruction *getPrevNode() const;
523 /// \Returns this Instruction's opcode. Note that SandboxIR has its own opcode
524 /// state to allow for new SandboxIR-specific instructions.
525 Opcode getOpcode() const { return Opc; }
526 /// Detach this from its parent BasicBlock without deleting it.
527 void removeFromParent();
528 /// Detach this Value from its parent and delete it.
529 void eraseFromParent();
530 /// Insert this detached instruction before \p BeforeI.
531 void insertBefore(Instruction *BeforeI);
532 /// Insert this detached instruction after \p AfterI.
533 void insertAfter(Instruction *AfterI);
534 /// Insert this detached instruction into \p BB at \p WhereIt.
535 void insertInto(BasicBlock *BB, const BBIterator &WhereIt);
536 /// Move this instruction to \p WhereIt.
537 void moveBefore(BasicBlock &BB, const BBIterator &WhereIt);
538 /// Move this instruction before \p Before.
540 moveBefore(*Before->getParent(), Before->getIterator());
541 }
542 /// Move this instruction after \p After.
544 moveBefore(*After->getParent(), std::next(After->getIterator()));
545 }
546 /// \Returns the BasicBlock containing this Instruction, or null if it is
547 /// detached.
548 BasicBlock *getParent() const;
549 /// For isa/dyn_cast.
550 static bool classof(const sandboxir::Value *From);
551
552#ifndef NDEBUG
554 const sandboxir::Instruction &SBI) {
555 SBI.dump(OS);
556 return OS;
557 }
558 void dump(raw_ostream &OS) const override;
559 LLVM_DUMP_METHOD void dump() const override;
560#endif
561};
562
563class LoadInst final : public Instruction {
564 /// Use LoadInst::create() instead of calling the constructor.
566 : Instruction(ClassID::Load, Opcode::Load, LI, Ctx) {}
567 friend Context; // for LoadInst()
568 Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
569 return getOperandUseDefault(OpIdx, Verify);
570 }
571 SmallVector<llvm::Instruction *, 1> getLLVMInstrs() const final {
572 return {cast<llvm::Instruction>(Val)};
573 }
574
575public:
576 unsigned getUseOperandNo(const Use &Use) const final {
578 }
579
580 unsigned getNumOfIRInstrs() const final { return 1u; }
582 Instruction *InsertBefore, Context &Ctx,
583 const Twine &Name = "");
585 BasicBlock *InsertAtEnd, Context &Ctx,
586 const Twine &Name = "");
587 /// For isa/dyn_cast.
588 static bool classof(const Value *From);
589 Value *getPointerOperand() const;
590 Align getAlign() const { return cast<llvm::LoadInst>(Val)->getAlign(); }
591 bool isUnordered() const { return cast<llvm::LoadInst>(Val)->isUnordered(); }
592 bool isSimple() const { return cast<llvm::LoadInst>(Val)->isSimple(); }
593#ifndef NDEBUG
594 void verify() const final {
595 assert(isa<llvm::LoadInst>(Val) && "Expected LoadInst!");
596 }
597 void dump(raw_ostream &OS) const override;
598 LLVM_DUMP_METHOD void dump() const override;
599#endif
600};
601
602/// An LLLVM Instruction that has no SandboxIR equivalent class gets mapped to
603/// an OpaqueInstr.
606 : sandboxir::Instruction(ClassID::Opaque, Opcode::Opaque, I, Ctx) {}
608 : sandboxir::Instruction(SubclassID, Opcode::Opaque, I, Ctx) {}
609 friend class Context; // For constructor.
610 Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
611 return getOperandUseDefault(OpIdx, Verify);
612 }
613 SmallVector<llvm::Instruction *, 1> getLLVMInstrs() const final {
614 return {cast<llvm::Instruction>(Val)};
615 }
616
617public:
618 static bool classof(const sandboxir::Value *From) {
619 return From->getSubclassID() == ClassID::Opaque;
620 }
621 unsigned getUseOperandNo(const Use &Use) const final {
623 }
624 unsigned getNumOfIRInstrs() const final { return 1u; }
625#ifndef NDEBUG
626 void verify() const final {
627 // Nothing to do
628 }
630 const sandboxir::OpaqueInst &OI) {
631 OI.dump(OS);
632 return OS;
633 }
634 void dump(raw_ostream &OS) const override;
635 LLVM_DUMP_METHOD void dump() const override;
636#endif
637};
638
639/// Contains a list of sandboxir::Instruction's.
640class BasicBlock : public Value {
641 /// Builds a graph that contains all values in \p BB in their original form
642 /// i.e., no vectorization is taking place here.
643 void buildBasicBlockFromLLVMIR(llvm::BasicBlock *LLVMBB);
644 friend class Context; // For `buildBasicBlockFromIR`
645 friend class Instruction; // For LLVM Val.
646
648 : Value(ClassID::Block, BB, SBCtx) {
649 buildBasicBlockFromLLVMIR(BB);
650 }
651
652public:
653 ~BasicBlock() = default;
654 /// For isa/dyn_cast.
655 static bool classof(const Value *From) {
656 return From->getSubclassID() == Value::ClassID::Block;
657 }
658 Function *getParent() const;
660 iterator begin() const;
661 iterator end() const {
662 auto *BB = cast<llvm::BasicBlock>(Val);
663 return iterator(BB, BB->end(), &Ctx);
664 }
665 std::reverse_iterator<iterator> rbegin() const {
666 return std::make_reverse_iterator(end());
667 }
668 std::reverse_iterator<iterator> rend() const {
669 return std::make_reverse_iterator(begin());
670 }
671 Context &getContext() const { return Ctx; }
672 Instruction *getTerminator() const;
673 bool empty() const { return begin() == end(); }
674 Instruction &front() const;
675 Instruction &back() const;
676
677#ifndef NDEBUG
678 void verify() const final {
679 assert(isa<llvm::BasicBlock>(Val) && "Expected BasicBlock!");
680 }
682 SBBB.dump(OS);
683 return OS;
684 }
685 void dump(raw_ostream &OS) const final;
686 LLVM_DUMP_METHOD void dump() const final;
687#endif
688};
689
690class Context {
691protected:
694
695 /// Maps LLVM Value to the corresponding sandboxir::Value. Owns all
696 /// SandboxIR objects.
699
700 /// Remove \p V from the maps and returns the unique_ptr.
701 std::unique_ptr<Value> detachLLVMValue(llvm::Value *V);
702 /// Remove \p SBV from all SandboxIR maps and stop owning it. This effectively
703 /// detaches \p V from the underlying IR.
704 std::unique_ptr<Value> detach(Value *V);
705 friend void Instruction::eraseFromParent(); // For detach().
706 /// Take ownership of VPtr and store it in `LLVMValueToValueMap`.
707 Value *registerValue(std::unique_ptr<Value> &&VPtr);
708 friend class EraseFromParent; // For registerValue().
709 /// This is the actual function that creates sandboxir values for \p V,
710 /// and among others handles all instruction types.
712 /// Get or create a sandboxir::Argument for an existing LLVM IR \p LLVMArg.
714 auto Pair = LLVMValueToValueMap.insert({LLVMArg, nullptr});
715 auto It = Pair.first;
716 if (Pair.second) {
717 It->second = std::unique_ptr<Argument>(new Argument(LLVMArg, *this));
718 return cast<Argument>(It->second.get());
719 }
720 return cast<Argument>(It->second.get());
721 }
722 /// Get or create a sandboxir::Value for an existing LLVM IR \p LLVMV.
724 return getOrCreateValueInternal(LLVMV, 0);
725 }
726 /// Create a sandboxir::BasicBlock for an existing LLVM IR \p BB. This will
727 /// also create all contents of the block.
729
730 friend class BasicBlock; // For getOrCreateValue().
731
733 auto &getLLVMIRBuilder() { return LLVMIRBuilder; }
734
736 friend LoadInst; // For createLoadInst()
737
738public:
740 : LLVMCtx(LLVMCtx), IRTracker(*this),
742
744 /// Convenience function for `getTracker().save()`
745 void save() { IRTracker.save(); }
746 /// Convenience function for `getTracker().revert()`
747 void revert() { IRTracker.revert(); }
748 /// Convenience function for `getTracker().accept()`
749 void accept() { IRTracker.accept(); }
750
752 const sandboxir::Value *getValue(const llvm::Value *V) const {
753 return getValue(const_cast<llvm::Value *>(V));
754 }
755 /// Create a sandboxir::Function for an existing LLVM IR \p F, including all
756 /// blocks and instructions.
757 /// This is the main API function for creating Sandbox IR.
759
760 /// \Returns the number of values registered with Context.
761 size_t getNumValues() const { return LLVMValueToValueMap.size(); }
762};
763
765 /// Helper for mapped_iterator.
766 struct LLVMBBToBB {
767 Context &Ctx;
768 LLVMBBToBB(Context &Ctx) : Ctx(Ctx) {}
769 BasicBlock &operator()(llvm::BasicBlock &LLVMBB) const {
770 return *cast<BasicBlock>(Ctx.getValue(&LLVMBB));
771 }
772 };
773 /// Use Context::createFunction() instead.
775 : sandboxir::Value(ClassID::Function, F, Ctx) {}
776 friend class Context; // For constructor.
777
778public:
779 /// For isa/dyn_cast.
780 static bool classof(const sandboxir::Value *From) {
781 return From->getSubclassID() == ClassID::Function;
782 }
783
784 Argument *getArg(unsigned Idx) const {
785 llvm::Argument *Arg = cast<llvm::Function>(Val)->getArg(Idx);
786 return cast<Argument>(Ctx.getValue(Arg));
787 }
788
789 size_t arg_size() const { return cast<llvm::Function>(Val)->arg_size(); }
790 bool arg_empty() const { return cast<llvm::Function>(Val)->arg_empty(); }
791
793 iterator begin() const {
794 LLVMBBToBB BBGetter(Ctx);
795 return iterator(cast<llvm::Function>(Val)->begin(), BBGetter);
796 }
797 iterator end() const {
798 LLVMBBToBB BBGetter(Ctx);
799 return iterator(cast<llvm::Function>(Val)->end(), BBGetter);
800 }
801
802#ifndef NDEBUG
803 void verify() const final {
804 assert(isa<llvm::Function>(Val) && "Expected Function!");
805 }
806 void dumpNameAndArgs(raw_ostream &OS) const;
807 void dump(raw_ostream &OS) const final;
808 LLVM_DUMP_METHOD void dump() const final;
809#endif
810};
811
812} // namespace sandboxir
813} // namespace llvm
814
815#endif // LLVM_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
std::string Name
#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
ConstantFolder - Create constants with minimum, target independent, folding.
This is an important base class in LLVM.
Definition: Constant.h:42
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2671
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
An instruction for reading from memory.
Definition: Instructions.h:174
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
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
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
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:291
friend raw_ostream & operator<<(raw_ostream &OS, const sandboxir::Argument &TArg)
Definition: SandboxIR.h:304
void dump(raw_ostream &OS) const final
Definition: SandboxIR.cpp:183
void printAsOperand(raw_ostream &OS) const
Definition: SandboxIR.cpp:180
LLVM_DUMP_METHOD void dump() const final
Definition: SandboxIR.cpp:187
void verify() const final
Should crash if there is something wrong with the instruction.
Definition: SandboxIR.h:301
static bool classof(const sandboxir::Value *From)
Definition: SandboxIR.h:297
Iterator for Instructions in a `BasicBlock.
Definition: SandboxIR.h:435
BBIterator operator++(int)
Definition: SandboxIR.h:455
bool operator!=(const BBIterator &Other) const
Definition: SandboxIR.h:470
BBIterator operator--(int)
Definition: SandboxIR.h:461
pointer get() const
\Returns the SBInstruction that corresponds to this iterator, or null if the instruction is not found...
Definition: SandboxIR.h:473
reference operator*() const
Definition: SandboxIR.h:453
std::bidirectional_iterator_tag iterator_category
Definition: SandboxIR.h:441
std::ptrdiff_t difference_type
Definition: SandboxIR.h:437
BBIterator(llvm::BasicBlock *BB, llvm::BasicBlock::iterator It, Context *Ctx)
Definition: SandboxIR.h:451
bool operator==(const BBIterator &Other) const
Definition: SandboxIR.h:466
Contains a list of sandboxir::Instruction's.
Definition: SandboxIR.h:640
std::reverse_iterator< iterator > rbegin() const
Definition: SandboxIR.h:665
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.h:655
void verify() const final
Should crash if there is something wrong with the instruction.
Definition: SandboxIR.h:678
std::reverse_iterator< iterator > rend() const
Definition: SandboxIR.h:668
Function * getParent() const
Definition: SandboxIR.cpp:665
void dump(raw_ostream &OS) const final
Definition: SandboxIR.cpp:733
Instruction & front() const
Definition: SandboxIR.cpp:716
Instruction * getTerminator() const
Definition: SandboxIR.cpp:710
Context & getContext() const
Definition: SandboxIR.h:671
Instruction & back() const
Definition: SandboxIR.cpp:724
iterator begin() const
Definition: SandboxIR.cpp:696
friend raw_ostream & operator<<(raw_ostream &OS, const BasicBlock &SBBB)
Definition: SandboxIR.h:681
iterator end() const
Definition: SandboxIR.h:661
LLVM_DUMP_METHOD void dump() const final
Definition: SandboxIR.cpp:767
sandboxir::Context & getParent() const
Definition: SandboxIR.h:415
friend raw_ostream & operator<<(raw_ostream &OS, const sandboxir::Constant &SBC)
Definition: SandboxIR.h:423
static bool classof(const sandboxir::Value *From)
For isa/dyn_cast.
Definition: SandboxIR.h:411
void verify() const final
Should crash if there is something wrong with the instruction.
Definition: SandboxIR.h:420
unsigned getUseOperandNo(const Use &Use) const final
\Returns the operand index of Use.
Definition: SandboxIR.h:416
LLVM_DUMP_METHOD void dump() const override
Definition: SandboxIR.cpp:515
DenseMap< llvm::Value *, std::unique_ptr< sandboxir::Value > > LLVMValueToValueMap
Maps LLVM Value to the corresponding sandboxir::Value.
Definition: SandboxIR.h:698
Value * registerValue(std::unique_ptr< Value > &&VPtr)
Take ownership of VPtr and store it in LLVMValueToValueMap.
Definition: SandboxIR.cpp:580
sandboxir::Value * getValue(llvm::Value *V) const
Definition: SandboxIR.cpp:645
IRBuilder< ConstantFolder > LLVMIRBuilder
Definition: SandboxIR.h:732
Argument * getOrCreateArgument(llvm::Argument *LLVMArg)
Get or create a sandboxir::Argument for an existing LLVM IR LLVMArg.
Definition: SandboxIR.h:713
Function * createFunction(llvm::Function *F)
Create a sandboxir::Function for an existing LLVM IR F, including all blocks and instructions.
Definition: SandboxIR.cpp:652
Value * getOrCreateValueInternal(llvm::Value *V, llvm::User *U=nullptr)
This is the actual function that creates sandboxir values for V, and among others handles all instruc...
Definition: SandboxIR.cpp:590
std::unique_ptr< Value > detach(Value *V)
Remove SBV from all SandboxIR maps and stop owning it.
Definition: SandboxIR.cpp:573
const sandboxir::Value * getValue(const llvm::Value *V) const
Definition: SandboxIR.h:752
void accept()
Convenience function for getTracker().accept()
Definition: SandboxIR.h:749
BasicBlock * createBasicBlock(llvm::BasicBlock *BB)
Create a sandboxir::BasicBlock for an existing LLVM IR BB.
Definition: SandboxIR.cpp:631
LoadInst * createLoadInst(llvm::LoadInst *LI)
Definition: SandboxIR.cpp:640
Context(LLVMContext &LLVMCtx)
Definition: SandboxIR.h:739
void revert()
Convenience function for getTracker().revert()
Definition: SandboxIR.h:747
std::unique_ptr< Value > detachLLVMValue(llvm::Value *V)
Remove V from the maps and returns the unique_ptr.
Definition: SandboxIR.cpp:562
void save()
Convenience function for getTracker().save()
Definition: SandboxIR.h:745
Value * getOrCreateValue(llvm::Value *LLVMV)
Get or create a sandboxir::Value for an existing LLVM IR LLVMV.
Definition: SandboxIR.h:723
LLVMContext & LLVMCtx
Definition: SandboxIR.h:692
Tracker & getTracker()
Definition: SandboxIR.h:743
size_t getNumValues() const
\Returns the number of values registered with Context.
Definition: SandboxIR.h:761
Argument * getArg(unsigned Idx) const
Definition: SandboxIR.h:784
iterator begin() const
Definition: SandboxIR.h:793
void dumpNameAndArgs(raw_ostream &OS) const
Definition: SandboxIR.cpp:520
LLVM_DUMP_METHOD void dump() const final
Definition: SandboxIR.cpp:551
size_t arg_size() const
Definition: SandboxIR.h:789
bool arg_empty() const
Definition: SandboxIR.h:790
void verify() const final
Should crash if there is something wrong with the instruction.
Definition: SandboxIR.h:803
static bool classof(const sandboxir::Value *From)
For isa/dyn_cast.
Definition: SandboxIR.h:780
mapped_iterator< llvm::Function::iterator, LLVMBBToBB > iterator
Definition: SandboxIR.h:792
iterator end() const
Definition: SandboxIR.h:797
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
Definition: SandboxIR.h:478
BBIterator getIterator() const
\Returns a BasicBlock::iterator for this Instruction.
Definition: SandboxIR.cpp:307
void removeFromParent()
Detach this from its parent BasicBlock without deleting it.
Definition: SandboxIR.cpp:335
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:437
void moveAfter(Instruction *After)
Move this instruction after After.
Definition: SandboxIR.h:543
void insertInto(BasicBlock *BB, const BBIterator &WhereIt)
Insert this detached instruction into BB at WhereIt.
Definition: SandboxIR.cpp:411
Opcode getOpcode() const
\Returns this Instruction's opcode.
Definition: SandboxIR.h:525
static const char * getOpcodeName(Opcode Opc)
Definition: SandboxIR.cpp:282
void dump(raw_ostream &OS) const override
Definition: SandboxIR.cpp:449
void eraseFromParent()
Detach this Value from its parent and delete it.
Definition: SandboxIR.cpp:345
Instruction * getNextNode() const
\Returns the next sandboxir::Instruction in the block, or nullptr if at the end of the block.
Definition: SandboxIR.cpp:312
void moveBefore(BasicBlock &BB, const BBIterator &WhereIt)
Move this instruction to WhereIt.
Definition: SandboxIR.cpp:370
friend raw_ostream & operator<<(raw_ostream &OS, Opcode Opc)
Definition: SandboxIR.h:508
llvm::Instruction * getTopmostLLVMInstruction() const
A SandboxIR Instruction may map to multiple LLVM IR Instruction.
Definition: SandboxIR.cpp:295
LLVM_DUMP_METHOD void dump() const override
Definition: SandboxIR.cpp:452
void insertAfter(Instruction *AfterI)
Insert this detached instruction after AfterI.
Definition: SandboxIR.cpp:407
virtual SmallVector< llvm::Instruction *, 1 > getLLVMInstrs() const =0
\Returns the LLVM IR Instructions that this SandboxIR maps to in program order.
void moveBefore(Instruction *Before)
Move this instruction before Before.
Definition: SandboxIR.h:539
Instruction * getPrevNode() const
\Returns the previous sandboxir::Instruction in the block, or nullptr if at the beginning of the bloc...
Definition: SandboxIR.cpp:327
Instruction(ClassID ID, Opcode Opc, llvm::Instruction *I, sandboxir::Context &SBCtx)
Definition: SandboxIR.h:489
friend raw_ostream & operator<<(raw_ostream &OS, const sandboxir::Instruction &SBI)
Definition: SandboxIR.h:553
void insertBefore(Instruction *BeforeI)
Insert this detached instruction before BeforeI.
Definition: SandboxIR.cpp:396
BasicBlock * getParent() const
\Returns the BasicBlock containing this Instruction, or null if it is detached.
Definition: SandboxIR.cpp:428
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.cpp:481
bool isUnordered() const
Definition: SandboxIR.h:591
void verify() const final
Should crash if there is something wrong with the instruction.
Definition: SandboxIR.h:594
unsigned getUseOperandNo(const Use &Use) const final
\Returns the operand index of Use.
Definition: SandboxIR.h:576
LLVM_DUMP_METHOD void dump() const override
Definition: SandboxIR.cpp:495
unsigned getNumOfIRInstrs() const final
This is used by BasicBlock::iterator.
Definition: SandboxIR.h:580
static LoadInst * create(Type *Ty, Value *Ptr, MaybeAlign Align, Instruction *InsertBefore, Context &Ctx, const Twine &Name="")
Definition: SandboxIR.cpp:458
Align getAlign() const
Definition: SandboxIR.h:590
Value * getPointerOperand() const
Definition: SandboxIR.cpp:485
An LLLVM Instruction that has no SandboxIR equivalent class gets mapped to an OpaqueInstr.
Definition: SandboxIR.h:604
void dump(raw_ostream &OS) const override
Definition: SandboxIR.cpp:500
unsigned getUseOperandNo(const Use &Use) const final
\Returns the operand index of Use.
Definition: SandboxIR.h:621
unsigned getNumOfIRInstrs() const final
This is used by BasicBlock::iterator.
Definition: SandboxIR.h:624
void verify() const final
Should crash if there is something wrong with the instruction.
Definition: SandboxIR.h:626
friend raw_ostream & operator<<(raw_ostream &OS, const sandboxir::OpaqueInst &OI)
Definition: SandboxIR.h:629
LLVM_DUMP_METHOD void dump() const override
Definition: SandboxIR.cpp:505
static bool classof(const sandboxir::Value *From)
Definition: SandboxIR.h:618
Iterator for the Use edges of a User's operands.
Definition: SandboxIR.h:84
std::input_iterator_tag iterator_category
Definition: SandboxIR.h:97
bool operator!=(const OperandUseIterator &Other) const
Definition: SandboxIR.h:105
bool operator==(const OperandUseIterator &Other) const
Definition: SandboxIR.h:102
OperandUseIterator & operator++()
Definition: SandboxIR.cpp:57
The tracker collects all the change objects and implements the main API for saving / reverting / acce...
Definition: Tracker.h:181
void revert()
Stops tracking and reverts to saved state.
Definition: Tracker.cpp:162
void save()
Turns on IR tracking.
Definition: Tracker.cpp:160
void accept()
Stops tracking and accept changes.
Definition: Tracker.cpp:170
Represents a Def-use/Use-def edge in SandboxIR.
Definition: Use.h:29
unsigned getOperandNo() const
Definition: SandboxIR.cpp:21
Value * get() const
Definition: SandboxIR.cpp:17
class User * getUser() const
Definition: Use.h:48
Iterator for the Use edges of a Value's users.
Definition: SandboxIR.h:112
std::input_iterator_tag iterator_category
Definition: SandboxIR.h:123
bool operator!=(const UserUseIterator &Other) const
Definition: SandboxIR.h:131
UserUseIterator & operator++()
Definition: SandboxIR.cpp:64
value_type operator*() const
Definition: SandboxIR.h:126
bool operator==(const UserUseIterator &Other) const
Definition: SandboxIR.h:128
A sandboxir::User has operands.
Definition: SandboxIR.h:316
virtual op_iterator op_begin()
Definition: SandboxIR.h:352
friend class OperandUseIterator
Definition: SandboxIR.h:329
Value * getOperand(unsigned OpIdx) const
Definition: SandboxIR.h:372
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:333
virtual const_op_iterator op_end() const
Definition: SandboxIR.h:364
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.cpp:211
void dump(raw_ostream &OS) const override
Definition: SandboxIR.h:392
bool replaceUsesOfWith(Value *FromV, Value *ToV)
Replaces any operands that match FromV with ToV.
Definition: SandboxIR.cpp:235
void verifyUserOfLLVMUse(const llvm::Use &Use) const
Definition: SandboxIR.cpp:205
op_range operands()
Definition: SandboxIR.h:368
void verify() const override
Should crash if there is something wrong with the instruction.
Definition: SandboxIR.h:388
Use getOperandUseDefault(unsigned OpIdx, bool Verify) const
\Returns the Use edge that corresponds to OpIdx.
Definition: SandboxIR.cpp:193
LLVM_DUMP_METHOD void dump() const override
Definition: SandboxIR.h:395
OperandUseIterator op_iterator
Definition: SandboxIR.h:347
const_op_range operands() const
Definition: SandboxIR.h:369
virtual void setOperand(unsigned OperandIdx, Value *Operand)
Definition: SandboxIR.cpp:226
virtual Use getOperandUseInternal(unsigned OpIdx, bool Verify) const =0
\Returns the Use for the OpIdx'th operand.
virtual unsigned getNumOperands() const
Definition: SandboxIR.h:378
virtual const_op_iterator op_begin() const
Definition: SandboxIR.h:361
Use getOperandUse(unsigned OpIdx) const
\Returns the operand edge for OpIdx.
Definition: SandboxIR.h:375
virtual op_iterator op_end()
Definition: SandboxIR.h:356
User(ClassID ID, llvm::Value *V, Context &Ctx)
Definition: SandboxIR.h:318
void dumpCommonHeader(raw_ostream &OS) const final
Definition: SandboxIR.cpp:249
A SandboxIR Value has users. This is the base class.
Definition: SandboxIR.h:137
mapped_iterator< sandboxir::UserUseIterator, UseToUser > user_iterator
Definition: SandboxIR.h:216
use_iterator use_begin()
Definition: SandboxIR.cpp:87
llvm::Value * Val
The LLVM Value that corresponds to this SandboxIR Value.
Definition: SandboxIR.h:173
virtual void dump(raw_ostream &OS) const =0
std::string getUid() const
Returns the unique id in the form 'SB<number>.' like 'SB1.'.
Definition: SandboxIR.cpp:143
const_use_iterator use_end() const
Definition: SandboxIR.h:200
user_iterator user_begin()
Definition: SandboxIR.cpp:97
void replaceAllUsesWith(Value *Other)
Definition: SandboxIR.cpp:130
ClassID getSubclassID() const
Definition: SandboxIR.h:190
StringRef getName() const
\Returns the LLVM IR name of the bottom-most LLVM value.
Definition: SandboxIR.h:269
virtual void verify() const =0
Should crash if there is something wrong with the instruction.
void dumpCommonFooter(raw_ostream &OS) const
Definition: SandboxIR.cpp:153
virtual void dumpCommonHeader(raw_ostream &OS) const
Definition: SandboxIR.cpp:149
UserUseIterator use_iterator
Definition: SandboxIR.h:192
Context & Ctx
All values point to the context.
Definition: SandboxIR.h:181
ClassID SubclassID
For isa/dyn_cast.
Definition: SandboxIR.h:164
user_iterator user_end()
Definition: SandboxIR.h:220
void dumpCommonSuffix(raw_ostream &OS) const
Definition: SandboxIR.cpp:169
Type * getType() const
Definition: SandboxIR.h:260
friend class Use
Definition: SandboxIR.h:177
friend class LLVMOpUserItToSBTy
Definition: SandboxIR.h:184
virtual ~Value()=default
const_use_iterator use_begin() const
Definition: SandboxIR.h:196
Context & getContext() const
Definition: SandboxIR.h:262
const_user_iterator user_begin() const
Definition: SandboxIR.h:223
bool hasNUsesOrMore(unsigned Num) const
Return true if this value has N uses or more.
Definition: SandboxIR.h:242
iterator_range< const_user_iterator > users() const
Definition: SandboxIR.h:233
iterator_range< user_iterator > users()
Definition: SandboxIR.h:230
virtual LLVM_DUMP_METHOD void dump() const =0
void replaceUsesWithIf(Value *OtherV, llvm::function_ref< bool(const Use &)> ShouldReplace)
Definition: SandboxIR.cpp:110
unsigned getNumUses() const
\Returns the number of user edges (not necessarily to unique users).
Definition: SandboxIR.cpp:108
bool hasNUses(unsigned Num) const
Return true if this Value has exactly N uses.
Definition: SandboxIR.h:251
unsigned UID
A unique ID used for forming the name (used for debugging).
Definition: SandboxIR.h:167
use_iterator use_end()
Definition: SandboxIR.h:199
iterator_range< use_iterator > uses()
Definition: SandboxIR.h:204
const_user_iterator user_end() const
Definition: SandboxIR.h:226
void dumpCommonPrefix(raw_ostream &OS) const
Definition: SandboxIR.cpp:162
void printAsOperandCommon(raw_ostream &OS) const
Definition: SandboxIR.cpp:173
static const char * getSubclassIDStr(ClassID ID)
Definition: SandboxIR.h:147
friend raw_ostream & operator<<(raw_ostream &OS, const sandboxir::Value &V)
Definition: SandboxIR.h:281
iterator_range< const_use_iterator > uses() const
Definition: SandboxIR.h:207
#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.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117
Helper for mapped_iterator.
Definition: SandboxIR.h:212
User * operator()(const Use &Use) const
Definition: SandboxIR.h:213