summaryrefslogtreecommitdiff
path: root/willow/lib/IR/BasicBlock.cpp
blob: ceb46536cb5a82734c6107b6878e5563d6499f60 (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
#include <willow/IR/BasicBlock.h>

namespace willow {

void BasicBlock::push_back(Instruction &inst) {
  assert(!inst.hasParent() && "Instruction is already parented");
  body.push_back(inst);
  inst.setParent(this);
}

void BasicBlock::push_front(Instruction &inst) {
  assert(!inst.hasParent() && "Instruction is already parented");
  body.push_front(inst);
  inst.setParent(this);
}

BasicBlock::Iterator BasicBlock::insert(Iterator pos, Instruction &inst) {
  assert(!inst.hasParent() && "Instruction is already parented");
  auto it = body.insert(pos, inst);
  inst.setParent(this);
  return it;
}

BasicBlock::Iterator BasicBlock::erase(BasicBlock::Iterator pos) {
  Instruction &I = *pos;
  I.setParent(nullptr);
  return body.erase(pos);
}

BasicBlock::Iterator BasicBlock::eraseAndDelete(BasicBlock::Iterator pos) {
  Instruction &inst = *pos;
  pos->setParent(nullptr);
  auto it = body.erase(pos);
  delete &inst;
  return it;
}

void BasicBlock::addPred(BasicBlock *bb) {
  auto [it, inserted] = predecessors.try_emplace(bb, 1);

  if (!inserted)
    it->second += 1;
}

void BasicBlock::delPred(BasicBlock *bb) {
  auto it = preds().find(bb);

  it->second -= 1;

  if (it->second <= 0)
    preds().erase(it);
}

} // namespace willow