1 #include "tir/BasicBlock.h"
5 #include "tir/Constant.h"
6 #include "tir/Context.h"
7 #include "tir/Instructions.h"
8 #include "utils/DotPrinter.h"
12 BasicBlock::BasicBlock(
Context& ctx, Function* parent)
17 parent->addBlock(
this);
21 void BasicBlock::iterator_pimpl::next() {
28 auto* next = inst->next();
37 void BasicBlock::iterator_pimpl::prev() {
44 auto* prev = inst->prev();
53 void BasicBlock::appendAfterEnd(
Instruction* instr) {
55 instr->insertAfter(last_);
58 first_ = last_ = instr;
60 instr->parent_ =
this;
63 void BasicBlock::insertBeforeBegin(
Instruction* instr) {
65 instr->insertBefore(first_);
68 first_ = last_ = instr;
70 instr->parent_ =
this;
73 std::ostream& BasicBlock::print(std::ostream& os)
const {
75 for(
auto inst : *
this) {
83 assert(instr->parent_ ==
this &&
"Instruction does not belong to this BB");
87 void BasicBlock::eraseFromParent() {
88 if(parent_ ==
nullptr)
return;
89 parent_->removeBlock(
this);
92 int BasicBlock::printDotNode(utils::
DotPrinter& dp)
const {
95 std::ostringstream os;
98 if(
auto term = dyn_cast_or_null<BranchInst>(terminator())) {
99 if(term->getSuccessor(0) != term->getSuccessor(1))
106 utils::
Generator<BasicBlock*> BasicBlock::successors()
const {
107 assert(terminator() &&
"Basic block has no terminator");
108 auto term = dyn_cast<BranchInst>(terminator());
110 co_yield term->getSuccessor(0);
111 if(term->getSuccessor(0) != term->getSuccessor(1)) {
112 co_yield term->getSuccessor(1);
117 utils::
Generator<BasicBlock*> BasicBlock::predecessors()
const {
118 std::unordered_set<BasicBlock*> visited;
119 for(
auto user : users()) {
120 if(
auto term = dyn_cast<BranchInst>(user)) {
121 visited.insert(term->parent());
124 for(
auto pred : visited) {