summaryrefslogtreecommitdiff
path: root/willow/include/willow/IR/Instruction.h
blob: 0702b20c9fe79d86a2e78df84fd8beff22a2d8d3 (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#ifndef WILLOW_INCLUDE_IR_INSTRUCTION_H
#define WILLOW_INCLUDE_IR_INSTRUCTION_H

#include <willow/IR/Location.h>
#include <willow/IR/Value.h>
#include <willow/Util/LogicalResult.h>

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

namespace willow {

class Value;
class BasicBlock;
class Function;

/// Defines an IR instruction.
class Instruction : public Value {
public:
  enum class Opcode {
    Add,
    Mul,
    Sub,
    Div,
    Mod,
    Shl,
    Shr,
    Ashl,
    Ashr,

    Eq,
    Lt,
    Gt,
    Le,
    Ge,

    And,
    Or,
    Not,

    Jmp,
    Br,
    Call,
    Ret,

    Phi,
    Alloca
  };

private:
  BasicBlock *parent = nullptr;
  Opcode op;

  std::optional<Location> loc;
  std::vector<Value *> operands;
  std::vector<BasicBlock *> successors;

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
  /// \param loc Source location of this instruction.
  Instruction(Opcode op, Type type, std::optional<Location> loc = std::nullopt)
      : Value(ValueKind::Instruction, type), op(op), loc(loc) {}

  /// \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.
  /// \param loc Source location of this instruction.
  Instruction(std::string name, Type type, Opcode op,
              std::optional<Location> loc = std::nullopt)
      : Value(ValueKind::Instruction, std::move(name), type), op(op), loc(loc) {
  }

  ~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; }

  std::optional<Location> getLoc() const { return loc; }

  const std::vector<BasicBlock *> &succs() const { return successors; }
  std::vector<BasicBlock *> &succs() { return successors; }

  void addSuccessor(BasicBlock *successor) { successors.push_back(successor); }

  static bool isTerminatorOp(Opcode op) {
    using enum Opcode;
    switch (op) {
    case Jmp:
    case Br:
    case Call:
    case Ret:
      return true;
    case Add:
    case Mul:
    case Sub:
    case Div:
    case Mod:
    case Shl:
    case Shr:
    case Ashl:
    case Ashr:
    case Eq:
    case Lt:
    case Gt:
    case Le:
    case Ge:
    case And:
    case Or:
    case Not:
    case Phi:
    case Alloca:
      return false;
    }
  }

  bool isTerminator() const { return isTerminatorOp(op); }

  Opcode opcode() 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);
  }
};

/// The base class for unary integer arithemetic instructions
class UnaryInst : public Instruction {
public:
  UnaryInst(std::string name, Type type, Opcode op, Value *value,
            std::optional<Location> loc = std::nullopt)
      : Instruction(std::move(name), type, op, loc) {
    addOperand(value);
  }

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

/// The base class for binary integer arithemetic instructions
class BinaryInst : public Instruction {
public:
  BinaryInst(std::string name, Type type, Opcode op, Value *lhs, Value *rhs,
             std::optional<Location> loc = std::nullopt)
      : Instruction(std::move(name), type, op, loc) {
    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,
          std::optional<Location> loc = std::nullopt)
      : BinaryInst(std::move(name), type, Opcode::Add, lhs, rhs, loc) {}
};

class MulInst : public BinaryInst {
public:
  MulInst(std::string name, Type type, Value *lhs, Value *rhs,
          std::optional<Location> loc = std::nullopt)
      : BinaryInst(std::move(name), type, Opcode::Mul, lhs, rhs, loc) {}
};

class SubInst : public BinaryInst {
public:
  SubInst(std::string name, Type type, Value *lhs, Value *rhs,
          std::optional<Location> loc = std::nullopt)
      : BinaryInst(std::move(name), type, Opcode::Sub, lhs, rhs, loc) {}
};

class DivInst : public BinaryInst {
public:
  DivInst(std::string name, Type type, Value *lhs, Value *rhs,
          std::optional<Location> loc = std::nullopt)
      : BinaryInst(std::move(name), type, Opcode::Div, lhs, rhs, loc) {}
};

class ModInst : public BinaryInst {
public:
  ModInst(std::string name, Type type, Value *lhs, Value *rhs,
          std::optional<Location> loc = std::nullopt)
      : BinaryInst(std::move(name), type, Opcode::Mod, lhs, rhs, loc) {}
};

class ShlInst : public BinaryInst {
public:
  ShlInst(std::string name, Type type, Value *lhs, Value *rhs,
          std::optional<Location> loc = std::nullopt)
      : BinaryInst(std::move(name), type, Opcode::Shl, lhs, rhs, loc) {}
};

class ShrInst : public BinaryInst {
public:
  ShrInst(std::string name, Type type, Value *lhs, Value *rhs,
          std::optional<Location> loc = std::nullopt)
      : BinaryInst(std::move(name), type, Opcode::Shr, lhs, rhs, loc) {}
};

class EqInst : public BinaryInst {
public:
  EqInst(std::string name, Type type, Value *lhs, Value *rhs,
         std::optional<Location> loc = std::nullopt)
      : BinaryInst(std::move(name), type, Opcode::Eq, lhs, rhs, loc) {}
};

class LtInst : public BinaryInst {
public:
  LtInst(std::string name, Type type, Value *lhs, Value *rhs,
         std::optional<Location> loc = std::nullopt)
      : BinaryInst(std::move(name), type, Opcode::Lt, lhs, rhs, loc) {}
};

class GtInst : public BinaryInst {
public:
  GtInst(std::string name, Type type, Value *lhs, Value *rhs,
         std::optional<Location> loc = std::nullopt)
      : BinaryInst(std::move(name), type, Opcode::Gt, lhs, rhs, loc) {}
};

class LeInst : public BinaryInst {
public:
  LeInst(std::string name, Type type, Value *lhs, Value *rhs,
         std::optional<Location> loc = std::nullopt)
      : BinaryInst(std::move(name), type, Opcode::Le, lhs, rhs, loc) {}
};

class GeInst : public BinaryInst {
public:
  GeInst(std::string name, Type type, Value *lhs, Value *rhs,
         std::optional<Location> loc = std::nullopt)
      : BinaryInst(std::move(name), type, Opcode::Ge, lhs, rhs, loc) {}
};

class AndInst : public BinaryInst {
public:
  AndInst(std::string name, Type type, Value *lhs, Value *rhs,
          std::optional<Location> loc = std::nullopt)
      : BinaryInst(std::move(name), type, Opcode::And, lhs, rhs, loc) {}
};

class OrInst : public BinaryInst {
public:
  OrInst(std::string name, Type type, Value *lhs, Value *rhs,
         std::optional<Location> loc = std::nullopt)
      : BinaryInst(std::move(name), type, Opcode::Or, lhs, rhs, loc) {}
};

class NotInst : public UnaryInst {
public:
  explicit NotInst(std::string name, Type type, Value *value,
                   std::optional<Location> loc = std::nullopt)
      : UnaryInst(std::move(name), type, Opcode::Not, value, loc) {}
};

class JmpInst : public Instruction {
public:
  JmpInst(Type type, BasicBlock *target,
          std::optional<Location> loc = std::nullopt)
      : Instruction(Opcode::Jmp, type, loc) {
    addSuccessor(target);
  }

  BasicBlock *getTarget() { return succs()[0]; }
  const BasicBlock *getTarget() const { return succs()[0]; }
};

class BrInst : public Instruction {
public:
  BrInst(Type type, Value *condition, BasicBlock *true_target,
         BasicBlock *false_target, std::optional<Location> loc = std::nullopt)
      : Instruction(Opcode::Br, type, loc) {
    addOperand(condition);
    addSuccessor(true_target);
    addSuccessor(false_target);
  }

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

  BasicBlock *getTrueTarget() { return succs()[0]; }
  const BasicBlock *getTrueTarget() const { return succs()[0]; }

  BasicBlock *getFalseTarget() { return succs()[1]; }
  const BasicBlock *getFalseTarget() const { return succs()[1]; }
};

} // namespace willow

#endif // WILLOW_INCLUDE_IR_INSTRUCTION_H