mini-llvm 0.1.0
Loading...
Searching...
No Matches
Cmp.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2
3#pragma once
4
5#include <format>
6#include <memory>
7#include <string>
8#include <unordered_set>
9#include <utility>
10
20
21namespace mini_llvm::mir {
22
24public:
25 Cmp(int width, std::shared_ptr<Register> src1, std::shared_ptr<Register> src2)
26 : width_(width),
27 src1_(RegisterClass::kGPR, std::move(src1)),
28 src2_(RegisterClass::kGPR, std::move(src2)) {}
29
30 int width() const {
31 return width_;
32 }
33
34 template <typename Self>
35 auto &src1(this Self &&self) {
36 return self.src1_;
37 }
38
39 template <typename Self>
40 auto &src2(this Self &&self) {
41 return self.src2_;
42 }
43
44 std::unordered_set<const RegisterOperand *> regOps() const override {
45 return {&src1(), &src2()};
46 }
47
48 std::unordered_set<const RegisterOperand *> dsts() const override {
49 return {};
50 }
51
52 std::unordered_set<const RegisterOperand *> srcs() const override {
53 return {&src1(), &src2()};
54 }
55
56 std::unordered_set<const ImmediateOperand *> immOps() const override {
57 return {};
58 }
59
60 std::unordered_set<const MemoryOperand *> memOps() const override {
61 return {};
62 }
63
64 bool hasSideEffects() const override {
65 return false;
66 }
67
68 std::string format() const override {
69 return std::format("CMP i{} {}, {}", width() * 8, *src1(), *src2());
70 }
71
72 std::unique_ptr<Instruction> clone() const override {
73 return std::make_unique<Cmp>(width(), share(*src1()), share(*src2()));
74 }
75
76 void accept(InstructionVisitor &visitor) override {
77 visitor.visitCmp(*this);
78 }
79
80 void accept(InstructionVisitor &visitor) const override {
81 visitor.visitCmp(*this);
82 }
83
84private:
85 int width_;
86 RegisterOperand src1_, src2_;
87};
88
89} // namespace mini_llvm::mir
#define MINI_LLVM_EXPORT
Definition Compiler.h:17
std::unique_ptr< Instruction > clone() const override
Definition Cmp.h:72
auto & src2(this Self &&self)
Definition Cmp.h:40
auto & src1(this Self &&self)
Definition Cmp.h:35
std::unordered_set< const RegisterOperand * > dsts() const override
Definition Cmp.h:48
std::unordered_set< const MemoryOperand * > memOps() const override
Definition Cmp.h:60
std::unordered_set< const RegisterOperand * > srcs() const override
Definition Cmp.h:52
int width() const
Definition Cmp.h:30
std::unordered_set< const RegisterOperand * > regOps() const override
Definition Cmp.h:44
bool hasSideEffects() const override
Definition Cmp.h:64
void accept(InstructionVisitor &visitor) const override
Definition Cmp.h:80
std::unordered_set< const ImmediateOperand * > immOps() const override
Definition Cmp.h:56
void accept(InstructionVisitor &visitor) override
Definition Cmp.h:76
std::string format() const override
Definition Cmp.h:68
Cmp(int width, std::shared_ptr< Register > src1, std::shared_ptr< Register > src2)
Definition Cmp.h:25
Definition InstructionVisitor.h:70
virtual void visitCmp(Cmp &I)
Definition InstructionVisitor.h:88
Definition RegisterOperand.h:13
Definition BasicBlock.h:22
RegisterClass
Definition RegisterClass.h:7
@ kGPR
Definition RegisterClass.h:8
std::shared_ptr< T > share(T &value)
Definition Memory.h:25