mini-llvm 0.1.0
Loading...
Searching...
No Matches
Instruction.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2
3#pragma once
4
5#include <concepts>
6#include <cstdlib>
7#include <memory>
8#include <optional>
9#include <string>
10#include <unordered_set>
11#include <utility>
12
16#include "mini-llvm/ir/Value.h"
19
20namespace mini_llvm::ir {
21
23public:
24 BasicBlock *parent() const {
25 return parent_;
26 }
27
29 return *parentIterator_;
30 }
31
32 virtual std::unordered_set<const UseBase *> operands() const = 0;
33 std::unordered_set<UseBase *> operands();
34
35 virtual bool isFoldable() const {
36 return false;
37 }
38
39 virtual std::shared_ptr<Constant> fold() const {
40 abort();
41 }
42
43 virtual void accept(InstructionVisitor &visitor) = 0;
44 virtual void accept(InstructionVisitor &visitor) const = 0;
45
46 bool isWellFormed() const override;
47
48 std::string formatAsOperand() const override {
49 return "%" + formatName();
50 }
51
52private:
53 mutable BasicBlock *parent_ = nullptr;
54 mutable std::optional<BasicBlock::iterator> parentIterator_ = std::nullopt;
55
56 friend class BasicBlock;
57};
58
59MINI_LLVM_EXPORT Instruction &addToParent(const Instruction &before, std::shared_ptr<Instruction> I);
60
61template <typename T>
62 requires std::derived_from<T, Instruction>
63T &addToParent(const Instruction &before, std::shared_ptr<T> I) {
64 return static_cast<T &>(addToParent(before, cast<Instruction>(std::move(I))));
65}
66
68
69} // namespace mini_llvm::ir
#define MINI_LLVM_EXPORT
Definition Compiler.h:17
Definition BasicBlock.h:28
IndirectIterator< InstructionList::iterator, Instruction > iterator
Definition BasicBlock.h:32
Definition InstructionVisitor.h:58
Definition Instruction.h:22
std::string formatAsOperand() const override
Definition Instruction.h:48
virtual std::shared_ptr< Constant > fold() const
Definition Instruction.h:39
BasicBlock::iterator parentIterator() const
Definition Instruction.h:28
virtual bool isFoldable() const
Definition Instruction.h:35
std::unordered_set< UseBase * > operands()
BasicBlock * parent() const
Definition Instruction.h:24
virtual void accept(InstructionVisitor &visitor)=0
bool isWellFormed() const override
virtual void accept(InstructionVisitor &visitor) const =0
virtual std::unordered_set< const UseBase * > operands() const =0
friend class BasicBlock
Definition Instruction.h:56
std::string formatName() const
Definition Argument.h:13
MINI_LLVM_EXPORT void removeFromParent(const BasicBlock &B)
MINI_LLVM_EXPORT Instruction & addToParent(const Instruction &before, std::shared_ptr< Instruction > I)
std::unique_ptr< To > cast(std::unique_ptr< From > from) noexcept
Definition Memory.h:10