summaryrefslogtreecommitdiff
path: root/willow/include/willow/IR/Instruction.h
blob: a4dfbbbb67c496d3f8488b806c09088a51ac8745 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#ifndef WILLOW_IR_INSTRUCTION_HPP
#define WILLOW_IR_INSTRUCTION_HPP

#include <willow/IR/Value.h>

#include <cassert>
#include <cstddef>
#include <utility>
#include <vector>

namespace willow {

class Value;
class BasicBlock;
class Function;

/// Defines an SSA value or control-flow effect.
class Instruction : public Value {
public:
  enum class Op {
    Add,
    Mul,
    Sub,
    Div,
    Mod,
    Shl,
    Shr,

    Eq,
    Lt,
    Gt,
    Le,
    Ge,

    And,
    Or,
    Not,

    Jmp,
    Br,
    Call,
    Ret,

    Phi,
    Alloca
  };

private:
  BasicBlock *parent = nullptr;
  Op op;

  std::vector<Value *> operands;

protected:
  void setOperand(std::size_t index, Value *operand) {
    assert(index < operands.size() && "Operand index out of bounds");
    assert(operand && "Operand cannot be null");
    Value *old = operands[index];
    if (old == operand)
      return;

    old->delUse(this);
    operands[index] = operand;
    operand->addUse(this);
  }

public:
  /// \param op Opcode for this instruction.
  /// \param type Type of the result of this instruction
  Instruction(Op op, Type type) : Value(ValueKind::Instruction, type), op(op) {}

  /// \param name Name of the ssa value produced by the instruction.
  /// \param op Opcode for the instruction.
  /// \param type Type of the result of the instruction.
  Instruction(std::string name, Type type, Op op)
      : Value(ValueKind::Instruction, std::move(name), type) {}

  ~Instruction() override {
    for (Value *operand : operands)
      operand->delUse(this);
  }

  bool hasParent() const { return parent != nullptr; }
  BasicBlock *getParent() { return parent; }
  const BasicBlock *getParent() const { return parent; }
  void setParent(BasicBlock *block) { parent = block; }

  Op getOp() const { return op; }
  std::size_t getNumOperands() const { return operands.size(); }

  Value *getOperand(std::size_t index) {
    assert(index < operands.size() && "Operand index out of bounds");
    return operands[index];
  }
  const Value *getOperand(std::size_t index) const {
    assert(index < operands.size() && "Operand index out of bounds");
    return operands[index];
  }

  std::vector<Value *> &getOperands() { return operands; }
  const std::vector<Value *> &getOperands() const { return operands; }

  void addOperand(Value *operand) {
    assert(operand && "Operand cannot be null");
    operands.push_back(operand);
    operand->addUse(this);
  }
};

class UnaryInst : public Instruction {
public:
  UnaryInst(std::string name, Type type, Op op, Value *value)
      : Instruction(std::move(name), type, op) {
    addOperand(value);
  }

  Value *getValue() { return getOperand(0); }
  const Value *getValue() const { return getOperand(0); }
};

class BinaryInst : public Instruction {
public:
  BinaryInst(std::string name, Type type, Op op, Value *lhs, Value *rhs)
      : Instruction(std::move(name), type, op) {
    addOperand(lhs);
    addOperand(rhs);
  }

  Value *getLHS() { return getOperand(0); }
  const Value *getLHS() const { return getOperand(0); }

  Value *getRHS() { return getOperand(1); }
  const Value *getRHS() const { return getOperand(1); }
};

class AddInst : public BinaryInst {
public:
  AddInst(std::string name, Type type, Value *lhs, Value *rhs)
      : BinaryInst(std::move(name), type, Op::Add, lhs, rhs) {}
};

class MulInst : public BinaryInst {
public:
  MulInst(std::string name, Type type, Value *lhs, Value *rhs)
      : BinaryInst(std::move(name), type, Op::Mul, lhs, rhs) {}
};

class SubInst : public BinaryInst {
public:
  SubInst(std::string name, Type type, Value *lhs, Value *rhs)
      : BinaryInst(std::move(name), type, Op::Sub, lhs, rhs) {}
};

class DivInst : public BinaryInst {
public:
  DivInst(std::string name, Type type, Value *lhs, Value *rhs)
      : BinaryInst(std::move(name), type, Op::Div, lhs, rhs) {}
};

class ModInst : public BinaryInst {
public:
  ModInst(std::string name, Type type, Value *lhs, Value *rhs)
      : BinaryInst(std::move(name), type, Op::Mod, lhs, rhs) {}
};

class ShlInst : public BinaryInst {
public:
  ShlInst(std::string name, Type type, Value *lhs, Value *rhs)
      : BinaryInst(std::move(name), type, Op::Shl, lhs, rhs) {}
};

class ShrInst : public BinaryInst {
public:
  ShrInst(std::string name, Type type, Value *lhs, Value *rhs)
      : BinaryInst(std::move(name), type, Op::Shr, lhs, rhs) {}
};

class EqInst : public BinaryInst {
public:
  EqInst(std::string name, Type type, Value *lhs, Value *rhs)
      : BinaryInst(std::move(name), type, Op::Eq, lhs, rhs) {}
};

class LtInst : public BinaryInst {
public:
  LtInst(std::string name, Type type, Value *lhs, Value *rhs)
      : BinaryInst(std::move(name), type, Op::Lt, lhs, rhs) {}
};

class GtInst : public BinaryInst {
public:
  GtInst(std::string name, Type type, Value *lhs, Value *rhs)
      : BinaryInst(std::move(name), type, Op::Gt, lhs, rhs) {}
};

class LeInst : public BinaryInst {
public:
  LeInst(std::string name, Type type, Value *lhs, Value *rhs)
      : BinaryInst(std::move(name), type, Op::Le, lhs, rhs) {}
};

class GeInst : public BinaryInst {
public:
  GeInst(std::string name, Type type, Value *lhs, Value *rhs)
      : BinaryInst(std::move(name), type, Op::Ge, lhs, rhs) {}
};

class AndInst : public BinaryInst {
public:
  AndInst(std::string name, Type type, Value *lhs, Value *rhs)
      : BinaryInst(std::move(name), type, Op::And, lhs, rhs) {}
};

class OrInst : public BinaryInst {
public:
  OrInst(std::string name, Type type, Value *lhs, Value *rhs)
      : BinaryInst(std::move(name), type, Op::Or, lhs, rhs) {}
};

class NotInst : public UnaryInst {
public:
  explicit NotInst(std::string name, Type type, Value *value)
      : UnaryInst(std::move(name), type, Op::Not, value) {}
};

class JmpInst : public Instruction {
  BasicBlock *target;

public:
  JmpInst(Type type, BasicBlock *target)
      : Instruction(Op::Jmp, type), target(target) {}

  BasicBlock *getTarget() { return target; }
  const BasicBlock *getTarget() const { return target; }
};

class BrInst : public Instruction {
  BasicBlock *true_target;
  BasicBlock *false_target;

public:
  BrInst(Type type, Value *condition, BasicBlock *true_target,
         BasicBlock *false_target)
      : Instruction(Op::Br, type), true_target(true_target),
        false_target(false_target) {
    addOperand(condition);
  }

  Value *getCondition() { return getOperand(0); }
  const Value *getCondition() const { return getOperand(0); }

  BasicBlock *getTrueTarget() { return true_target; }
  const BasicBlock *getTrueTarget() const { return true_target; }

  BasicBlock *getFalseTarget() { return false_target; }
  const BasicBlock *getFalseTarget() const { return false_target; }
};

} // namespace willow

#endif // WILLOW_IR_INSTRUCTION_HPP