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
|
#ifndef WILLOW_INCLUDE_IR_VALUE_H
#define WILLOW_INCLUDE_IR_VALUE_H
#include <willow/IR/Types.h>
#include <cassert>
#include <string>
#include <string_view>
#include <unordered_map>
namespace willow {
class Instruction;
class BasicBlock;
enum class ValueKind {
Instruction, ///< the identified result of an instruction
Parameter, ///< the named parameter to a function
Constant, ///< an anonymous typed constant
BasicBlock, ///< a basic block
Function, ///< a function signature
};
/// An SSA value that may be used.
class Value {
ValueKind valuekind;
std::string name;
Type type;
// Instructions that use this value
std::unordered_map<Instruction *, std::size_t> uses;
public:
virtual ~Value() = default;
Value(ValueKind valuekind, std::string name, Type type)
: valuekind(valuekind), name(std::move(name)), type(type) {}
Value(ValueKind valuekind, Type type) : valuekind(valuekind), type(type) {}
bool hasName() const { return !name.empty(); }
std::string_view getName() const { return name; }
void setName(std::string name) { this->name = std::move(name); }
Type getType() const { return type; }
ValueKind getValueKind() const { return valuekind; }
bool isVoid() const { return type.isVoid(); }
bool isBasicBlock() const { return type.isBasicBlock(); }
/// Get the instructions that use this value, and the number of times they use
/// it.
///
/// \return map of Instruction* -> num uses
std::unordered_map<Instruction *, std::size_t> &getUses() { return uses; }
const std::unordered_map<Instruction *, std::size_t> &getUses() const {
return uses;
}
void addUse(Instruction *i) {
auto [it, inserted] = uses.try_emplace(i, 1);
if (!inserted)
it->second += 1;
}
/// Remove a use of the instruction from te use list.
void delUse(Instruction *instruction) {
assert(uses.contains(instruction) && "No uses to remove");
auto it = uses.find(instruction);
it->second -= 1;
if (it->second == 0)
uses.erase(it);
}
};
class Parameter : public Value {
public:
Parameter(std::string name, Type type)
: Value(ValueKind::Parameter, std::move(name), type) {}
};
} // namespace willow
#endif // WILLOW_INCLUDE_IR_VALUE_H
|