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
|
#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(Type type, Value *lhs, Value *rhs,
std::optional<Location> loc = std::nullopt);
OrInst *buildOr(Type type, Value *lhs, Value *rhs,
std::optional<Location> loc = std::nullopt);
NotInst *buildNot(Type type, 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);
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);
};
template <typename... Args>
CallInst *IRBuilder::buildCall(Type rty, Function *func,
std::optional<Location> loc, Args &&...args) {
auto *inst = new CallInst(rty, func, std::forward<Args>(args)...);
assert(inst);
insert_point->insertAfter(*inst);
insert_point++;
return inst;
}
} // namespace willow
#endif // WILLOW_INCLUDE_IR_IR_BUILDER_H*
|