Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:25

0001 //===- BasicBlock.h ---------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_SANDBOXIR_BASICBLOCK_H
0010 #define LLVM_SANDBOXIR_BASICBLOCK_H
0011 
0012 #include "llvm/IR/BasicBlock.h"
0013 #include "llvm/SandboxIR/Value.h"
0014 
0015 namespace llvm::sandboxir {
0016 
0017 class BasicBlock;
0018 class Function;
0019 class Instruction;
0020 
0021 /// Iterator for `Instruction`s in a `BasicBlock.
0022 /// \Returns an sandboxir::Instruction & when derereferenced.
0023 class BBIterator {
0024 public:
0025   using difference_type = std::ptrdiff_t;
0026   using value_type = Instruction;
0027   using pointer = value_type *;
0028   using reference = value_type &;
0029   using iterator_category = std::bidirectional_iterator_tag;
0030 
0031 private:
0032   llvm::BasicBlock *BB;
0033   llvm::BasicBlock::iterator It;
0034   Context *Ctx;
0035   pointer getInstr(llvm::BasicBlock::iterator It) const;
0036 
0037 public:
0038   BBIterator() : BB(nullptr), Ctx(nullptr) {}
0039   BBIterator(llvm::BasicBlock *BB, llvm::BasicBlock::iterator It, Context *Ctx)
0040       : BB(BB), It(It), Ctx(Ctx) {}
0041   reference operator*() const { return *getInstr(It); }
0042   BBIterator &operator++();
0043   BBIterator operator++(int) {
0044     auto Copy = *this;
0045     ++*this;
0046     return Copy;
0047   }
0048   BBIterator &operator--();
0049   BBIterator operator--(int) {
0050     auto Copy = *this;
0051     --*this;
0052     return Copy;
0053   }
0054   bool operator==(const BBIterator &Other) const {
0055     assert(Ctx == Other.Ctx && "BBIterators in different context!");
0056     return It == Other.It;
0057   }
0058   bool operator!=(const BBIterator &Other) const { return !(*this == Other); }
0059   /// \Returns the SBInstruction that corresponds to this iterator, or null if
0060   /// the instruction is not found in the IR-to-SandboxIR tables.
0061   pointer get() const { return getInstr(It); }
0062   /// \Returns the parent BB.
0063   BasicBlock *getNodeParent() const;
0064 };
0065 
0066 /// Contains a list of sandboxir::Instruction's.
0067 class BasicBlock : public Value {
0068   /// Builds a graph that contains all values in \p BB in their original form
0069   /// i.e., no vectorization is taking place here.
0070   void buildBasicBlockFromLLVMIR(llvm::BasicBlock *LLVMBB);
0071   friend class Context;     // For `buildBasicBlockFromIR`
0072   friend class Instruction; // For LLVM Val.
0073 
0074   BasicBlock(llvm::BasicBlock *BB, Context &SBCtx)
0075       : Value(ClassID::Block, BB, SBCtx) {
0076     buildBasicBlockFromLLVMIR(BB);
0077   }
0078 
0079 public:
0080   ~BasicBlock() = default;
0081   /// For isa/dyn_cast.
0082   static bool classof(const Value *From) {
0083     return From->getSubclassID() == Value::ClassID::Block;
0084   }
0085   Function *getParent() const;
0086   using iterator = BBIterator;
0087   iterator begin() const;
0088   iterator end() const {
0089     auto *BB = cast<llvm::BasicBlock>(Val);
0090     return iterator(BB, BB->end(), &Ctx);
0091   }
0092   std::reverse_iterator<iterator> rbegin() const {
0093     return std::make_reverse_iterator(end());
0094   }
0095   std::reverse_iterator<iterator> rend() const {
0096     return std::make_reverse_iterator(begin());
0097   }
0098   Context &getContext() const { return Ctx; }
0099   Instruction *getTerminator() const;
0100   bool empty() const { return begin() == end(); }
0101   Instruction &front() const;
0102   Instruction &back() const;
0103 
0104 #ifndef NDEBUG
0105   void verify() const final;
0106   void dumpOS(raw_ostream &OS) const final;
0107 #endif
0108 };
0109 
0110 } // namespace llvm::sandboxir
0111 
0112 #endif // LLVM_SANDBOXIR_BASICBLOCK_H