summaryrefslogtreecommitdiff
path: root/willow/include/willow/IR/IRBuilder.h
blob: f2f36f2b67dd4b44d2bcc5df24c14add94815e60 (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
#ifndef WILLOW_INCLUDE_IR_IR_BUILDER_H
#define WILLOW_INCLUDE_IR_IR_BUILDER_H

#include <willow/IR/BasicBlock.h>
#include <willow/IR/Context.h>
#include <willow/IR/Function.h>
#include <willow/IR/Instruction.h>
#include <willow/IR/Instructions.h>

#include <cassert>

namespace willow {

/// Helper for constructing and modifiying IR.
class IRBuilder {
  BasicBlock::Iterator insert_point;
  WillowContext &ctx;

public:
  explicit IRBuilder(WillowContext &ctx, BasicBlock &bb,
                     BasicBlock::Iterator insertion_point)
      : insert_point(insertion_point), ctx(ctx) {}

  void SetInsertPoint(BasicBlock::Iterator point) { insert_point = point; }

  AddInst *BuildAdd(Type type, Value *lhs, Value *rhs,
                    std::optional<Location> loc = std::nullopt);
  MulInst *BuildMul(Type type, Value *lhs, Value *rhs,
                    std::optional<Location> loc = std::nullopt);
  SubInst *BuildSub(Type type, Value *lhs, Value *rhs,
                    std::optional<Location> loc = std::nullopt);
  DivInst *BuildDiv(Type type, Value *lhs, Value *rhs,
                    std::optional<Location> loc = std::nullopt);
  ModInst *BuildMod(Type type, Value *lhs, Value *rhs,
                    std::optional<Location> loc = std::nullopt);
  ShlInst *BuildShl(Type type, Value *lhs, Value *rhs,
                    std::optional<Location> loc = std::nullopt);
  ShrInst *BuildShr(Type type, Value *lhs, Value *rhs,
                    std::optional<Location> loc = std::nullopt);
  AshlInst *BuildAshl(Type type, Value *lhs, Value *rhs,
                      std::optional<Location> loc = std::nullopt);
  AshrInst *BuildAshr(Type type, Value *lhs, Value *rhs,
                      std::optional<Location> loc = std::nullopt);

  EqInst *BuildEq(Value *lhs, Value *rhs,
                  std::optional<Location> loc = std::nullopt);
  LtInst *BuildLt(Value *lhs, Value *rhs,
                  std::optional<Location> loc = std::nullopt);
  GtInst *BuildGt(Value *lhs, Value *rhs,
                  std::optional<Location> loc = std::nullopt);
  LeInst *BuildLe(Value *lhs, Value *rhs,
                  std::optional<Location> loc = std::nullopt);
  GeInst *BuildGe(Value *lhs, Value *rhs,
                  std::optional<Location> loc = std::nullopt);

  AndInst *BuildAnd(Value *lhs, Value *rhs,
                    std::optional<Location> loc = std::nullopt);
  OrInst *BuildOr(Value *lhs, Value *rhs,
                  std::optional<Location> loc = std::nullopt);
  NotInst *BuildNot(Value *val, std::optional<Location> loc = std::nullopt);

  JmpInst *BuildJmp(BasicBlock *dst,
                    std::optional<Location> loc = std::nullopt);
  BrInst *BuildBr(Value *predicate, BasicBlock *truedst, BasicBlock *falsedst,
                  std::optional<Location> loc = std::nullopt);
  template <typename... Args>
  CallInst *BuildCall(Type rty, Function *func,
                      std::optional<Location> loc = std::nullopt,
                      Args &&...args) {
    auto *inst = new CallInst(rty, func, std::forward<Args>(args)...);
    assert(inst);
    insert_point->insertAfter(*inst);
    insert_point++;

    return inst;
  }
  RetInst *BuildRet(Value *val, std::optional<Location> loc = std::nullopt);
  RetInst *BuildRet(std::optional<Location> loc = std::nullopt);

  PhiInst *
  BuildPhi(Type ty,
           std::initializer_list<std::pair<BasicBlock *, Value *>> args,
           std::optional<Location> loc = std::nullopt);
  AllocaInst *BuildAlloca(Type ty, std::optional<Location> loc = std::nullopt);
};

} // namespace willow

#endif // WILLOW_INCLUDE_IR_IR_BUILDER_H*