1 //===- BasicBlock.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 #ifndef LLVM_SANDBOXIR_BASICBLOCK_H 10 #define LLVM_SANDBOXIR_BASICBLOCK_H 11 12 #include "llvm/IR/BasicBlock.h" 13 #include "llvm/SandboxIR/Value.h" 14 #include "llvm/Support/Compiler.h" 15 16 namespace llvm::sandboxir { 17 18 class BasicBlock; 19 class Function; 20 class Instruction; 21 22 /// Iterator for `Instruction`s in a `BasicBlock. 23 /// \Returns an sandboxir::Instruction & when derereferenced. 24 class BBIterator { 25 public: 26 using difference_type = std::ptrdiff_t; 27 using value_type = Instruction; 28 using pointer = value_type *; 29 using reference = value_type &; 30 using iterator_category = std::bidirectional_iterator_tag; 31 32 private: 33 llvm::BasicBlock *BB; 34 llvm::BasicBlock::iterator It; 35 Context *Ctx; 36 LLVM_ABI pointer getInstr(llvm::BasicBlock::iterator It) const; 37 38 public: BBIterator()39 BBIterator() : BB(nullptr), Ctx(nullptr) {} BBIterator(llvm::BasicBlock * BB,llvm::BasicBlock::iterator It,Context * Ctx)40 BBIterator(llvm::BasicBlock *BB, llvm::BasicBlock::iterator It, Context *Ctx) 41 : BB(BB), It(It), Ctx(Ctx) {} 42 reference operator*() const { return *getInstr(It); } 43 LLVM_ABI BBIterator &operator++(); 44 BBIterator operator++(int) { 45 auto Copy = *this; 46 ++*this; 47 return Copy; 48 } 49 LLVM_ABI BBIterator &operator--(); 50 BBIterator operator--(int) { 51 auto Copy = *this; 52 --*this; 53 return Copy; 54 } 55 bool operator==(const BBIterator &Other) const { 56 assert(Ctx == Other.Ctx && "BBIterators in different context!"); 57 return It == Other.It; 58 } 59 bool operator!=(const BBIterator &Other) const { return !(*this == Other); } 60 /// \Returns the SBInstruction that corresponds to this iterator, or null if 61 /// the instruction is not found in the IR-to-SandboxIR tables. get()62 pointer get() const { return getInstr(It); } 63 /// \Returns the parent BB. 64 LLVM_ABI BasicBlock *getNodeParent() const; 65 }; 66 67 /// Contains a list of sandboxir::Instruction's. 68 class BasicBlock : public Value { 69 /// Builds a graph that contains all values in \p BB in their original form 70 /// i.e., no vectorization is taking place here. 71 LLVM_ABI void buildBasicBlockFromLLVMIR(llvm::BasicBlock *LLVMBB); 72 friend class Context; // For `buildBasicBlockFromIR` 73 friend class Instruction; // For LLVM Val. 74 BasicBlock(llvm::BasicBlock * BB,Context & SBCtx)75 BasicBlock(llvm::BasicBlock *BB, Context &SBCtx) 76 : Value(ClassID::Block, BB, SBCtx) { 77 buildBasicBlockFromLLVMIR(BB); 78 } 79 80 public: 81 ~BasicBlock() = default; 82 /// For isa/dyn_cast. classof(const Value * From)83 static bool classof(const Value *From) { 84 return From->getSubclassID() == Value::ClassID::Block; 85 } 86 LLVM_ABI Function *getParent() const; 87 using iterator = BBIterator; 88 LLVM_ABI iterator begin() const; end()89 iterator end() const { 90 auto *BB = cast<llvm::BasicBlock>(Val); 91 return iterator(BB, BB->end(), &Ctx); 92 } rbegin()93 std::reverse_iterator<iterator> rbegin() const { 94 return std::make_reverse_iterator(end()); 95 } rend()96 std::reverse_iterator<iterator> rend() const { 97 return std::make_reverse_iterator(begin()); 98 } getContext()99 Context &getContext() const { return Ctx; } 100 LLVM_ABI Instruction *getTerminator() const; empty()101 bool empty() const { return begin() == end(); } 102 LLVM_ABI Instruction &front() const; 103 LLVM_ABI Instruction &back() const; 104 105 #ifndef NDEBUG 106 void verify() const final; 107 void dumpOS(raw_ostream &OS) const final; 108 #endif 109 }; 110 111 } // namespace llvm::sandboxir 112 113 #endif // LLVM_SANDBOXIR_BASICBLOCK_H 114