mini-llvm 0.1.0
Loading...
Searching...
No Matches
GetElementPtr.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2
3#pragma once
4
5#include <cstddef>
6#include <memory>
7#include <ranges>
8#include <string>
9#include <unordered_set>
10#include <vector>
11
14#include "mini-llvm/ir/Type.h"
16#include "mini-llvm/ir/Use.h"
17#include "mini-llvm/ir/Value.h"
20
21namespace mini_llvm::ir {
22
24 using IdxList = std::vector<std::unique_ptr<Use<Value>>>;
25
26public:
29
30 GetElementPtr(std::unique_ptr<Type> sourceType, std::shared_ptr<Value> ptr, std::vector<std::shared_ptr<Value>> indices);
31
32 std::unique_ptr<Type> sourceType() const {
33 return sourceType_->clone();
34 }
35
36 template <typename Self>
37 auto &ptr(this Self &&self) {
38 return self.ptr_;
39 }
40
42 return idx_iterator(indices_.begin());
43 }
44
46 return const_idx_iterator(indices_.begin());
47 }
48
50 return idx_iterator(indices_.end());
51 }
52
54 return const_idx_iterator(indices_.end());
55 }
56
57 bool idx_empty() const {
58 return indices_.empty();
59 }
60
61 size_t idx_size() const {
62 return indices_.size();
63 }
64
65 Use<Value> &idx(size_t i) {
66 return *indices_[i];
67 }
68
69 const Use<Value> &idx(size_t i) const {
70 return *indices_[i];
71 }
72
73 std::unordered_set<const UseBase *> operands() const override;
74
75 void accept(InstructionVisitor &visitor) override {
76 visitor.visitGetElementPtr(*this);
77 }
78
79 void accept(InstructionVisitor &visitor) const override {
80 visitor.visitGetElementPtr(*this);
81 }
82
83 bool isWellFormed() const override;
84
85 std::unique_ptr<Type> type() const override {
86 return std::make_unique<Ptr>();
87 }
88
89 std::string format() const override;
90 std::unique_ptr<Value> clone() const override;
91
92private:
93 std::unique_ptr<Type> sourceType_;
94 Use<Value> ptr_;
95 IdxList indices_;
96};
97
98inline auto indices(GetElementPtr &I) {
99 return std::ranges::subrange(I.idx_begin(), I.idx_end());
100}
101
102inline auto indices(const GetElementPtr &I) {
103 return std::ranges::subrange(I.idx_begin(), I.idx_end());
104}
105
106} // namespace mini_llvm::ir
#define MINI_LLVM_EXPORT
Definition Compiler.h:17
Definition IndirectIterator.h:16
Definition GetElementPtr.h:23
Use< Value > & idx(size_t i)
Definition GetElementPtr.h:65
const Use< Value > & idx(size_t i) const
Definition GetElementPtr.h:69
const_idx_iterator idx_begin() const
Definition GetElementPtr.h:45
IndirectIterator< IdxList::iterator, Use< Value > > idx_iterator
Definition GetElementPtr.h:27
size_t idx_size() const
Definition GetElementPtr.h:61
bool idx_empty() const
Definition GetElementPtr.h:57
auto & ptr(this Self &&self)
Definition GetElementPtr.h:37
std::unordered_set< const UseBase * > operands() const override
idx_iterator idx_begin()
Definition GetElementPtr.h:41
void accept(InstructionVisitor &visitor) override
Definition GetElementPtr.h:75
std::unique_ptr< Value > clone() const override
std::unique_ptr< Type > type() const override
Definition GetElementPtr.h:85
bool isWellFormed() const override
const_idx_iterator idx_end() const
Definition GetElementPtr.h:53
GetElementPtr(std::unique_ptr< Type > sourceType, std::shared_ptr< Value > ptr, std::vector< std::shared_ptr< Value > > indices)
std::unique_ptr< Type > sourceType() const
Definition GetElementPtr.h:32
IndirectIterator< IdxList::const_iterator, const Use< Value > > const_idx_iterator
Definition GetElementPtr.h:28
std::string format() const override
idx_iterator idx_end()
Definition GetElementPtr.h:49
void accept(InstructionVisitor &visitor) const override
Definition GetElementPtr.h:79
Definition InstructionVisitor.h:58
virtual void visitGetElementPtr(GetElementPtr &I)
Definition InstructionVisitor.h:89
Definition Instruction.h:22
Definition Use.h:44
Definition Argument.h:13
auto indices(GetElementPtr &I)
Definition GetElementPtr.h:98